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