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