System.Data.SQLite
Check-in [add0673a62]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:3.6.1 debug sources
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sourceforge
Files: files | file ages | folders
SHA1:add0673a626214768ca8bb15c4c7d723a410d282
User & Date: rmsimpson 2008-08-06 21:48:06
Context
2008-08-06
21:49
1.0.55.0 check-in: 8848fb5ed8 user: rmsimpson tags: sourceforge
21:48
3.6.1 debug sources check-in: add0673a62 user: rmsimpson tags: sourceforge
21:40
1.0.55.0 check-in: 24425920c8 user: rmsimpson tags: sourceforge
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added SQLite.Interop/splitsource/alter.c.

            1  +/*
            2  +** 2005 February 15
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This file contains C code routines that used to generate VDBE code
           13  +** that implements the ALTER TABLE command.
           14  +**
           15  +** $Id: alter.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           16  +*/
           17  +#include "sqliteInt.h"
           18  +#include <ctype.h>
           19  +
           20  +/*
           21  +** The code in this file only exists if we are not omitting the
           22  +** ALTER TABLE logic from the build.
           23  +*/
           24  +#ifndef SQLITE_OMIT_ALTERTABLE
           25  +
           26  +
           27  +/*
           28  +** This function is used by SQL generated to implement the 
           29  +** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
           30  +** CREATE INDEX command. The second is a table name. The table name in 
           31  +** the CREATE TABLE or CREATE INDEX statement is replaced with the third
           32  +** argument and the result returned. Examples:
           33  +**
           34  +** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
           35  +**     -> 'CREATE TABLE def(a, b, c)'
           36  +**
           37  +** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
           38  +**     -> 'CREATE INDEX i ON def(a, b, c)'
           39  +*/
           40  +static void renameTableFunc(
           41  +  sqlite3_context *context,
           42  +  int argc,
           43  +  sqlite3_value **argv
           44  +){
           45  +  unsigned char const *zSql = sqlite3_value_text(argv[0]);
           46  +  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
           47  +
           48  +  int token;
           49  +  Token tname;
           50  +  unsigned char const *zCsr = zSql;
           51  +  int len = 0;
           52  +  char *zRet;
           53  +
           54  +  sqlite3 *db = sqlite3_context_db_handle(context);
           55  +
           56  +  /* The principle used to locate the table name in the CREATE TABLE 
           57  +  ** statement is that the table name is the first non-space token that
           58  +  ** is immediately followed by a TK_LP or TK_USING token.
           59  +  */
           60  +  if( zSql ){
           61  +    do {
           62  +      if( !*zCsr ){
           63  +        /* Ran out of input before finding an opening bracket. Return NULL. */
           64  +        return;
           65  +      }
           66  +
           67  +      /* Store the token that zCsr points to in tname. */
           68  +      tname.z = zCsr;
           69  +      tname.n = len;
           70  +
           71  +      /* Advance zCsr to the next token. Store that token type in 'token',
           72  +      ** and its length in 'len' (to be used next iteration of this loop).
           73  +      */
           74  +      do {
           75  +        zCsr += len;
           76  +        len = sqlite3GetToken(zCsr, &token);
           77  +      } while( token==TK_SPACE || token==TK_COMMENT );
           78  +      assert( len>0 );
           79  +    } while( token!=TK_LP && token!=TK_USING );
           80  +
           81  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
           82  +       zTableName, tname.z+tname.n);
           83  +    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
           84  +  }
           85  +}
           86  +
           87  +#ifndef SQLITE_OMIT_TRIGGER
           88  +/* This function is used by SQL generated to implement the
           89  +** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER 
           90  +** statement. The second is a table name. The table name in the CREATE 
           91  +** TRIGGER statement is replaced with the third argument and the result 
           92  +** returned. This is analagous to renameTableFunc() above, except for CREATE
           93  +** TRIGGER, not CREATE INDEX and CREATE TABLE.
           94  +*/
           95  +static void renameTriggerFunc(
           96  +  sqlite3_context *context,
           97  +  int argc,
           98  +  sqlite3_value **argv
           99  +){
          100  +  unsigned char const *zSql = sqlite3_value_text(argv[0]);
          101  +  unsigned char const *zTableName = sqlite3_value_text(argv[1]);
          102  +
          103  +  int token;
          104  +  Token tname;
          105  +  int dist = 3;
          106  +  unsigned char const *zCsr = zSql;
          107  +  int len = 0;
          108  +  char *zRet;
          109  +
          110  +  sqlite3 *db = sqlite3_context_db_handle(context);
          111  +
          112  +  /* The principle used to locate the table name in the CREATE TRIGGER 
          113  +  ** statement is that the table name is the first token that is immediatedly
          114  +  ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
          115  +  ** of TK_WHEN, TK_BEGIN or TK_FOR.
          116  +  */
          117  +  if( zSql ){
          118  +    do {
          119  +
          120  +      if( !*zCsr ){
          121  +        /* Ran out of input before finding the table name. Return NULL. */
          122  +        return;
          123  +      }
          124  +
          125  +      /* Store the token that zCsr points to in tname. */
          126  +      tname.z = zCsr;
          127  +      tname.n = len;
          128  +
          129  +      /* Advance zCsr to the next token. Store that token type in 'token',
          130  +      ** and its length in 'len' (to be used next iteration of this loop).
          131  +      */
          132  +      do {
          133  +        zCsr += len;
          134  +        len = sqlite3GetToken(zCsr, &token);
          135  +      }while( token==TK_SPACE );
          136  +      assert( len>0 );
          137  +
          138  +      /* Variable 'dist' stores the number of tokens read since the most
          139  +      ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN 
          140  +      ** token is read and 'dist' equals 2, the condition stated above
          141  +      ** to be met.
          142  +      **
          143  +      ** Note that ON cannot be a database, table or column name, so
          144  +      ** there is no need to worry about syntax like 
          145  +      ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
          146  +      */
          147  +      dist++;
          148  +      if( token==TK_DOT || token==TK_ON ){
          149  +        dist = 0;
          150  +      }
          151  +    } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
          152  +
          153  +    /* Variable tname now contains the token that is the old table-name
          154  +    ** in the CREATE TRIGGER statement.
          155  +    */
          156  +    zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", tname.z - zSql, zSql, 
          157  +       zTableName, tname.z+tname.n);
          158  +    sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
          159  +  }
          160  +}
          161  +#endif   /* !SQLITE_OMIT_TRIGGER */
          162  +
          163  +/*
          164  +** Register built-in functions used to help implement ALTER TABLE
          165  +*/
          166  +void sqlite3AlterFunctions(sqlite3 *db){
          167  +  sqlite3CreateFunc(db, "sqlite_rename_table", 2, SQLITE_UTF8, 0,
          168  +                         renameTableFunc, 0, 0);
          169  +#ifndef SQLITE_OMIT_TRIGGER
          170  +  sqlite3CreateFunc(db, "sqlite_rename_trigger", 2, SQLITE_UTF8, 0,
          171  +                         renameTriggerFunc, 0, 0);
          172  +#endif
          173  +}
          174  +
          175  +/*
          176  +** Generate the text of a WHERE expression which can be used to select all
          177  +** temporary triggers on table pTab from the sqlite_temp_master table. If
          178  +** table pTab has no temporary triggers, or is itself stored in the 
          179  +** temporary database, NULL is returned.
          180  +*/
          181  +static char *whereTempTriggers(Parse *pParse, Table *pTab){
          182  +  Trigger *pTrig;
          183  +  char *zWhere = 0;
          184  +  char *tmp = 0;
          185  +  const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
          186  +
          187  +  /* If the table is not located in the temp-db (in which case NULL is 
          188  +  ** returned, loop through the tables list of triggers. For each trigger
          189  +  ** that is not part of the temp-db schema, add a clause to the WHERE 
          190  +  ** expression being built up in zWhere.
          191  +  */
          192  +  if( pTab->pSchema!=pTempSchema ){
          193  +    sqlite3 *db = pParse->db;
          194  +    for( pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext ){
          195  +      if( pTrig->pSchema==pTempSchema ){
          196  +        if( !zWhere ){
          197  +          zWhere = sqlite3MPrintf(db, "name=%Q", pTrig->name);
          198  +        }else{
          199  +          tmp = zWhere;
          200  +          zWhere = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, pTrig->name);
          201  +          sqlite3DbFree(db, tmp);
          202  +        }
          203  +      }
          204  +    }
          205  +  }
          206  +  return zWhere;
          207  +}
          208  +
          209  +/*
          210  +** Generate code to drop and reload the internal representation of table
          211  +** pTab from the database, including triggers and temporary triggers.
          212  +** Argument zName is the name of the table in the database schema at
          213  +** the time the generated code is executed. This can be different from
          214  +** pTab->zName if this function is being called to code part of an 
          215  +** "ALTER TABLE RENAME TO" statement.
          216  +*/
          217  +static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
          218  +  Vdbe *v;
          219  +  char *zWhere;
          220  +  int iDb;                   /* Index of database containing pTab */
          221  +#ifndef SQLITE_OMIT_TRIGGER
          222  +  Trigger *pTrig;
          223  +#endif
          224  +
          225  +  v = sqlite3GetVdbe(pParse);
          226  +  if( !v ) return;
          227  +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
          228  +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
          229  +  assert( iDb>=0 );
          230  +
          231  +#ifndef SQLITE_OMIT_TRIGGER
          232  +  /* Drop any table triggers from the internal schema. */
          233  +  for(pTrig=pTab->pTrigger; pTrig; pTrig=pTrig->pNext){
          234  +    int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
          235  +    assert( iTrigDb==iDb || iTrigDb==1 );
          236  +    sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->name, 0);
          237  +  }
          238  +#endif
          239  +
          240  +  /* Drop the table and index from the internal schema */
          241  +  sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
          242  +
          243  +  /* Reload the table, index and permanent trigger schemas. */
          244  +  zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
          245  +  if( !zWhere ) return;
          246  +  sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC);
          247  +
          248  +#ifndef SQLITE_OMIT_TRIGGER
          249  +  /* Now, if the table is not stored in the temp database, reload any temp 
          250  +  ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. 
          251  +  */
          252  +  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
          253  +    sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC);
          254  +  }
          255  +#endif
          256  +}
          257  +
          258  +/*
          259  +** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" 
          260  +** command. 
          261  +*/
          262  +void sqlite3AlterRenameTable(
          263  +  Parse *pParse,            /* Parser context. */
          264  +  SrcList *pSrc,            /* The table to rename. */
          265  +  Token *pName              /* The new table name. */
          266  +){
          267  +  int iDb;                  /* Database that contains the table */
          268  +  char *zDb;                /* Name of database iDb */
          269  +  Table *pTab;              /* Table being renamed */
          270  +  char *zName = 0;          /* NULL-terminated version of pName */ 
          271  +  sqlite3 *db = pParse->db; /* Database connection */
          272  +  int nTabName;             /* Number of UTF-8 characters in zTabName */
          273  +  const char *zTabName;     /* Original name of the table */
          274  +  Vdbe *v;
          275  +#ifndef SQLITE_OMIT_TRIGGER
          276  +  char *zWhere = 0;         /* Where clause to locate temp triggers */
          277  +#endif
          278  +  int isVirtualRename = 0;  /* True if this is a v-table with an xRename() */
          279  +  
          280  +  if( db->mallocFailed ) goto exit_rename_table;
          281  +  assert( pSrc->nSrc==1 );
          282  +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
          283  +
          284  +  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
          285  +  if( !pTab ) goto exit_rename_table;
          286  +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
          287  +  zDb = db->aDb[iDb].zName;
          288  +
          289  +  /* Get a NULL terminated version of the new table name. */
          290  +  zName = sqlite3NameFromToken(db, pName);
          291  +  if( !zName ) goto exit_rename_table;
          292  +
          293  +  /* Check that a table or index named 'zName' does not already exist
          294  +  ** in database iDb. If so, this is an error.
          295  +  */
          296  +  if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
          297  +    sqlite3ErrorMsg(pParse, 
          298  +        "there is already another table or index with this name: %s", zName);
          299  +    goto exit_rename_table;
          300  +  }
          301  +
          302  +  /* Make sure it is not a system table being altered, or a reserved name
          303  +  ** that the table is being renamed to.
          304  +  */
          305  +  if( strlen(pTab->zName)>6 && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) ){
          306  +    sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName);
          307  +    goto exit_rename_table;
          308  +  }
          309  +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
          310  +    goto exit_rename_table;
          311  +  }
          312  +
          313  +#ifndef SQLITE_OMIT_VIEW
          314  +  if( pTab->pSelect ){
          315  +    sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
          316  +    goto exit_rename_table;
          317  +  }
          318  +#endif
          319  +
          320  +#ifndef SQLITE_OMIT_AUTHORIZATION
          321  +  /* Invoke the authorization callback. */
          322  +  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
          323  +    goto exit_rename_table;
          324  +  }
          325  +#endif
          326  +
          327  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          328  +  if( sqlite3ViewGetColumnNames(pParse, pTab) ){
          329  +    goto exit_rename_table;
          330  +  }
          331  +  if( IsVirtual(pTab) && pTab->pMod->pModule->xRename ){
          332  +    isVirtualRename = 1;
          333  +  }
          334  +#endif
          335  +
          336  +  /* Begin a transaction and code the VerifyCookie for database iDb. 
          337  +  ** Then modify the schema cookie (since the ALTER TABLE modifies the
          338  +  ** schema). Open a statement transaction if the table is a virtual
          339  +  ** table.
          340  +  */
          341  +  v = sqlite3GetVdbe(pParse);
          342  +  if( v==0 ){
          343  +    goto exit_rename_table;
          344  +  }
          345  +  sqlite3BeginWriteOperation(pParse, isVirtualRename, iDb);
          346  +  sqlite3ChangeCookie(pParse, iDb);
          347  +
          348  +  /* If this is a virtual table, invoke the xRename() function if
          349  +  ** one is defined. The xRename() callback will modify the names
          350  +  ** of any resources used by the v-table implementation (including other
          351  +  ** SQLite tables) that are identified by the name of the virtual table.
          352  +  */
          353  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          354  +  if( isVirtualRename ){
          355  +    int i = ++pParse->nMem;
          356  +    sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
          357  +    sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pTab->pVtab, P4_VTAB);
          358  +  }
          359  +#endif
          360  +
          361  +  /* figure out how many UTF-8 characters are in zName */
          362  +  zTabName = pTab->zName;
          363  +  nTabName = sqlite3Utf8CharLen(zTabName, -1);
          364  +
          365  +  /* Modify the sqlite_master table to use the new table name. */
          366  +  sqlite3NestedParse(pParse,
          367  +      "UPDATE %Q.%s SET "
          368  +#ifdef SQLITE_OMIT_TRIGGER
          369  +          "sql = sqlite_rename_table(sql, %Q), "
          370  +#else
          371  +          "sql = CASE "
          372  +            "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
          373  +            "ELSE sqlite_rename_table(sql, %Q) END, "
          374  +#endif
          375  +          "tbl_name = %Q, "
          376  +          "name = CASE "
          377  +            "WHEN type='table' THEN %Q "
          378  +            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
          379  +             "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
          380  +            "ELSE name END "
          381  +      "WHERE tbl_name=%Q AND "
          382  +          "(type='table' OR type='index' OR type='trigger');", 
          383  +      zDb, SCHEMA_TABLE(iDb), zName, zName, zName, 
          384  +#ifndef SQLITE_OMIT_TRIGGER
          385  +      zName,
          386  +#endif
          387  +      zName, nTabName, zTabName
          388  +  );
          389  +
          390  +#ifndef SQLITE_OMIT_AUTOINCREMENT
          391  +  /* If the sqlite_sequence table exists in this database, then update 
          392  +  ** it with the new table name.
          393  +  */
          394  +  if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
          395  +    sqlite3NestedParse(pParse,
          396  +        "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
          397  +        zDb, zName, pTab->zName);
          398  +  }
          399  +#endif
          400  +
          401  +#ifndef SQLITE_OMIT_TRIGGER
          402  +  /* If there are TEMP triggers on this table, modify the sqlite_temp_master
          403  +  ** table. Don't do this if the table being ALTERed is itself located in
          404  +  ** the temp database.
          405  +  */
          406  +  if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
          407  +    sqlite3NestedParse(pParse, 
          408  +        "UPDATE sqlite_temp_master SET "
          409  +            "sql = sqlite_rename_trigger(sql, %Q), "
          410  +            "tbl_name = %Q "
          411  +            "WHERE %s;", zName, zName, zWhere);
          412  +    sqlite3DbFree(db, zWhere);
          413  +  }
          414  +#endif
          415  +
          416  +  /* Drop and reload the internal table schema. */
          417  +  reloadTableSchema(pParse, pTab, zName);
          418  +
          419  +exit_rename_table:
          420  +  sqlite3SrcListDelete(db, pSrc);
          421  +  sqlite3DbFree(db, zName);
          422  +}
          423  +
          424  +
          425  +/*
          426  +** This function is called after an "ALTER TABLE ... ADD" statement
          427  +** has been parsed. Argument pColDef contains the text of the new
          428  +** column definition.
          429  +**
          430  +** The Table structure pParse->pNewTable was extended to include
          431  +** the new column during parsing.
          432  +*/
          433  +void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
          434  +  Table *pNew;              /* Copy of pParse->pNewTable */
          435  +  Table *pTab;              /* Table being altered */
          436  +  int iDb;                  /* Database number */
          437  +  const char *zDb;          /* Database name */
          438  +  const char *zTab;         /* Table name */
          439  +  char *zCol;               /* Null-terminated column definition */
          440  +  Column *pCol;             /* The new column */
          441  +  Expr *pDflt;              /* Default value for the new column */
          442  +  sqlite3 *db;              /* The database connection; */
          443  +
          444  +  if( pParse->nErr ) return;
          445  +  pNew = pParse->pNewTable;
          446  +  assert( pNew );
          447  +
          448  +  db = pParse->db;
          449  +  assert( sqlite3BtreeHoldsAllMutexes(db) );
          450  +  iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
          451  +  zDb = db->aDb[iDb].zName;
          452  +  zTab = pNew->zName;
          453  +  pCol = &pNew->aCol[pNew->nCol-1];
          454  +  pDflt = pCol->pDflt;
          455  +  pTab = sqlite3FindTable(db, zTab, zDb);
          456  +  assert( pTab );
          457  +
          458  +#ifndef SQLITE_OMIT_AUTHORIZATION
          459  +  /* Invoke the authorization callback. */
          460  +  if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
          461  +    return;
          462  +  }
          463  +#endif
          464  +
          465  +  /* If the default value for the new column was specified with a 
          466  +  ** literal NULL, then set pDflt to 0. This simplifies checking
          467  +  ** for an SQL NULL default below.
          468  +  */
          469  +  if( pDflt && pDflt->op==TK_NULL ){
          470  +    pDflt = 0;
          471  +  }
          472  +
          473  +  /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
          474  +  ** If there is a NOT NULL constraint, then the default value for the
          475  +  ** column must not be NULL.
          476  +  */
          477  +  if( pCol->isPrimKey ){
          478  +    sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
          479  +    return;
          480  +  }
          481  +  if( pNew->pIndex ){
          482  +    sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
          483  +    return;
          484  +  }
          485  +  if( pCol->notNull && !pDflt ){
          486  +    sqlite3ErrorMsg(pParse, 
          487  +        "Cannot add a NOT NULL column with default value NULL");
          488  +    return;
          489  +  }
          490  +
          491  +  /* Ensure the default expression is something that sqlite3ValueFromExpr()
          492  +  ** can handle (i.e. not CURRENT_TIME etc.)
          493  +  */
          494  +  if( pDflt ){
          495  +    sqlite3_value *pVal;
          496  +    if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
          497  +      db->mallocFailed = 1;
          498  +      return;
          499  +    }
          500  +    if( !pVal ){
          501  +      sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
          502  +      return;
          503  +    }
          504  +    sqlite3ValueFree(pVal);
          505  +  }
          506  +
          507  +  /* Modify the CREATE TABLE statement. */
          508  +  zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
          509  +  if( zCol ){
          510  +    char *zEnd = &zCol[pColDef->n-1];
          511  +    while( (zEnd>zCol && *zEnd==';') || isspace(*(unsigned char *)zEnd) ){
          512  +      *zEnd-- = '\0';
          513  +    }
          514  +    sqlite3NestedParse(pParse, 
          515  +        "UPDATE \"%w\".%s SET "
          516  +          "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
          517  +        "WHERE type = 'table' AND name = %Q", 
          518  +      zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
          519  +      zTab
          520  +    );
          521  +    sqlite3DbFree(db, zCol);
          522  +  }
          523  +
          524  +  /* If the default value of the new column is NULL, then set the file
          525  +  ** format to 2. If the default value of the new column is not NULL,
          526  +  ** the file format becomes 3.
          527  +  */
          528  +  sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
          529  +
          530  +  /* Reload the schema of the modified table. */
          531  +  reloadTableSchema(pParse, pTab, pTab->zName);
          532  +}
          533  +
          534  +/*
          535  +** This function is called by the parser after the table-name in
          536  +** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument 
          537  +** pSrc is the full-name of the table being altered.
          538  +**
          539  +** This routine makes a (partial) copy of the Table structure
          540  +** for the table being altered and sets Parse.pNewTable to point
          541  +** to it. Routines called by the parser as the column definition
          542  +** is parsed (i.e. sqlite3AddColumn()) add the new Column data to 
          543  +** the copy. The copy of the Table structure is deleted by tokenize.c 
          544  +** after parsing is finished.
          545  +**
          546  +** Routine sqlite3AlterFinishAddColumn() will be called to complete
          547  +** coding the "ALTER TABLE ... ADD" statement.
          548  +*/
          549  +void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
          550  +  Table *pNew;
          551  +  Table *pTab;
          552  +  Vdbe *v;
          553  +  int iDb;
          554  +  int i;
          555  +  int nAlloc;
          556  +  sqlite3 *db = pParse->db;
          557  +
          558  +  /* Look up the table being altered. */
          559  +  assert( pParse->pNewTable==0 );
          560  +  assert( sqlite3BtreeHoldsAllMutexes(db) );
          561  +  if( db->mallocFailed ) goto exit_begin_add_column;
          562  +  pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase);
          563  +  if( !pTab ) goto exit_begin_add_column;
          564  +
          565  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          566  +  if( IsVirtual(pTab) ){
          567  +    sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
          568  +    goto exit_begin_add_column;
          569  +  }
          570  +#endif
          571  +
          572  +  /* Make sure this is not an attempt to ALTER a view. */
          573  +  if( pTab->pSelect ){
          574  +    sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
          575  +    goto exit_begin_add_column;
          576  +  }
          577  +
          578  +  assert( pTab->addColOffset>0 );
          579  +  iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
          580  +
          581  +  /* Put a copy of the Table struct in Parse.pNewTable for the
          582  +  ** sqlite3AddColumn() function and friends to modify.
          583  +  */
          584  +  pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
          585  +  if( !pNew ) goto exit_begin_add_column;
          586  +  pParse->pNewTable = pNew;
          587  +  pNew->nRef = 1;
          588  +  pNew->db = db;
          589  +  pNew->nCol = pTab->nCol;
          590  +  assert( pNew->nCol>0 );
          591  +  nAlloc = (((pNew->nCol-1)/8)*8)+8;
          592  +  assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
          593  +  pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
          594  +  pNew->zName = sqlite3DbStrDup(db, pTab->zName);
          595  +  if( !pNew->aCol || !pNew->zName ){
          596  +    db->mallocFailed = 1;
          597  +    goto exit_begin_add_column;
          598  +  }
          599  +  memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
          600  +  for(i=0; i<pNew->nCol; i++){
          601  +    Column *pCol = &pNew->aCol[i];
          602  +    pCol->zName = sqlite3DbStrDup(db, pCol->zName);
          603  +    pCol->zColl = 0;
          604  +    pCol->zType = 0;
          605  +    pCol->pDflt = 0;
          606  +  }
          607  +  pNew->pSchema = db->aDb[iDb].pSchema;
          608  +  pNew->addColOffset = pTab->addColOffset;
          609  +  pNew->nRef = 1;
          610  +
          611  +  /* Begin a transaction and increment the schema cookie.  */
          612  +  sqlite3BeginWriteOperation(pParse, 0, iDb);
          613  +  v = sqlite3GetVdbe(pParse);
          614  +  if( !v ) goto exit_begin_add_column;
          615  +  sqlite3ChangeCookie(pParse, iDb);
          616  +
          617  +exit_begin_add_column:
          618  +  sqlite3SrcListDelete(db, pSrc);
          619  +  return;
          620  +}
          621  +#endif  /* SQLITE_ALTER_TABLE */

Added SQLite.Interop/splitsource/analyze.c.

            1  +/*
            2  +** 2005 July 8
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This file contains code associated with the ANALYZE command.
           13  +**
           14  +** @(#) $Id: analyze.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           15  +*/
           16  +#ifndef SQLITE_OMIT_ANALYZE
           17  +#include "sqliteInt.h"
           18  +
           19  +/*
           20  +** This routine generates code that opens the sqlite_stat1 table on cursor
           21  +** iStatCur.
           22  +**
           23  +** If the sqlite_stat1 tables does not previously exist, it is created.
           24  +** If it does previously exist, all entires associated with table zWhere
           25  +** are removed.  If zWhere==0 then all entries are removed.
           26  +*/
           27  +static void openStatTable(
           28  +  Parse *pParse,          /* Parsing context */
           29  +  int iDb,                /* The database we are looking in */
           30  +  int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
           31  +  const char *zWhere      /* Delete entries associated with this table */
           32  +){
           33  +  sqlite3 *db = pParse->db;
           34  +  Db *pDb;
           35  +  int iRootPage;
           36  +  int createStat1 = 0;
           37  +  Table *pStat;
           38  +  Vdbe *v = sqlite3GetVdbe(pParse);
           39  +
           40  +  if( v==0 ) return;
           41  +  assert( sqlite3BtreeHoldsAllMutexes(db) );
           42  +  assert( sqlite3VdbeDb(v)==db );
           43  +  pDb = &db->aDb[iDb];
           44  +  if( (pStat = sqlite3FindTable(db, "sqlite_stat1", pDb->zName))==0 ){
           45  +    /* The sqlite_stat1 tables does not exist.  Create it.  
           46  +    ** Note that a side-effect of the CREATE TABLE statement is to leave
           47  +    ** the rootpage of the new table in register pParse->regRoot.  This is
           48  +    ** important because the OpenWrite opcode below will be needing it. */
           49  +    sqlite3NestedParse(pParse,
           50  +      "CREATE TABLE %Q.sqlite_stat1(tbl,idx,stat)",
           51  +      pDb->zName
           52  +    );
           53  +    iRootPage = pParse->regRoot;
           54  +    createStat1 = 1;  /* Cause rootpage to be taken from top of stack */
           55  +  }else if( zWhere ){
           56  +    /* The sqlite_stat1 table exists.  Delete all entries associated with
           57  +    ** the table zWhere. */
           58  +    sqlite3NestedParse(pParse,
           59  +       "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q",
           60  +       pDb->zName, zWhere
           61  +    );
           62  +    iRootPage = pStat->tnum;
           63  +  }else{
           64  +    /* The sqlite_stat1 table already exists.  Delete all rows. */
           65  +    iRootPage = pStat->tnum;
           66  +    sqlite3VdbeAddOp2(v, OP_Clear, pStat->tnum, iDb);
           67  +  }
           68  +
           69  +  /* Open the sqlite_stat1 table for writing. Unless it was created
           70  +  ** by this vdbe program, lock it for writing at the shared-cache level. 
           71  +  ** If this vdbe did create the sqlite_stat1 table, then it must have 
           72  +  ** already obtained a schema-lock, making the write-lock redundant.
           73  +  */
           74  +  if( !createStat1 ){
           75  +    sqlite3TableLock(pParse, iDb, iRootPage, 1, "sqlite_stat1");
           76  +  }
           77  +  sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, 3);
           78  +  sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur, iRootPage, iDb);
           79  +  sqlite3VdbeChangeP5(v, createStat1);
           80  +}
           81  +
           82  +/*
           83  +** Generate code to do an analysis of all indices associated with
           84  +** a single table.
           85  +*/
           86  +static void analyzeOneTable(
           87  +  Parse *pParse,   /* Parser context */
           88  +  Table *pTab,     /* Table whose indices are to be analyzed */
           89  +  int iStatCur,    /* Cursor that writes to the sqlite_stat1 table */
           90  +  int iMem         /* Available memory locations begin here */
           91  +){
           92  +  Index *pIdx;     /* An index to being analyzed */
           93  +  int iIdxCur;     /* Cursor number for index being analyzed */
           94  +  int nCol;        /* Number of columns in the index */
           95  +  Vdbe *v;         /* The virtual machine being built up */
           96  +  int i;           /* Loop counter */
           97  +  int topOfLoop;   /* The top of the loop */
           98  +  int endOfLoop;   /* The end of the loop */
           99  +  int addr;        /* The address of an instruction */
          100  +  int iDb;         /* Index of database containing pTab */
          101  +
          102  +  v = sqlite3GetVdbe(pParse);
          103  +  if( v==0 || pTab==0 || pTab->pIndex==0 ){
          104  +    /* Do no analysis for tables that have no indices */
          105  +    return;
          106  +  }
          107  +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
          108  +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
          109  +  assert( iDb>=0 );
          110  +#ifndef SQLITE_OMIT_AUTHORIZATION
          111  +  if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
          112  +      pParse->db->aDb[iDb].zName ) ){
          113  +    return;
          114  +  }
          115  +#endif
          116  +
          117  +  /* Establish a read-lock on the table at the shared-cache level. */
          118  +  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
          119  +
          120  +  iIdxCur = pParse->nTab;
          121  +  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
          122  +    KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
          123  +    int regFields;    /* Register block for building records */
          124  +    int regRec;       /* Register holding completed record */
          125  +    int regTemp;      /* Temporary use register */
          126  +    int regCol;       /* Content of a column from the table being analyzed */
          127  +    int regRowid;     /* Rowid for the inserted record */
          128  +    int regF2;
          129  +
          130  +    /* Open a cursor to the index to be analyzed
          131  +    */
          132  +    assert( iDb==sqlite3SchemaToIndex(pParse->db, pIdx->pSchema) );
          133  +    nCol = pIdx->nColumn;
          134  +    sqlite3VdbeAddOp2(v, OP_SetNumColumns, 0, nCol+1);
          135  +    sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
          136  +        (char *)pKey, P4_KEYINFO_HANDOFF);
          137  +    VdbeComment((v, "%s", pIdx->zName));
          138  +    regFields = iMem+nCol*2;
          139  +    regTemp = regRowid = regCol = regFields+3;
          140  +    regRec = regCol+1;
          141  +    if( regRec>pParse->nMem ){
          142  +      pParse->nMem = regRec;
          143  +    }
          144  +
          145  +    /* Memory cells are used as follows:
          146  +    **
          147  +    **    mem[iMem]:             The total number of rows in the table.
          148  +    **    mem[iMem+1]:           Number of distinct values in column 1
          149  +    **    ...
          150  +    **    mem[iMem+nCol]:        Number of distinct values in column N
          151  +    **    mem[iMem+nCol+1]       Last observed value of column 1
          152  +    **    ...
          153  +    **    mem[iMem+nCol+nCol]:   Last observed value of column N
          154  +    **
          155  +    ** Cells iMem through iMem+nCol are initialized to 0.  The others
          156  +    ** are initialized to NULL.
          157  +    */
          158  +    for(i=0; i<=nCol; i++){
          159  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
          160  +    }
          161  +    for(i=0; i<nCol; i++){
          162  +      sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
          163  +    }
          164  +
          165  +    /* Do the analysis.
          166  +    */
          167  +    endOfLoop = sqlite3VdbeMakeLabel(v);
          168  +    sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
          169  +    topOfLoop = sqlite3VdbeCurrentAddr(v);
          170  +    sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
          171  +    for(i=0; i<nCol; i++){
          172  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
          173  +      sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
          174  +      /**** TODO:  add collating sequence *****/
          175  +      sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
          176  +    }
          177  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
          178  +    for(i=0; i<nCol; i++){
          179  +      sqlite3VdbeJumpHere(v, topOfLoop + 2*(i + 1));
          180  +      sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
          181  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
          182  +    }
          183  +    sqlite3VdbeResolveLabel(v, endOfLoop);
          184  +    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
          185  +    sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
          186  +
          187  +    /* Store the results.  
          188  +    **
          189  +    ** The result is a single row of the sqlite_stat1 table.  The first
          190  +    ** two columns are the names of the table and index.  The third column
          191  +    ** is a string composed of a list of integer statistics about the
          192  +    ** index.  The first integer in the list is the total number of entires
          193  +    ** in the index.  There is one additional integer in the list for each
          194  +    ** column of the table.  This additional integer is a guess of how many
          195  +    ** rows of the table the index will select.  If D is the count of distinct
          196  +    ** values and K is the total number of rows, then the integer is computed
          197  +    ** as:
          198  +    **
          199  +    **        I = (K+D-1)/D
          200  +    **
          201  +    ** If K==0 then no entry is made into the sqlite_stat1 table.  
          202  +    ** If K>0 then it is always the case the D>0 so division by zero
          203  +    ** is never possible.
          204  +    */
          205  +    addr = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
          206  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields, 0, pTab->zName, 0);
          207  +    sqlite3VdbeAddOp4(v, OP_String8, 0, regFields+1, 0, pIdx->zName, 0);
          208  +    regF2 = regFields+2;
          209  +    sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regF2);
          210  +    for(i=0; i<nCol; i++){
          211  +      sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
          212  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
          213  +      sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
          214  +      sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
          215  +      sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
          216  +      sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
          217  +      sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regF2, regF2);
          218  +    }
          219  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regFields, 3, regRec, "aaa", 0);
          220  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
          221  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
          222  +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
          223  +    sqlite3VdbeJumpHere(v, addr);
          224  +  }
          225  +}
          226  +
          227  +/*
          228  +** Generate code that will cause the most recent index analysis to
          229  +** be laoded into internal hash tables where is can be used.
          230  +*/
          231  +static void loadAnalysis(Parse *pParse, int iDb){
          232  +  Vdbe *v = sqlite3GetVdbe(pParse);
          233  +  if( v ){
          234  +    sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
          235  +  }
          236  +}
          237  +
          238  +/*
          239  +** Generate code that will do an analysis of an entire database
          240  +*/
          241  +static void analyzeDatabase(Parse *pParse, int iDb){
          242  +  sqlite3 *db = pParse->db;
          243  +  Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
          244  +  HashElem *k;
          245  +  int iStatCur;
          246  +  int iMem;
          247  +
          248  +  sqlite3BeginWriteOperation(pParse, 0, iDb);
          249  +  iStatCur = pParse->nTab++;
          250  +  openStatTable(pParse, iDb, iStatCur, 0);
          251  +  iMem = pParse->nMem+1;
          252  +  for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
          253  +    Table *pTab = (Table*)sqliteHashData(k);
          254  +    analyzeOneTable(pParse, pTab, iStatCur, iMem);
          255  +  }
          256  +  loadAnalysis(pParse, iDb);
          257  +}
          258  +
          259  +/*
          260  +** Generate code that will do an analysis of a single table in
          261  +** a database.
          262  +*/
          263  +static void analyzeTable(Parse *pParse, Table *pTab){
          264  +  int iDb;
          265  +  int iStatCur;
          266  +
          267  +  assert( pTab!=0 );
          268  +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
          269  +  iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
          270  +  sqlite3BeginWriteOperation(pParse, 0, iDb);
          271  +  iStatCur = pParse->nTab++;
          272  +  openStatTable(pParse, iDb, iStatCur, pTab->zName);
          273  +  analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
          274  +  loadAnalysis(pParse, iDb);
          275  +}
          276  +
          277  +/*
          278  +** Generate code for the ANALYZE command.  The parser calls this routine
          279  +** when it recognizes an ANALYZE command.
          280  +**
          281  +**        ANALYZE                            -- 1
          282  +**        ANALYZE  <database>                -- 2
          283  +**        ANALYZE  ?<database>.?<tablename>  -- 3
          284  +**
          285  +** Form 1 causes all indices in all attached databases to be analyzed.
          286  +** Form 2 analyzes all indices the single database named.
          287  +** Form 3 analyzes all indices associated with the named table.
          288  +*/
          289  +void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
          290  +  sqlite3 *db = pParse->db;
          291  +  int iDb;
          292  +  int i;
          293  +  char *z, *zDb;
          294  +  Table *pTab;
          295  +  Token *pTableName;
          296  +
          297  +  /* Read the database schema. If an error occurs, leave an error message
          298  +  ** and code in pParse and return NULL. */
          299  +  assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
          300  +  if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
          301  +    return;
          302  +  }
          303  +
          304  +  if( pName1==0 ){
          305  +    /* Form 1:  Analyze everything */
          306  +    for(i=0; i<db->nDb; i++){
          307  +      if( i==1 ) continue;  /* Do not analyze the TEMP database */
          308  +      analyzeDatabase(pParse, i);
          309  +    }
          310  +  }else if( pName2==0 || pName2->n==0 ){
          311  +    /* Form 2:  Analyze the database or table named */
          312  +    iDb = sqlite3FindDb(db, pName1);
          313  +    if( iDb>=0 ){
          314  +      analyzeDatabase(pParse, iDb);
          315  +    }else{
          316  +      z = sqlite3NameFromToken(db, pName1);
          317  +      if( z ){
          318  +        pTab = sqlite3LocateTable(pParse, 0, z, 0);
          319  +        sqlite3DbFree(db, z);
          320  +        if( pTab ){
          321  +          analyzeTable(pParse, pTab);
          322  +        }
          323  +      }
          324  +    }
          325  +  }else{
          326  +    /* Form 3: Analyze the fully qualified table name */
          327  +    iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
          328  +    if( iDb>=0 ){
          329  +      zDb = db->aDb[iDb].zName;
          330  +      z = sqlite3NameFromToken(db, pTableName);
          331  +      if( z ){
          332  +        pTab = sqlite3LocateTable(pParse, 0, z, zDb);
          333  +        sqlite3DbFree(db, z);
          334  +        if( pTab ){
          335  +          analyzeTable(pParse, pTab);
          336  +        }
          337  +      }
          338  +    }   
          339  +  }
          340  +}
          341  +
          342  +/*
          343  +** Used to pass information from the analyzer reader through to the
          344  +** callback routine.
          345  +*/
          346  +typedef struct analysisInfo analysisInfo;
          347  +struct analysisInfo {
          348  +  sqlite3 *db;
          349  +  const char *zDatabase;
          350  +};
          351  +
          352  +/*
          353  +** This callback is invoked once for each index when reading the
          354  +** sqlite_stat1 table.  
          355  +**
          356  +**     argv[0] = name of the index
          357  +**     argv[1] = results of analysis - on integer for each column
          358  +*/
          359  +static int analysisLoader(void *pData, int argc, char **argv, char **azNotUsed){
          360  +  analysisInfo *pInfo = (analysisInfo*)pData;
          361  +  Index *pIndex;
          362  +  int i, c;
          363  +  unsigned int v;
          364  +  const char *z;
          365  +
          366  +  assert( argc==2 );
          367  +  if( argv==0 || argv[0]==0 || argv[1]==0 ){
          368  +    return 0;
          369  +  }
          370  +  pIndex = sqlite3FindIndex(pInfo->db, argv[0], pInfo->zDatabase);
          371  +  if( pIndex==0 ){
          372  +    return 0;
          373  +  }
          374  +  z = argv[1];
          375  +  for(i=0; *z && i<=pIndex->nColumn; i++){
          376  +    v = 0;
          377  +    while( (c=z[0])>='0' && c<='9' ){
          378  +      v = v*10 + c - '0';
          379  +      z++;
          380  +    }
          381  +    pIndex->aiRowEst[i] = v;
          382  +    if( *z==' ' ) z++;
          383  +  }
          384  +  return 0;
          385  +}
          386  +
          387  +/*
          388  +** Load the content of the sqlite_stat1 table into the index hash tables.
          389  +*/
          390  +int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
          391  +  analysisInfo sInfo;
          392  +  HashElem *i;
          393  +  char *zSql;
          394  +  int rc;
          395  +
          396  +  assert( iDb>=0 && iDb<db->nDb );
          397  +  assert( db->aDb[iDb].pBt!=0 );
          398  +  assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
          399  +
          400  +  /* Clear any prior statistics */
          401  +  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
          402  +    Index *pIdx = sqliteHashData(i);
          403  +    sqlite3DefaultRowEst(pIdx);
          404  +  }
          405  +
          406  +  /* Check to make sure the sqlite_stat1 table existss */
          407  +  sInfo.db = db;
          408  +  sInfo.zDatabase = db->aDb[iDb].zName;
          409  +  if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
          410  +     return SQLITE_ERROR;
          411  +  }
          412  +
          413  +
          414  +  /* Load new statistics out of the sqlite_stat1 table */
          415  +  zSql = sqlite3MPrintf(db, "SELECT idx, stat FROM %Q.sqlite_stat1",
          416  +                        sInfo.zDatabase);
          417  +  (void)sqlite3SafetyOff(db);
          418  +  rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
          419  +  (void)sqlite3SafetyOn(db);
          420  +  sqlite3DbFree(db, zSql);
          421  +  return rc;
          422  +}
          423  +
          424  +
          425  +#endif /* SQLITE_OMIT_ANALYZE */

Added SQLite.Interop/splitsource/attach.c.

            1  +/*
            2  +** 2003 April 6
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This file contains code used to implement the ATTACH and DETACH commands.
           13  +**
           14  +** $Id: attach.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           15  +*/
           16  +#include "sqliteInt.h"
           17  +
           18  +#ifndef SQLITE_OMIT_ATTACH
           19  +/*
           20  +** Resolve an expression that was part of an ATTACH or DETACH statement. This
           21  +** is slightly different from resolving a normal SQL expression, because simple
           22  +** identifiers are treated as strings, not possible column names or aliases.
           23  +**
           24  +** i.e. if the parser sees:
           25  +**
           26  +**     ATTACH DATABASE abc AS def
           27  +**
           28  +** it treats the two expressions as literal strings 'abc' and 'def' instead of
           29  +** looking for columns of the same name.
           30  +**
           31  +** This only applies to the root node of pExpr, so the statement:
           32  +**
           33  +**     ATTACH DATABASE abc||def AS 'db2'
           34  +**
           35  +** will fail because neither abc or def can be resolved.
           36  +*/
           37  +static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
           38  +{
           39  +  int rc = SQLITE_OK;
           40  +  if( pExpr ){
           41  +    if( pExpr->op!=TK_ID ){
           42  +      rc = sqlite3ExprResolveNames(pName, pExpr);
           43  +      if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){
           44  +        sqlite3ErrorMsg(pName->pParse, "invalid name: \"%T\"", &pExpr->span);
           45  +        return SQLITE_ERROR;
           46  +      }
           47  +    }else{
           48  +      pExpr->op = TK_STRING;
           49  +    }
           50  +  }
           51  +  return rc;
           52  +}
           53  +
           54  +/*
           55  +** An SQL user-function registered to do the work of an ATTACH statement. The
           56  +** three arguments to the function come directly from an attach statement:
           57  +**
           58  +**     ATTACH DATABASE x AS y KEY z
           59  +**
           60  +**     SELECT sqlite_attach(x, y, z)
           61  +**
           62  +** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
           63  +** third argument.
           64  +*/
           65  +static void attachFunc(
           66  +  sqlite3_context *context,
           67  +  int argc,
           68  +  sqlite3_value **argv
           69  +){
           70  +  int i;
           71  +  int rc = 0;
           72  +  sqlite3 *db = sqlite3_context_db_handle(context);
           73  +  const char *zName;
           74  +  const char *zFile;
           75  +  Db *aNew;
           76  +  char *zErrDyn = 0;
           77  +  char zErr[128];
           78  +
           79  +  zFile = (const char *)sqlite3_value_text(argv[0]);
           80  +  zName = (const char *)sqlite3_value_text(argv[1]);
           81  +  if( zFile==0 ) zFile = "";
           82  +  if( zName==0 ) zName = "";
           83  +
           84  +  /* Check for the following errors:
           85  +  **
           86  +  **     * Too many attached databases,
           87  +  **     * Transaction currently open
           88  +  **     * Specified database name already being used.
           89  +  */
           90  +  if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
           91  +    sqlite3_snprintf(
           92  +      sizeof(zErr), zErr, "too many attached databases - max %d", 
           93  +      db->aLimit[SQLITE_LIMIT_ATTACHED]
           94  +    );
           95  +    goto attach_error;
           96  +  }
           97  +  if( !db->autoCommit ){
           98  +    sqlite3_snprintf(sizeof(zErr), zErr,
           99  +                     "cannot ATTACH database within transaction");
          100  +    goto attach_error;
          101  +  }
          102  +  for(i=0; i<db->nDb; i++){
          103  +    char *z = db->aDb[i].zName;
          104  +    if( z && zName && sqlite3StrICmp(z, zName)==0 ){
          105  +      sqlite3_snprintf(sizeof(zErr), zErr, 
          106  +                       "database %s is already in use", zName);
          107  +      goto attach_error;
          108  +    }
          109  +  }
          110  +
          111  +  /* Allocate the new entry in the db->aDb[] array and initialise the schema
          112  +  ** hash tables.
          113  +  */
          114  +  if( db->aDb==db->aDbStatic ){
          115  +    aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
          116  +    if( aNew==0 ) return;
          117  +    memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
          118  +  }else{
          119  +    aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
          120  +    if( aNew==0 ) return;
          121  +  }
          122  +  db->aDb = aNew;
          123  +  aNew = &db->aDb[db->nDb++];
          124  +  memset(aNew, 0, sizeof(*aNew));
          125  +
          126  +  /* Open the database file. If the btree is successfully opened, use
          127  +  ** it to obtain the database schema. At this point the schema may
          128  +  ** or may not be initialised.
          129  +  */
          130  +  rc = sqlite3BtreeFactory(db, zFile, 0, SQLITE_DEFAULT_CACHE_SIZE,
          131  +                           db->openFlags | SQLITE_OPEN_MAIN_DB,
          132  +                           &aNew->pBt);
          133  +  if( rc==SQLITE_OK ){
          134  +    Pager *pPager;
          135  +    aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
          136  +    if( !aNew->pSchema ){
          137  +      rc = SQLITE_NOMEM;
          138  +    }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
          139  +      sqlite3_snprintf(sizeof(zErr), zErr, 
          140  +        "attached databases must use the same text encoding as main database");
          141  +      goto attach_error;
          142  +    }
          143  +    pPager = sqlite3BtreePager(aNew->pBt);
          144  +    sqlite3PagerLockingMode(pPager, db->dfltLockMode);
          145  +    sqlite3PagerJournalMode(pPager, db->dfltJournalMode);
          146  +  }
          147  +  aNew->zName = sqlite3DbStrDup(db, zName);
          148  +  aNew->safety_level = 3;
          149  +
          150  +#if SQLITE_HAS_CODEC
          151  +  {
          152  +    extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
          153  +    extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
          154  +    int nKey;
          155  +    char *zKey;
          156  +    int t = sqlite3_value_type(argv[2]);
          157  +    switch( t ){
          158  +      case SQLITE_INTEGER:
          159  +      case SQLITE_FLOAT:
          160  +        zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
          161  +        rc = SQLITE_ERROR;
          162  +        break;
          163  +        
          164  +      case SQLITE_TEXT:
          165  +      case SQLITE_BLOB:
          166  +        nKey = sqlite3_value_bytes(argv[2]);
          167  +        zKey = (char *)sqlite3_value_blob(argv[2]);
          168  +        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
          169  +        break;
          170  +
          171  +      case SQLITE_NULL:
          172  +        /* No key specified.  Use the key from the main database */
          173  +        sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
          174  +        sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
          175  +        break;
          176  +    }
          177  +  }
          178  +#endif
          179  +
          180  +  /* If the file was opened successfully, read the schema for the new database.
          181  +  ** If this fails, or if opening the file failed, then close the file and 
          182  +  ** remove the entry from the db->aDb[] array. i.e. put everything back the way
          183  +  ** we found it.
          184  +  */
          185  +  if( rc==SQLITE_OK ){
          186  +    (void)sqlite3SafetyOn(db);
          187  +    sqlite3BtreeEnterAll(db);
          188  +    rc = sqlite3Init(db, &zErrDyn);
          189  +    sqlite3BtreeLeaveAll(db);
          190  +    (void)sqlite3SafetyOff(db);
          191  +  }
          192  +  if( rc ){
          193  +    int iDb = db->nDb - 1;
          194  +    assert( iDb>=2 );
          195  +    if( db->aDb[iDb].pBt ){
          196  +      sqlite3BtreeClose(db->aDb[iDb].pBt);
          197  +      db->aDb[iDb].pBt = 0;
          198  +      db->aDb[iDb].pSchema = 0;
          199  +    }
          200  +    sqlite3ResetInternalSchema(db, 0);
          201  +    db->nDb = iDb;
          202  +    if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
          203  +      db->mallocFailed = 1;
          204  +      sqlite3_snprintf(sizeof(zErr),zErr, "out of memory");
          205  +    }else{
          206  +      sqlite3_snprintf(sizeof(zErr),zErr, "unable to open database: %s", zFile);
          207  +    }
          208  +    goto attach_error;
          209  +  }
          210  +  
          211  +  return;
          212  +
          213  +attach_error:
          214  +  /* Return an error if we get here */
          215  +  if( zErrDyn ){
          216  +    sqlite3_result_error(context, zErrDyn, -1);
          217  +    sqlite3DbFree(db, zErrDyn);
          218  +  }else{
          219  +    zErr[sizeof(zErr)-1] = 0;
          220  +    sqlite3_result_error(context, zErr, -1);
          221  +  }
          222  +  if( rc ) sqlite3_result_error_code(context, rc);
          223  +}
          224  +
          225  +/*
          226  +** An SQL user-function registered to do the work of an DETACH statement. The
          227  +** three arguments to the function come directly from a detach statement:
          228  +**
          229  +**     DETACH DATABASE x
          230  +**
          231  +**     SELECT sqlite_detach(x)
          232  +*/
          233  +static void detachFunc(
          234  +  sqlite3_context *context,
          235  +  int argc,
          236  +  sqlite3_value **argv
          237  +){
          238  +  const char *zName = (const char *)sqlite3_value_text(argv[0]);
          239  +  sqlite3 *db = sqlite3_context_db_handle(context);
          240  +  int i;
          241  +  Db *pDb = 0;
          242  +  char zErr[128];
          243  +
          244  +  if( zName==0 ) zName = "";
          245  +  for(i=0; i<db->nDb; i++){
          246  +    pDb = &db->aDb[i];
          247  +    if( pDb->pBt==0 ) continue;
          248  +    if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
          249  +  }
          250  +
          251  +  if( i>=db->nDb ){
          252  +    sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
          253  +    goto detach_error;
          254  +  }
          255  +  if( i<2 ){
          256  +    sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
          257  +    goto detach_error;
          258  +  }
          259  +  if( !db->autoCommit ){
          260  +    sqlite3_snprintf(sizeof(zErr), zErr,
          261  +                     "cannot DETACH database within transaction");
          262  +    goto detach_error;
          263  +  }
          264  +  if( sqlite3BtreeIsInReadTrans(pDb->pBt) ){
          265  +    sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
          266  +    goto detach_error;
          267  +  }
          268  +
          269  +  sqlite3BtreeClose(pDb->pBt);
          270  +  pDb->pBt = 0;
          271  +  pDb->pSchema = 0;
          272  +  sqlite3ResetInternalSchema(db, 0);
          273  +  return;
          274  +
          275  +detach_error:
          276  +  sqlite3_result_error(context, zErr, -1);
          277  +}
          278  +
          279  +/*
          280  +** This procedure generates VDBE code for a single invocation of either the
          281  +** sqlite_detach() or sqlite_attach() SQL user functions.
          282  +*/
          283  +static void codeAttach(
          284  +  Parse *pParse,       /* The parser context */
          285  +  int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
          286  +  const char *zFunc,   /* Either "sqlite_attach" or "sqlite_detach */
          287  +  int nFunc,           /* Number of args to pass to zFunc */
          288  +  Expr *pAuthArg,      /* Expression to pass to authorization callback */
          289  +  Expr *pFilename,     /* Name of database file */
          290  +  Expr *pDbname,       /* Name of the database to use internally */
          291  +  Expr *pKey           /* Database key for encryption extension */
          292  +){
          293  +  int rc;
          294  +  NameContext sName;
          295  +  Vdbe *v;
          296  +  FuncDef *pFunc;
          297  +  sqlite3* db = pParse->db;
          298  +  int regArgs;
          299  +
          300  +#ifndef SQLITE_OMIT_AUTHORIZATION
          301  +  assert( db->mallocFailed || pAuthArg );
          302  +  if( pAuthArg ){
          303  +    char *zAuthArg = sqlite3NameFromToken(db, &pAuthArg->span);
          304  +    if( !zAuthArg ){
          305  +      goto attach_end;
          306  +    }
          307  +    rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
          308  +    sqlite3DbFree(db, zAuthArg);
          309  +    if(rc!=SQLITE_OK ){
          310  +      goto attach_end;
          311  +    }
          312  +  }
          313  +#endif /* SQLITE_OMIT_AUTHORIZATION */
          314  +
          315  +  memset(&sName, 0, sizeof(NameContext));
          316  +  sName.pParse = pParse;
          317  +
          318  +  if( 
          319  +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
          320  +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
          321  +      SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
          322  +  ){
          323  +    pParse->nErr++;
          324  +    goto attach_end;
          325  +  }
          326  +
          327  +  v = sqlite3GetVdbe(pParse);
          328  +  regArgs = sqlite3GetTempRange(pParse, 4);
          329  +  sqlite3ExprCode(pParse, pFilename, regArgs);
          330  +  sqlite3ExprCode(pParse, pDbname, regArgs+1);
          331  +  sqlite3ExprCode(pParse, pKey, regArgs+2);
          332  +
          333  +  assert( v || db->mallocFailed );
          334  +  if( v ){
          335  +    sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-nFunc, regArgs+3);
          336  +    sqlite3VdbeChangeP5(v, nFunc);
          337  +    pFunc = sqlite3FindFunction(db, zFunc, strlen(zFunc), nFunc, SQLITE_UTF8,0);
          338  +    sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
          339  +
          340  +    /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
          341  +    ** statement only). For DETACH, set it to false (expire all existing
          342  +    ** statements).
          343  +    */
          344  +    sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
          345  +  }
          346  +  
          347  +attach_end:
          348  +  sqlite3ExprDelete(db, pFilename);
          349  +  sqlite3ExprDelete(db, pDbname);
          350  +  sqlite3ExprDelete(db, pKey);
          351  +}
          352  +
          353  +/*
          354  +** Called by the parser to compile a DETACH statement.
          355  +**
          356  +**     DETACH pDbname
          357  +*/
          358  +void sqlite3Detach(Parse *pParse, Expr *pDbname){
          359  +  codeAttach(pParse, SQLITE_DETACH, "sqlite_detach", 1, pDbname, 0, 0, pDbname);
          360  +}
          361  +
          362  +/*
          363  +** Called by the parser to compile an ATTACH statement.
          364  +**
          365  +**     ATTACH p AS pDbname KEY pKey
          366  +*/
          367  +void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
          368  +  codeAttach(pParse, SQLITE_ATTACH, "sqlite_attach", 3, p, p, pDbname, pKey);
          369  +}
          370  +#endif /* SQLITE_OMIT_ATTACH */
          371  +
          372  +/*
          373  +** Register the functions sqlite_attach and sqlite_detach.
          374  +*/
          375  +void sqlite3AttachFunctions(sqlite3 *db){
          376  +#ifndef SQLITE_OMIT_ATTACH
          377  +  static const int enc = SQLITE_UTF8;
          378  +  sqlite3CreateFunc(db, "sqlite_attach", 3, enc, 0, attachFunc, 0, 0);
          379  +  sqlite3CreateFunc(db, "sqlite_detach", 1, enc, 0, detachFunc, 0, 0);
          380  +#endif
          381  +}
          382  +
          383  +/*
          384  +** Initialize a DbFixer structure.  This routine must be called prior
          385  +** to passing the structure to one of the sqliteFixAAAA() routines below.
          386  +**
          387  +** The return value indicates whether or not fixation is required.  TRUE
          388  +** means we do need to fix the database references, FALSE means we do not.
          389  +*/
          390  +int sqlite3FixInit(
          391  +  DbFixer *pFix,      /* The fixer to be initialized */
          392  +  Parse *pParse,      /* Error messages will be written here */
          393  +  int iDb,            /* This is the database that must be used */
          394  +  const char *zType,  /* "view", "trigger", or "index" */
          395  +  const Token *pName  /* Name of the view, trigger, or index */
          396  +){
          397  +  sqlite3 *db;
          398  +
          399  +  if( iDb<0 || iDb==1 ) return 0;
          400  +  db = pParse->db;
          401  +  assert( db->nDb>iDb );
          402  +  pFix->pParse = pParse;
          403  +  pFix->zDb = db->aDb[iDb].zName;
          404  +  pFix->zType = zType;
          405  +  pFix->pName = pName;
          406  +  return 1;
          407  +}
          408  +
          409  +/*
          410  +** The following set of routines walk through the parse tree and assign
          411  +** a specific database to all table references where the database name
          412  +** was left unspecified in the original SQL statement.  The pFix structure
          413  +** must have been initialized by a prior call to sqlite3FixInit().
          414  +**
          415  +** These routines are used to make sure that an index, trigger, or
          416  +** view in one database does not refer to objects in a different database.
          417  +** (Exception: indices, triggers, and views in the TEMP database are
          418  +** allowed to refer to anything.)  If a reference is explicitly made
          419  +** to an object in a different database, an error message is added to
          420  +** pParse->zErrMsg and these routines return non-zero.  If everything
          421  +** checks out, these routines return 0.
          422  +*/
          423  +int sqlite3FixSrcList(
          424  +  DbFixer *pFix,       /* Context of the fixation */
          425  +  SrcList *pList       /* The Source list to check and modify */
          426  +){
          427  +  int i;
          428  +  const char *zDb;
          429  +  struct SrcList_item *pItem;
          430  +
          431  +  if( pList==0 ) return 0;
          432  +  zDb = pFix->zDb;
          433  +  for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
          434  +    if( pItem->zDatabase==0 ){
          435  +      pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb);
          436  +    }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){
          437  +      sqlite3ErrorMsg(pFix->pParse,
          438  +         "%s %T cannot reference objects in database %s",
          439  +         pFix->zType, pFix->pName, pItem->zDatabase);
          440  +      return 1;
          441  +    }
          442  +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
          443  +    if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
          444  +    if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
          445  +#endif
          446  +  }
          447  +  return 0;
          448  +}
          449  +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
          450  +int sqlite3FixSelect(
          451  +  DbFixer *pFix,       /* Context of the fixation */
          452  +  Select *pSelect      /* The SELECT statement to be fixed to one database */
          453  +){
          454  +  while( pSelect ){
          455  +    if( sqlite3FixExprList(pFix, pSelect->pEList) ){
          456  +      return 1;
          457  +    }
          458  +    if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
          459  +      return 1;
          460  +    }
          461  +    if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
          462  +      return 1;
          463  +    }
          464  +    if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
          465  +      return 1;
          466  +    }
          467  +    pSelect = pSelect->pPrior;
          468  +  }
          469  +  return 0;
          470  +}
          471  +int sqlite3FixExpr(
          472  +  DbFixer *pFix,     /* Context of the fixation */
          473  +  Expr *pExpr        /* The expression to be fixed to one database */
          474  +){
          475  +  while( pExpr ){
          476  +    if( sqlite3FixSelect(pFix, pExpr->pSelect) ){
          477  +      return 1;
          478  +    }
          479  +    if( sqlite3FixExprList(pFix, pExpr->pList) ){
          480  +      return 1;
          481  +    }
          482  +    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
          483  +      return 1;
          484  +    }
          485  +    pExpr = pExpr->pLeft;
          486  +  }
          487  +  return 0;
          488  +}
          489  +int sqlite3FixExprList(
          490  +  DbFixer *pFix,     /* Context of the fixation */
          491  +  ExprList *pList    /* The expression to be fixed to one database */
          492  +){
          493  +  int i;
          494  +  struct ExprList_item *pItem;
          495  +  if( pList==0 ) return 0;
          496  +  for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
          497  +    if( sqlite3FixExpr(pFix, pItem->pExpr) ){
          498  +      return 1;
          499  +    }
          500  +  }
          501  +  return 0;
          502  +}
          503  +#endif
          504  +
          505  +#ifndef SQLITE_OMIT_TRIGGER
          506  +int sqlite3FixTriggerStep(
          507  +  DbFixer *pFix,     /* Context of the fixation */
          508  +  TriggerStep *pStep /* The trigger step be fixed to one database */
          509  +){
          510  +  while( pStep ){
          511  +    if( sqlite3FixSelect(pFix, pStep->pSelect) ){
          512  +      return 1;
          513  +    }
          514  +    if( sqlite3FixExpr(pFix, pStep->pWhere) ){
          515  +      return 1;
          516  +    }
          517  +    if( sqlite3FixExprList(pFix, pStep->pExprList) ){
          518  +      return 1;
          519  +    }
          520  +    pStep = pStep->pNext;
          521  +  }
          522  +  return 0;
          523  +}
          524  +#endif

Added SQLite.Interop/splitsource/auth.c.

            1  +/*
            2  +** 2003 January 11
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This file contains code used to implement the sqlite3_set_authorizer()
           13  +** API.  This facility is an optional feature of the library.  Embedded
           14  +** systems that do not need this facility may omit it by recompiling
           15  +** the library with -DSQLITE_OMIT_AUTHORIZATION=1
           16  +**
           17  +** $Id: auth.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           18  +*/
           19  +#include "sqliteInt.h"
           20  +
           21  +/*
           22  +** All of the code in this file may be omitted by defining a single
           23  +** macro.
           24  +*/
           25  +#ifndef SQLITE_OMIT_AUTHORIZATION
           26  +
           27  +/*
           28  +** Set or clear the access authorization function.
           29  +**
           30  +** The access authorization function is be called during the compilation
           31  +** phase to verify that the user has read and/or write access permission on
           32  +** various fields of the database.  The first argument to the auth function
           33  +** is a copy of the 3rd argument to this routine.  The second argument
           34  +** to the auth function is one of these constants:
           35  +**
           36  +**       SQLITE_CREATE_INDEX
           37  +**       SQLITE_CREATE_TABLE
           38  +**       SQLITE_CREATE_TEMP_INDEX
           39  +**       SQLITE_CREATE_TEMP_TABLE
           40  +**       SQLITE_CREATE_TEMP_TRIGGER
           41  +**       SQLITE_CREATE_TEMP_VIEW
           42  +**       SQLITE_CREATE_TRIGGER
           43  +**       SQLITE_CREATE_VIEW
           44  +**       SQLITE_DELETE
           45  +**       SQLITE_DROP_INDEX
           46  +**       SQLITE_DROP_TABLE
           47  +**       SQLITE_DROP_TEMP_INDEX
           48  +**       SQLITE_DROP_TEMP_TABLE
           49  +**       SQLITE_DROP_TEMP_TRIGGER
           50  +**       SQLITE_DROP_TEMP_VIEW
           51  +**       SQLITE_DROP_TRIGGER
           52  +**       SQLITE_DROP_VIEW
           53  +**       SQLITE_INSERT
           54  +**       SQLITE_PRAGMA
           55  +**       SQLITE_READ
           56  +**       SQLITE_SELECT
           57  +**       SQLITE_TRANSACTION
           58  +**       SQLITE_UPDATE
           59  +**
           60  +** The third and fourth arguments to the auth function are the name of
           61  +** the table and the column that are being accessed.  The auth function
           62  +** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
           63  +** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
           64  +** means that the SQL statement will never-run - the sqlite3_exec() call
           65  +** will return with an error.  SQLITE_IGNORE means that the SQL statement
           66  +** should run but attempts to read the specified column will return NULL
           67  +** and attempts to write the column will be ignored.
           68  +**
           69  +** Setting the auth function to NULL disables this hook.  The default
           70  +** setting of the auth function is NULL.
           71  +*/
           72  +int sqlite3_set_authorizer(
           73  +  sqlite3 *db,
           74  +  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
           75  +  void *pArg
           76  +){
           77  +  sqlite3_mutex_enter(db->mutex);
           78  +  db->xAuth = xAuth;
           79  +  db->pAuthArg = pArg;
           80  +  sqlite3ExpirePreparedStatements(db);
           81  +  sqlite3_mutex_leave(db->mutex);
           82  +  return SQLITE_OK;
           83  +}
           84  +
           85  +/*
           86  +** Write an error message into pParse->zErrMsg that explains that the
           87  +** user-supplied authorization function returned an illegal value.
           88  +*/
           89  +static void sqliteAuthBadReturnCode(Parse *pParse, int rc){
           90  +  sqlite3ErrorMsg(pParse, "illegal return value (%d) from the "
           91  +    "authorization function - should be SQLITE_OK, SQLITE_IGNORE, "
           92  +    "or SQLITE_DENY", rc);
           93  +  pParse->rc = SQLITE_ERROR;
           94  +}
           95  +
           96  +/*
           97  +** The pExpr should be a TK_COLUMN expression.  The table referred to
           98  +** is in pTabList or else it is the NEW or OLD table of a trigger.  
           99  +** Check to see if it is OK to read this particular column.
          100  +**
          101  +** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN 
          102  +** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
          103  +** then generate an error.
          104  +*/
          105  +void sqlite3AuthRead(
          106  +  Parse *pParse,        /* The parser context */
          107  +  Expr *pExpr,          /* The expression to check authorization on */
          108  +  Schema *pSchema,      /* The schema of the expression */
          109  +  SrcList *pTabList     /* All table that pExpr might refer to */
          110  +){
          111  +  sqlite3 *db = pParse->db;
          112  +  int rc;
          113  +  Table *pTab = 0;      /* The table being read */
          114  +  const char *zCol;     /* Name of the column of the table */
          115  +  int iSrc;             /* Index in pTabList->a[] of table being read */
          116  +  const char *zDBase;   /* Name of database being accessed */
          117  +  TriggerStack *pStack; /* The stack of current triggers */
          118  +  int iDb;              /* The index of the database the expression refers to */
          119  +
          120  +  if( db->xAuth==0 ) return;
          121  +  if( pExpr->op!=TK_COLUMN ) return;
          122  +  iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
          123  +  if( iDb<0 ){
          124  +    /* An attempt to read a column out of a subquery or other
          125  +    ** temporary table. */
          126  +    return;
          127  +  }
          128  +  for(iSrc=0; pTabList && iSrc<pTabList->nSrc; iSrc++){
          129  +    if( pExpr->iTable==pTabList->a[iSrc].iCursor ) break;
          130  +  }
          131  +  if( iSrc>=0 && pTabList && iSrc<pTabList->nSrc ){
          132  +    pTab = pTabList->a[iSrc].pTab;
          133  +  }else if( (pStack = pParse->trigStack)!=0 ){
          134  +    /* This must be an attempt to read the NEW or OLD pseudo-tables
          135  +    ** of a trigger.
          136  +    */
          137  +    assert( pExpr->iTable==pStack->newIdx || pExpr->iTable==pStack->oldIdx );
          138  +    pTab = pStack->pTab;
          139  +  }
          140  +  if( pTab==0 ) return;
          141  +  if( pExpr->iColumn>=0 ){
          142  +    assert( pExpr->iColumn<pTab->nCol );
          143  +    zCol = pTab->aCol[pExpr->iColumn].zName;
          144  +  }else if( pTab->iPKey>=0 ){
          145  +    assert( pTab->iPKey<pTab->nCol );
          146  +    zCol = pTab->aCol[pTab->iPKey].zName;
          147  +  }else{
          148  +    zCol = "ROWID";
          149  +  }
          150  +  assert( iDb>=0 && iDb<db->nDb );
          151  +  zDBase = db->aDb[iDb].zName;
          152  +  rc = db->xAuth(db->pAuthArg, SQLITE_READ, pTab->zName, zCol, zDBase, 
          153  +                 pParse->zAuthContext);
          154  +  if( rc==SQLITE_IGNORE ){
          155  +    pExpr->op = TK_NULL;
          156  +  }else if( rc==SQLITE_DENY ){
          157  +    if( db->nDb>2 || iDb!=0 ){
          158  +      sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited", 
          159  +         zDBase, pTab->zName, zCol);
          160  +    }else{
          161  +      sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited",pTab->zName,zCol);
          162  +    }
          163  +    pParse->rc = SQLITE_AUTH;
          164  +  }else if( rc!=SQLITE_OK ){
          165  +    sqliteAuthBadReturnCode(pParse, rc);
          166  +  }
          167  +}
          168  +
          169  +/*
          170  +** Do an authorization check using the code and arguments given.  Return
          171  +** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
          172  +** is returned, then the error count and error message in pParse are
          173  +** modified appropriately.
          174  +*/
          175  +int sqlite3AuthCheck(
          176  +  Parse *pParse,
          177  +  int code,
          178  +  const char *zArg1,
          179  +  const char *zArg2,
          180  +  const char *zArg3
          181  +){
          182  +  sqlite3 *db = pParse->db;
          183  +  int rc;
          184  +
          185  +  /* Don't do any authorization checks if the database is initialising
          186  +  ** or if the parser is being invoked from within sqlite3_declare_vtab.
          187  +  */
          188  +  if( db->init.busy || IN_DECLARE_VTAB ){
          189  +    return SQLITE_OK;
          190  +  }
          191  +
          192  +  if( db->xAuth==0 ){
          193  +    return SQLITE_OK;
          194  +  }
          195  +  rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
          196  +  if( rc==SQLITE_DENY ){
          197  +    sqlite3ErrorMsg(pParse, "not authorized");
          198  +    pParse->rc = SQLITE_AUTH;
          199  +  }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
          200  +    rc = SQLITE_DENY;
          201  +    sqliteAuthBadReturnCode(pParse, rc);
          202  +  }
          203  +  return rc;
          204  +}
          205  +
          206  +/*
          207  +** Push an authorization context.  After this routine is called, the
          208  +** zArg3 argument to authorization callbacks will be zContext until
          209  +** popped.  Or if pParse==0, this routine is a no-op.
          210  +*/
          211  +void sqlite3AuthContextPush(
          212  +  Parse *pParse,
          213  +  AuthContext *pContext, 
          214  +  const char *zContext
          215  +){
          216  +  pContext->pParse = pParse;
          217  +  if( pParse ){
          218  +    pContext->zAuthContext = pParse->zAuthContext;
          219  +    pParse->zAuthContext = zContext;
          220  +  }
          221  +}
          222  +
          223  +/*
          224  +** Pop an authorization context that was previously pushed
          225  +** by sqlite3AuthContextPush
          226  +*/
          227  +void sqlite3AuthContextPop(AuthContext *pContext){
          228  +  if( pContext->pParse ){
          229  +    pContext->pParse->zAuthContext = pContext->zAuthContext;
          230  +    pContext->pParse = 0;
          231  +  }
          232  +}
          233  +
          234  +#endif /* SQLITE_OMIT_AUTHORIZATION */

Added SQLite.Interop/splitsource/bitvec.c.

            1  +/*
            2  +** 2008 February 16
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This file implements an object that represents a fixed-length
           13  +** bitmap.  Bits are numbered starting with 1.
           14  +**
           15  +** A bitmap is used to record what pages a database file have been
           16  +** journalled during a transaction.  Usually only a few pages are
           17  +** journalled.  So the bitmap is usually sparse and has low cardinality.
           18  +** But sometimes (for example when during a DROP of a large table) most
           19  +** or all of the pages get journalled.  In those cases, the bitmap becomes
           20  +** dense.  The algorithm needs to handle both cases well.
           21  +**
           22  +** The size of the bitmap is fixed when the object is created.
           23  +**
           24  +** All bits are clear when the bitmap is created.  Individual bits
           25  +** may be set or cleared one at a time.
           26  +**
           27  +** Test operations are about 100 times more common that set operations.
           28  +** Clear operations are exceedingly rare.  There are usually between
           29  +** 5 and 500 set operations per Bitvec object, though the number of sets can
           30  +** sometimes grow into tens of thousands or larger.  The size of the
           31  +** Bitvec object is the number of pages in the database file at the
           32  +** start of a transaction, and is thus usually less than a few thousand,
           33  +** but can be as large as 2 billion for a really big database.
           34  +**
           35  +** @(#) $Id: bitvec.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           36  +*/
           37  +#include "sqliteInt.h"
           38  +
           39  +#define BITVEC_SZ        512
           40  +/* Round the union size down to the nearest pointer boundary, since that's how 
           41  +** it will be aligned within the Bitvec struct. */
           42  +#define BITVEC_USIZE     (((BITVEC_SZ-12)/sizeof(Bitvec*))*sizeof(Bitvec*))
           43  +#define BITVEC_NCHAR     BITVEC_USIZE
           44  +#define BITVEC_NBIT      (BITVEC_NCHAR*8)
           45  +#define BITVEC_NINT      (BITVEC_USIZE/4)
           46  +#define BITVEC_MXHASH    (BITVEC_NINT/2)
           47  +#define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
           48  +
           49  +#define BITVEC_HASH(X)   (((X)*37)%BITVEC_NINT)
           50  +
           51  +/*
           52  +** A bitmap is an instance of the following structure.
           53  +**
           54  +** This bitmap records the existance of zero or more bits
           55  +** with values between 1 and iSize, inclusive.
           56  +**
           57  +** There are three possible representations of the bitmap.
           58  +** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
           59  +** bitmap.  The least significant bit is bit 1.
           60  +**
           61  +** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
           62  +** a hash table that will hold up to BITVEC_MXHASH distinct values.
           63  +**
           64  +** Otherwise, the value i is redirected into one of BITVEC_NPTR
           65  +** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
           66  +** handles up to iDivisor separate values of i.  apSub[0] holds
           67  +** values between 1 and iDivisor.  apSub[1] holds values between
           68  +** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
           69  +** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
           70  +** to hold deal with values between 1 and iDivisor.
           71  +*/
           72  +struct Bitvec {
           73  +  u32 iSize;      /* Maximum bit index */
           74  +  u32 nSet;       /* Number of bits that are set */
           75  +  u32 iDivisor;   /* Number of bits handled by each apSub[] entry */
           76  +  union {
           77  +    u8 aBitmap[BITVEC_NCHAR];    /* Bitmap representation */
           78  +    u32 aHash[BITVEC_NINT];      /* Hash table representation */
           79  +    Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
           80  +  } u;
           81  +};
           82  +
           83  +/*
           84  +** Create a new bitmap object able to handle bits between 0 and iSize,
           85  +** inclusive.  Return a pointer to the new object.  Return NULL if 
           86  +** malloc fails.
           87  +*/
           88  +Bitvec *sqlite3BitvecCreate(u32 iSize){
           89  +  Bitvec *p;
           90  +  assert( sizeof(*p)==BITVEC_SZ );
           91  +  p = sqlite3MallocZero( sizeof(*p) );
           92  +  if( p ){
           93  +    p->iSize = iSize;
           94  +  }
           95  +  return p;
           96  +}
           97  +
           98  +/*
           99  +** Check to see if the i-th bit is set.  Return true or false.
          100  +** If p is NULL (if the bitmap has not been created) or if
          101  +** i is out of range, then return false.
          102  +*/
          103  +int sqlite3BitvecTest(Bitvec *p, u32 i){
          104  +  if( p==0 ) return 0;
          105  +  if( i>p->iSize || i==0 ) return 0;
          106  +  if( p->iSize<=BITVEC_NBIT ){
          107  +    i--;
          108  +    return (p->u.aBitmap[i/8] & (1<<(i&7)))!=0;
          109  +  }
          110  +  if( p->iDivisor>0 ){
          111  +    u32 bin = (i-1)/p->iDivisor;
          112  +    i = (i-1)%p->iDivisor + 1;
          113  +    return sqlite3BitvecTest(p->u.apSub[bin], i);
          114  +  }else{
          115  +    u32 h = BITVEC_HASH(i);
          116  +    while( p->u.aHash[h] ){
          117  +      if( p->u.aHash[h]==i ) return 1;
          118  +      h++;
          119  +      if( h>=BITVEC_NINT ) h = 0;
          120  +    }
          121  +    return 0;
          122  +  }
          123  +}
          124  +
          125  +/*
          126  +** Set the i-th bit.  Return 0 on success and an error code if
          127  +** anything goes wrong.
          128  +*/
          129  +int sqlite3BitvecSet(Bitvec *p, u32 i){
          130  +  u32 h;
          131  +  assert( p!=0 );
          132  +  assert( i>0 );
          133  +  assert( i<=p->iSize );
          134  +  if( p->iSize<=BITVEC_NBIT ){
          135  +    i--;
          136  +    p->u.aBitmap[i/8] |= 1 << (i&7);
          137  +    return SQLITE_OK;
          138  +  }
          139  +  if( p->iDivisor ){
          140  +    u32 bin = (i-1)/p->iDivisor;
          141  +    i = (i-1)%p->iDivisor + 1;
          142  +    if( p->u.apSub[bin]==0 ){
          143  +      sqlite3BeginBenignMalloc();
          144  +      p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
          145  +      sqlite3EndBenignMalloc();
          146  +      if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
          147  +    }
          148  +    return sqlite3BitvecSet(p->u.apSub[bin], i);
          149  +  }
          150  +  h = BITVEC_HASH(i);
          151  +  while( p->u.aHash[h] ){
          152  +    if( p->u.aHash[h]==i ) return SQLITE_OK;
          153  +    h++;
          154  +    if( h==BITVEC_NINT ) h = 0;
          155  +  }
          156  +  p->nSet++;
          157  +  if( p->nSet>=BITVEC_MXHASH ){
          158  +    int j, rc;
          159  +    u32 aiValues[BITVEC_NINT];
          160  +    memcpy(aiValues, p->u.aHash, sizeof(aiValues));
          161  +    memset(p->u.apSub, 0, sizeof(p->u.apSub[0])*BITVEC_NPTR);
          162  +    p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
          163  +    rc = sqlite3BitvecSet(p, i);
          164  +    for(j=0; j<BITVEC_NINT; j++){
          165  +      if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
          166  +    }
          167  +    return rc;
          168  +  }
          169  +  p->u.aHash[h] = i;
          170  +  return SQLITE_OK;
          171  +}
          172  +
          173  +/*
          174  +** Clear the i-th bit.  Return 0 on success and an error code if
          175  +** anything goes wrong.
          176  +*/
          177  +void sqlite3BitvecClear(Bitvec *p, u32 i){
          178  +  assert( p!=0 );
          179  +  assert( i>0 );
          180  +  if( p->iSize<=BITVEC_NBIT ){
          181  +    i--;
          182  +    p->u.aBitmap[i/8] &= ~(1 << (i&7));
          183  +  }else if( p->iDivisor ){
          184  +    u32 bin = (i-1)/p->iDivisor;
          185  +    i = (i-1)%p->iDivisor + 1;
          186  +    if( p->u.apSub[bin] ){
          187  +      sqlite3BitvecClear(p->u.apSub[bin], i);
          188  +    }
          189  +  }else{
          190  +    int j;
          191  +    u32 aiValues[BITVEC_NINT];
          192  +    memcpy(aiValues, p->u.aHash, sizeof(aiValues));
          193  +    memset(p->u.aHash, 0, sizeof(p->u.aHash[0])*BITVEC_NINT);
          194  +    p->nSet = 0;
          195  +    for(j=0; j<BITVEC_NINT; j++){
          196  +      if( aiValues[j] && aiValues[j]!=i ){
          197  +        sqlite3BitvecSet(p, aiValues[j]);
          198  +      }
          199  +    }
          200  +  }
          201  +}
          202  +
          203  +/*
          204  +** Destroy a bitmap object.  Reclaim all memory used.
          205  +*/
          206  +void sqlite3BitvecDestroy(Bitvec *p){
          207  +  if( p==0 ) return;
          208  +  if( p->iDivisor ){
          209  +    int i;
          210  +    for(i=0; i<BITVEC_NPTR; i++){
          211  +      sqlite3BitvecDestroy(p->u.apSub[i]);
          212  +    }
          213  +  }
          214  +  sqlite3_free(p);
          215  +}
          216  +
          217  +#ifndef SQLITE_OMIT_BUILTIN_TEST
          218  +/*
          219  +** Let V[] be an array of unsigned characters sufficient to hold
          220  +** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
          221  +** Then the following macros can be used to set, clear, or test
          222  +** individual bits within V.
          223  +*/
          224  +#define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
          225  +#define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
          226  +#define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
          227  +
          228  +/*
          229  +** This routine runs an extensive test of the Bitvec code.
          230  +**
          231  +** The input is an array of integers that acts as a program
          232  +** to test the Bitvec.  The integers are opcodes followed
          233  +** by 0, 1, or 3 operands, depending on the opcode.  Another
          234  +** opcode follows immediately after the last operand.
          235  +**
          236  +** There are 6 opcodes numbered from 0 through 5.  0 is the
          237  +** "halt" opcode and causes the test to end.
          238  +**
          239  +**    0          Halt and return the number of errors
          240  +**    1 N S X    Set N bits beginning with S and incrementing by X
          241  +**    2 N S X    Clear N bits beginning with S and incrementing by X
          242  +**    3 N        Set N randomly chosen bits
          243  +**    4 N        Clear N randomly chosen bits
          244  +**    5 N S X    Set N bits from S increment X in array only, not in bitvec
          245  +**
          246  +** The opcodes 1 through 4 perform set and clear operations are performed
          247  +** on both a Bitvec object and on a linear array of bits obtained from malloc.
          248  +** Opcode 5 works on the linear array only, not on the Bitvec.
          249  +** Opcode 5 is used to deliberately induce a fault in order to
          250  +** confirm that error detection works.
          251  +**
          252  +** At the conclusion of the test the linear array is compared
          253  +** against the Bitvec object.  If there are any differences,
          254  +** an error is returned.  If they are the same, zero is returned.
          255  +**
          256  +** If a memory allocation error occurs, return -1.
          257  +*/
          258  +int sqlite3BitvecBuiltinTest(int sz, int *aOp){
          259  +  Bitvec *pBitvec = 0;
          260  +  unsigned char *pV = 0;
          261  +  int rc = -1;
          262  +  int i, nx, pc, op;
          263  +
          264  +  /* Allocate the Bitvec to be tested and a linear array of
          265  +  ** bits to act as the reference */
          266  +  pBitvec = sqlite3BitvecCreate( sz );
          267  +  pV = sqlite3_malloc( (sz+7)/8 + 1 );
          268  +  if( pBitvec==0 || pV==0 ) goto bitvec_end;
          269  +  memset(pV, 0, (sz+7)/8 + 1);
          270  +
          271  +  /* Run the program */
          272  +  pc = 0;
          273  +  while( (op = aOp[pc])!=0 ){
          274  +    switch( op ){
          275  +      case 1:
          276  +      case 2:
          277  +      case 5: {
          278  +        nx = 4;
          279  +        i = aOp[pc+2] - 1;
          280  +        aOp[pc+2] += aOp[pc+3];
          281  +        break;
          282  +      }
          283  +      case 3:
          284  +      case 4: 
          285  +      default: {
          286  +        nx = 2;
          287  +        sqlite3_randomness(sizeof(i), &i);
          288  +        break;
          289  +      }
          290  +    }
          291  +    if( (--aOp[pc+1]) > 0 ) nx = 0;
          292  +    pc += nx;
          293  +    i = (i & 0x7fffffff)%sz;
          294  +    if( (op & 1)!=0 ){
          295  +      SETBIT(pV, (i+1));
          296  +      if( op!=5 ){
          297  +        if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
          298  +      }
          299  +    }else{
          300  +      CLEARBIT(pV, (i+1));
          301  +      sqlite3BitvecClear(pBitvec, i+1);
          302  +    }
          303  +  }
          304  +
          305  +  /* Test to make sure the linear array exactly matches the
          306  +  ** Bitvec object.  Start with the assumption that they do
          307  +  ** match (rc==0).  Change rc to non-zero if a discrepancy
          308  +  ** is found.
          309  +  */
          310  +  rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
          311  +          + sqlite3BitvecTest(pBitvec, 0);
          312  +  for(i=1; i<=sz; i++){
          313  +    if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
          314  +      rc = i;
          315  +      break;
          316  +    }
          317  +  }
          318  +
          319  +  /* Free allocated structure */
          320  +bitvec_end:
          321  +  sqlite3_free(pV);
          322  +  sqlite3BitvecDestroy(pBitvec);
          323  +  return rc;
          324  +}
          325  +#endif /* SQLITE_OMIT_BUILTIN_TEST */

Added SQLite.Interop/splitsource/btmutex.c.

            1  +/*
            2  +** 2007 August 27
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** $Id: btmutex.c,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           14  +**
           15  +** This file contains code used to implement mutexes on Btree objects.
           16  +** This code really belongs in btree.c.  But btree.c is getting too
           17  +** big and we want to break it down some.  This packaged seemed like
           18  +** a good breakout.
           19  +*/
           20  +#include "btreeInt.h"
           21  +#if SQLITE_THREADSAFE && !defined(SQLITE_OMIT_SHARED_CACHE)
           22  +
           23  +
           24  +/*
           25  +** Enter a mutex on the given BTree object.
           26  +**
           27  +** If the object is not sharable, then no mutex is ever required
           28  +** and this routine is a no-op.  The underlying mutex is non-recursive.
           29  +** But we keep a reference count in Btree.wantToLock so the behavior
           30  +** of this interface is recursive.
           31  +**
           32  +** To avoid deadlocks, multiple Btrees are locked in the same order
           33  +** by all database connections.  The p->pNext is a list of other
           34  +** Btrees belonging to the same database connection as the p Btree
           35  +** which need to be locked after p.  If we cannot get a lock on
           36  +** p, then first unlock all of the others on p->pNext, then wait
           37  +** for the lock to become available on p, then relock all of the
           38  +** subsequent Btrees that desire a lock.
           39  +*/
           40  +void sqlite3BtreeEnter(Btree *p){
           41  +  Btree *pLater;
           42  +
           43  +  /* Some basic sanity checking on the Btree.  The list of Btrees
           44  +  ** connected by pNext and pPrev should be in sorted order by
           45  +  ** Btree.pBt value. All elements of the list should belong to
           46  +  ** the same connection. Only shared Btrees are on the list. */
           47  +  assert( p->pNext==0 || p->pNext->pBt>p->pBt );
           48  +  assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
           49  +  assert( p->pNext==0 || p->pNext->db==p->db );
           50  +  assert( p->pPrev==0 || p->pPrev->db==p->db );
           51  +  assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
           52  +
           53  +  /* Check for locking consistency */
           54  +  assert( !p->locked || p->wantToLock>0 );
           55  +  assert( p->sharable || p->wantToLock==0 );
           56  +
           57  +  /* We should already hold a lock on the database connection */
           58  +  assert( sqlite3_mutex_held(p->db->mutex) );
           59  +
           60  +  if( !p->sharable ) return;
           61  +  p->wantToLock++;
           62  +  if( p->locked ) return;
           63  +
           64  +#ifndef SQLITE_MUTEX_NOOP
           65  +  /* In most cases, we should be able to acquire the lock we
           66  +  ** want without having to go throught the ascending lock
           67  +  ** procedure that follows.  Just be sure not to block.
           68  +  */
           69  +  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
           70  +    p->locked = 1;
           71  +    return;
           72  +  }
           73  +
           74  +  /* To avoid deadlock, first release all locks with a larger
           75  +  ** BtShared address.  Then acquire our lock.  Then reacquire
           76  +  ** the other BtShared locks that we used to hold in ascending
           77  +  ** order.
           78  +  */
           79  +  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
           80  +    assert( pLater->sharable );
           81  +    assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
           82  +    assert( !pLater->locked || pLater->wantToLock>0 );
           83  +    if( pLater->locked ){
           84  +      sqlite3_mutex_leave(pLater->pBt->mutex);
           85  +      pLater->locked = 0;
           86  +    }
           87  +  }
           88  +  sqlite3_mutex_enter(p->pBt->mutex);
           89  +  p->locked = 1;
           90  +  for(pLater=p->pNext; pLater; pLater=pLater->pNext){
           91  +    if( pLater->wantToLock ){
           92  +      sqlite3_mutex_enter(pLater->pBt->mutex);
           93  +      pLater->locked = 1;
           94  +    }
           95  +  }
           96  +#endif /* SQLITE_MUTEX_NOOP */
           97  +}
           98  +
           99  +/*
          100  +** Exit the recursive mutex on a Btree.
          101  +*/
          102  +void sqlite3BtreeLeave(Btree *p){
          103  +  if( p->sharable ){
          104  +    assert( p->wantToLock>0 );
          105  +    p->wantToLock--;
          106  +    if( p->wantToLock==0 ){
          107  +      assert( p->locked );
          108  +      sqlite3_mutex_leave(p->pBt->mutex);
          109  +      p->locked = 0;
          110  +    }
          111  +  }
          112  +}
          113  +
          114  +#ifndef NDEBUG
          115  +/*
          116  +** Return true if the BtShared mutex is held on the btree.  
          117  +**
          118  +** This routine makes no determination one why or another if the
          119  +** database connection mutex is held.
          120  +**
          121  +** This routine is used only from within assert() statements.
          122  +*/
          123  +int sqlite3BtreeHoldsMutex(Btree *p){
          124  +  return (p->sharable==0 ||
          125  +             (p->locked && p->wantToLock && sqlite3_mutex_held(p->pBt->mutex)));
          126  +}
          127  +#endif
          128  +
          129  +
          130  +#ifndef SQLITE_OMIT_INCRBLOB
          131  +/*
          132  +** Enter and leave a mutex on a Btree given a cursor owned by that
          133  +** Btree.  These entry points are used by incremental I/O and can be
          134  +** omitted if that module is not used.
          135  +*/
          136  +void sqlite3BtreeEnterCursor(BtCursor *pCur){
          137  +  sqlite3BtreeEnter(pCur->pBtree);
          138  +}
          139  +void sqlite3BtreeLeaveCursor(BtCursor *pCur){
          140  +  sqlite3BtreeLeave(pCur->pBtree);
          141  +}
          142  +#endif /* SQLITE_OMIT_INCRBLOB */
          143  +
          144  +
          145  +/*
          146  +** Enter the mutex on every Btree associated with a database
          147  +** connection.  This is needed (for example) prior to parsing
          148  +** a statement since we will be comparing table and column names
          149  +** against all schemas and we do not want those schemas being
          150  +** reset out from under us.
          151  +**
          152  +** There is a corresponding leave-all procedures.
          153  +**
          154  +** Enter the mutexes in accending order by BtShared pointer address
          155  +** to avoid the possibility of deadlock when two threads with
          156  +** two or more btrees in common both try to lock all their btrees
          157  +** at the same instant.
          158  +*/
          159  +void sqlite3BtreeEnterAll(sqlite3 *db){
          160  +  int i;
          161  +  Btree *p, *pLater;
          162  +  assert( sqlite3_mutex_held(db->mutex) );
          163  +  for(i=0; i<db->nDb; i++){
          164  +    p = db->aDb[i].pBt;
          165  +    if( p && p->sharable ){
          166  +      p->wantToLock++;
          167  +      if( !p->locked ){
          168  +        assert( p->wantToLock==1 );
          169  +        while( p->pPrev ) p = p->pPrev;
          170  +        while( p->locked && p->pNext ) p = p->pNext;
          171  +        for(pLater = p->pNext; pLater; pLater=pLater->pNext){
          172  +          if( pLater->locked ){
          173  +            sqlite3_mutex_leave(pLater->pBt->mutex);
          174  +            pLater->locked = 0;
          175  +          }
          176  +        }
          177  +        while( p ){
          178  +          sqlite3_mutex_enter(p->pBt->mutex);
          179  +          p->locked++;
          180  +          p = p->pNext;
          181  +        }
          182  +      }
          183  +    }
          184  +  }
          185  +}
          186  +void sqlite3BtreeLeaveAll(sqlite3 *db){
          187  +  int i;
          188  +  Btree *p;
          189  +  assert( sqlite3_mutex_held(db->mutex) );
          190  +  for(i=0; i<db->nDb; i++){
          191  +    p = db->aDb[i].pBt;
          192  +    if( p && p->sharable ){
          193  +      assert( p->wantToLock>0 );
          194  +      p->wantToLock--;
          195  +      if( p->wantToLock==0 ){
          196  +        assert( p->locked );
          197  +        sqlite3_mutex_leave(p->pBt->mutex);
          198  +        p->locked = 0;
          199  +      }
          200  +    }
          201  +  }
          202  +}
          203  +
          204  +#ifndef NDEBUG
          205  +/*
          206  +** Return true if the current thread holds the database connection
          207  +** mutex and all required BtShared mutexes.
          208  +**
          209  +** This routine is used inside assert() statements only.
          210  +*/
          211  +int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
          212  +  int i;
          213  +  if( !sqlite3_mutex_held(db->mutex) ){
          214  +    return 0;
          215  +  }
          216  +  for(i=0; i<db->nDb; i++){
          217  +    Btree *p;
          218  +    p = db->aDb[i].pBt;
          219  +    if( p && p->sharable &&
          220  +         (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
          221  +      return 0;
          222  +    }
          223  +  }
          224  +  return 1;
          225  +}
          226  +#endif /* NDEBUG */
          227  +
          228  +/*
          229  +** Add a new Btree pointer to a BtreeMutexArray. 
          230  +** if the pointer can possibly be shared with
          231  +** another database connection.
          232  +**
          233  +** The pointers are kept in sorted order by pBtree->pBt.  That
          234  +** way when we go to enter all the mutexes, we can enter them
          235  +** in order without every having to backup and retry and without
          236  +** worrying about deadlock.
          237  +**
          238  +** The number of shared btrees will always be small (usually 0 or 1)
          239  +** so an insertion sort is an adequate algorithm here.
          240  +*/
          241  +void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
          242  +  int i, j;
          243  +  BtShared *pBt;
          244  +  if( pBtree==0 || pBtree->sharable==0 ) return;
          245  +#ifndef NDEBUG
          246  +  {
          247  +    for(i=0; i<pArray->nMutex; i++){
          248  +      assert( pArray->aBtree[i]!=pBtree );
          249  +    }
          250  +  }
          251  +#endif
          252  +  assert( pArray->nMutex>=0 );
          253  +  assert( pArray->nMutex<sizeof(pArray->aBtree)/sizeof(pArray->aBtree[0])-1 );
          254  +  pBt = pBtree->pBt;
          255  +  for(i=0; i<pArray->nMutex; i++){
          256  +    assert( pArray->aBtree[i]!=pBtree );
          257  +    if( pArray->aBtree[i]->pBt>pBt ){
          258  +      for(j=pArray->nMutex; j>i; j--){
          259  +        pArray->aBtree[j] = pArray->aBtree[j-1];
          260  +      }
          261  +      pArray->aBtree[i] = pBtree;
          262  +      pArray->nMutex++;
          263  +      return;
          264  +    }
          265  +  }
          266  +  pArray->aBtree[pArray->nMutex++] = pBtree;
          267  +}
          268  +
          269  +/*
          270  +** Enter the mutex of every btree in the array.  This routine is
          271  +** called at the beginning of sqlite3VdbeExec().  The mutexes are
          272  +** exited at the end of the same function.
          273  +*/
          274  +void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
          275  +  int i;
          276  +  for(i=0; i<pArray->nMutex; i++){
          277  +    Btree *p = pArray->aBtree[i];
          278  +    /* Some basic sanity checking */
          279  +    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
          280  +    assert( !p->locked || p->wantToLock>0 );
          281  +
          282  +    /* We should already hold a lock on the database connection */
          283  +    assert( sqlite3_mutex_held(p->db->mutex) );
          284  +
          285  +    p->wantToLock++;
          286  +    if( !p->locked && p->sharable ){
          287  +      sqlite3_mutex_enter(p->pBt->mutex);
          288  +      p->locked = 1;
          289  +    }
          290  +  }
          291  +}
          292  +
          293  +/*
          294  +** Leave the mutex of every btree in the group.
          295  +*/
          296  +void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
          297  +  int i;
          298  +  for(i=0; i<pArray->nMutex; i++){
          299  +    Btree *p = pArray->aBtree[i];
          300  +    /* Some basic sanity checking */
          301  +    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
          302  +    assert( p->locked || !p->sharable );
          303  +    assert( p->wantToLock>0 );
          304  +
          305  +    /* We should already hold a lock on the database connection */
          306  +    assert( sqlite3_mutex_held(p->db->mutex) );
          307  +
          308  +    p->wantToLock--;
          309  +    if( p->wantToLock==0 && p->locked ){
          310  +      sqlite3_mutex_leave(p->pBt->mutex);
          311  +      p->locked = 0;
          312  +    }
          313  +  }
          314  +}
          315  +
          316  +
          317  +#endif  /* SQLITE_THREADSAFE && !SQLITE_OMIT_SHARED_CACHE */

Added SQLite.Interop/splitsource/btree.c.

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

Added SQLite.Interop/splitsource/btree.h.

            1  +/*
            2  +** 2001 September 15
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This header file defines the interface that the sqlite B-Tree file
           13  +** subsystem.  See comments in the source code for a detailed description
           14  +** of what each interface routine does.
           15  +**
           16  +** @(#) $Id: btree.h,v 1.1 2008/08/06 21:48:06 rmsimpson Exp $
           17  +*/
           18  +#ifndef _BTREE_H_
           19  +#define _BTREE_H_
           20  +
           21  +/* TODO: This definition is just included so other modules compile. It
           22  +** needs to be revisited.
           23  +*/
           24  +#define SQLITE_N_BTREE_META 10
           25  +
           26  +/*
           27  +** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
           28  +** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
           29  +*/
           30  +#ifndef SQLITE_DEFAULT_AUTOVACUUM
           31  +  #define SQLITE_DEFAULT_AUTOVACUUM 0
           32  +#endif
           33  +
           34  +#define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
           35  +#define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
           36  +#define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
           37  +
           38  +/*
           39  +** Forward declarations of structure
           40  +*/
           41  +typedef struct Btree Btree;
           42  +typedef struct BtCursor BtCursor;
           43  +typedef struct BtShared BtShared;
           44  +typedef struct BtreeMutexArray BtreeMutexArray;
           45  +
           46  +/*
           47  +** This structure records all of the Btrees that need to hold
           48  +** a mutex before we enter sqlite3VdbeExec().  The Btrees are
           49  +** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
           50  +** we can always lock and unlock them all quickly.
           51  +*/
           52  +struct BtreeMutexArray {
           53  +  int nMutex;
           54  +