sl@0: /*
sl@0: ** 2004 April 6
sl@0: **
sl@0: ** The author disclaims copyright to this source code.  In place of
sl@0: ** a legal notice, here is a blessing:
sl@0: **
sl@0: **    May you do good and not evil.
sl@0: **    May you find forgiveness for yourself and forgive others.
sl@0: **    May you share freely, never taking more than you give.
sl@0: **
sl@0: *************************************************************************
sl@0: ** $Id: btree.c,v 1.495 2008/08/02 17:36:46 danielk1977 Exp $
sl@0: **
sl@0: ** This file implements a external (disk-based) database using BTrees.
sl@0: ** See the header comment on "btreeInt.h" for additional information.
sl@0: ** Including a description of file format and an overview of operation.
sl@0: */
sl@0: #include "btreeInt.h"
sl@0: 
sl@0: /*
sl@0: ** The header string that appears at the beginning of every
sl@0: ** SQLite database.
sl@0: */
sl@0: static const char zMagicHeader[] = SQLITE_FILE_HEADER;
sl@0: 
sl@0: /*
sl@0: ** Set this global variable to 1 to enable tracing using the TRACE
sl@0: ** macro.
sl@0: */
sl@0: #if 0
sl@0: int sqlite3BtreeTrace=0;  /* True to enable tracing */
sl@0: # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
sl@0: #else
sl@0: # define TRACE(X)
sl@0: #endif
sl@0: 
sl@0: 
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** A flag to indicate whether or not shared cache is enabled.  Also,
sl@0: ** a list of BtShared objects that are eligible for participation
sl@0: ** in shared cache.  The variables have file scope during normal builds,
sl@0: ** but the test harness needs to access these variables so we make them
sl@0: ** global for test builds.
sl@0: */
sl@0: #ifdef SQLITE_TEST
sl@0: BtShared *sqlite3SharedCacheList = 0;
sl@0: int sqlite3SharedCacheEnabled = 0;
sl@0: #else
sl@0: static BtShared *sqlite3SharedCacheList = 0;
sl@0: static int sqlite3SharedCacheEnabled = 0;
sl@0: #endif
sl@0: #endif /* SQLITE_OMIT_SHARED_CACHE */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** Enable or disable the shared pager and schema features.
sl@0: **
sl@0: ** This routine has no effect on existing database connections.
sl@0: ** The shared cache setting effects only future calls to
sl@0: ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
sl@0: */
sl@0: int sqlite3_enable_shared_cache(int enable){
sl@0:   sqlite3SharedCacheEnabled = enable;
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Forward declaration
sl@0: */
sl@0: static int checkReadLocks(Btree*, Pgno, BtCursor*, i64);
sl@0: 
sl@0: 
sl@0: #ifdef SQLITE_OMIT_SHARED_CACHE
sl@0:   /*
sl@0:   ** The functions queryTableLock(), lockTable() and unlockAllTables()
sl@0:   ** manipulate entries in the BtShared.pLock linked list used to store
sl@0:   ** shared-cache table level locks. If the library is compiled with the
sl@0:   ** shared-cache feature disabled, then there is only ever one user
sl@0:   ** of each BtShared structure and so this locking is not necessary. 
sl@0:   ** So define the lock related functions as no-ops.
sl@0:   */
sl@0:   #define queryTableLock(a,b,c) SQLITE_OK
sl@0:   #define lockTable(a,b,c) SQLITE_OK
sl@0:   #define unlockAllTables(a)
sl@0: #endif
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** Query to see if btree handle p may obtain a lock of type eLock 
sl@0: ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
sl@0: ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
sl@0: ** SQLITE_LOCKED if not.
sl@0: */
sl@0: static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   BtLock *pIter;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
sl@0:   assert( p->db!=0 );
sl@0:   
sl@0:   /* This is a no-op if the shared-cache is not enabled */
sl@0:   if( !p->sharable ){
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0: 
sl@0:   /* If some other connection is holding an exclusive lock, the
sl@0:   ** requested lock may not be obtained.
sl@0:   */
sl@0:   if( pBt->pExclusive && pBt->pExclusive!=p ){
sl@0:     return SQLITE_LOCKED;
sl@0:   }
sl@0: 
sl@0:   /* This (along with lockTable()) is where the ReadUncommitted flag is
sl@0:   ** dealt with. If the caller is querying for a read-lock and the flag is
sl@0:   ** set, it is unconditionally granted - even if there are write-locks
sl@0:   ** on the table. If a write-lock is requested, the ReadUncommitted flag
sl@0:   ** is not considered.
sl@0:   **
sl@0:   ** In function lockTable(), if a read-lock is demanded and the 
sl@0:   ** ReadUncommitted flag is set, no entry is added to the locks list 
sl@0:   ** (BtShared.pLock).
sl@0:   **
sl@0:   ** To summarize: If the ReadUncommitted flag is set, then read cursors do
sl@0:   ** not create or respect table locks. The locking procedure for a 
sl@0:   ** write-cursor does not change.
sl@0:   */
sl@0:   if( 
sl@0:     0==(p->db->flags&SQLITE_ReadUncommitted) || 
sl@0:     eLock==WRITE_LOCK ||
sl@0:     iTab==MASTER_ROOT
sl@0:   ){
sl@0:     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
sl@0:       if( pIter->pBtree!=p && pIter->iTable==iTab && 
sl@0:           (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
sl@0:         return SQLITE_LOCKED;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: #endif /* !SQLITE_OMIT_SHARED_CACHE */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** Add a lock on the table with root-page iTable to the shared-btree used
sl@0: ** by Btree handle p. Parameter eLock must be either READ_LOCK or 
sl@0: ** WRITE_LOCK.
sl@0: **
sl@0: ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
sl@0: ** SQLITE_NOMEM may also be returned.
sl@0: */
sl@0: static int lockTable(Btree *p, Pgno iTable, u8 eLock){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   BtLock *pLock = 0;
sl@0:   BtLock *pIter;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
sl@0:   assert( p->db!=0 );
sl@0: 
sl@0:   /* This is a no-op if the shared-cache is not enabled */
sl@0:   if( !p->sharable ){
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0: 
sl@0:   assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
sl@0: 
sl@0:   /* If the read-uncommitted flag is set and a read-lock is requested,
sl@0:   ** return early without adding an entry to the BtShared.pLock list. See
sl@0:   ** comment in function queryTableLock() for more info on handling 
sl@0:   ** the ReadUncommitted flag.
sl@0:   */
sl@0:   if( 
sl@0:     (p->db->flags&SQLITE_ReadUncommitted) && 
sl@0:     (eLock==READ_LOCK) &&
sl@0:     iTable!=MASTER_ROOT
sl@0:   ){
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0: 
sl@0:   /* First search the list for an existing lock on this table. */
sl@0:   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
sl@0:     if( pIter->iTable==iTable && pIter->pBtree==p ){
sl@0:       pLock = pIter;
sl@0:       break;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* If the above search did not find a BtLock struct associating Btree p
sl@0:   ** with table iTable, allocate one and link it into the list.
sl@0:   */
sl@0:   if( !pLock ){
sl@0:     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
sl@0:     if( !pLock ){
sl@0:       return SQLITE_NOMEM;
sl@0:     }
sl@0:     pLock->iTable = iTable;
sl@0:     pLock->pBtree = p;
sl@0:     pLock->pNext = pBt->pLock;
sl@0:     pBt->pLock = pLock;
sl@0:   }
sl@0: 
sl@0:   /* Set the BtLock.eLock variable to the maximum of the current lock
sl@0:   ** and the requested lock. This means if a write-lock was already held
sl@0:   ** and a read-lock requested, we don't incorrectly downgrade the lock.
sl@0:   */
sl@0:   assert( WRITE_LOCK>READ_LOCK );
sl@0:   if( eLock>pLock->eLock ){
sl@0:     pLock->eLock = eLock;
sl@0:   }
sl@0: 
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: #endif /* !SQLITE_OMIT_SHARED_CACHE */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** Release all the table locks (locks obtained via calls to the lockTable()
sl@0: ** procedure) held by Btree handle p.
sl@0: */
sl@0: static void unlockAllTables(Btree *p){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   BtLock **ppIter = &pBt->pLock;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   assert( p->sharable || 0==*ppIter );
sl@0: 
sl@0:   while( *ppIter ){
sl@0:     BtLock *pLock = *ppIter;
sl@0:     assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree );
sl@0:     if( pLock->pBtree==p ){
sl@0:       *ppIter = pLock->pNext;
sl@0:       sqlite3_free(pLock);
sl@0:     }else{
sl@0:       ppIter = &pLock->pNext;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( pBt->pExclusive==p ){
sl@0:     pBt->pExclusive = 0;
sl@0:   }
sl@0: }
sl@0: #endif /* SQLITE_OMIT_SHARED_CACHE */
sl@0: 
sl@0: static void releasePage(MemPage *pPage);  /* Forward reference */
sl@0: 
sl@0: /*
sl@0: ** Verify that the cursor holds a mutex on the BtShared
sl@0: */
sl@0: #ifndef NDEBUG
sl@0: static int cursorHoldsMutex(BtCursor *p){
sl@0:   return sqlite3_mutex_held(p->pBt->mutex);
sl@0: }
sl@0: #endif
sl@0: 
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0: /*
sl@0: ** Invalidate the overflow page-list cache for cursor pCur, if any.
sl@0: */
sl@0: static void invalidateOverflowCache(BtCursor *pCur){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   sqlite3_free(pCur->aOverflow);
sl@0:   pCur->aOverflow = 0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Invalidate the overflow page-list cache for all cursors opened
sl@0: ** on the shared btree structure pBt.
sl@0: */
sl@0: static void invalidateAllOverflowCache(BtShared *pBt){
sl@0:   BtCursor *p;
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   for(p=pBt->pCursor; p; p=p->pNext){
sl@0:     invalidateOverflowCache(p);
sl@0:   }
sl@0: }
sl@0: #else
sl@0:   #define invalidateOverflowCache(x)
sl@0:   #define invalidateAllOverflowCache(x)
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Save the current cursor position in the variables BtCursor.nKey 
sl@0: ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
sl@0: */
sl@0: static int saveCursorPosition(BtCursor *pCur){
sl@0:   int rc;
sl@0: 
sl@0:   assert( CURSOR_VALID==pCur->eState );
sl@0:   assert( 0==pCur->pKey );
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0: 
sl@0:   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
sl@0: 
sl@0:   /* If this is an intKey table, then the above call to BtreeKeySize()
sl@0:   ** stores the integer key in pCur->nKey. In this case this value is
sl@0:   ** all that is required. Otherwise, if pCur is not open on an intKey
sl@0:   ** table, then malloc space for and store the pCur->nKey bytes of key 
sl@0:   ** data.
sl@0:   */
sl@0:   if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
sl@0:     void *pKey = sqlite3Malloc(pCur->nKey);
sl@0:     if( pKey ){
sl@0:       rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         pCur->pKey = pKey;
sl@0:       }else{
sl@0:         sqlite3_free(pKey);
sl@0:       }
sl@0:     }else{
sl@0:       rc = SQLITE_NOMEM;
sl@0:     }
sl@0:   }
sl@0:   assert( !pCur->pPage->intKey || !pCur->pKey );
sl@0: 
sl@0:   if( rc==SQLITE_OK ){
sl@0:     releasePage(pCur->pPage);
sl@0:     pCur->pPage = 0;
sl@0:     pCur->eState = CURSOR_REQUIRESEEK;
sl@0:   }
sl@0: 
sl@0:   invalidateOverflowCache(pCur);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Save the positions of all cursors except pExcept open on the table 
sl@0: ** with root-page iRoot. Usually, this is called just before cursor
sl@0: ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
sl@0: */
sl@0: static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
sl@0:   BtCursor *p;
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   assert( pExcept==0 || pExcept->pBt==pBt );
sl@0:   for(p=pBt->pCursor; p; p=p->pNext){
sl@0:     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && 
sl@0:         p->eState==CURSOR_VALID ){
sl@0:       int rc = saveCursorPosition(p);
sl@0:       if( SQLITE_OK!=rc ){
sl@0:         return rc;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Clear the current cursor position.
sl@0: */
sl@0: static void clearCursorPosition(BtCursor *pCur){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   sqlite3_free(pCur->pKey);
sl@0:   pCur->pKey = 0;
sl@0:   pCur->eState = CURSOR_INVALID;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Restore the cursor to the position it was in (or as close to as possible)
sl@0: ** when saveCursorPosition() was called. Note that this call deletes the 
sl@0: ** saved position info stored by saveCursorPosition(), so there can be
sl@0: ** at most one effective restoreCursorPosition() call after each 
sl@0: ** saveCursorPosition().
sl@0: */
sl@0: int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){
sl@0:   int rc;
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pCur->eState>=CURSOR_REQUIRESEEK );
sl@0:   if( pCur->eState==CURSOR_FAULT ){
sl@0:     return pCur->skip;
sl@0:   }
sl@0:   pCur->eState = CURSOR_INVALID;
sl@0:   rc = sqlite3BtreeMoveto(pCur, pCur->pKey, 0, pCur->nKey, 0, &pCur->skip);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     sqlite3_free(pCur->pKey);
sl@0:     pCur->pKey = 0;
sl@0:     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: #define restoreCursorPosition(p) \
sl@0:   (p->eState>=CURSOR_REQUIRESEEK ? \
sl@0:          sqlite3BtreeRestoreCursorPosition(p) : \
sl@0:          SQLITE_OK)
sl@0: 
sl@0: /*
sl@0: ** Determine whether or not a cursor has moved from the position it
sl@0: ** was last placed at.  Cursor can move when the row they are pointing
sl@0: ** at is deleted out from under them.
sl@0: **
sl@0: ** This routine returns an error code if something goes wrong.  The
sl@0: ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
sl@0: */
sl@0: int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
sl@0:   int rc;
sl@0: 
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc ){
sl@0:     *pHasMoved = 1;
sl@0:     return rc;
sl@0:   }
sl@0:   if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){
sl@0:     *pHasMoved = 1;
sl@0:   }else{
sl@0:     *pHasMoved = 0;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0: /*
sl@0: ** Given a page number of a regular database page, return the page
sl@0: ** number for the pointer-map page that contains the entry for the
sl@0: ** input page number.
sl@0: */
sl@0: static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
sl@0:   int nPagesPerMapPage, iPtrMap, ret;
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   nPagesPerMapPage = (pBt->usableSize/5)+1;
sl@0:   iPtrMap = (pgno-2)/nPagesPerMapPage;
sl@0:   ret = (iPtrMap*nPagesPerMapPage) + 2; 
sl@0:   if( ret==PENDING_BYTE_PAGE(pBt) ){
sl@0:     ret++;
sl@0:   }
sl@0:   return ret;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Write an entry into the pointer map.
sl@0: **
sl@0: ** This routine updates the pointer map entry for page number 'key'
sl@0: ** so that it maps to type 'eType' and parent page number 'pgno'.
sl@0: ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
sl@0: */
sl@0: static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
sl@0:   DbPage *pDbPage;  /* The pointer map page */
sl@0:   u8 *pPtrmap;      /* The pointer map data */
sl@0:   Pgno iPtrmap;     /* The pointer map page number */
sl@0:   int offset;       /* Offset in pointer map page */
sl@0:   int rc;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   /* The master-journal page number must never be used as a pointer map page */
sl@0:   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
sl@0: 
sl@0:   assert( pBt->autoVacuum );
sl@0:   if( key==0 ){
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   iPtrmap = PTRMAP_PAGENO(pBt, key);
sl@0:   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     return rc;
sl@0:   }
sl@0:   offset = PTRMAP_PTROFFSET(iPtrmap, key);
sl@0:   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
sl@0: 
sl@0:   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
sl@0:     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
sl@0:     rc = sqlite3PagerWrite(pDbPage);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       pPtrmap[offset] = eType;
sl@0:       put4byte(&pPtrmap[offset+1], parent);
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   sqlite3PagerUnref(pDbPage);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Read an entry from the pointer map.
sl@0: **
sl@0: ** This routine retrieves the pointer map entry for page 'key', writing
sl@0: ** the type and parent page number to *pEType and *pPgno respectively.
sl@0: ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
sl@0: */
sl@0: static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
sl@0:   DbPage *pDbPage;   /* The pointer map page */
sl@0:   int iPtrmap;       /* Pointer map page index */
sl@0:   u8 *pPtrmap;       /* Pointer map page data */
sl@0:   int offset;        /* Offset of entry in pointer map */
sl@0:   int rc;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0: 
sl@0:   iPtrmap = PTRMAP_PAGENO(pBt, key);
sl@0:   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
sl@0:   if( rc!=0 ){
sl@0:     return rc;
sl@0:   }
sl@0:   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
sl@0: 
sl@0:   offset = PTRMAP_PTROFFSET(iPtrmap, key);
sl@0:   assert( pEType!=0 );
sl@0:   *pEType = pPtrmap[offset];
sl@0:   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
sl@0: 
sl@0:   sqlite3PagerUnref(pDbPage);
sl@0:   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: #else /* if defined SQLITE_OMIT_AUTOVACUUM */
sl@0:   #define ptrmapPut(w,x,y,z) SQLITE_OK
sl@0:   #define ptrmapGet(w,x,y,z) SQLITE_OK
sl@0:   #define ptrmapPutOvfl(y,z) SQLITE_OK
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Given a btree page and a cell index (0 means the first cell on
sl@0: ** the page, 1 means the second cell, and so forth) return a pointer
sl@0: ** to the cell content.
sl@0: **
sl@0: ** This routine works only for pages that do not contain overflow cells.
sl@0: */
sl@0: #define findCell(P,I) \
sl@0:   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
sl@0: 
sl@0: /*
sl@0: ** This a more complex version of findCell() that works for
sl@0: ** pages that do contain overflow cells.  See insert
sl@0: */
sl@0: static u8 *findOverflowCell(MemPage *pPage, int iCell){
sl@0:   int i;
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   for(i=pPage->nOverflow-1; i>=0; i--){
sl@0:     int k;
sl@0:     struct _OvflCell *pOvfl;
sl@0:     pOvfl = &pPage->aOvfl[i];
sl@0:     k = pOvfl->idx;
sl@0:     if( k<=iCell ){
sl@0:       if( k==iCell ){
sl@0:         return pOvfl->pCell;
sl@0:       }
sl@0:       iCell--;
sl@0:     }
sl@0:   }
sl@0:   return findCell(pPage, iCell);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Parse a cell content block and fill in the CellInfo structure.  There
sl@0: ** are two versions of this function.  sqlite3BtreeParseCell() takes a 
sl@0: ** cell index as the second argument and sqlite3BtreeParseCellPtr() 
sl@0: ** takes a pointer to the body of the cell as its second argument.
sl@0: **
sl@0: ** Within this file, the parseCell() macro can be called instead of
sl@0: ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
sl@0: */
sl@0: void sqlite3BtreeParseCellPtr(
sl@0:   MemPage *pPage,         /* Page containing the cell */
sl@0:   u8 *pCell,              /* Pointer to the cell text. */
sl@0:   CellInfo *pInfo         /* Fill in this structure */
sl@0: ){
sl@0:   int n;                  /* Number bytes in cell content header */
sl@0:   u32 nPayload;           /* Number of bytes of cell payload */
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0: 
sl@0:   pInfo->pCell = pCell;
sl@0:   assert( pPage->leaf==0 || pPage->leaf==1 );
sl@0:   n = pPage->childPtrSize;
sl@0:   assert( n==4-4*pPage->leaf );
sl@0:   if( pPage->intKey ){
sl@0:     if( pPage->hasData ){
sl@0:       n += getVarint32(&pCell[n], nPayload);
sl@0:     }else{
sl@0:       nPayload = 0;
sl@0:     }
sl@0:     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
sl@0:     pInfo->nData = nPayload;
sl@0:   }else{
sl@0:     pInfo->nData = 0;
sl@0:     n += getVarint32(&pCell[n], nPayload);
sl@0:     pInfo->nKey = nPayload;
sl@0:   }
sl@0:   pInfo->nPayload = nPayload;
sl@0:   pInfo->nHeader = n;
sl@0:   if( likely(nPayload<=pPage->maxLocal) ){
sl@0:     /* This is the (easy) common case where the entire payload fits
sl@0:     ** on the local page.  No overflow is required.
sl@0:     */
sl@0:     int nSize;          /* Total size of cell content in bytes */
sl@0:     nSize = nPayload + n;
sl@0:     pInfo->nLocal = nPayload;
sl@0:     pInfo->iOverflow = 0;
sl@0:     if( (nSize & ~3)==0 ){
sl@0:       nSize = 4;        /* Minimum cell size is 4 */
sl@0:     }
sl@0:     pInfo->nSize = nSize;
sl@0:   }else{
sl@0:     /* If the payload will not fit completely on the local page, we have
sl@0:     ** to decide how much to store locally and how much to spill onto
sl@0:     ** overflow pages.  The strategy is to minimize the amount of unused
sl@0:     ** space on overflow pages while keeping the amount of local storage
sl@0:     ** in between minLocal and maxLocal.
sl@0:     **
sl@0:     ** Warning:  changing the way overflow payload is distributed in any
sl@0:     ** way will result in an incompatible file format.
sl@0:     */
sl@0:     int minLocal;  /* Minimum amount of payload held locally */
sl@0:     int maxLocal;  /* Maximum amount of payload held locally */
sl@0:     int surplus;   /* Overflow payload available for local storage */
sl@0: 
sl@0:     minLocal = pPage->minLocal;
sl@0:     maxLocal = pPage->maxLocal;
sl@0:     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
sl@0:     if( surplus <= maxLocal ){
sl@0:       pInfo->nLocal = surplus;
sl@0:     }else{
sl@0:       pInfo->nLocal = minLocal;
sl@0:     }
sl@0:     pInfo->iOverflow = pInfo->nLocal + n;
sl@0:     pInfo->nSize = pInfo->iOverflow + 4;
sl@0:   }
sl@0: }
sl@0: #define parseCell(pPage, iCell, pInfo) \
sl@0:   sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
sl@0: void sqlite3BtreeParseCell(
sl@0:   MemPage *pPage,         /* Page containing the cell */
sl@0:   int iCell,              /* The cell index.  First cell is 0 */
sl@0:   CellInfo *pInfo         /* Fill in this structure */
sl@0: ){
sl@0:   parseCell(pPage, iCell, pInfo);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Compute the total number of bytes that a Cell needs in the cell
sl@0: ** data area of the btree-page.  The return number includes the cell
sl@0: ** data header and the local payload, but not any overflow page or
sl@0: ** the space used by the cell pointer.
sl@0: */
sl@0: #ifndef NDEBUG
sl@0: static u16 cellSize(MemPage *pPage, int iCell){
sl@0:   CellInfo info;
sl@0:   sqlite3BtreeParseCell(pPage, iCell, &info);
sl@0:   return info.nSize;
sl@0: }
sl@0: #endif
sl@0: static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
sl@0:   CellInfo info;
sl@0:   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:   return info.nSize;
sl@0: }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0: /*
sl@0: ** If the cell pCell, part of page pPage contains a pointer
sl@0: ** to an overflow page, insert an entry into the pointer-map
sl@0: ** for the overflow page.
sl@0: */
sl@0: static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
sl@0:   CellInfo info;
sl@0:   assert( pCell!=0 );
sl@0:   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
sl@0:   if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
sl@0:     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
sl@0:     return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: /*
sl@0: ** If the cell with index iCell on page pPage contains a pointer
sl@0: ** to an overflow page, insert an entry into the pointer-map
sl@0: ** for the overflow page.
sl@0: */
sl@0: static int ptrmapPutOvfl(MemPage *pPage, int iCell){
sl@0:   u8 *pCell;
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   pCell = findOverflowCell(pPage, iCell);
sl@0:   return ptrmapPutOvflPtr(pPage, pCell);
sl@0: }
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Defragment the page given.  All Cells are moved to the
sl@0: ** end of the page and all free space is collected into one
sl@0: ** big FreeBlk that occurs in between the header and cell
sl@0: ** pointer array and the cell content area.
sl@0: */
sl@0: static void defragmentPage(MemPage *pPage){
sl@0:   int i;                     /* Loop counter */
sl@0:   int pc;                    /* Address of a i-th cell */
sl@0:   int addr;                  /* Offset of first byte after cell pointer array */
sl@0:   int hdr;                   /* Offset to the page header */
sl@0:   int size;                  /* Size of a cell */
sl@0:   int usableSize;            /* Number of usable bytes on a page */
sl@0:   int cellOffset;            /* Offset to the cell pointer array */
sl@0:   int brk;                   /* Offset to the cell content area */
sl@0:   int nCell;                 /* Number of cells on the page */
sl@0:   unsigned char *data;       /* The page data */
sl@0:   unsigned char *temp;       /* Temp area for cell content */
sl@0: 
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:   assert( pPage->pBt!=0 );
sl@0:   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
sl@0:   assert( pPage->nOverflow==0 );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
sl@0:   data = pPage->aData;
sl@0:   hdr = pPage->hdrOffset;
sl@0:   cellOffset = pPage->cellOffset;
sl@0:   nCell = pPage->nCell;
sl@0:   assert( nCell==get2byte(&data[hdr+3]) );
sl@0:   usableSize = pPage->pBt->usableSize;
sl@0:   brk = get2byte(&data[hdr+5]);
sl@0:   memcpy(&temp[brk], &data[brk], usableSize - brk);
sl@0:   brk = usableSize;
sl@0:   for(i=0; i<nCell; i++){
sl@0:     u8 *pAddr;     /* The i-th cell pointer */
sl@0:     pAddr = &data[cellOffset + i*2];
sl@0:     pc = get2byte(pAddr);
sl@0:     assert( pc<pPage->pBt->usableSize );
sl@0:     size = cellSizePtr(pPage, &temp[pc]);
sl@0:     brk -= size;
sl@0:     memcpy(&data[brk], &temp[pc], size);
sl@0:     put2byte(pAddr, brk);
sl@0:   }
sl@0:   assert( brk>=cellOffset+2*nCell );
sl@0:   put2byte(&data[hdr+5], brk);
sl@0:   data[hdr+1] = 0;
sl@0:   data[hdr+2] = 0;
sl@0:   data[hdr+7] = 0;
sl@0:   addr = cellOffset+2*nCell;
sl@0:   memset(&data[addr], 0, brk-addr);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Allocate nByte bytes of space on a page.
sl@0: **
sl@0: ** Return the index into pPage->aData[] of the first byte of
sl@0: ** the new allocation.  The caller guarantees that there is enough
sl@0: ** space.  This routine will never fail.
sl@0: **
sl@0: ** If the page contains nBytes of free space but does not contain
sl@0: ** nBytes of contiguous free space, then this routine automatically
sl@0: ** calls defragementPage() to consolidate all free space before 
sl@0: ** allocating the new chunk.
sl@0: */
sl@0: static int allocateSpace(MemPage *pPage, int nByte){
sl@0:   int addr, pc, hdr;
sl@0:   int size;
sl@0:   int nFrag;
sl@0:   int top;
sl@0:   int nCell;
sl@0:   int cellOffset;
sl@0:   unsigned char *data;
sl@0:   
sl@0:   data = pPage->aData;
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:   assert( pPage->pBt );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   assert( nByte>=0 );  /* Minimum cell size is 4 */
sl@0:   assert( pPage->nFree>=nByte );
sl@0:   assert( pPage->nOverflow==0 );
sl@0:   pPage->nFree -= nByte;
sl@0:   hdr = pPage->hdrOffset;
sl@0: 
sl@0:   nFrag = data[hdr+7];
sl@0:   if( nFrag<60 ){
sl@0:     /* Search the freelist looking for a slot big enough to satisfy the
sl@0:     ** space request. */
sl@0:     addr = hdr+1;
sl@0:     while( (pc = get2byte(&data[addr]))>0 ){
sl@0:       size = get2byte(&data[pc+2]);
sl@0:       if( size>=nByte ){
sl@0:         if( size<nByte+4 ){
sl@0:           memcpy(&data[addr], &data[pc], 2);
sl@0:           data[hdr+7] = nFrag + size - nByte;
sl@0:           return pc;
sl@0:         }else{
sl@0:           put2byte(&data[pc+2], size-nByte);
sl@0:           return pc + size - nByte;
sl@0:         }
sl@0:       }
sl@0:       addr = pc;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Allocate memory from the gap in between the cell pointer array
sl@0:   ** and the cell content area.
sl@0:   */
sl@0:   top = get2byte(&data[hdr+5]);
sl@0:   nCell = get2byte(&data[hdr+3]);
sl@0:   cellOffset = pPage->cellOffset;
sl@0:   if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
sl@0:     defragmentPage(pPage);
sl@0:     top = get2byte(&data[hdr+5]);
sl@0:   }
sl@0:   top -= nByte;
sl@0:   assert( cellOffset + 2*nCell <= top );
sl@0:   put2byte(&data[hdr+5], top);
sl@0:   return top;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return a section of the pPage->aData to the freelist.
sl@0: ** The first byte of the new free block is pPage->aDisk[start]
sl@0: ** and the size of the block is "size" bytes.
sl@0: **
sl@0: ** Most of the effort here is involved in coalesing adjacent
sl@0: ** free blocks into a single big free block.
sl@0: */
sl@0: static void freeSpace(MemPage *pPage, int start, int size){
sl@0:   int addr, pbegin, hdr;
sl@0:   unsigned char *data = pPage->aData;
sl@0: 
sl@0:   assert( pPage->pBt!=0 );
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:   assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
sl@0:   assert( (start + size)<=pPage->pBt->usableSize );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   assert( size>=0 );   /* Minimum cell size is 4 */
sl@0: 
sl@0: #ifdef SQLITE_SECURE_DELETE
sl@0:   /* Overwrite deleted information with zeros when the SECURE_DELETE 
sl@0:   ** option is enabled at compile-time */
sl@0:   memset(&data[start], 0, size);
sl@0: #endif
sl@0: 
sl@0:   /* Add the space back into the linked list of freeblocks */
sl@0:   hdr = pPage->hdrOffset;
sl@0:   addr = hdr + 1;
sl@0:   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
sl@0:     assert( pbegin<=pPage->pBt->usableSize-4 );
sl@0:     assert( pbegin>addr );
sl@0:     addr = pbegin;
sl@0:   }
sl@0:   assert( pbegin<=pPage->pBt->usableSize-4 );
sl@0:   assert( pbegin>addr || pbegin==0 );
sl@0:   put2byte(&data[addr], start);
sl@0:   put2byte(&data[start], pbegin);
sl@0:   put2byte(&data[start+2], size);
sl@0:   pPage->nFree += size;
sl@0: 
sl@0:   /* Coalesce adjacent free blocks */
sl@0:   addr = pPage->hdrOffset + 1;
sl@0:   while( (pbegin = get2byte(&data[addr]))>0 ){
sl@0:     int pnext, psize;
sl@0:     assert( pbegin>addr );
sl@0:     assert( pbegin<=pPage->pBt->usableSize-4 );
sl@0:     pnext = get2byte(&data[pbegin]);
sl@0:     psize = get2byte(&data[pbegin+2]);
sl@0:     if( pbegin + psize + 3 >= pnext && pnext>0 ){
sl@0:       int frag = pnext - (pbegin+psize);
sl@0:       assert( frag<=data[pPage->hdrOffset+7] );
sl@0:       data[pPage->hdrOffset+7] -= frag;
sl@0:       put2byte(&data[pbegin], get2byte(&data[pnext]));
sl@0:       put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
sl@0:     }else{
sl@0:       addr = pbegin;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* If the cell content area begins with a freeblock, remove it. */
sl@0:   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
sl@0:     int top;
sl@0:     pbegin = get2byte(&data[hdr+1]);
sl@0:     memcpy(&data[hdr+1], &data[pbegin], 2);
sl@0:     top = get2byte(&data[hdr+5]);
sl@0:     put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Decode the flags byte (the first byte of the header) for a page
sl@0: ** and initialize fields of the MemPage structure accordingly.
sl@0: **
sl@0: ** Only the following combinations are supported.  Anything different
sl@0: ** indicates a corrupt database files:
sl@0: **
sl@0: **         PTF_ZERODATA
sl@0: **         PTF_ZERODATA | PTF_LEAF
sl@0: **         PTF_LEAFDATA | PTF_INTKEY
sl@0: **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
sl@0: */
sl@0: static int decodeFlags(MemPage *pPage, int flagByte){
sl@0:   BtShared *pBt;     /* A copy of pPage->pBt */
sl@0: 
sl@0:   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   pPage->leaf = flagByte>>3;  assert( PTF_LEAF == 1<<3 );
sl@0:   flagByte &= ~PTF_LEAF;
sl@0:   pPage->childPtrSize = 4-4*pPage->leaf;
sl@0:   pBt = pPage->pBt;
sl@0:   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
sl@0:     pPage->intKey = 1;
sl@0:     pPage->hasData = pPage->leaf;
sl@0:     pPage->maxLocal = pBt->maxLeaf;
sl@0:     pPage->minLocal = pBt->minLeaf;
sl@0:   }else if( flagByte==PTF_ZERODATA ){
sl@0:     pPage->intKey = 0;
sl@0:     pPage->hasData = 0;
sl@0:     pPage->maxLocal = pBt->maxLocal;
sl@0:     pPage->minLocal = pBt->minLocal;
sl@0:   }else{
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Initialize the auxiliary information for a disk block.
sl@0: **
sl@0: ** The pParent parameter must be a pointer to the MemPage which
sl@0: ** is the parent of the page being initialized.  The root of a
sl@0: ** BTree has no parent and so for that page, pParent==NULL.
sl@0: **
sl@0: ** Return SQLITE_OK on success.  If we see that the page does
sl@0: ** not contain a well-formed database page, then return 
sl@0: ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
sl@0: ** guarantee that the page is well-formed.  It only shows that
sl@0: ** we failed to detect any corruption.
sl@0: */
sl@0: int sqlite3BtreeInitPage(
sl@0:   MemPage *pPage,        /* The page to be initialized */
sl@0:   MemPage *pParent       /* The parent.  Might be NULL */
sl@0: ){
sl@0:   int pc;            /* Address of a freeblock within pPage->aData[] */
sl@0:   int hdr;           /* Offset to beginning of page header */
sl@0:   u8 *data;          /* Equal to pPage->aData */
sl@0:   BtShared *pBt;        /* The main btree structure */
sl@0:   int usableSize;    /* Amount of usable space on each page */
sl@0:   int cellOffset;    /* Offset from start of page to first cell pointer */
sl@0:   int nFree;         /* Number of unused bytes on the page */
sl@0:   int top;           /* First byte of the cell content area */
sl@0: 
sl@0:   pBt = pPage->pBt;
sl@0:   assert( pBt!=0 );
sl@0:   assert( pParent==0 || pParent->pBt==pBt );
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
sl@0:   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
sl@0:   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
sl@0:   if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
sl@0:     /* The parent page should never change unless the file is corrupt */
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   if( pPage->isInit ) return SQLITE_OK;
sl@0:   if( pPage->pParent==0 && pParent!=0 ){
sl@0:     pPage->pParent = pParent;
sl@0:     sqlite3PagerRef(pParent->pDbPage);
sl@0:   }
sl@0:   hdr = pPage->hdrOffset;
sl@0:   data = pPage->aData;
sl@0:   if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
sl@0:   assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
sl@0:   pPage->maskPage = pBt->pageSize - 1;
sl@0:   pPage->nOverflow = 0;
sl@0:   pPage->idxShift = 0;
sl@0:   usableSize = pBt->usableSize;
sl@0:   pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
sl@0:   top = get2byte(&data[hdr+5]);
sl@0:   pPage->nCell = get2byte(&data[hdr+3]);
sl@0:   if( pPage->nCell>MX_CELL(pBt) ){
sl@0:     /* To many cells for a single page.  The page must be corrupt */
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
sl@0:     /* All pages must have at least one cell, except for root pages */
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0: 
sl@0:   /* Compute the total free space on the page */
sl@0:   pc = get2byte(&data[hdr+1]);
sl@0:   nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
sl@0:   while( pc>0 ){
sl@0:     int next, size;
sl@0:     if( pc>usableSize-4 ){
sl@0:       /* Free block is off the page */
sl@0:       return SQLITE_CORRUPT_BKPT; 
sl@0:     }
sl@0:     next = get2byte(&data[pc]);
sl@0:     size = get2byte(&data[pc+2]);
sl@0:     if( next>0 && next<=pc+size+3 ){
sl@0:       /* Free blocks must be in accending order */
sl@0:       return SQLITE_CORRUPT_BKPT; 
sl@0:     }
sl@0:     nFree += size;
sl@0:     pc = next;
sl@0:   }
sl@0:   pPage->nFree = nFree;
sl@0:   if( nFree>=usableSize ){
sl@0:     /* Free space cannot exceed total page size */
sl@0:     return SQLITE_CORRUPT_BKPT; 
sl@0:   }
sl@0: 
sl@0: #if 0
sl@0:   /* Check that all the offsets in the cell offset array are within range. 
sl@0:   ** 
sl@0:   ** Omitting this consistency check and using the pPage->maskPage mask
sl@0:   ** to prevent overrunning the page buffer in findCell() results in a
sl@0:   ** 2.5% performance gain.
sl@0:   */
sl@0:   {
sl@0:     u8 *pOff;        /* Iterator used to check all cell offsets are in range */
sl@0:     u8 *pEnd;        /* Pointer to end of cell offset array */
sl@0:     u8 mask;         /* Mask of bits that must be zero in MSB of cell offsets */
sl@0:     mask = ~(((u8)(pBt->pageSize>>8))-1);
sl@0:     pEnd = &data[cellOffset + pPage->nCell*2];
sl@0:     for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2);
sl@0:     if( pOff!=pEnd ){
sl@0:       return SQLITE_CORRUPT_BKPT;
sl@0:     }
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   pPage->isInit = 1;
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Set up a raw page so that it looks like a database page holding
sl@0: ** no entries.
sl@0: */
sl@0: static void zeroPage(MemPage *pPage, int flags){
sl@0:   unsigned char *data = pPage->aData;
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   int hdr = pPage->hdrOffset;
sl@0:   int first;
sl@0: 
sl@0:   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
sl@0:   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
sl@0:   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/
sl@0:   data[hdr] = flags;
sl@0:   first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
sl@0:   memset(&data[hdr+1], 0, 4);
sl@0:   data[hdr+7] = 0;
sl@0:   put2byte(&data[hdr+5], pBt->usableSize);
sl@0:   pPage->nFree = pBt->usableSize - first;
sl@0:   decodeFlags(pPage, flags);
sl@0:   pPage->hdrOffset = hdr;
sl@0:   pPage->cellOffset = first;
sl@0:   pPage->nOverflow = 0;
sl@0:   assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
sl@0:   pPage->maskPage = pBt->pageSize - 1;
sl@0:   pPage->idxShift = 0;
sl@0:   pPage->nCell = 0;
sl@0:   pPage->isInit = 1;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Get a page from the pager.  Initialize the MemPage.pBt and
sl@0: ** MemPage.aData elements if needed.
sl@0: **
sl@0: ** If the noContent flag is set, it means that we do not care about
sl@0: ** the content of the page at this time.  So do not go to the disk
sl@0: ** to fetch the content.  Just fill in the content with zeros for now.
sl@0: ** If in the future we call sqlite3PagerWrite() on this page, that
sl@0: ** means we have started to be concerned about content and the disk
sl@0: ** read should occur at that point.
sl@0: */
sl@0: int sqlite3BtreeGetPage(
sl@0:   BtShared *pBt,       /* The btree */
sl@0:   Pgno pgno,           /* Number of the page to fetch */
sl@0:   MemPage **ppPage,    /* Return the page in this parameter */
sl@0:   int noContent        /* Do not load page content if true */
sl@0: ){
sl@0:   int rc;
sl@0:   MemPage *pPage;
sl@0:   DbPage *pDbPage;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
sl@0:   if( rc ) return rc;
sl@0:   pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
sl@0:   pPage->aData = sqlite3PagerGetData(pDbPage);
sl@0:   pPage->pDbPage = pDbPage;
sl@0:   pPage->pBt = pBt;
sl@0:   pPage->pgno = pgno;
sl@0:   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
sl@0:   *ppPage = pPage;
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Get a page from the pager and initialize it.  This routine
sl@0: ** is just a convenience wrapper around separate calls to
sl@0: ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
sl@0: */
sl@0: static int getAndInitPage(
sl@0:   BtShared *pBt,          /* The database file */
sl@0:   Pgno pgno,           /* Number of the page to get */
sl@0:   MemPage **ppPage,    /* Write the page pointer here */
sl@0:   MemPage *pParent     /* Parent of the page */
sl@0: ){
sl@0:   int rc;
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   if( pgno==0 ){
sl@0:     return SQLITE_CORRUPT_BKPT; 
sl@0:   }
sl@0:   rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
sl@0:   if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
sl@0:     rc = sqlite3BtreeInitPage(*ppPage, pParent);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       releasePage(*ppPage);
sl@0:       *ppPage = 0;
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Release a MemPage.  This should be called once for each prior
sl@0: ** call to sqlite3BtreeGetPage.
sl@0: */
sl@0: static void releasePage(MemPage *pPage){
sl@0:   if( pPage ){
sl@0:     assert( pPage->aData );
sl@0:     assert( pPage->pBt );
sl@0:     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
sl@0:     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
sl@0:     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:     sqlite3PagerUnref(pPage->pDbPage);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine is called when the reference count for a page
sl@0: ** reaches zero.  We need to unref the pParent pointer when that
sl@0: ** happens.
sl@0: */
sl@0: static void pageDestructor(DbPage *pData, int pageSize){
sl@0:   MemPage *pPage;
sl@0:   assert( (pageSize & 7)==0 );
sl@0:   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
sl@0:   assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   if( pPage->pParent ){
sl@0:     MemPage *pParent = pPage->pParent;
sl@0:     assert( pParent->pBt==pPage->pBt );
sl@0:     pPage->pParent = 0;
sl@0:     releasePage(pParent);
sl@0:   }
sl@0:   pPage->isInit = 0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** During a rollback, when the pager reloads information into the cache
sl@0: ** so that the cache is restored to its original state at the start of
sl@0: ** the transaction, for each page restored this routine is called.
sl@0: **
sl@0: ** This routine needs to reset the extra data section at the end of the
sl@0: ** page to agree with the restored data.
sl@0: */
sl@0: static void pageReinit(DbPage *pData, int pageSize){
sl@0:   MemPage *pPage;
sl@0:   assert( (pageSize & 7)==0 );
sl@0:   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
sl@0:   if( pPage->isInit ){
sl@0:     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:     pPage->isInit = 0;
sl@0:     sqlite3BtreeInitPage(pPage, pPage->pParent);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Invoke the busy handler for a btree.
sl@0: */
sl@0: static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
sl@0:   BtShared *pBt = (BtShared*)pArg;
sl@0:   assert( pBt->db );
sl@0:   assert( sqlite3_mutex_held(pBt->db->mutex) );
sl@0:   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Open a database file.
sl@0: ** 
sl@0: ** zFilename is the name of the database file.  If zFilename is NULL
sl@0: ** a new database with a random name is created.  This randomly named
sl@0: ** database file will be deleted when sqlite3BtreeClose() is called.
sl@0: ** If zFilename is ":memory:" then an in-memory database is created
sl@0: ** that is automatically destroyed when it is closed.
sl@0: */
sl@0: int sqlite3BtreeOpen(
sl@0:   const char *zFilename,  /* Name of the file containing the BTree database */
sl@0:   sqlite3 *db,            /* Associated database handle */
sl@0:   Btree **ppBtree,        /* Pointer to new Btree object written here */
sl@0:   int flags,              /* Options */
sl@0:   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
sl@0: ){
sl@0:   sqlite3_vfs *pVfs;      /* The VFS to use for this btree */
sl@0:   BtShared *pBt = 0;      /* Shared part of btree structure */
sl@0:   Btree *p;               /* Handle to return */
sl@0:   int rc = SQLITE_OK;
sl@0:   int nReserve;
sl@0:   unsigned char zDbHeader[100];
sl@0: 
sl@0:   /* Set the variable isMemdb to true for an in-memory database, or 
sl@0:   ** false for a file-based database. This symbol is only required if
sl@0:   ** either of the shared-data or autovacuum features are compiled 
sl@0:   ** into the library.
sl@0:   */
sl@0: #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
sl@0:   #ifdef SQLITE_OMIT_MEMORYDB
sl@0:     const int isMemdb = 0;
sl@0:   #else
sl@0:     const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
sl@0:   #endif
sl@0: #endif
sl@0: 
sl@0:   assert( db!=0 );
sl@0:   assert( sqlite3_mutex_held(db->mutex) );
sl@0: 
sl@0:   pVfs = db->pVfs;
sl@0:   p = sqlite3MallocZero(sizeof(Btree));
sl@0:   if( !p ){
sl@0:     return SQLITE_NOMEM;
sl@0:   }
sl@0:   p->inTrans = TRANS_NONE;
sl@0:   p->db = db;
sl@0: 
sl@0: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
sl@0:   /*
sl@0:   ** If this Btree is a candidate for shared cache, try to find an
sl@0:   ** existing BtShared object that we can share with
sl@0:   */
sl@0:   if( isMemdb==0
sl@0:    && (db->flags & SQLITE_Vtab)==0
sl@0:    && zFilename && zFilename[0]
sl@0:   ){
sl@0:     if( sqlite3SharedCacheEnabled ){
sl@0:       int nFullPathname = pVfs->mxPathname+1;
sl@0:       char *zFullPathname = sqlite3Malloc(nFullPathname);
sl@0:       sqlite3_mutex *mutexShared;
sl@0:       p->sharable = 1;
sl@0:       db->flags |= SQLITE_SharedCache;
sl@0:       if( !zFullPathname ){
sl@0:         sqlite3_free(p);
sl@0:         return SQLITE_NOMEM;
sl@0:       }
sl@0:       sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
sl@0:       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
sl@0:       sqlite3_mutex_enter(mutexShared);
sl@0:       for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
sl@0:         assert( pBt->nRef>0 );
sl@0:         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
sl@0:                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
sl@0:           p->pBt = pBt;
sl@0:           pBt->nRef++;
sl@0:           break;
sl@0:         }
sl@0:       }
sl@0:       sqlite3_mutex_leave(mutexShared);
sl@0:       sqlite3_free(zFullPathname);
sl@0:     }
sl@0: #ifdef SQLITE_DEBUG
sl@0:     else{
sl@0:       /* In debug mode, we mark all persistent databases as sharable
sl@0:       ** even when they are not.  This exercises the locking code and
sl@0:       ** gives more opportunity for asserts(sqlite3_mutex_held())
sl@0:       ** statements to find locking problems.
sl@0:       */
sl@0:       p->sharable = 1;
sl@0:     }
sl@0: #endif
sl@0:   }
sl@0: #endif
sl@0:   if( pBt==0 ){
sl@0:     /*
sl@0:     ** The following asserts make sure that structures used by the btree are
sl@0:     ** the right size.  This is to guard against size changes that result
sl@0:     ** when compiling on a different architecture.
sl@0:     */
sl@0:     assert( sizeof(i64)==8 || sizeof(i64)==4 );
sl@0:     assert( sizeof(u64)==8 || sizeof(u64)==4 );
sl@0:     assert( sizeof(u32)==4 );
sl@0:     assert( sizeof(u16)==2 );
sl@0:     assert( sizeof(Pgno)==4 );
sl@0:   
sl@0:     pBt = sqlite3MallocZero( sizeof(*pBt) );
sl@0:     if( pBt==0 ){
sl@0:       rc = SQLITE_NOMEM;
sl@0:       goto btree_open_out;
sl@0:     }
sl@0:     pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
sl@0:     pBt->busyHdr.pArg = pBt;
sl@0:     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
sl@0:                           EXTRA_SIZE, flags, vfsFlags);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
sl@0:     }
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       goto btree_open_out;
sl@0:     }
sl@0:     sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
sl@0:     p->pBt = pBt;
sl@0:   
sl@0:     sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
sl@0:     sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
sl@0:     pBt->pCursor = 0;
sl@0:     pBt->pPage1 = 0;
sl@0:     pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
sl@0:     pBt->pageSize = get2byte(&zDbHeader[16]);
sl@0:     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
sl@0:          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
sl@0:       pBt->pageSize = 0;
sl@0:       sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       /* If the magic name ":memory:" will create an in-memory database, then
sl@0:       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
sl@0:       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
sl@0:       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
sl@0:       ** regular file-name. In this case the auto-vacuum applies as per normal.
sl@0:       */
sl@0:       if( zFilename && !isMemdb ){
sl@0:         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
sl@0:         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
sl@0:       }
sl@0: #endif
sl@0:       nReserve = 0;
sl@0:     }else{
sl@0:       nReserve = zDbHeader[20];
sl@0:       pBt->pageSizeFixed = 1;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
sl@0:       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
sl@0: #endif
sl@0:     }
sl@0:     pBt->usableSize = pBt->pageSize - nReserve;
sl@0:     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
sl@0:     sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
sl@0:    
sl@0: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
sl@0:     /* Add the new BtShared object to the linked list sharable BtShareds.
sl@0:     */
sl@0:     if( p->sharable ){
sl@0:       sqlite3_mutex *mutexShared;
sl@0:       pBt->nRef = 1;
sl@0:       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
sl@0:       if( SQLITE_THREADSAFE && sqlite3Config.bCoreMutex ){
sl@0:         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
sl@0:         if( pBt->mutex==0 ){
sl@0:           rc = SQLITE_NOMEM;
sl@0:           db->mallocFailed = 0;
sl@0:           goto btree_open_out;
sl@0:         }
sl@0:       }
sl@0:       sqlite3_mutex_enter(mutexShared);
sl@0:       pBt->pNext = sqlite3SharedCacheList;
sl@0:       sqlite3SharedCacheList = pBt;
sl@0:       sqlite3_mutex_leave(mutexShared);
sl@0:     }
sl@0: #endif
sl@0:   }
sl@0: 
sl@0: #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
sl@0:   /* If the new Btree uses a sharable pBtShared, then link the new
sl@0:   ** Btree into the list of all sharable Btrees for the same connection.
sl@0:   ** The list is kept in ascending order by pBt address.
sl@0:   */
sl@0:   if( p->sharable ){
sl@0:     int i;
sl@0:     Btree *pSib;
sl@0:     for(i=0; i<db->nDb; i++){
sl@0:       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
sl@0:         while( pSib->pPrev ){ pSib = pSib->pPrev; }
sl@0:         if( p->pBt<pSib->pBt ){
sl@0:           p->pNext = pSib;
sl@0:           p->pPrev = 0;
sl@0:           pSib->pPrev = p;
sl@0:         }else{
sl@0:           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
sl@0:             pSib = pSib->pNext;
sl@0:           }
sl@0:           p->pNext = pSib->pNext;
sl@0:           p->pPrev = pSib;
sl@0:           if( p->pNext ){
sl@0:             p->pNext->pPrev = p;
sl@0:           }
sl@0:           pSib->pNext = p;
sl@0:         }
sl@0:         break;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: #endif
sl@0:   *ppBtree = p;
sl@0: 
sl@0: btree_open_out:
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     if( pBt && pBt->pPager ){
sl@0:       sqlite3PagerClose(pBt->pPager);
sl@0:     }
sl@0:     sqlite3_free(pBt);
sl@0:     sqlite3_free(p);
sl@0:     *ppBtree = 0;
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Decrement the BtShared.nRef counter.  When it reaches zero,
sl@0: ** remove the BtShared structure from the sharing list.  Return
sl@0: ** true if the BtShared.nRef counter reaches zero and return
sl@0: ** false if it is still positive.
sl@0: */
sl@0: static int removeFromSharingList(BtShared *pBt){
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0:   sqlite3_mutex *pMaster;
sl@0:   BtShared *pList;
sl@0:   int removed = 0;
sl@0: 
sl@0:   assert( sqlite3_mutex_notheld(pBt->mutex) );
sl@0:   pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
sl@0:   sqlite3_mutex_enter(pMaster);
sl@0:   pBt->nRef--;
sl@0:   if( pBt->nRef<=0 ){
sl@0:     if( sqlite3SharedCacheList==pBt ){
sl@0:       sqlite3SharedCacheList = pBt->pNext;
sl@0:     }else{
sl@0:       pList = sqlite3SharedCacheList;
sl@0:       while( ALWAYS(pList) && pList->pNext!=pBt ){
sl@0:         pList=pList->pNext;
sl@0:       }
sl@0:       if( ALWAYS(pList) ){
sl@0:         pList->pNext = pBt->pNext;
sl@0:       }
sl@0:     }
sl@0:     if( SQLITE_THREADSAFE ){
sl@0:       sqlite3_mutex_free(pBt->mutex);
sl@0:     }
sl@0:     removed = 1;
sl@0:   }
sl@0:   sqlite3_mutex_leave(pMaster);
sl@0:   return removed;
sl@0: #else
sl@0:   return 1;
sl@0: #endif
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Make sure pBt->pTmpSpace points to an allocation of 
sl@0: ** MX_CELL_SIZE(pBt) bytes.
sl@0: */
sl@0: static void allocateTempSpace(BtShared *pBt){
sl@0:   if( !pBt->pTmpSpace ){
sl@0:     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Free the pBt->pTmpSpace allocation
sl@0: */
sl@0: static void freeTempSpace(BtShared *pBt){
sl@0:   sqlite3PageFree( pBt->pTmpSpace);
sl@0:   pBt->pTmpSpace = 0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Close an open database and invalidate all cursors.
sl@0: */
sl@0: int sqlite3BtreeClose(Btree *p){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   BtCursor *pCur;
sl@0: 
sl@0:   /* Close all cursors opened via this handle.  */
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   pCur = pBt->pCursor;
sl@0:   while( pCur ){
sl@0:     BtCursor *pTmp = pCur;
sl@0:     pCur = pCur->pNext;
sl@0:     if( pTmp->pBtree==p ){
sl@0:       sqlite3BtreeCloseCursor(pTmp);
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Rollback any active transaction and free the handle structure.
sl@0:   ** The call to sqlite3BtreeRollback() drops any table-locks held by
sl@0:   ** this handle.
sl@0:   */
sl@0:   sqlite3BtreeRollback(p);
sl@0:   sqlite3BtreeLeave(p);
sl@0: 
sl@0:   /* If there are still other outstanding references to the shared-btree
sl@0:   ** structure, return now. The remainder of this procedure cleans 
sl@0:   ** up the shared-btree.
sl@0:   */
sl@0:   assert( p->wantToLock==0 && p->locked==0 );
sl@0:   if( !p->sharable || removeFromSharingList(pBt) ){
sl@0:     /* The pBt is no longer on the sharing list, so we can access
sl@0:     ** it without having to hold the mutex.
sl@0:     **
sl@0:     ** Clean out and delete the BtShared object.
sl@0:     */
sl@0:     assert( !pBt->pCursor );
sl@0:     sqlite3PagerClose(pBt->pPager);
sl@0:     if( pBt->xFreeSchema && pBt->pSchema ){
sl@0:       pBt->xFreeSchema(pBt->pSchema);
sl@0:     }
sl@0:     sqlite3_free(pBt->pSchema);
sl@0:     freeTempSpace(pBt);
sl@0:     sqlite3_free(pBt);
sl@0:   }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0:   assert( p->wantToLock==0 );
sl@0:   assert( p->locked==0 );
sl@0:   if( p->pPrev ) p->pPrev->pNext = p->pNext;
sl@0:   if( p->pNext ) p->pNext->pPrev = p->pPrev;
sl@0: #endif
sl@0: 
sl@0:   sqlite3_free(p);
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Change the limit on the number of pages allowed in the cache.
sl@0: **
sl@0: ** The maximum number of cache pages is set to the absolute
sl@0: ** value of mxPage.  If mxPage is negative, the pager will
sl@0: ** operate asynchronously - it will not stop to do fsync()s
sl@0: ** to insure data is written to the disk surface before
sl@0: ** continuing.  Transactions still work if synchronous is off,
sl@0: ** and the database cannot be corrupted if this program
sl@0: ** crashes.  But if the operating system crashes or there is
sl@0: ** an abrupt power failure when synchronous is off, the database
sl@0: ** could be left in an inconsistent and unrecoverable state.
sl@0: ** Synchronous is on by default so database corruption is not
sl@0: ** normally a worry.
sl@0: */
sl@0: int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );
sl@0:   sqlite3BtreeEnter(p);
sl@0:   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Change the way data is synced to disk in order to increase or decrease
sl@0: ** how well the database resists damage due to OS crashes and power
sl@0: ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
sl@0: ** there is a high probability of damage)  Level 2 is the default.  There
sl@0: ** is a very low but non-zero probability of damage.  Level 3 reduces the
sl@0: ** probability of damage to near zero but with a write performance reduction.
sl@0: */
sl@0: #ifndef SQLITE_OMIT_PAGER_PRAGMAS
sl@0: int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );
sl@0:   sqlite3BtreeEnter(p);
sl@0:   sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Return TRUE if the given btree is set to safety level 1.  In other
sl@0: ** words, return TRUE if no sync() occurs on the disk files.
sl@0: */
sl@0: int sqlite3BtreeSyncDisabled(Btree *p){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   int rc;
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );  
sl@0:   sqlite3BtreeEnter(p);
sl@0:   assert( pBt && pBt->pPager );
sl@0:   rc = sqlite3PagerNosync(pBt->pPager);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
sl@0: /*
sl@0: ** Change the default pages size and the number of reserved bytes per page.
sl@0: **
sl@0: ** The page size must be a power of 2 between 512 and 65536.  If the page
sl@0: ** size supplied does not meet this constraint then the page size is not
sl@0: ** changed.
sl@0: **
sl@0: ** Page sizes are constrained to be a power of two so that the region
sl@0: ** of the database file used for locking (beginning at PENDING_BYTE,
sl@0: ** the first byte past the 1GB boundary, 0x40000000) needs to occur
sl@0: ** at the beginning of a page.
sl@0: **
sl@0: ** If parameter nReserve is less than zero, then the number of reserved
sl@0: ** bytes per page is left unchanged.
sl@0: */
sl@0: int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
sl@0:   int rc = SQLITE_OK;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   if( pBt->pageSizeFixed ){
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return SQLITE_READONLY;
sl@0:   }
sl@0:   if( nReserve<0 ){
sl@0:     nReserve = pBt->pageSize - pBt->usableSize;
sl@0:   }
sl@0:   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
sl@0:         ((pageSize-1)&pageSize)==0 ){
sl@0:     assert( (pageSize & 7)==0 );
sl@0:     assert( !pBt->pPage1 && !pBt->pCursor );
sl@0:     pBt->pageSize = pageSize;
sl@0:     freeTempSpace(pBt);
sl@0:     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
sl@0:   }
sl@0:   pBt->usableSize = pBt->pageSize - nReserve;
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the currently defined page size
sl@0: */
sl@0: int sqlite3BtreeGetPageSize(Btree *p){
sl@0:   return p->pBt->pageSize;
sl@0: }
sl@0: int sqlite3BtreeGetReserve(Btree *p){
sl@0:   int n;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   n = p->pBt->pageSize - p->pBt->usableSize;
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return n;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Set the maximum page count for a database if mxPage is positive.
sl@0: ** No changes are made if mxPage is 0 or negative.
sl@0: ** Regardless of the value of mxPage, return the maximum page count.
sl@0: */
sl@0: int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
sl@0:   int n;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return n;
sl@0: }
sl@0: #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
sl@0: 
sl@0: /*
sl@0: ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
sl@0: ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
sl@0: ** is disabled. The default value for the auto-vacuum property is 
sl@0: ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
sl@0: */
sl@0: int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:   return SQLITE_READONLY;
sl@0: #else
sl@0:   BtShared *pBt = p->pBt;
sl@0:   int rc = SQLITE_OK;
sl@0:   int av = (autoVacuum?1:0);
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
sl@0:     rc = SQLITE_READONLY;
sl@0:   }else{
sl@0:     pBt->autoVacuum = av;
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: #endif
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 
sl@0: ** enabled 1 is returned. Otherwise 0.
sl@0: */
sl@0: int sqlite3BtreeGetAutoVacuum(Btree *p){
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:   return BTREE_AUTOVACUUM_NONE;
sl@0: #else
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   rc = (
sl@0:     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
sl@0:     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
sl@0:     BTREE_AUTOVACUUM_INCR
sl@0:   );
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: #endif
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Get a reference to pPage1 of the database file.  This will
sl@0: ** also acquire a readlock on that file.
sl@0: **
sl@0: ** SQLITE_OK is returned on success.  If the file is not a
sl@0: ** well-formed database file, then SQLITE_CORRUPT is returned.
sl@0: ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
sl@0: ** is returned if we run out of memory. 
sl@0: */
sl@0: static int lockBtree(BtShared *pBt){
sl@0:   int rc;
sl@0:   MemPage *pPage1;
sl@0:   int nPage;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   if( pBt->pPage1 ) return SQLITE_OK;
sl@0:   rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
sl@0:   if( rc!=SQLITE_OK ) return rc;
sl@0: 
sl@0:   /* Do some checking to help insure the file we opened really is
sl@0:   ** a valid database file. 
sl@0:   */
sl@0:   rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     goto page1_init_failed;
sl@0:   }else if( nPage>0 ){
sl@0:     int pageSize;
sl@0:     int usableSize;
sl@0:     u8 *page1 = pPage1->aData;
sl@0:     rc = SQLITE_NOTADB;
sl@0:     if( memcmp(page1, zMagicHeader, 16)!=0 ){
sl@0:       goto page1_init_failed;
sl@0:     }
sl@0:     if( page1[18]>1 ){
sl@0:       pBt->readOnly = 1;
sl@0:     }
sl@0:     if( page1[19]>1 ){
sl@0:       goto page1_init_failed;
sl@0:     }
sl@0: 
sl@0:     /* The maximum embedded fraction must be exactly 25%.  And the minimum
sl@0:     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
sl@0:     ** The original design allowed these amounts to vary, but as of
sl@0:     ** version 3.6.0, we require them to be fixed.
sl@0:     */
sl@0:     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
sl@0:       goto page1_init_failed;
sl@0:     }
sl@0:     pageSize = get2byte(&page1[16]);
sl@0:     if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
sl@0:         (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
sl@0:     ){
sl@0:       goto page1_init_failed;
sl@0:     }
sl@0:     assert( (pageSize & 7)==0 );
sl@0:     usableSize = pageSize - page1[20];
sl@0:     if( pageSize!=pBt->pageSize ){
sl@0:       /* After reading the first page of the database assuming a page size
sl@0:       ** of BtShared.pageSize, we have discovered that the page-size is
sl@0:       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
sl@0:       ** zero and return SQLITE_OK. The caller will call this function
sl@0:       ** again with the correct page-size.
sl@0:       */
sl@0:       releasePage(pPage1);
sl@0:       pBt->usableSize = usableSize;
sl@0:       pBt->pageSize = pageSize;
sl@0:       freeTempSpace(pBt);
sl@0:       sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
sl@0:       return SQLITE_OK;
sl@0:     }
sl@0:     if( usableSize<500 ){
sl@0:       goto page1_init_failed;
sl@0:     }
sl@0:     pBt->pageSize = pageSize;
sl@0:     pBt->usableSize = usableSize;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
sl@0:     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
sl@0: #endif
sl@0:   }
sl@0: 
sl@0:   /* maxLocal is the maximum amount of payload to store locally for
sl@0:   ** a cell.  Make sure it is small enough so that at least minFanout
sl@0:   ** cells can will fit on one page.  We assume a 10-byte page header.
sl@0:   ** Besides the payload, the cell must store:
sl@0:   **     2-byte pointer to the cell
sl@0:   **     4-byte child pointer
sl@0:   **     9-byte nKey value
sl@0:   **     4-byte nData value
sl@0:   **     4-byte overflow page pointer
sl@0:   ** So a cell consists of a 2-byte poiner, a header which is as much as
sl@0:   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
sl@0:   ** page pointer.
sl@0:   */
sl@0:   pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23;
sl@0:   pBt->minLocal = (pBt->usableSize-12)*32/255 - 23;
sl@0:   pBt->maxLeaf = pBt->usableSize - 35;
sl@0:   pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23;
sl@0:   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
sl@0:   pBt->pPage1 = pPage1;
sl@0:   return SQLITE_OK;
sl@0: 
sl@0: page1_init_failed:
sl@0:   releasePage(pPage1);
sl@0:   pBt->pPage1 = 0;
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine works like lockBtree() except that it also invokes the
sl@0: ** busy callback if there is lock contention.
sl@0: */
sl@0: static int lockBtreeWithRetry(Btree *pRef){
sl@0:   int rc = SQLITE_OK;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(pRef) );
sl@0:   if( pRef->inTrans==TRANS_NONE ){
sl@0:     u8 inTransaction = pRef->pBt->inTransaction;
sl@0:     btreeIntegrity(pRef);
sl@0:     rc = sqlite3BtreeBeginTrans(pRef, 0);
sl@0:     pRef->pBt->inTransaction = inTransaction;
sl@0:     pRef->inTrans = TRANS_NONE;
sl@0:     if( rc==SQLITE_OK ){
sl@0:       pRef->pBt->nTransaction--;
sl@0:     }
sl@0:     btreeIntegrity(pRef);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0:        
sl@0: 
sl@0: /*
sl@0: ** If there are no outstanding cursors and we are not in the middle
sl@0: ** of a transaction but there is a read lock on the database, then
sl@0: ** this routine unrefs the first page of the database file which 
sl@0: ** has the effect of releasing the read lock.
sl@0: **
sl@0: ** If there are any outstanding cursors, this routine is a no-op.
sl@0: **
sl@0: ** If there is a transaction in progress, this routine is a no-op.
sl@0: */
sl@0: static void unlockBtreeIfUnused(BtShared *pBt){
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
sl@0:     if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
sl@0:       assert( pBt->pPage1->aData );
sl@0: #if 0
sl@0:       if( pBt->pPage1->aData==0 ){
sl@0:         MemPage *pPage = pBt->pPage1;
sl@0:         pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
sl@0:         pPage->pBt = pBt;
sl@0:         pPage->pgno = 1;
sl@0:       }
sl@0: #endif
sl@0:       releasePage(pBt->pPage1);
sl@0:     }
sl@0:     pBt->pPage1 = 0;
sl@0:     pBt->inStmt = 0;
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Create a new database by initializing the first page of the
sl@0: ** file.
sl@0: */
sl@0: static int newDatabase(BtShared *pBt){
sl@0:   MemPage *pP1;
sl@0:   unsigned char *data;
sl@0:   int rc;
sl@0:   int nPage;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
sl@0:   if( rc!=SQLITE_OK || nPage>0 ){
sl@0:     return rc;
sl@0:   }
sl@0:   pP1 = pBt->pPage1;
sl@0:   assert( pP1!=0 );
sl@0:   data = pP1->aData;
sl@0:   rc = sqlite3PagerWrite(pP1->pDbPage);
sl@0:   if( rc ) return rc;
sl@0:   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
sl@0:   assert( sizeof(zMagicHeader)==16 );
sl@0:   put2byte(&data[16], pBt->pageSize);
sl@0:   data[18] = 1;
sl@0:   data[19] = 1;
sl@0:   data[20] = pBt->pageSize - pBt->usableSize;
sl@0:   data[21] = 64;
sl@0:   data[22] = 32;
sl@0:   data[23] = 32;
sl@0:   memset(&data[24], 0, 100-24);
sl@0:   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
sl@0:   pBt->pageSizeFixed = 1;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
sl@0:   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
sl@0:   put4byte(&data[36 + 4*4], pBt->autoVacuum);
sl@0:   put4byte(&data[36 + 7*4], pBt->incrVacuum);
sl@0: #endif
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Attempt to start a new transaction. A write-transaction
sl@0: ** is started if the second argument is nonzero, otherwise a read-
sl@0: ** transaction.  If the second argument is 2 or more and exclusive
sl@0: ** transaction is started, meaning that no other process is allowed
sl@0: ** to access the database.  A preexisting transaction may not be
sl@0: ** upgraded to exclusive by calling this routine a second time - the
sl@0: ** exclusivity flag only works for a new transaction.
sl@0: **
sl@0: ** A write-transaction must be started before attempting any 
sl@0: ** changes to the database.  None of the following routines 
sl@0: ** will work unless a transaction is started first:
sl@0: **
sl@0: **      sqlite3BtreeCreateTable()
sl@0: **      sqlite3BtreeCreateIndex()
sl@0: **      sqlite3BtreeClearTable()
sl@0: **      sqlite3BtreeDropTable()
sl@0: **      sqlite3BtreeInsert()
sl@0: **      sqlite3BtreeDelete()
sl@0: **      sqlite3BtreeUpdateMeta()
sl@0: **
sl@0: ** If an initial attempt to acquire the lock fails because of lock contention
sl@0: ** and the database was previously unlocked, then invoke the busy handler
sl@0: ** if there is one.  But if there was previously a read-lock, do not
sl@0: ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is 
sl@0: ** returned when there is already a read-lock in order to avoid a deadlock.
sl@0: **
sl@0: ** Suppose there are two processes A and B.  A has a read lock and B has
sl@0: ** a reserved lock.  B tries to promote to exclusive but is blocked because
sl@0: ** of A's read lock.  A tries to promote to reserved but is blocked by B.
sl@0: ** One or the other of the two processes must give way or there can be
sl@0: ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
sl@0: ** when A already has a read lock, we encourage A to give up and let B
sl@0: ** proceed.
sl@0: */
sl@0: int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   int rc = SQLITE_OK;
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   btreeIntegrity(p);
sl@0: 
sl@0:   /* If the btree is already in a write-transaction, or it
sl@0:   ** is already in a read-transaction and a read-transaction
sl@0:   ** is requested, this is a no-op.
sl@0:   */
sl@0:   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
sl@0:     goto trans_begun;
sl@0:   }
sl@0: 
sl@0:   /* Write transactions are not possible on a read-only database */
sl@0:   if( pBt->readOnly && wrflag ){
sl@0:     rc = SQLITE_READONLY;
sl@0:     goto trans_begun;
sl@0:   }
sl@0: 
sl@0:   /* If another database handle has already opened a write transaction 
sl@0:   ** on this shared-btree structure and a second write transaction is
sl@0:   ** requested, return SQLITE_BUSY.
sl@0:   */
sl@0:   if( pBt->inTransaction==TRANS_WRITE && wrflag ){
sl@0:     rc = SQLITE_BUSY;
sl@0:     goto trans_begun;
sl@0:   }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0:   if( wrflag>1 ){
sl@0:     BtLock *pIter;
sl@0:     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
sl@0:       if( pIter->pBtree!=p ){
sl@0:         rc = SQLITE_BUSY;
sl@0:         goto trans_begun;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   do {
sl@0:     if( pBt->pPage1==0 ){
sl@0:       do{
sl@0:         rc = lockBtree(pBt);
sl@0:       }while( pBt->pPage1==0 && rc==SQLITE_OK );
sl@0:     }
sl@0: 
sl@0:     if( rc==SQLITE_OK && wrflag ){
sl@0:       if( pBt->readOnly ){
sl@0:         rc = SQLITE_READONLY;
sl@0:       }else{
sl@0:         rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
sl@0:         if( rc==SQLITE_OK ){
sl@0:           rc = newDatabase(pBt);
sl@0:         }
sl@0:       }
sl@0:     }
sl@0:   
sl@0:     if( rc==SQLITE_OK ){
sl@0:       if( wrflag ) pBt->inStmt = 0;
sl@0:     }else{
sl@0:       unlockBtreeIfUnused(pBt);
sl@0:     }
sl@0:   }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
sl@0:           sqlite3BtreeInvokeBusyHandler(pBt, 0) );
sl@0: 
sl@0:   if( rc==SQLITE_OK ){
sl@0:     if( p->inTrans==TRANS_NONE ){
sl@0:       pBt->nTransaction++;
sl@0:     }
sl@0:     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
sl@0:     if( p->inTrans>pBt->inTransaction ){
sl@0:       pBt->inTransaction = p->inTrans;
sl@0:     }
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0:     if( wrflag>1 ){
sl@0:       assert( !pBt->pExclusive );
sl@0:       pBt->pExclusive = p;
sl@0:     }
sl@0: #endif
sl@0:   }
sl@0: 
sl@0: 
sl@0: trans_begun:
sl@0:   btreeIntegrity(p);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the size of the database file in pages.  Or return -1 if
sl@0: ** there is any kind of error.
sl@0: */
sl@0: static int pagerPagecount(Pager *pPager){
sl@0:   int rc;
sl@0:   int nPage;
sl@0:   rc = sqlite3PagerPagecount(pPager, &nPage);
sl@0:   return (rc==SQLITE_OK?nPage:-1);
sl@0: }
sl@0: 
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0: 
sl@0: /*
sl@0: ** Set the pointer-map entries for all children of page pPage. Also, if
sl@0: ** pPage contains cells that point to overflow pages, set the pointer
sl@0: ** map entries for the overflow pages as well.
sl@0: */
sl@0: static int setChildPtrmaps(MemPage *pPage){
sl@0:   int i;                             /* Counter variable */
sl@0:   int nCell;                         /* Number of cells in page pPage */
sl@0:   int rc;                            /* Return code */
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   int isInitOrig = pPage->isInit;
sl@0:   Pgno pgno = pPage->pgno;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     goto set_child_ptrmaps_out;
sl@0:   }
sl@0:   nCell = pPage->nCell;
sl@0: 
sl@0:   for(i=0; i<nCell; i++){
sl@0:     u8 *pCell = findCell(pPage, i);
sl@0: 
sl@0:     rc = ptrmapPutOvflPtr(pPage, pCell);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       goto set_child_ptrmaps_out;
sl@0:     }
sl@0: 
sl@0:     if( !pPage->leaf ){
sl@0:       Pgno childPgno = get4byte(pCell);
sl@0:       rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
sl@0:        if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( !pPage->leaf ){
sl@0:     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0:     rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
sl@0:   }
sl@0: 
sl@0: set_child_ptrmaps_out:
sl@0:   pPage->isInit = isInitOrig;
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
sl@0: ** page, is a pointer to page iFrom. Modify this pointer so that it points to
sl@0: ** iTo. Parameter eType describes the type of pointer to be modified, as 
sl@0: ** follows:
sl@0: **
sl@0: ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child 
sl@0: **                   page of pPage.
sl@0: **
sl@0: ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
sl@0: **                   page pointed to by one of the cells on pPage.
sl@0: **
sl@0: ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
sl@0: **                   overflow page in the list.
sl@0: */
sl@0: static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   if( eType==PTRMAP_OVERFLOW2 ){
sl@0:     /* The pointer is always the first 4 bytes of the page in this case.  */
sl@0:     if( get4byte(pPage->aData)!=iFrom ){
sl@0:       return SQLITE_CORRUPT_BKPT;
sl@0:     }
sl@0:     put4byte(pPage->aData, iTo);
sl@0:   }else{
sl@0:     int isInitOrig = pPage->isInit;
sl@0:     int i;
sl@0:     int nCell;
sl@0: 
sl@0:     sqlite3BtreeInitPage(pPage, 0);
sl@0:     nCell = pPage->nCell;
sl@0: 
sl@0:     for(i=0; i<nCell; i++){
sl@0:       u8 *pCell = findCell(pPage, i);
sl@0:       if( eType==PTRMAP_OVERFLOW1 ){
sl@0:         CellInfo info;
sl@0:         sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:         if( info.iOverflow ){
sl@0:           if( iFrom==get4byte(&pCell[info.iOverflow]) ){
sl@0:             put4byte(&pCell[info.iOverflow], iTo);
sl@0:             break;
sl@0:           }
sl@0:         }
sl@0:       }else{
sl@0:         if( get4byte(pCell)==iFrom ){
sl@0:           put4byte(pCell, iTo);
sl@0:           break;
sl@0:         }
sl@0:       }
sl@0:     }
sl@0:   
sl@0:     if( i==nCell ){
sl@0:       if( eType!=PTRMAP_BTREE || 
sl@0:           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
sl@0:         return SQLITE_CORRUPT_BKPT;
sl@0:       }
sl@0:       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
sl@0:     }
sl@0: 
sl@0:     pPage->isInit = isInitOrig;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Move the open database page pDbPage to location iFreePage in the 
sl@0: ** database. The pDbPage reference remains valid.
sl@0: */
sl@0: static int relocatePage(
sl@0:   BtShared *pBt,           /* Btree */
sl@0:   MemPage *pDbPage,        /* Open page to move */
sl@0:   u8 eType,                /* Pointer map 'type' entry for pDbPage */
sl@0:   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
sl@0:   Pgno iFreePage,          /* The location to move pDbPage to */
sl@0:   int isCommit
sl@0: ){
sl@0:   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
sl@0:   Pgno iDbPage = pDbPage->pgno;
sl@0:   Pager *pPager = pBt->pPager;
sl@0:   int rc;
sl@0: 
sl@0:   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 
sl@0:       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   assert( pDbPage->pBt==pBt );
sl@0: 
sl@0:   /* Move page iDbPage from its current location to page number iFreePage */
sl@0:   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 
sl@0:       iDbPage, iFreePage, iPtrPage, eType));
sl@0:   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     return rc;
sl@0:   }
sl@0:   pDbPage->pgno = iFreePage;
sl@0: 
sl@0:   /* If pDbPage was a btree-page, then it may have child pages and/or cells
sl@0:   ** that point to overflow pages. The pointer map entries for all these
sl@0:   ** pages need to be changed.
sl@0:   **
sl@0:   ** If pDbPage is an overflow page, then the first 4 bytes may store a
sl@0:   ** pointer to a subsequent overflow page. If this is the case, then
sl@0:   ** the pointer map needs to be updated for the subsequent overflow page.
sl@0:   */
sl@0:   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
sl@0:     rc = setChildPtrmaps(pDbPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:   }else{
sl@0:     Pgno nextOvfl = get4byte(pDbPage->aData);
sl@0:     if( nextOvfl!=0 ){
sl@0:       rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
sl@0:   ** that it points at iFreePage. Also fix the pointer map entry for
sl@0:   ** iPtrPage.
sl@0:   */
sl@0:   if( eType!=PTRMAP_ROOTPAGE ){
sl@0:     rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       releasePage(pPtrPage);
sl@0:       return rc;
sl@0:     }
sl@0:     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
sl@0:     releasePage(pPtrPage);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /* Forward declaration required by incrVacuumStep(). */
sl@0: static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
sl@0: 
sl@0: /*
sl@0: ** Perform a single step of an incremental-vacuum. If successful,
sl@0: ** return SQLITE_OK. If there is no work to do (and therefore no
sl@0: ** point in calling this function again), return SQLITE_DONE.
sl@0: **
sl@0: ** More specificly, this function attempts to re-organize the 
sl@0: ** database so that the last page of the file currently in use
sl@0: ** is no longer in use.
sl@0: **
sl@0: ** If the nFin parameter is non-zero, the implementation assumes
sl@0: ** that the caller will keep calling incrVacuumStep() until
sl@0: ** it returns SQLITE_DONE or an error, and that nFin is the
sl@0: ** number of pages the database file will contain after this 
sl@0: ** process is complete.
sl@0: */
sl@0: static int incrVacuumStep(BtShared *pBt, Pgno nFin){
sl@0:   Pgno iLastPg;             /* Last page in the database */
sl@0:   Pgno nFreeList;           /* Number of pages still on the free-list */
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   iLastPg = pBt->nTrunc;
sl@0:   if( iLastPg==0 ){
sl@0:     iLastPg = pagerPagecount(pBt->pPager);
sl@0:   }
sl@0: 
sl@0:   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
sl@0:     int rc;
sl@0:     u8 eType;
sl@0:     Pgno iPtrPage;
sl@0: 
sl@0:     nFreeList = get4byte(&pBt->pPage1->aData[36]);
sl@0:     if( nFreeList==0 || nFin==iLastPg ){
sl@0:       return SQLITE_DONE;
sl@0:     }
sl@0: 
sl@0:     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     if( eType==PTRMAP_ROOTPAGE ){
sl@0:       return SQLITE_CORRUPT_BKPT;
sl@0:     }
sl@0: 
sl@0:     if( eType==PTRMAP_FREEPAGE ){
sl@0:       if( nFin==0 ){
sl@0:         /* Remove the page from the files free-list. This is not required
sl@0:         ** if nFin is non-zero. In that case, the free-list will be
sl@0:         ** truncated to zero after this function returns, so it doesn't 
sl@0:         ** matter if it still contains some garbage entries.
sl@0:         */
sl@0:         Pgno iFreePg;
sl@0:         MemPage *pFreePg;
sl@0:         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:         assert( iFreePg==iLastPg );
sl@0:         releasePage(pFreePg);
sl@0:       }
sl@0:     } else {
sl@0:       Pgno iFreePg;             /* Index of free page to move pLastPg to */
sl@0:       MemPage *pLastPg;
sl@0: 
sl@0:       rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0: 
sl@0:       /* If nFin is zero, this loop runs exactly once and page pLastPg
sl@0:       ** is swapped with the first free page pulled off the free list.
sl@0:       **
sl@0:       ** On the other hand, if nFin is greater than zero, then keep
sl@0:       ** looping until a free-page located within the first nFin pages
sl@0:       ** of the file is found.
sl@0:       */
sl@0:       do {
sl@0:         MemPage *pFreePg;
sl@0:         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           releasePage(pLastPg);
sl@0:           return rc;
sl@0:         }
sl@0:         releasePage(pFreePg);
sl@0:       }while( nFin!=0 && iFreePg>nFin );
sl@0:       assert( iFreePg<iLastPg );
sl@0:       
sl@0:       rc = sqlite3PagerWrite(pLastPg->pDbPage);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
sl@0:       }
sl@0:       releasePage(pLastPg);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   pBt->nTrunc = iLastPg - 1;
sl@0:   while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
sl@0:     pBt->nTrunc--;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** A write-transaction must be opened before calling this function.
sl@0: ** It performs a single unit of work towards an incremental vacuum.
sl@0: **
sl@0: ** If the incremental vacuum is finished after this function has run,
sl@0: ** SQLITE_DONE is returned. If it is not finished, but no error occured,
sl@0: ** SQLITE_OK is returned. Otherwise an SQLite error code. 
sl@0: */
sl@0: int sqlite3BtreeIncrVacuum(Btree *p){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
sl@0:   if( !pBt->autoVacuum ){
sl@0:     rc = SQLITE_DONE;
sl@0:   }else{
sl@0:     invalidateAllOverflowCache(pBt);
sl@0:     rc = incrVacuumStep(pBt, 0);
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine is called prior to sqlite3PagerCommit when a transaction
sl@0: ** is commited for an auto-vacuum database.
sl@0: **
sl@0: ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
sl@0: ** the database file should be truncated to during the commit process. 
sl@0: ** i.e. the database has been reorganized so that only the first *pnTrunc
sl@0: ** pages are in use.
sl@0: */
sl@0: static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
sl@0:   int rc = SQLITE_OK;
sl@0:   Pager *pPager = pBt->pPager;
sl@0: #ifndef NDEBUG
sl@0:   int nRef = sqlite3PagerRefcount(pPager);
sl@0: #endif
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   invalidateAllOverflowCache(pBt);
sl@0:   assert(pBt->autoVacuum);
sl@0:   if( !pBt->incrVacuum ){
sl@0:     Pgno nFin = 0;
sl@0: 
sl@0:     if( pBt->nTrunc==0 ){
sl@0:       Pgno nFree;
sl@0:       Pgno nPtrmap;
sl@0:       const int pgsz = pBt->pageSize;
sl@0:       int nOrig = pagerPagecount(pBt->pPager);
sl@0: 
sl@0:       if( PTRMAP_ISPAGE(pBt, nOrig) ){
sl@0:         return SQLITE_CORRUPT_BKPT;
sl@0:       }
sl@0:       if( nOrig==PENDING_BYTE_PAGE(pBt) ){
sl@0:         nOrig--;
sl@0:       }
sl@0:       nFree = get4byte(&pBt->pPage1->aData[36]);
sl@0:       nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
sl@0:       nFin = nOrig - nFree - nPtrmap;
sl@0:       if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
sl@0:         nFin--;
sl@0:       }
sl@0:       while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
sl@0:         nFin--;
sl@0:       }
sl@0:     }
sl@0: 
sl@0:     while( rc==SQLITE_OK ){
sl@0:       rc = incrVacuumStep(pBt, nFin);
sl@0:     }
sl@0:     if( rc==SQLITE_DONE ){
sl@0:       assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
sl@0:       rc = SQLITE_OK;
sl@0:       if( pBt->nTrunc && nFin ){
sl@0:         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
sl@0:         put4byte(&pBt->pPage1->aData[32], 0);
sl@0:         put4byte(&pBt->pPage1->aData[36], 0);
sl@0:         pBt->nTrunc = nFin;
sl@0:       }
sl@0:     }
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       sqlite3PagerRollback(pPager);
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( rc==SQLITE_OK ){
sl@0:     *pnTrunc = pBt->nTrunc;
sl@0:     pBt->nTrunc = 0;
sl@0:   }
sl@0:   assert( nRef==sqlite3PagerRefcount(pPager) );
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** This routine does the first phase of a two-phase commit.  This routine
sl@0: ** causes a rollback journal to be created (if it does not already exist)
sl@0: ** and populated with enough information so that if a power loss occurs
sl@0: ** the database can be restored to its original state by playing back
sl@0: ** the journal.  Then the contents of the journal are flushed out to
sl@0: ** the disk.  After the journal is safely on oxide, the changes to the
sl@0: ** database are written into the database file and flushed to oxide.
sl@0: ** At the end of this call, the rollback journal still exists on the
sl@0: ** disk and we are still holding all locks, so the transaction has not
sl@0: ** committed.  See sqlite3BtreeCommit() for the second phase of the
sl@0: ** commit process.
sl@0: **
sl@0: ** This call is a no-op if no write-transaction is currently active on pBt.
sl@0: **
sl@0: ** Otherwise, sync the database file for the btree pBt. zMaster points to
sl@0: ** the name of a master journal file that should be written into the
sl@0: ** individual journal file, or is NULL, indicating no master journal file 
sl@0: ** (single database transaction).
sl@0: **
sl@0: ** When this is called, the master journal should already have been
sl@0: ** created, populated with this journal pointer and synced to disk.
sl@0: **
sl@0: ** Once this is routine has returned, the only thing required to commit
sl@0: ** the write-transaction for this database file is to delete the journal.
sl@0: */
sl@0: int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
sl@0:   int rc = SQLITE_OK;
sl@0:   if( p->inTrans==TRANS_WRITE ){
sl@0:     BtShared *pBt = p->pBt;
sl@0:     Pgno nTrunc = 0;
sl@0:     sqlite3BtreeEnter(p);
sl@0:     pBt->db = p->db;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( pBt->autoVacuum ){
sl@0:       rc = autoVacuumCommit(pBt, &nTrunc); 
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         sqlite3BtreeLeave(p);
sl@0:         return rc;
sl@0:       }
sl@0:     }
sl@0: #endif
sl@0:     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0);
sl@0:     sqlite3BtreeLeave(p);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Commit the transaction currently in progress.
sl@0: **
sl@0: ** This routine implements the second phase of a 2-phase commit.  The
sl@0: ** sqlite3BtreeSync() routine does the first phase and should be invoked
sl@0: ** prior to calling this routine.  The sqlite3BtreeSync() routine did
sl@0: ** all the work of writing information out to disk and flushing the
sl@0: ** contents so that they are written onto the disk platter.  All this
sl@0: ** routine has to do is delete or truncate the rollback journal
sl@0: ** (which causes the transaction to commit) and drop locks.
sl@0: **
sl@0: ** This will release the write lock on the database file.  If there
sl@0: ** are no active cursors, it also releases the read lock.
sl@0: */
sl@0: int sqlite3BtreeCommitPhaseTwo(Btree *p){
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   btreeIntegrity(p);
sl@0: 
sl@0:   /* If the handle has a write-transaction open, commit the shared-btrees 
sl@0:   ** transaction and set the shared state to TRANS_READ.
sl@0:   */
sl@0:   if( p->inTrans==TRANS_WRITE ){
sl@0:     int rc;
sl@0:     assert( pBt->inTransaction==TRANS_WRITE );
sl@0:     assert( pBt->nTransaction>0 );
sl@0:     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       sqlite3BtreeLeave(p);
sl@0:       return rc;
sl@0:     }
sl@0:     pBt->inTransaction = TRANS_READ;
sl@0:     pBt->inStmt = 0;
sl@0:   }
sl@0:   unlockAllTables(p);
sl@0: 
sl@0:   /* If the handle has any kind of transaction open, decrement the transaction
sl@0:   ** count of the shared btree. If the transaction count reaches 0, set
sl@0:   ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
sl@0:   ** will unlock the pager.
sl@0:   */
sl@0:   if( p->inTrans!=TRANS_NONE ){
sl@0:     pBt->nTransaction--;
sl@0:     if( 0==pBt->nTransaction ){
sl@0:       pBt->inTransaction = TRANS_NONE;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Set the handles current transaction state to TRANS_NONE and unlock
sl@0:   ** the pager if this call closed the only read or write transaction.
sl@0:   */
sl@0:   p->inTrans = TRANS_NONE;
sl@0:   unlockBtreeIfUnused(pBt);
sl@0: 
sl@0:   btreeIntegrity(p);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Do both phases of a commit.
sl@0: */
sl@0: int sqlite3BtreeCommit(Btree *p){
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   rc = sqlite3BtreeCommitPhaseOne(p, 0);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     rc = sqlite3BtreeCommitPhaseTwo(p);
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: #ifndef NDEBUG
sl@0: /*
sl@0: ** Return the number of write-cursors open on this handle. This is for use
sl@0: ** in assert() expressions, so it is only compiled if NDEBUG is not
sl@0: ** defined.
sl@0: **
sl@0: ** For the purposes of this routine, a write-cursor is any cursor that
sl@0: ** is capable of writing to the databse.  That means the cursor was
sl@0: ** originally opened for writing and the cursor has not be disabled
sl@0: ** by having its state changed to CURSOR_FAULT.
sl@0: */
sl@0: static int countWriteCursors(BtShared *pBt){
sl@0:   BtCursor *pCur;
sl@0:   int r = 0;
sl@0:   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
sl@0:     if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; 
sl@0:   }
sl@0:   return r;
sl@0: }
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** This routine sets the state to CURSOR_FAULT and the error
sl@0: ** code to errCode for every cursor on BtShared that pBtree
sl@0: ** references.
sl@0: **
sl@0: ** Every cursor is tripped, including cursors that belong
sl@0: ** to other database connections that happen to be sharing
sl@0: ** the cache with pBtree.
sl@0: **
sl@0: ** This routine gets called when a rollback occurs.
sl@0: ** All cursors using the same cache must be tripped
sl@0: ** to prevent them from trying to use the btree after
sl@0: ** the rollback.  The rollback may have deleted tables
sl@0: ** or moved root pages, so it is not sufficient to
sl@0: ** save the state of the cursor.  The cursor must be
sl@0: ** invalidated.
sl@0: */
sl@0: void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
sl@0:   BtCursor *p;
sl@0:   sqlite3BtreeEnter(pBtree);
sl@0:   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
sl@0:     clearCursorPosition(p);
sl@0:     p->eState = CURSOR_FAULT;
sl@0:     p->skip = errCode;
sl@0:   }
sl@0:   sqlite3BtreeLeave(pBtree);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Rollback the transaction in progress.  All cursors will be
sl@0: ** invalided by this operation.  Any attempt to use a cursor
sl@0: ** that was open at the beginning of this operation will result
sl@0: ** in an error.
sl@0: **
sl@0: ** This will release the write lock on the database file.  If there
sl@0: ** are no active cursors, it also releases the read lock.
sl@0: */
sl@0: int sqlite3BtreeRollback(Btree *p){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   MemPage *pPage1;
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   rc = saveAllCursors(pBt, 0, 0);
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     /* This is a horrible situation. An IO or malloc() error occured whilst
sl@0:     ** trying to save cursor positions. If this is an automatic rollback (as
sl@0:     ** the result of a constraint, malloc() failure or IO error) then 
sl@0:     ** the cache may be internally inconsistent (not contain valid trees) so
sl@0:     ** we cannot simply return the error to the caller. Instead, abort 
sl@0:     ** all queries that may be using any of the cursors that failed to save.
sl@0:     */
sl@0:     sqlite3BtreeTripAllCursors(p, rc);
sl@0:   }
sl@0: #endif
sl@0:   btreeIntegrity(p);
sl@0:   unlockAllTables(p);
sl@0: 
sl@0:   if( p->inTrans==TRANS_WRITE ){
sl@0:     int rc2;
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     pBt->nTrunc = 0;
sl@0: #endif
sl@0: 
sl@0:     assert( TRANS_WRITE==pBt->inTransaction );
sl@0:     rc2 = sqlite3PagerRollback(pBt->pPager);
sl@0:     if( rc2!=SQLITE_OK ){
sl@0:       rc = rc2;
sl@0:     }
sl@0: 
sl@0:     /* The rollback may have destroyed the pPage1->aData value.  So
sl@0:     ** call sqlite3BtreeGetPage() on page 1 again to make
sl@0:     ** sure pPage1->aData is set correctly. */
sl@0:     if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
sl@0:       releasePage(pPage1);
sl@0:     }
sl@0:     assert( countWriteCursors(pBt)==0 );
sl@0:     pBt->inTransaction = TRANS_READ;
sl@0:   }
sl@0: 
sl@0:   if( p->inTrans!=TRANS_NONE ){
sl@0:     assert( pBt->nTransaction>0 );
sl@0:     pBt->nTransaction--;
sl@0:     if( 0==pBt->nTransaction ){
sl@0:       pBt->inTransaction = TRANS_NONE;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   p->inTrans = TRANS_NONE;
sl@0:   pBt->inStmt = 0;
sl@0:   unlockBtreeIfUnused(pBt);
sl@0: 
sl@0:   btreeIntegrity(p);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Start a statement subtransaction.  The subtransaction can
sl@0: ** can be rolled back independently of the main transaction.
sl@0: ** You must start a transaction before starting a subtransaction.
sl@0: ** The subtransaction is ended automatically if the main transaction
sl@0: ** commits or rolls back.
sl@0: **
sl@0: ** Only one subtransaction may be active at a time.  It is an error to try
sl@0: ** to start a new subtransaction if another subtransaction is already active.
sl@0: **
sl@0: ** Statement subtransactions are used around individual SQL statements
sl@0: ** that are contained within a BEGIN...COMMIT block.  If a constraint
sl@0: ** error occurs within the statement, the effect of that one statement
sl@0: ** can be rolled back without having to rollback the entire transaction.
sl@0: */
sl@0: int sqlite3BtreeBeginStmt(Btree *p){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:   }else{
sl@0:     assert( pBt->inTransaction==TRANS_WRITE );
sl@0:     rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
sl@0:     pBt->inStmt = 1;
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Commit the statment subtransaction currently in progress.  If no
sl@0: ** subtransaction is active, this is a no-op.
sl@0: */
sl@0: int sqlite3BtreeCommitStmt(Btree *p){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   if( pBt->inStmt && !pBt->readOnly ){
sl@0:     rc = sqlite3PagerStmtCommit(pBt->pPager);
sl@0:   }else{
sl@0:     rc = SQLITE_OK;
sl@0:   }
sl@0:   pBt->inStmt = 0;
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Rollback the active statement subtransaction.  If no subtransaction
sl@0: ** is active this routine is a no-op.
sl@0: **
sl@0: ** All cursors will be invalidated by this operation.  Any attempt
sl@0: ** to use a cursor that was open at the beginning of this operation
sl@0: ** will result in an error.
sl@0: */
sl@0: int sqlite3BtreeRollbackStmt(Btree *p){
sl@0:   int rc = SQLITE_OK;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   if( pBt->inStmt && !pBt->readOnly ){
sl@0:     rc = sqlite3PagerStmtRollback(pBt->pPager);
sl@0:     pBt->inStmt = 0;
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Create a new cursor for the BTree whose root is on the page
sl@0: ** iTable.  The act of acquiring a cursor gets a read lock on 
sl@0: ** the database file.
sl@0: **
sl@0: ** If wrFlag==0, then the cursor can only be used for reading.
sl@0: ** If wrFlag==1, then the cursor can be used for reading or for
sl@0: ** writing if other conditions for writing are also met.  These
sl@0: ** are the conditions that must be met in order for writing to
sl@0: ** be allowed:
sl@0: **
sl@0: ** 1:  The cursor must have been opened with wrFlag==1
sl@0: **
sl@0: ** 2:  Other database connections that share the same pager cache
sl@0: **     but which are not in the READ_UNCOMMITTED state may not have
sl@0: **     cursors open with wrFlag==0 on the same table.  Otherwise
sl@0: **     the changes made by this write cursor would be visible to
sl@0: **     the read cursors in the other database connection.
sl@0: **
sl@0: ** 3:  The database must be writable (not on read-only media)
sl@0: **
sl@0: ** 4:  There must be an active transaction.
sl@0: **
sl@0: ** No checking is done to make sure that page iTable really is the
sl@0: ** root page of a b-tree.  If it is not, then the cursor acquired
sl@0: ** will not work correctly.
sl@0: */
sl@0: static int btreeCursor(
sl@0:   Btree *p,                              /* The btree */
sl@0:   int iTable,                            /* Root page of table to open */
sl@0:   int wrFlag,                            /* 1 to write. 0 read-only */
sl@0:   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
sl@0:   BtCursor *pCur                         /* Space for new cursor */
sl@0: ){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   if( wrFlag ){
sl@0:     if( pBt->readOnly ){
sl@0:       return SQLITE_READONLY;
sl@0:     }
sl@0:     if( checkReadLocks(p, iTable, 0, 0) ){
sl@0:       return SQLITE_LOCKED;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( pBt->pPage1==0 ){
sl@0:     rc = lockBtreeWithRetry(p);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     if( pBt->readOnly && wrFlag ){
sl@0:       return SQLITE_READONLY;
sl@0:     }
sl@0:   }
sl@0:   pCur->pgnoRoot = (Pgno)iTable;
sl@0:   if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){
sl@0:     rc = SQLITE_EMPTY;
sl@0:     goto create_cursor_exception;
sl@0:   }
sl@0:   rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     goto create_cursor_exception;
sl@0:   }
sl@0: 
sl@0:   /* Now that no other errors can occur, finish filling in the BtCursor
sl@0:   ** variables, link the cursor into the BtShared list and set *ppCur (the
sl@0:   ** output argument to this function).
sl@0:   */
sl@0:   pCur->pKeyInfo = pKeyInfo;
sl@0:   pCur->pBtree = p;
sl@0:   pCur->pBt = pBt;
sl@0:   pCur->wrFlag = wrFlag;
sl@0:   pCur->pNext = pBt->pCursor;
sl@0:   if( pCur->pNext ){
sl@0:     pCur->pNext->pPrev = pCur;
sl@0:   }
sl@0:   pBt->pCursor = pCur;
sl@0:   pCur->eState = CURSOR_INVALID;
sl@0: 
sl@0:   return SQLITE_OK;
sl@0: 
sl@0: create_cursor_exception:
sl@0:   releasePage(pCur->pPage);
sl@0:   unlockBtreeIfUnused(pBt);
sl@0:   return rc;
sl@0: }
sl@0: int sqlite3BtreeCursor(
sl@0:   Btree *p,                                   /* The btree */
sl@0:   int iTable,                                 /* Root page of table to open */
sl@0:   int wrFlag,                                 /* 1 to write. 0 read-only */
sl@0:   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
sl@0:   BtCursor *pCur                              /* Write new cursor here */
sl@0: ){
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   p->pBt->db = p->db;
sl@0:   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: int sqlite3BtreeCursorSize(){
sl@0:   return sizeof(BtCursor);
sl@0: }
sl@0: 
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Close a cursor.  The read lock on the database file is released
sl@0: ** when the last cursor is closed.
sl@0: */
sl@0: int sqlite3BtreeCloseCursor(BtCursor *pCur){
sl@0:   Btree *pBtree = pCur->pBtree;
sl@0:   if( pBtree ){
sl@0:     BtShared *pBt = pCur->pBt;
sl@0:     sqlite3BtreeEnter(pBtree);
sl@0:     pBt->db = pBtree->db;
sl@0:     clearCursorPosition(pCur);
sl@0:     if( pCur->pPrev ){
sl@0:       pCur->pPrev->pNext = pCur->pNext;
sl@0:     }else{
sl@0:       pBt->pCursor = pCur->pNext;
sl@0:     }
sl@0:     if( pCur->pNext ){
sl@0:       pCur->pNext->pPrev = pCur->pPrev;
sl@0:     }
sl@0:     releasePage(pCur->pPage);
sl@0:     unlockBtreeIfUnused(pBt);
sl@0:     invalidateOverflowCache(pCur);
sl@0:     /* sqlite3_free(pCur); */
sl@0:     sqlite3BtreeLeave(pBtree);
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Make a temporary cursor by filling in the fields of pTempCur.
sl@0: ** The temporary cursor is not on the cursor list for the Btree.
sl@0: */
sl@0: void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   memcpy(pTempCur, pCur, sizeof(*pCur));
sl@0:   pTempCur->pNext = 0;
sl@0:   pTempCur->pPrev = 0;
sl@0:   if( pTempCur->pPage ){
sl@0:     sqlite3PagerRef(pTempCur->pPage->pDbPage);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
sl@0: ** function above.
sl@0: */
sl@0: void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   if( pCur->pPage ){
sl@0:     sqlite3PagerUnref(pCur->pPage->pDbPage);
sl@0:   }
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Make sure the BtCursor* given in the argument has a valid
sl@0: ** BtCursor.info structure.  If it is not already valid, call
sl@0: ** sqlite3BtreeParseCell() to fill it in.
sl@0: **
sl@0: ** BtCursor.info is a cache of the information in the current cell.
sl@0: ** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
sl@0: **
sl@0: ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
sl@0: ** compiler to crash when getCellInfo() is implemented as a macro.
sl@0: ** But there is a measureable speed advantage to using the macro on gcc
sl@0: ** (when less compiler optimizations like -Os or -O0 are used and the
sl@0: ** compiler is not doing agressive inlining.)  So we use a real function
sl@0: ** for MSVC and a macro for everything else.  Ticket #2457.
sl@0: */
sl@0: #ifndef NDEBUG
sl@0:   static void assertCellInfo(BtCursor *pCur){
sl@0:     CellInfo info;
sl@0:     memset(&info, 0, sizeof(info));
sl@0:     sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info);
sl@0:     assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
sl@0:   }
sl@0: #else
sl@0:   #define assertCellInfo(x)
sl@0: #endif
sl@0: #ifdef _MSC_VER
sl@0:   /* Use a real function in MSVC to work around bugs in that compiler. */
sl@0:   static void getCellInfo(BtCursor *pCur){
sl@0:     if( pCur->info.nSize==0 ){
sl@0:       sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);
sl@0:       pCur->validNKey = 1;
sl@0:     }else{
sl@0:       assertCellInfo(pCur);
sl@0:     }
sl@0:   }
sl@0: #else /* if not _MSC_VER */
sl@0:   /* Use a macro in all other compilers so that the function is inlined */
sl@0: #define getCellInfo(pCur)                                               \
sl@0:   if( pCur->info.nSize==0 ){                                            \
sl@0:     sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);         \
sl@0:     pCur->validNKey = 1;                                                \
sl@0:   }else{                                                                \
sl@0:     assertCellInfo(pCur);                                               \
sl@0:   }
sl@0: #endif /* _MSC_VER */
sl@0: 
sl@0: /*
sl@0: ** Set *pSize to the size of the buffer needed to hold the value of
sl@0: ** the key for the current entry.  If the cursor is not pointing
sl@0: ** to a valid entry, *pSize is set to 0. 
sl@0: **
sl@0: ** For a table with the INTKEY flag set, this routine returns the key
sl@0: ** itself, not the number of bytes in the key.
sl@0: */
sl@0: int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
sl@0:   int rc;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
sl@0:     if( pCur->eState==CURSOR_INVALID ){
sl@0:       *pSize = 0;
sl@0:     }else{
sl@0:       getCellInfo(pCur);
sl@0:       *pSize = pCur->info.nKey;
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Set *pSize to the number of bytes of data in the entry the
sl@0: ** cursor currently points to.  Always return SQLITE_OK.
sl@0: ** Failure is not possible.  If the cursor is not currently
sl@0: ** pointing to an entry (which can happen, for example, if
sl@0: ** the database is empty) then *pSize is set to 0.
sl@0: */
sl@0: int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
sl@0:   int rc;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
sl@0:     if( pCur->eState==CURSOR_INVALID ){
sl@0:       /* Not pointing at a valid entry - set *pSize to 0. */
sl@0:       *pSize = 0;
sl@0:     }else{
sl@0:       getCellInfo(pCur);
sl@0:       *pSize = pCur->info.nData;
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Given the page number of an overflow page in the database (parameter
sl@0: ** ovfl), this function finds the page number of the next page in the 
sl@0: ** linked list of overflow pages. If possible, it uses the auto-vacuum
sl@0: ** pointer-map data instead of reading the content of page ovfl to do so. 
sl@0: **
sl@0: ** If an error occurs an SQLite error code is returned. Otherwise:
sl@0: **
sl@0: ** Unless pPgnoNext is NULL, the page number of the next overflow 
sl@0: ** page in the linked list is written to *pPgnoNext. If page ovfl
sl@0: ** is the last page in its linked list, *pPgnoNext is set to zero. 
sl@0: **
sl@0: ** If ppPage is not NULL, *ppPage is set to the MemPage* handle
sl@0: ** for page ovfl. The underlying pager page may have been requested
sl@0: ** with the noContent flag set, so the page data accessable via
sl@0: ** this handle may not be trusted.
sl@0: */
sl@0: static int getOverflowPage(
sl@0:   BtShared *pBt, 
sl@0:   Pgno ovfl,                   /* Overflow page */
sl@0:   MemPage **ppPage,            /* OUT: MemPage handle */
sl@0:   Pgno *pPgnoNext              /* OUT: Next overflow page number */
sl@0: ){
sl@0:   Pgno next = 0;
sl@0:   int rc = SQLITE_OK;          /* Initialized to placate warning */
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   /* One of these must not be NULL. Otherwise, why call this function? */
sl@0:   assert(ppPage || pPgnoNext);
sl@0: 
sl@0:   /* If pPgnoNext is NULL, then this function is being called to obtain
sl@0:   ** a MemPage* reference only. No page-data is required in this case.
sl@0:   */
sl@0:   if( !pPgnoNext ){
sl@0:     return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
sl@0:   }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:   /* Try to find the next page in the overflow list using the
sl@0:   ** autovacuum pointer-map pages. Guess that the next page in 
sl@0:   ** the overflow list is page number (ovfl+1). If that guess turns 
sl@0:   ** out to be wrong, fall back to loading the data of page 
sl@0:   ** number ovfl to determine the next page number.
sl@0:   */
sl@0:   if( pBt->autoVacuum ){
sl@0:     Pgno pgno;
sl@0:     Pgno iGuess = ovfl+1;
sl@0:     u8 eType;
sl@0: 
sl@0:     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
sl@0:       iGuess++;
sl@0:     }
sl@0: 
sl@0:     if( iGuess<=pagerPagecount(pBt->pPager) ){
sl@0:       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:       if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
sl@0:         next = iGuess;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   if( next==0 || ppPage ){
sl@0:     MemPage *pPage = 0;
sl@0: 
sl@0:     rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
sl@0:     assert(rc==SQLITE_OK || pPage==0);
sl@0:     if( next==0 && rc==SQLITE_OK ){
sl@0:       next = get4byte(pPage->aData);
sl@0:     }
sl@0: 
sl@0:     if( ppPage ){
sl@0:       *ppPage = pPage;
sl@0:     }else{
sl@0:       releasePage(pPage);
sl@0:     }
sl@0:   }
sl@0:   *pPgnoNext = next;
sl@0: 
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Copy data from a buffer to a page, or from a page to a buffer.
sl@0: **
sl@0: ** pPayload is a pointer to data stored on database page pDbPage.
sl@0: ** If argument eOp is false, then nByte bytes of data are copied
sl@0: ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
sl@0: ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
sl@0: ** of data are copied from the buffer pBuf to pPayload.
sl@0: **
sl@0: ** SQLITE_OK is returned on success, otherwise an error code.
sl@0: */
sl@0: static int copyPayload(
sl@0:   void *pPayload,           /* Pointer to page data */
sl@0:   void *pBuf,               /* Pointer to buffer */
sl@0:   int nByte,                /* Number of bytes to copy */
sl@0:   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
sl@0:   DbPage *pDbPage           /* Page containing pPayload */
sl@0: ){
sl@0:   if( eOp ){
sl@0:     /* Copy data from buffer to page (a write operation) */
sl@0:     int rc = sqlite3PagerWrite(pDbPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     memcpy(pPayload, pBuf, nByte);
sl@0:   }else{
sl@0:     /* Copy data from page to buffer (a read operation) */
sl@0:     memcpy(pBuf, pPayload, nByte);
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This function is used to read or overwrite payload information
sl@0: ** for the entry that the pCur cursor is pointing to. If the eOp
sl@0: ** parameter is 0, this is a read operation (data copied into
sl@0: ** buffer pBuf). If it is non-zero, a write (data copied from
sl@0: ** buffer pBuf).
sl@0: **
sl@0: ** A total of "amt" bytes are read or written beginning at "offset".
sl@0: ** Data is read to or from the buffer pBuf.
sl@0: **
sl@0: ** This routine does not make a distinction between key and data.
sl@0: ** It just reads or writes bytes from the payload area.  Data might 
sl@0: ** appear on the main page or be scattered out on multiple overflow 
sl@0: ** pages.
sl@0: **
sl@0: ** If the BtCursor.isIncrblobHandle flag is set, and the current
sl@0: ** cursor entry uses one or more overflow pages, this function
sl@0: ** allocates space for and lazily popluates the overflow page-list 
sl@0: ** cache array (BtCursor.aOverflow). Subsequent calls use this
sl@0: ** cache to make seeking to the supplied offset more efficient.
sl@0: **
sl@0: ** Once an overflow page-list cache has been allocated, it may be
sl@0: ** invalidated if some other cursor writes to the same table, or if
sl@0: ** the cursor is moved to a different row. Additionally, in auto-vacuum
sl@0: ** mode, the following events may invalidate an overflow page-list cache.
sl@0: **
sl@0: **   * An incremental vacuum,
sl@0: **   * A commit in auto_vacuum="full" mode,
sl@0: **   * Creating a table (may require moving an overflow page).
sl@0: */
sl@0: static int accessPayload(
sl@0:   BtCursor *pCur,      /* Cursor pointing to entry to read from */
sl@0:   int offset,          /* Begin reading this far into payload */
sl@0:   int amt,             /* Read this many bytes */
sl@0:   unsigned char *pBuf, /* Write the bytes into this buffer */ 
sl@0:   int skipKey,         /* offset begins at data if this is true */
sl@0:   int eOp              /* zero to read. non-zero to write. */
sl@0: ){
sl@0:   unsigned char *aPayload;
sl@0:   int rc = SQLITE_OK;
sl@0:   u32 nKey;
sl@0:   int iIdx = 0;
sl@0:   MemPage *pPage = pCur->pPage;     /* Btree page of current cursor entry */
sl@0:   BtShared *pBt;                   /* Btree this cursor belongs to */
sl@0: 
sl@0:   assert( pPage );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
sl@0:   assert( offset>=0 );
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0: 
sl@0:   getCellInfo(pCur);
sl@0:   aPayload = pCur->info.pCell + pCur->info.nHeader;
sl@0:   nKey = (pPage->intKey ? 0 : pCur->info.nKey);
sl@0: 
sl@0:   if( skipKey ){
sl@0:     offset += nKey;
sl@0:   }
sl@0:   if( offset+amt > nKey+pCur->info.nData ){
sl@0:     /* Trying to read or write past the end of the data is an error */
sl@0:     return SQLITE_ERROR;
sl@0:   }
sl@0: 
sl@0:   /* Check if data must be read/written to/from the btree page itself. */
sl@0:   if( offset<pCur->info.nLocal ){
sl@0:     int a = amt;
sl@0:     if( a+offset>pCur->info.nLocal ){
sl@0:       a = pCur->info.nLocal - offset;
sl@0:     }
sl@0:     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
sl@0:     offset = 0;
sl@0:     pBuf += a;
sl@0:     amt -= a;
sl@0:   }else{
sl@0:     offset -= pCur->info.nLocal;
sl@0:   }
sl@0: 
sl@0:   pBt = pCur->pBt;
sl@0:   if( rc==SQLITE_OK && amt>0 ){
sl@0:     const int ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
sl@0:     Pgno nextPage;
sl@0: 
sl@0:     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
sl@0:     ** has not been allocated, allocate it now. The array is sized at
sl@0:     ** one entry for each overflow page in the overflow chain. The
sl@0:     ** page number of the first overflow page is stored in aOverflow[0],
sl@0:     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
sl@0:     ** (the cache is lazily populated).
sl@0:     */
sl@0:     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
sl@0:       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
sl@0:       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
sl@0:       if( nOvfl && !pCur->aOverflow ){
sl@0:         rc = SQLITE_NOMEM;
sl@0:       }
sl@0:     }
sl@0: 
sl@0:     /* If the overflow page-list cache has been allocated and the
sl@0:     ** entry for the first required overflow page is valid, skip
sl@0:     ** directly to it.
sl@0:     */
sl@0:     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
sl@0:       iIdx = (offset/ovflSize);
sl@0:       nextPage = pCur->aOverflow[iIdx];
sl@0:       offset = (offset%ovflSize);
sl@0:     }
sl@0: #endif
sl@0: 
sl@0:     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:       /* If required, populate the overflow page-list cache. */
sl@0:       if( pCur->aOverflow ){
sl@0:         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
sl@0:         pCur->aOverflow[iIdx] = nextPage;
sl@0:       }
sl@0: #endif
sl@0: 
sl@0:       if( offset>=ovflSize ){
sl@0:         /* The only reason to read this page is to obtain the page
sl@0:         ** number for the next page in the overflow chain. The page
sl@0:         ** data is not required. So first try to lookup the overflow
sl@0:         ** page-list cache, if any, then fall back to the getOverflowPage()
sl@0:         ** function.
sl@0:         */
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
sl@0:           nextPage = pCur->aOverflow[iIdx+1];
sl@0:         } else 
sl@0: #endif
sl@0:           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
sl@0:         offset -= ovflSize;
sl@0:       }else{
sl@0:         /* Need to read this page properly. It contains some of the
sl@0:         ** range of data that is being read (eOp==0) or written (eOp!=0).
sl@0:         */
sl@0:         DbPage *pDbPage;
sl@0:         int a = amt;
sl@0:         rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
sl@0:         if( rc==SQLITE_OK ){
sl@0:           aPayload = sqlite3PagerGetData(pDbPage);
sl@0:           nextPage = get4byte(aPayload);
sl@0:           if( a + offset > ovflSize ){
sl@0:             a = ovflSize - offset;
sl@0:           }
sl@0:           rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
sl@0:           sqlite3PagerUnref(pDbPage);
sl@0:           offset = 0;
sl@0:           amt -= a;
sl@0:           pBuf += a;
sl@0:         }
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( rc==SQLITE_OK && amt>0 ){
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Read part of the key associated with cursor pCur.  Exactly
sl@0: ** "amt" bytes will be transfered into pBuf[].  The transfer
sl@0: ** begins at "offset".
sl@0: **
sl@0: ** Return SQLITE_OK on success or an error code if anything goes
sl@0: ** wrong.  An error is returned if "offset+amt" is larger than
sl@0: ** the available payload.
sl@0: */
sl@0: int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
sl@0:   int rc;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     assert( pCur->eState==CURSOR_VALID );
sl@0:     assert( pCur->pPage!=0 );
sl@0:     if( pCur->pPage->intKey ){
sl@0:       return SQLITE_CORRUPT_BKPT;
sl@0:     }
sl@0:     assert( pCur->pPage->intKey==0 );
sl@0:     assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
sl@0:     rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Read part of the data associated with cursor pCur.  Exactly
sl@0: ** "amt" bytes will be transfered into pBuf[].  The transfer
sl@0: ** begins at "offset".
sl@0: **
sl@0: ** Return SQLITE_OK on success or an error code if anything goes
sl@0: ** wrong.  An error is returned if "offset+amt" is larger than
sl@0: ** the available payload.
sl@0: */
sl@0: int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
sl@0:   int rc;
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:   if ( pCur->eState==CURSOR_INVALID ){
sl@0:     return SQLITE_ABORT;
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     assert( pCur->eState==CURSOR_VALID );
sl@0:     assert( pCur->pPage!=0 );
sl@0:     assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
sl@0:     rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return a pointer to payload information from the entry that the 
sl@0: ** pCur cursor is pointing to.  The pointer is to the beginning of
sl@0: ** the key if skipKey==0 and it points to the beginning of data if
sl@0: ** skipKey==1.  The number of bytes of available key/data is written
sl@0: ** into *pAmt.  If *pAmt==0, then the value returned will not be
sl@0: ** a valid pointer.
sl@0: **
sl@0: ** This routine is an optimization.  It is common for the entire key
sl@0: ** and data to fit on the local page and for there to be no overflow
sl@0: ** pages.  When that is so, this routine can be used to access the
sl@0: ** key and data without making a copy.  If the key and/or data spills
sl@0: ** onto overflow pages, then accessPayload() must be used to reassembly
sl@0: ** the key/data and copy it into a preallocated buffer.
sl@0: **
sl@0: ** The pointer returned by this routine looks directly into the cached
sl@0: ** page of the database.  The data might change or move the next time
sl@0: ** any btree routine is called.
sl@0: */
sl@0: static const unsigned char *fetchPayload(
sl@0:   BtCursor *pCur,      /* Cursor pointing to entry to read from */
sl@0:   int *pAmt,           /* Write the number of available bytes here */
sl@0:   int skipKey          /* read beginning at data if this is true */
sl@0: ){
sl@0:   unsigned char *aPayload;
sl@0:   MemPage *pPage;
sl@0:   u32 nKey;
sl@0:   int nLocal;
sl@0: 
sl@0:   assert( pCur!=0 && pCur->pPage!=0 );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   pPage = pCur->pPage;
sl@0:   assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
sl@0:   getCellInfo(pCur);
sl@0:   aPayload = pCur->info.pCell;
sl@0:   aPayload += pCur->info.nHeader;
sl@0:   if( pPage->intKey ){
sl@0:     nKey = 0;
sl@0:   }else{
sl@0:     nKey = pCur->info.nKey;
sl@0:   }
sl@0:   if( skipKey ){
sl@0:     aPayload += nKey;
sl@0:     nLocal = pCur->info.nLocal - nKey;
sl@0:   }else{
sl@0:     nLocal = pCur->info.nLocal;
sl@0:     if( nLocal>nKey ){
sl@0:       nLocal = nKey;
sl@0:     }
sl@0:   }
sl@0:   *pAmt = nLocal;
sl@0:   return aPayload;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** For the entry that cursor pCur is point to, return as
sl@0: ** many bytes of the key or data as are available on the local
sl@0: ** b-tree page.  Write the number of available bytes into *pAmt.
sl@0: **
sl@0: ** The pointer returned is ephemeral.  The key/data may move
sl@0: ** or be destroyed on the next call to any Btree routine,
sl@0: ** including calls from other threads against the same cache.
sl@0: ** Hence, a mutex on the BtShared should be held prior to calling
sl@0: ** this routine.
sl@0: **
sl@0: ** These routines is used to get quick access to key and data
sl@0: ** in the common case where no overflow pages are used.
sl@0: */
sl@0: const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   if( pCur->eState==CURSOR_VALID ){
sl@0:     return (const void*)fetchPayload(pCur, pAmt, 0);
sl@0:   }
sl@0:   return 0;
sl@0: }
sl@0: const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   if( pCur->eState==CURSOR_VALID ){
sl@0:     return (const void*)fetchPayload(pCur, pAmt, 1);
sl@0:   }
sl@0:   return 0;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Move the cursor down to a new child page.  The newPgno argument is the
sl@0: ** page number of the child page to move to.
sl@0: */
sl@0: static int moveToChild(BtCursor *pCur, u32 newPgno){
sl@0:   int rc;
sl@0:   MemPage *pNewPage;
sl@0:   MemPage *pOldPage;
sl@0:   BtShared *pBt = pCur->pBt;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
sl@0:   if( rc ) return rc;
sl@0:   pNewPage->idxParent = pCur->idx;
sl@0:   pOldPage = pCur->pPage;
sl@0:   pOldPage->idxShift = 0;
sl@0:   releasePage(pOldPage);
sl@0:   pCur->pPage = pNewPage;
sl@0:   pCur->idx = 0;
sl@0:   pCur->info.nSize = 0;
sl@0:   pCur->validNKey = 0;
sl@0:   if( pNewPage->nCell<1 ){
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return true if the page is the virtual root of its table.
sl@0: **
sl@0: ** The virtual root page is the root page for most tables.  But
sl@0: ** for the table rooted on page 1, sometime the real root page
sl@0: ** is empty except for the right-pointer.  In such cases the
sl@0: ** virtual root page is the page that the right-pointer of page
sl@0: ** 1 is pointing to.
sl@0: */
sl@0: int sqlite3BtreeIsRootPage(MemPage *pPage){
sl@0:   MemPage *pParent;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   pParent = pPage->pParent;
sl@0:   if( pParent==0 ) return 1;
sl@0:   if( pParent->pgno>1 ) return 0;
sl@0:   if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
sl@0:   return 0;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Move the cursor up to the parent page.
sl@0: **
sl@0: ** pCur->idx is set to the cell index that contains the pointer
sl@0: ** to the page we are coming from.  If we are coming from the
sl@0: ** right-most child page then pCur->idx is set to one more than
sl@0: ** the largest cell index.
sl@0: */
sl@0: void sqlite3BtreeMoveToParent(BtCursor *pCur){
sl@0:   MemPage *pParent;
sl@0:   MemPage *pPage;
sl@0:   int idxParent;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   pPage = pCur->pPage;
sl@0:   assert( pPage!=0 );
sl@0:   assert( !sqlite3BtreeIsRootPage(pPage) );
sl@0:   pParent = pPage->pParent;
sl@0:   assert( pParent!=0 );
sl@0:   idxParent = pPage->idxParent;
sl@0:   sqlite3PagerRef(pParent->pDbPage);
sl@0:   releasePage(pPage);
sl@0:   pCur->pPage = pParent;
sl@0:   pCur->info.nSize = 0;
sl@0:   pCur->validNKey = 0;
sl@0:   assert( pParent->idxShift==0 );
sl@0:   pCur->idx = idxParent;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Move the cursor to the root page
sl@0: */
sl@0: static int moveToRoot(BtCursor *pCur){
sl@0:   MemPage *pRoot;
sl@0:   int rc = SQLITE_OK;
sl@0:   Btree *p = pCur->pBtree;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
sl@0:   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
sl@0:   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
sl@0:   if( pCur->eState>=CURSOR_REQUIRESEEK ){
sl@0:     if( pCur->eState==CURSOR_FAULT ){
sl@0:       return pCur->skip;
sl@0:     }
sl@0:     clearCursorPosition(pCur);
sl@0:   }
sl@0:   pRoot = pCur->pPage;
sl@0:   if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
sl@0:     assert( pRoot->isInit );
sl@0:   }else{
sl@0:     if( 
sl@0:       SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
sl@0:     ){
sl@0:       pCur->eState = CURSOR_INVALID;
sl@0:       return rc;
sl@0:     }
sl@0:     releasePage(pCur->pPage);
sl@0:     pCur->pPage = pRoot;
sl@0:   }
sl@0:   pCur->idx = 0;
sl@0:   pCur->info.nSize = 0;
sl@0:   pCur->atLast = 0;
sl@0:   pCur->validNKey = 0;
sl@0:   if( pRoot->nCell==0 && !pRoot->leaf ){
sl@0:     Pgno subpage;
sl@0:     assert( pRoot->pgno==1 );
sl@0:     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
sl@0:     assert( subpage>0 );
sl@0:     pCur->eState = CURSOR_VALID;
sl@0:     rc = moveToChild(pCur, subpage);
sl@0:   }
sl@0:   pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Move the cursor down to the left-most leaf entry beneath the
sl@0: ** entry to which it is currently pointing.
sl@0: **
sl@0: ** The left-most leaf is the one with the smallest key - the first
sl@0: ** in ascending order.
sl@0: */
sl@0: static int moveToLeftmost(BtCursor *pCur){
sl@0:   Pgno pgno;
sl@0:   int rc = SQLITE_OK;
sl@0:   MemPage *pPage;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
sl@0:     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
sl@0:     pgno = get4byte(findCell(pPage, pCur->idx));
sl@0:     rc = moveToChild(pCur, pgno);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Move the cursor down to the right-most leaf entry beneath the
sl@0: ** page to which it is currently pointing.  Notice the difference
sl@0: ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
sl@0: ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
sl@0: ** finds the right-most entry beneath the *page*.
sl@0: **
sl@0: ** The right-most entry is the one with the largest key - the last
sl@0: ** key in ascending order.
sl@0: */
sl@0: static int moveToRightmost(BtCursor *pCur){
sl@0:   Pgno pgno;
sl@0:   int rc = SQLITE_OK;
sl@0:   MemPage *pPage;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pCur->eState==CURSOR_VALID );
sl@0:   while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
sl@0:     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0:     pCur->idx = pPage->nCell;
sl@0:     rc = moveToChild(pCur, pgno);
sl@0:   }
sl@0:   if( rc==SQLITE_OK ){
sl@0:     pCur->idx = pPage->nCell - 1;
sl@0:     pCur->info.nSize = 0;
sl@0:     pCur->validNKey = 0;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /* Move the cursor to the first entry in the table.  Return SQLITE_OK
sl@0: ** on success.  Set *pRes to 0 if the cursor actually points to something
sl@0: ** or set *pRes to 1 if the table is empty.
sl@0: */
sl@0: int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
sl@0:   int rc;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
sl@0:   rc = moveToRoot(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     if( pCur->eState==CURSOR_INVALID ){
sl@0:       assert( pCur->pPage->nCell==0 );
sl@0:       *pRes = 1;
sl@0:       rc = SQLITE_OK;
sl@0:     }else{
sl@0:       assert( pCur->pPage->nCell>0 );
sl@0:       *pRes = 0;
sl@0:       rc = moveToLeftmost(pCur);
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /* Move the cursor to the last entry in the table.  Return SQLITE_OK
sl@0: ** on success.  Set *pRes to 0 if the cursor actually points to something
sl@0: ** or set *pRes to 1 if the table is empty.
sl@0: */
sl@0: int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
sl@0:   int rc;
sl@0:  
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
sl@0:   rc = moveToRoot(pCur);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     if( CURSOR_INVALID==pCur->eState ){
sl@0:       assert( pCur->pPage->nCell==0 );
sl@0:       *pRes = 1;
sl@0:     }else{
sl@0:       assert( pCur->eState==CURSOR_VALID );
sl@0:       *pRes = 0;
sl@0:       rc = moveToRightmost(pCur);
sl@0:       getCellInfo(pCur);
sl@0:       pCur->atLast = rc==SQLITE_OK;
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /* Move the cursor so that it points to an entry near the key 
sl@0: ** specified by pKey/nKey/pUnKey. Return a success code.
sl@0: **
sl@0: ** For INTKEY tables, only the nKey parameter is used.  pKey 
sl@0: ** and pUnKey must be NULL.  For index tables, either pUnKey
sl@0: ** must point to a key that has already been unpacked, or else
sl@0: ** pKey/nKey describes a blob containing the key.
sl@0: **
sl@0: ** If an exact match is not found, then the cursor is always
sl@0: ** left pointing at a leaf page which would hold the entry if it
sl@0: ** were present.  The cursor might point to an entry that comes
sl@0: ** before or after the key.
sl@0: **
sl@0: ** The result of comparing the key with the entry to which the
sl@0: ** cursor is written to *pRes if pRes!=NULL.  The meaning of
sl@0: ** this value is as follows:
sl@0: **
sl@0: **     *pRes<0      The cursor is left pointing at an entry that
sl@0: **                  is smaller than pKey or if the table is empty
sl@0: **                  and the cursor is therefore left point to nothing.
sl@0: **
sl@0: **     *pRes==0     The cursor is left pointing at an entry that
sl@0: **                  exactly matches pKey.
sl@0: **
sl@0: **     *pRes>0      The cursor is left pointing at an entry that
sl@0: **                  is larger than pKey.
sl@0: **
sl@0: */
sl@0: int sqlite3BtreeMoveto(
sl@0:   BtCursor *pCur,        /* The cursor to be moved */
sl@0:   const void *pKey,      /* The key content for indices.  Not used by tables */
sl@0:   UnpackedRecord *pUnKey,/* Unpacked version of pKey */
sl@0:   i64 nKey,              /* Size of pKey.  Or the key for tables */
sl@0:   int biasRight,         /* If true, bias the search to the high end */
sl@0:   int *pRes              /* Search result flag */
sl@0: ){
sl@0:   int rc;
sl@0:   char aSpace[200];
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
sl@0: 
sl@0:   /* If the cursor is already positioned at the point we are trying
sl@0:   ** to move to, then just return without doing any work */
sl@0:   if( pCur->eState==CURSOR_VALID && pCur->validNKey && pCur->pPage->intKey ){
sl@0:     if( pCur->info.nKey==nKey ){
sl@0:       *pRes = 0;
sl@0:       return SQLITE_OK;
sl@0:     }
sl@0:     if( pCur->atLast && pCur->info.nKey<nKey ){
sl@0:       *pRes = -1;
sl@0:       return SQLITE_OK;
sl@0:     }
sl@0:   }
sl@0: 
sl@0: 
sl@0:   rc = moveToRoot(pCur);
sl@0:   if( rc ){
sl@0:     return rc;
sl@0:   }
sl@0:   assert( pCur->pPage );
sl@0:   assert( pCur->pPage->isInit );
sl@0:   if( pCur->eState==CURSOR_INVALID ){
sl@0:     *pRes = -1;
sl@0:     assert( pCur->pPage->nCell==0 );
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   if( pCur->pPage->intKey ){
sl@0:     /* We are given an SQL table to search.  The key is the integer
sl@0:     ** rowid contained in nKey.  pKey and pUnKey should both be NULL */
sl@0:     assert( pUnKey==0 );
sl@0:     assert( pKey==0 );
sl@0:   }else if( pUnKey==0 ){
sl@0:     /* We are to search an SQL index using a key encoded as a blob.
sl@0:     ** The blob is found at pKey and is nKey bytes in length.  Unpack
sl@0:     ** this key so that we can use it. */
sl@0:     assert( pKey!=0 );
sl@0:     pUnKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey,
sl@0:                                    aSpace, sizeof(aSpace));
sl@0:     if( pUnKey==0 ) return SQLITE_NOMEM;
sl@0:   }else{
sl@0:     /* We are to search an SQL index using a key that is already unpacked
sl@0:     ** and handed to us in pUnKey. */
sl@0:     assert( pKey==0 );
sl@0:   }
sl@0:   for(;;){
sl@0:     int lwr, upr;
sl@0:     Pgno chldPg;
sl@0:     MemPage *pPage = pCur->pPage;
sl@0:     int c = -1;  /* pRes return if table is empty must be -1 */
sl@0:     lwr = 0;
sl@0:     upr = pPage->nCell-1;
sl@0:     if( !pPage->intKey && pUnKey==0 ){
sl@0:       rc = SQLITE_CORRUPT_BKPT;
sl@0:       goto moveto_finish;
sl@0:     }
sl@0:     if( biasRight ){
sl@0:       pCur->idx = upr;
sl@0:     }else{
sl@0:       pCur->idx = (upr+lwr)/2;
sl@0:     }
sl@0:     if( lwr<=upr ) for(;;){
sl@0:       void *pCellKey;
sl@0:       i64 nCellKey;
sl@0:       pCur->info.nSize = 0;
sl@0:       pCur->validNKey = 1;
sl@0:       if( pPage->intKey ){
sl@0:         u8 *pCell;
sl@0:         pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
sl@0:         if( pPage->hasData ){
sl@0:           u32 dummy;
sl@0:           pCell += getVarint32(pCell, dummy);
sl@0:         }
sl@0:         getVarint(pCell, (u64*)&nCellKey);
sl@0:         if( nCellKey==nKey ){
sl@0:           c = 0;
sl@0:         }else if( nCellKey<nKey ){
sl@0:           c = -1;
sl@0:         }else{
sl@0:           assert( nCellKey>nKey );
sl@0:           c = +1;
sl@0:         }
sl@0:       }else{
sl@0:         int available;
sl@0:         pCellKey = (void *)fetchPayload(pCur, &available, 0);
sl@0:         nCellKey = pCur->info.nKey;
sl@0:         if( available>=nCellKey ){
sl@0:           c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
sl@0:         }else{
sl@0:           pCellKey = sqlite3Malloc( nCellKey );
sl@0:           if( pCellKey==0 ){
sl@0:             rc = SQLITE_NOMEM;
sl@0:             goto moveto_finish;
sl@0:           }
sl@0:           rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
sl@0:           c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
sl@0:           sqlite3_free(pCellKey);
sl@0:           if( rc ) goto moveto_finish;
sl@0:         }
sl@0:       }
sl@0:       if( c==0 ){
sl@0:         pCur->info.nKey = nCellKey;
sl@0:         if( pPage->intKey && !pPage->leaf ){
sl@0:           lwr = pCur->idx;
sl@0:           upr = lwr - 1;
sl@0:           break;
sl@0:         }else{
sl@0:           if( pRes ) *pRes = 0;
sl@0:           rc = SQLITE_OK;
sl@0:           goto moveto_finish;
sl@0:         }
sl@0:       }
sl@0:       if( c<0 ){
sl@0:         lwr = pCur->idx+1;
sl@0:       }else{
sl@0:         upr = pCur->idx-1;
sl@0:       }
sl@0:       if( lwr>upr ){
sl@0:         pCur->info.nKey = nCellKey;
sl@0:         break;
sl@0:       }
sl@0:       pCur->idx = (lwr+upr)/2;
sl@0:     }
sl@0:     assert( lwr==upr+1 );
sl@0:     assert( pPage->isInit );
sl@0:     if( pPage->leaf ){
sl@0:       chldPg = 0;
sl@0:     }else if( lwr>=pPage->nCell ){
sl@0:       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0:     }else{
sl@0:       chldPg = get4byte(findCell(pPage, lwr));
sl@0:     }
sl@0:     if( chldPg==0 ){
sl@0:       assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
sl@0:       if( pRes ) *pRes = c;
sl@0:       rc = SQLITE_OK;
sl@0:       goto moveto_finish;
sl@0:     }
sl@0:     pCur->idx = lwr;
sl@0:     pCur->info.nSize = 0;
sl@0:     pCur->validNKey = 0;
sl@0:     rc = moveToChild(pCur, chldPg);
sl@0:     if( rc ) goto moveto_finish;
sl@0:   }
sl@0: moveto_finish:
sl@0:   if( pKey ){
sl@0:     /* If we created our own unpacked key at the top of this
sl@0:     ** procedure, then destroy that key before returning. */
sl@0:     sqlite3VdbeDeleteUnpackedRecord(pUnKey);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Return TRUE if the cursor is not pointing at an entry of the table.
sl@0: **
sl@0: ** TRUE will be returned after a call to sqlite3BtreeNext() moves
sl@0: ** past the last entry in the table or sqlite3BtreePrev() moves past
sl@0: ** the first entry.  TRUE is also returned if the table is empty.
sl@0: */
sl@0: int sqlite3BtreeEof(BtCursor *pCur){
sl@0:   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
sl@0:   ** have been deleted? This API will need to change to return an error code
sl@0:   ** as well as the boolean result value.
sl@0:   */
sl@0:   return (CURSOR_VALID!=pCur->eState);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the database connection handle for a cursor.
sl@0: */
sl@0: sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
sl@0:   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
sl@0:   return pCur->pBtree->db;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Advance the cursor to the next entry in the database.  If
sl@0: ** successful then set *pRes=0.  If the cursor
sl@0: ** was already pointing to the last entry in the database before
sl@0: ** this routine was called, then set *pRes=1.
sl@0: */
sl@0: int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
sl@0:   int rc;
sl@0:   MemPage *pPage;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     return rc;
sl@0:   }
sl@0:   assert( pRes!=0 );
sl@0:   pPage = pCur->pPage;
sl@0:   if( CURSOR_INVALID==pCur->eState ){
sl@0:     *pRes = 1;
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   if( pCur->skip>0 ){
sl@0:     pCur->skip = 0;
sl@0:     *pRes = 0;
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   pCur->skip = 0;
sl@0: 
sl@0:   assert( pPage->isInit );
sl@0:   assert( pCur->idx<pPage->nCell );
sl@0: 
sl@0:   pCur->idx++;
sl@0:   pCur->info.nSize = 0;
sl@0:   pCur->validNKey = 0;
sl@0:   if( pCur->idx>=pPage->nCell ){
sl@0:     if( !pPage->leaf ){
sl@0:       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
sl@0:       if( rc ) return rc;
sl@0:       rc = moveToLeftmost(pCur);
sl@0:       *pRes = 0;
sl@0:       return rc;
sl@0:     }
sl@0:     do{
sl@0:       if( sqlite3BtreeIsRootPage(pPage) ){
sl@0:         *pRes = 1;
sl@0:         pCur->eState = CURSOR_INVALID;
sl@0:         return SQLITE_OK;
sl@0:       }
sl@0:       sqlite3BtreeMoveToParent(pCur);
sl@0:       pPage = pCur->pPage;
sl@0:     }while( pCur->idx>=pPage->nCell );
sl@0:     *pRes = 0;
sl@0:     if( pPage->intKey ){
sl@0:       rc = sqlite3BtreeNext(pCur, pRes);
sl@0:     }else{
sl@0:       rc = SQLITE_OK;
sl@0:     }
sl@0:     return rc;
sl@0:   }
sl@0:   *pRes = 0;
sl@0:   if( pPage->leaf ){
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   rc = moveToLeftmost(pCur);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Step the cursor to the back to the previous entry in the database.  If
sl@0: ** successful then set *pRes=0.  If the cursor
sl@0: ** was already pointing to the first entry in the database before
sl@0: ** this routine was called, then set *pRes=1.
sl@0: */
sl@0: int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
sl@0:   int rc;
sl@0:   Pgno pgno;
sl@0:   MemPage *pPage;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   rc = restoreCursorPosition(pCur);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     return rc;
sl@0:   }
sl@0:   pCur->atLast = 0;
sl@0:   if( CURSOR_INVALID==pCur->eState ){
sl@0:     *pRes = 1;
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   if( pCur->skip<0 ){
sl@0:     pCur->skip = 0;
sl@0:     *pRes = 0;
sl@0:     return SQLITE_OK;
sl@0:   }
sl@0:   pCur->skip = 0;
sl@0: 
sl@0:   pPage = pCur->pPage;
sl@0:   assert( pPage->isInit );
sl@0:   assert( pCur->idx>=0 );
sl@0:   if( !pPage->leaf ){
sl@0:     pgno = get4byte( findCell(pPage, pCur->idx) );
sl@0:     rc = moveToChild(pCur, pgno);
sl@0:     if( rc ){
sl@0:       return rc;
sl@0:     }
sl@0:     rc = moveToRightmost(pCur);
sl@0:   }else{
sl@0:     while( pCur->idx==0 ){
sl@0:       if( sqlite3BtreeIsRootPage(pPage) ){
sl@0:         pCur->eState = CURSOR_INVALID;
sl@0:         *pRes = 1;
sl@0:         return SQLITE_OK;
sl@0:       }
sl@0:       sqlite3BtreeMoveToParent(pCur);
sl@0:       pPage = pCur->pPage;
sl@0:     }
sl@0:     pCur->idx--;
sl@0:     pCur->info.nSize = 0;
sl@0:     pCur->validNKey = 0;
sl@0:     if( pPage->intKey && !pPage->leaf ){
sl@0:       rc = sqlite3BtreePrevious(pCur, pRes);
sl@0:     }else{
sl@0:       rc = SQLITE_OK;
sl@0:     }
sl@0:   }
sl@0:   *pRes = 0;
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Allocate a new page from the database file.
sl@0: **
sl@0: ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
sl@0: ** has already been called on the new page.)  The new page has also
sl@0: ** been referenced and the calling routine is responsible for calling
sl@0: ** sqlite3PagerUnref() on the new page when it is done.
sl@0: **
sl@0: ** SQLITE_OK is returned on success.  Any other return value indicates
sl@0: ** an error.  *ppPage and *pPgno are undefined in the event of an error.
sl@0: ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
sl@0: **
sl@0: ** If the "nearby" parameter is not 0, then a (feeble) effort is made to 
sl@0: ** locate a page close to the page number "nearby".  This can be used in an
sl@0: ** attempt to keep related pages close to each other in the database file,
sl@0: ** which in turn can make database access faster.
sl@0: **
sl@0: ** If the "exact" parameter is not 0, and the page-number nearby exists 
sl@0: ** anywhere on the free-list, then it is guarenteed to be returned. This
sl@0: ** is only used by auto-vacuum databases when allocating a new table.
sl@0: */
sl@0: static int allocateBtreePage(
sl@0:   BtShared *pBt, 
sl@0:   MemPage **ppPage, 
sl@0:   Pgno *pPgno, 
sl@0:   Pgno nearby,
sl@0:   u8 exact
sl@0: ){
sl@0:   MemPage *pPage1;
sl@0:   int rc;
sl@0:   int n;     /* Number of pages on the freelist */
sl@0:   int k;     /* Number of leaves on the trunk of the freelist */
sl@0:   MemPage *pTrunk = 0;
sl@0:   MemPage *pPrevTrunk = 0;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   pPage1 = pBt->pPage1;
sl@0:   n = get4byte(&pPage1->aData[36]);
sl@0:   if( n>0 ){
sl@0:     /* There are pages on the freelist.  Reuse one of those pages. */
sl@0:     Pgno iTrunk;
sl@0:     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
sl@0:     
sl@0:     /* If the 'exact' parameter was true and a query of the pointer-map
sl@0:     ** shows that the page 'nearby' is somewhere on the free-list, then
sl@0:     ** the entire-list will be searched for that page.
sl@0:     */
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( exact && nearby<=pagerPagecount(pBt->pPager) ){
sl@0:       u8 eType;
sl@0:       assert( nearby>0 );
sl@0:       assert( pBt->autoVacuum );
sl@0:       rc = ptrmapGet(pBt, nearby, &eType, 0);
sl@0:       if( rc ) return rc;
sl@0:       if( eType==PTRMAP_FREEPAGE ){
sl@0:         searchList = 1;
sl@0:       }
sl@0:       *pPgno = nearby;
sl@0:     }
sl@0: #endif
sl@0: 
sl@0:     /* Decrement the free-list count by 1. Set iTrunk to the index of the
sl@0:     ** first free-list trunk page. iPrevTrunk is initially 1.
sl@0:     */
sl@0:     rc = sqlite3PagerWrite(pPage1->pDbPage);
sl@0:     if( rc ) return rc;
sl@0:     put4byte(&pPage1->aData[36], n-1);
sl@0: 
sl@0:     /* The code within this loop is run only once if the 'searchList' variable
sl@0:     ** is not true. Otherwise, it runs once for each trunk-page on the
sl@0:     ** free-list until the page 'nearby' is located.
sl@0:     */
sl@0:     do {
sl@0:       pPrevTrunk = pTrunk;
sl@0:       if( pPrevTrunk ){
sl@0:         iTrunk = get4byte(&pPrevTrunk->aData[0]);
sl@0:       }else{
sl@0:         iTrunk = get4byte(&pPage1->aData[32]);
sl@0:       }
sl@0:       rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
sl@0:       if( rc ){
sl@0:         pTrunk = 0;
sl@0:         goto end_allocate_page;
sl@0:       }
sl@0: 
sl@0:       k = get4byte(&pTrunk->aData[4]);
sl@0:       if( k==0 && !searchList ){
sl@0:         /* The trunk has no leaves and the list is not being searched. 
sl@0:         ** So extract the trunk page itself and use it as the newly 
sl@0:         ** allocated page */
sl@0:         assert( pPrevTrunk==0 );
sl@0:         rc = sqlite3PagerWrite(pTrunk->pDbPage);
sl@0:         if( rc ){
sl@0:           goto end_allocate_page;
sl@0:         }
sl@0:         *pPgno = iTrunk;
sl@0:         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
sl@0:         *ppPage = pTrunk;
sl@0:         pTrunk = 0;
sl@0:         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
sl@0:       }else if( k>pBt->usableSize/4 - 2 ){
sl@0:         /* Value of k is out of range.  Database corruption */
sl@0:         rc = SQLITE_CORRUPT_BKPT;
sl@0:         goto end_allocate_page;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       }else if( searchList && nearby==iTrunk ){
sl@0:         /* The list is being searched and this trunk page is the page
sl@0:         ** to allocate, regardless of whether it has leaves.
sl@0:         */
sl@0:         assert( *pPgno==iTrunk );
sl@0:         *ppPage = pTrunk;
sl@0:         searchList = 0;
sl@0:         rc = sqlite3PagerWrite(pTrunk->pDbPage);
sl@0:         if( rc ){
sl@0:           goto end_allocate_page;
sl@0:         }
sl@0:         if( k==0 ){
sl@0:           if( !pPrevTrunk ){
sl@0:             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
sl@0:           }else{
sl@0:             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
sl@0:           }
sl@0:         }else{
sl@0:           /* The trunk page is required by the caller but it contains 
sl@0:           ** pointers to free-list leaves. The first leaf becomes a trunk
sl@0:           ** page in this case.
sl@0:           */
sl@0:           MemPage *pNewTrunk;
sl@0:           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
sl@0:           rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
sl@0:           if( rc!=SQLITE_OK ){
sl@0:             goto end_allocate_page;
sl@0:           }
sl@0:           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
sl@0:           if( rc!=SQLITE_OK ){
sl@0:             releasePage(pNewTrunk);
sl@0:             goto end_allocate_page;
sl@0:           }
sl@0:           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
sl@0:           put4byte(&pNewTrunk->aData[4], k-1);
sl@0:           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
sl@0:           releasePage(pNewTrunk);
sl@0:           if( !pPrevTrunk ){
sl@0:             put4byte(&pPage1->aData[32], iNewTrunk);
sl@0:           }else{
sl@0:             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
sl@0:             if( rc ){
sl@0:               goto end_allocate_page;
sl@0:             }
sl@0:             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
sl@0:           }
sl@0:         }
sl@0:         pTrunk = 0;
sl@0:         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
sl@0: #endif
sl@0:       }else{
sl@0:         /* Extract a leaf from the trunk */
sl@0:         int closest;
sl@0:         Pgno iPage;
sl@0:         unsigned char *aData = pTrunk->aData;
sl@0:         rc = sqlite3PagerWrite(pTrunk->pDbPage);
sl@0:         if( rc ){
sl@0:           goto end_allocate_page;
sl@0:         }
sl@0:         if( nearby>0 ){
sl@0:           int i, dist;
sl@0:           closest = 0;
sl@0:           dist = get4byte(&aData[8]) - nearby;
sl@0:           if( dist<0 ) dist = -dist;
sl@0:           for(i=1; i<k; i++){
sl@0:             int d2 = get4byte(&aData[8+i*4]) - nearby;
sl@0:             if( d2<0 ) d2 = -d2;
sl@0:             if( d2<dist ){
sl@0:               closest = i;
sl@0:               dist = d2;
sl@0:             }
sl@0:           }
sl@0:         }else{
sl@0:           closest = 0;
sl@0:         }
sl@0: 
sl@0:         iPage = get4byte(&aData[8+closest*4]);
sl@0:         if( !searchList || iPage==nearby ){
sl@0:           int nPage;
sl@0:           *pPgno = iPage;
sl@0:           nPage = pagerPagecount(pBt->pPager);
sl@0:           if( *pPgno>nPage ){
sl@0:             /* Free page off the end of the file */
sl@0:             rc = SQLITE_CORRUPT_BKPT;
sl@0:             goto end_allocate_page;
sl@0:           }
sl@0:           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
sl@0:                  ": %d more free pages\n",
sl@0:                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
sl@0:           if( closest<k-1 ){
sl@0:             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
sl@0:           }
sl@0:           put4byte(&aData[4], k-1);
sl@0:           rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
sl@0:           if( rc==SQLITE_OK ){
sl@0:             sqlite3PagerDontRollback((*ppPage)->pDbPage);
sl@0:             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
sl@0:             if( rc!=SQLITE_OK ){
sl@0:               releasePage(*ppPage);
sl@0:             }
sl@0:           }
sl@0:           searchList = 0;
sl@0:         }
sl@0:       }
sl@0:       releasePage(pPrevTrunk);
sl@0:       pPrevTrunk = 0;
sl@0:     }while( searchList );
sl@0:   }else{
sl@0:     /* There are no pages on the freelist, so create a new page at the
sl@0:     ** end of the file */
sl@0:     int nPage = pagerPagecount(pBt->pPager);
sl@0:     *pPgno = nPage + 1;
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( pBt->nTrunc ){
sl@0:       /* An incr-vacuum has already run within this transaction. So the
sl@0:       ** page to allocate is not from the physical end of the file, but
sl@0:       ** at pBt->nTrunc. 
sl@0:       */
sl@0:       *pPgno = pBt->nTrunc+1;
sl@0:       if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
sl@0:         (*pPgno)++;
sl@0:       }
sl@0:     }
sl@0:     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
sl@0:       /* If *pPgno refers to a pointer-map page, allocate two new pages
sl@0:       ** at the end of the file instead of one. The first allocated page
sl@0:       ** becomes a new pointer-map page, the second is used by the caller.
sl@0:       */
sl@0:       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
sl@0:       assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
sl@0:       (*pPgno)++;
sl@0:       if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
sl@0:     }
sl@0:     if( pBt->nTrunc ){
sl@0:       pBt->nTrunc = *pPgno;
sl@0:     }
sl@0: #endif
sl@0: 
sl@0:     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
sl@0:     rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
sl@0:     if( rc ) return rc;
sl@0:     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       releasePage(*ppPage);
sl@0:     }
sl@0:     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
sl@0:   }
sl@0: 
sl@0:   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
sl@0: 
sl@0: end_allocate_page:
sl@0:   releasePage(pTrunk);
sl@0:   releasePage(pPrevTrunk);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Add a page of the database file to the freelist.
sl@0: **
sl@0: ** sqlite3PagerUnref() is NOT called for pPage.
sl@0: */
sl@0: static int freePage(MemPage *pPage){
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   MemPage *pPage1 = pBt->pPage1;
sl@0:   int rc, n, k;
sl@0: 
sl@0:   /* Prepare the page for freeing */
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   assert( pPage->pgno>1 );
sl@0:   pPage->isInit = 0;
sl@0:   releasePage(pPage->pParent);
sl@0:   pPage->pParent = 0;
sl@0: 
sl@0:   /* Increment the free page count on pPage1 */
sl@0:   rc = sqlite3PagerWrite(pPage1->pDbPage);
sl@0:   if( rc ) return rc;
sl@0:   n = get4byte(&pPage1->aData[36]);
sl@0:   put4byte(&pPage1->aData[36], n+1);
sl@0: 
sl@0: #ifdef SQLITE_SECURE_DELETE
sl@0:   /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
sl@0:   ** always fully overwrite deleted information with zeros.
sl@0:   */
sl@0:   rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:   if( rc ) return rc;
sl@0:   memset(pPage->aData, 0, pPage->pBt->pageSize);
sl@0: #endif
sl@0: 
sl@0:   /* If the database supports auto-vacuum, write an entry in the pointer-map
sl@0:   ** to indicate that the page is free.
sl@0:   */
sl@0:   if( ISAUTOVACUUM ){
sl@0:     rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
sl@0:     if( rc ) return rc;
sl@0:   }
sl@0: 
sl@0:   if( n==0 ){
sl@0:     /* This is the first free page */
sl@0:     rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:     if( rc ) return rc;
sl@0:     memset(pPage->aData, 0, 8);
sl@0:     put4byte(&pPage1->aData[32], pPage->pgno);
sl@0:     TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
sl@0:   }else{
sl@0:     /* Other free pages already exist.  Retrive the first trunk page
sl@0:     ** of the freelist and find out how many leaves it has. */
sl@0:     MemPage *pTrunk;
sl@0:     rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
sl@0:     if( rc ) return rc;
sl@0:     k = get4byte(&pTrunk->aData[4]);
sl@0:     if( k>=pBt->usableSize/4 - 8 ){
sl@0:       /* The trunk is full.  Turn the page being freed into a new
sl@0:       ** trunk page with no leaves.
sl@0:       **
sl@0:       ** Note that the trunk page is not really full until it contains
sl@0:       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
sl@0:       ** coded.  But due to a coding error in versions of SQLite prior to
sl@0:       ** 3.6.0, databases with freelist trunk pages holding more than
sl@0:       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
sl@0:       ** to maintain backwards compatibility with older versions of SQLite,
sl@0:       ** we will contain to restrict the number of entries to usableSize/4 - 8
sl@0:       ** for now.  At some point in the future (once everyone has upgraded
sl@0:       ** to 3.6.0 or later) we should consider fixing the conditional above
sl@0:       ** to read "usableSize/4-2" instead of "usableSize/4-8".
sl@0:       */
sl@0:       rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         put4byte(pPage->aData, pTrunk->pgno);
sl@0:         put4byte(&pPage->aData[4], 0);
sl@0:         put4byte(&pPage1->aData[32], pPage->pgno);
sl@0:         TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
sl@0:                 pPage->pgno, pTrunk->pgno));
sl@0:       }
sl@0:     }else if( k<0 ){
sl@0:       rc = SQLITE_CORRUPT;
sl@0:     }else{
sl@0:       /* Add the newly freed page as a leaf on the current trunk */
sl@0:       rc = sqlite3PagerWrite(pTrunk->pDbPage);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         put4byte(&pTrunk->aData[4], k+1);
sl@0:         put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
sl@0: #ifndef SQLITE_SECURE_DELETE
sl@0:         sqlite3PagerDontWrite(pPage->pDbPage);
sl@0: #endif
sl@0:       }
sl@0:       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
sl@0:     }
sl@0:     releasePage(pTrunk);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Free any overflow pages associated with the given Cell.
sl@0: */
sl@0: static int clearCell(MemPage *pPage, unsigned char *pCell){
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   CellInfo info;
sl@0:   Pgno ovflPgno;
sl@0:   int rc;
sl@0:   int nOvfl;
sl@0:   int ovflPageSize;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:   if( info.iOverflow==0 ){
sl@0:     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
sl@0:   }
sl@0:   ovflPgno = get4byte(&pCell[info.iOverflow]);
sl@0:   ovflPageSize = pBt->usableSize - 4;
sl@0:   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
sl@0:   assert( ovflPgno==0 || nOvfl>0 );
sl@0:   while( nOvfl-- ){
sl@0:     MemPage *pOvfl;
sl@0:     if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){
sl@0:       return SQLITE_CORRUPT_BKPT;
sl@0:     }
sl@0: 
sl@0:     rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
sl@0:     if( rc ) return rc;
sl@0:     rc = freePage(pOvfl);
sl@0:     sqlite3PagerUnref(pOvfl->pDbPage);
sl@0:     if( rc ) return rc;
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Create the byte sequence used to represent a cell on page pPage
sl@0: ** and write that byte sequence into pCell[].  Overflow pages are
sl@0: ** allocated and filled in as necessary.  The calling procedure
sl@0: ** is responsible for making sure sufficient space has been allocated
sl@0: ** for pCell[].
sl@0: **
sl@0: ** Note that pCell does not necessary need to point to the pPage->aData
sl@0: ** area.  pCell might point to some temporary storage.  The cell will
sl@0: ** be constructed in this temporary area then copied into pPage->aData
sl@0: ** later.
sl@0: */
sl@0: static int fillInCell(
sl@0:   MemPage *pPage,                /* The page that contains the cell */
sl@0:   unsigned char *pCell,          /* Complete text of the cell */
sl@0:   const void *pKey, i64 nKey,    /* The key */
sl@0:   const void *pData,int nData,   /* The data */
sl@0:   int nZero,                     /* Extra zero bytes to append to pData */
sl@0:   int *pnSize                    /* Write cell size here */
sl@0: ){
sl@0:   int nPayload;
sl@0:   const u8 *pSrc;
sl@0:   int nSrc, n, rc;
sl@0:   int spaceLeft;
sl@0:   MemPage *pOvfl = 0;
sl@0:   MemPage *pToRelease = 0;
sl@0:   unsigned char *pPrior;
sl@0:   unsigned char *pPayload;
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   Pgno pgnoOvfl = 0;
sl@0:   int nHeader;
sl@0:   CellInfo info;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0: 
sl@0:   /* Fill in the header. */
sl@0:   nHeader = 0;
sl@0:   if( !pPage->leaf ){
sl@0:     nHeader += 4;
sl@0:   }
sl@0:   if( pPage->hasData ){
sl@0:     nHeader += putVarint(&pCell[nHeader], nData+nZero);
sl@0:   }else{
sl@0:     nData = nZero = 0;
sl@0:   }
sl@0:   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
sl@0:   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:   assert( info.nHeader==nHeader );
sl@0:   assert( info.nKey==nKey );
sl@0:   assert( info.nData==nData+nZero );
sl@0:   
sl@0:   /* Fill in the payload */
sl@0:   nPayload = nData + nZero;
sl@0:   if( pPage->intKey ){
sl@0:     pSrc = pData;
sl@0:     nSrc = nData;
sl@0:     nData = 0;
sl@0:   }else{
sl@0:     nPayload += nKey;
sl@0:     pSrc = pKey;
sl@0:     nSrc = nKey;
sl@0:   }
sl@0:   *pnSize = info.nSize;
sl@0:   spaceLeft = info.nLocal;
sl@0:   pPayload = &pCell[nHeader];
sl@0:   pPrior = &pCell[info.iOverflow];
sl@0: 
sl@0:   while( nPayload>0 ){
sl@0:     if( spaceLeft==0 ){
sl@0:       int isExact = 0;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
sl@0:       if( pBt->autoVacuum ){
sl@0:         do{
sl@0:           pgnoOvfl++;
sl@0:         } while( 
sl@0:           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 
sl@0:         );
sl@0:         if( pgnoOvfl>1 ){
sl@0:           /* isExact = 1; */
sl@0:         }
sl@0:       }
sl@0: #endif
sl@0:       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       /* If the database supports auto-vacuum, and the second or subsequent
sl@0:       ** overflow page is being allocated, add an entry to the pointer-map
sl@0:       ** for that page now. 
sl@0:       **
sl@0:       ** If this is the first overflow page, then write a partial entry 
sl@0:       ** to the pointer-map. If we write nothing to this pointer-map slot,
sl@0:       ** then the optimistic overflow chain processing in clearCell()
sl@0:       ** may misinterpret the uninitialised values and delete the
sl@0:       ** wrong pages from the database.
sl@0:       */
sl@0:       if( pBt->autoVacuum && rc==SQLITE_OK ){
sl@0:         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
sl@0:         rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
sl@0:         if( rc ){
sl@0:           releasePage(pOvfl);
sl@0:         }
sl@0:       }
sl@0: #endif
sl@0:       if( rc ){
sl@0:         releasePage(pToRelease);
sl@0:         return rc;
sl@0:       }
sl@0:       put4byte(pPrior, pgnoOvfl);
sl@0:       releasePage(pToRelease);
sl@0:       pToRelease = pOvfl;
sl@0:       pPrior = pOvfl->aData;
sl@0:       put4byte(pPrior, 0);
sl@0:       pPayload = &pOvfl->aData[4];
sl@0:       spaceLeft = pBt->usableSize - 4;
sl@0:     }
sl@0:     n = nPayload;
sl@0:     if( n>spaceLeft ) n = spaceLeft;
sl@0:     if( nSrc>0 ){
sl@0:       if( n>nSrc ) n = nSrc;
sl@0:       assert( pSrc );
sl@0:       memcpy(pPayload, pSrc, n);
sl@0:     }else{
sl@0:       memset(pPayload, 0, n);
sl@0:     }
sl@0:     nPayload -= n;
sl@0:     pPayload += n;
sl@0:     pSrc += n;
sl@0:     nSrc -= n;
sl@0:     spaceLeft -= n;
sl@0:     if( nSrc==0 ){
sl@0:       nSrc = nData;
sl@0:       pSrc = pData;
sl@0:     }
sl@0:   }
sl@0:   releasePage(pToRelease);
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Change the MemPage.pParent pointer on the page whose number is
sl@0: ** given in the second argument so that MemPage.pParent holds the
sl@0: ** pointer in the third argument.
sl@0: **
sl@0: ** If the final argument, updatePtrmap, is non-zero and the database
sl@0: ** is an auto-vacuum database, then the pointer-map entry for pgno
sl@0: ** is updated.
sl@0: */
sl@0: static int reparentPage(
sl@0:   BtShared *pBt,                /* B-Tree structure */
sl@0:   Pgno pgno,                    /* Page number of child being adopted */
sl@0:   MemPage *pNewParent,          /* New parent of pgno */
sl@0:   int idx,                      /* Index of child page pgno in pNewParent */
sl@0:   int updatePtrmap              /* If true, update pointer-map for pgno */
sl@0: ){
sl@0:   MemPage *pThis;
sl@0:   DbPage *pDbPage;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   assert( pNewParent!=0 );
sl@0:   if( pgno==0 ) return SQLITE_OK;
sl@0:   assert( pBt->pPager!=0 );
sl@0:   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
sl@0:   if( pDbPage ){
sl@0:     pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
sl@0:     if( pThis->isInit ){
sl@0:       assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
sl@0:       if( pThis->pParent!=pNewParent ){
sl@0:         if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
sl@0:         pThis->pParent = pNewParent;
sl@0:         sqlite3PagerRef(pNewParent->pDbPage);
sl@0:       }
sl@0:       pThis->idxParent = idx;
sl@0:     }
sl@0:     sqlite3PagerUnref(pDbPage);
sl@0:   }
sl@0: 
sl@0:   if( ISAUTOVACUUM && updatePtrmap ){
sl@0:     return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
sl@0:   }
sl@0: 
sl@0: #ifndef NDEBUG
sl@0:   /* If the updatePtrmap flag was clear, assert that the entry in the
sl@0:   ** pointer-map is already correct.
sl@0:   */
sl@0:   if( ISAUTOVACUUM ){
sl@0:     pDbPage = sqlite3PagerLookup(pBt->pPager,PTRMAP_PAGENO(pBt,pgno));
sl@0:     if( pDbPage ){
sl@0:       u8 eType;
sl@0:       Pgno ii;
sl@0:       int rc = ptrmapGet(pBt, pgno, &eType, &ii);
sl@0:       assert( rc==SQLITE_OK && ii==pNewParent->pgno && eType==PTRMAP_BTREE );
sl@0:       sqlite3PagerUnref(pDbPage);
sl@0:     }
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Change the pParent pointer of all children of pPage to point back
sl@0: ** to pPage.
sl@0: **
sl@0: ** In other words, for every child of pPage, invoke reparentPage()
sl@0: ** to make sure that each child knows that pPage is its parent.
sl@0: **
sl@0: ** This routine gets called after you memcpy() one page into
sl@0: ** another.
sl@0: **
sl@0: ** If updatePtrmap is true, then the pointer-map entries for all child
sl@0: ** pages of pPage are updated.
sl@0: */
sl@0: static int reparentChildPages(MemPage *pPage, int updatePtrmap){
sl@0:   int rc = SQLITE_OK;
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   if( !pPage->leaf ){
sl@0:     int i;
sl@0:     BtShared *pBt = pPage->pBt;
sl@0:     Pgno iRight = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0: 
sl@0:     for(i=0; i<pPage->nCell; i++){
sl@0:       u8 *pCell = findCell(pPage, i);
sl@0:       rc = reparentPage(pBt, get4byte(pCell), pPage, i, updatePtrmap);
sl@0:       if( rc!=SQLITE_OK ) return rc;
sl@0:     }
sl@0:     rc = reparentPage(pBt, iRight, pPage, i, updatePtrmap);
sl@0:     pPage->idxShift = 0;
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Remove the i-th cell from pPage.  This routine effects pPage only.
sl@0: ** The cell content is not freed or deallocated.  It is assumed that
sl@0: ** the cell content has been copied someplace else.  This routine just
sl@0: ** removes the reference to the cell from pPage.
sl@0: **
sl@0: ** "sz" must be the number of bytes in the cell.
sl@0: */
sl@0: static void dropCell(MemPage *pPage, int idx, int sz){
sl@0:   int i;          /* Loop counter */
sl@0:   int pc;         /* Offset to cell content of cell being deleted */
sl@0:   u8 *data;       /* pPage->aData */
sl@0:   u8 *ptr;        /* Used to move bytes around within data[] */
sl@0: 
sl@0:   assert( idx>=0 && idx<pPage->nCell );
sl@0:   assert( sz==cellSize(pPage, idx) );
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   data = pPage->aData;
sl@0:   ptr = &data[pPage->cellOffset + 2*idx];
sl@0:   pc = get2byte(ptr);
sl@0:   assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
sl@0:   freeSpace(pPage, pc, sz);
sl@0:   for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
sl@0:     ptr[0] = ptr[2];
sl@0:     ptr[1] = ptr[3];
sl@0:   }
sl@0:   pPage->nCell--;
sl@0:   put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
sl@0:   pPage->nFree += 2;
sl@0:   pPage->idxShift = 1;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Insert a new cell on pPage at cell index "i".  pCell points to the
sl@0: ** content of the cell.
sl@0: **
sl@0: ** If the cell content will fit on the page, then put it there.  If it
sl@0: ** will not fit, then make a copy of the cell content into pTemp if
sl@0: ** pTemp is not null.  Regardless of pTemp, allocate a new entry
sl@0: ** in pPage->aOvfl[] and make it point to the cell content (either
sl@0: ** in pTemp or the original pCell) and also record its index. 
sl@0: ** Allocating a new entry in pPage->aCell[] implies that 
sl@0: ** pPage->nOverflow is incremented.
sl@0: **
sl@0: ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
sl@0: ** cell. The caller will overwrite them after this function returns. If
sl@0: ** nSkip is non-zero, then pCell may not point to an invalid memory location 
sl@0: ** (but pCell+nSkip is always valid).
sl@0: */
sl@0: static int insertCell(
sl@0:   MemPage *pPage,   /* Page into which we are copying */
sl@0:   int i,            /* New cell becomes the i-th cell of the page */
sl@0:   u8 *pCell,        /* Content of the new cell */
sl@0:   int sz,           /* Bytes of content in pCell */
sl@0:   u8 *pTemp,        /* Temp storage space for pCell, if needed */
sl@0:   u8 nSkip          /* Do not write the first nSkip bytes of the cell */
sl@0: ){
sl@0:   int idx;          /* Where to write new cell content in data[] */
sl@0:   int j;            /* Loop counter */
sl@0:   int top;          /* First byte of content for any cell in data[] */
sl@0:   int end;          /* First byte past the last cell pointer in data[] */
sl@0:   int ins;          /* Index in data[] where new cell pointer is inserted */
sl@0:   int hdr;          /* Offset into data[] of the page header */
sl@0:   int cellOffset;   /* Address of first cell pointer in data[] */
sl@0:   u8 *data;         /* The content of the whole page */
sl@0:   u8 *ptr;          /* Used for moving information around in data[] */
sl@0: 
sl@0:   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
sl@0:   assert( sz==cellSizePtr(pPage, pCell) );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   if( pPage->nOverflow || sz+2>pPage->nFree ){
sl@0:     if( pTemp ){
sl@0:       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
sl@0:       pCell = pTemp;
sl@0:     }
sl@0:     j = pPage->nOverflow++;
sl@0:     assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
sl@0:     pPage->aOvfl[j].pCell = pCell;
sl@0:     pPage->aOvfl[j].idx = i;
sl@0:     pPage->nFree = 0;
sl@0:   }else{
sl@0:     int rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
sl@0:     data = pPage->aData;
sl@0:     hdr = pPage->hdrOffset;
sl@0:     top = get2byte(&data[hdr+5]);
sl@0:     cellOffset = pPage->cellOffset;
sl@0:     end = cellOffset + 2*pPage->nCell + 2;
sl@0:     ins = cellOffset + 2*i;
sl@0:     if( end > top - sz ){
sl@0:       defragmentPage(pPage);
sl@0:       top = get2byte(&data[hdr+5]);
sl@0:       assert( end + sz <= top );
sl@0:     }
sl@0:     idx = allocateSpace(pPage, sz);
sl@0:     assert( idx>0 );
sl@0:     assert( end <= get2byte(&data[hdr+5]) );
sl@0:     pPage->nCell++;
sl@0:     pPage->nFree -= 2;
sl@0:     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
sl@0:     for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
sl@0:       ptr[0] = ptr[-2];
sl@0:       ptr[1] = ptr[-1];
sl@0:     }
sl@0:     put2byte(&data[ins], idx);
sl@0:     put2byte(&data[hdr+3], pPage->nCell);
sl@0:     pPage->idxShift = 1;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( pPage->pBt->autoVacuum ){
sl@0:       /* The cell may contain a pointer to an overflow page. If so, write
sl@0:       ** the entry for the overflow page into the pointer map.
sl@0:       */
sl@0:       CellInfo info;
sl@0:       sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:       assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
sl@0:       if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
sl@0:         Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
sl@0:         rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
sl@0:         if( rc!=SQLITE_OK ) return rc;
sl@0:       }
sl@0:     }
sl@0: #endif
sl@0:   }
sl@0: 
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Add a list of cells to a page.  The page should be initially empty.
sl@0: ** The cells are guaranteed to fit on the page.
sl@0: */
sl@0: static void assemblePage(
sl@0:   MemPage *pPage,   /* The page to be assemblied */
sl@0:   int nCell,        /* The number of cells to add to this page */
sl@0:   u8 **apCell,      /* Pointers to cell bodies */
sl@0:   u16 *aSize        /* Sizes of the cells */
sl@0: ){
sl@0:   int i;            /* Loop counter */
sl@0:   int totalSize;    /* Total size of all cells */
sl@0:   int hdr;          /* Index of page header */
sl@0:   int cellptr;      /* Address of next cell pointer */
sl@0:   int cellbody;     /* Address of next cell body */
sl@0:   u8 *data;         /* Data for the page */
sl@0: 
sl@0:   assert( pPage->nOverflow==0 );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   totalSize = 0;
sl@0:   for(i=0; i<nCell; i++){
sl@0:     totalSize += aSize[i];
sl@0:   }
sl@0:   assert( totalSize+2*nCell<=pPage->nFree );
sl@0:   assert( pPage->nCell==0 );
sl@0:   cellptr = pPage->cellOffset;
sl@0:   data = pPage->aData;
sl@0:   hdr = pPage->hdrOffset;
sl@0:   put2byte(&data[hdr+3], nCell);
sl@0:   if( nCell ){
sl@0:     cellbody = allocateSpace(pPage, totalSize);
sl@0:     assert( cellbody>0 );
sl@0:     assert( pPage->nFree >= 2*nCell );
sl@0:     pPage->nFree -= 2*nCell;
sl@0:     for(i=0; i<nCell; i++){
sl@0:       put2byte(&data[cellptr], cellbody);
sl@0:       memcpy(&data[cellbody], apCell[i], aSize[i]);
sl@0:       cellptr += 2;
sl@0:       cellbody += aSize[i];
sl@0:     }
sl@0:     assert( cellbody==pPage->pBt->usableSize );
sl@0:   }
sl@0:   pPage->nCell = nCell;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** The following parameters determine how many adjacent pages get involved
sl@0: ** in a balancing operation.  NN is the number of neighbors on either side
sl@0: ** of the page that participate in the balancing operation.  NB is the
sl@0: ** total number of pages that participate, including the target page and
sl@0: ** NN neighbors on either side.
sl@0: **
sl@0: ** The minimum value of NN is 1 (of course).  Increasing NN above 1
sl@0: ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
sl@0: ** in exchange for a larger degradation in INSERT and UPDATE performance.
sl@0: ** The value of NN appears to give the best results overall.
sl@0: */
sl@0: #define NN 1             /* Number of neighbors on either side of pPage */
sl@0: #define NB (NN*2+1)      /* Total pages involved in the balance */
sl@0: 
sl@0: /* Forward reference */
sl@0: static int balance(MemPage*, int);
sl@0: 
sl@0: #ifndef SQLITE_OMIT_QUICKBALANCE
sl@0: /*
sl@0: ** This version of balance() handles the common special case where
sl@0: ** a new entry is being inserted on the extreme right-end of the
sl@0: ** tree, in other words, when the new entry will become the largest
sl@0: ** entry in the tree.
sl@0: **
sl@0: ** Instead of trying balance the 3 right-most leaf pages, just add
sl@0: ** a new page to the right-hand side and put the one new entry in
sl@0: ** that page.  This leaves the right side of the tree somewhat
sl@0: ** unbalanced.  But odds are that we will be inserting new entries
sl@0: ** at the end soon afterwards so the nearly empty page will quickly
sl@0: ** fill up.  On average.
sl@0: **
sl@0: ** pPage is the leaf page which is the right-most page in the tree.
sl@0: ** pParent is its parent.  pPage must have a single overflow entry
sl@0: ** which is also the right-most entry on the page.
sl@0: */
sl@0: static int balance_quick(MemPage *pPage, MemPage *pParent){
sl@0:   int rc;
sl@0:   MemPage *pNew;
sl@0:   Pgno pgnoNew;
sl@0:   u8 *pCell;
sl@0:   u16 szCell;
sl@0:   CellInfo info;
sl@0:   BtShared *pBt = pPage->pBt;
sl@0:   int parentIdx = pParent->nCell;   /* pParent new divider cell index */
sl@0:   int parentSize;                   /* Size of new divider cell */
sl@0:   u8 parentCell[64];                /* Space for the new divider cell */
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0: 
sl@0:   /* Allocate a new page. Insert the overflow cell from pPage
sl@0:   ** into it. Then remove the overflow cell from pPage.
sl@0:   */
sl@0:   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     return rc;
sl@0:   }
sl@0:   pCell = pPage->aOvfl[0].pCell;
sl@0:   szCell = cellSizePtr(pPage, pCell);
sl@0:   zeroPage(pNew, pPage->aData[0]);
sl@0:   assemblePage(pNew, 1, &pCell, &szCell);
sl@0:   pPage->nOverflow = 0;
sl@0: 
sl@0:   /* Set the parent of the newly allocated page to pParent. */
sl@0:   pNew->pParent = pParent;
sl@0:   sqlite3PagerRef(pParent->pDbPage);
sl@0: 
sl@0:   /* pPage is currently the right-child of pParent. Change this
sl@0:   ** so that the right-child is the new page allocated above and
sl@0:   ** pPage is the next-to-right child. 
sl@0:   **
sl@0:   ** Ignore the return value of the call to fillInCell(). fillInCell()
sl@0:   ** may only return other than SQLITE_OK if it is required to allocate
sl@0:   ** one or more overflow pages. Since an internal table B-Tree cell 
sl@0:   ** may never spill over onto an overflow page (it is a maximum of 
sl@0:   ** 13 bytes in size), it is not neccessary to check the return code.
sl@0:   **
sl@0:   ** Similarly, the insertCell() function cannot fail if the page
sl@0:   ** being inserted into is already writable and the cell does not 
sl@0:   ** contain an overflow pointer. So ignore this return code too.
sl@0:   */
sl@0:   assert( pPage->nCell>0 );
sl@0:   pCell = findCell(pPage, pPage->nCell-1);
sl@0:   sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:   fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
sl@0:   assert( parentSize<64 );
sl@0:   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
sl@0:   insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
sl@0:   put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
sl@0:   put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
sl@0: 
sl@0:   /* If this is an auto-vacuum database, update the pointer map
sl@0:   ** with entries for the new page, and any pointer from the 
sl@0:   ** cell on the page to an overflow page.
sl@0:   */
sl@0:   if( ISAUTOVACUUM ){
sl@0:     rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       rc = ptrmapPutOvfl(pNew, 0);
sl@0:     }
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       releasePage(pNew);
sl@0:       return rc;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Release the reference to the new page and balance the parent page,
sl@0:   ** in case the divider cell inserted caused it to become overfull.
sl@0:   */
sl@0:   releasePage(pNew);
sl@0:   return balance(pParent, 0);
sl@0: }
sl@0: #endif /* SQLITE_OMIT_QUICKBALANCE */
sl@0: 
sl@0: /*
sl@0: ** This routine redistributes Cells on pPage and up to NN*2 siblings
sl@0: ** of pPage so that all pages have about the same amount of free space.
sl@0: ** Usually NN siblings on either side of pPage is used in the balancing,
sl@0: ** though more siblings might come from one side if pPage is the first
sl@0: ** or last child of its parent.  If pPage has fewer than 2*NN siblings
sl@0: ** (something which can only happen if pPage is the root page or a 
sl@0: ** child of root) then all available siblings participate in the balancing.
sl@0: **
sl@0: ** The number of siblings of pPage might be increased or decreased by one or
sl@0: ** two in an effort to keep pages nearly full but not over full. The root page
sl@0: ** is special and is allowed to be nearly empty. If pPage is 
sl@0: ** the root page, then the depth of the tree might be increased
sl@0: ** or decreased by one, as necessary, to keep the root page from being
sl@0: ** overfull or completely empty.
sl@0: **
sl@0: ** Note that when this routine is called, some of the Cells on pPage
sl@0: ** might not actually be stored in pPage->aData[].  This can happen
sl@0: ** if the page is overfull.  Part of the job of this routine is to
sl@0: ** make sure all Cells for pPage once again fit in pPage->aData[].
sl@0: **
sl@0: ** In the course of balancing the siblings of pPage, the parent of pPage
sl@0: ** might become overfull or underfull.  If that happens, then this routine
sl@0: ** is called recursively on the parent.
sl@0: **
sl@0: ** If this routine fails for any reason, it might leave the database
sl@0: ** in a corrupted state.  So if this routine fails, the database should
sl@0: ** be rolled back.
sl@0: */
sl@0: static int balance_nonroot(MemPage *pPage){
sl@0:   MemPage *pParent;            /* The parent of pPage */
sl@0:   BtShared *pBt;               /* The whole database */
sl@0:   int nCell = 0;               /* Number of cells in apCell[] */
sl@0:   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
sl@0:   int nOld;                    /* Number of pages in apOld[] */
sl@0:   int nNew;                    /* Number of pages in apNew[] */
sl@0:   int nDiv;                    /* Number of cells in apDiv[] */
sl@0:   int i, j, k;                 /* Loop counters */
sl@0:   int idx;                     /* Index of pPage in pParent->aCell[] */
sl@0:   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
sl@0:   int rc;                      /* The return code */
sl@0:   int leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
sl@0:   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
sl@0:   int usableSpace;             /* Bytes in pPage beyond the header */
sl@0:   int pageFlags;               /* Value of pPage->aData[0] */
sl@0:   int subtotal;                /* Subtotal of bytes in cells on one page */
sl@0:   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
sl@0:   int iSpace2 = 0;             /* First unused byte of aSpace2[] */
sl@0:   int szScratch;               /* Size of scratch memory requested */
sl@0:   MemPage *apOld[NB];          /* pPage and up to two siblings */
sl@0:   Pgno pgnoOld[NB];            /* Page numbers for each page in apOld[] */
sl@0:   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
sl@0:   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
sl@0:   Pgno pgnoNew[NB+2];          /* Page numbers for each page in apNew[] */
sl@0:   u8 *apDiv[NB];               /* Divider cells in pParent */
sl@0:   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
sl@0:   int szNew[NB+2];             /* Combined size of cells place on i-th page */
sl@0:   u8 **apCell = 0;             /* All cells begin balanced */
sl@0:   u16 *szCell;                 /* Local size of all cells in apCell[] */
sl@0:   u8 *aCopy[NB];         /* Space for holding data of apCopy[] */
sl@0:   u8 *aSpace1;           /* Space for copies of dividers cells before balance */
sl@0:   u8 *aSpace2 = 0;       /* Space for overflow dividers cells after balance */
sl@0:   u8 *aFrom = 0;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0: 
sl@0:   /* 
sl@0:   ** Find the parent page.
sl@0:   */
sl@0:   assert( pPage->isInit );
sl@0:   assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
sl@0:   pBt = pPage->pBt;
sl@0:   pParent = pPage->pParent;
sl@0:   assert( pParent );
sl@0:   if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
sl@0: 
sl@0: #ifndef SQLITE_OMIT_QUICKBALANCE
sl@0:   /*
sl@0:   ** A special case:  If a new entry has just been inserted into a
sl@0:   ** table (that is, a btree with integer keys and all data at the leaves)
sl@0:   ** and the new entry is the right-most entry in the tree (it has the
sl@0:   ** largest key) then use the special balance_quick() routine for
sl@0:   ** balancing.  balance_quick() is much faster and results in a tighter
sl@0:   ** packing of data in the common case.
sl@0:   */
sl@0:   if( pPage->leaf &&
sl@0:       pPage->intKey &&
sl@0:       pPage->nOverflow==1 &&
sl@0:       pPage->aOvfl[0].idx==pPage->nCell &&
sl@0:       pPage->pParent->pgno!=1 &&
sl@0:       get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
sl@0:   ){
sl@0:     assert( pPage->intKey );
sl@0:     /*
sl@0:     ** TODO: Check the siblings to the left of pPage. It may be that
sl@0:     ** they are not full and no new page is required.
sl@0:     */
sl@0:     return balance_quick(pPage, pParent);
sl@0:   }
sl@0: #endif
sl@0: 
sl@0:   if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Find the cell in the parent page whose left child points back
sl@0:   ** to pPage.  The "idx" variable is the index of that cell.  If pPage
sl@0:   ** is the rightmost child of pParent then set idx to pParent->nCell 
sl@0:   */
sl@0:   if( pParent->idxShift ){
sl@0:     Pgno pgno;
sl@0:     pgno = pPage->pgno;
sl@0:     assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
sl@0:     for(idx=0; idx<pParent->nCell; idx++){
sl@0:       if( get4byte(findCell(pParent, idx))==pgno ){
sl@0:         break;
sl@0:       }
sl@0:     }
sl@0:     assert( idx<pParent->nCell
sl@0:              || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
sl@0:   }else{
sl@0:     idx = pPage->idxParent;
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Initialize variables so that it will be safe to jump
sl@0:   ** directly to balance_cleanup at any moment.
sl@0:   */
sl@0:   nOld = nNew = 0;
sl@0:   sqlite3PagerRef(pParent->pDbPage);
sl@0: 
sl@0:   /*
sl@0:   ** Find sibling pages to pPage and the cells in pParent that divide
sl@0:   ** the siblings.  An attempt is made to find NN siblings on either
sl@0:   ** side of pPage.  More siblings are taken from one side, however, if
sl@0:   ** pPage there are fewer than NN siblings on the other side.  If pParent
sl@0:   ** has NB or fewer children then all children of pParent are taken.
sl@0:   */
sl@0:   nxDiv = idx - NN;
sl@0:   if( nxDiv + NB > pParent->nCell ){
sl@0:     nxDiv = pParent->nCell - NB + 1;
sl@0:   }
sl@0:   if( nxDiv<0 ){
sl@0:     nxDiv = 0;
sl@0:   }
sl@0:   nDiv = 0;
sl@0:   for(i=0, k=nxDiv; i<NB; i++, k++){
sl@0:     if( k<pParent->nCell ){
sl@0:       apDiv[i] = findCell(pParent, k);
sl@0:       nDiv++;
sl@0:       assert( !pParent->leaf );
sl@0:       pgnoOld[i] = get4byte(apDiv[i]);
sl@0:     }else if( k==pParent->nCell ){
sl@0:       pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
sl@0:     }else{
sl@0:       break;
sl@0:     }
sl@0:     rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
sl@0:     if( rc ) goto balance_cleanup;
sl@0:     apOld[i]->idxParent = k;
sl@0:     apCopy[i] = 0;
sl@0:     assert( i==nOld );
sl@0:     nOld++;
sl@0:     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
sl@0:   }
sl@0: 
sl@0:   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
sl@0:   ** alignment */
sl@0:   nMaxCells = (nMaxCells + 3)&~3;
sl@0: 
sl@0:   /*
sl@0:   ** Allocate space for memory structures
sl@0:   */
sl@0:   szScratch =
sl@0:        nMaxCells*sizeof(u8*)                       /* apCell */
sl@0:      + nMaxCells*sizeof(u16)                       /* szCell */
sl@0:      + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB  /* aCopy */
sl@0:      + pBt->pageSize                               /* aSpace1 */
sl@0:      + (ISAUTOVACUUM ? nMaxCells : 0);             /* aFrom */
sl@0:   apCell = sqlite3ScratchMalloc( szScratch ); 
sl@0:   if( apCell==0 ){
sl@0:     rc = SQLITE_NOMEM;
sl@0:     goto balance_cleanup;
sl@0:   }
sl@0:   szCell = (u16*)&apCell[nMaxCells];
sl@0:   aCopy[0] = (u8*)&szCell[nMaxCells];
sl@0:   assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
sl@0:   for(i=1; i<NB; i++){
sl@0:     aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
sl@0:     assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
sl@0:   }
sl@0:   aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
sl@0:   assert( ((aSpace1 - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
sl@0:   if( ISAUTOVACUUM ){
sl@0:     aFrom = &aSpace1[pBt->pageSize];
sl@0:   }
sl@0:   aSpace2 = sqlite3PageMalloc(pBt->pageSize);
sl@0:   if( aSpace2==0 ){
sl@0:     rc = SQLITE_NOMEM;
sl@0:     goto balance_cleanup;
sl@0:   }
sl@0:   
sl@0:   /*
sl@0:   ** Make copies of the content of pPage and its siblings into aOld[].
sl@0:   ** The rest of this function will use data from the copies rather
sl@0:   ** that the original pages since the original pages will be in the
sl@0:   ** process of being overwritten.
sl@0:   */
sl@0:   for(i=0; i<nOld; i++){
sl@0:     MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
sl@0:     memcpy(p, apOld[i], sizeof(MemPage));
sl@0:     p->aData = (void*)&p[1];
sl@0:     memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Load pointers to all cells on sibling pages and the divider cells
sl@0:   ** into the local apCell[] array.  Make copies of the divider cells
sl@0:   ** into space obtained form aSpace1[] and remove the the divider Cells
sl@0:   ** from pParent.
sl@0:   **
sl@0:   ** If the siblings are on leaf pages, then the child pointers of the
sl@0:   ** divider cells are stripped from the cells before they are copied
sl@0:   ** into aSpace1[].  In this way, all cells in apCell[] are without
sl@0:   ** child pointers.  If siblings are not leaves, then all cell in
sl@0:   ** apCell[] include child pointers.  Either way, all cells in apCell[]
sl@0:   ** are alike.
sl@0:   **
sl@0:   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
sl@0:   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
sl@0:   */
sl@0:   nCell = 0;
sl@0:   leafCorrection = pPage->leaf*4;
sl@0:   leafData = pPage->hasData;
sl@0:   for(i=0; i<nOld; i++){
sl@0:     MemPage *pOld = apCopy[i];
sl@0:     int limit = pOld->nCell+pOld->nOverflow;
sl@0:     for(j=0; j<limit; j++){
sl@0:       assert( nCell<nMaxCells );
sl@0:       apCell[nCell] = findOverflowCell(pOld, j);
sl@0:       szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
sl@0:       if( ISAUTOVACUUM ){
sl@0:         int a;
sl@0:         aFrom[nCell] = i;
sl@0:         for(a=0; a<pOld->nOverflow; a++){
sl@0:           if( pOld->aOvfl[a].pCell==apCell[nCell] ){
sl@0:             aFrom[nCell] = 0xFF;
sl@0:             break;
sl@0:           }
sl@0:         }
sl@0:       }
sl@0:       nCell++;
sl@0:     }
sl@0:     if( i<nOld-1 ){
sl@0:       u16 sz = cellSizePtr(pParent, apDiv[i]);
sl@0:       if( leafData ){
sl@0:         /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
sl@0:         ** are duplicates of keys on the child pages.  We need to remove
sl@0:         ** the divider cells from pParent, but the dividers cells are not
sl@0:         ** added to apCell[] because they are duplicates of child cells.
sl@0:         */
sl@0:         dropCell(pParent, nxDiv, sz);
sl@0:       }else{
sl@0:         u8 *pTemp;
sl@0:         assert( nCell<nMaxCells );
sl@0:         szCell[nCell] = sz;
sl@0:         pTemp = &aSpace1[iSpace1];
sl@0:         iSpace1 += sz;
sl@0:         assert( sz<=pBt->pageSize/4 );
sl@0:         assert( iSpace1<=pBt->pageSize );
sl@0:         memcpy(pTemp, apDiv[i], sz);
sl@0:         apCell[nCell] = pTemp+leafCorrection;
sl@0:         if( ISAUTOVACUUM ){
sl@0:           aFrom[nCell] = 0xFF;
sl@0:         }
sl@0:         dropCell(pParent, nxDiv, sz);
sl@0:         szCell[nCell] -= leafCorrection;
sl@0:         assert( get4byte(pTemp)==pgnoOld[i] );
sl@0:         if( !pOld->leaf ){
sl@0:           assert( leafCorrection==0 );
sl@0:           /* The right pointer of the child page pOld becomes the left
sl@0:           ** pointer of the divider cell */
sl@0:           memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
sl@0:         }else{
sl@0:           assert( leafCorrection==4 );
sl@0:           if( szCell[nCell]<4 ){
sl@0:             /* Do not allow any cells smaller than 4 bytes. */
sl@0:             szCell[nCell] = 4;
sl@0:           }
sl@0:         }
sl@0:         nCell++;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Figure out the number of pages needed to hold all nCell cells.
sl@0:   ** Store this number in "k".  Also compute szNew[] which is the total
sl@0:   ** size of all cells on the i-th page and cntNew[] which is the index
sl@0:   ** in apCell[] of the cell that divides page i from page i+1.  
sl@0:   ** cntNew[k] should equal nCell.
sl@0:   **
sl@0:   ** Values computed by this block:
sl@0:   **
sl@0:   **           k: The total number of sibling pages
sl@0:   **    szNew[i]: Spaced used on the i-th sibling page.
sl@0:   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
sl@0:   **              the right of the i-th sibling page.
sl@0:   ** usableSpace: Number of bytes of space available on each sibling.
sl@0:   ** 
sl@0:   */
sl@0:   usableSpace = pBt->usableSize - 12 + leafCorrection;
sl@0:   for(subtotal=k=i=0; i<nCell; i++){
sl@0:     assert( i<nMaxCells );
sl@0:     subtotal += szCell[i] + 2;
sl@0:     if( subtotal > usableSpace ){
sl@0:       szNew[k] = subtotal - szCell[i];
sl@0:       cntNew[k] = i;
sl@0:       if( leafData ){ i--; }
sl@0:       subtotal = 0;
sl@0:       k++;
sl@0:     }
sl@0:   }
sl@0:   szNew[k] = subtotal;
sl@0:   cntNew[k] = nCell;
sl@0:   k++;
sl@0: 
sl@0:   /*
sl@0:   ** The packing computed by the previous block is biased toward the siblings
sl@0:   ** on the left side.  The left siblings are always nearly full, while the
sl@0:   ** right-most sibling might be nearly empty.  This block of code attempts
sl@0:   ** to adjust the packing of siblings to get a better balance.
sl@0:   **
sl@0:   ** This adjustment is more than an optimization.  The packing above might
sl@0:   ** be so out of balance as to be illegal.  For example, the right-most
sl@0:   ** sibling might be completely empty.  This adjustment is not optional.
sl@0:   */
sl@0:   for(i=k-1; i>0; i--){
sl@0:     int szRight = szNew[i];  /* Size of sibling on the right */
sl@0:     int szLeft = szNew[i-1]; /* Size of sibling on the left */
sl@0:     int r;              /* Index of right-most cell in left sibling */
sl@0:     int d;              /* Index of first cell to the left of right sibling */
sl@0: 
sl@0:     r = cntNew[i-1] - 1;
sl@0:     d = r + 1 - leafData;
sl@0:     assert( d<nMaxCells );
sl@0:     assert( r<nMaxCells );
sl@0:     while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
sl@0:       szRight += szCell[d] + 2;
sl@0:       szLeft -= szCell[r] + 2;
sl@0:       cntNew[i-1]--;
sl@0:       r = cntNew[i-1] - 1;
sl@0:       d = r + 1 - leafData;
sl@0:     }
sl@0:     szNew[i] = szRight;
sl@0:     szNew[i-1] = szLeft;
sl@0:   }
sl@0: 
sl@0:   /* Either we found one or more cells (cntnew[0])>0) or we are the
sl@0:   ** a virtual root page.  A virtual root page is when the real root
sl@0:   ** page is page 1 and we are the only child of that page.
sl@0:   */
sl@0:   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
sl@0: 
sl@0:   /*
sl@0:   ** Allocate k new pages.  Reuse old pages where possible.
sl@0:   */
sl@0:   assert( pPage->pgno>1 );
sl@0:   pageFlags = pPage->aData[0];
sl@0:   for(i=0; i<k; i++){
sl@0:     MemPage *pNew;
sl@0:     if( i<nOld ){
sl@0:       pNew = apNew[i] = apOld[i];
sl@0:       pgnoNew[i] = pgnoOld[i];
sl@0:       apOld[i] = 0;
sl@0:       rc = sqlite3PagerWrite(pNew->pDbPage);
sl@0:       nNew++;
sl@0:       if( rc ) goto balance_cleanup;
sl@0:     }else{
sl@0:       assert( i>0 );
sl@0:       rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
sl@0:       if( rc ) goto balance_cleanup;
sl@0:       apNew[i] = pNew;
sl@0:       nNew++;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* Free any old pages that were not reused as new pages.
sl@0:   */
sl@0:   while( i<nOld ){
sl@0:     rc = freePage(apOld[i]);
sl@0:     if( rc ) goto balance_cleanup;
sl@0:     releasePage(apOld[i]);
sl@0:     apOld[i] = 0;
sl@0:     i++;
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Put the new pages in accending order.  This helps to
sl@0:   ** keep entries in the disk file in order so that a scan
sl@0:   ** of the table is a linear scan through the file.  That
sl@0:   ** in turn helps the operating system to deliver pages
sl@0:   ** from the disk more rapidly.
sl@0:   **
sl@0:   ** An O(n^2) insertion sort algorithm is used, but since
sl@0:   ** n is never more than NB (a small constant), that should
sl@0:   ** not be a problem.
sl@0:   **
sl@0:   ** When NB==3, this one optimization makes the database
sl@0:   ** about 25% faster for large insertions and deletions.
sl@0:   */
sl@0:   for(i=0; i<k-1; i++){
sl@0:     int minV = pgnoNew[i];
sl@0:     int minI = i;
sl@0:     for(j=i+1; j<k; j++){
sl@0:       if( pgnoNew[j]<(unsigned)minV ){
sl@0:         minI = j;
sl@0:         minV = pgnoNew[j];
sl@0:       }
sl@0:     }
sl@0:     if( minI>i ){
sl@0:       int t;
sl@0:       MemPage *pT;
sl@0:       t = pgnoNew[i];
sl@0:       pT = apNew[i];
sl@0:       pgnoNew[i] = pgnoNew[minI];
sl@0:       apNew[i] = apNew[minI];
sl@0:       pgnoNew[minI] = t;
sl@0:       apNew[minI] = pT;
sl@0:     }
sl@0:   }
sl@0:   TRACE(("BALANCE: old: %d %d %d  new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
sl@0:     pgnoOld[0], 
sl@0:     nOld>=2 ? pgnoOld[1] : 0,
sl@0:     nOld>=3 ? pgnoOld[2] : 0,
sl@0:     pgnoNew[0], szNew[0],
sl@0:     nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
sl@0:     nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
sl@0:     nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
sl@0:     nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
sl@0: 
sl@0:   /*
sl@0:   ** Evenly distribute the data in apCell[] across the new pages.
sl@0:   ** Insert divider cells into pParent as necessary.
sl@0:   */
sl@0:   j = 0;
sl@0:   for(i=0; i<nNew; i++){
sl@0:     /* Assemble the new sibling page. */
sl@0:     MemPage *pNew = apNew[i];
sl@0:     assert( j<nMaxCells );
sl@0:     assert( pNew->pgno==pgnoNew[i] );
sl@0:     zeroPage(pNew, pageFlags);
sl@0:     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
sl@0:     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
sl@0:     assert( pNew->nOverflow==0 );
sl@0: 
sl@0:     /* If this is an auto-vacuum database, update the pointer map entries
sl@0:     ** that point to the siblings that were rearranged. These can be: left
sl@0:     ** children of cells, the right-child of the page, or overflow pages
sl@0:     ** pointed to by cells.
sl@0:     */
sl@0:     if( ISAUTOVACUUM ){
sl@0:       for(k=j; k<cntNew[i]; k++){
sl@0:         assert( k<nMaxCells );
sl@0:         if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
sl@0:           rc = ptrmapPutOvfl(pNew, k-j);
sl@0:           if( rc==SQLITE_OK && leafCorrection==0 ){
sl@0:             rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno);
sl@0:           }
sl@0:           if( rc!=SQLITE_OK ){
sl@0:             goto balance_cleanup;
sl@0:           }
sl@0:         }
sl@0:       }
sl@0:     }
sl@0: 
sl@0:     j = cntNew[i];
sl@0: 
sl@0:     /* If the sibling page assembled above was not the right-most sibling,
sl@0:     ** insert a divider cell into the parent page.
sl@0:     */
sl@0:     if( i<nNew-1 && j<nCell ){
sl@0:       u8 *pCell;
sl@0:       u8 *pTemp;
sl@0:       int sz;
sl@0: 
sl@0:       assert( j<nMaxCells );
sl@0:       pCell = apCell[j];
sl@0:       sz = szCell[j] + leafCorrection;
sl@0:       pTemp = &aSpace2[iSpace2];
sl@0:       if( !pNew->leaf ){
sl@0:         memcpy(&pNew->aData[8], pCell, 4);
sl@0:         if( ISAUTOVACUUM 
sl@0:          && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno)
sl@0:         ){
sl@0:           rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno);
sl@0:           if( rc!=SQLITE_OK ){
sl@0:             goto balance_cleanup;
sl@0:           }
sl@0:         }
sl@0:       }else if( leafData ){
sl@0:         /* If the tree is a leaf-data tree, and the siblings are leaves, 
sl@0:         ** then there is no divider cell in apCell[]. Instead, the divider 
sl@0:         ** cell consists of the integer key for the right-most cell of 
sl@0:         ** the sibling-page assembled above only.
sl@0:         */
sl@0:         CellInfo info;
sl@0:         j--;
sl@0:         sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
sl@0:         pCell = pTemp;
sl@0:         fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
sl@0:         pTemp = 0;
sl@0:       }else{
sl@0:         pCell -= 4;
sl@0:         /* Obscure case for non-leaf-data trees: If the cell at pCell was
sl@0:         ** previously stored on a leaf node, and its reported size was 4
sl@0:         ** bytes, then it may actually be smaller than this 
sl@0:         ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
sl@0:         ** any cell). But it is important to pass the correct size to 
sl@0:         ** insertCell(), so reparse the cell now.
sl@0:         **
sl@0:         ** Note that this can never happen in an SQLite data file, as all
sl@0:         ** cells are at least 4 bytes. It only happens in b-trees used
sl@0:         ** to evaluate "IN (SELECT ...)" and similar clauses.
sl@0:         */
sl@0:         if( szCell[j]==4 ){
sl@0:           assert(leafCorrection==4);
sl@0:           sz = cellSizePtr(pParent, pCell);
sl@0:         }
sl@0:       }
sl@0:       iSpace2 += sz;
sl@0:       assert( sz<=pBt->pageSize/4 );
sl@0:       assert( iSpace2<=pBt->pageSize );
sl@0:       rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
sl@0:       if( rc!=SQLITE_OK ) goto balance_cleanup;
sl@0:       put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
sl@0: 
sl@0:       /* If this is an auto-vacuum database, and not a leaf-data tree,
sl@0:       ** then update the pointer map with an entry for the overflow page
sl@0:       ** that the cell just inserted points to (if any).
sl@0:       */
sl@0:       if( ISAUTOVACUUM && !leafData ){
sl@0:         rc = ptrmapPutOvfl(pParent, nxDiv);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           goto balance_cleanup;
sl@0:         }
sl@0:       }
sl@0:       j++;
sl@0:       nxDiv++;
sl@0:     }
sl@0: 
sl@0:     /* Set the pointer-map entry for the new sibling page. */
sl@0:     if( ISAUTOVACUUM ){
sl@0:       rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         goto balance_cleanup;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   assert( j==nCell );
sl@0:   assert( nOld>0 );
sl@0:   assert( nNew>0 );
sl@0:   if( (pageFlags & PTF_LEAF)==0 ){
sl@0:     u8 *zChild = &apCopy[nOld-1]->aData[8];
sl@0:     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
sl@0:     if( ISAUTOVACUUM ){
sl@0:       rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         goto balance_cleanup;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   if( nxDiv==pParent->nCell+pParent->nOverflow ){
sl@0:     /* Right-most sibling is the right-most child of pParent */
sl@0:     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
sl@0:   }else{
sl@0:     /* Right-most sibling is the left child of the first entry in pParent
sl@0:     ** past the right-most divider entry */
sl@0:     put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
sl@0:   }
sl@0: 
sl@0:   /*
sl@0:   ** Reparent children of all cells.
sl@0:   */
sl@0:   for(i=0; i<nNew; i++){
sl@0:     rc = reparentChildPages(apNew[i], 0);
sl@0:     if( rc!=SQLITE_OK ) goto balance_cleanup;
sl@0:   }
sl@0:   rc = reparentChildPages(pParent, 0);
sl@0:   if( rc!=SQLITE_OK ) goto balance_cleanup;
sl@0: 
sl@0:   /*
sl@0:   ** Balance the parent page.  Note that the current page (pPage) might
sl@0:   ** have been added to the freelist so it might no longer be initialized.
sl@0:   ** But the parent page will always be initialized.
sl@0:   */
sl@0:   assert( pParent->isInit );
sl@0:   sqlite3ScratchFree(apCell);
sl@0:   apCell = 0;
sl@0:   rc = balance(pParent, 0);
sl@0:   
sl@0:   /*
sl@0:   ** Cleanup before returning.
sl@0:   */
sl@0: balance_cleanup:
sl@0:   sqlite3PageFree(aSpace2);
sl@0:   sqlite3ScratchFree(apCell);
sl@0:   for(i=0; i<nOld; i++){
sl@0:     releasePage(apOld[i]);
sl@0:   }
sl@0:   for(i=0; i<nNew; i++){
sl@0:     releasePage(apNew[i]);
sl@0:   }
sl@0:   releasePage(pParent);
sl@0:   TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
sl@0:           pPage->pgno, nOld, nNew, nCell));
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine is called for the root page of a btree when the root
sl@0: ** page contains no cells.  This is an opportunity to make the tree
sl@0: ** shallower by one level.
sl@0: */
sl@0: static int balance_shallower(MemPage *pPage){
sl@0:   MemPage *pChild;             /* The only child page of pPage */
sl@0:   Pgno pgnoChild;              /* Page number for pChild */
sl@0:   int rc = SQLITE_OK;          /* Return code from subprocedures */
sl@0:   BtShared *pBt;                  /* The main BTree structure */
sl@0:   int mxCellPerPage;           /* Maximum number of cells per page */
sl@0:   u8 **apCell;                 /* All cells from pages being balanced */
sl@0:   u16 *szCell;                 /* Local size of all cells */
sl@0: 
sl@0:   assert( pPage->pParent==0 );
sl@0:   assert( pPage->nCell==0 );
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   pBt = pPage->pBt;
sl@0:   mxCellPerPage = MX_CELL(pBt);
sl@0:   apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
sl@0:   if( apCell==0 ) return SQLITE_NOMEM;
sl@0:   szCell = (u16*)&apCell[mxCellPerPage];
sl@0:   if( pPage->leaf ){
sl@0:     /* The table is completely empty */
sl@0:     TRACE(("BALANCE: empty table %d\n", pPage->pgno));
sl@0:   }else{
sl@0:     /* The root page is empty but has one child.  Transfer the
sl@0:     ** information from that one child into the root page if it 
sl@0:     ** will fit.  This reduces the depth of the tree by one.
sl@0:     **
sl@0:     ** If the root page is page 1, it has less space available than
sl@0:     ** its child (due to the 100 byte header that occurs at the beginning
sl@0:     ** of the database fle), so it might not be able to hold all of the 
sl@0:     ** information currently contained in the child.  If this is the 
sl@0:     ** case, then do not do the transfer.  Leave page 1 empty except
sl@0:     ** for the right-pointer to the child page.  The child page becomes
sl@0:     ** the virtual root of the tree.
sl@0:     */
sl@0:     pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0:     assert( pgnoChild>0 );
sl@0:     assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) );
sl@0:     rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
sl@0:     if( rc ) goto end_shallow_balance;
sl@0:     if( pPage->pgno==1 ){
sl@0:       rc = sqlite3BtreeInitPage(pChild, pPage);
sl@0:       if( rc ) goto end_shallow_balance;
sl@0:       assert( pChild->nOverflow==0 );
sl@0:       if( pChild->nFree>=100 ){
sl@0:         /* The child information will fit on the root page, so do the
sl@0:         ** copy */
sl@0:         int i;
sl@0:         zeroPage(pPage, pChild->aData[0]);
sl@0:         for(i=0; i<pChild->nCell; i++){
sl@0:           apCell[i] = findCell(pChild,i);
sl@0:           szCell[i] = cellSizePtr(pChild, apCell[i]);
sl@0:         }
sl@0:         assemblePage(pPage, pChild->nCell, apCell, szCell);
sl@0:         /* Copy the right-pointer of the child to the parent. */
sl@0:         put4byte(&pPage->aData[pPage->hdrOffset+8], 
sl@0:             get4byte(&pChild->aData[pChild->hdrOffset+8]));
sl@0:         freePage(pChild);
sl@0:         TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
sl@0:       }else{
sl@0:         /* The child has more information that will fit on the root.
sl@0:         ** The tree is already balanced.  Do nothing. */
sl@0:         TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
sl@0:       }
sl@0:     }else{
sl@0:       memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
sl@0:       pPage->isInit = 0;
sl@0:       pPage->pParent = 0;
sl@0:       rc = sqlite3BtreeInitPage(pPage, 0);
sl@0:       assert( rc==SQLITE_OK );
sl@0:       freePage(pChild);
sl@0:       TRACE(("BALANCE: transfer child %d into root %d\n",
sl@0:               pChild->pgno, pPage->pgno));
sl@0:     }
sl@0:     rc = reparentChildPages(pPage, 1);
sl@0:     assert( pPage->nOverflow==0 );
sl@0:     if( ISAUTOVACUUM ){
sl@0:       int i;
sl@0:       for(i=0; i<pPage->nCell; i++){ 
sl@0:         rc = ptrmapPutOvfl(pPage, i);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           goto end_shallow_balance;
sl@0:         }
sl@0:       }
sl@0:     }
sl@0:     releasePage(pChild);
sl@0:   }
sl@0: end_shallow_balance:
sl@0:   sqlite3_free(apCell);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** The root page is overfull
sl@0: **
sl@0: ** When this happens, Create a new child page and copy the
sl@0: ** contents of the root into the child.  Then make the root
sl@0: ** page an empty page with rightChild pointing to the new
sl@0: ** child.   Finally, call balance_internal() on the new child
sl@0: ** to cause it to split.
sl@0: */
sl@0: static int balance_deeper(MemPage *pPage){
sl@0:   int rc;             /* Return value from subprocedures */
sl@0:   MemPage *pChild;    /* Pointer to a new child page */
sl@0:   Pgno pgnoChild;     /* Page number of the new child page */
sl@0:   BtShared *pBt;         /* The BTree */
sl@0:   int usableSize;     /* Total usable size of a page */
sl@0:   u8 *data;           /* Content of the parent page */
sl@0:   u8 *cdata;          /* Content of the child page */
sl@0:   int hdr;            /* Offset to page header in parent */
sl@0:   int brk;            /* Offset to content of first cell in parent */
sl@0: 
sl@0:   assert( pPage->pParent==0 );
sl@0:   assert( pPage->nOverflow>0 );
sl@0:   pBt = pPage->pBt;
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
sl@0:   if( rc ) return rc;
sl@0:   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
sl@0:   usableSize = pBt->usableSize;
sl@0:   data = pPage->aData;
sl@0:   hdr = pPage->hdrOffset;
sl@0:   brk = get2byte(&data[hdr+5]);
sl@0:   cdata = pChild->aData;
sl@0:   memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
sl@0:   memcpy(&cdata[brk], &data[brk], usableSize-brk);
sl@0:   if( pChild->isInit ) return SQLITE_CORRUPT;
sl@0:   rc = sqlite3BtreeInitPage(pChild, pPage);
sl@0:   if( rc ) goto balancedeeper_out;
sl@0:   memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
sl@0:   pChild->nOverflow = pPage->nOverflow;
sl@0:   if( pChild->nOverflow ){
sl@0:     pChild->nFree = 0;
sl@0:   }
sl@0:   assert( pChild->nCell==pPage->nCell );
sl@0:   zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
sl@0:   put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
sl@0:   TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
sl@0:   if( ISAUTOVACUUM ){
sl@0:     int i;
sl@0:     rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
sl@0:     if( rc ) goto balancedeeper_out;
sl@0:     for(i=0; i<pChild->nCell; i++){
sl@0:       rc = ptrmapPutOvfl(pChild, i);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         goto balancedeeper_out;
sl@0:       }
sl@0:     }
sl@0:     rc = reparentChildPages(pChild, 1);
sl@0:   }
sl@0:   if( rc==SQLITE_OK ){
sl@0:     rc = balance_nonroot(pChild);
sl@0:   }
sl@0: 
sl@0: balancedeeper_out:
sl@0:   releasePage(pChild);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Decide if the page pPage needs to be balanced.  If balancing is
sl@0: ** required, call the appropriate balancing routine.
sl@0: */
sl@0: static int balance(MemPage *pPage, int insert){
sl@0:   int rc = SQLITE_OK;
sl@0:   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
sl@0:   if( pPage->pParent==0 ){
sl@0:     rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:     if( rc==SQLITE_OK && pPage->nOverflow>0 ){
sl@0:       rc = balance_deeper(pPage);
sl@0:     }
sl@0:     if( rc==SQLITE_OK && pPage->nCell==0 ){
sl@0:       rc = balance_shallower(pPage);
sl@0:     }
sl@0:   }else{
sl@0:     if( pPage->nOverflow>0 || 
sl@0:         (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
sl@0:       rc = balance_nonroot(pPage);
sl@0:     }
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This routine checks all cursors that point to table pgnoRoot.
sl@0: ** If any of those cursors were opened with wrFlag==0 in a different
sl@0: ** database connection (a database connection that shares the pager
sl@0: ** cache with the current connection) and that other connection 
sl@0: ** is not in the ReadUncommmitted state, then this routine returns 
sl@0: ** SQLITE_LOCKED.
sl@0: **
sl@0: ** As well as cursors with wrFlag==0, cursors with wrFlag==1 and 
sl@0: ** isIncrblobHandle==1 are also considered 'read' cursors. Incremental 
sl@0: ** blob cursors are used for both reading and writing.
sl@0: **
sl@0: ** When pgnoRoot is the root page of an intkey table, this function is also
sl@0: ** responsible for invalidating incremental blob cursors when the table row
sl@0: ** on which they are opened is deleted or modified. Cursors are invalidated
sl@0: ** according to the following rules:
sl@0: **
sl@0: **   1) When BtreeClearTable() is called to completely delete the contents
sl@0: **      of a B-Tree table, pExclude is set to zero and parameter iRow is 
sl@0: **      set to non-zero. In this case all incremental blob cursors open
sl@0: **      on the table rooted at pgnoRoot are invalidated.
sl@0: **
sl@0: **   2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to 
sl@0: **      modify a table row via an SQL statement, pExclude is set to the 
sl@0: **      write cursor used to do the modification and parameter iRow is set
sl@0: **      to the integer row id of the B-Tree entry being modified. Unless
sl@0: **      pExclude is itself an incremental blob cursor, then all incremental
sl@0: **      blob cursors open on row iRow of the B-Tree are invalidated.
sl@0: **
sl@0: **   3) If both pExclude and iRow are set to zero, no incremental blob 
sl@0: **      cursors are invalidated.
sl@0: */
sl@0: static int checkReadLocks(
sl@0:   Btree *pBtree, 
sl@0:   Pgno pgnoRoot, 
sl@0:   BtCursor *pExclude,
sl@0:   i64 iRow
sl@0: ){
sl@0:   BtCursor *p;
sl@0:   BtShared *pBt = pBtree->pBt;
sl@0:   sqlite3 *db = pBtree->db;
sl@0:   assert( sqlite3BtreeHoldsMutex(pBtree) );
sl@0:   for(p=pBt->pCursor; p; p=p->pNext){
sl@0:     if( p==pExclude ) continue;
sl@0:     if( p->pgnoRoot!=pgnoRoot ) continue;
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:     if( p->isIncrblobHandle && ( 
sl@0:          (!pExclude && iRow)
sl@0:       || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow)
sl@0:     )){
sl@0:       p->eState = CURSOR_INVALID;
sl@0:     }
sl@0: #endif
sl@0:     if( p->eState!=CURSOR_VALID ) continue;
sl@0:     if( p->wrFlag==0 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0:      || p->isIncrblobHandle
sl@0: #endif
sl@0:     ){
sl@0:       sqlite3 *dbOther = p->pBtree->db;
sl@0:       if( dbOther==0 ||
sl@0:          (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
sl@0:         return SQLITE_LOCKED;
sl@0:       }
sl@0:     }
sl@0:   }
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
sl@0: ** and the data is given by (pData,nData).  The cursor is used only to
sl@0: ** define what table the record should be inserted into.  The cursor
sl@0: ** is left pointing at a random location.
sl@0: **
sl@0: ** For an INTKEY table, only the nKey value of the key is used.  pKey is
sl@0: ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
sl@0: */
sl@0: int sqlite3BtreeInsert(
sl@0:   BtCursor *pCur,                /* Insert data into the table of this cursor */
sl@0:   const void *pKey, i64 nKey,    /* The key of the new record */
sl@0:   const void *pData, int nData,  /* The data of the new record */
sl@0:   int nZero,                     /* Number of extra 0 bytes to append to data */
sl@0:   int appendBias                 /* True if this is likely an append */
sl@0: ){
sl@0:   int rc;
sl@0:   int loc;
sl@0:   int szNew;
sl@0:   MemPage *pPage;
sl@0:   Btree *p = pCur->pBtree;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   unsigned char *oldCell;
sl@0:   unsigned char *newCell = 0;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   if( pBt->inTransaction!=TRANS_WRITE ){
sl@0:     /* Must start a transaction before doing an insert */
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:     return rc;
sl@0:   }
sl@0:   assert( !pBt->readOnly );
sl@0:   if( !pCur->wrFlag ){
sl@0:     return SQLITE_PERM;   /* Cursor not open for writing */
sl@0:   }
sl@0:   if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){
sl@0:     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
sl@0:   }
sl@0:   if( pCur->eState==CURSOR_FAULT ){
sl@0:     return pCur->skip;
sl@0:   }
sl@0: 
sl@0:   /* Save the positions of any other cursors open on this table */
sl@0:   clearCursorPosition(pCur);
sl@0:   if( 
sl@0:     SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
sl@0:     SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, 0, nKey, appendBias, &loc))
sl@0:   ){
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   pPage = pCur->pPage;
sl@0:   assert( pPage->intKey || nKey>=0 );
sl@0:   assert( pPage->leaf || !pPage->intKey );
sl@0:   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
sl@0:           pCur->pgnoRoot, nKey, nData, pPage->pgno,
sl@0:           loc==0 ? "overwrite" : "new entry"));
sl@0:   assert( pPage->isInit );
sl@0:   allocateTempSpace(pBt);
sl@0:   newCell = pBt->pTmpSpace;
sl@0:   if( newCell==0 ) return SQLITE_NOMEM;
sl@0:   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
sl@0:   if( rc ) goto end_insert;
sl@0:   assert( szNew==cellSizePtr(pPage, newCell) );
sl@0:   assert( szNew<=MX_CELL_SIZE(pBt) );
sl@0:   if( loc==0 && CURSOR_VALID==pCur->eState ){
sl@0:     u16 szOld;
sl@0:     assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
sl@0:     rc = sqlite3PagerWrite(pPage->pDbPage);
sl@0:     if( rc ){
sl@0:       goto end_insert;
sl@0:     }
sl@0:     oldCell = findCell(pPage, pCur->idx);
sl@0:     if( !pPage->leaf ){
sl@0:       memcpy(newCell, oldCell, 4);
sl@0:     }
sl@0:     szOld = cellSizePtr(pPage, oldCell);
sl@0:     rc = clearCell(pPage, oldCell);
sl@0:     if( rc ) goto end_insert;
sl@0:     dropCell(pPage, pCur->idx, szOld);
sl@0:   }else if( loc<0 && pPage->nCell>0 ){
sl@0:     assert( pPage->leaf );
sl@0:     pCur->idx++;
sl@0:     pCur->info.nSize = 0;
sl@0:     pCur->validNKey = 0;
sl@0:   }else{
sl@0:     assert( pPage->leaf );
sl@0:   }
sl@0:   rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
sl@0:   if( rc!=SQLITE_OK ) goto end_insert;
sl@0:   rc = balance(pPage, 1);
sl@0:   if( rc==SQLITE_OK ){
sl@0:     moveToRoot(pCur);
sl@0:   }
sl@0: end_insert:
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Delete the entry that the cursor is pointing to.  The cursor
sl@0: ** is left pointing at a random location.
sl@0: */
sl@0: int sqlite3BtreeDelete(BtCursor *pCur){
sl@0:   MemPage *pPage = pCur->pPage;
sl@0:   unsigned char *pCell;
sl@0:   int rc;
sl@0:   Pgno pgnoChild = 0;
sl@0:   Btree *p = pCur->pBtree;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pPage->isInit );
sl@0:   if( pBt->inTransaction!=TRANS_WRITE ){
sl@0:     /* Must start a transaction before doing a delete */
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:     return rc;
sl@0:   }
sl@0:   assert( !pBt->readOnly );
sl@0:   if( pCur->eState==CURSOR_FAULT ){
sl@0:     return pCur->skip;
sl@0:   }
sl@0:   if( pCur->idx >= pPage->nCell ){
sl@0:     return SQLITE_ERROR;  /* The cursor is not pointing to anything */
sl@0:   }
sl@0:   if( !pCur->wrFlag ){
sl@0:     return SQLITE_PERM;   /* Did not open this cursor for writing */
sl@0:   }
sl@0:   if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){
sl@0:     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
sl@0:   }
sl@0: 
sl@0:   /* Restore the current cursor position (a no-op if the cursor is not in 
sl@0:   ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors 
sl@0:   ** open on the same table. Then call sqlite3PagerWrite() on the page
sl@0:   ** that the entry will be deleted from.
sl@0:   */
sl@0:   if( 
sl@0:     (rc = restoreCursorPosition(pCur))!=0 ||
sl@0:     (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
sl@0:     (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
sl@0:   ){
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   /* Locate the cell within its page and leave pCell pointing to the
sl@0:   ** data. The clearCell() call frees any overflow pages associated with the
sl@0:   ** cell. The cell itself is still intact.
sl@0:   */
sl@0:   pCell = findCell(pPage, pCur->idx);
sl@0:   if( !pPage->leaf ){
sl@0:     pgnoChild = get4byte(pCell);
sl@0:   }
sl@0:   rc = clearCell(pPage, pCell);
sl@0:   if( rc ){
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   if( !pPage->leaf ){
sl@0:     /*
sl@0:     ** The entry we are about to delete is not a leaf so if we do not
sl@0:     ** do something we will leave a hole on an internal page.
sl@0:     ** We have to fill the hole by moving in a cell from a leaf.  The
sl@0:     ** next Cell after the one to be deleted is guaranteed to exist and
sl@0:     ** to be a leaf so we can use it.
sl@0:     */
sl@0:     BtCursor leafCur;
sl@0:     unsigned char *pNext;
sl@0:     int notUsed;
sl@0:     unsigned char *tempCell = 0;
sl@0:     assert( !pPage->intKey );
sl@0:     sqlite3BtreeGetTempCursor(pCur, &leafCur);
sl@0:     rc = sqlite3BtreeNext(&leafCur, &notUsed);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
sl@0:     }
sl@0:     if( rc==SQLITE_OK ){
sl@0:       u16 szNext;
sl@0:       TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
sl@0:          pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
sl@0:       dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
sl@0:       pNext = findCell(leafCur.pPage, leafCur.idx);
sl@0:       szNext = cellSizePtr(leafCur.pPage, pNext);
sl@0:       assert( MX_CELL_SIZE(pBt)>=szNext+4 );
sl@0:       allocateTempSpace(pBt);
sl@0:       tempCell = pBt->pTmpSpace;
sl@0:       if( tempCell==0 ){
sl@0:         rc = SQLITE_NOMEM;
sl@0:       }
sl@0:       if( rc==SQLITE_OK ){
sl@0:         rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
sl@0:       }
sl@0:       if( rc==SQLITE_OK ){
sl@0:         put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
sl@0:         rc = balance(pPage, 0);
sl@0:       }
sl@0:       if( rc==SQLITE_OK ){
sl@0:         dropCell(leafCur.pPage, leafCur.idx, szNext);
sl@0:         rc = balance(leafCur.pPage, 0);
sl@0:       }
sl@0:     }
sl@0:     sqlite3BtreeReleaseTempCursor(&leafCur);
sl@0:   }else{
sl@0:     TRACE(("DELETE: table=%d delete from leaf %d\n",
sl@0:        pCur->pgnoRoot, pPage->pgno));
sl@0:     dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
sl@0:     rc = balance(pPage, 0);
sl@0:   }
sl@0:   if( rc==SQLITE_OK ){
sl@0:     moveToRoot(pCur);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Create a new BTree table.  Write into *piTable the page
sl@0: ** number for the root page of the new table.
sl@0: **
sl@0: ** The type of type is determined by the flags parameter.  Only the
sl@0: ** following values of flags are currently in use.  Other values for
sl@0: ** flags might not work:
sl@0: **
sl@0: **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
sl@0: **     BTREE_ZERODATA                  Used for SQL indices
sl@0: */
sl@0: static int btreeCreateTable(Btree *p, int *piTable, int flags){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   MemPage *pRoot;
sl@0:   Pgno pgnoRoot;
sl@0:   int rc;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   if( pBt->inTransaction!=TRANS_WRITE ){
sl@0:     /* Must start a transaction first */
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:     return rc;
sl@0:   }
sl@0:   assert( !pBt->readOnly );
sl@0: 
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
sl@0:   if( rc ){
sl@0:     return rc;
sl@0:   }
sl@0: #else
sl@0:   if( pBt->autoVacuum ){
sl@0:     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
sl@0:     MemPage *pPageMove; /* The page to move to. */
sl@0: 
sl@0:     /* Creating a new table may probably require moving an existing database
sl@0:     ** to make room for the new tables root page. In case this page turns
sl@0:     ** out to be an overflow page, delete all overflow page-map caches
sl@0:     ** held by open cursors.
sl@0:     */
sl@0:     invalidateAllOverflowCache(pBt);
sl@0: 
sl@0:     /* Read the value of meta[3] from the database to determine where the
sl@0:     ** root page of the new table should go. meta[3] is the largest root-page
sl@0:     ** created so far, so the new root-page is (meta[3]+1).
sl@0:     */
sl@0:     rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0:     pgnoRoot++;
sl@0: 
sl@0:     /* The new root-page may not be allocated on a pointer-map page, or the
sl@0:     ** PENDING_BYTE page.
sl@0:     */
sl@0:     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
sl@0:         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
sl@0:       pgnoRoot++;
sl@0:     }
sl@0:     assert( pgnoRoot>=3 );
sl@0: 
sl@0:     /* Allocate a page. The page that currently resides at pgnoRoot will
sl@0:     ** be moved to the allocated page (unless the allocated page happens
sl@0:     ** to reside at pgnoRoot).
sl@0:     */
sl@0:     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
sl@0:     if( rc!=SQLITE_OK ){
sl@0:       return rc;
sl@0:     }
sl@0: 
sl@0:     if( pgnoMove!=pgnoRoot ){
sl@0:       /* pgnoRoot is the page that will be used for the root-page of
sl@0:       ** the new table (assuming an error did not occur). But we were
sl@0:       ** allocated pgnoMove. If required (i.e. if it was not allocated
sl@0:       ** by extending the file), the current page at position pgnoMove
sl@0:       ** is already journaled.
sl@0:       */
sl@0:       u8 eType;
sl@0:       Pgno iPtrPage;
sl@0: 
sl@0:       releasePage(pPageMove);
sl@0: 
sl@0:       /* Move the page currently at pgnoRoot to pgnoMove. */
sl@0:       rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
sl@0:       if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
sl@0:         releasePage(pRoot);
sl@0:         return rc;
sl@0:       }
sl@0:       assert( eType!=PTRMAP_ROOTPAGE );
sl@0:       assert( eType!=PTRMAP_FREEPAGE );
sl@0:       rc = sqlite3PagerWrite(pRoot->pDbPage);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         releasePage(pRoot);
sl@0:         return rc;
sl@0:       }
sl@0:       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
sl@0:       releasePage(pRoot);
sl@0: 
sl@0:       /* Obtain the page at pgnoRoot */
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:       rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         return rc;
sl@0:       }
sl@0:       rc = sqlite3PagerWrite(pRoot->pDbPage);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         releasePage(pRoot);
sl@0:         return rc;
sl@0:       }
sl@0:     }else{
sl@0:       pRoot = pPageMove;
sl@0:     } 
sl@0: 
sl@0:     /* Update the pointer-map and meta-data with the new root-page number. */
sl@0:     rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
sl@0:     if( rc ){
sl@0:       releasePage(pRoot);
sl@0:       return rc;
sl@0:     }
sl@0:     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
sl@0:     if( rc ){
sl@0:       releasePage(pRoot);
sl@0:       return rc;
sl@0:     }
sl@0: 
sl@0:   }else{
sl@0:     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
sl@0:     if( rc ) return rc;
sl@0:   }
sl@0: #endif
sl@0:   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
sl@0:   zeroPage(pRoot, flags | PTF_LEAF);
sl@0:   sqlite3PagerUnref(pRoot->pDbPage);
sl@0:   *piTable = (int)pgnoRoot;
sl@0:   return SQLITE_OK;
sl@0: }
sl@0: int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   p->pBt->db = p->db;
sl@0:   rc = btreeCreateTable(p, piTable, flags);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Erase the given database page and all its children.  Return
sl@0: ** the page to the freelist.
sl@0: */
sl@0: static int clearDatabasePage(
sl@0:   BtShared *pBt,           /* The BTree that contains the table */
sl@0:   Pgno pgno,            /* Page number to clear */
sl@0:   MemPage *pParent,     /* Parent page.  NULL for the root */
sl@0:   int freePageFlag      /* Deallocate page if true */
sl@0: ){
sl@0:   MemPage *pPage = 0;
sl@0:   int rc;
sl@0:   unsigned char *pCell;
sl@0:   int i;
sl@0: 
sl@0:   assert( sqlite3_mutex_held(pBt->mutex) );
sl@0:   if( pgno>pagerPagecount(pBt->pPager) ){
sl@0:     return SQLITE_CORRUPT_BKPT;
sl@0:   }
sl@0: 
sl@0:   rc = getAndInitPage(pBt, pgno, &pPage, pParent);
sl@0:   if( rc ) goto cleardatabasepage_out;
sl@0:   for(i=0; i<pPage->nCell; i++){
sl@0:     pCell = findCell(pPage, i);
sl@0:     if( !pPage->leaf ){
sl@0:       rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
sl@0:       if( rc ) goto cleardatabasepage_out;
sl@0:     }
sl@0:     rc = clearCell(pPage, pCell);
sl@0:     if( rc ) goto cleardatabasepage_out;
sl@0:   }
sl@0:   if( !pPage->leaf ){
sl@0:     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
sl@0:     if( rc ) goto cleardatabasepage_out;
sl@0:   }
sl@0:   if( freePageFlag ){
sl@0:     rc = freePage(pPage);
sl@0:   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
sl@0:     zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
sl@0:   }
sl@0: 
sl@0: cleardatabasepage_out:
sl@0:   releasePage(pPage);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Delete all information from a single table in the database.  iTable is
sl@0: ** the page number of the root of the table.  After this routine returns,
sl@0: ** the root page is empty, but still exists.
sl@0: **
sl@0: ** This routine will fail with SQLITE_LOCKED if there are any open
sl@0: ** read cursors on the table.  Open write cursors are moved to the
sl@0: ** root of the table.
sl@0: */
sl@0: int sqlite3BtreeClearTable(Btree *p, int iTable){
sl@0:   int rc;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   if( p->inTrans!=TRANS_WRITE ){
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:   }else if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
sl@0:     /* nothing to do */
sl@0:   }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
sl@0:     /* nothing to do */
sl@0:   }else{
sl@0:     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Erase all information in a table and add the root of the table to
sl@0: ** the freelist.  Except, the root of the principle table (the one on
sl@0: ** page 1) is never added to the freelist.
sl@0: **
sl@0: ** This routine will fail with SQLITE_LOCKED if there are any open
sl@0: ** cursors on the table.
sl@0: **
sl@0: ** If AUTOVACUUM is enabled and the page at iTable is not the last
sl@0: ** root page in the database file, then the last root page 
sl@0: ** in the database file is moved into the slot formerly occupied by
sl@0: ** iTable and that last slot formerly occupied by the last root page
sl@0: ** is added to the freelist instead of iTable.  In this say, all
sl@0: ** root pages are kept at the beginning of the database file, which
sl@0: ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the 
sl@0: ** page number that used to be the last root page in the file before
sl@0: ** the move.  If no page gets moved, *piMoved is set to 0.
sl@0: ** The last root page is recorded in meta[3] and the value of
sl@0: ** meta[3] is updated by this procedure.
sl@0: */
sl@0: static int btreeDropTable(Btree *p, int iTable, int *piMoved){
sl@0:   int rc;
sl@0:   MemPage *pPage = 0;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   if( p->inTrans!=TRANS_WRITE ){
sl@0:     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:   }
sl@0: 
sl@0:   /* It is illegal to drop a table if any cursors are open on the
sl@0:   ** database. This is because in auto-vacuum mode the backend may
sl@0:   ** need to move another root-page to fill a gap left by the deleted
sl@0:   ** root page. If an open cursor was using this page a problem would 
sl@0:   ** occur.
sl@0:   */
sl@0:   if( pBt->pCursor ){
sl@0:     return SQLITE_LOCKED;
sl@0:   }
sl@0: 
sl@0:   rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
sl@0:   if( rc ) return rc;
sl@0:   rc = sqlite3BtreeClearTable(p, iTable);
sl@0:   if( rc ){
sl@0:     releasePage(pPage);
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   *piMoved = 0;
sl@0: 
sl@0:   if( iTable>1 ){
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:     rc = freePage(pPage);
sl@0:     releasePage(pPage);
sl@0: #else
sl@0:     if( pBt->autoVacuum ){
sl@0:       Pgno maxRootPgno;
sl@0:       rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
sl@0:       if( rc!=SQLITE_OK ){
sl@0:         releasePage(pPage);
sl@0:         return rc;
sl@0:       }
sl@0: 
sl@0:       if( iTable==maxRootPgno ){
sl@0:         /* If the table being dropped is the table with the largest root-page
sl@0:         ** number in the database, put the root page on the free list. 
sl@0:         */
sl@0:         rc = freePage(pPage);
sl@0:         releasePage(pPage);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:       }else{
sl@0:         /* The table being dropped does not have the largest root-page
sl@0:         ** number in the database. So move the page that does into the 
sl@0:         ** gap left by the deleted root-page.
sl@0:         */
sl@0:         MemPage *pMove;
sl@0:         releasePage(pPage);
sl@0:         rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
sl@0:         releasePage(pMove);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:         rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:         rc = freePage(pMove);
sl@0:         releasePage(pMove);
sl@0:         if( rc!=SQLITE_OK ){
sl@0:           return rc;
sl@0:         }
sl@0:         *piMoved = maxRootPgno;
sl@0:       }
sl@0: 
sl@0:       /* Set the new 'max-root-page' value in the database header. This
sl@0:       ** is the old value less one, less one more if that happens to
sl@0:       ** be a root-page number, less one again if that is the
sl@0:       ** PENDING_BYTE_PAGE.
sl@0:       */
sl@0:       maxRootPgno--;
sl@0:       if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
sl@0:         maxRootPgno--;
sl@0:       }
sl@0:       if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
sl@0:         maxRootPgno--;
sl@0:       }
sl@0:       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
sl@0: 
sl@0:       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
sl@0:     }else{
sl@0:       rc = freePage(pPage);
sl@0:       releasePage(pPage);
sl@0:     }
sl@0: #endif
sl@0:   }else{
sl@0:     /* If sqlite3BtreeDropTable was called on page 1. */
sl@0:     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
sl@0:     releasePage(pPage);
sl@0:   }
sl@0:   return rc;  
sl@0: }
sl@0: int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   p->pBt->db = p->db;
sl@0:   rc = btreeDropTable(p, iTable, piMoved);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Read the meta-information out of a database file.  Meta[0]
sl@0: ** is the number of free pages currently in the database.  Meta[1]
sl@0: ** through meta[15] are available for use by higher layers.  Meta[0]
sl@0: ** is read-only, the others are read/write.
sl@0: ** 
sl@0: ** The schema layer numbers meta values differently.  At the schema
sl@0: ** layer (and the SetCookie and ReadCookie opcodes) the number of
sl@0: ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
sl@0: */
sl@0: int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
sl@0:   DbPage *pDbPage;
sl@0:   int rc;
sl@0:   unsigned char *pP1;
sl@0:   BtShared *pBt = p->pBt;
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0: 
sl@0:   /* Reading a meta-data value requires a read-lock on page 1 (and hence
sl@0:   ** the sqlite_master table. We grab this lock regardless of whether or
sl@0:   ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
sl@0:   ** 1 is treated as a special case by queryTableLock() and lockTable()).
sl@0:   */
sl@0:   rc = queryTableLock(p, 1, READ_LOCK);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return rc;
sl@0:   }
sl@0: 
sl@0:   assert( idx>=0 && idx<=15 );
sl@0:   rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
sl@0:   if( rc ){
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return rc;
sl@0:   }
sl@0:   pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
sl@0:   *pMeta = get4byte(&pP1[36 + idx*4]);
sl@0:   sqlite3PagerUnref(pDbPage);
sl@0: 
sl@0:   /* If autovacuumed is disabled in this build but we are trying to 
sl@0:   ** access an autovacuumed database, then make the database readonly. 
sl@0:   */
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:   if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
sl@0: #endif
sl@0: 
sl@0:   /* Grab the read-lock on page 1. */
sl@0:   rc = lockTable(p, 1, READ_LOCK);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Write meta-information back into the database.  Meta[0] is
sl@0: ** read-only and may not be written.
sl@0: */
sl@0: int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   unsigned char *pP1;
sl@0:   int rc;
sl@0:   assert( idx>=1 && idx<=15 );
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   if( p->inTrans!=TRANS_WRITE ){
sl@0:     rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
sl@0:   }else{
sl@0:     assert( pBt->pPage1!=0 );
sl@0:     pP1 = pBt->pPage1->aData;
sl@0:     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       put4byte(&pP1[36 + idx*4], iMeta);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       if( idx==7 ){
sl@0:         assert( pBt->autoVacuum || iMeta==0 );
sl@0:         assert( iMeta==0 || iMeta==1 );
sl@0:         pBt->incrVacuum = iMeta;
sl@0:       }
sl@0: #endif
sl@0:     }
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the flag byte at the beginning of the page that the cursor
sl@0: ** is currently pointing to.
sl@0: */
sl@0: int sqlite3BtreeFlags(BtCursor *pCur){
sl@0:   /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
sl@0:   ** restoreCursorPosition() here.
sl@0:   */
sl@0:   MemPage *pPage;
sl@0:   restoreCursorPosition(pCur);
sl@0:   pPage = pCur->pPage;
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( pPage->pBt==pCur->pBt );
sl@0:   return pPage ? pPage->aData[pPage->hdrOffset] : 0;
sl@0: }
sl@0: 
sl@0: 
sl@0: /*
sl@0: ** Return the pager associated with a BTree.  This routine is used for
sl@0: ** testing and debugging only.
sl@0: */
sl@0: Pager *sqlite3BtreePager(Btree *p){
sl@0:   return p->pBt->pPager;
sl@0: }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
sl@0: /*
sl@0: ** Append a message to the error message string.
sl@0: */
sl@0: static void checkAppendMsg(
sl@0:   IntegrityCk *pCheck,
sl@0:   char *zMsg1,
sl@0:   const char *zFormat,
sl@0:   ...
sl@0: ){
sl@0:   va_list ap;
sl@0:   if( !pCheck->mxErr ) return;
sl@0:   pCheck->mxErr--;
sl@0:   pCheck->nErr++;
sl@0:   va_start(ap, zFormat);
sl@0:   if( pCheck->errMsg.nChar ){
sl@0:     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
sl@0:   }
sl@0:   if( zMsg1 ){
sl@0:     sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
sl@0:   }
sl@0:   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
sl@0:   va_end(ap);
sl@0:   if( pCheck->errMsg.mallocFailed ){
sl@0:     pCheck->mallocFailed = 1;
sl@0:   }
sl@0: }
sl@0: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
sl@0: /*
sl@0: ** Add 1 to the reference count for page iPage.  If this is the second
sl@0: ** reference to the page, add an error message to pCheck->zErrMsg.
sl@0: ** Return 1 if there are 2 ore more references to the page and 0 if
sl@0: ** if this is the first reference to the page.
sl@0: **
sl@0: ** Also check that the page number is in bounds.
sl@0: */
sl@0: static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
sl@0:   if( iPage==0 ) return 1;
sl@0:   if( iPage>pCheck->nPage || iPage<0 ){
sl@0:     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
sl@0:     return 1;
sl@0:   }
sl@0:   if( pCheck->anRef[iPage]==1 ){
sl@0:     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
sl@0:     return 1;
sl@0:   }
sl@0:   return  (pCheck->anRef[iPage]++)>1;
sl@0: }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0: /*
sl@0: ** Check that the entry in the pointer-map for page iChild maps to 
sl@0: ** page iParent, pointer type ptrType. If not, append an error message
sl@0: ** to pCheck.
sl@0: */
sl@0: static void checkPtrmap(
sl@0:   IntegrityCk *pCheck,   /* Integrity check context */
sl@0:   Pgno iChild,           /* Child page number */
sl@0:   u8 eType,              /* Expected pointer map type */
sl@0:   Pgno iParent,          /* Expected pointer map parent page number */
sl@0:   char *zContext         /* Context description (used for error msg) */
sl@0: ){
sl@0:   int rc;
sl@0:   u8 ePtrmapType;
sl@0:   Pgno iPtrmapParent;
sl@0: 
sl@0:   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
sl@0:   if( rc!=SQLITE_OK ){
sl@0:     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
sl@0:     return;
sl@0:   }
sl@0: 
sl@0:   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
sl@0:     checkAppendMsg(pCheck, zContext, 
sl@0:       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 
sl@0:       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
sl@0:   }
sl@0: }
sl@0: #endif
sl@0: 
sl@0: /*
sl@0: ** Check the integrity of the freelist or of an overflow page list.
sl@0: ** Verify that the number of pages on the list is N.
sl@0: */
sl@0: static void checkList(
sl@0:   IntegrityCk *pCheck,  /* Integrity checking context */
sl@0:   int isFreeList,       /* True for a freelist.  False for overflow page list */
sl@0:   int iPage,            /* Page number for first page in the list */
sl@0:   int N,                /* Expected number of pages in the list */
sl@0:   char *zContext        /* Context for error messages */
sl@0: ){
sl@0:   int i;
sl@0:   int expected = N;
sl@0:   int iFirst = iPage;
sl@0:   while( N-- > 0 && pCheck->mxErr ){
sl@0:     DbPage *pOvflPage;
sl@0:     unsigned char *pOvflData;
sl@0:     if( iPage<1 ){
sl@0:       checkAppendMsg(pCheck, zContext,
sl@0:          "%d of %d pages missing from overflow list starting at %d",
sl@0:           N+1, expected, iFirst);
sl@0:       break;
sl@0:     }
sl@0:     if( checkRef(pCheck, iPage, zContext) ) break;
sl@0:     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
sl@0:       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
sl@0:       break;
sl@0:     }
sl@0:     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
sl@0:     if( isFreeList ){
sl@0:       int n = get4byte(&pOvflData[4]);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       if( pCheck->pBt->autoVacuum ){
sl@0:         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
sl@0:       }
sl@0: #endif
sl@0:       if( n>pCheck->pBt->usableSize/4-2 ){
sl@0:         checkAppendMsg(pCheck, zContext,
sl@0:            "freelist leaf count too big on page %d", iPage);
sl@0:         N--;
sl@0:       }else{
sl@0:         for(i=0; i<n; i++){
sl@0:           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:           if( pCheck->pBt->autoVacuum ){
sl@0:             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
sl@0:           }
sl@0: #endif
sl@0:           checkRef(pCheck, iFreePage, zContext);
sl@0:         }
sl@0:         N -= n;
sl@0:       }
sl@0:     }
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     else{
sl@0:       /* If this database supports auto-vacuum and iPage is not the last
sl@0:       ** page in this overflow list, check that the pointer-map entry for
sl@0:       ** the following page matches iPage.
sl@0:       */
sl@0:       if( pCheck->pBt->autoVacuum && N>0 ){
sl@0:         i = get4byte(pOvflData);
sl@0:         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
sl@0:       }
sl@0:     }
sl@0: #endif
sl@0:     iPage = get4byte(pOvflData);
sl@0:     sqlite3PagerUnref(pOvflPage);
sl@0:   }
sl@0: }
sl@0: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
sl@0: /*
sl@0: ** Do various sanity checks on a single page of a tree.  Return
sl@0: ** the tree depth.  Root pages return 0.  Parents of root pages
sl@0: ** return 1, and so forth.
sl@0: ** 
sl@0: ** These checks are done:
sl@0: **
sl@0: **      1.  Make sure that cells and freeblocks do not overlap
sl@0: **          but combine to completely cover the page.
sl@0: **  NO  2.  Make sure cell keys are in order.
sl@0: **  NO  3.  Make sure no key is less than or equal to zLowerBound.
sl@0: **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
sl@0: **      5.  Check the integrity of overflow pages.
sl@0: **      6.  Recursively call checkTreePage on all children.
sl@0: **      7.  Verify that the depth of all children is the same.
sl@0: **      8.  Make sure this page is at least 33% full or else it is
sl@0: **          the root of the tree.
sl@0: */
sl@0: static int checkTreePage(
sl@0:   IntegrityCk *pCheck,  /* Context for the sanity check */
sl@0:   int iPage,            /* Page number of the page to check */
sl@0:   MemPage *pParent,     /* Parent page */
sl@0:   char *zParentContext  /* Parent context */
sl@0: ){
sl@0:   MemPage *pPage;
sl@0:   int i, rc, depth, d2, pgno, cnt;
sl@0:   int hdr, cellStart;
sl@0:   int nCell;
sl@0:   u8 *data;
sl@0:   BtShared *pBt;
sl@0:   int usableSize;
sl@0:   char zContext[100];
sl@0:   char *hit;
sl@0: 
sl@0:   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
sl@0: 
sl@0:   /* Check that the page exists
sl@0:   */
sl@0:   pBt = pCheck->pBt;
sl@0:   usableSize = pBt->usableSize;
sl@0:   if( iPage==0 ) return 0;
sl@0:   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
sl@0:   if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
sl@0:     checkAppendMsg(pCheck, zContext,
sl@0:        "unable to get the page. error code=%d", rc);
sl@0:     return 0;
sl@0:   }
sl@0:   if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){
sl@0:     checkAppendMsg(pCheck, zContext, 
sl@0:                    "sqlite3BtreeInitPage() returns error code %d", rc);
sl@0:     releasePage(pPage);
sl@0:     return 0;
sl@0:   }
sl@0: 
sl@0:   /* Check out all the cells.
sl@0:   */
sl@0:   depth = 0;
sl@0:   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
sl@0:     u8 *pCell;
sl@0:     int sz;
sl@0:     CellInfo info;
sl@0: 
sl@0:     /* Check payload overflow pages
sl@0:     */
sl@0:     sqlite3_snprintf(sizeof(zContext), zContext,
sl@0:              "On tree page %d cell %d: ", iPage, i);
sl@0:     pCell = findCell(pPage,i);
sl@0:     sqlite3BtreeParseCellPtr(pPage, pCell, &info);
sl@0:     sz = info.nData;
sl@0:     if( !pPage->intKey ) sz += info.nKey;
sl@0:     assert( sz==info.nPayload );
sl@0:     if( sz>info.nLocal ){
sl@0:       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
sl@0:       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       if( pBt->autoVacuum ){
sl@0:         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
sl@0:       }
sl@0: #endif
sl@0:       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
sl@0:     }
sl@0: 
sl@0:     /* Check sanity of left child page.
sl@0:     */
sl@0:     if( !pPage->leaf ){
sl@0:       pgno = get4byte(pCell);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:       if( pBt->autoVacuum ){
sl@0:         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
sl@0:       }
sl@0: #endif
sl@0:       d2 = checkTreePage(pCheck,pgno,pPage,zContext);
sl@0:       if( i>0 && d2!=depth ){
sl@0:         checkAppendMsg(pCheck, zContext, "Child page depth differs");
sl@0:       }
sl@0:       depth = d2;
sl@0:     }
sl@0:   }
sl@0:   if( !pPage->leaf ){
sl@0:     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
sl@0:     sqlite3_snprintf(sizeof(zContext), zContext, 
sl@0:                      "On page %d at right child: ", iPage);
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( pBt->autoVacuum ){
sl@0:       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
sl@0:     }
sl@0: #endif
sl@0:     checkTreePage(pCheck, pgno, pPage, zContext);
sl@0:   }
sl@0:  
sl@0:   /* Check for complete coverage of the page
sl@0:   */
sl@0:   data = pPage->aData;
sl@0:   hdr = pPage->hdrOffset;
sl@0:   hit = sqlite3PageMalloc( pBt->pageSize );
sl@0:   if( hit==0 ){
sl@0:     pCheck->mallocFailed = 1;
sl@0:   }else{
sl@0:     memset(hit, 0, usableSize );
sl@0:     memset(hit, 1, get2byte(&data[hdr+5]));
sl@0:     nCell = get2byte(&data[hdr+3]);
sl@0:     cellStart = hdr + 12 - 4*pPage->leaf;
sl@0:     for(i=0; i<nCell; i++){
sl@0:       int pc = get2byte(&data[cellStart+i*2]);
sl@0:       u16 size = cellSizePtr(pPage, &data[pc]);
sl@0:       int j;
sl@0:       if( (pc+size-1)>=usableSize || pc<0 ){
sl@0:         checkAppendMsg(pCheck, 0, 
sl@0:             "Corruption detected in cell %d on page %d",i,iPage,0);
sl@0:       }else{
sl@0:         for(j=pc+size-1; j>=pc; j--) hit[j]++;
sl@0:       }
sl@0:     }
sl@0:     for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; 
sl@0:            cnt++){
sl@0:       int size = get2byte(&data[i+2]);
sl@0:       int j;
sl@0:       if( (i+size-1)>=usableSize || i<0 ){
sl@0:         checkAppendMsg(pCheck, 0,  
sl@0:             "Corruption detected in cell %d on page %d",i,iPage,0);
sl@0:       }else{
sl@0:         for(j=i+size-1; j>=i; j--) hit[j]++;
sl@0:       }
sl@0:       i = get2byte(&data[i]);
sl@0:     }
sl@0:     for(i=cnt=0; i<usableSize; i++){
sl@0:       if( hit[i]==0 ){
sl@0:         cnt++;
sl@0:       }else if( hit[i]>1 ){
sl@0:         checkAppendMsg(pCheck, 0,
sl@0:           "Multiple uses for byte %d of page %d", i, iPage);
sl@0:         break;
sl@0:       }
sl@0:     }
sl@0:     if( cnt!=data[hdr+7] ){
sl@0:       checkAppendMsg(pCheck, 0, 
sl@0:           "Fragmented space is %d byte reported as %d on page %d",
sl@0:           cnt, data[hdr+7], iPage);
sl@0:     }
sl@0:   }
sl@0:   sqlite3PageFree(hit);
sl@0: 
sl@0:   releasePage(pPage);
sl@0:   return depth+1;
sl@0: }
sl@0: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INTEGRITY_CHECK
sl@0: /*
sl@0: ** This routine does a complete check of the given BTree file.  aRoot[] is
sl@0: ** an array of pages numbers were each page number is the root page of
sl@0: ** a table.  nRoot is the number of entries in aRoot.
sl@0: **
sl@0: ** Write the number of error seen in *pnErr.  Except for some memory
sl@0: ** allocation errors,  nn error message is held in memory obtained from
sl@0: ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
sl@0: ** returned.
sl@0: */
sl@0: char *sqlite3BtreeIntegrityCheck(
sl@0:   Btree *p,     /* The btree to be checked */
sl@0:   int *aRoot,   /* An array of root pages numbers for individual trees */
sl@0:   int nRoot,    /* Number of entries in aRoot[] */
sl@0:   int mxErr,    /* Stop reporting errors after this many */
sl@0:   int *pnErr    /* Write number of errors seen to this variable */
sl@0: ){
sl@0:   int i;
sl@0:   int nRef;
sl@0:   IntegrityCk sCheck;
sl@0:   BtShared *pBt = p->pBt;
sl@0:   char zErr[100];
sl@0: 
sl@0:   sqlite3BtreeEnter(p);
sl@0:   pBt->db = p->db;
sl@0:   nRef = sqlite3PagerRefcount(pBt->pPager);
sl@0:   if( lockBtreeWithRetry(p)!=SQLITE_OK ){
sl@0:     *pnErr = 1;
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
sl@0:   }
sl@0:   sCheck.pBt = pBt;
sl@0:   sCheck.pPager = pBt->pPager;
sl@0:   sCheck.nPage = pagerPagecount(sCheck.pPager);
sl@0:   sCheck.mxErr = mxErr;
sl@0:   sCheck.nErr = 0;
sl@0:   sCheck.mallocFailed = 0;
sl@0:   *pnErr = 0;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:   if( pBt->nTrunc!=0 ){
sl@0:     sCheck.nPage = pBt->nTrunc;
sl@0:   }
sl@0: #endif
sl@0:   if( sCheck.nPage==0 ){
sl@0:     unlockBtreeIfUnused(pBt);
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return 0;
sl@0:   }
sl@0:   sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
sl@0:   if( !sCheck.anRef ){
sl@0:     unlockBtreeIfUnused(pBt);
sl@0:     *pnErr = 1;
sl@0:     sqlite3BtreeLeave(p);
sl@0:     return 0;
sl@0:   }
sl@0:   for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
sl@0:   i = PENDING_BYTE_PAGE(pBt);
sl@0:   if( i<=sCheck.nPage ){
sl@0:     sCheck.anRef[i] = 1;
sl@0:   }
sl@0:   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
sl@0: 
sl@0:   /* Check the integrity of the freelist
sl@0:   */
sl@0:   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
sl@0:             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
sl@0: 
sl@0:   /* Check all the tables.
sl@0:   */
sl@0:   for(i=0; i<nRoot && sCheck.mxErr; i++){
sl@0:     if( aRoot[i]==0 ) continue;
sl@0: #ifndef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( pBt->autoVacuum && aRoot[i]>1 ){
sl@0:       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
sl@0:     }
sl@0: #endif
sl@0:     checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
sl@0:   }
sl@0: 
sl@0:   /* Make sure every page in the file is referenced
sl@0:   */
sl@0:   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
sl@0: #ifdef SQLITE_OMIT_AUTOVACUUM
sl@0:     if( sCheck.anRef[i]==0 ){
sl@0:       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
sl@0:     }
sl@0: #else
sl@0:     /* If the database supports auto-vacuum, make sure no tables contain
sl@0:     ** references to pointer-map pages.
sl@0:     */
sl@0:     if( sCheck.anRef[i]==0 && 
sl@0:        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
sl@0:       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
sl@0:     }
sl@0:     if( sCheck.anRef[i]!=0 && 
sl@0:        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
sl@0:       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
sl@0:     }
sl@0: #endif
sl@0:   }
sl@0: 
sl@0:   /* Make sure this analysis did not leave any unref() pages
sl@0:   */
sl@0:   unlockBtreeIfUnused(pBt);
sl@0:   if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
sl@0:     checkAppendMsg(&sCheck, 0, 
sl@0:       "Outstanding page count goes from %d to %d during this analysis",
sl@0:       nRef, sqlite3PagerRefcount(pBt->pPager)
sl@0:     );
sl@0:   }
sl@0: 
sl@0:   /* Clean  up and report errors.
sl@0:   */
sl@0:   sqlite3BtreeLeave(p);
sl@0:   sqlite3_free(sCheck.anRef);
sl@0:   if( sCheck.mallocFailed ){
sl@0:     sqlite3StrAccumReset(&sCheck.errMsg);
sl@0:     *pnErr = sCheck.nErr+1;
sl@0:     return 0;
sl@0:   }
sl@0:   *pnErr = sCheck.nErr;
sl@0:   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
sl@0:   return sqlite3StrAccumFinish(&sCheck.errMsg);
sl@0: }
sl@0: #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
sl@0: 
sl@0: /*
sl@0: ** Return the full pathname of the underlying database file.
sl@0: **
sl@0: ** The pager filename is invariant as long as the pager is
sl@0: ** open so it is safe to access without the BtShared mutex.
sl@0: */
sl@0: const char *sqlite3BtreeGetFilename(Btree *p){
sl@0:   assert( p->pBt->pPager!=0 );
sl@0:   return sqlite3PagerFilename(p->pBt->pPager);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the pathname of the directory that contains the database file.
sl@0: **
sl@0: ** The pager directory name is invariant as long as the pager is
sl@0: ** open so it is safe to access without the BtShared mutex.
sl@0: */
sl@0: const char *sqlite3BtreeGetDirname(Btree *p){
sl@0:   assert( p->pBt->pPager!=0 );
sl@0:   return sqlite3PagerDirname(p->pBt->pPager);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return the pathname of the journal file for this database. The return
sl@0: ** value of this routine is the same regardless of whether the journal file
sl@0: ** has been created or not.
sl@0: **
sl@0: ** The pager journal filename is invariant as long as the pager is
sl@0: ** open so it is safe to access without the BtShared mutex.
sl@0: */
sl@0: const char *sqlite3BtreeGetJournalname(Btree *p){
sl@0:   assert( p->pBt->pPager!=0 );
sl@0:   return sqlite3PagerJournalname(p->pBt->pPager);
sl@0: }
sl@0: 
sl@0: #ifndef SQLITE_OMIT_VACUUM
sl@0: /*
sl@0: ** Copy the complete content of pBtFrom into pBtTo.  A transaction
sl@0: ** must be active for both files.
sl@0: **
sl@0: ** The size of file pTo may be reduced by this operation.
sl@0: ** If anything goes wrong, the transaction on pTo is rolled back. 
sl@0: **
sl@0: ** If successful, CommitPhaseOne() may be called on pTo before returning. 
sl@0: ** The caller should finish committing the transaction on pTo by calling
sl@0: ** sqlite3BtreeCommit().
sl@0: */
sl@0: static int btreeCopyFile(Btree *pTo, Btree *pFrom){
sl@0:   int rc = SQLITE_OK;
sl@0:   Pgno i;
sl@0: 
sl@0:   Pgno nFromPage;     /* Number of pages in pFrom */
sl@0:   Pgno nToPage;       /* Number of pages in pTo */
sl@0:   Pgno nNewPage;      /* Number of pages in pTo after the copy */
sl@0: 
sl@0:   Pgno iSkip;         /* Pending byte page in pTo */
sl@0:   int nToPageSize;    /* Page size of pTo in bytes */
sl@0:   int nFromPageSize;  /* Page size of pFrom in bytes */
sl@0: 
sl@0:   BtShared *pBtTo = pTo->pBt;
sl@0:   BtShared *pBtFrom = pFrom->pBt;
sl@0:   pBtTo->db = pTo->db;
sl@0:   pBtFrom->db = pFrom->db;
sl@0: 
sl@0:   nToPageSize = pBtTo->pageSize;
sl@0:   nFromPageSize = pBtFrom->pageSize;
sl@0: 
sl@0:   if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
sl@0:     return SQLITE_ERROR;
sl@0:   }
sl@0:   if( pBtTo->pCursor ){
sl@0:     return SQLITE_BUSY;
sl@0:   }
sl@0: 
sl@0:   nToPage = pagerPagecount(pBtTo->pPager);
sl@0:   nFromPage = pagerPagecount(pBtFrom->pPager);
sl@0:   iSkip = PENDING_BYTE_PAGE(pBtTo);
sl@0: 
sl@0:   /* Variable nNewPage is the number of pages required to store the
sl@0:   ** contents of pFrom using the current page-size of pTo.
sl@0:   */
sl@0:   nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / 
sl@0:       (i64)nToPageSize;
sl@0: 
sl@0:   for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){
sl@0: 
sl@0:     /* Journal the original page.
sl@0:     **
sl@0:     ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE)
sl@0:     ** in database *pTo (before the copy). This page is never written 
sl@0:     ** into the journal file. Unless i==iSkip or the page was not
sl@0:     ** present in pTo before the copy operation, journal page i from pTo.
sl@0:     */
sl@0:     if( i!=iSkip && i<=nToPage ){
sl@0:       DbPage *pDbPage = 0;
sl@0:       rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         rc = sqlite3PagerWrite(pDbPage);
sl@0:         if( rc==SQLITE_OK && i>nFromPage ){
sl@0:           /* Yeah.  It seems wierd to call DontWrite() right after Write(). But
sl@0:           ** that is because the names of those procedures do not exactly 
sl@0:           ** represent what they do.  Write() really means "put this page in the
sl@0:           ** rollback journal and mark it as dirty so that it will be written
sl@0:           ** to the database file later."  DontWrite() undoes the second part of
sl@0:           ** that and prevents the page from being written to the database. The
sl@0:           ** page is still on the rollback journal, though.  And that is the 
sl@0:           ** whole point of this block: to put pages on the rollback journal. 
sl@0:           */
sl@0:           sqlite3PagerDontWrite(pDbPage);
sl@0:         }
sl@0:         sqlite3PagerUnref(pDbPage);
sl@0:       }
sl@0:     }
sl@0: 
sl@0:     /* Overwrite the data in page i of the target database */
sl@0:     if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){
sl@0: 
sl@0:       DbPage *pToPage = 0;
sl@0:       sqlite3_int64 iOff;
sl@0: 
sl@0:       rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage);
sl@0:       if( rc==SQLITE_OK ){
sl@0:         rc = sqlite3PagerWrite(pToPage);
sl@0:       }
sl@0: 
sl@0:       for(
sl@0:         iOff=(i-1)*nToPageSize; 
sl@0:         rc==SQLITE_OK && iOff<i*nToPageSize; 
sl@0:         iOff += nFromPageSize
sl@0:       ){
sl@0:         DbPage *pFromPage = 0;
sl@0:         Pgno iFrom = (iOff/nFromPageSize)+1;
sl@0: 
sl@0:         if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){
sl@0:           continue;
sl@0:         }
sl@0: 
sl@0:         rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
sl@0:         if( rc==SQLITE_OK ){
sl@0:           char *zTo = sqlite3PagerGetData(pToPage);
sl@0:           char *zFrom = sqlite3PagerGetData(pFromPage);
sl@0:           int nCopy;
sl@0: 
sl@0:           if( nFromPageSize>=nToPageSize ){
sl@0:             zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize));
sl@0:             nCopy = nToPageSize;
sl@0:           }else{
sl@0:             zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize);
sl@0:             nCopy = nFromPageSize;
sl@0:           }
sl@0: 
sl@0:           memcpy(zTo, zFrom, nCopy);
sl@0: 	  sqlite3PagerUnref(pFromPage);
sl@0:         }
sl@0:       }
sl@0: 
sl@0:       if( pToPage ) sqlite3PagerUnref(pToPage);
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   /* If things have worked so far, the database file may need to be 
sl@0:   ** truncated. The complex part is that it may need to be truncated to
sl@0:   ** a size that is not an integer multiple of nToPageSize - the current
sl@0:   ** page size used by the pager associated with B-Tree pTo.
sl@0:   **
sl@0:   ** For example, say the page-size of pTo is 2048 bytes and the original 
sl@0:   ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 
sl@0:   ** bytes and 9 pages, then the file needs to be truncated to 9KB.
sl@0:   */
sl@0:   if( rc==SQLITE_OK ){
sl@0:     if( nFromPageSize!=nToPageSize ){
sl@0:       sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager);
sl@0:       i64 iSize = (i64)nFromPageSize * (i64)nFromPage;
sl@0:       i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; 
sl@0:       i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize;
sl@0:   
sl@0:       assert( iSize<=iNow );
sl@0:   
sl@0:       /* Commit phase one syncs the journal file associated with pTo 
sl@0:       ** containing the original data. It does not sync the database file
sl@0:       ** itself. After doing this it is safe to use OsTruncate() and other
sl@0:       ** file APIs on the database file directly.
sl@0:       */
sl@0:       pBtTo->db = pTo->db;
sl@0:       rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1);
sl@0:       if( iSize<iNow && rc==SQLITE_OK ){
sl@0:         rc = sqlite3OsTruncate(pFile, iSize);
sl@0:       }
sl@0:   
sl@0:       /* The loop that copied data from database pFrom to pTo did not
sl@0:       ** populate the locking page of database pTo. If the page-size of
sl@0:       ** pFrom is smaller than that of pTo, this means some data will
sl@0:       ** not have been copied. 
sl@0:       **
sl@0:       ** This block copies the missing data from database pFrom to pTo 
sl@0:       ** using file APIs. This is safe because at this point we know that
sl@0:       ** all of the original data from pTo has been synced into the 
sl@0:       ** journal file. At this point it would be safe to do anything at
sl@0:       ** all to the database file except truncate it to zero bytes.
sl@0:       */
sl@0:       if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){
sl@0:         i64 iOff;
sl@0:         for(
sl@0:           iOff=iPending; 
sl@0:           rc==SQLITE_OK && iOff<(iPending+nToPageSize); 
sl@0:           iOff += nFromPageSize
sl@0:         ){
sl@0:           DbPage *pFromPage = 0;
sl@0:           Pgno iFrom = (iOff/nFromPageSize)+1;
sl@0:   
sl@0:           if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){
sl@0:             continue;
sl@0:           }
sl@0:   
sl@0:           rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
sl@0:           if( rc==SQLITE_OK ){
sl@0:             char *zFrom = sqlite3PagerGetData(pFromPage);
sl@0:   	  rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff);
sl@0:             sqlite3PagerUnref(pFromPage);
sl@0:           }
sl@0:         }
sl@0:       }
sl@0:   
sl@0:       /* Sync the database file */
sl@0:       if( rc==SQLITE_OK ){
sl@0:         rc = sqlite3PagerSync(pBtTo->pPager);
sl@0:       }
sl@0:     }else{
sl@0:       rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage);
sl@0:     }
sl@0:     if( rc==SQLITE_OK ){
sl@0:       pBtTo->pageSizeFixed = 0;
sl@0:     }
sl@0:   }
sl@0: 
sl@0:   if( rc ){
sl@0:     sqlite3BtreeRollback(pTo);
sl@0:   }
sl@0: 
sl@0:   return rc;  
sl@0: }
sl@0: int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
sl@0:   int rc;
sl@0:   sqlite3BtreeEnter(pTo);
sl@0:   sqlite3BtreeEnter(pFrom);
sl@0:   rc = btreeCopyFile(pTo, pFrom);
sl@0:   sqlite3BtreeLeave(pFrom);
sl@0:   sqlite3BtreeLeave(pTo);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: #endif /* SQLITE_OMIT_VACUUM */
sl@0: 
sl@0: /*
sl@0: ** Return non-zero if a transaction is active.
sl@0: */
sl@0: int sqlite3BtreeIsInTrans(Btree *p){
sl@0:   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
sl@0:   return (p && (p->inTrans==TRANS_WRITE));
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return non-zero if a statement transaction is active.
sl@0: */
sl@0: int sqlite3BtreeIsInStmt(Btree *p){
sl@0:   assert( sqlite3BtreeHoldsMutex(p) );
sl@0:   return (p->pBt && p->pBt->inStmt);
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return non-zero if a read (or write) transaction is active.
sl@0: */
sl@0: int sqlite3BtreeIsInReadTrans(Btree *p){
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );
sl@0:   return (p && (p->inTrans!=TRANS_NONE));
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** This function returns a pointer to a blob of memory associated with
sl@0: ** a single shared-btree. The memory is used by client code for its own
sl@0: ** purposes (for example, to store a high-level schema associated with 
sl@0: ** the shared-btree). The btree layer manages reference counting issues.
sl@0: **
sl@0: ** The first time this is called on a shared-btree, nBytes bytes of memory
sl@0: ** are allocated, zeroed, and returned to the caller. For each subsequent 
sl@0: ** call the nBytes parameter is ignored and a pointer to the same blob
sl@0: ** of memory returned. 
sl@0: **
sl@0: ** If the nBytes parameter is 0 and the blob of memory has not yet been
sl@0: ** allocated, a null pointer is returned. If the blob has already been
sl@0: ** allocated, it is returned as normal.
sl@0: **
sl@0: ** Just before the shared-btree is closed, the function passed as the 
sl@0: ** xFree argument when the memory allocation was made is invoked on the 
sl@0: ** blob of allocated memory. This function should not call sqlite3_free()
sl@0: ** on the memory, the btree layer does that.
sl@0: */
sl@0: void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
sl@0:   BtShared *pBt = p->pBt;
sl@0:   sqlite3BtreeEnter(p);
sl@0:   if( !pBt->pSchema && nBytes ){
sl@0:     pBt->pSchema = sqlite3MallocZero(nBytes);
sl@0:     pBt->xFreeSchema = xFree;
sl@0:   }
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return pBt->pSchema;
sl@0: }
sl@0: 
sl@0: /*
sl@0: ** Return true if another user of the same shared btree as the argument
sl@0: ** handle holds an exclusive lock on the sqlite_master table.
sl@0: */
sl@0: int sqlite3BtreeSchemaLocked(Btree *p){
sl@0:   int rc;
sl@0:   assert( sqlite3_mutex_held(p->db->mutex) );
sl@0:   sqlite3BtreeEnter(p);
sl@0:   rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
sl@0:   sqlite3BtreeLeave(p);
sl@0:   return rc;
sl@0: }
sl@0: 
sl@0: 
sl@0: #ifndef SQLITE_OMIT_SHARED_CACHE
sl@0: /*
sl@0: ** Obtain a lock on the table whose root page is iTab.  The
sl@0: ** lock is a write lock if isWritelock is true or a read lock
sl@0: ** if it is false.
sl@0: */
sl@0: int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
sl@0:   int rc = SQLITE_OK;
sl@0:   if( p->sharable ){
sl@0:     u8 lockType = READ_LOCK + isWriteLock;
sl@0:     assert( READ_LOCK+1==WRITE_LOCK );
sl@0:     assert( isWriteLock==0 || isWriteLock==1 );
sl@0:     sqlite3BtreeEnter(p);
sl@0:     rc = queryTableLock(p, iTab, lockType);
sl@0:     if( rc==SQLITE_OK ){
sl@0:       rc = lockTable(p, iTab, lockType);
sl@0:     }
sl@0:     sqlite3BtreeLeave(p);
sl@0:   }
sl@0:   return rc;
sl@0: }
sl@0: #endif
sl@0: 
sl@0: #ifndef SQLITE_OMIT_INCRBLOB
sl@0: /*
sl@0: ** Argument pCsr must be a cursor opened for writing on an 
sl@0: ** INTKEY table currently pointing at a valid table entry. 
sl@0: ** This function modifies the data stored as part of that entry.
sl@0: ** Only the data content may only be modified, it is not possible
sl@0: ** to change the length of the data stored.
sl@0: */
sl@0: int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
sl@0:   assert( cursorHoldsMutex(pCsr) );
sl@0:   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
sl@0:   assert(pCsr->isIncrblobHandle);
sl@0: 
sl@0:   restoreCursorPosition(pCsr);
sl@0:   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
sl@0:   if( pCsr->eState!=CURSOR_VALID ){
sl@0:     return SQLITE_ABORT;
sl@0:   }
sl@0: 
sl@0:   /* Check some preconditions: 
sl@0:   **   (a) the cursor is open for writing,
sl@0:   **   (b) there is no read-lock on the table being modified and
sl@0:   **   (c) the cursor points at a valid row of an intKey table.
sl@0:   */
sl@0:   if( !pCsr->wrFlag ){
sl@0:     return SQLITE_READONLY;
sl@0:   }
sl@0:   assert( !pCsr->pBt->readOnly 
sl@0:           && pCsr->pBt->inTransaction==TRANS_WRITE );
sl@0:   if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){
sl@0:     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
sl@0:   }
sl@0:   if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
sl@0:     return SQLITE_ERROR;
sl@0:   }
sl@0: 
sl@0:   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
sl@0: }
sl@0: 
sl@0: /* 
sl@0: ** Set a flag on this cursor to cache the locations of pages from the 
sl@0: ** overflow list for the current row. This is used by cursors opened
sl@0: ** for incremental blob IO only.
sl@0: **
sl@0: ** This function sets a flag only. The actual page location cache
sl@0: ** (stored in BtCursor.aOverflow[]) is allocated and used by function
sl@0: ** accessPayload() (the worker function for sqlite3BtreeData() and
sl@0: ** sqlite3BtreePutData()).
sl@0: */
sl@0: void sqlite3BtreeCacheOverflow(BtCursor *pCur){
sl@0:   assert( cursorHoldsMutex(pCur) );
sl@0:   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
sl@0:   assert(!pCur->isIncrblobHandle);
sl@0:   assert(!pCur->aOverflow);
sl@0:   pCur->isIncrblobHandle = 1;
sl@0: }
sl@0: #endif