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