blob: 5fb0306f45899f39aa608a87c1c9bdf2642d403d [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
drh9e572e62004-04-23 23:43:10 +00002** 2004 April 6
drha059ad02001-04-17 20:09:11 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drha059ad02001-04-17 20:09:11 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drha059ad02001-04-17 20:09:11 +000010**
11*************************************************************************
drh076d4662009-02-18 20:31:18 +000012** $Id: btree.c,v 1.566 2009/02/18 20:31:18 drh Exp $
drh8b2f49b2001-06-08 00:21:52 +000013**
14** This file implements a external (disk-based) database using BTrees.
drha3152892007-05-05 11:48:52 +000015** See the header comment on "btreeInt.h" for additional information.
16** Including a description of file format and an overview of operation.
drha059ad02001-04-17 20:09:11 +000017*/
drha3152892007-05-05 11:48:52 +000018#include "btreeInt.h"
paulb95a8862003-04-01 21:16:41 +000019
drh8c42ca92001-06-22 19:15:00 +000020/*
drha3152892007-05-05 11:48:52 +000021** The header string that appears at the beginning of every
22** SQLite database.
drh556b2a22005-06-14 16:04:05 +000023*/
drh556b2a22005-06-14 16:04:05 +000024static const char zMagicHeader[] = SQLITE_FILE_HEADER;
drh08ed44e2001-04-29 23:32:55 +000025
drh8c42ca92001-06-22 19:15:00 +000026/*
drha3152892007-05-05 11:48:52 +000027** Set this global variable to 1 to enable tracing using the TRACE
28** macro.
drh615ae552005-01-16 23:21:00 +000029*/
drhe8f52c52008-07-12 14:52:20 +000030#if 0
mlcreech3a00f902008-03-04 17:45:01 +000031int sqlite3BtreeTrace=0; /* True to enable tracing */
drhe8f52c52008-07-12 14:52:20 +000032# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
33#else
34# define TRACE(X)
drh615ae552005-01-16 23:21:00 +000035#endif
drh615ae552005-01-16 23:21:00 +000036
drh86f8c192007-08-22 00:39:19 +000037
38
drhe53831d2007-08-17 01:14:38 +000039#ifndef SQLITE_OMIT_SHARED_CACHE
40/*
danielk1977502b4e02008-09-02 14:07:24 +000041** A list of BtShared objects that are eligible for participation
42** in shared cache. This variable has file scope during normal builds,
43** but the test harness needs to access it so we make it global for
44** test builds.
drhe53831d2007-08-17 01:14:38 +000045*/
46#ifdef SQLITE_TEST
drh78f82d12008-09-02 00:52:52 +000047BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:38 +000048#else
drh78f82d12008-09-02 00:52:52 +000049static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:38 +000050#endif
drhe53831d2007-08-17 01:14:38 +000051#endif /* SQLITE_OMIT_SHARED_CACHE */
52
53#ifndef SQLITE_OMIT_SHARED_CACHE
54/*
55** Enable or disable the shared pager and schema features.
56**
57** This routine has no effect on existing database connections.
58** The shared cache setting effects only future calls to
59** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
60*/
61int sqlite3_enable_shared_cache(int enable){
danielk1977502b4e02008-09-02 14:07:24 +000062 sqlite3GlobalConfig.sharedCacheEnabled = enable;
drhe53831d2007-08-17 01:14:38 +000063 return SQLITE_OK;
64}
65#endif
66
drhd677b3d2007-08-20 22:48:41 +000067
drh615ae552005-01-16 23:21:00 +000068/*
drh66cbd152004-09-01 16:12:25 +000069** Forward declaration
70*/
danielk19773588ceb2008-06-10 17:30:26 +000071static int checkReadLocks(Btree*, Pgno, BtCursor*, i64);
drh66cbd152004-09-01 16:12:25 +000072
danielk1977aef0bf62005-12-30 16:28:01 +000073
74#ifdef SQLITE_OMIT_SHARED_CACHE
75 /*
76 ** The functions queryTableLock(), lockTable() and unlockAllTables()
77 ** manipulate entries in the BtShared.pLock linked list used to store
78 ** shared-cache table level locks. If the library is compiled with the
79 ** shared-cache feature disabled, then there is only ever one user
danielk1977da184232006-01-05 11:34:32 +000080 ** of each BtShared structure and so this locking is not necessary.
81 ** So define the lock related functions as no-ops.
danielk1977aef0bf62005-12-30 16:28:01 +000082 */
83 #define queryTableLock(a,b,c) SQLITE_OK
84 #define lockTable(a,b,c) SQLITE_OK
danielk1977da184232006-01-05 11:34:32 +000085 #define unlockAllTables(a)
drhe53831d2007-08-17 01:14:38 +000086#endif
danielk1977aef0bf62005-12-30 16:28:01 +000087
drhe53831d2007-08-17 01:14:38 +000088#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977da184232006-01-05 11:34:32 +000089/*
danielk1977aef0bf62005-12-30 16:28:01 +000090** Query to see if btree handle p may obtain a lock of type eLock
91** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
92** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
danielk1977c87d34d2006-01-06 13:00:28 +000093** SQLITE_LOCKED if not.
danielk1977aef0bf62005-12-30 16:28:01 +000094*/
95static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
96 BtShared *pBt = p->pBt;
97 BtLock *pIter;
98
drh1fee73e2007-08-29 04:00:57 +000099 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40 +0000100 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
101 assert( p->db!=0 );
drhd677b3d2007-08-20 22:48:41 +0000102
danielk1977da184232006-01-05 11:34:32 +0000103 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000104 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000105 return SQLITE_OK;
106 }
107
danielk1977641b0f42007-12-21 04:47:25 +0000108 /* If some other connection is holding an exclusive lock, the
109 ** requested lock may not be obtained.
110 */
111 if( pBt->pExclusive && pBt->pExclusive!=p ){
112 return SQLITE_LOCKED;
113 }
114
danielk1977da184232006-01-05 11:34:32 +0000115 /* This (along with lockTable()) is where the ReadUncommitted flag is
116 ** dealt with. If the caller is querying for a read-lock and the flag is
117 ** set, it is unconditionally granted - even if there are write-locks
118 ** on the table. If a write-lock is requested, the ReadUncommitted flag
119 ** is not considered.
120 **
121 ** In function lockTable(), if a read-lock is demanded and the
122 ** ReadUncommitted flag is set, no entry is added to the locks list
123 ** (BtShared.pLock).
124 **
125 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
126 ** not create or respect table locks. The locking procedure for a
127 ** write-cursor does not change.
128 */
129 if(
drhe5fe6902007-12-07 18:55:28 +0000130 0==(p->db->flags&SQLITE_ReadUncommitted) ||
danielk1977da184232006-01-05 11:34:32 +0000131 eLock==WRITE_LOCK ||
drh47ded162006-01-06 01:42:58 +0000132 iTab==MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000133 ){
134 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
135 if( pIter->pBtree!=p && pIter->iTable==iTab &&
136 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
danielk1977c87d34d2006-01-06 13:00:28 +0000137 return SQLITE_LOCKED;
danielk1977da184232006-01-05 11:34:32 +0000138 }
danielk1977aef0bf62005-12-30 16:28:01 +0000139 }
140 }
141 return SQLITE_OK;
142}
drhe53831d2007-08-17 01:14:38 +0000143#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000144
drhe53831d2007-08-17 01:14:38 +0000145#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000146/*
147** Add a lock on the table with root-page iTable to the shared-btree used
148** by Btree handle p. Parameter eLock must be either READ_LOCK or
149** WRITE_LOCK.
150**
151** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
152** SQLITE_NOMEM may also be returned.
153*/
154static int lockTable(Btree *p, Pgno iTable, u8 eLock){
155 BtShared *pBt = p->pBt;
156 BtLock *pLock = 0;
157 BtLock *pIter;
158
drh1fee73e2007-08-29 04:00:57 +0000159 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40 +0000160 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
161 assert( p->db!=0 );
drhd677b3d2007-08-20 22:48:41 +0000162
danielk1977da184232006-01-05 11:34:32 +0000163 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000164 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000165 return SQLITE_OK;
166 }
167
danielk1977aef0bf62005-12-30 16:28:01 +0000168 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
169
danielk1977da184232006-01-05 11:34:32 +0000170 /* If the read-uncommitted flag is set and a read-lock is requested,
171 ** return early without adding an entry to the BtShared.pLock list. See
172 ** comment in function queryTableLock() for more info on handling
173 ** the ReadUncommitted flag.
174 */
175 if(
drhe5fe6902007-12-07 18:55:28 +0000176 (p->db->flags&SQLITE_ReadUncommitted) &&
danielk1977da184232006-01-05 11:34:32 +0000177 (eLock==READ_LOCK) &&
drh47ded162006-01-06 01:42:58 +0000178 iTable!=MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000179 ){
180 return SQLITE_OK;
181 }
182
danielk1977aef0bf62005-12-30 16:28:01 +0000183 /* First search the list for an existing lock on this table. */
184 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
185 if( pIter->iTable==iTable && pIter->pBtree==p ){
186 pLock = pIter;
187 break;
188 }
189 }
190
191 /* If the above search did not find a BtLock struct associating Btree p
192 ** with table iTable, allocate one and link it into the list.
193 */
194 if( !pLock ){
drh17435752007-08-16 04:30:38 +0000195 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
danielk1977aef0bf62005-12-30 16:28:01 +0000196 if( !pLock ){
197 return SQLITE_NOMEM;
198 }
199 pLock->iTable = iTable;
200 pLock->pBtree = p;
201 pLock->pNext = pBt->pLock;
202 pBt->pLock = pLock;
203 }
204
205 /* Set the BtLock.eLock variable to the maximum of the current lock
206 ** and the requested lock. This means if a write-lock was already held
207 ** and a read-lock requested, we don't incorrectly downgrade the lock.
208 */
209 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53 +0000210 if( eLock>pLock->eLock ){
211 pLock->eLock = eLock;
212 }
danielk1977aef0bf62005-12-30 16:28:01 +0000213
214 return SQLITE_OK;
215}
drhe53831d2007-08-17 01:14:38 +0000216#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000217
drhe53831d2007-08-17 01:14:38 +0000218#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000219/*
220** Release all the table locks (locks obtained via calls to the lockTable()
221** procedure) held by Btree handle p.
222*/
223static void unlockAllTables(Btree *p){
danielk1977641b0f42007-12-21 04:47:25 +0000224 BtShared *pBt = p->pBt;
225 BtLock **ppIter = &pBt->pLock;
danielk1977da184232006-01-05 11:34:32 +0000226
drh1fee73e2007-08-29 04:00:57 +0000227 assert( sqlite3BtreeHoldsMutex(p) );
drhe53831d2007-08-17 01:14:38 +0000228 assert( p->sharable || 0==*ppIter );
danielk1977da184232006-01-05 11:34:32 +0000229
danielk1977aef0bf62005-12-30 16:28:01 +0000230 while( *ppIter ){
231 BtLock *pLock = *ppIter;
danielk1977641b0f42007-12-21 04:47:25 +0000232 assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree );
danielk1977aef0bf62005-12-30 16:28:01 +0000233 if( pLock->pBtree==p ){
234 *ppIter = pLock->pNext;
drh17435752007-08-16 04:30:38 +0000235 sqlite3_free(pLock);
danielk1977aef0bf62005-12-30 16:28:01 +0000236 }else{
237 ppIter = &pLock->pNext;
238 }
239 }
danielk1977641b0f42007-12-21 04:47:25 +0000240
241 if( pBt->pExclusive==p ){
242 pBt->pExclusive = 0;
243 }
danielk1977aef0bf62005-12-30 16:28:01 +0000244}
245#endif /* SQLITE_OMIT_SHARED_CACHE */
246
drh980b1a72006-08-16 16:42:48 +0000247static void releasePage(MemPage *pPage); /* Forward reference */
248
drh1fee73e2007-08-29 04:00:57 +0000249/*
250** Verify that the cursor holds a mutex on the BtShared
251*/
252#ifndef NDEBUG
253static int cursorHoldsMutex(BtCursor *p){
drhff0587c2007-08-29 17:43:19 +0000254 return sqlite3_mutex_held(p->pBt->mutex);
drh1fee73e2007-08-29 04:00:57 +0000255}
256#endif
257
258
danielk197792d4d7a2007-05-04 12:05:56 +0000259#ifndef SQLITE_OMIT_INCRBLOB
260/*
261** Invalidate the overflow page-list cache for cursor pCur, if any.
262*/
263static void invalidateOverflowCache(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000264 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000265 sqlite3_free(pCur->aOverflow);
danielk197792d4d7a2007-05-04 12:05:56 +0000266 pCur->aOverflow = 0;
267}
268
269/*
270** Invalidate the overflow page-list cache for all cursors opened
271** on the shared btree structure pBt.
272*/
273static void invalidateAllOverflowCache(BtShared *pBt){
274 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000275 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +0000276 for(p=pBt->pCursor; p; p=p->pNext){
277 invalidateOverflowCache(p);
278 }
279}
280#else
281 #define invalidateOverflowCache(x)
282 #define invalidateAllOverflowCache(x)
283#endif
284
drh980b1a72006-08-16 16:42:48 +0000285/*
danielk1977bea2a942009-01-20 17:06:27 +0000286** Set bit pgno of the BtShared.pHasContent bitvec. This is called
287** when a page that previously contained data becomes a free-list leaf
288** page.
289**
290** The BtShared.pHasContent bitvec exists to work around an obscure
291** bug caused by the interaction of two useful IO optimizations surrounding
292** free-list leaf pages:
293**
294** 1) When all data is deleted from a page and the page becomes
295** a free-list leaf page, the page is not written to the database
296** (as free-list leaf pages contain no meaningful data). Sometimes
297** such a page is not even journalled (as it will not be modified,
298** why bother journalling it?).
299**
300** 2) When a free-list leaf page is reused, its content is not read
301** from the database or written to the journal file (why should it
302** be, if it is not at all meaningful?).
303**
304** By themselves, these optimizations work fine and provide a handy
305** performance boost to bulk delete or insert operations. However, if
306** a page is moved to the free-list and then reused within the same
307** transaction, a problem comes up. If the page is not journalled when
308** it is moved to the free-list and it is also not journalled when it
309** is extracted from the free-list and reused, then the original data
310** may be lost. In the event of a rollback, it may not be possible
311** to restore the database to its original configuration.
312**
313** The solution is the BtShared.pHasContent bitvec. Whenever a page is
314** moved to become a free-list leaf page, the corresponding bit is
315** set in the bitvec. Whenever a leaf page is extracted from the free-list,
316** optimization 2 above is ommitted if the corresponding bit is already
317** set in BtShared.pHasContent. The contents of the bitvec are cleared
318** at the end of every transaction.
319*/
320static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
321 int rc = SQLITE_OK;
322 if( !pBt->pHasContent ){
323 int nPage;
324 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
325 if( rc==SQLITE_OK ){
326 pBt->pHasContent = sqlite3BitvecCreate((u32)nPage);
327 if( !pBt->pHasContent ){
328 rc = SQLITE_NOMEM;
329 }
330 }
331 }
332 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
333 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
334 }
335 return rc;
336}
337
338/*
339** Query the BtShared.pHasContent vector.
340**
341** This function is called when a free-list leaf page is removed from the
342** free-list for reuse. It returns false if it is safe to retrieve the
343** page from the pager layer with the 'no-content' flag set. True otherwise.
344*/
345static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
346 Bitvec *p = pBt->pHasContent;
347 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
348}
349
350/*
351** Clear (destroy) the BtShared.pHasContent bitvec. This should be
352** invoked at the conclusion of each write-transaction.
353*/
354static void btreeClearHasContent(BtShared *pBt){
355 sqlite3BitvecDestroy(pBt->pHasContent);
356 pBt->pHasContent = 0;
357}
358
359/*
drh980b1a72006-08-16 16:42:48 +0000360** Save the current cursor position in the variables BtCursor.nKey
361** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
362*/
363static int saveCursorPosition(BtCursor *pCur){
364 int rc;
365
366 assert( CURSOR_VALID==pCur->eState );
367 assert( 0==pCur->pKey );
drh1fee73e2007-08-29 04:00:57 +0000368 assert( cursorHoldsMutex(pCur) );
drh980b1a72006-08-16 16:42:48 +0000369
370 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
371
372 /* If this is an intKey table, then the above call to BtreeKeySize()
373 ** stores the integer key in pCur->nKey. In this case this value is
374 ** all that is required. Otherwise, if pCur is not open on an intKey
375 ** table, then malloc space for and store the pCur->nKey bytes of key
376 ** data.
377 */
danielk197771d5d2c2008-09-29 11:49:47 +0000378 if( rc==SQLITE_OK && 0==pCur->apPage[0]->intKey){
drhf49661a2008-12-10 16:45:50 +0000379 void *pKey = sqlite3Malloc( (int)pCur->nKey );
drh980b1a72006-08-16 16:42:48 +0000380 if( pKey ){
drhf49661a2008-12-10 16:45:50 +0000381 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
drh980b1a72006-08-16 16:42:48 +0000382 if( rc==SQLITE_OK ){
383 pCur->pKey = pKey;
384 }else{
drh17435752007-08-16 04:30:38 +0000385 sqlite3_free(pKey);
drh980b1a72006-08-16 16:42:48 +0000386 }
387 }else{
388 rc = SQLITE_NOMEM;
389 }
390 }
danielk197771d5d2c2008-09-29 11:49:47 +0000391 assert( !pCur->apPage[0]->intKey || !pCur->pKey );
drh980b1a72006-08-16 16:42:48 +0000392
393 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +0000394 int i;
395 for(i=0; i<=pCur->iPage; i++){
396 releasePage(pCur->apPage[i]);
397 pCur->apPage[i] = 0;
398 }
399 pCur->iPage = -1;
drh980b1a72006-08-16 16:42:48 +0000400 pCur->eState = CURSOR_REQUIRESEEK;
401 }
402
danielk197792d4d7a2007-05-04 12:05:56 +0000403 invalidateOverflowCache(pCur);
drh980b1a72006-08-16 16:42:48 +0000404 return rc;
405}
406
407/*
408** Save the positions of all cursors except pExcept open on the table
409** with root-page iRoot. Usually, this is called just before cursor
410** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
411*/
412static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
413 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000414 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +0000415 assert( pExcept==0 || pExcept->pBt==pBt );
drh980b1a72006-08-16 16:42:48 +0000416 for(p=pBt->pCursor; p; p=p->pNext){
417 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
418 p->eState==CURSOR_VALID ){
419 int rc = saveCursorPosition(p);
420 if( SQLITE_OK!=rc ){
421 return rc;
422 }
423 }
424 }
425 return SQLITE_OK;
426}
427
428/*
drhbf700f32007-03-31 02:36:44 +0000429** Clear the current cursor position.
430*/
danielk1977be51a652008-10-08 17:58:48 +0000431void sqlite3BtreeClearCursor(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000432 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000433 sqlite3_free(pCur->pKey);
drhbf700f32007-03-31 02:36:44 +0000434 pCur->pKey = 0;
435 pCur->eState = CURSOR_INVALID;
436}
437
438/*
drh980b1a72006-08-16 16:42:48 +0000439** Restore the cursor to the position it was in (or as close to as possible)
440** when saveCursorPosition() was called. Note that this call deletes the
441** saved position info stored by saveCursorPosition(), so there can be
drha3460582008-07-11 21:02:53 +0000442** at most one effective restoreCursorPosition() call after each
drh980b1a72006-08-16 16:42:48 +0000443** saveCursorPosition().
drh980b1a72006-08-16 16:42:48 +0000444*/
drha3460582008-07-11 21:02:53 +0000445int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){
drhbf700f32007-03-31 02:36:44 +0000446 int rc;
drh1fee73e2007-08-29 04:00:57 +0000447 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +0000448 assert( pCur->eState>=CURSOR_REQUIRESEEK );
449 if( pCur->eState==CURSOR_FAULT ){
450 return pCur->skip;
451 }
drh980b1a72006-08-16 16:42:48 +0000452 pCur->eState = CURSOR_INVALID;
drhe63d9992008-08-13 19:11:48 +0000453 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
drh980b1a72006-08-16 16:42:48 +0000454 if( rc==SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000455 sqlite3_free(pCur->pKey);
drh980b1a72006-08-16 16:42:48 +0000456 pCur->pKey = 0;
drhbf700f32007-03-31 02:36:44 +0000457 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
drh980b1a72006-08-16 16:42:48 +0000458 }
459 return rc;
460}
461
drha3460582008-07-11 21:02:53 +0000462#define restoreCursorPosition(p) \
drhfb982642007-08-30 01:19:59 +0000463 (p->eState>=CURSOR_REQUIRESEEK ? \
drha3460582008-07-11 21:02:53 +0000464 sqlite3BtreeRestoreCursorPosition(p) : \
drh16a9b832007-05-05 18:39:25 +0000465 SQLITE_OK)
drh980b1a72006-08-16 16:42:48 +0000466
drha3460582008-07-11 21:02:53 +0000467/*
468** Determine whether or not a cursor has moved from the position it
drhdfe88ec2008-11-03 20:55:06 +0000469** was last placed at. Cursors can move when the row they are pointing
drha3460582008-07-11 21:02:53 +0000470** at is deleted out from under them.
471**
472** This routine returns an error code if something goes wrong. The
473** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
474*/
475int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
476 int rc;
477
478 rc = restoreCursorPosition(pCur);
479 if( rc ){
480 *pHasMoved = 1;
481 return rc;
482 }
483 if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){
484 *pHasMoved = 1;
485 }else{
486 *pHasMoved = 0;
487 }
488 return SQLITE_OK;
489}
490
danielk1977599fcba2004-11-08 07:13:13 +0000491#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000492/*
drha3152892007-05-05 11:48:52 +0000493** Given a page number of a regular database page, return the page
494** number for the pointer-map page that contains the entry for the
495** input page number.
danielk1977afcdd022004-10-31 16:25:42 +0000496*/
danielk1977266664d2006-02-10 08:24:21 +0000497static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
danielk197789d40042008-11-17 14:20:56 +0000498 int nPagesPerMapPage;
499 Pgno iPtrMap, ret;
drh1fee73e2007-08-29 04:00:57 +0000500 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000501 nPagesPerMapPage = (pBt->usableSize/5)+1;
502 iPtrMap = (pgno-2)/nPagesPerMapPage;
503 ret = (iPtrMap*nPagesPerMapPage) + 2;
danielk1977266664d2006-02-10 08:24:21 +0000504 if( ret==PENDING_BYTE_PAGE(pBt) ){
505 ret++;
506 }
507 return ret;
508}
danielk1977a19df672004-11-03 11:37:07 +0000509
danielk1977afcdd022004-10-31 16:25:42 +0000510/*
danielk1977afcdd022004-10-31 16:25:42 +0000511** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000512**
513** This routine updates the pointer map entry for page number 'key'
514** so that it maps to type 'eType' and parent page number 'pgno'.
515** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000516*/
danielk1977aef0bf62005-12-30 16:28:01 +0000517static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
danielk19773b8a05f2007-03-19 17:44:26 +0000518 DbPage *pDbPage; /* The pointer map page */
519 u8 *pPtrmap; /* The pointer map data */
520 Pgno iPtrmap; /* The pointer map page number */
521 int offset; /* Offset in pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000522 int rc;
523
drh1fee73e2007-08-29 04:00:57 +0000524 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977266664d2006-02-10 08:24:21 +0000525 /* The master-journal page number must never be used as a pointer map page */
526 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
527
danielk1977ac11ee62005-01-15 12:45:51 +0000528 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000529 if( key==0 ){
drh49285702005-09-17 15:20:26 +0000530 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +0000531 }
danielk1977266664d2006-02-10 08:24:21 +0000532 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000533 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977687566d2004-11-02 12:56:41 +0000534 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000535 return rc;
536 }
danielk19778c666b12008-07-18 09:34:57 +0000537 offset = PTRMAP_PTROFFSET(iPtrmap, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000538 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000539
drh615ae552005-01-16 23:21:00 +0000540 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
541 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk19773b8a05f2007-03-19 17:44:26 +0000542 rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000543 if( rc==SQLITE_OK ){
544 pPtrmap[offset] = eType;
545 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000546 }
danielk1977afcdd022004-10-31 16:25:42 +0000547 }
548
danielk19773b8a05f2007-03-19 17:44:26 +0000549 sqlite3PagerUnref(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000550 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000551}
552
553/*
554** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000555**
556** This routine retrieves the pointer map entry for page 'key', writing
557** the type and parent page number to *pEType and *pPgno respectively.
558** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000559*/
danielk1977aef0bf62005-12-30 16:28:01 +0000560static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:26 +0000561 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000562 int iPtrmap; /* Pointer map page index */
563 u8 *pPtrmap; /* Pointer map page data */
564 int offset; /* Offset of entry in pointer map */
565 int rc;
566
drh1fee73e2007-08-29 04:00:57 +0000567 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000568
danielk1977266664d2006-02-10 08:24:21 +0000569 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000570 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000571 if( rc!=0 ){
572 return rc;
573 }
danielk19773b8a05f2007-03-19 17:44:26 +0000574 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000575
danielk19778c666b12008-07-18 09:34:57 +0000576 offset = PTRMAP_PTROFFSET(iPtrmap, key);
drh43617e92006-03-06 20:55:46 +0000577 assert( pEType!=0 );
578 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:41 +0000579 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000580
danielk19773b8a05f2007-03-19 17:44:26 +0000581 sqlite3PagerUnref(pDbPage);
drh49285702005-09-17 15:20:26 +0000582 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
danielk1977afcdd022004-10-31 16:25:42 +0000583 return SQLITE_OK;
584}
585
danielk197785d90ca2008-07-19 14:25:15 +0000586#else /* if defined SQLITE_OMIT_AUTOVACUUM */
587 #define ptrmapPut(w,x,y,z) SQLITE_OK
588 #define ptrmapGet(w,x,y,z) SQLITE_OK
589 #define ptrmapPutOvfl(y,z) SQLITE_OK
590#endif
danielk1977afcdd022004-10-31 16:25:42 +0000591
drh0d316a42002-08-11 20:10:47 +0000592/*
drh271efa52004-05-30 19:19:05 +0000593** Given a btree page and a cell index (0 means the first cell on
594** the page, 1 means the second cell, and so forth) return a pointer
595** to the cell content.
596**
597** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000598*/
drh1688c862008-07-18 02:44:17 +0000599#define findCell(P,I) \
600 ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
drh43605152004-05-29 21:46:49 +0000601
602/*
drh93a960a2008-07-10 00:32:42 +0000603** This a more complex version of findCell() that works for
drh43605152004-05-29 21:46:49 +0000604** pages that do contain overflow cells. See insert
605*/
606static u8 *findOverflowCell(MemPage *pPage, int iCell){
607 int i;
drh1fee73e2007-08-29 04:00:57 +0000608 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +0000609 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000610 int k;
611 struct _OvflCell *pOvfl;
612 pOvfl = &pPage->aOvfl[i];
613 k = pOvfl->idx;
614 if( k<=iCell ){
615 if( k==iCell ){
616 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000617 }
618 iCell--;
619 }
620 }
danielk19771cc5ed82007-05-16 17:28:43 +0000621 return findCell(pPage, iCell);
drh43605152004-05-29 21:46:49 +0000622}
623
624/*
625** Parse a cell content block and fill in the CellInfo structure. There
drh16a9b832007-05-05 18:39:25 +0000626** are two versions of this function. sqlite3BtreeParseCell() takes a
627** cell index as the second argument and sqlite3BtreeParseCellPtr()
628** takes a pointer to the body of the cell as its second argument.
danielk19771cc5ed82007-05-16 17:28:43 +0000629**
630** Within this file, the parseCell() macro can be called instead of
631** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
drh43605152004-05-29 21:46:49 +0000632*/
drh16a9b832007-05-05 18:39:25 +0000633void sqlite3BtreeParseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000634 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000635 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000636 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000637){
drhf49661a2008-12-10 16:45:50 +0000638 u16 n; /* Number bytes in cell content header */
drh271efa52004-05-30 19:19:05 +0000639 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000640
drh1fee73e2007-08-29 04:00:57 +0000641 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000642
drh43605152004-05-29 21:46:49 +0000643 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000644 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000645 n = pPage->childPtrSize;
646 assert( n==4-4*pPage->leaf );
drh504b6982006-01-22 21:52:56 +0000647 if( pPage->intKey ){
drh79df1f42008-07-18 00:57:33 +0000648 if( pPage->hasData ){
649 n += getVarint32(&pCell[n], nPayload);
650 }else{
651 nPayload = 0;
652 }
drh1bd10f82008-12-10 21:19:56 +0000653 n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
drh79df1f42008-07-18 00:57:33 +0000654 pInfo->nData = nPayload;
drh504b6982006-01-22 21:52:56 +0000655 }else{
drh79df1f42008-07-18 00:57:33 +0000656 pInfo->nData = 0;
657 n += getVarint32(&pCell[n], nPayload);
658 pInfo->nKey = nPayload;
drh6f11bef2004-05-13 01:12:56 +0000659 }
drh72365832007-03-06 15:53:44 +0000660 pInfo->nPayload = nPayload;
drh504b6982006-01-22 21:52:56 +0000661 pInfo->nHeader = n;
drh79df1f42008-07-18 00:57:33 +0000662 if( likely(nPayload<=pPage->maxLocal) ){
drh271efa52004-05-30 19:19:05 +0000663 /* This is the (easy) common case where the entire payload fits
664 ** on the local page. No overflow is required.
665 */
666 int nSize; /* Total size of cell content in bytes */
drh79df1f42008-07-18 00:57:33 +0000667 nSize = nPayload + n;
drhf49661a2008-12-10 16:45:50 +0000668 pInfo->nLocal = (u16)nPayload;
drh6f11bef2004-05-13 01:12:56 +0000669 pInfo->iOverflow = 0;
drh79df1f42008-07-18 00:57:33 +0000670 if( (nSize & ~3)==0 ){
drh271efa52004-05-30 19:19:05 +0000671 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000672 }
drh1bd10f82008-12-10 21:19:56 +0000673 pInfo->nSize = (u16)nSize;
drh6f11bef2004-05-13 01:12:56 +0000674 }else{
drh271efa52004-05-30 19:19:05 +0000675 /* If the payload will not fit completely on the local page, we have
676 ** to decide how much to store locally and how much to spill onto
677 ** overflow pages. The strategy is to minimize the amount of unused
678 ** space on overflow pages while keeping the amount of local storage
679 ** in between minLocal and maxLocal.
680 **
681 ** Warning: changing the way overflow payload is distributed in any
682 ** way will result in an incompatible file format.
683 */
684 int minLocal; /* Minimum amount of payload held locally */
685 int maxLocal; /* Maximum amount of payload held locally */
686 int surplus; /* Overflow payload available for local storage */
687
688 minLocal = pPage->minLocal;
689 maxLocal = pPage->maxLocal;
690 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000691 if( surplus <= maxLocal ){
drhf49661a2008-12-10 16:45:50 +0000692 pInfo->nLocal = (u16)surplus;
drh6f11bef2004-05-13 01:12:56 +0000693 }else{
drhf49661a2008-12-10 16:45:50 +0000694 pInfo->nLocal = (u16)minLocal;
drh6f11bef2004-05-13 01:12:56 +0000695 }
drhf49661a2008-12-10 16:45:50 +0000696 pInfo->iOverflow = (u16)(pInfo->nLocal + n);
drh6f11bef2004-05-13 01:12:56 +0000697 pInfo->nSize = pInfo->iOverflow + 4;
698 }
drh3aac2dd2004-04-26 14:10:20 +0000699}
danielk19771cc5ed82007-05-16 17:28:43 +0000700#define parseCell(pPage, iCell, pInfo) \
701 sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
drh16a9b832007-05-05 18:39:25 +0000702void sqlite3BtreeParseCell(
drh43605152004-05-29 21:46:49 +0000703 MemPage *pPage, /* Page containing the cell */
704 int iCell, /* The cell index. First cell is 0 */
705 CellInfo *pInfo /* Fill in this structure */
706){
danielk19771cc5ed82007-05-16 17:28:43 +0000707 parseCell(pPage, iCell, pInfo);
drh43605152004-05-29 21:46:49 +0000708}
drh3aac2dd2004-04-26 14:10:20 +0000709
710/*
drh43605152004-05-29 21:46:49 +0000711** Compute the total number of bytes that a Cell needs in the cell
712** data area of the btree-page. The return number includes the cell
713** data header and the local payload, but not any overflow page or
714** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +0000715*/
danielk1977bc6ada42004-06-30 08:20:16 +0000716#ifndef NDEBUG
drha9121e42008-02-19 14:59:35 +0000717static u16 cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +0000718 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000719 sqlite3BtreeParseCell(pPage, iCell, &info);
drh43605152004-05-29 21:46:49 +0000720 return info.nSize;
721}
danielk1977bc6ada42004-06-30 08:20:16 +0000722#endif
drha9121e42008-02-19 14:59:35 +0000723static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
drh43605152004-05-29 21:46:49 +0000724 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000725 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +0000726 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +0000727}
728
danielk197779a40da2005-01-16 08:00:01 +0000729#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +0000730/*
danielk197726836652005-01-17 01:33:13 +0000731** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +0000732** to an overflow page, insert an entry into the pointer-map
733** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +0000734*/
danielk197726836652005-01-17 01:33:13 +0000735static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
drhfa67c3c2008-07-11 02:21:40 +0000736 CellInfo info;
737 assert( pCell!=0 );
738 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
739 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
740 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
741 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
742 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +0000743 }
danielk197779a40da2005-01-16 08:00:01 +0000744 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +0000745}
danielk197726836652005-01-17 01:33:13 +0000746/*
747** If the cell with index iCell on page pPage contains a pointer
748** to an overflow page, insert an entry into the pointer-map
749** for the overflow page.
750*/
751static int ptrmapPutOvfl(MemPage *pPage, int iCell){
752 u8 *pCell;
drh1fee73e2007-08-29 04:00:57 +0000753 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197726836652005-01-17 01:33:13 +0000754 pCell = findOverflowCell(pPage, iCell);
755 return ptrmapPutOvflPtr(pPage, pCell);
756}
danielk197779a40da2005-01-16 08:00:01 +0000757#endif
758
danielk1977ac11ee62005-01-15 12:45:51 +0000759
drhda200cc2004-05-09 11:51:38 +0000760/*
drh72f82862001-05-24 21:06:34 +0000761** Defragment the page given. All Cells are moved to the
drh3a4a2d42005-11-24 14:24:28 +0000762** end of the page and all free space is collected into one
763** big FreeBlk that occurs in between the header and cell
drh31beae92005-11-24 14:34:36 +0000764** pointer array and the cell content area.
drh365d68f2001-05-11 11:02:46 +0000765*/
shane0af3f892008-11-12 04:55:34 +0000766static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +0000767 int i; /* Loop counter */
768 int pc; /* Address of a i-th cell */
769 int addr; /* Offset of first byte after cell pointer array */
770 int hdr; /* Offset to the page header */
771 int size; /* Size of a cell */
772 int usableSize; /* Number of usable bytes on a page */
773 int cellOffset; /* Offset to the cell pointer array */
drh281b21d2008-08-22 12:57:08 +0000774 int cbrk; /* Offset to the cell content area */
drh43605152004-05-29 21:46:49 +0000775 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +0000776 unsigned char *data; /* The page data */
777 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +0000778
danielk19773b8a05f2007-03-19 17:44:26 +0000779 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000780 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000781 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +0000782 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +0000783 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh26b79942007-11-28 16:19:56 +0000784 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
drh43605152004-05-29 21:46:49 +0000785 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +0000786 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000787 cellOffset = pPage->cellOffset;
788 nCell = pPage->nCell;
789 assert( nCell==get2byte(&data[hdr+3]) );
790 usableSize = pPage->pBt->usableSize;
drh281b21d2008-08-22 12:57:08 +0000791 cbrk = get2byte(&data[hdr+5]);
792 memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
793 cbrk = usableSize;
drh43605152004-05-29 21:46:49 +0000794 for(i=0; i<nCell; i++){
795 u8 *pAddr; /* The i-th cell pointer */
796 pAddr = &data[cellOffset + i*2];
797 pc = get2byte(pAddr);
shanedcc50b72008-11-13 18:29:50 +0000798 if( pc>=usableSize ){
shane0af3f892008-11-12 04:55:34 +0000799 return SQLITE_CORRUPT_BKPT;
800 }
drh43605152004-05-29 21:46:49 +0000801 size = cellSizePtr(pPage, &temp[pc]);
drh281b21d2008-08-22 12:57:08 +0000802 cbrk -= size;
danielk19770d065412008-11-12 18:21:36 +0000803 if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){
shane0af3f892008-11-12 04:55:34 +0000804 return SQLITE_CORRUPT_BKPT;
805 }
danielk19770d065412008-11-12 18:21:36 +0000806 assert( cbrk+size<=usableSize && cbrk>=0 );
drh281b21d2008-08-22 12:57:08 +0000807 memcpy(&data[cbrk], &temp[pc], size);
808 put2byte(pAddr, cbrk);
drh2af926b2001-05-15 00:39:25 +0000809 }
drh281b21d2008-08-22 12:57:08 +0000810 assert( cbrk>=cellOffset+2*nCell );
811 put2byte(&data[hdr+5], cbrk);
drh43605152004-05-29 21:46:49 +0000812 data[hdr+1] = 0;
813 data[hdr+2] = 0;
814 data[hdr+7] = 0;
815 addr = cellOffset+2*nCell;
drh281b21d2008-08-22 12:57:08 +0000816 memset(&data[addr], 0, cbrk-addr);
drhc5053fb2008-11-27 02:22:10 +0000817 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977360e6342008-11-12 08:49:51 +0000818 if( cbrk-addr!=pPage->nFree ){
819 return SQLITE_CORRUPT_BKPT;
820 }
shane0af3f892008-11-12 04:55:34 +0000821 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +0000822}
823
drha059ad02001-04-17 20:09:11 +0000824/*
drh43605152004-05-29 21:46:49 +0000825** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +0000826**
drh9e572e62004-04-23 23:43:10 +0000827** Return the index into pPage->aData[] of the first byte of
drhfa67c3c2008-07-11 02:21:40 +0000828** the new allocation. The caller guarantees that there is enough
829** space. This routine will never fail.
drh2af926b2001-05-15 00:39:25 +0000830**
drh72f82862001-05-24 21:06:34 +0000831** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000832** nBytes of contiguous free space, then this routine automatically
833** calls defragementPage() to consolidate all free space before
834** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000835*/
drh9e572e62004-04-23 23:43:10 +0000836static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +0000837 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +0000838 int size;
drh24cd67e2004-05-10 16:18:47 +0000839 int nFrag;
drh43605152004-05-29 21:46:49 +0000840 int top;
841 int nCell;
842 int cellOffset;
drh9e572e62004-04-23 23:43:10 +0000843 unsigned char *data;
drh43605152004-05-29 21:46:49 +0000844
drh9e572e62004-04-23 23:43:10 +0000845 data = pPage->aData;
danielk19773b8a05f2007-03-19 17:44:26 +0000846 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000847 assert( pPage->pBt );
drh1fee73e2007-08-29 04:00:57 +0000848 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhfa67c3c2008-07-11 02:21:40 +0000849 assert( nByte>=0 ); /* Minimum cell size is 4 */
850 assert( pPage->nFree>=nByte );
851 assert( pPage->nOverflow==0 );
drhf49661a2008-12-10 16:45:50 +0000852 pPage->nFree -= (u16)nByte;
drh9e572e62004-04-23 23:43:10 +0000853 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000854
855 nFrag = data[hdr+7];
856 if( nFrag<60 ){
857 /* Search the freelist looking for a slot big enough to satisfy the
858 ** space request. */
859 addr = hdr+1;
860 while( (pc = get2byte(&data[addr]))>0 ){
861 size = get2byte(&data[pc+2]);
862 if( size>=nByte ){
drhf49661a2008-12-10 16:45:50 +0000863 int x = size - nByte;
drh43605152004-05-29 21:46:49 +0000864 if( size<nByte+4 ){
865 memcpy(&data[addr], &data[pc], 2);
drhf49661a2008-12-10 16:45:50 +0000866 data[hdr+7] = (u8)(nFrag + x);
drh43605152004-05-29 21:46:49 +0000867 return pc;
868 }else{
drhf49661a2008-12-10 16:45:50 +0000869 put2byte(&data[pc+2], x);
870 return pc + x;
drh43605152004-05-29 21:46:49 +0000871 }
872 }
873 addr = pc;
drh9e572e62004-04-23 23:43:10 +0000874 }
875 }
drh43605152004-05-29 21:46:49 +0000876
877 /* Allocate memory from the gap in between the cell pointer array
878 ** and the cell content area.
879 */
880 top = get2byte(&data[hdr+5]);
881 nCell = get2byte(&data[hdr+3]);
882 cellOffset = pPage->cellOffset;
883 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
danielk1977474b7cc2008-07-09 11:49:46 +0000884 defragmentPage(pPage);
drh43605152004-05-29 21:46:49 +0000885 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +0000886 }
drh43605152004-05-29 21:46:49 +0000887 top -= nByte;
888 assert( cellOffset + 2*nCell <= top );
889 put2byte(&data[hdr+5], top);
drhc5053fb2008-11-27 02:22:10 +0000890 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +0000891 return top;
drh7e3b0a02001-04-28 16:52:40 +0000892}
893
894/*
drh9e572e62004-04-23 23:43:10 +0000895** Return a section of the pPage->aData to the freelist.
896** The first byte of the new free block is pPage->aDisk[start]
897** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +0000898**
899** Most of the effort here is involved in coalesing adjacent
900** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000901*/
shanedcc50b72008-11-13 18:29:50 +0000902static int freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +0000903 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +0000904 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +0000905
drh9e572e62004-04-23 23:43:10 +0000906 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +0000907 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000908 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +0000909 assert( (start + size)<=pPage->pBt->usableSize );
drh1fee73e2007-08-29 04:00:57 +0000910 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh34004ce2008-07-11 16:15:17 +0000911 assert( size>=0 ); /* Minimum cell size is 4 */
drh9e572e62004-04-23 23:43:10 +0000912
drhfcce93f2006-02-22 03:08:32 +0000913#ifdef SQLITE_SECURE_DELETE
914 /* Overwrite deleted information with zeros when the SECURE_DELETE
915 ** option is enabled at compile-time */
916 memset(&data[start], 0, size);
917#endif
918
drh9e572e62004-04-23 23:43:10 +0000919 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +0000920 hdr = pPage->hdrOffset;
921 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +0000922 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +0000923 assert( pbegin<=pPage->pBt->usableSize-4 );
shanedcc50b72008-11-13 18:29:50 +0000924 if( pbegin<=addr ) {
925 return SQLITE_CORRUPT_BKPT;
926 }
drh3aac2dd2004-04-26 14:10:20 +0000927 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +0000928 }
shanedcc50b72008-11-13 18:29:50 +0000929 if ( pbegin>pPage->pBt->usableSize-4 ) {
930 return SQLITE_CORRUPT_BKPT;
931 }
drh3aac2dd2004-04-26 14:10:20 +0000932 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +0000933 put2byte(&data[addr], start);
934 put2byte(&data[start], pbegin);
935 put2byte(&data[start+2], size);
drhf49661a2008-12-10 16:45:50 +0000936 pPage->nFree += (u16)size;
drh9e572e62004-04-23 23:43:10 +0000937
938 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +0000939 addr = pPage->hdrOffset + 1;
940 while( (pbegin = get2byte(&data[addr]))>0 ){
drhf49661a2008-12-10 16:45:50 +0000941 int pnext, psize, x;
drh3aac2dd2004-04-26 14:10:20 +0000942 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +0000943 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +0000944 pnext = get2byte(&data[pbegin]);
945 psize = get2byte(&data[pbegin+2]);
946 if( pbegin + psize + 3 >= pnext && pnext>0 ){
947 int frag = pnext - (pbegin+psize);
drhf49661a2008-12-10 16:45:50 +0000948 if( (frag<0) || (frag>(int)data[pPage->hdrOffset+7]) ){
shanedcc50b72008-11-13 18:29:50 +0000949 return SQLITE_CORRUPT_BKPT;
950 }
drhf49661a2008-12-10 16:45:50 +0000951 data[pPage->hdrOffset+7] -= (u8)frag;
952 x = get2byte(&data[pnext]);
953 put2byte(&data[pbegin], x);
954 x = pnext + get2byte(&data[pnext+2]) - pbegin;
955 put2byte(&data[pbegin+2], x);
drh9e572e62004-04-23 23:43:10 +0000956 }else{
drh3aac2dd2004-04-26 14:10:20 +0000957 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +0000958 }
959 }
drh7e3b0a02001-04-28 16:52:40 +0000960
drh43605152004-05-29 21:46:49 +0000961 /* If the cell content area begins with a freeblock, remove it. */
962 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
963 int top;
964 pbegin = get2byte(&data[hdr+1]);
965 memcpy(&data[hdr+1], &data[pbegin], 2);
drhf49661a2008-12-10 16:45:50 +0000966 top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
967 put2byte(&data[hdr+5], top);
drh4b70f112004-05-02 21:12:19 +0000968 }
drhc5053fb2008-11-27 02:22:10 +0000969 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
shanedcc50b72008-11-13 18:29:50 +0000970 return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +0000971}
972
973/*
drh271efa52004-05-30 19:19:05 +0000974** Decode the flags byte (the first byte of the header) for a page
975** and initialize fields of the MemPage structure accordingly.
drh44845222008-07-17 18:39:57 +0000976**
977** Only the following combinations are supported. Anything different
978** indicates a corrupt database files:
979**
980** PTF_ZERODATA
981** PTF_ZERODATA | PTF_LEAF
982** PTF_LEAFDATA | PTF_INTKEY
983** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
drh271efa52004-05-30 19:19:05 +0000984*/
drh44845222008-07-17 18:39:57 +0000985static int decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:01 +0000986 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:05 +0000987
988 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
drh1fee73e2007-08-29 04:00:57 +0000989 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf49661a2008-12-10 16:45:50 +0000990 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
drh44845222008-07-17 18:39:57 +0000991 flagByte &= ~PTF_LEAF;
992 pPage->childPtrSize = 4-4*pPage->leaf;
drh271efa52004-05-30 19:19:05 +0000993 pBt = pPage->pBt;
drh44845222008-07-17 18:39:57 +0000994 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
995 pPage->intKey = 1;
996 pPage->hasData = pPage->leaf;
drh271efa52004-05-30 19:19:05 +0000997 pPage->maxLocal = pBt->maxLeaf;
998 pPage->minLocal = pBt->minLeaf;
drh44845222008-07-17 18:39:57 +0000999 }else if( flagByte==PTF_ZERODATA ){
1000 pPage->intKey = 0;
1001 pPage->hasData = 0;
drh271efa52004-05-30 19:19:05 +00001002 pPage->maxLocal = pBt->maxLocal;
1003 pPage->minLocal = pBt->minLocal;
drh44845222008-07-17 18:39:57 +00001004 }else{
1005 return SQLITE_CORRUPT_BKPT;
drh271efa52004-05-30 19:19:05 +00001006 }
drh44845222008-07-17 18:39:57 +00001007 return SQLITE_OK;
drh271efa52004-05-30 19:19:05 +00001008}
1009
1010/*
drh7e3b0a02001-04-28 16:52:40 +00001011** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +00001012**
1013** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +00001014** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +00001015** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1016** guarantee that the page is well-formed. It only shows that
1017** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +00001018*/
danielk197771d5d2c2008-09-29 11:49:47 +00001019int sqlite3BtreeInitPage(MemPage *pPage){
drh2af926b2001-05-15 00:39:25 +00001020
danielk197771d5d2c2008-09-29 11:49:47 +00001021 assert( pPage->pBt!=0 );
1022 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +00001023 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbf4bca52007-09-06 22:19:14 +00001024 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1025 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +00001026
1027 if( !pPage->isInit ){
drhf49661a2008-12-10 16:45:50 +00001028 u16 pc; /* Address of a freeblock within pPage->aData[] */
1029 u8 hdr; /* Offset to beginning of page header */
danielk197771d5d2c2008-09-29 11:49:47 +00001030 u8 *data; /* Equal to pPage->aData */
1031 BtShared *pBt; /* The main btree structure */
drhf49661a2008-12-10 16:45:50 +00001032 u16 usableSize; /* Amount of usable space on each page */
1033 u16 cellOffset; /* Offset from start of page to first cell pointer */
1034 u16 nFree; /* Number of unused bytes on the page */
1035 u16 top; /* First byte of the cell content area */
danielk197771d5d2c2008-09-29 11:49:47 +00001036
1037 pBt = pPage->pBt;
1038
danielk1977eaa06f62008-09-18 17:34:44 +00001039 hdr = pPage->hdrOffset;
1040 data = pPage->aData;
1041 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
1042 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
1043 pPage->maskPage = pBt->pageSize - 1;
1044 pPage->nOverflow = 0;
danielk1977eaa06f62008-09-18 17:34:44 +00001045 usableSize = pBt->usableSize;
1046 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1047 top = get2byte(&data[hdr+5]);
1048 pPage->nCell = get2byte(&data[hdr+3]);
1049 if( pPage->nCell>MX_CELL(pBt) ){
1050 /* To many cells for a single page. The page must be corrupt */
1051 return SQLITE_CORRUPT_BKPT;
1052 }
danielk1977eaa06f62008-09-18 17:34:44 +00001053
1054 /* Compute the total free space on the page */
1055 pc = get2byte(&data[hdr+1]);
1056 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
1057 while( pc>0 ){
drh1bd10f82008-12-10 21:19:56 +00001058 u16 next, size;
danielk1977eaa06f62008-09-18 17:34:44 +00001059 if( pc>usableSize-4 ){
1060 /* Free block is off the page */
1061 return SQLITE_CORRUPT_BKPT;
1062 }
1063 next = get2byte(&data[pc]);
1064 size = get2byte(&data[pc+2]);
1065 if( next>0 && next<=pc+size+3 ){
1066 /* Free blocks must be in accending order */
1067 return SQLITE_CORRUPT_BKPT;
1068 }
1069 nFree += size;
1070 pc = next;
1071 }
drhf49661a2008-12-10 16:45:50 +00001072 pPage->nFree = (u16)nFree;
danielk1977eaa06f62008-09-18 17:34:44 +00001073 if( nFree>=usableSize ){
1074 /* Free space cannot exceed total page size */
drh49285702005-09-17 15:20:26 +00001075 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001076 }
drh9e572e62004-04-23 23:43:10 +00001077
drh1688c862008-07-18 02:44:17 +00001078#if 0
1079 /* Check that all the offsets in the cell offset array are within range.
1080 **
1081 ** Omitting this consistency check and using the pPage->maskPage mask
1082 ** to prevent overrunning the page buffer in findCell() results in a
1083 ** 2.5% performance gain.
1084 */
1085 {
1086 u8 *pOff; /* Iterator used to check all cell offsets are in range */
1087 u8 *pEnd; /* Pointer to end of cell offset array */
1088 u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */
1089 mask = ~(((u8)(pBt->pageSize>>8))-1);
1090 pEnd = &data[cellOffset + pPage->nCell*2];
1091 for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2);
1092 if( pOff!=pEnd ){
1093 return SQLITE_CORRUPT_BKPT;
1094 }
danielk1977e16535f2008-06-11 18:15:29 +00001095 }
drh1688c862008-07-18 02:44:17 +00001096#endif
danielk1977e16535f2008-06-11 18:15:29 +00001097
danielk197771d5d2c2008-09-29 11:49:47 +00001098 pPage->isInit = 1;
1099 }
drh9e572e62004-04-23 23:43:10 +00001100 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001101}
1102
1103/*
drh8b2f49b2001-06-08 00:21:52 +00001104** Set up a raw page so that it looks like a database page holding
1105** no entries.
drhbd03cae2001-06-02 02:40:57 +00001106*/
drh9e572e62004-04-23 23:43:10 +00001107static void zeroPage(MemPage *pPage, int flags){
1108 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:01 +00001109 BtShared *pBt = pPage->pBt;
drhf49661a2008-12-10 16:45:50 +00001110 u8 hdr = pPage->hdrOffset;
1111 u16 first;
drh9e572e62004-04-23 23:43:10 +00001112
danielk19773b8a05f2007-03-19 17:44:26 +00001113 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
drhbf4bca52007-09-06 22:19:14 +00001114 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1115 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
danielk19773b8a05f2007-03-19 17:44:26 +00001116 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +00001117 assert( sqlite3_mutex_held(pBt->mutex) );
drh1af4a6e2008-07-18 03:32:51 +00001118 /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/
drh1bd10f82008-12-10 21:19:56 +00001119 data[hdr] = (char)flags;
1120 first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
drh43605152004-05-29 21:46:49 +00001121 memset(&data[hdr+1], 0, 4);
1122 data[hdr+7] = 0;
1123 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001124 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001125 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001126 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001127 pPage->cellOffset = first;
1128 pPage->nOverflow = 0;
drh1688c862008-07-18 02:44:17 +00001129 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
1130 pPage->maskPage = pBt->pageSize - 1;
drh43605152004-05-29 21:46:49 +00001131 pPage->nCell = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00001132 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:57 +00001133}
1134
drh897a8202008-09-18 01:08:15 +00001135
1136/*
1137** Convert a DbPage obtained from the pager into a MemPage used by
1138** the btree layer.
1139*/
1140static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
1141 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
1142 pPage->aData = sqlite3PagerGetData(pDbPage);
1143 pPage->pDbPage = pDbPage;
1144 pPage->pBt = pBt;
1145 pPage->pgno = pgno;
1146 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
1147 return pPage;
1148}
1149
drhbd03cae2001-06-02 02:40:57 +00001150/*
drh3aac2dd2004-04-26 14:10:20 +00001151** Get a page from the pager. Initialize the MemPage.pBt and
1152** MemPage.aData elements if needed.
drh538f5702007-04-13 02:14:30 +00001153**
1154** If the noContent flag is set, it means that we do not care about
1155** the content of the page at this time. So do not go to the disk
1156** to fetch the content. Just fill in the content with zeros for now.
1157** If in the future we call sqlite3PagerWrite() on this page, that
1158** means we have started to be concerned about content and the disk
1159** read should occur at that point.
drh3aac2dd2004-04-26 14:10:20 +00001160*/
drh16a9b832007-05-05 18:39:25 +00001161int sqlite3BtreeGetPage(
1162 BtShared *pBt, /* The btree */
1163 Pgno pgno, /* Number of the page to fetch */
1164 MemPage **ppPage, /* Return the page in this parameter */
1165 int noContent /* Do not load page content if true */
1166){
drh3aac2dd2004-04-26 14:10:20 +00001167 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001168 DbPage *pDbPage;
1169
drh1fee73e2007-08-29 04:00:57 +00001170 assert( sqlite3_mutex_held(pBt->mutex) );
drh538f5702007-04-13 02:14:30 +00001171 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
drh3aac2dd2004-04-26 14:10:20 +00001172 if( rc ) return rc;
drh897a8202008-09-18 01:08:15 +00001173 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
drh3aac2dd2004-04-26 14:10:20 +00001174 return SQLITE_OK;
1175}
1176
1177/*
danielk1977bea2a942009-01-20 17:06:27 +00001178** Retrieve a page from the pager cache. If the requested page is not
1179** already in the pager cache return NULL. Initialize the MemPage.pBt and
1180** MemPage.aData elements if needed.
1181*/
1182static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
1183 DbPage *pDbPage;
1184 assert( sqlite3_mutex_held(pBt->mutex) );
1185 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
1186 if( pDbPage ){
1187 return btreePageFromDbPage(pDbPage, pgno, pBt);
1188 }
1189 return 0;
1190}
1191
1192/*
danielk197789d40042008-11-17 14:20:56 +00001193** Return the size of the database file in pages. If there is any kind of
1194** error, return ((unsigned int)-1).
danielk197767fd7a92008-09-10 17:53:35 +00001195*/
danielk197789d40042008-11-17 14:20:56 +00001196static Pgno pagerPagecount(BtShared *pBt){
1197 int nPage = -1;
danielk197767fd7a92008-09-10 17:53:35 +00001198 int rc;
danielk197789d40042008-11-17 14:20:56 +00001199 assert( pBt->pPage1 );
1200 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1201 assert( rc==SQLITE_OK || nPage==-1 );
1202 return (Pgno)nPage;
danielk197767fd7a92008-09-10 17:53:35 +00001203}
1204
1205/*
drhde647132004-05-07 17:57:49 +00001206** Get a page from the pager and initialize it. This routine
1207** is just a convenience wrapper around separate calls to
drh16a9b832007-05-05 18:39:25 +00001208** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
drhde647132004-05-07 17:57:49 +00001209*/
1210static int getAndInitPage(
danielk1977aef0bf62005-12-30 16:28:01 +00001211 BtShared *pBt, /* The database file */
drhde647132004-05-07 17:57:49 +00001212 Pgno pgno, /* Number of the page to get */
danielk197771d5d2c2008-09-29 11:49:47 +00001213 MemPage **ppPage /* Write the page pointer here */
drhde647132004-05-07 17:57:49 +00001214){
1215 int rc;
drh897a8202008-09-18 01:08:15 +00001216 MemPage *pPage;
1217
drh1fee73e2007-08-29 04:00:57 +00001218 assert( sqlite3_mutex_held(pBt->mutex) );
drh897a8202008-09-18 01:08:15 +00001219 if( pgno==0 ){
drh49285702005-09-17 15:20:26 +00001220 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001221 }
danielk19779f580ad2008-09-10 14:45:57 +00001222
drh897a8202008-09-18 01:08:15 +00001223 /* It is often the case that the page we want is already in cache.
1224 ** If so, get it directly. This saves us from having to call
1225 ** pagerPagecount() to make sure pgno is within limits, which results
1226 ** in a measureable performance improvements.
1227 */
danielk1977bea2a942009-01-20 17:06:27 +00001228 *ppPage = pPage = btreePageLookup(pBt, pgno);
1229 if( pPage ){
drh897a8202008-09-18 01:08:15 +00001230 /* Page is already in cache */
drh897a8202008-09-18 01:08:15 +00001231 rc = SQLITE_OK;
1232 }else{
1233 /* Page not in cache. Acquire it. */
danielk197789d40042008-11-17 14:20:56 +00001234 if( pgno>pagerPagecount(pBt) ){
drh897a8202008-09-18 01:08:15 +00001235 return SQLITE_CORRUPT_BKPT;
1236 }
1237 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
1238 if( rc ) return rc;
1239 pPage = *ppPage;
1240 }
danielk197771d5d2c2008-09-29 11:49:47 +00001241 if( !pPage->isInit ){
1242 rc = sqlite3BtreeInitPage(pPage);
drh897a8202008-09-18 01:08:15 +00001243 }
1244 if( rc!=SQLITE_OK ){
1245 releasePage(pPage);
1246 *ppPage = 0;
1247 }
drhde647132004-05-07 17:57:49 +00001248 return rc;
1249}
1250
1251/*
drh3aac2dd2004-04-26 14:10:20 +00001252** Release a MemPage. This should be called once for each prior
drh16a9b832007-05-05 18:39:25 +00001253** call to sqlite3BtreeGetPage.
drh3aac2dd2004-04-26 14:10:20 +00001254*/
drh4b70f112004-05-02 21:12:19 +00001255static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001256 if( pPage ){
drh30df0092008-12-23 15:58:06 +00001257 assert( pPage->nOverflow==0 || sqlite3PagerPageRefcount(pPage->pDbPage)>1 );
drh3aac2dd2004-04-26 14:10:20 +00001258 assert( pPage->aData );
1259 assert( pPage->pBt );
drhbf4bca52007-09-06 22:19:14 +00001260 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1261 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
drh1fee73e2007-08-29 04:00:57 +00001262 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +00001263 sqlite3PagerUnref(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00001264 }
1265}
1266
1267/*
drha6abd042004-06-09 17:37:22 +00001268** During a rollback, when the pager reloads information into the cache
1269** so that the cache is restored to its original state at the start of
1270** the transaction, for each page restored this routine is called.
1271**
1272** This routine needs to reset the extra data section at the end of the
1273** page to agree with the restored data.
1274*/
danielk1977eaa06f62008-09-18 17:34:44 +00001275static void pageReinit(DbPage *pData){
drh07d183d2005-05-01 22:52:42 +00001276 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00001277 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
danielk197771d5d2c2008-09-29 11:49:47 +00001278 if( pPage->isInit ){
drh1fee73e2007-08-29 04:00:57 +00001279 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drha6abd042004-06-09 17:37:22 +00001280 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00001281 if( sqlite3PagerPageRefcount(pData)>0 ){
1282 sqlite3BtreeInitPage(pPage);
1283 }
drha6abd042004-06-09 17:37:22 +00001284 }
1285}
1286
1287/*
drhe5fe6902007-12-07 18:55:28 +00001288** Invoke the busy handler for a btree.
1289*/
danielk19771ceedd32008-11-19 10:22:33 +00001290static int btreeInvokeBusyHandler(void *pArg){
drhe5fe6902007-12-07 18:55:28 +00001291 BtShared *pBt = (BtShared*)pArg;
1292 assert( pBt->db );
1293 assert( sqlite3_mutex_held(pBt->db->mutex) );
1294 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
1295}
1296
1297/*
drhad3e0102004-09-03 23:32:18 +00001298** Open a database file.
1299**
drh382c0242001-10-06 16:33:02 +00001300** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001301** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001302** database file will be deleted when sqlite3BtreeClose() is called.
drhe53831d2007-08-17 01:14:38 +00001303** If zFilename is ":memory:" then an in-memory database is created
1304** that is automatically destroyed when it is closed.
drha059ad02001-04-17 20:09:11 +00001305*/
drh23e11ca2004-05-04 17:27:28 +00001306int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001307 const char *zFilename, /* Name of the file containing the BTree database */
drhe5fe6902007-12-07 18:55:28 +00001308 sqlite3 *db, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:20 +00001309 Btree **ppBtree, /* Pointer to new Btree object written here */
drh33f4e022007-09-03 15:19:34 +00001310 int flags, /* Options */
1311 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
drh6019e162001-07-02 17:51:45 +00001312){
drhd677b3d2007-08-20 22:48:41 +00001313 sqlite3_vfs *pVfs; /* The VFS to use for this btree */
drhe53831d2007-08-17 01:14:38 +00001314 BtShared *pBt = 0; /* Shared part of btree structure */
danielk1977aef0bf62005-12-30 16:28:01 +00001315 Btree *p; /* Handle to return */
danielk1977dddbcdc2007-04-26 14:42:34 +00001316 int rc = SQLITE_OK;
drhf49661a2008-12-10 16:45:50 +00001317 u8 nReserve;
drh90f5ecb2004-07-22 01:19:35 +00001318 unsigned char zDbHeader[100];
danielk1977aef0bf62005-12-30 16:28:01 +00001319
1320 /* Set the variable isMemdb to true for an in-memory database, or
1321 ** false for a file-based database. This symbol is only required if
1322 ** either of the shared-data or autovacuum features are compiled
1323 ** into the library.
1324 */
1325#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1326 #ifdef SQLITE_OMIT_MEMORYDB
drh980b1a72006-08-16 16:42:48 +00001327 const int isMemdb = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001328 #else
drh980b1a72006-08-16 16:42:48 +00001329 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
danielk1977aef0bf62005-12-30 16:28:01 +00001330 #endif
1331#endif
1332
drhe5fe6902007-12-07 18:55:28 +00001333 assert( db!=0 );
1334 assert( sqlite3_mutex_held(db->mutex) );
drh153c62c2007-08-24 03:51:33 +00001335
drhe5fe6902007-12-07 18:55:28 +00001336 pVfs = db->pVfs;
drh17435752007-08-16 04:30:38 +00001337 p = sqlite3MallocZero(sizeof(Btree));
danielk1977aef0bf62005-12-30 16:28:01 +00001338 if( !p ){
1339 return SQLITE_NOMEM;
1340 }
1341 p->inTrans = TRANS_NONE;
drhe5fe6902007-12-07 18:55:28 +00001342 p->db = db;
danielk1977aef0bf62005-12-30 16:28:01 +00001343
drh198bf392006-01-06 21:52:49 +00001344#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001345 /*
1346 ** If this Btree is a candidate for shared cache, try to find an
1347 ** existing BtShared object that we can share with
1348 */
drh34004ce2008-07-11 16:15:17 +00001349 if( isMemdb==0
drhe5fe6902007-12-07 18:55:28 +00001350 && (db->flags & SQLITE_Vtab)==0
drhe53831d2007-08-17 01:14:38 +00001351 && zFilename && zFilename[0]
drhe53831d2007-08-17 01:14:38 +00001352 ){
danielk1977502b4e02008-09-02 14:07:24 +00001353 if( sqlite3GlobalConfig.sharedCacheEnabled ){
danielk1977adfb9b02007-09-17 07:02:56 +00001354 int nFullPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001355 char *zFullPathname = sqlite3Malloc(nFullPathname);
drhff0587c2007-08-29 17:43:19 +00001356 sqlite3_mutex *mutexShared;
1357 p->sharable = 1;
drh34004ce2008-07-11 16:15:17 +00001358 db->flags |= SQLITE_SharedCache;
drhff0587c2007-08-29 17:43:19 +00001359 if( !zFullPathname ){
1360 sqlite3_free(p);
1361 return SQLITE_NOMEM;
1362 }
danielk1977adfb9b02007-09-17 07:02:56 +00001363 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
danielk197759f8c082008-06-18 17:09:10 +00001364 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhff0587c2007-08-29 17:43:19 +00001365 sqlite3_mutex_enter(mutexShared);
drh78f82d12008-09-02 00:52:52 +00001366 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
drhff0587c2007-08-29 17:43:19 +00001367 assert( pBt->nRef>0 );
1368 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
1369 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
1370 p->pBt = pBt;
1371 pBt->nRef++;
1372 break;
1373 }
1374 }
1375 sqlite3_mutex_leave(mutexShared);
1376 sqlite3_free(zFullPathname);
danielk1977aef0bf62005-12-30 16:28:01 +00001377 }
drhff0587c2007-08-29 17:43:19 +00001378#ifdef SQLITE_DEBUG
1379 else{
1380 /* In debug mode, we mark all persistent databases as sharable
1381 ** even when they are not. This exercises the locking code and
1382 ** gives more opportunity for asserts(sqlite3_mutex_held())
1383 ** statements to find locking problems.
1384 */
1385 p->sharable = 1;
1386 }
1387#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001388 }
1389#endif
drha059ad02001-04-17 20:09:11 +00001390 if( pBt==0 ){
drhe53831d2007-08-17 01:14:38 +00001391 /*
1392 ** The following asserts make sure that structures used by the btree are
1393 ** the right size. This is to guard against size changes that result
1394 ** when compiling on a different architecture.
danielk197703aded42004-11-22 05:26:27 +00001395 */
drhe53831d2007-08-17 01:14:38 +00001396 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1397 assert( sizeof(u64)==8 || sizeof(u64)==4 );
1398 assert( sizeof(u32)==4 );
1399 assert( sizeof(u16)==2 );
1400 assert( sizeof(Pgno)==4 );
1401
1402 pBt = sqlite3MallocZero( sizeof(*pBt) );
1403 if( pBt==0 ){
1404 rc = SQLITE_NOMEM;
1405 goto btree_open_out;
1406 }
danielk197771d5d2c2008-09-29 11:49:47 +00001407 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
drh33f4e022007-09-03 15:19:34 +00001408 EXTRA_SIZE, flags, vfsFlags);
drhe53831d2007-08-17 01:14:38 +00001409 if( rc==SQLITE_OK ){
1410 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
1411 }
1412 if( rc!=SQLITE_OK ){
1413 goto btree_open_out;
1414 }
danielk19771ceedd32008-11-19 10:22:33 +00001415 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
drhe53831d2007-08-17 01:14:38 +00001416 p->pBt = pBt;
1417
drhe53831d2007-08-17 01:14:38 +00001418 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
1419 pBt->pCursor = 0;
1420 pBt->pPage1 = 0;
1421 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
1422 pBt->pageSize = get2byte(&zDbHeader[16]);
1423 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1424 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
danielk1977a1644fd2007-08-29 12:31:25 +00001425 pBt->pageSize = 0;
1426 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001427#ifndef SQLITE_OMIT_AUTOVACUUM
1428 /* If the magic name ":memory:" will create an in-memory database, then
1429 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1430 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1431 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1432 ** regular file-name. In this case the auto-vacuum applies as per normal.
1433 */
1434 if( zFilename && !isMemdb ){
1435 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1436 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
1437 }
1438#endif
1439 nReserve = 0;
1440 }else{
1441 nReserve = zDbHeader[20];
drhe53831d2007-08-17 01:14:38 +00001442 pBt->pageSizeFixed = 1;
1443#ifndef SQLITE_OMIT_AUTOVACUUM
1444 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1445 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
1446#endif
1447 }
1448 pBt->usableSize = pBt->pageSize - nReserve;
1449 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
danielk1977a1644fd2007-08-29 12:31:25 +00001450 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001451
1452#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1453 /* Add the new BtShared object to the linked list sharable BtShareds.
1454 */
1455 if( p->sharable ){
1456 sqlite3_mutex *mutexShared;
1457 pBt->nRef = 1;
danielk197759f8c082008-06-18 17:09:10 +00001458 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
danielk1977075c23a2008-09-01 18:34:20 +00001459 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +00001460 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
drh3285db22007-09-03 22:00:39 +00001461 if( pBt->mutex==0 ){
1462 rc = SQLITE_NOMEM;
drhe5fe6902007-12-07 18:55:28 +00001463 db->mallocFailed = 0;
drh3285db22007-09-03 22:00:39 +00001464 goto btree_open_out;
1465 }
drhff0587c2007-08-29 17:43:19 +00001466 }
drhe53831d2007-08-17 01:14:38 +00001467 sqlite3_mutex_enter(mutexShared);
drh78f82d12008-09-02 00:52:52 +00001468 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
1469 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
drhe53831d2007-08-17 01:14:38 +00001470 sqlite3_mutex_leave(mutexShared);
danielk1977951af802004-11-05 15:45:09 +00001471 }
drheee46cf2004-11-06 00:02:48 +00001472#endif
drh90f5ecb2004-07-22 01:19:35 +00001473 }
danielk1977aef0bf62005-12-30 16:28:01 +00001474
drhcfed7bc2006-03-13 14:28:05 +00001475#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001476 /* If the new Btree uses a sharable pBtShared, then link the new
1477 ** Btree into the list of all sharable Btrees for the same connection.
drhabddb0c2007-08-20 13:14:28 +00001478 ** The list is kept in ascending order by pBt address.
danielk197754f01982006-01-18 15:25:17 +00001479 */
drhe53831d2007-08-17 01:14:38 +00001480 if( p->sharable ){
1481 int i;
1482 Btree *pSib;
drhe5fe6902007-12-07 18:55:28 +00001483 for(i=0; i<db->nDb; i++){
1484 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
drhe53831d2007-08-17 01:14:38 +00001485 while( pSib->pPrev ){ pSib = pSib->pPrev; }
1486 if( p->pBt<pSib->pBt ){
1487 p->pNext = pSib;
1488 p->pPrev = 0;
1489 pSib->pPrev = p;
1490 }else{
drhabddb0c2007-08-20 13:14:28 +00001491 while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
drhe53831d2007-08-17 01:14:38 +00001492 pSib = pSib->pNext;
1493 }
1494 p->pNext = pSib->pNext;
1495 p->pPrev = pSib;
1496 if( p->pNext ){
1497 p->pNext->pPrev = p;
1498 }
1499 pSib->pNext = p;
1500 }
1501 break;
1502 }
1503 }
danielk1977aef0bf62005-12-30 16:28:01 +00001504 }
danielk1977aef0bf62005-12-30 16:28:01 +00001505#endif
1506 *ppBtree = p;
danielk1977dddbcdc2007-04-26 14:42:34 +00001507
1508btree_open_out:
1509 if( rc!=SQLITE_OK ){
1510 if( pBt && pBt->pPager ){
1511 sqlite3PagerClose(pBt->pPager);
1512 }
drh17435752007-08-16 04:30:38 +00001513 sqlite3_free(pBt);
1514 sqlite3_free(p);
danielk1977dddbcdc2007-04-26 14:42:34 +00001515 *ppBtree = 0;
1516 }
1517 return rc;
drha059ad02001-04-17 20:09:11 +00001518}
1519
1520/*
drhe53831d2007-08-17 01:14:38 +00001521** Decrement the BtShared.nRef counter. When it reaches zero,
1522** remove the BtShared structure from the sharing list. Return
1523** true if the BtShared.nRef counter reaches zero and return
1524** false if it is still positive.
1525*/
1526static int removeFromSharingList(BtShared *pBt){
1527#ifndef SQLITE_OMIT_SHARED_CACHE
1528 sqlite3_mutex *pMaster;
1529 BtShared *pList;
1530 int removed = 0;
1531
drhd677b3d2007-08-20 22:48:41 +00001532 assert( sqlite3_mutex_notheld(pBt->mutex) );
danielk197759f8c082008-06-18 17:09:10 +00001533 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhe53831d2007-08-17 01:14:38 +00001534 sqlite3_mutex_enter(pMaster);
1535 pBt->nRef--;
1536 if( pBt->nRef<=0 ){
drh78f82d12008-09-02 00:52:52 +00001537 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
1538 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
drhe53831d2007-08-17 01:14:38 +00001539 }else{
drh78f82d12008-09-02 00:52:52 +00001540 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
drh34004ce2008-07-11 16:15:17 +00001541 while( ALWAYS(pList) && pList->pNext!=pBt ){
drhe53831d2007-08-17 01:14:38 +00001542 pList=pList->pNext;
1543 }
drh34004ce2008-07-11 16:15:17 +00001544 if( ALWAYS(pList) ){
drhe53831d2007-08-17 01:14:38 +00001545 pList->pNext = pBt->pNext;
1546 }
1547 }
drh3285db22007-09-03 22:00:39 +00001548 if( SQLITE_THREADSAFE ){
1549 sqlite3_mutex_free(pBt->mutex);
1550 }
drhe53831d2007-08-17 01:14:38 +00001551 removed = 1;
1552 }
1553 sqlite3_mutex_leave(pMaster);
1554 return removed;
1555#else
1556 return 1;
1557#endif
1558}
1559
1560/*
drhf7141992008-06-19 00:16:08 +00001561** Make sure pBt->pTmpSpace points to an allocation of
1562** MX_CELL_SIZE(pBt) bytes.
1563*/
1564static void allocateTempSpace(BtShared *pBt){
1565 if( !pBt->pTmpSpace ){
1566 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
1567 }
1568}
1569
1570/*
1571** Free the pBt->pTmpSpace allocation
1572*/
1573static void freeTempSpace(BtShared *pBt){
1574 sqlite3PageFree( pBt->pTmpSpace);
1575 pBt->pTmpSpace = 0;
1576}
1577
1578/*
drha059ad02001-04-17 20:09:11 +00001579** Close an open database and invalidate all cursors.
1580*/
danielk1977aef0bf62005-12-30 16:28:01 +00001581int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00001582 BtShared *pBt = p->pBt;
1583 BtCursor *pCur;
1584
danielk1977aef0bf62005-12-30 16:28:01 +00001585 /* Close all cursors opened via this handle. */
drhe5fe6902007-12-07 18:55:28 +00001586 assert( sqlite3_mutex_held(p->db->mutex) );
drhe53831d2007-08-17 01:14:38 +00001587 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00001588 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00001589 pCur = pBt->pCursor;
1590 while( pCur ){
1591 BtCursor *pTmp = pCur;
1592 pCur = pCur->pNext;
1593 if( pTmp->pBtree==p ){
1594 sqlite3BtreeCloseCursor(pTmp);
1595 }
drha059ad02001-04-17 20:09:11 +00001596 }
danielk1977aef0bf62005-12-30 16:28:01 +00001597
danielk19778d34dfd2006-01-24 16:37:57 +00001598 /* Rollback any active transaction and free the handle structure.
1599 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1600 ** this handle.
1601 */
danielk1977b597f742006-01-15 11:39:18 +00001602 sqlite3BtreeRollback(p);
drhe53831d2007-08-17 01:14:38 +00001603 sqlite3BtreeLeave(p);
danielk1977aef0bf62005-12-30 16:28:01 +00001604
danielk1977aef0bf62005-12-30 16:28:01 +00001605 /* If there are still other outstanding references to the shared-btree
1606 ** structure, return now. The remainder of this procedure cleans
1607 ** up the shared-btree.
1608 */
drhe53831d2007-08-17 01:14:38 +00001609 assert( p->wantToLock==0 && p->locked==0 );
1610 if( !p->sharable || removeFromSharingList(pBt) ){
1611 /* The pBt is no longer on the sharing list, so we can access
1612 ** it without having to hold the mutex.
1613 **
1614 ** Clean out and delete the BtShared object.
1615 */
1616 assert( !pBt->pCursor );
drhe53831d2007-08-17 01:14:38 +00001617 sqlite3PagerClose(pBt->pPager);
1618 if( pBt->xFreeSchema && pBt->pSchema ){
1619 pBt->xFreeSchema(pBt->pSchema);
1620 }
1621 sqlite3_free(pBt->pSchema);
drhf7141992008-06-19 00:16:08 +00001622 freeTempSpace(pBt);
drh65bbf292008-06-19 01:03:17 +00001623 sqlite3_free(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00001624 }
1625
drhe53831d2007-08-17 01:14:38 +00001626#ifndef SQLITE_OMIT_SHARED_CACHE
drhcab5ed72007-08-22 11:41:18 +00001627 assert( p->wantToLock==0 );
1628 assert( p->locked==0 );
1629 if( p->pPrev ) p->pPrev->pNext = p->pNext;
1630 if( p->pNext ) p->pNext->pPrev = p->pPrev;
danielk1977aef0bf62005-12-30 16:28:01 +00001631#endif
1632
drhe53831d2007-08-17 01:14:38 +00001633 sqlite3_free(p);
drha059ad02001-04-17 20:09:11 +00001634 return SQLITE_OK;
1635}
1636
1637/*
drhda47d772002-12-02 04:25:19 +00001638** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001639**
1640** The maximum number of cache pages is set to the absolute
1641** value of mxPage. If mxPage is negative, the pager will
1642** operate asynchronously - it will not stop to do fsync()s
1643** to insure data is written to the disk surface before
1644** continuing. Transactions still work if synchronous is off,
1645** and the database cannot be corrupted if this program
1646** crashes. But if the operating system crashes or there is
1647** an abrupt power failure when synchronous is off, the database
1648** could be left in an inconsistent and unrecoverable state.
1649** Synchronous is on by default so database corruption is not
1650** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001651*/
danielk1977aef0bf62005-12-30 16:28:01 +00001652int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1653 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001654 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001655 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001656 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhd677b3d2007-08-20 22:48:41 +00001657 sqlite3BtreeLeave(p);
drhf57b14a2001-09-14 18:54:08 +00001658 return SQLITE_OK;
1659}
1660
1661/*
drh973b6e32003-02-12 14:09:42 +00001662** Change the way data is synced to disk in order to increase or decrease
1663** how well the database resists damage due to OS crashes and power
1664** failures. Level 1 is the same as asynchronous (no syncs() occur and
1665** there is a high probability of damage) Level 2 is the default. There
1666** is a very low but non-zero probability of damage. Level 3 reduces the
1667** probability of damage to near zero but with a write performance reduction.
1668*/
danielk197793758c82005-01-21 08:13:14 +00001669#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001670int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
danielk1977aef0bf62005-12-30 16:28:01 +00001671 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001672 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001673 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001674 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
drhd677b3d2007-08-20 22:48:41 +00001675 sqlite3BtreeLeave(p);
drh973b6e32003-02-12 14:09:42 +00001676 return SQLITE_OK;
1677}
danielk197793758c82005-01-21 08:13:14 +00001678#endif
drh973b6e32003-02-12 14:09:42 +00001679
drh2c8997b2005-08-27 16:36:48 +00001680/*
1681** Return TRUE if the given btree is set to safety level 1. In other
1682** words, return TRUE if no sync() occurs on the disk files.
1683*/
danielk1977aef0bf62005-12-30 16:28:01 +00001684int sqlite3BtreeSyncDisabled(Btree *p){
1685 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001686 int rc;
drhe5fe6902007-12-07 18:55:28 +00001687 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001688 sqlite3BtreeEnter(p);
drhd0679ed2007-08-28 22:24:34 +00001689 assert( pBt && pBt->pPager );
drhd677b3d2007-08-20 22:48:41 +00001690 rc = sqlite3PagerNosync(pBt->pPager);
1691 sqlite3BtreeLeave(p);
1692 return rc;
drh2c8997b2005-08-27 16:36:48 +00001693}
1694
danielk1977576ec6b2005-01-21 11:55:25 +00001695#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001696/*
drh90f5ecb2004-07-22 01:19:35 +00001697** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001698**
1699** The page size must be a power of 2 between 512 and 65536. If the page
1700** size supplied does not meet this constraint then the page size is not
1701** changed.
1702**
1703** Page sizes are constrained to be a power of two so that the region
1704** of the database file used for locking (beginning at PENDING_BYTE,
1705** the first byte past the 1GB boundary, 0x40000000) needs to occur
1706** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001707**
1708** If parameter nReserve is less than zero, then the number of reserved
1709** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001710*/
danielk1977aef0bf62005-12-30 16:28:01 +00001711int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
danielk1977a1644fd2007-08-29 12:31:25 +00001712 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00001713 BtShared *pBt = p->pBt;
drhf49661a2008-12-10 16:45:50 +00001714 assert( nReserve>=-1 && nReserve<=255 );
drhd677b3d2007-08-20 22:48:41 +00001715 sqlite3BtreeEnter(p);
drh90f5ecb2004-07-22 01:19:35 +00001716 if( pBt->pageSizeFixed ){
drhd677b3d2007-08-20 22:48:41 +00001717 sqlite3BtreeLeave(p);
drh90f5ecb2004-07-22 01:19:35 +00001718 return SQLITE_READONLY;
1719 }
1720 if( nReserve<0 ){
1721 nReserve = pBt->pageSize - pBt->usableSize;
1722 }
drhf49661a2008-12-10 16:45:50 +00001723 assert( nReserve>=0 && nReserve<=255 );
drh06f50212004-11-02 14:24:33 +00001724 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1725 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:42 +00001726 assert( (pageSize & 7)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00001727 assert( !pBt->pPage1 && !pBt->pCursor );
drh1bd10f82008-12-10 21:19:56 +00001728 pBt->pageSize = (u16)pageSize;
drhf7141992008-06-19 00:16:08 +00001729 freeTempSpace(pBt);
danielk1977a1644fd2007-08-29 12:31:25 +00001730 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001731 }
drhf49661a2008-12-10 16:45:50 +00001732 pBt->usableSize = pBt->pageSize - (u16)nReserve;
drhd677b3d2007-08-20 22:48:41 +00001733 sqlite3BtreeLeave(p);
danielk1977a1644fd2007-08-29 12:31:25 +00001734 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001735}
1736
1737/*
1738** Return the currently defined page size
1739*/
danielk1977aef0bf62005-12-30 16:28:01 +00001740int sqlite3BtreeGetPageSize(Btree *p){
1741 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001742}
danielk1977aef0bf62005-12-30 16:28:01 +00001743int sqlite3BtreeGetReserve(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00001744 int n;
1745 sqlite3BtreeEnter(p);
1746 n = p->pBt->pageSize - p->pBt->usableSize;
1747 sqlite3BtreeLeave(p);
1748 return n;
drh2011d5f2004-07-22 02:40:37 +00001749}
drhf8e632b2007-05-08 14:51:36 +00001750
1751/*
1752** Set the maximum page count for a database if mxPage is positive.
1753** No changes are made if mxPage is 0 or negative.
1754** Regardless of the value of mxPage, return the maximum page count.
1755*/
1756int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
drhd677b3d2007-08-20 22:48:41 +00001757 int n;
1758 sqlite3BtreeEnter(p);
1759 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
1760 sqlite3BtreeLeave(p);
1761 return n;
drhf8e632b2007-05-08 14:51:36 +00001762}
danielk1977576ec6b2005-01-21 11:55:25 +00001763#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001764
1765/*
danielk1977951af802004-11-05 15:45:09 +00001766** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1767** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1768** is disabled. The default value for the auto-vacuum property is
1769** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1770*/
danielk1977aef0bf62005-12-30 16:28:01 +00001771int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
danielk1977951af802004-11-05 15:45:09 +00001772#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001773 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001774#else
danielk1977dddbcdc2007-04-26 14:42:34 +00001775 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001776 int rc = SQLITE_OK;
drh076d4662009-02-18 20:31:18 +00001777 u8 av = (u8)autoVacuum;
drhd677b3d2007-08-20 22:48:41 +00001778
1779 sqlite3BtreeEnter(p);
drh076d4662009-02-18 20:31:18 +00001780 if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00001781 rc = SQLITE_READONLY;
1782 }else{
drh076d4662009-02-18 20:31:18 +00001783 pBt->autoVacuum = av ?1:0;
1784 pBt->incrVacuum = av==2 ?1:0;
danielk1977951af802004-11-05 15:45:09 +00001785 }
drhd677b3d2007-08-20 22:48:41 +00001786 sqlite3BtreeLeave(p);
1787 return rc;
danielk1977951af802004-11-05 15:45:09 +00001788#endif
1789}
1790
1791/*
1792** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1793** enabled 1 is returned. Otherwise 0.
1794*/
danielk1977aef0bf62005-12-30 16:28:01 +00001795int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:09 +00001796#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001797 return BTREE_AUTOVACUUM_NONE;
danielk1977951af802004-11-05 15:45:09 +00001798#else
drhd677b3d2007-08-20 22:48:41 +00001799 int rc;
1800 sqlite3BtreeEnter(p);
1801 rc = (
danielk1977dddbcdc2007-04-26 14:42:34 +00001802 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1803 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1804 BTREE_AUTOVACUUM_INCR
1805 );
drhd677b3d2007-08-20 22:48:41 +00001806 sqlite3BtreeLeave(p);
1807 return rc;
danielk1977951af802004-11-05 15:45:09 +00001808#endif
1809}
1810
1811
1812/*
drha34b6762004-05-07 13:30:42 +00001813** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001814** also acquire a readlock on that file.
1815**
1816** SQLITE_OK is returned on success. If the file is not a
1817** well-formed database file, then SQLITE_CORRUPT is returned.
1818** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
drh4f0ee682007-03-30 20:43:40 +00001819** is returned if we run out of memory.
drh306dc212001-05-21 13:45:10 +00001820*/
danielk1977aef0bf62005-12-30 16:28:01 +00001821static int lockBtree(BtShared *pBt){
danielk1977f653d782008-03-20 11:04:21 +00001822 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001823 MemPage *pPage1;
danielk197793f7af92008-05-09 16:57:50 +00001824 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001825
drh1fee73e2007-08-29 04:00:57 +00001826 assert( sqlite3_mutex_held(pBt->mutex) );
drha34b6762004-05-07 13:30:42 +00001827 if( pBt->pPage1 ) return SQLITE_OK;
drh16a9b832007-05-05 18:39:25 +00001828 rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:10 +00001829 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +00001830
1831 /* Do some checking to help insure the file we opened really is
1832 ** a valid database file.
1833 */
danielk1977ad0132d2008-06-07 08:58:22 +00001834 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1835 if( rc!=SQLITE_OK ){
danielk197793f7af92008-05-09 16:57:50 +00001836 goto page1_init_failed;
1837 }else if( nPage>0 ){
danielk1977f653d782008-03-20 11:04:21 +00001838 int pageSize;
1839 int usableSize;
drhb6f41482004-05-14 01:58:11 +00001840 u8 *page1 = pPage1->aData;
danielk1977ad0132d2008-06-07 08:58:22 +00001841 rc = SQLITE_NOTADB;
drhb6f41482004-05-14 01:58:11 +00001842 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001843 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001844 }
drh309169a2007-04-24 17:27:51 +00001845 if( page1[18]>1 ){
1846 pBt->readOnly = 1;
1847 }
1848 if( page1[19]>1 ){
drhb6f41482004-05-14 01:58:11 +00001849 goto page1_init_failed;
1850 }
drhe5ae5732008-06-15 02:51:47 +00001851
1852 /* The maximum embedded fraction must be exactly 25%. And the minimum
1853 ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
1854 ** The original design allowed these amounts to vary, but as of
1855 ** version 3.6.0, we require them to be fixed.
1856 */
1857 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
1858 goto page1_init_failed;
1859 }
drh07d183d2005-05-01 22:52:42 +00001860 pageSize = get2byte(&page1[16]);
drh7dc385e2007-09-06 23:39:36 +00001861 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
1862 (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
1863 ){
drh07d183d2005-05-01 22:52:42 +00001864 goto page1_init_failed;
1865 }
1866 assert( (pageSize & 7)==0 );
danielk1977f653d782008-03-20 11:04:21 +00001867 usableSize = pageSize - page1[20];
1868 if( pageSize!=pBt->pageSize ){
1869 /* After reading the first page of the database assuming a page size
1870 ** of BtShared.pageSize, we have discovered that the page-size is
1871 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
1872 ** zero and return SQLITE_OK. The caller will call this function
1873 ** again with the correct page-size.
1874 */
1875 releasePage(pPage1);
drhf49661a2008-12-10 16:45:50 +00001876 pBt->usableSize = (u16)usableSize;
1877 pBt->pageSize = (u16)pageSize;
drhf7141992008-06-19 00:16:08 +00001878 freeTempSpace(pBt);
danielk1977f653d782008-03-20 11:04:21 +00001879 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1880 return SQLITE_OK;
1881 }
1882 if( usableSize<500 ){
drhb6f41482004-05-14 01:58:11 +00001883 goto page1_init_failed;
1884 }
drh1bd10f82008-12-10 21:19:56 +00001885 pBt->pageSize = (u16)pageSize;
1886 pBt->usableSize = (u16)usableSize;
drh057cd3a2005-02-15 16:23:02 +00001887#ifndef SQLITE_OMIT_AUTOVACUUM
1888 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
danielk197727b1f952007-06-25 08:16:58 +00001889 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
drh057cd3a2005-02-15 16:23:02 +00001890#endif
drh306dc212001-05-21 13:45:10 +00001891 }
drhb6f41482004-05-14 01:58:11 +00001892
1893 /* maxLocal is the maximum amount of payload to store locally for
1894 ** a cell. Make sure it is small enough so that at least minFanout
1895 ** cells can will fit on one page. We assume a 10-byte page header.
1896 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001897 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001898 ** 4-byte child pointer
1899 ** 9-byte nKey value
1900 ** 4-byte nData value
1901 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001902 ** So a cell consists of a 2-byte poiner, a header which is as much as
1903 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1904 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001905 */
drhe5ae5732008-06-15 02:51:47 +00001906 pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23;
1907 pBt->minLocal = (pBt->usableSize-12)*32/255 - 23;
drh43605152004-05-29 21:46:49 +00001908 pBt->maxLeaf = pBt->usableSize - 35;
drhe5ae5732008-06-15 02:51:47 +00001909 pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23;
drh2e38c322004-09-03 18:38:44 +00001910 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001911 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001912 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001913
drh72f82862001-05-24 21:06:34 +00001914page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001915 releasePage(pPage1);
1916 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001917 return rc;
drh306dc212001-05-21 13:45:10 +00001918}
1919
1920/*
drhb8ef32c2005-03-14 02:01:49 +00001921** This routine works like lockBtree() except that it also invokes the
1922** busy callback if there is lock contention.
1923*/
danielk1977aef0bf62005-12-30 16:28:01 +00001924static int lockBtreeWithRetry(Btree *pRef){
drhb8ef32c2005-03-14 02:01:49 +00001925 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00001926
drh1fee73e2007-08-29 04:00:57 +00001927 assert( sqlite3BtreeHoldsMutex(pRef) );
danielk1977aef0bf62005-12-30 16:28:01 +00001928 if( pRef->inTrans==TRANS_NONE ){
1929 u8 inTransaction = pRef->pBt->inTransaction;
1930 btreeIntegrity(pRef);
1931 rc = sqlite3BtreeBeginTrans(pRef, 0);
1932 pRef->pBt->inTransaction = inTransaction;
1933 pRef->inTrans = TRANS_NONE;
1934 if( rc==SQLITE_OK ){
1935 pRef->pBt->nTransaction--;
1936 }
1937 btreeIntegrity(pRef);
drhb8ef32c2005-03-14 02:01:49 +00001938 }
1939 return rc;
1940}
1941
1942
1943/*
drhb8ca3072001-12-05 00:21:20 +00001944** If there are no outstanding cursors and we are not in the middle
1945** of a transaction but there is a read lock on the database, then
1946** this routine unrefs the first page of the database file which
1947** has the effect of releasing the read lock.
1948**
1949** If there are any outstanding cursors, this routine is a no-op.
1950**
1951** If there is a transaction in progress, this routine is a no-op.
1952*/
danielk1977aef0bf62005-12-30 16:28:01 +00001953static void unlockBtreeIfUnused(BtShared *pBt){
drh1fee73e2007-08-29 04:00:57 +00001954 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00001955 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00001956 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001957 assert( pBt->pPage1->aData );
1958#if 0
drh24c9a2e2007-01-05 02:00:47 +00001959 if( pBt->pPage1->aData==0 ){
1960 MemPage *pPage = pBt->pPage1;
drhbf4bca52007-09-06 22:19:14 +00001961 pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
drh24c9a2e2007-01-05 02:00:47 +00001962 pPage->pBt = pBt;
1963 pPage->pgno = 1;
1964 }
drhde4fcfd2008-01-19 23:50:26 +00001965#endif
drh24c9a2e2007-01-05 02:00:47 +00001966 releasePage(pBt->pPage1);
drh51c6d962004-06-06 00:42:25 +00001967 }
drh3aac2dd2004-04-26 14:10:20 +00001968 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001969 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001970 }
1971}
1972
1973/*
drh9e572e62004-04-23 23:43:10 +00001974** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001975** file.
drh8b2f49b2001-06-08 00:21:52 +00001976*/
danielk1977aef0bf62005-12-30 16:28:01 +00001977static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:10 +00001978 MemPage *pP1;
1979 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001980 int rc;
danielk1977ad0132d2008-06-07 08:58:22 +00001981 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001982
drh1fee73e2007-08-29 04:00:57 +00001983 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977ad0132d2008-06-07 08:58:22 +00001984 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1985 if( rc!=SQLITE_OK || nPage>0 ){
1986 return rc;
1987 }
drh3aac2dd2004-04-26 14:10:20 +00001988 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001989 assert( pP1!=0 );
1990 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001991 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00001992 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001993 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1994 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001995 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001996 data[18] = 1;
1997 data[19] = 1;
drhf49661a2008-12-10 16:45:50 +00001998 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
1999 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
drhe5ae5732008-06-15 02:51:47 +00002000 data[21] = 64;
2001 data[22] = 32;
2002 data[23] = 32;
drhb6f41482004-05-14 01:58:11 +00002003 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00002004 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00002005 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00002006#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00002007 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
danielk1977418899a2007-06-24 10:14:00 +00002008 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
danielk1977dddbcdc2007-04-26 14:42:34 +00002009 put4byte(&data[36 + 4*4], pBt->autoVacuum);
danielk1977418899a2007-06-24 10:14:00 +00002010 put4byte(&data[36 + 7*4], pBt->incrVacuum);
danielk1977003ba062004-11-04 02:57:33 +00002011#endif
drh8b2f49b2001-06-08 00:21:52 +00002012 return SQLITE_OK;
2013}
2014
2015/*
danielk1977ee5741e2004-05-31 10:01:34 +00002016** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00002017** is started if the second argument is nonzero, otherwise a read-
2018** transaction. If the second argument is 2 or more and exclusive
2019** transaction is started, meaning that no other process is allowed
2020** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:49 +00002021** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:42 +00002022** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00002023**
danielk1977ee5741e2004-05-31 10:01:34 +00002024** A write-transaction must be started before attempting any
2025** changes to the database. None of the following routines
2026** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00002027**
drh23e11ca2004-05-04 17:27:28 +00002028** sqlite3BtreeCreateTable()
2029** sqlite3BtreeCreateIndex()
2030** sqlite3BtreeClearTable()
2031** sqlite3BtreeDropTable()
2032** sqlite3BtreeInsert()
2033** sqlite3BtreeDelete()
2034** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00002035**
drhb8ef32c2005-03-14 02:01:49 +00002036** If an initial attempt to acquire the lock fails because of lock contention
2037** and the database was previously unlocked, then invoke the busy handler
2038** if there is one. But if there was previously a read-lock, do not
2039** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
2040** returned when there is already a read-lock in order to avoid a deadlock.
2041**
2042** Suppose there are two processes A and B. A has a read lock and B has
2043** a reserved lock. B tries to promote to exclusive but is blocked because
2044** of A's read lock. A tries to promote to reserved but is blocked by B.
2045** One or the other of the two processes must give way or there can be
2046** no progress. By returning SQLITE_BUSY and not invoking the busy callback
2047** when A already has a read lock, we encourage A to give up and let B
2048** proceed.
drha059ad02001-04-17 20:09:11 +00002049*/
danielk1977aef0bf62005-12-30 16:28:01 +00002050int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
2051 BtShared *pBt = p->pBt;
danielk1977ee5741e2004-05-31 10:01:34 +00002052 int rc = SQLITE_OK;
2053
drhd677b3d2007-08-20 22:48:41 +00002054 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002055 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00002056 btreeIntegrity(p);
2057
danielk1977ee5741e2004-05-31 10:01:34 +00002058 /* If the btree is already in a write-transaction, or it
2059 ** is already in a read-transaction and a read-transaction
2060 ** is requested, this is a no-op.
2061 */
danielk1977aef0bf62005-12-30 16:28:01 +00002062 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
drhd677b3d2007-08-20 22:48:41 +00002063 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00002064 }
drhb8ef32c2005-03-14 02:01:49 +00002065
2066 /* Write transactions are not possible on a read-only database */
danielk1977ee5741e2004-05-31 10:01:34 +00002067 if( pBt->readOnly && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00002068 rc = SQLITE_READONLY;
2069 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00002070 }
2071
danielk1977aef0bf62005-12-30 16:28:01 +00002072 /* If another database handle has already opened a write transaction
2073 ** on this shared-btree structure and a second write transaction is
2074 ** requested, return SQLITE_BUSY.
2075 */
2076 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00002077 rc = SQLITE_BUSY;
2078 goto trans_begun;
danielk1977aef0bf62005-12-30 16:28:01 +00002079 }
2080
danielk1977641b0f42007-12-21 04:47:25 +00002081#ifndef SQLITE_OMIT_SHARED_CACHE
2082 if( wrflag>1 ){
2083 BtLock *pIter;
2084 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
2085 if( pIter->pBtree!=p ){
2086 rc = SQLITE_BUSY;
2087 goto trans_begun;
2088 }
2089 }
2090 }
2091#endif
2092
drhb8ef32c2005-03-14 02:01:49 +00002093 do {
drh8a9c17f2008-05-02 14:23:54 +00002094 if( pBt->pPage1==0 ){
2095 do{
2096 rc = lockBtree(pBt);
2097 }while( pBt->pPage1==0 && rc==SQLITE_OK );
drh8c42ca92001-06-22 19:15:00 +00002098 }
drh309169a2007-04-24 17:27:51 +00002099
drhb8ef32c2005-03-14 02:01:49 +00002100 if( rc==SQLITE_OK && wrflag ){
drh309169a2007-04-24 17:27:51 +00002101 if( pBt->readOnly ){
2102 rc = SQLITE_READONLY;
2103 }else{
danielk1977bea2a942009-01-20 17:06:27 +00002104 rc = sqlite3PagerBegin(pBt->pPager, wrflag>1);
drh309169a2007-04-24 17:27:51 +00002105 if( rc==SQLITE_OK ){
2106 rc = newDatabase(pBt);
2107 }
drhb8ef32c2005-03-14 02:01:49 +00002108 }
2109 }
2110
2111 if( rc==SQLITE_OK ){
drhb8ef32c2005-03-14 02:01:49 +00002112 if( wrflag ) pBt->inStmt = 0;
2113 }else{
2114 unlockBtreeIfUnused(pBt);
2115 }
danielk1977aef0bf62005-12-30 16:28:01 +00002116 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
danielk19771ceedd32008-11-19 10:22:33 +00002117 btreeInvokeBusyHandler(pBt) );
danielk1977aef0bf62005-12-30 16:28:01 +00002118
2119 if( rc==SQLITE_OK ){
2120 if( p->inTrans==TRANS_NONE ){
2121 pBt->nTransaction++;
2122 }
2123 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
2124 if( p->inTrans>pBt->inTransaction ){
2125 pBt->inTransaction = p->inTrans;
2126 }
danielk1977641b0f42007-12-21 04:47:25 +00002127#ifndef SQLITE_OMIT_SHARED_CACHE
2128 if( wrflag>1 ){
2129 assert( !pBt->pExclusive );
2130 pBt->pExclusive = p;
2131 }
2132#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002133 }
2134
drhd677b3d2007-08-20 22:48:41 +00002135
2136trans_begun:
danielk1977fd7f0452008-12-17 17:30:26 +00002137 if( rc==SQLITE_OK && wrflag ){
danielk197712dd5492008-12-18 15:45:07 +00002138 /* This call makes sure that the pager has the correct number of
2139 ** open savepoints. If the second parameter is greater than 0 and
2140 ** the sub-journal is not already open, then it will be opened here.
2141 */
danielk1977fd7f0452008-12-17 17:30:26 +00002142 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
2143 }
danielk197712dd5492008-12-18 15:45:07 +00002144
danielk1977aef0bf62005-12-30 16:28:01 +00002145 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002146 sqlite3BtreeLeave(p);
drhb8ca3072001-12-05 00:21:20 +00002147 return rc;
drha059ad02001-04-17 20:09:11 +00002148}
2149
danielk1977687566d2004-11-02 12:56:41 +00002150#ifndef SQLITE_OMIT_AUTOVACUUM
2151
2152/*
2153** Set the pointer-map entries for all children of page pPage. Also, if
2154** pPage contains cells that point to overflow pages, set the pointer
2155** map entries for the overflow pages as well.
2156*/
2157static int setChildPtrmaps(MemPage *pPage){
2158 int i; /* Counter variable */
2159 int nCell; /* Number of cells in page pPage */
danielk19772df71c72007-05-24 07:22:42 +00002160 int rc; /* Return code */
danielk1977aef0bf62005-12-30 16:28:01 +00002161 BtShared *pBt = pPage->pBt;
drhf49661a2008-12-10 16:45:50 +00002162 u8 isInitOrig = pPage->isInit;
danielk1977687566d2004-11-02 12:56:41 +00002163 Pgno pgno = pPage->pgno;
2164
drh1fee73e2007-08-29 04:00:57 +00002165 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197771d5d2c2008-09-29 11:49:47 +00002166 rc = sqlite3BtreeInitPage(pPage);
danielk19772df71c72007-05-24 07:22:42 +00002167 if( rc!=SQLITE_OK ){
2168 goto set_child_ptrmaps_out;
2169 }
danielk1977687566d2004-11-02 12:56:41 +00002170 nCell = pPage->nCell;
2171
2172 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00002173 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00002174
danielk197726836652005-01-17 01:33:13 +00002175 rc = ptrmapPutOvflPtr(pPage, pCell);
2176 if( rc!=SQLITE_OK ){
2177 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002178 }
danielk197726836652005-01-17 01:33:13 +00002179
danielk1977687566d2004-11-02 12:56:41 +00002180 if( !pPage->leaf ){
2181 Pgno childPgno = get4byte(pCell);
2182 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
danielk197700a696d2008-09-29 16:41:31 +00002183 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002184 }
2185 }
2186
2187 if( !pPage->leaf ){
2188 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2189 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2190 }
2191
2192set_child_ptrmaps_out:
2193 pPage->isInit = isInitOrig;
2194 return rc;
2195}
2196
2197/*
2198** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
2199** page, is a pointer to page iFrom. Modify this pointer so that it points to
2200** iTo. Parameter eType describes the type of pointer to be modified, as
2201** follows:
2202**
2203** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2204** page of pPage.
2205**
2206** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2207** page pointed to by one of the cells on pPage.
2208**
2209** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2210** overflow page in the list.
2211*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00002212static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
drh1fee73e2007-08-29 04:00:57 +00002213 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhc5053fb2008-11-27 02:22:10 +00002214 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977687566d2004-11-02 12:56:41 +00002215 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00002216 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00002217 if( get4byte(pPage->aData)!=iFrom ){
drh49285702005-09-17 15:20:26 +00002218 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002219 }
danielk1977f78fc082004-11-02 14:40:32 +00002220 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00002221 }else{
drhf49661a2008-12-10 16:45:50 +00002222 u8 isInitOrig = pPage->isInit;
danielk1977687566d2004-11-02 12:56:41 +00002223 int i;
2224 int nCell;
2225
danielk197771d5d2c2008-09-29 11:49:47 +00002226 sqlite3BtreeInitPage(pPage);
danielk1977687566d2004-11-02 12:56:41 +00002227 nCell = pPage->nCell;
2228
danielk1977687566d2004-11-02 12:56:41 +00002229 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00002230 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00002231 if( eType==PTRMAP_OVERFLOW1 ){
2232 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00002233 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
danielk1977687566d2004-11-02 12:56:41 +00002234 if( info.iOverflow ){
2235 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
2236 put4byte(&pCell[info.iOverflow], iTo);
2237 break;
2238 }
2239 }
2240 }else{
2241 if( get4byte(pCell)==iFrom ){
2242 put4byte(pCell, iTo);
2243 break;
2244 }
2245 }
2246 }
2247
2248 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00002249 if( eType!=PTRMAP_BTREE ||
2250 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
drh49285702005-09-17 15:20:26 +00002251 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002252 }
danielk1977687566d2004-11-02 12:56:41 +00002253 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2254 }
2255
2256 pPage->isInit = isInitOrig;
2257 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002258 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002259}
2260
danielk1977003ba062004-11-04 02:57:33 +00002261
danielk19777701e812005-01-10 12:59:51 +00002262/*
2263** Move the open database page pDbPage to location iFreePage in the
2264** database. The pDbPage reference remains valid.
2265*/
danielk1977003ba062004-11-04 02:57:33 +00002266static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:01 +00002267 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:51 +00002268 MemPage *pDbPage, /* Open page to move */
2269 u8 eType, /* Pointer map 'type' entry for pDbPage */
2270 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
danielk19774c999992008-07-16 18:17:55 +00002271 Pgno iFreePage, /* The location to move pDbPage to */
2272 int isCommit
danielk1977003ba062004-11-04 02:57:33 +00002273){
2274 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2275 Pgno iDbPage = pDbPage->pgno;
2276 Pager *pPager = pBt->pPager;
2277 int rc;
2278
danielk1977a0bf2652004-11-04 14:30:04 +00002279 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2280 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
drh1fee73e2007-08-29 04:00:57 +00002281 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +00002282 assert( pDbPage->pBt==pBt );
danielk1977003ba062004-11-04 02:57:33 +00002283
drh85b623f2007-12-13 21:54:09 +00002284 /* Move page iDbPage from its current location to page number iFreePage */
danielk1977003ba062004-11-04 02:57:33 +00002285 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2286 iDbPage, iFreePage, iPtrPage, eType));
danielk19774c999992008-07-16 18:17:55 +00002287 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
danielk1977003ba062004-11-04 02:57:33 +00002288 if( rc!=SQLITE_OK ){
2289 return rc;
2290 }
2291 pDbPage->pgno = iFreePage;
2292
2293 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2294 ** that point to overflow pages. The pointer map entries for all these
2295 ** pages need to be changed.
2296 **
2297 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2298 ** pointer to a subsequent overflow page. If this is the case, then
2299 ** the pointer map needs to be updated for the subsequent overflow page.
2300 */
danielk1977a0bf2652004-11-04 14:30:04 +00002301 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00002302 rc = setChildPtrmaps(pDbPage);
2303 if( rc!=SQLITE_OK ){
2304 return rc;
2305 }
2306 }else{
2307 Pgno nextOvfl = get4byte(pDbPage->aData);
2308 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00002309 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2310 if( rc!=SQLITE_OK ){
2311 return rc;
2312 }
2313 }
2314 }
2315
2316 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2317 ** that it points at iFreePage. Also fix the pointer map entry for
2318 ** iPtrPage.
2319 */
danielk1977a0bf2652004-11-04 14:30:04 +00002320 if( eType!=PTRMAP_ROOTPAGE ){
drh16a9b832007-05-05 18:39:25 +00002321 rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00002322 if( rc!=SQLITE_OK ){
2323 return rc;
2324 }
danielk19773b8a05f2007-03-19 17:44:26 +00002325 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:04 +00002326 if( rc!=SQLITE_OK ){
2327 releasePage(pPtrPage);
2328 return rc;
2329 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002330 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00002331 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00002332 if( rc==SQLITE_OK ){
2333 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2334 }
danielk1977003ba062004-11-04 02:57:33 +00002335 }
danielk1977003ba062004-11-04 02:57:33 +00002336 return rc;
2337}
2338
danielk1977dddbcdc2007-04-26 14:42:34 +00002339/* Forward declaration required by incrVacuumStep(). */
drh4f0c5872007-03-26 22:05:01 +00002340static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00002341
2342/*
danielk1977dddbcdc2007-04-26 14:42:34 +00002343** Perform a single step of an incremental-vacuum. If successful,
2344** return SQLITE_OK. If there is no work to do (and therefore no
2345** point in calling this function again), return SQLITE_DONE.
2346**
2347** More specificly, this function attempts to re-organize the
2348** database so that the last page of the file currently in use
2349** is no longer in use.
2350**
2351** If the nFin parameter is non-zero, the implementation assumes
2352** that the caller will keep calling incrVacuumStep() until
2353** it returns SQLITE_DONE or an error, and that nFin is the
2354** number of pages the database file will contain after this
2355** process is complete.
2356*/
danielk19773460d192008-12-27 15:23:13 +00002357static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
danielk1977dddbcdc2007-04-26 14:42:34 +00002358 Pgno nFreeList; /* Number of pages still on the free-list */
2359
drh1fee73e2007-08-29 04:00:57 +00002360 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977dddbcdc2007-04-26 14:42:34 +00002361
2362 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
2363 int rc;
2364 u8 eType;
2365 Pgno iPtrPage;
2366
2367 nFreeList = get4byte(&pBt->pPage1->aData[36]);
2368 if( nFreeList==0 || nFin==iLastPg ){
2369 return SQLITE_DONE;
2370 }
2371
2372 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
2373 if( rc!=SQLITE_OK ){
2374 return rc;
2375 }
2376 if( eType==PTRMAP_ROOTPAGE ){
2377 return SQLITE_CORRUPT_BKPT;
2378 }
2379
2380 if( eType==PTRMAP_FREEPAGE ){
2381 if( nFin==0 ){
2382 /* Remove the page from the files free-list. This is not required
danielk19774ef24492007-05-23 09:52:41 +00002383 ** if nFin is non-zero. In that case, the free-list will be
danielk1977dddbcdc2007-04-26 14:42:34 +00002384 ** truncated to zero after this function returns, so it doesn't
2385 ** matter if it still contains some garbage entries.
2386 */
2387 Pgno iFreePg;
2388 MemPage *pFreePg;
2389 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
2390 if( rc!=SQLITE_OK ){
2391 return rc;
2392 }
2393 assert( iFreePg==iLastPg );
2394 releasePage(pFreePg);
2395 }
2396 } else {
2397 Pgno iFreePg; /* Index of free page to move pLastPg to */
2398 MemPage *pLastPg;
2399
drh16a9b832007-05-05 18:39:25 +00002400 rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
danielk1977dddbcdc2007-04-26 14:42:34 +00002401 if( rc!=SQLITE_OK ){
2402 return rc;
2403 }
2404
danielk1977b4626a32007-04-28 15:47:43 +00002405 /* If nFin is zero, this loop runs exactly once and page pLastPg
2406 ** is swapped with the first free page pulled off the free list.
2407 **
2408 ** On the other hand, if nFin is greater than zero, then keep
2409 ** looping until a free-page located within the first nFin pages
2410 ** of the file is found.
2411 */
danielk1977dddbcdc2007-04-26 14:42:34 +00002412 do {
2413 MemPage *pFreePg;
2414 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
2415 if( rc!=SQLITE_OK ){
2416 releasePage(pLastPg);
2417 return rc;
2418 }
2419 releasePage(pFreePg);
2420 }while( nFin!=0 && iFreePg>nFin );
2421 assert( iFreePg<iLastPg );
danielk1977b4626a32007-04-28 15:47:43 +00002422
2423 rc = sqlite3PagerWrite(pLastPg->pDbPage);
danielk1977662278e2007-11-05 15:30:12 +00002424 if( rc==SQLITE_OK ){
danielk19774c999992008-07-16 18:17:55 +00002425 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
danielk1977662278e2007-11-05 15:30:12 +00002426 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002427 releasePage(pLastPg);
2428 if( rc!=SQLITE_OK ){
2429 return rc;
danielk1977662278e2007-11-05 15:30:12 +00002430 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002431 }
2432 }
2433
danielk19773460d192008-12-27 15:23:13 +00002434 if( nFin==0 ){
2435 iLastPg--;
2436 while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
2437 iLastPg--;
2438 }
2439 sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
danielk1977dddbcdc2007-04-26 14:42:34 +00002440 }
2441 return SQLITE_OK;
2442}
2443
2444/*
2445** A write-transaction must be opened before calling this function.
2446** It performs a single unit of work towards an incremental vacuum.
2447**
2448** If the incremental vacuum is finished after this function has run,
2449** SQLITE_DONE is returned. If it is not finished, but no error occured,
2450** SQLITE_OK is returned. Otherwise an SQLite error code.
2451*/
2452int sqlite3BtreeIncrVacuum(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00002453 int rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002454 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002455
2456 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002457 pBt->db = p->db;
danielk1977dddbcdc2007-04-26 14:42:34 +00002458 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
2459 if( !pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00002460 rc = SQLITE_DONE;
2461 }else{
2462 invalidateAllOverflowCache(pBt);
danielk1977bea2a942009-01-20 17:06:27 +00002463 rc = incrVacuumStep(pBt, 0, pagerPagecount(pBt));
danielk1977dddbcdc2007-04-26 14:42:34 +00002464 }
drhd677b3d2007-08-20 22:48:41 +00002465 sqlite3BtreeLeave(p);
2466 return rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002467}
2468
2469/*
danielk19773b8a05f2007-03-19 17:44:26 +00002470** This routine is called prior to sqlite3PagerCommit when a transaction
danielk1977687566d2004-11-02 12:56:41 +00002471** is commited for an auto-vacuum database.
danielk197724168722007-04-02 05:07:47 +00002472**
2473** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
2474** the database file should be truncated to during the commit process.
2475** i.e. the database has been reorganized so that only the first *pnTrunc
2476** pages are in use.
danielk1977687566d2004-11-02 12:56:41 +00002477*/
danielk19773460d192008-12-27 15:23:13 +00002478static int autoVacuumCommit(BtShared *pBt){
danielk1977dddbcdc2007-04-26 14:42:34 +00002479 int rc = SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002480 Pager *pPager = pBt->pPager;
drhf94a1732008-09-30 17:18:17 +00002481 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002482
drh1fee73e2007-08-29 04:00:57 +00002483 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +00002484 invalidateAllOverflowCache(pBt);
danielk1977dddbcdc2007-04-26 14:42:34 +00002485 assert(pBt->autoVacuum);
2486 if( !pBt->incrVacuum ){
danielk19773460d192008-12-27 15:23:13 +00002487 Pgno nFin;
2488 Pgno nFree;
2489 Pgno nPtrmap;
2490 Pgno iFree;
2491 const int pgsz = pBt->pageSize;
2492 Pgno nOrig = pagerPagecount(pBt);
danielk1977687566d2004-11-02 12:56:41 +00002493
danielk19773460d192008-12-27 15:23:13 +00002494 if( PTRMAP_ISPAGE(pBt, nOrig) ){
2495 return SQLITE_CORRUPT_BKPT;
2496 }
2497 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
2498 nOrig--;
2499 }
2500 nFree = get4byte(&pBt->pPage1->aData[36]);
2501 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
2502 nFin = nOrig - nFree - nPtrmap;
2503 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
2504 nFin--;
2505 }
2506 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
2507 nFin--;
danielk1977dddbcdc2007-04-26 14:42:34 +00002508 }
danielk1977687566d2004-11-02 12:56:41 +00002509
danielk19773460d192008-12-27 15:23:13 +00002510 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
2511 rc = incrVacuumStep(pBt, nFin, iFree);
danielk1977dddbcdc2007-04-26 14:42:34 +00002512 }
danielk19773460d192008-12-27 15:23:13 +00002513 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
danielk1977dddbcdc2007-04-26 14:42:34 +00002514 rc = SQLITE_OK;
danielk19773460d192008-12-27 15:23:13 +00002515 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
2516 put4byte(&pBt->pPage1->aData[32], 0);
2517 put4byte(&pBt->pPage1->aData[36], 0);
2518 sqlite3PagerTruncateImage(pBt->pPager, nFin);
danielk1977dddbcdc2007-04-26 14:42:34 +00002519 }
2520 if( rc!=SQLITE_OK ){
2521 sqlite3PagerRollback(pPager);
2522 }
danielk1977687566d2004-11-02 12:56:41 +00002523 }
2524
danielk19773b8a05f2007-03-19 17:44:26 +00002525 assert( nRef==sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002526 return rc;
2527}
danielk1977dddbcdc2007-04-26 14:42:34 +00002528
shane831c3292008-11-10 17:14:58 +00002529#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
danielk1977687566d2004-11-02 12:56:41 +00002530
2531/*
drh80e35f42007-03-30 14:06:34 +00002532** This routine does the first phase of a two-phase commit. This routine
2533** causes a rollback journal to be created (if it does not already exist)
2534** and populated with enough information so that if a power loss occurs
2535** the database can be restored to its original state by playing back
2536** the journal. Then the contents of the journal are flushed out to
2537** the disk. After the journal is safely on oxide, the changes to the
2538** database are written into the database file and flushed to oxide.
2539** At the end of this call, the rollback journal still exists on the
2540** disk and we are still holding all locks, so the transaction has not
2541** committed. See sqlite3BtreeCommit() for the second phase of the
2542** commit process.
2543**
2544** This call is a no-op if no write-transaction is currently active on pBt.
2545**
2546** Otherwise, sync the database file for the btree pBt. zMaster points to
2547** the name of a master journal file that should be written into the
2548** individual journal file, or is NULL, indicating no master journal file
2549** (single database transaction).
2550**
2551** When this is called, the master journal should already have been
2552** created, populated with this journal pointer and synced to disk.
2553**
2554** Once this is routine has returned, the only thing required to commit
2555** the write-transaction for this database file is to delete the journal.
2556*/
2557int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
2558 int rc = SQLITE_OK;
2559 if( p->inTrans==TRANS_WRITE ){
2560 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002561 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002562 pBt->db = p->db;
drh80e35f42007-03-30 14:06:34 +00002563#ifndef SQLITE_OMIT_AUTOVACUUM
2564 if( pBt->autoVacuum ){
danielk19773460d192008-12-27 15:23:13 +00002565 rc = autoVacuumCommit(pBt);
drh80e35f42007-03-30 14:06:34 +00002566 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002567 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002568 return rc;
2569 }
2570 }
2571#endif
drh49b9d332009-01-02 18:10:42 +00002572 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
drhd677b3d2007-08-20 22:48:41 +00002573 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002574 }
2575 return rc;
2576}
2577
2578/*
drh2aa679f2001-06-25 02:11:07 +00002579** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00002580**
drh6e345992007-03-30 11:12:08 +00002581** This routine implements the second phase of a 2-phase commit. The
2582** sqlite3BtreeSync() routine does the first phase and should be invoked
2583** prior to calling this routine. The sqlite3BtreeSync() routine did
2584** all the work of writing information out to disk and flushing the
2585** contents so that they are written onto the disk platter. All this
2586** routine has to do is delete or truncate the rollback journal
2587** (which causes the transaction to commit) and drop locks.
2588**
drh5e00f6c2001-09-13 13:46:56 +00002589** This will release the write lock on the database file. If there
2590** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002591*/
drh80e35f42007-03-30 14:06:34 +00002592int sqlite3BtreeCommitPhaseTwo(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00002593 BtShared *pBt = p->pBt;
2594
drhd677b3d2007-08-20 22:48:41 +00002595 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002596 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00002597 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002598
2599 /* If the handle has a write-transaction open, commit the shared-btrees
2600 ** transaction and set the shared state to TRANS_READ.
2601 */
2602 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:47 +00002603 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002604 assert( pBt->inTransaction==TRANS_WRITE );
2605 assert( pBt->nTransaction>0 );
drh80e35f42007-03-30 14:06:34 +00002606 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
danielk19777f7bc662006-01-23 13:47:47 +00002607 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002608 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002609 return rc;
2610 }
danielk1977aef0bf62005-12-30 16:28:01 +00002611 pBt->inTransaction = TRANS_READ;
2612 pBt->inStmt = 0;
danielk1977ee5741e2004-05-31 10:01:34 +00002613 }
danielk19777f7bc662006-01-23 13:47:47 +00002614 unlockAllTables(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002615
2616 /* If the handle has any kind of transaction open, decrement the transaction
2617 ** count of the shared btree. If the transaction count reaches 0, set
2618 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2619 ** will unlock the pager.
2620 */
2621 if( p->inTrans!=TRANS_NONE ){
2622 pBt->nTransaction--;
2623 if( 0==pBt->nTransaction ){
2624 pBt->inTransaction = TRANS_NONE;
2625 }
2626 }
2627
2628 /* Set the handles current transaction state to TRANS_NONE and unlock
2629 ** the pager if this call closed the only read or write transaction.
2630 */
danielk1977bea2a942009-01-20 17:06:27 +00002631 btreeClearHasContent(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002632 p->inTrans = TRANS_NONE;
drh5e00f6c2001-09-13 13:46:56 +00002633 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002634
2635 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002636 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002637 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002638}
2639
drh80e35f42007-03-30 14:06:34 +00002640/*
2641** Do both phases of a commit.
2642*/
2643int sqlite3BtreeCommit(Btree *p){
2644 int rc;
drhd677b3d2007-08-20 22:48:41 +00002645 sqlite3BtreeEnter(p);
drh80e35f42007-03-30 14:06:34 +00002646 rc = sqlite3BtreeCommitPhaseOne(p, 0);
2647 if( rc==SQLITE_OK ){
2648 rc = sqlite3BtreeCommitPhaseTwo(p);
2649 }
drhd677b3d2007-08-20 22:48:41 +00002650 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002651 return rc;
2652}
2653
danielk1977fbcd5852004-06-15 02:44:18 +00002654#ifndef NDEBUG
2655/*
2656** Return the number of write-cursors open on this handle. This is for use
2657** in assert() expressions, so it is only compiled if NDEBUG is not
2658** defined.
drhfb982642007-08-30 01:19:59 +00002659**
2660** For the purposes of this routine, a write-cursor is any cursor that
2661** is capable of writing to the databse. That means the cursor was
2662** originally opened for writing and the cursor has not be disabled
2663** by having its state changed to CURSOR_FAULT.
danielk1977fbcd5852004-06-15 02:44:18 +00002664*/
danielk1977aef0bf62005-12-30 16:28:01 +00002665static int countWriteCursors(BtShared *pBt){
danielk1977fbcd5852004-06-15 02:44:18 +00002666 BtCursor *pCur;
2667 int r = 0;
2668 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
drhfb982642007-08-30 01:19:59 +00002669 if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
danielk1977fbcd5852004-06-15 02:44:18 +00002670 }
2671 return r;
2672}
2673#endif
2674
drhc39e0002004-05-07 23:50:57 +00002675/*
drhfb982642007-08-30 01:19:59 +00002676** This routine sets the state to CURSOR_FAULT and the error
2677** code to errCode for every cursor on BtShared that pBtree
2678** references.
2679**
2680** Every cursor is tripped, including cursors that belong
2681** to other database connections that happen to be sharing
2682** the cache with pBtree.
2683**
2684** This routine gets called when a rollback occurs.
2685** All cursors using the same cache must be tripped
2686** to prevent them from trying to use the btree after
2687** the rollback. The rollback may have deleted tables
2688** or moved root pages, so it is not sufficient to
2689** save the state of the cursor. The cursor must be
2690** invalidated.
2691*/
2692void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
2693 BtCursor *p;
2694 sqlite3BtreeEnter(pBtree);
2695 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
danielk1977bc2ca9e2008-11-13 14:28:28 +00002696 int i;
danielk1977be51a652008-10-08 17:58:48 +00002697 sqlite3BtreeClearCursor(p);
drhfb982642007-08-30 01:19:59 +00002698 p->eState = CURSOR_FAULT;
2699 p->skip = errCode;
danielk1977bc2ca9e2008-11-13 14:28:28 +00002700 for(i=0; i<=p->iPage; i++){
2701 releasePage(p->apPage[i]);
2702 p->apPage[i] = 0;
2703 }
drhfb982642007-08-30 01:19:59 +00002704 }
2705 sqlite3BtreeLeave(pBtree);
2706}
2707
2708/*
drhecdc7532001-09-23 02:35:53 +00002709** Rollback the transaction in progress. All cursors will be
2710** invalided by this operation. Any attempt to use a cursor
2711** that was open at the beginning of this operation will result
2712** in an error.
drh5e00f6c2001-09-13 13:46:56 +00002713**
2714** This will release the write lock on the database file. If there
2715** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002716*/
danielk1977aef0bf62005-12-30 16:28:01 +00002717int sqlite3BtreeRollback(Btree *p){
danielk19778d34dfd2006-01-24 16:37:57 +00002718 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002719 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:47 +00002720 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:01 +00002721
drhd677b3d2007-08-20 22:48:41 +00002722 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002723 pBt->db = p->db;
danielk19772b8c13e2006-01-24 14:21:24 +00002724 rc = saveAllCursors(pBt, 0, 0);
danielk19778d34dfd2006-01-24 16:37:57 +00002725#ifndef SQLITE_OMIT_SHARED_CACHE
danielk19772b8c13e2006-01-24 14:21:24 +00002726 if( rc!=SQLITE_OK ){
danielk19778d34dfd2006-01-24 16:37:57 +00002727 /* This is a horrible situation. An IO or malloc() error occured whilst
2728 ** trying to save cursor positions. If this is an automatic rollback (as
2729 ** the result of a constraint, malloc() failure or IO error) then
2730 ** the cache may be internally inconsistent (not contain valid trees) so
2731 ** we cannot simply return the error to the caller. Instead, abort
2732 ** all queries that may be using any of the cursors that failed to save.
2733 */
drhfb982642007-08-30 01:19:59 +00002734 sqlite3BtreeTripAllCursors(p, rc);
danielk19772b8c13e2006-01-24 14:21:24 +00002735 }
danielk19778d34dfd2006-01-24 16:37:57 +00002736#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002737 btreeIntegrity(p);
2738 unlockAllTables(p);
2739
2740 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:57 +00002741 int rc2;
danielk1977aef0bf62005-12-30 16:28:01 +00002742
danielk19778d34dfd2006-01-24 16:37:57 +00002743 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:26 +00002744 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:57 +00002745 if( rc2!=SQLITE_OK ){
2746 rc = rc2;
2747 }
2748
drh24cd67e2004-05-10 16:18:47 +00002749 /* The rollback may have destroyed the pPage1->aData value. So
drh16a9b832007-05-05 18:39:25 +00002750 ** call sqlite3BtreeGetPage() on page 1 again to make
2751 ** sure pPage1->aData is set correctly. */
2752 if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh24cd67e2004-05-10 16:18:47 +00002753 releasePage(pPage1);
2754 }
danielk1977fbcd5852004-06-15 02:44:18 +00002755 assert( countWriteCursors(pBt)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00002756 pBt->inTransaction = TRANS_READ;
drh24cd67e2004-05-10 16:18:47 +00002757 }
danielk1977aef0bf62005-12-30 16:28:01 +00002758
2759 if( p->inTrans!=TRANS_NONE ){
2760 assert( pBt->nTransaction>0 );
2761 pBt->nTransaction--;
2762 if( 0==pBt->nTransaction ){
2763 pBt->inTransaction = TRANS_NONE;
2764 }
2765 }
2766
danielk1977bea2a942009-01-20 17:06:27 +00002767 btreeClearHasContent(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002768 p->inTrans = TRANS_NONE;
danielk1977ee5741e2004-05-31 10:01:34 +00002769 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00002770 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002771
2772 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002773 sqlite3BtreeLeave(p);
drha059ad02001-04-17 20:09:11 +00002774 return rc;
2775}
2776
2777/*
drhab01f612004-05-22 02:55:23 +00002778** Start a statement subtransaction. The subtransaction can
2779** can be rolled back independently of the main transaction.
2780** You must start a transaction before starting a subtransaction.
2781** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00002782** commits or rolls back.
2783**
drhab01f612004-05-22 02:55:23 +00002784** Only one subtransaction may be active at a time. It is an error to try
2785** to start a new subtransaction if another subtransaction is already active.
2786**
2787** Statement subtransactions are used around individual SQL statements
2788** that are contained within a BEGIN...COMMIT block. If a constraint
2789** error occurs within the statement, the effect of that one statement
2790** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002791*/
danielk1977aef0bf62005-12-30 16:28:01 +00002792int sqlite3BtreeBeginStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002793 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002794 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002795 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002796 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002797 assert( p->inTrans==TRANS_WRITE );
2798 assert( !pBt->inStmt );
2799 assert( pBt->readOnly==0 );
2800 if( NEVER(p->inTrans!=TRANS_WRITE || pBt->inStmt || pBt->readOnly) ){
2801 rc = SQLITE_INTERNAL;
drhd677b3d2007-08-20 22:48:41 +00002802 }else{
2803 assert( pBt->inTransaction==TRANS_WRITE );
drh64022502009-01-09 14:11:04 +00002804 /* At the pager level, a statement transaction is a savepoint with
2805 ** an index greater than all savepoints created explicitly using
2806 ** SQL statements. It is illegal to open, release or rollback any
2807 ** such savepoints while the statement transaction savepoint is active.
2808 */
2809 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint+1);
drhd677b3d2007-08-20 22:48:41 +00002810 pBt->inStmt = 1;
drh0d65dc02002-02-03 00:56:09 +00002811 }
drhd677b3d2007-08-20 22:48:41 +00002812 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002813 return rc;
2814}
2815
drh663fc632002-02-02 18:49:19 +00002816/*
drhab01f612004-05-22 02:55:23 +00002817** Commit the statment subtransaction currently in progress. If no
2818** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002819*/
danielk1977aef0bf62005-12-30 16:28:01 +00002820int sqlite3BtreeCommitStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002821 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002822 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002823 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002824 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002825 assert( pBt->readOnly==0 );
2826 if( pBt->inStmt ){
danielk1977fd7f0452008-12-17 17:30:26 +00002827 int iStmtpoint = p->db->nSavepoint;
2828 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint);
drh663fc632002-02-02 18:49:19 +00002829 }else{
2830 rc = SQLITE_OK;
2831 }
drh3aac2dd2004-04-26 14:10:20 +00002832 pBt->inStmt = 0;
drhd677b3d2007-08-20 22:48:41 +00002833 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002834 return rc;
2835}
2836
2837/*
drhab01f612004-05-22 02:55:23 +00002838** Rollback the active statement subtransaction. If no subtransaction
2839** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002840**
drhab01f612004-05-22 02:55:23 +00002841** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002842** to use a cursor that was open at the beginning of this operation
2843** will result in an error.
2844*/
danielk1977aef0bf62005-12-30 16:28:01 +00002845int sqlite3BtreeRollbackStmt(Btree *p){
danielk197797a227c2006-01-20 16:32:04 +00002846 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00002847 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002848 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002849 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002850 assert( pBt->readOnly==0 );
2851 if( pBt->inStmt ){
danielk1977fd7f0452008-12-17 17:30:26 +00002852 int iStmtpoint = p->db->nSavepoint;
2853 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_ROLLBACK, iStmtpoint);
2854 if( rc==SQLITE_OK ){
2855 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint);
2856 }
danielk197797a227c2006-01-20 16:32:04 +00002857 pBt->inStmt = 0;
2858 }
drhd677b3d2007-08-20 22:48:41 +00002859 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002860 return rc;
2861}
2862
2863/*
danielk1977fd7f0452008-12-17 17:30:26 +00002864** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
2865** or SAVEPOINT_RELEASE. This function either releases or rolls back the
danielk197712dd5492008-12-18 15:45:07 +00002866** savepoint identified by parameter iSavepoint, depending on the value
2867** of op.
2868**
2869** Normally, iSavepoint is greater than or equal to zero. However, if op is
2870** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
2871** contents of the entire transaction are rolled back. This is different
2872** from a normal transaction rollback, as no locks are released and the
2873** transaction remains open.
danielk1977fd7f0452008-12-17 17:30:26 +00002874*/
2875int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
2876 int rc = SQLITE_OK;
2877 if( p && p->inTrans==TRANS_WRITE ){
2878 BtShared *pBt = p->pBt;
2879 assert( pBt->inStmt==0 );
2880 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
2881 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
2882 sqlite3BtreeEnter(p);
2883 pBt->db = p->db;
2884 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
drh9f0bbf92009-01-02 21:08:09 +00002885 if( rc==SQLITE_OK ){
2886 rc = newDatabase(pBt);
2887 }
danielk1977fd7f0452008-12-17 17:30:26 +00002888 sqlite3BtreeLeave(p);
2889 }
2890 return rc;
2891}
2892
2893/*
drh8b2f49b2001-06-08 00:21:52 +00002894** Create a new cursor for the BTree whose root is on the page
2895** iTable. The act of acquiring a cursor gets a read lock on
2896** the database file.
drh1bee3d72001-10-15 00:44:35 +00002897**
2898** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002899** If wrFlag==1, then the cursor can be used for reading or for
2900** writing if other conditions for writing are also met. These
2901** are the conditions that must be met in order for writing to
2902** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002903**
drhf74b8d92002-09-01 23:20:45 +00002904** 1: The cursor must have been opened with wrFlag==1
2905**
drhfe5d71d2007-03-19 11:54:10 +00002906** 2: Other database connections that share the same pager cache
2907** but which are not in the READ_UNCOMMITTED state may not have
2908** cursors open with wrFlag==0 on the same table. Otherwise
2909** the changes made by this write cursor would be visible to
2910** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:45 +00002911**
2912** 3: The database must be writable (not on read-only media)
2913**
2914** 4: There must be an active transaction.
2915**
drh6446c4d2001-12-15 14:22:18 +00002916** No checking is done to make sure that page iTable really is the
2917** root page of a b-tree. If it is not, then the cursor acquired
2918** will not work correctly.
danielk197771d5d2c2008-09-29 11:49:47 +00002919**
2920** It is assumed that the sqlite3BtreeCursorSize() bytes of memory
2921** pointed to by pCur have been zeroed by the caller.
drha059ad02001-04-17 20:09:11 +00002922*/
drhd677b3d2007-08-20 22:48:41 +00002923static int btreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002924 Btree *p, /* The btree */
2925 int iTable, /* Root page of table to open */
2926 int wrFlag, /* 1 to write. 0 read-only */
2927 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
2928 BtCursor *pCur /* Space for new cursor */
drh3aac2dd2004-04-26 14:10:20 +00002929){
drha059ad02001-04-17 20:09:11 +00002930 int rc;
danielk197789d40042008-11-17 14:20:56 +00002931 Pgno nPage;
danielk1977aef0bf62005-12-30 16:28:01 +00002932 BtShared *pBt = p->pBt;
drhecdc7532001-09-23 02:35:53 +00002933
drh1fee73e2007-08-29 04:00:57 +00002934 assert( sqlite3BtreeHoldsMutex(p) );
drhf49661a2008-12-10 16:45:50 +00002935 assert( wrFlag==0 || wrFlag==1 );
drh8dcd7ca2004-08-08 19:43:29 +00002936 if( wrFlag ){
drh64022502009-01-09 14:11:04 +00002937 assert( !pBt->readOnly );
2938 if( NEVER(pBt->readOnly) ){
drh8dcd7ca2004-08-08 19:43:29 +00002939 return SQLITE_READONLY;
2940 }
danielk19773588ceb2008-06-10 17:30:26 +00002941 if( checkReadLocks(p, iTable, 0, 0) ){
drh8dcd7ca2004-08-08 19:43:29 +00002942 return SQLITE_LOCKED;
2943 }
drha0c9a112004-03-10 13:42:37 +00002944 }
danielk1977aef0bf62005-12-30 16:28:01 +00002945
drh4b70f112004-05-02 21:12:19 +00002946 if( pBt->pPage1==0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002947 rc = lockBtreeWithRetry(p);
drha059ad02001-04-17 20:09:11 +00002948 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002949 return rc;
2950 }
2951 }
drh8b2f49b2001-06-08 00:21:52 +00002952 pCur->pgnoRoot = (Pgno)iTable;
danielk197789d40042008-11-17 14:20:56 +00002953 rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage);
2954 if( rc!=SQLITE_OK ){
2955 return rc;
2956 }
2957 if( iTable==1 && nPage==0 ){
drh24cd67e2004-05-10 16:18:47 +00002958 rc = SQLITE_EMPTY;
2959 goto create_cursor_exception;
2960 }
danielk197771d5d2c2008-09-29 11:49:47 +00002961 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
drhbd03cae2001-06-02 02:40:57 +00002962 if( rc!=SQLITE_OK ){
2963 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002964 }
danielk1977aef0bf62005-12-30 16:28:01 +00002965
danielk1977aef0bf62005-12-30 16:28:01 +00002966 /* Now that no other errors can occur, finish filling in the BtCursor
2967 ** variables, link the cursor into the BtShared list and set *ppCur (the
2968 ** output argument to this function).
2969 */
drh1e968a02008-03-25 00:22:21 +00002970 pCur->pKeyInfo = pKeyInfo;
danielk1977aef0bf62005-12-30 16:28:01 +00002971 pCur->pBtree = p;
drhd0679ed2007-08-28 22:24:34 +00002972 pCur->pBt = pBt;
drhf49661a2008-12-10 16:45:50 +00002973 pCur->wrFlag = (u8)wrFlag;
drha059ad02001-04-17 20:09:11 +00002974 pCur->pNext = pBt->pCursor;
2975 if( pCur->pNext ){
2976 pCur->pNext->pPrev = pCur;
2977 }
2978 pBt->pCursor = pCur;
danielk1977da184232006-01-05 11:34:32 +00002979 pCur->eState = CURSOR_INVALID;
drhbd03cae2001-06-02 02:40:57 +00002980
danielk1977aef0bf62005-12-30 16:28:01 +00002981 return SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00002982
drhbd03cae2001-06-02 02:40:57 +00002983create_cursor_exception:
danielk197771d5d2c2008-09-29 11:49:47 +00002984 releasePage(pCur->apPage[0]);
drh5e00f6c2001-09-13 13:46:56 +00002985 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002986 return rc;
drha059ad02001-04-17 20:09:11 +00002987}
drhd677b3d2007-08-20 22:48:41 +00002988int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002989 Btree *p, /* The btree */
2990 int iTable, /* Root page of table to open */
2991 int wrFlag, /* 1 to write. 0 read-only */
2992 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
2993 BtCursor *pCur /* Write new cursor here */
drhd677b3d2007-08-20 22:48:41 +00002994){
2995 int rc;
2996 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002997 p->pBt->db = p->db;
danielk1977cd3e8f72008-03-25 09:47:35 +00002998 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
drhd677b3d2007-08-20 22:48:41 +00002999 sqlite3BtreeLeave(p);
3000 return rc;
3001}
danielk1977cd3e8f72008-03-25 09:47:35 +00003002int sqlite3BtreeCursorSize(){
3003 return sizeof(BtCursor);
3004}
3005
drhd677b3d2007-08-20 22:48:41 +00003006
drha059ad02001-04-17 20:09:11 +00003007
3008/*
drh5e00f6c2001-09-13 13:46:56 +00003009** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00003010** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00003011*/
drh3aac2dd2004-04-26 14:10:20 +00003012int sqlite3BtreeCloseCursor(BtCursor *pCur){
drhff0587c2007-08-29 17:43:19 +00003013 Btree *pBtree = pCur->pBtree;
danielk1977cd3e8f72008-03-25 09:47:35 +00003014 if( pBtree ){
danielk197771d5d2c2008-09-29 11:49:47 +00003015 int i;
danielk1977cd3e8f72008-03-25 09:47:35 +00003016 BtShared *pBt = pCur->pBt;
3017 sqlite3BtreeEnter(pBtree);
3018 pBt->db = pBtree->db;
danielk1977be51a652008-10-08 17:58:48 +00003019 sqlite3BtreeClearCursor(pCur);
danielk1977cd3e8f72008-03-25 09:47:35 +00003020 if( pCur->pPrev ){
3021 pCur->pPrev->pNext = pCur->pNext;
3022 }else{
3023 pBt->pCursor = pCur->pNext;
3024 }
3025 if( pCur->pNext ){
3026 pCur->pNext->pPrev = pCur->pPrev;
3027 }
danielk197771d5d2c2008-09-29 11:49:47 +00003028 for(i=0; i<=pCur->iPage; i++){
3029 releasePage(pCur->apPage[i]);
3030 }
danielk1977cd3e8f72008-03-25 09:47:35 +00003031 unlockBtreeIfUnused(pBt);
3032 invalidateOverflowCache(pCur);
3033 /* sqlite3_free(pCur); */
3034 sqlite3BtreeLeave(pBtree);
drha059ad02001-04-17 20:09:11 +00003035 }
drh8c42ca92001-06-22 19:15:00 +00003036 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00003037}
3038
drh7e3b0a02001-04-28 16:52:40 +00003039/*
drh5e2f8b92001-05-28 00:41:15 +00003040** Make a temporary cursor by filling in the fields of pTempCur.
3041** The temporary cursor is not on the cursor list for the Btree.
3042*/
drh16a9b832007-05-05 18:39:25 +00003043void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
danielk197771d5d2c2008-09-29 11:49:47 +00003044 int i;
drh1fee73e2007-08-29 04:00:57 +00003045 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00003046 memcpy(pTempCur, pCur, sizeof(BtCursor));
drh5e2f8b92001-05-28 00:41:15 +00003047 pTempCur->pNext = 0;
3048 pTempCur->pPrev = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003049 for(i=0; i<=pTempCur->iPage; i++){
3050 sqlite3PagerRef(pTempCur->apPage[i]->pDbPage);
drhecdc7532001-09-23 02:35:53 +00003051 }
danielk197736e20932008-11-26 07:40:30 +00003052 assert( pTempCur->pKey==0 );
drh5e2f8b92001-05-28 00:41:15 +00003053}
3054
3055/*
drhbd03cae2001-06-02 02:40:57 +00003056** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00003057** function above.
3058*/
drh16a9b832007-05-05 18:39:25 +00003059void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
danielk197771d5d2c2008-09-29 11:49:47 +00003060 int i;
drh1fee73e2007-08-29 04:00:57 +00003061 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00003062 for(i=0; i<=pCur->iPage; i++){
3063 sqlite3PagerUnref(pCur->apPage[i]->pDbPage);
drhecdc7532001-09-23 02:35:53 +00003064 }
danielk197736e20932008-11-26 07:40:30 +00003065 sqlite3_free(pCur->pKey);
drh5e2f8b92001-05-28 00:41:15 +00003066}
3067
3068/*
drh86057612007-06-26 01:04:48 +00003069** Make sure the BtCursor* given in the argument has a valid
3070** BtCursor.info structure. If it is not already valid, call
danielk19771cc5ed82007-05-16 17:28:43 +00003071** sqlite3BtreeParseCell() to fill it in.
drhab01f612004-05-22 02:55:23 +00003072**
3073** BtCursor.info is a cache of the information in the current cell.
drh16a9b832007-05-05 18:39:25 +00003074** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
drh86057612007-06-26 01:04:48 +00003075**
3076** 2007-06-25: There is a bug in some versions of MSVC that cause the
3077** compiler to crash when getCellInfo() is implemented as a macro.
3078** But there is a measureable speed advantage to using the macro on gcc
3079** (when less compiler optimizations like -Os or -O0 are used and the
3080** compiler is not doing agressive inlining.) So we use a real function
3081** for MSVC and a macro for everything else. Ticket #2457.
drh9188b382004-05-14 21:12:22 +00003082*/
drh9188b382004-05-14 21:12:22 +00003083#ifndef NDEBUG
danielk19771cc5ed82007-05-16 17:28:43 +00003084 static void assertCellInfo(BtCursor *pCur){
drh9188b382004-05-14 21:12:22 +00003085 CellInfo info;
danielk197771d5d2c2008-09-29 11:49:47 +00003086 int iPage = pCur->iPage;
drh51c6d962004-06-06 00:42:25 +00003087 memset(&info, 0, sizeof(info));
danielk197771d5d2c2008-09-29 11:49:47 +00003088 sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
drh9188b382004-05-14 21:12:22 +00003089 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
drh9188b382004-05-14 21:12:22 +00003090 }
danielk19771cc5ed82007-05-16 17:28:43 +00003091#else
3092 #define assertCellInfo(x)
3093#endif
drh86057612007-06-26 01:04:48 +00003094#ifdef _MSC_VER
3095 /* Use a real function in MSVC to work around bugs in that compiler. */
3096 static void getCellInfo(BtCursor *pCur){
3097 if( pCur->info.nSize==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003098 int iPage = pCur->iPage;
3099 sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
drha2c20e42008-03-29 16:01:04 +00003100 pCur->validNKey = 1;
drh86057612007-06-26 01:04:48 +00003101 }else{
3102 assertCellInfo(pCur);
3103 }
3104 }
3105#else /* if not _MSC_VER */
3106 /* Use a macro in all other compilers so that the function is inlined */
danielk197771d5d2c2008-09-29 11:49:47 +00003107#define getCellInfo(pCur) \
3108 if( pCur->info.nSize==0 ){ \
3109 int iPage = pCur->iPage; \
3110 sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
3111 pCur->validNKey = 1; \
3112 }else{ \
3113 assertCellInfo(pCur); \
drh86057612007-06-26 01:04:48 +00003114 }
3115#endif /* _MSC_VER */
drh9188b382004-05-14 21:12:22 +00003116
3117/*
drh3aac2dd2004-04-26 14:10:20 +00003118** Set *pSize to the size of the buffer needed to hold the value of
3119** the key for the current entry. If the cursor is not pointing
3120** to a valid entry, *pSize is set to 0.
3121**
drh4b70f112004-05-02 21:12:19 +00003122** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00003123** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00003124*/
drh4a1c3802004-05-12 15:15:47 +00003125int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
drhd677b3d2007-08-20 22:48:41 +00003126 int rc;
3127
drh1fee73e2007-08-29 04:00:57 +00003128 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003129 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003130 if( rc==SQLITE_OK ){
3131 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
3132 if( pCur->eState==CURSOR_INVALID ){
3133 *pSize = 0;
3134 }else{
drh86057612007-06-26 01:04:48 +00003135 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00003136 *pSize = pCur->info.nKey;
3137 }
drh72f82862001-05-24 21:06:34 +00003138 }
danielk1977da184232006-01-05 11:34:32 +00003139 return rc;
drha059ad02001-04-17 20:09:11 +00003140}
drh2af926b2001-05-15 00:39:25 +00003141
drh72f82862001-05-24 21:06:34 +00003142/*
drh0e1c19e2004-05-11 00:58:56 +00003143** Set *pSize to the number of bytes of data in the entry the
3144** cursor currently points to. Always return SQLITE_OK.
3145** Failure is not possible. If the cursor is not currently
3146** pointing to an entry (which can happen, for example, if
3147** the database is empty) then *pSize is set to 0.
3148*/
3149int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
drhd677b3d2007-08-20 22:48:41 +00003150 int rc;
3151
drh1fee73e2007-08-29 04:00:57 +00003152 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003153 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003154 if( rc==SQLITE_OK ){
3155 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
3156 if( pCur->eState==CURSOR_INVALID ){
3157 /* Not pointing at a valid entry - set *pSize to 0. */
3158 *pSize = 0;
3159 }else{
drh86057612007-06-26 01:04:48 +00003160 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00003161 *pSize = pCur->info.nData;
3162 }
drh0e1c19e2004-05-11 00:58:56 +00003163 }
danielk1977da184232006-01-05 11:34:32 +00003164 return rc;
drh0e1c19e2004-05-11 00:58:56 +00003165}
3166
3167/*
danielk1977d04417962007-05-02 13:16:30 +00003168** Given the page number of an overflow page in the database (parameter
3169** ovfl), this function finds the page number of the next page in the
3170** linked list of overflow pages. If possible, it uses the auto-vacuum
3171** pointer-map data instead of reading the content of page ovfl to do so.
3172**
3173** If an error occurs an SQLite error code is returned. Otherwise:
3174**
danielk1977bea2a942009-01-20 17:06:27 +00003175** The page number of the next overflow page in the linked list is
3176** written to *pPgnoNext. If page ovfl is the last page in its linked
3177** list, *pPgnoNext is set to zero.
danielk1977d04417962007-05-02 13:16:30 +00003178**
danielk1977bea2a942009-01-20 17:06:27 +00003179** If ppPage is not NULL, and a reference to the MemPage object corresponding
3180** to page number pOvfl was obtained, then *ppPage is set to point to that
3181** reference. It is the responsibility of the caller to call releasePage()
3182** on *ppPage to free the reference. In no reference was obtained (because
3183** the pointer-map was used to obtain the value for *pPgnoNext), then
3184** *ppPage is set to zero.
danielk1977d04417962007-05-02 13:16:30 +00003185*/
3186static int getOverflowPage(
3187 BtShared *pBt,
3188 Pgno ovfl, /* Overflow page */
danielk1977bea2a942009-01-20 17:06:27 +00003189 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
danielk1977d04417962007-05-02 13:16:30 +00003190 Pgno *pPgnoNext /* OUT: Next overflow page number */
3191){
3192 Pgno next = 0;
danielk1977bea2a942009-01-20 17:06:27 +00003193 MemPage *pPage = 0;
drh1bd10f82008-12-10 21:19:56 +00003194 int rc = SQLITE_OK;
danielk1977d04417962007-05-02 13:16:30 +00003195
drh1fee73e2007-08-29 04:00:57 +00003196 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977bea2a942009-01-20 17:06:27 +00003197 assert(pPgnoNext);
danielk1977d04417962007-05-02 13:16:30 +00003198
3199#ifndef SQLITE_OMIT_AUTOVACUUM
3200 /* Try to find the next page in the overflow list using the
3201 ** autovacuum pointer-map pages. Guess that the next page in
3202 ** the overflow list is page number (ovfl+1). If that guess turns
3203 ** out to be wrong, fall back to loading the data of page
3204 ** number ovfl to determine the next page number.
3205 */
3206 if( pBt->autoVacuum ){
3207 Pgno pgno;
3208 Pgno iGuess = ovfl+1;
3209 u8 eType;
3210
3211 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
3212 iGuess++;
3213 }
3214
danielk197789d40042008-11-17 14:20:56 +00003215 if( iGuess<=pagerPagecount(pBt) ){
danielk1977d04417962007-05-02 13:16:30 +00003216 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
danielk1977bea2a942009-01-20 17:06:27 +00003217 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
danielk1977d04417962007-05-02 13:16:30 +00003218 next = iGuess;
danielk1977bea2a942009-01-20 17:06:27 +00003219 rc = SQLITE_DONE;
danielk1977d04417962007-05-02 13:16:30 +00003220 }
3221 }
3222 }
3223#endif
3224
danielk1977bea2a942009-01-20 17:06:27 +00003225 if( rc==SQLITE_OK ){
3226 rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, 0);
danielk1977d04417962007-05-02 13:16:30 +00003227 assert(rc==SQLITE_OK || pPage==0);
3228 if( next==0 && rc==SQLITE_OK ){
3229 next = get4byte(pPage->aData);
3230 }
danielk1977443c0592009-01-16 15:21:05 +00003231 }
danielk197745d68822009-01-16 16:23:38 +00003232
danielk1977bea2a942009-01-20 17:06:27 +00003233 *pPgnoNext = next;
3234 if( ppPage ){
3235 *ppPage = pPage;
3236 }else{
3237 releasePage(pPage);
3238 }
3239 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
danielk1977d04417962007-05-02 13:16:30 +00003240}
3241
danielk1977da107192007-05-04 08:32:13 +00003242/*
3243** Copy data from a buffer to a page, or from a page to a buffer.
3244**
3245** pPayload is a pointer to data stored on database page pDbPage.
3246** If argument eOp is false, then nByte bytes of data are copied
3247** from pPayload to the buffer pointed at by pBuf. If eOp is true,
3248** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
3249** of data are copied from the buffer pBuf to pPayload.
3250**
3251** SQLITE_OK is returned on success, otherwise an error code.
3252*/
3253static int copyPayload(
3254 void *pPayload, /* Pointer to page data */
3255 void *pBuf, /* Pointer to buffer */
3256 int nByte, /* Number of bytes to copy */
3257 int eOp, /* 0 -> copy from page, 1 -> copy to page */
3258 DbPage *pDbPage /* Page containing pPayload */
3259){
3260 if( eOp ){
3261 /* Copy data from buffer to page (a write operation) */
3262 int rc = sqlite3PagerWrite(pDbPage);
3263 if( rc!=SQLITE_OK ){
3264 return rc;
3265 }
3266 memcpy(pPayload, pBuf, nByte);
3267 }else{
3268 /* Copy data from page to buffer (a read operation) */
3269 memcpy(pBuf, pPayload, nByte);
3270 }
3271 return SQLITE_OK;
3272}
danielk1977d04417962007-05-02 13:16:30 +00003273
3274/*
danielk19779f8d6402007-05-02 17:48:45 +00003275** This function is used to read or overwrite payload information
3276** for the entry that the pCur cursor is pointing to. If the eOp
3277** parameter is 0, this is a read operation (data copied into
3278** buffer pBuf). If it is non-zero, a write (data copied from
3279** buffer pBuf).
3280**
3281** A total of "amt" bytes are read or written beginning at "offset".
3282** Data is read to or from the buffer pBuf.
drh72f82862001-05-24 21:06:34 +00003283**
3284** This routine does not make a distinction between key and data.
danielk19779f8d6402007-05-02 17:48:45 +00003285** It just reads or writes bytes from the payload area. Data might
3286** appear on the main page or be scattered out on multiple overflow
3287** pages.
danielk1977da107192007-05-04 08:32:13 +00003288**
danielk1977dcbb5d32007-05-04 18:36:44 +00003289** If the BtCursor.isIncrblobHandle flag is set, and the current
danielk1977da107192007-05-04 08:32:13 +00003290** cursor entry uses one or more overflow pages, this function
3291** allocates space for and lazily popluates the overflow page-list
3292** cache array (BtCursor.aOverflow). Subsequent calls use this
3293** cache to make seeking to the supplied offset more efficient.
3294**
3295** Once an overflow page-list cache has been allocated, it may be
3296** invalidated if some other cursor writes to the same table, or if
3297** the cursor is moved to a different row. Additionally, in auto-vacuum
3298** mode, the following events may invalidate an overflow page-list cache.
3299**
3300** * An incremental vacuum,
3301** * A commit in auto_vacuum="full" mode,
3302** * Creating a table (may require moving an overflow page).
drh72f82862001-05-24 21:06:34 +00003303*/
danielk19779f8d6402007-05-02 17:48:45 +00003304static int accessPayload(
drh3aac2dd2004-04-26 14:10:20 +00003305 BtCursor *pCur, /* Cursor pointing to entry to read from */
danielk197789d40042008-11-17 14:20:56 +00003306 u32 offset, /* Begin reading this far into payload */
3307 u32 amt, /* Read this many bytes */
drh3aac2dd2004-04-26 14:10:20 +00003308 unsigned char *pBuf, /* Write the bytes into this buffer */
danielk19779f8d6402007-05-02 17:48:45 +00003309 int skipKey, /* offset begins at data if this is true */
3310 int eOp /* zero to read. non-zero to write. */
drh3aac2dd2004-04-26 14:10:20 +00003311){
3312 unsigned char *aPayload;
danielk1977da107192007-05-04 08:32:13 +00003313 int rc = SQLITE_OK;
drhfa1a98a2004-05-14 19:08:17 +00003314 u32 nKey;
danielk19772dec9702007-05-02 16:48:37 +00003315 int iIdx = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003316 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
danielk19770d065412008-11-12 18:21:36 +00003317 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
drh3aac2dd2004-04-26 14:10:20 +00003318
danielk1977da107192007-05-04 08:32:13 +00003319 assert( pPage );
danielk1977da184232006-01-05 11:34:32 +00003320 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003321 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
drh1fee73e2007-08-29 04:00:57 +00003322 assert( cursorHoldsMutex(pCur) );
danielk1977da107192007-05-04 08:32:13 +00003323
drh86057612007-06-26 01:04:48 +00003324 getCellInfo(pCur);
drh366fda62006-01-13 02:35:09 +00003325 aPayload = pCur->info.pCell + pCur->info.nHeader;
drhf49661a2008-12-10 16:45:50 +00003326 nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
danielk1977da107192007-05-04 08:32:13 +00003327
drh3aac2dd2004-04-26 14:10:20 +00003328 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003329 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00003330 }
danielk19770d065412008-11-12 18:21:36 +00003331 if( offset+amt > nKey+pCur->info.nData
3332 || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
3333 ){
danielk1977da107192007-05-04 08:32:13 +00003334 /* Trying to read or write past the end of the data is an error */
danielk197767fd7a92008-09-10 17:53:35 +00003335 return SQLITE_CORRUPT_BKPT;
drh3aac2dd2004-04-26 14:10:20 +00003336 }
danielk1977da107192007-05-04 08:32:13 +00003337
3338 /* Check if data must be read/written to/from the btree page itself. */
drhfa1a98a2004-05-14 19:08:17 +00003339 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00003340 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00003341 if( a+offset>pCur->info.nLocal ){
3342 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00003343 }
danielk1977da107192007-05-04 08:32:13 +00003344 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
drh2aa679f2001-06-25 02:11:07 +00003345 offset = 0;
drha34b6762004-05-07 13:30:42 +00003346 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00003347 amt -= a;
drhdd793422001-06-28 01:54:48 +00003348 }else{
drhfa1a98a2004-05-14 19:08:17 +00003349 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00003350 }
danielk1977da107192007-05-04 08:32:13 +00003351
3352 if( rc==SQLITE_OK && amt>0 ){
danielk197789d40042008-11-17 14:20:56 +00003353 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
danielk1977da107192007-05-04 08:32:13 +00003354 Pgno nextPage;
3355
drhfa1a98a2004-05-14 19:08:17 +00003356 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977da107192007-05-04 08:32:13 +00003357
danielk19772dec9702007-05-02 16:48:37 +00003358#ifndef SQLITE_OMIT_INCRBLOB
danielk1977dcbb5d32007-05-04 18:36:44 +00003359 /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
danielk1977da107192007-05-04 08:32:13 +00003360 ** has not been allocated, allocate it now. The array is sized at
3361 ** one entry for each overflow page in the overflow chain. The
3362 ** page number of the first overflow page is stored in aOverflow[0],
3363 ** etc. A value of 0 in the aOverflow[] array means "not yet known"
3364 ** (the cache is lazily populated).
3365 */
danielk1977dcbb5d32007-05-04 18:36:44 +00003366 if( pCur->isIncrblobHandle && !pCur->aOverflow ){
danielk19772dec9702007-05-02 16:48:37 +00003367 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
drh17435752007-08-16 04:30:38 +00003368 pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
danielk19772dec9702007-05-02 16:48:37 +00003369 if( nOvfl && !pCur->aOverflow ){
danielk1977da107192007-05-04 08:32:13 +00003370 rc = SQLITE_NOMEM;
danielk19772dec9702007-05-02 16:48:37 +00003371 }
3372 }
danielk1977da107192007-05-04 08:32:13 +00003373
3374 /* If the overflow page-list cache has been allocated and the
3375 ** entry for the first required overflow page is valid, skip
3376 ** directly to it.
3377 */
danielk19772dec9702007-05-02 16:48:37 +00003378 if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
3379 iIdx = (offset/ovflSize);
3380 nextPage = pCur->aOverflow[iIdx];
3381 offset = (offset%ovflSize);
3382 }
3383#endif
danielk1977da107192007-05-04 08:32:13 +00003384
3385 for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
3386
3387#ifndef SQLITE_OMIT_INCRBLOB
3388 /* If required, populate the overflow page-list cache. */
3389 if( pCur->aOverflow ){
3390 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
3391 pCur->aOverflow[iIdx] = nextPage;
3392 }
3393#endif
3394
danielk1977d04417962007-05-02 13:16:30 +00003395 if( offset>=ovflSize ){
3396 /* The only reason to read this page is to obtain the page
danielk1977da107192007-05-04 08:32:13 +00003397 ** number for the next page in the overflow chain. The page
drhfd131da2007-08-07 17:13:03 +00003398 ** data is not required. So first try to lookup the overflow
3399 ** page-list cache, if any, then fall back to the getOverflowPage()
danielk1977da107192007-05-04 08:32:13 +00003400 ** function.
danielk1977d04417962007-05-02 13:16:30 +00003401 */
danielk19772dec9702007-05-02 16:48:37 +00003402#ifndef SQLITE_OMIT_INCRBLOB
danielk1977da107192007-05-04 08:32:13 +00003403 if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
3404 nextPage = pCur->aOverflow[iIdx+1];
3405 } else
danielk19772dec9702007-05-02 16:48:37 +00003406#endif
danielk1977da107192007-05-04 08:32:13 +00003407 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
danielk1977da107192007-05-04 08:32:13 +00003408 offset -= ovflSize;
danielk1977d04417962007-05-02 13:16:30 +00003409 }else{
danielk19779f8d6402007-05-02 17:48:45 +00003410 /* Need to read this page properly. It contains some of the
3411 ** range of data that is being read (eOp==0) or written (eOp!=0).
danielk1977d04417962007-05-02 13:16:30 +00003412 */
3413 DbPage *pDbPage;
danielk1977cfe9a692004-06-16 12:00:29 +00003414 int a = amt;
danielk1977d04417962007-05-02 13:16:30 +00003415 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
danielk1977da107192007-05-04 08:32:13 +00003416 if( rc==SQLITE_OK ){
3417 aPayload = sqlite3PagerGetData(pDbPage);
3418 nextPage = get4byte(aPayload);
3419 if( a + offset > ovflSize ){
3420 a = ovflSize - offset;
danielk19779f8d6402007-05-02 17:48:45 +00003421 }
danielk1977da107192007-05-04 08:32:13 +00003422 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
3423 sqlite3PagerUnref(pDbPage);
3424 offset = 0;
3425 amt -= a;
3426 pBuf += a;
danielk19779f8d6402007-05-02 17:48:45 +00003427 }
danielk1977cfe9a692004-06-16 12:00:29 +00003428 }
drh2af926b2001-05-15 00:39:25 +00003429 }
drh2af926b2001-05-15 00:39:25 +00003430 }
danielk1977cfe9a692004-06-16 12:00:29 +00003431
danielk1977da107192007-05-04 08:32:13 +00003432 if( rc==SQLITE_OK && amt>0 ){
drh49285702005-09-17 15:20:26 +00003433 return SQLITE_CORRUPT_BKPT;
drha7fcb052001-12-14 15:09:55 +00003434 }
danielk1977da107192007-05-04 08:32:13 +00003435 return rc;
drh2af926b2001-05-15 00:39:25 +00003436}
3437
drh72f82862001-05-24 21:06:34 +00003438/*
drh3aac2dd2004-04-26 14:10:20 +00003439** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003440** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003441** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00003442**
drh3aac2dd2004-04-26 14:10:20 +00003443** Return SQLITE_OK on success or an error code if anything goes
3444** wrong. An error is returned if "offset+amt" is larger than
3445** the available payload.
drh72f82862001-05-24 21:06:34 +00003446*/
drha34b6762004-05-07 13:30:42 +00003447int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003448 int rc;
3449
drh1fee73e2007-08-29 04:00:57 +00003450 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003451 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003452 if( rc==SQLITE_OK ){
3453 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003454 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
3455 if( pCur->apPage[0]->intKey ){
danielk1977da184232006-01-05 11:34:32 +00003456 return SQLITE_CORRUPT_BKPT;
3457 }
danielk197771d5d2c2008-09-29 11:49:47 +00003458 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh16a9b832007-05-05 18:39:25 +00003459 rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
drh6575a222005-03-10 17:06:34 +00003460 }
danielk1977da184232006-01-05 11:34:32 +00003461 return rc;
drh3aac2dd2004-04-26 14:10:20 +00003462}
3463
3464/*
drh3aac2dd2004-04-26 14:10:20 +00003465** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003466** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003467** begins at "offset".
3468**
3469** Return SQLITE_OK on success or an error code if anything goes
3470** wrong. An error is returned if "offset+amt" is larger than
3471** the available payload.
drh72f82862001-05-24 21:06:34 +00003472*/
drh3aac2dd2004-04-26 14:10:20 +00003473int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003474 int rc;
3475
danielk19773588ceb2008-06-10 17:30:26 +00003476#ifndef SQLITE_OMIT_INCRBLOB
3477 if ( pCur->eState==CURSOR_INVALID ){
3478 return SQLITE_ABORT;
3479 }
3480#endif
3481
drh1fee73e2007-08-29 04:00:57 +00003482 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003483 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003484 if( rc==SQLITE_OK ){
3485 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003486 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
3487 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh16a9b832007-05-05 18:39:25 +00003488 rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
danielk1977da184232006-01-05 11:34:32 +00003489 }
3490 return rc;
drh2af926b2001-05-15 00:39:25 +00003491}
3492
drh72f82862001-05-24 21:06:34 +00003493/*
drh0e1c19e2004-05-11 00:58:56 +00003494** Return a pointer to payload information from the entry that the
3495** pCur cursor is pointing to. The pointer is to the beginning of
3496** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00003497** skipKey==1. The number of bytes of available key/data is written
3498** into *pAmt. If *pAmt==0, then the value returned will not be
3499** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00003500**
3501** This routine is an optimization. It is common for the entire key
3502** and data to fit on the local page and for there to be no overflow
3503** pages. When that is so, this routine can be used to access the
3504** key and data without making a copy. If the key and/or data spills
drh16a9b832007-05-05 18:39:25 +00003505** onto overflow pages, then accessPayload() must be used to reassembly
drh0e1c19e2004-05-11 00:58:56 +00003506** the key/data and copy it into a preallocated buffer.
3507**
3508** The pointer returned by this routine looks directly into the cached
3509** page of the database. The data might change or move the next time
3510** any btree routine is called.
3511*/
3512static const unsigned char *fetchPayload(
3513 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00003514 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00003515 int skipKey /* read beginning at data if this is true */
3516){
3517 unsigned char *aPayload;
3518 MemPage *pPage;
drhfa1a98a2004-05-14 19:08:17 +00003519 u32 nKey;
danielk197789d40042008-11-17 14:20:56 +00003520 u32 nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003521
danielk197771d5d2c2008-09-29 11:49:47 +00003522 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
danielk1977da184232006-01-05 11:34:32 +00003523 assert( pCur->eState==CURSOR_VALID );
drh1fee73e2007-08-29 04:00:57 +00003524 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00003525 pPage = pCur->apPage[pCur->iPage];
3526 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
drh86057612007-06-26 01:04:48 +00003527 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00003528 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00003529 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00003530 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003531 nKey = 0;
3532 }else{
drhf49661a2008-12-10 16:45:50 +00003533 nKey = (int)pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00003534 }
drh0e1c19e2004-05-11 00:58:56 +00003535 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003536 aPayload += nKey;
3537 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00003538 }else{
drhfa1a98a2004-05-14 19:08:17 +00003539 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00003540 if( nLocal>nKey ){
3541 nLocal = nKey;
3542 }
drh0e1c19e2004-05-11 00:58:56 +00003543 }
drhe51c44f2004-05-30 20:46:09 +00003544 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003545 return aPayload;
3546}
3547
3548
3549/*
drhe51c44f2004-05-30 20:46:09 +00003550** For the entry that cursor pCur is point to, return as
3551** many bytes of the key or data as are available on the local
3552** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00003553**
3554** The pointer returned is ephemeral. The key/data may move
drhd677b3d2007-08-20 22:48:41 +00003555** or be destroyed on the next call to any Btree routine,
3556** including calls from other threads against the same cache.
3557** Hence, a mutex on the BtShared should be held prior to calling
3558** this routine.
drh0e1c19e2004-05-11 00:58:56 +00003559**
3560** These routines is used to get quick access to key and data
3561** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00003562*/
drhe51c44f2004-05-30 20:46:09 +00003563const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003564 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003565 if( pCur->eState==CURSOR_VALID ){
3566 return (const void*)fetchPayload(pCur, pAmt, 0);
3567 }
3568 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003569}
drhe51c44f2004-05-30 20:46:09 +00003570const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003571 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003572 if( pCur->eState==CURSOR_VALID ){
3573 return (const void*)fetchPayload(pCur, pAmt, 1);
3574 }
3575 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003576}
3577
3578
3579/*
drh8178a752003-01-05 21:41:40 +00003580** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00003581** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00003582*/
drh3aac2dd2004-04-26 14:10:20 +00003583static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00003584 int rc;
danielk197771d5d2c2008-09-29 11:49:47 +00003585 int i = pCur->iPage;
drh72f82862001-05-24 21:06:34 +00003586 MemPage *pNewPage;
drhd0679ed2007-08-28 22:24:34 +00003587 BtShared *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00003588
drh1fee73e2007-08-29 04:00:57 +00003589 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003590 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003591 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
3592 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
3593 return SQLITE_CORRUPT_BKPT;
3594 }
3595 rc = getAndInitPage(pBt, newPgno, &pNewPage);
drh6019e162001-07-02 17:51:45 +00003596 if( rc ) return rc;
danielk197771d5d2c2008-09-29 11:49:47 +00003597 pCur->apPage[i+1] = pNewPage;
3598 pCur->aiIdx[i+1] = 0;
3599 pCur->iPage++;
3600
drh271efa52004-05-30 19:19:05 +00003601 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003602 pCur->validNKey = 0;
drh4be295b2003-12-16 03:44:47 +00003603 if( pNewPage->nCell<1 ){
drh49285702005-09-17 15:20:26 +00003604 return SQLITE_CORRUPT_BKPT;
drh4be295b2003-12-16 03:44:47 +00003605 }
drh72f82862001-05-24 21:06:34 +00003606 return SQLITE_OK;
3607}
3608
danielk1977bf93c562008-09-29 15:53:25 +00003609#ifndef NDEBUG
3610/*
3611** Page pParent is an internal (non-leaf) tree page. This function
3612** asserts that page number iChild is the left-child if the iIdx'th
3613** cell in page pParent. Or, if iIdx is equal to the total number of
3614** cells in pParent, that page number iChild is the right-child of
3615** the page.
3616*/
3617static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
3618 assert( iIdx<=pParent->nCell );
3619 if( iIdx==pParent->nCell ){
3620 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
3621 }else{
3622 assert( get4byte(findCell(pParent, iIdx))==iChild );
3623 }
3624}
3625#else
3626# define assertParentIndex(x,y,z)
3627#endif
3628
drh72f82862001-05-24 21:06:34 +00003629/*
drh5e2f8b92001-05-28 00:41:15 +00003630** Move the cursor up to the parent page.
3631**
3632** pCur->idx is set to the cell index that contains the pointer
3633** to the page we are coming from. If we are coming from the
3634** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00003635** the largest cell index.
drh72f82862001-05-24 21:06:34 +00003636*/
drh16a9b832007-05-05 18:39:25 +00003637void sqlite3BtreeMoveToParent(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00003638 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003639 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003640 assert( pCur->iPage>0 );
3641 assert( pCur->apPage[pCur->iPage] );
danielk1977bf93c562008-09-29 15:53:25 +00003642 assertParentIndex(
3643 pCur->apPage[pCur->iPage-1],
3644 pCur->aiIdx[pCur->iPage-1],
3645 pCur->apPage[pCur->iPage]->pgno
3646 );
danielk197771d5d2c2008-09-29 11:49:47 +00003647 releasePage(pCur->apPage[pCur->iPage]);
3648 pCur->iPage--;
drh271efa52004-05-30 19:19:05 +00003649 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003650 pCur->validNKey = 0;
drh72f82862001-05-24 21:06:34 +00003651}
3652
3653/*
3654** Move the cursor to the root page
3655*/
drh5e2f8b92001-05-28 00:41:15 +00003656static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00003657 MemPage *pRoot;
drh777e4c42006-01-13 04:31:58 +00003658 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00003659 Btree *p = pCur->pBtree;
3660 BtShared *pBt = p->pBt;
drhbd03cae2001-06-02 02:40:57 +00003661
drh1fee73e2007-08-29 04:00:57 +00003662 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +00003663 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
3664 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
3665 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
3666 if( pCur->eState>=CURSOR_REQUIRESEEK ){
3667 if( pCur->eState==CURSOR_FAULT ){
3668 return pCur->skip;
3669 }
danielk1977be51a652008-10-08 17:58:48 +00003670 sqlite3BtreeClearCursor(pCur);
drhbf700f32007-03-31 02:36:44 +00003671 }
danielk197771d5d2c2008-09-29 11:49:47 +00003672
3673 if( pCur->iPage>=0 ){
3674 int i;
3675 for(i=1; i<=pCur->iPage; i++){
3676 releasePage(pCur->apPage[i]);
danielk1977d9f6c532008-09-19 16:39:38 +00003677 }
drh777e4c42006-01-13 04:31:58 +00003678 }else{
3679 if(
danielk197771d5d2c2008-09-29 11:49:47 +00003680 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]))
drh777e4c42006-01-13 04:31:58 +00003681 ){
3682 pCur->eState = CURSOR_INVALID;
3683 return rc;
3684 }
drhc39e0002004-05-07 23:50:57 +00003685 }
danielk197771d5d2c2008-09-29 11:49:47 +00003686
3687 pRoot = pCur->apPage[0];
3688 assert( pRoot->pgno==pCur->pgnoRoot );
3689 pCur->iPage = 0;
3690 pCur->aiIdx[0] = 0;
drh271efa52004-05-30 19:19:05 +00003691 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003692 pCur->atLast = 0;
3693 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003694
drh8856d6a2004-04-29 14:42:46 +00003695 if( pRoot->nCell==0 && !pRoot->leaf ){
3696 Pgno subpage;
3697 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00003698 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00003699 assert( subpage>0 );
danielk1977da184232006-01-05 11:34:32 +00003700 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:19 +00003701 rc = moveToChild(pCur, subpage);
danielk197771d5d2c2008-09-29 11:49:47 +00003702 }else{
3703 pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
drh8856d6a2004-04-29 14:42:46 +00003704 }
3705 return rc;
drh72f82862001-05-24 21:06:34 +00003706}
drh2af926b2001-05-15 00:39:25 +00003707
drh5e2f8b92001-05-28 00:41:15 +00003708/*
3709** Move the cursor down to the left-most leaf entry beneath the
3710** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:58 +00003711**
3712** The left-most leaf is the one with the smallest key - the first
3713** in ascending order.
drh5e2f8b92001-05-28 00:41:15 +00003714*/
3715static int moveToLeftmost(BtCursor *pCur){
3716 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003717 int rc = SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00003718 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00003719
drh1fee73e2007-08-29 04:00:57 +00003720 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003721 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003722 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
3723 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
3724 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
drh8178a752003-01-05 21:41:40 +00003725 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00003726 }
drhd677b3d2007-08-20 22:48:41 +00003727 return rc;
drh5e2f8b92001-05-28 00:41:15 +00003728}
3729
drh2dcc9aa2002-12-04 13:40:25 +00003730/*
3731** Move the cursor down to the right-most leaf entry beneath the
3732** page to which it is currently pointing. Notice the difference
3733** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3734** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3735** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:58 +00003736**
3737** The right-most entry is the one with the largest key - the last
3738** key in ascending order.
drh2dcc9aa2002-12-04 13:40:25 +00003739*/
3740static int moveToRightmost(BtCursor *pCur){
3741 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003742 int rc = SQLITE_OK;
drh1bd10f82008-12-10 21:19:56 +00003743 MemPage *pPage = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003744
drh1fee73e2007-08-29 04:00:57 +00003745 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003746 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003747 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
drh43605152004-05-29 21:46:49 +00003748 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
danielk197771d5d2c2008-09-29 11:49:47 +00003749 pCur->aiIdx[pCur->iPage] = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00003750 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003751 }
drhd677b3d2007-08-20 22:48:41 +00003752 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00003753 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
drhd677b3d2007-08-20 22:48:41 +00003754 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003755 pCur->validNKey = 0;
drhd677b3d2007-08-20 22:48:41 +00003756 }
danielk1977518002e2008-09-05 05:02:46 +00003757 return rc;
drh2dcc9aa2002-12-04 13:40:25 +00003758}
3759
drh5e00f6c2001-09-13 13:46:56 +00003760/* Move the cursor to the first entry in the table. Return SQLITE_OK
3761** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003762** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00003763*/
drh3aac2dd2004-04-26 14:10:20 +00003764int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00003765 int rc;
drhd677b3d2007-08-20 22:48:41 +00003766
drh1fee73e2007-08-29 04:00:57 +00003767 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003768 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh5e00f6c2001-09-13 13:46:56 +00003769 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003770 if( rc==SQLITE_OK ){
3771 if( pCur->eState==CURSOR_INVALID ){
danielk197771d5d2c2008-09-29 11:49:47 +00003772 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhd677b3d2007-08-20 22:48:41 +00003773 *pRes = 1;
3774 rc = SQLITE_OK;
3775 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00003776 assert( pCur->apPage[pCur->iPage]->nCell>0 );
drhd677b3d2007-08-20 22:48:41 +00003777 *pRes = 0;
3778 rc = moveToLeftmost(pCur);
3779 }
drh5e00f6c2001-09-13 13:46:56 +00003780 }
drh5e00f6c2001-09-13 13:46:56 +00003781 return rc;
3782}
drh5e2f8b92001-05-28 00:41:15 +00003783
drh9562b552002-02-19 15:00:07 +00003784/* Move the cursor to the last entry in the table. Return SQLITE_OK
3785** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003786** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00003787*/
drh3aac2dd2004-04-26 14:10:20 +00003788int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00003789 int rc;
drhd677b3d2007-08-20 22:48:41 +00003790
drh1fee73e2007-08-29 04:00:57 +00003791 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003792 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh9562b552002-02-19 15:00:07 +00003793 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003794 if( rc==SQLITE_OK ){
3795 if( CURSOR_INVALID==pCur->eState ){
danielk197771d5d2c2008-09-29 11:49:47 +00003796 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhd677b3d2007-08-20 22:48:41 +00003797 *pRes = 1;
3798 }else{
3799 assert( pCur->eState==CURSOR_VALID );
3800 *pRes = 0;
3801 rc = moveToRightmost(pCur);
drha2c20e42008-03-29 16:01:04 +00003802 getCellInfo(pCur);
drhf49661a2008-12-10 16:45:50 +00003803 pCur->atLast = rc==SQLITE_OK ?1:0;
drhd677b3d2007-08-20 22:48:41 +00003804 }
drh9562b552002-02-19 15:00:07 +00003805 }
drh9562b552002-02-19 15:00:07 +00003806 return rc;
3807}
3808
drhe14006d2008-03-25 17:23:32 +00003809/* Move the cursor so that it points to an entry near the key
drhe63d9992008-08-13 19:11:48 +00003810** specified by pIdxKey or intKey. Return a success code.
drh72f82862001-05-24 21:06:34 +00003811**
drhe63d9992008-08-13 19:11:48 +00003812** For INTKEY tables, the intKey parameter is used. pIdxKey
3813** must be NULL. For index tables, pIdxKey is used and intKey
3814** is ignored.
drh3aac2dd2004-04-26 14:10:20 +00003815**
drh5e2f8b92001-05-28 00:41:15 +00003816** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00003817** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00003818** were present. The cursor might point to an entry that comes
3819** before or after the key.
3820**
drh64022502009-01-09 14:11:04 +00003821** An integer is written into *pRes which is the result of
3822** comparing the key with the entry to which the cursor is
3823** pointing. The meaning of the integer written into
3824** *pRes is as follows:
drhbd03cae2001-06-02 02:40:57 +00003825**
3826** *pRes<0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003827** is smaller than intKey/pIdxKey or if the table is empty
drh1a844c32002-12-04 22:29:28 +00003828** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00003829**
3830** *pRes==0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003831** exactly matches intKey/pIdxKey.
drhbd03cae2001-06-02 02:40:57 +00003832**
3833** *pRes>0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003834** is larger than intKey/pIdxKey.
drhd677b3d2007-08-20 22:48:41 +00003835**
drha059ad02001-04-17 20:09:11 +00003836*/
drhe63d9992008-08-13 19:11:48 +00003837int sqlite3BtreeMovetoUnpacked(
3838 BtCursor *pCur, /* The cursor to be moved */
3839 UnpackedRecord *pIdxKey, /* Unpacked index key */
3840 i64 intKey, /* The table key */
3841 int biasRight, /* If true, bias the search to the high end */
3842 int *pRes /* Write search results here */
drhe4d90812007-03-29 05:51:49 +00003843){
drh72f82862001-05-24 21:06:34 +00003844 int rc;
drhd677b3d2007-08-20 22:48:41 +00003845
drh1fee73e2007-08-29 04:00:57 +00003846 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003847 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drha2c20e42008-03-29 16:01:04 +00003848
3849 /* If the cursor is already positioned at the point we are trying
3850 ** to move to, then just return without doing any work */
danielk197771d5d2c2008-09-29 11:49:47 +00003851 if( pCur->eState==CURSOR_VALID && pCur->validNKey
3852 && pCur->apPage[0]->intKey
3853 ){
drhe63d9992008-08-13 19:11:48 +00003854 if( pCur->info.nKey==intKey ){
drha2c20e42008-03-29 16:01:04 +00003855 *pRes = 0;
3856 return SQLITE_OK;
3857 }
drhe63d9992008-08-13 19:11:48 +00003858 if( pCur->atLast && pCur->info.nKey<intKey ){
drha2c20e42008-03-29 16:01:04 +00003859 *pRes = -1;
3860 return SQLITE_OK;
3861 }
3862 }
3863
drh5e2f8b92001-05-28 00:41:15 +00003864 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003865 if( rc ){
3866 return rc;
3867 }
danielk197771d5d2c2008-09-29 11:49:47 +00003868 assert( pCur->apPage[pCur->iPage] );
3869 assert( pCur->apPage[pCur->iPage]->isInit );
danielk1977da184232006-01-05 11:34:32 +00003870 if( pCur->eState==CURSOR_INVALID ){
drhf328bc82004-05-10 23:29:49 +00003871 *pRes = -1;
danielk197771d5d2c2008-09-29 11:49:47 +00003872 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhc39e0002004-05-07 23:50:57 +00003873 return SQLITE_OK;
3874 }
danielk197771d5d2c2008-09-29 11:49:47 +00003875 assert( pCur->apPage[0]->intKey || pIdxKey );
drh14684382006-11-30 13:05:29 +00003876 for(;;){
drh72f82862001-05-24 21:06:34 +00003877 int lwr, upr;
3878 Pgno chldPg;
danielk197771d5d2c2008-09-29 11:49:47 +00003879 MemPage *pPage = pCur->apPage[pCur->iPage];
drh1a844c32002-12-04 22:29:28 +00003880 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00003881 lwr = 0;
3882 upr = pPage->nCell-1;
drh64022502009-01-09 14:11:04 +00003883 if( (!pPage->intKey && pIdxKey==0) || upr<0 ){
drh1e968a02008-03-25 00:22:21 +00003884 rc = SQLITE_CORRUPT_BKPT;
3885 goto moveto_finish;
drh4eec4c12005-01-21 00:22:37 +00003886 }
drhe4d90812007-03-29 05:51:49 +00003887 if( biasRight ){
drhf49661a2008-12-10 16:45:50 +00003888 pCur->aiIdx[pCur->iPage] = (u16)upr;
drhe4d90812007-03-29 05:51:49 +00003889 }else{
drhf49661a2008-12-10 16:45:50 +00003890 pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2);
drhe4d90812007-03-29 05:51:49 +00003891 }
drh64022502009-01-09 14:11:04 +00003892 for(;;){
danielk197713adf8a2004-06-03 16:08:41 +00003893 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00003894 i64 nCellKey;
danielk197771d5d2c2008-09-29 11:49:47 +00003895 int idx = pCur->aiIdx[pCur->iPage];
drh366fda62006-01-13 02:35:09 +00003896 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003897 pCur->validNKey = 1;
drh3aac2dd2004-04-26 14:10:20 +00003898 if( pPage->intKey ){
drh777e4c42006-01-13 04:31:58 +00003899 u8 *pCell;
danielk197771d5d2c2008-09-29 11:49:47 +00003900 pCell = findCell(pPage, idx) + pPage->childPtrSize;
drhd172f862006-01-12 15:01:15 +00003901 if( pPage->hasData ){
danielk1977bab45c62006-01-16 15:14:27 +00003902 u32 dummy;
shane3f8d5cf2008-04-24 19:15:09 +00003903 pCell += getVarint32(pCell, dummy);
drhd172f862006-01-12 15:01:15 +00003904 }
drha2c20e42008-03-29 16:01:04 +00003905 getVarint(pCell, (u64*)&nCellKey);
drhe63d9992008-08-13 19:11:48 +00003906 if( nCellKey==intKey ){
drh3aac2dd2004-04-26 14:10:20 +00003907 c = 0;
drhe63d9992008-08-13 19:11:48 +00003908 }else if( nCellKey<intKey ){
drh41eb9e92008-04-02 18:33:07 +00003909 c = -1;
3910 }else{
drhe63d9992008-08-13 19:11:48 +00003911 assert( nCellKey>intKey );
drh41eb9e92008-04-02 18:33:07 +00003912 c = +1;
drh3aac2dd2004-04-26 14:10:20 +00003913 }
drh3aac2dd2004-04-26 14:10:20 +00003914 }else{
drhe51c44f2004-05-30 20:46:09 +00003915 int available;
danielk197713adf8a2004-06-03 16:08:41 +00003916 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drh366fda62006-01-13 02:35:09 +00003917 nCellKey = pCur->info.nKey;
drhe51c44f2004-05-30 20:46:09 +00003918 if( available>=nCellKey ){
drhf49661a2008-12-10 16:45:50 +00003919 c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey);
drhe51c44f2004-05-30 20:46:09 +00003920 }else{
drhf49661a2008-12-10 16:45:50 +00003921 pCellKey = sqlite3Malloc( (int)nCellKey );
danielk19776507ecb2008-03-25 09:56:44 +00003922 if( pCellKey==0 ){
3923 rc = SQLITE_NOMEM;
3924 goto moveto_finish;
3925 }
drhf49661a2008-12-10 16:45:50 +00003926 rc = sqlite3BtreeKey(pCur, 0, (int)nCellKey, (void*)pCellKey);
drh1bd10f82008-12-10 21:19:56 +00003927 c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey);
drhfacf0302008-06-17 15:12:00 +00003928 sqlite3_free(pCellKey);
drh1e968a02008-03-25 00:22:21 +00003929 if( rc ) goto moveto_finish;
drhe51c44f2004-05-30 20:46:09 +00003930 }
drh3aac2dd2004-04-26 14:10:20 +00003931 }
drh72f82862001-05-24 21:06:34 +00003932 if( c==0 ){
drha2c20e42008-03-29 16:01:04 +00003933 pCur->info.nKey = nCellKey;
drh44845222008-07-17 18:39:57 +00003934 if( pPage->intKey && !pPage->leaf ){
danielk197771d5d2c2008-09-29 11:49:47 +00003935 lwr = idx;
drhfc70e6f2004-05-12 21:11:27 +00003936 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00003937 break;
3938 }else{
drh64022502009-01-09 14:11:04 +00003939 *pRes = 0;
drh1e968a02008-03-25 00:22:21 +00003940 rc = SQLITE_OK;
3941 goto moveto_finish;
drh8b18dd42004-05-12 19:18:15 +00003942 }
drh72f82862001-05-24 21:06:34 +00003943 }
3944 if( c<0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003945 lwr = idx+1;
drh72f82862001-05-24 21:06:34 +00003946 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00003947 upr = idx-1;
drh72f82862001-05-24 21:06:34 +00003948 }
drhf1d68b32007-03-29 04:43:26 +00003949 if( lwr>upr ){
drha2c20e42008-03-29 16:01:04 +00003950 pCur->info.nKey = nCellKey;
drhf1d68b32007-03-29 04:43:26 +00003951 break;
3952 }
drhf49661a2008-12-10 16:45:50 +00003953 pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2);
drh72f82862001-05-24 21:06:34 +00003954 }
3955 assert( lwr==upr+1 );
danielk197771d5d2c2008-09-29 11:49:47 +00003956 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00003957 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00003958 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00003959 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00003960 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00003961 }else{
danielk19771cc5ed82007-05-16 17:28:43 +00003962 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00003963 }
3964 if( chldPg==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003965 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh72f82862001-05-24 21:06:34 +00003966 if( pRes ) *pRes = c;
drh1e968a02008-03-25 00:22:21 +00003967 rc = SQLITE_OK;
3968 goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003969 }
drhf49661a2008-12-10 16:45:50 +00003970 pCur->aiIdx[pCur->iPage] = (u16)lwr;
drh271efa52004-05-30 19:19:05 +00003971 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003972 pCur->validNKey = 0;
drh8178a752003-01-05 21:41:40 +00003973 rc = moveToChild(pCur, chldPg);
drh1e968a02008-03-25 00:22:21 +00003974 if( rc ) goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003975 }
drh1e968a02008-03-25 00:22:21 +00003976moveto_finish:
drhe63d9992008-08-13 19:11:48 +00003977 return rc;
3978}
3979
3980/*
3981** In this version of BtreeMoveto, pKey is a packed index record
3982** such as is generated by the OP_MakeRecord opcode. Unpack the
3983** record and then call BtreeMovetoUnpacked() to do the work.
3984*/
3985int sqlite3BtreeMoveto(
3986 BtCursor *pCur, /* Cursor open on the btree to be searched */
3987 const void *pKey, /* Packed key if the btree is an index */
3988 i64 nKey, /* Integer key for tables. Size of pKey for indices */
3989 int bias, /* Bias search to the high end */
3990 int *pRes /* Write search results here */
3991){
3992 int rc; /* Status code */
3993 UnpackedRecord *pIdxKey; /* Unpacked index key */
drh23f79d02008-08-20 22:06:47 +00003994 UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */
drhe63d9992008-08-13 19:11:48 +00003995
drhe14006d2008-03-25 17:23:32 +00003996 if( pKey ){
drhf49661a2008-12-10 16:45:50 +00003997 assert( nKey==(i64)(int)nKey );
3998 pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
drh23f79d02008-08-20 22:06:47 +00003999 aSpace, sizeof(aSpace));
drhe63d9992008-08-13 19:11:48 +00004000 if( pIdxKey==0 ) return SQLITE_NOMEM;
4001 }else{
4002 pIdxKey = 0;
4003 }
4004 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
4005 if( pKey ){
4006 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drhe14006d2008-03-25 17:23:32 +00004007 }
drh1e968a02008-03-25 00:22:21 +00004008 return rc;
drh72f82862001-05-24 21:06:34 +00004009}
4010
drhd677b3d2007-08-20 22:48:41 +00004011
drh72f82862001-05-24 21:06:34 +00004012/*
drhc39e0002004-05-07 23:50:57 +00004013** Return TRUE if the cursor is not pointing at an entry of the table.
4014**
4015** TRUE will be returned after a call to sqlite3BtreeNext() moves
4016** past the last entry in the table or sqlite3BtreePrev() moves past
4017** the first entry. TRUE is also returned if the table is empty.
4018*/
4019int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00004020 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
4021 ** have been deleted? This API will need to change to return an error code
4022 ** as well as the boolean result value.
4023 */
4024 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:57 +00004025}
4026
4027/*
drhb21c8cd2007-08-21 19:33:56 +00004028** Return the database connection handle for a cursor.
4029*/
4030sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
drhe5fe6902007-12-07 18:55:28 +00004031 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
4032 return pCur->pBtree->db;
drhb21c8cd2007-08-21 19:33:56 +00004033}
4034
4035/*
drhbd03cae2001-06-02 02:40:57 +00004036** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00004037** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00004038** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00004039** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00004040*/
drhd094db12008-04-03 21:46:57 +00004041int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00004042 int rc;
danielk197771d5d2c2008-09-29 11:49:47 +00004043 int idx;
danielk197797a227c2006-01-20 16:32:04 +00004044 MemPage *pPage;
drh8b18dd42004-05-12 19:18:15 +00004045
drh1fee73e2007-08-29 04:00:57 +00004046 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00004047 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00004048 if( rc!=SQLITE_OK ){
4049 return rc;
4050 }
drh8c4d3a62007-04-06 01:03:32 +00004051 assert( pRes!=0 );
drh8c4d3a62007-04-06 01:03:32 +00004052 if( CURSOR_INVALID==pCur->eState ){
4053 *pRes = 1;
4054 return SQLITE_OK;
4055 }
danielk1977da184232006-01-05 11:34:32 +00004056 if( pCur->skip>0 ){
4057 pCur->skip = 0;
4058 *pRes = 0;
4059 return SQLITE_OK;
4060 }
4061 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00004062
danielk197771d5d2c2008-09-29 11:49:47 +00004063 pPage = pCur->apPage[pCur->iPage];
4064 idx = ++pCur->aiIdx[pCur->iPage];
4065 assert( pPage->isInit );
4066 assert( idx<=pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00004067
drh271efa52004-05-30 19:19:05 +00004068 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00004069 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00004070 if( idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00004071 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004072 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00004073 if( rc ) return rc;
4074 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00004075 *pRes = 0;
4076 return rc;
drh72f82862001-05-24 21:06:34 +00004077 }
drh5e2f8b92001-05-28 00:41:15 +00004078 do{
danielk197771d5d2c2008-09-29 11:49:47 +00004079 if( pCur->iPage==0 ){
drh8c1238a2003-01-02 14:43:55 +00004080 *pRes = 1;
danielk1977da184232006-01-05 11:34:32 +00004081 pCur->eState = CURSOR_INVALID;
drh5e2f8b92001-05-28 00:41:15 +00004082 return SQLITE_OK;
4083 }
drh16a9b832007-05-05 18:39:25 +00004084 sqlite3BtreeMoveToParent(pCur);
danielk197771d5d2c2008-09-29 11:49:47 +00004085 pPage = pCur->apPage[pCur->iPage];
4086 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00004087 *pRes = 0;
drh44845222008-07-17 18:39:57 +00004088 if( pPage->intKey ){
drh8b18dd42004-05-12 19:18:15 +00004089 rc = sqlite3BtreeNext(pCur, pRes);
4090 }else{
4091 rc = SQLITE_OK;
4092 }
4093 return rc;
drh8178a752003-01-05 21:41:40 +00004094 }
4095 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00004096 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00004097 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00004098 }
drh5e2f8b92001-05-28 00:41:15 +00004099 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00004100 return rc;
drh72f82862001-05-24 21:06:34 +00004101}
drhd677b3d2007-08-20 22:48:41 +00004102
drh72f82862001-05-24 21:06:34 +00004103
drh3b7511c2001-05-26 13:15:44 +00004104/*
drh2dcc9aa2002-12-04 13:40:25 +00004105** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00004106** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00004107** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00004108** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00004109*/
drhd094db12008-04-03 21:46:57 +00004110int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00004111 int rc;
drh8178a752003-01-05 21:41:40 +00004112 MemPage *pPage;
danielk1977da184232006-01-05 11:34:32 +00004113
drh1fee73e2007-08-29 04:00:57 +00004114 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00004115 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00004116 if( rc!=SQLITE_OK ){
4117 return rc;
4118 }
drha2c20e42008-03-29 16:01:04 +00004119 pCur->atLast = 0;
drh8c4d3a62007-04-06 01:03:32 +00004120 if( CURSOR_INVALID==pCur->eState ){
4121 *pRes = 1;
4122 return SQLITE_OK;
4123 }
danielk1977da184232006-01-05 11:34:32 +00004124 if( pCur->skip<0 ){
4125 pCur->skip = 0;
4126 *pRes = 0;
4127 return SQLITE_OK;
4128 }
4129 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00004130
danielk197771d5d2c2008-09-29 11:49:47 +00004131 pPage = pCur->apPage[pCur->iPage];
4132 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00004133 if( !pPage->leaf ){
danielk197771d5d2c2008-09-29 11:49:47 +00004134 int idx = pCur->aiIdx[pCur->iPage];
4135 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
drhd677b3d2007-08-20 22:48:41 +00004136 if( rc ){
4137 return rc;
4138 }
drh2dcc9aa2002-12-04 13:40:25 +00004139 rc = moveToRightmost(pCur);
4140 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00004141 while( pCur->aiIdx[pCur->iPage]==0 ){
4142 if( pCur->iPage==0 ){
danielk1977da184232006-01-05 11:34:32 +00004143 pCur->eState = CURSOR_INVALID;
drhc39e0002004-05-07 23:50:57 +00004144 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00004145 return SQLITE_OK;
4146 }
drh16a9b832007-05-05 18:39:25 +00004147 sqlite3BtreeMoveToParent(pCur);
drh2dcc9aa2002-12-04 13:40:25 +00004148 }
drh271efa52004-05-30 19:19:05 +00004149 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00004150 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00004151
4152 pCur->aiIdx[pCur->iPage]--;
4153 pPage = pCur->apPage[pCur->iPage];
drh44845222008-07-17 18:39:57 +00004154 if( pPage->intKey && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00004155 rc = sqlite3BtreePrevious(pCur, pRes);
4156 }else{
4157 rc = SQLITE_OK;
4158 }
drh2dcc9aa2002-12-04 13:40:25 +00004159 }
drh8178a752003-01-05 21:41:40 +00004160 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00004161 return rc;
4162}
4163
4164/*
drh3b7511c2001-05-26 13:15:44 +00004165** Allocate a new page from the database file.
4166**
danielk19773b8a05f2007-03-19 17:44:26 +00004167** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:44 +00004168** has already been called on the new page.) The new page has also
4169** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:26 +00004170** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00004171**
4172** SQLITE_OK is returned on success. Any other return value indicates
4173** an error. *ppPage and *pPgno are undefined in the event of an error.
danielk19773b8a05f2007-03-19 17:44:26 +00004174** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00004175**
drh199e3cf2002-07-18 11:01:47 +00004176** If the "nearby" parameter is not 0, then a (feeble) effort is made to
4177** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00004178** attempt to keep related pages close to each other in the database file,
4179** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00004180**
4181** If the "exact" parameter is not 0, and the page-number nearby exists
4182** anywhere on the free-list, then it is guarenteed to be returned. This
4183** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00004184*/
drh4f0c5872007-03-26 22:05:01 +00004185static int allocateBtreePage(
danielk1977aef0bf62005-12-30 16:28:01 +00004186 BtShared *pBt,
danielk1977cb1a7eb2004-11-05 12:27:02 +00004187 MemPage **ppPage,
4188 Pgno *pPgno,
4189 Pgno nearby,
4190 u8 exact
4191){
drh3aac2dd2004-04-26 14:10:20 +00004192 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00004193 int rc;
drh3aac2dd2004-04-26 14:10:20 +00004194 int n; /* Number of pages on the freelist */
4195 int k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:51 +00004196 MemPage *pTrunk = 0;
4197 MemPage *pPrevTrunk = 0;
drh30e58752002-03-02 20:41:57 +00004198
drh1fee73e2007-08-29 04:00:57 +00004199 assert( sqlite3_mutex_held(pBt->mutex) );
drh3aac2dd2004-04-26 14:10:20 +00004200 pPage1 = pBt->pPage1;
4201 n = get4byte(&pPage1->aData[36]);
4202 if( n>0 ){
drh91025292004-05-03 19:49:32 +00004203 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00004204 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004205 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
4206
4207 /* If the 'exact' parameter was true and a query of the pointer-map
4208 ** shows that the page 'nearby' is somewhere on the free-list, then
4209 ** the entire-list will be searched for that page.
4210 */
4211#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197789d40042008-11-17 14:20:56 +00004212 if( exact && nearby<=pagerPagecount(pBt) ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004213 u8 eType;
4214 assert( nearby>0 );
4215 assert( pBt->autoVacuum );
4216 rc = ptrmapGet(pBt, nearby, &eType, 0);
4217 if( rc ) return rc;
4218 if( eType==PTRMAP_FREEPAGE ){
4219 searchList = 1;
4220 }
4221 *pPgno = nearby;
4222 }
4223#endif
4224
4225 /* Decrement the free-list count by 1. Set iTrunk to the index of the
4226 ** first free-list trunk page. iPrevTrunk is initially 1.
4227 */
danielk19773b8a05f2007-03-19 17:44:26 +00004228 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:44 +00004229 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004230 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004231
4232 /* The code within this loop is run only once if the 'searchList' variable
4233 ** is not true. Otherwise, it runs once for each trunk-page on the
4234 ** free-list until the page 'nearby' is located.
4235 */
4236 do {
4237 pPrevTrunk = pTrunk;
4238 if( pPrevTrunk ){
4239 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00004240 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004241 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00004242 }
drh16a9b832007-05-05 18:39:25 +00004243 rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004244 if( rc ){
drhd3627af2006-12-18 18:34:51 +00004245 pTrunk = 0;
4246 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004247 }
4248
4249 k = get4byte(&pTrunk->aData[4]);
4250 if( k==0 && !searchList ){
4251 /* The trunk has no leaves and the list is not being searched.
4252 ** So extract the trunk page itself and use it as the newly
4253 ** allocated page */
4254 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004255 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004256 if( rc ){
4257 goto end_allocate_page;
4258 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004259 *pPgno = iTrunk;
4260 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4261 *ppPage = pTrunk;
4262 pTrunk = 0;
4263 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
drh45b1fac2008-07-04 17:52:42 +00004264 }else if( k>pBt->usableSize/4 - 2 ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004265 /* Value of k is out of range. Database corruption */
drhd3627af2006-12-18 18:34:51 +00004266 rc = SQLITE_CORRUPT_BKPT;
4267 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004268#ifndef SQLITE_OMIT_AUTOVACUUM
4269 }else if( searchList && nearby==iTrunk ){
4270 /* The list is being searched and this trunk page is the page
4271 ** to allocate, regardless of whether it has leaves.
4272 */
4273 assert( *pPgno==iTrunk );
4274 *ppPage = pTrunk;
4275 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004276 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004277 if( rc ){
4278 goto end_allocate_page;
4279 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004280 if( k==0 ){
4281 if( !pPrevTrunk ){
4282 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4283 }else{
4284 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
4285 }
4286 }else{
4287 /* The trunk page is required by the caller but it contains
4288 ** pointers to free-list leaves. The first leaf becomes a trunk
4289 ** page in this case.
4290 */
4291 MemPage *pNewTrunk;
4292 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
drh16a9b832007-05-05 18:39:25 +00004293 rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004294 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:51 +00004295 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004296 }
danielk19773b8a05f2007-03-19 17:44:26 +00004297 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004298 if( rc!=SQLITE_OK ){
4299 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:51 +00004300 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004301 }
4302 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
4303 put4byte(&pNewTrunk->aData[4], k-1);
4304 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:51 +00004305 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004306 if( !pPrevTrunk ){
drhc5053fb2008-11-27 02:22:10 +00004307 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
danielk1977cb1a7eb2004-11-05 12:27:02 +00004308 put4byte(&pPage1->aData[32], iNewTrunk);
4309 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004310 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004311 if( rc ){
4312 goto end_allocate_page;
4313 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004314 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
4315 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004316 }
4317 pTrunk = 0;
4318 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
4319#endif
4320 }else{
4321 /* Extract a leaf from the trunk */
4322 int closest;
4323 Pgno iPage;
4324 unsigned char *aData = pTrunk->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00004325 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004326 if( rc ){
4327 goto end_allocate_page;
4328 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004329 if( nearby>0 ){
4330 int i, dist;
4331 closest = 0;
4332 dist = get4byte(&aData[8]) - nearby;
4333 if( dist<0 ) dist = -dist;
4334 for(i=1; i<k; i++){
4335 int d2 = get4byte(&aData[8+i*4]) - nearby;
4336 if( d2<0 ) d2 = -d2;
4337 if( d2<dist ){
4338 closest = i;
4339 dist = d2;
4340 }
4341 }
4342 }else{
4343 closest = 0;
4344 }
4345
4346 iPage = get4byte(&aData[8+closest*4]);
4347 if( !searchList || iPage==nearby ){
danielk1977bea2a942009-01-20 17:06:27 +00004348 int noContent;
danielk197789d40042008-11-17 14:20:56 +00004349 Pgno nPage;
shane1f9e6aa2008-06-09 19:27:11 +00004350 *pPgno = iPage;
danielk197789d40042008-11-17 14:20:56 +00004351 nPage = pagerPagecount(pBt);
danielk1977ad0132d2008-06-07 08:58:22 +00004352 if( *pPgno>nPage ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004353 /* Free page off the end of the file */
danielk197743e377a2008-05-05 12:09:32 +00004354 rc = SQLITE_CORRUPT_BKPT;
4355 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004356 }
4357 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
4358 ": %d more free pages\n",
4359 *pPgno, closest+1, k, pTrunk->pgno, n-1));
4360 if( closest<k-1 ){
4361 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
4362 }
4363 put4byte(&aData[4], k-1);
drhc5053fb2008-11-27 02:22:10 +00004364 assert( sqlite3PagerIswriteable(pTrunk->pDbPage) );
danielk1977bea2a942009-01-20 17:06:27 +00004365 noContent = !btreeGetHasContent(pBt, *pPgno);
4366 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, noContent);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004367 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004368 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004369 if( rc!=SQLITE_OK ){
4370 releasePage(*ppPage);
4371 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004372 }
4373 searchList = 0;
4374 }
drhee696e22004-08-30 16:52:17 +00004375 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004376 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:51 +00004377 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004378 }while( searchList );
drh3b7511c2001-05-26 13:15:44 +00004379 }else{
drh3aac2dd2004-04-26 14:10:20 +00004380 /* There are no pages on the freelist, so create a new page at the
4381 ** end of the file */
danielk197789d40042008-11-17 14:20:56 +00004382 int nPage = pagerPagecount(pBt);
danielk1977ad0132d2008-06-07 08:58:22 +00004383 *pPgno = nPage + 1;
danielk1977afcdd022004-10-31 16:25:42 +00004384
danielk1977bea2a942009-01-20 17:06:27 +00004385 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
4386 (*pPgno)++;
4387 }
4388
danielk1977afcdd022004-10-31 16:25:42 +00004389#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977266664d2006-02-10 08:24:21 +00004390 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00004391 /* If *pPgno refers to a pointer-map page, allocate two new pages
4392 ** at the end of the file instead of one. The first allocated page
4393 ** becomes a new pointer-map page, the second is used by the caller.
4394 */
4395 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00004396 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00004397 (*pPgno)++;
drh72190432008-01-31 14:54:43 +00004398 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
danielk1977afcdd022004-10-31 16:25:42 +00004399 }
4400#endif
4401
danielk1977599fcba2004-11-08 07:13:13 +00004402 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh16a9b832007-05-05 18:39:25 +00004403 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
drh3b7511c2001-05-26 13:15:44 +00004404 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004405 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004406 if( rc!=SQLITE_OK ){
4407 releasePage(*ppPage);
4408 }
drh3a4c1412004-05-09 20:40:11 +00004409 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00004410 }
danielk1977599fcba2004-11-08 07:13:13 +00004411
4412 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:51 +00004413
4414end_allocate_page:
4415 releasePage(pTrunk);
4416 releasePage(pPrevTrunk);
danielk1977b247c212008-11-21 09:09:01 +00004417 if( rc==SQLITE_OK ){
4418 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
4419 releasePage(*ppPage);
4420 return SQLITE_CORRUPT_BKPT;
4421 }
4422 (*ppPage)->isInit = 0;
danielk1977eaa06f62008-09-18 17:34:44 +00004423 }
drh3b7511c2001-05-26 13:15:44 +00004424 return rc;
4425}
4426
4427/*
danielk1977bea2a942009-01-20 17:06:27 +00004428** This function is used to add page iPage to the database file free-list.
4429** It is assumed that the page is not already a part of the free-list.
drh5e2f8b92001-05-28 00:41:15 +00004430**
danielk1977bea2a942009-01-20 17:06:27 +00004431** The value passed as the second argument to this function is optional.
4432** If the caller happens to have a pointer to the MemPage object
4433** corresponding to page iPage handy, it may pass it as the second value.
4434** Otherwise, it may pass NULL.
4435**
4436** If a pointer to a MemPage object is passed as the second argument,
4437** its reference count is not altered by this function.
drh3b7511c2001-05-26 13:15:44 +00004438*/
danielk1977bea2a942009-01-20 17:06:27 +00004439static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
4440 MemPage *pTrunk = 0; /* Free-list trunk page */
4441 Pgno iTrunk = 0; /* Page number of free-list trunk page */
4442 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
4443 MemPage *pPage; /* Page being freed. May be NULL. */
4444 int rc; /* Return Code */
4445 int nFree; /* Initial number of pages on free-list */
drh8b2f49b2001-06-08 00:21:52 +00004446
danielk1977bea2a942009-01-20 17:06:27 +00004447 assert( sqlite3_mutex_held(pBt->mutex) );
4448 assert( iPage>1 );
4449 assert( !pMemPage || pMemPage->pgno==iPage );
4450
4451 if( pMemPage ){
4452 pPage = pMemPage;
4453 sqlite3PagerRef(pPage->pDbPage);
4454 }else{
4455 pPage = btreePageLookup(pBt, iPage);
4456 }
drh3aac2dd2004-04-26 14:10:20 +00004457
drha34b6762004-05-07 13:30:42 +00004458 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:26 +00004459 rc = sqlite3PagerWrite(pPage1->pDbPage);
danielk1977bea2a942009-01-20 17:06:27 +00004460 if( rc ) goto freepage_out;
4461 nFree = get4byte(&pPage1->aData[36]);
4462 put4byte(&pPage1->aData[36], nFree+1);
drh3aac2dd2004-04-26 14:10:20 +00004463
drhfcce93f2006-02-22 03:08:32 +00004464#ifdef SQLITE_SECURE_DELETE
4465 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
4466 ** always fully overwrite deleted information with zeros.
4467 */
danielk1977bea2a942009-01-20 17:06:27 +00004468 if( (!pPage && (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0)))
4469 || (rc = sqlite3PagerWrite(pPage->pDbPage))
4470 ){
4471 goto freepage_out;
4472 }
drhfcce93f2006-02-22 03:08:32 +00004473 memset(pPage->aData, 0, pPage->pBt->pageSize);
4474#endif
4475
danielk1977687566d2004-11-02 12:56:41 +00004476 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00004477 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00004478 */
danielk197785d90ca2008-07-19 14:25:15 +00004479 if( ISAUTOVACUUM ){
danielk1977bea2a942009-01-20 17:06:27 +00004480 rc = ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0);
4481 if( rc ) goto freepage_out;
danielk1977687566d2004-11-02 12:56:41 +00004482 }
danielk1977687566d2004-11-02 12:56:41 +00004483
danielk1977bea2a942009-01-20 17:06:27 +00004484 /* Now manipulate the actual database free-list structure. There are two
4485 ** possibilities. If the free-list is currently empty, or if the first
4486 ** trunk page in the free-list is full, then this page will become a
4487 ** new free-list trunk page. Otherwise, it will become a leaf of the
4488 ** first trunk page in the current free-list. This block tests if it
4489 ** is possible to add the page as a new free-list leaf.
4490 */
4491 if( nFree!=0 ){
4492 int nLeaf; /* Initial number of leaf cells on trunk page */
4493
4494 iTrunk = get4byte(&pPage1->aData[32]);
4495 rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
4496 if( rc!=SQLITE_OK ){
4497 goto freepage_out;
4498 }
4499
4500 nLeaf = get4byte(&pTrunk->aData[4]);
4501 if( nLeaf<0 ){
4502 rc = SQLITE_CORRUPT_BKPT;
4503 goto freepage_out;
4504 }
4505 if( nLeaf<pBt->usableSize/4 - 8 ){
4506 /* In this case there is room on the trunk page to insert the page
4507 ** being freed as a new leaf.
drh45b1fac2008-07-04 17:52:42 +00004508 **
4509 ** Note that the trunk page is not really full until it contains
4510 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
4511 ** coded. But due to a coding error in versions of SQLite prior to
4512 ** 3.6.0, databases with freelist trunk pages holding more than
4513 ** usableSize/4 - 8 entries will be reported as corrupt. In order
4514 ** to maintain backwards compatibility with older versions of SQLite,
4515 ** we will contain to restrict the number of entries to usableSize/4 - 8
4516 ** for now. At some point in the future (once everyone has upgraded
4517 ** to 3.6.0 or later) we should consider fixing the conditional above
4518 ** to read "usableSize/4-2" instead of "usableSize/4-8".
4519 */
danielk19773b8a05f2007-03-19 17:44:26 +00004520 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhf5345442007-04-09 12:45:02 +00004521 if( rc==SQLITE_OK ){
danielk1977bea2a942009-01-20 17:06:27 +00004522 put4byte(&pTrunk->aData[4], nLeaf+1);
4523 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
drhfcce93f2006-02-22 03:08:32 +00004524#ifndef SQLITE_SECURE_DELETE
danielk1977bea2a942009-01-20 17:06:27 +00004525 if( pPage ){
4526 sqlite3PagerDontWrite(pPage->pDbPage);
4527 }
drhfcce93f2006-02-22 03:08:32 +00004528#endif
danielk1977bea2a942009-01-20 17:06:27 +00004529 rc = btreeSetHasContent(pBt, iPage);
drhf5345442007-04-09 12:45:02 +00004530 }
drh3a4c1412004-05-09 20:40:11 +00004531 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
danielk1977bea2a942009-01-20 17:06:27 +00004532 goto freepage_out;
drh3aac2dd2004-04-26 14:10:20 +00004533 }
drh3b7511c2001-05-26 13:15:44 +00004534 }
danielk1977bea2a942009-01-20 17:06:27 +00004535
4536 /* If control flows to this point, then it was not possible to add the
4537 ** the page being freed as a leaf page of the first trunk in the free-list.
4538 ** Possibly because the free-list is empty, or possibly because the
4539 ** first trunk in the free-list is full. Either way, the page being freed
4540 ** will become the new first trunk page in the free-list.
4541 */
shane63207ab2009-02-04 01:49:30 +00004542 if( ((!pPage) && (0 != (rc = sqlite3BtreeGetPage(pBt, iPage, &pPage, 0))))
4543 || (0 != (rc = sqlite3PagerWrite(pPage->pDbPage)))
danielk1977bea2a942009-01-20 17:06:27 +00004544 ){
4545 goto freepage_out;
4546 }
4547 put4byte(pPage->aData, iTrunk);
4548 put4byte(&pPage->aData[4], 0);
4549 put4byte(&pPage1->aData[32], iPage);
4550 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
4551
4552freepage_out:
4553 if( pPage ){
4554 pPage->isInit = 0;
4555 }
4556 releasePage(pPage);
4557 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00004558 return rc;
4559}
danielk1977bea2a942009-01-20 17:06:27 +00004560static int freePage(MemPage *pPage){
4561 return freePage2(pPage->pBt, pPage, pPage->pgno);
4562}
drh3b7511c2001-05-26 13:15:44 +00004563
4564/*
drh3aac2dd2004-04-26 14:10:20 +00004565** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00004566*/
drh3aac2dd2004-04-26 14:10:20 +00004567static int clearCell(MemPage *pPage, unsigned char *pCell){
danielk1977aef0bf62005-12-30 16:28:01 +00004568 BtShared *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00004569 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00004570 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00004571 int rc;
drh94440812007-03-06 11:42:19 +00004572 int nOvfl;
shane63207ab2009-02-04 01:49:30 +00004573 u16 ovflPageSize;
drh3b7511c2001-05-26 13:15:44 +00004574
drh1fee73e2007-08-29 04:00:57 +00004575 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh16a9b832007-05-05 18:39:25 +00004576 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004577 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00004578 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00004579 }
drh6f11bef2004-05-13 01:12:56 +00004580 ovflPgno = get4byte(&pCell[info.iOverflow]);
shane63207ab2009-02-04 01:49:30 +00004581 assert( pBt->usableSize > 4 );
drh94440812007-03-06 11:42:19 +00004582 ovflPageSize = pBt->usableSize - 4;
drh72365832007-03-06 15:53:44 +00004583 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
4584 assert( ovflPgno==0 || nOvfl>0 );
4585 while( nOvfl-- ){
shane63207ab2009-02-04 01:49:30 +00004586 Pgno iNext = 0;
danielk1977bea2a942009-01-20 17:06:27 +00004587 MemPage *pOvfl = 0;
danielk197789d40042008-11-17 14:20:56 +00004588 if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){
drh49285702005-09-17 15:20:26 +00004589 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00004590 }
danielk1977bea2a942009-01-20 17:06:27 +00004591 if( nOvfl ){
4592 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
4593 if( rc ) return rc;
4594 }
4595 rc = freePage2(pBt, pOvfl, ovflPgno);
4596 if( pOvfl ){
4597 sqlite3PagerUnref(pOvfl->pDbPage);
4598 }
drh3b7511c2001-05-26 13:15:44 +00004599 if( rc ) return rc;
danielk1977bea2a942009-01-20 17:06:27 +00004600 ovflPgno = iNext;
drh3b7511c2001-05-26 13:15:44 +00004601 }
drh5e2f8b92001-05-28 00:41:15 +00004602 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00004603}
4604
4605/*
drh91025292004-05-03 19:49:32 +00004606** Create the byte sequence used to represent a cell on page pPage
4607** and write that byte sequence into pCell[]. Overflow pages are
4608** allocated and filled in as necessary. The calling procedure
4609** is responsible for making sure sufficient space has been allocated
4610** for pCell[].
4611**
4612** Note that pCell does not necessary need to point to the pPage->aData
4613** area. pCell might point to some temporary storage. The cell will
4614** be constructed in this temporary area then copied into pPage->aData
4615** later.
drh3b7511c2001-05-26 13:15:44 +00004616*/
4617static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00004618 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00004619 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00004620 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00004621 const void *pData,int nData, /* The data */
drhb026e052007-05-02 01:34:31 +00004622 int nZero, /* Extra zero bytes to append to pData */
drh4b70f112004-05-02 21:12:19 +00004623 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00004624){
drh3b7511c2001-05-26 13:15:44 +00004625 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00004626 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00004627 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00004628 int spaceLeft;
4629 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00004630 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00004631 unsigned char *pPrior;
4632 unsigned char *pPayload;
danielk1977aef0bf62005-12-30 16:28:01 +00004633 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00004634 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00004635 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00004636 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00004637
drh1fee73e2007-08-29 04:00:57 +00004638 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004639
drhc5053fb2008-11-27 02:22:10 +00004640 /* pPage is not necessarily writeable since pCell might be auxiliary
4641 ** buffer space that is separate from the pPage buffer area */
4642 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
4643 || sqlite3PagerIswriteable(pPage->pDbPage) );
4644
drh91025292004-05-03 19:49:32 +00004645 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00004646 nHeader = 0;
drh91025292004-05-03 19:49:32 +00004647 if( !pPage->leaf ){
4648 nHeader += 4;
4649 }
drh8b18dd42004-05-12 19:18:15 +00004650 if( pPage->hasData ){
drhb026e052007-05-02 01:34:31 +00004651 nHeader += putVarint(&pCell[nHeader], nData+nZero);
drh6f11bef2004-05-13 01:12:56 +00004652 }else{
drhb026e052007-05-02 01:34:31 +00004653 nData = nZero = 0;
drh91025292004-05-03 19:49:32 +00004654 }
drh6f11bef2004-05-13 01:12:56 +00004655 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh16a9b832007-05-05 18:39:25 +00004656 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004657 assert( info.nHeader==nHeader );
4658 assert( info.nKey==nKey );
danielk197789d40042008-11-17 14:20:56 +00004659 assert( info.nData==(u32)(nData+nZero) );
drh6f11bef2004-05-13 01:12:56 +00004660
4661 /* Fill in the payload */
drhb026e052007-05-02 01:34:31 +00004662 nPayload = nData + nZero;
drh3aac2dd2004-04-26 14:10:20 +00004663 if( pPage->intKey ){
4664 pSrc = pData;
4665 nSrc = nData;
drh91025292004-05-03 19:49:32 +00004666 nData = 0;
drhf49661a2008-12-10 16:45:50 +00004667 }else{
drh20abac22009-01-28 20:21:17 +00004668 if( nKey>0x7fffffff || pKey==0 ){
4669 return SQLITE_CORRUPT;
4670 }
drhf49661a2008-12-10 16:45:50 +00004671 nPayload += (int)nKey;
drh3aac2dd2004-04-26 14:10:20 +00004672 pSrc = pKey;
drhf49661a2008-12-10 16:45:50 +00004673 nSrc = (int)nKey;
drh3aac2dd2004-04-26 14:10:20 +00004674 }
drh6f11bef2004-05-13 01:12:56 +00004675 *pnSize = info.nSize;
4676 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00004677 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00004678 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00004679
drh3b7511c2001-05-26 13:15:44 +00004680 while( nPayload>0 ){
4681 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00004682#ifndef SQLITE_OMIT_AUTOVACUUM
4683 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
danielk1977b39f70b2007-05-17 18:28:11 +00004684 if( pBt->autoVacuum ){
4685 do{
4686 pgnoOvfl++;
4687 } while(
4688 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
4689 );
danielk1977b39f70b2007-05-17 18:28:11 +00004690 }
danielk1977afcdd022004-10-31 16:25:42 +00004691#endif
drhf49661a2008-12-10 16:45:50 +00004692 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00004693#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00004694 /* If the database supports auto-vacuum, and the second or subsequent
4695 ** overflow page is being allocated, add an entry to the pointer-map
danielk19774ef24492007-05-23 09:52:41 +00004696 ** for that page now.
4697 **
4698 ** If this is the first overflow page, then write a partial entry
4699 ** to the pointer-map. If we write nothing to this pointer-map slot,
4700 ** then the optimistic overflow chain processing in clearCell()
4701 ** may misinterpret the uninitialised values and delete the
4702 ** wrong pages from the database.
danielk1977afcdd022004-10-31 16:25:42 +00004703 */
danielk19774ef24492007-05-23 09:52:41 +00004704 if( pBt->autoVacuum && rc==SQLITE_OK ){
4705 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
4706 rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
danielk197789a4be82007-05-23 13:34:32 +00004707 if( rc ){
4708 releasePage(pOvfl);
4709 }
danielk1977afcdd022004-10-31 16:25:42 +00004710 }
4711#endif
drh3b7511c2001-05-26 13:15:44 +00004712 if( rc ){
drh9b171272004-05-08 02:03:22 +00004713 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004714 return rc;
4715 }
drhc5053fb2008-11-27 02:22:10 +00004716
4717 /* If pToRelease is not zero than pPrior points into the data area
4718 ** of pToRelease. Make sure pToRelease is still writeable. */
4719 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
4720
4721 /* If pPrior is part of the data area of pPage, then make sure pPage
4722 ** is still writeable */
4723 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
4724 || sqlite3PagerIswriteable(pPage->pDbPage) );
4725
drh3aac2dd2004-04-26 14:10:20 +00004726 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00004727 releasePage(pToRelease);
4728 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00004729 pPrior = pOvfl->aData;
4730 put4byte(pPrior, 0);
4731 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00004732 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00004733 }
4734 n = nPayload;
4735 if( n>spaceLeft ) n = spaceLeft;
drhc5053fb2008-11-27 02:22:10 +00004736
4737 /* If pToRelease is not zero than pPayload points into the data area
4738 ** of pToRelease. Make sure pToRelease is still writeable. */
4739 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
4740
4741 /* If pPayload is part of the data area of pPage, then make sure pPage
4742 ** is still writeable */
4743 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
4744 || sqlite3PagerIswriteable(pPage->pDbPage) );
4745
drhb026e052007-05-02 01:34:31 +00004746 if( nSrc>0 ){
4747 if( n>nSrc ) n = nSrc;
4748 assert( pSrc );
4749 memcpy(pPayload, pSrc, n);
4750 }else{
4751 memset(pPayload, 0, n);
4752 }
drh3b7511c2001-05-26 13:15:44 +00004753 nPayload -= n;
drhde647132004-05-07 17:57:49 +00004754 pPayload += n;
drh9b171272004-05-08 02:03:22 +00004755 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00004756 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00004757 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00004758 if( nSrc==0 ){
4759 nSrc = nData;
4760 pSrc = pData;
4761 }
drhdd793422001-06-28 01:54:48 +00004762 }
drh9b171272004-05-08 02:03:22 +00004763 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004764 return SQLITE_OK;
4765}
4766
drh14acc042001-06-10 19:56:58 +00004767/*
4768** Remove the i-th cell from pPage. This routine effects pPage only.
4769** The cell content is not freed or deallocated. It is assumed that
4770** the cell content has been copied someplace else. This routine just
4771** removes the reference to the cell from pPage.
4772**
4773** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00004774*/
shane0af3f892008-11-12 04:55:34 +00004775static int dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00004776 int i; /* Loop counter */
4777 int pc; /* Offset to cell content of cell being deleted */
4778 u8 *data; /* pPage->aData */
4779 u8 *ptr; /* Used to move bytes around within data[] */
shanedcc50b72008-11-13 18:29:50 +00004780 int rc; /* The return code */
drh43605152004-05-29 21:46:49 +00004781
drh8c42ca92001-06-22 19:15:00 +00004782 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004783 assert( sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:26 +00004784 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +00004785 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhda200cc2004-05-09 11:51:38 +00004786 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00004787 ptr = &data[pPage->cellOffset + 2*idx];
shane0af3f892008-11-12 04:55:34 +00004788 pc = get2byte(ptr);
drhc5053fb2008-11-27 02:22:10 +00004789 if( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4))
4790 || (pc+sz>pPage->pBt->usableSize) ){
shane0af3f892008-11-12 04:55:34 +00004791 return SQLITE_CORRUPT_BKPT;
4792 }
shanedcc50b72008-11-13 18:29:50 +00004793 rc = freeSpace(pPage, pc, sz);
4794 if( rc!=SQLITE_OK ){
4795 return rc;
4796 }
drh43605152004-05-29 21:46:49 +00004797 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4798 ptr[0] = ptr[2];
4799 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00004800 }
4801 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00004802 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4803 pPage->nFree += 2;
shane0af3f892008-11-12 04:55:34 +00004804 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004805}
4806
4807/*
4808** Insert a new cell on pPage at cell index "i". pCell points to the
4809** content of the cell.
4810**
4811** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00004812** will not fit, then make a copy of the cell content into pTemp if
4813** pTemp is not null. Regardless of pTemp, allocate a new entry
4814** in pPage->aOvfl[] and make it point to the cell content (either
4815** in pTemp or the original pCell) and also record its index.
4816** Allocating a new entry in pPage->aCell[] implies that
4817** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00004818**
4819** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4820** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00004821** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00004822** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00004823*/
danielk1977e80463b2004-11-03 03:01:16 +00004824static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00004825 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00004826 int i, /* New cell becomes the i-th cell of the page */
4827 u8 *pCell, /* Content of the new cell */
4828 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00004829 u8 *pTemp, /* Temp storage space for pCell, if needed */
4830 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00004831){
drh43605152004-05-29 21:46:49 +00004832 int idx; /* Where to write new cell content in data[] */
4833 int j; /* Loop counter */
4834 int top; /* First byte of content for any cell in data[] */
4835 int end; /* First byte past the last cell pointer in data[] */
4836 int ins; /* Index in data[] where new cell pointer is inserted */
4837 int hdr; /* Offset into data[] of the page header */
4838 int cellOffset; /* Address of first cell pointer in data[] */
4839 u8 *data; /* The content of the whole page */
4840 u8 *ptr; /* Used for moving information around in data[] */
4841
4842 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
drhf49661a2008-12-10 16:45:50 +00004843 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
4844 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
drh43605152004-05-29 21:46:49 +00004845 assert( sz==cellSizePtr(pPage, pCell) );
drh1fee73e2007-08-29 04:00:57 +00004846 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +00004847 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00004848 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00004849 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004850 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00004851 }
drh43605152004-05-29 21:46:49 +00004852 j = pPage->nOverflow++;
danielk197789d40042008-11-17 14:20:56 +00004853 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
drh43605152004-05-29 21:46:49 +00004854 pPage->aOvfl[j].pCell = pCell;
drhf49661a2008-12-10 16:45:50 +00004855 pPage->aOvfl[j].idx = (u16)i;
drh43605152004-05-29 21:46:49 +00004856 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00004857 }else{
danielk19776e465eb2007-08-21 13:11:00 +00004858 int rc = sqlite3PagerWrite(pPage->pDbPage);
4859 if( rc!=SQLITE_OK ){
4860 return rc;
4861 }
4862 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004863 data = pPage->aData;
4864 hdr = pPage->hdrOffset;
4865 top = get2byte(&data[hdr+5]);
4866 cellOffset = pPage->cellOffset;
4867 end = cellOffset + 2*pPage->nCell + 2;
4868 ins = cellOffset + 2*i;
4869 if( end > top - sz ){
shane0af3f892008-11-12 04:55:34 +00004870 rc = defragmentPage(pPage);
4871 if( rc!=SQLITE_OK ){
4872 return rc;
4873 }
drh43605152004-05-29 21:46:49 +00004874 top = get2byte(&data[hdr+5]);
4875 assert( end + sz <= top );
4876 }
4877 idx = allocateSpace(pPage, sz);
4878 assert( idx>0 );
4879 assert( end <= get2byte(&data[hdr+5]) );
shane0af3f892008-11-12 04:55:34 +00004880 if (idx+sz > pPage->pBt->usableSize) {
shane34ac18d2008-11-11 22:18:20 +00004881 return SQLITE_CORRUPT_BKPT;
shane0af3f892008-11-12 04:55:34 +00004882 }
drh43605152004-05-29 21:46:49 +00004883 pPage->nCell++;
4884 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00004885 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004886 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4887 ptr[0] = ptr[-2];
4888 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00004889 }
drh43605152004-05-29 21:46:49 +00004890 put2byte(&data[ins], idx);
4891 put2byte(&data[hdr+3], pPage->nCell);
danielk1977a19df672004-11-03 11:37:07 +00004892#ifndef SQLITE_OMIT_AUTOVACUUM
4893 if( pPage->pBt->autoVacuum ){
4894 /* The cell may contain a pointer to an overflow page. If so, write
4895 ** the entry for the overflow page into the pointer map.
4896 */
4897 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00004898 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00004899 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk1977a19df672004-11-03 11:37:07 +00004900 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4901 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
danielk19776e465eb2007-08-21 13:11:00 +00004902 rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977a19df672004-11-03 11:37:07 +00004903 if( rc!=SQLITE_OK ) return rc;
4904 }
4905 }
4906#endif
drh14acc042001-06-10 19:56:58 +00004907 }
danielk1977e80463b2004-11-03 03:01:16 +00004908
danielk1977e80463b2004-11-03 03:01:16 +00004909 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004910}
4911
4912/*
drhfa1a98a2004-05-14 19:08:17 +00004913** Add a list of cells to a page. The page should be initially empty.
4914** The cells are guaranteed to fit on the page.
4915*/
4916static void assemblePage(
4917 MemPage *pPage, /* The page to be assemblied */
4918 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00004919 u8 **apCell, /* Pointers to cell bodies */
drha9121e42008-02-19 14:59:35 +00004920 u16 *aSize /* Sizes of the cells */
drhfa1a98a2004-05-14 19:08:17 +00004921){
4922 int i; /* Loop counter */
4923 int totalSize; /* Total size of all cells */
4924 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00004925 int cellptr; /* Address of next cell pointer */
4926 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00004927 u8 *data; /* Data for the page */
4928
drh43605152004-05-29 21:46:49 +00004929 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +00004930 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf49661a2008-12-10 16:45:50 +00004931 assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
drhfa1a98a2004-05-14 19:08:17 +00004932 totalSize = 0;
4933 for(i=0; i<nCell; i++){
4934 totalSize += aSize[i];
4935 }
drh43605152004-05-29 21:46:49 +00004936 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00004937 assert( pPage->nCell==0 );
drhc5053fb2008-11-27 02:22:10 +00004938 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004939 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00004940 data = pPage->aData;
4941 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00004942 put2byte(&data[hdr+3], nCell);
drh09d0deb2005-08-02 17:13:09 +00004943 if( nCell ){
4944 cellbody = allocateSpace(pPage, totalSize);
4945 assert( cellbody>0 );
4946 assert( pPage->nFree >= 2*nCell );
4947 pPage->nFree -= 2*nCell;
4948 for(i=0; i<nCell; i++){
4949 put2byte(&data[cellptr], cellbody);
4950 memcpy(&data[cellbody], apCell[i], aSize[i]);
4951 cellptr += 2;
4952 cellbody += aSize[i];
4953 }
4954 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00004955 }
drhf49661a2008-12-10 16:45:50 +00004956 pPage->nCell = (u16)nCell;
drhfa1a98a2004-05-14 19:08:17 +00004957}
4958
drh14acc042001-06-10 19:56:58 +00004959/*
drhc3b70572003-01-04 19:44:07 +00004960** The following parameters determine how many adjacent pages get involved
4961** in a balancing operation. NN is the number of neighbors on either side
4962** of the page that participate in the balancing operation. NB is the
4963** total number of pages that participate, including the target page and
4964** NN neighbors on either side.
4965**
4966** The minimum value of NN is 1 (of course). Increasing NN above 1
4967** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4968** in exchange for a larger degradation in INSERT and UPDATE performance.
4969** The value of NN appears to give the best results overall.
4970*/
4971#define NN 1 /* Number of neighbors on either side of pPage */
4972#define NB (NN*2+1) /* Total pages involved in the balance */
4973
drh43605152004-05-29 21:46:49 +00004974/* Forward reference */
danielk197771d5d2c2008-09-29 11:49:47 +00004975static int balance(BtCursor*, int);
danielk1977ac245ec2005-01-14 13:50:11 +00004976
drh615ae552005-01-16 23:21:00 +00004977#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004978/*
4979** This version of balance() handles the common special case where
4980** a new entry is being inserted on the extreme right-end of the
4981** tree, in other words, when the new entry will become the largest
4982** entry in the tree.
4983**
4984** Instead of trying balance the 3 right-most leaf pages, just add
4985** a new page to the right-hand side and put the one new entry in
4986** that page. This leaves the right side of the tree somewhat
4987** unbalanced. But odds are that we will be inserting new entries
4988** at the end soon afterwards so the nearly empty page will quickly
4989** fill up. On average.
4990**
4991** pPage is the leaf page which is the right-most page in the tree.
4992** pParent is its parent. pPage must have a single overflow entry
4993** which is also the right-most entry on the page.
4994*/
danielk197771d5d2c2008-09-29 11:49:47 +00004995static int balance_quick(BtCursor *pCur){
danielk1977ac245ec2005-01-14 13:50:11 +00004996 int rc;
danielk1977eaa06f62008-09-18 17:34:44 +00004997 MemPage *pNew = 0;
danielk1977ac245ec2005-01-14 13:50:11 +00004998 Pgno pgnoNew;
4999 u8 *pCell;
drha9121e42008-02-19 14:59:35 +00005000 u16 szCell;
danielk1977ac245ec2005-01-14 13:50:11 +00005001 CellInfo info;
danielk197771d5d2c2008-09-29 11:49:47 +00005002 MemPage *pPage = pCur->apPage[pCur->iPage];
5003 MemPage *pParent = pCur->apPage[pCur->iPage-1];
danielk1977aef0bf62005-12-30 16:28:01 +00005004 BtShared *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00005005 int parentIdx = pParent->nCell; /* pParent new divider cell index */
5006 int parentSize; /* Size of new divider cell */
5007 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00005008
drh1fee73e2007-08-29 04:00:57 +00005009 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00005010
danielk1977ac245ec2005-01-14 13:50:11 +00005011 /* Allocate a new page. Insert the overflow cell from pPage
5012 ** into it. Then remove the overflow cell from pPage.
5013 */
drh4f0c5872007-03-26 22:05:01 +00005014 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977eaa06f62008-09-18 17:34:44 +00005015 if( rc==SQLITE_OK ){
5016 pCell = pPage->aOvfl[0].pCell;
5017 szCell = cellSizePtr(pPage, pCell);
drhc5053fb2008-11-27 02:22:10 +00005018 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
danielk1977eaa06f62008-09-18 17:34:44 +00005019 zeroPage(pNew, pPage->aData[0]);
5020 assemblePage(pNew, 1, &pCell, &szCell);
5021 pPage->nOverflow = 0;
5022
danielk1977eaa06f62008-09-18 17:34:44 +00005023 /* pPage is currently the right-child of pParent. Change this
5024 ** so that the right-child is the new page allocated above and
5025 ** pPage is the next-to-right child.
5026 **
5027 ** Ignore the return value of the call to fillInCell(). fillInCell()
5028 ** may only return other than SQLITE_OK if it is required to allocate
5029 ** one or more overflow pages. Since an internal table B-Tree cell
5030 ** may never spill over onto an overflow page (it is a maximum of
5031 ** 13 bytes in size), it is not neccessary to check the return code.
5032 **
5033 ** Similarly, the insertCell() function cannot fail if the page
5034 ** being inserted into is already writable and the cell does not
5035 ** contain an overflow pointer. So ignore this return code too.
5036 */
5037 assert( pPage->nCell>0 );
5038 pCell = findCell(pPage, pPage->nCell-1);
5039 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
5040 fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
5041 assert( parentSize<64 );
5042 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
5043 insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
5044 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
5045 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
5046
5047 /* If this is an auto-vacuum database, update the pointer map
5048 ** with entries for the new page, and any pointer from the
5049 ** cell on the page to an overflow page.
5050 */
5051 if( ISAUTOVACUUM ){
5052 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
5053 if( rc==SQLITE_OK ){
5054 rc = ptrmapPutOvfl(pNew, 0);
5055 }
danielk1977ac11ee62005-01-15 12:45:51 +00005056 }
danielk1977e08a3c42008-09-18 18:17:03 +00005057
5058 /* Release the reference to the new page. */
5059 releasePage(pNew);
danielk1977ac11ee62005-01-15 12:45:51 +00005060 }
5061
danielk1977eaa06f62008-09-18 17:34:44 +00005062 /* At this point the pPage->nFree variable is not set correctly with
5063 ** respect to the content of the page (because it was set to 0 by
5064 ** insertCell). So call sqlite3BtreeInitPage() to make sure it is
5065 ** correct.
5066 **
5067 ** This has to be done even if an error will be returned. Normally, if
5068 ** an error occurs during tree balancing, the contents of MemPage are
5069 ** not important, as they will be recalculated when the page is rolled
5070 ** back. But here, in balance_quick(), it is possible that pPage has
5071 ** not yet been marked dirty or written into the journal file. Therefore
5072 ** it will not be rolled back and so it is important to make sure that
5073 ** the page data and contents of MemPage are consistent.
5074 */
5075 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005076 sqlite3BtreeInitPage(pPage);
danielk1977a4124bd2008-12-23 10:37:47 +00005077 assert( pPage->nOverflow==0 );
danielk1977eaa06f62008-09-18 17:34:44 +00005078
danielk1977e08a3c42008-09-18 18:17:03 +00005079 /* If everything else succeeded, balance the parent page, in
5080 ** case the divider cell inserted caused it to become overfull.
danielk197779a40da2005-01-16 08:00:01 +00005081 */
danielk1977eaa06f62008-09-18 17:34:44 +00005082 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00005083 releasePage(pPage);
5084 pCur->iPage--;
5085 rc = balance(pCur, 0);
danielk1977eaa06f62008-09-18 17:34:44 +00005086 }
5087 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00005088}
drh615ae552005-01-16 23:21:00 +00005089#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00005090
drhc3b70572003-01-04 19:44:07 +00005091/*
drhab01f612004-05-22 02:55:23 +00005092** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00005093** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00005094** Usually NN siblings on either side of pPage is used in the balancing,
5095** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00005096** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00005097** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00005098** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00005099**
drh0c6cc4e2004-06-15 02:13:26 +00005100** The number of siblings of pPage might be increased or decreased by one or
5101** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00005102** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00005103** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00005104** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00005105** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00005106**
drh8b2f49b2001-06-08 00:21:52 +00005107** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00005108** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00005109** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00005110** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00005111**
drh8c42ca92001-06-22 19:15:00 +00005112** In the course of balancing the siblings of pPage, the parent of pPage
5113** might become overfull or underfull. If that happens, then this routine
5114** is called recursively on the parent.
5115**
drh5e00f6c2001-09-13 13:46:56 +00005116** If this routine fails for any reason, it might leave the database
5117** in a corrupted state. So if this routine fails, the database should
5118** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00005119*/
danielk197771d5d2c2008-09-29 11:49:47 +00005120static int balance_nonroot(BtCursor *pCur){
5121 MemPage *pPage; /* The over or underfull page to balance */
drh8b2f49b2001-06-08 00:21:52 +00005122 MemPage *pParent; /* The parent of pPage */
drh16a9b832007-05-05 18:39:25 +00005123 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:07 +00005124 int nCell = 0; /* Number of cells in apCell[] */
5125 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
danielk1977a4124bd2008-12-23 10:37:47 +00005126 int nOld = 0; /* Number of pages in apOld[] */
5127 int nNew = 0; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00005128 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00005129 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00005130 int idx; /* Index of pPage in pParent->aCell[] */
5131 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00005132 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00005133 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00005134 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00005135 int usableSpace; /* Bytes in pPage beyond the header */
5136 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00005137 int subtotal; /* Subtotal of bytes in cells on one page */
drhe5ae5732008-06-15 02:51:47 +00005138 int iSpace1 = 0; /* First unused byte of aSpace1[] */
5139 int iSpace2 = 0; /* First unused byte of aSpace2[] */
drhfacf0302008-06-17 15:12:00 +00005140 int szScratch; /* Size of scratch memory requested */
drhc3b70572003-01-04 19:44:07 +00005141 MemPage *apOld[NB]; /* pPage and up to two siblings */
5142 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00005143 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00005144 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
5145 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drh4b70f112004-05-02 21:12:19 +00005146 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00005147 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
5148 int szNew[NB+2]; /* Combined size of cells place on i-th page */
danielk197750f059b2005-03-29 02:54:03 +00005149 u8 **apCell = 0; /* All cells begin balanced */
drha9121e42008-02-19 14:59:35 +00005150 u16 *szCell; /* Local size of all cells in apCell[] */
drhe5ae5732008-06-15 02:51:47 +00005151 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
5152 u8 *aSpace1; /* Space for copies of dividers cells before balance */
5153 u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */
danielk1977ac11ee62005-01-15 12:45:51 +00005154 u8 *aFrom = 0;
drh8b2f49b2001-06-08 00:21:52 +00005155
danielk197771d5d2c2008-09-29 11:49:47 +00005156 pPage = pCur->apPage[pCur->iPage];
drh1fee73e2007-08-29 04:00:57 +00005157 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf94a1732008-09-30 17:18:17 +00005158 VVA_ONLY( pCur->pagesShuffled = 1 );
drhd677b3d2007-08-20 22:48:41 +00005159
drh14acc042001-06-10 19:56:58 +00005160 /*
drh43605152004-05-29 21:46:49 +00005161 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00005162 */
danielk197771d5d2c2008-09-29 11:49:47 +00005163 assert( pCur->iPage>0 );
5164 assert( pPage->isInit );
danielk19776e465eb2007-08-21 13:11:00 +00005165 assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
drh4b70f112004-05-02 21:12:19 +00005166 pBt = pPage->pBt;
danielk197771d5d2c2008-09-29 11:49:47 +00005167 pParent = pCur->apPage[pCur->iPage-1];
drh43605152004-05-29 21:46:49 +00005168 assert( pParent );
danielk19773b8a05f2007-03-19 17:44:26 +00005169 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
danielk1977a4124bd2008-12-23 10:37:47 +00005170 goto balance_cleanup;
danielk197707cb5602006-01-20 10:55:05 +00005171 }
danielk1977474b7cc2008-07-09 11:49:46 +00005172
drh43605152004-05-29 21:46:49 +00005173 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00005174
drh615ae552005-01-16 23:21:00 +00005175#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00005176 /*
5177 ** A special case: If a new entry has just been inserted into a
5178 ** table (that is, a btree with integer keys and all data at the leaves)
drh09d0deb2005-08-02 17:13:09 +00005179 ** and the new entry is the right-most entry in the tree (it has the
drhf222e712005-01-14 22:55:49 +00005180 ** largest key) then use the special balance_quick() routine for
5181 ** balancing. balance_quick() is much faster and results in a tighter
5182 ** packing of data in the common case.
5183 */
danielk1977ac245ec2005-01-14 13:50:11 +00005184 if( pPage->leaf &&
5185 pPage->intKey &&
danielk1977ac245ec2005-01-14 13:50:11 +00005186 pPage->nOverflow==1 &&
5187 pPage->aOvfl[0].idx==pPage->nCell &&
danielk197771d5d2c2008-09-29 11:49:47 +00005188 pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00005189 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
5190 ){
drh44845222008-07-17 18:39:57 +00005191 assert( pPage->intKey );
danielk1977ac11ee62005-01-15 12:45:51 +00005192 /*
5193 ** TODO: Check the siblings to the left of pPage. It may be that
5194 ** they are not full and no new page is required.
5195 */
danielk197771d5d2c2008-09-29 11:49:47 +00005196 return balance_quick(pCur);
danielk1977ac245ec2005-01-14 13:50:11 +00005197 }
5198#endif
5199
danielk19776e465eb2007-08-21 13:11:00 +00005200 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
danielk1977a4124bd2008-12-23 10:37:47 +00005201 goto balance_cleanup;
danielk19776e465eb2007-08-21 13:11:00 +00005202 }
5203
drh2e38c322004-09-03 18:38:44 +00005204 /*
drh4b70f112004-05-02 21:12:19 +00005205 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00005206 ** to pPage. The "idx" variable is the index of that cell. If pPage
5207 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00005208 */
danielk1977bf93c562008-09-29 15:53:25 +00005209 idx = pCur->aiIdx[pCur->iPage-1];
5210 assertParentIndex(pParent, idx, pPage->pgno);
drh8b2f49b2001-06-08 00:21:52 +00005211
5212 /*
drh4b70f112004-05-02 21:12:19 +00005213 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00005214 ** the siblings. An attempt is made to find NN siblings on either
5215 ** side of pPage. More siblings are taken from one side, however, if
5216 ** pPage there are fewer than NN siblings on the other side. If pParent
5217 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00005218 */
drhc3b70572003-01-04 19:44:07 +00005219 nxDiv = idx - NN;
5220 if( nxDiv + NB > pParent->nCell ){
5221 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00005222 }
drhc3b70572003-01-04 19:44:07 +00005223 if( nxDiv<0 ){
5224 nxDiv = 0;
5225 }
drh8b2f49b2001-06-08 00:21:52 +00005226 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00005227 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00005228 if( k<pParent->nCell ){
danielk19771cc5ed82007-05-16 17:28:43 +00005229 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00005230 nDiv++;
drha34b6762004-05-07 13:30:42 +00005231 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00005232 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00005233 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00005234 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00005235 }else{
5236 break;
drh8b2f49b2001-06-08 00:21:52 +00005237 }
danielk197771d5d2c2008-09-29 11:49:47 +00005238 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]);
drh6019e162001-07-02 17:51:45 +00005239 if( rc ) goto balance_cleanup;
danielk197771d5d2c2008-09-29 11:49:47 +00005240 /* apOld[i]->idxParent = k; */
drh91025292004-05-03 19:49:32 +00005241 apCopy[i] = 0;
5242 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00005243 nOld++;
danielk1977634f2982005-03-28 08:44:07 +00005244 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
drh8b2f49b2001-06-08 00:21:52 +00005245 }
5246
drha9121e42008-02-19 14:59:35 +00005247 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
drh8d97f1f2005-05-05 18:14:13 +00005248 ** alignment */
drha9121e42008-02-19 14:59:35 +00005249 nMaxCells = (nMaxCells + 3)&~3;
drh8d97f1f2005-05-05 18:14:13 +00005250
drh8b2f49b2001-06-08 00:21:52 +00005251 /*
danielk1977634f2982005-03-28 08:44:07 +00005252 ** Allocate space for memory structures
5253 */
drhfacf0302008-06-17 15:12:00 +00005254 szScratch =
drha9121e42008-02-19 14:59:35 +00005255 nMaxCells*sizeof(u8*) /* apCell */
5256 + nMaxCells*sizeof(u16) /* szCell */
5257 + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */
drhe5ae5732008-06-15 02:51:47 +00005258 + pBt->pageSize /* aSpace1 */
drhfacf0302008-06-17 15:12:00 +00005259 + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */
5260 apCell = sqlite3ScratchMalloc( szScratch );
danielk1977634f2982005-03-28 08:44:07 +00005261 if( apCell==0 ){
5262 rc = SQLITE_NOMEM;
5263 goto balance_cleanup;
5264 }
drha9121e42008-02-19 14:59:35 +00005265 szCell = (u16*)&apCell[nMaxCells];
danielk1977634f2982005-03-28 08:44:07 +00005266 aCopy[0] = (u8*)&szCell[nMaxCells];
drh66e80082008-12-16 13:46:29 +00005267 assert( ((aCopy[0] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00005268 for(i=1; i<NB; i++){
drhc96d8532005-05-03 12:30:33 +00005269 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
drh66e80082008-12-16 13:46:29 +00005270 assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00005271 }
drhe5ae5732008-06-15 02:51:47 +00005272 aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
drh66e80082008-12-16 13:46:29 +00005273 assert( ((aSpace1 - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk197785d90ca2008-07-19 14:25:15 +00005274 if( ISAUTOVACUUM ){
drhe5ae5732008-06-15 02:51:47 +00005275 aFrom = &aSpace1[pBt->pageSize];
danielk1977634f2982005-03-28 08:44:07 +00005276 }
drhfacf0302008-06-17 15:12:00 +00005277 aSpace2 = sqlite3PageMalloc(pBt->pageSize);
drhe5ae5732008-06-15 02:51:47 +00005278 if( aSpace2==0 ){
5279 rc = SQLITE_NOMEM;
5280 goto balance_cleanup;
5281 }
danielk1977634f2982005-03-28 08:44:07 +00005282
5283 /*
drh14acc042001-06-10 19:56:58 +00005284 ** Make copies of the content of pPage and its siblings into aOld[].
5285 ** The rest of this function will use data from the copies rather
5286 ** that the original pages since the original pages will be in the
5287 ** process of being overwritten.
5288 */
5289 for(i=0; i<nOld; i++){
drhbf4bca52007-09-06 22:19:14 +00005290 MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
5291 memcpy(p, apOld[i], sizeof(MemPage));
5292 p->aData = (void*)&p[1];
5293 memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
drh14acc042001-06-10 19:56:58 +00005294 }
5295
5296 /*
5297 ** Load pointers to all cells on sibling pages and the divider cells
5298 ** into the local apCell[] array. Make copies of the divider cells
drhe5ae5732008-06-15 02:51:47 +00005299 ** into space obtained form aSpace1[] and remove the the divider Cells
drhb6f41482004-05-14 01:58:11 +00005300 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00005301 **
5302 ** If the siblings are on leaf pages, then the child pointers of the
5303 ** divider cells are stripped from the cells before they are copied
drhe5ae5732008-06-15 02:51:47 +00005304 ** into aSpace1[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00005305 ** child pointers. If siblings are not leaves, then all cell in
5306 ** apCell[] include child pointers. Either way, all cells in apCell[]
5307 ** are alike.
drh96f5b762004-05-16 16:24:36 +00005308 **
5309 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5310 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00005311 */
5312 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00005313 leafCorrection = pPage->leaf*4;
drh44845222008-07-17 18:39:57 +00005314 leafData = pPage->hasData;
drh8b2f49b2001-06-08 00:21:52 +00005315 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00005316 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00005317 int limit = pOld->nCell+pOld->nOverflow;
5318 for(j=0; j<limit; j++){
danielk1977634f2982005-03-28 08:44:07 +00005319 assert( nCell<nMaxCells );
drh43605152004-05-29 21:46:49 +00005320 apCell[nCell] = findOverflowCell(pOld, j);
5321 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk197785d90ca2008-07-19 14:25:15 +00005322 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005323 int a;
drhf49661a2008-12-10 16:45:50 +00005324 aFrom[nCell] = (u8)i; assert( i>=0 && i<6 );
danielk1977ac11ee62005-01-15 12:45:51 +00005325 for(a=0; a<pOld->nOverflow; a++){
5326 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
5327 aFrom[nCell] = 0xFF;
5328 break;
5329 }
5330 }
5331 }
drh14acc042001-06-10 19:56:58 +00005332 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00005333 }
5334 if( i<nOld-1 ){
drha9121e42008-02-19 14:59:35 +00005335 u16 sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00005336 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00005337 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
5338 ** are duplicates of keys on the child pages. We need to remove
5339 ** the divider cells from pParent, but the dividers cells are not
5340 ** added to apCell[] because they are duplicates of child cells.
5341 */
drh8b18dd42004-05-12 19:18:15 +00005342 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00005343 }else{
drhb6f41482004-05-14 01:58:11 +00005344 u8 *pTemp;
danielk1977634f2982005-03-28 08:44:07 +00005345 assert( nCell<nMaxCells );
drhb6f41482004-05-14 01:58:11 +00005346 szCell[nCell] = sz;
drhe5ae5732008-06-15 02:51:47 +00005347 pTemp = &aSpace1[iSpace1];
5348 iSpace1 += sz;
5349 assert( sz<=pBt->pageSize/4 );
5350 assert( iSpace1<=pBt->pageSize );
drhb6f41482004-05-14 01:58:11 +00005351 memcpy(pTemp, apDiv[i], sz);
5352 apCell[nCell] = pTemp+leafCorrection;
danielk197785d90ca2008-07-19 14:25:15 +00005353 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005354 aFrom[nCell] = 0xFF;
5355 }
drhb6f41482004-05-14 01:58:11 +00005356 dropCell(pParent, nxDiv, sz);
drhf49661a2008-12-10 16:45:50 +00005357 assert( leafCorrection==0 || leafCorrection==4 );
5358 szCell[nCell] -= (u16)leafCorrection;
drh43605152004-05-29 21:46:49 +00005359 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00005360 if( !pOld->leaf ){
5361 assert( leafCorrection==0 );
5362 /* The right pointer of the child page pOld becomes the left
5363 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00005364 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00005365 }else{
5366 assert( leafCorrection==4 );
danielk197739c96042007-05-12 10:41:47 +00005367 if( szCell[nCell]<4 ){
5368 /* Do not allow any cells smaller than 4 bytes. */
5369 szCell[nCell] = 4;
5370 }
drh8b18dd42004-05-12 19:18:15 +00005371 }
5372 nCell++;
drh4b70f112004-05-02 21:12:19 +00005373 }
drh8b2f49b2001-06-08 00:21:52 +00005374 }
5375 }
5376
5377 /*
drh6019e162001-07-02 17:51:45 +00005378 ** Figure out the number of pages needed to hold all nCell cells.
5379 ** Store this number in "k". Also compute szNew[] which is the total
5380 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00005381 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00005382 ** cntNew[k] should equal nCell.
5383 **
drh96f5b762004-05-16 16:24:36 +00005384 ** Values computed by this block:
5385 **
5386 ** k: The total number of sibling pages
5387 ** szNew[i]: Spaced used on the i-th sibling page.
5388 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
5389 ** the right of the i-th sibling page.
5390 ** usableSpace: Number of bytes of space available on each sibling.
5391 **
drh8b2f49b2001-06-08 00:21:52 +00005392 */
drh43605152004-05-29 21:46:49 +00005393 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00005394 for(subtotal=k=i=0; i<nCell; i++){
danielk1977634f2982005-03-28 08:44:07 +00005395 assert( i<nMaxCells );
drh43605152004-05-29 21:46:49 +00005396 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00005397 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00005398 szNew[k] = subtotal - szCell[i];
5399 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00005400 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00005401 subtotal = 0;
5402 k++;
5403 }
5404 }
5405 szNew[k] = subtotal;
5406 cntNew[k] = nCell;
5407 k++;
drh96f5b762004-05-16 16:24:36 +00005408
5409 /*
5410 ** The packing computed by the previous block is biased toward the siblings
5411 ** on the left side. The left siblings are always nearly full, while the
5412 ** right-most sibling might be nearly empty. This block of code attempts
5413 ** to adjust the packing of siblings to get a better balance.
5414 **
5415 ** This adjustment is more than an optimization. The packing above might
5416 ** be so out of balance as to be illegal. For example, the right-most
5417 ** sibling might be completely empty. This adjustment is not optional.
5418 */
drh6019e162001-07-02 17:51:45 +00005419 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00005420 int szRight = szNew[i]; /* Size of sibling on the right */
5421 int szLeft = szNew[i-1]; /* Size of sibling on the left */
5422 int r; /* Index of right-most cell in left sibling */
5423 int d; /* Index of first cell to the left of right sibling */
5424
5425 r = cntNew[i-1] - 1;
5426 d = r + 1 - leafData;
danielk1977634f2982005-03-28 08:44:07 +00005427 assert( d<nMaxCells );
5428 assert( r<nMaxCells );
drh43605152004-05-29 21:46:49 +00005429 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
5430 szRight += szCell[d] + 2;
5431 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00005432 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00005433 r = cntNew[i-1] - 1;
5434 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00005435 }
drh96f5b762004-05-16 16:24:36 +00005436 szNew[i] = szRight;
5437 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00005438 }
drh09d0deb2005-08-02 17:13:09 +00005439
5440 /* Either we found one or more cells (cntnew[0])>0) or we are the
5441 ** a virtual root page. A virtual root page is when the real root
5442 ** page is page 1 and we are the only child of that page.
5443 */
5444 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
drh8b2f49b2001-06-08 00:21:52 +00005445
5446 /*
drh6b308672002-07-08 02:16:37 +00005447 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00005448 */
drh4b70f112004-05-02 21:12:19 +00005449 assert( pPage->pgno>1 );
5450 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00005451 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00005452 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00005453 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00005454 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00005455 pgnoNew[i] = pgnoOld[i];
5456 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00005457 rc = sqlite3PagerWrite(pNew->pDbPage);
drhf5345442007-04-09 12:45:02 +00005458 nNew++;
danielk197728129562005-01-11 10:25:06 +00005459 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00005460 }else{
drh7aa8f852006-03-28 00:24:44 +00005461 assert( i>0 );
drh4f0c5872007-03-26 22:05:01 +00005462 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00005463 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00005464 apNew[i] = pNew;
drhf5345442007-04-09 12:45:02 +00005465 nNew++;
drh6b308672002-07-08 02:16:37 +00005466 }
drh8b2f49b2001-06-08 00:21:52 +00005467 }
5468
danielk1977299b1872004-11-22 10:02:10 +00005469 /* Free any old pages that were not reused as new pages.
5470 */
5471 while( i<nOld ){
5472 rc = freePage(apOld[i]);
5473 if( rc ) goto balance_cleanup;
5474 releasePage(apOld[i]);
5475 apOld[i] = 0;
5476 i++;
5477 }
5478
drh8b2f49b2001-06-08 00:21:52 +00005479 /*
drhf9ffac92002-03-02 19:00:31 +00005480 ** Put the new pages in accending order. This helps to
5481 ** keep entries in the disk file in order so that a scan
5482 ** of the table is a linear scan through the file. That
5483 ** in turn helps the operating system to deliver pages
5484 ** from the disk more rapidly.
5485 **
5486 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00005487 ** n is never more than NB (a small constant), that should
5488 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00005489 **
drhc3b70572003-01-04 19:44:07 +00005490 ** When NB==3, this one optimization makes the database
5491 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00005492 */
5493 for(i=0; i<k-1; i++){
5494 int minV = pgnoNew[i];
5495 int minI = i;
5496 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00005497 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00005498 minI = j;
5499 minV = pgnoNew[j];
5500 }
5501 }
5502 if( minI>i ){
5503 int t;
5504 MemPage *pT;
5505 t = pgnoNew[i];
5506 pT = apNew[i];
5507 pgnoNew[i] = pgnoNew[minI];
5508 apNew[i] = apNew[minI];
5509 pgnoNew[minI] = t;
5510 apNew[minI] = pT;
5511 }
5512 }
drha2fce642004-06-05 00:01:44 +00005513 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00005514 pgnoOld[0],
5515 nOld>=2 ? pgnoOld[1] : 0,
5516 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00005517 pgnoNew[0], szNew[0],
5518 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
5519 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00005520 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
5521 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00005522
drhf9ffac92002-03-02 19:00:31 +00005523 /*
drh14acc042001-06-10 19:56:58 +00005524 ** Evenly distribute the data in apCell[] across the new pages.
5525 ** Insert divider cells into pParent as necessary.
5526 */
5527 j = 0;
5528 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00005529 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00005530 MemPage *pNew = apNew[i];
drh19642e52005-03-29 13:17:45 +00005531 assert( j<nMaxCells );
drh4b70f112004-05-02 21:12:19 +00005532 assert( pNew->pgno==pgnoNew[i] );
drh10131482008-07-11 03:34:09 +00005533 zeroPage(pNew, pageFlags);
drhfa1a98a2004-05-14 19:08:17 +00005534 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh09d0deb2005-08-02 17:13:09 +00005535 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
drh43605152004-05-29 21:46:49 +00005536 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00005537
danielk1977ac11ee62005-01-15 12:45:51 +00005538 /* If this is an auto-vacuum database, update the pointer map entries
5539 ** that point to the siblings that were rearranged. These can be: left
5540 ** children of cells, the right-child of the page, or overflow pages
5541 ** pointed to by cells.
5542 */
danielk197785d90ca2008-07-19 14:25:15 +00005543 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005544 for(k=j; k<cntNew[i]; k++){
danielk1977634f2982005-03-28 08:44:07 +00005545 assert( k<nMaxCells );
danielk1977ac11ee62005-01-15 12:45:51 +00005546 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00005547 rc = ptrmapPutOvfl(pNew, k-j);
danielk197787c52b52008-07-19 11:49:07 +00005548 if( rc==SQLITE_OK && leafCorrection==0 ){
5549 rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno);
5550 }
danielk197779a40da2005-01-16 08:00:01 +00005551 if( rc!=SQLITE_OK ){
5552 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005553 }
5554 }
5555 }
5556 }
danielk1977ac11ee62005-01-15 12:45:51 +00005557
5558 j = cntNew[i];
5559
5560 /* If the sibling page assembled above was not the right-most sibling,
5561 ** insert a divider cell into the parent page.
5562 */
drh14acc042001-06-10 19:56:58 +00005563 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00005564 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00005565 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00005566 int sz;
danielk1977634f2982005-03-28 08:44:07 +00005567
5568 assert( j<nMaxCells );
drh8b18dd42004-05-12 19:18:15 +00005569 pCell = apCell[j];
5570 sz = szCell[j] + leafCorrection;
drhe5ae5732008-06-15 02:51:47 +00005571 pTemp = &aSpace2[iSpace2];
drh4b70f112004-05-02 21:12:19 +00005572 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00005573 memcpy(&pNew->aData[8], pCell, 4);
danielk197785d90ca2008-07-19 14:25:15 +00005574 if( ISAUTOVACUUM
danielk197787c52b52008-07-19 11:49:07 +00005575 && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno)
5576 ){
5577 rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno);
5578 if( rc!=SQLITE_OK ){
5579 goto balance_cleanup;
5580 }
5581 }
drh8b18dd42004-05-12 19:18:15 +00005582 }else if( leafData ){
drhfd131da2007-08-07 17:13:03 +00005583 /* If the tree is a leaf-data tree, and the siblings are leaves,
danielk1977ac11ee62005-01-15 12:45:51 +00005584 ** then there is no divider cell in apCell[]. Instead, the divider
5585 ** cell consists of the integer key for the right-most cell of
5586 ** the sibling-page assembled above only.
5587 */
drh6f11bef2004-05-13 01:12:56 +00005588 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00005589 j--;
drh16a9b832007-05-05 18:39:25 +00005590 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
drhe5ae5732008-06-15 02:51:47 +00005591 pCell = pTemp;
drh20abac22009-01-28 20:21:17 +00005592 rc = fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
5593 if( rc!=SQLITE_OK ){
5594 goto balance_cleanup;
5595 }
drh8b18dd42004-05-12 19:18:15 +00005596 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00005597 }else{
5598 pCell -= 4;
danielk19774aeff622007-05-12 09:30:47 +00005599 /* Obscure case for non-leaf-data trees: If the cell at pCell was
drh85b623f2007-12-13 21:54:09 +00005600 ** previously stored on a leaf node, and its reported size was 4
danielk19774aeff622007-05-12 09:30:47 +00005601 ** bytes, then it may actually be smaller than this
5602 ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
drh85b623f2007-12-13 21:54:09 +00005603 ** any cell). But it is important to pass the correct size to
danielk19774aeff622007-05-12 09:30:47 +00005604 ** insertCell(), so reparse the cell now.
5605 **
5606 ** Note that this can never happen in an SQLite data file, as all
5607 ** cells are at least 4 bytes. It only happens in b-trees used
5608 ** to evaluate "IN (SELECT ...)" and similar clauses.
5609 */
5610 if( szCell[j]==4 ){
5611 assert(leafCorrection==4);
5612 sz = cellSizePtr(pParent, pCell);
5613 }
drh4b70f112004-05-02 21:12:19 +00005614 }
drhe5ae5732008-06-15 02:51:47 +00005615 iSpace2 += sz;
5616 assert( sz<=pBt->pageSize/4 );
5617 assert( iSpace2<=pBt->pageSize );
danielk1977a3ad5e72005-01-07 08:56:44 +00005618 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00005619 if( rc!=SQLITE_OK ) goto balance_cleanup;
drhc5053fb2008-11-27 02:22:10 +00005620 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
drh43605152004-05-29 21:46:49 +00005621 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk197785d90ca2008-07-19 14:25:15 +00005622
danielk1977ac11ee62005-01-15 12:45:51 +00005623 /* If this is an auto-vacuum database, and not a leaf-data tree,
5624 ** then update the pointer map with an entry for the overflow page
5625 ** that the cell just inserted points to (if any).
5626 */
danielk197785d90ca2008-07-19 14:25:15 +00005627 if( ISAUTOVACUUM && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00005628 rc = ptrmapPutOvfl(pParent, nxDiv);
5629 if( rc!=SQLITE_OK ){
5630 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005631 }
5632 }
drh14acc042001-06-10 19:56:58 +00005633 j++;
5634 nxDiv++;
5635 }
danielk197787c52b52008-07-19 11:49:07 +00005636
danielk197787c52b52008-07-19 11:49:07 +00005637 /* Set the pointer-map entry for the new sibling page. */
danielk197785d90ca2008-07-19 14:25:15 +00005638 if( ISAUTOVACUUM ){
danielk197787c52b52008-07-19 11:49:07 +00005639 rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
5640 if( rc!=SQLITE_OK ){
5641 goto balance_cleanup;
5642 }
5643 }
drh14acc042001-06-10 19:56:58 +00005644 }
drh6019e162001-07-02 17:51:45 +00005645 assert( j==nCell );
drh7aa8f852006-03-28 00:24:44 +00005646 assert( nOld>0 );
5647 assert( nNew>0 );
drh4b70f112004-05-02 21:12:19 +00005648 if( (pageFlags & PTF_LEAF)==0 ){
danielk197787c52b52008-07-19 11:49:07 +00005649 u8 *zChild = &apCopy[nOld-1]->aData[8];
5650 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
danielk197785d90ca2008-07-19 14:25:15 +00005651 if( ISAUTOVACUUM ){
danielk197787c52b52008-07-19 11:49:07 +00005652 rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno);
5653 if( rc!=SQLITE_OK ){
5654 goto balance_cleanup;
5655 }
5656 }
drh14acc042001-06-10 19:56:58 +00005657 }
drhc5053fb2008-11-27 02:22:10 +00005658 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
drh43605152004-05-29 21:46:49 +00005659 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00005660 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00005661 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00005662 }else{
5663 /* Right-most sibling is the left child of the first entry in pParent
5664 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00005665 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00005666 }
5667
5668 /*
drh3a4c1412004-05-09 20:40:11 +00005669 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00005670 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00005671 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00005672 */
danielk197771d5d2c2008-09-29 11:49:47 +00005673 assert( pParent->isInit );
drhfacf0302008-06-17 15:12:00 +00005674 sqlite3ScratchFree(apCell);
drhe5ae5732008-06-15 02:51:47 +00005675 apCell = 0;
danielk1977a4124bd2008-12-23 10:37:47 +00005676 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
5677 pPage->pgno, nOld, nNew, nCell));
5678 pPage->nOverflow = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005679 releasePage(pPage);
5680 pCur->iPage--;
5681 rc = balance(pCur, 0);
drhda200cc2004-05-09 11:51:38 +00005682
drh8b2f49b2001-06-08 00:21:52 +00005683 /*
drh14acc042001-06-10 19:56:58 +00005684 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00005685 */
drh14acc042001-06-10 19:56:58 +00005686balance_cleanup:
drhfacf0302008-06-17 15:12:00 +00005687 sqlite3PageFree(aSpace2);
5688 sqlite3ScratchFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00005689 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00005690 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00005691 }
drh14acc042001-06-10 19:56:58 +00005692 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00005693 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00005694 }
danielk1977a4124bd2008-12-23 10:37:47 +00005695 pCur->apPage[pCur->iPage]->nOverflow = 0;
danielk1977eaa06f62008-09-18 17:34:44 +00005696
drh8b2f49b2001-06-08 00:21:52 +00005697 return rc;
5698}
5699
5700/*
drh43605152004-05-29 21:46:49 +00005701** This routine is called for the root page of a btree when the root
5702** page contains no cells. This is an opportunity to make the tree
5703** shallower by one level.
5704*/
danielk197771d5d2c2008-09-29 11:49:47 +00005705static int balance_shallower(BtCursor *pCur){
5706 MemPage *pPage; /* Root page of B-Tree */
drh43605152004-05-29 21:46:49 +00005707 MemPage *pChild; /* The only child page of pPage */
5708 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00005709 int rc = SQLITE_OK; /* Return code from subprocedures */
danielk1977aef0bf62005-12-30 16:28:01 +00005710 BtShared *pBt; /* The main BTree structure */
drh2e38c322004-09-03 18:38:44 +00005711 int mxCellPerPage; /* Maximum number of cells per page */
5712 u8 **apCell; /* All cells from pages being balanced */
drha9121e42008-02-19 14:59:35 +00005713 u16 *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00005714
danielk197771d5d2c2008-09-29 11:49:47 +00005715 assert( pCur->iPage==0 );
5716 pPage = pCur->apPage[0];
5717
drh43605152004-05-29 21:46:49 +00005718 assert( pPage->nCell==0 );
drh1fee73e2007-08-29 04:00:57 +00005719 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh2e38c322004-09-03 18:38:44 +00005720 pBt = pPage->pBt;
5721 mxCellPerPage = MX_CELL(pBt);
drhe5ae5732008-06-15 02:51:47 +00005722 apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
drh2e38c322004-09-03 18:38:44 +00005723 if( apCell==0 ) return SQLITE_NOMEM;
drha9121e42008-02-19 14:59:35 +00005724 szCell = (u16*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00005725 if( pPage->leaf ){
5726 /* The table is completely empty */
5727 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
5728 }else{
5729 /* The root page is empty but has one child. Transfer the
5730 ** information from that one child into the root page if it
5731 ** will fit. This reduces the depth of the tree by one.
5732 **
5733 ** If the root page is page 1, it has less space available than
5734 ** its child (due to the 100 byte header that occurs at the beginning
5735 ** of the database fle), so it might not be able to hold all of the
5736 ** information currently contained in the child. If this is the
5737 ** case, then do not do the transfer. Leave page 1 empty except
5738 ** for the right-pointer to the child page. The child page becomes
5739 ** the virtual root of the tree.
5740 */
drhf94a1732008-09-30 17:18:17 +00005741 VVA_ONLY( pCur->pagesShuffled = 1 );
drh43605152004-05-29 21:46:49 +00005742 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5743 assert( pgnoChild>0 );
danielk197789d40042008-11-17 14:20:56 +00005744 assert( pgnoChild<=pagerPagecount(pPage->pBt) );
drh16a9b832007-05-05 18:39:25 +00005745 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
drh2e38c322004-09-03 18:38:44 +00005746 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005747 if( pPage->pgno==1 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005748 rc = sqlite3BtreeInitPage(pChild);
drh2e38c322004-09-03 18:38:44 +00005749 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005750 assert( pChild->nOverflow==0 );
5751 if( pChild->nFree>=100 ){
5752 /* The child information will fit on the root page, so do the
5753 ** copy */
5754 int i;
5755 zeroPage(pPage, pChild->aData[0]);
5756 for(i=0; i<pChild->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00005757 apCell[i] = findCell(pChild,i);
drh43605152004-05-29 21:46:49 +00005758 szCell[i] = cellSizePtr(pChild, apCell[i]);
5759 }
5760 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00005761 /* Copy the right-pointer of the child to the parent. */
drhc5053fb2008-11-27 02:22:10 +00005762 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977ae825582004-11-23 09:06:55 +00005763 put4byte(&pPage->aData[pPage->hdrOffset+8],
5764 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh9bf9e9c2008-12-05 20:01:43 +00005765 rc = freePage(pChild);
drh43605152004-05-29 21:46:49 +00005766 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5767 }else{
5768 /* The child has more information that will fit on the root.
5769 ** The tree is already balanced. Do nothing. */
5770 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5771 }
5772 }else{
5773 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5774 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005775 rc = sqlite3BtreeInitPage(pPage);
drh43605152004-05-29 21:46:49 +00005776 assert( rc==SQLITE_OK );
5777 freePage(pChild);
5778 TRACE(("BALANCE: transfer child %d into root %d\n",
5779 pChild->pgno, pPage->pgno));
5780 }
danielk1977ac11ee62005-01-15 12:45:51 +00005781 assert( pPage->nOverflow==0 );
shane831c3292008-11-10 17:14:58 +00005782#ifndef SQLITE_OMIT_AUTOVACUUM
drh9bf9e9c2008-12-05 20:01:43 +00005783 if( ISAUTOVACUUM && rc==SQLITE_OK ){
danielk197700a696d2008-09-29 16:41:31 +00005784 rc = setChildPtrmaps(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00005785 }
shane831c3292008-11-10 17:14:58 +00005786#endif
drh43605152004-05-29 21:46:49 +00005787 releasePage(pChild);
5788 }
drh2e38c322004-09-03 18:38:44 +00005789end_shallow_balance:
drh17435752007-08-16 04:30:38 +00005790 sqlite3_free(apCell);
drh2e38c322004-09-03 18:38:44 +00005791 return rc;
drh43605152004-05-29 21:46:49 +00005792}
5793
5794
5795/*
5796** The root page is overfull
5797**
5798** When this happens, Create a new child page and copy the
5799** contents of the root into the child. Then make the root
5800** page an empty page with rightChild pointing to the new
5801** child. Finally, call balance_internal() on the new child
5802** to cause it to split.
5803*/
danielk197771d5d2c2008-09-29 11:49:47 +00005804static int balance_deeper(BtCursor *pCur){
drh43605152004-05-29 21:46:49 +00005805 int rc; /* Return value from subprocedures */
danielk197771d5d2c2008-09-29 11:49:47 +00005806 MemPage *pPage; /* Pointer to the root page */
drh43605152004-05-29 21:46:49 +00005807 MemPage *pChild; /* Pointer to a new child page */
5808 Pgno pgnoChild; /* Page number of the new child page */
danielk1977aef0bf62005-12-30 16:28:01 +00005809 BtShared *pBt; /* The BTree */
drh43605152004-05-29 21:46:49 +00005810 int usableSize; /* Total usable size of a page */
5811 u8 *data; /* Content of the parent page */
5812 u8 *cdata; /* Content of the child page */
5813 int hdr; /* Offset to page header in parent */
drh281b21d2008-08-22 12:57:08 +00005814 int cbrk; /* Offset to content of first cell in parent */
drh43605152004-05-29 21:46:49 +00005815
danielk197771d5d2c2008-09-29 11:49:47 +00005816 assert( pCur->iPage==0 );
5817 assert( pCur->apPage[0]->nOverflow>0 );
5818
drhf94a1732008-09-30 17:18:17 +00005819 VVA_ONLY( pCur->pagesShuffled = 1 );
danielk197771d5d2c2008-09-29 11:49:47 +00005820 pPage = pCur->apPage[0];
drh43605152004-05-29 21:46:49 +00005821 pBt = pPage->pBt;
drh1fee73e2007-08-29 04:00:57 +00005822 assert( sqlite3_mutex_held(pBt->mutex) );
drhc5053fb2008-11-27 02:22:10 +00005823 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh4f0c5872007-03-26 22:05:01 +00005824 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00005825 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005826 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
drh43605152004-05-29 21:46:49 +00005827 usableSize = pBt->usableSize;
5828 data = pPage->aData;
5829 hdr = pPage->hdrOffset;
drh281b21d2008-08-22 12:57:08 +00005830 cbrk = get2byte(&data[hdr+5]);
drh43605152004-05-29 21:46:49 +00005831 cdata = pChild->aData;
5832 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
drh281b21d2008-08-22 12:57:08 +00005833 memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00005834
5835 assert( pChild->isInit==0 );
danielk197771d5d2c2008-09-29 11:49:47 +00005836 rc = sqlite3BtreeInitPage(pChild);
5837 if( rc==SQLITE_OK ){
5838 int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
5839 memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
5840 pChild->nOverflow = pPage->nOverflow;
5841 if( pChild->nOverflow ){
5842 pChild->nFree = 0;
5843 }
5844 assert( pChild->nCell==pPage->nCell );
drhc5053fb2008-11-27 02:22:10 +00005845 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +00005846 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5847 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5848 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
5849 if( ISAUTOVACUUM ){
danielk197771d5d2c2008-09-29 11:49:47 +00005850 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
shane831c3292008-11-10 17:14:58 +00005851#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771d5d2c2008-09-29 11:49:47 +00005852 if( rc==SQLITE_OK ){
danielk197700a696d2008-09-29 16:41:31 +00005853 rc = setChildPtrmaps(pChild);
danielk1977ac11ee62005-01-15 12:45:51 +00005854 }
drh30df0092008-12-23 15:58:06 +00005855 if( rc ){
5856 pChild->nOverflow = 0;
5857 }
shane831c3292008-11-10 17:14:58 +00005858#endif
danielk1977ac11ee62005-01-15 12:45:51 +00005859 }
danielk197787c52b52008-07-19 11:49:07 +00005860 }
danielk19776b456a22005-03-21 04:04:02 +00005861
danielk197771d5d2c2008-09-29 11:49:47 +00005862 if( rc==SQLITE_OK ){
5863 pCur->iPage++;
5864 pCur->apPage[1] = pChild;
danielk1977bf93c562008-09-29 15:53:25 +00005865 pCur->aiIdx[0] = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005866 rc = balance_nonroot(pCur);
5867 }else{
5868 releasePage(pChild);
5869 }
5870
drh43605152004-05-29 21:46:49 +00005871 return rc;
5872}
5873
5874/*
danielk197771d5d2c2008-09-29 11:49:47 +00005875** The page that pCur currently points to has just been modified in
5876** some way. This function figures out if this modification means the
5877** tree needs to be balanced, and if so calls the appropriate balancing
5878** routine.
5879**
5880** Parameter isInsert is true if a new cell was just inserted into the
5881** page, or false otherwise.
drh43605152004-05-29 21:46:49 +00005882*/
danielk197771d5d2c2008-09-29 11:49:47 +00005883static int balance(BtCursor *pCur, int isInsert){
drh43605152004-05-29 21:46:49 +00005884 int rc = SQLITE_OK;
danielk197771d5d2c2008-09-29 11:49:47 +00005885 MemPage *pPage = pCur->apPage[pCur->iPage];
5886
drh1fee73e2007-08-29 04:00:57 +00005887 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197771d5d2c2008-09-29 11:49:47 +00005888 if( pCur->iPage==0 ){
danielk19776e465eb2007-08-21 13:11:00 +00005889 rc = sqlite3PagerWrite(pPage->pDbPage);
5890 if( rc==SQLITE_OK && pPage->nOverflow>0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005891 rc = balance_deeper(pCur);
danielk1977a4124bd2008-12-23 10:37:47 +00005892 assert( pCur->apPage[0]==pPage );
drh9bf9e9c2008-12-05 20:01:43 +00005893 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
drh43605152004-05-29 21:46:49 +00005894 }
danielk1977687566d2004-11-02 12:56:41 +00005895 if( rc==SQLITE_OK && pPage->nCell==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005896 rc = balance_shallower(pCur);
danielk1977a4124bd2008-12-23 10:37:47 +00005897 assert( pCur->apPage[0]==pPage );
drh9bf9e9c2008-12-05 20:01:43 +00005898 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
drh43605152004-05-29 21:46:49 +00005899 }
5900 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00005901 if( pPage->nOverflow>0 ||
danielk197771d5d2c2008-09-29 11:49:47 +00005902 (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
5903 rc = balance_nonroot(pCur);
drh43605152004-05-29 21:46:49 +00005904 }
5905 }
5906 return rc;
5907}
5908
5909/*
drh8dcd7ca2004-08-08 19:43:29 +00005910** This routine checks all cursors that point to table pgnoRoot.
drh980b1a72006-08-16 16:42:48 +00005911** If any of those cursors were opened with wrFlag==0 in a different
5912** database connection (a database connection that shares the pager
5913** cache with the current connection) and that other connection
5914** is not in the ReadUncommmitted state, then this routine returns
5915** SQLITE_LOCKED.
danielk1977299b1872004-11-22 10:02:10 +00005916**
danielk19773588ceb2008-06-10 17:30:26 +00005917** As well as cursors with wrFlag==0, cursors with wrFlag==1 and
5918** isIncrblobHandle==1 are also considered 'read' cursors. Incremental
5919** blob cursors are used for both reading and writing.
5920**
5921** When pgnoRoot is the root page of an intkey table, this function is also
5922** responsible for invalidating incremental blob cursors when the table row
5923** on which they are opened is deleted or modified. Cursors are invalidated
5924** according to the following rules:
5925**
5926** 1) When BtreeClearTable() is called to completely delete the contents
5927** of a B-Tree table, pExclude is set to zero and parameter iRow is
5928** set to non-zero. In this case all incremental blob cursors open
5929** on the table rooted at pgnoRoot are invalidated.
5930**
5931** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to
5932** modify a table row via an SQL statement, pExclude is set to the
5933** write cursor used to do the modification and parameter iRow is set
5934** to the integer row id of the B-Tree entry being modified. Unless
5935** pExclude is itself an incremental blob cursor, then all incremental
5936** blob cursors open on row iRow of the B-Tree are invalidated.
5937**
5938** 3) If both pExclude and iRow are set to zero, no incremental blob
5939** cursors are invalidated.
drhf74b8d92002-09-01 23:20:45 +00005940*/
danielk19773588ceb2008-06-10 17:30:26 +00005941static int checkReadLocks(
5942 Btree *pBtree,
5943 Pgno pgnoRoot,
5944 BtCursor *pExclude,
5945 i64 iRow
5946){
danielk1977299b1872004-11-22 10:02:10 +00005947 BtCursor *p;
drh980b1a72006-08-16 16:42:48 +00005948 BtShared *pBt = pBtree->pBt;
drhe5fe6902007-12-07 18:55:28 +00005949 sqlite3 *db = pBtree->db;
drh1fee73e2007-08-29 04:00:57 +00005950 assert( sqlite3BtreeHoldsMutex(pBtree) );
danielk1977299b1872004-11-22 10:02:10 +00005951 for(p=pBt->pCursor; p; p=p->pNext){
drh980b1a72006-08-16 16:42:48 +00005952 if( p==pExclude ) continue;
drh980b1a72006-08-16 16:42:48 +00005953 if( p->pgnoRoot!=pgnoRoot ) continue;
danielk19773588ceb2008-06-10 17:30:26 +00005954#ifndef SQLITE_OMIT_INCRBLOB
5955 if( p->isIncrblobHandle && (
5956 (!pExclude && iRow)
5957 || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow)
5958 )){
5959 p->eState = CURSOR_INVALID;
5960 }
5961#endif
5962 if( p->eState!=CURSOR_VALID ) continue;
5963 if( p->wrFlag==0
5964#ifndef SQLITE_OMIT_INCRBLOB
5965 || p->isIncrblobHandle
5966#endif
5967 ){
drhe5fe6902007-12-07 18:55:28 +00005968 sqlite3 *dbOther = p->pBtree->db;
drh980b1a72006-08-16 16:42:48 +00005969 if( dbOther==0 ||
5970 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5971 return SQLITE_LOCKED;
5972 }
danielk1977299b1872004-11-22 10:02:10 +00005973 }
5974 }
drhf74b8d92002-09-01 23:20:45 +00005975 return SQLITE_OK;
5976}
5977
5978/*
drh3b7511c2001-05-26 13:15:44 +00005979** Insert a new record into the BTree. The key is given by (pKey,nKey)
5980** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00005981** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00005982** is left pointing at a random location.
5983**
5984** For an INTKEY table, only the nKey value of the key is used. pKey is
5985** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00005986*/
drh3aac2dd2004-04-26 14:10:20 +00005987int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00005988 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00005989 const void *pKey, i64 nKey, /* The key of the new record */
drhe4d90812007-03-29 05:51:49 +00005990 const void *pData, int nData, /* The data of the new record */
drhb026e052007-05-02 01:34:31 +00005991 int nZero, /* Number of extra 0 bytes to append to data */
drhe4d90812007-03-29 05:51:49 +00005992 int appendBias /* True if this is likely an append */
drh3b7511c2001-05-26 13:15:44 +00005993){
drh3b7511c2001-05-26 13:15:44 +00005994 int rc;
5995 int loc;
drh14acc042001-06-10 19:56:58 +00005996 int szNew;
danielk197771d5d2c2008-09-29 11:49:47 +00005997 int idx;
drh3b7511c2001-05-26 13:15:44 +00005998 MemPage *pPage;
drhd677b3d2007-08-20 22:48:41 +00005999 Btree *p = pCur->pBtree;
6000 BtShared *pBt = p->pBt;
drha34b6762004-05-07 13:30:42 +00006001 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00006002 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00006003
drh1fee73e2007-08-29 04:00:57 +00006004 assert( cursorHoldsMutex(pCur) );
drh64022502009-01-09 14:11:04 +00006005 assert( pBt->inTransaction==TRANS_WRITE );
drhf74b8d92002-09-01 23:20:45 +00006006 assert( !pBt->readOnly );
drh64022502009-01-09 14:11:04 +00006007 assert( pCur->wrFlag );
danielk19773588ceb2008-06-10 17:30:26 +00006008 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){
drhf74b8d92002-09-01 23:20:45 +00006009 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
6010 }
drhfb982642007-08-30 01:19:59 +00006011 if( pCur->eState==CURSOR_FAULT ){
6012 return pCur->skip;
6013 }
danielk1977da184232006-01-05 11:34:32 +00006014
6015 /* Save the positions of any other cursors open on this table */
danielk1977be51a652008-10-08 17:58:48 +00006016 sqlite3BtreeClearCursor(pCur);
danielk19772e94d4d2006-01-09 05:36:27 +00006017 if(
danielk19772e94d4d2006-01-09 05:36:27 +00006018 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
drhe63d9992008-08-13 19:11:48 +00006019 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
danielk19772e94d4d2006-01-09 05:36:27 +00006020 ){
danielk1977da184232006-01-05 11:34:32 +00006021 return rc;
6022 }
6023
danielk197771d5d2c2008-09-29 11:49:47 +00006024 pPage = pCur->apPage[pCur->iPage];
drh4a1c3802004-05-12 15:15:47 +00006025 assert( pPage->intKey || nKey>=0 );
drh44845222008-07-17 18:39:57 +00006026 assert( pPage->leaf || !pPage->intKey );
drh3a4c1412004-05-09 20:40:11 +00006027 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
6028 pCur->pgnoRoot, nKey, nData, pPage->pgno,
6029 loc==0 ? "overwrite" : "new entry"));
danielk197771d5d2c2008-09-29 11:49:47 +00006030 assert( pPage->isInit );
danielk197752ae7242008-03-25 14:24:56 +00006031 allocateTempSpace(pBt);
6032 newCell = pBt->pTmpSpace;
drh2e38c322004-09-03 18:38:44 +00006033 if( newCell==0 ) return SQLITE_NOMEM;
drhb026e052007-05-02 01:34:31 +00006034 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
drh2e38c322004-09-03 18:38:44 +00006035 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00006036 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00006037 assert( szNew<=MX_CELL_SIZE(pBt) );
danielk197771d5d2c2008-09-29 11:49:47 +00006038 idx = pCur->aiIdx[pCur->iPage];
danielk1977da184232006-01-05 11:34:32 +00006039 if( loc==0 && CURSOR_VALID==pCur->eState ){
drha9121e42008-02-19 14:59:35 +00006040 u16 szOld;
danielk197771d5d2c2008-09-29 11:49:47 +00006041 assert( idx<pPage->nCell );
danielk19776e465eb2007-08-21 13:11:00 +00006042 rc = sqlite3PagerWrite(pPage->pDbPage);
6043 if( rc ){
6044 goto end_insert;
6045 }
danielk197771d5d2c2008-09-29 11:49:47 +00006046 oldCell = findCell(pPage, idx);
drh4b70f112004-05-02 21:12:19 +00006047 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006048 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00006049 }
drh43605152004-05-29 21:46:49 +00006050 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00006051 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00006052 if( rc ) goto end_insert;
shane0af3f892008-11-12 04:55:34 +00006053 rc = dropCell(pPage, idx, szOld);
6054 if( rc!=SQLITE_OK ) {
6055 goto end_insert;
6056 }
drh7c717f72001-06-24 20:39:41 +00006057 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00006058 assert( pPage->leaf );
danielk197771d5d2c2008-09-29 11:49:47 +00006059 idx = ++pCur->aiIdx[pCur->iPage];
drh271efa52004-05-30 19:19:05 +00006060 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00006061 pCur->validNKey = 0;
drh14acc042001-06-10 19:56:58 +00006062 }else{
drh4b70f112004-05-02 21:12:19 +00006063 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00006064 }
danielk197771d5d2c2008-09-29 11:49:47 +00006065 rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
drh9bf9e9c2008-12-05 20:01:43 +00006066 if( rc==SQLITE_OK ){
6067 rc = balance(pCur, 1);
6068 }
6069
6070 /* Must make sure nOverflow is reset to zero even if the balance()
6071 ** fails. Internal data structure corruption will result otherwise. */
danielk1977a4124bd2008-12-23 10:37:47 +00006072 pCur->apPage[pCur->iPage]->nOverflow = 0;
drh9bf9e9c2008-12-05 20:01:43 +00006073
danielk1977299b1872004-11-22 10:02:10 +00006074 if( rc==SQLITE_OK ){
6075 moveToRoot(pCur);
6076 }
drh2e38c322004-09-03 18:38:44 +00006077end_insert:
drh5e2f8b92001-05-28 00:41:15 +00006078 return rc;
6079}
6080
6081/*
drh4b70f112004-05-02 21:12:19 +00006082** Delete the entry that the cursor is pointing to. The cursor
drhf94a1732008-09-30 17:18:17 +00006083** is left pointing at a arbitrary location.
drh3b7511c2001-05-26 13:15:44 +00006084*/
drh3aac2dd2004-04-26 14:10:20 +00006085int sqlite3BtreeDelete(BtCursor *pCur){
danielk197771d5d2c2008-09-29 11:49:47 +00006086 MemPage *pPage = pCur->apPage[pCur->iPage];
6087 int idx;
drh4b70f112004-05-02 21:12:19 +00006088 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00006089 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00006090 Pgno pgnoChild = 0;
drhd677b3d2007-08-20 22:48:41 +00006091 Btree *p = pCur->pBtree;
6092 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006093
drh1fee73e2007-08-29 04:00:57 +00006094 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00006095 assert( pPage->isInit );
drh64022502009-01-09 14:11:04 +00006096 assert( pBt->inTransaction==TRANS_WRITE );
drhf74b8d92002-09-01 23:20:45 +00006097 assert( !pBt->readOnly );
drhfb982642007-08-30 01:19:59 +00006098 if( pCur->eState==CURSOR_FAULT ){
6099 return pCur->skip;
6100 }
drh64022502009-01-09 14:11:04 +00006101 if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){
drhbd03cae2001-06-02 02:40:57 +00006102 return SQLITE_ERROR; /* The cursor is not pointing to anything */
6103 }
drh64022502009-01-09 14:11:04 +00006104 assert( pCur->wrFlag );
danielk19773588ceb2008-06-10 17:30:26 +00006105 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){
drhf74b8d92002-09-01 23:20:45 +00006106 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
6107 }
danielk1977da184232006-01-05 11:34:32 +00006108
6109 /* Restore the current cursor position (a no-op if the cursor is not in
6110 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
danielk19773b8a05f2007-03-19 17:44:26 +00006111 ** open on the same table. Then call sqlite3PagerWrite() on the page
danielk1977da184232006-01-05 11:34:32 +00006112 ** that the entry will be deleted from.
6113 */
6114 if(
drha3460582008-07-11 21:02:53 +00006115 (rc = restoreCursorPosition(pCur))!=0 ||
drhd1167392006-01-23 13:00:35 +00006116 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
danielk19773b8a05f2007-03-19 17:44:26 +00006117 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
danielk1977da184232006-01-05 11:34:32 +00006118 ){
6119 return rc;
6120 }
danielk1977e6efa742004-11-10 11:55:10 +00006121
drh85b623f2007-12-13 21:54:09 +00006122 /* Locate the cell within its page and leave pCell pointing to the
danielk1977e6efa742004-11-10 11:55:10 +00006123 ** data. The clearCell() call frees any overflow pages associated with the
6124 ** cell. The cell itself is still intact.
6125 */
danielk197771d5d2c2008-09-29 11:49:47 +00006126 idx = pCur->aiIdx[pCur->iPage];
6127 pCell = findCell(pPage, idx);
drh4b70f112004-05-02 21:12:19 +00006128 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006129 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00006130 }
danielk197728129562005-01-11 10:25:06 +00006131 rc = clearCell(pPage, pCell);
drhd677b3d2007-08-20 22:48:41 +00006132 if( rc ){
drhd677b3d2007-08-20 22:48:41 +00006133 return rc;
6134 }
danielk1977e6efa742004-11-10 11:55:10 +00006135
drh4b70f112004-05-02 21:12:19 +00006136 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00006137 /*
drh5e00f6c2001-09-13 13:46:56 +00006138 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00006139 ** do something we will leave a hole on an internal page.
6140 ** We have to fill the hole by moving in a cell from a leaf. The
6141 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00006142 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00006143 */
drh14acc042001-06-10 19:56:58 +00006144 BtCursor leafCur;
drh1bd10f82008-12-10 21:19:56 +00006145 MemPage *pLeafPage = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00006146
drh4b70f112004-05-02 21:12:19 +00006147 unsigned char *pNext;
danielk1977299b1872004-11-22 10:02:10 +00006148 int notUsed;
danielk19776b456a22005-03-21 04:04:02 +00006149 unsigned char *tempCell = 0;
drh44845222008-07-17 18:39:57 +00006150 assert( !pPage->intKey );
drh16a9b832007-05-05 18:39:25 +00006151 sqlite3BtreeGetTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00006152 rc = sqlite3BtreeNext(&leafCur, &notUsed);
danielk19776b456a22005-03-21 04:04:02 +00006153 if( rc==SQLITE_OK ){
danielk19772f78fc62008-09-30 09:31:45 +00006154 assert( leafCur.aiIdx[leafCur.iPage]==0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006155 pLeafPage = leafCur.apPage[leafCur.iPage];
danielk197771d5d2c2008-09-29 11:49:47 +00006156 rc = sqlite3PagerWrite(pLeafPage->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00006157 }
6158 if( rc==SQLITE_OK ){
danielk19772f78fc62008-09-30 09:31:45 +00006159 int leafCursorInvalid = 0;
drha9121e42008-02-19 14:59:35 +00006160 u16 szNext;
danielk19776b456a22005-03-21 04:04:02 +00006161 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
danielk197771d5d2c2008-09-29 11:49:47 +00006162 pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno));
6163 dropCell(pPage, idx, cellSizePtr(pPage, pCell));
danielk19772f78fc62008-09-30 09:31:45 +00006164 pNext = findCell(pLeafPage, 0);
danielk197771d5d2c2008-09-29 11:49:47 +00006165 szNext = cellSizePtr(pLeafPage, pNext);
danielk19776b456a22005-03-21 04:04:02 +00006166 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
danielk197752ae7242008-03-25 14:24:56 +00006167 allocateTempSpace(pBt);
6168 tempCell = pBt->pTmpSpace;
danielk19776b456a22005-03-21 04:04:02 +00006169 if( tempCell==0 ){
6170 rc = SQLITE_NOMEM;
6171 }
danielk19778ea1cfa2008-01-01 06:19:02 +00006172 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00006173 rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0);
danielk19778ea1cfa2008-01-01 06:19:02 +00006174 }
danielk19772f78fc62008-09-30 09:31:45 +00006175
drhf94a1732008-09-30 17:18:17 +00006176
6177 /* The "if" statement in the next code block is critical. The
6178 ** slightest error in that statement would allow SQLite to operate
6179 ** correctly most of the time but produce very rare failures. To
6180 ** guard against this, the following macros help to verify that
6181 ** the "if" statement is well tested.
6182 */
6183 testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3
6184 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6185 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3
6186 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6187 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1
6188 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6189 testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3
6190 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6191 testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3))
6192 && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 );
6193
6194
danielk19772f78fc62008-09-30 09:31:45 +00006195 if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) &&
6196 (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3)
6197 ){
drhf94a1732008-09-30 17:18:17 +00006198 /* This branch is taken if the internal node is now either overflowing
6199 ** or underfull and the leaf node will be underfull after the just cell
danielk19772f78fc62008-09-30 09:31:45 +00006200 ** copied to the internal node is deleted from it. This is a special
6201 ** case because the call to balance() to correct the internal node
6202 ** may change the tree structure and invalidate the contents of
6203 ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be
6204 ** used by the balance() required to correct the underfull leaf
6205 ** node.
6206 **
6207 ** The formula used in the expression above are based on facets of
6208 ** the SQLite file-format that do not change over time.
6209 */
drhf94a1732008-09-30 17:18:17 +00006210 testcase( pPage->nFree==pBt->usableSize*2/3+1 );
6211 testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 );
danielk19772f78fc62008-09-30 09:31:45 +00006212 leafCursorInvalid = 1;
6213 }
6214
danielk19778ea1cfa2008-01-01 06:19:02 +00006215 if( rc==SQLITE_OK ){
drhc5053fb2008-11-27 02:22:10 +00006216 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +00006217 put4byte(findOverflowCell(pPage, idx), pgnoChild);
drhf94a1732008-09-30 17:18:17 +00006218 VVA_ONLY( pCur->pagesShuffled = 0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006219 rc = balance(pCur, 0);
danielk19778ea1cfa2008-01-01 06:19:02 +00006220 }
danielk19772f78fc62008-09-30 09:31:45 +00006221
6222 if( rc==SQLITE_OK && leafCursorInvalid ){
6223 /* The leaf-node is now underfull and so the tree needs to be
6224 ** rebalanced. However, the balance() operation on the internal
6225 ** node above may have modified the structure of the B-Tree and
6226 ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[]
6227 ** may not be trusted.
6228 **
6229 ** It is not possible to copy the ancestry from pCur, as the same
6230 ** balance() call has invalidated the pCur->apPage[] and aiIdx[]
6231 ** arrays.
drh7b682802008-09-30 14:06:28 +00006232 **
6233 ** The call to saveCursorPosition() below internally saves the
6234 ** key that leafCur is currently pointing to. Currently, there
6235 ** are two copies of that key in the tree - one here on the leaf
6236 ** page and one on some internal node in the tree. The copy on
6237 ** the leaf node is always the next key in tree-order after the
6238 ** copy on the internal node. So, the call to sqlite3BtreeNext()
6239 ** calls restoreCursorPosition() to point the cursor to the copy
6240 ** stored on the internal node, then advances to the next entry,
6241 ** which happens to be the copy of the key on the internal node.
danielk1977a69fda22008-09-30 16:48:10 +00006242 ** Net effect: leafCur is pointing back to the duplicate cell
6243 ** that needs to be removed, and the leafCur.apPage[] and
6244 ** leafCur.aiIdx[] arrays are correct.
danielk19772f78fc62008-09-30 09:31:45 +00006245 */
drhf94a1732008-09-30 17:18:17 +00006246 VVA_ONLY( Pgno leafPgno = pLeafPage->pgno );
danielk19772f78fc62008-09-30 09:31:45 +00006247 rc = saveCursorPosition(&leafCur);
6248 if( rc==SQLITE_OK ){
6249 rc = sqlite3BtreeNext(&leafCur, &notUsed);
6250 }
6251 pLeafPage = leafCur.apPage[leafCur.iPage];
6252 assert( pLeafPage->pgno==leafPgno );
6253 assert( leafCur.aiIdx[leafCur.iPage]==0 );
6254 }
6255
danielk19770cd1bbd2008-11-26 07:25:52 +00006256 if( SQLITE_OK==rc
6257 && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage))
6258 ){
danielk19772f78fc62008-09-30 09:31:45 +00006259 dropCell(pLeafPage, 0, szNext);
drhf94a1732008-09-30 17:18:17 +00006260 VVA_ONLY( leafCur.pagesShuffled = 0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006261 rc = balance(&leafCur, 0);
drhf94a1732008-09-30 17:18:17 +00006262 assert( leafCursorInvalid || !leafCur.pagesShuffled
6263 || !pCur->pagesShuffled );
danielk19778ea1cfa2008-01-01 06:19:02 +00006264 }
danielk19776b456a22005-03-21 04:04:02 +00006265 }
drh16a9b832007-05-05 18:39:25 +00006266 sqlite3BtreeReleaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00006267 }else{
danielk1977299b1872004-11-22 10:02:10 +00006268 TRACE(("DELETE: table=%d delete from leaf %d\n",
6269 pCur->pgnoRoot, pPage->pgno));
shanedcc50b72008-11-13 18:29:50 +00006270 rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
6271 if( rc==SQLITE_OK ){
6272 rc = balance(pCur, 0);
6273 }
drh5e2f8b92001-05-28 00:41:15 +00006274 }
danielk19776b456a22005-03-21 04:04:02 +00006275 if( rc==SQLITE_OK ){
6276 moveToRoot(pCur);
6277 }
drh5e2f8b92001-05-28 00:41:15 +00006278 return rc;
drh3b7511c2001-05-26 13:15:44 +00006279}
drh8b2f49b2001-06-08 00:21:52 +00006280
6281/*
drhc6b52df2002-01-04 03:09:29 +00006282** Create a new BTree table. Write into *piTable the page
6283** number for the root page of the new table.
6284**
drhab01f612004-05-22 02:55:23 +00006285** The type of type is determined by the flags parameter. Only the
6286** following values of flags are currently in use. Other values for
6287** flags might not work:
6288**
6289** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
6290** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00006291*/
drhd677b3d2007-08-20 22:48:41 +00006292static int btreeCreateTable(Btree *p, int *piTable, int flags){
danielk1977aef0bf62005-12-30 16:28:01 +00006293 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006294 MemPage *pRoot;
6295 Pgno pgnoRoot;
6296 int rc;
drhd677b3d2007-08-20 22:48:41 +00006297
drh1fee73e2007-08-29 04:00:57 +00006298 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00006299 assert( pBt->inTransaction==TRANS_WRITE );
danielk197728129562005-01-11 10:25:06 +00006300 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00006301
danielk1977003ba062004-11-04 02:57:33 +00006302#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:01 +00006303 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drhd677b3d2007-08-20 22:48:41 +00006304 if( rc ){
6305 return rc;
6306 }
danielk1977003ba062004-11-04 02:57:33 +00006307#else
danielk1977687566d2004-11-02 12:56:41 +00006308 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00006309 Pgno pgnoMove; /* Move a page here to make room for the root-page */
6310 MemPage *pPageMove; /* The page to move to. */
6311
danielk197720713f32007-05-03 11:43:33 +00006312 /* Creating a new table may probably require moving an existing database
6313 ** to make room for the new tables root page. In case this page turns
6314 ** out to be an overflow page, delete all overflow page-map caches
6315 ** held by open cursors.
6316 */
danielk197792d4d7a2007-05-04 12:05:56 +00006317 invalidateAllOverflowCache(pBt);
danielk197720713f32007-05-03 11:43:33 +00006318
danielk1977003ba062004-11-04 02:57:33 +00006319 /* Read the value of meta[3] from the database to determine where the
6320 ** root page of the new table should go. meta[3] is the largest root-page
6321 ** created so far, so the new root-page is (meta[3]+1).
6322 */
danielk1977aef0bf62005-12-30 16:28:01 +00006323 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
drhd677b3d2007-08-20 22:48:41 +00006324 if( rc!=SQLITE_OK ){
6325 return rc;
6326 }
danielk1977003ba062004-11-04 02:57:33 +00006327 pgnoRoot++;
6328
danielk1977599fcba2004-11-08 07:13:13 +00006329 /* The new root-page may not be allocated on a pointer-map page, or the
6330 ** PENDING_BYTE page.
6331 */
drh72190432008-01-31 14:54:43 +00006332 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00006333 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00006334 pgnoRoot++;
6335 }
6336 assert( pgnoRoot>=3 );
6337
6338 /* Allocate a page. The page that currently resides at pgnoRoot will
6339 ** be moved to the allocated page (unless the allocated page happens
6340 ** to reside at pgnoRoot).
6341 */
drh4f0c5872007-03-26 22:05:01 +00006342 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00006343 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00006344 return rc;
6345 }
danielk1977003ba062004-11-04 02:57:33 +00006346
6347 if( pgnoMove!=pgnoRoot ){
danielk1977f35843b2007-04-07 15:03:17 +00006348 /* pgnoRoot is the page that will be used for the root-page of
6349 ** the new table (assuming an error did not occur). But we were
6350 ** allocated pgnoMove. If required (i.e. if it was not allocated
6351 ** by extending the file), the current page at position pgnoMove
6352 ** is already journaled.
6353 */
danielk1977003ba062004-11-04 02:57:33 +00006354 u8 eType;
6355 Pgno iPtrPage;
6356
6357 releasePage(pPageMove);
danielk1977f35843b2007-04-07 15:03:17 +00006358
6359 /* Move the page currently at pgnoRoot to pgnoMove. */
drh16a9b832007-05-05 18:39:25 +00006360 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00006361 if( rc!=SQLITE_OK ){
6362 return rc;
6363 }
6364 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drhccae6022005-02-26 17:31:26 +00006365 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00006366 releasePage(pRoot);
6367 return rc;
6368 }
drhccae6022005-02-26 17:31:26 +00006369 assert( eType!=PTRMAP_ROOTPAGE );
6370 assert( eType!=PTRMAP_FREEPAGE );
danielk19774c999992008-07-16 18:17:55 +00006371 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
danielk1977003ba062004-11-04 02:57:33 +00006372 releasePage(pRoot);
danielk1977f35843b2007-04-07 15:03:17 +00006373
6374 /* Obtain the page at pgnoRoot */
danielk1977003ba062004-11-04 02:57:33 +00006375 if( rc!=SQLITE_OK ){
6376 return rc;
6377 }
drh16a9b832007-05-05 18:39:25 +00006378 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00006379 if( rc!=SQLITE_OK ){
6380 return rc;
6381 }
danielk19773b8a05f2007-03-19 17:44:26 +00006382 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:33 +00006383 if( rc!=SQLITE_OK ){
6384 releasePage(pRoot);
6385 return rc;
6386 }
6387 }else{
6388 pRoot = pPageMove;
6389 }
6390
danielk197742741be2005-01-08 12:42:39 +00006391 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00006392 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
6393 if( rc ){
6394 releasePage(pRoot);
6395 return rc;
6396 }
danielk1977aef0bf62005-12-30 16:28:01 +00006397 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00006398 if( rc ){
6399 releasePage(pRoot);
6400 return rc;
6401 }
danielk197742741be2005-01-08 12:42:39 +00006402
danielk1977003ba062004-11-04 02:57:33 +00006403 }else{
drh4f0c5872007-03-26 22:05:01 +00006404 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00006405 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00006406 }
6407#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006408 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhde647132004-05-07 17:57:49 +00006409 zeroPage(pRoot, flags | PTF_LEAF);
danielk19773b8a05f2007-03-19 17:44:26 +00006410 sqlite3PagerUnref(pRoot->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00006411 *piTable = (int)pgnoRoot;
6412 return SQLITE_OK;
6413}
drhd677b3d2007-08-20 22:48:41 +00006414int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
6415 int rc;
6416 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006417 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006418 rc = btreeCreateTable(p, piTable, flags);
6419 sqlite3BtreeLeave(p);
6420 return rc;
6421}
drh8b2f49b2001-06-08 00:21:52 +00006422
6423/*
6424** Erase the given database page and all its children. Return
6425** the page to the freelist.
6426*/
drh4b70f112004-05-02 21:12:19 +00006427static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:01 +00006428 BtShared *pBt, /* The BTree that contains the table */
drh4b70f112004-05-02 21:12:19 +00006429 Pgno pgno, /* Page number to clear */
danielk1977c7af4842008-10-27 13:59:33 +00006430 int freePageFlag, /* Deallocate page if true */
6431 int *pnChange
drh4b70f112004-05-02 21:12:19 +00006432){
danielk19776b456a22005-03-21 04:04:02 +00006433 MemPage *pPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00006434 int rc;
drh4b70f112004-05-02 21:12:19 +00006435 unsigned char *pCell;
6436 int i;
drh8b2f49b2001-06-08 00:21:52 +00006437
drh1fee73e2007-08-29 04:00:57 +00006438 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197789d40042008-11-17 14:20:56 +00006439 if( pgno>pagerPagecount(pBt) ){
drh49285702005-09-17 15:20:26 +00006440 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00006441 }
6442
danielk197771d5d2c2008-09-29 11:49:47 +00006443 rc = getAndInitPage(pBt, pgno, &pPage);
danielk19776b456a22005-03-21 04:04:02 +00006444 if( rc ) goto cleardatabasepage_out;
drh4b70f112004-05-02 21:12:19 +00006445 for(i=0; i<pPage->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00006446 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00006447 if( !pPage->leaf ){
danielk197762c14b32008-11-19 09:05:26 +00006448 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
danielk19776b456a22005-03-21 04:04:02 +00006449 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006450 }
drh4b70f112004-05-02 21:12:19 +00006451 rc = clearCell(pPage, pCell);
danielk19776b456a22005-03-21 04:04:02 +00006452 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006453 }
drha34b6762004-05-07 13:30:42 +00006454 if( !pPage->leaf ){
danielk197762c14b32008-11-19 09:05:26 +00006455 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
danielk19776b456a22005-03-21 04:04:02 +00006456 if( rc ) goto cleardatabasepage_out;
danielk1977c7af4842008-10-27 13:59:33 +00006457 }else if( pnChange ){
6458 assert( pPage->intKey );
6459 *pnChange += pPage->nCell;
drh2aa679f2001-06-25 02:11:07 +00006460 }
6461 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00006462 rc = freePage(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00006463 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
drh3a4c1412004-05-09 20:40:11 +00006464 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00006465 }
danielk19776b456a22005-03-21 04:04:02 +00006466
6467cleardatabasepage_out:
drh4b70f112004-05-02 21:12:19 +00006468 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00006469 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006470}
6471
6472/*
drhab01f612004-05-22 02:55:23 +00006473** Delete all information from a single table in the database. iTable is
6474** the page number of the root of the table. After this routine returns,
6475** the root page is empty, but still exists.
6476**
6477** This routine will fail with SQLITE_LOCKED if there are any open
6478** read cursors on the table. Open write cursors are moved to the
6479** root of the table.
danielk1977c7af4842008-10-27 13:59:33 +00006480**
6481** If pnChange is not NULL, then table iTable must be an intkey table. The
6482** integer value pointed to by pnChange is incremented by the number of
6483** entries in the table.
drh8b2f49b2001-06-08 00:21:52 +00006484*/
danielk1977c7af4842008-10-27 13:59:33 +00006485int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
drh8b2f49b2001-06-08 00:21:52 +00006486 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00006487 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00006488 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006489 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00006490 assert( p->inTrans==TRANS_WRITE );
6491 if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006492 /* nothing to do */
6493 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
6494 /* nothing to do */
6495 }else{
danielk197762c14b32008-11-19 09:05:26 +00006496 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
drh8b2f49b2001-06-08 00:21:52 +00006497 }
drhd677b3d2007-08-20 22:48:41 +00006498 sqlite3BtreeLeave(p);
6499 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006500}
6501
6502/*
6503** Erase all information in a table and add the root of the table to
6504** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00006505** page 1) is never added to the freelist.
6506**
6507** This routine will fail with SQLITE_LOCKED if there are any open
6508** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00006509**
6510** If AUTOVACUUM is enabled and the page at iTable is not the last
6511** root page in the database file, then the last root page
6512** in the database file is moved into the slot formerly occupied by
6513** iTable and that last slot formerly occupied by the last root page
6514** is added to the freelist instead of iTable. In this say, all
6515** root pages are kept at the beginning of the database file, which
6516** is necessary for AUTOVACUUM to work right. *piMoved is set to the
6517** page number that used to be the last root page in the file before
6518** the move. If no page gets moved, *piMoved is set to 0.
6519** The last root page is recorded in meta[3] and the value of
6520** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00006521*/
danielk197789d40042008-11-17 14:20:56 +00006522static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00006523 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00006524 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00006525 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:04 +00006526
drh1fee73e2007-08-29 04:00:57 +00006527 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00006528 assert( p->inTrans==TRANS_WRITE );
danielk1977a0bf2652004-11-04 14:30:04 +00006529
danielk1977e6efa742004-11-10 11:55:10 +00006530 /* It is illegal to drop a table if any cursors are open on the
6531 ** database. This is because in auto-vacuum mode the backend may
6532 ** need to move another root-page to fill a gap left by the deleted
6533 ** root page. If an open cursor was using this page a problem would
6534 ** occur.
6535 */
6536 if( pBt->pCursor ){
6537 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00006538 }
danielk1977a0bf2652004-11-04 14:30:04 +00006539
drh16a9b832007-05-05 18:39:25 +00006540 rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
drh2aa679f2001-06-25 02:11:07 +00006541 if( rc ) return rc;
danielk1977c7af4842008-10-27 13:59:33 +00006542 rc = sqlite3BtreeClearTable(p, iTable, 0);
danielk19776b456a22005-03-21 04:04:02 +00006543 if( rc ){
6544 releasePage(pPage);
6545 return rc;
6546 }
danielk1977a0bf2652004-11-04 14:30:04 +00006547
drh205f48e2004-11-05 00:43:11 +00006548 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00006549
drh4b70f112004-05-02 21:12:19 +00006550 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00006551#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00006552 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00006553 releasePage(pPage);
6554#else
6555 if( pBt->autoVacuum ){
6556 Pgno maxRootPgno;
danielk1977aef0bf62005-12-30 16:28:01 +00006557 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006558 if( rc!=SQLITE_OK ){
6559 releasePage(pPage);
6560 return rc;
6561 }
6562
6563 if( iTable==maxRootPgno ){
6564 /* If the table being dropped is the table with the largest root-page
6565 ** number in the database, put the root page on the free list.
6566 */
6567 rc = freePage(pPage);
6568 releasePage(pPage);
6569 if( rc!=SQLITE_OK ){
6570 return rc;
6571 }
6572 }else{
6573 /* The table being dropped does not have the largest root-page
6574 ** number in the database. So move the page that does into the
6575 ** gap left by the deleted root-page.
6576 */
6577 MemPage *pMove;
6578 releasePage(pPage);
drh16a9b832007-05-05 18:39:25 +00006579 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006580 if( rc!=SQLITE_OK ){
6581 return rc;
6582 }
danielk19774c999992008-07-16 18:17:55 +00006583 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006584 releasePage(pMove);
6585 if( rc!=SQLITE_OK ){
6586 return rc;
6587 }
drh16a9b832007-05-05 18:39:25 +00006588 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006589 if( rc!=SQLITE_OK ){
6590 return rc;
6591 }
6592 rc = freePage(pMove);
6593 releasePage(pMove);
6594 if( rc!=SQLITE_OK ){
6595 return rc;
6596 }
6597 *piMoved = maxRootPgno;
6598 }
6599
danielk1977599fcba2004-11-08 07:13:13 +00006600 /* Set the new 'max-root-page' value in the database header. This
6601 ** is the old value less one, less one more if that happens to
6602 ** be a root-page number, less one again if that is the
6603 ** PENDING_BYTE_PAGE.
6604 */
danielk197787a6e732004-11-05 12:58:25 +00006605 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00006606 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
6607 maxRootPgno--;
6608 }
danielk1977266664d2006-02-10 08:24:21 +00006609 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00006610 maxRootPgno--;
6611 }
danielk1977599fcba2004-11-08 07:13:13 +00006612 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
6613
danielk1977aef0bf62005-12-30 16:28:01 +00006614 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006615 }else{
6616 rc = freePage(pPage);
6617 releasePage(pPage);
6618 }
6619#endif
drh2aa679f2001-06-25 02:11:07 +00006620 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00006621 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00006622 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00006623 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00006624 }
drh8b2f49b2001-06-08 00:21:52 +00006625 return rc;
6626}
drhd677b3d2007-08-20 22:48:41 +00006627int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
6628 int rc;
6629 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006630 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006631 rc = btreeDropTable(p, iTable, piMoved);
6632 sqlite3BtreeLeave(p);
6633 return rc;
6634}
drh8b2f49b2001-06-08 00:21:52 +00006635
drh001bbcb2003-03-19 03:14:00 +00006636
drh8b2f49b2001-06-08 00:21:52 +00006637/*
drh23e11ca2004-05-04 17:27:28 +00006638** Read the meta-information out of a database file. Meta[0]
6639** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00006640** through meta[15] are available for use by higher layers. Meta[0]
6641** is read-only, the others are read/write.
6642**
6643** The schema layer numbers meta values differently. At the schema
6644** layer (and the SetCookie and ReadCookie opcodes) the number of
6645** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00006646*/
danielk1977aef0bf62005-12-30 16:28:01 +00006647int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
drh1bd10f82008-12-10 21:19:56 +00006648 DbPage *pDbPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00006649 int rc;
drh4b70f112004-05-02 21:12:19 +00006650 unsigned char *pP1;
danielk1977aef0bf62005-12-30 16:28:01 +00006651 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006652
drhd677b3d2007-08-20 22:48:41 +00006653 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006654 pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006655
danielk1977da184232006-01-05 11:34:32 +00006656 /* Reading a meta-data value requires a read-lock on page 1 (and hence
6657 ** the sqlite_master table. We grab this lock regardless of whether or
6658 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
6659 ** 1 is treated as a special case by queryTableLock() and lockTable()).
6660 */
6661 rc = queryTableLock(p, 1, READ_LOCK);
6662 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006663 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006664 return rc;
6665 }
6666
drh23e11ca2004-05-04 17:27:28 +00006667 assert( idx>=0 && idx<=15 );
danielk1977d9f6c532008-09-19 16:39:38 +00006668 if( pBt->pPage1 ){
6669 /* The b-tree is already holding a reference to page 1 of the database
6670 ** file. In this case the required meta-data value can be read directly
6671 ** from the page data of this reference. This is slightly faster than
6672 ** requesting a new reference from the pager layer.
6673 */
6674 pP1 = (unsigned char *)pBt->pPage1->aData;
6675 }else{
6676 /* The b-tree does not have a reference to page 1 of the database file.
6677 ** Obtain one from the pager layer.
6678 */
danielk1977ea897302008-09-19 15:10:58 +00006679 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
6680 if( rc ){
6681 sqlite3BtreeLeave(p);
6682 return rc;
6683 }
6684 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
drhd677b3d2007-08-20 22:48:41 +00006685 }
drh23e11ca2004-05-04 17:27:28 +00006686 *pMeta = get4byte(&pP1[36 + idx*4]);
danielk1977ea897302008-09-19 15:10:58 +00006687
danielk1977d9f6c532008-09-19 16:39:38 +00006688 /* If the b-tree is not holding a reference to page 1, then one was
6689 ** requested from the pager layer in the above block. Release it now.
6690 */
danielk1977ea897302008-09-19 15:10:58 +00006691 if( !pBt->pPage1 ){
6692 sqlite3PagerUnref(pDbPage);
6693 }
drhae157872004-08-14 19:20:09 +00006694
danielk1977599fcba2004-11-08 07:13:13 +00006695 /* If autovacuumed is disabled in this build but we are trying to
6696 ** access an autovacuumed database, then make the database readonly.
6697 */
danielk1977003ba062004-11-04 02:57:33 +00006698#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00006699 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00006700#endif
drhae157872004-08-14 19:20:09 +00006701
danielk1977da184232006-01-05 11:34:32 +00006702 /* Grab the read-lock on page 1. */
6703 rc = lockTable(p, 1, READ_LOCK);
drhd677b3d2007-08-20 22:48:41 +00006704 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006705 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006706}
6707
6708/*
drh23e11ca2004-05-04 17:27:28 +00006709** Write meta-information back into the database. Meta[0] is
6710** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00006711*/
danielk1977aef0bf62005-12-30 16:28:01 +00006712int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
6713 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:19 +00006714 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00006715 int rc;
drh23e11ca2004-05-04 17:27:28 +00006716 assert( idx>=1 && idx<=15 );
drhd677b3d2007-08-20 22:48:41 +00006717 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006718 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00006719 assert( p->inTrans==TRANS_WRITE );
6720 assert( pBt->pPage1!=0 );
6721 pP1 = pBt->pPage1->aData;
6722 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6723 if( rc==SQLITE_OK ){
6724 put4byte(&pP1[36 + idx*4], iMeta);
danielk19774152e672007-09-12 17:01:45 +00006725#ifndef SQLITE_OMIT_AUTOVACUUM
drh64022502009-01-09 14:11:04 +00006726 if( idx==7 ){
6727 assert( pBt->autoVacuum || iMeta==0 );
6728 assert( iMeta==0 || iMeta==1 );
6729 pBt->incrVacuum = (u8)iMeta;
drhd677b3d2007-08-20 22:48:41 +00006730 }
drh64022502009-01-09 14:11:04 +00006731#endif
drh5df72a52002-06-06 23:16:05 +00006732 }
drhd677b3d2007-08-20 22:48:41 +00006733 sqlite3BtreeLeave(p);
6734 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006735}
drh8c42ca92001-06-22 19:15:00 +00006736
drhf328bc82004-05-10 23:29:49 +00006737/*
6738** Return the flag byte at the beginning of the page that the cursor
6739** is currently pointing to.
6740*/
6741int sqlite3BtreeFlags(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00006742 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
drha3460582008-07-11 21:02:53 +00006743 ** restoreCursorPosition() here.
danielk1977da184232006-01-05 11:34:32 +00006744 */
danielk1977e448dc42008-01-02 11:50:51 +00006745 MemPage *pPage;
drha3460582008-07-11 21:02:53 +00006746 restoreCursorPosition(pCur);
danielk197771d5d2c2008-09-29 11:49:47 +00006747 pPage = pCur->apPage[pCur->iPage];
drh1fee73e2007-08-29 04:00:57 +00006748 assert( cursorHoldsMutex(pCur) );
drh64022502009-01-09 14:11:04 +00006749 assert( pPage!=0 );
drhd0679ed2007-08-28 22:24:34 +00006750 assert( pPage->pBt==pCur->pBt );
drh64022502009-01-09 14:11:04 +00006751 return pPage->aData[pPage->hdrOffset];
drhf328bc82004-05-10 23:29:49 +00006752}
6753
drhdd793422001-06-28 01:54:48 +00006754
drhdd793422001-06-28 01:54:48 +00006755/*
drh5eddca62001-06-30 21:53:53 +00006756** Return the pager associated with a BTree. This routine is used for
6757** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00006758*/
danielk1977aef0bf62005-12-30 16:28:01 +00006759Pager *sqlite3BtreePager(Btree *p){
6760 return p->pBt->pPager;
drhdd793422001-06-28 01:54:48 +00006761}
drh5eddca62001-06-30 21:53:53 +00006762
drhb7f91642004-10-31 02:22:47 +00006763#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006764/*
6765** Append a message to the error message string.
6766*/
drh2e38c322004-09-03 18:38:44 +00006767static void checkAppendMsg(
6768 IntegrityCk *pCheck,
6769 char *zMsg1,
6770 const char *zFormat,
6771 ...
6772){
6773 va_list ap;
drh1dcdbc02007-01-27 02:24:54 +00006774 if( !pCheck->mxErr ) return;
6775 pCheck->mxErr--;
6776 pCheck->nErr++;
drh2e38c322004-09-03 18:38:44 +00006777 va_start(ap, zFormat);
drhf089aa42008-07-08 19:34:06 +00006778 if( pCheck->errMsg.nChar ){
6779 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
drh5eddca62001-06-30 21:53:53 +00006780 }
drhf089aa42008-07-08 19:34:06 +00006781 if( zMsg1 ){
6782 sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
6783 }
6784 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
6785 va_end(ap);
drhc890fec2008-08-01 20:10:08 +00006786 if( pCheck->errMsg.mallocFailed ){
6787 pCheck->mallocFailed = 1;
6788 }
drh5eddca62001-06-30 21:53:53 +00006789}
drhb7f91642004-10-31 02:22:47 +00006790#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006791
drhb7f91642004-10-31 02:22:47 +00006792#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006793/*
6794** Add 1 to the reference count for page iPage. If this is the second
6795** reference to the page, add an error message to pCheck->zErrMsg.
6796** Return 1 if there are 2 ore more references to the page and 0 if
6797** if this is the first reference to the page.
6798**
6799** Also check that the page number is in bounds.
6800*/
danielk197789d40042008-11-17 14:20:56 +00006801static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00006802 if( iPage==0 ) return 1;
danielk197789d40042008-11-17 14:20:56 +00006803 if( iPage>pCheck->nPage ){
drh2e38c322004-09-03 18:38:44 +00006804 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006805 return 1;
6806 }
6807 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00006808 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006809 return 1;
6810 }
6811 return (pCheck->anRef[iPage]++)>1;
6812}
6813
danielk1977afcdd022004-10-31 16:25:42 +00006814#ifndef SQLITE_OMIT_AUTOVACUUM
6815/*
6816** Check that the entry in the pointer-map for page iChild maps to
6817** page iParent, pointer type ptrType. If not, append an error message
6818** to pCheck.
6819*/
6820static void checkPtrmap(
6821 IntegrityCk *pCheck, /* Integrity check context */
6822 Pgno iChild, /* Child page number */
6823 u8 eType, /* Expected pointer map type */
6824 Pgno iParent, /* Expected pointer map parent page number */
6825 char *zContext /* Context description (used for error msg) */
6826){
6827 int rc;
6828 u8 ePtrmapType;
6829 Pgno iPtrmapParent;
6830
6831 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6832 if( rc!=SQLITE_OK ){
drhe43ba702008-12-05 22:40:08 +00006833 if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1;
danielk1977afcdd022004-10-31 16:25:42 +00006834 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6835 return;
6836 }
6837
6838 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6839 checkAppendMsg(pCheck, zContext,
6840 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6841 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6842 }
6843}
6844#endif
6845
drh5eddca62001-06-30 21:53:53 +00006846/*
6847** Check the integrity of the freelist or of an overflow page list.
6848** Verify that the number of pages on the list is N.
6849*/
drh30e58752002-03-02 20:41:57 +00006850static void checkList(
6851 IntegrityCk *pCheck, /* Integrity checking context */
6852 int isFreeList, /* True for a freelist. False for overflow page list */
6853 int iPage, /* Page number for first page in the list */
6854 int N, /* Expected number of pages in the list */
6855 char *zContext /* Context for error messages */
6856){
6857 int i;
drh3a4c1412004-05-09 20:40:11 +00006858 int expected = N;
6859 int iFirst = iPage;
drh1dcdbc02007-01-27 02:24:54 +00006860 while( N-- > 0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:26 +00006861 DbPage *pOvflPage;
6862 unsigned char *pOvflData;
drh5eddca62001-06-30 21:53:53 +00006863 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00006864 checkAppendMsg(pCheck, zContext,
6865 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00006866 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00006867 break;
6868 }
6869 if( checkRef(pCheck, iPage, zContext) ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006870 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
drh2e38c322004-09-03 18:38:44 +00006871 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006872 break;
6873 }
danielk19773b8a05f2007-03-19 17:44:26 +00006874 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:57 +00006875 if( isFreeList ){
danielk19773b8a05f2007-03-19 17:44:26 +00006876 int n = get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:41 +00006877#ifndef SQLITE_OMIT_AUTOVACUUM
6878 if( pCheck->pBt->autoVacuum ){
6879 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6880 }
6881#endif
drh45b1fac2008-07-04 17:52:42 +00006882 if( n>pCheck->pBt->usableSize/4-2 ){
drh2e38c322004-09-03 18:38:44 +00006883 checkAppendMsg(pCheck, zContext,
6884 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00006885 N--;
6886 }else{
6887 for(i=0; i<n; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006888 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:41 +00006889#ifndef SQLITE_OMIT_AUTOVACUUM
6890 if( pCheck->pBt->autoVacuum ){
6891 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6892 }
6893#endif
6894 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00006895 }
6896 N -= n;
drh30e58752002-03-02 20:41:57 +00006897 }
drh30e58752002-03-02 20:41:57 +00006898 }
danielk1977afcdd022004-10-31 16:25:42 +00006899#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006900 else{
6901 /* If this database supports auto-vacuum and iPage is not the last
6902 ** page in this overflow list, check that the pointer-map entry for
6903 ** the following page matches iPage.
6904 */
6905 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00006906 i = get4byte(pOvflData);
danielk1977687566d2004-11-02 12:56:41 +00006907 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6908 }
danielk1977afcdd022004-10-31 16:25:42 +00006909 }
6910#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006911 iPage = get4byte(pOvflData);
6912 sqlite3PagerUnref(pOvflPage);
drh5eddca62001-06-30 21:53:53 +00006913 }
6914}
drhb7f91642004-10-31 02:22:47 +00006915#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006916
drhb7f91642004-10-31 02:22:47 +00006917#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006918/*
6919** Do various sanity checks on a single page of a tree. Return
6920** the tree depth. Root pages return 0. Parents of root pages
6921** return 1, and so forth.
6922**
6923** These checks are done:
6924**
6925** 1. Make sure that cells and freeblocks do not overlap
6926** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00006927** NO 2. Make sure cell keys are in order.
6928** NO 3. Make sure no key is less than or equal to zLowerBound.
6929** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00006930** 5. Check the integrity of overflow pages.
6931** 6. Recursively call checkTreePage on all children.
6932** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00006933** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00006934** the root of the tree.
6935*/
6936static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00006937 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00006938 int iPage, /* Page number of the page to check */
drh74161702006-02-24 02:53:49 +00006939 char *zParentContext /* Parent context */
drh5eddca62001-06-30 21:53:53 +00006940){
6941 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00006942 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00006943 int hdr, cellStart;
6944 int nCell;
drhda200cc2004-05-09 11:51:38 +00006945 u8 *data;
danielk1977aef0bf62005-12-30 16:28:01 +00006946 BtShared *pBt;
drh4f26bb62005-09-08 14:17:20 +00006947 int usableSize;
drh5eddca62001-06-30 21:53:53 +00006948 char zContext[100];
shane0af3f892008-11-12 04:55:34 +00006949 char *hit = 0;
drh5eddca62001-06-30 21:53:53 +00006950
drh5bb3eb92007-05-04 13:15:55 +00006951 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
danielk1977ef73ee92004-11-06 12:26:07 +00006952
drh5eddca62001-06-30 21:53:53 +00006953 /* Check that the page exists
6954 */
drhd9cb6ac2005-10-20 07:28:17 +00006955 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00006956 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00006957 if( iPage==0 ) return 0;
6958 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh16a9b832007-05-05 18:39:25 +00006959 if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
drhe43ba702008-12-05 22:40:08 +00006960 if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1;
drh2e38c322004-09-03 18:38:44 +00006961 checkAppendMsg(pCheck, zContext,
6962 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00006963 return 0;
6964 }
danielk197771d5d2c2008-09-29 11:49:47 +00006965 if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){
drh64022502009-01-09 14:11:04 +00006966 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
drh16a9b832007-05-05 18:39:25 +00006967 checkAppendMsg(pCheck, zContext,
6968 "sqlite3BtreeInitPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00006969 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00006970 return 0;
6971 }
6972
6973 /* Check out all the cells.
6974 */
6975 depth = 0;
drh1dcdbc02007-01-27 02:24:54 +00006976 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
drh6f11bef2004-05-13 01:12:56 +00006977 u8 *pCell;
danielk197789d40042008-11-17 14:20:56 +00006978 u32 sz;
drh6f11bef2004-05-13 01:12:56 +00006979 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00006980
6981 /* Check payload overflow pages
6982 */
drh5bb3eb92007-05-04 13:15:55 +00006983 sqlite3_snprintf(sizeof(zContext), zContext,
6984 "On tree page %d cell %d: ", iPage, i);
danielk19771cc5ed82007-05-16 17:28:43 +00006985 pCell = findCell(pPage,i);
drh16a9b832007-05-05 18:39:25 +00006986 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006987 sz = info.nData;
drhf49661a2008-12-10 16:45:50 +00006988 if( !pPage->intKey ) sz += (int)info.nKey;
drh72365832007-03-06 15:53:44 +00006989 assert( sz==info.nPayload );
drh6f11bef2004-05-13 01:12:56 +00006990 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00006991 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00006992 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6993#ifndef SQLITE_OMIT_AUTOVACUUM
6994 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006995 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00006996 }
6997#endif
6998 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00006999 }
7000
7001 /* Check sanity of left child page.
7002 */
drhda200cc2004-05-09 11:51:38 +00007003 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00007004 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00007005#ifndef SQLITE_OMIT_AUTOVACUUM
7006 if( pBt->autoVacuum ){
7007 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
7008 }
7009#endif
danielk197762c14b32008-11-19 09:05:26 +00007010 d2 = checkTreePage(pCheck, pgno, zContext);
drhda200cc2004-05-09 11:51:38 +00007011 if( i>0 && d2!=depth ){
7012 checkAppendMsg(pCheck, zContext, "Child page depth differs");
7013 }
7014 depth = d2;
drh5eddca62001-06-30 21:53:53 +00007015 }
drh5eddca62001-06-30 21:53:53 +00007016 }
drhda200cc2004-05-09 11:51:38 +00007017 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00007018 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh5bb3eb92007-05-04 13:15:55 +00007019 sqlite3_snprintf(sizeof(zContext), zContext,
7020 "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00007021#ifndef SQLITE_OMIT_AUTOVACUUM
7022 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00007023 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00007024 }
7025#endif
danielk197762c14b32008-11-19 09:05:26 +00007026 checkTreePage(pCheck, pgno, zContext);
drhda200cc2004-05-09 11:51:38 +00007027 }
drh5eddca62001-06-30 21:53:53 +00007028
7029 /* Check for complete coverage of the page
7030 */
drhda200cc2004-05-09 11:51:38 +00007031 data = pPage->aData;
7032 hdr = pPage->hdrOffset;
drhf7141992008-06-19 00:16:08 +00007033 hit = sqlite3PageMalloc( pBt->pageSize );
drhc890fec2008-08-01 20:10:08 +00007034 if( hit==0 ){
7035 pCheck->mallocFailed = 1;
7036 }else{
shane5780ebd2008-11-11 17:36:30 +00007037 u16 contentOffset = get2byte(&data[hdr+5]);
7038 if (contentOffset > usableSize) {
7039 checkAppendMsg(pCheck, 0,
7040 "Corruption detected in header on page %d",iPage,0);
shane0af3f892008-11-12 04:55:34 +00007041 goto check_page_abort;
shane5780ebd2008-11-11 17:36:30 +00007042 }
7043 memset(hit+contentOffset, 0, usableSize-contentOffset);
7044 memset(hit, 1, contentOffset);
drh2e38c322004-09-03 18:38:44 +00007045 nCell = get2byte(&data[hdr+3]);
7046 cellStart = hdr + 12 - 4*pPage->leaf;
7047 for(i=0; i<nCell; i++){
7048 int pc = get2byte(&data[cellStart+i*2]);
danielk1977daca5432008-08-25 11:57:16 +00007049 u16 size = 1024;
drh2e38c322004-09-03 18:38:44 +00007050 int j;
danielk1977daca5432008-08-25 11:57:16 +00007051 if( pc<=usableSize ){
7052 size = cellSizePtr(pPage, &data[pc]);
7053 }
danielk19777701e812005-01-10 12:59:51 +00007054 if( (pc+size-1)>=usableSize || pc<0 ){
7055 checkAppendMsg(pCheck, 0,
7056 "Corruption detected in cell %d on page %d",i,iPage,0);
7057 }else{
7058 for(j=pc+size-1; j>=pc; j--) hit[j]++;
7059 }
drh2e38c322004-09-03 18:38:44 +00007060 }
7061 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
7062 cnt++){
7063 int size = get2byte(&data[i+2]);
7064 int j;
danielk19777701e812005-01-10 12:59:51 +00007065 if( (i+size-1)>=usableSize || i<0 ){
7066 checkAppendMsg(pCheck, 0,
7067 "Corruption detected in cell %d on page %d",i,iPage,0);
7068 }else{
7069 for(j=i+size-1; j>=i; j--) hit[j]++;
7070 }
drh2e38c322004-09-03 18:38:44 +00007071 i = get2byte(&data[i]);
7072 }
7073 for(i=cnt=0; i<usableSize; i++){
7074 if( hit[i]==0 ){
7075 cnt++;
7076 }else if( hit[i]>1 ){
7077 checkAppendMsg(pCheck, 0,
7078 "Multiple uses for byte %d of page %d", i, iPage);
7079 break;
7080 }
7081 }
7082 if( cnt!=data[hdr+7] ){
7083 checkAppendMsg(pCheck, 0,
7084 "Fragmented space is %d byte reported as %d on page %d",
7085 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00007086 }
7087 }
shane0af3f892008-11-12 04:55:34 +00007088check_page_abort:
7089 if (hit) sqlite3PageFree(hit);
drh6019e162001-07-02 17:51:45 +00007090
drh4b70f112004-05-02 21:12:19 +00007091 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00007092 return depth+1;
drh5eddca62001-06-30 21:53:53 +00007093}
drhb7f91642004-10-31 02:22:47 +00007094#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00007095
drhb7f91642004-10-31 02:22:47 +00007096#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00007097/*
7098** This routine does a complete check of the given BTree file. aRoot[] is
7099** an array of pages numbers were each page number is the root page of
7100** a table. nRoot is the number of entries in aRoot.
7101**
drhc890fec2008-08-01 20:10:08 +00007102** Write the number of error seen in *pnErr. Except for some memory
drhe43ba702008-12-05 22:40:08 +00007103** allocation errors, an error message held in memory obtained from
drhc890fec2008-08-01 20:10:08 +00007104** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
drhe43ba702008-12-05 22:40:08 +00007105** returned. If a memory allocation error occurs, NULL is returned.
drh5eddca62001-06-30 21:53:53 +00007106*/
drh1dcdbc02007-01-27 02:24:54 +00007107char *sqlite3BtreeIntegrityCheck(
7108 Btree *p, /* The btree to be checked */
7109 int *aRoot, /* An array of root pages numbers for individual trees */
7110 int nRoot, /* Number of entries in aRoot[] */
7111 int mxErr, /* Stop reporting errors after this many */
7112 int *pnErr /* Write number of errors seen to this variable */
7113){
danielk197789d40042008-11-17 14:20:56 +00007114 Pgno i;
drh5eddca62001-06-30 21:53:53 +00007115 int nRef;
drhaaab5722002-02-19 13:39:21 +00007116 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:01 +00007117 BtShared *pBt = p->pBt;
drhf089aa42008-07-08 19:34:06 +00007118 char zErr[100];
drh5eddca62001-06-30 21:53:53 +00007119
drhd677b3d2007-08-20 22:48:41 +00007120 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00007121 pBt->db = p->db;
danielk19773b8a05f2007-03-19 17:44:26 +00007122 nRef = sqlite3PagerRefcount(pBt->pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00007123 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
drhc890fec2008-08-01 20:10:08 +00007124 *pnErr = 1;
drhd677b3d2007-08-20 22:48:41 +00007125 sqlite3BtreeLeave(p);
drhc890fec2008-08-01 20:10:08 +00007126 return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
drhefc251d2001-07-01 22:12:01 +00007127 }
drh5eddca62001-06-30 21:53:53 +00007128 sCheck.pBt = pBt;
7129 sCheck.pPager = pBt->pPager;
danielk197789d40042008-11-17 14:20:56 +00007130 sCheck.nPage = pagerPagecount(sCheck.pBt);
drh1dcdbc02007-01-27 02:24:54 +00007131 sCheck.mxErr = mxErr;
7132 sCheck.nErr = 0;
drhc890fec2008-08-01 20:10:08 +00007133 sCheck.mallocFailed = 0;
drh1dcdbc02007-01-27 02:24:54 +00007134 *pnErr = 0;
drh0de8c112002-07-06 16:32:14 +00007135 if( sCheck.nPage==0 ){
7136 unlockBtreeIfUnused(pBt);
drhd677b3d2007-08-20 22:48:41 +00007137 sqlite3BtreeLeave(p);
drh0de8c112002-07-06 16:32:14 +00007138 return 0;
7139 }
drhe5ae5732008-06-15 02:51:47 +00007140 sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00007141 if( !sCheck.anRef ){
7142 unlockBtreeIfUnused(pBt);
drh1dcdbc02007-01-27 02:24:54 +00007143 *pnErr = 1;
drhd677b3d2007-08-20 22:48:41 +00007144 sqlite3BtreeLeave(p);
drhc890fec2008-08-01 20:10:08 +00007145 return 0;
danielk1977ac245ec2005-01-14 13:50:11 +00007146 }
drhda200cc2004-05-09 11:51:38 +00007147 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00007148 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00007149 if( i<=sCheck.nPage ){
7150 sCheck.anRef[i] = 1;
7151 }
drhf089aa42008-07-08 19:34:06 +00007152 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
drh5eddca62001-06-30 21:53:53 +00007153
7154 /* Check the integrity of the freelist
7155 */
drha34b6762004-05-07 13:30:42 +00007156 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
7157 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00007158
7159 /* Check all the tables.
7160 */
danielk197789d40042008-11-17 14:20:56 +00007161 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
drh4ff6dfa2002-03-03 23:06:00 +00007162 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00007163#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00007164 if( pBt->autoVacuum && aRoot[i]>1 ){
7165 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
7166 }
7167#endif
danielk197762c14b32008-11-19 09:05:26 +00007168 checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
drh5eddca62001-06-30 21:53:53 +00007169 }
7170
7171 /* Make sure every page in the file is referenced
7172 */
drh1dcdbc02007-01-27 02:24:54 +00007173 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:42 +00007174#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00007175 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00007176 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00007177 }
danielk1977afcdd022004-10-31 16:25:42 +00007178#else
7179 /* If the database supports auto-vacuum, make sure no tables contain
7180 ** references to pointer-map pages.
7181 */
7182 if( sCheck.anRef[i]==0 &&
danielk1977266664d2006-02-10 08:24:21 +00007183 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00007184 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
7185 }
7186 if( sCheck.anRef[i]!=0 &&
danielk1977266664d2006-02-10 08:24:21 +00007187 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00007188 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
7189 }
7190#endif
drh5eddca62001-06-30 21:53:53 +00007191 }
7192
drh64022502009-01-09 14:11:04 +00007193 /* Make sure this analysis did not leave any unref() pages.
7194 ** This is an internal consistency check; an integrity check
7195 ** of the integrity check.
drh5eddca62001-06-30 21:53:53 +00007196 */
drh5e00f6c2001-09-13 13:46:56 +00007197 unlockBtreeIfUnused(pBt);
drh64022502009-01-09 14:11:04 +00007198 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
drh2e38c322004-09-03 18:38:44 +00007199 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00007200 "Outstanding page count goes from %d to %d during this analysis",
danielk19773b8a05f2007-03-19 17:44:26 +00007201 nRef, sqlite3PagerRefcount(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00007202 );
drh5eddca62001-06-30 21:53:53 +00007203 }
7204
7205 /* Clean up and report errors.
7206 */
drhd677b3d2007-08-20 22:48:41 +00007207 sqlite3BtreeLeave(p);
drh17435752007-08-16 04:30:38 +00007208 sqlite3_free(sCheck.anRef);
drhc890fec2008-08-01 20:10:08 +00007209 if( sCheck.mallocFailed ){
7210 sqlite3StrAccumReset(&sCheck.errMsg);
7211 *pnErr = sCheck.nErr+1;
7212 return 0;
7213 }
drh1dcdbc02007-01-27 02:24:54 +00007214 *pnErr = sCheck.nErr;
drhf089aa42008-07-08 19:34:06 +00007215 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
7216 return sqlite3StrAccumFinish(&sCheck.errMsg);
drh5eddca62001-06-30 21:53:53 +00007217}
drhb7f91642004-10-31 02:22:47 +00007218#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00007219
drh73509ee2003-04-06 20:44:45 +00007220/*
7221** Return the full pathname of the underlying database file.
drhd0679ed2007-08-28 22:24:34 +00007222**
7223** The pager filename is invariant as long as the pager is
7224** open so it is safe to access without the BtShared mutex.
drh73509ee2003-04-06 20:44:45 +00007225*/
danielk1977aef0bf62005-12-30 16:28:01 +00007226const char *sqlite3BtreeGetFilename(Btree *p){
7227 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00007228 return sqlite3PagerFilename(p->pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00007229}
7230
7231/*
danielk19775865e3d2004-06-14 06:03:57 +00007232** Return the pathname of the journal file for this database. The return
7233** value of this routine is the same regardless of whether the journal file
7234** has been created or not.
drhd0679ed2007-08-28 22:24:34 +00007235**
7236** The pager journal filename is invariant as long as the pager is
7237** open so it is safe to access without the BtShared mutex.
danielk19775865e3d2004-06-14 06:03:57 +00007238*/
danielk1977aef0bf62005-12-30 16:28:01 +00007239const char *sqlite3BtreeGetJournalname(Btree *p){
7240 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00007241 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00007242}
7243
danielk19771d850a72004-05-31 08:26:49 +00007244/*
7245** Return non-zero if a transaction is active.
7246*/
danielk1977aef0bf62005-12-30 16:28:01 +00007247int sqlite3BtreeIsInTrans(Btree *p){
drhe5fe6902007-12-07 18:55:28 +00007248 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00007249 return (p && (p->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00007250}
7251
7252/*
7253** Return non-zero if a statement transaction is active.
7254*/
danielk1977aef0bf62005-12-30 16:28:01 +00007255int sqlite3BtreeIsInStmt(Btree *p){
drh1fee73e2007-08-29 04:00:57 +00007256 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00007257 return ALWAYS(p->pBt) && p->pBt->inStmt;
danielk19771d850a72004-05-31 08:26:49 +00007258}
danielk197713adf8a2004-06-03 16:08:41 +00007259
7260/*
danielk19772372c2b2006-06-27 16:34:56 +00007261** Return non-zero if a read (or write) transaction is active.
7262*/
7263int sqlite3BtreeIsInReadTrans(Btree *p){
drh64022502009-01-09 14:11:04 +00007264 assert( p );
drhe5fe6902007-12-07 18:55:28 +00007265 assert( sqlite3_mutex_held(p->db->mutex) );
drh64022502009-01-09 14:11:04 +00007266 return p->inTrans!=TRANS_NONE;
danielk19772372c2b2006-06-27 16:34:56 +00007267}
7268
danielk197704103022009-02-03 16:51:24 +00007269int sqlite3BtreeIsInBackup(Btree *p){
7270 assert( p );
7271 assert( sqlite3_mutex_held(p->db->mutex) );
7272 return p->nBackup!=0;
7273}
7274
danielk19772372c2b2006-06-27 16:34:56 +00007275/*
danielk1977da184232006-01-05 11:34:32 +00007276** This function returns a pointer to a blob of memory associated with
drh85b623f2007-12-13 21:54:09 +00007277** a single shared-btree. The memory is used by client code for its own
danielk1977da184232006-01-05 11:34:32 +00007278** purposes (for example, to store a high-level schema associated with
7279** the shared-btree). The btree layer manages reference counting issues.
7280**
7281** The first time this is called on a shared-btree, nBytes bytes of memory
7282** are allocated, zeroed, and returned to the caller. For each subsequent
7283** call the nBytes parameter is ignored and a pointer to the same blob
7284** of memory returned.
7285**
danielk1977171bfed2008-06-23 09:50:50 +00007286** If the nBytes parameter is 0 and the blob of memory has not yet been
7287** allocated, a null pointer is returned. If the blob has already been
7288** allocated, it is returned as normal.
7289**
danielk1977da184232006-01-05 11:34:32 +00007290** Just before the shared-btree is closed, the function passed as the
7291** xFree argument when the memory allocation was made is invoked on the
drh17435752007-08-16 04:30:38 +00007292** blob of allocated memory. This function should not call sqlite3_free()
danielk1977da184232006-01-05 11:34:32 +00007293** on the memory, the btree layer does that.
7294*/
7295void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
7296 BtShared *pBt = p->pBt;
drh27641702007-08-22 02:56:42 +00007297 sqlite3BtreeEnter(p);
danielk1977171bfed2008-06-23 09:50:50 +00007298 if( !pBt->pSchema && nBytes ){
drh17435752007-08-16 04:30:38 +00007299 pBt->pSchema = sqlite3MallocZero(nBytes);
danielk1977da184232006-01-05 11:34:32 +00007300 pBt->xFreeSchema = xFree;
7301 }
drh27641702007-08-22 02:56:42 +00007302 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00007303 return pBt->pSchema;
7304}
7305
danielk1977c87d34d2006-01-06 13:00:28 +00007306/*
7307** Return true if another user of the same shared btree as the argument
7308** handle holds an exclusive lock on the sqlite_master table.
7309*/
7310int sqlite3BtreeSchemaLocked(Btree *p){
drh27641702007-08-22 02:56:42 +00007311 int rc;
drhe5fe6902007-12-07 18:55:28 +00007312 assert( sqlite3_mutex_held(p->db->mutex) );
drh27641702007-08-22 02:56:42 +00007313 sqlite3BtreeEnter(p);
7314 rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
7315 sqlite3BtreeLeave(p);
7316 return rc;
danielk1977c87d34d2006-01-06 13:00:28 +00007317}
7318
drha154dcd2006-03-22 22:10:07 +00007319
7320#ifndef SQLITE_OMIT_SHARED_CACHE
7321/*
7322** Obtain a lock on the table whose root page is iTab. The
7323** lock is a write lock if isWritelock is true or a read lock
7324** if it is false.
7325*/
danielk1977c00da102006-01-07 13:21:04 +00007326int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:27 +00007327 int rc = SQLITE_OK;
drh6a9ad3d2008-04-02 16:29:30 +00007328 if( p->sharable ){
7329 u8 lockType = READ_LOCK + isWriteLock;
7330 assert( READ_LOCK+1==WRITE_LOCK );
7331 assert( isWriteLock==0 || isWriteLock==1 );
7332 sqlite3BtreeEnter(p);
7333 rc = queryTableLock(p, iTab, lockType);
7334 if( rc==SQLITE_OK ){
7335 rc = lockTable(p, iTab, lockType);
7336 }
7337 sqlite3BtreeLeave(p);
danielk1977c00da102006-01-07 13:21:04 +00007338 }
7339 return rc;
7340}
drha154dcd2006-03-22 22:10:07 +00007341#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00007342
danielk1977b4e9af92007-05-01 17:49:49 +00007343#ifndef SQLITE_OMIT_INCRBLOB
7344/*
7345** Argument pCsr must be a cursor opened for writing on an
7346** INTKEY table currently pointing at a valid table entry.
7347** This function modifies the data stored as part of that entry.
7348** Only the data content may only be modified, it is not possible
7349** to change the length of the data stored.
7350*/
danielk1977dcbb5d32007-05-04 18:36:44 +00007351int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
drh1fee73e2007-08-29 04:00:57 +00007352 assert( cursorHoldsMutex(pCsr) );
drhe5fe6902007-12-07 18:55:28 +00007353 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007354 assert(pCsr->isIncrblobHandle);
danielk19773588ceb2008-06-10 17:30:26 +00007355
drha3460582008-07-11 21:02:53 +00007356 restoreCursorPosition(pCsr);
danielk19773588ceb2008-06-10 17:30:26 +00007357 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
7358 if( pCsr->eState!=CURSOR_VALID ){
7359 return SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +00007360 }
7361
danielk1977d04417962007-05-02 13:16:30 +00007362 /* Check some preconditions:
danielk1977dcbb5d32007-05-04 18:36:44 +00007363 ** (a) the cursor is open for writing,
7364 ** (b) there is no read-lock on the table being modified and
7365 ** (c) the cursor points at a valid row of an intKey table.
danielk1977d04417962007-05-02 13:16:30 +00007366 */
danielk1977d04417962007-05-02 13:16:30 +00007367 if( !pCsr->wrFlag ){
danielk1977dcbb5d32007-05-04 18:36:44 +00007368 return SQLITE_READONLY;
danielk1977d04417962007-05-02 13:16:30 +00007369 }
drhd0679ed2007-08-28 22:24:34 +00007370 assert( !pCsr->pBt->readOnly
7371 && pCsr->pBt->inTransaction==TRANS_WRITE );
danielk19773588ceb2008-06-10 17:30:26 +00007372 if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){
danielk1977d04417962007-05-02 13:16:30 +00007373 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
7374 }
danielk197771d5d2c2008-09-29 11:49:47 +00007375 if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){
danielk1977d04417962007-05-02 13:16:30 +00007376 return SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +00007377 }
7378
danielk19779f8d6402007-05-02 17:48:45 +00007379 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
danielk1977b4e9af92007-05-01 17:49:49 +00007380}
danielk19772dec9702007-05-02 16:48:37 +00007381
7382/*
7383** Set a flag on this cursor to cache the locations of pages from the
danielk1977da107192007-05-04 08:32:13 +00007384** overflow list for the current row. This is used by cursors opened
7385** for incremental blob IO only.
7386**
7387** This function sets a flag only. The actual page location cache
7388** (stored in BtCursor.aOverflow[]) is allocated and used by function
7389** accessPayload() (the worker function for sqlite3BtreeData() and
7390** sqlite3BtreePutData()).
danielk19772dec9702007-05-02 16:48:37 +00007391*/
7392void sqlite3BtreeCacheOverflow(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00007393 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00007394 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007395 assert(!pCur->isIncrblobHandle);
danielk19772dec9702007-05-02 16:48:37 +00007396 assert(!pCur->aOverflow);
danielk1977dcbb5d32007-05-04 18:36:44 +00007397 pCur->isIncrblobHandle = 1;
danielk19772dec9702007-05-02 16:48:37 +00007398}
danielk1977b4e9af92007-05-01 17:49:49 +00007399#endif