Remove the MySQL upsert syntax. As an optional alias to the insert table
[sqlite.git] / src / btree.c
blob402503916df6a04251909af34e2873004f747160
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 return pCur->eState!=CURSOR_VALID;
871 ** Return a pointer to a fake BtCursor object that will always answer
872 ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake
873 ** cursor returned must not be used with any other Btree interface.
875 BtCursor *sqlite3BtreeFakeValidCursor(void){
876 static u8 fakeCursor = CURSOR_VALID;
877 assert( offsetof(BtCursor, eState)==0 );
878 return (BtCursor*)&fakeCursor;
882 ** This routine restores a cursor back to its original position after it
883 ** has been moved by some outside activity (such as a btree rebalance or
884 ** a row having been deleted out from under the cursor).
886 ** On success, the *pDifferentRow parameter is false if the cursor is left
887 ** pointing at exactly the same row. *pDifferntRow is the row the cursor
888 ** was pointing to has been deleted, forcing the cursor to point to some
889 ** nearby row.
891 ** This routine should only be called for a cursor that just returned
892 ** TRUE from sqlite3BtreeCursorHasMoved().
894 int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
895 int rc;
897 assert( pCur!=0 );
898 assert( pCur->eState!=CURSOR_VALID );
899 rc = restoreCursorPosition(pCur);
900 if( rc ){
901 *pDifferentRow = 1;
902 return rc;
904 if( pCur->eState!=CURSOR_VALID ){
905 *pDifferentRow = 1;
906 }else{
907 assert( pCur->skipNext==0 );
908 *pDifferentRow = 0;
910 return SQLITE_OK;
913 #ifdef SQLITE_ENABLE_CURSOR_HINTS
915 ** Provide hints to the cursor. The particular hint given (and the type
916 ** and number of the varargs parameters) is determined by the eHintType
917 ** parameter. See the definitions of the BTREE_HINT_* macros for details.
919 void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){
920 /* Used only by system that substitute their own storage engine */
922 #endif
925 ** Provide flag hints to the cursor.
927 void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
928 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
929 pCur->hints = x;
933 #ifndef SQLITE_OMIT_AUTOVACUUM
935 ** Given a page number of a regular database page, return the page
936 ** number for the pointer-map page that contains the entry for the
937 ** input page number.
939 ** Return 0 (not a valid page) for pgno==1 since there is
940 ** no pointer map associated with page 1. The integrity_check logic
941 ** requires that ptrmapPageno(*,1)!=1.
943 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
944 int nPagesPerMapPage;
945 Pgno iPtrMap, ret;
946 assert( sqlite3_mutex_held(pBt->mutex) );
947 if( pgno<2 ) return 0;
948 nPagesPerMapPage = (pBt->usableSize/5)+1;
949 iPtrMap = (pgno-2)/nPagesPerMapPage;
950 ret = (iPtrMap*nPagesPerMapPage) + 2;
951 if( ret==PENDING_BYTE_PAGE(pBt) ){
952 ret++;
954 return ret;
958 ** Write an entry into the pointer map.
960 ** This routine updates the pointer map entry for page number 'key'
961 ** so that it maps to type 'eType' and parent page number 'pgno'.
963 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
964 ** a no-op. If an error occurs, the appropriate error code is written
965 ** into *pRC.
967 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
968 DbPage *pDbPage; /* The pointer map page */
969 u8 *pPtrmap; /* The pointer map data */
970 Pgno iPtrmap; /* The pointer map page number */
971 int offset; /* Offset in pointer map page */
972 int rc; /* Return code from subfunctions */
974 if( *pRC ) return;
976 assert( sqlite3_mutex_held(pBt->mutex) );
977 /* The master-journal page number must never be used as a pointer map page */
978 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
980 assert( pBt->autoVacuum );
981 if( key==0 ){
982 *pRC = SQLITE_CORRUPT_BKPT;
983 return;
985 iPtrmap = PTRMAP_PAGENO(pBt, key);
986 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
987 if( rc!=SQLITE_OK ){
988 *pRC = rc;
989 return;
991 offset = PTRMAP_PTROFFSET(iPtrmap, key);
992 if( offset<0 ){
993 *pRC = SQLITE_CORRUPT_BKPT;
994 goto ptrmap_exit;
996 assert( offset <= (int)pBt->usableSize-5 );
997 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
999 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
1000 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
1001 *pRC= rc = sqlite3PagerWrite(pDbPage);
1002 if( rc==SQLITE_OK ){
1003 pPtrmap[offset] = eType;
1004 put4byte(&pPtrmap[offset+1], parent);
1008 ptrmap_exit:
1009 sqlite3PagerUnref(pDbPage);
1013 ** Read an entry from the pointer map.
1015 ** This routine retrieves the pointer map entry for page 'key', writing
1016 ** the type and parent page number to *pEType and *pPgno respectively.
1017 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
1019 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
1020 DbPage *pDbPage; /* The pointer map page */
1021 int iPtrmap; /* Pointer map page index */
1022 u8 *pPtrmap; /* Pointer map page data */
1023 int offset; /* Offset of entry in pointer map */
1024 int rc;
1026 assert( sqlite3_mutex_held(pBt->mutex) );
1028 iPtrmap = PTRMAP_PAGENO(pBt, key);
1029 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
1030 if( rc!=0 ){
1031 return rc;
1033 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1035 offset = PTRMAP_PTROFFSET(iPtrmap, key);
1036 if( offset<0 ){
1037 sqlite3PagerUnref(pDbPage);
1038 return SQLITE_CORRUPT_BKPT;
1040 assert( offset <= (int)pBt->usableSize-5 );
1041 assert( pEType!=0 );
1042 *pEType = pPtrmap[offset];
1043 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
1045 sqlite3PagerUnref(pDbPage);
1046 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap);
1047 return SQLITE_OK;
1050 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
1051 #define ptrmapPut(w,x,y,z,rc)
1052 #define ptrmapGet(w,x,y,z) SQLITE_OK
1053 #define ptrmapPutOvflPtr(x, y, rc)
1054 #endif
1057 ** Given a btree page and a cell index (0 means the first cell on
1058 ** the page, 1 means the second cell, and so forth) return a pointer
1059 ** to the cell content.
1061 ** findCellPastPtr() does the same except it skips past the initial
1062 ** 4-byte child pointer found on interior pages, if there is one.
1064 ** This routine works only for pages that do not contain overflow cells.
1066 #define findCell(P,I) \
1067 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1068 #define findCellPastPtr(P,I) \
1069 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)])))
1073 ** This is common tail processing for btreeParseCellPtr() and
1074 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely
1075 ** on a single B-tree page. Make necessary adjustments to the CellInfo
1076 ** structure.
1078 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow(
1079 MemPage *pPage, /* Page containing the cell */
1080 u8 *pCell, /* Pointer to the cell text. */
1081 CellInfo *pInfo /* Fill in this structure */
1083 /* If the payload will not fit completely on the local page, we have
1084 ** to decide how much to store locally and how much to spill onto
1085 ** overflow pages. The strategy is to minimize the amount of unused
1086 ** space on overflow pages while keeping the amount of local storage
1087 ** in between minLocal and maxLocal.
1089 ** Warning: changing the way overflow payload is distributed in any
1090 ** way will result in an incompatible file format.
1092 int minLocal; /* Minimum amount of payload held locally */
1093 int maxLocal; /* Maximum amount of payload held locally */
1094 int surplus; /* Overflow payload available for local storage */
1096 minLocal = pPage->minLocal;
1097 maxLocal = pPage->maxLocal;
1098 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
1099 testcase( surplus==maxLocal );
1100 testcase( surplus==maxLocal+1 );
1101 if( surplus <= maxLocal ){
1102 pInfo->nLocal = (u16)surplus;
1103 }else{
1104 pInfo->nLocal = (u16)minLocal;
1106 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
1110 ** The following routines are implementations of the MemPage.xParseCell()
1111 ** method.
1113 ** Parse a cell content block and fill in the CellInfo structure.
1115 ** btreeParseCellPtr() => table btree leaf nodes
1116 ** btreeParseCellNoPayload() => table btree internal nodes
1117 ** btreeParseCellPtrIndex() => index btree nodes
1119 ** There is also a wrapper function btreeParseCell() that works for
1120 ** all MemPage types and that references the cell by index rather than
1121 ** by pointer.
1123 static void btreeParseCellPtrNoPayload(
1124 MemPage *pPage, /* Page containing the cell */
1125 u8 *pCell, /* Pointer to the cell text. */
1126 CellInfo *pInfo /* Fill in this structure */
1128 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1129 assert( pPage->leaf==0 );
1130 assert( pPage->childPtrSize==4 );
1131 #ifndef SQLITE_DEBUG
1132 UNUSED_PARAMETER(pPage);
1133 #endif
1134 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey);
1135 pInfo->nPayload = 0;
1136 pInfo->nLocal = 0;
1137 pInfo->pPayload = 0;
1138 return;
1140 static void btreeParseCellPtr(
1141 MemPage *pPage, /* Page containing the cell */
1142 u8 *pCell, /* Pointer to the cell text. */
1143 CellInfo *pInfo /* Fill in this structure */
1145 u8 *pIter; /* For scanning through pCell */
1146 u32 nPayload; /* Number of bytes of cell payload */
1147 u64 iKey; /* Extracted Key value */
1149 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1150 assert( pPage->leaf==0 || pPage->leaf==1 );
1151 assert( pPage->intKeyLeaf );
1152 assert( pPage->childPtrSize==0 );
1153 pIter = pCell;
1155 /* The next block of code is equivalent to:
1157 ** pIter += getVarint32(pIter, nPayload);
1159 ** The code is inlined to avoid a function call.
1161 nPayload = *pIter;
1162 if( nPayload>=0x80 ){
1163 u8 *pEnd = &pIter[8];
1164 nPayload &= 0x7f;
1166 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1167 }while( (*pIter)>=0x80 && pIter<pEnd );
1169 pIter++;
1171 /* The next block of code is equivalent to:
1173 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey);
1175 ** The code is inlined to avoid a function call.
1177 iKey = *pIter;
1178 if( iKey>=0x80 ){
1179 u8 *pEnd = &pIter[7];
1180 iKey &= 0x7f;
1181 while(1){
1182 iKey = (iKey<<7) | (*++pIter & 0x7f);
1183 if( (*pIter)<0x80 ) break;
1184 if( pIter>=pEnd ){
1185 iKey = (iKey<<8) | *++pIter;
1186 break;
1190 pIter++;
1192 pInfo->nKey = *(i64*)&iKey;
1193 pInfo->nPayload = nPayload;
1194 pInfo->pPayload = pIter;
1195 testcase( nPayload==pPage->maxLocal );
1196 testcase( nPayload==pPage->maxLocal+1 );
1197 if( nPayload<=pPage->maxLocal ){
1198 /* This is the (easy) common case where the entire payload fits
1199 ** on the local page. No overflow is required.
1201 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1202 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1203 pInfo->nLocal = (u16)nPayload;
1204 }else{
1205 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1208 static void btreeParseCellPtrIndex(
1209 MemPage *pPage, /* Page containing the cell */
1210 u8 *pCell, /* Pointer to the cell text. */
1211 CellInfo *pInfo /* Fill in this structure */
1213 u8 *pIter; /* For scanning through pCell */
1214 u32 nPayload; /* Number of bytes of cell payload */
1216 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1217 assert( pPage->leaf==0 || pPage->leaf==1 );
1218 assert( pPage->intKeyLeaf==0 );
1219 pIter = pCell + pPage->childPtrSize;
1220 nPayload = *pIter;
1221 if( nPayload>=0x80 ){
1222 u8 *pEnd = &pIter[8];
1223 nPayload &= 0x7f;
1225 nPayload = (nPayload<<7) | (*++pIter & 0x7f);
1226 }while( *(pIter)>=0x80 && pIter<pEnd );
1228 pIter++;
1229 pInfo->nKey = nPayload;
1230 pInfo->nPayload = nPayload;
1231 pInfo->pPayload = pIter;
1232 testcase( nPayload==pPage->maxLocal );
1233 testcase( nPayload==pPage->maxLocal+1 );
1234 if( nPayload<=pPage->maxLocal ){
1235 /* This is the (easy) common case where the entire payload fits
1236 ** on the local page. No overflow is required.
1238 pInfo->nSize = nPayload + (u16)(pIter - pCell);
1239 if( pInfo->nSize<4 ) pInfo->nSize = 4;
1240 pInfo->nLocal = (u16)nPayload;
1241 }else{
1242 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
1245 static void btreeParseCell(
1246 MemPage *pPage, /* Page containing the cell */
1247 int iCell, /* The cell index. First cell is 0 */
1248 CellInfo *pInfo /* Fill in this structure */
1250 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo);
1254 ** The following routines are implementations of the MemPage.xCellSize
1255 ** method.
1257 ** Compute the total number of bytes that a Cell needs in the cell
1258 ** data area of the btree-page. The return number includes the cell
1259 ** data header and the local payload, but not any overflow page or
1260 ** the space used by the cell pointer.
1262 ** cellSizePtrNoPayload() => table internal nodes
1263 ** cellSizePtr() => all index nodes & table leaf nodes
1265 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
1266 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */
1267 u8 *pEnd; /* End mark for a varint */
1268 u32 nSize; /* Size value to return */
1270 #ifdef SQLITE_DEBUG
1271 /* The value returned by this function should always be the same as
1272 ** the (CellInfo.nSize) value found by doing a full parse of the
1273 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1274 ** this function verifies that this invariant is not violated. */
1275 CellInfo debuginfo;
1276 pPage->xParseCell(pPage, pCell, &debuginfo);
1277 #endif
1279 nSize = *pIter;
1280 if( nSize>=0x80 ){
1281 pEnd = &pIter[8];
1282 nSize &= 0x7f;
1284 nSize = (nSize<<7) | (*++pIter & 0x7f);
1285 }while( *(pIter)>=0x80 && pIter<pEnd );
1287 pIter++;
1288 if( pPage->intKey ){
1289 /* pIter now points at the 64-bit integer key value, a variable length
1290 ** integer. The following block moves pIter to point at the first byte
1291 ** past the end of the key value. */
1292 pEnd = &pIter[9];
1293 while( (*pIter++)&0x80 && pIter<pEnd );
1295 testcase( nSize==pPage->maxLocal );
1296 testcase( nSize==pPage->maxLocal+1 );
1297 if( nSize<=pPage->maxLocal ){
1298 nSize += (u32)(pIter - pCell);
1299 if( nSize<4 ) nSize = 4;
1300 }else{
1301 int minLocal = pPage->minLocal;
1302 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
1303 testcase( nSize==pPage->maxLocal );
1304 testcase( nSize==pPage->maxLocal+1 );
1305 if( nSize>pPage->maxLocal ){
1306 nSize = minLocal;
1308 nSize += 4 + (u16)(pIter - pCell);
1310 assert( nSize==debuginfo.nSize || CORRUPT_DB );
1311 return (u16)nSize;
1313 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
1314 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */
1315 u8 *pEnd; /* End mark for a varint */
1317 #ifdef SQLITE_DEBUG
1318 /* The value returned by this function should always be the same as
1319 ** the (CellInfo.nSize) value found by doing a full parse of the
1320 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
1321 ** this function verifies that this invariant is not violated. */
1322 CellInfo debuginfo;
1323 pPage->xParseCell(pPage, pCell, &debuginfo);
1324 #else
1325 UNUSED_PARAMETER(pPage);
1326 #endif
1328 assert( pPage->childPtrSize==4 );
1329 pEnd = pIter + 9;
1330 while( (*pIter++)&0x80 && pIter<pEnd );
1331 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB );
1332 return (u16)(pIter - pCell);
1336 #ifdef SQLITE_DEBUG
1337 /* This variation on cellSizePtr() is used inside of assert() statements
1338 ** only. */
1339 static u16 cellSize(MemPage *pPage, int iCell){
1340 return pPage->xCellSize(pPage, findCell(pPage, iCell));
1342 #endif
1344 #ifndef SQLITE_OMIT_AUTOVACUUM
1346 ** If the cell pCell, part of page pPage contains a pointer
1347 ** to an overflow page, insert an entry into the pointer-map
1348 ** for the overflow page.
1350 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
1351 CellInfo info;
1352 if( *pRC ) return;
1353 assert( pCell!=0 );
1354 pPage->xParseCell(pPage, pCell, &info);
1355 if( info.nLocal<info.nPayload ){
1356 Pgno ovfl = get4byte(&pCell[info.nSize-4]);
1357 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
1360 #endif
1364 ** Defragment the page given. This routine reorganizes cells within the
1365 ** page so that there are no free-blocks on the free-block list.
1367 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be
1368 ** present in the page after this routine returns.
1370 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a
1371 ** b-tree page so that there are no freeblocks or fragment bytes, all
1372 ** unused bytes are contained in the unallocated space region, and all
1373 ** cells are packed tightly at the end of the page.
1375 static int defragmentPage(MemPage *pPage, int nMaxFrag){
1376 int i; /* Loop counter */
1377 int pc; /* Address of the i-th cell */
1378 int hdr; /* Offset to the page header */
1379 int size; /* Size of a cell */
1380 int usableSize; /* Number of usable bytes on a page */
1381 int cellOffset; /* Offset to the cell pointer array */
1382 int cbrk; /* Offset to the cell content area */
1383 int nCell; /* Number of cells on the page */
1384 unsigned char *data; /* The page data */
1385 unsigned char *temp; /* Temp area for cell content */
1386 unsigned char *src; /* Source of content */
1387 int iCellFirst; /* First allowable cell index */
1388 int iCellLast; /* Last possible cell index */
1390 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1391 assert( pPage->pBt!=0 );
1392 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1393 assert( pPage->nOverflow==0 );
1394 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1395 temp = 0;
1396 src = data = pPage->aData;
1397 hdr = pPage->hdrOffset;
1398 cellOffset = pPage->cellOffset;
1399 nCell = pPage->nCell;
1400 assert( nCell==get2byte(&data[hdr+3]) );
1401 iCellFirst = cellOffset + 2*nCell;
1402 usableSize = pPage->pBt->usableSize;
1404 /* This block handles pages with two or fewer free blocks and nMaxFrag
1405 ** or fewer fragmented bytes. In this case it is faster to move the
1406 ** two (or one) blocks of cells using memmove() and add the required
1407 ** offsets to each pointer in the cell-pointer array than it is to
1408 ** reconstruct the entire page. */
1409 if( (int)data[hdr+7]<=nMaxFrag ){
1410 int iFree = get2byte(&data[hdr+1]);
1411 if( iFree ){
1412 int iFree2 = get2byte(&data[iFree]);
1414 /* pageFindSlot() has already verified that free blocks are sorted
1415 ** in order of offset within the page, and that no block extends
1416 ** past the end of the page. Provided the two free slots do not
1417 ** overlap, this guarantees that the memmove() calls below will not
1418 ** overwrite the usableSize byte buffer, even if the database page
1419 ** is corrupt. */
1420 assert( iFree2==0 || iFree2>iFree );
1421 assert( iFree+get2byte(&data[iFree+2]) <= usableSize );
1422 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize );
1424 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
1425 u8 *pEnd = &data[cellOffset + nCell*2];
1426 u8 *pAddr;
1427 int sz2 = 0;
1428 int sz = get2byte(&data[iFree+2]);
1429 int top = get2byte(&data[hdr+5]);
1430 if( top>=iFree ){
1431 return SQLITE_CORRUPT_PAGE(pPage);
1433 if( iFree2 ){
1434 assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
1435 sz2 = get2byte(&data[iFree2+2]);
1436 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
1437 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
1438 sz += sz2;
1440 cbrk = top+sz;
1441 assert( cbrk+(iFree-top) <= usableSize );
1442 memmove(&data[cbrk], &data[top], iFree-top);
1443 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
1444 pc = get2byte(pAddr);
1445 if( pc<iFree ){ put2byte(pAddr, pc+sz); }
1446 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); }
1448 goto defragment_out;
1453 cbrk = usableSize;
1454 iCellLast = usableSize - 4;
1455 for(i=0; i<nCell; i++){
1456 u8 *pAddr; /* The i-th cell pointer */
1457 pAddr = &data[cellOffset + i*2];
1458 pc = get2byte(pAddr);
1459 testcase( pc==iCellFirst );
1460 testcase( pc==iCellLast );
1461 /* These conditions have already been verified in btreeInitPage()
1462 ** if PRAGMA cell_size_check=ON.
1464 if( pc<iCellFirst || pc>iCellLast ){
1465 return SQLITE_CORRUPT_PAGE(pPage);
1467 assert( pc>=iCellFirst && pc<=iCellLast );
1468 size = pPage->xCellSize(pPage, &src[pc]);
1469 cbrk -= size;
1470 if( cbrk<iCellFirst || pc+size>usableSize ){
1471 return SQLITE_CORRUPT_PAGE(pPage);
1473 assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
1474 testcase( cbrk+size==usableSize );
1475 testcase( pc+size==usableSize );
1476 put2byte(pAddr, cbrk);
1477 if( temp==0 ){
1478 int x;
1479 if( cbrk==pc ) continue;
1480 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
1481 x = get2byte(&data[hdr+5]);
1482 memcpy(&temp[x], &data[x], (cbrk+size) - x);
1483 src = temp;
1485 memcpy(&data[cbrk], &src[pc], size);
1487 data[hdr+7] = 0;
1489 defragment_out:
1490 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
1491 return SQLITE_CORRUPT_PAGE(pPage);
1493 assert( cbrk>=iCellFirst );
1494 put2byte(&data[hdr+5], cbrk);
1495 data[hdr+1] = 0;
1496 data[hdr+2] = 0;
1497 memset(&data[iCellFirst], 0, cbrk-iCellFirst);
1498 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1499 return SQLITE_OK;
1503 ** Search the free-list on page pPg for space to store a cell nByte bytes in
1504 ** size. If one can be found, return a pointer to the space and remove it
1505 ** from the free-list.
1507 ** If no suitable space can be found on the free-list, return NULL.
1509 ** This function may detect corruption within pPg. If corruption is
1510 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned.
1512 ** Slots on the free list that are between 1 and 3 bytes larger than nByte
1513 ** will be ignored if adding the extra space to the fragmentation count
1514 ** causes the fragmentation count to exceed 60.
1516 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
1517 const int hdr = pPg->hdrOffset;
1518 u8 * const aData = pPg->aData;
1519 int iAddr = hdr + 1;
1520 int pc = get2byte(&aData[iAddr]);
1521 int x;
1522 int usableSize = pPg->pBt->usableSize;
1523 int size; /* Size of the free slot */
1525 assert( pc>0 );
1526 while( pc<=usableSize-4 ){
1527 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each
1528 ** freeblock form a big-endian integer which is the size of the freeblock
1529 ** in bytes, including the 4-byte header. */
1530 size = get2byte(&aData[pc+2]);
1531 if( (x = size - nByte)>=0 ){
1532 testcase( x==4 );
1533 testcase( x==3 );
1534 if( size+pc > usableSize ){
1535 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1536 return 0;
1537 }else if( x<4 ){
1538 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
1539 ** number of bytes in fragments may not exceed 60. */
1540 if( aData[hdr+7]>57 ) return 0;
1542 /* Remove the slot from the free-list. Update the number of
1543 ** fragmented bytes within the page. */
1544 memcpy(&aData[iAddr], &aData[pc], 2);
1545 aData[hdr+7] += (u8)x;
1546 }else{
1547 /* The slot remains on the free-list. Reduce its size to account
1548 ** for the portion used by the new allocation. */
1549 put2byte(&aData[pc+2], x);
1551 return &aData[pc + x];
1553 iAddr = pc;
1554 pc = get2byte(&aData[pc]);
1555 if( pc<iAddr+size ) break;
1557 if( pc ){
1558 *pRc = SQLITE_CORRUPT_PAGE(pPg);
1561 return 0;
1565 ** Allocate nByte bytes of space from within the B-Tree page passed
1566 ** as the first argument. Write into *pIdx the index into pPage->aData[]
1567 ** of the first byte of allocated space. Return either SQLITE_OK or
1568 ** an error code (usually SQLITE_CORRUPT).
1570 ** The caller guarantees that there is sufficient space to make the
1571 ** allocation. This routine might need to defragment in order to bring
1572 ** all the space together, however. This routine will avoid using
1573 ** the first two bytes past the cell pointer area since presumably this
1574 ** allocation is being made in order to insert a new cell, so we will
1575 ** also end up needing a new cell pointer.
1577 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
1578 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */
1579 u8 * const data = pPage->aData; /* Local cache of pPage->aData */
1580 int top; /* First byte of cell content area */
1581 int rc = SQLITE_OK; /* Integer return code */
1582 int gap; /* First byte of gap between cell pointers and cell content */
1584 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1585 assert( pPage->pBt );
1586 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1587 assert( nByte>=0 ); /* Minimum cell size is 4 */
1588 assert( pPage->nFree>=nByte );
1589 assert( pPage->nOverflow==0 );
1590 assert( nByte < (int)(pPage->pBt->usableSize-8) );
1592 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
1593 gap = pPage->cellOffset + 2*pPage->nCell;
1594 assert( gap<=65536 );
1595 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
1596 ** and the reserved space is zero (the usual value for reserved space)
1597 ** then the cell content offset of an empty page wants to be 65536.
1598 ** However, that integer is too large to be stored in a 2-byte unsigned
1599 ** integer, so a value of 0 is used in its place. */
1600 top = get2byte(&data[hdr+5]);
1601 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
1602 if( gap>top ){
1603 if( top==0 && pPage->pBt->usableSize==65536 ){
1604 top = 65536;
1605 }else{
1606 return SQLITE_CORRUPT_PAGE(pPage);
1610 /* If there is enough space between gap and top for one more cell pointer
1611 ** array entry offset, and if the freelist is not empty, then search the
1612 ** freelist looking for a free slot big enough to satisfy the request.
1614 testcase( gap+2==top );
1615 testcase( gap+1==top );
1616 testcase( gap==top );
1617 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
1618 u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
1619 if( pSpace ){
1620 assert( pSpace>=data && (pSpace - data)<65536 );
1621 *pIdx = (int)(pSpace - data);
1622 return SQLITE_OK;
1623 }else if( rc ){
1624 return rc;
1628 /* The request could not be fulfilled using a freelist slot. Check
1629 ** to see if defragmentation is necessary.
1631 testcase( gap+2+nByte==top );
1632 if( gap+2+nByte>top ){
1633 assert( pPage->nCell>0 || CORRUPT_DB );
1634 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte)));
1635 if( rc ) return rc;
1636 top = get2byteNotZero(&data[hdr+5]);
1637 assert( gap+2+nByte<=top );
1641 /* Allocate memory from the gap in between the cell pointer array
1642 ** and the cell content area. The btreeInitPage() call has already
1643 ** validated the freelist. Given that the freelist is valid, there
1644 ** is no way that the allocation can extend off the end of the page.
1645 ** The assert() below verifies the previous sentence.
1647 top -= nByte;
1648 put2byte(&data[hdr+5], top);
1649 assert( top+nByte <= (int)pPage->pBt->usableSize );
1650 *pIdx = top;
1651 return SQLITE_OK;
1655 ** Return a section of the pPage->aData to the freelist.
1656 ** The first byte of the new free block is pPage->aData[iStart]
1657 ** and the size of the block is iSize bytes.
1659 ** Adjacent freeblocks are coalesced.
1661 ** Note that even though the freeblock list was checked by btreeInitPage(),
1662 ** that routine will not detect overlap between cells or freeblocks. Nor
1663 ** does it detect cells or freeblocks that encrouch into the reserved bytes
1664 ** at the end of the page. So do additional corruption checks inside this
1665 ** routine and return SQLITE_CORRUPT if any problems are found.
1667 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
1668 u16 iPtr; /* Address of ptr to next freeblock */
1669 u16 iFreeBlk; /* Address of the next freeblock */
1670 u8 hdr; /* Page header size. 0 or 100 */
1671 u8 nFrag = 0; /* Reduction in fragmentation */
1672 u16 iOrigSize = iSize; /* Original value of iSize */
1673 u16 x; /* Offset to cell content area */
1674 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
1675 unsigned char *data = pPage->aData; /* Page content */
1677 assert( pPage->pBt!=0 );
1678 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1679 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
1680 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize );
1681 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1682 assert( iSize>=4 ); /* Minimum cell size is 4 */
1683 assert( iStart<=pPage->pBt->usableSize-4 );
1685 /* The list of freeblocks must be in ascending order. Find the
1686 ** spot on the list where iStart should be inserted.
1688 hdr = pPage->hdrOffset;
1689 iPtr = hdr + 1;
1690 if( data[iPtr+1]==0 && data[iPtr]==0 ){
1691 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */
1692 }else{
1693 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
1694 if( iFreeBlk<iPtr+4 ){
1695 if( iFreeBlk==0 ) break;
1696 return SQLITE_CORRUPT_PAGE(pPage);
1698 iPtr = iFreeBlk;
1700 if( iFreeBlk>pPage->pBt->usableSize-4 ){
1701 return SQLITE_CORRUPT_PAGE(pPage);
1703 assert( iFreeBlk>iPtr || iFreeBlk==0 );
1705 /* At this point:
1706 ** iFreeBlk: First freeblock after iStart, or zero if none
1707 ** iPtr: The address of a pointer to iFreeBlk
1709 ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
1711 if( iFreeBlk && iEnd+3>=iFreeBlk ){
1712 nFrag = iFreeBlk - iEnd;
1713 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
1714 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
1715 if( iEnd > pPage->pBt->usableSize ){
1716 return SQLITE_CORRUPT_PAGE(pPage);
1718 iSize = iEnd - iStart;
1719 iFreeBlk = get2byte(&data[iFreeBlk]);
1722 /* If iPtr is another freeblock (that is, if iPtr is not the freelist
1723 ** pointer in the page header) then check to see if iStart should be
1724 ** coalesced onto the end of iPtr.
1726 if( iPtr>hdr+1 ){
1727 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
1728 if( iPtrEnd+3>=iStart ){
1729 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
1730 nFrag += iStart - iPtrEnd;
1731 iSize = iEnd - iPtr;
1732 iStart = iPtr;
1735 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
1736 data[hdr+7] -= nFrag;
1738 x = get2byte(&data[hdr+5]);
1739 if( iStart<=x ){
1740 /* The new freeblock is at the beginning of the cell content area,
1741 ** so just extend the cell content area rather than create another
1742 ** freelist entry */
1743 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
1744 put2byte(&data[hdr+1], iFreeBlk);
1745 put2byte(&data[hdr+5], iEnd);
1746 }else{
1747 /* Insert the new freeblock into the freelist */
1748 put2byte(&data[iPtr], iStart);
1750 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
1751 /* Overwrite deleted information with zeros when the secure_delete
1752 ** option is enabled */
1753 memset(&data[iStart], 0, iSize);
1755 put2byte(&data[iStart], iFreeBlk);
1756 put2byte(&data[iStart+2], iSize);
1757 pPage->nFree += iOrigSize;
1758 return SQLITE_OK;
1762 ** Decode the flags byte (the first byte of the header) for a page
1763 ** and initialize fields of the MemPage structure accordingly.
1765 ** Only the following combinations are supported. Anything different
1766 ** indicates a corrupt database files:
1768 ** PTF_ZERODATA
1769 ** PTF_ZERODATA | PTF_LEAF
1770 ** PTF_LEAFDATA | PTF_INTKEY
1771 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
1773 static int decodeFlags(MemPage *pPage, int flagByte){
1774 BtShared *pBt; /* A copy of pPage->pBt */
1776 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1777 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1778 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
1779 flagByte &= ~PTF_LEAF;
1780 pPage->childPtrSize = 4-4*pPage->leaf;
1781 pPage->xCellSize = cellSizePtr;
1782 pBt = pPage->pBt;
1783 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
1784 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an
1785 ** interior table b-tree page. */
1786 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 );
1787 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a
1788 ** leaf table b-tree page. */
1789 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 );
1790 pPage->intKey = 1;
1791 if( pPage->leaf ){
1792 pPage->intKeyLeaf = 1;
1793 pPage->xParseCell = btreeParseCellPtr;
1794 }else{
1795 pPage->intKeyLeaf = 0;
1796 pPage->xCellSize = cellSizePtrNoPayload;
1797 pPage->xParseCell = btreeParseCellPtrNoPayload;
1799 pPage->maxLocal = pBt->maxLeaf;
1800 pPage->minLocal = pBt->minLeaf;
1801 }else if( flagByte==PTF_ZERODATA ){
1802 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an
1803 ** interior index b-tree page. */
1804 assert( (PTF_ZERODATA)==2 );
1805 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a
1806 ** leaf index b-tree page. */
1807 assert( (PTF_ZERODATA|PTF_LEAF)==10 );
1808 pPage->intKey = 0;
1809 pPage->intKeyLeaf = 0;
1810 pPage->xParseCell = btreeParseCellPtrIndex;
1811 pPage->maxLocal = pBt->maxLocal;
1812 pPage->minLocal = pBt->minLocal;
1813 }else{
1814 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
1815 ** an error. */
1816 return SQLITE_CORRUPT_PAGE(pPage);
1818 pPage->max1bytePayload = pBt->max1bytePayload;
1819 return SQLITE_OK;
1823 ** Initialize the auxiliary information for a disk block.
1825 ** Return SQLITE_OK on success. If we see that the page does
1826 ** not contain a well-formed database page, then return
1827 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1828 ** guarantee that the page is well-formed. It only shows that
1829 ** we failed to detect any corruption.
1831 static int btreeInitPage(MemPage *pPage){
1832 int pc; /* Address of a freeblock within pPage->aData[] */
1833 u8 hdr; /* Offset to beginning of page header */
1834 u8 *data; /* Equal to pPage->aData */
1835 BtShared *pBt; /* The main btree structure */
1836 int usableSize; /* Amount of usable space on each page */
1837 u16 cellOffset; /* Offset from start of page to first cell pointer */
1838 int nFree; /* Number of unused bytes on the page */
1839 int top; /* First byte of the cell content area */
1840 int iCellFirst; /* First allowable cell or freeblock offset */
1841 int iCellLast; /* Last possible cell or freeblock offset */
1843 assert( pPage->pBt!=0 );
1844 assert( pPage->pBt->db!=0 );
1845 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1846 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1847 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1848 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
1849 assert( pPage->isInit==0 );
1851 pBt = pPage->pBt;
1852 hdr = pPage->hdrOffset;
1853 data = pPage->aData;
1854 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
1855 ** the b-tree page type. */
1856 if( decodeFlags(pPage, data[hdr]) ){
1857 return SQLITE_CORRUPT_PAGE(pPage);
1859 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
1860 pPage->maskPage = (u16)(pBt->pageSize - 1);
1861 pPage->nOverflow = 0;
1862 usableSize = pBt->usableSize;
1863 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
1864 pPage->aDataEnd = &data[usableSize];
1865 pPage->aCellIdx = &data[cellOffset];
1866 pPage->aDataOfst = &data[pPage->childPtrSize];
1867 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates
1868 ** the start of the cell content area. A zero value for this integer is
1869 ** interpreted as 65536. */
1870 top = get2byteNotZero(&data[hdr+5]);
1871 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
1872 ** number of cells on the page. */
1873 pPage->nCell = get2byte(&data[hdr+3]);
1874 if( pPage->nCell>MX_CELL(pBt) ){
1875 /* To many cells for a single page. The page must be corrupt */
1876 return SQLITE_CORRUPT_PAGE(pPage);
1878 testcase( pPage->nCell==MX_CELL(pBt) );
1879 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
1880 ** possible for a root page of a table that contains no rows) then the
1881 ** offset to the cell content area will equal the page size minus the
1882 ** bytes of reserved space. */
1883 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
1885 /* A malformed database page might cause us to read past the end
1886 ** of page when parsing a cell.
1888 ** The following block of code checks early to see if a cell extends
1889 ** past the end of a page boundary and causes SQLITE_CORRUPT to be
1890 ** returned if it does.
1892 iCellFirst = cellOffset + 2*pPage->nCell;
1893 iCellLast = usableSize - 4;
1894 if( pBt->db->flags & SQLITE_CellSizeCk ){
1895 int i; /* Index into the cell pointer array */
1896 int sz; /* Size of a cell */
1898 if( !pPage->leaf ) iCellLast--;
1899 for(i=0; i<pPage->nCell; i++){
1900 pc = get2byteAligned(&data[cellOffset+i*2]);
1901 testcase( pc==iCellFirst );
1902 testcase( pc==iCellLast );
1903 if( pc<iCellFirst || pc>iCellLast ){
1904 return SQLITE_CORRUPT_PAGE(pPage);
1906 sz = pPage->xCellSize(pPage, &data[pc]);
1907 testcase( pc+sz==usableSize );
1908 if( pc+sz>usableSize ){
1909 return SQLITE_CORRUPT_PAGE(pPage);
1912 if( !pPage->leaf ) iCellLast++;
1915 /* Compute the total free space on the page
1916 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
1917 ** start of the first freeblock on the page, or is zero if there are no
1918 ** freeblocks. */
1919 pc = get2byte(&data[hdr+1]);
1920 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */
1921 if( pc>0 ){
1922 u32 next, size;
1923 if( pc<iCellFirst ){
1924 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
1925 ** always be at least one cell before the first freeblock.
1927 return SQLITE_CORRUPT_PAGE(pPage);
1929 while( 1 ){
1930 if( pc>iCellLast ){
1931 /* Freeblock off the end of the page */
1932 return SQLITE_CORRUPT_PAGE(pPage);
1934 next = get2byte(&data[pc]);
1935 size = get2byte(&data[pc+2]);
1936 nFree = nFree + size;
1937 if( next<=pc+size+3 ) break;
1938 pc = next;
1940 if( next>0 ){
1941 /* Freeblock not in ascending order */
1942 return SQLITE_CORRUPT_PAGE(pPage);
1944 if( pc+size>(unsigned int)usableSize ){
1945 /* Last freeblock extends past page end */
1946 return SQLITE_CORRUPT_PAGE(pPage);
1950 /* At this point, nFree contains the sum of the offset to the start
1951 ** of the cell-content area plus the number of free bytes within
1952 ** the cell-content area. If this is greater than the usable-size
1953 ** of the page, then the page must be corrupted. This check also
1954 ** serves to verify that the offset to the start of the cell-content
1955 ** area, according to the page header, lies within the page.
1957 if( nFree>usableSize ){
1958 return SQLITE_CORRUPT_PAGE(pPage);
1960 pPage->nFree = (u16)(nFree - iCellFirst);
1961 pPage->isInit = 1;
1962 return SQLITE_OK;
1966 ** Set up a raw page so that it looks like a database page holding
1967 ** no entries.
1969 static void zeroPage(MemPage *pPage, int flags){
1970 unsigned char *data = pPage->aData;
1971 BtShared *pBt = pPage->pBt;
1972 u8 hdr = pPage->hdrOffset;
1973 u16 first;
1975 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
1976 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1977 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
1978 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1979 assert( sqlite3_mutex_held(pBt->mutex) );
1980 if( pBt->btsFlags & BTS_FAST_SECURE ){
1981 memset(&data[hdr], 0, pBt->usableSize - hdr);
1983 data[hdr] = (char)flags;
1984 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
1985 memset(&data[hdr+1], 0, 4);
1986 data[hdr+7] = 0;
1987 put2byte(&data[hdr+5], pBt->usableSize);
1988 pPage->nFree = (u16)(pBt->usableSize - first);
1989 decodeFlags(pPage, flags);
1990 pPage->cellOffset = first;
1991 pPage->aDataEnd = &data[pBt->usableSize];
1992 pPage->aCellIdx = &data[first];
1993 pPage->aDataOfst = &data[pPage->childPtrSize];
1994 pPage->nOverflow = 0;
1995 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
1996 pPage->maskPage = (u16)(pBt->pageSize - 1);
1997 pPage->nCell = 0;
1998 pPage->isInit = 1;
2003 ** Convert a DbPage obtained from the pager into a MemPage used by
2004 ** the btree layer.
2006 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
2007 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2008 if( pgno!=pPage->pgno ){
2009 pPage->aData = sqlite3PagerGetData(pDbPage);
2010 pPage->pDbPage = pDbPage;
2011 pPage->pBt = pBt;
2012 pPage->pgno = pgno;
2013 pPage->hdrOffset = pgno==1 ? 100 : 0;
2015 assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
2016 return pPage;
2020 ** Get a page from the pager. Initialize the MemPage.pBt and
2021 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage().
2023 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care
2024 ** about the content of the page at this time. So do not go to the disk
2025 ** to fetch the content. Just fill in the content with zeros for now.
2026 ** If in the future we call sqlite3PagerWrite() on this page, that
2027 ** means we have started to be concerned about content and the disk
2028 ** read should occur at that point.
2030 static int btreeGetPage(
2031 BtShared *pBt, /* The btree */
2032 Pgno pgno, /* Number of the page to fetch */
2033 MemPage **ppPage, /* Return the page in this parameter */
2034 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2036 int rc;
2037 DbPage *pDbPage;
2039 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
2040 assert( sqlite3_mutex_held(pBt->mutex) );
2041 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
2042 if( rc ) return rc;
2043 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
2044 return SQLITE_OK;
2048 ** Retrieve a page from the pager cache. If the requested page is not
2049 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
2050 ** MemPage.aData elements if needed.
2052 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
2053 DbPage *pDbPage;
2054 assert( sqlite3_mutex_held(pBt->mutex) );
2055 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
2056 if( pDbPage ){
2057 return btreePageFromDbPage(pDbPage, pgno, pBt);
2059 return 0;
2063 ** Return the size of the database file in pages. If there is any kind of
2064 ** error, return ((unsigned int)-1).
2066 static Pgno btreePagecount(BtShared *pBt){
2067 return pBt->nPage;
2069 u32 sqlite3BtreeLastPage(Btree *p){
2070 assert( sqlite3BtreeHoldsMutex(p) );
2071 assert( ((p->pBt->nPage)&0x80000000)==0 );
2072 return btreePagecount(p->pBt);
2076 ** Get a page from the pager and initialize it.
2078 ** If pCur!=0 then the page is being fetched as part of a moveToChild()
2079 ** call. Do additional sanity checking on the page in this case.
2080 ** And if the fetch fails, this routine must decrement pCur->iPage.
2082 ** The page is fetched as read-write unless pCur is not NULL and is
2083 ** a read-only cursor.
2085 ** If an error occurs, then *ppPage is undefined. It
2086 ** may remain unchanged, or it may be set to an invalid value.
2088 static int getAndInitPage(
2089 BtShared *pBt, /* The database file */
2090 Pgno pgno, /* Number of the page to get */
2091 MemPage **ppPage, /* Write the page pointer here */
2092 BtCursor *pCur, /* Cursor to receive the page, or NULL */
2093 int bReadOnly /* True for a read-only page */
2095 int rc;
2096 DbPage *pDbPage;
2097 assert( sqlite3_mutex_held(pBt->mutex) );
2098 assert( pCur==0 || ppPage==&pCur->pPage );
2099 assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
2100 assert( pCur==0 || pCur->iPage>0 );
2102 if( pgno>btreePagecount(pBt) ){
2103 rc = SQLITE_CORRUPT_BKPT;
2104 goto getAndInitPage_error;
2106 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
2107 if( rc ){
2108 goto getAndInitPage_error;
2110 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
2111 if( (*ppPage)->isInit==0 ){
2112 btreePageFromDbPage(pDbPage, pgno, pBt);
2113 rc = btreeInitPage(*ppPage);
2114 if( rc!=SQLITE_OK ){
2115 releasePage(*ppPage);
2116 goto getAndInitPage_error;
2119 assert( (*ppPage)->pgno==pgno );
2120 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
2122 /* If obtaining a child page for a cursor, we must verify that the page is
2123 ** compatible with the root page. */
2124 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
2125 rc = SQLITE_CORRUPT_PGNO(pgno);
2126 releasePage(*ppPage);
2127 goto getAndInitPage_error;
2129 return SQLITE_OK;
2131 getAndInitPage_error:
2132 if( pCur ){
2133 pCur->iPage--;
2134 pCur->pPage = pCur->apPage[pCur->iPage];
2136 testcase( pgno==0 );
2137 assert( pgno!=0 || rc==SQLITE_CORRUPT );
2138 return rc;
2142 ** Release a MemPage. This should be called once for each prior
2143 ** call to btreeGetPage.
2145 ** Page1 is a special case and must be released using releasePageOne().
2147 static void releasePageNotNull(MemPage *pPage){
2148 assert( pPage->aData );
2149 assert( pPage->pBt );
2150 assert( pPage->pDbPage!=0 );
2151 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2152 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2153 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2154 sqlite3PagerUnrefNotNull(pPage->pDbPage);
2156 static void releasePage(MemPage *pPage){
2157 if( pPage ) releasePageNotNull(pPage);
2159 static void releasePageOne(MemPage *pPage){
2160 assert( pPage!=0 );
2161 assert( pPage->aData );
2162 assert( pPage->pBt );
2163 assert( pPage->pDbPage!=0 );
2164 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
2165 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
2166 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2167 sqlite3PagerUnrefPageOne(pPage->pDbPage);
2171 ** Get an unused page.
2173 ** This works just like btreeGetPage() with the addition:
2175 ** * If the page is already in use for some other purpose, immediately
2176 ** release it and return an SQLITE_CURRUPT error.
2177 ** * Make sure the isInit flag is clear
2179 static int btreeGetUnusedPage(
2180 BtShared *pBt, /* The btree */
2181 Pgno pgno, /* Number of the page to fetch */
2182 MemPage **ppPage, /* Return the page in this parameter */
2183 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
2185 int rc = btreeGetPage(pBt, pgno, ppPage, flags);
2186 if( rc==SQLITE_OK ){
2187 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
2188 releasePage(*ppPage);
2189 *ppPage = 0;
2190 return SQLITE_CORRUPT_BKPT;
2192 (*ppPage)->isInit = 0;
2193 }else{
2194 *ppPage = 0;
2196 return rc;
2201 ** During a rollback, when the pager reloads information into the cache
2202 ** so that the cache is restored to its original state at the start of
2203 ** the transaction, for each page restored this routine is called.
2205 ** This routine needs to reset the extra data section at the end of the
2206 ** page to agree with the restored data.
2208 static void pageReinit(DbPage *pData){
2209 MemPage *pPage;
2210 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
2211 assert( sqlite3PagerPageRefcount(pData)>0 );
2212 if( pPage->isInit ){
2213 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
2214 pPage->isInit = 0;
2215 if( sqlite3PagerPageRefcount(pData)>1 ){
2216 /* pPage might not be a btree page; it might be an overflow page
2217 ** or ptrmap page or a free page. In those cases, the following
2218 ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
2219 ** But no harm is done by this. And it is very important that
2220 ** btreeInitPage() be called on every btree page so we make
2221 ** the call for every page that comes in for re-initing. */
2222 btreeInitPage(pPage);
2228 ** Invoke the busy handler for a btree.
2230 static int btreeInvokeBusyHandler(void *pArg){
2231 BtShared *pBt = (BtShared*)pArg;
2232 assert( pBt->db );
2233 assert( sqlite3_mutex_held(pBt->db->mutex) );
2234 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
2235 sqlite3PagerFile(pBt->pPager));
2239 ** Open a database file.
2241 ** zFilename is the name of the database file. If zFilename is NULL
2242 ** then an ephemeral database is created. The ephemeral database might
2243 ** be exclusively in memory, or it might use a disk-based memory cache.
2244 ** Either way, the ephemeral database will be automatically deleted
2245 ** when sqlite3BtreeClose() is called.
2247 ** If zFilename is ":memory:" then an in-memory database is created
2248 ** that is automatically destroyed when it is closed.
2250 ** The "flags" parameter is a bitmask that might contain bits like
2251 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
2253 ** If the database is already opened in the same database connection
2254 ** and we are in shared cache mode, then the open will fail with an
2255 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared
2256 ** objects in the same database connection since doing so will lead
2257 ** to problems with locking.
2259 int sqlite3BtreeOpen(
2260 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */
2261 const char *zFilename, /* Name of the file containing the BTree database */
2262 sqlite3 *db, /* Associated database handle */
2263 Btree **ppBtree, /* Pointer to new Btree object written here */
2264 int flags, /* Options */
2265 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
2267 BtShared *pBt = 0; /* Shared part of btree structure */
2268 Btree *p; /* Handle to return */
2269 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */
2270 int rc = SQLITE_OK; /* Result code from this function */
2271 u8 nReserve; /* Byte of unused space on each page */
2272 unsigned char zDbHeader[100]; /* Database header content */
2274 /* True if opening an ephemeral, temporary database */
2275 const int isTempDb = zFilename==0 || zFilename[0]==0;
2277 /* Set the variable isMemdb to true for an in-memory database, or
2278 ** false for a file-based database.
2280 #ifdef SQLITE_OMIT_MEMORYDB
2281 const int isMemdb = 0;
2282 #else
2283 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
2284 || (isTempDb && sqlite3TempInMemory(db))
2285 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
2286 #endif
2288 assert( db!=0 );
2289 assert( pVfs!=0 );
2290 assert( sqlite3_mutex_held(db->mutex) );
2291 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */
2293 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
2294 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
2296 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
2297 assert( (flags & BTREE_SINGLE)==0 || isTempDb );
2299 if( isMemdb ){
2300 flags |= BTREE_MEMORY;
2302 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
2303 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
2305 p = sqlite3MallocZero(sizeof(Btree));
2306 if( !p ){
2307 return SQLITE_NOMEM_BKPT;
2309 p->inTrans = TRANS_NONE;
2310 p->db = db;
2311 #ifndef SQLITE_OMIT_SHARED_CACHE
2312 p->lock.pBtree = p;
2313 p->lock.iTable = 1;
2314 #endif
2316 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2318 ** If this Btree is a candidate for shared cache, try to find an
2319 ** existing BtShared object that we can share with
2321 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
2322 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
2323 int nFilename = sqlite3Strlen30(zFilename)+1;
2324 int nFullPathname = pVfs->mxPathname+1;
2325 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename));
2326 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2328 p->sharable = 1;
2329 if( !zFullPathname ){
2330 sqlite3_free(p);
2331 return SQLITE_NOMEM_BKPT;
2333 if( isMemdb ){
2334 memcpy(zFullPathname, zFilename, nFilename);
2335 }else{
2336 rc = sqlite3OsFullPathname(pVfs, zFilename,
2337 nFullPathname, zFullPathname);
2338 if( rc ){
2339 sqlite3_free(zFullPathname);
2340 sqlite3_free(p);
2341 return rc;
2344 #if SQLITE_THREADSAFE
2345 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
2346 sqlite3_mutex_enter(mutexOpen);
2347 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
2348 sqlite3_mutex_enter(mutexShared);
2349 #endif
2350 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
2351 assert( pBt->nRef>0 );
2352 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
2353 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
2354 int iDb;
2355 for(iDb=db->nDb-1; iDb>=0; iDb--){
2356 Btree *pExisting = db->aDb[iDb].pBt;
2357 if( pExisting && pExisting->pBt==pBt ){
2358 sqlite3_mutex_leave(mutexShared);
2359 sqlite3_mutex_leave(mutexOpen);
2360 sqlite3_free(zFullPathname);
2361 sqlite3_free(p);
2362 return SQLITE_CONSTRAINT;
2365 p->pBt = pBt;
2366 pBt->nRef++;
2367 break;
2370 sqlite3_mutex_leave(mutexShared);
2371 sqlite3_free(zFullPathname);
2373 #ifdef SQLITE_DEBUG
2374 else{
2375 /* In debug mode, we mark all persistent databases as sharable
2376 ** even when they are not. This exercises the locking code and
2377 ** gives more opportunity for asserts(sqlite3_mutex_held())
2378 ** statements to find locking problems.
2380 p->sharable = 1;
2382 #endif
2384 #endif
2385 if( pBt==0 ){
2387 ** The following asserts make sure that structures used by the btree are
2388 ** the right size. This is to guard against size changes that result
2389 ** when compiling on a different architecture.
2391 assert( sizeof(i64)==8 );
2392 assert( sizeof(u64)==8 );
2393 assert( sizeof(u32)==4 );
2394 assert( sizeof(u16)==2 );
2395 assert( sizeof(Pgno)==4 );
2397 pBt = sqlite3MallocZero( sizeof(*pBt) );
2398 if( pBt==0 ){
2399 rc = SQLITE_NOMEM_BKPT;
2400 goto btree_open_out;
2402 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
2403 sizeof(MemPage), flags, vfsFlags, pageReinit);
2404 if( rc==SQLITE_OK ){
2405 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
2406 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
2408 if( rc!=SQLITE_OK ){
2409 goto btree_open_out;
2411 pBt->openFlags = (u8)flags;
2412 pBt->db = db;
2413 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
2414 p->pBt = pBt;
2416 pBt->pCursor = 0;
2417 pBt->pPage1 = 0;
2418 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
2419 #if defined(SQLITE_SECURE_DELETE)
2420 pBt->btsFlags |= BTS_SECURE_DELETE;
2421 #elif defined(SQLITE_FAST_SECURE_DELETE)
2422 pBt->btsFlags |= BTS_OVERWRITE;
2423 #endif
2424 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
2425 ** determined by the 2-byte integer located at an offset of 16 bytes from
2426 ** the beginning of the database file. */
2427 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
2428 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
2429 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
2430 pBt->pageSize = 0;
2431 #ifndef SQLITE_OMIT_AUTOVACUUM
2432 /* If the magic name ":memory:" will create an in-memory database, then
2433 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
2434 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
2435 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
2436 ** regular file-name. In this case the auto-vacuum applies as per normal.
2438 if( zFilename && !isMemdb ){
2439 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
2440 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
2442 #endif
2443 nReserve = 0;
2444 }else{
2445 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is
2446 ** determined by the one-byte unsigned integer found at an offset of 20
2447 ** into the database file header. */
2448 nReserve = zDbHeader[20];
2449 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2450 #ifndef SQLITE_OMIT_AUTOVACUUM
2451 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
2452 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
2453 #endif
2455 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2456 if( rc ) goto btree_open_out;
2457 pBt->usableSize = pBt->pageSize - nReserve;
2458 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
2460 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2461 /* Add the new BtShared object to the linked list sharable BtShareds.
2463 pBt->nRef = 1;
2464 if( p->sharable ){
2465 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
2466 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
2467 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
2468 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
2469 if( pBt->mutex==0 ){
2470 rc = SQLITE_NOMEM_BKPT;
2471 goto btree_open_out;
2474 sqlite3_mutex_enter(mutexShared);
2475 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
2476 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
2477 sqlite3_mutex_leave(mutexShared);
2479 #endif
2482 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
2483 /* If the new Btree uses a sharable pBtShared, then link the new
2484 ** Btree into the list of all sharable Btrees for the same connection.
2485 ** The list is kept in ascending order by pBt address.
2487 if( p->sharable ){
2488 int i;
2489 Btree *pSib;
2490 for(i=0; i<db->nDb; i++){
2491 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
2492 while( pSib->pPrev ){ pSib = pSib->pPrev; }
2493 if( (uptr)p->pBt<(uptr)pSib->pBt ){
2494 p->pNext = pSib;
2495 p->pPrev = 0;
2496 pSib->pPrev = p;
2497 }else{
2498 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
2499 pSib = pSib->pNext;
2501 p->pNext = pSib->pNext;
2502 p->pPrev = pSib;
2503 if( p->pNext ){
2504 p->pNext->pPrev = p;
2506 pSib->pNext = p;
2508 break;
2512 #endif
2513 *ppBtree = p;
2515 btree_open_out:
2516 if( rc!=SQLITE_OK ){
2517 if( pBt && pBt->pPager ){
2518 sqlite3PagerClose(pBt->pPager, 0);
2520 sqlite3_free(pBt);
2521 sqlite3_free(p);
2522 *ppBtree = 0;
2523 }else{
2524 sqlite3_file *pFile;
2526 /* If the B-Tree was successfully opened, set the pager-cache size to the
2527 ** default value. Except, when opening on an existing shared pager-cache,
2528 ** do not change the pager-cache size.
2530 if( sqlite3BtreeSchema(p, 0, 0)==0 ){
2531 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
2534 pFile = sqlite3PagerFile(pBt->pPager);
2535 if( pFile->pMethods ){
2536 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db);
2539 if( mutexOpen ){
2540 assert( sqlite3_mutex_held(mutexOpen) );
2541 sqlite3_mutex_leave(mutexOpen);
2543 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 );
2544 return rc;
2548 ** Decrement the BtShared.nRef counter. When it reaches zero,
2549 ** remove the BtShared structure from the sharing list. Return
2550 ** true if the BtShared.nRef counter reaches zero and return
2551 ** false if it is still positive.
2553 static int removeFromSharingList(BtShared *pBt){
2554 #ifndef SQLITE_OMIT_SHARED_CACHE
2555 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
2556 BtShared *pList;
2557 int removed = 0;
2559 assert( sqlite3_mutex_notheld(pBt->mutex) );
2560 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
2561 sqlite3_mutex_enter(pMaster);
2562 pBt->nRef--;
2563 if( pBt->nRef<=0 ){
2564 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
2565 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
2566 }else{
2567 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
2568 while( ALWAYS(pList) && pList->pNext!=pBt ){
2569 pList=pList->pNext;
2571 if( ALWAYS(pList) ){
2572 pList->pNext = pBt->pNext;
2575 if( SQLITE_THREADSAFE ){
2576 sqlite3_mutex_free(pBt->mutex);
2578 removed = 1;
2580 sqlite3_mutex_leave(pMaster);
2581 return removed;
2582 #else
2583 return 1;
2584 #endif
2588 ** Make sure pBt->pTmpSpace points to an allocation of
2589 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child
2590 ** pointer.
2592 static void allocateTempSpace(BtShared *pBt){
2593 if( !pBt->pTmpSpace ){
2594 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
2596 /* One of the uses of pBt->pTmpSpace is to format cells before
2597 ** inserting them into a leaf page (function fillInCell()). If
2598 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
2599 ** by the various routines that manipulate binary cells. Which
2600 ** can mean that fillInCell() only initializes the first 2 or 3
2601 ** bytes of pTmpSpace, but that the first 4 bytes are copied from
2602 ** it into a database page. This is not actually a problem, but it
2603 ** does cause a valgrind error when the 1 or 2 bytes of unitialized
2604 ** data is passed to system call write(). So to avoid this error,
2605 ** zero the first 4 bytes of temp space here.
2607 ** Also: Provide four bytes of initialized space before the
2608 ** beginning of pTmpSpace as an area available to prepend the
2609 ** left-child pointer to the beginning of a cell.
2611 if( pBt->pTmpSpace ){
2612 memset(pBt->pTmpSpace, 0, 8);
2613 pBt->pTmpSpace += 4;
2619 ** Free the pBt->pTmpSpace allocation
2621 static void freeTempSpace(BtShared *pBt){
2622 if( pBt->pTmpSpace ){
2623 pBt->pTmpSpace -= 4;
2624 sqlite3PageFree(pBt->pTmpSpace);
2625 pBt->pTmpSpace = 0;
2630 ** Close an open database and invalidate all cursors.
2632 int sqlite3BtreeClose(Btree *p){
2633 BtShared *pBt = p->pBt;
2634 BtCursor *pCur;
2636 /* Close all cursors opened via this handle. */
2637 assert( sqlite3_mutex_held(p->db->mutex) );
2638 sqlite3BtreeEnter(p);
2639 pCur = pBt->pCursor;
2640 while( pCur ){
2641 BtCursor *pTmp = pCur;
2642 pCur = pCur->pNext;
2643 if( pTmp->pBtree==p ){
2644 sqlite3BtreeCloseCursor(pTmp);
2648 /* Rollback any active transaction and free the handle structure.
2649 ** The call to sqlite3BtreeRollback() drops any table-locks held by
2650 ** this handle.
2652 sqlite3BtreeRollback(p, SQLITE_OK, 0);
2653 sqlite3BtreeLeave(p);
2655 /* If there are still other outstanding references to the shared-btree
2656 ** structure, return now. The remainder of this procedure cleans
2657 ** up the shared-btree.
2659 assert( p->wantToLock==0 && p->locked==0 );
2660 if( !p->sharable || removeFromSharingList(pBt) ){
2661 /* The pBt is no longer on the sharing list, so we can access
2662 ** it without having to hold the mutex.
2664 ** Clean out and delete the BtShared object.
2666 assert( !pBt->pCursor );
2667 sqlite3PagerClose(pBt->pPager, p->db);
2668 if( pBt->xFreeSchema && pBt->pSchema ){
2669 pBt->xFreeSchema(pBt->pSchema);
2671 sqlite3DbFree(0, pBt->pSchema);
2672 freeTempSpace(pBt);
2673 sqlite3_free(pBt);
2676 #ifndef SQLITE_OMIT_SHARED_CACHE
2677 assert( p->wantToLock==0 );
2678 assert( p->locked==0 );
2679 if( p->pPrev ) p->pPrev->pNext = p->pNext;
2680 if( p->pNext ) p->pNext->pPrev = p->pPrev;
2681 #endif
2683 sqlite3_free(p);
2684 return SQLITE_OK;
2688 ** Change the "soft" limit on the number of pages in the cache.
2689 ** Unused and unmodified pages will be recycled when the number of
2690 ** pages in the cache exceeds this soft limit. But the size of the
2691 ** cache is allowed to grow larger than this limit if it contains
2692 ** dirty pages or pages still in active use.
2694 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
2695 BtShared *pBt = p->pBt;
2696 assert( sqlite3_mutex_held(p->db->mutex) );
2697 sqlite3BtreeEnter(p);
2698 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
2699 sqlite3BtreeLeave(p);
2700 return SQLITE_OK;
2704 ** Change the "spill" limit on the number of pages in the cache.
2705 ** If the number of pages exceeds this limit during a write transaction,
2706 ** the pager might attempt to "spill" pages to the journal early in
2707 ** order to free up memory.
2709 ** The value returned is the current spill size. If zero is passed
2710 ** as an argument, no changes are made to the spill size setting, so
2711 ** using mxPage of 0 is a way to query the current spill size.
2713 int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
2714 BtShared *pBt = p->pBt;
2715 int res;
2716 assert( sqlite3_mutex_held(p->db->mutex) );
2717 sqlite3BtreeEnter(p);
2718 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
2719 sqlite3BtreeLeave(p);
2720 return res;
2723 #if SQLITE_MAX_MMAP_SIZE>0
2725 ** Change the limit on the amount of the database file that may be
2726 ** memory mapped.
2728 int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
2729 BtShared *pBt = p->pBt;
2730 assert( sqlite3_mutex_held(p->db->mutex) );
2731 sqlite3BtreeEnter(p);
2732 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
2733 sqlite3BtreeLeave(p);
2734 return SQLITE_OK;
2736 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
2739 ** Change the way data is synced to disk in order to increase or decrease
2740 ** how well the database resists damage due to OS crashes and power
2741 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
2742 ** there is a high probability of damage) Level 2 is the default. There
2743 ** is a very low but non-zero probability of damage. Level 3 reduces the
2744 ** probability of damage to near zero but with a write performance reduction.
2746 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
2747 int sqlite3BtreeSetPagerFlags(
2748 Btree *p, /* The btree to set the safety level on */
2749 unsigned pgFlags /* Various PAGER_* flags */
2751 BtShared *pBt = p->pBt;
2752 assert( sqlite3_mutex_held(p->db->mutex) );
2753 sqlite3BtreeEnter(p);
2754 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
2755 sqlite3BtreeLeave(p);
2756 return SQLITE_OK;
2758 #endif
2761 ** Change the default pages size and the number of reserved bytes per page.
2762 ** Or, if the page size has already been fixed, return SQLITE_READONLY
2763 ** without changing anything.
2765 ** The page size must be a power of 2 between 512 and 65536. If the page
2766 ** size supplied does not meet this constraint then the page size is not
2767 ** changed.
2769 ** Page sizes are constrained to be a power of two so that the region
2770 ** of the database file used for locking (beginning at PENDING_BYTE,
2771 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
2772 ** at the beginning of a page.
2774 ** If parameter nReserve is less than zero, then the number of reserved
2775 ** bytes per page is left unchanged.
2777 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
2778 ** and autovacuum mode can no longer be changed.
2780 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
2781 int rc = SQLITE_OK;
2782 BtShared *pBt = p->pBt;
2783 assert( nReserve>=-1 && nReserve<=255 );
2784 sqlite3BtreeEnter(p);
2785 #if SQLITE_HAS_CODEC
2786 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve;
2787 #endif
2788 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
2789 sqlite3BtreeLeave(p);
2790 return SQLITE_READONLY;
2792 if( nReserve<0 ){
2793 nReserve = pBt->pageSize - pBt->usableSize;
2795 assert( nReserve>=0 && nReserve<=255 );
2796 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
2797 ((pageSize-1)&pageSize)==0 ){
2798 assert( (pageSize & 7)==0 );
2799 assert( !pBt->pCursor );
2800 pBt->pageSize = (u32)pageSize;
2801 freeTempSpace(pBt);
2803 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
2804 pBt->usableSize = pBt->pageSize - (u16)nReserve;
2805 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
2806 sqlite3BtreeLeave(p);
2807 return rc;
2811 ** Return the currently defined page size
2813 int sqlite3BtreeGetPageSize(Btree *p){
2814 return p->pBt->pageSize;
2818 ** This function is similar to sqlite3BtreeGetReserve(), except that it
2819 ** may only be called if it is guaranteed that the b-tree mutex is already
2820 ** held.
2822 ** This is useful in one special case in the backup API code where it is
2823 ** known that the shared b-tree mutex is held, but the mutex on the
2824 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
2825 ** were to be called, it might collide with some other operation on the
2826 ** database handle that owns *p, causing undefined behavior.
2828 int sqlite3BtreeGetReserveNoMutex(Btree *p){
2829 int n;
2830 assert( sqlite3_mutex_held(p->pBt->mutex) );
2831 n = p->pBt->pageSize - p->pBt->usableSize;
2832 return n;
2836 ** Return the number of bytes of space at the end of every page that
2837 ** are intentually left unused. This is the "reserved" space that is
2838 ** sometimes used by extensions.
2840 ** If SQLITE_HAS_MUTEX is defined then the number returned is the
2841 ** greater of the current reserved space and the maximum requested
2842 ** reserve space.
2844 int sqlite3BtreeGetOptimalReserve(Btree *p){
2845 int n;
2846 sqlite3BtreeEnter(p);
2847 n = sqlite3BtreeGetReserveNoMutex(p);
2848 #ifdef SQLITE_HAS_CODEC
2849 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve;
2850 #endif
2851 sqlite3BtreeLeave(p);
2852 return n;
2857 ** Set the maximum page count for a database if mxPage is positive.
2858 ** No changes are made if mxPage is 0 or negative.
2859 ** Regardless of the value of mxPage, return the maximum page count.
2861 int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
2862 int n;
2863 sqlite3BtreeEnter(p);
2864 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
2865 sqlite3BtreeLeave(p);
2866 return n;
2870 ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags:
2872 ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared
2873 ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared
2874 ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set
2875 ** newFlag==(-1) No changes
2877 ** This routine acts as a query if newFlag is less than zero
2879 ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but
2880 ** freelist leaf pages are not written back to the database. Thus in-page
2881 ** deleted content is cleared, but freelist deleted content is not.
2883 ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition
2884 ** that freelist leaf pages are written back into the database, increasing
2885 ** the amount of disk I/O.
2887 int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
2888 int b;
2889 if( p==0 ) return 0;
2890 sqlite3BtreeEnter(p);
2891 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
2892 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
2893 if( newFlag>=0 ){
2894 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
2895 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
2897 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
2898 sqlite3BtreeLeave(p);
2899 return b;
2903 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
2904 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
2905 ** is disabled. The default value for the auto-vacuum property is
2906 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
2908 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
2909 #ifdef SQLITE_OMIT_AUTOVACUUM
2910 return SQLITE_READONLY;
2911 #else
2912 BtShared *pBt = p->pBt;
2913 int rc = SQLITE_OK;
2914 u8 av = (u8)autoVacuum;
2916 sqlite3BtreeEnter(p);
2917 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
2918 rc = SQLITE_READONLY;
2919 }else{
2920 pBt->autoVacuum = av ?1:0;
2921 pBt->incrVacuum = av==2 ?1:0;
2923 sqlite3BtreeLeave(p);
2924 return rc;
2925 #endif
2929 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
2930 ** enabled 1 is returned. Otherwise 0.
2932 int sqlite3BtreeGetAutoVacuum(Btree *p){
2933 #ifdef SQLITE_OMIT_AUTOVACUUM
2934 return BTREE_AUTOVACUUM_NONE;
2935 #else
2936 int rc;
2937 sqlite3BtreeEnter(p);
2938 rc = (
2939 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
2940 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
2941 BTREE_AUTOVACUUM_INCR
2943 sqlite3BtreeLeave(p);
2944 return rc;
2945 #endif
2949 ** If the user has not set the safety-level for this database connection
2950 ** using "PRAGMA synchronous", and if the safety-level is not already
2951 ** set to the value passed to this function as the second parameter,
2952 ** set it so.
2954 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \
2955 && !defined(SQLITE_OMIT_WAL)
2956 static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){
2957 sqlite3 *db;
2958 Db *pDb;
2959 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){
2960 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; }
2961 if( pDb->bSyncSet==0
2962 && pDb->safety_level!=safety_level
2963 && pDb!=&db->aDb[1]
2965 pDb->safety_level = safety_level;
2966 sqlite3PagerSetFlags(pBt->pPager,
2967 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK));
2971 #else
2972 # define setDefaultSyncFlag(pBt,safety_level)
2973 #endif
2976 ** Get a reference to pPage1 of the database file. This will
2977 ** also acquire a readlock on that file.
2979 ** SQLITE_OK is returned on success. If the file is not a
2980 ** well-formed database file, then SQLITE_CORRUPT is returned.
2981 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
2982 ** is returned if we run out of memory.
2984 static int lockBtree(BtShared *pBt){
2985 int rc; /* Result code from subfunctions */
2986 MemPage *pPage1; /* Page 1 of the database file */
2987 int nPage; /* Number of pages in the database */
2988 int nPageFile = 0; /* Number of pages in the database file */
2989 int nPageHeader; /* Number of pages in the database according to hdr */
2991 assert( sqlite3_mutex_held(pBt->mutex) );
2992 assert( pBt->pPage1==0 );
2993 rc = sqlite3PagerSharedLock(pBt->pPager);
2994 if( rc!=SQLITE_OK ) return rc;
2995 rc = btreeGetPage(pBt, 1, &pPage1, 0);
2996 if( rc!=SQLITE_OK ) return rc;
2998 /* Do some checking to help insure the file we opened really is
2999 ** a valid database file.
3001 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
3002 sqlite3PagerPagecount(pBt->pPager, &nPageFile);
3003 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
3004 nPage = nPageFile;
3006 if( nPage>0 ){
3007 u32 pageSize;
3008 u32 usableSize;
3009 u8 *page1 = pPage1->aData;
3010 rc = SQLITE_NOTADB;
3011 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins
3012 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d
3013 ** 61 74 20 33 00. */
3014 if( memcmp(page1, zMagicHeader, 16)!=0 ){
3015 goto page1_init_failed;
3018 #ifdef SQLITE_OMIT_WAL
3019 if( page1[18]>1 ){
3020 pBt->btsFlags |= BTS_READ_ONLY;
3022 if( page1[19]>1 ){
3023 goto page1_init_failed;
3025 #else
3026 if( page1[18]>2 ){
3027 pBt->btsFlags |= BTS_READ_ONLY;
3029 if( page1[19]>2 ){
3030 goto page1_init_failed;
3033 /* If the write version is set to 2, this database should be accessed
3034 ** in WAL mode. If the log is not already open, open it now. Then
3035 ** return SQLITE_OK and return without populating BtShared.pPage1.
3036 ** The caller detects this and calls this function again. This is
3037 ** required as the version of page 1 currently in the page1 buffer
3038 ** may not be the latest version - there may be a newer one in the log
3039 ** file.
3041 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
3042 int isOpen = 0;
3043 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
3044 if( rc!=SQLITE_OK ){
3045 goto page1_init_failed;
3046 }else{
3047 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1);
3048 if( isOpen==0 ){
3049 releasePageOne(pPage1);
3050 return SQLITE_OK;
3053 rc = SQLITE_NOTADB;
3054 }else{
3055 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1);
3057 #endif
3059 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload
3060 ** fractions and the leaf payload fraction values must be 64, 32, and 32.
3062 ** The original design allowed these amounts to vary, but as of
3063 ** version 3.6.0, we require them to be fixed.
3065 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
3066 goto page1_init_failed;
3068 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is
3069 ** determined by the 2-byte integer located at an offset of 16 bytes from
3070 ** the beginning of the database file. */
3071 pageSize = (page1[16]<<8) | (page1[17]<<16);
3072 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two
3073 ** between 512 and 65536 inclusive. */
3074 if( ((pageSize-1)&pageSize)!=0
3075 || pageSize>SQLITE_MAX_PAGE_SIZE
3076 || pageSize<=256
3078 goto page1_init_failed;
3080 assert( (pageSize & 7)==0 );
3081 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte
3082 ** integer at offset 20 is the number of bytes of space at the end of
3083 ** each page to reserve for extensions.
3085 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is
3086 ** determined by the one-byte unsigned integer found at an offset of 20
3087 ** into the database file header. */
3088 usableSize = pageSize - page1[20];
3089 if( (u32)pageSize!=pBt->pageSize ){
3090 /* After reading the first page of the database assuming a page size
3091 ** of BtShared.pageSize, we have discovered that the page-size is
3092 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
3093 ** zero and return SQLITE_OK. The caller will call this function
3094 ** again with the correct page-size.
3096 releasePageOne(pPage1);
3097 pBt->usableSize = usableSize;
3098 pBt->pageSize = pageSize;
3099 freeTempSpace(pBt);
3100 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
3101 pageSize-usableSize);
3102 return rc;
3104 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){
3105 rc = SQLITE_CORRUPT_BKPT;
3106 goto page1_init_failed;
3108 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to
3109 ** be less than 480. In other words, if the page size is 512, then the
3110 ** reserved space size cannot exceed 32. */
3111 if( usableSize<480 ){
3112 goto page1_init_failed;
3114 pBt->pageSize = pageSize;
3115 pBt->usableSize = usableSize;
3116 #ifndef SQLITE_OMIT_AUTOVACUUM
3117 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
3118 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
3119 #endif
3122 /* maxLocal is the maximum amount of payload to store locally for
3123 ** a cell. Make sure it is small enough so that at least minFanout
3124 ** cells can will fit on one page. We assume a 10-byte page header.
3125 ** Besides the payload, the cell must store:
3126 ** 2-byte pointer to the cell
3127 ** 4-byte child pointer
3128 ** 9-byte nKey value
3129 ** 4-byte nData value
3130 ** 4-byte overflow page pointer
3131 ** So a cell consists of a 2-byte pointer, a header which is as much as
3132 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
3133 ** page pointer.
3135 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
3136 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
3137 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
3138 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
3139 if( pBt->maxLocal>127 ){
3140 pBt->max1bytePayload = 127;
3141 }else{
3142 pBt->max1bytePayload = (u8)pBt->maxLocal;
3144 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
3145 pBt->pPage1 = pPage1;
3146 pBt->nPage = nPage;
3147 return SQLITE_OK;
3149 page1_init_failed:
3150 releasePageOne(pPage1);
3151 pBt->pPage1 = 0;
3152 return rc;
3155 #ifndef NDEBUG
3157 ** Return the number of cursors open on pBt. This is for use
3158 ** in assert() expressions, so it is only compiled if NDEBUG is not
3159 ** defined.
3161 ** Only write cursors are counted if wrOnly is true. If wrOnly is
3162 ** false then all cursors are counted.
3164 ** For the purposes of this routine, a cursor is any cursor that
3165 ** is capable of reading or writing to the database. Cursors that
3166 ** have been tripped into the CURSOR_FAULT state are not counted.
3168 static int countValidCursors(BtShared *pBt, int wrOnly){
3169 BtCursor *pCur;
3170 int r = 0;
3171 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
3172 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0)
3173 && pCur->eState!=CURSOR_FAULT ) r++;
3175 return r;
3177 #endif
3180 ** If there are no outstanding cursors and we are not in the middle
3181 ** of a transaction but there is a read lock on the database, then
3182 ** this routine unrefs the first page of the database file which
3183 ** has the effect of releasing the read lock.
3185 ** If there is a transaction in progress, this routine is a no-op.
3187 static void unlockBtreeIfUnused(BtShared *pBt){
3188 assert( sqlite3_mutex_held(pBt->mutex) );
3189 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
3190 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
3191 MemPage *pPage1 = pBt->pPage1;
3192 assert( pPage1->aData );
3193 assert( sqlite3PagerRefcount(pBt->pPager)==1 );
3194 pBt->pPage1 = 0;
3195 releasePageOne(pPage1);
3200 ** If pBt points to an empty file then convert that empty file
3201 ** into a new empty database by initializing the first page of
3202 ** the database.
3204 static int newDatabase(BtShared *pBt){
3205 MemPage *pP1;
3206 unsigned char *data;
3207 int rc;
3209 assert( sqlite3_mutex_held(pBt->mutex) );
3210 if( pBt->nPage>0 ){
3211 return SQLITE_OK;
3213 pP1 = pBt->pPage1;
3214 assert( pP1!=0 );
3215 data = pP1->aData;
3216 rc = sqlite3PagerWrite(pP1->pDbPage);
3217 if( rc ) return rc;
3218 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
3219 assert( sizeof(zMagicHeader)==16 );
3220 data[16] = (u8)((pBt->pageSize>>8)&0xff);
3221 data[17] = (u8)((pBt->pageSize>>16)&0xff);
3222 data[18] = 1;
3223 data[19] = 1;
3224 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
3225 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
3226 data[21] = 64;
3227 data[22] = 32;
3228 data[23] = 32;
3229 memset(&data[24], 0, 100-24);
3230 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
3231 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
3232 #ifndef SQLITE_OMIT_AUTOVACUUM
3233 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
3234 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
3235 put4byte(&data[36 + 4*4], pBt->autoVacuum);
3236 put4byte(&data[36 + 7*4], pBt->incrVacuum);
3237 #endif
3238 pBt->nPage = 1;
3239 data[31] = 1;
3240 return SQLITE_OK;
3244 ** Initialize the first page of the database file (creating a database
3245 ** consisting of a single page and no schema objects). Return SQLITE_OK
3246 ** if successful, or an SQLite error code otherwise.
3248 int sqlite3BtreeNewDb(Btree *p){
3249 int rc;
3250 sqlite3BtreeEnter(p);
3251 p->pBt->nPage = 0;
3252 rc = newDatabase(p->pBt);
3253 sqlite3BtreeLeave(p);
3254 return rc;
3258 ** Attempt to start a new transaction. A write-transaction
3259 ** is started if the second argument is nonzero, otherwise a read-
3260 ** transaction. If the second argument is 2 or more and exclusive
3261 ** transaction is started, meaning that no other process is allowed
3262 ** to access the database. A preexisting transaction may not be
3263 ** upgraded to exclusive by calling this routine a second time - the
3264 ** exclusivity flag only works for a new transaction.
3266 ** A write-transaction must be started before attempting any
3267 ** changes to the database. None of the following routines
3268 ** will work unless a transaction is started first:
3270 ** sqlite3BtreeCreateTable()
3271 ** sqlite3BtreeCreateIndex()
3272 ** sqlite3BtreeClearTable()
3273 ** sqlite3BtreeDropTable()
3274 ** sqlite3BtreeInsert()
3275 ** sqlite3BtreeDelete()
3276 ** sqlite3BtreeUpdateMeta()
3278 ** If an initial attempt to acquire the lock fails because of lock contention
3279 ** and the database was previously unlocked, then invoke the busy handler
3280 ** if there is one. But if there was previously a read-lock, do not
3281 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
3282 ** returned when there is already a read-lock in order to avoid a deadlock.
3284 ** Suppose there are two processes A and B. A has a read lock and B has
3285 ** a reserved lock. B tries to promote to exclusive but is blocked because
3286 ** of A's read lock. A tries to promote to reserved but is blocked by B.
3287 ** One or the other of the two processes must give way or there can be
3288 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
3289 ** when A already has a read lock, we encourage A to give up and let B
3290 ** proceed.
3292 int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
3293 BtShared *pBt = p->pBt;
3294 int rc = SQLITE_OK;
3296 sqlite3BtreeEnter(p);
3297 btreeIntegrity(p);
3299 /* If the btree is already in a write-transaction, or it
3300 ** is already in a read-transaction and a read-transaction
3301 ** is requested, this is a no-op.
3303 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
3304 goto trans_begun;
3306 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
3308 /* Write transactions are not possible on a read-only database */
3309 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
3310 rc = SQLITE_READONLY;
3311 goto trans_begun;
3314 #ifndef SQLITE_OMIT_SHARED_CACHE
3316 sqlite3 *pBlock = 0;
3317 /* If another database handle has already opened a write transaction
3318 ** on this shared-btree structure and a second write transaction is
3319 ** requested, return SQLITE_LOCKED.
3321 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
3322 || (pBt->btsFlags & BTS_PENDING)!=0
3324 pBlock = pBt->pWriter->db;
3325 }else if( wrflag>1 ){
3326 BtLock *pIter;
3327 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3328 if( pIter->pBtree!=p ){
3329 pBlock = pIter->pBtree->db;
3330 break;
3334 if( pBlock ){
3335 sqlite3ConnectionBlocked(p->db, pBlock);
3336 rc = SQLITE_LOCKED_SHAREDCACHE;
3337 goto trans_begun;
3340 #endif
3342 /* Any read-only or read-write transaction implies a read-lock on
3343 ** page 1. So if some other shared-cache client already has a write-lock
3344 ** on page 1, the transaction cannot be opened. */
3345 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
3346 if( SQLITE_OK!=rc ) goto trans_begun;
3348 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
3349 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
3350 do {
3351 /* Call lockBtree() until either pBt->pPage1 is populated or
3352 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
3353 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
3354 ** reading page 1 it discovers that the page-size of the database
3355 ** file is not pBt->pageSize. In this case lockBtree() will update
3356 ** pBt->pageSize to the page-size of the file on disk.
3358 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
3360 if( rc==SQLITE_OK && wrflag ){
3361 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
3362 rc = SQLITE_READONLY;
3363 }else{
3364 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
3365 if( rc==SQLITE_OK ){
3366 rc = newDatabase(pBt);
3371 if( rc!=SQLITE_OK ){
3372 unlockBtreeIfUnused(pBt);
3374 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
3375 btreeInvokeBusyHandler(pBt) );
3376 sqlite3PagerResetLockTimeout(pBt->pPager);
3378 if( rc==SQLITE_OK ){
3379 if( p->inTrans==TRANS_NONE ){
3380 pBt->nTransaction++;
3381 #ifndef SQLITE_OMIT_SHARED_CACHE
3382 if( p->sharable ){
3383 assert( p->lock.pBtree==p && p->lock.iTable==1 );
3384 p->lock.eLock = READ_LOCK;
3385 p->lock.pNext = pBt->pLock;
3386 pBt->pLock = &p->lock;
3388 #endif
3390 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
3391 if( p->inTrans>pBt->inTransaction ){
3392 pBt->inTransaction = p->inTrans;
3394 if( wrflag ){
3395 MemPage *pPage1 = pBt->pPage1;
3396 #ifndef SQLITE_OMIT_SHARED_CACHE
3397 assert( !pBt->pWriter );
3398 pBt->pWriter = p;
3399 pBt->btsFlags &= ~BTS_EXCLUSIVE;
3400 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
3401 #endif
3403 /* If the db-size header field is incorrect (as it may be if an old
3404 ** client has been writing the database file), update it now. Doing
3405 ** this sooner rather than later means the database size can safely
3406 ** re-read the database size from page 1 if a savepoint or transaction
3407 ** rollback occurs within the transaction.
3409 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
3410 rc = sqlite3PagerWrite(pPage1->pDbPage);
3411 if( rc==SQLITE_OK ){
3412 put4byte(&pPage1->aData[28], pBt->nPage);
3419 trans_begun:
3420 if( rc==SQLITE_OK && wrflag ){
3421 /* This call makes sure that the pager has the correct number of
3422 ** open savepoints. If the second parameter is greater than 0 and
3423 ** the sub-journal is not already open, then it will be opened here.
3425 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
3428 btreeIntegrity(p);
3429 sqlite3BtreeLeave(p);
3430 return rc;
3433 #ifndef SQLITE_OMIT_AUTOVACUUM
3436 ** Set the pointer-map entries for all children of page pPage. Also, if
3437 ** pPage contains cells that point to overflow pages, set the pointer
3438 ** map entries for the overflow pages as well.
3440 static int setChildPtrmaps(MemPage *pPage){
3441 int i; /* Counter variable */
3442 int nCell; /* Number of cells in page pPage */
3443 int rc; /* Return code */
3444 BtShared *pBt = pPage->pBt;
3445 Pgno pgno = pPage->pgno;
3447 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3448 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3449 if( rc!=SQLITE_OK ) return rc;
3450 nCell = pPage->nCell;
3452 for(i=0; i<nCell; i++){
3453 u8 *pCell = findCell(pPage, i);
3455 ptrmapPutOvflPtr(pPage, pCell, &rc);
3457 if( !pPage->leaf ){
3458 Pgno childPgno = get4byte(pCell);
3459 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3463 if( !pPage->leaf ){
3464 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
3465 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
3468 return rc;
3472 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so
3473 ** that it points to iTo. Parameter eType describes the type of pointer to
3474 ** be modified, as follows:
3476 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
3477 ** page of pPage.
3479 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
3480 ** page pointed to by one of the cells on pPage.
3482 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
3483 ** overflow page in the list.
3485 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
3486 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
3487 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
3488 if( eType==PTRMAP_OVERFLOW2 ){
3489 /* The pointer is always the first 4 bytes of the page in this case. */
3490 if( get4byte(pPage->aData)!=iFrom ){
3491 return SQLITE_CORRUPT_PAGE(pPage);
3493 put4byte(pPage->aData, iTo);
3494 }else{
3495 int i;
3496 int nCell;
3497 int rc;
3499 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage);
3500 if( rc ) return rc;
3501 nCell = pPage->nCell;
3503 for(i=0; i<nCell; i++){
3504 u8 *pCell = findCell(pPage, i);
3505 if( eType==PTRMAP_OVERFLOW1 ){
3506 CellInfo info;
3507 pPage->xParseCell(pPage, pCell, &info);
3508 if( info.nLocal<info.nPayload ){
3509 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
3510 return SQLITE_CORRUPT_PAGE(pPage);
3512 if( iFrom==get4byte(pCell+info.nSize-4) ){
3513 put4byte(pCell+info.nSize-4, iTo);
3514 break;
3517 }else{
3518 if( get4byte(pCell)==iFrom ){
3519 put4byte(pCell, iTo);
3520 break;
3525 if( i==nCell ){
3526 if( eType!=PTRMAP_BTREE ||
3527 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
3528 return SQLITE_CORRUPT_PAGE(pPage);
3530 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
3533 return SQLITE_OK;
3538 ** Move the open database page pDbPage to location iFreePage in the
3539 ** database. The pDbPage reference remains valid.
3541 ** The isCommit flag indicates that there is no need to remember that
3542 ** the journal needs to be sync()ed before database page pDbPage->pgno
3543 ** can be written to. The caller has already promised not to write to that
3544 ** page.
3546 static int relocatePage(
3547 BtShared *pBt, /* Btree */
3548 MemPage *pDbPage, /* Open page to move */
3549 u8 eType, /* Pointer map 'type' entry for pDbPage */
3550 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
3551 Pgno iFreePage, /* The location to move pDbPage to */
3552 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */
3554 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
3555 Pgno iDbPage = pDbPage->pgno;
3556 Pager *pPager = pBt->pPager;
3557 int rc;
3559 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
3560 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
3561 assert( sqlite3_mutex_held(pBt->mutex) );
3562 assert( pDbPage->pBt==pBt );
3564 /* Move page iDbPage from its current location to page number iFreePage */
3565 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
3566 iDbPage, iFreePage, iPtrPage, eType));
3567 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
3568 if( rc!=SQLITE_OK ){
3569 return rc;
3571 pDbPage->pgno = iFreePage;
3573 /* If pDbPage was a btree-page, then it may have child pages and/or cells
3574 ** that point to overflow pages. The pointer map entries for all these
3575 ** pages need to be changed.
3577 ** If pDbPage is an overflow page, then the first 4 bytes may store a
3578 ** pointer to a subsequent overflow page. If this is the case, then
3579 ** the pointer map needs to be updated for the subsequent overflow page.
3581 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
3582 rc = setChildPtrmaps(pDbPage);
3583 if( rc!=SQLITE_OK ){
3584 return rc;
3586 }else{
3587 Pgno nextOvfl = get4byte(pDbPage->aData);
3588 if( nextOvfl!=0 ){
3589 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
3590 if( rc!=SQLITE_OK ){
3591 return rc;
3596 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
3597 ** that it points at iFreePage. Also fix the pointer map entry for
3598 ** iPtrPage.
3600 if( eType!=PTRMAP_ROOTPAGE ){
3601 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
3602 if( rc!=SQLITE_OK ){
3603 return rc;
3605 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
3606 if( rc!=SQLITE_OK ){
3607 releasePage(pPtrPage);
3608 return rc;
3610 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
3611 releasePage(pPtrPage);
3612 if( rc==SQLITE_OK ){
3613 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
3616 return rc;
3619 /* Forward declaration required by incrVacuumStep(). */
3620 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
3623 ** Perform a single step of an incremental-vacuum. If successful, return
3624 ** SQLITE_OK. If there is no work to do (and therefore no point in
3625 ** calling this function again), return SQLITE_DONE. Or, if an error
3626 ** occurs, return some other error code.
3628 ** More specifically, this function attempts to re-organize the database so
3629 ** that the last page of the file currently in use is no longer in use.
3631 ** Parameter nFin is the number of pages that this database would contain
3632 ** were this function called until it returns SQLITE_DONE.
3634 ** If the bCommit parameter is non-zero, this function assumes that the
3635 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
3636 ** or an error. bCommit is passed true for an auto-vacuum-on-commit
3637 ** operation, or false for an incremental vacuum.
3639 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
3640 Pgno nFreeList; /* Number of pages still on the free-list */
3641 int rc;
3643 assert( sqlite3_mutex_held(pBt->mutex) );
3644 assert( iLastPg>nFin );
3646 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
3647 u8 eType;
3648 Pgno iPtrPage;
3650 nFreeList = get4byte(&pBt->pPage1->aData[36]);
3651 if( nFreeList==0 ){
3652 return SQLITE_DONE;
3655 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
3656 if( rc!=SQLITE_OK ){
3657 return rc;
3659 if( eType==PTRMAP_ROOTPAGE ){
3660 return SQLITE_CORRUPT_BKPT;
3663 if( eType==PTRMAP_FREEPAGE ){
3664 if( bCommit==0 ){
3665 /* Remove the page from the files free-list. This is not required
3666 ** if bCommit is non-zero. In that case, the free-list will be
3667 ** truncated to zero after this function returns, so it doesn't
3668 ** matter if it still contains some garbage entries.
3670 Pgno iFreePg;
3671 MemPage *pFreePg;
3672 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
3673 if( rc!=SQLITE_OK ){
3674 return rc;
3676 assert( iFreePg==iLastPg );
3677 releasePage(pFreePg);
3679 } else {
3680 Pgno iFreePg; /* Index of free page to move pLastPg to */
3681 MemPage *pLastPg;
3682 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
3683 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
3685 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
3686 if( rc!=SQLITE_OK ){
3687 return rc;
3690 /* If bCommit is zero, this loop runs exactly once and page pLastPg
3691 ** is swapped with the first free page pulled off the free list.
3693 ** On the other hand, if bCommit is greater than zero, then keep
3694 ** looping until a free-page located within the first nFin pages
3695 ** of the file is found.
3697 if( bCommit==0 ){
3698 eMode = BTALLOC_LE;
3699 iNear = nFin;
3701 do {
3702 MemPage *pFreePg;
3703 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
3704 if( rc!=SQLITE_OK ){
3705 releasePage(pLastPg);
3706 return rc;
3708 releasePage(pFreePg);
3709 }while( bCommit && iFreePg>nFin );
3710 assert( iFreePg<iLastPg );
3712 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
3713 releasePage(pLastPg);
3714 if( rc!=SQLITE_OK ){
3715 return rc;
3720 if( bCommit==0 ){
3721 do {
3722 iLastPg--;
3723 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
3724 pBt->bDoTruncate = 1;
3725 pBt->nPage = iLastPg;
3727 return SQLITE_OK;
3731 ** The database opened by the first argument is an auto-vacuum database
3732 ** nOrig pages in size containing nFree free pages. Return the expected
3733 ** size of the database in pages following an auto-vacuum operation.
3735 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
3736 int nEntry; /* Number of entries on one ptrmap page */
3737 Pgno nPtrmap; /* Number of PtrMap pages to be freed */
3738 Pgno nFin; /* Return value */
3740 nEntry = pBt->usableSize/5;
3741 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
3742 nFin = nOrig - nFree - nPtrmap;
3743 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
3744 nFin--;
3746 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
3747 nFin--;
3750 return nFin;
3754 ** A write-transaction must be opened before calling this function.
3755 ** It performs a single unit of work towards an incremental vacuum.
3757 ** If the incremental vacuum is finished after this function has run,
3758 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
3759 ** SQLITE_OK is returned. Otherwise an SQLite error code.
3761 int sqlite3BtreeIncrVacuum(Btree *p){
3762 int rc;
3763 BtShared *pBt = p->pBt;
3765 sqlite3BtreeEnter(p);
3766 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
3767 if( !pBt->autoVacuum ){
3768 rc = SQLITE_DONE;
3769 }else{
3770 Pgno nOrig = btreePagecount(pBt);
3771 Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
3772 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
3774 if( nOrig<nFin ){
3775 rc = SQLITE_CORRUPT_BKPT;
3776 }else if( nFree>0 ){
3777 rc = saveAllCursors(pBt, 0, 0);
3778 if( rc==SQLITE_OK ){
3779 invalidateAllOverflowCache(pBt);
3780 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
3782 if( rc==SQLITE_OK ){
3783 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3784 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
3786 }else{
3787 rc = SQLITE_DONE;
3790 sqlite3BtreeLeave(p);
3791 return rc;
3795 ** This routine is called prior to sqlite3PagerCommit when a transaction
3796 ** is committed for an auto-vacuum database.
3798 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
3799 ** the database file should be truncated to during the commit process.
3800 ** i.e. the database has been reorganized so that only the first *pnTrunc
3801 ** pages are in use.
3803 static int autoVacuumCommit(BtShared *pBt){
3804 int rc = SQLITE_OK;
3805 Pager *pPager = pBt->pPager;
3806 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); )
3808 assert( sqlite3_mutex_held(pBt->mutex) );
3809 invalidateAllOverflowCache(pBt);
3810 assert(pBt->autoVacuum);
3811 if( !pBt->incrVacuum ){
3812 Pgno nFin; /* Number of pages in database after autovacuuming */
3813 Pgno nFree; /* Number of pages on the freelist initially */
3814 Pgno iFree; /* The next page to be freed */
3815 Pgno nOrig; /* Database size before freeing */
3817 nOrig = btreePagecount(pBt);
3818 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
3819 /* It is not possible to create a database for which the final page
3820 ** is either a pointer-map page or the pending-byte page. If one
3821 ** is encountered, this indicates corruption.
3823 return SQLITE_CORRUPT_BKPT;
3826 nFree = get4byte(&pBt->pPage1->aData[36]);
3827 nFin = finalDbSize(pBt, nOrig, nFree);
3828 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
3829 if( nFin<nOrig ){
3830 rc = saveAllCursors(pBt, 0, 0);
3832 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
3833 rc = incrVacuumStep(pBt, nFin, iFree, 1);
3835 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
3836 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
3837 put4byte(&pBt->pPage1->aData[32], 0);
3838 put4byte(&pBt->pPage1->aData[36], 0);
3839 put4byte(&pBt->pPage1->aData[28], nFin);
3840 pBt->bDoTruncate = 1;
3841 pBt->nPage = nFin;
3843 if( rc!=SQLITE_OK ){
3844 sqlite3PagerRollback(pPager);
3848 assert( nRef>=sqlite3PagerRefcount(pPager) );
3849 return rc;
3852 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
3853 # define setChildPtrmaps(x) SQLITE_OK
3854 #endif
3857 ** This routine does the first phase of a two-phase commit. This routine
3858 ** causes a rollback journal to be created (if it does not already exist)
3859 ** and populated with enough information so that if a power loss occurs
3860 ** the database can be restored to its original state by playing back
3861 ** the journal. Then the contents of the journal are flushed out to
3862 ** the disk. After the journal is safely on oxide, the changes to the
3863 ** database are written into the database file and flushed to oxide.
3864 ** At the end of this call, the rollback journal still exists on the
3865 ** disk and we are still holding all locks, so the transaction has not
3866 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the
3867 ** commit process.
3869 ** This call is a no-op if no write-transaction is currently active on pBt.
3871 ** Otherwise, sync the database file for the btree pBt. zMaster points to
3872 ** the name of a master journal file that should be written into the
3873 ** individual journal file, or is NULL, indicating no master journal file
3874 ** (single database transaction).
3876 ** When this is called, the master journal should already have been
3877 ** created, populated with this journal pointer and synced to disk.
3879 ** Once this is routine has returned, the only thing required to commit
3880 ** the write-transaction for this database file is to delete the journal.
3882 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
3883 int rc = SQLITE_OK;
3884 if( p->inTrans==TRANS_WRITE ){
3885 BtShared *pBt = p->pBt;
3886 sqlite3BtreeEnter(p);
3887 #ifndef SQLITE_OMIT_AUTOVACUUM
3888 if( pBt->autoVacuum ){
3889 rc = autoVacuumCommit(pBt);
3890 if( rc!=SQLITE_OK ){
3891 sqlite3BtreeLeave(p);
3892 return rc;
3895 if( pBt->bDoTruncate ){
3896 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
3898 #endif
3899 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
3900 sqlite3BtreeLeave(p);
3902 return rc;
3906 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
3907 ** at the conclusion of a transaction.
3909 static void btreeEndTransaction(Btree *p){
3910 BtShared *pBt = p->pBt;
3911 sqlite3 *db = p->db;
3912 assert( sqlite3BtreeHoldsMutex(p) );
3914 #ifndef SQLITE_OMIT_AUTOVACUUM
3915 pBt->bDoTruncate = 0;
3916 #endif
3917 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
3918 /* If there are other active statements that belong to this database
3919 ** handle, downgrade to a read-only transaction. The other statements
3920 ** may still be reading from the database. */
3921 downgradeAllSharedCacheTableLocks(p);
3922 p->inTrans = TRANS_READ;
3923 }else{
3924 /* If the handle had any kind of transaction open, decrement the
3925 ** transaction count of the shared btree. If the transaction count
3926 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
3927 ** call below will unlock the pager. */
3928 if( p->inTrans!=TRANS_NONE ){
3929 clearAllSharedCacheTableLocks(p);
3930 pBt->nTransaction--;
3931 if( 0==pBt->nTransaction ){
3932 pBt->inTransaction = TRANS_NONE;
3936 /* Set the current transaction state to TRANS_NONE and unlock the
3937 ** pager if this call closed the only read or write transaction. */
3938 p->inTrans = TRANS_NONE;
3939 unlockBtreeIfUnused(pBt);
3942 btreeIntegrity(p);
3946 ** Commit the transaction currently in progress.
3948 ** This routine implements the second phase of a 2-phase commit. The
3949 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
3950 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne()
3951 ** routine did all the work of writing information out to disk and flushing the
3952 ** contents so that they are written onto the disk platter. All this
3953 ** routine has to do is delete or truncate or zero the header in the
3954 ** the rollback journal (which causes the transaction to commit) and
3955 ** drop locks.
3957 ** Normally, if an error occurs while the pager layer is attempting to
3958 ** finalize the underlying journal file, this function returns an error and
3959 ** the upper layer will attempt a rollback. However, if the second argument
3960 ** is non-zero then this b-tree transaction is part of a multi-file
3961 ** transaction. In this case, the transaction has already been committed
3962 ** (by deleting a master journal file) and the caller will ignore this
3963 ** functions return code. So, even if an error occurs in the pager layer,
3964 ** reset the b-tree objects internal state to indicate that the write
3965 ** transaction has been closed. This is quite safe, as the pager will have
3966 ** transitioned to the error state.
3968 ** This will release the write lock on the database file. If there
3969 ** are no active cursors, it also releases the read lock.
3971 int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
3973 if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
3974 sqlite3BtreeEnter(p);
3975 btreeIntegrity(p);
3977 /* If the handle has a write-transaction open, commit the shared-btrees
3978 ** transaction and set the shared state to TRANS_READ.
3980 if( p->inTrans==TRANS_WRITE ){
3981 int rc;
3982 BtShared *pBt = p->pBt;
3983 assert( pBt->inTransaction==TRANS_WRITE );
3984 assert( pBt->nTransaction>0 );
3985 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
3986 if( rc!=SQLITE_OK && bCleanup==0 ){
3987 sqlite3BtreeLeave(p);
3988 return rc;
3990 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */
3991 pBt->inTransaction = TRANS_READ;
3992 btreeClearHasContent(pBt);
3995 btreeEndTransaction(p);
3996 sqlite3BtreeLeave(p);
3997 return SQLITE_OK;
4001 ** Do both phases of a commit.
4003 int sqlite3BtreeCommit(Btree *p){
4004 int rc;
4005 sqlite3BtreeEnter(p);
4006 rc = sqlite3BtreeCommitPhaseOne(p, 0);
4007 if( rc==SQLITE_OK ){
4008 rc = sqlite3BtreeCommitPhaseTwo(p, 0);
4010 sqlite3BtreeLeave(p);
4011 return rc;
4015 ** This routine sets the state to CURSOR_FAULT and the error
4016 ** code to errCode for every cursor on any BtShared that pBtree
4017 ** references. Or if the writeOnly flag is set to 1, then only
4018 ** trip write cursors and leave read cursors unchanged.
4020 ** Every cursor is a candidate to be tripped, including cursors
4021 ** that belong to other database connections that happen to be
4022 ** sharing the cache with pBtree.
4024 ** This routine gets called when a rollback occurs. If the writeOnly
4025 ** flag is true, then only write-cursors need be tripped - read-only
4026 ** cursors save their current positions so that they may continue
4027 ** following the rollback. Or, if writeOnly is false, all cursors are
4028 ** tripped. In general, writeOnly is false if the transaction being
4029 ** rolled back modified the database schema. In this case b-tree root
4030 ** pages may be moved or deleted from the database altogether, making
4031 ** it unsafe for read cursors to continue.
4033 ** If the writeOnly flag is true and an error is encountered while
4034 ** saving the current position of a read-only cursor, all cursors,
4035 ** including all read-cursors are tripped.
4037 ** SQLITE_OK is returned if successful, or if an error occurs while
4038 ** saving a cursor position, an SQLite error code.
4040 int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
4041 BtCursor *p;
4042 int rc = SQLITE_OK;
4044 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 );
4045 if( pBtree ){
4046 sqlite3BtreeEnter(pBtree);
4047 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
4048 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){
4049 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){
4050 rc = saveCursorPosition(p);
4051 if( rc!=SQLITE_OK ){
4052 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
4053 break;
4056 }else{
4057 sqlite3BtreeClearCursor(p);
4058 p->eState = CURSOR_FAULT;
4059 p->skipNext = errCode;
4061 btreeReleaseAllCursorPages(p);
4063 sqlite3BtreeLeave(pBtree);
4065 return rc;
4069 ** Rollback the transaction in progress.
4071 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
4072 ** Only write cursors are tripped if writeOnly is true but all cursors are
4073 ** tripped if writeOnly is false. Any attempt to use
4074 ** a tripped cursor will result in an error.
4076 ** This will release the write lock on the database file. If there
4077 ** are no active cursors, it also releases the read lock.
4079 int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
4080 int rc;
4081 BtShared *pBt = p->pBt;
4082 MemPage *pPage1;
4084 assert( writeOnly==1 || writeOnly==0 );
4085 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK );
4086 sqlite3BtreeEnter(p);
4087 if( tripCode==SQLITE_OK ){
4088 rc = tripCode = saveAllCursors(pBt, 0, 0);
4089 if( rc ) writeOnly = 0;
4090 }else{
4091 rc = SQLITE_OK;
4093 if( tripCode ){
4094 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
4095 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) );
4096 if( rc2!=SQLITE_OK ) rc = rc2;
4098 btreeIntegrity(p);
4100 if( p->inTrans==TRANS_WRITE ){
4101 int rc2;
4103 assert( TRANS_WRITE==pBt->inTransaction );
4104 rc2 = sqlite3PagerRollback(pBt->pPager);
4105 if( rc2!=SQLITE_OK ){
4106 rc = rc2;
4109 /* The rollback may have destroyed the pPage1->aData value. So
4110 ** call btreeGetPage() on page 1 again to make
4111 ** sure pPage1->aData is set correctly. */
4112 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
4113 int nPage = get4byte(28+(u8*)pPage1->aData);
4114 testcase( nPage==0 );
4115 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
4116 testcase( pBt->nPage!=nPage );
4117 pBt->nPage = nPage;
4118 releasePageOne(pPage1);
4120 assert( countValidCursors(pBt, 1)==0 );
4121 pBt->inTransaction = TRANS_READ;
4122 btreeClearHasContent(pBt);
4125 btreeEndTransaction(p);
4126 sqlite3BtreeLeave(p);
4127 return rc;
4131 ** Start a statement subtransaction. The subtransaction can be rolled
4132 ** back independently of the main transaction. You must start a transaction
4133 ** before starting a subtransaction. The subtransaction is ended automatically
4134 ** if the main transaction commits or rolls back.
4136 ** Statement subtransactions are used around individual SQL statements
4137 ** that are contained within a BEGIN...COMMIT block. If a constraint
4138 ** error occurs within the statement, the effect of that one statement
4139 ** can be rolled back without having to rollback the entire transaction.
4141 ** A statement sub-transaction is implemented as an anonymous savepoint. The
4142 ** value passed as the second parameter is the total number of savepoints,
4143 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
4144 ** are no active savepoints and no other statement-transactions open,
4145 ** iStatement is 1. This anonymous savepoint can be released or rolled back
4146 ** using the sqlite3BtreeSavepoint() function.
4148 int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
4149 int rc;
4150 BtShared *pBt = p->pBt;
4151 sqlite3BtreeEnter(p);
4152 assert( p->inTrans==TRANS_WRITE );
4153 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
4154 assert( iStatement>0 );
4155 assert( iStatement>p->db->nSavepoint );
4156 assert( pBt->inTransaction==TRANS_WRITE );
4157 /* At the pager level, a statement transaction is a savepoint with
4158 ** an index greater than all savepoints created explicitly using
4159 ** SQL statements. It is illegal to open, release or rollback any
4160 ** such savepoints while the statement transaction savepoint is active.
4162 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
4163 sqlite3BtreeLeave(p);
4164 return rc;
4168 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
4169 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
4170 ** savepoint identified by parameter iSavepoint, depending on the value
4171 ** of op.
4173 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
4174 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
4175 ** contents of the entire transaction are rolled back. This is different
4176 ** from a normal transaction rollback, as no locks are released and the
4177 ** transaction remains open.
4179 int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
4180 int rc = SQLITE_OK;
4181 if( p && p->inTrans==TRANS_WRITE ){
4182 BtShared *pBt = p->pBt;
4183 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
4184 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
4185 sqlite3BtreeEnter(p);
4186 if( op==SAVEPOINT_ROLLBACK ){
4187 rc = saveAllCursors(pBt, 0, 0);
4189 if( rc==SQLITE_OK ){
4190 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
4192 if( rc==SQLITE_OK ){
4193 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
4194 pBt->nPage = 0;
4196 rc = newDatabase(pBt);
4197 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
4199 /* The database size was written into the offset 28 of the header
4200 ** when the transaction started, so we know that the value at offset
4201 ** 28 is nonzero. */
4202 assert( pBt->nPage>0 );
4204 sqlite3BtreeLeave(p);
4206 return rc;
4210 ** Create a new cursor for the BTree whose root is on the page
4211 ** iTable. If a read-only cursor is requested, it is assumed that
4212 ** the caller already has at least a read-only transaction open
4213 ** on the database already. If a write-cursor is requested, then
4214 ** the caller is assumed to have an open write transaction.
4216 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
4217 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
4218 ** can be used for reading or for writing if other conditions for writing
4219 ** are also met. These are the conditions that must be met in order
4220 ** for writing to be allowed:
4222 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
4224 ** 2: Other database connections that share the same pager cache
4225 ** but which are not in the READ_UNCOMMITTED state may not have
4226 ** cursors open with wrFlag==0 on the same table. Otherwise
4227 ** the changes made by this write cursor would be visible to
4228 ** the read cursors in the other database connection.
4230 ** 3: The database must be writable (not on read-only media)
4232 ** 4: There must be an active transaction.
4234 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
4235 ** is set. If FORDELETE is set, that is a hint to the implementation that
4236 ** this cursor will only be used to seek to and delete entries of an index
4237 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
4238 ** this implementation. But in a hypothetical alternative storage engine
4239 ** in which index entries are automatically deleted when corresponding table
4240 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
4241 ** operations on this cursor can be no-ops and all READ operations can
4242 ** return a null row (2-bytes: 0x01 0x00).
4244 ** No checking is done to make sure that page iTable really is the
4245 ** root page of a b-tree. If it is not, then the cursor acquired
4246 ** will not work correctly.
4248 ** It is assumed that the sqlite3BtreeCursorZero() has been called
4249 ** on pCur to initialize the memory space prior to invoking this routine.
4251 static int btreeCursor(
4252 Btree *p, /* The btree */
4253 int iTable, /* Root page of table to open */
4254 int wrFlag, /* 1 to write. 0 read-only */
4255 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
4256 BtCursor *pCur /* Space for new cursor */
4258 BtShared *pBt = p->pBt; /* Shared b-tree handle */
4259 BtCursor *pX; /* Looping over other all cursors */
4261 assert( sqlite3BtreeHoldsMutex(p) );
4262 assert( wrFlag==0
4263 || wrFlag==BTREE_WRCSR
4264 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE)
4267 /* The following assert statements verify that if this is a sharable
4268 ** b-tree database, the connection is holding the required table locks,
4269 ** and that no other connection has any open cursor that conflicts with
4270 ** this lock. */
4271 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
4272 assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
4274 /* Assert that the caller has opened the required transaction. */
4275 assert( p->inTrans>TRANS_NONE );
4276 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
4277 assert( pBt->pPage1 && pBt->pPage1->aData );
4278 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 );
4280 if( wrFlag ){
4281 allocateTempSpace(pBt);
4282 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT;
4284 if( iTable==1 && btreePagecount(pBt)==0 ){
4285 assert( wrFlag==0 );
4286 iTable = 0;
4289 /* Now that no other errors can occur, finish filling in the BtCursor
4290 ** variables and link the cursor into the BtShared list. */
4291 pCur->pgnoRoot = (Pgno)iTable;
4292 pCur->iPage = -1;
4293 pCur->pKeyInfo = pKeyInfo;
4294 pCur->pBtree = p;
4295 pCur->pBt = pBt;
4296 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0;
4297 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY;
4298 /* If there are two or more cursors on the same btree, then all such
4299 ** cursors *must* have the BTCF_Multiple flag set. */
4300 for(pX=pBt->pCursor; pX; pX=pX->pNext){
4301 if( pX->pgnoRoot==(Pgno)iTable ){
4302 pX->curFlags |= BTCF_Multiple;
4303 pCur->curFlags |= BTCF_Multiple;
4306 pCur->pNext = pBt->pCursor;
4307 pBt->pCursor = pCur;
4308 pCur->eState = CURSOR_INVALID;
4309 return SQLITE_OK;
4311 int sqlite3BtreeCursor(
4312 Btree *p, /* The btree */
4313 int iTable, /* Root page of table to open */
4314 int wrFlag, /* 1 to write. 0 read-only */
4315 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
4316 BtCursor *pCur /* Write new cursor here */
4318 int rc;
4319 if( iTable<1 ){
4320 rc = SQLITE_CORRUPT_BKPT;
4321 }else{
4322 sqlite3BtreeEnter(p);
4323 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
4324 sqlite3BtreeLeave(p);
4326 return rc;
4330 ** Return the size of a BtCursor object in bytes.
4332 ** This interfaces is needed so that users of cursors can preallocate
4333 ** sufficient storage to hold a cursor. The BtCursor object is opaque
4334 ** to users so they cannot do the sizeof() themselves - they must call
4335 ** this routine.
4337 int sqlite3BtreeCursorSize(void){
4338 return ROUND8(sizeof(BtCursor));
4342 ** Initialize memory that will be converted into a BtCursor object.
4344 ** The simple approach here would be to memset() the entire object
4345 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays
4346 ** do not need to be zeroed and they are large, so we can save a lot
4347 ** of run-time by skipping the initialization of those elements.
4349 void sqlite3BtreeCursorZero(BtCursor *p){
4350 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT));
4354 ** Close a cursor. The read lock on the database file is released
4355 ** when the last cursor is closed.
4357 int sqlite3BtreeCloseCursor(BtCursor *pCur){
4358 Btree *pBtree = pCur->pBtree;
4359 if( pBtree ){
4360 BtShared *pBt = pCur->pBt;
4361 sqlite3BtreeEnter(pBtree);
4362 assert( pBt->pCursor!=0 );
4363 if( pBt->pCursor==pCur ){
4364 pBt->pCursor = pCur->pNext;
4365 }else{
4366 BtCursor *pPrev = pBt->pCursor;
4368 if( pPrev->pNext==pCur ){
4369 pPrev->pNext = pCur->pNext;
4370 break;
4372 pPrev = pPrev->pNext;
4373 }while( ALWAYS(pPrev) );
4375 btreeReleaseAllCursorPages(pCur);
4376 unlockBtreeIfUnused(pBt);
4377 sqlite3_free(pCur->aOverflow);
4378 sqlite3_free(pCur->pKey);
4379 sqlite3BtreeLeave(pBtree);
4381 return SQLITE_OK;
4385 ** Make sure the BtCursor* given in the argument has a valid
4386 ** BtCursor.info structure. If it is not already valid, call
4387 ** btreeParseCell() to fill it in.
4389 ** BtCursor.info is a cache of the information in the current cell.
4390 ** Using this cache reduces the number of calls to btreeParseCell().
4392 #ifndef NDEBUG
4393 static int cellInfoEqual(CellInfo *a, CellInfo *b){
4394 if( a->nKey!=b->nKey ) return 0;
4395 if( a->pPayload!=b->pPayload ) return 0;
4396 if( a->nPayload!=b->nPayload ) return 0;
4397 if( a->nLocal!=b->nLocal ) return 0;
4398 if( a->nSize!=b->nSize ) return 0;
4399 return 1;
4401 static void assertCellInfo(BtCursor *pCur){
4402 CellInfo info;
4403 memset(&info, 0, sizeof(info));
4404 btreeParseCell(pCur->pPage, pCur->ix, &info);
4405 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) );
4407 #else
4408 #define assertCellInfo(x)
4409 #endif
4410 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){
4411 if( pCur->info.nSize==0 ){
4412 pCur->curFlags |= BTCF_ValidNKey;
4413 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
4414 }else{
4415 assertCellInfo(pCur);
4419 #ifndef NDEBUG /* The next routine used only within assert() statements */
4421 ** Return true if the given BtCursor is valid. A valid cursor is one
4422 ** that is currently pointing to a row in a (non-empty) table.
4423 ** This is a verification routine is used only within assert() statements.
4425 int sqlite3BtreeCursorIsValid(BtCursor *pCur){
4426 return pCur && pCur->eState==CURSOR_VALID;
4428 #endif /* NDEBUG */
4429 int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
4430 assert( pCur!=0 );
4431 return pCur->eState==CURSOR_VALID;
4435 ** Return the value of the integer key or "rowid" for a table btree.
4436 ** This routine is only valid for a cursor that is pointing into a
4437 ** ordinary table btree. If the cursor points to an index btree or
4438 ** is invalid, the result of this routine is undefined.
4440 i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
4441 assert( cursorHoldsMutex(pCur) );
4442 assert( pCur->eState==CURSOR_VALID );
4443 assert( pCur->curIntKey );
4444 getCellInfo(pCur);
4445 return pCur->info.nKey;
4448 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
4450 ** Return the offset into the database file for the start of the
4451 ** payload to which the cursor is pointing.
4453 i64 sqlite3BtreeOffset(BtCursor *pCur){
4454 assert( cursorHoldsMutex(pCur) );
4455 assert( pCur->eState==CURSOR_VALID );
4456 getCellInfo(pCur);
4457 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) +
4458 (i64)(pCur->info.pPayload - pCur->pPage->aData);
4460 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */
4463 ** Return the number of bytes of payload for the entry that pCur is
4464 ** currently pointing to. For table btrees, this will be the amount
4465 ** of data. For index btrees, this will be the size of the key.
4467 ** The caller must guarantee that the cursor is pointing to a non-NULL
4468 ** valid entry. In other words, the calling procedure must guarantee
4469 ** that the cursor has Cursor.eState==CURSOR_VALID.
4471 u32 sqlite3BtreePayloadSize(BtCursor *pCur){
4472 assert( cursorHoldsMutex(pCur) );
4473 assert( pCur->eState==CURSOR_VALID );
4474 getCellInfo(pCur);
4475 return pCur->info.nPayload;
4479 ** Given the page number of an overflow page in the database (parameter
4480 ** ovfl), this function finds the page number of the next page in the
4481 ** linked list of overflow pages. If possible, it uses the auto-vacuum
4482 ** pointer-map data instead of reading the content of page ovfl to do so.
4484 ** If an error occurs an SQLite error code is returned. Otherwise:
4486 ** The page number of the next overflow page in the linked list is
4487 ** written to *pPgnoNext. If page ovfl is the last page in its linked
4488 ** list, *pPgnoNext is set to zero.
4490 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
4491 ** to page number pOvfl was obtained, then *ppPage is set to point to that
4492 ** reference. It is the responsibility of the caller to call releasePage()
4493 ** on *ppPage to free the reference. In no reference was obtained (because
4494 ** the pointer-map was used to obtain the value for *pPgnoNext), then
4495 ** *ppPage is set to zero.
4497 static int getOverflowPage(
4498 BtShared *pBt, /* The database file */
4499 Pgno ovfl, /* Current overflow page number */
4500 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */
4501 Pgno *pPgnoNext /* OUT: Next overflow page number */
4503 Pgno next = 0;
4504 MemPage *pPage = 0;
4505 int rc = SQLITE_OK;
4507 assert( sqlite3_mutex_held(pBt->mutex) );
4508 assert(pPgnoNext);
4510 #ifndef SQLITE_OMIT_AUTOVACUUM
4511 /* Try to find the next page in the overflow list using the
4512 ** autovacuum pointer-map pages. Guess that the next page in
4513 ** the overflow list is page number (ovfl+1). If that guess turns
4514 ** out to be wrong, fall back to loading the data of page
4515 ** number ovfl to determine the next page number.
4517 if( pBt->autoVacuum ){
4518 Pgno pgno;
4519 Pgno iGuess = ovfl+1;
4520 u8 eType;
4522 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
4523 iGuess++;
4526 if( iGuess<=btreePagecount(pBt) ){
4527 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
4528 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
4529 next = iGuess;
4530 rc = SQLITE_DONE;
4534 #endif
4536 assert( next==0 || rc==SQLITE_DONE );
4537 if( rc==SQLITE_OK ){
4538 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
4539 assert( rc==SQLITE_OK || pPage==0 );
4540 if( rc==SQLITE_OK ){
4541 next = get4byte(pPage->aData);
4545 *pPgnoNext = next;
4546 if( ppPage ){
4547 *ppPage = pPage;
4548 }else{
4549 releasePage(pPage);
4551 return (rc==SQLITE_DONE ? SQLITE_OK : rc);
4555 ** Copy data from a buffer to a page, or from a page to a buffer.
4557 ** pPayload is a pointer to data stored on database page pDbPage.
4558 ** If argument eOp is false, then nByte bytes of data are copied
4559 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
4560 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
4561 ** of data are copied from the buffer pBuf to pPayload.
4563 ** SQLITE_OK is returned on success, otherwise an error code.
4565 static int copyPayload(
4566 void *pPayload, /* Pointer to page data */
4567 void *pBuf, /* Pointer to buffer */
4568 int nByte, /* Number of bytes to copy */
4569 int eOp, /* 0 -> copy from page, 1 -> copy to page */
4570 DbPage *pDbPage /* Page containing pPayload */
4572 if( eOp ){
4573 /* Copy data from buffer to page (a write operation) */
4574 int rc = sqlite3PagerWrite(pDbPage);
4575 if( rc!=SQLITE_OK ){
4576 return rc;
4578 memcpy(pPayload, pBuf, nByte);
4579 }else{
4580 /* Copy data from page to buffer (a read operation) */
4581 memcpy(pBuf, pPayload, nByte);
4583 return SQLITE_OK;
4587 ** This function is used to read or overwrite payload information
4588 ** for the entry that the pCur cursor is pointing to. The eOp
4589 ** argument is interpreted as follows:
4591 ** 0: The operation is a read. Populate the overflow cache.
4592 ** 1: The operation is a write. Populate the overflow cache.
4594 ** A total of "amt" bytes are read or written beginning at "offset".
4595 ** Data is read to or from the buffer pBuf.
4597 ** The content being read or written might appear on the main page
4598 ** or be scattered out on multiple overflow pages.
4600 ** If the current cursor entry uses one or more overflow pages
4601 ** this function may allocate space for and lazily populate
4602 ** the overflow page-list cache array (BtCursor.aOverflow).
4603 ** Subsequent calls use this cache to make seeking to the supplied offset
4604 ** more efficient.
4606 ** Once an overflow page-list cache has been allocated, it must be
4607 ** invalidated if some other cursor writes to the same table, or if
4608 ** the cursor is moved to a different row. Additionally, in auto-vacuum
4609 ** mode, the following events may invalidate an overflow page-list cache.
4611 ** * An incremental vacuum,
4612 ** * A commit in auto_vacuum="full" mode,
4613 ** * Creating a table (may require moving an overflow page).
4615 static int accessPayload(
4616 BtCursor *pCur, /* Cursor pointing to entry to read from */
4617 u32 offset, /* Begin reading this far into payload */
4618 u32 amt, /* Read this many bytes */
4619 unsigned char *pBuf, /* Write the bytes into this buffer */
4620 int eOp /* zero to read. non-zero to write. */
4622 unsigned char *aPayload;
4623 int rc = SQLITE_OK;
4624 int iIdx = 0;
4625 MemPage *pPage = pCur->pPage; /* Btree page of current entry */
4626 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
4627 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4628 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */
4629 #endif
4631 assert( pPage );
4632 assert( eOp==0 || eOp==1 );
4633 assert( pCur->eState==CURSOR_VALID );
4634 assert( pCur->ix<pPage->nCell );
4635 assert( cursorHoldsMutex(pCur) );
4637 getCellInfo(pCur);
4638 aPayload = pCur->info.pPayload;
4639 assert( offset+amt <= pCur->info.nPayload );
4641 assert( aPayload > pPage->aData );
4642 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
4643 /* Trying to read or write past the end of the data is an error. The
4644 ** conditional above is really:
4645 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
4646 ** but is recast into its current form to avoid integer overflow problems
4648 return SQLITE_CORRUPT_PAGE(pPage);
4651 /* Check if data must be read/written to/from the btree page itself. */
4652 if( offset<pCur->info.nLocal ){
4653 int a = amt;
4654 if( a+offset>pCur->info.nLocal ){
4655 a = pCur->info.nLocal - offset;
4657 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
4658 offset = 0;
4659 pBuf += a;
4660 amt -= a;
4661 }else{
4662 offset -= pCur->info.nLocal;
4666 if( rc==SQLITE_OK && amt>0 ){
4667 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
4668 Pgno nextPage;
4670 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
4672 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now.
4674 ** The aOverflow[] array is sized at one entry for each overflow page
4675 ** in the overflow chain. The page number of the first overflow page is
4676 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array
4677 ** means "not yet known" (the cache is lazily populated).
4679 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){
4680 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
4681 if( pCur->aOverflow==0
4682 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
4684 Pgno *aNew = (Pgno*)sqlite3Realloc(
4685 pCur->aOverflow, nOvfl*2*sizeof(Pgno)
4687 if( aNew==0 ){
4688 return SQLITE_NOMEM_BKPT;
4689 }else{
4690 pCur->aOverflow = aNew;
4693 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
4694 pCur->curFlags |= BTCF_ValidOvfl;
4695 }else{
4696 /* If the overflow page-list cache has been allocated and the
4697 ** entry for the first required overflow page is valid, skip
4698 ** directly to it.
4700 if( pCur->aOverflow[offset/ovflSize] ){
4701 iIdx = (offset/ovflSize);
4702 nextPage = pCur->aOverflow[iIdx];
4703 offset = (offset%ovflSize);
4707 assert( rc==SQLITE_OK && amt>0 );
4708 while( nextPage ){
4709 /* If required, populate the overflow page-list cache. */
4710 assert( pCur->aOverflow[iIdx]==0
4711 || pCur->aOverflow[iIdx]==nextPage
4712 || CORRUPT_DB );
4713 pCur->aOverflow[iIdx] = nextPage;
4715 if( offset>=ovflSize ){
4716 /* The only reason to read this page is to obtain the page
4717 ** number for the next page in the overflow chain. The page
4718 ** data is not required. So first try to lookup the overflow
4719 ** page-list cache, if any, then fall back to the getOverflowPage()
4720 ** function.
4722 assert( pCur->curFlags & BTCF_ValidOvfl );
4723 assert( pCur->pBtree->db==pBt->db );
4724 if( pCur->aOverflow[iIdx+1] ){
4725 nextPage = pCur->aOverflow[iIdx+1];
4726 }else{
4727 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
4729 offset -= ovflSize;
4730 }else{
4731 /* Need to read this page properly. It contains some of the
4732 ** range of data that is being read (eOp==0) or written (eOp!=0).
4734 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4735 sqlite3_file *fd; /* File from which to do direct overflow read */
4736 #endif
4737 int a = amt;
4738 if( a + offset > ovflSize ){
4739 a = ovflSize - offset;
4742 #ifdef SQLITE_DIRECT_OVERFLOW_READ
4743 /* If all the following are true:
4745 ** 1) this is a read operation, and
4746 ** 2) data is required from the start of this overflow page, and
4747 ** 3) there is no open write-transaction, and
4748 ** 4) the database is file-backed, and
4749 ** 5) the page is not in the WAL file
4750 ** 6) at least 4 bytes have already been read into the output buffer
4752 ** then data can be read directly from the database file into the
4753 ** output buffer, bypassing the page-cache altogether. This speeds
4754 ** up loading large records that span many overflow pages.
4756 if( eOp==0 /* (1) */
4757 && offset==0 /* (2) */
4758 && pBt->inTransaction==TRANS_READ /* (3) */
4759 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */
4760 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */
4761 && &pBuf[-4]>=pBufStart /* (6) */
4763 u8 aSave[4];
4764 u8 *aWrite = &pBuf[-4];
4765 assert( aWrite>=pBufStart ); /* due to (6) */
4766 memcpy(aSave, aWrite, 4);
4767 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
4768 nextPage = get4byte(aWrite);
4769 memcpy(aWrite, aSave, 4);
4770 }else
4771 #endif
4774 DbPage *pDbPage;
4775 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
4776 (eOp==0 ? PAGER_GET_READONLY : 0)
4778 if( rc==SQLITE_OK ){
4779 aPayload = sqlite3PagerGetData(pDbPage);
4780 nextPage = get4byte(aPayload);
4781 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
4782 sqlite3PagerUnref(pDbPage);
4783 offset = 0;
4786 amt -= a;
4787 if( amt==0 ) return rc;
4788 pBuf += a;
4790 if( rc ) break;
4791 iIdx++;
4795 if( rc==SQLITE_OK && amt>0 ){
4796 /* Overflow chain ends prematurely */
4797 return SQLITE_CORRUPT_PAGE(pPage);
4799 return rc;
4803 ** Read part of the payload for the row at which that cursor pCur is currently
4804 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer
4805 ** begins at "offset".
4807 ** pCur can be pointing to either a table or an index b-tree.
4808 ** If pointing to a table btree, then the content section is read. If
4809 ** pCur is pointing to an index b-tree then the key section is read.
4811 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing
4812 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the
4813 ** cursor might be invalid or might need to be restored before being read.
4815 ** Return SQLITE_OK on success or an error code if anything goes
4816 ** wrong. An error is returned if "offset+amt" is larger than
4817 ** the available payload.
4819 int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
4820 assert( cursorHoldsMutex(pCur) );
4821 assert( pCur->eState==CURSOR_VALID );
4822 assert( pCur->iPage>=0 && pCur->pPage );
4823 assert( pCur->ix<pCur->pPage->nCell );
4824 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
4828 ** This variant of sqlite3BtreePayload() works even if the cursor has not
4829 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read()
4830 ** interface.
4832 #ifndef SQLITE_OMIT_INCRBLOB
4833 static SQLITE_NOINLINE int accessPayloadChecked(
4834 BtCursor *pCur,
4835 u32 offset,
4836 u32 amt,
4837 void *pBuf
4839 int rc;
4840 if ( pCur->eState==CURSOR_INVALID ){
4841 return SQLITE_ABORT;
4843 assert( cursorOwnsBtShared(pCur) );
4844 rc = btreeRestoreCursorPosition(pCur);
4845 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
4847 int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
4848 if( pCur->eState==CURSOR_VALID ){
4849 assert( cursorOwnsBtShared(pCur) );
4850 return accessPayload(pCur, offset, amt, pBuf, 0);
4851 }else{
4852 return accessPayloadChecked(pCur, offset, amt, pBuf);
4855 #endif /* SQLITE_OMIT_INCRBLOB */
4858 ** Return a pointer to payload information from the entry that the
4859 ** pCur cursor is pointing to. The pointer is to the beginning of
4860 ** the key if index btrees (pPage->intKey==0) and is the data for
4861 ** table btrees (pPage->intKey==1). The number of bytes of available
4862 ** key/data is written into *pAmt. If *pAmt==0, then the value
4863 ** returned will not be a valid pointer.
4865 ** This routine is an optimization. It is common for the entire key
4866 ** and data to fit on the local page and for there to be no overflow
4867 ** pages. When that is so, this routine can be used to access the
4868 ** key and data without making a copy. If the key and/or data spills
4869 ** onto overflow pages, then accessPayload() must be used to reassemble
4870 ** the key/data and copy it into a preallocated buffer.
4872 ** The pointer returned by this routine looks directly into the cached
4873 ** page of the database. The data might change or move the next time
4874 ** any btree routine is called.
4876 static const void *fetchPayload(
4877 BtCursor *pCur, /* Cursor pointing to entry to read from */
4878 u32 *pAmt /* Write the number of available bytes here */
4880 int amt;
4881 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
4882 assert( pCur->eState==CURSOR_VALID );
4883 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
4884 assert( cursorOwnsBtShared(pCur) );
4885 assert( pCur->ix<pCur->pPage->nCell );
4886 assert( pCur->info.nSize>0 );
4887 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB );
4888 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB);
4889 amt = pCur->info.nLocal;
4890 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
4891 /* There is too little space on the page for the expected amount
4892 ** of local content. Database must be corrupt. */
4893 assert( CORRUPT_DB );
4894 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload));
4896 *pAmt = (u32)amt;
4897 return (void*)pCur->info.pPayload;
4902 ** For the entry that cursor pCur is point to, return as
4903 ** many bytes of the key or data as are available on the local
4904 ** b-tree page. Write the number of available bytes into *pAmt.
4906 ** The pointer returned is ephemeral. The key/data may move
4907 ** or be destroyed on the next call to any Btree routine,
4908 ** including calls from other threads against the same cache.
4909 ** Hence, a mutex on the BtShared should be held prior to calling
4910 ** this routine.
4912 ** These routines is used to get quick access to key and data
4913 ** in the common case where no overflow pages are used.
4915 const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
4916 return fetchPayload(pCur, pAmt);
4921 ** Move the cursor down to a new child page. The newPgno argument is the
4922 ** page number of the child page to move to.
4924 ** This function returns SQLITE_CORRUPT if the page-header flags field of
4925 ** the new child page does not match the flags field of the parent (i.e.
4926 ** if an intkey page appears to be the parent of a non-intkey page, or
4927 ** vice-versa).
4929 static int moveToChild(BtCursor *pCur, u32 newPgno){
4930 BtShared *pBt = pCur->pBt;
4932 assert( cursorOwnsBtShared(pCur) );
4933 assert( pCur->eState==CURSOR_VALID );
4934 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
4935 assert( pCur->iPage>=0 );
4936 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
4937 return SQLITE_CORRUPT_BKPT;
4939 pCur->info.nSize = 0;
4940 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
4941 pCur->aiIdx[pCur->iPage] = pCur->ix;
4942 pCur->apPage[pCur->iPage] = pCur->pPage;
4943 pCur->ix = 0;
4944 pCur->iPage++;
4945 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
4948 #ifdef SQLITE_DEBUG
4950 ** Page pParent is an internal (non-leaf) tree page. This function
4951 ** asserts that page number iChild is the left-child if the iIdx'th
4952 ** cell in page pParent. Or, if iIdx is equal to the total number of
4953 ** cells in pParent, that page number iChild is the right-child of
4954 ** the page.
4956 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
4957 if( CORRUPT_DB ) return; /* The conditions tested below might not be true
4958 ** in a corrupt database */
4959 assert( iIdx<=pParent->nCell );
4960 if( iIdx==pParent->nCell ){
4961 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
4962 }else{
4963 assert( get4byte(findCell(pParent, iIdx))==iChild );
4966 #else
4967 # define assertParentIndex(x,y,z)
4968 #endif
4971 ** Move the cursor up to the parent page.
4973 ** pCur->idx is set to the cell index that contains the pointer
4974 ** to the page we are coming from. If we are coming from the
4975 ** right-most child page then pCur->idx is set to one more than
4976 ** the largest cell index.
4978 static void moveToParent(BtCursor *pCur){
4979 MemPage *pLeaf;
4980 assert( cursorOwnsBtShared(pCur) );
4981 assert( pCur->eState==CURSOR_VALID );
4982 assert( pCur->iPage>0 );
4983 assert( pCur->pPage );
4984 assertParentIndex(
4985 pCur->apPage[pCur->iPage-1],
4986 pCur->aiIdx[pCur->iPage-1],
4987 pCur->pPage->pgno
4989 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
4990 pCur->info.nSize = 0;
4991 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
4992 pCur->ix = pCur->aiIdx[pCur->iPage-1];
4993 pLeaf = pCur->pPage;
4994 pCur->pPage = pCur->apPage[--pCur->iPage];
4995 releasePageNotNull(pLeaf);
4999 ** Move the cursor to point to the root page of its b-tree structure.
5001 ** If the table has a virtual root page, then the cursor is moved to point
5002 ** to the virtual root page instead of the actual root page. A table has a
5003 ** virtual root page when the actual root page contains no cells and a
5004 ** single child page. This can only happen with the table rooted at page 1.
5006 ** If the b-tree structure is empty, the cursor state is set to
5007 ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise,
5008 ** the cursor is set to point to the first cell located on the root
5009 ** (or virtual root) page and the cursor state is set to CURSOR_VALID.
5011 ** If this function returns successfully, it may be assumed that the
5012 ** page-header flags indicate that the [virtual] root-page is the expected
5013 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
5014 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
5015 ** indicating a table b-tree, or if the caller did specify a KeyInfo
5016 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
5017 ** b-tree).
5019 static int moveToRoot(BtCursor *pCur){
5020 MemPage *pRoot;
5021 int rc = SQLITE_OK;
5023 assert( cursorOwnsBtShared(pCur) );
5024 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
5025 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
5026 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
5027 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 );
5028 assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
5030 if( pCur->iPage>=0 ){
5031 if( pCur->iPage ){
5032 releasePageNotNull(pCur->pPage);
5033 while( --pCur->iPage ){
5034 releasePageNotNull(pCur->apPage[pCur->iPage]);
5036 pCur->pPage = pCur->apPage[0];
5037 goto skip_init;
5039 }else if( pCur->pgnoRoot==0 ){
5040 pCur->eState = CURSOR_INVALID;
5041 return SQLITE_EMPTY;
5042 }else{
5043 assert( pCur->iPage==(-1) );
5044 if( pCur->eState>=CURSOR_REQUIRESEEK ){
5045 if( pCur->eState==CURSOR_FAULT ){
5046 assert( pCur->skipNext!=SQLITE_OK );
5047 return pCur->skipNext;
5049 sqlite3BtreeClearCursor(pCur);
5051 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
5052 0, pCur->curPagerFlags);
5053 if( rc!=SQLITE_OK ){
5054 pCur->eState = CURSOR_INVALID;
5055 return rc;
5057 pCur->iPage = 0;
5058 pCur->curIntKey = pCur->pPage->intKey;
5060 pRoot = pCur->pPage;
5061 assert( pRoot->pgno==pCur->pgnoRoot );
5063 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
5064 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
5065 ** NULL, the caller expects a table b-tree. If this is not the case,
5066 ** return an SQLITE_CORRUPT error.
5068 ** Earlier versions of SQLite assumed that this test could not fail
5069 ** if the root page was already loaded when this function was called (i.e.
5070 ** if pCur->iPage>=0). But this is not so if the database is corrupted
5071 ** in such a way that page pRoot is linked into a second b-tree table
5072 ** (or the freelist). */
5073 assert( pRoot->intKey==1 || pRoot->intKey==0 );
5074 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
5075 return SQLITE_CORRUPT_PAGE(pCur->pPage);
5078 skip_init:
5079 pCur->ix = 0;
5080 pCur->info.nSize = 0;
5081 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
5083 pRoot = pCur->pPage;
5084 if( pRoot->nCell>0 ){
5085 pCur->eState = CURSOR_VALID;
5086 }else if( !pRoot->leaf ){
5087 Pgno subpage;
5088 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
5089 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
5090 pCur->eState = CURSOR_VALID;
5091 rc = moveToChild(pCur, subpage);
5092 }else{
5093 pCur->eState = CURSOR_INVALID;
5094 rc = SQLITE_EMPTY;
5096 return rc;
5100 ** Move the cursor down to the left-most leaf entry beneath the
5101 ** entry to which it is currently pointing.
5103 ** The left-most leaf is the one with the smallest key - the first
5104 ** in ascending order.
5106 static int moveToLeftmost(BtCursor *pCur){
5107 Pgno pgno;
5108 int rc = SQLITE_OK;
5109 MemPage *pPage;
5111 assert( cursorOwnsBtShared(pCur) );
5112 assert( pCur->eState==CURSOR_VALID );
5113 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
5114 assert( pCur->ix<pPage->nCell );
5115 pgno = get4byte(findCell(pPage, pCur->ix));
5116 rc = moveToChild(pCur, pgno);
5118 return rc;
5122 ** Move the cursor down to the right-most leaf entry beneath the
5123 ** page to which it is currently pointing. Notice the difference
5124 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
5125 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
5126 ** finds the right-most entry beneath the *page*.
5128 ** The right-most entry is the one with the largest key - the last
5129 ** key in ascending order.
5131 static int moveToRightmost(BtCursor *pCur){
5132 Pgno pgno;
5133 int rc = SQLITE_OK;
5134 MemPage *pPage = 0;
5136 assert( cursorOwnsBtShared(pCur) );
5137 assert( pCur->eState==CURSOR_VALID );
5138 while( !(pPage = pCur->pPage)->leaf ){
5139 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5140 pCur->ix = pPage->nCell;
5141 rc = moveToChild(pCur, pgno);
5142 if( rc ) return rc;
5144 pCur->ix = pPage->nCell-1;
5145 assert( pCur->info.nSize==0 );
5146 assert( (pCur->curFlags & BTCF_ValidNKey)==0 );
5147 return SQLITE_OK;
5150 /* Move the cursor to the first entry in the table. Return SQLITE_OK
5151 ** on success. Set *pRes to 0 if the cursor actually points to something
5152 ** or set *pRes to 1 if the table is empty.
5154 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
5155 int rc;
5157 assert( cursorOwnsBtShared(pCur) );
5158 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5159 rc = moveToRoot(pCur);
5160 if( rc==SQLITE_OK ){
5161 assert( pCur->pPage->nCell>0 );
5162 *pRes = 0;
5163 rc = moveToLeftmost(pCur);
5164 }else if( rc==SQLITE_EMPTY ){
5165 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5166 *pRes = 1;
5167 rc = SQLITE_OK;
5169 return rc;
5172 /* Move the cursor to the last entry in the table. Return SQLITE_OK
5173 ** on success. Set *pRes to 0 if the cursor actually points to something
5174 ** or set *pRes to 1 if the table is empty.
5176 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
5177 int rc;
5179 assert( cursorOwnsBtShared(pCur) );
5180 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5182 /* If the cursor already points to the last entry, this is a no-op. */
5183 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){
5184 #ifdef SQLITE_DEBUG
5185 /* This block serves to assert() that the cursor really does point
5186 ** to the last entry in the b-tree. */
5187 int ii;
5188 for(ii=0; ii<pCur->iPage; ii++){
5189 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
5191 assert( pCur->ix==pCur->pPage->nCell-1 );
5192 assert( pCur->pPage->leaf );
5193 #endif
5194 return SQLITE_OK;
5197 rc = moveToRoot(pCur);
5198 if( rc==SQLITE_OK ){
5199 assert( pCur->eState==CURSOR_VALID );
5200 *pRes = 0;
5201 rc = moveToRightmost(pCur);
5202 if( rc==SQLITE_OK ){
5203 pCur->curFlags |= BTCF_AtLast;
5204 }else{
5205 pCur->curFlags &= ~BTCF_AtLast;
5207 }else if( rc==SQLITE_EMPTY ){
5208 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5209 *pRes = 1;
5210 rc = SQLITE_OK;
5212 return rc;
5215 /* Move the cursor so that it points to an entry near the key
5216 ** specified by pIdxKey or intKey. Return a success code.
5218 ** For INTKEY tables, the intKey parameter is used. pIdxKey
5219 ** must be NULL. For index tables, pIdxKey is used and intKey
5220 ** is ignored.
5222 ** If an exact match is not found, then the cursor is always
5223 ** left pointing at a leaf page which would hold the entry if it
5224 ** were present. The cursor might point to an entry that comes
5225 ** before or after the key.
5227 ** An integer is written into *pRes which is the result of
5228 ** comparing the key with the entry to which the cursor is
5229 ** pointing. The meaning of the integer written into
5230 ** *pRes is as follows:
5232 ** *pRes<0 The cursor is left pointing at an entry that
5233 ** is smaller than intKey/pIdxKey or if the table is empty
5234 ** and the cursor is therefore left point to nothing.
5236 ** *pRes==0 The cursor is left pointing at an entry that
5237 ** exactly matches intKey/pIdxKey.
5239 ** *pRes>0 The cursor is left pointing at an entry that
5240 ** is larger than intKey/pIdxKey.
5242 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there
5243 ** exists an entry in the table that exactly matches pIdxKey.
5245 int sqlite3BtreeMovetoUnpacked(
5246 BtCursor *pCur, /* The cursor to be moved */
5247 UnpackedRecord *pIdxKey, /* Unpacked index key */
5248 i64 intKey, /* The table key */
5249 int biasRight, /* If true, bias the search to the high end */
5250 int *pRes /* Write search results here */
5252 int rc;
5253 RecordCompare xRecordCompare;
5255 assert( cursorOwnsBtShared(pCur) );
5256 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5257 assert( pRes );
5258 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
5259 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );
5261 /* If the cursor is already positioned at the point we are trying
5262 ** to move to, then just return without doing any work */
5263 if( pIdxKey==0
5264 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
5266 if( pCur->info.nKey==intKey ){
5267 *pRes = 0;
5268 return SQLITE_OK;
5270 if( pCur->info.nKey<intKey ){
5271 if( (pCur->curFlags & BTCF_AtLast)!=0 ){
5272 *pRes = -1;
5273 return SQLITE_OK;
5275 /* If the requested key is one more than the previous key, then
5276 ** try to get there using sqlite3BtreeNext() rather than a full
5277 ** binary search. This is an optimization only. The correct answer
5278 ** is still obtained without this case, only a little more slowely */
5279 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){
5280 *pRes = 0;
5281 rc = sqlite3BtreeNext(pCur, 0);
5282 if( rc==SQLITE_OK ){
5283 getCellInfo(pCur);
5284 if( pCur->info.nKey==intKey ){
5285 return SQLITE_OK;
5287 }else if( rc==SQLITE_DONE ){
5288 rc = SQLITE_OK;
5289 }else{
5290 return rc;
5296 if( pIdxKey ){
5297 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
5298 pIdxKey->errCode = 0;
5299 assert( pIdxKey->default_rc==1
5300 || pIdxKey->default_rc==0
5301 || pIdxKey->default_rc==-1
5303 }else{
5304 xRecordCompare = 0; /* All keys are integers */
5307 rc = moveToRoot(pCur);
5308 if( rc ){
5309 if( rc==SQLITE_EMPTY ){
5310 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
5311 *pRes = -1;
5312 return SQLITE_OK;
5314 return rc;
5316 assert( pCur->pPage );
5317 assert( pCur->pPage->isInit );
5318 assert( pCur->eState==CURSOR_VALID );
5319 assert( pCur->pPage->nCell > 0 );
5320 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
5321 assert( pCur->curIntKey || pIdxKey );
5322 for(;;){
5323 int lwr, upr, idx, c;
5324 Pgno chldPg;
5325 MemPage *pPage = pCur->pPage;
5326 u8 *pCell; /* Pointer to current cell in pPage */
5328 /* pPage->nCell must be greater than zero. If this is the root-page
5329 ** the cursor would have been INVALID above and this for(;;) loop
5330 ** not run. If this is not the root-page, then the moveToChild() routine
5331 ** would have already detected db corruption. Similarly, pPage must
5332 ** be the right kind (index or table) of b-tree page. Otherwise
5333 ** a moveToChild() or moveToRoot() call would have detected corruption. */
5334 assert( pPage->nCell>0 );
5335 assert( pPage->intKey==(pIdxKey==0) );
5336 lwr = 0;
5337 upr = pPage->nCell-1;
5338 assert( biasRight==0 || biasRight==1 );
5339 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
5340 pCur->ix = (u16)idx;
5341 if( xRecordCompare==0 ){
5342 for(;;){
5343 i64 nCellKey;
5344 pCell = findCellPastPtr(pPage, idx);
5345 if( pPage->intKeyLeaf ){
5346 while( 0x80 <= *(pCell++) ){
5347 if( pCell>=pPage->aDataEnd ){
5348 return SQLITE_CORRUPT_PAGE(pPage);
5352 getVarint(pCell, (u64*)&nCellKey);
5353 if( nCellKey<intKey ){
5354 lwr = idx+1;
5355 if( lwr>upr ){ c = -1; break; }
5356 }else if( nCellKey>intKey ){
5357 upr = idx-1;
5358 if( lwr>upr ){ c = +1; break; }
5359 }else{
5360 assert( nCellKey==intKey );
5361 pCur->ix = (u16)idx;
5362 if( !pPage->leaf ){
5363 lwr = idx;
5364 goto moveto_next_layer;
5365 }else{
5366 pCur->curFlags |= BTCF_ValidNKey;
5367 pCur->info.nKey = nCellKey;
5368 pCur->info.nSize = 0;
5369 *pRes = 0;
5370 return SQLITE_OK;
5373 assert( lwr+upr>=0 );
5374 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */
5376 }else{
5377 for(;;){
5378 int nCell; /* Size of the pCell cell in bytes */
5379 pCell = findCellPastPtr(pPage, idx);
5381 /* The maximum supported page-size is 65536 bytes. This means that
5382 ** the maximum number of record bytes stored on an index B-Tree
5383 ** page is less than 16384 bytes and may be stored as a 2-byte
5384 ** varint. This information is used to attempt to avoid parsing
5385 ** the entire cell by checking for the cases where the record is
5386 ** stored entirely within the b-tree page by inspecting the first
5387 ** 2 bytes of the cell.
5389 nCell = pCell[0];
5390 if( nCell<=pPage->max1bytePayload ){
5391 /* This branch runs if the record-size field of the cell is a
5392 ** single byte varint and the record fits entirely on the main
5393 ** b-tree page. */
5394 testcase( pCell+nCell+1==pPage->aDataEnd );
5395 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5396 }else if( !(pCell[1] & 0x80)
5397 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
5399 /* The record-size field is a 2 byte varint and the record
5400 ** fits entirely on the main b-tree page. */
5401 testcase( pCell+nCell+2==pPage->aDataEnd );
5402 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5403 }else{
5404 /* The record flows over onto one or more overflow pages. In
5405 ** this case the whole cell needs to be parsed, a buffer allocated
5406 ** and accessPayload() used to retrieve the record into the
5407 ** buffer before VdbeRecordCompare() can be called.
5409 ** If the record is corrupt, the xRecordCompare routine may read
5410 ** up to two varints past the end of the buffer. An extra 18
5411 ** bytes of padding is allocated at the end of the buffer in
5412 ** case this happens. */
5413 void *pCellKey;
5414 u8 * const pCellBody = pCell - pPage->childPtrSize;
5415 pPage->xParseCell(pPage, pCellBody, &pCur->info);
5416 nCell = (int)pCur->info.nKey;
5417 testcase( nCell<0 ); /* True if key size is 2^32 or more */
5418 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */
5419 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */
5420 testcase( nCell==2 ); /* Minimum legal index key size */
5421 if( nCell<2 ){
5422 rc = SQLITE_CORRUPT_PAGE(pPage);
5423 goto moveto_finish;
5425 pCellKey = sqlite3Malloc( nCell+18 );
5426 if( pCellKey==0 ){
5427 rc = SQLITE_NOMEM_BKPT;
5428 goto moveto_finish;
5430 pCur->ix = (u16)idx;
5431 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
5432 pCur->curFlags &= ~BTCF_ValidOvfl;
5433 if( rc ){
5434 sqlite3_free(pCellKey);
5435 goto moveto_finish;
5437 c = xRecordCompare(nCell, pCellKey, pIdxKey);
5438 sqlite3_free(pCellKey);
5440 assert(
5441 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
5442 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
5444 if( c<0 ){
5445 lwr = idx+1;
5446 }else if( c>0 ){
5447 upr = idx-1;
5448 }else{
5449 assert( c==0 );
5450 *pRes = 0;
5451 rc = SQLITE_OK;
5452 pCur->ix = (u16)idx;
5453 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
5454 goto moveto_finish;
5456 if( lwr>upr ) break;
5457 assert( lwr+upr>=0 );
5458 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */
5461 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
5462 assert( pPage->isInit );
5463 if( pPage->leaf ){
5464 assert( pCur->ix<pCur->pPage->nCell );
5465 pCur->ix = (u16)idx;
5466 *pRes = c;
5467 rc = SQLITE_OK;
5468 goto moveto_finish;
5470 moveto_next_layer:
5471 if( lwr>=pPage->nCell ){
5472 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5473 }else{
5474 chldPg = get4byte(findCell(pPage, lwr));
5476 pCur->ix = (u16)lwr;
5477 rc = moveToChild(pCur, chldPg);
5478 if( rc ) break;
5480 moveto_finish:
5481 pCur->info.nSize = 0;
5482 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5483 return rc;
5488 ** Return TRUE if the cursor is not pointing at an entry of the table.
5490 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
5491 ** past the last entry in the table or sqlite3BtreePrev() moves past
5492 ** the first entry. TRUE is also returned if the table is empty.
5494 int sqlite3BtreeEof(BtCursor *pCur){
5495 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
5496 ** have been deleted? This API will need to change to return an error code
5497 ** as well as the boolean result value.
5499 return (CURSOR_VALID!=pCur->eState);
5503 ** Return an estimate for the number of rows in the table that pCur is
5504 ** pointing to. Return a negative number if no estimate is currently
5505 ** available.
5507 i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
5508 i64 n;
5509 u8 i;
5511 assert( cursorOwnsBtShared(pCur) );
5512 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5514 /* Currently this interface is only called by the OP_IfSmaller
5515 ** opcode, and it that case the cursor will always be valid and
5516 ** will always point to a leaf node. */
5517 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1;
5518 if( NEVER(pCur->pPage->leaf==0) ) return -1;
5520 n = pCur->pPage->nCell;
5521 for(i=0; i<pCur->iPage; i++){
5522 n *= pCur->apPage[i]->nCell;
5524 return n;
5528 ** Advance the cursor to the next entry in the database.
5529 ** Return value:
5531 ** SQLITE_OK success
5532 ** SQLITE_DONE cursor is already pointing at the last element
5533 ** otherwise some kind of error occurred
5535 ** The main entry point is sqlite3BtreeNext(). That routine is optimized
5536 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx
5537 ** to the next cell on the current page. The (slower) btreeNext() helper
5538 ** routine is called when it is necessary to move to a different page or
5539 ** to restore the cursor.
5541 ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the
5542 ** cursor corresponds to an SQL index and this routine could have been
5543 ** skipped if the SQL index had been a unique index. The F argument
5544 ** is a hint to the implement. SQLite btree implementation does not use
5545 ** this hint, but COMDB2 does.
5547 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){
5548 int rc;
5549 int idx;
5550 MemPage *pPage;
5552 assert( cursorOwnsBtShared(pCur) );
5553 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5554 if( pCur->eState!=CURSOR_VALID ){
5555 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
5556 rc = restoreCursorPosition(pCur);
5557 if( rc!=SQLITE_OK ){
5558 return rc;
5560 if( CURSOR_INVALID==pCur->eState ){
5561 return SQLITE_DONE;
5563 if( pCur->skipNext ){
5564 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5565 pCur->eState = CURSOR_VALID;
5566 if( pCur->skipNext>0 ){
5567 pCur->skipNext = 0;
5568 return SQLITE_OK;
5570 pCur->skipNext = 0;
5574 pPage = pCur->pPage;
5575 idx = ++pCur->ix;
5576 assert( pPage->isInit );
5578 /* If the database file is corrupt, it is possible for the value of idx
5579 ** to be invalid here. This can only occur if a second cursor modifies
5580 ** the page while cursor pCur is holding a reference to it. Which can
5581 ** only happen if the database is corrupt in such a way as to link the
5582 ** page into more than one b-tree structure. */
5583 testcase( idx>pPage->nCell );
5585 if( idx>=pPage->nCell ){
5586 if( !pPage->leaf ){
5587 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
5588 if( rc ) return rc;
5589 return moveToLeftmost(pCur);
5592 if( pCur->iPage==0 ){
5593 pCur->eState = CURSOR_INVALID;
5594 return SQLITE_DONE;
5596 moveToParent(pCur);
5597 pPage = pCur->pPage;
5598 }while( pCur->ix>=pPage->nCell );
5599 if( pPage->intKey ){
5600 return sqlite3BtreeNext(pCur, 0);
5601 }else{
5602 return SQLITE_OK;
5605 if( pPage->leaf ){
5606 return SQLITE_OK;
5607 }else{
5608 return moveToLeftmost(pCur);
5611 int sqlite3BtreeNext(BtCursor *pCur, int flags){
5612 MemPage *pPage;
5613 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
5614 assert( cursorOwnsBtShared(pCur) );
5615 assert( flags==0 || flags==1 );
5616 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5617 pCur->info.nSize = 0;
5618 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl);
5619 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur);
5620 pPage = pCur->pPage;
5621 if( (++pCur->ix)>=pPage->nCell ){
5622 pCur->ix--;
5623 return btreeNext(pCur);
5625 if( pPage->leaf ){
5626 return SQLITE_OK;
5627 }else{
5628 return moveToLeftmost(pCur);
5633 ** Step the cursor to the back to the previous entry in the database.
5634 ** Return values:
5636 ** SQLITE_OK success
5637 ** SQLITE_DONE the cursor is already on the first element of the table
5638 ** otherwise some kind of error occurred
5640 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized
5641 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx
5642 ** to the previous cell on the current page. The (slower) btreePrevious()
5643 ** helper routine is called when it is necessary to move to a different page
5644 ** or to restore the cursor.
5646 ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then
5647 ** the cursor corresponds to an SQL index and this routine could have been
5648 ** skipped if the SQL index had been a unique index. The F argument is a
5649 ** hint to the implement. The native SQLite btree implementation does not
5650 ** use this hint, but COMDB2 does.
5652 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){
5653 int rc;
5654 MemPage *pPage;
5656 assert( cursorOwnsBtShared(pCur) );
5657 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5658 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 );
5659 assert( pCur->info.nSize==0 );
5660 if( pCur->eState!=CURSOR_VALID ){
5661 rc = restoreCursorPosition(pCur);
5662 if( rc!=SQLITE_OK ){
5663 return rc;
5665 if( CURSOR_INVALID==pCur->eState ){
5666 return SQLITE_DONE;
5668 if( pCur->skipNext ){
5669 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
5670 pCur->eState = CURSOR_VALID;
5671 if( pCur->skipNext<0 ){
5672 pCur->skipNext = 0;
5673 return SQLITE_OK;
5675 pCur->skipNext = 0;
5679 pPage = pCur->pPage;
5680 assert( pPage->isInit );
5681 if( !pPage->leaf ){
5682 int idx = pCur->ix;
5683 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
5684 if( rc ) return rc;
5685 rc = moveToRightmost(pCur);
5686 }else{
5687 while( pCur->ix==0 ){
5688 if( pCur->iPage==0 ){
5689 pCur->eState = CURSOR_INVALID;
5690 return SQLITE_DONE;
5692 moveToParent(pCur);
5694 assert( pCur->info.nSize==0 );
5695 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 );
5697 pCur->ix--;
5698 pPage = pCur->pPage;
5699 if( pPage->intKey && !pPage->leaf ){
5700 rc = sqlite3BtreePrevious(pCur, 0);
5701 }else{
5702 rc = SQLITE_OK;
5705 return rc;
5707 int sqlite3BtreePrevious(BtCursor *pCur, int flags){
5708 assert( cursorOwnsBtShared(pCur) );
5709 assert( flags==0 || flags==1 );
5710 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5711 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */
5712 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey);
5713 pCur->info.nSize = 0;
5714 if( pCur->eState!=CURSOR_VALID
5715 || pCur->ix==0
5716 || pCur->pPage->leaf==0
5718 return btreePrevious(pCur);
5720 pCur->ix--;
5721 return SQLITE_OK;
5725 ** Allocate a new page from the database file.
5727 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
5728 ** has already been called on the new page.) The new page has also
5729 ** been referenced and the calling routine is responsible for calling
5730 ** sqlite3PagerUnref() on the new page when it is done.
5732 ** SQLITE_OK is returned on success. Any other return value indicates
5733 ** an error. *ppPage is set to NULL in the event of an error.
5735 ** If the "nearby" parameter is not 0, then an effort is made to
5736 ** locate a page close to the page number "nearby". This can be used in an
5737 ** attempt to keep related pages close to each other in the database file,
5738 ** which in turn can make database access faster.
5740 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
5741 ** anywhere on the free-list, then it is guaranteed to be returned. If
5742 ** eMode is BTALLOC_LT then the page returned will be less than or equal
5743 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there
5744 ** are no restrictions on which page is returned.
5746 static int allocateBtreePage(
5747 BtShared *pBt, /* The btree */
5748 MemPage **ppPage, /* Store pointer to the allocated page here */
5749 Pgno *pPgno, /* Store the page number here */
5750 Pgno nearby, /* Search for a page near this one */
5751 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
5753 MemPage *pPage1;
5754 int rc;
5755 u32 n; /* Number of pages on the freelist */
5756 u32 k; /* Number of leaves on the trunk of the freelist */
5757 MemPage *pTrunk = 0;
5758 MemPage *pPrevTrunk = 0;
5759 Pgno mxPage; /* Total size of the database file */
5761 assert( sqlite3_mutex_held(pBt->mutex) );
5762 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
5763 pPage1 = pBt->pPage1;
5764 mxPage = btreePagecount(pBt);
5765 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36
5766 ** stores stores the total number of pages on the freelist. */
5767 n = get4byte(&pPage1->aData[36]);
5768 testcase( n==mxPage-1 );
5769 if( n>=mxPage ){
5770 return SQLITE_CORRUPT_BKPT;
5772 if( n>0 ){
5773 /* There are pages on the freelist. Reuse one of those pages. */
5774 Pgno iTrunk;
5775 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
5776 u32 nSearch = 0; /* Count of the number of search attempts */
5778 /* If eMode==BTALLOC_EXACT and a query of the pointer-map
5779 ** shows that the page 'nearby' is somewhere on the free-list, then
5780 ** the entire-list will be searched for that page.
5782 #ifndef SQLITE_OMIT_AUTOVACUUM
5783 if( eMode==BTALLOC_EXACT ){
5784 if( nearby<=mxPage ){
5785 u8 eType;
5786 assert( nearby>0 );
5787 assert( pBt->autoVacuum );
5788 rc = ptrmapGet(pBt, nearby, &eType, 0);
5789 if( rc ) return rc;
5790 if( eType==PTRMAP_FREEPAGE ){
5791 searchList = 1;
5794 }else if( eMode==BTALLOC_LE ){
5795 searchList = 1;
5797 #endif
5799 /* Decrement the free-list count by 1. Set iTrunk to the index of the
5800 ** first free-list trunk page. iPrevTrunk is initially 1.
5802 rc = sqlite3PagerWrite(pPage1->pDbPage);
5803 if( rc ) return rc;
5804 put4byte(&pPage1->aData[36], n-1);
5806 /* The code within this loop is run only once if the 'searchList' variable
5807 ** is not true. Otherwise, it runs once for each trunk-page on the
5808 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
5809 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
5811 do {
5812 pPrevTrunk = pTrunk;
5813 if( pPrevTrunk ){
5814 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page
5815 ** is the page number of the next freelist trunk page in the list or
5816 ** zero if this is the last freelist trunk page. */
5817 iTrunk = get4byte(&pPrevTrunk->aData[0]);
5818 }else{
5819 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32
5820 ** stores the page number of the first page of the freelist, or zero if
5821 ** the freelist is empty. */
5822 iTrunk = get4byte(&pPage1->aData[32]);
5824 testcase( iTrunk==mxPage );
5825 if( iTrunk>mxPage || nSearch++ > n ){
5826 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1);
5827 }else{
5828 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
5830 if( rc ){
5831 pTrunk = 0;
5832 goto end_allocate_page;
5834 assert( pTrunk!=0 );
5835 assert( pTrunk->aData!=0 );
5836 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page
5837 ** is the number of leaf page pointers to follow. */
5838 k = get4byte(&pTrunk->aData[4]);
5839 if( k==0 && !searchList ){
5840 /* The trunk has no leaves and the list is not being searched.
5841 ** So extract the trunk page itself and use it as the newly
5842 ** allocated page */
5843 assert( pPrevTrunk==0 );
5844 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5845 if( rc ){
5846 goto end_allocate_page;
5848 *pPgno = iTrunk;
5849 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5850 *ppPage = pTrunk;
5851 pTrunk = 0;
5852 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5853 }else if( k>(u32)(pBt->usableSize/4 - 2) ){
5854 /* Value of k is out of range. Database corruption */
5855 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5856 goto end_allocate_page;
5857 #ifndef SQLITE_OMIT_AUTOVACUUM
5858 }else if( searchList
5859 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
5861 /* The list is being searched and this trunk page is the page
5862 ** to allocate, regardless of whether it has leaves.
5864 *pPgno = iTrunk;
5865 *ppPage = pTrunk;
5866 searchList = 0;
5867 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5868 if( rc ){
5869 goto end_allocate_page;
5871 if( k==0 ){
5872 if( !pPrevTrunk ){
5873 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
5874 }else{
5875 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5876 if( rc!=SQLITE_OK ){
5877 goto end_allocate_page;
5879 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
5881 }else{
5882 /* The trunk page is required by the caller but it contains
5883 ** pointers to free-list leaves. The first leaf becomes a trunk
5884 ** page in this case.
5886 MemPage *pNewTrunk;
5887 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
5888 if( iNewTrunk>mxPage ){
5889 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5890 goto end_allocate_page;
5892 testcase( iNewTrunk==mxPage );
5893 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
5894 if( rc!=SQLITE_OK ){
5895 goto end_allocate_page;
5897 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5898 if( rc!=SQLITE_OK ){
5899 releasePage(pNewTrunk);
5900 goto end_allocate_page;
5902 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
5903 put4byte(&pNewTrunk->aData[4], k-1);
5904 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
5905 releasePage(pNewTrunk);
5906 if( !pPrevTrunk ){
5907 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
5908 put4byte(&pPage1->aData[32], iNewTrunk);
5909 }else{
5910 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
5911 if( rc ){
5912 goto end_allocate_page;
5914 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
5917 pTrunk = 0;
5918 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
5919 #endif
5920 }else if( k>0 ){
5921 /* Extract a leaf from the trunk */
5922 u32 closest;
5923 Pgno iPage;
5924 unsigned char *aData = pTrunk->aData;
5925 if( nearby>0 ){
5926 u32 i;
5927 closest = 0;
5928 if( eMode==BTALLOC_LE ){
5929 for(i=0; i<k; i++){
5930 iPage = get4byte(&aData[8+i*4]);
5931 if( iPage<=nearby ){
5932 closest = i;
5933 break;
5936 }else{
5937 int dist;
5938 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
5939 for(i=1; i<k; i++){
5940 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
5941 if( d2<dist ){
5942 closest = i;
5943 dist = d2;
5947 }else{
5948 closest = 0;
5951 iPage = get4byte(&aData[8+closest*4]);
5952 testcase( iPage==mxPage );
5953 if( iPage>mxPage ){
5954 rc = SQLITE_CORRUPT_PGNO(iTrunk);
5955 goto end_allocate_page;
5957 testcase( iPage==mxPage );
5958 if( !searchList
5959 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
5961 int noContent;
5962 *pPgno = iPage;
5963 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
5964 ": %d more free pages\n",
5965 *pPgno, closest+1, k, pTrunk->pgno, n-1));
5966 rc = sqlite3PagerWrite(pTrunk->pDbPage);
5967 if( rc ) goto end_allocate_page;
5968 if( closest<k-1 ){
5969 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5971 put4byte(&aData[4], k-1);
5972 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0;
5973 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
5974 if( rc==SQLITE_OK ){
5975 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5976 if( rc!=SQLITE_OK ){
5977 releasePage(*ppPage);
5978 *ppPage = 0;
5981 searchList = 0;
5984 releasePage(pPrevTrunk);
5985 pPrevTrunk = 0;
5986 }while( searchList );
5987 }else{
5988 /* There are no pages on the freelist, so append a new page to the
5989 ** database image.
5991 ** Normally, new pages allocated by this block can be requested from the
5992 ** pager layer with the 'no-content' flag set. This prevents the pager
5993 ** from trying to read the pages content from disk. However, if the
5994 ** current transaction has already run one or more incremental-vacuum
5995 ** steps, then the page we are about to allocate may contain content
5996 ** that is required in the event of a rollback. In this case, do
5997 ** not set the no-content flag. This causes the pager to load and journal
5998 ** the current page content before overwriting it.
6000 ** Note that the pager will not actually attempt to load or journal
6001 ** content for any page that really does lie past the end of the database
6002 ** file on disk. So the effects of disabling the no-content optimization
6003 ** here are confined to those pages that lie between the end of the
6004 ** database image and the end of the database file.
6006 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0;
6008 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6009 if( rc ) return rc;
6010 pBt->nPage++;
6011 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
6013 #ifndef SQLITE_OMIT_AUTOVACUUM
6014 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
6015 /* If *pPgno refers to a pointer-map page, allocate two new pages
6016 ** at the end of the file instead of one. The first allocated page
6017 ** becomes a new pointer-map page, the second is used by the caller.
6019 MemPage *pPg = 0;
6020 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
6021 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
6022 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
6023 if( rc==SQLITE_OK ){
6024 rc = sqlite3PagerWrite(pPg->pDbPage);
6025 releasePage(pPg);
6027 if( rc ) return rc;
6028 pBt->nPage++;
6029 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
6031 #endif
6032 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
6033 *pPgno = pBt->nPage;
6035 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
6036 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
6037 if( rc ) return rc;
6038 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
6039 if( rc!=SQLITE_OK ){
6040 releasePage(*ppPage);
6041 *ppPage = 0;
6043 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
6046 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
6048 end_allocate_page:
6049 releasePage(pTrunk);
6050 releasePage(pPrevTrunk);
6051 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
6052 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 );
6053 return rc;
6057 ** This function is used to add page iPage to the database file free-list.
6058 ** It is assumed that the page is not already a part of the free-list.
6060 ** The value passed as the second argument to this function is optional.
6061 ** If the caller happens to have a pointer to the MemPage object
6062 ** corresponding to page iPage handy, it may pass it as the second value.
6063 ** Otherwise, it may pass NULL.
6065 ** If a pointer to a MemPage object is passed as the second argument,
6066 ** its reference count is not altered by this function.
6068 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
6069 MemPage *pTrunk = 0; /* Free-list trunk page */
6070 Pgno iTrunk = 0; /* Page number of free-list trunk page */
6071 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */
6072 MemPage *pPage; /* Page being freed. May be NULL. */
6073 int rc; /* Return Code */
6074 int nFree; /* Initial number of pages on free-list */
6076 assert( sqlite3_mutex_held(pBt->mutex) );
6077 assert( CORRUPT_DB || iPage>1 );
6078 assert( !pMemPage || pMemPage->pgno==iPage );
6080 if( iPage<2 ) return SQLITE_CORRUPT_BKPT;
6081 if( pMemPage ){
6082 pPage = pMemPage;
6083 sqlite3PagerRef(pPage->pDbPage);
6084 }else{
6085 pPage = btreePageLookup(pBt, iPage);
6088 /* Increment the free page count on pPage1 */
6089 rc = sqlite3PagerWrite(pPage1->pDbPage);
6090 if( rc ) goto freepage_out;
6091 nFree = get4byte(&pPage1->aData[36]);
6092 put4byte(&pPage1->aData[36], nFree+1);
6094 if( pBt->btsFlags & BTS_SECURE_DELETE ){
6095 /* If the secure_delete option is enabled, then
6096 ** always fully overwrite deleted information with zeros.
6098 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
6099 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
6101 goto freepage_out;
6103 memset(pPage->aData, 0, pPage->pBt->pageSize);
6106 /* If the database supports auto-vacuum, write an entry in the pointer-map
6107 ** to indicate that the page is free.
6109 if( ISAUTOVACUUM ){
6110 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
6111 if( rc ) goto freepage_out;
6114 /* Now manipulate the actual database free-list structure. There are two
6115 ** possibilities. If the free-list is currently empty, or if the first
6116 ** trunk page in the free-list is full, then this page will become a
6117 ** new free-list trunk page. Otherwise, it will become a leaf of the
6118 ** first trunk page in the current free-list. This block tests if it
6119 ** is possible to add the page as a new free-list leaf.
6121 if( nFree!=0 ){
6122 u32 nLeaf; /* Initial number of leaf cells on trunk page */
6124 iTrunk = get4byte(&pPage1->aData[32]);
6125 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
6126 if( rc!=SQLITE_OK ){
6127 goto freepage_out;
6130 nLeaf = get4byte(&pTrunk->aData[4]);
6131 assert( pBt->usableSize>32 );
6132 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
6133 rc = SQLITE_CORRUPT_BKPT;
6134 goto freepage_out;
6136 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
6137 /* In this case there is room on the trunk page to insert the page
6138 ** being freed as a new leaf.
6140 ** Note that the trunk page is not really full until it contains
6141 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
6142 ** coded. But due to a coding error in versions of SQLite prior to
6143 ** 3.6.0, databases with freelist trunk pages holding more than
6144 ** usableSize/4 - 8 entries will be reported as corrupt. In order
6145 ** to maintain backwards compatibility with older versions of SQLite,
6146 ** we will continue to restrict the number of entries to usableSize/4 - 8
6147 ** for now. At some point in the future (once everyone has upgraded
6148 ** to 3.6.0 or later) we should consider fixing the conditional above
6149 ** to read "usableSize/4-2" instead of "usableSize/4-8".
6151 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
6152 ** avoid using the last six entries in the freelist trunk page array in
6153 ** order that database files created by newer versions of SQLite can be
6154 ** read by older versions of SQLite.
6156 rc = sqlite3PagerWrite(pTrunk->pDbPage);
6157 if( rc==SQLITE_OK ){
6158 put4byte(&pTrunk->aData[4], nLeaf+1);
6159 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
6160 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
6161 sqlite3PagerDontWrite(pPage->pDbPage);
6163 rc = btreeSetHasContent(pBt, iPage);
6165 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
6166 goto freepage_out;
6170 /* If control flows to this point, then it was not possible to add the
6171 ** the page being freed as a leaf page of the first trunk in the free-list.
6172 ** Possibly because the free-list is empty, or possibly because the
6173 ** first trunk in the free-list is full. Either way, the page being freed
6174 ** will become the new first trunk page in the free-list.
6176 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
6177 goto freepage_out;
6179 rc = sqlite3PagerWrite(pPage->pDbPage);
6180 if( rc!=SQLITE_OK ){
6181 goto freepage_out;
6183 put4byte(pPage->aData, iTrunk);
6184 put4byte(&pPage->aData[4], 0);
6185 put4byte(&pPage1->aData[32], iPage);
6186 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
6188 freepage_out:
6189 if( pPage ){
6190 pPage->isInit = 0;
6192 releasePage(pPage);
6193 releasePage(pTrunk);
6194 return rc;
6196 static void freePage(MemPage *pPage, int *pRC){
6197 if( (*pRC)==SQLITE_OK ){
6198 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
6203 ** Free any overflow pages associated with the given Cell. Store
6204 ** size information about the cell in pInfo.
6206 static int clearCell(
6207 MemPage *pPage, /* The page that contains the Cell */
6208 unsigned char *pCell, /* First byte of the Cell */
6209 CellInfo *pInfo /* Size information about the cell */
6211 BtShared *pBt;
6212 Pgno ovflPgno;
6213 int rc;
6214 int nOvfl;
6215 u32 ovflPageSize;
6217 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6218 pPage->xParseCell(pPage, pCell, pInfo);
6219 if( pInfo->nLocal==pInfo->nPayload ){
6220 return SQLITE_OK; /* No overflow pages. Return without doing anything */
6222 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
6223 /* Cell extends past end of page */
6224 return SQLITE_CORRUPT_PAGE(pPage);
6226 ovflPgno = get4byte(pCell + pInfo->nSize - 4);
6227 pBt = pPage->pBt;
6228 assert( pBt->usableSize > 4 );
6229 ovflPageSize = pBt->usableSize - 4;
6230 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
6231 assert( nOvfl>0 ||
6232 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
6234 while( nOvfl-- ){
6235 Pgno iNext = 0;
6236 MemPage *pOvfl = 0;
6237 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
6238 /* 0 is not a legal page number and page 1 cannot be an
6239 ** overflow page. Therefore if ovflPgno<2 or past the end of the
6240 ** file the database must be corrupt. */
6241 return SQLITE_CORRUPT_BKPT;
6243 if( nOvfl ){
6244 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
6245 if( rc ) return rc;
6248 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
6249 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
6251 /* There is no reason any cursor should have an outstanding reference
6252 ** to an overflow page belonging to a cell that is being deleted/updated.
6253 ** So if there exists more than one reference to this page, then it
6254 ** must not really be an overflow page and the database must be corrupt.
6255 ** It is helpful to detect this before calling freePage2(), as
6256 ** freePage2() may zero the page contents if secure-delete mode is
6257 ** enabled. If this 'overflow' page happens to be a page that the
6258 ** caller is iterating through or using in some other way, this
6259 ** can be problematic.
6261 rc = SQLITE_CORRUPT_BKPT;
6262 }else{
6263 rc = freePage2(pBt, pOvfl, ovflPgno);
6266 if( pOvfl ){
6267 sqlite3PagerUnref(pOvfl->pDbPage);
6269 if( rc ) return rc;
6270 ovflPgno = iNext;
6272 return SQLITE_OK;
6276 ** Create the byte sequence used to represent a cell on page pPage
6277 ** and write that byte sequence into pCell[]. Overflow pages are
6278 ** allocated and filled in as necessary. The calling procedure
6279 ** is responsible for making sure sufficient space has been allocated
6280 ** for pCell[].
6282 ** Note that pCell does not necessary need to point to the pPage->aData
6283 ** area. pCell might point to some temporary storage. The cell will
6284 ** be constructed in this temporary area then copied into pPage->aData
6285 ** later.
6287 static int fillInCell(
6288 MemPage *pPage, /* The page that contains the cell */
6289 unsigned char *pCell, /* Complete text of the cell */
6290 const BtreePayload *pX, /* Payload with which to construct the cell */
6291 int *pnSize /* Write cell size here */
6293 int nPayload;
6294 const u8 *pSrc;
6295 int nSrc, n, rc, mn;
6296 int spaceLeft;
6297 MemPage *pToRelease;
6298 unsigned char *pPrior;
6299 unsigned char *pPayload;
6300 BtShared *pBt;
6301 Pgno pgnoOvfl;
6302 int nHeader;
6304 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6306 /* pPage is not necessarily writeable since pCell might be auxiliary
6307 ** buffer space that is separate from the pPage buffer area */
6308 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
6309 || sqlite3PagerIswriteable(pPage->pDbPage) );
6311 /* Fill in the header. */
6312 nHeader = pPage->childPtrSize;
6313 if( pPage->intKey ){
6314 nPayload = pX->nData + pX->nZero;
6315 pSrc = pX->pData;
6316 nSrc = pX->nData;
6317 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
6318 nHeader += putVarint32(&pCell[nHeader], nPayload);
6319 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
6320 }else{
6321 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
6322 nSrc = nPayload = (int)pX->nKey;
6323 pSrc = pX->pKey;
6324 nHeader += putVarint32(&pCell[nHeader], nPayload);
6327 /* Fill in the payload */
6328 pPayload = &pCell[nHeader];
6329 if( nPayload<=pPage->maxLocal ){
6330 /* This is the common case where everything fits on the btree page
6331 ** and no overflow pages are required. */
6332 n = nHeader + nPayload;
6333 testcase( n==3 );
6334 testcase( n==4 );
6335 if( n<4 ) n = 4;
6336 *pnSize = n;
6337 assert( nSrc<=nPayload );
6338 testcase( nSrc<nPayload );
6339 memcpy(pPayload, pSrc, nSrc);
6340 memset(pPayload+nSrc, 0, nPayload-nSrc);
6341 return SQLITE_OK;
6344 /* If we reach this point, it means that some of the content will need
6345 ** to spill onto overflow pages.
6347 mn = pPage->minLocal;
6348 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
6349 testcase( n==pPage->maxLocal );
6350 testcase( n==pPage->maxLocal+1 );
6351 if( n > pPage->maxLocal ) n = mn;
6352 spaceLeft = n;
6353 *pnSize = n + nHeader + 4;
6354 pPrior = &pCell[nHeader+n];
6355 pToRelease = 0;
6356 pgnoOvfl = 0;
6357 pBt = pPage->pBt;
6359 /* At this point variables should be set as follows:
6361 ** nPayload Total payload size in bytes
6362 ** pPayload Begin writing payload here
6363 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft,
6364 ** that means content must spill into overflow pages.
6365 ** *pnSize Size of the local cell (not counting overflow pages)
6366 ** pPrior Where to write the pgno of the first overflow page
6368 ** Use a call to btreeParseCellPtr() to verify that the values above
6369 ** were computed correctly.
6371 #ifdef SQLITE_DEBUG
6373 CellInfo info;
6374 pPage->xParseCell(pPage, pCell, &info);
6375 assert( nHeader==(int)(info.pPayload - pCell) );
6376 assert( info.nKey==pX->nKey );
6377 assert( *pnSize == info.nSize );
6378 assert( spaceLeft == info.nLocal );
6380 #endif
6382 /* Write the payload into the local Cell and any extra into overflow pages */
6383 while( 1 ){
6384 n = nPayload;
6385 if( n>spaceLeft ) n = spaceLeft;
6387 /* If pToRelease is not zero than pPayload points into the data area
6388 ** of pToRelease. Make sure pToRelease is still writeable. */
6389 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6391 /* If pPayload is part of the data area of pPage, then make sure pPage
6392 ** is still writeable */
6393 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
6394 || sqlite3PagerIswriteable(pPage->pDbPage) );
6396 if( nSrc>=n ){
6397 memcpy(pPayload, pSrc, n);
6398 }else if( nSrc>0 ){
6399 n = nSrc;
6400 memcpy(pPayload, pSrc, n);
6401 }else{
6402 memset(pPayload, 0, n);
6404 nPayload -= n;
6405 if( nPayload<=0 ) break;
6406 pPayload += n;
6407 pSrc += n;
6408 nSrc -= n;
6409 spaceLeft -= n;
6410 if( spaceLeft==0 ){
6411 MemPage *pOvfl = 0;
6412 #ifndef SQLITE_OMIT_AUTOVACUUM
6413 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
6414 if( pBt->autoVacuum ){
6416 pgnoOvfl++;
6417 } while(
6418 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
6421 #endif
6422 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
6423 #ifndef SQLITE_OMIT_AUTOVACUUM
6424 /* If the database supports auto-vacuum, and the second or subsequent
6425 ** overflow page is being allocated, add an entry to the pointer-map
6426 ** for that page now.
6428 ** If this is the first overflow page, then write a partial entry
6429 ** to the pointer-map. If we write nothing to this pointer-map slot,
6430 ** then the optimistic overflow chain processing in clearCell()
6431 ** may misinterpret the uninitialized values and delete the
6432 ** wrong pages from the database.
6434 if( pBt->autoVacuum && rc==SQLITE_OK ){
6435 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
6436 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
6437 if( rc ){
6438 releasePage(pOvfl);
6441 #endif
6442 if( rc ){
6443 releasePage(pToRelease);
6444 return rc;
6447 /* If pToRelease is not zero than pPrior points into the data area
6448 ** of pToRelease. Make sure pToRelease is still writeable. */
6449 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
6451 /* If pPrior is part of the data area of pPage, then make sure pPage
6452 ** is still writeable */
6453 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
6454 || sqlite3PagerIswriteable(pPage->pDbPage) );
6456 put4byte(pPrior, pgnoOvfl);
6457 releasePage(pToRelease);
6458 pToRelease = pOvfl;
6459 pPrior = pOvfl->aData;
6460 put4byte(pPrior, 0);
6461 pPayload = &pOvfl->aData[4];
6462 spaceLeft = pBt->usableSize - 4;
6465 releasePage(pToRelease);
6466 return SQLITE_OK;
6470 ** Remove the i-th cell from pPage. This routine effects pPage only.
6471 ** The cell content is not freed or deallocated. It is assumed that
6472 ** the cell content has been copied someplace else. This routine just
6473 ** removes the reference to the cell from pPage.
6475 ** "sz" must be the number of bytes in the cell.
6477 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
6478 u32 pc; /* Offset to cell content of cell being deleted */
6479 u8 *data; /* pPage->aData */
6480 u8 *ptr; /* Used to move bytes around within data[] */
6481 int rc; /* The return code */
6482 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */
6484 if( *pRC ) return;
6485 assert( idx>=0 && idx<pPage->nCell );
6486 assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
6487 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6488 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6489 data = pPage->aData;
6490 ptr = &pPage->aCellIdx[2*idx];
6491 pc = get2byte(ptr);
6492 hdr = pPage->hdrOffset;
6493 testcase( pc==get2byte(&data[hdr+5]) );
6494 testcase( pc+sz==pPage->pBt->usableSize );
6495 if( pc+sz > pPage->pBt->usableSize ){
6496 *pRC = SQLITE_CORRUPT_BKPT;
6497 return;
6499 rc = freeSpace(pPage, pc, sz);
6500 if( rc ){
6501 *pRC = rc;
6502 return;
6504 pPage->nCell--;
6505 if( pPage->nCell==0 ){
6506 memset(&data[hdr+1], 0, 4);
6507 data[hdr+7] = 0;
6508 put2byte(&data[hdr+5], pPage->pBt->usableSize);
6509 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
6510 - pPage->childPtrSize - 8;
6511 }else{
6512 memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
6513 put2byte(&data[hdr+3], pPage->nCell);
6514 pPage->nFree += 2;
6519 ** Insert a new cell on pPage at cell index "i". pCell points to the
6520 ** content of the cell.
6522 ** If the cell content will fit on the page, then put it there. If it
6523 ** will not fit, then make a copy of the cell content into pTemp if
6524 ** pTemp is not null. Regardless of pTemp, allocate a new entry
6525 ** in pPage->apOvfl[] and make it point to the cell content (either
6526 ** in pTemp or the original pCell) and also record its index.
6527 ** Allocating a new entry in pPage->aCell[] implies that
6528 ** pPage->nOverflow is incremented.
6530 ** *pRC must be SQLITE_OK when this routine is called.
6532 static void insertCell(
6533 MemPage *pPage, /* Page into which we are copying */
6534 int i, /* New cell becomes the i-th cell of the page */
6535 u8 *pCell, /* Content of the new cell */
6536 int sz, /* Bytes of content in pCell */
6537 u8 *pTemp, /* Temp storage space for pCell, if needed */
6538 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */
6539 int *pRC /* Read and write return code from here */
6541 int idx = 0; /* Where to write new cell content in data[] */
6542 int j; /* Loop counter */
6543 u8 *data; /* The content of the whole page */
6544 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */
6546 assert( *pRC==SQLITE_OK );
6547 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
6548 assert( MX_CELL(pPage->pBt)<=10921 );
6549 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
6550 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
6551 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
6552 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6553 /* The cell should normally be sized correctly. However, when moving a
6554 ** malformed cell from a leaf page to an interior page, if the cell size
6555 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
6556 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence
6557 ** the term after the || in the following assert(). */
6558 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
6559 if( pPage->nOverflow || sz+2>pPage->nFree ){
6560 if( pTemp ){
6561 memcpy(pTemp, pCell, sz);
6562 pCell = pTemp;
6564 if( iChild ){
6565 put4byte(pCell, iChild);
6567 j = pPage->nOverflow++;
6568 /* Comparison against ArraySize-1 since we hold back one extra slot
6569 ** as a contingency. In other words, never need more than 3 overflow
6570 ** slots but 4 are allocated, just to be safe. */
6571 assert( j < ArraySize(pPage->apOvfl)-1 );
6572 pPage->apOvfl[j] = pCell;
6573 pPage->aiOvfl[j] = (u16)i;
6575 /* When multiple overflows occur, they are always sequential and in
6576 ** sorted order. This invariants arise because multiple overflows can
6577 ** only occur when inserting divider cells into the parent page during
6578 ** balancing, and the dividers are adjacent and sorted.
6580 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */
6581 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */
6582 }else{
6583 int rc = sqlite3PagerWrite(pPage->pDbPage);
6584 if( rc!=SQLITE_OK ){
6585 *pRC = rc;
6586 return;
6588 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6589 data = pPage->aData;
6590 assert( &data[pPage->cellOffset]==pPage->aCellIdx );
6591 rc = allocateSpace(pPage, sz, &idx);
6592 if( rc ){ *pRC = rc; return; }
6593 /* The allocateSpace() routine guarantees the following properties
6594 ** if it returns successfully */
6595 assert( idx >= 0 );
6596 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB );
6597 assert( idx+sz <= (int)pPage->pBt->usableSize );
6598 pPage->nFree -= (u16)(2 + sz);
6599 memcpy(&data[idx], pCell, sz);
6600 if( iChild ){
6601 put4byte(&data[idx], iChild);
6603 pIns = pPage->aCellIdx + i*2;
6604 memmove(pIns+2, pIns, 2*(pPage->nCell - i));
6605 put2byte(pIns, idx);
6606 pPage->nCell++;
6607 /* increment the cell count */
6608 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
6609 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell );
6610 #ifndef SQLITE_OMIT_AUTOVACUUM
6611 if( pPage->pBt->autoVacuum ){
6612 /* The cell may contain a pointer to an overflow page. If so, write
6613 ** the entry for the overflow page into the pointer map.
6615 ptrmapPutOvflPtr(pPage, pCell, pRC);
6617 #endif
6622 ** A CellArray object contains a cache of pointers and sizes for a
6623 ** consecutive sequence of cells that might be held on multiple pages.
6625 typedef struct CellArray CellArray;
6626 struct CellArray {
6627 int nCell; /* Number of cells in apCell[] */
6628 MemPage *pRef; /* Reference page */
6629 u8 **apCell; /* All cells begin balanced */
6630 u16 *szCell; /* Local size of all cells in apCell[] */
6634 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been
6635 ** computed.
6637 static void populateCellCache(CellArray *p, int idx, int N){
6638 assert( idx>=0 && idx+N<=p->nCell );
6639 while( N>0 ){
6640 assert( p->apCell[idx]!=0 );
6641 if( p->szCell[idx]==0 ){
6642 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
6643 }else{
6644 assert( CORRUPT_DB ||
6645 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
6647 idx++;
6648 N--;
6653 ** Return the size of the Nth element of the cell array
6655 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){
6656 assert( N>=0 && N<p->nCell );
6657 assert( p->szCell[N]==0 );
6658 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
6659 return p->szCell[N];
6661 static u16 cachedCellSize(CellArray *p, int N){
6662 assert( N>=0 && N<p->nCell );
6663 if( p->szCell[N] ) return p->szCell[N];
6664 return computeCellSize(p, N);
6668 ** Array apCell[] contains pointers to nCell b-tree page cells. The
6669 ** szCell[] array contains the size in bytes of each cell. This function
6670 ** replaces the current contents of page pPg with the contents of the cell
6671 ** array.
6673 ** Some of the cells in apCell[] may currently be stored in pPg. This
6674 ** function works around problems caused by this by making a copy of any
6675 ** such cells before overwriting the page data.
6677 ** The MemPage.nFree field is invalidated by this function. It is the
6678 ** responsibility of the caller to set it correctly.
6680 static int rebuildPage(
6681 MemPage *pPg, /* Edit this page */
6682 int nCell, /* Final number of cells on page */
6683 u8 **apCell, /* Array of cells */
6684 u16 *szCell /* Array of cell sizes */
6686 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */
6687 u8 * const aData = pPg->aData; /* Pointer to data for pPg */
6688 const int usableSize = pPg->pBt->usableSize;
6689 u8 * const pEnd = &aData[usableSize];
6690 int i;
6691 u8 *pCellptr = pPg->aCellIdx;
6692 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6693 u8 *pData;
6695 i = get2byte(&aData[hdr+5]);
6696 memcpy(&pTmp[i], &aData[i], usableSize - i);
6698 pData = pEnd;
6699 for(i=0; i<nCell; i++){
6700 u8 *pCell = apCell[i];
6701 if( SQLITE_WITHIN(pCell,aData,pEnd) ){
6702 pCell = &pTmp[pCell - aData];
6704 pData -= szCell[i];
6705 put2byte(pCellptr, (pData - aData));
6706 pCellptr += 2;
6707 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
6708 memcpy(pData, pCell, szCell[i]);
6709 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
6710 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) );
6713 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
6714 pPg->nCell = nCell;
6715 pPg->nOverflow = 0;
6717 put2byte(&aData[hdr+1], 0);
6718 put2byte(&aData[hdr+3], pPg->nCell);
6719 put2byte(&aData[hdr+5], pData - aData);
6720 aData[hdr+7] = 0x00;
6721 return SQLITE_OK;
6725 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6726 ** contains the size in bytes of each such cell. This function attempts to
6727 ** add the cells stored in the array to page pPg. If it cannot (because
6728 ** the page needs to be defragmented before the cells will fit), non-zero
6729 ** is returned. Otherwise, if the cells are added successfully, zero is
6730 ** returned.
6732 ** Argument pCellptr points to the first entry in the cell-pointer array
6733 ** (part of page pPg) to populate. After cell apCell[0] is written to the
6734 ** page body, a 16-bit offset is written to pCellptr. And so on, for each
6735 ** cell in the array. It is the responsibility of the caller to ensure
6736 ** that it is safe to overwrite this part of the cell-pointer array.
6738 ** When this function is called, *ppData points to the start of the
6739 ** content area on page pPg. If the size of the content area is extended,
6740 ** *ppData is updated to point to the new start of the content area
6741 ** before returning.
6743 ** Finally, argument pBegin points to the byte immediately following the
6744 ** end of the space required by this page for the cell-pointer area (for
6745 ** all cells - not just those inserted by the current call). If the content
6746 ** area must be extended to before this point in order to accomodate all
6747 ** cells in apCell[], then the cells do not fit and non-zero is returned.
6749 static int pageInsertArray(
6750 MemPage *pPg, /* Page to add cells to */
6751 u8 *pBegin, /* End of cell-pointer array */
6752 u8 **ppData, /* IN/OUT: Page content -area pointer */
6753 u8 *pCellptr, /* Pointer to cell-pointer area */
6754 int iFirst, /* Index of first cell to add */
6755 int nCell, /* Number of cells to add to pPg */
6756 CellArray *pCArray /* Array of cells */
6758 int i;
6759 u8 *aData = pPg->aData;
6760 u8 *pData = *ppData;
6761 int iEnd = iFirst + nCell;
6762 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */
6763 for(i=iFirst; i<iEnd; i++){
6764 int sz, rc;
6765 u8 *pSlot;
6766 sz = cachedCellSize(pCArray, i);
6767 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
6768 if( (pData - pBegin)<sz ) return 1;
6769 pData -= sz;
6770 pSlot = pData;
6772 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
6773 ** database. But they might for a corrupt database. Hence use memmove()
6774 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */
6775 assert( (pSlot+sz)<=pCArray->apCell[i]
6776 || pSlot>=(pCArray->apCell[i]+sz)
6777 || CORRUPT_DB );
6778 memmove(pSlot, pCArray->apCell[i], sz);
6779 put2byte(pCellptr, (pSlot - aData));
6780 pCellptr += 2;
6782 *ppData = pData;
6783 return 0;
6787 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell
6788 ** contains the size in bytes of each such cell. This function adds the
6789 ** space associated with each cell in the array that is currently stored
6790 ** within the body of pPg to the pPg free-list. The cell-pointers and other
6791 ** fields of the page are not updated.
6793 ** This function returns the total number of cells added to the free-list.
6795 static int pageFreeArray(
6796 MemPage *pPg, /* Page to edit */
6797 int iFirst, /* First cell to delete */
6798 int nCell, /* Cells to delete */
6799 CellArray *pCArray /* Array of cells */
6801 u8 * const aData = pPg->aData;
6802 u8 * const pEnd = &aData[pPg->pBt->usableSize];
6803 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
6804 int nRet = 0;
6805 int i;
6806 int iEnd = iFirst + nCell;
6807 u8 *pFree = 0;
6808 int szFree = 0;
6810 for(i=iFirst; i<iEnd; i++){
6811 u8 *pCell = pCArray->apCell[i];
6812 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){
6813 int sz;
6814 /* No need to use cachedCellSize() here. The sizes of all cells that
6815 ** are to be freed have already been computing while deciding which
6816 ** cells need freeing */
6817 sz = pCArray->szCell[i]; assert( sz>0 );
6818 if( pFree!=(pCell + sz) ){
6819 if( pFree ){
6820 assert( pFree>aData && (pFree - aData)<65536 );
6821 freeSpace(pPg, (u16)(pFree - aData), szFree);
6823 pFree = pCell;
6824 szFree = sz;
6825 if( pFree+sz>pEnd ) return 0;
6826 }else{
6827 pFree = pCell;
6828 szFree += sz;
6830 nRet++;
6833 if( pFree ){
6834 assert( pFree>aData && (pFree - aData)<65536 );
6835 freeSpace(pPg, (u16)(pFree - aData), szFree);
6837 return nRet;
6841 ** apCell[] and szCell[] contains pointers to and sizes of all cells in the
6842 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting
6843 ** with apCell[iOld]. After balancing, this page should hold nNew cells
6844 ** starting at apCell[iNew].
6846 ** This routine makes the necessary adjustments to pPg so that it contains
6847 ** the correct cells after being balanced.
6849 ** The pPg->nFree field is invalid when this function returns. It is the
6850 ** responsibility of the caller to set it correctly.
6852 static int editPage(
6853 MemPage *pPg, /* Edit this page */
6854 int iOld, /* Index of first cell currently on page */
6855 int iNew, /* Index of new first cell on page */
6856 int nNew, /* Final number of cells on page */
6857 CellArray *pCArray /* Array of cells and sizes */
6859 u8 * const aData = pPg->aData;
6860 const int hdr = pPg->hdrOffset;
6861 u8 *pBegin = &pPg->aCellIdx[nNew * 2];
6862 int nCell = pPg->nCell; /* Cells stored on pPg */
6863 u8 *pData;
6864 u8 *pCellptr;
6865 int i;
6866 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
6867 int iNewEnd = iNew + nNew;
6869 #ifdef SQLITE_DEBUG
6870 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
6871 memcpy(pTmp, aData, pPg->pBt->usableSize);
6872 #endif
6874 /* Remove cells from the start and end of the page */
6875 if( iOld<iNew ){
6876 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
6877 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
6878 nCell -= nShift;
6880 if( iNewEnd < iOldEnd ){
6881 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
6884 pData = &aData[get2byteNotZero(&aData[hdr+5])];
6885 if( pData<pBegin ) goto editpage_fail;
6887 /* Add cells to the start of the page */
6888 if( iNew<iOld ){
6889 int nAdd = MIN(nNew,iOld-iNew);
6890 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
6891 pCellptr = pPg->aCellIdx;
6892 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
6893 if( pageInsertArray(
6894 pPg, pBegin, &pData, pCellptr,
6895 iNew, nAdd, pCArray
6896 ) ) goto editpage_fail;
6897 nCell += nAdd;
6900 /* Add any overflow cells */
6901 for(i=0; i<pPg->nOverflow; i++){
6902 int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
6903 if( iCell>=0 && iCell<nNew ){
6904 pCellptr = &pPg->aCellIdx[iCell * 2];
6905 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
6906 nCell++;
6907 if( pageInsertArray(
6908 pPg, pBegin, &pData, pCellptr,
6909 iCell+iNew, 1, pCArray
6910 ) ) goto editpage_fail;
6914 /* Append cells to the end of the page */
6915 pCellptr = &pPg->aCellIdx[nCell*2];
6916 if( pageInsertArray(
6917 pPg, pBegin, &pData, pCellptr,
6918 iNew+nCell, nNew-nCell, pCArray
6919 ) ) goto editpage_fail;
6921 pPg->nCell = nNew;
6922 pPg->nOverflow = 0;
6924 put2byte(&aData[hdr+3], pPg->nCell);
6925 put2byte(&aData[hdr+5], pData - aData);
6927 #ifdef SQLITE_DEBUG
6928 for(i=0; i<nNew && !CORRUPT_DB; i++){
6929 u8 *pCell = pCArray->apCell[i+iNew];
6930 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
6931 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
6932 pCell = &pTmp[pCell - aData];
6934 assert( 0==memcmp(pCell, &aData[iOff],
6935 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
6937 #endif
6939 return SQLITE_OK;
6940 editpage_fail:
6941 /* Unable to edit this page. Rebuild it from scratch instead. */
6942 populateCellCache(pCArray, iNew, nNew);
6943 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]);
6947 ** The following parameters determine how many adjacent pages get involved
6948 ** in a balancing operation. NN is the number of neighbors on either side
6949 ** of the page that participate in the balancing operation. NB is the
6950 ** total number of pages that participate, including the target page and
6951 ** NN neighbors on either side.
6953 ** The minimum value of NN is 1 (of course). Increasing NN above 1
6954 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
6955 ** in exchange for a larger degradation in INSERT and UPDATE performance.
6956 ** The value of NN appears to give the best results overall.
6958 #define NN 1 /* Number of neighbors on either side of pPage */
6959 #define NB (NN*2+1) /* Total pages involved in the balance */
6962 #ifndef SQLITE_OMIT_QUICKBALANCE
6964 ** This version of balance() handles the common special case where
6965 ** a new entry is being inserted on the extreme right-end of the
6966 ** tree, in other words, when the new entry will become the largest
6967 ** entry in the tree.
6969 ** Instead of trying to balance the 3 right-most leaf pages, just add
6970 ** a new page to the right-hand side and put the one new entry in
6971 ** that page. This leaves the right side of the tree somewhat
6972 ** unbalanced. But odds are that we will be inserting new entries
6973 ** at the end soon afterwards so the nearly empty page will quickly
6974 ** fill up. On average.
6976 ** pPage is the leaf page which is the right-most page in the tree.
6977 ** pParent is its parent. pPage must have a single overflow entry
6978 ** which is also the right-most entry on the page.
6980 ** The pSpace buffer is used to store a temporary copy of the divider
6981 ** cell that will be inserted into pParent. Such a cell consists of a 4
6982 ** byte page number followed by a variable length integer. In other
6983 ** words, at most 13 bytes. Hence the pSpace buffer must be at
6984 ** least 13 bytes in size.
6986 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
6987 BtShared *const pBt = pPage->pBt; /* B-Tree Database */
6988 MemPage *pNew; /* Newly allocated page */
6989 int rc; /* Return Code */
6990 Pgno pgnoNew; /* Page number of pNew */
6992 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6993 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
6994 assert( pPage->nOverflow==1 );
6996 /* This error condition is now caught prior to reaching this function */
6997 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT;
6999 /* Allocate a new page. This page will become the right-sibling of
7000 ** pPage. Make the parent page writable, so that the new divider cell
7001 ** may be inserted. If both these operations are successful, proceed.
7003 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
7005 if( rc==SQLITE_OK ){
7007 u8 *pOut = &pSpace[4];
7008 u8 *pCell = pPage->apOvfl[0];
7009 u16 szCell = pPage->xCellSize(pPage, pCell);
7010 u8 *pStop;
7012 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
7013 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
7014 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
7015 rc = rebuildPage(pNew, 1, &pCell, &szCell);
7016 if( NEVER(rc) ) return rc;
7017 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
7019 /* If this is an auto-vacuum database, update the pointer map
7020 ** with entries for the new page, and any pointer from the
7021 ** cell on the page to an overflow page. If either of these
7022 ** operations fails, the return code is set, but the contents
7023 ** of the parent page are still manipulated by thh code below.
7024 ** That is Ok, at this point the parent page is guaranteed to
7025 ** be marked as dirty. Returning an error code will cause a
7026 ** rollback, undoing any changes made to the parent page.
7028 if( ISAUTOVACUUM ){
7029 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
7030 if( szCell>pNew->minLocal ){
7031 ptrmapPutOvflPtr(pNew, pCell, &rc);
7035 /* Create a divider cell to insert into pParent. The divider cell
7036 ** consists of a 4-byte page number (the page number of pPage) and
7037 ** a variable length key value (which must be the same value as the
7038 ** largest key on pPage).
7040 ** To find the largest key value on pPage, first find the right-most
7041 ** cell on pPage. The first two fields of this cell are the
7042 ** record-length (a variable length integer at most 32-bits in size)
7043 ** and the key value (a variable length integer, may have any value).
7044 ** The first of the while(...) loops below skips over the record-length
7045 ** field. The second while(...) loop copies the key value from the
7046 ** cell on pPage into the pSpace buffer.
7048 pCell = findCell(pPage, pPage->nCell-1);
7049 pStop = &pCell[9];
7050 while( (*(pCell++)&0x80) && pCell<pStop );
7051 pStop = &pCell[9];
7052 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
7054 /* Insert the new divider cell into pParent. */
7055 if( rc==SQLITE_OK ){
7056 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
7057 0, pPage->pgno, &rc);
7060 /* Set the right-child pointer of pParent to point to the new page. */
7061 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
7063 /* Release the reference to the new page. */
7064 releasePage(pNew);
7067 return rc;
7069 #endif /* SQLITE_OMIT_QUICKBALANCE */
7071 #if 0
7073 ** This function does not contribute anything to the operation of SQLite.
7074 ** it is sometimes activated temporarily while debugging code responsible
7075 ** for setting pointer-map entries.
7077 static int ptrmapCheckPages(MemPage **apPage, int nPage){
7078 int i, j;
7079 for(i=0; i<nPage; i++){
7080 Pgno n;
7081 u8 e;
7082 MemPage *pPage = apPage[i];
7083 BtShared *pBt = pPage->pBt;
7084 assert( pPage->isInit );
7086 for(j=0; j<pPage->nCell; j++){
7087 CellInfo info;
7088 u8 *z;
7090 z = findCell(pPage, j);
7091 pPage->xParseCell(pPage, z, &info);
7092 if( info.nLocal<info.nPayload ){
7093 Pgno ovfl = get4byte(&z[info.nSize-4]);
7094 ptrmapGet(pBt, ovfl, &e, &n);
7095 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
7097 if( !pPage->leaf ){
7098 Pgno child = get4byte(z);
7099 ptrmapGet(pBt, child, &e, &n);
7100 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7103 if( !pPage->leaf ){
7104 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
7105 ptrmapGet(pBt, child, &e, &n);
7106 assert( n==pPage->pgno && e==PTRMAP_BTREE );
7109 return 1;
7111 #endif
7114 ** This function is used to copy the contents of the b-tree node stored
7115 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
7116 ** the pointer-map entries for each child page are updated so that the
7117 ** parent page stored in the pointer map is page pTo. If pFrom contained
7118 ** any cells with overflow page pointers, then the corresponding pointer
7119 ** map entries are also updated so that the parent page is page pTo.
7121 ** If pFrom is currently carrying any overflow cells (entries in the
7122 ** MemPage.apOvfl[] array), they are not copied to pTo.
7124 ** Before returning, page pTo is reinitialized using btreeInitPage().
7126 ** The performance of this function is not critical. It is only used by
7127 ** the balance_shallower() and balance_deeper() procedures, neither of
7128 ** which are called often under normal circumstances.
7130 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
7131 if( (*pRC)==SQLITE_OK ){
7132 BtShared * const pBt = pFrom->pBt;
7133 u8 * const aFrom = pFrom->aData;
7134 u8 * const aTo = pTo->aData;
7135 int const iFromHdr = pFrom->hdrOffset;
7136 int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
7137 int rc;
7138 int iData;
7141 assert( pFrom->isInit );
7142 assert( pFrom->nFree>=iToHdr );
7143 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
7145 /* Copy the b-tree node content from page pFrom to page pTo. */
7146 iData = get2byte(&aFrom[iFromHdr+5]);
7147 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
7148 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
7150 /* Reinitialize page pTo so that the contents of the MemPage structure
7151 ** match the new data. The initialization of pTo can actually fail under
7152 ** fairly obscure circumstances, even though it is a copy of initialized
7153 ** page pFrom.
7155 pTo->isInit = 0;
7156 rc = btreeInitPage(pTo);
7157 if( rc!=SQLITE_OK ){
7158 *pRC = rc;
7159 return;
7162 /* If this is an auto-vacuum database, update the pointer-map entries
7163 ** for any b-tree or overflow pages that pTo now contains the pointers to.
7165 if( ISAUTOVACUUM ){
7166 *pRC = setChildPtrmaps(pTo);
7172 ** This routine redistributes cells on the iParentIdx'th child of pParent
7173 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
7174 ** same amount of free space. Usually a single sibling on either side of the
7175 ** page are used in the balancing, though both siblings might come from one
7176 ** side if the page is the first or last child of its parent. If the page
7177 ** has fewer than 2 siblings (something which can only happen if the page
7178 ** is a root page or a child of a root page) then all available siblings
7179 ** participate in the balancing.
7181 ** The number of siblings of the page might be increased or decreased by
7182 ** one or two in an effort to keep pages nearly full but not over full.
7184 ** Note that when this routine is called, some of the cells on the page
7185 ** might not actually be stored in MemPage.aData[]. This can happen
7186 ** if the page is overfull. This routine ensures that all cells allocated
7187 ** to the page and its siblings fit into MemPage.aData[] before returning.
7189 ** In the course of balancing the page and its siblings, cells may be
7190 ** inserted into or removed from the parent page (pParent). Doing so
7191 ** may cause the parent page to become overfull or underfull. If this
7192 ** happens, it is the responsibility of the caller to invoke the correct
7193 ** balancing routine to fix this problem (see the balance() routine).
7195 ** If this routine fails for any reason, it might leave the database
7196 ** in a corrupted state. So if this routine fails, the database should
7197 ** be rolled back.
7199 ** The third argument to this function, aOvflSpace, is a pointer to a
7200 ** buffer big enough to hold one page. If while inserting cells into the parent
7201 ** page (pParent) the parent page becomes overfull, this buffer is
7202 ** used to store the parent's overflow cells. Because this function inserts
7203 ** a maximum of four divider cells into the parent page, and the maximum
7204 ** size of a cell stored within an internal node is always less than 1/4
7205 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
7206 ** enough for all overflow cells.
7208 ** If aOvflSpace is set to a null pointer, this function returns
7209 ** SQLITE_NOMEM.
7211 static int balance_nonroot(
7212 MemPage *pParent, /* Parent page of siblings being balanced */
7213 int iParentIdx, /* Index of "the page" in pParent */
7214 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */
7215 int isRoot, /* True if pParent is a root-page */
7216 int bBulk /* True if this call is part of a bulk load */
7218 BtShared *pBt; /* The whole database */
7219 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
7220 int nNew = 0; /* Number of pages in apNew[] */
7221 int nOld; /* Number of pages in apOld[] */
7222 int i, j, k; /* Loop counters */
7223 int nxDiv; /* Next divider slot in pParent->aCell[] */
7224 int rc = SQLITE_OK; /* The return code */
7225 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */
7226 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
7227 int usableSpace; /* Bytes in pPage beyond the header */
7228 int pageFlags; /* Value of pPage->aData[0] */
7229 int iSpace1 = 0; /* First unused byte of aSpace1[] */
7230 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
7231 int szScratch; /* Size of scratch memory requested */
7232 MemPage *apOld[NB]; /* pPage and up to two siblings */
7233 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
7234 u8 *pRight; /* Location in parent of right-sibling pointer */
7235 u8 *apDiv[NB-1]; /* Divider cells in pParent */
7236 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
7237 int cntOld[NB+2]; /* Old index in b.apCell[] */
7238 int szNew[NB+2]; /* Combined size of cells placed on i-th page */
7239 u8 *aSpace1; /* Space for copies of dividers cells */
7240 Pgno pgno; /* Temp var to store a page number in */
7241 u8 abDone[NB+2]; /* True after i'th new page is populated */
7242 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */
7243 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */
7244 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */
7245 CellArray b; /* Parsed information on cells being balanced */
7247 memset(abDone, 0, sizeof(abDone));
7248 b.nCell = 0;
7249 b.apCell = 0;
7250 pBt = pParent->pBt;
7251 assert( sqlite3_mutex_held(pBt->mutex) );
7252 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7254 #if 0
7255 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
7256 #endif
7258 /* At this point pParent may have at most one overflow cell. And if
7259 ** this overflow cell is present, it must be the cell with
7260 ** index iParentIdx. This scenario comes about when this function
7261 ** is called (indirectly) from sqlite3BtreeDelete().
7263 assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
7264 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
7266 if( !aOvflSpace ){
7267 return SQLITE_NOMEM_BKPT;
7270 /* Find the sibling pages to balance. Also locate the cells in pParent
7271 ** that divide the siblings. An attempt is made to find NN siblings on
7272 ** either side of pPage. More siblings are taken from one side, however,
7273 ** if there are fewer than NN siblings on the other side. If pParent
7274 ** has NB or fewer children then all children of pParent are taken.
7276 ** This loop also drops the divider cells from the parent page. This
7277 ** way, the remainder of the function does not have to deal with any
7278 ** overflow cells in the parent page, since if any existed they will
7279 ** have already been removed.
7281 i = pParent->nOverflow + pParent->nCell;
7282 if( i<2 ){
7283 nxDiv = 0;
7284 }else{
7285 assert( bBulk==0 || bBulk==1 );
7286 if( iParentIdx==0 ){
7287 nxDiv = 0;
7288 }else if( iParentIdx==i ){
7289 nxDiv = i-2+bBulk;
7290 }else{
7291 nxDiv = iParentIdx-1;
7293 i = 2-bBulk;
7295 nOld = i+1;
7296 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
7297 pRight = &pParent->aData[pParent->hdrOffset+8];
7298 }else{
7299 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
7301 pgno = get4byte(pRight);
7302 while( 1 ){
7303 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
7304 if( rc ){
7305 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7306 goto balance_cleanup;
7308 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
7309 if( (i--)==0 ) break;
7311 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
7312 apDiv[i] = pParent->apOvfl[0];
7313 pgno = get4byte(apDiv[i]);
7314 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7315 pParent->nOverflow = 0;
7316 }else{
7317 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
7318 pgno = get4byte(apDiv[i]);
7319 szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
7321 /* Drop the cell from the parent page. apDiv[i] still points to
7322 ** the cell within the parent, even though it has been dropped.
7323 ** This is safe because dropping a cell only overwrites the first
7324 ** four bytes of it, and this function does not need the first
7325 ** four bytes of the divider cell. So the pointer is safe to use
7326 ** later on.
7328 ** But not if we are in secure-delete mode. In secure-delete mode,
7329 ** the dropCell() routine will overwrite the entire cell with zeroes.
7330 ** In this case, temporarily copy the cell into the aOvflSpace[]
7331 ** buffer. It will be copied out again as soon as the aSpace[] buffer
7332 ** is allocated. */
7333 if( pBt->btsFlags & BTS_FAST_SECURE ){
7334 int iOff;
7336 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
7337 if( (iOff+szNew[i])>(int)pBt->usableSize ){
7338 rc = SQLITE_CORRUPT_BKPT;
7339 memset(apOld, 0, (i+1)*sizeof(MemPage*));
7340 goto balance_cleanup;
7341 }else{
7342 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
7343 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
7346 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
7350 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
7351 ** alignment */
7352 nMaxCells = (nMaxCells + 3)&~3;
7355 ** Allocate space for memory structures
7357 szScratch =
7358 nMaxCells*sizeof(u8*) /* b.apCell */
7359 + nMaxCells*sizeof(u16) /* b.szCell */
7360 + pBt->pageSize; /* aSpace1 */
7362 assert( szScratch<=6*(int)pBt->pageSize );
7363 b.apCell = sqlite3StackAllocRaw(0, szScratch );
7364 if( b.apCell==0 ){
7365 rc = SQLITE_NOMEM_BKPT;
7366 goto balance_cleanup;
7368 b.szCell = (u16*)&b.apCell[nMaxCells];
7369 aSpace1 = (u8*)&b.szCell[nMaxCells];
7370 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
7373 ** Load pointers to all cells on sibling pages and the divider cells
7374 ** into the local b.apCell[] array. Make copies of the divider cells
7375 ** into space obtained from aSpace1[]. The divider cells have already
7376 ** been removed from pParent.
7378 ** If the siblings are on leaf pages, then the child pointers of the
7379 ** divider cells are stripped from the cells before they are copied
7380 ** into aSpace1[]. In this way, all cells in b.apCell[] are without
7381 ** child pointers. If siblings are not leaves, then all cell in
7382 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[]
7383 ** are alike.
7385 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
7386 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
7388 b.pRef = apOld[0];
7389 leafCorrection = b.pRef->leaf*4;
7390 leafData = b.pRef->intKeyLeaf;
7391 for(i=0; i<nOld; i++){
7392 MemPage *pOld = apOld[i];
7393 int limit = pOld->nCell;
7394 u8 *aData = pOld->aData;
7395 u16 maskPage = pOld->maskPage;
7396 u8 *piCell = aData + pOld->cellOffset;
7397 u8 *piEnd;
7399 /* Verify that all sibling pages are of the same "type" (table-leaf,
7400 ** table-interior, index-leaf, or index-interior).
7402 if( pOld->aData[0]!=apOld[0]->aData[0] ){
7403 rc = SQLITE_CORRUPT_BKPT;
7404 goto balance_cleanup;
7407 /* Load b.apCell[] with pointers to all cells in pOld. If pOld
7408 ** contains overflow cells, include them in the b.apCell[] array
7409 ** in the correct spot.
7411 ** Note that when there are multiple overflow cells, it is always the
7412 ** case that they are sequential and adjacent. This invariant arises
7413 ** because multiple overflows can only occurs when inserting divider
7414 ** cells into a parent on a prior balance, and divider cells are always
7415 ** adjacent and are inserted in order. There is an assert() tagged
7416 ** with "NOTE 1" in the overflow cell insertion loop to prove this
7417 ** invariant.
7419 ** This must be done in advance. Once the balance starts, the cell
7420 ** offset section of the btree page will be overwritten and we will no
7421 ** long be able to find the cells if a pointer to each cell is not saved
7422 ** first.
7424 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
7425 if( pOld->nOverflow>0 ){
7426 limit = pOld->aiOvfl[0];
7427 for(j=0; j<limit; j++){
7428 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7429 piCell += 2;
7430 b.nCell++;
7432 for(k=0; k<pOld->nOverflow; k++){
7433 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */
7434 b.apCell[b.nCell] = pOld->apOvfl[k];
7435 b.nCell++;
7438 piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
7439 while( piCell<piEnd ){
7440 assert( b.nCell<nMaxCells );
7441 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
7442 piCell += 2;
7443 b.nCell++;
7446 cntOld[i] = b.nCell;
7447 if( i<nOld-1 && !leafData){
7448 u16 sz = (u16)szNew[i];
7449 u8 *pTemp;
7450 assert( b.nCell<nMaxCells );
7451 b.szCell[b.nCell] = sz;
7452 pTemp = &aSpace1[iSpace1];
7453 iSpace1 += sz;
7454 assert( sz<=pBt->maxLocal+23 );
7455 assert( iSpace1 <= (int)pBt->pageSize );
7456 memcpy(pTemp, apDiv[i], sz);
7457 b.apCell[b.nCell] = pTemp+leafCorrection;
7458 assert( leafCorrection==0 || leafCorrection==4 );
7459 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
7460 if( !pOld->leaf ){
7461 assert( leafCorrection==0 );
7462 assert( pOld->hdrOffset==0 );
7463 /* The right pointer of the child page pOld becomes the left
7464 ** pointer of the divider cell */
7465 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
7466 }else{
7467 assert( leafCorrection==4 );
7468 while( b.szCell[b.nCell]<4 ){
7469 /* Do not allow any cells smaller than 4 bytes. If a smaller cell
7470 ** does exist, pad it with 0x00 bytes. */
7471 assert( b.szCell[b.nCell]==3 || CORRUPT_DB );
7472 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB );
7473 aSpace1[iSpace1++] = 0x00;
7474 b.szCell[b.nCell]++;
7477 b.nCell++;
7482 ** Figure out the number of pages needed to hold all b.nCell cells.
7483 ** Store this number in "k". Also compute szNew[] which is the total
7484 ** size of all cells on the i-th page and cntNew[] which is the index
7485 ** in b.apCell[] of the cell that divides page i from page i+1.
7486 ** cntNew[k] should equal b.nCell.
7488 ** Values computed by this block:
7490 ** k: The total number of sibling pages
7491 ** szNew[i]: Spaced used on the i-th sibling page.
7492 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to
7493 ** the right of the i-th sibling page.
7494 ** usableSpace: Number of bytes of space available on each sibling.
7497 usableSpace = pBt->usableSize - 12 + leafCorrection;
7498 for(i=0; i<nOld; i++){
7499 MemPage *p = apOld[i];
7500 szNew[i] = usableSpace - p->nFree;
7501 for(j=0; j<p->nOverflow; j++){
7502 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
7504 cntNew[i] = cntOld[i];
7506 k = nOld;
7507 for(i=0; i<k; i++){
7508 int sz;
7509 while( szNew[i]>usableSpace ){
7510 if( i+1>=k ){
7511 k = i+2;
7512 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
7513 szNew[k-1] = 0;
7514 cntNew[k-1] = b.nCell;
7516 sz = 2 + cachedCellSize(&b, cntNew[i]-1);
7517 szNew[i] -= sz;
7518 if( !leafData ){
7519 if( cntNew[i]<b.nCell ){
7520 sz = 2 + cachedCellSize(&b, cntNew[i]);
7521 }else{
7522 sz = 0;
7525 szNew[i+1] += sz;
7526 cntNew[i]--;
7528 while( cntNew[i]<b.nCell ){
7529 sz = 2 + cachedCellSize(&b, cntNew[i]);
7530 if( szNew[i]+sz>usableSpace ) break;
7531 szNew[i] += sz;
7532 cntNew[i]++;
7533 if( !leafData ){
7534 if( cntNew[i]<b.nCell ){
7535 sz = 2 + cachedCellSize(&b, cntNew[i]);
7536 }else{
7537 sz = 0;
7540 szNew[i+1] -= sz;
7542 if( cntNew[i]>=b.nCell ){
7543 k = i+1;
7544 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
7545 rc = SQLITE_CORRUPT_BKPT;
7546 goto balance_cleanup;
7551 ** The packing computed by the previous block is biased toward the siblings
7552 ** on the left side (siblings with smaller keys). The left siblings are
7553 ** always nearly full, while the right-most sibling might be nearly empty.
7554 ** The next block of code attempts to adjust the packing of siblings to
7555 ** get a better balance.
7557 ** This adjustment is more than an optimization. The packing above might
7558 ** be so out of balance as to be illegal. For example, the right-most
7559 ** sibling might be completely empty. This adjustment is not optional.
7561 for(i=k-1; i>0; i--){
7562 int szRight = szNew[i]; /* Size of sibling on the right */
7563 int szLeft = szNew[i-1]; /* Size of sibling on the left */
7564 int r; /* Index of right-most cell in left sibling */
7565 int d; /* Index of first cell to the left of right sibling */
7567 r = cntNew[i-1] - 1;
7568 d = r + 1 - leafData;
7569 (void)cachedCellSize(&b, d);
7571 assert( d<nMaxCells );
7572 assert( r<nMaxCells );
7573 (void)cachedCellSize(&b, r);
7574 if( szRight!=0
7575 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
7576 break;
7578 szRight += b.szCell[d] + 2;
7579 szLeft -= b.szCell[r] + 2;
7580 cntNew[i-1] = r;
7581 r--;
7582 d--;
7583 }while( r>=0 );
7584 szNew[i] = szRight;
7585 szNew[i-1] = szLeft;
7586 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
7587 rc = SQLITE_CORRUPT_BKPT;
7588 goto balance_cleanup;
7592 /* Sanity check: For a non-corrupt database file one of the follwing
7593 ** must be true:
7594 ** (1) We found one or more cells (cntNew[0])>0), or
7595 ** (2) pPage is a virtual root page. A virtual root page is when
7596 ** the real root page is page 1 and we are the only child of
7597 ** that page.
7599 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB);
7600 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n",
7601 apOld[0]->pgno, apOld[0]->nCell,
7602 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0,
7603 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0
7607 ** Allocate k new pages. Reuse old pages where possible.
7609 pageFlags = apOld[0]->aData[0];
7610 for(i=0; i<k; i++){
7611 MemPage *pNew;
7612 if( i<nOld ){
7613 pNew = apNew[i] = apOld[i];
7614 apOld[i] = 0;
7615 rc = sqlite3PagerWrite(pNew->pDbPage);
7616 nNew++;
7617 if( rc ) goto balance_cleanup;
7618 }else{
7619 assert( i>0 );
7620 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
7621 if( rc ) goto balance_cleanup;
7622 zeroPage(pNew, pageFlags);
7623 apNew[i] = pNew;
7624 nNew++;
7625 cntOld[i] = b.nCell;
7627 /* Set the pointer-map entry for the new sibling page. */
7628 if( ISAUTOVACUUM ){
7629 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
7630 if( rc!=SQLITE_OK ){
7631 goto balance_cleanup;
7638 ** Reassign page numbers so that the new pages are in ascending order.
7639 ** This helps to keep entries in the disk file in order so that a scan
7640 ** of the table is closer to a linear scan through the file. That in turn
7641 ** helps the operating system to deliver pages from the disk more rapidly.
7643 ** An O(n^2) insertion sort algorithm is used, but since n is never more
7644 ** than (NB+2) (a small constant), that should not be a problem.
7646 ** When NB==3, this one optimization makes the database about 25% faster
7647 ** for large insertions and deletions.
7649 for(i=0; i<nNew; i++){
7650 aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
7651 aPgFlags[i] = apNew[i]->pDbPage->flags;
7652 for(j=0; j<i; j++){
7653 if( aPgno[j]==aPgno[i] ){
7654 /* This branch is taken if the set of sibling pages somehow contains
7655 ** duplicate entries. This can happen if the database is corrupt.
7656 ** It would be simpler to detect this as part of the loop below, but
7657 ** we do the detection here in order to avoid populating the pager
7658 ** cache with two separate objects associated with the same
7659 ** page number. */
7660 assert( CORRUPT_DB );
7661 rc = SQLITE_CORRUPT_BKPT;
7662 goto balance_cleanup;
7666 for(i=0; i<nNew; i++){
7667 int iBest = 0; /* aPgno[] index of page number to use */
7668 for(j=1; j<nNew; j++){
7669 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
7671 pgno = aPgOrder[iBest];
7672 aPgOrder[iBest] = 0xffffffff;
7673 if( iBest!=i ){
7674 if( iBest>i ){
7675 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
7677 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
7678 apNew[i]->pgno = pgno;
7682 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) "
7683 "%d(%d nc=%d) %d(%d nc=%d)\n",
7684 apNew[0]->pgno, szNew[0], cntNew[0],
7685 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
7686 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0,
7687 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
7688 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0,
7689 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
7690 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
7691 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
7692 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
7695 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7696 put4byte(pRight, apNew[nNew-1]->pgno);
7698 /* If the sibling pages are not leaves, ensure that the right-child pointer
7699 ** of the right-most new sibling page is set to the value that was
7700 ** originally in the same field of the right-most old sibling page. */
7701 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
7702 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
7703 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
7706 /* Make any required updates to pointer map entries associated with
7707 ** cells stored on sibling pages following the balance operation. Pointer
7708 ** map entries associated with divider cells are set by the insertCell()
7709 ** routine. The associated pointer map entries are:
7711 ** a) if the cell contains a reference to an overflow chain, the
7712 ** entry associated with the first page in the overflow chain, and
7714 ** b) if the sibling pages are not leaves, the child page associated
7715 ** with the cell.
7717 ** If the sibling pages are not leaves, then the pointer map entry
7718 ** associated with the right-child of each sibling may also need to be
7719 ** updated. This happens below, after the sibling pages have been
7720 ** populated, not here.
7722 if( ISAUTOVACUUM ){
7723 MemPage *pNew = apNew[0];
7724 u8 *aOld = pNew->aData;
7725 int cntOldNext = pNew->nCell + pNew->nOverflow;
7726 int usableSize = pBt->usableSize;
7727 int iNew = 0;
7728 int iOld = 0;
7730 for(i=0; i<b.nCell; i++){
7731 u8 *pCell = b.apCell[i];
7732 if( i==cntOldNext ){
7733 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld];
7734 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
7735 aOld = pOld->aData;
7737 if( i==cntNew[iNew] ){
7738 pNew = apNew[++iNew];
7739 if( !leafData ) continue;
7742 /* Cell pCell is destined for new sibling page pNew. Originally, it
7743 ** was either part of sibling page iOld (possibly an overflow cell),
7744 ** or else the divider cell to the left of sibling page iOld. So,
7745 ** if sibling page iOld had the same page number as pNew, and if
7746 ** pCell really was a part of sibling page iOld (not a divider or
7747 ** overflow cell), we can skip updating the pointer map entries. */
7748 if( iOld>=nNew
7749 || pNew->pgno!=aPgno[iOld]
7750 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize])
7752 if( !leafCorrection ){
7753 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc);
7755 if( cachedCellSize(&b,i)>pNew->minLocal ){
7756 ptrmapPutOvflPtr(pNew, pCell, &rc);
7758 if( rc ) goto balance_cleanup;
7763 /* Insert new divider cells into pParent. */
7764 for(i=0; i<nNew-1; i++){
7765 u8 *pCell;
7766 u8 *pTemp;
7767 int sz;
7768 MemPage *pNew = apNew[i];
7769 j = cntNew[i];
7771 assert( j<nMaxCells );
7772 assert( b.apCell[j]!=0 );
7773 pCell = b.apCell[j];
7774 sz = b.szCell[j] + leafCorrection;
7775 pTemp = &aOvflSpace[iOvflSpace];
7776 if( !pNew->leaf ){
7777 memcpy(&pNew->aData[8], pCell, 4);
7778 }else if( leafData ){
7779 /* If the tree is a leaf-data tree, and the siblings are leaves,
7780 ** then there is no divider cell in b.apCell[]. Instead, the divider
7781 ** cell consists of the integer key for the right-most cell of
7782 ** the sibling-page assembled above only.
7784 CellInfo info;
7785 j--;
7786 pNew->xParseCell(pNew, b.apCell[j], &info);
7787 pCell = pTemp;
7788 sz = 4 + putVarint(&pCell[4], info.nKey);
7789 pTemp = 0;
7790 }else{
7791 pCell -= 4;
7792 /* Obscure case for non-leaf-data trees: If the cell at pCell was
7793 ** previously stored on a leaf node, and its reported size was 4
7794 ** bytes, then it may actually be smaller than this
7795 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
7796 ** any cell). But it is important to pass the correct size to
7797 ** insertCell(), so reparse the cell now.
7799 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)"
7800 ** and WITHOUT ROWID tables with exactly one column which is the
7801 ** primary key.
7803 if( b.szCell[j]==4 ){
7804 assert(leafCorrection==4);
7805 sz = pParent->xCellSize(pParent, pCell);
7808 iOvflSpace += sz;
7809 assert( sz<=pBt->maxLocal+23 );
7810 assert( iOvflSpace <= (int)pBt->pageSize );
7811 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
7812 if( rc!=SQLITE_OK ) goto balance_cleanup;
7813 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
7816 /* Now update the actual sibling pages. The order in which they are updated
7817 ** is important, as this code needs to avoid disrupting any page from which
7818 ** cells may still to be read. In practice, this means:
7820 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1])
7821 ** then it is not safe to update page apNew[iPg] until after
7822 ** the left-hand sibling apNew[iPg-1] has been updated.
7824 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1])
7825 ** then it is not safe to update page apNew[iPg] until after
7826 ** the right-hand sibling apNew[iPg+1] has been updated.
7828 ** If neither of the above apply, the page is safe to update.
7830 ** The iPg value in the following loop starts at nNew-1 goes down
7831 ** to 0, then back up to nNew-1 again, thus making two passes over
7832 ** the pages. On the initial downward pass, only condition (1) above
7833 ** needs to be tested because (2) will always be true from the previous
7834 ** step. On the upward pass, both conditions are always true, so the
7835 ** upwards pass simply processes pages that were missed on the downward
7836 ** pass.
7838 for(i=1-nNew; i<nNew; i++){
7839 int iPg = i<0 ? -i : i;
7840 assert( iPg>=0 && iPg<nNew );
7841 if( abDone[iPg] ) continue; /* Skip pages already processed */
7842 if( i>=0 /* On the upwards pass, or... */
7843 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */
7845 int iNew;
7846 int iOld;
7847 int nNewCell;
7849 /* Verify condition (1): If cells are moving left, update iPg
7850 ** only after iPg-1 has already been updated. */
7851 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
7853 /* Verify condition (2): If cells are moving right, update iPg
7854 ** only after iPg+1 has already been updated. */
7855 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
7857 if( iPg==0 ){
7858 iNew = iOld = 0;
7859 nNewCell = cntNew[0];
7860 }else{
7861 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
7862 iNew = cntNew[iPg-1] + !leafData;
7863 nNewCell = cntNew[iPg] - iNew;
7866 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
7867 if( rc ) goto balance_cleanup;
7868 abDone[iPg]++;
7869 apNew[iPg]->nFree = usableSpace-szNew[iPg];
7870 assert( apNew[iPg]->nOverflow==0 );
7871 assert( apNew[iPg]->nCell==nNewCell );
7875 /* All pages have been processed exactly once */
7876 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
7878 assert( nOld>0 );
7879 assert( nNew>0 );
7881 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
7882 /* The root page of the b-tree now contains no cells. The only sibling
7883 ** page is the right-child of the parent. Copy the contents of the
7884 ** child page into the parent, decreasing the overall height of the
7885 ** b-tree structure by one. This is described as the "balance-shallower"
7886 ** sub-algorithm in some documentation.
7888 ** If this is an auto-vacuum database, the call to copyNodeContent()
7889 ** sets all pointer-map entries corresponding to database image pages
7890 ** for which the pointer is stored within the content being copied.
7892 ** It is critical that the child page be defragmented before being
7893 ** copied into the parent, because if the parent is page 1 then it will
7894 ** by smaller than the child due to the database header, and so all the
7895 ** free space needs to be up front.
7897 assert( nNew==1 || CORRUPT_DB );
7898 rc = defragmentPage(apNew[0], -1);
7899 testcase( rc!=SQLITE_OK );
7900 assert( apNew[0]->nFree ==
7901 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
7902 || rc!=SQLITE_OK
7904 copyNodeContent(apNew[0], pParent, &rc);
7905 freePage(apNew[0], &rc);
7906 }else if( ISAUTOVACUUM && !leafCorrection ){
7907 /* Fix the pointer map entries associated with the right-child of each
7908 ** sibling page. All other pointer map entries have already been taken
7909 ** care of. */
7910 for(i=0; i<nNew; i++){
7911 u32 key = get4byte(&apNew[i]->aData[8]);
7912 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
7916 assert( pParent->isInit );
7917 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
7918 nOld, nNew, b.nCell));
7920 /* Free any old pages that were not reused as new pages.
7922 for(i=nNew; i<nOld; i++){
7923 freePage(apOld[i], &rc);
7926 #if 0
7927 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){
7928 /* The ptrmapCheckPages() contains assert() statements that verify that
7929 ** all pointer map pages are set correctly. This is helpful while
7930 ** debugging. This is usually disabled because a corrupt database may
7931 ** cause an assert() statement to fail. */
7932 ptrmapCheckPages(apNew, nNew);
7933 ptrmapCheckPages(&pParent, 1);
7935 #endif
7938 ** Cleanup before returning.
7940 balance_cleanup:
7941 sqlite3StackFree(0, b.apCell);
7942 for(i=0; i<nOld; i++){
7943 releasePage(apOld[i]);
7945 for(i=0; i<nNew; i++){
7946 releasePage(apNew[i]);
7949 return rc;
7954 ** This function is called when the root page of a b-tree structure is
7955 ** overfull (has one or more overflow pages).
7957 ** A new child page is allocated and the contents of the current root
7958 ** page, including overflow cells, are copied into the child. The root
7959 ** page is then overwritten to make it an empty page with the right-child
7960 ** pointer pointing to the new page.
7962 ** Before returning, all pointer-map entries corresponding to pages
7963 ** that the new child-page now contains pointers to are updated. The
7964 ** entry corresponding to the new right-child pointer of the root
7965 ** page is also updated.
7967 ** If successful, *ppChild is set to contain a reference to the child
7968 ** page and SQLITE_OK is returned. In this case the caller is required
7969 ** to call releasePage() on *ppChild exactly once. If an error occurs,
7970 ** an error code is returned and *ppChild is set to 0.
7972 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
7973 int rc; /* Return value from subprocedures */
7974 MemPage *pChild = 0; /* Pointer to a new child page */
7975 Pgno pgnoChild = 0; /* Page number of the new child page */
7976 BtShared *pBt = pRoot->pBt; /* The BTree */
7978 assert( pRoot->nOverflow>0 );
7979 assert( sqlite3_mutex_held(pBt->mutex) );
7981 /* Make pRoot, the root page of the b-tree, writable. Allocate a new
7982 ** page that will become the new right-child of pPage. Copy the contents
7983 ** of the node stored on pRoot into the new child page.
7985 rc = sqlite3PagerWrite(pRoot->pDbPage);
7986 if( rc==SQLITE_OK ){
7987 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
7988 copyNodeContent(pRoot, pChild, &rc);
7989 if( ISAUTOVACUUM ){
7990 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
7993 if( rc ){
7994 *ppChild = 0;
7995 releasePage(pChild);
7996 return rc;
7998 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
7999 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8000 assert( pChild->nCell==pRoot->nCell );
8002 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
8004 /* Copy the overflow cells from pRoot to pChild */
8005 memcpy(pChild->aiOvfl, pRoot->aiOvfl,
8006 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
8007 memcpy(pChild->apOvfl, pRoot->apOvfl,
8008 pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
8009 pChild->nOverflow = pRoot->nOverflow;
8011 /* Zero the contents of pRoot. Then install pChild as the right-child. */
8012 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
8013 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
8015 *ppChild = pChild;
8016 return SQLITE_OK;
8020 ** The page that pCur currently points to has just been modified in
8021 ** some way. This function figures out if this modification means the
8022 ** tree needs to be balanced, and if so calls the appropriate balancing
8023 ** routine. Balancing routines are:
8025 ** balance_quick()
8026 ** balance_deeper()
8027 ** balance_nonroot()
8029 static int balance(BtCursor *pCur){
8030 int rc = SQLITE_OK;
8031 const int nMin = pCur->pBt->usableSize * 2 / 3;
8032 u8 aBalanceQuickSpace[13];
8033 u8 *pFree = 0;
8035 VVA_ONLY( int balance_quick_called = 0 );
8036 VVA_ONLY( int balance_deeper_called = 0 );
8038 do {
8039 int iPage = pCur->iPage;
8040 MemPage *pPage = pCur->pPage;
8042 if( iPage==0 ){
8043 if( pPage->nOverflow ){
8044 /* The root page of the b-tree is overfull. In this case call the
8045 ** balance_deeper() function to create a new child for the root-page
8046 ** and copy the current contents of the root-page to it. The
8047 ** next iteration of the do-loop will balance the child page.
8049 assert( balance_deeper_called==0 );
8050 VVA_ONLY( balance_deeper_called++ );
8051 rc = balance_deeper(pPage, &pCur->apPage[1]);
8052 if( rc==SQLITE_OK ){
8053 pCur->iPage = 1;
8054 pCur->ix = 0;
8055 pCur->aiIdx[0] = 0;
8056 pCur->apPage[0] = pPage;
8057 pCur->pPage = pCur->apPage[1];
8058 assert( pCur->pPage->nOverflow );
8060 }else{
8061 break;
8063 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
8064 break;
8065 }else{
8066 MemPage * const pParent = pCur->apPage[iPage-1];
8067 int const iIdx = pCur->aiIdx[iPage-1];
8069 rc = sqlite3PagerWrite(pParent->pDbPage);
8070 if( rc==SQLITE_OK ){
8071 #ifndef SQLITE_OMIT_QUICKBALANCE
8072 if( pPage->intKeyLeaf
8073 && pPage->nOverflow==1
8074 && pPage->aiOvfl[0]==pPage->nCell
8075 && pParent->pgno!=1
8076 && pParent->nCell==iIdx
8078 /* Call balance_quick() to create a new sibling of pPage on which
8079 ** to store the overflow cell. balance_quick() inserts a new cell
8080 ** into pParent, which may cause pParent overflow. If this
8081 ** happens, the next iteration of the do-loop will balance pParent
8082 ** use either balance_nonroot() or balance_deeper(). Until this
8083 ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
8084 ** buffer.
8086 ** The purpose of the following assert() is to check that only a
8087 ** single call to balance_quick() is made for each call to this
8088 ** function. If this were not verified, a subtle bug involving reuse
8089 ** of the aBalanceQuickSpace[] might sneak in.
8091 assert( balance_quick_called==0 );
8092 VVA_ONLY( balance_quick_called++ );
8093 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
8094 }else
8095 #endif
8097 /* In this case, call balance_nonroot() to redistribute cells
8098 ** between pPage and up to 2 of its sibling pages. This involves
8099 ** modifying the contents of pParent, which may cause pParent to
8100 ** become overfull or underfull. The next iteration of the do-loop
8101 ** will balance the parent page to correct this.
8103 ** If the parent page becomes overfull, the overflow cell or cells
8104 ** are stored in the pSpace buffer allocated immediately below.
8105 ** A subsequent iteration of the do-loop will deal with this by
8106 ** calling balance_nonroot() (balance_deeper() may be called first,
8107 ** but it doesn't deal with overflow cells - just moves them to a
8108 ** different page). Once this subsequent call to balance_nonroot()
8109 ** has completed, it is safe to release the pSpace buffer used by
8110 ** the previous call, as the overflow cell data will have been
8111 ** copied either into the body of a database page or into the new
8112 ** pSpace buffer passed to the latter call to balance_nonroot().
8114 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
8115 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
8116 pCur->hints&BTREE_BULKLOAD);
8117 if( pFree ){
8118 /* If pFree is not NULL, it points to the pSpace buffer used
8119 ** by a previous call to balance_nonroot(). Its contents are
8120 ** now stored either on real database pages or within the
8121 ** new pSpace buffer, so it may be safely freed here. */
8122 sqlite3PageFree(pFree);
8125 /* The pSpace buffer will be freed after the next call to
8126 ** balance_nonroot(), or just before this function returns, whichever
8127 ** comes first. */
8128 pFree = pSpace;
8132 pPage->nOverflow = 0;
8134 /* The next iteration of the do-loop balances the parent page. */
8135 releasePage(pPage);
8136 pCur->iPage--;
8137 assert( pCur->iPage>=0 );
8138 pCur->pPage = pCur->apPage[pCur->iPage];
8140 }while( rc==SQLITE_OK );
8142 if( pFree ){
8143 sqlite3PageFree(pFree);
8145 return rc;
8150 ** Insert a new record into the BTree. The content of the new record
8151 ** is described by the pX object. The pCur cursor is used only to
8152 ** define what table the record should be inserted into, and is left
8153 ** pointing at a random location.
8155 ** For a table btree (used for rowid tables), only the pX.nKey value of
8156 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the
8157 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields
8158 ** hold the content of the row.
8160 ** For an index btree (used for indexes and WITHOUT ROWID tables), the
8161 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The
8162 ** pX.pData,nData,nZero fields must be zero.
8164 ** If the seekResult parameter is non-zero, then a successful call to
8165 ** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already
8166 ** been performed. In other words, if seekResult!=0 then the cursor
8167 ** is currently pointing to a cell that will be adjacent to the cell
8168 ** to be inserted. If seekResult<0 then pCur points to a cell that is
8169 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell
8170 ** that is larger than (pKey,nKey).
8172 ** If seekResult==0, that means pCur is pointing at some unknown location.
8173 ** In that case, this routine must seek the cursor to the correct insertion
8174 ** point for (pKey,nKey) before doing the insertion. For index btrees,
8175 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked
8176 ** key values and pX->aMem can be used instead of pX->pKey to avoid having
8177 ** to decode the key.
8179 int sqlite3BtreeInsert(
8180 BtCursor *pCur, /* Insert data into the table of this cursor */
8181 const BtreePayload *pX, /* Content of the row to be inserted */
8182 int flags, /* True if this is likely an append */
8183 int seekResult /* Result of prior MovetoUnpacked() call */
8185 int rc;
8186 int loc = seekResult; /* -1: before desired location +1: after */
8187 int szNew = 0;
8188 int idx;
8189 MemPage *pPage;
8190 Btree *p = pCur->pBtree;
8191 BtShared *pBt = p->pBt;
8192 unsigned char *oldCell;
8193 unsigned char *newCell = 0;
8195 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags );
8197 if( pCur->eState==CURSOR_FAULT ){
8198 assert( pCur->skipNext!=SQLITE_OK );
8199 return pCur->skipNext;
8202 assert( cursorOwnsBtShared(pCur) );
8203 assert( (pCur->curFlags & BTCF_WriteFlag)!=0
8204 && pBt->inTransaction==TRANS_WRITE
8205 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
8206 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
8208 /* Assert that the caller has been consistent. If this cursor was opened
8209 ** expecting an index b-tree, then the caller should be inserting blob
8210 ** keys with no associated data. If the cursor was opened expecting an
8211 ** intkey table, the caller should be inserting integer keys with a
8212 ** blob of associated data. */
8213 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
8215 /* Save the positions of any other cursors open on this table.
8217 ** In some cases, the call to btreeMoveto() below is a no-op. For
8218 ** example, when inserting data into a table with auto-generated integer
8219 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
8220 ** integer key to use. It then calls this function to actually insert the
8221 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
8222 ** that the cursor is already where it needs to be and returns without
8223 ** doing any work. To avoid thwarting these optimizations, it is important
8224 ** not to clear the cursor here.
8226 if( pCur->curFlags & BTCF_Multiple ){
8227 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8228 if( rc ) return rc;
8231 if( pCur->pKeyInfo==0 ){
8232 assert( pX->pKey==0 );
8233 /* If this is an insert into a table b-tree, invalidate any incrblob
8234 ** cursors open on the row being replaced */
8235 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
8237 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing
8238 ** to a row with the same key as the new entry being inserted. */
8239 assert( (flags & BTREE_SAVEPOSITION)==0 ||
8240 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) );
8242 /* If the cursor is currently on the last row and we are appending a
8243 ** new row onto the end, set the "loc" to avoid an unnecessary
8244 ** btreeMoveto() call */
8245 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){
8246 loc = 0;
8247 }else if( loc==0 ){
8248 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
8249 if( rc ) return rc;
8251 }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){
8252 if( pX->nMem ){
8253 UnpackedRecord r;
8254 r.pKeyInfo = pCur->pKeyInfo;
8255 r.aMem = pX->aMem;
8256 r.nField = pX->nMem;
8257 r.default_rc = 0;
8258 r.errCode = 0;
8259 r.r1 = 0;
8260 r.r2 = 0;
8261 r.eqSeen = 0;
8262 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
8263 }else{
8264 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
8266 if( rc ) return rc;
8268 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
8270 pPage = pCur->pPage;
8271 assert( pPage->intKey || pX->nKey>=0 );
8272 assert( pPage->leaf || !pPage->intKey );
8274 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
8275 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno,
8276 loc==0 ? "overwrite" : "new entry"));
8277 assert( pPage->isInit );
8278 newCell = pBt->pTmpSpace;
8279 assert( newCell!=0 );
8280 rc = fillInCell(pPage, newCell, pX, &szNew);
8281 if( rc ) goto end_insert;
8282 assert( szNew==pPage->xCellSize(pPage, newCell) );
8283 assert( szNew <= MX_CELL_SIZE(pBt) );
8284 idx = pCur->ix;
8285 if( loc==0 ){
8286 CellInfo info;
8287 assert( idx<pPage->nCell );
8288 rc = sqlite3PagerWrite(pPage->pDbPage);
8289 if( rc ){
8290 goto end_insert;
8292 oldCell = findCell(pPage, idx);
8293 if( !pPage->leaf ){
8294 memcpy(newCell, oldCell, 4);
8296 rc = clearCell(pPage, oldCell, &info);
8297 if( info.nSize==szNew && info.nLocal==info.nPayload
8298 && (!ISAUTOVACUUM || szNew<pPage->minLocal)
8300 /* Overwrite the old cell with the new if they are the same size.
8301 ** We could also try to do this if the old cell is smaller, then add
8302 ** the leftover space to the free list. But experiments show that
8303 ** doing that is no faster then skipping this optimization and just
8304 ** calling dropCell() and insertCell().
8306 ** This optimization cannot be used on an autovacuum database if the
8307 ** new entry uses overflow pages, as the insertCell() call below is
8308 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */
8309 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
8310 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
8311 memcpy(oldCell, newCell, szNew);
8312 return SQLITE_OK;
8314 dropCell(pPage, idx, info.nSize, &rc);
8315 if( rc ) goto end_insert;
8316 }else if( loc<0 && pPage->nCell>0 ){
8317 assert( pPage->leaf );
8318 idx = ++pCur->ix;
8319 pCur->curFlags &= ~BTCF_ValidNKey;
8320 }else{
8321 assert( pPage->leaf );
8323 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
8324 assert( pPage->nOverflow==0 || rc==SQLITE_OK );
8325 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
8327 /* If no error has occurred and pPage has an overflow cell, call balance()
8328 ** to redistribute the cells within the tree. Since balance() may move
8329 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey
8330 ** variables.
8332 ** Previous versions of SQLite called moveToRoot() to move the cursor
8333 ** back to the root page as balance() used to invalidate the contents
8334 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
8335 ** set the cursor state to "invalid". This makes common insert operations
8336 ** slightly faster.
8338 ** There is a subtle but important optimization here too. When inserting
8339 ** multiple records into an intkey b-tree using a single cursor (as can
8340 ** happen while processing an "INSERT INTO ... SELECT" statement), it
8341 ** is advantageous to leave the cursor pointing to the last entry in
8342 ** the b-tree if possible. If the cursor is left pointing to the last
8343 ** entry in the table, and the next row inserted has an integer key
8344 ** larger than the largest existing key, it is possible to insert the
8345 ** row without seeking the cursor. This can be a big performance boost.
8347 pCur->info.nSize = 0;
8348 if( pPage->nOverflow ){
8349 assert( rc==SQLITE_OK );
8350 pCur->curFlags &= ~(BTCF_ValidNKey);
8351 rc = balance(pCur);
8353 /* Must make sure nOverflow is reset to zero even if the balance()
8354 ** fails. Internal data structure corruption will result otherwise.
8355 ** Also, set the cursor state to invalid. This stops saveCursorPosition()
8356 ** from trying to save the current position of the cursor. */
8357 pCur->pPage->nOverflow = 0;
8358 pCur->eState = CURSOR_INVALID;
8359 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){
8360 btreeReleaseAllCursorPages(pCur);
8361 if( pCur->pKeyInfo ){
8362 assert( pCur->pKey==0 );
8363 pCur->pKey = sqlite3Malloc( pX->nKey );
8364 if( pCur->pKey==0 ){
8365 rc = SQLITE_NOMEM;
8366 }else{
8367 memcpy(pCur->pKey, pX->pKey, pX->nKey);
8370 pCur->eState = CURSOR_REQUIRESEEK;
8371 pCur->nKey = pX->nKey;
8374 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
8376 end_insert:
8377 return rc;
8381 ** Delete the entry that the cursor is pointing to.
8383 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
8384 ** the cursor is left pointing at an arbitrary location after the delete.
8385 ** But if that bit is set, then the cursor is left in a state such that
8386 ** the next call to BtreeNext() or BtreePrev() moves it to the same row
8387 ** as it would have been on if the call to BtreeDelete() had been omitted.
8389 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
8390 ** associated with a single table entry and its indexes. Only one of those
8391 ** deletes is considered the "primary" delete. The primary delete occurs
8392 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
8393 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
8394 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
8395 ** but which might be used by alternative storage engines.
8397 int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
8398 Btree *p = pCur->pBtree;
8399 BtShared *pBt = p->pBt;
8400 int rc; /* Return code */
8401 MemPage *pPage; /* Page to delete cell from */
8402 unsigned char *pCell; /* Pointer to cell to delete */
8403 int iCellIdx; /* Index of cell to delete */
8404 int iCellDepth; /* Depth of node containing pCell */
8405 CellInfo info; /* Size of the cell being deleted */
8406 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
8407 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
8409 assert( cursorOwnsBtShared(pCur) );
8410 assert( pBt->inTransaction==TRANS_WRITE );
8411 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8412 assert( pCur->curFlags & BTCF_WriteFlag );
8413 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
8414 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
8415 assert( pCur->ix<pCur->pPage->nCell );
8416 assert( pCur->eState==CURSOR_VALID );
8417 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
8419 iCellDepth = pCur->iPage;
8420 iCellIdx = pCur->ix;
8421 pPage = pCur->pPage;
8422 pCell = findCell(pPage, iCellIdx);
8424 /* If the bPreserve flag is set to true, then the cursor position must
8425 ** be preserved following this delete operation. If the current delete
8426 ** will cause a b-tree rebalance, then this is done by saving the cursor
8427 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before
8428 ** returning.
8430 ** Or, if the current delete will not cause a rebalance, then the cursor
8431 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately
8432 ** before or after the deleted entry. In this case set bSkipnext to true. */
8433 if( bPreserve ){
8434 if( !pPage->leaf
8435 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
8437 /* A b-tree rebalance will be required after deleting this entry.
8438 ** Save the cursor key. */
8439 rc = saveCursorKey(pCur);
8440 if( rc ) return rc;
8441 }else{
8442 bSkipnext = 1;
8446 /* If the page containing the entry to delete is not a leaf page, move
8447 ** the cursor to the largest entry in the tree that is smaller than
8448 ** the entry being deleted. This cell will replace the cell being deleted
8449 ** from the internal node. The 'previous' entry is used for this instead
8450 ** of the 'next' entry, as the previous entry is always a part of the
8451 ** sub-tree headed by the child page of the cell being deleted. This makes
8452 ** balancing the tree following the delete operation easier. */
8453 if( !pPage->leaf ){
8454 rc = sqlite3BtreePrevious(pCur, 0);
8455 assert( rc!=SQLITE_DONE );
8456 if( rc ) return rc;
8459 /* Save the positions of any other cursors open on this table before
8460 ** making any modifications. */
8461 if( pCur->curFlags & BTCF_Multiple ){
8462 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
8463 if( rc ) return rc;
8466 /* If this is a delete operation to remove a row from a table b-tree,
8467 ** invalidate any incrblob cursors open on the row being deleted. */
8468 if( pCur->pKeyInfo==0 ){
8469 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
8472 /* Make the page containing the entry to be deleted writable. Then free any
8473 ** overflow pages associated with the entry and finally remove the cell
8474 ** itself from within the page. */
8475 rc = sqlite3PagerWrite(pPage->pDbPage);
8476 if( rc ) return rc;
8477 rc = clearCell(pPage, pCell, &info);
8478 dropCell(pPage, iCellIdx, info.nSize, &rc);
8479 if( rc ) return rc;
8481 /* If the cell deleted was not located on a leaf page, then the cursor
8482 ** is currently pointing to the largest entry in the sub-tree headed
8483 ** by the child-page of the cell that was just deleted from an internal
8484 ** node. The cell from the leaf node needs to be moved to the internal
8485 ** node to replace the deleted cell. */
8486 if( !pPage->leaf ){
8487 MemPage *pLeaf = pCur->pPage;
8488 int nCell;
8489 Pgno n;
8490 unsigned char *pTmp;
8492 if( iCellDepth<pCur->iPage-1 ){
8493 n = pCur->apPage[iCellDepth+1]->pgno;
8494 }else{
8495 n = pCur->pPage->pgno;
8497 pCell = findCell(pLeaf, pLeaf->nCell-1);
8498 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT;
8499 nCell = pLeaf->xCellSize(pLeaf, pCell);
8500 assert( MX_CELL_SIZE(pBt) >= nCell );
8501 pTmp = pBt->pTmpSpace;
8502 assert( pTmp!=0 );
8503 rc = sqlite3PagerWrite(pLeaf->pDbPage);
8504 if( rc==SQLITE_OK ){
8505 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
8507 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
8508 if( rc ) return rc;
8511 /* Balance the tree. If the entry deleted was located on a leaf page,
8512 ** then the cursor still points to that page. In this case the first
8513 ** call to balance() repairs the tree, and the if(...) condition is
8514 ** never true.
8516 ** Otherwise, if the entry deleted was on an internal node page, then
8517 ** pCur is pointing to the leaf page from which a cell was removed to
8518 ** replace the cell deleted from the internal node. This is slightly
8519 ** tricky as the leaf node may be underfull, and the internal node may
8520 ** be either under or overfull. In this case run the balancing algorithm
8521 ** on the leaf node first. If the balance proceeds far enough up the
8522 ** tree that we can be sure that any problem in the internal node has
8523 ** been corrected, so be it. Otherwise, after balancing the leaf node,
8524 ** walk the cursor up the tree to the internal node and balance it as
8525 ** well. */
8526 rc = balance(pCur);
8527 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
8528 releasePageNotNull(pCur->pPage);
8529 pCur->iPage--;
8530 while( pCur->iPage>iCellDepth ){
8531 releasePage(pCur->apPage[pCur->iPage--]);
8533 pCur->pPage = pCur->apPage[pCur->iPage];
8534 rc = balance(pCur);
8537 if( rc==SQLITE_OK ){
8538 if( bSkipnext ){
8539 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
8540 assert( pPage==pCur->pPage || CORRUPT_DB );
8541 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
8542 pCur->eState = CURSOR_SKIPNEXT;
8543 if( iCellIdx>=pPage->nCell ){
8544 pCur->skipNext = -1;
8545 pCur->ix = pPage->nCell-1;
8546 }else{
8547 pCur->skipNext = 1;
8549 }else{
8550 rc = moveToRoot(pCur);
8551 if( bPreserve ){
8552 btreeReleaseAllCursorPages(pCur);
8553 pCur->eState = CURSOR_REQUIRESEEK;
8555 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK;
8558 return rc;
8562 ** Create a new BTree table. Write into *piTable the page
8563 ** number for the root page of the new table.
8565 ** The type of type is determined by the flags parameter. Only the
8566 ** following values of flags are currently in use. Other values for
8567 ** flags might not work:
8569 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
8570 ** BTREE_ZERODATA Used for SQL indices
8572 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
8573 BtShared *pBt = p->pBt;
8574 MemPage *pRoot;
8575 Pgno pgnoRoot;
8576 int rc;
8577 int ptfFlags; /* Page-type flage for the root page of new table */
8579 assert( sqlite3BtreeHoldsMutex(p) );
8580 assert( pBt->inTransaction==TRANS_WRITE );
8581 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
8583 #ifdef SQLITE_OMIT_AUTOVACUUM
8584 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8585 if( rc ){
8586 return rc;
8588 #else
8589 if( pBt->autoVacuum ){
8590 Pgno pgnoMove; /* Move a page here to make room for the root-page */
8591 MemPage *pPageMove; /* The page to move to. */
8593 /* Creating a new table may probably require moving an existing database
8594 ** to make room for the new tables root page. In case this page turns
8595 ** out to be an overflow page, delete all overflow page-map caches
8596 ** held by open cursors.
8598 invalidateAllOverflowCache(pBt);
8600 /* Read the value of meta[3] from the database to determine where the
8601 ** root page of the new table should go. meta[3] is the largest root-page
8602 ** created so far, so the new root-page is (meta[3]+1).
8604 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
8605 pgnoRoot++;
8607 /* The new root-page may not be allocated on a pointer-map page, or the
8608 ** PENDING_BYTE page.
8610 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
8611 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
8612 pgnoRoot++;
8614 assert( pgnoRoot>=3 || CORRUPT_DB );
8615 testcase( pgnoRoot<3 );
8617 /* Allocate a page. The page that currently resides at pgnoRoot will
8618 ** be moved to the allocated page (unless the allocated page happens
8619 ** to reside at pgnoRoot).
8621 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
8622 if( rc!=SQLITE_OK ){
8623 return rc;
8626 if( pgnoMove!=pgnoRoot ){
8627 /* pgnoRoot is the page that will be used for the root-page of
8628 ** the new table (assuming an error did not occur). But we were
8629 ** allocated pgnoMove. If required (i.e. if it was not allocated
8630 ** by extending the file), the current page at position pgnoMove
8631 ** is already journaled.
8633 u8 eType = 0;
8634 Pgno iPtrPage = 0;
8636 /* Save the positions of any open cursors. This is required in
8637 ** case they are holding a reference to an xFetch reference
8638 ** corresponding to page pgnoRoot. */
8639 rc = saveAllCursors(pBt, 0, 0);
8640 releasePage(pPageMove);
8641 if( rc!=SQLITE_OK ){
8642 return rc;
8645 /* Move the page currently at pgnoRoot to pgnoMove. */
8646 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8647 if( rc!=SQLITE_OK ){
8648 return rc;
8650 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
8651 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
8652 rc = SQLITE_CORRUPT_BKPT;
8654 if( rc!=SQLITE_OK ){
8655 releasePage(pRoot);
8656 return rc;
8658 assert( eType!=PTRMAP_ROOTPAGE );
8659 assert( eType!=PTRMAP_FREEPAGE );
8660 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
8661 releasePage(pRoot);
8663 /* Obtain the page at pgnoRoot */
8664 if( rc!=SQLITE_OK ){
8665 return rc;
8667 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
8668 if( rc!=SQLITE_OK ){
8669 return rc;
8671 rc = sqlite3PagerWrite(pRoot->pDbPage);
8672 if( rc!=SQLITE_OK ){
8673 releasePage(pRoot);
8674 return rc;
8676 }else{
8677 pRoot = pPageMove;
8680 /* Update the pointer-map and meta-data with the new root-page number. */
8681 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
8682 if( rc ){
8683 releasePage(pRoot);
8684 return rc;
8687 /* When the new root page was allocated, page 1 was made writable in
8688 ** order either to increase the database filesize, or to decrement the
8689 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
8691 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
8692 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
8693 if( NEVER(rc) ){
8694 releasePage(pRoot);
8695 return rc;
8698 }else{
8699 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
8700 if( rc ) return rc;
8702 #endif
8703 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
8704 if( createTabFlags & BTREE_INTKEY ){
8705 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
8706 }else{
8707 ptfFlags = PTF_ZERODATA | PTF_LEAF;
8709 zeroPage(pRoot, ptfFlags);
8710 sqlite3PagerUnref(pRoot->pDbPage);
8711 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
8712 *piTable = (int)pgnoRoot;
8713 return SQLITE_OK;
8715 int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
8716 int rc;
8717 sqlite3BtreeEnter(p);
8718 rc = btreeCreateTable(p, piTable, flags);
8719 sqlite3BtreeLeave(p);
8720 return rc;
8724 ** Erase the given database page and all its children. Return
8725 ** the page to the freelist.
8727 static int clearDatabasePage(
8728 BtShared *pBt, /* The BTree that contains the table */
8729 Pgno pgno, /* Page number to clear */
8730 int freePageFlag, /* Deallocate page if true */
8731 int *pnChange /* Add number of Cells freed to this counter */
8733 MemPage *pPage;
8734 int rc;
8735 unsigned char *pCell;
8736 int i;
8737 int hdr;
8738 CellInfo info;
8740 assert( sqlite3_mutex_held(pBt->mutex) );
8741 if( pgno>btreePagecount(pBt) ){
8742 return SQLITE_CORRUPT_BKPT;
8744 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
8745 if( rc ) return rc;
8746 if( pPage->bBusy ){
8747 rc = SQLITE_CORRUPT_BKPT;
8748 goto cleardatabasepage_out;
8750 pPage->bBusy = 1;
8751 hdr = pPage->hdrOffset;
8752 for(i=0; i<pPage->nCell; i++){
8753 pCell = findCell(pPage, i);
8754 if( !pPage->leaf ){
8755 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
8756 if( rc ) goto cleardatabasepage_out;
8758 rc = clearCell(pPage, pCell, &info);
8759 if( rc ) goto cleardatabasepage_out;
8761 if( !pPage->leaf ){
8762 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
8763 if( rc ) goto cleardatabasepage_out;
8764 }else if( pnChange ){
8765 assert( pPage->intKey || CORRUPT_DB );
8766 testcase( !pPage->intKey );
8767 *pnChange += pPage->nCell;
8769 if( freePageFlag ){
8770 freePage(pPage, &rc);
8771 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
8772 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
8775 cleardatabasepage_out:
8776 pPage->bBusy = 0;
8777 releasePage(pPage);
8778 return rc;
8782 ** Delete all information from a single table in the database. iTable is
8783 ** the page number of the root of the table. After this routine returns,
8784 ** the root page is empty, but still exists.
8786 ** This routine will fail with SQLITE_LOCKED if there are any open
8787 ** read cursors on the table. Open write cursors are moved to the
8788 ** root of the table.
8790 ** If pnChange is not NULL, then table iTable must be an intkey table. The
8791 ** integer value pointed to by pnChange is incremented by the number of
8792 ** entries in the table.
8794 int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
8795 int rc;
8796 BtShared *pBt = p->pBt;
8797 sqlite3BtreeEnter(p);
8798 assert( p->inTrans==TRANS_WRITE );
8800 rc = saveAllCursors(pBt, (Pgno)iTable, 0);
8802 if( SQLITE_OK==rc ){
8803 /* Invalidate all incrblob cursors open on table iTable (assuming iTable
8804 ** is the root of a table b-tree - if it is not, the following call is
8805 ** a no-op). */
8806 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
8807 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
8809 sqlite3BtreeLeave(p);
8810 return rc;
8814 ** Delete all information from the single table that pCur is open on.
8816 ** This routine only work for pCur on an ephemeral table.
8818 int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
8819 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
8823 ** Erase all information in a table and add the root of the table to
8824 ** the freelist. Except, the root of the principle table (the one on
8825 ** page 1) is never added to the freelist.
8827 ** This routine will fail with SQLITE_LOCKED if there are any open
8828 ** cursors on the table.
8830 ** If AUTOVACUUM is enabled and the page at iTable is not the last
8831 ** root page in the database file, then the last root page
8832 ** in the database file is moved into the slot formerly occupied by
8833 ** iTable and that last slot formerly occupied by the last root page
8834 ** is added to the freelist instead of iTable. In this say, all
8835 ** root pages are kept at the beginning of the database file, which
8836 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
8837 ** page number that used to be the last root page in the file before
8838 ** the move. If no page gets moved, *piMoved is set to 0.
8839 ** The last root page is recorded in meta[3] and the value of
8840 ** meta[3] is updated by this procedure.
8842 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
8843 int rc;
8844 MemPage *pPage = 0;
8845 BtShared *pBt = p->pBt;
8847 assert( sqlite3BtreeHoldsMutex(p) );
8848 assert( p->inTrans==TRANS_WRITE );
8849 assert( iTable>=2 );
8851 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
8852 if( rc ) return rc;
8853 rc = sqlite3BtreeClearTable(p, iTable, 0);
8854 if( rc ){
8855 releasePage(pPage);
8856 return rc;
8859 *piMoved = 0;
8861 #ifdef SQLITE_OMIT_AUTOVACUUM
8862 freePage(pPage, &rc);
8863 releasePage(pPage);
8864 #else
8865 if( pBt->autoVacuum ){
8866 Pgno maxRootPgno;
8867 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
8869 if( iTable==maxRootPgno ){
8870 /* If the table being dropped is the table with the largest root-page
8871 ** number in the database, put the root page on the free list.
8873 freePage(pPage, &rc);
8874 releasePage(pPage);
8875 if( rc!=SQLITE_OK ){
8876 return rc;
8878 }else{
8879 /* The table being dropped does not have the largest root-page
8880 ** number in the database. So move the page that does into the
8881 ** gap left by the deleted root-page.
8883 MemPage *pMove;
8884 releasePage(pPage);
8885 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
8886 if( rc!=SQLITE_OK ){
8887 return rc;
8889 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
8890 releasePage(pMove);
8891 if( rc!=SQLITE_OK ){
8892 return rc;
8894 pMove = 0;
8895 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
8896 freePage(pMove, &rc);
8897 releasePage(pMove);
8898 if( rc!=SQLITE_OK ){
8899 return rc;
8901 *piMoved = maxRootPgno;
8904 /* Set the new 'max-root-page' value in the database header. This
8905 ** is the old value less one, less one more if that happens to
8906 ** be a root-page number, less one again if that is the
8907 ** PENDING_BYTE_PAGE.
8909 maxRootPgno--;
8910 while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
8911 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
8912 maxRootPgno--;
8914 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
8916 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
8917 }else{
8918 freePage(pPage, &rc);
8919 releasePage(pPage);
8921 #endif
8922 return rc;
8924 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
8925 int rc;
8926 sqlite3BtreeEnter(p);
8927 rc = btreeDropTable(p, iTable, piMoved);
8928 sqlite3BtreeLeave(p);
8929 return rc;
8934 ** This function may only be called if the b-tree connection already
8935 ** has a read or write transaction open on the database.
8937 ** Read the meta-information out of a database file. Meta[0]
8938 ** is the number of free pages currently in the database. Meta[1]
8939 ** through meta[15] are available for use by higher layers. Meta[0]
8940 ** is read-only, the others are read/write.
8942 ** The schema layer numbers meta values differently. At the schema
8943 ** layer (and the SetCookie and ReadCookie opcodes) the number of
8944 ** free pages is not visible. So Cookie[0] is the same as Meta[1].
8946 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead
8947 ** of reading the value out of the header, it instead loads the "DataVersion"
8948 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the
8949 ** database file. It is a number computed by the pager. But its access
8950 ** pattern is the same as header meta values, and so it is convenient to
8951 ** read it from this routine.
8953 void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
8954 BtShared *pBt = p->pBt;
8956 sqlite3BtreeEnter(p);
8957 assert( p->inTrans>TRANS_NONE );
8958 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
8959 assert( pBt->pPage1 );
8960 assert( idx>=0 && idx<=15 );
8962 if( idx==BTREE_DATA_VERSION ){
8963 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
8964 }else{
8965 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
8968 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
8969 ** database, mark the database as read-only. */
8970 #ifdef SQLITE_OMIT_AUTOVACUUM
8971 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
8972 pBt->btsFlags |= BTS_READ_ONLY;
8974 #endif
8976 sqlite3BtreeLeave(p);
8980 ** Write meta-information back into the database. Meta[0] is
8981 ** read-only and may not be written.
8983 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
8984 BtShared *pBt = p->pBt;
8985 unsigned char *pP1;
8986 int rc;
8987 assert( idx>=1 && idx<=15 );
8988 sqlite3BtreeEnter(p);
8989 assert( p->inTrans==TRANS_WRITE );
8990 assert( pBt->pPage1!=0 );
8991 pP1 = pBt->pPage1->aData;
8992 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
8993 if( rc==SQLITE_OK ){
8994 put4byte(&pP1[36 + idx*4], iMeta);
8995 #ifndef SQLITE_OMIT_AUTOVACUUM
8996 if( idx==BTREE_INCR_VACUUM ){
8997 assert( pBt->autoVacuum || iMeta==0 );
8998 assert( iMeta==0 || iMeta==1 );
8999 pBt->incrVacuum = (u8)iMeta;
9001 #endif
9003 sqlite3BtreeLeave(p);
9004 return rc;
9007 #ifndef SQLITE_OMIT_BTREECOUNT
9009 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
9010 ** number of entries in the b-tree and write the result to *pnEntry.
9012 ** SQLITE_OK is returned if the operation is successfully executed.
9013 ** Otherwise, if an error is encountered (i.e. an IO error or database
9014 ** corruption) an SQLite error code is returned.
9016 int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
9017 i64 nEntry = 0; /* Value to return in *pnEntry */
9018 int rc; /* Return code */
9020 rc = moveToRoot(pCur);
9021 if( rc==SQLITE_EMPTY ){
9022 *pnEntry = 0;
9023 return SQLITE_OK;
9026 /* Unless an error occurs, the following loop runs one iteration for each
9027 ** page in the B-Tree structure (not including overflow pages).
9029 while( rc==SQLITE_OK ){
9030 int iIdx; /* Index of child node in parent */
9031 MemPage *pPage; /* Current page of the b-tree */
9033 /* If this is a leaf page or the tree is not an int-key tree, then
9034 ** this page contains countable entries. Increment the entry counter
9035 ** accordingly.
9037 pPage = pCur->pPage;
9038 if( pPage->leaf || !pPage->intKey ){
9039 nEntry += pPage->nCell;
9042 /* pPage is a leaf node. This loop navigates the cursor so that it
9043 ** points to the first interior cell that it points to the parent of
9044 ** the next page in the tree that has not yet been visited. The
9045 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
9046 ** of the page, or to the number of cells in the page if the next page
9047 ** to visit is the right-child of its parent.
9049 ** If all pages in the tree have been visited, return SQLITE_OK to the
9050 ** caller.
9052 if( pPage->leaf ){
9053 do {
9054 if( pCur->iPage==0 ){
9055 /* All pages of the b-tree have been visited. Return successfully. */
9056 *pnEntry = nEntry;
9057 return moveToRoot(pCur);
9059 moveToParent(pCur);
9060 }while ( pCur->ix>=pCur->pPage->nCell );
9062 pCur->ix++;
9063 pPage = pCur->pPage;
9066 /* Descend to the child node of the cell that the cursor currently
9067 ** points at. This is the right-child if (iIdx==pPage->nCell).
9069 iIdx = pCur->ix;
9070 if( iIdx==pPage->nCell ){
9071 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
9072 }else{
9073 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
9077 /* An error has occurred. Return an error code. */
9078 return rc;
9080 #endif
9083 ** Return the pager associated with a BTree. This routine is used for
9084 ** testing and debugging only.
9086 Pager *sqlite3BtreePager(Btree *p){
9087 return p->pBt->pPager;
9090 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9092 ** Append a message to the error message string.
9094 static void checkAppendMsg(
9095 IntegrityCk *pCheck,
9096 const char *zFormat,
9099 va_list ap;
9100 if( !pCheck->mxErr ) return;
9101 pCheck->mxErr--;
9102 pCheck->nErr++;
9103 va_start(ap, zFormat);
9104 if( pCheck->errMsg.nChar ){
9105 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
9107 if( pCheck->zPfx ){
9108 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
9110 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
9111 va_end(ap);
9112 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
9113 pCheck->mallocFailed = 1;
9116 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9118 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9121 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
9122 ** corresponds to page iPg is already set.
9124 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
9125 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
9126 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
9130 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
9132 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
9133 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
9134 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
9139 ** Add 1 to the reference count for page iPage. If this is the second
9140 ** reference to the page, add an error message to pCheck->zErrMsg.
9141 ** Return 1 if there are 2 or more references to the page and 0 if
9142 ** if this is the first reference to the page.
9144 ** Also check that the page number is in bounds.
9146 static int checkRef(IntegrityCk *pCheck, Pgno iPage){
9147 if( iPage==0 ) return 1;
9148 if( iPage>pCheck->nPage ){
9149 checkAppendMsg(pCheck, "invalid page number %d", iPage);
9150 return 1;
9152 if( getPageReferenced(pCheck, iPage) ){
9153 checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
9154 return 1;
9156 setPageReferenced(pCheck, iPage);
9157 return 0;
9160 #ifndef SQLITE_OMIT_AUTOVACUUM
9162 ** Check that the entry in the pointer-map for page iChild maps to
9163 ** page iParent, pointer type ptrType. If not, append an error message
9164 ** to pCheck.
9166 static void checkPtrmap(
9167 IntegrityCk *pCheck, /* Integrity check context */
9168 Pgno iChild, /* Child page number */
9169 u8 eType, /* Expected pointer map type */
9170 Pgno iParent /* Expected pointer map parent page number */
9172 int rc;
9173 u8 ePtrmapType;
9174 Pgno iPtrmapParent;
9176 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
9177 if( rc!=SQLITE_OK ){
9178 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
9179 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
9180 return;
9183 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
9184 checkAppendMsg(pCheck,
9185 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
9186 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
9189 #endif
9192 ** Check the integrity of the freelist or of an overflow page list.
9193 ** Verify that the number of pages on the list is N.
9195 static void checkList(
9196 IntegrityCk *pCheck, /* Integrity checking context */
9197 int isFreeList, /* True for a freelist. False for overflow page list */
9198 int iPage, /* Page number for first page in the list */
9199 int N /* Expected number of pages in the list */
9201 int i;
9202 int expected = N;
9203 int iFirst = iPage;
9204 while( N-- > 0 && pCheck->mxErr ){
9205 DbPage *pOvflPage;
9206 unsigned char *pOvflData;
9207 if( iPage<1 ){
9208 checkAppendMsg(pCheck,
9209 "%d of %d pages missing from overflow list starting at %d",
9210 N+1, expected, iFirst);
9211 break;
9213 if( checkRef(pCheck, iPage) ) break;
9214 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
9215 checkAppendMsg(pCheck, "failed to get page %d", iPage);
9216 break;
9218 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
9219 if( isFreeList ){
9220 int n = get4byte(&pOvflData[4]);
9221 #ifndef SQLITE_OMIT_AUTOVACUUM
9222 if( pCheck->pBt->autoVacuum ){
9223 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0);
9225 #endif
9226 if( n>(int)pCheck->pBt->usableSize/4-2 ){
9227 checkAppendMsg(pCheck,
9228 "freelist leaf count too big on page %d", iPage);
9229 N--;
9230 }else{
9231 for(i=0; i<n; i++){
9232 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
9233 #ifndef SQLITE_OMIT_AUTOVACUUM
9234 if( pCheck->pBt->autoVacuum ){
9235 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0);
9237 #endif
9238 checkRef(pCheck, iFreePage);
9240 N -= n;
9243 #ifndef SQLITE_OMIT_AUTOVACUUM
9244 else{
9245 /* If this database supports auto-vacuum and iPage is not the last
9246 ** page in this overflow list, check that the pointer-map entry for
9247 ** the following page matches iPage.
9249 if( pCheck->pBt->autoVacuum && N>0 ){
9250 i = get4byte(pOvflData);
9251 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage);
9254 #endif
9255 iPage = get4byte(pOvflData);
9256 sqlite3PagerUnref(pOvflPage);
9258 if( isFreeList && N<(iPage!=0) ){
9259 checkAppendMsg(pCheck, "free-page count in header is too small");
9263 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9266 ** An implementation of a min-heap.
9268 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the
9269 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2]
9270 ** and aHeap[N*2+1].
9272 ** The heap property is this: Every node is less than or equal to both
9273 ** of its daughter nodes. A consequence of the heap property is that the
9274 ** root node aHeap[1] is always the minimum value currently in the heap.
9276 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto
9277 ** the heap, preserving the heap property. The btreeHeapPull() routine
9278 ** removes the root element from the heap (the minimum value in the heap)
9279 ** and then moves other nodes around as necessary to preserve the heap
9280 ** property.
9282 ** This heap is used for cell overlap and coverage testing. Each u32
9283 ** entry represents the span of a cell or freeblock on a btree page.
9284 ** The upper 16 bits are the index of the first byte of a range and the
9285 ** lower 16 bits are the index of the last byte of that range.
9287 static void btreeHeapInsert(u32 *aHeap, u32 x){
9288 u32 j, i = ++aHeap[0];
9289 aHeap[i] = x;
9290 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
9291 x = aHeap[j];
9292 aHeap[j] = aHeap[i];
9293 aHeap[i] = x;
9294 i = j;
9297 static int btreeHeapPull(u32 *aHeap, u32 *pOut){
9298 u32 j, i, x;
9299 if( (x = aHeap[0])==0 ) return 0;
9300 *pOut = aHeap[1];
9301 aHeap[1] = aHeap[x];
9302 aHeap[x] = 0xffffffff;
9303 aHeap[0]--;
9304 i = 1;
9305 while( (j = i*2)<=aHeap[0] ){
9306 if( aHeap[j]>aHeap[j+1] ) j++;
9307 if( aHeap[i]<aHeap[j] ) break;
9308 x = aHeap[i];
9309 aHeap[i] = aHeap[j];
9310 aHeap[j] = x;
9311 i = j;
9313 return 1;
9316 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9318 ** Do various sanity checks on a single page of a tree. Return
9319 ** the tree depth. Root pages return 0. Parents of root pages
9320 ** return 1, and so forth.
9322 ** These checks are done:
9324 ** 1. Make sure that cells and freeblocks do not overlap
9325 ** but combine to completely cover the page.
9326 ** 2. Make sure integer cell keys are in order.
9327 ** 3. Check the integrity of overflow pages.
9328 ** 4. Recursively call checkTreePage on all children.
9329 ** 5. Verify that the depth of all children is the same.
9331 static int checkTreePage(
9332 IntegrityCk *pCheck, /* Context for the sanity check */
9333 int iPage, /* Page number of the page to check */
9334 i64 *piMinKey, /* Write minimum integer primary key here */
9335 i64 maxKey /* Error if integer primary key greater than this */
9337 MemPage *pPage = 0; /* The page being analyzed */
9338 int i; /* Loop counter */
9339 int rc; /* Result code from subroutine call */
9340 int depth = -1, d2; /* Depth of a subtree */
9341 int pgno; /* Page number */
9342 int nFrag; /* Number of fragmented bytes on the page */
9343 int hdr; /* Offset to the page header */
9344 int cellStart; /* Offset to the start of the cell pointer array */
9345 int nCell; /* Number of cells */
9346 int doCoverageCheck = 1; /* True if cell coverage checking should be done */
9347 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey
9348 ** False if IPK must be strictly less than maxKey */
9349 u8 *data; /* Page content */
9350 u8 *pCell; /* Cell content */
9351 u8 *pCellIdx; /* Next element of the cell pointer array */
9352 BtShared *pBt; /* The BtShared object that owns pPage */
9353 u32 pc; /* Address of a cell */
9354 u32 usableSize; /* Usable size of the page */
9355 u32 contentOffset; /* Offset to the start of the cell content area */
9356 u32 *heap = 0; /* Min-heap used for checking cell coverage */
9357 u32 x, prev = 0; /* Next and previous entry on the min-heap */
9358 const char *saved_zPfx = pCheck->zPfx;
9359 int saved_v1 = pCheck->v1;
9360 int saved_v2 = pCheck->v2;
9361 u8 savedIsInit = 0;
9363 /* Check that the page exists
9365 pBt = pCheck->pBt;
9366 usableSize = pBt->usableSize;
9367 if( iPage==0 ) return 0;
9368 if( checkRef(pCheck, iPage) ) return 0;
9369 pCheck->zPfx = "Page %d: ";
9370 pCheck->v1 = iPage;
9371 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
9372 checkAppendMsg(pCheck,
9373 "unable to get the page. error code=%d", rc);
9374 goto end_of_check;
9377 /* Clear MemPage.isInit to make sure the corruption detection code in
9378 ** btreeInitPage() is executed. */
9379 savedIsInit = pPage->isInit;
9380 pPage->isInit = 0;
9381 if( (rc = btreeInitPage(pPage))!=0 ){
9382 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
9383 checkAppendMsg(pCheck,
9384 "btreeInitPage() returns error code %d", rc);
9385 goto end_of_check;
9387 data = pPage->aData;
9388 hdr = pPage->hdrOffset;
9390 /* Set up for cell analysis */
9391 pCheck->zPfx = "On tree page %d cell %d: ";
9392 contentOffset = get2byteNotZero(&data[hdr+5]);
9393 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */
9395 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
9396 ** number of cells on the page. */
9397 nCell = get2byte(&data[hdr+3]);
9398 assert( pPage->nCell==nCell );
9400 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page
9401 ** immediately follows the b-tree page header. */
9402 cellStart = hdr + 12 - 4*pPage->leaf;
9403 assert( pPage->aCellIdx==&data[cellStart] );
9404 pCellIdx = &data[cellStart + 2*(nCell-1)];
9406 if( !pPage->leaf ){
9407 /* Analyze the right-child page of internal pages */
9408 pgno = get4byte(&data[hdr+8]);
9409 #ifndef SQLITE_OMIT_AUTOVACUUM
9410 if( pBt->autoVacuum ){
9411 pCheck->zPfx = "On page %d at right child: ";
9412 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9414 #endif
9415 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9416 keyCanBeEqual = 0;
9417 }else{
9418 /* For leaf pages, the coverage check will occur in the same loop
9419 ** as the other cell checks, so initialize the heap. */
9420 heap = pCheck->heap;
9421 heap[0] = 0;
9424 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte
9425 ** integer offsets to the cell contents. */
9426 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
9427 CellInfo info;
9429 /* Check cell size */
9430 pCheck->v2 = i;
9431 assert( pCellIdx==&data[cellStart + i*2] );
9432 pc = get2byteAligned(pCellIdx);
9433 pCellIdx -= 2;
9434 if( pc<contentOffset || pc>usableSize-4 ){
9435 checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
9436 pc, contentOffset, usableSize-4);
9437 doCoverageCheck = 0;
9438 continue;
9440 pCell = &data[pc];
9441 pPage->xParseCell(pPage, pCell, &info);
9442 if( pc+info.nSize>usableSize ){
9443 checkAppendMsg(pCheck, "Extends off end of page");
9444 doCoverageCheck = 0;
9445 continue;
9448 /* Check for integer primary key out of range */
9449 if( pPage->intKey ){
9450 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
9451 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
9453 maxKey = info.nKey;
9454 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */
9457 /* Check the content overflow list */
9458 if( info.nPayload>info.nLocal ){
9459 int nPage; /* Number of pages on the overflow chain */
9460 Pgno pgnoOvfl; /* First page of the overflow chain */
9461 assert( pc + info.nSize - 4 <= usableSize );
9462 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
9463 pgnoOvfl = get4byte(&pCell[info.nSize - 4]);
9464 #ifndef SQLITE_OMIT_AUTOVACUUM
9465 if( pBt->autoVacuum ){
9466 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage);
9468 #endif
9469 checkList(pCheck, 0, pgnoOvfl, nPage);
9472 if( !pPage->leaf ){
9473 /* Check sanity of left child page for internal pages */
9474 pgno = get4byte(pCell);
9475 #ifndef SQLITE_OMIT_AUTOVACUUM
9476 if( pBt->autoVacuum ){
9477 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage);
9479 #endif
9480 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
9481 keyCanBeEqual = 0;
9482 if( d2!=depth ){
9483 checkAppendMsg(pCheck, "Child page depth differs");
9484 depth = d2;
9486 }else{
9487 /* Populate the coverage-checking heap for leaf pages */
9488 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
9491 *piMinKey = maxKey;
9493 /* Check for complete coverage of the page
9495 pCheck->zPfx = 0;
9496 if( doCoverageCheck && pCheck->mxErr>0 ){
9497 /* For leaf pages, the min-heap has already been initialized and the
9498 ** cells have already been inserted. But for internal pages, that has
9499 ** not yet been done, so do it now */
9500 if( !pPage->leaf ){
9501 heap = pCheck->heap;
9502 heap[0] = 0;
9503 for(i=nCell-1; i>=0; i--){
9504 u32 size;
9505 pc = get2byteAligned(&data[cellStart+i*2]);
9506 size = pPage->xCellSize(pPage, &data[pc]);
9507 btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
9510 /* Add the freeblocks to the min-heap
9512 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header
9513 ** is the offset of the first freeblock, or zero if there are no
9514 ** freeblocks on the page.
9516 i = get2byte(&data[hdr+1]);
9517 while( i>0 ){
9518 int size, j;
9519 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */
9520 size = get2byte(&data[i+2]);
9521 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */
9522 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
9523 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a
9524 ** big-endian integer which is the offset in the b-tree page of the next
9525 ** freeblock in the chain, or zero if the freeblock is the last on the
9526 ** chain. */
9527 j = get2byte(&data[i]);
9528 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of
9529 ** increasing offset. */
9530 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */
9531 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */
9532 i = j;
9534 /* Analyze the min-heap looking for overlap between cells and/or
9535 ** freeblocks, and counting the number of untracked bytes in nFrag.
9537 ** Each min-heap entry is of the form: (start_address<<16)|end_address.
9538 ** There is an implied first entry the covers the page header, the cell
9539 ** pointer index, and the gap between the cell pointer index and the start
9540 ** of cell content.
9542 ** The loop below pulls entries from the min-heap in order and compares
9543 ** the start_address against the previous end_address. If there is an
9544 ** overlap, that means bytes are used multiple times. If there is a gap,
9545 ** that gap is added to the fragmentation count.
9547 nFrag = 0;
9548 prev = contentOffset - 1; /* Implied first min-heap entry */
9549 while( btreeHeapPull(heap,&x) ){
9550 if( (prev&0xffff)>=(x>>16) ){
9551 checkAppendMsg(pCheck,
9552 "Multiple uses for byte %u of page %d", x>>16, iPage);
9553 break;
9554 }else{
9555 nFrag += (x>>16) - (prev&0xffff) - 1;
9556 prev = x;
9559 nFrag += usableSize - (prev&0xffff) - 1;
9560 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments
9561 ** is stored in the fifth field of the b-tree page header.
9562 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the
9563 ** number of fragmented free bytes within the cell content area.
9565 if( heap[0]==0 && nFrag!=data[hdr+7] ){
9566 checkAppendMsg(pCheck,
9567 "Fragmentation of %d bytes reported as %d on page %d",
9568 nFrag, data[hdr+7], iPage);
9572 end_of_check:
9573 if( !doCoverageCheck ) pPage->isInit = savedIsInit;
9574 releasePage(pPage);
9575 pCheck->zPfx = saved_zPfx;
9576 pCheck->v1 = saved_v1;
9577 pCheck->v2 = saved_v2;
9578 return depth+1;
9580 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9582 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
9584 ** This routine does a complete check of the given BTree file. aRoot[] is
9585 ** an array of pages numbers were each page number is the root page of
9586 ** a table. nRoot is the number of entries in aRoot.
9588 ** A read-only or read-write transaction must be opened before calling
9589 ** this function.
9591 ** Write the number of error seen in *pnErr. Except for some memory
9592 ** allocation errors, an error message held in memory obtained from
9593 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
9594 ** returned. If a memory allocation error occurs, NULL is returned.
9596 char *sqlite3BtreeIntegrityCheck(
9597 Btree *p, /* The btree to be checked */
9598 int *aRoot, /* An array of root pages numbers for individual trees */
9599 int nRoot, /* Number of entries in aRoot[] */
9600 int mxErr, /* Stop reporting errors after this many */
9601 int *pnErr /* Write number of errors seen to this variable */
9603 Pgno i;
9604 IntegrityCk sCheck;
9605 BtShared *pBt = p->pBt;
9606 int savedDbFlags = pBt->db->flags;
9607 char zErr[100];
9608 VVA_ONLY( int nRef );
9610 sqlite3BtreeEnter(p);
9611 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
9612 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
9613 assert( nRef>=0 );
9614 sCheck.pBt = pBt;
9615 sCheck.pPager = pBt->pPager;
9616 sCheck.nPage = btreePagecount(sCheck.pBt);
9617 sCheck.mxErr = mxErr;
9618 sCheck.nErr = 0;
9619 sCheck.mallocFailed = 0;
9620 sCheck.zPfx = 0;
9621 sCheck.v1 = 0;
9622 sCheck.v2 = 0;
9623 sCheck.aPgRef = 0;
9624 sCheck.heap = 0;
9625 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
9626 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
9627 if( sCheck.nPage==0 ){
9628 goto integrity_ck_cleanup;
9631 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
9632 if( !sCheck.aPgRef ){
9633 sCheck.mallocFailed = 1;
9634 goto integrity_ck_cleanup;
9636 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
9637 if( sCheck.heap==0 ){
9638 sCheck.mallocFailed = 1;
9639 goto integrity_ck_cleanup;
9642 i = PENDING_BYTE_PAGE(pBt);
9643 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
9645 /* Check the integrity of the freelist
9647 sCheck.zPfx = "Main freelist: ";
9648 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
9649 get4byte(&pBt->pPage1->aData[36]));
9650 sCheck.zPfx = 0;
9652 /* Check all the tables.
9654 testcase( pBt->db->flags & SQLITE_CellSizeCk );
9655 pBt->db->flags &= ~SQLITE_CellSizeCk;
9656 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
9657 i64 notUsed;
9658 if( aRoot[i]==0 ) continue;
9659 #ifndef SQLITE_OMIT_AUTOVACUUM
9660 if( pBt->autoVacuum && aRoot[i]>1 ){
9661 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0);
9663 #endif
9664 checkTreePage(&sCheck, aRoot[i], &notUsed, LARGEST_INT64);
9666 pBt->db->flags = savedDbFlags;
9668 /* Make sure every page in the file is referenced
9670 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
9671 #ifdef SQLITE_OMIT_AUTOVACUUM
9672 if( getPageReferenced(&sCheck, i)==0 ){
9673 checkAppendMsg(&sCheck, "Page %d is never used", i);
9675 #else
9676 /* If the database supports auto-vacuum, make sure no tables contain
9677 ** references to pointer-map pages.
9679 if( getPageReferenced(&sCheck, i)==0 &&
9680 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
9681 checkAppendMsg(&sCheck, "Page %d is never used", i);
9683 if( getPageReferenced(&sCheck, i)!=0 &&
9684 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
9685 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
9687 #endif
9690 /* Clean up and report errors.
9692 integrity_ck_cleanup:
9693 sqlite3PageFree(sCheck.heap);
9694 sqlite3_free(sCheck.aPgRef);
9695 if( sCheck.mallocFailed ){
9696 sqlite3StrAccumReset(&sCheck.errMsg);
9697 sCheck.nErr++;
9699 *pnErr = sCheck.nErr;
9700 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
9701 /* Make sure this analysis did not leave any unref() pages. */
9702 assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
9703 sqlite3BtreeLeave(p);
9704 return sqlite3StrAccumFinish(&sCheck.errMsg);
9706 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
9709 ** Return the full pathname of the underlying database file. Return
9710 ** an empty string if the database is in-memory or a TEMP database.
9712 ** The pager filename is invariant as long as the pager is
9713 ** open so it is safe to access without the BtShared mutex.
9715 const char *sqlite3BtreeGetFilename(Btree *p){
9716 assert( p->pBt->pPager!=0 );
9717 return sqlite3PagerFilename(p->pBt->pPager, 1);
9721 ** Return the pathname of the journal file for this database. The return
9722 ** value of this routine is the same regardless of whether the journal file
9723 ** has been created or not.
9725 ** The pager journal filename is invariant as long as the pager is
9726 ** open so it is safe to access without the BtShared mutex.
9728 const char *sqlite3BtreeGetJournalname(Btree *p){
9729 assert( p->pBt->pPager!=0 );
9730 return sqlite3PagerJournalname(p->pBt->pPager);
9734 ** Return non-zero if a transaction is active.
9736 int sqlite3BtreeIsInTrans(Btree *p){
9737 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
9738 return (p && (p->inTrans==TRANS_WRITE));
9741 #ifndef SQLITE_OMIT_WAL
9743 ** Run a checkpoint on the Btree passed as the first argument.
9745 ** Return SQLITE_LOCKED if this or any other connection has an open
9746 ** transaction on the shared-cache the argument Btree is connected to.
9748 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
9750 int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
9751 int rc = SQLITE_OK;
9752 if( p ){
9753 BtShared *pBt = p->pBt;
9754 sqlite3BtreeEnter(p);
9755 if( pBt->inTransaction!=TRANS_NONE ){
9756 rc = SQLITE_LOCKED;
9757 }else{
9758 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
9760 sqlite3BtreeLeave(p);
9762 return rc;
9764 #endif
9767 ** Return non-zero if a read (or write) transaction is active.
9769 int sqlite3BtreeIsInReadTrans(Btree *p){
9770 assert( p );
9771 assert( sqlite3_mutex_held(p->db->mutex) );
9772 return p->inTrans!=TRANS_NONE;
9775 int sqlite3BtreeIsInBackup(Btree *p){
9776 assert( p );
9777 assert( sqlite3_mutex_held(p->db->mutex) );
9778 return p->nBackup!=0;
9782 ** This function returns a pointer to a blob of memory associated with
9783 ** a single shared-btree. The memory is used by client code for its own
9784 ** purposes (for example, to store a high-level schema associated with
9785 ** the shared-btree). The btree layer manages reference counting issues.
9787 ** The first time this is called on a shared-btree, nBytes bytes of memory
9788 ** are allocated, zeroed, and returned to the caller. For each subsequent
9789 ** call the nBytes parameter is ignored and a pointer to the same blob
9790 ** of memory returned.
9792 ** If the nBytes parameter is 0 and the blob of memory has not yet been
9793 ** allocated, a null pointer is returned. If the blob has already been
9794 ** allocated, it is returned as normal.
9796 ** Just before the shared-btree is closed, the function passed as the
9797 ** xFree argument when the memory allocation was made is invoked on the
9798 ** blob of allocated memory. The xFree function should not call sqlite3_free()
9799 ** on the memory, the btree layer does that.
9801 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
9802 BtShared *pBt = p->pBt;
9803 sqlite3BtreeEnter(p);
9804 if( !pBt->pSchema && nBytes ){
9805 pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
9806 pBt->xFreeSchema = xFree;
9808 sqlite3BtreeLeave(p);
9809 return pBt->pSchema;
9813 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
9814 ** btree as the argument handle holds an exclusive lock on the
9815 ** sqlite_master table. Otherwise SQLITE_OK.
9817 int sqlite3BtreeSchemaLocked(Btree *p){
9818 int rc;
9819 assert( sqlite3_mutex_held(p->db->mutex) );
9820 sqlite3BtreeEnter(p);
9821 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
9822 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
9823 sqlite3BtreeLeave(p);
9824 return rc;
9828 #ifndef SQLITE_OMIT_SHARED_CACHE
9830 ** Obtain a lock on the table whose root page is iTab. The
9831 ** lock is a write lock if isWritelock is true or a read lock
9832 ** if it is false.
9834 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
9835 int rc = SQLITE_OK;
9836 assert( p->inTrans!=TRANS_NONE );
9837 if( p->sharable ){
9838 u8 lockType = READ_LOCK + isWriteLock;
9839 assert( READ_LOCK+1==WRITE_LOCK );
9840 assert( isWriteLock==0 || isWriteLock==1 );
9842 sqlite3BtreeEnter(p);
9843 rc = querySharedCacheTableLock(p, iTab, lockType);
9844 if( rc==SQLITE_OK ){
9845 rc = setSharedCacheTableLock(p, iTab, lockType);
9847 sqlite3BtreeLeave(p);
9849 return rc;
9851 #endif
9853 #ifndef SQLITE_OMIT_INCRBLOB
9855 ** Argument pCsr must be a cursor opened for writing on an
9856 ** INTKEY table currently pointing at a valid table entry.
9857 ** This function modifies the data stored as part of that entry.
9859 ** Only the data content may only be modified, it is not possible to
9860 ** change the length of the data stored. If this function is called with
9861 ** parameters that attempt to write past the end of the existing data,
9862 ** no modifications are made and SQLITE_CORRUPT is returned.
9864 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
9865 int rc;
9866 assert( cursorOwnsBtShared(pCsr) );
9867 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
9868 assert( pCsr->curFlags & BTCF_Incrblob );
9870 rc = restoreCursorPosition(pCsr);
9871 if( rc!=SQLITE_OK ){
9872 return rc;
9874 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
9875 if( pCsr->eState!=CURSOR_VALID ){
9876 return SQLITE_ABORT;
9879 /* Save the positions of all other cursors open on this table. This is
9880 ** required in case any of them are holding references to an xFetch
9881 ** version of the b-tree page modified by the accessPayload call below.
9883 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition()
9884 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
9885 ** saveAllCursors can only return SQLITE_OK.
9887 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
9888 assert( rc==SQLITE_OK );
9890 /* Check some assumptions:
9891 ** (a) the cursor is open for writing,
9892 ** (b) there is a read/write transaction open,
9893 ** (c) the connection holds a write-lock on the table (if required),
9894 ** (d) there are no conflicting read-locks, and
9895 ** (e) the cursor points at a valid row of an intKey table.
9897 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){
9898 return SQLITE_READONLY;
9900 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
9901 && pCsr->pBt->inTransaction==TRANS_WRITE );
9902 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
9903 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
9904 assert( pCsr->pPage->intKey );
9906 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
9910 ** Mark this cursor as an incremental blob cursor.
9912 void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
9913 pCur->curFlags |= BTCF_Incrblob;
9914 pCur->pBtree->hasIncrblobCur = 1;
9916 #endif
9919 ** Set both the "read version" (single byte at byte offset 18) and
9920 ** "write version" (single byte at byte offset 19) fields in the database
9921 ** header to iVersion.
9923 int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
9924 BtShared *pBt = pBtree->pBt;
9925 int rc; /* Return code */
9927 assert( iVersion==1 || iVersion==2 );
9929 /* If setting the version fields to 1, do not automatically open the
9930 ** WAL connection, even if the version fields are currently set to 2.
9932 pBt->btsFlags &= ~BTS_NO_WAL;
9933 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
9935 rc = sqlite3BtreeBeginTrans(pBtree, 0);
9936 if( rc==SQLITE_OK ){
9937 u8 *aData = pBt->pPage1->aData;
9938 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
9939 rc = sqlite3BtreeBeginTrans(pBtree, 2);
9940 if( rc==SQLITE_OK ){
9941 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
9942 if( rc==SQLITE_OK ){
9943 aData[18] = (u8)iVersion;
9944 aData[19] = (u8)iVersion;
9950 pBt->btsFlags &= ~BTS_NO_WAL;
9951 return rc;
9955 ** Return true if the cursor has a hint specified. This routine is
9956 ** only used from within assert() statements
9958 int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
9959 return (pCsr->hints & mask)!=0;
9963 ** Return true if the given Btree is read-only.
9965 int sqlite3BtreeIsReadonly(Btree *p){
9966 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
9970 ** Return the size of the header added to each page by this module.
9972 int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); }
9974 #if !defined(SQLITE_OMIT_SHARED_CACHE)
9976 ** Return true if the Btree passed as the only argument is sharable.
9978 int sqlite3BtreeSharable(Btree *p){
9979 return p->sharable;
9983 ** Return the number of connections to the BtShared object accessed by
9984 ** the Btree handle passed as the only argument. For private caches
9985 ** this is always 1. For shared caches it may be 1 or greater.
9987 int sqlite3BtreeConnectionCount(Btree *p){
9988 testcase( p->sharable );
9989 return p->pBt->nRef;
9991 #endif