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