1.1 --- a/extensions/sqlite/sqlite-source/btree.c Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,6890 +0,0 @@
1.4 -/*
1.5 -** 2004 April 6
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** $Id$
1.16 -**
1.17 -** This file implements a external (disk-based) database using BTrees.
1.18 -** See the header comment on "btreeInt.h" for additional information.
1.19 -** Including a description of file format and an overview of operation.
1.20 -*/
1.21 -#include "btreeInt.h"
1.22 -
1.23 -/*
1.24 -** The header string that appears at the beginning of every
1.25 -** SQLite database.
1.26 -*/
1.27 -static const char zMagicHeader[] = SQLITE_FILE_HEADER;
1.28 -
1.29 -/*
1.30 -** Set this global variable to 1 to enable tracing using the TRACE
1.31 -** macro.
1.32 -*/
1.33 -#if SQLITE_TEST
1.34 -int sqlite3_btree_trace=0; /* True to enable tracing */
1.35 -#endif
1.36 -
1.37 -
1.38 -
1.39 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.40 -/*
1.41 -** A flag to indicate whether or not shared cache is enabled. Also,
1.42 -** a list of BtShared objects that are eligible for participation
1.43 -** in shared cache. The variables have file scope during normal builds,
1.44 -** but the test harness needs to access these variables so we make them
1.45 -** global for test builds.
1.46 -*/
1.47 -#ifdef SQLITE_TEST
1.48 -BtShared *sqlite3SharedCacheList = 0;
1.49 -int sqlite3SharedCacheEnabled = 0;
1.50 -#else
1.51 -static BtShared *sqlite3SharedCacheList = 0;
1.52 -static int sqlite3SharedCacheEnabled = 0;
1.53 -#endif
1.54 -#endif /* SQLITE_OMIT_SHARED_CACHE */
1.55 -
1.56 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.57 -/*
1.58 -** Enable or disable the shared pager and schema features.
1.59 -**
1.60 -** This routine has no effect on existing database connections.
1.61 -** The shared cache setting effects only future calls to
1.62 -** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
1.63 -*/
1.64 -int sqlite3_enable_shared_cache(int enable){
1.65 - sqlite3SharedCacheEnabled = enable;
1.66 - return SQLITE_OK;
1.67 -}
1.68 -#endif
1.69 -
1.70 -
1.71 -/*
1.72 -** Forward declaration
1.73 -*/
1.74 -static int checkReadLocks(Btree*,Pgno,BtCursor*);
1.75 -
1.76 -
1.77 -#ifdef SQLITE_OMIT_SHARED_CACHE
1.78 - /*
1.79 - ** The functions queryTableLock(), lockTable() and unlockAllTables()
1.80 - ** manipulate entries in the BtShared.pLock linked list used to store
1.81 - ** shared-cache table level locks. If the library is compiled with the
1.82 - ** shared-cache feature disabled, then there is only ever one user
1.83 - ** of each BtShared structure and so this locking is not necessary.
1.84 - ** So define the lock related functions as no-ops.
1.85 - */
1.86 - #define queryTableLock(a,b,c) SQLITE_OK
1.87 - #define lockTable(a,b,c) SQLITE_OK
1.88 - #define unlockAllTables(a)
1.89 -#endif
1.90 -
1.91 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.92 -/*
1.93 -** Query to see if btree handle p may obtain a lock of type eLock
1.94 -** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
1.95 -** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
1.96 -** SQLITE_LOCKED if not.
1.97 -*/
1.98 -static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
1.99 - BtShared *pBt = p->pBt;
1.100 - BtLock *pIter;
1.101 -
1.102 - assert( sqlite3BtreeHoldsMutex(p) );
1.103 -
1.104 - /* This is a no-op if the shared-cache is not enabled */
1.105 - if( !p->sharable ){
1.106 - return SQLITE_OK;
1.107 - }
1.108 -
1.109 - /* This (along with lockTable()) is where the ReadUncommitted flag is
1.110 - ** dealt with. If the caller is querying for a read-lock and the flag is
1.111 - ** set, it is unconditionally granted - even if there are write-locks
1.112 - ** on the table. If a write-lock is requested, the ReadUncommitted flag
1.113 - ** is not considered.
1.114 - **
1.115 - ** In function lockTable(), if a read-lock is demanded and the
1.116 - ** ReadUncommitted flag is set, no entry is added to the locks list
1.117 - ** (BtShared.pLock).
1.118 - **
1.119 - ** To summarize: If the ReadUncommitted flag is set, then read cursors do
1.120 - ** not create or respect table locks. The locking procedure for a
1.121 - ** write-cursor does not change.
1.122 - */
1.123 - if(
1.124 - !p->pSqlite ||
1.125 - 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) ||
1.126 - eLock==WRITE_LOCK ||
1.127 - iTab==MASTER_ROOT
1.128 - ){
1.129 - for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
1.130 - if( pIter->pBtree!=p && pIter->iTable==iTab &&
1.131 - (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
1.132 - return SQLITE_LOCKED;
1.133 - }
1.134 - }
1.135 - }
1.136 - return SQLITE_OK;
1.137 -}
1.138 -#endif /* !SQLITE_OMIT_SHARED_CACHE */
1.139 -
1.140 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.141 -/*
1.142 -** Add a lock on the table with root-page iTable to the shared-btree used
1.143 -** by Btree handle p. Parameter eLock must be either READ_LOCK or
1.144 -** WRITE_LOCK.
1.145 -**
1.146 -** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
1.147 -** SQLITE_NOMEM may also be returned.
1.148 -*/
1.149 -static int lockTable(Btree *p, Pgno iTable, u8 eLock){
1.150 - BtShared *pBt = p->pBt;
1.151 - BtLock *pLock = 0;
1.152 - BtLock *pIter;
1.153 -
1.154 - assert( sqlite3BtreeHoldsMutex(p) );
1.155 -
1.156 - /* This is a no-op if the shared-cache is not enabled */
1.157 - if( !p->sharable ){
1.158 - return SQLITE_OK;
1.159 - }
1.160 -
1.161 - assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
1.162 -
1.163 - /* If the read-uncommitted flag is set and a read-lock is requested,
1.164 - ** return early without adding an entry to the BtShared.pLock list. See
1.165 - ** comment in function queryTableLock() for more info on handling
1.166 - ** the ReadUncommitted flag.
1.167 - */
1.168 - if(
1.169 - (p->pSqlite) &&
1.170 - (p->pSqlite->flags&SQLITE_ReadUncommitted) &&
1.171 - (eLock==READ_LOCK) &&
1.172 - iTable!=MASTER_ROOT
1.173 - ){
1.174 - return SQLITE_OK;
1.175 - }
1.176 -
1.177 - /* First search the list for an existing lock on this table. */
1.178 - for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
1.179 - if( pIter->iTable==iTable && pIter->pBtree==p ){
1.180 - pLock = pIter;
1.181 - break;
1.182 - }
1.183 - }
1.184 -
1.185 - /* If the above search did not find a BtLock struct associating Btree p
1.186 - ** with table iTable, allocate one and link it into the list.
1.187 - */
1.188 - if( !pLock ){
1.189 - pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
1.190 - if( !pLock ){
1.191 - return SQLITE_NOMEM;
1.192 - }
1.193 - pLock->iTable = iTable;
1.194 - pLock->pBtree = p;
1.195 - pLock->pNext = pBt->pLock;
1.196 - pBt->pLock = pLock;
1.197 - }
1.198 -
1.199 - /* Set the BtLock.eLock variable to the maximum of the current lock
1.200 - ** and the requested lock. This means if a write-lock was already held
1.201 - ** and a read-lock requested, we don't incorrectly downgrade the lock.
1.202 - */
1.203 - assert( WRITE_LOCK>READ_LOCK );
1.204 - if( eLock>pLock->eLock ){
1.205 - pLock->eLock = eLock;
1.206 - }
1.207 -
1.208 - return SQLITE_OK;
1.209 -}
1.210 -#endif /* !SQLITE_OMIT_SHARED_CACHE */
1.211 -
1.212 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.213 -/*
1.214 -** Release all the table locks (locks obtained via calls to the lockTable()
1.215 -** procedure) held by Btree handle p.
1.216 -*/
1.217 -static void unlockAllTables(Btree *p){
1.218 - BtLock **ppIter = &p->pBt->pLock;
1.219 -
1.220 - assert( sqlite3BtreeHoldsMutex(p) );
1.221 - assert( p->sharable || 0==*ppIter );
1.222 -
1.223 - while( *ppIter ){
1.224 - BtLock *pLock = *ppIter;
1.225 - if( pLock->pBtree==p ){
1.226 - *ppIter = pLock->pNext;
1.227 - sqlite3_free(pLock);
1.228 - }else{
1.229 - ppIter = &pLock->pNext;
1.230 - }
1.231 - }
1.232 -}
1.233 -#endif /* SQLITE_OMIT_SHARED_CACHE */
1.234 -
1.235 -static void releasePage(MemPage *pPage); /* Forward reference */
1.236 -
1.237 -/*
1.238 -** Verify that the cursor holds a mutex on the BtShared
1.239 -*/
1.240 -#ifndef NDEBUG
1.241 -static int cursorHoldsMutex(BtCursor *p){
1.242 - return sqlite3_mutex_held(p->pBt->mutex);
1.243 -}
1.244 -#endif
1.245 -
1.246 -
1.247 -#ifndef SQLITE_OMIT_INCRBLOB
1.248 -/*
1.249 -** Invalidate the overflow page-list cache for cursor pCur, if any.
1.250 -*/
1.251 -static void invalidateOverflowCache(BtCursor *pCur){
1.252 - assert( cursorHoldsMutex(pCur) );
1.253 - sqlite3_free(pCur->aOverflow);
1.254 - pCur->aOverflow = 0;
1.255 -}
1.256 -
1.257 -/*
1.258 -** Invalidate the overflow page-list cache for all cursors opened
1.259 -** on the shared btree structure pBt.
1.260 -*/
1.261 -static void invalidateAllOverflowCache(BtShared *pBt){
1.262 - BtCursor *p;
1.263 - assert( sqlite3_mutex_held(pBt->mutex) );
1.264 - for(p=pBt->pCursor; p; p=p->pNext){
1.265 - invalidateOverflowCache(p);
1.266 - }
1.267 -}
1.268 -#else
1.269 - #define invalidateOverflowCache(x)
1.270 - #define invalidateAllOverflowCache(x)
1.271 -#endif
1.272 -
1.273 -/*
1.274 -** Save the current cursor position in the variables BtCursor.nKey
1.275 -** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
1.276 -*/
1.277 -static int saveCursorPosition(BtCursor *pCur){
1.278 - int rc;
1.279 -
1.280 - assert( CURSOR_VALID==pCur->eState );
1.281 - assert( 0==pCur->pKey );
1.282 - assert( cursorHoldsMutex(pCur) );
1.283 -
1.284 - rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
1.285 -
1.286 - /* If this is an intKey table, then the above call to BtreeKeySize()
1.287 - ** stores the integer key in pCur->nKey. In this case this value is
1.288 - ** all that is required. Otherwise, if pCur is not open on an intKey
1.289 - ** table, then malloc space for and store the pCur->nKey bytes of key
1.290 - ** data.
1.291 - */
1.292 - if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
1.293 - void *pKey = sqlite3_malloc(pCur->nKey);
1.294 - if( pKey ){
1.295 - rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
1.296 - if( rc==SQLITE_OK ){
1.297 - pCur->pKey = pKey;
1.298 - }else{
1.299 - sqlite3_free(pKey);
1.300 - }
1.301 - }else{
1.302 - rc = SQLITE_NOMEM;
1.303 - }
1.304 - }
1.305 - assert( !pCur->pPage->intKey || !pCur->pKey );
1.306 -
1.307 - if( rc==SQLITE_OK ){
1.308 - releasePage(pCur->pPage);
1.309 - pCur->pPage = 0;
1.310 - pCur->eState = CURSOR_REQUIRESEEK;
1.311 - }
1.312 -
1.313 - invalidateOverflowCache(pCur);
1.314 - return rc;
1.315 -}
1.316 -
1.317 -/*
1.318 -** Save the positions of all cursors except pExcept open on the table
1.319 -** with root-page iRoot. Usually, this is called just before cursor
1.320 -** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
1.321 -*/
1.322 -static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
1.323 - BtCursor *p;
1.324 - assert( sqlite3_mutex_held(pBt->mutex) );
1.325 - assert( pExcept==0 || pExcept->pBt==pBt );
1.326 - for(p=pBt->pCursor; p; p=p->pNext){
1.327 - if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
1.328 - p->eState==CURSOR_VALID ){
1.329 - int rc = saveCursorPosition(p);
1.330 - if( SQLITE_OK!=rc ){
1.331 - return rc;
1.332 - }
1.333 - }
1.334 - }
1.335 - return SQLITE_OK;
1.336 -}
1.337 -
1.338 -/*
1.339 -** Clear the current cursor position.
1.340 -*/
1.341 -static void clearCursorPosition(BtCursor *pCur){
1.342 - assert( cursorHoldsMutex(pCur) );
1.343 - sqlite3_free(pCur->pKey);
1.344 - pCur->pKey = 0;
1.345 - pCur->eState = CURSOR_INVALID;
1.346 -}
1.347 -
1.348 -/*
1.349 -** Restore the cursor to the position it was in (or as close to as possible)
1.350 -** when saveCursorPosition() was called. Note that this call deletes the
1.351 -** saved position info stored by saveCursorPosition(), so there can be
1.352 -** at most one effective restoreOrClearCursorPosition() call after each
1.353 -** saveCursorPosition().
1.354 -**
1.355 -** If the second argument argument - doSeek - is false, then instead of
1.356 -** returning the cursor to it's saved position, any saved position is deleted
1.357 -** and the cursor state set to CURSOR_INVALID.
1.358 -*/
1.359 -int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){
1.360 - int rc;
1.361 - assert( cursorHoldsMutex(pCur) );
1.362 - assert( pCur->eState>=CURSOR_REQUIRESEEK );
1.363 - if( pCur->eState==CURSOR_FAULT ){
1.364 - return pCur->skip;
1.365 - }
1.366 -#ifndef SQLITE_OMIT_INCRBLOB
1.367 - if( pCur->isIncrblobHandle ){
1.368 - return SQLITE_ABORT;
1.369 - }
1.370 -#endif
1.371 - pCur->eState = CURSOR_INVALID;
1.372 - rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
1.373 - if( rc==SQLITE_OK ){
1.374 - sqlite3_free(pCur->pKey);
1.375 - pCur->pKey = 0;
1.376 - assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
1.377 - }
1.378 - return rc;
1.379 -}
1.380 -
1.381 -#define restoreOrClearCursorPosition(p) \
1.382 - (p->eState>=CURSOR_REQUIRESEEK ? \
1.383 - sqlite3BtreeRestoreOrClearCursorPosition(p) : \
1.384 - SQLITE_OK)
1.385 -
1.386 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.387 -/*
1.388 -** Given a page number of a regular database page, return the page
1.389 -** number for the pointer-map page that contains the entry for the
1.390 -** input page number.
1.391 -*/
1.392 -static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
1.393 - int nPagesPerMapPage, iPtrMap, ret;
1.394 - assert( sqlite3_mutex_held(pBt->mutex) );
1.395 - nPagesPerMapPage = (pBt->usableSize/5)+1;
1.396 - iPtrMap = (pgno-2)/nPagesPerMapPage;
1.397 - ret = (iPtrMap*nPagesPerMapPage) + 2;
1.398 - if( ret==PENDING_BYTE_PAGE(pBt) ){
1.399 - ret++;
1.400 - }
1.401 - return ret;
1.402 -}
1.403 -
1.404 -/*
1.405 -** Write an entry into the pointer map.
1.406 -**
1.407 -** This routine updates the pointer map entry for page number 'key'
1.408 -** so that it maps to type 'eType' and parent page number 'pgno'.
1.409 -** An error code is returned if something goes wrong, otherwise SQLITE_OK.
1.410 -*/
1.411 -static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
1.412 - DbPage *pDbPage; /* The pointer map page */
1.413 - u8 *pPtrmap; /* The pointer map data */
1.414 - Pgno iPtrmap; /* The pointer map page number */
1.415 - int offset; /* Offset in pointer map page */
1.416 - int rc;
1.417 -
1.418 - assert( sqlite3_mutex_held(pBt->mutex) );
1.419 - /* The master-journal page number must never be used as a pointer map page */
1.420 - assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
1.421 -
1.422 - assert( pBt->autoVacuum );
1.423 - if( key==0 ){
1.424 - return SQLITE_CORRUPT_BKPT;
1.425 - }
1.426 - iPtrmap = PTRMAP_PAGENO(pBt, key);
1.427 - rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
1.428 - if( rc!=SQLITE_OK ){
1.429 - return rc;
1.430 - }
1.431 - offset = PTRMAP_PTROFFSET(pBt, key);
1.432 - pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1.433 -
1.434 - if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
1.435 - TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
1.436 - rc = sqlite3PagerWrite(pDbPage);
1.437 - if( rc==SQLITE_OK ){
1.438 - pPtrmap[offset] = eType;
1.439 - put4byte(&pPtrmap[offset+1], parent);
1.440 - }
1.441 - }
1.442 -
1.443 - sqlite3PagerUnref(pDbPage);
1.444 - return rc;
1.445 -}
1.446 -
1.447 -/*
1.448 -** Read an entry from the pointer map.
1.449 -**
1.450 -** This routine retrieves the pointer map entry for page 'key', writing
1.451 -** the type and parent page number to *pEType and *pPgno respectively.
1.452 -** An error code is returned if something goes wrong, otherwise SQLITE_OK.
1.453 -*/
1.454 -static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
1.455 - DbPage *pDbPage; /* The pointer map page */
1.456 - int iPtrmap; /* Pointer map page index */
1.457 - u8 *pPtrmap; /* Pointer map page data */
1.458 - int offset; /* Offset of entry in pointer map */
1.459 - int rc;
1.460 -
1.461 - assert( sqlite3_mutex_held(pBt->mutex) );
1.462 -
1.463 - iPtrmap = PTRMAP_PAGENO(pBt, key);
1.464 - rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
1.465 - if( rc!=0 ){
1.466 - return rc;
1.467 - }
1.468 - pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
1.469 -
1.470 - offset = PTRMAP_PTROFFSET(pBt, key);
1.471 - assert( pEType!=0 );
1.472 - *pEType = pPtrmap[offset];
1.473 - if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
1.474 -
1.475 - sqlite3PagerUnref(pDbPage);
1.476 - if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
1.477 - return SQLITE_OK;
1.478 -}
1.479 -
1.480 -#endif /* SQLITE_OMIT_AUTOVACUUM */
1.481 -
1.482 -/*
1.483 -** Given a btree page and a cell index (0 means the first cell on
1.484 -** the page, 1 means the second cell, and so forth) return a pointer
1.485 -** to the cell content.
1.486 -**
1.487 -** This routine works only for pages that do not contain overflow cells.
1.488 -*/
1.489 -#define findCell(pPage, iCell) \
1.490 - ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)]))
1.491 -#ifdef SQLITE_TEST
1.492 -u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){
1.493 - assert( iCell>=0 );
1.494 - assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) );
1.495 - return findCell(pPage, iCell);
1.496 -}
1.497 -#endif
1.498 -
1.499 -/*
1.500 -** This a more complex version of sqlite3BtreeFindCell() that works for
1.501 -** pages that do contain overflow cells. See insert
1.502 -*/
1.503 -static u8 *findOverflowCell(MemPage *pPage, int iCell){
1.504 - int i;
1.505 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.506 - for(i=pPage->nOverflow-1; i>=0; i--){
1.507 - int k;
1.508 - struct _OvflCell *pOvfl;
1.509 - pOvfl = &pPage->aOvfl[i];
1.510 - k = pOvfl->idx;
1.511 - if( k<=iCell ){
1.512 - if( k==iCell ){
1.513 - return pOvfl->pCell;
1.514 - }
1.515 - iCell--;
1.516 - }
1.517 - }
1.518 - return findCell(pPage, iCell);
1.519 -}
1.520 -
1.521 -/*
1.522 -** Parse a cell content block and fill in the CellInfo structure. There
1.523 -** are two versions of this function. sqlite3BtreeParseCell() takes a
1.524 -** cell index as the second argument and sqlite3BtreeParseCellPtr()
1.525 -** takes a pointer to the body of the cell as its second argument.
1.526 -**
1.527 -** Within this file, the parseCell() macro can be called instead of
1.528 -** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
1.529 -*/
1.530 -void sqlite3BtreeParseCellPtr(
1.531 - MemPage *pPage, /* Page containing the cell */
1.532 - u8 *pCell, /* Pointer to the cell text. */
1.533 - CellInfo *pInfo /* Fill in this structure */
1.534 -){
1.535 - int n; /* Number bytes in cell content header */
1.536 - u32 nPayload; /* Number of bytes of cell payload */
1.537 -
1.538 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.539 -
1.540 - pInfo->pCell = pCell;
1.541 - assert( pPage->leaf==0 || pPage->leaf==1 );
1.542 - n = pPage->childPtrSize;
1.543 - assert( n==4-4*pPage->leaf );
1.544 - if( pPage->hasData ){
1.545 - n += getVarint32(&pCell[n], &nPayload);
1.546 - }else{
1.547 - nPayload = 0;
1.548 - }
1.549 - pInfo->nData = nPayload;
1.550 - if( pPage->intKey ){
1.551 - n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
1.552 - }else{
1.553 - u32 x;
1.554 - n += getVarint32(&pCell[n], &x);
1.555 - pInfo->nKey = x;
1.556 - nPayload += x;
1.557 - }
1.558 - pInfo->nPayload = nPayload;
1.559 - pInfo->nHeader = n;
1.560 - if( nPayload<=pPage->maxLocal ){
1.561 - /* This is the (easy) common case where the entire payload fits
1.562 - ** on the local page. No overflow is required.
1.563 - */
1.564 - int nSize; /* Total size of cell content in bytes */
1.565 - pInfo->nLocal = nPayload;
1.566 - pInfo->iOverflow = 0;
1.567 - nSize = nPayload + n;
1.568 - if( nSize<4 ){
1.569 - nSize = 4; /* Minimum cell size is 4 */
1.570 - }
1.571 - pInfo->nSize = nSize;
1.572 - }else{
1.573 - /* If the payload will not fit completely on the local page, we have
1.574 - ** to decide how much to store locally and how much to spill onto
1.575 - ** overflow pages. The strategy is to minimize the amount of unused
1.576 - ** space on overflow pages while keeping the amount of local storage
1.577 - ** in between minLocal and maxLocal.
1.578 - **
1.579 - ** Warning: changing the way overflow payload is distributed in any
1.580 - ** way will result in an incompatible file format.
1.581 - */
1.582 - int minLocal; /* Minimum amount of payload held locally */
1.583 - int maxLocal; /* Maximum amount of payload held locally */
1.584 - int surplus; /* Overflow payload available for local storage */
1.585 -
1.586 - minLocal = pPage->minLocal;
1.587 - maxLocal = pPage->maxLocal;
1.588 - surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
1.589 - if( surplus <= maxLocal ){
1.590 - pInfo->nLocal = surplus;
1.591 - }else{
1.592 - pInfo->nLocal = minLocal;
1.593 - }
1.594 - pInfo->iOverflow = pInfo->nLocal + n;
1.595 - pInfo->nSize = pInfo->iOverflow + 4;
1.596 - }
1.597 -}
1.598 -#define parseCell(pPage, iCell, pInfo) \
1.599 - sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
1.600 -void sqlite3BtreeParseCell(
1.601 - MemPage *pPage, /* Page containing the cell */
1.602 - int iCell, /* The cell index. First cell is 0 */
1.603 - CellInfo *pInfo /* Fill in this structure */
1.604 -){
1.605 - parseCell(pPage, iCell, pInfo);
1.606 -}
1.607 -
1.608 -/*
1.609 -** Compute the total number of bytes that a Cell needs in the cell
1.610 -** data area of the btree-page. The return number includes the cell
1.611 -** data header and the local payload, but not any overflow page or
1.612 -** the space used by the cell pointer.
1.613 -*/
1.614 -#ifndef NDEBUG
1.615 -static int cellSize(MemPage *pPage, int iCell){
1.616 - CellInfo info;
1.617 - sqlite3BtreeParseCell(pPage, iCell, &info);
1.618 - return info.nSize;
1.619 -}
1.620 -#endif
1.621 -static int cellSizePtr(MemPage *pPage, u8 *pCell){
1.622 - CellInfo info;
1.623 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.624 - return info.nSize;
1.625 -}
1.626 -
1.627 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.628 -/*
1.629 -** If the cell pCell, part of page pPage contains a pointer
1.630 -** to an overflow page, insert an entry into the pointer-map
1.631 -** for the overflow page.
1.632 -*/
1.633 -static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
1.634 - if( pCell ){
1.635 - CellInfo info;
1.636 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.637 - assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
1.638 - if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
1.639 - Pgno ovfl = get4byte(&pCell[info.iOverflow]);
1.640 - return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
1.641 - }
1.642 - }
1.643 - return SQLITE_OK;
1.644 -}
1.645 -/*
1.646 -** If the cell with index iCell on page pPage contains a pointer
1.647 -** to an overflow page, insert an entry into the pointer-map
1.648 -** for the overflow page.
1.649 -*/
1.650 -static int ptrmapPutOvfl(MemPage *pPage, int iCell){
1.651 - u8 *pCell;
1.652 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.653 - pCell = findOverflowCell(pPage, iCell);
1.654 - return ptrmapPutOvflPtr(pPage, pCell);
1.655 -}
1.656 -#endif
1.657 -
1.658 -
1.659 -/*
1.660 -** Defragment the page given. All Cells are moved to the
1.661 -** end of the page and all free space is collected into one
1.662 -** big FreeBlk that occurs in between the header and cell
1.663 -** pointer array and the cell content area.
1.664 -*/
1.665 -static int defragmentPage(MemPage *pPage){
1.666 - int i; /* Loop counter */
1.667 - int pc; /* Address of a i-th cell */
1.668 - int addr; /* Offset of first byte after cell pointer array */
1.669 - int hdr; /* Offset to the page header */
1.670 - int size; /* Size of a cell */
1.671 - int usableSize; /* Number of usable bytes on a page */
1.672 - int cellOffset; /* Offset to the cell pointer array */
1.673 - int brk; /* Offset to the cell content area */
1.674 - int nCell; /* Number of cells on the page */
1.675 - unsigned char *data; /* The page data */
1.676 - unsigned char *temp; /* Temp area for cell content */
1.677 -
1.678 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.679 - assert( pPage->pBt!=0 );
1.680 - assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
1.681 - assert( pPage->nOverflow==0 );
1.682 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.683 - temp = sqlite3_malloc( pPage->pBt->pageSize );
1.684 - if( temp==0 ) return SQLITE_NOMEM;
1.685 - data = pPage->aData;
1.686 - hdr = pPage->hdrOffset;
1.687 - cellOffset = pPage->cellOffset;
1.688 - nCell = pPage->nCell;
1.689 - assert( nCell==get2byte(&data[hdr+3]) );
1.690 - usableSize = pPage->pBt->usableSize;
1.691 - brk = get2byte(&data[hdr+5]);
1.692 - memcpy(&temp[brk], &data[brk], usableSize - brk);
1.693 - brk = usableSize;
1.694 - for(i=0; i<nCell; i++){
1.695 - u8 *pAddr; /* The i-th cell pointer */
1.696 - pAddr = &data[cellOffset + i*2];
1.697 - pc = get2byte(pAddr);
1.698 - assert( pc<pPage->pBt->usableSize );
1.699 - size = cellSizePtr(pPage, &temp[pc]);
1.700 - brk -= size;
1.701 - memcpy(&data[brk], &temp[pc], size);
1.702 - put2byte(pAddr, brk);
1.703 - }
1.704 - assert( brk>=cellOffset+2*nCell );
1.705 - put2byte(&data[hdr+5], brk);
1.706 - data[hdr+1] = 0;
1.707 - data[hdr+2] = 0;
1.708 - data[hdr+7] = 0;
1.709 - addr = cellOffset+2*nCell;
1.710 - memset(&data[addr], 0, brk-addr);
1.711 - sqlite3_free(temp);
1.712 - return SQLITE_OK;
1.713 -}
1.714 -
1.715 -/*
1.716 -** Allocate nByte bytes of space on a page.
1.717 -**
1.718 -** Return the index into pPage->aData[] of the first byte of
1.719 -** the new allocation. Or return 0 if there is not enough free
1.720 -** space on the page to satisfy the allocation request.
1.721 -**
1.722 -** If the page contains nBytes of free space but does not contain
1.723 -** nBytes of contiguous free space, then this routine automatically
1.724 -** calls defragementPage() to consolidate all free space before
1.725 -** allocating the new chunk.
1.726 -*/
1.727 -static int allocateSpace(MemPage *pPage, int nByte){
1.728 - int addr, pc, hdr;
1.729 - int size;
1.730 - int nFrag;
1.731 - int top;
1.732 - int nCell;
1.733 - int cellOffset;
1.734 - unsigned char *data;
1.735 -
1.736 - data = pPage->aData;
1.737 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.738 - assert( pPage->pBt );
1.739 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.740 - if( nByte<4 ) nByte = 4;
1.741 - if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
1.742 - pPage->nFree -= nByte;
1.743 - hdr = pPage->hdrOffset;
1.744 -
1.745 - nFrag = data[hdr+7];
1.746 - if( nFrag<60 ){
1.747 - /* Search the freelist looking for a slot big enough to satisfy the
1.748 - ** space request. */
1.749 - addr = hdr+1;
1.750 - while( (pc = get2byte(&data[addr]))>0 ){
1.751 - size = get2byte(&data[pc+2]);
1.752 - if( size>=nByte ){
1.753 - if( size<nByte+4 ){
1.754 - memcpy(&data[addr], &data[pc], 2);
1.755 - data[hdr+7] = nFrag + size - nByte;
1.756 - return pc;
1.757 - }else{
1.758 - put2byte(&data[pc+2], size-nByte);
1.759 - return pc + size - nByte;
1.760 - }
1.761 - }
1.762 - addr = pc;
1.763 - }
1.764 - }
1.765 -
1.766 - /* Allocate memory from the gap in between the cell pointer array
1.767 - ** and the cell content area.
1.768 - */
1.769 - top = get2byte(&data[hdr+5]);
1.770 - nCell = get2byte(&data[hdr+3]);
1.771 - cellOffset = pPage->cellOffset;
1.772 - if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
1.773 - if( defragmentPage(pPage) ) return 0;
1.774 - top = get2byte(&data[hdr+5]);
1.775 - }
1.776 - top -= nByte;
1.777 - assert( cellOffset + 2*nCell <= top );
1.778 - put2byte(&data[hdr+5], top);
1.779 - return top;
1.780 -}
1.781 -
1.782 -/*
1.783 -** Return a section of the pPage->aData to the freelist.
1.784 -** The first byte of the new free block is pPage->aDisk[start]
1.785 -** and the size of the block is "size" bytes.
1.786 -**
1.787 -** Most of the effort here is involved in coalesing adjacent
1.788 -** free blocks into a single big free block.
1.789 -*/
1.790 -static void freeSpace(MemPage *pPage, int start, int size){
1.791 - int addr, pbegin, hdr;
1.792 - unsigned char *data = pPage->aData;
1.793 -
1.794 - assert( pPage->pBt!=0 );
1.795 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.796 - assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
1.797 - assert( (start + size)<=pPage->pBt->usableSize );
1.798 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.799 - if( size<4 ) size = 4;
1.800 -
1.801 -#ifdef SQLITE_SECURE_DELETE
1.802 - /* Overwrite deleted information with zeros when the SECURE_DELETE
1.803 - ** option is enabled at compile-time */
1.804 - memset(&data[start], 0, size);
1.805 -#endif
1.806 -
1.807 - /* Add the space back into the linked list of freeblocks */
1.808 - hdr = pPage->hdrOffset;
1.809 - addr = hdr + 1;
1.810 - while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
1.811 - assert( pbegin<=pPage->pBt->usableSize-4 );
1.812 - assert( pbegin>addr );
1.813 - addr = pbegin;
1.814 - }
1.815 - assert( pbegin<=pPage->pBt->usableSize-4 );
1.816 - assert( pbegin>addr || pbegin==0 );
1.817 - put2byte(&data[addr], start);
1.818 - put2byte(&data[start], pbegin);
1.819 - put2byte(&data[start+2], size);
1.820 - pPage->nFree += size;
1.821 -
1.822 - /* Coalesce adjacent free blocks */
1.823 - addr = pPage->hdrOffset + 1;
1.824 - while( (pbegin = get2byte(&data[addr]))>0 ){
1.825 - int pnext, psize;
1.826 - assert( pbegin>addr );
1.827 - assert( pbegin<=pPage->pBt->usableSize-4 );
1.828 - pnext = get2byte(&data[pbegin]);
1.829 - psize = get2byte(&data[pbegin+2]);
1.830 - if( pbegin + psize + 3 >= pnext && pnext>0 ){
1.831 - int frag = pnext - (pbegin+psize);
1.832 - assert( frag<=data[pPage->hdrOffset+7] );
1.833 - data[pPage->hdrOffset+7] -= frag;
1.834 - put2byte(&data[pbegin], get2byte(&data[pnext]));
1.835 - put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
1.836 - }else{
1.837 - addr = pbegin;
1.838 - }
1.839 - }
1.840 -
1.841 - /* If the cell content area begins with a freeblock, remove it. */
1.842 - if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
1.843 - int top;
1.844 - pbegin = get2byte(&data[hdr+1]);
1.845 - memcpy(&data[hdr+1], &data[pbegin], 2);
1.846 - top = get2byte(&data[hdr+5]);
1.847 - put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
1.848 - }
1.849 -}
1.850 -
1.851 -/*
1.852 -** Decode the flags byte (the first byte of the header) for a page
1.853 -** and initialize fields of the MemPage structure accordingly.
1.854 -*/
1.855 -static void decodeFlags(MemPage *pPage, int flagByte){
1.856 - BtShared *pBt; /* A copy of pPage->pBt */
1.857 -
1.858 - assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1.859 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.860 - pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
1.861 - pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
1.862 - pPage->leaf = (flagByte & PTF_LEAF)!=0;
1.863 - pPage->childPtrSize = 4*(pPage->leaf==0);
1.864 - pBt = pPage->pBt;
1.865 - if( flagByte & PTF_LEAFDATA ){
1.866 - pPage->leafData = 1;
1.867 - pPage->maxLocal = pBt->maxLeaf;
1.868 - pPage->minLocal = pBt->minLeaf;
1.869 - }else{
1.870 - pPage->leafData = 0;
1.871 - pPage->maxLocal = pBt->maxLocal;
1.872 - pPage->minLocal = pBt->minLocal;
1.873 - }
1.874 - pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
1.875 -}
1.876 -
1.877 -/*
1.878 -** Initialize the auxiliary information for a disk block.
1.879 -**
1.880 -** The pParent parameter must be a pointer to the MemPage which
1.881 -** is the parent of the page being initialized. The root of a
1.882 -** BTree has no parent and so for that page, pParent==NULL.
1.883 -**
1.884 -** Return SQLITE_OK on success. If we see that the page does
1.885 -** not contain a well-formed database page, then return
1.886 -** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1.887 -** guarantee that the page is well-formed. It only shows that
1.888 -** we failed to detect any corruption.
1.889 -*/
1.890 -int sqlite3BtreeInitPage(
1.891 - MemPage *pPage, /* The page to be initialized */
1.892 - MemPage *pParent /* The parent. Might be NULL */
1.893 -){
1.894 - int pc; /* Address of a freeblock within pPage->aData[] */
1.895 - int hdr; /* Offset to beginning of page header */
1.896 - u8 *data; /* Equal to pPage->aData */
1.897 - BtShared *pBt; /* The main btree structure */
1.898 - int usableSize; /* Amount of usable space on each page */
1.899 - int cellOffset; /* Offset from start of page to first cell pointer */
1.900 - int nFree; /* Number of unused bytes on the page */
1.901 - int top; /* First byte of the cell content area */
1.902 -
1.903 - pBt = pPage->pBt;
1.904 - assert( pBt!=0 );
1.905 - assert( pParent==0 || pParent->pBt==pBt );
1.906 - assert( sqlite3_mutex_held(pBt->mutex) );
1.907 - assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1.908 - assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
1.909 - assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
1.910 - if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
1.911 - /* The parent page should never change unless the file is corrupt */
1.912 - return SQLITE_CORRUPT_BKPT;
1.913 - }
1.914 - if( pPage->isInit ) return SQLITE_OK;
1.915 - if( pPage->pParent==0 && pParent!=0 ){
1.916 - pPage->pParent = pParent;
1.917 - sqlite3PagerRef(pParent->pDbPage);
1.918 - }
1.919 - hdr = pPage->hdrOffset;
1.920 - data = pPage->aData;
1.921 - decodeFlags(pPage, data[hdr]);
1.922 - pPage->nOverflow = 0;
1.923 - pPage->idxShift = 0;
1.924 - usableSize = pBt->usableSize;
1.925 - pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1.926 - top = get2byte(&data[hdr+5]);
1.927 - pPage->nCell = get2byte(&data[hdr+3]);
1.928 - if( pPage->nCell>MX_CELL(pBt) ){
1.929 - /* To many cells for a single page. The page must be corrupt */
1.930 - return SQLITE_CORRUPT_BKPT;
1.931 - }
1.932 - if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
1.933 - /* All pages must have at least one cell, except for root pages */
1.934 - return SQLITE_CORRUPT_BKPT;
1.935 - }
1.936 -
1.937 - /* Compute the total free space on the page */
1.938 - pc = get2byte(&data[hdr+1]);
1.939 - nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
1.940 - while( pc>0 ){
1.941 - int next, size;
1.942 - if( pc>usableSize-4 ){
1.943 - /* Free block is off the page */
1.944 - return SQLITE_CORRUPT_BKPT;
1.945 - }
1.946 - next = get2byte(&data[pc]);
1.947 - size = get2byte(&data[pc+2]);
1.948 - if( next>0 && next<=pc+size+3 ){
1.949 - /* Free blocks must be in accending order */
1.950 - return SQLITE_CORRUPT_BKPT;
1.951 - }
1.952 - nFree += size;
1.953 - pc = next;
1.954 - }
1.955 - pPage->nFree = nFree;
1.956 - if( nFree>=usableSize ){
1.957 - /* Free space cannot exceed total page size */
1.958 - return SQLITE_CORRUPT_BKPT;
1.959 - }
1.960 -
1.961 - pPage->isInit = 1;
1.962 - return SQLITE_OK;
1.963 -}
1.964 -
1.965 -/*
1.966 -** Set up a raw page so that it looks like a database page holding
1.967 -** no entries.
1.968 -*/
1.969 -static void zeroPage(MemPage *pPage, int flags){
1.970 - unsigned char *data = pPage->aData;
1.971 - BtShared *pBt = pPage->pBt;
1.972 - int hdr = pPage->hdrOffset;
1.973 - int first;
1.974 -
1.975 - assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
1.976 - assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1.977 - assert( sqlite3PagerGetData(pPage->pDbPage) == data );
1.978 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.979 - assert( sqlite3_mutex_held(pBt->mutex) );
1.980 - memset(&data[hdr], 0, pBt->usableSize - hdr);
1.981 - data[hdr] = flags;
1.982 - first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
1.983 - memset(&data[hdr+1], 0, 4);
1.984 - data[hdr+7] = 0;
1.985 - put2byte(&data[hdr+5], pBt->usableSize);
1.986 - pPage->nFree = pBt->usableSize - first;
1.987 - decodeFlags(pPage, flags);
1.988 - pPage->hdrOffset = hdr;
1.989 - pPage->cellOffset = first;
1.990 - pPage->nOverflow = 0;
1.991 - pPage->idxShift = 0;
1.992 - pPage->nCell = 0;
1.993 - pPage->isInit = 1;
1.994 -}
1.995 -
1.996 -/*
1.997 -** Get a page from the pager. Initialize the MemPage.pBt and
1.998 -** MemPage.aData elements if needed.
1.999 -**
1.1000 -** If the noContent flag is set, it means that we do not care about
1.1001 -** the content of the page at this time. So do not go to the disk
1.1002 -** to fetch the content. Just fill in the content with zeros for now.
1.1003 -** If in the future we call sqlite3PagerWrite() on this page, that
1.1004 -** means we have started to be concerned about content and the disk
1.1005 -** read should occur at that point.
1.1006 -*/
1.1007 -int sqlite3BtreeGetPage(
1.1008 - BtShared *pBt, /* The btree */
1.1009 - Pgno pgno, /* Number of the page to fetch */
1.1010 - MemPage **ppPage, /* Return the page in this parameter */
1.1011 - int noContent /* Do not load page content if true */
1.1012 -){
1.1013 - int rc;
1.1014 - MemPage *pPage;
1.1015 - DbPage *pDbPage;
1.1016 -
1.1017 - assert( sqlite3_mutex_held(pBt->mutex) );
1.1018 - rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
1.1019 - if( rc ) return rc;
1.1020 - pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1.1021 - pPage->aData = sqlite3PagerGetData(pDbPage);
1.1022 - pPage->pDbPage = pDbPage;
1.1023 - pPage->pBt = pBt;
1.1024 - pPage->pgno = pgno;
1.1025 - pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
1.1026 - *ppPage = pPage;
1.1027 - return SQLITE_OK;
1.1028 -}
1.1029 -
1.1030 -/*
1.1031 -** Get a page from the pager and initialize it. This routine
1.1032 -** is just a convenience wrapper around separate calls to
1.1033 -** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
1.1034 -*/
1.1035 -static int getAndInitPage(
1.1036 - BtShared *pBt, /* The database file */
1.1037 - Pgno pgno, /* Number of the page to get */
1.1038 - MemPage **ppPage, /* Write the page pointer here */
1.1039 - MemPage *pParent /* Parent of the page */
1.1040 -){
1.1041 - int rc;
1.1042 - assert( sqlite3_mutex_held(pBt->mutex) );
1.1043 - if( pgno==0 ){
1.1044 - return SQLITE_CORRUPT_BKPT;
1.1045 - }
1.1046 - rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
1.1047 - if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
1.1048 - rc = sqlite3BtreeInitPage(*ppPage, pParent);
1.1049 - }
1.1050 - return rc;
1.1051 -}
1.1052 -
1.1053 -/*
1.1054 -** Release a MemPage. This should be called once for each prior
1.1055 -** call to sqlite3BtreeGetPage.
1.1056 -*/
1.1057 -static void releasePage(MemPage *pPage){
1.1058 - if( pPage ){
1.1059 - assert( pPage->aData );
1.1060 - assert( pPage->pBt );
1.1061 - assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1.1062 - assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
1.1063 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.1064 - sqlite3PagerUnref(pPage->pDbPage);
1.1065 - }
1.1066 -}
1.1067 -
1.1068 -/*
1.1069 -** This routine is called when the reference count for a page
1.1070 -** reaches zero. We need to unref the pParent pointer when that
1.1071 -** happens.
1.1072 -*/
1.1073 -static void pageDestructor(DbPage *pData, int pageSize){
1.1074 - MemPage *pPage;
1.1075 - assert( (pageSize & 7)==0 );
1.1076 - pPage = (MemPage *)sqlite3PagerGetExtra(pData);
1.1077 - assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
1.1078 - if( pPage->pParent ){
1.1079 - MemPage *pParent = pPage->pParent;
1.1080 - assert( pParent->pBt==pPage->pBt );
1.1081 - pPage->pParent = 0;
1.1082 - releasePage(pParent);
1.1083 - }
1.1084 - pPage->isInit = 0;
1.1085 -}
1.1086 -
1.1087 -/*
1.1088 -** During a rollback, when the pager reloads information into the cache
1.1089 -** so that the cache is restored to its original state at the start of
1.1090 -** the transaction, for each page restored this routine is called.
1.1091 -**
1.1092 -** This routine needs to reset the extra data section at the end of the
1.1093 -** page to agree with the restored data.
1.1094 -*/
1.1095 -static void pageReinit(DbPage *pData, int pageSize){
1.1096 - MemPage *pPage;
1.1097 - assert( (pageSize & 7)==0 );
1.1098 - pPage = (MemPage *)sqlite3PagerGetExtra(pData);
1.1099 - if( pPage->isInit ){
1.1100 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.1101 - pPage->isInit = 0;
1.1102 - sqlite3BtreeInitPage(pPage, pPage->pParent);
1.1103 - }
1.1104 -}
1.1105 -
1.1106 -/*
1.1107 -** Open a database file.
1.1108 -**
1.1109 -** zFilename is the name of the database file. If zFilename is NULL
1.1110 -** a new database with a random name is created. This randomly named
1.1111 -** database file will be deleted when sqlite3BtreeClose() is called.
1.1112 -** If zFilename is ":memory:" then an in-memory database is created
1.1113 -** that is automatically destroyed when it is closed.
1.1114 -*/
1.1115 -int sqlite3BtreeOpen(
1.1116 - const char *zFilename, /* Name of the file containing the BTree database */
1.1117 - sqlite3 *pSqlite, /* Associated database handle */
1.1118 - Btree **ppBtree, /* Pointer to new Btree object written here */
1.1119 - int flags, /* Options */
1.1120 - int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
1.1121 -){
1.1122 - sqlite3_vfs *pVfs; /* The VFS to use for this btree */
1.1123 - BtShared *pBt = 0; /* Shared part of btree structure */
1.1124 - Btree *p; /* Handle to return */
1.1125 - int rc = SQLITE_OK;
1.1126 - int nReserve;
1.1127 - unsigned char zDbHeader[100];
1.1128 -
1.1129 - /* Set the variable isMemdb to true for an in-memory database, or
1.1130 - ** false for a file-based database. This symbol is only required if
1.1131 - ** either of the shared-data or autovacuum features are compiled
1.1132 - ** into the library.
1.1133 - */
1.1134 -#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1.1135 - #ifdef SQLITE_OMIT_MEMORYDB
1.1136 - const int isMemdb = 0;
1.1137 - #else
1.1138 - const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
1.1139 - #endif
1.1140 -#endif
1.1141 -
1.1142 - assert( pSqlite!=0 );
1.1143 - assert( sqlite3_mutex_held(pSqlite->mutex) );
1.1144 -
1.1145 - pVfs = pSqlite->pVfs;
1.1146 - p = sqlite3MallocZero(sizeof(Btree));
1.1147 - if( !p ){
1.1148 - return SQLITE_NOMEM;
1.1149 - }
1.1150 - p->inTrans = TRANS_NONE;
1.1151 - p->pSqlite = pSqlite;
1.1152 -
1.1153 -#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1.1154 - /*
1.1155 - ** If this Btree is a candidate for shared cache, try to find an
1.1156 - ** existing BtShared object that we can share with
1.1157 - */
1.1158 - if( (flags & BTREE_PRIVATE)==0
1.1159 - && isMemdb==0
1.1160 - && (pSqlite->flags & SQLITE_Vtab)==0
1.1161 - && zFilename && zFilename[0]
1.1162 - ){
1.1163 - if( sqlite3SharedCacheEnabled ){
1.1164 - int nFullPathname = pVfs->mxPathname+1;
1.1165 - char *zFullPathname = (char *)sqlite3_malloc(nFullPathname);
1.1166 - sqlite3_mutex *mutexShared;
1.1167 - p->sharable = 1;
1.1168 - if( pSqlite ){
1.1169 - pSqlite->flags |= SQLITE_SharedCache;
1.1170 - }
1.1171 - if( !zFullPathname ){
1.1172 - sqlite3_free(p);
1.1173 - return SQLITE_NOMEM;
1.1174 - }
1.1175 - sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
1.1176 - mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1.1177 - sqlite3_mutex_enter(mutexShared);
1.1178 - for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
1.1179 - assert( pBt->nRef>0 );
1.1180 - if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
1.1181 - && sqlite3PagerVfs(pBt->pPager)==pVfs ){
1.1182 - p->pBt = pBt;
1.1183 - pBt->nRef++;
1.1184 - break;
1.1185 - }
1.1186 - }
1.1187 - sqlite3_mutex_leave(mutexShared);
1.1188 - sqlite3_free(zFullPathname);
1.1189 - }
1.1190 -#ifdef SQLITE_DEBUG
1.1191 - else{
1.1192 - /* In debug mode, we mark all persistent databases as sharable
1.1193 - ** even when they are not. This exercises the locking code and
1.1194 - ** gives more opportunity for asserts(sqlite3_mutex_held())
1.1195 - ** statements to find locking problems.
1.1196 - */
1.1197 - p->sharable = 1;
1.1198 - }
1.1199 -#endif
1.1200 - }
1.1201 -#endif
1.1202 - if( pBt==0 ){
1.1203 - /*
1.1204 - ** The following asserts make sure that structures used by the btree are
1.1205 - ** the right size. This is to guard against size changes that result
1.1206 - ** when compiling on a different architecture.
1.1207 - */
1.1208 - assert( sizeof(i64)==8 || sizeof(i64)==4 );
1.1209 - assert( sizeof(u64)==8 || sizeof(u64)==4 );
1.1210 - assert( sizeof(u32)==4 );
1.1211 - assert( sizeof(u16)==2 );
1.1212 - assert( sizeof(Pgno)==4 );
1.1213 -
1.1214 - pBt = sqlite3MallocZero( sizeof(*pBt) );
1.1215 - if( pBt==0 ){
1.1216 - rc = SQLITE_NOMEM;
1.1217 - goto btree_open_out;
1.1218 - }
1.1219 - rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
1.1220 - EXTRA_SIZE, flags, vfsFlags);
1.1221 - if( rc==SQLITE_OK ){
1.1222 - rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
1.1223 - }
1.1224 - if( rc!=SQLITE_OK ){
1.1225 - goto btree_open_out;
1.1226 - }
1.1227 - p->pBt = pBt;
1.1228 -
1.1229 - sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
1.1230 - sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
1.1231 - pBt->pCursor = 0;
1.1232 - pBt->pPage1 = 0;
1.1233 - pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
1.1234 - pBt->pageSize = get2byte(&zDbHeader[16]);
1.1235 - if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1.1236 - || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
1.1237 - pBt->pageSize = 0;
1.1238 - sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1.1239 - pBt->maxEmbedFrac = 64; /* 25% */
1.1240 - pBt->minEmbedFrac = 32; /* 12.5% */
1.1241 - pBt->minLeafFrac = 32; /* 12.5% */
1.1242 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.1243 - /* If the magic name ":memory:" will create an in-memory database, then
1.1244 - ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1.1245 - ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1.1246 - ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1.1247 - ** regular file-name. In this case the auto-vacuum applies as per normal.
1.1248 - */
1.1249 - if( zFilename && !isMemdb ){
1.1250 - pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1.1251 - pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
1.1252 - }
1.1253 -#endif
1.1254 - nReserve = 0;
1.1255 - }else{
1.1256 - nReserve = zDbHeader[20];
1.1257 - pBt->maxEmbedFrac = zDbHeader[21];
1.1258 - pBt->minEmbedFrac = zDbHeader[22];
1.1259 - pBt->minLeafFrac = zDbHeader[23];
1.1260 - pBt->pageSizeFixed = 1;
1.1261 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.1262 - pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1.1263 - pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
1.1264 -#endif
1.1265 - }
1.1266 - pBt->usableSize = pBt->pageSize - nReserve;
1.1267 - assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
1.1268 - sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1.1269 -
1.1270 -#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1.1271 - /* Add the new BtShared object to the linked list sharable BtShareds.
1.1272 - */
1.1273 - if( p->sharable ){
1.1274 - sqlite3_mutex *mutexShared;
1.1275 - pBt->nRef = 1;
1.1276 - mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1.1277 - if( SQLITE_THREADSAFE ){
1.1278 - pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
1.1279 - if( pBt->mutex==0 ){
1.1280 - rc = SQLITE_NOMEM;
1.1281 - pSqlite->mallocFailed = 0;
1.1282 - goto btree_open_out;
1.1283 - }
1.1284 - }
1.1285 - sqlite3_mutex_enter(mutexShared);
1.1286 - pBt->pNext = sqlite3SharedCacheList;
1.1287 - sqlite3SharedCacheList = pBt;
1.1288 - sqlite3_mutex_leave(mutexShared);
1.1289 - }
1.1290 -#endif
1.1291 - }
1.1292 -
1.1293 -#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1.1294 - /* If the new Btree uses a sharable pBtShared, then link the new
1.1295 - ** Btree into the list of all sharable Btrees for the same connection.
1.1296 - ** The list is kept in ascending order by pBt address.
1.1297 - */
1.1298 - if( p->sharable ){
1.1299 - int i;
1.1300 - Btree *pSib;
1.1301 - for(i=0; i<pSqlite->nDb; i++){
1.1302 - if( (pSib = pSqlite->aDb[i].pBt)!=0 && pSib->sharable ){
1.1303 - while( pSib->pPrev ){ pSib = pSib->pPrev; }
1.1304 - if( p->pBt<pSib->pBt ){
1.1305 - p->pNext = pSib;
1.1306 - p->pPrev = 0;
1.1307 - pSib->pPrev = p;
1.1308 - }else{
1.1309 - while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
1.1310 - pSib = pSib->pNext;
1.1311 - }
1.1312 - p->pNext = pSib->pNext;
1.1313 - p->pPrev = pSib;
1.1314 - if( p->pNext ){
1.1315 - p->pNext->pPrev = p;
1.1316 - }
1.1317 - pSib->pNext = p;
1.1318 - }
1.1319 - break;
1.1320 - }
1.1321 - }
1.1322 - }
1.1323 -#endif
1.1324 - *ppBtree = p;
1.1325 -
1.1326 -btree_open_out:
1.1327 - if( rc!=SQLITE_OK ){
1.1328 - if( pBt && pBt->pPager ){
1.1329 - sqlite3PagerClose(pBt->pPager);
1.1330 - }
1.1331 - sqlite3_free(pBt);
1.1332 - sqlite3_free(p);
1.1333 - *ppBtree = 0;
1.1334 - }
1.1335 - return rc;
1.1336 -}
1.1337 -
1.1338 -/*
1.1339 -** Decrement the BtShared.nRef counter. When it reaches zero,
1.1340 -** remove the BtShared structure from the sharing list. Return
1.1341 -** true if the BtShared.nRef counter reaches zero and return
1.1342 -** false if it is still positive.
1.1343 -*/
1.1344 -static int removeFromSharingList(BtShared *pBt){
1.1345 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.1346 - sqlite3_mutex *pMaster;
1.1347 - BtShared *pList;
1.1348 - int removed = 0;
1.1349 -
1.1350 - assert( sqlite3_mutex_notheld(pBt->mutex) );
1.1351 - pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1.1352 - sqlite3_mutex_enter(pMaster);
1.1353 - pBt->nRef--;
1.1354 - if( pBt->nRef<=0 ){
1.1355 - if( sqlite3SharedCacheList==pBt ){
1.1356 - sqlite3SharedCacheList = pBt->pNext;
1.1357 - }else{
1.1358 - pList = sqlite3SharedCacheList;
1.1359 - while( pList && pList->pNext!=pBt ){
1.1360 - pList=pList->pNext;
1.1361 - }
1.1362 - if( pList ){
1.1363 - pList->pNext = pBt->pNext;
1.1364 - }
1.1365 - }
1.1366 - if( SQLITE_THREADSAFE ){
1.1367 - sqlite3_mutex_free(pBt->mutex);
1.1368 - }
1.1369 - removed = 1;
1.1370 - }
1.1371 - sqlite3_mutex_leave(pMaster);
1.1372 - return removed;
1.1373 -#else
1.1374 - return 1;
1.1375 -#endif
1.1376 -}
1.1377 -
1.1378 -/*
1.1379 -** Close an open database and invalidate all cursors.
1.1380 -*/
1.1381 -int sqlite3BtreeClose(Btree *p){
1.1382 - BtShared *pBt = p->pBt;
1.1383 - BtCursor *pCur;
1.1384 -
1.1385 - /* Close all cursors opened via this handle. */
1.1386 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.1387 - sqlite3BtreeEnter(p);
1.1388 - pCur = pBt->pCursor;
1.1389 - while( pCur ){
1.1390 - BtCursor *pTmp = pCur;
1.1391 - pCur = pCur->pNext;
1.1392 - if( pTmp->pBtree==p ){
1.1393 - sqlite3BtreeCloseCursor(pTmp);
1.1394 - }
1.1395 - }
1.1396 -
1.1397 - /* Rollback any active transaction and free the handle structure.
1.1398 - ** The call to sqlite3BtreeRollback() drops any table-locks held by
1.1399 - ** this handle.
1.1400 - */
1.1401 - sqlite3BtreeRollback(p);
1.1402 - sqlite3BtreeLeave(p);
1.1403 -
1.1404 - /* If there are still other outstanding references to the shared-btree
1.1405 - ** structure, return now. The remainder of this procedure cleans
1.1406 - ** up the shared-btree.
1.1407 - */
1.1408 - assert( p->wantToLock==0 && p->locked==0 );
1.1409 - if( !p->sharable || removeFromSharingList(pBt) ){
1.1410 - /* The pBt is no longer on the sharing list, so we can access
1.1411 - ** it without having to hold the mutex.
1.1412 - **
1.1413 - ** Clean out and delete the BtShared object.
1.1414 - */
1.1415 - assert( !pBt->pCursor );
1.1416 - sqlite3PagerClose(pBt->pPager);
1.1417 - if( pBt->xFreeSchema && pBt->pSchema ){
1.1418 - pBt->xFreeSchema(pBt->pSchema);
1.1419 - }
1.1420 - sqlite3_free(pBt->pSchema);
1.1421 - sqlite3_free(pBt);
1.1422 - }
1.1423 -
1.1424 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.1425 - assert( p->wantToLock==0 );
1.1426 - assert( p->locked==0 );
1.1427 - if( p->pPrev ) p->pPrev->pNext = p->pNext;
1.1428 - if( p->pNext ) p->pNext->pPrev = p->pPrev;
1.1429 -#endif
1.1430 -
1.1431 - sqlite3_free(p);
1.1432 - return SQLITE_OK;
1.1433 -}
1.1434 -
1.1435 -/*
1.1436 -** Change the busy handler callback function.
1.1437 -*/
1.1438 -int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
1.1439 - BtShared *pBt = p->pBt;
1.1440 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.1441 - sqlite3BtreeEnter(p);
1.1442 - pBt->pBusyHandler = pHandler;
1.1443 - sqlite3PagerSetBusyhandler(pBt->pPager, pHandler);
1.1444 - sqlite3BtreeLeave(p);
1.1445 - return SQLITE_OK;
1.1446 -}
1.1447 -
1.1448 -/*
1.1449 -** Change the limit on the number of pages allowed in the cache.
1.1450 -**
1.1451 -** The maximum number of cache pages is set to the absolute
1.1452 -** value of mxPage. If mxPage is negative, the pager will
1.1453 -** operate asynchronously - it will not stop to do fsync()s
1.1454 -** to insure data is written to the disk surface before
1.1455 -** continuing. Transactions still work if synchronous is off,
1.1456 -** and the database cannot be corrupted if this program
1.1457 -** crashes. But if the operating system crashes or there is
1.1458 -** an abrupt power failure when synchronous is off, the database
1.1459 -** could be left in an inconsistent and unrecoverable state.
1.1460 -** Synchronous is on by default so database corruption is not
1.1461 -** normally a worry.
1.1462 -*/
1.1463 -int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1.1464 - BtShared *pBt = p->pBt;
1.1465 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.1466 - sqlite3BtreeEnter(p);
1.1467 - sqlite3PagerSetCachesize(pBt->pPager, mxPage);
1.1468 - sqlite3BtreeLeave(p);
1.1469 - return SQLITE_OK;
1.1470 -}
1.1471 -
1.1472 -/*
1.1473 -** Change the way data is synced to disk in order to increase or decrease
1.1474 -** how well the database resists damage due to OS crashes and power
1.1475 -** failures. Level 1 is the same as asynchronous (no syncs() occur and
1.1476 -** there is a high probability of damage) Level 2 is the default. There
1.1477 -** is a very low but non-zero probability of damage. Level 3 reduces the
1.1478 -** probability of damage to near zero but with a write performance reduction.
1.1479 -*/
1.1480 -#ifndef SQLITE_OMIT_PAGER_PRAGMAS
1.1481 -int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
1.1482 - BtShared *pBt = p->pBt;
1.1483 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.1484 - sqlite3BtreeEnter(p);
1.1485 - sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
1.1486 - sqlite3BtreeLeave(p);
1.1487 - return SQLITE_OK;
1.1488 -}
1.1489 -#endif
1.1490 -
1.1491 -/*
1.1492 -** Return TRUE if the given btree is set to safety level 1. In other
1.1493 -** words, return TRUE if no sync() occurs on the disk files.
1.1494 -*/
1.1495 -int sqlite3BtreeSyncDisabled(Btree *p){
1.1496 - BtShared *pBt = p->pBt;
1.1497 - int rc;
1.1498 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.1499 - sqlite3BtreeEnter(p);
1.1500 - assert( pBt && pBt->pPager );
1.1501 - rc = sqlite3PagerNosync(pBt->pPager);
1.1502 - sqlite3BtreeLeave(p);
1.1503 - return rc;
1.1504 -}
1.1505 -
1.1506 -#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
1.1507 -/*
1.1508 -** Change the default pages size and the number of reserved bytes per page.
1.1509 -**
1.1510 -** The page size must be a power of 2 between 512 and 65536. If the page
1.1511 -** size supplied does not meet this constraint then the page size is not
1.1512 -** changed.
1.1513 -**
1.1514 -** Page sizes are constrained to be a power of two so that the region
1.1515 -** of the database file used for locking (beginning at PENDING_BYTE,
1.1516 -** the first byte past the 1GB boundary, 0x40000000) needs to occur
1.1517 -** at the beginning of a page.
1.1518 -**
1.1519 -** If parameter nReserve is less than zero, then the number of reserved
1.1520 -** bytes per page is left unchanged.
1.1521 -*/
1.1522 -int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
1.1523 - int rc = SQLITE_OK;
1.1524 - BtShared *pBt = p->pBt;
1.1525 - sqlite3BtreeEnter(p);
1.1526 - if( pBt->pageSizeFixed ){
1.1527 - sqlite3BtreeLeave(p);
1.1528 - return SQLITE_READONLY;
1.1529 - }
1.1530 - if( nReserve<0 ){
1.1531 - nReserve = pBt->pageSize - pBt->usableSize;
1.1532 - }
1.1533 - if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1.1534 - ((pageSize-1)&pageSize)==0 ){
1.1535 - assert( (pageSize & 7)==0 );
1.1536 - assert( !pBt->pPage1 && !pBt->pCursor );
1.1537 - pBt->pageSize = pageSize;
1.1538 - rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1.1539 - }
1.1540 - pBt->usableSize = pBt->pageSize - nReserve;
1.1541 - sqlite3BtreeLeave(p);
1.1542 - return rc;
1.1543 -}
1.1544 -
1.1545 -/*
1.1546 -** Return the currently defined page size
1.1547 -*/
1.1548 -int sqlite3BtreeGetPageSize(Btree *p){
1.1549 - return p->pBt->pageSize;
1.1550 -}
1.1551 -int sqlite3BtreeGetReserve(Btree *p){
1.1552 - int n;
1.1553 - sqlite3BtreeEnter(p);
1.1554 - n = p->pBt->pageSize - p->pBt->usableSize;
1.1555 - sqlite3BtreeLeave(p);
1.1556 - return n;
1.1557 -}
1.1558 -
1.1559 -/*
1.1560 -** Set the maximum page count for a database if mxPage is positive.
1.1561 -** No changes are made if mxPage is 0 or negative.
1.1562 -** Regardless of the value of mxPage, return the maximum page count.
1.1563 -*/
1.1564 -int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
1.1565 - int n;
1.1566 - sqlite3BtreeEnter(p);
1.1567 - n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
1.1568 - sqlite3BtreeLeave(p);
1.1569 - return n;
1.1570 -}
1.1571 -#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
1.1572 -
1.1573 -/*
1.1574 -** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1.1575 -** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1.1576 -** is disabled. The default value for the auto-vacuum property is
1.1577 -** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1.1578 -*/
1.1579 -int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
1.1580 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.1581 - return SQLITE_READONLY;
1.1582 -#else
1.1583 - BtShared *pBt = p->pBt;
1.1584 - int rc = SQLITE_OK;
1.1585 - int av = (autoVacuum?1:0);
1.1586 -
1.1587 - sqlite3BtreeEnter(p);
1.1588 - if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
1.1589 - rc = SQLITE_READONLY;
1.1590 - }else{
1.1591 - pBt->autoVacuum = av;
1.1592 - }
1.1593 - sqlite3BtreeLeave(p);
1.1594 - return rc;
1.1595 -#endif
1.1596 -}
1.1597 -
1.1598 -/*
1.1599 -** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1.1600 -** enabled 1 is returned. Otherwise 0.
1.1601 -*/
1.1602 -int sqlite3BtreeGetAutoVacuum(Btree *p){
1.1603 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.1604 - return BTREE_AUTOVACUUM_NONE;
1.1605 -#else
1.1606 - int rc;
1.1607 - sqlite3BtreeEnter(p);
1.1608 - rc = (
1.1609 - (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1.1610 - (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1.1611 - BTREE_AUTOVACUUM_INCR
1.1612 - );
1.1613 - sqlite3BtreeLeave(p);
1.1614 - return rc;
1.1615 -#endif
1.1616 -}
1.1617 -
1.1618 -
1.1619 -/*
1.1620 -** Get a reference to pPage1 of the database file. This will
1.1621 -** also acquire a readlock on that file.
1.1622 -**
1.1623 -** SQLITE_OK is returned on success. If the file is not a
1.1624 -** well-formed database file, then SQLITE_CORRUPT is returned.
1.1625 -** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
1.1626 -** is returned if we run out of memory.
1.1627 -*/
1.1628 -static int lockBtree(BtShared *pBt){
1.1629 - int rc, pageSize;
1.1630 - MemPage *pPage1;
1.1631 -
1.1632 - assert( sqlite3_mutex_held(pBt->mutex) );
1.1633 - if( pBt->pPage1 ) return SQLITE_OK;
1.1634 - rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
1.1635 - if( rc!=SQLITE_OK ) return rc;
1.1636 -
1.1637 -
1.1638 - /* Do some checking to help insure the file we opened really is
1.1639 - ** a valid database file.
1.1640 - */
1.1641 - rc = SQLITE_NOTADB;
1.1642 - if( sqlite3PagerPagecount(pBt->pPager)>0 ){
1.1643 - u8 *page1 = pPage1->aData;
1.1644 - if( memcmp(page1, zMagicHeader, 16)!=0 ){
1.1645 - goto page1_init_failed;
1.1646 - }
1.1647 - if( page1[18]>1 ){
1.1648 - pBt->readOnly = 1;
1.1649 - }
1.1650 - if( page1[19]>1 ){
1.1651 - goto page1_init_failed;
1.1652 - }
1.1653 - pageSize = get2byte(&page1[16]);
1.1654 - if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
1.1655 - (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
1.1656 - ){
1.1657 - goto page1_init_failed;
1.1658 - }
1.1659 - assert( (pageSize & 7)==0 );
1.1660 - pBt->pageSize = pageSize;
1.1661 - pBt->usableSize = pageSize - page1[20];
1.1662 - if( pBt->usableSize<500 ){
1.1663 - goto page1_init_failed;
1.1664 - }
1.1665 - pBt->maxEmbedFrac = page1[21];
1.1666 - pBt->minEmbedFrac = page1[22];
1.1667 - pBt->minLeafFrac = page1[23];
1.1668 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.1669 - pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
1.1670 - pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
1.1671 -#endif
1.1672 - }
1.1673 -
1.1674 - /* maxLocal is the maximum amount of payload to store locally for
1.1675 - ** a cell. Make sure it is small enough so that at least minFanout
1.1676 - ** cells can will fit on one page. We assume a 10-byte page header.
1.1677 - ** Besides the payload, the cell must store:
1.1678 - ** 2-byte pointer to the cell
1.1679 - ** 4-byte child pointer
1.1680 - ** 9-byte nKey value
1.1681 - ** 4-byte nData value
1.1682 - ** 4-byte overflow page pointer
1.1683 - ** So a cell consists of a 2-byte poiner, a header which is as much as
1.1684 - ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1.1685 - ** page pointer.
1.1686 - */
1.1687 - pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1.1688 - pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1.1689 - pBt->maxLeaf = pBt->usableSize - 35;
1.1690 - pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
1.1691 - if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1.1692 - goto page1_init_failed;
1.1693 - }
1.1694 - assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
1.1695 - pBt->pPage1 = pPage1;
1.1696 - return SQLITE_OK;
1.1697 -
1.1698 -page1_init_failed:
1.1699 - releasePage(pPage1);
1.1700 - pBt->pPage1 = 0;
1.1701 - return rc;
1.1702 -}
1.1703 -
1.1704 -/*
1.1705 -** This routine works like lockBtree() except that it also invokes the
1.1706 -** busy callback if there is lock contention.
1.1707 -*/
1.1708 -static int lockBtreeWithRetry(Btree *pRef){
1.1709 - int rc = SQLITE_OK;
1.1710 -
1.1711 - assert( sqlite3BtreeHoldsMutex(pRef) );
1.1712 - if( pRef->inTrans==TRANS_NONE ){
1.1713 - u8 inTransaction = pRef->pBt->inTransaction;
1.1714 - btreeIntegrity(pRef);
1.1715 - rc = sqlite3BtreeBeginTrans(pRef, 0);
1.1716 - pRef->pBt->inTransaction = inTransaction;
1.1717 - pRef->inTrans = TRANS_NONE;
1.1718 - if( rc==SQLITE_OK ){
1.1719 - pRef->pBt->nTransaction--;
1.1720 - }
1.1721 - btreeIntegrity(pRef);
1.1722 - }
1.1723 - return rc;
1.1724 -}
1.1725 -
1.1726 -
1.1727 -/*
1.1728 -** If there are no outstanding cursors and we are not in the middle
1.1729 -** of a transaction but there is a read lock on the database, then
1.1730 -** this routine unrefs the first page of the database file which
1.1731 -** has the effect of releasing the read lock.
1.1732 -**
1.1733 -** If there are any outstanding cursors, this routine is a no-op.
1.1734 -**
1.1735 -** If there is a transaction in progress, this routine is a no-op.
1.1736 -*/
1.1737 -static void unlockBtreeIfUnused(BtShared *pBt){
1.1738 - assert( sqlite3_mutex_held(pBt->mutex) );
1.1739 - if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
1.1740 - if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
1.1741 - if( pBt->pPage1->aData==0 ){
1.1742 - MemPage *pPage = pBt->pPage1;
1.1743 - pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
1.1744 - pPage->pBt = pBt;
1.1745 - pPage->pgno = 1;
1.1746 - }
1.1747 - releasePage(pBt->pPage1);
1.1748 - }
1.1749 - pBt->pPage1 = 0;
1.1750 - pBt->inStmt = 0;
1.1751 - }
1.1752 -}
1.1753 -
1.1754 -/*
1.1755 -** Create a new database by initializing the first page of the
1.1756 -** file.
1.1757 -*/
1.1758 -static int newDatabase(BtShared *pBt){
1.1759 - MemPage *pP1;
1.1760 - unsigned char *data;
1.1761 - int rc;
1.1762 -
1.1763 - assert( sqlite3_mutex_held(pBt->mutex) );
1.1764 - if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
1.1765 - pP1 = pBt->pPage1;
1.1766 - assert( pP1!=0 );
1.1767 - data = pP1->aData;
1.1768 - rc = sqlite3PagerWrite(pP1->pDbPage);
1.1769 - if( rc ) return rc;
1.1770 - memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1.1771 - assert( sizeof(zMagicHeader)==16 );
1.1772 - put2byte(&data[16], pBt->pageSize);
1.1773 - data[18] = 1;
1.1774 - data[19] = 1;
1.1775 - data[20] = pBt->pageSize - pBt->usableSize;
1.1776 - data[21] = pBt->maxEmbedFrac;
1.1777 - data[22] = pBt->minEmbedFrac;
1.1778 - data[23] = pBt->minLeafFrac;
1.1779 - memset(&data[24], 0, 100-24);
1.1780 - zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
1.1781 - pBt->pageSizeFixed = 1;
1.1782 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.1783 - assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
1.1784 - assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
1.1785 - put4byte(&data[36 + 4*4], pBt->autoVacuum);
1.1786 - put4byte(&data[36 + 7*4], pBt->incrVacuum);
1.1787 -#endif
1.1788 - return SQLITE_OK;
1.1789 -}
1.1790 -
1.1791 -/*
1.1792 -** Attempt to start a new transaction. A write-transaction
1.1793 -** is started if the second argument is nonzero, otherwise a read-
1.1794 -** transaction. If the second argument is 2 or more and exclusive
1.1795 -** transaction is started, meaning that no other process is allowed
1.1796 -** to access the database. A preexisting transaction may not be
1.1797 -** upgraded to exclusive by calling this routine a second time - the
1.1798 -** exclusivity flag only works for a new transaction.
1.1799 -**
1.1800 -** A write-transaction must be started before attempting any
1.1801 -** changes to the database. None of the following routines
1.1802 -** will work unless a transaction is started first:
1.1803 -**
1.1804 -** sqlite3BtreeCreateTable()
1.1805 -** sqlite3BtreeCreateIndex()
1.1806 -** sqlite3BtreeClearTable()
1.1807 -** sqlite3BtreeDropTable()
1.1808 -** sqlite3BtreeInsert()
1.1809 -** sqlite3BtreeDelete()
1.1810 -** sqlite3BtreeUpdateMeta()
1.1811 -**
1.1812 -** If an initial attempt to acquire the lock fails because of lock contention
1.1813 -** and the database was previously unlocked, then invoke the busy handler
1.1814 -** if there is one. But if there was previously a read-lock, do not
1.1815 -** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
1.1816 -** returned when there is already a read-lock in order to avoid a deadlock.
1.1817 -**
1.1818 -** Suppose there are two processes A and B. A has a read lock and B has
1.1819 -** a reserved lock. B tries to promote to exclusive but is blocked because
1.1820 -** of A's read lock. A tries to promote to reserved but is blocked by B.
1.1821 -** One or the other of the two processes must give way or there can be
1.1822 -** no progress. By returning SQLITE_BUSY and not invoking the busy callback
1.1823 -** when A already has a read lock, we encourage A to give up and let B
1.1824 -** proceed.
1.1825 -*/
1.1826 -int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
1.1827 - BtShared *pBt = p->pBt;
1.1828 - int rc = SQLITE_OK;
1.1829 -
1.1830 - sqlite3BtreeEnter(p);
1.1831 - btreeIntegrity(p);
1.1832 -
1.1833 - /* If the btree is already in a write-transaction, or it
1.1834 - ** is already in a read-transaction and a read-transaction
1.1835 - ** is requested, this is a no-op.
1.1836 - */
1.1837 - if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
1.1838 - goto trans_begun;
1.1839 - }
1.1840 -
1.1841 - /* Write transactions are not possible on a read-only database */
1.1842 - if( pBt->readOnly && wrflag ){
1.1843 - rc = SQLITE_READONLY;
1.1844 - goto trans_begun;
1.1845 - }
1.1846 -
1.1847 - /* If another database handle has already opened a write transaction
1.1848 - ** on this shared-btree structure and a second write transaction is
1.1849 - ** requested, return SQLITE_BUSY.
1.1850 - */
1.1851 - if( pBt->inTransaction==TRANS_WRITE && wrflag ){
1.1852 - rc = SQLITE_BUSY;
1.1853 - goto trans_begun;
1.1854 - }
1.1855 -
1.1856 - do {
1.1857 - if( pBt->pPage1==0 ){
1.1858 - rc = lockBtree(pBt);
1.1859 - }
1.1860 -
1.1861 - if( rc==SQLITE_OK && wrflag ){
1.1862 - if( pBt->readOnly ){
1.1863 - rc = SQLITE_READONLY;
1.1864 - }else{
1.1865 - rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
1.1866 - if( rc==SQLITE_OK ){
1.1867 - rc = newDatabase(pBt);
1.1868 - }
1.1869 - }
1.1870 - }
1.1871 -
1.1872 - if( rc==SQLITE_OK ){
1.1873 - if( wrflag ) pBt->inStmt = 0;
1.1874 - }else{
1.1875 - unlockBtreeIfUnused(pBt);
1.1876 - }
1.1877 - }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
1.1878 - sqlite3InvokeBusyHandler(pBt->pBusyHandler) );
1.1879 -
1.1880 - if( rc==SQLITE_OK ){
1.1881 - if( p->inTrans==TRANS_NONE ){
1.1882 - pBt->nTransaction++;
1.1883 - }
1.1884 - p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
1.1885 - if( p->inTrans>pBt->inTransaction ){
1.1886 - pBt->inTransaction = p->inTrans;
1.1887 - }
1.1888 - }
1.1889 -
1.1890 -
1.1891 -trans_begun:
1.1892 - btreeIntegrity(p);
1.1893 - sqlite3BtreeLeave(p);
1.1894 - return rc;
1.1895 -}
1.1896 -
1.1897 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.1898 -
1.1899 -/*
1.1900 -** Set the pointer-map entries for all children of page pPage. Also, if
1.1901 -** pPage contains cells that point to overflow pages, set the pointer
1.1902 -** map entries for the overflow pages as well.
1.1903 -*/
1.1904 -static int setChildPtrmaps(MemPage *pPage){
1.1905 - int i; /* Counter variable */
1.1906 - int nCell; /* Number of cells in page pPage */
1.1907 - int rc; /* Return code */
1.1908 - BtShared *pBt = pPage->pBt;
1.1909 - int isInitOrig = pPage->isInit;
1.1910 - Pgno pgno = pPage->pgno;
1.1911 -
1.1912 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.1913 - rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
1.1914 - if( rc!=SQLITE_OK ){
1.1915 - goto set_child_ptrmaps_out;
1.1916 - }
1.1917 - nCell = pPage->nCell;
1.1918 -
1.1919 - for(i=0; i<nCell; i++){
1.1920 - u8 *pCell = findCell(pPage, i);
1.1921 -
1.1922 - rc = ptrmapPutOvflPtr(pPage, pCell);
1.1923 - if( rc!=SQLITE_OK ){
1.1924 - goto set_child_ptrmaps_out;
1.1925 - }
1.1926 -
1.1927 - if( !pPage->leaf ){
1.1928 - Pgno childPgno = get4byte(pCell);
1.1929 - rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1.1930 - if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1.1931 - }
1.1932 - }
1.1933 -
1.1934 - if( !pPage->leaf ){
1.1935 - Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1.1936 - rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1.1937 - }
1.1938 -
1.1939 -set_child_ptrmaps_out:
1.1940 - pPage->isInit = isInitOrig;
1.1941 - return rc;
1.1942 -}
1.1943 -
1.1944 -/*
1.1945 -** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
1.1946 -** page, is a pointer to page iFrom. Modify this pointer so that it points to
1.1947 -** iTo. Parameter eType describes the type of pointer to be modified, as
1.1948 -** follows:
1.1949 -**
1.1950 -** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
1.1951 -** page of pPage.
1.1952 -**
1.1953 -** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
1.1954 -** page pointed to by one of the cells on pPage.
1.1955 -**
1.1956 -** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
1.1957 -** overflow page in the list.
1.1958 -*/
1.1959 -static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
1.1960 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.1961 - if( eType==PTRMAP_OVERFLOW2 ){
1.1962 - /* The pointer is always the first 4 bytes of the page in this case. */
1.1963 - if( get4byte(pPage->aData)!=iFrom ){
1.1964 - return SQLITE_CORRUPT_BKPT;
1.1965 - }
1.1966 - put4byte(pPage->aData, iTo);
1.1967 - }else{
1.1968 - int isInitOrig = pPage->isInit;
1.1969 - int i;
1.1970 - int nCell;
1.1971 -
1.1972 - sqlite3BtreeInitPage(pPage, 0);
1.1973 - nCell = pPage->nCell;
1.1974 -
1.1975 - for(i=0; i<nCell; i++){
1.1976 - u8 *pCell = findCell(pPage, i);
1.1977 - if( eType==PTRMAP_OVERFLOW1 ){
1.1978 - CellInfo info;
1.1979 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.1980 - if( info.iOverflow ){
1.1981 - if( iFrom==get4byte(&pCell[info.iOverflow]) ){
1.1982 - put4byte(&pCell[info.iOverflow], iTo);
1.1983 - break;
1.1984 - }
1.1985 - }
1.1986 - }else{
1.1987 - if( get4byte(pCell)==iFrom ){
1.1988 - put4byte(pCell, iTo);
1.1989 - break;
1.1990 - }
1.1991 - }
1.1992 - }
1.1993 -
1.1994 - if( i==nCell ){
1.1995 - if( eType!=PTRMAP_BTREE ||
1.1996 - get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
1.1997 - return SQLITE_CORRUPT_BKPT;
1.1998 - }
1.1999 - put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
1.2000 - }
1.2001 -
1.2002 - pPage->isInit = isInitOrig;
1.2003 - }
1.2004 - return SQLITE_OK;
1.2005 -}
1.2006 -
1.2007 -
1.2008 -/*
1.2009 -** Move the open database page pDbPage to location iFreePage in the
1.2010 -** database. The pDbPage reference remains valid.
1.2011 -*/
1.2012 -static int relocatePage(
1.2013 - BtShared *pBt, /* Btree */
1.2014 - MemPage *pDbPage, /* Open page to move */
1.2015 - u8 eType, /* Pointer map 'type' entry for pDbPage */
1.2016 - Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
1.2017 - Pgno iFreePage /* The location to move pDbPage to */
1.2018 -){
1.2019 - MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
1.2020 - Pgno iDbPage = pDbPage->pgno;
1.2021 - Pager *pPager = pBt->pPager;
1.2022 - int rc;
1.2023 -
1.2024 - assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
1.2025 - eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
1.2026 - assert( sqlite3_mutex_held(pBt->mutex) );
1.2027 - assert( pDbPage->pBt==pBt );
1.2028 -
1.2029 - /* Move page iDbPage from it's current location to page number iFreePage */
1.2030 - TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
1.2031 - iDbPage, iFreePage, iPtrPage, eType));
1.2032 - rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
1.2033 - if( rc!=SQLITE_OK ){
1.2034 - return rc;
1.2035 - }
1.2036 - pDbPage->pgno = iFreePage;
1.2037 -
1.2038 - /* If pDbPage was a btree-page, then it may have child pages and/or cells
1.2039 - ** that point to overflow pages. The pointer map entries for all these
1.2040 - ** pages need to be changed.
1.2041 - **
1.2042 - ** If pDbPage is an overflow page, then the first 4 bytes may store a
1.2043 - ** pointer to a subsequent overflow page. If this is the case, then
1.2044 - ** the pointer map needs to be updated for the subsequent overflow page.
1.2045 - */
1.2046 - if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
1.2047 - rc = setChildPtrmaps(pDbPage);
1.2048 - if( rc!=SQLITE_OK ){
1.2049 - return rc;
1.2050 - }
1.2051 - }else{
1.2052 - Pgno nextOvfl = get4byte(pDbPage->aData);
1.2053 - if( nextOvfl!=0 ){
1.2054 - rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
1.2055 - if( rc!=SQLITE_OK ){
1.2056 - return rc;
1.2057 - }
1.2058 - }
1.2059 - }
1.2060 -
1.2061 - /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
1.2062 - ** that it points at iFreePage. Also fix the pointer map entry for
1.2063 - ** iPtrPage.
1.2064 - */
1.2065 - if( eType!=PTRMAP_ROOTPAGE ){
1.2066 - rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
1.2067 - if( rc!=SQLITE_OK ){
1.2068 - return rc;
1.2069 - }
1.2070 - rc = sqlite3PagerWrite(pPtrPage->pDbPage);
1.2071 - if( rc!=SQLITE_OK ){
1.2072 - releasePage(pPtrPage);
1.2073 - return rc;
1.2074 - }
1.2075 - rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
1.2076 - releasePage(pPtrPage);
1.2077 - if( rc==SQLITE_OK ){
1.2078 - rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
1.2079 - }
1.2080 - }
1.2081 - return rc;
1.2082 -}
1.2083 -
1.2084 -/* Forward declaration required by incrVacuumStep(). */
1.2085 -static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
1.2086 -
1.2087 -/*
1.2088 -** Perform a single step of an incremental-vacuum. If successful,
1.2089 -** return SQLITE_OK. If there is no work to do (and therefore no
1.2090 -** point in calling this function again), return SQLITE_DONE.
1.2091 -**
1.2092 -** More specificly, this function attempts to re-organize the
1.2093 -** database so that the last page of the file currently in use
1.2094 -** is no longer in use.
1.2095 -**
1.2096 -** If the nFin parameter is non-zero, the implementation assumes
1.2097 -** that the caller will keep calling incrVacuumStep() until
1.2098 -** it returns SQLITE_DONE or an error, and that nFin is the
1.2099 -** number of pages the database file will contain after this
1.2100 -** process is complete.
1.2101 -*/
1.2102 -static int incrVacuumStep(BtShared *pBt, Pgno nFin){
1.2103 - Pgno iLastPg; /* Last page in the database */
1.2104 - Pgno nFreeList; /* Number of pages still on the free-list */
1.2105 -
1.2106 - assert( sqlite3_mutex_held(pBt->mutex) );
1.2107 - iLastPg = pBt->nTrunc;
1.2108 - if( iLastPg==0 ){
1.2109 - iLastPg = sqlite3PagerPagecount(pBt->pPager);
1.2110 - }
1.2111 -
1.2112 - if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
1.2113 - int rc;
1.2114 - u8 eType;
1.2115 - Pgno iPtrPage;
1.2116 -
1.2117 - nFreeList = get4byte(&pBt->pPage1->aData[36]);
1.2118 - if( nFreeList==0 || nFin==iLastPg ){
1.2119 - return SQLITE_DONE;
1.2120 - }
1.2121 -
1.2122 - rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
1.2123 - if( rc!=SQLITE_OK ){
1.2124 - return rc;
1.2125 - }
1.2126 - if( eType==PTRMAP_ROOTPAGE ){
1.2127 - return SQLITE_CORRUPT_BKPT;
1.2128 - }
1.2129 -
1.2130 - if( eType==PTRMAP_FREEPAGE ){
1.2131 - if( nFin==0 ){
1.2132 - /* Remove the page from the files free-list. This is not required
1.2133 - ** if nFin is non-zero. In that case, the free-list will be
1.2134 - ** truncated to zero after this function returns, so it doesn't
1.2135 - ** matter if it still contains some garbage entries.
1.2136 - */
1.2137 - Pgno iFreePg;
1.2138 - MemPage *pFreePg;
1.2139 - rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
1.2140 - if( rc!=SQLITE_OK ){
1.2141 - return rc;
1.2142 - }
1.2143 - assert( iFreePg==iLastPg );
1.2144 - releasePage(pFreePg);
1.2145 - }
1.2146 - } else {
1.2147 - Pgno iFreePg; /* Index of free page to move pLastPg to */
1.2148 - MemPage *pLastPg;
1.2149 -
1.2150 - rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
1.2151 - if( rc!=SQLITE_OK ){
1.2152 - return rc;
1.2153 - }
1.2154 -
1.2155 - /* If nFin is zero, this loop runs exactly once and page pLastPg
1.2156 - ** is swapped with the first free page pulled off the free list.
1.2157 - **
1.2158 - ** On the other hand, if nFin is greater than zero, then keep
1.2159 - ** looping until a free-page located within the first nFin pages
1.2160 - ** of the file is found.
1.2161 - */
1.2162 - do {
1.2163 - MemPage *pFreePg;
1.2164 - rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
1.2165 - if( rc!=SQLITE_OK ){
1.2166 - releasePage(pLastPg);
1.2167 - return rc;
1.2168 - }
1.2169 - releasePage(pFreePg);
1.2170 - }while( nFin!=0 && iFreePg>nFin );
1.2171 - assert( iFreePg<iLastPg );
1.2172 -
1.2173 - rc = sqlite3PagerWrite(pLastPg->pDbPage);
1.2174 - if( rc!=SQLITE_OK ){
1.2175 - return rc;
1.2176 - }
1.2177 - rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg);
1.2178 - releasePage(pLastPg);
1.2179 - if( rc!=SQLITE_OK ){
1.2180 - return rc;
1.2181 - }
1.2182 - }
1.2183 - }
1.2184 -
1.2185 - pBt->nTrunc = iLastPg - 1;
1.2186 - while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
1.2187 - pBt->nTrunc--;
1.2188 - }
1.2189 - return SQLITE_OK;
1.2190 -}
1.2191 -
1.2192 -/*
1.2193 -** A write-transaction must be opened before calling this function.
1.2194 -** It performs a single unit of work towards an incremental vacuum.
1.2195 -**
1.2196 -** If the incremental vacuum is finished after this function has run,
1.2197 -** SQLITE_DONE is returned. If it is not finished, but no error occured,
1.2198 -** SQLITE_OK is returned. Otherwise an SQLite error code.
1.2199 -*/
1.2200 -int sqlite3BtreeIncrVacuum(Btree *p){
1.2201 - int rc;
1.2202 - BtShared *pBt = p->pBt;
1.2203 -
1.2204 - sqlite3BtreeEnter(p);
1.2205 - assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
1.2206 - if( !pBt->autoVacuum ){
1.2207 - rc = SQLITE_DONE;
1.2208 - }else{
1.2209 - invalidateAllOverflowCache(pBt);
1.2210 - rc = incrVacuumStep(pBt, 0);
1.2211 - }
1.2212 - sqlite3BtreeLeave(p);
1.2213 - return rc;
1.2214 -}
1.2215 -
1.2216 -/*
1.2217 -** This routine is called prior to sqlite3PagerCommit when a transaction
1.2218 -** is commited for an auto-vacuum database.
1.2219 -**
1.2220 -** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
1.2221 -** the database file should be truncated to during the commit process.
1.2222 -** i.e. the database has been reorganized so that only the first *pnTrunc
1.2223 -** pages are in use.
1.2224 -*/
1.2225 -static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
1.2226 - int rc = SQLITE_OK;
1.2227 - Pager *pPager = pBt->pPager;
1.2228 -#ifndef NDEBUG
1.2229 - int nRef = sqlite3PagerRefcount(pPager);
1.2230 -#endif
1.2231 -
1.2232 - assert( sqlite3_mutex_held(pBt->mutex) );
1.2233 - invalidateAllOverflowCache(pBt);
1.2234 - assert(pBt->autoVacuum);
1.2235 - if( !pBt->incrVacuum ){
1.2236 - Pgno nFin = 0;
1.2237 -
1.2238 - if( pBt->nTrunc==0 ){
1.2239 - Pgno nFree;
1.2240 - Pgno nPtrmap;
1.2241 - const int pgsz = pBt->pageSize;
1.2242 - Pgno nOrig = sqlite3PagerPagecount(pBt->pPager);
1.2243 -
1.2244 - if( PTRMAP_ISPAGE(pBt, nOrig) ){
1.2245 - return SQLITE_CORRUPT_BKPT;
1.2246 - }
1.2247 - if( nOrig==PENDING_BYTE_PAGE(pBt) ){
1.2248 - nOrig--;
1.2249 - }
1.2250 - nFree = get4byte(&pBt->pPage1->aData[36]);
1.2251 - nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
1.2252 - nFin = nOrig - nFree - nPtrmap;
1.2253 - if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
1.2254 - nFin--;
1.2255 - }
1.2256 - while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
1.2257 - nFin--;
1.2258 - }
1.2259 - }
1.2260 -
1.2261 - while( rc==SQLITE_OK ){
1.2262 - rc = incrVacuumStep(pBt, nFin);
1.2263 - }
1.2264 - if( rc==SQLITE_DONE ){
1.2265 - assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
1.2266 - rc = SQLITE_OK;
1.2267 - if( pBt->nTrunc ){
1.2268 - rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
1.2269 - put4byte(&pBt->pPage1->aData[32], 0);
1.2270 - put4byte(&pBt->pPage1->aData[36], 0);
1.2271 - pBt->nTrunc = nFin;
1.2272 - }
1.2273 - }
1.2274 - if( rc!=SQLITE_OK ){
1.2275 - sqlite3PagerRollback(pPager);
1.2276 - }
1.2277 - }
1.2278 -
1.2279 - if( rc==SQLITE_OK ){
1.2280 - *pnTrunc = pBt->nTrunc;
1.2281 - pBt->nTrunc = 0;
1.2282 - }
1.2283 - assert( nRef==sqlite3PagerRefcount(pPager) );
1.2284 - return rc;
1.2285 -}
1.2286 -
1.2287 -#endif
1.2288 -
1.2289 -/*
1.2290 -** This routine does the first phase of a two-phase commit. This routine
1.2291 -** causes a rollback journal to be created (if it does not already exist)
1.2292 -** and populated with enough information so that if a power loss occurs
1.2293 -** the database can be restored to its original state by playing back
1.2294 -** the journal. Then the contents of the journal are flushed out to
1.2295 -** the disk. After the journal is safely on oxide, the changes to the
1.2296 -** database are written into the database file and flushed to oxide.
1.2297 -** At the end of this call, the rollback journal still exists on the
1.2298 -** disk and we are still holding all locks, so the transaction has not
1.2299 -** committed. See sqlite3BtreeCommit() for the second phase of the
1.2300 -** commit process.
1.2301 -**
1.2302 -** This call is a no-op if no write-transaction is currently active on pBt.
1.2303 -**
1.2304 -** Otherwise, sync the database file for the btree pBt. zMaster points to
1.2305 -** the name of a master journal file that should be written into the
1.2306 -** individual journal file, or is NULL, indicating no master journal file
1.2307 -** (single database transaction).
1.2308 -**
1.2309 -** When this is called, the master journal should already have been
1.2310 -** created, populated with this journal pointer and synced to disk.
1.2311 -**
1.2312 -** Once this is routine has returned, the only thing required to commit
1.2313 -** the write-transaction for this database file is to delete the journal.
1.2314 -*/
1.2315 -int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
1.2316 - int rc = SQLITE_OK;
1.2317 - if( p->inTrans==TRANS_WRITE ){
1.2318 - BtShared *pBt = p->pBt;
1.2319 - Pgno nTrunc = 0;
1.2320 - sqlite3BtreeEnter(p);
1.2321 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.2322 - if( pBt->autoVacuum ){
1.2323 - rc = autoVacuumCommit(pBt, &nTrunc);
1.2324 - if( rc!=SQLITE_OK ){
1.2325 - sqlite3BtreeLeave(p);
1.2326 - return rc;
1.2327 - }
1.2328 - }
1.2329 -#endif
1.2330 - rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc);
1.2331 - sqlite3BtreeLeave(p);
1.2332 - }
1.2333 - return rc;
1.2334 -}
1.2335 -
1.2336 -/*
1.2337 -** Commit the transaction currently in progress.
1.2338 -**
1.2339 -** This routine implements the second phase of a 2-phase commit. The
1.2340 -** sqlite3BtreeSync() routine does the first phase and should be invoked
1.2341 -** prior to calling this routine. The sqlite3BtreeSync() routine did
1.2342 -** all the work of writing information out to disk and flushing the
1.2343 -** contents so that they are written onto the disk platter. All this
1.2344 -** routine has to do is delete or truncate the rollback journal
1.2345 -** (which causes the transaction to commit) and drop locks.
1.2346 -**
1.2347 -** This will release the write lock on the database file. If there
1.2348 -** are no active cursors, it also releases the read lock.
1.2349 -*/
1.2350 -int sqlite3BtreeCommitPhaseTwo(Btree *p){
1.2351 - BtShared *pBt = p->pBt;
1.2352 -
1.2353 - sqlite3BtreeEnter(p);
1.2354 - btreeIntegrity(p);
1.2355 -
1.2356 - /* If the handle has a write-transaction open, commit the shared-btrees
1.2357 - ** transaction and set the shared state to TRANS_READ.
1.2358 - */
1.2359 - if( p->inTrans==TRANS_WRITE ){
1.2360 - int rc;
1.2361 - assert( pBt->inTransaction==TRANS_WRITE );
1.2362 - assert( pBt->nTransaction>0 );
1.2363 - rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
1.2364 - if( rc!=SQLITE_OK ){
1.2365 - sqlite3BtreeLeave(p);
1.2366 - return rc;
1.2367 - }
1.2368 - pBt->inTransaction = TRANS_READ;
1.2369 - pBt->inStmt = 0;
1.2370 - }
1.2371 - unlockAllTables(p);
1.2372 -
1.2373 - /* If the handle has any kind of transaction open, decrement the transaction
1.2374 - ** count of the shared btree. If the transaction count reaches 0, set
1.2375 - ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
1.2376 - ** will unlock the pager.
1.2377 - */
1.2378 - if( p->inTrans!=TRANS_NONE ){
1.2379 - pBt->nTransaction--;
1.2380 - if( 0==pBt->nTransaction ){
1.2381 - pBt->inTransaction = TRANS_NONE;
1.2382 - }
1.2383 - }
1.2384 -
1.2385 - /* Set the handles current transaction state to TRANS_NONE and unlock
1.2386 - ** the pager if this call closed the only read or write transaction.
1.2387 - */
1.2388 - p->inTrans = TRANS_NONE;
1.2389 - unlockBtreeIfUnused(pBt);
1.2390 -
1.2391 - btreeIntegrity(p);
1.2392 - sqlite3BtreeLeave(p);
1.2393 - return SQLITE_OK;
1.2394 -}
1.2395 -
1.2396 -/*
1.2397 -** Do both phases of a commit.
1.2398 -*/
1.2399 -int sqlite3BtreeCommit(Btree *p){
1.2400 - int rc;
1.2401 - sqlite3BtreeEnter(p);
1.2402 - rc = sqlite3BtreeCommitPhaseOne(p, 0);
1.2403 - if( rc==SQLITE_OK ){
1.2404 - rc = sqlite3BtreeCommitPhaseTwo(p);
1.2405 - }
1.2406 - sqlite3BtreeLeave(p);
1.2407 - return rc;
1.2408 -}
1.2409 -
1.2410 -#ifndef NDEBUG
1.2411 -/*
1.2412 -** Return the number of write-cursors open on this handle. This is for use
1.2413 -** in assert() expressions, so it is only compiled if NDEBUG is not
1.2414 -** defined.
1.2415 -**
1.2416 -** For the purposes of this routine, a write-cursor is any cursor that
1.2417 -** is capable of writing to the databse. That means the cursor was
1.2418 -** originally opened for writing and the cursor has not be disabled
1.2419 -** by having its state changed to CURSOR_FAULT.
1.2420 -*/
1.2421 -static int countWriteCursors(BtShared *pBt){
1.2422 - BtCursor *pCur;
1.2423 - int r = 0;
1.2424 - for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1.2425 - if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
1.2426 - }
1.2427 - return r;
1.2428 -}
1.2429 -#endif
1.2430 -
1.2431 -/*
1.2432 -** This routine sets the state to CURSOR_FAULT and the error
1.2433 -** code to errCode for every cursor on BtShared that pBtree
1.2434 -** references.
1.2435 -**
1.2436 -** Every cursor is tripped, including cursors that belong
1.2437 -** to other database connections that happen to be sharing
1.2438 -** the cache with pBtree.
1.2439 -**
1.2440 -** This routine gets called when a rollback occurs.
1.2441 -** All cursors using the same cache must be tripped
1.2442 -** to prevent them from trying to use the btree after
1.2443 -** the rollback. The rollback may have deleted tables
1.2444 -** or moved root pages, so it is not sufficient to
1.2445 -** save the state of the cursor. The cursor must be
1.2446 -** invalidated.
1.2447 -*/
1.2448 -void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
1.2449 - BtCursor *p;
1.2450 - sqlite3BtreeEnter(pBtree);
1.2451 - for(p=pBtree->pBt->pCursor; p; p=p->pNext){
1.2452 - clearCursorPosition(p);
1.2453 - p->eState = CURSOR_FAULT;
1.2454 - p->skip = errCode;
1.2455 - }
1.2456 - sqlite3BtreeLeave(pBtree);
1.2457 -}
1.2458 -
1.2459 -/*
1.2460 -** Rollback the transaction in progress. All cursors will be
1.2461 -** invalided by this operation. Any attempt to use a cursor
1.2462 -** that was open at the beginning of this operation will result
1.2463 -** in an error.
1.2464 -**
1.2465 -** This will release the write lock on the database file. If there
1.2466 -** are no active cursors, it also releases the read lock.
1.2467 -*/
1.2468 -int sqlite3BtreeRollback(Btree *p){
1.2469 - int rc;
1.2470 - BtShared *pBt = p->pBt;
1.2471 - MemPage *pPage1;
1.2472 -
1.2473 - sqlite3BtreeEnter(p);
1.2474 - rc = saveAllCursors(pBt, 0, 0);
1.2475 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.2476 - if( rc!=SQLITE_OK ){
1.2477 - /* This is a horrible situation. An IO or malloc() error occured whilst
1.2478 - ** trying to save cursor positions. If this is an automatic rollback (as
1.2479 - ** the result of a constraint, malloc() failure or IO error) then
1.2480 - ** the cache may be internally inconsistent (not contain valid trees) so
1.2481 - ** we cannot simply return the error to the caller. Instead, abort
1.2482 - ** all queries that may be using any of the cursors that failed to save.
1.2483 - */
1.2484 - sqlite3BtreeTripAllCursors(p, rc);
1.2485 - }
1.2486 -#endif
1.2487 - btreeIntegrity(p);
1.2488 - unlockAllTables(p);
1.2489 -
1.2490 - if( p->inTrans==TRANS_WRITE ){
1.2491 - int rc2;
1.2492 -
1.2493 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.2494 - pBt->nTrunc = 0;
1.2495 -#endif
1.2496 -
1.2497 - assert( TRANS_WRITE==pBt->inTransaction );
1.2498 - rc2 = sqlite3PagerRollback(pBt->pPager);
1.2499 - if( rc2!=SQLITE_OK ){
1.2500 - rc = rc2;
1.2501 - }
1.2502 -
1.2503 - /* The rollback may have destroyed the pPage1->aData value. So
1.2504 - ** call sqlite3BtreeGetPage() on page 1 again to make
1.2505 - ** sure pPage1->aData is set correctly. */
1.2506 - if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
1.2507 - releasePage(pPage1);
1.2508 - }
1.2509 - assert( countWriteCursors(pBt)==0 );
1.2510 - pBt->inTransaction = TRANS_READ;
1.2511 - }
1.2512 -
1.2513 - if( p->inTrans!=TRANS_NONE ){
1.2514 - assert( pBt->nTransaction>0 );
1.2515 - pBt->nTransaction--;
1.2516 - if( 0==pBt->nTransaction ){
1.2517 - pBt->inTransaction = TRANS_NONE;
1.2518 - }
1.2519 - }
1.2520 -
1.2521 - p->inTrans = TRANS_NONE;
1.2522 - pBt->inStmt = 0;
1.2523 - unlockBtreeIfUnused(pBt);
1.2524 -
1.2525 - btreeIntegrity(p);
1.2526 - sqlite3BtreeLeave(p);
1.2527 - return rc;
1.2528 -}
1.2529 -
1.2530 -/*
1.2531 -** Start a statement subtransaction. The subtransaction can
1.2532 -** can be rolled back independently of the main transaction.
1.2533 -** You must start a transaction before starting a subtransaction.
1.2534 -** The subtransaction is ended automatically if the main transaction
1.2535 -** commits or rolls back.
1.2536 -**
1.2537 -** Only one subtransaction may be active at a time. It is an error to try
1.2538 -** to start a new subtransaction if another subtransaction is already active.
1.2539 -**
1.2540 -** Statement subtransactions are used around individual SQL statements
1.2541 -** that are contained within a BEGIN...COMMIT block. If a constraint
1.2542 -** error occurs within the statement, the effect of that one statement
1.2543 -** can be rolled back without having to rollback the entire transaction.
1.2544 -*/
1.2545 -int sqlite3BtreeBeginStmt(Btree *p){
1.2546 - int rc;
1.2547 - BtShared *pBt = p->pBt;
1.2548 - sqlite3BtreeEnter(p);
1.2549 - if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
1.2550 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.2551 - }else{
1.2552 - assert( pBt->inTransaction==TRANS_WRITE );
1.2553 - rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
1.2554 - pBt->inStmt = 1;
1.2555 - }
1.2556 - sqlite3BtreeLeave(p);
1.2557 - return rc;
1.2558 -}
1.2559 -
1.2560 -
1.2561 -/*
1.2562 -** Commit the statment subtransaction currently in progress. If no
1.2563 -** subtransaction is active, this is a no-op.
1.2564 -*/
1.2565 -int sqlite3BtreeCommitStmt(Btree *p){
1.2566 - int rc;
1.2567 - BtShared *pBt = p->pBt;
1.2568 - sqlite3BtreeEnter(p);
1.2569 - if( pBt->inStmt && !pBt->readOnly ){
1.2570 - rc = sqlite3PagerStmtCommit(pBt->pPager);
1.2571 - }else{
1.2572 - rc = SQLITE_OK;
1.2573 - }
1.2574 - pBt->inStmt = 0;
1.2575 - sqlite3BtreeLeave(p);
1.2576 - return rc;
1.2577 -}
1.2578 -
1.2579 -/*
1.2580 -** Rollback the active statement subtransaction. If no subtransaction
1.2581 -** is active this routine is a no-op.
1.2582 -**
1.2583 -** All cursors will be invalidated by this operation. Any attempt
1.2584 -** to use a cursor that was open at the beginning of this operation
1.2585 -** will result in an error.
1.2586 -*/
1.2587 -int sqlite3BtreeRollbackStmt(Btree *p){
1.2588 - int rc = SQLITE_OK;
1.2589 - BtShared *pBt = p->pBt;
1.2590 - sqlite3BtreeEnter(p);
1.2591 - if( pBt->inStmt && !pBt->readOnly ){
1.2592 - rc = sqlite3PagerStmtRollback(pBt->pPager);
1.2593 - assert( countWriteCursors(pBt)==0 );
1.2594 - pBt->inStmt = 0;
1.2595 - }
1.2596 - sqlite3BtreeLeave(p);
1.2597 - return rc;
1.2598 -}
1.2599 -
1.2600 -/*
1.2601 -** Default key comparison function to be used if no comparison function
1.2602 -** is specified on the sqlite3BtreeCursor() call.
1.2603 -*/
1.2604 -static int dfltCompare(
1.2605 - void *NotUsed, /* User data is not used */
1.2606 - int n1, const void *p1, /* First key to compare */
1.2607 - int n2, const void *p2 /* Second key to compare */
1.2608 -){
1.2609 - int c;
1.2610 - c = memcmp(p1, p2, n1<n2 ? n1 : n2);
1.2611 - if( c==0 ){
1.2612 - c = n1 - n2;
1.2613 - }
1.2614 - return c;
1.2615 -}
1.2616 -
1.2617 -/*
1.2618 -** Create a new cursor for the BTree whose root is on the page
1.2619 -** iTable. The act of acquiring a cursor gets a read lock on
1.2620 -** the database file.
1.2621 -**
1.2622 -** If wrFlag==0, then the cursor can only be used for reading.
1.2623 -** If wrFlag==1, then the cursor can be used for reading or for
1.2624 -** writing if other conditions for writing are also met. These
1.2625 -** are the conditions that must be met in order for writing to
1.2626 -** be allowed:
1.2627 -**
1.2628 -** 1: The cursor must have been opened with wrFlag==1
1.2629 -**
1.2630 -** 2: Other database connections that share the same pager cache
1.2631 -** but which are not in the READ_UNCOMMITTED state may not have
1.2632 -** cursors open with wrFlag==0 on the same table. Otherwise
1.2633 -** the changes made by this write cursor would be visible to
1.2634 -** the read cursors in the other database connection.
1.2635 -**
1.2636 -** 3: The database must be writable (not on read-only media)
1.2637 -**
1.2638 -** 4: There must be an active transaction.
1.2639 -**
1.2640 -** No checking is done to make sure that page iTable really is the
1.2641 -** root page of a b-tree. If it is not, then the cursor acquired
1.2642 -** will not work correctly.
1.2643 -**
1.2644 -** The comparison function must be logically the same for every cursor
1.2645 -** on a particular table. Changing the comparison function will result
1.2646 -** in incorrect operations. If the comparison function is NULL, a
1.2647 -** default comparison function is used. The comparison function is
1.2648 -** always ignored for INTKEY tables.
1.2649 -*/
1.2650 -static int btreeCursor(
1.2651 - Btree *p, /* The btree */
1.2652 - int iTable, /* Root page of table to open */
1.2653 - int wrFlag, /* 1 to write. 0 read-only */
1.2654 - int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
1.2655 - void *pArg, /* First arg to xCompare() */
1.2656 - BtCursor **ppCur /* Write new cursor here */
1.2657 -){
1.2658 - int rc;
1.2659 - BtCursor *pCur;
1.2660 - BtShared *pBt = p->pBt;
1.2661 -
1.2662 - assert( sqlite3BtreeHoldsMutex(p) );
1.2663 - *ppCur = 0;
1.2664 - if( wrFlag ){
1.2665 - if( pBt->readOnly ){
1.2666 - return SQLITE_READONLY;
1.2667 - }
1.2668 - if( checkReadLocks(p, iTable, 0) ){
1.2669 - return SQLITE_LOCKED;
1.2670 - }
1.2671 - }
1.2672 -
1.2673 - if( pBt->pPage1==0 ){
1.2674 - rc = lockBtreeWithRetry(p);
1.2675 - if( rc!=SQLITE_OK ){
1.2676 - return rc;
1.2677 - }
1.2678 - if( pBt->readOnly && wrFlag ){
1.2679 - return SQLITE_READONLY;
1.2680 - }
1.2681 - }
1.2682 - pCur = sqlite3MallocZero( sizeof(*pCur) );
1.2683 - if( pCur==0 ){
1.2684 - rc = SQLITE_NOMEM;
1.2685 - goto create_cursor_exception;
1.2686 - }
1.2687 - pCur->pgnoRoot = (Pgno)iTable;
1.2688 - if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
1.2689 - rc = SQLITE_EMPTY;
1.2690 - goto create_cursor_exception;
1.2691 - }
1.2692 - rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
1.2693 - if( rc!=SQLITE_OK ){
1.2694 - goto create_cursor_exception;
1.2695 - }
1.2696 -
1.2697 - /* Now that no other errors can occur, finish filling in the BtCursor
1.2698 - ** variables, link the cursor into the BtShared list and set *ppCur (the
1.2699 - ** output argument to this function).
1.2700 - */
1.2701 - pCur->xCompare = xCmp ? xCmp : dfltCompare;
1.2702 - pCur->pArg = pArg;
1.2703 - pCur->pBtree = p;
1.2704 - pCur->pBt = pBt;
1.2705 - pCur->wrFlag = wrFlag;
1.2706 - pCur->pNext = pBt->pCursor;
1.2707 - if( pCur->pNext ){
1.2708 - pCur->pNext->pPrev = pCur;
1.2709 - }
1.2710 - pBt->pCursor = pCur;
1.2711 - pCur->eState = CURSOR_INVALID;
1.2712 - *ppCur = pCur;
1.2713 -
1.2714 - return SQLITE_OK;
1.2715 -
1.2716 -create_cursor_exception:
1.2717 - if( pCur ){
1.2718 - releasePage(pCur->pPage);
1.2719 - sqlite3_free(pCur);
1.2720 - }
1.2721 - unlockBtreeIfUnused(pBt);
1.2722 - return rc;
1.2723 -}
1.2724 -int sqlite3BtreeCursor(
1.2725 - Btree *p, /* The btree */
1.2726 - int iTable, /* Root page of table to open */
1.2727 - int wrFlag, /* 1 to write. 0 read-only */
1.2728 - int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
1.2729 - void *pArg, /* First arg to xCompare() */
1.2730 - BtCursor **ppCur /* Write new cursor here */
1.2731 -){
1.2732 - int rc;
1.2733 - sqlite3BtreeEnter(p);
1.2734 - rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur);
1.2735 - sqlite3BtreeLeave(p);
1.2736 - return rc;
1.2737 -}
1.2738 -
1.2739 -
1.2740 -/*
1.2741 -** Close a cursor. The read lock on the database file is released
1.2742 -** when the last cursor is closed.
1.2743 -*/
1.2744 -int sqlite3BtreeCloseCursor(BtCursor *pCur){
1.2745 - BtShared *pBt = pCur->pBt;
1.2746 - Btree *pBtree = pCur->pBtree;
1.2747 -
1.2748 - sqlite3BtreeEnter(pBtree);
1.2749 - clearCursorPosition(pCur);
1.2750 - if( pCur->pPrev ){
1.2751 - pCur->pPrev->pNext = pCur->pNext;
1.2752 - }else{
1.2753 - pBt->pCursor = pCur->pNext;
1.2754 - }
1.2755 - if( pCur->pNext ){
1.2756 - pCur->pNext->pPrev = pCur->pPrev;
1.2757 - }
1.2758 - releasePage(pCur->pPage);
1.2759 - unlockBtreeIfUnused(pBt);
1.2760 - invalidateOverflowCache(pCur);
1.2761 - sqlite3_free(pCur);
1.2762 - sqlite3BtreeLeave(pBtree);
1.2763 - return SQLITE_OK;
1.2764 -}
1.2765 -
1.2766 -/*
1.2767 -** Make a temporary cursor by filling in the fields of pTempCur.
1.2768 -** The temporary cursor is not on the cursor list for the Btree.
1.2769 -*/
1.2770 -void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
1.2771 - assert( cursorHoldsMutex(pCur) );
1.2772 - memcpy(pTempCur, pCur, sizeof(*pCur));
1.2773 - pTempCur->pNext = 0;
1.2774 - pTempCur->pPrev = 0;
1.2775 - if( pTempCur->pPage ){
1.2776 - sqlite3PagerRef(pTempCur->pPage->pDbPage);
1.2777 - }
1.2778 -}
1.2779 -
1.2780 -/*
1.2781 -** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
1.2782 -** function above.
1.2783 -*/
1.2784 -void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
1.2785 - assert( cursorHoldsMutex(pCur) );
1.2786 - if( pCur->pPage ){
1.2787 - sqlite3PagerUnref(pCur->pPage->pDbPage);
1.2788 - }
1.2789 -}
1.2790 -
1.2791 -/*
1.2792 -** Make sure the BtCursor* given in the argument has a valid
1.2793 -** BtCursor.info structure. If it is not already valid, call
1.2794 -** sqlite3BtreeParseCell() to fill it in.
1.2795 -**
1.2796 -** BtCursor.info is a cache of the information in the current cell.
1.2797 -** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
1.2798 -**
1.2799 -** 2007-06-25: There is a bug in some versions of MSVC that cause the
1.2800 -** compiler to crash when getCellInfo() is implemented as a macro.
1.2801 -** But there is a measureable speed advantage to using the macro on gcc
1.2802 -** (when less compiler optimizations like -Os or -O0 are used and the
1.2803 -** compiler is not doing agressive inlining.) So we use a real function
1.2804 -** for MSVC and a macro for everything else. Ticket #2457.
1.2805 -*/
1.2806 -#ifndef NDEBUG
1.2807 - static void assertCellInfo(BtCursor *pCur){
1.2808 - CellInfo info;
1.2809 - memset(&info, 0, sizeof(info));
1.2810 - sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info);
1.2811 - assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
1.2812 - }
1.2813 -#else
1.2814 - #define assertCellInfo(x)
1.2815 -#endif
1.2816 -#ifdef _MSC_VER
1.2817 - /* Use a real function in MSVC to work around bugs in that compiler. */
1.2818 - static void getCellInfo(BtCursor *pCur){
1.2819 - if( pCur->info.nSize==0 ){
1.2820 - sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);
1.2821 - }else{
1.2822 - assertCellInfo(pCur);
1.2823 - }
1.2824 - }
1.2825 -#else /* if not _MSC_VER */
1.2826 - /* Use a macro in all other compilers so that the function is inlined */
1.2827 -#define getCellInfo(pCur) \
1.2828 - if( pCur->info.nSize==0 ){ \
1.2829 - sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \
1.2830 - }else{ \
1.2831 - assertCellInfo(pCur); \
1.2832 - }
1.2833 -#endif /* _MSC_VER */
1.2834 -
1.2835 -/*
1.2836 -** Set *pSize to the size of the buffer needed to hold the value of
1.2837 -** the key for the current entry. If the cursor is not pointing
1.2838 -** to a valid entry, *pSize is set to 0.
1.2839 -**
1.2840 -** For a table with the INTKEY flag set, this routine returns the key
1.2841 -** itself, not the number of bytes in the key.
1.2842 -*/
1.2843 -int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
1.2844 - int rc;
1.2845 -
1.2846 - assert( cursorHoldsMutex(pCur) );
1.2847 - rc = restoreOrClearCursorPosition(pCur);
1.2848 - if( rc==SQLITE_OK ){
1.2849 - assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
1.2850 - if( pCur->eState==CURSOR_INVALID ){
1.2851 - *pSize = 0;
1.2852 - }else{
1.2853 - getCellInfo(pCur);
1.2854 - *pSize = pCur->info.nKey;
1.2855 - }
1.2856 - }
1.2857 - return rc;
1.2858 -}
1.2859 -
1.2860 -/*
1.2861 -** Set *pSize to the number of bytes of data in the entry the
1.2862 -** cursor currently points to. Always return SQLITE_OK.
1.2863 -** Failure is not possible. If the cursor is not currently
1.2864 -** pointing to an entry (which can happen, for example, if
1.2865 -** the database is empty) then *pSize is set to 0.
1.2866 -*/
1.2867 -int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
1.2868 - int rc;
1.2869 -
1.2870 - assert( cursorHoldsMutex(pCur) );
1.2871 - rc = restoreOrClearCursorPosition(pCur);
1.2872 - if( rc==SQLITE_OK ){
1.2873 - assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
1.2874 - if( pCur->eState==CURSOR_INVALID ){
1.2875 - /* Not pointing at a valid entry - set *pSize to 0. */
1.2876 - *pSize = 0;
1.2877 - }else{
1.2878 - getCellInfo(pCur);
1.2879 - *pSize = pCur->info.nData;
1.2880 - }
1.2881 - }
1.2882 - return rc;
1.2883 -}
1.2884 -
1.2885 -/*
1.2886 -** Given the page number of an overflow page in the database (parameter
1.2887 -** ovfl), this function finds the page number of the next page in the
1.2888 -** linked list of overflow pages. If possible, it uses the auto-vacuum
1.2889 -** pointer-map data instead of reading the content of page ovfl to do so.
1.2890 -**
1.2891 -** If an error occurs an SQLite error code is returned. Otherwise:
1.2892 -**
1.2893 -** Unless pPgnoNext is NULL, the page number of the next overflow
1.2894 -** page in the linked list is written to *pPgnoNext. If page ovfl
1.2895 -** is the last page in it's linked list, *pPgnoNext is set to zero.
1.2896 -**
1.2897 -** If ppPage is not NULL, *ppPage is set to the MemPage* handle
1.2898 -** for page ovfl. The underlying pager page may have been requested
1.2899 -** with the noContent flag set, so the page data accessable via
1.2900 -** this handle may not be trusted.
1.2901 -*/
1.2902 -static int getOverflowPage(
1.2903 - BtShared *pBt,
1.2904 - Pgno ovfl, /* Overflow page */
1.2905 - MemPage **ppPage, /* OUT: MemPage handle */
1.2906 - Pgno *pPgnoNext /* OUT: Next overflow page number */
1.2907 -){
1.2908 - Pgno next = 0;
1.2909 - int rc;
1.2910 -
1.2911 - assert( sqlite3_mutex_held(pBt->mutex) );
1.2912 - /* One of these must not be NULL. Otherwise, why call this function? */
1.2913 - assert(ppPage || pPgnoNext);
1.2914 -
1.2915 - /* If pPgnoNext is NULL, then this function is being called to obtain
1.2916 - ** a MemPage* reference only. No page-data is required in this case.
1.2917 - */
1.2918 - if( !pPgnoNext ){
1.2919 - return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
1.2920 - }
1.2921 -
1.2922 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.2923 - /* Try to find the next page in the overflow list using the
1.2924 - ** autovacuum pointer-map pages. Guess that the next page in
1.2925 - ** the overflow list is page number (ovfl+1). If that guess turns
1.2926 - ** out to be wrong, fall back to loading the data of page
1.2927 - ** number ovfl to determine the next page number.
1.2928 - */
1.2929 - if( pBt->autoVacuum ){
1.2930 - Pgno pgno;
1.2931 - Pgno iGuess = ovfl+1;
1.2932 - u8 eType;
1.2933 -
1.2934 - while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
1.2935 - iGuess++;
1.2936 - }
1.2937 -
1.2938 - if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){
1.2939 - rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
1.2940 - if( rc!=SQLITE_OK ){
1.2941 - return rc;
1.2942 - }
1.2943 - if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
1.2944 - next = iGuess;
1.2945 - }
1.2946 - }
1.2947 - }
1.2948 -#endif
1.2949 -
1.2950 - if( next==0 || ppPage ){
1.2951 - MemPage *pPage = 0;
1.2952 -
1.2953 - rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
1.2954 - assert(rc==SQLITE_OK || pPage==0);
1.2955 - if( next==0 && rc==SQLITE_OK ){
1.2956 - next = get4byte(pPage->aData);
1.2957 - }
1.2958 -
1.2959 - if( ppPage ){
1.2960 - *ppPage = pPage;
1.2961 - }else{
1.2962 - releasePage(pPage);
1.2963 - }
1.2964 - }
1.2965 - *pPgnoNext = next;
1.2966 -
1.2967 - return rc;
1.2968 -}
1.2969 -
1.2970 -/*
1.2971 -** Copy data from a buffer to a page, or from a page to a buffer.
1.2972 -**
1.2973 -** pPayload is a pointer to data stored on database page pDbPage.
1.2974 -** If argument eOp is false, then nByte bytes of data are copied
1.2975 -** from pPayload to the buffer pointed at by pBuf. If eOp is true,
1.2976 -** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
1.2977 -** of data are copied from the buffer pBuf to pPayload.
1.2978 -**
1.2979 -** SQLITE_OK is returned on success, otherwise an error code.
1.2980 -*/
1.2981 -static int copyPayload(
1.2982 - void *pPayload, /* Pointer to page data */
1.2983 - void *pBuf, /* Pointer to buffer */
1.2984 - int nByte, /* Number of bytes to copy */
1.2985 - int eOp, /* 0 -> copy from page, 1 -> copy to page */
1.2986 - DbPage *pDbPage /* Page containing pPayload */
1.2987 -){
1.2988 - if( eOp ){
1.2989 - /* Copy data from buffer to page (a write operation) */
1.2990 - int rc = sqlite3PagerWrite(pDbPage);
1.2991 - if( rc!=SQLITE_OK ){
1.2992 - return rc;
1.2993 - }
1.2994 - memcpy(pPayload, pBuf, nByte);
1.2995 - }else{
1.2996 - /* Copy data from page to buffer (a read operation) */
1.2997 - memcpy(pBuf, pPayload, nByte);
1.2998 - }
1.2999 - return SQLITE_OK;
1.3000 -}
1.3001 -
1.3002 -/*
1.3003 -** This function is used to read or overwrite payload information
1.3004 -** for the entry that the pCur cursor is pointing to. If the eOp
1.3005 -** parameter is 0, this is a read operation (data copied into
1.3006 -** buffer pBuf). If it is non-zero, a write (data copied from
1.3007 -** buffer pBuf).
1.3008 -**
1.3009 -** A total of "amt" bytes are read or written beginning at "offset".
1.3010 -** Data is read to or from the buffer pBuf.
1.3011 -**
1.3012 -** This routine does not make a distinction between key and data.
1.3013 -** It just reads or writes bytes from the payload area. Data might
1.3014 -** appear on the main page or be scattered out on multiple overflow
1.3015 -** pages.
1.3016 -**
1.3017 -** If the BtCursor.isIncrblobHandle flag is set, and the current
1.3018 -** cursor entry uses one or more overflow pages, this function
1.3019 -** allocates space for and lazily popluates the overflow page-list
1.3020 -** cache array (BtCursor.aOverflow). Subsequent calls use this
1.3021 -** cache to make seeking to the supplied offset more efficient.
1.3022 -**
1.3023 -** Once an overflow page-list cache has been allocated, it may be
1.3024 -** invalidated if some other cursor writes to the same table, or if
1.3025 -** the cursor is moved to a different row. Additionally, in auto-vacuum
1.3026 -** mode, the following events may invalidate an overflow page-list cache.
1.3027 -**
1.3028 -** * An incremental vacuum,
1.3029 -** * A commit in auto_vacuum="full" mode,
1.3030 -** * Creating a table (may require moving an overflow page).
1.3031 -*/
1.3032 -static int accessPayload(
1.3033 - BtCursor *pCur, /* Cursor pointing to entry to read from */
1.3034 - int offset, /* Begin reading this far into payload */
1.3035 - int amt, /* Read this many bytes */
1.3036 - unsigned char *pBuf, /* Write the bytes into this buffer */
1.3037 - int skipKey, /* offset begins at data if this is true */
1.3038 - int eOp /* zero to read. non-zero to write. */
1.3039 -){
1.3040 - unsigned char *aPayload;
1.3041 - int rc = SQLITE_OK;
1.3042 - u32 nKey;
1.3043 - int iIdx = 0;
1.3044 - MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */
1.3045 - BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
1.3046 -
1.3047 - assert( pPage );
1.3048 - assert( pCur->eState==CURSOR_VALID );
1.3049 - assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
1.3050 - assert( offset>=0 );
1.3051 - assert( cursorHoldsMutex(pCur) );
1.3052 -
1.3053 - getCellInfo(pCur);
1.3054 - aPayload = pCur->info.pCell + pCur->info.nHeader;
1.3055 - nKey = (pPage->intKey ? 0 : pCur->info.nKey);
1.3056 -
1.3057 - if( skipKey ){
1.3058 - offset += nKey;
1.3059 - }
1.3060 - if( offset+amt > nKey+pCur->info.nData ){
1.3061 - /* Trying to read or write past the end of the data is an error */
1.3062 - return SQLITE_ERROR;
1.3063 - }
1.3064 -
1.3065 - /* Check if data must be read/written to/from the btree page itself. */
1.3066 - if( offset<pCur->info.nLocal ){
1.3067 - int a = amt;
1.3068 - if( a+offset>pCur->info.nLocal ){
1.3069 - a = pCur->info.nLocal - offset;
1.3070 - }
1.3071 - rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
1.3072 - offset = 0;
1.3073 - pBuf += a;
1.3074 - amt -= a;
1.3075 - }else{
1.3076 - offset -= pCur->info.nLocal;
1.3077 - }
1.3078 -
1.3079 - if( rc==SQLITE_OK && amt>0 ){
1.3080 - const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
1.3081 - Pgno nextPage;
1.3082 -
1.3083 - nextPage = get4byte(&aPayload[pCur->info.nLocal]);
1.3084 -
1.3085 -#ifndef SQLITE_OMIT_INCRBLOB
1.3086 - /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
1.3087 - ** has not been allocated, allocate it now. The array is sized at
1.3088 - ** one entry for each overflow page in the overflow chain. The
1.3089 - ** page number of the first overflow page is stored in aOverflow[0],
1.3090 - ** etc. A value of 0 in the aOverflow[] array means "not yet known"
1.3091 - ** (the cache is lazily populated).
1.3092 - */
1.3093 - if( pCur->isIncrblobHandle && !pCur->aOverflow ){
1.3094 - int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
1.3095 - pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
1.3096 - if( nOvfl && !pCur->aOverflow ){
1.3097 - rc = SQLITE_NOMEM;
1.3098 - }
1.3099 - }
1.3100 -
1.3101 - /* If the overflow page-list cache has been allocated and the
1.3102 - ** entry for the first required overflow page is valid, skip
1.3103 - ** directly to it.
1.3104 - */
1.3105 - if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
1.3106 - iIdx = (offset/ovflSize);
1.3107 - nextPage = pCur->aOverflow[iIdx];
1.3108 - offset = (offset%ovflSize);
1.3109 - }
1.3110 -#endif
1.3111 -
1.3112 - for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
1.3113 -
1.3114 -#ifndef SQLITE_OMIT_INCRBLOB
1.3115 - /* If required, populate the overflow page-list cache. */
1.3116 - if( pCur->aOverflow ){
1.3117 - assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
1.3118 - pCur->aOverflow[iIdx] = nextPage;
1.3119 - }
1.3120 -#endif
1.3121 -
1.3122 - if( offset>=ovflSize ){
1.3123 - /* The only reason to read this page is to obtain the page
1.3124 - ** number for the next page in the overflow chain. The page
1.3125 - ** data is not required. So first try to lookup the overflow
1.3126 - ** page-list cache, if any, then fall back to the getOverflowPage()
1.3127 - ** function.
1.3128 - */
1.3129 -#ifndef SQLITE_OMIT_INCRBLOB
1.3130 - if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
1.3131 - nextPage = pCur->aOverflow[iIdx+1];
1.3132 - } else
1.3133 -#endif
1.3134 - rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
1.3135 - offset -= ovflSize;
1.3136 - }else{
1.3137 - /* Need to read this page properly. It contains some of the
1.3138 - ** range of data that is being read (eOp==0) or written (eOp!=0).
1.3139 - */
1.3140 - DbPage *pDbPage;
1.3141 - int a = amt;
1.3142 - rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
1.3143 - if( rc==SQLITE_OK ){
1.3144 - aPayload = sqlite3PagerGetData(pDbPage);
1.3145 - nextPage = get4byte(aPayload);
1.3146 - if( a + offset > ovflSize ){
1.3147 - a = ovflSize - offset;
1.3148 - }
1.3149 - rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
1.3150 - sqlite3PagerUnref(pDbPage);
1.3151 - offset = 0;
1.3152 - amt -= a;
1.3153 - pBuf += a;
1.3154 - }
1.3155 - }
1.3156 - }
1.3157 - }
1.3158 -
1.3159 - if( rc==SQLITE_OK && amt>0 ){
1.3160 - return SQLITE_CORRUPT_BKPT;
1.3161 - }
1.3162 - return rc;
1.3163 -}
1.3164 -
1.3165 -/*
1.3166 -** Read part of the key associated with cursor pCur. Exactly
1.3167 -** "amt" bytes will be transfered into pBuf[]. The transfer
1.3168 -** begins at "offset".
1.3169 -**
1.3170 -** Return SQLITE_OK on success or an error code if anything goes
1.3171 -** wrong. An error is returned if "offset+amt" is larger than
1.3172 -** the available payload.
1.3173 -*/
1.3174 -int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
1.3175 - int rc;
1.3176 -
1.3177 - assert( cursorHoldsMutex(pCur) );
1.3178 - rc = restoreOrClearCursorPosition(pCur);
1.3179 - if( rc==SQLITE_OK ){
1.3180 - assert( pCur->eState==CURSOR_VALID );
1.3181 - assert( pCur->pPage!=0 );
1.3182 - if( pCur->pPage->intKey ){
1.3183 - return SQLITE_CORRUPT_BKPT;
1.3184 - }
1.3185 - assert( pCur->pPage->intKey==0 );
1.3186 - assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
1.3187 - rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
1.3188 - }
1.3189 - return rc;
1.3190 -}
1.3191 -
1.3192 -/*
1.3193 -** Read part of the data associated with cursor pCur. Exactly
1.3194 -** "amt" bytes will be transfered into pBuf[]. The transfer
1.3195 -** begins at "offset".
1.3196 -**
1.3197 -** Return SQLITE_OK on success or an error code if anything goes
1.3198 -** wrong. An error is returned if "offset+amt" is larger than
1.3199 -** the available payload.
1.3200 -*/
1.3201 -int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
1.3202 - int rc;
1.3203 -
1.3204 - assert( cursorHoldsMutex(pCur) );
1.3205 - rc = restoreOrClearCursorPosition(pCur);
1.3206 - if( rc==SQLITE_OK ){
1.3207 - assert( pCur->eState==CURSOR_VALID );
1.3208 - assert( pCur->pPage!=0 );
1.3209 - assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
1.3210 - rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
1.3211 - }
1.3212 - return rc;
1.3213 -}
1.3214 -
1.3215 -/*
1.3216 -** Return a pointer to payload information from the entry that the
1.3217 -** pCur cursor is pointing to. The pointer is to the beginning of
1.3218 -** the key if skipKey==0 and it points to the beginning of data if
1.3219 -** skipKey==1. The number of bytes of available key/data is written
1.3220 -** into *pAmt. If *pAmt==0, then the value returned will not be
1.3221 -** a valid pointer.
1.3222 -**
1.3223 -** This routine is an optimization. It is common for the entire key
1.3224 -** and data to fit on the local page and for there to be no overflow
1.3225 -** pages. When that is so, this routine can be used to access the
1.3226 -** key and data without making a copy. If the key and/or data spills
1.3227 -** onto overflow pages, then accessPayload() must be used to reassembly
1.3228 -** the key/data and copy it into a preallocated buffer.
1.3229 -**
1.3230 -** The pointer returned by this routine looks directly into the cached
1.3231 -** page of the database. The data might change or move the next time
1.3232 -** any btree routine is called.
1.3233 -*/
1.3234 -static const unsigned char *fetchPayload(
1.3235 - BtCursor *pCur, /* Cursor pointing to entry to read from */
1.3236 - int *pAmt, /* Write the number of available bytes here */
1.3237 - int skipKey /* read beginning at data if this is true */
1.3238 -){
1.3239 - unsigned char *aPayload;
1.3240 - MemPage *pPage;
1.3241 - u32 nKey;
1.3242 - int nLocal;
1.3243 -
1.3244 - assert( pCur!=0 && pCur->pPage!=0 );
1.3245 - assert( pCur->eState==CURSOR_VALID );
1.3246 - assert( cursorHoldsMutex(pCur) );
1.3247 - pPage = pCur->pPage;
1.3248 - assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
1.3249 - getCellInfo(pCur);
1.3250 - aPayload = pCur->info.pCell;
1.3251 - aPayload += pCur->info.nHeader;
1.3252 - if( pPage->intKey ){
1.3253 - nKey = 0;
1.3254 - }else{
1.3255 - nKey = pCur->info.nKey;
1.3256 - }
1.3257 - if( skipKey ){
1.3258 - aPayload += nKey;
1.3259 - nLocal = pCur->info.nLocal - nKey;
1.3260 - }else{
1.3261 - nLocal = pCur->info.nLocal;
1.3262 - if( nLocal>nKey ){
1.3263 - nLocal = nKey;
1.3264 - }
1.3265 - }
1.3266 - *pAmt = nLocal;
1.3267 - return aPayload;
1.3268 -}
1.3269 -
1.3270 -
1.3271 -/*
1.3272 -** For the entry that cursor pCur is point to, return as
1.3273 -** many bytes of the key or data as are available on the local
1.3274 -** b-tree page. Write the number of available bytes into *pAmt.
1.3275 -**
1.3276 -** The pointer returned is ephemeral. The key/data may move
1.3277 -** or be destroyed on the next call to any Btree routine,
1.3278 -** including calls from other threads against the same cache.
1.3279 -** Hence, a mutex on the BtShared should be held prior to calling
1.3280 -** this routine.
1.3281 -**
1.3282 -** These routines is used to get quick access to key and data
1.3283 -** in the common case where no overflow pages are used.
1.3284 -*/
1.3285 -const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
1.3286 - assert( cursorHoldsMutex(pCur) );
1.3287 - if( pCur->eState==CURSOR_VALID ){
1.3288 - return (const void*)fetchPayload(pCur, pAmt, 0);
1.3289 - }
1.3290 - return 0;
1.3291 -}
1.3292 -const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
1.3293 - assert( cursorHoldsMutex(pCur) );
1.3294 - if( pCur->eState==CURSOR_VALID ){
1.3295 - return (const void*)fetchPayload(pCur, pAmt, 1);
1.3296 - }
1.3297 - return 0;
1.3298 -}
1.3299 -
1.3300 -
1.3301 -/*
1.3302 -** Move the cursor down to a new child page. The newPgno argument is the
1.3303 -** page number of the child page to move to.
1.3304 -*/
1.3305 -static int moveToChild(BtCursor *pCur, u32 newPgno){
1.3306 - int rc;
1.3307 - MemPage *pNewPage;
1.3308 - MemPage *pOldPage;
1.3309 - BtShared *pBt = pCur->pBt;
1.3310 -
1.3311 - assert( cursorHoldsMutex(pCur) );
1.3312 - assert( pCur->eState==CURSOR_VALID );
1.3313 - rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
1.3314 - if( rc ) return rc;
1.3315 - pNewPage->idxParent = pCur->idx;
1.3316 - pOldPage = pCur->pPage;
1.3317 - pOldPage->idxShift = 0;
1.3318 - releasePage(pOldPage);
1.3319 - pCur->pPage = pNewPage;
1.3320 - pCur->idx = 0;
1.3321 - pCur->info.nSize = 0;
1.3322 - if( pNewPage->nCell<1 ){
1.3323 - return SQLITE_CORRUPT_BKPT;
1.3324 - }
1.3325 - return SQLITE_OK;
1.3326 -}
1.3327 -
1.3328 -/*
1.3329 -** Return true if the page is the virtual root of its table.
1.3330 -**
1.3331 -** The virtual root page is the root page for most tables. But
1.3332 -** for the table rooted on page 1, sometime the real root page
1.3333 -** is empty except for the right-pointer. In such cases the
1.3334 -** virtual root page is the page that the right-pointer of page
1.3335 -** 1 is pointing to.
1.3336 -*/
1.3337 -int sqlite3BtreeIsRootPage(MemPage *pPage){
1.3338 - MemPage *pParent;
1.3339 -
1.3340 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.3341 - pParent = pPage->pParent;
1.3342 - if( pParent==0 ) return 1;
1.3343 - if( pParent->pgno>1 ) return 0;
1.3344 - if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
1.3345 - return 0;
1.3346 -}
1.3347 -
1.3348 -/*
1.3349 -** Move the cursor up to the parent page.
1.3350 -**
1.3351 -** pCur->idx is set to the cell index that contains the pointer
1.3352 -** to the page we are coming from. If we are coming from the
1.3353 -** right-most child page then pCur->idx is set to one more than
1.3354 -** the largest cell index.
1.3355 -*/
1.3356 -void sqlite3BtreeMoveToParent(BtCursor *pCur){
1.3357 - MemPage *pParent;
1.3358 - MemPage *pPage;
1.3359 - int idxParent;
1.3360 -
1.3361 - assert( cursorHoldsMutex(pCur) );
1.3362 - assert( pCur->eState==CURSOR_VALID );
1.3363 - pPage = pCur->pPage;
1.3364 - assert( pPage!=0 );
1.3365 - assert( !sqlite3BtreeIsRootPage(pPage) );
1.3366 - pParent = pPage->pParent;
1.3367 - assert( pParent!=0 );
1.3368 - idxParent = pPage->idxParent;
1.3369 - sqlite3PagerRef(pParent->pDbPage);
1.3370 - releasePage(pPage);
1.3371 - pCur->pPage = pParent;
1.3372 - pCur->info.nSize = 0;
1.3373 - assert( pParent->idxShift==0 );
1.3374 - pCur->idx = idxParent;
1.3375 -}
1.3376 -
1.3377 -/*
1.3378 -** Move the cursor to the root page
1.3379 -*/
1.3380 -static int moveToRoot(BtCursor *pCur){
1.3381 - MemPage *pRoot;
1.3382 - int rc = SQLITE_OK;
1.3383 - Btree *p = pCur->pBtree;
1.3384 - BtShared *pBt = p->pBt;
1.3385 -
1.3386 - assert( cursorHoldsMutex(pCur) );
1.3387 - assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
1.3388 - assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
1.3389 - assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
1.3390 - if( pCur->eState>=CURSOR_REQUIRESEEK ){
1.3391 - if( pCur->eState==CURSOR_FAULT ){
1.3392 - return pCur->skip;
1.3393 - }
1.3394 - clearCursorPosition(pCur);
1.3395 - }
1.3396 - pRoot = pCur->pPage;
1.3397 - if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
1.3398 - assert( pRoot->isInit );
1.3399 - }else{
1.3400 - if(
1.3401 - SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
1.3402 - ){
1.3403 - pCur->eState = CURSOR_INVALID;
1.3404 - return rc;
1.3405 - }
1.3406 - releasePage(pCur->pPage);
1.3407 - pCur->pPage = pRoot;
1.3408 - }
1.3409 - pCur->idx = 0;
1.3410 - pCur->info.nSize = 0;
1.3411 - if( pRoot->nCell==0 && !pRoot->leaf ){
1.3412 - Pgno subpage;
1.3413 - assert( pRoot->pgno==1 );
1.3414 - subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
1.3415 - assert( subpage>0 );
1.3416 - pCur->eState = CURSOR_VALID;
1.3417 - rc = moveToChild(pCur, subpage);
1.3418 - }
1.3419 - pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
1.3420 - return rc;
1.3421 -}
1.3422 -
1.3423 -/*
1.3424 -** Move the cursor down to the left-most leaf entry beneath the
1.3425 -** entry to which it is currently pointing.
1.3426 -**
1.3427 -** The left-most leaf is the one with the smallest key - the first
1.3428 -** in ascending order.
1.3429 -*/
1.3430 -static int moveToLeftmost(BtCursor *pCur){
1.3431 - Pgno pgno;
1.3432 - int rc = SQLITE_OK;
1.3433 - MemPage *pPage;
1.3434 -
1.3435 - assert( cursorHoldsMutex(pCur) );
1.3436 - assert( pCur->eState==CURSOR_VALID );
1.3437 - while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
1.3438 - assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
1.3439 - pgno = get4byte(findCell(pPage, pCur->idx));
1.3440 - rc = moveToChild(pCur, pgno);
1.3441 - }
1.3442 - return rc;
1.3443 -}
1.3444 -
1.3445 -/*
1.3446 -** Move the cursor down to the right-most leaf entry beneath the
1.3447 -** page to which it is currently pointing. Notice the difference
1.3448 -** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
1.3449 -** finds the left-most entry beneath the *entry* whereas moveToRightmost()
1.3450 -** finds the right-most entry beneath the *page*.
1.3451 -**
1.3452 -** The right-most entry is the one with the largest key - the last
1.3453 -** key in ascending order.
1.3454 -*/
1.3455 -static int moveToRightmost(BtCursor *pCur){
1.3456 - Pgno pgno;
1.3457 - int rc = SQLITE_OK;
1.3458 - MemPage *pPage;
1.3459 -
1.3460 - assert( cursorHoldsMutex(pCur) );
1.3461 - assert( pCur->eState==CURSOR_VALID );
1.3462 - while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
1.3463 - pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1.3464 - pCur->idx = pPage->nCell;
1.3465 - rc = moveToChild(pCur, pgno);
1.3466 - }
1.3467 - if( rc==SQLITE_OK ){
1.3468 - pCur->idx = pPage->nCell - 1;
1.3469 - pCur->info.nSize = 0;
1.3470 - }
1.3471 - return SQLITE_OK;
1.3472 -}
1.3473 -
1.3474 -/* Move the cursor to the first entry in the table. Return SQLITE_OK
1.3475 -** on success. Set *pRes to 0 if the cursor actually points to something
1.3476 -** or set *pRes to 1 if the table is empty.
1.3477 -*/
1.3478 -int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
1.3479 - int rc;
1.3480 -
1.3481 - assert( cursorHoldsMutex(pCur) );
1.3482 - assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
1.3483 - rc = moveToRoot(pCur);
1.3484 - if( rc==SQLITE_OK ){
1.3485 - if( pCur->eState==CURSOR_INVALID ){
1.3486 - assert( pCur->pPage->nCell==0 );
1.3487 - *pRes = 1;
1.3488 - rc = SQLITE_OK;
1.3489 - }else{
1.3490 - assert( pCur->pPage->nCell>0 );
1.3491 - *pRes = 0;
1.3492 - rc = moveToLeftmost(pCur);
1.3493 - }
1.3494 - }
1.3495 - return rc;
1.3496 -}
1.3497 -
1.3498 -/* Move the cursor to the last entry in the table. Return SQLITE_OK
1.3499 -** on success. Set *pRes to 0 if the cursor actually points to something
1.3500 -** or set *pRes to 1 if the table is empty.
1.3501 -*/
1.3502 -int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
1.3503 - int rc;
1.3504 -
1.3505 - assert( cursorHoldsMutex(pCur) );
1.3506 - assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
1.3507 - rc = moveToRoot(pCur);
1.3508 - if( rc==SQLITE_OK ){
1.3509 - if( CURSOR_INVALID==pCur->eState ){
1.3510 - assert( pCur->pPage->nCell==0 );
1.3511 - *pRes = 1;
1.3512 - }else{
1.3513 - assert( pCur->eState==CURSOR_VALID );
1.3514 - *pRes = 0;
1.3515 - rc = moveToRightmost(pCur);
1.3516 - }
1.3517 - }
1.3518 - return rc;
1.3519 -}
1.3520 -
1.3521 -/* Move the cursor so that it points to an entry near pKey/nKey.
1.3522 -** Return a success code.
1.3523 -**
1.3524 -** For INTKEY tables, only the nKey parameter is used. pKey is
1.3525 -** ignored. For other tables, nKey is the number of bytes of data
1.3526 -** in pKey. The comparison function specified when the cursor was
1.3527 -** created is used to compare keys.
1.3528 -**
1.3529 -** If an exact match is not found, then the cursor is always
1.3530 -** left pointing at a leaf page which would hold the entry if it
1.3531 -** were present. The cursor might point to an entry that comes
1.3532 -** before or after the key.
1.3533 -**
1.3534 -** The result of comparing the key with the entry to which the
1.3535 -** cursor is written to *pRes if pRes!=NULL. The meaning of
1.3536 -** this value is as follows:
1.3537 -**
1.3538 -** *pRes<0 The cursor is left pointing at an entry that
1.3539 -** is smaller than pKey or if the table is empty
1.3540 -** and the cursor is therefore left point to nothing.
1.3541 -**
1.3542 -** *pRes==0 The cursor is left pointing at an entry that
1.3543 -** exactly matches pKey.
1.3544 -**
1.3545 -** *pRes>0 The cursor is left pointing at an entry that
1.3546 -** is larger than pKey.
1.3547 -**
1.3548 -*/
1.3549 -int sqlite3BtreeMoveto(
1.3550 - BtCursor *pCur, /* The cursor to be moved */
1.3551 - const void *pKey, /* The key content for indices. Not used by tables */
1.3552 - i64 nKey, /* Size of pKey. Or the key for tables */
1.3553 - int biasRight, /* If true, bias the search to the high end */
1.3554 - int *pRes /* Search result flag */
1.3555 -){
1.3556 - int rc;
1.3557 -
1.3558 - assert( cursorHoldsMutex(pCur) );
1.3559 - assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
1.3560 - rc = moveToRoot(pCur);
1.3561 - if( rc ){
1.3562 - return rc;
1.3563 - }
1.3564 - assert( pCur->pPage );
1.3565 - assert( pCur->pPage->isInit );
1.3566 - if( pCur->eState==CURSOR_INVALID ){
1.3567 - *pRes = -1;
1.3568 - assert( pCur->pPage->nCell==0 );
1.3569 - return SQLITE_OK;
1.3570 - }
1.3571 - for(;;){
1.3572 - int lwr, upr;
1.3573 - Pgno chldPg;
1.3574 - MemPage *pPage = pCur->pPage;
1.3575 - int c = -1; /* pRes return if table is empty must be -1 */
1.3576 - lwr = 0;
1.3577 - upr = pPage->nCell-1;
1.3578 - if( !pPage->intKey && pKey==0 ){
1.3579 - return SQLITE_CORRUPT_BKPT;
1.3580 - }
1.3581 - if( biasRight ){
1.3582 - pCur->idx = upr;
1.3583 - }else{
1.3584 - pCur->idx = (upr+lwr)/2;
1.3585 - }
1.3586 - if( lwr<=upr ) for(;;){
1.3587 - void *pCellKey;
1.3588 - i64 nCellKey;
1.3589 - pCur->info.nSize = 0;
1.3590 - if( pPage->intKey ){
1.3591 - u8 *pCell;
1.3592 - pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
1.3593 - if( pPage->hasData ){
1.3594 - u32 dummy;
1.3595 - pCell += getVarint32(pCell, &dummy);
1.3596 - }
1.3597 - getVarint(pCell, (u64 *)&nCellKey);
1.3598 - if( nCellKey<nKey ){
1.3599 - c = -1;
1.3600 - }else if( nCellKey>nKey ){
1.3601 - c = +1;
1.3602 - }else{
1.3603 - c = 0;
1.3604 - }
1.3605 - }else{
1.3606 - int available;
1.3607 - pCellKey = (void *)fetchPayload(pCur, &available, 0);
1.3608 - nCellKey = pCur->info.nKey;
1.3609 - if( available>=nCellKey ){
1.3610 - c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
1.3611 - }else{
1.3612 - pCellKey = sqlite3_malloc( nCellKey );
1.3613 - if( pCellKey==0 ) return SQLITE_NOMEM;
1.3614 - rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
1.3615 - c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
1.3616 - sqlite3_free(pCellKey);
1.3617 - if( rc ){
1.3618 - return rc;
1.3619 - }
1.3620 - }
1.3621 - }
1.3622 - if( c==0 ){
1.3623 - if( pPage->leafData && !pPage->leaf ){
1.3624 - lwr = pCur->idx;
1.3625 - upr = lwr - 1;
1.3626 - break;
1.3627 - }else{
1.3628 - if( pRes ) *pRes = 0;
1.3629 - return SQLITE_OK;
1.3630 - }
1.3631 - }
1.3632 - if( c<0 ){
1.3633 - lwr = pCur->idx+1;
1.3634 - }else{
1.3635 - upr = pCur->idx-1;
1.3636 - }
1.3637 - if( lwr>upr ){
1.3638 - break;
1.3639 - }
1.3640 - pCur->idx = (lwr+upr)/2;
1.3641 - }
1.3642 - assert( lwr==upr+1 );
1.3643 - assert( pPage->isInit );
1.3644 - if( pPage->leaf ){
1.3645 - chldPg = 0;
1.3646 - }else if( lwr>=pPage->nCell ){
1.3647 - chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1.3648 - }else{
1.3649 - chldPg = get4byte(findCell(pPage, lwr));
1.3650 - }
1.3651 - if( chldPg==0 ){
1.3652 - assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
1.3653 - if( pRes ) *pRes = c;
1.3654 - return SQLITE_OK;
1.3655 - }
1.3656 - pCur->idx = lwr;
1.3657 - pCur->info.nSize = 0;
1.3658 - rc = moveToChild(pCur, chldPg);
1.3659 - if( rc ){
1.3660 - return rc;
1.3661 - }
1.3662 - }
1.3663 - /* NOT REACHED */
1.3664 -}
1.3665 -
1.3666 -
1.3667 -/*
1.3668 -** Return TRUE if the cursor is not pointing at an entry of the table.
1.3669 -**
1.3670 -** TRUE will be returned after a call to sqlite3BtreeNext() moves
1.3671 -** past the last entry in the table or sqlite3BtreePrev() moves past
1.3672 -** the first entry. TRUE is also returned if the table is empty.
1.3673 -*/
1.3674 -int sqlite3BtreeEof(BtCursor *pCur){
1.3675 - /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
1.3676 - ** have been deleted? This API will need to change to return an error code
1.3677 - ** as well as the boolean result value.
1.3678 - */
1.3679 - return (CURSOR_VALID!=pCur->eState);
1.3680 -}
1.3681 -
1.3682 -/*
1.3683 -** Return the database connection handle for a cursor.
1.3684 -*/
1.3685 -sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
1.3686 - assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
1.3687 - return pCur->pBtree->pSqlite;
1.3688 -}
1.3689 -
1.3690 -/*
1.3691 -** Advance the cursor to the next entry in the database. If
1.3692 -** successful then set *pRes=0. If the cursor
1.3693 -** was already pointing to the last entry in the database before
1.3694 -** this routine was called, then set *pRes=1.
1.3695 -*/
1.3696 -static int btreeNext(BtCursor *pCur, int *pRes){
1.3697 - int rc;
1.3698 - MemPage *pPage;
1.3699 -
1.3700 - assert( cursorHoldsMutex(pCur) );
1.3701 - rc = restoreOrClearCursorPosition(pCur);
1.3702 - if( rc!=SQLITE_OK ){
1.3703 - return rc;
1.3704 - }
1.3705 - assert( pRes!=0 );
1.3706 - pPage = pCur->pPage;
1.3707 - if( CURSOR_INVALID==pCur->eState ){
1.3708 - *pRes = 1;
1.3709 - return SQLITE_OK;
1.3710 - }
1.3711 - if( pCur->skip>0 ){
1.3712 - pCur->skip = 0;
1.3713 - *pRes = 0;
1.3714 - return SQLITE_OK;
1.3715 - }
1.3716 - pCur->skip = 0;
1.3717 -
1.3718 - assert( pPage->isInit );
1.3719 - assert( pCur->idx<pPage->nCell );
1.3720 -
1.3721 - pCur->idx++;
1.3722 - pCur->info.nSize = 0;
1.3723 - if( pCur->idx>=pPage->nCell ){
1.3724 - if( !pPage->leaf ){
1.3725 - rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
1.3726 - if( rc ) return rc;
1.3727 - rc = moveToLeftmost(pCur);
1.3728 - *pRes = 0;
1.3729 - return rc;
1.3730 - }
1.3731 - do{
1.3732 - if( sqlite3BtreeIsRootPage(pPage) ){
1.3733 - *pRes = 1;
1.3734 - pCur->eState = CURSOR_INVALID;
1.3735 - return SQLITE_OK;
1.3736 - }
1.3737 - sqlite3BtreeMoveToParent(pCur);
1.3738 - pPage = pCur->pPage;
1.3739 - }while( pCur->idx>=pPage->nCell );
1.3740 - *pRes = 0;
1.3741 - if( pPage->leafData ){
1.3742 - rc = sqlite3BtreeNext(pCur, pRes);
1.3743 - }else{
1.3744 - rc = SQLITE_OK;
1.3745 - }
1.3746 - return rc;
1.3747 - }
1.3748 - *pRes = 0;
1.3749 - if( pPage->leaf ){
1.3750 - return SQLITE_OK;
1.3751 - }
1.3752 - rc = moveToLeftmost(pCur);
1.3753 - return rc;
1.3754 -}
1.3755 -int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
1.3756 - int rc;
1.3757 - assert( cursorHoldsMutex(pCur) );
1.3758 - rc = btreeNext(pCur, pRes);
1.3759 - return rc;
1.3760 -}
1.3761 -
1.3762 -
1.3763 -/*
1.3764 -** Step the cursor to the back to the previous entry in the database. If
1.3765 -** successful then set *pRes=0. If the cursor
1.3766 -** was already pointing to the first entry in the database before
1.3767 -** this routine was called, then set *pRes=1.
1.3768 -*/
1.3769 -static int btreePrevious(BtCursor *pCur, int *pRes){
1.3770 - int rc;
1.3771 - Pgno pgno;
1.3772 - MemPage *pPage;
1.3773 -
1.3774 - assert( cursorHoldsMutex(pCur) );
1.3775 - rc = restoreOrClearCursorPosition(pCur);
1.3776 - if( rc!=SQLITE_OK ){
1.3777 - return rc;
1.3778 - }
1.3779 - if( CURSOR_INVALID==pCur->eState ){
1.3780 - *pRes = 1;
1.3781 - return SQLITE_OK;
1.3782 - }
1.3783 - if( pCur->skip<0 ){
1.3784 - pCur->skip = 0;
1.3785 - *pRes = 0;
1.3786 - return SQLITE_OK;
1.3787 - }
1.3788 - pCur->skip = 0;
1.3789 -
1.3790 - pPage = pCur->pPage;
1.3791 - assert( pPage->isInit );
1.3792 - assert( pCur->idx>=0 );
1.3793 - if( !pPage->leaf ){
1.3794 - pgno = get4byte( findCell(pPage, pCur->idx) );
1.3795 - rc = moveToChild(pCur, pgno);
1.3796 - if( rc ){
1.3797 - return rc;
1.3798 - }
1.3799 - rc = moveToRightmost(pCur);
1.3800 - }else{
1.3801 - while( pCur->idx==0 ){
1.3802 - if( sqlite3BtreeIsRootPage(pPage) ){
1.3803 - pCur->eState = CURSOR_INVALID;
1.3804 - *pRes = 1;
1.3805 - return SQLITE_OK;
1.3806 - }
1.3807 - sqlite3BtreeMoveToParent(pCur);
1.3808 - pPage = pCur->pPage;
1.3809 - }
1.3810 - pCur->idx--;
1.3811 - pCur->info.nSize = 0;
1.3812 - if( pPage->leafData && !pPage->leaf ){
1.3813 - rc = sqlite3BtreePrevious(pCur, pRes);
1.3814 - }else{
1.3815 - rc = SQLITE_OK;
1.3816 - }
1.3817 - }
1.3818 - *pRes = 0;
1.3819 - return rc;
1.3820 -}
1.3821 -int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
1.3822 - int rc;
1.3823 - assert( cursorHoldsMutex(pCur) );
1.3824 - rc = btreePrevious(pCur, pRes);
1.3825 - return rc;
1.3826 -}
1.3827 -
1.3828 -/*
1.3829 -** Allocate a new page from the database file.
1.3830 -**
1.3831 -** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
1.3832 -** has already been called on the new page.) The new page has also
1.3833 -** been referenced and the calling routine is responsible for calling
1.3834 -** sqlite3PagerUnref() on the new page when it is done.
1.3835 -**
1.3836 -** SQLITE_OK is returned on success. Any other return value indicates
1.3837 -** an error. *ppPage and *pPgno are undefined in the event of an error.
1.3838 -** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
1.3839 -**
1.3840 -** If the "nearby" parameter is not 0, then a (feeble) effort is made to
1.3841 -** locate a page close to the page number "nearby". This can be used in an
1.3842 -** attempt to keep related pages close to each other in the database file,
1.3843 -** which in turn can make database access faster.
1.3844 -**
1.3845 -** If the "exact" parameter is not 0, and the page-number nearby exists
1.3846 -** anywhere on the free-list, then it is guarenteed to be returned. This
1.3847 -** is only used by auto-vacuum databases when allocating a new table.
1.3848 -*/
1.3849 -static int allocateBtreePage(
1.3850 - BtShared *pBt,
1.3851 - MemPage **ppPage,
1.3852 - Pgno *pPgno,
1.3853 - Pgno nearby,
1.3854 - u8 exact
1.3855 -){
1.3856 - MemPage *pPage1;
1.3857 - int rc;
1.3858 - int n; /* Number of pages on the freelist */
1.3859 - int k; /* Number of leaves on the trunk of the freelist */
1.3860 - MemPage *pTrunk = 0;
1.3861 - MemPage *pPrevTrunk = 0;
1.3862 -
1.3863 - assert( sqlite3_mutex_held(pBt->mutex) );
1.3864 - pPage1 = pBt->pPage1;
1.3865 - n = get4byte(&pPage1->aData[36]);
1.3866 - if( n>0 ){
1.3867 - /* There are pages on the freelist. Reuse one of those pages. */
1.3868 - Pgno iTrunk;
1.3869 - u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
1.3870 -
1.3871 - /* If the 'exact' parameter was true and a query of the pointer-map
1.3872 - ** shows that the page 'nearby' is somewhere on the free-list, then
1.3873 - ** the entire-list will be searched for that page.
1.3874 - */
1.3875 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.3876 - if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){
1.3877 - u8 eType;
1.3878 - assert( nearby>0 );
1.3879 - assert( pBt->autoVacuum );
1.3880 - rc = ptrmapGet(pBt, nearby, &eType, 0);
1.3881 - if( rc ) return rc;
1.3882 - if( eType==PTRMAP_FREEPAGE ){
1.3883 - searchList = 1;
1.3884 - }
1.3885 - *pPgno = nearby;
1.3886 - }
1.3887 -#endif
1.3888 -
1.3889 - /* Decrement the free-list count by 1. Set iTrunk to the index of the
1.3890 - ** first free-list trunk page. iPrevTrunk is initially 1.
1.3891 - */
1.3892 - rc = sqlite3PagerWrite(pPage1->pDbPage);
1.3893 - if( rc ) return rc;
1.3894 - put4byte(&pPage1->aData[36], n-1);
1.3895 -
1.3896 - /* The code within this loop is run only once if the 'searchList' variable
1.3897 - ** is not true. Otherwise, it runs once for each trunk-page on the
1.3898 - ** free-list until the page 'nearby' is located.
1.3899 - */
1.3900 - do {
1.3901 - pPrevTrunk = pTrunk;
1.3902 - if( pPrevTrunk ){
1.3903 - iTrunk = get4byte(&pPrevTrunk->aData[0]);
1.3904 - }else{
1.3905 - iTrunk = get4byte(&pPage1->aData[32]);
1.3906 - }
1.3907 - rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
1.3908 - if( rc ){
1.3909 - pTrunk = 0;
1.3910 - goto end_allocate_page;
1.3911 - }
1.3912 -
1.3913 - k = get4byte(&pTrunk->aData[4]);
1.3914 - if( k==0 && !searchList ){
1.3915 - /* The trunk has no leaves and the list is not being searched.
1.3916 - ** So extract the trunk page itself and use it as the newly
1.3917 - ** allocated page */
1.3918 - assert( pPrevTrunk==0 );
1.3919 - rc = sqlite3PagerWrite(pTrunk->pDbPage);
1.3920 - if( rc ){
1.3921 - goto end_allocate_page;
1.3922 - }
1.3923 - *pPgno = iTrunk;
1.3924 - memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
1.3925 - *ppPage = pTrunk;
1.3926 - pTrunk = 0;
1.3927 - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
1.3928 - }else if( k>pBt->usableSize/4 - 8 ){
1.3929 - /* Value of k is out of range. Database corruption */
1.3930 - rc = SQLITE_CORRUPT_BKPT;
1.3931 - goto end_allocate_page;
1.3932 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.3933 - }else if( searchList && nearby==iTrunk ){
1.3934 - /* The list is being searched and this trunk page is the page
1.3935 - ** to allocate, regardless of whether it has leaves.
1.3936 - */
1.3937 - assert( *pPgno==iTrunk );
1.3938 - *ppPage = pTrunk;
1.3939 - searchList = 0;
1.3940 - rc = sqlite3PagerWrite(pTrunk->pDbPage);
1.3941 - if( rc ){
1.3942 - goto end_allocate_page;
1.3943 - }
1.3944 - if( k==0 ){
1.3945 - if( !pPrevTrunk ){
1.3946 - memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
1.3947 - }else{
1.3948 - memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
1.3949 - }
1.3950 - }else{
1.3951 - /* The trunk page is required by the caller but it contains
1.3952 - ** pointers to free-list leaves. The first leaf becomes a trunk
1.3953 - ** page in this case.
1.3954 - */
1.3955 - MemPage *pNewTrunk;
1.3956 - Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
1.3957 - rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
1.3958 - if( rc!=SQLITE_OK ){
1.3959 - goto end_allocate_page;
1.3960 - }
1.3961 - rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
1.3962 - if( rc!=SQLITE_OK ){
1.3963 - releasePage(pNewTrunk);
1.3964 - goto end_allocate_page;
1.3965 - }
1.3966 - memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
1.3967 - put4byte(&pNewTrunk->aData[4], k-1);
1.3968 - memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
1.3969 - releasePage(pNewTrunk);
1.3970 - if( !pPrevTrunk ){
1.3971 - put4byte(&pPage1->aData[32], iNewTrunk);
1.3972 - }else{
1.3973 - rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
1.3974 - if( rc ){
1.3975 - goto end_allocate_page;
1.3976 - }
1.3977 - put4byte(&pPrevTrunk->aData[0], iNewTrunk);
1.3978 - }
1.3979 - }
1.3980 - pTrunk = 0;
1.3981 - TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
1.3982 -#endif
1.3983 - }else{
1.3984 - /* Extract a leaf from the trunk */
1.3985 - int closest;
1.3986 - Pgno iPage;
1.3987 - unsigned char *aData = pTrunk->aData;
1.3988 - rc = sqlite3PagerWrite(pTrunk->pDbPage);
1.3989 - if( rc ){
1.3990 - goto end_allocate_page;
1.3991 - }
1.3992 - if( nearby>0 ){
1.3993 - int i, dist;
1.3994 - closest = 0;
1.3995 - dist = get4byte(&aData[8]) - nearby;
1.3996 - if( dist<0 ) dist = -dist;
1.3997 - for(i=1; i<k; i++){
1.3998 - int d2 = get4byte(&aData[8+i*4]) - nearby;
1.3999 - if( d2<0 ) d2 = -d2;
1.4000 - if( d2<dist ){
1.4001 - closest = i;
1.4002 - dist = d2;
1.4003 - }
1.4004 - }
1.4005 - }else{
1.4006 - closest = 0;
1.4007 - }
1.4008 -
1.4009 - iPage = get4byte(&aData[8+closest*4]);
1.4010 - if( !searchList || iPage==nearby ){
1.4011 - *pPgno = iPage;
1.4012 - if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
1.4013 - /* Free page off the end of the file */
1.4014 - return SQLITE_CORRUPT_BKPT;
1.4015 - }
1.4016 - TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
1.4017 - ": %d more free pages\n",
1.4018 - *pPgno, closest+1, k, pTrunk->pgno, n-1));
1.4019 - if( closest<k-1 ){
1.4020 - memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
1.4021 - }
1.4022 - put4byte(&aData[4], k-1);
1.4023 - rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
1.4024 - if( rc==SQLITE_OK ){
1.4025 - sqlite3PagerDontRollback((*ppPage)->pDbPage);
1.4026 - rc = sqlite3PagerWrite((*ppPage)->pDbPage);
1.4027 - if( rc!=SQLITE_OK ){
1.4028 - releasePage(*ppPage);
1.4029 - }
1.4030 - }
1.4031 - searchList = 0;
1.4032 - }
1.4033 - }
1.4034 - releasePage(pPrevTrunk);
1.4035 - pPrevTrunk = 0;
1.4036 - }while( searchList );
1.4037 - }else{
1.4038 - /* There are no pages on the freelist, so create a new page at the
1.4039 - ** end of the file */
1.4040 - *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
1.4041 -
1.4042 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4043 - if( pBt->nTrunc ){
1.4044 - /* An incr-vacuum has already run within this transaction. So the
1.4045 - ** page to allocate is not from the physical end of the file, but
1.4046 - ** at pBt->nTrunc.
1.4047 - */
1.4048 - *pPgno = pBt->nTrunc+1;
1.4049 - if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
1.4050 - (*pPgno)++;
1.4051 - }
1.4052 - }
1.4053 - if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
1.4054 - /* If *pPgno refers to a pointer-map page, allocate two new pages
1.4055 - ** at the end of the file instead of one. The first allocated page
1.4056 - ** becomes a new pointer-map page, the second is used by the caller.
1.4057 - */
1.4058 - TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
1.4059 - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
1.4060 - (*pPgno)++;
1.4061 - }
1.4062 - if( pBt->nTrunc ){
1.4063 - pBt->nTrunc = *pPgno;
1.4064 - }
1.4065 -#endif
1.4066 -
1.4067 - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
1.4068 - rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
1.4069 - if( rc ) return rc;
1.4070 - rc = sqlite3PagerWrite((*ppPage)->pDbPage);
1.4071 - if( rc!=SQLITE_OK ){
1.4072 - releasePage(*ppPage);
1.4073 - }
1.4074 - TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
1.4075 - }
1.4076 -
1.4077 - assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
1.4078 -
1.4079 -end_allocate_page:
1.4080 - releasePage(pTrunk);
1.4081 - releasePage(pPrevTrunk);
1.4082 - return rc;
1.4083 -}
1.4084 -
1.4085 -/*
1.4086 -** Add a page of the database file to the freelist.
1.4087 -**
1.4088 -** sqlite3PagerUnref() is NOT called for pPage.
1.4089 -*/
1.4090 -static int freePage(MemPage *pPage){
1.4091 - BtShared *pBt = pPage->pBt;
1.4092 - MemPage *pPage1 = pBt->pPage1;
1.4093 - int rc, n, k;
1.4094 -
1.4095 - /* Prepare the page for freeing */
1.4096 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4097 - assert( pPage->pgno>1 );
1.4098 - pPage->isInit = 0;
1.4099 - releasePage(pPage->pParent);
1.4100 - pPage->pParent = 0;
1.4101 -
1.4102 - /* Increment the free page count on pPage1 */
1.4103 - rc = sqlite3PagerWrite(pPage1->pDbPage);
1.4104 - if( rc ) return rc;
1.4105 - n = get4byte(&pPage1->aData[36]);
1.4106 - put4byte(&pPage1->aData[36], n+1);
1.4107 -
1.4108 -#ifdef SQLITE_SECURE_DELETE
1.4109 - /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
1.4110 - ** always fully overwrite deleted information with zeros.
1.4111 - */
1.4112 - rc = sqlite3PagerWrite(pPage->pDbPage);
1.4113 - if( rc ) return rc;
1.4114 - memset(pPage->aData, 0, pPage->pBt->pageSize);
1.4115 -#endif
1.4116 -
1.4117 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4118 - /* If the database supports auto-vacuum, write an entry in the pointer-map
1.4119 - ** to indicate that the page is free.
1.4120 - */
1.4121 - if( pBt->autoVacuum ){
1.4122 - rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
1.4123 - if( rc ) return rc;
1.4124 - }
1.4125 -#endif
1.4126 -
1.4127 - if( n==0 ){
1.4128 - /* This is the first free page */
1.4129 - rc = sqlite3PagerWrite(pPage->pDbPage);
1.4130 - if( rc ) return rc;
1.4131 - memset(pPage->aData, 0, 8);
1.4132 - put4byte(&pPage1->aData[32], pPage->pgno);
1.4133 - TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
1.4134 - }else{
1.4135 - /* Other free pages already exist. Retrive the first trunk page
1.4136 - ** of the freelist and find out how many leaves it has. */
1.4137 - MemPage *pTrunk;
1.4138 - rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
1.4139 - if( rc ) return rc;
1.4140 - k = get4byte(&pTrunk->aData[4]);
1.4141 - if( k>=pBt->usableSize/4 - 8 ){
1.4142 - /* The trunk is full. Turn the page being freed into a new
1.4143 - ** trunk page with no leaves. */
1.4144 - rc = sqlite3PagerWrite(pPage->pDbPage);
1.4145 - if( rc==SQLITE_OK ){
1.4146 - put4byte(pPage->aData, pTrunk->pgno);
1.4147 - put4byte(&pPage->aData[4], 0);
1.4148 - put4byte(&pPage1->aData[32], pPage->pgno);
1.4149 - TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
1.4150 - pPage->pgno, pTrunk->pgno));
1.4151 - }
1.4152 - }else if( k<0 ){
1.4153 - rc = SQLITE_CORRUPT;
1.4154 - }else{
1.4155 - /* Add the newly freed page as a leaf on the current trunk */
1.4156 - rc = sqlite3PagerWrite(pTrunk->pDbPage);
1.4157 - if( rc==SQLITE_OK ){
1.4158 - put4byte(&pTrunk->aData[4], k+1);
1.4159 - put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
1.4160 -#ifndef SQLITE_SECURE_DELETE
1.4161 - sqlite3PagerDontWrite(pPage->pDbPage);
1.4162 -#endif
1.4163 - }
1.4164 - TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
1.4165 - }
1.4166 - releasePage(pTrunk);
1.4167 - }
1.4168 - return rc;
1.4169 -}
1.4170 -
1.4171 -/*
1.4172 -** Free any overflow pages associated with the given Cell.
1.4173 -*/
1.4174 -static int clearCell(MemPage *pPage, unsigned char *pCell){
1.4175 - BtShared *pBt = pPage->pBt;
1.4176 - CellInfo info;
1.4177 - Pgno ovflPgno;
1.4178 - int rc;
1.4179 - int nOvfl;
1.4180 - int ovflPageSize;
1.4181 -
1.4182 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4183 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.4184 - if( info.iOverflow==0 ){
1.4185 - return SQLITE_OK; /* No overflow pages. Return without doing anything */
1.4186 - }
1.4187 - ovflPgno = get4byte(&pCell[info.iOverflow]);
1.4188 - ovflPageSize = pBt->usableSize - 4;
1.4189 - nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
1.4190 - assert( ovflPgno==0 || nOvfl>0 );
1.4191 - while( nOvfl-- ){
1.4192 - MemPage *pOvfl;
1.4193 - if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
1.4194 - return SQLITE_CORRUPT_BKPT;
1.4195 - }
1.4196 -
1.4197 - rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
1.4198 - if( rc ) return rc;
1.4199 - rc = freePage(pOvfl);
1.4200 - sqlite3PagerUnref(pOvfl->pDbPage);
1.4201 - if( rc ) return rc;
1.4202 - }
1.4203 - return SQLITE_OK;
1.4204 -}
1.4205 -
1.4206 -/*
1.4207 -** Create the byte sequence used to represent a cell on page pPage
1.4208 -** and write that byte sequence into pCell[]. Overflow pages are
1.4209 -** allocated and filled in as necessary. The calling procedure
1.4210 -** is responsible for making sure sufficient space has been allocated
1.4211 -** for pCell[].
1.4212 -**
1.4213 -** Note that pCell does not necessary need to point to the pPage->aData
1.4214 -** area. pCell might point to some temporary storage. The cell will
1.4215 -** be constructed in this temporary area then copied into pPage->aData
1.4216 -** later.
1.4217 -*/
1.4218 -static int fillInCell(
1.4219 - MemPage *pPage, /* The page that contains the cell */
1.4220 - unsigned char *pCell, /* Complete text of the cell */
1.4221 - const void *pKey, i64 nKey, /* The key */
1.4222 - const void *pData,int nData, /* The data */
1.4223 - int nZero, /* Extra zero bytes to append to pData */
1.4224 - int *pnSize /* Write cell size here */
1.4225 -){
1.4226 - int nPayload;
1.4227 - const u8 *pSrc;
1.4228 - int nSrc, n, rc;
1.4229 - int spaceLeft;
1.4230 - MemPage *pOvfl = 0;
1.4231 - MemPage *pToRelease = 0;
1.4232 - unsigned char *pPrior;
1.4233 - unsigned char *pPayload;
1.4234 - BtShared *pBt = pPage->pBt;
1.4235 - Pgno pgnoOvfl = 0;
1.4236 - int nHeader;
1.4237 - CellInfo info;
1.4238 -
1.4239 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4240 -
1.4241 - /* Fill in the header. */
1.4242 - nHeader = 0;
1.4243 - if( !pPage->leaf ){
1.4244 - nHeader += 4;
1.4245 - }
1.4246 - if( pPage->hasData ){
1.4247 - nHeader += putVarint(&pCell[nHeader], nData+nZero);
1.4248 - }else{
1.4249 - nData = nZero = 0;
1.4250 - }
1.4251 - nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
1.4252 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.4253 - assert( info.nHeader==nHeader );
1.4254 - assert( info.nKey==nKey );
1.4255 - assert( info.nData==nData+nZero );
1.4256 -
1.4257 - /* Fill in the payload */
1.4258 - nPayload = nData + nZero;
1.4259 - if( pPage->intKey ){
1.4260 - pSrc = pData;
1.4261 - nSrc = nData;
1.4262 - nData = 0;
1.4263 - }else{
1.4264 - nPayload += nKey;
1.4265 - pSrc = pKey;
1.4266 - nSrc = nKey;
1.4267 - }
1.4268 - *pnSize = info.nSize;
1.4269 - spaceLeft = info.nLocal;
1.4270 - pPayload = &pCell[nHeader];
1.4271 - pPrior = &pCell[info.iOverflow];
1.4272 -
1.4273 - while( nPayload>0 ){
1.4274 - if( spaceLeft==0 ){
1.4275 - int isExact = 0;
1.4276 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4277 - Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
1.4278 - if( pBt->autoVacuum ){
1.4279 - do{
1.4280 - pgnoOvfl++;
1.4281 - } while(
1.4282 - PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
1.4283 - );
1.4284 - if( pgnoOvfl>1 ){
1.4285 - /* isExact = 1; */
1.4286 - }
1.4287 - }
1.4288 -#endif
1.4289 - rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact);
1.4290 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4291 - /* If the database supports auto-vacuum, and the second or subsequent
1.4292 - ** overflow page is being allocated, add an entry to the pointer-map
1.4293 - ** for that page now.
1.4294 - **
1.4295 - ** If this is the first overflow page, then write a partial entry
1.4296 - ** to the pointer-map. If we write nothing to this pointer-map slot,
1.4297 - ** then the optimistic overflow chain processing in clearCell()
1.4298 - ** may misinterpret the uninitialised values and delete the
1.4299 - ** wrong pages from the database.
1.4300 - */
1.4301 - if( pBt->autoVacuum && rc==SQLITE_OK ){
1.4302 - u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
1.4303 - rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
1.4304 - if( rc ){
1.4305 - releasePage(pOvfl);
1.4306 - }
1.4307 - }
1.4308 -#endif
1.4309 - if( rc ){
1.4310 - releasePage(pToRelease);
1.4311 - return rc;
1.4312 - }
1.4313 - put4byte(pPrior, pgnoOvfl);
1.4314 - releasePage(pToRelease);
1.4315 - pToRelease = pOvfl;
1.4316 - pPrior = pOvfl->aData;
1.4317 - put4byte(pPrior, 0);
1.4318 - pPayload = &pOvfl->aData[4];
1.4319 - spaceLeft = pBt->usableSize - 4;
1.4320 - }
1.4321 - n = nPayload;
1.4322 - if( n>spaceLeft ) n = spaceLeft;
1.4323 - if( nSrc>0 ){
1.4324 - if( n>nSrc ) n = nSrc;
1.4325 - assert( pSrc );
1.4326 - memcpy(pPayload, pSrc, n);
1.4327 - }else{
1.4328 - memset(pPayload, 0, n);
1.4329 - }
1.4330 - nPayload -= n;
1.4331 - pPayload += n;
1.4332 - pSrc += n;
1.4333 - nSrc -= n;
1.4334 - spaceLeft -= n;
1.4335 - if( nSrc==0 ){
1.4336 - nSrc = nData;
1.4337 - pSrc = pData;
1.4338 - }
1.4339 - }
1.4340 - releasePage(pToRelease);
1.4341 - return SQLITE_OK;
1.4342 -}
1.4343 -
1.4344 -/*
1.4345 -** Change the MemPage.pParent pointer on the page whose number is
1.4346 -** given in the second argument so that MemPage.pParent holds the
1.4347 -** pointer in the third argument.
1.4348 -*/
1.4349 -static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
1.4350 - MemPage *pThis;
1.4351 - DbPage *pDbPage;
1.4352 -
1.4353 - assert( sqlite3_mutex_held(pBt->mutex) );
1.4354 - assert( pNewParent!=0 );
1.4355 - if( pgno==0 ) return SQLITE_OK;
1.4356 - assert( pBt->pPager!=0 );
1.4357 - pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
1.4358 - if( pDbPage ){
1.4359 - pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1.4360 - if( pThis->isInit ){
1.4361 - assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
1.4362 - if( pThis->pParent!=pNewParent ){
1.4363 - if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
1.4364 - pThis->pParent = pNewParent;
1.4365 - sqlite3PagerRef(pNewParent->pDbPage);
1.4366 - }
1.4367 - pThis->idxParent = idx;
1.4368 - }
1.4369 - sqlite3PagerUnref(pDbPage);
1.4370 - }
1.4371 -
1.4372 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4373 - if( pBt->autoVacuum ){
1.4374 - return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
1.4375 - }
1.4376 -#endif
1.4377 - return SQLITE_OK;
1.4378 -}
1.4379 -
1.4380 -
1.4381 -
1.4382 -/*
1.4383 -** Change the pParent pointer of all children of pPage to point back
1.4384 -** to pPage.
1.4385 -**
1.4386 -** In other words, for every child of pPage, invoke reparentPage()
1.4387 -** to make sure that each child knows that pPage is its parent.
1.4388 -**
1.4389 -** This routine gets called after you memcpy() one page into
1.4390 -** another.
1.4391 -*/
1.4392 -static int reparentChildPages(MemPage *pPage){
1.4393 - int i;
1.4394 - BtShared *pBt = pPage->pBt;
1.4395 - int rc = SQLITE_OK;
1.4396 -
1.4397 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4398 - if( pPage->leaf ) return SQLITE_OK;
1.4399 -
1.4400 - for(i=0; i<pPage->nCell; i++){
1.4401 - u8 *pCell = findCell(pPage, i);
1.4402 - if( !pPage->leaf ){
1.4403 - rc = reparentPage(pBt, get4byte(pCell), pPage, i);
1.4404 - if( rc!=SQLITE_OK ) return rc;
1.4405 - }
1.4406 - }
1.4407 - if( !pPage->leaf ){
1.4408 - rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
1.4409 - pPage, i);
1.4410 - pPage->idxShift = 0;
1.4411 - }
1.4412 - return rc;
1.4413 -}
1.4414 -
1.4415 -/*
1.4416 -** Remove the i-th cell from pPage. This routine effects pPage only.
1.4417 -** The cell content is not freed or deallocated. It is assumed that
1.4418 -** the cell content has been copied someplace else. This routine just
1.4419 -** removes the reference to the cell from pPage.
1.4420 -**
1.4421 -** "sz" must be the number of bytes in the cell.
1.4422 -*/
1.4423 -static void dropCell(MemPage *pPage, int idx, int sz){
1.4424 - int i; /* Loop counter */
1.4425 - int pc; /* Offset to cell content of cell being deleted */
1.4426 - u8 *data; /* pPage->aData */
1.4427 - u8 *ptr; /* Used to move bytes around within data[] */
1.4428 -
1.4429 - assert( idx>=0 && idx<pPage->nCell );
1.4430 - assert( sz==cellSize(pPage, idx) );
1.4431 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.4432 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4433 - data = pPage->aData;
1.4434 - ptr = &data[pPage->cellOffset + 2*idx];
1.4435 - pc = get2byte(ptr);
1.4436 - assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
1.4437 - freeSpace(pPage, pc, sz);
1.4438 - for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
1.4439 - ptr[0] = ptr[2];
1.4440 - ptr[1] = ptr[3];
1.4441 - }
1.4442 - pPage->nCell--;
1.4443 - put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
1.4444 - pPage->nFree += 2;
1.4445 - pPage->idxShift = 1;
1.4446 -}
1.4447 -
1.4448 -/*
1.4449 -** Insert a new cell on pPage at cell index "i". pCell points to the
1.4450 -** content of the cell.
1.4451 -**
1.4452 -** If the cell content will fit on the page, then put it there. If it
1.4453 -** will not fit, then make a copy of the cell content into pTemp if
1.4454 -** pTemp is not null. Regardless of pTemp, allocate a new entry
1.4455 -** in pPage->aOvfl[] and make it point to the cell content (either
1.4456 -** in pTemp or the original pCell) and also record its index.
1.4457 -** Allocating a new entry in pPage->aCell[] implies that
1.4458 -** pPage->nOverflow is incremented.
1.4459 -**
1.4460 -** If nSkip is non-zero, then do not copy the first nSkip bytes of the
1.4461 -** cell. The caller will overwrite them after this function returns. If
1.4462 -** nSkip is non-zero, then pCell may not point to an invalid memory location
1.4463 -** (but pCell+nSkip is always valid).
1.4464 -*/
1.4465 -static int insertCell(
1.4466 - MemPage *pPage, /* Page into which we are copying */
1.4467 - int i, /* New cell becomes the i-th cell of the page */
1.4468 - u8 *pCell, /* Content of the new cell */
1.4469 - int sz, /* Bytes of content in pCell */
1.4470 - u8 *pTemp, /* Temp storage space for pCell, if needed */
1.4471 - u8 nSkip /* Do not write the first nSkip bytes of the cell */
1.4472 -){
1.4473 - int idx; /* Where to write new cell content in data[] */
1.4474 - int j; /* Loop counter */
1.4475 - int top; /* First byte of content for any cell in data[] */
1.4476 - int end; /* First byte past the last cell pointer in data[] */
1.4477 - int ins; /* Index in data[] where new cell pointer is inserted */
1.4478 - int hdr; /* Offset into data[] of the page header */
1.4479 - int cellOffset; /* Address of first cell pointer in data[] */
1.4480 - u8 *data; /* The content of the whole page */
1.4481 - u8 *ptr; /* Used for moving information around in data[] */
1.4482 -
1.4483 - assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
1.4484 - assert( sz==cellSizePtr(pPage, pCell) );
1.4485 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4486 - if( pPage->nOverflow || sz+2>pPage->nFree ){
1.4487 - if( pTemp ){
1.4488 - memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
1.4489 - pCell = pTemp;
1.4490 - }
1.4491 - j = pPage->nOverflow++;
1.4492 - assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
1.4493 - pPage->aOvfl[j].pCell = pCell;
1.4494 - pPage->aOvfl[j].idx = i;
1.4495 - pPage->nFree = 0;
1.4496 - }else{
1.4497 - int rc = sqlite3PagerWrite(pPage->pDbPage);
1.4498 - if( rc!=SQLITE_OK ){
1.4499 - return rc;
1.4500 - }
1.4501 - assert( sqlite3PagerIswriteable(pPage->pDbPage) );
1.4502 - data = pPage->aData;
1.4503 - hdr = pPage->hdrOffset;
1.4504 - top = get2byte(&data[hdr+5]);
1.4505 - cellOffset = pPage->cellOffset;
1.4506 - end = cellOffset + 2*pPage->nCell + 2;
1.4507 - ins = cellOffset + 2*i;
1.4508 - if( end > top - sz ){
1.4509 - rc = defragmentPage(pPage);
1.4510 - if( rc!=SQLITE_OK ) return rc;
1.4511 - top = get2byte(&data[hdr+5]);
1.4512 - assert( end + sz <= top );
1.4513 - }
1.4514 - idx = allocateSpace(pPage, sz);
1.4515 - assert( idx>0 );
1.4516 - assert( end <= get2byte(&data[hdr+5]) );
1.4517 - pPage->nCell++;
1.4518 - pPage->nFree -= 2;
1.4519 - memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
1.4520 - for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
1.4521 - ptr[0] = ptr[-2];
1.4522 - ptr[1] = ptr[-1];
1.4523 - }
1.4524 - put2byte(&data[ins], idx);
1.4525 - put2byte(&data[hdr+3], pPage->nCell);
1.4526 - pPage->idxShift = 1;
1.4527 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4528 - if( pPage->pBt->autoVacuum ){
1.4529 - /* The cell may contain a pointer to an overflow page. If so, write
1.4530 - ** the entry for the overflow page into the pointer map.
1.4531 - */
1.4532 - CellInfo info;
1.4533 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.4534 - assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
1.4535 - if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
1.4536 - Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
1.4537 - rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
1.4538 - if( rc!=SQLITE_OK ) return rc;
1.4539 - }
1.4540 - }
1.4541 -#endif
1.4542 - }
1.4543 -
1.4544 - return SQLITE_OK;
1.4545 -}
1.4546 -
1.4547 -/*
1.4548 -** Add a list of cells to a page. The page should be initially empty.
1.4549 -** The cells are guaranteed to fit on the page.
1.4550 -*/
1.4551 -static void assemblePage(
1.4552 - MemPage *pPage, /* The page to be assemblied */
1.4553 - int nCell, /* The number of cells to add to this page */
1.4554 - u8 **apCell, /* Pointers to cell bodies */
1.4555 - int *aSize /* Sizes of the cells */
1.4556 -){
1.4557 - int i; /* Loop counter */
1.4558 - int totalSize; /* Total size of all cells */
1.4559 - int hdr; /* Index of page header */
1.4560 - int cellptr; /* Address of next cell pointer */
1.4561 - int cellbody; /* Address of next cell body */
1.4562 - u8 *data; /* Data for the page */
1.4563 -
1.4564 - assert( pPage->nOverflow==0 );
1.4565 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4566 - totalSize = 0;
1.4567 - for(i=0; i<nCell; i++){
1.4568 - totalSize += aSize[i];
1.4569 - }
1.4570 - assert( totalSize+2*nCell<=pPage->nFree );
1.4571 - assert( pPage->nCell==0 );
1.4572 - cellptr = pPage->cellOffset;
1.4573 - data = pPage->aData;
1.4574 - hdr = pPage->hdrOffset;
1.4575 - put2byte(&data[hdr+3], nCell);
1.4576 - if( nCell ){
1.4577 - cellbody = allocateSpace(pPage, totalSize);
1.4578 - assert( cellbody>0 );
1.4579 - assert( pPage->nFree >= 2*nCell );
1.4580 - pPage->nFree -= 2*nCell;
1.4581 - for(i=0; i<nCell; i++){
1.4582 - put2byte(&data[cellptr], cellbody);
1.4583 - memcpy(&data[cellbody], apCell[i], aSize[i]);
1.4584 - cellptr += 2;
1.4585 - cellbody += aSize[i];
1.4586 - }
1.4587 - assert( cellbody==pPage->pBt->usableSize );
1.4588 - }
1.4589 - pPage->nCell = nCell;
1.4590 -}
1.4591 -
1.4592 -/*
1.4593 -** The following parameters determine how many adjacent pages get involved
1.4594 -** in a balancing operation. NN is the number of neighbors on either side
1.4595 -** of the page that participate in the balancing operation. NB is the
1.4596 -** total number of pages that participate, including the target page and
1.4597 -** NN neighbors on either side.
1.4598 -**
1.4599 -** The minimum value of NN is 1 (of course). Increasing NN above 1
1.4600 -** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
1.4601 -** in exchange for a larger degradation in INSERT and UPDATE performance.
1.4602 -** The value of NN appears to give the best results overall.
1.4603 -*/
1.4604 -#define NN 1 /* Number of neighbors on either side of pPage */
1.4605 -#define NB (NN*2+1) /* Total pages involved in the balance */
1.4606 -
1.4607 -/* Forward reference */
1.4608 -static int balance(MemPage*, int);
1.4609 -
1.4610 -#ifndef SQLITE_OMIT_QUICKBALANCE
1.4611 -/*
1.4612 -** This version of balance() handles the common special case where
1.4613 -** a new entry is being inserted on the extreme right-end of the
1.4614 -** tree, in other words, when the new entry will become the largest
1.4615 -** entry in the tree.
1.4616 -**
1.4617 -** Instead of trying balance the 3 right-most leaf pages, just add
1.4618 -** a new page to the right-hand side and put the one new entry in
1.4619 -** that page. This leaves the right side of the tree somewhat
1.4620 -** unbalanced. But odds are that we will be inserting new entries
1.4621 -** at the end soon afterwards so the nearly empty page will quickly
1.4622 -** fill up. On average.
1.4623 -**
1.4624 -** pPage is the leaf page which is the right-most page in the tree.
1.4625 -** pParent is its parent. pPage must have a single overflow entry
1.4626 -** which is also the right-most entry on the page.
1.4627 -*/
1.4628 -static int balance_quick(MemPage *pPage, MemPage *pParent){
1.4629 - int rc;
1.4630 - MemPage *pNew;
1.4631 - Pgno pgnoNew;
1.4632 - u8 *pCell;
1.4633 - int szCell;
1.4634 - CellInfo info;
1.4635 - BtShared *pBt = pPage->pBt;
1.4636 - int parentIdx = pParent->nCell; /* pParent new divider cell index */
1.4637 - int parentSize; /* Size of new divider cell */
1.4638 - u8 parentCell[64]; /* Space for the new divider cell */
1.4639 -
1.4640 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4641 -
1.4642 - /* Allocate a new page. Insert the overflow cell from pPage
1.4643 - ** into it. Then remove the overflow cell from pPage.
1.4644 - */
1.4645 - rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
1.4646 - if( rc!=SQLITE_OK ){
1.4647 - return rc;
1.4648 - }
1.4649 - pCell = pPage->aOvfl[0].pCell;
1.4650 - szCell = cellSizePtr(pPage, pCell);
1.4651 - zeroPage(pNew, pPage->aData[0]);
1.4652 - assemblePage(pNew, 1, &pCell, &szCell);
1.4653 - pPage->nOverflow = 0;
1.4654 -
1.4655 - /* Set the parent of the newly allocated page to pParent. */
1.4656 - pNew->pParent = pParent;
1.4657 - sqlite3PagerRef(pParent->pDbPage);
1.4658 -
1.4659 - /* pPage is currently the right-child of pParent. Change this
1.4660 - ** so that the right-child is the new page allocated above and
1.4661 - ** pPage is the next-to-right child.
1.4662 - */
1.4663 - assert( pPage->nCell>0 );
1.4664 - pCell = findCell(pPage, pPage->nCell-1);
1.4665 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.4666 - rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
1.4667 - if( rc!=SQLITE_OK ){
1.4668 - return rc;
1.4669 - }
1.4670 - assert( parentSize<64 );
1.4671 - rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
1.4672 - if( rc!=SQLITE_OK ){
1.4673 - return rc;
1.4674 - }
1.4675 - put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
1.4676 - put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
1.4677 -
1.4678 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4679 - /* If this is an auto-vacuum database, update the pointer map
1.4680 - ** with entries for the new page, and any pointer from the
1.4681 - ** cell on the page to an overflow page.
1.4682 - */
1.4683 - if( pBt->autoVacuum ){
1.4684 - rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
1.4685 - if( rc==SQLITE_OK ){
1.4686 - rc = ptrmapPutOvfl(pNew, 0);
1.4687 - }
1.4688 - if( rc!=SQLITE_OK ){
1.4689 - releasePage(pNew);
1.4690 - return rc;
1.4691 - }
1.4692 - }
1.4693 -#endif
1.4694 -
1.4695 - /* Release the reference to the new page and balance the parent page,
1.4696 - ** in case the divider cell inserted caused it to become overfull.
1.4697 - */
1.4698 - releasePage(pNew);
1.4699 - return balance(pParent, 0);
1.4700 -}
1.4701 -#endif /* SQLITE_OMIT_QUICKBALANCE */
1.4702 -
1.4703 -/*
1.4704 -** This routine redistributes Cells on pPage and up to NN*2 siblings
1.4705 -** of pPage so that all pages have about the same amount of free space.
1.4706 -** Usually NN siblings on either side of pPage is used in the balancing,
1.4707 -** though more siblings might come from one side if pPage is the first
1.4708 -** or last child of its parent. If pPage has fewer than 2*NN siblings
1.4709 -** (something which can only happen if pPage is the root page or a
1.4710 -** child of root) then all available siblings participate in the balancing.
1.4711 -**
1.4712 -** The number of siblings of pPage might be increased or decreased by one or
1.4713 -** two in an effort to keep pages nearly full but not over full. The root page
1.4714 -** is special and is allowed to be nearly empty. If pPage is
1.4715 -** the root page, then the depth of the tree might be increased
1.4716 -** or decreased by one, as necessary, to keep the root page from being
1.4717 -** overfull or completely empty.
1.4718 -**
1.4719 -** Note that when this routine is called, some of the Cells on pPage
1.4720 -** might not actually be stored in pPage->aData[]. This can happen
1.4721 -** if the page is overfull. Part of the job of this routine is to
1.4722 -** make sure all Cells for pPage once again fit in pPage->aData[].
1.4723 -**
1.4724 -** In the course of balancing the siblings of pPage, the parent of pPage
1.4725 -** might become overfull or underfull. If that happens, then this routine
1.4726 -** is called recursively on the parent.
1.4727 -**
1.4728 -** If this routine fails for any reason, it might leave the database
1.4729 -** in a corrupted state. So if this routine fails, the database should
1.4730 -** be rolled back.
1.4731 -*/
1.4732 -static int balance_nonroot(MemPage *pPage){
1.4733 - MemPage *pParent; /* The parent of pPage */
1.4734 - BtShared *pBt; /* The whole database */
1.4735 - int nCell = 0; /* Number of cells in apCell[] */
1.4736 - int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
1.4737 - int nOld; /* Number of pages in apOld[] */
1.4738 - int nNew; /* Number of pages in apNew[] */
1.4739 - int nDiv; /* Number of cells in apDiv[] */
1.4740 - int i, j, k; /* Loop counters */
1.4741 - int idx; /* Index of pPage in pParent->aCell[] */
1.4742 - int nxDiv; /* Next divider slot in pParent->aCell[] */
1.4743 - int rc; /* The return code */
1.4744 - int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
1.4745 - int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
1.4746 - int usableSpace; /* Bytes in pPage beyond the header */
1.4747 - int pageFlags; /* Value of pPage->aData[0] */
1.4748 - int subtotal; /* Subtotal of bytes in cells on one page */
1.4749 - int iSpace = 0; /* First unused byte of aSpace[] */
1.4750 - MemPage *apOld[NB]; /* pPage and up to two siblings */
1.4751 - Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
1.4752 - MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
1.4753 - MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
1.4754 - Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
1.4755 - u8 *apDiv[NB]; /* Divider cells in pParent */
1.4756 - int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
1.4757 - int szNew[NB+2]; /* Combined size of cells place on i-th page */
1.4758 - u8 **apCell = 0; /* All cells begin balanced */
1.4759 - int *szCell; /* Local size of all cells in apCell[] */
1.4760 - u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
1.4761 - u8 *aSpace; /* Space to hold copies of dividers cells */
1.4762 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4763 - u8 *aFrom = 0;
1.4764 -#endif
1.4765 -
1.4766 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.4767 -
1.4768 - /*
1.4769 - ** Find the parent page.
1.4770 - */
1.4771 - assert( pPage->isInit );
1.4772 - assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
1.4773 - pBt = pPage->pBt;
1.4774 - pParent = pPage->pParent;
1.4775 - assert( pParent );
1.4776 - if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
1.4777 - return rc;
1.4778 - }
1.4779 - TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
1.4780 -
1.4781 -#ifndef SQLITE_OMIT_QUICKBALANCE
1.4782 - /*
1.4783 - ** A special case: If a new entry has just been inserted into a
1.4784 - ** table (that is, a btree with integer keys and all data at the leaves)
1.4785 - ** and the new entry is the right-most entry in the tree (it has the
1.4786 - ** largest key) then use the special balance_quick() routine for
1.4787 - ** balancing. balance_quick() is much faster and results in a tighter
1.4788 - ** packing of data in the common case.
1.4789 - */
1.4790 - if( pPage->leaf &&
1.4791 - pPage->intKey &&
1.4792 - pPage->leafData &&
1.4793 - pPage->nOverflow==1 &&
1.4794 - pPage->aOvfl[0].idx==pPage->nCell &&
1.4795 - pPage->pParent->pgno!=1 &&
1.4796 - get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
1.4797 - ){
1.4798 - /*
1.4799 - ** TODO: Check the siblings to the left of pPage. It may be that
1.4800 - ** they are not full and no new page is required.
1.4801 - */
1.4802 - return balance_quick(pPage, pParent);
1.4803 - }
1.4804 -#endif
1.4805 -
1.4806 - if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
1.4807 - return rc;
1.4808 - }
1.4809 -
1.4810 - /*
1.4811 - ** Find the cell in the parent page whose left child points back
1.4812 - ** to pPage. The "idx" variable is the index of that cell. If pPage
1.4813 - ** is the rightmost child of pParent then set idx to pParent->nCell
1.4814 - */
1.4815 - if( pParent->idxShift ){
1.4816 - Pgno pgno;
1.4817 - pgno = pPage->pgno;
1.4818 - assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
1.4819 - for(idx=0; idx<pParent->nCell; idx++){
1.4820 - if( get4byte(findCell(pParent, idx))==pgno ){
1.4821 - break;
1.4822 - }
1.4823 - }
1.4824 - assert( idx<pParent->nCell
1.4825 - || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
1.4826 - }else{
1.4827 - idx = pPage->idxParent;
1.4828 - }
1.4829 -
1.4830 - /*
1.4831 - ** Initialize variables so that it will be safe to jump
1.4832 - ** directly to balance_cleanup at any moment.
1.4833 - */
1.4834 - nOld = nNew = 0;
1.4835 - sqlite3PagerRef(pParent->pDbPage);
1.4836 -
1.4837 - /*
1.4838 - ** Find sibling pages to pPage and the cells in pParent that divide
1.4839 - ** the siblings. An attempt is made to find NN siblings on either
1.4840 - ** side of pPage. More siblings are taken from one side, however, if
1.4841 - ** pPage there are fewer than NN siblings on the other side. If pParent
1.4842 - ** has NB or fewer children then all children of pParent are taken.
1.4843 - */
1.4844 - nxDiv = idx - NN;
1.4845 - if( nxDiv + NB > pParent->nCell ){
1.4846 - nxDiv = pParent->nCell - NB + 1;
1.4847 - }
1.4848 - if( nxDiv<0 ){
1.4849 - nxDiv = 0;
1.4850 - }
1.4851 - nDiv = 0;
1.4852 - for(i=0, k=nxDiv; i<NB; i++, k++){
1.4853 - if( k<pParent->nCell ){
1.4854 - apDiv[i] = findCell(pParent, k);
1.4855 - nDiv++;
1.4856 - assert( !pParent->leaf );
1.4857 - pgnoOld[i] = get4byte(apDiv[i]);
1.4858 - }else if( k==pParent->nCell ){
1.4859 - pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
1.4860 - }else{
1.4861 - break;
1.4862 - }
1.4863 - rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
1.4864 - if( rc ) goto balance_cleanup;
1.4865 - apOld[i]->idxParent = k;
1.4866 - apCopy[i] = 0;
1.4867 - assert( i==nOld );
1.4868 - nOld++;
1.4869 - nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
1.4870 - }
1.4871 -
1.4872 - /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
1.4873 - ** alignment */
1.4874 - nMaxCells = (nMaxCells + 1)&~1;
1.4875 -
1.4876 - /*
1.4877 - ** Allocate space for memory structures
1.4878 - */
1.4879 - apCell = sqlite3_malloc(
1.4880 - nMaxCells*sizeof(u8*) /* apCell */
1.4881 - + nMaxCells*sizeof(int) /* szCell */
1.4882 - + ROUND8(sizeof(MemPage))*NB /* aCopy */
1.4883 - + pBt->pageSize*(5+NB) /* aSpace */
1.4884 - + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
1.4885 - );
1.4886 - if( apCell==0 ){
1.4887 - rc = SQLITE_NOMEM;
1.4888 - goto balance_cleanup;
1.4889 - }
1.4890 - szCell = (int*)&apCell[nMaxCells];
1.4891 - aCopy[0] = (u8*)&szCell[nMaxCells];
1.4892 - assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
1.4893 - for(i=1; i<NB; i++){
1.4894 - aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
1.4895 - assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
1.4896 - }
1.4897 - aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
1.4898 - assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
1.4899 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4900 - if( pBt->autoVacuum ){
1.4901 - aFrom = &aSpace[5*pBt->pageSize];
1.4902 - }
1.4903 -#endif
1.4904 -
1.4905 - /*
1.4906 - ** Make copies of the content of pPage and its siblings into aOld[].
1.4907 - ** The rest of this function will use data from the copies rather
1.4908 - ** that the original pages since the original pages will be in the
1.4909 - ** process of being overwritten.
1.4910 - */
1.4911 - for(i=0; i<nOld; i++){
1.4912 - MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
1.4913 - memcpy(p, apOld[i], sizeof(MemPage));
1.4914 - p->aData = (void*)&p[1];
1.4915 - memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
1.4916 - }
1.4917 -
1.4918 - /*
1.4919 - ** Load pointers to all cells on sibling pages and the divider cells
1.4920 - ** into the local apCell[] array. Make copies of the divider cells
1.4921 - ** into space obtained form aSpace[] and remove the the divider Cells
1.4922 - ** from pParent.
1.4923 - **
1.4924 - ** If the siblings are on leaf pages, then the child pointers of the
1.4925 - ** divider cells are stripped from the cells before they are copied
1.4926 - ** into aSpace[]. In this way, all cells in apCell[] are without
1.4927 - ** child pointers. If siblings are not leaves, then all cell in
1.4928 - ** apCell[] include child pointers. Either way, all cells in apCell[]
1.4929 - ** are alike.
1.4930 - **
1.4931 - ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
1.4932 - ** leafData: 1 if pPage holds key+data and pParent holds only keys.
1.4933 - */
1.4934 - nCell = 0;
1.4935 - leafCorrection = pPage->leaf*4;
1.4936 - leafData = pPage->leafData && pPage->leaf;
1.4937 - for(i=0; i<nOld; i++){
1.4938 - MemPage *pOld = apCopy[i];
1.4939 - int limit = pOld->nCell+pOld->nOverflow;
1.4940 - for(j=0; j<limit; j++){
1.4941 - assert( nCell<nMaxCells );
1.4942 - apCell[nCell] = findOverflowCell(pOld, j);
1.4943 - szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
1.4944 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4945 - if( pBt->autoVacuum ){
1.4946 - int a;
1.4947 - aFrom[nCell] = i;
1.4948 - for(a=0; a<pOld->nOverflow; a++){
1.4949 - if( pOld->aOvfl[a].pCell==apCell[nCell] ){
1.4950 - aFrom[nCell] = 0xFF;
1.4951 - break;
1.4952 - }
1.4953 - }
1.4954 - }
1.4955 -#endif
1.4956 - nCell++;
1.4957 - }
1.4958 - if( i<nOld-1 ){
1.4959 - int sz = cellSizePtr(pParent, apDiv[i]);
1.4960 - if( leafData ){
1.4961 - /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
1.4962 - ** are duplicates of keys on the child pages. We need to remove
1.4963 - ** the divider cells from pParent, but the dividers cells are not
1.4964 - ** added to apCell[] because they are duplicates of child cells.
1.4965 - */
1.4966 - dropCell(pParent, nxDiv, sz);
1.4967 - }else{
1.4968 - u8 *pTemp;
1.4969 - assert( nCell<nMaxCells );
1.4970 - szCell[nCell] = sz;
1.4971 - pTemp = &aSpace[iSpace];
1.4972 - iSpace += sz;
1.4973 - assert( iSpace<=pBt->pageSize*5 );
1.4974 - memcpy(pTemp, apDiv[i], sz);
1.4975 - apCell[nCell] = pTemp+leafCorrection;
1.4976 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.4977 - if( pBt->autoVacuum ){
1.4978 - aFrom[nCell] = 0xFF;
1.4979 - }
1.4980 -#endif
1.4981 - dropCell(pParent, nxDiv, sz);
1.4982 - szCell[nCell] -= leafCorrection;
1.4983 - assert( get4byte(pTemp)==pgnoOld[i] );
1.4984 - if( !pOld->leaf ){
1.4985 - assert( leafCorrection==0 );
1.4986 - /* The right pointer of the child page pOld becomes the left
1.4987 - ** pointer of the divider cell */
1.4988 - memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
1.4989 - }else{
1.4990 - assert( leafCorrection==4 );
1.4991 - if( szCell[nCell]<4 ){
1.4992 - /* Do not allow any cells smaller than 4 bytes. */
1.4993 - szCell[nCell] = 4;
1.4994 - }
1.4995 - }
1.4996 - nCell++;
1.4997 - }
1.4998 - }
1.4999 - }
1.5000 -
1.5001 - /*
1.5002 - ** Figure out the number of pages needed to hold all nCell cells.
1.5003 - ** Store this number in "k". Also compute szNew[] which is the total
1.5004 - ** size of all cells on the i-th page and cntNew[] which is the index
1.5005 - ** in apCell[] of the cell that divides page i from page i+1.
1.5006 - ** cntNew[k] should equal nCell.
1.5007 - **
1.5008 - ** Values computed by this block:
1.5009 - **
1.5010 - ** k: The total number of sibling pages
1.5011 - ** szNew[i]: Spaced used on the i-th sibling page.
1.5012 - ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
1.5013 - ** the right of the i-th sibling page.
1.5014 - ** usableSpace: Number of bytes of space available on each sibling.
1.5015 - **
1.5016 - */
1.5017 - usableSpace = pBt->usableSize - 12 + leafCorrection;
1.5018 - for(subtotal=k=i=0; i<nCell; i++){
1.5019 - assert( i<nMaxCells );
1.5020 - subtotal += szCell[i] + 2;
1.5021 - if( subtotal > usableSpace ){
1.5022 - szNew[k] = subtotal - szCell[i];
1.5023 - cntNew[k] = i;
1.5024 - if( leafData ){ i--; }
1.5025 - subtotal = 0;
1.5026 - k++;
1.5027 - }
1.5028 - }
1.5029 - szNew[k] = subtotal;
1.5030 - cntNew[k] = nCell;
1.5031 - k++;
1.5032 -
1.5033 - /*
1.5034 - ** The packing computed by the previous block is biased toward the siblings
1.5035 - ** on the left side. The left siblings are always nearly full, while the
1.5036 - ** right-most sibling might be nearly empty. This block of code attempts
1.5037 - ** to adjust the packing of siblings to get a better balance.
1.5038 - **
1.5039 - ** This adjustment is more than an optimization. The packing above might
1.5040 - ** be so out of balance as to be illegal. For example, the right-most
1.5041 - ** sibling might be completely empty. This adjustment is not optional.
1.5042 - */
1.5043 - for(i=k-1; i>0; i--){
1.5044 - int szRight = szNew[i]; /* Size of sibling on the right */
1.5045 - int szLeft = szNew[i-1]; /* Size of sibling on the left */
1.5046 - int r; /* Index of right-most cell in left sibling */
1.5047 - int d; /* Index of first cell to the left of right sibling */
1.5048 -
1.5049 - r = cntNew[i-1] - 1;
1.5050 - d = r + 1 - leafData;
1.5051 - assert( d<nMaxCells );
1.5052 - assert( r<nMaxCells );
1.5053 - while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
1.5054 - szRight += szCell[d] + 2;
1.5055 - szLeft -= szCell[r] + 2;
1.5056 - cntNew[i-1]--;
1.5057 - r = cntNew[i-1] - 1;
1.5058 - d = r + 1 - leafData;
1.5059 - }
1.5060 - szNew[i] = szRight;
1.5061 - szNew[i-1] = szLeft;
1.5062 - }
1.5063 -
1.5064 - /* Either we found one or more cells (cntnew[0])>0) or we are the
1.5065 - ** a virtual root page. A virtual root page is when the real root
1.5066 - ** page is page 1 and we are the only child of that page.
1.5067 - */
1.5068 - assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
1.5069 -
1.5070 - /*
1.5071 - ** Allocate k new pages. Reuse old pages where possible.
1.5072 - */
1.5073 - assert( pPage->pgno>1 );
1.5074 - pageFlags = pPage->aData[0];
1.5075 - for(i=0; i<k; i++){
1.5076 - MemPage *pNew;
1.5077 - if( i<nOld ){
1.5078 - pNew = apNew[i] = apOld[i];
1.5079 - pgnoNew[i] = pgnoOld[i];
1.5080 - apOld[i] = 0;
1.5081 - rc = sqlite3PagerWrite(pNew->pDbPage);
1.5082 - nNew++;
1.5083 - if( rc ) goto balance_cleanup;
1.5084 - }else{
1.5085 - assert( i>0 );
1.5086 - rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
1.5087 - if( rc ) goto balance_cleanup;
1.5088 - apNew[i] = pNew;
1.5089 - nNew++;
1.5090 - }
1.5091 - zeroPage(pNew, pageFlags);
1.5092 - }
1.5093 -
1.5094 - /* Free any old pages that were not reused as new pages.
1.5095 - */
1.5096 - while( i<nOld ){
1.5097 - rc = freePage(apOld[i]);
1.5098 - if( rc ) goto balance_cleanup;
1.5099 - releasePage(apOld[i]);
1.5100 - apOld[i] = 0;
1.5101 - i++;
1.5102 - }
1.5103 -
1.5104 - /*
1.5105 - ** Put the new pages in accending order. This helps to
1.5106 - ** keep entries in the disk file in order so that a scan
1.5107 - ** of the table is a linear scan through the file. That
1.5108 - ** in turn helps the operating system to deliver pages
1.5109 - ** from the disk more rapidly.
1.5110 - **
1.5111 - ** An O(n^2) insertion sort algorithm is used, but since
1.5112 - ** n is never more than NB (a small constant), that should
1.5113 - ** not be a problem.
1.5114 - **
1.5115 - ** When NB==3, this one optimization makes the database
1.5116 - ** about 25% faster for large insertions and deletions.
1.5117 - */
1.5118 - for(i=0; i<k-1; i++){
1.5119 - int minV = pgnoNew[i];
1.5120 - int minI = i;
1.5121 - for(j=i+1; j<k; j++){
1.5122 - if( pgnoNew[j]<(unsigned)minV ){
1.5123 - minI = j;
1.5124 - minV = pgnoNew[j];
1.5125 - }
1.5126 - }
1.5127 - if( minI>i ){
1.5128 - int t;
1.5129 - MemPage *pT;
1.5130 - t = pgnoNew[i];
1.5131 - pT = apNew[i];
1.5132 - pgnoNew[i] = pgnoNew[minI];
1.5133 - apNew[i] = apNew[minI];
1.5134 - pgnoNew[minI] = t;
1.5135 - apNew[minI] = pT;
1.5136 - }
1.5137 - }
1.5138 - TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
1.5139 - pgnoOld[0],
1.5140 - nOld>=2 ? pgnoOld[1] : 0,
1.5141 - nOld>=3 ? pgnoOld[2] : 0,
1.5142 - pgnoNew[0], szNew[0],
1.5143 - nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
1.5144 - nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
1.5145 - nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
1.5146 - nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
1.5147 -
1.5148 - /*
1.5149 - ** Evenly distribute the data in apCell[] across the new pages.
1.5150 - ** Insert divider cells into pParent as necessary.
1.5151 - */
1.5152 - j = 0;
1.5153 - for(i=0; i<nNew; i++){
1.5154 - /* Assemble the new sibling page. */
1.5155 - MemPage *pNew = apNew[i];
1.5156 - assert( j<nMaxCells );
1.5157 - assert( pNew->pgno==pgnoNew[i] );
1.5158 - assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
1.5159 - assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
1.5160 - assert( pNew->nOverflow==0 );
1.5161 -
1.5162 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.5163 - /* If this is an auto-vacuum database, update the pointer map entries
1.5164 - ** that point to the siblings that were rearranged. These can be: left
1.5165 - ** children of cells, the right-child of the page, or overflow pages
1.5166 - ** pointed to by cells.
1.5167 - */
1.5168 - if( pBt->autoVacuum ){
1.5169 - for(k=j; k<cntNew[i]; k++){
1.5170 - assert( k<nMaxCells );
1.5171 - if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
1.5172 - rc = ptrmapPutOvfl(pNew, k-j);
1.5173 - if( rc!=SQLITE_OK ){
1.5174 - goto balance_cleanup;
1.5175 - }
1.5176 - }
1.5177 - }
1.5178 - }
1.5179 -#endif
1.5180 -
1.5181 - j = cntNew[i];
1.5182 -
1.5183 - /* If the sibling page assembled above was not the right-most sibling,
1.5184 - ** insert a divider cell into the parent page.
1.5185 - */
1.5186 - if( i<nNew-1 && j<nCell ){
1.5187 - u8 *pCell;
1.5188 - u8 *pTemp;
1.5189 - int sz;
1.5190 -
1.5191 - assert( j<nMaxCells );
1.5192 - pCell = apCell[j];
1.5193 - sz = szCell[j] + leafCorrection;
1.5194 - if( !pNew->leaf ){
1.5195 - memcpy(&pNew->aData[8], pCell, 4);
1.5196 - pTemp = 0;
1.5197 - }else if( leafData ){
1.5198 - /* If the tree is a leaf-data tree, and the siblings are leaves,
1.5199 - ** then there is no divider cell in apCell[]. Instead, the divider
1.5200 - ** cell consists of the integer key for the right-most cell of
1.5201 - ** the sibling-page assembled above only.
1.5202 - */
1.5203 - CellInfo info;
1.5204 - j--;
1.5205 - sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
1.5206 - pCell = &aSpace[iSpace];
1.5207 - fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
1.5208 - iSpace += sz;
1.5209 - assert( iSpace<=pBt->pageSize*5 );
1.5210 - pTemp = 0;
1.5211 - }else{
1.5212 - pCell -= 4;
1.5213 - pTemp = &aSpace[iSpace];
1.5214 - iSpace += sz;
1.5215 - assert( iSpace<=pBt->pageSize*5 );
1.5216 - /* Obscure case for non-leaf-data trees: If the cell at pCell was
1.5217 - ** previously stored on a leaf node, and it's reported size was 4
1.5218 - ** bytes, then it may actually be smaller than this
1.5219 - ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
1.5220 - ** any cell). But it's important to pass the correct size to
1.5221 - ** insertCell(), so reparse the cell now.
1.5222 - **
1.5223 - ** Note that this can never happen in an SQLite data file, as all
1.5224 - ** cells are at least 4 bytes. It only happens in b-trees used
1.5225 - ** to evaluate "IN (SELECT ...)" and similar clauses.
1.5226 - */
1.5227 - if( szCell[j]==4 ){
1.5228 - assert(leafCorrection==4);
1.5229 - sz = cellSizePtr(pParent, pCell);
1.5230 - }
1.5231 - }
1.5232 - rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
1.5233 - if( rc!=SQLITE_OK ) goto balance_cleanup;
1.5234 - put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
1.5235 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.5236 - /* If this is an auto-vacuum database, and not a leaf-data tree,
1.5237 - ** then update the pointer map with an entry for the overflow page
1.5238 - ** that the cell just inserted points to (if any).
1.5239 - */
1.5240 - if( pBt->autoVacuum && !leafData ){
1.5241 - rc = ptrmapPutOvfl(pParent, nxDiv);
1.5242 - if( rc!=SQLITE_OK ){
1.5243 - goto balance_cleanup;
1.5244 - }
1.5245 - }
1.5246 -#endif
1.5247 - j++;
1.5248 - nxDiv++;
1.5249 - }
1.5250 - }
1.5251 - assert( j==nCell );
1.5252 - assert( nOld>0 );
1.5253 - assert( nNew>0 );
1.5254 - if( (pageFlags & PTF_LEAF)==0 ){
1.5255 - memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
1.5256 - }
1.5257 - if( nxDiv==pParent->nCell+pParent->nOverflow ){
1.5258 - /* Right-most sibling is the right-most child of pParent */
1.5259 - put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
1.5260 - }else{
1.5261 - /* Right-most sibling is the left child of the first entry in pParent
1.5262 - ** past the right-most divider entry */
1.5263 - put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
1.5264 - }
1.5265 -
1.5266 - /*
1.5267 - ** Reparent children of all cells.
1.5268 - */
1.5269 - for(i=0; i<nNew; i++){
1.5270 - rc = reparentChildPages(apNew[i]);
1.5271 - if( rc!=SQLITE_OK ) goto balance_cleanup;
1.5272 - }
1.5273 - rc = reparentChildPages(pParent);
1.5274 - if( rc!=SQLITE_OK ) goto balance_cleanup;
1.5275 -
1.5276 - /*
1.5277 - ** Balance the parent page. Note that the current page (pPage) might
1.5278 - ** have been added to the freelist so it might no longer be initialized.
1.5279 - ** But the parent page will always be initialized.
1.5280 - */
1.5281 - assert( pParent->isInit );
1.5282 - rc = balance(pParent, 0);
1.5283 -
1.5284 - /*
1.5285 - ** Cleanup before returning.
1.5286 - */
1.5287 -balance_cleanup:
1.5288 - sqlite3_free(apCell);
1.5289 - for(i=0; i<nOld; i++){
1.5290 - releasePage(apOld[i]);
1.5291 - }
1.5292 - for(i=0; i<nNew; i++){
1.5293 - releasePage(apNew[i]);
1.5294 - }
1.5295 - releasePage(pParent);
1.5296 - TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
1.5297 - pPage->pgno, nOld, nNew, nCell));
1.5298 - return rc;
1.5299 -}
1.5300 -
1.5301 -/*
1.5302 -** This routine is called for the root page of a btree when the root
1.5303 -** page contains no cells. This is an opportunity to make the tree
1.5304 -** shallower by one level.
1.5305 -*/
1.5306 -static int balance_shallower(MemPage *pPage){
1.5307 - MemPage *pChild; /* The only child page of pPage */
1.5308 - Pgno pgnoChild; /* Page number for pChild */
1.5309 - int rc = SQLITE_OK; /* Return code from subprocedures */
1.5310 - BtShared *pBt; /* The main BTree structure */
1.5311 - int mxCellPerPage; /* Maximum number of cells per page */
1.5312 - u8 **apCell; /* All cells from pages being balanced */
1.5313 - int *szCell; /* Local size of all cells */
1.5314 -
1.5315 - assert( pPage->pParent==0 );
1.5316 - assert( pPage->nCell==0 );
1.5317 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.5318 - pBt = pPage->pBt;
1.5319 - mxCellPerPage = MX_CELL(pBt);
1.5320 - apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
1.5321 - if( apCell==0 ) return SQLITE_NOMEM;
1.5322 - szCell = (int*)&apCell[mxCellPerPage];
1.5323 - if( pPage->leaf ){
1.5324 - /* The table is completely empty */
1.5325 - TRACE(("BALANCE: empty table %d\n", pPage->pgno));
1.5326 - }else{
1.5327 - /* The root page is empty but has one child. Transfer the
1.5328 - ** information from that one child into the root page if it
1.5329 - ** will fit. This reduces the depth of the tree by one.
1.5330 - **
1.5331 - ** If the root page is page 1, it has less space available than
1.5332 - ** its child (due to the 100 byte header that occurs at the beginning
1.5333 - ** of the database fle), so it might not be able to hold all of the
1.5334 - ** information currently contained in the child. If this is the
1.5335 - ** case, then do not do the transfer. Leave page 1 empty except
1.5336 - ** for the right-pointer to the child page. The child page becomes
1.5337 - ** the virtual root of the tree.
1.5338 - */
1.5339 - pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1.5340 - assert( pgnoChild>0 );
1.5341 - assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
1.5342 - rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
1.5343 - if( rc ) goto end_shallow_balance;
1.5344 - if( pPage->pgno==1 ){
1.5345 - rc = sqlite3BtreeInitPage(pChild, pPage);
1.5346 - if( rc ) goto end_shallow_balance;
1.5347 - assert( pChild->nOverflow==0 );
1.5348 - if( pChild->nFree>=100 ){
1.5349 - /* The child information will fit on the root page, so do the
1.5350 - ** copy */
1.5351 - int i;
1.5352 - zeroPage(pPage, pChild->aData[0]);
1.5353 - for(i=0; i<pChild->nCell; i++){
1.5354 - apCell[i] = findCell(pChild,i);
1.5355 - szCell[i] = cellSizePtr(pChild, apCell[i]);
1.5356 - }
1.5357 - assemblePage(pPage, pChild->nCell, apCell, szCell);
1.5358 - /* Copy the right-pointer of the child to the parent. */
1.5359 - put4byte(&pPage->aData[pPage->hdrOffset+8],
1.5360 - get4byte(&pChild->aData[pChild->hdrOffset+8]));
1.5361 - freePage(pChild);
1.5362 - TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
1.5363 - }else{
1.5364 - /* The child has more information that will fit on the root.
1.5365 - ** The tree is already balanced. Do nothing. */
1.5366 - TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
1.5367 - }
1.5368 - }else{
1.5369 - memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
1.5370 - pPage->isInit = 0;
1.5371 - pPage->pParent = 0;
1.5372 - rc = sqlite3BtreeInitPage(pPage, 0);
1.5373 - assert( rc==SQLITE_OK );
1.5374 - freePage(pChild);
1.5375 - TRACE(("BALANCE: transfer child %d into root %d\n",
1.5376 - pChild->pgno, pPage->pgno));
1.5377 - }
1.5378 - rc = reparentChildPages(pPage);
1.5379 - assert( pPage->nOverflow==0 );
1.5380 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.5381 - if( pBt->autoVacuum ){
1.5382 - int i;
1.5383 - for(i=0; i<pPage->nCell; i++){
1.5384 - rc = ptrmapPutOvfl(pPage, i);
1.5385 - if( rc!=SQLITE_OK ){
1.5386 - goto end_shallow_balance;
1.5387 - }
1.5388 - }
1.5389 - }
1.5390 -#endif
1.5391 - releasePage(pChild);
1.5392 - }
1.5393 -end_shallow_balance:
1.5394 - sqlite3_free(apCell);
1.5395 - return rc;
1.5396 -}
1.5397 -
1.5398 -
1.5399 -/*
1.5400 -** The root page is overfull
1.5401 -**
1.5402 -** When this happens, Create a new child page and copy the
1.5403 -** contents of the root into the child. Then make the root
1.5404 -** page an empty page with rightChild pointing to the new
1.5405 -** child. Finally, call balance_internal() on the new child
1.5406 -** to cause it to split.
1.5407 -*/
1.5408 -static int balance_deeper(MemPage *pPage){
1.5409 - int rc; /* Return value from subprocedures */
1.5410 - MemPage *pChild; /* Pointer to a new child page */
1.5411 - Pgno pgnoChild; /* Page number of the new child page */
1.5412 - BtShared *pBt; /* The BTree */
1.5413 - int usableSize; /* Total usable size of a page */
1.5414 - u8 *data; /* Content of the parent page */
1.5415 - u8 *cdata; /* Content of the child page */
1.5416 - int hdr; /* Offset to page header in parent */
1.5417 - int brk; /* Offset to content of first cell in parent */
1.5418 -
1.5419 - assert( pPage->pParent==0 );
1.5420 - assert( pPage->nOverflow>0 );
1.5421 - pBt = pPage->pBt;
1.5422 - assert( sqlite3_mutex_held(pBt->mutex) );
1.5423 - rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
1.5424 - if( rc ) return rc;
1.5425 - assert( sqlite3PagerIswriteable(pChild->pDbPage) );
1.5426 - usableSize = pBt->usableSize;
1.5427 - data = pPage->aData;
1.5428 - hdr = pPage->hdrOffset;
1.5429 - brk = get2byte(&data[hdr+5]);
1.5430 - cdata = pChild->aData;
1.5431 - memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
1.5432 - memcpy(&cdata[brk], &data[brk], usableSize-brk);
1.5433 - assert( pChild->isInit==0 );
1.5434 - rc = sqlite3BtreeInitPage(pChild, pPage);
1.5435 - if( rc ) goto balancedeeper_out;
1.5436 - memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
1.5437 - pChild->nOverflow = pPage->nOverflow;
1.5438 - if( pChild->nOverflow ){
1.5439 - pChild->nFree = 0;
1.5440 - }
1.5441 - assert( pChild->nCell==pPage->nCell );
1.5442 - zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
1.5443 - put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
1.5444 - TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
1.5445 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.5446 - if( pBt->autoVacuum ){
1.5447 - int i;
1.5448 - rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
1.5449 - if( rc ) goto balancedeeper_out;
1.5450 - for(i=0; i<pChild->nCell; i++){
1.5451 - rc = ptrmapPutOvfl(pChild, i);
1.5452 - if( rc!=SQLITE_OK ){
1.5453 - return rc;
1.5454 - }
1.5455 - }
1.5456 - }
1.5457 -#endif
1.5458 - rc = balance_nonroot(pChild);
1.5459 -
1.5460 -balancedeeper_out:
1.5461 - releasePage(pChild);
1.5462 - return rc;
1.5463 -}
1.5464 -
1.5465 -/*
1.5466 -** Decide if the page pPage needs to be balanced. If balancing is
1.5467 -** required, call the appropriate balancing routine.
1.5468 -*/
1.5469 -static int balance(MemPage *pPage, int insert){
1.5470 - int rc = SQLITE_OK;
1.5471 - assert( sqlite3_mutex_held(pPage->pBt->mutex) );
1.5472 - if( pPage->pParent==0 ){
1.5473 - rc = sqlite3PagerWrite(pPage->pDbPage);
1.5474 - if( rc==SQLITE_OK && pPage->nOverflow>0 ){
1.5475 - rc = balance_deeper(pPage);
1.5476 - }
1.5477 - if( rc==SQLITE_OK && pPage->nCell==0 ){
1.5478 - rc = balance_shallower(pPage);
1.5479 - }
1.5480 - }else{
1.5481 - if( pPage->nOverflow>0 ||
1.5482 - (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
1.5483 - rc = balance_nonroot(pPage);
1.5484 - }
1.5485 - }
1.5486 - return rc;
1.5487 -}
1.5488 -
1.5489 -/*
1.5490 -** This routine checks all cursors that point to table pgnoRoot.
1.5491 -** If any of those cursors were opened with wrFlag==0 in a different
1.5492 -** database connection (a database connection that shares the pager
1.5493 -** cache with the current connection) and that other connection
1.5494 -** is not in the ReadUncommmitted state, then this routine returns
1.5495 -** SQLITE_LOCKED.
1.5496 -**
1.5497 -** In addition to checking for read-locks (where a read-lock
1.5498 -** means a cursor opened with wrFlag==0) this routine also moves
1.5499 -** all write cursors so that they are pointing to the
1.5500 -** first Cell on the root page. This is necessary because an insert
1.5501 -** or delete might change the number of cells on a page or delete
1.5502 -** a page entirely and we do not want to leave any cursors
1.5503 -** pointing to non-existant pages or cells.
1.5504 -*/
1.5505 -static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
1.5506 - BtCursor *p;
1.5507 - BtShared *pBt = pBtree->pBt;
1.5508 - sqlite3 *db = pBtree->pSqlite;
1.5509 - assert( sqlite3BtreeHoldsMutex(pBtree) );
1.5510 - for(p=pBt->pCursor; p; p=p->pNext){
1.5511 - if( p==pExclude ) continue;
1.5512 - if( p->eState!=CURSOR_VALID ) continue;
1.5513 - if( p->pgnoRoot!=pgnoRoot ) continue;
1.5514 - if( p->wrFlag==0 ){
1.5515 - sqlite3 *dbOther = p->pBtree->pSqlite;
1.5516 - if( dbOther==0 ||
1.5517 - (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
1.5518 - return SQLITE_LOCKED;
1.5519 - }
1.5520 - }else if( p->pPage->pgno!=p->pgnoRoot ){
1.5521 - moveToRoot(p);
1.5522 - }
1.5523 - }
1.5524 - return SQLITE_OK;
1.5525 -}
1.5526 -
1.5527 -/*
1.5528 -** Insert a new record into the BTree. The key is given by (pKey,nKey)
1.5529 -** and the data is given by (pData,nData). The cursor is used only to
1.5530 -** define what table the record should be inserted into. The cursor
1.5531 -** is left pointing at a random location.
1.5532 -**
1.5533 -** For an INTKEY table, only the nKey value of the key is used. pKey is
1.5534 -** ignored. For a ZERODATA table, the pData and nData are both ignored.
1.5535 -*/
1.5536 -int sqlite3BtreeInsert(
1.5537 - BtCursor *pCur, /* Insert data into the table of this cursor */
1.5538 - const void *pKey, i64 nKey, /* The key of the new record */
1.5539 - const void *pData, int nData, /* The data of the new record */
1.5540 - int nZero, /* Number of extra 0 bytes to append to data */
1.5541 - int appendBias /* True if this is likely an append */
1.5542 -){
1.5543 - int rc;
1.5544 - int loc;
1.5545 - int szNew;
1.5546 - MemPage *pPage;
1.5547 - Btree *p = pCur->pBtree;
1.5548 - BtShared *pBt = p->pBt;
1.5549 - unsigned char *oldCell;
1.5550 - unsigned char *newCell = 0;
1.5551 -
1.5552 - assert( cursorHoldsMutex(pCur) );
1.5553 - if( pBt->inTransaction!=TRANS_WRITE ){
1.5554 - /* Must start a transaction before doing an insert */
1.5555 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.5556 - return rc;
1.5557 - }
1.5558 - assert( !pBt->readOnly );
1.5559 - if( !pCur->wrFlag ){
1.5560 - return SQLITE_PERM; /* Cursor not open for writing */
1.5561 - }
1.5562 - if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
1.5563 - return SQLITE_LOCKED; /* The table pCur points to has a read lock */
1.5564 - }
1.5565 - if( pCur->eState==CURSOR_FAULT ){
1.5566 - return pCur->skip;
1.5567 - }
1.5568 -
1.5569 - /* Save the positions of any other cursors open on this table */
1.5570 - clearCursorPosition(pCur);
1.5571 - if(
1.5572 - SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
1.5573 - SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
1.5574 - ){
1.5575 - return rc;
1.5576 - }
1.5577 -
1.5578 - pPage = pCur->pPage;
1.5579 - assert( pPage->intKey || nKey>=0 );
1.5580 - assert( pPage->leaf || !pPage->leafData );
1.5581 - TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
1.5582 - pCur->pgnoRoot, nKey, nData, pPage->pgno,
1.5583 - loc==0 ? "overwrite" : "new entry"));
1.5584 - assert( pPage->isInit );
1.5585 - newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
1.5586 - if( newCell==0 ) return SQLITE_NOMEM;
1.5587 - rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
1.5588 - if( rc ) goto end_insert;
1.5589 - assert( szNew==cellSizePtr(pPage, newCell) );
1.5590 - assert( szNew<=MX_CELL_SIZE(pBt) );
1.5591 - if( loc==0 && CURSOR_VALID==pCur->eState ){
1.5592 - int szOld;
1.5593 - assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
1.5594 - rc = sqlite3PagerWrite(pPage->pDbPage);
1.5595 - if( rc ){
1.5596 - goto end_insert;
1.5597 - }
1.5598 - oldCell = findCell(pPage, pCur->idx);
1.5599 - if( !pPage->leaf ){
1.5600 - memcpy(newCell, oldCell, 4);
1.5601 - }
1.5602 - szOld = cellSizePtr(pPage, oldCell);
1.5603 - rc = clearCell(pPage, oldCell);
1.5604 - if( rc ) goto end_insert;
1.5605 - dropCell(pPage, pCur->idx, szOld);
1.5606 - }else if( loc<0 && pPage->nCell>0 ){
1.5607 - assert( pPage->leaf );
1.5608 - pCur->idx++;
1.5609 - pCur->info.nSize = 0;
1.5610 - }else{
1.5611 - assert( pPage->leaf );
1.5612 - }
1.5613 - rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
1.5614 - if( rc!=SQLITE_OK ) goto end_insert;
1.5615 - rc = balance(pPage, 1);
1.5616 - /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
1.5617 - /* fflush(stdout); */
1.5618 - if( rc==SQLITE_OK ){
1.5619 - moveToRoot(pCur);
1.5620 - }
1.5621 -end_insert:
1.5622 - sqlite3_free(newCell);
1.5623 - return rc;
1.5624 -}
1.5625 -
1.5626 -/*
1.5627 -** Delete the entry that the cursor is pointing to. The cursor
1.5628 -** is left pointing at a random location.
1.5629 -*/
1.5630 -int sqlite3BtreeDelete(BtCursor *pCur){
1.5631 - MemPage *pPage = pCur->pPage;
1.5632 - unsigned char *pCell;
1.5633 - int rc;
1.5634 - Pgno pgnoChild = 0;
1.5635 - Btree *p = pCur->pBtree;
1.5636 - BtShared *pBt = p->pBt;
1.5637 -
1.5638 - assert( cursorHoldsMutex(pCur) );
1.5639 - assert( pPage->isInit );
1.5640 - if( pBt->inTransaction!=TRANS_WRITE ){
1.5641 - /* Must start a transaction before doing a delete */
1.5642 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.5643 - return rc;
1.5644 - }
1.5645 - assert( !pBt->readOnly );
1.5646 - if( pCur->eState==CURSOR_FAULT ){
1.5647 - return pCur->skip;
1.5648 - }
1.5649 - if( pCur->idx >= pPage->nCell ){
1.5650 - return SQLITE_ERROR; /* The cursor is not pointing to anything */
1.5651 - }
1.5652 - if( !pCur->wrFlag ){
1.5653 - return SQLITE_PERM; /* Did not open this cursor for writing */
1.5654 - }
1.5655 - if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
1.5656 - return SQLITE_LOCKED; /* The table pCur points to has a read lock */
1.5657 - }
1.5658 -
1.5659 - /* Restore the current cursor position (a no-op if the cursor is not in
1.5660 - ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
1.5661 - ** open on the same table. Then call sqlite3PagerWrite() on the page
1.5662 - ** that the entry will be deleted from.
1.5663 - */
1.5664 - if(
1.5665 - (rc = restoreOrClearCursorPosition(pCur))!=0 ||
1.5666 - (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
1.5667 - (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
1.5668 - ){
1.5669 - return rc;
1.5670 - }
1.5671 -
1.5672 - /* Locate the cell within it's page and leave pCell pointing to the
1.5673 - ** data. The clearCell() call frees any overflow pages associated with the
1.5674 - ** cell. The cell itself is still intact.
1.5675 - */
1.5676 - pCell = findCell(pPage, pCur->idx);
1.5677 - if( !pPage->leaf ){
1.5678 - pgnoChild = get4byte(pCell);
1.5679 - }
1.5680 - rc = clearCell(pPage, pCell);
1.5681 - if( rc ){
1.5682 - return rc;
1.5683 - }
1.5684 -
1.5685 - if( !pPage->leaf ){
1.5686 - /*
1.5687 - ** The entry we are about to delete is not a leaf so if we do not
1.5688 - ** do something we will leave a hole on an internal page.
1.5689 - ** We have to fill the hole by moving in a cell from a leaf. The
1.5690 - ** next Cell after the one to be deleted is guaranteed to exist and
1.5691 - ** to be a leaf so we can use it.
1.5692 - */
1.5693 - BtCursor leafCur;
1.5694 - unsigned char *pNext;
1.5695 - int szNext; /* The compiler warning is wrong: szNext is always
1.5696 - ** initialized before use. Adding an extra initialization
1.5697 - ** to silence the compiler slows down the code. */
1.5698 - int notUsed;
1.5699 - unsigned char *tempCell = 0;
1.5700 - assert( !pPage->leafData );
1.5701 - sqlite3BtreeGetTempCursor(pCur, &leafCur);
1.5702 - rc = sqlite3BtreeNext(&leafCur, ¬Used);
1.5703 - if( rc==SQLITE_OK ){
1.5704 - rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
1.5705 - }
1.5706 - if( rc==SQLITE_OK ){
1.5707 - TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
1.5708 - pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
1.5709 - dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
1.5710 - pNext = findCell(leafCur.pPage, leafCur.idx);
1.5711 - szNext = cellSizePtr(leafCur.pPage, pNext);
1.5712 - assert( MX_CELL_SIZE(pBt)>=szNext+4 );
1.5713 - tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) );
1.5714 - if( tempCell==0 ){
1.5715 - rc = SQLITE_NOMEM;
1.5716 - }
1.5717 - }
1.5718 - if( rc==SQLITE_OK ){
1.5719 - rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
1.5720 - }
1.5721 - if( rc==SQLITE_OK ){
1.5722 - put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
1.5723 - rc = balance(pPage, 0);
1.5724 - }
1.5725 - if( rc==SQLITE_OK ){
1.5726 - dropCell(leafCur.pPage, leafCur.idx, szNext);
1.5727 - rc = balance(leafCur.pPage, 0);
1.5728 - }
1.5729 - sqlite3_free(tempCell);
1.5730 - sqlite3BtreeReleaseTempCursor(&leafCur);
1.5731 - }else{
1.5732 - TRACE(("DELETE: table=%d delete from leaf %d\n",
1.5733 - pCur->pgnoRoot, pPage->pgno));
1.5734 - dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
1.5735 - rc = balance(pPage, 0);
1.5736 - }
1.5737 - if( rc==SQLITE_OK ){
1.5738 - moveToRoot(pCur);
1.5739 - }
1.5740 - return rc;
1.5741 -}
1.5742 -
1.5743 -/*
1.5744 -** Create a new BTree table. Write into *piTable the page
1.5745 -** number for the root page of the new table.
1.5746 -**
1.5747 -** The type of type is determined by the flags parameter. Only the
1.5748 -** following values of flags are currently in use. Other values for
1.5749 -** flags might not work:
1.5750 -**
1.5751 -** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
1.5752 -** BTREE_ZERODATA Used for SQL indices
1.5753 -*/
1.5754 -static int btreeCreateTable(Btree *p, int *piTable, int flags){
1.5755 - BtShared *pBt = p->pBt;
1.5756 - MemPage *pRoot;
1.5757 - Pgno pgnoRoot;
1.5758 - int rc;
1.5759 -
1.5760 - assert( sqlite3BtreeHoldsMutex(p) );
1.5761 - if( pBt->inTransaction!=TRANS_WRITE ){
1.5762 - /* Must start a transaction first */
1.5763 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.5764 - return rc;
1.5765 - }
1.5766 - assert( !pBt->readOnly );
1.5767 -
1.5768 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.5769 - rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
1.5770 - if( rc ){
1.5771 - return rc;
1.5772 - }
1.5773 -#else
1.5774 - if( pBt->autoVacuum ){
1.5775 - Pgno pgnoMove; /* Move a page here to make room for the root-page */
1.5776 - MemPage *pPageMove; /* The page to move to. */
1.5777 -
1.5778 - /* Creating a new table may probably require moving an existing database
1.5779 - ** to make room for the new tables root page. In case this page turns
1.5780 - ** out to be an overflow page, delete all overflow page-map caches
1.5781 - ** held by open cursors.
1.5782 - */
1.5783 - invalidateAllOverflowCache(pBt);
1.5784 -
1.5785 - /* Read the value of meta[3] from the database to determine where the
1.5786 - ** root page of the new table should go. meta[3] is the largest root-page
1.5787 - ** created so far, so the new root-page is (meta[3]+1).
1.5788 - */
1.5789 - rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
1.5790 - if( rc!=SQLITE_OK ){
1.5791 - return rc;
1.5792 - }
1.5793 - pgnoRoot++;
1.5794 -
1.5795 - /* The new root-page may not be allocated on a pointer-map page, or the
1.5796 - ** PENDING_BYTE page.
1.5797 - */
1.5798 - if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
1.5799 - pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
1.5800 - pgnoRoot++;
1.5801 - }
1.5802 - assert( pgnoRoot>=3 );
1.5803 -
1.5804 - /* Allocate a page. The page that currently resides at pgnoRoot will
1.5805 - ** be moved to the allocated page (unless the allocated page happens
1.5806 - ** to reside at pgnoRoot).
1.5807 - */
1.5808 - rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
1.5809 - if( rc!=SQLITE_OK ){
1.5810 - return rc;
1.5811 - }
1.5812 -
1.5813 - if( pgnoMove!=pgnoRoot ){
1.5814 - /* pgnoRoot is the page that will be used for the root-page of
1.5815 - ** the new table (assuming an error did not occur). But we were
1.5816 - ** allocated pgnoMove. If required (i.e. if it was not allocated
1.5817 - ** by extending the file), the current page at position pgnoMove
1.5818 - ** is already journaled.
1.5819 - */
1.5820 - u8 eType;
1.5821 - Pgno iPtrPage;
1.5822 -
1.5823 - releasePage(pPageMove);
1.5824 -
1.5825 - /* Move the page currently at pgnoRoot to pgnoMove. */
1.5826 - rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
1.5827 - if( rc!=SQLITE_OK ){
1.5828 - return rc;
1.5829 - }
1.5830 - rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
1.5831 - if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
1.5832 - releasePage(pRoot);
1.5833 - return rc;
1.5834 - }
1.5835 - assert( eType!=PTRMAP_ROOTPAGE );
1.5836 - assert( eType!=PTRMAP_FREEPAGE );
1.5837 - rc = sqlite3PagerWrite(pRoot->pDbPage);
1.5838 - if( rc!=SQLITE_OK ){
1.5839 - releasePage(pRoot);
1.5840 - return rc;
1.5841 - }
1.5842 - rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
1.5843 - releasePage(pRoot);
1.5844 -
1.5845 - /* Obtain the page at pgnoRoot */
1.5846 - if( rc!=SQLITE_OK ){
1.5847 - return rc;
1.5848 - }
1.5849 - rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
1.5850 - if( rc!=SQLITE_OK ){
1.5851 - return rc;
1.5852 - }
1.5853 - rc = sqlite3PagerWrite(pRoot->pDbPage);
1.5854 - if( rc!=SQLITE_OK ){
1.5855 - releasePage(pRoot);
1.5856 - return rc;
1.5857 - }
1.5858 - }else{
1.5859 - pRoot = pPageMove;
1.5860 - }
1.5861 -
1.5862 - /* Update the pointer-map and meta-data with the new root-page number. */
1.5863 - rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
1.5864 - if( rc ){
1.5865 - releasePage(pRoot);
1.5866 - return rc;
1.5867 - }
1.5868 - rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
1.5869 - if( rc ){
1.5870 - releasePage(pRoot);
1.5871 - return rc;
1.5872 - }
1.5873 -
1.5874 - }else{
1.5875 - rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
1.5876 - if( rc ) return rc;
1.5877 - }
1.5878 -#endif
1.5879 - assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
1.5880 - zeroPage(pRoot, flags | PTF_LEAF);
1.5881 - sqlite3PagerUnref(pRoot->pDbPage);
1.5882 - *piTable = (int)pgnoRoot;
1.5883 - return SQLITE_OK;
1.5884 -}
1.5885 -int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
1.5886 - int rc;
1.5887 - sqlite3BtreeEnter(p);
1.5888 - rc = btreeCreateTable(p, piTable, flags);
1.5889 - sqlite3BtreeLeave(p);
1.5890 - return rc;
1.5891 -}
1.5892 -
1.5893 -/*
1.5894 -** Erase the given database page and all its children. Return
1.5895 -** the page to the freelist.
1.5896 -*/
1.5897 -static int clearDatabasePage(
1.5898 - BtShared *pBt, /* The BTree that contains the table */
1.5899 - Pgno pgno, /* Page number to clear */
1.5900 - MemPage *pParent, /* Parent page. NULL for the root */
1.5901 - int freePageFlag /* Deallocate page if true */
1.5902 -){
1.5903 - MemPage *pPage = 0;
1.5904 - int rc;
1.5905 - unsigned char *pCell;
1.5906 - int i;
1.5907 -
1.5908 - assert( sqlite3_mutex_held(pBt->mutex) );
1.5909 - if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
1.5910 - return SQLITE_CORRUPT_BKPT;
1.5911 - }
1.5912 -
1.5913 - rc = getAndInitPage(pBt, pgno, &pPage, pParent);
1.5914 - if( rc ) goto cleardatabasepage_out;
1.5915 - for(i=0; i<pPage->nCell; i++){
1.5916 - pCell = findCell(pPage, i);
1.5917 - if( !pPage->leaf ){
1.5918 - rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
1.5919 - if( rc ) goto cleardatabasepage_out;
1.5920 - }
1.5921 - rc = clearCell(pPage, pCell);
1.5922 - if( rc ) goto cleardatabasepage_out;
1.5923 - }
1.5924 - if( !pPage->leaf ){
1.5925 - rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
1.5926 - if( rc ) goto cleardatabasepage_out;
1.5927 - }
1.5928 - if( freePageFlag ){
1.5929 - rc = freePage(pPage);
1.5930 - }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
1.5931 - zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
1.5932 - }
1.5933 -
1.5934 -cleardatabasepage_out:
1.5935 - releasePage(pPage);
1.5936 - return rc;
1.5937 -}
1.5938 -
1.5939 -/*
1.5940 -** Delete all information from a single table in the database. iTable is
1.5941 -** the page number of the root of the table. After this routine returns,
1.5942 -** the root page is empty, but still exists.
1.5943 -**
1.5944 -** This routine will fail with SQLITE_LOCKED if there are any open
1.5945 -** read cursors on the table. Open write cursors are moved to the
1.5946 -** root of the table.
1.5947 -*/
1.5948 -int sqlite3BtreeClearTable(Btree *p, int iTable){
1.5949 - int rc;
1.5950 - BtShared *pBt = p->pBt;
1.5951 - sqlite3BtreeEnter(p);
1.5952 - if( p->inTrans!=TRANS_WRITE ){
1.5953 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.5954 - }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){
1.5955 - /* nothing to do */
1.5956 - }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
1.5957 - /* nothing to do */
1.5958 - }else{
1.5959 - rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
1.5960 - }
1.5961 - sqlite3BtreeLeave(p);
1.5962 - return rc;
1.5963 -}
1.5964 -
1.5965 -/*
1.5966 -** Erase all information in a table and add the root of the table to
1.5967 -** the freelist. Except, the root of the principle table (the one on
1.5968 -** page 1) is never added to the freelist.
1.5969 -**
1.5970 -** This routine will fail with SQLITE_LOCKED if there are any open
1.5971 -** cursors on the table.
1.5972 -**
1.5973 -** If AUTOVACUUM is enabled and the page at iTable is not the last
1.5974 -** root page in the database file, then the last root page
1.5975 -** in the database file is moved into the slot formerly occupied by
1.5976 -** iTable and that last slot formerly occupied by the last root page
1.5977 -** is added to the freelist instead of iTable. In this say, all
1.5978 -** root pages are kept at the beginning of the database file, which
1.5979 -** is necessary for AUTOVACUUM to work right. *piMoved is set to the
1.5980 -** page number that used to be the last root page in the file before
1.5981 -** the move. If no page gets moved, *piMoved is set to 0.
1.5982 -** The last root page is recorded in meta[3] and the value of
1.5983 -** meta[3] is updated by this procedure.
1.5984 -*/
1.5985 -static int btreeDropTable(Btree *p, int iTable, int *piMoved){
1.5986 - int rc;
1.5987 - MemPage *pPage = 0;
1.5988 - BtShared *pBt = p->pBt;
1.5989 -
1.5990 - assert( sqlite3BtreeHoldsMutex(p) );
1.5991 - if( p->inTrans!=TRANS_WRITE ){
1.5992 - return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.5993 - }
1.5994 -
1.5995 - /* It is illegal to drop a table if any cursors are open on the
1.5996 - ** database. This is because in auto-vacuum mode the backend may
1.5997 - ** need to move another root-page to fill a gap left by the deleted
1.5998 - ** root page. If an open cursor was using this page a problem would
1.5999 - ** occur.
1.6000 - */
1.6001 - if( pBt->pCursor ){
1.6002 - return SQLITE_LOCKED;
1.6003 - }
1.6004 -
1.6005 - rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
1.6006 - if( rc ) return rc;
1.6007 - rc = sqlite3BtreeClearTable(p, iTable);
1.6008 - if( rc ){
1.6009 - releasePage(pPage);
1.6010 - return rc;
1.6011 - }
1.6012 -
1.6013 - *piMoved = 0;
1.6014 -
1.6015 - if( iTable>1 ){
1.6016 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.6017 - rc = freePage(pPage);
1.6018 - releasePage(pPage);
1.6019 -#else
1.6020 - if( pBt->autoVacuum ){
1.6021 - Pgno maxRootPgno;
1.6022 - rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
1.6023 - if( rc!=SQLITE_OK ){
1.6024 - releasePage(pPage);
1.6025 - return rc;
1.6026 - }
1.6027 -
1.6028 - if( iTable==maxRootPgno ){
1.6029 - /* If the table being dropped is the table with the largest root-page
1.6030 - ** number in the database, put the root page on the free list.
1.6031 - */
1.6032 - rc = freePage(pPage);
1.6033 - releasePage(pPage);
1.6034 - if( rc!=SQLITE_OK ){
1.6035 - return rc;
1.6036 - }
1.6037 - }else{
1.6038 - /* The table being dropped does not have the largest root-page
1.6039 - ** number in the database. So move the page that does into the
1.6040 - ** gap left by the deleted root-page.
1.6041 - */
1.6042 - MemPage *pMove;
1.6043 - releasePage(pPage);
1.6044 - rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
1.6045 - if( rc!=SQLITE_OK ){
1.6046 - return rc;
1.6047 - }
1.6048 - rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
1.6049 - releasePage(pMove);
1.6050 - if( rc!=SQLITE_OK ){
1.6051 - return rc;
1.6052 - }
1.6053 - rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
1.6054 - if( rc!=SQLITE_OK ){
1.6055 - return rc;
1.6056 - }
1.6057 - rc = freePage(pMove);
1.6058 - releasePage(pMove);
1.6059 - if( rc!=SQLITE_OK ){
1.6060 - return rc;
1.6061 - }
1.6062 - *piMoved = maxRootPgno;
1.6063 - }
1.6064 -
1.6065 - /* Set the new 'max-root-page' value in the database header. This
1.6066 - ** is the old value less one, less one more if that happens to
1.6067 - ** be a root-page number, less one again if that is the
1.6068 - ** PENDING_BYTE_PAGE.
1.6069 - */
1.6070 - maxRootPgno--;
1.6071 - if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
1.6072 - maxRootPgno--;
1.6073 - }
1.6074 - if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
1.6075 - maxRootPgno--;
1.6076 - }
1.6077 - assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
1.6078 -
1.6079 - rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
1.6080 - }else{
1.6081 - rc = freePage(pPage);
1.6082 - releasePage(pPage);
1.6083 - }
1.6084 -#endif
1.6085 - }else{
1.6086 - /* If sqlite3BtreeDropTable was called on page 1. */
1.6087 - zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
1.6088 - releasePage(pPage);
1.6089 - }
1.6090 - return rc;
1.6091 -}
1.6092 -int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
1.6093 - int rc;
1.6094 - sqlite3BtreeEnter(p);
1.6095 - rc = btreeDropTable(p, iTable, piMoved);
1.6096 - sqlite3BtreeLeave(p);
1.6097 - return rc;
1.6098 -}
1.6099 -
1.6100 -
1.6101 -/*
1.6102 -** Read the meta-information out of a database file. Meta[0]
1.6103 -** is the number of free pages currently in the database. Meta[1]
1.6104 -** through meta[15] are available for use by higher layers. Meta[0]
1.6105 -** is read-only, the others are read/write.
1.6106 -**
1.6107 -** The schema layer numbers meta values differently. At the schema
1.6108 -** layer (and the SetCookie and ReadCookie opcodes) the number of
1.6109 -** free pages is not visible. So Cookie[0] is the same as Meta[1].
1.6110 -*/
1.6111 -int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
1.6112 - DbPage *pDbPage;
1.6113 - int rc;
1.6114 - unsigned char *pP1;
1.6115 - BtShared *pBt = p->pBt;
1.6116 -
1.6117 - sqlite3BtreeEnter(p);
1.6118 -
1.6119 - /* Reading a meta-data value requires a read-lock on page 1 (and hence
1.6120 - ** the sqlite_master table. We grab this lock regardless of whether or
1.6121 - ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
1.6122 - ** 1 is treated as a special case by queryTableLock() and lockTable()).
1.6123 - */
1.6124 - rc = queryTableLock(p, 1, READ_LOCK);
1.6125 - if( rc!=SQLITE_OK ){
1.6126 - sqlite3BtreeLeave(p);
1.6127 - return rc;
1.6128 - }
1.6129 -
1.6130 - assert( idx>=0 && idx<=15 );
1.6131 - rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
1.6132 - if( rc ){
1.6133 - sqlite3BtreeLeave(p);
1.6134 - return rc;
1.6135 - }
1.6136 - pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
1.6137 - *pMeta = get4byte(&pP1[36 + idx*4]);
1.6138 - sqlite3PagerUnref(pDbPage);
1.6139 -
1.6140 - /* If autovacuumed is disabled in this build but we are trying to
1.6141 - ** access an autovacuumed database, then make the database readonly.
1.6142 - */
1.6143 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.6144 - if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
1.6145 -#endif
1.6146 -
1.6147 - /* Grab the read-lock on page 1. */
1.6148 - rc = lockTable(p, 1, READ_LOCK);
1.6149 - sqlite3BtreeLeave(p);
1.6150 - return rc;
1.6151 -}
1.6152 -
1.6153 -/*
1.6154 -** Write meta-information back into the database. Meta[0] is
1.6155 -** read-only and may not be written.
1.6156 -*/
1.6157 -int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
1.6158 - BtShared *pBt = p->pBt;
1.6159 - unsigned char *pP1;
1.6160 - int rc;
1.6161 - assert( idx>=1 && idx<=15 );
1.6162 - sqlite3BtreeEnter(p);
1.6163 - if( p->inTrans!=TRANS_WRITE ){
1.6164 - rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
1.6165 - }else{
1.6166 - assert( pBt->pPage1!=0 );
1.6167 - pP1 = pBt->pPage1->aData;
1.6168 - rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
1.6169 - if( rc==SQLITE_OK ){
1.6170 - put4byte(&pP1[36 + idx*4], iMeta);
1.6171 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6172 - if( idx==7 ){
1.6173 - assert( pBt->autoVacuum || iMeta==0 );
1.6174 - assert( iMeta==0 || iMeta==1 );
1.6175 - pBt->incrVacuum = iMeta;
1.6176 - }
1.6177 -#endif
1.6178 - }
1.6179 - }
1.6180 - sqlite3BtreeLeave(p);
1.6181 - return rc;
1.6182 -}
1.6183 -
1.6184 -/*
1.6185 -** Return the flag byte at the beginning of the page that the cursor
1.6186 -** is currently pointing to.
1.6187 -*/
1.6188 -int sqlite3BtreeFlags(BtCursor *pCur){
1.6189 - /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
1.6190 - ** restoreOrClearCursorPosition() here.
1.6191 - */
1.6192 - MemPage *pPage = pCur->pPage;
1.6193 - assert( cursorHoldsMutex(pCur) );
1.6194 - assert( pPage->pBt==pCur->pBt );
1.6195 - return pPage ? pPage->aData[pPage->hdrOffset] : 0;
1.6196 -}
1.6197 -
1.6198 -
1.6199 -/*
1.6200 -** Return the pager associated with a BTree. This routine is used for
1.6201 -** testing and debugging only.
1.6202 -*/
1.6203 -Pager *sqlite3BtreePager(Btree *p){
1.6204 - return p->pBt->pPager;
1.6205 -}
1.6206 -
1.6207 -#ifndef SQLITE_OMIT_INTEGRITY_CHECK
1.6208 -/*
1.6209 -** Append a message to the error message string.
1.6210 -*/
1.6211 -static void checkAppendMsg(
1.6212 - IntegrityCk *pCheck,
1.6213 - char *zMsg1,
1.6214 - const char *zFormat,
1.6215 - ...
1.6216 -){
1.6217 - va_list ap;
1.6218 - char *zMsg2;
1.6219 - if( !pCheck->mxErr ) return;
1.6220 - pCheck->mxErr--;
1.6221 - pCheck->nErr++;
1.6222 - va_start(ap, zFormat);
1.6223 - zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
1.6224 - va_end(ap);
1.6225 - if( zMsg1==0 ) zMsg1 = "";
1.6226 - if( pCheck->zErrMsg ){
1.6227 - char *zOld = pCheck->zErrMsg;
1.6228 - pCheck->zErrMsg = 0;
1.6229 - sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
1.6230 - sqlite3_free(zOld);
1.6231 - }else{
1.6232 - sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
1.6233 - }
1.6234 - sqlite3_free(zMsg2);
1.6235 -}
1.6236 -#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.6237 -
1.6238 -#ifndef SQLITE_OMIT_INTEGRITY_CHECK
1.6239 -/*
1.6240 -** Add 1 to the reference count for page iPage. If this is the second
1.6241 -** reference to the page, add an error message to pCheck->zErrMsg.
1.6242 -** Return 1 if there are 2 ore more references to the page and 0 if
1.6243 -** if this is the first reference to the page.
1.6244 -**
1.6245 -** Also check that the page number is in bounds.
1.6246 -*/
1.6247 -static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
1.6248 - if( iPage==0 ) return 1;
1.6249 - if( iPage>pCheck->nPage || iPage<0 ){
1.6250 - checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
1.6251 - return 1;
1.6252 - }
1.6253 - if( pCheck->anRef[iPage]==1 ){
1.6254 - checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
1.6255 - return 1;
1.6256 - }
1.6257 - return (pCheck->anRef[iPage]++)>1;
1.6258 -}
1.6259 -
1.6260 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6261 -/*
1.6262 -** Check that the entry in the pointer-map for page iChild maps to
1.6263 -** page iParent, pointer type ptrType. If not, append an error message
1.6264 -** to pCheck.
1.6265 -*/
1.6266 -static void checkPtrmap(
1.6267 - IntegrityCk *pCheck, /* Integrity check context */
1.6268 - Pgno iChild, /* Child page number */
1.6269 - u8 eType, /* Expected pointer map type */
1.6270 - Pgno iParent, /* Expected pointer map parent page number */
1.6271 - char *zContext /* Context description (used for error msg) */
1.6272 -){
1.6273 - int rc;
1.6274 - u8 ePtrmapType;
1.6275 - Pgno iPtrmapParent;
1.6276 -
1.6277 - rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
1.6278 - if( rc!=SQLITE_OK ){
1.6279 - checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
1.6280 - return;
1.6281 - }
1.6282 -
1.6283 - if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
1.6284 - checkAppendMsg(pCheck, zContext,
1.6285 - "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
1.6286 - iChild, eType, iParent, ePtrmapType, iPtrmapParent);
1.6287 - }
1.6288 -}
1.6289 -#endif
1.6290 -
1.6291 -/*
1.6292 -** Check the integrity of the freelist or of an overflow page list.
1.6293 -** Verify that the number of pages on the list is N.
1.6294 -*/
1.6295 -static void checkList(
1.6296 - IntegrityCk *pCheck, /* Integrity checking context */
1.6297 - int isFreeList, /* True for a freelist. False for overflow page list */
1.6298 - int iPage, /* Page number for first page in the list */
1.6299 - int N, /* Expected number of pages in the list */
1.6300 - char *zContext /* Context for error messages */
1.6301 -){
1.6302 - int i;
1.6303 - int expected = N;
1.6304 - int iFirst = iPage;
1.6305 - while( N-- > 0 && pCheck->mxErr ){
1.6306 - DbPage *pOvflPage;
1.6307 - unsigned char *pOvflData;
1.6308 - if( iPage<1 ){
1.6309 - checkAppendMsg(pCheck, zContext,
1.6310 - "%d of %d pages missing from overflow list starting at %d",
1.6311 - N+1, expected, iFirst);
1.6312 - break;
1.6313 - }
1.6314 - if( checkRef(pCheck, iPage, zContext) ) break;
1.6315 - if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
1.6316 - checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
1.6317 - break;
1.6318 - }
1.6319 - pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
1.6320 - if( isFreeList ){
1.6321 - int n = get4byte(&pOvflData[4]);
1.6322 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6323 - if( pCheck->pBt->autoVacuum ){
1.6324 - checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
1.6325 - }
1.6326 -#endif
1.6327 - if( n>pCheck->pBt->usableSize/4-8 ){
1.6328 - checkAppendMsg(pCheck, zContext,
1.6329 - "freelist leaf count too big on page %d", iPage);
1.6330 - N--;
1.6331 - }else{
1.6332 - for(i=0; i<n; i++){
1.6333 - Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
1.6334 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6335 - if( pCheck->pBt->autoVacuum ){
1.6336 - checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
1.6337 - }
1.6338 -#endif
1.6339 - checkRef(pCheck, iFreePage, zContext);
1.6340 - }
1.6341 - N -= n;
1.6342 - }
1.6343 - }
1.6344 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6345 - else{
1.6346 - /* If this database supports auto-vacuum and iPage is not the last
1.6347 - ** page in this overflow list, check that the pointer-map entry for
1.6348 - ** the following page matches iPage.
1.6349 - */
1.6350 - if( pCheck->pBt->autoVacuum && N>0 ){
1.6351 - i = get4byte(pOvflData);
1.6352 - checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
1.6353 - }
1.6354 - }
1.6355 -#endif
1.6356 - iPage = get4byte(pOvflData);
1.6357 - sqlite3PagerUnref(pOvflPage);
1.6358 - }
1.6359 -}
1.6360 -#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.6361 -
1.6362 -#ifndef SQLITE_OMIT_INTEGRITY_CHECK
1.6363 -/*
1.6364 -** Do various sanity checks on a single page of a tree. Return
1.6365 -** the tree depth. Root pages return 0. Parents of root pages
1.6366 -** return 1, and so forth.
1.6367 -**
1.6368 -** These checks are done:
1.6369 -**
1.6370 -** 1. Make sure that cells and freeblocks do not overlap
1.6371 -** but combine to completely cover the page.
1.6372 -** NO 2. Make sure cell keys are in order.
1.6373 -** NO 3. Make sure no key is less than or equal to zLowerBound.
1.6374 -** NO 4. Make sure no key is greater than or equal to zUpperBound.
1.6375 -** 5. Check the integrity of overflow pages.
1.6376 -** 6. Recursively call checkTreePage on all children.
1.6377 -** 7. Verify that the depth of all children is the same.
1.6378 -** 8. Make sure this page is at least 33% full or else it is
1.6379 -** the root of the tree.
1.6380 -*/
1.6381 -static int checkTreePage(
1.6382 - IntegrityCk *pCheck, /* Context for the sanity check */
1.6383 - int iPage, /* Page number of the page to check */
1.6384 - MemPage *pParent, /* Parent page */
1.6385 - char *zParentContext /* Parent context */
1.6386 -){
1.6387 - MemPage *pPage;
1.6388 - int i, rc, depth, d2, pgno, cnt;
1.6389 - int hdr, cellStart;
1.6390 - int nCell;
1.6391 - u8 *data;
1.6392 - BtShared *pBt;
1.6393 - int usableSize;
1.6394 - char zContext[100];
1.6395 - char *hit;
1.6396 -
1.6397 - sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
1.6398 -
1.6399 - /* Check that the page exists
1.6400 - */
1.6401 - pBt = pCheck->pBt;
1.6402 - usableSize = pBt->usableSize;
1.6403 - if( iPage==0 ) return 0;
1.6404 - if( checkRef(pCheck, iPage, zParentContext) ) return 0;
1.6405 - if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
1.6406 - checkAppendMsg(pCheck, zContext,
1.6407 - "unable to get the page. error code=%d", rc);
1.6408 - return 0;
1.6409 - }
1.6410 - if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){
1.6411 - checkAppendMsg(pCheck, zContext,
1.6412 - "sqlite3BtreeInitPage() returns error code %d", rc);
1.6413 - releasePage(pPage);
1.6414 - return 0;
1.6415 - }
1.6416 -
1.6417 - /* Check out all the cells.
1.6418 - */
1.6419 - depth = 0;
1.6420 - for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
1.6421 - u8 *pCell;
1.6422 - int sz;
1.6423 - CellInfo info;
1.6424 -
1.6425 - /* Check payload overflow pages
1.6426 - */
1.6427 - sqlite3_snprintf(sizeof(zContext), zContext,
1.6428 - "On tree page %d cell %d: ", iPage, i);
1.6429 - pCell = findCell(pPage,i);
1.6430 - sqlite3BtreeParseCellPtr(pPage, pCell, &info);
1.6431 - sz = info.nData;
1.6432 - if( !pPage->intKey ) sz += info.nKey;
1.6433 - assert( sz==info.nPayload );
1.6434 - if( sz>info.nLocal ){
1.6435 - int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
1.6436 - Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
1.6437 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6438 - if( pBt->autoVacuum ){
1.6439 - checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
1.6440 - }
1.6441 -#endif
1.6442 - checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
1.6443 - }
1.6444 -
1.6445 - /* Check sanity of left child page.
1.6446 - */
1.6447 - if( !pPage->leaf ){
1.6448 - pgno = get4byte(pCell);
1.6449 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6450 - if( pBt->autoVacuum ){
1.6451 - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
1.6452 - }
1.6453 -#endif
1.6454 - d2 = checkTreePage(pCheck,pgno,pPage,zContext);
1.6455 - if( i>0 && d2!=depth ){
1.6456 - checkAppendMsg(pCheck, zContext, "Child page depth differs");
1.6457 - }
1.6458 - depth = d2;
1.6459 - }
1.6460 - }
1.6461 - if( !pPage->leaf ){
1.6462 - pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1.6463 - sqlite3_snprintf(sizeof(zContext), zContext,
1.6464 - "On page %d at right child: ", iPage);
1.6465 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6466 - if( pBt->autoVacuum ){
1.6467 - checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
1.6468 - }
1.6469 -#endif
1.6470 - checkTreePage(pCheck, pgno, pPage, zContext);
1.6471 - }
1.6472 -
1.6473 - /* Check for complete coverage of the page
1.6474 - */
1.6475 - data = pPage->aData;
1.6476 - hdr = pPage->hdrOffset;
1.6477 - hit = sqlite3MallocZero( usableSize );
1.6478 - if( hit ){
1.6479 - memset(hit, 1, get2byte(&data[hdr+5]));
1.6480 - nCell = get2byte(&data[hdr+3]);
1.6481 - cellStart = hdr + 12 - 4*pPage->leaf;
1.6482 - for(i=0; i<nCell; i++){
1.6483 - int pc = get2byte(&data[cellStart+i*2]);
1.6484 - int size = cellSizePtr(pPage, &data[pc]);
1.6485 - int j;
1.6486 - if( (pc+size-1)>=usableSize || pc<0 ){
1.6487 - checkAppendMsg(pCheck, 0,
1.6488 - "Corruption detected in cell %d on page %d",i,iPage,0);
1.6489 - }else{
1.6490 - for(j=pc+size-1; j>=pc; j--) hit[j]++;
1.6491 - }
1.6492 - }
1.6493 - for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
1.6494 - cnt++){
1.6495 - int size = get2byte(&data[i+2]);
1.6496 - int j;
1.6497 - if( (i+size-1)>=usableSize || i<0 ){
1.6498 - checkAppendMsg(pCheck, 0,
1.6499 - "Corruption detected in cell %d on page %d",i,iPage,0);
1.6500 - }else{
1.6501 - for(j=i+size-1; j>=i; j--) hit[j]++;
1.6502 - }
1.6503 - i = get2byte(&data[i]);
1.6504 - }
1.6505 - for(i=cnt=0; i<usableSize; i++){
1.6506 - if( hit[i]==0 ){
1.6507 - cnt++;
1.6508 - }else if( hit[i]>1 ){
1.6509 - checkAppendMsg(pCheck, 0,
1.6510 - "Multiple uses for byte %d of page %d", i, iPage);
1.6511 - break;
1.6512 - }
1.6513 - }
1.6514 - if( cnt!=data[hdr+7] ){
1.6515 - checkAppendMsg(pCheck, 0,
1.6516 - "Fragmented space is %d byte reported as %d on page %d",
1.6517 - cnt, data[hdr+7], iPage);
1.6518 - }
1.6519 - }
1.6520 - sqlite3_free(hit);
1.6521 -
1.6522 - releasePage(pPage);
1.6523 - return depth+1;
1.6524 -}
1.6525 -#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.6526 -
1.6527 -#ifndef SQLITE_OMIT_INTEGRITY_CHECK
1.6528 -/*
1.6529 -** This routine does a complete check of the given BTree file. aRoot[] is
1.6530 -** an array of pages numbers were each page number is the root page of
1.6531 -** a table. nRoot is the number of entries in aRoot.
1.6532 -**
1.6533 -** If everything checks out, this routine returns NULL. If something is
1.6534 -** amiss, an error message is written into memory obtained from malloc()
1.6535 -** and a pointer to that error message is returned. The calling function
1.6536 -** is responsible for freeing the error message when it is done.
1.6537 -*/
1.6538 -char *sqlite3BtreeIntegrityCheck(
1.6539 - Btree *p, /* The btree to be checked */
1.6540 - int *aRoot, /* An array of root pages numbers for individual trees */
1.6541 - int nRoot, /* Number of entries in aRoot[] */
1.6542 - int mxErr, /* Stop reporting errors after this many */
1.6543 - int *pnErr /* Write number of errors seen to this variable */
1.6544 -){
1.6545 - int i;
1.6546 - int nRef;
1.6547 - IntegrityCk sCheck;
1.6548 - BtShared *pBt = p->pBt;
1.6549 -
1.6550 - sqlite3BtreeEnter(p);
1.6551 - nRef = sqlite3PagerRefcount(pBt->pPager);
1.6552 - if( lockBtreeWithRetry(p)!=SQLITE_OK ){
1.6553 - sqlite3BtreeLeave(p);
1.6554 - return sqlite3StrDup("Unable to acquire a read lock on the database");
1.6555 - }
1.6556 - sCheck.pBt = pBt;
1.6557 - sCheck.pPager = pBt->pPager;
1.6558 - sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
1.6559 - sCheck.mxErr = mxErr;
1.6560 - sCheck.nErr = 0;
1.6561 - *pnErr = 0;
1.6562 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6563 - if( pBt->nTrunc!=0 ){
1.6564 - sCheck.nPage = pBt->nTrunc;
1.6565 - }
1.6566 -#endif
1.6567 - if( sCheck.nPage==0 ){
1.6568 - unlockBtreeIfUnused(pBt);
1.6569 - sqlite3BtreeLeave(p);
1.6570 - return 0;
1.6571 - }
1.6572 - sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
1.6573 - if( !sCheck.anRef ){
1.6574 - unlockBtreeIfUnused(pBt);
1.6575 - *pnErr = 1;
1.6576 - sqlite3BtreeLeave(p);
1.6577 - return sqlite3MPrintf(p->pSqlite, "Unable to malloc %d bytes",
1.6578 - (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
1.6579 - }
1.6580 - for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
1.6581 - i = PENDING_BYTE_PAGE(pBt);
1.6582 - if( i<=sCheck.nPage ){
1.6583 - sCheck.anRef[i] = 1;
1.6584 - }
1.6585 - sCheck.zErrMsg = 0;
1.6586 -
1.6587 - /* Check the integrity of the freelist
1.6588 - */
1.6589 - checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
1.6590 - get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
1.6591 -
1.6592 - /* Check all the tables.
1.6593 - */
1.6594 - for(i=0; i<nRoot && sCheck.mxErr; i++){
1.6595 - if( aRoot[i]==0 ) continue;
1.6596 -#ifndef SQLITE_OMIT_AUTOVACUUM
1.6597 - if( pBt->autoVacuum && aRoot[i]>1 ){
1.6598 - checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
1.6599 - }
1.6600 -#endif
1.6601 - checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
1.6602 - }
1.6603 -
1.6604 - /* Make sure every page in the file is referenced
1.6605 - */
1.6606 - for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
1.6607 -#ifdef SQLITE_OMIT_AUTOVACUUM
1.6608 - if( sCheck.anRef[i]==0 ){
1.6609 - checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
1.6610 - }
1.6611 -#else
1.6612 - /* If the database supports auto-vacuum, make sure no tables contain
1.6613 - ** references to pointer-map pages.
1.6614 - */
1.6615 - if( sCheck.anRef[i]==0 &&
1.6616 - (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
1.6617 - checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
1.6618 - }
1.6619 - if( sCheck.anRef[i]!=0 &&
1.6620 - (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
1.6621 - checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
1.6622 - }
1.6623 -#endif
1.6624 - }
1.6625 -
1.6626 - /* Make sure this analysis did not leave any unref() pages
1.6627 - */
1.6628 - unlockBtreeIfUnused(pBt);
1.6629 - if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
1.6630 - checkAppendMsg(&sCheck, 0,
1.6631 - "Outstanding page count goes from %d to %d during this analysis",
1.6632 - nRef, sqlite3PagerRefcount(pBt->pPager)
1.6633 - );
1.6634 - }
1.6635 -
1.6636 - /* Clean up and report errors.
1.6637 - */
1.6638 - sqlite3BtreeLeave(p);
1.6639 - sqlite3_free(sCheck.anRef);
1.6640 - *pnErr = sCheck.nErr;
1.6641 - return sCheck.zErrMsg;
1.6642 -}
1.6643 -#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
1.6644 -
1.6645 -/*
1.6646 -** Return the full pathname of the underlying database file.
1.6647 -**
1.6648 -** The pager filename is invariant as long as the pager is
1.6649 -** open so it is safe to access without the BtShared mutex.
1.6650 -*/
1.6651 -const char *sqlite3BtreeGetFilename(Btree *p){
1.6652 - assert( p->pBt->pPager!=0 );
1.6653 - return sqlite3PagerFilename(p->pBt->pPager);
1.6654 -}
1.6655 -
1.6656 -/*
1.6657 -** Return the pathname of the directory that contains the database file.
1.6658 -**
1.6659 -** The pager directory name is invariant as long as the pager is
1.6660 -** open so it is safe to access without the BtShared mutex.
1.6661 -*/
1.6662 -const char *sqlite3BtreeGetDirname(Btree *p){
1.6663 - assert( p->pBt->pPager!=0 );
1.6664 - return sqlite3PagerDirname(p->pBt->pPager);
1.6665 -}
1.6666 -
1.6667 -/*
1.6668 -** Return the pathname of the journal file for this database. The return
1.6669 -** value of this routine is the same regardless of whether the journal file
1.6670 -** has been created or not.
1.6671 -**
1.6672 -** The pager journal filename is invariant as long as the pager is
1.6673 -** open so it is safe to access without the BtShared mutex.
1.6674 -*/
1.6675 -const char *sqlite3BtreeGetJournalname(Btree *p){
1.6676 - assert( p->pBt->pPager!=0 );
1.6677 - return sqlite3PagerJournalname(p->pBt->pPager);
1.6678 -}
1.6679 -
1.6680 -#ifndef SQLITE_OMIT_VACUUM
1.6681 -/*
1.6682 -** Copy the complete content of pBtFrom into pBtTo. A transaction
1.6683 -** must be active for both files.
1.6684 -**
1.6685 -** The size of file pBtFrom may be reduced by this operation.
1.6686 -** If anything goes wrong, the transaction on pBtFrom is rolled back.
1.6687 -*/
1.6688 -static int btreeCopyFile(Btree *pTo, Btree *pFrom){
1.6689 - int rc = SQLITE_OK;
1.6690 - Pgno i, nPage, nToPage, iSkip;
1.6691 -
1.6692 - BtShared *pBtTo = pTo->pBt;
1.6693 - BtShared *pBtFrom = pFrom->pBt;
1.6694 -
1.6695 - if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
1.6696 - return SQLITE_ERROR;
1.6697 - }
1.6698 - if( pBtTo->pCursor ) return SQLITE_BUSY;
1.6699 - nToPage = sqlite3PagerPagecount(pBtTo->pPager);
1.6700 - nPage = sqlite3PagerPagecount(pBtFrom->pPager);
1.6701 - iSkip = PENDING_BYTE_PAGE(pBtTo);
1.6702 - for(i=1; rc==SQLITE_OK && i<=nPage; i++){
1.6703 - DbPage *pDbPage;
1.6704 - if( i==iSkip ) continue;
1.6705 - rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage);
1.6706 - if( rc ) break;
1.6707 - rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage));
1.6708 - sqlite3PagerUnref(pDbPage);
1.6709 - }
1.6710 -
1.6711 - /* If the file is shrinking, journal the pages that are being truncated
1.6712 - ** so that they can be rolled back if the commit fails.
1.6713 - */
1.6714 - for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
1.6715 - DbPage *pDbPage;
1.6716 - if( i==iSkip ) continue;
1.6717 - rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
1.6718 - if( rc ) break;
1.6719 - rc = sqlite3PagerWrite(pDbPage);
1.6720 - sqlite3PagerDontWrite(pDbPage);
1.6721 - /* Yeah. It seems wierd to call DontWrite() right after Write(). But
1.6722 - ** that is because the names of those procedures do not exactly
1.6723 - ** represent what they do. Write() really means "put this page in the
1.6724 - ** rollback journal and mark it as dirty so that it will be written
1.6725 - ** to the database file later." DontWrite() undoes the second part of
1.6726 - ** that and prevents the page from being written to the database. The
1.6727 - ** page is still on the rollback journal, though. And that is the whole
1.6728 - ** point of this loop: to put pages on the rollback journal. */
1.6729 - sqlite3PagerUnref(pDbPage);
1.6730 - }
1.6731 - if( !rc && nPage<nToPage ){
1.6732 - rc = sqlite3PagerTruncate(pBtTo->pPager, nPage);
1.6733 - }
1.6734 -
1.6735 - if( rc ){
1.6736 - sqlite3BtreeRollback(pTo);
1.6737 - }
1.6738 - return rc;
1.6739 -}
1.6740 -int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
1.6741 - int rc;
1.6742 - sqlite3BtreeEnter(pTo);
1.6743 - sqlite3BtreeEnter(pFrom);
1.6744 - rc = btreeCopyFile(pTo, pFrom);
1.6745 - sqlite3BtreeLeave(pFrom);
1.6746 - sqlite3BtreeLeave(pTo);
1.6747 - return rc;
1.6748 -}
1.6749 -
1.6750 -#endif /* SQLITE_OMIT_VACUUM */
1.6751 -
1.6752 -/*
1.6753 -** Return non-zero if a transaction is active.
1.6754 -*/
1.6755 -int sqlite3BtreeIsInTrans(Btree *p){
1.6756 - assert( p==0 || sqlite3_mutex_held(p->pSqlite->mutex) );
1.6757 - return (p && (p->inTrans==TRANS_WRITE));
1.6758 -}
1.6759 -
1.6760 -/*
1.6761 -** Return non-zero if a statement transaction is active.
1.6762 -*/
1.6763 -int sqlite3BtreeIsInStmt(Btree *p){
1.6764 - assert( sqlite3BtreeHoldsMutex(p) );
1.6765 - return (p->pBt && p->pBt->inStmt);
1.6766 -}
1.6767 -
1.6768 -/*
1.6769 -** Return non-zero if a read (or write) transaction is active.
1.6770 -*/
1.6771 -int sqlite3BtreeIsInReadTrans(Btree *p){
1.6772 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.6773 - return (p && (p->inTrans!=TRANS_NONE));
1.6774 -}
1.6775 -
1.6776 -/*
1.6777 -** This function returns a pointer to a blob of memory associated with
1.6778 -** a single shared-btree. The memory is used by client code for it's own
1.6779 -** purposes (for example, to store a high-level schema associated with
1.6780 -** the shared-btree). The btree layer manages reference counting issues.
1.6781 -**
1.6782 -** The first time this is called on a shared-btree, nBytes bytes of memory
1.6783 -** are allocated, zeroed, and returned to the caller. For each subsequent
1.6784 -** call the nBytes parameter is ignored and a pointer to the same blob
1.6785 -** of memory returned.
1.6786 -**
1.6787 -** Just before the shared-btree is closed, the function passed as the
1.6788 -** xFree argument when the memory allocation was made is invoked on the
1.6789 -** blob of allocated memory. This function should not call sqlite3_free()
1.6790 -** on the memory, the btree layer does that.
1.6791 -*/
1.6792 -void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
1.6793 - BtShared *pBt = p->pBt;
1.6794 - sqlite3BtreeEnter(p);
1.6795 - if( !pBt->pSchema ){
1.6796 - pBt->pSchema = sqlite3MallocZero(nBytes);
1.6797 - pBt->xFreeSchema = xFree;
1.6798 - }
1.6799 - sqlite3BtreeLeave(p);
1.6800 - return pBt->pSchema;
1.6801 -}
1.6802 -
1.6803 -/*
1.6804 -** Return true if another user of the same shared btree as the argument
1.6805 -** handle holds an exclusive lock on the sqlite_master table.
1.6806 -*/
1.6807 -int sqlite3BtreeSchemaLocked(Btree *p){
1.6808 - int rc;
1.6809 - assert( sqlite3_mutex_held(p->pSqlite->mutex) );
1.6810 - sqlite3BtreeEnter(p);
1.6811 - rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
1.6812 - sqlite3BtreeLeave(p);
1.6813 - return rc;
1.6814 -}
1.6815 -
1.6816 -
1.6817 -#ifndef SQLITE_OMIT_SHARED_CACHE
1.6818 -/*
1.6819 -** Obtain a lock on the table whose root page is iTab. The
1.6820 -** lock is a write lock if isWritelock is true or a read lock
1.6821 -** if it is false.
1.6822 -*/
1.6823 -int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
1.6824 - int rc = SQLITE_OK;
1.6825 - u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
1.6826 - sqlite3BtreeEnter(p);
1.6827 - rc = queryTableLock(p, iTab, lockType);
1.6828 - if( rc==SQLITE_OK ){
1.6829 - rc = lockTable(p, iTab, lockType);
1.6830 - }
1.6831 - sqlite3BtreeLeave(p);
1.6832 - return rc;
1.6833 -}
1.6834 -#endif
1.6835 -
1.6836 -#ifndef SQLITE_OMIT_INCRBLOB
1.6837 -/*
1.6838 -** Argument pCsr must be a cursor opened for writing on an
1.6839 -** INTKEY table currently pointing at a valid table entry.
1.6840 -** This function modifies the data stored as part of that entry.
1.6841 -** Only the data content may only be modified, it is not possible
1.6842 -** to change the length of the data stored.
1.6843 -*/
1.6844 -int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
1.6845 - assert( cursorHoldsMutex(pCsr) );
1.6846 - assert( sqlite3_mutex_held(pCsr->pBtree->pSqlite->mutex) );
1.6847 - assert(pCsr->isIncrblobHandle);
1.6848 - if( pCsr->eState>=CURSOR_REQUIRESEEK ){
1.6849 - if( pCsr->eState==CURSOR_FAULT ){
1.6850 - return pCsr->skip;
1.6851 - }else{
1.6852 - return SQLITE_ABORT;
1.6853 - }
1.6854 - }
1.6855 -
1.6856 - /* Check some preconditions:
1.6857 - ** (a) the cursor is open for writing,
1.6858 - ** (b) there is no read-lock on the table being modified and
1.6859 - ** (c) the cursor points at a valid row of an intKey table.
1.6860 - */
1.6861 - if( !pCsr->wrFlag ){
1.6862 - return SQLITE_READONLY;
1.6863 - }
1.6864 - assert( !pCsr->pBt->readOnly
1.6865 - && pCsr->pBt->inTransaction==TRANS_WRITE );
1.6866 - if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){
1.6867 - return SQLITE_LOCKED; /* The table pCur points to has a read lock */
1.6868 - }
1.6869 - if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
1.6870 - return SQLITE_ERROR;
1.6871 - }
1.6872 -
1.6873 - return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
1.6874 -}
1.6875 -
1.6876 -/*
1.6877 -** Set a flag on this cursor to cache the locations of pages from the
1.6878 -** overflow list for the current row. This is used by cursors opened
1.6879 -** for incremental blob IO only.
1.6880 -**
1.6881 -** This function sets a flag only. The actual page location cache
1.6882 -** (stored in BtCursor.aOverflow[]) is allocated and used by function
1.6883 -** accessPayload() (the worker function for sqlite3BtreeData() and
1.6884 -** sqlite3BtreePutData()).
1.6885 -*/
1.6886 -void sqlite3BtreeCacheOverflow(BtCursor *pCur){
1.6887 - assert( cursorHoldsMutex(pCur) );
1.6888 - assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) );
1.6889 - assert(!pCur->isIncrblobHandle);
1.6890 - assert(!pCur->aOverflow);
1.6891 - pCur->isIncrblobHandle = 1;
1.6892 -}
1.6893 -#endif