00001 /* 00002 ** 2004 April 6 00003 ** 00004 ** The author disclaims copyright to this source code. In place of 00005 ** a legal notice, here is a blessing: 00006 ** 00007 ** May you do good and not evil. 00008 ** May you find forgiveness for yourself and forgive others. 00009 ** May you share freely, never taking more than you give. 00010 ** 00011 ************************************************************************* 00012 ** $Id: btreeInt.h,v 1.34 2008/09/30 17:18:17 drh Exp $ 00013 ** 00014 ** This file implements a external (disk-based) database using BTrees. 00015 ** For a detailed discussion of BTrees, refer to 00016 ** 00017 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: 00018 ** "Sorting And Searching", pages 473-480. Addison-Wesley 00019 ** Publishing Company, Reading, Massachusetts. 00020 ** 00021 ** The basic idea is that each page of the file contains N database 00022 ** entries and N+1 pointers to subpages. 00023 ** 00024 ** ---------------------------------------------------------------- 00025 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | 00026 ** ---------------------------------------------------------------- 00027 ** 00028 ** All of the keys on the page that Ptr(0) points to have values less 00029 ** than Key(0). All of the keys on page Ptr(1) and its subpages have 00030 ** values greater than Key(0) and less than Key(1). All of the keys 00031 ** on Ptr(N) and its subpages have values greater than Key(N-1). And 00032 ** so forth. 00033 ** 00034 ** Finding a particular key requires reading O(log(M)) pages from the 00035 ** disk where M is the number of entries in the tree. 00036 ** 00037 ** In this implementation, a single file can hold one or more separate 00038 ** BTrees. Each BTree is identified by the index of its root page. The 00039 ** key and data for any entry are combined to form the "payload". A 00040 ** fixed amount of payload can be carried directly on the database 00041 ** page. If the payload is larger than the preset amount then surplus 00042 ** bytes are stored on overflow pages. The payload for an entry 00043 ** and the preceding pointer are combined to form a "Cell". Each 00044 ** page has a small header which contains the Ptr(N) pointer and other 00045 ** information such as the size of key and data. 00046 ** 00047 ** FORMAT DETAILS 00048 ** 00049 ** The file is divided into pages. The first page is called page 1, 00050 ** the second is page 2, and so forth. A page number of zero indicates 00051 ** "no such page". The page size can be anything between 512 and 65536. 00052 ** Each page can be either a btree page, a freelist page or an overflow 00053 ** page. 00054 ** 00055 ** The first page is always a btree page. The first 100 bytes of the first 00056 ** page contain a special header (the "file header") that describes the file. 00057 ** The format of the file header is as follows: 00058 ** 00059 ** OFFSET SIZE DESCRIPTION 00060 ** 0 16 Header string: "SQLite format 3\000" 00061 ** 16 2 Page size in bytes. 00062 ** 18 1 File format write version 00063 ** 19 1 File format read version 00064 ** 20 1 Bytes of unused space at the end of each page 00065 ** 21 1 Max embedded payload fraction 00066 ** 22 1 Min embedded payload fraction 00067 ** 23 1 Min leaf payload fraction 00068 ** 24 4 File change counter 00069 ** 28 4 Reserved for future use 00070 ** 32 4 First freelist page 00071 ** 36 4 Number of freelist pages in the file 00072 ** 40 60 15 4-byte meta values passed to higher layers 00073 ** 00074 ** All of the integer values are big-endian (most significant byte first). 00075 ** 00076 ** The file change counter is incremented when the database is changed 00077 ** This counter allows other processes to know when the file has changed 00078 ** and thus when they need to flush their cache. 00079 ** 00080 ** The max embedded payload fraction is the amount of the total usable 00081 ** space in a page that can be consumed by a single cell for standard 00082 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default 00083 ** is to limit the maximum cell size so that at least 4 cells will fit 00084 ** on one page. Thus the default max embedded payload fraction is 64. 00085 ** 00086 ** If the payload for a cell is larger than the max payload, then extra 00087 ** payload is spilled to overflow pages. Once an overflow page is allocated, 00088 ** as many bytes as possible are moved into the overflow pages without letting 00089 ** the cell size drop below the min embedded payload fraction. 00090 ** 00091 ** The min leaf payload fraction is like the min embedded payload fraction 00092 ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum 00093 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it 00094 ** not specified in the header. 00095 ** 00096 ** Each btree pages is divided into three sections: The header, the 00097 ** cell pointer array, and the cell content area. Page 1 also has a 100-byte 00098 ** file header that occurs before the page header. 00099 ** 00100 ** |----------------| 00101 ** | file header | 100 bytes. Page 1 only. 00102 ** |----------------| 00103 ** | page header | 8 bytes for leaves. 12 bytes for interior nodes 00104 ** |----------------| 00105 ** | cell pointer | | 2 bytes per cell. Sorted order. 00106 ** | array | | Grows downward 00107 ** | | v 00108 ** |----------------| 00109 ** | unallocated | 00110 ** | space | 00111 ** |----------------| ^ Grows upwards 00112 ** | cell content | | Arbitrary order interspersed with freeblocks. 00113 ** | area | | and free space fragments. 00114 ** |----------------| 00115 ** 00116 ** The page headers looks like this: 00117 ** 00118 ** OFFSET SIZE DESCRIPTION 00119 ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf 00120 ** 1 2 byte offset to the first freeblock 00121 ** 3 2 number of cells on this page 00122 ** 5 2 first byte of the cell content area 00123 ** 7 1 number of fragmented free bytes 00124 ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. 00125 ** 00126 ** The flags define the format of this btree page. The leaf flag means that 00127 ** this page has no children. The zerodata flag means that this page carries 00128 ** only keys and no data. The intkey flag means that the key is a integer 00129 ** which is stored in the key size entry of the cell header rather than in 00130 ** the payload area. 00131 ** 00132 ** The cell pointer array begins on the first byte after the page header. 00133 ** The cell pointer array contains zero or more 2-byte numbers which are 00134 ** offsets from the beginning of the page to the cell content in the cell 00135 ** content area. The cell pointers occur in sorted order. The system strives 00136 ** to keep free space after the last cell pointer so that new cells can 00137 ** be easily added without having to defragment the page. 00138 ** 00139 ** Cell content is stored at the very end of the page and grows toward the 00140 ** beginning of the page. 00141 ** 00142 ** Unused space within the cell content area is collected into a linked list of 00143 ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset 00144 ** to the first freeblock is given in the header. Freeblocks occur in 00145 ** increasing order. Because a freeblock must be at least 4 bytes in size, 00146 ** any group of 3 or fewer unused bytes in the cell content area cannot 00147 ** exist on the freeblock chain. A group of 3 or fewer free bytes is called 00148 ** a fragment. The total number of bytes in all fragments is recorded. 00149 ** in the page header at offset 7. 00150 ** 00151 ** SIZE DESCRIPTION 00152 ** 2 Byte offset of the next freeblock 00153 ** 2 Bytes in this freeblock 00154 ** 00155 ** Cells are of variable length. Cells are stored in the cell content area at 00156 ** the end of the page. Pointers to the cells are in the cell pointer array 00157 ** that immediately follows the page header. Cells is not necessarily 00158 ** contiguous or in order, but cell pointers are contiguous and in order. 00159 ** 00160 ** Cell content makes use of variable length integers. A variable 00161 ** length integer is 1 to 9 bytes where the lower 7 bits of each 00162 ** byte are used. The integer consists of all bytes that have bit 8 set and 00163 ** the first byte with bit 8 clear. The most significant byte of the integer 00164 ** appears first. A variable-length integer may not be more than 9 bytes long. 00165 ** As a special case, all 8 bytes of the 9th byte are used as data. This 00166 ** allows a 64-bit integer to be encoded in 9 bytes. 00167 ** 00168 ** 0x00 becomes 0x00000000 00169 ** 0x7f becomes 0x0000007f 00170 ** 0x81 0x00 becomes 0x00000080 00171 ** 0x82 0x00 becomes 0x00000100 00172 ** 0x80 0x7f becomes 0x0000007f 00173 ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 00174 ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 00175 ** 00176 ** Variable length integers are used for rowids and to hold the number of 00177 ** bytes of key and data in a btree cell. 00178 ** 00179 ** The content of a cell looks like this: 00180 ** 00181 ** SIZE DESCRIPTION 00182 ** 4 Page number of the left child. Omitted if leaf flag is set. 00183 ** var Number of bytes of data. Omitted if the zerodata flag is set. 00184 ** var Number of bytes of key. Or the key itself if intkey flag is set. 00185 ** * Payload 00186 ** 4 First page of the overflow chain. Omitted if no overflow 00187 ** 00188 ** Overflow pages form a linked list. Each page except the last is completely 00189 ** filled with data (pagesize - 4 bytes). The last page can have as little 00190 ** as 1 byte of data. 00191 ** 00192 ** SIZE DESCRIPTION 00193 ** 4 Page number of next overflow page 00194 ** * Data 00195 ** 00196 ** Freelist pages come in two subtypes: trunk pages and leaf pages. The 00197 ** file header points to the first in a linked list of trunk page. Each trunk 00198 ** page points to multiple leaf pages. The content of a leaf page is 00199 ** unspecified. A trunk page looks like this: 00200 ** 00201 ** SIZE DESCRIPTION 00202 ** 4 Page number of next trunk page 00203 ** 4 Number of leaf pointers on this page 00204 ** * zero or more pages numbers of leaves 00205 */ 00206 #include "sqliteInt.h" 00207 #include "pager.h" 00208 #include "btree.h" 00209 #include "os.h" 00210 #include <assert.h> 00211 00212 /* Round up a number to the next larger multiple of 8. This is used 00213 ** to force 8-byte alignment on 64-bit architectures. 00214 */ 00215 #define ROUND8(x) ((x+7)&~7) 00216 00217 00218 /* The following value is the maximum cell size assuming a maximum page 00219 ** size give above. 00220 */ 00221 #define MX_CELL_SIZE(pBt) (pBt->pageSize-8) 00222 00223 /* The maximum number of cells on a single page of the database. This 00224 ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself 00225 ** plus 2 bytes for the index to the cell in the page header). Such 00226 ** small cells will be rare, but they are possible. 00227 */ 00228 #define MX_CELL(pBt) ((pBt->pageSize-8)/6) 00229 00230 /* Forward declarations */ 00231 typedef struct MemPage MemPage; 00232 typedef struct BtLock BtLock; 00233 00234 /* 00235 ** This is a magic string that appears at the beginning of every 00236 ** SQLite database in order to identify the file as a real database. 00237 ** 00238 ** You can change this value at compile-time by specifying a 00239 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The 00240 ** header must be exactly 16 bytes including the zero-terminator so 00241 ** the string itself should be 15 characters long. If you change 00242 ** the header, then your custom library will not be able to read 00243 ** databases generated by the standard tools and the standard tools 00244 ** will not be able to read databases created by your custom library. 00245 */ 00246 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ 00247 # define SQLITE_FILE_HEADER "SQLite format 3" 00248 #endif 00249 00250 /* 00251 ** Page type flags. An ORed combination of these flags appear as the 00252 ** first byte of on-disk image of every BTree page. 00253 */ 00254 #define PTF_INTKEY 0x01 00255 #define PTF_ZERODATA 0x02 00256 #define PTF_LEAFDATA 0x04 00257 #define PTF_LEAF 0x08 00258 00259 /* 00260 ** As each page of the file is loaded into memory, an instance of the following 00261 ** structure is appended and initialized to zero. This structure stores 00262 ** information about the page that is decoded from the raw file page. 00263 ** 00264 ** The pParent field points back to the parent page. This allows us to 00265 ** walk up the BTree from any leaf to the root. Care must be taken to 00266 ** unref() the parent page pointer when this page is no longer referenced. 00267 ** The pageDestructor() routine handles that chore. 00268 ** 00269 ** Access to all fields of this structure is controlled by the mutex 00270 ** stored in MemPage.pBt->mutex. 00271 */ 00272 struct MemPage { 00273 u8 isInit; /* True if previously initialized. MUST BE FIRST! */ 00274 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ 00275 u8 intKey; /* True if intkey flag is set */ 00276 u8 leaf; /* True if leaf flag is set */ 00277 u8 hasData; /* True if this page stores data */ 00278 u8 hdrOffset; /* 100 for page 1. 0 otherwise */ 00279 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ 00280 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ 00281 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ 00282 u16 cellOffset; /* Index in aData of first cell pointer */ 00283 u16 nFree; /* Number of free bytes on the page */ 00284 u16 nCell; /* Number of cells on this page, local and ovfl */ 00285 u16 maskPage; /* Mask for page offset */ 00286 struct _OvflCell { /* Cells that will not fit on aData[] */ 00287 u8 *pCell; /* Pointers to the body of the overflow cell */ 00288 u16 idx; /* Insert this cell before idx-th non-overflow cell */ 00289 } aOvfl[5]; 00290 BtShared *pBt; /* Pointer to BtShared that this page is part of */ 00291 u8 *aData; /* Pointer to disk image of the page data */ 00292 DbPage *pDbPage; /* Pager page handle */ 00293 Pgno pgno; /* Page number for this page */ 00294 }; 00295 00296 /* 00297 ** The in-memory image of a disk page has the auxiliary information appended 00298 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold 00299 ** that extra information. 00300 */ 00301 #define EXTRA_SIZE sizeof(MemPage) 00302 00303 /* A Btree handle 00304 ** 00305 ** A database connection contains a pointer to an instance of 00306 ** this object for every database file that it has open. This structure 00307 ** is opaque to the database connection. The database connection cannot 00308 ** see the internals of this structure and only deals with pointers to 00309 ** this structure. 00310 ** 00311 ** For some database files, the same underlying database cache might be 00312 ** shared between multiple connections. In that case, each contection 00313 ** has it own pointer to this object. But each instance of this object 00314 ** points to the same BtShared object. The database cache and the 00315 ** schema associated with the database file are all contained within 00316 ** the BtShared object. 00317 ** 00318 ** All fields in this structure are accessed under sqlite3.mutex. 00319 ** The pBt pointer itself may not be changed while there exists cursors 00320 ** in the referenced BtShared that point back to this Btree since those 00321 ** cursors have to do go through this Btree to find their BtShared and 00322 ** they often do so without holding sqlite3.mutex. 00323 */ 00324 struct Btree { 00325 sqlite3 *db; /* The database connection holding this btree */ 00326 BtShared *pBt; /* Sharable content of this btree */ 00327 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ 00328 u8 sharable; /* True if we can share pBt with another db */ 00329 u8 locked; /* True if db currently has pBt locked */ 00330 int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ 00331 Btree *pNext; /* List of other sharable Btrees from the same db */ 00332 Btree *pPrev; /* Back pointer of the same list */ 00333 }; 00334 00335 /* 00336 ** Btree.inTrans may take one of the following values. 00337 ** 00338 ** If the shared-data extension is enabled, there may be multiple users 00339 ** of the Btree structure. At most one of these may open a write transaction, 00340 ** but any number may have active read transactions. 00341 */ 00342 #define TRANS_NONE 0 00343 #define TRANS_READ 1 00344 #define TRANS_WRITE 2 00345 00346 /* 00347 ** An instance of this object represents a single database file. 00348 ** 00349 ** A single database file can be in use as the same time by two 00350 ** or more database connections. When two or more connections are 00351 ** sharing the same database file, each connection has it own 00352 ** private Btree object for the file and each of those Btrees points 00353 ** to this one BtShared object. BtShared.nRef is the number of 00354 ** connections currently sharing this database file. 00355 ** 00356 ** Fields in this structure are accessed under the BtShared.mutex 00357 ** mutex, except for nRef and pNext which are accessed under the 00358 ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field 00359 ** may not be modified once it is initially set as long as nRef>0. 00360 ** The pSchema field may be set once under BtShared.mutex and 00361 ** thereafter is unchanged as long as nRef>0. 00362 */ 00363 struct BtShared { 00364 Pager *pPager; /* The page cache */ 00365 sqlite3 *db; /* Database connection currently using this Btree */ 00366 BtCursor *pCursor; /* A list of all open cursors */ 00367 MemPage *pPage1; /* First page of the database */ 00368 u8 inStmt; /* True if we are in a statement subtransaction */ 00369 u8 readOnly; /* True if the underlying file is readonly */ 00370 u8 pageSizeFixed; /* True if the page size can no longer be changed */ 00371 #ifndef SQLITE_OMIT_AUTOVACUUM 00372 u8 autoVacuum; /* True if auto-vacuum is enabled */ 00373 u8 incrVacuum; /* True if incr-vacuum is enabled */ 00374 Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */ 00375 #endif 00376 u16 pageSize; /* Total number of bytes on a page */ 00377 u16 usableSize; /* Number of usable bytes on each page */ 00378 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ 00379 int minLocal; /* Minimum local payload in non-LEAFDATA tables */ 00380 int maxLeaf; /* Maximum local payload in a LEAFDATA table */ 00381 int minLeaf; /* Minimum local payload in a LEAFDATA table */ 00382 u8 inTransaction; /* Transaction state */ 00383 int nTransaction; /* Number of open transactions (read + write) */ 00384 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ 00385 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ 00386 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */ 00387 BusyHandler busyHdr; /* The busy handler for this btree */ 00388 #ifndef SQLITE_OMIT_SHARED_CACHE 00389 int nRef; /* Number of references to this structure */ 00390 BtShared *pNext; /* Next on a list of sharable BtShared structs */ 00391 BtLock *pLock; /* List of locks held on this shared-btree struct */ 00392 Btree *pExclusive; /* Btree with an EXCLUSIVE lock on the whole db */ 00393 #endif 00394 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */ 00395 }; 00396 00397 /* 00398 ** An instance of the following structure is used to hold information 00399 ** about a cell. The parseCellPtr() function fills in this structure 00400 ** based on information extract from the raw disk page. 00401 */ 00402 typedef struct CellInfo CellInfo; 00403 struct CellInfo { 00404 u8 *pCell; /* Pointer to the start of cell content */ 00405 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ 00406 u32 nData; /* Number of bytes of data */ 00407 u32 nPayload; /* Total amount of payload */ 00408 u16 nHeader; /* Size of the cell content header in bytes */ 00409 u16 nLocal; /* Amount of payload held locally */ 00410 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ 00411 u16 nSize; /* Size of the cell content on the main b-tree page */ 00412 }; 00413 00414 /* 00415 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than 00416 ** this will be declared corrupt. This value is calculated based on a 00417 ** maximum database size of 2^31 pages a minimum fanout of 2 for a 00418 ** root-node and 3 for all other internal nodes. 00419 ** 00420 ** If a tree that appears to be taller than this is encountered, it is 00421 ** assumed that the database is corrupt. 00422 */ 00423 #define BTCURSOR_MAX_DEPTH 20 00424 00425 /* 00426 ** A cursor is a pointer to a particular entry within a particular 00427 ** b-tree within a database file. 00428 ** 00429 ** The entry is identified by its MemPage and the index in 00430 ** MemPage.aCell[] of the entry. 00431 ** 00432 ** When a single database file can shared by two more database connections, 00433 ** but cursors cannot be shared. Each cursor is associated with a 00434 ** particular database connection identified BtCursor.pBtree.db. 00435 ** 00436 ** Fields in this structure are accessed under the BtShared.mutex 00437 ** found at self->pBt->mutex. 00438 */ 00439 struct BtCursor { 00440 Btree *pBtree; /* The Btree to which this cursor belongs */ 00441 BtShared *pBt; /* The BtShared this cursor points to */ 00442 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ 00443 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ 00444 Pgno pgnoRoot; /* The root page of this tree */ 00445 CellInfo info; /* A parse of the cell we are pointing at */ 00446 u8 wrFlag; /* True if writable */ 00447 u8 atLast; /* Cursor pointing to the last entry */ 00448 u8 validNKey; /* True if info.nKey is valid */ 00449 u8 eState; /* One of the CURSOR_XXX constants (see below) */ 00450 void *pKey; /* Saved key that was cursor's last known position */ 00451 i64 nKey; /* Size of pKey, or last integer key */ 00452 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ 00453 #ifndef SQLITE_OMIT_INCRBLOB 00454 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */ 00455 Pgno *aOverflow; /* Cache of overflow page locations */ 00456 #endif 00457 #ifndef NDEBUG 00458 u8 pagesShuffled; /* True if Btree pages are rearranged by balance()*/ 00459 #endif 00460 i16 iPage; /* Index of current page in apPage */ 00461 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ 00462 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ 00463 }; 00464 00465 /* 00466 ** Potential values for BtCursor.eState. 00467 ** 00468 ** CURSOR_VALID: 00469 ** Cursor points to a valid entry. getPayload() etc. may be called. 00470 ** 00471 ** CURSOR_INVALID: 00472 ** Cursor does not point to a valid entry. This can happen (for example) 00473 ** because the table is empty or because BtreeCursorFirst() has not been 00474 ** called. 00475 ** 00476 ** CURSOR_REQUIRESEEK: 00477 ** The table that this cursor was opened on still exists, but has been 00478 ** modified since the cursor was last used. The cursor position is saved 00479 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 00480 ** this state, restoreCursorPosition() can be called to attempt to 00481 ** seek the cursor to the saved position. 00482 ** 00483 ** CURSOR_FAULT: 00484 ** A unrecoverable error (an I/O error or a malloc failure) has occurred 00485 ** on a different connection that shares the BtShared cache with this 00486 ** cursor. The error has left the cache in an inconsistent state. 00487 ** Do nothing else with this cursor. Any attempt to use the cursor 00488 ** should return the error code stored in BtCursor.skip 00489 */ 00490 #define CURSOR_INVALID 0 00491 #define CURSOR_VALID 1 00492 #define CURSOR_REQUIRESEEK 2 00493 #define CURSOR_FAULT 3 00494 00495 /* The database page the PENDING_BYTE occupies. This page is never used. 00496 ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They 00497 ** should possibly be consolidated (presumably in pager.h). 00498 ** 00499 ** If disk I/O is omitted (meaning that the database is stored purely 00500 ** in memory) then there is no pending byte. 00501 */ 00502 #ifdef SQLITE_OMIT_DISKIO 00503 # define PENDING_BYTE_PAGE(pBt) 0x7fffffff 00504 #else 00505 # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) 00506 #endif 00507 00508 /* 00509 ** A linked list of the following structures is stored at BtShared.pLock. 00510 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 00511 ** is opened on the table with root page BtShared.iTable. Locks are removed 00512 ** from this list when a transaction is committed or rolled back, or when 00513 ** a btree handle is closed. 00514 */ 00515 struct BtLock { 00516 Btree *pBtree; /* Btree handle holding this lock */ 00517 Pgno iTable; /* Root page of table */ 00518 u8 eLock; /* READ_LOCK or WRITE_LOCK */ 00519 BtLock *pNext; /* Next in BtShared.pLock list */ 00520 }; 00521 00522 /* Candidate values for BtLock.eLock */ 00523 #define READ_LOCK 1 00524 #define WRITE_LOCK 2 00525 00526 /* 00527 ** These macros define the location of the pointer-map entry for a 00528 ** database page. The first argument to each is the number of usable 00529 ** bytes on each page of the database (often 1024). The second is the 00530 ** page number to look up in the pointer map. 00531 ** 00532 ** PTRMAP_PAGENO returns the database page number of the pointer-map 00533 ** page that stores the required pointer. PTRMAP_PTROFFSET returns 00534 ** the offset of the requested map entry. 00535 ** 00536 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, 00537 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be 00538 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements 00539 ** this test. 00540 */ 00541 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) 00542 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1)) 00543 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) 00544 00545 /* 00546 ** The pointer map is a lookup table that identifies the parent page for 00547 ** each child page in the database file. The parent page is the page that 00548 ** contains a pointer to the child. Every page in the database contains 00549 ** 0 or 1 parent pages. (In this context 'database page' refers 00550 ** to any page that is not part of the pointer map itself.) Each pointer map 00551 ** entry consists of a single byte 'type' and a 4 byte parent page number. 00552 ** The PTRMAP_XXX identifiers below are the valid types. 00553 ** 00554 ** The purpose of the pointer map is to facility moving pages from one 00555 ** position in the file to another as part of autovacuum. When a page 00556 ** is moved, the pointer in its parent must be updated to point to the 00557 ** new location. The pointer map is used to locate the parent page quickly. 00558 ** 00559 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not 00560 ** used in this case. 00561 ** 00562 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 00563 ** is not used in this case. 00564 ** 00565 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 00566 ** overflow pages. The page number identifies the page that 00567 ** contains the cell with a pointer to this overflow page. 00568 ** 00569 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of 00570 ** overflow pages. The page-number identifies the previous 00571 ** page in the overflow page list. 00572 ** 00573 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number 00574 ** identifies the parent page in the btree. 00575 */ 00576 #define PTRMAP_ROOTPAGE 1 00577 #define PTRMAP_FREEPAGE 2 00578 #define PTRMAP_OVERFLOW1 3 00579 #define PTRMAP_OVERFLOW2 4 00580 #define PTRMAP_BTREE 5 00581 00582 /* A bunch of assert() statements to check the transaction state variables 00583 ** of handle p (type Btree*) are internally consistent. 00584 */ 00585 #define btreeIntegrity(p) \ 00586 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ 00587 assert( p->pBt->inTransaction>=p->inTrans ); 00588 00589 00590 /* 00591 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine 00592 ** if the database supports auto-vacuum or not. Because it is used 00593 ** within an expression that is an argument to another macro 00594 ** (sqliteMallocRaw), it is not possible to use conditional compilation. 00595 ** So, this macro is defined instead. 00596 */ 00597 #ifndef SQLITE_OMIT_AUTOVACUUM 00598 #define ISAUTOVACUUM (pBt->autoVacuum) 00599 #else 00600 #define ISAUTOVACUUM 0 00601 #endif 00602 00603 00604 /* 00605 ** This structure is passed around through all the sanity checking routines 00606 ** in order to keep track of some global state information. 00607 */ 00608 typedef struct IntegrityCk IntegrityCk; 00609 struct IntegrityCk { 00610 BtShared *pBt; /* The tree being checked out */ 00611 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ 00612 int nPage; /* Number of pages in the database */ 00613 int *anRef; /* Number of times each page is referenced */ 00614 int mxErr; /* Stop accumulating errors when this reaches zero */ 00615 int nErr; /* Number of messages written to zErrMsg so far */ 00616 int mallocFailed; /* A memory allocation error has occurred */ 00617 StrAccum errMsg; /* Accumulate the error message text here */ 00618 }; 00619 00620 /* 00621 ** Read or write a two- and four-byte big-endian integer values. 00622 */ 00623 #define get2byte(x) ((x)[0]<<8 | (x)[1]) 00624 #define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v)) 00625 #define get4byte sqlite3Get4byte 00626 #define put4byte sqlite3Put4byte 00627 00628 /* 00629 ** Internal routines that should be accessed by the btree layer only. 00630 */ 00631 int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int); 00632 int sqlite3BtreeInitPage(MemPage *pPage); 00633 void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*); 00634 void sqlite3BtreeParseCell(MemPage*, int, CellInfo*); 00635 int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur); 00636 void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur); 00637 void sqlite3BtreeReleaseTempCursor(BtCursor *pCur); 00638 void sqlite3BtreeMoveToParent(BtCursor *pCur);
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1