drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.189 2004/09/08 20:13:05 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21: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) | Ptr(N+1) | |
| 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+1) and its subpages have values greater than Key(N). And |
| 32 | ** so forth. |
| 33 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 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. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 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 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 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+1) pointer and other |
| 45 | ** information such as the size of key and data. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 46 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 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 |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 56 | ** page contain a special header (the "file header") that describes the file. |
| 57 | ** The format of the file header is as follows: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 58 | ** |
| 59 | ** OFFSET SIZE DESCRIPTION |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 60 | ** 0 16 Header string: "SQLite format 3\000" |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 61 | ** 16 2 Page size in bytes. |
| 62 | ** 18 1 File format write version |
| 63 | ** 19 1 File format read version |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 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 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 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). |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 75 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 76 | ** The file change counter is incremented when the database is changed more |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 77 | ** than once within the same second. This counter, together with the |
| 78 | ** modification time of the file, allows other processes to know |
| 79 | ** when the file has changed and thus when they need to flush their |
| 80 | ** cache. |
| 81 | ** |
| 82 | ** The max embedded payload fraction is the amount of the total usable |
| 83 | ** space in a page that can be consumed by a single cell for standard |
| 84 | ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
| 85 | ** is to limit the maximum cell size so that at least 4 cells will fit |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 86 | ** on one page. Thus the default max embedded payload fraction is 64. |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 87 | ** |
| 88 | ** If the payload for a cell is larger than the max payload, then extra |
| 89 | ** payload is spilled to overflow pages. Once an overflow page is allocated, |
| 90 | ** as many bytes as possible are moved into the overflow pages without letting |
| 91 | ** the cell size drop below the min embedded payload fraction. |
| 92 | ** |
| 93 | ** The min leaf payload fraction is like the min embedded payload fraction |
| 94 | ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum |
| 95 | ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it |
| 96 | ** not specified in the header. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 97 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 98 | ** Each btree pages is divided into three sections: The header, the |
| 99 | ** cell pointer array, and the cell area area. Page 1 also has a 100-byte |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 100 | ** file header that occurs before the page header. |
| 101 | ** |
| 102 | ** |----------------| |
| 103 | ** | file header | 100 bytes. Page 1 only. |
| 104 | ** |----------------| |
| 105 | ** | page header | 8 bytes for leaves. 12 bytes for interior nodes |
| 106 | ** |----------------| |
| 107 | ** | cell pointer | | 2 bytes per cell. Sorted order. |
| 108 | ** | array | | Grows downward |
| 109 | ** | | v |
| 110 | ** |----------------| |
| 111 | ** | unallocated | |
| 112 | ** | space | |
| 113 | ** |----------------| ^ Grows upwards |
| 114 | ** | cell content | | Arbitrary order interspersed with freeblocks. |
| 115 | ** | area | | and free space fragments. |
| 116 | ** |----------------| |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 117 | ** |
| 118 | ** The page headers looks like this: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 119 | ** |
| 120 | ** OFFSET SIZE DESCRIPTION |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 121 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 122 | ** 1 2 byte offset to the first freeblock |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 123 | ** 3 2 number of cells on this page |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 124 | ** 5 2 first byte of the cell content area |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 125 | ** 7 1 number of fragmented free bytes |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 126 | ** 8 4 Right child (the Ptr(N+1) value). Omitted on leaves. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 127 | ** |
| 128 | ** The flags define the format of this btree page. The leaf flag means that |
| 129 | ** this page has no children. The zerodata flag means that this page carries |
| 130 | ** only keys and no data. The intkey flag means that the key is a single |
| 131 | ** variable length integer at the beginning of the payload. |
| 132 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 133 | ** The cell pointer array begins on the first byte after the page header. |
| 134 | ** The cell pointer array contains zero or more 2-byte numbers which are |
| 135 | ** offsets from the beginning of the page to the cell content in the cell |
| 136 | ** content area. The cell pointers occur in sorted order. The system strives |
| 137 | ** to keep free space after the last cell pointer so that new cells can |
| 138 | ** be easily added without have to defragment the page. |
| 139 | ** |
| 140 | ** Cell content is stored at the very end of the page and grows toward the |
| 141 | ** beginning of the page. |
| 142 | ** |
| 143 | ** Unused space within the cell content area is collected into a linked list of |
| 144 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 145 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 146 | ** increasing order. Because a freeblock must be at least 4 bytes in size, |
| 147 | ** any group of 3 or fewer unused bytes in the cell content area cannot |
| 148 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
| 149 | ** a fragment. The total number of bytes in all fragments is recorded. |
| 150 | ** in the page header at offset 7. |
| 151 | ** |
| 152 | ** SIZE DESCRIPTION |
| 153 | ** 2 Byte offset of the next freeblock |
| 154 | ** 2 Bytes in this freeblock |
| 155 | ** |
| 156 | ** Cells are of variable length. Cells are stored in the cell content area at |
| 157 | ** the end of the page. Pointers to the cells are in the cell pointer array |
| 158 | ** that immediately follows the page header. Cells is not necessarily |
| 159 | ** contiguous or in order, but cell pointers are contiguous and in order. |
| 160 | ** |
| 161 | ** Cell content makes use of variable length integers. A variable |
| 162 | ** length integer is 1 to 9 bytes where the lower 7 bits of each |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 163 | ** byte are used. The integer consists of all bytes that have bit 8 set and |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 164 | ** the first byte with bit 8 clear. The most significant byte of the integer |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 165 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
| 166 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
| 167 | ** allows a 64-bit integer to be encoded in 9 bytes. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 168 | ** |
| 169 | ** 0x00 becomes 0x00000000 |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 170 | ** 0x7f becomes 0x0000007f |
| 171 | ** 0x81 0x00 becomes 0x00000080 |
| 172 | ** 0x82 0x00 becomes 0x00000100 |
| 173 | ** 0x80 0x7f becomes 0x0000007f |
| 174 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 175 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 176 | ** |
| 177 | ** Variable length integers are used for rowids and to hold the number of |
| 178 | ** bytes of key and data in a btree cell. |
| 179 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 180 | ** The content of a cell looks like this: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 181 | ** |
| 182 | ** SIZE DESCRIPTION |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 183 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 184 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 185 | ** var Number of bytes of key. Or the key itself if intkey flag is set. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 186 | ** * Payload |
| 187 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 188 | ** |
| 189 | ** Overflow pages form a linked list. Each page except the last is completely |
| 190 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 191 | ** as 1 byte of data. |
| 192 | ** |
| 193 | ** SIZE DESCRIPTION |
| 194 | ** 4 Page number of next overflow page |
| 195 | ** * Data |
| 196 | ** |
| 197 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
| 198 | ** file header points to first in a linked list of trunk page. Each trunk |
| 199 | ** page points to multiple leaf pages. The content of a leaf page is |
| 200 | ** unspecified. A trunk page looks like this: |
| 201 | ** |
| 202 | ** SIZE DESCRIPTION |
| 203 | ** 4 Page number of next trunk page |
| 204 | ** 4 Number of leaf pointers on this page |
| 205 | ** * zero or more pages numbers of leaves |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 206 | */ |
| 207 | #include "sqliteInt.h" |
| 208 | #include "pager.h" |
| 209 | #include "btree.h" |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 210 | #include "os.h" |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 211 | #include <assert.h> |
| 212 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 213 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 214 | /* The following value is the maximum cell size assuming a maximum page |
| 215 | ** size give above. |
| 216 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 217 | #define MX_CELL_SIZE(pBt) (pBt->pageSize-8) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 218 | |
| 219 | /* The maximum number of cells on a single page of the database. This |
| 220 | ** assumes a minimum cell size of 3 bytes. Such small cells will be |
| 221 | ** exceedingly rare, but they are possible. |
| 222 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 223 | #define MX_CELL(pBt) ((pBt->pageSize-8)/3) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 224 | |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 225 | /* Forward declarations */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 226 | typedef struct MemPage MemPage; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 227 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 228 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 229 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 230 | ** SQLite database in order to identify the file as a real database. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 231 | ** 123456789 123456 */ |
| 232 | static const char zMagicHeader[] = "SQLite format 3"; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 233 | |
| 234 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 235 | ** Page type flags. An ORed combination of these flags appear as the |
| 236 | ** first byte of every BTree page. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 237 | */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 238 | #define PTF_INTKEY 0x01 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 239 | #define PTF_ZERODATA 0x02 |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 240 | #define PTF_LEAFDATA 0x04 |
| 241 | #define PTF_LEAF 0x08 |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 242 | |
| 243 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 244 | ** As each page of the file is loaded into memory, an instance of the following |
| 245 | ** structure is appended and initialized to zero. This structure stores |
| 246 | ** information about the page that is decoded from the raw file page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 247 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 248 | ** The pParent field points back to the parent page. This allows us to |
| 249 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 250 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 251 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 252 | */ |
| 253 | struct MemPage { |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 254 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 255 | u8 idxShift; /* True if Cell indices have changed */ |
| 256 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 257 | u8 intKey; /* True if intkey flag is set */ |
| 258 | u8 leaf; /* True if leaf flag is set */ |
| 259 | u8 zeroData; /* True if table stores keys only */ |
| 260 | u8 leafData; /* True if tables stores data on leaves only */ |
| 261 | u8 hasData; /* True if this page stores data */ |
| 262 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 263 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 264 | u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ |
| 265 | u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 266 | u16 cellOffset; /* Index in aData of first cell pointer */ |
| 267 | u16 idxParent; /* Index in parent of this node */ |
| 268 | u16 nFree; /* Number of free bytes on the page */ |
| 269 | u16 nCell; /* Number of cells on this page, local and ovfl */ |
| 270 | struct _OvflCell { /* Cells that will not fit on aData[] */ |
| 271 | u8 *pCell; /* Pointers to the body of the overflow cell */ |
| 272 | u16 idx; /* Insert this cell before idx-th non-overflow cell */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 273 | } aOvfl[5]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 274 | struct Btree *pBt; /* Pointer back to BTree structure */ |
| 275 | u8 *aData; /* Pointer back to the start of the page */ |
| 276 | Pgno pgno; /* Page number for this page */ |
| 277 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 278 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 279 | |
| 280 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 281 | ** The in-memory image of a disk page has the auxiliary information appended |
| 282 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 283 | ** that extra information. |
| 284 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 285 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 286 | |
| 287 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 288 | ** Everything we need to know about an open database |
| 289 | */ |
| 290 | struct Btree { |
| 291 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 292 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 293 | MemPage *pPage1; /* First page of the database */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 294 | u8 inTrans; /* True if a transaction is in progress */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 295 | u8 inStmt; /* True if we are in a statement subtransaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 296 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 297 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 298 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 299 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 300 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 301 | u16 pageSize; /* Total number of bytes on a page */ |
| 302 | u16 usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 303 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 304 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 305 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 306 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 307 | }; |
| 308 | typedef Btree Bt; |
| 309 | |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 310 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 311 | ** Btree.inTrans may take one of the following values. |
| 312 | */ |
| 313 | #define TRANS_NONE 0 |
| 314 | #define TRANS_READ 1 |
| 315 | #define TRANS_WRITE 2 |
| 316 | |
| 317 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 318 | ** An instance of the following structure is used to hold information |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 319 | ** about a cell. The parseCellPtr() function fills in this structure |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 320 | ** based on information extract from the raw disk page. |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 321 | */ |
| 322 | typedef struct CellInfo CellInfo; |
| 323 | struct CellInfo { |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 324 | u8 *pCell; /* Pointer to the start of cell content */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 325 | i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 326 | u32 nData; /* Number of bytes of data */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 327 | u16 nHeader; /* Size of the cell content header in bytes */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 328 | u16 nLocal; /* Amount of payload held locally */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 329 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 330 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | /* |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 334 | ** A cursor is a pointer to a particular entry in the BTree. |
| 335 | ** The entry is identified by its MemPage and the index in |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 336 | ** MemPage.aCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 337 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 338 | struct BtCursor { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 339 | Btree *pBt; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 340 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 341 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 342 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 343 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 344 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 345 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 346 | CellInfo info; /* A parse of the cell we are pointing at */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 347 | u8 wrFlag; /* True if writable */ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 348 | u8 isValid; /* TRUE if points to a valid entry */ |
| 349 | u8 status; /* Set to SQLITE_ABORT if cursors is invalidated */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 350 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 351 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 352 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 353 | ** Forward declaration |
| 354 | */ |
| 355 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
| 356 | |
| 357 | |
| 358 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 359 | ** Read or write a two- and four-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 360 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 361 | static u32 get2byte(unsigned char *p){ |
| 362 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 363 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 364 | static u32 get4byte(unsigned char *p){ |
| 365 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 366 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 367 | static void put2byte(unsigned char *p, u32 v){ |
| 368 | p[0] = v>>8; |
| 369 | p[1] = v; |
| 370 | } |
| 371 | static void put4byte(unsigned char *p, u32 v){ |
| 372 | p[0] = v>>24; |
| 373 | p[1] = v>>16; |
| 374 | p[2] = v>>8; |
| 375 | p[3] = v; |
| 376 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 377 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 378 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 379 | ** Routines to read and write variable-length integers. These used to |
| 380 | ** be defined locally, but now we use the varint routines in the util.c |
| 381 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 382 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 383 | #define getVarint sqlite3GetVarint |
| 384 | #define getVarint32 sqlite3GetVarint32 |
| 385 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 386 | |
| 387 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 388 | ** Given a btree page and a cell index (0 means the first cell on |
| 389 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 390 | ** to the cell content. |
| 391 | ** |
| 392 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 393 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 394 | static u8 *findCell(MemPage *pPage, int iCell){ |
| 395 | u8 *data = pPage->aData; |
| 396 | assert( iCell>=0 ); |
| 397 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
| 398 | return data + get2byte(&data[pPage->cellOffset+2*iCell]); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | ** This a more complex version of findCell() that works for |
| 403 | ** pages that do contain overflow cells. See insert |
| 404 | */ |
| 405 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 406 | int i; |
| 407 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 408 | int k; |
| 409 | struct _OvflCell *pOvfl; |
| 410 | pOvfl = &pPage->aOvfl[i]; |
| 411 | k = pOvfl->idx; |
| 412 | if( k<=iCell ){ |
| 413 | if( k==iCell ){ |
| 414 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 415 | } |
| 416 | iCell--; |
| 417 | } |
| 418 | } |
| 419 | return findCell(pPage, iCell); |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 424 | ** are two versions of this function. parseCell() takes a cell index |
| 425 | ** as the second argument and parseCellPtr() takes a pointer to the |
| 426 | ** body of the cell as its second argument. |
| 427 | */ |
| 428 | static void parseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 429 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 430 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 431 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 432 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 433 | int n; /* Number bytes in cell content header */ |
| 434 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 435 | |
| 436 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 437 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 438 | n = pPage->childPtrSize; |
| 439 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 440 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 441 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 442 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 443 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 444 | } |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 445 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 446 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 447 | pInfo->nData = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 448 | if( !pPage->intKey ){ |
| 449 | nPayload += pInfo->nKey; |
| 450 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 451 | if( nPayload<=pPage->maxLocal ){ |
| 452 | /* This is the (easy) common case where the entire payload fits |
| 453 | ** on the local page. No overflow is required. |
| 454 | */ |
| 455 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 456 | pInfo->nLocal = nPayload; |
| 457 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 458 | nSize = nPayload + n; |
| 459 | if( nSize<4 ){ |
| 460 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 461 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 462 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 463 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 464 | /* If the payload will not fit completely on the local page, we have |
| 465 | ** to decide how much to store locally and how much to spill onto |
| 466 | ** overflow pages. The strategy is to minimize the amount of unused |
| 467 | ** space on overflow pages while keeping the amount of local storage |
| 468 | ** in between minLocal and maxLocal. |
| 469 | ** |
| 470 | ** Warning: changing the way overflow payload is distributed in any |
| 471 | ** way will result in an incompatible file format. |
| 472 | */ |
| 473 | int minLocal; /* Minimum amount of payload held locally */ |
| 474 | int maxLocal; /* Maximum amount of payload held locally */ |
| 475 | int surplus; /* Overflow payload available for local storage */ |
| 476 | |
| 477 | minLocal = pPage->minLocal; |
| 478 | maxLocal = pPage->maxLocal; |
| 479 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 480 | if( surplus <= maxLocal ){ |
| 481 | pInfo->nLocal = surplus; |
| 482 | }else{ |
| 483 | pInfo->nLocal = minLocal; |
| 484 | } |
| 485 | pInfo->iOverflow = pInfo->nLocal + n; |
| 486 | pInfo->nSize = pInfo->iOverflow + 4; |
| 487 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 488 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 489 | static void parseCell( |
| 490 | MemPage *pPage, /* Page containing the cell */ |
| 491 | int iCell, /* The cell index. First cell is 0 */ |
| 492 | CellInfo *pInfo /* Fill in this structure */ |
| 493 | ){ |
| 494 | parseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 495 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 496 | |
| 497 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 498 | ** Compute the total number of bytes that a Cell needs in the cell |
| 499 | ** data area of the btree-page. The return number includes the cell |
| 500 | ** data header and the local payload, but not any overflow page or |
| 501 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 502 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 503 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 504 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 505 | CellInfo info; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 506 | parseCell(pPage, iCell, &info); |
| 507 | return info.nSize; |
| 508 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 509 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 510 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 511 | CellInfo info; |
| 512 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 513 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 517 | ** Do sanity checking on a page. Throw an exception if anything is |
| 518 | ** not right. |
| 519 | ** |
| 520 | ** This routine is used for internal error checking only. It is omitted |
| 521 | ** from most builds. |
| 522 | */ |
| 523 | #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0 |
| 524 | static void _pageIntegrity(MemPage *pPage){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 525 | int usableSize; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 526 | u8 *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 527 | int i, j, idx, c, pc, hdr, nFree; |
| 528 | int cellOffset; |
| 529 | int nCell, cellLimit; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 530 | u8 *used; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 531 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 532 | used = sqliteMallocRaw( pPage->pBt->pageSize ); |
| 533 | if( used==0 ) return; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 534 | usableSize = pPage->pBt->usableSize; |
| 535 | assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->pageSize] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 536 | hdr = pPage->hdrOffset; |
| 537 | assert( hdr==(pPage->pgno==1 ? 100 : 0) ); |
| 538 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
| 539 | c = pPage->aData[hdr]; |
| 540 | if( pPage->isInit ){ |
| 541 | assert( pPage->leaf == ((c & PTF_LEAF)!=0) ); |
| 542 | assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 543 | assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) ); |
| 544 | assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) ); |
| 545 | assert( pPage->hasData == |
| 546 | !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 547 | assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf ); |
| 548 | assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 549 | } |
| 550 | data = pPage->aData; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 551 | memset(used, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 552 | for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1; |
| 553 | nFree = 0; |
| 554 | pc = get2byte(&data[hdr+1]); |
| 555 | while( pc ){ |
| 556 | int size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 557 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 558 | size = get2byte(&data[pc+2]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 559 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 560 | nFree += size; |
| 561 | for(i=pc; i<pc+size; i++){ |
| 562 | assert( used[i]==0 ); |
| 563 | used[i] = 1; |
| 564 | } |
| 565 | pc = get2byte(&data[pc]); |
| 566 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 567 | idx = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 568 | nCell = get2byte(&data[hdr+3]); |
| 569 | cellLimit = get2byte(&data[hdr+5]); |
| 570 | assert( pPage->isInit==0 |
| 571 | || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) ); |
| 572 | cellOffset = pPage->cellOffset; |
| 573 | for(i=0; i<nCell; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 574 | int size; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 575 | pc = get2byte(&data[cellOffset+2*i]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 576 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 577 | size = cellSize(pPage, &data[pc]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 578 | assert( pc+size<=usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 579 | for(j=pc; j<pc+size; j++){ |
| 580 | assert( used[j]==0 ); |
| 581 | used[j] = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 582 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 583 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 584 | for(i=cellOffset+2*nCell; i<cellimit; i++){ |
| 585 | assert( used[i]==0 ); |
| 586 | used[i] = 1; |
| 587 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 588 | nFree = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 589 | for(i=0; i<usableSize; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 590 | assert( used[i]<=1 ); |
| 591 | if( used[i]==0 ) nFree++; |
| 592 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 593 | assert( nFree==data[hdr+7] ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 594 | sqliteFree(used); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 595 | } |
| 596 | #define pageIntegrity(X) _pageIntegrity(X) |
| 597 | #else |
| 598 | # define pageIntegrity(X) |
| 599 | #endif |
| 600 | |
| 601 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 602 | ** Defragment the page given. All Cells are moved to the |
| 603 | ** beginning of the page and all free space is collected |
| 604 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 605 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 606 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 607 | int i; /* Loop counter */ |
| 608 | int pc; /* Address of a i-th cell */ |
| 609 | int addr; /* Offset of first byte after cell pointer array */ |
| 610 | int hdr; /* Offset to the page header */ |
| 611 | int size; /* Size of a cell */ |
| 612 | int usableSize; /* Number of usable bytes on a page */ |
| 613 | int cellOffset; /* Offset to the cell pointer array */ |
| 614 | int brk; /* Offset to the cell content area */ |
| 615 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 616 | unsigned char *data; /* The page data */ |
| 617 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 618 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 619 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 620 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 621 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 622 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 623 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 624 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 625 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 626 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 627 | cellOffset = pPage->cellOffset; |
| 628 | nCell = pPage->nCell; |
| 629 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 630 | usableSize = pPage->pBt->usableSize; |
| 631 | brk = get2byte(&data[hdr+5]); |
| 632 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 633 | brk = usableSize; |
| 634 | for(i=0; i<nCell; i++){ |
| 635 | u8 *pAddr; /* The i-th cell pointer */ |
| 636 | pAddr = &data[cellOffset + i*2]; |
| 637 | pc = get2byte(pAddr); |
| 638 | assert( pc<pPage->pBt->usableSize ); |
| 639 | size = cellSizePtr(pPage, &temp[pc]); |
| 640 | brk -= size; |
| 641 | memcpy(&data[brk], &temp[pc], size); |
| 642 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 643 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 644 | assert( brk>=cellOffset+2*nCell ); |
| 645 | put2byte(&data[hdr+5], brk); |
| 646 | data[hdr+1] = 0; |
| 647 | data[hdr+2] = 0; |
| 648 | data[hdr+7] = 0; |
| 649 | addr = cellOffset+2*nCell; |
| 650 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 651 | sqliteFree(temp); |
| 652 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 653 | } |
| 654 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 655 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 656 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 657 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 658 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 659 | ** the new allocation. Or return 0 if there is not enough free |
| 660 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 661 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 662 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 663 | ** nBytes of contiguous free space, then this routine automatically |
| 664 | ** calls defragementPage() to consolidate all free space before |
| 665 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 666 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 667 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 668 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 669 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 670 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 671 | int top; |
| 672 | int nCell; |
| 673 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 674 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 675 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 676 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 677 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 678 | assert( pPage->pBt ); |
| 679 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 680 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 681 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 682 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 683 | |
| 684 | nFrag = data[hdr+7]; |
| 685 | if( nFrag<60 ){ |
| 686 | /* Search the freelist looking for a slot big enough to satisfy the |
| 687 | ** space request. */ |
| 688 | addr = hdr+1; |
| 689 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 690 | size = get2byte(&data[pc+2]); |
| 691 | if( size>=nByte ){ |
| 692 | if( size<nByte+4 ){ |
| 693 | memcpy(&data[addr], &data[pc], 2); |
| 694 | data[hdr+7] = nFrag + size - nByte; |
| 695 | return pc; |
| 696 | }else{ |
| 697 | put2byte(&data[pc+2], size-nByte); |
| 698 | return pc + size - nByte; |
| 699 | } |
| 700 | } |
| 701 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 704 | |
| 705 | /* Allocate memory from the gap in between the cell pointer array |
| 706 | ** and the cell content area. |
| 707 | */ |
| 708 | top = get2byte(&data[hdr+5]); |
| 709 | nCell = get2byte(&data[hdr+3]); |
| 710 | cellOffset = pPage->cellOffset; |
| 711 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 712 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 713 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 714 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 715 | top -= nByte; |
| 716 | assert( cellOffset + 2*nCell <= top ); |
| 717 | put2byte(&data[hdr+5], top); |
| 718 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 722 | ** Return a section of the pPage->aData to the freelist. |
| 723 | ** The first byte of the new free block is pPage->aDisk[start] |
| 724 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 725 | ** |
| 726 | ** Most of the effort here is involved in coalesing adjacent |
| 727 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 728 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 729 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 730 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 731 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 732 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 733 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 734 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 735 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 736 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 737 | if( size<4 ) size = 4; |
| 738 | |
| 739 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 740 | hdr = pPage->hdrOffset; |
| 741 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 742 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 743 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 744 | assert( pbegin>addr ); |
| 745 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 746 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 747 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 748 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 749 | put2byte(&data[addr], start); |
| 750 | put2byte(&data[start], pbegin); |
| 751 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 752 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 753 | |
| 754 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 755 | addr = pPage->hdrOffset + 1; |
| 756 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 757 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 758 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 759 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 760 | pnext = get2byte(&data[pbegin]); |
| 761 | psize = get2byte(&data[pbegin+2]); |
| 762 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 763 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 764 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 765 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 766 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 767 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 768 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 769 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 772 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 773 | /* If the cell content area begins with a freeblock, remove it. */ |
| 774 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 775 | int top; |
| 776 | pbegin = get2byte(&data[hdr+1]); |
| 777 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 778 | top = get2byte(&data[hdr+5]); |
| 779 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 780 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 784 | ** Decode the flags byte (the first byte of the header) for a page |
| 785 | ** and initialize fields of the MemPage structure accordingly. |
| 786 | */ |
| 787 | static void decodeFlags(MemPage *pPage, int flagByte){ |
| 788 | Btree *pBt; /* A copy of pPage->pBt */ |
| 789 | |
| 790 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 791 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 792 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 793 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 794 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 795 | pBt = pPage->pBt; |
| 796 | if( flagByte & PTF_LEAFDATA ){ |
| 797 | pPage->leafData = 1; |
| 798 | pPage->maxLocal = pBt->maxLeaf; |
| 799 | pPage->minLocal = pBt->minLeaf; |
| 800 | }else{ |
| 801 | pPage->leafData = 0; |
| 802 | pPage->maxLocal = pBt->maxLocal; |
| 803 | pPage->minLocal = pBt->minLocal; |
| 804 | } |
| 805 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 806 | } |
| 807 | |
| 808 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 809 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 810 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 811 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 812 | ** is the parent of the page being initialized. The root of a |
| 813 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 814 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 815 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 816 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 817 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 818 | ** guarantee that the page is well-formed. It only shows that |
| 819 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 820 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 821 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 822 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 823 | MemPage *pParent /* The parent. Might be NULL */ |
| 824 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 825 | int pc; /* Address of a freeblock within pPage->aData[] */ |
| 826 | int i; /* Loop counter */ |
| 827 | int hdr; /* Offset to beginning of page header */ |
| 828 | u8 *data; /* Equal to pPage->aData */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 829 | Btree *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 830 | int usableSize; /* Amount of usable space on each page */ |
| 831 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 832 | int nFree; /* Number of unused bytes on the page */ |
| 833 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 834 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 835 | pBt = pPage->pBt; |
| 836 | assert( pBt!=0 ); |
| 837 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 838 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 839 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 840 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 841 | /* The parent page should never change unless the file is corrupt */ |
| 842 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 843 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 844 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 845 | if( pPage->pParent==0 && pParent!=0 ){ |
| 846 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 847 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 848 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 849 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 850 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 851 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 852 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 853 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 854 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 855 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 856 | top = get2byte(&data[hdr+5]); |
| 857 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 858 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 859 | /* To many cells for a single page. The page must be corrupt */ |
| 860 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 861 | } |
| 862 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 863 | /* All pages must have at least one cell, except for root pages */ |
| 864 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 865 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 866 | |
| 867 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 868 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 869 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 870 | i = 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 871 | while( pc>0 ){ |
| 872 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 873 | if( pc>usableSize-4 ){ |
| 874 | /* Free block is off the page */ |
| 875 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 876 | } |
| 877 | if( i++>SQLITE_MAX_PAGE_SIZE/4 ){ |
| 878 | /* The free block list forms an infinite loop */ |
| 879 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 880 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 881 | next = get2byte(&data[pc]); |
| 882 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 883 | if( next>0 && next<=pc+size+3 ){ |
| 884 | /* Free blocks must be in accending order */ |
| 885 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 886 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 887 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 888 | pc = next; |
| 889 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 890 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 891 | if( nFree>=usableSize ){ |
| 892 | /* Free space cannot exceed total page size */ |
| 893 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 894 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 895 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 896 | pPage->isInit = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 897 | pageIntegrity(pPage); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 898 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 902 | ** Set up a raw page so that it looks like a database page holding |
| 903 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 904 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 905 | static void zeroPage(MemPage *pPage, int flags){ |
| 906 | unsigned char *data = pPage->aData; |
| 907 | Btree *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 908 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 909 | int first; |
| 910 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 911 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
| 912 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 913 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 914 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 915 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 916 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 917 | memset(&data[hdr+1], 0, 4); |
| 918 | data[hdr+7] = 0; |
| 919 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 920 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 921 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 922 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 923 | pPage->cellOffset = first; |
| 924 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 925 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 926 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 927 | pPage->isInit = 1; |
| 928 | pageIntegrity(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 932 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 933 | ** MemPage.aData elements if needed. |
| 934 | */ |
| 935 | static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){ |
| 936 | int rc; |
| 937 | unsigned char *aData; |
| 938 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 939 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 940 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 941 | pPage = (MemPage*)&aData[pBt->pageSize]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 942 | pPage->aData = aData; |
| 943 | pPage->pBt = pBt; |
| 944 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 945 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 946 | *ppPage = pPage; |
| 947 | return SQLITE_OK; |
| 948 | } |
| 949 | |
| 950 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 951 | ** Get a page from the pager and initialize it. This routine |
| 952 | ** is just a convenience wrapper around separate calls to |
| 953 | ** getPage() and initPage(). |
| 954 | */ |
| 955 | static int getAndInitPage( |
| 956 | Btree *pBt, /* The database file */ |
| 957 | Pgno pgno, /* Number of the page to get */ |
| 958 | MemPage **ppPage, /* Write the page pointer here */ |
| 959 | MemPage *pParent /* Parent of the page */ |
| 960 | ){ |
| 961 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 962 | if( pgno==0 ){ |
| 963 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 964 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 965 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 966 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 967 | rc = initPage(*ppPage, pParent); |
| 968 | } |
| 969 | return rc; |
| 970 | } |
| 971 | |
| 972 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 973 | ** Release a MemPage. This should be called once for each prior |
| 974 | ** call to getPage. |
| 975 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 976 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 977 | if( pPage ){ |
| 978 | assert( pPage->aData ); |
| 979 | assert( pPage->pBt ); |
| 980 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 981 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
| 985 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 986 | ** This routine is called when the reference count for a page |
| 987 | ** reaches zero. We need to unref the pParent pointer when that |
| 988 | ** happens. |
| 989 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 990 | static void pageDestructor(void *pData, int pageSize){ |
| 991 | MemPage *pPage = (MemPage*)&((char*)pData)[pageSize]; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 992 | if( pPage->pParent ){ |
| 993 | MemPage *pParent = pPage->pParent; |
| 994 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 995 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 996 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 997 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1001 | ** During a rollback, when the pager reloads information into the cache |
| 1002 | ** so that the cache is restored to its original state at the start of |
| 1003 | ** the transaction, for each page restored this routine is called. |
| 1004 | ** |
| 1005 | ** This routine needs to reset the extra data section at the end of the |
| 1006 | ** page to agree with the restored data. |
| 1007 | */ |
| 1008 | static void pageReinit(void *pData, int pageSize){ |
| 1009 | MemPage *pPage = (MemPage*)&((char*)pData)[pageSize]; |
| 1010 | if( pPage->isInit ){ |
| 1011 | pPage->isInit = 0; |
| 1012 | initPage(pPage, pPage->pParent); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1017 | ** Open a database file. |
| 1018 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1019 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1020 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1021 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1022 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1023 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1024 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 1025 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1026 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1027 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1028 | Btree *pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1029 | int rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1030 | int nReserve; |
| 1031 | unsigned char zDbHeader[100]; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1032 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1033 | /* |
| 1034 | ** The following asserts make sure that structures used by the btree are |
| 1035 | ** the right size. This is to guard against size changes that result |
| 1036 | ** when compiling on a different architecture. |
| 1037 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1038 | assert( sizeof(i64)==8 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1039 | assert( sizeof(u64)==8 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1040 | assert( sizeof(u32)==4 ); |
| 1041 | assert( sizeof(u16)==2 ); |
| 1042 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1043 | assert( sizeof(ptr)==sizeof(char*) ); |
| 1044 | assert( sizeof(uptr)==sizeof(ptr) ); |
| 1045 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1046 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1047 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1048 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1049 | return SQLITE_NOMEM; |
| 1050 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1051 | rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, |
| 1052 | (flags & BTREE_OMIT_JOURNAL)==0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1053 | if( rc!=SQLITE_OK ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1054 | if( pBt->pPager ) sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1055 | sqliteFree(pBt); |
| 1056 | *ppBtree = 0; |
| 1057 | return rc; |
| 1058 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1059 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1060 | sqlite3pager_set_reiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1061 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1062 | pBt->pPage1 = 0; |
| 1063 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1064 | sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader); |
| 1065 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1066 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){ |
| 1067 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1068 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1069 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1070 | pBt->minLeafFrac = 32; /* 12.5% */ |
| 1071 | nReserve = 0; |
| 1072 | }else{ |
| 1073 | nReserve = zDbHeader[20]; |
| 1074 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1075 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1076 | pBt->minLeafFrac = zDbHeader[23]; |
| 1077 | pBt->pageSizeFixed = 1; |
| 1078 | } |
| 1079 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1080 | sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1081 | *ppBtree = pBt; |
| 1082 | return SQLITE_OK; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | ** Close an open database and invalidate all cursors. |
| 1087 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1088 | int sqlite3BtreeClose(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1089 | while( pBt->pCursor ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1090 | sqlite3BtreeCloseCursor(pBt->pCursor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1091 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1092 | sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1093 | sqliteFree(pBt); |
| 1094 | return SQLITE_OK; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1098 | ** Change the busy handler callback function. |
| 1099 | */ |
| 1100 | int sqlite3BtreeSetBusyHandler(Btree *pBt, BusyHandler *pHandler){ |
| 1101 | sqlite3pager_set_busyhandler(pBt->pPager, pHandler); |
| 1102 | return SQLITE_OK; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1106 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1107 | ** |
| 1108 | ** The maximum number of cache pages is set to the absolute |
| 1109 | ** value of mxPage. If mxPage is negative, the pager will |
| 1110 | ** operate asynchronously - it will not stop to do fsync()s |
| 1111 | ** to insure data is written to the disk surface before |
| 1112 | ** continuing. Transactions still work if synchronous is off, |
| 1113 | ** and the database cannot be corrupted if this program |
| 1114 | ** crashes. But if the operating system crashes or there is |
| 1115 | ** an abrupt power failure when synchronous is off, the database |
| 1116 | ** could be left in an inconsistent and unrecoverable state. |
| 1117 | ** Synchronous is on by default so database corruption is not |
| 1118 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1119 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1120 | int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1121 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1122 | return SQLITE_OK; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1126 | ** Change the way data is synced to disk in order to increase or decrease |
| 1127 | ** how well the database resists damage due to OS crashes and power |
| 1128 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1129 | ** there is a high probability of damage) Level 2 is the default. There |
| 1130 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1131 | ** probability of damage to near zero but with a write performance reduction. |
| 1132 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1133 | int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1134 | sqlite3pager_set_safety_level(pBt->pPager, level); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1135 | return SQLITE_OK; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1139 | ** Change the default pages size and the number of reserved bytes per page. |
| 1140 | */ |
| 1141 | int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){ |
| 1142 | if( pBt->pageSizeFixed ){ |
| 1143 | return SQLITE_READONLY; |
| 1144 | } |
| 1145 | if( nReserve<0 ){ |
| 1146 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1147 | } |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1148 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1149 | pBt->pageSize = pageSize; |
| 1150 | sqlite3pager_set_pagesize(pBt->pPager, pageSize); |
| 1151 | } |
| 1152 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1153 | return SQLITE_OK; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | ** Return the currently defined page size |
| 1158 | */ |
| 1159 | int sqlite3BtreeGetPageSize(Btree *pBt){ |
| 1160 | return pBt->pageSize; |
| 1161 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1162 | int sqlite3BtreeGetReserve(Btree *pBt){ |
| 1163 | return pBt->pageSize - pBt->usableSize; |
| 1164 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1165 | |
| 1166 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1167 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1168 | ** also acquire a readlock on that file. |
| 1169 | ** |
| 1170 | ** SQLITE_OK is returned on success. If the file is not a |
| 1171 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1172 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1173 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1174 | ** if there is a locking protocol violation. |
| 1175 | */ |
| 1176 | static int lockBtree(Btree *pBt){ |
| 1177 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1178 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1179 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1180 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1181 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1182 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* Do some checking to help insure the file we opened really is |
| 1185 | ** a valid database file. |
| 1186 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1187 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1188 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1189 | u8 *page1 = pPage1->aData; |
| 1190 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1191 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1192 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1193 | if( page1[18]>1 || page1[19]>1 ){ |
| 1194 | goto page1_init_failed; |
| 1195 | } |
| 1196 | pBt->pageSize = get2byte(&page1[16]); |
| 1197 | pBt->usableSize = pBt->pageSize - page1[20]; |
| 1198 | if( pBt->usableSize<500 ){ |
| 1199 | goto page1_init_failed; |
| 1200 | } |
| 1201 | pBt->maxEmbedFrac = page1[21]; |
| 1202 | pBt->minEmbedFrac = page1[22]; |
| 1203 | pBt->minLeafFrac = page1[23]; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1204 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* maxLocal is the maximum amount of payload to store locally for |
| 1207 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1208 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1209 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1210 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1211 | ** 4-byte child pointer |
| 1212 | ** 9-byte nKey value |
| 1213 | ** 4-byte nData value |
| 1214 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1215 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1216 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1217 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1218 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1219 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1220 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1221 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1222 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1223 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1224 | goto page1_init_failed; |
| 1225 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1226 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1227 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1228 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1229 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1230 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1231 | releasePage(pPage1); |
| 1232 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1233 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1237 | ** If there are no outstanding cursors and we are not in the middle |
| 1238 | ** of a transaction but there is a read lock on the database, then |
| 1239 | ** this routine unrefs the first page of the database file which |
| 1240 | ** has the effect of releasing the read lock. |
| 1241 | ** |
| 1242 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1243 | ** |
| 1244 | ** If there is a transaction in progress, this routine is a no-op. |
| 1245 | */ |
| 1246 | static void unlockBtreeIfUnused(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1247 | if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1248 | if( pBt->pPage1->aData==0 ){ |
| 1249 | MemPage *pPage = pBt->pPage1; |
| 1250 | pPage->aData = &((char*)pPage)[-pBt->pageSize]; |
| 1251 | pPage->pBt = pBt; |
| 1252 | pPage->pgno = 1; |
| 1253 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1254 | releasePage(pBt->pPage1); |
| 1255 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1256 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1261 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1262 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1263 | */ |
| 1264 | static int newDatabase(Btree *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1265 | MemPage *pP1; |
| 1266 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1267 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1268 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1269 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1270 | assert( pP1!=0 ); |
| 1271 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1272 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1273 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1274 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1275 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1276 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1277 | data[18] = 1; |
| 1278 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1279 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1280 | data[21] = pBt->maxEmbedFrac; |
| 1281 | data[22] = pBt->minEmbedFrac; |
| 1282 | data[23] = pBt->minLeafFrac; |
| 1283 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1284 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1285 | pBt->pageSizeFixed = 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1286 | return SQLITE_OK; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1290 | ** Attempt to start a new transaction. A write-transaction |
| 1291 | ** is started if the second argument is true, otherwise a read- |
| 1292 | ** transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1293 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1294 | ** A write-transaction must be started before attempting any |
| 1295 | ** changes to the database. None of the following routines |
| 1296 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1297 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1298 | ** sqlite3BtreeCreateTable() |
| 1299 | ** sqlite3BtreeCreateIndex() |
| 1300 | ** sqlite3BtreeClearTable() |
| 1301 | ** sqlite3BtreeDropTable() |
| 1302 | ** sqlite3BtreeInsert() |
| 1303 | ** sqlite3BtreeDelete() |
| 1304 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1305 | ** |
| 1306 | ** If wrflag is true, then nMaster specifies the maximum length of |
| 1307 | ** a master journal file name supplied later via sqlite3BtreeSync(). |
| 1308 | ** This is so that appropriate space can be allocated in the journal file |
| 1309 | ** when it is created.. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1310 | */ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1311 | int sqlite3BtreeBeginTrans(Btree *pBt, int wrflag){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1312 | int rc = SQLITE_OK; |
| 1313 | |
| 1314 | /* If the btree is already in a write-transaction, or it |
| 1315 | ** is already in a read-transaction and a read-transaction |
| 1316 | ** is requested, this is a no-op. |
| 1317 | */ |
| 1318 | if( pBt->inTrans==TRANS_WRITE || |
| 1319 | (pBt->inTrans==TRANS_READ && !wrflag) ){ |
| 1320 | return SQLITE_OK; |
| 1321 | } |
| 1322 | if( pBt->readOnly && wrflag ){ |
| 1323 | return SQLITE_READONLY; |
| 1324 | } |
| 1325 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1326 | if( pBt->pPage1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1327 | rc = lockBtree(pBt); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1331 | rc = sqlite3pager_begin(pBt->pPage1->aData); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1332 | if( rc==SQLITE_OK ){ |
| 1333 | rc = newDatabase(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1334 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1335 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1336 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1337 | if( rc==SQLITE_OK ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1338 | pBt->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1339 | if( wrflag ) pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1340 | }else{ |
| 1341 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1342 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1343 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1347 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1348 | ** |
| 1349 | ** This will release the write lock on the database file. If there |
| 1350 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1351 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1352 | int sqlite3BtreeCommit(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1353 | int rc = SQLITE_OK; |
| 1354 | if( pBt->inTrans==TRANS_WRITE ){ |
| 1355 | rc = sqlite3pager_commit(pBt->pPager); |
| 1356 | } |
| 1357 | pBt->inTrans = TRANS_NONE; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1358 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1359 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1360 | return rc; |
| 1361 | } |
| 1362 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1363 | #ifndef NDEBUG |
| 1364 | /* |
| 1365 | ** Return the number of write-cursors open on this handle. This is for use |
| 1366 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 1367 | ** defined. |
| 1368 | */ |
| 1369 | static int countWriteCursors(Btree *pBt){ |
| 1370 | BtCursor *pCur; |
| 1371 | int r = 0; |
| 1372 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1373 | if( pCur->wrFlag ) r++; |
| 1374 | } |
| 1375 | return r; |
| 1376 | } |
| 1377 | #endif |
| 1378 | |
| 1379 | #if 0 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1380 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1381 | ** Invalidate all cursors |
| 1382 | */ |
| 1383 | static void invalidateCursors(Btree *pBt){ |
| 1384 | BtCursor *pCur; |
| 1385 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1386 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1387 | if( pPage /* && !pPage->isInit */ ){ |
| 1388 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1389 | releasePage(pPage); |
| 1390 | pCur->pPage = 0; |
| 1391 | pCur->isValid = 0; |
| 1392 | pCur->status = SQLITE_ABORT; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1396 | #endif |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1397 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1398 | #ifdef SQLITE_TEST |
| 1399 | /* |
| 1400 | ** Print debugging information about all cursors to standard output. |
| 1401 | */ |
| 1402 | void sqlite3BtreeCursorList(Btree *pBt){ |
| 1403 | BtCursor *pCur; |
| 1404 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1405 | MemPage *pPage = pCur->pPage; |
| 1406 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 1407 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 1408 | pCur, pCur->pgnoRoot, zMode, |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1409 | pPage ? pPage->pgno : 0, pCur->idx, |
| 1410 | pCur->isValid ? "" : " eof" |
| 1411 | ); |
| 1412 | } |
| 1413 | } |
| 1414 | #endif |
| 1415 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1416 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1417 | ** Rollback the transaction in progress. All cursors will be |
| 1418 | ** invalided by this operation. Any attempt to use a cursor |
| 1419 | ** that was open at the beginning of this operation will result |
| 1420 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1421 | ** |
| 1422 | ** This will release the write lock on the database file. If there |
| 1423 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1424 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1425 | int sqlite3BtreeRollback(Btree *pBt){ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1426 | int rc = SQLITE_OK; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1427 | MemPage *pPage1; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1428 | if( pBt->inTrans==TRANS_WRITE ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1429 | rc = sqlite3pager_rollback(pBt->pPager); |
| 1430 | /* The rollback may have destroyed the pPage1->aData value. So |
| 1431 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 1432 | ** set correctly. */ |
| 1433 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 1434 | releasePage(pPage1); |
| 1435 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1436 | assert( countWriteCursors(pBt)==0 ); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1437 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1438 | pBt->inTrans = TRANS_NONE; |
| 1439 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1440 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1441 | return rc; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1445 | ** Start a statement subtransaction. The subtransaction can |
| 1446 | ** can be rolled back independently of the main transaction. |
| 1447 | ** You must start a transaction before starting a subtransaction. |
| 1448 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1449 | ** commits or rolls back. |
| 1450 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1451 | ** Only one subtransaction may be active at a time. It is an error to try |
| 1452 | ** to start a new subtransaction if another subtransaction is already active. |
| 1453 | ** |
| 1454 | ** Statement subtransactions are used around individual SQL statements |
| 1455 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 1456 | ** error occurs within the statement, the effect of that one statement |
| 1457 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1458 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1459 | int sqlite3BtreeBeginStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1460 | int rc; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1461 | if( (pBt->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1462 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 1463 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1464 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1465 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1466 | return rc; |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1471 | ** Commit the statment subtransaction currently in progress. If no |
| 1472 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1473 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1474 | int sqlite3BtreeCommitStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1475 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1476 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1477 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1478 | }else{ |
| 1479 | rc = SQLITE_OK; |
| 1480 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1481 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1482 | return rc; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1486 | ** Rollback the active statement subtransaction. If no subtransaction |
| 1487 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1488 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1489 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1490 | ** to use a cursor that was open at the beginning of this operation |
| 1491 | ** will result in an error. |
| 1492 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1493 | int sqlite3BtreeRollbackStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1494 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1495 | if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1496 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1497 | assert( countWriteCursors(pBt)==0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1498 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1499 | return rc; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1503 | ** Default key comparison function to be used if no comparison function |
| 1504 | ** is specified on the sqlite3BtreeCursor() call. |
| 1505 | */ |
| 1506 | static int dfltCompare( |
| 1507 | void *NotUsed, /* User data is not used */ |
| 1508 | int n1, const void *p1, /* First key to compare */ |
| 1509 | int n2, const void *p2 /* Second key to compare */ |
| 1510 | ){ |
| 1511 | int c; |
| 1512 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 1513 | if( c==0 ){ |
| 1514 | c = n1 - n2; |
| 1515 | } |
| 1516 | return c; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1520 | ** Create a new cursor for the BTree whose root is on the page |
| 1521 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 1522 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1523 | ** |
| 1524 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1525 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 1526 | ** writing if other conditions for writing are also met. These |
| 1527 | ** are the conditions that must be met in order for writing to |
| 1528 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1529 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1530 | ** 1: The cursor must have been opened with wrFlag==1 |
| 1531 | ** |
| 1532 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 1533 | ** |
| 1534 | ** 3: The database must be writable (not on read-only media) |
| 1535 | ** |
| 1536 | ** 4: There must be an active transaction. |
| 1537 | ** |
| 1538 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 1539 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 1540 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 1541 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 1542 | ** change as long as the cursor is open. This allows the cursor to |
| 1543 | ** do a sequential scan of the table without having to worry about |
| 1544 | ** entries being inserted or deleted during the scan. Cursors should |
| 1545 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 1546 | ** That is to say, cursors should be opened with wrFlag==0 only if they |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1547 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1548 | ** should be opened with wrFlag==1 even if they never really intend |
| 1549 | ** to write. |
| 1550 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1551 | ** No checking is done to make sure that page iTable really is the |
| 1552 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 1553 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1554 | ** |
| 1555 | ** The comparison function must be logically the same for every cursor |
| 1556 | ** on a particular table. Changing the comparison function will result |
| 1557 | ** in incorrect operations. If the comparison function is NULL, a |
| 1558 | ** default comparison function is used. The comparison function is |
| 1559 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1560 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1561 | int sqlite3BtreeCursor( |
| 1562 | Btree *pBt, /* The btree */ |
| 1563 | int iTable, /* Root page of table to open */ |
| 1564 | int wrFlag, /* 1 to write. 0 read-only */ |
| 1565 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 1566 | void *pArg, /* First arg to xCompare() */ |
| 1567 | BtCursor **ppCur /* Write new cursor here */ |
| 1568 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1569 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 1570 | BtCursor *pCur; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1571 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 1572 | *ppCur = 0; |
| 1573 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 1574 | if( pBt->readOnly ){ |
| 1575 | return SQLITE_READONLY; |
| 1576 | } |
| 1577 | if( checkReadLocks(pBt, iTable, 0) ){ |
| 1578 | return SQLITE_LOCKED; |
| 1579 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 1580 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1581 | if( pBt->pPage1==0 ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1582 | rc = lockBtree(pBt); |
| 1583 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1584 | return rc; |
| 1585 | } |
| 1586 | } |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 1587 | pCur = sqliteMallocRaw( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1588 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1589 | rc = SQLITE_NOMEM; |
| 1590 | goto create_cursor_exception; |
| 1591 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1592 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1593 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 1594 | rc = SQLITE_EMPTY; |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 1595 | pCur->pPage = 0; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1596 | goto create_cursor_exception; |
| 1597 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 1598 | pCur->pPage = 0; /* For exit-handler, in case getAndInitPage() fails. */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1599 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1600 | if( rc!=SQLITE_OK ){ |
| 1601 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1602 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1603 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 1604 | pCur->pArg = pArg; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1605 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1606 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1607 | pCur->idx = 0; |
drh | 59eb676 | 2004-06-13 23:07:04 +0000 | [diff] [blame] | 1608 | memset(&pCur->info, 0, sizeof(pCur->info)); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1609 | pCur->pNext = pBt->pCursor; |
| 1610 | if( pCur->pNext ){ |
| 1611 | pCur->pNext->pPrev = pCur; |
| 1612 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1613 | pCur->pPrev = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1614 | pBt->pCursor = pCur; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1615 | pCur->isValid = 0; |
| 1616 | pCur->status = SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1617 | *ppCur = pCur; |
| 1618 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1619 | |
| 1620 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1621 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1622 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1623 | sqliteFree(pCur); |
| 1624 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1625 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1626 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 1629 | #if 0 /* Not Used */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1630 | /* |
| 1631 | ** Change the value of the comparison function used by a cursor. |
| 1632 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1633 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1634 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 1635 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 1636 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1637 | ){ |
| 1638 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 1639 | pCur->pArg = pArg; |
| 1640 | } |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 1641 | #endif |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1642 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1643 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1644 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1645 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1646 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1647 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1648 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1649 | if( pCur->pPrev ){ |
| 1650 | pCur->pPrev->pNext = pCur->pNext; |
| 1651 | }else{ |
| 1652 | pBt->pCursor = pCur->pNext; |
| 1653 | } |
| 1654 | if( pCur->pNext ){ |
| 1655 | pCur->pNext->pPrev = pCur->pPrev; |
| 1656 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1657 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1658 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1659 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1660 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1663 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1664 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 1665 | ** The temporary cursor is not on the cursor list for the Btree. |
| 1666 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1667 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1668 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 1669 | pTempCur->pNext = 0; |
| 1670 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1671 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1672 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1673 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1677 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1678 | ** function above. |
| 1679 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1680 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1681 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1682 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1683 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1687 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1688 | ** If it is not already valid, call parseCell() to fill it in. |
| 1689 | ** |
| 1690 | ** BtCursor.info is a cache of the information in the current cell. |
| 1691 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1692 | */ |
| 1693 | static void getCellInfo(BtCursor *pCur){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1694 | if( pCur->info.nSize==0 ){ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 1695 | parseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1696 | }else{ |
| 1697 | #ifndef NDEBUG |
| 1698 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1699 | memset(&info, 0, sizeof(info)); |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 1700 | parseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1701 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 1702 | #endif |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1707 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 1708 | ** the key for the current entry. If the cursor is not pointing |
| 1709 | ** to a valid entry, *pSize is set to 0. |
| 1710 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1711 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1712 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1713 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1714 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1715 | if( !pCur->isValid ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1716 | *pSize = 0; |
| 1717 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1718 | getCellInfo(pCur); |
| 1719 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1720 | } |
| 1721 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1722 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1723 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1724 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1725 | ** Set *pSize to the number of bytes of data in the entry the |
| 1726 | ** cursor currently points to. Always return SQLITE_OK. |
| 1727 | ** Failure is not possible. If the cursor is not currently |
| 1728 | ** pointing to an entry (which can happen, for example, if |
| 1729 | ** the database is empty) then *pSize is set to 0. |
| 1730 | */ |
| 1731 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1732 | if( !pCur->isValid ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1733 | /* Not pointing at a valid entry - set *pSize to 0. */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1734 | *pSize = 0; |
| 1735 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1736 | getCellInfo(pCur); |
| 1737 | *pSize = pCur->info.nData; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1738 | } |
| 1739 | return SQLITE_OK; |
| 1740 | } |
| 1741 | |
| 1742 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1743 | ** Read payload information from the entry that the pCur cursor is |
| 1744 | ** pointing to. Begin reading the payload at "offset" and read |
| 1745 | ** a total of "amt" bytes. Put the result in zBuf. |
| 1746 | ** |
| 1747 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1748 | ** It just reads bytes from the payload area. Data might appear |
| 1749 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1750 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1751 | static int getPayload( |
| 1752 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 1753 | int offset, /* Begin reading this far into payload */ |
| 1754 | int amt, /* Read this many bytes */ |
| 1755 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 1756 | int skipKey /* offset begins at data if this is true */ |
| 1757 | ){ |
| 1758 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1759 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1760 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1761 | MemPage *pPage; |
| 1762 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1763 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1764 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1765 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1766 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1767 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1768 | pBt = pCur->pBt; |
| 1769 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1770 | pageIntegrity(pPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1771 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1772 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1773 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1774 | aPayload += pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1775 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1776 | nKey = 0; |
| 1777 | }else{ |
| 1778 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1779 | } |
| 1780 | assert( offset>=0 ); |
| 1781 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1782 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1783 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1784 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1785 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1786 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1787 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1788 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1789 | if( a+offset>pCur->info.nLocal ){ |
| 1790 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1791 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1792 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1793 | if( a==amt ){ |
| 1794 | return SQLITE_OK; |
| 1795 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1796 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1797 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1798 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1799 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1800 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1801 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1802 | ovflSize = pBt->usableSize - 4; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1803 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1804 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1805 | while( amt>0 && nextPage ){ |
| 1806 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
| 1807 | if( rc!=0 ){ |
| 1808 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1809 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1810 | nextPage = get4byte(aPayload); |
| 1811 | if( offset<ovflSize ){ |
| 1812 | int a = amt; |
| 1813 | if( a + offset > ovflSize ){ |
| 1814 | a = ovflSize - offset; |
| 1815 | } |
| 1816 | memcpy(pBuf, &aPayload[offset+4], a); |
| 1817 | offset = 0; |
| 1818 | amt -= a; |
| 1819 | pBuf += a; |
| 1820 | }else{ |
| 1821 | offset -= ovflSize; |
| 1822 | } |
| 1823 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1824 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1825 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1826 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1827 | if( amt>0 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1828 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1829 | } |
| 1830 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1833 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1834 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1835 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1836 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1837 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1838 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1839 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1840 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1841 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1842 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1843 | if( pCur->isValid==0 ){ |
| 1844 | return pCur->status; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1845 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1846 | assert( pCur->pPage!=0 ); |
| 1847 | assert( pCur->pPage->intKey==0 ); |
| 1848 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1849 | return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 1850 | } |
| 1851 | |
| 1852 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1853 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1854 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1855 | ** begins at "offset". |
| 1856 | ** |
| 1857 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1858 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1859 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1860 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1861 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1862 | if( !pCur->isValid ){ |
| 1863 | return pCur->status ? pCur->status : SQLITE_INTERNAL; |
| 1864 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1865 | assert( pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1866 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1867 | return getPayload(pCur, offset, amt, pBuf, 1); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1870 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1871 | ** Return a pointer to payload information from the entry that the |
| 1872 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 1873 | ** the key if skipKey==0 and it points to the beginning of data if |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1874 | ** skipKey==1. The number of bytes of available key/data is written |
| 1875 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 1876 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1877 | ** |
| 1878 | ** This routine is an optimization. It is common for the entire key |
| 1879 | ** and data to fit on the local page and for there to be no overflow |
| 1880 | ** pages. When that is so, this routine can be used to access the |
| 1881 | ** key and data without making a copy. If the key and/or data spills |
| 1882 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 1883 | ** the key/data and copy it into a preallocated buffer. |
| 1884 | ** |
| 1885 | ** The pointer returned by this routine looks directly into the cached |
| 1886 | ** page of the database. The data might change or move the next time |
| 1887 | ** any btree routine is called. |
| 1888 | */ |
| 1889 | static const unsigned char *fetchPayload( |
| 1890 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1891 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1892 | int skipKey /* read beginning at data if this is true */ |
| 1893 | ){ |
| 1894 | unsigned char *aPayload; |
| 1895 | MemPage *pPage; |
| 1896 | Btree *pBt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1897 | u32 nKey; |
| 1898 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1899 | |
| 1900 | assert( pCur!=0 && pCur->pPage!=0 ); |
| 1901 | assert( pCur->isValid ); |
| 1902 | pBt = pCur->pBt; |
| 1903 | pPage = pCur->pPage; |
| 1904 | pageIntegrity(pPage); |
| 1905 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1906 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1907 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1908 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1909 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1910 | nKey = 0; |
| 1911 | }else{ |
| 1912 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1913 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1914 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1915 | aPayload += nKey; |
| 1916 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1917 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1918 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1919 | if( nLocal>nKey ){ |
| 1920 | nLocal = nKey; |
| 1921 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1922 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1923 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1924 | return aPayload; |
| 1925 | } |
| 1926 | |
| 1927 | |
| 1928 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1929 | ** For the entry that cursor pCur is point to, return as |
| 1930 | ** many bytes of the key or data as are available on the local |
| 1931 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1932 | ** |
| 1933 | ** The pointer returned is ephemeral. The key/data may move |
| 1934 | ** or be destroyed on the next call to any Btree routine. |
| 1935 | ** |
| 1936 | ** These routines is used to get quick access to key and data |
| 1937 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1938 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1939 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
| 1940 | return (const void*)fetchPayload(pCur, pAmt, 0); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1941 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 1942 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
| 1943 | return (const void*)fetchPayload(pCur, pAmt, 1); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1948 | ** Move the cursor down to a new child page. The newPgno argument is the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1949 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1950 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1951 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1952 | int rc; |
| 1953 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1954 | MemPage *pOldPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1955 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1956 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1957 | assert( pCur->isValid ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1958 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1959 | if( rc ) return rc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1960 | pageIntegrity(pNewPage); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1961 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1962 | pOldPage = pCur->pPage; |
| 1963 | pOldPage->idxShift = 0; |
| 1964 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1965 | pCur->pPage = pNewPage; |
| 1966 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1967 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 1968 | if( pNewPage->nCell<1 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1969 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 1970 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1971 | return SQLITE_OK; |
| 1972 | } |
| 1973 | |
| 1974 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1975 | ** Return true if the page is the virtual root of its table. |
| 1976 | ** |
| 1977 | ** The virtual root page is the root page for most tables. But |
| 1978 | ** for the table rooted on page 1, sometime the real root page |
| 1979 | ** is empty except for the right-pointer. In such cases the |
| 1980 | ** virtual root page is the page that the right-pointer of page |
| 1981 | ** 1 is pointing to. |
| 1982 | */ |
| 1983 | static int isRootPage(MemPage *pPage){ |
| 1984 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1985 | if( pParent==0 ) return 1; |
| 1986 | if( pParent->pgno>1 ) return 0; |
| 1987 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1988 | return 0; |
| 1989 | } |
| 1990 | |
| 1991 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1992 | ** Move the cursor up to the parent page. |
| 1993 | ** |
| 1994 | ** pCur->idx is set to the cell index that contains the pointer |
| 1995 | ** to the page we are coming from. If we are coming from the |
| 1996 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1997 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1998 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1999 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2000 | Pgno oldPgno; |
| 2001 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2002 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2003 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2004 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2005 | assert( pCur->isValid ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2006 | pPage = pCur->pPage; |
| 2007 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2008 | assert( !isRootPage(pPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2009 | pageIntegrity(pPage); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2010 | pParent = pPage->pParent; |
| 2011 | assert( pParent!=0 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2012 | pageIntegrity(pParent); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2013 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2014 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2015 | oldPgno = pPage->pgno; |
| 2016 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2017 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2018 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2019 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2020 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /* |
| 2024 | ** Move the cursor to the root page |
| 2025 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2026 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2027 | MemPage *pRoot; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2028 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2029 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2030 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2031 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2032 | if( rc ){ |
| 2033 | pCur->isValid = 0; |
| 2034 | return rc; |
| 2035 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2036 | releasePage(pCur->pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2037 | pageIntegrity(pRoot); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2038 | pCur->pPage = pRoot; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2039 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2040 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2041 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 2042 | Pgno subpage; |
| 2043 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2044 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2045 | assert( subpage>0 ); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 2046 | pCur->isValid = 1; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2047 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2048 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2049 | pCur->isValid = pCur->pPage->nCell>0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2050 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2051 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2052 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2053 | /* |
| 2054 | ** Move the cursor down to the left-most leaf entry beneath the |
| 2055 | ** entry to which it is currently pointing. |
| 2056 | */ |
| 2057 | static int moveToLeftmost(BtCursor *pCur){ |
| 2058 | Pgno pgno; |
| 2059 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2060 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2061 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2062 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2063 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2064 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2065 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2066 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2067 | if( rc ) return rc; |
| 2068 | } |
| 2069 | return SQLITE_OK; |
| 2070 | } |
| 2071 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2072 | /* |
| 2073 | ** Move the cursor down to the right-most leaf entry beneath the |
| 2074 | ** page to which it is currently pointing. Notice the difference |
| 2075 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 2076 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 2077 | ** finds the right-most entry beneath the *page*. |
| 2078 | */ |
| 2079 | static int moveToRightmost(BtCursor *pCur){ |
| 2080 | Pgno pgno; |
| 2081 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2082 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2083 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2084 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2085 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2086 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2087 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2088 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2089 | if( rc ) return rc; |
| 2090 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2091 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2092 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2093 | return SQLITE_OK; |
| 2094 | } |
| 2095 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2096 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 2097 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2098 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2099 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2100 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2101 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2102 | if( pCur->status ){ |
| 2103 | return pCur->status; |
| 2104 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2105 | rc = moveToRoot(pCur); |
| 2106 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2107 | if( pCur->isValid==0 ){ |
| 2108 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2109 | *pRes = 1; |
| 2110 | return SQLITE_OK; |
| 2111 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2112 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2113 | *pRes = 0; |
| 2114 | rc = moveToLeftmost(pCur); |
| 2115 | return rc; |
| 2116 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2117 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2118 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 2119 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2120 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2121 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2122 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2123 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2124 | if( pCur->status ){ |
| 2125 | return pCur->status; |
| 2126 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2127 | rc = moveToRoot(pCur); |
| 2128 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2129 | if( pCur->isValid==0 ){ |
| 2130 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2131 | *pRes = 1; |
| 2132 | return SQLITE_OK; |
| 2133 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2134 | assert( pCur->isValid ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2135 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2136 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2137 | return rc; |
| 2138 | } |
| 2139 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2140 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2141 | ** Return a success code. |
| 2142 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2143 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 2144 | ** ignored. For other tables, nKey is the number of bytes of data |
| 2145 | ** in nKey. The comparison function specified when the cursor was |
| 2146 | ** created is used to compare keys. |
| 2147 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2148 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2149 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2150 | ** were present. The cursor might point to an entry that comes |
| 2151 | ** before or after the key. |
| 2152 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2153 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2154 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2155 | ** this value is as follows: |
| 2156 | ** |
| 2157 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2158 | ** is smaller than pKey or if the table is empty |
| 2159 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2160 | ** |
| 2161 | ** *pRes==0 The cursor is left pointing at an entry that |
| 2162 | ** exactly matches pKey. |
| 2163 | ** |
| 2164 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2165 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2166 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2167 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2168 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2169 | |
| 2170 | if( pCur->status ){ |
| 2171 | return pCur->status; |
| 2172 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2173 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2174 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2175 | assert( pCur->pPage ); |
| 2176 | assert( pCur->pPage->isInit ); |
| 2177 | if( pCur->isValid==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2178 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2179 | assert( pCur->pPage->nCell==0 ); |
| 2180 | return SQLITE_OK; |
| 2181 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2182 | for(;;){ |
| 2183 | int lwr, upr; |
| 2184 | Pgno chldPg; |
| 2185 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2186 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2187 | lwr = 0; |
| 2188 | upr = pPage->nCell-1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2189 | pageIntegrity(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2190 | while( lwr<=upr ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2191 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2192 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2193 | pCur->idx = (lwr+upr)/2; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2194 | pCur->info.nSize = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2195 | sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2196 | if( pPage->intKey ){ |
| 2197 | if( nCellKey<nKey ){ |
| 2198 | c = -1; |
| 2199 | }else if( nCellKey>nKey ){ |
| 2200 | c = +1; |
| 2201 | }else{ |
| 2202 | c = 0; |
| 2203 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2204 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2205 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2206 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2207 | if( available>=nCellKey ){ |
| 2208 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2209 | }else{ |
| 2210 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 2211 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2212 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2213 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2214 | sqliteFree(pCellKey); |
| 2215 | if( rc ) return rc; |
| 2216 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2217 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2218 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2219 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 2220 | lwr = pCur->idx; |
| 2221 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2222 | break; |
| 2223 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2224 | if( pRes ) *pRes = 0; |
| 2225 | return SQLITE_OK; |
| 2226 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2227 | } |
| 2228 | if( c<0 ){ |
| 2229 | lwr = pCur->idx+1; |
| 2230 | }else{ |
| 2231 | upr = pCur->idx-1; |
| 2232 | } |
| 2233 | } |
| 2234 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2235 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2236 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2237 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2238 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2239 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2240 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2241 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2242 | } |
| 2243 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2244 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2245 | if( pRes ) *pRes = c; |
| 2246 | return SQLITE_OK; |
| 2247 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2248 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2249 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2250 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2251 | if( rc ){ |
| 2252 | return rc; |
| 2253 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2254 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2255 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2259 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 2260 | ** |
| 2261 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 2262 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 2263 | ** the first entry. TRUE is also returned if the table is empty. |
| 2264 | */ |
| 2265 | int sqlite3BtreeEof(BtCursor *pCur){ |
| 2266 | return pCur->isValid==0; |
| 2267 | } |
| 2268 | |
| 2269 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2270 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2271 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2272 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2273 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2274 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2275 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2276 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2277 | MemPage *pPage = pCur->pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2278 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2279 | assert( pRes!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2280 | if( pCur->isValid==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2281 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2282 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2283 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2284 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2285 | assert( pCur->idx<pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2286 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2287 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2288 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2289 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2290 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2291 | if( rc ) return rc; |
| 2292 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2293 | *pRes = 0; |
| 2294 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2295 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2296 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2297 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2298 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2299 | pCur->isValid = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2300 | return SQLITE_OK; |
| 2301 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2302 | moveToParent(pCur); |
| 2303 | pPage = pCur->pPage; |
| 2304 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2305 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2306 | if( pPage->leafData ){ |
| 2307 | rc = sqlite3BtreeNext(pCur, pRes); |
| 2308 | }else{ |
| 2309 | rc = SQLITE_OK; |
| 2310 | } |
| 2311 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2312 | } |
| 2313 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2314 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2315 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2316 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2317 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2318 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2321 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2322 | ** Step the cursor to the back to the previous entry in the database. If |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2323 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2324 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2325 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2326 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2327 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2328 | int rc; |
| 2329 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2330 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2331 | if( pCur->isValid==0 ){ |
| 2332 | *pRes = 1; |
| 2333 | return SQLITE_OK; |
| 2334 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2335 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2336 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2337 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2338 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2339 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2340 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2341 | if( rc ) return rc; |
| 2342 | rc = moveToRightmost(pCur); |
| 2343 | }else{ |
| 2344 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2345 | if( isRootPage(pPage) ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2346 | pCur->isValid = 0; |
| 2347 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2348 | return SQLITE_OK; |
| 2349 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2350 | moveToParent(pCur); |
| 2351 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2352 | } |
| 2353 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2354 | pCur->info.nSize = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2355 | if( pPage->leafData ){ |
| 2356 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 2357 | }else{ |
| 2358 | rc = SQLITE_OK; |
| 2359 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2360 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2361 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2362 | return rc; |
| 2363 | } |
| 2364 | |
| 2365 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2366 | ** The TRACE macro will print high-level status information about the |
| 2367 | ** btree operation when the global variable sqlite3_btree_trace is |
| 2368 | ** enabled. |
| 2369 | */ |
| 2370 | #if SQLITE_TEST |
drh | e54ca3f | 2004-06-07 01:52:14 +0000 | [diff] [blame] | 2371 | # define TRACE(X) if( sqlite3_btree_trace )\ |
| 2372 | { sqlite3DebugPrintf X; fflush(stdout); } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2373 | #else |
| 2374 | # define TRACE(X) |
| 2375 | #endif |
| 2376 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
| 2377 | |
| 2378 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2379 | ** Allocate a new page from the database file. |
| 2380 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2381 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2382 | ** has already been called on the new page.) The new page has also |
| 2383 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2384 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2385 | ** |
| 2386 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 2387 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2388 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2389 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2390 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 2391 | ** locate a page close to the page number "nearby". This can be used in an |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2392 | ** attempt to keep related pages close to each other in the database file, |
| 2393 | ** which in turn can make database access faster. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2394 | */ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2395 | static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2396 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2397 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2398 | int n; /* Number of pages on the freelist */ |
| 2399 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2400 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2401 | pPage1 = pBt->pPage1; |
| 2402 | n = get4byte(&pPage1->aData[36]); |
| 2403 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2404 | /* There are pages on the freelist. Reuse one of those pages. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2405 | MemPage *pTrunk; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2406 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2407 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2408 | put4byte(&pPage1->aData[36], n-1); |
| 2409 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2410 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2411 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2412 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2413 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2414 | return rc; |
| 2415 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2416 | k = get4byte(&pTrunk->aData[4]); |
| 2417 | if( k==0 ){ |
| 2418 | /* The trunk has no leaves. So extract the trunk page itself and |
| 2419 | ** use it as the newly allocated page */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2420 | *pPgno = get4byte(&pPage1->aData[32]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2421 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 2422 | *ppPage = pTrunk; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2423 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2424 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 2425 | /* Value of k is out of range. Database corruption */ |
| 2426 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2427 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2428 | /* Extract a leaf from the trunk */ |
| 2429 | int closest; |
| 2430 | unsigned char *aData = pTrunk->aData; |
| 2431 | if( nearby>0 ){ |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2432 | int i, dist; |
| 2433 | closest = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2434 | dist = get4byte(&aData[8]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2435 | if( dist<0 ) dist = -dist; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2436 | for(i=1; i<k; i++){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2437 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2438 | if( d2<0 ) d2 = -d2; |
| 2439 | if( d2<dist ) closest = i; |
| 2440 | } |
| 2441 | }else{ |
| 2442 | closest = 0; |
| 2443 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2444 | *pPgno = get4byte(&aData[8+closest*4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2445 | if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 2446 | /* Free page off the end of the file */ |
| 2447 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 2448 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2449 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d: %d more free pages\n", |
| 2450 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2451 | if( closest<k-1 ){ |
| 2452 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 2453 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2454 | put4byte(&aData[4], k-1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2455 | rc = getPage(pBt, *pPgno, ppPage); |
| 2456 | releasePage(pTrunk); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2457 | if( rc==SQLITE_OK ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2458 | sqlite3pager_dont_rollback((*ppPage)->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2459 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2462 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2463 | /* There are no pages on the freelist, so create a new page at the |
| 2464 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2465 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2466 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2467 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2468 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2469 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2470 | } |
| 2471 | return rc; |
| 2472 | } |
| 2473 | |
| 2474 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2475 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2476 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2477 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2478 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2479 | static int freePage(MemPage *pPage){ |
| 2480 | Btree *pBt = pPage->pBt; |
| 2481 | MemPage *pPage1 = pBt->pPage1; |
| 2482 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2483 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2484 | /* Prepare the page for freeing */ |
| 2485 | assert( pPage->pgno>1 ); |
| 2486 | pPage->isInit = 0; |
| 2487 | releasePage(pPage->pParent); |
| 2488 | pPage->pParent = 0; |
| 2489 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2490 | /* Increment the free page count on pPage1 */ |
| 2491 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2492 | if( rc ) return rc; |
| 2493 | n = get4byte(&pPage1->aData[36]); |
| 2494 | put4byte(&pPage1->aData[36], n+1); |
| 2495 | |
| 2496 | if( n==0 ){ |
| 2497 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2498 | rc = sqlite3pager_write(pPage->aData); |
| 2499 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2500 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2501 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2502 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2503 | }else{ |
| 2504 | /* Other free pages already exist. Retrive the first trunk page |
| 2505 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2506 | MemPage *pTrunk; |
| 2507 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2508 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2509 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2510 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2511 | /* The trunk is full. Turn the page being freed into a new |
| 2512 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2513 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2514 | if( rc ) return rc; |
| 2515 | put4byte(pPage->aData, pTrunk->pgno); |
| 2516 | put4byte(&pPage->aData[4], 0); |
| 2517 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2518 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 2519 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2520 | }else{ |
| 2521 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2522 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2523 | if( rc ) return rc; |
| 2524 | put4byte(&pTrunk->aData[4], k+1); |
| 2525 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2526 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2527 | TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2528 | } |
| 2529 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2530 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2531 | return rc; |
| 2532 | } |
| 2533 | |
| 2534 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2535 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2536 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2537 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 2538 | Btree *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2539 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2540 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2541 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2542 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2543 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2544 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2545 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2546 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2547 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2548 | while( ovflPgno!=0 ){ |
| 2549 | MemPage *pOvfl; |
| 2550 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2551 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2552 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2553 | rc = freePage(pOvfl); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2554 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2555 | sqlite3pager_unref(pOvfl->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2556 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2557 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2561 | ** Create the byte sequence used to represent a cell on page pPage |
| 2562 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 2563 | ** allocated and filled in as necessary. The calling procedure |
| 2564 | ** is responsible for making sure sufficient space has been allocated |
| 2565 | ** for pCell[]. |
| 2566 | ** |
| 2567 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 2568 | ** area. pCell might point to some temporary storage. The cell will |
| 2569 | ** be constructed in this temporary area then copied into pPage->aData |
| 2570 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2571 | */ |
| 2572 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2573 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2574 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2575 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2576 | const void *pData,int nData, /* The data */ |
| 2577 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2578 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2579 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 2580 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2581 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2582 | int spaceLeft; |
| 2583 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2584 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2585 | unsigned char *pPrior; |
| 2586 | unsigned char *pPayload; |
| 2587 | Btree *pBt = pPage->pBt; |
| 2588 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2589 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2590 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2591 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2592 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2593 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2594 | if( !pPage->leaf ){ |
| 2595 | nHeader += 4; |
| 2596 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2597 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2598 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2599 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2600 | nData = 0; |
| 2601 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2602 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2603 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2604 | assert( info.nHeader==nHeader ); |
| 2605 | assert( info.nKey==nKey ); |
| 2606 | assert( info.nData==nData ); |
| 2607 | |
| 2608 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2609 | nPayload = nData; |
| 2610 | if( pPage->intKey ){ |
| 2611 | pSrc = pData; |
| 2612 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2613 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2614 | }else{ |
| 2615 | nPayload += nKey; |
| 2616 | pSrc = pKey; |
| 2617 | nSrc = nKey; |
| 2618 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2619 | *pnSize = info.nSize; |
| 2620 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2621 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2622 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2623 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2624 | while( nPayload>0 ){ |
| 2625 | if( spaceLeft==0 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2626 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2627 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2628 | releasePage(pToRelease); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2629 | clearCell(pPage, pCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2630 | return rc; |
| 2631 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2632 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2633 | releasePage(pToRelease); |
| 2634 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2635 | pPrior = pOvfl->aData; |
| 2636 | put4byte(pPrior, 0); |
| 2637 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2638 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2639 | } |
| 2640 | n = nPayload; |
| 2641 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2642 | if( n>nSrc ) n = nSrc; |
| 2643 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2644 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2645 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2646 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2647 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2648 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2649 | if( nSrc==0 ){ |
| 2650 | nSrc = nData; |
| 2651 | pSrc = pData; |
| 2652 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2653 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2654 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2655 | return SQLITE_OK; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2659 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2660 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2661 | ** pointer in the third argument. |
| 2662 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2663 | static void reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2664 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2665 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2666 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2667 | if( pgno==0 ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2668 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2669 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2670 | if( aData ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2671 | pThis = (MemPage*)&aData[pBt->usableSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2672 | if( pThis->isInit ){ |
| 2673 | if( pThis->pParent!=pNewParent ){ |
| 2674 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 2675 | pThis->pParent = pNewParent; |
| 2676 | if( pNewParent ) sqlite3pager_ref(pNewParent->aData); |
| 2677 | } |
| 2678 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2679 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2680 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2685 | ** Change the pParent pointer of all children of pPage to point back |
| 2686 | ** to pPage. |
| 2687 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2688 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2689 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2690 | ** |
| 2691 | ** This routine gets called after you memcpy() one page into |
| 2692 | ** another. |
| 2693 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2694 | static void reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2695 | int i; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2696 | Btree *pBt; |
| 2697 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2698 | if( pPage->leaf ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2699 | pBt = pPage->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2700 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2701 | reparentPage(pBt, get4byte(findCell(pPage,i)), pPage, i); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2702 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2703 | reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), pPage, i); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2704 | pPage->idxShift = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | /* |
| 2708 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 2709 | ** The cell content is not freed or deallocated. It is assumed that |
| 2710 | ** the cell content has been copied someplace else. This routine just |
| 2711 | ** removes the reference to the cell from pPage. |
| 2712 | ** |
| 2713 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2714 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2715 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2716 | int i; /* Loop counter */ |
| 2717 | int pc; /* Offset to cell content of cell being deleted */ |
| 2718 | u8 *data; /* pPage->aData */ |
| 2719 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 2720 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2721 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2722 | assert( sz==cellSize(pPage, idx) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2723 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2724 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2725 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 2726 | pc = get2byte(ptr); |
| 2727 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2728 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2729 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 2730 | ptr[0] = ptr[2]; |
| 2731 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2732 | } |
| 2733 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2734 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 2735 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2736 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | /* |
| 2740 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 2741 | ** content of the cell. |
| 2742 | ** |
| 2743 | ** If the cell content will fit on the page, then put it there. If it |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2744 | ** will not fit, then make a copy of the cell content into pTemp if |
| 2745 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 2746 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 2747 | ** in pTemp or the original pCell) and also record its index. |
| 2748 | ** Allocating a new entry in pPage->aCell[] implies that |
| 2749 | ** pPage->nOverflow is incremented. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2750 | */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2751 | static void insertCell( |
| 2752 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2753 | int i, /* New cell becomes the i-th cell of the page */ |
| 2754 | u8 *pCell, /* Content of the new cell */ |
| 2755 | int sz, /* Bytes of content in pCell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2756 | u8 *pTemp /* Temp storage space for pCell, if needed */ |
| 2757 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2758 | int idx; /* Where to write new cell content in data[] */ |
| 2759 | int j; /* Loop counter */ |
| 2760 | int top; /* First byte of content for any cell in data[] */ |
| 2761 | int end; /* First byte past the last cell pointer in data[] */ |
| 2762 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 2763 | int hdr; /* Offset into data[] of the page header */ |
| 2764 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 2765 | u8 *data; /* The content of the whole page */ |
| 2766 | u8 *ptr; /* Used for moving information around in data[] */ |
| 2767 | |
| 2768 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 2769 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2770 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2771 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2772 | if( pTemp ){ |
| 2773 | memcpy(pTemp, pCell, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2774 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2775 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2776 | j = pPage->nOverflow++; |
| 2777 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 2778 | pPage->aOvfl[j].pCell = pCell; |
| 2779 | pPage->aOvfl[j].idx = i; |
| 2780 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2781 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2782 | data = pPage->aData; |
| 2783 | hdr = pPage->hdrOffset; |
| 2784 | top = get2byte(&data[hdr+5]); |
| 2785 | cellOffset = pPage->cellOffset; |
| 2786 | end = cellOffset + 2*pPage->nCell + 2; |
| 2787 | ins = cellOffset + 2*i; |
| 2788 | if( end > top - sz ){ |
| 2789 | defragmentPage(pPage); |
| 2790 | top = get2byte(&data[hdr+5]); |
| 2791 | assert( end + sz <= top ); |
| 2792 | } |
| 2793 | idx = allocateSpace(pPage, sz); |
| 2794 | assert( idx>0 ); |
| 2795 | assert( end <= get2byte(&data[hdr+5]) ); |
| 2796 | pPage->nCell++; |
| 2797 | pPage->nFree -= 2; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2798 | memcpy(&data[idx], pCell, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2799 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 2800 | ptr[0] = ptr[-2]; |
| 2801 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2802 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2803 | put2byte(&data[ins], idx); |
| 2804 | put2byte(&data[hdr+3], pPage->nCell); |
| 2805 | pPage->idxShift = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2806 | pageIntegrity(pPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2811 | ** Add a list of cells to a page. The page should be initially empty. |
| 2812 | ** The cells are guaranteed to fit on the page. |
| 2813 | */ |
| 2814 | static void assemblePage( |
| 2815 | MemPage *pPage, /* The page to be assemblied */ |
| 2816 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2817 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2818 | int *aSize /* Sizes of the cells */ |
| 2819 | ){ |
| 2820 | int i; /* Loop counter */ |
| 2821 | int totalSize; /* Total size of all cells */ |
| 2822 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2823 | int cellptr; /* Address of next cell pointer */ |
| 2824 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2825 | u8 *data; /* Data for the page */ |
| 2826 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2827 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2828 | totalSize = 0; |
| 2829 | for(i=0; i<nCell; i++){ |
| 2830 | totalSize += aSize[i]; |
| 2831 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2832 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2833 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2834 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2835 | data = pPage->aData; |
| 2836 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2837 | put2byte(&data[hdr+3], nCell); |
| 2838 | cellbody = allocateSpace(pPage, totalSize); |
| 2839 | assert( cellbody>0 ); |
| 2840 | assert( pPage->nFree >= 2*nCell ); |
| 2841 | pPage->nFree -= 2*nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2842 | for(i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2843 | put2byte(&data[cellptr], cellbody); |
| 2844 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 2845 | cellptr += 2; |
| 2846 | cellbody += aSize[i]; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2847 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2848 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2849 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2852 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2853 | ** GCC does not define the offsetof() macro so we'll have to do it |
| 2854 | ** ourselves. |
| 2855 | */ |
| 2856 | #ifndef offsetof |
| 2857 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 2858 | #endif |
| 2859 | |
| 2860 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2861 | ** The following parameters determine how many adjacent pages get involved |
| 2862 | ** in a balancing operation. NN is the number of neighbors on either side |
| 2863 | ** of the page that participate in the balancing operation. NB is the |
| 2864 | ** total number of pages that participate, including the target page and |
| 2865 | ** NN neighbors on either side. |
| 2866 | ** |
| 2867 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 2868 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 2869 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 2870 | ** The value of NN appears to give the best results overall. |
| 2871 | */ |
| 2872 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 2873 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 2874 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2875 | /* Forward reference */ |
| 2876 | static int balance(MemPage*); |
| 2877 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2878 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2879 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2880 | ** of pPage so that all pages have about the same amount of free space. |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 2881 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 2882 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2883 | ** or last child of its parent. If pPage has fewer than 2*NN siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2884 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2885 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2886 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 2887 | ** The number of siblings of pPage might be increased or decreased by one or |
| 2888 | ** two in an effort to keep pages nearly full but not over full. The root page |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2889 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2890 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2891 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2892 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2893 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2894 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2895 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2896 | ** if the page is overfull. Part of the job of this routine is to |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2897 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2898 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2899 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 2900 | ** might become overfull or underfull. If that happens, then this routine |
| 2901 | ** is called recursively on the parent. |
| 2902 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2903 | ** If this routine fails for any reason, it might leave the database |
| 2904 | ** in a corrupted state. So if this routine fails, the database should |
| 2905 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2906 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2907 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2908 | MemPage *pParent; /* The parent of pPage */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2909 | Btree *pBt; /* The whole database */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2910 | int nCell = 0; /* Number of cells in aCell[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2911 | int nOld; /* Number of pages in apOld[] */ |
| 2912 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2913 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2914 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2915 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 2916 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2917 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2918 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2919 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2920 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 2921 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2922 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2923 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2924 | int mxCellPerPage; /* Maximum number of cells in one page */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2925 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 2926 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2927 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 2928 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 2929 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2930 | int idxDiv[NB]; /* Indices of divider cells in pParent */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2931 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 2932 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 2933 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2934 | u8 **apCell; /* All cells begin balanced */ |
| 2935 | int *szCell; /* Local size of all cells in apCell[] */ |
| 2936 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 2937 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2938 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2939 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2940 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2941 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2942 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2943 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2944 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2945 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2946 | sqlite3pager_write(pParent->aData); |
| 2947 | assert( pParent ); |
| 2948 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 2949 | |
| 2950 | /* |
| 2951 | ** Allocate space for memory structures |
| 2952 | */ |
| 2953 | mxCellPerPage = MX_CELL(pBt); |
| 2954 | apCell = sqliteMallocRaw( |
| 2955 | (mxCellPerPage+2)*NB*(sizeof(u8*)+sizeof(int)) |
| 2956 | + sizeof(MemPage)*NB |
| 2957 | + pBt->pageSize*(5+NB) |
| 2958 | ); |
| 2959 | if( apCell==0 ){ |
| 2960 | return SQLITE_NOMEM; |
| 2961 | } |
| 2962 | szCell = (int*)&apCell[(mxCellPerPage+2)*NB]; |
| 2963 | aCopy[0] = (u8*)&szCell[(mxCellPerPage+2)*NB]; |
| 2964 | for(i=1; i<NB; i++){ |
| 2965 | aCopy[i] = &aCopy[i-1][pBt->pageSize+sizeof(MemPage)]; |
| 2966 | } |
| 2967 | aSpace = &aCopy[NB-1][pBt->pageSize+sizeof(MemPage)]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2968 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2969 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2970 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2971 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 2972 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2973 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2974 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2975 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2976 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2977 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2978 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2979 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2980 | break; |
| 2981 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2982 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2983 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2984 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2985 | }else{ |
| 2986 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2987 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2988 | |
| 2989 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2990 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2991 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2992 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2993 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2994 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2995 | |
| 2996 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2997 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2998 | ** the siblings. An attempt is made to find NN siblings on either |
| 2999 | ** side of pPage. More siblings are taken from one side, however, if |
| 3000 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 3001 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3002 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3003 | nxDiv = idx - NN; |
| 3004 | if( nxDiv + NB > pParent->nCell ){ |
| 3005 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3006 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3007 | if( nxDiv<0 ){ |
| 3008 | nxDiv = 0; |
| 3009 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3010 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3011 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3012 | if( k<pParent->nCell ){ |
| 3013 | idxDiv[i] = k; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3014 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3015 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3016 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3017 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3018 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3019 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3020 | }else{ |
| 3021 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3022 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3023 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3024 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3025 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3026 | apCopy[i] = 0; |
| 3027 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3028 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3032 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 3033 | ** The rest of this function will use data from the copies rather |
| 3034 | ** that the original pages since the original pages will be in the |
| 3035 | ** process of being overwritten. |
| 3036 | */ |
| 3037 | for(i=0; i<nOld; i++){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3038 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3039 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 3040 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 3041 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | /* |
| 3045 | ** Load pointers to all cells on sibling pages and the divider cells |
| 3046 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3047 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 3048 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3049 | ** |
| 3050 | ** If the siblings are on leaf pages, then the child pointers of the |
| 3051 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3052 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3053 | ** child pointers. If siblings are not leaves, then all cell in |
| 3054 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 3055 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3056 | ** |
| 3057 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 3058 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3059 | */ |
| 3060 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3061 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3062 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3063 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3064 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3065 | int limit = pOld->nCell+pOld->nOverflow; |
| 3066 | for(j=0; j<limit; j++){ |
| 3067 | apCell[nCell] = findOverflowCell(pOld, j); |
| 3068 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3069 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3070 | } |
| 3071 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3072 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3073 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3074 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 3075 | ** are duplicates of keys on the child pages. We need to remove |
| 3076 | ** the divider cells from pParent, but the dividers cells are not |
| 3077 | ** added to apCell[] because they are duplicates of child cells. |
| 3078 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3079 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3080 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3081 | u8 *pTemp; |
| 3082 | szCell[nCell] = sz; |
| 3083 | pTemp = &aSpace[iSpace]; |
| 3084 | iSpace += sz; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3085 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3086 | memcpy(pTemp, apDiv[i], sz); |
| 3087 | apCell[nCell] = pTemp+leafCorrection; |
| 3088 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3089 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3090 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3091 | if( !pOld->leaf ){ |
| 3092 | assert( leafCorrection==0 ); |
| 3093 | /* The right pointer of the child page pOld becomes the left |
| 3094 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3095 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3096 | }else{ |
| 3097 | assert( leafCorrection==4 ); |
| 3098 | } |
| 3099 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3100 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3105 | ** Figure out the number of pages needed to hold all nCell cells. |
| 3106 | ** Store this number in "k". Also compute szNew[] which is the total |
| 3107 | ** size of all cells on the i-th page and cntNew[] which is the index |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3108 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3109 | ** cntNew[k] should equal nCell. |
| 3110 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3111 | ** Values computed by this block: |
| 3112 | ** |
| 3113 | ** k: The total number of sibling pages |
| 3114 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 3115 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 3116 | ** the right of the i-th sibling page. |
| 3117 | ** usableSpace: Number of bytes of space available on each sibling. |
| 3118 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3119 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3120 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3121 | for(subtotal=k=i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3122 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3123 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3124 | szNew[k] = subtotal - szCell[i]; |
| 3125 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3126 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3127 | subtotal = 0; |
| 3128 | k++; |
| 3129 | } |
| 3130 | } |
| 3131 | szNew[k] = subtotal; |
| 3132 | cntNew[k] = nCell; |
| 3133 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3134 | |
| 3135 | /* |
| 3136 | ** The packing computed by the previous block is biased toward the siblings |
| 3137 | ** on the left side. The left siblings are always nearly full, while the |
| 3138 | ** right-most sibling might be nearly empty. This block of code attempts |
| 3139 | ** to adjust the packing of siblings to get a better balance. |
| 3140 | ** |
| 3141 | ** This adjustment is more than an optimization. The packing above might |
| 3142 | ** be so out of balance as to be illegal. For example, the right-most |
| 3143 | ** sibling might be completely empty. This adjustment is not optional. |
| 3144 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3145 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3146 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 3147 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 3148 | int r; /* Index of right-most cell in left sibling */ |
| 3149 | int d; /* Index of first cell to the left of right sibling */ |
| 3150 | |
| 3151 | r = cntNew[i-1] - 1; |
| 3152 | d = r + 1 - leafData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3153 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 3154 | szRight += szCell[d] + 2; |
| 3155 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3156 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3157 | r = cntNew[i-1] - 1; |
| 3158 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3159 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3160 | szNew[i] = szRight; |
| 3161 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3162 | } |
| 3163 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3164 | |
| 3165 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3166 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3167 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3168 | assert( pPage->pgno>1 ); |
| 3169 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3170 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3171 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3172 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3173 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3174 | pgnoNew[i] = pgnoOld[i]; |
| 3175 | apOld[i] = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3176 | sqlite3pager_write(pNew->aData); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3177 | }else{ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3178 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3179 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3180 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3181 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3182 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3183 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3186 | /* Free any old pages that were not reused as new pages. |
| 3187 | */ |
| 3188 | while( i<nOld ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3189 | rc = freePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3190 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3191 | releasePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3192 | apOld[i] = 0; |
| 3193 | i++; |
| 3194 | } |
| 3195 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3196 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3197 | ** Put the new pages in accending order. This helps to |
| 3198 | ** keep entries in the disk file in order so that a scan |
| 3199 | ** of the table is a linear scan through the file. That |
| 3200 | ** in turn helps the operating system to deliver pages |
| 3201 | ** from the disk more rapidly. |
| 3202 | ** |
| 3203 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3204 | ** n is never more than NB (a small constant), that should |
| 3205 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3206 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3207 | ** When NB==3, this one optimization makes the database |
| 3208 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3209 | */ |
| 3210 | for(i=0; i<k-1; i++){ |
| 3211 | int minV = pgnoNew[i]; |
| 3212 | int minI = i; |
| 3213 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 3214 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3215 | minI = j; |
| 3216 | minV = pgnoNew[j]; |
| 3217 | } |
| 3218 | } |
| 3219 | if( minI>i ){ |
| 3220 | int t; |
| 3221 | MemPage *pT; |
| 3222 | t = pgnoNew[i]; |
| 3223 | pT = apNew[i]; |
| 3224 | pgnoNew[i] = pgnoNew[minI]; |
| 3225 | apNew[i] = apNew[minI]; |
| 3226 | pgnoNew[minI] = t; |
| 3227 | apNew[minI] = pT; |
| 3228 | } |
| 3229 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3230 | TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3231 | pgnoOld[0], |
| 3232 | nOld>=2 ? pgnoOld[1] : 0, |
| 3233 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 3234 | pgnoNew[0], szNew[0], |
| 3235 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 3236 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3237 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 3238 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3239 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3240 | |
| 3241 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3242 | ** Evenly distribute the data in apCell[] across the new pages. |
| 3243 | ** Insert divider cells into pParent as necessary. |
| 3244 | */ |
| 3245 | j = 0; |
| 3246 | for(i=0; i<nNew; i++){ |
| 3247 | MemPage *pNew = apNew[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3248 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3249 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
| 3250 | j = cntNew[i]; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3251 | assert( pNew->nCell>0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3252 | assert( pNew->nOverflow==0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3253 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3254 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3255 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3256 | int sz; |
| 3257 | pCell = apCell[j]; |
| 3258 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3259 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3260 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3261 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3262 | }else if( leafData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3263 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3264 | j--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3265 | parseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3266 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3267 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3268 | iSpace += sz; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3269 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3270 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3271 | }else{ |
| 3272 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3273 | pTemp = &aSpace[iSpace]; |
| 3274 | iSpace += sz; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3275 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3276 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3277 | insertCell(pParent, nxDiv, pCell, sz, pTemp); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3278 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3279 | j++; |
| 3280 | nxDiv++; |
| 3281 | } |
| 3282 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3283 | assert( j==nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3284 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3285 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3286 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3287 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3288 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3289 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3290 | }else{ |
| 3291 | /* Right-most sibling is the left child of the first entry in pParent |
| 3292 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3293 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3298 | */ |
| 3299 | for(i=0; i<nNew; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3300 | reparentChildPages(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3301 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3302 | reparentChildPages(pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3303 | |
| 3304 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3305 | ** Balance the parent page. Note that the current page (pPage) might |
| 3306 | ** have been added to the freelist is it might no longer be initialized. |
| 3307 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3308 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3309 | assert( pParent->isInit ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3310 | /* assert( pPage->isInit ); // No! pPage might have been added to freelist */ |
| 3311 | /* pageIntegrity(pPage); // No! pPage might have been added to freelist */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3312 | rc = balance(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3313 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3314 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3315 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3316 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3317 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3318 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3319 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3320 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3321 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3322 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3323 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3324 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3325 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3326 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 3327 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3328 | return rc; |
| 3329 | } |
| 3330 | |
| 3331 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3332 | ** This routine is called for the root page of a btree when the root |
| 3333 | ** page contains no cells. This is an opportunity to make the tree |
| 3334 | ** shallower by one level. |
| 3335 | */ |
| 3336 | static int balance_shallower(MemPage *pPage){ |
| 3337 | MemPage *pChild; /* The only child page of pPage */ |
| 3338 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3339 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
| 3340 | Btree *pBt; /* The main BTree structure */ |
| 3341 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 3342 | u8 **apCell; /* All cells from pages being balanced */ |
| 3343 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3344 | |
| 3345 | assert( pPage->pParent==0 ); |
| 3346 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3347 | pBt = pPage->pBt; |
| 3348 | mxCellPerPage = MX_CELL(pBt); |
| 3349 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 3350 | if( apCell==0 ) return SQLITE_NOMEM; |
| 3351 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3352 | if( pPage->leaf ){ |
| 3353 | /* The table is completely empty */ |
| 3354 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 3355 | }else{ |
| 3356 | /* The root page is empty but has one child. Transfer the |
| 3357 | ** information from that one child into the root page if it |
| 3358 | ** will fit. This reduces the depth of the tree by one. |
| 3359 | ** |
| 3360 | ** If the root page is page 1, it has less space available than |
| 3361 | ** its child (due to the 100 byte header that occurs at the beginning |
| 3362 | ** of the database fle), so it might not be able to hold all of the |
| 3363 | ** information currently contained in the child. If this is the |
| 3364 | ** case, then do not do the transfer. Leave page 1 empty except |
| 3365 | ** for the right-pointer to the child page. The child page becomes |
| 3366 | ** the virtual root of the tree. |
| 3367 | */ |
| 3368 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 3369 | assert( pgnoChild>0 ); |
| 3370 | assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) ); |
| 3371 | rc = getPage(pPage->pBt, pgnoChild, &pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3372 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3373 | if( pPage->pgno==1 ){ |
| 3374 | rc = initPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3375 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3376 | assert( pChild->nOverflow==0 ); |
| 3377 | if( pChild->nFree>=100 ){ |
| 3378 | /* The child information will fit on the root page, so do the |
| 3379 | ** copy */ |
| 3380 | int i; |
| 3381 | zeroPage(pPage, pChild->aData[0]); |
| 3382 | for(i=0; i<pChild->nCell; i++){ |
| 3383 | apCell[i] = findCell(pChild,i); |
| 3384 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 3385 | } |
| 3386 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
| 3387 | freePage(pChild); |
| 3388 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 3389 | }else{ |
| 3390 | /* The child has more information that will fit on the root. |
| 3391 | ** The tree is already balanced. Do nothing. */ |
| 3392 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 3393 | } |
| 3394 | }else{ |
| 3395 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 3396 | pPage->isInit = 0; |
| 3397 | pPage->pParent = 0; |
| 3398 | rc = initPage(pPage, 0); |
| 3399 | assert( rc==SQLITE_OK ); |
| 3400 | freePage(pChild); |
| 3401 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 3402 | pChild->pgno, pPage->pgno)); |
| 3403 | } |
| 3404 | reparentChildPages(pPage); |
| 3405 | releasePage(pChild); |
| 3406 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3407 | end_shallow_balance: |
| 3408 | sqliteFree(apCell); |
| 3409 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | |
| 3413 | /* |
| 3414 | ** The root page is overfull |
| 3415 | ** |
| 3416 | ** When this happens, Create a new child page and copy the |
| 3417 | ** contents of the root into the child. Then make the root |
| 3418 | ** page an empty page with rightChild pointing to the new |
| 3419 | ** child. Finally, call balance_internal() on the new child |
| 3420 | ** to cause it to split. |
| 3421 | */ |
| 3422 | static int balance_deeper(MemPage *pPage){ |
| 3423 | int rc; /* Return value from subprocedures */ |
| 3424 | MemPage *pChild; /* Pointer to a new child page */ |
| 3425 | Pgno pgnoChild; /* Page number of the new child page */ |
| 3426 | Btree *pBt; /* The BTree */ |
| 3427 | int usableSize; /* Total usable size of a page */ |
| 3428 | u8 *data; /* Content of the parent page */ |
| 3429 | u8 *cdata; /* Content of the child page */ |
| 3430 | int hdr; /* Offset to page header in parent */ |
| 3431 | int brk; /* Offset to content of first cell in parent */ |
| 3432 | |
| 3433 | assert( pPage->pParent==0 ); |
| 3434 | assert( pPage->nOverflow>0 ); |
| 3435 | pBt = pPage->pBt; |
| 3436 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno); |
| 3437 | if( rc ) return rc; |
| 3438 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
| 3439 | usableSize = pBt->usableSize; |
| 3440 | data = pPage->aData; |
| 3441 | hdr = pPage->hdrOffset; |
| 3442 | brk = get2byte(&data[hdr+5]); |
| 3443 | cdata = pChild->aData; |
| 3444 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 3445 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
| 3446 | rc = initPage(pChild, pPage); |
| 3447 | if( rc ) return rc; |
| 3448 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 3449 | pChild->nOverflow = pPage->nOverflow; |
| 3450 | if( pChild->nOverflow ){ |
| 3451 | pChild->nFree = 0; |
| 3452 | } |
| 3453 | assert( pChild->nCell==pPage->nCell ); |
| 3454 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 3455 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 3456 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 3457 | rc = balance_nonroot(pChild); |
| 3458 | releasePage(pChild); |
| 3459 | return rc; |
| 3460 | } |
| 3461 | |
| 3462 | /* |
| 3463 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 3464 | ** required, call the appropriate balancing routine. |
| 3465 | */ |
| 3466 | static int balance(MemPage *pPage){ |
| 3467 | int rc = SQLITE_OK; |
| 3468 | if( pPage->pParent==0 ){ |
| 3469 | if( pPage->nOverflow>0 ){ |
| 3470 | rc = balance_deeper(pPage); |
| 3471 | } |
| 3472 | if( pPage->nCell==0 ){ |
| 3473 | rc = balance_shallower(pPage); |
| 3474 | } |
| 3475 | }else{ |
| 3476 | if( pPage->nOverflow>0 || pPage->nFree>pPage->pBt->usableSize*2/3 ){ |
| 3477 | rc = balance_nonroot(pPage); |
| 3478 | } |
| 3479 | } |
| 3480 | return rc; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3484 | ** This routine checks all cursors that point to table pgnoRoot. |
| 3485 | ** If any of those cursors other than pExclude were opened with |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3486 | ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3487 | ** cursors that point to pgnoRoot were opened with wrFlag==1 |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3488 | ** then this routine returns SQLITE_OK. |
| 3489 | ** |
| 3490 | ** In addition to checking for read-locks (where a read-lock |
| 3491 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3492 | ** all cursors other than pExclude so that they are pointing to the |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3493 | ** first Cell on root page. This is necessary because an insert |
| 3494 | ** or delete might change the number of cells on a page or delete |
| 3495 | ** a page entirely and we do not want to leave any cursors |
| 3496 | ** pointing to non-existant pages or cells. |
| 3497 | */ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3498 | static int checkReadLocks(Btree *pBt, Pgno pgnoRoot, BtCursor *pExclude){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3499 | BtCursor *p; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3500 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 3501 | if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3502 | if( p->wrFlag==0 ) return SQLITE_LOCKED; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3503 | if( p->pPage->pgno!=p->pgnoRoot ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3504 | moveToRoot(p); |
| 3505 | } |
| 3506 | } |
| 3507 | return SQLITE_OK; |
| 3508 | } |
| 3509 | |
| 3510 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3511 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 3512 | ** and the data is given by (pData,nData). The cursor is used only to |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3513 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3514 | ** is left pointing at a random location. |
| 3515 | ** |
| 3516 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 3517 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3518 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3519 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3520 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3521 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3522 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3523 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3524 | int rc; |
| 3525 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3526 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3527 | MemPage *pPage; |
| 3528 | Btree *pBt = pCur->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3529 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3530 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3531 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3532 | if( pCur->status ){ |
| 3533 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3534 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3535 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3536 | /* Must start a transaction before doing an insert */ |
| 3537 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3538 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3539 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3540 | if( !pCur->wrFlag ){ |
| 3541 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 3542 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3543 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3544 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3545 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3546 | rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3547 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3548 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3549 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3550 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3551 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 3552 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 3553 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3554 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3555 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3556 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3557 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 3558 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3559 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3560 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3561 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3562 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3563 | if( loc==0 && pCur->isValid ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3564 | int szOld; |
| 3565 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3566 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3567 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3568 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3569 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3570 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3571 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3572 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3573 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3574 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3575 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3576 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3577 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3578 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3579 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3580 | } |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3581 | insertCell(pPage, pCur->idx, newCell, szNew, 0); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3582 | rc = balance(pPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3583 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3584 | /* fflush(stdout); */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3585 | moveToRoot(pCur); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3586 | end_insert: |
| 3587 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3588 | return rc; |
| 3589 | } |
| 3590 | |
| 3591 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3592 | ** Delete the entry that the cursor is pointing to. The cursor |
| 3593 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3594 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3595 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3596 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3597 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3598 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3599 | Pgno pgnoChild = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3600 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3601 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3602 | assert( pPage->isInit ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3603 | if( pCur->status ){ |
| 3604 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3605 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3606 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3607 | /* Must start a transaction before doing a delete */ |
| 3608 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3609 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3610 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3611 | if( pCur->idx >= pPage->nCell ){ |
| 3612 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 3613 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3614 | if( !pCur->wrFlag ){ |
| 3615 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 3616 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 3617 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3618 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3619 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3620 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3621 | if( rc ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3622 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3623 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3624 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3625 | } |
| 3626 | clearCell(pPage, pCell); |
| 3627 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3628 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3629 | ** The entry we are about to delete is not a leaf so if we do not |
drh | 9ca7d3b | 2001-06-28 11:50:21 +0000 | [diff] [blame] | 3630 | ** do something we will leave a hole on an internal page. |
| 3631 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 3632 | ** next Cell after the one to be deleted is guaranteed to exist and |
| 3633 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3634 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3635 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3636 | unsigned char *pNext; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3637 | int szNext; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3638 | int notUsed; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3639 | unsigned char *tempCell; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3640 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3641 | getTempCursor(pCur, &leafCur); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3642 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3643 | if( rc!=SQLITE_OK ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3644 | if( rc!=SQLITE_NOMEM ){ |
| 3645 | rc = SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 3646 | } |
drh | 8a6ac0a | 2004-02-14 17:35:07 +0000 | [diff] [blame] | 3647 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3648 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3649 | rc = sqlite3pager_write(leafCur.pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3650 | if( rc ) return rc; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3651 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 3652 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3653 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
| 3654 | pNext = findCell(leafCur.pPage, leafCur.idx); |
| 3655 | szNext = cellSizePtr(leafCur.pPage, pNext); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3656 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 3657 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 3658 | if( tempCell==0 ) return SQLITE_NOMEM; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3659 | insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3660 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3661 | rc = balance(pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3662 | sqliteFree(tempCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3663 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3664 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 3665 | rc = balance(leafCur.pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3666 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3667 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3668 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 3669 | pCur->pgnoRoot, pPage->pgno)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3670 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3671 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3672 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3673 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3674 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3675 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3678 | ** Create a new BTree table. Write into *piTable the page |
| 3679 | ** number for the root page of the new table. |
| 3680 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3681 | ** The type of type is determined by the flags parameter. Only the |
| 3682 | ** following values of flags are currently in use. Other values for |
| 3683 | ** flags might not work: |
| 3684 | ** |
| 3685 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 3686 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3687 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3688 | int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3689 | MemPage *pRoot; |
| 3690 | Pgno pgnoRoot; |
| 3691 | int rc; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3692 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3693 | /* Must start a transaction first */ |
| 3694 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3695 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3696 | if( pBt->readOnly ){ |
| 3697 | return SQLITE_READONLY; |
| 3698 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3699 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3700 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3701 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3702 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3703 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3704 | *piTable = (int)pgnoRoot; |
| 3705 | return SQLITE_OK; |
| 3706 | } |
| 3707 | |
| 3708 | /* |
| 3709 | ** Erase the given database page and all its children. Return |
| 3710 | ** the page to the freelist. |
| 3711 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3712 | static int clearDatabasePage( |
| 3713 | Btree *pBt, /* The BTree that contains the table */ |
| 3714 | Pgno pgno, /* Page number to clear */ |
| 3715 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 3716 | int freePageFlag /* Deallocate page if true */ |
| 3717 | ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3718 | MemPage *pPage; |
| 3719 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3720 | unsigned char *pCell; |
| 3721 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3722 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3723 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3724 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3725 | rc = sqlite3pager_write(pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3726 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3727 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3728 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3729 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3730 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3731 | if( rc ) return rc; |
| 3732 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3733 | rc = clearCell(pPage, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3734 | if( rc ) return rc; |
| 3735 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3736 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3737 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3738 | if( rc ) return rc; |
| 3739 | } |
| 3740 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3741 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3742 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3743 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3744 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3745 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3746 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3750 | ** Delete all information from a single table in the database. iTable is |
| 3751 | ** the page number of the root of the table. After this routine returns, |
| 3752 | ** the root page is empty, but still exists. |
| 3753 | ** |
| 3754 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 3755 | ** read cursors on the table. Open write cursors are moved to the |
| 3756 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3757 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3758 | int sqlite3BtreeClearTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3759 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3760 | BtCursor *pCur; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3761 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3762 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3763 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3764 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3765 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3766 | if( pCur->wrFlag==0 ) return SQLITE_LOCKED; |
| 3767 | moveToRoot(pCur); |
| 3768 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3769 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3770 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3771 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3772 | sqlite3BtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3773 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3774 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /* |
| 3778 | ** Erase all information in a table and add the root of the table to |
| 3779 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3780 | ** page 1) is never added to the freelist. |
| 3781 | ** |
| 3782 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 3783 | ** cursors on the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3784 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3785 | int sqlite3BtreeDropTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3786 | int rc; |
| 3787 | MemPage *pPage; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3788 | BtCursor *pCur; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3789 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3790 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3791 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3792 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3793 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3794 | return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */ |
| 3795 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3796 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3797 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3798 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3799 | rc = sqlite3BtreeClearTable(pBt, iTable); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3800 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3801 | if( iTable>1 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3802 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3803 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3804 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3805 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3806 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3807 | return rc; |
| 3808 | } |
| 3809 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3810 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3811 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3812 | ** Read the meta-information out of a database file. Meta[0] |
| 3813 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3814 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 3815 | ** is read-only, the others are read/write. |
| 3816 | ** |
| 3817 | ** The schema layer numbers meta values differently. At the schema |
| 3818 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 3819 | ** free pages is not visible. So Cookie[0] is the same as Meta[1]. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3820 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3821 | int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3822 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3823 | unsigned char *pP1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3824 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3825 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3826 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3827 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3828 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3829 | sqlite3pager_unref(pP1); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 3830 | |
| 3831 | /* The current implementation is unable to handle writes to an autovacuumed |
| 3832 | ** database. So make such a database readonly. */ |
| 3833 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
| 3834 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3835 | return SQLITE_OK; |
| 3836 | } |
| 3837 | |
| 3838 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3839 | ** Write meta-information back into the database. Meta[0] is |
| 3840 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3841 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3842 | int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3843 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3844 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3845 | assert( idx>=1 && idx<=15 ); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 3846 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3847 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3848 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3849 | assert( pBt->pPage1!=0 ); |
| 3850 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3851 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3852 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3853 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3854 | return SQLITE_OK; |
| 3855 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3856 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3857 | /* |
| 3858 | ** Return the flag byte at the beginning of the page that the cursor |
| 3859 | ** is currently pointing to. |
| 3860 | */ |
| 3861 | int sqlite3BtreeFlags(BtCursor *pCur){ |
| 3862 | MemPage *pPage = pCur->pPage; |
| 3863 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 3864 | } |
| 3865 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3866 | /* |
| 3867 | ** Print a disassembly of the given page on standard output. This routine |
| 3868 | ** is used for debugging and testing only. |
| 3869 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3870 | #ifdef SQLITE_TEST |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3871 | int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3872 | int rc; |
| 3873 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3874 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3875 | int nFree; |
| 3876 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3877 | int hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3878 | int nCell; |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3879 | int isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3880 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3881 | char range[20]; |
| 3882 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3883 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3884 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3885 | isInit = pPage->isInit; |
| 3886 | if( pPage->isInit==0 ){ |
| 3887 | initPage(pPage, 0); |
| 3888 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3889 | if( rc ){ |
| 3890 | return rc; |
| 3891 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3892 | hdr = pPage->hdrOffset; |
| 3893 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3894 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3895 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3896 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3897 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3898 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3899 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3900 | nCell = get2byte(&data[hdr+3]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 3901 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3902 | data[hdr], data[hdr+7], |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3903 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3904 | assert( hdr == (pgno==1 ? 100 : 0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3905 | idx = hdr + 12 - pPage->leaf*4; |
| 3906 | for(i=0; i<nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3907 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3908 | Pgno child; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3909 | unsigned char *pCell; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3910 | int sz; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3911 | int addr; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3912 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3913 | addr = get2byte(&data[idx + 2*i]); |
| 3914 | pCell = &data[addr]; |
| 3915 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3916 | sz = info.nSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3917 | sprintf(range,"%d..%d", addr, addr+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3918 | if( pPage->leaf ){ |
| 3919 | child = 0; |
| 3920 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3921 | child = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3922 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3923 | sz = info.nData; |
| 3924 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3925 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3926 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3927 | for(j=0; j<sz; j++){ |
| 3928 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 3929 | } |
| 3930 | payload[sz] = 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 3931 | sqlite3DebugPrintf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3932 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 3933 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3934 | ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3935 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3936 | if( !pPage->leaf ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 3937 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3938 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3939 | nFree = 0; |
| 3940 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3941 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3942 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3943 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3944 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 3945 | nFree += sz; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 3946 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3947 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3948 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3949 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3950 | } |
| 3951 | if( idx!=0 ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame^] | 3952 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3953 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3954 | if( recursive && !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3955 | for(i=0; i<nCell; i++){ |
| 3956 | unsigned char *pCell = findCell(pPage, i); |
| 3957 | sqlite3BtreePageDump(pBt, get4byte(pCell), 1); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3958 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3959 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3960 | sqlite3BtreePageDump(pBt, get4byte(&data[hdr+8]), 1); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3961 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3962 | pPage->isInit = isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3963 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 3964 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3965 | return SQLITE_OK; |
| 3966 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3967 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3968 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3969 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3970 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3971 | ** Fill aResult[] with information about the entry and page that the |
| 3972 | ** cursor is pointing to. |
| 3973 | ** |
| 3974 | ** aResult[0] = The page number |
| 3975 | ** aResult[1] = The entry number |
| 3976 | ** aResult[2] = Total number of entries on this page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3977 | ** aResult[3] = Cell size (local payload + header) |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3978 | ** aResult[4] = Number of free bytes on this page |
| 3979 | ** aResult[5] = Number of free blocks on the page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3980 | ** aResult[6] = Total payload size (local + overflow) |
| 3981 | ** aResult[7] = Header size in bytes |
| 3982 | ** aResult[8] = Local payload size |
| 3983 | ** aResult[9] = Parent page number |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3984 | ** |
| 3985 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3986 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3987 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3988 | int cnt, idx; |
| 3989 | MemPage *pPage = pCur->pPage; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3990 | BtCursor tmpCur; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3991 | |
| 3992 | pageIntegrity(pPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3993 | assert( pPage->isInit ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 3994 | getTempCursor(pCur, &tmpCur); |
| 3995 | while( upCnt-- ){ |
| 3996 | moveToParent(&tmpCur); |
| 3997 | } |
| 3998 | pPage = tmpCur.pPage; |
| 3999 | pageIntegrity(pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4000 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4001 | assert( aResult[0]==pPage->pgno ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4002 | aResult[1] = tmpCur.idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4003 | aResult[2] = pPage->nCell; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4004 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 4005 | getCellInfo(&tmpCur); |
| 4006 | aResult[3] = tmpCur.info.nSize; |
| 4007 | aResult[6] = tmpCur.info.nData; |
| 4008 | aResult[7] = tmpCur.info.nHeader; |
| 4009 | aResult[8] = tmpCur.info.nLocal; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4010 | }else{ |
| 4011 | aResult[3] = 0; |
| 4012 | aResult[6] = 0; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4013 | aResult[7] = 0; |
| 4014 | aResult[8] = 0; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4015 | } |
| 4016 | aResult[4] = pPage->nFree; |
| 4017 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4018 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4019 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4020 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4021 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4022 | } |
| 4023 | aResult[5] = cnt; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4024 | if( pPage->pParent==0 || isRootPage(pPage) ){ |
| 4025 | aResult[9] = 0; |
| 4026 | }else{ |
| 4027 | aResult[9] = pPage->pParent->pgno; |
| 4028 | } |
| 4029 | releaseTempCursor(&tmpCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4030 | return SQLITE_OK; |
| 4031 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4032 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4033 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4034 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4035 | ** Return the pager associated with a BTree. This routine is used for |
| 4036 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4037 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4038 | Pager *sqlite3BtreePager(Btree *pBt){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4039 | return pBt->pPager; |
| 4040 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4041 | |
| 4042 | /* |
| 4043 | ** This structure is passed around through all the sanity checking routines |
| 4044 | ** in order to keep track of some global state information. |
| 4045 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4046 | typedef struct IntegrityCk IntegrityCk; |
| 4047 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 4048 | Btree *pBt; /* The tree being checked out */ |
| 4049 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 4050 | int nPage; /* Number of pages in the database */ |
| 4051 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 4052 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4053 | }; |
| 4054 | |
| 4055 | /* |
| 4056 | ** Append a message to the error message string. |
| 4057 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4058 | static void checkAppendMsg( |
| 4059 | IntegrityCk *pCheck, |
| 4060 | char *zMsg1, |
| 4061 | const char *zFormat, |
| 4062 | ... |
| 4063 | ){ |
| 4064 | va_list ap; |
| 4065 | char *zMsg2; |
| 4066 | va_start(ap, zFormat); |
| 4067 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 4068 | va_end(ap); |
| 4069 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4070 | if( pCheck->zErrMsg ){ |
| 4071 | char *zOld = pCheck->zErrMsg; |
| 4072 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4073 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4074 | sqliteFree(zOld); |
| 4075 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4076 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4077 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4078 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | /* |
| 4082 | ** Add 1 to the reference count for page iPage. If this is the second |
| 4083 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 4084 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 4085 | ** if this is the first reference to the page. |
| 4086 | ** |
| 4087 | ** Also check that the page number is in bounds. |
| 4088 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4089 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4090 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 4091 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4092 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4093 | return 1; |
| 4094 | } |
| 4095 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4096 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4097 | return 1; |
| 4098 | } |
| 4099 | return (pCheck->anRef[iPage]++)>1; |
| 4100 | } |
| 4101 | |
| 4102 | /* |
| 4103 | ** Check the integrity of the freelist or of an overflow page list. |
| 4104 | ** Verify that the number of pages on the list is N. |
| 4105 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4106 | static void checkList( |
| 4107 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 4108 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 4109 | int iPage, /* Page number for first page in the list */ |
| 4110 | int N, /* Expected number of pages in the list */ |
| 4111 | char *zContext /* Context for error messages */ |
| 4112 | ){ |
| 4113 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4114 | int expected = N; |
| 4115 | int iFirst = iPage; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4116 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4117 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4118 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4119 | checkAppendMsg(pCheck, zContext, |
| 4120 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4121 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4122 | break; |
| 4123 | } |
| 4124 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4125 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4126 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4127 | break; |
| 4128 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4129 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4130 | int n = get4byte(&pOvfl[4]); |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 4131 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4132 | checkAppendMsg(pCheck, zContext, |
| 4133 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4134 | N--; |
| 4135 | }else{ |
| 4136 | for(i=0; i<n; i++){ |
| 4137 | checkRef(pCheck, get4byte(&pOvfl[8+i*4]), zContext); |
| 4138 | } |
| 4139 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4140 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 4141 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4142 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4143 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | /* |
| 4148 | ** Do various sanity checks on a single page of a tree. Return |
| 4149 | ** the tree depth. Root pages return 0. Parents of root pages |
| 4150 | ** return 1, and so forth. |
| 4151 | ** |
| 4152 | ** These checks are done: |
| 4153 | ** |
| 4154 | ** 1. Make sure that cells and freeblocks do not overlap |
| 4155 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4156 | ** NO 2. Make sure cell keys are in order. |
| 4157 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 4158 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4159 | ** 5. Check the integrity of overflow pages. |
| 4160 | ** 6. Recursively call checkTreePage on all children. |
| 4161 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4162 | ** 8. Make sure this page is at least 33% full or else it is |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4163 | ** the root of the tree. |
| 4164 | */ |
| 4165 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4166 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4167 | int iPage, /* Page number of the page to check */ |
| 4168 | MemPage *pParent, /* Parent page */ |
| 4169 | char *zParentContext, /* Parent context */ |
| 4170 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 4171 | int nLower, /* Number of characters in zLowerBound */ |
| 4172 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 4173 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4174 | ){ |
| 4175 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4176 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4177 | int hdr, cellStart; |
| 4178 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4179 | u8 *data; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4180 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 4181 | Btree *pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4182 | int maxLocal, usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4183 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4184 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4185 | |
| 4186 | /* Check that the page exists |
| 4187 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 4188 | cur.pBt = pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4189 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4190 | if( iPage==0 ) return 0; |
| 4191 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4192 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4193 | checkAppendMsg(pCheck, zContext, |
| 4194 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4197 | maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4198 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4199 | checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4200 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4201 | return 0; |
| 4202 | } |
| 4203 | |
| 4204 | /* Check out all the cells. |
| 4205 | */ |
| 4206 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4207 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4208 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4209 | u8 *pCell; |
| 4210 | int sz; |
| 4211 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4212 | |
| 4213 | /* Check payload overflow pages |
| 4214 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4215 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4216 | pCell = findCell(pPage,i); |
| 4217 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4218 | sz = info.nData; |
| 4219 | if( !pPage->intKey ) sz += info.nKey; |
| 4220 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4221 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4222 | checkList(pCheck, 0, get4byte(&pCell[info.iOverflow]),nPage,zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* Check sanity of left child page. |
| 4226 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4227 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4228 | pgno = get4byte(pCell); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4229 | d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0); |
| 4230 | if( i>0 && d2!=depth ){ |
| 4231 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 4232 | } |
| 4233 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4234 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4235 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4236 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4237 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4238 | sprintf(zContext, "On page %d at right child: ", iPage); |
| 4239 | checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0); |
| 4240 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4241 | |
| 4242 | /* Check for complete coverage of the page |
| 4243 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4244 | data = pPage->aData; |
| 4245 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4246 | hit = sqliteMalloc( usableSize ); |
| 4247 | if( hit ){ |
| 4248 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 4249 | nCell = get2byte(&data[hdr+3]); |
| 4250 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 4251 | for(i=0; i<nCell; i++){ |
| 4252 | int pc = get2byte(&data[cellStart+i*2]); |
| 4253 | int size = cellSizePtr(pPage, &data[pc]); |
| 4254 | int j; |
| 4255 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 4256 | } |
| 4257 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 4258 | cnt++){ |
| 4259 | int size = get2byte(&data[i+2]); |
| 4260 | int j; |
| 4261 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 4262 | i = get2byte(&data[i]); |
| 4263 | } |
| 4264 | for(i=cnt=0; i<usableSize; i++){ |
| 4265 | if( hit[i]==0 ){ |
| 4266 | cnt++; |
| 4267 | }else if( hit[i]>1 ){ |
| 4268 | checkAppendMsg(pCheck, 0, |
| 4269 | "Multiple uses for byte %d of page %d", i, iPage); |
| 4270 | break; |
| 4271 | } |
| 4272 | } |
| 4273 | if( cnt!=data[hdr+7] ){ |
| 4274 | checkAppendMsg(pCheck, 0, |
| 4275 | "Fragmented space is %d byte reported as %d on page %d", |
| 4276 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4277 | } |
| 4278 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4279 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4280 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4281 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4282 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /* |
| 4286 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 4287 | ** an array of pages numbers were each page number is the root page of |
| 4288 | ** a table. nRoot is the number of entries in aRoot. |
| 4289 | ** |
| 4290 | ** If everything checks out, this routine returns NULL. If something is |
| 4291 | ** amiss, an error message is written into memory obtained from malloc() |
| 4292 | ** and a pointer to that error message is returned. The calling function |
| 4293 | ** is responsible for freeing the error message when it is done. |
| 4294 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4295 | char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4296 | int i; |
| 4297 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4298 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4299 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4300 | nRef = *sqlite3pager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 4301 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 4302 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 4303 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4304 | sCheck.pBt = pBt; |
| 4305 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4306 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 4307 | if( sCheck.nPage==0 ){ |
| 4308 | unlockBtreeIfUnused(pBt); |
| 4309 | return 0; |
| 4310 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4311 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4312 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 4313 | i = PENDING_BYTE/pBt->pageSize + 1; |
| 4314 | if( i<=sCheck.nPage ){ |
| 4315 | sCheck.anRef[i] = 1; |
| 4316 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4317 | sCheck.zErrMsg = 0; |
| 4318 | |
| 4319 | /* Check the integrity of the freelist |
| 4320 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4321 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 4322 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4323 | |
| 4324 | /* Check all the tables. |
| 4325 | */ |
| 4326 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 4327 | if( aRoot[i]==0 ) continue; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 4328 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
| 4331 | /* Make sure every page in the file is referenced |
| 4332 | */ |
| 4333 | for(i=1; i<=sCheck.nPage; i++){ |
| 4334 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4335 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | /* Make sure this analysis did not leave any unref() pages |
| 4340 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4341 | unlockBtreeIfUnused(pBt); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4342 | if( nRef != *sqlite3pager_stats(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4343 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4344 | "Outstanding page count goes from %d to %d during this analysis", |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4345 | nRef, *sqlite3pager_stats(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4346 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | /* Clean up and report errors. |
| 4350 | */ |
| 4351 | sqliteFree(sCheck.anRef); |
| 4352 | return sCheck.zErrMsg; |
| 4353 | } |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 4354 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4355 | /* |
| 4356 | ** Return the full pathname of the underlying database file. |
| 4357 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4358 | const char *sqlite3BtreeGetFilename(Btree *pBt){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4359 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4360 | return sqlite3pager_filename(pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 4364 | ** Return the pathname of the directory that contains the database file. |
| 4365 | */ |
| 4366 | const char *sqlite3BtreeGetDirname(Btree *pBt){ |
| 4367 | assert( pBt->pPager!=0 ); |
| 4368 | return sqlite3pager_dirname(pBt->pPager); |
| 4369 | } |
| 4370 | |
| 4371 | /* |
| 4372 | ** Return the pathname of the journal file for this database. The return |
| 4373 | ** value of this routine is the same regardless of whether the journal file |
| 4374 | ** has been created or not. |
| 4375 | */ |
| 4376 | const char *sqlite3BtreeGetJournalname(Btree *pBt){ |
| 4377 | assert( pBt->pPager!=0 ); |
| 4378 | return sqlite3pager_journalname(pBt->pPager); |
| 4379 | } |
| 4380 | |
| 4381 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4382 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 4383 | ** must be active for both files. |
| 4384 | ** |
| 4385 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4386 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4387 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4388 | int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4389 | int rc = SQLITE_OK; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4390 | Pgno i, nPage, nToPage; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4391 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4392 | if( pBtTo->inTrans!=TRANS_WRITE || pBtFrom->inTrans!=TRANS_WRITE ){ |
| 4393 | return SQLITE_ERROR; |
| 4394 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4395 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4396 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 4397 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 4398 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4399 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4400 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4401 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4402 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4403 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4404 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4405 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4406 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 4407 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4408 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4409 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4410 | rc = sqlite3pager_write(pPage); |
| 4411 | sqlite3pager_unref(pPage); |
| 4412 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4413 | } |
| 4414 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4415 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4416 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4417 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4418 | sqlite3BtreeRollback(pBtTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4419 | } |
| 4420 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4421 | } |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4422 | |
| 4423 | /* |
| 4424 | ** Return non-zero if a transaction is active. |
| 4425 | */ |
| 4426 | int sqlite3BtreeIsInTrans(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4427 | return (pBt && (pBt->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | /* |
| 4431 | ** Return non-zero if a statement transaction is active. |
| 4432 | */ |
| 4433 | int sqlite3BtreeIsInStmt(Btree *pBt){ |
| 4434 | return (pBt && pBt->inStmt); |
| 4435 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 4436 | |
| 4437 | /* |
| 4438 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 4439 | ** |
| 4440 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 4441 | ** the name of a master journal file that should be written into the |
| 4442 | ** individual journal file, or is NULL, indicating no master journal file |
| 4443 | ** (single database transaction). |
| 4444 | ** |
| 4445 | ** When this is called, the master journal should already have been |
| 4446 | ** created, populated with this journal pointer and synced to disk. |
| 4447 | ** |
| 4448 | ** Once this is routine has returned, the only thing required to commit |
| 4449 | ** the write-transaction for this database file is to delete the journal. |
| 4450 | */ |
| 4451 | int sqlite3BtreeSync(Btree *pBt, const char *zMaster){ |
| 4452 | if( pBt->inTrans==TRANS_WRITE ){ |
| 4453 | return sqlite3pager_sync(pBt->pPager, zMaster); |
| 4454 | } |
| 4455 | return SQLITE_OK; |
| 4456 | } |