1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/btreeInt.h Sun Jul 29 01:15:31 2007 +0000
1.3 @@ -0,0 +1,588 @@
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 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 +struct MemPage {
1.272 + u8 isInit; /* True if previously initialized. MUST BE FIRST! */
1.273 + u8 idxShift; /* True if Cell indices have changed */
1.274 + u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
1.275 + u8 intKey; /* True if intkey flag is set */
1.276 + u8 leaf; /* True if leaf flag is set */
1.277 + u8 zeroData; /* True if table stores keys only */
1.278 + u8 leafData; /* True if tables stores data on leaves only */
1.279 + u8 hasData; /* True if this page stores data */
1.280 + u8 hdrOffset; /* 100 for page 1. 0 otherwise */
1.281 + u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
1.282 + u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */
1.283 + u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */
1.284 + u16 cellOffset; /* Index in aData of first cell pointer */
1.285 + u16 idxParent; /* Index in parent of this node */
1.286 + u16 nFree; /* Number of free bytes on the page */
1.287 + u16 nCell; /* Number of cells on this page, local and ovfl */
1.288 + struct _OvflCell { /* Cells that will not fit on aData[] */
1.289 + u8 *pCell; /* Pointers to the body of the overflow cell */
1.290 + u16 idx; /* Insert this cell before idx-th non-overflow cell */
1.291 + } aOvfl[5];
1.292 + BtShared *pBt; /* Pointer back to BTree structure */
1.293 + u8 *aData; /* Pointer back to the start of the page */
1.294 + DbPage *pDbPage; /* Pager page handle */
1.295 + Pgno pgno; /* Page number for this page */
1.296 + MemPage *pParent; /* The parent of this page. NULL for root */
1.297 +};
1.298 +
1.299 +/*
1.300 +** The in-memory image of a disk page has the auxiliary information appended
1.301 +** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
1.302 +** that extra information.
1.303 +*/
1.304 +#define EXTRA_SIZE sizeof(MemPage)
1.305 +
1.306 +/* Btree handle */
1.307 +struct Btree {
1.308 + sqlite3 *pSqlite;
1.309 + BtShared *pBt;
1.310 + u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
1.311 +};
1.312 +
1.313 +/*
1.314 +** Btree.inTrans may take one of the following values.
1.315 +**
1.316 +** If the shared-data extension is enabled, there may be multiple users
1.317 +** of the Btree structure. At most one of these may open a write transaction,
1.318 +** but any number may have active read transactions. Variable Btree.pDb
1.319 +** points to the handle that owns any current write-transaction.
1.320 +*/
1.321 +#define TRANS_NONE 0
1.322 +#define TRANS_READ 1
1.323 +#define TRANS_WRITE 2
1.324 +
1.325 +/*
1.326 +** Everything we need to know about an open database
1.327 +*/
1.328 +struct BtShared {
1.329 + Pager *pPager; /* The page cache */
1.330 + BtCursor *pCursor; /* A list of all open cursors */
1.331 + MemPage *pPage1; /* First page of the database */
1.332 + u8 inStmt; /* True if we are in a statement subtransaction */
1.333 + u8 readOnly; /* True if the underlying file is readonly */
1.334 + u8 maxEmbedFrac; /* Maximum payload as % of total page size */
1.335 + u8 minEmbedFrac; /* Minimum payload as % of total page size */
1.336 + u8 minLeafFrac; /* Minimum leaf payload as % of total page size */
1.337 + u8 pageSizeFixed; /* True if the page size can no longer be changed */
1.338 +#ifndef SQLITE_OMIT_AUTOVACUUM
1.339 + u8 autoVacuum; /* True if auto-vacuum is enabled */
1.340 + u8 incrVacuum; /* True if incr-vacuum is enabled */
1.341 + Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */
1.342 +#endif
1.343 + u16 pageSize; /* Total number of bytes on a page */
1.344 + u16 usableSize; /* Number of usable bytes on each page */
1.345 + int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
1.346 + int minLocal; /* Minimum local payload in non-LEAFDATA tables */
1.347 + int maxLeaf; /* Maximum local payload in a LEAFDATA table */
1.348 + int minLeaf; /* Minimum local payload in a LEAFDATA table */
1.349 + BusyHandler *pBusyHandler; /* Callback for when there is lock contention */
1.350 + u8 inTransaction; /* Transaction state */
1.351 + int nRef; /* Number of references to this structure */
1.352 + int nTransaction; /* Number of open transactions (read + write) */
1.353 + void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
1.354 + void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
1.355 +#ifndef SQLITE_OMIT_SHARED_CACHE
1.356 + BtLock *pLock; /* List of locks held on this shared-btree struct */
1.357 + BtShared *pNext; /* Next in ThreadData.pBtree linked list */
1.358 +#endif
1.359 +};
1.360 +
1.361 +/*
1.362 +** An instance of the following structure is used to hold information
1.363 +** about a cell. The parseCellPtr() function fills in this structure
1.364 +** based on information extract from the raw disk page.
1.365 +*/
1.366 +typedef struct CellInfo CellInfo;
1.367 +struct CellInfo {
1.368 + u8 *pCell; /* Pointer to the start of cell content */
1.369 + i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
1.370 + u32 nData; /* Number of bytes of data */
1.371 + u32 nPayload; /* Total amount of payload */
1.372 + u16 nHeader; /* Size of the cell content header in bytes */
1.373 + u16 nLocal; /* Amount of payload held locally */
1.374 + u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
1.375 + u16 nSize; /* Size of the cell content on the main b-tree page */
1.376 +};
1.377 +
1.378 +/*
1.379 +** A cursor is a pointer to a particular entry in the BTree.
1.380 +** The entry is identified by its MemPage and the index in
1.381 +** MemPage.aCell[] of the entry.
1.382 +*/
1.383 +struct BtCursor {
1.384 + Btree *pBtree; /* The Btree to which this cursor belongs */
1.385 + BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
1.386 + int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
1.387 + void *pArg; /* First arg to xCompare() */
1.388 + Pgno pgnoRoot; /* The root page of this tree */
1.389 + MemPage *pPage; /* Page that contains the entry */
1.390 + int idx; /* Index of the entry in pPage->aCell[] */
1.391 + CellInfo info; /* A parse of the cell we are pointing at */
1.392 + u8 wrFlag; /* True if writable */
1.393 + u8 eState; /* One of the CURSOR_XXX constants (see below) */
1.394 + void *pKey; /* Saved key that was cursor's last known position */
1.395 + i64 nKey; /* Size of pKey, or last integer key */
1.396 + int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
1.397 +#ifndef SQLITE_OMIT_INCRBLOB
1.398 + u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */
1.399 + Pgno *aOverflow; /* Cache of overflow page locations */
1.400 +#endif
1.401 +};
1.402 +
1.403 +/*
1.404 +** Potential values for BtCursor.eState.
1.405 +**
1.406 +** CURSOR_VALID:
1.407 +** Cursor points to a valid entry. getPayload() etc. may be called.
1.408 +**
1.409 +** CURSOR_INVALID:
1.410 +** Cursor does not point to a valid entry. This can happen (for example)
1.411 +** because the table is empty or because BtreeCursorFirst() has not been
1.412 +** called.
1.413 +**
1.414 +** CURSOR_REQUIRESEEK:
1.415 +** The table that this cursor was opened on still exists, but has been
1.416 +** modified since the cursor was last used. The cursor position is saved
1.417 +** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
1.418 +** this state, restoreOrClearCursorPosition() can be called to attempt to
1.419 +** seek the cursor to the saved position.
1.420 +*/
1.421 +#define CURSOR_INVALID 0
1.422 +#define CURSOR_VALID 1
1.423 +#define CURSOR_REQUIRESEEK 2
1.424 +
1.425 +/*
1.426 +** The TRACE macro will print high-level status information about the
1.427 +** btree operation when the global variable sqlite3_btree_trace is
1.428 +** enabled.
1.429 +*/
1.430 +#if SQLITE_TEST
1.431 +# define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); }
1.432 +#else
1.433 +# define TRACE(X)
1.434 +#endif
1.435 +
1.436 +/*
1.437 +** Routines to read and write variable-length integers. These used to
1.438 +** be defined locally, but now we use the varint routines in the util.c
1.439 +** file.
1.440 +*/
1.441 +#define getVarint sqlite3GetVarint
1.442 +#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
1.443 +#define putVarint sqlite3PutVarint
1.444 +
1.445 +/* The database page the PENDING_BYTE occupies. This page is never used.
1.446 +** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
1.447 +** should possibly be consolidated (presumably in pager.h).
1.448 +**
1.449 +** If disk I/O is omitted (meaning that the database is stored purely
1.450 +** in memory) then there is no pending byte.
1.451 +*/
1.452 +#ifdef SQLITE_OMIT_DISKIO
1.453 +# define PENDING_BYTE_PAGE(pBt) 0x7fffffff
1.454 +#else
1.455 +# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
1.456 +#endif
1.457 +
1.458 +/*
1.459 +** A linked list of the following structures is stored at BtShared.pLock.
1.460 +** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
1.461 +** is opened on the table with root page BtShared.iTable. Locks are removed
1.462 +** from this list when a transaction is committed or rolled back, or when
1.463 +** a btree handle is closed.
1.464 +*/
1.465 +struct BtLock {
1.466 + Btree *pBtree; /* Btree handle holding this lock */
1.467 + Pgno iTable; /* Root page of table */
1.468 + u8 eLock; /* READ_LOCK or WRITE_LOCK */
1.469 + BtLock *pNext; /* Next in BtShared.pLock list */
1.470 +};
1.471 +
1.472 +/* Candidate values for BtLock.eLock */
1.473 +#define READ_LOCK 1
1.474 +#define WRITE_LOCK 2
1.475 +
1.476 +/*
1.477 +** These macros define the location of the pointer-map entry for a
1.478 +** database page. The first argument to each is the number of usable
1.479 +** bytes on each page of the database (often 1024). The second is the
1.480 +** page number to look up in the pointer map.
1.481 +**
1.482 +** PTRMAP_PAGENO returns the database page number of the pointer-map
1.483 +** page that stores the required pointer. PTRMAP_PTROFFSET returns
1.484 +** the offset of the requested map entry.
1.485 +**
1.486 +** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
1.487 +** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
1.488 +** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
1.489 +** this test.
1.490 +*/
1.491 +#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
1.492 +#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
1.493 +#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
1.494 +
1.495 +/*
1.496 +** The pointer map is a lookup table that identifies the parent page for
1.497 +** each child page in the database file. The parent page is the page that
1.498 +** contains a pointer to the child. Every page in the database contains
1.499 +** 0 or 1 parent pages. (In this context 'database page' refers
1.500 +** to any page that is not part of the pointer map itself.) Each pointer map
1.501 +** entry consists of a single byte 'type' and a 4 byte parent page number.
1.502 +** The PTRMAP_XXX identifiers below are the valid types.
1.503 +**
1.504 +** The purpose of the pointer map is to facility moving pages from one
1.505 +** position in the file to another as part of autovacuum. When a page
1.506 +** is moved, the pointer in its parent must be updated to point to the
1.507 +** new location. The pointer map is used to locate the parent page quickly.
1.508 +**
1.509 +** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
1.510 +** used in this case.
1.511 +**
1.512 +** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
1.513 +** is not used in this case.
1.514 +**
1.515 +** PTRMAP_OVERFLOW1: The database page is the first page in a list of
1.516 +** overflow pages. The page number identifies the page that
1.517 +** contains the cell with a pointer to this overflow page.
1.518 +**
1.519 +** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
1.520 +** overflow pages. The page-number identifies the previous
1.521 +** page in the overflow page list.
1.522 +**
1.523 +** PTRMAP_BTREE: The database page is a non-root btree page. The page number
1.524 +** identifies the parent page in the btree.
1.525 +*/
1.526 +#define PTRMAP_ROOTPAGE 1
1.527 +#define PTRMAP_FREEPAGE 2
1.528 +#define PTRMAP_OVERFLOW1 3
1.529 +#define PTRMAP_OVERFLOW2 4
1.530 +#define PTRMAP_BTREE 5
1.531 +
1.532 +/* A bunch of assert() statements to check the transaction state variables
1.533 +** of handle p (type Btree*) are internally consistent.
1.534 +*/
1.535 +#define btreeIntegrity(p) \
1.536 + assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \
1.537 + assert( p->pBt->nTransaction<=p->pBt->nRef ); \
1.538 + assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
1.539 + assert( p->pBt->inTransaction>=p->inTrans );
1.540 +
1.541 +
1.542 +/*
1.543 +** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
1.544 +** if the database supports auto-vacuum or not. Because it is used
1.545 +** within an expression that is an argument to another macro
1.546 +** (sqliteMallocRaw), it is not possible to use conditional compilation.
1.547 +** So, this macro is defined instead.
1.548 +*/
1.549 +#ifndef SQLITE_OMIT_AUTOVACUUM
1.550 +#define ISAUTOVACUUM (pBt->autoVacuum)
1.551 +#else
1.552 +#define ISAUTOVACUUM 0
1.553 +#endif
1.554 +
1.555 +
1.556 +/*
1.557 +** This structure is passed around through all the sanity checking routines
1.558 +** in order to keep track of some global state information.
1.559 +*/
1.560 +typedef struct IntegrityCk IntegrityCk;
1.561 +struct IntegrityCk {
1.562 + BtShared *pBt; /* The tree being checked out */
1.563 + Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
1.564 + int nPage; /* Number of pages in the database */
1.565 + int *anRef; /* Number of times each page is referenced */
1.566 + int mxErr; /* Stop accumulating errors when this reaches zero */
1.567 + char *zErrMsg; /* An error message. NULL if no errors seen. */
1.568 + int nErr; /* Number of messages written to zErrMsg so far */
1.569 +};
1.570 +
1.571 +/*
1.572 +** Read or write a two- and four-byte big-endian integer values.
1.573 +*/
1.574 +#define get2byte(x) ((x)[0]<<8 | (x)[1])
1.575 +#define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v))
1.576 +#define get4byte sqlite3Get4byte
1.577 +#define put4byte sqlite3Put4byte
1.578 +
1.579 +/*
1.580 +** Internal routines that should be accessed by the btree layer only.
1.581 +*/
1.582 +int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int);
1.583 +int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent);
1.584 +void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*);
1.585 +void sqlite3BtreeParseCell(MemPage*, int, CellInfo*);
1.586 +u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell);
1.587 +int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur);
1.588 +void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur);
1.589 +void sqlite3BtreeReleaseTempCursor(BtCursor *pCur);
1.590 +int sqlite3BtreeIsRootPage(MemPage *pPage);
1.591 +void sqlite3BtreeMoveToParent(BtCursor *pCur);