drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 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 | ************************************************************************* |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 12 | ** This file implements an external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 13 | ** For a detailed discussion of BTrees, refer to |
| 14 | ** |
| 15 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 16 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 17 | ** Publishing Company, Reading, Massachusetts. |
| 18 | ** |
| 19 | ** The basic idea is that each page of the file contains N database |
| 20 | ** entries and N+1 pointers to subpages. |
| 21 | ** |
| 22 | ** ---------------------------------------------------------------- |
| 23 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) | |
| 24 | ** ---------------------------------------------------------------- |
| 25 | ** |
| 26 | ** All of the keys on the page that Ptr(0) points to have values less |
| 27 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
| 28 | ** values greater than Key(0) and less than Key(1). All of the keys |
| 29 | ** on Ptr(N) and its subpages have values greater than Key(N-1). And |
| 30 | ** so forth. |
| 31 | ** |
| 32 | ** Finding a particular key requires reading O(log(M)) pages from the |
| 33 | ** disk where M is the number of entries in the tree. |
| 34 | ** |
| 35 | ** In this implementation, a single file can hold one or more separate |
| 36 | ** BTrees. Each BTree is identified by the index of its root page. The |
| 37 | ** key and data for any entry are combined to form the "payload". A |
| 38 | ** fixed amount of payload can be carried directly on the database |
| 39 | ** page. If the payload is larger than the preset amount then surplus |
| 40 | ** bytes are stored on overflow pages. The payload for an entry |
| 41 | ** and the preceding pointer are combined to form a "Cell". Each |
| 42 | ** page has a small header which contains the Ptr(N) pointer and other |
| 43 | ** information such as the size of key and data. |
| 44 | ** |
| 45 | ** FORMAT DETAILS |
| 46 | ** |
| 47 | ** The file is divided into pages. The first page is called page 1, |
| 48 | ** the second is page 2, and so forth. A page number of zero indicates |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 49 | ** "no such page". The page size can be any power of 2 between 512 and 65536. |
drh | 5bbe548 | 2009-10-27 18:06:10 +0000 | [diff] [blame] | 50 | ** Each page can be either a btree page, a freelist page, an overflow |
| 51 | ** page, or a pointer-map page. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 52 | ** |
| 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. |
| 55 | ** The format of the file header is as follows: |
| 56 | ** |
| 57 | ** OFFSET SIZE DESCRIPTION |
| 58 | ** 0 16 Header string: "SQLite format 3\000" |
drh | e75fb06 | 2013-10-01 20:29:30 +0000 | [diff] [blame] | 59 | ** 16 2 Page size in bytes. (1 means 65536) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 60 | ** 18 1 File format write version |
| 61 | ** 19 1 File format read version |
| 62 | ** 20 1 Bytes of unused space at the end of each page |
drh | e75fb06 | 2013-10-01 20:29:30 +0000 | [diff] [blame] | 63 | ** 21 1 Max embedded payload fraction (must be 64) |
| 64 | ** 22 1 Min embedded payload fraction (must be 32) |
| 65 | ** 23 1 Min leaf payload fraction (must be 32) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 66 | ** 24 4 File change counter |
| 67 | ** 28 4 Reserved for future use |
| 68 | ** 32 4 First freelist page |
| 69 | ** 36 4 Number of freelist pages in the file |
| 70 | ** 40 60 15 4-byte meta values passed to higher layers |
| 71 | ** |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 72 | ** 40 4 Schema cookie |
| 73 | ** 44 4 File format of schema layer |
| 74 | ** 48 4 Size of page cache |
| 75 | ** 52 4 Largest root-page (auto/incr_vacuum) |
| 76 | ** 56 4 1=UTF-8 2=UTF16le 3=UTF16be |
| 77 | ** 60 4 User version |
| 78 | ** 64 4 Incremental vacuum mode |
drh | e75fb06 | 2013-10-01 20:29:30 +0000 | [diff] [blame] | 79 | ** 68 4 Application-ID |
| 80 | ** 72 20 unused |
| 81 | ** 92 4 The version-valid-for number |
| 82 | ** 96 4 SQLITE_VERSION_NUMBER |
drh | 27731d7 | 2009-06-22 12:05:10 +0000 | [diff] [blame] | 83 | ** |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 84 | ** All of the integer values are big-endian (most significant byte first). |
| 85 | ** |
drh | 8030869 | 2007-06-15 12:06:58 +0000 | [diff] [blame] | 86 | ** The file change counter is incremented when the database is changed |
| 87 | ** This counter allows other processes to know when the file has changed |
| 88 | ** and thus when they need to flush their cache. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 89 | ** |
| 90 | ** The max embedded payload fraction is the amount of the total usable |
| 91 | ** space in a page that can be consumed by a single cell for standard |
| 92 | ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
| 93 | ** is to limit the maximum cell size so that at least 4 cells will fit |
| 94 | ** on one page. Thus the default max embedded payload fraction is 64. |
| 95 | ** |
| 96 | ** If the payload for a cell is larger than the max payload, then extra |
| 97 | ** payload is spilled to overflow pages. Once an overflow page is allocated, |
| 98 | ** as many bytes as possible are moved into the overflow pages without letting |
| 99 | ** the cell size drop below the min embedded payload fraction. |
| 100 | ** |
| 101 | ** The min leaf payload fraction is like the min embedded payload fraction |
| 102 | ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum |
| 103 | ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it |
| 104 | ** not specified in the header. |
| 105 | ** |
| 106 | ** Each btree pages is divided into three sections: The header, the |
drh | 8030869 | 2007-06-15 12:06:58 +0000 | [diff] [blame] | 107 | ** cell pointer array, and the cell content area. Page 1 also has a 100-byte |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 108 | ** file header that occurs before the page header. |
| 109 | ** |
| 110 | ** |----------------| |
| 111 | ** | file header | 100 bytes. Page 1 only. |
| 112 | ** |----------------| |
| 113 | ** | page header | 8 bytes for leaves. 12 bytes for interior nodes |
| 114 | ** |----------------| |
| 115 | ** | cell pointer | | 2 bytes per cell. Sorted order. |
| 116 | ** | array | | Grows downward |
| 117 | ** | | v |
| 118 | ** |----------------| |
| 119 | ** | unallocated | |
| 120 | ** | space | |
| 121 | ** |----------------| ^ Grows upwards |
| 122 | ** | cell content | | Arbitrary order interspersed with freeblocks. |
| 123 | ** | area | | and free space fragments. |
| 124 | ** |----------------| |
| 125 | ** |
| 126 | ** The page headers looks like this: |
| 127 | ** |
| 128 | ** OFFSET SIZE DESCRIPTION |
| 129 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
| 130 | ** 1 2 byte offset to the first freeblock |
| 131 | ** 3 2 number of cells on this page |
| 132 | ** 5 2 first byte of the cell content area |
| 133 | ** 7 1 number of fragmented free bytes |
| 134 | ** 8 4 Right child (the Ptr(N) value). Omitted on leaves. |
| 135 | ** |
| 136 | ** The flags define the format of this btree page. The leaf flag means that |
| 137 | ** this page has no children. The zerodata flag means that this page carries |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 138 | ** only keys and no data. The intkey flag means that the key is an integer |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 139 | ** which is stored in the key size entry of the cell header rather than in |
| 140 | ** the payload area. |
| 141 | ** |
| 142 | ** The cell pointer array begins on the first byte after the page header. |
| 143 | ** The cell pointer array contains zero or more 2-byte numbers which are |
| 144 | ** offsets from the beginning of the page to the cell content in the cell |
| 145 | ** content area. The cell pointers occur in sorted order. The system strives |
| 146 | ** to keep free space after the last cell pointer so that new cells can |
| 147 | ** be easily added without having to defragment the page. |
| 148 | ** |
| 149 | ** Cell content is stored at the very end of the page and grows toward the |
| 150 | ** beginning of the page. |
| 151 | ** |
| 152 | ** Unused space within the cell content area is collected into a linked list of |
| 153 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 154 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 155 | ** increasing order. Because a freeblock must be at least 4 bytes in size, |
| 156 | ** any group of 3 or fewer unused bytes in the cell content area cannot |
| 157 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
| 158 | ** a fragment. The total number of bytes in all fragments is recorded. |
| 159 | ** in the page header at offset 7. |
| 160 | ** |
| 161 | ** SIZE DESCRIPTION |
| 162 | ** 2 Byte offset of the next freeblock |
| 163 | ** 2 Bytes in this freeblock |
| 164 | ** |
| 165 | ** Cells are of variable length. Cells are stored in the cell content area at |
| 166 | ** the end of the page. Pointers to the cells are in the cell pointer array |
| 167 | ** that immediately follows the page header. Cells is not necessarily |
| 168 | ** contiguous or in order, but cell pointers are contiguous and in order. |
| 169 | ** |
| 170 | ** Cell content makes use of variable length integers. A variable |
| 171 | ** length integer is 1 to 9 bytes where the lower 7 bits of each |
| 172 | ** byte are used. The integer consists of all bytes that have bit 8 set and |
| 173 | ** the first byte with bit 8 clear. The most significant byte of the integer |
| 174 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
| 175 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
| 176 | ** allows a 64-bit integer to be encoded in 9 bytes. |
| 177 | ** |
| 178 | ** 0x00 becomes 0x00000000 |
| 179 | ** 0x7f becomes 0x0000007f |
| 180 | ** 0x81 0x00 becomes 0x00000080 |
| 181 | ** 0x82 0x00 becomes 0x00000100 |
| 182 | ** 0x80 0x7f becomes 0x0000007f |
| 183 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
| 184 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 185 | ** |
| 186 | ** Variable length integers are used for rowids and to hold the number of |
| 187 | ** bytes of key and data in a btree cell. |
| 188 | ** |
| 189 | ** The content of a cell looks like this: |
| 190 | ** |
| 191 | ** SIZE DESCRIPTION |
| 192 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 193 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 194 | ** var Number of bytes of key. Or the key itself if intkey flag is set. |
| 195 | ** * Payload |
| 196 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 197 | ** |
| 198 | ** Overflow pages form a linked list. Each page except the last is completely |
| 199 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 200 | ** as 1 byte of data. |
| 201 | ** |
| 202 | ** SIZE DESCRIPTION |
| 203 | ** 4 Page number of next overflow page |
| 204 | ** * Data |
| 205 | ** |
| 206 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
drh | 8030869 | 2007-06-15 12:06:58 +0000 | [diff] [blame] | 207 | ** file header points to the first in a linked list of trunk page. Each trunk |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 208 | ** page points to multiple leaf pages. The content of a leaf page is |
| 209 | ** unspecified. A trunk page looks like this: |
| 210 | ** |
| 211 | ** SIZE DESCRIPTION |
| 212 | ** 4 Page number of next trunk page |
| 213 | ** 4 Number of leaf pointers on this page |
| 214 | ** * zero or more pages numbers of leaves |
| 215 | */ |
| 216 | #include "sqliteInt.h" |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 217 | |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 218 | |
| 219 | /* The following value is the maximum cell size assuming a maximum page |
| 220 | ** size give above. |
| 221 | */ |
drh | fcd71b6 | 2011-04-05 22:08:24 +0000 | [diff] [blame] | 222 | #define MX_CELL_SIZE(pBt) ((int)(pBt->pageSize-8)) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 223 | |
| 224 | /* The maximum number of cells on a single page of the database. This |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 225 | ** assumes a minimum cell size of 6 bytes (4 bytes for the cell itself |
| 226 | ** plus 2 bytes for the index to the cell in the page header). Such |
| 227 | ** small cells will be rare, but they are possible. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 228 | */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 229 | #define MX_CELL(pBt) ((pBt->pageSize-8)/6) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 230 | |
| 231 | /* Forward declarations */ |
| 232 | typedef struct MemPage MemPage; |
| 233 | typedef struct BtLock BtLock; |
drh | 5fa6051 | 2015-06-19 17:19:34 +0000 | [diff] [blame] | 234 | typedef struct CellInfo CellInfo; |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | ** This is a magic string that appears at the beginning of every |
| 238 | ** SQLite database in order to identify the file as a real database. |
| 239 | ** |
| 240 | ** You can change this value at compile-time by specifying a |
| 241 | ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The |
| 242 | ** header must be exactly 16 bytes including the zero-terminator so |
| 243 | ** the string itself should be 15 characters long. If you change |
| 244 | ** the header, then your custom library will not be able to read |
| 245 | ** databases generated by the standard tools and the standard tools |
| 246 | ** will not be able to read databases created by your custom library. |
| 247 | */ |
| 248 | #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ |
| 249 | # define SQLITE_FILE_HEADER "SQLite format 3" |
| 250 | #endif |
| 251 | |
| 252 | /* |
| 253 | ** Page type flags. An ORed combination of these flags appear as the |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 254 | ** first byte of on-disk image of every BTree page. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 255 | */ |
| 256 | #define PTF_INTKEY 0x01 |
| 257 | #define PTF_ZERODATA 0x02 |
| 258 | #define PTF_LEAFDATA 0x04 |
| 259 | #define PTF_LEAF 0x08 |
| 260 | |
| 261 | /* |
| 262 | ** As each page of the file is loaded into memory, an instance of the following |
| 263 | ** structure is appended and initialized to zero. This structure stores |
| 264 | ** information about the page that is decoded from the raw file page. |
| 265 | ** |
| 266 | ** The pParent field points back to the parent page. This allows us to |
| 267 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 268 | ** unref() the parent page pointer when this page is no longer referenced. |
| 269 | ** The pageDestructor() routine handles that chore. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 270 | ** |
| 271 | ** Access to all fields of this structure is controlled by the mutex |
| 272 | ** stored in MemPage.pBt->mutex. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 273 | */ |
| 274 | struct MemPage { |
| 275 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 276 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
drh | 3e28ff5 | 2014-09-24 00:59:08 +0000 | [diff] [blame] | 277 | u8 intKey; /* True if table b-trees. False for index b-trees */ |
| 278 | u8 intKeyLeaf; /* True if the leaf of an intKey table */ |
| 279 | u8 noPayload; /* True if internal intKey page (thus w/o data) */ |
| 280 | u8 leaf; /* True if a leaf page */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 281 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
| 282 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 283 | u8 max1bytePayload; /* min(maxLocal,127) */ |
drh | ccf46d0 | 2015-04-01 13:21:33 +0000 | [diff] [blame] | 284 | u8 bBusy; /* Prevent endless loops on corrupt database files */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 285 | u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */ |
| 286 | u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 287 | u16 cellOffset; /* Index in aData of first cell pointer */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 288 | u16 nFree; /* Number of free bytes on the page */ |
| 289 | u16 nCell; /* Number of cells on this page, local and ovfl */ |
drh | 1688c86 | 2008-07-18 02:44:17 +0000 | [diff] [blame] | 290 | u16 maskPage; /* Mask for page offset */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 291 | u16 aiOvfl[5]; /* Insert the i-th overflow cell before the aiOvfl-th |
| 292 | ** non-overflow cell */ |
| 293 | u8 *apOvfl[5]; /* Pointers to the body of overflow cells */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 294 | BtShared *pBt; /* Pointer to BtShared that this page is part of */ |
| 295 | u8 *aData; /* Pointer to disk image of the page data */ |
drh | 3def235 | 2011-11-11 00:27:15 +0000 | [diff] [blame] | 296 | u8 *aDataEnd; /* One byte past the end of usable data */ |
| 297 | u8 *aCellIdx; /* The cell index area */ |
drh | f44890a | 2015-06-27 03:58:15 +0000 | [diff] [blame] | 298 | u8 *aDataOfst; /* Same as aData for leaves. aData+4 for interior */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 299 | DbPage *pDbPage; /* Pager page handle */ |
drh | 5fa6051 | 2015-06-19 17:19:34 +0000 | [diff] [blame] | 300 | u16 (*xCellSize)(MemPage*,u8*); /* cellSizePtr method */ |
| 301 | void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 302 | Pgno pgno; /* Page number for this page */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | /* |
| 306 | ** The in-memory image of a disk page has the auxiliary information appended |
| 307 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 308 | ** that extra information. |
| 309 | */ |
| 310 | #define EXTRA_SIZE sizeof(MemPage) |
| 311 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 312 | /* |
| 313 | ** A linked list of the following structures is stored at BtShared.pLock. |
| 314 | ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor |
| 315 | ** is opened on the table with root page BtShared.iTable. Locks are removed |
| 316 | ** from this list when a transaction is committed or rolled back, or when |
| 317 | ** a btree handle is closed. |
| 318 | */ |
| 319 | struct BtLock { |
| 320 | Btree *pBtree; /* Btree handle holding this lock */ |
| 321 | Pgno iTable; /* Root page of table */ |
| 322 | u8 eLock; /* READ_LOCK or WRITE_LOCK */ |
| 323 | BtLock *pNext; /* Next in BtShared.pLock list */ |
| 324 | }; |
| 325 | |
| 326 | /* Candidate values for BtLock.eLock */ |
| 327 | #define READ_LOCK 1 |
| 328 | #define WRITE_LOCK 2 |
| 329 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 330 | /* A Btree handle |
| 331 | ** |
| 332 | ** A database connection contains a pointer to an instance of |
| 333 | ** this object for every database file that it has open. This structure |
| 334 | ** is opaque to the database connection. The database connection cannot |
| 335 | ** see the internals of this structure and only deals with pointers to |
| 336 | ** this structure. |
| 337 | ** |
| 338 | ** For some database files, the same underlying database cache might be |
drh | ed1f878 | 2009-10-16 13:23:33 +0000 | [diff] [blame] | 339 | ** shared between multiple connections. In that case, each connection |
| 340 | ** has it own instance of this object. But each instance of this object |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 341 | ** points to the same BtShared object. The database cache and the |
| 342 | ** schema associated with the database file are all contained within |
| 343 | ** the BtShared object. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 344 | ** |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 345 | ** All fields in this structure are accessed under sqlite3.mutex. |
| 346 | ** The pBt pointer itself may not be changed while there exists cursors |
| 347 | ** in the referenced BtShared that point back to this Btree since those |
drh | 4fa7d7c | 2011-04-03 02:41:00 +0000 | [diff] [blame] | 348 | ** cursors have to go through this Btree to find their BtShared and |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 349 | ** they often do so without holding sqlite3.mutex. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 350 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 351 | struct Btree { |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 352 | sqlite3 *db; /* The database connection holding this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 353 | BtShared *pBt; /* Sharable content of this btree */ |
| 354 | u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 355 | u8 sharable; /* True if we can share pBt with another db */ |
| 356 | u8 locked; /* True if db currently has pBt locked */ |
drh | 6918095 | 2015-06-25 13:03:10 +0000 | [diff] [blame] | 357 | u8 hasIncrblobCur; /* True if there are one or more Incrblob cursors */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 358 | int wantToLock; /* Number of nested calls to sqlite3BtreeEnter() */ |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 359 | int nBackup; /* Number of backup operations reading this btree */ |
drh | 3da9c04 | 2014-12-22 18:41:21 +0000 | [diff] [blame] | 360 | u32 iDataVersion; /* Combines with pBt->pPager->iDataVersion */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 361 | Btree *pNext; /* List of other sharable Btrees from the same db */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 362 | Btree *pPrev; /* Back pointer of the same list */ |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 363 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 364 | BtLock lock; /* Object used to lock page 1 */ |
| 365 | #endif |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | /* |
| 369 | ** Btree.inTrans may take one of the following values. |
| 370 | ** |
| 371 | ** If the shared-data extension is enabled, there may be multiple users |
| 372 | ** of the Btree structure. At most one of these may open a write transaction, |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 373 | ** but any number may have active read transactions. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 374 | */ |
| 375 | #define TRANS_NONE 0 |
| 376 | #define TRANS_READ 1 |
| 377 | #define TRANS_WRITE 2 |
| 378 | |
| 379 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 380 | ** An instance of this object represents a single database file. |
| 381 | ** |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 382 | ** A single database file can be in use at the same time by two |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 383 | ** or more database connections. When two or more connections are |
| 384 | ** sharing the same database file, each connection has it own |
| 385 | ** private Btree object for the file and each of those Btrees points |
| 386 | ** to this one BtShared object. BtShared.nRef is the number of |
| 387 | ** connections currently sharing this database file. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 388 | ** |
| 389 | ** Fields in this structure are accessed under the BtShared.mutex |
| 390 | ** mutex, except for nRef and pNext which are accessed under the |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 391 | ** global SQLITE_MUTEX_STATIC_MASTER mutex. The pPager field |
| 392 | ** may not be modified once it is initially set as long as nRef>0. |
| 393 | ** The pSchema field may be set once under BtShared.mutex and |
| 394 | ** thereafter is unchanged as long as nRef>0. |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 395 | ** |
| 396 | ** isPending: |
| 397 | ** |
| 398 | ** If a BtShared client fails to obtain a write-lock on a database |
| 399 | ** table (because there exists one or more read-locks on the table), |
| 400 | ** the shared-cache enters 'pending-lock' state and isPending is |
| 401 | ** set to true. |
| 402 | ** |
| 403 | ** The shared-cache leaves the 'pending lock' state when either of |
| 404 | ** the following occur: |
| 405 | ** |
| 406 | ** 1) The current writer (BtShared.pWriter) concludes its transaction, OR |
| 407 | ** 2) The number of locks held by other connections drops to zero. |
| 408 | ** |
| 409 | ** while in the 'pending-lock' state, no connection may start a new |
| 410 | ** transaction. |
| 411 | ** |
| 412 | ** This feature is included to help prevent writer-starvation. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 413 | */ |
| 414 | struct BtShared { |
| 415 | Pager *pPager; /* The page cache */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 416 | sqlite3 *db; /* Database connection currently using this Btree */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 417 | BtCursor *pCursor; /* A list of all open cursors */ |
| 418 | MemPage *pPage1; /* First page of the database */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 419 | u8 openFlags; /* Flags to sqlite3BtreeOpen() */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 420 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 421 | u8 autoVacuum; /* True if auto-vacuum is enabled */ |
| 422 | u8 incrVacuum; /* True if incr-vacuum is enabled */ |
dan | bc1a3c6 | 2013-02-23 16:40:46 +0000 | [diff] [blame] | 423 | u8 bDoTruncate; /* True to truncate db on commit */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 424 | #endif |
drh | 2e5de2f | 2011-01-07 02:50:40 +0000 | [diff] [blame] | 425 | u8 inTransaction; /* Transaction state */ |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 426 | u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */ |
drh | ad0961b | 2015-02-21 00:19:25 +0000 | [diff] [blame] | 427 | #ifdef SQLITE_HAS_CODEC |
| 428 | u8 optimalReserve; /* Desired amount of reserved space per page */ |
| 429 | #endif |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 430 | u16 btsFlags; /* Boolean parameters. See BTS_* macros below */ |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 431 | u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 432 | u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 433 | u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 434 | u16 minLeaf; /* Minimum local payload in a LEAFDATA table */ |
drh | b2eced5 | 2010-08-12 02:41:12 +0000 | [diff] [blame] | 435 | u32 pageSize; /* Total number of bytes on a page */ |
| 436 | u32 usableSize; /* Number of usable bytes on each page */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 437 | int nTransaction; /* Number of open transactions (read + write) */ |
drh | dd3cd97 | 2010-03-27 17:12:36 +0000 | [diff] [blame] | 438 | u32 nPage; /* Number of pages in the database */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 439 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 440 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
drh | bdaec52 | 2011-04-04 00:14:43 +0000 | [diff] [blame] | 441 | sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 442 | Bitvec *pHasContent; /* Set of pages moved to free-list this transaction */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 443 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 444 | int nRef; /* Number of references to this structure */ |
| 445 | BtShared *pNext; /* Next on a list of sharable BtShared structs */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 446 | BtLock *pLock; /* List of locks held on this shared-btree struct */ |
danielk1977 | 404ca07 | 2009-03-16 13:19:36 +0000 | [diff] [blame] | 447 | Btree *pWriter; /* Btree with currently open write transaction */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 448 | #endif |
drh | 92787cf | 2014-10-15 11:55:51 +0000 | [diff] [blame] | 449 | u8 *pTmpSpace; /* Temp space sufficient to hold a single cell */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | /* |
drh | c916634 | 2012-01-05 23:32:06 +0000 | [diff] [blame] | 453 | ** Allowed values for BtShared.btsFlags |
| 454 | */ |
| 455 | #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */ |
| 456 | #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */ |
| 457 | #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */ |
| 458 | #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */ |
| 459 | #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */ |
| 460 | #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */ |
| 461 | #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */ |
| 462 | |
| 463 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 464 | ** An instance of the following structure is used to hold information |
| 465 | ** about a cell. The parseCellPtr() function fills in this structure |
| 466 | ** based on information extract from the raw disk page. |
| 467 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 468 | struct CellInfo { |
drh | ab1cc58 | 2014-09-23 21:25:19 +0000 | [diff] [blame] | 469 | i64 nKey; /* The key for INTKEY tables, or nPayload otherwise */ |
| 470 | u8 *pPayload; /* Pointer to the start of payload */ |
| 471 | u32 nPayload; /* Bytes of payload */ |
| 472 | u16 nLocal; /* Amount of payload held locally, not on overflow */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 473 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
| 474 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
| 475 | }; |
| 476 | |
| 477 | /* |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 478 | ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than |
| 479 | ** this will be declared corrupt. This value is calculated based on a |
| 480 | ** maximum database size of 2^31 pages a minimum fanout of 2 for a |
| 481 | ** root-node and 3 for all other internal nodes. |
| 482 | ** |
| 483 | ** If a tree that appears to be taller than this is encountered, it is |
| 484 | ** assumed that the database is corrupt. |
| 485 | */ |
| 486 | #define BTCURSOR_MAX_DEPTH 20 |
| 487 | |
| 488 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 489 | ** A cursor is a pointer to a particular entry within a particular |
| 490 | ** b-tree within a database file. |
| 491 | ** |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 492 | ** The entry is identified by its MemPage and the index in |
| 493 | ** MemPage.aCell[] of the entry. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 494 | ** |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 495 | ** A single database file can be shared by two more database connections, |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 496 | ** but cursors cannot be shared. Each cursor is associated with a |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 497 | ** particular database connection identified BtCursor.pBtree.db. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 498 | ** |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 499 | ** Fields in this structure are accessed under the BtShared.mutex |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 500 | ** found at self->pBt->mutex. |
drh | d816e00 | 2014-11-17 19:25:15 +0000 | [diff] [blame] | 501 | ** |
| 502 | ** skipNext meaning: |
| 503 | ** eState==SKIPNEXT && skipNext>0: Next sqlite3BtreeNext() is no-op. |
| 504 | ** eState==SKIPNEXT && skipNext<0: Next sqlite3BtreePrevious() is no-op. |
| 505 | ** eState==FAULT: Cursor fault with skipNext as error code. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 506 | */ |
| 507 | struct BtCursor { |
| 508 | Btree *pBtree; /* The Btree to which this cursor belongs */ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 509 | BtShared *pBt; /* The BtShared this cursor points to */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 510 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 511 | Pgno *aOverflow; /* Cache of overflow page locations */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 512 | CellInfo info; /* A parse of the cell we are pointing at */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 513 | i64 nKey; /* Size of pKey, or last integer key */ |
| 514 | void *pKey; /* Saved key that was cursor last known position */ |
| 515 | Pgno pgnoRoot; /* The root page of this tree */ |
| 516 | int nOvflAlloc; /* Allocated size of aOverflow[] array */ |
drh | d816e00 | 2014-11-17 19:25:15 +0000 | [diff] [blame] | 517 | int skipNext; /* Prev() is noop if negative. Next() is noop if positive. |
| 518 | ** Error code if eState==CURSOR_FAULT */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 519 | u8 curFlags; /* zero or more BTCF_* flags defined below */ |
drh | 28f58dd | 2015-06-27 19:45:03 +0000 | [diff] [blame] | 520 | u8 curPagerFlags; /* Flags to send to sqlite3PagerAcquire() */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 521 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
drh | 408efc0 | 2015-06-27 22:49:10 +0000 | [diff] [blame^] | 522 | u8 hints; /* As configured by CursorSetHints() */ |
| 523 | /* All fields above are zeroed when the cursor is allocated. See |
| 524 | ** sqlite3BtreeCursorZero(). Fields that follow must be manually |
| 525 | ** initialized. */ |
| 526 | i8 iPage; /* Index of current page in apPage */ |
| 527 | u8 curIntKey; /* Value of apPage[0]->intKey */ |
| 528 | struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */ |
danielk1977 | 71d5d2c | 2008-09-29 11:49:47 +0000 | [diff] [blame] | 529 | u16 aiIdx[BTCURSOR_MAX_DEPTH]; /* Current index in apPage[i] */ |
drh | 2e5de2f | 2011-01-07 02:50:40 +0000 | [diff] [blame] | 530 | MemPage *apPage[BTCURSOR_MAX_DEPTH]; /* Pages from root to current page */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
| 533 | /* |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 534 | ** Legal values for BtCursor.curFlags |
| 535 | */ |
drh | 4c41718 | 2014-03-31 23:57:41 +0000 | [diff] [blame] | 536 | #define BTCF_WriteFlag 0x01 /* True if a write cursor */ |
| 537 | #define BTCF_ValidNKey 0x02 /* True if info.nKey is valid */ |
| 538 | #define BTCF_ValidOvfl 0x04 /* True if aOverflow is valid */ |
| 539 | #define BTCF_AtLast 0x08 /* Cursor is pointing ot the last entry */ |
drh | 036dbec | 2014-03-11 23:40:44 +0000 | [diff] [blame] | 540 | #define BTCF_Incrblob 0x10 /* True if an incremental I/O handle */ |
| 541 | |
| 542 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 543 | ** Potential values for BtCursor.eState. |
| 544 | ** |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 545 | ** CURSOR_INVALID: |
| 546 | ** Cursor does not point to a valid entry. This can happen (for example) |
| 547 | ** because the table is empty or because BtreeCursorFirst() has not been |
| 548 | ** called. |
| 549 | ** |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 550 | ** CURSOR_VALID: |
| 551 | ** Cursor points to a valid entry. getPayload() etc. may be called. |
| 552 | ** |
| 553 | ** CURSOR_SKIPNEXT: |
| 554 | ** Cursor is valid except that the Cursor.skipNext field is non-zero |
| 555 | ** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious() |
| 556 | ** operation should be a no-op. |
| 557 | ** |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 558 | ** CURSOR_REQUIRESEEK: |
| 559 | ** The table that this cursor was opened on still exists, but has been |
| 560 | ** modified since the cursor was last used. The cursor position is saved |
| 561 | ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in |
drh | a346058 | 2008-07-11 21:02:53 +0000 | [diff] [blame] | 562 | ** this state, restoreCursorPosition() can be called to attempt to |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 563 | ** seek the cursor to the saved position. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 564 | ** |
| 565 | ** CURSOR_FAULT: |
peter.d.reid | 60ec914 | 2014-09-06 16:39:46 +0000 | [diff] [blame] | 566 | ** An unrecoverable error (an I/O error or a malloc failure) has occurred |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 567 | ** on a different connection that shares the BtShared cache with this |
| 568 | ** cursor. The error has left the cache in an inconsistent state. |
| 569 | ** Do nothing else with this cursor. Any attempt to use the cursor |
drh | d816e00 | 2014-11-17 19:25:15 +0000 | [diff] [blame] | 570 | ** should return the error code stored in BtCursor.skipNext |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 571 | */ |
| 572 | #define CURSOR_INVALID 0 |
| 573 | #define CURSOR_VALID 1 |
drh | 9b47ee3 | 2013-08-20 03:13:51 +0000 | [diff] [blame] | 574 | #define CURSOR_SKIPNEXT 2 |
| 575 | #define CURSOR_REQUIRESEEK 3 |
| 576 | #define CURSOR_FAULT 4 |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 577 | |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 578 | /* |
| 579 | ** The database page the PENDING_BYTE occupies. This page is never used. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 580 | */ |
danielk1977 | bea2a94 | 2009-01-20 17:06:27 +0000 | [diff] [blame] | 581 | # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 582 | |
| 583 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 584 | ** These macros define the location of the pointer-map entry for a |
| 585 | ** database page. The first argument to each is the number of usable |
| 586 | ** bytes on each page of the database (often 1024). The second is the |
| 587 | ** page number to look up in the pointer map. |
| 588 | ** |
| 589 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
| 590 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
| 591 | ** the offset of the requested map entry. |
| 592 | ** |
| 593 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
| 594 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
| 595 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
| 596 | ** this test. |
| 597 | */ |
| 598 | #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) |
danielk1977 | 8c666b1 | 2008-07-18 09:34:57 +0000 | [diff] [blame] | 599 | #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1)) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 600 | #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) |
| 601 | |
| 602 | /* |
| 603 | ** The pointer map is a lookup table that identifies the parent page for |
| 604 | ** each child page in the database file. The parent page is the page that |
| 605 | ** contains a pointer to the child. Every page in the database contains |
| 606 | ** 0 or 1 parent pages. (In this context 'database page' refers |
| 607 | ** to any page that is not part of the pointer map itself.) Each pointer map |
| 608 | ** entry consists of a single byte 'type' and a 4 byte parent page number. |
| 609 | ** The PTRMAP_XXX identifiers below are the valid types. |
| 610 | ** |
| 611 | ** The purpose of the pointer map is to facility moving pages from one |
| 612 | ** position in the file to another as part of autovacuum. When a page |
| 613 | ** is moved, the pointer in its parent must be updated to point to the |
| 614 | ** new location. The pointer map is used to locate the parent page quickly. |
| 615 | ** |
| 616 | ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not |
| 617 | ** used in this case. |
| 618 | ** |
| 619 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
| 620 | ** is not used in this case. |
| 621 | ** |
| 622 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
| 623 | ** overflow pages. The page number identifies the page that |
| 624 | ** contains the cell with a pointer to this overflow page. |
| 625 | ** |
| 626 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
| 627 | ** overflow pages. The page-number identifies the previous |
| 628 | ** page in the overflow page list. |
| 629 | ** |
| 630 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
| 631 | ** identifies the parent page in the btree. |
| 632 | */ |
| 633 | #define PTRMAP_ROOTPAGE 1 |
| 634 | #define PTRMAP_FREEPAGE 2 |
| 635 | #define PTRMAP_OVERFLOW1 3 |
| 636 | #define PTRMAP_OVERFLOW2 4 |
| 637 | #define PTRMAP_BTREE 5 |
| 638 | |
| 639 | /* A bunch of assert() statements to check the transaction state variables |
| 640 | ** of handle p (type Btree*) are internally consistent. |
| 641 | */ |
| 642 | #define btreeIntegrity(p) \ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 643 | assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ |
| 644 | assert( p->pBt->inTransaction>=p->inTrans ); |
| 645 | |
| 646 | |
| 647 | /* |
| 648 | ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine |
| 649 | ** if the database supports auto-vacuum or not. Because it is used |
| 650 | ** within an expression that is an argument to another macro |
| 651 | ** (sqliteMallocRaw), it is not possible to use conditional compilation. |
| 652 | ** So, this macro is defined instead. |
| 653 | */ |
| 654 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 655 | #define ISAUTOVACUUM (pBt->autoVacuum) |
| 656 | #else |
| 657 | #define ISAUTOVACUUM 0 |
| 658 | #endif |
| 659 | |
| 660 | |
| 661 | /* |
| 662 | ** This structure is passed around through all the sanity checking routines |
| 663 | ** in order to keep track of some global state information. |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 664 | ** |
| 665 | ** The aRef[] array is allocated so that there is 1 bit for each page in |
| 666 | ** the database. As the integrity-check proceeds, for each page used in |
| 667 | ** the database the corresponding bit is set. This allows integrity-check to |
| 668 | ** detect pages that are used twice and orphaned pages (both of which |
| 669 | ** indicate corruption). |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 670 | */ |
| 671 | typedef struct IntegrityCk IntegrityCk; |
| 672 | struct IntegrityCk { |
| 673 | BtShared *pBt; /* The tree being checked out */ |
| 674 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
dan | 1235bb1 | 2012-04-03 17:43:28 +0000 | [diff] [blame] | 675 | u8 *aPgRef; /* 1 bit per page in the db (see above) */ |
drh | 2cbd78b | 2012-02-02 19:37:18 +0000 | [diff] [blame] | 676 | Pgno nPage; /* Number of pages in the database */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 677 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 678 | int nErr; /* Number of messages written to zErrMsg so far */ |
drh | c890fec | 2008-08-01 20:10:08 +0000 | [diff] [blame] | 679 | int mallocFailed; /* A memory allocation error has occurred */ |
drh | 867db83 | 2014-09-26 02:41:05 +0000 | [diff] [blame] | 680 | const char *zPfx; /* Error message prefix */ |
| 681 | int v1, v2; /* Values for up to two %d fields in zPfx */ |
drh | f089aa4 | 2008-07-08 19:34:06 +0000 | [diff] [blame] | 682 | StrAccum errMsg; /* Accumulate the error message text here */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 683 | }; |
| 684 | |
| 685 | /* |
drh | b8a45bb | 2011-12-31 21:51:55 +0000 | [diff] [blame] | 686 | ** Routines to read or write a two- and four-byte big-endian integer values. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 687 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 688 | #define get2byte(x) ((x)[0]<<8 | (x)[1]) |
drh | f49661a | 2008-12-10 16:45:50 +0000 | [diff] [blame] | 689 | #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v)) |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 690 | #define get4byte sqlite3Get4byte |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 691 | #define put4byte sqlite3Put4byte |