engine/sqlite/src/btree.cpp
changeset 2 29cda98b007e
equal deleted inserted replaced
1:5f8e5adbbed9 2:29cda98b007e
       
     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.
       
    10 **
       
    11 *************************************************************************
       
    12 ** $Id: btree.cpp 1282 2008-11-13 09:31:33Z LarsPson $
       
    13 **
       
    14 ** This file implements a external (disk-based) database using BTrees.
       
    15 ** See the header comment on "btreeInt.h" for additional information.
       
    16 ** Including a description of file format and an overview of operation.
       
    17 */
       
    18 #include "btreeInt.h"
       
    19 
       
    20 /*
       
    21 ** The header string that appears at the beginning of every
       
    22 ** SQLite database.
       
    23 */
       
    24 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
       
    25 
       
    26 /*
       
    27 ** Set this global variable to 1 to enable tracing using the TRACE
       
    28 ** macro.
       
    29 */
       
    30 #if SQLITE_TEST
       
    31 int sqlite3_btree_trace=0;  /* True to enable tracing */
       
    32 #endif
       
    33 
       
    34 
       
    35 
       
    36 #ifndef SQLITE_OMIT_SHARED_CACHE
       
    37 /*
       
    38 ** A flag to indicate whether or not shared cache is enabled.  Also,
       
    39 ** a list of BtShared objects that are eligible for participation
       
    40 ** in shared cache.  The variables have file scope during normal builds,
       
    41 ** but the test harness needs to access these variables so we make them
       
    42 ** global for test builds.
       
    43 */
       
    44 #ifdef SQLITE_TEST
       
    45 BtShared *sqlite3SharedCacheList = 0;
       
    46 int sqlite3SharedCacheEnabled = 0;
       
    47 #else
       
    48 static BtShared *sqlite3SharedCacheList = 0;
       
    49 static int sqlite3SharedCacheEnabled = 0;
       
    50 #endif
       
    51 #endif /* SQLITE_OMIT_SHARED_CACHE */
       
    52 
       
    53 #ifndef SQLITE_OMIT_SHARED_CACHE
       
    54 /*
       
    55 ** Enable or disable the shared pager and schema features.
       
    56 **
       
    57 ** This routine has no effect on existing database connections.
       
    58 ** The shared cache setting effects only future calls to
       
    59 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
       
    60 */
       
    61 EXPORT_C int sqlite3_enable_shared_cache(int enable){
       
    62   sqlite3SharedCacheEnabled = enable;
       
    63   return SQLITE_OK;
       
    64 }
       
    65 #endif
       
    66 
       
    67 
       
    68 /*
       
    69 ** Forward declaration
       
    70 */
       
    71 static int checkReadLocks(Btree*,Pgno,BtCursor*);
       
    72 
       
    73 
       
    74 #ifdef SQLITE_OMIT_SHARED_CACHE
       
    75   /*
       
    76   ** The functions queryTableLock(), lockTable() and unlockAllTables()
       
    77   ** manipulate entries in the BtShared.pLock linked list used to store
       
    78   ** shared-cache table level locks. If the library is compiled with the
       
    79   ** shared-cache feature disabled, then there is only ever one user
       
    80   ** of each BtShared structure and so this locking is not necessary. 
       
    81   ** So define the lock related functions as no-ops.
       
    82   */
       
    83   #define queryTableLock(a,b,c) SQLITE_OK
       
    84   #define lockTable(a,b,c) SQLITE_OK
       
    85   #define unlockAllTables(a)
       
    86 #endif
       
    87 
       
    88 #ifndef SQLITE_OMIT_SHARED_CACHE
       
    89 /*
       
    90 ** Query to see if btree handle p may obtain a lock of type eLock 
       
    91 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
       
    92 ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
       
    93 ** SQLITE_LOCKED if not.
       
    94 */
       
    95 static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
       
    96   BtShared *pBt = p->pBt;
       
    97   BtLock *pIter;
       
    98 
       
    99   assert( sqlite3BtreeHoldsMutex(p) );
       
   100   
       
   101   /* This is a no-op if the shared-cache is not enabled */
       
   102   if( !p->sharable ){
       
   103     return SQLITE_OK;
       
   104   }
       
   105 
       
   106   /* This (along with lockTable()) is where the ReadUncommitted flag is
       
   107   ** dealt with. If the caller is querying for a read-lock and the flag is
       
   108   ** set, it is unconditionally granted - even if there are write-locks
       
   109   ** on the table. If a write-lock is requested, the ReadUncommitted flag
       
   110   ** is not considered.
       
   111   **
       
   112   ** In function lockTable(), if a read-lock is demanded and the 
       
   113   ** ReadUncommitted flag is set, no entry is added to the locks list 
       
   114   ** (BtShared.pLock).
       
   115   **
       
   116   ** To summarize: If the ReadUncommitted flag is set, then read cursors do
       
   117   ** not create or respect table locks. The locking procedure for a 
       
   118   ** write-cursor does not change.
       
   119   */
       
   120   if( 
       
   121     !p->db || 
       
   122     0==(p->db->flags&SQLITE_ReadUncommitted) || 
       
   123     eLock==WRITE_LOCK ||
       
   124     iTab==MASTER_ROOT
       
   125   ){
       
   126     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
       
   127       if( pIter->pBtree!=p && pIter->iTable==iTab && 
       
   128           (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
       
   129         return SQLITE_LOCKED;
       
   130       }
       
   131     }
       
   132   }
       
   133   return SQLITE_OK;
       
   134 }
       
   135 #endif /* !SQLITE_OMIT_SHARED_CACHE */
       
   136 
       
   137 #ifndef SQLITE_OMIT_SHARED_CACHE
       
   138 /*
       
   139 ** Add a lock on the table with root-page iTable to the shared-btree used
       
   140 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
       
   141 ** WRITE_LOCK.
       
   142 **
       
   143 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
       
   144 ** SQLITE_NOMEM may also be returned.
       
   145 */
       
   146 static int lockTable(Btree *p, Pgno iTable, u8 eLock){
       
   147   BtShared *pBt = p->pBt;
       
   148   BtLock *pLock = 0;
       
   149   BtLock *pIter;
       
   150 
       
   151   assert( sqlite3BtreeHoldsMutex(p) );
       
   152 
       
   153   /* This is a no-op if the shared-cache is not enabled */
       
   154   if( !p->sharable ){
       
   155     return SQLITE_OK;
       
   156   }
       
   157 
       
   158   assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
       
   159 
       
   160   /* If the read-uncommitted flag is set and a read-lock is requested,
       
   161   ** return early without adding an entry to the BtShared.pLock list. See
       
   162   ** comment in function queryTableLock() for more info on handling 
       
   163   ** the ReadUncommitted flag.
       
   164   */
       
   165   if( 
       
   166     (p->db) && 
       
   167     (p->db->flags&SQLITE_ReadUncommitted) && 
       
   168     (eLock==READ_LOCK) &&
       
   169     iTable!=MASTER_ROOT
       
   170   ){
       
   171     return SQLITE_OK;
       
   172   }
       
   173 
       
   174   /* First search the list for an existing lock on this table. */
       
   175   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
       
   176     if( pIter->iTable==iTable && pIter->pBtree==p ){
       
   177       pLock = pIter;
       
   178       break;
       
   179     }
       
   180   }
       
   181 
       
   182   /* If the above search did not find a BtLock struct associating Btree p
       
   183   ** with table iTable, allocate one and link it into the list.
       
   184   */
       
   185   if( !pLock ){
       
   186     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
       
   187     if( !pLock ){
       
   188       return SQLITE_NOMEM;
       
   189     }
       
   190     pLock->iTable = iTable;
       
   191     pLock->pBtree = p;
       
   192     pLock->pNext = pBt->pLock;
       
   193     pBt->pLock = pLock;
       
   194   }
       
   195 
       
   196   /* Set the BtLock.eLock variable to the maximum of the current lock
       
   197   ** and the requested lock. This means if a write-lock was already held
       
   198   ** and a read-lock requested, we don't incorrectly downgrade the lock.
       
   199   */
       
   200   assert( WRITE_LOCK>READ_LOCK );
       
   201   if( eLock>pLock->eLock ){
       
   202     pLock->eLock = eLock;
       
   203   }
       
   204 
       
   205   return SQLITE_OK;
       
   206 }
       
   207 #endif /* !SQLITE_OMIT_SHARED_CACHE */
       
   208 
       
   209 #ifndef SQLITE_OMIT_SHARED_CACHE
       
   210 /*
       
   211 ** Release all the table locks (locks obtained via calls to the lockTable()
       
   212 ** procedure) held by Btree handle p.
       
   213 */
       
   214 static void unlockAllTables(Btree *p){
       
   215   BtLock **ppIter = &p->pBt->pLock;
       
   216 
       
   217   assert( sqlite3BtreeHoldsMutex(p) );
       
   218   assert( p->sharable || 0==*ppIter );
       
   219 
       
   220   while( *ppIter ){
       
   221     BtLock *pLock = *ppIter;
       
   222     if( pLock->pBtree==p ){
       
   223       *ppIter = pLock->pNext;
       
   224       sqlite3_free(pLock);
       
   225     }else{
       
   226       ppIter = &pLock->pNext;
       
   227     }
       
   228   }
       
   229 }
       
   230 #endif /* SQLITE_OMIT_SHARED_CACHE */
       
   231 
       
   232 static void releasePage(MemPage *pPage);  /* Forward reference */
       
   233 
       
   234 /*
       
   235 ** Verify that the cursor holds a mutex on the BtShared
       
   236 */
       
   237 #ifndef NDEBUG
       
   238 static int cursorHoldsMutex(BtCursor *p){
       
   239   return sqlite3_mutex_held(p->pBt->mutex);
       
   240 }
       
   241 #endif
       
   242 
       
   243 
       
   244 #ifndef SQLITE_OMIT_INCRBLOB
       
   245 /*
       
   246 ** Invalidate the overflow page-list cache for cursor pCur, if any.
       
   247 */
       
   248 static void invalidateOverflowCache(BtCursor *pCur){
       
   249   assert( cursorHoldsMutex(pCur) );
       
   250   sqlite3_free(pCur->aOverflow);
       
   251   pCur->aOverflow = 0;
       
   252 }
       
   253 
       
   254 /*
       
   255 ** Invalidate the overflow page-list cache for all cursors opened
       
   256 ** on the shared btree structure pBt.
       
   257 */
       
   258 static void invalidateAllOverflowCache(BtShared *pBt){
       
   259   BtCursor *p;
       
   260   assert( sqlite3_mutex_held(pBt->mutex) );
       
   261   for(p=pBt->pCursor; p; p=p->pNext){
       
   262     invalidateOverflowCache(p);
       
   263   }
       
   264 }
       
   265 #else
       
   266   #define invalidateOverflowCache(x)
       
   267   #define invalidateAllOverflowCache(x)
       
   268 #endif
       
   269 
       
   270 /*
       
   271 ** Save the current cursor position in the variables BtCursor.nKey 
       
   272 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
       
   273 */
       
   274 static int saveCursorPosition(BtCursor *pCur){
       
   275   int rc;
       
   276 
       
   277   assert( CURSOR_VALID==pCur->eState );
       
   278   assert( 0==pCur->pKey );
       
   279   assert( cursorHoldsMutex(pCur) );
       
   280 
       
   281   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
       
   282 
       
   283   /* If this is an intKey table, then the above call to BtreeKeySize()
       
   284   ** stores the integer key in pCur->nKey. In this case this value is
       
   285   ** all that is required. Otherwise, if pCur is not open on an intKey
       
   286   ** table, then malloc space for and store the pCur->nKey bytes of key 
       
   287   ** data.
       
   288   */
       
   289   if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
       
   290     void *pKey = sqlite3_malloc(pCur->nKey);
       
   291     if( pKey ){
       
   292       rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
       
   293       if( rc==SQLITE_OK ){
       
   294         pCur->pKey = pKey;
       
   295       }else{
       
   296         sqlite3_free(pKey);
       
   297       }
       
   298     }else{
       
   299       rc = SQLITE_NOMEM;
       
   300     }
       
   301   }
       
   302   assert( !pCur->pPage->intKey || !pCur->pKey );
       
   303 
       
   304   if( rc==SQLITE_OK ){
       
   305     releasePage(pCur->pPage);
       
   306     pCur->pPage = 0;
       
   307     pCur->eState = CURSOR_REQUIRESEEK;
       
   308   }
       
   309 
       
   310   invalidateOverflowCache(pCur);
       
   311   return rc;
       
   312 }
       
   313 
       
   314 /*
       
   315 ** Save the positions of all cursors except pExcept open on the table 
       
   316 ** with root-page iRoot. Usually, this is called just before cursor
       
   317 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
       
   318 */
       
   319 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
       
   320   BtCursor *p;
       
   321   assert( sqlite3_mutex_held(pBt->mutex) );
       
   322   assert( pExcept==0 || pExcept->pBt==pBt );
       
   323   for(p=pBt->pCursor; p; p=p->pNext){
       
   324     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
       
   325         p->eState==CURSOR_VALID ){
       
   326       int rc = saveCursorPosition(p);
       
   327       if( SQLITE_OK!=rc ){
       
   328         return rc;
       
   329       }
       
   330     }
       
   331   }
       
   332   return SQLITE_OK;
       
   333 }
       
   334 
       
   335 /*
       
   336 ** Clear the current cursor position.
       
   337 */
       
   338 static void clearCursorPosition(BtCursor *pCur){
       
   339   assert( cursorHoldsMutex(pCur) );
       
   340   sqlite3_free(pCur->pKey);
       
   341   pCur->pKey = 0;
       
   342   pCur->eState = CURSOR_INVALID;
       
   343 }
       
   344 
       
   345 /*
       
   346 ** Restore the cursor to the position it was in (or as close to as possible)
       
   347 ** when saveCursorPosition() was called. Note that this call deletes the 
       
   348 ** saved position info stored by saveCursorPosition(), so there can be
       
   349 ** at most one effective restoreOrClearCursorPosition() call after each 
       
   350 ** saveCursorPosition().
       
   351 **
       
   352 ** If the second argument argument - doSeek - is false, then instead of 
       
   353 ** returning the cursor to its saved position, any saved position is deleted
       
   354 ** and the cursor state set to CURSOR_INVALID.
       
   355 */
       
   356 int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){
       
   357   int rc;
       
   358   assert( cursorHoldsMutex(pCur) );
       
   359   assert( pCur->eState>=CURSOR_REQUIRESEEK );
       
   360   if( pCur->eState==CURSOR_FAULT ){
       
   361     return pCur->skip;
       
   362   }
       
   363 #ifndef SQLITE_OMIT_INCRBLOB
       
   364   if( pCur->isIncrblobHandle ){
       
   365     return SQLITE_ABORT;
       
   366   }
       
   367 #endif
       
   368   pCur->eState = CURSOR_INVALID;
       
   369   rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
       
   370   if( rc==SQLITE_OK ){
       
   371     sqlite3_free(pCur->pKey);
       
   372     pCur->pKey = 0;
       
   373     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
       
   374   }
       
   375   return rc;
       
   376 }
       
   377 
       
   378 #define restoreOrClearCursorPosition(p) \
       
   379   (p->eState>=CURSOR_REQUIRESEEK ? \
       
   380          sqlite3BtreeRestoreOrClearCursorPosition(p) : \
       
   381          SQLITE_OK)
       
   382 
       
   383 #ifndef SQLITE_OMIT_AUTOVACUUM
       
   384 /*
       
   385 ** Given a page number of a regular database page, return the page
       
   386 ** number for the pointer-map page that contains the entry for the
       
   387 ** input page number.
       
   388 */
       
   389 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
       
   390   int nPagesPerMapPage, iPtrMap, ret;
       
   391   assert( sqlite3_mutex_held(pBt->mutex) );
       
   392   nPagesPerMapPage = (pBt->usableSize/5)+1;
       
   393   iPtrMap = (pgno-2)/nPagesPerMapPage;
       
   394   ret = (iPtrMap*nPagesPerMapPage) + 2; 
       
   395   if( ret==PENDING_BYTE_PAGE(pBt) ){
       
   396     ret++;
       
   397   }
       
   398   return ret;
       
   399 }
       
   400 
       
   401 /*
       
   402 ** Write an entry into the pointer map.
       
   403 **
       
   404 ** This routine updates the pointer map entry for page number 'key'
       
   405 ** so that it maps to type 'eType' and parent page number 'pgno'.
       
   406 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
       
   407 */
       
   408 static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
       
   409   DbPage *pDbPage;  /* The pointer map page */
       
   410   u8 *pPtrmap;      /* The pointer map data */
       
   411   Pgno iPtrmap;     /* The pointer map page number */
       
   412   int offset;       /* Offset in pointer map page */
       
   413   int rc;
       
   414 
       
   415   assert( sqlite3_mutex_held(pBt->mutex) );
       
   416   /* The master-journal page number must never be used as a pointer map page */
       
   417   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
       
   418 
       
   419   assert( pBt->autoVacuum );
       
   420   if( key==0 ){
       
   421     return SQLITE_CORRUPT_BKPT;
       
   422   }
       
   423   iPtrmap = PTRMAP_PAGENO(pBt, key);
       
   424   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
       
   425   if( rc!=SQLITE_OK ){
       
   426     return rc;
       
   427   }
       
   428   offset = PTRMAP_PTROFFSET(pBt, key);
       
   429   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
       
   430 
       
   431   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
       
   432     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
       
   433     rc = sqlite3PagerWrite(pDbPage);
       
   434     if( rc==SQLITE_OK ){
       
   435       pPtrmap[offset] = eType;
       
   436       put4byte(&pPtrmap[offset+1], parent);
       
   437     }
       
   438   }
       
   439 
       
   440   sqlite3PagerUnref(pDbPage);
       
   441   return rc;
       
   442 }
       
   443 
       
   444 /*
       
   445 ** Read an entry from the pointer map.
       
   446 **
       
   447 ** This routine retrieves the pointer map entry for page 'key', writing
       
   448 ** the type and parent page number to *pEType and *pPgno respectively.
       
   449 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
       
   450 */
       
   451 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
       
   452   DbPage *pDbPage;   /* The pointer map page */
       
   453   int iPtrmap;       /* Pointer map page index */
       
   454   u8 *pPtrmap;       /* Pointer map page data */
       
   455   int offset;        /* Offset of entry in pointer map */
       
   456   int rc;
       
   457 
       
   458   assert( sqlite3_mutex_held(pBt->mutex) );
       
   459 
       
   460   iPtrmap = PTRMAP_PAGENO(pBt, key);
       
   461   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
       
   462   if( rc!=0 ){
       
   463     return rc;
       
   464   }
       
   465   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
       
   466 
       
   467   offset = PTRMAP_PTROFFSET(pBt, key);
       
   468   assert( pEType!=0 );
       
   469   *pEType = pPtrmap[offset];
       
   470   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
       
   471 
       
   472   sqlite3PagerUnref(pDbPage);
       
   473   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
       
   474   return SQLITE_OK;
       
   475 }
       
   476 
       
   477 #endif /* SQLITE_OMIT_AUTOVACUUM */
       
   478 
       
   479 /*
       
   480 ** Given a btree page and a cell index (0 means the first cell on
       
   481 ** the page, 1 means the second cell, and so forth) return a pointer
       
   482 ** to the cell content.
       
   483 **
       
   484 ** This routine works only for pages that do not contain overflow cells.
       
   485 */
       
   486 #define findCell(pPage, iCell) \
       
   487   ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)]))
       
   488 #ifdef SQLITE_TEST
       
   489 u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){
       
   490   assert( iCell>=0 );
       
   491   assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) );
       
   492   return findCell(pPage, iCell);
       
   493 }
       
   494 #endif
       
   495 
       
   496 /*
       
   497 ** This a more complex version of sqlite3BtreeFindCell() that works for
       
   498 ** pages that do contain overflow cells.  See insert
       
   499 */
       
   500 static u8 *findOverflowCell(MemPage *pPage, int iCell){
       
   501   int i;
       
   502   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   503   for(i=pPage->nOverflow-1; i>=0; i--){
       
   504     int k;
       
   505 	MemPage::_OvflCell *pOvfl;
       
   506     pOvfl = &pPage->aOvfl[i];
       
   507     k = pOvfl->idx;
       
   508     if( k<=iCell ){
       
   509       if( k==iCell ){
       
   510         return pOvfl->pCell;
       
   511       }
       
   512       iCell--;
       
   513     }
       
   514   }
       
   515   return findCell(pPage, iCell);
       
   516 }
       
   517 
       
   518 /*
       
   519 ** Parse a cell content block and fill in the CellInfo structure.  There
       
   520 ** are two versions of this function.  sqlite3BtreeParseCell() takes a 
       
   521 ** cell index as the second argument and sqlite3BtreeParseCellPtr() 
       
   522 ** takes a pointer to the body of the cell as its second argument.
       
   523 **
       
   524 ** Within this file, the parseCell() macro can be called instead of
       
   525 ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
       
   526 */
       
   527 void sqlite3BtreeParseCellPtr(
       
   528   MemPage *pPage,         /* Page containing the cell */
       
   529   u8 *pCell,              /* Pointer to the cell text. */
       
   530   CellInfo *pInfo         /* Fill in this structure */
       
   531 ){
       
   532   int n;                  /* Number bytes in cell content header */
       
   533   u32 nPayload;           /* Number of bytes of cell payload */
       
   534 
       
   535   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   536 
       
   537   pInfo->pCell = pCell;
       
   538   assert( pPage->leaf==0 || pPage->leaf==1 );
       
   539   n = pPage->childPtrSize;
       
   540   assert( n==4-4*pPage->leaf );
       
   541   if( pPage->hasData ){
       
   542     n += getVarint32(&pCell[n], &nPayload);
       
   543   }else{
       
   544     nPayload = 0;
       
   545   }
       
   546   pInfo->nData = nPayload;
       
   547   if( pPage->intKey ){
       
   548     n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
       
   549   }else{
       
   550     u32 x;
       
   551     n += getVarint32(&pCell[n], &x);
       
   552     pInfo->nKey = x;
       
   553     nPayload += x;
       
   554   }
       
   555   pInfo->nPayload = nPayload;
       
   556   pInfo->nHeader = n;
       
   557   if( nPayload<=pPage->maxLocal ){
       
   558     /* This is the (easy) common case where the entire payload fits
       
   559     ** on the local page.  No overflow is required.
       
   560     */
       
   561     int nSize;          /* Total size of cell content in bytes */
       
   562     pInfo->nLocal = nPayload;
       
   563     pInfo->iOverflow = 0;
       
   564     nSize = nPayload + n;
       
   565     if( nSize<4 ){
       
   566       nSize = 4;        /* Minimum cell size is 4 */
       
   567     }
       
   568     pInfo->nSize = nSize;
       
   569   }else{
       
   570     /* If the payload will not fit completely on the local page, we have
       
   571     ** to decide how much to store locally and how much to spill onto
       
   572     ** overflow pages.  The strategy is to minimize the amount of unused
       
   573     ** space on overflow pages while keeping the amount of local storage
       
   574     ** in between minLocal and maxLocal.
       
   575     **
       
   576     ** Warning:  changing the way overflow payload is distributed in any
       
   577     ** way will result in an incompatible file format.
       
   578     */
       
   579     int minLocal;  /* Minimum amount of payload held locally */
       
   580     int maxLocal;  /* Maximum amount of payload held locally */
       
   581     int surplus;   /* Overflow payload available for local storage */
       
   582 
       
   583     minLocal = pPage->minLocal;
       
   584     maxLocal = pPage->maxLocal;
       
   585     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
       
   586     if( surplus <= maxLocal ){
       
   587       pInfo->nLocal = surplus;
       
   588     }else{
       
   589       pInfo->nLocal = minLocal;
       
   590     }
       
   591     pInfo->iOverflow = pInfo->nLocal + n;
       
   592     pInfo->nSize = pInfo->iOverflow + 4;
       
   593   }
       
   594 }
       
   595 #define parseCell(pPage, iCell, pInfo) \
       
   596   sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
       
   597 void sqlite3BtreeParseCell(
       
   598   MemPage *pPage,         /* Page containing the cell */
       
   599   int iCell,              /* The cell index.  First cell is 0 */
       
   600   CellInfo *pInfo         /* Fill in this structure */
       
   601 ){
       
   602   parseCell(pPage, iCell, pInfo);
       
   603 }
       
   604 
       
   605 /*
       
   606 ** Compute the total number of bytes that a Cell needs in the cell
       
   607 ** data area of the btree-page.  The return number includes the cell
       
   608 ** data header and the local payload, but not any overflow page or
       
   609 ** the space used by the cell pointer.
       
   610 */
       
   611 #ifndef NDEBUG
       
   612 static int cellSize(MemPage *pPage, int iCell){
       
   613   CellInfo info;
       
   614   sqlite3BtreeParseCell(pPage, iCell, &info);
       
   615   return info.nSize;
       
   616 }
       
   617 #endif
       
   618 static int cellSizePtr(MemPage *pPage, u8 *pCell){
       
   619   CellInfo info;
       
   620   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
   621   return info.nSize;
       
   622 }
       
   623 
       
   624 #ifndef SQLITE_OMIT_AUTOVACUUM
       
   625 /*
       
   626 ** If the cell pCell, part of page pPage contains a pointer
       
   627 ** to an overflow page, insert an entry into the pointer-map
       
   628 ** for the overflow page.
       
   629 */
       
   630 static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
       
   631   if( pCell ){
       
   632     CellInfo info;
       
   633     sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
   634     assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
       
   635     if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
       
   636       Pgno ovfl = get4byte(&pCell[info.iOverflow]);
       
   637       return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
       
   638     }
       
   639   }
       
   640   return SQLITE_OK;
       
   641 }
       
   642 /*
       
   643 ** If the cell with index iCell on page pPage contains a pointer
       
   644 ** to an overflow page, insert an entry into the pointer-map
       
   645 ** for the overflow page.
       
   646 */
       
   647 static int ptrmapPutOvfl(MemPage *pPage, int iCell){
       
   648   u8 *pCell;
       
   649   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   650   pCell = findOverflowCell(pPage, iCell);
       
   651   return ptrmapPutOvflPtr(pPage, pCell);
       
   652 }
       
   653 #endif
       
   654 
       
   655 
       
   656 /*
       
   657 ** Defragment the page given.  All Cells are moved to the
       
   658 ** end of the page and all free space is collected into one
       
   659 ** big FreeBlk that occurs in between the header and cell
       
   660 ** pointer array and the cell content area.
       
   661 */
       
   662 static int defragmentPage(MemPage *pPage){
       
   663   int i;                     /* Loop counter */
       
   664   int pc;                    /* Address of a i-th cell */
       
   665   int addr;                  /* Offset of first byte after cell pointer array */
       
   666   int hdr;                   /* Offset to the page header */
       
   667   int size;                  /* Size of a cell */
       
   668   int usableSize;            /* Number of usable bytes on a page */
       
   669   int cellOffset;            /* Offset to the cell pointer array */
       
   670   int brk;                   /* Offset to the cell content area */
       
   671   int nCell;                 /* Number of cells on the page */
       
   672   unsigned char *data;       /* The page data */
       
   673   unsigned char *temp;       /* Temp area for cell content */
       
   674 
       
   675   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
   676   assert( pPage->pBt!=0 );
       
   677   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
       
   678   assert( pPage->nOverflow==0 );
       
   679   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   680   temp = (unsigned char*)sqlite3PagerTempSpace(pPage->pBt->pPager);
       
   681   data = pPage->aData;
       
   682   hdr = pPage->hdrOffset;
       
   683   cellOffset = pPage->cellOffset;
       
   684   nCell = pPage->nCell;
       
   685   assert( nCell==get2byte(&data[hdr+3]) );
       
   686   usableSize = pPage->pBt->usableSize;
       
   687   brk = get2byte(&data[hdr+5]);
       
   688   memcpy(&temp[brk], &data[brk], usableSize - brk);
       
   689   brk = usableSize;
       
   690   for(i=0; i<nCell; i++){
       
   691     u8 *pAddr;     /* The i-th cell pointer */
       
   692     pAddr = &data[cellOffset + i*2];
       
   693     pc = get2byte(pAddr);
       
   694     assert( pc<pPage->pBt->usableSize );
       
   695     size = cellSizePtr(pPage, &temp[pc]);
       
   696     brk -= size;
       
   697     memcpy(&data[brk], &temp[pc], size);
       
   698     put2byte(pAddr, brk);
       
   699   }
       
   700   assert( brk>=cellOffset+2*nCell );
       
   701   put2byte(&data[hdr+5], brk);
       
   702   data[hdr+1] = 0;
       
   703   data[hdr+2] = 0;
       
   704   data[hdr+7] = 0;
       
   705   addr = cellOffset+2*nCell;
       
   706   memset(&data[addr], 0, brk-addr);
       
   707   return SQLITE_OK;
       
   708 }
       
   709 
       
   710 /*
       
   711 ** Allocate nByte bytes of space on a page.
       
   712 **
       
   713 ** Return the index into pPage->aData[] of the first byte of
       
   714 ** the new allocation. Or return 0 if there is not enough free
       
   715 ** space on the page to satisfy the allocation request.
       
   716 **
       
   717 ** If the page contains nBytes of free space but does not contain
       
   718 ** nBytes of contiguous free space, then this routine automatically
       
   719 ** calls defragementPage() to consolidate all free space before 
       
   720 ** allocating the new chunk.
       
   721 */
       
   722 static int allocateSpace(MemPage *pPage, int nByte){
       
   723   int addr, pc, hdr;
       
   724   int size;
       
   725   int nFrag;
       
   726   int top;
       
   727   int nCell;
       
   728   int cellOffset;
       
   729   unsigned char *data;
       
   730   
       
   731   data = pPage->aData;
       
   732   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
   733   assert( pPage->pBt );
       
   734   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   735   if( nByte<4 ) nByte = 4;
       
   736   if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
       
   737   pPage->nFree -= nByte;
       
   738   hdr = pPage->hdrOffset;
       
   739 
       
   740   nFrag = data[hdr+7];
       
   741   if( nFrag<60 ){
       
   742     /* Search the freelist looking for a slot big enough to satisfy the
       
   743     ** space request. */
       
   744     addr = hdr+1;
       
   745     while( (pc = get2byte(&data[addr]))>0 ){
       
   746       size = get2byte(&data[pc+2]);
       
   747       if( size>=nByte ){
       
   748         if( size<nByte+4 ){
       
   749           memcpy(&data[addr], &data[pc], 2);
       
   750           data[hdr+7] = nFrag + size - nByte;
       
   751           return pc;
       
   752         }else{
       
   753           put2byte(&data[pc+2], size-nByte);
       
   754           return pc + size - nByte;
       
   755         }
       
   756       }
       
   757       addr = pc;
       
   758     }
       
   759   }
       
   760 
       
   761   /* Allocate memory from the gap in between the cell pointer array
       
   762   ** and the cell content area.
       
   763   */
       
   764   top = get2byte(&data[hdr+5]);
       
   765   nCell = get2byte(&data[hdr+3]);
       
   766   cellOffset = pPage->cellOffset;
       
   767   if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
       
   768     if( defragmentPage(pPage) ) return 0;
       
   769     top = get2byte(&data[hdr+5]);
       
   770   }
       
   771   top -= nByte;
       
   772   assert( cellOffset + 2*nCell <= top );
       
   773   put2byte(&data[hdr+5], top);
       
   774   return top;
       
   775 }
       
   776 
       
   777 /*
       
   778 ** Return a section of the pPage->aData to the freelist.
       
   779 ** The first byte of the new free block is pPage->aDisk[start]
       
   780 ** and the size of the block is "size" bytes.
       
   781 **
       
   782 ** Most of the effort here is involved in coalesing adjacent
       
   783 ** free blocks into a single big free block.
       
   784 */
       
   785 static void freeSpace(MemPage *pPage, int start, int size){
       
   786   int addr, pbegin, hdr;
       
   787   unsigned char *data = pPage->aData;
       
   788 
       
   789   assert( pPage->pBt!=0 );
       
   790   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
   791   assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
       
   792   assert( (start + size)<=pPage->pBt->usableSize );
       
   793   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   794   if( size<4 ) size = 4;
       
   795 
       
   796 #ifdef SQLITE_SECURE_DELETE
       
   797   /* Overwrite deleted information with zeros when the SECURE_DELETE 
       
   798   ** option is enabled at compile-time */
       
   799   memset(&data[start], 0, size);
       
   800 #endif
       
   801 
       
   802   /* Add the space back into the linked list of freeblocks */
       
   803   hdr = pPage->hdrOffset;
       
   804   addr = hdr + 1;
       
   805   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
       
   806     assert( pbegin<=pPage->pBt->usableSize-4 );
       
   807     assert( pbegin>addr );
       
   808     addr = pbegin;
       
   809   }
       
   810   assert( pbegin<=pPage->pBt->usableSize-4 );
       
   811   assert( pbegin>addr || pbegin==0 );
       
   812   put2byte(&data[addr], start);
       
   813   put2byte(&data[start], pbegin);
       
   814   put2byte(&data[start+2], size);
       
   815   pPage->nFree += size;
       
   816 
       
   817   /* Coalesce adjacent free blocks */
       
   818   addr = pPage->hdrOffset + 1;
       
   819   while( (pbegin = get2byte(&data[addr]))>0 ){
       
   820     int pnext, psize;
       
   821     assert( pbegin>addr );
       
   822     assert( pbegin<=pPage->pBt->usableSize-4 );
       
   823     pnext = get2byte(&data[pbegin]);
       
   824     psize = get2byte(&data[pbegin+2]);
       
   825     if( pbegin + psize + 3 >= pnext && pnext>0 ){
       
   826       int frag = pnext - (pbegin+psize);
       
   827       assert( frag<=data[pPage->hdrOffset+7] );
       
   828       data[pPage->hdrOffset+7] -= frag;
       
   829       put2byte(&data[pbegin], get2byte(&data[pnext]));
       
   830       put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
       
   831     }else{
       
   832       addr = pbegin;
       
   833     }
       
   834   }
       
   835 
       
   836   /* If the cell content area begins with a freeblock, remove it. */
       
   837   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
       
   838     int top;
       
   839     pbegin = get2byte(&data[hdr+1]);
       
   840     memcpy(&data[hdr+1], &data[pbegin], 2);
       
   841     top = get2byte(&data[hdr+5]);
       
   842     put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
       
   843   }
       
   844 }
       
   845 
       
   846 /*
       
   847 ** Decode the flags byte (the first byte of the header) for a page
       
   848 ** and initialize fields of the MemPage structure accordingly.
       
   849 */
       
   850 static void decodeFlags(MemPage *pPage, int flagByte){
       
   851   BtShared *pBt;     /* A copy of pPage->pBt */
       
   852 
       
   853   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
       
   854   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
   855   pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
       
   856   pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
       
   857   pPage->leaf = (flagByte & PTF_LEAF)!=0;
       
   858   pPage->childPtrSize = 4*(pPage->leaf==0);
       
   859   pBt = pPage->pBt;
       
   860   if( flagByte & PTF_LEAFDATA ){
       
   861     pPage->leafData = 1;
       
   862     pPage->maxLocal = pBt->maxLeaf;
       
   863     pPage->minLocal = pBt->minLeaf;
       
   864   }else{
       
   865     pPage->leafData = 0;
       
   866     pPage->maxLocal = pBt->maxLocal;
       
   867     pPage->minLocal = pBt->minLocal;
       
   868   }
       
   869   pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
       
   870 }
       
   871 
       
   872 /*
       
   873 ** Initialize the auxiliary information for a disk block.
       
   874 **
       
   875 ** The pParent parameter must be a pointer to the MemPage which
       
   876 ** is the parent of the page being initialized.  The root of a
       
   877 ** BTree has no parent and so for that page, pParent==NULL.
       
   878 **
       
   879 ** Return SQLITE_OK on success.  If we see that the page does
       
   880 ** not contain a well-formed database page, then return 
       
   881 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
       
   882 ** guarantee that the page is well-formed.  It only shows that
       
   883 ** we failed to detect any corruption.
       
   884 */
       
   885 int sqlite3BtreeInitPage(
       
   886   MemPage *pPage,        /* The page to be initialized */
       
   887   MemPage *pParent       /* The parent.  Might be NULL */
       
   888 ){
       
   889   int pc;            /* Address of a freeblock within pPage->aData[] */
       
   890   int hdr;           /* Offset to beginning of page header */
       
   891   u8 *data;          /* Equal to pPage->aData */
       
   892   BtShared *pBt;        /* The main btree structure */
       
   893   int usableSize;    /* Amount of usable space on each page */
       
   894   int cellOffset;    /* Offset from start of page to first cell pointer */
       
   895   int nFree;         /* Number of unused bytes on the page */
       
   896   int top;           /* First byte of the cell content area */
       
   897 
       
   898   pBt = pPage->pBt;
       
   899   assert( pBt!=0 );
       
   900   assert( pParent==0 || pParent->pBt==pBt );
       
   901   assert( sqlite3_mutex_held(pBt->mutex) );
       
   902   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
       
   903   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
       
   904   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
       
   905   if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
       
   906     /* The parent page should never change unless the file is corrupt */
       
   907     return SQLITE_CORRUPT_BKPT;
       
   908   }
       
   909   if( pPage->isInit ) return SQLITE_OK;
       
   910   if( pPage->pParent==0 && pParent!=0 ){
       
   911     pPage->pParent = pParent;
       
   912     sqlite3PagerRef(pParent->pDbPage);
       
   913   }
       
   914   hdr = pPage->hdrOffset;
       
   915   data = pPage->aData;
       
   916   decodeFlags(pPage, data[hdr]);
       
   917   pPage->nOverflow = 0;
       
   918   pPage->idxShift = 0;
       
   919   usableSize = pBt->usableSize;
       
   920   pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
       
   921   top = get2byte(&data[hdr+5]);
       
   922   pPage->nCell = get2byte(&data[hdr+3]);
       
   923   if( pPage->nCell>MX_CELL(pBt) ){
       
   924     /* To many cells for a single page.  The page must be corrupt */
       
   925     return SQLITE_CORRUPT_BKPT;
       
   926   }
       
   927   if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
       
   928     /* All pages must have at least one cell, except for root pages */
       
   929     return SQLITE_CORRUPT_BKPT;
       
   930   }
       
   931 
       
   932   /* Compute the total free space on the page */
       
   933   pc = get2byte(&data[hdr+1]);
       
   934   nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
       
   935   while( pc>0 ){
       
   936     int next, size;
       
   937     if( pc>usableSize-4 ){
       
   938       /* Free block is off the page */
       
   939       return SQLITE_CORRUPT_BKPT; 
       
   940     }
       
   941     next = get2byte(&data[pc]);
       
   942     size = get2byte(&data[pc+2]);
       
   943     if( next>0 && next<=pc+size+3 ){
       
   944       /* Free blocks must be in accending order */
       
   945       return SQLITE_CORRUPT_BKPT; 
       
   946     }
       
   947     nFree += size;
       
   948     pc = next;
       
   949   }
       
   950   pPage->nFree = nFree;
       
   951   if( nFree>=usableSize ){
       
   952     /* Free space cannot exceed total page size */
       
   953     return SQLITE_CORRUPT_BKPT; 
       
   954   }
       
   955 
       
   956   pPage->isInit = 1;
       
   957   return SQLITE_OK;
       
   958 }
       
   959 
       
   960 /*
       
   961 ** Set up a raw page so that it looks like a database page holding
       
   962 ** no entries.
       
   963 */
       
   964 static void zeroPage(MemPage *pPage, int flags){
       
   965   unsigned char *data = pPage->aData;
       
   966   BtShared *pBt = pPage->pBt;
       
   967   int hdr = pPage->hdrOffset;
       
   968   int first;
       
   969 
       
   970   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
       
   971   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
       
   972   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
       
   973   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
   974   assert( sqlite3_mutex_held(pBt->mutex) );
       
   975   memset(&data[hdr], 0, pBt->usableSize - hdr);
       
   976   data[hdr] = flags;
       
   977   first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
       
   978   memset(&data[hdr+1], 0, 4);
       
   979   data[hdr+7] = 0;
       
   980   put2byte(&data[hdr+5], pBt->usableSize);
       
   981   pPage->nFree = pBt->usableSize - first;
       
   982   decodeFlags(pPage, flags);
       
   983   pPage->hdrOffset = hdr;
       
   984   pPage->cellOffset = first;
       
   985   pPage->nOverflow = 0;
       
   986   pPage->idxShift = 0;
       
   987   pPage->nCell = 0;
       
   988   pPage->isInit = 1;
       
   989 }
       
   990 
       
   991 /*
       
   992 ** Get a page from the pager.  Initialize the MemPage.pBt and
       
   993 ** MemPage.aData elements if needed.
       
   994 **
       
   995 ** If the noContent flag is set, it means that we do not care about
       
   996 ** the content of the page at this time.  So do not go to the disk
       
   997 ** to fetch the content.  Just fill in the content with zeros for now.
       
   998 ** If in the future we call sqlite3PagerWrite() on this page, that
       
   999 ** means we have started to be concerned about content and the disk
       
  1000 ** read should occur at that point.
       
  1001 */
       
  1002 int sqlite3BtreeGetPage(
       
  1003   BtShared *pBt,       /* The btree */
       
  1004   Pgno pgno,           /* Number of the page to fetch */
       
  1005   MemPage **ppPage,    /* Return the page in this parameter */
       
  1006   int noContent        /* Do not load page content if true */
       
  1007 ){
       
  1008   int rc;
       
  1009   MemPage *pPage;
       
  1010   DbPage *pDbPage;
       
  1011 
       
  1012   assert( sqlite3_mutex_held(pBt->mutex) );
       
  1013   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
       
  1014   if( rc ) return rc;
       
  1015   pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
       
  1016   pPage->aData = (u8*)sqlite3PagerGetData(pDbPage);
       
  1017   pPage->pDbPage = pDbPage;
       
  1018   pPage->pBt = pBt;
       
  1019   pPage->pgno = pgno;
       
  1020   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
       
  1021   *ppPage = pPage;
       
  1022   return SQLITE_OK;
       
  1023 }
       
  1024 
       
  1025 /*
       
  1026 ** Get a page from the pager and initialize it.  This routine
       
  1027 ** is just a convenience wrapper around separate calls to
       
  1028 ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
       
  1029 */
       
  1030 static int getAndInitPage(
       
  1031   BtShared *pBt,          /* The database file */
       
  1032   Pgno pgno,           /* Number of the page to get */
       
  1033   MemPage **ppPage,    /* Write the page pointer here */
       
  1034   MemPage *pParent     /* Parent of the page */
       
  1035 ){
       
  1036   int rc;
       
  1037   assert( sqlite3_mutex_held(pBt->mutex) );
       
  1038   if( pgno==0 ){
       
  1039     return SQLITE_CORRUPT_BKPT; 
       
  1040   }
       
  1041   rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
       
  1042   if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
       
  1043     rc = sqlite3BtreeInitPage(*ppPage, pParent);
       
  1044   }
       
  1045   return rc;
       
  1046 }
       
  1047 
       
  1048 /*
       
  1049 ** Release a MemPage.  This should be called once for each prior
       
  1050 ** call to sqlite3BtreeGetPage.
       
  1051 */
       
  1052 static void releasePage(MemPage *pPage){
       
  1053   if( pPage ){
       
  1054     assert( pPage->aData );
       
  1055     assert( pPage->pBt );
       
  1056     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
       
  1057     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
       
  1058     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  1059     sqlite3PagerUnref(pPage->pDbPage);
       
  1060   }
       
  1061 }
       
  1062 
       
  1063 /*
       
  1064 ** This routine is called when the reference count for a page
       
  1065 ** reaches zero.  We need to unref the pParent pointer when that
       
  1066 ** happens.
       
  1067 */
       
  1068 static void pageDestructor(DbPage *pData, int pageSize){
       
  1069   MemPage *pPage;
       
  1070   assert( (pageSize & 7)==0 );
       
  1071   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
       
  1072   assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
       
  1073   if( pPage->pParent ){
       
  1074     MemPage *pParent = pPage->pParent;
       
  1075     assert( pParent->pBt==pPage->pBt );
       
  1076     pPage->pParent = 0;
       
  1077     releasePage(pParent);
       
  1078   }
       
  1079   pPage->isInit = 0;
       
  1080 }
       
  1081 
       
  1082 /*
       
  1083 ** During a rollback, when the pager reloads information into the cache
       
  1084 ** so that the cache is restored to its original state at the start of
       
  1085 ** the transaction, for each page restored this routine is called.
       
  1086 **
       
  1087 ** This routine needs to reset the extra data section at the end of the
       
  1088 ** page to agree with the restored data.
       
  1089 */
       
  1090 static void pageReinit(DbPage *pData, int pageSize){
       
  1091   MemPage *pPage;
       
  1092   assert( (pageSize & 7)==0 );
       
  1093   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
       
  1094   if( pPage->isInit ){
       
  1095     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  1096     pPage->isInit = 0;
       
  1097     sqlite3BtreeInitPage(pPage, pPage->pParent);
       
  1098   }
       
  1099 }
       
  1100 
       
  1101 /*
       
  1102 ** Invoke the busy handler for a btree.
       
  1103 */
       
  1104 static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
       
  1105   BtShared *pBt = (BtShared*)pArg;
       
  1106   assert( pBt->db );
       
  1107   assert( sqlite3_mutex_held(pBt->db->mutex) );
       
  1108   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
       
  1109 }
       
  1110 
       
  1111 /*
       
  1112 ** Open a database file.
       
  1113 ** 
       
  1114 ** zFilename is the name of the database file.  If zFilename is NULL
       
  1115 ** a new database with a random name is created.  This randomly named
       
  1116 ** database file will be deleted when sqlite3BtreeClose() is called.
       
  1117 ** If zFilename is ":memory:" then an in-memory database is created
       
  1118 ** that is automatically destroyed when it is closed.
       
  1119 */
       
  1120 int sqlite3BtreeOpen(
       
  1121   const char *zFilename,  /* Name of the file containing the BTree database */
       
  1122   sqlite3 *db,            /* Associated database handle */
       
  1123   Btree **ppBtree,        /* Pointer to new Btree object written here */
       
  1124   int flags,              /* Options */
       
  1125   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
       
  1126 ){
       
  1127   sqlite3_vfs *pVfs;      /* The VFS to use for this btree */
       
  1128   BtShared *pBt = 0;      /* Shared part of btree structure */
       
  1129   Btree *p;               /* Handle to return */
       
  1130   int rc = SQLITE_OK;
       
  1131   int nReserve;
       
  1132   unsigned char zDbHeader[100];
       
  1133 
       
  1134   /* Set the variable isMemdb to true for an in-memory database, or 
       
  1135   ** false for a file-based database. This symbol is only required if
       
  1136   ** either of the shared-data or autovacuum features are compiled 
       
  1137   ** into the library.
       
  1138   */
       
  1139 #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
       
  1140   #ifdef SQLITE_OMIT_MEMORYDB
       
  1141     const int isMemdb = 0;
       
  1142   #else
       
  1143     const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
       
  1144   #endif
       
  1145 #endif
       
  1146 
       
  1147   assert( db!=0 );
       
  1148   assert( sqlite3_mutex_held(db->mutex) );
       
  1149 
       
  1150   pVfs = db->pVfs;
       
  1151   p = (Btree*)sqlite3MallocZero(sizeof(Btree));
       
  1152   if( !p ){
       
  1153     return SQLITE_NOMEM;
       
  1154   }
       
  1155   p->inTrans = TRANS_NONE;
       
  1156   p->db = db;
       
  1157 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
  1158   /*
       
  1159   ** If this Btree is a candidate for shared cache, try to find an
       
  1160   ** existing BtShared object that we can share with
       
  1161   */
       
  1162 
       
  1163   if( (flags & BTREE_PRIVATE)==0
       
  1164    && isMemdb==0
       
  1165    && (db->flags & SQLITE_Vtab)==0
       
  1166    && zFilename && zFilename[0]
       
  1167   ){
       
  1168     if( sqlite3SharedCacheEnabled ){
       
  1169       int nFullPathname = pVfs->mxPathname+1;
       
  1170 
       
  1171       char *zFullPathname = (char *)sqlite3_malloc(nFullPathname);
       
  1172       
       
  1173       sqlite3_mutex *mutexShared;
       
  1174       p->sharable = 1;
       
  1175       if( db ){
       
  1176         db->flags |= SQLITE_SharedCache;
       
  1177       }
       
  1178       if( !zFullPathname ){
       
  1179         sqlite3_free(p);
       
  1180         return SQLITE_NOMEM;
       
  1181       }
       
  1182 
       
  1183       sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
       
  1184       mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
  1185       sqlite3_mutex_enter(mutexShared);
       
  1186       for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
       
  1187         assert( pBt->nRef>0 );
       
  1188         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
       
  1189                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
       
  1190           p->pBt = pBt;
       
  1191           pBt->nRef++;
       
  1192           break;
       
  1193         }
       
  1194       }
       
  1195       sqlite3_mutex_leave(mutexShared);
       
  1196       sqlite3_free(zFullPathname);
       
  1197     }
       
  1198 #ifdef SQLITE_DEBUG
       
  1199     else{
       
  1200       /* In debug mode, we mark all persistent databases as sharable
       
  1201       ** even when they are not.  This exercises the locking code and
       
  1202       ** gives more opportunity for asserts(sqlite3_mutex_held())
       
  1203       ** statements to find locking problems.
       
  1204       */
       
  1205       p->sharable = 1;
       
  1206     }
       
  1207 #endif
       
  1208   }
       
  1209 #endif
       
  1210 
       
  1211   if( pBt==0 ){
       
  1212 
       
  1213 	/*
       
  1214     ** The following asserts make sure that structures used by the btree are
       
  1215     ** the right size.  This is to guard against size changes that result
       
  1216     ** when compiling on a different architecture.
       
  1217     */
       
  1218     assert( sizeof(i64)==8 || sizeof(i64)==4 );
       
  1219     assert( sizeof(u64)==8 || sizeof(u64)==4 );
       
  1220     assert( sizeof(u32)==4 );
       
  1221     assert( sizeof(u16)==2 );
       
  1222     assert( sizeof(Pgno)==4 );
       
  1223   
       
  1224     pBt = (BtShared*)sqlite3MallocZero( sizeof(*pBt) );
       
  1225     if( pBt==0 ){
       
  1226       rc = SQLITE_NOMEM;
       
  1227       goto btree_open_out;
       
  1228     }
       
  1229     pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
       
  1230     pBt->busyHdr.pArg = pBt;
       
  1231 
       
  1232 
       
  1233 	rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
       
  1234                           EXTRA_SIZE, flags, vfsFlags);
       
  1235     if( rc==SQLITE_OK ){
       
  1236       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
       
  1237     }
       
  1238     if( rc!=SQLITE_OK ){
       
  1239       goto btree_open_out;
       
  1240     }
       
  1241     
       
  1242     sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
       
  1243     p->pBt = pBt;
       
  1244   
       
  1245     sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
       
  1246     sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
       
  1247     pBt->pCursor = 0;
       
  1248     pBt->pPage1 = 0;
       
  1249     pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
       
  1250     pBt->pageSize = get2byte(&zDbHeader[16]);
       
  1251     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
       
  1252          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
       
  1253       pBt->pageSize = 0;
       
  1254       sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
       
  1255       pBt->maxEmbedFrac = 64;   /* 25% */
       
  1256       pBt->minEmbedFrac = 32;   /* 12.5% */
       
  1257       pBt->minLeafFrac = 32;    /* 12.5% */
       
  1258 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  1259       /* If the magic name ":memory:" will create an in-memory database, then
       
  1260       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
       
  1261       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
       
  1262       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
       
  1263       ** regular file-name. In this case the auto-vacuum applies as per normal.
       
  1264       */
       
  1265       if( zFilename && !isMemdb ){
       
  1266         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
       
  1267         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
       
  1268       }
       
  1269 #endif
       
  1270       nReserve = 0;
       
  1271     }else{
       
  1272       nReserve = zDbHeader[20];
       
  1273       pBt->maxEmbedFrac = zDbHeader[21];
       
  1274       pBt->minEmbedFrac = zDbHeader[22];
       
  1275       pBt->minLeafFrac = zDbHeader[23];
       
  1276       pBt->pageSizeFixed = 1;
       
  1277 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  1278       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
       
  1279       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
       
  1280 #endif
       
  1281     }
       
  1282     pBt->usableSize = pBt->pageSize - nReserve;
       
  1283     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
       
  1284     sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
       
  1285    
       
  1286 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
  1287     /* Add the new BtShared object to the linked list sharable BtShareds.
       
  1288     */
       
  1289     if( p->sharable ){
       
  1290       sqlite3_mutex *mutexShared;
       
  1291       pBt->nRef = 1;
       
  1292       mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
  1293       if( SQLITE_THREADSAFE ){
       
  1294         pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
       
  1295         if( pBt->mutex==0 ){
       
  1296           rc = SQLITE_NOMEM;
       
  1297           db->mallocFailed = 0;
       
  1298           goto btree_open_out;
       
  1299         }
       
  1300       }
       
  1301       sqlite3_mutex_enter(mutexShared);
       
  1302       pBt->pNext = sqlite3SharedCacheList;
       
  1303       sqlite3SharedCacheList = pBt;
       
  1304       sqlite3_mutex_leave(mutexShared);
       
  1305     }
       
  1306 #endif
       
  1307   }
       
  1308 
       
  1309 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
       
  1310   /* If the new Btree uses a sharable pBtShared, then link the new
       
  1311   ** Btree into the list of all sharable Btrees for the same connection.
       
  1312   ** The list is kept in ascending order by pBt address.
       
  1313   */
       
  1314   if( p->sharable ){
       
  1315     int i;
       
  1316     Btree *pSib;
       
  1317     for(i=0; i<db->nDb; i++){
       
  1318       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
       
  1319         while( pSib->pPrev ){ pSib = pSib->pPrev; }
       
  1320         if( p->pBt<pSib->pBt ){
       
  1321           p->pNext = pSib;
       
  1322           p->pPrev = 0;
       
  1323           pSib->pPrev = p;
       
  1324         }else{
       
  1325           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
       
  1326             pSib = pSib->pNext;
       
  1327           }
       
  1328           p->pNext = pSib->pNext;
       
  1329           p->pPrev = pSib;
       
  1330           if( p->pNext ){
       
  1331             p->pNext->pPrev = p;
       
  1332           }
       
  1333           pSib->pNext = p;
       
  1334         }
       
  1335         break;
       
  1336       }
       
  1337     }
       
  1338   }
       
  1339 #endif
       
  1340   *ppBtree = p;
       
  1341 
       
  1342 btree_open_out:
       
  1343   if( rc!=SQLITE_OK ){
       
  1344     if( pBt && pBt->pPager ){
       
  1345       sqlite3PagerClose(pBt->pPager);
       
  1346     }
       
  1347     sqlite3_free(pBt);
       
  1348     sqlite3_free(p);
       
  1349     *ppBtree = 0;
       
  1350   }
       
  1351   return rc;
       
  1352 }
       
  1353 
       
  1354 /*
       
  1355 ** Decrement the BtShared.nRef counter.  When it reaches zero,
       
  1356 ** remove the BtShared structure from the sharing list.  Return
       
  1357 ** true if the BtShared.nRef counter reaches zero and return
       
  1358 ** false if it is still positive.
       
  1359 */
       
  1360 static int removeFromSharingList(BtShared *pBt){
       
  1361 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  1362   sqlite3_mutex *pMaster;
       
  1363   BtShared *pList;
       
  1364   int removed = 0;
       
  1365 
       
  1366   assert( sqlite3_mutex_notheld(pBt->mutex) );
       
  1367   pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
  1368   sqlite3_mutex_enter(pMaster);
       
  1369   pBt->nRef--;
       
  1370   if( pBt->nRef<=0 ){
       
  1371     if( sqlite3SharedCacheList==pBt ){
       
  1372       sqlite3SharedCacheList = pBt->pNext;
       
  1373     }else{
       
  1374       pList = sqlite3SharedCacheList;
       
  1375       while( pList && pList->pNext!=pBt ){
       
  1376         pList=pList->pNext;
       
  1377       }
       
  1378       if( pList ){
       
  1379         pList->pNext = pBt->pNext;
       
  1380       }
       
  1381     }
       
  1382     if( SQLITE_THREADSAFE ){
       
  1383       sqlite3_mutex_free(pBt->mutex);
       
  1384     }
       
  1385     removed = 1;
       
  1386   }
       
  1387   sqlite3_mutex_leave(pMaster);
       
  1388   return removed;
       
  1389 #else
       
  1390   return 1;
       
  1391 #endif
       
  1392 }
       
  1393 
       
  1394 /*
       
  1395 ** Close an open database and invalidate all cursors.
       
  1396 */
       
  1397 int sqlite3BtreeClose(Btree *p){
       
  1398   BtShared *pBt = p->pBt;
       
  1399   BtCursor *pCur;
       
  1400 
       
  1401   /* Close all cursors opened via this handle.  */
       
  1402   assert( sqlite3_mutex_held(p->db->mutex) );
       
  1403   sqlite3BtreeEnter(p);
       
  1404   pBt->db = p->db;
       
  1405   pCur = pBt->pCursor;
       
  1406   while( pCur ){
       
  1407     BtCursor *pTmp = pCur;
       
  1408     pCur = pCur->pNext;
       
  1409     if( pTmp->pBtree==p ){
       
  1410       sqlite3BtreeCloseCursor(pTmp);
       
  1411     }
       
  1412   }
       
  1413 
       
  1414   /* Rollback any active transaction and free the handle structure.
       
  1415   ** The call to sqlite3BtreeRollback() drops any table-locks held by
       
  1416   ** this handle.
       
  1417   */
       
  1418   sqlite3BtreeRollback(p);
       
  1419   sqlite3BtreeLeave(p);
       
  1420 
       
  1421   /* If there are still other outstanding references to the shared-btree
       
  1422   ** structure, return now. The remainder of this procedure cleans 
       
  1423   ** up the shared-btree.
       
  1424   */
       
  1425   assert( p->wantToLock==0 && p->locked==0 );
       
  1426   if( !p->sharable || removeFromSharingList(pBt) ){
       
  1427     /* The pBt is no longer on the sharing list, so we can access
       
  1428     ** it without having to hold the mutex.
       
  1429     **
       
  1430     ** Clean out and delete the BtShared object.
       
  1431     */
       
  1432     assert( !pBt->pCursor );
       
  1433     sqlite3PagerClose(pBt->pPager);
       
  1434     if( pBt->xFreeSchema && pBt->pSchema ){
       
  1435       pBt->xFreeSchema(pBt->pSchema);
       
  1436     }
       
  1437     sqlite3_free(pBt->pSchema);
       
  1438     sqlite3_free(pBt);
       
  1439   }
       
  1440 
       
  1441 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  1442   assert( p->wantToLock==0 );
       
  1443   assert( p->locked==0 );
       
  1444   if( p->pPrev ) p->pPrev->pNext = p->pNext;
       
  1445   if( p->pNext ) p->pNext->pPrev = p->pPrev;
       
  1446 #endif
       
  1447 
       
  1448   sqlite3_free(p);
       
  1449   return SQLITE_OK;
       
  1450 }
       
  1451 
       
  1452 /*
       
  1453 ** Change the limit on the number of pages allowed in the cache.
       
  1454 **
       
  1455 ** The maximum number of cache pages is set to the absolute
       
  1456 ** value of mxPage.  If mxPage is negative, the pager will
       
  1457 ** operate asynchronously - it will not stop to do fsync()s
       
  1458 ** to insure data is written to the disk surface before
       
  1459 ** continuing.  Transactions still work if synchronous is off,
       
  1460 ** and the database cannot be corrupted if this program
       
  1461 ** crashes.  But if the operating system crashes or there is
       
  1462 ** an abrupt power failure when synchronous is off, the database
       
  1463 ** could be left in an inconsistent and unrecoverable state.
       
  1464 ** Synchronous is on by default so database corruption is not
       
  1465 ** normally a worry.
       
  1466 */
       
  1467 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
       
  1468   BtShared *pBt = p->pBt;
       
  1469   assert( sqlite3_mutex_held(p->db->mutex) );
       
  1470   sqlite3BtreeEnter(p);
       
  1471   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
       
  1472   sqlite3BtreeLeave(p);
       
  1473   return SQLITE_OK;
       
  1474 }
       
  1475 
       
  1476 /*
       
  1477 ** Change the way data is synced to disk in order to increase or decrease
       
  1478 ** how well the database resists damage due to OS crashes and power
       
  1479 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
       
  1480 ** there is a high probability of damage)  Level 2 is the default.  There
       
  1481 ** is a very low but non-zero probability of damage.  Level 3 reduces the
       
  1482 ** probability of damage to near zero but with a write performance reduction.
       
  1483 */
       
  1484 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
       
  1485 int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
       
  1486   BtShared *pBt = p->pBt;
       
  1487   assert( sqlite3_mutex_held(p->db->mutex) );
       
  1488   sqlite3BtreeEnter(p);
       
  1489   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
       
  1490   sqlite3BtreeLeave(p);
       
  1491   return SQLITE_OK;
       
  1492 }
       
  1493 #endif
       
  1494 
       
  1495 /*
       
  1496 ** Return TRUE if the given btree is set to safety level 1.  In other
       
  1497 ** words, return TRUE if no sync() occurs on the disk files.
       
  1498 */
       
  1499 int sqlite3BtreeSyncDisabled(Btree *p){
       
  1500   BtShared *pBt = p->pBt;
       
  1501   int rc;
       
  1502   assert( sqlite3_mutex_held(p->db->mutex) );  
       
  1503   sqlite3BtreeEnter(p);
       
  1504   assert( pBt && pBt->pPager );
       
  1505   rc = sqlite3PagerNosync(pBt->pPager);
       
  1506   sqlite3BtreeLeave(p);
       
  1507   return rc;
       
  1508 }
       
  1509 
       
  1510 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
       
  1511 /*
       
  1512 ** Change the default pages size and the number of reserved bytes per page.
       
  1513 **
       
  1514 ** The page size must be a power of 2 between 512 and 65536.  If the page
       
  1515 ** size supplied does not meet this constraint then the page size is not
       
  1516 ** changed.
       
  1517 **
       
  1518 ** Page sizes are constrained to be a power of two so that the region
       
  1519 ** of the database file used for locking (beginning at PENDING_BYTE,
       
  1520 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
       
  1521 ** at the beginning of a page.
       
  1522 **
       
  1523 ** If parameter nReserve is less than zero, then the number of reserved
       
  1524 ** bytes per page is left unchanged.
       
  1525 */
       
  1526 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
       
  1527   int rc = SQLITE_OK;
       
  1528   BtShared *pBt = p->pBt;
       
  1529   sqlite3BtreeEnter(p);
       
  1530   if( pBt->pageSizeFixed ){
       
  1531     sqlite3BtreeLeave(p);
       
  1532     return SQLITE_READONLY;
       
  1533   }
       
  1534   if( nReserve<0 ){
       
  1535     nReserve = pBt->pageSize - pBt->usableSize;
       
  1536   }
       
  1537   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
       
  1538         ((pageSize-1)&pageSize)==0 ){
       
  1539     assert( (pageSize & 7)==0 );
       
  1540     assert( !pBt->pPage1 && !pBt->pCursor );
       
  1541     pBt->pageSize = pageSize;
       
  1542     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
       
  1543   }
       
  1544   pBt->usableSize = pBt->pageSize - nReserve;
       
  1545   sqlite3BtreeLeave(p);
       
  1546   return rc;
       
  1547 }
       
  1548 
       
  1549 /*
       
  1550 ** Return the currently defined page size
       
  1551 */
       
  1552 int sqlite3BtreeGetPageSize(Btree *p){
       
  1553   return p->pBt->pageSize;
       
  1554 }
       
  1555 int sqlite3BtreeGetReserve(Btree *p){
       
  1556   int n;
       
  1557   sqlite3BtreeEnter(p);
       
  1558   n = p->pBt->pageSize - p->pBt->usableSize;
       
  1559   sqlite3BtreeLeave(p);
       
  1560   return n;
       
  1561 }
       
  1562 
       
  1563 /*
       
  1564 ** Set the maximum page count for a database if mxPage is positive.
       
  1565 ** No changes are made if mxPage is 0 or negative.
       
  1566 ** Regardless of the value of mxPage, return the maximum page count.
       
  1567 */
       
  1568 int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
       
  1569   int n;
       
  1570   sqlite3BtreeEnter(p);
       
  1571   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
       
  1572   sqlite3BtreeLeave(p);
       
  1573   return n;
       
  1574 }
       
  1575 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
       
  1576 
       
  1577 /*
       
  1578 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
       
  1579 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
       
  1580 ** is disabled. The default value for the auto-vacuum property is 
       
  1581 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
       
  1582 */
       
  1583 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
       
  1584 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  1585   return SQLITE_READONLY;
       
  1586 #else
       
  1587   BtShared *pBt = p->pBt;
       
  1588   int rc = SQLITE_OK;
       
  1589   int av = (autoVacuum?1:0);
       
  1590 
       
  1591   sqlite3BtreeEnter(p);
       
  1592   if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
       
  1593     rc = SQLITE_READONLY;
       
  1594   }else{
       
  1595     pBt->autoVacuum = av;
       
  1596   }
       
  1597   sqlite3BtreeLeave(p);
       
  1598   return rc;
       
  1599 #endif
       
  1600 }
       
  1601 
       
  1602 /*
       
  1603 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
       
  1604 ** enabled 1 is returned. Otherwise 0.
       
  1605 */
       
  1606 int sqlite3BtreeGetAutoVacuum(Btree *p){
       
  1607 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  1608   return BTREE_AUTOVACUUM_NONE;
       
  1609 #else
       
  1610   int rc;
       
  1611   sqlite3BtreeEnter(p);
       
  1612   rc = (
       
  1613     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
       
  1614     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
       
  1615     BTREE_AUTOVACUUM_INCR
       
  1616   );
       
  1617   sqlite3BtreeLeave(p);
       
  1618   return rc;
       
  1619 #endif
       
  1620 }
       
  1621 
       
  1622 
       
  1623 /*
       
  1624 ** Get a reference to pPage1 of the database file.  This will
       
  1625 ** also acquire a readlock on that file.
       
  1626 **
       
  1627 ** SQLITE_OK is returned on success.  If the file is not a
       
  1628 ** well-formed database file, then SQLITE_CORRUPT is returned.
       
  1629 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
       
  1630 ** is returned if we run out of memory. 
       
  1631 */
       
  1632 static int lockBtree(BtShared *pBt){
       
  1633   int rc, pageSize;
       
  1634   MemPage *pPage1;
       
  1635 
       
  1636   assert( sqlite3_mutex_held(pBt->mutex) );
       
  1637   if( pBt->pPage1 ) return SQLITE_OK;
       
  1638   rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
       
  1639   if( rc!=SQLITE_OK ) return rc;
       
  1640   
       
  1641 
       
  1642   /* Do some checking to help insure the file we opened really is
       
  1643   ** a valid database file. 
       
  1644   */
       
  1645   rc = SQLITE_NOTADB;
       
  1646   if( sqlite3PagerPagecount(pBt->pPager)>0 ){
       
  1647     u8 *page1 = pPage1->aData;
       
  1648     if( memcmp(page1, zMagicHeader, 16)!=0 ){
       
  1649       goto page1_init_failed;
       
  1650     }
       
  1651     if( page1[18]>1 ){
       
  1652       pBt->readOnly = 1;
       
  1653     }
       
  1654     if( page1[19]>1 ){
       
  1655       goto page1_init_failed;
       
  1656     }
       
  1657     pageSize = get2byte(&page1[16]);
       
  1658     if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
       
  1659         (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
       
  1660     ){
       
  1661       goto page1_init_failed;
       
  1662     }
       
  1663     assert( (pageSize & 7)==0 );
       
  1664     pBt->pageSize = pageSize;
       
  1665     pBt->usableSize = pageSize - page1[20];
       
  1666     if( pBt->usableSize<500 ){
       
  1667       goto page1_init_failed;
       
  1668     }
       
  1669     pBt->maxEmbedFrac = page1[21];
       
  1670     pBt->minEmbedFrac = page1[22];
       
  1671     pBt->minLeafFrac = page1[23];
       
  1672 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  1673     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
       
  1674     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
       
  1675 #endif
       
  1676   }
       
  1677 
       
  1678   /* maxLocal is the maximum amount of payload to store locally for
       
  1679   ** a cell.  Make sure it is small enough so that at least minFanout
       
  1680   ** cells can will fit on one page.  We assume a 10-byte page header.
       
  1681   ** Besides the payload, the cell must store:
       
  1682   **     2-byte pointer to the cell
       
  1683   **     4-byte child pointer
       
  1684   **     9-byte nKey value
       
  1685   **     4-byte nData value
       
  1686   **     4-byte overflow page pointer
       
  1687   ** So a cell consists of a 2-byte poiner, a header which is as much as
       
  1688   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
       
  1689   ** page pointer.
       
  1690   */
       
  1691   pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
       
  1692   pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
       
  1693   pBt->maxLeaf = pBt->usableSize - 35;
       
  1694   pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
       
  1695   if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
       
  1696     goto page1_init_failed;
       
  1697   }
       
  1698   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
       
  1699   pBt->pPage1 = pPage1;
       
  1700   return SQLITE_OK;
       
  1701 
       
  1702 page1_init_failed:
       
  1703   releasePage(pPage1);
       
  1704   pBt->pPage1 = 0;
       
  1705   return rc;
       
  1706 }
       
  1707 
       
  1708 /*
       
  1709 ** This routine works like lockBtree() except that it also invokes the
       
  1710 ** busy callback if there is lock contention.
       
  1711 */
       
  1712 static int lockBtreeWithRetry(Btree *pRef){
       
  1713   int rc = SQLITE_OK;
       
  1714 
       
  1715   assert( sqlite3BtreeHoldsMutex(pRef) );
       
  1716   if( pRef->inTrans==TRANS_NONE ){
       
  1717     u8 inTransaction = pRef->pBt->inTransaction;
       
  1718     btreeIntegrity(pRef);
       
  1719     rc = sqlite3BtreeBeginTrans(pRef, 0);
       
  1720     pRef->pBt->inTransaction = inTransaction;
       
  1721     pRef->inTrans = TRANS_NONE;
       
  1722     if( rc==SQLITE_OK ){
       
  1723       pRef->pBt->nTransaction--;
       
  1724     }
       
  1725     btreeIntegrity(pRef);
       
  1726   }
       
  1727   return rc;
       
  1728 }
       
  1729        
       
  1730 
       
  1731 /*
       
  1732 ** If there are no outstanding cursors and we are not in the middle
       
  1733 ** of a transaction but there is a read lock on the database, then
       
  1734 ** this routine unrefs the first page of the database file which 
       
  1735 ** has the effect of releasing the read lock.
       
  1736 **
       
  1737 ** If there are any outstanding cursors, this routine is a no-op.
       
  1738 **
       
  1739 ** If there is a transaction in progress, this routine is a no-op.
       
  1740 */
       
  1741 static void unlockBtreeIfUnused(BtShared *pBt){
       
  1742   assert( sqlite3_mutex_held(pBt->mutex) );
       
  1743   if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
       
  1744     if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
       
  1745       if( pBt->pPage1->aData==0 ){
       
  1746         MemPage *pPage = pBt->pPage1;
       
  1747         pPage->aData = (u8*)sqlite3PagerGetData(pPage->pDbPage);
       
  1748         pPage->pBt = pBt;
       
  1749         pPage->pgno = 1;
       
  1750       }
       
  1751       releasePage(pBt->pPage1);
       
  1752     }
       
  1753     pBt->pPage1 = 0;
       
  1754     pBt->inStmt = 0;
       
  1755   }
       
  1756 }
       
  1757 
       
  1758 /*
       
  1759 ** Create a new database by initializing the first page of the
       
  1760 ** file.
       
  1761 */
       
  1762 static int newDatabase(BtShared *pBt){
       
  1763   MemPage *pP1;
       
  1764   unsigned char *data;
       
  1765   int rc;
       
  1766 
       
  1767   assert( sqlite3_mutex_held(pBt->mutex) );
       
  1768   if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
       
  1769   pP1 = pBt->pPage1;
       
  1770   assert( pP1!=0 );
       
  1771   data = pP1->aData;
       
  1772   rc = sqlite3PagerWrite(pP1->pDbPage);
       
  1773   if( rc ) return rc;
       
  1774   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
       
  1775   assert( sizeof(zMagicHeader)==16 );
       
  1776   put2byte(&data[16], pBt->pageSize);
       
  1777   data[18] = 1;
       
  1778   data[19] = 1;
       
  1779   data[20] = pBt->pageSize - pBt->usableSize;
       
  1780   data[21] = pBt->maxEmbedFrac;
       
  1781   data[22] = pBt->minEmbedFrac;
       
  1782   data[23] = pBt->minLeafFrac;
       
  1783   memset(&data[24], 0, 100-24);
       
  1784   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
       
  1785   pBt->pageSizeFixed = 1;
       
  1786 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  1787   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
       
  1788   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
       
  1789   put4byte(&data[36 + 4*4], pBt->autoVacuum);
       
  1790   put4byte(&data[36 + 7*4], pBt->incrVacuum);
       
  1791 #endif
       
  1792   return SQLITE_OK;
       
  1793 }
       
  1794 
       
  1795 /*
       
  1796 ** Attempt to start a new transaction. A write-transaction
       
  1797 ** is started if the second argument is nonzero, otherwise a read-
       
  1798 ** transaction.  If the second argument is 2 or more and exclusive
       
  1799 ** transaction is started, meaning that no other process is allowed
       
  1800 ** to access the database.  A preexisting transaction may not be
       
  1801 ** upgraded to exclusive by calling this routine a second time - the
       
  1802 ** exclusivity flag only works for a new transaction.
       
  1803 **
       
  1804 ** A write-transaction must be started before attempting any 
       
  1805 ** changes to the database.  None of the following routines 
       
  1806 ** will work unless a transaction is started first:
       
  1807 **
       
  1808 **      sqlite3BtreeCreateTable()
       
  1809 **      sqlite3BtreeCreateIndex()
       
  1810 **      sqlite3BtreeClearTable()
       
  1811 **      sqlite3BtreeDropTable()
       
  1812 **      sqlite3BtreeInsert()
       
  1813 **      sqlite3BtreeDelete()
       
  1814 **      sqlite3BtreeUpdateMeta()
       
  1815 **
       
  1816 ** If an initial attempt to acquire the lock fails because of lock contention
       
  1817 ** and the database was previously unlocked, then invoke the busy handler
       
  1818 ** if there is one.  But if there was previously a read-lock, do not
       
  1819 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
       
  1820 ** returned when there is already a read-lock in order to avoid a deadlock.
       
  1821 **
       
  1822 ** Suppose there are two processes A and B.  A has a read lock and B has
       
  1823 ** a reserved lock.  B tries to promote to exclusive but is blocked because
       
  1824 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
       
  1825 ** One or the other of the two processes must give way or there can be
       
  1826 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
       
  1827 ** when A already has a read lock, we encourage A to give up and let B
       
  1828 ** proceed.
       
  1829 */
       
  1830 int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
       
  1831   BtShared *pBt = p->pBt;
       
  1832   int rc = SQLITE_OK;
       
  1833 
       
  1834   sqlite3BtreeEnter(p);
       
  1835   pBt->db = p->db;
       
  1836   btreeIntegrity(p);
       
  1837 
       
  1838   /* If the btree is already in a write-transaction, or it
       
  1839   ** is already in a read-transaction and a read-transaction
       
  1840   ** is requested, this is a no-op.
       
  1841   */
       
  1842   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
       
  1843     goto trans_begun;
       
  1844   }
       
  1845 
       
  1846   /* Write transactions are not possible on a read-only database */
       
  1847   if( pBt->readOnly && wrflag ){
       
  1848     rc = SQLITE_READONLY;
       
  1849     goto trans_begun;
       
  1850   }
       
  1851 
       
  1852   /* If another database handle has already opened a write transaction 
       
  1853   ** on this shared-btree structure and a second write transaction is
       
  1854   ** requested, return SQLITE_BUSY.
       
  1855   */
       
  1856   if( pBt->inTransaction==TRANS_WRITE && wrflag ){
       
  1857     rc = SQLITE_BUSY;
       
  1858     goto trans_begun;
       
  1859   }
       
  1860 
       
  1861   do {
       
  1862     if( pBt->pPage1==0 ){
       
  1863       rc = lockBtree(pBt);
       
  1864     }
       
  1865 
       
  1866     if( rc==SQLITE_OK && wrflag ){
       
  1867       if( pBt->readOnly ){
       
  1868         rc = SQLITE_READONLY;
       
  1869       }else{
       
  1870         rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
       
  1871         if( rc==SQLITE_OK ){
       
  1872           rc = newDatabase(pBt);
       
  1873         }
       
  1874       }
       
  1875     }
       
  1876   
       
  1877     if( rc==SQLITE_OK ){
       
  1878       if( wrflag ) pBt->inStmt = 0;
       
  1879     }else{
       
  1880       unlockBtreeIfUnused(pBt);
       
  1881     }
       
  1882   }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
       
  1883           sqlite3BtreeInvokeBusyHandler(pBt, 0) );
       
  1884 
       
  1885   if( rc==SQLITE_OK ){
       
  1886     if( p->inTrans==TRANS_NONE ){
       
  1887       pBt->nTransaction++;
       
  1888     }
       
  1889     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
       
  1890     if( p->inTrans>pBt->inTransaction ){
       
  1891       pBt->inTransaction = p->inTrans;
       
  1892     }
       
  1893   }
       
  1894 
       
  1895 
       
  1896 trans_begun:
       
  1897   btreeIntegrity(p);
       
  1898   sqlite3BtreeLeave(p);
       
  1899   return rc;
       
  1900 }
       
  1901 
       
  1902 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  1903 
       
  1904 /*
       
  1905 ** Set the pointer-map entries for all children of page pPage. Also, if
       
  1906 ** pPage contains cells that point to overflow pages, set the pointer
       
  1907 ** map entries for the overflow pages as well.
       
  1908 */
       
  1909 static int setChildPtrmaps(MemPage *pPage){
       
  1910   int i;                             /* Counter variable */
       
  1911   int nCell;                         /* Number of cells in page pPage */
       
  1912   int rc;                            /* Return code */
       
  1913   BtShared *pBt = pPage->pBt;
       
  1914   int isInitOrig = pPage->isInit;
       
  1915   Pgno pgno = pPage->pgno;
       
  1916 
       
  1917   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  1918   rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
       
  1919   if( rc!=SQLITE_OK ){
       
  1920     goto set_child_ptrmaps_out;
       
  1921   }
       
  1922   nCell = pPage->nCell;
       
  1923 
       
  1924   for(i=0; i<nCell; i++){
       
  1925     u8 *pCell = findCell(pPage, i);
       
  1926 
       
  1927     rc = ptrmapPutOvflPtr(pPage, pCell);
       
  1928     if( rc!=SQLITE_OK ){
       
  1929       goto set_child_ptrmaps_out;
       
  1930     }
       
  1931 
       
  1932     if( !pPage->leaf ){
       
  1933       Pgno childPgno = get4byte(pCell);
       
  1934       rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
       
  1935       if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
       
  1936     }
       
  1937   }
       
  1938 
       
  1939   if( !pPage->leaf ){
       
  1940     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
  1941     rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
       
  1942   }
       
  1943 
       
  1944 set_child_ptrmaps_out:
       
  1945   pPage->isInit = isInitOrig;
       
  1946   return rc;
       
  1947 }
       
  1948 
       
  1949 /*
       
  1950 ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
       
  1951 ** page, is a pointer to page iFrom. Modify this pointer so that it points to
       
  1952 ** iTo. Parameter eType describes the type of pointer to be modified, as 
       
  1953 ** follows:
       
  1954 **
       
  1955 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
       
  1956 **                   page of pPage.
       
  1957 **
       
  1958 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
       
  1959 **                   page pointed to by one of the cells on pPage.
       
  1960 **
       
  1961 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
       
  1962 **                   overflow page in the list.
       
  1963 */
       
  1964 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
       
  1965   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  1966   if( eType==PTRMAP_OVERFLOW2 ){
       
  1967     /* The pointer is always the first 4 bytes of the page in this case.  */
       
  1968     if( get4byte(pPage->aData)!=iFrom ){
       
  1969       return SQLITE_CORRUPT_BKPT;
       
  1970     }
       
  1971     put4byte(pPage->aData, iTo);
       
  1972   }else{
       
  1973     int isInitOrig = pPage->isInit;
       
  1974     int i;
       
  1975     int nCell;
       
  1976 
       
  1977     sqlite3BtreeInitPage(pPage, 0);
       
  1978     nCell = pPage->nCell;
       
  1979 
       
  1980     for(i=0; i<nCell; i++){
       
  1981       u8 *pCell = findCell(pPage, i);
       
  1982       if( eType==PTRMAP_OVERFLOW1 ){
       
  1983         CellInfo info;
       
  1984         sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  1985         if( info.iOverflow ){
       
  1986           if( iFrom==get4byte(&pCell[info.iOverflow]) ){
       
  1987             put4byte(&pCell[info.iOverflow], iTo);
       
  1988             break;
       
  1989           }
       
  1990         }
       
  1991       }else{
       
  1992         if( get4byte(pCell)==iFrom ){
       
  1993           put4byte(pCell, iTo);
       
  1994           break;
       
  1995         }
       
  1996       }
       
  1997     }
       
  1998   
       
  1999     if( i==nCell ){
       
  2000       if( eType!=PTRMAP_BTREE || 
       
  2001           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
       
  2002         return SQLITE_CORRUPT_BKPT;
       
  2003       }
       
  2004       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
       
  2005     }
       
  2006 
       
  2007     pPage->isInit = isInitOrig;
       
  2008   }
       
  2009   return SQLITE_OK;
       
  2010 }
       
  2011 
       
  2012 
       
  2013 /*
       
  2014 ** Move the open database page pDbPage to location iFreePage in the 
       
  2015 ** database. The pDbPage reference remains valid.
       
  2016 */
       
  2017 static int relocatePage(
       
  2018   BtShared *pBt,           /* Btree */
       
  2019   MemPage *pDbPage,        /* Open page to move */
       
  2020   u8 eType,                /* Pointer map 'type' entry for pDbPage */
       
  2021   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
       
  2022   Pgno iFreePage           /* The location to move pDbPage to */
       
  2023 ){
       
  2024   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
       
  2025   Pgno iDbPage = pDbPage->pgno;
       
  2026   Pager *pPager = pBt->pPager;
       
  2027   int rc;
       
  2028 
       
  2029   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
       
  2030       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
       
  2031   assert( sqlite3_mutex_held(pBt->mutex) );
       
  2032   assert( pDbPage->pBt==pBt );
       
  2033 
       
  2034   /* Move page iDbPage from its current location to page number iFreePage */
       
  2035   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
       
  2036       iDbPage, iFreePage, iPtrPage, eType));
       
  2037   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
       
  2038   if( rc!=SQLITE_OK ){
       
  2039     return rc;
       
  2040   }
       
  2041   pDbPage->pgno = iFreePage;
       
  2042 
       
  2043   /* If pDbPage was a btree-page, then it may have child pages and/or cells
       
  2044   ** that point to overflow pages. The pointer map entries for all these
       
  2045   ** pages need to be changed.
       
  2046   **
       
  2047   ** If pDbPage is an overflow page, then the first 4 bytes may store a
       
  2048   ** pointer to a subsequent overflow page. If this is the case, then
       
  2049   ** the pointer map needs to be updated for the subsequent overflow page.
       
  2050   */
       
  2051   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
       
  2052     rc = setChildPtrmaps(pDbPage);
       
  2053     if( rc!=SQLITE_OK ){
       
  2054       return rc;
       
  2055     }
       
  2056   }else{
       
  2057     Pgno nextOvfl = get4byte(pDbPage->aData);
       
  2058     if( nextOvfl!=0 ){
       
  2059       rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
       
  2060       if( rc!=SQLITE_OK ){
       
  2061         return rc;
       
  2062       }
       
  2063     }
       
  2064   }
       
  2065 
       
  2066   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
       
  2067   ** that it points at iFreePage. Also fix the pointer map entry for
       
  2068   ** iPtrPage.
       
  2069   */
       
  2070   if( eType!=PTRMAP_ROOTPAGE ){
       
  2071     rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
       
  2072     if( rc!=SQLITE_OK ){
       
  2073       return rc;
       
  2074     }
       
  2075     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
       
  2076     if( rc!=SQLITE_OK ){
       
  2077       releasePage(pPtrPage);
       
  2078       return rc;
       
  2079     }
       
  2080     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
       
  2081     releasePage(pPtrPage);
       
  2082     if( rc==SQLITE_OK ){
       
  2083       rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
       
  2084     }
       
  2085   }
       
  2086   return rc;
       
  2087 }
       
  2088 
       
  2089 /* Forward declaration required by incrVacuumStep(). */
       
  2090 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
       
  2091 
       
  2092 /*
       
  2093 ** Perform a single step of an incremental-vacuum. If successful,
       
  2094 ** return SQLITE_OK. If there is no work to do (and therefore no
       
  2095 ** point in calling this function again), return SQLITE_DONE.
       
  2096 **
       
  2097 ** More specificly, this function attempts to re-organize the 
       
  2098 ** database so that the last page of the file currently in use
       
  2099 ** is no longer in use.
       
  2100 **
       
  2101 ** If the nFin parameter is non-zero, the implementation assumes
       
  2102 ** that the caller will keep calling incrVacuumStep() until
       
  2103 ** it returns SQLITE_DONE or an error, and that nFin is the
       
  2104 ** number of pages the database file will contain after this 
       
  2105 ** process is complete.
       
  2106 */
       
  2107 static int incrVacuumStep(BtShared *pBt, Pgno nFin){
       
  2108   Pgno iLastPg;             /* Last page in the database */
       
  2109   Pgno nFreeList;           /* Number of pages still on the free-list */
       
  2110 
       
  2111   assert( sqlite3_mutex_held(pBt->mutex) );
       
  2112   iLastPg = pBt->nTrunc;
       
  2113   if( iLastPg==0 ){
       
  2114     iLastPg = sqlite3PagerPagecount(pBt->pPager);
       
  2115   }
       
  2116 
       
  2117   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
       
  2118     int rc;
       
  2119     u8 eType;
       
  2120     Pgno iPtrPage;
       
  2121 
       
  2122     nFreeList = get4byte(&pBt->pPage1->aData[36]);
       
  2123     if( nFreeList==0 || nFin==iLastPg ){
       
  2124       return SQLITE_DONE;
       
  2125     }
       
  2126 
       
  2127     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
       
  2128     if( rc!=SQLITE_OK ){
       
  2129       return rc;
       
  2130     }
       
  2131     if( eType==PTRMAP_ROOTPAGE ){
       
  2132       return SQLITE_CORRUPT_BKPT;
       
  2133     }
       
  2134 
       
  2135     if( eType==PTRMAP_FREEPAGE ){
       
  2136       if( nFin==0 ){
       
  2137         /* Remove the page from the files free-list. This is not required
       
  2138         ** if nFin is non-zero. In that case, the free-list will be
       
  2139         ** truncated to zero after this function returns, so it doesn't 
       
  2140         ** matter if it still contains some garbage entries.
       
  2141         */
       
  2142         Pgno iFreePg;
       
  2143         MemPage *pFreePg;
       
  2144         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
       
  2145         if( rc!=SQLITE_OK ){
       
  2146           return rc;
       
  2147         }
       
  2148         assert( iFreePg==iLastPg );
       
  2149         releasePage(pFreePg);
       
  2150       }
       
  2151     } else {
       
  2152       Pgno iFreePg;             /* Index of free page to move pLastPg to */
       
  2153       MemPage *pLastPg;
       
  2154 
       
  2155       rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
       
  2156       if( rc!=SQLITE_OK ){
       
  2157         return rc;
       
  2158       }
       
  2159 
       
  2160       /* If nFin is zero, this loop runs exactly once and page pLastPg
       
  2161       ** is swapped with the first free page pulled off the free list.
       
  2162       **
       
  2163       ** On the other hand, if nFin is greater than zero, then keep
       
  2164       ** looping until a free-page located within the first nFin pages
       
  2165       ** of the file is found.
       
  2166       */
       
  2167       do {
       
  2168         MemPage *pFreePg;
       
  2169         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
       
  2170         if( rc!=SQLITE_OK ){
       
  2171           releasePage(pLastPg);
       
  2172           return rc;
       
  2173         }
       
  2174         releasePage(pFreePg);
       
  2175       }while( nFin!=0 && iFreePg>nFin );
       
  2176       assert( iFreePg<iLastPg );
       
  2177       
       
  2178       rc = sqlite3PagerWrite(pLastPg->pDbPage);
       
  2179       if( rc==SQLITE_OK ){
       
  2180         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg);
       
  2181       }
       
  2182       releasePage(pLastPg);
       
  2183       if( rc!=SQLITE_OK ){
       
  2184         return rc;
       
  2185       }
       
  2186     }
       
  2187   }
       
  2188 
       
  2189   pBt->nTrunc = iLastPg - 1;
       
  2190   while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
       
  2191     pBt->nTrunc--;
       
  2192   }
       
  2193   return SQLITE_OK;
       
  2194 }
       
  2195 
       
  2196 /*
       
  2197 ** A write-transaction must be opened before calling this function.
       
  2198 ** It performs a single unit of work towards an incremental vacuum.
       
  2199 **
       
  2200 ** If the incremental vacuum is finished after this function has run,
       
  2201 ** SQLITE_DONE is returned. If it is not finished, but no error occured,
       
  2202 ** SQLITE_OK is returned. Otherwise an SQLite error code. 
       
  2203 */
       
  2204 int sqlite3BtreeIncrVacuum(Btree *p){
       
  2205   int rc;
       
  2206   BtShared *pBt = p->pBt;
       
  2207 
       
  2208   sqlite3BtreeEnter(p);
       
  2209   pBt->db = p->db;
       
  2210   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
       
  2211   if( !pBt->autoVacuum ){
       
  2212     rc = SQLITE_DONE;
       
  2213   }else{
       
  2214     invalidateAllOverflowCache(pBt);
       
  2215     rc = incrVacuumStep(pBt, 0);
       
  2216   }
       
  2217   sqlite3BtreeLeave(p);
       
  2218   return rc;
       
  2219 }
       
  2220 
       
  2221 /*
       
  2222 ** This routine is called prior to sqlite3PagerCommit when a transaction
       
  2223 ** is commited for an auto-vacuum database.
       
  2224 **
       
  2225 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
       
  2226 ** the database file should be truncated to during the commit process. 
       
  2227 ** i.e. the database has been reorganized so that only the first *pnTrunc
       
  2228 ** pages are in use.
       
  2229 */
       
  2230 static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
       
  2231   int rc = SQLITE_OK;
       
  2232   Pager *pPager = pBt->pPager;
       
  2233 #ifndef NDEBUG
       
  2234   int nRef = sqlite3PagerRefcount(pPager);
       
  2235 #endif
       
  2236 
       
  2237   assert( sqlite3_mutex_held(pBt->mutex) );
       
  2238   invalidateAllOverflowCache(pBt);
       
  2239   assert(pBt->autoVacuum);
       
  2240   if( !pBt->incrVacuum ){
       
  2241     Pgno nFin = 0;
       
  2242 
       
  2243     if( pBt->nTrunc==0 ){
       
  2244       Pgno nFree;
       
  2245       Pgno nPtrmap;
       
  2246       const int pgsz = pBt->pageSize;
       
  2247       Pgno nOrig = sqlite3PagerPagecount(pBt->pPager);
       
  2248 
       
  2249       if( PTRMAP_ISPAGE(pBt, nOrig) ){
       
  2250         return SQLITE_CORRUPT_BKPT;
       
  2251       }
       
  2252       if( nOrig==PENDING_BYTE_PAGE(pBt) ){
       
  2253         nOrig--;
       
  2254       }
       
  2255       nFree = get4byte(&pBt->pPage1->aData[36]);
       
  2256       nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
       
  2257       nFin = nOrig - nFree - nPtrmap;
       
  2258       if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
       
  2259         nFin--;
       
  2260       }
       
  2261       while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
       
  2262         nFin--;
       
  2263       }
       
  2264     }
       
  2265 
       
  2266     while( rc==SQLITE_OK ){
       
  2267       rc = incrVacuumStep(pBt, nFin);
       
  2268     }
       
  2269     if( rc==SQLITE_DONE ){
       
  2270       assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
       
  2271       rc = SQLITE_OK;
       
  2272       if( pBt->nTrunc ){
       
  2273         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
       
  2274         put4byte(&pBt->pPage1->aData[32], 0);
       
  2275         put4byte(&pBt->pPage1->aData[36], 0);
       
  2276         pBt->nTrunc = nFin;
       
  2277       }
       
  2278     }
       
  2279     if( rc!=SQLITE_OK ){
       
  2280       sqlite3PagerRollback(pPager);
       
  2281     }
       
  2282   }
       
  2283 
       
  2284   if( rc==SQLITE_OK ){
       
  2285     *pnTrunc = pBt->nTrunc;
       
  2286     pBt->nTrunc = 0;
       
  2287   }
       
  2288   assert( nRef==sqlite3PagerRefcount(pPager) );
       
  2289   return rc;
       
  2290 }
       
  2291 
       
  2292 #endif
       
  2293 
       
  2294 /*
       
  2295 ** This routine does the first phase of a two-phase commit.  This routine
       
  2296 ** causes a rollback journal to be created (if it does not already exist)
       
  2297 ** and populated with enough information so that if a power loss occurs
       
  2298 ** the database can be restored to its original state by playing back
       
  2299 ** the journal.  Then the contents of the journal are flushed out to
       
  2300 ** the disk.  After the journal is safely on oxide, the changes to the
       
  2301 ** database are written into the database file and flushed to oxide.
       
  2302 ** At the end of this call, the rollback journal still exists on the
       
  2303 ** disk and we are still holding all locks, so the transaction has not
       
  2304 ** committed.  See sqlite3BtreeCommit() for the second phase of the
       
  2305 ** commit process.
       
  2306 **
       
  2307 ** This call is a no-op if no write-transaction is currently active on pBt.
       
  2308 **
       
  2309 ** Otherwise, sync the database file for the btree pBt. zMaster points to
       
  2310 ** the name of a master journal file that should be written into the
       
  2311 ** individual journal file, or is NULL, indicating no master journal file 
       
  2312 ** (single database transaction).
       
  2313 **
       
  2314 ** When this is called, the master journal should already have been
       
  2315 ** created, populated with this journal pointer and synced to disk.
       
  2316 **
       
  2317 ** Once this is routine has returned, the only thing required to commit
       
  2318 ** the write-transaction for this database file is to delete the journal.
       
  2319 */
       
  2320 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
       
  2321   int rc = SQLITE_OK;
       
  2322   if( p->inTrans==TRANS_WRITE ){
       
  2323     BtShared *pBt = p->pBt;
       
  2324     Pgno nTrunc = 0;
       
  2325     sqlite3BtreeEnter(p);
       
  2326     pBt->db = p->db;
       
  2327 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  2328     if( pBt->autoVacuum ){
       
  2329       rc = autoVacuumCommit(pBt, &nTrunc); 
       
  2330       if( rc!=SQLITE_OK ){
       
  2331         sqlite3BtreeLeave(p);
       
  2332         return rc;
       
  2333       }
       
  2334     }
       
  2335 #endif
       
  2336     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc);
       
  2337     sqlite3BtreeLeave(p);
       
  2338   }
       
  2339   return rc;
       
  2340 }
       
  2341 
       
  2342 /*
       
  2343 ** Commit the transaction currently in progress.
       
  2344 **
       
  2345 ** This routine implements the second phase of a 2-phase commit.  The
       
  2346 ** sqlite3BtreeSync() routine does the first phase and should be invoked
       
  2347 ** prior to calling this routine.  The sqlite3BtreeSync() routine did
       
  2348 ** all the work of writing information out to disk and flushing the
       
  2349 ** contents so that they are written onto the disk platter.  All this
       
  2350 ** routine has to do is delete or truncate the rollback journal
       
  2351 ** (which causes the transaction to commit) and drop locks.
       
  2352 **
       
  2353 ** This will release the write lock on the database file.  If there
       
  2354 ** are no active cursors, it also releases the read lock.
       
  2355 */
       
  2356 int sqlite3BtreeCommitPhaseTwo(Btree *p){
       
  2357   BtShared *pBt = p->pBt;
       
  2358 
       
  2359   sqlite3BtreeEnter(p);
       
  2360   pBt->db = p->db;
       
  2361   btreeIntegrity(p);
       
  2362 
       
  2363   /* If the handle has a write-transaction open, commit the shared-btrees 
       
  2364   ** transaction and set the shared state to TRANS_READ.
       
  2365   */
       
  2366   if( p->inTrans==TRANS_WRITE ){
       
  2367     int rc;
       
  2368     assert( pBt->inTransaction==TRANS_WRITE );
       
  2369     assert( pBt->nTransaction>0 );
       
  2370     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
       
  2371     if( rc!=SQLITE_OK ){
       
  2372       sqlite3BtreeLeave(p);
       
  2373       return rc;
       
  2374     }
       
  2375     pBt->inTransaction = TRANS_READ;
       
  2376     pBt->inStmt = 0;
       
  2377   }
       
  2378   unlockAllTables(p);
       
  2379 
       
  2380   /* If the handle has any kind of transaction open, decrement the transaction
       
  2381   ** count of the shared btree. If the transaction count reaches 0, set
       
  2382   ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
       
  2383   ** will unlock the pager.
       
  2384   */
       
  2385   if( p->inTrans!=TRANS_NONE ){
       
  2386     pBt->nTransaction--;
       
  2387     if( 0==pBt->nTransaction ){
       
  2388       pBt->inTransaction = TRANS_NONE;
       
  2389     }
       
  2390   }
       
  2391 
       
  2392   /* Set the handles current transaction state to TRANS_NONE and unlock
       
  2393   ** the pager if this call closed the only read or write transaction.
       
  2394   */
       
  2395   p->inTrans = TRANS_NONE;
       
  2396   unlockBtreeIfUnused(pBt);
       
  2397 
       
  2398   btreeIntegrity(p);
       
  2399   sqlite3BtreeLeave(p);
       
  2400   return SQLITE_OK;
       
  2401 }
       
  2402 
       
  2403 /*
       
  2404 ** Do both phases of a commit.
       
  2405 */
       
  2406 int sqlite3BtreeCommit(Btree *p){
       
  2407   int rc;
       
  2408   sqlite3BtreeEnter(p);
       
  2409   rc = sqlite3BtreeCommitPhaseOne(p, 0);
       
  2410   if( rc==SQLITE_OK ){
       
  2411     rc = sqlite3BtreeCommitPhaseTwo(p);
       
  2412   }
       
  2413   sqlite3BtreeLeave(p);
       
  2414   return rc;
       
  2415 }
       
  2416 
       
  2417 #ifndef NDEBUG
       
  2418 /*
       
  2419 ** Return the number of write-cursors open on this handle. This is for use
       
  2420 ** in assert() expressions, so it is only compiled if NDEBUG is not
       
  2421 ** defined.
       
  2422 **
       
  2423 ** For the purposes of this routine, a write-cursor is any cursor that
       
  2424 ** is capable of writing to the databse.  That means the cursor was
       
  2425 ** originally opened for writing and the cursor has not be disabled
       
  2426 ** by having its state changed to CURSOR_FAULT.
       
  2427 */
       
  2428 static int countWriteCursors(BtShared *pBt){
       
  2429   BtCursor *pCur;
       
  2430   int r = 0;
       
  2431   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
       
  2432     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
       
  2433   }
       
  2434   return r;
       
  2435 }
       
  2436 #endif
       
  2437 
       
  2438 /*
       
  2439 ** This routine sets the state to CURSOR_FAULT and the error
       
  2440 ** code to errCode for every cursor on BtShared that pBtree
       
  2441 ** references.
       
  2442 **
       
  2443 ** Every cursor is tripped, including cursors that belong
       
  2444 ** to other database connections that happen to be sharing
       
  2445 ** the cache with pBtree.
       
  2446 **
       
  2447 ** This routine gets called when a rollback occurs.
       
  2448 ** All cursors using the same cache must be tripped
       
  2449 ** to prevent them from trying to use the btree after
       
  2450 ** the rollback.  The rollback may have deleted tables
       
  2451 ** or moved root pages, so it is not sufficient to
       
  2452 ** save the state of the cursor.  The cursor must be
       
  2453 ** invalidated.
       
  2454 */
       
  2455 void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
       
  2456   BtCursor *p;
       
  2457   sqlite3BtreeEnter(pBtree);
       
  2458   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
       
  2459     clearCursorPosition(p);
       
  2460     p->eState = CURSOR_FAULT;
       
  2461     p->skip = errCode;
       
  2462   }
       
  2463   sqlite3BtreeLeave(pBtree);
       
  2464 }
       
  2465 
       
  2466 /*
       
  2467 ** Rollback the transaction in progress.  All cursors will be
       
  2468 ** invalided by this operation.  Any attempt to use a cursor
       
  2469 ** that was open at the beginning of this operation will result
       
  2470 ** in an error.
       
  2471 **
       
  2472 ** This will release the write lock on the database file.  If there
       
  2473 ** are no active cursors, it also releases the read lock.
       
  2474 */
       
  2475 int sqlite3BtreeRollback(Btree *p){
       
  2476   int rc;
       
  2477   BtShared *pBt = p->pBt;
       
  2478   MemPage *pPage1;
       
  2479 
       
  2480   sqlite3BtreeEnter(p);
       
  2481   pBt->db = p->db;
       
  2482   rc = saveAllCursors(pBt, 0, 0);
       
  2483 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  2484   if( rc!=SQLITE_OK ){
       
  2485     /* This is a horrible situation. An IO or malloc() error occured whilst
       
  2486     ** trying to save cursor positions. If this is an automatic rollback (as
       
  2487     ** the result of a constraint, malloc() failure or IO error) then 
       
  2488     ** the cache may be internally inconsistent (not contain valid trees) so
       
  2489     ** we cannot simply return the error to the caller. Instead, abort 
       
  2490     ** all queries that may be using any of the cursors that failed to save.
       
  2491     */
       
  2492     sqlite3BtreeTripAllCursors(p, rc);
       
  2493   }
       
  2494 #endif
       
  2495   btreeIntegrity(p);
       
  2496   unlockAllTables(p);
       
  2497 
       
  2498   if( p->inTrans==TRANS_WRITE ){
       
  2499     int rc2;
       
  2500 
       
  2501 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  2502     pBt->nTrunc = 0;
       
  2503 #endif
       
  2504 
       
  2505     assert( TRANS_WRITE==pBt->inTransaction );
       
  2506     rc2 = sqlite3PagerRollback(pBt->pPager);
       
  2507     if( rc2!=SQLITE_OK ){
       
  2508       rc = rc2;
       
  2509     }
       
  2510 
       
  2511     /* The rollback may have destroyed the pPage1->aData value.  So
       
  2512     ** call sqlite3BtreeGetPage() on page 1 again to make
       
  2513     ** sure pPage1->aData is set correctly. */
       
  2514     if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
       
  2515       releasePage(pPage1);
       
  2516     }
       
  2517     assert( countWriteCursors(pBt)==0 );
       
  2518     pBt->inTransaction = TRANS_READ;
       
  2519   }
       
  2520 
       
  2521   if( p->inTrans!=TRANS_NONE ){
       
  2522     assert( pBt->nTransaction>0 );
       
  2523     pBt->nTransaction--;
       
  2524     if( 0==pBt->nTransaction ){
       
  2525       pBt->inTransaction = TRANS_NONE;
       
  2526     }
       
  2527   }
       
  2528 
       
  2529   p->inTrans = TRANS_NONE;
       
  2530   pBt->inStmt = 0;
       
  2531   unlockBtreeIfUnused(pBt);
       
  2532 
       
  2533   btreeIntegrity(p);
       
  2534   sqlite3BtreeLeave(p);
       
  2535   return rc;
       
  2536 }
       
  2537 
       
  2538 /*
       
  2539 ** Start a statement subtransaction.  The subtransaction can
       
  2540 ** can be rolled back independently of the main transaction.
       
  2541 ** You must start a transaction before starting a subtransaction.
       
  2542 ** The subtransaction is ended automatically if the main transaction
       
  2543 ** commits or rolls back.
       
  2544 **
       
  2545 ** Only one subtransaction may be active at a time.  It is an error to try
       
  2546 ** to start a new subtransaction if another subtransaction is already active.
       
  2547 **
       
  2548 ** Statement subtransactions are used around individual SQL statements
       
  2549 ** that are contained within a BEGIN...COMMIT block.  If a constraint
       
  2550 ** error occurs within the statement, the effect of that one statement
       
  2551 ** can be rolled back without having to rollback the entire transaction.
       
  2552 */
       
  2553 int sqlite3BtreeBeginStmt(Btree *p){
       
  2554   int rc;
       
  2555   BtShared *pBt = p->pBt;
       
  2556   sqlite3BtreeEnter(p);
       
  2557   pBt->db = p->db;
       
  2558   if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
       
  2559     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  2560   }else{
       
  2561     assert( pBt->inTransaction==TRANS_WRITE );
       
  2562     rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
       
  2563     pBt->inStmt = 1;
       
  2564   }
       
  2565   sqlite3BtreeLeave(p);
       
  2566   return rc;
       
  2567 }
       
  2568 
       
  2569 
       
  2570 /*
       
  2571 ** Commit the statment subtransaction currently in progress.  If no
       
  2572 ** subtransaction is active, this is a no-op.
       
  2573 */
       
  2574 int sqlite3BtreeCommitStmt(Btree *p){
       
  2575   int rc;
       
  2576   BtShared *pBt = p->pBt;
       
  2577   sqlite3BtreeEnter(p);
       
  2578   pBt->db = p->db;
       
  2579   if( pBt->inStmt && !pBt->readOnly ){
       
  2580     rc = sqlite3PagerStmtCommit(pBt->pPager);
       
  2581   }else{
       
  2582     rc = SQLITE_OK;
       
  2583   }
       
  2584   pBt->inStmt = 0;
       
  2585   sqlite3BtreeLeave(p);
       
  2586   return rc;
       
  2587 }
       
  2588 
       
  2589 /*
       
  2590 ** Rollback the active statement subtransaction.  If no subtransaction
       
  2591 ** is active this routine is a no-op.
       
  2592 **
       
  2593 ** All cursors will be invalidated by this operation.  Any attempt
       
  2594 ** to use a cursor that was open at the beginning of this operation
       
  2595 ** will result in an error.
       
  2596 */
       
  2597 int sqlite3BtreeRollbackStmt(Btree *p){
       
  2598   int rc = SQLITE_OK;
       
  2599   BtShared *pBt = p->pBt;
       
  2600   sqlite3BtreeEnter(p);
       
  2601   pBt->db = p->db;
       
  2602   if( pBt->inStmt && !pBt->readOnly ){
       
  2603     rc = sqlite3PagerStmtRollback(pBt->pPager);
       
  2604     assert( countWriteCursors(pBt)==0 );
       
  2605     pBt->inStmt = 0;
       
  2606   }
       
  2607   sqlite3BtreeLeave(p);
       
  2608   return rc;
       
  2609 }
       
  2610 
       
  2611 /*
       
  2612 ** Default key comparison function to be used if no comparison function
       
  2613 ** is specified on the sqlite3BtreeCursor() call.
       
  2614 */
       
  2615 static int dfltCompare(
       
  2616   void *NotUsed,             /* User data is not used */
       
  2617   int n1, const void *p1,    /* First key to compare */
       
  2618   int n2, const void *p2     /* Second key to compare */
       
  2619 ){
       
  2620   int c;
       
  2621   c = memcmp(p1, p2, n1<n2 ? n1 : n2);
       
  2622   if( c==0 ){
       
  2623     c = n1 - n2;
       
  2624   }
       
  2625   return c;
       
  2626 }
       
  2627 
       
  2628 /*
       
  2629 ** Create a new cursor for the BTree whose root is on the page
       
  2630 ** iTable.  The act of acquiring a cursor gets a read lock on 
       
  2631 ** the database file.
       
  2632 **
       
  2633 ** If wrFlag==0, then the cursor can only be used for reading.
       
  2634 ** If wrFlag==1, then the cursor can be used for reading or for
       
  2635 ** writing if other conditions for writing are also met.  These
       
  2636 ** are the conditions that must be met in order for writing to
       
  2637 ** be allowed:
       
  2638 **
       
  2639 ** 1:  The cursor must have been opened with wrFlag==1
       
  2640 **
       
  2641 ** 2:  Other database connections that share the same pager cache
       
  2642 **     but which are not in the READ_UNCOMMITTED state may not have
       
  2643 **     cursors open with wrFlag==0 on the same table.  Otherwise
       
  2644 **     the changes made by this write cursor would be visible to
       
  2645 **     the read cursors in the other database connection.
       
  2646 **
       
  2647 ** 3:  The database must be writable (not on read-only media)
       
  2648 **
       
  2649 ** 4:  There must be an active transaction.
       
  2650 **
       
  2651 ** No checking is done to make sure that page iTable really is the
       
  2652 ** root page of a b-tree.  If it is not, then the cursor acquired
       
  2653 ** will not work correctly.
       
  2654 **
       
  2655 ** The comparison function must be logically the same for every cursor
       
  2656 ** on a particular table.  Changing the comparison function will result
       
  2657 ** in incorrect operations.  If the comparison function is NULL, a
       
  2658 ** default comparison function is used.  The comparison function is
       
  2659 ** always ignored for INTKEY tables.
       
  2660 */
       
  2661 static int btreeCursor(
       
  2662   Btree *p,                                   /* The btree */
       
  2663   int iTable,                                 /* Root page of table to open */
       
  2664   int wrFlag,                                 /* 1 to write. 0 read-only */
       
  2665   int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
       
  2666   void *pArg,                                 /* First arg to xCompare() */
       
  2667   BtCursor **ppCur                            /* Write new cursor here */
       
  2668 ){
       
  2669   int rc;
       
  2670   BtCursor *pCur;
       
  2671   BtShared *pBt = p->pBt;
       
  2672 
       
  2673   assert( sqlite3BtreeHoldsMutex(p) );
       
  2674   *ppCur = 0;
       
  2675   if( wrFlag ){
       
  2676     if( pBt->readOnly ){
       
  2677       return SQLITE_READONLY;
       
  2678     }
       
  2679     if( checkReadLocks(p, iTable, 0) ){
       
  2680       return SQLITE_LOCKED;
       
  2681     }
       
  2682   }
       
  2683 
       
  2684   if( pBt->pPage1==0 ){
       
  2685     rc = lockBtreeWithRetry(p);
       
  2686     if( rc!=SQLITE_OK ){
       
  2687       return rc;
       
  2688     }
       
  2689     if( pBt->readOnly && wrFlag ){
       
  2690       return SQLITE_READONLY;
       
  2691     }
       
  2692   }
       
  2693   pCur = (BtCursor*)sqlite3MallocZero( sizeof(*pCur) );
       
  2694   if( pCur==0 ){
       
  2695     rc = SQLITE_NOMEM;
       
  2696     goto create_cursor_exception;
       
  2697   }
       
  2698   pCur->pgnoRoot = (Pgno)iTable;
       
  2699   if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
       
  2700     rc = SQLITE_EMPTY;
       
  2701     goto create_cursor_exception;
       
  2702   }
       
  2703   rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
       
  2704   if( rc!=SQLITE_OK ){
       
  2705     goto create_cursor_exception;
       
  2706   }
       
  2707 
       
  2708   /* Now that no other errors can occur, finish filling in the BtCursor
       
  2709   ** variables, link the cursor into the BtShared list and set *ppCur (the
       
  2710   ** output argument to this function).
       
  2711   */
       
  2712   pCur->xCompare = xCmp ? xCmp : dfltCompare;
       
  2713   pCur->pArg = pArg;
       
  2714   pCur->pBtree = p;
       
  2715   pCur->pBt = pBt;
       
  2716   pCur->wrFlag = wrFlag;
       
  2717   pCur->pNext = pBt->pCursor;
       
  2718   if( pCur->pNext ){
       
  2719     pCur->pNext->pPrev = pCur;
       
  2720   }
       
  2721   pBt->pCursor = pCur;
       
  2722   pCur->eState = CURSOR_INVALID;
       
  2723   *ppCur = pCur;
       
  2724 
       
  2725   return SQLITE_OK;
       
  2726 
       
  2727 create_cursor_exception:
       
  2728   if( pCur ){
       
  2729     releasePage(pCur->pPage);
       
  2730     sqlite3_free(pCur);
       
  2731   }
       
  2732   unlockBtreeIfUnused(pBt);
       
  2733   return rc;
       
  2734 }
       
  2735 int sqlite3BtreeCursor(
       
  2736   Btree *p,                                   /* The btree */
       
  2737   int iTable,                                 /* Root page of table to open */
       
  2738   int wrFlag,                                 /* 1 to write. 0 read-only */
       
  2739   int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
       
  2740   void *pArg,                                 /* First arg to xCompare() */
       
  2741   BtCursor **ppCur                            /* Write new cursor here */
       
  2742 ){
       
  2743   int rc;
       
  2744   sqlite3BtreeEnter(p);
       
  2745   p->pBt->db = p->db;
       
  2746   rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur);
       
  2747   sqlite3BtreeLeave(p);
       
  2748   return rc;
       
  2749 }
       
  2750 
       
  2751 
       
  2752 /*
       
  2753 ** Close a cursor.  The read lock on the database file is released
       
  2754 ** when the last cursor is closed.
       
  2755 */
       
  2756 int sqlite3BtreeCloseCursor(BtCursor *pCur){
       
  2757   BtShared *pBt = pCur->pBt;
       
  2758   Btree *pBtree = pCur->pBtree;
       
  2759 
       
  2760   sqlite3BtreeEnter(pBtree);
       
  2761   pBt->db = pBtree->db;
       
  2762   clearCursorPosition(pCur);
       
  2763   if( pCur->pPrev ){
       
  2764     pCur->pPrev->pNext = pCur->pNext;
       
  2765   }else{
       
  2766     pBt->pCursor = pCur->pNext;
       
  2767   }
       
  2768   if( pCur->pNext ){
       
  2769     pCur->pNext->pPrev = pCur->pPrev;
       
  2770   }
       
  2771   releasePage(pCur->pPage);
       
  2772   unlockBtreeIfUnused(pBt);
       
  2773   invalidateOverflowCache(pCur);
       
  2774   sqlite3_free(pCur);
       
  2775   sqlite3BtreeLeave(pBtree);
       
  2776   return SQLITE_OK;
       
  2777 }
       
  2778 
       
  2779 /*
       
  2780 ** Make a temporary cursor by filling in the fields of pTempCur.
       
  2781 ** The temporary cursor is not on the cursor list for the Btree.
       
  2782 */
       
  2783 void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
       
  2784   assert( cursorHoldsMutex(pCur) );
       
  2785   memcpy(pTempCur, pCur, sizeof(*pCur));
       
  2786   pTempCur->pNext = 0;
       
  2787   pTempCur->pPrev = 0;
       
  2788   if( pTempCur->pPage ){
       
  2789     sqlite3PagerRef(pTempCur->pPage->pDbPage);
       
  2790   }
       
  2791 }
       
  2792 
       
  2793 /*
       
  2794 ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
       
  2795 ** function above.
       
  2796 */
       
  2797 void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
       
  2798   assert( cursorHoldsMutex(pCur) );
       
  2799   if( pCur->pPage ){
       
  2800     sqlite3PagerUnref(pCur->pPage->pDbPage);
       
  2801   }
       
  2802 }
       
  2803 
       
  2804 /*
       
  2805 ** Make sure the BtCursor* given in the argument has a valid
       
  2806 ** BtCursor.info structure.  If it is not already valid, call
       
  2807 ** sqlite3BtreeParseCell() to fill it in.
       
  2808 **
       
  2809 ** BtCursor.info is a cache of the information in the current cell.
       
  2810 ** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
       
  2811 **
       
  2812 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
       
  2813 ** compiler to crash when getCellInfo() is implemented as a macro.
       
  2814 ** But there is a measureable speed advantage to using the macro on gcc
       
  2815 ** (when less compiler optimizations like -Os or -O0 are used and the
       
  2816 ** compiler is not doing agressive inlining.)  So we use a real function
       
  2817 ** for MSVC and a macro for everything else.  Ticket #2457.
       
  2818 */
       
  2819 #ifndef NDEBUG
       
  2820   static void assertCellInfo(BtCursor *pCur){
       
  2821     CellInfo info;
       
  2822     memset(&info, 0, sizeof(info));
       
  2823     sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info);
       
  2824     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
       
  2825   }
       
  2826 #else
       
  2827   #define assertCellInfo(x)
       
  2828 #endif
       
  2829 #ifdef _MSC_VER
       
  2830   /* Use a real function in MSVC to work around bugs in that compiler. */
       
  2831   static void getCellInfo(BtCursor *pCur){
       
  2832     if( pCur->info.nSize==0 ){
       
  2833       sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);
       
  2834     }else{
       
  2835       assertCellInfo(pCur);
       
  2836     }
       
  2837   }
       
  2838 #else /* if not _MSC_VER */
       
  2839   /* Use a macro in all other compilers so that the function is inlined */
       
  2840 #define getCellInfo(pCur)                                               \
       
  2841   if( pCur->info.nSize==0 ){                                            \
       
  2842     sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);         \
       
  2843   }else{                                                                \
       
  2844     assertCellInfo(pCur);                                               \
       
  2845   }
       
  2846 #endif /* _MSC_VER */
       
  2847 
       
  2848 /*
       
  2849 ** Set *pSize to the size of the buffer needed to hold the value of
       
  2850 ** the key for the current entry.  If the cursor is not pointing
       
  2851 ** to a valid entry, *pSize is set to 0. 
       
  2852 **
       
  2853 ** For a table with the INTKEY flag set, this routine returns the key
       
  2854 ** itself, not the number of bytes in the key.
       
  2855 */
       
  2856 int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
       
  2857   int rc;
       
  2858 
       
  2859   assert( cursorHoldsMutex(pCur) );
       
  2860   rc = restoreOrClearCursorPosition(pCur);
       
  2861   if( rc==SQLITE_OK ){
       
  2862     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
       
  2863     if( pCur->eState==CURSOR_INVALID ){
       
  2864       *pSize = 0;
       
  2865     }else{
       
  2866       getCellInfo(pCur);
       
  2867       *pSize = pCur->info.nKey;
       
  2868     }
       
  2869   }
       
  2870   return rc;
       
  2871 }
       
  2872 
       
  2873 /*
       
  2874 ** Set *pSize to the number of bytes of data in the entry the
       
  2875 ** cursor currently points to.  Always return SQLITE_OK.
       
  2876 ** Failure is not possible.  If the cursor is not currently
       
  2877 ** pointing to an entry (which can happen, for example, if
       
  2878 ** the database is empty) then *pSize is set to 0.
       
  2879 */
       
  2880 int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
       
  2881   int rc;
       
  2882 
       
  2883   assert( cursorHoldsMutex(pCur) );
       
  2884   rc = restoreOrClearCursorPosition(pCur);
       
  2885   if( rc==SQLITE_OK ){
       
  2886     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
       
  2887     if( pCur->eState==CURSOR_INVALID ){
       
  2888       /* Not pointing at a valid entry - set *pSize to 0. */
       
  2889       *pSize = 0;
       
  2890     }else{
       
  2891       getCellInfo(pCur);
       
  2892       *pSize = pCur->info.nData;
       
  2893     }
       
  2894   }
       
  2895   return rc;
       
  2896 }
       
  2897 
       
  2898 /*
       
  2899 ** Given the page number of an overflow page in the database (parameter
       
  2900 ** ovfl), this function finds the page number of the next page in the 
       
  2901 ** linked list of overflow pages. If possible, it uses the auto-vacuum
       
  2902 ** pointer-map data instead of reading the content of page ovfl to do so. 
       
  2903 **
       
  2904 ** If an error occurs an SQLite error code is returned. Otherwise:
       
  2905 **
       
  2906 ** Unless pPgnoNext is NULL, the page number of the next overflow 
       
  2907 ** page in the linked list is written to *pPgnoNext. If page ovfl
       
  2908 ** is the last page in its linked list, *pPgnoNext is set to zero. 
       
  2909 **
       
  2910 ** If ppPage is not NULL, *ppPage is set to the MemPage* handle
       
  2911 ** for page ovfl. The underlying pager page may have been requested
       
  2912 ** with the noContent flag set, so the page data accessable via
       
  2913 ** this handle may not be trusted.
       
  2914 */
       
  2915 static int getOverflowPage(
       
  2916   BtShared *pBt, 
       
  2917   Pgno ovfl,                   /* Overflow page */
       
  2918   MemPage **ppPage,            /* OUT: MemPage handle */
       
  2919   Pgno *pPgnoNext              /* OUT: Next overflow page number */
       
  2920 ){
       
  2921   Pgno next = 0;
       
  2922   int rc;
       
  2923 
       
  2924   assert( sqlite3_mutex_held(pBt->mutex) );
       
  2925   /* One of these must not be NULL. Otherwise, why call this function? */
       
  2926   assert(ppPage || pPgnoNext);
       
  2927 
       
  2928   /* If pPgnoNext is NULL, then this function is being called to obtain
       
  2929   ** a MemPage* reference only. No page-data is required in this case.
       
  2930   */
       
  2931   if( !pPgnoNext ){
       
  2932     return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
       
  2933   }
       
  2934 
       
  2935 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  2936   /* Try to find the next page in the overflow list using the
       
  2937   ** autovacuum pointer-map pages. Guess that the next page in 
       
  2938   ** the overflow list is page number (ovfl+1). If that guess turns 
       
  2939   ** out to be wrong, fall back to loading the data of page 
       
  2940   ** number ovfl to determine the next page number.
       
  2941   */
       
  2942   if( pBt->autoVacuum ){
       
  2943     Pgno pgno;
       
  2944     Pgno iGuess = ovfl+1;
       
  2945     u8 eType;
       
  2946 
       
  2947     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
       
  2948       iGuess++;
       
  2949     }
       
  2950 
       
  2951     if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){
       
  2952       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
       
  2953       if( rc!=SQLITE_OK ){
       
  2954         return rc;
       
  2955       }
       
  2956       if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
       
  2957         next = iGuess;
       
  2958       }
       
  2959     }
       
  2960   }
       
  2961 #endif
       
  2962 
       
  2963   if( next==0 || ppPage ){
       
  2964     MemPage *pPage = 0;
       
  2965 
       
  2966     rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
       
  2967     assert(rc==SQLITE_OK || pPage==0);
       
  2968     if( next==0 && rc==SQLITE_OK ){
       
  2969       next = get4byte(pPage->aData);
       
  2970     }
       
  2971 
       
  2972     if( ppPage ){
       
  2973       *ppPage = pPage;
       
  2974     }else{
       
  2975       releasePage(pPage);
       
  2976     }
       
  2977   }
       
  2978   *pPgnoNext = next;
       
  2979 
       
  2980   return rc;
       
  2981 }
       
  2982 
       
  2983 /*
       
  2984 ** Copy data from a buffer to a page, or from a page to a buffer.
       
  2985 **
       
  2986 ** pPayload is a pointer to data stored on database page pDbPage.
       
  2987 ** If argument eOp is false, then nByte bytes of data are copied
       
  2988 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
       
  2989 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
       
  2990 ** of data are copied from the buffer pBuf to pPayload.
       
  2991 **
       
  2992 ** SQLITE_OK is returned on success, otherwise an error code.
       
  2993 */
       
  2994 static int copyPayload(
       
  2995   void *pPayload,           /* Pointer to page data */
       
  2996   void *pBuf,               /* Pointer to buffer */
       
  2997   int nByte,                /* Number of bytes to copy */
       
  2998   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
       
  2999   DbPage *pDbPage           /* Page containing pPayload */
       
  3000 ){
       
  3001   if( eOp ){
       
  3002     /* Copy data from buffer to page (a write operation) */
       
  3003     int rc = sqlite3PagerWrite(pDbPage);
       
  3004     if( rc!=SQLITE_OK ){
       
  3005       return rc;
       
  3006     }
       
  3007     memcpy(pPayload, pBuf, nByte);
       
  3008   }else{
       
  3009     /* Copy data from page to buffer (a read operation) */
       
  3010     memcpy(pBuf, pPayload, nByte);
       
  3011   }
       
  3012   return SQLITE_OK;
       
  3013 }
       
  3014 
       
  3015 /*
       
  3016 ** This function is used to read or overwrite payload information
       
  3017 ** for the entry that the pCur cursor is pointing to. If the eOp
       
  3018 ** parameter is 0, this is a read operation (data copied into
       
  3019 ** buffer pBuf). If it is non-zero, a write (data copied from
       
  3020 ** buffer pBuf).
       
  3021 **
       
  3022 ** A total of "amt" bytes are read or written beginning at "offset".
       
  3023 ** Data is read to or from the buffer pBuf.
       
  3024 **
       
  3025 ** This routine does not make a distinction between key and data.
       
  3026 ** It just reads or writes bytes from the payload area.  Data might 
       
  3027 ** appear on the main page or be scattered out on multiple overflow 
       
  3028 ** pages.
       
  3029 **
       
  3030 ** If the BtCursor.isIncrblobHandle flag is set, and the current
       
  3031 ** cursor entry uses one or more overflow pages, this function
       
  3032 ** allocates space for and lazily popluates the overflow page-list 
       
  3033 ** cache array (BtCursor.aOverflow). Subsequent calls use this
       
  3034 ** cache to make seeking to the supplied offset more efficient.
       
  3035 **
       
  3036 ** Once an overflow page-list cache has been allocated, it may be
       
  3037 ** invalidated if some other cursor writes to the same table, or if
       
  3038 ** the cursor is moved to a different row. Additionally, in auto-vacuum
       
  3039 ** mode, the following events may invalidate an overflow page-list cache.
       
  3040 **
       
  3041 **   * An incremental vacuum,
       
  3042 **   * A commit in auto_vacuum="full" mode,
       
  3043 **   * Creating a table (may require moving an overflow page).
       
  3044 */
       
  3045 static int accessPayload(
       
  3046   BtCursor *pCur,      /* Cursor pointing to entry to read from */
       
  3047   int offset,          /* Begin reading this far into payload */
       
  3048   int amt,             /* Read this many bytes */
       
  3049   unsigned char *pBuf, /* Write the bytes into this buffer */ 
       
  3050   int skipKey,         /* offset begins at data if this is true */
       
  3051   int eOp              /* zero to read. non-zero to write. */
       
  3052 ){
       
  3053   unsigned char *aPayload;
       
  3054   int rc = SQLITE_OK;
       
  3055   u32 nKey;
       
  3056   int iIdx = 0;
       
  3057   MemPage *pPage = pCur->pPage;     /* Btree page of current cursor entry */
       
  3058   BtShared *pBt;                   /* Btree this cursor belongs to */
       
  3059 
       
  3060   assert( pPage );
       
  3061   assert( pCur->eState==CURSOR_VALID );
       
  3062   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
       
  3063   assert( offset>=0 );
       
  3064   assert( cursorHoldsMutex(pCur) );
       
  3065 
       
  3066   getCellInfo(pCur);
       
  3067   aPayload = pCur->info.pCell + pCur->info.nHeader;
       
  3068   nKey = (pPage->intKey ? 0 : pCur->info.nKey);
       
  3069 
       
  3070   if( skipKey ){
       
  3071     offset += nKey;
       
  3072   }
       
  3073   if( offset+amt > nKey+pCur->info.nData ){
       
  3074     /* Trying to read or write past the end of the data is an error */
       
  3075     return SQLITE_ERROR;
       
  3076   }
       
  3077 
       
  3078   /* Check if data must be read/written to/from the btree page itself. */
       
  3079   if( offset<pCur->info.nLocal ){
       
  3080     int a = amt;
       
  3081     if( a+offset>pCur->info.nLocal ){
       
  3082       a = pCur->info.nLocal - offset;
       
  3083     }
       
  3084     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
       
  3085     offset = 0;
       
  3086     pBuf += a;
       
  3087     amt -= a;
       
  3088   }else{
       
  3089     offset -= pCur->info.nLocal;
       
  3090   }
       
  3091 
       
  3092   pBt = pCur->pBt;
       
  3093   if( rc==SQLITE_OK && amt>0 ){
       
  3094     const int ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
       
  3095     Pgno nextPage;
       
  3096 
       
  3097     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
       
  3098 
       
  3099 #ifndef SQLITE_OMIT_INCRBLOB
       
  3100     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
       
  3101     ** has not been allocated, allocate it now. The array is sized at
       
  3102     ** one entry for each overflow page in the overflow chain. The
       
  3103     ** page number of the first overflow page is stored in aOverflow[0],
       
  3104     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
       
  3105     ** (the cache is lazily populated).
       
  3106     */
       
  3107     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
       
  3108       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
       
  3109       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
       
  3110       if( nOvfl && !pCur->aOverflow ){
       
  3111         rc = SQLITE_NOMEM;
       
  3112       }
       
  3113     }
       
  3114 
       
  3115     /* If the overflow page-list cache has been allocated and the
       
  3116     ** entry for the first required overflow page is valid, skip
       
  3117     ** directly to it.
       
  3118     */
       
  3119     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
       
  3120       iIdx = (offset/ovflSize);
       
  3121       nextPage = pCur->aOverflow[iIdx];
       
  3122       offset = (offset%ovflSize);
       
  3123     }
       
  3124 #endif
       
  3125 
       
  3126     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
       
  3127 
       
  3128 #ifndef SQLITE_OMIT_INCRBLOB
       
  3129       /* If required, populate the overflow page-list cache. */
       
  3130       if( pCur->aOverflow ){
       
  3131         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
       
  3132         pCur->aOverflow[iIdx] = nextPage;
       
  3133       }
       
  3134 #endif
       
  3135 
       
  3136       if( offset>=ovflSize ){
       
  3137         /* The only reason to read this page is to obtain the page
       
  3138         ** number for the next page in the overflow chain. The page
       
  3139         ** data is not required. So first try to lookup the overflow
       
  3140         ** page-list cache, if any, then fall back to the getOverflowPage()
       
  3141         ** function.
       
  3142         */
       
  3143 #ifndef SQLITE_OMIT_INCRBLOB
       
  3144         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
       
  3145           nextPage = pCur->aOverflow[iIdx+1];
       
  3146         } else 
       
  3147 #endif
       
  3148           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
       
  3149         offset -= ovflSize;
       
  3150       }else{
       
  3151         /* Need to read this page properly. It contains some of the
       
  3152         ** range of data that is being read (eOp==0) or written (eOp!=0).
       
  3153         */
       
  3154         DbPage *pDbPage;
       
  3155         int a = amt;
       
  3156         rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
       
  3157         if( rc==SQLITE_OK ){
       
  3158           aPayload = (unsigned char*)sqlite3PagerGetData(pDbPage);
       
  3159           nextPage = get4byte(aPayload);
       
  3160           if( a + offset > ovflSize ){
       
  3161             a = ovflSize - offset;
       
  3162           }
       
  3163           rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
       
  3164           sqlite3PagerUnref(pDbPage);
       
  3165           offset = 0;
       
  3166           amt -= a;
       
  3167           pBuf += a;
       
  3168         }
       
  3169       }
       
  3170     }
       
  3171   }
       
  3172 
       
  3173   if( rc==SQLITE_OK && amt>0 ){
       
  3174     return SQLITE_CORRUPT_BKPT;
       
  3175   }
       
  3176   return rc;
       
  3177 }
       
  3178 
       
  3179 /*
       
  3180 ** Read part of the key associated with cursor pCur.  Exactly
       
  3181 ** "amt" bytes will be transfered into pBuf[].  The transfer
       
  3182 ** begins at "offset".
       
  3183 **
       
  3184 ** Return SQLITE_OK on success or an error code if anything goes
       
  3185 ** wrong.  An error is returned if "offset+amt" is larger than
       
  3186 ** the available payload.
       
  3187 */
       
  3188 int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
       
  3189   int rc;
       
  3190 
       
  3191   assert( cursorHoldsMutex(pCur) );
       
  3192   rc = restoreOrClearCursorPosition(pCur);
       
  3193   if( rc==SQLITE_OK ){
       
  3194     assert( pCur->eState==CURSOR_VALID );
       
  3195     assert( pCur->pPage!=0 );
       
  3196     if( pCur->pPage->intKey ){
       
  3197       return SQLITE_CORRUPT_BKPT;
       
  3198     }
       
  3199     assert( pCur->pPage->intKey==0 );
       
  3200     assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
       
  3201     rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
       
  3202   }
       
  3203   return rc;
       
  3204 }
       
  3205 
       
  3206 /*
       
  3207 ** Read part of the data associated with cursor pCur.  Exactly
       
  3208 ** "amt" bytes will be transfered into pBuf[].  The transfer
       
  3209 ** begins at "offset".
       
  3210 **
       
  3211 ** Return SQLITE_OK on success or an error code if anything goes
       
  3212 ** wrong.  An error is returned if "offset+amt" is larger than
       
  3213 ** the available payload.
       
  3214 */
       
  3215 int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
       
  3216   int rc;
       
  3217 
       
  3218   assert( cursorHoldsMutex(pCur) );
       
  3219   rc = restoreOrClearCursorPosition(pCur);
       
  3220   if( rc==SQLITE_OK ){
       
  3221     assert( pCur->eState==CURSOR_VALID );
       
  3222     assert( pCur->pPage!=0 );
       
  3223     assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
       
  3224     rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 1, 0);
       
  3225   }
       
  3226   return rc;
       
  3227 }
       
  3228 
       
  3229 /*
       
  3230 ** Return a pointer to payload information from the entry that the 
       
  3231 ** pCur cursor is pointing to.  The pointer is to the beginning of
       
  3232 ** the key if skipKey==0 and it points to the beginning of data if
       
  3233 ** skipKey==1.  The number of bytes of available key/data is written
       
  3234 ** into *pAmt.  If *pAmt==0, then the value returned will not be
       
  3235 ** a valid pointer.
       
  3236 **
       
  3237 ** This routine is an optimization.  It is common for the entire key
       
  3238 ** and data to fit on the local page and for there to be no overflow
       
  3239 ** pages.  When that is so, this routine can be used to access the
       
  3240 ** key and data without making a copy.  If the key and/or data spills
       
  3241 ** onto overflow pages, then accessPayload() must be used to reassembly
       
  3242 ** the key/data and copy it into a preallocated buffer.
       
  3243 **
       
  3244 ** The pointer returned by this routine looks directly into the cached
       
  3245 ** page of the database.  The data might change or move the next time
       
  3246 ** any btree routine is called.
       
  3247 */
       
  3248 static const unsigned char *fetchPayload(
       
  3249   BtCursor *pCur,      /* Cursor pointing to entry to read from */
       
  3250   int *pAmt,           /* Write the number of available bytes here */
       
  3251   int skipKey          /* read beginning at data if this is true */
       
  3252 ){
       
  3253   unsigned char *aPayload;
       
  3254   MemPage *pPage;
       
  3255   u32 nKey;
       
  3256   int nLocal;
       
  3257 
       
  3258   assert( pCur!=0 && pCur->pPage!=0 );
       
  3259   assert( pCur->eState==CURSOR_VALID );
       
  3260   assert( cursorHoldsMutex(pCur) );
       
  3261   pPage = pCur->pPage;
       
  3262   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
       
  3263   getCellInfo(pCur);
       
  3264   aPayload = pCur->info.pCell;
       
  3265   aPayload += pCur->info.nHeader;
       
  3266   if( pPage->intKey ){
       
  3267     nKey = 0;
       
  3268   }else{
       
  3269     nKey = pCur->info.nKey;
       
  3270   }
       
  3271   if( skipKey ){
       
  3272     aPayload += nKey;
       
  3273     nLocal = pCur->info.nLocal - nKey;
       
  3274   }else{
       
  3275     nLocal = pCur->info.nLocal;
       
  3276     if( nLocal>nKey ){
       
  3277       nLocal = nKey;
       
  3278     }
       
  3279   }
       
  3280   *pAmt = nLocal;
       
  3281   return aPayload;
       
  3282 }
       
  3283 
       
  3284 
       
  3285 /*
       
  3286 ** For the entry that cursor pCur is point to, return as
       
  3287 ** many bytes of the key or data as are available on the local
       
  3288 ** b-tree page.  Write the number of available bytes into *pAmt.
       
  3289 **
       
  3290 ** The pointer returned is ephemeral.  The key/data may move
       
  3291 ** or be destroyed on the next call to any Btree routine,
       
  3292 ** including calls from other threads against the same cache.
       
  3293 ** Hence, a mutex on the BtShared should be held prior to calling
       
  3294 ** this routine.
       
  3295 **
       
  3296 ** These routines is used to get quick access to key and data
       
  3297 ** in the common case where no overflow pages are used.
       
  3298 */
       
  3299 const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
       
  3300   assert( cursorHoldsMutex(pCur) );
       
  3301   if( pCur->eState==CURSOR_VALID ){
       
  3302     return (const void*)fetchPayload(pCur, pAmt, 0);
       
  3303   }
       
  3304   return 0;
       
  3305 }
       
  3306 const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
       
  3307   assert( cursorHoldsMutex(pCur) );
       
  3308   if( pCur->eState==CURSOR_VALID ){
       
  3309     return (const void*)fetchPayload(pCur, pAmt, 1);
       
  3310   }
       
  3311   return 0;
       
  3312 }
       
  3313 
       
  3314 
       
  3315 /*
       
  3316 ** Move the cursor down to a new child page.  The newPgno argument is the
       
  3317 ** page number of the child page to move to.
       
  3318 */
       
  3319 static int moveToChild(BtCursor *pCur, u32 newPgno){
       
  3320   int rc;
       
  3321   MemPage *pNewPage;
       
  3322   MemPage *pOldPage;
       
  3323   BtShared *pBt = pCur->pBt;
       
  3324 
       
  3325   assert( cursorHoldsMutex(pCur) );
       
  3326   assert( pCur->eState==CURSOR_VALID );
       
  3327   rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
       
  3328   if( rc ) return rc;
       
  3329   pNewPage->idxParent = pCur->idx;
       
  3330   pOldPage = pCur->pPage;
       
  3331   pOldPage->idxShift = 0;
       
  3332   releasePage(pOldPage);
       
  3333   pCur->pPage = pNewPage;
       
  3334   pCur->idx = 0;
       
  3335   pCur->info.nSize = 0;
       
  3336   if( pNewPage->nCell<1 ){
       
  3337     return SQLITE_CORRUPT_BKPT;
       
  3338   }
       
  3339   return SQLITE_OK;
       
  3340 }
       
  3341 
       
  3342 /*
       
  3343 ** Return true if the page is the virtual root of its table.
       
  3344 **
       
  3345 ** The virtual root page is the root page for most tables.  But
       
  3346 ** for the table rooted on page 1, sometime the real root page
       
  3347 ** is empty except for the right-pointer.  In such cases the
       
  3348 ** virtual root page is the page that the right-pointer of page
       
  3349 ** 1 is pointing to.
       
  3350 */
       
  3351 int sqlite3BtreeIsRootPage(MemPage *pPage){
       
  3352   MemPage *pParent;
       
  3353 
       
  3354   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  3355   pParent = pPage->pParent;
       
  3356   if( pParent==0 ) return 1;
       
  3357   if( pParent->pgno>1 ) return 0;
       
  3358   if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
       
  3359   return 0;
       
  3360 }
       
  3361 
       
  3362 /*
       
  3363 ** Move the cursor up to the parent page.
       
  3364 **
       
  3365 ** pCur->idx is set to the cell index that contains the pointer
       
  3366 ** to the page we are coming from.  If we are coming from the
       
  3367 ** right-most child page then pCur->idx is set to one more than
       
  3368 ** the largest cell index.
       
  3369 */
       
  3370 void sqlite3BtreeMoveToParent(BtCursor *pCur){
       
  3371   MemPage *pParent;
       
  3372   MemPage *pPage;
       
  3373   int idxParent;
       
  3374 
       
  3375   assert( cursorHoldsMutex(pCur) );
       
  3376   assert( pCur->eState==CURSOR_VALID );
       
  3377   pPage = pCur->pPage;
       
  3378   assert( pPage!=0 );
       
  3379   assert( !sqlite3BtreeIsRootPage(pPage) );
       
  3380   pParent = pPage->pParent;
       
  3381   assert( pParent!=0 );
       
  3382   idxParent = pPage->idxParent;
       
  3383   sqlite3PagerRef(pParent->pDbPage);
       
  3384   releasePage(pPage);
       
  3385   pCur->pPage = pParent;
       
  3386   pCur->info.nSize = 0;
       
  3387   assert( pParent->idxShift==0 );
       
  3388   pCur->idx = idxParent;
       
  3389 }
       
  3390 
       
  3391 /*
       
  3392 ** Move the cursor to the root page
       
  3393 */
       
  3394 static int moveToRoot(BtCursor *pCur){
       
  3395   MemPage *pRoot;
       
  3396   int rc = SQLITE_OK;
       
  3397   Btree *p = pCur->pBtree;
       
  3398   BtShared *pBt = p->pBt;
       
  3399 
       
  3400   assert( cursorHoldsMutex(pCur) );
       
  3401   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
       
  3402   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
       
  3403   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
       
  3404   if( pCur->eState>=CURSOR_REQUIRESEEK ){
       
  3405     if( pCur->eState==CURSOR_FAULT ){
       
  3406       return pCur->skip;
       
  3407     }
       
  3408     clearCursorPosition(pCur);
       
  3409   }
       
  3410   pRoot = pCur->pPage;
       
  3411   if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
       
  3412     assert( pRoot->isInit );
       
  3413   }else{
       
  3414     if( 
       
  3415       SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
       
  3416     ){
       
  3417       pCur->eState = CURSOR_INVALID;
       
  3418       return rc;
       
  3419     }
       
  3420     releasePage(pCur->pPage);
       
  3421     pCur->pPage = pRoot;
       
  3422   }
       
  3423   pCur->idx = 0;
       
  3424   pCur->info.nSize = 0;
       
  3425   if( pRoot->nCell==0 && !pRoot->leaf ){
       
  3426     Pgno subpage;
       
  3427     assert( pRoot->pgno==1 );
       
  3428     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
       
  3429     assert( subpage>0 );
       
  3430     pCur->eState = CURSOR_VALID;
       
  3431     rc = moveToChild(pCur, subpage);
       
  3432   }
       
  3433   pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
       
  3434   return rc;
       
  3435 }
       
  3436 
       
  3437 /*
       
  3438 ** Move the cursor down to the left-most leaf entry beneath the
       
  3439 ** entry to which it is currently pointing.
       
  3440 **
       
  3441 ** The left-most leaf is the one with the smallest key - the first
       
  3442 ** in ascending order.
       
  3443 */
       
  3444 static int moveToLeftmost(BtCursor *pCur){
       
  3445   Pgno pgno;
       
  3446   int rc = SQLITE_OK;
       
  3447   MemPage *pPage;
       
  3448 
       
  3449   assert( cursorHoldsMutex(pCur) );
       
  3450   assert( pCur->eState==CURSOR_VALID );
       
  3451   while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
       
  3452     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
       
  3453     pgno = get4byte(findCell(pPage, pCur->idx));
       
  3454     rc = moveToChild(pCur, pgno);
       
  3455   }
       
  3456   return rc;
       
  3457 }
       
  3458 
       
  3459 /*
       
  3460 ** Move the cursor down to the right-most leaf entry beneath the
       
  3461 ** page to which it is currently pointing.  Notice the difference
       
  3462 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
       
  3463 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
       
  3464 ** finds the right-most entry beneath the *page*.
       
  3465 **
       
  3466 ** The right-most entry is the one with the largest key - the last
       
  3467 ** key in ascending order.
       
  3468 */
       
  3469 static int moveToRightmost(BtCursor *pCur){
       
  3470   Pgno pgno;
       
  3471   int rc = SQLITE_OK;
       
  3472   MemPage *pPage;
       
  3473 
       
  3474   assert( cursorHoldsMutex(pCur) );
       
  3475   assert( pCur->eState==CURSOR_VALID );
       
  3476   while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
       
  3477     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
  3478     pCur->idx = pPage->nCell;
       
  3479     rc = moveToChild(pCur, pgno);
       
  3480   }
       
  3481   if( rc==SQLITE_OK ){
       
  3482     pCur->idx = pPage->nCell - 1;
       
  3483     pCur->info.nSize = 0;
       
  3484   }
       
  3485   return SQLITE_OK;
       
  3486 }
       
  3487 
       
  3488 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
       
  3489 ** on success.  Set *pRes to 0 if the cursor actually points to something
       
  3490 ** or set *pRes to 1 if the table is empty.
       
  3491 */
       
  3492 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
       
  3493   int rc;
       
  3494 
       
  3495   assert( cursorHoldsMutex(pCur) );
       
  3496   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
  3497   rc = moveToRoot(pCur);
       
  3498   if( rc==SQLITE_OK ){
       
  3499     if( pCur->eState==CURSOR_INVALID ){
       
  3500       assert( pCur->pPage->nCell==0 );
       
  3501       *pRes = 1;
       
  3502       rc = SQLITE_OK;
       
  3503     }else{
       
  3504       assert( pCur->pPage->nCell>0 );
       
  3505       *pRes = 0;
       
  3506       rc = moveToLeftmost(pCur);
       
  3507     }
       
  3508   }
       
  3509   return rc;
       
  3510 }
       
  3511 
       
  3512 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
       
  3513 ** on success.  Set *pRes to 0 if the cursor actually points to something
       
  3514 ** or set *pRes to 1 if the table is empty.
       
  3515 */
       
  3516 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
       
  3517   int rc;
       
  3518  
       
  3519   assert( cursorHoldsMutex(pCur) );
       
  3520   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
  3521   rc = moveToRoot(pCur);
       
  3522   if( rc==SQLITE_OK ){
       
  3523     if( CURSOR_INVALID==pCur->eState ){
       
  3524       assert( pCur->pPage->nCell==0 );
       
  3525       *pRes = 1;
       
  3526     }else{
       
  3527       assert( pCur->eState==CURSOR_VALID );
       
  3528       *pRes = 0;
       
  3529       rc = moveToRightmost(pCur);
       
  3530     }
       
  3531   }
       
  3532   return rc;
       
  3533 }
       
  3534 
       
  3535 /* Move the cursor so that it points to an entry near pKey/nKey.
       
  3536 ** Return a success code.
       
  3537 **
       
  3538 ** For INTKEY tables, only the nKey parameter is used.  pKey is
       
  3539 ** ignored.  For other tables, nKey is the number of bytes of data
       
  3540 ** in pKey.  The comparison function specified when the cursor was
       
  3541 ** created is used to compare keys.
       
  3542 **
       
  3543 ** If an exact match is not found, then the cursor is always
       
  3544 ** left pointing at a leaf page which would hold the entry if it
       
  3545 ** were present.  The cursor might point to an entry that comes
       
  3546 ** before or after the key.
       
  3547 **
       
  3548 ** The result of comparing the key with the entry to which the
       
  3549 ** cursor is written to *pRes if pRes!=NULL.  The meaning of
       
  3550 ** this value is as follows:
       
  3551 **
       
  3552 **     *pRes<0      The cursor is left pointing at an entry that
       
  3553 **                  is smaller than pKey or if the table is empty
       
  3554 **                  and the cursor is therefore left point to nothing.
       
  3555 **
       
  3556 **     *pRes==0     The cursor is left pointing at an entry that
       
  3557 **                  exactly matches pKey.
       
  3558 **
       
  3559 **     *pRes>0      The cursor is left pointing at an entry that
       
  3560 **                  is larger than pKey.
       
  3561 **
       
  3562 */
       
  3563 int sqlite3BtreeMoveto(
       
  3564   BtCursor *pCur,        /* The cursor to be moved */
       
  3565   const void *pKey,      /* The key content for indices.  Not used by tables */
       
  3566   i64 nKey,              /* Size of pKey.  Or the key for tables */
       
  3567   int biasRight,         /* If true, bias the search to the high end */
       
  3568   int *pRes              /* Search result flag */
       
  3569 ){
       
  3570   int rc;
       
  3571 
       
  3572   assert( cursorHoldsMutex(pCur) );
       
  3573   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
  3574   rc = moveToRoot(pCur);
       
  3575   if( rc ){
       
  3576     return rc;
       
  3577   }
       
  3578   assert( pCur->pPage );
       
  3579   assert( pCur->pPage->isInit );
       
  3580   if( pCur->eState==CURSOR_INVALID ){
       
  3581     *pRes = -1;
       
  3582     assert( pCur->pPage->nCell==0 );
       
  3583     return SQLITE_OK;
       
  3584   }
       
  3585   for(;;){
       
  3586     int lwr, upr;
       
  3587     Pgno chldPg;
       
  3588     MemPage *pPage = pCur->pPage;
       
  3589     int c = -1;  /* pRes return if table is empty must be -1 */
       
  3590     lwr = 0;
       
  3591     upr = pPage->nCell-1;
       
  3592     if( !pPage->intKey && pKey==0 ){
       
  3593       return SQLITE_CORRUPT_BKPT;
       
  3594     }
       
  3595     if( biasRight ){
       
  3596       pCur->idx = upr;
       
  3597     }else{
       
  3598       pCur->idx = (upr+lwr)/2;
       
  3599     }
       
  3600     if( lwr<=upr ) for(;;){
       
  3601       void *pCellKey;
       
  3602       i64 nCellKey;
       
  3603       pCur->info.nSize = 0;
       
  3604       if( pPage->intKey ){
       
  3605         u8 *pCell;
       
  3606         pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
       
  3607         if( pPage->hasData ){
       
  3608           u32 dummy;
       
  3609           pCell += getVarint32(pCell, &dummy);
       
  3610         }
       
  3611         getVarint(pCell, (u64 *)&nCellKey);
       
  3612         if( nCellKey<nKey ){
       
  3613           c = -1;
       
  3614         }else if( nCellKey>nKey ){
       
  3615           c = +1;
       
  3616         }else{
       
  3617           c = 0;
       
  3618         }
       
  3619       }else{
       
  3620         int available;
       
  3621         pCellKey = (void *)fetchPayload(pCur, &available, 0);
       
  3622         nCellKey = pCur->info.nKey;
       
  3623         if( available>=nCellKey ){
       
  3624           c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
       
  3625         }else{
       
  3626           pCellKey = sqlite3_malloc( nCellKey );
       
  3627           if( pCellKey==0 ) return SQLITE_NOMEM;
       
  3628           rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
       
  3629           c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
       
  3630           sqlite3_free(pCellKey);
       
  3631           if( rc ){
       
  3632             return rc;
       
  3633           }
       
  3634         }
       
  3635       }
       
  3636       if( c==0 ){
       
  3637         if( pPage->leafData && !pPage->leaf ){
       
  3638           lwr = pCur->idx;
       
  3639           upr = lwr - 1;
       
  3640           break;
       
  3641         }else{
       
  3642           if( pRes ) *pRes = 0;
       
  3643           return SQLITE_OK;
       
  3644         }
       
  3645       }
       
  3646       if( c<0 ){
       
  3647         lwr = pCur->idx+1;
       
  3648       }else{
       
  3649         upr = pCur->idx-1;
       
  3650       }
       
  3651       if( lwr>upr ){
       
  3652         break;
       
  3653       }
       
  3654       pCur->idx = (lwr+upr)/2;
       
  3655     }
       
  3656     assert( lwr==upr+1 );
       
  3657     assert( pPage->isInit );
       
  3658     if( pPage->leaf ){
       
  3659       chldPg = 0;
       
  3660     }else if( lwr>=pPage->nCell ){
       
  3661       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
  3662     }else{
       
  3663       chldPg = get4byte(findCell(pPage, lwr));
       
  3664     }
       
  3665     if( chldPg==0 ){
       
  3666       assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
       
  3667       if( pRes ) *pRes = c;
       
  3668       return SQLITE_OK;
       
  3669     }
       
  3670     pCur->idx = lwr;
       
  3671     pCur->info.nSize = 0;
       
  3672     rc = moveToChild(pCur, chldPg);
       
  3673     if( rc ){
       
  3674       return rc;
       
  3675     }
       
  3676   }
       
  3677   /* NOT REACHED */
       
  3678 }
       
  3679 
       
  3680 
       
  3681 /*
       
  3682 ** Return TRUE if the cursor is not pointing at an entry of the table.
       
  3683 **
       
  3684 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
       
  3685 ** past the last entry in the table or sqlite3BtreePrev() moves past
       
  3686 ** the first entry.  TRUE is also returned if the table is empty.
       
  3687 */
       
  3688 int sqlite3BtreeEof(BtCursor *pCur){
       
  3689   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
       
  3690   ** have been deleted? This API will need to change to return an error code
       
  3691   ** as well as the boolean result value.
       
  3692   */
       
  3693   return (CURSOR_VALID!=pCur->eState);
       
  3694 }
       
  3695 
       
  3696 /*
       
  3697 ** Return the database connection handle for a cursor.
       
  3698 */
       
  3699 sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
       
  3700   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
  3701   return pCur->pBtree->db;
       
  3702 }
       
  3703 
       
  3704 /*
       
  3705 ** Advance the cursor to the next entry in the database.  If
       
  3706 ** successful then set *pRes=0.  If the cursor
       
  3707 ** was already pointing to the last entry in the database before
       
  3708 ** this routine was called, then set *pRes=1.
       
  3709 */
       
  3710 static int btreeNext(BtCursor *pCur, int *pRes){
       
  3711   int rc;
       
  3712   MemPage *pPage;
       
  3713 
       
  3714   assert( cursorHoldsMutex(pCur) );
       
  3715   rc = restoreOrClearCursorPosition(pCur);
       
  3716   if( rc!=SQLITE_OK ){
       
  3717     return rc;
       
  3718   }
       
  3719   assert( pRes!=0 );
       
  3720   pPage = pCur->pPage;
       
  3721   if( CURSOR_INVALID==pCur->eState ){
       
  3722     *pRes = 1;
       
  3723     return SQLITE_OK;
       
  3724   }
       
  3725   if( pCur->skip>0 ){
       
  3726     pCur->skip = 0;
       
  3727     *pRes = 0;
       
  3728     return SQLITE_OK;
       
  3729   }
       
  3730   pCur->skip = 0;
       
  3731 
       
  3732   assert( pPage->isInit );
       
  3733   assert( pCur->idx<pPage->nCell );
       
  3734 
       
  3735   pCur->idx++;
       
  3736   pCur->info.nSize = 0;
       
  3737   if( pCur->idx>=pPage->nCell ){
       
  3738     if( !pPage->leaf ){
       
  3739       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
       
  3740       if( rc ) return rc;
       
  3741       rc = moveToLeftmost(pCur);
       
  3742       *pRes = 0;
       
  3743       return rc;
       
  3744     }
       
  3745     do{
       
  3746       if( sqlite3BtreeIsRootPage(pPage) ){
       
  3747         *pRes = 1;
       
  3748         pCur->eState = CURSOR_INVALID;
       
  3749         return SQLITE_OK;
       
  3750       }
       
  3751       sqlite3BtreeMoveToParent(pCur);
       
  3752       pPage = pCur->pPage;
       
  3753     }while( pCur->idx>=pPage->nCell );
       
  3754     *pRes = 0;
       
  3755     if( pPage->leafData ){
       
  3756       rc = sqlite3BtreeNext(pCur, pRes);
       
  3757     }else{
       
  3758       rc = SQLITE_OK;
       
  3759     }
       
  3760     return rc;
       
  3761   }
       
  3762   *pRes = 0;
       
  3763   if( pPage->leaf ){
       
  3764     return SQLITE_OK;
       
  3765   }
       
  3766   rc = moveToLeftmost(pCur);
       
  3767   return rc;
       
  3768 }
       
  3769 int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
       
  3770   int rc;
       
  3771   assert( cursorHoldsMutex(pCur) );
       
  3772   rc = btreeNext(pCur, pRes);
       
  3773   return rc;
       
  3774 }
       
  3775 
       
  3776 
       
  3777 /*
       
  3778 ** Step the cursor to the back to the previous entry in the database.  If
       
  3779 ** successful then set *pRes=0.  If the cursor
       
  3780 ** was already pointing to the first entry in the database before
       
  3781 ** this routine was called, then set *pRes=1.
       
  3782 */
       
  3783 static int btreePrevious(BtCursor *pCur, int *pRes){
       
  3784   int rc;
       
  3785   Pgno pgno;
       
  3786   MemPage *pPage;
       
  3787 
       
  3788   assert( cursorHoldsMutex(pCur) );
       
  3789   rc = restoreOrClearCursorPosition(pCur);
       
  3790   if( rc!=SQLITE_OK ){
       
  3791     return rc;
       
  3792   }
       
  3793   if( CURSOR_INVALID==pCur->eState ){
       
  3794     *pRes = 1;
       
  3795     return SQLITE_OK;
       
  3796   }
       
  3797   if( pCur->skip<0 ){
       
  3798     pCur->skip = 0;
       
  3799     *pRes = 0;
       
  3800     return SQLITE_OK;
       
  3801   }
       
  3802   pCur->skip = 0;
       
  3803 
       
  3804   pPage = pCur->pPage;
       
  3805   assert( pPage->isInit );
       
  3806   assert( pCur->idx>=0 );
       
  3807   if( !pPage->leaf ){
       
  3808     pgno = get4byte( findCell(pPage, pCur->idx) );
       
  3809     rc = moveToChild(pCur, pgno);
       
  3810     if( rc ){
       
  3811       return rc;
       
  3812     }
       
  3813     rc = moveToRightmost(pCur);
       
  3814   }else{
       
  3815     while( pCur->idx==0 ){
       
  3816       if( sqlite3BtreeIsRootPage(pPage) ){
       
  3817         pCur->eState = CURSOR_INVALID;
       
  3818         *pRes = 1;
       
  3819         return SQLITE_OK;
       
  3820       }
       
  3821       sqlite3BtreeMoveToParent(pCur);
       
  3822       pPage = pCur->pPage;
       
  3823     }
       
  3824     pCur->idx--;
       
  3825     pCur->info.nSize = 0;
       
  3826     if( pPage->leafData && !pPage->leaf ){
       
  3827       rc = sqlite3BtreePrevious(pCur, pRes);
       
  3828     }else{
       
  3829       rc = SQLITE_OK;
       
  3830     }
       
  3831   }
       
  3832   *pRes = 0;
       
  3833   return rc;
       
  3834 }
       
  3835 int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
       
  3836   int rc;
       
  3837   assert( cursorHoldsMutex(pCur) );
       
  3838   rc = btreePrevious(pCur, pRes);
       
  3839   return rc;
       
  3840 }
       
  3841 
       
  3842 /*
       
  3843 ** Allocate a new page from the database file.
       
  3844 **
       
  3845 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
       
  3846 ** has already been called on the new page.)  The new page has also
       
  3847 ** been referenced and the calling routine is responsible for calling
       
  3848 ** sqlite3PagerUnref() on the new page when it is done.
       
  3849 **
       
  3850 ** SQLITE_OK is returned on success.  Any other return value indicates
       
  3851 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
       
  3852 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
       
  3853 **
       
  3854 ** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
       
  3855 ** locate a page close to the page number "nearby".  This can be used in an
       
  3856 ** attempt to keep related pages close to each other in the database file,
       
  3857 ** which in turn can make database access faster.
       
  3858 **
       
  3859 ** If the "exact" parameter is not 0, and the page-number nearby exists 
       
  3860 ** anywhere on the free-list, then it is guarenteed to be returned. This
       
  3861 ** is only used by auto-vacuum databases when allocating a new table.
       
  3862 */
       
  3863 static int allocateBtreePage(
       
  3864   BtShared *pBt, 
       
  3865   MemPage **ppPage, 
       
  3866   Pgno *pPgno, 
       
  3867   Pgno nearby,
       
  3868   u8 exact
       
  3869 ){
       
  3870   MemPage *pPage1;
       
  3871   int rc;
       
  3872   int n;     /* Number of pages on the freelist */
       
  3873   int k;     /* Number of leaves on the trunk of the freelist */
       
  3874   MemPage *pTrunk = 0;
       
  3875   MemPage *pPrevTrunk = 0;
       
  3876 
       
  3877   assert( sqlite3_mutex_held(pBt->mutex) );
       
  3878   pPage1 = pBt->pPage1;
       
  3879   n = get4byte(&pPage1->aData[36]);
       
  3880   if( n>0 ){
       
  3881     /* There are pages on the freelist.  Reuse one of those pages. */
       
  3882     Pgno iTrunk;
       
  3883     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
       
  3884     
       
  3885     /* If the 'exact' parameter was true and a query of the pointer-map
       
  3886     ** shows that the page 'nearby' is somewhere on the free-list, then
       
  3887     ** the entire-list will be searched for that page.
       
  3888     */
       
  3889 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  3890     if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){
       
  3891       u8 eType;
       
  3892       assert( nearby>0 );
       
  3893       assert( pBt->autoVacuum );
       
  3894       rc = ptrmapGet(pBt, nearby, &eType, 0);
       
  3895       if( rc ) return rc;
       
  3896       if( eType==PTRMAP_FREEPAGE ){
       
  3897         searchList = 1;
       
  3898       }
       
  3899       *pPgno = nearby;
       
  3900     }
       
  3901 #endif
       
  3902 
       
  3903     /* Decrement the free-list count by 1. Set iTrunk to the index of the
       
  3904     ** first free-list trunk page. iPrevTrunk is initially 1.
       
  3905     */
       
  3906     rc = sqlite3PagerWrite(pPage1->pDbPage);
       
  3907     if( rc ) return rc;
       
  3908     put4byte(&pPage1->aData[36], n-1);
       
  3909 
       
  3910     /* The code within this loop is run only once if the 'searchList' variable
       
  3911     ** is not true. Otherwise, it runs once for each trunk-page on the
       
  3912     ** free-list until the page 'nearby' is located.
       
  3913     */
       
  3914     do {
       
  3915       pPrevTrunk = pTrunk;
       
  3916       if( pPrevTrunk ){
       
  3917         iTrunk = get4byte(&pPrevTrunk->aData[0]);
       
  3918       }else{
       
  3919         iTrunk = get4byte(&pPage1->aData[32]);
       
  3920       }
       
  3921       rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
       
  3922       if( rc ){
       
  3923         pTrunk = 0;
       
  3924         goto end_allocate_page;
       
  3925       }
       
  3926 
       
  3927       k = get4byte(&pTrunk->aData[4]);
       
  3928       if( k==0 && !searchList ){
       
  3929         /* The trunk has no leaves and the list is not being searched. 
       
  3930         ** So extract the trunk page itself and use it as the newly 
       
  3931         ** allocated page */
       
  3932         assert( pPrevTrunk==0 );
       
  3933         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
  3934         if( rc ){
       
  3935           goto end_allocate_page;
       
  3936         }
       
  3937         *pPgno = iTrunk;
       
  3938         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
       
  3939         *ppPage = pTrunk;
       
  3940         pTrunk = 0;
       
  3941         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
       
  3942       }else if( k>pBt->usableSize/4 - 8 ){
       
  3943         /* Value of k is out of range.  Database corruption */
       
  3944         rc = SQLITE_CORRUPT_BKPT;
       
  3945         goto end_allocate_page;
       
  3946 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  3947       }else if( searchList && nearby==iTrunk ){
       
  3948         /* The list is being searched and this trunk page is the page
       
  3949         ** to allocate, regardless of whether it has leaves.
       
  3950         */
       
  3951         assert( *pPgno==iTrunk );
       
  3952         *ppPage = pTrunk;
       
  3953         searchList = 0;
       
  3954         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
  3955         if( rc ){
       
  3956           goto end_allocate_page;
       
  3957         }
       
  3958         if( k==0 ){
       
  3959           if( !pPrevTrunk ){
       
  3960             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
       
  3961           }else{
       
  3962             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
       
  3963           }
       
  3964         }else{
       
  3965           /* The trunk page is required by the caller but it contains 
       
  3966           ** pointers to free-list leaves. The first leaf becomes a trunk
       
  3967           ** page in this case.
       
  3968           */
       
  3969           MemPage *pNewTrunk;
       
  3970           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
       
  3971           rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
       
  3972           if( rc!=SQLITE_OK ){
       
  3973             goto end_allocate_page;
       
  3974           }
       
  3975           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
       
  3976           if( rc!=SQLITE_OK ){
       
  3977             releasePage(pNewTrunk);
       
  3978             goto end_allocate_page;
       
  3979           }
       
  3980           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
       
  3981           put4byte(&pNewTrunk->aData[4], k-1);
       
  3982           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
       
  3983           releasePage(pNewTrunk);
       
  3984           if( !pPrevTrunk ){
       
  3985             put4byte(&pPage1->aData[32], iNewTrunk);
       
  3986           }else{
       
  3987             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
       
  3988             if( rc ){
       
  3989               goto end_allocate_page;
       
  3990             }
       
  3991             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
       
  3992           }
       
  3993         }
       
  3994         pTrunk = 0;
       
  3995         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
       
  3996 #endif
       
  3997       }else{
       
  3998         /* Extract a leaf from the trunk */
       
  3999         int closest;
       
  4000         Pgno iPage;
       
  4001         unsigned char *aData = pTrunk->aData;
       
  4002         rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
  4003         if( rc ){
       
  4004           goto end_allocate_page;
       
  4005         }
       
  4006         if( nearby>0 ){
       
  4007           int i, dist;
       
  4008           closest = 0;
       
  4009           dist = get4byte(&aData[8]) - nearby;
       
  4010           if( dist<0 ) dist = -dist;
       
  4011           for(i=1; i<k; i++){
       
  4012             int d2 = get4byte(&aData[8+i*4]) - nearby;
       
  4013             if( d2<0 ) d2 = -d2;
       
  4014             if( d2<dist ){
       
  4015               closest = i;
       
  4016               dist = d2;
       
  4017             }
       
  4018           }
       
  4019         }else{
       
  4020           closest = 0;
       
  4021         }
       
  4022 
       
  4023         iPage = get4byte(&aData[8+closest*4]);
       
  4024         if( !searchList || iPage==nearby ){
       
  4025           *pPgno = iPage;
       
  4026           if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
       
  4027             /* Free page off the end of the file */
       
  4028             return SQLITE_CORRUPT_BKPT;
       
  4029           }
       
  4030           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
       
  4031                  ": %d more free pages\n",
       
  4032                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
       
  4033           if( closest<k-1 ){
       
  4034             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
       
  4035           }
       
  4036           put4byte(&aData[4], k-1);
       
  4037           rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
       
  4038           if( rc==SQLITE_OK ){
       
  4039             sqlite3PagerDontRollback((*ppPage)->pDbPage);
       
  4040             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
       
  4041             if( rc!=SQLITE_OK ){
       
  4042               releasePage(*ppPage);
       
  4043             }
       
  4044           }
       
  4045           searchList = 0;
       
  4046         }
       
  4047       }
       
  4048       releasePage(pPrevTrunk);
       
  4049       pPrevTrunk = 0;
       
  4050     }while( searchList );
       
  4051   }else{
       
  4052     /* There are no pages on the freelist, so create a new page at the
       
  4053     ** end of the file */
       
  4054     *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
       
  4055 
       
  4056 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4057     if( pBt->nTrunc ){
       
  4058       /* An incr-vacuum has already run within this transaction. So the
       
  4059       ** page to allocate is not from the physical end of the file, but
       
  4060       ** at pBt->nTrunc. 
       
  4061       */
       
  4062       *pPgno = pBt->nTrunc+1;
       
  4063       if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
       
  4064         (*pPgno)++;
       
  4065       }
       
  4066     }
       
  4067     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
       
  4068       /* If *pPgno refers to a pointer-map page, allocate two new pages
       
  4069       ** at the end of the file instead of one. The first allocated page
       
  4070       ** becomes a new pointer-map page, the second is used by the caller.
       
  4071       */
       
  4072       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
       
  4073       assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
  4074       (*pPgno)++;
       
  4075     }
       
  4076     if( pBt->nTrunc ){
       
  4077       pBt->nTrunc = *pPgno;
       
  4078     }
       
  4079 #endif
       
  4080 
       
  4081     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
  4082     rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
       
  4083     if( rc ) return rc;
       
  4084     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
       
  4085     if( rc!=SQLITE_OK ){
       
  4086       releasePage(*ppPage);
       
  4087     }
       
  4088     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
       
  4089   }
       
  4090 
       
  4091   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
       
  4092 
       
  4093 end_allocate_page:
       
  4094   releasePage(pTrunk);
       
  4095   releasePage(pPrevTrunk);
       
  4096   return rc;
       
  4097 }
       
  4098 
       
  4099 /*
       
  4100 ** Add a page of the database file to the freelist.
       
  4101 **
       
  4102 ** sqlite3PagerUnref() is NOT called for pPage.
       
  4103 */
       
  4104 static int freePage(MemPage *pPage){
       
  4105   BtShared *pBt = pPage->pBt;
       
  4106   MemPage *pPage1 = pBt->pPage1;
       
  4107   int rc, n, k;
       
  4108 
       
  4109   /* Prepare the page for freeing */
       
  4110   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4111   assert( pPage->pgno>1 );
       
  4112   pPage->isInit = 0;
       
  4113   releasePage(pPage->pParent);
       
  4114   pPage->pParent = 0;
       
  4115 
       
  4116   /* Increment the free page count on pPage1 */
       
  4117   rc = sqlite3PagerWrite(pPage1->pDbPage);
       
  4118   if( rc ) return rc;
       
  4119   n = get4byte(&pPage1->aData[36]);
       
  4120   put4byte(&pPage1->aData[36], n+1);
       
  4121 
       
  4122 #ifdef SQLITE_SECURE_DELETE
       
  4123   /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
       
  4124   ** always fully overwrite deleted information with zeros.
       
  4125   */
       
  4126   rc = sqlite3PagerWrite(pPage->pDbPage);
       
  4127   if( rc ) return rc;
       
  4128   memset(pPage->aData, 0, pPage->pBt->pageSize);
       
  4129 #endif
       
  4130 
       
  4131 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4132   /* If the database supports auto-vacuum, write an entry in the pointer-map
       
  4133   ** to indicate that the page is free.
       
  4134   */
       
  4135   if( pBt->autoVacuum ){
       
  4136     rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
       
  4137     if( rc ) return rc;
       
  4138   }
       
  4139 #endif
       
  4140 
       
  4141   if( n==0 ){
       
  4142     /* This is the first free page */
       
  4143     rc = sqlite3PagerWrite(pPage->pDbPage);
       
  4144     if( rc ) return rc;
       
  4145     memset(pPage->aData, 0, 8);
       
  4146     put4byte(&pPage1->aData[32], pPage->pgno);
       
  4147     TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
       
  4148   }else{
       
  4149     /* Other free pages already exist.  Retrive the first trunk page
       
  4150     ** of the freelist and find out how many leaves it has. */
       
  4151     MemPage *pTrunk;
       
  4152     rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
       
  4153     if( rc ) return rc;
       
  4154     k = get4byte(&pTrunk->aData[4]);
       
  4155     if( k>=pBt->usableSize/4 - 8 ){
       
  4156       /* The trunk is full.  Turn the page being freed into a new
       
  4157       ** trunk page with no leaves. */
       
  4158       rc = sqlite3PagerWrite(pPage->pDbPage);
       
  4159       if( rc==SQLITE_OK ){
       
  4160         put4byte(pPage->aData, pTrunk->pgno);
       
  4161         put4byte(&pPage->aData[4], 0);
       
  4162         put4byte(&pPage1->aData[32], pPage->pgno);
       
  4163         TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
       
  4164                 pPage->pgno, pTrunk->pgno));
       
  4165       }
       
  4166     }else if( k<0 ){
       
  4167       rc = SQLITE_CORRUPT;
       
  4168     }else{
       
  4169       /* Add the newly freed page as a leaf on the current trunk */
       
  4170       rc = sqlite3PagerWrite(pTrunk->pDbPage);
       
  4171       if( rc==SQLITE_OK ){
       
  4172         put4byte(&pTrunk->aData[4], k+1);
       
  4173         put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
       
  4174 #ifndef SQLITE_SECURE_DELETE
       
  4175         sqlite3PagerDontWrite(pPage->pDbPage);
       
  4176 #endif
       
  4177       }
       
  4178       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
       
  4179     }
       
  4180     releasePage(pTrunk);
       
  4181   }
       
  4182   return rc;
       
  4183 }
       
  4184 
       
  4185 /*
       
  4186 ** Free any overflow pages associated with the given Cell.
       
  4187 */
       
  4188 static int clearCell(MemPage *pPage, unsigned char *pCell){
       
  4189   BtShared *pBt = pPage->pBt;
       
  4190   CellInfo info;
       
  4191   Pgno ovflPgno;
       
  4192   int rc;
       
  4193   int nOvfl;
       
  4194   int ovflPageSize;
       
  4195 
       
  4196   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4197   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  4198   if( info.iOverflow==0 ){
       
  4199     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
       
  4200   }
       
  4201   ovflPgno = get4byte(&pCell[info.iOverflow]);
       
  4202   ovflPageSize = pBt->usableSize - 4;
       
  4203   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
       
  4204   assert( ovflPgno==0 || nOvfl>0 );
       
  4205   while( nOvfl-- ){
       
  4206     MemPage *pOvfl;
       
  4207     if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
       
  4208       return SQLITE_CORRUPT_BKPT;
       
  4209     }
       
  4210 
       
  4211     rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
       
  4212     if( rc ) return rc;
       
  4213     rc = freePage(pOvfl);
       
  4214     sqlite3PagerUnref(pOvfl->pDbPage);
       
  4215     if( rc ) return rc;
       
  4216   }
       
  4217   return SQLITE_OK;
       
  4218 }
       
  4219 
       
  4220 /*
       
  4221 ** Create the byte sequence used to represent a cell on page pPage
       
  4222 ** and write that byte sequence into pCell[].  Overflow pages are
       
  4223 ** allocated and filled in as necessary.  The calling procedure
       
  4224 ** is responsible for making sure sufficient space has been allocated
       
  4225 ** for pCell[].
       
  4226 **
       
  4227 ** Note that pCell does not necessary need to point to the pPage->aData
       
  4228 ** area.  pCell might point to some temporary storage.  The cell will
       
  4229 ** be constructed in this temporary area then copied into pPage->aData
       
  4230 ** later.
       
  4231 */
       
  4232 static int fillInCell(
       
  4233   MemPage *pPage,                /* The page that contains the cell */
       
  4234   unsigned char *pCell,          /* Complete text of the cell */
       
  4235   const void *pKey, i64 nKey,    /* The key */
       
  4236   const void *pData,int nData,   /* The data */
       
  4237   int nZero,                     /* Extra zero bytes to append to pData */
       
  4238   int *pnSize                    /* Write cell size here */
       
  4239 ){
       
  4240   int nPayload;
       
  4241   const u8 *pSrc;
       
  4242   int nSrc, n, rc;
       
  4243   int spaceLeft;
       
  4244   MemPage *pOvfl = 0;
       
  4245   MemPage *pToRelease = 0;
       
  4246   unsigned char *pPrior;
       
  4247   unsigned char *pPayload;
       
  4248   BtShared *pBt = pPage->pBt;
       
  4249   Pgno pgnoOvfl = 0;
       
  4250   int nHeader;
       
  4251   CellInfo info;
       
  4252 
       
  4253   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4254 
       
  4255   /* Fill in the header. */
       
  4256   nHeader = 0;
       
  4257   if( !pPage->leaf ){
       
  4258     nHeader += 4;
       
  4259   }
       
  4260   if( pPage->hasData ){
       
  4261     nHeader += putVarint(&pCell[nHeader], nData+nZero);
       
  4262   }else{
       
  4263     nData = nZero = 0;
       
  4264   }
       
  4265   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
       
  4266   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  4267   assert( info.nHeader==nHeader );
       
  4268   assert( info.nKey==nKey );
       
  4269   assert( info.nData==nData+nZero );
       
  4270   
       
  4271   /* Fill in the payload */
       
  4272   nPayload = nData + nZero;
       
  4273   if( pPage->intKey ){
       
  4274     pSrc = (const u8*)pData;
       
  4275     nSrc = nData;
       
  4276     nData = 0;
       
  4277   }else{
       
  4278     nPayload += nKey;
       
  4279     pSrc = (const u8*)pKey;
       
  4280     nSrc = nKey;
       
  4281   }
       
  4282   *pnSize = info.nSize;
       
  4283   spaceLeft = info.nLocal;
       
  4284   pPayload = &pCell[nHeader];
       
  4285   pPrior = &pCell[info.iOverflow];
       
  4286 
       
  4287   while( nPayload>0 ){
       
  4288     if( spaceLeft==0 ){
       
  4289       int isExact = 0;
       
  4290 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4291       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
       
  4292       if( pBt->autoVacuum ){
       
  4293         do{
       
  4294           pgnoOvfl++;
       
  4295         } while( 
       
  4296           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
       
  4297         );
       
  4298         if( pgnoOvfl>1 ){
       
  4299           /* isExact = 1; */
       
  4300         }
       
  4301       }
       
  4302 #endif
       
  4303       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact);
       
  4304 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4305       /* If the database supports auto-vacuum, and the second or subsequent
       
  4306       ** overflow page is being allocated, add an entry to the pointer-map
       
  4307       ** for that page now. 
       
  4308       **
       
  4309       ** If this is the first overflow page, then write a partial entry 
       
  4310       ** to the pointer-map. If we write nothing to this pointer-map slot,
       
  4311       ** then the optimistic overflow chain processing in clearCell()
       
  4312       ** may misinterpret the uninitialised values and delete the
       
  4313       ** wrong pages from the database.
       
  4314       */
       
  4315       if( pBt->autoVacuum && rc==SQLITE_OK ){
       
  4316         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
       
  4317         rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
       
  4318         if( rc ){
       
  4319           releasePage(pOvfl);
       
  4320         }
       
  4321       }
       
  4322 #endif
       
  4323       if( rc ){
       
  4324         releasePage(pToRelease);
       
  4325         return rc;
       
  4326       }
       
  4327       put4byte(pPrior, pgnoOvfl);
       
  4328       releasePage(pToRelease);
       
  4329       pToRelease = pOvfl;
       
  4330       pPrior = pOvfl->aData;
       
  4331       put4byte(pPrior, 0);
       
  4332       pPayload = &pOvfl->aData[4];
       
  4333       spaceLeft = pBt->usableSize - 4;
       
  4334     }
       
  4335     n = nPayload;
       
  4336     if( n>spaceLeft ) n = spaceLeft;
       
  4337     if( nSrc>0 ){
       
  4338       if( n>nSrc ) n = nSrc;
       
  4339       assert( pSrc );
       
  4340       memcpy(pPayload, pSrc, n);
       
  4341     }else{
       
  4342       memset(pPayload, 0, n);
       
  4343     }
       
  4344     nPayload -= n;
       
  4345     pPayload += n;
       
  4346     pSrc += n;
       
  4347     nSrc -= n;
       
  4348     spaceLeft -= n;
       
  4349     if( nSrc==0 ){
       
  4350       nSrc = nData;
       
  4351       pSrc = (u8*)pData;
       
  4352     }
       
  4353   }
       
  4354   releasePage(pToRelease);
       
  4355   return SQLITE_OK;
       
  4356 }
       
  4357 
       
  4358 /*
       
  4359 ** Change the MemPage.pParent pointer on the page whose number is
       
  4360 ** given in the second argument so that MemPage.pParent holds the
       
  4361 ** pointer in the third argument.
       
  4362 */
       
  4363 static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
       
  4364   MemPage *pThis;
       
  4365   DbPage *pDbPage;
       
  4366 
       
  4367   assert( sqlite3_mutex_held(pBt->mutex) );
       
  4368   assert( pNewParent!=0 );
       
  4369   if( pgno==0 ) return SQLITE_OK;
       
  4370   assert( pBt->pPager!=0 );
       
  4371   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
       
  4372   if( pDbPage ){
       
  4373     pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
       
  4374     if( pThis->isInit ){
       
  4375       assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
       
  4376       if( pThis->pParent!=pNewParent ){
       
  4377         if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
       
  4378         pThis->pParent = pNewParent;
       
  4379         sqlite3PagerRef(pNewParent->pDbPage);
       
  4380       }
       
  4381       pThis->idxParent = idx;
       
  4382     }
       
  4383     sqlite3PagerUnref(pDbPage);
       
  4384   }
       
  4385 
       
  4386 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4387   if( pBt->autoVacuum ){
       
  4388     return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
       
  4389   }
       
  4390 #endif
       
  4391   return SQLITE_OK;
       
  4392 }
       
  4393 
       
  4394 
       
  4395 
       
  4396 /*
       
  4397 ** Change the pParent pointer of all children of pPage to point back
       
  4398 ** to pPage.
       
  4399 **
       
  4400 ** In other words, for every child of pPage, invoke reparentPage()
       
  4401 ** to make sure that each child knows that pPage is its parent.
       
  4402 **
       
  4403 ** This routine gets called after you memcpy() one page into
       
  4404 ** another.
       
  4405 */
       
  4406 static int reparentChildPages(MemPage *pPage){
       
  4407   int i;
       
  4408   BtShared *pBt = pPage->pBt;
       
  4409   int rc = SQLITE_OK;
       
  4410 
       
  4411   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4412   if( pPage->leaf ) return SQLITE_OK;
       
  4413 
       
  4414   for(i=0; i<pPage->nCell; i++){
       
  4415     u8 *pCell = findCell(pPage, i);
       
  4416     if( !pPage->leaf ){
       
  4417       rc = reparentPage(pBt, get4byte(pCell), pPage, i);
       
  4418       if( rc!=SQLITE_OK ) return rc;
       
  4419     }
       
  4420   }
       
  4421   if( !pPage->leaf ){
       
  4422     rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), 
       
  4423        pPage, i);
       
  4424     pPage->idxShift = 0;
       
  4425   }
       
  4426   return rc;
       
  4427 }
       
  4428 
       
  4429 /*
       
  4430 ** Remove the i-th cell from pPage.  This routine effects pPage only.
       
  4431 ** The cell content is not freed or deallocated.  It is assumed that
       
  4432 ** the cell content has been copied someplace else.  This routine just
       
  4433 ** removes the reference to the cell from pPage.
       
  4434 **
       
  4435 ** "sz" must be the number of bytes in the cell.
       
  4436 */
       
  4437 static void dropCell(MemPage *pPage, int idx, int sz){
       
  4438   int i;          /* Loop counter */
       
  4439   int pc;         /* Offset to cell content of cell being deleted */
       
  4440   u8 *data;       /* pPage->aData */
       
  4441   u8 *ptr;        /* Used to move bytes around within data[] */
       
  4442 
       
  4443   assert( idx>=0 && idx<pPage->nCell );
       
  4444   assert( sz==cellSize(pPage, idx) );
       
  4445   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
  4446   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4447   data = pPage->aData;
       
  4448   ptr = &data[pPage->cellOffset + 2*idx];
       
  4449   pc = get2byte(ptr);
       
  4450   assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
       
  4451   freeSpace(pPage, pc, sz);
       
  4452   for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
       
  4453     ptr[0] = ptr[2];
       
  4454     ptr[1] = ptr[3];
       
  4455   }
       
  4456   pPage->nCell--;
       
  4457   put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
       
  4458   pPage->nFree += 2;
       
  4459   pPage->idxShift = 1;
       
  4460 }
       
  4461 
       
  4462 /*
       
  4463 ** Insert a new cell on pPage at cell index "i".  pCell points to the
       
  4464 ** content of the cell.
       
  4465 **
       
  4466 ** If the cell content will fit on the page, then put it there.  If it
       
  4467 ** will not fit, then make a copy of the cell content into pTemp if
       
  4468 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
       
  4469 ** in pPage->aOvfl[] and make it point to the cell content (either
       
  4470 ** in pTemp or the original pCell) and also record its index. 
       
  4471 ** Allocating a new entry in pPage->aCell[] implies that 
       
  4472 ** pPage->nOverflow is incremented.
       
  4473 **
       
  4474 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
       
  4475 ** cell. The caller will overwrite them after this function returns. If
       
  4476 ** nSkip is non-zero, then pCell may not point to an invalid memory location 
       
  4477 ** (but pCell+nSkip is always valid).
       
  4478 */
       
  4479 static int insertCell(
       
  4480   MemPage *pPage,   /* Page into which we are copying */
       
  4481   int i,            /* New cell becomes the i-th cell of the page */
       
  4482   u8 *pCell,        /* Content of the new cell */
       
  4483   int sz,           /* Bytes of content in pCell */
       
  4484   u8 *pTemp,        /* Temp storage space for pCell, if needed */
       
  4485   u8 nSkip          /* Do not write the first nSkip bytes of the cell */
       
  4486 ){
       
  4487   int idx;          /* Where to write new cell content in data[] */
       
  4488   int j;            /* Loop counter */
       
  4489   int top;          /* First byte of content for any cell in data[] */
       
  4490   int end;          /* First byte past the last cell pointer in data[] */
       
  4491   int ins;          /* Index in data[] where new cell pointer is inserted */
       
  4492   int hdr;          /* Offset into data[] of the page header */
       
  4493   int cellOffset;   /* Address of first cell pointer in data[] */
       
  4494   u8 *data;         /* The content of the whole page */
       
  4495   u8 *ptr;          /* Used for moving information around in data[] */
       
  4496 
       
  4497   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
       
  4498   assert( sz==cellSizePtr(pPage, pCell) );
       
  4499   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4500   if( pPage->nOverflow || sz+2>pPage->nFree ){
       
  4501     if( pTemp ){
       
  4502       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
       
  4503       pCell = pTemp;
       
  4504     }
       
  4505     j = pPage->nOverflow++;
       
  4506     assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
       
  4507     pPage->aOvfl[j].pCell = pCell;
       
  4508     pPage->aOvfl[j].idx = i;
       
  4509     pPage->nFree = 0;
       
  4510   }else{
       
  4511     int rc = sqlite3PagerWrite(pPage->pDbPage);
       
  4512     if( rc!=SQLITE_OK ){
       
  4513       return rc;
       
  4514     }
       
  4515     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
       
  4516     data = pPage->aData;
       
  4517     hdr = pPage->hdrOffset;
       
  4518     top = get2byte(&data[hdr+5]);
       
  4519     cellOffset = pPage->cellOffset;
       
  4520     end = cellOffset + 2*pPage->nCell + 2;
       
  4521     ins = cellOffset + 2*i;
       
  4522     if( end > top - sz ){
       
  4523       rc = defragmentPage(pPage);
       
  4524       if( rc!=SQLITE_OK ) return rc;
       
  4525       top = get2byte(&data[hdr+5]);
       
  4526       assert( end + sz <= top );
       
  4527     }
       
  4528     idx = allocateSpace(pPage, sz);
       
  4529     assert( idx>0 );
       
  4530     assert( end <= get2byte(&data[hdr+5]) );
       
  4531     pPage->nCell++;
       
  4532     pPage->nFree -= 2;
       
  4533     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
       
  4534     for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
       
  4535       ptr[0] = ptr[-2];
       
  4536       ptr[1] = ptr[-1];
       
  4537     }
       
  4538     put2byte(&data[ins], idx);
       
  4539     put2byte(&data[hdr+3], pPage->nCell);
       
  4540     pPage->idxShift = 1;
       
  4541 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4542     if( pPage->pBt->autoVacuum ){
       
  4543       /* The cell may contain a pointer to an overflow page. If so, write
       
  4544       ** the entry for the overflow page into the pointer map.
       
  4545       */
       
  4546       CellInfo info;
       
  4547       sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  4548       assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
       
  4549       if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
       
  4550         Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
       
  4551         rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
       
  4552         if( rc!=SQLITE_OK ) return rc;
       
  4553       }
       
  4554     }
       
  4555 #endif
       
  4556   }
       
  4557 
       
  4558   return SQLITE_OK;
       
  4559 }
       
  4560 
       
  4561 /*
       
  4562 ** Add a list of cells to a page.  The page should be initially empty.
       
  4563 ** The cells are guaranteed to fit on the page.
       
  4564 */
       
  4565 static void assemblePage(
       
  4566   MemPage *pPage,   /* The page to be assemblied */
       
  4567   int nCell,        /* The number of cells to add to this page */
       
  4568   u8 **apCell,      /* Pointers to cell bodies */
       
  4569   int *aSize        /* Sizes of the cells */
       
  4570 ){
       
  4571   int i;            /* Loop counter */
       
  4572   int totalSize;    /* Total size of all cells */
       
  4573   int hdr;          /* Index of page header */
       
  4574   int cellptr;      /* Address of next cell pointer */
       
  4575   int cellbody;     /* Address of next cell body */
       
  4576   u8 *data;         /* Data for the page */
       
  4577 
       
  4578   assert( pPage->nOverflow==0 );
       
  4579   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4580   totalSize = 0;
       
  4581   for(i=0; i<nCell; i++){
       
  4582     totalSize += aSize[i];
       
  4583   }
       
  4584   assert( totalSize+2*nCell<=pPage->nFree );
       
  4585   assert( pPage->nCell==0 );
       
  4586   cellptr = pPage->cellOffset;
       
  4587   data = pPage->aData;
       
  4588   hdr = pPage->hdrOffset;
       
  4589   put2byte(&data[hdr+3], nCell);
       
  4590   if( nCell ){
       
  4591     cellbody = allocateSpace(pPage, totalSize);
       
  4592     assert( cellbody>0 );
       
  4593     assert( pPage->nFree >= 2*nCell );
       
  4594     pPage->nFree -= 2*nCell;
       
  4595     for(i=0; i<nCell; i++){
       
  4596       put2byte(&data[cellptr], cellbody);
       
  4597       memcpy(&data[cellbody], apCell[i], aSize[i]);
       
  4598       cellptr += 2;
       
  4599       cellbody += aSize[i];
       
  4600     }
       
  4601     assert( cellbody==pPage->pBt->usableSize );
       
  4602   }
       
  4603   pPage->nCell = nCell;
       
  4604 }
       
  4605 
       
  4606 /*
       
  4607 ** The following parameters determine how many adjacent pages get involved
       
  4608 ** in a balancing operation.  NN is the number of neighbors on either side
       
  4609 ** of the page that participate in the balancing operation.  NB is the
       
  4610 ** total number of pages that participate, including the target page and
       
  4611 ** NN neighbors on either side.
       
  4612 **
       
  4613 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
       
  4614 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
       
  4615 ** in exchange for a larger degradation in INSERT and UPDATE performance.
       
  4616 ** The value of NN appears to give the best results overall.
       
  4617 */
       
  4618 #define NN 1             /* Number of neighbors on either side of pPage */
       
  4619 #define NB (NN*2+1)      /* Total pages involved in the balance */
       
  4620 
       
  4621 /* Forward reference */
       
  4622 static int balance(MemPage*, int);
       
  4623 
       
  4624 #ifndef SQLITE_OMIT_QUICKBALANCE
       
  4625 /*
       
  4626 ** This version of balance() handles the common special case where
       
  4627 ** a new entry is being inserted on the extreme right-end of the
       
  4628 ** tree, in other words, when the new entry will become the largest
       
  4629 ** entry in the tree.
       
  4630 **
       
  4631 ** Instead of trying balance the 3 right-most leaf pages, just add
       
  4632 ** a new page to the right-hand side and put the one new entry in
       
  4633 ** that page.  This leaves the right side of the tree somewhat
       
  4634 ** unbalanced.  But odds are that we will be inserting new entries
       
  4635 ** at the end soon afterwards so the nearly empty page will quickly
       
  4636 ** fill up.  On average.
       
  4637 **
       
  4638 ** pPage is the leaf page which is the right-most page in the tree.
       
  4639 ** pParent is its parent.  pPage must have a single overflow entry
       
  4640 ** which is also the right-most entry on the page.
       
  4641 */
       
  4642 static int balance_quick(MemPage *pPage, MemPage *pParent){
       
  4643   int rc;
       
  4644   MemPage *pNew;
       
  4645   Pgno pgnoNew;
       
  4646   u8 *pCell;
       
  4647   int szCell;
       
  4648   CellInfo info;
       
  4649   BtShared *pBt = pPage->pBt;
       
  4650   int parentIdx = pParent->nCell;   /* pParent new divider cell index */
       
  4651   int parentSize;                   /* Size of new divider cell */
       
  4652   u8 parentCell[64];                /* Space for the new divider cell */
       
  4653 
       
  4654   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4655 
       
  4656   /* Allocate a new page. Insert the overflow cell from pPage
       
  4657   ** into it. Then remove the overflow cell from pPage.
       
  4658   */
       
  4659   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
       
  4660   if( rc!=SQLITE_OK ){
       
  4661     return rc;
       
  4662   }
       
  4663   pCell = pPage->aOvfl[0].pCell;
       
  4664   szCell = cellSizePtr(pPage, pCell);
       
  4665   zeroPage(pNew, pPage->aData[0]);
       
  4666   assemblePage(pNew, 1, &pCell, &szCell);
       
  4667   pPage->nOverflow = 0;
       
  4668 
       
  4669   /* Set the parent of the newly allocated page to pParent. */
       
  4670   pNew->pParent = pParent;
       
  4671   sqlite3PagerRef(pParent->pDbPage);
       
  4672 
       
  4673   /* pPage is currently the right-child of pParent. Change this
       
  4674   ** so that the right-child is the new page allocated above and
       
  4675   ** pPage is the next-to-right child. 
       
  4676   */
       
  4677   assert( pPage->nCell>0 );
       
  4678   pCell = findCell(pPage, pPage->nCell-1);
       
  4679   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  4680   rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
       
  4681   if( rc!=SQLITE_OK ){
       
  4682     return rc;
       
  4683   }
       
  4684   assert( parentSize<64 );
       
  4685   rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
       
  4686   if( rc!=SQLITE_OK ){
       
  4687     return rc;
       
  4688   }
       
  4689   put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
       
  4690   put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
       
  4691 
       
  4692 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4693   /* If this is an auto-vacuum database, update the pointer map
       
  4694   ** with entries for the new page, and any pointer from the 
       
  4695   ** cell on the page to an overflow page.
       
  4696   */
       
  4697   if( pBt->autoVacuum ){
       
  4698     rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
       
  4699     if( rc==SQLITE_OK ){
       
  4700       rc = ptrmapPutOvfl(pNew, 0);
       
  4701     }
       
  4702     if( rc!=SQLITE_OK ){
       
  4703       releasePage(pNew);
       
  4704       return rc;
       
  4705     }
       
  4706   }
       
  4707 #endif
       
  4708 
       
  4709   /* Release the reference to the new page and balance the parent page,
       
  4710   ** in case the divider cell inserted caused it to become overfull.
       
  4711   */
       
  4712   releasePage(pNew);
       
  4713   return balance(pParent, 0);
       
  4714 }
       
  4715 #endif /* SQLITE_OMIT_QUICKBALANCE */
       
  4716 
       
  4717 /*
       
  4718 ** This routine redistributes Cells on pPage and up to NN*2 siblings
       
  4719 ** of pPage so that all pages have about the same amount of free space.
       
  4720 ** Usually NN siblings on either side of pPage is used in the balancing,
       
  4721 ** though more siblings might come from one side if pPage is the first
       
  4722 ** or last child of its parent.  If pPage has fewer than 2*NN siblings
       
  4723 ** (something which can only happen if pPage is the root page or a 
       
  4724 ** child of root) then all available siblings participate in the balancing.
       
  4725 **
       
  4726 ** The number of siblings of pPage might be increased or decreased by one or
       
  4727 ** two in an effort to keep pages nearly full but not over full. The root page
       
  4728 ** is special and is allowed to be nearly empty. If pPage is 
       
  4729 ** the root page, then the depth of the tree might be increased
       
  4730 ** or decreased by one, as necessary, to keep the root page from being
       
  4731 ** overfull or completely empty.
       
  4732 **
       
  4733 ** Note that when this routine is called, some of the Cells on pPage
       
  4734 ** might not actually be stored in pPage->aData[].  This can happen
       
  4735 ** if the page is overfull.  Part of the job of this routine is to
       
  4736 ** make sure all Cells for pPage once again fit in pPage->aData[].
       
  4737 **
       
  4738 ** In the course of balancing the siblings of pPage, the parent of pPage
       
  4739 ** might become overfull or underfull.  If that happens, then this routine
       
  4740 ** is called recursively on the parent.
       
  4741 **
       
  4742 ** If this routine fails for any reason, it might leave the database
       
  4743 ** in a corrupted state.  So if this routine fails, the database should
       
  4744 ** be rolled back.
       
  4745 */
       
  4746 static int balance_nonroot(MemPage *pPage){
       
  4747   MemPage *pParent;            /* The parent of pPage */
       
  4748   BtShared *pBt;               /* The whole database */
       
  4749   int nCell = 0;               /* Number of cells in apCell[] */
       
  4750   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
       
  4751   int nOld;                    /* Number of pages in apOld[] */
       
  4752   int nNew;                    /* Number of pages in apNew[] */
       
  4753   int nDiv;                    /* Number of cells in apDiv[] */
       
  4754   int i, j, k;                 /* Loop counters */
       
  4755   int idx;                     /* Index of pPage in pParent->aCell[] */
       
  4756   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
       
  4757   int rc;                      /* The return code */
       
  4758   int leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
       
  4759   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
       
  4760   int usableSpace;             /* Bytes in pPage beyond the header */
       
  4761   int pageFlags;               /* Value of pPage->aData[0] */
       
  4762   int subtotal;                /* Subtotal of bytes in cells on one page */
       
  4763   int iSpace = 0;              /* First unused byte of aSpace[] */
       
  4764   MemPage *apOld[NB];          /* pPage and up to two siblings */
       
  4765   Pgno pgnoOld[NB];            /* Page numbers for each page in apOld[] */
       
  4766   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
       
  4767   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
       
  4768   Pgno pgnoNew[NB+2];          /* Page numbers for each page in apNew[] */
       
  4769   u8 *apDiv[NB];               /* Divider cells in pParent */
       
  4770   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
       
  4771   int szNew[NB+2];             /* Combined size of cells place on i-th page */
       
  4772   u8 **apCell = 0;             /* All cells begin balanced */
       
  4773   int *szCell;                 /* Local size of all cells in apCell[] */
       
  4774   u8 *aCopy[NB];               /* Space for holding data of apCopy[] */
       
  4775   u8 *aSpace;                  /* Space to hold copies of dividers cells */
       
  4776 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4777   u8 *aFrom = 0;
       
  4778 #endif
       
  4779 
       
  4780   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  4781 
       
  4782   /* 
       
  4783   ** Find the parent page.
       
  4784   */
       
  4785   assert( pPage->isInit );
       
  4786   assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
       
  4787   pBt = pPage->pBt;
       
  4788   pParent = pPage->pParent;
       
  4789   assert( pParent );
       
  4790   if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
       
  4791     return rc;
       
  4792   }
       
  4793   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
       
  4794 
       
  4795 #ifndef SQLITE_OMIT_QUICKBALANCE
       
  4796   /*
       
  4797   ** A special case:  If a new entry has just been inserted into a
       
  4798   ** table (that is, a btree with integer keys and all data at the leaves)
       
  4799   ** and the new entry is the right-most entry in the tree (it has the
       
  4800   ** largest key) then use the special balance_quick() routine for
       
  4801   ** balancing.  balance_quick() is much faster and results in a tighter
       
  4802   ** packing of data in the common case.
       
  4803   */
       
  4804   if( pPage->leaf &&
       
  4805       pPage->intKey &&
       
  4806       pPage->leafData &&
       
  4807       pPage->nOverflow==1 &&
       
  4808       pPage->aOvfl[0].idx==pPage->nCell &&
       
  4809       pPage->pParent->pgno!=1 &&
       
  4810       get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
       
  4811   ){
       
  4812     /*
       
  4813     ** TODO: Check the siblings to the left of pPage. It may be that
       
  4814     ** they are not full and no new page is required.
       
  4815     */
       
  4816     return balance_quick(pPage, pParent);
       
  4817   }
       
  4818 #endif
       
  4819 
       
  4820   if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
       
  4821     return rc;
       
  4822   }
       
  4823 
       
  4824   /*
       
  4825   ** Find the cell in the parent page whose left child points back
       
  4826   ** to pPage.  The "idx" variable is the index of that cell.  If pPage
       
  4827   ** is the rightmost child of pParent then set idx to pParent->nCell 
       
  4828   */
       
  4829   if( pParent->idxShift ){
       
  4830     Pgno pgno;
       
  4831     pgno = pPage->pgno;
       
  4832     assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
       
  4833     for(idx=0; idx<pParent->nCell; idx++){
       
  4834       if( get4byte(findCell(pParent, idx))==pgno ){
       
  4835         break;
       
  4836       }
       
  4837     }
       
  4838     assert( idx<pParent->nCell
       
  4839              || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
       
  4840   }else{
       
  4841     idx = pPage->idxParent;
       
  4842   }
       
  4843 
       
  4844   /*
       
  4845   ** Initialize variables so that it will be safe to jump
       
  4846   ** directly to balance_cleanup at any moment.
       
  4847   */
       
  4848   nOld = nNew = 0;
       
  4849   sqlite3PagerRef(pParent->pDbPage);
       
  4850 
       
  4851   /*
       
  4852   ** Find sibling pages to pPage and the cells in pParent that divide
       
  4853   ** the siblings.  An attempt is made to find NN siblings on either
       
  4854   ** side of pPage.  More siblings are taken from one side, however, if
       
  4855   ** pPage there are fewer than NN siblings on the other side.  If pParent
       
  4856   ** has NB or fewer children then all children of pParent are taken.
       
  4857   */
       
  4858   nxDiv = idx - NN;
       
  4859   if( nxDiv + NB > pParent->nCell ){
       
  4860     nxDiv = pParent->nCell - NB + 1;
       
  4861   }
       
  4862   if( nxDiv<0 ){
       
  4863     nxDiv = 0;
       
  4864   }
       
  4865   nDiv = 0;
       
  4866   for(i=0, k=nxDiv; i<NB; i++, k++){
       
  4867     if( k<pParent->nCell ){
       
  4868       apDiv[i] = findCell(pParent, k);
       
  4869       nDiv++;
       
  4870       assert( !pParent->leaf );
       
  4871       pgnoOld[i] = get4byte(apDiv[i]);
       
  4872     }else if( k==pParent->nCell ){
       
  4873       pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
       
  4874     }else{
       
  4875       break;
       
  4876     }
       
  4877     rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
       
  4878     if( rc ) goto balance_cleanup;
       
  4879     apOld[i]->idxParent = k;
       
  4880     apCopy[i] = 0;
       
  4881     assert( i==nOld );
       
  4882     nOld++;
       
  4883     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
       
  4884   }
       
  4885 
       
  4886   /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
       
  4887   ** alignment */
       
  4888   nMaxCells = (nMaxCells + 1)&~1;
       
  4889 
       
  4890   /*
       
  4891   ** Allocate space for memory structures
       
  4892   */
       
  4893   apCell = (u8**)sqlite3_malloc( 
       
  4894        nMaxCells*sizeof(u8*)                           /* apCell */
       
  4895      + nMaxCells*sizeof(int)                           /* szCell */
       
  4896      + ROUND8(sizeof(MemPage))*NB                      /* aCopy */
       
  4897      + pBt->pageSize*(5+NB)                            /* aSpace */
       
  4898      + (ISAUTOVACUUM ? nMaxCells : 0)                  /* aFrom */
       
  4899   );
       
  4900   if( apCell==0 ){
       
  4901     rc = SQLITE_NOMEM;
       
  4902     goto balance_cleanup;
       
  4903   }
       
  4904   szCell = (int*)&apCell[nMaxCells];
       
  4905   aCopy[0] = (u8*)&szCell[nMaxCells];
       
  4906   assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
       
  4907   for(i=1; i<NB; i++){
       
  4908     aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
       
  4909     assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
       
  4910   }
       
  4911   aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
       
  4912   assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
       
  4913 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4914   if( pBt->autoVacuum ){
       
  4915     aFrom = &aSpace[5*pBt->pageSize];
       
  4916   }
       
  4917 #endif
       
  4918   
       
  4919   /*
       
  4920   ** Make copies of the content of pPage and its siblings into aOld[].
       
  4921   ** The rest of this function will use data from the copies rather
       
  4922   ** that the original pages since the original pages will be in the
       
  4923   ** process of being overwritten.
       
  4924   */
       
  4925   for(i=0; i<nOld; i++){
       
  4926     MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
       
  4927     memcpy(p, apOld[i], sizeof(MemPage));
       
  4928     p->aData = (u8*)(void*)&p[1];
       
  4929     memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
       
  4930   }
       
  4931 
       
  4932   /*
       
  4933   ** Load pointers to all cells on sibling pages and the divider cells
       
  4934   ** into the local apCell[] array.  Make copies of the divider cells
       
  4935   ** into space obtained form aSpace[] and remove the the divider Cells
       
  4936   ** from pParent.
       
  4937   **
       
  4938   ** If the siblings are on leaf pages, then the child pointers of the
       
  4939   ** divider cells are stripped from the cells before they are copied
       
  4940   ** into aSpace[].  In this way, all cells in apCell[] are without
       
  4941   ** child pointers.  If siblings are not leaves, then all cell in
       
  4942   ** apCell[] include child pointers.  Either way, all cells in apCell[]
       
  4943   ** are alike.
       
  4944   **
       
  4945   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
       
  4946   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
       
  4947   */
       
  4948   nCell = 0;
       
  4949   leafCorrection = pPage->leaf*4;
       
  4950   leafData = pPage->leafData && pPage->leaf;
       
  4951   for(i=0; i<nOld; i++){
       
  4952     MemPage *pOld = apCopy[i];
       
  4953     int limit = pOld->nCell+pOld->nOverflow;
       
  4954     for(j=0; j<limit; j++){
       
  4955       assert( nCell<nMaxCells );
       
  4956       apCell[nCell] = findOverflowCell(pOld, j);
       
  4957       szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
       
  4958 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4959       if( pBt->autoVacuum ){
       
  4960         int a;
       
  4961         aFrom[nCell] = i;
       
  4962         for(a=0; a<pOld->nOverflow; a++){
       
  4963           if( pOld->aOvfl[a].pCell==apCell[nCell] ){
       
  4964             aFrom[nCell] = 0xFF;
       
  4965             break;
       
  4966           }
       
  4967         }
       
  4968       }
       
  4969 #endif
       
  4970       nCell++;
       
  4971     }
       
  4972     if( i<nOld-1 ){
       
  4973       int sz = cellSizePtr(pParent, apDiv[i]);
       
  4974       if( leafData ){
       
  4975         /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
       
  4976         ** are duplicates of keys on the child pages.  We need to remove
       
  4977         ** the divider cells from pParent, but the dividers cells are not
       
  4978         ** added to apCell[] because they are duplicates of child cells.
       
  4979         */
       
  4980         dropCell(pParent, nxDiv, sz);
       
  4981       }else{
       
  4982         u8 *pTemp;
       
  4983         assert( nCell<nMaxCells );
       
  4984         szCell[nCell] = sz;
       
  4985         pTemp = &aSpace[iSpace];
       
  4986         iSpace += sz;
       
  4987         assert( iSpace<=pBt->pageSize*5 );
       
  4988         memcpy(pTemp, apDiv[i], sz);
       
  4989         apCell[nCell] = pTemp+leafCorrection;
       
  4990 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  4991         if( pBt->autoVacuum ){
       
  4992           aFrom[nCell] = 0xFF;
       
  4993         }
       
  4994 #endif
       
  4995         dropCell(pParent, nxDiv, sz);
       
  4996         szCell[nCell] -= leafCorrection;
       
  4997         assert( get4byte(pTemp)==pgnoOld[i] );
       
  4998         if( !pOld->leaf ){
       
  4999           assert( leafCorrection==0 );
       
  5000           /* The right pointer of the child page pOld becomes the left
       
  5001           ** pointer of the divider cell */
       
  5002           memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
       
  5003         }else{
       
  5004           assert( leafCorrection==4 );
       
  5005           if( szCell[nCell]<4 ){
       
  5006             /* Do not allow any cells smaller than 4 bytes. */
       
  5007             szCell[nCell] = 4;
       
  5008           }
       
  5009         }
       
  5010         nCell++;
       
  5011       }
       
  5012     }
       
  5013   }
       
  5014 
       
  5015   /*
       
  5016   ** Figure out the number of pages needed to hold all nCell cells.
       
  5017   ** Store this number in "k".  Also compute szNew[] which is the total
       
  5018   ** size of all cells on the i-th page and cntNew[] which is the index
       
  5019   ** in apCell[] of the cell that divides page i from page i+1.  
       
  5020   ** cntNew[k] should equal nCell.
       
  5021   **
       
  5022   ** Values computed by this block:
       
  5023   **
       
  5024   **           k: The total number of sibling pages
       
  5025   **    szNew[i]: Spaced used on the i-th sibling page.
       
  5026   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
       
  5027   **              the right of the i-th sibling page.
       
  5028   ** usableSpace: Number of bytes of space available on each sibling.
       
  5029   ** 
       
  5030   */
       
  5031   usableSpace = pBt->usableSize - 12 + leafCorrection;
       
  5032   for(subtotal=k=i=0; i<nCell; i++){
       
  5033     assert( i<nMaxCells );
       
  5034     subtotal += szCell[i] + 2;
       
  5035     if( subtotal > usableSpace ){
       
  5036       szNew[k] = subtotal - szCell[i];
       
  5037       cntNew[k] = i;
       
  5038       if( leafData ){ i--; }
       
  5039       subtotal = 0;
       
  5040       k++;
       
  5041     }
       
  5042   }
       
  5043   szNew[k] = subtotal;
       
  5044   cntNew[k] = nCell;
       
  5045   k++;
       
  5046 
       
  5047   /*
       
  5048   ** The packing computed by the previous block is biased toward the siblings
       
  5049   ** on the left side.  The left siblings are always nearly full, while the
       
  5050   ** right-most sibling might be nearly empty.  This block of code attempts
       
  5051   ** to adjust the packing of siblings to get a better balance.
       
  5052   **
       
  5053   ** This adjustment is more than an optimization.  The packing above might
       
  5054   ** be so out of balance as to be illegal.  For example, the right-most
       
  5055   ** sibling might be completely empty.  This adjustment is not optional.
       
  5056   */
       
  5057   for(i=k-1; i>0; i--){
       
  5058     int szRight = szNew[i];  /* Size of sibling on the right */
       
  5059     int szLeft = szNew[i-1]; /* Size of sibling on the left */
       
  5060     int r;              /* Index of right-most cell in left sibling */
       
  5061     int d;              /* Index of first cell to the left of right sibling */
       
  5062 
       
  5063     r = cntNew[i-1] - 1;
       
  5064     d = r + 1 - leafData;
       
  5065     assert( d<nMaxCells );
       
  5066     assert( r<nMaxCells );
       
  5067     while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
       
  5068       szRight += szCell[d] + 2;
       
  5069       szLeft -= szCell[r] + 2;
       
  5070       cntNew[i-1]--;
       
  5071       r = cntNew[i-1] - 1;
       
  5072       d = r + 1 - leafData;
       
  5073     }
       
  5074     szNew[i] = szRight;
       
  5075     szNew[i-1] = szLeft;
       
  5076   }
       
  5077 
       
  5078   /* Either we found one or more cells (cntnew[0])>0) or we are the
       
  5079   ** a virtual root page.  A virtual root page is when the real root
       
  5080   ** page is page 1 and we are the only child of that page.
       
  5081   */
       
  5082   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
       
  5083 
       
  5084   /*
       
  5085   ** Allocate k new pages.  Reuse old pages where possible.
       
  5086   */
       
  5087   assert( pPage->pgno>1 );
       
  5088   pageFlags = pPage->aData[0];
       
  5089   for(i=0; i<k; i++){
       
  5090     MemPage *pNew;
       
  5091     if( i<nOld ){
       
  5092       pNew = apNew[i] = apOld[i];
       
  5093       pgnoNew[i] = pgnoOld[i];
       
  5094       apOld[i] = 0;
       
  5095       rc = sqlite3PagerWrite(pNew->pDbPage);
       
  5096       nNew++;
       
  5097       if( rc ) goto balance_cleanup;
       
  5098     }else{
       
  5099       assert( i>0 );
       
  5100       rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
       
  5101       if( rc ) goto balance_cleanup;
       
  5102       apNew[i] = pNew;
       
  5103       nNew++;
       
  5104     }
       
  5105     zeroPage(pNew, pageFlags);
       
  5106   }
       
  5107 
       
  5108   /* Free any old pages that were not reused as new pages.
       
  5109   */
       
  5110   while( i<nOld ){
       
  5111     rc = freePage(apOld[i]);
       
  5112     if( rc ) goto balance_cleanup;
       
  5113     releasePage(apOld[i]);
       
  5114     apOld[i] = 0;
       
  5115     i++;
       
  5116   }
       
  5117 
       
  5118   /*
       
  5119   ** Put the new pages in accending order.  This helps to
       
  5120   ** keep entries in the disk file in order so that a scan
       
  5121   ** of the table is a linear scan through the file.  That
       
  5122   ** in turn helps the operating system to deliver pages
       
  5123   ** from the disk more rapidly.
       
  5124   **
       
  5125   ** An O(n^2) insertion sort algorithm is used, but since
       
  5126   ** n is never more than NB (a small constant), that should
       
  5127   ** not be a problem.
       
  5128   **
       
  5129   ** When NB==3, this one optimization makes the database
       
  5130   ** about 25% faster for large insertions and deletions.
       
  5131   */
       
  5132   for(i=0; i<k-1; i++){
       
  5133     int minV = pgnoNew[i];
       
  5134     int minI = i;
       
  5135     for(j=i+1; j<k; j++){
       
  5136       if( pgnoNew[j]<(unsigned)minV ){
       
  5137         minI = j;
       
  5138         minV = pgnoNew[j];
       
  5139       }
       
  5140     }
       
  5141     if( minI>i ){
       
  5142       int t;
       
  5143       MemPage *pT;
       
  5144       t = pgnoNew[i];
       
  5145       pT = apNew[i];
       
  5146       pgnoNew[i] = pgnoNew[minI];
       
  5147       apNew[i] = apNew[minI];
       
  5148       pgnoNew[minI] = t;
       
  5149       apNew[minI] = pT;
       
  5150     }
       
  5151   }
       
  5152   TRACE(("BALANCE: old: %d %d %d  new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
       
  5153     pgnoOld[0], 
       
  5154     nOld>=2 ? pgnoOld[1] : 0,
       
  5155     nOld>=3 ? pgnoOld[2] : 0,
       
  5156     pgnoNew[0], szNew[0],
       
  5157     nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
       
  5158     nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
       
  5159     nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
       
  5160     nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
       
  5161 
       
  5162   /*
       
  5163   ** Evenly distribute the data in apCell[] across the new pages.
       
  5164   ** Insert divider cells into pParent as necessary.
       
  5165   */
       
  5166   j = 0;
       
  5167   for(i=0; i<nNew; i++){
       
  5168     /* Assemble the new sibling page. */
       
  5169     MemPage *pNew = apNew[i];
       
  5170     assert( j<nMaxCells );
       
  5171     assert( pNew->pgno==pgnoNew[i] );
       
  5172     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
       
  5173     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
       
  5174     assert( pNew->nOverflow==0 );
       
  5175 
       
  5176 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  5177     /* If this is an auto-vacuum database, update the pointer map entries
       
  5178     ** that point to the siblings that were rearranged. These can be: left
       
  5179     ** children of cells, the right-child of the page, or overflow pages
       
  5180     ** pointed to by cells.
       
  5181     */
       
  5182     if( pBt->autoVacuum ){
       
  5183       for(k=j; k<cntNew[i]; k++){
       
  5184         assert( k<nMaxCells );
       
  5185         if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
       
  5186           rc = ptrmapPutOvfl(pNew, k-j);
       
  5187           if( rc!=SQLITE_OK ){
       
  5188             goto balance_cleanup;
       
  5189           }
       
  5190         }
       
  5191       }
       
  5192     }
       
  5193 #endif
       
  5194 
       
  5195     j = cntNew[i];
       
  5196 
       
  5197     /* If the sibling page assembled above was not the right-most sibling,
       
  5198     ** insert a divider cell into the parent page.
       
  5199     */
       
  5200     if( i<nNew-1 && j<nCell ){
       
  5201       u8 *pCell;
       
  5202       u8 *pTemp;
       
  5203       int sz;
       
  5204 
       
  5205       assert( j<nMaxCells );
       
  5206       pCell = apCell[j];
       
  5207       sz = szCell[j] + leafCorrection;
       
  5208       if( !pNew->leaf ){
       
  5209         memcpy(&pNew->aData[8], pCell, 4);
       
  5210         pTemp = 0;
       
  5211       }else if( leafData ){
       
  5212         /* If the tree is a leaf-data tree, and the siblings are leaves, 
       
  5213         ** then there is no divider cell in apCell[]. Instead, the divider 
       
  5214         ** cell consists of the integer key for the right-most cell of 
       
  5215         ** the sibling-page assembled above only.
       
  5216         */
       
  5217         CellInfo info;
       
  5218         j--;
       
  5219         sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
       
  5220         pCell = &aSpace[iSpace];
       
  5221         fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
       
  5222         iSpace += sz;
       
  5223         assert( iSpace<=pBt->pageSize*5 );
       
  5224         pTemp = 0;
       
  5225       }else{
       
  5226         pCell -= 4;
       
  5227         pTemp = &aSpace[iSpace];
       
  5228         iSpace += sz;
       
  5229         assert( iSpace<=pBt->pageSize*5 );
       
  5230         /* Obscure case for non-leaf-data trees: If the cell at pCell was
       
  5231         ** previously stored on a leaf node, and its reported size was 4
       
  5232         ** bytes, then it may actually be smaller than this 
       
  5233         ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
       
  5234         ** any cell). But it is important to pass the correct size to 
       
  5235         ** insertCell(), so reparse the cell now.
       
  5236         **
       
  5237         ** Note that this can never happen in an SQLite data file, as all
       
  5238         ** cells are at least 4 bytes. It only happens in b-trees used
       
  5239         ** to evaluate "IN (SELECT ...)" and similar clauses.
       
  5240         */
       
  5241         if( szCell[j]==4 ){
       
  5242           assert(leafCorrection==4);
       
  5243           sz = cellSizePtr(pParent, pCell);
       
  5244         }
       
  5245       }
       
  5246       rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
       
  5247       if( rc!=SQLITE_OK ) goto balance_cleanup;
       
  5248       put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
       
  5249 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  5250       /* If this is an auto-vacuum database, and not a leaf-data tree,
       
  5251       ** then update the pointer map with an entry for the overflow page
       
  5252       ** that the cell just inserted points to (if any).
       
  5253       */
       
  5254       if( pBt->autoVacuum && !leafData ){
       
  5255         rc = ptrmapPutOvfl(pParent, nxDiv);
       
  5256         if( rc!=SQLITE_OK ){
       
  5257           goto balance_cleanup;
       
  5258         }
       
  5259       }
       
  5260 #endif
       
  5261       j++;
       
  5262       nxDiv++;
       
  5263     }
       
  5264   }
       
  5265   assert( j==nCell );
       
  5266   assert( nOld>0 );
       
  5267   assert( nNew>0 );
       
  5268   if( (pageFlags & PTF_LEAF)==0 ){
       
  5269     memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
       
  5270   }
       
  5271   if( nxDiv==pParent->nCell+pParent->nOverflow ){
       
  5272     /* Right-most sibling is the right-most child of pParent */
       
  5273     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
       
  5274   }else{
       
  5275     /* Right-most sibling is the left child of the first entry in pParent
       
  5276     ** past the right-most divider entry */
       
  5277     put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
       
  5278   }
       
  5279 
       
  5280   /*
       
  5281   ** Reparent children of all cells.
       
  5282   */
       
  5283   for(i=0; i<nNew; i++){
       
  5284     rc = reparentChildPages(apNew[i]);
       
  5285     if( rc!=SQLITE_OK ) goto balance_cleanup;
       
  5286   }
       
  5287   rc = reparentChildPages(pParent);
       
  5288   if( rc!=SQLITE_OK ) goto balance_cleanup;
       
  5289 
       
  5290   /*
       
  5291   ** Balance the parent page.  Note that the current page (pPage) might
       
  5292   ** have been added to the freelist so it might no longer be initialized.
       
  5293   ** But the parent page will always be initialized.
       
  5294   */
       
  5295   assert( pParent->isInit );
       
  5296   rc = balance(pParent, 0);
       
  5297   
       
  5298   /*
       
  5299   ** Cleanup before returning.
       
  5300   */
       
  5301 balance_cleanup:
       
  5302   sqlite3_free(apCell);
       
  5303   for(i=0; i<nOld; i++){
       
  5304     releasePage(apOld[i]);
       
  5305   }
       
  5306   for(i=0; i<nNew; i++){
       
  5307     releasePage(apNew[i]);
       
  5308   }
       
  5309   releasePage(pParent);
       
  5310   TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
       
  5311           pPage->pgno, nOld, nNew, nCell));
       
  5312   return rc;
       
  5313 }
       
  5314 
       
  5315 /*
       
  5316 ** This routine is called for the root page of a btree when the root
       
  5317 ** page contains no cells.  This is an opportunity to make the tree
       
  5318 ** shallower by one level.
       
  5319 */
       
  5320 static int balance_shallower(MemPage *pPage){
       
  5321   MemPage *pChild;             /* The only child page of pPage */
       
  5322   Pgno pgnoChild;              /* Page number for pChild */
       
  5323   int rc = SQLITE_OK;          /* Return code from subprocedures */
       
  5324   BtShared *pBt;                  /* The main BTree structure */
       
  5325   int mxCellPerPage;           /* Maximum number of cells per page */
       
  5326   u8 **apCell;                 /* All cells from pages being balanced */
       
  5327   int *szCell;                 /* Local size of all cells */
       
  5328 
       
  5329   assert( pPage->pParent==0 );
       
  5330   assert( pPage->nCell==0 );
       
  5331   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  5332   pBt = pPage->pBt;
       
  5333   mxCellPerPage = MX_CELL(pBt);
       
  5334   apCell = (u8**)sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
       
  5335   if( apCell==0 ) return SQLITE_NOMEM;
       
  5336   szCell = (int*)&apCell[mxCellPerPage];
       
  5337   if( pPage->leaf ){
       
  5338     /* The table is completely empty */
       
  5339     TRACE(("BALANCE: empty table %d\n", pPage->pgno));
       
  5340   }else{
       
  5341     /* The root page is empty but has one child.  Transfer the
       
  5342     ** information from that one child into the root page if it 
       
  5343     ** will fit.  This reduces the depth of the tree by one.
       
  5344     **
       
  5345     ** If the root page is page 1, it has less space available than
       
  5346     ** its child (due to the 100 byte header that occurs at the beginning
       
  5347     ** of the database fle), so it might not be able to hold all of the 
       
  5348     ** information currently contained in the child.  If this is the 
       
  5349     ** case, then do not do the transfer.  Leave page 1 empty except
       
  5350     ** for the right-pointer to the child page.  The child page becomes
       
  5351     ** the virtual root of the tree.
       
  5352     */
       
  5353     pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
  5354     assert( pgnoChild>0 );
       
  5355     assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
       
  5356     rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
       
  5357     if( rc ) goto end_shallow_balance;
       
  5358     if( pPage->pgno==1 ){
       
  5359       rc = sqlite3BtreeInitPage(pChild, pPage);
       
  5360       if( rc ) goto end_shallow_balance;
       
  5361       assert( pChild->nOverflow==0 );
       
  5362       if( pChild->nFree>=100 ){
       
  5363         /* The child information will fit on the root page, so do the
       
  5364         ** copy */
       
  5365         int i;
       
  5366         zeroPage(pPage, pChild->aData[0]);
       
  5367         for(i=0; i<pChild->nCell; i++){
       
  5368           apCell[i] = findCell(pChild,i);
       
  5369           szCell[i] = cellSizePtr(pChild, apCell[i]);
       
  5370         }
       
  5371         assemblePage(pPage, pChild->nCell, apCell, szCell);
       
  5372         /* Copy the right-pointer of the child to the parent. */
       
  5373         put4byte(&pPage->aData[pPage->hdrOffset+8], 
       
  5374             get4byte(&pChild->aData[pChild->hdrOffset+8]));
       
  5375         freePage(pChild);
       
  5376         TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
       
  5377       }else{
       
  5378         /* The child has more information that will fit on the root.
       
  5379         ** The tree is already balanced.  Do nothing. */
       
  5380         TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
       
  5381       }
       
  5382     }else{
       
  5383       memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
       
  5384       pPage->isInit = 0;
       
  5385       pPage->pParent = 0;
       
  5386       rc = sqlite3BtreeInitPage(pPage, 0);
       
  5387       assert( rc==SQLITE_OK );
       
  5388       freePage(pChild);
       
  5389       TRACE(("BALANCE: transfer child %d into root %d\n",
       
  5390               pChild->pgno, pPage->pgno));
       
  5391     }
       
  5392     rc = reparentChildPages(pPage);
       
  5393     assert( pPage->nOverflow==0 );
       
  5394 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  5395     if( pBt->autoVacuum ){
       
  5396       int i;
       
  5397       for(i=0; i<pPage->nCell; i++){ 
       
  5398         rc = ptrmapPutOvfl(pPage, i);
       
  5399         if( rc!=SQLITE_OK ){
       
  5400           goto end_shallow_balance;
       
  5401         }
       
  5402       }
       
  5403     }
       
  5404 #endif
       
  5405     releasePage(pChild);
       
  5406   }
       
  5407 end_shallow_balance:
       
  5408   sqlite3_free(apCell);
       
  5409   return rc;
       
  5410 }
       
  5411 
       
  5412 
       
  5413 /*
       
  5414 ** The root page is overfull
       
  5415 **
       
  5416 ** When this happens, Create a new child page and copy the
       
  5417 ** contents of the root into the child.  Then make the root
       
  5418 ** page an empty page with rightChild pointing to the new
       
  5419 ** child.   Finally, call balance_internal() on the new child
       
  5420 ** to cause it to split.
       
  5421 */
       
  5422 static int balance_deeper(MemPage *pPage){
       
  5423   int rc;             /* Return value from subprocedures */
       
  5424   MemPage *pChild;    /* Pointer to a new child page */
       
  5425   Pgno pgnoChild;     /* Page number of the new child page */
       
  5426   BtShared *pBt;         /* The BTree */
       
  5427   int usableSize;     /* Total usable size of a page */
       
  5428   u8 *data;           /* Content of the parent page */
       
  5429   u8 *cdata;          /* Content of the child page */
       
  5430   int hdr;            /* Offset to page header in parent */
       
  5431   int brk;            /* Offset to content of first cell in parent */
       
  5432 
       
  5433   assert( pPage->pParent==0 );
       
  5434   assert( pPage->nOverflow>0 );
       
  5435   pBt = pPage->pBt;
       
  5436   assert( sqlite3_mutex_held(pBt->mutex) );
       
  5437   rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
       
  5438   if( rc ) return rc;
       
  5439   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
       
  5440   usableSize = pBt->usableSize;
       
  5441   data = pPage->aData;
       
  5442   hdr = pPage->hdrOffset;
       
  5443   brk = get2byte(&data[hdr+5]);
       
  5444   cdata = pChild->aData;
       
  5445   memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
       
  5446   memcpy(&cdata[brk], &data[brk], usableSize-brk);
       
  5447   assert( pChild->isInit==0 );
       
  5448   rc = sqlite3BtreeInitPage(pChild, pPage);
       
  5449   if( rc ) goto balancedeeper_out;
       
  5450   memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
       
  5451   pChild->nOverflow = pPage->nOverflow;
       
  5452   if( pChild->nOverflow ){
       
  5453     pChild->nFree = 0;
       
  5454   }
       
  5455   assert( pChild->nCell==pPage->nCell );
       
  5456   zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
       
  5457   put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
       
  5458   TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
       
  5459 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  5460   if( pBt->autoVacuum ){
       
  5461     int i;
       
  5462     rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
       
  5463     if( rc ) goto balancedeeper_out;
       
  5464     for(i=0; i<pChild->nCell; i++){
       
  5465       rc = ptrmapPutOvfl(pChild, i);
       
  5466       if( rc!=SQLITE_OK ){
       
  5467         return rc;
       
  5468       }
       
  5469     }
       
  5470   }
       
  5471 #endif
       
  5472   rc = balance_nonroot(pChild);
       
  5473 
       
  5474 balancedeeper_out:
       
  5475   releasePage(pChild);
       
  5476   return rc;
       
  5477 }
       
  5478 
       
  5479 /*
       
  5480 ** Decide if the page pPage needs to be balanced.  If balancing is
       
  5481 ** required, call the appropriate balancing routine.
       
  5482 */
       
  5483 static int balance(MemPage *pPage, int insert){
       
  5484   int rc = SQLITE_OK;
       
  5485   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
       
  5486   if( pPage->pParent==0 ){
       
  5487     rc = sqlite3PagerWrite(pPage->pDbPage);
       
  5488     if( rc==SQLITE_OK && pPage->nOverflow>0 ){
       
  5489       rc = balance_deeper(pPage);
       
  5490     }
       
  5491     if( rc==SQLITE_OK && pPage->nCell==0 ){
       
  5492       rc = balance_shallower(pPage);
       
  5493     }
       
  5494   }else{
       
  5495     if( pPage->nOverflow>0 || 
       
  5496         (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
       
  5497       rc = balance_nonroot(pPage);
       
  5498     }
       
  5499   }
       
  5500   return rc;
       
  5501 }
       
  5502 
       
  5503 /*
       
  5504 ** This routine checks all cursors that point to table pgnoRoot.
       
  5505 ** If any of those cursors were opened with wrFlag==0 in a different
       
  5506 ** database connection (a database connection that shares the pager
       
  5507 ** cache with the current connection) and that other connection 
       
  5508 ** is not in the ReadUncommmitted state, then this routine returns 
       
  5509 ** SQLITE_LOCKED.
       
  5510 **
       
  5511 ** In addition to checking for read-locks (where a read-lock 
       
  5512 ** means a cursor opened with wrFlag==0) this routine also moves
       
  5513 ** all write cursors so that they are pointing to the 
       
  5514 ** first Cell on the root page.  This is necessary because an insert 
       
  5515 ** or delete might change the number of cells on a page or delete
       
  5516 ** a page entirely and we do not want to leave any cursors 
       
  5517 ** pointing to non-existant pages or cells.
       
  5518 */
       
  5519 static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
       
  5520   BtCursor *p;
       
  5521   BtShared *pBt = pBtree->pBt;
       
  5522   sqlite3 *db = pBtree->db;
       
  5523   assert( sqlite3BtreeHoldsMutex(pBtree) );
       
  5524   for(p=pBt->pCursor; p; p=p->pNext){
       
  5525     if( p==pExclude ) continue;
       
  5526     if( p->eState!=CURSOR_VALID ) continue;
       
  5527     if( p->pgnoRoot!=pgnoRoot ) continue;
       
  5528     if( p->wrFlag==0 ){
       
  5529       sqlite3 *dbOther = p->pBtree->db;
       
  5530       if( dbOther==0 ||
       
  5531          (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
       
  5532         return SQLITE_LOCKED;
       
  5533       }
       
  5534     }else if( p->pPage->pgno!=p->pgnoRoot ){
       
  5535       moveToRoot(p);
       
  5536     }
       
  5537   }
       
  5538   return SQLITE_OK;
       
  5539 }
       
  5540 
       
  5541 /*
       
  5542 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
       
  5543 ** and the data is given by (pData,nData).  The cursor is used only to
       
  5544 ** define what table the record should be inserted into.  The cursor
       
  5545 ** is left pointing at a random location.
       
  5546 **
       
  5547 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
       
  5548 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
       
  5549 */
       
  5550 int sqlite3BtreeInsert(
       
  5551   BtCursor *pCur,                /* Insert data into the table of this cursor */
       
  5552   const void *pKey, i64 nKey,    /* The key of the new record */
       
  5553   const void *pData, int nData,  /* The data of the new record */
       
  5554   int nZero,                     /* Number of extra 0 bytes to append to data */
       
  5555   int appendBias                 /* True if this is likely an append */
       
  5556 ){
       
  5557   int rc;
       
  5558   int loc;
       
  5559   int szNew;
       
  5560   MemPage *pPage;
       
  5561   Btree *p = pCur->pBtree;
       
  5562   BtShared *pBt = p->pBt;
       
  5563   unsigned char *oldCell;
       
  5564   unsigned char *newCell = 0;
       
  5565 
       
  5566   assert( cursorHoldsMutex(pCur) );
       
  5567   if( pBt->inTransaction!=TRANS_WRITE ){
       
  5568     /* Must start a transaction before doing an insert */
       
  5569     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  5570     return rc;
       
  5571   }
       
  5572   assert( !pBt->readOnly );
       
  5573   if( !pCur->wrFlag ){
       
  5574     return SQLITE_PERM;   /* Cursor not open for writing */
       
  5575   }
       
  5576   if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
       
  5577     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
       
  5578   }
       
  5579   if( pCur->eState==CURSOR_FAULT ){
       
  5580     return pCur->skip;
       
  5581   }
       
  5582 
       
  5583   /* Save the positions of any other cursors open on this table */
       
  5584   clearCursorPosition(pCur);
       
  5585   if( 
       
  5586     SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
       
  5587     SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
       
  5588   ){
       
  5589     return rc;
       
  5590   }
       
  5591 
       
  5592   pPage = pCur->pPage;
       
  5593   assert( pPage->intKey || nKey>=0 );
       
  5594   assert( pPage->leaf || !pPage->leafData );
       
  5595   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
       
  5596           pCur->pgnoRoot, nKey, nData, pPage->pgno,
       
  5597           loc==0 ? "overwrite" : "new entry"));
       
  5598   assert( pPage->isInit );
       
  5599   newCell = (unsigned char*)sqlite3_malloc( MX_CELL_SIZE(pBt) );
       
  5600   if( newCell==0 ) return SQLITE_NOMEM;
       
  5601   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
       
  5602   if( rc ) goto end_insert;
       
  5603   assert( szNew==cellSizePtr(pPage, newCell) );
       
  5604   assert( szNew<=MX_CELL_SIZE(pBt) );
       
  5605   if( loc==0 && CURSOR_VALID==pCur->eState ){
       
  5606     int szOld;
       
  5607     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
       
  5608     rc = sqlite3PagerWrite(pPage->pDbPage);
       
  5609     if( rc ){
       
  5610       goto end_insert;
       
  5611     }
       
  5612     oldCell = findCell(pPage, pCur->idx);
       
  5613     if( !pPage->leaf ){
       
  5614       memcpy(newCell, oldCell, 4);
       
  5615     }
       
  5616     szOld = cellSizePtr(pPage, oldCell);
       
  5617     rc = clearCell(pPage, oldCell);
       
  5618     if( rc ) goto end_insert;
       
  5619     dropCell(pPage, pCur->idx, szOld);
       
  5620   }else if( loc<0 && pPage->nCell>0 ){
       
  5621     assert( pPage->leaf );
       
  5622     pCur->idx++;
       
  5623     pCur->info.nSize = 0;
       
  5624   }else{
       
  5625     assert( pPage->leaf );
       
  5626   }
       
  5627   rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
       
  5628   if( rc!=SQLITE_OK ) goto end_insert;
       
  5629   rc = balance(pPage, 1);
       
  5630   /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
       
  5631   /* fflush(stdout); */
       
  5632   if( rc==SQLITE_OK ){
       
  5633     moveToRoot(pCur);
       
  5634   }
       
  5635 end_insert:
       
  5636   sqlite3_free(newCell);
       
  5637   return rc;
       
  5638 }
       
  5639 
       
  5640 /*
       
  5641 ** Delete the entry that the cursor is pointing to.  The cursor
       
  5642 ** is left pointing at a random location.
       
  5643 */
       
  5644 int sqlite3BtreeDelete(BtCursor *pCur){
       
  5645   MemPage *pPage = pCur->pPage;
       
  5646   unsigned char *pCell;
       
  5647   int rc;
       
  5648   Pgno pgnoChild = 0;
       
  5649   Btree *p = pCur->pBtree;
       
  5650   BtShared *pBt = p->pBt;
       
  5651 
       
  5652   assert( cursorHoldsMutex(pCur) );
       
  5653   assert( pPage->isInit );
       
  5654   if( pBt->inTransaction!=TRANS_WRITE ){
       
  5655     /* Must start a transaction before doing a delete */
       
  5656     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  5657     return rc;
       
  5658   }
       
  5659   assert( !pBt->readOnly );
       
  5660   if( pCur->eState==CURSOR_FAULT ){
       
  5661     return pCur->skip;
       
  5662   }
       
  5663   if( pCur->idx >= pPage->nCell ){
       
  5664     return SQLITE_ERROR;  /* The cursor is not pointing to anything */
       
  5665   }
       
  5666   if( !pCur->wrFlag ){
       
  5667     return SQLITE_PERM;   /* Did not open this cursor for writing */
       
  5668   }
       
  5669   if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
       
  5670     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
       
  5671   }
       
  5672 
       
  5673   /* Restore the current cursor position (a no-op if the cursor is not in 
       
  5674   ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors 
       
  5675   ** open on the same table. Then call sqlite3PagerWrite() on the page
       
  5676   ** that the entry will be deleted from.
       
  5677   */
       
  5678   if( 
       
  5679     (rc = restoreOrClearCursorPosition(pCur))!=0 ||
       
  5680     (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
       
  5681     (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
       
  5682   ){
       
  5683     return rc;
       
  5684   }
       
  5685 
       
  5686   /* Locate the cell within its page and leave pCell pointing to the
       
  5687   ** data. The clearCell() call frees any overflow pages associated with the
       
  5688   ** cell. The cell itself is still intact.
       
  5689   */
       
  5690   pCell = findCell(pPage, pCur->idx);
       
  5691   if( !pPage->leaf ){
       
  5692     pgnoChild = get4byte(pCell);
       
  5693   }
       
  5694   rc = clearCell(pPage, pCell);
       
  5695   if( rc ){
       
  5696     return rc;
       
  5697   }
       
  5698 
       
  5699   if( !pPage->leaf ){
       
  5700     /*
       
  5701     ** The entry we are about to delete is not a leaf so if we do not
       
  5702     ** do something we will leave a hole on an internal page.
       
  5703     ** We have to fill the hole by moving in a cell from a leaf.  The
       
  5704     ** next Cell after the one to be deleted is guaranteed to exist and
       
  5705     ** to be a leaf so we can use it.
       
  5706     */
       
  5707     BtCursor leafCur;
       
  5708     unsigned char *pNext;
       
  5709     int szNext;  /* The compiler warning is wrong: szNext is always 
       
  5710                  ** initialized before use.  Adding an extra initialization
       
  5711                  ** to silence the compiler slows down the code. */
       
  5712     int notUsed;
       
  5713     unsigned char *tempCell = 0;
       
  5714     assert( !pPage->leafData );
       
  5715     sqlite3BtreeGetTempCursor(pCur, &leafCur);
       
  5716     rc = sqlite3BtreeNext(&leafCur, &notUsed);
       
  5717     if( rc==SQLITE_OK ){
       
  5718       rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
       
  5719     }
       
  5720     if( rc==SQLITE_OK ){
       
  5721       TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
       
  5722          pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
       
  5723       dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
       
  5724       pNext = findCell(leafCur.pPage, leafCur.idx);
       
  5725       szNext = cellSizePtr(leafCur.pPage, pNext);
       
  5726       assert( MX_CELL_SIZE(pBt)>=szNext+4 );
       
  5727       tempCell = (unsigned char*)sqlite3_malloc( MX_CELL_SIZE(pBt) );
       
  5728       if( tempCell==0 ){
       
  5729         rc = SQLITE_NOMEM;
       
  5730       }
       
  5731     }
       
  5732     if( rc==SQLITE_OK ){
       
  5733       rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
       
  5734     }
       
  5735     if( rc==SQLITE_OK ){
       
  5736       put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
       
  5737       rc = balance(pPage, 0);
       
  5738     }
       
  5739     if( rc==SQLITE_OK ){
       
  5740       dropCell(leafCur.pPage, leafCur.idx, szNext);
       
  5741       rc = balance(leafCur.pPage, 0);
       
  5742     }
       
  5743     sqlite3_free(tempCell);
       
  5744     sqlite3BtreeReleaseTempCursor(&leafCur);
       
  5745   }else{
       
  5746     TRACE(("DELETE: table=%d delete from leaf %d\n",
       
  5747        pCur->pgnoRoot, pPage->pgno));
       
  5748     dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
       
  5749     rc = balance(pPage, 0);
       
  5750   }
       
  5751   if( rc==SQLITE_OK ){
       
  5752     moveToRoot(pCur);
       
  5753   }
       
  5754   return rc;
       
  5755 }
       
  5756 
       
  5757 /*
       
  5758 ** Create a new BTree table.  Write into *piTable the page
       
  5759 ** number for the root page of the new table.
       
  5760 **
       
  5761 ** The type of type is determined by the flags parameter.  Only the
       
  5762 ** following values of flags are currently in use.  Other values for
       
  5763 ** flags might not work:
       
  5764 **
       
  5765 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
       
  5766 **     BTREE_ZERODATA                  Used for SQL indices
       
  5767 */
       
  5768 static int btreeCreateTable(Btree *p, int *piTable, int flags){
       
  5769   BtShared *pBt = p->pBt;
       
  5770   MemPage *pRoot;
       
  5771   Pgno pgnoRoot;
       
  5772   int rc;
       
  5773 
       
  5774   assert( sqlite3BtreeHoldsMutex(p) );
       
  5775   if( pBt->inTransaction!=TRANS_WRITE ){
       
  5776     /* Must start a transaction first */
       
  5777     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  5778     return rc;
       
  5779   }
       
  5780   assert( !pBt->readOnly );
       
  5781 
       
  5782 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  5783   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
       
  5784   if( rc ){
       
  5785     return rc;
       
  5786   }
       
  5787 #else
       
  5788   if( pBt->autoVacuum ){
       
  5789     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
       
  5790     MemPage *pPageMove; /* The page to move to. */
       
  5791 
       
  5792     /* Creating a new table may probably require moving an existing database
       
  5793     ** to make room for the new tables root page. In case this page turns
       
  5794     ** out to be an overflow page, delete all overflow page-map caches
       
  5795     ** held by open cursors.
       
  5796     */
       
  5797     invalidateAllOverflowCache(pBt);
       
  5798 
       
  5799     /* Read the value of meta[3] from the database to determine where the
       
  5800     ** root page of the new table should go. meta[3] is the largest root-page
       
  5801     ** created so far, so the new root-page is (meta[3]+1).
       
  5802     */
       
  5803     rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
       
  5804     if( rc!=SQLITE_OK ){
       
  5805       return rc;
       
  5806     }
       
  5807     pgnoRoot++;
       
  5808 
       
  5809     /* The new root-page may not be allocated on a pointer-map page, or the
       
  5810     ** PENDING_BYTE page.
       
  5811     */
       
  5812     if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
       
  5813         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
       
  5814       pgnoRoot++;
       
  5815     }
       
  5816     assert( pgnoRoot>=3 );
       
  5817 
       
  5818     /* Allocate a page. The page that currently resides at pgnoRoot will
       
  5819     ** be moved to the allocated page (unless the allocated page happens
       
  5820     ** to reside at pgnoRoot).
       
  5821     */
       
  5822     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
       
  5823     if( rc!=SQLITE_OK ){
       
  5824       return rc;
       
  5825     }
       
  5826 
       
  5827     if( pgnoMove!=pgnoRoot ){
       
  5828       /* pgnoRoot is the page that will be used for the root-page of
       
  5829       ** the new table (assuming an error did not occur). But we were
       
  5830       ** allocated pgnoMove. If required (i.e. if it was not allocated
       
  5831       ** by extending the file), the current page at position pgnoMove
       
  5832       ** is already journaled.
       
  5833       */
       
  5834       u8 eType;
       
  5835       Pgno iPtrPage;
       
  5836 
       
  5837       releasePage(pPageMove);
       
  5838 
       
  5839       /* Move the page currently at pgnoRoot to pgnoMove. */
       
  5840       rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
       
  5841       if( rc!=SQLITE_OK ){
       
  5842         return rc;
       
  5843       }
       
  5844       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
       
  5845       if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
       
  5846         releasePage(pRoot);
       
  5847         return rc;
       
  5848       }
       
  5849       assert( eType!=PTRMAP_ROOTPAGE );
       
  5850       assert( eType!=PTRMAP_FREEPAGE );
       
  5851       rc = sqlite3PagerWrite(pRoot->pDbPage);
       
  5852       if( rc!=SQLITE_OK ){
       
  5853         releasePage(pRoot);
       
  5854         return rc;
       
  5855       }
       
  5856       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
       
  5857       releasePage(pRoot);
       
  5858 
       
  5859       /* Obtain the page at pgnoRoot */
       
  5860       if( rc!=SQLITE_OK ){
       
  5861         return rc;
       
  5862       }
       
  5863       rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
       
  5864       if( rc!=SQLITE_OK ){
       
  5865         return rc;
       
  5866       }
       
  5867       rc = sqlite3PagerWrite(pRoot->pDbPage);
       
  5868       if( rc!=SQLITE_OK ){
       
  5869         releasePage(pRoot);
       
  5870         return rc;
       
  5871       }
       
  5872     }else{
       
  5873       pRoot = pPageMove;
       
  5874     } 
       
  5875 
       
  5876     /* Update the pointer-map and meta-data with the new root-page number. */
       
  5877     rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
       
  5878     if( rc ){
       
  5879       releasePage(pRoot);
       
  5880       return rc;
       
  5881     }
       
  5882     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
       
  5883     if( rc ){
       
  5884       releasePage(pRoot);
       
  5885       return rc;
       
  5886     }
       
  5887 
       
  5888   }else{
       
  5889     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
       
  5890     if( rc ) return rc;
       
  5891   }
       
  5892 #endif
       
  5893   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
       
  5894   zeroPage(pRoot, flags | PTF_LEAF);
       
  5895   sqlite3PagerUnref(pRoot->pDbPage);
       
  5896   *piTable = (int)pgnoRoot;
       
  5897   return SQLITE_OK;
       
  5898 }
       
  5899 int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
       
  5900   int rc;
       
  5901   sqlite3BtreeEnter(p);
       
  5902   p->pBt->db = p->db;
       
  5903   rc = btreeCreateTable(p, piTable, flags);
       
  5904   sqlite3BtreeLeave(p);
       
  5905   return rc;
       
  5906 }
       
  5907 
       
  5908 /*
       
  5909 ** Erase the given database page and all its children.  Return
       
  5910 ** the page to the freelist.
       
  5911 */
       
  5912 static int clearDatabasePage(
       
  5913   BtShared *pBt,           /* The BTree that contains the table */
       
  5914   Pgno pgno,            /* Page number to clear */
       
  5915   MemPage *pParent,     /* Parent page.  NULL for the root */
       
  5916   int freePageFlag      /* Deallocate page if true */
       
  5917 ){
       
  5918   MemPage *pPage = 0;
       
  5919   int rc;
       
  5920   unsigned char *pCell;
       
  5921   int i;
       
  5922 
       
  5923   assert( sqlite3_mutex_held(pBt->mutex) );
       
  5924   if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
       
  5925     return SQLITE_CORRUPT_BKPT;
       
  5926   }
       
  5927 
       
  5928   rc = getAndInitPage(pBt, pgno, &pPage, pParent);
       
  5929   if( rc ) goto cleardatabasepage_out;
       
  5930   for(i=0; i<pPage->nCell; i++){
       
  5931     pCell = findCell(pPage, i);
       
  5932     if( !pPage->leaf ){
       
  5933       rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
       
  5934       if( rc ) goto cleardatabasepage_out;
       
  5935     }
       
  5936     rc = clearCell(pPage, pCell);
       
  5937     if( rc ) goto cleardatabasepage_out;
       
  5938   }
       
  5939   if( !pPage->leaf ){
       
  5940     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
       
  5941     if( rc ) goto cleardatabasepage_out;
       
  5942   }
       
  5943   if( freePageFlag ){
       
  5944     rc = freePage(pPage);
       
  5945   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
       
  5946     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
       
  5947   }
       
  5948 
       
  5949 cleardatabasepage_out:
       
  5950   releasePage(pPage);
       
  5951   return rc;
       
  5952 }
       
  5953 
       
  5954 /*
       
  5955 ** Delete all information from a single table in the database.  iTable is
       
  5956 ** the page number of the root of the table.  After this routine returns,
       
  5957 ** the root page is empty, but still exists.
       
  5958 **
       
  5959 ** This routine will fail with SQLITE_LOCKED if there are any open
       
  5960 ** read cursors on the table.  Open write cursors are moved to the
       
  5961 ** root of the table.
       
  5962 */
       
  5963 int sqlite3BtreeClearTable(Btree *p, int iTable){
       
  5964   int rc;
       
  5965   BtShared *pBt = p->pBt;
       
  5966   sqlite3BtreeEnter(p);
       
  5967   pBt->db = p->db;
       
  5968   if( p->inTrans!=TRANS_WRITE ){
       
  5969     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  5970   }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){
       
  5971     /* nothing to do */
       
  5972   }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
       
  5973     /* nothing to do */
       
  5974   }else{
       
  5975     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
       
  5976   }
       
  5977   sqlite3BtreeLeave(p);
       
  5978   return rc;
       
  5979 }
       
  5980 
       
  5981 /*
       
  5982 ** Erase all information in a table and add the root of the table to
       
  5983 ** the freelist.  Except, the root of the principle table (the one on
       
  5984 ** page 1) is never added to the freelist.
       
  5985 **
       
  5986 ** This routine will fail with SQLITE_LOCKED if there are any open
       
  5987 ** cursors on the table.
       
  5988 **
       
  5989 ** If AUTOVACUUM is enabled and the page at iTable is not the last
       
  5990 ** root page in the database file, then the last root page 
       
  5991 ** in the database file is moved into the slot formerly occupied by
       
  5992 ** iTable and that last slot formerly occupied by the last root page
       
  5993 ** is added to the freelist instead of iTable.  In this say, all
       
  5994 ** root pages are kept at the beginning of the database file, which
       
  5995 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
       
  5996 ** page number that used to be the last root page in the file before
       
  5997 ** the move.  If no page gets moved, *piMoved is set to 0.
       
  5998 ** The last root page is recorded in meta[3] and the value of
       
  5999 ** meta[3] is updated by this procedure.
       
  6000 */
       
  6001 static int btreeDropTable(Btree *p, int iTable, int *piMoved){
       
  6002   int rc;
       
  6003   MemPage *pPage = 0;
       
  6004   BtShared *pBt = p->pBt;
       
  6005 
       
  6006   assert( sqlite3BtreeHoldsMutex(p) );
       
  6007   if( p->inTrans!=TRANS_WRITE ){
       
  6008     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  6009   }
       
  6010 
       
  6011   /* It is illegal to drop a table if any cursors are open on the
       
  6012   ** database. This is because in auto-vacuum mode the backend may
       
  6013   ** need to move another root-page to fill a gap left by the deleted
       
  6014   ** root page. If an open cursor was using this page a problem would 
       
  6015   ** occur.
       
  6016   */
       
  6017   if( pBt->pCursor ){
       
  6018     return SQLITE_LOCKED;
       
  6019   }
       
  6020 
       
  6021   rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
       
  6022   if( rc ) return rc;
       
  6023   rc = sqlite3BtreeClearTable(p, iTable);
       
  6024   if( rc ){
       
  6025     releasePage(pPage);
       
  6026     return rc;
       
  6027   }
       
  6028 
       
  6029   *piMoved = 0;
       
  6030 
       
  6031   if( iTable>1 ){
       
  6032 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  6033     rc = freePage(pPage);
       
  6034     releasePage(pPage);
       
  6035 #else
       
  6036     if( pBt->autoVacuum ){
       
  6037       Pgno maxRootPgno;
       
  6038       rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
       
  6039       if( rc!=SQLITE_OK ){
       
  6040         releasePage(pPage);
       
  6041         return rc;
       
  6042       }
       
  6043 
       
  6044       if( iTable==maxRootPgno ){
       
  6045         /* If the table being dropped is the table with the largest root-page
       
  6046         ** number in the database, put the root page on the free list. 
       
  6047         */
       
  6048         rc = freePage(pPage);
       
  6049         releasePage(pPage);
       
  6050         if( rc!=SQLITE_OK ){
       
  6051           return rc;
       
  6052         }
       
  6053       }else{
       
  6054         /* The table being dropped does not have the largest root-page
       
  6055         ** number in the database. So move the page that does into the 
       
  6056         ** gap left by the deleted root-page.
       
  6057         */
       
  6058         MemPage *pMove;
       
  6059         releasePage(pPage);
       
  6060         rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
       
  6061         if( rc!=SQLITE_OK ){
       
  6062           return rc;
       
  6063         }
       
  6064         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
       
  6065         releasePage(pMove);
       
  6066         if( rc!=SQLITE_OK ){
       
  6067           return rc;
       
  6068         }
       
  6069         rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
       
  6070         if( rc!=SQLITE_OK ){
       
  6071           return rc;
       
  6072         }
       
  6073         rc = freePage(pMove);
       
  6074         releasePage(pMove);
       
  6075         if( rc!=SQLITE_OK ){
       
  6076           return rc;
       
  6077         }
       
  6078         *piMoved = maxRootPgno;
       
  6079       }
       
  6080 
       
  6081       /* Set the new 'max-root-page' value in the database header. This
       
  6082       ** is the old value less one, less one more if that happens to
       
  6083       ** be a root-page number, less one again if that is the
       
  6084       ** PENDING_BYTE_PAGE.
       
  6085       */
       
  6086       maxRootPgno--;
       
  6087       if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
       
  6088         maxRootPgno--;
       
  6089       }
       
  6090       if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
       
  6091         maxRootPgno--;
       
  6092       }
       
  6093       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
       
  6094 
       
  6095       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
       
  6096     }else{
       
  6097       rc = freePage(pPage);
       
  6098       releasePage(pPage);
       
  6099     }
       
  6100 #endif
       
  6101   }else{
       
  6102     /* If sqlite3BtreeDropTable was called on page 1. */
       
  6103     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
       
  6104     releasePage(pPage);
       
  6105   }
       
  6106   return rc;  
       
  6107 }
       
  6108 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
       
  6109   int rc;
       
  6110   sqlite3BtreeEnter(p);
       
  6111   p->pBt->db = p->db;
       
  6112   rc = btreeDropTable(p, iTable, piMoved);
       
  6113   sqlite3BtreeLeave(p);
       
  6114   return rc;
       
  6115 }
       
  6116 
       
  6117 
       
  6118 /*
       
  6119 ** Read the meta-information out of a database file.  Meta[0]
       
  6120 ** is the number of free pages currently in the database.  Meta[1]
       
  6121 ** through meta[15] are available for use by higher layers.  Meta[0]
       
  6122 ** is read-only, the others are read/write.
       
  6123 ** 
       
  6124 ** The schema layer numbers meta values differently.  At the schema
       
  6125 ** layer (and the SetCookie and ReadCookie opcodes) the number of
       
  6126 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
       
  6127 */
       
  6128 int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
       
  6129   DbPage *pDbPage;
       
  6130   int rc;
       
  6131   unsigned char *pP1;
       
  6132   BtShared *pBt = p->pBt;
       
  6133 
       
  6134   sqlite3BtreeEnter(p);
       
  6135   pBt->db = p->db;
       
  6136 
       
  6137   /* Reading a meta-data value requires a read-lock on page 1 (and hence
       
  6138   ** the sqlite_master table. We grab this lock regardless of whether or
       
  6139   ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
       
  6140   ** 1 is treated as a special case by queryTableLock() and lockTable()).
       
  6141   */
       
  6142   rc = queryTableLock(p, 1, READ_LOCK);
       
  6143   if( rc!=SQLITE_OK ){
       
  6144     sqlite3BtreeLeave(p);
       
  6145     return rc;
       
  6146   }
       
  6147 
       
  6148   assert( idx>=0 && idx<=15 );
       
  6149   rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
       
  6150   if( rc ){
       
  6151     sqlite3BtreeLeave(p);
       
  6152     return rc;
       
  6153   }
       
  6154   pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
       
  6155   *pMeta = get4byte(&pP1[36 + idx*4]);
       
  6156   sqlite3PagerUnref(pDbPage);
       
  6157 
       
  6158   /* If autovacuumed is disabled in this build but we are trying to 
       
  6159   ** access an autovacuumed database, then make the database readonly. 
       
  6160   */
       
  6161 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  6162   if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
       
  6163 #endif
       
  6164 
       
  6165   /* Grab the read-lock on page 1. */
       
  6166   rc = lockTable(p, 1, READ_LOCK);
       
  6167   sqlite3BtreeLeave(p);
       
  6168   return rc;
       
  6169 }
       
  6170 
       
  6171 /*
       
  6172 ** Write meta-information back into the database.  Meta[0] is
       
  6173 ** read-only and may not be written.
       
  6174 */
       
  6175 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
       
  6176   BtShared *pBt = p->pBt;
       
  6177   unsigned char *pP1;
       
  6178   int rc;
       
  6179   assert( idx>=1 && idx<=15 );
       
  6180   sqlite3BtreeEnter(p);
       
  6181   pBt->db = p->db;
       
  6182   if( p->inTrans!=TRANS_WRITE ){
       
  6183     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
       
  6184   }else{
       
  6185     assert( pBt->pPage1!=0 );
       
  6186     pP1 = pBt->pPage1->aData;
       
  6187     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
       
  6188     if( rc==SQLITE_OK ){
       
  6189       put4byte(&pP1[36 + idx*4], iMeta);
       
  6190 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6191       if( idx==7 ){
       
  6192         assert( pBt->autoVacuum || iMeta==0 );
       
  6193         assert( iMeta==0 || iMeta==1 );
       
  6194         pBt->incrVacuum = iMeta;
       
  6195       }
       
  6196 #endif
       
  6197     }
       
  6198   }
       
  6199   sqlite3BtreeLeave(p);
       
  6200   return rc;
       
  6201 }
       
  6202 
       
  6203 /*
       
  6204 ** Return the flag byte at the beginning of the page that the cursor
       
  6205 ** is currently pointing to.
       
  6206 */
       
  6207 int sqlite3BtreeFlags(BtCursor *pCur){
       
  6208   /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
       
  6209   ** restoreOrClearCursorPosition() here.
       
  6210   */
       
  6211   MemPage *pPage = pCur->pPage;
       
  6212   assert( cursorHoldsMutex(pCur) );
       
  6213   assert( pPage->pBt==pCur->pBt );
       
  6214   return pPage ? pPage->aData[pPage->hdrOffset] : 0;
       
  6215 }
       
  6216 
       
  6217 
       
  6218 /*
       
  6219 ** Return the pager associated with a BTree.  This routine is used for
       
  6220 ** testing and debugging only.
       
  6221 */
       
  6222 Pager *sqlite3BtreePager(Btree *p){
       
  6223   return p->pBt->pPager;
       
  6224 }
       
  6225 
       
  6226 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
  6227 /*
       
  6228 ** Append a message to the error message string.
       
  6229 */
       
  6230 static void checkAppendMsg(
       
  6231   IntegrityCk *pCheck,
       
  6232   char *zMsg1,
       
  6233   const char *zFormat,
       
  6234   ...
       
  6235 ){
       
  6236   va_list ap;
       
  6237   char *zMsg2;
       
  6238   if( !pCheck->mxErr ) return;
       
  6239   pCheck->mxErr--;
       
  6240   pCheck->nErr++;
       
  6241   va_start(ap, zFormat);
       
  6242   zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
       
  6243   va_end(ap);
       
  6244   if( zMsg1==0 ) zMsg1 = "";
       
  6245   if( pCheck->zErrMsg ){
       
  6246     char *zOld = pCheck->zErrMsg;
       
  6247     pCheck->zErrMsg = 0;
       
  6248     sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
       
  6249     sqlite3_free(zOld);
       
  6250   }else{
       
  6251     sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
       
  6252   }
       
  6253   sqlite3_free(zMsg2);
       
  6254 }
       
  6255 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
  6256 
       
  6257 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
  6258 /*
       
  6259 ** Add 1 to the reference count for page iPage.  If this is the second
       
  6260 ** reference to the page, add an error message to pCheck->zErrMsg.
       
  6261 ** Return 1 if there are 2 ore more references to the page and 0 if
       
  6262 ** if this is the first reference to the page.
       
  6263 **
       
  6264 ** Also check that the page number is in bounds.
       
  6265 */
       
  6266 static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
       
  6267   if( iPage==0 ) return 1;
       
  6268   if( iPage>pCheck->nPage || iPage<0 ){
       
  6269     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
       
  6270     return 1;
       
  6271   }
       
  6272   if( pCheck->anRef[iPage]==1 ){
       
  6273     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
       
  6274     return 1;
       
  6275   }
       
  6276   return  (pCheck->anRef[iPage]++)>1;
       
  6277 }
       
  6278 
       
  6279 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6280 /*
       
  6281 ** Check that the entry in the pointer-map for page iChild maps to 
       
  6282 ** page iParent, pointer type ptrType. If not, append an error message
       
  6283 ** to pCheck.
       
  6284 */
       
  6285 static void checkPtrmap(
       
  6286   IntegrityCk *pCheck,   /* Integrity check context */
       
  6287   Pgno iChild,           /* Child page number */
       
  6288   u8 eType,              /* Expected pointer map type */
       
  6289   Pgno iParent,          /* Expected pointer map parent page number */
       
  6290   char *zContext         /* Context description (used for error msg) */
       
  6291 ){
       
  6292   int rc;
       
  6293   u8 ePtrmapType;
       
  6294   Pgno iPtrmapParent;
       
  6295 
       
  6296   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
       
  6297   if( rc!=SQLITE_OK ){
       
  6298     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
       
  6299     return;
       
  6300   }
       
  6301 
       
  6302   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
       
  6303     checkAppendMsg(pCheck, zContext, 
       
  6304       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
       
  6305       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
       
  6306   }
       
  6307 }
       
  6308 #endif
       
  6309 
       
  6310 /*
       
  6311 ** Check the integrity of the freelist or of an overflow page list.
       
  6312 ** Verify that the number of pages on the list is N.
       
  6313 */
       
  6314 static void checkList(
       
  6315   IntegrityCk *pCheck,  /* Integrity checking context */
       
  6316   int isFreeList,       /* True for a freelist.  False for overflow page list */
       
  6317   int iPage,            /* Page number for first page in the list */
       
  6318   int N,                /* Expected number of pages in the list */
       
  6319   char *zContext        /* Context for error messages */
       
  6320 ){
       
  6321   int i;
       
  6322   int expected = N;
       
  6323   int iFirst = iPage;
       
  6324   while( N-- > 0 && pCheck->mxErr ){
       
  6325     DbPage *pOvflPage;
       
  6326     unsigned char *pOvflData;
       
  6327     if( iPage<1 ){
       
  6328       checkAppendMsg(pCheck, zContext,
       
  6329          "%d of %d pages missing from overflow list starting at %d",
       
  6330           N+1, expected, iFirst);
       
  6331       break;
       
  6332     }
       
  6333     if( checkRef(pCheck, iPage, zContext) ) break;
       
  6334     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
       
  6335       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
       
  6336       break;
       
  6337     }
       
  6338     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
       
  6339     if( isFreeList ){
       
  6340       int n = get4byte(&pOvflData[4]);
       
  6341 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6342       if( pCheck->pBt->autoVacuum ){
       
  6343         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
       
  6344       }
       
  6345 #endif
       
  6346       if( n>pCheck->pBt->usableSize/4-8 ){
       
  6347         checkAppendMsg(pCheck, zContext,
       
  6348            "freelist leaf count too big on page %d", iPage);
       
  6349         N--;
       
  6350       }else{
       
  6351         for(i=0; i<n; i++){
       
  6352           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
       
  6353 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6354           if( pCheck->pBt->autoVacuum ){
       
  6355             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
       
  6356           }
       
  6357 #endif
       
  6358           checkRef(pCheck, iFreePage, zContext);
       
  6359         }
       
  6360         N -= n;
       
  6361       }
       
  6362     }
       
  6363 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6364     else{
       
  6365       /* If this database supports auto-vacuum and iPage is not the last
       
  6366       ** page in this overflow list, check that the pointer-map entry for
       
  6367       ** the following page matches iPage.
       
  6368       */
       
  6369       if( pCheck->pBt->autoVacuum && N>0 ){
       
  6370         i = get4byte(pOvflData);
       
  6371         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
       
  6372       }
       
  6373     }
       
  6374 #endif
       
  6375     iPage = get4byte(pOvflData);
       
  6376     sqlite3PagerUnref(pOvflPage);
       
  6377   }
       
  6378 }
       
  6379 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
  6380 
       
  6381 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
  6382 /*
       
  6383 ** Do various sanity checks on a single page of a tree.  Return
       
  6384 ** the tree depth.  Root pages return 0.  Parents of root pages
       
  6385 ** return 1, and so forth.
       
  6386 ** 
       
  6387 ** These checks are done:
       
  6388 **
       
  6389 **      1.  Make sure that cells and freeblocks do not overlap
       
  6390 **          but combine to completely cover the page.
       
  6391 **  NO  2.  Make sure cell keys are in order.
       
  6392 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
       
  6393 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
       
  6394 **      5.  Check the integrity of overflow pages.
       
  6395 **      6.  Recursively call checkTreePage on all children.
       
  6396 **      7.  Verify that the depth of all children is the same.
       
  6397 **      8.  Make sure this page is at least 33% full or else it is
       
  6398 **          the root of the tree.
       
  6399 */
       
  6400 static int checkTreePage(
       
  6401   IntegrityCk *pCheck,  /* Context for the sanity check */
       
  6402   int iPage,            /* Page number of the page to check */
       
  6403   MemPage *pParent,     /* Parent page */
       
  6404   char *zParentContext  /* Parent context */
       
  6405 ){
       
  6406   MemPage *pPage;
       
  6407   int i, rc, depth, d2, pgno, cnt;
       
  6408   int hdr, cellStart;
       
  6409   int nCell;
       
  6410   u8 *data;
       
  6411   BtShared *pBt;
       
  6412   int usableSize;
       
  6413   char zContext[100];
       
  6414   char *hit;
       
  6415 
       
  6416   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
       
  6417 
       
  6418   /* Check that the page exists
       
  6419   */
       
  6420   pBt = pCheck->pBt;
       
  6421   usableSize = pBt->usableSize;
       
  6422   if( iPage==0 ) return 0;
       
  6423   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
       
  6424   if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
       
  6425     checkAppendMsg(pCheck, zContext,
       
  6426        "unable to get the page. error code=%d", rc);
       
  6427     return 0;
       
  6428   }
       
  6429   if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){
       
  6430     checkAppendMsg(pCheck, zContext, 
       
  6431                    "sqlite3BtreeInitPage() returns error code %d", rc);
       
  6432     releasePage(pPage);
       
  6433     return 0;
       
  6434   }
       
  6435 
       
  6436   /* Check out all the cells.
       
  6437   */
       
  6438   depth = 0;
       
  6439   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
       
  6440     u8 *pCell;
       
  6441     int sz;
       
  6442     CellInfo info;
       
  6443 
       
  6444     /* Check payload overflow pages
       
  6445     */
       
  6446     sqlite3_snprintf(sizeof(zContext), zContext,
       
  6447              "On tree page %d cell %d: ", iPage, i);
       
  6448     pCell = findCell(pPage,i);
       
  6449     sqlite3BtreeParseCellPtr(pPage, pCell, &info);
       
  6450     sz = info.nData;
       
  6451     if( !pPage->intKey ) sz += info.nKey;
       
  6452     assert( sz==info.nPayload );
       
  6453     if( sz>info.nLocal ){
       
  6454       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
       
  6455       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
       
  6456 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6457       if( pBt->autoVacuum ){
       
  6458         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
       
  6459       }
       
  6460 #endif
       
  6461       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
       
  6462     }
       
  6463 
       
  6464     /* Check sanity of left child page.
       
  6465     */
       
  6466     if( !pPage->leaf ){
       
  6467       pgno = get4byte(pCell);
       
  6468 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6469       if( pBt->autoVacuum ){
       
  6470         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
       
  6471       }
       
  6472 #endif
       
  6473       d2 = checkTreePage(pCheck,pgno,pPage,zContext);
       
  6474       if( i>0 && d2!=depth ){
       
  6475         checkAppendMsg(pCheck, zContext, "Child page depth differs");
       
  6476       }
       
  6477       depth = d2;
       
  6478     }
       
  6479   }
       
  6480   if( !pPage->leaf ){
       
  6481     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
       
  6482     sqlite3_snprintf(sizeof(zContext), zContext, 
       
  6483                      "On page %d at right child: ", iPage);
       
  6484 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6485     if( pBt->autoVacuum ){
       
  6486       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
       
  6487     }
       
  6488 #endif
       
  6489     checkTreePage(pCheck, pgno, pPage, zContext);
       
  6490   }
       
  6491  
       
  6492   /* Check for complete coverage of the page
       
  6493   */
       
  6494   data = pPage->aData;
       
  6495   hdr = pPage->hdrOffset;
       
  6496   hit = (char*)sqlite3MallocZero( usableSize );
       
  6497   if( hit ){
       
  6498     memset(hit, 1, get2byte(&data[hdr+5]));
       
  6499     nCell = get2byte(&data[hdr+3]);
       
  6500     cellStart = hdr + 12 - 4*pPage->leaf;
       
  6501     for(i=0; i<nCell; i++){
       
  6502       int pc = get2byte(&data[cellStart+i*2]);
       
  6503       int size = cellSizePtr(pPage, &data[pc]);
       
  6504       int j;
       
  6505       if( (pc+size-1)>=usableSize || pc<0 ){
       
  6506         checkAppendMsg(pCheck, 0, 
       
  6507             "Corruption detected in cell %d on page %d",i,iPage,0);
       
  6508       }else{
       
  6509         for(j=pc+size-1; j>=pc; j--) hit[j]++;
       
  6510       }
       
  6511     }
       
  6512     for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; 
       
  6513            cnt++){
       
  6514       int size = get2byte(&data[i+2]);
       
  6515       int j;
       
  6516       if( (i+size-1)>=usableSize || i<0 ){
       
  6517         checkAppendMsg(pCheck, 0,  
       
  6518             "Corruption detected in cell %d on page %d",i,iPage,0);
       
  6519       }else{
       
  6520         for(j=i+size-1; j>=i; j--) hit[j]++;
       
  6521       }
       
  6522       i = get2byte(&data[i]);
       
  6523     }
       
  6524     for(i=cnt=0; i<usableSize; i++){
       
  6525       if( hit[i]==0 ){
       
  6526         cnt++;
       
  6527       }else if( hit[i]>1 ){
       
  6528         checkAppendMsg(pCheck, 0,
       
  6529           "Multiple uses for byte %d of page %d", i, iPage);
       
  6530         break;
       
  6531       }
       
  6532     }
       
  6533     if( cnt!=data[hdr+7] ){
       
  6534       checkAppendMsg(pCheck, 0, 
       
  6535           "Fragmented space is %d byte reported as %d on page %d",
       
  6536           cnt, data[hdr+7], iPage);
       
  6537     }
       
  6538   }
       
  6539   sqlite3_free(hit);
       
  6540 
       
  6541   releasePage(pPage);
       
  6542   return depth+1;
       
  6543 }
       
  6544 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
  6545 
       
  6546 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
       
  6547 /*
       
  6548 ** This routine does a complete check of the given BTree file.  aRoot[] is
       
  6549 ** an array of pages numbers were each page number is the root page of
       
  6550 ** a table.  nRoot is the number of entries in aRoot.
       
  6551 **
       
  6552 ** If everything checks out, this routine returns NULL.  If something is
       
  6553 ** amiss, an error message is written into memory obtained from malloc()
       
  6554 ** and a pointer to that error message is returned.  The calling function
       
  6555 ** is responsible for freeing the error message when it is done.
       
  6556 */
       
  6557 char *sqlite3BtreeIntegrityCheck(
       
  6558   Btree *p,     /* The btree to be checked */
       
  6559   int *aRoot,   /* An array of root pages numbers for individual trees */
       
  6560   int nRoot,    /* Number of entries in aRoot[] */
       
  6561   int mxErr,    /* Stop reporting errors after this many */
       
  6562   int *pnErr    /* Write number of errors seen to this variable */
       
  6563 ){
       
  6564   int i;
       
  6565   int nRef;
       
  6566   IntegrityCk sCheck;
       
  6567   BtShared *pBt = p->pBt;
       
  6568 
       
  6569   sqlite3BtreeEnter(p);
       
  6570   pBt->db = p->db;
       
  6571   nRef = sqlite3PagerRefcount(pBt->pPager);
       
  6572   if( lockBtreeWithRetry(p)!=SQLITE_OK ){
       
  6573     sqlite3BtreeLeave(p);
       
  6574     return sqlite3StrDup("Unable to acquire a read lock on the database");
       
  6575   }
       
  6576   sCheck.pBt = pBt;
       
  6577   sCheck.pPager = pBt->pPager;
       
  6578   sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
       
  6579   sCheck.mxErr = mxErr;
       
  6580   sCheck.nErr = 0;
       
  6581   *pnErr = 0;
       
  6582 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6583   if( pBt->nTrunc!=0 ){
       
  6584     sCheck.nPage = pBt->nTrunc;
       
  6585   }
       
  6586 #endif
       
  6587   if( sCheck.nPage==0 ){
       
  6588     unlockBtreeIfUnused(pBt);
       
  6589     sqlite3BtreeLeave(p);
       
  6590     return 0;
       
  6591   }
       
  6592   sCheck.anRef = (int*)sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
       
  6593   if( !sCheck.anRef ){
       
  6594     unlockBtreeIfUnused(pBt);
       
  6595     *pnErr = 1;
       
  6596     sqlite3BtreeLeave(p);
       
  6597     return sqlite3MPrintf(p->db, "Unable to malloc %d bytes", 
       
  6598         (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
       
  6599   }
       
  6600   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
       
  6601   i = PENDING_BYTE_PAGE(pBt);
       
  6602   if( i<=sCheck.nPage ){
       
  6603     sCheck.anRef[i] = 1;
       
  6604   }
       
  6605   sCheck.zErrMsg = 0;
       
  6606 
       
  6607   /* Check the integrity of the freelist
       
  6608   */
       
  6609   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
       
  6610             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
       
  6611 
       
  6612   /* Check all the tables.
       
  6613   */
       
  6614   for(i=0; i<nRoot && sCheck.mxErr; i++){
       
  6615     if( aRoot[i]==0 ) continue;
       
  6616 #ifndef SQLITE_OMIT_AUTOVACUUM
       
  6617     if( pBt->autoVacuum && aRoot[i]>1 ){
       
  6618       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
       
  6619     }
       
  6620 #endif
       
  6621     checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
       
  6622   }
       
  6623 
       
  6624   /* Make sure every page in the file is referenced
       
  6625   */
       
  6626   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
       
  6627 #ifdef SQLITE_OMIT_AUTOVACUUM
       
  6628     if( sCheck.anRef[i]==0 ){
       
  6629       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
       
  6630     }
       
  6631 #else
       
  6632     /* If the database supports auto-vacuum, make sure no tables contain
       
  6633     ** references to pointer-map pages.
       
  6634     */
       
  6635     if( sCheck.anRef[i]==0 && 
       
  6636        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
       
  6637       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
       
  6638     }
       
  6639     if( sCheck.anRef[i]!=0 && 
       
  6640        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
       
  6641       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
       
  6642     }
       
  6643 #endif
       
  6644   }
       
  6645 
       
  6646   /* Make sure this analysis did not leave any unref() pages
       
  6647   */
       
  6648   unlockBtreeIfUnused(pBt);
       
  6649   if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
       
  6650     checkAppendMsg(&sCheck, 0, 
       
  6651       "Outstanding page count goes from %d to %d during this analysis",
       
  6652       nRef, sqlite3PagerRefcount(pBt->pPager)
       
  6653     );
       
  6654   }
       
  6655 
       
  6656   /* Clean  up and report errors.
       
  6657   */
       
  6658   sqlite3BtreeLeave(p);
       
  6659   sqlite3_free(sCheck.anRef);
       
  6660   *pnErr = sCheck.nErr;
       
  6661   return sCheck.zErrMsg;
       
  6662 }
       
  6663 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
       
  6664 
       
  6665 /*
       
  6666 ** Return the full pathname of the underlying database file.
       
  6667 **
       
  6668 ** The pager filename is invariant as long as the pager is
       
  6669 ** open so it is safe to access without the BtShared mutex.
       
  6670 */
       
  6671 const char *sqlite3BtreeGetFilename(Btree *p){
       
  6672   assert( p->pBt->pPager!=0 );
       
  6673   return sqlite3PagerFilename(p->pBt->pPager);
       
  6674 }
       
  6675 
       
  6676 /*
       
  6677 ** Return the pathname of the directory that contains the database file.
       
  6678 **
       
  6679 ** The pager directory name is invariant as long as the pager is
       
  6680 ** open so it is safe to access without the BtShared mutex.
       
  6681 */
       
  6682 const char *sqlite3BtreeGetDirname(Btree *p){
       
  6683   assert( p->pBt->pPager!=0 );
       
  6684   return sqlite3PagerDirname(p->pBt->pPager);
       
  6685 }
       
  6686 
       
  6687 /*
       
  6688 ** Return the pathname of the journal file for this database. The return
       
  6689 ** value of this routine is the same regardless of whether the journal file
       
  6690 ** has been created or not.
       
  6691 **
       
  6692 ** The pager journal filename is invariant as long as the pager is
       
  6693 ** open so it is safe to access without the BtShared mutex.
       
  6694 */
       
  6695 const char *sqlite3BtreeGetJournalname(Btree *p){
       
  6696   assert( p->pBt->pPager!=0 );
       
  6697   return sqlite3PagerJournalname(p->pBt->pPager);
       
  6698 }
       
  6699 
       
  6700 #ifndef SQLITE_OMIT_VACUUM
       
  6701 /*
       
  6702 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
       
  6703 ** must be active for both files.
       
  6704 **
       
  6705 ** The size of file pBtFrom may be reduced by this operation.
       
  6706 ** If anything goes wrong, the transaction on pBtFrom is rolled back.
       
  6707 */
       
  6708 static int btreeCopyFile(Btree *pTo, Btree *pFrom){
       
  6709   int rc = SQLITE_OK;
       
  6710   Pgno i, nPage, nToPage, iSkip;
       
  6711 
       
  6712   BtShared *pBtTo = pTo->pBt;
       
  6713   BtShared *pBtFrom = pFrom->pBt;
       
  6714   pBtTo->db = pTo->db;
       
  6715   pBtFrom->db = pFrom->db;
       
  6716   
       
  6717 
       
  6718   if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
       
  6719     return SQLITE_ERROR;
       
  6720   }
       
  6721   if( pBtTo->pCursor ) return SQLITE_BUSY;
       
  6722   nToPage = sqlite3PagerPagecount(pBtTo->pPager);
       
  6723   nPage = sqlite3PagerPagecount(pBtFrom->pPager);
       
  6724   iSkip = PENDING_BYTE_PAGE(pBtTo);
       
  6725   for(i=1; rc==SQLITE_OK && i<=nPage; i++){
       
  6726     DbPage *pDbPage;
       
  6727     if( i==iSkip ) continue;
       
  6728     rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage);
       
  6729     if( rc ) break;
       
  6730     rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage));
       
  6731     sqlite3PagerUnref(pDbPage);
       
  6732   }
       
  6733 
       
  6734   /* If the file is shrinking, journal the pages that are being truncated
       
  6735   ** so that they can be rolled back if the commit fails.
       
  6736   */
       
  6737   for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
       
  6738     DbPage *pDbPage;
       
  6739     if( i==iSkip ) continue;
       
  6740     rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
       
  6741     if( rc ) break;
       
  6742     rc = sqlite3PagerWrite(pDbPage);
       
  6743     sqlite3PagerDontWrite(pDbPage);
       
  6744     /* Yeah.  It seems wierd to call DontWrite() right after Write().  But
       
  6745     ** that is because the names of those procedures do not exactly 
       
  6746     ** represent what they do.  Write() really means "put this page in the
       
  6747     ** rollback journal and mark it as dirty so that it will be written
       
  6748     ** to the database file later."  DontWrite() undoes the second part of
       
  6749     ** that and prevents the page from being written to the database.  The
       
  6750     ** page is still on the rollback journal, though.  And that is the whole
       
  6751     ** point of this loop: to put pages on the rollback journal. */
       
  6752     sqlite3PagerUnref(pDbPage);
       
  6753   }
       
  6754   if( !rc && nPage<nToPage ){
       
  6755     rc = sqlite3PagerTruncate(pBtTo->pPager, nPage);
       
  6756   }
       
  6757 
       
  6758   if( rc ){
       
  6759     sqlite3BtreeRollback(pTo);
       
  6760   }
       
  6761   return rc;  
       
  6762 }
       
  6763 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
       
  6764   int rc;
       
  6765   sqlite3BtreeEnter(pTo);
       
  6766   sqlite3BtreeEnter(pFrom);
       
  6767   rc = btreeCopyFile(pTo, pFrom);
       
  6768   sqlite3BtreeLeave(pFrom);
       
  6769   sqlite3BtreeLeave(pTo);
       
  6770   return rc;
       
  6771 }
       
  6772 
       
  6773 #endif /* SQLITE_OMIT_VACUUM */
       
  6774 
       
  6775 /*
       
  6776 ** Return non-zero if a transaction is active.
       
  6777 */
       
  6778 int sqlite3BtreeIsInTrans(Btree *p){
       
  6779   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
       
  6780   return (p && (p->inTrans==TRANS_WRITE));
       
  6781 }
       
  6782 
       
  6783 /*
       
  6784 ** Return non-zero if a statement transaction is active.
       
  6785 */
       
  6786 int sqlite3BtreeIsInStmt(Btree *p){
       
  6787   assert( sqlite3BtreeHoldsMutex(p) );
       
  6788   return (p->pBt && p->pBt->inStmt);
       
  6789 }
       
  6790 
       
  6791 /*
       
  6792 ** Return non-zero if a read (or write) transaction is active.
       
  6793 */
       
  6794 int sqlite3BtreeIsInReadTrans(Btree *p){
       
  6795   assert( sqlite3_mutex_held(p->db->mutex) );
       
  6796   return (p && (p->inTrans!=TRANS_NONE));
       
  6797 }
       
  6798 
       
  6799 /*
       
  6800 ** This function returns a pointer to a blob of memory associated with
       
  6801 ** a single shared-btree. The memory is used by client code for its own
       
  6802 ** purposes (for example, to store a high-level schema associated with 
       
  6803 ** the shared-btree). The btree layer manages reference counting issues.
       
  6804 **
       
  6805 ** The first time this is called on a shared-btree, nBytes bytes of memory
       
  6806 ** are allocated, zeroed, and returned to the caller. For each subsequent 
       
  6807 ** call the nBytes parameter is ignored and a pointer to the same blob
       
  6808 ** of memory returned. 
       
  6809 **
       
  6810 ** Just before the shared-btree is closed, the function passed as the 
       
  6811 ** xFree argument when the memory allocation was made is invoked on the 
       
  6812 ** blob of allocated memory. This function should not call sqlite3_free()
       
  6813 ** on the memory, the btree layer does that.
       
  6814 */
       
  6815 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
       
  6816   BtShared *pBt = p->pBt;
       
  6817   sqlite3BtreeEnter(p);
       
  6818   if( !pBt->pSchema ){
       
  6819     pBt->pSchema = sqlite3MallocZero(nBytes);
       
  6820     pBt->xFreeSchema = xFree;
       
  6821   }
       
  6822   sqlite3BtreeLeave(p);
       
  6823   return pBt->pSchema;
       
  6824 }
       
  6825 
       
  6826 /*
       
  6827 ** Return true if another user of the same shared btree as the argument
       
  6828 ** handle holds an exclusive lock on the sqlite_master table.
       
  6829 */
       
  6830 int sqlite3BtreeSchemaLocked(Btree *p){
       
  6831   int rc;
       
  6832   assert( sqlite3_mutex_held(p->db->mutex) );
       
  6833   sqlite3BtreeEnter(p);
       
  6834   rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
       
  6835   sqlite3BtreeLeave(p);
       
  6836   return rc;
       
  6837 }
       
  6838 
       
  6839 
       
  6840 #ifndef SQLITE_OMIT_SHARED_CACHE
       
  6841 /*
       
  6842 ** Obtain a lock on the table whose root page is iTab.  The
       
  6843 ** lock is a write lock if isWritelock is true or a read lock
       
  6844 ** if it is false.
       
  6845 */
       
  6846 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
       
  6847   int rc = SQLITE_OK;
       
  6848   u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
       
  6849   sqlite3BtreeEnter(p);
       
  6850   rc = queryTableLock(p, iTab, lockType);
       
  6851   if( rc==SQLITE_OK ){
       
  6852     rc = lockTable(p, iTab, lockType);
       
  6853   }
       
  6854   sqlite3BtreeLeave(p);
       
  6855   return rc;
       
  6856 }
       
  6857 #endif
       
  6858 
       
  6859 #ifndef SQLITE_OMIT_INCRBLOB
       
  6860 /*
       
  6861 ** Argument pCsr must be a cursor opened for writing on an 
       
  6862 ** INTKEY table currently pointing at a valid table entry. 
       
  6863 ** This function modifies the data stored as part of that entry.
       
  6864 ** Only the data content may only be modified, it is not possible
       
  6865 ** to change the length of the data stored.
       
  6866 */
       
  6867 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
       
  6868   assert( cursorHoldsMutex(pCsr) );
       
  6869   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
       
  6870   assert(pCsr->isIncrblobHandle);
       
  6871   if( pCsr->eState>=CURSOR_REQUIRESEEK ){
       
  6872     if( pCsr->eState==CURSOR_FAULT ){
       
  6873       return pCsr->skip;
       
  6874     }else{
       
  6875       return SQLITE_ABORT;
       
  6876     }
       
  6877   }
       
  6878 
       
  6879   /* Check some preconditions: 
       
  6880   **   (a) the cursor is open for writing,
       
  6881   **   (b) there is no read-lock on the table being modified and
       
  6882   **   (c) the cursor points at a valid row of an intKey table.
       
  6883   */
       
  6884   if( !pCsr->wrFlag ){
       
  6885     return SQLITE_READONLY;
       
  6886   }
       
  6887   assert( !pCsr->pBt->readOnly 
       
  6888           && pCsr->pBt->inTransaction==TRANS_WRITE );
       
  6889   if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){
       
  6890     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
       
  6891   }
       
  6892   if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
       
  6893     return SQLITE_ERROR;
       
  6894   }
       
  6895 
       
  6896   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
       
  6897 }
       
  6898 
       
  6899 /* 
       
  6900 ** Set a flag on this cursor to cache the locations of pages from the 
       
  6901 ** overflow list for the current row. This is used by cursors opened
       
  6902 ** for incremental blob IO only.
       
  6903 **
       
  6904 ** This function sets a flag only. The actual page location cache
       
  6905 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
       
  6906 ** accessPayload() (the worker function for sqlite3BtreeData() and
       
  6907 ** sqlite3BtreePutData()).
       
  6908 */
       
  6909 void sqlite3BtreeCacheOverflow(BtCursor *pCur){
       
  6910   assert( cursorHoldsMutex(pCur) );
       
  6911   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
       
  6912   assert(!pCur->isIncrblobHandle);
       
  6913   assert(!pCur->aOverflow);
       
  6914   pCur->isIncrblobHandle = 1;
       
  6915 }
       
  6916 #endif