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