Merge sqlite-release(3.45.1) into prerelease-integration
[sqlcipher.git] / src / btree.c
blobc41fb811ab348af83a9b1783e703ccd3f45ec049
1 /*
2 ** 2004 April 6
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** 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.
11 *************************************************************************
12 ** This file implements an external (disk-based) database using BTrees.
13 ** See the header comment on "btreeInt.h" for additional information.
14 ** Including a description of file format and an overview of operation.
16 #include "btreeInt.h"
19 ** The header string that appears at the beginning of every
20 ** SQLite database.
22 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
25 ** Set this global variable to 1 to enable tracing using the TRACE
26 ** macro.
28 #if 0
29 int sqlite3BtreeTrace=1; /* True to enable tracing */
30 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
31 #else
32 # define TRACE(X)
33 #endif
36 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
37 ** But if the value is zero, make it 65536.
39 ** This routine is used to extract the "offset to cell content area" value
40 ** from the header of a btree page. If the page size is 65536 and the page
41 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
42 ** This routine makes the necessary adjustment to 65536.
44 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1)
47 ** Values passed as the 5th argument to allocateBtreePage()
49 #define BTALLOC_ANY 0 /* Allocate any page */
50 #define BTALLOC_EXACT 1 /* Allocate exact page if possible */
51 #define BTALLOC_LE 2 /* Allocate any page <= the parameter */
54 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
55 ** defined, or 0 if it is. For example:
57 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
59 #ifndef SQLITE_OMIT_AUTOVACUUM
60 #define IfNotOmitAV(expr) (expr)
61 #else
62 #define IfNotOmitAV(expr) 0
63 #endif
65 #ifndef SQLITE_OMIT_SHARED_CACHE
67 ** A list of BtShared objects that are eligible for participation
68 ** in shared cache. This variable has file scope during normal builds,
69 ** but the test harness needs to access it so we make it global for
70 ** test builds.
72 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MAIN.
74 #ifdef SQLITE_TEST
75 BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
76 #else
77 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
78 #endif
79 #endif /* SQLITE_OMIT_SHARED_CACHE */
81 #ifndef SQLITE_OMIT_SHARED_CACHE
83 ** Enable or disable the shared pager and schema features.
85 ** This routine has no effect on existing database connections.
86 ** The shared cache setting effects only future calls to
87 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
89 int sqlite3_enable_shared_cache(int enable){
90 sqlite3GlobalConfig.sharedCacheEnabled = enable;
91 return SQLITE_OK;
93 #endif
97 #ifdef SQLITE_OMIT_SHARED_CACHE
99 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
100 ** and clearAllSharedCacheTableLocks()
101 ** manipulate entries in the BtShared.pLock linked list used to store
102 ** shared-cache table level locks. If the library is compiled with the
103 ** shared-cache feature disabled, then there is only ever one user
104 ** of each BtShared structure and so this locking is not necessary.
105 ** So define the lock related functions as no-ops.
107 #define querySharedCacheTableLock(a,b,c) SQLITE_OK
108 #define setSharedCacheTableLock(a,b,c) SQLITE_OK
109 #define clearAllSharedCacheTableLocks(a)
110 #define downgradeAllSharedCacheTableLocks(a)
111 #define hasSharedCacheTableLock(a,b,c,d) 1
112 #define hasReadConflicts(a, b) 0
113 #endif
115 #ifdef SQLITE_DEBUG
117 ** Return and reset the seek counter for a Btree object.
119 sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){
120 u64 n = pBt->nSeek;
121 pBt->nSeek = 0;
122 return n;
124 #endif
127 ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
128 ** (MemPage*) as an argument. The (MemPage*) must not be NULL.
130 ** If SQLITE_DEBUG is not defined, then this macro is equivalent to
131 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
132 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
133 ** with the page number and filename associated with the (MemPage*).
135 #ifdef SQLITE_DEBUG
136 int corruptPageError(int lineno, MemPage *p){
137 char *zMsg;
138 sqlite3BeginBenignMalloc();
139 zMsg = sqlite3_mprintf("database corruption page %u of %s",
140 p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
142 sqlite3EndBenignMalloc();
143 if( zMsg ){
144 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
146 sqlite3_free(zMsg);
147 return SQLITE_CORRUPT_BKPT;
149 # define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
150 #else
151 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
152 #endif
154 #ifndef SQLITE_OMIT_SHARED_CACHE
156 #ifdef SQLITE_DEBUG
158 **** This function is only used as part of an assert() statement. ***
160 ** Check to see if pBtree holds the required locks to read or write to the
161 ** table with root page iRoot. Return 1 if it does and 0 if not.
163 ** For example, when writing to a table with root-page iRoot via
164 ** Btree connection pBtree:
166 ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
168 ** When writing to an index that resides in a sharable database, the
169 ** caller should have first obtained a lock specifying the root page of
170 ** the corresponding table. This makes things a bit more complicated,
171 ** as this module treats each table as a separate structure. To determine
172 ** the table corresponding to the index being written, this
173 ** function has to search through the database schema.
175 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
176 ** hold a write-lock on the schema table (root page 1). This is also
177 ** acceptable.
179 static int hasSharedCacheTableLock(
180 Btree *pBtree, /* Handle that must hold lock */
181 Pgno iRoot, /* Root page of b-tree */
182 int isIndex, /* True if iRoot is the root of an index b-tree */
183 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
185 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
186 Pgno iTab = 0;
187 BtLock *pLock;
189 /* If this database is not shareable, or if the client is reading
190 ** and has the read-uncommitted flag set, then no lock is required.
191 ** Return true immediately.
193 if( (pBtree->sharable==0)
194 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
196 return 1;
199 /* If the client is reading or writing an index and the schema is
200 ** not loaded, then it is too difficult to actually check to see if
201 ** the correct locks are held. So do not bother - just return true.
202 ** This case does not come up very often anyhow.
204 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
205 return 1;
208 /* Figure out the root-page that the lock should be held on. For table
209 ** b-trees, this is just the root page of the b-tree being read or
210 ** written. For index b-trees, it is the root page of the associated
211 ** table. */
212 if( isIndex ){
213 HashElem *p;
214 int bSeen = 0;
215 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
216 Index *pIdx = (Index *)sqliteHashData(p);
217 if( pIdx->tnum==iRoot ){
218 if( bSeen ){
219 /* Two or more indexes share the same root page. There must
220 ** be imposter tables. So just return true. The assert is not
221 ** useful in that case. */
222 return 1;
224 iTab = pIdx->pTable->tnum;
225 bSeen = 1;
228 }else{
229 iTab = iRoot;
232 /* Search for the required lock. Either a write-lock on root-page iTab, a
233 ** write-lock on the schema table, or (if the client is reading) a
234 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
235 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
236 if( pLock->pBtree==pBtree
237 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
238 && pLock->eLock>=eLockType
240 return 1;
244 /* Failed to find the required lock. */
245 return 0;
247 #endif /* SQLITE_DEBUG */
249 #ifdef SQLITE_DEBUG
251 **** This function may be used as part of assert() statements only. ****
253 ** Return true if it would be illegal for pBtree to write into the
254 ** table or index rooted at iRoot because other shared connections are
255 ** simultaneously reading that same table or index.
257 ** It is illegal for pBtree to write if some other Btree object that
258 ** shares the same BtShared object is currently reading or writing
259 ** the iRoot table. Except, if the other Btree object has the
260 ** read-uncommitted flag set, then it is OK for the other object to
261 ** have a read cursor.
263 ** For example, before writing to any part of the table or index
264 ** rooted at page iRoot, one should call:
266 ** assert( !hasReadConflicts(pBtree, iRoot) );
268 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
269 BtCursor *p;
270 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
271 if( p->pgnoRoot==iRoot
272 && p->pBtree!=pBtree
273 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
275 return 1;
278 return 0;
280 #endif /* #ifdef SQLITE_DEBUG */
283 ** Query to see if Btree handle p may obtain a lock of type eLock
284 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
285 ** SQLITE_OK if the lock may be obtained (by calling
286 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
288 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
289 BtShared *pBt = p->pBt;
290 BtLock *pIter;
292 assert( sqlite3BtreeHoldsMutex(p) );
293 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
294 assert( p->db!=0 );
295 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
297 /* If requesting a write-lock, then the Btree must have an open write
298 ** transaction on this file. And, obviously, for this to be so there
299 ** must be an open write transaction on the file itself.
301 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
302 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
304 /* This routine is a no-op if the shared-cache is not enabled */
305 if( !p->sharable ){
306 return SQLITE_OK;
309 /* If some other connection is holding an exclusive lock, the
310 ** requested lock may not be obtained.
312 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
313 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
314 return SQLITE_LOCKED_SHAREDCACHE;
317 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
318 /* The condition (pIter->eLock!=eLock) in the following if(...)
319 ** statement is a simplification of:
321 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
323 ** since we know that if eLock==WRITE_LOCK, then no other connection
324 ** may hold a WRITE_LOCK on any table in this file (since there can
325 ** only be a single writer).
327 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
328 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
329 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
330 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
331 if( eLock==WRITE_LOCK ){
332 assert( p==pBt->pWriter );
333 pBt->btsFlags |= BTS_PENDING;
335 return SQLITE_LOCKED_SHAREDCACHE;
338 return SQLITE_OK;
340 #endif /* !SQLITE_OMIT_SHARED_CACHE */
342 #ifndef SQLITE_OMIT_SHARED_CACHE
344 ** Add a lock on the table with root-page iTable to the shared-btree used
345 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
346 ** WRITE_LOCK.
348 ** This function assumes the following:
350 ** (a) The specified Btree object p is connected to a sharable
351 ** database (one with the BtShared.sharable flag set), and
353 ** (b) No other Btree objects hold a lock that conflicts
354 ** with the requested lock (i.e. querySharedCacheTableLock() has
355 ** already been called and returned SQLITE_OK).
357 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
358 ** is returned if a malloc attempt fails.
360 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
361 BtShared *pBt = p->pBt;
362 BtLock *pLock = 0;
363 BtLock *pIter;
365 assert( sqlite3BtreeHoldsMutex(p) );
366 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
367 assert( p->db!=0 );
369 /* A connection with the read-uncommitted flag set will never try to
370 ** obtain a read-lock using this function. The only read-lock obtained
371 ** by a connection in read-uncommitted mode is on the sqlite_schema
372 ** table, and that lock is obtained in BtreeBeginTrans(). */
373 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
375 /* This function should only be called on a sharable b-tree after it
376 ** has been determined that no other b-tree holds a conflicting lock. */
377 assert( p->sharable );
378 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
380 /* First search the list for an existing lock on this table. */
381 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
382 if( pIter->iTable==iTable && pIter->pBtree==p ){
383 pLock = pIter;
384 break;
388 /* If the above search did not find a BtLock struct associating Btree p
389 ** with table iTable, allocate one and link it into the list.
391 if( !pLock ){
392 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
393 if( !pLock ){
394 return SQLITE_NOMEM_BKPT;
396 pLock->iTable = iTable;
397 pLock->pBtree = p;
398 pLock->pNext = pBt->pLock;
399 pBt->pLock = pLock;
402 /* Set the BtLock.eLock variable to the maximum of the current lock
403 ** and the requested lock. This means if a write-lock was already held
404 ** and a read-lock requested, we don't incorrectly downgrade the lock.
406 assert( WRITE_LOCK>READ_LOCK );
407 if( eLock>pLock->eLock ){
408 pLock->eLock = eLock;
411 return SQLITE_OK;
413 #endif /* !SQLITE_OMIT_SHARED_CACHE */
415 #ifndef SQLITE_OMIT_SHARED_CACHE
417 ** Release all the table locks (locks obtained via calls to
418 ** the setSharedCacheTableLock() procedure) held by Btree object p.
420 ** This function assumes that Btree p has an open read or write
421 ** transaction. If it does not, then the BTS_PENDING flag
422 ** may be incorrectly cleared.
424 static void clearAllSharedCacheTableLocks(Btree *p){
425 BtShared *pBt = p->pBt;
426 BtLock **ppIter = &pBt->pLock;
428 assert( sqlite3BtreeHoldsMutex(p) );
429 assert( p->sharable || 0==*ppIter );
430 assert( p->inTrans>0 );
432 while( *ppIter ){
433 BtLock *pLock = *ppIter;
434 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
435 assert( pLock->pBtree->inTrans>=pLock->eLock );
436 if( pLock->pBtree==p ){
437 *ppIter = pLock->pNext;
438 assert( pLock->iTable!=1 || pLock==&p->lock );
439 if( pLock->iTable!=1 ){
440 sqlite3_free(pLock);
442 }else{
443 ppIter = &pLock->pNext;
447 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
448 if( pBt->pWriter==p ){
449 pBt->pWriter = 0;
450 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
451 }else if( pBt->nTransaction==2 ){
452 /* This function is called when Btree p is concluding its
453 ** transaction. If there currently exists a writer, and p is not
454 ** that writer, then the number of locks held by connections other
455 ** than the writer must be about to drop to zero. In this case
456 ** set the BTS_PENDING flag to 0.
458 ** If there is not currently a writer, then BTS_PENDING must
459 ** be zero already. So this next line is harmless in that case.
461 pBt->btsFlags &= ~BTS_PENDING;
466 ** This function changes all write-locks held by Btree p into read-locks.
468 static void downgradeAllSharedCacheTableLocks(Btree *p){
469 BtShared *pBt = p->pBt;
470 if( pBt->pWriter==p ){
471 BtLock *pLock;
472 pBt->pWriter = 0;
473 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
474 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
475 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
476 pLock->eLock = READ_LOCK;
481 #endif /* SQLITE_OMIT_SHARED_CACHE */
483 static void releasePage(MemPage *pPage); /* Forward reference */
484 static void releasePageOne(MemPage *pPage); /* Forward reference */
485 static void releasePageNotNull(MemPage *pPage); /* Forward reference */
488 ***** This routine is used inside of assert() only ****
490 ** Verify that the cursor holds the mutex on its BtShared
492 #ifdef SQLITE_DEBUG
493 static int cursorHoldsMutex(BtCursor *p){
494 return sqlite3_mutex_held(p->pBt->mutex);
497 /* Verify that the cursor and the BtShared agree about what is the current
498 ** database connetion. This is important in shared-cache mode. If the database
499 ** connection pointers get out-of-sync, it is possible for routines like
500 ** btreeInitPage() to reference an stale connection pointer that references a
501 ** a connection that has already closed. This routine is used inside assert()
502 ** statements only and for the purpose of double-checking that the btree code
503 ** does keep the database connection pointers up-to-date.
505 static int cursorOwnsBtShared(BtCursor *p){
506 assert( cursorHoldsMutex(p) );
507 return (p->pBtree->db==p->pBt->db);
509 #endif
512 ** Invalidate the overflow cache of the cursor passed as the first argument.
513 ** on the shared btree structure pBt.
515 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
518 ** Invalidate the overflow page-list cache for all cursors opened
519 ** on the shared btree structure pBt.
521 static void invalidateAllOverflowCache(BtShared *pBt){
522 BtCursor *p;
523 assert( sqlite3_mutex_held(pBt->mutex) );
524 for(p=pBt->pCursor; p; p=p->pNext){
525 invalidateOverflowCache(p);
529 #ifndef SQLITE_OMIT_INCRBLOB
531 ** This function is called before modifying the contents of a table
532 ** to invalidate any incrblob cursors that are open on the
533 ** row or one of the rows being modified.
535 ** If argument isClearTable is true, then the entire contents of the
536 ** table is about to be deleted. In this case invalidate all incrblob
537 ** cursors open on any row within the table with root-page pgnoRoot.
539 ** Otherwise, if argument isClearTable is false, then the row with
540 ** rowid iRow is being replaced or deleted. In this case invalidate
541 ** only those incrblob cursors open on that specific row.
543 static void invalidateIncrblobCursors(
544 Btree *pBtree, /* The database file to check */
545 Pgno pgnoRoot, /* The table that might be changing */
546 i64 iRow, /* The rowid that might be changing */
547 int isClearTable /* True if all rows are being deleted */
549 BtCursor *p;
550 assert( pBtree->hasIncrblobCur );
551 assert( sqlite3BtreeHoldsMutex(pBtree) );
552 pBtree->hasIncrblobCur = 0;
553 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
554 if( (p->curFlags & BTCF_Incrblob)!=0 ){
555 pBtree->hasIncrblobCur = 1;
556 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
557 p->eState = CURSOR_INVALID;
563 #else
564 /* Stub function when INCRBLOB is omitted */
565 #define invalidateIncrblobCursors(w,x,y,z)
566 #endif /* SQLITE_OMIT_INCRBLOB */
569 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
570 ** when a page that previously contained data becomes a free-list leaf
571 ** page.
573 ** The BtShared.pHasContent bitvec exists to work around an obscure
574 ** bug caused by the interaction of two useful IO optimizations surrounding
575 ** free-list leaf pages:
577 ** 1) When all data is deleted from a page and the page becomes
578 ** a free-list leaf page, the page is not written to the database
579 ** (as free-list leaf pages contain no meaningful data). Sometimes
580 ** such a page is not even journalled (as it will not be modified,
581 ** why bother journalling it?).
583 ** 2) When a free-list leaf page is reused, its content is not read
584 ** from the database or written to the journal file (why should it
585 ** be, if it is not at all meaningful?).
587 ** By themselves, these optimizations work fine and provide a handy
588 ** performance boost to bulk delete or insert operations. However, if
589 ** a page is moved to the free-list and then reused within the same
590 ** transaction, a problem comes up. If the page is not journalled when
591 ** it is moved to the free-list and it is also not journalled when it
592 ** is extracted from the free-list and reused, then the original data
593 ** may be lost. In the event of a rollback, it may not be possible
594 ** to restore the database to its original configuration.
596 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
597 ** moved to become a free-list leaf page, the corresponding bit is
598 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
599 ** optimization 2 above is omitted if the corresponding bit is already
600 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
601 ** at the end of every transaction.
603 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
604 int rc = SQLITE_OK;
605 if( !pBt->pHasContent ){
606 assert( pgno<=pBt->nPage );
607 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
608 if( !pBt->pHasContent ){
609 rc = SQLITE_NOMEM_BKPT;
612 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
613 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
615 return rc;
619 ** Query the BtShared.pHasContent vector.
621 ** This function is called when a free-list leaf page is removed from the
622 ** free-list for reuse. It returns false if it is safe to retrieve the
623 ** page from the pager layer with the 'no-content' flag set. True otherwise.
625 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
626 Bitvec *p = pBt->pHasContent;
627 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno));
631 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
632 ** invoked at the conclusion of each write-transaction.
634 static void btreeClearHasContent(BtShared *pBt){
635 sqlite3BitvecDestroy(pBt->pHasContent);
636 pBt->pHasContent = 0;
640 ** Release all of the apPage[] pages for a cursor.
642 static void btreeReleaseAllCursorPages(BtCursor *pCur){
643 int i;
644 if( pCur->iPage>=0 ){
645 for(i=0; i<pCur->iPage; i++){
646 releasePageNotNull(pCur->apPage[i]);
648 releasePageNotNull(pCur->pPage);
649 pCur->iPage = -1;
654 ** The cursor passed as the only argument must point to a valid entry
655 ** when this function is called (i.e. have eState==CURSOR_VALID). This
656 ** function saves the current cursor key in variables pCur->nKey and
657 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
658 ** code otherwise.
660 ** If the cursor is open on an intkey table, then the integer key
661 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
662 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
663 ** set to point to a malloced buffer pCur->nKey bytes in size containing
664 ** the key.
666 static int saveCursorKey(BtCursor *pCur){
667 int rc = SQLITE_OK;
668 assert( CURSOR_VALID==pCur->eState );
669 assert( 0==pCur->pKey );
670 assert( cursorHoldsMutex(pCur) );
672 if( pCur->curIntKey ){
673 /* Only the rowid is required for a table btree */
674 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
675 }else{
676 /* For an index btree, save the complete key content. It is possible
677 ** that the current key is corrupt. In that case, it is possible that
678 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by
679 ** up to the size of 1 varint plus 1 8-byte value when the cursor
680 ** position is restored. Hence the 17 bytes of padding allocated
681 ** below. */
682 void *pKey;
683 pCur->nKey = sqlite3BtreePayloadSize(pCur);
684 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
685 if( pKey ){
686 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
687 if( rc==SQLITE_OK ){
688 memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
689 pCur->pKey = pKey;
690 }else{
691 sqlite3_free(pKey);
693 }else{
694 rc = SQLITE_NOMEM_BKPT;
697 assert( !pCur->curIntKey || !pCur->pKey );
698 return rc;
702 ** Save the current cursor position in the variables BtCursor.nKey
703 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
705 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
706 ** prior to calling this routine.
708 static int saveCursorPosition(BtCursor *pCur){
709 int rc;
711 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
712 assert( 0==pCur->pKey );
713 assert( cursorHoldsMutex(pCur) );
715 if( pCur->curFlags & BTCF_Pinned ){
716 return SQLITE_CONSTRAINT_PINNED;
718 if( pCur->eState==CURSOR_SKIPNEXT ){
719 pCur->eState = CURSOR_VALID;
720 }else{
721 pCur->skipNext = 0;
724 rc = saveCursorKey(pCur);
725 if( rc==SQLITE_OK ){
726 btreeReleaseAllCursorPages(pCur);
727 pCur->eState = CURSOR_REQUIRESEEK;
730 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
731 return rc;
734 /* Forward reference */
735 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
738 ** Save the positions of all cursors (except pExcept) that are open on
739 ** the table with root-page iRoot. "Saving the cursor position" means that
740 ** the location in the btree is remembered in such a way that it can be
741 ** moved back to the same spot after the btree has been modified. This
742 ** routine is called just before cursor pExcept is used to modify the
743 ** table, for example in BtreeDelete() or BtreeInsert().
745 ** If there are two or more cursors on the same btree, then all such
746 ** cursors should have their BTCF_Multiple flag set. The btreeCursor()
747 ** routine enforces that rule. This routine only needs to be called in
748 ** the uncommon case when pExpect has the BTCF_Multiple flag set.
750 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
751 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
752 ** pointless call to this routine.
754 ** Implementation note: This routine merely checks to see if any cursors
755 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
756 ** event that cursors are in need to being saved.
758 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
759 BtCursor *p;
760 assert( sqlite3_mutex_held(pBt->mutex) );
761 assert( pExcept==0 || pExcept->pBt==pBt );
762 for(p=pBt->pCursor; p; p=p->pNext){
763 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
765 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
766 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
767 return SQLITE_OK;
770 /* This helper routine to saveAllCursors does the actual work of saving
771 ** the cursors if and when a cursor is found that actually requires saving.
772 ** The common case is that no cursors need to be saved, so this routine is
773 ** broken out from its caller to avoid unnecessary stack pointer movement.
775 static int SQLITE_NOINLINE saveCursorsOnList(
776 BtCursor *p, /* The first cursor that needs saving */
777 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
778 BtCursor *pExcept /* Do not save this cursor */
781 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
782 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
783 int rc = saveCursorPosition(p);
784 if( SQLITE_OK!=rc ){
785 return rc;
787 }else{
788 testcase( p->iPage>=0 );
789 btreeReleaseAllCursorPages(p);
792 p = p->pNext;
793 }while( p );
794 return SQLITE_OK;
798 ** Clear the current cursor position.
800 void sqlite3BtreeClearCursor(BtCursor *pCur){
801 assert( cursorHoldsMutex(pCur) );
802 sqlite3_free(pCur->pKey);
803 pCur->pKey = 0;
804 pCur->eState = CURSOR_INVALID;
808 ** In this version of BtreeMoveto, pKey is a packed index record
809 ** such as is generated by the OP_MakeRecord opcode. Unpack the
810 ** record and then call sqlite3BtreeIndexMoveto() to do the work.
812 static int btreeMoveto(
813 BtCursor *pCur, /* Cursor open on the btree to be searched */
814 const void *pKey, /* Packed key if the btree is an index */
815 i64 nKey, /* Integer key for tables. Size of pKey for indices */
816 int bias, /* Bias search to the high end */
817 int *pRes /* Write search results here */
819 int rc; /* Status code */
820 UnpackedRecord *pIdxKey; /* Unpacked index key */
822 if( pKey ){
823 KeyInfo *pKeyInfo = pCur->pKeyInfo;
824 assert( nKey==(i64)(int)nKey );
825 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
826 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
827 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
828 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
829 rc = SQLITE_CORRUPT_BKPT;
830 }else{
831 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes);
833 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
834 }else{
835 pIdxKey = 0;
836 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes);
838 return rc;
842 ** Restore the cursor to the position it was in (or as close to as possible)
843 ** when saveCursorPosition() was called. Note that this call deletes the
844 ** saved position info stored by saveCursorPosition(), so there can be
845 ** at most one effective restoreCursorPosition() call after each
846 ** saveCursorPosition().
848 static int btreeRestoreCursorPosition(BtCursor *pCur){
849 int rc;
850 int skipNext = 0;
851 assert( cursorOwnsBtShared(pCur) );
852 assert( pCur->eState>=CURSOR_REQUIRESEEK );
853 if( pCur->eState==CURSOR_FAULT ){
854 return pCur->skipNext;
856 pCur->eState = CURSOR_INVALID;
857 if( sqlite3FaultSim(410) ){
858 rc = SQLITE_IOERR;
859 }else{
860 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
862 if( rc==SQLITE_OK ){
863 sqlite3_free(pCur->pKey);
864 pCur->pKey = 0;
865 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
866 if( skipNext ) pCur->skipNext = skipNext;
867 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
868 pCur->eState = CURSOR_SKIPNEXT;
871 return rc;
874 #define restoreCursorPosition(p) \
875 (p->eState>=CURSOR_REQUIRESEEK ? \
876 btreeRestoreCursorPosition(p) : \
877 SQLITE_OK)
880 ** Determine whether or not a cursor has moved from the position where
881 ** it was last placed, or has been invalidated for any other reason.
882 ** Cursors can move when the row they are pointing at is deleted out
883 ** from under them, for example. Cursor might also move if a btree
884 ** is rebalanced.
886 ** Calling this routine with a NULL cursor pointer returns false.
888 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
889 ** back to where it ought to be if this routine returns true.
891 int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
892 assert( EIGHT_BYTE_ALIGNMENT(pCur)
893 || pCur==sqlite3BtreeFakeValidCursor() );
894 assert( offsetof(BtCursor, eState)==0 );
895 assert( sizeof(pCur->eState)==1 );
896 return CURSOR_VALID != *(u8*)pCur;
900 ** Return a pointer to a fake BtCursor object that will always answer
901 ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
902 ** cursor returned must not be used with any other Btree interface.
904 BtCursor *sqlite3BtreeFakeValidCursor(void){
905 static u8 fakeCursor = CURSOR_VALID;
906 assert( offsetof(BtCursor, eState)==0 );
907 return (BtCursor*)&fakeCursor;
911 ** This routine restores a cursor back to its original position after it
912 ** has been moved by some outside activity (such as a btree rebalance or
913 ** a row having been deleted out from under the cursor).
915 ** On success, the *pDifferentRow parameter is false if the cursor is left
916 ** pointing at exactly the same row. *pDifferntRow is the row the cursor
917 ** was pointing to has been deleted, forcing the cursor to point to some
918 ** nearby row.
920 ** This routine should only be called for a cursor that just returned
921 ** TRUE from sqlite3BtreeCursorHasMoved().
923 int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
924 int rc;
926 assert( pCur!=0 );
927 assert( pCur->eState!=CURSOR_VALID );
928 rc = restoreCursorPosition(pCur);
929 if( rc ){
930 *pDifferentRow = 1;
931 return rc;
933 if( pCur->eState!=CURSOR_VALID ){
934 *pDifferentRow = 1;
935 }else{
936 *pDifferentRow = 0;
938 return SQLITE_OK;
941 #ifdef SQLITE_ENABLE_CURSOR_HINTS
943 ** Provide hints to the cursor. The particular hint given (and the type
944 ** and number of the varargs parameters) is determined by the eHintType
945 ** parameter. See the definitions of the BTREE_HINT_* macros for details.
947 void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
948 /* Used only by system that substitute their own storage engine */
949 #ifdef SQLITE_DEBUG
950 if( ALWAYS(eHintType==BTREE_HINT_RANGE) ){
951 va_list ap;
952 Expr *pExpr;
953 Walker w;
954 memset(&w, 0, sizeof(w));
955 w.xExprCallback = sqlite3CursorRangeHintExprCheck;
956 va_start(ap, eHintType);
957 pExpr = va_arg(ap, Expr*);
958 w.u.aMem = va_arg(ap, Mem*);
959 va_end(ap);
960 assert( pExpr!=0 );
961 assert( w.u.aMem!=0 );
962 sqlite3WalkExpr(&w, pExpr);
964 #endif /* SQLITE_DEBUG */
966 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
970 ** Provide flag hints to the cursor.
972 void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
973 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
974 pCur->hints = x;
978 #ifndef SQLITE_OMIT_AUTOVACUUM
980 ** Given a page number of a regular database page, return the page
981 ** number for the pointer-map page that contains the entry for the
982 ** input page number.
984 ** Return 0 (not a valid page) for pgno==1 since there is
985 ** no pointer map associated with page 1. The integrity_check logic
986 ** requires that ptrmapPageno(*,1)!=1.
988 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
989 int nPagesPerMapPage;
990 Pgno iPtrMap, ret;
991 assert( sqlite3_mutex_held(pBt->mutex) );
992 if( pgno<2 ) return 0;
993 nPagesPerMapPage = (pBt->usableSize/5)+1;
994 iPtrMap = (pgno-2)/nPagesPerMapPage;
995 ret = (iPtrMap*nPagesPerMapPage) + 2;
996 if( ret==PENDING_BYTE_PAGE(pBt) ){
997 ret++;
999 return ret;
1003 ** Write an entry into the pointer map.
1005 ** This routine updates the pointer map entry for page number 'key'
1006 ** so that it maps to type 'eType' and parent page number 'pgno'.
1008 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
1009 ** a no-op. If an error occurs, the appropriate error code is written
1010 ** into *pRC.
1012 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
1013 DbPage *pDbPage; /* The pointer map page */
1014 u8 *pPtrmap; /* The pointer map data */
1015 Pgno iPtrmap; /* The pointer map page number */
1016 int offset; /* Offset in pointer map page */
1017 int rc; /* Return code from subfunctions */
1019 if( *pRC ) return;
1021 assert( sqlite3_mutex_held(pBt->mutex) );
1022 /* The super-journal page number must never be used as a pointer map page */
1023 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
1025 assert( pBt->autoVacuum );
1026 if( key==0 ){
1027 *pRC = SQLITE_CORRUPT_BKPT;
1028 return;
1030 iPtrmap = PTRMAP_PAGENO(pBt, key);
1031 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
1032 if( rc!=SQLITE_OK ){
1033 *pRC = rc;
1034 return;
1036 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
1037 /* The first byte of the extra data is the MemPage.isInit byte.
1038 ** If that byte is set, it means this page is also being used
1039 ** as a btree page. */
1040 *pRC = SQLITE_CORRUPT_BKPT;
1041 goto ptrmap_exit;
1043 offset = PTRMAP_PTROFFSET(iPtrmap, key);
1044 if( offset<0 ){
1045 *pRC = SQLITE_CORRUPT_BKPT;
1046 goto ptrmap_exit;
1048 assert( offset <= (int)pBt->usableSize-5 );
1049 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1051 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
1052 TRACE(("PTRMAP_UPDATE: %u->(%u,%u)\n", key, eType, parent));
1053 *pRC= rc = sqlite3PagerWrite(pDbPage);
1054 if( rc==SQLITE_OK ){
1055 pPtrmap[offset] = eType;
1056 put4byte(&pPtrmap[offset+1], parent);
1060 ptrmap_exit:
1061 sqlite3PagerUnref(pDbPage);
1065 ** Read an entry from the pointer map.
1067 ** This routine retrieves the pointer map entry for page 'key', writing
1068 ** the type and parent page number to *pEType and *pPgno respectively.
1069 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
1071 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
1072 DbPage *pDbPage; /* The pointer map page */
1073 int iPtrmap; /* Pointer map page index */
1074 u8 *pPtrmap; /* Pointer map page data */
1075 int offset; /* Offset of entry in pointer map */
1076 int rc;
1078 assert( sqlite3_mutex_held(pBt->mutex) );
1080 iPtrmap = PTRMAP_PAGENO(pBt, key);
1081 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
1082 if( rc!=0 ){
1083 return rc;
1085 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1087 offset = PTRMAP_PTROFFSET(iPtrmap, key);
1088 if( offset<0 ){
1089 sqlite3PagerUnref(pDbPage);
1090 return SQLITE_CORRUPT_BKPT;
1092 assert( offset <= (int)pBt->usableSize-5 );
1093 assert( pEType!=0 );
1094 *pEType = pPtrmap[offset];
1095 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
1097 sqlite3PagerUnref(pDbPage);
1098 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
1099 return SQLITE_OK;
1102 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
1103 #define ptrmapPut(w,x,y,z,rc)
1104 #define ptrmapGet(w,x,y,z) SQLITE_OK
1105 #define ptrmapPutOvflPtr(x, y, z, rc)
1106 #endif
1109 ** Given a btree page and a cell index (0 means the first cell on
1110 ** the page, 1 means the second cell, and so forth) return a pointer
1111 ** to the cell content.
1113 ** findCellPastPtr() does the same except it skips past the initial
1114 ** 4-byte child pointer found on interior pages, if there is one.
1116 ** This routine works only for pages that do not contain overflow cells.
1118 #define findCell(P,I) \
1119 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1120 #define findCellPastPtr(P,I) \
1121 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1125 ** This is common tail processing for btreeParseCellPtr() and
1126 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
1127 ** on a single B-tree page. Make necessary adjustments to the CellInfo
1128 ** structure.
1130 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
1131 MemPage *pPage, /* Page containing the cell */
1132 u8 *pCell, /* Pointer to the cell text. */
1133 CellInfo *pInfo /* Fill in this structure */
1135 /* If the payload will not fit completely on the local page, we have
1136 ** to decide how much to store locally and how much to spill onto
1137 ** overflow pages. The strategy is to minimize the amount of unused
1138 ** space on overflow pages while keeping the amount of local storage
1139 ** in between minLocal and maxLocal.
1141 ** Warning: changing the way overflow payload is distributed in any
1142 ** way will result in an incompatible file format.
1144 int minLocal; /* Minimum amount of payload held locally */
1145 int maxLocal; /* Maximum amount of payload held locally */
1146 int surplus; /* Overflow payload available for local storage */
1148 minLocal = pPage->minLocal;
1149 maxLocal = pPage->maxLocal;
1150 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
1151 testcase( surplus==maxLocal );
1152 testcase( surplus==maxLocal+1 );
1153 if( surplus <= maxLocal ){
1154 pInfo->nLocal = (u16)surplus;
1155 }else{
1156 pInfo->nLocal = (u16)minLocal;
1158 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
1162 ** Given a record with nPayload bytes of payload stored within btree
1163 ** page pPage, return the number of bytes of payload stored locally.
1165 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
1166 int maxLocal; /* Maximum amount of payload held locally */
1167 maxLocal = pPage->maxLocal;
1168 if( nPayload<=maxLocal ){
1169 return nPayload;
1170 }else{
1171 int minLocal; /* Minimum amount of payload held locally */
1172 int surplus; /* Overflow payload available for local storage */
1173 minLocal = pPage->minLocal;
1174 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
1175 return ( surplus <= maxLocal ) ? surplus : minLocal;
1180 ** The following routines are implementations of the MemPage.xParseCell()
1181 ** method.
1183 ** Parse a cell content block and fill in the CellInfo structure.
1185 ** btreeParseCellPtr() => table btree leaf nodes
1186 ** btreeParseCellNoPayload() => table btree internal nodes
1187 ** btreeParseCellPtrIndex() => index btree nodes
1189 ** There is also a wrapper function btreeParseCell() that works for
1190 ** all MemPage types and that references the cell by index rather than
1191 ** by pointer.
1193 static void btreeParseCellPtrNoPayload(
1194 MemPage *pPage, /* Page containing the cell */
1195 u8 *pCell, /* Pointer to the cell text. */
1196 CellInfo *pInfo /* Fill in this structure */
1198 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1199 assert( pPage->leaf==0 );
1200 assert( pPage->childPtrSize==4 );
1201 #ifndef SQLITE_DEBUG
1202 UNUSED_PARAMETER(pPage);
1203 #endif
1204 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
1205 pInfo->nPayload = 0;
1206 pInfo->nLocal = 0;
1207 pInfo->pPayload = 0;
1208 return;
1210 static void btreeParseCellPtr(
1211 MemPage *pPage, /* Page containing the cell */
1212 u8 *pCell, /* Pointer to the cell text. */
1213 CellInfo *pInfo /* Fill in this structure */
1215 u8 *pIter; /* For scanning through pCell */
1216 u32 nPayload; /* Number of bytes of cell payload */
1217 u64 iKey; /* Extracted Key value */
1219 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1220 assert( pPage->leaf==0 || pPage->leaf==1 );
1221 assert( pPage->intKeyLeaf );
1222 assert( pPage->childPtrSize==0 );
1223 pIter = pCell;
1225 /* The next block of code is equivalent to:
1227 ** pIter += getVarint32(pIter, nPayload);
1229 ** The code is inlined to avoid a function call.
1231 nPayload = *pIter;
1232 if( nPayload>=0x80 ){
1233 u8 *pEnd = &pIter[8];
1234 nPayload &= 0x7f;
1236 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1237 }while( (*pIter)>=0x80 && pIter<pEnd );
1239 pIter++;
1241 /* The next block of code is equivalent to:
1243 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
1245 ** The code is inlined and the loop is unrolled for performance.
1246 ** This routine is a high-runner.
1248 iKey = *pIter;
1249 if( iKey>=0x80 ){
1250 u8 x;
1251 iKey = (iKey<<7) ^ (x = *++pIter);
1252 if( x>=0x80 ){
1253 iKey = (iKey<<7) ^ (x = *++pIter);
1254 if( x>=0x80 ){
1255 iKey = (iKey<<7) ^ 0x10204000 ^ (x = *++pIter);
1256 if( x>=0x80 ){
1257 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
1258 if( x>=0x80 ){
1259 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
1260 if( x>=0x80 ){
1261 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
1262 if( x>=0x80 ){
1263 iKey = (iKey<<7) ^ 0x4000 ^ (x = *++pIter);
1264 if( x>=0x80 ){
1265 iKey = (iKey<<8) ^ 0x8000 ^ (*++pIter);
1271 }else{
1272 iKey ^= 0x204000;
1274 }else{
1275 iKey ^= 0x4000;
1278 pIter++;
1280 pInfo->nKey = *(i64*)&iKey;
1281 pInfo->nPayload = nPayload;
1282 pInfo->pPayload = pIter;
1283 testcase( nPayload==pPage->maxLocal );
1284 testcase( nPayload==(u32)pPage->maxLocal+1 );
1285 if( nPayload<=pPage->maxLocal ){
1286 /* This is the (easy) common case where the entire payload fits
1287 ** on the local page. No overflow is required.
1289 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1290 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1291 pInfo->nLocal = (u16)nPayload;
1292 }else{
1293 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1296 static void btreeParseCellPtrIndex(
1297 MemPage *pPage, /* Page containing the cell */
1298 u8 *pCell, /* Pointer to the cell text. */
1299 CellInfo *pInfo /* Fill in this structure */
1301 u8 *pIter; /* For scanning through pCell */
1302 u32 nPayload; /* Number of bytes of cell payload */
1304 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1305 assert( pPage->leaf==0 || pPage->leaf==1 );
1306 assert( pPage->intKeyLeaf==0 );
1307 pIter = pCell + pPage->childPtrSize;
1308 nPayload = *pIter;
1309 if( nPayload>=0x80 ){
1310 u8 *pEnd = &pIter[8];
1311 nPayload &= 0x7f;
1313 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1314 }while( *(pIter)>=0x80 && pIter<pEnd );
1316 pIter++;
1317 pInfo->nKey = nPayload;
1318 pInfo->nPayload = nPayload;
1319 pInfo->pPayload = pIter;
1320 testcase( nPayload==pPage->maxLocal );
1321 testcase( nPayload==(u32)pPage->maxLocal+1 );
1322 if( nPayload<=pPage->maxLocal ){
1323 /* This is the (easy) common case where the entire payload fits
1324 ** on the local page. No overflow is required.
1326 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1327 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1328 pInfo->nLocal = (u16)nPayload;
1329 }else{
1330 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1333 static void btreeParseCell(
1334 MemPage *pPage, /* Page containing the cell */
1335 int iCell, /* The cell index. First cell is 0 */
1336 CellInfo *pInfo /* Fill in this structure */
1338 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
1342 ** The following routines are implementations of the MemPage.xCellSize
1343 ** method.
1345 ** Compute the total number of bytes that a Cell needs in the cell
1346 ** data area of the btree-page. The return number includes the cell
1347 ** data header and the local payload, but not any overflow page or
1348 ** the space used by the cell pointer.
1350 ** cellSizePtrNoPayload() => table internal nodes
1351 ** cellSizePtrTableLeaf() => table leaf nodes
1352 ** cellSizePtr() => index internal nodes
1353 ** cellSizeIdxLeaf() => index leaf nodes
1355 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
1356 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1357 u8 *pEnd; /* End mark for a varint */
1358 u32 nSize; /* Size value to return */
1360 #ifdef SQLITE_DEBUG
1361 /* The value returned by this function should always be the same as
1362 ** the (CellInfo.nSize) value found by doing a full parse of the
1363 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1364 ** this function verifies that this invariant is not violated. */
1365 CellInfo debuginfo;
1366 pPage->xParseCell(pPage, pCell, &debuginfo);
1367 #endif
1369 assert( pPage->childPtrSize==4 );
1370 nSize = *pIter;
1371 if( nSize>=0x80 ){
1372 pEnd = &pIter[8];
1373 nSize &= 0x7f;
1375 nSize = (nSize<<7) | (*++pIter & 0x7f);
1376 }while( *(pIter)>=0x80 && pIter<pEnd );
1378 pIter++;
1379 testcase( nSize==pPage->maxLocal );
1380 testcase( nSize==(u32)pPage->maxLocal+1 );
1381 if( nSize<=pPage->maxLocal ){
1382 nSize += (u32)(pIter - pCell);
1383 assert( nSize>4 );
1384 }else{
1385 int minLocal = pPage->minLocal;
1386 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1387 testcase( nSize==pPage->maxLocal );
1388 testcase( nSize==(u32)pPage->maxLocal+1 );
1389 if( nSize>pPage->maxLocal ){
1390 nSize = minLocal;
1392 nSize += 4 + (u16)(pIter - pCell);
1394 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1395 return (u16)nSize;
1397 static u16 cellSizePtrIdxLeaf(MemPage *pPage, u8 *pCell){
1398 u8 *pIter = pCell; /* For looping over bytes of pCell */
1399 u8 *pEnd; /* End mark for a varint */
1400 u32 nSize; /* Size value to return */
1402 #ifdef SQLITE_DEBUG
1403 /* The value returned by this function should always be the same as
1404 ** the (CellInfo.nSize) value found by doing a full parse of the
1405 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1406 ** this function verifies that this invariant is not violated. */
1407 CellInfo debuginfo;
1408 pPage->xParseCell(pPage, pCell, &debuginfo);
1409 #endif
1411 assert( pPage->childPtrSize==0 );
1412 nSize = *pIter;
1413 if( nSize>=0x80 ){
1414 pEnd = &pIter[8];
1415 nSize &= 0x7f;
1417 nSize = (nSize<<7) | (*++pIter & 0x7f);
1418 }while( *(pIter)>=0x80 && pIter<pEnd );
1420 pIter++;
1421 testcase( nSize==pPage->maxLocal );
1422 testcase( nSize==(u32)pPage->maxLocal+1 );
1423 if( nSize<=pPage->maxLocal ){
1424 nSize += (u32)(pIter - pCell);
1425 if( nSize<4 ) nSize = 4;
1426 }else{
1427 int minLocal = pPage->minLocal;
1428 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1429 testcase( nSize==pPage->maxLocal );
1430 testcase( nSize==(u32)pPage->maxLocal+1 );
1431 if( nSize>pPage->maxLocal ){
1432 nSize = minLocal;
1434 nSize += 4 + (u16)(pIter - pCell);
1436 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1437 return (u16)nSize;
1439 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
1440 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1441 u8 *pEnd; /* End mark for a varint */
1443 #ifdef SQLITE_DEBUG
1444 /* The value returned by this function should always be the same as
1445 ** the (CellInfo.nSize) value found by doing a full parse of the
1446 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1447 ** this function verifies that this invariant is not violated. */
1448 CellInfo debuginfo;
1449 pPage->xParseCell(pPage, pCell, &debuginfo);
1450 #else
1451 UNUSED_PARAMETER(pPage);
1452 #endif
1454 assert( pPage->childPtrSize==4 );
1455 pEnd = pIter + 9;
1456 while( (*pIter++)&0x80 && pIter<pEnd );
1457 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
1458 return (u16)(pIter - pCell);
1460 static u16 cellSizePtrTableLeaf(MemPage *pPage, u8 *pCell){
1461 u8 *pIter = pCell; /* For looping over bytes of pCell */
1462 u8 *pEnd; /* End mark for a varint */
1463 u32 nSize; /* Size value to return */
1465 #ifdef SQLITE_DEBUG
1466 /* The value returned by this function should always be the same as
1467 ** the (CellInfo.nSize) value found by doing a full parse of the
1468 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1469 ** this function verifies that this invariant is not violated. */
1470 CellInfo debuginfo;
1471 pPage->xParseCell(pPage, pCell, &debuginfo);
1472 #endif
1474 nSize = *pIter;
1475 if( nSize>=0x80 ){
1476 pEnd = &pIter[8];
1477 nSize &= 0x7f;
1479 nSize = (nSize<<7) | (*++pIter & 0x7f);
1480 }while( *(pIter)>=0x80 && pIter<pEnd );
1482 pIter++;
1483 /* pIter now points at the 64-bit integer key value, a variable length
1484 ** integer. The following block moves pIter to point at the first byte
1485 ** past the end of the key value. */
1486 if( (*pIter++)&0x80
1487 && (*pIter++)&0x80
1488 && (*pIter++)&0x80
1489 && (*pIter++)&0x80
1490 && (*pIter++)&0x80
1491 && (*pIter++)&0x80
1492 && (*pIter++)&0x80
1493 && (*pIter++)&0x80 ){ pIter++; }
1494 testcase( nSize==pPage->maxLocal );
1495 testcase( nSize==(u32)pPage->maxLocal+1 );
1496 if( nSize<=pPage->maxLocal ){
1497 nSize += (u32)(pIter - pCell);
1498 if( nSize<4 ) nSize = 4;
1499 }else{
1500 int minLocal = pPage->minLocal;
1501 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1502 testcase( nSize==pPage->maxLocal );
1503 testcase( nSize==(u32)pPage->maxLocal+1 );
1504 if( nSize>pPage->maxLocal ){
1505 nSize = minLocal;
1507 nSize += 4 + (u16)(pIter - pCell);
1509 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1510 return (u16)nSize;
1514 #ifdef SQLITE_DEBUG
1515 /* This variation on cellSizePtr() is used inside of assert() statements
1516 ** only. */
1517 static u16 cellSize(MemPage *pPage, int iCell){
1518 return pPage->xCellSize(pPage, findCell(pPage, iCell));
1520 #endif
1522 #ifndef SQLITE_OMIT_AUTOVACUUM
1524 ** The cell pCell is currently part of page pSrc but will ultimately be part
1525 ** of pPage. (pSrc and pPage are often the same.) If pCell contains a
1526 ** pointer to an overflow page, insert an entry into the pointer-map for
1527 ** the overflow page that will be valid after pCell has been moved to pPage.
1529 static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
1530 CellInfo info;
1531 if( *pRC ) return;
1532 assert( pCell!=0 );
1533 pPage->xParseCell(pPage, pCell, &info);
1534 if( info.nLocal<info.nPayload ){
1535 Pgno ovfl;
1536 if( SQLITE_OVERFLOW(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){
1537 testcase( pSrc!=pPage );
1538 *pRC = SQLITE_CORRUPT_BKPT;
1539 return;
1541 ovfl = get4byte(&pCell[info.nSize-4]);
1542 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
1545 #endif
1549 ** Defragment the page given. This routine reorganizes cells within the
1550 ** page so that there are no free-blocks on the free-block list.
1552 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be
1553 ** present in the page after this routine returns.
1555 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
1556 ** b-tree page so that there are no freeblocks or fragment bytes, all
1557 ** unused bytes are contained in the unallocated space region, and all
1558 ** cells are packed tightly at the end of the page.
1560 static int defragmentPage(MemPage *pPage, int nMaxFrag){
1561 int i; /* Loop counter */
1562 int pc; /* Address of the i-th cell */
1563 int hdr; /* Offset to the page header */
1564 int size; /* Size of a cell */
1565 int usableSize; /* Number of usable bytes on a page */
1566 int cellOffset; /* Offset to the cell pointer array */
1567 int cbrk; /* Offset to the cell content area */
1568 int nCell; /* Number of cells on the page */
1569 unsigned char *data; /* The page data */
1570 unsigned char *temp; /* Temp area for cell content */
1571 unsigned char *src; /* Source of content */
1572 int iCellFirst; /* First allowable cell index */
1573 int iCellLast; /* Last possible cell index */
1574 int iCellStart; /* First cell offset in input */
1576 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1577 assert( pPage->pBt!=0 );
1578 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1579 assert( pPage->nOverflow==0 );
1580 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1581 data = pPage->aData;
1582 hdr = pPage->hdrOffset;
1583 cellOffset = pPage->cellOffset;
1584 nCell = pPage->nCell;
1585 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB );
1586 iCellFirst = cellOffset + 2*nCell;
1587 usableSize = pPage->pBt->usableSize;
1589 /* This block handles pages with two or fewer free blocks and nMaxFrag
1590 ** or fewer fragmented bytes. In this case it is faster to move the
1591 ** two (or one) blocks of cells using memmove() and add the required
1592 ** offsets to each pointer in the cell-pointer array than it is to
1593 ** reconstruct the entire page. */
1594 if( (int)data[hdr+7]<=nMaxFrag ){
1595 int iFree = get2byte(&data[hdr+1]);
1596 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
1597 if( iFree ){
1598 int iFree2 = get2byte(&data[iFree]);
1599 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage);
1600 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
1601 u8 *pEnd = &data[cellOffset + nCell*2];
1602 u8 *pAddr;
1603 int sz2 = 0;
1604 int sz = get2byte(&data[iFree+2]);
1605 int top = get2byte(&data[hdr+5]);
1606 if( top>=iFree ){
1607 return SQLITE_CORRUPT_PAGE(pPage);
1609 if( iFree2 ){
1610 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage);
1611 sz2 = get2byte(&data[iFree2+2]);
1612 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage);
1613 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
1614 sz += sz2;
1615 }else if( iFree+sz>usableSize ){
1616 return SQLITE_CORRUPT_PAGE(pPage);
1619 cbrk = top+sz;
1620 assert( cbrk+(iFree-top) <= usableSize );
1621 memmove(&data[cbrk], &data[top], iFree-top);
1622 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
1623 pc = get2byte(pAddr);
1624 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
1625 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
1627 goto defragment_out;
1632 cbrk = usableSize;
1633 iCellLast = usableSize - 4;
1634 iCellStart = get2byte(&data[hdr+5]);
1635 if( nCell>0 ){
1636 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
1637 memcpy(temp, data, usableSize);
1638 src = temp;
1639 for(i=0; i<nCell; i++){
1640 u8 *pAddr; /* The i-th cell pointer */
1641 pAddr = &data[cellOffset + i*2];
1642 pc = get2byte(pAddr);
1643 testcase( pc==iCellFirst );
1644 testcase( pc==iCellLast );
1645 /* These conditions have already been verified in btreeInitPage()
1646 ** if PRAGMA cell_size_check=ON.
1648 if( pc>iCellLast ){
1649 return SQLITE_CORRUPT_PAGE(pPage);
1651 assert( pc>=0 && pc<=iCellLast );
1652 size = pPage->xCellSize(pPage, &src[pc]);
1653 cbrk -= size;
1654 if( cbrk<iCellStart || pc+size>usableSize ){
1655 return SQLITE_CORRUPT_PAGE(pPage);
1657 assert( cbrk+size<=usableSize && cbrk>=iCellStart );
1658 testcase( cbrk+size==usableSize );
1659 testcase( pc+size==usableSize );
1660 put2byte(pAddr, cbrk);
1661 memcpy(&data[cbrk], &src[pc], size);
1664 data[hdr+7] = 0;
1666 defragment_out:
1667 assert( pPage->nFree>=0 );
1668 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
1669 return SQLITE_CORRUPT_PAGE(pPage);
1671 assert( cbrk>=iCellFirst );
1672 put2byte(&data[hdr+5], cbrk);
1673 data[hdr+1] = 0;
1674 data[hdr+2] = 0;
1675 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
1676 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1677 return SQLITE_OK;
1681 ** Search the free-list on page pPg for space to store a cell nByte bytes in
1682 ** size. If one can be found, return a pointer to the space and remove it
1683 ** from the free-list.
1685 ** If no suitable space can be found on the free-list, return NULL.
1687 ** This function may detect corruption within pPg. If corruption is
1688 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
1690 ** Slots on the free list that are between 1 and 3 bytes larger than nByte
1691 ** will be ignored if adding the extra space to the fragmentation count
1692 ** causes the fragmentation count to exceed 60.
1694 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
1695 const int hdr = pPg->hdrOffset; /* Offset to page header */
1696 u8 * const aData = pPg->aData; /* Page data */
1697 int iAddr = hdr + 1; /* Address of ptr to pc */
1698 u8 *pTmp = &aData[iAddr]; /* Temporary ptr into aData[] */
1699 int pc = get2byte(pTmp); /* Address of a free slot */
1700 int x; /* Excess size of the slot */
1701 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */
1702 int size; /* Size of the free slot */
1704 assert( pc>0 );
1705 while( pc<=maxPC ){
1706 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
1707 ** freeblock form a big-endian integer which is the size of the freeblock
1708 ** in bytes, including the 4-byte header. */
1709 pTmp = &aData[pc+2];
1710 size = get2byte(pTmp);
1711 if( (x = size - nByte)>=0 ){
1712 testcase( x==4 );
1713 testcase( x==3 );
1714 if( x<4 ){
1715 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
1716 ** number of bytes in fragments may not exceed 60. */
1717 if( aData[hdr+7]>57 ) return 0;
1719 /* Remove the slot from the free-list. Update the number of
1720 ** fragmented bytes within the page. */
1721 memcpy(&aData[iAddr], &aData[pc], 2);
1722 aData[hdr+7] += (u8)x;
1723 return &aData[pc];
1724 }else if( x+pc > maxPC ){
1725 /* This slot extends off the end of the usable part of the page */
1726 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1727 return 0;
1728 }else{
1729 /* The slot remains on the free-list. Reduce its size to account
1730 ** for the portion used by the new allocation. */
1731 put2byte(&aData[pc+2], x);
1733 return &aData[pc + x];
1735 iAddr = pc;
1736 pTmp = &aData[pc];
1737 pc = get2byte(pTmp);
1738 if( pc<=iAddr ){
1739 if( pc ){
1740 /* The next slot in the chain comes before the current slot */
1741 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1743 return 0;
1746 if( pc>maxPC+nByte-4 ){
1747 /* The free slot chain extends off the end of the page */
1748 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1750 return 0;
1754 ** Allocate nByte bytes of space from within the B-Tree page passed
1755 ** as the first argument. Write into *pIdx the index into pPage->aData[]
1756 ** of the first byte of allocated space. Return either SQLITE_OK or
1757 ** an error code (usually SQLITE_CORRUPT).
1759 ** The caller guarantees that there is sufficient space to make the
1760 ** allocation. This routine might need to defragment in order to bring
1761 ** all the space together, however. This routine will avoid using
1762 ** the first two bytes past the cell pointer area since presumably this
1763 ** allocation is being made in order to insert a new cell, so we will
1764 ** also end up needing a new cell pointer.
1766 static SQLITE_INLINE int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
1767 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1768 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
1769 int top; /* First byte of cell content area */
1770 int rc = SQLITE_OK; /* Integer return code */
1771 u8 *pTmp; /* Temp ptr into data[] */
1772 int gap; /* First byte of gap between cell pointers and cell content */
1774 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1775 assert( pPage->pBt );
1776 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1777 assert( nByte>=0 ); /* Minimum cell size is 4 */
1778 assert( pPage->nFree>=nByte );
1779 assert( pPage->nOverflow==0 );
1780 assert( nByte < (int)(pPage->pBt->usableSize-8) );
1782 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1783 gap = pPage->cellOffset + 2*pPage->nCell;
1784 assert( gap<=65536 );
1785 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
1786 ** and the reserved space is zero (the usual value for reserved space)
1787 ** then the cell content offset of an empty page wants to be 65536.
1788 ** However, that integer is too large to be stored in a 2-byte unsigned
1789 ** integer, so a value of 0 is used in its place. */
1790 pTmp = &data[hdr+5];
1791 top = get2byte(pTmp);
1792 if( gap>top ){
1793 if( top==0 && pPage->pBt->usableSize==65536 ){
1794 top = 65536;
1795 }else{
1796 return SQLITE_CORRUPT_PAGE(pPage);
1798 }else if( top>(int)pPage->pBt->usableSize ){
1799 return SQLITE_CORRUPT_PAGE(pPage);
1802 /* If there is enough space between gap and top for one more cell pointer,
1803 ** and if the freelist is not empty, then search the
1804 ** freelist looking for a slot big enough to satisfy the request.
1806 testcase( gap+2==top );
1807 testcase( gap+1==top );
1808 testcase( gap==top );
1809 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
1810 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
1811 if( pSpace ){
1812 int g2;
1813 assert( pSpace+nByte<=data+pPage->pBt->usableSize );
1814 *pIdx = g2 = (int)(pSpace-data);
1815 if( g2<=gap ){
1816 return SQLITE_CORRUPT_PAGE(pPage);
1817 }else{
1818 return SQLITE_OK;
1820 }else if( rc ){
1821 return rc;
1825 /* The request could not be fulfilled using a freelist slot. Check
1826 ** to see if defragmentation is necessary.
1828 testcase( gap+2+nByte==top );
1829 if( gap+2+nByte>top ){
1830 assert( pPage->nCell>0 || CORRUPT_DB );
1831 assert( pPage->nFree>=0 );
1832 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
1833 if( rc ) return rc;
1834 top = get2byteNotZero(&data[hdr+5]);
1835 assert( gap+2+nByte<=top );
1839 /* Allocate memory from the gap in between the cell pointer array
1840 ** and the cell content area. The btreeComputeFreeSpace() call has already
1841 ** validated the freelist. Given that the freelist is valid, there
1842 ** is no way that the allocation can extend off the end of the page.
1843 ** The assert() below verifies the previous sentence.
1845 top -= nByte;
1846 put2byte(&data[hdr+5], top);
1847 assert( top+nByte <= (int)pPage->pBt->usableSize );
1848 *pIdx = top;
1849 return SQLITE_OK;
1853 ** Return a section of the pPage->aData to the freelist.
1854 ** The first byte of the new free block is pPage->aData[iStart]
1855 ** and the size of the block is iSize bytes.
1857 ** Adjacent freeblocks are coalesced.
1859 ** Even though the freeblock list was checked by btreeComputeFreeSpace(),
1860 ** that routine will not detect overlap between cells or freeblocks. Nor
1861 ** does it detect cells or freeblocks that encroach into the reserved bytes
1862 ** at the end of the page. So do additional corruption checks inside this
1863 ** routine and return SQLITE_CORRUPT if any problems are found.
1865 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
1866 u16 iPtr; /* Address of ptr to next freeblock */
1867 u16 iFreeBlk; /* Address of the next freeblock */
1868 u8 hdr; /* Page header size. 0 or 100 */
1869 u8 nFrag = 0; /* Reduction in fragmentation */
1870 u16 iOrigSize = iSize; /* Original value of iSize */
1871 u16 x; /* Offset to cell content area */
1872 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
1873 unsigned char *data = pPage->aData; /* Page content */
1874 u8 *pTmp; /* Temporary ptr into data[] */
1876 assert( pPage->pBt!=0 );
1877 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1878 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
1879 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
1880 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1881 assert( iSize>=4 ); /* Minimum cell size is 4 */
1882 assert( CORRUPT_DB || iStart<=pPage->pBt->usableSize-4 );
1884 /* The list of freeblocks must be in ascending order. Find the
1885 ** spot on the list where iStart should be inserted.
1887 hdr = pPage->hdrOffset;
1888 iPtr = hdr + 1;
1889 if( data[iPtr+1]==0 && data[iPtr]==0 ){
1890 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
1891 }else{
1892 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
1893 if( iFreeBlk<=iPtr ){
1894 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
1895 return SQLITE_CORRUPT_PAGE(pPage);
1897 iPtr = iFreeBlk;
1899 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
1900 return SQLITE_CORRUPT_PAGE(pPage);
1902 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
1904 /* At this point:
1905 ** iFreeBlk: First freeblock after iStart, or zero if none
1906 ** iPtr: The address of a pointer to iFreeBlk
1908 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
1910 if( iFreeBlk && iEnd+3>=iFreeBlk ){
1911 nFrag = iFreeBlk - iEnd;
1912 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
1913 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
1914 if( iEnd > pPage->pBt->usableSize ){
1915 return SQLITE_CORRUPT_PAGE(pPage);
1917 iSize = iEnd - iStart;
1918 iFreeBlk = get2byte(&data[iFreeBlk]);
1921 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
1922 ** pointer in the page header) then check to see if iStart should be
1923 ** coalesced onto the end of iPtr.
1925 if( iPtr>hdr+1 ){
1926 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
1927 if( iPtrEnd+3>=iStart ){
1928 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
1929 nFrag += iStart - iPtrEnd;
1930 iSize = iEnd - iPtr;
1931 iStart = iPtr;
1934 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
1935 data[hdr+7] -= nFrag;
1937 pTmp = &data[hdr+5];
1938 x = get2byte(pTmp);
1939 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
1940 /* Overwrite deleted information with zeros when the secure_delete
1941 ** option is enabled */
1942 memset(&data[iStart], 0, iSize);
1944 if( iStart<=x ){
1945 /* The new freeblock is at the beginning of the cell content area,
1946 ** so just extend the cell content area rather than create another
1947 ** freelist entry */
1948 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage);
1949 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
1950 put2byte(&data[hdr+1], iFreeBlk);
1951 put2byte(&data[hdr+5], iEnd);
1952 }else{
1953 /* Insert the new freeblock into the freelist */
1954 put2byte(&data[iPtr], iStart);
1955 put2byte(&data[iStart], iFreeBlk);
1956 put2byte(&data[iStart+2], iSize);
1958 pPage->nFree += iOrigSize;
1959 return SQLITE_OK;
1963 ** Decode the flags byte (the first byte of the header) for a page
1964 ** and initialize fields of the MemPage structure accordingly.
1966 ** Only the following combinations are supported. Anything different
1967 ** indicates a corrupt database files:
1969 ** PTF_ZERODATA (0x02, 2)
1970 ** PTF_LEAFDATA | PTF_INTKEY (0x05, 5)
1971 ** PTF_ZERODATA | PTF_LEAF (0x0a, 10)
1972 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF (0x0d, 13)
1974 static int decodeFlags(MemPage *pPage, int flagByte){
1975 BtShared *pBt; /* A copy of pPage->pBt */
1977 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1978 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1979 pBt = pPage->pBt;
1980 pPage->max1bytePayload = pBt->max1bytePayload;
1981 if( flagByte>=(PTF_ZERODATA | PTF_LEAF) ){
1982 pPage->childPtrSize = 0;
1983 pPage->leaf = 1;
1984 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF) ){
1985 pPage->intKeyLeaf = 1;
1986 pPage->xCellSize = cellSizePtrTableLeaf;
1987 pPage->xParseCell = btreeParseCellPtr;
1988 pPage->intKey = 1;
1989 pPage->maxLocal = pBt->maxLeaf;
1990 pPage->minLocal = pBt->minLeaf;
1991 }else if( flagByte==(PTF_ZERODATA | PTF_LEAF) ){
1992 pPage->intKey = 0;
1993 pPage->intKeyLeaf = 0;
1994 pPage->xCellSize = cellSizePtrIdxLeaf;
1995 pPage->xParseCell = btreeParseCellPtrIndex;
1996 pPage->maxLocal = pBt->maxLocal;
1997 pPage->minLocal = pBt->minLocal;
1998 }else{
1999 pPage->intKey = 0;
2000 pPage->intKeyLeaf = 0;
2001 pPage->xCellSize = cellSizePtrIdxLeaf;
2002 pPage->xParseCell = btreeParseCellPtrIndex;
2003 return SQLITE_CORRUPT_PAGE(pPage);
2005 }else{
2006 pPage->childPtrSize = 4;
2007 pPage->leaf = 0;
2008 if( flagByte==(PTF_ZERODATA) ){
2009 pPage->intKey = 0;
2010 pPage->intKeyLeaf = 0;
2011 pPage->xCellSize = cellSizePtr;
2012 pPage->xParseCell = btreeParseCellPtrIndex;
2013 pPage->maxLocal = pBt->maxLocal;
2014 pPage->minLocal = pBt->minLocal;
2015 }else if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
2016 pPage->intKeyLeaf = 0;
2017 pPage->xCellSize = cellSizePtrNoPayload;
2018 pPage->xParseCell = btreeParseCellPtrNoPayload;
2019 pPage->intKey = 1;
2020 pPage->maxLocal = pBt->maxLeaf;
2021 pPage->minLocal = pBt->minLeaf;
2022 }else{
2023 pPage->intKey = 0;
2024 pPage->intKeyLeaf = 0;
2025 pPage->xCellSize = cellSizePtr;
2026 pPage->xParseCell = btreeParseCellPtrIndex;
2027 return SQLITE_CORRUPT_PAGE(pPage);
2030 return SQLITE_OK;
2034 ** Compute the amount of freespace on the page. In other words, fill
2035 ** in the pPage->nFree field.
2037 static int btreeComputeFreeSpace(MemPage *pPage){
2038 int pc; /* Address of a freeblock within pPage->aData[] */
2039 u8 hdr; /* Offset to beginning of page header */
2040 u8 *data; /* Equal to pPage->aData */
2041 int usableSize; /* Amount of usable space on each page */
2042 int nFree; /* Number of unused bytes on the page */
2043 int top; /* First byte of the cell content area */
2044 int iCellFirst; /* First allowable cell or freeblock offset */
2045 int iCellLast; /* Last possible cell or freeblock offset */
2047 assert( pPage->pBt!=0 );
2048 assert( pPage->pBt->db!=0 );
2049 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2050 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
2051 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
2052 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
2053 assert( pPage->isInit==1 );
2054 assert( pPage->nFree<0 );
2056 usableSize = pPage->pBt->usableSize;
2057 hdr = pPage->hdrOffset;
2058 data = pPage->aData;
2059 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
2060 ** the start of the cell content area. A zero value for this integer is
2061 ** interpreted as 65536. */
2062 top = get2byteNotZero(&data[hdr+5]);
2063 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
2064 iCellLast = usableSize - 4;
2066 /* Compute the total free space on the page
2067 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
2068 ** start of the first freeblock on the page, or is zero if there are no
2069 ** freeblocks. */
2070 pc = get2byte(&data[hdr+1]);
2071 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
2072 if( pc>0 ){
2073 u32 next, size;
2074 if( pc<top ){
2075 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
2076 ** always be at least one cell before the first freeblock.
2078 return SQLITE_CORRUPT_PAGE(pPage);
2080 while( 1 ){
2081 if( pc>iCellLast ){
2082 /* Freeblock off the end of the page */
2083 return SQLITE_CORRUPT_PAGE(pPage);
2085 next = get2byte(&data[pc]);
2086 size = get2byte(&data[pc+2]);
2087 nFree = nFree + size;
2088 if( next<=pc+size+3 ) break;
2089 pc = next;
2091 if( next>0 ){
2092 /* Freeblock not in ascending order */
2093 return SQLITE_CORRUPT_PAGE(pPage);
2095 if( pc+size>(unsigned int)usableSize ){
2096 /* Last freeblock extends past page end */
2097 return SQLITE_CORRUPT_PAGE(pPage);
2101 /* At this point, nFree contains the sum of the offset to the start
2102 ** of the cell-content area plus the number of free bytes within
2103 ** the cell-content area. If this is greater than the usable-size
2104 ** of the page, then the page must be corrupted. This check also
2105 ** serves to verify that the offset to the start of the cell-content
2106 ** area, according to the page header, lies within the page.
2108 if( nFree>usableSize || nFree<iCellFirst ){
2109 return SQLITE_CORRUPT_PAGE(pPage);
2111 pPage->nFree = (u16)(nFree - iCellFirst);
2112 return SQLITE_OK;
2116 ** Do additional sanity check after btreeInitPage() if
2117 ** PRAGMA cell_size_check=ON
2119 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){
2120 int iCellFirst; /* First allowable cell or freeblock offset */
2121 int iCellLast; /* Last possible cell or freeblock offset */
2122 int i; /* Index into the cell pointer array */
2123 int sz; /* Size of a cell */
2124 int pc; /* Address of a freeblock within pPage->aData[] */
2125 u8 *data; /* Equal to pPage->aData */
2126 int usableSize; /* Maximum usable space on the page */
2127 int cellOffset; /* Start of cell content area */
2129 iCellFirst = pPage->cellOffset + 2*pPage->nCell;
2130 usableSize = pPage->pBt->usableSize;
2131 iCellLast = usableSize - 4;
2132 data = pPage->aData;
2133 cellOffset = pPage->cellOffset;
2134 if( !pPage->leaf ) iCellLast--;
2135 for(i=0; i<pPage->nCell; i++){
2136 pc = get2byteAligned(&data[cellOffset+i*2]);
2137 testcase( pc==iCellFirst );
2138 testcase( pc==iCellLast );
2139 if( pc<iCellFirst || pc>iCellLast ){
2140 return SQLITE_CORRUPT_PAGE(pPage);
2142 sz = pPage->xCellSize(pPage, &data[pc]);
2143 testcase( pc+sz==usableSize );
2144 if( pc+sz>usableSize ){
2145 return SQLITE_CORRUPT_PAGE(pPage);
2148 return SQLITE_OK;
2152 ** Initialize the auxiliary information for a disk block.
2154 ** Return SQLITE_OK on success. If we see that the page does
2155 ** not contain a well-formed database page, then return
2156 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
2157 ** guarantee that the page is well-formed. It only shows that
2158 ** we failed to detect any corruption.
2160 static int btreeInitPage(MemPage *pPage){
2161 u8 *data; /* Equal to pPage->aData */
2162 BtShared *pBt; /* The main btree structure */
2164 assert( pPage->pBt!=0 );
2165 assert( pPage->pBt->db!=0 );
2166 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2167 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
2168 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
2169 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
2170 assert( pPage->isInit==0 );
2172 pBt = pPage->pBt;
2173 data = pPage->aData + pPage->hdrOffset;
2174 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
2175 ** the b-tree page type. */
2176 if( decodeFlags(pPage, data[0]) ){
2177 return SQLITE_CORRUPT_PAGE(pPage);
2179 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
2180 pPage->maskPage = (u16)(pBt->pageSize - 1);
2181 pPage->nOverflow = 0;
2182 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
2183 pPage->aCellIdx = data + pPage->childPtrSize + 8;
2184 pPage->aDataEnd = pPage->aData + pBt->pageSize;
2185 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
2186 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
2187 ** number of cells on the page. */
2188 pPage->nCell = get2byte(&data[3]);
2189 if( pPage->nCell>MX_CELL(pBt) ){
2190 /* To many cells for a single page. The page must be corrupt */
2191 return SQLITE_CORRUPT_PAGE(pPage);
2193 testcase( pPage->nCell==MX_CELL(pBt) );
2194 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
2195 ** possible for a root page of a table that contains no rows) then the
2196 ** offset to the cell content area will equal the page size minus the
2197 ** bytes of reserved space. */
2198 assert( pPage->nCell>0
2199 || get2byteNotZero(&data[5])==(int)pBt->usableSize
2200 || CORRUPT_DB );
2201 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */
2202 pPage->isInit = 1;
2203 if( pBt->db->flags & SQLITE_CellSizeCk ){
2204 return btreeCellSizeCheck(pPage);
2206 return SQLITE_OK;
2210 ** Set up a raw page so that it looks like a database page holding
2211 ** no entries.
2213 static void zeroPage(MemPage *pPage, int flags){
2214 unsigned char *data = pPage->aData;
2215 BtShared *pBt = pPage->pBt;
2216 u8 hdr = pPage->hdrOffset;
2217 u16 first;
2219 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
2220 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2221 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
2222 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
2223 assert( sqlite3_mutex_held(pBt->mutex) );
2224 if( pBt->btsFlags & BTS_FAST_SECURE ){
2225 memset(&data[hdr], 0, pBt->usableSize - hdr);
2227 data[hdr] = (char)flags;
2228 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
2229 memset(&data[hdr+1], 0, 4);
2230 data[hdr+7] = 0;
2231 put2byte(&data[hdr+5], pBt->usableSize);
2232 pPage->nFree = (u16)(pBt->usableSize - first);
2233 decodeFlags(pPage, flags);
2234 pPage->cellOffset = first;
2235 pPage->aDataEnd = &data[pBt->pageSize];
2236 pPage->aCellIdx = &data[first];
2237 pPage->aDataOfst = &data[pPage->childPtrSize];
2238 pPage->nOverflow = 0;
2239 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
2240 pPage->maskPage = (u16)(pBt->pageSize - 1);
2241 pPage->nCell = 0;
2242 pPage->isInit = 1;
2247 ** Convert a DbPage obtained from the pager into a MemPage used by
2248 ** the btree layer.
2250 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
2251 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2252 if( pgno!=pPage->pgno ){
2253 pPage->aData = sqlite3PagerGetData(pDbPage);
2254 pPage->pDbPage = pDbPage;
2255 pPage->pBt = pBt;
2256 pPage->pgno = pgno;
2257 pPage->hdrOffset = pgno==1 ? 100 : 0;
2259 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
2260 return pPage;
2264 ** Get a page from the pager. Initialize the MemPage.pBt and
2265 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
2267 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
2268 ** about the content of the page at this time. So do not go to the disk
2269 ** to fetch the content. Just fill in the content with zeros for now.
2270 ** If in the future we call sqlite3PagerWrite() on this page, that
2271 ** means we have started to be concerned about content and the disk
2272 ** read should occur at that point.
2274 static int btreeGetPage(
2275 BtShared *pBt, /* The btree */
2276 Pgno pgno, /* Number of the page to fetch */
2277 MemPage **ppPage, /* Return the page in this parameter */
2278 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2280 int rc;
2281 DbPage *pDbPage;
2283 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
2284 assert( sqlite3_mutex_held(pBt->mutex) );
2285 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
2286 if( rc ) return rc;
2287 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
2288 return SQLITE_OK;
2292 ** Retrieve a page from the pager cache. If the requested page is not
2293 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
2294 ** MemPage.aData elements if needed.
2296 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
2297 DbPage *pDbPage;
2298 assert( sqlite3_mutex_held(pBt->mutex) );
2299 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
2300 if( pDbPage ){
2301 return btreePageFromDbPage(pDbPage, pgno, pBt);
2303 return 0;
2307 ** Return the size of the database file in pages. If there is any kind of
2308 ** error, return ((unsigned int)-1).
2310 static Pgno btreePagecount(BtShared *pBt){
2311 return pBt->nPage;
2313 Pgno sqlite3BtreeLastPage(Btree *p){
2314 assert( sqlite3BtreeHoldsMutex(p) );
2315 return btreePagecount(p->pBt);
2319 ** Get a page from the pager and initialize it.
2321 static int getAndInitPage(
2322 BtShared *pBt, /* The database file */
2323 Pgno pgno, /* Number of the page to get */
2324 MemPage **ppPage, /* Write the page pointer here */
2325 int bReadOnly /* True for a read-only page */
2327 int rc;
2328 DbPage *pDbPage;
2329 MemPage *pPage;
2330 assert( sqlite3_mutex_held(pBt->mutex) );
2332 if( pgno>btreePagecount(pBt) ){
2333 *ppPage = 0;
2334 return SQLITE_CORRUPT_BKPT;
2336 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
2337 if( rc ){
2338 *ppPage = 0;
2339 return rc;
2341 pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2342 if( pPage->isInit==0 ){
2343 btreePageFromDbPage(pDbPage, pgno, pBt);
2344 rc = btreeInitPage(pPage);
2345 if( rc!=SQLITE_OK ){
2346 releasePage(pPage);
2347 *ppPage = 0;
2348 return rc;
2351 assert( pPage->pgno==pgno || CORRUPT_DB );
2352 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
2353 *ppPage = pPage;
2354 return SQLITE_OK;
2358 ** Release a MemPage. This should be called once for each prior
2359 ** call to btreeGetPage.
2361 ** Page1 is a special case and must be released using releasePageOne().
2363 static void releasePageNotNull(MemPage *pPage){
2364 assert( pPage->aData );
2365 assert( pPage->pBt );
2366 assert( pPage->pDbPage!=0 );
2367 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2368 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2369 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2370 sqlite3PagerUnrefNotNull(pPage->pDbPage);
2372 static void releasePage(MemPage *pPage){
2373 if( pPage ) releasePageNotNull(pPage);
2375 static void releasePageOne(MemPage *pPage){
2376 assert( pPage!=0 );
2377 assert( pPage->aData );
2378 assert( pPage->pBt );
2379 assert( pPage->pDbPage!=0 );
2380 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2381 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2382 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2383 sqlite3PagerUnrefPageOne(pPage->pDbPage);
2387 ** Get an unused page.
2389 ** This works just like btreeGetPage() with the addition:
2391 ** * If the page is already in use for some other purpose, immediately
2392 ** release it and return an SQLITE_CURRUPT error.
2393 ** * Make sure the isInit flag is clear
2395 static int btreeGetUnusedPage(
2396 BtShared *pBt, /* The btree */
2397 Pgno pgno, /* Number of the page to fetch */
2398 MemPage **ppPage, /* Return the page in this parameter */
2399 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2401 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
2402 if( rc==SQLITE_OK ){
2403 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
2404 releasePage(*ppPage);
2405 *ppPage = 0;
2406 return SQLITE_CORRUPT_BKPT;
2408 (*ppPage)->isInit = 0;
2409 }else{
2410 *ppPage = 0;
2412 return rc;
2417 ** During a rollback, when the pager reloads information into the cache
2418 ** so that the cache is restored to its original state at the start of
2419 ** the transaction, for each page restored this routine is called.
2421 ** This routine needs to reset the extra data section at the end of the
2422 ** page to agree with the restored data.
2424 static void pageReinit(DbPage *pData){
2425 MemPage *pPage;
2426 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
2427 assert( sqlite3PagerPageRefcount(pData)>0 );
2428 if( pPage->isInit ){
2429 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2430 pPage->isInit = 0;
2431 if( sqlite3PagerPageRefcount(pData)>1 ){
2432 /* pPage might not be a btree page; it might be an overflow page
2433 ** or ptrmap page or a free page. In those cases, the following
2434 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
2435 ** But no harm is done by this. And it is very important that
2436 ** btreeInitPage() be called on every btree page so we make
2437 ** the call for every page that comes in for re-initializing. */
2438 btreeInitPage(pPage);
2444 ** Invoke the busy handler for a btree.
2446 static int btreeInvokeBusyHandler(void *pArg){
2447 BtShared *pBt = (BtShared*)pArg;
2448 assert( pBt->db );
2449 assert( sqlite3_mutex_held(pBt->db->mutex) );
2450 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
2454 ** Open a database file.
2456 ** zFilename is the name of the database file. If zFilename is NULL
2457 ** then an ephemeral database is created. The ephemeral database might
2458 ** be exclusively in memory, or it might use a disk-based memory cache.
2459 ** Either way, the ephemeral database will be automatically deleted
2460 ** when sqlite3BtreeClose() is called.
2462 ** If zFilename is ":memory:" then an in-memory database is created
2463 ** that is automatically destroyed when it is closed.
2465 ** The "flags" parameter is a bitmask that might contain bits like
2466 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
2468 ** If the database is already opened in the same database connection
2469 ** and we are in shared cache mode, then the open will fail with an
2470 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
2471 ** objects in the same database connection since doing so will lead
2472 ** to problems with locking.
2474 int sqlite3BtreeOpen(
2475 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
2476 const char *zFilename, /* Name of the file containing the BTree database */
2477 sqlite3 *db, /* Associated database handle */
2478 Btree **ppBtree, /* Pointer to new Btree object written here */
2479 int flags, /* Options */
2480 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
2482 BtShared *pBt = 0; /* Shared part of btree structure */
2483 Btree *p; /* Handle to return */
2484 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
2485 int rc = SQLITE_OK; /* Result code from this function */
2486 u8 nReserve; /* Byte of unused space on each page */
2487 unsigned char zDbHeader[100]; /* Database header content */
2489 /* True if opening an ephemeral, temporary database */
2490 const int isTempDb = zFilename==0 || zFilename[0]==0;
2492 /* Set the variable isMemdb to true for an in-memory database, or
2493 ** false for a file-based database.
2495 #ifdef SQLITE_OMIT_MEMORYDB
2496 const int isMemdb = 0;
2497 #else
2498 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
2499 || (isTempDb && sqlite3TempInMemory(db))
2500 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
2501 #endif
2503 assert( db!=0 );
2504 assert( pVfs!=0 );
2505 assert( sqlite3_mutex_held(db->mutex) );
2506 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
2508 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
2509 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
2511 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
2512 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
2514 if( isMemdb ){
2515 flags |= BTREE_MEMORY;
2517 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
2518 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
2520 p = sqlite3MallocZero(sizeof(Btree));
2521 if( !p ){
2522 return SQLITE_NOMEM_BKPT;
2524 p->inTrans = TRANS_NONE;
2525 p->db = db;
2526 #ifndef SQLITE_OMIT_SHARED_CACHE
2527 p->lock.pBtree = p;
2528 p->lock.iTable = 1;
2529 #endif
2531 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2533 ** If this Btree is a candidate for shared cache, try to find an
2534 ** existing BtShared object that we can share with
2536 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
2537 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
2538 int nFilename = sqlite3Strlen30(zFilename)+1;
2539 int nFullPathname = pVfs->mxPathname+1;
2540 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
2541 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2543 p->sharable = 1;
2544 if( !zFullPathname ){
2545 sqlite3_free(p);
2546 return SQLITE_NOMEM_BKPT;
2548 if( isMemdb ){
2549 memcpy(zFullPathname, zFilename, nFilename);
2550 }else{
2551 rc = sqlite3OsFullPathname(pVfs, zFilename,
2552 nFullPathname, zFullPathname);
2553 if( rc ){
2554 if( rc==SQLITE_OK_SYMLINK ){
2555 rc = SQLITE_OK;
2556 }else{
2557 sqlite3_free(zFullPathname);
2558 sqlite3_free(p);
2559 return rc;
2563 #if SQLITE_THREADSAFE
2564 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
2565 sqlite3_mutex_enter(mutexOpen);
2566 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);
2567 sqlite3_mutex_enter(mutexShared);
2568 #endif
2569 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
2570 assert( pBt->nRef>0 );
2571 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
2572 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
2573 int iDb;
2574 for(iDb=db->nDb-1; iDb>=0; iDb--){
2575 Btree *pExisting = db->aDb[iDb].pBt;
2576 if( pExisting && pExisting->pBt==pBt ){
2577 sqlite3_mutex_leave(mutexShared);
2578 sqlite3_mutex_leave(mutexOpen);
2579 sqlite3_free(zFullPathname);
2580 sqlite3_free(p);
2581 return SQLITE_CONSTRAINT;
2584 p->pBt = pBt;
2585 pBt->nRef++;
2586 break;
2589 sqlite3_mutex_leave(mutexShared);
2590 sqlite3_free(zFullPathname);
2592 #ifdef SQLITE_DEBUG
2593 else{
2594 /* In debug mode, we mark all persistent databases as sharable
2595 ** even when they are not. This exercises the locking code and
2596 ** gives more opportunity for asserts(sqlite3_mutex_held())
2597 ** statements to find locking problems.
2599 p->sharable = 1;
2601 #endif
2603 #endif
2604 if( pBt==0 ){
2606 ** The following asserts make sure that structures used by the btree are
2607 ** the right size. This is to guard against size changes that result
2608 ** when compiling on a different architecture.
2610 assert( sizeof(i64)==8 );
2611 assert( sizeof(u64)==8 );
2612 assert( sizeof(u32)==4 );
2613 assert( sizeof(u16)==2 );
2614 assert( sizeof(Pgno)==4 );
2616 /* Suppress false-positive compiler warning from PVS-Studio */
2617 memset(&zDbHeader[16], 0, 8);
2619 pBt = sqlite3MallocZero( sizeof(*pBt) );
2620 if( pBt==0 ){
2621 rc = SQLITE_NOMEM_BKPT;
2622 goto btree_open_out;
2624 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
2625 sizeof(MemPage), flags, vfsFlags, pageReinit);
2626 if( rc==SQLITE_OK ){
2627 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
2628 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
2630 if( rc!=SQLITE_OK ){
2631 goto btree_open_out;
2633 pBt->openFlags = (u8)flags;
2634 pBt->db = db;
2635 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
2636 p->pBt = pBt;
2638 pBt->pCursor = 0;
2639 pBt->pPage1 = 0;
2640 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
2641 #if defined(SQLITE_SECURE_DELETE)
2642 pBt->btsFlags |= BTS_SECURE_DELETE;
2643 #elif defined(SQLITE_FAST_SECURE_DELETE)
2644 pBt->btsFlags |= BTS_OVERWRITE;
2645 #endif
2646 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2647 ** determined by the 2-byte integer located at an offset of 16 bytes from
2648 ** the beginning of the database file. */
2649 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
2650 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
2651 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
2652 pBt->pageSize = 0;
2653 #ifndef SQLITE_OMIT_AUTOVACUUM
2654 /* If the magic name ":memory:" will create an in-memory database, then
2655 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
2656 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
2657 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
2658 ** regular file-name. In this case the auto-vacuum applies as per normal.
2660 if( zFilename && !isMemdb ){
2661 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
2662 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
2664 #endif
2665 nReserve = 0;
2666 }else{
2667 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2668 ** determined by the one-byte unsigned integer found at an offset of 20
2669 ** into the database file header. */
2670 nReserve = zDbHeader[20];
2671 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2672 #ifndef SQLITE_OMIT_AUTOVACUUM
2673 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
2674 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
2675 #endif
2677 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2678 if( rc ) goto btree_open_out;
2679 pBt->usableSize = pBt->pageSize - nReserve;
2680 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
2682 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2683 /* Add the new BtShared object to the linked list sharable BtShareds.
2685 pBt->nRef = 1;
2686 if( p->sharable ){
2687 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2688 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);)
2689 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
2690 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
2691 if( pBt->mutex==0 ){
2692 rc = SQLITE_NOMEM_BKPT;
2693 goto btree_open_out;
2696 sqlite3_mutex_enter(mutexShared);
2697 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
2698 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
2699 sqlite3_mutex_leave(mutexShared);
2701 #endif
2704 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2705 /* If the new Btree uses a sharable pBtShared, then link the new
2706 ** Btree into the list of all sharable Btrees for the same connection.
2707 ** The list is kept in ascending order by pBt address.
2709 if( p->sharable ){
2710 int i;
2711 Btree *pSib;
2712 for(i=0; i<db->nDb; i++){
2713 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
2714 while( pSib->pPrev ){ pSib = pSib->pPrev; }
2715 if( (uptr)p->pBt<(uptr)pSib->pBt ){
2716 p->pNext = pSib;
2717 p->pPrev = 0;
2718 pSib->pPrev = p;
2719 }else{
2720 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
2721 pSib = pSib->pNext;
2723 p->pNext = pSib->pNext;
2724 p->pPrev = pSib;
2725 if( p->pNext ){
2726 p->pNext->pPrev = p;
2728 pSib->pNext = p;
2730 break;
2734 #endif
2735 *ppBtree = p;
2737 btree_open_out:
2738 if( rc!=SQLITE_OK ){
2739 if( pBt && pBt->pPager ){
2740 sqlite3PagerClose(pBt->pPager, 0);
2742 sqlite3_free(pBt);
2743 sqlite3_free(p);
2744 *ppBtree = 0;
2745 }else{
2746 sqlite3_file *pFile;
2748 /* If the B-Tree was successfully opened, set the pager-cache size to the
2749 ** default value. Except, when opening on an existing shared pager-cache,
2750 ** do not change the pager-cache size.
2752 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
2753 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE);
2756 pFile = sqlite3PagerFile(pBt->pPager);
2757 if( pFile->pMethods ){
2758 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
2761 if( mutexOpen ){
2762 assert( sqlite3_mutex_held(mutexOpen) );
2763 sqlite3_mutex_leave(mutexOpen);
2765 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
2766 return rc;
2770 ** Decrement the BtShared.nRef counter. When it reaches zero,
2771 ** remove the BtShared structure from the sharing list. Return
2772 ** true if the BtShared.nRef counter reaches zero and return
2773 ** false if it is still positive.
2775 static int removeFromSharingList(BtShared *pBt){
2776 #ifndef SQLITE_OMIT_SHARED_CACHE
2777 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; )
2778 BtShared *pList;
2779 int removed = 0;
2781 assert( sqlite3_mutex_notheld(pBt->mutex) );
2782 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); )
2783 sqlite3_mutex_enter(pMainMtx);
2784 pBt->nRef--;
2785 if( pBt->nRef<=0 ){
2786 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
2787 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
2788 }else{
2789 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
2790 while( ALWAYS(pList) && pList->pNext!=pBt ){
2791 pList=pList->pNext;
2793 if( ALWAYS(pList) ){
2794 pList->pNext = pBt->pNext;
2797 if( SQLITE_THREADSAFE ){
2798 sqlite3_mutex_free(pBt->mutex);
2800 removed = 1;
2802 sqlite3_mutex_leave(pMainMtx);
2803 return removed;
2804 #else
2805 return 1;
2806 #endif
2810 ** Make sure pBt->pTmpSpace points to an allocation of
2811 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
2812 ** pointer.
2814 static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){
2815 assert( pBt!=0 );
2816 assert( pBt->pTmpSpace==0 );
2817 /* This routine is called only by btreeCursor() when allocating the
2818 ** first write cursor for the BtShared object */
2819 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 );
2820 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
2821 if( pBt->pTmpSpace==0 ){
2822 BtCursor *pCur = pBt->pCursor;
2823 pBt->pCursor = pCur->pNext; /* Unlink the cursor */
2824 memset(pCur, 0, sizeof(*pCur));
2825 return SQLITE_NOMEM_BKPT;
2828 /* One of the uses of pBt->pTmpSpace is to format cells before
2829 ** inserting them into a leaf page (function fillInCell()). If
2830 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
2831 ** by the various routines that manipulate binary cells. Which
2832 ** can mean that fillInCell() only initializes the first 2 or 3
2833 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
2834 ** it into a database page. This is not actually a problem, but it
2835 ** does cause a valgrind error when the 1 or 2 bytes of uninitialized
2836 ** data is passed to system call write(). So to avoid this error,
2837 ** zero the first 4 bytes of temp space here.
2839 ** Also: Provide four bytes of initialized space before the
2840 ** beginning of pTmpSpace as an area available to prepend the
2841 ** left-child pointer to the beginning of a cell.
2843 memset(pBt->pTmpSpace, 0, 8);
2844 pBt->pTmpSpace += 4;
2845 return SQLITE_OK;
2849 ** Free the pBt->pTmpSpace allocation
2851 static void freeTempSpace(BtShared *pBt){
2852 if( pBt->pTmpSpace ){
2853 pBt->pTmpSpace -= 4;
2854 sqlite3PageFree(pBt->pTmpSpace);
2855 pBt->pTmpSpace = 0;
2860 ** Close an open database and invalidate all cursors.
2862 int sqlite3BtreeClose(Btree *p){
2863 BtShared *pBt = p->pBt;
2865 /* Close all cursors opened via this handle. */
2866 assert( sqlite3_mutex_held(p->db->mutex) );
2867 sqlite3BtreeEnter(p);
2869 /* Verify that no other cursors have this Btree open */
2870 #ifdef SQLITE_DEBUG
2872 BtCursor *pCur = pBt->pCursor;
2873 while( pCur ){
2874 BtCursor *pTmp = pCur;
2875 pCur = pCur->pNext;
2876 assert( pTmp->pBtree!=p );
2880 #endif
2882 /* Rollback any active transaction and free the handle structure.
2883 ** The call to sqlite3BtreeRollback() drops any table-locks held by
2884 ** this handle.
2886 sqlite3BtreeRollback(p, SQLITE_OK, 0);
2887 sqlite3BtreeLeave(p);
2889 /* If there are still other outstanding references to the shared-btree
2890 ** structure, return now. The remainder of this procedure cleans
2891 ** up the shared-btree.
2893 assert( p->wantToLock==0 && p->locked==0 );
2894 if( !p->sharable || removeFromSharingList(pBt) ){
2895 /* The pBt is no longer on the sharing list, so we can access
2896 ** it without having to hold the mutex.
2898 ** Clean out and delete the BtShared object.
2900 assert( !pBt->pCursor );
2901 sqlite3PagerClose(pBt->pPager, p->db);
2902 if( pBt->xFreeSchema && pBt->pSchema ){
2903 pBt->xFreeSchema(pBt->pSchema);
2905 sqlite3DbFree(0, pBt->pSchema);
2906 freeTempSpace(pBt);
2907 sqlite3_free(pBt);
2910 #ifndef SQLITE_OMIT_SHARED_CACHE
2911 assert( p->wantToLock==0 );
2912 assert( p->locked==0 );
2913 if( p->pPrev ) p->pPrev->pNext = p->pNext;
2914 if( p->pNext ) p->pNext->pPrev = p->pPrev;
2915 #endif
2917 sqlite3_free(p);
2918 return SQLITE_OK;
2922 ** Change the "soft" limit on the number of pages in the cache.
2923 ** Unused and unmodified pages will be recycled when the number of
2924 ** pages in the cache exceeds this soft limit. But the size of the
2925 ** cache is allowed to grow larger than this limit if it contains
2926 ** dirty pages or pages still in active use.
2928 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
2929 BtShared *pBt = p->pBt;
2930 assert( sqlite3_mutex_held(p->db->mutex) );
2931 sqlite3BtreeEnter(p);
2932 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
2933 sqlite3BtreeLeave(p);
2934 return SQLITE_OK;
2938 ** Change the "spill" limit on the number of pages in the cache.
2939 ** If the number of pages exceeds this limit during a write transaction,
2940 ** the pager might attempt to "spill" pages to the journal early in
2941 ** order to free up memory.
2943 ** The value returned is the current spill size. If zero is passed
2944 ** as an argument, no changes are made to the spill size setting, so
2945 ** using mxPage of 0 is a way to query the current spill size.
2947 int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
2948 BtShared *pBt = p->pBt;
2949 int res;
2950 assert( sqlite3_mutex_held(p->db->mutex) );
2951 sqlite3BtreeEnter(p);
2952 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
2953 sqlite3BtreeLeave(p);
2954 return res;
2957 #if SQLITE_MAX_MMAP_SIZE>0
2959 ** Change the limit on the amount of the database file that may be
2960 ** memory mapped.
2962 int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
2963 BtShared *pBt = p->pBt;
2964 assert( sqlite3_mutex_held(p->db->mutex) );
2965 sqlite3BtreeEnter(p);
2966 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
2967 sqlite3BtreeLeave(p);
2968 return SQLITE_OK;
2970 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
2973 ** Change the way data is synced to disk in order to increase or decrease
2974 ** how well the database resists damage due to OS crashes and power
2975 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
2976 ** there is a high probability of damage) Level 2 is the default. There
2977 ** is a very low but non-zero probability of damage. Level 3 reduces the
2978 ** probability of damage to near zero but with a write performance reduction.
2980 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2981 int sqlite3BtreeSetPagerFlags(
2982 Btree *p, /* The btree to set the safety level on */
2983 unsigned pgFlags /* Various PAGER_* flags */
2985 BtShared *pBt = p->pBt;
2986 assert( sqlite3_mutex_held(p->db->mutex) );
2987 sqlite3BtreeEnter(p);
2988 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
2989 sqlite3BtreeLeave(p);
2990 return SQLITE_OK;
2992 #endif
2995 ** Change the default pages size and the number of reserved bytes per page.
2996 ** Or, if the page size has already been fixed, return SQLITE_READONLY
2997 ** without changing anything.
2999 ** The page size must be a power of 2 between 512 and 65536. If the page
3000 ** size supplied does not meet this constraint then the page size is not
3001 ** changed.
3003 ** Page sizes are constrained to be a power of two so that the region
3004 ** of the database file used for locking (beginning at PENDING_BYTE,
3005 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
3006 ** at the beginning of a page.
3008 ** If parameter nReserve is less than zero, then the number of reserved
3009 ** bytes per page is left unchanged.
3011 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
3012 ** and autovacuum mode can no longer be changed.
3014 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
3015 int rc = SQLITE_OK;
3016 int x;
3017 BtShared *pBt = p->pBt;
3018 assert( nReserve>=0 && nReserve<=255 );
3019 sqlite3BtreeEnter(p);
3020 pBt->nReserveWanted = nReserve;
3021 x = pBt->pageSize - pBt->usableSize;
3022 if( nReserve<x ) nReserve = x;
3023 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
3024 sqlite3BtreeLeave(p);
3025 return SQLITE_READONLY;
3027 assert( nReserve>=0 && nReserve<=255 );
3028 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
3029 ((pageSize-1)&pageSize)==0 ){
3030 assert( (pageSize & 7)==0 );
3031 assert( !pBt->pCursor );
3032 if( nReserve>32 && pageSize==512 ) pageSize = 1024;
3033 pBt->pageSize = (u32)pageSize;
3034 freeTempSpace(pBt);
3036 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
3037 pBt->usableSize = pBt->pageSize - (u16)nReserve;
3038 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3039 sqlite3BtreeLeave(p);
3040 return rc;
3044 ** Return the currently defined page size
3046 int sqlite3BtreeGetPageSize(Btree *p){
3047 return p->pBt->pageSize;
3051 ** This function is similar to sqlite3BtreeGetReserve(), except that it
3052 ** may only be called if it is guaranteed that the b-tree mutex is already
3053 ** held.
3055 ** This is useful in one special case in the backup API code where it is
3056 ** known that the shared b-tree mutex is held, but the mutex on the
3057 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
3058 ** were to be called, it might collide with some other operation on the
3059 ** database handle that owns *p, causing undefined behavior.
3061 int sqlite3BtreeGetReserveNoMutex(Btree *p){
3062 int n;
3063 assert( sqlite3_mutex_held(p->pBt->mutex) );
3064 n = p->pBt->pageSize - p->pBt->usableSize;
3065 return n;
3069 ** Return the number of bytes of space at the end of every page that
3070 ** are intentionally left unused. This is the "reserved" space that is
3071 ** sometimes used by extensions.
3073 ** The value returned is the larger of the current reserve size and
3074 ** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES.
3075 ** The amount of reserve can only grow - never shrink.
3077 int sqlite3BtreeGetRequestedReserve(Btree *p){
3078 int n1, n2;
3079 sqlite3BtreeEnter(p);
3080 n1 = (int)p->pBt->nReserveWanted;
3081 n2 = sqlite3BtreeGetReserveNoMutex(p);
3082 sqlite3BtreeLeave(p);
3083 return n1>n2 ? n1 : n2;
3088 ** Set the maximum page count for a database if mxPage is positive.
3089 ** No changes are made if mxPage is 0 or negative.
3090 ** Regardless of the value of mxPage, return the maximum page count.
3092 Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){
3093 Pgno n;
3094 sqlite3BtreeEnter(p);
3095 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
3096 sqlite3BtreeLeave(p);
3097 return n;
3101 ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
3103 ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
3104 ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
3105 ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
3106 ** newFlag==(-1) No changes
3108 ** This routine acts as a query if newFlag is less than zero
3110 ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
3111 ** freelist leaf pages are not written back to the database. Thus in-page
3112 ** deleted content is cleared, but freelist deleted content is not.
3114 ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
3115 ** that freelist leaf pages are written back into the database, increasing
3116 ** the amount of disk I/O.
3118 int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
3119 int b;
3120 if( p==0 ) return 0;
3121 sqlite3BtreeEnter(p);
3122 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
3123 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
3124 if( newFlag>=0 ){
3125 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
3126 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
3128 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
3129 sqlite3BtreeLeave(p);
3130 return b;
3134 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
3135 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
3136 ** is disabled. The default value for the auto-vacuum property is
3137 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
3139 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
3140 #ifdef SQLITE_OMIT_AUTOVACUUM
3141 return SQLITE_READONLY;
3142 #else
3143 BtShared *pBt = p->pBt;
3144 int rc = SQLITE_OK;
3145 u8 av = (u8)autoVacuum;
3147 sqlite3BtreeEnter(p);
3148 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
3149 rc = SQLITE_READONLY;
3150 }else{
3151 pBt->autoVacuum = av ?1:0;
3152 pBt->incrVacuum = av==2 ?1:0;
3154 sqlite3BtreeLeave(p);
3155 return rc;
3156 #endif
3160 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
3161 ** enabled 1 is returned. Otherwise 0.
3163 int sqlite3BtreeGetAutoVacuum(Btree *p){
3164 #ifdef SQLITE_OMIT_AUTOVACUUM
3165 return BTREE_AUTOVACUUM_NONE;
3166 #else
3167 int rc;
3168 sqlite3BtreeEnter(p);
3169 rc = (
3170 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
3171 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
3172 BTREE_AUTOVACUUM_INCR
3174 sqlite3BtreeLeave(p);
3175 return rc;
3176 #endif
3180 ** If the user has not set the safety-level for this database connection
3181 ** using "PRAGMA synchronous", and if the safety-level is not already
3182 ** set to the value passed to this function as the second parameter,
3183 ** set it so.
3185 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
3186 && !defined(SQLITE_OMIT_WAL)
3187 static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
3188 sqlite3 *db;
3189 Db *pDb;
3190 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
3191 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
3192 if( pDb->bSyncSet==0
3193 && pDb->safety_level!=safety_level
3194 && pDb!=&db->aDb[1]
3196 pDb->safety_level = safety_level;
3197 sqlite3PagerSetFlags(pBt->pPager,
3198 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
3202 #else
3203 # define setDefaultSyncFlag(pBt,safety_level)
3204 #endif
3206 /* Forward declaration */
3207 static int newDatabase(BtShared*);
3211 ** Get a reference to pPage1 of the database file. This will
3212 ** also acquire a readlock on that file.
3214 ** SQLITE_OK is returned on success. If the file is not a
3215 ** well-formed database file, then SQLITE_CORRUPT is returned.
3216 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
3217 ** is returned if we run out of memory.
3219 static int lockBtree(BtShared *pBt){
3220 int rc; /* Result code from subfunctions */
3221 MemPage *pPage1; /* Page 1 of the database file */
3222 u32 nPage; /* Number of pages in the database */
3223 u32 nPageFile = 0; /* Number of pages in the database file */
3225 assert( sqlite3_mutex_held(pBt->mutex) );
3226 assert( pBt->pPage1==0 );
3227 rc = sqlite3PagerSharedLock(pBt->pPager);
3228 if( rc!=SQLITE_OK ) return rc;
3229 rc = btreeGetPage(pBt, 1, &pPage1, 0);
3230 if( rc!=SQLITE_OK ) return rc;
3232 /* Do some checking to help insure the file we opened really is
3233 ** a valid database file.
3235 nPage = get4byte(28+(u8*)pPage1->aData);
3236 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
3237 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
3238 nPage = nPageFile;
3240 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
3241 nPage = 0;
3243 if( nPage>0 ){
3244 u32 pageSize;
3245 u32 usableSize;
3246 u8 *page1 = pPage1->aData;
3247 rc = SQLITE_NOTADB;
3248 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
3249 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
3250 ** 61 74 20 33 00. */
3251 if( memcmp(page1, zMagicHeader, 16)!=0 ){
3252 goto page1_init_failed;
3255 #ifdef SQLITE_OMIT_WAL
3256 if( page1[18]>1 ){
3257 pBt->btsFlags |= BTS_READ_ONLY;
3259 if( page1[19]>1 ){
3260 goto page1_init_failed;
3262 #else
3263 if( page1[18]>2 ){
3264 pBt->btsFlags |= BTS_READ_ONLY;
3266 if( page1[19]>2 ){
3267 goto page1_init_failed;
3270 /* If the read version is set to 2, this database should be accessed
3271 ** in WAL mode. If the log is not already open, open it now. Then
3272 ** return SQLITE_OK and return without populating BtShared.pPage1.
3273 ** The caller detects this and calls this function again. This is
3274 ** required as the version of page 1 currently in the page1 buffer
3275 ** may not be the latest version - there may be a newer one in the log
3276 ** file.
3278 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
3279 int isOpen = 0;
3280 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
3281 if( rc!=SQLITE_OK ){
3282 goto page1_init_failed;
3283 }else{
3284 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
3285 if( isOpen==0 ){
3286 releasePageOne(pPage1);
3287 return SQLITE_OK;
3290 rc = SQLITE_NOTADB;
3291 }else{
3292 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
3294 #endif
3296 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
3297 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
3299 ** The original design allowed these amounts to vary, but as of
3300 ** version 3.6.0, we require them to be fixed.
3302 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
3303 goto page1_init_failed;
3305 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
3306 ** determined by the 2-byte integer located at an offset of 16 bytes from
3307 ** the beginning of the database file. */
3308 pageSize = (page1[16]<<8) | (page1[17]<<16);
3309 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
3310 ** between 512 and 65536 inclusive. */
3311 if( ((pageSize-1)&pageSize)!=0
3312 || pageSize>SQLITE_MAX_PAGE_SIZE
3313 || pageSize<=256
3315 goto page1_init_failed;
3317 assert( (pageSize & 7)==0 );
3318 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
3319 ** integer at offset 20 is the number of bytes of space at the end of
3320 ** each page to reserve for extensions.
3322 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
3323 ** determined by the one-byte unsigned integer found at an offset of 20
3324 ** into the database file header. */
3325 usableSize = pageSize - page1[20];
3326 if( (u32)pageSize!=pBt->pageSize ){
3327 /* After reading the first page of the database assuming a page size
3328 ** of BtShared.pageSize, we have discovered that the page-size is
3329 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
3330 ** zero and return SQLITE_OK. The caller will call this function
3331 ** again with the correct page-size.
3333 releasePageOne(pPage1);
3334 pBt->usableSize = usableSize;
3335 pBt->pageSize = pageSize;
3336 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3337 freeTempSpace(pBt);
3338 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
3339 pageSize-usableSize);
3340 return rc;
3342 if( nPage>nPageFile ){
3343 if( sqlite3WritableSchema(pBt->db)==0 ){
3344 rc = SQLITE_CORRUPT_BKPT;
3345 goto page1_init_failed;
3346 }else{
3347 nPage = nPageFile;
3350 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
3351 ** be less than 480. In other words, if the page size is 512, then the
3352 ** reserved space size cannot exceed 32. */
3353 if( usableSize<480 ){
3354 goto page1_init_failed;
3356 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3357 pBt->pageSize = pageSize;
3358 pBt->usableSize = usableSize;
3359 #ifndef SQLITE_OMIT_AUTOVACUUM
3360 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
3361 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
3362 #endif
3365 /* maxLocal is the maximum amount of payload to store locally for
3366 ** a cell. Make sure it is small enough so that at least minFanout
3367 ** cells can will fit on one page. We assume a 10-byte page header.
3368 ** Besides the payload, the cell must store:
3369 ** 2-byte pointer to the cell
3370 ** 4-byte child pointer
3371 ** 9-byte nKey value
3372 ** 4-byte nData value
3373 ** 4-byte overflow page pointer
3374 ** So a cell consists of a 2-byte pointer, a header which is as much as
3375 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
3376 ** page pointer.
3378 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
3379 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
3380 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
3381 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
3382 if( pBt->maxLocal>127 ){
3383 pBt->max1bytePayload = 127;
3384 }else{
3385 pBt->max1bytePayload = (u8)pBt->maxLocal;
3387 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
3388 pBt->pPage1 = pPage1;
3389 pBt->nPage = nPage;
3390 return SQLITE_OK;
3392 page1_init_failed:
3393 releasePageOne(pPage1);
3394 pBt->pPage1 = 0;
3395 return rc;
3398 #ifndef NDEBUG
3400 ** Return the number of cursors open on pBt. This is for use
3401 ** in assert() expressions, so it is only compiled if NDEBUG is not
3402 ** defined.
3404 ** Only write cursors are counted if wrOnly is true. If wrOnly is
3405 ** false then all cursors are counted.
3407 ** For the purposes of this routine, a cursor is any cursor that
3408 ** is capable of reading or writing to the database. Cursors that
3409 ** have been tripped into the CURSOR_FAULT state are not counted.
3411 static int countValidCursors(BtShared *pBt, int wrOnly){
3412 BtCursor *pCur;
3413 int r = 0;
3414 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
3415 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
3416 && pCur->eState!=CURSOR_FAULT ) r++;
3418 return r;
3420 #endif
3423 ** If there are no outstanding cursors and we are not in the middle
3424 ** of a transaction but there is a read lock on the database, then
3425 ** this routine unrefs the first page of the database file which
3426 ** has the effect of releasing the read lock.
3428 ** If there is a transaction in progress, this routine is a no-op.
3430 static void unlockBtreeIfUnused(BtShared *pBt){
3431 assert( sqlite3_mutex_held(pBt->mutex) );
3432 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
3433 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
3434 MemPage *pPage1 = pBt->pPage1;
3435 assert( pPage1->aData );
3436 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
3437 pBt->pPage1 = 0;
3438 releasePageOne(pPage1);
3443 ** If pBt points to an empty file then convert that empty file
3444 ** into a new empty database by initializing the first page of
3445 ** the database.
3447 static int newDatabase(BtShared *pBt){
3448 MemPage *pP1;
3449 unsigned char *data;
3450 int rc;
3452 assert( sqlite3_mutex_held(pBt->mutex) );
3453 if( pBt->nPage>0 ){
3454 return SQLITE_OK;
3456 pP1 = pBt->pPage1;
3457 assert( pP1!=0 );
3458 data = pP1->aData;
3459 rc = sqlite3PagerWrite(pP1->pDbPage);
3460 if( rc ) return rc;
3461 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
3462 assert( sizeof(zMagicHeader)==16 );
3463 data[16] = (u8)((pBt->pageSize>>8)&0xff);
3464 data[17] = (u8)((pBt->pageSize>>16)&0xff);
3465 data[18] = 1;
3466 data[19] = 1;
3467 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
3468 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
3469 data[21] = 64;
3470 data[22] = 32;
3471 data[23] = 32;
3472 memset(&data[24], 0, 100-24);
3473 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
3474 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3475 #ifndef SQLITE_OMIT_AUTOVACUUM
3476 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
3477 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
3478 put4byte(&data[36 + 4*4], pBt->autoVacuum);
3479 put4byte(&data[36 + 7*4], pBt->incrVacuum);
3480 #endif
3481 pBt->nPage = 1;
3482 data[31] = 1;
3483 return SQLITE_OK;
3487 ** Initialize the first page of the database file (creating a database
3488 ** consisting of a single page and no schema objects). Return SQLITE_OK
3489 ** if successful, or an SQLite error code otherwise.
3491 int sqlite3BtreeNewDb(Btree *p){
3492 int rc;
3493 sqlite3BtreeEnter(p);
3494 p->pBt->nPage = 0;
3495 rc = newDatabase(p->pBt);
3496 sqlite3BtreeLeave(p);
3497 return rc;
3501 ** Attempt to start a new transaction. A write-transaction
3502 ** is started if the second argument is nonzero, otherwise a read-
3503 ** transaction. If the second argument is 2 or more and exclusive
3504 ** transaction is started, meaning that no other process is allowed
3505 ** to access the database. A preexisting transaction may not be
3506 ** upgraded to exclusive by calling this routine a second time - the
3507 ** exclusivity flag only works for a new transaction.
3509 ** A write-transaction must be started before attempting any
3510 ** changes to the database. None of the following routines
3511 ** will work unless a transaction is started first:
3513 ** sqlite3BtreeCreateTable()
3514 ** sqlite3BtreeCreateIndex()
3515 ** sqlite3BtreeClearTable()
3516 ** sqlite3BtreeDropTable()
3517 ** sqlite3BtreeInsert()
3518 ** sqlite3BtreeDelete()
3519 ** sqlite3BtreeUpdateMeta()
3521 ** If an initial attempt to acquire the lock fails because of lock contention
3522 ** and the database was previously unlocked, then invoke the busy handler
3523 ** if there is one. But if there was previously a read-lock, do not
3524 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
3525 ** returned when there is already a read-lock in order to avoid a deadlock.
3527 ** Suppose there are two processes A and B. A has a read lock and B has
3528 ** a reserved lock. B tries to promote to exclusive but is blocked because
3529 ** of A's read lock. A tries to promote to reserved but is blocked by B.
3530 ** One or the other of the two processes must give way or there can be
3531 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
3532 ** when A already has a read lock, we encourage A to give up and let B
3533 ** proceed.
3535 static SQLITE_NOINLINE int btreeBeginTrans(
3536 Btree *p, /* The btree in which to start the transaction */
3537 int wrflag, /* True to start a write transaction */
3538 int *pSchemaVersion /* Put schema version number here, if not NULL */
3540 BtShared *pBt = p->pBt;
3541 Pager *pPager = pBt->pPager;
3542 int rc = SQLITE_OK;
3544 sqlite3BtreeEnter(p);
3545 btreeIntegrity(p);
3547 /* If the btree is already in a write-transaction, or it
3548 ** is already in a read-transaction and a read-transaction
3549 ** is requested, this is a no-op.
3551 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
3552 goto trans_begun;
3554 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
3556 if( (p->db->flags & SQLITE_ResetDatabase)
3557 && sqlite3PagerIsreadonly(pPager)==0
3559 pBt->btsFlags &= ~BTS_READ_ONLY;
3562 /* Write transactions are not possible on a read-only database */
3563 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
3564 rc = SQLITE_READONLY;
3565 goto trans_begun;
3568 #ifndef SQLITE_OMIT_SHARED_CACHE
3570 sqlite3 *pBlock = 0;
3571 /* If another database handle has already opened a write transaction
3572 ** on this shared-btree structure and a second write transaction is
3573 ** requested, return SQLITE_LOCKED.
3575 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
3576 || (pBt->btsFlags & BTS_PENDING)!=0
3578 pBlock = pBt->pWriter->db;
3579 }else if( wrflag>1 ){
3580 BtLock *pIter;
3581 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3582 if( pIter->pBtree!=p ){
3583 pBlock = pIter->pBtree->db;
3584 break;
3588 if( pBlock ){
3589 sqlite3ConnectionBlocked(p->db, pBlock);
3590 rc = SQLITE_LOCKED_SHAREDCACHE;
3591 goto trans_begun;
3594 #endif
3596 /* Any read-only or read-write transaction implies a read-lock on
3597 ** page 1. So if some other shared-cache client already has a write-lock
3598 ** on page 1, the transaction cannot be opened. */
3599 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
3600 if( SQLITE_OK!=rc ) goto trans_begun;
3602 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
3603 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
3604 do {
3605 sqlite3PagerWalDb(pPager, p->db);
3607 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
3608 /* If transitioning from no transaction directly to a write transaction,
3609 ** block for the WRITER lock first if possible. */
3610 if( pBt->pPage1==0 && wrflag ){
3611 assert( pBt->inTransaction==TRANS_NONE );
3612 rc = sqlite3PagerWalWriteLock(pPager, 1);
3613 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
3615 #endif
3617 /* Call lockBtree() until either pBt->pPage1 is populated or
3618 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
3619 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
3620 ** reading page 1 it discovers that the page-size of the database
3621 ** file is not pBt->pageSize. In this case lockBtree() will update
3622 ** pBt->pageSize to the page-size of the file on disk.
3624 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
3626 if( rc==SQLITE_OK && wrflag ){
3627 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
3628 rc = SQLITE_READONLY;
3629 }else{
3630 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db));
3631 if( rc==SQLITE_OK ){
3632 rc = newDatabase(pBt);
3633 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
3634 /* if there was no transaction opened when this function was
3635 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
3636 ** code to SQLITE_BUSY. */
3637 rc = SQLITE_BUSY;
3642 if( rc!=SQLITE_OK ){
3643 (void)sqlite3PagerWalWriteLock(pPager, 0);
3644 unlockBtreeIfUnused(pBt);
3646 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
3647 btreeInvokeBusyHandler(pBt) );
3648 sqlite3PagerWalDb(pPager, 0);
3649 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
3650 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
3651 #endif
3653 if( rc==SQLITE_OK ){
3654 if( p->inTrans==TRANS_NONE ){
3655 pBt->nTransaction++;
3656 #ifndef SQLITE_OMIT_SHARED_CACHE
3657 if( p->sharable ){
3658 assert( p->lock.pBtree==p && p->lock.iTable==1 );
3659 p->lock.eLock = READ_LOCK;
3660 p->lock.pNext = pBt->pLock;
3661 pBt->pLock = &p->lock;
3663 #endif
3665 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
3666 if( p->inTrans>pBt->inTransaction ){
3667 pBt->inTransaction = p->inTrans;
3669 if( wrflag ){
3670 MemPage *pPage1 = pBt->pPage1;
3671 #ifndef SQLITE_OMIT_SHARED_CACHE
3672 assert( !pBt->pWriter );
3673 pBt->pWriter = p;
3674 pBt->btsFlags &= ~BTS_EXCLUSIVE;
3675 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
3676 #endif
3678 /* If the db-size header field is incorrect (as it may be if an old
3679 ** client has been writing the database file), update it now. Doing
3680 ** this sooner rather than later means the database size can safely
3681 ** re-read the database size from page 1 if a savepoint or transaction
3682 ** rollback occurs within the transaction.
3684 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
3685 rc = sqlite3PagerWrite(pPage1->pDbPage);
3686 if( rc==SQLITE_OK ){
3687 put4byte(&pPage1->aData[28], pBt->nPage);
3693 trans_begun:
3694 if( rc==SQLITE_OK ){
3695 if( pSchemaVersion ){
3696 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
3698 if( wrflag ){
3699 /* This call makes sure that the pager has the correct number of
3700 ** open savepoints. If the second parameter is greater than 0 and
3701 ** the sub-journal is not already open, then it will be opened here.
3703 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint);
3707 btreeIntegrity(p);
3708 sqlite3BtreeLeave(p);
3709 return rc;
3711 int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
3712 BtShared *pBt;
3713 if( p->sharable
3714 || p->inTrans==TRANS_NONE
3715 || (p->inTrans==TRANS_READ && wrflag!=0)
3717 return btreeBeginTrans(p,wrflag,pSchemaVersion);
3719 pBt = p->pBt;
3720 if( pSchemaVersion ){
3721 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
3723 if( wrflag ){
3724 /* This call makes sure that the pager has the correct number of
3725 ** open savepoints. If the second parameter is greater than 0 and
3726 ** the sub-journal is not already open, then it will be opened here.
3728 return sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
3729 }else{
3730 return SQLITE_OK;
3734 #ifndef SQLITE_OMIT_AUTOVACUUM
3737 ** Set the pointer-map entries for all children of page pPage. Also, if
3738 ** pPage contains cells that point to overflow pages, set the pointer
3739 ** map entries for the overflow pages as well.
3741 static int setChildPtrmaps(MemPage *pPage){
3742 int i; /* Counter variable */
3743 int nCell; /* Number of cells in page pPage */
3744 int rc; /* Return code */
3745 BtShared *pBt = pPage->pBt;
3746 Pgno pgno = pPage->pgno;
3748 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3749 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3750 if( rc!=SQLITE_OK ) return rc;
3751 nCell = pPage->nCell;
3753 for(i=0; i<nCell; i++){
3754 u8 *pCell = findCell(pPage, i);
3756 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
3758 if( !pPage->leaf ){
3759 Pgno childPgno = get4byte(pCell);
3760 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3764 if( !pPage->leaf ){
3765 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3766 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3769 return rc;
3773 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
3774 ** that it points to iTo. Parameter eType describes the type of pointer to
3775 ** be modified, as follows:
3777 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
3778 ** page of pPage.
3780 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
3781 ** page pointed to by one of the cells on pPage.
3783 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
3784 ** overflow page in the list.
3786 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
3787 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3788 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
3789 if( eType==PTRMAP_OVERFLOW2 ){
3790 /* The pointer is always the first 4 bytes of the page in this case. */
3791 if( get4byte(pPage->aData)!=iFrom ){
3792 return SQLITE_CORRUPT_PAGE(pPage);
3794 put4byte(pPage->aData, iTo);
3795 }else{
3796 int i;
3797 int nCell;
3798 int rc;
3800 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3801 if( rc ) return rc;
3802 nCell = pPage->nCell;
3804 for(i=0; i<nCell; i++){
3805 u8 *pCell = findCell(pPage, i);
3806 if( eType==PTRMAP_OVERFLOW1 ){
3807 CellInfo info;
3808 pPage->xParseCell(pPage, pCell, &info);
3809 if( info.nLocal<info.nPayload ){
3810 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
3811 return SQLITE_CORRUPT_PAGE(pPage);
3813 if( iFrom==get4byte(pCell+info.nSize-4) ){
3814 put4byte(pCell+info.nSize-4, iTo);
3815 break;
3818 }else{
3819 if( pCell+4 > pPage->aData+pPage->pBt->usableSize ){
3820 return SQLITE_CORRUPT_PAGE(pPage);
3822 if( get4byte(pCell)==iFrom ){
3823 put4byte(pCell, iTo);
3824 break;
3829 if( i==nCell ){
3830 if( eType!=PTRMAP_BTREE ||
3831 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
3832 return SQLITE_CORRUPT_PAGE(pPage);
3834 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
3837 return SQLITE_OK;
3842 ** Move the open database page pDbPage to location iFreePage in the
3843 ** database. The pDbPage reference remains valid.
3845 ** The isCommit flag indicates that there is no need to remember that
3846 ** the journal needs to be sync()ed before database page pDbPage->pgno
3847 ** can be written to. The caller has already promised not to write to that
3848 ** page.
3850 static int relocatePage(
3851 BtShared *pBt, /* Btree */
3852 MemPage *pDbPage, /* Open page to move */
3853 u8 eType, /* Pointer map 'type' entry for pDbPage */
3854 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
3855 Pgno iFreePage, /* The location to move pDbPage to */
3856 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
3858 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
3859 Pgno iDbPage = pDbPage->pgno;
3860 Pager *pPager = pBt->pPager;
3861 int rc;
3863 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
3864 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
3865 assert( sqlite3_mutex_held(pBt->mutex) );
3866 assert( pDbPage->pBt==pBt );
3867 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT;
3869 /* Move page iDbPage from its current location to page number iFreePage */
3870 TRACE(("AUTOVACUUM: Moving %u to free page %u (ptr page %u type %u)\n",
3871 iDbPage, iFreePage, iPtrPage, eType));
3872 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
3873 if( rc!=SQLITE_OK ){
3874 return rc;
3876 pDbPage->pgno = iFreePage;
3878 /* If pDbPage was a btree-page, then it may have child pages and/or cells
3879 ** that point to overflow pages. The pointer map entries for all these
3880 ** pages need to be changed.
3882 ** If pDbPage is an overflow page, then the first 4 bytes may store a
3883 ** pointer to a subsequent overflow page. If this is the case, then
3884 ** the pointer map needs to be updated for the subsequent overflow page.
3886 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
3887 rc = setChildPtrmaps(pDbPage);
3888 if( rc!=SQLITE_OK ){
3889 return rc;
3891 }else{
3892 Pgno nextOvfl = get4byte(pDbPage->aData);
3893 if( nextOvfl!=0 ){
3894 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
3895 if( rc!=SQLITE_OK ){
3896 return rc;
3901 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
3902 ** that it points at iFreePage. Also fix the pointer map entry for
3903 ** iPtrPage.
3905 if( eType!=PTRMAP_ROOTPAGE ){
3906 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
3907 if( rc!=SQLITE_OK ){
3908 return rc;
3910 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
3911 if( rc!=SQLITE_OK ){
3912 releasePage(pPtrPage);
3913 return rc;
3915 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
3916 releasePage(pPtrPage);
3917 if( rc==SQLITE_OK ){
3918 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
3921 return rc;
3924 /* Forward declaration required by incrVacuumStep(). */
3925 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
3928 ** Perform a single step of an incremental-vacuum. If successful, return
3929 ** SQLITE_OK. If there is no work to do (and therefore no point in
3930 ** calling this function again), return SQLITE_DONE. Or, if an error
3931 ** occurs, return some other error code.
3933 ** More specifically, this function attempts to re-organize the database so
3934 ** that the last page of the file currently in use is no longer in use.
3936 ** Parameter nFin is the number of pages that this database would contain
3937 ** were this function called until it returns SQLITE_DONE.
3939 ** If the bCommit parameter is non-zero, this function assumes that the
3940 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
3941 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
3942 ** operation, or false for an incremental vacuum.
3944 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
3945 Pgno nFreeList; /* Number of pages still on the free-list */
3946 int rc;
3948 assert( sqlite3_mutex_held(pBt->mutex) );
3949 assert( iLastPg>nFin );
3951 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
3952 u8 eType;
3953 Pgno iPtrPage;
3955 nFreeList = get4byte(&pBt->pPage1->aData[36]);
3956 if( nFreeList==0 ){
3957 return SQLITE_DONE;
3960 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
3961 if( rc!=SQLITE_OK ){
3962 return rc;
3964 if( eType==PTRMAP_ROOTPAGE ){
3965 return SQLITE_CORRUPT_BKPT;
3968 if( eType==PTRMAP_FREEPAGE ){
3969 if( bCommit==0 ){
3970 /* Remove the page from the files free-list. This is not required
3971 ** if bCommit is non-zero. In that case, the free-list will be
3972 ** truncated to zero after this function returns, so it doesn't
3973 ** matter if it still contains some garbage entries.
3975 Pgno iFreePg;
3976 MemPage *pFreePg;
3977 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
3978 if( rc!=SQLITE_OK ){
3979 return rc;
3981 assert( iFreePg==iLastPg );
3982 releasePage(pFreePg);
3984 } else {
3985 Pgno iFreePg; /* Index of free page to move pLastPg to */
3986 MemPage *pLastPg;
3987 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
3988 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
3990 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
3991 if( rc!=SQLITE_OK ){
3992 return rc;
3995 /* If bCommit is zero, this loop runs exactly once and page pLastPg
3996 ** is swapped with the first free page pulled off the free list.
3998 ** On the other hand, if bCommit is greater than zero, then keep
3999 ** looping until a free-page located within the first nFin pages
4000 ** of the file is found.
4002 if( bCommit==0 ){
4003 eMode = BTALLOC_LE;
4004 iNear = nFin;
4006 do {
4007 MemPage *pFreePg;
4008 Pgno dbSize = btreePagecount(pBt);
4009 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
4010 if( rc!=SQLITE_OK ){
4011 releasePage(pLastPg);
4012 return rc;
4014 releasePage(pFreePg);
4015 if( iFreePg>dbSize ){
4016 releasePage(pLastPg);
4017 return SQLITE_CORRUPT_BKPT;
4019 }while( bCommit && iFreePg>nFin );
4020 assert( iFreePg<iLastPg );
4022 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
4023 releasePage(pLastPg);
4024 if( rc!=SQLITE_OK ){
4025 return rc;
4030 if( bCommit==0 ){
4031 do {
4032 iLastPg--;
4033 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
4034 pBt->bDoTruncate = 1;
4035 pBt->nPage = iLastPg;
4037 return SQLITE_OK;
4041 ** The database opened by the first argument is an auto-vacuum database
4042 ** nOrig pages in size containing nFree free pages. Return the expected
4043 ** size of the database in pages following an auto-vacuum operation.
4045 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
4046 int nEntry; /* Number of entries on one ptrmap page */
4047 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
4048 Pgno nFin; /* Return value */
4050 nEntry = pBt->usableSize/5;
4051 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
4052 nFin = nOrig - nFree - nPtrmap;
4053 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
4054 nFin--;
4056 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
4057 nFin--;
4060 return nFin;
4064 ** A write-transaction must be opened before calling this function.
4065 ** It performs a single unit of work towards an incremental vacuum.
4067 ** If the incremental vacuum is finished after this function has run,
4068 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
4069 ** SQLITE_OK is returned. Otherwise an SQLite error code.
4071 int sqlite3BtreeIncrVacuum(Btree *p){
4072 int rc;
4073 BtShared *pBt = p->pBt;
4075 sqlite3BtreeEnter(p);
4076 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
4077 if( !pBt->autoVacuum ){
4078 rc = SQLITE_DONE;
4079 }else{
4080 Pgno nOrig = btreePagecount(pBt);
4081 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
4082 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
4084 if( nOrig<nFin || nFree>=nOrig ){
4085 rc = SQLITE_CORRUPT_BKPT;
4086 }else if( nFree>0 ){
4087 rc = saveAllCursors(pBt, 0, 0);
4088 if( rc==SQLITE_OK ){
4089 invalidateAllOverflowCache(pBt);
4090 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
4092 if( rc==SQLITE_OK ){
4093 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
4094 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
4096 }else{
4097 rc = SQLITE_DONE;
4100 sqlite3BtreeLeave(p);
4101 return rc;
4105 ** This routine is called prior to sqlite3PagerCommit when a transaction
4106 ** is committed for an auto-vacuum database.
4108 static int autoVacuumCommit(Btree *p){
4109 int rc = SQLITE_OK;
4110 Pager *pPager;
4111 BtShared *pBt;
4112 sqlite3 *db;
4113 VVA_ONLY( int nRef );
4115 assert( p!=0 );
4116 pBt = p->pBt;
4117 pPager = pBt->pPager;
4118 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); )
4120 assert( sqlite3_mutex_held(pBt->mutex) );
4121 invalidateAllOverflowCache(pBt);
4122 assert(pBt->autoVacuum);
4123 if( !pBt->incrVacuum ){
4124 Pgno nFin; /* Number of pages in database after autovacuuming */
4125 Pgno nFree; /* Number of pages on the freelist initially */
4126 Pgno nVac; /* Number of pages to vacuum */
4127 Pgno iFree; /* The next page to be freed */
4128 Pgno nOrig; /* Database size before freeing */
4130 nOrig = btreePagecount(pBt);
4131 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
4132 /* It is not possible to create a database for which the final page
4133 ** is either a pointer-map page or the pending-byte page. If one
4134 ** is encountered, this indicates corruption.
4136 return SQLITE_CORRUPT_BKPT;
4139 nFree = get4byte(&pBt->pPage1->aData[36]);
4140 db = p->db;
4141 if( db->xAutovacPages ){
4142 int iDb;
4143 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){
4144 if( db->aDb[iDb].pBt==p ) break;
4146 nVac = db->xAutovacPages(
4147 db->pAutovacPagesArg,
4148 db->aDb[iDb].zDbSName,
4149 nOrig,
4150 nFree,
4151 pBt->pageSize
4153 if( nVac>nFree ){
4154 nVac = nFree;
4156 if( nVac==0 ){
4157 return SQLITE_OK;
4159 }else{
4160 nVac = nFree;
4162 nFin = finalDbSize(pBt, nOrig, nVac);
4163 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
4164 if( nFin<nOrig ){
4165 rc = saveAllCursors(pBt, 0, 0);
4167 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
4168 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree);
4170 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
4171 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
4172 if( nVac==nFree ){
4173 put4byte(&pBt->pPage1->aData[32], 0);
4174 put4byte(&pBt->pPage1->aData[36], 0);
4176 put4byte(&pBt->pPage1->aData[28], nFin);
4177 pBt->bDoTruncate = 1;
4178 pBt->nPage = nFin;
4180 if( rc!=SQLITE_OK ){
4181 sqlite3PagerRollback(pPager);
4185 assert( nRef>=sqlite3PagerRefcount(pPager) );
4186 return rc;
4189 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
4190 # define setChildPtrmaps(x) SQLITE_OK
4191 #endif
4194 ** This routine does the first phase of a two-phase commit. This routine
4195 ** causes a rollback journal to be created (if it does not already exist)
4196 ** and populated with enough information so that if a power loss occurs
4197 ** the database can be restored to its original state by playing back
4198 ** the journal. Then the contents of the journal are flushed out to
4199 ** the disk. After the journal is safely on oxide, the changes to the
4200 ** database are written into the database file and flushed to oxide.
4201 ** At the end of this call, the rollback journal still exists on the
4202 ** disk and we are still holding all locks, so the transaction has not
4203 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
4204 ** commit process.
4206 ** This call is a no-op if no write-transaction is currently active on pBt.
4208 ** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to
4209 ** the name of a super-journal file that should be written into the
4210 ** individual journal file, or is NULL, indicating no super-journal file
4211 ** (single database transaction).
4213 ** When this is called, the super-journal should already have been
4214 ** created, populated with this journal pointer and synced to disk.
4216 ** Once this is routine has returned, the only thing required to commit
4217 ** the write-transaction for this database file is to delete the journal.
4219 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){
4220 int rc = SQLITE_OK;
4221 if( p->inTrans==TRANS_WRITE ){
4222 BtShared *pBt = p->pBt;
4223 sqlite3BtreeEnter(p);
4224 #ifndef SQLITE_OMIT_AUTOVACUUM
4225 if( pBt->autoVacuum ){
4226 rc = autoVacuumCommit(p);
4227 if( rc!=SQLITE_OK ){
4228 sqlite3BtreeLeave(p);
4229 return rc;
4232 if( pBt->bDoTruncate ){
4233 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
4235 #endif
4236 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0);
4237 sqlite3BtreeLeave(p);
4239 return rc;
4243 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
4244 ** at the conclusion of a transaction.
4246 static void btreeEndTransaction(Btree *p){
4247 BtShared *pBt = p->pBt;
4248 sqlite3 *db = p->db;
4249 assert( sqlite3BtreeHoldsMutex(p) );
4251 #ifndef SQLITE_OMIT_AUTOVACUUM
4252 pBt->bDoTruncate = 0;
4253 #endif
4254 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
4255 /* If there are other active statements that belong to this database
4256 ** handle, downgrade to a read-only transaction. The other statements
4257 ** may still be reading from the database. */
4258 downgradeAllSharedCacheTableLocks(p);
4259 p->inTrans = TRANS_READ;
4260 }else{
4261 /* If the handle had any kind of transaction open, decrement the
4262 ** transaction count of the shared btree. If the transaction count
4263 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
4264 ** call below will unlock the pager. */
4265 if( p->inTrans!=TRANS_NONE ){
4266 clearAllSharedCacheTableLocks(p);
4267 pBt->nTransaction--;
4268 if( 0==pBt->nTransaction ){
4269 pBt->inTransaction = TRANS_NONE;
4273 /* Set the current transaction state to TRANS_NONE and unlock the
4274 ** pager if this call closed the only read or write transaction. */
4275 p->inTrans = TRANS_NONE;
4276 unlockBtreeIfUnused(pBt);
4279 btreeIntegrity(p);
4283 ** Commit the transaction currently in progress.
4285 ** This routine implements the second phase of a 2-phase commit. The
4286 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
4287 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
4288 ** routine did all the work of writing information out to disk and flushing the
4289 ** contents so that they are written onto the disk platter. All this
4290 ** routine has to do is delete or truncate or zero the header in the
4291 ** the rollback journal (which causes the transaction to commit) and
4292 ** drop locks.
4294 ** Normally, if an error occurs while the pager layer is attempting to
4295 ** finalize the underlying journal file, this function returns an error and
4296 ** the upper layer will attempt a rollback. However, if the second argument
4297 ** is non-zero then this b-tree transaction is part of a multi-file
4298 ** transaction. In this case, the transaction has already been committed
4299 ** (by deleting a super-journal file) and the caller will ignore this
4300 ** functions return code. So, even if an error occurs in the pager layer,
4301 ** reset the b-tree objects internal state to indicate that the write
4302 ** transaction has been closed. This is quite safe, as the pager will have
4303 ** transitioned to the error state.
4305 ** This will release the write lock on the database file. If there
4306 ** are no active cursors, it also releases the read lock.
4308 int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
4310 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
4311 sqlite3BtreeEnter(p);
4312 btreeIntegrity(p);
4314 /* If the handle has a write-transaction open, commit the shared-btrees
4315 ** transaction and set the shared state to TRANS_READ.
4317 if( p->inTrans==TRANS_WRITE ){
4318 int rc;
4319 BtShared *pBt = p->pBt;
4320 assert( pBt->inTransaction==TRANS_WRITE );
4321 assert( pBt->nTransaction>0 );
4322 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
4323 if( rc!=SQLITE_OK && bCleanup==0 ){
4324 sqlite3BtreeLeave(p);
4325 return rc;
4327 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */
4328 pBt->inTransaction = TRANS_READ;
4329 btreeClearHasContent(pBt);
4332 btreeEndTransaction(p);
4333 sqlite3BtreeLeave(p);
4334 return SQLITE_OK;
4338 ** Do both phases of a commit.
4340 int sqlite3BtreeCommit(Btree *p){
4341 int rc;
4342 sqlite3BtreeEnter(p);
4343 rc = sqlite3BtreeCommitPhaseOne(p, 0);
4344 if( rc==SQLITE_OK ){
4345 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
4347 sqlite3BtreeLeave(p);
4348 return rc;
4352 ** This routine sets the state to CURSOR_FAULT and the error
4353 ** code to errCode for every cursor on any BtShared that pBtree
4354 ** references. Or if the writeOnly flag is set to 1, then only
4355 ** trip write cursors and leave read cursors unchanged.
4357 ** Every cursor is a candidate to be tripped, including cursors
4358 ** that belong to other database connections that happen to be
4359 ** sharing the cache with pBtree.
4361 ** This routine gets called when a rollback occurs. If the writeOnly
4362 ** flag is true, then only write-cursors need be tripped - read-only
4363 ** cursors save their current positions so that they may continue
4364 ** following the rollback. Or, if writeOnly is false, all cursors are
4365 ** tripped. In general, writeOnly is false if the transaction being
4366 ** rolled back modified the database schema. In this case b-tree root
4367 ** pages may be moved or deleted from the database altogether, making
4368 ** it unsafe for read cursors to continue.
4370 ** If the writeOnly flag is true and an error is encountered while
4371 ** saving the current position of a read-only cursor, all cursors,
4372 ** including all read-cursors are tripped.
4374 ** SQLITE_OK is returned if successful, or if an error occurs while
4375 ** saving a cursor position, an SQLite error code.
4377 int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
4378 BtCursor *p;
4379 int rc = SQLITE_OK;
4381 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
4382 if( pBtree ){
4383 sqlite3BtreeEnter(pBtree);
4384 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
4385 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
4386 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
4387 rc = saveCursorPosition(p);
4388 if( rc!=SQLITE_OK ){
4389 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
4390 break;
4393 }else{
4394 sqlite3BtreeClearCursor(p);
4395 p->eState = CURSOR_FAULT;
4396 p->skipNext = errCode;
4398 btreeReleaseAllCursorPages(p);
4400 sqlite3BtreeLeave(pBtree);
4402 return rc;
4406 ** Set the pBt->nPage field correctly, according to the current
4407 ** state of the database. Assume pBt->pPage1 is valid.
4409 static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
4410 int nPage = get4byte(&pPage1->aData[28]);
4411 testcase( nPage==0 );
4412 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
4413 testcase( pBt->nPage!=(u32)nPage );
4414 pBt->nPage = nPage;
4418 ** Rollback the transaction in progress.
4420 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
4421 ** Only write cursors are tripped if writeOnly is true but all cursors are
4422 ** tripped if writeOnly is false. Any attempt to use
4423 ** a tripped cursor will result in an error.
4425 ** This will release the write lock on the database file. If there
4426 ** are no active cursors, it also releases the read lock.
4428 int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
4429 int rc;
4430 BtShared *pBt = p->pBt;
4431 MemPage *pPage1;
4433 assert( writeOnly==1 || writeOnly==0 );
4434 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
4435 sqlite3BtreeEnter(p);
4436 if( tripCode==SQLITE_OK ){
4437 rc = tripCode = saveAllCursors(pBt, 0, 0);
4438 if( rc ) writeOnly = 0;
4439 }else{
4440 rc = SQLITE_OK;
4442 if( tripCode ){
4443 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
4444 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
4445 if( rc2!=SQLITE_OK ) rc = rc2;
4447 btreeIntegrity(p);
4449 if( p->inTrans==TRANS_WRITE ){
4450 int rc2;
4452 assert( TRANS_WRITE==pBt->inTransaction );
4453 rc2 = sqlite3PagerRollback(pBt->pPager);
4454 if( rc2!=SQLITE_OK ){
4455 rc = rc2;
4458 /* The rollback may have destroyed the pPage1->aData value. So
4459 ** call btreeGetPage() on page 1 again to make
4460 ** sure pPage1->aData is set correctly. */
4461 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
4462 btreeSetNPage(pBt, pPage1);
4463 releasePageOne(pPage1);
4465 assert( countValidCursors(pBt, 1)==0 );
4466 pBt->inTransaction = TRANS_READ;
4467 btreeClearHasContent(pBt);
4470 btreeEndTransaction(p);
4471 sqlite3BtreeLeave(p);
4472 return rc;
4476 ** Start a statement subtransaction. The subtransaction can be rolled
4477 ** back independently of the main transaction. You must start a transaction
4478 ** before starting a subtransaction. The subtransaction is ended automatically
4479 ** if the main transaction commits or rolls back.
4481 ** Statement subtransactions are used around individual SQL statements
4482 ** that are contained within a BEGIN...COMMIT block. If a constraint
4483 ** error occurs within the statement, the effect of that one statement
4484 ** can be rolled back without having to rollback the entire transaction.
4486 ** A statement sub-transaction is implemented as an anonymous savepoint. The
4487 ** value passed as the second parameter is the total number of savepoints,
4488 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
4489 ** are no active savepoints and no other statement-transactions open,
4490 ** iStatement is 1. This anonymous savepoint can be released or rolled back
4491 ** using the sqlite3BtreeSavepoint() function.
4493 int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
4494 int rc;
4495 BtShared *pBt = p->pBt;
4496 sqlite3BtreeEnter(p);
4497 assert( p->inTrans==TRANS_WRITE );
4498 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
4499 assert( iStatement>0 );
4500 assert( iStatement>p->db->nSavepoint );
4501 assert( pBt->inTransaction==TRANS_WRITE );
4502 /* At the pager level, a statement transaction is a savepoint with
4503 ** an index greater than all savepoints created explicitly using
4504 ** SQL statements. It is illegal to open, release or rollback any
4505 ** such savepoints while the statement transaction savepoint is active.
4507 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
4508 sqlite3BtreeLeave(p);
4509 return rc;
4513 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
4514 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
4515 ** savepoint identified by parameter iSavepoint, depending on the value
4516 ** of op.
4518 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
4519 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
4520 ** contents of the entire transaction are rolled back. This is different
4521 ** from a normal transaction rollback, as no locks are released and the
4522 ** transaction remains open.
4524 int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
4525 int rc = SQLITE_OK;
4526 if( p && p->inTrans==TRANS_WRITE ){
4527 BtShared *pBt = p->pBt;
4528 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4529 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
4530 sqlite3BtreeEnter(p);
4531 if( op==SAVEPOINT_ROLLBACK ){
4532 rc = saveAllCursors(pBt, 0, 0);
4534 if( rc==SQLITE_OK ){
4535 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
4537 if( rc==SQLITE_OK ){
4538 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
4539 pBt->nPage = 0;
4541 rc = newDatabase(pBt);
4542 btreeSetNPage(pBt, pBt->pPage1);
4544 /* pBt->nPage might be zero if the database was corrupt when
4545 ** the transaction was started. Otherwise, it must be at least 1. */
4546 assert( CORRUPT_DB || pBt->nPage>0 );
4548 sqlite3BtreeLeave(p);
4550 return rc;
4554 ** Create a new cursor for the BTree whose root is on the page
4555 ** iTable. If a read-only cursor is requested, it is assumed that
4556 ** the caller already has at least a read-only transaction open
4557 ** on the database already. If a write-cursor is requested, then
4558 ** the caller is assumed to have an open write transaction.
4560 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
4561 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
4562 ** can be used for reading or for writing if other conditions for writing
4563 ** are also met. These are the conditions that must be met in order
4564 ** for writing to be allowed:
4566 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
4568 ** 2: Other database connections that share the same pager cache
4569 ** but which are not in the READ_UNCOMMITTED state may not have
4570 ** cursors open with wrFlag==0 on the same table. Otherwise
4571 ** the changes made by this write cursor would be visible to
4572 ** the read cursors in the other database connection.
4574 ** 3: The database must be writable (not on read-only media)
4576 ** 4: There must be an active transaction.
4578 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
4579 ** is set. If FORDELETE is set, that is a hint to the implementation that
4580 ** this cursor will only be used to seek to and delete entries of an index
4581 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
4582 ** this implementation. But in a hypothetical alternative storage engine
4583 ** in which index entries are automatically deleted when corresponding table
4584 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
4585 ** operations on this cursor can be no-ops and all READ operations can
4586 ** return a null row (2-bytes: 0x01 0x00).
4588 ** No checking is done to make sure that page iTable really is the
4589 ** root page of a b-tree. If it is not, then the cursor acquired
4590 ** will not work correctly.
4592 ** It is assumed that the sqlite3BtreeCursorZero() has been called
4593 ** on pCur to initialize the memory space prior to invoking this routine.
4595 static int btreeCursor(
4596 Btree *p, /* The btree */
4597 Pgno iTable, /* Root page of table to open */
4598 int wrFlag, /* 1 to write. 0 read-only */
4599 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4600 BtCursor *pCur /* Space for new cursor */
4602 BtShared *pBt = p->pBt; /* Shared b-tree handle */
4603 BtCursor *pX; /* Looping over other all cursors */
4605 assert( sqlite3BtreeHoldsMutex(p) );
4606 assert( wrFlag==0
4607 || wrFlag==BTREE_WRCSR
4608 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
4611 /* The following assert statements verify that if this is a sharable
4612 ** b-tree database, the connection is holding the required table locks,
4613 ** and that no other connection has any open cursor that conflicts with
4614 ** this lock. The iTable<1 term disables the check for corrupt schemas. */
4615 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1))
4616 || iTable<1 );
4617 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
4619 /* Assert that the caller has opened the required transaction. */
4620 assert( p->inTrans>TRANS_NONE );
4621 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
4622 assert( pBt->pPage1 && pBt->pPage1->aData );
4623 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
4625 if( iTable<=1 ){
4626 if( iTable<1 ){
4627 return SQLITE_CORRUPT_BKPT;
4628 }else if( btreePagecount(pBt)==0 ){
4629 assert( wrFlag==0 );
4630 iTable = 0;
4634 /* Now that no other errors can occur, finish filling in the BtCursor
4635 ** variables and link the cursor into the BtShared list. */
4636 pCur->pgnoRoot = iTable;
4637 pCur->iPage = -1;
4638 pCur->pKeyInfo = pKeyInfo;
4639 pCur->pBtree = p;
4640 pCur->pBt = pBt;
4641 pCur->curFlags = 0;
4642 /* If there are two or more cursors on the same btree, then all such
4643 ** cursors *must* have the BTCF_Multiple flag set. */
4644 for(pX=pBt->pCursor; pX; pX=pX->pNext){
4645 if( pX->pgnoRoot==iTable ){
4646 pX->curFlags |= BTCF_Multiple;
4647 pCur->curFlags = BTCF_Multiple;
4650 pCur->eState = CURSOR_INVALID;
4651 pCur->pNext = pBt->pCursor;
4652 pBt->pCursor = pCur;
4653 if( wrFlag ){
4654 pCur->curFlags |= BTCF_WriteFlag;
4655 pCur->curPagerFlags = 0;
4656 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt);
4657 }else{
4658 pCur->curPagerFlags = PAGER_GET_READONLY;
4660 return SQLITE_OK;
4662 static int btreeCursorWithLock(
4663 Btree *p, /* The btree */
4664 Pgno iTable, /* Root page of table to open */
4665 int wrFlag, /* 1 to write. 0 read-only */
4666 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4667 BtCursor *pCur /* Space for new cursor */
4669 int rc;
4670 sqlite3BtreeEnter(p);
4671 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4672 sqlite3BtreeLeave(p);
4673 return rc;
4675 int sqlite3BtreeCursor(
4676 Btree *p, /* The btree */
4677 Pgno iTable, /* Root page of table to open */
4678 int wrFlag, /* 1 to write. 0 read-only */
4679 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
4680 BtCursor *pCur /* Write new cursor here */
4682 if( p->sharable ){
4683 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur);
4684 }else{
4685 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4690 ** Return the size of a BtCursor object in bytes.
4692 ** This interfaces is needed so that users of cursors can preallocate
4693 ** sufficient storage to hold a cursor. The BtCursor object is opaque
4694 ** to users so they cannot do the sizeof() themselves - they must call
4695 ** this routine.
4697 int sqlite3BtreeCursorSize(void){
4698 return ROUND8(sizeof(BtCursor));
4702 ** Initialize memory that will be converted into a BtCursor object.
4704 ** The simple approach here would be to memset() the entire object
4705 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
4706 ** do not need to be zeroed and they are large, so we can save a lot
4707 ** of run-time by skipping the initialization of those elements.
4709 void sqlite3BtreeCursorZero(BtCursor *p){
4710 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
4714 ** Close a cursor. The read lock on the database file is released
4715 ** when the last cursor is closed.
4717 int sqlite3BtreeCloseCursor(BtCursor *pCur){
4718 Btree *pBtree = pCur->pBtree;
4719 if( pBtree ){
4720 BtShared *pBt = pCur->pBt;
4721 sqlite3BtreeEnter(pBtree);
4722 assert( pBt->pCursor!=0 );
4723 if( pBt->pCursor==pCur ){
4724 pBt->pCursor = pCur->pNext;
4725 }else{
4726 BtCursor *pPrev = pBt->pCursor;
4728 if( pPrev->pNext==pCur ){
4729 pPrev->pNext = pCur->pNext;
4730 break;
4732 pPrev = pPrev->pNext;
4733 }while( ALWAYS(pPrev) );
4735 btreeReleaseAllCursorPages(pCur);
4736 unlockBtreeIfUnused(pBt);
4737 sqlite3_free(pCur->aOverflow);
4738 sqlite3_free(pCur->pKey);
4739 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){
4740 /* Since the BtShared is not sharable, there is no need to
4741 ** worry about the missing sqlite3BtreeLeave() call here. */
4742 assert( pBtree->sharable==0 );
4743 sqlite3BtreeClose(pBtree);
4744 }else{
4745 sqlite3BtreeLeave(pBtree);
4747 pCur->pBtree = 0;
4749 return SQLITE_OK;
4753 ** Make sure the BtCursor* given in the argument has a valid
4754 ** BtCursor.info structure. If it is not already valid, call
4755 ** btreeParseCell() to fill it in.
4757 ** BtCursor.info is a cache of the information in the current cell.
4758 ** Using this cache reduces the number of calls to btreeParseCell().
4760 #ifndef NDEBUG
4761 static int cellInfoEqual(CellInfo *a, CellInfo *b){
4762 if( a->nKey!=b->nKey ) return 0;
4763 if( a->pPayload!=b->pPayload ) return 0;
4764 if( a->nPayload!=b->nPayload ) return 0;
4765 if( a->nLocal!=b->nLocal ) return 0;
4766 if( a->nSize!=b->nSize ) return 0;
4767 return 1;
4769 static void assertCellInfo(BtCursor *pCur){
4770 CellInfo info;
4771 memset(&info, 0, sizeof(info));
4772 btreeParseCell(pCur->pPage, pCur->ix, &info);
4773 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
4775 #else
4776 #define assertCellInfo(x)
4777 #endif
4778 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
4779 if( pCur->info.nSize==0 ){
4780 pCur->curFlags |= BTCF_ValidNKey;
4781 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
4782 }else{
4783 assertCellInfo(pCur);
4787 #ifndef NDEBUG /* The next routine used only within assert() statements */
4789 ** Return true if the given BtCursor is valid. A valid cursor is one
4790 ** that is currently pointing to a row in a (non-empty) table.
4791 ** This is a verification routine is used only within assert() statements.
4793 int sqlite3BtreeCursorIsValid(BtCursor *pCur){
4794 return pCur && pCur->eState==CURSOR_VALID;
4796 #endif /* NDEBUG */
4797 int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
4798 assert( pCur!=0 );
4799 return pCur->eState==CURSOR_VALID;
4803 ** Return the value of the integer key or "rowid" for a table btree.
4804 ** This routine is only valid for a cursor that is pointing into a
4805 ** ordinary table btree. If the cursor points to an index btree or
4806 ** is invalid, the result of this routine is undefined.
4808 i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
4809 assert( cursorHoldsMutex(pCur) );
4810 assert( pCur->eState==CURSOR_VALID );
4811 assert( pCur->curIntKey );
4812 getCellInfo(pCur);
4813 return pCur->info.nKey;
4817 ** Pin or unpin a cursor.
4819 void sqlite3BtreeCursorPin(BtCursor *pCur){
4820 assert( (pCur->curFlags & BTCF_Pinned)==0 );
4821 pCur->curFlags |= BTCF_Pinned;
4823 void sqlite3BtreeCursorUnpin(BtCursor *pCur){
4824 assert( (pCur->curFlags & BTCF_Pinned)!=0 );
4825 pCur->curFlags &= ~BTCF_Pinned;
4829 ** Return the offset into the database file for the start of the
4830 ** payload to which the cursor is pointing.
4832 i64 sqlite3BtreeOffset(BtCursor *pCur){
4833 assert( cursorHoldsMutex(pCur) );
4834 assert( pCur->eState==CURSOR_VALID );
4835 getCellInfo(pCur);
4836 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
4837 (i64)(pCur->info.pPayload - pCur->pPage->aData);
4841 ** Return the number of bytes of payload for the entry that pCur is
4842 ** currently pointing to. For table btrees, this will be the amount
4843 ** of data. For index btrees, this will be the size of the key.
4845 ** The caller must guarantee that the cursor is pointing to a non-NULL
4846 ** valid entry. In other words, the calling procedure must guarantee
4847 ** that the cursor has Cursor.eState==CURSOR_VALID.
4849 u32 sqlite3BtreePayloadSize(BtCursor *pCur){
4850 assert( cursorHoldsMutex(pCur) );
4851 assert( pCur->eState==CURSOR_VALID );
4852 getCellInfo(pCur);
4853 return pCur->info.nPayload;
4857 ** Return an upper bound on the size of any record for the table
4858 ** that the cursor is pointing into.
4860 ** This is an optimization. Everything will still work if this
4861 ** routine always returns 2147483647 (which is the largest record
4862 ** that SQLite can handle) or more. But returning a smaller value might
4863 ** prevent large memory allocations when trying to interpret a
4864 ** corrupt database.
4866 ** The current implementation merely returns the size of the underlying
4867 ** database file.
4869 sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
4870 assert( cursorHoldsMutex(pCur) );
4871 assert( pCur->eState==CURSOR_VALID );
4872 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
4876 ** Given the page number of an overflow page in the database (parameter
4877 ** ovfl), this function finds the page number of the next page in the
4878 ** linked list of overflow pages. If possible, it uses the auto-vacuum
4879 ** pointer-map data instead of reading the content of page ovfl to do so.
4881 ** If an error occurs an SQLite error code is returned. Otherwise:
4883 ** The page number of the next overflow page in the linked list is
4884 ** written to *pPgnoNext. If page ovfl is the last page in its linked
4885 ** list, *pPgnoNext is set to zero.
4887 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
4888 ** to page number pOvfl was obtained, then *ppPage is set to point to that
4889 ** reference. It is the responsibility of the caller to call releasePage()
4890 ** on *ppPage to free the reference. In no reference was obtained (because
4891 ** the pointer-map was used to obtain the value for *pPgnoNext), then
4892 ** *ppPage is set to zero.
4894 static int getOverflowPage(
4895 BtShared *pBt, /* The database file */
4896 Pgno ovfl, /* Current overflow page number */
4897 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
4898 Pgno *pPgnoNext /* OUT: Next overflow page number */
4900 Pgno next = 0;
4901 MemPage *pPage = 0;
4902 int rc = SQLITE_OK;
4904 assert( sqlite3_mutex_held(pBt->mutex) );
4905 assert(pPgnoNext);
4907 #ifndef SQLITE_OMIT_AUTOVACUUM
4908 /* Try to find the next page in the overflow list using the
4909 ** autovacuum pointer-map pages. Guess that the next page in
4910 ** the overflow list is page number (ovfl+1). If that guess turns
4911 ** out to be wrong, fall back to loading the data of page
4912 ** number ovfl to determine the next page number.
4914 if( pBt->autoVacuum ){
4915 Pgno pgno;
4916 Pgno iGuess = ovfl+1;
4917 u8 eType;
4919 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
4920 iGuess++;
4923 if( iGuess<=btreePagecount(pBt) ){
4924 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
4925 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
4926 next = iGuess;
4927 rc = SQLITE_DONE;
4931 #endif
4933 assert( next==0 || rc==SQLITE_DONE );
4934 if( rc==SQLITE_OK ){
4935 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
4936 assert( rc==SQLITE_OK || pPage==0 );
4937 if( rc==SQLITE_OK ){
4938 next = get4byte(pPage->aData);
4942 *pPgnoNext = next;
4943 if( ppPage ){
4944 *ppPage = pPage;
4945 }else{
4946 releasePage(pPage);
4948 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
4952 ** Copy data from a buffer to a page, or from a page to a buffer.
4954 ** pPayload is a pointer to data stored on database page pDbPage.
4955 ** If argument eOp is false, then nByte bytes of data are copied
4956 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
4957 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
4958 ** of data are copied from the buffer pBuf to pPayload.
4960 ** SQLITE_OK is returned on success, otherwise an error code.
4962 static int copyPayload(
4963 void *pPayload, /* Pointer to page data */
4964 void *pBuf, /* Pointer to buffer */
4965 int nByte, /* Number of bytes to copy */
4966 int eOp, /* 0 -> copy from page, 1 -> copy to page */
4967 DbPage *pDbPage /* Page containing pPayload */
4969 if( eOp ){
4970 /* Copy data from buffer to page (a write operation) */
4971 int rc = sqlite3PagerWrite(pDbPage);
4972 if( rc!=SQLITE_OK ){
4973 return rc;
4975 memcpy(pPayload, pBuf, nByte);
4976 }else{
4977 /* Copy data from page to buffer (a read operation) */
4978 memcpy(pBuf, pPayload, nByte);
4980 return SQLITE_OK;
4984 ** This function is used to read or overwrite payload information
4985 ** for the entry that the pCur cursor is pointing to. The eOp
4986 ** argument is interpreted as follows:
4988 ** 0: The operation is a read. Populate the overflow cache.
4989 ** 1: The operation is a write. Populate the overflow cache.
4991 ** A total of "amt" bytes are read or written beginning at "offset".
4992 ** Data is read to or from the buffer pBuf.
4994 ** The content being read or written might appear on the main page
4995 ** or be scattered out on multiple overflow pages.
4997 ** If the current cursor entry uses one or more overflow pages
4998 ** this function may allocate space for and lazily populate
4999 ** the overflow page-list cache array (BtCursor.aOverflow).
5000 ** Subsequent calls use this cache to make seeking to the supplied offset
5001 ** more efficient.
5003 ** Once an overflow page-list cache has been allocated, it must be
5004 ** invalidated if some other cursor writes to the same table, or if
5005 ** the cursor is moved to a different row. Additionally, in auto-vacuum
5006 ** mode, the following events may invalidate an overflow page-list cache.
5008 ** * An incremental vacuum,
5009 ** * A commit in auto_vacuum="full" mode,
5010 ** * Creating a table (may require moving an overflow page).
5012 static int accessPayload(
5013 BtCursor *pCur, /* Cursor pointing to entry to read from */
5014 u32 offset, /* Begin reading this far into payload */
5015 u32 amt, /* Read this many bytes */
5016 unsigned char *pBuf, /* Write the bytes into this buffer */
5017 int eOp /* zero to read. non-zero to write. */
5019 unsigned char *aPayload;
5020 int rc = SQLITE_OK;
5021 int iIdx = 0;
5022 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
5023 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
5024 #ifdef SQLITE_DIRECT_OVERFLOW_READ
5025 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
5026 #endif
5028 assert( pPage );
5029 assert( eOp==0 || eOp==1 );
5030 assert( pCur->eState==CURSOR_VALID );
5031 if( pCur->ix>=pPage->nCell ){
5032 return SQLITE_CORRUPT_PAGE(pPage);
5034 assert( cursorHoldsMutex(pCur) );
5036 getCellInfo(pCur);
5037 aPayload = pCur->info.pPayload;
5038 assert( offset+amt <= pCur->info.nPayload );
5040 assert( aPayload > pPage->aData );
5041 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
5042 /* Trying to read or write past the end of the data is an error. The
5043 ** conditional above is really:
5044 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
5045 ** but is recast into its current form to avoid integer overflow problems
5047 return SQLITE_CORRUPT_PAGE(pPage);
5050 /* Check if data must be read/written to/from the btree page itself. */
5051 if( offset<pCur->info.nLocal ){
5052 int a = amt;
5053 if( a+offset>pCur->info.nLocal ){
5054 a = pCur->info.nLocal - offset;
5056 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
5057 offset = 0;
5058 pBuf += a;
5059 amt -= a;
5060 }else{
5061 offset -= pCur->info.nLocal;
5065 if( rc==SQLITE_OK && amt>0 ){
5066 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
5067 Pgno nextPage;
5069 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
5071 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
5073 ** The aOverflow[] array is sized at one entry for each overflow page
5074 ** in the overflow chain. The page number of the first overflow page is
5075 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
5076 ** means "not yet known" (the cache is lazily populated).
5078 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
5079 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
5080 if( pCur->aOverflow==0
5081 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
5083 Pgno *aNew = (Pgno*)sqlite3Realloc(
5084 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
5086 if( aNew==0 ){
5087 return SQLITE_NOMEM_BKPT;
5088 }else{
5089 pCur->aOverflow = aNew;
5092 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
5093 pCur->curFlags |= BTCF_ValidOvfl;
5094 }else{
5095 /* If the overflow page-list cache has been allocated and the
5096 ** entry for the first required overflow page is valid, skip
5097 ** directly to it.
5099 if( pCur->aOverflow[offset/ovflSize] ){
5100 iIdx = (offset/ovflSize);
5101 nextPage = pCur->aOverflow[iIdx];
5102 offset = (offset%ovflSize);
5106 assert( rc==SQLITE_OK && amt>0 );
5107 while( nextPage ){
5108 /* If required, populate the overflow page-list cache. */
5109 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT;
5110 assert( pCur->aOverflow[iIdx]==0
5111 || pCur->aOverflow[iIdx]==nextPage
5112 || CORRUPT_DB );
5113 pCur->aOverflow[iIdx] = nextPage;
5115 if( offset>=ovflSize ){
5116 /* The only reason to read this page is to obtain the page
5117 ** number for the next page in the overflow chain. The page
5118 ** data is not required. So first try to lookup the overflow
5119 ** page-list cache, if any, then fall back to the getOverflowPage()
5120 ** function.
5122 assert( pCur->curFlags & BTCF_ValidOvfl );
5123 assert( pCur->pBtree->db==pBt->db );
5124 if( pCur->aOverflow[iIdx+1] ){
5125 nextPage = pCur->aOverflow[iIdx+1];
5126 }else{
5127 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
5129 offset -= ovflSize;
5130 }else{
5131 /* Need to read this page properly. It contains some of the
5132 ** range of data that is being read (eOp==0) or written (eOp!=0).
5134 int a = amt;
5135 if( a + offset > ovflSize ){
5136 a = ovflSize - offset;
5139 #ifdef SQLITE_DIRECT_OVERFLOW_READ
5140 /* If all the following are true:
5142 ** 1) this is a read operation, and
5143 ** 2) data is required from the start of this overflow page, and
5144 ** 3) there are no dirty pages in the page-cache
5145 ** 4) the database is file-backed, and
5146 ** 5) the page is not in the WAL file
5147 ** 6) at least 4 bytes have already been read into the output buffer
5149 ** then data can be read directly from the database file into the
5150 ** output buffer, bypassing the page-cache altogether. This speeds
5151 ** up loading large records that span many overflow pages.
5153 if( eOp==0 /* (1) */
5154 && offset==0 /* (2) */
5155 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */
5156 && &pBuf[-4]>=pBufStart /* (6) */
5158 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
5159 u8 aSave[4];
5160 u8 *aWrite = &pBuf[-4];
5161 assert( aWrite>=pBufStart ); /* due to (6) */
5162 memcpy(aSave, aWrite, 4);
5163 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
5164 nextPage = get4byte(aWrite);
5165 memcpy(aWrite, aSave, 4);
5166 }else
5167 #endif
5170 DbPage *pDbPage;
5171 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
5172 (eOp==0 ? PAGER_GET_READONLY : 0)
5174 if( rc==SQLITE_OK ){
5175 aPayload = sqlite3PagerGetData(pDbPage);
5176 nextPage = get4byte(aPayload);
5177 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
5178 sqlite3PagerUnref(pDbPage);
5179 offset = 0;
5182 amt -= a;
5183 if( amt==0 ) return rc;
5184 pBuf += a;
5186 if( rc ) break;
5187 iIdx++;
5191 if( rc==SQLITE_OK && amt>0 ){
5192 /* Overflow chain ends prematurely */
5193 return SQLITE_CORRUPT_PAGE(pPage);
5195 return rc;
5199 ** Read part of the payload for the row at which that cursor pCur is currently
5200 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
5201 ** begins at "offset".
5203 ** pCur can be pointing to either a table or an index b-tree.
5204 ** If pointing to a table btree, then the content section is read. If
5205 ** pCur is pointing to an index b-tree then the key section is read.
5207 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
5208 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
5209 ** cursor might be invalid or might need to be restored before being read.
5211 ** Return SQLITE_OK on success or an error code if anything goes
5212 ** wrong. An error is returned if "offset+amt" is larger than
5213 ** the available payload.
5215 int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
5216 assert( cursorHoldsMutex(pCur) );
5217 assert( pCur->eState==CURSOR_VALID );
5218 assert( pCur->iPage>=0 && pCur->pPage );
5219 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
5223 ** This variant of sqlite3BtreePayload() works even if the cursor has not
5224 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
5225 ** interface.
5227 #ifndef SQLITE_OMIT_INCRBLOB
5228 static SQLITE_NOINLINE int accessPayloadChecked(
5229 BtCursor *pCur,
5230 u32 offset,
5231 u32 amt,
5232 void *pBuf
5234 int rc;
5235 if ( pCur->eState==CURSOR_INVALID ){
5236 return SQLITE_ABORT;
5238 assert( cursorOwnsBtShared(pCur) );
5239 rc = btreeRestoreCursorPosition(pCur);
5240 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
5242 int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
5243 if( pCur->eState==CURSOR_VALID ){
5244 assert( cursorOwnsBtShared(pCur) );
5245 return accessPayload(pCur, offset, amt, pBuf, 0);
5246 }else{
5247 return accessPayloadChecked(pCur, offset, amt, pBuf);
5250 #endif /* SQLITE_OMIT_INCRBLOB */
5253 ** Return a pointer to payload information from the entry that the
5254 ** pCur cursor is pointing to. The pointer is to the beginning of
5255 ** the key if index btrees (pPage->intKey==0) and is the data for
5256 ** table btrees (pPage->intKey==1). The number of bytes of available
5257 ** key/data is written into *pAmt. If *pAmt==0, then the value
5258 ** returned will not be a valid pointer.
5260 ** This routine is an optimization. It is common for the entire key
5261 ** and data to fit on the local page and for there to be no overflow
5262 ** pages. When that is so, this routine can be used to access the
5263 ** key and data without making a copy. If the key and/or data spills
5264 ** onto overflow pages, then accessPayload() must be used to reassemble
5265 ** the key/data and copy it into a preallocated buffer.
5267 ** The pointer returned by this routine looks directly into the cached
5268 ** page of the database. The data might change or move the next time
5269 ** any btree routine is called.
5271 static const void *fetchPayload(
5272 BtCursor *pCur, /* Cursor pointing to entry to read from */
5273 u32 *pAmt /* Write the number of available bytes here */
5275 int amt;
5276 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
5277 assert( pCur->eState==CURSOR_VALID );
5278 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5279 assert( cursorOwnsBtShared(pCur) );
5280 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
5281 assert( pCur->info.nSize>0 );
5282 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
5283 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
5284 amt = pCur->info.nLocal;
5285 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
5286 /* There is too little space on the page for the expected amount
5287 ** of local content. Database must be corrupt. */
5288 assert( CORRUPT_DB );
5289 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
5291 *pAmt = (u32)amt;
5292 return (void*)pCur->info.pPayload;
5297 ** For the entry that cursor pCur is point to, return as
5298 ** many bytes of the key or data as are available on the local
5299 ** b-tree page. Write the number of available bytes into *pAmt.
5301 ** The pointer returned is ephemeral. The key/data may move
5302 ** or be destroyed on the next call to any Btree routine,
5303 ** including calls from other threads against the same cache.
5304 ** Hence, a mutex on the BtShared should be held prior to calling
5305 ** this routine.
5307 ** These routines is used to get quick access to key and data
5308 ** in the common case where no overflow pages are used.
5310 const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
5311 return fetchPayload(pCur, pAmt);
5316 ** Move the cursor down to a new child page. The newPgno argument is the
5317 ** page number of the child page to move to.
5319 ** This function returns SQLITE_CORRUPT if the page-header flags field of
5320 ** the new child page does not match the flags field of the parent (i.e.
5321 ** if an intkey page appears to be the parent of a non-intkey page, or
5322 ** vice-versa).
5324 static int moveToChild(BtCursor *pCur, u32 newPgno){
5325 int rc;
5326 assert( cursorOwnsBtShared(pCur) );
5327 assert( pCur->eState==CURSOR_VALID );
5328 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
5329 assert( pCur->iPage>=0 );
5330 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
5331 return SQLITE_CORRUPT_BKPT;
5333 pCur->info.nSize = 0;
5334 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5335 pCur->aiIdx[pCur->iPage] = pCur->ix;
5336 pCur->apPage[pCur->iPage] = pCur->pPage;
5337 pCur->ix = 0;
5338 pCur->iPage++;
5339 rc = getAndInitPage(pCur->pBt, newPgno, &pCur->pPage, pCur->curPagerFlags);
5340 assert( pCur->pPage!=0 || rc!=SQLITE_OK );
5341 if( rc==SQLITE_OK
5342 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
5344 releasePage(pCur->pPage);
5345 rc = SQLITE_CORRUPT_PGNO(newPgno);
5347 if( rc ){
5348 pCur->pPage = pCur->apPage[--pCur->iPage];
5350 return rc;
5353 #ifdef SQLITE_DEBUG
5355 ** Page pParent is an internal (non-leaf) tree page. This function
5356 ** asserts that page number iChild is the left-child if the iIdx'th
5357 ** cell in page pParent. Or, if iIdx is equal to the total number of
5358 ** cells in pParent, that page number iChild is the right-child of
5359 ** the page.
5361 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
5362 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
5363 ** in a corrupt database */
5364 assert( iIdx<=pParent->nCell );
5365 if( iIdx==pParent->nCell ){
5366 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
5367 }else{
5368 assert( get4byte(findCell(pParent, iIdx))==iChild );
5371 #else
5372 # define assertParentIndex(x,y,z)
5373 #endif
5376 ** Move the cursor up to the parent page.
5378 ** pCur->idx is set to the cell index that contains the pointer
5379 ** to the page we are coming from. If we are coming from the
5380 ** right-most child page then pCur->idx is set to one more than
5381 ** the largest cell index.
5383 static void moveToParent(BtCursor *pCur){
5384 MemPage *pLeaf;
5385 assert( cursorOwnsBtShared(pCur) );
5386 assert( pCur->eState==CURSOR_VALID );
5387 assert( pCur->iPage>0 );
5388 assert( pCur->pPage );
5389 assertParentIndex(
5390 pCur->apPage[pCur->iPage-1],
5391 pCur->aiIdx[pCur->iPage-1],
5392 pCur->pPage->pgno
5394 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
5395 pCur->info.nSize = 0;
5396 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5397 pCur->ix = pCur->aiIdx[pCur->iPage-1];
5398 pLeaf = pCur->pPage;
5399 pCur->pPage = pCur->apPage[--pCur->iPage];
5400 releasePageNotNull(pLeaf);
5404 ** Move the cursor to point to the root page of its b-tree structure.
5406 ** If the table has a virtual root page, then the cursor is moved to point
5407 ** to the virtual root page instead of the actual root page. A table has a
5408 ** virtual root page when the actual root page contains no cells and a
5409 ** single child page. This can only happen with the table rooted at page 1.
5411 ** If the b-tree structure is empty, the cursor state is set to
5412 ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
5413 ** the cursor is set to point to the first cell located on the root
5414 ** (or virtual root) page and the cursor state is set to CURSOR_VALID.
5416 ** If this function returns successfully, it may be assumed that the
5417 ** page-header flags indicate that the [virtual] root-page is the expected
5418 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
5419 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
5420 ** indicating a table b-tree, or if the caller did specify a KeyInfo
5421 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
5422 ** b-tree).
5424 static int moveToRoot(BtCursor *pCur){
5425 MemPage *pRoot;
5426 int rc = SQLITE_OK;
5428 assert( cursorOwnsBtShared(pCur) );
5429 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
5430 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
5431 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
5432 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
5433 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
5435 if( pCur->iPage>=0 ){
5436 if( pCur->iPage ){
5437 releasePageNotNull(pCur->pPage);
5438 while( --pCur->iPage ){
5439 releasePageNotNull(pCur->apPage[pCur->iPage]);
5441 pRoot = pCur->pPage = pCur->apPage[0];
5442 goto skip_init;
5444 }else if( pCur->pgnoRoot==0 ){
5445 pCur->eState = CURSOR_INVALID;
5446 return SQLITE_EMPTY;
5447 }else{
5448 assert( pCur->iPage==(-1) );
5449 if( pCur->eState>=CURSOR_REQUIRESEEK ){
5450 if( pCur->eState==CURSOR_FAULT ){
5451 assert( pCur->skipNext!=SQLITE_OK );
5452 return pCur->skipNext;
5454 sqlite3BtreeClearCursor(pCur);
5456 rc = getAndInitPage(pCur->pBt, pCur->pgnoRoot, &pCur->pPage,
5457 pCur->curPagerFlags);
5458 if( rc!=SQLITE_OK ){
5459 pCur->eState = CURSOR_INVALID;
5460 return rc;
5462 pCur->iPage = 0;
5463 pCur->curIntKey = pCur->pPage->intKey;
5465 pRoot = pCur->pPage;
5466 assert( pRoot->pgno==pCur->pgnoRoot || CORRUPT_DB );
5468 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
5469 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
5470 ** NULL, the caller expects a table b-tree. If this is not the case,
5471 ** return an SQLITE_CORRUPT error.
5473 ** Earlier versions of SQLite assumed that this test could not fail
5474 ** if the root page was already loaded when this function was called (i.e.
5475 ** if pCur->iPage>=0). But this is not so if the database is corrupted
5476 ** in such a way that page pRoot is linked into a second b-tree table
5477 ** (or the freelist). */
5478 assert( pRoot->intKey==1 || pRoot->intKey==0 );
5479 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
5480 return SQLITE_CORRUPT_PAGE(pCur->pPage);
5483 skip_init:
5484 pCur->ix = 0;
5485 pCur->info.nSize = 0;
5486 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
5488 if( pRoot->nCell>0 ){
5489 pCur->eState = CURSOR_VALID;
5490 }else if( !pRoot->leaf ){
5491 Pgno subpage;
5492 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
5493 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
5494 pCur->eState = CURSOR_VALID;
5495 rc = moveToChild(pCur, subpage);
5496 }else{
5497 pCur->eState = CURSOR_INVALID;
5498 rc = SQLITE_EMPTY;
5500 return rc;
5504 ** Move the cursor down to the left-most leaf entry beneath the
5505 ** entry to which it is currently pointing.
5507 ** The left-most leaf is the one with the smallest key - the first
5508 ** in ascending order.
5510 static int moveToLeftmost(BtCursor *pCur){
5511 Pgno pgno;
5512 int rc = SQLITE_OK;
5513 MemPage *pPage;
5515 assert( cursorOwnsBtShared(pCur) );
5516 assert( pCur->eState==CURSOR_VALID );
5517 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
5518 assert( pCur->ix<pPage->nCell );
5519 pgno = get4byte(findCell(pPage, pCur->ix));
5520 rc = moveToChild(pCur, pgno);
5522 return rc;
5526 ** Move the cursor down to the right-most leaf entry beneath the
5527 ** page to which it is currently pointing. Notice the difference
5528 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
5529 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
5530 ** finds the right-most entry beneath the *page*.
5532 ** The right-most entry is the one with the largest key - the last
5533 ** key in ascending order.
5535 static int moveToRightmost(BtCursor *pCur){
5536 Pgno pgno;
5537 int rc = SQLITE_OK;
5538 MemPage *pPage = 0;
5540 assert( cursorOwnsBtShared(pCur) );
5541 assert( pCur->eState==CURSOR_VALID );
5542 while( !(pPage = pCur->pPage)->leaf ){
5543 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5544 pCur->ix = pPage->nCell;
5545 rc = moveToChild(pCur, pgno);
5546 if( rc ) return rc;
5548 pCur->ix = pPage->nCell-1;
5549 assert( pCur->info.nSize==0 );
5550 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
5551 return SQLITE_OK;
5554 /* Move the cursor to the first entry in the table. Return SQLITE_OK
5555 ** on success. Set *pRes to 0 if the cursor actually points to something
5556 ** or set *pRes to 1 if the table is empty.
5558 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
5559 int rc;
5561 assert( cursorOwnsBtShared(pCur) );
5562 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5563 rc = moveToRoot(pCur);
5564 if( rc==SQLITE_OK ){
5565 assert( pCur->pPage->nCell>0 );
5566 *pRes = 0;
5567 rc = moveToLeftmost(pCur);
5568 }else if( rc==SQLITE_EMPTY ){
5569 assert( pCur->pgnoRoot==0 || (pCur->pPage!=0 && pCur->pPage->nCell==0) );
5570 *pRes = 1;
5571 rc = SQLITE_OK;
5573 return rc;
5576 /* Move the cursor to the last entry in the table. Return SQLITE_OK
5577 ** on success. Set *pRes to 0 if the cursor actually points to something
5578 ** or set *pRes to 1 if the table is empty.
5580 static SQLITE_NOINLINE int btreeLast(BtCursor *pCur, int *pRes){
5581 int rc = moveToRoot(pCur);
5582 if( rc==SQLITE_OK ){
5583 assert( pCur->eState==CURSOR_VALID );
5584 *pRes = 0;
5585 rc = moveToRightmost(pCur);
5586 if( rc==SQLITE_OK ){
5587 pCur->curFlags |= BTCF_AtLast;
5588 }else{
5589 pCur->curFlags &= ~BTCF_AtLast;
5591 }else if( rc==SQLITE_EMPTY ){
5592 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5593 *pRes = 1;
5594 rc = SQLITE_OK;
5596 return rc;
5598 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
5599 assert( cursorOwnsBtShared(pCur) );
5600 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5602 /* If the cursor already points to the last entry, this is a no-op. */
5603 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
5604 #ifdef SQLITE_DEBUG
5605 /* This block serves to assert() that the cursor really does point
5606 ** to the last entry in the b-tree. */
5607 int ii;
5608 for(ii=0; ii<pCur->iPage; ii++){
5609 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
5611 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB );
5612 testcase( pCur->ix!=pCur->pPage->nCell-1 );
5613 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */
5614 assert( pCur->pPage->leaf );
5615 #endif
5616 *pRes = 0;
5617 return SQLITE_OK;
5619 return btreeLast(pCur, pRes);
5622 /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY)
5623 ** table near the key intKey. Return a success code.
5625 ** If an exact match is not found, then the cursor is always
5626 ** left pointing at a leaf page which would hold the entry if it
5627 ** were present. The cursor might point to an entry that comes
5628 ** before or after the key.
5630 ** An integer is written into *pRes which is the result of
5631 ** comparing the key with the entry to which the cursor is
5632 ** pointing. The meaning of the integer written into
5633 ** *pRes is as follows:
5635 ** *pRes<0 The cursor is left pointing at an entry that
5636 ** is smaller than intKey or if the table is empty
5637 ** and the cursor is therefore left point to nothing.
5639 ** *pRes==0 The cursor is left pointing at an entry that
5640 ** exactly matches intKey.
5642 ** *pRes>0 The cursor is left pointing at an entry that
5643 ** is larger than intKey.
5645 int sqlite3BtreeTableMoveto(
5646 BtCursor *pCur, /* The cursor to be moved */
5647 i64 intKey, /* The table key */
5648 int biasRight, /* If true, bias the search to the high end */
5649 int *pRes /* Write search results here */
5651 int rc;
5653 assert( cursorOwnsBtShared(pCur) );
5654 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5655 assert( pRes );
5656 assert( pCur->pKeyInfo==0 );
5657 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 );
5659 /* If the cursor is already positioned at the point we are trying
5660 ** to move to, then just return without doing any work */
5661 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){
5662 if( pCur->info.nKey==intKey ){
5663 *pRes = 0;
5664 return SQLITE_OK;
5666 if( pCur->info.nKey<intKey ){
5667 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
5668 *pRes = -1;
5669 return SQLITE_OK;
5671 /* If the requested key is one more than the previous key, then
5672 ** try to get there using sqlite3BtreeNext() rather than a full
5673 ** binary search. This is an optimization only. The correct answer
5674 ** is still obtained without this case, only a little more slowly. */
5675 if( pCur->info.nKey+1==intKey ){
5676 *pRes = 0;
5677 rc = sqlite3BtreeNext(pCur, 0);
5678 if( rc==SQLITE_OK ){
5679 getCellInfo(pCur);
5680 if( pCur->info.nKey==intKey ){
5681 return SQLITE_OK;
5683 }else if( rc!=SQLITE_DONE ){
5684 return rc;
5690 #ifdef SQLITE_DEBUG
5691 pCur->pBtree->nSeek++; /* Performance measurement during testing */
5692 #endif
5694 rc = moveToRoot(pCur);
5695 if( rc ){
5696 if( rc==SQLITE_EMPTY ){
5697 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5698 *pRes = -1;
5699 return SQLITE_OK;
5701 return rc;
5703 assert( pCur->pPage );
5704 assert( pCur->pPage->isInit );
5705 assert( pCur->eState==CURSOR_VALID );
5706 assert( pCur->pPage->nCell > 0 );
5707 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
5708 assert( pCur->curIntKey );
5710 for(;;){
5711 int lwr, upr, idx, c;
5712 Pgno chldPg;
5713 MemPage *pPage = pCur->pPage;
5714 u8 *pCell; /* Pointer to current cell in pPage */
5716 /* pPage->nCell must be greater than zero. If this is the root-page
5717 ** the cursor would have been INVALID above and this for(;;) loop
5718 ** not run. If this is not the root-page, then the moveToChild() routine
5719 ** would have already detected db corruption. Similarly, pPage must
5720 ** be the right kind (index or table) of b-tree page. Otherwise
5721 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5722 assert( pPage->nCell>0 );
5723 assert( pPage->intKey );
5724 lwr = 0;
5725 upr = pPage->nCell-1;
5726 assert( biasRight==0 || biasRight==1 );
5727 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
5728 for(;;){
5729 i64 nCellKey;
5730 pCell = findCellPastPtr(pPage, idx);
5731 if( pPage->intKeyLeaf ){
5732 while( 0x80 <= *(pCell++) ){
5733 if( pCell>=pPage->aDataEnd ){
5734 return SQLITE_CORRUPT_PAGE(pPage);
5738 getVarint(pCell, (u64*)&nCellKey);
5739 if( nCellKey<intKey ){
5740 lwr = idx+1;
5741 if( lwr>upr ){ c = -1; break; }
5742 }else if( nCellKey>intKey ){
5743 upr = idx-1;
5744 if( lwr>upr ){ c = +1; break; }
5745 }else{
5746 assert( nCellKey==intKey );
5747 pCur->ix = (u16)idx;
5748 if( !pPage->leaf ){
5749 lwr = idx;
5750 goto moveto_table_next_layer;
5751 }else{
5752 pCur->curFlags |= BTCF_ValidNKey;
5753 pCur->info.nKey = nCellKey;
5754 pCur->info.nSize = 0;
5755 *pRes = 0;
5756 return SQLITE_OK;
5759 assert( lwr+upr>=0 );
5760 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5762 assert( lwr==upr+1 || !pPage->leaf );
5763 assert( pPage->isInit );
5764 if( pPage->leaf ){
5765 assert( pCur->ix<pCur->pPage->nCell );
5766 pCur->ix = (u16)idx;
5767 *pRes = c;
5768 rc = SQLITE_OK;
5769 goto moveto_table_finish;
5771 moveto_table_next_layer:
5772 if( lwr>=pPage->nCell ){
5773 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5774 }else{
5775 chldPg = get4byte(findCell(pPage, lwr));
5777 pCur->ix = (u16)lwr;
5778 rc = moveToChild(pCur, chldPg);
5779 if( rc ) break;
5781 moveto_table_finish:
5782 pCur->info.nSize = 0;
5783 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5784 return rc;
5788 ** Compare the "idx"-th cell on the page the cursor pCur is currently
5789 ** pointing to to pIdxKey using xRecordCompare. Return negative or
5790 ** zero if the cell is less than or equal pIdxKey. Return positive
5791 ** if unknown.
5793 ** Return value negative: Cell at pCur[idx] less than pIdxKey
5795 ** Return value is zero: Cell at pCur[idx] equals pIdxKey
5797 ** Return value positive: Nothing is known about the relationship
5798 ** of the cell at pCur[idx] and pIdxKey.
5800 ** This routine is part of an optimization. It is always safe to return
5801 ** a positive value as that will cause the optimization to be skipped.
5803 static int indexCellCompare(
5804 BtCursor *pCur,
5805 int idx,
5806 UnpackedRecord *pIdxKey,
5807 RecordCompare xRecordCompare
5809 MemPage *pPage = pCur->pPage;
5810 int c;
5811 int nCell; /* Size of the pCell cell in bytes */
5812 u8 *pCell = findCellPastPtr(pPage, idx);
5814 nCell = pCell[0];
5815 if( nCell<=pPage->max1bytePayload ){
5816 /* This branch runs if the record-size field of the cell is a
5817 ** single byte varint and the record fits entirely on the main
5818 ** b-tree page. */
5819 testcase( pCell+nCell+1==pPage->aDataEnd );
5820 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5821 }else if( !(pCell[1] & 0x80)
5822 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5824 /* The record-size field is a 2 byte varint and the record
5825 ** fits entirely on the main b-tree page. */
5826 testcase( pCell+nCell+2==pPage->aDataEnd );
5827 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5828 }else{
5829 /* If the record extends into overflow pages, do not attempt
5830 ** the optimization. */
5831 c = 99;
5833 return c;
5837 ** Return true (non-zero) if pCur is current pointing to the last
5838 ** page of a table.
5840 static int cursorOnLastPage(BtCursor *pCur){
5841 int i;
5842 assert( pCur->eState==CURSOR_VALID );
5843 for(i=0; i<pCur->iPage; i++){
5844 MemPage *pPage = pCur->apPage[i];
5845 if( pCur->aiIdx[i]<pPage->nCell ) return 0;
5847 return 1;
5850 /* Move the cursor so that it points to an entry in an index table
5851 ** near the key pIdxKey. Return a success code.
5853 ** If an exact match is not found, then the cursor is always
5854 ** left pointing at a leaf page which would hold the entry if it
5855 ** were present. The cursor might point to an entry that comes
5856 ** before or after the key.
5858 ** An integer is written into *pRes which is the result of
5859 ** comparing the key with the entry to which the cursor is
5860 ** pointing. The meaning of the integer written into
5861 ** *pRes is as follows:
5863 ** *pRes<0 The cursor is left pointing at an entry that
5864 ** is smaller than pIdxKey or if the table is empty
5865 ** and the cursor is therefore left point to nothing.
5867 ** *pRes==0 The cursor is left pointing at an entry that
5868 ** exactly matches pIdxKey.
5870 ** *pRes>0 The cursor is left pointing at an entry that
5871 ** is larger than pIdxKey.
5873 ** The pIdxKey->eqSeen field is set to 1 if there
5874 ** exists an entry in the table that exactly matches pIdxKey.
5876 int sqlite3BtreeIndexMoveto(
5877 BtCursor *pCur, /* The cursor to be moved */
5878 UnpackedRecord *pIdxKey, /* Unpacked index key */
5879 int *pRes /* Write search results here */
5881 int rc;
5882 RecordCompare xRecordCompare;
5884 assert( cursorOwnsBtShared(pCur) );
5885 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5886 assert( pRes );
5887 assert( pCur->pKeyInfo!=0 );
5889 #ifdef SQLITE_DEBUG
5890 pCur->pBtree->nSeek++; /* Performance measurement during testing */
5891 #endif
5893 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
5894 pIdxKey->errCode = 0;
5895 assert( pIdxKey->default_rc==1
5896 || pIdxKey->default_rc==0
5897 || pIdxKey->default_rc==-1
5901 /* Check to see if we can skip a lot of work. Two cases:
5903 ** (1) If the cursor is already pointing to the very last cell
5904 ** in the table and the pIdxKey search key is greater than or
5905 ** equal to that last cell, then no movement is required.
5907 ** (2) If the cursor is on the last page of the table and the first
5908 ** cell on that last page is less than or equal to the pIdxKey
5909 ** search key, then we can start the search on the current page
5910 ** without needing to go back to root.
5912 if( pCur->eState==CURSOR_VALID
5913 && pCur->pPage->leaf
5914 && cursorOnLastPage(pCur)
5916 int c;
5917 if( pCur->ix==pCur->pPage->nCell-1
5918 && (c = indexCellCompare(pCur, pCur->ix, pIdxKey, xRecordCompare))<=0
5919 && pIdxKey->errCode==SQLITE_OK
5921 *pRes = c;
5922 return SQLITE_OK; /* Cursor already pointing at the correct spot */
5924 if( pCur->iPage>0
5925 && indexCellCompare(pCur, 0, pIdxKey, xRecordCompare)<=0
5926 && pIdxKey->errCode==SQLITE_OK
5928 pCur->curFlags &= ~BTCF_ValidOvfl;
5929 if( !pCur->pPage->isInit ){
5930 return SQLITE_CORRUPT_BKPT;
5932 goto bypass_moveto_root; /* Start search on the current page */
5934 pIdxKey->errCode = SQLITE_OK;
5937 rc = moveToRoot(pCur);
5938 if( rc ){
5939 if( rc==SQLITE_EMPTY ){
5940 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5941 *pRes = -1;
5942 return SQLITE_OK;
5944 return rc;
5947 bypass_moveto_root:
5948 assert( pCur->pPage );
5949 assert( pCur->pPage->isInit );
5950 assert( pCur->eState==CURSOR_VALID );
5951 assert( pCur->pPage->nCell > 0 );
5952 assert( pCur->curIntKey==0 );
5953 assert( pIdxKey!=0 );
5954 for(;;){
5955 int lwr, upr, idx, c;
5956 Pgno chldPg;
5957 MemPage *pPage = pCur->pPage;
5958 u8 *pCell; /* Pointer to current cell in pPage */
5960 /* pPage->nCell must be greater than zero. If this is the root-page
5961 ** the cursor would have been INVALID above and this for(;;) loop
5962 ** not run. If this is not the root-page, then the moveToChild() routine
5963 ** would have already detected db corruption. Similarly, pPage must
5964 ** be the right kind (index or table) of b-tree page. Otherwise
5965 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5966 assert( pPage->nCell>0 );
5967 assert( pPage->intKey==0 );
5968 lwr = 0;
5969 upr = pPage->nCell-1;
5970 idx = upr>>1; /* idx = (lwr+upr)/2; */
5971 for(;;){
5972 int nCell; /* Size of the pCell cell in bytes */
5973 pCell = findCellPastPtr(pPage, idx);
5975 /* The maximum supported page-size is 65536 bytes. This means that
5976 ** the maximum number of record bytes stored on an index B-Tree
5977 ** page is less than 16384 bytes and may be stored as a 2-byte
5978 ** varint. This information is used to attempt to avoid parsing
5979 ** the entire cell by checking for the cases where the record is
5980 ** stored entirely within the b-tree page by inspecting the first
5981 ** 2 bytes of the cell.
5983 nCell = pCell[0];
5984 if( nCell<=pPage->max1bytePayload ){
5985 /* This branch runs if the record-size field of the cell is a
5986 ** single byte varint and the record fits entirely on the main
5987 ** b-tree page. */
5988 testcase( pCell+nCell+1==pPage->aDataEnd );
5989 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5990 }else if( !(pCell[1] & 0x80)
5991 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5993 /* The record-size field is a 2 byte varint and the record
5994 ** fits entirely on the main b-tree page. */
5995 testcase( pCell+nCell+2==pPage->aDataEnd );
5996 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5997 }else{
5998 /* The record flows over onto one or more overflow pages. In
5999 ** this case the whole cell needs to be parsed, a buffer allocated
6000 ** and accessPayload() used to retrieve the record into the
6001 ** buffer before VdbeRecordCompare() can be called.
6003 ** If the record is corrupt, the xRecordCompare routine may read
6004 ** up to two varints past the end of the buffer. An extra 18
6005 ** bytes of padding is allocated at the end of the buffer in
6006 ** case this happens. */
6007 void *pCellKey;
6008 u8 * const pCellBody = pCell - pPage->childPtrSize;
6009 const int nOverrun = 18; /* Size of the overrun padding */
6010 pPage->xParseCell(pPage, pCellBody, &pCur->info);
6011 nCell = (int)pCur->info.nKey;
6012 testcase( nCell<0 ); /* True if key size is 2^32 or more */
6013 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
6014 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
6015 testcase( nCell==2 ); /* Minimum legal index key size */
6016 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
6017 rc = SQLITE_CORRUPT_PAGE(pPage);
6018 goto moveto_index_finish;
6020 pCellKey = sqlite3Malloc( nCell+nOverrun );
6021 if( pCellKey==0 ){
6022 rc = SQLITE_NOMEM_BKPT;
6023 goto moveto_index_finish;
6025 pCur->ix = (u16)idx;
6026 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
6027 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
6028 pCur->curFlags &= ~BTCF_ValidOvfl;
6029 if( rc ){
6030 sqlite3_free(pCellKey);
6031 goto moveto_index_finish;
6033 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
6034 sqlite3_free(pCellKey);
6036 assert(
6037 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
6038 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
6040 if( c<0 ){
6041 lwr = idx+1;
6042 }else if( c>0 ){
6043 upr = idx-1;
6044 }else{
6045 assert( c==0 );
6046 *pRes = 0;
6047 rc = SQLITE_OK;
6048 pCur->ix = (u16)idx;
6049 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
6050 goto moveto_index_finish;
6052 if( lwr>upr ) break;
6053 assert( lwr+upr>=0 );
6054 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
6056 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
6057 assert( pPage->isInit );
6058 if( pPage->leaf ){
6059 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB );
6060 pCur->ix = (u16)idx;
6061 *pRes = c;
6062 rc = SQLITE_OK;
6063 goto moveto_index_finish;
6065 if( lwr>=pPage->nCell ){
6066 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
6067 }else{
6068 chldPg = get4byte(findCell(pPage, lwr));
6071 /* This block is similar to an in-lined version of:
6073 ** pCur->ix = (u16)lwr;
6074 ** rc = moveToChild(pCur, chldPg);
6075 ** if( rc ) break;
6077 pCur->info.nSize = 0;
6078 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
6079 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
6080 return SQLITE_CORRUPT_BKPT;
6082 pCur->aiIdx[pCur->iPage] = (u16)lwr;
6083 pCur->apPage[pCur->iPage] = pCur->pPage;
6084 pCur->ix = 0;
6085 pCur->iPage++;
6086 rc = getAndInitPage(pCur->pBt, chldPg, &pCur->pPage, pCur->curPagerFlags);
6087 if( rc==SQLITE_OK
6088 && (pCur->pPage->nCell<1 || pCur->pPage->intKey!=pCur->curIntKey)
6090 releasePage(pCur->pPage);
6091 rc = SQLITE_CORRUPT_PGNO(chldPg);
6093 if( rc ){
6094 pCur->pPage = pCur->apPage[--pCur->iPage];
6095 break;
6098 ***** End of in-lined moveToChild() call */
6100 moveto_index_finish:
6101 pCur->info.nSize = 0;
6102 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
6103 return rc;
6108 ** Return TRUE if the cursor is not pointing at an entry of the table.
6110 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
6111 ** past the last entry in the table or sqlite3BtreePrev() moves past
6112 ** the first entry. TRUE is also returned if the table is empty.
6114 int sqlite3BtreeEof(BtCursor *pCur){
6115 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
6116 ** have been deleted? This API will need to change to return an error code
6117 ** as well as the boolean result value.
6119 return (CURSOR_VALID!=pCur->eState);
6123 ** Return an estimate for the number of rows in the table that pCur is
6124 ** pointing to. Return a negative number if no estimate is currently
6125 ** available.
6127 i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
6128 i64 n;
6129 u8 i;
6131 assert( cursorOwnsBtShared(pCur) );
6132 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
6134 /* Currently this interface is only called by the OP_IfSmaller
6135 ** opcode, and it that case the cursor will always be valid and
6136 ** will always point to a leaf node. */
6137 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
6138 if( NEVER(pCur->pPage->leaf==0) ) return -1;
6140 n = pCur->pPage->nCell;
6141 for(i=0; i<pCur->iPage; i++){
6142 n *= pCur->apPage[i]->nCell;
6144 return n;
6148 ** Advance the cursor to the next entry in the database.
6149 ** Return value:
6151 ** SQLITE_OK success
6152 ** SQLITE_DONE cursor is already pointing at the last element
6153 ** otherwise some kind of error occurred
6155 ** The main entry point is sqlite3BtreeNext(). That routine is optimized
6156 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx
6157 ** to the next cell on the current page. The (slower) btreeNext() helper
6158 ** routine is called when it is necessary to move to a different page or
6159 ** to restore the cursor.
6161 ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
6162 ** cursor corresponds to an SQL index and this routine could have been
6163 ** skipped if the SQL index had been a unique index. The F argument
6164 ** is a hint to the implement. SQLite btree implementation does not use
6165 ** this hint, but COMDB2 does.
6167 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
6168 int rc;
6169 int idx;
6170 MemPage *pPage;
6172 assert( cursorOwnsBtShared(pCur) );
6173 if( pCur->eState!=CURSOR_VALID ){
6174 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
6175 rc = restoreCursorPosition(pCur);
6176 if( rc!=SQLITE_OK ){
6177 return rc;
6179 if( CURSOR_INVALID==pCur->eState ){
6180 return SQLITE_DONE;
6182 if( pCur->eState==CURSOR_SKIPNEXT ){
6183 pCur->eState = CURSOR_VALID;
6184 if( pCur->skipNext>0 ) return SQLITE_OK;
6188 pPage = pCur->pPage;
6189 idx = ++pCur->ix;
6190 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
6191 if( !pPage->isInit ){
6192 return SQLITE_CORRUPT_BKPT;
6195 if( idx>=pPage->nCell ){
6196 if( !pPage->leaf ){
6197 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
6198 if( rc ) return rc;
6199 return moveToLeftmost(pCur);
6202 if( pCur->iPage==0 ){
6203 pCur->eState = CURSOR_INVALID;
6204 return SQLITE_DONE;
6206 moveToParent(pCur);
6207 pPage = pCur->pPage;
6208 }while( pCur->ix>=pPage->nCell );
6209 if( pPage->intKey ){
6210 return sqlite3BtreeNext(pCur, 0);
6211 }else{
6212 return SQLITE_OK;
6215 if( pPage->leaf ){
6216 return SQLITE_OK;
6217 }else{
6218 return moveToLeftmost(pCur);
6221 int sqlite3BtreeNext(BtCursor *pCur, int flags){
6222 MemPage *pPage;
6223 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
6224 assert( cursorOwnsBtShared(pCur) );
6225 assert( flags==0 || flags==1 );
6226 pCur->info.nSize = 0;
6227 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
6228 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
6229 pPage = pCur->pPage;
6230 if( (++pCur->ix)>=pPage->nCell ){
6231 pCur->ix--;
6232 return btreeNext(pCur);
6234 if( pPage->leaf ){
6235 return SQLITE_OK;
6236 }else{
6237 return moveToLeftmost(pCur);
6242 ** Step the cursor to the back to the previous entry in the database.
6243 ** Return values:
6245 ** SQLITE_OK success
6246 ** SQLITE_DONE the cursor is already on the first element of the table
6247 ** otherwise some kind of error occurred
6249 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
6250 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
6251 ** to the previous cell on the current page. The (slower) btreePrevious()
6252 ** helper routine is called when it is necessary to move to a different page
6253 ** or to restore the cursor.
6255 ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
6256 ** the cursor corresponds to an SQL index and this routine could have been
6257 ** skipped if the SQL index had been a unique index. The F argument is a
6258 ** hint to the implement. The native SQLite btree implementation does not
6259 ** use this hint, but COMDB2 does.
6261 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
6262 int rc;
6263 MemPage *pPage;
6265 assert( cursorOwnsBtShared(pCur) );
6266 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
6267 assert( pCur->info.nSize==0 );
6268 if( pCur->eState!=CURSOR_VALID ){
6269 rc = restoreCursorPosition(pCur);
6270 if( rc!=SQLITE_OK ){
6271 return rc;
6273 if( CURSOR_INVALID==pCur->eState ){
6274 return SQLITE_DONE;
6276 if( CURSOR_SKIPNEXT==pCur->eState ){
6277 pCur->eState = CURSOR_VALID;
6278 if( pCur->skipNext<0 ) return SQLITE_OK;
6282 pPage = pCur->pPage;
6283 if( sqlite3FaultSim(412) ) pPage->isInit = 0;
6284 if( !pPage->isInit ){
6285 return SQLITE_CORRUPT_BKPT;
6287 if( !pPage->leaf ){
6288 int idx = pCur->ix;
6289 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
6290 if( rc ) return rc;
6291 rc = moveToRightmost(pCur);
6292 }else{
6293 while( pCur->ix==0 ){
6294 if( pCur->iPage==0 ){
6295 pCur->eState = CURSOR_INVALID;
6296 return SQLITE_DONE;
6298 moveToParent(pCur);
6300 assert( pCur->info.nSize==0 );
6301 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
6303 pCur->ix--;
6304 pPage = pCur->pPage;
6305 if( pPage->intKey && !pPage->leaf ){
6306 rc = sqlite3BtreePrevious(pCur, 0);
6307 }else{
6308 rc = SQLITE_OK;
6311 return rc;
6313 int sqlite3BtreePrevious(BtCursor *pCur, int flags){
6314 assert( cursorOwnsBtShared(pCur) );
6315 assert( flags==0 || flags==1 );
6316 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
6317 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
6318 pCur->info.nSize = 0;
6319 if( pCur->eState!=CURSOR_VALID
6320 || pCur->ix==0
6321 || pCur->pPage->leaf==0
6323 return btreePrevious(pCur);
6325 pCur->ix--;
6326 return SQLITE_OK;
6330 ** Allocate a new page from the database file.
6332 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
6333 ** has already been called on the new page.) The new page has also
6334 ** been referenced and the calling routine is responsible for calling
6335 ** sqlite3PagerUnref() on the new page when it is done.
6337 ** SQLITE_OK is returned on success. Any other return value indicates
6338 ** an error. *ppPage is set to NULL in the event of an error.
6340 ** If the "nearby" parameter is not 0, then an effort is made to
6341 ** locate a page close to the page number "nearby". This can be used in an
6342 ** attempt to keep related pages close to each other in the database file,
6343 ** which in turn can make database access faster.
6345 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
6346 ** anywhere on the free-list, then it is guaranteed to be returned. If
6347 ** eMode is BTALLOC_LT then the page returned will be less than or equal
6348 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
6349 ** are no restrictions on which page is returned.
6351 static int allocateBtreePage(
6352 BtShared *pBt, /* The btree */
6353 MemPage **ppPage, /* Store pointer to the allocated page here */
6354 Pgno *pPgno, /* Store the page number here */
6355 Pgno nearby, /* Search for a page near this one */
6356 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
6358 MemPage *pPage1;
6359 int rc;
6360 u32 n; /* Number of pages on the freelist */
6361 u32 k; /* Number of leaves on the trunk of the freelist */
6362 MemPage *pTrunk = 0;
6363 MemPage *pPrevTrunk = 0;
6364 Pgno mxPage; /* Total size of the database file */
6366 assert( sqlite3_mutex_held(pBt->mutex) );
6367 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
6368 pPage1 = pBt->pPage1;
6369 mxPage = btreePagecount(pBt);
6370 /* EVIDENCE-OF: R-21003-45125 The 4-byte big-endian integer at offset 36
6371 ** stores the total number of pages on the freelist. */
6372 n = get4byte(&pPage1->aData[36]);
6373 testcase( n==mxPage-1 );
6374 if( n>=mxPage ){
6375 return SQLITE_CORRUPT_BKPT;
6377 if( n>0 ){
6378 /* There are pages on the freelist. Reuse one of those pages. */
6379 Pgno iTrunk;
6380 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
6381 u32 nSearch = 0; /* Count of the number of search attempts */
6383 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
6384 ** shows that the page 'nearby' is somewhere on the free-list, then
6385 ** the entire-list will be searched for that page.
6387 #ifndef SQLITE_OMIT_AUTOVACUUM
6388 if( eMode==BTALLOC_EXACT ){
6389 if( nearby<=mxPage ){
6390 u8 eType;
6391 assert( nearby>0 );
6392 assert( pBt->autoVacuum );
6393 rc = ptrmapGet(pBt, nearby, &eType, 0);
6394 if( rc ) return rc;
6395 if( eType==PTRMAP_FREEPAGE ){
6396 searchList = 1;
6399 }else if( eMode==BTALLOC_LE ){
6400 searchList = 1;
6402 #endif
6404 /* Decrement the free-list count by 1. Set iTrunk to the index of the
6405 ** first free-list trunk page. iPrevTrunk is initially 1.
6407 rc = sqlite3PagerWrite(pPage1->pDbPage);
6408 if( rc ) return rc;
6409 put4byte(&pPage1->aData[36], n-1);
6411 /* The code within this loop is run only once if the 'searchList' variable
6412 ** is not true. Otherwise, it runs once for each trunk-page on the
6413 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
6414 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
6416 do {
6417 pPrevTrunk = pTrunk;
6418 if( pPrevTrunk ){
6419 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
6420 ** is the page number of the next freelist trunk page in the list or
6421 ** zero if this is the last freelist trunk page. */
6422 iTrunk = get4byte(&pPrevTrunk->aData[0]);
6423 }else{
6424 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
6425 ** stores the page number of the first page of the freelist, or zero if
6426 ** the freelist is empty. */
6427 iTrunk = get4byte(&pPage1->aData[32]);
6429 testcase( iTrunk==mxPage );
6430 if( iTrunk>mxPage || nSearch++ > n ){
6431 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
6432 }else{
6433 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
6435 if( rc ){
6436 pTrunk = 0;
6437 goto end_allocate_page;
6439 assert( pTrunk!=0 );
6440 assert( pTrunk->aData!=0 );
6441 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
6442 ** is the number of leaf page pointers to follow. */
6443 k = get4byte(&pTrunk->aData[4]);
6444 if( k==0 && !searchList ){
6445 /* The trunk has no leaves and the list is not being searched.
6446 ** So extract the trunk page itself and use it as the newly
6447 ** allocated page */
6448 assert( pPrevTrunk==0 );
6449 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6450 if( rc ){
6451 goto end_allocate_page;
6453 *pPgno = iTrunk;
6454 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
6455 *ppPage = pTrunk;
6456 pTrunk = 0;
6457 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
6458 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
6459 /* Value of k is out of range. Database corruption */
6460 rc = SQLITE_CORRUPT_PGNO(iTrunk);
6461 goto end_allocate_page;
6462 #ifndef SQLITE_OMIT_AUTOVACUUM
6463 }else if( searchList
6464 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
6466 /* The list is being searched and this trunk page is the page
6467 ** to allocate, regardless of whether it has leaves.
6469 *pPgno = iTrunk;
6470 *ppPage = pTrunk;
6471 searchList = 0;
6472 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6473 if( rc ){
6474 goto end_allocate_page;
6476 if( k==0 ){
6477 if( !pPrevTrunk ){
6478 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
6479 }else{
6480 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
6481 if( rc!=SQLITE_OK ){
6482 goto end_allocate_page;
6484 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
6486 }else{
6487 /* The trunk page is required by the caller but it contains
6488 ** pointers to free-list leaves. The first leaf becomes a trunk
6489 ** page in this case.
6491 MemPage *pNewTrunk;
6492 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
6493 if( iNewTrunk>mxPage ){
6494 rc = SQLITE_CORRUPT_PGNO(iTrunk);
6495 goto end_allocate_page;
6497 testcase( iNewTrunk==mxPage );
6498 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
6499 if( rc!=SQLITE_OK ){
6500 goto end_allocate_page;
6502 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
6503 if( rc!=SQLITE_OK ){
6504 releasePage(pNewTrunk);
6505 goto end_allocate_page;
6507 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
6508 put4byte(&pNewTrunk->aData[4], k-1);
6509 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
6510 releasePage(pNewTrunk);
6511 if( !pPrevTrunk ){
6512 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
6513 put4byte(&pPage1->aData[32], iNewTrunk);
6514 }else{
6515 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
6516 if( rc ){
6517 goto end_allocate_page;
6519 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
6522 pTrunk = 0;
6523 TRACE(("ALLOCATE: %u trunk - %u free pages left\n", *pPgno, n-1));
6524 #endif
6525 }else if( k>0 ){
6526 /* Extract a leaf from the trunk */
6527 u32 closest;
6528 Pgno iPage;
6529 unsigned char *aData = pTrunk->aData;
6530 if( nearby>0 ){
6531 u32 i;
6532 closest = 0;
6533 if( eMode==BTALLOC_LE ){
6534 for(i=0; i<k; i++){
6535 iPage = get4byte(&aData[8+i*4]);
6536 if( iPage<=nearby ){
6537 closest = i;
6538 break;
6541 }else{
6542 int dist;
6543 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
6544 for(i=1; i<k; i++){
6545 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
6546 if( d2<dist ){
6547 closest = i;
6548 dist = d2;
6552 }else{
6553 closest = 0;
6556 iPage = get4byte(&aData[8+closest*4]);
6557 testcase( iPage==mxPage );
6558 if( iPage>mxPage || iPage<2 ){
6559 rc = SQLITE_CORRUPT_PGNO(iTrunk);
6560 goto end_allocate_page;
6562 testcase( iPage==mxPage );
6563 if( !searchList
6564 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
6566 int noContent;
6567 *pPgno = iPage;
6568 TRACE(("ALLOCATE: %u was leaf %u of %u on trunk %u"
6569 ": %u more free pages\n",
6570 *pPgno, closest+1, k, pTrunk->pgno, n-1));
6571 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6572 if( rc ) goto end_allocate_page;
6573 if( closest<k-1 ){
6574 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
6576 put4byte(&aData[4], k-1);
6577 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
6578 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
6579 if( rc==SQLITE_OK ){
6580 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
6581 if( rc!=SQLITE_OK ){
6582 releasePage(*ppPage);
6583 *ppPage = 0;
6586 searchList = 0;
6589 releasePage(pPrevTrunk);
6590 pPrevTrunk = 0;
6591 }while( searchList );
6592 }else{
6593 /* There are no pages on the freelist, so append a new page to the
6594 ** database image.
6596 ** Normally, new pages allocated by this block can be requested from the
6597 ** pager layer with the 'no-content' flag set. This prevents the pager
6598 ** from trying to read the pages content from disk. However, if the
6599 ** current transaction has already run one or more incremental-vacuum
6600 ** steps, then the page we are about to allocate may contain content
6601 ** that is required in the event of a rollback. In this case, do
6602 ** not set the no-content flag. This causes the pager to load and journal
6603 ** the current page content before overwriting it.
6605 ** Note that the pager will not actually attempt to load or journal
6606 ** content for any page that really does lie past the end of the database
6607 ** file on disk. So the effects of disabling the no-content optimization
6608 ** here are confined to those pages that lie between the end of the
6609 ** database image and the end of the database file.
6611 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
6613 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6614 if( rc ) return rc;
6615 pBt->nPage++;
6616 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
6618 #ifndef SQLITE_OMIT_AUTOVACUUM
6619 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
6620 /* If *pPgno refers to a pointer-map page, allocate two new pages
6621 ** at the end of the file instead of one. The first allocated page
6622 ** becomes a new pointer-map page, the second is used by the caller.
6624 MemPage *pPg = 0;
6625 TRACE(("ALLOCATE: %u from end of file (pointer-map page)\n", pBt->nPage));
6626 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
6627 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
6628 if( rc==SQLITE_OK ){
6629 rc = sqlite3PagerWrite(pPg->pDbPage);
6630 releasePage(pPg);
6632 if( rc ) return rc;
6633 pBt->nPage++;
6634 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
6636 #endif
6637 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
6638 *pPgno = pBt->nPage;
6640 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
6641 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
6642 if( rc ) return rc;
6643 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
6644 if( rc!=SQLITE_OK ){
6645 releasePage(*ppPage);
6646 *ppPage = 0;
6648 TRACE(("ALLOCATE: %u from end of file\n", *pPgno));
6651 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) );
6653 end_allocate_page:
6654 releasePage(pTrunk);
6655 releasePage(pPrevTrunk);
6656 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
6657 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
6658 return rc;
6662 ** This function is used to add page iPage to the database file free-list.
6663 ** It is assumed that the page is not already a part of the free-list.
6665 ** The value passed as the second argument to this function is optional.
6666 ** If the caller happens to have a pointer to the MemPage object
6667 ** corresponding to page iPage handy, it may pass it as the second value.
6668 ** Otherwise, it may pass NULL.
6670 ** If a pointer to a MemPage object is passed as the second argument,
6671 ** its reference count is not altered by this function.
6673 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
6674 MemPage *pTrunk = 0; /* Free-list trunk page */
6675 Pgno iTrunk = 0; /* Page number of free-list trunk page */
6676 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
6677 MemPage *pPage; /* Page being freed. May be NULL. */
6678 int rc; /* Return Code */
6679 u32 nFree; /* Initial number of pages on free-list */
6681 assert( sqlite3_mutex_held(pBt->mutex) );
6682 assert( CORRUPT_DB || iPage>1 );
6683 assert( !pMemPage || pMemPage->pgno==iPage );
6685 if( iPage<2 || iPage>pBt->nPage ){
6686 return SQLITE_CORRUPT_BKPT;
6688 if( pMemPage ){
6689 pPage = pMemPage;
6690 sqlite3PagerRef(pPage->pDbPage);
6691 }else{
6692 pPage = btreePageLookup(pBt, iPage);
6695 /* Increment the free page count on pPage1 */
6696 rc = sqlite3PagerWrite(pPage1->pDbPage);
6697 if( rc ) goto freepage_out;
6698 nFree = get4byte(&pPage1->aData[36]);
6699 put4byte(&pPage1->aData[36], nFree+1);
6701 if( pBt->btsFlags & BTS_SECURE_DELETE ){
6702 /* If the secure_delete option is enabled, then
6703 ** always fully overwrite deleted information with zeros.
6705 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
6706 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
6708 goto freepage_out;
6710 memset(pPage->aData, 0, pPage->pBt->pageSize);
6713 /* If the database supports auto-vacuum, write an entry in the pointer-map
6714 ** to indicate that the page is free.
6716 if( ISAUTOVACUUM(pBt) ){
6717 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
6718 if( rc ) goto freepage_out;
6721 /* Now manipulate the actual database free-list structure. There are two
6722 ** possibilities. If the free-list is currently empty, or if the first
6723 ** trunk page in the free-list is full, then this page will become a
6724 ** new free-list trunk page. Otherwise, it will become a leaf of the
6725 ** first trunk page in the current free-list. This block tests if it
6726 ** is possible to add the page as a new free-list leaf.
6728 if( nFree!=0 ){
6729 u32 nLeaf; /* Initial number of leaf cells on trunk page */
6731 iTrunk = get4byte(&pPage1->aData[32]);
6732 if( iTrunk>btreePagecount(pBt) ){
6733 rc = SQLITE_CORRUPT_BKPT;
6734 goto freepage_out;
6736 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
6737 if( rc!=SQLITE_OK ){
6738 goto freepage_out;
6741 nLeaf = get4byte(&pTrunk->aData[4]);
6742 assert( pBt->usableSize>32 );
6743 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
6744 rc = SQLITE_CORRUPT_BKPT;
6745 goto freepage_out;
6747 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
6748 /* In this case there is room on the trunk page to insert the page
6749 ** being freed as a new leaf.
6751 ** Note that the trunk page is not really full until it contains
6752 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
6753 ** coded. But due to a coding error in versions of SQLite prior to
6754 ** 3.6.0, databases with freelist trunk pages holding more than
6755 ** usableSize/4 - 8 entries will be reported as corrupt. In order
6756 ** to maintain backwards compatibility with older versions of SQLite,
6757 ** we will continue to restrict the number of entries to usableSize/4 - 8
6758 ** for now. At some point in the future (once everyone has upgraded
6759 ** to 3.6.0 or later) we should consider fixing the conditional above
6760 ** to read "usableSize/4-2" instead of "usableSize/4-8".
6762 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
6763 ** avoid using the last six entries in the freelist trunk page array in
6764 ** order that database files created by newer versions of SQLite can be
6765 ** read by older versions of SQLite.
6767 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6768 if( rc==SQLITE_OK ){
6769 put4byte(&pTrunk->aData[4], nLeaf+1);
6770 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
6771 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
6772 sqlite3PagerDontWrite(pPage->pDbPage);
6774 rc = btreeSetHasContent(pBt, iPage);
6776 TRACE(("FREE-PAGE: %u leaf on trunk page %u\n",pPage->pgno,pTrunk->pgno));
6777 goto freepage_out;
6781 /* If control flows to this point, then it was not possible to add the
6782 ** the page being freed as a leaf page of the first trunk in the free-list.
6783 ** Possibly because the free-list is empty, or possibly because the
6784 ** first trunk in the free-list is full. Either way, the page being freed
6785 ** will become the new first trunk page in the free-list.
6787 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
6788 goto freepage_out;
6790 rc = sqlite3PagerWrite(pPage->pDbPage);
6791 if( rc!=SQLITE_OK ){
6792 goto freepage_out;
6794 put4byte(pPage->aData, iTrunk);
6795 put4byte(&pPage->aData[4], 0);
6796 put4byte(&pPage1->aData[32], iPage);
6797 TRACE(("FREE-PAGE: %u new trunk page replacing %u\n", pPage->pgno, iTrunk));
6799 freepage_out:
6800 if( pPage ){
6801 pPage->isInit = 0;
6803 releasePage(pPage);
6804 releasePage(pTrunk);
6805 return rc;
6807 static void freePage(MemPage *pPage, int *pRC){
6808 if( (*pRC)==SQLITE_OK ){
6809 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6814 ** Free the overflow pages associated with the given Cell.
6816 static SQLITE_NOINLINE int clearCellOverflow(
6817 MemPage *pPage, /* The page that contains the Cell */
6818 unsigned char *pCell, /* First byte of the Cell */
6819 CellInfo *pInfo /* Size information about the cell */
6821 BtShared *pBt;
6822 Pgno ovflPgno;
6823 int rc;
6824 int nOvfl;
6825 u32 ovflPageSize;
6827 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6828 assert( pInfo->nLocal!=pInfo->nPayload );
6829 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
6830 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
6831 if( pCell + pInfo->nSize > pPage->aDataEnd ){
6832 /* Cell extends past end of page */
6833 return SQLITE_CORRUPT_PAGE(pPage);
6835 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
6836 pBt = pPage->pBt;
6837 assert( pBt->usableSize > 4 );
6838 ovflPageSize = pBt->usableSize - 4;
6839 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
6840 assert( nOvfl>0 ||
6841 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
6843 while( nOvfl-- ){
6844 Pgno iNext = 0;
6845 MemPage *pOvfl = 0;
6846 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
6847 /* 0 is not a legal page number and page 1 cannot be an
6848 ** overflow page. Therefore if ovflPgno<2 or past the end of the
6849 ** file the database must be corrupt. */
6850 return SQLITE_CORRUPT_BKPT;
6852 if( nOvfl ){
6853 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
6854 if( rc ) return rc;
6857 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
6858 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
6860 /* There is no reason any cursor should have an outstanding reference
6861 ** to an overflow page belonging to a cell that is being deleted/updated.
6862 ** So if there exists more than one reference to this page, then it
6863 ** must not really be an overflow page and the database must be corrupt.
6864 ** It is helpful to detect this before calling freePage2(), as
6865 ** freePage2() may zero the page contents if secure-delete mode is
6866 ** enabled. If this 'overflow' page happens to be a page that the
6867 ** caller is iterating through or using in some other way, this
6868 ** can be problematic.
6870 rc = SQLITE_CORRUPT_BKPT;
6871 }else{
6872 rc = freePage2(pBt, pOvfl, ovflPgno);
6875 if( pOvfl ){
6876 sqlite3PagerUnref(pOvfl->pDbPage);
6878 if( rc ) return rc;
6879 ovflPgno = iNext;
6881 return SQLITE_OK;
6884 /* Call xParseCell to compute the size of a cell. If the cell contains
6885 ** overflow, then invoke cellClearOverflow to clear out that overflow.
6886 ** Store the result code (SQLITE_OK or some error code) in rc.
6888 ** Implemented as macro to force inlining for performance.
6890 #define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \
6891 pPage->xParseCell(pPage, pCell, &sInfo); \
6892 if( sInfo.nLocal!=sInfo.nPayload ){ \
6893 rc = clearCellOverflow(pPage, pCell, &sInfo); \
6894 }else{ \
6895 rc = SQLITE_OK; \
6900 ** Create the byte sequence used to represent a cell on page pPage
6901 ** and write that byte sequence into pCell[]. Overflow pages are
6902 ** allocated and filled in as necessary. The calling procedure
6903 ** is responsible for making sure sufficient space has been allocated
6904 ** for pCell[].
6906 ** Note that pCell does not necessary need to point to the pPage->aData
6907 ** area. pCell might point to some temporary storage. The cell will
6908 ** be constructed in this temporary area then copied into pPage->aData
6909 ** later.
6911 static int fillInCell(
6912 MemPage *pPage, /* The page that contains the cell */
6913 unsigned char *pCell, /* Complete text of the cell */
6914 const BtreePayload *pX, /* Payload with which to construct the cell */
6915 int *pnSize /* Write cell size here */
6917 int nPayload;
6918 const u8 *pSrc;
6919 int nSrc, n, rc, mn;
6920 int spaceLeft;
6921 MemPage *pToRelease;
6922 unsigned char *pPrior;
6923 unsigned char *pPayload;
6924 BtShared *pBt;
6925 Pgno pgnoOvfl;
6926 int nHeader;
6928 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6930 /* pPage is not necessarily writeable since pCell might be auxiliary
6931 ** buffer space that is separate from the pPage buffer area */
6932 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
6933 || sqlite3PagerIswriteable(pPage->pDbPage) );
6935 /* Fill in the header. */
6936 nHeader = pPage->childPtrSize;
6937 if( pPage->intKey ){
6938 nPayload = pX->nData + pX->nZero;
6939 pSrc = pX->pData;
6940 nSrc = pX->nData;
6941 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
6942 nHeader += putVarint32(&pCell[nHeader], nPayload);
6943 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
6944 }else{
6945 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
6946 nSrc = nPayload = (int)pX->nKey;
6947 pSrc = pX->pKey;
6948 nHeader += putVarint32(&pCell[nHeader], nPayload);
6951 /* Fill in the payload */
6952 pPayload = &pCell[nHeader];
6953 if( nPayload<=pPage->maxLocal ){
6954 /* This is the common case where everything fits on the btree page
6955 ** and no overflow pages are required. */
6956 n = nHeader + nPayload;
6957 testcase( n==3 );
6958 testcase( n==4 );
6959 if( n<4 ) n = 4;
6960 *pnSize = n;
6961 assert( nSrc<=nPayload );
6962 testcase( nSrc<nPayload );
6963 memcpy(pPayload, pSrc, nSrc);
6964 memset(pPayload+nSrc, 0, nPayload-nSrc);
6965 return SQLITE_OK;
6968 /* If we reach this point, it means that some of the content will need
6969 ** to spill onto overflow pages.
6971 mn = pPage->minLocal;
6972 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
6973 testcase( n==pPage->maxLocal );
6974 testcase( n==pPage->maxLocal+1 );
6975 if( n > pPage->maxLocal ) n = mn;
6976 spaceLeft = n;
6977 *pnSize = n + nHeader + 4;
6978 pPrior = &pCell[nHeader+n];
6979 pToRelease = 0;
6980 pgnoOvfl = 0;
6981 pBt = pPage->pBt;
6983 /* At this point variables should be set as follows:
6985 ** nPayload Total payload size in bytes
6986 ** pPayload Begin writing payload here
6987 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
6988 ** that means content must spill into overflow pages.
6989 ** *pnSize Size of the local cell (not counting overflow pages)
6990 ** pPrior Where to write the pgno of the first overflow page
6992 ** Use a call to btreeParseCellPtr() to verify that the values above
6993 ** were computed correctly.
6995 #ifdef SQLITE_DEBUG
6997 CellInfo info;
6998 pPage->xParseCell(pPage, pCell, &info);
6999 assert( nHeader==(int)(info.pPayload - pCell) );
7000 assert( info.nKey==pX->nKey );
7001 assert( *pnSize == info.nSize );
7002 assert( spaceLeft == info.nLocal );
7004 #endif
7006 /* Write the payload into the local Cell and any extra into overflow pages */
7007 while( 1 ){
7008 n = nPayload;
7009 if( n>spaceLeft ) n = spaceLeft;
7011 /* If pToRelease is not zero than pPayload points into the data area
7012 ** of pToRelease. Make sure pToRelease is still writeable. */
7013 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
7015 /* If pPayload is part of the data area of pPage, then make sure pPage
7016 ** is still writeable */
7017 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
7018 || sqlite3PagerIswriteable(pPage->pDbPage) );
7020 if( nSrc>=n ){
7021 memcpy(pPayload, pSrc, n);
7022 }else if( nSrc>0 ){
7023 n = nSrc;
7024 memcpy(pPayload, pSrc, n);
7025 }else{
7026 memset(pPayload, 0, n);
7028 nPayload -= n;
7029 if( nPayload<=0 ) break;
7030 pPayload += n;
7031 pSrc += n;
7032 nSrc -= n;
7033 spaceLeft -= n;
7034 if( spaceLeft==0 ){
7035 MemPage *pOvfl = 0;
7036 #ifndef SQLITE_OMIT_AUTOVACUUM
7037 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
7038 if( pBt->autoVacuum ){
7040 pgnoOvfl++;
7041 } while(
7042 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
7045 #endif
7046 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
7047 #ifndef SQLITE_OMIT_AUTOVACUUM
7048 /* If the database supports auto-vacuum, and the second or subsequent
7049 ** overflow page is being allocated, add an entry to the pointer-map
7050 ** for that page now.
7052 ** If this is the first overflow page, then write a partial entry
7053 ** to the pointer-map. If we write nothing to this pointer-map slot,
7054 ** then the optimistic overflow chain processing in clearCell()
7055 ** may misinterpret the uninitialized values and delete the
7056 ** wrong pages from the database.
7058 if( pBt->autoVacuum && rc==SQLITE_OK ){
7059 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
7060 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
7061 if( rc ){
7062 releasePage(pOvfl);
7065 #endif
7066 if( rc ){
7067 releasePage(pToRelease);
7068 return rc;
7071 /* If pToRelease is not zero than pPrior points into the data area
7072 ** of pToRelease. Make sure pToRelease is still writeable. */
7073 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
7075 /* If pPrior is part of the data area of pPage, then make sure pPage
7076 ** is still writeable */
7077 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
7078 || sqlite3PagerIswriteable(pPage->pDbPage) );
7080 put4byte(pPrior, pgnoOvfl);
7081 releasePage(pToRelease);
7082 pToRelease = pOvfl;
7083 pPrior = pOvfl->aData;
7084 put4byte(pPrior, 0);
7085 pPayload = &pOvfl->aData[4];
7086 spaceLeft = pBt->usableSize - 4;
7089 releasePage(pToRelease);
7090 return SQLITE_OK;
7094 ** Remove the i-th cell from pPage. This routine effects pPage only.
7095 ** The cell content is not freed or deallocated. It is assumed that
7096 ** the cell content has been copied someplace else. This routine just
7097 ** removes the reference to the cell from pPage.
7099 ** "sz" must be the number of bytes in the cell.
7101 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
7102 u32 pc; /* Offset to cell content of cell being deleted */
7103 u8 *data; /* pPage->aData */
7104 u8 *ptr; /* Used to move bytes around within data[] */
7105 int rc; /* The return code */
7106 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
7108 if( *pRC ) return;
7109 assert( idx>=0 );
7110 assert( idx<pPage->nCell );
7111 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
7112 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
7113 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7114 assert( pPage->nFree>=0 );
7115 data = pPage->aData;
7116 ptr = &pPage->aCellIdx[2*idx];
7117 assert( pPage->pBt->usableSize > (u32)(ptr-data) );
7118 pc = get2byte(ptr);
7119 hdr = pPage->hdrOffset;
7120 testcase( pc==(u32)get2byte(&data[hdr+5]) );
7121 testcase( pc+sz==pPage->pBt->usableSize );
7122 if( pc+sz > pPage->pBt->usableSize ){
7123 *pRC = SQLITE_CORRUPT_BKPT;
7124 return;
7126 rc = freeSpace(pPage, pc, sz);
7127 if( rc ){
7128 *pRC = rc;
7129 return;
7131 pPage->nCell--;
7132 if( pPage->nCell==0 ){
7133 memset(&data[hdr+1], 0, 4);
7134 data[hdr+7] = 0;
7135 put2byte(&data[hdr+5], pPage->pBt->usableSize);
7136 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
7137 - pPage->childPtrSize - 8;
7138 }else{
7139 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
7140 put2byte(&data[hdr+3], pPage->nCell);
7141 pPage->nFree += 2;
7146 ** Insert a new cell on pPage at cell index "i". pCell points to the
7147 ** content of the cell.
7149 ** If the cell content will fit on the page, then put it there. If it
7150 ** will not fit, then make a copy of the cell content into pTemp if
7151 ** pTemp is not null. Regardless of pTemp, allocate a new entry
7152 ** in pPage->apOvfl[] and make it point to the cell content (either
7153 ** in pTemp or the original pCell) and also record its index.
7154 ** Allocating a new entry in pPage->aCell[] implies that
7155 ** pPage->nOverflow is incremented.
7157 ** The insertCellFast() routine below works exactly the same as
7158 ** insertCell() except that it lacks the pTemp and iChild parameters
7159 ** which are assumed zero. Other than that, the two routines are the
7160 ** same.
7162 ** Fixes or enhancements to this routine should be reflected in
7163 ** insertCellFast()!
7165 static int insertCell(
7166 MemPage *pPage, /* Page into which we are copying */
7167 int i, /* New cell becomes the i-th cell of the page */
7168 u8 *pCell, /* Content of the new cell */
7169 int sz, /* Bytes of content in pCell */
7170 u8 *pTemp, /* Temp storage space for pCell, if needed */
7171 Pgno iChild /* If non-zero, replace first 4 bytes with this value */
7173 int idx = 0; /* Where to write new cell content in data[] */
7174 int j; /* Loop counter */
7175 u8 *data; /* The content of the whole page */
7176 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
7178 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
7179 assert( MX_CELL(pPage->pBt)<=10921 );
7180 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
7181 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
7182 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
7183 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7184 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
7185 assert( pPage->nFree>=0 );
7186 assert( iChild>0 );
7187 if( pPage->nOverflow || sz+2>pPage->nFree ){
7188 if( pTemp ){
7189 memcpy(pTemp, pCell, sz);
7190 pCell = pTemp;
7192 put4byte(pCell, iChild);
7193 j = pPage->nOverflow++;
7194 /* Comparison against ArraySize-1 since we hold back one extra slot
7195 ** as a contingency. In other words, never need more than 3 overflow
7196 ** slots but 4 are allocated, just to be safe. */
7197 assert( j < ArraySize(pPage->apOvfl)-1 );
7198 pPage->apOvfl[j] = pCell;
7199 pPage->aiOvfl[j] = (u16)i;
7201 /* When multiple overflows occur, they are always sequential and in
7202 ** sorted order. This invariants arise because multiple overflows can
7203 ** only occur when inserting divider cells into the parent page during
7204 ** balancing, and the dividers are adjacent and sorted.
7206 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
7207 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
7208 }else{
7209 int rc = sqlite3PagerWrite(pPage->pDbPage);
7210 if( NEVER(rc!=SQLITE_OK) ){
7211 return rc;
7213 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
7214 data = pPage->aData;
7215 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
7216 rc = allocateSpace(pPage, sz, &idx);
7217 if( rc ){ return rc; }
7218 /* The allocateSpace() routine guarantees the following properties
7219 ** if it returns successfully */
7220 assert( idx >= 0 );
7221 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
7222 assert( idx+sz <= (int)pPage->pBt->usableSize );
7223 pPage->nFree -= (u16)(2 + sz);
7224 /* In a corrupt database where an entry in the cell index section of
7225 ** a btree page has a value of 3 or less, the pCell value might point
7226 ** as many as 4 bytes in front of the start of the aData buffer for
7227 ** the source page. Make sure this does not cause problems by not
7228 ** reading the first 4 bytes */
7229 memcpy(&data[idx+4], pCell+4, sz-4);
7230 put4byte(&data[idx], iChild);
7231 pIns = pPage->aCellIdx + i*2;
7232 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
7233 put2byte(pIns, idx);
7234 pPage->nCell++;
7235 /* increment the cell count */
7236 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
7237 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
7238 #ifndef SQLITE_OMIT_AUTOVACUUM
7239 if( pPage->pBt->autoVacuum ){
7240 int rc2 = SQLITE_OK;
7241 /* The cell may contain a pointer to an overflow page. If so, write
7242 ** the entry for the overflow page into the pointer map.
7244 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
7245 if( rc2 ) return rc2;
7247 #endif
7249 return SQLITE_OK;
7253 ** This variant of insertCell() assumes that the pTemp and iChild
7254 ** parameters are both zero. Use this variant in sqlite3BtreeInsert()
7255 ** for performance improvement, and also so that this variant is only
7256 ** called from that one place, and is thus inlined, and thus runs must
7257 ** faster.
7259 ** Fixes or enhancements to this routine should be reflected into
7260 ** the insertCell() routine.
7262 static int insertCellFast(
7263 MemPage *pPage, /* Page into which we are copying */
7264 int i, /* New cell becomes the i-th cell of the page */
7265 u8 *pCell, /* Content of the new cell */
7266 int sz /* Bytes of content in pCell */
7268 int idx = 0; /* Where to write new cell content in data[] */
7269 int j; /* Loop counter */
7270 u8 *data; /* The content of the whole page */
7271 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
7273 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
7274 assert( MX_CELL(pPage->pBt)<=10921 );
7275 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
7276 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
7277 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
7278 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7279 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
7280 assert( pPage->nFree>=0 );
7281 assert( pPage->nOverflow==0 );
7282 if( sz+2>pPage->nFree ){
7283 j = pPage->nOverflow++;
7284 /* Comparison against ArraySize-1 since we hold back one extra slot
7285 ** as a contingency. In other words, never need more than 3 overflow
7286 ** slots but 4 are allocated, just to be safe. */
7287 assert( j < ArraySize(pPage->apOvfl)-1 );
7288 pPage->apOvfl[j] = pCell;
7289 pPage->aiOvfl[j] = (u16)i;
7291 /* When multiple overflows occur, they are always sequential and in
7292 ** sorted order. This invariants arise because multiple overflows can
7293 ** only occur when inserting divider cells into the parent page during
7294 ** balancing, and the dividers are adjacent and sorted.
7296 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
7297 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
7298 }else{
7299 int rc = sqlite3PagerWrite(pPage->pDbPage);
7300 if( rc!=SQLITE_OK ){
7301 return rc;
7303 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
7304 data = pPage->aData;
7305 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
7306 rc = allocateSpace(pPage, sz, &idx);
7307 if( rc ){ return rc; }
7308 /* The allocateSpace() routine guarantees the following properties
7309 ** if it returns successfully */
7310 assert( idx >= 0 );
7311 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
7312 assert( idx+sz <= (int)pPage->pBt->usableSize );
7313 pPage->nFree -= (u16)(2 + sz);
7314 memcpy(&data[idx], pCell, sz);
7315 pIns = pPage->aCellIdx + i*2;
7316 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
7317 put2byte(pIns, idx);
7318 pPage->nCell++;
7319 /* increment the cell count */
7320 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
7321 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB );
7322 #ifndef SQLITE_OMIT_AUTOVACUUM
7323 if( pPage->pBt->autoVacuum ){
7324 int rc2 = SQLITE_OK;
7325 /* The cell may contain a pointer to an overflow page. If so, write
7326 ** the entry for the overflow page into the pointer map.
7328 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc2);
7329 if( rc2 ) return rc2;
7331 #endif
7333 return SQLITE_OK;
7337 ** The following parameters determine how many adjacent pages get involved
7338 ** in a balancing operation. NN is the number of neighbors on either side
7339 ** of the page that participate in the balancing operation. NB is the
7340 ** total number of pages that participate, including the target page and
7341 ** NN neighbors on either side.
7343 ** The minimum value of NN is 1 (of course). Increasing NN above 1
7344 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
7345 ** in exchange for a larger degradation in INSERT and UPDATE performance.
7346 ** The value of NN appears to give the best results overall.
7348 ** (Later:) The description above makes it seem as if these values are
7349 ** tunable - as if you could change them and recompile and it would all work.
7350 ** But that is unlikely. NB has been 3 since the inception of SQLite and
7351 ** we have never tested any other value.
7353 #define NN 1 /* Number of neighbors on either side of pPage */
7354 #define NB 3 /* (NN*2+1): Total pages involved in the balance */
7357 ** A CellArray object contains a cache of pointers and sizes for a
7358 ** consecutive sequence of cells that might be held on multiple pages.
7360 ** The cells in this array are the divider cell or cells from the pParent
7361 ** page plus up to three child pages. There are a total of nCell cells.
7363 ** pRef is a pointer to one of the pages that contributes cells. This is
7364 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize
7365 ** which should be common to all pages that contribute cells to this array.
7367 ** apCell[] and szCell[] hold, respectively, pointers to the start of each
7368 ** cell and the size of each cell. Some of the apCell[] pointers might refer
7369 ** to overflow cells. In other words, some apCel[] pointers might not point
7370 ** to content area of the pages.
7372 ** A szCell[] of zero means the size of that cell has not yet been computed.
7374 ** The cells come from as many as four different pages:
7376 ** -----------
7377 ** | Parent |
7378 ** -----------
7379 ** / | \
7380 ** / | \
7381 ** --------- --------- ---------
7382 ** |Child-1| |Child-2| |Child-3|
7383 ** --------- --------- ---------
7385 ** The order of cells is in the array is for an index btree is:
7387 ** 1. All cells from Child-1 in order
7388 ** 2. The first divider cell from Parent
7389 ** 3. All cells from Child-2 in order
7390 ** 4. The second divider cell from Parent
7391 ** 5. All cells from Child-3 in order
7393 ** For a table-btree (with rowids) the items 2 and 4 are empty because
7394 ** content exists only in leaves and there are no divider cells.
7396 ** For an index btree, the apEnd[] array holds pointer to the end of page
7397 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3,
7398 ** respectively. The ixNx[] array holds the number of cells contained in
7399 ** each of these 5 stages, and all stages to the left. Hence:
7401 ** ixNx[0] = Number of cells in Child-1.
7402 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider.
7403 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider.
7404 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells
7405 ** ixNx[4] = Total number of cells.
7407 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2]
7408 ** are used and they point to the leaf pages only, and the ixNx value are:
7410 ** ixNx[0] = Number of cells in Child-1.
7411 ** ixNx[1] = Number of cells in Child-1 and Child-2.
7412 ** ixNx[2] = Total number of cells.
7414 ** Sometimes when deleting, a child page can have zero cells. In those
7415 ** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[]
7416 ** entries, shift down. The end result is that each ixNx[] entry should
7417 ** be larger than the previous
7419 typedef struct CellArray CellArray;
7420 struct CellArray {
7421 int nCell; /* Number of cells in apCell[] */
7422 MemPage *pRef; /* Reference page */
7423 u8 **apCell; /* All cells begin balanced */
7424 u16 *szCell; /* Local size of all cells in apCell[] */
7425 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */
7426 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */
7430 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
7431 ** computed.
7433 static void populateCellCache(CellArray *p, int idx, int N){
7434 MemPage *pRef = p->pRef;
7435 u16 *szCell = p->szCell;
7436 assert( idx>=0 && idx+N<=p->nCell );
7437 while( N>0 ){
7438 assert( p->apCell[idx]!=0 );
7439 if( szCell[idx]==0 ){
7440 szCell[idx] = pRef->xCellSize(pRef, p->apCell[idx]);
7441 }else{
7442 assert( CORRUPT_DB ||
7443 szCell[idx]==pRef->xCellSize(pRef, p->apCell[idx]) );
7445 idx++;
7446 N--;
7451 ** Return the size of the Nth element of the cell array
7453 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
7454 assert( N>=0 && N<p->nCell );
7455 assert( p->szCell[N]==0 );
7456 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
7457 return p->szCell[N];
7459 static u16 cachedCellSize(CellArray *p, int N){
7460 assert( N>=0 && N<p->nCell );
7461 if( p->szCell[N] ) return p->szCell[N];
7462 return computeCellSize(p, N);
7466 ** Array apCell[] contains pointers to nCell b-tree page cells. The
7467 ** szCell[] array contains the size in bytes of each cell. This function
7468 ** replaces the current contents of page pPg with the contents of the cell
7469 ** array.
7471 ** Some of the cells in apCell[] may currently be stored in pPg. This
7472 ** function works around problems caused by this by making a copy of any
7473 ** such cells before overwriting the page data.
7475 ** The MemPage.nFree field is invalidated by this function. It is the
7476 ** responsibility of the caller to set it correctly.
7478 static int rebuildPage(
7479 CellArray *pCArray, /* Content to be added to page pPg */
7480 int iFirst, /* First cell in pCArray to use */
7481 int nCell, /* Final number of cells on page */
7482 MemPage *pPg /* The page to be reconstructed */
7484 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
7485 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
7486 const int usableSize = pPg->pBt->usableSize;
7487 u8 * const pEnd = &aData[usableSize];
7488 int i = iFirst; /* Which cell to copy from pCArray*/
7489 u32 j; /* Start of cell content area */
7490 int iEnd = i+nCell; /* Loop terminator */
7491 u8 *pCellptr = pPg->aCellIdx;
7492 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
7493 u8 *pData;
7494 int k; /* Current slot in pCArray->apEnd[] */
7495 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */
7497 assert( nCell>0 );
7498 assert( i<iEnd );
7499 j = get2byte(&aData[hdr+5]);
7500 if( j>(u32)usableSize ){ j = 0; }
7501 memcpy(&pTmp[j], &aData[j], usableSize - j);
7503 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i; k++){}
7504 pSrcEnd = pCArray->apEnd[k];
7506 pData = pEnd;
7507 while( 1/*exit by break*/ ){
7508 u8 *pCell = pCArray->apCell[i];
7509 u16 sz = pCArray->szCell[i];
7510 assert( sz>0 );
7511 if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){
7512 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
7513 pCell = &pTmp[pCell - aData];
7514 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
7515 && (uptr)(pCell)<(uptr)pSrcEnd
7517 return SQLITE_CORRUPT_BKPT;
7520 pData -= sz;
7521 put2byte(pCellptr, (pData - aData));
7522 pCellptr += 2;
7523 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
7524 memmove(pData, pCell, sz);
7525 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
7526 i++;
7527 if( i>=iEnd ) break;
7528 if( pCArray->ixNx[k]<=i ){
7529 k++;
7530 pSrcEnd = pCArray->apEnd[k];
7534 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
7535 pPg->nCell = nCell;
7536 pPg->nOverflow = 0;
7538 put2byte(&aData[hdr+1], 0);
7539 put2byte(&aData[hdr+3], pPg->nCell);
7540 put2byte(&aData[hdr+5], pData - aData);
7541 aData[hdr+7] = 0x00;
7542 return SQLITE_OK;
7546 ** The pCArray objects contains pointers to b-tree cells and the cell sizes.
7547 ** This function attempts to add the cells stored in the array to page pPg.
7548 ** If it cannot (because the page needs to be defragmented before the cells
7549 ** will fit), non-zero is returned. Otherwise, if the cells are added
7550 ** successfully, zero is returned.
7552 ** Argument pCellptr points to the first entry in the cell-pointer array
7553 ** (part of page pPg) to populate. After cell apCell[0] is written to the
7554 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
7555 ** cell in the array. It is the responsibility of the caller to ensure
7556 ** that it is safe to overwrite this part of the cell-pointer array.
7558 ** When this function is called, *ppData points to the start of the
7559 ** content area on page pPg. If the size of the content area is extended,
7560 ** *ppData is updated to point to the new start of the content area
7561 ** before returning.
7563 ** Finally, argument pBegin points to the byte immediately following the
7564 ** end of the space required by this page for the cell-pointer area (for
7565 ** all cells - not just those inserted by the current call). If the content
7566 ** area must be extended to before this point in order to accommodate all
7567 ** cells in apCell[], then the cells do not fit and non-zero is returned.
7569 static int pageInsertArray(
7570 MemPage *pPg, /* Page to add cells to */
7571 u8 *pBegin, /* End of cell-pointer array */
7572 u8 **ppData, /* IN/OUT: Page content-area pointer */
7573 u8 *pCellptr, /* Pointer to cell-pointer area */
7574 int iFirst, /* Index of first cell to add */
7575 int nCell, /* Number of cells to add to pPg */
7576 CellArray *pCArray /* Array of cells */
7578 int i = iFirst; /* Loop counter - cell index to insert */
7579 u8 *aData = pPg->aData; /* Complete page */
7580 u8 *pData = *ppData; /* Content area. A subset of aData[] */
7581 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */
7582 int k; /* Current slot in pCArray->apEnd[] */
7583 u8 *pEnd; /* Maximum extent of cell data */
7584 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
7585 if( iEnd<=iFirst ) return 0;
7586 for(k=0; ALWAYS(k<NB*2) && pCArray->ixNx[k]<=i ; k++){}
7587 pEnd = pCArray->apEnd[k];
7588 while( 1 /*Exit by break*/ ){
7589 int sz, rc;
7590 u8 *pSlot;
7591 assert( pCArray->szCell[i]!=0 );
7592 sz = pCArray->szCell[i];
7593 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
7594 if( (pData - pBegin)<sz ) return 1;
7595 pData -= sz;
7596 pSlot = pData;
7598 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
7599 ** database. But they might for a corrupt database. Hence use memmove()
7600 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
7601 assert( (pSlot+sz)<=pCArray->apCell[i]
7602 || pSlot>=(pCArray->apCell[i]+sz)
7603 || CORRUPT_DB );
7604 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
7605 && (uptr)(pCArray->apCell[i])<(uptr)pEnd
7607 assert( CORRUPT_DB );
7608 (void)SQLITE_CORRUPT_BKPT;
7609 return 1;
7611 memmove(pSlot, pCArray->apCell[i], sz);
7612 put2byte(pCellptr, (pSlot - aData));
7613 pCellptr += 2;
7614 i++;
7615 if( i>=iEnd ) break;
7616 if( pCArray->ixNx[k]<=i ){
7617 k++;
7618 pEnd = pCArray->apEnd[k];
7621 *ppData = pData;
7622 return 0;
7626 ** The pCArray object contains pointers to b-tree cells and their sizes.
7628 ** This function adds the space associated with each cell in the array
7629 ** that is currently stored within the body of pPg to the pPg free-list.
7630 ** The cell-pointers and other fields of the page are not updated.
7632 ** This function returns the total number of cells added to the free-list.
7634 static int pageFreeArray(
7635 MemPage *pPg, /* Page to edit */
7636 int iFirst, /* First cell to delete */
7637 int nCell, /* Cells to delete */
7638 CellArray *pCArray /* Array of cells */
7640 u8 * const aData = pPg->aData;
7641 u8 * const pEnd = &aData[pPg->pBt->usableSize];
7642 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
7643 int nRet = 0;
7644 int i, j;
7645 int iEnd = iFirst + nCell;
7646 int nFree = 0;
7647 int aOfst[10];
7648 int aAfter[10];
7650 for(i=iFirst; i<iEnd; i++){
7651 u8 *pCell = pCArray->apCell[i];
7652 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
7653 int sz;
7654 int iAfter;
7655 int iOfst;
7656 /* No need to use cachedCellSize() here. The sizes of all cells that
7657 ** are to be freed have already been computing while deciding which
7658 ** cells need freeing */
7659 sz = pCArray->szCell[i]; assert( sz>0 );
7660 iOfst = (u16)(pCell - aData);
7661 iAfter = iOfst+sz;
7662 for(j=0; j<nFree; j++){
7663 if( aOfst[j]==iAfter ){
7664 aOfst[j] = iOfst;
7665 break;
7666 }else if( aAfter[j]==iOfst ){
7667 aAfter[j] = iAfter;
7668 break;
7671 if( j>=nFree ){
7672 if( nFree>=(int)(sizeof(aOfst)/sizeof(aOfst[0])) ){
7673 for(j=0; j<nFree; j++){
7674 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
7676 nFree = 0;
7678 aOfst[nFree] = iOfst;
7679 aAfter[nFree] = iAfter;
7680 if( &aData[iAfter]>pEnd ) return 0;
7681 nFree++;
7683 nRet++;
7686 for(j=0; j<nFree; j++){
7687 freeSpace(pPg, aOfst[j], aAfter[j]-aOfst[j]);
7689 return nRet;
7693 ** pCArray contains pointers to and sizes of all cells in the page being
7694 ** balanced. The current page, pPg, has pPg->nCell cells starting with
7695 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells
7696 ** starting at apCell[iNew].
7698 ** This routine makes the necessary adjustments to pPg so that it contains
7699 ** the correct cells after being balanced.
7701 ** The pPg->nFree field is invalid when this function returns. It is the
7702 ** responsibility of the caller to set it correctly.
7704 static int editPage(
7705 MemPage *pPg, /* Edit this page */
7706 int iOld, /* Index of first cell currently on page */
7707 int iNew, /* Index of new first cell on page */
7708 int nNew, /* Final number of cells on page */
7709 CellArray *pCArray /* Array of cells and sizes */
7711 u8 * const aData = pPg->aData;
7712 const int hdr = pPg->hdrOffset;
7713 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
7714 int nCell = pPg->nCell; /* Cells stored on pPg */
7715 u8 *pData;
7716 u8 *pCellptr;
7717 int i;
7718 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
7719 int iNewEnd = iNew + nNew;
7721 #ifdef SQLITE_DEBUG
7722 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
7723 memcpy(pTmp, aData, pPg->pBt->usableSize);
7724 #endif
7726 /* Remove cells from the start and end of the page */
7727 assert( nCell>=0 );
7728 if( iOld<iNew ){
7729 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
7730 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
7731 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
7732 nCell -= nShift;
7734 if( iNewEnd < iOldEnd ){
7735 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
7736 assert( nCell>=nTail );
7737 nCell -= nTail;
7740 pData = &aData[get2byte(&aData[hdr+5])];
7741 if( pData<pBegin ) goto editpage_fail;
7742 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail;
7744 /* Add cells to the start of the page */
7745 if( iNew<iOld ){
7746 int nAdd = MIN(nNew,iOld-iNew);
7747 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
7748 assert( nAdd>=0 );
7749 pCellptr = pPg->aCellIdx;
7750 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
7751 if( pageInsertArray(
7752 pPg, pBegin, &pData, pCellptr,
7753 iNew, nAdd, pCArray
7754 ) ) goto editpage_fail;
7755 nCell += nAdd;
7758 /* Add any overflow cells */
7759 for(i=0; i<pPg->nOverflow; i++){
7760 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
7761 if( iCell>=0 && iCell<nNew ){
7762 pCellptr = &pPg->aCellIdx[iCell * 2];
7763 if( nCell>iCell ){
7764 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
7766 nCell++;
7767 cachedCellSize(pCArray, iCell+iNew);
7768 if( pageInsertArray(
7769 pPg, pBegin, &pData, pCellptr,
7770 iCell+iNew, 1, pCArray
7771 ) ) goto editpage_fail;
7775 /* Append cells to the end of the page */
7776 assert( nCell>=0 );
7777 pCellptr = &pPg->aCellIdx[nCell*2];
7778 if( pageInsertArray(
7779 pPg, pBegin, &pData, pCellptr,
7780 iNew+nCell, nNew-nCell, pCArray
7781 ) ) goto editpage_fail;
7783 pPg->nCell = nNew;
7784 pPg->nOverflow = 0;
7786 put2byte(&aData[hdr+3], pPg->nCell);
7787 put2byte(&aData[hdr+5], pData - aData);
7789 #ifdef SQLITE_DEBUG
7790 for(i=0; i<nNew && !CORRUPT_DB; i++){
7791 u8 *pCell = pCArray->apCell[i+iNew];
7792 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
7793 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
7794 pCell = &pTmp[pCell - aData];
7796 assert( 0==memcmp(pCell, &aData[iOff],
7797 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
7799 #endif
7801 return SQLITE_OK;
7802 editpage_fail:
7803 /* Unable to edit this page. Rebuild it from scratch instead. */
7804 if( nNew<1 ) return SQLITE_CORRUPT_BKPT;
7805 populateCellCache(pCArray, iNew, nNew);
7806 return rebuildPage(pCArray, iNew, nNew, pPg);
7810 #ifndef SQLITE_OMIT_QUICKBALANCE
7812 ** This version of balance() handles the common special case where
7813 ** a new entry is being inserted on the extreme right-end of the
7814 ** tree, in other words, when the new entry will become the largest
7815 ** entry in the tree.
7817 ** Instead of trying to balance the 3 right-most leaf pages, just add
7818 ** a new page to the right-hand side and put the one new entry in
7819 ** that page. This leaves the right side of the tree somewhat
7820 ** unbalanced. But odds are that we will be inserting new entries
7821 ** at the end soon afterwards so the nearly empty page will quickly
7822 ** fill up. On average.
7824 ** pPage is the leaf page which is the right-most page in the tree.
7825 ** pParent is its parent. pPage must have a single overflow entry
7826 ** which is also the right-most entry on the page.
7828 ** The pSpace buffer is used to store a temporary copy of the divider
7829 ** cell that will be inserted into pParent. Such a cell consists of a 4
7830 ** byte page number followed by a variable length integer. In other
7831 ** words, at most 13 bytes. Hence the pSpace buffer must be at
7832 ** least 13 bytes in size.
7834 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
7835 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
7836 MemPage *pNew; /* Newly allocated page */
7837 int rc; /* Return Code */
7838 Pgno pgnoNew; /* Page number of pNew */
7840 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7841 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7842 assert( pPage->nOverflow==1 );
7844 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */
7845 assert( pPage->nFree>=0 );
7846 assert( pParent->nFree>=0 );
7848 /* Allocate a new page. This page will become the right-sibling of
7849 ** pPage. Make the parent page writable, so that the new divider cell
7850 ** may be inserted. If both these operations are successful, proceed.
7852 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
7854 if( rc==SQLITE_OK ){
7856 u8 *pOut = &pSpace[4];
7857 u8 *pCell = pPage->apOvfl[0];
7858 u16 szCell = pPage->xCellSize(pPage, pCell);
7859 u8 *pStop;
7860 CellArray b;
7862 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
7863 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
7864 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
7865 b.nCell = 1;
7866 b.pRef = pPage;
7867 b.apCell = &pCell;
7868 b.szCell = &szCell;
7869 b.apEnd[0] = pPage->aDataEnd;
7870 b.ixNx[0] = 2;
7871 rc = rebuildPage(&b, 0, 1, pNew);
7872 if( NEVER(rc) ){
7873 releasePage(pNew);
7874 return rc;
7876 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
7878 /* If this is an auto-vacuum database, update the pointer map
7879 ** with entries for the new page, and any pointer from the
7880 ** cell on the page to an overflow page. If either of these
7881 ** operations fails, the return code is set, but the contents
7882 ** of the parent page are still manipulated by the code below.
7883 ** That is Ok, at this point the parent page is guaranteed to
7884 ** be marked as dirty. Returning an error code will cause a
7885 ** rollback, undoing any changes made to the parent page.
7887 if( ISAUTOVACUUM(pBt) ){
7888 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
7889 if( szCell>pNew->minLocal ){
7890 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
7894 /* Create a divider cell to insert into pParent. The divider cell
7895 ** consists of a 4-byte page number (the page number of pPage) and
7896 ** a variable length key value (which must be the same value as the
7897 ** largest key on pPage).
7899 ** To find the largest key value on pPage, first find the right-most
7900 ** cell on pPage. The first two fields of this cell are the
7901 ** record-length (a variable length integer at most 32-bits in size)
7902 ** and the key value (a variable length integer, may have any value).
7903 ** The first of the while(...) loops below skips over the record-length
7904 ** field. The second while(...) loop copies the key value from the
7905 ** cell on pPage into the pSpace buffer.
7907 pCell = findCell(pPage, pPage->nCell-1);
7908 pStop = &pCell[9];
7909 while( (*(pCell++)&0x80) && pCell<pStop );
7910 pStop = &pCell[9];
7911 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
7913 /* Insert the new divider cell into pParent. */
7914 if( rc==SQLITE_OK ){
7915 rc = insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
7916 0, pPage->pgno);
7919 /* Set the right-child pointer of pParent to point to the new page. */
7920 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
7922 /* Release the reference to the new page. */
7923 releasePage(pNew);
7926 return rc;
7928 #endif /* SQLITE_OMIT_QUICKBALANCE */
7930 #if 0
7932 ** This function does not contribute anything to the operation of SQLite.
7933 ** it is sometimes activated temporarily while debugging code responsible
7934 ** for setting pointer-map entries.
7936 static int ptrmapCheckPages(MemPage **apPage, int nPage){
7937 int i, j;
7938 for(i=0; i<nPage; i++){
7939 Pgno n;
7940 u8 e;
7941 MemPage *pPage = apPage[i];
7942 BtShared *pBt = pPage->pBt;
7943 assert( pPage->isInit );
7945 for(j=0; j<pPage->nCell; j++){
7946 CellInfo info;
7947 u8 *z;
7949 z = findCell(pPage, j);
7950 pPage->xParseCell(pPage, z, &info);
7951 if( info.nLocal<info.nPayload ){
7952 Pgno ovfl = get4byte(&z[info.nSize-4]);
7953 ptrmapGet(pBt, ovfl, &e, &n);
7954 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
7956 if( !pPage->leaf ){
7957 Pgno child = get4byte(z);
7958 ptrmapGet(pBt, child, &e, &n);
7959 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7962 if( !pPage->leaf ){
7963 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
7964 ptrmapGet(pBt, child, &e, &n);
7965 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7968 return 1;
7970 #endif
7973 ** This function is used to copy the contents of the b-tree node stored
7974 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
7975 ** the pointer-map entries for each child page are updated so that the
7976 ** parent page stored in the pointer map is page pTo. If pFrom contained
7977 ** any cells with overflow page pointers, then the corresponding pointer
7978 ** map entries are also updated so that the parent page is page pTo.
7980 ** If pFrom is currently carrying any overflow cells (entries in the
7981 ** MemPage.apOvfl[] array), they are not copied to pTo.
7983 ** Before returning, page pTo is reinitialized using btreeInitPage().
7985 ** The performance of this function is not critical. It is only used by
7986 ** the balance_shallower() and balance_deeper() procedures, neither of
7987 ** which are called often under normal circumstances.
7989 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
7990 if( (*pRC)==SQLITE_OK ){
7991 BtShared * const pBt = pFrom->pBt;
7992 u8 * const aFrom = pFrom->aData;
7993 u8 * const aTo = pTo->aData;
7994 int const iFromHdr = pFrom->hdrOffset;
7995 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
7996 int rc;
7997 int iData;
8000 assert( pFrom->isInit );
8001 assert( pFrom->nFree>=iToHdr );
8002 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
8004 /* Copy the b-tree node content from page pFrom to page pTo. */
8005 iData = get2byte(&aFrom[iFromHdr+5]);
8006 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
8007 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
8009 /* Reinitialize page pTo so that the contents of the MemPage structure
8010 ** match the new data. The initialization of pTo can actually fail under
8011 ** fairly obscure circumstances, even though it is a copy of initialized
8012 ** page pFrom.
8014 pTo->isInit = 0;
8015 rc = btreeInitPage(pTo);
8016 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo);
8017 if( rc!=SQLITE_OK ){
8018 *pRC = rc;
8019 return;
8022 /* If this is an auto-vacuum database, update the pointer-map entries
8023 ** for any b-tree or overflow pages that pTo now contains the pointers to.
8025 if( ISAUTOVACUUM(pBt) ){
8026 *pRC = setChildPtrmaps(pTo);
8032 ** This routine redistributes cells on the iParentIdx'th child of pParent
8033 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
8034 ** same amount of free space. Usually a single sibling on either side of the
8035 ** page are used in the balancing, though both siblings might come from one
8036 ** side if the page is the first or last child of its parent. If the page
8037 ** has fewer than 2 siblings (something which can only happen if the page
8038 ** is a root page or a child of a root page) then all available siblings
8039 ** participate in the balancing.
8041 ** The number of siblings of the page might be increased or decreased by
8042 ** one or two in an effort to keep pages nearly full but not over full.
8044 ** Note that when this routine is called, some of the cells on the page
8045 ** might not actually be stored in MemPage.aData[]. This can happen
8046 ** if the page is overfull. This routine ensures that all cells allocated
8047 ** to the page and its siblings fit into MemPage.aData[] before returning.
8049 ** In the course of balancing the page and its siblings, cells may be
8050 ** inserted into or removed from the parent page (pParent). Doing so
8051 ** may cause the parent page to become overfull or underfull. If this
8052 ** happens, it is the responsibility of the caller to invoke the correct
8053 ** balancing routine to fix this problem (see the balance() routine).
8055 ** If this routine fails for any reason, it might leave the database
8056 ** in a corrupted state. So if this routine fails, the database should
8057 ** be rolled back.
8059 ** The third argument to this function, aOvflSpace, is a pointer to a
8060 ** buffer big enough to hold one page. If while inserting cells into the parent
8061 ** page (pParent) the parent page becomes overfull, this buffer is
8062 ** used to store the parent's overflow cells. Because this function inserts
8063 ** a maximum of four divider cells into the parent page, and the maximum
8064 ** size of a cell stored within an internal node is always less than 1/4
8065 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
8066 ** enough for all overflow cells.
8068 ** If aOvflSpace is set to a null pointer, this function returns
8069 ** SQLITE_NOMEM.
8071 static int balance_nonroot(
8072 MemPage *pParent, /* Parent page of siblings being balanced */
8073 int iParentIdx, /* Index of "the page" in pParent */
8074 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
8075 int isRoot, /* True if pParent is a root-page */
8076 int bBulk /* True if this call is part of a bulk load */
8078 BtShared *pBt; /* The whole database */
8079 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
8080 int nNew = 0; /* Number of pages in apNew[] */
8081 int nOld; /* Number of pages in apOld[] */
8082 int i, j, k; /* Loop counters */
8083 int nxDiv; /* Next divider slot in pParent->aCell[] */
8084 int rc = SQLITE_OK; /* The return code */
8085 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
8086 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
8087 int usableSpace; /* Bytes in pPage beyond the header */
8088 int pageFlags; /* Value of pPage->aData[0] */
8089 int iSpace1 = 0; /* First unused byte of aSpace1[] */
8090 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
8091 int szScratch; /* Size of scratch memory requested */
8092 MemPage *apOld[NB]; /* pPage and up to two siblings */
8093 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
8094 u8 *pRight; /* Location in parent of right-sibling pointer */
8095 u8 *apDiv[NB-1]; /* Divider cells in pParent */
8096 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
8097 int cntOld[NB+2]; /* Old index in b.apCell[] */
8098 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
8099 u8 *aSpace1; /* Space for copies of dividers cells */
8100 Pgno pgno; /* Temp var to store a page number in */
8101 u8 abDone[NB+2]; /* True after i'th new page is populated */
8102 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
8103 CellArray b; /* Parsed information on cells being balanced */
8105 memset(abDone, 0, sizeof(abDone));
8106 memset(&b, 0, sizeof(b));
8107 pBt = pParent->pBt;
8108 assert( sqlite3_mutex_held(pBt->mutex) );
8109 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
8111 /* At this point pParent may have at most one overflow cell. And if
8112 ** this overflow cell is present, it must be the cell with
8113 ** index iParentIdx. This scenario comes about when this function
8114 ** is called (indirectly) from sqlite3BtreeDelete().
8116 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
8117 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
8119 if( !aOvflSpace ){
8120 return SQLITE_NOMEM_BKPT;
8122 assert( pParent->nFree>=0 );
8124 /* Find the sibling pages to balance. Also locate the cells in pParent
8125 ** that divide the siblings. An attempt is made to find NN siblings on
8126 ** either side of pPage. More siblings are taken from one side, however,
8127 ** if there are fewer than NN siblings on the other side. If pParent
8128 ** has NB or fewer children then all children of pParent are taken.
8130 ** This loop also drops the divider cells from the parent page. This
8131 ** way, the remainder of the function does not have to deal with any
8132 ** overflow cells in the parent page, since if any existed they will
8133 ** have already been removed.
8135 i = pParent->nOverflow + pParent->nCell;
8136 if( i<2 ){
8137 nxDiv = 0;
8138 }else{
8139 assert( bBulk==0 || bBulk==1 );
8140 if( iParentIdx==0 ){
8141 nxDiv = 0;
8142 }else if( iParentIdx==i ){
8143 nxDiv = i-2+bBulk;
8144 }else{
8145 nxDiv = iParentIdx-1;
8147 i = 2-bBulk;
8149 nOld = i+1;
8150 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
8151 pRight = &pParent->aData[pParent->hdrOffset+8];
8152 }else{
8153 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
8155 pgno = get4byte(pRight);
8156 while( 1 ){
8157 if( rc==SQLITE_OK ){
8158 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
8160 if( rc ){
8161 memset(apOld, 0, (i+1)*sizeof(MemPage*));
8162 goto balance_cleanup;
8164 if( apOld[i]->nFree<0 ){
8165 rc = btreeComputeFreeSpace(apOld[i]);
8166 if( rc ){
8167 memset(apOld, 0, (i)*sizeof(MemPage*));
8168 goto balance_cleanup;
8171 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl);
8172 if( (i--)==0 ) break;
8174 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
8175 apDiv[i] = pParent->apOvfl[0];
8176 pgno = get4byte(apDiv[i]);
8177 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
8178 pParent->nOverflow = 0;
8179 }else{
8180 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
8181 pgno = get4byte(apDiv[i]);
8182 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
8184 /* Drop the cell from the parent page. apDiv[i] still points to
8185 ** the cell within the parent, even though it has been dropped.
8186 ** This is safe because dropping a cell only overwrites the first
8187 ** four bytes of it, and this function does not need the first
8188 ** four bytes of the divider cell. So the pointer is safe to use
8189 ** later on.
8191 ** But not if we are in secure-delete mode. In secure-delete mode,
8192 ** the dropCell() routine will overwrite the entire cell with zeroes.
8193 ** In this case, temporarily copy the cell into the aOvflSpace[]
8194 ** buffer. It will be copied out again as soon as the aSpace[] buffer
8195 ** is allocated. */
8196 if( pBt->btsFlags & BTS_FAST_SECURE ){
8197 int iOff;
8199 /* If the following if() condition is not true, the db is corrupted.
8200 ** The call to dropCell() below will detect this. */
8201 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
8202 if( (iOff+szNew[i])<=(int)pBt->usableSize ){
8203 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
8204 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
8207 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
8211 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
8212 ** alignment */
8213 nMaxCells = (nMaxCells + 3)&~3;
8216 ** Allocate space for memory structures
8218 szScratch =
8219 nMaxCells*sizeof(u8*) /* b.apCell */
8220 + nMaxCells*sizeof(u16) /* b.szCell */
8221 + pBt->pageSize; /* aSpace1 */
8223 assert( szScratch<=7*(int)pBt->pageSize );
8224 b.apCell = sqlite3StackAllocRaw(0, szScratch );
8225 if( b.apCell==0 ){
8226 rc = SQLITE_NOMEM_BKPT;
8227 goto balance_cleanup;
8229 b.szCell = (u16*)&b.apCell[nMaxCells];
8230 aSpace1 = (u8*)&b.szCell[nMaxCells];
8231 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
8234 ** Load pointers to all cells on sibling pages and the divider cells
8235 ** into the local b.apCell[] array. Make copies of the divider cells
8236 ** into space obtained from aSpace1[]. The divider cells have already
8237 ** been removed from pParent.
8239 ** If the siblings are on leaf pages, then the child pointers of the
8240 ** divider cells are stripped from the cells before they are copied
8241 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
8242 ** child pointers. If siblings are not leaves, then all cell in
8243 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
8244 ** are alike.
8246 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
8247 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
8249 b.pRef = apOld[0];
8250 leafCorrection = b.pRef->leaf*4;
8251 leafData = b.pRef->intKeyLeaf;
8252 for(i=0; i<nOld; i++){
8253 MemPage *pOld = apOld[i];
8254 int limit = pOld->nCell;
8255 u8 *aData = pOld->aData;
8256 u16 maskPage = pOld->maskPage;
8257 u8 *piCell = aData + pOld->cellOffset;
8258 u8 *piEnd;
8259 VVA_ONLY( int nCellAtStart = b.nCell; )
8261 /* Verify that all sibling pages are of the same "type" (table-leaf,
8262 ** table-interior, index-leaf, or index-interior).
8264 if( pOld->aData[0]!=apOld[0]->aData[0] ){
8265 rc = SQLITE_CORRUPT_BKPT;
8266 goto balance_cleanup;
8269 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
8270 ** contains overflow cells, include them in the b.apCell[] array
8271 ** in the correct spot.
8273 ** Note that when there are multiple overflow cells, it is always the
8274 ** case that they are sequential and adjacent. This invariant arises
8275 ** because multiple overflows can only occurs when inserting divider
8276 ** cells into a parent on a prior balance, and divider cells are always
8277 ** adjacent and are inserted in order. There is an assert() tagged
8278 ** with "NOTE 1" in the overflow cell insertion loop to prove this
8279 ** invariant.
8281 ** This must be done in advance. Once the balance starts, the cell
8282 ** offset section of the btree page will be overwritten and we will no
8283 ** long be able to find the cells if a pointer to each cell is not saved
8284 ** first.
8286 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
8287 if( pOld->nOverflow>0 ){
8288 if( NEVER(limit<pOld->aiOvfl[0]) ){
8289 rc = SQLITE_CORRUPT_BKPT;
8290 goto balance_cleanup;
8292 limit = pOld->aiOvfl[0];
8293 for(j=0; j<limit; j++){
8294 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
8295 piCell += 2;
8296 b.nCell++;
8298 for(k=0; k<pOld->nOverflow; k++){
8299 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
8300 b.apCell[b.nCell] = pOld->apOvfl[k];
8301 b.nCell++;
8304 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
8305 while( piCell<piEnd ){
8306 assert( b.nCell<nMaxCells );
8307 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
8308 piCell += 2;
8309 b.nCell++;
8311 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
8313 cntOld[i] = b.nCell;
8314 if( i<nOld-1 && !leafData){
8315 u16 sz = (u16)szNew[i];
8316 u8 *pTemp;
8317 assert( b.nCell<nMaxCells );
8318 b.szCell[b.nCell] = sz;
8319 pTemp = &aSpace1[iSpace1];
8320 iSpace1 += sz;
8321 assert( sz<=pBt->maxLocal+23 );
8322 assert( iSpace1 <= (int)pBt->pageSize );
8323 memcpy(pTemp, apDiv[i], sz);
8324 b.apCell[b.nCell] = pTemp+leafCorrection;
8325 assert( leafCorrection==0 || leafCorrection==4 );
8326 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
8327 if( !pOld->leaf ){
8328 assert( leafCorrection==0 );
8329 assert( pOld->hdrOffset==0 || CORRUPT_DB );
8330 /* The right pointer of the child page pOld becomes the left
8331 ** pointer of the divider cell */
8332 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
8333 }else{
8334 assert( leafCorrection==4 );
8335 while( b.szCell[b.nCell]<4 ){
8336 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
8337 ** does exist, pad it with 0x00 bytes. */
8338 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
8339 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
8340 aSpace1[iSpace1++] = 0x00;
8341 b.szCell[b.nCell]++;
8344 b.nCell++;
8349 ** Figure out the number of pages needed to hold all b.nCell cells.
8350 ** Store this number in "k". Also compute szNew[] which is the total
8351 ** size of all cells on the i-th page and cntNew[] which is the index
8352 ** in b.apCell[] of the cell that divides page i from page i+1.
8353 ** cntNew[k] should equal b.nCell.
8355 ** Values computed by this block:
8357 ** k: The total number of sibling pages
8358 ** szNew[i]: Spaced used on the i-th sibling page.
8359 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
8360 ** the right of the i-th sibling page.
8361 ** usableSpace: Number of bytes of space available on each sibling.
8364 usableSpace = pBt->usableSize - 12 + leafCorrection;
8365 for(i=k=0; i<nOld; i++, k++){
8366 MemPage *p = apOld[i];
8367 b.apEnd[k] = p->aDataEnd;
8368 b.ixNx[k] = cntOld[i];
8369 if( k && b.ixNx[k]==b.ixNx[k-1] ){
8370 k--; /* Omit b.ixNx[] entry for child pages with no cells */
8372 if( !leafData ){
8373 k++;
8374 b.apEnd[k] = pParent->aDataEnd;
8375 b.ixNx[k] = cntOld[i]+1;
8377 assert( p->nFree>=0 );
8378 szNew[i] = usableSpace - p->nFree;
8379 for(j=0; j<p->nOverflow; j++){
8380 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
8382 cntNew[i] = cntOld[i];
8384 k = nOld;
8385 for(i=0; i<k; i++){
8386 int sz;
8387 while( szNew[i]>usableSpace ){
8388 if( i+1>=k ){
8389 k = i+2;
8390 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
8391 szNew[k-1] = 0;
8392 cntNew[k-1] = b.nCell;
8394 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
8395 szNew[i] -= sz;
8396 if( !leafData ){
8397 if( cntNew[i]<b.nCell ){
8398 sz = 2 + cachedCellSize(&b, cntNew[i]);
8399 }else{
8400 sz = 0;
8403 szNew[i+1] += sz;
8404 cntNew[i]--;
8406 while( cntNew[i]<b.nCell ){
8407 sz = 2 + cachedCellSize(&b, cntNew[i]);
8408 if( szNew[i]+sz>usableSpace ) break;
8409 szNew[i] += sz;
8410 cntNew[i]++;
8411 if( !leafData ){
8412 if( cntNew[i]<b.nCell ){
8413 sz = 2 + cachedCellSize(&b, cntNew[i]);
8414 }else{
8415 sz = 0;
8418 szNew[i+1] -= sz;
8420 if( cntNew[i]>=b.nCell ){
8421 k = i+1;
8422 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
8423 rc = SQLITE_CORRUPT_BKPT;
8424 goto balance_cleanup;
8429 ** The packing computed by the previous block is biased toward the siblings
8430 ** on the left side (siblings with smaller keys). The left siblings are
8431 ** always nearly full, while the right-most sibling might be nearly empty.
8432 ** The next block of code attempts to adjust the packing of siblings to
8433 ** get a better balance.
8435 ** This adjustment is more than an optimization. The packing above might
8436 ** be so out of balance as to be illegal. For example, the right-most
8437 ** sibling might be completely empty. This adjustment is not optional.
8439 for(i=k-1; i>0; i--){
8440 int szRight = szNew[i]; /* Size of sibling on the right */
8441 int szLeft = szNew[i-1]; /* Size of sibling on the left */
8442 int r; /* Index of right-most cell in left sibling */
8443 int d; /* Index of first cell to the left of right sibling */
8445 r = cntNew[i-1] - 1;
8446 d = r + 1 - leafData;
8447 (void)cachedCellSize(&b, d);
8449 int szR, szD;
8450 assert( d<nMaxCells );
8451 assert( r<nMaxCells );
8452 szR = cachedCellSize(&b, r);
8453 szD = b.szCell[d];
8454 if( szRight!=0
8455 && (bBulk || szRight+szD+2 > szLeft-(szR+(i==k-1?0:2)))){
8456 break;
8458 szRight += szD + 2;
8459 szLeft -= szR + 2;
8460 cntNew[i-1] = r;
8461 r--;
8462 d--;
8463 }while( r>=0 );
8464 szNew[i] = szRight;
8465 szNew[i-1] = szLeft;
8466 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
8467 rc = SQLITE_CORRUPT_BKPT;
8468 goto balance_cleanup;
8472 /* Sanity check: For a non-corrupt database file one of the following
8473 ** must be true:
8474 ** (1) We found one or more cells (cntNew[0])>0), or
8475 ** (2) pPage is a virtual root page. A virtual root page is when
8476 ** the real root page is page 1 and we are the only child of
8477 ** that page.
8479 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
8480 TRACE(("BALANCE: old: %u(nc=%u) %u(nc=%u) %u(nc=%u)\n",
8481 apOld[0]->pgno, apOld[0]->nCell,
8482 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
8483 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
8487 ** Allocate k new pages. Reuse old pages where possible.
8489 pageFlags = apOld[0]->aData[0];
8490 for(i=0; i<k; i++){
8491 MemPage *pNew;
8492 if( i<nOld ){
8493 pNew = apNew[i] = apOld[i];
8494 apOld[i] = 0;
8495 rc = sqlite3PagerWrite(pNew->pDbPage);
8496 nNew++;
8497 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv))
8498 && rc==SQLITE_OK
8500 rc = SQLITE_CORRUPT_BKPT;
8502 if( rc ) goto balance_cleanup;
8503 }else{
8504 assert( i>0 );
8505 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
8506 if( rc ) goto balance_cleanup;
8507 zeroPage(pNew, pageFlags);
8508 apNew[i] = pNew;
8509 nNew++;
8510 cntOld[i] = b.nCell;
8512 /* Set the pointer-map entry for the new sibling page. */
8513 if( ISAUTOVACUUM(pBt) ){
8514 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
8515 if( rc!=SQLITE_OK ){
8516 goto balance_cleanup;
8523 ** Reassign page numbers so that the new pages are in ascending order.
8524 ** This helps to keep entries in the disk file in order so that a scan
8525 ** of the table is closer to a linear scan through the file. That in turn
8526 ** helps the operating system to deliver pages from the disk more rapidly.
8528 ** An O(N*N) sort algorithm is used, but since N is never more than NB+2
8529 ** (5), that is not a performance concern.
8531 ** When NB==3, this one optimization makes the database about 25% faster
8532 ** for large insertions and deletions.
8534 for(i=0; i<nNew; i++){
8535 aPgno[i] = apNew[i]->pgno;
8536 assert( apNew[i]->pDbPage->flags & PGHDR_WRITEABLE );
8537 assert( apNew[i]->pDbPage->flags & PGHDR_DIRTY );
8539 for(i=0; i<nNew-1; i++){
8540 int iB = i;
8541 for(j=i+1; j<nNew; j++){
8542 if( apNew[j]->pgno < apNew[iB]->pgno ) iB = j;
8545 /* If apNew[i] has a page number that is bigger than any of the
8546 ** subsequence apNew[i] entries, then swap apNew[i] with the subsequent
8547 ** entry that has the smallest page number (which we know to be
8548 ** entry apNew[iB]).
8550 if( iB!=i ){
8551 Pgno pgnoA = apNew[i]->pgno;
8552 Pgno pgnoB = apNew[iB]->pgno;
8553 Pgno pgnoTemp = (PENDING_BYTE/pBt->pageSize)+1;
8554 u16 fgA = apNew[i]->pDbPage->flags;
8555 u16 fgB = apNew[iB]->pDbPage->flags;
8556 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoTemp, fgB);
8557 sqlite3PagerRekey(apNew[iB]->pDbPage, pgnoA, fgA);
8558 sqlite3PagerRekey(apNew[i]->pDbPage, pgnoB, fgB);
8559 apNew[i]->pgno = pgnoB;
8560 apNew[iB]->pgno = pgnoA;
8564 TRACE(("BALANCE: new: %u(%u nc=%u) %u(%u nc=%u) %u(%u nc=%u) "
8565 "%u(%u nc=%u) %u(%u nc=%u)\n",
8566 apNew[0]->pgno, szNew[0], cntNew[0],
8567 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
8568 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
8569 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
8570 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
8571 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
8572 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
8573 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
8574 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
8577 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
8578 assert( nNew>=1 && nNew<=ArraySize(apNew) );
8579 assert( apNew[nNew-1]!=0 );
8580 put4byte(pRight, apNew[nNew-1]->pgno);
8582 /* If the sibling pages are not leaves, ensure that the right-child pointer
8583 ** of the right-most new sibling page is set to the value that was
8584 ** originally in the same field of the right-most old sibling page. */
8585 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
8586 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
8587 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
8590 /* Make any required updates to pointer map entries associated with
8591 ** cells stored on sibling pages following the balance operation. Pointer
8592 ** map entries associated with divider cells are set by the insertCell()
8593 ** routine. The associated pointer map entries are:
8595 ** a) if the cell contains a reference to an overflow chain, the
8596 ** entry associated with the first page in the overflow chain, and
8598 ** b) if the sibling pages are not leaves, the child page associated
8599 ** with the cell.
8601 ** If the sibling pages are not leaves, then the pointer map entry
8602 ** associated with the right-child of each sibling may also need to be
8603 ** updated. This happens below, after the sibling pages have been
8604 ** populated, not here.
8606 if( ISAUTOVACUUM(pBt) ){
8607 MemPage *pOld;
8608 MemPage *pNew = pOld = apNew[0];
8609 int cntOldNext = pNew->nCell + pNew->nOverflow;
8610 int iNew = 0;
8611 int iOld = 0;
8613 for(i=0; i<b.nCell; i++){
8614 u8 *pCell = b.apCell[i];
8615 while( i==cntOldNext ){
8616 iOld++;
8617 assert( iOld<nNew || iOld<nOld );
8618 assert( iOld>=0 && iOld<NB );
8619 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
8620 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
8622 if( i==cntNew[iNew] ){
8623 pNew = apNew[++iNew];
8624 if( !leafData ) continue;
8627 /* Cell pCell is destined for new sibling page pNew. Originally, it
8628 ** was either part of sibling page iOld (possibly an overflow cell),
8629 ** or else the divider cell to the left of sibling page iOld. So,
8630 ** if sibling page iOld had the same page number as pNew, and if
8631 ** pCell really was a part of sibling page iOld (not a divider or
8632 ** overflow cell), we can skip updating the pointer map entries. */
8633 if( iOld>=nNew
8634 || pNew->pgno!=aPgno[iOld]
8635 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd)
8637 if( !leafCorrection ){
8638 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
8640 if( cachedCellSize(&b,i)>pNew->minLocal ){
8641 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
8643 if( rc ) goto balance_cleanup;
8648 /* Insert new divider cells into pParent. */
8649 for(i=0; i<nNew-1; i++){
8650 u8 *pCell;
8651 u8 *pTemp;
8652 int sz;
8653 u8 *pSrcEnd;
8654 MemPage *pNew = apNew[i];
8655 j = cntNew[i];
8657 assert( j<nMaxCells );
8658 assert( b.apCell[j]!=0 );
8659 pCell = b.apCell[j];
8660 sz = b.szCell[j] + leafCorrection;
8661 pTemp = &aOvflSpace[iOvflSpace];
8662 if( !pNew->leaf ){
8663 memcpy(&pNew->aData[8], pCell, 4);
8664 }else if( leafData ){
8665 /* If the tree is a leaf-data tree, and the siblings are leaves,
8666 ** then there is no divider cell in b.apCell[]. Instead, the divider
8667 ** cell consists of the integer key for the right-most cell of
8668 ** the sibling-page assembled above only.
8670 CellInfo info;
8671 j--;
8672 pNew->xParseCell(pNew, b.apCell[j], &info);
8673 pCell = pTemp;
8674 sz = 4 + putVarint(&pCell[4], info.nKey);
8675 pTemp = 0;
8676 }else{
8677 pCell -= 4;
8678 /* Obscure case for non-leaf-data trees: If the cell at pCell was
8679 ** previously stored on a leaf node, and its reported size was 4
8680 ** bytes, then it may actually be smaller than this
8681 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
8682 ** any cell). But it is important to pass the correct size to
8683 ** insertCell(), so reparse the cell now.
8685 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
8686 ** and WITHOUT ROWID tables with exactly one column which is the
8687 ** primary key.
8689 if( b.szCell[j]==4 ){
8690 assert(leafCorrection==4);
8691 sz = pParent->xCellSize(pParent, pCell);
8694 iOvflSpace += sz;
8695 assert( sz<=pBt->maxLocal+23 );
8696 assert( iOvflSpace <= (int)pBt->pageSize );
8697 for(k=0; ALWAYS(k<NB*2) && b.ixNx[k]<=j; k++){}
8698 pSrcEnd = b.apEnd[k];
8699 if( SQLITE_OVERFLOW(pSrcEnd, pCell, pCell+sz) ){
8700 rc = SQLITE_CORRUPT_BKPT;
8701 goto balance_cleanup;
8703 rc = insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno);
8704 if( rc!=SQLITE_OK ) goto balance_cleanup;
8705 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
8708 /* Now update the actual sibling pages. The order in which they are updated
8709 ** is important, as this code needs to avoid disrupting any page from which
8710 ** cells may still to be read. In practice, this means:
8712 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
8713 ** then it is not safe to update page apNew[iPg] until after
8714 ** the left-hand sibling apNew[iPg-1] has been updated.
8716 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
8717 ** then it is not safe to update page apNew[iPg] until after
8718 ** the right-hand sibling apNew[iPg+1] has been updated.
8720 ** If neither of the above apply, the page is safe to update.
8722 ** The iPg value in the following loop starts at nNew-1 goes down
8723 ** to 0, then back up to nNew-1 again, thus making two passes over
8724 ** the pages. On the initial downward pass, only condition (1) above
8725 ** needs to be tested because (2) will always be true from the previous
8726 ** step. On the upward pass, both conditions are always true, so the
8727 ** upwards pass simply processes pages that were missed on the downward
8728 ** pass.
8730 for(i=1-nNew; i<nNew; i++){
8731 int iPg = i<0 ? -i : i;
8732 assert( iPg>=0 && iPg<nNew );
8733 assert( iPg>=1 || i>=0 );
8734 assert( iPg<ArraySize(cntOld) );
8735 if( abDone[iPg] ) continue; /* Skip pages already processed */
8736 if( i>=0 /* On the upwards pass, or... */
8737 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
8739 int iNew;
8740 int iOld;
8741 int nNewCell;
8743 /* Verify condition (1): If cells are moving left, update iPg
8744 ** only after iPg-1 has already been updated. */
8745 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
8747 /* Verify condition (2): If cells are moving right, update iPg
8748 ** only after iPg+1 has already been updated. */
8749 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
8751 if( iPg==0 ){
8752 iNew = iOld = 0;
8753 nNewCell = cntNew[0];
8754 }else{
8755 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
8756 iNew = cntNew[iPg-1] + !leafData;
8757 nNewCell = cntNew[iPg] - iNew;
8760 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
8761 if( rc ) goto balance_cleanup;
8762 abDone[iPg]++;
8763 apNew[iPg]->nFree = usableSpace-szNew[iPg];
8764 assert( apNew[iPg]->nOverflow==0 );
8765 assert( apNew[iPg]->nCell==nNewCell );
8769 /* All pages have been processed exactly once */
8770 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
8772 assert( nOld>0 );
8773 assert( nNew>0 );
8775 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
8776 /* The root page of the b-tree now contains no cells. The only sibling
8777 ** page is the right-child of the parent. Copy the contents of the
8778 ** child page into the parent, decreasing the overall height of the
8779 ** b-tree structure by one. This is described as the "balance-shallower"
8780 ** sub-algorithm in some documentation.
8782 ** If this is an auto-vacuum database, the call to copyNodeContent()
8783 ** sets all pointer-map entries corresponding to database image pages
8784 ** for which the pointer is stored within the content being copied.
8786 ** It is critical that the child page be defragmented before being
8787 ** copied into the parent, because if the parent is page 1 then it will
8788 ** by smaller than the child due to the database header, and so all the
8789 ** free space needs to be up front.
8791 assert( nNew==1 || CORRUPT_DB );
8792 rc = defragmentPage(apNew[0], -1);
8793 testcase( rc!=SQLITE_OK );
8794 assert( apNew[0]->nFree ==
8795 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset
8796 - apNew[0]->nCell*2)
8797 || rc!=SQLITE_OK
8799 copyNodeContent(apNew[0], pParent, &rc);
8800 freePage(apNew[0], &rc);
8801 }else if( ISAUTOVACUUM(pBt) && !leafCorrection ){
8802 /* Fix the pointer map entries associated with the right-child of each
8803 ** sibling page. All other pointer map entries have already been taken
8804 ** care of. */
8805 for(i=0; i<nNew; i++){
8806 u32 key = get4byte(&apNew[i]->aData[8]);
8807 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
8811 assert( pParent->isInit );
8812 TRACE(("BALANCE: finished: old=%u new=%u cells=%u\n",
8813 nOld, nNew, b.nCell));
8815 /* Free any old pages that were not reused as new pages.
8817 for(i=nNew; i<nOld; i++){
8818 freePage(apOld[i], &rc);
8821 #if 0
8822 if( ISAUTOVACUUM(pBt) && rc==SQLITE_OK && apNew[0]->isInit ){
8823 /* The ptrmapCheckPages() contains assert() statements that verify that
8824 ** all pointer map pages are set correctly. This is helpful while
8825 ** debugging. This is usually disabled because a corrupt database may
8826 ** cause an assert() statement to fail. */
8827 ptrmapCheckPages(apNew, nNew);
8828 ptrmapCheckPages(&pParent, 1);
8830 #endif
8833 ** Cleanup before returning.
8835 balance_cleanup:
8836 sqlite3StackFree(0, b.apCell);
8837 for(i=0; i<nOld; i++){
8838 releasePage(apOld[i]);
8840 for(i=0; i<nNew; i++){
8841 releasePage(apNew[i]);
8844 return rc;
8849 ** This function is called when the root page of a b-tree structure is
8850 ** overfull (has one or more overflow pages).
8852 ** A new child page is allocated and the contents of the current root
8853 ** page, including overflow cells, are copied into the child. The root
8854 ** page is then overwritten to make it an empty page with the right-child
8855 ** pointer pointing to the new page.
8857 ** Before returning, all pointer-map entries corresponding to pages
8858 ** that the new child-page now contains pointers to are updated. The
8859 ** entry corresponding to the new right-child pointer of the root
8860 ** page is also updated.
8862 ** If successful, *ppChild is set to contain a reference to the child
8863 ** page and SQLITE_OK is returned. In this case the caller is required
8864 ** to call releasePage() on *ppChild exactly once. If an error occurs,
8865 ** an error code is returned and *ppChild is set to 0.
8867 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
8868 int rc; /* Return value from subprocedures */
8869 MemPage *pChild = 0; /* Pointer to a new child page */
8870 Pgno pgnoChild = 0; /* Page number of the new child page */
8871 BtShared *pBt = pRoot->pBt; /* The BTree */
8873 assert( pRoot->nOverflow>0 );
8874 assert( sqlite3_mutex_held(pBt->mutex) );
8876 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
8877 ** page that will become the new right-child of pPage. Copy the contents
8878 ** of the node stored on pRoot into the new child page.
8880 rc = sqlite3PagerWrite(pRoot->pDbPage);
8881 if( rc==SQLITE_OK ){
8882 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
8883 copyNodeContent(pRoot, pChild, &rc);
8884 if( ISAUTOVACUUM(pBt) ){
8885 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
8888 if( rc ){
8889 *ppChild = 0;
8890 releasePage(pChild);
8891 return rc;
8893 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
8894 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8895 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB );
8897 TRACE(("BALANCE: copy root %u into %u\n", pRoot->pgno, pChild->pgno));
8899 /* Copy the overflow cells from pRoot to pChild */
8900 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
8901 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
8902 memcpy(pChild->apOvfl, pRoot->apOvfl,
8903 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
8904 pChild->nOverflow = pRoot->nOverflow;
8906 /* Zero the contents of pRoot. Then install pChild as the right-child. */
8907 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
8908 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
8910 *ppChild = pChild;
8911 return SQLITE_OK;
8915 ** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid
8916 ** on the same B-tree as pCur.
8918 ** This can occur if a database is corrupt with two or more SQL tables
8919 ** pointing to the same b-tree. If an insert occurs on one SQL table
8920 ** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL
8921 ** table linked to the same b-tree. If the secondary insert causes a
8922 ** rebalance, that can change content out from under the cursor on the
8923 ** first SQL table, violating invariants on the first insert.
8925 static int anotherValidCursor(BtCursor *pCur){
8926 BtCursor *pOther;
8927 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){
8928 if( pOther!=pCur
8929 && pOther->eState==CURSOR_VALID
8930 && pOther->pPage==pCur->pPage
8932 return SQLITE_CORRUPT_BKPT;
8935 return SQLITE_OK;
8939 ** The page that pCur currently points to has just been modified in
8940 ** some way. This function figures out if this modification means the
8941 ** tree needs to be balanced, and if so calls the appropriate balancing
8942 ** routine. Balancing routines are:
8944 ** balance_quick()
8945 ** balance_deeper()
8946 ** balance_nonroot()
8948 static int balance(BtCursor *pCur){
8949 int rc = SQLITE_OK;
8950 u8 aBalanceQuickSpace[13];
8951 u8 *pFree = 0;
8953 VVA_ONLY( int balance_quick_called = 0 );
8954 VVA_ONLY( int balance_deeper_called = 0 );
8956 do {
8957 int iPage;
8958 MemPage *pPage = pCur->pPage;
8960 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
8961 if( pPage->nOverflow==0 && pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
8962 /* No rebalance required as long as:
8963 ** (1) There are no overflow cells
8964 ** (2) The amount of free space on the page is less than 2/3rds of
8965 ** the total usable space on the page. */
8966 break;
8967 }else if( (iPage = pCur->iPage)==0 ){
8968 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){
8969 /* The root page of the b-tree is overfull. In this case call the
8970 ** balance_deeper() function to create a new child for the root-page
8971 ** and copy the current contents of the root-page to it. The
8972 ** next iteration of the do-loop will balance the child page.
8974 assert( balance_deeper_called==0 );
8975 VVA_ONLY( balance_deeper_called++ );
8976 rc = balance_deeper(pPage, &pCur->apPage[1]);
8977 if( rc==SQLITE_OK ){
8978 pCur->iPage = 1;
8979 pCur->ix = 0;
8980 pCur->aiIdx[0] = 0;
8981 pCur->apPage[0] = pPage;
8982 pCur->pPage = pCur->apPage[1];
8983 assert( pCur->pPage->nOverflow );
8985 }else{
8986 break;
8988 }else if( sqlite3PagerPageRefcount(pPage->pDbPage)>1 ){
8989 /* The page being written is not a root page, and there is currently
8990 ** more than one reference to it. This only happens if the page is one
8991 ** of its own ancestor pages. Corruption. */
8992 rc = SQLITE_CORRUPT_BKPT;
8993 }else{
8994 MemPage * const pParent = pCur->apPage[iPage-1];
8995 int const iIdx = pCur->aiIdx[iPage-1];
8997 rc = sqlite3PagerWrite(pParent->pDbPage);
8998 if( rc==SQLITE_OK && pParent->nFree<0 ){
8999 rc = btreeComputeFreeSpace(pParent);
9001 if( rc==SQLITE_OK ){
9002 #ifndef SQLITE_OMIT_QUICKBALANCE
9003 if( pPage->intKeyLeaf
9004 && pPage->nOverflow==1
9005 && pPage->aiOvfl[0]==pPage->nCell
9006 && pParent->pgno!=1
9007 && pParent->nCell==iIdx
9009 /* Call balance_quick() to create a new sibling of pPage on which
9010 ** to store the overflow cell. balance_quick() inserts a new cell
9011 ** into pParent, which may cause pParent overflow. If this
9012 ** happens, the next iteration of the do-loop will balance pParent
9013 ** use either balance_nonroot() or balance_deeper(). Until this
9014 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
9015 ** buffer.
9017 ** The purpose of the following assert() is to check that only a
9018 ** single call to balance_quick() is made for each call to this
9019 ** function. If this were not verified, a subtle bug involving reuse
9020 ** of the aBalanceQuickSpace[] might sneak in.
9022 assert( balance_quick_called==0 );
9023 VVA_ONLY( balance_quick_called++ );
9024 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
9025 }else
9026 #endif
9028 /* In this case, call balance_nonroot() to redistribute cells
9029 ** between pPage and up to 2 of its sibling pages. This involves
9030 ** modifying the contents of pParent, which may cause pParent to
9031 ** become overfull or underfull. The next iteration of the do-loop
9032 ** will balance the parent page to correct this.
9034 ** If the parent page becomes overfull, the overflow cell or cells
9035 ** are stored in the pSpace buffer allocated immediately below.
9036 ** A subsequent iteration of the do-loop will deal with this by
9037 ** calling balance_nonroot() (balance_deeper() may be called first,
9038 ** but it doesn't deal with overflow cells - just moves them to a
9039 ** different page). Once this subsequent call to balance_nonroot()
9040 ** has completed, it is safe to release the pSpace buffer used by
9041 ** the previous call, as the overflow cell data will have been
9042 ** copied either into the body of a database page or into the new
9043 ** pSpace buffer passed to the latter call to balance_nonroot().
9045 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
9046 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
9047 pCur->hints&BTREE_BULKLOAD);
9048 if( pFree ){
9049 /* If pFree is not NULL, it points to the pSpace buffer used
9050 ** by a previous call to balance_nonroot(). Its contents are
9051 ** now stored either on real database pages or within the
9052 ** new pSpace buffer, so it may be safely freed here. */
9053 sqlite3PageFree(pFree);
9056 /* The pSpace buffer will be freed after the next call to
9057 ** balance_nonroot(), or just before this function returns, whichever
9058 ** comes first. */
9059 pFree = pSpace;
9063 pPage->nOverflow = 0;
9065 /* The next iteration of the do-loop balances the parent page. */
9066 releasePage(pPage);
9067 pCur->iPage--;
9068 assert( pCur->iPage>=0 );
9069 pCur->pPage = pCur->apPage[pCur->iPage];
9071 }while( rc==SQLITE_OK );
9073 if( pFree ){
9074 sqlite3PageFree(pFree);
9076 return rc;
9079 /* Overwrite content from pX into pDest. Only do the write if the
9080 ** content is different from what is already there.
9082 static int btreeOverwriteContent(
9083 MemPage *pPage, /* MemPage on which writing will occur */
9084 u8 *pDest, /* Pointer to the place to start writing */
9085 const BtreePayload *pX, /* Source of data to write */
9086 int iOffset, /* Offset of first byte to write */
9087 int iAmt /* Number of bytes to be written */
9089 int nData = pX->nData - iOffset;
9090 if( nData<=0 ){
9091 /* Overwriting with zeros */
9092 int i;
9093 for(i=0; i<iAmt && pDest[i]==0; i++){}
9094 if( i<iAmt ){
9095 int rc = sqlite3PagerWrite(pPage->pDbPage);
9096 if( rc ) return rc;
9097 memset(pDest + i, 0, iAmt - i);
9099 }else{
9100 if( nData<iAmt ){
9101 /* Mixed read data and zeros at the end. Make a recursive call
9102 ** to write the zeros then fall through to write the real data */
9103 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
9104 iAmt-nData);
9105 if( rc ) return rc;
9106 iAmt = nData;
9108 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
9109 int rc = sqlite3PagerWrite(pPage->pDbPage);
9110 if( rc ) return rc;
9111 /* In a corrupt database, it is possible for the source and destination
9112 ** buffers to overlap. This is harmless since the database is already
9113 ** corrupt but it does cause valgrind and ASAN warnings. So use
9114 ** memmove(). */
9115 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
9118 return SQLITE_OK;
9122 ** Overwrite the cell that cursor pCur is pointing to with fresh content
9123 ** contained in pX. In this variant, pCur is pointing to an overflow
9124 ** cell.
9126 static SQLITE_NOINLINE int btreeOverwriteOverflowCell(
9127 BtCursor *pCur, /* Cursor pointing to cell to overwrite */
9128 const BtreePayload *pX /* Content to write into the cell */
9130 int iOffset; /* Next byte of pX->pData to write */
9131 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
9132 int rc; /* Return code */
9133 MemPage *pPage = pCur->pPage; /* Page being written */
9134 BtShared *pBt; /* Btree */
9135 Pgno ovflPgno; /* Next overflow page to write */
9136 u32 ovflPageSize; /* Size to write on overflow page */
9138 assert( pCur->info.nLocal<nTotal ); /* pCur is an overflow cell */
9140 /* Overwrite the local portion first */
9141 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
9142 0, pCur->info.nLocal);
9143 if( rc ) return rc;
9145 /* Now overwrite the overflow pages */
9146 iOffset = pCur->info.nLocal;
9147 assert( nTotal>=0 );
9148 assert( iOffset>=0 );
9149 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
9150 pBt = pPage->pBt;
9151 ovflPageSize = pBt->usableSize - 4;
9153 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
9154 if( rc ) return rc;
9155 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){
9156 rc = SQLITE_CORRUPT_BKPT;
9157 }else{
9158 if( iOffset+ovflPageSize<(u32)nTotal ){
9159 ovflPgno = get4byte(pPage->aData);
9160 }else{
9161 ovflPageSize = nTotal - iOffset;
9163 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
9164 iOffset, ovflPageSize);
9166 sqlite3PagerUnref(pPage->pDbPage);
9167 if( rc ) return rc;
9168 iOffset += ovflPageSize;
9169 }while( iOffset<nTotal );
9170 return SQLITE_OK;
9174 ** Overwrite the cell that cursor pCur is pointing to with fresh content
9175 ** contained in pX.
9177 static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
9178 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
9179 MemPage *pPage = pCur->pPage; /* Page being written */
9181 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
9182 || pCur->info.pPayload < pPage->aData + pPage->cellOffset
9184 return SQLITE_CORRUPT_BKPT;
9186 if( pCur->info.nLocal==nTotal ){
9187 /* The entire cell is local */
9188 return btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
9189 0, pCur->info.nLocal);
9190 }else{
9191 /* The cell contains overflow content */
9192 return btreeOverwriteOverflowCell(pCur, pX);
9198 ** Insert a new record into the BTree. The content of the new record
9199 ** is described by the pX object. The pCur cursor is used only to
9200 ** define what table the record should be inserted into, and is left
9201 ** pointing at a random location.
9203 ** For a table btree (used for rowid tables), only the pX.nKey value of
9204 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
9205 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
9206 ** hold the content of the row.
9208 ** For an index btree (used for indexes and WITHOUT ROWID tables), the
9209 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
9210 ** pX.pData,nData,nZero fields must be zero.
9212 ** If the seekResult parameter is non-zero, then a successful call to
9213 ** sqlite3BtreeIndexMoveto() to seek cursor pCur to (pKey,nKey) has already
9214 ** been performed. In other words, if seekResult!=0 then the cursor
9215 ** is currently pointing to a cell that will be adjacent to the cell
9216 ** to be inserted. If seekResult<0 then pCur points to a cell that is
9217 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
9218 ** that is larger than (pKey,nKey).
9220 ** If seekResult==0, that means pCur is pointing at some unknown location.
9221 ** In that case, this routine must seek the cursor to the correct insertion
9222 ** point for (pKey,nKey) before doing the insertion. For index btrees,
9223 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
9224 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
9225 ** to decode the key.
9227 int sqlite3BtreeInsert(
9228 BtCursor *pCur, /* Insert data into the table of this cursor */
9229 const BtreePayload *pX, /* Content of the row to be inserted */
9230 int flags, /* True if this is likely an append */
9231 int seekResult /* Result of prior IndexMoveto() call */
9233 int rc;
9234 int loc = seekResult; /* -1: before desired location +1: after */
9235 int szNew = 0;
9236 int idx;
9237 MemPage *pPage;
9238 Btree *p = pCur->pBtree;
9239 unsigned char *oldCell;
9240 unsigned char *newCell = 0;
9242 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags );
9243 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 );
9245 /* Save the positions of any other cursors open on this table.
9247 ** In some cases, the call to btreeMoveto() below is a no-op. For
9248 ** example, when inserting data into a table with auto-generated integer
9249 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
9250 ** integer key to use. It then calls this function to actually insert the
9251 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
9252 ** that the cursor is already where it needs to be and returns without
9253 ** doing any work. To avoid thwarting these optimizations, it is important
9254 ** not to clear the cursor here.
9256 if( pCur->curFlags & BTCF_Multiple ){
9257 rc = saveAllCursors(p->pBt, pCur->pgnoRoot, pCur);
9258 if( rc ) return rc;
9259 if( loc && pCur->iPage<0 ){
9260 /* This can only happen if the schema is corrupt such that there is more
9261 ** than one table or index with the same root page as used by the cursor.
9262 ** Which can only happen if the SQLITE_NoSchemaError flag was set when
9263 ** the schema was loaded. This cannot be asserted though, as a user might
9264 ** set the flag, load the schema, and then unset the flag. */
9265 return SQLITE_CORRUPT_BKPT;
9269 /* Ensure that the cursor is not in the CURSOR_FAULT state and that it
9270 ** points to a valid cell.
9272 if( pCur->eState>=CURSOR_REQUIRESEEK ){
9273 testcase( pCur->eState==CURSOR_REQUIRESEEK );
9274 testcase( pCur->eState==CURSOR_FAULT );
9275 rc = moveToRoot(pCur);
9276 if( rc && rc!=SQLITE_EMPTY ) return rc;
9279 assert( cursorOwnsBtShared(pCur) );
9280 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
9281 && p->pBt->inTransaction==TRANS_WRITE
9282 && (p->pBt->btsFlags & BTS_READ_ONLY)==0 );
9283 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
9285 /* Assert that the caller has been consistent. If this cursor was opened
9286 ** expecting an index b-tree, then the caller should be inserting blob
9287 ** keys with no associated data. If the cursor was opened expecting an
9288 ** intkey table, the caller should be inserting integer keys with a
9289 ** blob of associated data. */
9290 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) );
9292 if( pCur->pKeyInfo==0 ){
9293 assert( pX->pKey==0 );
9294 /* If this is an insert into a table b-tree, invalidate any incrblob
9295 ** cursors open on the row being replaced */
9296 if( p->hasIncrblobCur ){
9297 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
9300 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
9301 ** to a row with the same key as the new entry being inserted.
9303 #ifdef SQLITE_DEBUG
9304 if( flags & BTREE_SAVEPOSITION ){
9305 assert( pCur->curFlags & BTCF_ValidNKey );
9306 assert( pX->nKey==pCur->info.nKey );
9307 assert( loc==0 );
9309 #endif
9311 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
9312 ** that the cursor is not pointing to a row to be overwritten.
9313 ** So do a complete check.
9315 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
9316 /* The cursor is pointing to the entry that is to be
9317 ** overwritten */
9318 assert( pX->nData>=0 && pX->nZero>=0 );
9319 if( pCur->info.nSize!=0
9320 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
9322 /* New entry is the same size as the old. Do an overwrite */
9323 return btreeOverwriteCell(pCur, pX);
9325 assert( loc==0 );
9326 }else if( loc==0 ){
9327 /* The cursor is *not* pointing to the cell to be overwritten, nor
9328 ** to an adjacent cell. Move the cursor so that it is pointing either
9329 ** to the cell to be overwritten or an adjacent cell.
9331 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey,
9332 (flags & BTREE_APPEND)!=0, &loc);
9333 if( rc ) return rc;
9335 }else{
9336 /* This is an index or a WITHOUT ROWID table */
9338 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
9339 ** to a row with the same key as the new entry being inserted.
9341 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
9343 /* If the cursor is not already pointing either to the cell to be
9344 ** overwritten, or if a new cell is being inserted, if the cursor is
9345 ** not pointing to an immediately adjacent cell, then move the cursor
9346 ** so that it does.
9348 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
9349 if( pX->nMem ){
9350 UnpackedRecord r;
9351 r.pKeyInfo = pCur->pKeyInfo;
9352 r.aMem = pX->aMem;
9353 r.nField = pX->nMem;
9354 r.default_rc = 0;
9355 r.eqSeen = 0;
9356 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc);
9357 }else{
9358 rc = btreeMoveto(pCur, pX->pKey, pX->nKey,
9359 (flags & BTREE_APPEND)!=0, &loc);
9361 if( rc ) return rc;
9364 /* If the cursor is currently pointing to an entry to be overwritten
9365 ** and the new content is the same as as the old, then use the
9366 ** overwrite optimization.
9368 if( loc==0 ){
9369 getCellInfo(pCur);
9370 if( pCur->info.nKey==pX->nKey ){
9371 BtreePayload x2;
9372 x2.pData = pX->pKey;
9373 x2.nData = pX->nKey;
9374 x2.nZero = 0;
9375 return btreeOverwriteCell(pCur, &x2);
9379 assert( pCur->eState==CURSOR_VALID
9380 || (pCur->eState==CURSOR_INVALID && loc) || CORRUPT_DB );
9382 pPage = pCur->pPage;
9383 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) );
9384 assert( pPage->leaf || !pPage->intKey );
9385 if( pPage->nFree<0 ){
9386 if( NEVER(pCur->eState>CURSOR_INVALID) ){
9387 /* ^^^^^--- due to the moveToRoot() call above */
9388 rc = SQLITE_CORRUPT_BKPT;
9389 }else{
9390 rc = btreeComputeFreeSpace(pPage);
9392 if( rc ) return rc;
9395 TRACE(("INSERT: table=%u nkey=%lld ndata=%u page=%u %s\n",
9396 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
9397 loc==0 ? "overwrite" : "new entry"));
9398 assert( pPage->isInit || CORRUPT_DB );
9399 newCell = p->pBt->pTmpSpace;
9400 assert( newCell!=0 );
9401 assert( BTREE_PREFORMAT==OPFLAG_PREFORMAT );
9402 if( flags & BTREE_PREFORMAT ){
9403 rc = SQLITE_OK;
9404 szNew = p->pBt->nPreformatSize;
9405 if( szNew<4 ) szNew = 4;
9406 if( ISAUTOVACUUM(p->pBt) && szNew>pPage->maxLocal ){
9407 CellInfo info;
9408 pPage->xParseCell(pPage, newCell, &info);
9409 if( info.nPayload!=info.nLocal ){
9410 Pgno ovfl = get4byte(&newCell[szNew-4]);
9411 ptrmapPut(p->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc);
9412 if( NEVER(rc) ) goto end_insert;
9415 }else{
9416 rc = fillInCell(pPage, newCell, pX, &szNew);
9417 if( rc ) goto end_insert;
9419 assert( szNew==pPage->xCellSize(pPage, newCell) );
9420 assert( szNew <= MX_CELL_SIZE(p->pBt) );
9421 idx = pCur->ix;
9422 pCur->info.nSize = 0;
9423 if( loc==0 ){
9424 CellInfo info;
9425 assert( idx>=0 );
9426 if( idx>=pPage->nCell ){
9427 return SQLITE_CORRUPT_BKPT;
9429 rc = sqlite3PagerWrite(pPage->pDbPage);
9430 if( rc ){
9431 goto end_insert;
9433 oldCell = findCell(pPage, idx);
9434 if( !pPage->leaf ){
9435 memcpy(newCell, oldCell, 4);
9437 BTREE_CLEAR_CELL(rc, pPage, oldCell, info);
9438 testcase( pCur->curFlags & BTCF_ValidOvfl );
9439 invalidateOverflowCache(pCur);
9440 if( info.nSize==szNew && info.nLocal==info.nPayload
9441 && (!ISAUTOVACUUM(p->pBt) || szNew<pPage->minLocal)
9443 /* Overwrite the old cell with the new if they are the same size.
9444 ** We could also try to do this if the old cell is smaller, then add
9445 ** the leftover space to the free list. But experiments show that
9446 ** doing that is no faster then skipping this optimization and just
9447 ** calling dropCell() and insertCell().
9449 ** This optimization cannot be used on an autovacuum database if the
9450 ** new entry uses overflow pages, as the insertCell() call below is
9451 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
9452 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
9453 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
9454 return SQLITE_CORRUPT_BKPT;
9456 if( oldCell+szNew > pPage->aDataEnd ){
9457 return SQLITE_CORRUPT_BKPT;
9459 memcpy(oldCell, newCell, szNew);
9460 return SQLITE_OK;
9462 dropCell(pPage, idx, info.nSize, &rc);
9463 if( rc ) goto end_insert;
9464 }else if( loc<0 && pPage->nCell>0 ){
9465 assert( pPage->leaf );
9466 idx = ++pCur->ix;
9467 pCur->curFlags &= ~BTCF_ValidNKey;
9468 }else{
9469 assert( pPage->leaf );
9471 rc = insertCellFast(pPage, idx, newCell, szNew);
9472 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
9473 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
9475 /* If no error has occurred and pPage has an overflow cell, call balance()
9476 ** to redistribute the cells within the tree. Since balance() may move
9477 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
9478 ** variables.
9480 ** Previous versions of SQLite called moveToRoot() to move the cursor
9481 ** back to the root page as balance() used to invalidate the contents
9482 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
9483 ** set the cursor state to "invalid". This makes common insert operations
9484 ** slightly faster.
9486 ** There is a subtle but important optimization here too. When inserting
9487 ** multiple records into an intkey b-tree using a single cursor (as can
9488 ** happen while processing an "INSERT INTO ... SELECT" statement), it
9489 ** is advantageous to leave the cursor pointing to the last entry in
9490 ** the b-tree if possible. If the cursor is left pointing to the last
9491 ** entry in the table, and the next row inserted has an integer key
9492 ** larger than the largest existing key, it is possible to insert the
9493 ** row without seeking the cursor. This can be a big performance boost.
9495 if( pPage->nOverflow ){
9496 assert( rc==SQLITE_OK );
9497 pCur->curFlags &= ~(BTCF_ValidNKey);
9498 rc = balance(pCur);
9500 /* Must make sure nOverflow is reset to zero even if the balance()
9501 ** fails. Internal data structure corruption will result otherwise.
9502 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
9503 ** from trying to save the current position of the cursor. */
9504 pCur->pPage->nOverflow = 0;
9505 pCur->eState = CURSOR_INVALID;
9506 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
9507 btreeReleaseAllCursorPages(pCur);
9508 if( pCur->pKeyInfo ){
9509 assert( pCur->pKey==0 );
9510 pCur->pKey = sqlite3Malloc( pX->nKey );
9511 if( pCur->pKey==0 ){
9512 rc = SQLITE_NOMEM;
9513 }else{
9514 memcpy(pCur->pKey, pX->pKey, pX->nKey);
9517 pCur->eState = CURSOR_REQUIRESEEK;
9518 pCur->nKey = pX->nKey;
9521 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
9523 end_insert:
9524 return rc;
9528 ** This function is used as part of copying the current row from cursor
9529 ** pSrc into cursor pDest. If the cursors are open on intkey tables, then
9530 ** parameter iKey is used as the rowid value when the record is copied
9531 ** into pDest. Otherwise, the record is copied verbatim.
9533 ** This function does not actually write the new value to cursor pDest.
9534 ** Instead, it creates and populates any required overflow pages and
9535 ** writes the data for the new cell into the BtShared.pTmpSpace buffer
9536 ** for the destination database. The size of the cell, in bytes, is left
9537 ** in BtShared.nPreformatSize. The caller completes the insertion by
9538 ** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified.
9540 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9542 int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){
9543 BtShared *pBt = pDest->pBt;
9544 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */
9545 const u8 *aIn; /* Pointer to next input buffer */
9546 u32 nIn; /* Size of input buffer aIn[] */
9547 u32 nRem; /* Bytes of data still to copy */
9549 getCellInfo(pSrc);
9550 if( pSrc->info.nPayload<0x80 ){
9551 *(aOut++) = pSrc->info.nPayload;
9552 }else{
9553 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
9555 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
9556 nIn = pSrc->info.nLocal;
9557 aIn = pSrc->info.pPayload;
9558 if( aIn+nIn>pSrc->pPage->aDataEnd ){
9559 return SQLITE_CORRUPT_BKPT;
9561 nRem = pSrc->info.nPayload;
9562 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
9563 memcpy(aOut, aIn, nIn);
9564 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
9565 return SQLITE_OK;
9566 }else{
9567 int rc = SQLITE_OK;
9568 Pager *pSrcPager = pSrc->pBt->pPager;
9569 u8 *pPgnoOut = 0;
9570 Pgno ovflIn = 0;
9571 DbPage *pPageIn = 0;
9572 MemPage *pPageOut = 0;
9573 u32 nOut; /* Size of output buffer aOut[] */
9575 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
9576 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
9577 if( nOut<pSrc->info.nPayload ){
9578 pPgnoOut = &aOut[nOut];
9579 pBt->nPreformatSize += 4;
9582 if( nRem>nIn ){
9583 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){
9584 return SQLITE_CORRUPT_BKPT;
9586 ovflIn = get4byte(&pSrc->info.pPayload[nIn]);
9589 do {
9590 nRem -= nOut;
9592 assert( nOut>0 );
9593 if( nIn>0 ){
9594 int nCopy = MIN(nOut, nIn);
9595 memcpy(aOut, aIn, nCopy);
9596 nOut -= nCopy;
9597 nIn -= nCopy;
9598 aOut += nCopy;
9599 aIn += nCopy;
9601 if( nOut>0 ){
9602 sqlite3PagerUnref(pPageIn);
9603 pPageIn = 0;
9604 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY);
9605 if( rc==SQLITE_OK ){
9606 aIn = (const u8*)sqlite3PagerGetData(pPageIn);
9607 ovflIn = get4byte(aIn);
9608 aIn += 4;
9609 nIn = pSrc->pBt->usableSize - 4;
9612 }while( rc==SQLITE_OK && nOut>0 );
9614 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){
9615 Pgno pgnoNew;
9616 MemPage *pNew = 0;
9617 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
9618 put4byte(pPgnoOut, pgnoNew);
9619 if( ISAUTOVACUUM(pBt) && pPageOut ){
9620 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc);
9622 releasePage(pPageOut);
9623 pPageOut = pNew;
9624 if( pPageOut ){
9625 pPgnoOut = pPageOut->aData;
9626 put4byte(pPgnoOut, 0);
9627 aOut = &pPgnoOut[4];
9628 nOut = MIN(pBt->usableSize - 4, nRem);
9631 }while( nRem>0 && rc==SQLITE_OK );
9633 releasePage(pPageOut);
9634 sqlite3PagerUnref(pPageIn);
9635 return rc;
9640 ** Delete the entry that the cursor is pointing to.
9642 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
9643 ** the cursor is left pointing at an arbitrary location after the delete.
9644 ** But if that bit is set, then the cursor is left in a state such that
9645 ** the next call to BtreeNext() or BtreePrev() moves it to the same row
9646 ** as it would have been on if the call to BtreeDelete() had been omitted.
9648 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
9649 ** associated with a single table entry and its indexes. Only one of those
9650 ** deletes is considered the "primary" delete. The primary delete occurs
9651 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
9652 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
9653 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
9654 ** but which might be used by alternative storage engines.
9656 int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
9657 Btree *p = pCur->pBtree;
9658 BtShared *pBt = p->pBt;
9659 int rc; /* Return code */
9660 MemPage *pPage; /* Page to delete cell from */
9661 unsigned char *pCell; /* Pointer to cell to delete */
9662 int iCellIdx; /* Index of cell to delete */
9663 int iCellDepth; /* Depth of node containing pCell */
9664 CellInfo info; /* Size of the cell being deleted */
9665 u8 bPreserve; /* Keep cursor valid. 2 for CURSOR_SKIPNEXT */
9667 assert( cursorOwnsBtShared(pCur) );
9668 assert( pBt->inTransaction==TRANS_WRITE );
9669 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
9670 assert( pCur->curFlags & BTCF_WriteFlag );
9671 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
9672 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
9673 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
9674 if( pCur->eState!=CURSOR_VALID ){
9675 if( pCur->eState>=CURSOR_REQUIRESEEK ){
9676 rc = btreeRestoreCursorPosition(pCur);
9677 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID );
9678 if( rc || pCur->eState!=CURSOR_VALID ) return rc;
9679 }else{
9680 return SQLITE_CORRUPT_BKPT;
9683 assert( pCur->eState==CURSOR_VALID );
9685 iCellDepth = pCur->iPage;
9686 iCellIdx = pCur->ix;
9687 pPage = pCur->pPage;
9688 if( pPage->nCell<=iCellIdx ){
9689 return SQLITE_CORRUPT_BKPT;
9691 pCell = findCell(pPage, iCellIdx);
9692 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ){
9693 return SQLITE_CORRUPT_BKPT;
9695 if( pCell<&pPage->aCellIdx[pPage->nCell] ){
9696 return SQLITE_CORRUPT_BKPT;
9699 /* If the BTREE_SAVEPOSITION bit is on, then the cursor position must
9700 ** be preserved following this delete operation. If the current delete
9701 ** will cause a b-tree rebalance, then this is done by saving the cursor
9702 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
9703 ** returning.
9705 ** If the current delete will not cause a rebalance, then the cursor
9706 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
9707 ** before or after the deleted entry.
9709 ** The bPreserve value records which path is required:
9711 ** bPreserve==0 Not necessary to save the cursor position
9712 ** bPreserve==1 Use CURSOR_REQUIRESEEK to save the cursor position
9713 ** bPreserve==2 Cursor won't move. Set CURSOR_SKIPNEXT.
9715 bPreserve = (flags & BTREE_SAVEPOSITION)!=0;
9716 if( bPreserve ){
9717 if( !pPage->leaf
9718 || (pPage->nFree+pPage->xCellSize(pPage,pCell)+2) >
9719 (int)(pBt->usableSize*2/3)
9720 || pPage->nCell==1 /* See dbfuzz001.test for a test case */
9722 /* A b-tree rebalance will be required after deleting this entry.
9723 ** Save the cursor key. */
9724 rc = saveCursorKey(pCur);
9725 if( rc ) return rc;
9726 }else{
9727 bPreserve = 2;
9731 /* If the page containing the entry to delete is not a leaf page, move
9732 ** the cursor to the largest entry in the tree that is smaller than
9733 ** the entry being deleted. This cell will replace the cell being deleted
9734 ** from the internal node. The 'previous' entry is used for this instead
9735 ** of the 'next' entry, as the previous entry is always a part of the
9736 ** sub-tree headed by the child page of the cell being deleted. This makes
9737 ** balancing the tree following the delete operation easier. */
9738 if( !pPage->leaf ){
9739 rc = sqlite3BtreePrevious(pCur, 0);
9740 assert( rc!=SQLITE_DONE );
9741 if( rc ) return rc;
9744 /* Save the positions of any other cursors open on this table before
9745 ** making any modifications. */
9746 if( pCur->curFlags & BTCF_Multiple ){
9747 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
9748 if( rc ) return rc;
9751 /* If this is a delete operation to remove a row from a table b-tree,
9752 ** invalidate any incrblob cursors open on the row being deleted. */
9753 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){
9754 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
9757 /* Make the page containing the entry to be deleted writable. Then free any
9758 ** overflow pages associated with the entry and finally remove the cell
9759 ** itself from within the page. */
9760 rc = sqlite3PagerWrite(pPage->pDbPage);
9761 if( rc ) return rc;
9762 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
9763 dropCell(pPage, iCellIdx, info.nSize, &rc);
9764 if( rc ) return rc;
9766 /* If the cell deleted was not located on a leaf page, then the cursor
9767 ** is currently pointing to the largest entry in the sub-tree headed
9768 ** by the child-page of the cell that was just deleted from an internal
9769 ** node. The cell from the leaf node needs to be moved to the internal
9770 ** node to replace the deleted cell. */
9771 if( !pPage->leaf ){
9772 MemPage *pLeaf = pCur->pPage;
9773 int nCell;
9774 Pgno n;
9775 unsigned char *pTmp;
9777 if( pLeaf->nFree<0 ){
9778 rc = btreeComputeFreeSpace(pLeaf);
9779 if( rc ) return rc;
9781 if( iCellDepth<pCur->iPage-1 ){
9782 n = pCur->apPage[iCellDepth+1]->pgno;
9783 }else{
9784 n = pCur->pPage->pgno;
9786 pCell = findCell(pLeaf, pLeaf->nCell-1);
9787 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
9788 nCell = pLeaf->xCellSize(pLeaf, pCell);
9789 assert( MX_CELL_SIZE(pBt) >= nCell );
9790 pTmp = pBt->pTmpSpace;
9791 assert( pTmp!=0 );
9792 rc = sqlite3PagerWrite(pLeaf->pDbPage);
9793 if( rc==SQLITE_OK ){
9794 rc = insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n);
9796 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
9797 if( rc ) return rc;
9800 /* Balance the tree. If the entry deleted was located on a leaf page,
9801 ** then the cursor still points to that page. In this case the first
9802 ** call to balance() repairs the tree, and the if(...) condition is
9803 ** never true.
9805 ** Otherwise, if the entry deleted was on an internal node page, then
9806 ** pCur is pointing to the leaf page from which a cell was removed to
9807 ** replace the cell deleted from the internal node. This is slightly
9808 ** tricky as the leaf node may be underfull, and the internal node may
9809 ** be either under or overfull. In this case run the balancing algorithm
9810 ** on the leaf node first. If the balance proceeds far enough up the
9811 ** tree that we can be sure that any problem in the internal node has
9812 ** been corrected, so be it. Otherwise, after balancing the leaf node,
9813 ** walk the cursor up the tree to the internal node and balance it as
9814 ** well. */
9815 assert( pCur->pPage->nOverflow==0 );
9816 assert( pCur->pPage->nFree>=0 );
9817 if( pCur->pPage->nFree*3<=(int)pCur->pBt->usableSize*2 ){
9818 /* Optimization: If the free space is less than 2/3rds of the page,
9819 ** then balance() will always be a no-op. No need to invoke it. */
9820 rc = SQLITE_OK;
9821 }else{
9822 rc = balance(pCur);
9824 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
9825 releasePageNotNull(pCur->pPage);
9826 pCur->iPage--;
9827 while( pCur->iPage>iCellDepth ){
9828 releasePage(pCur->apPage[pCur->iPage--]);
9830 pCur->pPage = pCur->apPage[pCur->iPage];
9831 rc = balance(pCur);
9834 if( rc==SQLITE_OK ){
9835 if( bPreserve>1 ){
9836 assert( (pCur->iPage==iCellDepth || CORRUPT_DB) );
9837 assert( pPage==pCur->pPage || CORRUPT_DB );
9838 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
9839 pCur->eState = CURSOR_SKIPNEXT;
9840 if( iCellIdx>=pPage->nCell ){
9841 pCur->skipNext = -1;
9842 pCur->ix = pPage->nCell-1;
9843 }else{
9844 pCur->skipNext = 1;
9846 }else{
9847 rc = moveToRoot(pCur);
9848 if( bPreserve ){
9849 btreeReleaseAllCursorPages(pCur);
9850 pCur->eState = CURSOR_REQUIRESEEK;
9852 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
9855 return rc;
9859 ** Create a new BTree table. Write into *piTable the page
9860 ** number for the root page of the new table.
9862 ** The type of type is determined by the flags parameter. Only the
9863 ** following values of flags are currently in use. Other values for
9864 ** flags might not work:
9866 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
9867 ** BTREE_ZERODATA Used for SQL indices
9869 static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){
9870 BtShared *pBt = p->pBt;
9871 MemPage *pRoot;
9872 Pgno pgnoRoot;
9873 int rc;
9874 int ptfFlags; /* Page-type flags for the root page of new table */
9876 assert( sqlite3BtreeHoldsMutex(p) );
9877 assert( pBt->inTransaction==TRANS_WRITE );
9878 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
9880 #ifdef SQLITE_OMIT_AUTOVACUUM
9881 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
9882 if( rc ){
9883 return rc;
9885 #else
9886 if( pBt->autoVacuum ){
9887 Pgno pgnoMove; /* Move a page here to make room for the root-page */
9888 MemPage *pPageMove; /* The page to move to. */
9890 /* Creating a new table may probably require moving an existing database
9891 ** to make room for the new tables root page. In case this page turns
9892 ** out to be an overflow page, delete all overflow page-map caches
9893 ** held by open cursors.
9895 invalidateAllOverflowCache(pBt);
9897 /* Read the value of meta[3] from the database to determine where the
9898 ** root page of the new table should go. meta[3] is the largest root-page
9899 ** created so far, so the new root-page is (meta[3]+1).
9901 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
9902 if( pgnoRoot>btreePagecount(pBt) ){
9903 return SQLITE_CORRUPT_BKPT;
9905 pgnoRoot++;
9907 /* The new root-page may not be allocated on a pointer-map page, or the
9908 ** PENDING_BYTE page.
9910 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
9911 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
9912 pgnoRoot++;
9914 assert( pgnoRoot>=3 );
9916 /* Allocate a page. The page that currently resides at pgnoRoot will
9917 ** be moved to the allocated page (unless the allocated page happens
9918 ** to reside at pgnoRoot).
9920 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
9921 if( rc!=SQLITE_OK ){
9922 return rc;
9925 if( pgnoMove!=pgnoRoot ){
9926 /* pgnoRoot is the page that will be used for the root-page of
9927 ** the new table (assuming an error did not occur). But we were
9928 ** allocated pgnoMove. If required (i.e. if it was not allocated
9929 ** by extending the file), the current page at position pgnoMove
9930 ** is already journaled.
9932 u8 eType = 0;
9933 Pgno iPtrPage = 0;
9935 /* Save the positions of any open cursors. This is required in
9936 ** case they are holding a reference to an xFetch reference
9937 ** corresponding to page pgnoRoot. */
9938 rc = saveAllCursors(pBt, 0, 0);
9939 releasePage(pPageMove);
9940 if( rc!=SQLITE_OK ){
9941 return rc;
9944 /* Move the page currently at pgnoRoot to pgnoMove. */
9945 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
9946 if( rc!=SQLITE_OK ){
9947 return rc;
9949 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
9950 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
9951 rc = SQLITE_CORRUPT_BKPT;
9953 if( rc!=SQLITE_OK ){
9954 releasePage(pRoot);
9955 return rc;
9957 assert( eType!=PTRMAP_ROOTPAGE );
9958 assert( eType!=PTRMAP_FREEPAGE );
9959 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
9960 releasePage(pRoot);
9962 /* Obtain the page at pgnoRoot */
9963 if( rc!=SQLITE_OK ){
9964 return rc;
9966 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
9967 if( rc!=SQLITE_OK ){
9968 return rc;
9970 rc = sqlite3PagerWrite(pRoot->pDbPage);
9971 if( rc!=SQLITE_OK ){
9972 releasePage(pRoot);
9973 return rc;
9975 }else{
9976 pRoot = pPageMove;
9979 /* Update the pointer-map and meta-data with the new root-page number. */
9980 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
9981 if( rc ){
9982 releasePage(pRoot);
9983 return rc;
9986 /* When the new root page was allocated, page 1 was made writable in
9987 ** order either to increase the database filesize, or to decrement the
9988 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
9990 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
9991 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
9992 if( NEVER(rc) ){
9993 releasePage(pRoot);
9994 return rc;
9997 }else{
9998 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
9999 if( rc ) return rc;
10001 #endif
10002 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
10003 if( createTabFlags & BTREE_INTKEY ){
10004 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
10005 }else{
10006 ptfFlags = PTF_ZERODATA | PTF_LEAF;
10008 zeroPage(pRoot, ptfFlags);
10009 sqlite3PagerUnref(pRoot->pDbPage);
10010 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
10011 *piTable = pgnoRoot;
10012 return SQLITE_OK;
10014 int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){
10015 int rc;
10016 sqlite3BtreeEnter(p);
10017 rc = btreeCreateTable(p, piTable, flags);
10018 sqlite3BtreeLeave(p);
10019 return rc;
10023 ** Erase the given database page and all its children. Return
10024 ** the page to the freelist.
10026 static int clearDatabasePage(
10027 BtShared *pBt, /* The BTree that contains the table */
10028 Pgno pgno, /* Page number to clear */
10029 int freePageFlag, /* Deallocate page if true */
10030 i64 *pnChange /* Add number of Cells freed to this counter */
10032 MemPage *pPage;
10033 int rc;
10034 unsigned char *pCell;
10035 int i;
10036 int hdr;
10037 CellInfo info;
10039 assert( sqlite3_mutex_held(pBt->mutex) );
10040 if( pgno>btreePagecount(pBt) ){
10041 return SQLITE_CORRUPT_BKPT;
10043 rc = getAndInitPage(pBt, pgno, &pPage, 0);
10044 if( rc ) return rc;
10045 if( (pBt->openFlags & BTREE_SINGLE)==0
10046 && sqlite3PagerPageRefcount(pPage->pDbPage) != (1 + (pgno==1))
10048 rc = SQLITE_CORRUPT_BKPT;
10049 goto cleardatabasepage_out;
10051 hdr = pPage->hdrOffset;
10052 for(i=0; i<pPage->nCell; i++){
10053 pCell = findCell(pPage, i);
10054 if( !pPage->leaf ){
10055 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
10056 if( rc ) goto cleardatabasepage_out;
10058 BTREE_CLEAR_CELL(rc, pPage, pCell, info);
10059 if( rc ) goto cleardatabasepage_out;
10061 if( !pPage->leaf ){
10062 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
10063 if( rc ) goto cleardatabasepage_out;
10064 if( pPage->intKey ) pnChange = 0;
10066 if( pnChange ){
10067 testcase( !pPage->intKey );
10068 *pnChange += pPage->nCell;
10070 if( freePageFlag ){
10071 freePage(pPage, &rc);
10072 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
10073 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
10076 cleardatabasepage_out:
10077 releasePage(pPage);
10078 return rc;
10082 ** Delete all information from a single table in the database. iTable is
10083 ** the page number of the root of the table. After this routine returns,
10084 ** the root page is empty, but still exists.
10086 ** This routine will fail with SQLITE_LOCKED if there are any open
10087 ** read cursors on the table. Open write cursors are moved to the
10088 ** root of the table.
10090 ** If pnChange is not NULL, then the integer value pointed to by pnChange
10091 ** is incremented by the number of entries in the table.
10093 int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){
10094 int rc;
10095 BtShared *pBt = p->pBt;
10096 sqlite3BtreeEnter(p);
10097 assert( p->inTrans==TRANS_WRITE );
10099 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
10101 if( SQLITE_OK==rc ){
10102 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
10103 ** is the root of a table b-tree - if it is not, the following call is
10104 ** a no-op). */
10105 if( p->hasIncrblobCur ){
10106 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
10108 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
10110 sqlite3BtreeLeave(p);
10111 return rc;
10115 ** Delete all information from the single table that pCur is open on.
10117 ** This routine only work for pCur on an ephemeral table.
10119 int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
10120 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
10124 ** Erase all information in a table and add the root of the table to
10125 ** the freelist. Except, the root of the principle table (the one on
10126 ** page 1) is never added to the freelist.
10128 ** This routine will fail with SQLITE_LOCKED if there are any open
10129 ** cursors on the table.
10131 ** If AUTOVACUUM is enabled and the page at iTable is not the last
10132 ** root page in the database file, then the last root page
10133 ** in the database file is moved into the slot formerly occupied by
10134 ** iTable and that last slot formerly occupied by the last root page
10135 ** is added to the freelist instead of iTable. In this say, all
10136 ** root pages are kept at the beginning of the database file, which
10137 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
10138 ** page number that used to be the last root page in the file before
10139 ** the move. If no page gets moved, *piMoved is set to 0.
10140 ** The last root page is recorded in meta[3] and the value of
10141 ** meta[3] is updated by this procedure.
10143 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
10144 int rc;
10145 MemPage *pPage = 0;
10146 BtShared *pBt = p->pBt;
10148 assert( sqlite3BtreeHoldsMutex(p) );
10149 assert( p->inTrans==TRANS_WRITE );
10150 assert( iTable>=2 );
10151 if( iTable>btreePagecount(pBt) ){
10152 return SQLITE_CORRUPT_BKPT;
10155 rc = sqlite3BtreeClearTable(p, iTable, 0);
10156 if( rc ) return rc;
10157 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
10158 if( NEVER(rc) ){
10159 releasePage(pPage);
10160 return rc;
10163 *piMoved = 0;
10165 #ifdef SQLITE_OMIT_AUTOVACUUM
10166 freePage(pPage, &rc);
10167 releasePage(pPage);
10168 #else
10169 if( pBt->autoVacuum ){
10170 Pgno maxRootPgno;
10171 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
10173 if( iTable==maxRootPgno ){
10174 /* If the table being dropped is the table with the largest root-page
10175 ** number in the database, put the root page on the free list.
10177 freePage(pPage, &rc);
10178 releasePage(pPage);
10179 if( rc!=SQLITE_OK ){
10180 return rc;
10182 }else{
10183 /* The table being dropped does not have the largest root-page
10184 ** number in the database. So move the page that does into the
10185 ** gap left by the deleted root-page.
10187 MemPage *pMove;
10188 releasePage(pPage);
10189 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10190 if( rc!=SQLITE_OK ){
10191 return rc;
10193 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
10194 releasePage(pMove);
10195 if( rc!=SQLITE_OK ){
10196 return rc;
10198 pMove = 0;
10199 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10200 freePage(pMove, &rc);
10201 releasePage(pMove);
10202 if( rc!=SQLITE_OK ){
10203 return rc;
10205 *piMoved = maxRootPgno;
10208 /* Set the new 'max-root-page' value in the database header. This
10209 ** is the old value less one, less one more if that happens to
10210 ** be a root-page number, less one again if that is the
10211 ** PENDING_BYTE_PAGE.
10213 maxRootPgno--;
10214 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
10215 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
10216 maxRootPgno--;
10218 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
10220 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
10221 }else{
10222 freePage(pPage, &rc);
10223 releasePage(pPage);
10225 #endif
10226 return rc;
10228 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
10229 int rc;
10230 sqlite3BtreeEnter(p);
10231 rc = btreeDropTable(p, iTable, piMoved);
10232 sqlite3BtreeLeave(p);
10233 return rc;
10238 ** This function may only be called if the b-tree connection already
10239 ** has a read or write transaction open on the database.
10241 ** Read the meta-information out of a database file. Meta[0]
10242 ** is the number of free pages currently in the database. Meta[1]
10243 ** through meta[15] are available for use by higher layers. Meta[0]
10244 ** is read-only, the others are read/write.
10246 ** The schema layer numbers meta values differently. At the schema
10247 ** layer (and the SetCookie and ReadCookie opcodes) the number of
10248 ** free pages is not visible. So Cookie[0] is the same as Meta[1].
10250 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
10251 ** of reading the value out of the header, it instead loads the "DataVersion"
10252 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
10253 ** database file. It is a number computed by the pager. But its access
10254 ** pattern is the same as header meta values, and so it is convenient to
10255 ** read it from this routine.
10257 void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
10258 BtShared *pBt = p->pBt;
10260 sqlite3BtreeEnter(p);
10261 assert( p->inTrans>TRANS_NONE );
10262 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) );
10263 assert( pBt->pPage1 );
10264 assert( idx>=0 && idx<=15 );
10266 if( idx==BTREE_DATA_VERSION ){
10267 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion;
10268 }else{
10269 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
10272 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
10273 ** database, mark the database as read-only. */
10274 #ifdef SQLITE_OMIT_AUTOVACUUM
10275 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
10276 pBt->btsFlags |= BTS_READ_ONLY;
10278 #endif
10280 sqlite3BtreeLeave(p);
10284 ** Write meta-information back into the database. Meta[0] is
10285 ** read-only and may not be written.
10287 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
10288 BtShared *pBt = p->pBt;
10289 unsigned char *pP1;
10290 int rc;
10291 assert( idx>=1 && idx<=15 );
10292 sqlite3BtreeEnter(p);
10293 assert( p->inTrans==TRANS_WRITE );
10294 assert( pBt->pPage1!=0 );
10295 pP1 = pBt->pPage1->aData;
10296 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
10297 if( rc==SQLITE_OK ){
10298 put4byte(&pP1[36 + idx*4], iMeta);
10299 #ifndef SQLITE_OMIT_AUTOVACUUM
10300 if( idx==BTREE_INCR_VACUUM ){
10301 assert( pBt->autoVacuum || iMeta==0 );
10302 assert( iMeta==0 || iMeta==1 );
10303 pBt->incrVacuum = (u8)iMeta;
10305 #endif
10307 sqlite3BtreeLeave(p);
10308 return rc;
10312 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
10313 ** number of entries in the b-tree and write the result to *pnEntry.
10315 ** SQLITE_OK is returned if the operation is successfully executed.
10316 ** Otherwise, if an error is encountered (i.e. an IO error or database
10317 ** corruption) an SQLite error code is returned.
10319 int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){
10320 i64 nEntry = 0; /* Value to return in *pnEntry */
10321 int rc; /* Return code */
10323 rc = moveToRoot(pCur);
10324 if( rc==SQLITE_EMPTY ){
10325 *pnEntry = 0;
10326 return SQLITE_OK;
10329 /* Unless an error occurs, the following loop runs one iteration for each
10330 ** page in the B-Tree structure (not including overflow pages).
10332 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){
10333 int iIdx; /* Index of child node in parent */
10334 MemPage *pPage; /* Current page of the b-tree */
10336 /* If this is a leaf page or the tree is not an int-key tree, then
10337 ** this page contains countable entries. Increment the entry counter
10338 ** accordingly.
10340 pPage = pCur->pPage;
10341 if( pPage->leaf || !pPage->intKey ){
10342 nEntry += pPage->nCell;
10345 /* pPage is a leaf node. This loop navigates the cursor so that it
10346 ** points to the first interior cell that it points to the parent of
10347 ** the next page in the tree that has not yet been visited. The
10348 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
10349 ** of the page, or to the number of cells in the page if the next page
10350 ** to visit is the right-child of its parent.
10352 ** If all pages in the tree have been visited, return SQLITE_OK to the
10353 ** caller.
10355 if( pPage->leaf ){
10356 do {
10357 if( pCur->iPage==0 ){
10358 /* All pages of the b-tree have been visited. Return successfully. */
10359 *pnEntry = nEntry;
10360 return moveToRoot(pCur);
10362 moveToParent(pCur);
10363 }while ( pCur->ix>=pCur->pPage->nCell );
10365 pCur->ix++;
10366 pPage = pCur->pPage;
10369 /* Descend to the child node of the cell that the cursor currently
10370 ** points at. This is the right-child if (iIdx==pPage->nCell).
10372 iIdx = pCur->ix;
10373 if( iIdx==pPage->nCell ){
10374 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
10375 }else{
10376 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
10380 /* An error has occurred. Return an error code. */
10381 return rc;
10385 ** Return the pager associated with a BTree. This routine is used for
10386 ** testing and debugging only.
10388 Pager *sqlite3BtreePager(Btree *p){
10389 return p->pBt->pPager;
10392 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
10394 ** Record an OOM error during integrity_check
10396 static void checkOom(IntegrityCk *pCheck){
10397 pCheck->rc = SQLITE_NOMEM;
10398 pCheck->mxErr = 0; /* Causes integrity_check processing to stop */
10399 if( pCheck->nErr==0 ) pCheck->nErr++;
10403 ** Invoke the progress handler, if appropriate. Also check for an
10404 ** interrupt.
10406 static void checkProgress(IntegrityCk *pCheck){
10407 sqlite3 *db = pCheck->db;
10408 if( AtomicLoad(&db->u1.isInterrupted) ){
10409 pCheck->rc = SQLITE_INTERRUPT;
10410 pCheck->nErr++;
10411 pCheck->mxErr = 0;
10413 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10414 if( db->xProgress ){
10415 assert( db->nProgressOps>0 );
10416 pCheck->nStep++;
10417 if( (pCheck->nStep % db->nProgressOps)==0
10418 && db->xProgress(db->pProgressArg)
10420 pCheck->rc = SQLITE_INTERRUPT;
10421 pCheck->nErr++;
10422 pCheck->mxErr = 0;
10425 #endif
10429 ** Append a message to the error message string.
10431 static void checkAppendMsg(
10432 IntegrityCk *pCheck,
10433 const char *zFormat,
10436 va_list ap;
10437 checkProgress(pCheck);
10438 if( !pCheck->mxErr ) return;
10439 pCheck->mxErr--;
10440 pCheck->nErr++;
10441 va_start(ap, zFormat);
10442 if( pCheck->errMsg.nChar ){
10443 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
10445 if( pCheck->zPfx ){
10446 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx,
10447 pCheck->v0, pCheck->v1, pCheck->v2);
10449 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
10450 va_end(ap);
10451 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
10452 checkOom(pCheck);
10455 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
10457 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
10460 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
10461 ** corresponds to page iPg is already set.
10463 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
10464 assert( pCheck->aPgRef!=0 );
10465 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
10466 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
10470 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
10472 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
10473 assert( pCheck->aPgRef!=0 );
10474 assert( iPg<=pCheck->nCkPage && sizeof(pCheck->aPgRef[0])==1 );
10475 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
10480 ** Add 1 to the reference count for page iPage. If this is the second
10481 ** reference to the page, add an error message to pCheck->zErrMsg.
10482 ** Return 1 if there are 2 or more references to the page and 0 if
10483 ** if this is the first reference to the page.
10485 ** Also check that the page number is in bounds.
10487 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
10488 if( iPage>pCheck->nCkPage || iPage==0 ){
10489 checkAppendMsg(pCheck, "invalid page number %u", iPage);
10490 return 1;
10492 if( getPageReferenced(pCheck, iPage) ){
10493 checkAppendMsg(pCheck, "2nd reference to page %u", iPage);
10494 return 1;
10496 setPageReferenced(pCheck, iPage);
10497 return 0;
10500 #ifndef SQLITE_OMIT_AUTOVACUUM
10502 ** Check that the entry in the pointer-map for page iChild maps to
10503 ** page iParent, pointer type ptrType. If not, append an error message
10504 ** to pCheck.
10506 static void checkPtrmap(
10507 IntegrityCk *pCheck, /* Integrity check context */
10508 Pgno iChild, /* Child page number */
10509 u8 eType, /* Expected pointer map type */
10510 Pgno iParent /* Expected pointer map parent page number */
10512 int rc;
10513 u8 ePtrmapType;
10514 Pgno iPtrmapParent;
10516 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
10517 if( rc!=SQLITE_OK ){
10518 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) checkOom(pCheck);
10519 checkAppendMsg(pCheck, "Failed to read ptrmap key=%u", iChild);
10520 return;
10523 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
10524 checkAppendMsg(pCheck,
10525 "Bad ptr map entry key=%u expected=(%u,%u) got=(%u,%u)",
10526 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
10529 #endif
10532 ** Check the integrity of the freelist or of an overflow page list.
10533 ** Verify that the number of pages on the list is N.
10535 static void checkList(
10536 IntegrityCk *pCheck, /* Integrity checking context */
10537 int isFreeList, /* True for a freelist. False for overflow page list */
10538 Pgno iPage, /* Page number for first page in the list */
10539 u32 N /* Expected number of pages in the list */
10541 int i;
10542 u32 expected = N;
10543 int nErrAtStart = pCheck->nErr;
10544 while( iPage!=0 && pCheck->mxErr ){
10545 DbPage *pOvflPage;
10546 unsigned char *pOvflData;
10547 if( checkRef(pCheck, iPage) ) break;
10548 N--;
10549 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
10550 checkAppendMsg(pCheck, "failed to get page %u", iPage);
10551 break;
10553 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
10554 if( isFreeList ){
10555 u32 n = (u32)get4byte(&pOvflData[4]);
10556 #ifndef SQLITE_OMIT_AUTOVACUUM
10557 if( pCheck->pBt->autoVacuum ){
10558 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
10560 #endif
10561 if( n>pCheck->pBt->usableSize/4-2 ){
10562 checkAppendMsg(pCheck,
10563 "freelist leaf count too big on page %u", iPage);
10564 N--;
10565 }else{
10566 for(i=0; i<(int)n; i++){
10567 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
10568 #ifndef SQLITE_OMIT_AUTOVACUUM
10569 if( pCheck->pBt->autoVacuum ){
10570 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
10572 #endif
10573 checkRef(pCheck, iFreePage);
10575 N -= n;
10578 #ifndef SQLITE_OMIT_AUTOVACUUM
10579 else{
10580 /* If this database supports auto-vacuum and iPage is not the last
10581 ** page in this overflow list, check that the pointer-map entry for
10582 ** the following page matches iPage.
10584 if( pCheck->pBt->autoVacuum && N>0 ){
10585 i = get4byte(pOvflData);
10586 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
10589 #endif
10590 iPage = get4byte(pOvflData);
10591 sqlite3PagerUnref(pOvflPage);
10593 if( N && nErrAtStart==pCheck->nErr ){
10594 checkAppendMsg(pCheck,
10595 "%s is %u but should be %u",
10596 isFreeList ? "size" : "overflow list length",
10597 expected-N, expected);
10600 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
10603 ** An implementation of a min-heap.
10605 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the
10606 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
10607 ** and aHeap[N*2+1].
10609 ** The heap property is this: Every node is less than or equal to both
10610 ** of its daughter nodes. A consequence of the heap property is that the
10611 ** root node aHeap[1] is always the minimum value currently in the heap.
10613 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
10614 ** the heap, preserving the heap property. The btreeHeapPull() routine
10615 ** removes the root element from the heap (the minimum value in the heap)
10616 ** and then moves other nodes around as necessary to preserve the heap
10617 ** property.
10619 ** This heap is used for cell overlap and coverage testing. Each u32
10620 ** entry represents the span of a cell or freeblock on a btree page.
10621 ** The upper 16 bits are the index of the first byte of a range and the
10622 ** lower 16 bits are the index of the last byte of that range.
10624 static void btreeHeapInsert(u32 *aHeap, u32 x){
10625 u32 j, i;
10626 assert( aHeap!=0 );
10627 i = ++aHeap[0];
10628 aHeap[i] = x;
10629 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
10630 x = aHeap[j];
10631 aHeap[j] = aHeap[i];
10632 aHeap[i] = x;
10633 i = j;
10636 static int btreeHeapPull(u32 *aHeap, u32 *pOut){
10637 u32 j, i, x;
10638 if( (x = aHeap[0])==0 ) return 0;
10639 *pOut = aHeap[1];
10640 aHeap[1] = aHeap[x];
10641 aHeap[x] = 0xffffffff;
10642 aHeap[0]--;
10643 i = 1;
10644 while( (j = i*2)<=aHeap[0] ){
10645 if( aHeap[j]>aHeap[j+1] ) j++;
10646 if( aHeap[i]<aHeap[j] ) break;
10647 x = aHeap[i];
10648 aHeap[i] = aHeap[j];
10649 aHeap[j] = x;
10650 i = j;
10652 return 1;
10655 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
10657 ** Do various sanity checks on a single page of a tree. Return
10658 ** the tree depth. Root pages return 0. Parents of root pages
10659 ** return 1, and so forth.
10661 ** These checks are done:
10663 ** 1. Make sure that cells and freeblocks do not overlap
10664 ** but combine to completely cover the page.
10665 ** 2. Make sure integer cell keys are in order.
10666 ** 3. Check the integrity of overflow pages.
10667 ** 4. Recursively call checkTreePage on all children.
10668 ** 5. Verify that the depth of all children is the same.
10670 static int checkTreePage(
10671 IntegrityCk *pCheck, /* Context for the sanity check */
10672 Pgno iPage, /* Page number of the page to check */
10673 i64 *piMinKey, /* Write minimum integer primary key here */
10674 i64 maxKey /* Error if integer primary key greater than this */
10676 MemPage *pPage = 0; /* The page being analyzed */
10677 int i; /* Loop counter */
10678 int rc; /* Result code from subroutine call */
10679 int depth = -1, d2; /* Depth of a subtree */
10680 int pgno; /* Page number */
10681 int nFrag; /* Number of fragmented bytes on the page */
10682 int hdr; /* Offset to the page header */
10683 int cellStart; /* Offset to the start of the cell pointer array */
10684 int nCell; /* Number of cells */
10685 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
10686 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
10687 ** False if IPK must be strictly less than maxKey */
10688 u8 *data; /* Page content */
10689 u8 *pCell; /* Cell content */
10690 u8 *pCellIdx; /* Next element of the cell pointer array */
10691 BtShared *pBt; /* The BtShared object that owns pPage */
10692 u32 pc; /* Address of a cell */
10693 u32 usableSize; /* Usable size of the page */
10694 u32 contentOffset; /* Offset to the start of the cell content area */
10695 u32 *heap = 0; /* Min-heap used for checking cell coverage */
10696 u32 x, prev = 0; /* Next and previous entry on the min-heap */
10697 const char *saved_zPfx = pCheck->zPfx;
10698 int saved_v1 = pCheck->v1;
10699 int saved_v2 = pCheck->v2;
10700 u8 savedIsInit = 0;
10702 /* Check that the page exists
10704 checkProgress(pCheck);
10705 if( pCheck->mxErr==0 ) goto end_of_check;
10706 pBt = pCheck->pBt;
10707 usableSize = pBt->usableSize;
10708 if( iPage==0 ) return 0;
10709 if( checkRef(pCheck, iPage) ) return 0;
10710 pCheck->zPfx = "Tree %u page %u: ";
10711 pCheck->v1 = iPage;
10712 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){
10713 checkAppendMsg(pCheck,
10714 "unable to get the page. error code=%d", rc);
10715 if( rc==SQLITE_IOERR_NOMEM ) pCheck->rc = SQLITE_NOMEM;
10716 goto end_of_check;
10719 /* Clear MemPage.isInit to make sure the corruption detection code in
10720 ** btreeInitPage() is executed. */
10721 savedIsInit = pPage->isInit;
10722 pPage->isInit = 0;
10723 if( (rc = btreeInitPage(pPage))!=0 ){
10724 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
10725 checkAppendMsg(pCheck,
10726 "btreeInitPage() returns error code %d", rc);
10727 goto end_of_check;
10729 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
10730 assert( rc==SQLITE_CORRUPT );
10731 checkAppendMsg(pCheck, "free space corruption", rc);
10732 goto end_of_check;
10734 data = pPage->aData;
10735 hdr = pPage->hdrOffset;
10737 /* Set up for cell analysis */
10738 pCheck->zPfx = "Tree %u page %u cell %u: ";
10739 contentOffset = get2byteNotZero(&data[hdr+5]);
10740 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
10742 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
10743 ** number of cells on the page. */
10744 nCell = get2byte(&data[hdr+3]);
10745 assert( pPage->nCell==nCell );
10747 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
10748 ** immediately follows the b-tree page header. */
10749 cellStart = hdr + 12 - 4*pPage->leaf;
10750 assert( pPage->aCellIdx==&data[cellStart] );
10751 pCellIdx = &data[cellStart + 2*(nCell-1)];
10753 if( !pPage->leaf ){
10754 /* Analyze the right-child page of internal pages */
10755 pgno = get4byte(&data[hdr+8]);
10756 #ifndef SQLITE_OMIT_AUTOVACUUM
10757 if( pBt->autoVacuum ){
10758 pCheck->zPfx = "Tree %u page %u right child: ";
10759 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
10761 #endif
10762 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10763 keyCanBeEqual = 0;
10764 }else{
10765 /* For leaf pages, the coverage check will occur in the same loop
10766 ** as the other cell checks, so initialize the heap. */
10767 heap = pCheck->heap;
10768 heap[0] = 0;
10771 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
10772 ** integer offsets to the cell contents. */
10773 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
10774 CellInfo info;
10776 /* Check cell size */
10777 pCheck->v2 = i;
10778 assert( pCellIdx==&data[cellStart + i*2] );
10779 pc = get2byteAligned(pCellIdx);
10780 pCellIdx -= 2;
10781 if( pc<contentOffset || pc>usableSize-4 ){
10782 checkAppendMsg(pCheck, "Offset %u out of range %u..%u",
10783 pc, contentOffset, usableSize-4);
10784 doCoverageCheck = 0;
10785 continue;
10787 pCell = &data[pc];
10788 pPage->xParseCell(pPage, pCell, &info);
10789 if( pc+info.nSize>usableSize ){
10790 checkAppendMsg(pCheck, "Extends off end of page");
10791 doCoverageCheck = 0;
10792 continue;
10795 /* Check for integer primary key out of range */
10796 if( pPage->intKey ){
10797 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
10798 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
10800 maxKey = info.nKey;
10801 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
10804 /* Check the content overflow list */
10805 if( info.nPayload>info.nLocal ){
10806 u32 nPage; /* Number of pages on the overflow chain */
10807 Pgno pgnoOvfl; /* First page of the overflow chain */
10808 assert( pc + info.nSize - 4 <= usableSize );
10809 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
10810 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
10811 #ifndef SQLITE_OMIT_AUTOVACUUM
10812 if( pBt->autoVacuum ){
10813 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
10815 #endif
10816 checkList(pCheck, 0, pgnoOvfl, nPage);
10819 if( !pPage->leaf ){
10820 /* Check sanity of left child page for internal pages */
10821 pgno = get4byte(pCell);
10822 #ifndef SQLITE_OMIT_AUTOVACUUM
10823 if( pBt->autoVacuum ){
10824 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
10826 #endif
10827 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10828 keyCanBeEqual = 0;
10829 if( d2!=depth ){
10830 checkAppendMsg(pCheck, "Child page depth differs");
10831 depth = d2;
10833 }else{
10834 /* Populate the coverage-checking heap for leaf pages */
10835 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
10838 *piMinKey = maxKey;
10840 /* Check for complete coverage of the page
10842 pCheck->zPfx = 0;
10843 if( doCoverageCheck && pCheck->mxErr>0 ){
10844 /* For leaf pages, the min-heap has already been initialized and the
10845 ** cells have already been inserted. But for internal pages, that has
10846 ** not yet been done, so do it now */
10847 if( !pPage->leaf ){
10848 heap = pCheck->heap;
10849 heap[0] = 0;
10850 for(i=nCell-1; i>=0; i--){
10851 u32 size;
10852 pc = get2byteAligned(&data[cellStart+i*2]);
10853 size = pPage->xCellSize(pPage, &data[pc]);
10854 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
10857 /* Add the freeblocks to the min-heap
10859 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
10860 ** is the offset of the first freeblock, or zero if there are no
10861 ** freeblocks on the page.
10863 i = get2byte(&data[hdr+1]);
10864 while( i>0 ){
10865 int size, j;
10866 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
10867 size = get2byte(&data[i+2]);
10868 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */
10869 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
10870 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
10871 ** big-endian integer which is the offset in the b-tree page of the next
10872 ** freeblock in the chain, or zero if the freeblock is the last on the
10873 ** chain. */
10874 j = get2byte(&data[i]);
10875 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
10876 ** increasing offset. */
10877 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */
10878 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */
10879 i = j;
10881 /* Analyze the min-heap looking for overlap between cells and/or
10882 ** freeblocks, and counting the number of untracked bytes in nFrag.
10884 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
10885 ** There is an implied first entry the covers the page header, the cell
10886 ** pointer index, and the gap between the cell pointer index and the start
10887 ** of cell content.
10889 ** The loop below pulls entries from the min-heap in order and compares
10890 ** the start_address against the previous end_address. If there is an
10891 ** overlap, that means bytes are used multiple times. If there is a gap,
10892 ** that gap is added to the fragmentation count.
10894 nFrag = 0;
10895 prev = contentOffset - 1; /* Implied first min-heap entry */
10896 while( btreeHeapPull(heap,&x) ){
10897 if( (prev&0xffff)>=(x>>16) ){
10898 checkAppendMsg(pCheck,
10899 "Multiple uses for byte %u of page %u", x>>16, iPage);
10900 break;
10901 }else{
10902 nFrag += (x>>16) - (prev&0xffff) - 1;
10903 prev = x;
10906 nFrag += usableSize - (prev&0xffff) - 1;
10907 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
10908 ** is stored in the fifth field of the b-tree page header.
10909 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
10910 ** number of fragmented free bytes within the cell content area.
10912 if( heap[0]==0 && nFrag!=data[hdr+7] ){
10913 checkAppendMsg(pCheck,
10914 "Fragmentation of %u bytes reported as %u on page %u",
10915 nFrag, data[hdr+7], iPage);
10919 end_of_check:
10920 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
10921 releasePage(pPage);
10922 pCheck->zPfx = saved_zPfx;
10923 pCheck->v1 = saved_v1;
10924 pCheck->v2 = saved_v2;
10925 return depth+1;
10927 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
10929 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
10931 ** This routine does a complete check of the given BTree file. aRoot[] is
10932 ** an array of pages numbers were each page number is the root page of
10933 ** a table. nRoot is the number of entries in aRoot.
10935 ** A read-only or read-write transaction must be opened before calling
10936 ** this function.
10938 ** Write the number of error seen in *pnErr. Except for some memory
10939 ** allocation errors, an error message held in memory obtained from
10940 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
10941 ** returned. If a memory allocation error occurs, NULL is returned.
10943 ** If the first entry in aRoot[] is 0, that indicates that the list of
10944 ** root pages is incomplete. This is a "partial integrity-check". This
10945 ** happens when performing an integrity check on a single table. The
10946 ** zero is skipped, of course. But in addition, the freelist checks
10947 ** and the checks to make sure every page is referenced are also skipped,
10948 ** since obviously it is not possible to know which pages are covered by
10949 ** the unverified btrees. Except, if aRoot[1] is 1, then the freelist
10950 ** checks are still performed.
10952 int sqlite3BtreeIntegrityCheck(
10953 sqlite3 *db, /* Database connection that is running the check */
10954 Btree *p, /* The btree to be checked */
10955 Pgno *aRoot, /* An array of root pages numbers for individual trees */
10956 int nRoot, /* Number of entries in aRoot[] */
10957 int mxErr, /* Stop reporting errors after this many */
10958 int *pnErr, /* OUT: Write number of errors seen to this variable */
10959 char **pzOut /* OUT: Write the error message string here */
10961 Pgno i;
10962 IntegrityCk sCheck;
10963 BtShared *pBt = p->pBt;
10964 u64 savedDbFlags = pBt->db->flags;
10965 char zErr[100];
10966 int bPartial = 0; /* True if not checking all btrees */
10967 int bCkFreelist = 1; /* True to scan the freelist */
10968 VVA_ONLY( int nRef );
10969 assert( nRoot>0 );
10971 /* aRoot[0]==0 means this is a partial check */
10972 if( aRoot[0]==0 ){
10973 assert( nRoot>1 );
10974 bPartial = 1;
10975 if( aRoot[1]!=1 ) bCkFreelist = 0;
10978 sqlite3BtreeEnter(p);
10979 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
10980 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
10981 assert( nRef>=0 );
10982 memset(&sCheck, 0, sizeof(sCheck));
10983 sCheck.db = db;
10984 sCheck.pBt = pBt;
10985 sCheck.pPager = pBt->pPager;
10986 sCheck.nCkPage = btreePagecount(sCheck.pBt);
10987 sCheck.mxErr = mxErr;
10988 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
10989 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
10990 if( sCheck.nCkPage==0 ){
10991 goto integrity_ck_cleanup;
10994 sCheck.aPgRef = sqlite3MallocZero((sCheck.nCkPage / 8)+ 1);
10995 if( !sCheck.aPgRef ){
10996 checkOom(&sCheck);
10997 goto integrity_ck_cleanup;
10999 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
11000 if( sCheck.heap==0 ){
11001 checkOom(&sCheck);
11002 goto integrity_ck_cleanup;
11005 i = PENDING_BYTE_PAGE(pBt);
11006 if( i<=sCheck.nCkPage ) setPageReferenced(&sCheck, i);
11008 /* Check the integrity of the freelist
11010 if( bCkFreelist ){
11011 sCheck.zPfx = "Freelist: ";
11012 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
11013 get4byte(&pBt->pPage1->aData[36]));
11014 sCheck.zPfx = 0;
11017 /* Check all the tables.
11019 #ifndef SQLITE_OMIT_AUTOVACUUM
11020 if( !bPartial ){
11021 if( pBt->autoVacuum ){
11022 Pgno mx = 0;
11023 Pgno mxInHdr;
11024 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
11025 mxInHdr = get4byte(&pBt->pPage1->aData[52]);
11026 if( mx!=mxInHdr ){
11027 checkAppendMsg(&sCheck,
11028 "max rootpage (%u) disagrees with header (%u)",
11029 mx, mxInHdr
11032 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){
11033 checkAppendMsg(&sCheck,
11034 "incremental_vacuum enabled with a max rootpage of zero"
11038 #endif
11039 testcase( pBt->db->flags & SQLITE_CellSizeCk );
11040 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk;
11041 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
11042 i64 notUsed;
11043 if( aRoot[i]==0 ) continue;
11044 #ifndef SQLITE_OMIT_AUTOVACUUM
11045 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){
11046 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
11048 #endif
11049 sCheck.v0 = aRoot[i];
11050 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
11052 pBt->db->flags = savedDbFlags;
11054 /* Make sure every page in the file is referenced
11056 if( !bPartial ){
11057 for(i=1; i<=sCheck.nCkPage && sCheck.mxErr; i++){
11058 #ifdef SQLITE_OMIT_AUTOVACUUM
11059 if( getPageReferenced(&sCheck, i)==0 ){
11060 checkAppendMsg(&sCheck, "Page %u: never used", i);
11062 #else
11063 /* If the database supports auto-vacuum, make sure no tables contain
11064 ** references to pointer-map pages.
11066 if( getPageReferenced(&sCheck, i)==0 &&
11067 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
11068 checkAppendMsg(&sCheck, "Page %u: never used", i);
11070 if( getPageReferenced(&sCheck, i)!=0 &&
11071 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
11072 checkAppendMsg(&sCheck, "Page %u: pointer map referenced", i);
11074 #endif
11078 /* Clean up and report errors.
11080 integrity_ck_cleanup:
11081 sqlite3PageFree(sCheck.heap);
11082 sqlite3_free(sCheck.aPgRef);
11083 *pnErr = sCheck.nErr;
11084 if( sCheck.nErr==0 ){
11085 sqlite3_str_reset(&sCheck.errMsg);
11086 *pzOut = 0;
11087 }else{
11088 *pzOut = sqlite3StrAccumFinish(&sCheck.errMsg);
11090 /* Make sure this analysis did not leave any unref() pages. */
11091 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
11092 sqlite3BtreeLeave(p);
11093 return sCheck.rc;
11095 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
11098 ** Return the full pathname of the underlying database file. Return
11099 ** an empty string if the database is in-memory or a TEMP database.
11101 ** The pager filename is invariant as long as the pager is
11102 ** open so it is safe to access without the BtShared mutex.
11104 const char *sqlite3BtreeGetFilename(Btree *p){
11105 assert( p->pBt->pPager!=0 );
11106 return sqlite3PagerFilename(p->pBt->pPager, 1);
11110 ** Return the pathname of the journal file for this database. The return
11111 ** value of this routine is the same regardless of whether the journal file
11112 ** has been created or not.
11114 ** The pager journal filename is invariant as long as the pager is
11115 ** open so it is safe to access without the BtShared mutex.
11117 const char *sqlite3BtreeGetJournalname(Btree *p){
11118 assert( p->pBt->pPager!=0 );
11119 return sqlite3PagerJournalname(p->pBt->pPager);
11123 ** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE
11124 ** to describe the current transaction state of Btree p.
11126 int sqlite3BtreeTxnState(Btree *p){
11127 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
11128 return p ? p->inTrans : 0;
11131 #ifndef SQLITE_OMIT_WAL
11133 ** Run a checkpoint on the Btree passed as the first argument.
11135 ** Return SQLITE_LOCKED if this or any other connection has an open
11136 ** transaction on the shared-cache the argument Btree is connected to.
11138 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
11140 int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
11141 int rc = SQLITE_OK;
11142 if( p ){
11143 BtShared *pBt = p->pBt;
11144 sqlite3BtreeEnter(p);
11145 if( pBt->inTransaction!=TRANS_NONE ){
11146 rc = SQLITE_LOCKED;
11147 }else{
11148 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
11150 sqlite3BtreeLeave(p);
11152 return rc;
11154 #endif
11157 ** Return true if there is currently a backup running on Btree p.
11159 int sqlite3BtreeIsInBackup(Btree *p){
11160 assert( p );
11161 assert( sqlite3_mutex_held(p->db->mutex) );
11162 return p->nBackup!=0;
11166 ** This function returns a pointer to a blob of memory associated with
11167 ** a single shared-btree. The memory is used by client code for its own
11168 ** purposes (for example, to store a high-level schema associated with
11169 ** the shared-btree). The btree layer manages reference counting issues.
11171 ** The first time this is called on a shared-btree, nBytes bytes of memory
11172 ** are allocated, zeroed, and returned to the caller. For each subsequent
11173 ** call the nBytes parameter is ignored and a pointer to the same blob
11174 ** of memory returned.
11176 ** If the nBytes parameter is 0 and the blob of memory has not yet been
11177 ** allocated, a null pointer is returned. If the blob has already been
11178 ** allocated, it is returned as normal.
11180 ** Just before the shared-btree is closed, the function passed as the
11181 ** xFree argument when the memory allocation was made is invoked on the
11182 ** blob of allocated memory. The xFree function should not call sqlite3_free()
11183 ** on the memory, the btree layer does that.
11185 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
11186 BtShared *pBt = p->pBt;
11187 sqlite3BtreeEnter(p);
11188 if( !pBt->pSchema && nBytes ){
11189 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
11190 pBt->xFreeSchema = xFree;
11192 sqlite3BtreeLeave(p);
11193 return pBt->pSchema;
11197 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
11198 ** btree as the argument handle holds an exclusive lock on the
11199 ** sqlite_schema table. Otherwise SQLITE_OK.
11201 int sqlite3BtreeSchemaLocked(Btree *p){
11202 int rc;
11203 assert( sqlite3_mutex_held(p->db->mutex) );
11204 sqlite3BtreeEnter(p);
11205 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK);
11206 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
11207 sqlite3BtreeLeave(p);
11208 return rc;
11212 #ifndef SQLITE_OMIT_SHARED_CACHE
11214 ** Obtain a lock on the table whose root page is iTab. The
11215 ** lock is a write lock if isWritelock is true or a read lock
11216 ** if it is false.
11218 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
11219 int rc = SQLITE_OK;
11220 assert( p->inTrans!=TRANS_NONE );
11221 if( p->sharable ){
11222 u8 lockType = READ_LOCK + isWriteLock;
11223 assert( READ_LOCK+1==WRITE_LOCK );
11224 assert( isWriteLock==0 || isWriteLock==1 );
11226 sqlite3BtreeEnter(p);
11227 rc = querySharedCacheTableLock(p, iTab, lockType);
11228 if( rc==SQLITE_OK ){
11229 rc = setSharedCacheTableLock(p, iTab, lockType);
11231 sqlite3BtreeLeave(p);
11233 return rc;
11235 #endif
11237 #ifndef SQLITE_OMIT_INCRBLOB
11239 ** Argument pCsr must be a cursor opened for writing on an
11240 ** INTKEY table currently pointing at a valid table entry.
11241 ** This function modifies the data stored as part of that entry.
11243 ** Only the data content may only be modified, it is not possible to
11244 ** change the length of the data stored. If this function is called with
11245 ** parameters that attempt to write past the end of the existing data,
11246 ** no modifications are made and SQLITE_CORRUPT is returned.
11248 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
11249 int rc;
11250 assert( cursorOwnsBtShared(pCsr) );
11251 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
11252 assert( pCsr->curFlags & BTCF_Incrblob );
11254 rc = restoreCursorPosition(pCsr);
11255 if( rc!=SQLITE_OK ){
11256 return rc;
11258 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
11259 if( pCsr->eState!=CURSOR_VALID ){
11260 return SQLITE_ABORT;
11263 /* Save the positions of all other cursors open on this table. This is
11264 ** required in case any of them are holding references to an xFetch
11265 ** version of the b-tree page modified by the accessPayload call below.
11267 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
11268 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
11269 ** saveAllCursors can only return SQLITE_OK.
11271 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
11272 assert( rc==SQLITE_OK );
11274 /* Check some assumptions:
11275 ** (a) the cursor is open for writing,
11276 ** (b) there is a read/write transaction open,
11277 ** (c) the connection holds a write-lock on the table (if required),
11278 ** (d) there are no conflicting read-locks, and
11279 ** (e) the cursor points at a valid row of an intKey table.
11281 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
11282 return SQLITE_READONLY;
11284 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
11285 && pCsr->pBt->inTransaction==TRANS_WRITE );
11286 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
11287 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
11288 assert( pCsr->pPage->intKey );
11290 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
11294 ** Mark this cursor as an incremental blob cursor.
11296 void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
11297 pCur->curFlags |= BTCF_Incrblob;
11298 pCur->pBtree->hasIncrblobCur = 1;
11300 #endif
11303 ** Set both the "read version" (single byte at byte offset 18) and
11304 ** "write version" (single byte at byte offset 19) fields in the database
11305 ** header to iVersion.
11307 int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
11308 BtShared *pBt = pBtree->pBt;
11309 int rc; /* Return code */
11311 assert( iVersion==1 || iVersion==2 );
11313 /* If setting the version fields to 1, do not automatically open the
11314 ** WAL connection, even if the version fields are currently set to 2.
11316 pBt->btsFlags &= ~BTS_NO_WAL;
11317 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
11319 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
11320 if( rc==SQLITE_OK ){
11321 u8 *aData = pBt->pPage1->aData;
11322 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
11323 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
11324 if( rc==SQLITE_OK ){
11325 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
11326 if( rc==SQLITE_OK ){
11327 aData[18] = (u8)iVersion;
11328 aData[19] = (u8)iVersion;
11334 pBt->btsFlags &= ~BTS_NO_WAL;
11335 return rc;
11339 ** Return true if the cursor has a hint specified. This routine is
11340 ** only used from within assert() statements
11342 int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
11343 return (pCsr->hints & mask)!=0;
11347 ** Return true if the given Btree is read-only.
11349 int sqlite3BtreeIsReadonly(Btree *p){
11350 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
11354 ** Return the size of the header added to each page by this module.
11356 int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
11359 ** If no transaction is active and the database is not a temp-db, clear
11360 ** the in-memory pager cache.
11362 void sqlite3BtreeClearCache(Btree *p){
11363 BtShared *pBt = p->pBt;
11364 if( pBt->inTransaction==TRANS_NONE ){
11365 sqlite3PagerClearCache(pBt->pPager);
11369 #if !defined(SQLITE_OMIT_SHARED_CACHE)
11371 ** Return true if the Btree passed as the only argument is sharable.
11373 int sqlite3BtreeSharable(Btree *p){
11374 return p->sharable;
11378 ** Return the number of connections to the BtShared object accessed by
11379 ** the Btree handle passed as the only argument. For private caches
11380 ** this is always 1. For shared caches it may be 1 or greater.
11382 int sqlite3BtreeConnectionCount(Btree *p){
11383 testcase( p->sharable );
11384 return p->pBt->nRef;
11386 #endif