| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2004 April 6 | 2 ** 2004 April 6 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file implements a external (disk-based) database using BTrees. | 12 ** This file implements an external (disk-based) database using BTrees. |
| 13 ** For a detailed discussion of BTrees, refer to | 13 ** For a detailed discussion of BTrees, refer to |
| 14 ** | 14 ** |
| 15 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: | 15 ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 16 ** "Sorting And Searching", pages 473-480. Addison-Wesley | 16 ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 17 ** Publishing Company, Reading, Massachusetts. | 17 ** Publishing Company, Reading, Massachusetts. |
| 18 ** | 18 ** |
| 19 ** The basic idea is that each page of the file contains N database | 19 ** The basic idea is that each page of the file contains N database |
| 20 ** entries and N+1 pointers to subpages. | 20 ** entries and N+1 pointers to subpages. |
| 21 ** | 21 ** |
| 22 ** ---------------------------------------------------------------- | 22 ** ---------------------------------------------------------------- |
| (...skipping 26 matching lines...) Expand all Loading... |
| 49 ** "no such page". The page size can be any power of 2 between 512 and 65536. | 49 ** "no such page". The page size can be any power of 2 between 512 and 65536. |
| 50 ** Each page can be either a btree page, a freelist page, an overflow | 50 ** Each page can be either a btree page, a freelist page, an overflow |
| 51 ** page, or a pointer-map page. | 51 ** page, or a pointer-map page. |
| 52 ** | 52 ** |
| 53 ** The first page is always a btree page. The first 100 bytes of the first | 53 ** The first page is always a btree page. The first 100 bytes of the first |
| 54 ** page contain a special header (the "file header") that describes the file. | 54 ** page contain a special header (the "file header") that describes the file. |
| 55 ** The format of the file header is as follows: | 55 ** The format of the file header is as follows: |
| 56 ** | 56 ** |
| 57 ** OFFSET SIZE DESCRIPTION | 57 ** OFFSET SIZE DESCRIPTION |
| 58 ** 0 16 Header string: "SQLite format 3\000" | 58 ** 0 16 Header string: "SQLite format 3\000" |
| 59 ** 16 2 Page size in bytes. | 59 ** 16 2 Page size in bytes. (1 means 65536) |
| 60 ** 18 1 File format write version | 60 ** 18 1 File format write version |
| 61 ** 19 1 File format read version | 61 ** 19 1 File format read version |
| 62 ** 20 1 Bytes of unused space at the end of each page | 62 ** 20 1 Bytes of unused space at the end of each page |
| 63 ** 21 1 Max embedded payload fraction | 63 ** 21 1 Max embedded payload fraction (must be 64) |
| 64 ** 22 1 Min embedded payload fraction | 64 ** 22 1 Min embedded payload fraction (must be 32) |
| 65 ** 23 1 Min leaf payload fraction | 65 ** 23 1 Min leaf payload fraction (must be 32) |
| 66 ** 24 4 File change counter | 66 ** 24 4 File change counter |
| 67 ** 28 4 Reserved for future use | 67 ** 28 4 Reserved for future use |
| 68 ** 32 4 First freelist page | 68 ** 32 4 First freelist page |
| 69 ** 36 4 Number of freelist pages in the file | 69 ** 36 4 Number of freelist pages in the file |
| 70 ** 40 60 15 4-byte meta values passed to higher layers | 70 ** 40 60 15 4-byte meta values passed to higher layers |
| 71 ** | 71 ** |
| 72 ** 40 4 Schema cookie | 72 ** 40 4 Schema cookie |
| 73 ** 44 4 File format of schema layer | 73 ** 44 4 File format of schema layer |
| 74 ** 48 4 Size of page cache | 74 ** 48 4 Size of page cache |
| 75 ** 52 4 Largest root-page (auto/incr_vacuum) | 75 ** 52 4 Largest root-page (auto/incr_vacuum) |
| 76 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be | 76 ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be |
| 77 ** 60 4 User version | 77 ** 60 4 User version |
| 78 ** 64 4 Incremental vacuum mode | 78 ** 64 4 Incremental vacuum mode |
| 79 ** 68 4 unused | 79 ** 68 4 Application-ID |
| 80 ** 72 4 unused | 80 ** 72 20 unused |
| 81 ** 76 4 unused | 81 ** 92 4 The version-valid-for number |
| 82 ** 96 4 SQLITE_VERSION_NUMBER |
| 82 ** | 83 ** |
| 83 ** All of the integer values are big-endian (most significant byte first). | 84 ** All of the integer values are big-endian (most significant byte first). |
| 84 ** | 85 ** |
| 85 ** The file change counter is incremented when the database is changed | 86 ** The file change counter is incremented when the database is changed |
| 86 ** This counter allows other processes to know when the file has changed | 87 ** This counter allows other processes to know when the file has changed |
| 87 ** and thus when they need to flush their cache. | 88 ** and thus when they need to flush their cache. |
| 88 ** | 89 ** |
| 89 ** The max embedded payload fraction is the amount of the total usable | 90 ** The max embedded payload fraction is the amount of the total usable |
| 90 ** space in a page that can be consumed by a single cell for standard | 91 ** space in a page that can be consumed by a single cell for standard |
| 91 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default | 92 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 ** OFFSET SIZE DESCRIPTION | 128 ** OFFSET SIZE DESCRIPTION |
| 128 ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf | 129 ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
| 129 ** 1 2 byte offset to the first freeblock | 130 ** 1 2 byte offset to the first freeblock |
| 130 ** 3 2 number of cells on this page | 131 ** 3 2 number of cells on this page |
| 131 ** 5 2 first byte of the cell content area | 132 ** 5 2 first byte of the cell content area |
| 132 ** 7 1 number of fragmented free bytes | 133 ** 7 1 number of fragmented free bytes |
| 133 ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. | 134 ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. |
| 134 ** | 135 ** |
| 135 ** The flags define the format of this btree page. The leaf flag means that | 136 ** The flags define the format of this btree page. The leaf flag means that |
| 136 ** this page has no children. The zerodata flag means that this page carries | 137 ** this page has no children. The zerodata flag means that this page carries |
| 137 ** only keys and no data. The intkey flag means that the key is a integer | 138 ** only keys and no data. The intkey flag means that the key is an integer |
| 138 ** which is stored in the key size entry of the cell header rather than in | 139 ** which is stored in the key size entry of the cell header rather than in |
| 139 ** the payload area. | 140 ** the payload area. |
| 140 ** | 141 ** |
| 141 ** The cell pointer array begins on the first byte after the page header. | 142 ** The cell pointer array begins on the first byte after the page header. |
| 142 ** The cell pointer array contains zero or more 2-byte numbers which are | 143 ** The cell pointer array contains zero or more 2-byte numbers which are |
| 143 ** offsets from the beginning of the page to the cell content in the cell | 144 ** offsets from the beginning of the page to the cell content in the cell |
| 144 ** content area. The cell pointers occur in sorted order. The system strives | 145 ** content area. The cell pointers occur in sorted order. The system strives |
| 145 ** to keep free space after the last cell pointer so that new cells can | 146 ** to keep free space after the last cell pointer so that new cells can |
| 146 ** be easily added without having to defragment the page. | 147 ** be easily added without having to defragment the page. |
| 147 ** | 148 ** |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 ** walk up the BTree from any leaf to the root. Care must be taken to | 266 ** walk up the BTree from any leaf to the root. Care must be taken to |
| 266 ** unref() the parent page pointer when this page is no longer referenced. | 267 ** unref() the parent page pointer when this page is no longer referenced. |
| 267 ** The pageDestructor() routine handles that chore. | 268 ** The pageDestructor() routine handles that chore. |
| 268 ** | 269 ** |
| 269 ** Access to all fields of this structure is controlled by the mutex | 270 ** Access to all fields of this structure is controlled by the mutex |
| 270 ** stored in MemPage.pBt->mutex. | 271 ** stored in MemPage.pBt->mutex. |
| 271 */ | 272 */ |
| 272 struct MemPage { | 273 struct MemPage { |
| 273 u8 isInit; /* True if previously initialized. MUST BE FIRST! */ | 274 u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
| 274 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ | 275 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 275 u8 intKey; /* True if intkey flag is set */ | 276 u8 intKey; /* True if table b-trees. False for index b-trees */ |
| 276 u8 leaf; /* True if leaf flag is set */ | 277 u8 intKeyLeaf; /* True if the leaf of an intKey table */ |
| 277 u8 hasData; /* True if this page stores data */ | 278 u8 noPayload; /* True if internal intKey page (thus w/o data) */ |
| 279 u8 leaf; /* True if a leaf page */ |
| 278 u8 hdrOffset; /* 100 for page 1. 0 otherwise */ | 280 u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
| 279 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ | 281 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
| 282 u8 max1bytePayload; /* min(maxLocal,127) */ |
| 280 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ | 283 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ |
| 281 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ | 284 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ |
| 282 u16 cellOffset; /* Index in aData of first cell pointer */ | 285 u16 cellOffset; /* Index in aData of first cell pointer */ |
| 283 u16 nFree; /* Number of free bytes on the page */ | 286 u16 nFree; /* Number of free bytes on the page */ |
| 284 u16 nCell; /* Number of cells on this page, local and ovfl */ | 287 u16 nCell; /* Number of cells on this page, local and ovfl */ |
| 285 u16 maskPage; /* Mask for page offset */ | 288 u16 maskPage; /* Mask for page offset */ |
| 286 struct _OvflCell { /* Cells that will not fit on aData[] */ | 289 u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th |
| 287 u8 *pCell; /* Pointers to the body of the overflow cell */ | 290 ** non-overflow cell */ |
| 288 u16 idx; /* Insert this cell before idx-th non-overflow cell */ | 291 u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ |
| 289 } aOvfl[5]; | |
| 290 BtShared *pBt; /* Pointer to BtShared that this page is part of */ | 292 BtShared *pBt; /* Pointer to BtShared that this page is part of */ |
| 291 u8 *aData; /* Pointer to disk image of the page data */ | 293 u8 *aData; /* Pointer to disk image of the page data */ |
| 294 u8 *aDataEnd; /* One byte past the end of usable data */ |
| 295 u8 *aCellIdx; /* The cell index area */ |
| 292 DbPage *pDbPage; /* Pager page handle */ | 296 DbPage *pDbPage; /* Pager page handle */ |
| 293 Pgno pgno; /* Page number for this page */ | 297 Pgno pgno; /* Page number for this page */ |
| 294 }; | 298 }; |
| 295 | 299 |
| 296 /* | 300 /* |
| 297 ** The in-memory image of a disk page has the auxiliary information appended | 301 ** The in-memory image of a disk page has the auxiliary information appended |
| 298 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold | 302 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 299 ** that extra information. | 303 ** that extra information. |
| 300 */ | 304 */ |
| 301 #define EXTRA_SIZE sizeof(MemPage) | 305 #define EXTRA_SIZE sizeof(MemPage) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 ** of the Btree structure. At most one of these may open a write transaction, | 365 ** of the Btree structure. At most one of these may open a write transaction, |
| 362 ** but any number may have active read transactions. | 366 ** but any number may have active read transactions. |
| 363 */ | 367 */ |
| 364 #define TRANS_NONE 0 | 368 #define TRANS_NONE 0 |
| 365 #define TRANS_READ 1 | 369 #define TRANS_READ 1 |
| 366 #define TRANS_WRITE 2 | 370 #define TRANS_WRITE 2 |
| 367 | 371 |
| 368 /* | 372 /* |
| 369 ** An instance of this object represents a single database file. | 373 ** An instance of this object represents a single database file. |
| 370 ** | 374 ** |
| 371 ** A single database file can be in use as the same time by two | 375 ** A single database file can be in use at the same time by two |
| 372 ** or more database connections. When two or more connections are | 376 ** or more database connections. When two or more connections are |
| 373 ** sharing the same database file, each connection has it own | 377 ** sharing the same database file, each connection has it own |
| 374 ** private Btree object for the file and each of those Btrees points | 378 ** private Btree object for the file and each of those Btrees points |
| 375 ** to this one BtShared object. BtShared.nRef is the number of | 379 ** to this one BtShared object. BtShared.nRef is the number of |
| 376 ** connections currently sharing this database file. | 380 ** connections currently sharing this database file. |
| 377 ** | 381 ** |
| 378 ** Fields in this structure are accessed under the BtShared.mutex | 382 ** Fields in this structure are accessed under the BtShared.mutex |
| 379 ** mutex, except for nRef and pNext which are accessed under the | 383 ** mutex, except for nRef and pNext which are accessed under the |
| 380 ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field | 384 ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field |
| 381 ** may not be modified once it is initially set as long as nRef>0. | 385 ** may not be modified once it is initially set as long as nRef>0. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 398 ** while in the 'pending-lock' state, no connection may start a new | 402 ** while in the 'pending-lock' state, no connection may start a new |
| 399 ** transaction. | 403 ** transaction. |
| 400 ** | 404 ** |
| 401 ** This feature is included to help prevent writer-starvation. | 405 ** This feature is included to help prevent writer-starvation. |
| 402 */ | 406 */ |
| 403 struct BtShared { | 407 struct BtShared { |
| 404 Pager *pPager; /* The page cache */ | 408 Pager *pPager; /* The page cache */ |
| 405 sqlite3 *db; /* Database connection currently using this Btree */ | 409 sqlite3 *db; /* Database connection currently using this Btree */ |
| 406 BtCursor *pCursor; /* A list of all open cursors */ | 410 BtCursor *pCursor; /* A list of all open cursors */ |
| 407 MemPage *pPage1; /* First page of the database */ | 411 MemPage *pPage1; /* First page of the database */ |
| 408 u8 readOnly; /* True if the underlying file is readonly */ | |
| 409 u8 pageSizeFixed; /* True if the page size can no longer be changed */ | |
| 410 u8 secureDelete; /* True if secure_delete is enabled */ | |
| 411 u8 initiallyEmpty; /* Database is empty at start of transaction */ | |
| 412 u8 openFlags; /* Flags to sqlite3BtreeOpen() */ | 412 u8 openFlags; /* Flags to sqlite3BtreeOpen() */ |
| 413 #ifndef SQLITE_OMIT_AUTOVACUUM | 413 #ifndef SQLITE_OMIT_AUTOVACUUM |
| 414 u8 autoVacuum; /* True if auto-vacuum is enabled */ | 414 u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 415 u8 incrVacuum; /* True if incr-vacuum is enabled */ | 415 u8 incrVacuum; /* True if incr-vacuum is enabled */ |
| 416 u8 bDoTruncate; /* True to truncate db on commit */ |
| 416 #endif | 417 #endif |
| 417 u8 inTransaction; /* Transaction state */ | 418 u8 inTransaction; /* Transaction state */ |
| 418 u8 doNotUseWAL; /* If true, do not open write-ahead-log file */ | 419 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ |
| 420 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
| 419 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ | 421 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 420 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ | 422 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 421 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ | 423 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 422 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ | 424 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 423 u32 pageSize; /* Total number of bytes on a page */ | 425 u32 pageSize; /* Total number of bytes on a page */ |
| 424 u32 usableSize; /* Number of usable bytes on each page */ | 426 u32 usableSize; /* Number of usable bytes on each page */ |
| 425 int nTransaction; /* Number of open transactions (read + write) */ | 427 int nTransaction; /* Number of open transactions (read + write) */ |
| 426 u32 nPage; /* Number of pages in the database */ | 428 u32 nPage; /* Number of pages in the database */ |
| 427 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ | 429 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 428 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ | 430 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 429 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */ | 431 sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */ |
| 430 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ | 432 Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ |
| 431 #ifndef SQLITE_OMIT_SHARED_CACHE | 433 #ifndef SQLITE_OMIT_SHARED_CACHE |
| 432 int nRef; /* Number of references to this structure */ | 434 int nRef; /* Number of references to this structure */ |
| 433 BtShared *pNext; /* Next on a list of sharable BtShared structs */ | 435 BtShared *pNext; /* Next on a list of sharable BtShared structs */ |
| 434 BtLock *pLock; /* List of locks held on this shared-btree struct */ | 436 BtLock *pLock; /* List of locks held on this shared-btree struct */ |
| 435 Btree *pWriter; /* Btree with currently open write transaction */ | 437 Btree *pWriter; /* Btree with currently open write transaction */ |
| 436 u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */ | |
| 437 u8 isPending; /* If waiting for read-locks to clear */ | |
| 438 #endif | 438 #endif |
| 439 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */ | 439 u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ |
| 440 }; | 440 }; |
| 441 | 441 |
| 442 /* | 442 /* |
| 443 ** Allowed values for BtShared.btsFlags |
| 444 */ |
| 445 #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ |
| 446 #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ |
| 447 #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ |
| 448 #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ |
| 449 #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ |
| 450 #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ |
| 451 #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ |
| 452 |
| 453 /* |
| 443 ** An instance of the following structure is used to hold information | 454 ** An instance of the following structure is used to hold information |
| 444 ** about a cell. The parseCellPtr() function fills in this structure | 455 ** about a cell. The parseCellPtr() function fills in this structure |
| 445 ** based on information extract from the raw disk page. | 456 ** based on information extract from the raw disk page. |
| 446 */ | 457 */ |
| 447 typedef struct CellInfo CellInfo; | 458 typedef struct CellInfo CellInfo; |
| 448 struct CellInfo { | 459 struct CellInfo { |
| 449 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ | 460 i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ |
| 450 u8 *pCell; /* Pointer to the start of cell content */ | 461 u8 *pPayload; /* Pointer to the start of payload */ |
| 451 u32 nData; /* Number of bytes of data */ | 462 u32 nPayload; /* Bytes of payload */ |
| 452 u32 nPayload; /* Total amount of payload */ | 463 u16 nLocal; /* Amount of payload held locally, not on overflow */ |
| 453 u16 nHeader; /* Size of the cell content header in bytes */ | |
| 454 u16 nLocal; /* Amount of payload held locally */ | |
| 455 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ | 464 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
| 456 u16 nSize; /* Size of the cell content on the main b-tree page */ | 465 u16 nSize; /* Size of the cell content on the main b-tree page */ |
| 457 }; | 466 }; |
| 458 | 467 |
| 459 /* | 468 /* |
| 460 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than | 469 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than |
| 461 ** this will be declared corrupt. This value is calculated based on a | 470 ** this will be declared corrupt. This value is calculated based on a |
| 462 ** maximum database size of 2^31 pages a minimum fanout of 2 for a | 471 ** maximum database size of 2^31 pages a minimum fanout of 2 for a |
| 463 ** root-node and 3 for all other internal nodes. | 472 ** root-node and 3 for all other internal nodes. |
| 464 ** | 473 ** |
| 465 ** If a tree that appears to be taller than this is encountered, it is | 474 ** If a tree that appears to be taller than this is encountered, it is |
| 466 ** assumed that the database is corrupt. | 475 ** assumed that the database is corrupt. |
| 467 */ | 476 */ |
| 468 #define BTCURSOR_MAX_DEPTH 20 | 477 #define BTCURSOR_MAX_DEPTH 20 |
| 469 | 478 |
| 470 /* | 479 /* |
| 471 ** A cursor is a pointer to a particular entry within a particular | 480 ** A cursor is a pointer to a particular entry within a particular |
| 472 ** b-tree within a database file. | 481 ** b-tree within a database file. |
| 473 ** | 482 ** |
| 474 ** The entry is identified by its MemPage and the index in | 483 ** The entry is identified by its MemPage and the index in |
| 475 ** MemPage.aCell[] of the entry. | 484 ** MemPage.aCell[] of the entry. |
| 476 ** | 485 ** |
| 477 ** A single database file can shared by two more database connections, | 486 ** A single database file can be shared by two more database connections, |
| 478 ** but cursors cannot be shared. Each cursor is associated with a | 487 ** but cursors cannot be shared. Each cursor is associated with a |
| 479 ** particular database connection identified BtCursor.pBtree.db. | 488 ** particular database connection identified BtCursor.pBtree.db. |
| 480 ** | 489 ** |
| 481 ** Fields in this structure are accessed under the BtShared.mutex | 490 ** Fields in this structure are accessed under the BtShared.mutex |
| 482 ** found at self->pBt->mutex. | 491 ** found at self->pBt->mutex. |
| 492 ** |
| 493 ** skipNext meaning: |
| 494 ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. |
| 495 ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. |
| 496 ** eState==FAULT: Cursor fault with skipNext as error code. |
| 483 */ | 497 */ |
| 484 struct BtCursor { | 498 struct BtCursor { |
| 485 Btree *pBtree; /* The Btree to which this cursor belongs */ | 499 Btree *pBtree; /* The Btree to which this cursor belongs */ |
| 486 BtShared *pBt; /* The BtShared this cursor points to */ | 500 BtShared *pBt; /* The BtShared this cursor points to */ |
| 487 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ | 501 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
| 488 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ | 502 struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ |
| 503 Pgno *aOverflow; /* Cache of overflow page locations */ |
| 504 CellInfo info; /* A parse of the cell we are pointing at */ |
| 505 i64 nKey; /* Size of pKey, or last integer key */ |
| 506 void *pKey; /* Saved key that was cursor last known position */ |
| 489 Pgno pgnoRoot; /* The root page of this tree */ | 507 Pgno pgnoRoot; /* The root page of this tree */ |
| 490 sqlite3_int64 cachedRowid; /* Next rowid cache. 0 means not valid */ | 508 int nOvflAlloc; /* Allocated size of aOverflow[] array */ |
| 491 CellInfo info; /* A parse of the cell we are pointing at */ | 509 int skipNext; /* Prev() is noop if negative. Next() is noop if positive. |
| 492 i64 nKey; /* Size of pKey, or last integer key */ | 510 ** Error code if eState==CURSOR_FAULT */ |
| 493 void *pKey; /* Saved key that was cursor's last known position */ | 511 u8 curFlags; /* zero or more BTCF_* flags defined below */ |
| 494 int skipNext; /* Prev() is noop if negative. Next() is noop if positive */ | |
| 495 u8 wrFlag; /* True if writable */ | |
| 496 u8 atLast; /* Cursor pointing to the last entry */ | |
| 497 u8 validNKey; /* True if info.nKey is valid */ | |
| 498 u8 eState; /* One of the CURSOR_XXX constants (see below) */ | 512 u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
| 499 #ifndef SQLITE_OMIT_INCRBLOB | 513 u8 hints; /* As configured by CursorSetHints() */ |
| 500 Pgno *aOverflow; /* Cache of overflow page locations */ | |
| 501 u8 isIncrblobHandle; /* True if this cursor is an incr. io handle */ | |
| 502 #endif | |
| 503 i16 iPage; /* Index of current page in apPage */ | 514 i16 iPage; /* Index of current page in apPage */ |
| 504 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ | 515 u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ |
| 505 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ | 516 MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ |
| 506 }; | 517 }; |
| 507 | 518 |
| 508 /* | 519 /* |
| 520 ** Legal values for BtCursor.curFlags |
| 521 */ |
| 522 #define BTCF_WriteFlag 0x01 /* True if a write cursor */ |
| 523 #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ |
| 524 #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ |
| 525 #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ |
| 526 #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ |
| 527 |
| 528 /* |
| 509 ** Potential values for BtCursor.eState. | 529 ** Potential values for BtCursor.eState. |
| 510 ** | 530 ** |
| 511 ** CURSOR_VALID: | |
| 512 ** Cursor points to a valid entry. getPayload() etc. may be called. | |
| 513 ** | |
| 514 ** CURSOR_INVALID: | 531 ** CURSOR_INVALID: |
| 515 ** Cursor does not point to a valid entry. This can happen (for example) | 532 ** Cursor does not point to a valid entry. This can happen (for example) |
| 516 ** because the table is empty or because BtreeCursorFirst() has not been | 533 ** because the table is empty or because BtreeCursorFirst() has not been |
| 517 ** called. | 534 ** called. |
| 518 ** | 535 ** |
| 536 ** CURSOR_VALID: |
| 537 ** Cursor points to a valid entry. getPayload() etc. may be called. |
| 538 ** |
| 539 ** CURSOR_SKIPNEXT: |
| 540 ** Cursor is valid except that the Cursor.skipNext field is non-zero |
| 541 ** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious() |
| 542 ** operation should be a no-op. |
| 543 ** |
| 519 ** CURSOR_REQUIRESEEK: | 544 ** CURSOR_REQUIRESEEK: |
| 520 ** The table that this cursor was opened on still exists, but has been | 545 ** The table that this cursor was opened on still exists, but has been |
| 521 ** modified since the cursor was last used. The cursor position is saved | 546 ** modified since the cursor was last used. The cursor position is saved |
| 522 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in | 547 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in |
| 523 ** this state, restoreCursorPosition() can be called to attempt to | 548 ** this state, restoreCursorPosition() can be called to attempt to |
| 524 ** seek the cursor to the saved position. | 549 ** seek the cursor to the saved position. |
| 525 ** | 550 ** |
| 526 ** CURSOR_FAULT: | 551 ** CURSOR_FAULT: |
| 527 ** A unrecoverable error (an I/O error or a malloc failure) has occurred | 552 ** An unrecoverable error (an I/O error or a malloc failure) has occurred |
| 528 ** on a different connection that shares the BtShared cache with this | 553 ** on a different connection that shares the BtShared cache with this |
| 529 ** cursor. The error has left the cache in an inconsistent state. | 554 ** cursor. The error has left the cache in an inconsistent state. |
| 530 ** Do nothing else with this cursor. Any attempt to use the cursor | 555 ** Do nothing else with this cursor. Any attempt to use the cursor |
| 531 ** should return the error code stored in BtCursor.skip | 556 ** should return the error code stored in BtCursor.skipNext |
| 532 */ | 557 */ |
| 533 #define CURSOR_INVALID 0 | 558 #define CURSOR_INVALID 0 |
| 534 #define CURSOR_VALID 1 | 559 #define CURSOR_VALID 1 |
| 535 #define CURSOR_REQUIRESEEK 2 | 560 #define CURSOR_SKIPNEXT 2 |
| 536 #define CURSOR_FAULT 3 | 561 #define CURSOR_REQUIRESEEK 3 |
| 562 #define CURSOR_FAULT 4 |
| 537 | 563 |
| 538 /* | 564 /* |
| 539 ** The database page the PENDING_BYTE occupies. This page is never used. | 565 ** The database page the PENDING_BYTE occupies. This page is never used. |
| 540 */ | 566 */ |
| 541 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) | 567 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) |
| 542 | 568 |
| 543 /* | 569 /* |
| 544 ** These macros define the location of the pointer-map entry for a | 570 ** These macros define the location of the pointer-map entry for a |
| 545 ** database page. The first argument to each is the number of usable | 571 ** database page. The first argument to each is the number of usable |
| 546 ** bytes on each page of the database (often 1024). The second is the | 572 ** bytes on each page of the database (often 1024). The second is the |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 #ifndef SQLITE_OMIT_AUTOVACUUM | 640 #ifndef SQLITE_OMIT_AUTOVACUUM |
| 615 #define ISAUTOVACUUM (pBt->autoVacuum) | 641 #define ISAUTOVACUUM (pBt->autoVacuum) |
| 616 #else | 642 #else |
| 617 #define ISAUTOVACUUM 0 | 643 #define ISAUTOVACUUM 0 |
| 618 #endif | 644 #endif |
| 619 | 645 |
| 620 | 646 |
| 621 /* | 647 /* |
| 622 ** This structure is passed around through all the sanity checking routines | 648 ** This structure is passed around through all the sanity checking routines |
| 623 ** in order to keep track of some global state information. | 649 ** in order to keep track of some global state information. |
| 650 ** |
| 651 ** The aRef[] array is allocated so that there is 1 bit for each page in |
| 652 ** the database. As the integrity-check proceeds, for each page used in |
| 653 ** the database the corresponding bit is set. This allows integrity-check to |
| 654 ** detect pages that are used twice and orphaned pages (both of which |
| 655 ** indicate corruption). |
| 624 */ | 656 */ |
| 625 typedef struct IntegrityCk IntegrityCk; | 657 typedef struct IntegrityCk IntegrityCk; |
| 626 struct IntegrityCk { | 658 struct IntegrityCk { |
| 627 BtShared *pBt; /* The tree being checked out */ | 659 BtShared *pBt; /* The tree being checked out */ |
| 628 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ | 660 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 661 u8 *aPgRef; /* 1 bit per page in the db (see above) */ |
| 629 Pgno nPage; /* Number of pages in the database */ | 662 Pgno nPage; /* Number of pages in the database */ |
| 630 int *anRef; /* Number of times each page is referenced */ | |
| 631 int mxErr; /* Stop accumulating errors when this reaches zero */ | 663 int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 632 int nErr; /* Number of messages written to zErrMsg so far */ | 664 int nErr; /* Number of messages written to zErrMsg so far */ |
| 633 int mallocFailed; /* A memory allocation error has occurred */ | 665 int mallocFailed; /* A memory allocation error has occurred */ |
| 666 const char *zPfx; /* Error message prefix */ |
| 667 int v1, v2; /* Values for up to two %d fields in zPfx */ |
| 634 StrAccum errMsg; /* Accumulate the error message text here */ | 668 StrAccum errMsg; /* Accumulate the error message text here */ |
| 635 }; | 669 }; |
| 636 | 670 |
| 637 /* | 671 /* |
| 638 ** Read or write a two- and four-byte big-endian integer values. | 672 ** Routines to read or write a two- and four-byte big-endian integer values. |
| 639 */ | 673 */ |
| 640 #define get2byte(x) ((x)[0]<<8 | (x)[1]) | 674 #define get2byte(x) ((x)[0]<<8 | (x)[1]) |
| 641 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) | 675 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) |
| 642 #define get4byte sqlite3Get4byte | 676 #define get4byte sqlite3Get4byte |
| 643 #define put4byte sqlite3Put4byte | 677 #define put4byte sqlite3Put4byte |
| OLD | NEW |