Add missing VdbeCoverage() macro to window.c.
[sqlite.git] / src / btree.c
blob20cc3eb106af95a394d58d59a482570ef076367c
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_MASTER.
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
116 ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
117 ** (MemPage*) as an argument. The (MemPage*) must not be NULL.
119 ** If SQLITE_DEBUG is not defined, then this macro is equivalent to
120 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
121 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
122 ** with the page number and filename associated with the (MemPage*).
124 #ifdef SQLITE_DEBUG
125 int corruptPageError(int lineno, MemPage *p){
126 char *zMsg;
127 sqlite3BeginBenignMalloc();
128 zMsg = sqlite3_mprintf("database corruption page %d of %s",
129 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
131 sqlite3EndBenignMalloc();
132 if( zMsg ){
133 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
135 sqlite3_free(zMsg);
136 return SQLITE_CORRUPT_BKPT;
138 # define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
139 #else
140 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
141 #endif
143 #ifndef SQLITE_OMIT_SHARED_CACHE
145 #ifdef SQLITE_DEBUG
147 **** This function is only used as part of an assert() statement. ***
149 ** Check to see if pBtree holds the required locks to read or write to the
150 ** table with root page iRoot. Return 1 if it does and 0 if not.
152 ** For example, when writing to a table with root-page iRoot via
153 ** Btree connection pBtree:
155 ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
157 ** When writing to an index that resides in a sharable database, the
158 ** caller should have first obtained a lock specifying the root page of
159 ** the corresponding table. This makes things a bit more complicated,
160 ** as this module treats each table as a separate structure. To determine
161 ** the table corresponding to the index being written, this
162 ** function has to search through the database schema.
164 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
165 ** hold a write-lock on the schema table (root page 1). This is also
166 ** acceptable.
168 static int hasSharedCacheTableLock(
169 Btree *pBtree, /* Handle that must hold lock */
170 Pgno iRoot, /* Root page of b-tree */
171 int isIndex, /* True if iRoot is the root of an index b-tree */
172 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */
174 Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
175 Pgno iTab = 0;
176 BtLock *pLock;
178 /* If this database is not shareable, or if the client is reading
179 ** and has the read-uncommitted flag set, then no lock is required.
180 ** Return true immediately.
182 if( (pBtree->sharable==0)
183 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit))
185 return 1;
188 /* If the client is reading or writing an index and the schema is
189 ** not loaded, then it is too difficult to actually check to see if
190 ** the correct locks are held. So do not bother - just return true.
191 ** This case does not come up very often anyhow.
193 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
194 return 1;
197 /* Figure out the root-page that the lock should be held on. For table
198 ** b-trees, this is just the root page of the b-tree being read or
199 ** written. For index b-trees, it is the root page of the associated
200 ** table. */
201 if( isIndex ){
202 HashElem *p;
203 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
204 Index *pIdx = (Index *)sqliteHashData(p);
205 if( pIdx->tnum==(int)iRoot ){
206 if( iTab ){
207 /* Two or more indexes share the same root page. There must
208 ** be imposter tables. So just return true. The assert is not
209 ** useful in that case. */
210 return 1;
212 iTab = pIdx->pTable->tnum;
215 }else{
216 iTab = iRoot;
219 /* Search for the required lock. Either a write-lock on root-page iTab, a
220 ** write-lock on the schema table, or (if the client is reading) a
221 ** read-lock on iTab will suffice. Return 1 if any of these are found. */
222 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
223 if( pLock->pBtree==pBtree
224 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
225 && pLock->eLock>=eLockType
227 return 1;
231 /* Failed to find the required lock. */
232 return 0;
234 #endif /* SQLITE_DEBUG */
236 #ifdef SQLITE_DEBUG
238 **** This function may be used as part of assert() statements only. ****
240 ** Return true if it would be illegal for pBtree to write into the
241 ** table or index rooted at iRoot because other shared connections are
242 ** simultaneously reading that same table or index.
244 ** It is illegal for pBtree to write if some other Btree object that
245 ** shares the same BtShared object is currently reading or writing
246 ** the iRoot table. Except, if the other Btree object has the
247 ** read-uncommitted flag set, then it is OK for the other object to
248 ** have a read cursor.
250 ** For example, before writing to any part of the table or index
251 ** rooted at page iRoot, one should call:
253 ** assert( !hasReadConflicts(pBtree, iRoot) );
255 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
256 BtCursor *p;
257 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
258 if( p->pgnoRoot==iRoot
259 && p->pBtree!=pBtree
260 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit)
262 return 1;
265 return 0;
267 #endif /* #ifdef SQLITE_DEBUG */
270 ** Query to see if Btree handle p may obtain a lock of type eLock
271 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
272 ** SQLITE_OK if the lock may be obtained (by calling
273 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
275 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
276 BtShared *pBt = p->pBt;
277 BtLock *pIter;
279 assert( sqlite3BtreeHoldsMutex(p) );
280 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
281 assert( p->db!=0 );
282 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 );
284 /* If requesting a write-lock, then the Btree must have an open write
285 ** transaction on this file. And, obviously, for this to be so there
286 ** must be an open write transaction on the file itself.
288 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
289 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
291 /* This routine is a no-op if the shared-cache is not enabled */
292 if( !p->sharable ){
293 return SQLITE_OK;
296 /* If some other connection is holding an exclusive lock, the
297 ** requested lock may not be obtained.
299 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
300 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
301 return SQLITE_LOCKED_SHAREDCACHE;
304 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
305 /* The condition (pIter->eLock!=eLock) in the following if(...)
306 ** statement is a simplification of:
308 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
310 ** since we know that if eLock==WRITE_LOCK, then no other connection
311 ** may hold a WRITE_LOCK on any table in this file (since there can
312 ** only be a single writer).
314 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
315 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
316 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
317 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
318 if( eLock==WRITE_LOCK ){
319 assert( p==pBt->pWriter );
320 pBt->btsFlags |= BTS_PENDING;
322 return SQLITE_LOCKED_SHAREDCACHE;
325 return SQLITE_OK;
327 #endif /* !SQLITE_OMIT_SHARED_CACHE */
329 #ifndef SQLITE_OMIT_SHARED_CACHE
331 ** Add a lock on the table with root-page iTable to the shared-btree used
332 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
333 ** WRITE_LOCK.
335 ** This function assumes the following:
337 ** (a) The specified Btree object p is connected to a sharable
338 ** database (one with the BtShared.sharable flag set), and
340 ** (b) No other Btree objects hold a lock that conflicts
341 ** with the requested lock (i.e. querySharedCacheTableLock() has
342 ** already been called and returned SQLITE_OK).
344 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
345 ** is returned if a malloc attempt fails.
347 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
348 BtShared *pBt = p->pBt;
349 BtLock *pLock = 0;
350 BtLock *pIter;
352 assert( sqlite3BtreeHoldsMutex(p) );
353 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
354 assert( p->db!=0 );
356 /* A connection with the read-uncommitted flag set will never try to
357 ** obtain a read-lock using this function. The only read-lock obtained
358 ** by a connection in read-uncommitted mode is on the sqlite_master
359 ** table, and that lock is obtained in BtreeBeginTrans(). */
360 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK );
362 /* This function should only be called on a sharable b-tree after it
363 ** has been determined that no other b-tree holds a conflicting lock. */
364 assert( p->sharable );
365 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
367 /* First search the list for an existing lock on this table. */
368 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
369 if( pIter->iTable==iTable && pIter->pBtree==p ){
370 pLock = pIter;
371 break;
375 /* If the above search did not find a BtLock struct associating Btree p
376 ** with table iTable, allocate one and link it into the list.
378 if( !pLock ){
379 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
380 if( !pLock ){
381 return SQLITE_NOMEM_BKPT;
383 pLock->iTable = iTable;
384 pLock->pBtree = p;
385 pLock->pNext = pBt->pLock;
386 pBt->pLock = pLock;
389 /* Set the BtLock.eLock variable to the maximum of the current lock
390 ** and the requested lock. This means if a write-lock was already held
391 ** and a read-lock requested, we don't incorrectly downgrade the lock.
393 assert( WRITE_LOCK>READ_LOCK );
394 if( eLock>pLock->eLock ){
395 pLock->eLock = eLock;
398 return SQLITE_OK;
400 #endif /* !SQLITE_OMIT_SHARED_CACHE */
402 #ifndef SQLITE_OMIT_SHARED_CACHE
404 ** Release all the table locks (locks obtained via calls to
405 ** the setSharedCacheTableLock() procedure) held by Btree object p.
407 ** This function assumes that Btree p has an open read or write
408 ** transaction. If it does not, then the BTS_PENDING flag
409 ** may be incorrectly cleared.
411 static void clearAllSharedCacheTableLocks(Btree *p){
412 BtShared *pBt = p->pBt;
413 BtLock **ppIter = &pBt->pLock;
415 assert( sqlite3BtreeHoldsMutex(p) );
416 assert( p->sharable || 0==*ppIter );
417 assert( p->inTrans>0 );
419 while( *ppIter ){
420 BtLock *pLock = *ppIter;
421 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
422 assert( pLock->pBtree->inTrans>=pLock->eLock );
423 if( pLock->pBtree==p ){
424 *ppIter = pLock->pNext;
425 assert( pLock->iTable!=1 || pLock==&p->lock );
426 if( pLock->iTable!=1 ){
427 sqlite3_free(pLock);
429 }else{
430 ppIter = &pLock->pNext;
434 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
435 if( pBt->pWriter==p ){
436 pBt->pWriter = 0;
437 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
438 }else if( pBt->nTransaction==2 ){
439 /* This function is called when Btree p is concluding its
440 ** transaction. If there currently exists a writer, and p is not
441 ** that writer, then the number of locks held by connections other
442 ** than the writer must be about to drop to zero. In this case
443 ** set the BTS_PENDING flag to 0.
445 ** If there is not currently a writer, then BTS_PENDING must
446 ** be zero already. So this next line is harmless in that case.
448 pBt->btsFlags &= ~BTS_PENDING;
453 ** This function changes all write-locks held by Btree p into read-locks.
455 static void downgradeAllSharedCacheTableLocks(Btree *p){
456 BtShared *pBt = p->pBt;
457 if( pBt->pWriter==p ){
458 BtLock *pLock;
459 pBt->pWriter = 0;
460 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
461 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
462 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
463 pLock->eLock = READ_LOCK;
468 #endif /* SQLITE_OMIT_SHARED_CACHE */
470 static void releasePage(MemPage *pPage); /* Forward reference */
471 static void releasePageOne(MemPage *pPage); /* Forward reference */
472 static void releasePageNotNull(MemPage *pPage); /* Forward reference */
475 ***** This routine is used inside of assert() only ****
477 ** Verify that the cursor holds the mutex on its BtShared
479 #ifdef SQLITE_DEBUG
480 static int cursorHoldsMutex(BtCursor *p){
481 return sqlite3_mutex_held(p->pBt->mutex);
484 /* Verify that the cursor and the BtShared agree about what is the current
485 ** database connetion. This is important in shared-cache mode. If the database
486 ** connection pointers get out-of-sync, it is possible for routines like
487 ** btreeInitPage() to reference an stale connection pointer that references a
488 ** a connection that has already closed. This routine is used inside assert()
489 ** statements only and for the purpose of double-checking that the btree code
490 ** does keep the database connection pointers up-to-date.
492 static int cursorOwnsBtShared(BtCursor *p){
493 assert( cursorHoldsMutex(p) );
494 return (p->pBtree->db==p->pBt->db);
496 #endif
499 ** Invalidate the overflow cache of the cursor passed as the first argument.
500 ** on the shared btree structure pBt.
502 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl)
505 ** Invalidate the overflow page-list cache for all cursors opened
506 ** on the shared btree structure pBt.
508 static void invalidateAllOverflowCache(BtShared *pBt){
509 BtCursor *p;
510 assert( sqlite3_mutex_held(pBt->mutex) );
511 for(p=pBt->pCursor; p; p=p->pNext){
512 invalidateOverflowCache(p);
516 #ifndef SQLITE_OMIT_INCRBLOB
518 ** This function is called before modifying the contents of a table
519 ** to invalidate any incrblob cursors that are open on the
520 ** row or one of the rows being modified.
522 ** If argument isClearTable is true, then the entire contents of the
523 ** table is about to be deleted. In this case invalidate all incrblob
524 ** cursors open on any row within the table with root-page pgnoRoot.
526 ** Otherwise, if argument isClearTable is false, then the row with
527 ** rowid iRow is being replaced or deleted. In this case invalidate
528 ** only those incrblob cursors open on that specific row.
530 static void invalidateIncrblobCursors(
531 Btree *pBtree, /* The database file to check */
532 Pgno pgnoRoot, /* The table that might be changing */
533 i64 iRow, /* The rowid that might be changing */
534 int isClearTable /* True if all rows are being deleted */
536 BtCursor *p;
537 if( pBtree->hasIncrblobCur==0 ) return;
538 assert( sqlite3BtreeHoldsMutex(pBtree) );
539 pBtree->hasIncrblobCur = 0;
540 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
541 if( (p->curFlags & BTCF_Incrblob)!=0 ){
542 pBtree->hasIncrblobCur = 1;
543 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
544 p->eState = CURSOR_INVALID;
550 #else
551 /* Stub function when INCRBLOB is omitted */
552 #define invalidateIncrblobCursors(w,x,y,z)
553 #endif /* SQLITE_OMIT_INCRBLOB */
556 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
557 ** when a page that previously contained data becomes a free-list leaf
558 ** page.
560 ** The BtShared.pHasContent bitvec exists to work around an obscure
561 ** bug caused by the interaction of two useful IO optimizations surrounding
562 ** free-list leaf pages:
564 ** 1) When all data is deleted from a page and the page becomes
565 ** a free-list leaf page, the page is not written to the database
566 ** (as free-list leaf pages contain no meaningful data). Sometimes
567 ** such a page is not even journalled (as it will not be modified,
568 ** why bother journalling it?).
570 ** 2) When a free-list leaf page is reused, its content is not read
571 ** from the database or written to the journal file (why should it
572 ** be, if it is not at all meaningful?).
574 ** By themselves, these optimizations work fine and provide a handy
575 ** performance boost to bulk delete or insert operations. However, if
576 ** a page is moved to the free-list and then reused within the same
577 ** transaction, a problem comes up. If the page is not journalled when
578 ** it is moved to the free-list and it is also not journalled when it
579 ** is extracted from the free-list and reused, then the original data
580 ** may be lost. In the event of a rollback, it may not be possible
581 ** to restore the database to its original configuration.
583 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
584 ** moved to become a free-list leaf page, the corresponding bit is
585 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
586 ** optimization 2 above is omitted if the corresponding bit is already
587 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
588 ** at the end of every transaction.
590 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
591 int rc = SQLITE_OK;
592 if( !pBt->pHasContent ){
593 assert( pgno<=pBt->nPage );
594 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
595 if( !pBt->pHasContent ){
596 rc = SQLITE_NOMEM_BKPT;
599 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
600 rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
602 return rc;
606 ** Query the BtShared.pHasContent vector.
608 ** This function is called when a free-list leaf page is removed from the
609 ** free-list for reuse. It returns false if it is safe to retrieve the
610 ** page from the pager layer with the 'no-content' flag set. True otherwise.
612 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
613 Bitvec *p = pBt->pHasContent;
614 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
618 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
619 ** invoked at the conclusion of each write-transaction.
621 static void btreeClearHasContent(BtShared *pBt){
622 sqlite3BitvecDestroy(pBt->pHasContent);
623 pBt->pHasContent = 0;
627 ** Release all of the apPage[] pages for a cursor.
629 static void btreeReleaseAllCursorPages(BtCursor *pCur){
630 int i;
631 if( pCur->iPage>=0 ){
632 for(i=0; i<pCur->iPage; i++){
633 releasePageNotNull(pCur->apPage[i]);
635 releasePageNotNull(pCur->pPage);
636 pCur->iPage = -1;
641 ** The cursor passed as the only argument must point to a valid entry
642 ** when this function is called (i.e. have eState==CURSOR_VALID). This
643 ** function saves the current cursor key in variables pCur->nKey and
644 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error
645 ** code otherwise.
647 ** If the cursor is open on an intkey table, then the integer key
648 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to
649 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is
650 ** set to point to a malloced buffer pCur->nKey bytes in size containing
651 ** the key.
653 static int saveCursorKey(BtCursor *pCur){
654 int rc = SQLITE_OK;
655 assert( CURSOR_VALID==pCur->eState );
656 assert( 0==pCur->pKey );
657 assert( cursorHoldsMutex(pCur) );
659 if( pCur->curIntKey ){
660 /* Only the rowid is required for a table btree */
661 pCur->nKey = sqlite3BtreeIntegerKey(pCur);
662 }else{
663 /* For an index btree, save the complete key content */
664 void *pKey;
665 pCur->nKey = sqlite3BtreePayloadSize(pCur);
666 pKey = sqlite3Malloc( pCur->nKey );
667 if( pKey ){
668 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
669 if( rc==SQLITE_OK ){
670 pCur->pKey = pKey;
671 }else{
672 sqlite3_free(pKey);
674 }else{
675 rc = SQLITE_NOMEM_BKPT;
678 assert( !pCur->curIntKey || !pCur->pKey );
679 return rc;
683 ** Save the current cursor position in the variables BtCursor.nKey
684 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
686 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
687 ** prior to calling this routine.
689 static int saveCursorPosition(BtCursor *pCur){
690 int rc;
692 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState );
693 assert( 0==pCur->pKey );
694 assert( cursorHoldsMutex(pCur) );
696 if( pCur->eState==CURSOR_SKIPNEXT ){
697 pCur->eState = CURSOR_VALID;
698 }else{
699 pCur->skipNext = 0;
702 rc = saveCursorKey(pCur);
703 if( rc==SQLITE_OK ){
704 btreeReleaseAllCursorPages(pCur);
705 pCur->eState = CURSOR_REQUIRESEEK;
708 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast);
709 return rc;
712 /* Forward reference */
713 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
716 ** Save the positions of all cursors (except pExcept) that are open on
717 ** the table with root-page iRoot. "Saving the cursor position" means that
718 ** the location in the btree is remembered in such a way that it can be
719 ** moved back to the same spot after the btree has been modified. This
720 ** routine is called just before cursor pExcept is used to modify the
721 ** table, for example in BtreeDelete() or BtreeInsert().
723 ** If there are two or more cursors on the same btree, then all such
724 ** cursors should have their BTCF_Multiple flag set. The btreeCursor()
725 ** routine enforces that rule. This routine only needs to be called in
726 ** the uncommon case when pExpect has the BTCF_Multiple flag set.
728 ** If pExpect!=NULL and if no other cursors are found on the same root-page,
729 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another
730 ** pointless call to this routine.
732 ** Implementation note: This routine merely checks to see if any cursors
733 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual)
734 ** event that cursors are in need to being saved.
736 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
737 BtCursor *p;
738 assert( sqlite3_mutex_held(pBt->mutex) );
739 assert( pExcept==0 || pExcept->pBt==pBt );
740 for(p=pBt->pCursor; p; p=p->pNext){
741 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
743 if( p ) return saveCursorsOnList(p, iRoot, pExcept);
744 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple;
745 return SQLITE_OK;
748 /* This helper routine to saveAllCursors does the actual work of saving
749 ** the cursors if and when a cursor is found that actually requires saving.
750 ** The common case is that no cursors need to be saved, so this routine is
751 ** broken out from its caller to avoid unnecessary stack pointer movement.
753 static int SQLITE_NOINLINE saveCursorsOnList(
754 BtCursor *p, /* The first cursor that needs saving */
755 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */
756 BtCursor *pExcept /* Do not save this cursor */
759 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
760 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
761 int rc = saveCursorPosition(p);
762 if( SQLITE_OK!=rc ){
763 return rc;
765 }else{
766 testcase( p->iPage>=0 );
767 btreeReleaseAllCursorPages(p);
770 p = p->pNext;
771 }while( p );
772 return SQLITE_OK;
776 ** Clear the current cursor position.
778 void sqlite3BtreeClearCursor(BtCursor *pCur){
779 assert( cursorHoldsMutex(pCur) );
780 sqlite3_free(pCur->pKey);
781 pCur->pKey = 0;
782 pCur->eState = CURSOR_INVALID;
786 ** In this version of BtreeMoveto, pKey is a packed index record
787 ** such as is generated by the OP_MakeRecord opcode. Unpack the
788 ** record and then call BtreeMovetoUnpacked() to do the work.
790 static int btreeMoveto(
791 BtCursor *pCur, /* Cursor open on the btree to be searched */
792 const void *pKey, /* Packed key if the btree is an index */
793 i64 nKey, /* Integer key for tables. Size of pKey for indices */
794 int bias, /* Bias search to the high end */
795 int *pRes /* Write search results here */
797 int rc; /* Status code */
798 UnpackedRecord *pIdxKey; /* Unpacked index key */
800 if( pKey ){
801 assert( nKey==(i64)(int)nKey );
802 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo);
803 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT;
804 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
805 if( pIdxKey->nField==0 ){
806 rc = SQLITE_CORRUPT_BKPT;
807 goto moveto_done;
809 }else{
810 pIdxKey = 0;
812 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
813 moveto_done:
814 if( pIdxKey ){
815 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
817 return rc;
821 ** Restore the cursor to the position it was in (or as close to as possible)
822 ** when saveCursorPosition() was called. Note that this call deletes the
823 ** saved position info stored by saveCursorPosition(), so there can be
824 ** at most one effective restoreCursorPosition() call after each
825 ** saveCursorPosition().
827 static int btreeRestoreCursorPosition(BtCursor *pCur){
828 int rc;
829 int skipNext;
830 assert( cursorOwnsBtShared(pCur) );
831 assert( pCur->eState>=CURSOR_REQUIRESEEK );
832 if( pCur->eState==CURSOR_FAULT ){
833 return pCur->skipNext;
835 pCur->eState = CURSOR_INVALID;
836 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
837 if( rc==SQLITE_OK ){
838 sqlite3_free(pCur->pKey);
839 pCur->pKey = 0;
840 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
841 pCur->skipNext |= skipNext;
842 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
843 pCur->eState = CURSOR_SKIPNEXT;
846 return rc;
849 #define restoreCursorPosition(p) \
850 (p->eState>=CURSOR_REQUIRESEEK ? \
851 btreeRestoreCursorPosition(p) : \
852 SQLITE_OK)
855 ** Determine whether or not a cursor has moved from the position where
856 ** it was last placed, or has been invalidated for any other reason.
857 ** Cursors can move when the row they are pointing at is deleted out
858 ** from under them, for example. Cursor might also move if a btree
859 ** is rebalanced.
861 ** Calling this routine with a NULL cursor pointer returns false.
863 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
864 ** back to where it ought to be if this routine returns true.
866 int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
867 assert( EIGHT_BYTE_ALIGNMENT(pCur)
868 || pCur==sqlite3BtreeFakeValidCursor() );
869 assert( offsetof(BtCursor, eState)==0 );
870 assert( sizeof(pCur->eState)==1 );
871 return CURSOR_VALID != *(u8*)pCur;
875 ** Return a pointer to a fake BtCursor object that will always answer
876 ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
877 ** cursor returned must not be used with any other Btree interface.
879 BtCursor *sqlite3BtreeFakeValidCursor(void){
880 static u8 fakeCursor = CURSOR_VALID;
881 assert( offsetof(BtCursor, eState)==0 );
882 return (BtCursor*)&fakeCursor;
886 ** This routine restores a cursor back to its original position after it
887 ** has been moved by some outside activity (such as a btree rebalance or
888 ** a row having been deleted out from under the cursor).
890 ** On success, the *pDifferentRow parameter is false if the cursor is left
891 ** pointing at exactly the same row. *pDifferntRow is the row the cursor
892 ** was pointing to has been deleted, forcing the cursor to point to some
893 ** nearby row.
895 ** This routine should only be called for a cursor that just returned
896 ** TRUE from sqlite3BtreeCursorHasMoved().
898 int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
899 int rc;
901 assert( pCur!=0 );
902 assert( pCur->eState!=CURSOR_VALID );
903 rc = restoreCursorPosition(pCur);
904 if( rc ){
905 *pDifferentRow = 1;
906 return rc;
908 if( pCur->eState!=CURSOR_VALID ){
909 *pDifferentRow = 1;
910 }else{
911 assert( pCur->skipNext==0 );
912 *pDifferentRow = 0;
914 return SQLITE_OK;
917 #ifdef SQLITE_ENABLE_CURSOR_HINTS
919 ** Provide hints to the cursor. The particular hint given (and the type
920 ** and number of the varargs parameters) is determined by the eHintType
921 ** parameter. See the definitions of the BTREE_HINT_* macros for details.
923 void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
924 /* Used only by system that substitute their own storage engine */
926 #endif
929 ** Provide flag hints to the cursor.
931 void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
932 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
933 pCur->hints = x;
937 #ifndef SQLITE_OMIT_AUTOVACUUM
939 ** Given a page number of a regular database page, return the page
940 ** number for the pointer-map page that contains the entry for the
941 ** input page number.
943 ** Return 0 (not a valid page) for pgno==1 since there is
944 ** no pointer map associated with page 1. The integrity_check logic
945 ** requires that ptrmapPageno(*,1)!=1.
947 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
948 int nPagesPerMapPage;
949 Pgno iPtrMap, ret;
950 assert( sqlite3_mutex_held(pBt->mutex) );
951 if( pgno<2 ) return 0;
952 nPagesPerMapPage = (pBt->usableSize/5)+1;
953 iPtrMap = (pgno-2)/nPagesPerMapPage;
954 ret = (iPtrMap*nPagesPerMapPage) + 2;
955 if( ret==PENDING_BYTE_PAGE(pBt) ){
956 ret++;
958 return ret;
962 ** Write an entry into the pointer map.
964 ** This routine updates the pointer map entry for page number 'key'
965 ** so that it maps to type 'eType' and parent page number 'pgno'.
967 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
968 ** a no-op. If an error occurs, the appropriate error code is written
969 ** into *pRC.
971 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
972 DbPage *pDbPage; /* The pointer map page */
973 u8 *pPtrmap; /* The pointer map data */
974 Pgno iPtrmap; /* The pointer map page number */
975 int offset; /* Offset in pointer map page */
976 int rc; /* Return code from subfunctions */
978 if( *pRC ) return;
980 assert( sqlite3_mutex_held(pBt->mutex) );
981 /* The master-journal page number must never be used as a pointer map page */
982 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
984 assert( pBt->autoVacuum );
985 if( key==0 ){
986 *pRC = SQLITE_CORRUPT_BKPT;
987 return;
989 iPtrmap = PTRMAP_PAGENO(pBt, key);
990 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
991 if( rc!=SQLITE_OK ){
992 *pRC = rc;
993 return;
995 offset = PTRMAP_PTROFFSET(iPtrmap, key);
996 if( offset<0 ){
997 *pRC = SQLITE_CORRUPT_BKPT;
998 goto ptrmap_exit;
1000 assert( offset <= (int)pBt->usableSize-5 );
1001 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1003 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
1004 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
1005 *pRC= rc = sqlite3PagerWrite(pDbPage);
1006 if( rc==SQLITE_OK ){
1007 pPtrmap[offset] = eType;
1008 put4byte(&pPtrmap[offset+1], parent);
1012 ptrmap_exit:
1013 sqlite3PagerUnref(pDbPage);
1017 ** Read an entry from the pointer map.
1019 ** This routine retrieves the pointer map entry for page 'key', writing
1020 ** the type and parent page number to *pEType and *pPgno respectively.
1021 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
1023 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
1024 DbPage *pDbPage; /* The pointer map page */
1025 int iPtrmap; /* Pointer map page index */
1026 u8 *pPtrmap; /* Pointer map page data */
1027 int offset; /* Offset of entry in pointer map */
1028 int rc;
1030 assert( sqlite3_mutex_held(pBt->mutex) );
1032 iPtrmap = PTRMAP_PAGENO(pBt, key);
1033 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
1034 if( rc!=0 ){
1035 return rc;
1037 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1039 offset = PTRMAP_PTROFFSET(iPtrmap, key);
1040 if( offset<0 ){
1041 sqlite3PagerUnref(pDbPage);
1042 return SQLITE_CORRUPT_BKPT;
1044 assert( offset <= (int)pBt->usableSize-5 );
1045 assert( pEType!=0 );
1046 *pEType = pPtrmap[offset];
1047 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
1049 sqlite3PagerUnref(pDbPage);
1050 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
1051 return SQLITE_OK;
1054 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
1055 #define ptrmapPut(w,x,y,z,rc)
1056 #define ptrmapGet(w,x,y,z) SQLITE_OK
1057 #define ptrmapPutOvflPtr(x, y, rc)
1058 #endif
1061 ** Given a btree page and a cell index (0 means the first cell on
1062 ** the page, 1 means the second cell, and so forth) return a pointer
1063 ** to the cell content.
1065 ** findCellPastPtr() does the same except it skips past the initial
1066 ** 4-byte child pointer found on interior pages, if there is one.
1068 ** This routine works only for pages that do not contain overflow cells.
1070 #define findCell(P,I) \
1071 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1072 #define findCellPastPtr(P,I) \
1073 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1077 ** This is common tail processing for btreeParseCellPtr() and
1078 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
1079 ** on a single B-tree page. Make necessary adjustments to the CellInfo
1080 ** structure.
1082 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
1083 MemPage *pPage, /* Page containing the cell */
1084 u8 *pCell, /* Pointer to the cell text. */
1085 CellInfo *pInfo /* Fill in this structure */
1087 /* If the payload will not fit completely on the local page, we have
1088 ** to decide how much to store locally and how much to spill onto
1089 ** overflow pages. The strategy is to minimize the amount of unused
1090 ** space on overflow pages while keeping the amount of local storage
1091 ** in between minLocal and maxLocal.
1093 ** Warning: changing the way overflow payload is distributed in any
1094 ** way will result in an incompatible file format.
1096 int minLocal; /* Minimum amount of payload held locally */
1097 int maxLocal; /* Maximum amount of payload held locally */
1098 int surplus; /* Overflow payload available for local storage */
1100 minLocal = pPage->minLocal;
1101 maxLocal = pPage->maxLocal;
1102 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
1103 testcase( surplus==maxLocal );
1104 testcase( surplus==maxLocal+1 );
1105 if( surplus <= maxLocal ){
1106 pInfo->nLocal = (u16)surplus;
1107 }else{
1108 pInfo->nLocal = (u16)minLocal;
1110 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
1114 ** The following routines are implementations of the MemPage.xParseCell()
1115 ** method.
1117 ** Parse a cell content block and fill in the CellInfo structure.
1119 ** btreeParseCellPtr() => table btree leaf nodes
1120 ** btreeParseCellNoPayload() => table btree internal nodes
1121 ** btreeParseCellPtrIndex() => index btree nodes
1123 ** There is also a wrapper function btreeParseCell() that works for
1124 ** all MemPage types and that references the cell by index rather than
1125 ** by pointer.
1127 static void btreeParseCellPtrNoPayload(
1128 MemPage *pPage, /* Page containing the cell */
1129 u8 *pCell, /* Pointer to the cell text. */
1130 CellInfo *pInfo /* Fill in this structure */
1132 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1133 assert( pPage->leaf==0 );
1134 assert( pPage->childPtrSize==4 );
1135 #ifndef SQLITE_DEBUG
1136 UNUSED_PARAMETER(pPage);
1137 #endif
1138 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
1139 pInfo->nPayload = 0;
1140 pInfo->nLocal = 0;
1141 pInfo->pPayload = 0;
1142 return;
1144 static void btreeParseCellPtr(
1145 MemPage *pPage, /* Page containing the cell */
1146 u8 *pCell, /* Pointer to the cell text. */
1147 CellInfo *pInfo /* Fill in this structure */
1149 u8 *pIter; /* For scanning through pCell */
1150 u32 nPayload; /* Number of bytes of cell payload */
1151 u64 iKey; /* Extracted Key value */
1153 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1154 assert( pPage->leaf==0 || pPage->leaf==1 );
1155 assert( pPage->intKeyLeaf );
1156 assert( pPage->childPtrSize==0 );
1157 pIter = pCell;
1159 /* The next block of code is equivalent to:
1161 ** pIter += getVarint32(pIter, nPayload);
1163 ** The code is inlined to avoid a function call.
1165 nPayload = *pIter;
1166 if( nPayload>=0x80 ){
1167 u8 *pEnd = &pIter[8];
1168 nPayload &= 0x7f;
1170 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1171 }while( (*pIter)>=0x80 && pIter<pEnd );
1173 pIter++;
1175 /* The next block of code is equivalent to:
1177 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
1179 ** The code is inlined to avoid a function call.
1181 iKey = *pIter;
1182 if( iKey>=0x80 ){
1183 u8 *pEnd = &pIter[7];
1184 iKey &= 0x7f;
1185 while(1){
1186 iKey = (iKey<<7) | (*++pIter & 0x7f);
1187 if( (*pIter)<0x80 ) break;
1188 if( pIter>=pEnd ){
1189 iKey = (iKey<<8) | *++pIter;
1190 break;
1194 pIter++;
1196 pInfo->nKey = *(i64*)&iKey;
1197 pInfo->nPayload = nPayload;
1198 pInfo->pPayload = pIter;
1199 testcase( nPayload==pPage->maxLocal );
1200 testcase( nPayload==pPage->maxLocal+1 );
1201 if( nPayload<=pPage->maxLocal ){
1202 /* This is the (easy) common case where the entire payload fits
1203 ** on the local page. No overflow is required.
1205 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1206 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1207 pInfo->nLocal = (u16)nPayload;
1208 }else{
1209 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1212 static void btreeParseCellPtrIndex(
1213 MemPage *pPage, /* Page containing the cell */
1214 u8 *pCell, /* Pointer to the cell text. */
1215 CellInfo *pInfo /* Fill in this structure */
1217 u8 *pIter; /* For scanning through pCell */
1218 u32 nPayload; /* Number of bytes of cell payload */
1220 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1221 assert( pPage->leaf==0 || pPage->leaf==1 );
1222 assert( pPage->intKeyLeaf==0 );
1223 pIter = pCell + pPage->childPtrSize;
1224 nPayload = *pIter;
1225 if( nPayload>=0x80 ){
1226 u8 *pEnd = &pIter[8];
1227 nPayload &= 0x7f;
1229 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1230 }while( *(pIter)>=0x80 && pIter<pEnd );
1232 pIter++;
1233 pInfo->nKey = nPayload;
1234 pInfo->nPayload = nPayload;
1235 pInfo->pPayload = pIter;
1236 testcase( nPayload==pPage->maxLocal );
1237 testcase( nPayload==pPage->maxLocal+1 );
1238 if( nPayload<=pPage->maxLocal ){
1239 /* This is the (easy) common case where the entire payload fits
1240 ** on the local page. No overflow is required.
1242 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1243 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1244 pInfo->nLocal = (u16)nPayload;
1245 }else{
1246 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1249 static void btreeParseCell(
1250 MemPage *pPage, /* Page containing the cell */
1251 int iCell, /* The cell index. First cell is 0 */
1252 CellInfo *pInfo /* Fill in this structure */
1254 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
1258 ** The following routines are implementations of the MemPage.xCellSize
1259 ** method.
1261 ** Compute the total number of bytes that a Cell needs in the cell
1262 ** data area of the btree-page. The return number includes the cell
1263 ** data header and the local payload, but not any overflow page or
1264 ** the space used by the cell pointer.
1266 ** cellSizePtrNoPayload() => table internal nodes
1267 ** cellSizePtr() => all index nodes & table leaf nodes
1269 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
1270 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
1271 u8 *pEnd; /* End mark for a varint */
1272 u32 nSize; /* Size value to return */
1274 #ifdef SQLITE_DEBUG
1275 /* The value returned by this function should always be the same as
1276 ** the (CellInfo.nSize) value found by doing a full parse of the
1277 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1278 ** this function verifies that this invariant is not violated. */
1279 CellInfo debuginfo;
1280 pPage->xParseCell(pPage, pCell, &debuginfo);
1281 #endif
1283 nSize = *pIter;
1284 if( nSize>=0x80 ){
1285 pEnd = &pIter[8];
1286 nSize &= 0x7f;
1288 nSize = (nSize<<7) | (*++pIter & 0x7f);
1289 }while( *(pIter)>=0x80 && pIter<pEnd );
1291 pIter++;
1292 if( pPage->intKey ){
1293 /* pIter now points at the 64-bit integer key value, a variable length
1294 ** integer. The following block moves pIter to point at the first byte
1295 ** past the end of the key value. */
1296 pEnd = &pIter[9];
1297 while( (*pIter++)&0x80 && pIter<pEnd );
1299 testcase( nSize==pPage->maxLocal );
1300 testcase( nSize==pPage->maxLocal+1 );
1301 if( nSize<=pPage->maxLocal ){
1302 nSize += (u32)(pIter - pCell);
1303 if( nSize<4 ) nSize = 4;
1304 }else{
1305 int minLocal = pPage->minLocal;
1306 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1307 testcase( nSize==pPage->maxLocal );
1308 testcase( nSize==pPage->maxLocal+1 );
1309 if( nSize>pPage->maxLocal ){
1310 nSize = minLocal;
1312 nSize += 4 + (u16)(pIter - pCell);
1314 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1315 return (u16)nSize;
1317 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
1318 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1319 u8 *pEnd; /* End mark for a varint */
1321 #ifdef SQLITE_DEBUG
1322 /* The value returned by this function should always be the same as
1323 ** the (CellInfo.nSize) value found by doing a full parse of the
1324 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1325 ** this function verifies that this invariant is not violated. */
1326 CellInfo debuginfo;
1327 pPage->xParseCell(pPage, pCell, &debuginfo);
1328 #else
1329 UNUSED_PARAMETER(pPage);
1330 #endif
1332 assert( pPage->childPtrSize==4 );
1333 pEnd = pIter + 9;
1334 while( (*pIter++)&0x80 && pIter<pEnd );
1335 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
1336 return (u16)(pIter - pCell);
1340 #ifdef SQLITE_DEBUG
1341 /* This variation on cellSizePtr() is used inside of assert() statements
1342 ** only. */
1343 static u16 cellSize(MemPage *pPage, int iCell){
1344 return pPage->xCellSize(pPage, findCell(pPage, iCell));
1346 #endif
1348 #ifndef SQLITE_OMIT_AUTOVACUUM
1350 ** If the cell pCell, part of page pPage contains a pointer
1351 ** to an overflow page, insert an entry into the pointer-map
1352 ** for the overflow page.
1354 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
1355 CellInfo info;
1356 if( *pRC ) return;
1357 assert( pCell!=0 );
1358 pPage->xParseCell(pPage, pCell, &info);
1359 if( info.nLocal<info.nPayload ){
1360 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
1361 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
1364 #endif
1368 ** Defragment the page given. This routine reorganizes cells within the
1369 ** page so that there are no free-blocks on the free-block list.
1371 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be
1372 ** present in the page after this routine returns.
1374 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
1375 ** b-tree page so that there are no freeblocks or fragment bytes, all
1376 ** unused bytes are contained in the unallocated space region, and all
1377 ** cells are packed tightly at the end of the page.
1379 static int defragmentPage(MemPage *pPage, int nMaxFrag){
1380 int i; /* Loop counter */
1381 int pc; /* Address of the i-th cell */
1382 int hdr; /* Offset to the page header */
1383 int size; /* Size of a cell */
1384 int usableSize; /* Number of usable bytes on a page */
1385 int cellOffset; /* Offset to the cell pointer array */
1386 int cbrk; /* Offset to the cell content area */
1387 int nCell; /* Number of cells on the page */
1388 unsigned char *data; /* The page data */
1389 unsigned char *temp; /* Temp area for cell content */
1390 unsigned char *src; /* Source of content */
1391 int iCellFirst; /* First allowable cell index */
1392 int iCellLast; /* Last possible cell index */
1394 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1395 assert( pPage->pBt!=0 );
1396 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1397 assert( pPage->nOverflow==0 );
1398 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1399 temp = 0;
1400 src = data = pPage->aData;
1401 hdr = pPage->hdrOffset;
1402 cellOffset = pPage->cellOffset;
1403 nCell = pPage->nCell;
1404 assert( nCell==get2byte(&data[hdr+3]) );
1405 iCellFirst = cellOffset + 2*nCell;
1406 usableSize = pPage->pBt->usableSize;
1408 /* This block handles pages with two or fewer free blocks and nMaxFrag
1409 ** or fewer fragmented bytes. In this case it is faster to move the
1410 ** two (or one) blocks of cells using memmove() and add the required
1411 ** offsets to each pointer in the cell-pointer array than it is to
1412 ** reconstruct the entire page. */
1413 if( (int)data[hdr+7]<=nMaxFrag ){
1414 int iFree = get2byte(&data[hdr+1]);
1415 if( iFree ){
1416 int iFree2 = get2byte(&data[iFree]);
1418 /* pageFindSlot() has already verified that free blocks are sorted
1419 ** in order of offset within the page, and that no block extends
1420 ** past the end of the page. Provided the two free slots do not
1421 ** overlap, this guarantees that the memmove() calls below will not
1422 ** overwrite the usableSize byte buffer, even if the database page
1423 ** is corrupt. */
1424 assert( iFree2==0 || iFree2>iFree );
1425 assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
1426 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
1428 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
1429 u8 *pEnd = &data[cellOffset + nCell*2];
1430 u8 *pAddr;
1431 int sz2 = 0;
1432 int sz = get2byte(&data[iFree+2]);
1433 int top = get2byte(&data[hdr+5]);
1434 if( top>=iFree ){
1435 return SQLITE_CORRUPT_PAGE(pPage);
1437 if( iFree2 ){
1438 assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
1439 sz2 = get2byte(&data[iFree2+2]);
1440 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
1441 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
1442 sz += sz2;
1444 cbrk = top+sz;
1445 assert( cbrk+(iFree-top) <= usableSize );
1446 memmove(&data[cbrk], &data[top], iFree-top);
1447 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
1448 pc = get2byte(pAddr);
1449 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
1450 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
1452 goto defragment_out;
1457 cbrk = usableSize;
1458 iCellLast = usableSize - 4;
1459 for(i=0; i<nCell; i++){
1460 u8 *pAddr; /* The i-th cell pointer */
1461 pAddr = &data[cellOffset + i*2];
1462 pc = get2byte(pAddr);
1463 testcase( pc==iCellFirst );
1464 testcase( pc==iCellLast );
1465 /* These conditions have already been verified in btreeInitPage()
1466 ** if PRAGMA cell_size_check=ON.
1468 if( pc<iCellFirst || pc>iCellLast ){
1469 return SQLITE_CORRUPT_PAGE(pPage);
1471 assert( pc>=iCellFirst && pc<=iCellLast );
1472 size = pPage->xCellSize(pPage, &src[pc]);
1473 cbrk -= size;
1474 if( cbrk<iCellFirst || pc+size>usableSize ){
1475 return SQLITE_CORRUPT_PAGE(pPage);
1477 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
1478 testcase( cbrk+size==usableSize );
1479 testcase( pc+size==usableSize );
1480 put2byte(pAddr, cbrk);
1481 if( temp==0 ){
1482 int x;
1483 if( cbrk==pc ) continue;
1484 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
1485 x = get2byte(&data[hdr+5]);
1486 memcpy(&temp[x], &data[x], (cbrk+size) - x);
1487 src = temp;
1489 memcpy(&data[cbrk], &src[pc], size);
1491 data[hdr+7] = 0;
1493 defragment_out:
1494 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
1495 return SQLITE_CORRUPT_PAGE(pPage);
1497 assert( cbrk>=iCellFirst );
1498 put2byte(&data[hdr+5], cbrk);
1499 data[hdr+1] = 0;
1500 data[hdr+2] = 0;
1501 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
1502 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1503 return SQLITE_OK;
1507 ** Search the free-list on page pPg for space to store a cell nByte bytes in
1508 ** size. If one can be found, return a pointer to the space and remove it
1509 ** from the free-list.
1511 ** If no suitable space can be found on the free-list, return NULL.
1513 ** This function may detect corruption within pPg. If corruption is
1514 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
1516 ** Slots on the free list that are between 1 and 3 bytes larger than nByte
1517 ** will be ignored if adding the extra space to the fragmentation count
1518 ** causes the fragmentation count to exceed 60.
1520 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
1521 const int hdr = pPg->hdrOffset;
1522 u8 * const aData = pPg->aData;
1523 int iAddr = hdr + 1;
1524 int pc = get2byte(&aData[iAddr]);
1525 int x;
1526 int usableSize = pPg->pBt->usableSize;
1527 int size; /* Size of the free slot */
1529 assert( pc>0 );
1530 while( pc<=usableSize-4 ){
1531 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
1532 ** freeblock form a big-endian integer which is the size of the freeblock
1533 ** in bytes, including the 4-byte header. */
1534 size = get2byte(&aData[pc+2]);
1535 if( (x = size - nByte)>=0 ){
1536 testcase( x==4 );
1537 testcase( x==3 );
1538 if( size+pc > usableSize ){
1539 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1540 return 0;
1541 }else if( x<4 ){
1542 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
1543 ** number of bytes in fragments may not exceed 60. */
1544 if( aData[hdr+7]>57 ) return 0;
1546 /* Remove the slot from the free-list. Update the number of
1547 ** fragmented bytes within the page. */
1548 memcpy(&aData[iAddr], &aData[pc], 2);
1549 aData[hdr+7] += (u8)x;
1550 }else{
1551 /* The slot remains on the free-list. Reduce its size to account
1552 ** for the portion used by the new allocation. */
1553 put2byte(&aData[pc+2], x);
1555 return &aData[pc + x];
1557 iAddr = pc;
1558 pc = get2byte(&aData[pc]);
1559 if( pc<iAddr+size ) break;
1561 if( pc ){
1562 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1565 return 0;
1569 ** Allocate nByte bytes of space from within the B-Tree page passed
1570 ** as the first argument. Write into *pIdx the index into pPage->aData[]
1571 ** of the first byte of allocated space. Return either SQLITE_OK or
1572 ** an error code (usually SQLITE_CORRUPT).
1574 ** The caller guarantees that there is sufficient space to make the
1575 ** allocation. This routine might need to defragment in order to bring
1576 ** all the space together, however. This routine will avoid using
1577 ** the first two bytes past the cell pointer area since presumably this
1578 ** allocation is being made in order to insert a new cell, so we will
1579 ** also end up needing a new cell pointer.
1581 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
1582 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1583 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
1584 int top; /* First byte of cell content area */
1585 int rc = SQLITE_OK; /* Integer return code */
1586 int gap; /* First byte of gap between cell pointers and cell content */
1588 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1589 assert( pPage->pBt );
1590 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1591 assert( nByte>=0 ); /* Minimum cell size is 4 */
1592 assert( pPage->nFree>=nByte );
1593 assert( pPage->nOverflow==0 );
1594 assert( nByte < (int)(pPage->pBt->usableSize-8) );
1596 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1597 gap = pPage->cellOffset + 2*pPage->nCell;
1598 assert( gap<=65536 );
1599 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
1600 ** and the reserved space is zero (the usual value for reserved space)
1601 ** then the cell content offset of an empty page wants to be 65536.
1602 ** However, that integer is too large to be stored in a 2-byte unsigned
1603 ** integer, so a value of 0 is used in its place. */
1604 top = get2byte(&data[hdr+5]);
1605 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
1606 if( gap>top ){
1607 if( top==0 && pPage->pBt->usableSize==65536 ){
1608 top = 65536;
1609 }else{
1610 return SQLITE_CORRUPT_PAGE(pPage);
1614 /* If there is enough space between gap and top for one more cell pointer
1615 ** array entry offset, and if the freelist is not empty, then search the
1616 ** freelist looking for a free slot big enough to satisfy the request.
1618 testcase( gap+2==top );
1619 testcase( gap+1==top );
1620 testcase( gap==top );
1621 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
1622 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
1623 if( pSpace ){
1624 assert( pSpace>=data && (pSpace - data)<65536 );
1625 *pIdx = (int)(pSpace - data);
1626 return SQLITE_OK;
1627 }else if( rc ){
1628 return rc;
1632 /* The request could not be fulfilled using a freelist slot. Check
1633 ** to see if defragmentation is necessary.
1635 testcase( gap+2+nByte==top );
1636 if( gap+2+nByte>top ){
1637 assert( pPage->nCell>0 || CORRUPT_DB );
1638 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
1639 if( rc ) return rc;
1640 top = get2byteNotZero(&data[hdr+5]);
1641 assert( gap+2+nByte<=top );
1645 /* Allocate memory from the gap in between the cell pointer array
1646 ** and the cell content area. The btreeInitPage() call has already
1647 ** validated the freelist. Given that the freelist is valid, there
1648 ** is no way that the allocation can extend off the end of the page.
1649 ** The assert() below verifies the previous sentence.
1651 top -= nByte;
1652 put2byte(&data[hdr+5], top);
1653 assert( top+nByte <= (int)pPage->pBt->usableSize );
1654 *pIdx = top;
1655 return SQLITE_OK;
1659 ** Return a section of the pPage->aData to the freelist.
1660 ** The first byte of the new free block is pPage->aData[iStart]
1661 ** and the size of the block is iSize bytes.
1663 ** Adjacent freeblocks are coalesced.
1665 ** Note that even though the freeblock list was checked by btreeInitPage(),
1666 ** that routine will not detect overlap between cells or freeblocks. Nor
1667 ** does it detect cells or freeblocks that encrouch into the reserved bytes
1668 ** at the end of the page. So do additional corruption checks inside this
1669 ** routine and return SQLITE_CORRUPT if any problems are found.
1671 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
1672 u16 iPtr; /* Address of ptr to next freeblock */
1673 u16 iFreeBlk; /* Address of the next freeblock */
1674 u8 hdr; /* Page header size. 0 or 100 */
1675 u8 nFrag = 0; /* Reduction in fragmentation */
1676 u16 iOrigSize = iSize; /* Original value of iSize */
1677 u16 x; /* Offset to cell content area */
1678 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
1679 unsigned char *data = pPage->aData; /* Page content */
1681 assert( pPage->pBt!=0 );
1682 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1683 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
1684 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
1685 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1686 assert( iSize>=4 ); /* Minimum cell size is 4 */
1687 assert( iStart<=pPage->pBt->usableSize-4 );
1689 /* The list of freeblocks must be in ascending order. Find the
1690 ** spot on the list where iStart should be inserted.
1692 hdr = pPage->hdrOffset;
1693 iPtr = hdr + 1;
1694 if( data[iPtr+1]==0 && data[iPtr]==0 ){
1695 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
1696 }else{
1697 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
1698 if( iFreeBlk<iPtr+4 ){
1699 if( iFreeBlk==0 ) break;
1700 return SQLITE_CORRUPT_PAGE(pPage);
1702 iPtr = iFreeBlk;
1704 if( iFreeBlk>pPage->pBt->usableSize-4 ){
1705 return SQLITE_CORRUPT_PAGE(pPage);
1707 assert( iFreeBlk>iPtr || iFreeBlk==0 );
1709 /* At this point:
1710 ** iFreeBlk: First freeblock after iStart, or zero if none
1711 ** iPtr: The address of a pointer to iFreeBlk
1713 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
1715 if( iFreeBlk && iEnd+3>=iFreeBlk ){
1716 nFrag = iFreeBlk - iEnd;
1717 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
1718 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
1719 if( iEnd > pPage->pBt->usableSize ){
1720 return SQLITE_CORRUPT_PAGE(pPage);
1722 iSize = iEnd - iStart;
1723 iFreeBlk = get2byte(&data[iFreeBlk]);
1726 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
1727 ** pointer in the page header) then check to see if iStart should be
1728 ** coalesced onto the end of iPtr.
1730 if( iPtr>hdr+1 ){
1731 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
1732 if( iPtrEnd+3>=iStart ){
1733 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
1734 nFrag += iStart - iPtrEnd;
1735 iSize = iEnd - iPtr;
1736 iStart = iPtr;
1739 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
1740 data[hdr+7] -= nFrag;
1742 x = get2byte(&data[hdr+5]);
1743 if( iStart<=x ){
1744 /* The new freeblock is at the beginning of the cell content area,
1745 ** so just extend the cell content area rather than create another
1746 ** freelist entry */
1747 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
1748 put2byte(&data[hdr+1], iFreeBlk);
1749 put2byte(&data[hdr+5], iEnd);
1750 }else{
1751 /* Insert the new freeblock into the freelist */
1752 put2byte(&data[iPtr], iStart);
1754 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
1755 /* Overwrite deleted information with zeros when the secure_delete
1756 ** option is enabled */
1757 memset(&data[iStart], 0, iSize);
1759 put2byte(&data[iStart], iFreeBlk);
1760 put2byte(&data[iStart+2], iSize);
1761 pPage->nFree += iOrigSize;
1762 return SQLITE_OK;
1766 ** Decode the flags byte (the first byte of the header) for a page
1767 ** and initialize fields of the MemPage structure accordingly.
1769 ** Only the following combinations are supported. Anything different
1770 ** indicates a corrupt database files:
1772 ** PTF_ZERODATA
1773 ** PTF_ZERODATA | PTF_LEAF
1774 ** PTF_LEAFDATA | PTF_INTKEY
1775 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
1777 static int decodeFlags(MemPage *pPage, int flagByte){
1778 BtShared *pBt; /* A copy of pPage->pBt */
1780 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1781 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1782 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
1783 flagByte &= ~PTF_LEAF;
1784 pPage->childPtrSize = 4-4*pPage->leaf;
1785 pPage->xCellSize = cellSizePtr;
1786 pBt = pPage->pBt;
1787 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
1788 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
1789 ** interior table b-tree page. */
1790 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
1791 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
1792 ** leaf table b-tree page. */
1793 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
1794 pPage->intKey = 1;
1795 if( pPage->leaf ){
1796 pPage->intKeyLeaf = 1;
1797 pPage->xParseCell = btreeParseCellPtr;
1798 }else{
1799 pPage->intKeyLeaf = 0;
1800 pPage->xCellSize = cellSizePtrNoPayload;
1801 pPage->xParseCell = btreeParseCellPtrNoPayload;
1803 pPage->maxLocal = pBt->maxLeaf;
1804 pPage->minLocal = pBt->minLeaf;
1805 }else if( flagByte==PTF_ZERODATA ){
1806 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
1807 ** interior index b-tree page. */
1808 assert( (PTF_ZERODATA)==2 );
1809 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
1810 ** leaf index b-tree page. */
1811 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
1812 pPage->intKey = 0;
1813 pPage->intKeyLeaf = 0;
1814 pPage->xParseCell = btreeParseCellPtrIndex;
1815 pPage->maxLocal = pBt->maxLocal;
1816 pPage->minLocal = pBt->minLocal;
1817 }else{
1818 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
1819 ** an error. */
1820 return SQLITE_CORRUPT_PAGE(pPage);
1822 pPage->max1bytePayload = pBt->max1bytePayload;
1823 return SQLITE_OK;
1827 ** Initialize the auxiliary information for a disk block.
1829 ** Return SQLITE_OK on success. If we see that the page does
1830 ** not contain a well-formed database page, then return
1831 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1832 ** guarantee that the page is well-formed. It only shows that
1833 ** we failed to detect any corruption.
1835 static int btreeInitPage(MemPage *pPage){
1836 int pc; /* Address of a freeblock within pPage->aData[] */
1837 u8 hdr; /* Offset to beginning of page header */
1838 u8 *data; /* Equal to pPage->aData */
1839 BtShared *pBt; /* The main btree structure */
1840 int usableSize; /* Amount of usable space on each page */
1841 u16 cellOffset; /* Offset from start of page to first cell pointer */
1842 int nFree; /* Number of unused bytes on the page */
1843 int top; /* First byte of the cell content area */
1844 int iCellFirst; /* First allowable cell or freeblock offset */
1845 int iCellLast; /* Last possible cell or freeblock offset */
1847 assert( pPage->pBt!=0 );
1848 assert( pPage->pBt->db!=0 );
1849 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1850 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1851 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1852 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
1853 assert( pPage->isInit==0 );
1855 pBt = pPage->pBt;
1856 hdr = pPage->hdrOffset;
1857 data = pPage->aData;
1858 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
1859 ** the b-tree page type. */
1860 if( decodeFlags(pPage, data[hdr]) ){
1861 return SQLITE_CORRUPT_PAGE(pPage);
1863 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
1864 pPage->maskPage = (u16)(pBt->pageSize - 1);
1865 pPage->nOverflow = 0;
1866 usableSize = pBt->usableSize;
1867 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
1868 pPage->aDataEnd = &data[usableSize];
1869 pPage->aCellIdx = &data[cellOffset];
1870 pPage->aDataOfst = &data[pPage->childPtrSize];
1871 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
1872 ** the start of the cell content area. A zero value for this integer is
1873 ** interpreted as 65536. */
1874 top = get2byteNotZero(&data[hdr+5]);
1875 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
1876 ** number of cells on the page. */
1877 pPage->nCell = get2byte(&data[hdr+3]);
1878 if( pPage->nCell>MX_CELL(pBt) ){
1879 /* To many cells for a single page. The page must be corrupt */
1880 return SQLITE_CORRUPT_PAGE(pPage);
1882 testcase( pPage->nCell==MX_CELL(pBt) );
1883 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
1884 ** possible for a root page of a table that contains no rows) then the
1885 ** offset to the cell content area will equal the page size minus the
1886 ** bytes of reserved space. */
1887 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
1889 /* A malformed database page might cause us to read past the end
1890 ** of page when parsing a cell.
1892 ** The following block of code checks early to see if a cell extends
1893 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
1894 ** returned if it does.
1896 iCellFirst = cellOffset + 2*pPage->nCell;
1897 iCellLast = usableSize - 4;
1898 if( pBt->db->flags & SQLITE_CellSizeCk ){
1899 int i; /* Index into the cell pointer array */
1900 int sz; /* Size of a cell */
1902 if( !pPage->leaf ) iCellLast--;
1903 for(i=0; i<pPage->nCell; i++){
1904 pc = get2byteAligned(&data[cellOffset+i*2]);
1905 testcase( pc==iCellFirst );
1906 testcase( pc==iCellLast );
1907 if( pc<iCellFirst || pc>iCellLast ){
1908 return SQLITE_CORRUPT_PAGE(pPage);
1910 sz = pPage->xCellSize(pPage, &data[pc]);
1911 testcase( pc+sz==usableSize );
1912 if( pc+sz>usableSize ){
1913 return SQLITE_CORRUPT_PAGE(pPage);
1916 if( !pPage->leaf ) iCellLast++;
1919 /* Compute the total free space on the page
1920 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
1921 ** start of the first freeblock on the page, or is zero if there are no
1922 ** freeblocks. */
1923 pc = get2byte(&data[hdr+1]);
1924 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
1925 if( pc>0 ){
1926 u32 next, size;
1927 if( pc<iCellFirst ){
1928 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
1929 ** always be at least one cell before the first freeblock.
1931 return SQLITE_CORRUPT_PAGE(pPage);
1933 while( 1 ){
1934 if( pc>iCellLast ){
1935 /* Freeblock off the end of the page */
1936 return SQLITE_CORRUPT_PAGE(pPage);
1938 next = get2byte(&data[pc]);
1939 size = get2byte(&data[pc+2]);
1940 nFree = nFree + size;
1941 if( next<=pc+size+3 ) break;
1942 pc = next;
1944 if( next>0 ){
1945 /* Freeblock not in ascending order */
1946 return SQLITE_CORRUPT_PAGE(pPage);
1948 if( pc+size>(unsigned int)usableSize ){
1949 /* Last freeblock extends past page end */
1950 return SQLITE_CORRUPT_PAGE(pPage);
1954 /* At this point, nFree contains the sum of the offset to the start
1955 ** of the cell-content area plus the number of free bytes within
1956 ** the cell-content area. If this is greater than the usable-size
1957 ** of the page, then the page must be corrupted. This check also
1958 ** serves to verify that the offset to the start of the cell-content
1959 ** area, according to the page header, lies within the page.
1961 if( nFree>usableSize ){
1962 return SQLITE_CORRUPT_PAGE(pPage);
1964 pPage->nFree = (u16)(nFree - iCellFirst);
1965 pPage->isInit = 1;
1966 return SQLITE_OK;
1970 ** Set up a raw page so that it looks like a database page holding
1971 ** no entries.
1973 static void zeroPage(MemPage *pPage, int flags){
1974 unsigned char *data = pPage->aData;
1975 BtShared *pBt = pPage->pBt;
1976 u8 hdr = pPage->hdrOffset;
1977 u16 first;
1979 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
1980 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1981 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
1982 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1983 assert( sqlite3_mutex_held(pBt->mutex) );
1984 if( pBt->btsFlags & BTS_FAST_SECURE ){
1985 memset(&data[hdr], 0, pBt->usableSize - hdr);
1987 data[hdr] = (char)flags;
1988 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
1989 memset(&data[hdr+1], 0, 4);
1990 data[hdr+7] = 0;
1991 put2byte(&data[hdr+5], pBt->usableSize);
1992 pPage->nFree = (u16)(pBt->usableSize - first);
1993 decodeFlags(pPage, flags);
1994 pPage->cellOffset = first;
1995 pPage->aDataEnd = &data[pBt->usableSize];
1996 pPage->aCellIdx = &data[first];
1997 pPage->aDataOfst = &data[pPage->childPtrSize];
1998 pPage->nOverflow = 0;
1999 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
2000 pPage->maskPage = (u16)(pBt->pageSize - 1);
2001 pPage->nCell = 0;
2002 pPage->isInit = 1;
2007 ** Convert a DbPage obtained from the pager into a MemPage used by
2008 ** the btree layer.
2010 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
2011 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2012 if( pgno!=pPage->pgno ){
2013 pPage->aData = sqlite3PagerGetData(pDbPage);
2014 pPage->pDbPage = pDbPage;
2015 pPage->pBt = pBt;
2016 pPage->pgno = pgno;
2017 pPage->hdrOffset = pgno==1 ? 100 : 0;
2019 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
2020 return pPage;
2024 ** Get a page from the pager. Initialize the MemPage.pBt and
2025 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
2027 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
2028 ** about the content of the page at this time. So do not go to the disk
2029 ** to fetch the content. Just fill in the content with zeros for now.
2030 ** If in the future we call sqlite3PagerWrite() on this page, that
2031 ** means we have started to be concerned about content and the disk
2032 ** read should occur at that point.
2034 static int btreeGetPage(
2035 BtShared *pBt, /* The btree */
2036 Pgno pgno, /* Number of the page to fetch */
2037 MemPage **ppPage, /* Return the page in this parameter */
2038 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2040 int rc;
2041 DbPage *pDbPage;
2043 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
2044 assert( sqlite3_mutex_held(pBt->mutex) );
2045 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
2046 if( rc ) return rc;
2047 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
2048 return SQLITE_OK;
2052 ** Retrieve a page from the pager cache. If the requested page is not
2053 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
2054 ** MemPage.aData elements if needed.
2056 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
2057 DbPage *pDbPage;
2058 assert( sqlite3_mutex_held(pBt->mutex) );
2059 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
2060 if( pDbPage ){
2061 return btreePageFromDbPage(pDbPage, pgno, pBt);
2063 return 0;
2067 ** Return the size of the database file in pages. If there is any kind of
2068 ** error, return ((unsigned int)-1).
2070 static Pgno btreePagecount(BtShared *pBt){
2071 return pBt->nPage;
2073 u32 sqlite3BtreeLastPage(Btree *p){
2074 assert( sqlite3BtreeHoldsMutex(p) );
2075 assert( ((p->pBt->nPage)&0x80000000)==0 );
2076 return btreePagecount(p->pBt);
2080 ** Get a page from the pager and initialize it.
2082 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
2083 ** call. Do additional sanity checking on the page in this case.
2084 ** And if the fetch fails, this routine must decrement pCur->iPage.
2086 ** The page is fetched as read-write unless pCur is not NULL and is
2087 ** a read-only cursor.
2089 ** If an error occurs, then *ppPage is undefined. It
2090 ** may remain unchanged, or it may be set to an invalid value.
2092 static int getAndInitPage(
2093 BtShared *pBt, /* The database file */
2094 Pgno pgno, /* Number of the page to get */
2095 MemPage **ppPage, /* Write the page pointer here */
2096 BtCursor *pCur, /* Cursor to receive the page, or NULL */
2097 int bReadOnly /* True for a read-only page */
2099 int rc;
2100 DbPage *pDbPage;
2101 assert( sqlite3_mutex_held(pBt->mutex) );
2102 assert( pCur==0 || ppPage==&pCur->pPage );
2103 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
2104 assert( pCur==0 || pCur->iPage>0 );
2106 if( pgno>btreePagecount(pBt) ){
2107 rc = SQLITE_CORRUPT_BKPT;
2108 goto getAndInitPage_error;
2110 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
2111 if( rc ){
2112 goto getAndInitPage_error;
2114 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2115 if( (*ppPage)->isInit==0 ){
2116 btreePageFromDbPage(pDbPage, pgno, pBt);
2117 rc = btreeInitPage(*ppPage);
2118 if( rc!=SQLITE_OK ){
2119 releasePage(*ppPage);
2120 goto getAndInitPage_error;
2123 assert( (*ppPage)->pgno==pgno );
2124 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
2126 /* If obtaining a child page for a cursor, we must verify that the page is
2127 ** compatible with the root page. */
2128 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
2129 rc = SQLITE_CORRUPT_PGNO(pgno);
2130 releasePage(*ppPage);
2131 goto getAndInitPage_error;
2133 return SQLITE_OK;
2135 getAndInitPage_error:
2136 if( pCur ){
2137 pCur->iPage--;
2138 pCur->pPage = pCur->apPage[pCur->iPage];
2140 testcase( pgno==0 );
2141 assert( pgno!=0 || rc==SQLITE_CORRUPT );
2142 return rc;
2146 ** Release a MemPage. This should be called once for each prior
2147 ** call to btreeGetPage.
2149 ** Page1 is a special case and must be released using releasePageOne().
2151 static void releasePageNotNull(MemPage *pPage){
2152 assert( pPage->aData );
2153 assert( pPage->pBt );
2154 assert( pPage->pDbPage!=0 );
2155 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2156 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2157 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2158 sqlite3PagerUnrefNotNull(pPage->pDbPage);
2160 static void releasePage(MemPage *pPage){
2161 if( pPage ) releasePageNotNull(pPage);
2163 static void releasePageOne(MemPage *pPage){
2164 assert( pPage!=0 );
2165 assert( pPage->aData );
2166 assert( pPage->pBt );
2167 assert( pPage->pDbPage!=0 );
2168 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2169 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2170 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2171 sqlite3PagerUnrefPageOne(pPage->pDbPage);
2175 ** Get an unused page.
2177 ** This works just like btreeGetPage() with the addition:
2179 ** * If the page is already in use for some other purpose, immediately
2180 ** release it and return an SQLITE_CURRUPT error.
2181 ** * Make sure the isInit flag is clear
2183 static int btreeGetUnusedPage(
2184 BtShared *pBt, /* The btree */
2185 Pgno pgno, /* Number of the page to fetch */
2186 MemPage **ppPage, /* Return the page in this parameter */
2187 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2189 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
2190 if( rc==SQLITE_OK ){
2191 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
2192 releasePage(*ppPage);
2193 *ppPage = 0;
2194 return SQLITE_CORRUPT_BKPT;
2196 (*ppPage)->isInit = 0;
2197 }else{
2198 *ppPage = 0;
2200 return rc;
2205 ** During a rollback, when the pager reloads information into the cache
2206 ** so that the cache is restored to its original state at the start of
2207 ** the transaction, for each page restored this routine is called.
2209 ** This routine needs to reset the extra data section at the end of the
2210 ** page to agree with the restored data.
2212 static void pageReinit(DbPage *pData){
2213 MemPage *pPage;
2214 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
2215 assert( sqlite3PagerPageRefcount(pData)>0 );
2216 if( pPage->isInit ){
2217 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2218 pPage->isInit = 0;
2219 if( sqlite3PagerPageRefcount(pData)>1 ){
2220 /* pPage might not be a btree page; it might be an overflow page
2221 ** or ptrmap page or a free page. In those cases, the following
2222 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
2223 ** But no harm is done by this. And it is very important that
2224 ** btreeInitPage() be called on every btree page so we make
2225 ** the call for every page that comes in for re-initing. */
2226 btreeInitPage(pPage);
2232 ** Invoke the busy handler for a btree.
2234 static int btreeInvokeBusyHandler(void *pArg){
2235 BtShared *pBt = (BtShared*)pArg;
2236 assert( pBt->db );
2237 assert( sqlite3_mutex_held(pBt->db->mutex) );
2238 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
2239 sqlite3PagerFile(pBt->pPager));
2243 ** Open a database file.
2245 ** zFilename is the name of the database file. If zFilename is NULL
2246 ** then an ephemeral database is created. The ephemeral database might
2247 ** be exclusively in memory, or it might use a disk-based memory cache.
2248 ** Either way, the ephemeral database will be automatically deleted
2249 ** when sqlite3BtreeClose() is called.
2251 ** If zFilename is ":memory:" then an in-memory database is created
2252 ** that is automatically destroyed when it is closed.
2254 ** The "flags" parameter is a bitmask that might contain bits like
2255 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
2257 ** If the database is already opened in the same database connection
2258 ** and we are in shared cache mode, then the open will fail with an
2259 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
2260 ** objects in the same database connection since doing so will lead
2261 ** to problems with locking.
2263 int sqlite3BtreeOpen(
2264 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
2265 const char *zFilename, /* Name of the file containing the BTree database */
2266 sqlite3 *db, /* Associated database handle */
2267 Btree **ppBtree, /* Pointer to new Btree object written here */
2268 int flags, /* Options */
2269 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
2271 BtShared *pBt = 0; /* Shared part of btree structure */
2272 Btree *p; /* Handle to return */
2273 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
2274 int rc = SQLITE_OK; /* Result code from this function */
2275 u8 nReserve; /* Byte of unused space on each page */
2276 unsigned char zDbHeader[100]; /* Database header content */
2278 /* True if opening an ephemeral, temporary database */
2279 const int isTempDb = zFilename==0 || zFilename[0]==0;
2281 /* Set the variable isMemdb to true for an in-memory database, or
2282 ** false for a file-based database.
2284 #ifdef SQLITE_OMIT_MEMORYDB
2285 const int isMemdb = 0;
2286 #else
2287 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
2288 || (isTempDb && sqlite3TempInMemory(db))
2289 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
2290 #endif
2292 assert( db!=0 );
2293 assert( pVfs!=0 );
2294 assert( sqlite3_mutex_held(db->mutex) );
2295 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
2297 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
2298 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
2300 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
2301 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
2303 if( isMemdb ){
2304 flags |= BTREE_MEMORY;
2306 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
2307 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
2309 p = sqlite3MallocZero(sizeof(Btree));
2310 if( !p ){
2311 return SQLITE_NOMEM_BKPT;
2313 p->inTrans = TRANS_NONE;
2314 p->db = db;
2315 #ifndef SQLITE_OMIT_SHARED_CACHE
2316 p->lock.pBtree = p;
2317 p->lock.iTable = 1;
2318 #endif
2320 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2322 ** If this Btree is a candidate for shared cache, try to find an
2323 ** existing BtShared object that we can share with
2325 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
2326 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
2327 int nFilename = sqlite3Strlen30(zFilename)+1;
2328 int nFullPathname = pVfs->mxPathname+1;
2329 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
2330 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2332 p->sharable = 1;
2333 if( !zFullPathname ){
2334 sqlite3_free(p);
2335 return SQLITE_NOMEM_BKPT;
2337 if( isMemdb ){
2338 memcpy(zFullPathname, zFilename, nFilename);
2339 }else{
2340 rc = sqlite3OsFullPathname(pVfs, zFilename,
2341 nFullPathname, zFullPathname);
2342 if( rc ){
2343 sqlite3_free(zFullPathname);
2344 sqlite3_free(p);
2345 return rc;
2348 #if SQLITE_THREADSAFE
2349 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
2350 sqlite3_mutex_enter(mutexOpen);
2351 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
2352 sqlite3_mutex_enter(mutexShared);
2353 #endif
2354 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
2355 assert( pBt->nRef>0 );
2356 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
2357 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
2358 int iDb;
2359 for(iDb=db->nDb-1; iDb>=0; iDb--){
2360 Btree *pExisting = db->aDb[iDb].pBt;
2361 if( pExisting && pExisting->pBt==pBt ){
2362 sqlite3_mutex_leave(mutexShared);
2363 sqlite3_mutex_leave(mutexOpen);
2364 sqlite3_free(zFullPathname);
2365 sqlite3_free(p);
2366 return SQLITE_CONSTRAINT;
2369 p->pBt = pBt;
2370 pBt->nRef++;
2371 break;
2374 sqlite3_mutex_leave(mutexShared);
2375 sqlite3_free(zFullPathname);
2377 #ifdef SQLITE_DEBUG
2378 else{
2379 /* In debug mode, we mark all persistent databases as sharable
2380 ** even when they are not. This exercises the locking code and
2381 ** gives more opportunity for asserts(sqlite3_mutex_held())
2382 ** statements to find locking problems.
2384 p->sharable = 1;
2386 #endif
2388 #endif
2389 if( pBt==0 ){
2391 ** The following asserts make sure that structures used by the btree are
2392 ** the right size. This is to guard against size changes that result
2393 ** when compiling on a different architecture.
2395 assert( sizeof(i64)==8 );
2396 assert( sizeof(u64)==8 );
2397 assert( sizeof(u32)==4 );
2398 assert( sizeof(u16)==2 );
2399 assert( sizeof(Pgno)==4 );
2401 pBt = sqlite3MallocZero( sizeof(*pBt) );
2402 if( pBt==0 ){
2403 rc = SQLITE_NOMEM_BKPT;
2404 goto btree_open_out;
2406 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
2407 sizeof(MemPage), flags, vfsFlags, pageReinit);
2408 if( rc==SQLITE_OK ){
2409 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
2410 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
2412 if( rc!=SQLITE_OK ){
2413 goto btree_open_out;
2415 pBt->openFlags = (u8)flags;
2416 pBt->db = db;
2417 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
2418 p->pBt = pBt;
2420 pBt->pCursor = 0;
2421 pBt->pPage1 = 0;
2422 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
2423 #if defined(SQLITE_SECURE_DELETE)
2424 pBt->btsFlags |= BTS_SECURE_DELETE;
2425 #elif defined(SQLITE_FAST_SECURE_DELETE)
2426 pBt->btsFlags |= BTS_OVERWRITE;
2427 #endif
2428 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2429 ** determined by the 2-byte integer located at an offset of 16 bytes from
2430 ** the beginning of the database file. */
2431 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
2432 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
2433 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
2434 pBt->pageSize = 0;
2435 #ifndef SQLITE_OMIT_AUTOVACUUM
2436 /* If the magic name ":memory:" will create an in-memory database, then
2437 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
2438 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
2439 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
2440 ** regular file-name. In this case the auto-vacuum applies as per normal.
2442 if( zFilename && !isMemdb ){
2443 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
2444 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
2446 #endif
2447 nReserve = 0;
2448 }else{
2449 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2450 ** determined by the one-byte unsigned integer found at an offset of 20
2451 ** into the database file header. */
2452 nReserve = zDbHeader[20];
2453 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2454 #ifndef SQLITE_OMIT_AUTOVACUUM
2455 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
2456 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
2457 #endif
2459 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2460 if( rc ) goto btree_open_out;
2461 pBt->usableSize = pBt->pageSize - nReserve;
2462 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
2464 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2465 /* Add the new BtShared object to the linked list sharable BtShareds.
2467 pBt->nRef = 1;
2468 if( p->sharable ){
2469 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2470 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
2471 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
2472 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
2473 if( pBt->mutex==0 ){
2474 rc = SQLITE_NOMEM_BKPT;
2475 goto btree_open_out;
2478 sqlite3_mutex_enter(mutexShared);
2479 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
2480 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
2481 sqlite3_mutex_leave(mutexShared);
2483 #endif
2486 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2487 /* If the new Btree uses a sharable pBtShared, then link the new
2488 ** Btree into the list of all sharable Btrees for the same connection.
2489 ** The list is kept in ascending order by pBt address.
2491 if( p->sharable ){
2492 int i;
2493 Btree *pSib;
2494 for(i=0; i<db->nDb; i++){
2495 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
2496 while( pSib->pPrev ){ pSib = pSib->pPrev; }
2497 if( (uptr)p->pBt<(uptr)pSib->pBt ){
2498 p->pNext = pSib;
2499 p->pPrev = 0;
2500 pSib->pPrev = p;
2501 }else{
2502 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
2503 pSib = pSib->pNext;
2505 p->pNext = pSib->pNext;
2506 p->pPrev = pSib;
2507 if( p->pNext ){
2508 p->pNext->pPrev = p;
2510 pSib->pNext = p;
2512 break;
2516 #endif
2517 *ppBtree = p;
2519 btree_open_out:
2520 if( rc!=SQLITE_OK ){
2521 if( pBt && pBt->pPager ){
2522 sqlite3PagerClose(pBt->pPager, 0);
2524 sqlite3_free(pBt);
2525 sqlite3_free(p);
2526 *ppBtree = 0;
2527 }else{
2528 sqlite3_file *pFile;
2530 /* If the B-Tree was successfully opened, set the pager-cache size to the
2531 ** default value. Except, when opening on an existing shared pager-cache,
2532 ** do not change the pager-cache size.
2534 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
2535 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
2538 pFile = sqlite3PagerFile(pBt->pPager);
2539 if( pFile->pMethods ){
2540 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
2543 if( mutexOpen ){
2544 assert( sqlite3_mutex_held(mutexOpen) );
2545 sqlite3_mutex_leave(mutexOpen);
2547 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
2548 return rc;
2552 ** Decrement the BtShared.nRef counter. When it reaches zero,
2553 ** remove the BtShared structure from the sharing list. Return
2554 ** true if the BtShared.nRef counter reaches zero and return
2555 ** false if it is still positive.
2557 static int removeFromSharingList(BtShared *pBt){
2558 #ifndef SQLITE_OMIT_SHARED_CACHE
2559 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
2560 BtShared *pList;
2561 int removed = 0;
2563 assert( sqlite3_mutex_notheld(pBt->mutex) );
2564 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
2565 sqlite3_mutex_enter(pMaster);
2566 pBt->nRef--;
2567 if( pBt->nRef<=0 ){
2568 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
2569 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
2570 }else{
2571 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
2572 while( ALWAYS(pList) && pList->pNext!=pBt ){
2573 pList=pList->pNext;
2575 if( ALWAYS(pList) ){
2576 pList->pNext = pBt->pNext;
2579 if( SQLITE_THREADSAFE ){
2580 sqlite3_mutex_free(pBt->mutex);
2582 removed = 1;
2584 sqlite3_mutex_leave(pMaster);
2585 return removed;
2586 #else
2587 return 1;
2588 #endif
2592 ** Make sure pBt->pTmpSpace points to an allocation of
2593 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
2594 ** pointer.
2596 static void allocateTempSpace(BtShared *pBt){
2597 if( !pBt->pTmpSpace ){
2598 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
2600 /* One of the uses of pBt->pTmpSpace is to format cells before
2601 ** inserting them into a leaf page (function fillInCell()). If
2602 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
2603 ** by the various routines that manipulate binary cells. Which
2604 ** can mean that fillInCell() only initializes the first 2 or 3
2605 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
2606 ** it into a database page. This is not actually a problem, but it
2607 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
2608 ** data is passed to system call write(). So to avoid this error,
2609 ** zero the first 4 bytes of temp space here.
2611 ** Also: Provide four bytes of initialized space before the
2612 ** beginning of pTmpSpace as an area available to prepend the
2613 ** left-child pointer to the beginning of a cell.
2615 if( pBt->pTmpSpace ){
2616 memset(pBt->pTmpSpace, 0, 8);
2617 pBt->pTmpSpace += 4;
2623 ** Free the pBt->pTmpSpace allocation
2625 static void freeTempSpace(BtShared *pBt){
2626 if( pBt->pTmpSpace ){
2627 pBt->pTmpSpace -= 4;
2628 sqlite3PageFree(pBt->pTmpSpace);
2629 pBt->pTmpSpace = 0;
2634 ** Close an open database and invalidate all cursors.
2636 int sqlite3BtreeClose(Btree *p){
2637 BtShared *pBt = p->pBt;
2638 BtCursor *pCur;
2640 /* Close all cursors opened via this handle. */
2641 assert( sqlite3_mutex_held(p->db->mutex) );
2642 sqlite3BtreeEnter(p);
2643 pCur = pBt->pCursor;
2644 while( pCur ){
2645 BtCursor *pTmp = pCur;
2646 pCur = pCur->pNext;
2647 if( pTmp->pBtree==p ){
2648 sqlite3BtreeCloseCursor(pTmp);
2652 /* Rollback any active transaction and free the handle structure.
2653 ** The call to sqlite3BtreeRollback() drops any table-locks held by
2654 ** this handle.
2656 sqlite3BtreeRollback(p, SQLITE_OK, 0);
2657 sqlite3BtreeLeave(p);
2659 /* If there are still other outstanding references to the shared-btree
2660 ** structure, return now. The remainder of this procedure cleans
2661 ** up the shared-btree.
2663 assert( p->wantToLock==0 && p->locked==0 );
2664 if( !p->sharable || removeFromSharingList(pBt) ){
2665 /* The pBt is no longer on the sharing list, so we can access
2666 ** it without having to hold the mutex.
2668 ** Clean out and delete the BtShared object.
2670 assert( !pBt->pCursor );
2671 sqlite3PagerClose(pBt->pPager, p->db);
2672 if( pBt->xFreeSchema && pBt->pSchema ){
2673 pBt->xFreeSchema(pBt->pSchema);
2675 sqlite3DbFree(0, pBt->pSchema);
2676 freeTempSpace(pBt);
2677 sqlite3_free(pBt);
2680 #ifndef SQLITE_OMIT_SHARED_CACHE
2681 assert( p->wantToLock==0 );
2682 assert( p->locked==0 );
2683 if( p->pPrev ) p->pPrev->pNext = p->pNext;
2684 if( p->pNext ) p->pNext->pPrev = p->pPrev;
2685 #endif
2687 sqlite3_free(p);
2688 return SQLITE_OK;
2692 ** Change the "soft" limit on the number of pages in the cache.
2693 ** Unused and unmodified pages will be recycled when the number of
2694 ** pages in the cache exceeds this soft limit. But the size of the
2695 ** cache is allowed to grow larger than this limit if it contains
2696 ** dirty pages or pages still in active use.
2698 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
2699 BtShared *pBt = p->pBt;
2700 assert( sqlite3_mutex_held(p->db->mutex) );
2701 sqlite3BtreeEnter(p);
2702 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
2703 sqlite3BtreeLeave(p);
2704 return SQLITE_OK;
2708 ** Change the "spill" limit on the number of pages in the cache.
2709 ** If the number of pages exceeds this limit during a write transaction,
2710 ** the pager might attempt to "spill" pages to the journal early in
2711 ** order to free up memory.
2713 ** The value returned is the current spill size. If zero is passed
2714 ** as an argument, no changes are made to the spill size setting, so
2715 ** using mxPage of 0 is a way to query the current spill size.
2717 int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
2718 BtShared *pBt = p->pBt;
2719 int res;
2720 assert( sqlite3_mutex_held(p->db->mutex) );
2721 sqlite3BtreeEnter(p);
2722 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
2723 sqlite3BtreeLeave(p);
2724 return res;
2727 #if SQLITE_MAX_MMAP_SIZE>0
2729 ** Change the limit on the amount of the database file that may be
2730 ** memory mapped.
2732 int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
2733 BtShared *pBt = p->pBt;
2734 assert( sqlite3_mutex_held(p->db->mutex) );
2735 sqlite3BtreeEnter(p);
2736 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
2737 sqlite3BtreeLeave(p);
2738 return SQLITE_OK;
2740 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
2743 ** Change the way data is synced to disk in order to increase or decrease
2744 ** how well the database resists damage due to OS crashes and power
2745 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
2746 ** there is a high probability of damage) Level 2 is the default. There
2747 ** is a very low but non-zero probability of damage. Level 3 reduces the
2748 ** probability of damage to near zero but with a write performance reduction.
2750 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2751 int sqlite3BtreeSetPagerFlags(
2752 Btree *p, /* The btree to set the safety level on */
2753 unsigned pgFlags /* Various PAGER_* flags */
2755 BtShared *pBt = p->pBt;
2756 assert( sqlite3_mutex_held(p->db->mutex) );
2757 sqlite3BtreeEnter(p);
2758 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
2759 sqlite3BtreeLeave(p);
2760 return SQLITE_OK;
2762 #endif
2765 ** Change the default pages size and the number of reserved bytes per page.
2766 ** Or, if the page size has already been fixed, return SQLITE_READONLY
2767 ** without changing anything.
2769 ** The page size must be a power of 2 between 512 and 65536. If the page
2770 ** size supplied does not meet this constraint then the page size is not
2771 ** changed.
2773 ** Page sizes are constrained to be a power of two so that the region
2774 ** of the database file used for locking (beginning at PENDING_BYTE,
2775 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
2776 ** at the beginning of a page.
2778 ** If parameter nReserve is less than zero, then the number of reserved
2779 ** bytes per page is left unchanged.
2781 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
2782 ** and autovacuum mode can no longer be changed.
2784 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
2785 int rc = SQLITE_OK;
2786 BtShared *pBt = p->pBt;
2787 assert( nReserve>=-1 && nReserve<=255 );
2788 sqlite3BtreeEnter(p);
2789 #if SQLITE_HAS_CODEC
2790 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
2791 #endif
2792 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
2793 sqlite3BtreeLeave(p);
2794 return SQLITE_READONLY;
2796 if( nReserve<0 ){
2797 nReserve = pBt->pageSize - pBt->usableSize;
2799 assert( nReserve>=0 && nReserve<=255 );
2800 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
2801 ((pageSize-1)&pageSize)==0 ){
2802 assert( (pageSize & 7)==0 );
2803 assert( !pBt->pCursor );
2804 pBt->pageSize = (u32)pageSize;
2805 freeTempSpace(pBt);
2807 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2808 pBt->usableSize = pBt->pageSize - (u16)nReserve;
2809 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2810 sqlite3BtreeLeave(p);
2811 return rc;
2815 ** Return the currently defined page size
2817 int sqlite3BtreeGetPageSize(Btree *p){
2818 return p->pBt->pageSize;
2822 ** This function is similar to sqlite3BtreeGetReserve(), except that it
2823 ** may only be called if it is guaranteed that the b-tree mutex is already
2824 ** held.
2826 ** This is useful in one special case in the backup API code where it is
2827 ** known that the shared b-tree mutex is held, but the mutex on the
2828 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
2829 ** were to be called, it might collide with some other operation on the
2830 ** database handle that owns *p, causing undefined behavior.
2832 int sqlite3BtreeGetReserveNoMutex(Btree *p){
2833 int n;
2834 assert( sqlite3_mutex_held(p->pBt->mutex) );
2835 n = p->pBt->pageSize - p->pBt->usableSize;
2836 return n;
2840 ** Return the number of bytes of space at the end of every page that
2841 ** are intentually left unused. This is the "reserved" space that is
2842 ** sometimes used by extensions.
2844 ** If SQLITE_HAS_MUTEX is defined then the number returned is the
2845 ** greater of the current reserved space and the maximum requested
2846 ** reserve space.
2848 int sqlite3BtreeGetOptimalReserve(Btree *p){
2849 int n;
2850 sqlite3BtreeEnter(p);
2851 n = sqlite3BtreeGetReserveNoMutex(p);
2852 #ifdef SQLITE_HAS_CODEC
2853 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
2854 #endif
2855 sqlite3BtreeLeave(p);
2856 return n;
2861 ** Set the maximum page count for a database if mxPage is positive.
2862 ** No changes are made if mxPage is 0 or negative.
2863 ** Regardless of the value of mxPage, return the maximum page count.
2865 int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
2866 int n;
2867 sqlite3BtreeEnter(p);
2868 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
2869 sqlite3BtreeLeave(p);
2870 return n;
2874 ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
2876 ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
2877 ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
2878 ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
2879 ** newFlag==(-1) No changes
2881 ** This routine acts as a query if newFlag is less than zero
2883 ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
2884 ** freelist leaf pages are not written back to the database. Thus in-page
2885 ** deleted content is cleared, but freelist deleted content is not.
2887 ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
2888 ** that freelist leaf pages are written back into the database, increasing
2889 ** the amount of disk I/O.
2891 int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
2892 int b;
2893 if( p==0 ) return 0;
2894 sqlite3BtreeEnter(p);
2895 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
2896 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
2897 if( newFlag>=0 ){
2898 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
2899 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
2901 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
2902 sqlite3BtreeLeave(p);
2903 return b;
2907 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
2908 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
2909 ** is disabled. The default value for the auto-vacuum property is
2910 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
2912 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
2913 #ifdef SQLITE_OMIT_AUTOVACUUM
2914 return SQLITE_READONLY;
2915 #else
2916 BtShared *pBt = p->pBt;
2917 int rc = SQLITE_OK;
2918 u8 av = (u8)autoVacuum;
2920 sqlite3BtreeEnter(p);
2921 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
2922 rc = SQLITE_READONLY;
2923 }else{
2924 pBt->autoVacuum = av ?1:0;
2925 pBt->incrVacuum = av==2 ?1:0;
2927 sqlite3BtreeLeave(p);
2928 return rc;
2929 #endif
2933 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
2934 ** enabled 1 is returned. Otherwise 0.
2936 int sqlite3BtreeGetAutoVacuum(Btree *p){
2937 #ifdef SQLITE_OMIT_AUTOVACUUM
2938 return BTREE_AUTOVACUUM_NONE;
2939 #else
2940 int rc;
2941 sqlite3BtreeEnter(p);
2942 rc = (
2943 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
2944 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
2945 BTREE_AUTOVACUUM_INCR
2947 sqlite3BtreeLeave(p);
2948 return rc;
2949 #endif
2953 ** If the user has not set the safety-level for this database connection
2954 ** using "PRAGMA synchronous", and if the safety-level is not already
2955 ** set to the value passed to this function as the second parameter,
2956 ** set it so.
2958 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
2959 && !defined(SQLITE_OMIT_WAL)
2960 static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
2961 sqlite3 *db;
2962 Db *pDb;
2963 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
2964 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
2965 if( pDb->bSyncSet==0
2966 && pDb->safety_level!=safety_level
2967 && pDb!=&db->aDb[1]
2969 pDb->safety_level = safety_level;
2970 sqlite3PagerSetFlags(pBt->pPager,
2971 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
2975 #else
2976 # define setDefaultSyncFlag(pBt,safety_level)
2977 #endif
2979 /* Forward declaration */
2980 static int newDatabase(BtShared*);
2984 ** Get a reference to pPage1 of the database file. This will
2985 ** also acquire a readlock on that file.
2987 ** SQLITE_OK is returned on success. If the file is not a
2988 ** well-formed database file, then SQLITE_CORRUPT is returned.
2989 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
2990 ** is returned if we run out of memory.
2992 static int lockBtree(BtShared *pBt){
2993 int rc; /* Result code from subfunctions */
2994 MemPage *pPage1; /* Page 1 of the database file */
2995 int nPage; /* Number of pages in the database */
2996 int nPageFile = 0; /* Number of pages in the database file */
2997 int nPageHeader; /* Number of pages in the database according to hdr */
2999 assert( sqlite3_mutex_held(pBt->mutex) );
3000 assert( pBt->pPage1==0 );
3001 rc = sqlite3PagerSharedLock(pBt->pPager);
3002 if( rc!=SQLITE_OK ) return rc;
3003 rc = btreeGetPage(pBt, 1, &pPage1, 0);
3004 if( rc!=SQLITE_OK ) return rc;
3006 /* Do some checking to help insure the file we opened really is
3007 ** a valid database file.
3009 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
3010 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
3011 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
3012 nPage = nPageFile;
3014 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){
3015 nPage = 0;
3017 if( nPage>0 ){
3018 u32 pageSize;
3019 u32 usableSize;
3020 u8 *page1 = pPage1->aData;
3021 rc = SQLITE_NOTADB;
3022 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
3023 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
3024 ** 61 74 20 33 00. */
3025 if( memcmp(page1, zMagicHeader, 16)!=0 ){
3026 goto page1_init_failed;
3029 #ifdef SQLITE_OMIT_WAL
3030 if( page1[18]>1 ){
3031 pBt->btsFlags |= BTS_READ_ONLY;
3033 if( page1[19]>1 ){
3034 goto page1_init_failed;
3036 #else
3037 if( page1[18]>2 ){
3038 pBt->btsFlags |= BTS_READ_ONLY;
3040 if( page1[19]>2 ){
3041 goto page1_init_failed;
3044 /* If the write version is set to 2, this database should be accessed
3045 ** in WAL mode. If the log is not already open, open it now. Then
3046 ** return SQLITE_OK and return without populating BtShared.pPage1.
3047 ** The caller detects this and calls this function again. This is
3048 ** required as the version of page 1 currently in the page1 buffer
3049 ** may not be the latest version - there may be a newer one in the log
3050 ** file.
3052 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
3053 int isOpen = 0;
3054 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
3055 if( rc!=SQLITE_OK ){
3056 goto page1_init_failed;
3057 }else{
3058 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
3059 if( isOpen==0 ){
3060 releasePageOne(pPage1);
3061 return SQLITE_OK;
3064 rc = SQLITE_NOTADB;
3065 }else{
3066 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
3068 #endif
3070 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
3071 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
3073 ** The original design allowed these amounts to vary, but as of
3074 ** version 3.6.0, we require them to be fixed.
3076 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
3077 goto page1_init_failed;
3079 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
3080 ** determined by the 2-byte integer located at an offset of 16 bytes from
3081 ** the beginning of the database file. */
3082 pageSize = (page1[16]<<8) | (page1[17]<<16);
3083 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
3084 ** between 512 and 65536 inclusive. */
3085 if( ((pageSize-1)&pageSize)!=0
3086 || pageSize>SQLITE_MAX_PAGE_SIZE
3087 || pageSize<=256
3089 goto page1_init_failed;
3091 assert( (pageSize & 7)==0 );
3092 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
3093 ** integer at offset 20 is the number of bytes of space at the end of
3094 ** each page to reserve for extensions.
3096 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
3097 ** determined by the one-byte unsigned integer found at an offset of 20
3098 ** into the database file header. */
3099 usableSize = pageSize - page1[20];
3100 if( (u32)pageSize!=pBt->pageSize ){
3101 /* After reading the first page of the database assuming a page size
3102 ** of BtShared.pageSize, we have discovered that the page-size is
3103 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
3104 ** zero and return SQLITE_OK. The caller will call this function
3105 ** again with the correct page-size.
3107 releasePageOne(pPage1);
3108 pBt->usableSize = usableSize;
3109 pBt->pageSize = pageSize;
3110 freeTempSpace(pBt);
3111 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
3112 pageSize-usableSize);
3113 return rc;
3115 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
3116 rc = SQLITE_CORRUPT_BKPT;
3117 goto page1_init_failed;
3119 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
3120 ** be less than 480. In other words, if the page size is 512, then the
3121 ** reserved space size cannot exceed 32. */
3122 if( usableSize<480 ){
3123 goto page1_init_failed;
3125 pBt->pageSize = pageSize;
3126 pBt->usableSize = usableSize;
3127 #ifndef SQLITE_OMIT_AUTOVACUUM
3128 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
3129 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
3130 #endif
3133 /* maxLocal is the maximum amount of payload to store locally for
3134 ** a cell. Make sure it is small enough so that at least minFanout
3135 ** cells can will fit on one page. We assume a 10-byte page header.
3136 ** Besides the payload, the cell must store:
3137 ** 2-byte pointer to the cell
3138 ** 4-byte child pointer
3139 ** 9-byte nKey value
3140 ** 4-byte nData value
3141 ** 4-byte overflow page pointer
3142 ** So a cell consists of a 2-byte pointer, a header which is as much as
3143 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
3144 ** page pointer.
3146 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
3147 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
3148 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
3149 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
3150 if( pBt->maxLocal>127 ){
3151 pBt->max1bytePayload = 127;
3152 }else{
3153 pBt->max1bytePayload = (u8)pBt->maxLocal;
3155 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
3156 pBt->pPage1 = pPage1;
3157 pBt->nPage = nPage;
3158 return SQLITE_OK;
3160 page1_init_failed:
3161 releasePageOne(pPage1);
3162 pBt->pPage1 = 0;
3163 return rc;
3166 #ifndef NDEBUG
3168 ** Return the number of cursors open on pBt. This is for use
3169 ** in assert() expressions, so it is only compiled if NDEBUG is not
3170 ** defined.
3172 ** Only write cursors are counted if wrOnly is true. If wrOnly is
3173 ** false then all cursors are counted.
3175 ** For the purposes of this routine, a cursor is any cursor that
3176 ** is capable of reading or writing to the database. Cursors that
3177 ** have been tripped into the CURSOR_FAULT state are not counted.
3179 static int countValidCursors(BtShared *pBt, int wrOnly){
3180 BtCursor *pCur;
3181 int r = 0;
3182 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
3183 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
3184 && pCur->eState!=CURSOR_FAULT ) r++;
3186 return r;
3188 #endif
3191 ** If there are no outstanding cursors and we are not in the middle
3192 ** of a transaction but there is a read lock on the database, then
3193 ** this routine unrefs the first page of the database file which
3194 ** has the effect of releasing the read lock.
3196 ** If there is a transaction in progress, this routine is a no-op.
3198 static void unlockBtreeIfUnused(BtShared *pBt){
3199 assert( sqlite3_mutex_held(pBt->mutex) );
3200 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
3201 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
3202 MemPage *pPage1 = pBt->pPage1;
3203 assert( pPage1->aData );
3204 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
3205 pBt->pPage1 = 0;
3206 releasePageOne(pPage1);
3211 ** If pBt points to an empty file then convert that empty file
3212 ** into a new empty database by initializing the first page of
3213 ** the database.
3215 static int newDatabase(BtShared *pBt){
3216 MemPage *pP1;
3217 unsigned char *data;
3218 int rc;
3220 assert( sqlite3_mutex_held(pBt->mutex) );
3221 if( pBt->nPage>0 ){
3222 return SQLITE_OK;
3224 pP1 = pBt->pPage1;
3225 assert( pP1!=0 );
3226 data = pP1->aData;
3227 rc = sqlite3PagerWrite(pP1->pDbPage);
3228 if( rc ) return rc;
3229 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
3230 assert( sizeof(zMagicHeader)==16 );
3231 data[16] = (u8)((pBt->pageSize>>8)&0xff);
3232 data[17] = (u8)((pBt->pageSize>>16)&0xff);
3233 data[18] = 1;
3234 data[19] = 1;
3235 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
3236 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
3237 data[21] = 64;
3238 data[22] = 32;
3239 data[23] = 32;
3240 memset(&data[24], 0, 100-24);
3241 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
3242 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3243 #ifndef SQLITE_OMIT_AUTOVACUUM
3244 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
3245 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
3246 put4byte(&data[36 + 4*4], pBt->autoVacuum);
3247 put4byte(&data[36 + 7*4], pBt->incrVacuum);
3248 #endif
3249 pBt->nPage = 1;
3250 data[31] = 1;
3251 return SQLITE_OK;
3255 ** Initialize the first page of the database file (creating a database
3256 ** consisting of a single page and no schema objects). Return SQLITE_OK
3257 ** if successful, or an SQLite error code otherwise.
3259 int sqlite3BtreeNewDb(Btree *p){
3260 int rc;
3261 sqlite3BtreeEnter(p);
3262 p->pBt->nPage = 0;
3263 rc = newDatabase(p->pBt);
3264 sqlite3BtreeLeave(p);
3265 return rc;
3269 ** Attempt to start a new transaction. A write-transaction
3270 ** is started if the second argument is nonzero, otherwise a read-
3271 ** transaction. If the second argument is 2 or more and exclusive
3272 ** transaction is started, meaning that no other process is allowed
3273 ** to access the database. A preexisting transaction may not be
3274 ** upgraded to exclusive by calling this routine a second time - the
3275 ** exclusivity flag only works for a new transaction.
3277 ** A write-transaction must be started before attempting any
3278 ** changes to the database. None of the following routines
3279 ** will work unless a transaction is started first:
3281 ** sqlite3BtreeCreateTable()
3282 ** sqlite3BtreeCreateIndex()
3283 ** sqlite3BtreeClearTable()
3284 ** sqlite3BtreeDropTable()
3285 ** sqlite3BtreeInsert()
3286 ** sqlite3BtreeDelete()
3287 ** sqlite3BtreeUpdateMeta()
3289 ** If an initial attempt to acquire the lock fails because of lock contention
3290 ** and the database was previously unlocked, then invoke the busy handler
3291 ** if there is one. But if there was previously a read-lock, do not
3292 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
3293 ** returned when there is already a read-lock in order to avoid a deadlock.
3295 ** Suppose there are two processes A and B. A has a read lock and B has
3296 ** a reserved lock. B tries to promote to exclusive but is blocked because
3297 ** of A's read lock. A tries to promote to reserved but is blocked by B.
3298 ** One or the other of the two processes must give way or there can be
3299 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
3300 ** when A already has a read lock, we encourage A to give up and let B
3301 ** proceed.
3303 int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
3304 BtShared *pBt = p->pBt;
3305 int rc = SQLITE_OK;
3307 sqlite3BtreeEnter(p);
3308 btreeIntegrity(p);
3310 /* If the btree is already in a write-transaction, or it
3311 ** is already in a read-transaction and a read-transaction
3312 ** is requested, this is a no-op.
3314 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
3315 goto trans_begun;
3317 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
3319 /* Write transactions are not possible on a read-only database */
3320 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
3321 rc = SQLITE_READONLY;
3322 goto trans_begun;
3325 #ifndef SQLITE_OMIT_SHARED_CACHE
3327 sqlite3 *pBlock = 0;
3328 /* If another database handle has already opened a write transaction
3329 ** on this shared-btree structure and a second write transaction is
3330 ** requested, return SQLITE_LOCKED.
3332 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
3333 || (pBt->btsFlags & BTS_PENDING)!=0
3335 pBlock = pBt->pWriter->db;
3336 }else if( wrflag>1 ){
3337 BtLock *pIter;
3338 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3339 if( pIter->pBtree!=p ){
3340 pBlock = pIter->pBtree->db;
3341 break;
3345 if( pBlock ){
3346 sqlite3ConnectionBlocked(p->db, pBlock);
3347 rc = SQLITE_LOCKED_SHAREDCACHE;
3348 goto trans_begun;
3351 #endif
3353 /* Any read-only or read-write transaction implies a read-lock on
3354 ** page 1. So if some other shared-cache client already has a write-lock
3355 ** on page 1, the transaction cannot be opened. */
3356 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
3357 if( SQLITE_OK!=rc ) goto trans_begun;
3359 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
3360 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
3361 do {
3362 /* Call lockBtree() until either pBt->pPage1 is populated or
3363 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
3364 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
3365 ** reading page 1 it discovers that the page-size of the database
3366 ** file is not pBt->pageSize. In this case lockBtree() will update
3367 ** pBt->pageSize to the page-size of the file on disk.
3369 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
3371 if( rc==SQLITE_OK && wrflag ){
3372 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
3373 rc = SQLITE_READONLY;
3374 }else{
3375 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
3376 if( rc==SQLITE_OK ){
3377 rc = newDatabase(pBt);
3378 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
3379 /* if there was no transaction opened when this function was
3380 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
3381 ** code to SQLITE_BUSY. */
3382 rc = SQLITE_BUSY;
3387 if( rc!=SQLITE_OK ){
3388 unlockBtreeIfUnused(pBt);
3390 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
3391 btreeInvokeBusyHandler(pBt) );
3392 sqlite3PagerResetLockTimeout(pBt->pPager);
3394 if( rc==SQLITE_OK ){
3395 if( p->inTrans==TRANS_NONE ){
3396 pBt->nTransaction++;
3397 #ifndef SQLITE_OMIT_SHARED_CACHE
3398 if( p->sharable ){
3399 assert( p->lock.pBtree==p && p->lock.iTable==1 );
3400 p->lock.eLock = READ_LOCK;
3401 p->lock.pNext = pBt->pLock;
3402 pBt->pLock = &p->lock;
3404 #endif
3406 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
3407 if( p->inTrans>pBt->inTransaction ){
3408 pBt->inTransaction = p->inTrans;
3410 if( wrflag ){
3411 MemPage *pPage1 = pBt->pPage1;
3412 #ifndef SQLITE_OMIT_SHARED_CACHE
3413 assert( !pBt->pWriter );
3414 pBt->pWriter = p;
3415 pBt->btsFlags &= ~BTS_EXCLUSIVE;
3416 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
3417 #endif
3419 /* If the db-size header field is incorrect (as it may be if an old
3420 ** client has been writing the database file), update it now. Doing
3421 ** this sooner rather than later means the database size can safely
3422 ** re-read the database size from page 1 if a savepoint or transaction
3423 ** rollback occurs within the transaction.
3425 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
3426 rc = sqlite3PagerWrite(pPage1->pDbPage);
3427 if( rc==SQLITE_OK ){
3428 put4byte(&pPage1->aData[28], pBt->nPage);
3434 trans_begun:
3435 if( rc==SQLITE_OK ){
3436 if( pSchemaVersion ){
3437 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
3439 if( wrflag ){
3440 /* This call makes sure that the pager has the correct number of
3441 ** open savepoints. If the second parameter is greater than 0 and
3442 ** the sub-journal is not already open, then it will be opened here.
3444 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
3448 btreeIntegrity(p);
3449 sqlite3BtreeLeave(p);
3450 return rc;
3453 #ifndef SQLITE_OMIT_AUTOVACUUM
3456 ** Set the pointer-map entries for all children of page pPage. Also, if
3457 ** pPage contains cells that point to overflow pages, set the pointer
3458 ** map entries for the overflow pages as well.
3460 static int setChildPtrmaps(MemPage *pPage){
3461 int i; /* Counter variable */
3462 int nCell; /* Number of cells in page pPage */
3463 int rc; /* Return code */
3464 BtShared *pBt = pPage->pBt;
3465 Pgno pgno = pPage->pgno;
3467 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3468 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3469 if( rc!=SQLITE_OK ) return rc;
3470 nCell = pPage->nCell;
3472 for(i=0; i<nCell; i++){
3473 u8 *pCell = findCell(pPage, i);
3475 ptrmapPutOvflPtr(pPage, pCell, &rc);
3477 if( !pPage->leaf ){
3478 Pgno childPgno = get4byte(pCell);
3479 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3483 if( !pPage->leaf ){
3484 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3485 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3488 return rc;
3492 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
3493 ** that it points to iTo. Parameter eType describes the type of pointer to
3494 ** be modified, as follows:
3496 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
3497 ** page of pPage.
3499 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
3500 ** page pointed to by one of the cells on pPage.
3502 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
3503 ** overflow page in the list.
3505 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
3506 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3507 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
3508 if( eType==PTRMAP_OVERFLOW2 ){
3509 /* The pointer is always the first 4 bytes of the page in this case. */
3510 if( get4byte(pPage->aData)!=iFrom ){
3511 return SQLITE_CORRUPT_PAGE(pPage);
3513 put4byte(pPage->aData, iTo);
3514 }else{
3515 int i;
3516 int nCell;
3517 int rc;
3519 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3520 if( rc ) return rc;
3521 nCell = pPage->nCell;
3523 for(i=0; i<nCell; i++){
3524 u8 *pCell = findCell(pPage, i);
3525 if( eType==PTRMAP_OVERFLOW1 ){
3526 CellInfo info;
3527 pPage->xParseCell(pPage, pCell, &info);
3528 if( info.nLocal<info.nPayload ){
3529 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
3530 return SQLITE_CORRUPT_PAGE(pPage);
3532 if( iFrom==get4byte(pCell+info.nSize-4) ){
3533 put4byte(pCell+info.nSize-4, iTo);
3534 break;
3537 }else{
3538 if( get4byte(pCell)==iFrom ){
3539 put4byte(pCell, iTo);
3540 break;
3545 if( i==nCell ){
3546 if( eType!=PTRMAP_BTREE ||
3547 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
3548 return SQLITE_CORRUPT_PAGE(pPage);
3550 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
3553 return SQLITE_OK;
3558 ** Move the open database page pDbPage to location iFreePage in the
3559 ** database. The pDbPage reference remains valid.
3561 ** The isCommit flag indicates that there is no need to remember that
3562 ** the journal needs to be sync()ed before database page pDbPage->pgno
3563 ** can be written to. The caller has already promised not to write to that
3564 ** page.
3566 static int relocatePage(
3567 BtShared *pBt, /* Btree */
3568 MemPage *pDbPage, /* Open page to move */
3569 u8 eType, /* Pointer map 'type' entry for pDbPage */
3570 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
3571 Pgno iFreePage, /* The location to move pDbPage to */
3572 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
3574 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
3575 Pgno iDbPage = pDbPage->pgno;
3576 Pager *pPager = pBt->pPager;
3577 int rc;
3579 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
3580 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
3581 assert( sqlite3_mutex_held(pBt->mutex) );
3582 assert( pDbPage->pBt==pBt );
3584 /* Move page iDbPage from its current location to page number iFreePage */
3585 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
3586 iDbPage, iFreePage, iPtrPage, eType));
3587 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
3588 if( rc!=SQLITE_OK ){
3589 return rc;
3591 pDbPage->pgno = iFreePage;
3593 /* If pDbPage was a btree-page, then it may have child pages and/or cells
3594 ** that point to overflow pages. The pointer map entries for all these
3595 ** pages need to be changed.
3597 ** If pDbPage is an overflow page, then the first 4 bytes may store a
3598 ** pointer to a subsequent overflow page. If this is the case, then
3599 ** the pointer map needs to be updated for the subsequent overflow page.
3601 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
3602 rc = setChildPtrmaps(pDbPage);
3603 if( rc!=SQLITE_OK ){
3604 return rc;
3606 }else{
3607 Pgno nextOvfl = get4byte(pDbPage->aData);
3608 if( nextOvfl!=0 ){
3609 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
3610 if( rc!=SQLITE_OK ){
3611 return rc;
3616 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
3617 ** that it points at iFreePage. Also fix the pointer map entry for
3618 ** iPtrPage.
3620 if( eType!=PTRMAP_ROOTPAGE ){
3621 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
3622 if( rc!=SQLITE_OK ){
3623 return rc;
3625 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
3626 if( rc!=SQLITE_OK ){
3627 releasePage(pPtrPage);
3628 return rc;
3630 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
3631 releasePage(pPtrPage);
3632 if( rc==SQLITE_OK ){
3633 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
3636 return rc;
3639 /* Forward declaration required by incrVacuumStep(). */
3640 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
3643 ** Perform a single step of an incremental-vacuum. If successful, return
3644 ** SQLITE_OK. If there is no work to do (and therefore no point in
3645 ** calling this function again), return SQLITE_DONE. Or, if an error
3646 ** occurs, return some other error code.
3648 ** More specifically, this function attempts to re-organize the database so
3649 ** that the last page of the file currently in use is no longer in use.
3651 ** Parameter nFin is the number of pages that this database would contain
3652 ** were this function called until it returns SQLITE_DONE.
3654 ** If the bCommit parameter is non-zero, this function assumes that the
3655 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
3656 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
3657 ** operation, or false for an incremental vacuum.
3659 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
3660 Pgno nFreeList; /* Number of pages still on the free-list */
3661 int rc;
3663 assert( sqlite3_mutex_held(pBt->mutex) );
3664 assert( iLastPg>nFin );
3666 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
3667 u8 eType;
3668 Pgno iPtrPage;
3670 nFreeList = get4byte(&pBt->pPage1->aData[36]);
3671 if( nFreeList==0 ){
3672 return SQLITE_DONE;
3675 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
3676 if( rc!=SQLITE_OK ){
3677 return rc;
3679 if( eType==PTRMAP_ROOTPAGE ){
3680 return SQLITE_CORRUPT_BKPT;
3683 if( eType==PTRMAP_FREEPAGE ){
3684 if( bCommit==0 ){
3685 /* Remove the page from the files free-list. This is not required
3686 ** if bCommit is non-zero. In that case, the free-list will be
3687 ** truncated to zero after this function returns, so it doesn't
3688 ** matter if it still contains some garbage entries.
3690 Pgno iFreePg;
3691 MemPage *pFreePg;
3692 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
3693 if( rc!=SQLITE_OK ){
3694 return rc;
3696 assert( iFreePg==iLastPg );
3697 releasePage(pFreePg);
3699 } else {
3700 Pgno iFreePg; /* Index of free page to move pLastPg to */
3701 MemPage *pLastPg;
3702 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
3703 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
3705 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
3706 if( rc!=SQLITE_OK ){
3707 return rc;
3710 /* If bCommit is zero, this loop runs exactly once and page pLastPg
3711 ** is swapped with the first free page pulled off the free list.
3713 ** On the other hand, if bCommit is greater than zero, then keep
3714 ** looping until a free-page located within the first nFin pages
3715 ** of the file is found.
3717 if( bCommit==0 ){
3718 eMode = BTALLOC_LE;
3719 iNear = nFin;
3721 do {
3722 MemPage *pFreePg;
3723 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
3724 if( rc!=SQLITE_OK ){
3725 releasePage(pLastPg);
3726 return rc;
3728 releasePage(pFreePg);
3729 }while( bCommit && iFreePg>nFin );
3730 assert( iFreePg<iLastPg );
3732 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
3733 releasePage(pLastPg);
3734 if( rc!=SQLITE_OK ){
3735 return rc;
3740 if( bCommit==0 ){
3741 do {
3742 iLastPg--;
3743 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
3744 pBt->bDoTruncate = 1;
3745 pBt->nPage = iLastPg;
3747 return SQLITE_OK;
3751 ** The database opened by the first argument is an auto-vacuum database
3752 ** nOrig pages in size containing nFree free pages. Return the expected
3753 ** size of the database in pages following an auto-vacuum operation.
3755 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
3756 int nEntry; /* Number of entries on one ptrmap page */
3757 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
3758 Pgno nFin; /* Return value */
3760 nEntry = pBt->usableSize/5;
3761 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
3762 nFin = nOrig - nFree - nPtrmap;
3763 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
3764 nFin--;
3766 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
3767 nFin--;
3770 return nFin;
3774 ** A write-transaction must be opened before calling this function.
3775 ** It performs a single unit of work towards an incremental vacuum.
3777 ** If the incremental vacuum is finished after this function has run,
3778 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
3779 ** SQLITE_OK is returned. Otherwise an SQLite error code.
3781 int sqlite3BtreeIncrVacuum(Btree *p){
3782 int rc;
3783 BtShared *pBt = p->pBt;
3785 sqlite3BtreeEnter(p);
3786 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
3787 if( !pBt->autoVacuum ){
3788 rc = SQLITE_DONE;
3789 }else{
3790 Pgno nOrig = btreePagecount(pBt);
3791 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
3792 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
3794 if( nOrig<nFin ){
3795 rc = SQLITE_CORRUPT_BKPT;
3796 }else if( nFree>0 ){
3797 rc = saveAllCursors(pBt, 0, 0);
3798 if( rc==SQLITE_OK ){
3799 invalidateAllOverflowCache(pBt);
3800 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
3802 if( rc==SQLITE_OK ){
3803 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3804 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
3806 }else{
3807 rc = SQLITE_DONE;
3810 sqlite3BtreeLeave(p);
3811 return rc;
3815 ** This routine is called prior to sqlite3PagerCommit when a transaction
3816 ** is committed for an auto-vacuum database.
3818 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
3819 ** the database file should be truncated to during the commit process.
3820 ** i.e. the database has been reorganized so that only the first *pnTrunc
3821 ** pages are in use.
3823 static int autoVacuumCommit(BtShared *pBt){
3824 int rc = SQLITE_OK;
3825 Pager *pPager = pBt->pPager;
3826 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
3828 assert( sqlite3_mutex_held(pBt->mutex) );
3829 invalidateAllOverflowCache(pBt);
3830 assert(pBt->autoVacuum);
3831 if( !pBt->incrVacuum ){
3832 Pgno nFin; /* Number of pages in database after autovacuuming */
3833 Pgno nFree; /* Number of pages on the freelist initially */
3834 Pgno iFree; /* The next page to be freed */
3835 Pgno nOrig; /* Database size before freeing */
3837 nOrig = btreePagecount(pBt);
3838 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
3839 /* It is not possible to create a database for which the final page
3840 ** is either a pointer-map page or the pending-byte page. If one
3841 ** is encountered, this indicates corruption.
3843 return SQLITE_CORRUPT_BKPT;
3846 nFree = get4byte(&pBt->pPage1->aData[36]);
3847 nFin = finalDbSize(pBt, nOrig, nFree);
3848 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
3849 if( nFin<nOrig ){
3850 rc = saveAllCursors(pBt, 0, 0);
3852 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
3853 rc = incrVacuumStep(pBt, nFin, iFree, 1);
3855 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
3856 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3857 put4byte(&pBt->pPage1->aData[32], 0);
3858 put4byte(&pBt->pPage1->aData[36], 0);
3859 put4byte(&pBt->pPage1->aData[28], nFin);
3860 pBt->bDoTruncate = 1;
3861 pBt->nPage = nFin;
3863 if( rc!=SQLITE_OK ){
3864 sqlite3PagerRollback(pPager);
3868 assert( nRef>=sqlite3PagerRefcount(pPager) );
3869 return rc;
3872 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
3873 # define setChildPtrmaps(x) SQLITE_OK
3874 #endif
3877 ** This routine does the first phase of a two-phase commit. This routine
3878 ** causes a rollback journal to be created (if it does not already exist)
3879 ** and populated with enough information so that if a power loss occurs
3880 ** the database can be restored to its original state by playing back
3881 ** the journal. Then the contents of the journal are flushed out to
3882 ** the disk. After the journal is safely on oxide, the changes to the
3883 ** database are written into the database file and flushed to oxide.
3884 ** At the end of this call, the rollback journal still exists on the
3885 ** disk and we are still holding all locks, so the transaction has not
3886 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
3887 ** commit process.
3889 ** This call is a no-op if no write-transaction is currently active on pBt.
3891 ** Otherwise, sync the database file for the btree pBt. zMaster points to
3892 ** the name of a master journal file that should be written into the
3893 ** individual journal file, or is NULL, indicating no master journal file
3894 ** (single database transaction).
3896 ** When this is called, the master journal should already have been
3897 ** created, populated with this journal pointer and synced to disk.
3899 ** Once this is routine has returned, the only thing required to commit
3900 ** the write-transaction for this database file is to delete the journal.
3902 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
3903 int rc = SQLITE_OK;
3904 if( p->inTrans==TRANS_WRITE ){
3905 BtShared *pBt = p->pBt;
3906 sqlite3BtreeEnter(p);
3907 #ifndef SQLITE_OMIT_AUTOVACUUM
3908 if( pBt->autoVacuum ){
3909 rc = autoVacuumCommit(pBt);
3910 if( rc!=SQLITE_OK ){
3911 sqlite3BtreeLeave(p);
3912 return rc;
3915 if( pBt->bDoTruncate ){
3916 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
3918 #endif
3919 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
3920 sqlite3BtreeLeave(p);
3922 return rc;
3926 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
3927 ** at the conclusion of a transaction.
3929 static void btreeEndTransaction(Btree *p){
3930 BtShared *pBt = p->pBt;
3931 sqlite3 *db = p->db;
3932 assert( sqlite3BtreeHoldsMutex(p) );
3934 #ifndef SQLITE_OMIT_AUTOVACUUM
3935 pBt->bDoTruncate = 0;
3936 #endif
3937 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
3938 /* If there are other active statements that belong to this database
3939 ** handle, downgrade to a read-only transaction. The other statements
3940 ** may still be reading from the database. */
3941 downgradeAllSharedCacheTableLocks(p);
3942 p->inTrans = TRANS_READ;
3943 }else{
3944 /* If the handle had any kind of transaction open, decrement the
3945 ** transaction count of the shared btree. If the transaction count
3946 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
3947 ** call below will unlock the pager. */
3948 if( p->inTrans!=TRANS_NONE ){
3949 clearAllSharedCacheTableLocks(p);
3950 pBt->nTransaction--;
3951 if( 0==pBt->nTransaction ){
3952 pBt->inTransaction = TRANS_NONE;
3956 /* Set the current transaction state to TRANS_NONE and unlock the
3957 ** pager if this call closed the only read or write transaction. */
3958 p->inTrans = TRANS_NONE;
3959 unlockBtreeIfUnused(pBt);
3962 btreeIntegrity(p);
3966 ** Commit the transaction currently in progress.
3968 ** This routine implements the second phase of a 2-phase commit. The
3969 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
3970 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
3971 ** routine did all the work of writing information out to disk and flushing the
3972 ** contents so that they are written onto the disk platter. All this
3973 ** routine has to do is delete or truncate or zero the header in the
3974 ** the rollback journal (which causes the transaction to commit) and
3975 ** drop locks.
3977 ** Normally, if an error occurs while the pager layer is attempting to
3978 ** finalize the underlying journal file, this function returns an error and
3979 ** the upper layer will attempt a rollback. However, if the second argument
3980 ** is non-zero then this b-tree transaction is part of a multi-file
3981 ** transaction. In this case, the transaction has already been committed
3982 ** (by deleting a master journal file) and the caller will ignore this
3983 ** functions return code. So, even if an error occurs in the pager layer,
3984 ** reset the b-tree objects internal state to indicate that the write
3985 ** transaction has been closed. This is quite safe, as the pager will have
3986 ** transitioned to the error state.
3988 ** This will release the write lock on the database file. If there
3989 ** are no active cursors, it also releases the read lock.
3991 int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
3993 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
3994 sqlite3BtreeEnter(p);
3995 btreeIntegrity(p);
3997 /* If the handle has a write-transaction open, commit the shared-btrees
3998 ** transaction and set the shared state to TRANS_READ.
4000 if( p->inTrans==TRANS_WRITE ){
4001 int rc;
4002 BtShared *pBt = p->pBt;
4003 assert( pBt->inTransaction==TRANS_WRITE );
4004 assert( pBt->nTransaction>0 );
4005 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
4006 if( rc!=SQLITE_OK && bCleanup==0 ){
4007 sqlite3BtreeLeave(p);
4008 return rc;
4010 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
4011 pBt->inTransaction = TRANS_READ;
4012 btreeClearHasContent(pBt);
4015 btreeEndTransaction(p);
4016 sqlite3BtreeLeave(p);
4017 return SQLITE_OK;
4021 ** Do both phases of a commit.
4023 int sqlite3BtreeCommit(Btree *p){
4024 int rc;
4025 sqlite3BtreeEnter(p);
4026 rc = sqlite3BtreeCommitPhaseOne(p, 0);
4027 if( rc==SQLITE_OK ){
4028 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
4030 sqlite3BtreeLeave(p);
4031 return rc;
4035 ** This routine sets the state to CURSOR_FAULT and the error
4036 ** code to errCode for every cursor on any BtShared that pBtree
4037 ** references. Or if the writeOnly flag is set to 1, then only
4038 ** trip write cursors and leave read cursors unchanged.
4040 ** Every cursor is a candidate to be tripped, including cursors
4041 ** that belong to other database connections that happen to be
4042 ** sharing the cache with pBtree.
4044 ** This routine gets called when a rollback occurs. If the writeOnly
4045 ** flag is true, then only write-cursors need be tripped - read-only
4046 ** cursors save their current positions so that they may continue
4047 ** following the rollback. Or, if writeOnly is false, all cursors are
4048 ** tripped. In general, writeOnly is false if the transaction being
4049 ** rolled back modified the database schema. In this case b-tree root
4050 ** pages may be moved or deleted from the database altogether, making
4051 ** it unsafe for read cursors to continue.
4053 ** If the writeOnly flag is true and an error is encountered while
4054 ** saving the current position of a read-only cursor, all cursors,
4055 ** including all read-cursors are tripped.
4057 ** SQLITE_OK is returned if successful, or if an error occurs while
4058 ** saving a cursor position, an SQLite error code.
4060 int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
4061 BtCursor *p;
4062 int rc = SQLITE_OK;
4064 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
4065 if( pBtree ){
4066 sqlite3BtreeEnter(pBtree);
4067 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
4068 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
4069 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
4070 rc = saveCursorPosition(p);
4071 if( rc!=SQLITE_OK ){
4072 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
4073 break;
4076 }else{
4077 sqlite3BtreeClearCursor(p);
4078 p->eState = CURSOR_FAULT;
4079 p->skipNext = errCode;
4081 btreeReleaseAllCursorPages(p);
4083 sqlite3BtreeLeave(pBtree);
4085 return rc;
4089 ** Rollback the transaction in progress.
4091 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
4092 ** Only write cursors are tripped if writeOnly is true but all cursors are
4093 ** tripped if writeOnly is false. Any attempt to use
4094 ** a tripped cursor will result in an error.
4096 ** This will release the write lock on the database file. If there
4097 ** are no active cursors, it also releases the read lock.
4099 int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
4100 int rc;
4101 BtShared *pBt = p->pBt;
4102 MemPage *pPage1;
4104 assert( writeOnly==1 || writeOnly==0 );
4105 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
4106 sqlite3BtreeEnter(p);
4107 if( tripCode==SQLITE_OK ){
4108 rc = tripCode = saveAllCursors(pBt, 0, 0);
4109 if( rc ) writeOnly = 0;
4110 }else{
4111 rc = SQLITE_OK;
4113 if( tripCode ){
4114 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
4115 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
4116 if( rc2!=SQLITE_OK ) rc = rc2;
4118 btreeIntegrity(p);
4120 if( p->inTrans==TRANS_WRITE ){
4121 int rc2;
4123 assert( TRANS_WRITE==pBt->inTransaction );
4124 rc2 = sqlite3PagerRollback(pBt->pPager);
4125 if( rc2!=SQLITE_OK ){
4126 rc = rc2;
4129 /* The rollback may have destroyed the pPage1->aData value. So
4130 ** call btreeGetPage() on page 1 again to make
4131 ** sure pPage1->aData is set correctly. */
4132 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
4133 int nPage = get4byte(28+(u8*)pPage1->aData);
4134 testcase( nPage==0 );
4135 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
4136 testcase( pBt->nPage!=nPage );
4137 pBt->nPage = nPage;
4138 releasePageOne(pPage1);
4140 assert( countValidCursors(pBt, 1)==0 );
4141 pBt->inTransaction = TRANS_READ;
4142 btreeClearHasContent(pBt);
4145 btreeEndTransaction(p);
4146 sqlite3BtreeLeave(p);
4147 return rc;
4151 ** Start a statement subtransaction. The subtransaction can be rolled
4152 ** back independently of the main transaction. You must start a transaction
4153 ** before starting a subtransaction. The subtransaction is ended automatically
4154 ** if the main transaction commits or rolls back.
4156 ** Statement subtransactions are used around individual SQL statements
4157 ** that are contained within a BEGIN...COMMIT block. If a constraint
4158 ** error occurs within the statement, the effect of that one statement
4159 ** can be rolled back without having to rollback the entire transaction.
4161 ** A statement sub-transaction is implemented as an anonymous savepoint. The
4162 ** value passed as the second parameter is the total number of savepoints,
4163 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
4164 ** are no active savepoints and no other statement-transactions open,
4165 ** iStatement is 1. This anonymous savepoint can be released or rolled back
4166 ** using the sqlite3BtreeSavepoint() function.
4168 int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
4169 int rc;
4170 BtShared *pBt = p->pBt;
4171 sqlite3BtreeEnter(p);
4172 assert( p->inTrans==TRANS_WRITE );
4173 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
4174 assert( iStatement>0 );
4175 assert( iStatement>p->db->nSavepoint );
4176 assert( pBt->inTransaction==TRANS_WRITE );
4177 /* At the pager level, a statement transaction is a savepoint with
4178 ** an index greater than all savepoints created explicitly using
4179 ** SQL statements. It is illegal to open, release or rollback any
4180 ** such savepoints while the statement transaction savepoint is active.
4182 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
4183 sqlite3BtreeLeave(p);
4184 return rc;
4188 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
4189 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
4190 ** savepoint identified by parameter iSavepoint, depending on the value
4191 ** of op.
4193 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
4194 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
4195 ** contents of the entire transaction are rolled back. This is different
4196 ** from a normal transaction rollback, as no locks are released and the
4197 ** transaction remains open.
4199 int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
4200 int rc = SQLITE_OK;
4201 if( p && p->inTrans==TRANS_WRITE ){
4202 BtShared *pBt = p->pBt;
4203 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4204 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
4205 sqlite3BtreeEnter(p);
4206 if( op==SAVEPOINT_ROLLBACK ){
4207 rc = saveAllCursors(pBt, 0, 0);
4209 if( rc==SQLITE_OK ){
4210 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
4212 if( rc==SQLITE_OK ){
4213 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
4214 pBt->nPage = 0;
4216 rc = newDatabase(pBt);
4217 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
4219 /* The database size was written into the offset 28 of the header
4220 ** when the transaction started, so we know that the value at offset
4221 ** 28 is nonzero. */
4222 assert( pBt->nPage>0 );
4224 sqlite3BtreeLeave(p);
4226 return rc;
4230 ** Create a new cursor for the BTree whose root is on the page
4231 ** iTable. If a read-only cursor is requested, it is assumed that
4232 ** the caller already has at least a read-only transaction open
4233 ** on the database already. If a write-cursor is requested, then
4234 ** the caller is assumed to have an open write transaction.
4236 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
4237 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
4238 ** can be used for reading or for writing if other conditions for writing
4239 ** are also met. These are the conditions that must be met in order
4240 ** for writing to be allowed:
4242 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
4244 ** 2: Other database connections that share the same pager cache
4245 ** but which are not in the READ_UNCOMMITTED state may not have
4246 ** cursors open with wrFlag==0 on the same table. Otherwise
4247 ** the changes made by this write cursor would be visible to
4248 ** the read cursors in the other database connection.
4250 ** 3: The database must be writable (not on read-only media)
4252 ** 4: There must be an active transaction.
4254 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
4255 ** is set. If FORDELETE is set, that is a hint to the implementation that
4256 ** this cursor will only be used to seek to and delete entries of an index
4257 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
4258 ** this implementation. But in a hypothetical alternative storage engine
4259 ** in which index entries are automatically deleted when corresponding table
4260 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
4261 ** operations on this cursor can be no-ops and all READ operations can
4262 ** return a null row (2-bytes: 0x01 0x00).
4264 ** No checking is done to make sure that page iTable really is the
4265 ** root page of a b-tree. If it is not, then the cursor acquired
4266 ** will not work correctly.
4268 ** It is assumed that the sqlite3BtreeCursorZero() has been called
4269 ** on pCur to initialize the memory space prior to invoking this routine.
4271 static int btreeCursor(
4272 Btree *p, /* The btree */
4273 int iTable, /* Root page of table to open */
4274 int wrFlag, /* 1 to write. 0 read-only */
4275 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4276 BtCursor *pCur /* Space for new cursor */
4278 BtShared *pBt = p->pBt; /* Shared b-tree handle */
4279 BtCursor *pX; /* Looping over other all cursors */
4281 assert( sqlite3BtreeHoldsMutex(p) );
4282 assert( wrFlag==0
4283 || wrFlag==BTREE_WRCSR
4284 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
4287 /* The following assert statements verify that if this is a sharable
4288 ** b-tree database, the connection is holding the required table locks,
4289 ** and that no other connection has any open cursor that conflicts with
4290 ** this lock. */
4291 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
4292 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
4294 /* Assert that the caller has opened the required transaction. */
4295 assert( p->inTrans>TRANS_NONE );
4296 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
4297 assert( pBt->pPage1 && pBt->pPage1->aData );
4298 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
4300 if( wrFlag ){
4301 allocateTempSpace(pBt);
4302 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
4304 if( iTable==1 && btreePagecount(pBt)==0 ){
4305 assert( wrFlag==0 );
4306 iTable = 0;
4309 /* Now that no other errors can occur, finish filling in the BtCursor
4310 ** variables and link the cursor into the BtShared list. */
4311 pCur->pgnoRoot = (Pgno)iTable;
4312 pCur->iPage = -1;
4313 pCur->pKeyInfo = pKeyInfo;
4314 pCur->pBtree = p;
4315 pCur->pBt = pBt;
4316 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
4317 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
4318 /* If there are two or more cursors on the same btree, then all such
4319 ** cursors *must* have the BTCF_Multiple flag set. */
4320 for(pX=pBt->pCursor; pX; pX=pX->pNext){
4321 if( pX->pgnoRoot==(Pgno)iTable ){
4322 pX->curFlags |= BTCF_Multiple;
4323 pCur->curFlags |= BTCF_Multiple;
4326 pCur->pNext = pBt->pCursor;
4327 pBt->pCursor = pCur;
4328 pCur->eState = CURSOR_INVALID;
4329 return SQLITE_OK;
4331 int sqlite3BtreeCursor(
4332 Btree *p, /* The btree */
4333 int iTable, /* Root page of table to open */
4334 int wrFlag, /* 1 to write. 0 read-only */
4335 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
4336 BtCursor *pCur /* Write new cursor here */
4338 int rc;
4339 if( iTable<1 ){
4340 rc = SQLITE_CORRUPT_BKPT;
4341 }else{
4342 sqlite3BtreeEnter(p);
4343 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4344 sqlite3BtreeLeave(p);
4346 return rc;
4350 ** Return the size of a BtCursor object in bytes.
4352 ** This interfaces is needed so that users of cursors can preallocate
4353 ** sufficient storage to hold a cursor. The BtCursor object is opaque
4354 ** to users so they cannot do the sizeof() themselves - they must call
4355 ** this routine.
4357 int sqlite3BtreeCursorSize(void){
4358 return ROUND8(sizeof(BtCursor));
4362 ** Initialize memory that will be converted into a BtCursor object.
4364 ** The simple approach here would be to memset() the entire object
4365 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
4366 ** do not need to be zeroed and they are large, so we can save a lot
4367 ** of run-time by skipping the initialization of those elements.
4369 void sqlite3BtreeCursorZero(BtCursor *p){
4370 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
4374 ** Close a cursor. The read lock on the database file is released
4375 ** when the last cursor is closed.
4377 int sqlite3BtreeCloseCursor(BtCursor *pCur){
4378 Btree *pBtree = pCur->pBtree;
4379 if( pBtree ){
4380 BtShared *pBt = pCur->pBt;
4381 sqlite3BtreeEnter(pBtree);
4382 assert( pBt->pCursor!=0 );
4383 if( pBt->pCursor==pCur ){
4384 pBt->pCursor = pCur->pNext;
4385 }else{
4386 BtCursor *pPrev = pBt->pCursor;
4388 if( pPrev->pNext==pCur ){
4389 pPrev->pNext = pCur->pNext;
4390 break;
4392 pPrev = pPrev->pNext;
4393 }while( ALWAYS(pPrev) );
4395 btreeReleaseAllCursorPages(pCur);
4396 unlockBtreeIfUnused(pBt);
4397 sqlite3_free(pCur->aOverflow);
4398 sqlite3_free(pCur->pKey);
4399 sqlite3BtreeLeave(pBtree);
4401 return SQLITE_OK;
4405 ** Make sure the BtCursor* given in the argument has a valid
4406 ** BtCursor.info structure. If it is not already valid, call
4407 ** btreeParseCell() to fill it in.
4409 ** BtCursor.info is a cache of the information in the current cell.
4410 ** Using this cache reduces the number of calls to btreeParseCell().
4412 #ifndef NDEBUG
4413 static int cellInfoEqual(CellInfo *a, CellInfo *b){
4414 if( a->nKey!=b->nKey ) return 0;
4415 if( a->pPayload!=b->pPayload ) return 0;
4416 if( a->nPayload!=b->nPayload ) return 0;
4417 if( a->nLocal!=b->nLocal ) return 0;
4418 if( a->nSize!=b->nSize ) return 0;
4419 return 1;
4421 static void assertCellInfo(BtCursor *pCur){
4422 CellInfo info;
4423 memset(&info, 0, sizeof(info));
4424 btreeParseCell(pCur->pPage, pCur->ix, &info);
4425 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
4427 #else
4428 #define assertCellInfo(x)
4429 #endif
4430 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
4431 if( pCur->info.nSize==0 ){
4432 pCur->curFlags |= BTCF_ValidNKey;
4433 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
4434 }else{
4435 assertCellInfo(pCur);
4439 #ifndef NDEBUG /* The next routine used only within assert() statements */
4441 ** Return true if the given BtCursor is valid. A valid cursor is one
4442 ** that is currently pointing to a row in a (non-empty) table.
4443 ** This is a verification routine is used only within assert() statements.
4445 int sqlite3BtreeCursorIsValid(BtCursor *pCur){
4446 return pCur && pCur->eState==CURSOR_VALID;
4448 #endif /* NDEBUG */
4449 int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
4450 assert( pCur!=0 );
4451 return pCur->eState==CURSOR_VALID;
4455 ** Return the value of the integer key or "rowid" for a table btree.
4456 ** This routine is only valid for a cursor that is pointing into a
4457 ** ordinary table btree. If the cursor points to an index btree or
4458 ** is invalid, the result of this routine is undefined.
4460 i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
4461 assert( cursorHoldsMutex(pCur) );
4462 assert( pCur->eState==CURSOR_VALID );
4463 assert( pCur->curIntKey );
4464 getCellInfo(pCur);
4465 return pCur->info.nKey;
4468 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
4470 ** Return the offset into the database file for the start of the
4471 ** payload to which the cursor is pointing.
4473 i64 sqlite3BtreeOffset(BtCursor *pCur){
4474 assert( cursorHoldsMutex(pCur) );
4475 assert( pCur->eState==CURSOR_VALID );
4476 getCellInfo(pCur);
4477 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
4478 (i64)(pCur->info.pPayload - pCur->pPage->aData);
4480 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
4483 ** Return the number of bytes of payload for the entry that pCur is
4484 ** currently pointing to. For table btrees, this will be the amount
4485 ** of data. For index btrees, this will be the size of the key.
4487 ** The caller must guarantee that the cursor is pointing to a non-NULL
4488 ** valid entry. In other words, the calling procedure must guarantee
4489 ** that the cursor has Cursor.eState==CURSOR_VALID.
4491 u32 sqlite3BtreePayloadSize(BtCursor *pCur){
4492 assert( cursorHoldsMutex(pCur) );
4493 assert( pCur->eState==CURSOR_VALID );
4494 getCellInfo(pCur);
4495 return pCur->info.nPayload;
4499 ** Given the page number of an overflow page in the database (parameter
4500 ** ovfl), this function finds the page number of the next page in the
4501 ** linked list of overflow pages. If possible, it uses the auto-vacuum
4502 ** pointer-map data instead of reading the content of page ovfl to do so.
4504 ** If an error occurs an SQLite error code is returned. Otherwise:
4506 ** The page number of the next overflow page in the linked list is
4507 ** written to *pPgnoNext. If page ovfl is the last page in its linked
4508 ** list, *pPgnoNext is set to zero.
4510 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
4511 ** to page number pOvfl was obtained, then *ppPage is set to point to that
4512 ** reference. It is the responsibility of the caller to call releasePage()
4513 ** on *ppPage to free the reference. In no reference was obtained (because
4514 ** the pointer-map was used to obtain the value for *pPgnoNext), then
4515 ** *ppPage is set to zero.
4517 static int getOverflowPage(
4518 BtShared *pBt, /* The database file */
4519 Pgno ovfl, /* Current overflow page number */
4520 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
4521 Pgno *pPgnoNext /* OUT: Next overflow page number */
4523 Pgno next = 0;
4524 MemPage *pPage = 0;
4525 int rc = SQLITE_OK;
4527 assert( sqlite3_mutex_held(pBt->mutex) );
4528 assert(pPgnoNext);
4530 #ifndef SQLITE_OMIT_AUTOVACUUM
4531 /* Try to find the next page in the overflow list using the
4532 ** autovacuum pointer-map pages. Guess that the next page in
4533 ** the overflow list is page number (ovfl+1). If that guess turns
4534 ** out to be wrong, fall back to loading the data of page
4535 ** number ovfl to determine the next page number.
4537 if( pBt->autoVacuum ){
4538 Pgno pgno;
4539 Pgno iGuess = ovfl+1;
4540 u8 eType;
4542 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
4543 iGuess++;
4546 if( iGuess<=btreePagecount(pBt) ){
4547 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
4548 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
4549 next = iGuess;
4550 rc = SQLITE_DONE;
4554 #endif
4556 assert( next==0 || rc==SQLITE_DONE );
4557 if( rc==SQLITE_OK ){
4558 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
4559 assert( rc==SQLITE_OK || pPage==0 );
4560 if( rc==SQLITE_OK ){
4561 next = get4byte(pPage->aData);
4565 *pPgnoNext = next;
4566 if( ppPage ){
4567 *ppPage = pPage;
4568 }else{
4569 releasePage(pPage);
4571 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
4575 ** Copy data from a buffer to a page, or from a page to a buffer.
4577 ** pPayload is a pointer to data stored on database page pDbPage.
4578 ** If argument eOp is false, then nByte bytes of data are copied
4579 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
4580 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
4581 ** of data are copied from the buffer pBuf to pPayload.
4583 ** SQLITE_OK is returned on success, otherwise an error code.
4585 static int copyPayload(
4586 void *pPayload, /* Pointer to page data */
4587 void *pBuf, /* Pointer to buffer */
4588 int nByte, /* Number of bytes to copy */
4589 int eOp, /* 0 -> copy from page, 1 -> copy to page */
4590 DbPage *pDbPage /* Page containing pPayload */
4592 if( eOp ){
4593 /* Copy data from buffer to page (a write operation) */
4594 int rc = sqlite3PagerWrite(pDbPage);
4595 if( rc!=SQLITE_OK ){
4596 return rc;
4598 memcpy(pPayload, pBuf, nByte);
4599 }else{
4600 /* Copy data from page to buffer (a read operation) */
4601 memcpy(pBuf, pPayload, nByte);
4603 return SQLITE_OK;
4607 ** This function is used to read or overwrite payload information
4608 ** for the entry that the pCur cursor is pointing to. The eOp
4609 ** argument is interpreted as follows:
4611 ** 0: The operation is a read. Populate the overflow cache.
4612 ** 1: The operation is a write. Populate the overflow cache.
4614 ** A total of "amt" bytes are read or written beginning at "offset".
4615 ** Data is read to or from the buffer pBuf.
4617 ** The content being read or written might appear on the main page
4618 ** or be scattered out on multiple overflow pages.
4620 ** If the current cursor entry uses one or more overflow pages
4621 ** this function may allocate space for and lazily populate
4622 ** the overflow page-list cache array (BtCursor.aOverflow).
4623 ** Subsequent calls use this cache to make seeking to the supplied offset
4624 ** more efficient.
4626 ** Once an overflow page-list cache has been allocated, it must be
4627 ** invalidated if some other cursor writes to the same table, or if
4628 ** the cursor is moved to a different row. Additionally, in auto-vacuum
4629 ** mode, the following events may invalidate an overflow page-list cache.
4631 ** * An incremental vacuum,
4632 ** * A commit in auto_vacuum="full" mode,
4633 ** * Creating a table (may require moving an overflow page).
4635 static int accessPayload(
4636 BtCursor *pCur, /* Cursor pointing to entry to read from */
4637 u32 offset, /* Begin reading this far into payload */
4638 u32 amt, /* Read this many bytes */
4639 unsigned char *pBuf, /* Write the bytes into this buffer */
4640 int eOp /* zero to read. non-zero to write. */
4642 unsigned char *aPayload;
4643 int rc = SQLITE_OK;
4644 int iIdx = 0;
4645 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
4646 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
4647 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4648 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
4649 #endif
4651 assert( pPage );
4652 assert( eOp==0 || eOp==1 );
4653 assert( pCur->eState==CURSOR_VALID );
4654 assert( pCur->ix<pPage->nCell );
4655 assert( cursorHoldsMutex(pCur) );
4657 getCellInfo(pCur);
4658 aPayload = pCur->info.pPayload;
4659 assert( offset+amt <= pCur->info.nPayload );
4661 assert( aPayload > pPage->aData );
4662 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
4663 /* Trying to read or write past the end of the data is an error. The
4664 ** conditional above is really:
4665 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
4666 ** but is recast into its current form to avoid integer overflow problems
4668 return SQLITE_CORRUPT_PAGE(pPage);
4671 /* Check if data must be read/written to/from the btree page itself. */
4672 if( offset<pCur->info.nLocal ){
4673 int a = amt;
4674 if( a+offset>pCur->info.nLocal ){
4675 a = pCur->info.nLocal - offset;
4677 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
4678 offset = 0;
4679 pBuf += a;
4680 amt -= a;
4681 }else{
4682 offset -= pCur->info.nLocal;
4686 if( rc==SQLITE_OK && amt>0 ){
4687 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
4688 Pgno nextPage;
4690 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
4692 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
4694 ** The aOverflow[] array is sized at one entry for each overflow page
4695 ** in the overflow chain. The page number of the first overflow page is
4696 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
4697 ** means "not yet known" (the cache is lazily populated).
4699 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
4700 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
4701 if( pCur->aOverflow==0
4702 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
4704 Pgno *aNew = (Pgno*)sqlite3Realloc(
4705 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
4707 if( aNew==0 ){
4708 return SQLITE_NOMEM_BKPT;
4709 }else{
4710 pCur->aOverflow = aNew;
4713 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
4714 pCur->curFlags |= BTCF_ValidOvfl;
4715 }else{
4716 /* If the overflow page-list cache has been allocated and the
4717 ** entry for the first required overflow page is valid, skip
4718 ** directly to it.
4720 if( pCur->aOverflow[offset/ovflSize] ){
4721 iIdx = (offset/ovflSize);
4722 nextPage = pCur->aOverflow[iIdx];
4723 offset = (offset%ovflSize);
4727 assert( rc==SQLITE_OK && amt>0 );
4728 while( nextPage ){
4729 /* If required, populate the overflow page-list cache. */
4730 assert( pCur->aOverflow[iIdx]==0
4731 || pCur->aOverflow[iIdx]==nextPage
4732 || CORRUPT_DB );
4733 pCur->aOverflow[iIdx] = nextPage;
4735 if( offset>=ovflSize ){
4736 /* The only reason to read this page is to obtain the page
4737 ** number for the next page in the overflow chain. The page
4738 ** data is not required. So first try to lookup the overflow
4739 ** page-list cache, if any, then fall back to the getOverflowPage()
4740 ** function.
4742 assert( pCur->curFlags & BTCF_ValidOvfl );
4743 assert( pCur->pBtree->db==pBt->db );
4744 if( pCur->aOverflow[iIdx+1] ){
4745 nextPage = pCur->aOverflow[iIdx+1];
4746 }else{
4747 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
4749 offset -= ovflSize;
4750 }else{
4751 /* Need to read this page properly. It contains some of the
4752 ** range of data that is being read (eOp==0) or written (eOp!=0).
4754 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4755 sqlite3_file *fd; /* File from which to do direct overflow read */
4756 #endif
4757 int a = amt;
4758 if( a + offset > ovflSize ){
4759 a = ovflSize - offset;
4762 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4763 /* If all the following are true:
4765 ** 1) this is a read operation, and
4766 ** 2) data is required from the start of this overflow page, and
4767 ** 3) there is no open write-transaction, and
4768 ** 4) the database is file-backed, and
4769 ** 5) the page is not in the WAL file
4770 ** 6) at least 4 bytes have already been read into the output buffer
4772 ** then data can be read directly from the database file into the
4773 ** output buffer, bypassing the page-cache altogether. This speeds
4774 ** up loading large records that span many overflow pages.
4776 if( eOp==0 /* (1) */
4777 && offset==0 /* (2) */
4778 && pBt->inTransaction==TRANS_READ /* (3) */
4779 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
4780 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
4781 && &pBuf[-4]>=pBufStart /* (6) */
4783 u8 aSave[4];
4784 u8 *aWrite = &pBuf[-4];
4785 assert( aWrite>=pBufStart ); /* due to (6) */
4786 memcpy(aSave, aWrite, 4);
4787 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
4788 nextPage = get4byte(aWrite);
4789 memcpy(aWrite, aSave, 4);
4790 }else
4791 #endif
4794 DbPage *pDbPage;
4795 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
4796 (eOp==0 ? PAGER_GET_READONLY : 0)
4798 if( rc==SQLITE_OK ){
4799 aPayload = sqlite3PagerGetData(pDbPage);
4800 nextPage = get4byte(aPayload);
4801 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
4802 sqlite3PagerUnref(pDbPage);
4803 offset = 0;
4806 amt -= a;
4807 if( amt==0 ) return rc;
4808 pBuf += a;
4810 if( rc ) break;
4811 iIdx++;
4815 if( rc==SQLITE_OK && amt>0 ){
4816 /* Overflow chain ends prematurely */
4817 return SQLITE_CORRUPT_PAGE(pPage);
4819 return rc;
4823 ** Read part of the payload for the row at which that cursor pCur is currently
4824 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
4825 ** begins at "offset".
4827 ** pCur can be pointing to either a table or an index b-tree.
4828 ** If pointing to a table btree, then the content section is read. If
4829 ** pCur is pointing to an index b-tree then the key section is read.
4831 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
4832 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
4833 ** cursor might be invalid or might need to be restored before being read.
4835 ** Return SQLITE_OK on success or an error code if anything goes
4836 ** wrong. An error is returned if "offset+amt" is larger than
4837 ** the available payload.
4839 int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
4840 assert( cursorHoldsMutex(pCur) );
4841 assert( pCur->eState==CURSOR_VALID );
4842 assert( pCur->iPage>=0 && pCur->pPage );
4843 assert( pCur->ix<pCur->pPage->nCell );
4844 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
4848 ** This variant of sqlite3BtreePayload() works even if the cursor has not
4849 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
4850 ** interface.
4852 #ifndef SQLITE_OMIT_INCRBLOB
4853 static SQLITE_NOINLINE int accessPayloadChecked(
4854 BtCursor *pCur,
4855 u32 offset,
4856 u32 amt,
4857 void *pBuf
4859 int rc;
4860 if ( pCur->eState==CURSOR_INVALID ){
4861 return SQLITE_ABORT;
4863 assert( cursorOwnsBtShared(pCur) );
4864 rc = btreeRestoreCursorPosition(pCur);
4865 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
4867 int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
4868 if( pCur->eState==CURSOR_VALID ){
4869 assert( cursorOwnsBtShared(pCur) );
4870 return accessPayload(pCur, offset, amt, pBuf, 0);
4871 }else{
4872 return accessPayloadChecked(pCur, offset, amt, pBuf);
4875 #endif /* SQLITE_OMIT_INCRBLOB */
4878 ** Return a pointer to payload information from the entry that the
4879 ** pCur cursor is pointing to. The pointer is to the beginning of
4880 ** the key if index btrees (pPage->intKey==0) and is the data for
4881 ** table btrees (pPage->intKey==1). The number of bytes of available
4882 ** key/data is written into *pAmt. If *pAmt==0, then the value
4883 ** returned will not be a valid pointer.
4885 ** This routine is an optimization. It is common for the entire key
4886 ** and data to fit on the local page and for there to be no overflow
4887 ** pages. When that is so, this routine can be used to access the
4888 ** key and data without making a copy. If the key and/or data spills
4889 ** onto overflow pages, then accessPayload() must be used to reassemble
4890 ** the key/data and copy it into a preallocated buffer.
4892 ** The pointer returned by this routine looks directly into the cached
4893 ** page of the database. The data might change or move the next time
4894 ** any btree routine is called.
4896 static const void *fetchPayload(
4897 BtCursor *pCur, /* Cursor pointing to entry to read from */
4898 u32 *pAmt /* Write the number of available bytes here */
4900 int amt;
4901 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
4902 assert( pCur->eState==CURSOR_VALID );
4903 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
4904 assert( cursorOwnsBtShared(pCur) );
4905 assert( pCur->ix<pCur->pPage->nCell );
4906 assert( pCur->info.nSize>0 );
4907 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
4908 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
4909 amt = pCur->info.nLocal;
4910 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
4911 /* There is too little space on the page for the expected amount
4912 ** of local content. Database must be corrupt. */
4913 assert( CORRUPT_DB );
4914 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
4916 *pAmt = (u32)amt;
4917 return (void*)pCur->info.pPayload;
4922 ** For the entry that cursor pCur is point to, return as
4923 ** many bytes of the key or data as are available on the local
4924 ** b-tree page. Write the number of available bytes into *pAmt.
4926 ** The pointer returned is ephemeral. The key/data may move
4927 ** or be destroyed on the next call to any Btree routine,
4928 ** including calls from other threads against the same cache.
4929 ** Hence, a mutex on the BtShared should be held prior to calling
4930 ** this routine.
4932 ** These routines is used to get quick access to key and data
4933 ** in the common case where no overflow pages are used.
4935 const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
4936 return fetchPayload(pCur, pAmt);
4941 ** Move the cursor down to a new child page. The newPgno argument is the
4942 ** page number of the child page to move to.
4944 ** This function returns SQLITE_CORRUPT if the page-header flags field of
4945 ** the new child page does not match the flags field of the parent (i.e.
4946 ** if an intkey page appears to be the parent of a non-intkey page, or
4947 ** vice-versa).
4949 static int moveToChild(BtCursor *pCur, u32 newPgno){
4950 BtShared *pBt = pCur->pBt;
4952 assert( cursorOwnsBtShared(pCur) );
4953 assert( pCur->eState==CURSOR_VALID );
4954 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
4955 assert( pCur->iPage>=0 );
4956 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
4957 return SQLITE_CORRUPT_BKPT;
4959 pCur->info.nSize = 0;
4960 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
4961 pCur->aiIdx[pCur->iPage] = pCur->ix;
4962 pCur->apPage[pCur->iPage] = pCur->pPage;
4963 pCur->ix = 0;
4964 pCur->iPage++;
4965 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
4968 #ifdef SQLITE_DEBUG
4970 ** Page pParent is an internal (non-leaf) tree page. This function
4971 ** asserts that page number iChild is the left-child if the iIdx'th
4972 ** cell in page pParent. Or, if iIdx is equal to the total number of
4973 ** cells in pParent, that page number iChild is the right-child of
4974 ** the page.
4976 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
4977 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
4978 ** in a corrupt database */
4979 assert( iIdx<=pParent->nCell );
4980 if( iIdx==pParent->nCell ){
4981 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
4982 }else{
4983 assert( get4byte(findCell(pParent, iIdx))==iChild );
4986 #else
4987 # define assertParentIndex(x,y,z)
4988 #endif
4991 ** Move the cursor up to the parent page.
4993 ** pCur->idx is set to the cell index that contains the pointer
4994 ** to the page we are coming from. If we are coming from the
4995 ** right-most child page then pCur->idx is set to one more than
4996 ** the largest cell index.
4998 static void moveToParent(BtCursor *pCur){
4999 MemPage *pLeaf;
5000 assert( cursorOwnsBtShared(pCur) );
5001 assert( pCur->eState==CURSOR_VALID );
5002 assert( pCur->iPage>0 );
5003 assert( pCur->pPage );
5004 assertParentIndex(
5005 pCur->apPage[pCur->iPage-1],
5006 pCur->aiIdx[pCur->iPage-1],
5007 pCur->pPage->pgno
5009 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
5010 pCur->info.nSize = 0;
5011 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5012 pCur->ix = pCur->aiIdx[pCur->iPage-1];
5013 pLeaf = pCur->pPage;
5014 pCur->pPage = pCur->apPage[--pCur->iPage];
5015 releasePageNotNull(pLeaf);
5019 ** Move the cursor to point to the root page of its b-tree structure.
5021 ** If the table has a virtual root page, then the cursor is moved to point
5022 ** to the virtual root page instead of the actual root page. A table has a
5023 ** virtual root page when the actual root page contains no cells and a
5024 ** single child page. This can only happen with the table rooted at page 1.
5026 ** If the b-tree structure is empty, the cursor state is set to
5027 ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
5028 ** the cursor is set to point to the first cell located on the root
5029 ** (or virtual root) page and the cursor state is set to CURSOR_VALID.
5031 ** If this function returns successfully, it may be assumed that the
5032 ** page-header flags indicate that the [virtual] root-page is the expected
5033 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
5034 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
5035 ** indicating a table b-tree, or if the caller did specify a KeyInfo
5036 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
5037 ** b-tree).
5039 static int moveToRoot(BtCursor *pCur){
5040 MemPage *pRoot;
5041 int rc = SQLITE_OK;
5043 assert( cursorOwnsBtShared(pCur) );
5044 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
5045 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
5046 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
5047 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
5048 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
5050 if( pCur->iPage>=0 ){
5051 if( pCur->iPage ){
5052 releasePageNotNull(pCur->pPage);
5053 while( --pCur->iPage ){
5054 releasePageNotNull(pCur->apPage[pCur->iPage]);
5056 pCur->pPage = pCur->apPage[0];
5057 goto skip_init;
5059 }else if( pCur->pgnoRoot==0 ){
5060 pCur->eState = CURSOR_INVALID;
5061 return SQLITE_EMPTY;
5062 }else{
5063 assert( pCur->iPage==(-1) );
5064 if( pCur->eState>=CURSOR_REQUIRESEEK ){
5065 if( pCur->eState==CURSOR_FAULT ){
5066 assert( pCur->skipNext!=SQLITE_OK );
5067 return pCur->skipNext;
5069 sqlite3BtreeClearCursor(pCur);
5071 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
5072 0, pCur->curPagerFlags);
5073 if( rc!=SQLITE_OK ){
5074 pCur->eState = CURSOR_INVALID;
5075 return rc;
5077 pCur->iPage = 0;
5078 pCur->curIntKey = pCur->pPage->intKey;
5080 pRoot = pCur->pPage;
5081 assert( pRoot->pgno==pCur->pgnoRoot );
5083 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
5084 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
5085 ** NULL, the caller expects a table b-tree. If this is not the case,
5086 ** return an SQLITE_CORRUPT error.
5088 ** Earlier versions of SQLite assumed that this test could not fail
5089 ** if the root page was already loaded when this function was called (i.e.
5090 ** if pCur->iPage>=0). But this is not so if the database is corrupted
5091 ** in such a way that page pRoot is linked into a second b-tree table
5092 ** (or the freelist). */
5093 assert( pRoot->intKey==1 || pRoot->intKey==0 );
5094 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
5095 return SQLITE_CORRUPT_PAGE(pCur->pPage);
5098 skip_init:
5099 pCur->ix = 0;
5100 pCur->info.nSize = 0;
5101 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
5103 pRoot = pCur->pPage;
5104 if( pRoot->nCell>0 ){
5105 pCur->eState = CURSOR_VALID;
5106 }else if( !pRoot->leaf ){
5107 Pgno subpage;
5108 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
5109 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
5110 pCur->eState = CURSOR_VALID;
5111 rc = moveToChild(pCur, subpage);
5112 }else{
5113 pCur->eState = CURSOR_INVALID;
5114 rc = SQLITE_EMPTY;
5116 return rc;
5120 ** Move the cursor down to the left-most leaf entry beneath the
5121 ** entry to which it is currently pointing.
5123 ** The left-most leaf is the one with the smallest key - the first
5124 ** in ascending order.
5126 static int moveToLeftmost(BtCursor *pCur){
5127 Pgno pgno;
5128 int rc = SQLITE_OK;
5129 MemPage *pPage;
5131 assert( cursorOwnsBtShared(pCur) );
5132 assert( pCur->eState==CURSOR_VALID );
5133 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
5134 assert( pCur->ix<pPage->nCell );
5135 pgno = get4byte(findCell(pPage, pCur->ix));
5136 rc = moveToChild(pCur, pgno);
5138 return rc;
5142 ** Move the cursor down to the right-most leaf entry beneath the
5143 ** page to which it is currently pointing. Notice the difference
5144 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
5145 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
5146 ** finds the right-most entry beneath the *page*.
5148 ** The right-most entry is the one with the largest key - the last
5149 ** key in ascending order.
5151 static int moveToRightmost(BtCursor *pCur){
5152 Pgno pgno;
5153 int rc = SQLITE_OK;
5154 MemPage *pPage = 0;
5156 assert( cursorOwnsBtShared(pCur) );
5157 assert( pCur->eState==CURSOR_VALID );
5158 while( !(pPage = pCur->pPage)->leaf ){
5159 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5160 pCur->ix = pPage->nCell;
5161 rc = moveToChild(pCur, pgno);
5162 if( rc ) return rc;
5164 pCur->ix = pPage->nCell-1;
5165 assert( pCur->info.nSize==0 );
5166 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
5167 return SQLITE_OK;
5170 /* Move the cursor to the first entry in the table. Return SQLITE_OK
5171 ** on success. Set *pRes to 0 if the cursor actually points to something
5172 ** or set *pRes to 1 if the table is empty.
5174 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
5175 int rc;
5177 assert( cursorOwnsBtShared(pCur) );
5178 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5179 rc = moveToRoot(pCur);
5180 if( rc==SQLITE_OK ){
5181 assert( pCur->pPage->nCell>0 );
5182 *pRes = 0;
5183 rc = moveToLeftmost(pCur);
5184 }else if( rc==SQLITE_EMPTY ){
5185 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5186 *pRes = 1;
5187 rc = SQLITE_OK;
5189 return rc;
5193 ** This function is a no-op if cursor pCur does not point to a valid row.
5194 ** Otherwise, if pCur is valid, configure it so that the next call to
5195 ** sqlite3BtreeNext() is a no-op.
5197 #ifndef SQLITE_OMIT_WINDOWFUNC
5198 void sqlite3BtreeSkipNext(BtCursor *pCur){
5199 if( pCur->eState==CURSOR_VALID ){
5200 pCur->eState = CURSOR_SKIPNEXT;
5201 pCur->skipNext = 1;
5204 #endif /* SQLITE_OMIT_WINDOWFUNC */
5206 /* Move the cursor to the last entry in the table. Return SQLITE_OK
5207 ** on success. Set *pRes to 0 if the cursor actually points to something
5208 ** or set *pRes to 1 if the table is empty.
5210 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
5211 int rc;
5213 assert( cursorOwnsBtShared(pCur) );
5214 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5216 /* If the cursor already points to the last entry, this is a no-op. */
5217 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
5218 #ifdef SQLITE_DEBUG
5219 /* This block serves to assert() that the cursor really does point
5220 ** to the last entry in the b-tree. */
5221 int ii;
5222 for(ii=0; ii<pCur->iPage; ii++){
5223 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
5225 assert( pCur->ix==pCur->pPage->nCell-1 );
5226 assert( pCur->pPage->leaf );
5227 #endif
5228 return SQLITE_OK;
5231 rc = moveToRoot(pCur);
5232 if( rc==SQLITE_OK ){
5233 assert( pCur->eState==CURSOR_VALID );
5234 *pRes = 0;
5235 rc = moveToRightmost(pCur);
5236 if( rc==SQLITE_OK ){
5237 pCur->curFlags |= BTCF_AtLast;
5238 }else{
5239 pCur->curFlags &= ~BTCF_AtLast;
5241 }else if( rc==SQLITE_EMPTY ){
5242 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5243 *pRes = 1;
5244 rc = SQLITE_OK;
5246 return rc;
5249 /* Move the cursor so that it points to an entry near the key
5250 ** specified by pIdxKey or intKey. Return a success code.
5252 ** For INTKEY tables, the intKey parameter is used. pIdxKey
5253 ** must be NULL. For index tables, pIdxKey is used and intKey
5254 ** is ignored.
5256 ** If an exact match is not found, then the cursor is always
5257 ** left pointing at a leaf page which would hold the entry if it
5258 ** were present. The cursor might point to an entry that comes
5259 ** before or after the key.
5261 ** An integer is written into *pRes which is the result of
5262 ** comparing the key with the entry to which the cursor is
5263 ** pointing. The meaning of the integer written into
5264 ** *pRes is as follows:
5266 ** *pRes<0 The cursor is left pointing at an entry that
5267 ** is smaller than intKey/pIdxKey or if the table is empty
5268 ** and the cursor is therefore left point to nothing.
5270 ** *pRes==0 The cursor is left pointing at an entry that
5271 ** exactly matches intKey/pIdxKey.
5273 ** *pRes>0 The cursor is left pointing at an entry that
5274 ** is larger than intKey/pIdxKey.
5276 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
5277 ** exists an entry in the table that exactly matches pIdxKey.
5279 int sqlite3BtreeMovetoUnpacked(
5280 BtCursor *pCur, /* The cursor to be moved */
5281 UnpackedRecord *pIdxKey, /* Unpacked index key */
5282 i64 intKey, /* The table key */
5283 int biasRight, /* If true, bias the search to the high end */
5284 int *pRes /* Write search results here */
5286 int rc;
5287 RecordCompare xRecordCompare;
5289 assert( cursorOwnsBtShared(pCur) );
5290 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5291 assert( pRes );
5292 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
5293 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
5295 /* If the cursor is already positioned at the point we are trying
5296 ** to move to, then just return without doing any work */
5297 if( pIdxKey==0
5298 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
5300 if( pCur->info.nKey==intKey ){
5301 *pRes = 0;
5302 return SQLITE_OK;
5304 if( pCur->info.nKey<intKey ){
5305 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
5306 *pRes = -1;
5307 return SQLITE_OK;
5309 /* If the requested key is one more than the previous key, then
5310 ** try to get there using sqlite3BtreeNext() rather than a full
5311 ** binary search. This is an optimization only. The correct answer
5312 ** is still obtained without this case, only a little more slowely */
5313 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
5314 *pRes = 0;
5315 rc = sqlite3BtreeNext(pCur, 0);
5316 if( rc==SQLITE_OK ){
5317 getCellInfo(pCur);
5318 if( pCur->info.nKey==intKey ){
5319 return SQLITE_OK;
5321 }else if( rc==SQLITE_DONE ){
5322 rc = SQLITE_OK;
5323 }else{
5324 return rc;
5330 if( pIdxKey ){
5331 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
5332 pIdxKey->errCode = 0;
5333 assert( pIdxKey->default_rc==1
5334 || pIdxKey->default_rc==0
5335 || pIdxKey->default_rc==-1
5337 }else{
5338 xRecordCompare = 0; /* All keys are integers */
5341 rc = moveToRoot(pCur);
5342 if( rc ){
5343 if( rc==SQLITE_EMPTY ){
5344 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5345 *pRes = -1;
5346 return SQLITE_OK;
5348 return rc;
5350 assert( pCur->pPage );
5351 assert( pCur->pPage->isInit );
5352 assert( pCur->eState==CURSOR_VALID );
5353 assert( pCur->pPage->nCell > 0 );
5354 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
5355 assert( pCur->curIntKey || pIdxKey );
5356 for(;;){
5357 int lwr, upr, idx, c;
5358 Pgno chldPg;
5359 MemPage *pPage = pCur->pPage;
5360 u8 *pCell; /* Pointer to current cell in pPage */
5362 /* pPage->nCell must be greater than zero. If this is the root-page
5363 ** the cursor would have been INVALID above and this for(;;) loop
5364 ** not run. If this is not the root-page, then the moveToChild() routine
5365 ** would have already detected db corruption. Similarly, pPage must
5366 ** be the right kind (index or table) of b-tree page. Otherwise
5367 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5368 assert( pPage->nCell>0 );
5369 assert( pPage->intKey==(pIdxKey==0) );
5370 lwr = 0;
5371 upr = pPage->nCell-1;
5372 assert( biasRight==0 || biasRight==1 );
5373 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
5374 pCur->ix = (u16)idx;
5375 if( xRecordCompare==0 ){
5376 for(;;){
5377 i64 nCellKey;
5378 pCell = findCellPastPtr(pPage, idx);
5379 if( pPage->intKeyLeaf ){
5380 while( 0x80 <= *(pCell++) ){
5381 if( pCell>=pPage->aDataEnd ){
5382 return SQLITE_CORRUPT_PAGE(pPage);
5386 getVarint(pCell, (u64*)&nCellKey);
5387 if( nCellKey<intKey ){
5388 lwr = idx+1;
5389 if( lwr>upr ){ c = -1; break; }
5390 }else if( nCellKey>intKey ){
5391 upr = idx-1;
5392 if( lwr>upr ){ c = +1; break; }
5393 }else{
5394 assert( nCellKey==intKey );
5395 pCur->ix = (u16)idx;
5396 if( !pPage->leaf ){
5397 lwr = idx;
5398 goto moveto_next_layer;
5399 }else{
5400 pCur->curFlags |= BTCF_ValidNKey;
5401 pCur->info.nKey = nCellKey;
5402 pCur->info.nSize = 0;
5403 *pRes = 0;
5404 return SQLITE_OK;
5407 assert( lwr+upr>=0 );
5408 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5410 }else{
5411 for(;;){
5412 int nCell; /* Size of the pCell cell in bytes */
5413 pCell = findCellPastPtr(pPage, idx);
5415 /* The maximum supported page-size is 65536 bytes. This means that
5416 ** the maximum number of record bytes stored on an index B-Tree
5417 ** page is less than 16384 bytes and may be stored as a 2-byte
5418 ** varint. This information is used to attempt to avoid parsing
5419 ** the entire cell by checking for the cases where the record is
5420 ** stored entirely within the b-tree page by inspecting the first
5421 ** 2 bytes of the cell.
5423 nCell = pCell[0];
5424 if( nCell<=pPage->max1bytePayload ){
5425 /* This branch runs if the record-size field of the cell is a
5426 ** single byte varint and the record fits entirely on the main
5427 ** b-tree page. */
5428 testcase( pCell+nCell+1==pPage->aDataEnd );
5429 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5430 }else if( !(pCell[1] & 0x80)
5431 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5433 /* The record-size field is a 2 byte varint and the record
5434 ** fits entirely on the main b-tree page. */
5435 testcase( pCell+nCell+2==pPage->aDataEnd );
5436 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5437 }else{
5438 /* The record flows over onto one or more overflow pages. In
5439 ** this case the whole cell needs to be parsed, a buffer allocated
5440 ** and accessPayload() used to retrieve the record into the
5441 ** buffer before VdbeRecordCompare() can be called.
5443 ** If the record is corrupt, the xRecordCompare routine may read
5444 ** up to two varints past the end of the buffer. An extra 18
5445 ** bytes of padding is allocated at the end of the buffer in
5446 ** case this happens. */
5447 void *pCellKey;
5448 u8 * const pCellBody = pCell - pPage->childPtrSize;
5449 pPage->xParseCell(pPage, pCellBody, &pCur->info);
5450 nCell = (int)pCur->info.nKey;
5451 testcase( nCell<0 ); /* True if key size is 2^32 or more */
5452 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
5453 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
5454 testcase( nCell==2 ); /* Minimum legal index key size */
5455 if( nCell<2 ){
5456 rc = SQLITE_CORRUPT_PAGE(pPage);
5457 goto moveto_finish;
5459 pCellKey = sqlite3Malloc( nCell+18 );
5460 if( pCellKey==0 ){
5461 rc = SQLITE_NOMEM_BKPT;
5462 goto moveto_finish;
5464 pCur->ix = (u16)idx;
5465 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
5466 pCur->curFlags &= ~BTCF_ValidOvfl;
5467 if( rc ){
5468 sqlite3_free(pCellKey);
5469 goto moveto_finish;
5471 c = xRecordCompare(nCell, pCellKey, pIdxKey);
5472 sqlite3_free(pCellKey);
5474 assert(
5475 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
5476 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
5478 if( c<0 ){
5479 lwr = idx+1;
5480 }else if( c>0 ){
5481 upr = idx-1;
5482 }else{
5483 assert( c==0 );
5484 *pRes = 0;
5485 rc = SQLITE_OK;
5486 pCur->ix = (u16)idx;
5487 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
5488 goto moveto_finish;
5490 if( lwr>upr ) break;
5491 assert( lwr+upr>=0 );
5492 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
5495 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
5496 assert( pPage->isInit );
5497 if( pPage->leaf ){
5498 assert( pCur->ix<pCur->pPage->nCell );
5499 pCur->ix = (u16)idx;
5500 *pRes = c;
5501 rc = SQLITE_OK;
5502 goto moveto_finish;
5504 moveto_next_layer:
5505 if( lwr>=pPage->nCell ){
5506 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5507 }else{
5508 chldPg = get4byte(findCell(pPage, lwr));
5510 pCur->ix = (u16)lwr;
5511 rc = moveToChild(pCur, chldPg);
5512 if( rc ) break;
5514 moveto_finish:
5515 pCur->info.nSize = 0;
5516 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5517 return rc;
5522 ** Return TRUE if the cursor is not pointing at an entry of the table.
5524 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
5525 ** past the last entry in the table or sqlite3BtreePrev() moves past
5526 ** the first entry. TRUE is also returned if the table is empty.
5528 int sqlite3BtreeEof(BtCursor *pCur){
5529 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
5530 ** have been deleted? This API will need to change to return an error code
5531 ** as well as the boolean result value.
5533 return (CURSOR_VALID!=pCur->eState);
5537 ** Return an estimate for the number of rows in the table that pCur is
5538 ** pointing to. Return a negative number if no estimate is currently
5539 ** available.
5541 i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
5542 i64 n;
5543 u8 i;
5545 assert( cursorOwnsBtShared(pCur) );
5546 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5548 /* Currently this interface is only called by the OP_IfSmaller
5549 ** opcode, and it that case the cursor will always be valid and
5550 ** will always point to a leaf node. */
5551 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
5552 if( NEVER(pCur->pPage->leaf==0) ) return -1;
5554 n = pCur->pPage->nCell;
5555 for(i=0; i<pCur->iPage; i++){
5556 n *= pCur->apPage[i]->nCell;
5558 return n;
5562 ** Advance the cursor to the next entry in the database.
5563 ** Return value:
5565 ** SQLITE_OK success
5566 ** SQLITE_DONE cursor is already pointing at the last element
5567 ** otherwise some kind of error occurred
5569 ** The main entry point is sqlite3BtreeNext(). That routine is optimized
5570 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx
5571 ** to the next cell on the current page. The (slower) btreeNext() helper
5572 ** routine is called when it is necessary to move to a different page or
5573 ** to restore the cursor.
5575 ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
5576 ** cursor corresponds to an SQL index and this routine could have been
5577 ** skipped if the SQL index had been a unique index. The F argument
5578 ** is a hint to the implement. SQLite btree implementation does not use
5579 ** this hint, but COMDB2 does.
5581 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
5582 int rc;
5583 int idx;
5584 MemPage *pPage;
5586 assert( cursorOwnsBtShared(pCur) );
5587 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5588 if( pCur->eState!=CURSOR_VALID ){
5589 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5590 rc = restoreCursorPosition(pCur);
5591 if( rc!=SQLITE_OK ){
5592 return rc;
5594 if( CURSOR_INVALID==pCur->eState ){
5595 return SQLITE_DONE;
5597 if( pCur->skipNext ){
5598 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5599 pCur->eState = CURSOR_VALID;
5600 if( pCur->skipNext>0 ){
5601 pCur->skipNext = 0;
5602 return SQLITE_OK;
5604 pCur->skipNext = 0;
5608 pPage = pCur->pPage;
5609 idx = ++pCur->ix;
5610 if( !pPage->isInit ){
5611 /* The only known way for this to happen is for there to be a
5612 ** recursive SQL function that does a DELETE operation as part of a
5613 ** SELECT which deletes content out from under an active cursor
5614 ** in a corrupt database file where the table being DELETE-ed from
5615 ** has pages in common with the table being queried. See TH3
5616 ** module cov1/btree78.test testcase 220 (2018-06-08) for an
5617 ** example. */
5618 return SQLITE_CORRUPT_BKPT;
5621 /* If the database file is corrupt, it is possible for the value of idx
5622 ** to be invalid here. This can only occur if a second cursor modifies
5623 ** the page while cursor pCur is holding a reference to it. Which can
5624 ** only happen if the database is corrupt in such a way as to link the
5625 ** page into more than one b-tree structure. */
5626 testcase( idx>pPage->nCell );
5628 if( idx>=pPage->nCell ){
5629 if( !pPage->leaf ){
5630 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
5631 if( rc ) return rc;
5632 return moveToLeftmost(pCur);
5635 if( pCur->iPage==0 ){
5636 pCur->eState = CURSOR_INVALID;
5637 return SQLITE_DONE;
5639 moveToParent(pCur);
5640 pPage = pCur->pPage;
5641 }while( pCur->ix>=pPage->nCell );
5642 if( pPage->intKey ){
5643 return sqlite3BtreeNext(pCur, 0);
5644 }else{
5645 return SQLITE_OK;
5648 if( pPage->leaf ){
5649 return SQLITE_OK;
5650 }else{
5651 return moveToLeftmost(pCur);
5654 int sqlite3BtreeNext(BtCursor *pCur, int flags){
5655 MemPage *pPage;
5656 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
5657 assert( cursorOwnsBtShared(pCur) );
5658 assert( flags==0 || flags==1 );
5659 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5660 pCur->info.nSize = 0;
5661 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5662 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
5663 pPage = pCur->pPage;
5664 if( (++pCur->ix)>=pPage->nCell ){
5665 pCur->ix--;
5666 return btreeNext(pCur);
5668 if( pPage->leaf ){
5669 return SQLITE_OK;
5670 }else{
5671 return moveToLeftmost(pCur);
5676 ** Step the cursor to the back to the previous entry in the database.
5677 ** Return values:
5679 ** SQLITE_OK success
5680 ** SQLITE_DONE the cursor is already on the first element of the table
5681 ** otherwise some kind of error occurred
5683 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
5684 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
5685 ** to the previous cell on the current page. The (slower) btreePrevious()
5686 ** helper routine is called when it is necessary to move to a different page
5687 ** or to restore the cursor.
5689 ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
5690 ** the cursor corresponds to an SQL index and this routine could have been
5691 ** skipped if the SQL index had been a unique index. The F argument is a
5692 ** hint to the implement. The native SQLite btree implementation does not
5693 ** use this hint, but COMDB2 does.
5695 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
5696 int rc;
5697 MemPage *pPage;
5699 assert( cursorOwnsBtShared(pCur) );
5700 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5701 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
5702 assert( pCur->info.nSize==0 );
5703 if( pCur->eState!=CURSOR_VALID ){
5704 rc = restoreCursorPosition(pCur);
5705 if( rc!=SQLITE_OK ){
5706 return rc;
5708 if( CURSOR_INVALID==pCur->eState ){
5709 return SQLITE_DONE;
5711 if( pCur->skipNext ){
5712 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5713 pCur->eState = CURSOR_VALID;
5714 if( pCur->skipNext<0 ){
5715 pCur->skipNext = 0;
5716 return SQLITE_OK;
5718 pCur->skipNext = 0;
5722 pPage = pCur->pPage;
5723 assert( pPage->isInit );
5724 if( !pPage->leaf ){
5725 int idx = pCur->ix;
5726 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
5727 if( rc ) return rc;
5728 rc = moveToRightmost(pCur);
5729 }else{
5730 while( pCur->ix==0 ){
5731 if( pCur->iPage==0 ){
5732 pCur->eState = CURSOR_INVALID;
5733 return SQLITE_DONE;
5735 moveToParent(pCur);
5737 assert( pCur->info.nSize==0 );
5738 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
5740 pCur->ix--;
5741 pPage = pCur->pPage;
5742 if( pPage->intKey && !pPage->leaf ){
5743 rc = sqlite3BtreePrevious(pCur, 0);
5744 }else{
5745 rc = SQLITE_OK;
5748 return rc;
5750 int sqlite3BtreePrevious(BtCursor *pCur, int flags){
5751 assert( cursorOwnsBtShared(pCur) );
5752 assert( flags==0 || flags==1 );
5753 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5754 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
5755 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
5756 pCur->info.nSize = 0;
5757 if( pCur->eState!=CURSOR_VALID
5758 || pCur->ix==0
5759 || pCur->pPage->leaf==0
5761 return btreePrevious(pCur);
5763 pCur->ix--;
5764 return SQLITE_OK;
5768 ** Allocate a new page from the database file.
5770 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
5771 ** has already been called on the new page.) The new page has also
5772 ** been referenced and the calling routine is responsible for calling
5773 ** sqlite3PagerUnref() on the new page when it is done.
5775 ** SQLITE_OK is returned on success. Any other return value indicates
5776 ** an error. *ppPage is set to NULL in the event of an error.
5778 ** If the "nearby" parameter is not 0, then an effort is made to
5779 ** locate a page close to the page number "nearby". This can be used in an
5780 ** attempt to keep related pages close to each other in the database file,
5781 ** which in turn can make database access faster.
5783 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
5784 ** anywhere on the free-list, then it is guaranteed to be returned. If
5785 ** eMode is BTALLOC_LT then the page returned will be less than or equal
5786 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
5787 ** are no restrictions on which page is returned.
5789 static int allocateBtreePage(
5790 BtShared *pBt, /* The btree */
5791 MemPage **ppPage, /* Store pointer to the allocated page here */
5792 Pgno *pPgno, /* Store the page number here */
5793 Pgno nearby, /* Search for a page near this one */
5794 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
5796 MemPage *pPage1;
5797 int rc;
5798 u32 n; /* Number of pages on the freelist */
5799 u32 k; /* Number of leaves on the trunk of the freelist */
5800 MemPage *pTrunk = 0;
5801 MemPage *pPrevTrunk = 0;
5802 Pgno mxPage; /* Total size of the database file */
5804 assert( sqlite3_mutex_held(pBt->mutex) );
5805 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
5806 pPage1 = pBt->pPage1;
5807 mxPage = btreePagecount(pBt);
5808 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
5809 ** stores stores the total number of pages on the freelist. */
5810 n = get4byte(&pPage1->aData[36]);
5811 testcase( n==mxPage-1 );
5812 if( n>=mxPage ){
5813 return SQLITE_CORRUPT_BKPT;
5815 if( n>0 ){
5816 /* There are pages on the freelist. Reuse one of those pages. */
5817 Pgno iTrunk;
5818 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
5819 u32 nSearch = 0; /* Count of the number of search attempts */
5821 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
5822 ** shows that the page 'nearby' is somewhere on the free-list, then
5823 ** the entire-list will be searched for that page.
5825 #ifndef SQLITE_OMIT_AUTOVACUUM
5826 if( eMode==BTALLOC_EXACT ){
5827 if( nearby<=mxPage ){
5828 u8 eType;
5829 assert( nearby>0 );
5830 assert( pBt->autoVacuum );
5831 rc = ptrmapGet(pBt, nearby, &eType, 0);
5832 if( rc ) return rc;
5833 if( eType==PTRMAP_FREEPAGE ){
5834 searchList = 1;
5837 }else if( eMode==BTALLOC_LE ){
5838 searchList = 1;
5840 #endif
5842 /* Decrement the free-list count by 1. Set iTrunk to the index of the
5843 ** first free-list trunk page. iPrevTrunk is initially 1.
5845 rc = sqlite3PagerWrite(pPage1->pDbPage);
5846 if( rc ) return rc;
5847 put4byte(&pPage1->aData[36], n-1);
5849 /* The code within this loop is run only once if the 'searchList' variable
5850 ** is not true. Otherwise, it runs once for each trunk-page on the
5851 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
5852 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
5854 do {
5855 pPrevTrunk = pTrunk;
5856 if( pPrevTrunk ){
5857 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
5858 ** is the page number of the next freelist trunk page in the list or
5859 ** zero if this is the last freelist trunk page. */
5860 iTrunk = get4byte(&pPrevTrunk->aData[0]);
5861 }else{
5862 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
5863 ** stores the page number of the first page of the freelist, or zero if
5864 ** the freelist is empty. */
5865 iTrunk = get4byte(&pPage1->aData[32]);
5867 testcase( iTrunk==mxPage );
5868 if( iTrunk>mxPage || nSearch++ > n ){
5869 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
5870 }else{
5871 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
5873 if( rc ){
5874 pTrunk = 0;
5875 goto end_allocate_page;
5877 assert( pTrunk!=0 );
5878 assert( pTrunk->aData!=0 );
5879 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
5880 ** is the number of leaf page pointers to follow. */
5881 k = get4byte(&pTrunk->aData[4]);
5882 if( k==0 && !searchList ){
5883 /* The trunk has no leaves and the list is not being searched.
5884 ** So extract the trunk page itself and use it as the newly
5885 ** allocated page */
5886 assert( pPrevTrunk==0 );
5887 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5888 if( rc ){
5889 goto end_allocate_page;
5891 *pPgno = iTrunk;
5892 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5893 *ppPage = pTrunk;
5894 pTrunk = 0;
5895 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5896 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
5897 /* Value of k is out of range. Database corruption */
5898 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5899 goto end_allocate_page;
5900 #ifndef SQLITE_OMIT_AUTOVACUUM
5901 }else if( searchList
5902 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
5904 /* The list is being searched and this trunk page is the page
5905 ** to allocate, regardless of whether it has leaves.
5907 *pPgno = iTrunk;
5908 *ppPage = pTrunk;
5909 searchList = 0;
5910 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5911 if( rc ){
5912 goto end_allocate_page;
5914 if( k==0 ){
5915 if( !pPrevTrunk ){
5916 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5917 }else{
5918 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5919 if( rc!=SQLITE_OK ){
5920 goto end_allocate_page;
5922 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
5924 }else{
5925 /* The trunk page is required by the caller but it contains
5926 ** pointers to free-list leaves. The first leaf becomes a trunk
5927 ** page in this case.
5929 MemPage *pNewTrunk;
5930 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
5931 if( iNewTrunk>mxPage ){
5932 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5933 goto end_allocate_page;
5935 testcase( iNewTrunk==mxPage );
5936 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
5937 if( rc!=SQLITE_OK ){
5938 goto end_allocate_page;
5940 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5941 if( rc!=SQLITE_OK ){
5942 releasePage(pNewTrunk);
5943 goto end_allocate_page;
5945 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
5946 put4byte(&pNewTrunk->aData[4], k-1);
5947 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
5948 releasePage(pNewTrunk);
5949 if( !pPrevTrunk ){
5950 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
5951 put4byte(&pPage1->aData[32], iNewTrunk);
5952 }else{
5953 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5954 if( rc ){
5955 goto end_allocate_page;
5957 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
5960 pTrunk = 0;
5961 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5962 #endif
5963 }else if( k>0 ){
5964 /* Extract a leaf from the trunk */
5965 u32 closest;
5966 Pgno iPage;
5967 unsigned char *aData = pTrunk->aData;
5968 if( nearby>0 ){
5969 u32 i;
5970 closest = 0;
5971 if( eMode==BTALLOC_LE ){
5972 for(i=0; i<k; i++){
5973 iPage = get4byte(&aData[8+i*4]);
5974 if( iPage<=nearby ){
5975 closest = i;
5976 break;
5979 }else{
5980 int dist;
5981 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
5982 for(i=1; i<k; i++){
5983 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
5984 if( d2<dist ){
5985 closest = i;
5986 dist = d2;
5990 }else{
5991 closest = 0;
5994 iPage = get4byte(&aData[8+closest*4]);
5995 testcase( iPage==mxPage );
5996 if( iPage>mxPage ){
5997 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5998 goto end_allocate_page;
6000 testcase( iPage==mxPage );
6001 if( !searchList
6002 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
6004 int noContent;
6005 *pPgno = iPage;
6006 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
6007 ": %d more free pages\n",
6008 *pPgno, closest+1, k, pTrunk->pgno, n-1));
6009 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6010 if( rc ) goto end_allocate_page;
6011 if( closest<k-1 ){
6012 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
6014 put4byte(&aData[4], k-1);
6015 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
6016 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
6017 if( rc==SQLITE_OK ){
6018 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
6019 if( rc!=SQLITE_OK ){
6020 releasePage(*ppPage);
6021 *ppPage = 0;
6024 searchList = 0;
6027 releasePage(pPrevTrunk);
6028 pPrevTrunk = 0;
6029 }while( searchList );
6030 }else{
6031 /* There are no pages on the freelist, so append a new page to the
6032 ** database image.
6034 ** Normally, new pages allocated by this block can be requested from the
6035 ** pager layer with the 'no-content' flag set. This prevents the pager
6036 ** from trying to read the pages content from disk. However, if the
6037 ** current transaction has already run one or more incremental-vacuum
6038 ** steps, then the page we are about to allocate may contain content
6039 ** that is required in the event of a rollback. In this case, do
6040 ** not set the no-content flag. This causes the pager to load and journal
6041 ** the current page content before overwriting it.
6043 ** Note that the pager will not actually attempt to load or journal
6044 ** content for any page that really does lie past the end of the database
6045 ** file on disk. So the effects of disabling the no-content optimization
6046 ** here are confined to those pages that lie between the end of the
6047 ** database image and the end of the database file.
6049 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
6051 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6052 if( rc ) return rc;
6053 pBt->nPage++;
6054 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
6056 #ifndef SQLITE_OMIT_AUTOVACUUM
6057 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
6058 /* If *pPgno refers to a pointer-map page, allocate two new pages
6059 ** at the end of the file instead of one. The first allocated page
6060 ** becomes a new pointer-map page, the second is used by the caller.
6062 MemPage *pPg = 0;
6063 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
6064 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
6065 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
6066 if( rc==SQLITE_OK ){
6067 rc = sqlite3PagerWrite(pPg->pDbPage);
6068 releasePage(pPg);
6070 if( rc ) return rc;
6071 pBt->nPage++;
6072 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
6074 #endif
6075 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
6076 *pPgno = pBt->nPage;
6078 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
6079 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
6080 if( rc ) return rc;
6081 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
6082 if( rc!=SQLITE_OK ){
6083 releasePage(*ppPage);
6084 *ppPage = 0;
6086 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
6089 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
6091 end_allocate_page:
6092 releasePage(pTrunk);
6093 releasePage(pPrevTrunk);
6094 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
6095 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
6096 return rc;
6100 ** This function is used to add page iPage to the database file free-list.
6101 ** It is assumed that the page is not already a part of the free-list.
6103 ** The value passed as the second argument to this function is optional.
6104 ** If the caller happens to have a pointer to the MemPage object
6105 ** corresponding to page iPage handy, it may pass it as the second value.
6106 ** Otherwise, it may pass NULL.
6108 ** If a pointer to a MemPage object is passed as the second argument,
6109 ** its reference count is not altered by this function.
6111 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
6112 MemPage *pTrunk = 0; /* Free-list trunk page */
6113 Pgno iTrunk = 0; /* Page number of free-list trunk page */
6114 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
6115 MemPage *pPage; /* Page being freed. May be NULL. */
6116 int rc; /* Return Code */
6117 int nFree; /* Initial number of pages on free-list */
6119 assert( sqlite3_mutex_held(pBt->mutex) );
6120 assert( CORRUPT_DB || iPage>1 );
6121 assert( !pMemPage || pMemPage->pgno==iPage );
6123 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
6124 if( pMemPage ){
6125 pPage = pMemPage;
6126 sqlite3PagerRef(pPage->pDbPage);
6127 }else{
6128 pPage = btreePageLookup(pBt, iPage);
6131 /* Increment the free page count on pPage1 */
6132 rc = sqlite3PagerWrite(pPage1->pDbPage);
6133 if( rc ) goto freepage_out;
6134 nFree = get4byte(&pPage1->aData[36]);
6135 put4byte(&pPage1->aData[36], nFree+1);
6137 if( pBt->btsFlags & BTS_SECURE_DELETE ){
6138 /* If the secure_delete option is enabled, then
6139 ** always fully overwrite deleted information with zeros.
6141 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
6142 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
6144 goto freepage_out;
6146 memset(pPage->aData, 0, pPage->pBt->pageSize);
6149 /* If the database supports auto-vacuum, write an entry in the pointer-map
6150 ** to indicate that the page is free.
6152 if( ISAUTOVACUUM ){
6153 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
6154 if( rc ) goto freepage_out;
6157 /* Now manipulate the actual database free-list structure. There are two
6158 ** possibilities. If the free-list is currently empty, or if the first
6159 ** trunk page in the free-list is full, then this page will become a
6160 ** new free-list trunk page. Otherwise, it will become a leaf of the
6161 ** first trunk page in the current free-list. This block tests if it
6162 ** is possible to add the page as a new free-list leaf.
6164 if( nFree!=0 ){
6165 u32 nLeaf; /* Initial number of leaf cells on trunk page */
6167 iTrunk = get4byte(&pPage1->aData[32]);
6168 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
6169 if( rc!=SQLITE_OK ){
6170 goto freepage_out;
6173 nLeaf = get4byte(&pTrunk->aData[4]);
6174 assert( pBt->usableSize>32 );
6175 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
6176 rc = SQLITE_CORRUPT_BKPT;
6177 goto freepage_out;
6179 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
6180 /* In this case there is room on the trunk page to insert the page
6181 ** being freed as a new leaf.
6183 ** Note that the trunk page is not really full until it contains
6184 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
6185 ** coded. But due to a coding error in versions of SQLite prior to
6186 ** 3.6.0, databases with freelist trunk pages holding more than
6187 ** usableSize/4 - 8 entries will be reported as corrupt. In order
6188 ** to maintain backwards compatibility with older versions of SQLite,
6189 ** we will continue to restrict the number of entries to usableSize/4 - 8
6190 ** for now. At some point in the future (once everyone has upgraded
6191 ** to 3.6.0 or later) we should consider fixing the conditional above
6192 ** to read "usableSize/4-2" instead of "usableSize/4-8".
6194 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
6195 ** avoid using the last six entries in the freelist trunk page array in
6196 ** order that database files created by newer versions of SQLite can be
6197 ** read by older versions of SQLite.
6199 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6200 if( rc==SQLITE_OK ){
6201 put4byte(&pTrunk->aData[4], nLeaf+1);
6202 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
6203 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
6204 sqlite3PagerDontWrite(pPage->pDbPage);
6206 rc = btreeSetHasContent(pBt, iPage);
6208 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
6209 goto freepage_out;
6213 /* If control flows to this point, then it was not possible to add the
6214 ** the page being freed as a leaf page of the first trunk in the free-list.
6215 ** Possibly because the free-list is empty, or possibly because the
6216 ** first trunk in the free-list is full. Either way, the page being freed
6217 ** will become the new first trunk page in the free-list.
6219 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
6220 goto freepage_out;
6222 rc = sqlite3PagerWrite(pPage->pDbPage);
6223 if( rc!=SQLITE_OK ){
6224 goto freepage_out;
6226 put4byte(pPage->aData, iTrunk);
6227 put4byte(&pPage->aData[4], 0);
6228 put4byte(&pPage1->aData[32], iPage);
6229 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
6231 freepage_out:
6232 if( pPage ){
6233 pPage->isInit = 0;
6235 releasePage(pPage);
6236 releasePage(pTrunk);
6237 return rc;
6239 static void freePage(MemPage *pPage, int *pRC){
6240 if( (*pRC)==SQLITE_OK ){
6241 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6246 ** Free any overflow pages associated with the given Cell. Store
6247 ** size information about the cell in pInfo.
6249 static int clearCell(
6250 MemPage *pPage, /* The page that contains the Cell */
6251 unsigned char *pCell, /* First byte of the Cell */
6252 CellInfo *pInfo /* Size information about the cell */
6254 BtShared *pBt;
6255 Pgno ovflPgno;
6256 int rc;
6257 int nOvfl;
6258 u32 ovflPageSize;
6260 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6261 pPage->xParseCell(pPage, pCell, pInfo);
6262 if( pInfo->nLocal==pInfo->nPayload ){
6263 return SQLITE_OK; /* No overflow pages. Return without doing anything */
6265 testcase( pCell + pInfo->nSize == pPage->aDataEnd );
6266 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd );
6267 if( pCell + pInfo->nSize > pPage->aDataEnd ){
6268 /* Cell extends past end of page */
6269 return SQLITE_CORRUPT_PAGE(pPage);
6271 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
6272 pBt = pPage->pBt;
6273 assert( pBt->usableSize > 4 );
6274 ovflPageSize = pBt->usableSize - 4;
6275 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
6276 assert( nOvfl>0 ||
6277 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
6279 while( nOvfl-- ){
6280 Pgno iNext = 0;
6281 MemPage *pOvfl = 0;
6282 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
6283 /* 0 is not a legal page number and page 1 cannot be an
6284 ** overflow page. Therefore if ovflPgno<2 or past the end of the
6285 ** file the database must be corrupt. */
6286 return SQLITE_CORRUPT_BKPT;
6288 if( nOvfl ){
6289 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
6290 if( rc ) return rc;
6293 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
6294 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
6296 /* There is no reason any cursor should have an outstanding reference
6297 ** to an overflow page belonging to a cell that is being deleted/updated.
6298 ** So if there exists more than one reference to this page, then it
6299 ** must not really be an overflow page and the database must be corrupt.
6300 ** It is helpful to detect this before calling freePage2(), as
6301 ** freePage2() may zero the page contents if secure-delete mode is
6302 ** enabled. If this 'overflow' page happens to be a page that the
6303 ** caller is iterating through or using in some other way, this
6304 ** can be problematic.
6306 rc = SQLITE_CORRUPT_BKPT;
6307 }else{
6308 rc = freePage2(pBt, pOvfl, ovflPgno);
6311 if( pOvfl ){
6312 sqlite3PagerUnref(pOvfl->pDbPage);
6314 if( rc ) return rc;
6315 ovflPgno = iNext;
6317 return SQLITE_OK;
6321 ** Create the byte sequence used to represent a cell on page pPage
6322 ** and write that byte sequence into pCell[]. Overflow pages are
6323 ** allocated and filled in as necessary. The calling procedure
6324 ** is responsible for making sure sufficient space has been allocated
6325 ** for pCell[].
6327 ** Note that pCell does not necessary need to point to the pPage->aData
6328 ** area. pCell might point to some temporary storage. The cell will
6329 ** be constructed in this temporary area then copied into pPage->aData
6330 ** later.
6332 static int fillInCell(
6333 MemPage *pPage, /* The page that contains the cell */
6334 unsigned char *pCell, /* Complete text of the cell */
6335 const BtreePayload *pX, /* Payload with which to construct the cell */
6336 int *pnSize /* Write cell size here */
6338 int nPayload;
6339 const u8 *pSrc;
6340 int nSrc, n, rc, mn;
6341 int spaceLeft;
6342 MemPage *pToRelease;
6343 unsigned char *pPrior;
6344 unsigned char *pPayload;
6345 BtShared *pBt;
6346 Pgno pgnoOvfl;
6347 int nHeader;
6349 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6351 /* pPage is not necessarily writeable since pCell might be auxiliary
6352 ** buffer space that is separate from the pPage buffer area */
6353 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
6354 || sqlite3PagerIswriteable(pPage->pDbPage) );
6356 /* Fill in the header. */
6357 nHeader = pPage->childPtrSize;
6358 if( pPage->intKey ){
6359 nPayload = pX->nData + pX->nZero;
6360 pSrc = pX->pData;
6361 nSrc = pX->nData;
6362 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
6363 nHeader += putVarint32(&pCell[nHeader], nPayload);
6364 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
6365 }else{
6366 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
6367 nSrc = nPayload = (int)pX->nKey;
6368 pSrc = pX->pKey;
6369 nHeader += putVarint32(&pCell[nHeader], nPayload);
6372 /* Fill in the payload */
6373 pPayload = &pCell[nHeader];
6374 if( nPayload<=pPage->maxLocal ){
6375 /* This is the common case where everything fits on the btree page
6376 ** and no overflow pages are required. */
6377 n = nHeader + nPayload;
6378 testcase( n==3 );
6379 testcase( n==4 );
6380 if( n<4 ) n = 4;
6381 *pnSize = n;
6382 assert( nSrc<=nPayload );
6383 testcase( nSrc<nPayload );
6384 memcpy(pPayload, pSrc, nSrc);
6385 memset(pPayload+nSrc, 0, nPayload-nSrc);
6386 return SQLITE_OK;
6389 /* If we reach this point, it means that some of the content will need
6390 ** to spill onto overflow pages.
6392 mn = pPage->minLocal;
6393 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
6394 testcase( n==pPage->maxLocal );
6395 testcase( n==pPage->maxLocal+1 );
6396 if( n > pPage->maxLocal ) n = mn;
6397 spaceLeft = n;
6398 *pnSize = n + nHeader + 4;
6399 pPrior = &pCell[nHeader+n];
6400 pToRelease = 0;
6401 pgnoOvfl = 0;
6402 pBt = pPage->pBt;
6404 /* At this point variables should be set as follows:
6406 ** nPayload Total payload size in bytes
6407 ** pPayload Begin writing payload here
6408 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
6409 ** that means content must spill into overflow pages.
6410 ** *pnSize Size of the local cell (not counting overflow pages)
6411 ** pPrior Where to write the pgno of the first overflow page
6413 ** Use a call to btreeParseCellPtr() to verify that the values above
6414 ** were computed correctly.
6416 #ifdef SQLITE_DEBUG
6418 CellInfo info;
6419 pPage->xParseCell(pPage, pCell, &info);
6420 assert( nHeader==(int)(info.pPayload - pCell) );
6421 assert( info.nKey==pX->nKey );
6422 assert( *pnSize == info.nSize );
6423 assert( spaceLeft == info.nLocal );
6425 #endif
6427 /* Write the payload into the local Cell and any extra into overflow pages */
6428 while( 1 ){
6429 n = nPayload;
6430 if( n>spaceLeft ) n = spaceLeft;
6432 /* If pToRelease is not zero than pPayload points into the data area
6433 ** of pToRelease. Make sure pToRelease is still writeable. */
6434 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6436 /* If pPayload is part of the data area of pPage, then make sure pPage
6437 ** is still writeable */
6438 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
6439 || sqlite3PagerIswriteable(pPage->pDbPage) );
6441 if( nSrc>=n ){
6442 memcpy(pPayload, pSrc, n);
6443 }else if( nSrc>0 ){
6444 n = nSrc;
6445 memcpy(pPayload, pSrc, n);
6446 }else{
6447 memset(pPayload, 0, n);
6449 nPayload -= n;
6450 if( nPayload<=0 ) break;
6451 pPayload += n;
6452 pSrc += n;
6453 nSrc -= n;
6454 spaceLeft -= n;
6455 if( spaceLeft==0 ){
6456 MemPage *pOvfl = 0;
6457 #ifndef SQLITE_OMIT_AUTOVACUUM
6458 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
6459 if( pBt->autoVacuum ){
6461 pgnoOvfl++;
6462 } while(
6463 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
6466 #endif
6467 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
6468 #ifndef SQLITE_OMIT_AUTOVACUUM
6469 /* If the database supports auto-vacuum, and the second or subsequent
6470 ** overflow page is being allocated, add an entry to the pointer-map
6471 ** for that page now.
6473 ** If this is the first overflow page, then write a partial entry
6474 ** to the pointer-map. If we write nothing to this pointer-map slot,
6475 ** then the optimistic overflow chain processing in clearCell()
6476 ** may misinterpret the uninitialized values and delete the
6477 ** wrong pages from the database.
6479 if( pBt->autoVacuum && rc==SQLITE_OK ){
6480 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
6481 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
6482 if( rc ){
6483 releasePage(pOvfl);
6486 #endif
6487 if( rc ){
6488 releasePage(pToRelease);
6489 return rc;
6492 /* If pToRelease is not zero than pPrior points into the data area
6493 ** of pToRelease. Make sure pToRelease is still writeable. */
6494 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6496 /* If pPrior is part of the data area of pPage, then make sure pPage
6497 ** is still writeable */
6498 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
6499 || sqlite3PagerIswriteable(pPage->pDbPage) );
6501 put4byte(pPrior, pgnoOvfl);
6502 releasePage(pToRelease);
6503 pToRelease = pOvfl;
6504 pPrior = pOvfl->aData;
6505 put4byte(pPrior, 0);
6506 pPayload = &pOvfl->aData[4];
6507 spaceLeft = pBt->usableSize - 4;
6510 releasePage(pToRelease);
6511 return SQLITE_OK;
6515 ** Remove the i-th cell from pPage. This routine effects pPage only.
6516 ** The cell content is not freed or deallocated. It is assumed that
6517 ** the cell content has been copied someplace else. This routine just
6518 ** removes the reference to the cell from pPage.
6520 ** "sz" must be the number of bytes in the cell.
6522 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
6523 u32 pc; /* Offset to cell content of cell being deleted */
6524 u8 *data; /* pPage->aData */
6525 u8 *ptr; /* Used to move bytes around within data[] */
6526 int rc; /* The return code */
6527 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
6529 if( *pRC ) return;
6530 assert( idx>=0 && idx<pPage->nCell );
6531 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
6532 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6533 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6534 data = pPage->aData;
6535 ptr = &pPage->aCellIdx[2*idx];
6536 pc = get2byte(ptr);
6537 hdr = pPage->hdrOffset;
6538 testcase( pc==get2byte(&data[hdr+5]) );
6539 testcase( pc+sz==pPage->pBt->usableSize );
6540 if( pc+sz > pPage->pBt->usableSize ){
6541 *pRC = SQLITE_CORRUPT_BKPT;
6542 return;
6544 rc = freeSpace(pPage, pc, sz);
6545 if( rc ){
6546 *pRC = rc;
6547 return;
6549 pPage->nCell--;
6550 if( pPage->nCell==0 ){
6551 memset(&data[hdr+1], 0, 4);
6552 data[hdr+7] = 0;
6553 put2byte(&data[hdr+5], pPage->pBt->usableSize);
6554 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
6555 - pPage->childPtrSize - 8;
6556 }else{
6557 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
6558 put2byte(&data[hdr+3], pPage->nCell);
6559 pPage->nFree += 2;
6564 ** Insert a new cell on pPage at cell index "i". pCell points to the
6565 ** content of the cell.
6567 ** If the cell content will fit on the page, then put it there. If it
6568 ** will not fit, then make a copy of the cell content into pTemp if
6569 ** pTemp is not null. Regardless of pTemp, allocate a new entry
6570 ** in pPage->apOvfl[] and make it point to the cell content (either
6571 ** in pTemp or the original pCell) and also record its index.
6572 ** Allocating a new entry in pPage->aCell[] implies that
6573 ** pPage->nOverflow is incremented.
6575 ** *pRC must be SQLITE_OK when this routine is called.
6577 static void insertCell(
6578 MemPage *pPage, /* Page into which we are copying */
6579 int i, /* New cell becomes the i-th cell of the page */
6580 u8 *pCell, /* Content of the new cell */
6581 int sz, /* Bytes of content in pCell */
6582 u8 *pTemp, /* Temp storage space for pCell, if needed */
6583 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
6584 int *pRC /* Read and write return code from here */
6586 int idx = 0; /* Where to write new cell content in data[] */
6587 int j; /* Loop counter */
6588 u8 *data; /* The content of the whole page */
6589 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
6591 assert( *pRC==SQLITE_OK );
6592 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
6593 assert( MX_CELL(pPage->pBt)<=10921 );
6594 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
6595 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
6596 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
6597 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6598 /* The cell should normally be sized correctly. However, when moving a
6599 ** malformed cell from a leaf page to an interior page, if the cell size
6600 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
6601 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
6602 ** the term after the || in the following assert(). */
6603 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
6604 if( pPage->nOverflow || sz+2>pPage->nFree ){
6605 if( pTemp ){
6606 memcpy(pTemp, pCell, sz);
6607 pCell = pTemp;
6609 if( iChild ){
6610 put4byte(pCell, iChild);
6612 j = pPage->nOverflow++;
6613 /* Comparison against ArraySize-1 since we hold back one extra slot
6614 ** as a contingency. In other words, never need more than 3 overflow
6615 ** slots but 4 are allocated, just to be safe. */
6616 assert( j < ArraySize(pPage->apOvfl)-1 );
6617 pPage->apOvfl[j] = pCell;
6618 pPage->aiOvfl[j] = (u16)i;
6620 /* When multiple overflows occur, they are always sequential and in
6621 ** sorted order. This invariants arise because multiple overflows can
6622 ** only occur when inserting divider cells into the parent page during
6623 ** balancing, and the dividers are adjacent and sorted.
6625 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
6626 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
6627 }else{
6628 int rc = sqlite3PagerWrite(pPage->pDbPage);
6629 if( rc!=SQLITE_OK ){
6630 *pRC = rc;
6631 return;
6633 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6634 data = pPage->aData;
6635 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
6636 rc = allocateSpace(pPage, sz, &idx);
6637 if( rc ){ *pRC = rc; return; }
6638 /* The allocateSpace() routine guarantees the following properties
6639 ** if it returns successfully */
6640 assert( idx >= 0 );
6641 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
6642 assert( idx+sz <= (int)pPage->pBt->usableSize );
6643 pPage->nFree -= (u16)(2 + sz);
6644 memcpy(&data[idx], pCell, sz);
6645 if( iChild ){
6646 put4byte(&data[idx], iChild);
6648 pIns = pPage->aCellIdx + i*2;
6649 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
6650 put2byte(pIns, idx);
6651 pPage->nCell++;
6652 /* increment the cell count */
6653 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
6654 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
6655 #ifndef SQLITE_OMIT_AUTOVACUUM
6656 if( pPage->pBt->autoVacuum ){
6657 /* The cell may contain a pointer to an overflow page. If so, write
6658 ** the entry for the overflow page into the pointer map.
6660 ptrmapPutOvflPtr(pPage, pCell, pRC);
6662 #endif
6667 ** A CellArray object contains a cache of pointers and sizes for a
6668 ** consecutive sequence of cells that might be held on multiple pages.
6670 typedef struct CellArray CellArray;
6671 struct CellArray {
6672 int nCell; /* Number of cells in apCell[] */
6673 MemPage *pRef; /* Reference page */
6674 u8 **apCell; /* All cells begin balanced */
6675 u16 *szCell; /* Local size of all cells in apCell[] */
6679 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
6680 ** computed.
6682 static void populateCellCache(CellArray *p, int idx, int N){
6683 assert( idx>=0 && idx+N<=p->nCell );
6684 while( N>0 ){
6685 assert( p->apCell[idx]!=0 );
6686 if( p->szCell[idx]==0 ){
6687 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
6688 }else{
6689 assert( CORRUPT_DB ||
6690 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
6692 idx++;
6693 N--;
6698 ** Return the size of the Nth element of the cell array
6700 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
6701 assert( N>=0 && N<p->nCell );
6702 assert( p->szCell[N]==0 );
6703 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
6704 return p->szCell[N];
6706 static u16 cachedCellSize(CellArray *p, int N){
6707 assert( N>=0 && N<p->nCell );
6708 if( p->szCell[N] ) return p->szCell[N];
6709 return computeCellSize(p, N);
6713 ** Array apCell[] contains pointers to nCell b-tree page cells. The
6714 ** szCell[] array contains the size in bytes of each cell. This function
6715 ** replaces the current contents of page pPg with the contents of the cell
6716 ** array.
6718 ** Some of the cells in apCell[] may currently be stored in pPg. This
6719 ** function works around problems caused by this by making a copy of any
6720 ** such cells before overwriting the page data.
6722 ** The MemPage.nFree field is invalidated by this function. It is the
6723 ** responsibility of the caller to set it correctly.
6725 static int rebuildPage(
6726 MemPage *pPg, /* Edit this page */
6727 int nCell, /* Final number of cells on page */
6728 u8 **apCell, /* Array of cells */
6729 u16 *szCell /* Array of cell sizes */
6731 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
6732 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
6733 const int usableSize = pPg->pBt->usableSize;
6734 u8 * const pEnd = &aData[usableSize];
6735 int i;
6736 u8 *pCellptr = pPg->aCellIdx;
6737 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6738 u8 *pData;
6740 i = get2byte(&aData[hdr+5]);
6741 memcpy(&pTmp[i], &aData[i], usableSize - i);
6743 pData = pEnd;
6744 for(i=0; i<nCell; i++){
6745 u8 *pCell = apCell[i];
6746 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
6747 pCell = &pTmp[pCell - aData];
6749 pData -= szCell[i];
6750 put2byte(pCellptr, (pData - aData));
6751 pCellptr += 2;
6752 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
6753 memcpy(pData, pCell, szCell[i]);
6754 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
6755 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
6758 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
6759 pPg->nCell = nCell;
6760 pPg->nOverflow = 0;
6762 put2byte(&aData[hdr+1], 0);
6763 put2byte(&aData[hdr+3], pPg->nCell);
6764 put2byte(&aData[hdr+5], pData - aData);
6765 aData[hdr+7] = 0x00;
6766 return SQLITE_OK;
6770 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6771 ** contains the size in bytes of each such cell. This function attempts to
6772 ** add the cells stored in the array to page pPg. If it cannot (because
6773 ** the page needs to be defragmented before the cells will fit), non-zero
6774 ** is returned. Otherwise, if the cells are added successfully, zero is
6775 ** returned.
6777 ** Argument pCellptr points to the first entry in the cell-pointer array
6778 ** (part of page pPg) to populate. After cell apCell[0] is written to the
6779 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
6780 ** cell in the array. It is the responsibility of the caller to ensure
6781 ** that it is safe to overwrite this part of the cell-pointer array.
6783 ** When this function is called, *ppData points to the start of the
6784 ** content area on page pPg. If the size of the content area is extended,
6785 ** *ppData is updated to point to the new start of the content area
6786 ** before returning.
6788 ** Finally, argument pBegin points to the byte immediately following the
6789 ** end of the space required by this page for the cell-pointer area (for
6790 ** all cells - not just those inserted by the current call). If the content
6791 ** area must be extended to before this point in order to accomodate all
6792 ** cells in apCell[], then the cells do not fit and non-zero is returned.
6794 static int pageInsertArray(
6795 MemPage *pPg, /* Page to add cells to */
6796 u8 *pBegin, /* End of cell-pointer array */
6797 u8 **ppData, /* IN/OUT: Page content -area pointer */
6798 u8 *pCellptr, /* Pointer to cell-pointer area */
6799 int iFirst, /* Index of first cell to add */
6800 int nCell, /* Number of cells to add to pPg */
6801 CellArray *pCArray /* Array of cells */
6803 int i;
6804 u8 *aData = pPg->aData;
6805 u8 *pData = *ppData;
6806 int iEnd = iFirst + nCell;
6807 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
6808 for(i=iFirst; i<iEnd; i++){
6809 int sz, rc;
6810 u8 *pSlot;
6811 sz = cachedCellSize(pCArray, i);
6812 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
6813 if( (pData - pBegin)<sz ) return 1;
6814 pData -= sz;
6815 pSlot = pData;
6817 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
6818 ** database. But they might for a corrupt database. Hence use memmove()
6819 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
6820 assert( (pSlot+sz)<=pCArray->apCell[i]
6821 || pSlot>=(pCArray->apCell[i]+sz)
6822 || CORRUPT_DB );
6823 memmove(pSlot, pCArray->apCell[i], sz);
6824 put2byte(pCellptr, (pSlot - aData));
6825 pCellptr += 2;
6827 *ppData = pData;
6828 return 0;
6832 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6833 ** contains the size in bytes of each such cell. This function adds the
6834 ** space associated with each cell in the array that is currently stored
6835 ** within the body of pPg to the pPg free-list. The cell-pointers and other
6836 ** fields of the page are not updated.
6838 ** This function returns the total number of cells added to the free-list.
6840 static int pageFreeArray(
6841 MemPage *pPg, /* Page to edit */
6842 int iFirst, /* First cell to delete */
6843 int nCell, /* Cells to delete */
6844 CellArray *pCArray /* Array of cells */
6846 u8 * const aData = pPg->aData;
6847 u8 * const pEnd = &aData[pPg->pBt->usableSize];
6848 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
6849 int nRet = 0;
6850 int i;
6851 int iEnd = iFirst + nCell;
6852 u8 *pFree = 0;
6853 int szFree = 0;
6855 for(i=iFirst; i<iEnd; i++){
6856 u8 *pCell = pCArray->apCell[i];
6857 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
6858 int sz;
6859 /* No need to use cachedCellSize() here. The sizes of all cells that
6860 ** are to be freed have already been computing while deciding which
6861 ** cells need freeing */
6862 sz = pCArray->szCell[i]; assert( sz>0 );
6863 if( pFree!=(pCell + sz) ){
6864 if( pFree ){
6865 assert( pFree>aData && (pFree - aData)<65536 );
6866 freeSpace(pPg, (u16)(pFree - aData), szFree);
6868 pFree = pCell;
6869 szFree = sz;
6870 if( pFree+sz>pEnd ) return 0;
6871 }else{
6872 pFree = pCell;
6873 szFree += sz;
6875 nRet++;
6878 if( pFree ){
6879 assert( pFree>aData && (pFree - aData)<65536 );
6880 freeSpace(pPg, (u16)(pFree - aData), szFree);
6882 return nRet;
6886 ** apCell[] and szCell[] contains pointers to and sizes of all cells in the
6887 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting
6888 ** with apCell[iOld]. After balancing, this page should hold nNew cells
6889 ** starting at apCell[iNew].
6891 ** This routine makes the necessary adjustments to pPg so that it contains
6892 ** the correct cells after being balanced.
6894 ** The pPg->nFree field is invalid when this function returns. It is the
6895 ** responsibility of the caller to set it correctly.
6897 static int editPage(
6898 MemPage *pPg, /* Edit this page */
6899 int iOld, /* Index of first cell currently on page */
6900 int iNew, /* Index of new first cell on page */
6901 int nNew, /* Final number of cells on page */
6902 CellArray *pCArray /* Array of cells and sizes */
6904 u8 * const aData = pPg->aData;
6905 const int hdr = pPg->hdrOffset;
6906 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
6907 int nCell = pPg->nCell; /* Cells stored on pPg */
6908 u8 *pData;
6909 u8 *pCellptr;
6910 int i;
6911 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
6912 int iNewEnd = iNew + nNew;
6914 #ifdef SQLITE_DEBUG
6915 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6916 memcpy(pTmp, aData, pPg->pBt->usableSize);
6917 #endif
6919 /* Remove cells from the start and end of the page */
6920 if( iOld<iNew ){
6921 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
6922 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
6923 nCell -= nShift;
6925 if( iNewEnd < iOldEnd ){
6926 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
6929 pData = &aData[get2byteNotZero(&aData[hdr+5])];
6930 if( pData<pBegin ) goto editpage_fail;
6932 /* Add cells to the start of the page */
6933 if( iNew<iOld ){
6934 int nAdd = MIN(nNew,iOld-iNew);
6935 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
6936 pCellptr = pPg->aCellIdx;
6937 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
6938 if( pageInsertArray(
6939 pPg, pBegin, &pData, pCellptr,
6940 iNew, nAdd, pCArray
6941 ) ) goto editpage_fail;
6942 nCell += nAdd;
6945 /* Add any overflow cells */
6946 for(i=0; i<pPg->nOverflow; i++){
6947 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
6948 if( iCell>=0 && iCell<nNew ){
6949 pCellptr = &pPg->aCellIdx[iCell * 2];
6950 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
6951 nCell++;
6952 if( pageInsertArray(
6953 pPg, pBegin, &pData, pCellptr,
6954 iCell+iNew, 1, pCArray
6955 ) ) goto editpage_fail;
6959 /* Append cells to the end of the page */
6960 pCellptr = &pPg->aCellIdx[nCell*2];
6961 if( pageInsertArray(
6962 pPg, pBegin, &pData, pCellptr,
6963 iNew+nCell, nNew-nCell, pCArray
6964 ) ) goto editpage_fail;
6966 pPg->nCell = nNew;
6967 pPg->nOverflow = 0;
6969 put2byte(&aData[hdr+3], pPg->nCell);
6970 put2byte(&aData[hdr+5], pData - aData);
6972 #ifdef SQLITE_DEBUG
6973 for(i=0; i<nNew && !CORRUPT_DB; i++){
6974 u8 *pCell = pCArray->apCell[i+iNew];
6975 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
6976 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
6977 pCell = &pTmp[pCell - aData];
6979 assert( 0==memcmp(pCell, &aData[iOff],
6980 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
6982 #endif
6984 return SQLITE_OK;
6985 editpage_fail:
6986 /* Unable to edit this page. Rebuild it from scratch instead. */
6987 populateCellCache(pCArray, iNew, nNew);
6988 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
6992 ** The following parameters determine how many adjacent pages get involved
6993 ** in a balancing operation. NN is the number of neighbors on either side
6994 ** of the page that participate in the balancing operation. NB is the
6995 ** total number of pages that participate, including the target page and
6996 ** NN neighbors on either side.
6998 ** The minimum value of NN is 1 (of course). Increasing NN above 1
6999 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
7000 ** in exchange for a larger degradation in INSERT and UPDATE performance.
7001 ** The value of NN appears to give the best results overall.
7003 #define NN 1 /* Number of neighbors on either side of pPage */
7004 #define NB (NN*2+1) /* Total pages involved in the balance */
7007 #ifndef SQLITE_OMIT_QUICKBALANCE
7009 ** This version of balance() handles the common special case where
7010 ** a new entry is being inserted on the extreme right-end of the
7011 ** tree, in other words, when the new entry will become the largest
7012 ** entry in the tree.
7014 ** Instead of trying to balance the 3 right-most leaf pages, just add
7015 ** a new page to the right-hand side and put the one new entry in
7016 ** that page. This leaves the right side of the tree somewhat
7017 ** unbalanced. But odds are that we will be inserting new entries
7018 ** at the end soon afterwards so the nearly empty page will quickly
7019 ** fill up. On average.
7021 ** pPage is the leaf page which is the right-most page in the tree.
7022 ** pParent is its parent. pPage must have a single overflow entry
7023 ** which is also the right-most entry on the page.
7025 ** The pSpace buffer is used to store a temporary copy of the divider
7026 ** cell that will be inserted into pParent. Such a cell consists of a 4
7027 ** byte page number followed by a variable length integer. In other
7028 ** words, at most 13 bytes. Hence the pSpace buffer must be at
7029 ** least 13 bytes in size.
7031 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
7032 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
7033 MemPage *pNew; /* Newly allocated page */
7034 int rc; /* Return Code */
7035 Pgno pgnoNew; /* Page number of pNew */
7037 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
7038 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7039 assert( pPage->nOverflow==1 );
7041 /* This error condition is now caught prior to reaching this function */
7042 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
7044 /* Allocate a new page. This page will become the right-sibling of
7045 ** pPage. Make the parent page writable, so that the new divider cell
7046 ** may be inserted. If both these operations are successful, proceed.
7048 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
7050 if( rc==SQLITE_OK ){
7052 u8 *pOut = &pSpace[4];
7053 u8 *pCell = pPage->apOvfl[0];
7054 u16 szCell = pPage->xCellSize(pPage, pCell);
7055 u8 *pStop;
7057 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
7058 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
7059 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
7060 rc = rebuildPage(pNew, 1, &pCell, &szCell);
7061 if( NEVER(rc) ) return rc;
7062 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
7064 /* If this is an auto-vacuum database, update the pointer map
7065 ** with entries for the new page, and any pointer from the
7066 ** cell on the page to an overflow page. If either of these
7067 ** operations fails, the return code is set, but the contents
7068 ** of the parent page are still manipulated by thh code below.
7069 ** That is Ok, at this point the parent page is guaranteed to
7070 ** be marked as dirty. Returning an error code will cause a
7071 ** rollback, undoing any changes made to the parent page.
7073 if( ISAUTOVACUUM ){
7074 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
7075 if( szCell>pNew->minLocal ){
7076 ptrmapPutOvflPtr(pNew, pCell, &rc);
7080 /* Create a divider cell to insert into pParent. The divider cell
7081 ** consists of a 4-byte page number (the page number of pPage) and
7082 ** a variable length key value (which must be the same value as the
7083 ** largest key on pPage).
7085 ** To find the largest key value on pPage, first find the right-most
7086 ** cell on pPage. The first two fields of this cell are the
7087 ** record-length (a variable length integer at most 32-bits in size)
7088 ** and the key value (a variable length integer, may have any value).
7089 ** The first of the while(...) loops below skips over the record-length
7090 ** field. The second while(...) loop copies the key value from the
7091 ** cell on pPage into the pSpace buffer.
7093 pCell = findCell(pPage, pPage->nCell-1);
7094 pStop = &pCell[9];
7095 while( (*(pCell++)&0x80) && pCell<pStop );
7096 pStop = &pCell[9];
7097 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
7099 /* Insert the new divider cell into pParent. */
7100 if( rc==SQLITE_OK ){
7101 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
7102 0, pPage->pgno, &rc);
7105 /* Set the right-child pointer of pParent to point to the new page. */
7106 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
7108 /* Release the reference to the new page. */
7109 releasePage(pNew);
7112 return rc;
7114 #endif /* SQLITE_OMIT_QUICKBALANCE */
7116 #if 0
7118 ** This function does not contribute anything to the operation of SQLite.
7119 ** it is sometimes activated temporarily while debugging code responsible
7120 ** for setting pointer-map entries.
7122 static int ptrmapCheckPages(MemPage **apPage, int nPage){
7123 int i, j;
7124 for(i=0; i<nPage; i++){
7125 Pgno n;
7126 u8 e;
7127 MemPage *pPage = apPage[i];
7128 BtShared *pBt = pPage->pBt;
7129 assert( pPage->isInit );
7131 for(j=0; j<pPage->nCell; j++){
7132 CellInfo info;
7133 u8 *z;
7135 z = findCell(pPage, j);
7136 pPage->xParseCell(pPage, z, &info);
7137 if( info.nLocal<info.nPayload ){
7138 Pgno ovfl = get4byte(&z[info.nSize-4]);
7139 ptrmapGet(pBt, ovfl, &e, &n);
7140 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
7142 if( !pPage->leaf ){
7143 Pgno child = get4byte(z);
7144 ptrmapGet(pBt, child, &e, &n);
7145 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7148 if( !pPage->leaf ){
7149 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
7150 ptrmapGet(pBt, child, &e, &n);
7151 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7154 return 1;
7156 #endif
7159 ** This function is used to copy the contents of the b-tree node stored
7160 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
7161 ** the pointer-map entries for each child page are updated so that the
7162 ** parent page stored in the pointer map is page pTo. If pFrom contained
7163 ** any cells with overflow page pointers, then the corresponding pointer
7164 ** map entries are also updated so that the parent page is page pTo.
7166 ** If pFrom is currently carrying any overflow cells (entries in the
7167 ** MemPage.apOvfl[] array), they are not copied to pTo.
7169 ** Before returning, page pTo is reinitialized using btreeInitPage().
7171 ** The performance of this function is not critical. It is only used by
7172 ** the balance_shallower() and balance_deeper() procedures, neither of
7173 ** which are called often under normal circumstances.
7175 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
7176 if( (*pRC)==SQLITE_OK ){
7177 BtShared * const pBt = pFrom->pBt;
7178 u8 * const aFrom = pFrom->aData;
7179 u8 * const aTo = pTo->aData;
7180 int const iFromHdr = pFrom->hdrOffset;
7181 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
7182 int rc;
7183 int iData;
7186 assert( pFrom->isInit );
7187 assert( pFrom->nFree>=iToHdr );
7188 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
7190 /* Copy the b-tree node content from page pFrom to page pTo. */
7191 iData = get2byte(&aFrom[iFromHdr+5]);
7192 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
7193 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
7195 /* Reinitialize page pTo so that the contents of the MemPage structure
7196 ** match the new data. The initialization of pTo can actually fail under
7197 ** fairly obscure circumstances, even though it is a copy of initialized
7198 ** page pFrom.
7200 pTo->isInit = 0;
7201 rc = btreeInitPage(pTo);
7202 if( rc!=SQLITE_OK ){
7203 *pRC = rc;
7204 return;
7207 /* If this is an auto-vacuum database, update the pointer-map entries
7208 ** for any b-tree or overflow pages that pTo now contains the pointers to.
7210 if( ISAUTOVACUUM ){
7211 *pRC = setChildPtrmaps(pTo);
7217 ** This routine redistributes cells on the iParentIdx'th child of pParent
7218 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
7219 ** same amount of free space. Usually a single sibling on either side of the
7220 ** page are used in the balancing, though both siblings might come from one
7221 ** side if the page is the first or last child of its parent. If the page
7222 ** has fewer than 2 siblings (something which can only happen if the page
7223 ** is a root page or a child of a root page) then all available siblings
7224 ** participate in the balancing.
7226 ** The number of siblings of the page might be increased or decreased by
7227 ** one or two in an effort to keep pages nearly full but not over full.
7229 ** Note that when this routine is called, some of the cells on the page
7230 ** might not actually be stored in MemPage.aData[]. This can happen
7231 ** if the page is overfull. This routine ensures that all cells allocated
7232 ** to the page and its siblings fit into MemPage.aData[] before returning.
7234 ** In the course of balancing the page and its siblings, cells may be
7235 ** inserted into or removed from the parent page (pParent). Doing so
7236 ** may cause the parent page to become overfull or underfull. If this
7237 ** happens, it is the responsibility of the caller to invoke the correct
7238 ** balancing routine to fix this problem (see the balance() routine).
7240 ** If this routine fails for any reason, it might leave the database
7241 ** in a corrupted state. So if this routine fails, the database should
7242 ** be rolled back.
7244 ** The third argument to this function, aOvflSpace, is a pointer to a
7245 ** buffer big enough to hold one page. If while inserting cells into the parent
7246 ** page (pParent) the parent page becomes overfull, this buffer is
7247 ** used to store the parent's overflow cells. Because this function inserts
7248 ** a maximum of four divider cells into the parent page, and the maximum
7249 ** size of a cell stored within an internal node is always less than 1/4
7250 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
7251 ** enough for all overflow cells.
7253 ** If aOvflSpace is set to a null pointer, this function returns
7254 ** SQLITE_NOMEM.
7256 static int balance_nonroot(
7257 MemPage *pParent, /* Parent page of siblings being balanced */
7258 int iParentIdx, /* Index of "the page" in pParent */
7259 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
7260 int isRoot, /* True if pParent is a root-page */
7261 int bBulk /* True if this call is part of a bulk load */
7263 BtShared *pBt; /* The whole database */
7264 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
7265 int nNew = 0; /* Number of pages in apNew[] */
7266 int nOld; /* Number of pages in apOld[] */
7267 int i, j, k; /* Loop counters */
7268 int nxDiv; /* Next divider slot in pParent->aCell[] */
7269 int rc = SQLITE_OK; /* The return code */
7270 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
7271 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
7272 int usableSpace; /* Bytes in pPage beyond the header */
7273 int pageFlags; /* Value of pPage->aData[0] */
7274 int iSpace1 = 0; /* First unused byte of aSpace1[] */
7275 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
7276 int szScratch; /* Size of scratch memory requested */
7277 MemPage *apOld[NB]; /* pPage and up to two siblings */
7278 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
7279 u8 *pRight; /* Location in parent of right-sibling pointer */
7280 u8 *apDiv[NB-1]; /* Divider cells in pParent */
7281 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
7282 int cntOld[NB+2]; /* Old index in b.apCell[] */
7283 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
7284 u8 *aSpace1; /* Space for copies of dividers cells */
7285 Pgno pgno; /* Temp var to store a page number in */
7286 u8 abDone[NB+2]; /* True after i'th new page is populated */
7287 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
7288 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
7289 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
7290 CellArray b; /* Parsed information on cells being balanced */
7292 memset(abDone, 0, sizeof(abDone));
7293 b.nCell = 0;
7294 b.apCell = 0;
7295 pBt = pParent->pBt;
7296 assert( sqlite3_mutex_held(pBt->mutex) );
7297 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7299 #if 0
7300 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
7301 #endif
7303 /* At this point pParent may have at most one overflow cell. And if
7304 ** this overflow cell is present, it must be the cell with
7305 ** index iParentIdx. This scenario comes about when this function
7306 ** is called (indirectly) from sqlite3BtreeDelete().
7308 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
7309 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
7311 if( !aOvflSpace ){
7312 return SQLITE_NOMEM_BKPT;
7315 /* Find the sibling pages to balance. Also locate the cells in pParent
7316 ** that divide the siblings. An attempt is made to find NN siblings on
7317 ** either side of pPage. More siblings are taken from one side, however,
7318 ** if there are fewer than NN siblings on the other side. If pParent
7319 ** has NB or fewer children then all children of pParent are taken.
7321 ** This loop also drops the divider cells from the parent page. This
7322 ** way, the remainder of the function does not have to deal with any
7323 ** overflow cells in the parent page, since if any existed they will
7324 ** have already been removed.
7326 i = pParent->nOverflow + pParent->nCell;
7327 if( i<2 ){
7328 nxDiv = 0;
7329 }else{
7330 assert( bBulk==0 || bBulk==1 );
7331 if( iParentIdx==0 ){
7332 nxDiv = 0;
7333 }else if( iParentIdx==i ){
7334 nxDiv = i-2+bBulk;
7335 }else{
7336 nxDiv = iParentIdx-1;
7338 i = 2-bBulk;
7340 nOld = i+1;
7341 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
7342 pRight = &pParent->aData[pParent->hdrOffset+8];
7343 }else{
7344 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
7346 pgno = get4byte(pRight);
7347 while( 1 ){
7348 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7349 if( rc ){
7350 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7351 goto balance_cleanup;
7353 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
7354 if( (i--)==0 ) break;
7356 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
7357 apDiv[i] = pParent->apOvfl[0];
7358 pgno = get4byte(apDiv[i]);
7359 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7360 pParent->nOverflow = 0;
7361 }else{
7362 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
7363 pgno = get4byte(apDiv[i]);
7364 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7366 /* Drop the cell from the parent page. apDiv[i] still points to
7367 ** the cell within the parent, even though it has been dropped.
7368 ** This is safe because dropping a cell only overwrites the first
7369 ** four bytes of it, and this function does not need the first
7370 ** four bytes of the divider cell. So the pointer is safe to use
7371 ** later on.
7373 ** But not if we are in secure-delete mode. In secure-delete mode,
7374 ** the dropCell() routine will overwrite the entire cell with zeroes.
7375 ** In this case, temporarily copy the cell into the aOvflSpace[]
7376 ** buffer. It will be copied out again as soon as the aSpace[] buffer
7377 ** is allocated. */
7378 if( pBt->btsFlags & BTS_FAST_SECURE ){
7379 int iOff;
7381 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
7382 if( (iOff+szNew[i])>(int)pBt->usableSize ){
7383 rc = SQLITE_CORRUPT_BKPT;
7384 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7385 goto balance_cleanup;
7386 }else{
7387 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
7388 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
7391 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
7395 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
7396 ** alignment */
7397 nMaxCells = (nMaxCells + 3)&~3;
7400 ** Allocate space for memory structures
7402 szScratch =
7403 nMaxCells*sizeof(u8*) /* b.apCell */
7404 + nMaxCells*sizeof(u16) /* b.szCell */
7405 + pBt->pageSize; /* aSpace1 */
7407 assert( szScratch<=6*(int)pBt->pageSize );
7408 b.apCell = sqlite3StackAllocRaw(0, szScratch );
7409 if( b.apCell==0 ){
7410 rc = SQLITE_NOMEM_BKPT;
7411 goto balance_cleanup;
7413 b.szCell = (u16*)&b.apCell[nMaxCells];
7414 aSpace1 = (u8*)&b.szCell[nMaxCells];
7415 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
7418 ** Load pointers to all cells on sibling pages and the divider cells
7419 ** into the local b.apCell[] array. Make copies of the divider cells
7420 ** into space obtained from aSpace1[]. The divider cells have already
7421 ** been removed from pParent.
7423 ** If the siblings are on leaf pages, then the child pointers of the
7424 ** divider cells are stripped from the cells before they are copied
7425 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
7426 ** child pointers. If siblings are not leaves, then all cell in
7427 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
7428 ** are alike.
7430 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
7431 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
7433 b.pRef = apOld[0];
7434 leafCorrection = b.pRef->leaf*4;
7435 leafData = b.pRef->intKeyLeaf;
7436 for(i=0; i<nOld; i++){
7437 MemPage *pOld = apOld[i];
7438 int limit = pOld->nCell;
7439 u8 *aData = pOld->aData;
7440 u16 maskPage = pOld->maskPage;
7441 u8 *piCell = aData + pOld->cellOffset;
7442 u8 *piEnd;
7444 /* Verify that all sibling pages are of the same "type" (table-leaf,
7445 ** table-interior, index-leaf, or index-interior).
7447 if( pOld->aData[0]!=apOld[0]->aData[0] ){
7448 rc = SQLITE_CORRUPT_BKPT;
7449 goto balance_cleanup;
7452 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
7453 ** contains overflow cells, include them in the b.apCell[] array
7454 ** in the correct spot.
7456 ** Note that when there are multiple overflow cells, it is always the
7457 ** case that they are sequential and adjacent. This invariant arises
7458 ** because multiple overflows can only occurs when inserting divider
7459 ** cells into a parent on a prior balance, and divider cells are always
7460 ** adjacent and are inserted in order. There is an assert() tagged
7461 ** with "NOTE 1" in the overflow cell insertion loop to prove this
7462 ** invariant.
7464 ** This must be done in advance. Once the balance starts, the cell
7465 ** offset section of the btree page will be overwritten and we will no
7466 ** long be able to find the cells if a pointer to each cell is not saved
7467 ** first.
7469 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
7470 if( pOld->nOverflow>0 ){
7471 limit = pOld->aiOvfl[0];
7472 for(j=0; j<limit; j++){
7473 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7474 piCell += 2;
7475 b.nCell++;
7477 for(k=0; k<pOld->nOverflow; k++){
7478 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
7479 b.apCell[b.nCell] = pOld->apOvfl[k];
7480 b.nCell++;
7483 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
7484 while( piCell<piEnd ){
7485 assert( b.nCell<nMaxCells );
7486 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7487 piCell += 2;
7488 b.nCell++;
7491 cntOld[i] = b.nCell;
7492 if( i<nOld-1 && !leafData){
7493 u16 sz = (u16)szNew[i];
7494 u8 *pTemp;
7495 assert( b.nCell<nMaxCells );
7496 b.szCell[b.nCell] = sz;
7497 pTemp = &aSpace1[iSpace1];
7498 iSpace1 += sz;
7499 assert( sz<=pBt->maxLocal+23 );
7500 assert( iSpace1 <= (int)pBt->pageSize );
7501 memcpy(pTemp, apDiv[i], sz);
7502 b.apCell[b.nCell] = pTemp+leafCorrection;
7503 assert( leafCorrection==0 || leafCorrection==4 );
7504 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
7505 if( !pOld->leaf ){
7506 assert( leafCorrection==0 );
7507 assert( pOld->hdrOffset==0 );
7508 /* The right pointer of the child page pOld becomes the left
7509 ** pointer of the divider cell */
7510 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
7511 }else{
7512 assert( leafCorrection==4 );
7513 while( b.szCell[b.nCell]<4 ){
7514 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
7515 ** does exist, pad it with 0x00 bytes. */
7516 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
7517 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
7518 aSpace1[iSpace1++] = 0x00;
7519 b.szCell[b.nCell]++;
7522 b.nCell++;
7527 ** Figure out the number of pages needed to hold all b.nCell cells.
7528 ** Store this number in "k". Also compute szNew[] which is the total
7529 ** size of all cells on the i-th page and cntNew[] which is the index
7530 ** in b.apCell[] of the cell that divides page i from page i+1.
7531 ** cntNew[k] should equal b.nCell.
7533 ** Values computed by this block:
7535 ** k: The total number of sibling pages
7536 ** szNew[i]: Spaced used on the i-th sibling page.
7537 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
7538 ** the right of the i-th sibling page.
7539 ** usableSpace: Number of bytes of space available on each sibling.
7542 usableSpace = pBt->usableSize - 12 + leafCorrection;
7543 for(i=0; i<nOld; i++){
7544 MemPage *p = apOld[i];
7545 szNew[i] = usableSpace - p->nFree;
7546 for(j=0; j<p->nOverflow; j++){
7547 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
7549 cntNew[i] = cntOld[i];
7551 k = nOld;
7552 for(i=0; i<k; i++){
7553 int sz;
7554 while( szNew[i]>usableSpace ){
7555 if( i+1>=k ){
7556 k = i+2;
7557 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
7558 szNew[k-1] = 0;
7559 cntNew[k-1] = b.nCell;
7561 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
7562 szNew[i] -= sz;
7563 if( !leafData ){
7564 if( cntNew[i]<b.nCell ){
7565 sz = 2 + cachedCellSize(&b, cntNew[i]);
7566 }else{
7567 sz = 0;
7570 szNew[i+1] += sz;
7571 cntNew[i]--;
7573 while( cntNew[i]<b.nCell ){
7574 sz = 2 + cachedCellSize(&b, cntNew[i]);
7575 if( szNew[i]+sz>usableSpace ) break;
7576 szNew[i] += sz;
7577 cntNew[i]++;
7578 if( !leafData ){
7579 if( cntNew[i]<b.nCell ){
7580 sz = 2 + cachedCellSize(&b, cntNew[i]);
7581 }else{
7582 sz = 0;
7585 szNew[i+1] -= sz;
7587 if( cntNew[i]>=b.nCell ){
7588 k = i+1;
7589 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
7590 rc = SQLITE_CORRUPT_BKPT;
7591 goto balance_cleanup;
7596 ** The packing computed by the previous block is biased toward the siblings
7597 ** on the left side (siblings with smaller keys). The left siblings are
7598 ** always nearly full, while the right-most sibling might be nearly empty.
7599 ** The next block of code attempts to adjust the packing of siblings to
7600 ** get a better balance.
7602 ** This adjustment is more than an optimization. The packing above might
7603 ** be so out of balance as to be illegal. For example, the right-most
7604 ** sibling might be completely empty. This adjustment is not optional.
7606 for(i=k-1; i>0; i--){
7607 int szRight = szNew[i]; /* Size of sibling on the right */
7608 int szLeft = szNew[i-1]; /* Size of sibling on the left */
7609 int r; /* Index of right-most cell in left sibling */
7610 int d; /* Index of first cell to the left of right sibling */
7612 r = cntNew[i-1] - 1;
7613 d = r + 1 - leafData;
7614 (void)cachedCellSize(&b, d);
7616 assert( d<nMaxCells );
7617 assert( r<nMaxCells );
7618 (void)cachedCellSize(&b, r);
7619 if( szRight!=0
7620 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
7621 break;
7623 szRight += b.szCell[d] + 2;
7624 szLeft -= b.szCell[r] + 2;
7625 cntNew[i-1] = r;
7626 r--;
7627 d--;
7628 }while( r>=0 );
7629 szNew[i] = szRight;
7630 szNew[i-1] = szLeft;
7631 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
7632 rc = SQLITE_CORRUPT_BKPT;
7633 goto balance_cleanup;
7637 /* Sanity check: For a non-corrupt database file one of the follwing
7638 ** must be true:
7639 ** (1) We found one or more cells (cntNew[0])>0), or
7640 ** (2) pPage is a virtual root page. A virtual root page is when
7641 ** the real root page is page 1 and we are the only child of
7642 ** that page.
7644 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
7645 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
7646 apOld[0]->pgno, apOld[0]->nCell,
7647 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
7648 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
7652 ** Allocate k new pages. Reuse old pages where possible.
7654 pageFlags = apOld[0]->aData[0];
7655 for(i=0; i<k; i++){
7656 MemPage *pNew;
7657 if( i<nOld ){
7658 pNew = apNew[i] = apOld[i];
7659 apOld[i] = 0;
7660 rc = sqlite3PagerWrite(pNew->pDbPage);
7661 nNew++;
7662 if( rc ) goto balance_cleanup;
7663 }else{
7664 assert( i>0 );
7665 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
7666 if( rc ) goto balance_cleanup;
7667 zeroPage(pNew, pageFlags);
7668 apNew[i] = pNew;
7669 nNew++;
7670 cntOld[i] = b.nCell;
7672 /* Set the pointer-map entry for the new sibling page. */
7673 if( ISAUTOVACUUM ){
7674 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
7675 if( rc!=SQLITE_OK ){
7676 goto balance_cleanup;
7683 ** Reassign page numbers so that the new pages are in ascending order.
7684 ** This helps to keep entries in the disk file in order so that a scan
7685 ** of the table is closer to a linear scan through the file. That in turn
7686 ** helps the operating system to deliver pages from the disk more rapidly.
7688 ** An O(n^2) insertion sort algorithm is used, but since n is never more
7689 ** than (NB+2) (a small constant), that should not be a problem.
7691 ** When NB==3, this one optimization makes the database about 25% faster
7692 ** for large insertions and deletions.
7694 for(i=0; i<nNew; i++){
7695 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
7696 aPgFlags[i] = apNew[i]->pDbPage->flags;
7697 for(j=0; j<i; j++){
7698 if( aPgno[j]==aPgno[i] ){
7699 /* This branch is taken if the set of sibling pages somehow contains
7700 ** duplicate entries. This can happen if the database is corrupt.
7701 ** It would be simpler to detect this as part of the loop below, but
7702 ** we do the detection here in order to avoid populating the pager
7703 ** cache with two separate objects associated with the same
7704 ** page number. */
7705 assert( CORRUPT_DB );
7706 rc = SQLITE_CORRUPT_BKPT;
7707 goto balance_cleanup;
7711 for(i=0; i<nNew; i++){
7712 int iBest = 0; /* aPgno[] index of page number to use */
7713 for(j=1; j<nNew; j++){
7714 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
7716 pgno = aPgOrder[iBest];
7717 aPgOrder[iBest] = 0xffffffff;
7718 if( iBest!=i ){
7719 if( iBest>i ){
7720 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
7722 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
7723 apNew[i]->pgno = pgno;
7727 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
7728 "%d(%d nc=%d) %d(%d nc=%d)\n",
7729 apNew[0]->pgno, szNew[0], cntNew[0],
7730 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
7731 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
7732 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
7733 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
7734 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
7735 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
7736 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
7737 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
7740 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7741 put4byte(pRight, apNew[nNew-1]->pgno);
7743 /* If the sibling pages are not leaves, ensure that the right-child pointer
7744 ** of the right-most new sibling page is set to the value that was
7745 ** originally in the same field of the right-most old sibling page. */
7746 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
7747 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
7748 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
7751 /* Make any required updates to pointer map entries associated with
7752 ** cells stored on sibling pages following the balance operation. Pointer
7753 ** map entries associated with divider cells are set by the insertCell()
7754 ** routine. The associated pointer map entries are:
7756 ** a) if the cell contains a reference to an overflow chain, the
7757 ** entry associated with the first page in the overflow chain, and
7759 ** b) if the sibling pages are not leaves, the child page associated
7760 ** with the cell.
7762 ** If the sibling pages are not leaves, then the pointer map entry
7763 ** associated with the right-child of each sibling may also need to be
7764 ** updated. This happens below, after the sibling pages have been
7765 ** populated, not here.
7767 if( ISAUTOVACUUM ){
7768 MemPage *pNew = apNew[0];
7769 u8 *aOld = pNew->aData;
7770 int cntOldNext = pNew->nCell + pNew->nOverflow;
7771 int usableSize = pBt->usableSize;
7772 int iNew = 0;
7773 int iOld = 0;
7775 for(i=0; i<b.nCell; i++){
7776 u8 *pCell = b.apCell[i];
7777 if( i==cntOldNext ){
7778 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
7779 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
7780 aOld = pOld->aData;
7782 if( i==cntNew[iNew] ){
7783 pNew = apNew[++iNew];
7784 if( !leafData ) continue;
7787 /* Cell pCell is destined for new sibling page pNew. Originally, it
7788 ** was either part of sibling page iOld (possibly an overflow cell),
7789 ** or else the divider cell to the left of sibling page iOld. So,
7790 ** if sibling page iOld had the same page number as pNew, and if
7791 ** pCell really was a part of sibling page iOld (not a divider or
7792 ** overflow cell), we can skip updating the pointer map entries. */
7793 if( iOld>=nNew
7794 || pNew->pgno!=aPgno[iOld]
7795 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
7797 if( !leafCorrection ){
7798 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
7800 if( cachedCellSize(&b,i)>pNew->minLocal ){
7801 ptrmapPutOvflPtr(pNew, pCell, &rc);
7803 if( rc ) goto balance_cleanup;
7808 /* Insert new divider cells into pParent. */
7809 for(i=0; i<nNew-1; i++){
7810 u8 *pCell;
7811 u8 *pTemp;
7812 int sz;
7813 MemPage *pNew = apNew[i];
7814 j = cntNew[i];
7816 assert( j<nMaxCells );
7817 assert( b.apCell[j]!=0 );
7818 pCell = b.apCell[j];
7819 sz = b.szCell[j] + leafCorrection;
7820 pTemp = &aOvflSpace[iOvflSpace];
7821 if( !pNew->leaf ){
7822 memcpy(&pNew->aData[8], pCell, 4);
7823 }else if( leafData ){
7824 /* If the tree is a leaf-data tree, and the siblings are leaves,
7825 ** then there is no divider cell in b.apCell[]. Instead, the divider
7826 ** cell consists of the integer key for the right-most cell of
7827 ** the sibling-page assembled above only.
7829 CellInfo info;
7830 j--;
7831 pNew->xParseCell(pNew, b.apCell[j], &info);
7832 pCell = pTemp;
7833 sz = 4 + putVarint(&pCell[4], info.nKey);
7834 pTemp = 0;
7835 }else{
7836 pCell -= 4;
7837 /* Obscure case for non-leaf-data trees: If the cell at pCell was
7838 ** previously stored on a leaf node, and its reported size was 4
7839 ** bytes, then it may actually be smaller than this
7840 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
7841 ** any cell). But it is important to pass the correct size to
7842 ** insertCell(), so reparse the cell now.
7844 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
7845 ** and WITHOUT ROWID tables with exactly one column which is the
7846 ** primary key.
7848 if( b.szCell[j]==4 ){
7849 assert(leafCorrection==4);
7850 sz = pParent->xCellSize(pParent, pCell);
7853 iOvflSpace += sz;
7854 assert( sz<=pBt->maxLocal+23 );
7855 assert( iOvflSpace <= (int)pBt->pageSize );
7856 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
7857 if( rc!=SQLITE_OK ) goto balance_cleanup;
7858 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7861 /* Now update the actual sibling pages. The order in which they are updated
7862 ** is important, as this code needs to avoid disrupting any page from which
7863 ** cells may still to be read. In practice, this means:
7865 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
7866 ** then it is not safe to update page apNew[iPg] until after
7867 ** the left-hand sibling apNew[iPg-1] has been updated.
7869 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
7870 ** then it is not safe to update page apNew[iPg] until after
7871 ** the right-hand sibling apNew[iPg+1] has been updated.
7873 ** If neither of the above apply, the page is safe to update.
7875 ** The iPg value in the following loop starts at nNew-1 goes down
7876 ** to 0, then back up to nNew-1 again, thus making two passes over
7877 ** the pages. On the initial downward pass, only condition (1) above
7878 ** needs to be tested because (2) will always be true from the previous
7879 ** step. On the upward pass, both conditions are always true, so the
7880 ** upwards pass simply processes pages that were missed on the downward
7881 ** pass.
7883 for(i=1-nNew; i<nNew; i++){
7884 int iPg = i<0 ? -i : i;
7885 assert( iPg>=0 && iPg<nNew );
7886 if( abDone[iPg] ) continue; /* Skip pages already processed */
7887 if( i>=0 /* On the upwards pass, or... */
7888 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
7890 int iNew;
7891 int iOld;
7892 int nNewCell;
7894 /* Verify condition (1): If cells are moving left, update iPg
7895 ** only after iPg-1 has already been updated. */
7896 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
7898 /* Verify condition (2): If cells are moving right, update iPg
7899 ** only after iPg+1 has already been updated. */
7900 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
7902 if( iPg==0 ){
7903 iNew = iOld = 0;
7904 nNewCell = cntNew[0];
7905 }else{
7906 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
7907 iNew = cntNew[iPg-1] + !leafData;
7908 nNewCell = cntNew[iPg] - iNew;
7911 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
7912 if( rc ) goto balance_cleanup;
7913 abDone[iPg]++;
7914 apNew[iPg]->nFree = usableSpace-szNew[iPg];
7915 assert( apNew[iPg]->nOverflow==0 );
7916 assert( apNew[iPg]->nCell==nNewCell );
7920 /* All pages have been processed exactly once */
7921 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
7923 assert( nOld>0 );
7924 assert( nNew>0 );
7926 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
7927 /* The root page of the b-tree now contains no cells. The only sibling
7928 ** page is the right-child of the parent. Copy the contents of the
7929 ** child page into the parent, decreasing the overall height of the
7930 ** b-tree structure by one. This is described as the "balance-shallower"
7931 ** sub-algorithm in some documentation.
7933 ** If this is an auto-vacuum database, the call to copyNodeContent()
7934 ** sets all pointer-map entries corresponding to database image pages
7935 ** for which the pointer is stored within the content being copied.
7937 ** It is critical that the child page be defragmented before being
7938 ** copied into the parent, because if the parent is page 1 then it will
7939 ** by smaller than the child due to the database header, and so all the
7940 ** free space needs to be up front.
7942 assert( nNew==1 || CORRUPT_DB );
7943 rc = defragmentPage(apNew[0], -1);
7944 testcase( rc!=SQLITE_OK );
7945 assert( apNew[0]->nFree ==
7946 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
7947 || rc!=SQLITE_OK
7949 copyNodeContent(apNew[0], pParent, &rc);
7950 freePage(apNew[0], &rc);
7951 }else if( ISAUTOVACUUM && !leafCorrection ){
7952 /* Fix the pointer map entries associated with the right-child of each
7953 ** sibling page. All other pointer map entries have already been taken
7954 ** care of. */
7955 for(i=0; i<nNew; i++){
7956 u32 key = get4byte(&apNew[i]->aData[8]);
7957 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
7961 assert( pParent->isInit );
7962 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
7963 nOld, nNew, b.nCell));
7965 /* Free any old pages that were not reused as new pages.
7967 for(i=nNew; i<nOld; i++){
7968 freePage(apOld[i], &rc);
7971 #if 0
7972 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
7973 /* The ptrmapCheckPages() contains assert() statements that verify that
7974 ** all pointer map pages are set correctly. This is helpful while
7975 ** debugging. This is usually disabled because a corrupt database may
7976 ** cause an assert() statement to fail. */
7977 ptrmapCheckPages(apNew, nNew);
7978 ptrmapCheckPages(&pParent, 1);
7980 #endif
7983 ** Cleanup before returning.
7985 balance_cleanup:
7986 sqlite3StackFree(0, b.apCell);
7987 for(i=0; i<nOld; i++){
7988 releasePage(apOld[i]);
7990 for(i=0; i<nNew; i++){
7991 releasePage(apNew[i]);
7994 return rc;
7999 ** This function is called when the root page of a b-tree structure is
8000 ** overfull (has one or more overflow pages).
8002 ** A new child page is allocated and the contents of the current root
8003 ** page, including overflow cells, are copied into the child. The root
8004 ** page is then overwritten to make it an empty page with the right-child
8005 ** pointer pointing to the new page.
8007 ** Before returning, all pointer-map entries corresponding to pages
8008 ** that the new child-page now contains pointers to are updated. The
8009 ** entry corresponding to the new right-child pointer of the root
8010 ** page is also updated.
8012 ** If successful, *ppChild is set to contain a reference to the child
8013 ** page and SQLITE_OK is returned. In this case the caller is required
8014 ** to call releasePage() on *ppChild exactly once. If an error occurs,
8015 ** an error code is returned and *ppChild is set to 0.
8017 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
8018 int rc; /* Return value from subprocedures */
8019 MemPage *pChild = 0; /* Pointer to a new child page */
8020 Pgno pgnoChild = 0; /* Page number of the new child page */
8021 BtShared *pBt = pRoot->pBt; /* The BTree */
8023 assert( pRoot->nOverflow>0 );
8024 assert( sqlite3_mutex_held(pBt->mutex) );
8026 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
8027 ** page that will become the new right-child of pPage. Copy the contents
8028 ** of the node stored on pRoot into the new child page.
8030 rc = sqlite3PagerWrite(pRoot->pDbPage);
8031 if( rc==SQLITE_OK ){
8032 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
8033 copyNodeContent(pRoot, pChild, &rc);
8034 if( ISAUTOVACUUM ){
8035 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
8038 if( rc ){
8039 *ppChild = 0;
8040 releasePage(pChild);
8041 return rc;
8043 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
8044 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8045 assert( pChild->nCell==pRoot->nCell );
8047 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
8049 /* Copy the overflow cells from pRoot to pChild */
8050 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
8051 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
8052 memcpy(pChild->apOvfl, pRoot->apOvfl,
8053 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
8054 pChild->nOverflow = pRoot->nOverflow;
8056 /* Zero the contents of pRoot. Then install pChild as the right-child. */
8057 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
8058 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
8060 *ppChild = pChild;
8061 return SQLITE_OK;
8065 ** The page that pCur currently points to has just been modified in
8066 ** some way. This function figures out if this modification means the
8067 ** tree needs to be balanced, and if so calls the appropriate balancing
8068 ** routine. Balancing routines are:
8070 ** balance_quick()
8071 ** balance_deeper()
8072 ** balance_nonroot()
8074 static int balance(BtCursor *pCur){
8075 int rc = SQLITE_OK;
8076 const int nMin = pCur->pBt->usableSize * 2 / 3;
8077 u8 aBalanceQuickSpace[13];
8078 u8 *pFree = 0;
8080 VVA_ONLY( int balance_quick_called = 0 );
8081 VVA_ONLY( int balance_deeper_called = 0 );
8083 do {
8084 int iPage = pCur->iPage;
8085 MemPage *pPage = pCur->pPage;
8087 if( iPage==0 ){
8088 if( pPage->nOverflow ){
8089 /* The root page of the b-tree is overfull. In this case call the
8090 ** balance_deeper() function to create a new child for the root-page
8091 ** and copy the current contents of the root-page to it. The
8092 ** next iteration of the do-loop will balance the child page.
8094 assert( balance_deeper_called==0 );
8095 VVA_ONLY( balance_deeper_called++ );
8096 rc = balance_deeper(pPage, &pCur->apPage[1]);
8097 if( rc==SQLITE_OK ){
8098 pCur->iPage = 1;
8099 pCur->ix = 0;
8100 pCur->aiIdx[0] = 0;
8101 pCur->apPage[0] = pPage;
8102 pCur->pPage = pCur->apPage[1];
8103 assert( pCur->pPage->nOverflow );
8105 }else{
8106 break;
8108 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
8109 break;
8110 }else{
8111 MemPage * const pParent = pCur->apPage[iPage-1];
8112 int const iIdx = pCur->aiIdx[iPage-1];
8114 rc = sqlite3PagerWrite(pParent->pDbPage);
8115 if( rc==SQLITE_OK ){
8116 #ifndef SQLITE_OMIT_QUICKBALANCE
8117 if( pPage->intKeyLeaf
8118 && pPage->nOverflow==1
8119 && pPage->aiOvfl[0]==pPage->nCell
8120 && pParent->pgno!=1
8121 && pParent->nCell==iIdx
8123 /* Call balance_quick() to create a new sibling of pPage on which
8124 ** to store the overflow cell. balance_quick() inserts a new cell
8125 ** into pParent, which may cause pParent overflow. If this
8126 ** happens, the next iteration of the do-loop will balance pParent
8127 ** use either balance_nonroot() or balance_deeper(). Until this
8128 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
8129 ** buffer.
8131 ** The purpose of the following assert() is to check that only a
8132 ** single call to balance_quick() is made for each call to this
8133 ** function. If this were not verified, a subtle bug involving reuse
8134 ** of the aBalanceQuickSpace[] might sneak in.
8136 assert( balance_quick_called==0 );
8137 VVA_ONLY( balance_quick_called++ );
8138 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
8139 }else
8140 #endif
8142 /* In this case, call balance_nonroot() to redistribute cells
8143 ** between pPage and up to 2 of its sibling pages. This involves
8144 ** modifying the contents of pParent, which may cause pParent to
8145 ** become overfull or underfull. The next iteration of the do-loop
8146 ** will balance the parent page to correct this.
8148 ** If the parent page becomes overfull, the overflow cell or cells
8149 ** are stored in the pSpace buffer allocated immediately below.
8150 ** A subsequent iteration of the do-loop will deal with this by
8151 ** calling balance_nonroot() (balance_deeper() may be called first,
8152 ** but it doesn't deal with overflow cells - just moves them to a
8153 ** different page). Once this subsequent call to balance_nonroot()
8154 ** has completed, it is safe to release the pSpace buffer used by
8155 ** the previous call, as the overflow cell data will have been
8156 ** copied either into the body of a database page or into the new
8157 ** pSpace buffer passed to the latter call to balance_nonroot().
8159 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
8160 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
8161 pCur->hints&BTREE_BULKLOAD);
8162 if( pFree ){
8163 /* If pFree is not NULL, it points to the pSpace buffer used
8164 ** by a previous call to balance_nonroot(). Its contents are
8165 ** now stored either on real database pages or within the
8166 ** new pSpace buffer, so it may be safely freed here. */
8167 sqlite3PageFree(pFree);
8170 /* The pSpace buffer will be freed after the next call to
8171 ** balance_nonroot(), or just before this function returns, whichever
8172 ** comes first. */
8173 pFree = pSpace;
8177 pPage->nOverflow = 0;
8179 /* The next iteration of the do-loop balances the parent page. */
8180 releasePage(pPage);
8181 pCur->iPage--;
8182 assert( pCur->iPage>=0 );
8183 pCur->pPage = pCur->apPage[pCur->iPage];
8185 }while( rc==SQLITE_OK );
8187 if( pFree ){
8188 sqlite3PageFree(pFree);
8190 return rc;
8193 /* Overwrite content from pX into pDest. Only do the write if the
8194 ** content is different from what is already there.
8196 static int btreeOverwriteContent(
8197 MemPage *pPage, /* MemPage on which writing will occur */
8198 u8 *pDest, /* Pointer to the place to start writing */
8199 const BtreePayload *pX, /* Source of data to write */
8200 int iOffset, /* Offset of first byte to write */
8201 int iAmt /* Number of bytes to be written */
8203 int nData = pX->nData - iOffset;
8204 if( nData<=0 ){
8205 /* Overwritting with zeros */
8206 int i;
8207 for(i=0; i<iAmt && pDest[i]==0; i++){}
8208 if( i<iAmt ){
8209 int rc = sqlite3PagerWrite(pPage->pDbPage);
8210 if( rc ) return rc;
8211 memset(pDest + i, 0, iAmt - i);
8213 }else{
8214 if( nData<iAmt ){
8215 /* Mixed read data and zeros at the end. Make a recursive call
8216 ** to write the zeros then fall through to write the real data */
8217 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
8218 iAmt-nData);
8219 if( rc ) return rc;
8220 iAmt = nData;
8222 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
8223 int rc = sqlite3PagerWrite(pPage->pDbPage);
8224 if( rc ) return rc;
8225 memcpy(pDest, ((u8*)pX->pData) + iOffset, iAmt);
8228 return SQLITE_OK;
8232 ** Overwrite the cell that cursor pCur is pointing to with fresh content
8233 ** contained in pX.
8235 static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
8236 int iOffset; /* Next byte of pX->pData to write */
8237 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
8238 int rc; /* Return code */
8239 MemPage *pPage = pCur->pPage; /* Page being written */
8240 BtShared *pBt; /* Btree */
8241 Pgno ovflPgno; /* Next overflow page to write */
8242 u32 ovflPageSize; /* Size to write on overflow page */
8244 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
8245 return SQLITE_CORRUPT_BKPT;
8247 /* Overwrite the local portion first */
8248 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
8249 0, pCur->info.nLocal);
8250 if( rc ) return rc;
8251 if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
8253 /* Now overwrite the overflow pages */
8254 iOffset = pCur->info.nLocal;
8255 assert( nTotal>=0 );
8256 assert( iOffset>=0 );
8257 ovflPgno = get4byte(pCur->info.pPayload + iOffset);
8258 pBt = pPage->pBt;
8259 ovflPageSize = pBt->usableSize - 4;
8261 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
8262 if( rc ) return rc;
8263 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
8264 rc = SQLITE_CORRUPT_BKPT;
8265 }else{
8266 if( iOffset+ovflPageSize<(u32)nTotal ){
8267 ovflPgno = get4byte(pPage->aData);
8268 }else{
8269 ovflPageSize = nTotal - iOffset;
8271 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
8272 iOffset, ovflPageSize);
8274 sqlite3PagerUnref(pPage->pDbPage);
8275 if( rc ) return rc;
8276 iOffset += ovflPageSize;
8277 }while( iOffset<nTotal );
8278 return SQLITE_OK;
8283 ** Insert a new record into the BTree. The content of the new record
8284 ** is described by the pX object. The pCur cursor is used only to
8285 ** define what table the record should be inserted into, and is left
8286 ** pointing at a random location.
8288 ** For a table btree (used for rowid tables), only the pX.nKey value of
8289 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
8290 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
8291 ** hold the content of the row.
8293 ** For an index btree (used for indexes and WITHOUT ROWID tables), the
8294 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
8295 ** pX.pData,nData,nZero fields must be zero.
8297 ** If the seekResult parameter is non-zero, then a successful call to
8298 ** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
8299 ** been performed. In other words, if seekResult!=0 then the cursor
8300 ** is currently pointing to a cell that will be adjacent to the cell
8301 ** to be inserted. If seekResult<0 then pCur points to a cell that is
8302 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
8303 ** that is larger than (pKey,nKey).
8305 ** If seekResult==0, that means pCur is pointing at some unknown location.
8306 ** In that case, this routine must seek the cursor to the correct insertion
8307 ** point for (pKey,nKey) before doing the insertion. For index btrees,
8308 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
8309 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
8310 ** to decode the key.
8312 int sqlite3BtreeInsert(
8313 BtCursor *pCur, /* Insert data into the table of this cursor */
8314 const BtreePayload *pX, /* Content of the row to be inserted */
8315 int flags, /* True if this is likely an append */
8316 int seekResult /* Result of prior MovetoUnpacked() call */
8318 int rc;
8319 int loc = seekResult; /* -1: before desired location +1: after */
8320 int szNew = 0;
8321 int idx;
8322 MemPage *pPage;
8323 Btree *p = pCur->pBtree;
8324 BtShared *pBt = p->pBt;
8325 unsigned char *oldCell;
8326 unsigned char *newCell = 0;
8328 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
8330 if( pCur->eState==CURSOR_FAULT ){
8331 assert( pCur->skipNext!=SQLITE_OK );
8332 return pCur->skipNext;
8335 assert( cursorOwnsBtShared(pCur) );
8336 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
8337 && pBt->inTransaction==TRANS_WRITE
8338 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
8339 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
8341 /* Assert that the caller has been consistent. If this cursor was opened
8342 ** expecting an index b-tree, then the caller should be inserting blob
8343 ** keys with no associated data. If the cursor was opened expecting an
8344 ** intkey table, the caller should be inserting integer keys with a
8345 ** blob of associated data. */
8346 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
8348 /* Save the positions of any other cursors open on this table.
8350 ** In some cases, the call to btreeMoveto() below is a no-op. For
8351 ** example, when inserting data into a table with auto-generated integer
8352 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
8353 ** integer key to use. It then calls this function to actually insert the
8354 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
8355 ** that the cursor is already where it needs to be and returns without
8356 ** doing any work. To avoid thwarting these optimizations, it is important
8357 ** not to clear the cursor here.
8359 if( pCur->curFlags & BTCF_Multiple ){
8360 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8361 if( rc ) return rc;
8364 if( pCur->pKeyInfo==0 ){
8365 assert( pX->pKey==0 );
8366 /* If this is an insert into a table b-tree, invalidate any incrblob
8367 ** cursors open on the row being replaced */
8368 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
8370 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
8371 ** to a row with the same key as the new entry being inserted.
8373 #ifdef SQLITE_DEBUG
8374 if( flags & BTREE_SAVEPOSITION ){
8375 assert( pCur->curFlags & BTCF_ValidNKey );
8376 assert( pX->nKey==pCur->info.nKey );
8377 assert( pCur->info.nSize!=0 );
8378 assert( loc==0 );
8380 #endif
8382 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply
8383 ** that the cursor is not pointing to a row to be overwritten.
8384 ** So do a complete check.
8386 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
8387 /* The cursor is pointing to the entry that is to be
8388 ** overwritten */
8389 assert( pX->nData>=0 && pX->nZero>=0 );
8390 if( pCur->info.nSize!=0
8391 && pCur->info.nPayload==(u32)pX->nData+pX->nZero
8393 /* New entry is the same size as the old. Do an overwrite */
8394 return btreeOverwriteCell(pCur, pX);
8396 assert( loc==0 );
8397 }else if( loc==0 ){
8398 /* The cursor is *not* pointing to the cell to be overwritten, nor
8399 ** to an adjacent cell. Move the cursor so that it is pointing either
8400 ** to the cell to be overwritten or an adjacent cell.
8402 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
8403 if( rc ) return rc;
8405 }else{
8406 /* This is an index or a WITHOUT ROWID table */
8408 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
8409 ** to a row with the same key as the new entry being inserted.
8411 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 );
8413 /* If the cursor is not already pointing either to the cell to be
8414 ** overwritten, or if a new cell is being inserted, if the cursor is
8415 ** not pointing to an immediately adjacent cell, then move the cursor
8416 ** so that it does.
8418 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
8419 if( pX->nMem ){
8420 UnpackedRecord r;
8421 r.pKeyInfo = pCur->pKeyInfo;
8422 r.aMem = pX->aMem;
8423 r.nField = pX->nMem;
8424 r.default_rc = 0;
8425 r.errCode = 0;
8426 r.r1 = 0;
8427 r.r2 = 0;
8428 r.eqSeen = 0;
8429 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
8430 }else{
8431 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
8433 if( rc ) return rc;
8436 /* If the cursor is currently pointing to an entry to be overwritten
8437 ** and the new content is the same as as the old, then use the
8438 ** overwrite optimization.
8440 if( loc==0 ){
8441 getCellInfo(pCur);
8442 if( pCur->info.nKey==pX->nKey ){
8443 BtreePayload x2;
8444 x2.pData = pX->pKey;
8445 x2.nData = pX->nKey;
8446 x2.nZero = 0;
8447 return btreeOverwriteCell(pCur, &x2);
8452 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
8454 pPage = pCur->pPage;
8455 assert( pPage->intKey || pX->nKey>=0 );
8456 assert( pPage->leaf || !pPage->intKey );
8458 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
8459 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
8460 loc==0 ? "overwrite" : "new entry"));
8461 assert( pPage->isInit );
8462 newCell = pBt->pTmpSpace;
8463 assert( newCell!=0 );
8464 rc = fillInCell(pPage, newCell, pX, &szNew);
8465 if( rc ) goto end_insert;
8466 assert( szNew==pPage->xCellSize(pPage, newCell) );
8467 assert( szNew <= MX_CELL_SIZE(pBt) );
8468 idx = pCur->ix;
8469 if( loc==0 ){
8470 CellInfo info;
8471 assert( idx<pPage->nCell );
8472 rc = sqlite3PagerWrite(pPage->pDbPage);
8473 if( rc ){
8474 goto end_insert;
8476 oldCell = findCell(pPage, idx);
8477 if( !pPage->leaf ){
8478 memcpy(newCell, oldCell, 4);
8480 rc = clearCell(pPage, oldCell, &info);
8481 if( info.nSize==szNew && info.nLocal==info.nPayload
8482 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
8484 /* Overwrite the old cell with the new if they are the same size.
8485 ** We could also try to do this if the old cell is smaller, then add
8486 ** the leftover space to the free list. But experiments show that
8487 ** doing that is no faster then skipping this optimization and just
8488 ** calling dropCell() and insertCell().
8490 ** This optimization cannot be used on an autovacuum database if the
8491 ** new entry uses overflow pages, as the insertCell() call below is
8492 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
8493 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
8494 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
8495 memcpy(oldCell, newCell, szNew);
8496 return SQLITE_OK;
8498 dropCell(pPage, idx, info.nSize, &rc);
8499 if( rc ) goto end_insert;
8500 }else if( loc<0 && pPage->nCell>0 ){
8501 assert( pPage->leaf );
8502 idx = ++pCur->ix;
8503 pCur->curFlags &= ~BTCF_ValidNKey;
8504 }else{
8505 assert( pPage->leaf );
8507 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
8508 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
8509 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
8511 /* If no error has occurred and pPage has an overflow cell, call balance()
8512 ** to redistribute the cells within the tree. Since balance() may move
8513 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
8514 ** variables.
8516 ** Previous versions of SQLite called moveToRoot() to move the cursor
8517 ** back to the root page as balance() used to invalidate the contents
8518 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
8519 ** set the cursor state to "invalid". This makes common insert operations
8520 ** slightly faster.
8522 ** There is a subtle but important optimization here too. When inserting
8523 ** multiple records into an intkey b-tree using a single cursor (as can
8524 ** happen while processing an "INSERT INTO ... SELECT" statement), it
8525 ** is advantageous to leave the cursor pointing to the last entry in
8526 ** the b-tree if possible. If the cursor is left pointing to the last
8527 ** entry in the table, and the next row inserted has an integer key
8528 ** larger than the largest existing key, it is possible to insert the
8529 ** row without seeking the cursor. This can be a big performance boost.
8531 pCur->info.nSize = 0;
8532 if( pPage->nOverflow ){
8533 assert( rc==SQLITE_OK );
8534 pCur->curFlags &= ~(BTCF_ValidNKey);
8535 rc = balance(pCur);
8537 /* Must make sure nOverflow is reset to zero even if the balance()
8538 ** fails. Internal data structure corruption will result otherwise.
8539 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
8540 ** from trying to save the current position of the cursor. */
8541 pCur->pPage->nOverflow = 0;
8542 pCur->eState = CURSOR_INVALID;
8543 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
8544 btreeReleaseAllCursorPages(pCur);
8545 if( pCur->pKeyInfo ){
8546 assert( pCur->pKey==0 );
8547 pCur->pKey = sqlite3Malloc( pX->nKey );
8548 if( pCur->pKey==0 ){
8549 rc = SQLITE_NOMEM;
8550 }else{
8551 memcpy(pCur->pKey, pX->pKey, pX->nKey);
8554 pCur->eState = CURSOR_REQUIRESEEK;
8555 pCur->nKey = pX->nKey;
8558 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
8560 end_insert:
8561 return rc;
8565 ** Delete the entry that the cursor is pointing to.
8567 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
8568 ** the cursor is left pointing at an arbitrary location after the delete.
8569 ** But if that bit is set, then the cursor is left in a state such that
8570 ** the next call to BtreeNext() or BtreePrev() moves it to the same row
8571 ** as it would have been on if the call to BtreeDelete() had been omitted.
8573 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
8574 ** associated with a single table entry and its indexes. Only one of those
8575 ** deletes is considered the "primary" delete. The primary delete occurs
8576 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
8577 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
8578 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
8579 ** but which might be used by alternative storage engines.
8581 int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
8582 Btree *p = pCur->pBtree;
8583 BtShared *pBt = p->pBt;
8584 int rc; /* Return code */
8585 MemPage *pPage; /* Page to delete cell from */
8586 unsigned char *pCell; /* Pointer to cell to delete */
8587 int iCellIdx; /* Index of cell to delete */
8588 int iCellDepth; /* Depth of node containing pCell */
8589 CellInfo info; /* Size of the cell being deleted */
8590 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
8591 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
8593 assert( cursorOwnsBtShared(pCur) );
8594 assert( pBt->inTransaction==TRANS_WRITE );
8595 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8596 assert( pCur->curFlags & BTCF_WriteFlag );
8597 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
8598 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
8599 assert( pCur->ix<pCur->pPage->nCell );
8600 assert( pCur->eState==CURSOR_VALID );
8601 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
8603 iCellDepth = pCur->iPage;
8604 iCellIdx = pCur->ix;
8605 pPage = pCur->pPage;
8606 pCell = findCell(pPage, iCellIdx);
8608 /* If the bPreserve flag is set to true, then the cursor position must
8609 ** be preserved following this delete operation. If the current delete
8610 ** will cause a b-tree rebalance, then this is done by saving the cursor
8611 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
8612 ** returning.
8614 ** Or, if the current delete will not cause a rebalance, then the cursor
8615 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
8616 ** before or after the deleted entry. In this case set bSkipnext to true. */
8617 if( bPreserve ){
8618 if( !pPage->leaf
8619 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
8621 /* A b-tree rebalance will be required after deleting this entry.
8622 ** Save the cursor key. */
8623 rc = saveCursorKey(pCur);
8624 if( rc ) return rc;
8625 }else{
8626 bSkipnext = 1;
8630 /* If the page containing the entry to delete is not a leaf page, move
8631 ** the cursor to the largest entry in the tree that is smaller than
8632 ** the entry being deleted. This cell will replace the cell being deleted
8633 ** from the internal node. The 'previous' entry is used for this instead
8634 ** of the 'next' entry, as the previous entry is always a part of the
8635 ** sub-tree headed by the child page of the cell being deleted. This makes
8636 ** balancing the tree following the delete operation easier. */
8637 if( !pPage->leaf ){
8638 rc = sqlite3BtreePrevious(pCur, 0);
8639 assert( rc!=SQLITE_DONE );
8640 if( rc ) return rc;
8643 /* Save the positions of any other cursors open on this table before
8644 ** making any modifications. */
8645 if( pCur->curFlags & BTCF_Multiple ){
8646 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8647 if( rc ) return rc;
8650 /* If this is a delete operation to remove a row from a table b-tree,
8651 ** invalidate any incrblob cursors open on the row being deleted. */
8652 if( pCur->pKeyInfo==0 ){
8653 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
8656 /* Make the page containing the entry to be deleted writable. Then free any
8657 ** overflow pages associated with the entry and finally remove the cell
8658 ** itself from within the page. */
8659 rc = sqlite3PagerWrite(pPage->pDbPage);
8660 if( rc ) return rc;
8661 rc = clearCell(pPage, pCell, &info);
8662 dropCell(pPage, iCellIdx, info.nSize, &rc);
8663 if( rc ) return rc;
8665 /* If the cell deleted was not located on a leaf page, then the cursor
8666 ** is currently pointing to the largest entry in the sub-tree headed
8667 ** by the child-page of the cell that was just deleted from an internal
8668 ** node. The cell from the leaf node needs to be moved to the internal
8669 ** node to replace the deleted cell. */
8670 if( !pPage->leaf ){
8671 MemPage *pLeaf = pCur->pPage;
8672 int nCell;
8673 Pgno n;
8674 unsigned char *pTmp;
8676 if( iCellDepth<pCur->iPage-1 ){
8677 n = pCur->apPage[iCellDepth+1]->pgno;
8678 }else{
8679 n = pCur->pPage->pgno;
8681 pCell = findCell(pLeaf, pLeaf->nCell-1);
8682 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
8683 nCell = pLeaf->xCellSize(pLeaf, pCell);
8684 assert( MX_CELL_SIZE(pBt) >= nCell );
8685 pTmp = pBt->pTmpSpace;
8686 assert( pTmp!=0 );
8687 rc = sqlite3PagerWrite(pLeaf->pDbPage);
8688 if( rc==SQLITE_OK ){
8689 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
8691 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
8692 if( rc ) return rc;
8695 /* Balance the tree. If the entry deleted was located on a leaf page,
8696 ** then the cursor still points to that page. In this case the first
8697 ** call to balance() repairs the tree, and the if(...) condition is
8698 ** never true.
8700 ** Otherwise, if the entry deleted was on an internal node page, then
8701 ** pCur is pointing to the leaf page from which a cell was removed to
8702 ** replace the cell deleted from the internal node. This is slightly
8703 ** tricky as the leaf node may be underfull, and the internal node may
8704 ** be either under or overfull. In this case run the balancing algorithm
8705 ** on the leaf node first. If the balance proceeds far enough up the
8706 ** tree that we can be sure that any problem in the internal node has
8707 ** been corrected, so be it. Otherwise, after balancing the leaf node,
8708 ** walk the cursor up the tree to the internal node and balance it as
8709 ** well. */
8710 rc = balance(pCur);
8711 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
8712 releasePageNotNull(pCur->pPage);
8713 pCur->iPage--;
8714 while( pCur->iPage>iCellDepth ){
8715 releasePage(pCur->apPage[pCur->iPage--]);
8717 pCur->pPage = pCur->apPage[pCur->iPage];
8718 rc = balance(pCur);
8721 if( rc==SQLITE_OK ){
8722 if( bSkipnext ){
8723 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
8724 assert( pPage==pCur->pPage || CORRUPT_DB );
8725 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
8726 pCur->eState = CURSOR_SKIPNEXT;
8727 if( iCellIdx>=pPage->nCell ){
8728 pCur->skipNext = -1;
8729 pCur->ix = pPage->nCell-1;
8730 }else{
8731 pCur->skipNext = 1;
8733 }else{
8734 rc = moveToRoot(pCur);
8735 if( bPreserve ){
8736 btreeReleaseAllCursorPages(pCur);
8737 pCur->eState = CURSOR_REQUIRESEEK;
8739 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
8742 return rc;
8746 ** Create a new BTree table. Write into *piTable the page
8747 ** number for the root page of the new table.
8749 ** The type of type is determined by the flags parameter. Only the
8750 ** following values of flags are currently in use. Other values for
8751 ** flags might not work:
8753 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
8754 ** BTREE_ZERODATA Used for SQL indices
8756 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
8757 BtShared *pBt = p->pBt;
8758 MemPage *pRoot;
8759 Pgno pgnoRoot;
8760 int rc;
8761 int ptfFlags; /* Page-type flage for the root page of new table */
8763 assert( sqlite3BtreeHoldsMutex(p) );
8764 assert( pBt->inTransaction==TRANS_WRITE );
8765 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8767 #ifdef SQLITE_OMIT_AUTOVACUUM
8768 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8769 if( rc ){
8770 return rc;
8772 #else
8773 if( pBt->autoVacuum ){
8774 Pgno pgnoMove; /* Move a page here to make room for the root-page */
8775 MemPage *pPageMove; /* The page to move to. */
8777 /* Creating a new table may probably require moving an existing database
8778 ** to make room for the new tables root page. In case this page turns
8779 ** out to be an overflow page, delete all overflow page-map caches
8780 ** held by open cursors.
8782 invalidateAllOverflowCache(pBt);
8784 /* Read the value of meta[3] from the database to determine where the
8785 ** root page of the new table should go. meta[3] is the largest root-page
8786 ** created so far, so the new root-page is (meta[3]+1).
8788 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
8789 pgnoRoot++;
8791 /* The new root-page may not be allocated on a pointer-map page, or the
8792 ** PENDING_BYTE page.
8794 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
8795 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
8796 pgnoRoot++;
8798 assert( pgnoRoot>=3 || CORRUPT_DB );
8799 testcase( pgnoRoot<3 );
8801 /* Allocate a page. The page that currently resides at pgnoRoot will
8802 ** be moved to the allocated page (unless the allocated page happens
8803 ** to reside at pgnoRoot).
8805 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
8806 if( rc!=SQLITE_OK ){
8807 return rc;
8810 if( pgnoMove!=pgnoRoot ){
8811 /* pgnoRoot is the page that will be used for the root-page of
8812 ** the new table (assuming an error did not occur). But we were
8813 ** allocated pgnoMove. If required (i.e. if it was not allocated
8814 ** by extending the file), the current page at position pgnoMove
8815 ** is already journaled.
8817 u8 eType = 0;
8818 Pgno iPtrPage = 0;
8820 /* Save the positions of any open cursors. This is required in
8821 ** case they are holding a reference to an xFetch reference
8822 ** corresponding to page pgnoRoot. */
8823 rc = saveAllCursors(pBt, 0, 0);
8824 releasePage(pPageMove);
8825 if( rc!=SQLITE_OK ){
8826 return rc;
8829 /* Move the page currently at pgnoRoot to pgnoMove. */
8830 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8831 if( rc!=SQLITE_OK ){
8832 return rc;
8834 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
8835 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
8836 rc = SQLITE_CORRUPT_BKPT;
8838 if( rc!=SQLITE_OK ){
8839 releasePage(pRoot);
8840 return rc;
8842 assert( eType!=PTRMAP_ROOTPAGE );
8843 assert( eType!=PTRMAP_FREEPAGE );
8844 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
8845 releasePage(pRoot);
8847 /* Obtain the page at pgnoRoot */
8848 if( rc!=SQLITE_OK ){
8849 return rc;
8851 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8852 if( rc!=SQLITE_OK ){
8853 return rc;
8855 rc = sqlite3PagerWrite(pRoot->pDbPage);
8856 if( rc!=SQLITE_OK ){
8857 releasePage(pRoot);
8858 return rc;
8860 }else{
8861 pRoot = pPageMove;
8864 /* Update the pointer-map and meta-data with the new root-page number. */
8865 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
8866 if( rc ){
8867 releasePage(pRoot);
8868 return rc;
8871 /* When the new root page was allocated, page 1 was made writable in
8872 ** order either to increase the database filesize, or to decrement the
8873 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
8875 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
8876 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
8877 if( NEVER(rc) ){
8878 releasePage(pRoot);
8879 return rc;
8882 }else{
8883 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8884 if( rc ) return rc;
8886 #endif
8887 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8888 if( createTabFlags & BTREE_INTKEY ){
8889 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
8890 }else{
8891 ptfFlags = PTF_ZERODATA | PTF_LEAF;
8893 zeroPage(pRoot, ptfFlags);
8894 sqlite3PagerUnref(pRoot->pDbPage);
8895 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
8896 *piTable = (int)pgnoRoot;
8897 return SQLITE_OK;
8899 int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
8900 int rc;
8901 sqlite3BtreeEnter(p);
8902 rc = btreeCreateTable(p, piTable, flags);
8903 sqlite3BtreeLeave(p);
8904 return rc;
8908 ** Erase the given database page and all its children. Return
8909 ** the page to the freelist.
8911 static int clearDatabasePage(
8912 BtShared *pBt, /* The BTree that contains the table */
8913 Pgno pgno, /* Page number to clear */
8914 int freePageFlag, /* Deallocate page if true */
8915 int *pnChange /* Add number of Cells freed to this counter */
8917 MemPage *pPage;
8918 int rc;
8919 unsigned char *pCell;
8920 int i;
8921 int hdr;
8922 CellInfo info;
8924 assert( sqlite3_mutex_held(pBt->mutex) );
8925 if( pgno>btreePagecount(pBt) ){
8926 return SQLITE_CORRUPT_BKPT;
8928 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
8929 if( rc ) return rc;
8930 if( pPage->bBusy ){
8931 rc = SQLITE_CORRUPT_BKPT;
8932 goto cleardatabasepage_out;
8934 pPage->bBusy = 1;
8935 hdr = pPage->hdrOffset;
8936 for(i=0; i<pPage->nCell; i++){
8937 pCell = findCell(pPage, i);
8938 if( !pPage->leaf ){
8939 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
8940 if( rc ) goto cleardatabasepage_out;
8942 rc = clearCell(pPage, pCell, &info);
8943 if( rc ) goto cleardatabasepage_out;
8945 if( !pPage->leaf ){
8946 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
8947 if( rc ) goto cleardatabasepage_out;
8948 }else if( pnChange ){
8949 assert( pPage->intKey || CORRUPT_DB );
8950 testcase( !pPage->intKey );
8951 *pnChange += pPage->nCell;
8953 if( freePageFlag ){
8954 freePage(pPage, &rc);
8955 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
8956 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
8959 cleardatabasepage_out:
8960 pPage->bBusy = 0;
8961 releasePage(pPage);
8962 return rc;
8966 ** Delete all information from a single table in the database. iTable is
8967 ** the page number of the root of the table. After this routine returns,
8968 ** the root page is empty, but still exists.
8970 ** This routine will fail with SQLITE_LOCKED if there are any open
8971 ** read cursors on the table. Open write cursors are moved to the
8972 ** root of the table.
8974 ** If pnChange is not NULL, then table iTable must be an intkey table. The
8975 ** integer value pointed to by pnChange is incremented by the number of
8976 ** entries in the table.
8978 int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
8979 int rc;
8980 BtShared *pBt = p->pBt;
8981 sqlite3BtreeEnter(p);
8982 assert( p->inTrans==TRANS_WRITE );
8984 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
8986 if( SQLITE_OK==rc ){
8987 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
8988 ** is the root of a table b-tree - if it is not, the following call is
8989 ** a no-op). */
8990 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
8991 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
8993 sqlite3BtreeLeave(p);
8994 return rc;
8998 ** Delete all information from the single table that pCur is open on.
9000 ** This routine only work for pCur on an ephemeral table.
9002 int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
9003 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
9007 ** Erase all information in a table and add the root of the table to
9008 ** the freelist. Except, the root of the principle table (the one on
9009 ** page 1) is never added to the freelist.
9011 ** This routine will fail with SQLITE_LOCKED if there are any open
9012 ** cursors on the table.
9014 ** If AUTOVACUUM is enabled and the page at iTable is not the last
9015 ** root page in the database file, then the last root page
9016 ** in the database file is moved into the slot formerly occupied by
9017 ** iTable and that last slot formerly occupied by the last root page
9018 ** is added to the freelist instead of iTable. In this say, all
9019 ** root pages are kept at the beginning of the database file, which
9020 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
9021 ** page number that used to be the last root page in the file before
9022 ** the move. If no page gets moved, *piMoved is set to 0.
9023 ** The last root page is recorded in meta[3] and the value of
9024 ** meta[3] is updated by this procedure.
9026 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
9027 int rc;
9028 MemPage *pPage = 0;
9029 BtShared *pBt = p->pBt;
9031 assert( sqlite3BtreeHoldsMutex(p) );
9032 assert( p->inTrans==TRANS_WRITE );
9033 assert( iTable>=2 );
9035 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
9036 if( rc ) return rc;
9037 rc = sqlite3BtreeClearTable(p, iTable, 0);
9038 if( rc ){
9039 releasePage(pPage);
9040 return rc;
9043 *piMoved = 0;
9045 #ifdef SQLITE_OMIT_AUTOVACUUM
9046 freePage(pPage, &rc);
9047 releasePage(pPage);
9048 #else
9049 if( pBt->autoVacuum ){
9050 Pgno maxRootPgno;
9051 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
9053 if( iTable==maxRootPgno ){
9054 /* If the table being dropped is the table with the largest root-page
9055 ** number in the database, put the root page on the free list.
9057 freePage(pPage, &rc);
9058 releasePage(pPage);
9059 if( rc!=SQLITE_OK ){
9060 return rc;
9062 }else{
9063 /* The table being dropped does not have the largest root-page
9064 ** number in the database. So move the page that does into the
9065 ** gap left by the deleted root-page.
9067 MemPage *pMove;
9068 releasePage(pPage);
9069 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
9070 if( rc!=SQLITE_OK ){
9071 return rc;
9073 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
9074 releasePage(pMove);
9075 if( rc!=SQLITE_OK ){
9076 return rc;
9078 pMove = 0;
9079 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
9080 freePage(pMove, &rc);
9081 releasePage(pMove);
9082 if( rc!=SQLITE_OK ){
9083 return rc;
9085 *piMoved = maxRootPgno;
9088 /* Set the new 'max-root-page' value in the database header. This
9089 ** is the old value less one, less one more if that happens to
9090 ** be a root-page number, less one again if that is the
9091 ** PENDING_BYTE_PAGE.
9093 maxRootPgno--;
9094 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
9095 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
9096 maxRootPgno--;
9098 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
9100 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
9101 }else{
9102 freePage(pPage, &rc);
9103 releasePage(pPage);
9105 #endif
9106 return rc;
9108 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
9109 int rc;
9110 sqlite3BtreeEnter(p);
9111 rc = btreeDropTable(p, iTable, piMoved);
9112 sqlite3BtreeLeave(p);
9113 return rc;
9118 ** This function may only be called if the b-tree connection already
9119 ** has a read or write transaction open on the database.
9121 ** Read the meta-information out of a database file. Meta[0]
9122 ** is the number of free pages currently in the database. Meta[1]
9123 ** through meta[15] are available for use by higher layers. Meta[0]
9124 ** is read-only, the others are read/write.
9126 ** The schema layer numbers meta values differently. At the schema
9127 ** layer (and the SetCookie and ReadCookie opcodes) the number of
9128 ** free pages is not visible. So Cookie[0] is the same as Meta[1].
9130 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
9131 ** of reading the value out of the header, it instead loads the "DataVersion"
9132 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
9133 ** database file. It is a number computed by the pager. But its access
9134 ** pattern is the same as header meta values, and so it is convenient to
9135 ** read it from this routine.
9137 void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
9138 BtShared *pBt = p->pBt;
9140 sqlite3BtreeEnter(p);
9141 assert( p->inTrans>TRANS_NONE );
9142 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
9143 assert( pBt->pPage1 );
9144 assert( idx>=0 && idx<=15 );
9146 if( idx==BTREE_DATA_VERSION ){
9147 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
9148 }else{
9149 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
9152 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
9153 ** database, mark the database as read-only. */
9154 #ifdef SQLITE_OMIT_AUTOVACUUM
9155 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
9156 pBt->btsFlags |= BTS_READ_ONLY;
9158 #endif
9160 sqlite3BtreeLeave(p);
9164 ** Write meta-information back into the database. Meta[0] is
9165 ** read-only and may not be written.
9167 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
9168 BtShared *pBt = p->pBt;
9169 unsigned char *pP1;
9170 int rc;
9171 assert( idx>=1 && idx<=15 );
9172 sqlite3BtreeEnter(p);
9173 assert( p->inTrans==TRANS_WRITE );
9174 assert( pBt->pPage1!=0 );
9175 pP1 = pBt->pPage1->aData;
9176 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
9177 if( rc==SQLITE_OK ){
9178 put4byte(&pP1[36 + idx*4], iMeta);
9179 #ifndef SQLITE_OMIT_AUTOVACUUM
9180 if( idx==BTREE_INCR_VACUUM ){
9181 assert( pBt->autoVacuum || iMeta==0 );
9182 assert( iMeta==0 || iMeta==1 );
9183 pBt->incrVacuum = (u8)iMeta;
9185 #endif
9187 sqlite3BtreeLeave(p);
9188 return rc;
9191 #ifndef SQLITE_OMIT_BTREECOUNT
9193 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
9194 ** number of entries in the b-tree and write the result to *pnEntry.
9196 ** SQLITE_OK is returned if the operation is successfully executed.
9197 ** Otherwise, if an error is encountered (i.e. an IO error or database
9198 ** corruption) an SQLite error code is returned.
9200 int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
9201 i64 nEntry = 0; /* Value to return in *pnEntry */
9202 int rc; /* Return code */
9204 rc = moveToRoot(pCur);
9205 if( rc==SQLITE_EMPTY ){
9206 *pnEntry = 0;
9207 return SQLITE_OK;
9210 /* Unless an error occurs, the following loop runs one iteration for each
9211 ** page in the B-Tree structure (not including overflow pages).
9213 while( rc==SQLITE_OK ){
9214 int iIdx; /* Index of child node in parent */
9215 MemPage *pPage; /* Current page of the b-tree */
9217 /* If this is a leaf page or the tree is not an int-key tree, then
9218 ** this page contains countable entries. Increment the entry counter
9219 ** accordingly.
9221 pPage = pCur->pPage;
9222 if( pPage->leaf || !pPage->intKey ){
9223 nEntry += pPage->nCell;
9226 /* pPage is a leaf node. This loop navigates the cursor so that it
9227 ** points to the first interior cell that it points to the parent of
9228 ** the next page in the tree that has not yet been visited. The
9229 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
9230 ** of the page, or to the number of cells in the page if the next page
9231 ** to visit is the right-child of its parent.
9233 ** If all pages in the tree have been visited, return SQLITE_OK to the
9234 ** caller.
9236 if( pPage->leaf ){
9237 do {
9238 if( pCur->iPage==0 ){
9239 /* All pages of the b-tree have been visited. Return successfully. */
9240 *pnEntry = nEntry;
9241 return moveToRoot(pCur);
9243 moveToParent(pCur);
9244 }while ( pCur->ix>=pCur->pPage->nCell );
9246 pCur->ix++;
9247 pPage = pCur->pPage;
9250 /* Descend to the child node of the cell that the cursor currently
9251 ** points at. This is the right-child if (iIdx==pPage->nCell).
9253 iIdx = pCur->ix;
9254 if( iIdx==pPage->nCell ){
9255 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
9256 }else{
9257 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
9261 /* An error has occurred. Return an error code. */
9262 return rc;
9264 #endif
9267 ** Return the pager associated with a BTree. This routine is used for
9268 ** testing and debugging only.
9270 Pager *sqlite3BtreePager(Btree *p){
9271 return p->pBt->pPager;
9274 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9276 ** Append a message to the error message string.
9278 static void checkAppendMsg(
9279 IntegrityCk *pCheck,
9280 const char *zFormat,
9283 va_list ap;
9284 if( !pCheck->mxErr ) return;
9285 pCheck->mxErr--;
9286 pCheck->nErr++;
9287 va_start(ap, zFormat);
9288 if( pCheck->errMsg.nChar ){
9289 sqlite3_str_append(&pCheck->errMsg, "\n", 1);
9291 if( pCheck->zPfx ){
9292 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
9294 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
9295 va_end(ap);
9296 if( pCheck->errMsg.accError==SQLITE_NOMEM ){
9297 pCheck->mallocFailed = 1;
9300 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9302 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9305 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
9306 ** corresponds to page iPg is already set.
9308 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
9309 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
9310 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
9314 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
9316 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
9317 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
9318 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
9323 ** Add 1 to the reference count for page iPage. If this is the second
9324 ** reference to the page, add an error message to pCheck->zErrMsg.
9325 ** Return 1 if there are 2 or more references to the page and 0 if
9326 ** if this is the first reference to the page.
9328 ** Also check that the page number is in bounds.
9330 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
9331 if( iPage==0 ) return 1;
9332 if( iPage>pCheck->nPage ){
9333 checkAppendMsg(pCheck, "invalid page number %d", iPage);
9334 return 1;
9336 if( getPageReferenced(pCheck, iPage) ){
9337 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
9338 return 1;
9340 setPageReferenced(pCheck, iPage);
9341 return 0;
9344 #ifndef SQLITE_OMIT_AUTOVACUUM
9346 ** Check that the entry in the pointer-map for page iChild maps to
9347 ** page iParent, pointer type ptrType. If not, append an error message
9348 ** to pCheck.
9350 static void checkPtrmap(
9351 IntegrityCk *pCheck, /* Integrity check context */
9352 Pgno iChild, /* Child page number */
9353 u8 eType, /* Expected pointer map type */
9354 Pgno iParent /* Expected pointer map parent page number */
9356 int rc;
9357 u8 ePtrmapType;
9358 Pgno iPtrmapParent;
9360 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
9361 if( rc!=SQLITE_OK ){
9362 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
9363 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
9364 return;
9367 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
9368 checkAppendMsg(pCheck,
9369 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
9370 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
9373 #endif
9376 ** Check the integrity of the freelist or of an overflow page list.
9377 ** Verify that the number of pages on the list is N.
9379 static void checkList(
9380 IntegrityCk *pCheck, /* Integrity checking context */
9381 int isFreeList, /* True for a freelist. False for overflow page list */
9382 int iPage, /* Page number for first page in the list */
9383 int N /* Expected number of pages in the list */
9385 int i;
9386 int expected = N;
9387 int iFirst = iPage;
9388 while( N-- > 0 && pCheck->mxErr ){
9389 DbPage *pOvflPage;
9390 unsigned char *pOvflData;
9391 if( iPage<1 ){
9392 checkAppendMsg(pCheck,
9393 "%d of %d pages missing from overflow list starting at %d",
9394 N+1, expected, iFirst);
9395 break;
9397 if( checkRef(pCheck, iPage) ) break;
9398 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
9399 checkAppendMsg(pCheck, "failed to get page %d", iPage);
9400 break;
9402 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
9403 if( isFreeList ){
9404 int n = get4byte(&pOvflData[4]);
9405 #ifndef SQLITE_OMIT_AUTOVACUUM
9406 if( pCheck->pBt->autoVacuum ){
9407 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
9409 #endif
9410 if( n>(int)pCheck->pBt->usableSize/4-2 ){
9411 checkAppendMsg(pCheck,
9412 "freelist leaf count too big on page %d", iPage);
9413 N--;
9414 }else{
9415 for(i=0; i<n; i++){
9416 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
9417 #ifndef SQLITE_OMIT_AUTOVACUUM
9418 if( pCheck->pBt->autoVacuum ){
9419 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
9421 #endif
9422 checkRef(pCheck, iFreePage);
9424 N -= n;
9427 #ifndef SQLITE_OMIT_AUTOVACUUM
9428 else{
9429 /* If this database supports auto-vacuum and iPage is not the last
9430 ** page in this overflow list, check that the pointer-map entry for
9431 ** the following page matches iPage.
9433 if( pCheck->pBt->autoVacuum && N>0 ){
9434 i = get4byte(pOvflData);
9435 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
9438 #endif
9439 iPage = get4byte(pOvflData);
9440 sqlite3PagerUnref(pOvflPage);
9442 if( isFreeList && N<(iPage!=0) ){
9443 checkAppendMsg(pCheck, "free-page count in header is too small");
9447 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9450 ** An implementation of a min-heap.
9452 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the
9453 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
9454 ** and aHeap[N*2+1].
9456 ** The heap property is this: Every node is less than or equal to both
9457 ** of its daughter nodes. A consequence of the heap property is that the
9458 ** root node aHeap[1] is always the minimum value currently in the heap.
9460 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
9461 ** the heap, preserving the heap property. The btreeHeapPull() routine
9462 ** removes the root element from the heap (the minimum value in the heap)
9463 ** and then moves other nodes around as necessary to preserve the heap
9464 ** property.
9466 ** This heap is used for cell overlap and coverage testing. Each u32
9467 ** entry represents the span of a cell or freeblock on a btree page.
9468 ** The upper 16 bits are the index of the first byte of a range and the
9469 ** lower 16 bits are the index of the last byte of that range.
9471 static void btreeHeapInsert(u32 *aHeap, u32 x){
9472 u32 j, i = ++aHeap[0];
9473 aHeap[i] = x;
9474 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
9475 x = aHeap[j];
9476 aHeap[j] = aHeap[i];
9477 aHeap[i] = x;
9478 i = j;
9481 static int btreeHeapPull(u32 *aHeap, u32 *pOut){
9482 u32 j, i, x;
9483 if( (x = aHeap[0])==0 ) return 0;
9484 *pOut = aHeap[1];
9485 aHeap[1] = aHeap[x];
9486 aHeap[x] = 0xffffffff;
9487 aHeap[0]--;
9488 i = 1;
9489 while( (j = i*2)<=aHeap[0] ){
9490 if( aHeap[j]>aHeap[j+1] ) j++;
9491 if( aHeap[i]<aHeap[j] ) break;
9492 x = aHeap[i];
9493 aHeap[i] = aHeap[j];
9494 aHeap[j] = x;
9495 i = j;
9497 return 1;
9500 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9502 ** Do various sanity checks on a single page of a tree. Return
9503 ** the tree depth. Root pages return 0. Parents of root pages
9504 ** return 1, and so forth.
9506 ** These checks are done:
9508 ** 1. Make sure that cells and freeblocks do not overlap
9509 ** but combine to completely cover the page.
9510 ** 2. Make sure integer cell keys are in order.
9511 ** 3. Check the integrity of overflow pages.
9512 ** 4. Recursively call checkTreePage on all children.
9513 ** 5. Verify that the depth of all children is the same.
9515 static int checkTreePage(
9516 IntegrityCk *pCheck, /* Context for the sanity check */
9517 int iPage, /* Page number of the page to check */
9518 i64 *piMinKey, /* Write minimum integer primary key here */
9519 i64 maxKey /* Error if integer primary key greater than this */
9521 MemPage *pPage = 0; /* The page being analyzed */
9522 int i; /* Loop counter */
9523 int rc; /* Result code from subroutine call */
9524 int depth = -1, d2; /* Depth of a subtree */
9525 int pgno; /* Page number */
9526 int nFrag; /* Number of fragmented bytes on the page */
9527 int hdr; /* Offset to the page header */
9528 int cellStart; /* Offset to the start of the cell pointer array */
9529 int nCell; /* Number of cells */
9530 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
9531 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
9532 ** False if IPK must be strictly less than maxKey */
9533 u8 *data; /* Page content */
9534 u8 *pCell; /* Cell content */
9535 u8 *pCellIdx; /* Next element of the cell pointer array */
9536 BtShared *pBt; /* The BtShared object that owns pPage */
9537 u32 pc; /* Address of a cell */
9538 u32 usableSize; /* Usable size of the page */
9539 u32 contentOffset; /* Offset to the start of the cell content area */
9540 u32 *heap = 0; /* Min-heap used for checking cell coverage */
9541 u32 x, prev = 0; /* Next and previous entry on the min-heap */
9542 const char *saved_zPfx = pCheck->zPfx;
9543 int saved_v1 = pCheck->v1;
9544 int saved_v2 = pCheck->v2;
9545 u8 savedIsInit = 0;
9547 /* Check that the page exists
9549 pBt = pCheck->pBt;
9550 usableSize = pBt->usableSize;
9551 if( iPage==0 ) return 0;
9552 if( checkRef(pCheck, iPage) ) return 0;
9553 pCheck->zPfx = "Page %d: ";
9554 pCheck->v1 = iPage;
9555 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
9556 checkAppendMsg(pCheck,
9557 "unable to get the page. error code=%d", rc);
9558 goto end_of_check;
9561 /* Clear MemPage.isInit to make sure the corruption detection code in
9562 ** btreeInitPage() is executed. */
9563 savedIsInit = pPage->isInit;
9564 pPage->isInit = 0;
9565 if( (rc = btreeInitPage(pPage))!=0 ){
9566 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
9567 checkAppendMsg(pCheck,
9568 "btreeInitPage() returns error code %d", rc);
9569 goto end_of_check;
9571 data = pPage->aData;
9572 hdr = pPage->hdrOffset;
9574 /* Set up for cell analysis */
9575 pCheck->zPfx = "On tree page %d cell %d: ";
9576 contentOffset = get2byteNotZero(&data[hdr+5]);
9577 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
9579 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
9580 ** number of cells on the page. */
9581 nCell = get2byte(&data[hdr+3]);
9582 assert( pPage->nCell==nCell );
9584 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
9585 ** immediately follows the b-tree page header. */
9586 cellStart = hdr + 12 - 4*pPage->leaf;
9587 assert( pPage->aCellIdx==&data[cellStart] );
9588 pCellIdx = &data[cellStart + 2*(nCell-1)];
9590 if( !pPage->leaf ){
9591 /* Analyze the right-child page of internal pages */
9592 pgno = get4byte(&data[hdr+8]);
9593 #ifndef SQLITE_OMIT_AUTOVACUUM
9594 if( pBt->autoVacuum ){
9595 pCheck->zPfx = "On page %d at right child: ";
9596 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9598 #endif
9599 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9600 keyCanBeEqual = 0;
9601 }else{
9602 /* For leaf pages, the coverage check will occur in the same loop
9603 ** as the other cell checks, so initialize the heap. */
9604 heap = pCheck->heap;
9605 heap[0] = 0;
9608 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
9609 ** integer offsets to the cell contents. */
9610 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
9611 CellInfo info;
9613 /* Check cell size */
9614 pCheck->v2 = i;
9615 assert( pCellIdx==&data[cellStart + i*2] );
9616 pc = get2byteAligned(pCellIdx);
9617 pCellIdx -= 2;
9618 if( pc<contentOffset || pc>usableSize-4 ){
9619 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
9620 pc, contentOffset, usableSize-4);
9621 doCoverageCheck = 0;
9622 continue;
9624 pCell = &data[pc];
9625 pPage->xParseCell(pPage, pCell, &info);
9626 if( pc+info.nSize>usableSize ){
9627 checkAppendMsg(pCheck, "Extends off end of page");
9628 doCoverageCheck = 0;
9629 continue;
9632 /* Check for integer primary key out of range */
9633 if( pPage->intKey ){
9634 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
9635 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
9637 maxKey = info.nKey;
9638 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
9641 /* Check the content overflow list */
9642 if( info.nPayload>info.nLocal ){
9643 int nPage; /* Number of pages on the overflow chain */
9644 Pgno pgnoOvfl; /* First page of the overflow chain */
9645 assert( pc + info.nSize - 4 <= usableSize );
9646 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
9647 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
9648 #ifndef SQLITE_OMIT_AUTOVACUUM
9649 if( pBt->autoVacuum ){
9650 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
9652 #endif
9653 checkList(pCheck, 0, pgnoOvfl, nPage);
9656 if( !pPage->leaf ){
9657 /* Check sanity of left child page for internal pages */
9658 pgno = get4byte(pCell);
9659 #ifndef SQLITE_OMIT_AUTOVACUUM
9660 if( pBt->autoVacuum ){
9661 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9663 #endif
9664 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9665 keyCanBeEqual = 0;
9666 if( d2!=depth ){
9667 checkAppendMsg(pCheck, "Child page depth differs");
9668 depth = d2;
9670 }else{
9671 /* Populate the coverage-checking heap for leaf pages */
9672 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
9675 *piMinKey = maxKey;
9677 /* Check for complete coverage of the page
9679 pCheck->zPfx = 0;
9680 if( doCoverageCheck && pCheck->mxErr>0 ){
9681 /* For leaf pages, the min-heap has already been initialized and the
9682 ** cells have already been inserted. But for internal pages, that has
9683 ** not yet been done, so do it now */
9684 if( !pPage->leaf ){
9685 heap = pCheck->heap;
9686 heap[0] = 0;
9687 for(i=nCell-1; i>=0; i--){
9688 u32 size;
9689 pc = get2byteAligned(&data[cellStart+i*2]);
9690 size = pPage->xCellSize(pPage, &data[pc]);
9691 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
9694 /* Add the freeblocks to the min-heap
9696 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
9697 ** is the offset of the first freeblock, or zero if there are no
9698 ** freeblocks on the page.
9700 i = get2byte(&data[hdr+1]);
9701 while( i>0 ){
9702 int size, j;
9703 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
9704 size = get2byte(&data[i+2]);
9705 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
9706 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
9707 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
9708 ** big-endian integer which is the offset in the b-tree page of the next
9709 ** freeblock in the chain, or zero if the freeblock is the last on the
9710 ** chain. */
9711 j = get2byte(&data[i]);
9712 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
9713 ** increasing offset. */
9714 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
9715 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
9716 i = j;
9718 /* Analyze the min-heap looking for overlap between cells and/or
9719 ** freeblocks, and counting the number of untracked bytes in nFrag.
9721 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
9722 ** There is an implied first entry the covers the page header, the cell
9723 ** pointer index, and the gap between the cell pointer index and the start
9724 ** of cell content.
9726 ** The loop below pulls entries from the min-heap in order and compares
9727 ** the start_address against the previous end_address. If there is an
9728 ** overlap, that means bytes are used multiple times. If there is a gap,
9729 ** that gap is added to the fragmentation count.
9731 nFrag = 0;
9732 prev = contentOffset - 1; /* Implied first min-heap entry */
9733 while( btreeHeapPull(heap,&x) ){
9734 if( (prev&0xffff)>=(x>>16) ){
9735 checkAppendMsg(pCheck,
9736 "Multiple uses for byte %u of page %d", x>>16, iPage);
9737 break;
9738 }else{
9739 nFrag += (x>>16) - (prev&0xffff) - 1;
9740 prev = x;
9743 nFrag += usableSize - (prev&0xffff) - 1;
9744 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
9745 ** is stored in the fifth field of the b-tree page header.
9746 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
9747 ** number of fragmented free bytes within the cell content area.
9749 if( heap[0]==0 && nFrag!=data[hdr+7] ){
9750 checkAppendMsg(pCheck,
9751 "Fragmentation of %d bytes reported as %d on page %d",
9752 nFrag, data[hdr+7], iPage);
9756 end_of_check:
9757 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
9758 releasePage(pPage);
9759 pCheck->zPfx = saved_zPfx;
9760 pCheck->v1 = saved_v1;
9761 pCheck->v2 = saved_v2;
9762 return depth+1;
9764 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9766 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9768 ** This routine does a complete check of the given BTree file. aRoot[] is
9769 ** an array of pages numbers were each page number is the root page of
9770 ** a table. nRoot is the number of entries in aRoot.
9772 ** A read-only or read-write transaction must be opened before calling
9773 ** this function.
9775 ** Write the number of error seen in *pnErr. Except for some memory
9776 ** allocation errors, an error message held in memory obtained from
9777 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
9778 ** returned. If a memory allocation error occurs, NULL is returned.
9780 char *sqlite3BtreeIntegrityCheck(
9781 Btree *p, /* The btree to be checked */
9782 int *aRoot, /* An array of root pages numbers for individual trees */
9783 int nRoot, /* Number of entries in aRoot[] */
9784 int mxErr, /* Stop reporting errors after this many */
9785 int *pnErr /* Write number of errors seen to this variable */
9787 Pgno i;
9788 IntegrityCk sCheck;
9789 BtShared *pBt = p->pBt;
9790 int savedDbFlags = pBt->db->flags;
9791 char zErr[100];
9792 VVA_ONLY( int nRef );
9794 sqlite3BtreeEnter(p);
9795 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
9796 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
9797 assert( nRef>=0 );
9798 sCheck.pBt = pBt;
9799 sCheck.pPager = pBt->pPager;
9800 sCheck.nPage = btreePagecount(sCheck.pBt);
9801 sCheck.mxErr = mxErr;
9802 sCheck.nErr = 0;
9803 sCheck.mallocFailed = 0;
9804 sCheck.zPfx = 0;
9805 sCheck.v1 = 0;
9806 sCheck.v2 = 0;
9807 sCheck.aPgRef = 0;
9808 sCheck.heap = 0;
9809 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
9810 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
9811 if( sCheck.nPage==0 ){
9812 goto integrity_ck_cleanup;
9815 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
9816 if( !sCheck.aPgRef ){
9817 sCheck.mallocFailed = 1;
9818 goto integrity_ck_cleanup;
9820 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
9821 if( sCheck.heap==0 ){
9822 sCheck.mallocFailed = 1;
9823 goto integrity_ck_cleanup;
9826 i = PENDING_BYTE_PAGE(pBt);
9827 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
9829 /* Check the integrity of the freelist
9831 sCheck.zPfx = "Main freelist: ";
9832 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
9833 get4byte(&pBt->pPage1->aData[36]));
9834 sCheck.zPfx = 0;
9836 /* Check all the tables.
9838 testcase( pBt->db->flags & SQLITE_CellSizeCk );
9839 pBt->db->flags &= ~SQLITE_CellSizeCk;
9840 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
9841 i64 notUsed;
9842 if( aRoot[i]==0 ) continue;
9843 #ifndef SQLITE_OMIT_AUTOVACUUM
9844 if( pBt->autoVacuum && aRoot[i]>1 ){
9845 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
9847 #endif
9848 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
9850 pBt->db->flags = savedDbFlags;
9852 /* Make sure every page in the file is referenced
9854 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
9855 #ifdef SQLITE_OMIT_AUTOVACUUM
9856 if( getPageReferenced(&sCheck, i)==0 ){
9857 checkAppendMsg(&sCheck, "Page %d is never used", i);
9859 #else
9860 /* If the database supports auto-vacuum, make sure no tables contain
9861 ** references to pointer-map pages.
9863 if( getPageReferenced(&sCheck, i)==0 &&
9864 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
9865 checkAppendMsg(&sCheck, "Page %d is never used", i);
9867 if( getPageReferenced(&sCheck, i)!=0 &&
9868 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
9869 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
9871 #endif
9874 /* Clean up and report errors.
9876 integrity_ck_cleanup:
9877 sqlite3PageFree(sCheck.heap);
9878 sqlite3_free(sCheck.aPgRef);
9879 if( sCheck.mallocFailed ){
9880 sqlite3_str_reset(&sCheck.errMsg);
9881 sCheck.nErr++;
9883 *pnErr = sCheck.nErr;
9884 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
9885 /* Make sure this analysis did not leave any unref() pages. */
9886 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
9887 sqlite3BtreeLeave(p);
9888 return sqlite3StrAccumFinish(&sCheck.errMsg);
9890 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9893 ** Return the full pathname of the underlying database file. Return
9894 ** an empty string if the database is in-memory or a TEMP database.
9896 ** The pager filename is invariant as long as the pager is
9897 ** open so it is safe to access without the BtShared mutex.
9899 const char *sqlite3BtreeGetFilename(Btree *p){
9900 assert( p->pBt->pPager!=0 );
9901 return sqlite3PagerFilename(p->pBt->pPager, 1);
9905 ** Return the pathname of the journal file for this database. The return
9906 ** value of this routine is the same regardless of whether the journal file
9907 ** has been created or not.
9909 ** The pager journal filename is invariant as long as the pager is
9910 ** open so it is safe to access without the BtShared mutex.
9912 const char *sqlite3BtreeGetJournalname(Btree *p){
9913 assert( p->pBt->pPager!=0 );
9914 return sqlite3PagerJournalname(p->pBt->pPager);
9918 ** Return non-zero if a transaction is active.
9920 int sqlite3BtreeIsInTrans(Btree *p){
9921 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
9922 return (p && (p->inTrans==TRANS_WRITE));
9925 #ifndef SQLITE_OMIT_WAL
9927 ** Run a checkpoint on the Btree passed as the first argument.
9929 ** Return SQLITE_LOCKED if this or any other connection has an open
9930 ** transaction on the shared-cache the argument Btree is connected to.
9932 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
9934 int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
9935 int rc = SQLITE_OK;
9936 if( p ){
9937 BtShared *pBt = p->pBt;
9938 sqlite3BtreeEnter(p);
9939 if( pBt->inTransaction!=TRANS_NONE ){
9940 rc = SQLITE_LOCKED;
9941 }else{
9942 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
9944 sqlite3BtreeLeave(p);
9946 return rc;
9948 #endif
9951 ** Return non-zero if a read (or write) transaction is active.
9953 int sqlite3BtreeIsInReadTrans(Btree *p){
9954 assert( p );
9955 assert( sqlite3_mutex_held(p->db->mutex) );
9956 return p->inTrans!=TRANS_NONE;
9959 int sqlite3BtreeIsInBackup(Btree *p){
9960 assert( p );
9961 assert( sqlite3_mutex_held(p->db->mutex) );
9962 return p->nBackup!=0;
9966 ** This function returns a pointer to a blob of memory associated with
9967 ** a single shared-btree. The memory is used by client code for its own
9968 ** purposes (for example, to store a high-level schema associated with
9969 ** the shared-btree). The btree layer manages reference counting issues.
9971 ** The first time this is called on a shared-btree, nBytes bytes of memory
9972 ** are allocated, zeroed, and returned to the caller. For each subsequent
9973 ** call the nBytes parameter is ignored and a pointer to the same blob
9974 ** of memory returned.
9976 ** If the nBytes parameter is 0 and the blob of memory has not yet been
9977 ** allocated, a null pointer is returned. If the blob has already been
9978 ** allocated, it is returned as normal.
9980 ** Just before the shared-btree is closed, the function passed as the
9981 ** xFree argument when the memory allocation was made is invoked on the
9982 ** blob of allocated memory. The xFree function should not call sqlite3_free()
9983 ** on the memory, the btree layer does that.
9985 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
9986 BtShared *pBt = p->pBt;
9987 sqlite3BtreeEnter(p);
9988 if( !pBt->pSchema && nBytes ){
9989 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
9990 pBt->xFreeSchema = xFree;
9992 sqlite3BtreeLeave(p);
9993 return pBt->pSchema;
9997 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
9998 ** btree as the argument handle holds an exclusive lock on the
9999 ** sqlite_master table. Otherwise SQLITE_OK.
10001 int sqlite3BtreeSchemaLocked(Btree *p){
10002 int rc;
10003 assert( sqlite3_mutex_held(p->db->mutex) );
10004 sqlite3BtreeEnter(p);
10005 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
10006 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
10007 sqlite3BtreeLeave(p);
10008 return rc;
10012 #ifndef SQLITE_OMIT_SHARED_CACHE
10014 ** Obtain a lock on the table whose root page is iTab. The
10015 ** lock is a write lock if isWritelock is true or a read lock
10016 ** if it is false.
10018 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
10019 int rc = SQLITE_OK;
10020 assert( p->inTrans!=TRANS_NONE );
10021 if( p->sharable ){
10022 u8 lockType = READ_LOCK + isWriteLock;
10023 assert( READ_LOCK+1==WRITE_LOCK );
10024 assert( isWriteLock==0 || isWriteLock==1 );
10026 sqlite3BtreeEnter(p);
10027 rc = querySharedCacheTableLock(p, iTab, lockType);
10028 if( rc==SQLITE_OK ){
10029 rc = setSharedCacheTableLock(p, iTab, lockType);
10031 sqlite3BtreeLeave(p);
10033 return rc;
10035 #endif
10037 #ifndef SQLITE_OMIT_INCRBLOB
10039 ** Argument pCsr must be a cursor opened for writing on an
10040 ** INTKEY table currently pointing at a valid table entry.
10041 ** This function modifies the data stored as part of that entry.
10043 ** Only the data content may only be modified, it is not possible to
10044 ** change the length of the data stored. If this function is called with
10045 ** parameters that attempt to write past the end of the existing data,
10046 ** no modifications are made and SQLITE_CORRUPT is returned.
10048 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
10049 int rc;
10050 assert( cursorOwnsBtShared(pCsr) );
10051 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
10052 assert( pCsr->curFlags & BTCF_Incrblob );
10054 rc = restoreCursorPosition(pCsr);
10055 if( rc!=SQLITE_OK ){
10056 return rc;
10058 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
10059 if( pCsr->eState!=CURSOR_VALID ){
10060 return SQLITE_ABORT;
10063 /* Save the positions of all other cursors open on this table. This is
10064 ** required in case any of them are holding references to an xFetch
10065 ** version of the b-tree page modified by the accessPayload call below.
10067 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
10068 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
10069 ** saveAllCursors can only return SQLITE_OK.
10071 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
10072 assert( rc==SQLITE_OK );
10074 /* Check some assumptions:
10075 ** (a) the cursor is open for writing,
10076 ** (b) there is a read/write transaction open,
10077 ** (c) the connection holds a write-lock on the table (if required),
10078 ** (d) there are no conflicting read-locks, and
10079 ** (e) the cursor points at a valid row of an intKey table.
10081 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
10082 return SQLITE_READONLY;
10084 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
10085 && pCsr->pBt->inTransaction==TRANS_WRITE );
10086 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
10087 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
10088 assert( pCsr->pPage->intKey );
10090 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
10094 ** Mark this cursor as an incremental blob cursor.
10096 void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
10097 pCur->curFlags |= BTCF_Incrblob;
10098 pCur->pBtree->hasIncrblobCur = 1;
10100 #endif
10103 ** Set both the "read version" (single byte at byte offset 18) and
10104 ** "write version" (single byte at byte offset 19) fields in the database
10105 ** header to iVersion.
10107 int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
10108 BtShared *pBt = pBtree->pBt;
10109 int rc; /* Return code */
10111 assert( iVersion==1 || iVersion==2 );
10113 /* If setting the version fields to 1, do not automatically open the
10114 ** WAL connection, even if the version fields are currently set to 2.
10116 pBt->btsFlags &= ~BTS_NO_WAL;
10117 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
10119 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
10120 if( rc==SQLITE_OK ){
10121 u8 *aData = pBt->pPage1->aData;
10122 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
10123 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
10124 if( rc==SQLITE_OK ){
10125 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
10126 if( rc==SQLITE_OK ){
10127 aData[18] = (u8)iVersion;
10128 aData[19] = (u8)iVersion;
10134 pBt->btsFlags &= ~BTS_NO_WAL;
10135 return rc;
10139 ** Return true if the cursor has a hint specified. This routine is
10140 ** only used from within assert() statements
10142 int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
10143 return (pCsr->hints & mask)!=0;
10147 ** Return true if the given Btree is read-only.
10149 int sqlite3BtreeIsReadonly(Btree *p){
10150 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
10154 ** Return the size of the header added to each page by this module.
10156 int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
10158 #if !defined(SQLITE_OMIT_SHARED_CACHE)
10160 ** Return true if the Btree passed as the only argument is sharable.
10162 int sqlite3BtreeSharable(Btree *p){
10163 return p->sharable;
10167 ** Return the number of connections to the BtShared object accessed by
10168 ** the Btree handle passed as the only argument. For private caches
10169 ** this is always 1. For shared caches it may be 1 or greater.
10171 int sqlite3BtreeConnectionCount(Btree *p){
10172 testcase( p->sharable );
10173 return p->pBt->nRef;
10175 #endif