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