extensions/sqlite/sqlite-source/btreeInt.h
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/extensions/sqlite/sqlite-source/btreeInt.h	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,648 +0,0 @@
     1.4 -/*
     1.5 -** 2004 April 6
     1.6 -**
     1.7 -** The author disclaims copyright to this source code.  In place of
     1.8 -** a legal notice, here is a blessing:
     1.9 -**
    1.10 -**    May you do good and not evil.
    1.11 -**    May you find forgiveness for yourself and forgive others.
    1.12 -**    May you share freely, never taking more than you give.
    1.13 -**
    1.14 -*************************************************************************
    1.15 -** $Id$
    1.16 -**
    1.17 -** This file implements a external (disk-based) database using BTrees.
    1.18 -** For a detailed discussion of BTrees, refer to
    1.19 -**
    1.20 -**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    1.21 -**     "Sorting And Searching", pages 473-480. Addison-Wesley
    1.22 -**     Publishing Company, Reading, Massachusetts.
    1.23 -**
    1.24 -** The basic idea is that each page of the file contains N database
    1.25 -** entries and N+1 pointers to subpages.
    1.26 -**
    1.27 -**   ----------------------------------------------------------------
    1.28 -**   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
    1.29 -**   ----------------------------------------------------------------
    1.30 -**
    1.31 -** All of the keys on the page that Ptr(0) points to have values less
    1.32 -** than Key(0).  All of the keys on page Ptr(1) and its subpages have
    1.33 -** values greater than Key(0) and less than Key(1).  All of the keys
    1.34 -** on Ptr(N) and its subpages have values greater than Key(N-1).  And
    1.35 -** so forth.
    1.36 -**
    1.37 -** Finding a particular key requires reading O(log(M)) pages from the 
    1.38 -** disk where M is the number of entries in the tree.
    1.39 -**
    1.40 -** In this implementation, a single file can hold one or more separate 
    1.41 -** BTrees.  Each BTree is identified by the index of its root page.  The
    1.42 -** key and data for any entry are combined to form the "payload".  A
    1.43 -** fixed amount of payload can be carried directly on the database
    1.44 -** page.  If the payload is larger than the preset amount then surplus
    1.45 -** bytes are stored on overflow pages.  The payload for an entry
    1.46 -** and the preceding pointer are combined to form a "Cell".  Each 
    1.47 -** page has a small header which contains the Ptr(N) pointer and other
    1.48 -** information such as the size of key and data.
    1.49 -**
    1.50 -** FORMAT DETAILS
    1.51 -**
    1.52 -** The file is divided into pages.  The first page is called page 1,
    1.53 -** the second is page 2, and so forth.  A page number of zero indicates
    1.54 -** "no such page".  The page size can be anything between 512 and 65536.
    1.55 -** Each page can be either a btree page, a freelist page or an overflow
    1.56 -** page.
    1.57 -**
    1.58 -** The first page is always a btree page.  The first 100 bytes of the first
    1.59 -** page contain a special header (the "file header") that describes the file.
    1.60 -** The format of the file header is as follows:
    1.61 -**
    1.62 -**   OFFSET   SIZE    DESCRIPTION
    1.63 -**      0      16     Header string: "SQLite format 3\000"
    1.64 -**     16       2     Page size in bytes.  
    1.65 -**     18       1     File format write version
    1.66 -**     19       1     File format read version
    1.67 -**     20       1     Bytes of unused space at the end of each page
    1.68 -**     21       1     Max embedded payload fraction
    1.69 -**     22       1     Min embedded payload fraction
    1.70 -**     23       1     Min leaf payload fraction
    1.71 -**     24       4     File change counter
    1.72 -**     28       4     Reserved for future use
    1.73 -**     32       4     First freelist page
    1.74 -**     36       4     Number of freelist pages in the file
    1.75 -**     40      60     15 4-byte meta values passed to higher layers
    1.76 -**
    1.77 -** All of the integer values are big-endian (most significant byte first).
    1.78 -**
    1.79 -** The file change counter is incremented when the database is changed
    1.80 -** This counter allows other processes to know when the file has changed
    1.81 -** and thus when they need to flush their cache.
    1.82 -**
    1.83 -** The max embedded payload fraction is the amount of the total usable
    1.84 -** space in a page that can be consumed by a single cell for standard
    1.85 -** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
    1.86 -** is to limit the maximum cell size so that at least 4 cells will fit
    1.87 -** on one page.  Thus the default max embedded payload fraction is 64.
    1.88 -**
    1.89 -** If the payload for a cell is larger than the max payload, then extra
    1.90 -** payload is spilled to overflow pages.  Once an overflow page is allocated,
    1.91 -** as many bytes as possible are moved into the overflow pages without letting
    1.92 -** the cell size drop below the min embedded payload fraction.
    1.93 -**
    1.94 -** The min leaf payload fraction is like the min embedded payload fraction
    1.95 -** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
    1.96 -** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
    1.97 -** not specified in the header.
    1.98 -**
    1.99 -** Each btree pages is divided into three sections:  The header, the
   1.100 -** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
   1.101 -** file header that occurs before the page header.
   1.102 -**
   1.103 -**      |----------------|
   1.104 -**      | file header    |   100 bytes.  Page 1 only.
   1.105 -**      |----------------|
   1.106 -**      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
   1.107 -**      |----------------|
   1.108 -**      | cell pointer   |   |  2 bytes per cell.  Sorted order.
   1.109 -**      | array          |   |  Grows downward
   1.110 -**      |                |   v
   1.111 -**      |----------------|
   1.112 -**      | unallocated    |
   1.113 -**      | space          |
   1.114 -**      |----------------|   ^  Grows upwards
   1.115 -**      | cell content   |   |  Arbitrary order interspersed with freeblocks.
   1.116 -**      | area           |   |  and free space fragments.
   1.117 -**      |----------------|
   1.118 -**
   1.119 -** The page headers looks like this:
   1.120 -**
   1.121 -**   OFFSET   SIZE     DESCRIPTION
   1.122 -**      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
   1.123 -**      1       2      byte offset to the first freeblock
   1.124 -**      3       2      number of cells on this page
   1.125 -**      5       2      first byte of the cell content area
   1.126 -**      7       1      number of fragmented free bytes
   1.127 -**      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
   1.128 -**
   1.129 -** The flags define the format of this btree page.  The leaf flag means that
   1.130 -** this page has no children.  The zerodata flag means that this page carries
   1.131 -** only keys and no data.  The intkey flag means that the key is a integer
   1.132 -** which is stored in the key size entry of the cell header rather than in
   1.133 -** the payload area.
   1.134 -**
   1.135 -** The cell pointer array begins on the first byte after the page header.
   1.136 -** The cell pointer array contains zero or more 2-byte numbers which are
   1.137 -** offsets from the beginning of the page to the cell content in the cell
   1.138 -** content area.  The cell pointers occur in sorted order.  The system strives
   1.139 -** to keep free space after the last cell pointer so that new cells can
   1.140 -** be easily added without having to defragment the page.
   1.141 -**
   1.142 -** Cell content is stored at the very end of the page and grows toward the
   1.143 -** beginning of the page.
   1.144 -**
   1.145 -** Unused space within the cell content area is collected into a linked list of
   1.146 -** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
   1.147 -** to the first freeblock is given in the header.  Freeblocks occur in
   1.148 -** increasing order.  Because a freeblock must be at least 4 bytes in size,
   1.149 -** any group of 3 or fewer unused bytes in the cell content area cannot
   1.150 -** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
   1.151 -** a fragment.  The total number of bytes in all fragments is recorded.
   1.152 -** in the page header at offset 7.
   1.153 -**
   1.154 -**    SIZE    DESCRIPTION
   1.155 -**      2     Byte offset of the next freeblock
   1.156 -**      2     Bytes in this freeblock
   1.157 -**
   1.158 -** Cells are of variable length.  Cells are stored in the cell content area at
   1.159 -** the end of the page.  Pointers to the cells are in the cell pointer array
   1.160 -** that immediately follows the page header.  Cells is not necessarily
   1.161 -** contiguous or in order, but cell pointers are contiguous and in order.
   1.162 -**
   1.163 -** Cell content makes use of variable length integers.  A variable
   1.164 -** length integer is 1 to 9 bytes where the lower 7 bits of each 
   1.165 -** byte are used.  The integer consists of all bytes that have bit 8 set and
   1.166 -** the first byte with bit 8 clear.  The most significant byte of the integer
   1.167 -** appears first.  A variable-length integer may not be more than 9 bytes long.
   1.168 -** As a special case, all 8 bytes of the 9th byte are used as data.  This
   1.169 -** allows a 64-bit integer to be encoded in 9 bytes.
   1.170 -**
   1.171 -**    0x00                      becomes  0x00000000
   1.172 -**    0x7f                      becomes  0x0000007f
   1.173 -**    0x81 0x00                 becomes  0x00000080
   1.174 -**    0x82 0x00                 becomes  0x00000100
   1.175 -**    0x80 0x7f                 becomes  0x0000007f
   1.176 -**    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
   1.177 -**    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
   1.178 -**
   1.179 -** Variable length integers are used for rowids and to hold the number of
   1.180 -** bytes of key and data in a btree cell.
   1.181 -**
   1.182 -** The content of a cell looks like this:
   1.183 -**
   1.184 -**    SIZE    DESCRIPTION
   1.185 -**      4     Page number of the left child. Omitted if leaf flag is set.
   1.186 -**     var    Number of bytes of data. Omitted if the zerodata flag is set.
   1.187 -**     var    Number of bytes of key. Or the key itself if intkey flag is set.
   1.188 -**      *     Payload
   1.189 -**      4     First page of the overflow chain.  Omitted if no overflow
   1.190 -**
   1.191 -** Overflow pages form a linked list.  Each page except the last is completely
   1.192 -** filled with data (pagesize - 4 bytes).  The last page can have as little
   1.193 -** as 1 byte of data.
   1.194 -**
   1.195 -**    SIZE    DESCRIPTION
   1.196 -**      4     Page number of next overflow page
   1.197 -**      *     Data
   1.198 -**
   1.199 -** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
   1.200 -** file header points to the first in a linked list of trunk page.  Each trunk
   1.201 -** page points to multiple leaf pages.  The content of a leaf page is
   1.202 -** unspecified.  A trunk page looks like this:
   1.203 -**
   1.204 -**    SIZE    DESCRIPTION
   1.205 -**      4     Page number of next trunk page
   1.206 -**      4     Number of leaf pointers on this page
   1.207 -**      *     zero or more pages numbers of leaves
   1.208 -*/
   1.209 -#include "sqliteInt.h"
   1.210 -#include "pager.h"
   1.211 -#include "btree.h"
   1.212 -#include "os.h"
   1.213 -#include <assert.h>
   1.214 -
   1.215 -/* Round up a number to the next larger multiple of 8.  This is used
   1.216 -** to force 8-byte alignment on 64-bit architectures.
   1.217 -*/
   1.218 -#define ROUND8(x)   ((x+7)&~7)
   1.219 -
   1.220 -
   1.221 -/* The following value is the maximum cell size assuming a maximum page
   1.222 -** size give above.
   1.223 -*/
   1.224 -#define MX_CELL_SIZE(pBt)  (pBt->pageSize-8)
   1.225 -
   1.226 -/* The maximum number of cells on a single page of the database.  This
   1.227 -** assumes a minimum cell size of 3 bytes.  Such small cells will be
   1.228 -** exceedingly rare, but they are possible.
   1.229 -*/
   1.230 -#define MX_CELL(pBt) ((pBt->pageSize-8)/3)
   1.231 -
   1.232 -/* Forward declarations */
   1.233 -typedef struct MemPage MemPage;
   1.234 -typedef struct BtLock BtLock;
   1.235 -
   1.236 -/*
   1.237 -** This is a magic string that appears at the beginning of every
   1.238 -** SQLite database in order to identify the file as a real database.
   1.239 -**
   1.240 -** You can change this value at compile-time by specifying a
   1.241 -** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
   1.242 -** header must be exactly 16 bytes including the zero-terminator so
   1.243 -** the string itself should be 15 characters long.  If you change
   1.244 -** the header, then your custom library will not be able to read 
   1.245 -** databases generated by the standard tools and the standard tools
   1.246 -** will not be able to read databases created by your custom library.
   1.247 -*/
   1.248 -#ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
   1.249 -#  define SQLITE_FILE_HEADER "SQLite format 3"
   1.250 -#endif
   1.251 -
   1.252 -/*
   1.253 -** Page type flags.  An ORed combination of these flags appear as the
   1.254 -** first byte of on-disk image of every BTree page.
   1.255 -*/
   1.256 -#define PTF_INTKEY    0x01
   1.257 -#define PTF_ZERODATA  0x02
   1.258 -#define PTF_LEAFDATA  0x04
   1.259 -#define PTF_LEAF      0x08
   1.260 -
   1.261 -/*
   1.262 -** As each page of the file is loaded into memory, an instance of the following
   1.263 -** structure is appended and initialized to zero.  This structure stores
   1.264 -** information about the page that is decoded from the raw file page.
   1.265 -**
   1.266 -** The pParent field points back to the parent page.  This allows us to
   1.267 -** walk up the BTree from any leaf to the root.  Care must be taken to
   1.268 -** unref() the parent page pointer when this page is no longer referenced.
   1.269 -** The pageDestructor() routine handles that chore.
   1.270 -**
   1.271 -** Access to all fields of this structure is controlled by the mutex
   1.272 -** stored in MemPage.pBt->mutex.
   1.273 -*/
   1.274 -struct MemPage {
   1.275 -  u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   1.276 -  u8 idxShift;         /* True if Cell indices have changed */
   1.277 -  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
   1.278 -  u8 intKey;           /* True if intkey flag is set */
   1.279 -  u8 leaf;             /* True if leaf flag is set */
   1.280 -  u8 zeroData;         /* True if table stores keys only */
   1.281 -  u8 leafData;         /* True if tables stores data on leaves only */
   1.282 -  u8 hasData;          /* True if this page stores data */
   1.283 -  u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   1.284 -  u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
   1.285 -  u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   1.286 -  u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   1.287 -  u16 cellOffset;      /* Index in aData of first cell pointer */
   1.288 -  u16 idxParent;       /* Index in parent of this node */
   1.289 -  u16 nFree;           /* Number of free bytes on the page */
   1.290 -  u16 nCell;           /* Number of cells on this page, local and ovfl */
   1.291 -  struct _OvflCell {   /* Cells that will not fit on aData[] */
   1.292 -    u8 *pCell;          /* Pointers to the body of the overflow cell */
   1.293 -    u16 idx;            /* Insert this cell before idx-th non-overflow cell */
   1.294 -  } aOvfl[5];
   1.295 -  BtShared *pBt;       /* Pointer to BtShared that this page is part of */
   1.296 -  u8 *aData;           /* Pointer to disk image of the page data */
   1.297 -  DbPage *pDbPage;     /* Pager page handle */
   1.298 -  Pgno pgno;           /* Page number for this page */
   1.299 -  MemPage *pParent;    /* The parent of this page.  NULL for root */
   1.300 -};
   1.301 -
   1.302 -/*
   1.303 -** The in-memory image of a disk page has the auxiliary information appended
   1.304 -** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
   1.305 -** that extra information.
   1.306 -*/
   1.307 -#define EXTRA_SIZE sizeof(MemPage)
   1.308 -
   1.309 -/* A Btree handle
   1.310 -**
   1.311 -** A database connection contains a pointer to an instance of
   1.312 -** this object for every database file that it has open.  This structure
   1.313 -** is opaque to the database connection.  The database connection cannot
   1.314 -** see the internals of this structure and only deals with pointers to
   1.315 -** this structure.
   1.316 -**
   1.317 -** For some database files, the same underlying database cache might be 
   1.318 -** shared between multiple connections.  In that case, each contection
   1.319 -** has it own pointer to this object.  But each instance of this object
   1.320 -** points to the same BtShared object.  The database cache and the
   1.321 -** schema associated with the database file are all contained within
   1.322 -** the BtShared object.
   1.323 -**
   1.324 -** All fields in this structure are accessed under sqlite3.mutex.
   1.325 -** The pBt pointer itself may not be changed while there exists cursors 
   1.326 -** in the referenced BtShared that point back to this Btree since those
   1.327 -** cursors have to do go through this Btree to find their BtShared and
   1.328 -** they often do so without holding sqlite3.mutex.
   1.329 -*/
   1.330 -struct Btree {
   1.331 -  sqlite3 *pSqlite;  /* The database connection holding this btree */
   1.332 -  BtShared *pBt;     /* Sharable content of this btree */
   1.333 -  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
   1.334 -  u8 sharable;       /* True if we can share pBt with other pSqlite */
   1.335 -  u8 locked;         /* True if pSqlite currently has pBt locked */
   1.336 -  int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
   1.337 -  Btree *pNext;      /* List of other sharable Btrees from the same pSqlite */
   1.338 -  Btree *pPrev;      /* Back pointer of the same list */
   1.339 -};
   1.340 -
   1.341 -/*
   1.342 -** Btree.inTrans may take one of the following values.
   1.343 -**
   1.344 -** If the shared-data extension is enabled, there may be multiple users
   1.345 -** of the Btree structure. At most one of these may open a write transaction,
   1.346 -** but any number may have active read transactions.
   1.347 -*/
   1.348 -#define TRANS_NONE  0
   1.349 -#define TRANS_READ  1
   1.350 -#define TRANS_WRITE 2
   1.351 -
   1.352 -/*
   1.353 -** An instance of this object represents a single database file.
   1.354 -** 
   1.355 -** A single database file can be in use as the same time by two
   1.356 -** or more database connections.  When two or more connections are
   1.357 -** sharing the same database file, each connection has it own
   1.358 -** private Btree object for the file and each of those Btrees points
   1.359 -** to this one BtShared object.  BtShared.nRef is the number of
   1.360 -** connections currently sharing this database file.
   1.361 -**
   1.362 -** Fields in this structure are accessed under the BtShared.mutex
   1.363 -** mutex, except for nRef and pNext which are accessed under the
   1.364 -** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
   1.365 -** may not be modified once it is initially set as long as nRef>0.
   1.366 -** The pSchema field may be set once under BtShared.mutex and
   1.367 -** thereafter is unchanged as long as nRef>0.
   1.368 -*/
   1.369 -struct BtShared {
   1.370 -  Pager *pPager;        /* The page cache */
   1.371 -  BtCursor *pCursor;    /* A list of all open cursors */
   1.372 -  MemPage *pPage1;      /* First page of the database */
   1.373 -  u8 inStmt;            /* True if we are in a statement subtransaction */
   1.374 -  u8 readOnly;          /* True if the underlying file is readonly */
   1.375 -  u8 maxEmbedFrac;      /* Maximum payload as % of total page size */
   1.376 -  u8 minEmbedFrac;      /* Minimum payload as % of total page size */
   1.377 -  u8 minLeafFrac;       /* Minimum leaf payload as % of total page size */
   1.378 -  u8 pageSizeFixed;     /* True if the page size can no longer be changed */
   1.379 -#ifndef SQLITE_OMIT_AUTOVACUUM
   1.380 -  u8 autoVacuum;        /* True if auto-vacuum is enabled */
   1.381 -  u8 incrVacuum;        /* True if incr-vacuum is enabled */
   1.382 -  Pgno nTrunc;          /* Non-zero if the db will be truncated (incr vacuum) */
   1.383 -#endif
   1.384 -  u16 pageSize;         /* Total number of bytes on a page */
   1.385 -  u16 usableSize;       /* Number of usable bytes on each page */
   1.386 -  int maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
   1.387 -  int minLocal;         /* Minimum local payload in non-LEAFDATA tables */
   1.388 -  int maxLeaf;          /* Maximum local payload in a LEAFDATA table */
   1.389 -  int minLeaf;          /* Minimum local payload in a LEAFDATA table */
   1.390 -  BusyHandler *pBusyHandler;   /* Callback for when there is lock contention */
   1.391 -  u8 inTransaction;     /* Transaction state */
   1.392 -  int nTransaction;     /* Number of open transactions (read + write) */
   1.393 -  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
   1.394 -  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
   1.395 -  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
   1.396 -#ifndef SQLITE_OMIT_SHARED_CACHE
   1.397 -  int nRef;             /* Number of references to this structure */
   1.398 -  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
   1.399 -  BtLock *pLock;        /* List of locks held on this shared-btree struct */
   1.400 -#endif
   1.401 -};
   1.402 -
   1.403 -/*
   1.404 -** An instance of the following structure is used to hold information
   1.405 -** about a cell.  The parseCellPtr() function fills in this structure
   1.406 -** based on information extract from the raw disk page.
   1.407 -*/
   1.408 -typedef struct CellInfo CellInfo;
   1.409 -struct CellInfo {
   1.410 -  u8 *pCell;     /* Pointer to the start of cell content */
   1.411 -  i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
   1.412 -  u32 nData;     /* Number of bytes of data */
   1.413 -  u32 nPayload;  /* Total amount of payload */
   1.414 -  u16 nHeader;   /* Size of the cell content header in bytes */
   1.415 -  u16 nLocal;    /* Amount of payload held locally */
   1.416 -  u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
   1.417 -  u16 nSize;     /* Size of the cell content on the main b-tree page */
   1.418 -};
   1.419 -
   1.420 -/*
   1.421 -** A cursor is a pointer to a particular entry within a particular
   1.422 -** b-tree within a database file.
   1.423 -**
   1.424 -** The entry is identified by its MemPage and the index in
   1.425 -** MemPage.aCell[] of the entry.
   1.426 -**
   1.427 -** When a single database file can shared by two more database connections,
   1.428 -** but cursors cannot be shared.  Each cursor is associated with a
   1.429 -** particular database connection identified BtCursor.pBtree.pSqlite.
   1.430 -**
   1.431 -** Fields in this structure are accessed under the BtShared.mutex
   1.432 -** found at self->pBt->mutex. 
   1.433 -*/
   1.434 -struct BtCursor {
   1.435 -  Btree *pBtree;            /* The Btree to which this cursor belongs */
   1.436 -  BtShared *pBt;            /* The BtShared this cursor points to */
   1.437 -  BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
   1.438 -  int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
   1.439 -  void *pArg;               /* First arg to xCompare() */
   1.440 -  Pgno pgnoRoot;            /* The root page of this tree */
   1.441 -  MemPage *pPage;           /* Page that contains the entry */
   1.442 -  int idx;                  /* Index of the entry in pPage->aCell[] */
   1.443 -  CellInfo info;            /* A parse of the cell we are pointing at */
   1.444 -  u8 wrFlag;                /* True if writable */
   1.445 -  u8 eState;                /* One of the CURSOR_XXX constants (see below) */
   1.446 -  void *pKey;      /* Saved key that was cursor's last known position */
   1.447 -  i64 nKey;        /* Size of pKey, or last integer key */
   1.448 -  int skip;        /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
   1.449 -#ifndef SQLITE_OMIT_INCRBLOB
   1.450 -  u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
   1.451 -  Pgno *aOverflow;          /* Cache of overflow page locations */
   1.452 -#endif
   1.453 -};
   1.454 -
   1.455 -/*
   1.456 -** Potential values for BtCursor.eState.
   1.457 -**
   1.458 -** CURSOR_VALID:
   1.459 -**   Cursor points to a valid entry. getPayload() etc. may be called.
   1.460 -**
   1.461 -** CURSOR_INVALID:
   1.462 -**   Cursor does not point to a valid entry. This can happen (for example) 
   1.463 -**   because the table is empty or because BtreeCursorFirst() has not been
   1.464 -**   called.
   1.465 -**
   1.466 -** CURSOR_REQUIRESEEK:
   1.467 -**   The table that this cursor was opened on still exists, but has been 
   1.468 -**   modified since the cursor was last used. The cursor position is saved
   1.469 -**   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 
   1.470 -**   this state, restoreOrClearCursorPosition() can be called to attempt to
   1.471 -**   seek the cursor to the saved position.
   1.472 -**
   1.473 -** CURSOR_FAULT:
   1.474 -**   A unrecoverable error (an I/O error or a malloc failure) has occurred
   1.475 -**   on a different connection that shares the BtShared cache with this
   1.476 -**   cursor.  The error has left the cache in an inconsistent state.
   1.477 -**   Do nothing else with this cursor.  Any attempt to use the cursor
   1.478 -**   should return the error code stored in BtCursor.skip
   1.479 -*/
   1.480 -#define CURSOR_INVALID           0
   1.481 -#define CURSOR_VALID             1
   1.482 -#define CURSOR_REQUIRESEEK       2
   1.483 -#define CURSOR_FAULT             3
   1.484 -
   1.485 -/*
   1.486 -** The TRACE macro will print high-level status information about the
   1.487 -** btree operation when the global variable sqlite3_btree_trace is
   1.488 -** enabled.
   1.489 -*/
   1.490 -#if SQLITE_TEST
   1.491 -# define TRACE(X)   if( sqlite3_btree_trace ){ printf X; fflush(stdout); }
   1.492 -#else
   1.493 -# define TRACE(X)
   1.494 -#endif
   1.495 -
   1.496 -/*
   1.497 -** Routines to read and write variable-length integers.  These used to
   1.498 -** be defined locally, but now we use the varint routines in the util.c
   1.499 -** file.
   1.500 -*/
   1.501 -#define getVarint    sqlite3GetVarint
   1.502 -#define getVarint32(A,B)  ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
   1.503 -#define putVarint    sqlite3PutVarint
   1.504 -
   1.505 -/* The database page the PENDING_BYTE occupies. This page is never used.
   1.506 -** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
   1.507 -** should possibly be consolidated (presumably in pager.h).
   1.508 -**
   1.509 -** If disk I/O is omitted (meaning that the database is stored purely
   1.510 -** in memory) then there is no pending byte.
   1.511 -*/
   1.512 -#ifdef SQLITE_OMIT_DISKIO
   1.513 -# define PENDING_BYTE_PAGE(pBt)  0x7fffffff
   1.514 -#else
   1.515 -# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
   1.516 -#endif
   1.517 -
   1.518 -/*
   1.519 -** A linked list of the following structures is stored at BtShared.pLock.
   1.520 -** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
   1.521 -** is opened on the table with root page BtShared.iTable. Locks are removed
   1.522 -** from this list when a transaction is committed or rolled back, or when
   1.523 -** a btree handle is closed.
   1.524 -*/
   1.525 -struct BtLock {
   1.526 -  Btree *pBtree;        /* Btree handle holding this lock */
   1.527 -  Pgno iTable;          /* Root page of table */
   1.528 -  u8 eLock;             /* READ_LOCK or WRITE_LOCK */
   1.529 -  BtLock *pNext;        /* Next in BtShared.pLock list */
   1.530 -};
   1.531 -
   1.532 -/* Candidate values for BtLock.eLock */
   1.533 -#define READ_LOCK     1
   1.534 -#define WRITE_LOCK    2
   1.535 -
   1.536 -/*
   1.537 -** These macros define the location of the pointer-map entry for a 
   1.538 -** database page. The first argument to each is the number of usable
   1.539 -** bytes on each page of the database (often 1024). The second is the
   1.540 -** page number to look up in the pointer map.
   1.541 -**
   1.542 -** PTRMAP_PAGENO returns the database page number of the pointer-map
   1.543 -** page that stores the required pointer. PTRMAP_PTROFFSET returns
   1.544 -** the offset of the requested map entry.
   1.545 -**
   1.546 -** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
   1.547 -** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
   1.548 -** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
   1.549 -** this test.
   1.550 -*/
   1.551 -#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
   1.552 -#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
   1.553 -#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
   1.554 -
   1.555 -/*
   1.556 -** The pointer map is a lookup table that identifies the parent page for
   1.557 -** each child page in the database file.  The parent page is the page that
   1.558 -** contains a pointer to the child.  Every page in the database contains
   1.559 -** 0 or 1 parent pages.  (In this context 'database page' refers
   1.560 -** to any page that is not part of the pointer map itself.)  Each pointer map
   1.561 -** entry consists of a single byte 'type' and a 4 byte parent page number.
   1.562 -** The PTRMAP_XXX identifiers below are the valid types.
   1.563 -**
   1.564 -** The purpose of the pointer map is to facility moving pages from one
   1.565 -** position in the file to another as part of autovacuum.  When a page
   1.566 -** is moved, the pointer in its parent must be updated to point to the
   1.567 -** new location.  The pointer map is used to locate the parent page quickly.
   1.568 -**
   1.569 -** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
   1.570 -**                  used in this case.
   1.571 -**
   1.572 -** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 
   1.573 -**                  is not used in this case.
   1.574 -**
   1.575 -** PTRMAP_OVERFLOW1: The database page is the first page in a list of 
   1.576 -**                   overflow pages. The page number identifies the page that
   1.577 -**                   contains the cell with a pointer to this overflow page.
   1.578 -**
   1.579 -** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
   1.580 -**                   overflow pages. The page-number identifies the previous
   1.581 -**                   page in the overflow page list.
   1.582 -**
   1.583 -** PTRMAP_BTREE: The database page is a non-root btree page. The page number
   1.584 -**               identifies the parent page in the btree.
   1.585 -*/
   1.586 -#define PTRMAP_ROOTPAGE 1
   1.587 -#define PTRMAP_FREEPAGE 2
   1.588 -#define PTRMAP_OVERFLOW1 3
   1.589 -#define PTRMAP_OVERFLOW2 4
   1.590 -#define PTRMAP_BTREE 5
   1.591 -
   1.592 -/* A bunch of assert() statements to check the transaction state variables
   1.593 -** of handle p (type Btree*) are internally consistent.
   1.594 -*/
   1.595 -#define btreeIntegrity(p) \
   1.596 -  assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
   1.597 -  assert( p->pBt->inTransaction>=p->inTrans ); 
   1.598 -
   1.599 -
   1.600 -/*
   1.601 -** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
   1.602 -** if the database supports auto-vacuum or not. Because it is used
   1.603 -** within an expression that is an argument to another macro 
   1.604 -** (sqliteMallocRaw), it is not possible to use conditional compilation.
   1.605 -** So, this macro is defined instead.
   1.606 -*/
   1.607 -#ifndef SQLITE_OMIT_AUTOVACUUM
   1.608 -#define ISAUTOVACUUM (pBt->autoVacuum)
   1.609 -#else
   1.610 -#define ISAUTOVACUUM 0
   1.611 -#endif
   1.612 -
   1.613 -
   1.614 -/*
   1.615 -** This structure is passed around through all the sanity checking routines
   1.616 -** in order to keep track of some global state information.
   1.617 -*/
   1.618 -typedef struct IntegrityCk IntegrityCk;
   1.619 -struct IntegrityCk {
   1.620 -  BtShared *pBt;    /* The tree being checked out */
   1.621 -  Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
   1.622 -  int nPage;        /* Number of pages in the database */
   1.623 -  int *anRef;       /* Number of times each page is referenced */
   1.624 -  int mxErr;        /* Stop accumulating errors when this reaches zero */
   1.625 -  char *zErrMsg;    /* An error message.  NULL if no errors seen. */
   1.626 -  int nErr;         /* Number of messages written to zErrMsg so far */
   1.627 -};
   1.628 -
   1.629 -/*
   1.630 -** Read or write a two- and four-byte big-endian integer values.
   1.631 -*/
   1.632 -#define get2byte(x)   ((x)[0]<<8 | (x)[1])
   1.633 -#define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v))
   1.634 -#define get4byte sqlite3Get4byte
   1.635 -#define put4byte sqlite3Put4byte
   1.636 -
   1.637 -/*
   1.638 -** Internal routines that should be accessed by the btree layer only.
   1.639 -*/
   1.640 -int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
   1.641 -int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent);
   1.642 -void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
   1.643 -void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
   1.644 -#ifdef SQLITE_TEST
   1.645 -u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell);
   1.646 -#endif
   1.647 -int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur);
   1.648 -void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
   1.649 -void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
   1.650 -int sqlite3BtreeIsRootPage(MemPage *pPage);
   1.651 -void sqlite3BtreeMoveToParent(BtCursor *pCur);