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