1 /* 2 ** 2004 April 6 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** $Id: btreeInt.h,v 1.4 2007/05/16 17:28:43 danielk1977 Exp $ 13 ** 14 ** This file implements a external (disk-based) database using BTrees. 15 ** For a detailed discussion of BTrees, refer to 16 ** 17 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: 18 ** "Sorting And Searching", pages 473-480. Addison-Wesley 19 ** Publishing Company, Reading, Massachusetts. 20 ** 21 ** The basic idea is that each page of the file contains N database 22 ** entries and N+1 pointers to subpages. 23 ** 24 ** ---------------------------------------------------------------- 25 ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | 26 ** ---------------------------------------------------------------- 27 ** 28 ** All of the keys on the page that Ptr(0) points to have values less 29 ** than Key(0). All of the keys on page Ptr(1) and its subpages have 30 ** values greater than Key(0) and less than Key(1). All of the keys 31 ** on Ptr(N) and its subpages have values greater than Key(N-1). And 32 ** so forth. 33 ** 34 ** Finding a particular key requires reading O(log(M)) pages from the 35 ** disk where M is the number of entries in the tree. 36 ** 37 ** In this implementation, a single file can hold one or more separate 38 ** BTrees. Each BTree is identified by the index of its root page. The 39 ** key and data for any entry are combined to form the "payload". A 40 ** fixed amount of payload can be carried directly on the database 41 ** page. If the payload is larger than the preset amount then surplus 42 ** bytes are stored on overflow pages. The payload for an entry 43 ** and the preceding pointer are combined to form a "Cell". Each 44 ** page has a small header which contains the Ptr(N) pointer and other 45 ** information such as the size of key and data. 46 ** 47 ** FORMAT DETAILS 48 ** 49 ** The file is divided into pages. The first page is called page 1, 50 ** the second is page 2, and so forth. A page number of zero indicates 51 ** "no such page". The page size can be anything between 512 and 65536. 52 ** Each page can be either a btree page, a freelist page or an overflow 53 ** page. 54 ** 55 ** The first page is always a btree page. The first 100 bytes of the first 56 ** page contain a special header (the "file header") that describes the file. 57 ** The format of the file header is as follows: 58 ** 59 ** OFFSET SIZE DESCRIPTION 60 ** 0 16 Header string: "SQLite format 3\000" 61 ** 16 2 Page size in bytes. 62 ** 18 1 File format write version 63 ** 19 1 File format read version 64 ** 20 1 Bytes of unused space at the end of each page 65 ** 21 1 Max embedded payload fraction 66 ** 22 1 Min embedded payload fraction 67 ** 23 1 Min leaf payload fraction 68 ** 24 4 File change counter 69 ** 28 4 Reserved for future use 70 ** 32 4 First freelist page 71 ** 36 4 Number of freelist pages in the file 72 ** 40 60 15 4-byte meta values passed to higher layers 73 ** 74 ** All of the integer values are big-endian (most significant byte first). 75 ** 76 ** The file change counter is incremented when the database is changed more 77 ** than once within the same second. This counter, together with the 78 ** modification time of the file, allows other processes to know 79 ** when the file has changed and thus when they need to flush their 80 ** cache. 81 ** 82 ** The max embedded payload fraction is the amount of the total usable 83 ** space in a page that can be consumed by a single cell for standard 84 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default 85 ** is to limit the maximum cell size so that at least 4 cells will fit 86 ** on one page. Thus the default max embedded payload fraction is 64. 87 ** 88 ** If the payload for a cell is larger than the max payload, then extra 89 ** payload is spilled to overflow pages. Once an overflow page is allocated, 90 ** as many bytes as possible are moved into the overflow pages without letting 91 ** the cell size drop below the min embedded payload fraction. 92 ** 93 ** The min leaf payload fraction is like the min embedded payload fraction 94 ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum 95 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it 96 ** not specified in the header. 97 ** 98 ** Each btree pages is divided into three sections: The header, the 99 ** cell pointer array, and the cell area area. Page 1 also has a 100-byte 100 ** file header that occurs before the page header. 101 ** 102 ** |----------------| 103 ** | file header | 100 bytes. Page 1 only. 104 ** |----------------| 105 ** | page header | 8 bytes for leaves. 12 bytes for interior nodes 106 ** |----------------| 107 ** | cell pointer | | 2 bytes per cell. Sorted order. 108 ** | array | | Grows downward 109 ** | | v 110 ** |----------------| 111 ** | unallocated | 112 ** | space | 113 ** |----------------| ^ Grows upwards 114 ** | cell content | | Arbitrary order interspersed with freeblocks. 115 ** | area | | and free space fragments. 116 ** |----------------| 117 ** 118 ** The page headers looks like this: 119 ** 120 ** OFFSET SIZE DESCRIPTION 121 ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf 122 ** 1 2 byte offset to the first freeblock 123 ** 3 2 number of cells on this page 124 ** 5 2 first byte of the cell content area 125 ** 7 1 number of fragmented free bytes 126 ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. 127 ** 128 ** The flags define the format of this btree page. The leaf flag means that 129 ** this page has no children. The zerodata flag means that this page carries 130 ** only keys and no data. The intkey flag means that the key is a integer 131 ** which is stored in the key size entry of the cell header rather than in 132 ** the payload area. 133 ** 134 ** The cell pointer array begins on the first byte after the page header. 135 ** The cell pointer array contains zero or more 2-byte numbers which are 136 ** offsets from the beginning of the page to the cell content in the cell 137 ** content area. The cell pointers occur in sorted order. The system strives 138 ** to keep free space after the last cell pointer so that new cells can 139 ** be easily added without having to defragment the page. 140 ** 141 ** Cell content is stored at the very end of the page and grows toward the 142 ** beginning of the page. 143 ** 144 ** Unused space within the cell content area is collected into a linked list of 145 ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset 146 ** to the first freeblock is given in the header. Freeblocks occur in 147 ** increasing order. Because a freeblock must be at least 4 bytes in size, 148 ** any group of 3 or fewer unused bytes in the cell content area cannot 149 ** exist on the freeblock chain. A group of 3 or fewer free bytes is called 150 ** a fragment. The total number of bytes in all fragments is recorded. 151 ** in the page header at offset 7. 152 ** 153 ** SIZE DESCRIPTION 154 ** 2 Byte offset of the next freeblock 155 ** 2 Bytes in this freeblock 156 ** 157 ** Cells are of variable length. Cells are stored in the cell content area at 158 ** the end of the page. Pointers to the cells are in the cell pointer array 159 ** that immediately follows the page header. Cells is not necessarily 160 ** contiguous or in order, but cell pointers are contiguous and in order. 161 ** 162 ** Cell content makes use of variable length integers. A variable 163 ** length integer is 1 to 9 bytes where the lower 7 bits of each 164 ** byte are used. The integer consists of all bytes that have bit 8 set and 165 ** the first byte with bit 8 clear. The most significant byte of the integer 166 ** appears first. A variable-length integer may not be more than 9 bytes long. 167 ** As a special case, all 8 bytes of the 9th byte are used as data. This 168 ** allows a 64-bit integer to be encoded in 9 bytes. 169 ** 170 ** 0x00 becomes 0x00000000 171 ** 0x7f becomes 0x0000007f 172 ** 0x81 0x00 becomes 0x00000080 173 ** 0x82 0x00 becomes 0x00000100 174 ** 0x80 0x7f becomes 0x0000007f 175 ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 176 ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 177 ** 178 ** Variable length integers are used for rowids and to hold the number of 179 ** bytes of key and data in a btree cell. 180 ** 181 ** The content of a cell looks like this: 182 ** 183 ** SIZE DESCRIPTION 184 ** 4 Page number of the left child. Omitted if leaf flag is set. 185 ** var Number of bytes of data. Omitted if the zerodata flag is set. 186 ** var Number of bytes of key. Or the key itself if intkey flag is set. 187 ** * Payload 188 ** 4 First page of the overflow chain. Omitted if no overflow 189 ** 190 ** Overflow pages form a linked list. Each page except the last is completely 191 ** filled with data (pagesize - 4 bytes). The last page can have as little 192 ** as 1 byte of data. 193 ** 194 ** SIZE DESCRIPTION 195 ** 4 Page number of next overflow page 196 ** * Data 197 ** 198 ** Freelist pages come in two subtypes: trunk pages and leaf pages. The 199 ** file header points to first in a linked list of trunk page. Each trunk 200 ** page points to multiple leaf pages. The content of a leaf page is 201 ** unspecified. A trunk page looks like this: 202 ** 203 ** SIZE DESCRIPTION 204 ** 4 Page number of next trunk page 205 ** 4 Number of leaf pointers on this page 206 ** * zero or more pages numbers of leaves 207 */ 208 #include "sqliteInt.h" 209 #include "pager.h" 210 #include "btree.h" 211 #include "os.h" 212 #include <assert.h> 213 214 /* Round up a number to the next larger multiple of 8. This is used 215 ** to force 8-byte alignment on 64-bit architectures. 216 */ 217 #define ROUND8(x) ((x+7)&~7) 218 219 220 /* The following value is the maximum cell size assuming a maximum page 221 ** size give above. 222 */ 223 #define MX_CELL_SIZE(pBt) (pBt->pageSize-8) 224 225 /* The maximum number of cells on a single page of the database. This 226 ** assumes a minimum cell size of 3 bytes. Such small cells will be 227 ** exceedingly rare, but they are possible. 228 */ 229 #define MX_CELL(pBt) ((pBt->pageSize-8)/3) 230 231 /* Forward declarations */ 232 typedef struct MemPage MemPage; 233 typedef struct BtLock BtLock; 234 235 /* 236 ** This is a magic string that appears at the beginning of every 237 ** SQLite database in order to identify the file as a real database. 238 ** 239 ** You can change this value at compile-time by specifying a 240 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The 241 ** header must be exactly 16 bytes including the zero-terminator so 242 ** the string itself should be 15 characters long. If you change 243 ** the header, then your custom library will not be able to read 244 ** databases generated by the standard tools and the standard tools 245 ** will not be able to read databases created by your custom library. 246 */ 247 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ 248 # define SQLITE_FILE_HEADER "SQLite format 3" 249 #endif 250 251 /* 252 ** Page type flags. An ORed combination of these flags appear as the 253 ** first byte of every BTree page. 254 */ 255 #define PTF_INTKEY 0x01 256 #define PTF_ZERODATA 0x02 257 #define PTF_LEAFDATA 0x04 258 #define PTF_LEAF 0x08 259 260 /* 261 ** As each page of the file is loaded into memory, an instance of the following 262 ** structure is appended and initialized to zero. This structure stores 263 ** information about the page that is decoded from the raw file page. 264 ** 265 ** The pParent field points back to the parent page. This allows us to 266 ** walk up the BTree from any leaf to the root. Care must be taken to 267 ** unref() the parent page pointer when this page is no longer referenced. 268 ** The pageDestructor() routine handles that chore. 269 */ 270 struct MemPage { 271 u8 isInit; /* True if previously initialized. MUST BE FIRST! */ 272 u8 idxShift; /* True if Cell indices have changed */ 273 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ 274 u8 intKey; /* True if intkey flag is set */ 275 u8 leaf; /* True if leaf flag is set */ 276 u8 zeroData; /* True if table stores keys only */ 277 u8 leafData; /* True if tables stores data on leaves only */ 278 u8 hasData; /* True if this page stores data */ 279 u8 hdrOffset; /* 100 for page 1. 0 otherwise */ 280 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ 281 u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ 282 u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ 283 u16 cellOffset; /* Index in aData of first cell pointer */ 284 u16 idxParent; /* Index in parent of this node */ 285 u16 nFree; /* Number of free bytes on the page */ 286 u16 nCell; /* Number of cells on this page, local and ovfl */ 287 struct _OvflCell { /* Cells that will not fit on aData[] */ 288 u8 *pCell; /* Pointers to the body of the overflow cell */ 289 u16 idx; /* Insert this cell before idx-th non-overflow cell */ 290 } aOvfl[5]; 291 BtShared *pBt; /* Pointer back to BTree structure */ 292 u8 *aData; /* Pointer back to the start of the page */ 293 DbPage *pDbPage; /* Pager page handle */ 294 Pgno pgno; /* Page number for this page */ 295 MemPage *pParent; /* The parent of this page. NULL for root */ 296 }; 297 298 /* 299 ** The in-memory image of a disk page has the auxiliary information appended 300 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold 301 ** that extra information. 302 */ 303 #define EXTRA_SIZE sizeof(MemPage) 304 305 /* Btree handle */ 306 struct Btree { 307 sqlite3 *pSqlite; 308 BtShared *pBt; 309 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ 310 }; 311 312 /* 313 ** Btree.inTrans may take one of the following values. 314 ** 315 ** If the shared-data extension is enabled, there may be multiple users 316 ** of the Btree structure. At most one of these may open a write transaction, 317 ** but any number may have active read transactions. Variable Btree.pDb 318 ** points to the handle that owns any current write-transaction. 319 */ 320 #define TRANS_NONE 0 321 #define TRANS_READ 1 322 #define TRANS_WRITE 2 323 324 /* 325 ** Everything we need to know about an open database 326 */ 327 struct BtShared { 328 Pager *pPager; /* The page cache */ 329 BtCursor *pCursor; /* A list of all open cursors */ 330 MemPage *pPage1; /* First page of the database */ 331 u8 inStmt; /* True if we are in a statement subtransaction */ 332 u8 readOnly; /* True if the underlying file is readonly */ 333 u8 maxEmbedFrac; /* Maximum payload as % of total page size */ 334 u8 minEmbedFrac; /* Minimum payload as % of total page size */ 335 u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ 336 u8 pageSizeFixed; /* True if the page size can no longer be changed */ 337 #ifndef SQLITE_OMIT_AUTOVACUUM 338 u8 autoVacuum; /* True if auto-vacuum is enabled */ 339 u8 incrVacuum; /* True if incr-vacuum is enabled */ 340 Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */ 341 #endif 342 u16 pageSize; /* Total number of bytes on a page */ 343 u16 usableSize; /* Number of usable bytes on each page */ 344 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ 345 int minLocal; /* Minimum local payload in non-LEAFDATA tables */ 346 int maxLeaf; /* Maximum local payload in a LEAFDATA table */ 347 int minLeaf; /* Minimum local payload in a LEAFDATA table */ 348 BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ 349 u8 inTransaction; /* Transaction state */ 350 int nRef; /* Number of references to this structure */ 351 int nTransaction; /* Number of open transactions (read + write) */ 352 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ 353 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ 354 #ifndef SQLITE_OMIT_SHARED_CACHE 355 BtLock *pLock; /* List of locks held on this shared-btree struct */ 356 BtShared *pNext; /* Next in ThreadData.pBtree linked list */ 357 #endif 358 }; 359 360 /* 361 ** An instance of the following structure is used to hold information 362 ** about a cell. The parseCellPtr() function fills in this structure 363 ** based on information extract from the raw disk page. 364 */ 365 typedef struct CellInfo CellInfo; 366 struct CellInfo { 367 u8 *pCell; /* Pointer to the start of cell content */ 368 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ 369 u32 nData; /* Number of bytes of data */ 370 u32 nPayload; /* Total amount of payload */ 371 u16 nHeader; /* Size of the cell content header in bytes */ 372 u16 nLocal; /* Amount of payload held locally */ 373 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ 374 u16 nSize; /* Size of the cell content on the main b-tree page */ 375 }; 376 377 /* 378 ** A cursor is a pointer to a particular entry in the BTree. 379 ** The entry is identified by its MemPage and the index in 380 ** MemPage.aCell[] of the entry. 381 */ 382 struct BtCursor { 383 Btree *pBtree; /* The Btree to which this cursor belongs */ 384 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ 385 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ 386 void *pArg; /* First arg to xCompare() */ 387 Pgno pgnoRoot; /* The root page of this tree */ 388 MemPage *pPage; /* Page that contains the entry */ 389 int idx; /* Index of the entry in pPage->aCell[] */ 390 CellInfo info; /* A parse of the cell we are pointing at */ 391 u8 wrFlag; /* True if writable */ 392 u8 eState; /* One of the CURSOR_XXX constants (see below) */ 393 void *pKey; /* Saved key that was cursor's last known position */ 394 i64 nKey; /* Size of pKey, or last integer key */ 395 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ 396 #ifndef SQLITE_OMIT_INCRBLOB 397 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */ 398 Pgno *aOverflow; /* Cache of overflow page locations */ 399 #endif 400 }; 401 402 /* 403 ** Potential values for BtCursor.eState. 404 ** 405 ** CURSOR_VALID: 406 ** Cursor points to a valid entry. getPayload() etc. may be called. 407 ** 408 ** CURSOR_INVALID: 409 ** Cursor does not point to a valid entry. This can happen (for example) 410 ** because the table is empty or because BtreeCursorFirst() has not been 411 ** called. 412 ** 413 ** CURSOR_REQUIRESEEK: 414 ** The table that this cursor was opened on still exists, but has been 415 ** modified since the cursor was last used. The cursor position is saved 416 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in 417 ** this state, restoreOrClearCursorPosition() can be called to attempt to 418 ** seek the cursor to the saved position. 419 */ 420 #define CURSOR_INVALID 0 421 #define CURSOR_VALID 1 422 #define CURSOR_REQUIRESEEK 2 423 424 /* 425 ** The TRACE macro will print high-level status information about the 426 ** btree operation when the global variable sqlite3_btree_trace is 427 ** enabled. 428 */ 429 #if SQLITE_TEST 430 # define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); } 431 #else 432 # define TRACE(X) 433 #endif 434 435 /* 436 ** Routines to read and write variable-length integers. These used to 437 ** be defined locally, but now we use the varint routines in the util.c 438 ** file. 439 */ 440 #define getVarint sqlite3GetVarint 441 #define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B)) 442 #define putVarint sqlite3PutVarint 443 444 /* The database page the PENDING_BYTE occupies. This page is never used. 445 ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They 446 ** should possibly be consolidated (presumably in pager.h). 447 ** 448 ** If disk I/O is omitted (meaning that the database is stored purely 449 ** in memory) then there is no pending byte. 450 */ 451 #ifdef SQLITE_OMIT_DISKIO 452 # define PENDING_BYTE_PAGE(pBt) 0x7fffffff 453 #else 454 # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) 455 #endif 456 457 /* 458 ** A linked list of the following structures is stored at BtShared.pLock. 459 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 460 ** is opened on the table with root page BtShared.iTable. Locks are removed 461 ** from this list when a transaction is committed or rolled back, or when 462 ** a btree handle is closed. 463 */ 464 struct BtLock { 465 Btree *pBtree; /* Btree handle holding this lock */ 466 Pgno iTable; /* Root page of table */ 467 u8 eLock; /* READ_LOCK or WRITE_LOCK */ 468 BtLock *pNext; /* Next in BtShared.pLock list */ 469 }; 470 471 /* Candidate values for BtLock.eLock */ 472 #define READ_LOCK 1 473 #define WRITE_LOCK 2 474 475 /* 476 ** These macros define the location of the pointer-map entry for a 477 ** database page. The first argument to each is the number of usable 478 ** bytes on each page of the database (often 1024). The second is the 479 ** page number to look up in the pointer map. 480 ** 481 ** PTRMAP_PAGENO returns the database page number of the pointer-map 482 ** page that stores the required pointer. PTRMAP_PTROFFSET returns 483 ** the offset of the requested map entry. 484 ** 485 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, 486 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be 487 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements 488 ** this test. 489 */ 490 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) 491 #define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1)) 492 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) 493 494 /* 495 ** The pointer map is a lookup table that identifies the parent page for 496 ** each child page in the database file. The parent page is the page that 497 ** contains a pointer to the child. Every page in the database contains 498 ** 0 or 1 parent pages. (In this context 'database page' refers 499 ** to any page that is not part of the pointer map itself.) Each pointer map 500 ** entry consists of a single byte 'type' and a 4 byte parent page number. 501 ** The PTRMAP_XXX identifiers below are the valid types. 502 ** 503 ** The purpose of the pointer map is to facility moving pages from one 504 ** position in the file to another as part of autovacuum. When a page 505 ** is moved, the pointer in its parent must be updated to point to the 506 ** new location. The pointer map is used to locate the parent page quickly. 507 ** 508 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not 509 ** used in this case. 510 ** 511 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number 512 ** is not used in this case. 513 ** 514 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of 515 ** overflow pages. The page number identifies the page that 516 ** contains the cell with a pointer to this overflow page. 517 ** 518 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of 519 ** overflow pages. The page-number identifies the previous 520 ** page in the overflow page list. 521 ** 522 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number 523 ** identifies the parent page in the btree. 524 */ 525 #define PTRMAP_ROOTPAGE 1 526 #define PTRMAP_FREEPAGE 2 527 #define PTRMAP_OVERFLOW1 3 528 #define PTRMAP_OVERFLOW2 4 529 #define PTRMAP_BTREE 5 530 531 /* A bunch of assert() statements to check the transaction state variables 532 ** of handle p (type Btree*) are internally consistent. 533 */ 534 #define btreeIntegrity(p) \ 535 assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \ 536 assert( p->pBt->nTransaction<=p->pBt->nRef ); \ 537 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ 538 assert( p->pBt->inTransaction>=p->inTrans ); 539 540 541 /* 542 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine 543 ** if the database supports auto-vacuum or not. Because it is used 544 ** within an expression that is an argument to another macro 545 ** (sqliteMallocRaw), it is not possible to use conditional compilation. 546 ** So, this macro is defined instead. 547 */ 548 #ifndef SQLITE_OMIT_AUTOVACUUM 549 #define ISAUTOVACUUM (pBt->autoVacuum) 550 #else 551 #define ISAUTOVACUUM 0 552 #endif 553 554 555 /* 556 ** This structure is passed around through all the sanity checking routines 557 ** in order to keep track of some global state information. 558 */ 559 typedef struct IntegrityCk IntegrityCk; 560 struct IntegrityCk { 561 BtShared *pBt; /* The tree being checked out */ 562 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ 563 int nPage; /* Number of pages in the database */ 564 int *anRef; /* Number of times each page is referenced */ 565 int mxErr; /* Stop accumulating errors when this reaches zero */ 566 char *zErrMsg; /* An error message. NULL if no errors seen. */ 567 int nErr; /* Number of messages written to zErrMsg so far */ 568 }; 569 570 /* 571 ** Read or write a two- and four-byte big-endian integer values. 572 */ 573 #define get2byte(x) ((x)[0]<<8 | (x)[1]) 574 #define put2byte(p,v) ((p)[0] = (v)>>8, (p)[1] = (v)) 575 #define get4byte sqlite3Get4byte 576 #define put4byte sqlite3Put4byte 577 578 /* 579 ** Internal routines that should be accessed by the btree layer only. 580 */ 581 int sqlite3BtreeGetPage(BtShared*, Pgno, MemPage**, int); 582 int sqlite3BtreeInitPage(MemPage *pPage, MemPage *pParent); 583 void sqlite3BtreeParseCellPtr(MemPage*, u8*, CellInfo*); 584 void sqlite3BtreeParseCell(MemPage*, int, CellInfo*); 585 u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell); 586 int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur); 587 void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur); 588 void sqlite3BtreeReleaseTempCursor(BtCursor *pCur); 589 int sqlite3BtreeIsRootPage(MemPage *pPage); 590 void sqlite3BtreeMoveToParent(BtCursor *pCur); 591