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 | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.137 2004/05/14 16:50:06 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 |
| 56 | ** page contain a special header that describes the file. The format |
| 57 | ** of that header is as follows: |
| 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 | ** |
| 76 | ** The file change counter is incremented every time the database is more |
| 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 |
| 86 | ** on one pages. Thus the default max embedded payload fraction is 64. |
| 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 | ** |
| 98 | ** Each btree page begins with a header described below. Note that the |
| 99 | ** header for page one begins at byte 100. For all other btree pages, the |
| 100 | ** header begins on byte zero. |
| 101 | ** |
| 102 | ** OFFSET SIZE DESCRIPTION |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 103 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 104 | ** 1 2 byte offset to the first freeblock |
| 105 | ** 3 2 byte offset to the first cell |
| 106 | ** 5 1 number of fragmented free bytes |
| 107 | ** 6 4 Right child (the Ptr(N+1) value). Omitted if leaf |
| 108 | ** |
| 109 | ** The flags define the format of this btree page. The leaf flag means that |
| 110 | ** this page has no children. The zerodata flag means that this page carries |
| 111 | ** only keys and no data. The intkey flag means that the key is a single |
| 112 | ** variable length integer at the beginning of the payload. |
| 113 | ** |
| 114 | ** A variable-length integer is 1 to 9 bytes where the lower 7 bits of each |
| 115 | ** 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] | 116 | ** the first byte with bit 8 clear. The most significant byte of the integer |
| 117 | ** appears first. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 118 | ** |
| 119 | ** 0x00 becomes 0x00000000 |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 120 | ** 0x7f becomes 0x0000007f |
| 121 | ** 0x81 0x00 becomes 0x00000080 |
| 122 | ** 0x82 0x00 becomes 0x00000100 |
| 123 | ** 0x80 0x7f becomes 0x0000007f |
| 124 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 125 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 126 | ** |
| 127 | ** Variable length integers are used for rowids and to hold the number of |
| 128 | ** bytes of key and data in a btree cell. |
| 129 | ** |
| 130 | ** Unused space within a btree page is collected into a linked list of |
| 131 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 132 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 133 | ** increasing order. Because a freeblock is 4 bytes in size, the minimum |
| 134 | ** size allocation on a btree page is 4 bytes. Because a freeblock must be |
| 135 | ** at least 4 bytes in size, any group of 3 or fewer unused bytes cannot |
| 136 | ** exist on the freeblock chain. The total number of such fragmented bytes |
| 137 | ** is recorded in the page header at offset 5. |
| 138 | ** |
| 139 | ** SIZE DESCRIPTION |
| 140 | ** 2 Byte offset of the next freeblock |
| 141 | ** 2 Bytes in this freeblock |
| 142 | ** |
| 143 | ** Cells are of variable length. The first cell begins on the byte defined |
| 144 | ** in the page header. Cells do not necessarily occur in order - they can |
| 145 | ** skip around on the page. |
| 146 | ** |
| 147 | ** SIZE DESCRIPTION |
| 148 | ** 2 Byte offset of the next cell. 0 if this is the last cell |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 149 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 150 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 151 | ** 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] | 152 | ** * Payload |
| 153 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 154 | ** |
| 155 | ** Overflow pages form a linked list. Each page except the last is completely |
| 156 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 157 | ** as 1 byte of data. |
| 158 | ** |
| 159 | ** SIZE DESCRIPTION |
| 160 | ** 4 Page number of next overflow page |
| 161 | ** * Data |
| 162 | ** |
| 163 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
| 164 | ** file header points to first in a linked list of trunk page. Each trunk |
| 165 | ** page points to multiple leaf pages. The content of a leaf page is |
| 166 | ** unspecified. A trunk page looks like this: |
| 167 | ** |
| 168 | ** SIZE DESCRIPTION |
| 169 | ** 4 Page number of next trunk page |
| 170 | ** 4 Number of leaf pointers on this page |
| 171 | ** * zero or more pages numbers of leaves |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 172 | */ |
| 173 | #include "sqliteInt.h" |
| 174 | #include "pager.h" |
| 175 | #include "btree.h" |
| 176 | #include <assert.h> |
| 177 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 178 | |
| 179 | /* Maximum page size. The upper bound on this value is 65536 (a limit |
| 180 | ** imposed by the 2-byte offset at the beginning of each cell.) The |
| 181 | ** maximum page size determines the amount of stack space allocated |
| 182 | ** by many of the routines in this module. On embedded architectures |
| 183 | ** or any machine where memory and especially stack memory is limited, |
| 184 | ** one may wish to chose a smaller value for the maximum page size. |
| 185 | */ |
| 186 | #ifndef MX_PAGE_SIZE |
| 187 | # define MX_PAGE_SIZE 1024 |
| 188 | #endif |
| 189 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 190 | /* The following value is the maximum cell size assuming a maximum page |
| 191 | ** size give above. |
| 192 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 193 | #define MX_CELL_SIZE (MX_PAGE_SIZE-10) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 194 | |
| 195 | /* The maximum number of cells on a single page of the database. This |
| 196 | ** assumes a minimum cell size of 3 bytes. Such small cells will be |
| 197 | ** exceedingly rare, but they are possible. |
| 198 | */ |
| 199 | #define MX_CELL ((MX_PAGE_SIZE-10)/3) |
| 200 | |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 201 | /* Forward declarations */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 202 | typedef struct MemPage MemPage; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 203 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 204 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 205 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 206 | ** SQLite database in order to identify the file as a real database. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 207 | ** 123456789 123456 */ |
| 208 | static const char zMagicHeader[] = "SQLite format 3"; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 211 | ** Page type flags. An ORed combination of these flags appear as the |
| 212 | ** first byte of every BTree page. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 213 | */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 214 | #define PTF_INTKEY 0x01 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 215 | #define PTF_ZERODATA 0x02 |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 216 | #define PTF_LEAFDATA 0x04 |
| 217 | #define PTF_LEAF 0x08 |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 218 | |
| 219 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 220 | ** As each page of the file is loaded into memory, an instance of the following |
| 221 | ** structure is appended and initialized to zero. This structure stores |
| 222 | ** information about the page that is decoded from the raw file page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 223 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 224 | ** The pParent field points back to the parent page. This allows us to |
| 225 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 226 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 227 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 228 | */ |
| 229 | struct MemPage { |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 230 | u32 notUsed; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 231 | u8 isInit; /* True if previously initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 232 | u8 idxShift; /* True if Cell indices have changed */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 233 | u8 isOverfull; /* Some aCell[] do not fit on page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 234 | u8 intKey; /* True if intkey flag is set */ |
| 235 | u8 leaf; /* True if leaf flag is set */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 236 | u8 zeroData; /* True if table stores keys only */ |
| 237 | u8 leafData; /* True if tables stores data on leaves only */ |
| 238 | u8 hasData; /* True if this page stores data */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 239 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 240 | u8 needRelink; /* True if need to run relinkCellList() */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 241 | int idxParent; /* Index in pParent->aCell[] of this node */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 242 | int nFree; /* Number of free bytes on the page */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 243 | int nCell; /* Number of entries on this page */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 244 | int nCellAlloc; /* Number of slots allocated in aCell[] */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 245 | unsigned char **aCell; /* Pointer to start of each cell */ |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 246 | struct Btree *pBt; /* Pointer back to BTree structure */ |
| 247 | |
| 248 | unsigned char *aData; /* Pointer back to the start of the page */ |
| 249 | Pgno pgno; /* Page number for this page */ |
| 250 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 251 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 252 | |
| 253 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 254 | ** The in-memory image of a disk page has the auxiliary information appended |
| 255 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 256 | ** that extra information. |
| 257 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 258 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 259 | |
| 260 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 261 | ** Everything we need to know about an open database |
| 262 | */ |
| 263 | struct Btree { |
| 264 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 265 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 266 | MemPage *pPage1; /* First page of the database */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 267 | u8 inTrans; /* True if a transaction is in progress */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 268 | u8 inStmt; /* True if there is a checkpoint on the transaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 269 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 270 | int pageSize; /* Total number of bytes on a page */ |
| 271 | int usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 272 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 273 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 274 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 275 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
| 276 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 277 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 278 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 279 | }; |
| 280 | typedef Btree Bt; |
| 281 | |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 282 | /* |
| 283 | ** A cursor is a pointer to a particular entry in the BTree. |
| 284 | ** The entry is identified by its MemPage and the index in |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 285 | ** MemPage.aCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 286 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 287 | struct BtCursor { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 288 | Btree *pBt; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 289 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 290 | BtCursor *pShared; /* Loop of cursors with the same root page */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 291 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 292 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 293 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 294 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 295 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 296 | u8 wrFlag; /* True if writable */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 297 | u8 iMatch; /* compare result from last sqlite3BtreeMoveto() */ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 298 | u8 isValid; /* TRUE if points to a valid entry */ |
| 299 | u8 status; /* Set to SQLITE_ABORT if cursors is invalidated */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 300 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 301 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 302 | /* |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 303 | ** An instance of the following structure is used to hold information |
| 304 | ** about a cell. The parseCell() function fills the structure in. |
| 305 | */ |
| 306 | typedef struct CellInfo CellInfo; |
| 307 | struct CellInfo { |
| 308 | i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 309 | u32 nData; /* Number of bytes of data */ |
| 310 | int nHeader; /* Size of the header in bytes */ |
| 311 | int nLocal; /* Amount of payload held locally */ |
| 312 | int iOverflow; /* Offset to overflow page number. Zero if none */ |
| 313 | int nSize; /* Size of the cell */ |
| 314 | }; |
| 315 | |
| 316 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 317 | ** Read or write a two-, four-, and eight-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 318 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 319 | static u32 get2byte(unsigned char *p){ |
| 320 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 321 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 322 | static u32 get4byte(unsigned char *p){ |
| 323 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 324 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 325 | static void put2byte(unsigned char *p, u32 v){ |
| 326 | p[0] = v>>8; |
| 327 | p[1] = v; |
| 328 | } |
| 329 | static void put4byte(unsigned char *p, u32 v){ |
| 330 | p[0] = v>>24; |
| 331 | p[1] = v>>16; |
| 332 | p[2] = v>>8; |
| 333 | p[3] = v; |
| 334 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 335 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 336 | /* |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame^] | 337 | ** Routines to read and write variable-length integers. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 338 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame^] | 339 | #define getVarint sqlite3GetVarint |
| 340 | #define getVarint32 sqlite3GetVarint32 |
| 341 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 344 | ** Parse a cell header and fill in the CellInfo structure. |
| 345 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 346 | static void parseCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 347 | MemPage *pPage, /* Page containing the cell */ |
| 348 | unsigned char *pCell, /* The cell */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 349 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 350 | ){ |
| 351 | int n; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 352 | int nPayload; |
| 353 | Btree *pBt; |
| 354 | int minLocal, maxLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 355 | if( pPage->leaf ){ |
| 356 | n = 2; |
| 357 | }else{ |
| 358 | n = 6; |
| 359 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 360 | if( pPage->hasData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 361 | n += getVarint32(&pCell[n], &pInfo->nData); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 362 | }else{ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 363 | pInfo->nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 364 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 365 | n += getVarint(&pCell[n], &pInfo->nKey); |
| 366 | pInfo->nHeader = n; |
| 367 | nPayload = pInfo->nData; |
| 368 | if( !pPage->intKey ){ |
| 369 | nPayload += pInfo->nKey; |
| 370 | } |
| 371 | pBt = pPage->pBt; |
| 372 | if( pPage->leafData ){ |
| 373 | minLocal = pBt->minLeaf; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 374 | maxLocal = pBt->usableSize - 23; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 375 | }else{ |
| 376 | minLocal = pBt->minLocal; |
| 377 | maxLocal = pBt->maxLocal; |
| 378 | } |
| 379 | if( nPayload<=maxLocal ){ |
| 380 | pInfo->nLocal = nPayload; |
| 381 | pInfo->iOverflow = 0; |
| 382 | pInfo->nSize = nPayload + n; |
| 383 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 384 | int surplus = minLocal + (nPayload - minLocal)%(pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 385 | if( surplus <= maxLocal ){ |
| 386 | pInfo->nLocal = surplus; |
| 387 | }else{ |
| 388 | pInfo->nLocal = minLocal; |
| 389 | } |
| 390 | pInfo->iOverflow = pInfo->nLocal + n; |
| 391 | pInfo->nSize = pInfo->iOverflow + 4; |
| 392 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 396 | ** Compute the total number of bytes that a Cell needs on the main |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 397 | ** database page. The number returned includes the Cell header, |
| 398 | ** local payload storage, and the pointer to overflow pages (if |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 399 | ** applicable). Additional space allocated on overflow pages |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 400 | ** is NOT included in the value returned from this routine. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 401 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 402 | static int cellSize(MemPage *pPage, unsigned char *pCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 403 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 404 | |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 405 | parseCell(pPage, pCell, &info); |
| 406 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 410 | ** Do sanity checking on a page. Throw an exception if anything is |
| 411 | ** not right. |
| 412 | ** |
| 413 | ** This routine is used for internal error checking only. It is omitted |
| 414 | ** from most builds. |
| 415 | */ |
| 416 | #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0 |
| 417 | static void _pageIntegrity(MemPage *pPage){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 418 | int usableSize; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 419 | u8 *data; |
| 420 | int i, idx, c, pc, hdr, nFree; |
| 421 | u8 used[MX_PAGE_SIZE]; |
| 422 | |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 423 | usableSize = pPage->pBt->usableSize; |
| 424 | assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->pageSize] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 425 | hdr = pPage->hdrOffset; |
| 426 | assert( hdr==(pPage->pgno==1 ? 100 : 0) ); |
| 427 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
| 428 | c = pPage->aData[hdr]; |
| 429 | if( pPage->isInit ){ |
| 430 | assert( pPage->leaf == ((c & PTF_LEAF)!=0) ); |
| 431 | assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 432 | assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) ); |
| 433 | assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) ); |
| 434 | assert( pPage->hasData == |
| 435 | !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 436 | } |
| 437 | data = pPage->aData; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 438 | memset(used, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 439 | for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1; |
| 440 | nFree = 0; |
| 441 | pc = get2byte(&data[hdr+1]); |
| 442 | while( pc ){ |
| 443 | int size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 444 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 445 | size = get2byte(&data[pc+2]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 446 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 447 | nFree += size; |
| 448 | for(i=pc; i<pc+size; i++){ |
| 449 | assert( used[i]==0 ); |
| 450 | used[i] = 1; |
| 451 | } |
| 452 | pc = get2byte(&data[pc]); |
| 453 | } |
| 454 | assert( pPage->isInit==0 || pPage->nFree==nFree+data[hdr+5] ); |
| 455 | idx = 0; |
| 456 | pc = get2byte(&data[hdr+3]); |
| 457 | while( pc ){ |
| 458 | int size; |
| 459 | assert( pPage->isInit==0 || idx<pPage->nCell ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 460 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 461 | assert( pPage->isInit==0 || pPage->aCell[idx]==&data[pc] ); |
| 462 | size = cellSize(pPage, &data[pc]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 463 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 464 | for(i=pc; i<pc+size; i++){ |
| 465 | assert( used[i]==0 ); |
| 466 | used[i] = 1; |
| 467 | } |
| 468 | pc = get2byte(&data[pc]); |
| 469 | idx++; |
| 470 | } |
| 471 | assert( idx==pPage->nCell ); |
| 472 | nFree = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 473 | for(i=0; i<usableSize; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 474 | assert( used[i]<=1 ); |
| 475 | if( used[i]==0 ) nFree++; |
| 476 | } |
| 477 | assert( nFree==data[hdr+5] ); |
| 478 | } |
| 479 | #define pageIntegrity(X) _pageIntegrity(X) |
| 480 | #else |
| 481 | # define pageIntegrity(X) |
| 482 | #endif |
| 483 | |
| 484 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 485 | ** Defragment the page given. All Cells are moved to the |
| 486 | ** beginning of the page and all free space is collected |
| 487 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 488 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 489 | static void defragmentPage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 490 | int pc, i, n, addr; |
| 491 | int start, hdr, size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 492 | int leftover; |
| 493 | unsigned char *oldPage; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 494 | unsigned char newPage[MX_PAGE_SIZE]; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 495 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 496 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 497 | assert( pPage->pBt!=0 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 498 | assert( pPage->pBt->usableSize <= MX_PAGE_SIZE ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 499 | assert( !pPage->needRelink ); |
| 500 | assert( !pPage->isOverfull ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 501 | oldPage = pPage->aData; |
| 502 | hdr = pPage->hdrOffset; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 503 | addr = 3+hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 504 | n = 6+hdr; |
| 505 | if( !pPage->leaf ){ |
| 506 | n += 4; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 507 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 508 | memcpy(&newPage[hdr], &oldPage[hdr], n-hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 509 | start = n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 510 | pc = get2byte(&oldPage[addr]); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 511 | i = 0; |
| 512 | while( pc>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 513 | assert( n<pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 514 | size = cellSize(pPage, &oldPage[pc]); |
| 515 | memcpy(&newPage[n], &oldPage[pc], size); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 516 | put2byte(&newPage[addr],n); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 517 | assert( pPage->aCell[i]==&oldPage[pc] ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 518 | pPage->aCell[i++] = &oldPage[n]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 519 | addr = n; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 520 | n += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 521 | pc = get2byte(&oldPage[pc]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 522 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 523 | assert( i==pPage->nCell ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 524 | leftover = pPage->pBt->usableSize - n; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 525 | assert( leftover>=0 ); |
| 526 | assert( pPage->nFree==leftover ); |
| 527 | if( leftover<4 ){ |
| 528 | oldPage[hdr+5] = leftover; |
| 529 | leftover = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 530 | n = pPage->pBt->usableSize; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 531 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 532 | memcpy(&oldPage[hdr], &newPage[hdr], n-hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 533 | if( leftover==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 534 | put2byte(&oldPage[hdr+1], 0); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 535 | }else if( leftover>=4 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 536 | put2byte(&oldPage[hdr+1], n); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 537 | put2byte(&oldPage[n], 0); |
| 538 | put2byte(&oldPage[n+2], leftover); |
| 539 | memset(&oldPage[n+4], 0, leftover-4); |
| 540 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 541 | oldPage[hdr+5] = 0; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 542 | } |
| 543 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 544 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 545 | ** Allocate nByte bytes of space on a page. If nByte is less than |
| 546 | ** 4 it is rounded up to 4. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 547 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 548 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 549 | ** the new allocation. Or return 0 if there is not enough free |
| 550 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 551 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 552 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 553 | ** nBytes of contiguous free space, then this routine automatically |
| 554 | ** calls defragementPage() to consolidate all free space before |
| 555 | ** allocating the new chunk. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 556 | ** |
| 557 | ** Algorithm: Carve a piece off of the first freeblock that is |
| 558 | ** nByte in size or that larger. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 559 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 560 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 561 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 562 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 563 | int nFrag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 564 | unsigned char *data; |
drh | 44ce7e2 | 2003-06-17 02:57:17 +0000 | [diff] [blame] | 565 | #ifndef NDEBUG |
| 566 | int cnt = 0; |
| 567 | #endif |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 568 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 569 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 570 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 571 | assert( pPage->pBt ); |
| 572 | if( nByte<4 ) nByte = 4; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 573 | if( pPage->nFree<nByte || pPage->isOverfull ) return 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 574 | hdr = pPage->hdrOffset; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 575 | nFrag = data[hdr+5]; |
| 576 | if( nFrag>=60 || nFrag>pPage->nFree-nByte ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 577 | defragmentPage(pPage); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 578 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 579 | addr = hdr+1; |
| 580 | pc = get2byte(&data[addr]); |
| 581 | assert( addr<pc ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 582 | assert( pc<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 583 | while( (size = get2byte(&data[pc+2]))<nByte ){ |
| 584 | addr = pc; |
| 585 | pc = get2byte(&data[addr]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 586 | assert( pc<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 587 | assert( pc>=addr+size+4 || pc==0 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 588 | if( pc==0 ){ |
| 589 | assert( (cnt++)==0 ); |
| 590 | defragmentPage(pPage); |
| 591 | assert( data[hdr+5]==0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 592 | addr = pPage->hdrOffset+1; |
| 593 | pc = get2byte(&data[addr]); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | assert( pc>0 && size>=nByte ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 597 | assert( pc+size<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 598 | if( size>nByte+4 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 599 | int newStart = pc+nByte; |
| 600 | put2byte(&data[addr], newStart); |
| 601 | put2byte(&data[newStart], get2byte(&data[pc])); |
| 602 | put2byte(&data[newStart+2], size-nByte); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 603 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 604 | put2byte(&data[addr], get2byte(&data[pc])); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 605 | data[hdr+5] += size-nByte; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 606 | } |
| 607 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 608 | assert( pPage->nFree>=0 ); |
| 609 | return pc; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 613 | ** Return a section of the pPage->aData to the freelist. |
| 614 | ** The first byte of the new free block is pPage->aDisk[start] |
| 615 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 616 | ** |
| 617 | ** Most of the effort here is involved in coalesing adjacent |
| 618 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 619 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 620 | static void freeSpace(MemPage *pPage, int start, int size){ |
| 621 | int end = start + size; /* End of the segment being freed */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 622 | int addr, pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 623 | #ifndef NDEBUG |
| 624 | int tsize = 0; /* Total size of all freeblocks */ |
| 625 | #endif |
| 626 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 627 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 628 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 629 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 630 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 631 | assert( end<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 632 | if( size<4 ) size = 4; |
| 633 | |
| 634 | /* Add the space back into the linked list of freeblocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 635 | addr = pPage->hdrOffset + 1; |
| 636 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 637 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 638 | assert( pbegin>addr ); |
| 639 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 640 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 641 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 642 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 643 | put2byte(&data[addr], start); |
| 644 | put2byte(&data[start], pbegin); |
| 645 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 646 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 647 | |
| 648 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 649 | addr = pPage->hdrOffset + 1; |
| 650 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 651 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 652 | assert( pbegin>addr ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 653 | assert( pbegin<pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 654 | pnext = get2byte(&data[pbegin]); |
| 655 | psize = get2byte(&data[pbegin+2]); |
| 656 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 657 | int frag = pnext - (pbegin+psize); |
| 658 | assert( frag<=data[pPage->hdrOffset+5] ); |
| 659 | data[pPage->hdrOffset+5] -= frag; |
| 660 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 661 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 662 | }else{ |
| 663 | assert( (tsize += psize)>0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 664 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | assert( tsize+data[pPage->hdrOffset+5]==pPage->nFree ); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 668 | } |
| 669 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 670 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 671 | ** Resize the aCell[] array of the given page so that it is able to |
| 672 | ** hold at least nNewSz entries. |
| 673 | ** |
| 674 | ** Return SQLITE_OK or SQLITE_NOMEM. |
| 675 | */ |
| 676 | static int resizeCellArray(MemPage *pPage, int nNewSz){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 677 | if( pPage->nCellAlloc<nNewSz ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 678 | pPage->aCell = sqliteRealloc(pPage->aCell, nNewSz*sizeof(pPage->aCell[0]) ); |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 679 | if( sqlite3_malloc_failed ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 680 | pPage->nCellAlloc = nNewSz; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 681 | } |
| 682 | return SQLITE_OK; |
| 683 | } |
| 684 | |
| 685 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 686 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 687 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 688 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 689 | ** is the parent of the page being initialized. The root of a |
| 690 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 691 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 692 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 693 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 694 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 695 | ** guarantee that the page is well-formed. It only shows that |
| 696 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 697 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 698 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 699 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 700 | MemPage *pParent /* The parent. Might be NULL */ |
| 701 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 702 | int c, pc, i, hdr; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 703 | unsigned char *data; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 704 | int usableSize; |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 705 | /* int sumCell = 0; // Total size of all cells */ |
| 706 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 707 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 708 | assert( pPage->pBt!=0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 709 | assert( pParent==0 || pParent->pBt==pPage->pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 710 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 711 | assert( pPage->aData == &((unsigned char*)pPage)[-pPage->pBt->pageSize] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 712 | assert( pPage->pParent==0 || pPage->pParent==pParent ); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 713 | assert( pPage->pParent==pParent || !pPage->isInit ); |
| 714 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 715 | if( pPage->pParent==0 && pParent!=0 ){ |
| 716 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 717 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 718 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 719 | pPage->nCell = pPage->nCellAlloc = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 720 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 721 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 722 | data = pPage->aData; |
| 723 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 724 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 725 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 726 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 727 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 728 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 729 | pPage->isOverfull = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 730 | pPage->needRelink = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 731 | pPage->idxShift = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 732 | usableSize = pPage->pBt->usableSize; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 733 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 734 | /* Initialize the cell count and cell pointers */ |
| 735 | pc = get2byte(&data[hdr+3]); |
| 736 | while( pc>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 737 | if( pc>=usableSize ) return SQLITE_CORRUPT; |
| 738 | if( pPage->nCell>usableSize ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 739 | pPage->nCell++; |
| 740 | pc = get2byte(&data[pc]); |
| 741 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 742 | if( resizeCellArray(pPage, pPage->nCell) ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 743 | return SQLITE_NOMEM; |
| 744 | } |
| 745 | pc = get2byte(&data[hdr+3]); |
| 746 | for(i=0; pc>0; i++){ |
| 747 | pPage->aCell[i] = &data[pc]; |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 748 | /* sumCell += cellSize(pPage, &data[pc]); */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 749 | pc = get2byte(&data[pc]); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Compute the total free space on the page */ |
| 753 | pPage->nFree = data[hdr+5]; |
| 754 | pc = get2byte(&data[hdr+1]); |
| 755 | while( pc>0 ){ |
| 756 | int next, size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 757 | if( pc>=usableSize ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 758 | next = get2byte(&data[pc]); |
| 759 | size = get2byte(&data[pc+2]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 760 | if( next>0 && next<=pc+size+3 ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 761 | pPage->nFree += size; |
| 762 | pc = next; |
| 763 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 764 | if( pPage->nFree>=usableSize ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 765 | |
| 766 | /* Sanity check: Cells and freespace and header must sum to the size |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 767 | ** a page. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 768 | if( sumCell+pPage->nFree+hdr+10-pPage->leaf*4 != usableSize ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 769 | return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 770 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 771 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 772 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 773 | pPage->isInit = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 774 | pageIntegrity(pPage); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 775 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 779 | ** Set up a raw page so that it looks like a database page holding |
| 780 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 781 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 782 | static void zeroPage(MemPage *pPage, int flags){ |
| 783 | unsigned char *data = pPage->aData; |
| 784 | Btree *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 785 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 786 | int first; |
| 787 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 788 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
| 789 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 790 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 791 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 792 | data[hdr] = flags; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 793 | first = hdr + 6 + 4*((flags&PTF_LEAF)==0); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 794 | put2byte(&data[hdr+1], first); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 795 | put2byte(&data[first+2], pBt->usableSize - first); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 796 | sqliteFree(pPage->aCell); |
| 797 | pPage->aCell = 0; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 798 | pPage->nCell = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 799 | pPage->nCellAlloc = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 800 | pPage->nFree = pBt->usableSize - first; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 801 | pPage->intKey = (flags & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 802 | pPage->zeroData = (flags & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 803 | pPage->leafData = (flags & PTF_LEAFDATA)!=0; |
| 804 | pPage->leaf = (flags & PTF_LEAF)!=0; |
| 805 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 806 | pPage->hdrOffset = hdr; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 807 | pPage->isOverfull = 0; |
| 808 | pPage->needRelink = 0; |
| 809 | pPage->idxShift = 0; |
| 810 | pPage->isInit = 1; |
| 811 | pageIntegrity(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 815 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 816 | ** MemPage.aData elements if needed. |
| 817 | */ |
| 818 | static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){ |
| 819 | int rc; |
| 820 | unsigned char *aData; |
| 821 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 822 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 823 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 824 | pPage = (MemPage*)&aData[pBt->pageSize]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 825 | pPage->aData = aData; |
| 826 | pPage->pBt = pBt; |
| 827 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 828 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 829 | *ppPage = pPage; |
| 830 | return SQLITE_OK; |
| 831 | } |
| 832 | |
| 833 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 834 | ** Get a page from the pager and initialize it. This routine |
| 835 | ** is just a convenience wrapper around separate calls to |
| 836 | ** getPage() and initPage(). |
| 837 | */ |
| 838 | static int getAndInitPage( |
| 839 | Btree *pBt, /* The database file */ |
| 840 | Pgno pgno, /* Number of the page to get */ |
| 841 | MemPage **ppPage, /* Write the page pointer here */ |
| 842 | MemPage *pParent /* Parent of the page */ |
| 843 | ){ |
| 844 | int rc; |
| 845 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 846 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 847 | rc = initPage(*ppPage, pParent); |
| 848 | } |
| 849 | return rc; |
| 850 | } |
| 851 | |
| 852 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 853 | ** Release a MemPage. This should be called once for each prior |
| 854 | ** call to getPage. |
| 855 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 856 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 857 | if( pPage ){ |
| 858 | assert( pPage->aData ); |
| 859 | assert( pPage->pBt ); |
| 860 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 861 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 866 | ** This routine is called when the reference count for a page |
| 867 | ** reaches zero. We need to unref the pParent pointer when that |
| 868 | ** happens. |
| 869 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 870 | static void pageDestructor(void *pData, int pageSize){ |
| 871 | MemPage *pPage = (MemPage*)&((char*)pData)[pageSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 872 | assert( pPage->isInit==0 || pPage->needRelink==0 ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 873 | if( pPage->pParent ){ |
| 874 | MemPage *pParent = pPage->pParent; |
| 875 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 876 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 877 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 878 | sqliteFree(pPage->aCell); |
| 879 | pPage->aCell = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 880 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 884 | ** Open a new database. |
| 885 | ** |
| 886 | ** Actually, this routine just sets up the internal data structures |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 887 | ** for accessing the database. We do not open the database file |
| 888 | ** until the first page is loaded. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 889 | ** |
| 890 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 891 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 892 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 893 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 894 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 895 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 896 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
| 897 | int nCache, /* Number of cache pages */ |
| 898 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 899 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 900 | Btree *pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 901 | int rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 902 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 903 | /* |
| 904 | ** The following asserts make sure that structures used by the btree are |
| 905 | ** the right size. This is to guard against size changes that result |
| 906 | ** when compiling on a different architecture. |
| 907 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 908 | assert( sizeof(i64)==8 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 909 | assert( sizeof(u64)==8 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 910 | assert( sizeof(u32)==4 ); |
| 911 | assert( sizeof(u16)==2 ); |
| 912 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 913 | assert( sizeof(ptr)==sizeof(char*) ); |
| 914 | assert( sizeof(uptr)==sizeof(ptr) ); |
| 915 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 916 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 917 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 918 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 919 | return SQLITE_NOMEM; |
| 920 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 921 | if( nCache<10 ) nCache = 10; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 922 | rc = sqlite3pager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE, |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 923 | (flags & BTREE_OMIT_JOURNAL)==0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 924 | if( rc!=SQLITE_OK ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 925 | if( pBt->pPager ) sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 926 | sqliteFree(pBt); |
| 927 | *ppBtree = 0; |
| 928 | return rc; |
| 929 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 930 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 931 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 932 | pBt->pPage1 = 0; |
| 933 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 934 | pBt->pageSize = SQLITE_PAGE_SIZE; /* FIX ME - read from header */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 935 | pBt->usableSize = pBt->pageSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 936 | pBt->maxEmbedFrac = 64; /* FIX ME - read from header */ |
| 937 | pBt->minEmbedFrac = 32; /* FIX ME - read from header */ |
| 938 | pBt->minLeafFrac = 32; /* FIX ME - read from header */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 939 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 940 | *ppBtree = pBt; |
| 941 | return SQLITE_OK; |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | ** Close an open database and invalidate all cursors. |
| 946 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 947 | int sqlite3BtreeClose(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 948 | while( pBt->pCursor ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 949 | sqlite3BtreeCloseCursor(pBt->pCursor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 950 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 951 | sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 952 | sqliteFree(pBt); |
| 953 | return SQLITE_OK; |
| 954 | } |
| 955 | |
| 956 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 957 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 958 | ** |
| 959 | ** The maximum number of cache pages is set to the absolute |
| 960 | ** value of mxPage. If mxPage is negative, the pager will |
| 961 | ** operate asynchronously - it will not stop to do fsync()s |
| 962 | ** to insure data is written to the disk surface before |
| 963 | ** continuing. Transactions still work if synchronous is off, |
| 964 | ** and the database cannot be corrupted if this program |
| 965 | ** crashes. But if the operating system crashes or there is |
| 966 | ** an abrupt power failure when synchronous is off, the database |
| 967 | ** could be left in an inconsistent and unrecoverable state. |
| 968 | ** Synchronous is on by default so database corruption is not |
| 969 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 970 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 971 | int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 972 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 973 | return SQLITE_OK; |
| 974 | } |
| 975 | |
| 976 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 977 | ** Change the way data is synced to disk in order to increase or decrease |
| 978 | ** how well the database resists damage due to OS crashes and power |
| 979 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 980 | ** there is a high probability of damage) Level 2 is the default. There |
| 981 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 982 | ** probability of damage to near zero but with a write performance reduction. |
| 983 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 984 | int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 985 | sqlite3pager_set_safety_level(pBt->pPager, level); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 986 | return SQLITE_OK; |
| 987 | } |
| 988 | |
| 989 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 990 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 991 | ** also acquire a readlock on that file. |
| 992 | ** |
| 993 | ** SQLITE_OK is returned on success. If the file is not a |
| 994 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 995 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 996 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 997 | ** if there is a locking protocol violation. |
| 998 | */ |
| 999 | static int lockBtree(Btree *pBt){ |
| 1000 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1001 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1002 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1003 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1004 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1005 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1006 | |
| 1007 | /* Do some checking to help insure the file we opened really is |
| 1008 | ** a valid database file. |
| 1009 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1010 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1011 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1012 | u8 *page1 = pPage1->aData; |
| 1013 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1014 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1015 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1016 | if( page1[18]>1 || page1[19]>1 ){ |
| 1017 | goto page1_init_failed; |
| 1018 | } |
| 1019 | pBt->pageSize = get2byte(&page1[16]); |
| 1020 | pBt->usableSize = pBt->pageSize - page1[20]; |
| 1021 | if( pBt->usableSize<500 ){ |
| 1022 | goto page1_init_failed; |
| 1023 | } |
| 1024 | pBt->maxEmbedFrac = page1[21]; |
| 1025 | pBt->minEmbedFrac = page1[22]; |
| 1026 | pBt->minLeafFrac = page1[23]; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1027 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1028 | |
| 1029 | /* maxLocal is the maximum amount of payload to store locally for |
| 1030 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1031 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1032 | ** Besides the payload, the cell must store: |
| 1033 | ** 2-byte pointer to next cell |
| 1034 | ** 4-byte child pointer |
| 1035 | ** 9-byte nKey value |
| 1036 | ** 4-byte nData value |
| 1037 | ** 4-byte overflow page pointer |
| 1038 | ** So a cell consists of a header which is as much as 19 bytes long, |
| 1039 | ** 0 to N bytes of payload, and an optional 4 byte overflow page pointer. |
| 1040 | */ |
| 1041 | pBt->maxLocal = (pBt->usableSize-10)*pBt->maxEmbedFrac/255 - 23; |
| 1042 | pBt->minLocal = (pBt->usableSize-10)*pBt->minEmbedFrac/255 - 23; |
| 1043 | pBt->maxLeaf = pBt->usableSize - 33; |
| 1044 | pBt->minLeaf = (pBt->usableSize-10)*pBt->minLeafFrac/255 - 23; |
| 1045 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1046 | goto page1_init_failed; |
| 1047 | } |
| 1048 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1049 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1050 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1051 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1052 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1053 | releasePage(pPage1); |
| 1054 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1055 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1059 | ** If there are no outstanding cursors and we are not in the middle |
| 1060 | ** of a transaction but there is a read lock on the database, then |
| 1061 | ** this routine unrefs the first page of the database file which |
| 1062 | ** has the effect of releasing the read lock. |
| 1063 | ** |
| 1064 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1065 | ** |
| 1066 | ** If there is a transaction in progress, this routine is a no-op. |
| 1067 | */ |
| 1068 | static void unlockBtreeIfUnused(Btree *pBt){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1069 | if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
| 1070 | releasePage(pBt->pPage1); |
| 1071 | pBt->pPage1 = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1072 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1073 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1078 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1079 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1080 | */ |
| 1081 | static int newDatabase(Btree *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1082 | MemPage *pP1; |
| 1083 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1084 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1085 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1086 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1087 | assert( pP1!=0 ); |
| 1088 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1089 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1090 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1091 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1092 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1093 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1094 | data[18] = 1; |
| 1095 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1096 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1097 | data[21] = pBt->maxEmbedFrac; |
| 1098 | data[22] = pBt->minEmbedFrac; |
| 1099 | data[23] = pBt->minLeafFrac; |
| 1100 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1101 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1102 | return SQLITE_OK; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1106 | ** Attempt to start a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1107 | ** |
| 1108 | ** A transaction must be started before attempting any changes |
| 1109 | ** to the database. None of the following routines will work |
| 1110 | ** unless a transaction is started first: |
| 1111 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1112 | ** sqlite3BtreeCreateTable() |
| 1113 | ** sqlite3BtreeCreateIndex() |
| 1114 | ** sqlite3BtreeClearTable() |
| 1115 | ** sqlite3BtreeDropTable() |
| 1116 | ** sqlite3BtreeInsert() |
| 1117 | ** sqlite3BtreeDelete() |
| 1118 | ** sqlite3BtreeUpdateMeta() |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1119 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1120 | int sqlite3BtreeBeginTrans(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1121 | int rc; |
| 1122 | if( pBt->inTrans ) return SQLITE_ERROR; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1123 | if( pBt->readOnly ) return SQLITE_READONLY; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1124 | if( pBt->pPage1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1125 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1126 | if( rc!=SQLITE_OK ){ |
| 1127 | return rc; |
| 1128 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1129 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1130 | rc = sqlite3pager_begin(pBt->pPage1->aData); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1131 | if( rc==SQLITE_OK ){ |
| 1132 | rc = newDatabase(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1133 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1134 | if( rc==SQLITE_OK ){ |
| 1135 | pBt->inTrans = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1136 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1137 | }else{ |
| 1138 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1139 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1140 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1144 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1145 | ** |
| 1146 | ** This will release the write lock on the database file. If there |
| 1147 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1148 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1149 | int sqlite3BtreeCommit(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1150 | int rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1151 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_commit(pBt->pPager); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1152 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1153 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1154 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1155 | return rc; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1159 | ** Invalidate all cursors |
| 1160 | */ |
| 1161 | static void invalidateCursors(Btree *pBt){ |
| 1162 | BtCursor *pCur; |
| 1163 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1164 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1165 | if( pPage /* && !pPage->isInit */ ){ |
| 1166 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1167 | releasePage(pPage); |
| 1168 | pCur->pPage = 0; |
| 1169 | pCur->isValid = 0; |
| 1170 | pCur->status = SQLITE_ABORT; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1175 | #ifdef SQLITE_TEST |
| 1176 | /* |
| 1177 | ** Print debugging information about all cursors to standard output. |
| 1178 | */ |
| 1179 | void sqlite3BtreeCursorList(Btree *pBt){ |
| 1180 | BtCursor *pCur; |
| 1181 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1182 | MemPage *pPage = pCur->pPage; |
| 1183 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
| 1184 | printf("CURSOR %08x rooted at %4d(%s) currently at %d.%d%s\n", |
| 1185 | (int)pCur, pCur->pgnoRoot, zMode, |
| 1186 | pPage ? pPage->pgno : 0, pCur->idx, |
| 1187 | pCur->isValid ? "" : " eof" |
| 1188 | ); |
| 1189 | } |
| 1190 | } |
| 1191 | #endif |
| 1192 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1193 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1194 | ** Rollback the transaction in progress. All cursors will be |
| 1195 | ** invalided by this operation. Any attempt to use a cursor |
| 1196 | ** that was open at the beginning of this operation will result |
| 1197 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1198 | ** |
| 1199 | ** This will release the write lock on the database file. If there |
| 1200 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1201 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1202 | int sqlite3BtreeRollback(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1203 | int rc; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1204 | MemPage *pPage1; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1205 | if( pBt->inTrans==0 ) return SQLITE_OK; |
| 1206 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1207 | pBt->inStmt = 0; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1208 | if( pBt->readOnly ){ |
| 1209 | rc = SQLITE_OK; |
| 1210 | }else{ |
| 1211 | rc = sqlite3pager_rollback(pBt->pPager); |
| 1212 | /* The rollback may have destroyed the pPage1->aData value. So |
| 1213 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 1214 | ** set correctly. */ |
| 1215 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 1216 | releasePage(pPage1); |
| 1217 | } |
| 1218 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1219 | invalidateCursors(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1220 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1221 | return rc; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1225 | ** Set the checkpoint for the current transaction. The checkpoint serves |
| 1226 | ** as a sub-transaction that can be rolled back independently of the |
| 1227 | ** main transaction. You must start a transaction before starting a |
| 1228 | ** checkpoint. The checkpoint is ended automatically if the transaction |
| 1229 | ** commits or rolls back. |
| 1230 | ** |
| 1231 | ** Only one checkpoint may be active at a time. It is an error to try |
| 1232 | ** to start a new checkpoint if another checkpoint is already active. |
| 1233 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1234 | int sqlite3BtreeBeginStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1235 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1236 | if( !pBt->inTrans || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1237 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 1238 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1239 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1240 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1241 | return rc; |
| 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | /* |
| 1246 | ** Commit a checkpoint to transaction currently in progress. If no |
| 1247 | ** checkpoint is active, this is a no-op. |
| 1248 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1249 | int sqlite3BtreeCommitStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1250 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1251 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1252 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1253 | }else{ |
| 1254 | rc = SQLITE_OK; |
| 1255 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1256 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1257 | return rc; |
| 1258 | } |
| 1259 | |
| 1260 | /* |
| 1261 | ** Rollback the checkpoint to the current transaction. If there |
| 1262 | ** is no active checkpoint or transaction, this routine is a no-op. |
| 1263 | ** |
| 1264 | ** All cursors will be invalided by this operation. Any attempt |
| 1265 | ** to use a cursor that was open at the beginning of this operation |
| 1266 | ** will result in an error. |
| 1267 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1268 | int sqlite3BtreeRollbackStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1269 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1270 | if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1271 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1272 | invalidateCursors(pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1273 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1274 | return rc; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1278 | ** Default key comparison function to be used if no comparison function |
| 1279 | ** is specified on the sqlite3BtreeCursor() call. |
| 1280 | */ |
| 1281 | static int dfltCompare( |
| 1282 | void *NotUsed, /* User data is not used */ |
| 1283 | int n1, const void *p1, /* First key to compare */ |
| 1284 | int n2, const void *p2 /* Second key to compare */ |
| 1285 | ){ |
| 1286 | int c; |
| 1287 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 1288 | if( c==0 ){ |
| 1289 | c = n1 - n2; |
| 1290 | } |
| 1291 | return c; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1295 | ** Create a new cursor for the BTree whose root is on the page |
| 1296 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 1297 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1298 | ** |
| 1299 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1300 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 1301 | ** writing if other conditions for writing are also met. These |
| 1302 | ** are the conditions that must be met in order for writing to |
| 1303 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1304 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1305 | ** 1: The cursor must have been opened with wrFlag==1 |
| 1306 | ** |
| 1307 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 1308 | ** |
| 1309 | ** 3: The database must be writable (not on read-only media) |
| 1310 | ** |
| 1311 | ** 4: There must be an active transaction. |
| 1312 | ** |
| 1313 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 1314 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 1315 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 1316 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 1317 | ** change as long as the cursor is open. This allows the cursor to |
| 1318 | ** do a sequential scan of the table without having to worry about |
| 1319 | ** entries being inserted or deleted during the scan. Cursors should |
| 1320 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 1321 | ** 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] | 1322 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1323 | ** should be opened with wrFlag==1 even if they never really intend |
| 1324 | ** to write. |
| 1325 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1326 | ** No checking is done to make sure that page iTable really is the |
| 1327 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 1328 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1329 | ** |
| 1330 | ** The comparison function must be logically the same for every cursor |
| 1331 | ** on a particular table. Changing the comparison function will result |
| 1332 | ** in incorrect operations. If the comparison function is NULL, a |
| 1333 | ** default comparison function is used. The comparison function is |
| 1334 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1335 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1336 | int sqlite3BtreeCursor( |
| 1337 | Btree *pBt, /* The btree */ |
| 1338 | int iTable, /* Root page of table to open */ |
| 1339 | int wrFlag, /* 1 to write. 0 read-only */ |
| 1340 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 1341 | void *pArg, /* First arg to xCompare() */ |
| 1342 | BtCursor **ppCur /* Write new cursor here */ |
| 1343 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1344 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1345 | BtCursor *pCur, *pRing; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1346 | |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 1347 | if( pBt->readOnly && wrFlag ){ |
| 1348 | *ppCur = 0; |
| 1349 | return SQLITE_READONLY; |
| 1350 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1351 | if( pBt->pPage1==0 ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1352 | rc = lockBtree(pBt); |
| 1353 | if( rc!=SQLITE_OK ){ |
| 1354 | *ppCur = 0; |
| 1355 | return rc; |
| 1356 | } |
| 1357 | } |
| 1358 | pCur = sqliteMalloc( sizeof(*pCur) ); |
| 1359 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1360 | rc = SQLITE_NOMEM; |
| 1361 | goto create_cursor_exception; |
| 1362 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1363 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1364 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 1365 | rc = SQLITE_EMPTY; |
| 1366 | goto create_cursor_exception; |
| 1367 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1368 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1369 | if( rc!=SQLITE_OK ){ |
| 1370 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1371 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1372 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 1373 | pCur->pArg = pArg; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1374 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1375 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1376 | pCur->idx = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1377 | pCur->pNext = pBt->pCursor; |
| 1378 | if( pCur->pNext ){ |
| 1379 | pCur->pNext->pPrev = pCur; |
| 1380 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1381 | pCur->pPrev = 0; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1382 | pRing = pBt->pCursor; |
| 1383 | while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; } |
| 1384 | if( pRing ){ |
| 1385 | pCur->pShared = pRing->pShared; |
| 1386 | pRing->pShared = pCur; |
| 1387 | }else{ |
| 1388 | pCur->pShared = pCur; |
| 1389 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1390 | pBt->pCursor = pCur; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1391 | pCur->isValid = 0; |
| 1392 | pCur->status = SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1393 | *ppCur = pCur; |
| 1394 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1395 | |
| 1396 | create_cursor_exception: |
| 1397 | *ppCur = 0; |
| 1398 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1399 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1400 | sqliteFree(pCur); |
| 1401 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1402 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1403 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1407 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1408 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1409 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1410 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1411 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1412 | if( pCur->pPrev ){ |
| 1413 | pCur->pPrev->pNext = pCur->pNext; |
| 1414 | }else{ |
| 1415 | pBt->pCursor = pCur->pNext; |
| 1416 | } |
| 1417 | if( pCur->pNext ){ |
| 1418 | pCur->pNext->pPrev = pCur->pPrev; |
| 1419 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1420 | releasePage(pCur->pPage); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1421 | if( pCur->pShared!=pCur ){ |
| 1422 | BtCursor *pRing = pCur->pShared; |
| 1423 | while( pRing->pShared!=pCur ){ pRing = pRing->pShared; } |
| 1424 | pRing->pShared = pCur->pShared; |
| 1425 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1426 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1427 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1428 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1431 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1432 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 1433 | ** The temporary cursor is not on the cursor list for the Btree. |
| 1434 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1435 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1436 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 1437 | pTempCur->pNext = 0; |
| 1438 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1439 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1440 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1441 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1445 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1446 | ** function above. |
| 1447 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1448 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1449 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1450 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1451 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1455 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 1456 | ** the key for the current entry. If the cursor is not pointing |
| 1457 | ** to a valid entry, *pSize is set to 0. |
| 1458 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1459 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1460 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1461 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1462 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1463 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1464 | unsigned char *cell; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1465 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1466 | if( !pCur->isValid ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1467 | *pSize = 0; |
| 1468 | }else{ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1469 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1470 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1471 | assert( pPage!=0 ); |
| 1472 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1473 | cell = pPage->aCell[pCur->idx]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1474 | cell += 2; /* Skip the offset to the next cell */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1475 | if( !pPage->leaf ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1476 | cell += 4; /* Skip the child pointer */ |
| 1477 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 1478 | if( pPage->hasData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1479 | while( (0x80&*(cell++))!=0 ){} /* Skip the data size number */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1480 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1481 | getVarint(cell, pSize); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1482 | } |
| 1483 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1484 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1485 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1486 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1487 | ** Set *pSize to the number of bytes of data in the entry the |
| 1488 | ** cursor currently points to. Always return SQLITE_OK. |
| 1489 | ** Failure is not possible. If the cursor is not currently |
| 1490 | ** pointing to an entry (which can happen, for example, if |
| 1491 | ** the database is empty) then *pSize is set to 0. |
| 1492 | */ |
| 1493 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
| 1494 | MemPage *pPage; |
| 1495 | unsigned char *cell; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1496 | |
| 1497 | if( !pCur->isValid ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1498 | /* Not pointing at a valid entry - set *pSize to 0. */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1499 | *pSize = 0; |
| 1500 | }else{ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1501 | pPage = pCur->pPage; |
| 1502 | assert( pPage!=0 ); |
| 1503 | assert( pPage->isInit ); |
| 1504 | pageIntegrity(pPage); |
| 1505 | if( !pPage->hasData ){ |
| 1506 | *pSize = 0; |
| 1507 | }else{ |
| 1508 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1509 | cell = pPage->aCell[pCur->idx]; |
| 1510 | cell += 2; /* Skip the offset to the next cell */ |
| 1511 | if( !pPage->leaf ){ |
| 1512 | cell += 4; /* Skip the child pointer */ |
| 1513 | } |
| 1514 | getVarint32(cell, pSize); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1515 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1516 | } |
| 1517 | return SQLITE_OK; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1521 | ** Read payload information from the entry that the pCur cursor is |
| 1522 | ** pointing to. Begin reading the payload at "offset" and read |
| 1523 | ** a total of "amt" bytes. Put the result in zBuf. |
| 1524 | ** |
| 1525 | ** This routine does not make a distinction between key and data. |
| 1526 | ** It just reads bytes from the payload area. |
| 1527 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1528 | static int getPayload( |
| 1529 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 1530 | int offset, /* Begin reading this far into payload */ |
| 1531 | int amt, /* Read this many bytes */ |
| 1532 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 1533 | int skipKey /* offset begins at data if this is true */ |
| 1534 | ){ |
| 1535 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1536 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1537 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1538 | MemPage *pPage; |
| 1539 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1540 | int ovflSize; |
| 1541 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1542 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1543 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1544 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1545 | pBt = pCur->pBt; |
| 1546 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1547 | pageIntegrity(pPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1548 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1549 | aPayload = pPage->aCell[pCur->idx]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1550 | parseCell(pPage, aPayload, &info); |
| 1551 | aPayload += info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1552 | if( pPage->intKey ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1553 | info.nKey = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1554 | } |
| 1555 | assert( offset>=0 ); |
| 1556 | if( skipKey ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1557 | offset += info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1558 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1559 | if( offset+amt > info.nKey+info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1560 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1561 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1562 | if( offset<info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1563 | int a = amt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1564 | if( a+offset>info.nLocal ){ |
| 1565 | a = info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1566 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1567 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1568 | if( a==amt ){ |
| 1569 | return SQLITE_OK; |
| 1570 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1571 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1572 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1573 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1574 | }else{ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1575 | offset -= info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1576 | } |
| 1577 | if( amt>0 ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1578 | nextPage = get4byte(&aPayload[info.nLocal]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1579 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1580 | ovflSize = pBt->usableSize - 4; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1581 | while( amt>0 && nextPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1582 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1583 | if( rc!=0 ){ |
| 1584 | return rc; |
| 1585 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1586 | nextPage = get4byte(aPayload); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1587 | if( offset<ovflSize ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1588 | int a = amt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1589 | if( a + offset > ovflSize ){ |
| 1590 | a = ovflSize - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1591 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 1592 | memcpy(pBuf, &aPayload[offset+4], a); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1593 | offset = 0; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1594 | amt -= a; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1595 | pBuf += a; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1596 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1597 | offset -= ovflSize; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1598 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1599 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1600 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1601 | if( amt>0 ){ |
| 1602 | return SQLITE_CORRUPT; |
| 1603 | } |
| 1604 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1607 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1608 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1609 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1610 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1611 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1612 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1613 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1614 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1615 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1616 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1617 | assert( amt>=0 ); |
| 1618 | assert( offset>=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1619 | if( pCur->isValid==0 ){ |
| 1620 | return pCur->status; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1621 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1622 | assert( pCur->pPage!=0 ); |
| 1623 | assert( pCur->pPage->intKey==0 ); |
| 1624 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1625 | return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 1626 | } |
| 1627 | |
| 1628 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1629 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1630 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1631 | ** begins at "offset". |
| 1632 | ** |
| 1633 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1634 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1635 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1636 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1637 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1638 | if( !pCur->isValid ){ |
| 1639 | return pCur->status ? pCur->status : SQLITE_INTERNAL; |
| 1640 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1641 | assert( amt>=0 ); |
| 1642 | assert( offset>=0 ); |
| 1643 | assert( pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1644 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1645 | return getPayload(pCur, offset, amt, pBuf, 1); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1648 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1649 | ** Return a pointer to payload information from the entry that the |
| 1650 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 1651 | ** the key if skipKey==0 and it points to the beginning of data if |
| 1652 | ** skipKey==1. |
| 1653 | ** |
| 1654 | ** At least amt bytes of information must be available on the local |
| 1655 | ** page or else this routine returns NULL. If amt<0 then the entire |
| 1656 | ** key/data must be available. |
| 1657 | ** |
| 1658 | ** This routine is an optimization. It is common for the entire key |
| 1659 | ** and data to fit on the local page and for there to be no overflow |
| 1660 | ** pages. When that is so, this routine can be used to access the |
| 1661 | ** key and data without making a copy. If the key and/or data spills |
| 1662 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 1663 | ** the key/data and copy it into a preallocated buffer. |
| 1664 | ** |
| 1665 | ** The pointer returned by this routine looks directly into the cached |
| 1666 | ** page of the database. The data might change or move the next time |
| 1667 | ** any btree routine is called. |
| 1668 | */ |
| 1669 | static const unsigned char *fetchPayload( |
| 1670 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 1671 | int amt, /* Amount requested */ |
| 1672 | int skipKey /* read beginning at data if this is true */ |
| 1673 | ){ |
| 1674 | unsigned char *aPayload; |
| 1675 | MemPage *pPage; |
| 1676 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1677 | CellInfo info; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1678 | |
| 1679 | assert( pCur!=0 && pCur->pPage!=0 ); |
| 1680 | assert( pCur->isValid ); |
| 1681 | pBt = pCur->pBt; |
| 1682 | pPage = pCur->pPage; |
| 1683 | pageIntegrity(pPage); |
| 1684 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1685 | aPayload = pPage->aCell[pCur->idx]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1686 | parseCell(pPage, aPayload, &info); |
| 1687 | aPayload += info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1688 | if( pPage->intKey ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1689 | info.nKey = 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1690 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1691 | if( skipKey ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1692 | aPayload += info.nKey; |
| 1693 | info.nLocal -= info.nKey; |
| 1694 | if( amt<0 ) amt = info.nData; |
| 1695 | assert( amt<=info.nData ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1696 | }else{ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1697 | if( amt<0 ) amt = info.nKey; |
| 1698 | assert( amt<=info.nKey ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1699 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1700 | if( amt>info.nLocal ){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1701 | return 0; /* If any of the data is not local, return nothing */ |
| 1702 | } |
| 1703 | return aPayload; |
| 1704 | } |
| 1705 | |
| 1706 | |
| 1707 | /* |
| 1708 | ** Return a pointer to the first amt bytes of the key or data |
| 1709 | ** for record that cursor pCur is point to if the entire request |
| 1710 | ** exists in contiguous memory on the main tree page. If any |
| 1711 | ** any part of the request is on an overflow page, return 0. |
| 1712 | ** If pCur is not pointing to a valid entry return 0. |
| 1713 | ** |
| 1714 | ** If amt<0 then return the entire key or data. |
| 1715 | ** |
| 1716 | ** The pointer returned is ephemeral. The key/data may move |
| 1717 | ** or be destroyed on the next call to any Btree routine. |
| 1718 | ** |
| 1719 | ** These routines is used to get quick access to key and data |
| 1720 | ** in the common case where no overflow pages are used. |
| 1721 | ** |
| 1722 | ** It is a fatal error to call these routines with amt values that |
| 1723 | ** are larger than the key/data size. |
| 1724 | */ |
| 1725 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int amt){ |
| 1726 | return (const void*)fetchPayload(pCur, amt, 0); |
| 1727 | } |
| 1728 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int amt){ |
| 1729 | return (const void*)fetchPayload(pCur, amt, 1); |
| 1730 | } |
| 1731 | |
| 1732 | |
| 1733 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1734 | ** Move the cursor down to a new child page. The newPgno argument is the |
| 1735 | ** page number of the child page in the byte order of the disk image. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1736 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1737 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1738 | int rc; |
| 1739 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1740 | MemPage *pOldPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1741 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1742 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1743 | assert( pCur->isValid ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1744 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1745 | if( rc ) return rc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1746 | pageIntegrity(pNewPage); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1747 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1748 | pOldPage = pCur->pPage; |
| 1749 | pOldPage->idxShift = 0; |
| 1750 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1751 | pCur->pPage = pNewPage; |
| 1752 | pCur->idx = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 1753 | if( pNewPage->nCell<1 ){ |
| 1754 | return SQLITE_CORRUPT; |
| 1755 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1756 | return SQLITE_OK; |
| 1757 | } |
| 1758 | |
| 1759 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1760 | ** Return true if the page is the virtual root of its table. |
| 1761 | ** |
| 1762 | ** The virtual root page is the root page for most tables. But |
| 1763 | ** for the table rooted on page 1, sometime the real root page |
| 1764 | ** is empty except for the right-pointer. In such cases the |
| 1765 | ** virtual root page is the page that the right-pointer of page |
| 1766 | ** 1 is pointing to. |
| 1767 | */ |
| 1768 | static int isRootPage(MemPage *pPage){ |
| 1769 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1770 | if( pParent==0 ) return 1; |
| 1771 | if( pParent->pgno>1 ) return 0; |
| 1772 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1777 | ** Move the cursor up to the parent page. |
| 1778 | ** |
| 1779 | ** pCur->idx is set to the cell index that contains the pointer |
| 1780 | ** to the page we are coming from. If we are coming from the |
| 1781 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1782 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1783 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1784 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1785 | Pgno oldPgno; |
| 1786 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1787 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1788 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1789 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1790 | assert( pCur->isValid ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1791 | pPage = pCur->pPage; |
| 1792 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1793 | assert( !isRootPage(pPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1794 | pageIntegrity(pPage); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1795 | pParent = pPage->pParent; |
| 1796 | assert( pParent!=0 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1797 | pageIntegrity(pParent); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1798 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1799 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1800 | oldPgno = pPage->pgno; |
| 1801 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1802 | pCur->pPage = pParent; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1803 | assert( pParent->idxShift==0 ); |
| 1804 | if( pParent->idxShift==0 ){ |
| 1805 | pCur->idx = idxParent; |
| 1806 | #ifndef NDEBUG |
| 1807 | /* Verify that pCur->idx is the correct index to point back to the child |
| 1808 | ** page we just came from |
| 1809 | */ |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1810 | if( pCur->idx<pParent->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1811 | assert( get4byte(&pParent->aCell[idxParent][2])==oldPgno ); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1812 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1813 | assert( get4byte(&pParent->aData[pParent->hdrOffset+6])==oldPgno ); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1814 | } |
| 1815 | #endif |
| 1816 | }else{ |
| 1817 | /* The MemPage.idxShift flag indicates that cell indices might have |
| 1818 | ** changed since idxParent was set and hence idxParent might be out |
| 1819 | ** of date. So recompute the parent cell index by scanning all cells |
| 1820 | ** and locating the one that points to the child we just came from. |
| 1821 | */ |
| 1822 | int i; |
| 1823 | pCur->idx = pParent->nCell; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1824 | for(i=0; i<pParent->nCell; i++){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1825 | if( get4byte(&pParent->aCell[i][2])==oldPgno ){ |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1826 | pCur->idx = i; |
| 1827 | break; |
| 1828 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | ** Move the cursor to the root page |
| 1835 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1836 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1837 | MemPage *pRoot; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1838 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1839 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1840 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1841 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1842 | if( rc ){ |
| 1843 | pCur->isValid = 0; |
| 1844 | return rc; |
| 1845 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1846 | releasePage(pCur->pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1847 | pageIntegrity(pRoot); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1848 | pCur->pPage = pRoot; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1849 | pCur->idx = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1850 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 1851 | Pgno subpage; |
| 1852 | assert( pRoot->pgno==1 ); |
| 1853 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+6]); |
| 1854 | assert( subpage>0 ); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 1855 | pCur->isValid = 1; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1856 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1857 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1858 | pCur->isValid = pCur->pPage->nCell>0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1859 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1860 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1861 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1862 | /* |
| 1863 | ** Move the cursor down to the left-most leaf entry beneath the |
| 1864 | ** entry to which it is currently pointing. |
| 1865 | */ |
| 1866 | static int moveToLeftmost(BtCursor *pCur){ |
| 1867 | Pgno pgno; |
| 1868 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1869 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1870 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1871 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1872 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1873 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1874 | pgno = get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1875 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1876 | if( rc ) return rc; |
| 1877 | } |
| 1878 | return SQLITE_OK; |
| 1879 | } |
| 1880 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1881 | /* |
| 1882 | ** Move the cursor down to the right-most leaf entry beneath the |
| 1883 | ** page to which it is currently pointing. Notice the difference |
| 1884 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 1885 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 1886 | ** finds the right-most entry beneath the *page*. |
| 1887 | */ |
| 1888 | static int moveToRightmost(BtCursor *pCur){ |
| 1889 | Pgno pgno; |
| 1890 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1891 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1892 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1893 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1894 | while( !(pPage = pCur->pPage)->leaf ){ |
| 1895 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 1896 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1897 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1898 | if( rc ) return rc; |
| 1899 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1900 | pCur->idx = pPage->nCell - 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1901 | return SQLITE_OK; |
| 1902 | } |
| 1903 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1904 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 1905 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1906 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1907 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1908 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1909 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1910 | if( pCur->status ){ |
| 1911 | return pCur->status; |
| 1912 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1913 | rc = moveToRoot(pCur); |
| 1914 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1915 | if( pCur->isValid==0 ){ |
| 1916 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1917 | *pRes = 1; |
| 1918 | return SQLITE_OK; |
| 1919 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1920 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1921 | *pRes = 0; |
| 1922 | rc = moveToLeftmost(pCur); |
| 1923 | return rc; |
| 1924 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1925 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1926 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 1927 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1928 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1929 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1930 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1931 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1932 | if( pCur->status ){ |
| 1933 | return pCur->status; |
| 1934 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1935 | rc = moveToRoot(pCur); |
| 1936 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1937 | if( pCur->isValid==0 ){ |
| 1938 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1939 | *pRes = 1; |
| 1940 | return SQLITE_OK; |
| 1941 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1942 | assert( pCur->isValid ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1943 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1944 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1945 | return rc; |
| 1946 | } |
| 1947 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1948 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1949 | ** Return a success code. |
| 1950 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1951 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 1952 | ** ignored. For other tables, nKey is the number of bytes of data |
| 1953 | ** in nKey. The comparison function specified when the cursor was |
| 1954 | ** created is used to compare keys. |
| 1955 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1956 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1957 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1958 | ** were present. The cursor might point to an entry that comes |
| 1959 | ** before or after the key. |
| 1960 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1961 | ** The result of comparing the key with the entry to which the |
| 1962 | ** cursor is left pointing is stored in pCur->iMatch. The same |
| 1963 | ** value is also written to *pRes if pRes!=NULL. The meaning of |
| 1964 | ** this value is as follows: |
| 1965 | ** |
| 1966 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 1967 | ** is smaller than pKey or if the table is empty |
| 1968 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1969 | ** |
| 1970 | ** *pRes==0 The cursor is left pointing at an entry that |
| 1971 | ** exactly matches pKey. |
| 1972 | ** |
| 1973 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1974 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1975 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1976 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1977 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1978 | |
| 1979 | if( pCur->status ){ |
| 1980 | return pCur->status; |
| 1981 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1982 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1983 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1984 | assert( pCur->pPage ); |
| 1985 | assert( pCur->pPage->isInit ); |
| 1986 | if( pCur->isValid==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 1987 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1988 | assert( pCur->pPage->nCell==0 ); |
| 1989 | return SQLITE_OK; |
| 1990 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1991 | for(;;){ |
| 1992 | int lwr, upr; |
| 1993 | Pgno chldPg; |
| 1994 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 1995 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1996 | lwr = 0; |
| 1997 | upr = pPage->nCell-1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1998 | pageIntegrity(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1999 | while( lwr<=upr ){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2000 | const void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2001 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2002 | pCur->idx = (lwr+upr)/2; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2003 | sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2004 | if( pPage->intKey ){ |
| 2005 | if( nCellKey<nKey ){ |
| 2006 | c = -1; |
| 2007 | }else if( nCellKey>nKey ){ |
| 2008 | c = +1; |
| 2009 | }else{ |
| 2010 | c = 0; |
| 2011 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2012 | }else if( (pCellKey = sqlite3BtreeKeyFetch(pCur, nCellKey))!=0 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2013 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2014 | }else{ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2015 | u8 *pCellKey = sqliteMalloc( nCellKey ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2016 | if( pCellKey==0 ) return SQLITE_NOMEM; |
| 2017 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey); |
| 2018 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2019 | sqliteFree(pCellKey); |
| 2020 | if( rc ) return rc; |
| 2021 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2022 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2023 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 2024 | lwr = pCur->idx; |
| 2025 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2026 | break; |
| 2027 | }else{ |
| 2028 | pCur->iMatch = c; |
| 2029 | if( pRes ) *pRes = 0; |
| 2030 | return SQLITE_OK; |
| 2031 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2032 | } |
| 2033 | if( c<0 ){ |
| 2034 | lwr = pCur->idx+1; |
| 2035 | }else{ |
| 2036 | upr = pCur->idx-1; |
| 2037 | } |
| 2038 | } |
| 2039 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2040 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2041 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2042 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2043 | }else if( lwr>=pPage->nCell ){ |
| 2044 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2045 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2046 | chldPg = get4byte(&pPage->aCell[lwr][2]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2047 | } |
| 2048 | if( chldPg==0 ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2049 | pCur->iMatch = c; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2050 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2051 | if( pRes ) *pRes = c; |
| 2052 | return SQLITE_OK; |
| 2053 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2054 | pCur->idx = lwr; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2055 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2056 | if( rc ){ |
| 2057 | return rc; |
| 2058 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2059 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2060 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2064 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 2065 | ** |
| 2066 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 2067 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 2068 | ** the first entry. TRUE is also returned if the table is empty. |
| 2069 | */ |
| 2070 | int sqlite3BtreeEof(BtCursor *pCur){ |
| 2071 | return pCur->isValid==0; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2075 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2076 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2077 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2078 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2079 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2080 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2081 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2082 | MemPage *pPage = pCur->pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2083 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2084 | assert( pRes!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2085 | if( pCur->isValid==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2086 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2087 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2088 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2089 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2090 | assert( pCur->idx<pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2091 | pCur->idx++; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2092 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2093 | if( !pPage->leaf ){ |
| 2094 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+6])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2095 | if( rc ) return rc; |
| 2096 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2097 | *pRes = 0; |
| 2098 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2099 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2100 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2101 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2102 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2103 | pCur->isValid = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2104 | return SQLITE_OK; |
| 2105 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2106 | moveToParent(pCur); |
| 2107 | pPage = pCur->pPage; |
| 2108 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2109 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2110 | if( pPage->leafData ){ |
| 2111 | rc = sqlite3BtreeNext(pCur, pRes); |
| 2112 | }else{ |
| 2113 | rc = SQLITE_OK; |
| 2114 | } |
| 2115 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2116 | } |
| 2117 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2118 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2119 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2120 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2121 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2122 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2125 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2126 | ** 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] | 2127 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2128 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2129 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2130 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2131 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2132 | int rc; |
| 2133 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2134 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2135 | if( pCur->isValid==0 ){ |
| 2136 | *pRes = 1; |
| 2137 | return SQLITE_OK; |
| 2138 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2139 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2140 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2141 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2142 | if( !pPage->leaf ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2143 | pgno = get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2144 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2145 | if( rc ) return rc; |
| 2146 | rc = moveToRightmost(pCur); |
| 2147 | }else{ |
| 2148 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2149 | if( isRootPage(pPage) ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2150 | pCur->isValid = 0; |
| 2151 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2152 | return SQLITE_OK; |
| 2153 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2154 | moveToParent(pCur); |
| 2155 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | pCur->idx--; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2158 | if( pPage->leafData ){ |
| 2159 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 2160 | }else{ |
| 2161 | rc = SQLITE_OK; |
| 2162 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2163 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2164 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2165 | return rc; |
| 2166 | } |
| 2167 | |
| 2168 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2169 | ** The TRACE macro will print high-level status information about the |
| 2170 | ** btree operation when the global variable sqlite3_btree_trace is |
| 2171 | ** enabled. |
| 2172 | */ |
| 2173 | #if SQLITE_TEST |
| 2174 | # define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); } |
| 2175 | #else |
| 2176 | # define TRACE(X) |
| 2177 | #endif |
| 2178 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
| 2179 | |
| 2180 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2181 | ** Allocate a new page from the database file. |
| 2182 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2183 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2184 | ** has already been called on the new page.) The new page has also |
| 2185 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2186 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2187 | ** |
| 2188 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 2189 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2190 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2191 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2192 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 2193 | ** 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] | 2194 | ** attempt to keep related pages close to each other in the database file, |
| 2195 | ** which in turn can make database access faster. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2196 | */ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2197 | static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2198 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2199 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2200 | int n; /* Number of pages on the freelist */ |
| 2201 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2202 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2203 | pPage1 = pBt->pPage1; |
| 2204 | n = get4byte(&pPage1->aData[36]); |
| 2205 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2206 | /* There are pages on the freelist. Reuse one of those pages. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2207 | MemPage *pTrunk; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2208 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2209 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2210 | put4byte(&pPage1->aData[36], n-1); |
| 2211 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2212 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2213 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2214 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2215 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2216 | return rc; |
| 2217 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2218 | k = get4byte(&pTrunk->aData[4]); |
| 2219 | if( k==0 ){ |
| 2220 | /* The trunk has no leaves. So extract the trunk page itself and |
| 2221 | ** use it as the newly allocated page */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2222 | *pPgno = get4byte(&pPage1->aData[32]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2223 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 2224 | *ppPage = pTrunk; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2225 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2226 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2227 | /* Extract a leaf from the trunk */ |
| 2228 | int closest; |
| 2229 | unsigned char *aData = pTrunk->aData; |
| 2230 | if( nearby>0 ){ |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2231 | int i, dist; |
| 2232 | closest = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2233 | dist = get4byte(&aData[8]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2234 | if( dist<0 ) dist = -dist; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2235 | for(i=1; i<k; i++){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2236 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2237 | if( d2<0 ) d2 = -d2; |
| 2238 | if( d2<dist ) closest = i; |
| 2239 | } |
| 2240 | }else{ |
| 2241 | closest = 0; |
| 2242 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2243 | *pPgno = get4byte(&aData[8+closest*4]); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2244 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d: %d more free pages\n", |
| 2245 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2246 | if( closest<k-1 ){ |
| 2247 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 2248 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2249 | put4byte(&aData[4], k-1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2250 | rc = getPage(pBt, *pPgno, ppPage); |
| 2251 | releasePage(pTrunk); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2252 | if( rc==SQLITE_OK ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2253 | sqlite3pager_dont_rollback((*ppPage)->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2254 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2255 | } |
| 2256 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2257 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2258 | /* There are no pages on the freelist, so create a new page at the |
| 2259 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2260 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2261 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2262 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2263 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2264 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2265 | } |
| 2266 | return rc; |
| 2267 | } |
| 2268 | |
| 2269 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2270 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2271 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2272 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2273 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2274 | static int freePage(MemPage *pPage){ |
| 2275 | Btree *pBt = pPage->pBt; |
| 2276 | MemPage *pPage1 = pBt->pPage1; |
| 2277 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2278 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2279 | /* Prepare the page for freeing */ |
| 2280 | assert( pPage->pgno>1 ); |
| 2281 | pPage->isInit = 0; |
| 2282 | releasePage(pPage->pParent); |
| 2283 | pPage->pParent = 0; |
| 2284 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2285 | /* Increment the free page count on pPage1 */ |
| 2286 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2287 | if( rc ) return rc; |
| 2288 | n = get4byte(&pPage1->aData[36]); |
| 2289 | put4byte(&pPage1->aData[36], n+1); |
| 2290 | |
| 2291 | if( n==0 ){ |
| 2292 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2293 | rc = sqlite3pager_write(pPage->aData); |
| 2294 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2295 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2296 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2297 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2298 | }else{ |
| 2299 | /* Other free pages already exist. Retrive the first trunk page |
| 2300 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2301 | MemPage *pTrunk; |
| 2302 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2303 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2304 | k = get4byte(&pTrunk->aData[4]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2305 | if( k==pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2306 | /* The trunk is full. Turn the page being freed into a new |
| 2307 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2308 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2309 | if( rc ) return rc; |
| 2310 | put4byte(pPage->aData, pTrunk->pgno); |
| 2311 | put4byte(&pPage->aData[4], 0); |
| 2312 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2313 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 2314 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2315 | }else{ |
| 2316 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2317 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2318 | if( rc ) return rc; |
| 2319 | put4byte(&pTrunk->aData[4], k+1); |
| 2320 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2321 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2322 | 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] | 2323 | } |
| 2324 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2325 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2326 | return rc; |
| 2327 | } |
| 2328 | |
| 2329 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2330 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2331 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2332 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 2333 | Btree *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2334 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2335 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2336 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2337 | |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2338 | parseCell(pPage, pCell, &info); |
| 2339 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2340 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2341 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2342 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2343 | while( ovflPgno!=0 ){ |
| 2344 | MemPage *pOvfl; |
| 2345 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2346 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2347 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2348 | rc = freePage(pOvfl); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2349 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2350 | sqlite3pager_unref(pOvfl->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2351 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2352 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2356 | ** Create the byte sequence used to represent a cell on page pPage |
| 2357 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 2358 | ** allocated and filled in as necessary. The calling procedure |
| 2359 | ** is responsible for making sure sufficient space has been allocated |
| 2360 | ** for pCell[]. |
| 2361 | ** |
| 2362 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 2363 | ** area. pCell might point to some temporary storage. The cell will |
| 2364 | ** be constructed in this temporary area then copied into pPage->aData |
| 2365 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2366 | */ |
| 2367 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2368 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2369 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2370 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2371 | const void *pData,int nData, /* The data */ |
| 2372 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2373 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2374 | int nPayload; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2375 | const void *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2376 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2377 | int spaceLeft; |
| 2378 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2379 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2380 | unsigned char *pPrior; |
| 2381 | unsigned char *pPayload; |
| 2382 | Btree *pBt = pPage->pBt; |
| 2383 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2384 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2385 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2386 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2387 | /* Fill in the header. */ |
| 2388 | nHeader = 2; |
| 2389 | if( !pPage->leaf ){ |
| 2390 | nHeader += 4; |
| 2391 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2392 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2393 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2394 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2395 | nData = 0; |
| 2396 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2397 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
| 2398 | parseCell(pPage, pCell, &info); |
| 2399 | assert( info.nHeader==nHeader ); |
| 2400 | assert( info.nKey==nKey ); |
| 2401 | assert( info.nData==nData ); |
| 2402 | |
| 2403 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2404 | nPayload = nData; |
| 2405 | if( pPage->intKey ){ |
| 2406 | pSrc = pData; |
| 2407 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2408 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2409 | }else{ |
| 2410 | nPayload += nKey; |
| 2411 | pSrc = pKey; |
| 2412 | nSrc = nKey; |
| 2413 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2414 | *pnSize = info.nSize; |
| 2415 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2416 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2417 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2418 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2419 | while( nPayload>0 ){ |
| 2420 | if( spaceLeft==0 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2421 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2422 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2423 | releasePage(pToRelease); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2424 | clearCell(pPage, pCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2425 | return rc; |
| 2426 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2427 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2428 | releasePage(pToRelease); |
| 2429 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2430 | pPrior = pOvfl->aData; |
| 2431 | put4byte(pPrior, 0); |
| 2432 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2433 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2434 | } |
| 2435 | n = nPayload; |
| 2436 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2437 | if( n>nSrc ) n = nSrc; |
| 2438 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2439 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2440 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2441 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2442 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2443 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2444 | if( nSrc==0 ){ |
| 2445 | nSrc = nData; |
| 2446 | pSrc = pData; |
| 2447 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2448 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2449 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2450 | return SQLITE_OK; |
| 2451 | } |
| 2452 | |
| 2453 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2454 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2455 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2456 | ** pointer in the third argument. |
| 2457 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2458 | static void reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2459 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2460 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2461 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2462 | if( pgno==0 ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2463 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2464 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2465 | if( aData ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2466 | pThis = (MemPage*)&aData[pBt->usableSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2467 | if( pThis->isInit ){ |
| 2468 | if( pThis->pParent!=pNewParent ){ |
| 2469 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 2470 | pThis->pParent = pNewParent; |
| 2471 | if( pNewParent ) sqlite3pager_ref(pNewParent->aData); |
| 2472 | } |
| 2473 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2474 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2475 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2480 | ** Change the pParent pointer of all children of pPage to point back |
| 2481 | ** to pPage. |
| 2482 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2483 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2484 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2485 | ** |
| 2486 | ** This routine gets called after you memcpy() one page into |
| 2487 | ** another. |
| 2488 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2489 | static void reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2490 | int i; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2491 | Btree *pBt; |
| 2492 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2493 | if( pPage->leaf ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2494 | pBt = pPage->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2495 | for(i=0; i<pPage->nCell; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2496 | reparentPage(pBt, get4byte(&pPage->aCell[i][2]), pPage, i); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2497 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2498 | reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+6]), pPage, i); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2499 | pPage->idxShift = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | /* |
| 2503 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 2504 | ** The cell content is not freed or deallocated. It is assumed that |
| 2505 | ** the cell content has been copied someplace else. This routine just |
| 2506 | ** removes the reference to the cell from pPage. |
| 2507 | ** |
| 2508 | ** "sz" must be the number of bytes in the cell. |
| 2509 | ** |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2510 | ** Try to maintain the integrity of the linked list of cells. But if |
| 2511 | ** the cell being inserted does not fit on the page, this will not be |
| 2512 | ** possible. If the linked list is not maintained, then just update |
| 2513 | ** pPage->aCell[] and set the pPage->needRelink flag so that we will |
| 2514 | ** know to rebuild the linked list later. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2515 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2516 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2517 | int j, pc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2518 | u8 *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2519 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2520 | assert( sz==cellSize(pPage, pPage->aCell[idx]) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2521 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2522 | assert( pPage->aCell[idx]>=pPage->aData ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2523 | assert( pPage->aCell[idx]<=&pPage->aData[pPage->pBt->usableSize-sz] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2524 | data = pPage->aData; |
| 2525 | pc = Addr(pPage->aCell[idx]) - Addr(data); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2526 | assert( pc>pPage->hdrOffset && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2527 | freeSpace(pPage, pc, sz); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2528 | for(j=idx; j<pPage->nCell-1; j++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2529 | pPage->aCell[j] = pPage->aCell[j+1]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2530 | } |
| 2531 | pPage->nCell--; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2532 | if( !pPage->isOverfull && !pPage->needRelink ){ |
| 2533 | u8 *pPrev; |
| 2534 | if( idx==0 ){ |
| 2535 | pPrev = &data[pPage->hdrOffset+3]; |
| 2536 | }else{ |
| 2537 | pPrev = pPage->aCell[idx-1]; |
| 2538 | } |
| 2539 | if( idx<pPage->nCell ){ |
| 2540 | pc = Addr(pPage->aCell[idx]) - Addr(data); |
| 2541 | }else{ |
| 2542 | pc = 0; |
| 2543 | } |
| 2544 | put2byte(pPrev, pc); |
| 2545 | pageIntegrity(pPage); |
| 2546 | }else{ |
| 2547 | pPage->needRelink = 1; |
| 2548 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2549 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | /* |
| 2553 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 2554 | ** content of the cell. |
| 2555 | ** |
| 2556 | ** If the cell content will fit on the page, then put it there. If it |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2557 | ** will not fit and pTemp is not NULL, then make a copy of the content |
| 2558 | ** into pTemp, set pPage->aCell[i] point to pTemp, and set pPage->isOverfull. |
| 2559 | ** If the content will not fit and pTemp is NULL, then make pPage->aCell[i] |
| 2560 | ** point to pCell and set pPage->isOverfull. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2561 | ** |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2562 | ** Try to maintain the integrity of the linked list of cells. But if |
| 2563 | ** the cell being inserted does not fit on the page, this will not be |
| 2564 | ** possible. If the linked list is not maintained, then just update |
| 2565 | ** pPage->aCell[] and set the pPage->needRelink flag so that we will |
| 2566 | ** know to rebuild the linked list later. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2567 | */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2568 | static void insertCell( |
| 2569 | MemPage *pPage, /* Page into which we are copying */ |
| 2570 | int i, /* Which cell on pPage to insert after */ |
| 2571 | u8 *pCell, /* Text of the new cell to insert */ |
| 2572 | int sz, /* Bytes of data in pCell */ |
| 2573 | u8 *pTemp /* Temp storage space for pCell, if needed */ |
| 2574 | ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2575 | int idx, j; |
| 2576 | assert( i>=0 && i<=pPage->nCell ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2577 | assert( sz==cellSize(pPage, pCell) ); |
| 2578 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2579 | idx = pPage->needRelink ? 0 : allocateSpace(pPage, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2580 | resizeCellArray(pPage, pPage->nCell+1); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2581 | for(j=pPage->nCell; j>i; j--){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2582 | pPage->aCell[j] = pPage->aCell[j-1]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2583 | } |
| 2584 | pPage->nCell++; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2585 | if( idx<=0 ){ |
| 2586 | pPage->isOverfull = 1; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2587 | if( pTemp ){ |
| 2588 | memcpy(pTemp, pCell, sz); |
| 2589 | }else{ |
| 2590 | pTemp = pCell; |
| 2591 | } |
| 2592 | pPage->aCell[i] = pTemp; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2593 | }else{ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2594 | u8 *data = pPage->aData; |
| 2595 | memcpy(&data[idx], pCell, sz); |
| 2596 | pPage->aCell[i] = &data[idx]; |
| 2597 | } |
| 2598 | if( !pPage->isOverfull && !pPage->needRelink ){ |
| 2599 | u8 *pPrev; |
| 2600 | int pc; |
| 2601 | if( i==0 ){ |
| 2602 | pPrev = &pPage->aData[pPage->hdrOffset+3]; |
| 2603 | }else{ |
| 2604 | pPrev = pPage->aCell[i-1]; |
| 2605 | } |
| 2606 | pc = get2byte(pPrev); |
| 2607 | put2byte(pPrev, idx); |
| 2608 | put2byte(pPage->aCell[i], pc); |
| 2609 | pageIntegrity(pPage); |
| 2610 | }else{ |
| 2611 | pPage->needRelink = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2612 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2613 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | /* |
| 2617 | ** Rebuild the linked list of cells on a page so that the cells |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2618 | ** occur in the order specified by the pPage->aCell[] array. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2619 | ** Invoke this routine once to repair damage after one or more |
| 2620 | ** invocations of either insertCell() or dropCell(). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2621 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2622 | static void relinkCellList(MemPage *pPage){ |
| 2623 | int i, idxFrom; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2624 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2625 | if( !pPage->needRelink ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2626 | idxFrom = pPage->hdrOffset+3; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2627 | for(i=0; i<pPage->nCell; i++){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2628 | int idx = Addr(pPage->aCell[i]) - Addr(pPage->aData); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2629 | assert( idx>pPage->hdrOffset && idx<pPage->pBt->usableSize ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2630 | put2byte(&pPage->aData[idxFrom], idx); |
| 2631 | idxFrom = idx; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2632 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2633 | put2byte(&pPage->aData[idxFrom], 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2634 | pPage->needRelink = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2638 | ** GCC does not define the offsetof() macro so we'll have to do it |
| 2639 | ** ourselves. |
| 2640 | */ |
| 2641 | #ifndef offsetof |
| 2642 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 2643 | #endif |
| 2644 | |
| 2645 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2646 | ** Move the content of the page at pFrom over to pTo. The pFrom->aCell[] |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2647 | ** pointers that point into pFrom->aData[] must be adjusted to point |
| 2648 | ** into pTo->aData[] instead. But some pFrom->aCell[] entries might |
| 2649 | ** not point to pFrom->aData[]. Those are unchanged. |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2650 | ** |
| 2651 | ** Over this operation completes, the meta data for pFrom is zeroed. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2652 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2653 | static void movePage(MemPage *pTo, MemPage *pFrom){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2654 | uptr from, to; |
| 2655 | int i; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2656 | int usableSize; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2657 | int ofst; |
| 2658 | |
| 2659 | assert( pTo->hdrOffset==0 ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2660 | assert( pFrom->isInit ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2661 | ofst = pFrom->hdrOffset; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2662 | usableSize = pFrom->pBt->usableSize; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2663 | sqliteFree(pTo->aCell); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2664 | memcpy(pTo->aData, &pFrom->aData[ofst], usableSize - ofst); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2665 | memcpy(pTo, pFrom, offsetof(MemPage, aData)); |
| 2666 | pFrom->isInit = 0; |
| 2667 | pFrom->aCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2668 | assert( pTo->aData[5]<155 ); |
| 2669 | pTo->aData[5] += ofst; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2670 | pTo->isOverfull = pFrom->isOverfull; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2671 | to = Addr(pTo->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2672 | from = Addr(&pFrom->aData[ofst]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2673 | for(i=0; i<pTo->nCell; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2674 | uptr x = Addr(pTo->aCell[i]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2675 | if( x>from && x<from+usableSize-ofst ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2676 | *((uptr*)&pTo->aCell[i]) = x + to - from; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2677 | } |
| 2678 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2682 | ** The following parameters determine how many adjacent pages get involved |
| 2683 | ** in a balancing operation. NN is the number of neighbors on either side |
| 2684 | ** of the page that participate in the balancing operation. NB is the |
| 2685 | ** total number of pages that participate, including the target page and |
| 2686 | ** NN neighbors on either side. |
| 2687 | ** |
| 2688 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 2689 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 2690 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 2691 | ** The value of NN appears to give the best results overall. |
| 2692 | */ |
| 2693 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 2694 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 2695 | |
| 2696 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2697 | ** This routine redistributes Cells on pPage and up to two siblings |
| 2698 | ** of pPage so that all pages have about the same amount of free space. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2699 | ** Usually one sibling on either side of pPage is used in the balancing, |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2700 | ** though both siblings might come from one side if pPage is the first |
| 2701 | ** or last child of its parent. If pPage has fewer than two siblings |
| 2702 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2703 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2704 | ** |
| 2705 | ** The number of siblings of pPage might be increased or decreased by |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2706 | ** one in an effort to keep pages between 66% and 100% full. The root page |
| 2707 | ** is special and is allowed to be less than 66% full. If pPage is |
| 2708 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2709 | ** or decreased by one, as necessary, to keep the root page from being |
| 2710 | ** overfull or empty. |
| 2711 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2712 | ** This routine alwyas calls relinkCellList() on its input page regardless of |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2713 | ** whether or not it does any real balancing. Client routines will typically |
| 2714 | ** invoke insertCell() or dropCell() before calling this routine, so we |
| 2715 | ** need to call relinkCellList() to clean up the mess that those other |
| 2716 | ** routines left behind. |
| 2717 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2718 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2719 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2720 | ** 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] | 2721 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2722 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2723 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 2724 | ** might become overfull or underfull. If that happens, then this routine |
| 2725 | ** is called recursively on the parent. |
| 2726 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2727 | ** If this routine fails for any reason, it might leave the database |
| 2728 | ** in a corrupted state. So if this routine fails, the database should |
| 2729 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2730 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2731 | static int balance(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2732 | MemPage *pParent; /* The parent of pPage */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2733 | Btree *pBt; /* The whole database */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2734 | int nCell; /* Number of cells in aCell[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2735 | int nOld; /* Number of pages in apOld[] */ |
| 2736 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2737 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2738 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2739 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 2740 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2741 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2742 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2743 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2744 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 2745 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2746 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2747 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2748 | MemPage *extraUnref = 0; /* Unref this page if not zero */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2749 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 2750 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2751 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2752 | MemPage *apNew[NB+1]; /* pPage and up to NB siblings after balancing */ |
| 2753 | Pgno pgnoNew[NB+1]; /* Page numbers for each page in apNew[] */ |
| 2754 | int idxDiv[NB]; /* Indices of divider cells in pParent */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2755 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2756 | int cntNew[NB+1]; /* Index in aCell[] of cell after i-th page */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2757 | int szNew[NB+1]; /* Combined size of cells place on i-th page */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2758 | u8 *apCell[(MX_CELL+2)*NB]; /* All cells from pages being balanced */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2759 | int szCell[(MX_CELL+2)*NB]; /* Local size of all cells */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2760 | u8 aCopy[NB][MX_PAGE_SIZE+sizeof(MemPage)]; /* Space for apCopy[] */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2761 | u8 aSpace[MX_PAGE_SIZE*4]; /* Space to copies of divider cells */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2762 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2763 | /* |
| 2764 | ** Return without doing any work if pPage is neither overfull nor |
| 2765 | ** underfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2766 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2767 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2768 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2769 | pBt = pPage->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2770 | if( !pPage->isOverfull && pPage->nFree<pBt->usableSize*2/3 && pPage->nCell>=2){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2771 | relinkCellList(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2772 | return SQLITE_OK; |
| 2773 | } |
| 2774 | |
| 2775 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2776 | ** Find the parent of the page to be balanced. If there is no parent, |
| 2777 | ** it means this page is the root page and special rules apply. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2778 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2779 | pParent = pPage->pParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2780 | if( pParent==0 ){ |
| 2781 | Pgno pgnoChild; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2782 | MemPage *pChild; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2783 | assert( pPage->isInit ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2784 | if( pPage->nCell==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2785 | if( pPage->leaf ){ |
| 2786 | /* The table is completely empty */ |
| 2787 | relinkCellList(pPage); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2788 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2789 | }else{ |
| 2790 | /* The root page is empty but has one child. Transfer the |
| 2791 | ** information from that one child into the root page if it |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2792 | ** will fit. This reduces the depth of the tree by one. |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2793 | ** |
| 2794 | ** If the root page is page 1, it has less space available than |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2795 | ** its child (due to the 100 byte header that occurs at the beginning |
| 2796 | ** of the database fle), so it might not be able to hold all of the |
| 2797 | ** information currently contained in the child. If this is the |
| 2798 | ** case, then do not do the transfer. Leave page 1 empty except |
| 2799 | ** for the right-pointer to the child page. The child page becomes |
| 2800 | ** the virtual root of the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2801 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2802 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 2803 | assert( pgnoChild>0 && pgnoChild<=sqlite3pager_pagecount(pBt->pPager) ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2804 | rc = getPage(pBt, pgnoChild, &pChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2805 | if( rc ) return rc; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2806 | if( pPage->pgno==1 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2807 | rc = initPage(pChild, pPage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2808 | if( rc ) return rc; |
| 2809 | if( pChild->nFree>=100 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2810 | /* The child information will fit on the root page, so do the |
| 2811 | ** copy */ |
| 2812 | zeroPage(pPage, pChild->aData[0]); |
| 2813 | resizeCellArray(pPage, pChild->nCell); |
| 2814 | for(i=0; i<pChild->nCell; i++){ |
| 2815 | insertCell(pPage, i, pChild->aCell[i], |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2816 | cellSize(pChild, pChild->aCell[i]), 0); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2817 | } |
| 2818 | freePage(pChild); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2819 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2820 | }else{ |
| 2821 | /* The child has more information that will fit on the root. |
| 2822 | ** The tree is already balanced. Do nothing. */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2823 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2824 | } |
| 2825 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2826 | memcpy(pPage->aData, pChild->aData, pBt->usableSize); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2827 | pPage->isInit = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2828 | pPage->pParent = 0; |
| 2829 | rc = initPage(pPage, 0); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2830 | assert( rc==SQLITE_OK ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2831 | freePage(pChild); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2832 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 2833 | pChild->pgno, pPage->pgno)); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2834 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2835 | reparentChildPages(pPage); |
| 2836 | releasePage(pChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2837 | } |
| 2838 | return SQLITE_OK; |
| 2839 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2840 | if( !pPage->isOverfull ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2841 | /* It is OK for the root page to be less than half full. |
| 2842 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2843 | relinkCellList(pPage); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2844 | TRACE(("BALANCE: root page %d is low - no changes\n", pPage->pgno)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2845 | return SQLITE_OK; |
| 2846 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2847 | /* |
| 2848 | ** If we get to here, it means the root page is overfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2849 | ** When this happens, Create a new child page and copy the |
| 2850 | ** contents of the root into the child. Then make the root |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2851 | ** page an empty page with rightChild pointing to the new |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2852 | ** child. Then fall thru to the code below which will cause |
| 2853 | ** the overfull child page to be split. |
| 2854 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2855 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2856 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2857 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2858 | movePage(pChild, pPage); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2859 | assert( pChild->aData[0]==pPage->aData[pPage->hdrOffset] ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2860 | pChild->pParent = pPage; |
drh | 457f501 | 2004-05-09 01:35:05 +0000 | [diff] [blame] | 2861 | sqlite3pager_ref(pPage->aData); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2862 | pChild->idxParent = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2863 | pChild->isOverfull = 1; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2864 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2865 | put4byte(&pPage->aData[pPage->hdrOffset+6], pChild->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2866 | pParent = pPage; |
| 2867 | pPage = pChild; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2868 | extraUnref = pChild; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2869 | TRACE(("BALANCE: copy root %d into %d and balance %d\n", |
| 2870 | pParent->pgno, pPage->pgno, pPage->pgno)); |
| 2871 | }else{ |
| 2872 | TRACE(("BALANCE: begin page %d child of %d\n", |
| 2873 | pPage->pgno, pParent->pgno)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2874 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2875 | rc = sqlite3pager_write(pParent->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2876 | if( rc ) return rc; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2877 | assert( pParent->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2878 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2879 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2880 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2881 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 2882 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2883 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2884 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2885 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2886 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2887 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2888 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2889 | if( get4byte(&pParent->aCell[idx][2])==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2890 | break; |
| 2891 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2892 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2893 | assert( idx<pParent->nCell |
| 2894 | || get4byte(&pParent->aData[pParent->hdrOffset+6])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2895 | }else{ |
| 2896 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2897 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2898 | |
| 2899 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2900 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2901 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2902 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2903 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2904 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2905 | |
| 2906 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2907 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2908 | ** the siblings. An attempt is made to find NN siblings on either |
| 2909 | ** side of pPage. More siblings are taken from one side, however, if |
| 2910 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 2911 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2912 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2913 | nxDiv = idx - NN; |
| 2914 | if( nxDiv + NB > pParent->nCell ){ |
| 2915 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2916 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2917 | if( nxDiv<0 ){ |
| 2918 | nxDiv = 0; |
| 2919 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2920 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2921 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2922 | if( k<pParent->nCell ){ |
| 2923 | idxDiv[i] = k; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2924 | apDiv[i] = pParent->aCell[k]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2925 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2926 | assert( !pParent->leaf ); |
| 2927 | pgnoOld[i] = get4byte(&apDiv[i][2]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2928 | }else if( k==pParent->nCell ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2929 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+6]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2930 | }else{ |
| 2931 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2932 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2933 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2934 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2935 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2936 | apCopy[i] = 0; |
| 2937 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2938 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2942 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 2943 | ** The rest of this function will use data from the copies rather |
| 2944 | ** that the original pages since the original pages will be in the |
| 2945 | ** process of being overwritten. |
| 2946 | */ |
| 2947 | for(i=0; i<nOld; i++){ |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2948 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i+1][-sizeof(MemPage)]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2949 | p->aData = &((u8*)p)[-pBt->usableSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2950 | p->aCell = 0; |
| 2951 | p->hdrOffset = 0; |
| 2952 | movePage(p, apOld[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | /* |
| 2956 | ** Load pointers to all cells on sibling pages and the divider cells |
| 2957 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2958 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 2959 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2960 | ** |
| 2961 | ** If the siblings are on leaf pages, then the child pointers of the |
| 2962 | ** divider cells are stripped from the cells before they are copied |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2963 | ** into aSpace[]. In this wall, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2964 | ** child pointers. If siblings are not leaves, then all cell in |
| 2965 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 2966 | ** are alike. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2967 | */ |
| 2968 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2969 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2970 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2971 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2972 | MemPage *pOld = apCopy[i]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2973 | for(j=0; j<pOld->nCell; j++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2974 | apCell[nCell] = pOld->aCell[j]; |
| 2975 | szCell[nCell] = cellSize(pOld, apCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2976 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2977 | } |
| 2978 | if( i<nOld-1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2979 | int sz = cellSize(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2980 | if( leafData ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2981 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2982 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2983 | u8 *pTemp; |
| 2984 | szCell[nCell] = sz; |
| 2985 | pTemp = &aSpace[iSpace]; |
| 2986 | iSpace += sz; |
| 2987 | assert( iSpace<=sizeof(aSpace) ); |
| 2988 | memcpy(pTemp, apDiv[i], sz); |
| 2989 | apCell[nCell] = pTemp+leafCorrection; |
| 2990 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2991 | szCell[nCell] -= leafCorrection; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2992 | assert( get4byte(pTemp+2)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2993 | if( !pOld->leaf ){ |
| 2994 | assert( leafCorrection==0 ); |
| 2995 | /* The right pointer of the child page pOld becomes the left |
| 2996 | ** pointer of the divider cell */ |
| 2997 | memcpy(&apCell[nCell][2], &pOld->aData[pOld->hdrOffset+6], 4); |
| 2998 | }else{ |
| 2999 | assert( leafCorrection==4 ); |
| 3000 | } |
| 3001 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3002 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3007 | ** Figure out the number of pages needed to hold all nCell cells. |
| 3008 | ** Store this number in "k". Also compute szNew[] which is the total |
| 3009 | ** 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] | 3010 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3011 | ** cntNew[k] should equal nCell. |
| 3012 | ** |
| 3013 | ** This little patch of code is critical for keeping the tree |
| 3014 | ** balanced. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3015 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3016 | usableSpace = pBt->usableSize - 10 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3017 | for(subtotal=k=i=0; i<nCell; i++){ |
| 3018 | subtotal += szCell[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3019 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3020 | szNew[k] = subtotal - szCell[i]; |
| 3021 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3022 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3023 | subtotal = 0; |
| 3024 | k++; |
| 3025 | } |
| 3026 | } |
| 3027 | szNew[k] = subtotal; |
| 3028 | cntNew[k] = nCell; |
| 3029 | k++; |
| 3030 | for(i=k-1; i>0; i--){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3031 | while( szNew[i]<usableSpace/2 ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3032 | cntNew[i-1]--; |
| 3033 | assert( cntNew[i-1]>0 ); |
| 3034 | szNew[i] += szCell[cntNew[i-1]]; |
| 3035 | szNew[i-1] -= szCell[cntNew[i-1]-1]; |
| 3036 | } |
| 3037 | } |
| 3038 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3039 | |
| 3040 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3041 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3042 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3043 | assert( pPage->pgno>1 ); |
| 3044 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3045 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3046 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3047 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3048 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3049 | pgnoNew[i] = pgnoOld[i]; |
| 3050 | apOld[i] = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3051 | sqlite3pager_write(pNew->aData); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3052 | }else{ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3053 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3054 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3055 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3056 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3057 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3058 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3061 | /* Free any old pages that were not reused as new pages. |
| 3062 | */ |
| 3063 | while( i<nOld ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3064 | rc = freePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3065 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3066 | releasePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3067 | apOld[i] = 0; |
| 3068 | i++; |
| 3069 | } |
| 3070 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3071 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3072 | ** Put the new pages in accending order. This helps to |
| 3073 | ** keep entries in the disk file in order so that a scan |
| 3074 | ** of the table is a linear scan through the file. That |
| 3075 | ** in turn helps the operating system to deliver pages |
| 3076 | ** from the disk more rapidly. |
| 3077 | ** |
| 3078 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3079 | ** n is never more than NB (a small constant), that should |
| 3080 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3081 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3082 | ** When NB==3, this one optimization makes the database |
| 3083 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3084 | */ |
| 3085 | for(i=0; i<k-1; i++){ |
| 3086 | int minV = pgnoNew[i]; |
| 3087 | int minI = i; |
| 3088 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 3089 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3090 | minI = j; |
| 3091 | minV = pgnoNew[j]; |
| 3092 | } |
| 3093 | } |
| 3094 | if( minI>i ){ |
| 3095 | int t; |
| 3096 | MemPage *pT; |
| 3097 | t = pgnoNew[i]; |
| 3098 | pT = apNew[i]; |
| 3099 | pgnoNew[i] = pgnoNew[minI]; |
| 3100 | apNew[i] = apNew[minI]; |
| 3101 | pgnoNew[minI] = t; |
| 3102 | apNew[minI] = pT; |
| 3103 | } |
| 3104 | } |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3105 | TRACE(("BALANCE: old: %d %d %d new: %d %d %d %d\n", |
| 3106 | pgnoOld[0], |
| 3107 | nOld>=2 ? pgnoOld[1] : 0, |
| 3108 | nOld>=3 ? pgnoOld[2] : 0, |
| 3109 | pgnoNew[0], |
| 3110 | nNew>=2 ? pgnoNew[1] : 0, |
| 3111 | nNew>=3 ? pgnoNew[2] : 0, |
| 3112 | nNew>=4 ? pgnoNew[3] : 0)); |
| 3113 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3114 | |
| 3115 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3116 | ** Evenly distribute the data in apCell[] across the new pages. |
| 3117 | ** Insert divider cells into pParent as necessary. |
| 3118 | */ |
| 3119 | j = 0; |
| 3120 | for(i=0; i<nNew; i++){ |
| 3121 | MemPage *pNew = apNew[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3122 | assert( pNew->pgno==pgnoNew[i] ); |
| 3123 | resizeCellArray(pNew, cntNew[i] - j); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3124 | while( j<cntNew[i] ){ |
| 3125 | assert( pNew->nFree>=szCell[j] ); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3126 | insertCell(pNew, pNew->nCell, apCell[j], szCell[j], 0); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3127 | j++; |
| 3128 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3129 | assert( pNew->nCell>0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3130 | assert( !pNew->isOverfull ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3131 | relinkCellList(pNew); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3132 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3133 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3134 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3135 | int sz; |
| 3136 | pCell = apCell[j]; |
| 3137 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3138 | if( !pNew->leaf ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3139 | memcpy(&pNew->aData[6], pCell+2, 4); |
| 3140 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3141 | }else if( leafData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3142 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3143 | j--; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3144 | parseCell(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3145 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3146 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3147 | iSpace += sz; |
| 3148 | assert( iSpace<=sizeof(aSpace) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3149 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3150 | }else{ |
| 3151 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3152 | pTemp = &aSpace[iSpace]; |
| 3153 | iSpace += sz; |
| 3154 | assert( iSpace<=sizeof(aSpace) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3155 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3156 | insertCell(pParent, nxDiv, pCell, sz, pTemp); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3157 | put4byte(&pParent->aCell[nxDiv][2], pNew->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3158 | j++; |
| 3159 | nxDiv++; |
| 3160 | } |
| 3161 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3162 | assert( j==nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3163 | if( (pageFlags & PTF_LEAF)==0 ){ |
| 3164 | memcpy(&apNew[nNew-1]->aData[6], &apCopy[nOld-1]->aData[6], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3165 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3166 | if( nxDiv==pParent->nCell ){ |
| 3167 | /* Right-most sibling is the right-most child of pParent */ |
| 3168 | put4byte(&pParent->aData[pParent->hdrOffset+6], pgnoNew[nNew-1]); |
| 3169 | }else{ |
| 3170 | /* Right-most sibling is the left child of the first entry in pParent |
| 3171 | ** past the right-most divider entry */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3172 | put4byte(&pParent->aCell[nxDiv][2], pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | /* |
| 3176 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3177 | */ |
| 3178 | for(i=0; i<nNew; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3179 | reparentChildPages(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3180 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3181 | reparentChildPages(pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3184 | ** Balance the parent page. Note that the current page (pPage) might |
| 3185 | ** have been added to the freelist is it might no longer be initialized. |
| 3186 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3187 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3188 | assert( pParent->isInit ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3189 | /* assert( pPage->isInit ); // No! pPage might have been added to freelist */ |
| 3190 | /* pageIntegrity(pPage); // No! pPage might have been added to freelist */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3191 | rc = balance(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3192 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3193 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3194 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3195 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3196 | balance_cleanup: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3197 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3198 | releasePage(apOld[i]); |
| 3199 | if( apCopy[i] ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3200 | sqliteFree(apCopy[i]->aCell); |
| 3201 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3202 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3203 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3204 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3205 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3206 | releasePage(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3207 | releasePage(extraUnref); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3208 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 3209 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3210 | return rc; |
| 3211 | } |
| 3212 | |
| 3213 | /* |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3214 | ** This routine checks all cursors that point to the same table |
| 3215 | ** as pCur points to. If any of those cursors were opened with |
| 3216 | ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all |
| 3217 | ** cursors point to the same table were opened with wrFlag==1 |
| 3218 | ** then this routine returns SQLITE_OK. |
| 3219 | ** |
| 3220 | ** In addition to checking for read-locks (where a read-lock |
| 3221 | ** means a cursor opened with wrFlag==0) this routine also moves |
| 3222 | ** all cursors other than pCur so that they are pointing to the |
| 3223 | ** first Cell on root page. This is necessary because an insert |
| 3224 | ** or delete might change the number of cells on a page or delete |
| 3225 | ** a page entirely and we do not want to leave any cursors |
| 3226 | ** pointing to non-existant pages or cells. |
| 3227 | */ |
| 3228 | static int checkReadLocks(BtCursor *pCur){ |
| 3229 | BtCursor *p; |
| 3230 | assert( pCur->wrFlag ); |
| 3231 | for(p=pCur->pShared; p!=pCur; p=p->pShared){ |
| 3232 | assert( p ); |
| 3233 | assert( p->pgnoRoot==pCur->pgnoRoot ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3234 | assert( p->pPage->pgno==sqlite3pager_pagenumber(p->pPage->aData) ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3235 | if( p->wrFlag==0 ) return SQLITE_LOCKED; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3236 | if( p->pPage->pgno!=p->pgnoRoot ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3237 | moveToRoot(p); |
| 3238 | } |
| 3239 | } |
| 3240 | return SQLITE_OK; |
| 3241 | } |
| 3242 | |
| 3243 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3244 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 3245 | ** 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] | 3246 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3247 | ** is left pointing at a random location. |
| 3248 | ** |
| 3249 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 3250 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3251 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3252 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3253 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3254 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3255 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3256 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3257 | int rc; |
| 3258 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3259 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3260 | MemPage *pPage; |
| 3261 | Btree *pBt = pCur->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3262 | unsigned char *oldCell; |
| 3263 | unsigned char newCell[MX_CELL_SIZE]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3264 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3265 | if( pCur->status ){ |
| 3266 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3267 | } |
danielk1977 | e7c8d58 | 2004-05-13 13:38:52 +0000 | [diff] [blame] | 3268 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3269 | /* Must start a transaction before doing an insert */ |
| 3270 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3271 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3272 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3273 | if( !pCur->wrFlag ){ |
| 3274 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 3275 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3276 | if( checkReadLocks(pCur) ){ |
| 3277 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3278 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3279 | rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3280 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3281 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3282 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3283 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3284 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 3285 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 3286 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3287 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3288 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3289 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3290 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3291 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3292 | assert( szNew==cellSize(pPage, newCell) ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3293 | assert( szNew<=sizeof(newCell) ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3294 | if( loc==0 && pCur->isValid ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3295 | int szOld; |
| 3296 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3297 | oldCell = pPage->aCell[pCur->idx]; |
| 3298 | if( !pPage->leaf ){ |
| 3299 | memcpy(&newCell[2], &oldCell[2], 4); |
| 3300 | } |
| 3301 | szOld = cellSize(pPage, oldCell); |
| 3302 | rc = clearCell(pPage, oldCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3303 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3304 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3305 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3306 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3307 | pCur->idx++; |
| 3308 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3309 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3310 | } |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3311 | insertCell(pPage, pCur->idx, newCell, szNew, 0); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3312 | rc = balance(pPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3313 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3314 | /* fflush(stdout); */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3315 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3316 | return rc; |
| 3317 | } |
| 3318 | |
| 3319 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3320 | ** Delete the entry that the cursor is pointing to. The cursor |
| 3321 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3322 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3323 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3324 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3325 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3326 | int rc; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3327 | Pgno pgnoChild; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3328 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3329 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3330 | assert( pPage->isInit ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3331 | if( pCur->status ){ |
| 3332 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3333 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3334 | if( !pBt->inTrans ){ |
| 3335 | /* Must start a transaction before doing a delete */ |
| 3336 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3337 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3338 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3339 | if( pCur->idx >= pPage->nCell ){ |
| 3340 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 3341 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3342 | if( !pCur->wrFlag ){ |
| 3343 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 3344 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3345 | if( checkReadLocks(pCur) ){ |
| 3346 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3347 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3348 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3349 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3350 | pCell = pPage->aCell[pCur->idx]; |
| 3351 | if( !pPage->leaf ){ |
| 3352 | pgnoChild = get4byte(&pCell[2]); |
| 3353 | } |
| 3354 | clearCell(pPage, pCell); |
| 3355 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3356 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3357 | ** 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] | 3358 | ** do something we will leave a hole on an internal page. |
| 3359 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 3360 | ** next Cell after the one to be deleted is guaranteed to exist and |
| 3361 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3362 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3363 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3364 | unsigned char *pNext; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3365 | int szNext; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3366 | int notUsed; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3367 | unsigned char tempCell[MX_CELL_SIZE]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3368 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3369 | getTempCursor(pCur, &leafCur); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3370 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3371 | if( rc!=SQLITE_OK ){ |
drh | 8a6ac0a | 2004-02-14 17:35:07 +0000 | [diff] [blame] | 3372 | if( rc!=SQLITE_NOMEM ) rc = SQLITE_CORRUPT; |
| 3373 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3374 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3375 | rc = sqlite3pager_write(leafCur.pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3376 | if( rc ) return rc; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3377 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 3378 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3379 | dropCell(pPage, pCur->idx, cellSize(pPage, pCell)); |
| 3380 | pNext = leafCur.pPage->aCell[leafCur.idx]; |
| 3381 | szNext = cellSize(leafCur.pPage, pNext); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3382 | assert( sizeof(tempCell)>=szNext+4 ); |
| 3383 | insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell); |
| 3384 | put4byte(pPage->aCell[pCur->idx]+2, pgnoChild); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3385 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3386 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3387 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 3388 | rc = balance(leafCur.pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3389 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3390 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3391 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 3392 | pCur->pgnoRoot, pPage->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3393 | dropCell(pPage, pCur->idx, cellSize(pPage, pCell)); |
| 3394 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3395 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3396 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3397 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3398 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3399 | |
| 3400 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3401 | ** Create a new BTree table. Write into *piTable the page |
| 3402 | ** number for the root page of the new table. |
| 3403 | ** |
| 3404 | ** In the current implementation, BTree tables and BTree indices are the |
drh | 144f9ea | 2003-04-16 01:28:16 +0000 | [diff] [blame] | 3405 | ** the same. In the future, we may change this so that BTree tables |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3406 | ** are restricted to having a 4-byte integer key and arbitrary data and |
| 3407 | ** BTree indices are restricted to having an arbitrary key and no data. |
drh | 144f9ea | 2003-04-16 01:28:16 +0000 | [diff] [blame] | 3408 | ** But for now, this routine also serves to create indices. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3409 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3410 | int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3411 | MemPage *pRoot; |
| 3412 | Pgno pgnoRoot; |
| 3413 | int rc; |
| 3414 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3415 | /* Must start a transaction first */ |
| 3416 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3417 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3418 | if( pBt->readOnly ){ |
| 3419 | return SQLITE_READONLY; |
| 3420 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3421 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3422 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3423 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3424 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3425 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3426 | *piTable = (int)pgnoRoot; |
| 3427 | return SQLITE_OK; |
| 3428 | } |
| 3429 | |
| 3430 | /* |
| 3431 | ** Erase the given database page and all its children. Return |
| 3432 | ** the page to the freelist. |
| 3433 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3434 | static int clearDatabasePage( |
| 3435 | Btree *pBt, /* The BTree that contains the table */ |
| 3436 | Pgno pgno, /* Page number to clear */ |
| 3437 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 3438 | int freePageFlag /* Deallocate page if true */ |
| 3439 | ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3440 | MemPage *pPage; |
| 3441 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3442 | unsigned char *pCell; |
| 3443 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3444 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3445 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3446 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3447 | rc = sqlite3pager_write(pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3448 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3449 | for(i=0; i<pPage->nCell; i++){ |
| 3450 | pCell = pPage->aCell[i]; |
| 3451 | if( !pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3452 | rc = clearDatabasePage(pBt, get4byte(&pCell[2]), pPage->pParent, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3453 | if( rc ) return rc; |
| 3454 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3455 | rc = clearCell(pPage, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3456 | if( rc ) return rc; |
| 3457 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3458 | if( !pPage->leaf ){ |
| 3459 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[6]), pPage->pParent, 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3460 | if( rc ) return rc; |
| 3461 | } |
| 3462 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3463 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3464 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3465 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3466 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3467 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3468 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | ** Delete all information from a single table in the database. |
| 3473 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3474 | int sqlite3BtreeClearTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3475 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3476 | BtCursor *pCur; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3477 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3478 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3479 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3480 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3481 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3482 | if( pCur->wrFlag==0 ) return SQLITE_LOCKED; |
| 3483 | moveToRoot(pCur); |
| 3484 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3485 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3486 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3487 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3488 | sqlite3BtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3489 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3490 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* |
| 3494 | ** Erase all information in a table and add the root of the table to |
| 3495 | ** the freelist. Except, the root of the principle table (the one on |
| 3496 | ** page 2) is never added to the freelist. |
| 3497 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3498 | int sqlite3BtreeDropTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3499 | int rc; |
| 3500 | MemPage *pPage; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3501 | BtCursor *pCur; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3502 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3503 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3504 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3505 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3506 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3507 | return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */ |
| 3508 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3509 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3510 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3511 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3512 | rc = sqlite3BtreeClearTable(pBt, iTable); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3513 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3514 | if( iTable>1 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3515 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3516 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3517 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3518 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3519 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3520 | return rc; |
| 3521 | } |
| 3522 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3523 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3524 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3525 | ** Read the meta-information out of a database file. Meta[0] |
| 3526 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3527 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 3528 | ** is read-only, the others are read/write. |
| 3529 | ** |
| 3530 | ** The schema layer numbers meta values differently. At the schema |
| 3531 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 3532 | ** 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] | 3533 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3534 | int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3535 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3536 | unsigned char *pP1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3537 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3538 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3539 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3540 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3541 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3542 | sqlite3pager_unref(pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3543 | return SQLITE_OK; |
| 3544 | } |
| 3545 | |
| 3546 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3547 | ** Write meta-information back into the database. Meta[0] is |
| 3548 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3549 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3550 | int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3551 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3552 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3553 | assert( idx>=1 && idx<=15 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3554 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3555 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3556 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3557 | assert( pBt->pPage1!=0 ); |
| 3558 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3559 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3560 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3561 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3562 | return SQLITE_OK; |
| 3563 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3564 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3565 | /* |
| 3566 | ** Return the flag byte at the beginning of the page that the cursor |
| 3567 | ** is currently pointing to. |
| 3568 | */ |
| 3569 | int sqlite3BtreeFlags(BtCursor *pCur){ |
| 3570 | MemPage *pPage = pCur->pPage; |
| 3571 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 3572 | } |
| 3573 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3574 | /****************************************************************************** |
| 3575 | ** The complete implementation of the BTree subsystem is above this line. |
| 3576 | ** All the code the follows is for testing and troubleshooting the BTree |
| 3577 | ** subsystem. None of the code that follows is used during normal operation. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3578 | ******************************************************************************/ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3579 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3580 | /* |
| 3581 | ** Print a disassembly of the given page on standard output. This routine |
| 3582 | ** is used for debugging and testing only. |
| 3583 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3584 | #ifdef SQLITE_TEST |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3585 | int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3586 | int rc; |
| 3587 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3588 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3589 | int nFree; |
| 3590 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3591 | int hdr; |
| 3592 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3593 | char range[20]; |
| 3594 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3595 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3596 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3597 | if( rc ){ |
| 3598 | return rc; |
| 3599 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3600 | hdr = pPage->hdrOffset; |
| 3601 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3602 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3603 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3604 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3605 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3606 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3607 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3608 | printf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
| 3609 | data[hdr], data[hdr+5], |
| 3610 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3611 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3612 | assert( hdr == (pgno==1 ? 100 : 0) ); |
| 3613 | idx = get2byte(&data[hdr+3]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3614 | while( idx>0 && idx<=pBt->usableSize ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3615 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3616 | Pgno child; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3617 | unsigned char *pCell = &data[idx]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3618 | int sz; |
| 3619 | |
| 3620 | pCell = &data[idx]; |
| 3621 | parseCell(pPage, pCell, &info); |
| 3622 | sz = info.nSize; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3623 | sprintf(range,"%d..%d", idx, idx+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3624 | if( pPage->leaf ){ |
| 3625 | child = 0; |
| 3626 | }else{ |
| 3627 | child = get4byte(&pCell[2]); |
| 3628 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3629 | sz = info.nData; |
| 3630 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3631 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3632 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3633 | for(j=0; j<sz; j++){ |
| 3634 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 3635 | } |
| 3636 | payload[sz] = 0; |
| 3637 | printf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3638 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 3639 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3640 | ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3641 | if( pPage->isInit && pPage->aCell[i]!=pCell ){ |
| 3642 | printf("**** aCell[%d] does not match on prior entry ****\n", i); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3643 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3644 | i++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3645 | idx = get2byte(pCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3646 | } |
| 3647 | if( idx!=0 ){ |
| 3648 | printf("ERROR: next cell index out of range: %d\n", idx); |
| 3649 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3650 | if( !pPage->leaf ){ |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 3651 | printf("right_child: %d\n", get4byte(&data[hdr+6])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3652 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3653 | nFree = 0; |
| 3654 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3655 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3656 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3657 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3658 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 3659 | nFree += sz; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3660 | printf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3661 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3662 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3663 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3664 | } |
| 3665 | if( idx!=0 ){ |
| 3666 | printf("ERROR: next freeblock index out of range: %d\n", idx); |
| 3667 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3668 | if( recursive && !pPage->leaf ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3669 | idx = get2byte(&data[hdr+3]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3670 | while( idx>0 && idx<pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3671 | unsigned char *pCell = &data[idx]; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3672 | sqlite3BtreePageDump(pBt, get4byte(&pCell[2]), 1); |
| 3673 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3674 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3675 | sqlite3BtreePageDump(pBt, get4byte(&data[hdr+6]), 1); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3676 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3677 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 3678 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3679 | return SQLITE_OK; |
| 3680 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3681 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3682 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3683 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3684 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3685 | ** Fill aResult[] with information about the entry and page that the |
| 3686 | ** cursor is pointing to. |
| 3687 | ** |
| 3688 | ** aResult[0] = The page number |
| 3689 | ** aResult[1] = The entry number |
| 3690 | ** aResult[2] = Total number of entries on this page |
| 3691 | ** aResult[3] = Size of this entry |
| 3692 | ** aResult[4] = Number of free bytes on this page |
| 3693 | ** aResult[5] = Number of free blocks on the page |
| 3694 | ** aResult[6] = Page number of the left child of this entry |
| 3695 | ** aResult[7] = Page number of the right child for the whole page |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3696 | ** |
| 3697 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3698 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3699 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3700 | int cnt, idx; |
| 3701 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3702 | |
| 3703 | pageIntegrity(pPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3704 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3705 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3706 | assert( aResult[0]==pPage->pgno ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3707 | aResult[1] = pCur->idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3708 | aResult[2] = pPage->nCell; |
| 3709 | if( pCur->idx>=0 && pCur->idx<pPage->nCell ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3710 | aResult[3] = cellSize(pPage, pPage->aCell[pCur->idx]); |
| 3711 | aResult[6] = pPage->leaf ? 0 : get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3712 | }else{ |
| 3713 | aResult[3] = 0; |
| 3714 | aResult[6] = 0; |
| 3715 | } |
| 3716 | aResult[4] = pPage->nFree; |
| 3717 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3718 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3719 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3720 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3721 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3722 | } |
| 3723 | aResult[5] = cnt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3724 | aResult[7] = pPage->leaf ? 0 : get4byte(&pPage->aData[pPage->hdrOffset+6]); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3725 | return SQLITE_OK; |
| 3726 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3727 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3728 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3729 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3730 | ** Return the pager associated with a BTree. This routine is used for |
| 3731 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3732 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3733 | Pager *sqlite3BtreePager(Btree *pBt){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3734 | return pBt->pPager; |
| 3735 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* |
| 3738 | ** This structure is passed around through all the sanity checking routines |
| 3739 | ** in order to keep track of some global state information. |
| 3740 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3741 | typedef struct IntegrityCk IntegrityCk; |
| 3742 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 3743 | Btree *pBt; /* The tree being checked out */ |
| 3744 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 3745 | int nPage; /* Number of pages in the database */ |
| 3746 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 3747 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3748 | }; |
| 3749 | |
| 3750 | /* |
| 3751 | ** Append a message to the error message string. |
| 3752 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3753 | static void checkAppendMsg(IntegrityCk *pCheck, char *zMsg1, char *zMsg2){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3754 | if( pCheck->zErrMsg ){ |
| 3755 | char *zOld = pCheck->zErrMsg; |
| 3756 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3757 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3758 | sqliteFree(zOld); |
| 3759 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3760 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | /* |
| 3765 | ** Add 1 to the reference count for page iPage. If this is the second |
| 3766 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 3767 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 3768 | ** if this is the first reference to the page. |
| 3769 | ** |
| 3770 | ** Also check that the page number is in bounds. |
| 3771 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3772 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3773 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 3774 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3775 | char zBuf[100]; |
| 3776 | sprintf(zBuf, "invalid page number %d", iPage); |
| 3777 | checkAppendMsg(pCheck, zContext, zBuf); |
| 3778 | return 1; |
| 3779 | } |
| 3780 | if( pCheck->anRef[iPage]==1 ){ |
| 3781 | char zBuf[100]; |
| 3782 | sprintf(zBuf, "2nd reference to page %d", iPage); |
| 3783 | checkAppendMsg(pCheck, zContext, zBuf); |
| 3784 | return 1; |
| 3785 | } |
| 3786 | return (pCheck->anRef[iPage]++)>1; |
| 3787 | } |
| 3788 | |
| 3789 | /* |
| 3790 | ** Check the integrity of the freelist or of an overflow page list. |
| 3791 | ** Verify that the number of pages on the list is N. |
| 3792 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3793 | static void checkList( |
| 3794 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 3795 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 3796 | int iPage, /* Page number for first page in the list */ |
| 3797 | int N, /* Expected number of pages in the list */ |
| 3798 | char *zContext /* Context for error messages */ |
| 3799 | ){ |
| 3800 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3801 | int expected = N; |
| 3802 | int iFirst = iPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3803 | char zMsg[100]; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3804 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3805 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3806 | if( iPage<1 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3807 | sprintf(zMsg, "%d of %d pages missing from overflow list starting at %d", |
| 3808 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3809 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3810 | break; |
| 3811 | } |
| 3812 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3813 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3814 | sprintf(zMsg, "failed to get page %d", iPage); |
| 3815 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3816 | break; |
| 3817 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3818 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3819 | int n = get4byte(&pOvfl[4]); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3820 | for(i=0; i<n; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3821 | checkRef(pCheck, get4byte(&pOvfl[8+i*4]), zContext); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3822 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3823 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3824 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3825 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3826 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | /* |
| 3831 | ** Do various sanity checks on a single page of a tree. Return |
| 3832 | ** the tree depth. Root pages return 0. Parents of root pages |
| 3833 | ** return 1, and so forth. |
| 3834 | ** |
| 3835 | ** These checks are done: |
| 3836 | ** |
| 3837 | ** 1. Make sure that cells and freeblocks do not overlap |
| 3838 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3839 | ** NO 2. Make sure cell keys are in order. |
| 3840 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 3841 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3842 | ** 5. Check the integrity of overflow pages. |
| 3843 | ** 6. Recursively call checkTreePage on all children. |
| 3844 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3845 | ** 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] | 3846 | ** the root of the tree. |
| 3847 | */ |
| 3848 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3849 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3850 | int iPage, /* Page number of the page to check */ |
| 3851 | MemPage *pParent, /* Parent page */ |
| 3852 | char *zParentContext, /* Parent context */ |
| 3853 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3854 | int nLower, /* Number of characters in zLowerBound */ |
| 3855 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 3856 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3857 | ){ |
| 3858 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3859 | int i, rc, depth, d2, pgno, cnt; |
| 3860 | int hdr; |
| 3861 | u8 *data; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3862 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3863 | Btree *pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3864 | int maxLocal, usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3865 | char zMsg[100]; |
| 3866 | char zContext[100]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3867 | char hit[MX_PAGE_SIZE]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3868 | |
| 3869 | /* Check that the page exists |
| 3870 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3871 | cur.pBt = pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3872 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3873 | if( iPage==0 ) return 0; |
| 3874 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3875 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3876 | sprintf(zMsg, "unable to get the page. error code=%d", rc); |
| 3877 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3878 | return 0; |
| 3879 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3880 | maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3881 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3882 | sprintf(zMsg, "initPage() returns error code %d", rc); |
| 3883 | checkAppendMsg(pCheck, zContext, zMsg); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3884 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3885 | return 0; |
| 3886 | } |
| 3887 | |
| 3888 | /* Check out all the cells. |
| 3889 | */ |
| 3890 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3891 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3892 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3893 | u8 *pCell; |
| 3894 | int sz; |
| 3895 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3896 | |
| 3897 | /* Check payload overflow pages |
| 3898 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3899 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3900 | pCell = pPage->aCell[i]; |
| 3901 | parseCell(pPage, pCell, &info); |
| 3902 | sz = info.nData; |
| 3903 | if( !pPage->intKey ) sz += info.nKey; |
| 3904 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3905 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3906 | checkList(pCheck, 0, get4byte(&pCell[info.iOverflow]),nPage,zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | /* Check sanity of left child page. |
| 3910 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3911 | if( !pPage->leaf ){ |
| 3912 | pgno = get4byte(&pCell[2]); |
| 3913 | d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0); |
| 3914 | if( i>0 && d2!=depth ){ |
| 3915 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 3916 | } |
| 3917 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3918 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3919 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3920 | if( !pPage->leaf ){ |
| 3921 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 3922 | sprintf(zContext, "On page %d at right child: ", iPage); |
| 3923 | checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0); |
| 3924 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3925 | |
| 3926 | /* Check for complete coverage of the page |
| 3927 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3928 | memset(hit, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3929 | memset(hit, 1, pPage->hdrOffset+10-4*(pPage->leaf)); |
| 3930 | data = pPage->aData; |
| 3931 | hdr = pPage->hdrOffset; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3932 | for(cnt=0, i=get2byte(&data[hdr+3]); i>0 && i<usableSize && cnt<10000; cnt++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3933 | int size = cellSize(pPage, &data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3934 | int j; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3935 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 3936 | i = get2byte(&data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3937 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3938 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; cnt++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3939 | int size = get2byte(&data[i+2]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3940 | int j; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3941 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 3942 | i = get2byte(&data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3943 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3944 | for(i=cnt=0; i<usableSize; i++){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3945 | if( hit[i]==0 ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3946 | cnt++; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3947 | }else if( hit[i]>1 ){ |
| 3948 | sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage); |
| 3949 | checkAppendMsg(pCheck, zMsg, 0); |
| 3950 | break; |
| 3951 | } |
| 3952 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3953 | if( cnt!=data[hdr+5] ){ |
| 3954 | sprintf(zMsg, "Fragmented space is %d byte reported as %d on page %d", |
| 3955 | cnt, data[hdr+5], iPage); |
| 3956 | checkAppendMsg(pCheck, zMsg, 0); |
| 3957 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3958 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3959 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3960 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /* |
| 3964 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 3965 | ** an array of pages numbers were each page number is the root page of |
| 3966 | ** a table. nRoot is the number of entries in aRoot. |
| 3967 | ** |
| 3968 | ** If everything checks out, this routine returns NULL. If something is |
| 3969 | ** amiss, an error message is written into memory obtained from malloc() |
| 3970 | ** and a pointer to that error message is returned. The calling function |
| 3971 | ** is responsible for freeing the error message when it is done. |
| 3972 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3973 | char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3974 | int i; |
| 3975 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3976 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3977 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3978 | nRef = *sqlite3pager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 3979 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 3980 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 3981 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3982 | sCheck.pBt = pBt; |
| 3983 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3984 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 3985 | if( sCheck.nPage==0 ){ |
| 3986 | unlockBtreeIfUnused(pBt); |
| 3987 | return 0; |
| 3988 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3989 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3990 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3991 | sCheck.zErrMsg = 0; |
| 3992 | |
| 3993 | /* Check the integrity of the freelist |
| 3994 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3995 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 3996 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3997 | |
| 3998 | /* Check all the tables. |
| 3999 | */ |
| 4000 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 4001 | if( aRoot[i]==0 ) continue; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 4002 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /* Make sure every page in the file is referenced |
| 4006 | */ |
| 4007 | for(i=1; i<=sCheck.nPage; i++){ |
| 4008 | if( sCheck.anRef[i]==0 ){ |
| 4009 | char zBuf[100]; |
| 4010 | sprintf(zBuf, "Page %d is never used", i); |
| 4011 | checkAppendMsg(&sCheck, zBuf, 0); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | /* Make sure this analysis did not leave any unref() pages |
| 4016 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4017 | unlockBtreeIfUnused(pBt); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4018 | if( nRef != *sqlite3pager_stats(pBt->pPager) ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4019 | char zBuf[100]; |
| 4020 | sprintf(zBuf, |
| 4021 | "Outstanding page count goes from %d to %d during this analysis", |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4022 | nRef, *sqlite3pager_stats(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4023 | ); |
| 4024 | checkAppendMsg(&sCheck, zBuf, 0); |
| 4025 | } |
| 4026 | |
| 4027 | /* Clean up and report errors. |
| 4028 | */ |
| 4029 | sqliteFree(sCheck.anRef); |
| 4030 | return sCheck.zErrMsg; |
| 4031 | } |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 4032 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4033 | /* |
| 4034 | ** Return the full pathname of the underlying database file. |
| 4035 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4036 | const char *sqlite3BtreeGetFilename(Btree *pBt){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4037 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4038 | return sqlite3pager_filename(pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4042 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 4043 | ** must be active for both files. |
| 4044 | ** |
| 4045 | ** The size of file pBtFrom may be reduced by this operation. |
| 4046 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4047 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4048 | int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4049 | int rc = SQLITE_OK; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4050 | Pgno i, nPage, nToPage; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4051 | |
| 4052 | if( !pBtTo->inTrans || !pBtFrom->inTrans ) return SQLITE_ERROR; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4053 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4054 | memcpy(pBtTo->pPage1, pBtFrom->pPage1, pBtFrom->usableSize); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4055 | rc = sqlite3pager_overwrite(pBtTo->pPager, 1, pBtFrom->pPage1); |
| 4056 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 4057 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4058 | for(i=2; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4059 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4060 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4061 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4062 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4063 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4064 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4065 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4066 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 4067 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4068 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4069 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4070 | rc = sqlite3pager_write(pPage); |
| 4071 | sqlite3pager_unref(pPage); |
| 4072 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4073 | } |
| 4074 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4075 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4076 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4077 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4078 | sqlite3BtreeRollback(pBtTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4079 | } |
| 4080 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4081 | } |