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 | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.146 2004/05/22 02:55:23 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
| 15 | ** For a detailed discussion of BTrees, refer to |
| 16 | ** |
| 17 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 18 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 19 | ** Publishing Company, Reading, Massachusetts. |
| 20 | ** |
| 21 | ** The basic idea is that each page of the file contains N database |
| 22 | ** entries and N+1 pointers to subpages. |
| 23 | ** |
| 24 | ** ---------------------------------------------------------------- |
| 25 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) | |
| 26 | ** ---------------------------------------------------------------- |
| 27 | ** |
| 28 | ** All of the keys on the page that Ptr(0) points to have values less |
| 29 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
| 30 | ** values greater than Key(0) and less than Key(1). All of the keys |
| 31 | ** on Ptr(N+1) and its subpages have values greater than Key(N). And |
| 32 | ** so forth. |
| 33 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 34 | ** Finding a particular key requires reading O(log(M)) pages from the |
| 35 | ** disk where M is the number of entries in the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** In this implementation, a single file can hold one or more separate |
| 38 | ** BTrees. Each BTree is identified by the index of its root page. The |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 39 | ** key and data for any entry are combined to form the "payload". A |
| 40 | ** fixed amount of payload can be carried directly on the database |
| 41 | ** page. If the payload is larger than the preset amount then surplus |
| 42 | ** bytes are stored on overflow pages. The payload for an entry |
| 43 | ** and the preceding pointer are combined to form a "Cell". Each |
| 44 | ** page has a small header which contains the Ptr(N+1) pointer and other |
| 45 | ** information such as the size of key and data. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 46 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 47 | ** FORMAT DETAILS |
| 48 | ** |
| 49 | ** The file is divided into pages. The first page is called page 1, |
| 50 | ** the second is page 2, and so forth. A page number of zero indicates |
| 51 | ** "no such page". The page size can be anything between 512 and 65536. |
| 52 | ** Each page can be either a btree page, a freelist page or an overflow |
| 53 | ** page. |
| 54 | ** |
| 55 | ** The first page is always a btree page. The first 100 bytes of the first |
| 56 | ** page contain a special header that describes the file. The format |
| 57 | ** of that header is as follows: |
| 58 | ** |
| 59 | ** OFFSET SIZE DESCRIPTION |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 60 | ** 0 16 Header string: "SQLite format 3\000" |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 61 | ** 16 2 Page size in bytes. |
| 62 | ** 18 1 File format write version |
| 63 | ** 19 1 File format read version |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 64 | ** 20 1 Bytes of unused space at the end of each page |
| 65 | ** 21 1 Max embedded payload fraction |
| 66 | ** 22 1 Min embedded payload fraction |
| 67 | ** 23 1 Min leaf payload fraction |
| 68 | ** 24 4 File change counter |
| 69 | ** 28 4 Reserved for future use |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 70 | ** 32 4 First freelist page |
| 71 | ** 36 4 Number of freelist pages in the file |
| 72 | ** 40 60 15 4-byte meta values passed to higher layers |
| 73 | ** |
| 74 | ** All of the integer values are big-endian (most significant byte first). |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 75 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 76 | ** The file change counter is incremented when the database is changed more |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 77 | ** than once within the same second. This counter, together with the |
| 78 | ** modification time of the file, allows other processes to know |
| 79 | ** when the file has changed and thus when they need to flush their |
| 80 | ** cache. |
| 81 | ** |
| 82 | ** The max embedded payload fraction is the amount of the total usable |
| 83 | ** space in a page that can be consumed by a single cell for standard |
| 84 | ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
| 85 | ** is to limit the maximum cell size so that at least 4 cells will fit |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 86 | ** on one page. Thus the default max embedded payload fraction is 64. |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 87 | ** |
| 88 | ** If the payload for a cell is larger than the max payload, then extra |
| 89 | ** payload is spilled to overflow pages. Once an overflow page is allocated, |
| 90 | ** as many bytes as possible are moved into the overflow pages without letting |
| 91 | ** the cell size drop below the min embedded payload fraction. |
| 92 | ** |
| 93 | ** The min leaf payload fraction is like the min embedded payload fraction |
| 94 | ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum |
| 95 | ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it |
| 96 | ** not specified in the header. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 97 | ** |
| 98 | ** Each btree page begins with a header described below. Note that the |
| 99 | ** header for page one begins at byte 100. For all other btree pages, the |
| 100 | ** header begins on byte zero. |
| 101 | ** |
| 102 | ** OFFSET SIZE DESCRIPTION |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 103 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 104 | ** 1 2 byte offset to the first freeblock |
| 105 | ** 3 2 byte offset to the first cell |
| 106 | ** 5 1 number of fragmented free bytes |
| 107 | ** 6 4 Right child (the Ptr(N+1) value). Omitted if leaf |
| 108 | ** |
| 109 | ** The flags define the format of this btree page. The leaf flag means that |
| 110 | ** this page has no children. The zerodata flag means that this page carries |
| 111 | ** only keys and no data. The intkey flag means that the key is a single |
| 112 | ** variable length integer at the beginning of the payload. |
| 113 | ** |
| 114 | ** A variable-length integer is 1 to 9 bytes where the lower 7 bits of each |
| 115 | ** byte are used. The integer consists of all bytes that have bit 8 set and |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 116 | ** the first byte with bit 8 clear. The most significant byte of the integer |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 117 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
| 118 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
| 119 | ** allows a 64-bit integer to be encoded in 9 bytes. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 120 | ** |
| 121 | ** 0x00 becomes 0x00000000 |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 122 | ** 0x7f becomes 0x0000007f |
| 123 | ** 0x81 0x00 becomes 0x00000080 |
| 124 | ** 0x82 0x00 becomes 0x00000100 |
| 125 | ** 0x80 0x7f becomes 0x0000007f |
| 126 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 127 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 128 | ** |
| 129 | ** Variable length integers are used for rowids and to hold the number of |
| 130 | ** bytes of key and data in a btree cell. |
| 131 | ** |
| 132 | ** Unused space within a btree page is collected into a linked list of |
| 133 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 134 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 135 | ** increasing order. Because a freeblock is 4 bytes in size, the minimum |
| 136 | ** size allocation on a btree page is 4 bytes. Because a freeblock must be |
| 137 | ** at least 4 bytes in size, any group of 3 or fewer unused bytes cannot |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 138 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
| 139 | ** a fragment. The total number of bytes in all fragments is recorded. |
| 140 | ** in the page header at offset 5. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 141 | ** |
| 142 | ** SIZE DESCRIPTION |
| 143 | ** 2 Byte offset of the next freeblock |
| 144 | ** 2 Bytes in this freeblock |
| 145 | ** |
| 146 | ** Cells are of variable length. The first cell begins on the byte defined |
| 147 | ** in the page header. Cells do not necessarily occur in order - they can |
| 148 | ** skip around on the page. |
| 149 | ** |
| 150 | ** SIZE DESCRIPTION |
| 151 | ** 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] | 152 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 153 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 154 | ** 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] | 155 | ** * Payload |
| 156 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 157 | ** |
| 158 | ** Overflow pages form a linked list. Each page except the last is completely |
| 159 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 160 | ** as 1 byte of data. |
| 161 | ** |
| 162 | ** SIZE DESCRIPTION |
| 163 | ** 4 Page number of next overflow page |
| 164 | ** * Data |
| 165 | ** |
| 166 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
| 167 | ** file header points to first in a linked list of trunk page. Each trunk |
| 168 | ** page points to multiple leaf pages. The content of a leaf page is |
| 169 | ** unspecified. A trunk page looks like this: |
| 170 | ** |
| 171 | ** SIZE DESCRIPTION |
| 172 | ** 4 Page number of next trunk page |
| 173 | ** 4 Number of leaf pointers on this page |
| 174 | ** * zero or more pages numbers of leaves |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 175 | */ |
| 176 | #include "sqliteInt.h" |
| 177 | #include "pager.h" |
| 178 | #include "btree.h" |
| 179 | #include <assert.h> |
| 180 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 181 | |
| 182 | /* Maximum page size. The upper bound on this value is 65536 (a limit |
| 183 | ** imposed by the 2-byte offset at the beginning of each cell.) The |
| 184 | ** maximum page size determines the amount of stack space allocated |
| 185 | ** by many of the routines in this module. On embedded architectures |
| 186 | ** or any machine where memory and especially stack memory is limited, |
| 187 | ** one may wish to chose a smaller value for the maximum page size. |
| 188 | */ |
| 189 | #ifndef MX_PAGE_SIZE |
| 190 | # define MX_PAGE_SIZE 1024 |
| 191 | #endif |
| 192 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 193 | /* The following value is the maximum cell size assuming a maximum page |
| 194 | ** size give above. |
| 195 | */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 196 | #define MX_CELL_SIZE (MX_PAGE_SIZE-6) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 197 | |
| 198 | /* The maximum number of cells on a single page of the database. This |
| 199 | ** assumes a minimum cell size of 3 bytes. Such small cells will be |
| 200 | ** exceedingly rare, but they are possible. |
| 201 | */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 202 | #define MX_CELL ((MX_PAGE_SIZE-6)/3) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 203 | |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 204 | /* Forward declarations */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 205 | typedef struct MemPage MemPage; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 206 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 207 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 208 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 209 | ** SQLite database in order to identify the file as a real database. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 210 | ** 123456789 123456 */ |
| 211 | static const char zMagicHeader[] = "SQLite format 3"; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 212 | |
| 213 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 214 | ** Page type flags. An ORed combination of these flags appear as the |
| 215 | ** first byte of every BTree page. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 216 | */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 217 | #define PTF_INTKEY 0x01 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 218 | #define PTF_ZERODATA 0x02 |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 219 | #define PTF_LEAFDATA 0x04 |
| 220 | #define PTF_LEAF 0x08 |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 221 | |
| 222 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 223 | ** As each page of the file is loaded into memory, an instance of the following |
| 224 | ** structure is appended and initialized to zero. This structure stores |
| 225 | ** information about the page that is decoded from the raw file page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 226 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 227 | ** The pParent field points back to the parent page. This allows us to |
| 228 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 229 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 230 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 231 | */ |
| 232 | struct MemPage { |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 233 | u8 isInit; /* True if previously initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 234 | u8 idxShift; /* True if Cell indices have changed */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 235 | u8 isOverfull; /* Some aCell[] do not fit on page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 236 | u8 intKey; /* True if intkey flag is set */ |
| 237 | u8 leaf; /* True if leaf flag is set */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 238 | u8 zeroData; /* True if table stores keys only */ |
| 239 | u8 leafData; /* True if tables stores data on leaves only */ |
| 240 | u8 hasData; /* True if this page stores data */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 241 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 242 | u8 needRelink; /* True if cell not linked properly in aData */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 243 | int idxParent; /* Index in pParent->aCell[] of this node */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 244 | int nFree; /* Number of free bytes on the page */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 245 | int nCell; /* Number of entries on this page */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 246 | int nCellAlloc; /* Number of slots allocated in aCell[] */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 247 | unsigned char **aCell; /* Pointer to start of each cell */ |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 248 | struct Btree *pBt; /* Pointer back to BTree structure */ |
| 249 | |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 250 | /* When page content is move from one page to the other (by the movePage() |
| 251 | ** subroutine) only the information about is moved. The information below |
| 252 | ** is fixed. */ |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 253 | unsigned char *aData; /* Pointer back to the start of the page */ |
| 254 | Pgno pgno; /* Page number for this page */ |
| 255 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 256 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 257 | |
| 258 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 259 | ** The in-memory image of a disk page has the auxiliary information appended |
| 260 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 261 | ** that extra information. |
| 262 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 263 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 264 | |
| 265 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 266 | ** Everything we need to know about an open database |
| 267 | */ |
| 268 | struct Btree { |
| 269 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 270 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 271 | MemPage *pPage1; /* First page of the database */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 272 | u8 inTrans; /* True if a transaction is in progress */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 273 | u8 inStmt; /* True if we are in a statement subtransaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 274 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 275 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 276 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 277 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 278 | int pageSize; /* Total number of bytes on a page */ |
| 279 | int usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 280 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 281 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 282 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 283 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 284 | }; |
| 285 | typedef Btree Bt; |
| 286 | |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 287 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 288 | ** An instance of the following structure is used to hold information |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 289 | ** about a cell. The parseCell() function fills in this structure |
| 290 | ** based on information extract from the raw disk page. |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 291 | */ |
| 292 | typedef struct CellInfo CellInfo; |
| 293 | struct CellInfo { |
| 294 | i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 295 | u32 nData; /* Number of bytes of data */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 296 | u16 nHeader; /* Size of the cell header in bytes */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 297 | u16 nLocal; /* Amount of payload held locally */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 298 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
| 299 | u16 nSize; /* Total size of the cell (on the main b-tree page) */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | /* |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 303 | ** A cursor is a pointer to a particular entry in the BTree. |
| 304 | ** The entry is identified by its MemPage and the index in |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 305 | ** MemPage.aCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 306 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 307 | struct BtCursor { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 308 | Btree *pBt; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 309 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 310 | BtCursor *pShared; /* Loop of cursors with the same root page */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 311 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 312 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 313 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 314 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 315 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 316 | CellInfo info; /* A parse of the cell we are pointing at */ |
| 317 | u8 infoValid; /* True if information in BtCursor.info is valid */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 318 | u8 wrFlag; /* True if writable */ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 319 | u8 isValid; /* TRUE if points to a valid entry */ |
| 320 | u8 status; /* Set to SQLITE_ABORT if cursors is invalidated */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 321 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 322 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 323 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 324 | ** Read or write a two- and four-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 325 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 326 | static u32 get2byte(unsigned char *p){ |
| 327 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 328 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 329 | static u32 get4byte(unsigned char *p){ |
| 330 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 331 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 332 | static void put2byte(unsigned char *p, u32 v){ |
| 333 | p[0] = v>>8; |
| 334 | p[1] = v; |
| 335 | } |
| 336 | static void put4byte(unsigned char *p, u32 v){ |
| 337 | p[0] = v>>24; |
| 338 | p[1] = v>>16; |
| 339 | p[2] = v>>8; |
| 340 | p[3] = v; |
| 341 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 342 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 343 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 344 | ** Routines to read and write variable-length integers. These used to |
| 345 | ** be defined locally, but now we use the varint routines in the util.c |
| 346 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 347 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 348 | #define getVarint sqlite3GetVarint |
| 349 | #define getVarint32 sqlite3GetVarint32 |
| 350 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 351 | |
| 352 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 353 | ** Parse a cell header and fill in the CellInfo structure. |
| 354 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 355 | static void parseCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 356 | MemPage *pPage, /* Page containing the cell */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 357 | unsigned char *pCell, /* Pointer to the first byte of the cell */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 358 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 359 | ){ |
| 360 | int n; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 361 | int nPayload; |
| 362 | Btree *pBt; |
| 363 | int minLocal, maxLocal; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 364 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
| 365 | n = 6 - 4*pPage->leaf; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 366 | if( pPage->hasData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 367 | n += getVarint32(&pCell[n], &pInfo->nData); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 368 | }else{ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 369 | pInfo->nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 370 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 371 | n += getVarint(&pCell[n], &pInfo->nKey); |
| 372 | pInfo->nHeader = n; |
| 373 | nPayload = pInfo->nData; |
| 374 | if( !pPage->intKey ){ |
| 375 | nPayload += pInfo->nKey; |
| 376 | } |
| 377 | pBt = pPage->pBt; |
| 378 | if( pPage->leafData ){ |
| 379 | minLocal = pBt->minLeaf; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 380 | maxLocal = pBt->maxLeaf; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 381 | }else{ |
| 382 | minLocal = pBt->minLocal; |
| 383 | maxLocal = pBt->maxLocal; |
| 384 | } |
| 385 | if( nPayload<=maxLocal ){ |
| 386 | pInfo->nLocal = nPayload; |
| 387 | pInfo->iOverflow = 0; |
| 388 | pInfo->nSize = nPayload + n; |
| 389 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 390 | int surplus = minLocal + (nPayload - minLocal)%(pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 391 | if( surplus <= maxLocal ){ |
| 392 | pInfo->nLocal = surplus; |
| 393 | }else{ |
| 394 | pInfo->nLocal = minLocal; |
| 395 | } |
| 396 | pInfo->iOverflow = pInfo->nLocal + n; |
| 397 | pInfo->nSize = pInfo->iOverflow + 4; |
| 398 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 402 | ** Compute the total number of bytes that a Cell needs on the main |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 403 | ** database page. The number returned includes the Cell header, |
| 404 | ** local payload storage, and the pointer to overflow pages (if |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 405 | ** applicable). Additional space allocated on overflow pages |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 406 | ** is NOT included in the value returned from this routine. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 407 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 408 | static int cellSize(MemPage *pPage, unsigned char *pCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 409 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 410 | |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 411 | parseCell(pPage, pCell, &info); |
| 412 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 416 | ** Do sanity checking on a page. Throw an exception if anything is |
| 417 | ** not right. |
| 418 | ** |
| 419 | ** This routine is used for internal error checking only. It is omitted |
| 420 | ** from most builds. |
| 421 | */ |
| 422 | #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0 |
| 423 | static void _pageIntegrity(MemPage *pPage){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 424 | int usableSize; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 425 | u8 *data; |
| 426 | int i, idx, c, pc, hdr, nFree; |
| 427 | u8 used[MX_PAGE_SIZE]; |
| 428 | |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 429 | usableSize = pPage->pBt->usableSize; |
| 430 | assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->pageSize] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 431 | hdr = pPage->hdrOffset; |
| 432 | assert( hdr==(pPage->pgno==1 ? 100 : 0) ); |
| 433 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
| 434 | c = pPage->aData[hdr]; |
| 435 | if( pPage->isInit ){ |
| 436 | assert( pPage->leaf == ((c & PTF_LEAF)!=0) ); |
| 437 | assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 438 | assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) ); |
| 439 | assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) ); |
| 440 | assert( pPage->hasData == |
| 441 | !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 442 | } |
| 443 | data = pPage->aData; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 444 | memset(used, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 445 | for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1; |
| 446 | nFree = 0; |
| 447 | pc = get2byte(&data[hdr+1]); |
| 448 | while( pc ){ |
| 449 | int size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 450 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 451 | size = get2byte(&data[pc+2]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 452 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 453 | nFree += size; |
| 454 | for(i=pc; i<pc+size; i++){ |
| 455 | assert( used[i]==0 ); |
| 456 | used[i] = 1; |
| 457 | } |
| 458 | pc = get2byte(&data[pc]); |
| 459 | } |
| 460 | assert( pPage->isInit==0 || pPage->nFree==nFree+data[hdr+5] ); |
| 461 | idx = 0; |
| 462 | pc = get2byte(&data[hdr+3]); |
| 463 | while( pc ){ |
| 464 | int size; |
| 465 | assert( pPage->isInit==0 || idx<pPage->nCell ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 466 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 467 | assert( pPage->isInit==0 || pPage->aCell[idx]==&data[pc] ); |
| 468 | size = cellSize(pPage, &data[pc]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 469 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 470 | for(i=pc; i<pc+size; i++){ |
| 471 | assert( used[i]==0 ); |
| 472 | used[i] = 1; |
| 473 | } |
| 474 | pc = get2byte(&data[pc]); |
| 475 | idx++; |
| 476 | } |
| 477 | assert( idx==pPage->nCell ); |
| 478 | nFree = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 479 | for(i=0; i<usableSize; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 480 | assert( used[i]<=1 ); |
| 481 | if( used[i]==0 ) nFree++; |
| 482 | } |
| 483 | assert( nFree==data[hdr+5] ); |
| 484 | } |
| 485 | #define pageIntegrity(X) _pageIntegrity(X) |
| 486 | #else |
| 487 | # define pageIntegrity(X) |
| 488 | #endif |
| 489 | |
| 490 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 491 | ** Defragment the page given. All Cells are moved to the |
| 492 | ** beginning of the page and all free space is collected |
| 493 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 494 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 495 | static void defragmentPage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 496 | int pc, i, n, addr; |
| 497 | int start, hdr, size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 498 | int leftover; |
| 499 | unsigned char *oldPage; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 500 | unsigned char newPage[MX_PAGE_SIZE]; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 501 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 502 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 503 | assert( pPage->pBt!=0 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 504 | assert( pPage->pBt->usableSize <= MX_PAGE_SIZE ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 505 | assert( !pPage->needRelink ); |
| 506 | assert( !pPage->isOverfull ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 507 | oldPage = pPage->aData; |
| 508 | hdr = pPage->hdrOffset; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 509 | addr = 3+hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 510 | n = 6+hdr; |
| 511 | if( !pPage->leaf ){ |
| 512 | n += 4; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 513 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 514 | memcpy(&newPage[hdr], &oldPage[hdr], n-hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 515 | start = n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 516 | pc = get2byte(&oldPage[addr]); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 517 | i = 0; |
| 518 | while( pc>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 519 | assert( n<pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 520 | size = cellSize(pPage, &oldPage[pc]); |
| 521 | memcpy(&newPage[n], &oldPage[pc], size); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 522 | put2byte(&newPage[addr],n); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 523 | assert( pPage->aCell[i]==&oldPage[pc] ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 524 | pPage->aCell[i++] = &oldPage[n]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 525 | addr = n; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 526 | n += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 527 | pc = get2byte(&oldPage[pc]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 528 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 529 | assert( i==pPage->nCell ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 530 | leftover = pPage->pBt->usableSize - n; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 531 | assert( leftover>=0 ); |
| 532 | assert( pPage->nFree==leftover ); |
| 533 | if( leftover<4 ){ |
| 534 | oldPage[hdr+5] = leftover; |
| 535 | leftover = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 536 | n = pPage->pBt->usableSize; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 537 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 538 | memcpy(&oldPage[hdr], &newPage[hdr], n-hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 539 | if( leftover==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 540 | put2byte(&oldPage[hdr+1], 0); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 541 | }else if( leftover>=4 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 542 | put2byte(&oldPage[hdr+1], n); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 543 | put2byte(&oldPage[n], 0); |
| 544 | put2byte(&oldPage[n+2], leftover); |
| 545 | memset(&oldPage[n+4], 0, leftover-4); |
| 546 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 547 | oldPage[hdr+5] = 0; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 550 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 551 | ** Allocate nByte bytes of space on a page. If nByte is less than |
| 552 | ** 4 it is rounded up to 4. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 553 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 554 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 555 | ** the new allocation. Or return 0 if there is not enough free |
| 556 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 557 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 558 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 559 | ** nBytes of contiguous free space, then this routine automatically |
| 560 | ** calls defragementPage() to consolidate all free space before |
| 561 | ** allocating the new chunk. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 562 | ** |
| 563 | ** Algorithm: Carve a piece off of the first freeblock that is |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 564 | ** nByte in size or larger. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 565 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 566 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 567 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 568 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 569 | int nFrag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 570 | unsigned char *data; |
drh | 44ce7e2 | 2003-06-17 02:57:17 +0000 | [diff] [blame] | 571 | #ifndef NDEBUG |
| 572 | int cnt = 0; |
| 573 | #endif |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 574 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 575 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 576 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 577 | assert( pPage->pBt ); |
| 578 | if( nByte<4 ) nByte = 4; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 579 | if( pPage->nFree<nByte || pPage->isOverfull ) return 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 580 | hdr = pPage->hdrOffset; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 581 | nFrag = data[hdr+5]; |
| 582 | if( nFrag>=60 || nFrag>pPage->nFree-nByte ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 583 | defragmentPage(pPage); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 584 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 585 | addr = hdr+1; |
| 586 | pc = get2byte(&data[addr]); |
| 587 | assert( addr<pc ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 588 | assert( pc<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 589 | while( (size = get2byte(&data[pc+2]))<nByte ){ |
| 590 | addr = pc; |
| 591 | pc = get2byte(&data[addr]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 592 | assert( pc<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 593 | assert( pc>=addr+size+4 || pc==0 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 594 | if( pc==0 ){ |
| 595 | assert( (cnt++)==0 ); |
| 596 | defragmentPage(pPage); |
| 597 | assert( data[hdr+5]==0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 598 | addr = pPage->hdrOffset+1; |
| 599 | pc = get2byte(&data[addr]); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | assert( pc>0 && size>=nByte ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 603 | assert( pc+size<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 604 | if( size>nByte+4 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 605 | int newStart = pc+nByte; |
| 606 | put2byte(&data[addr], newStart); |
| 607 | put2byte(&data[newStart], get2byte(&data[pc])); |
| 608 | put2byte(&data[newStart+2], size-nByte); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 609 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 610 | put2byte(&data[addr], get2byte(&data[pc])); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 611 | data[hdr+5] += size-nByte; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 612 | } |
| 613 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 614 | assert( pPage->nFree>=0 ); |
| 615 | return pc; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 619 | ** Return a section of the pPage->aData to the freelist. |
| 620 | ** The first byte of the new free block is pPage->aDisk[start] |
| 621 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 622 | ** |
| 623 | ** Most of the effort here is involved in coalesing adjacent |
| 624 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 625 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 626 | static void freeSpace(MemPage *pPage, int start, int size){ |
| 627 | int end = start + size; /* End of the segment being freed */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 628 | int addr, pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 629 | #ifndef NDEBUG |
| 630 | int tsize = 0; /* Total size of all freeblocks */ |
| 631 | #endif |
| 632 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 633 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 634 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 635 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 636 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 637 | assert( end<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 638 | if( size<4 ) size = 4; |
| 639 | |
| 640 | /* Add the space back into the linked list of freeblocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 641 | addr = pPage->hdrOffset + 1; |
| 642 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 643 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 644 | assert( pbegin>addr ); |
| 645 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 646 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 647 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 648 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 649 | put2byte(&data[addr], start); |
| 650 | put2byte(&data[start], pbegin); |
| 651 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 652 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 653 | |
| 654 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 655 | addr = pPage->hdrOffset + 1; |
| 656 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 657 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 658 | assert( pbegin>addr ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 659 | assert( pbegin<pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 660 | pnext = get2byte(&data[pbegin]); |
| 661 | psize = get2byte(&data[pbegin+2]); |
| 662 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 663 | int frag = pnext - (pbegin+psize); |
| 664 | assert( frag<=data[pPage->hdrOffset+5] ); |
| 665 | data[pPage->hdrOffset+5] -= frag; |
| 666 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 667 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 668 | }else{ |
| 669 | assert( (tsize += psize)>0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 670 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | assert( tsize+data[pPage->hdrOffset+5]==pPage->nFree ); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 674 | } |
| 675 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 676 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 677 | ** Resize the aCell[] array of the given page so that it is able to |
| 678 | ** hold at least nNewSz entries. |
| 679 | ** |
| 680 | ** Return SQLITE_OK or SQLITE_NOMEM. |
| 681 | */ |
| 682 | static int resizeCellArray(MemPage *pPage, int nNewSz){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 683 | if( pPage->nCellAlloc<nNewSz ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 684 | int n = nNewSz*sizeof(pPage->aCell[0]); |
| 685 | if( pPage->aCell==0 ){ |
| 686 | pPage->aCell = sqliteMallocRaw( n ); |
| 687 | }else{ |
| 688 | pPage->aCell = sqliteRealloc(pPage->aCell, n); |
| 689 | } |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 690 | if( sqlite3_malloc_failed ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 691 | pPage->nCellAlloc = nNewSz; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 692 | } |
| 693 | return SQLITE_OK; |
| 694 | } |
| 695 | |
| 696 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 697 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 698 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 699 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 700 | ** is the parent of the page being initialized. The root of a |
| 701 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 702 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 703 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 704 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 705 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 706 | ** guarantee that the page is well-formed. It only shows that |
| 707 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 708 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 709 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 710 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 711 | MemPage *pParent /* The parent. Might be NULL */ |
| 712 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 713 | int c, pc, i, hdr; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 714 | unsigned char *data; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 715 | int usableSize; |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 716 | int nCell, nFree; |
| 717 | u8 *aCell[MX_PAGE_SIZE/2]; |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 718 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 719 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 720 | assert( pPage->pBt!=0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 721 | assert( pParent==0 || pParent->pBt==pPage->pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 722 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 723 | assert( pPage->aData == &((unsigned char*)pPage)[-pPage->pBt->pageSize] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 724 | assert( pPage->pParent==0 || pPage->pParent==pParent ); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 725 | assert( pPage->pParent==pParent || !pPage->isInit ); |
| 726 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 727 | if( pPage->pParent==0 && pParent!=0 ){ |
| 728 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 729 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 730 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 731 | pPage->nCell = pPage->nCellAlloc = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 732 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 733 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 734 | data = pPage->aData; |
| 735 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 736 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 737 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 738 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 739 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 740 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 741 | pPage->isOverfull = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 742 | pPage->needRelink = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 743 | pPage->idxShift = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 744 | usableSize = pPage->pBt->usableSize; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 745 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 746 | /* Initialize the cell count and cell pointers */ |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 747 | i = 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 748 | pc = get2byte(&data[hdr+3]); |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 749 | nCell = 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 750 | while( pc>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 751 | if( pc>=usableSize ) return SQLITE_CORRUPT; |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 752 | if( nCell>sizeof(aCell)/sizeof(aCell[0]) ) return SQLITE_CORRUPT; |
| 753 | aCell[nCell++] = &data[pc]; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 754 | pc = get2byte(&data[pc]); |
| 755 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 756 | if( resizeCellArray(pPage, nCell) ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 757 | return SQLITE_NOMEM; |
| 758 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 759 | pPage->nCell = nCell; |
| 760 | memcpy(pPage->aCell, aCell, nCell*sizeof(aCell[0])); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 761 | |
| 762 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 763 | pc = get2byte(&data[hdr+1]); |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 764 | nFree = data[hdr+5]; |
| 765 | i = 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 766 | while( pc>0 ){ |
| 767 | int next, size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 768 | if( pc>=usableSize ) return SQLITE_CORRUPT; |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 769 | if( i++>MX_PAGE_SIZE ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 770 | next = get2byte(&data[pc]); |
| 771 | size = get2byte(&data[pc+2]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 772 | if( next>0 && next<=pc+size+3 ) return SQLITE_CORRUPT; |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 773 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 774 | pc = next; |
| 775 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 776 | pPage->nFree = nFree; |
| 777 | if( nFree>=usableSize ) return SQLITE_CORRUPT; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 778 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 779 | pPage->isInit = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 780 | pageIntegrity(pPage); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 781 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 785 | ** Set up a raw page so that it looks like a database page holding |
| 786 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 787 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 788 | static void zeroPage(MemPage *pPage, int flags){ |
| 789 | unsigned char *data = pPage->aData; |
| 790 | Btree *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 791 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 792 | int first; |
| 793 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 794 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
| 795 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 796 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 797 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 798 | data[hdr] = flags; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 799 | first = hdr + 6 + 4*((flags&PTF_LEAF)==0); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 800 | put2byte(&data[hdr+1], first); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 801 | put2byte(&data[first+2], pBt->usableSize - first); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 802 | sqliteFree(pPage->aCell); |
| 803 | pPage->aCell = 0; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 804 | pPage->nCell = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 805 | pPage->nCellAlloc = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 806 | pPage->nFree = pBt->usableSize - first; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 807 | pPage->intKey = (flags & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 808 | pPage->zeroData = (flags & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 809 | pPage->leafData = (flags & PTF_LEAFDATA)!=0; |
| 810 | pPage->leaf = (flags & PTF_LEAF)!=0; |
| 811 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 812 | pPage->hdrOffset = hdr; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 813 | pPage->isOverfull = 0; |
| 814 | pPage->needRelink = 0; |
| 815 | pPage->idxShift = 0; |
| 816 | pPage->isInit = 1; |
| 817 | pageIntegrity(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 821 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 822 | ** MemPage.aData elements if needed. |
| 823 | */ |
| 824 | static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){ |
| 825 | int rc; |
| 826 | unsigned char *aData; |
| 827 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 828 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 829 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 830 | pPage = (MemPage*)&aData[pBt->pageSize]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 831 | pPage->aData = aData; |
| 832 | pPage->pBt = pBt; |
| 833 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 834 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 835 | *ppPage = pPage; |
| 836 | return SQLITE_OK; |
| 837 | } |
| 838 | |
| 839 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 840 | ** Get a page from the pager and initialize it. This routine |
| 841 | ** is just a convenience wrapper around separate calls to |
| 842 | ** getPage() and initPage(). |
| 843 | */ |
| 844 | static int getAndInitPage( |
| 845 | Btree *pBt, /* The database file */ |
| 846 | Pgno pgno, /* Number of the page to get */ |
| 847 | MemPage **ppPage, /* Write the page pointer here */ |
| 848 | MemPage *pParent /* Parent of the page */ |
| 849 | ){ |
| 850 | int rc; |
| 851 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 852 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 853 | rc = initPage(*ppPage, pParent); |
| 854 | } |
| 855 | return rc; |
| 856 | } |
| 857 | |
| 858 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 859 | ** Release a MemPage. This should be called once for each prior |
| 860 | ** call to getPage. |
| 861 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 862 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 863 | if( pPage ){ |
| 864 | assert( pPage->aData ); |
| 865 | assert( pPage->pBt ); |
| 866 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 867 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 872 | ** This routine is called when the reference count for a page |
| 873 | ** reaches zero. We need to unref the pParent pointer when that |
| 874 | ** happens. |
| 875 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 876 | static void pageDestructor(void *pData, int pageSize){ |
| 877 | MemPage *pPage = (MemPage*)&((char*)pData)[pageSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 878 | assert( pPage->isInit==0 || pPage->needRelink==0 ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 879 | if( pPage->pParent ){ |
| 880 | MemPage *pParent = pPage->pParent; |
| 881 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 882 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 883 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 884 | sqliteFree(pPage->aCell); |
| 885 | pPage->aCell = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 886 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 890 | ** Open a new database. |
| 891 | ** |
| 892 | ** Actually, this routine just sets up the internal data structures |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 893 | ** for accessing the database. We do not open the database file |
| 894 | ** until the first page is loaded. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 895 | ** |
| 896 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 897 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 898 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 899 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 900 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 901 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 902 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
| 903 | int nCache, /* Number of cache pages */ |
| 904 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 905 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 906 | Btree *pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 907 | int rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 908 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 909 | /* |
| 910 | ** The following asserts make sure that structures used by the btree are |
| 911 | ** the right size. This is to guard against size changes that result |
| 912 | ** when compiling on a different architecture. |
| 913 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 914 | assert( sizeof(i64)==8 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 915 | assert( sizeof(u64)==8 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 916 | assert( sizeof(u32)==4 ); |
| 917 | assert( sizeof(u16)==2 ); |
| 918 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 919 | assert( sizeof(ptr)==sizeof(char*) ); |
| 920 | assert( sizeof(uptr)==sizeof(ptr) ); |
| 921 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 922 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 923 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 924 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 925 | return SQLITE_NOMEM; |
| 926 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 927 | if( nCache<10 ) nCache = 10; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 928 | rc = sqlite3pager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE, |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 929 | (flags & BTREE_OMIT_JOURNAL)==0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 930 | if( rc!=SQLITE_OK ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 931 | if( pBt->pPager ) sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 932 | sqliteFree(pBt); |
| 933 | *ppBtree = 0; |
| 934 | return rc; |
| 935 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 936 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 937 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 938 | pBt->pPage1 = 0; |
| 939 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 940 | pBt->pageSize = SQLITE_PAGE_SIZE; /* FIX ME - read from header */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 941 | pBt->usableSize = pBt->pageSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 942 | pBt->maxEmbedFrac = 64; /* FIX ME - read from header */ |
| 943 | pBt->minEmbedFrac = 32; /* FIX ME - read from header */ |
| 944 | pBt->minLeafFrac = 32; /* FIX ME - read from header */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 945 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 946 | *ppBtree = pBt; |
| 947 | return SQLITE_OK; |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | ** Close an open database and invalidate all cursors. |
| 952 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 953 | int sqlite3BtreeClose(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 954 | while( pBt->pCursor ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 955 | sqlite3BtreeCloseCursor(pBt->pCursor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 956 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 957 | sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 958 | sqliteFree(pBt); |
| 959 | return SQLITE_OK; |
| 960 | } |
| 961 | |
| 962 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 963 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 964 | ** |
| 965 | ** The maximum number of cache pages is set to the absolute |
| 966 | ** value of mxPage. If mxPage is negative, the pager will |
| 967 | ** operate asynchronously - it will not stop to do fsync()s |
| 968 | ** to insure data is written to the disk surface before |
| 969 | ** continuing. Transactions still work if synchronous is off, |
| 970 | ** and the database cannot be corrupted if this program |
| 971 | ** crashes. But if the operating system crashes or there is |
| 972 | ** an abrupt power failure when synchronous is off, the database |
| 973 | ** could be left in an inconsistent and unrecoverable state. |
| 974 | ** Synchronous is on by default so database corruption is not |
| 975 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 976 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 977 | int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 978 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 979 | return SQLITE_OK; |
| 980 | } |
| 981 | |
| 982 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 983 | ** Change the way data is synced to disk in order to increase or decrease |
| 984 | ** how well the database resists damage due to OS crashes and power |
| 985 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 986 | ** there is a high probability of damage) Level 2 is the default. There |
| 987 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 988 | ** probability of damage to near zero but with a write performance reduction. |
| 989 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 990 | int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 991 | sqlite3pager_set_safety_level(pBt->pPager, level); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 992 | return SQLITE_OK; |
| 993 | } |
| 994 | |
| 995 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 996 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 997 | ** also acquire a readlock on that file. |
| 998 | ** |
| 999 | ** SQLITE_OK is returned on success. If the file is not a |
| 1000 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1001 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1002 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1003 | ** if there is a locking protocol violation. |
| 1004 | */ |
| 1005 | static int lockBtree(Btree *pBt){ |
| 1006 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1007 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1008 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1009 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1010 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1011 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* Do some checking to help insure the file we opened really is |
| 1014 | ** a valid database file. |
| 1015 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1016 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1017 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1018 | u8 *page1 = pPage1->aData; |
| 1019 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1020 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1021 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1022 | if( page1[18]>1 || page1[19]>1 ){ |
| 1023 | goto page1_init_failed; |
| 1024 | } |
| 1025 | pBt->pageSize = get2byte(&page1[16]); |
| 1026 | pBt->usableSize = pBt->pageSize - page1[20]; |
| 1027 | if( pBt->usableSize<500 ){ |
| 1028 | goto page1_init_failed; |
| 1029 | } |
| 1030 | pBt->maxEmbedFrac = page1[21]; |
| 1031 | pBt->minEmbedFrac = page1[22]; |
| 1032 | pBt->minLeafFrac = page1[23]; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1033 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1034 | |
| 1035 | /* maxLocal is the maximum amount of payload to store locally for |
| 1036 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1037 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1038 | ** Besides the payload, the cell must store: |
| 1039 | ** 2-byte pointer to next cell |
| 1040 | ** 4-byte child pointer |
| 1041 | ** 9-byte nKey value |
| 1042 | ** 4-byte nData value |
| 1043 | ** 4-byte overflow page pointer |
| 1044 | ** So a cell consists of a header which is as much as 19 bytes long, |
| 1045 | ** 0 to N bytes of payload, and an optional 4 byte overflow page pointer. |
| 1046 | */ |
| 1047 | pBt->maxLocal = (pBt->usableSize-10)*pBt->maxEmbedFrac/255 - 23; |
| 1048 | pBt->minLocal = (pBt->usableSize-10)*pBt->minEmbedFrac/255 - 23; |
| 1049 | pBt->maxLeaf = pBt->usableSize - 33; |
| 1050 | pBt->minLeaf = (pBt->usableSize-10)*pBt->minLeafFrac/255 - 23; |
| 1051 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1052 | goto page1_init_failed; |
| 1053 | } |
| 1054 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1055 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1056 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1057 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1058 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1059 | releasePage(pPage1); |
| 1060 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1061 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1065 | ** If there are no outstanding cursors and we are not in the middle |
| 1066 | ** of a transaction but there is a read lock on the database, then |
| 1067 | ** this routine unrefs the first page of the database file which |
| 1068 | ** has the effect of releasing the read lock. |
| 1069 | ** |
| 1070 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1071 | ** |
| 1072 | ** If there is a transaction in progress, this routine is a no-op. |
| 1073 | */ |
| 1074 | static void unlockBtreeIfUnused(Btree *pBt){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1075 | if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
| 1076 | releasePage(pBt->pPage1); |
| 1077 | pBt->pPage1 = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1078 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1079 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1084 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1085 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1086 | */ |
| 1087 | static int newDatabase(Btree *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1088 | MemPage *pP1; |
| 1089 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1090 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1091 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1092 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1093 | assert( pP1!=0 ); |
| 1094 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1095 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1096 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1097 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1098 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1099 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1100 | data[18] = 1; |
| 1101 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1102 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1103 | data[21] = pBt->maxEmbedFrac; |
| 1104 | data[22] = pBt->minEmbedFrac; |
| 1105 | data[23] = pBt->minLeafFrac; |
| 1106 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1107 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1108 | return SQLITE_OK; |
| 1109 | } |
| 1110 | |
| 1111 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1112 | ** Attempt to start a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1113 | ** |
| 1114 | ** A transaction must be started before attempting any changes |
| 1115 | ** to the database. None of the following routines will work |
| 1116 | ** unless a transaction is started first: |
| 1117 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1118 | ** sqlite3BtreeCreateTable() |
| 1119 | ** sqlite3BtreeCreateIndex() |
| 1120 | ** sqlite3BtreeClearTable() |
| 1121 | ** sqlite3BtreeDropTable() |
| 1122 | ** sqlite3BtreeInsert() |
| 1123 | ** sqlite3BtreeDelete() |
| 1124 | ** sqlite3BtreeUpdateMeta() |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1125 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1126 | int sqlite3BtreeBeginTrans(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1127 | int rc; |
| 1128 | if( pBt->inTrans ) return SQLITE_ERROR; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1129 | if( pBt->readOnly ) return SQLITE_READONLY; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1130 | if( pBt->pPage1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1131 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1132 | if( rc!=SQLITE_OK ){ |
| 1133 | return rc; |
| 1134 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1135 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1136 | rc = sqlite3pager_begin(pBt->pPage1->aData); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1137 | if( rc==SQLITE_OK ){ |
| 1138 | rc = newDatabase(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1139 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1140 | if( rc==SQLITE_OK ){ |
| 1141 | pBt->inTrans = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1142 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1143 | }else{ |
| 1144 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1145 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1146 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1150 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1151 | ** |
| 1152 | ** This will release the write lock on the database file. If there |
| 1153 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1154 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1155 | int sqlite3BtreeCommit(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1156 | int rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1157 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_commit(pBt->pPager); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1158 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1159 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1160 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1161 | return rc; |
| 1162 | } |
| 1163 | |
| 1164 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1165 | ** Invalidate all cursors |
| 1166 | */ |
| 1167 | static void invalidateCursors(Btree *pBt){ |
| 1168 | BtCursor *pCur; |
| 1169 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1170 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1171 | if( pPage /* && !pPage->isInit */ ){ |
| 1172 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1173 | releasePage(pPage); |
| 1174 | pCur->pPage = 0; |
| 1175 | pCur->isValid = 0; |
| 1176 | pCur->status = SQLITE_ABORT; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1181 | #ifdef SQLITE_TEST |
| 1182 | /* |
| 1183 | ** Print debugging information about all cursors to standard output. |
| 1184 | */ |
| 1185 | void sqlite3BtreeCursorList(Btree *pBt){ |
| 1186 | BtCursor *pCur; |
| 1187 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1188 | MemPage *pPage = pCur->pPage; |
| 1189 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
| 1190 | printf("CURSOR %08x rooted at %4d(%s) currently at %d.%d%s\n", |
| 1191 | (int)pCur, pCur->pgnoRoot, zMode, |
| 1192 | pPage ? pPage->pgno : 0, pCur->idx, |
| 1193 | pCur->isValid ? "" : " eof" |
| 1194 | ); |
| 1195 | } |
| 1196 | } |
| 1197 | #endif |
| 1198 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1199 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1200 | ** Rollback the transaction in progress. All cursors will be |
| 1201 | ** invalided by this operation. Any attempt to use a cursor |
| 1202 | ** that was open at the beginning of this operation will result |
| 1203 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1204 | ** |
| 1205 | ** This will release the write lock on the database file. If there |
| 1206 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1207 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1208 | int sqlite3BtreeRollback(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1209 | int rc; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1210 | MemPage *pPage1; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1211 | if( pBt->inTrans==0 ) return SQLITE_OK; |
| 1212 | pBt->inTrans = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1213 | pBt->inStmt = 0; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1214 | if( pBt->readOnly ){ |
| 1215 | rc = SQLITE_OK; |
| 1216 | }else{ |
| 1217 | rc = sqlite3pager_rollback(pBt->pPager); |
| 1218 | /* The rollback may have destroyed the pPage1->aData value. So |
| 1219 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 1220 | ** set correctly. */ |
| 1221 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 1222 | releasePage(pPage1); |
| 1223 | } |
| 1224 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1225 | invalidateCursors(pBt); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1226 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1227 | return rc; |
| 1228 | } |
| 1229 | |
| 1230 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1231 | ** Start a statement subtransaction. The subtransaction can |
| 1232 | ** can be rolled back independently of the main transaction. |
| 1233 | ** You must start a transaction before starting a subtransaction. |
| 1234 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1235 | ** commits or rolls back. |
| 1236 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1237 | ** Only one subtransaction may be active at a time. It is an error to try |
| 1238 | ** to start a new subtransaction if another subtransaction is already active. |
| 1239 | ** |
| 1240 | ** Statement subtransactions are used around individual SQL statements |
| 1241 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 1242 | ** error occurs within the statement, the effect of that one statement |
| 1243 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1244 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1245 | int sqlite3BtreeBeginStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1246 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1247 | if( !pBt->inTrans || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1248 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 1249 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1250 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1251 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1252 | return rc; |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1257 | ** Commit the statment subtransaction currently in progress. If no |
| 1258 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1259 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1260 | int sqlite3BtreeCommitStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1261 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1262 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1263 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1264 | }else{ |
| 1265 | rc = SQLITE_OK; |
| 1266 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1267 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1268 | return rc; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1272 | ** Rollback the active statement subtransaction. If no subtransaction |
| 1273 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1274 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1275 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1276 | ** to use a cursor that was open at the beginning of this operation |
| 1277 | ** will result in an error. |
| 1278 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1279 | int sqlite3BtreeRollbackStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1280 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1281 | if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1282 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1283 | invalidateCursors(pBt); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1284 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1285 | return rc; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1289 | ** Default key comparison function to be used if no comparison function |
| 1290 | ** is specified on the sqlite3BtreeCursor() call. |
| 1291 | */ |
| 1292 | static int dfltCompare( |
| 1293 | void *NotUsed, /* User data is not used */ |
| 1294 | int n1, const void *p1, /* First key to compare */ |
| 1295 | int n2, const void *p2 /* Second key to compare */ |
| 1296 | ){ |
| 1297 | int c; |
| 1298 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 1299 | if( c==0 ){ |
| 1300 | c = n1 - n2; |
| 1301 | } |
| 1302 | return c; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1306 | ** Create a new cursor for the BTree whose root is on the page |
| 1307 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 1308 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1309 | ** |
| 1310 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1311 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 1312 | ** writing if other conditions for writing are also met. These |
| 1313 | ** are the conditions that must be met in order for writing to |
| 1314 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1315 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1316 | ** 1: The cursor must have been opened with wrFlag==1 |
| 1317 | ** |
| 1318 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 1319 | ** |
| 1320 | ** 3: The database must be writable (not on read-only media) |
| 1321 | ** |
| 1322 | ** 4: There must be an active transaction. |
| 1323 | ** |
| 1324 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 1325 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 1326 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 1327 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 1328 | ** change as long as the cursor is open. This allows the cursor to |
| 1329 | ** do a sequential scan of the table without having to worry about |
| 1330 | ** entries being inserted or deleted during the scan. Cursors should |
| 1331 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 1332 | ** 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] | 1333 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1334 | ** should be opened with wrFlag==1 even if they never really intend |
| 1335 | ** to write. |
| 1336 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 1337 | ** No checking is done to make sure that page iTable really is the |
| 1338 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 1339 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1340 | ** |
| 1341 | ** The comparison function must be logically the same for every cursor |
| 1342 | ** on a particular table. Changing the comparison function will result |
| 1343 | ** in incorrect operations. If the comparison function is NULL, a |
| 1344 | ** default comparison function is used. The comparison function is |
| 1345 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1346 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1347 | int sqlite3BtreeCursor( |
| 1348 | Btree *pBt, /* The btree */ |
| 1349 | int iTable, /* Root page of table to open */ |
| 1350 | int wrFlag, /* 1 to write. 0 read-only */ |
| 1351 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 1352 | void *pArg, /* First arg to xCompare() */ |
| 1353 | BtCursor **ppCur /* Write new cursor here */ |
| 1354 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1355 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1356 | BtCursor *pCur, *pRing; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1357 | |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 1358 | if( pBt->readOnly && wrFlag ){ |
| 1359 | *ppCur = 0; |
| 1360 | return SQLITE_READONLY; |
| 1361 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1362 | if( pBt->pPage1==0 ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1363 | rc = lockBtree(pBt); |
| 1364 | if( rc!=SQLITE_OK ){ |
| 1365 | *ppCur = 0; |
| 1366 | return rc; |
| 1367 | } |
| 1368 | } |
| 1369 | pCur = sqliteMalloc( sizeof(*pCur) ); |
| 1370 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1371 | rc = SQLITE_NOMEM; |
| 1372 | goto create_cursor_exception; |
| 1373 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1374 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1375 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 1376 | rc = SQLITE_EMPTY; |
| 1377 | goto create_cursor_exception; |
| 1378 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1379 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1380 | if( rc!=SQLITE_OK ){ |
| 1381 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1382 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1383 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 1384 | pCur->pArg = pArg; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1385 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1386 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1387 | pCur->idx = 0; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1388 | pCur->infoValid = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1389 | pCur->pNext = pBt->pCursor; |
| 1390 | if( pCur->pNext ){ |
| 1391 | pCur->pNext->pPrev = pCur; |
| 1392 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1393 | pCur->pPrev = 0; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1394 | pRing = pBt->pCursor; |
| 1395 | while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; } |
| 1396 | if( pRing ){ |
| 1397 | pCur->pShared = pRing->pShared; |
| 1398 | pRing->pShared = pCur; |
| 1399 | }else{ |
| 1400 | pCur->pShared = pCur; |
| 1401 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1402 | pBt->pCursor = pCur; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1403 | pCur->isValid = 0; |
| 1404 | pCur->status = SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1405 | *ppCur = pCur; |
| 1406 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1407 | |
| 1408 | create_cursor_exception: |
| 1409 | *ppCur = 0; |
| 1410 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1411 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1412 | sqliteFree(pCur); |
| 1413 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1414 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1415 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1418 | /* |
| 1419 | ** Change the value of the comparison function used by a cursor. |
| 1420 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1421 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 1422 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 1423 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 1424 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 1425 | ){ |
| 1426 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 1427 | pCur->pArg = pArg; |
| 1428 | } |
| 1429 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1430 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1431 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1432 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1433 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1434 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1435 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1436 | if( pCur->pPrev ){ |
| 1437 | pCur->pPrev->pNext = pCur->pNext; |
| 1438 | }else{ |
| 1439 | pBt->pCursor = pCur->pNext; |
| 1440 | } |
| 1441 | if( pCur->pNext ){ |
| 1442 | pCur->pNext->pPrev = pCur->pPrev; |
| 1443 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1444 | releasePage(pCur->pPage); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1445 | if( pCur->pShared!=pCur ){ |
| 1446 | BtCursor *pRing = pCur->pShared; |
| 1447 | while( pRing->pShared!=pCur ){ pRing = pRing->pShared; } |
| 1448 | pRing->pShared = pCur->pShared; |
| 1449 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1450 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1451 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1452 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1455 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1456 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 1457 | ** The temporary cursor is not on the cursor list for the Btree. |
| 1458 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1459 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1460 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 1461 | pTempCur->pNext = 0; |
| 1462 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1463 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1464 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1465 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1469 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1470 | ** function above. |
| 1471 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1472 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1473 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1474 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1475 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1479 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1480 | ** If it is not already valid, call parseCell() to fill it in. |
| 1481 | ** |
| 1482 | ** BtCursor.info is a cache of the information in the current cell. |
| 1483 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1484 | */ |
| 1485 | static void getCellInfo(BtCursor *pCur){ |
| 1486 | MemPage *pPage = pCur->pPage; |
| 1487 | if( !pCur->infoValid ){ |
| 1488 | parseCell(pPage, pPage->aCell[pCur->idx], &pCur->info); |
| 1489 | pCur->infoValid = 1; |
| 1490 | }else{ |
| 1491 | #ifndef NDEBUG |
| 1492 | CellInfo info; |
| 1493 | parseCell(pPage, pPage->aCell[pCur->idx], &info); |
| 1494 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 1495 | #endif |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1500 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 1501 | ** the key for the current entry. If the cursor is not pointing |
| 1502 | ** to a valid entry, *pSize is set to 0. |
| 1503 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1504 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1505 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1506 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1507 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1508 | if( !pCur->isValid ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1509 | *pSize = 0; |
| 1510 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1511 | getCellInfo(pCur); |
| 1512 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1513 | } |
| 1514 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1515 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1516 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1517 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1518 | ** Set *pSize to the number of bytes of data in the entry the |
| 1519 | ** cursor currently points to. Always return SQLITE_OK. |
| 1520 | ** Failure is not possible. If the cursor is not currently |
| 1521 | ** pointing to an entry (which can happen, for example, if |
| 1522 | ** the database is empty) then *pSize is set to 0. |
| 1523 | */ |
| 1524 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1525 | if( !pCur->isValid ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 1526 | /* Not pointing at a valid entry - set *pSize to 0. */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1527 | *pSize = 0; |
| 1528 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1529 | getCellInfo(pCur); |
| 1530 | *pSize = pCur->info.nData; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1531 | } |
| 1532 | return SQLITE_OK; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1536 | ** Read payload information from the entry that the pCur cursor is |
| 1537 | ** pointing to. Begin reading the payload at "offset" and read |
| 1538 | ** a total of "amt" bytes. Put the result in zBuf. |
| 1539 | ** |
| 1540 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1541 | ** It just reads bytes from the payload area. Data might appear |
| 1542 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1543 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1544 | static int getPayload( |
| 1545 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 1546 | int offset, /* Begin reading this far into payload */ |
| 1547 | int amt, /* Read this many bytes */ |
| 1548 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 1549 | int skipKey /* offset begins at data if this is true */ |
| 1550 | ){ |
| 1551 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1552 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1553 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1554 | MemPage *pPage; |
| 1555 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1556 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1557 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1558 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1559 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1560 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1561 | pBt = pCur->pBt; |
| 1562 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1563 | pageIntegrity(pPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1564 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1565 | aPayload = pPage->aCell[pCur->idx]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1566 | getCellInfo(pCur); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1567 | aPayload += pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1568 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1569 | nKey = 0; |
| 1570 | }else{ |
| 1571 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1572 | } |
| 1573 | assert( offset>=0 ); |
| 1574 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1575 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1576 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1577 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1578 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1579 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1580 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1581 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1582 | if( a+offset>pCur->info.nLocal ){ |
| 1583 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1584 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1585 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1586 | if( a==amt ){ |
| 1587 | return SQLITE_OK; |
| 1588 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1589 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1590 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1591 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1592 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1593 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1594 | } |
| 1595 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1596 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1597 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1598 | ovflSize = pBt->usableSize - 4; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1599 | while( amt>0 && nextPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1600 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1601 | if( rc!=0 ){ |
| 1602 | return rc; |
| 1603 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1604 | nextPage = get4byte(aPayload); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1605 | if( offset<ovflSize ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1606 | int a = amt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1607 | if( a + offset > ovflSize ){ |
| 1608 | a = ovflSize - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1609 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 1610 | memcpy(pBuf, &aPayload[offset+4], a); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1611 | offset = 0; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1612 | amt -= a; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1613 | pBuf += a; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1614 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1615 | offset -= ovflSize; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1616 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1617 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1618 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1619 | if( amt>0 ){ |
| 1620 | return SQLITE_CORRUPT; |
| 1621 | } |
| 1622 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1625 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1626 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1627 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1628 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1629 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1630 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1631 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1632 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1633 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1634 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1635 | assert( amt>=0 ); |
| 1636 | assert( offset>=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1637 | if( pCur->isValid==0 ){ |
| 1638 | return pCur->status; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1639 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1640 | assert( pCur->pPage!=0 ); |
| 1641 | assert( pCur->pPage->intKey==0 ); |
| 1642 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1643 | return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 1644 | } |
| 1645 | |
| 1646 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1647 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1648 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1649 | ** begins at "offset". |
| 1650 | ** |
| 1651 | ** Return SQLITE_OK on success or an error code if anything goes |
| 1652 | ** wrong. An error is returned if "offset+amt" is larger than |
| 1653 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1654 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1655 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1656 | if( !pCur->isValid ){ |
| 1657 | return pCur->status ? pCur->status : SQLITE_INTERNAL; |
| 1658 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 1659 | assert( amt>=0 ); |
| 1660 | assert( offset>=0 ); |
| 1661 | assert( pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1662 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1663 | return getPayload(pCur, offset, amt, pBuf, 1); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1666 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1667 | ** Return a pointer to payload information from the entry that the |
| 1668 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 1669 | ** the key if skipKey==0 and it points to the beginning of data if |
| 1670 | ** skipKey==1. |
| 1671 | ** |
| 1672 | ** At least amt bytes of information must be available on the local |
| 1673 | ** page or else this routine returns NULL. If amt<0 then the entire |
| 1674 | ** key/data must be available. |
| 1675 | ** |
| 1676 | ** This routine is an optimization. It is common for the entire key |
| 1677 | ** and data to fit on the local page and for there to be no overflow |
| 1678 | ** pages. When that is so, this routine can be used to access the |
| 1679 | ** key and data without making a copy. If the key and/or data spills |
| 1680 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 1681 | ** the key/data and copy it into a preallocated buffer. |
| 1682 | ** |
| 1683 | ** The pointer returned by this routine looks directly into the cached |
| 1684 | ** page of the database. The data might change or move the next time |
| 1685 | ** any btree routine is called. |
| 1686 | */ |
| 1687 | static const unsigned char *fetchPayload( |
| 1688 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 1689 | int amt, /* Amount requested */ |
| 1690 | int skipKey /* read beginning at data if this is true */ |
| 1691 | ){ |
| 1692 | unsigned char *aPayload; |
| 1693 | MemPage *pPage; |
| 1694 | Btree *pBt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1695 | u32 nKey; |
| 1696 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1697 | |
| 1698 | assert( pCur!=0 && pCur->pPage!=0 ); |
| 1699 | assert( pCur->isValid ); |
| 1700 | pBt = pCur->pBt; |
| 1701 | pPage = pCur->pPage; |
| 1702 | pageIntegrity(pPage); |
| 1703 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1704 | aPayload = pPage->aCell[pCur->idx]; |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 1705 | getCellInfo(pCur); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1706 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1707 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1708 | nKey = 0; |
| 1709 | }else{ |
| 1710 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1711 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1712 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1713 | aPayload += nKey; |
| 1714 | nLocal = pCur->info.nLocal - nKey; |
| 1715 | if( amt<0 ) amt = pCur->info.nData; |
| 1716 | assert( amt<=pCur->info.nData ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1717 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1718 | nLocal = pCur->info.nLocal; |
| 1719 | if( amt<0 ) amt = nKey; |
| 1720 | assert( amt<=nKey ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1721 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1722 | if( amt>nLocal ){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 1723 | return 0; /* If any of the data is not local, return nothing */ |
| 1724 | } |
| 1725 | return aPayload; |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | /* |
| 1730 | ** Return a pointer to the first amt bytes of the key or data |
| 1731 | ** for record that cursor pCur is point to if the entire request |
| 1732 | ** exists in contiguous memory on the main tree page. If any |
| 1733 | ** any part of the request is on an overflow page, return 0. |
| 1734 | ** If pCur is not pointing to a valid entry return 0. |
| 1735 | ** |
| 1736 | ** If amt<0 then return the entire key or data. |
| 1737 | ** |
| 1738 | ** The pointer returned is ephemeral. The key/data may move |
| 1739 | ** or be destroyed on the next call to any Btree routine. |
| 1740 | ** |
| 1741 | ** These routines is used to get quick access to key and data |
| 1742 | ** in the common case where no overflow pages are used. |
| 1743 | ** |
| 1744 | ** It is a fatal error to call these routines with amt values that |
| 1745 | ** are larger than the key/data size. |
| 1746 | */ |
| 1747 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int amt){ |
| 1748 | return (const void*)fetchPayload(pCur, amt, 0); |
| 1749 | } |
| 1750 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int amt){ |
| 1751 | return (const void*)fetchPayload(pCur, amt, 1); |
| 1752 | } |
| 1753 | |
| 1754 | |
| 1755 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1756 | ** Move the cursor down to a new child page. The newPgno argument is the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1757 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1758 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1759 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1760 | int rc; |
| 1761 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1762 | MemPage *pOldPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1763 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1764 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1765 | assert( pCur->isValid ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1766 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1767 | if( rc ) return rc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1768 | pageIntegrity(pNewPage); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1769 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1770 | pOldPage = pCur->pPage; |
| 1771 | pOldPage->idxShift = 0; |
| 1772 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1773 | pCur->pPage = pNewPage; |
| 1774 | pCur->idx = 0; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1775 | pCur->infoValid = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 1776 | if( pNewPage->nCell<1 ){ |
| 1777 | return SQLITE_CORRUPT; |
| 1778 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1779 | return SQLITE_OK; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1783 | ** Return true if the page is the virtual root of its table. |
| 1784 | ** |
| 1785 | ** The virtual root page is the root page for most tables. But |
| 1786 | ** for the table rooted on page 1, sometime the real root page |
| 1787 | ** is empty except for the right-pointer. In such cases the |
| 1788 | ** virtual root page is the page that the right-pointer of page |
| 1789 | ** 1 is pointing to. |
| 1790 | */ |
| 1791 | static int isRootPage(MemPage *pPage){ |
| 1792 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1793 | if( pParent==0 ) return 1; |
| 1794 | if( pParent->pgno>1 ) return 0; |
| 1795 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1800 | ** Move the cursor up to the parent page. |
| 1801 | ** |
| 1802 | ** pCur->idx is set to the cell index that contains the pointer |
| 1803 | ** to the page we are coming from. If we are coming from the |
| 1804 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1805 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1806 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1807 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1808 | Pgno oldPgno; |
| 1809 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1810 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1811 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1812 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1813 | assert( pCur->isValid ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1814 | pPage = pCur->pPage; |
| 1815 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1816 | assert( !isRootPage(pPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1817 | pageIntegrity(pPage); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1818 | pParent = pPage->pParent; |
| 1819 | assert( pParent!=0 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1820 | pageIntegrity(pParent); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1821 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1822 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1823 | oldPgno = pPage->pgno; |
| 1824 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1825 | pCur->pPage = pParent; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1826 | pCur->infoValid = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1827 | assert( pParent->idxShift==0 ); |
| 1828 | if( pParent->idxShift==0 ){ |
| 1829 | pCur->idx = idxParent; |
| 1830 | #ifndef NDEBUG |
| 1831 | /* Verify that pCur->idx is the correct index to point back to the child |
| 1832 | ** page we just came from |
| 1833 | */ |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1834 | if( pCur->idx<pParent->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1835 | assert( get4byte(&pParent->aCell[idxParent][2])==oldPgno ); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1836 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1837 | assert( get4byte(&pParent->aData[pParent->hdrOffset+6])==oldPgno ); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1838 | } |
| 1839 | #endif |
| 1840 | }else{ |
| 1841 | /* The MemPage.idxShift flag indicates that cell indices might have |
| 1842 | ** changed since idxParent was set and hence idxParent might be out |
| 1843 | ** of date. So recompute the parent cell index by scanning all cells |
| 1844 | ** and locating the one that points to the child we just came from. |
| 1845 | */ |
| 1846 | int i; |
| 1847 | pCur->idx = pParent->nCell; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1848 | for(i=0; i<pParent->nCell; i++){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1849 | if( get4byte(&pParent->aCell[i][2])==oldPgno ){ |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 1850 | pCur->idx = i; |
| 1851 | break; |
| 1852 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | ** Move the cursor to the root page |
| 1859 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1860 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1861 | MemPage *pRoot; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1862 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1863 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1864 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1865 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1866 | if( rc ){ |
| 1867 | pCur->isValid = 0; |
| 1868 | return rc; |
| 1869 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1870 | releasePage(pCur->pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1871 | pageIntegrity(pRoot); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1872 | pCur->pPage = pRoot; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1873 | pCur->idx = 0; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1874 | pCur->infoValid = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1875 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 1876 | Pgno subpage; |
| 1877 | assert( pRoot->pgno==1 ); |
| 1878 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+6]); |
| 1879 | assert( subpage>0 ); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 1880 | pCur->isValid = 1; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1881 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1882 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1883 | pCur->isValid = pCur->pPage->nCell>0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 1884 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1885 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1886 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1887 | /* |
| 1888 | ** Move the cursor down to the left-most leaf entry beneath the |
| 1889 | ** entry to which it is currently pointing. |
| 1890 | */ |
| 1891 | static int moveToLeftmost(BtCursor *pCur){ |
| 1892 | Pgno pgno; |
| 1893 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1894 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1895 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1896 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1897 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1898 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
| 1899 | pgno = get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1900 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1901 | if( rc ) return rc; |
| 1902 | } |
| 1903 | return SQLITE_OK; |
| 1904 | } |
| 1905 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1906 | /* |
| 1907 | ** Move the cursor down to the right-most leaf entry beneath the |
| 1908 | ** page to which it is currently pointing. Notice the difference |
| 1909 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 1910 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 1911 | ** finds the right-most entry beneath the *page*. |
| 1912 | */ |
| 1913 | static int moveToRightmost(BtCursor *pCur){ |
| 1914 | Pgno pgno; |
| 1915 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1916 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1917 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1918 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1919 | while( !(pPage = pCur->pPage)->leaf ){ |
| 1920 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 1921 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 1922 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1923 | if( rc ) return rc; |
| 1924 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1925 | pCur->idx = pPage->nCell - 1; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 1926 | pCur->infoValid = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1927 | return SQLITE_OK; |
| 1928 | } |
| 1929 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1930 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 1931 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1932 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1933 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1934 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1935 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1936 | if( pCur->status ){ |
| 1937 | return pCur->status; |
| 1938 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1939 | rc = moveToRoot(pCur); |
| 1940 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1941 | if( pCur->isValid==0 ){ |
| 1942 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1943 | *pRes = 1; |
| 1944 | return SQLITE_OK; |
| 1945 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1946 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1947 | *pRes = 0; |
| 1948 | rc = moveToLeftmost(pCur); |
| 1949 | return rc; |
| 1950 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1951 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1952 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 1953 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1954 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1955 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1956 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1957 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1958 | if( pCur->status ){ |
| 1959 | return pCur->status; |
| 1960 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1961 | rc = moveToRoot(pCur); |
| 1962 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1963 | if( pCur->isValid==0 ){ |
| 1964 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1965 | *pRes = 1; |
| 1966 | return SQLITE_OK; |
| 1967 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1968 | assert( pCur->isValid ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1969 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 1970 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1971 | return rc; |
| 1972 | } |
| 1973 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1974 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1975 | ** Return a success code. |
| 1976 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1977 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 1978 | ** ignored. For other tables, nKey is the number of bytes of data |
| 1979 | ** in nKey. The comparison function specified when the cursor was |
| 1980 | ** created is used to compare keys. |
| 1981 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1982 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1983 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1984 | ** were present. The cursor might point to an entry that comes |
| 1985 | ** before or after the key. |
| 1986 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1987 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 1988 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1989 | ** this value is as follows: |
| 1990 | ** |
| 1991 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 1992 | ** is smaller than pKey or if the table is empty |
| 1993 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1994 | ** |
| 1995 | ** *pRes==0 The cursor is left pointing at an entry that |
| 1996 | ** exactly matches pKey. |
| 1997 | ** |
| 1998 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1999 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2000 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2001 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2002 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2003 | |
| 2004 | if( pCur->status ){ |
| 2005 | return pCur->status; |
| 2006 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2007 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2008 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2009 | assert( pCur->pPage ); |
| 2010 | assert( pCur->pPage->isInit ); |
| 2011 | if( pCur->isValid==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2012 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2013 | assert( pCur->pPage->nCell==0 ); |
| 2014 | return SQLITE_OK; |
| 2015 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2016 | for(;;){ |
| 2017 | int lwr, upr; |
| 2018 | Pgno chldPg; |
| 2019 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2020 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2021 | lwr = 0; |
| 2022 | upr = pPage->nCell-1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2023 | pageIntegrity(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2024 | while( lwr<=upr ){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2025 | const void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2026 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2027 | pCur->idx = (lwr+upr)/2; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2028 | pCur->infoValid = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2029 | sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2030 | if( pPage->intKey ){ |
| 2031 | if( nCellKey<nKey ){ |
| 2032 | c = -1; |
| 2033 | }else if( nCellKey>nKey ){ |
| 2034 | c = +1; |
| 2035 | }else{ |
| 2036 | c = 0; |
| 2037 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2038 | }else if( (pCellKey = sqlite3BtreeKeyFetch(pCur, nCellKey))!=0 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2039 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2040 | }else{ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2041 | u8 *pCellKey = sqliteMalloc( nCellKey ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2042 | if( pCellKey==0 ) return SQLITE_NOMEM; |
| 2043 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey); |
| 2044 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2045 | sqliteFree(pCellKey); |
| 2046 | if( rc ) return rc; |
| 2047 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2048 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2049 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 2050 | lwr = pCur->idx; |
| 2051 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2054 | if( pRes ) *pRes = 0; |
| 2055 | return SQLITE_OK; |
| 2056 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2057 | } |
| 2058 | if( c<0 ){ |
| 2059 | lwr = pCur->idx+1; |
| 2060 | }else{ |
| 2061 | upr = pCur->idx-1; |
| 2062 | } |
| 2063 | } |
| 2064 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2065 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2066 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2067 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2068 | }else if( lwr>=pPage->nCell ){ |
| 2069 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2070 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2071 | chldPg = get4byte(&pPage->aCell[lwr][2]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2072 | } |
| 2073 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2074 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2075 | if( pRes ) *pRes = c; |
| 2076 | return SQLITE_OK; |
| 2077 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2078 | pCur->idx = lwr; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2079 | pCur->infoValid = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2080 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2081 | if( rc ){ |
| 2082 | return rc; |
| 2083 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2084 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2085 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2089 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 2090 | ** |
| 2091 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 2092 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 2093 | ** the first entry. TRUE is also returned if the table is empty. |
| 2094 | */ |
| 2095 | int sqlite3BtreeEof(BtCursor *pCur){ |
| 2096 | return pCur->isValid==0; |
| 2097 | } |
| 2098 | |
| 2099 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2100 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2101 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2102 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2103 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2104 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2105 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2106 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2107 | MemPage *pPage = pCur->pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2108 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2109 | assert( pRes!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2110 | if( pCur->isValid==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2111 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2112 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2113 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2114 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2115 | assert( pCur->idx<pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2116 | pCur->idx++; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2117 | pCur->infoValid = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2118 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2119 | if( !pPage->leaf ){ |
| 2120 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+6])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2121 | if( rc ) return rc; |
| 2122 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2123 | *pRes = 0; |
| 2124 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2125 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2126 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2127 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2128 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2129 | pCur->isValid = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2130 | return SQLITE_OK; |
| 2131 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2132 | moveToParent(pCur); |
| 2133 | pPage = pCur->pPage; |
| 2134 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2135 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2136 | if( pPage->leafData ){ |
| 2137 | rc = sqlite3BtreeNext(pCur, pRes); |
| 2138 | }else{ |
| 2139 | rc = SQLITE_OK; |
| 2140 | } |
| 2141 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2142 | } |
| 2143 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2144 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2145 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2146 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2147 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2148 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2151 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2152 | ** 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] | 2153 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2154 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2155 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2156 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2157 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2158 | int rc; |
| 2159 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2160 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2161 | if( pCur->isValid==0 ){ |
| 2162 | *pRes = 1; |
| 2163 | return SQLITE_OK; |
| 2164 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2165 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2166 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2167 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2168 | if( !pPage->leaf ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2169 | pgno = get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2170 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2171 | if( rc ) return rc; |
| 2172 | rc = moveToRightmost(pCur); |
| 2173 | }else{ |
| 2174 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2175 | if( isRootPage(pPage) ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2176 | pCur->isValid = 0; |
| 2177 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2178 | return SQLITE_OK; |
| 2179 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2180 | moveToParent(pCur); |
| 2181 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2182 | } |
| 2183 | pCur->idx--; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2184 | pCur->infoValid = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2185 | if( pPage->leafData ){ |
| 2186 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 2187 | }else{ |
| 2188 | rc = SQLITE_OK; |
| 2189 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2190 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2191 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2192 | return rc; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2196 | ** The TRACE macro will print high-level status information about the |
| 2197 | ** btree operation when the global variable sqlite3_btree_trace is |
| 2198 | ** enabled. |
| 2199 | */ |
| 2200 | #if SQLITE_TEST |
| 2201 | # define TRACE(X) if( sqlite3_btree_trace ){ printf X; fflush(stdout); } |
| 2202 | #else |
| 2203 | # define TRACE(X) |
| 2204 | #endif |
| 2205 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
| 2206 | |
| 2207 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2208 | ** Allocate a new page from the database file. |
| 2209 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2210 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2211 | ** has already been called on the new page.) The new page has also |
| 2212 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2213 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2214 | ** |
| 2215 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 2216 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2217 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2218 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2219 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 2220 | ** 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] | 2221 | ** attempt to keep related pages close to each other in the database file, |
| 2222 | ** which in turn can make database access faster. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2223 | */ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2224 | static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2225 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2226 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2227 | int n; /* Number of pages on the freelist */ |
| 2228 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2229 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2230 | pPage1 = pBt->pPage1; |
| 2231 | n = get4byte(&pPage1->aData[36]); |
| 2232 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2233 | /* There are pages on the freelist. Reuse one of those pages. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2234 | MemPage *pTrunk; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2235 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2236 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2237 | put4byte(&pPage1->aData[36], n-1); |
| 2238 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2239 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2240 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2241 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2242 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2243 | return rc; |
| 2244 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2245 | k = get4byte(&pTrunk->aData[4]); |
| 2246 | if( k==0 ){ |
| 2247 | /* The trunk has no leaves. So extract the trunk page itself and |
| 2248 | ** use it as the newly allocated page */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2249 | *pPgno = get4byte(&pPage1->aData[32]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2250 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 2251 | *ppPage = pTrunk; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2252 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2253 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2254 | /* Extract a leaf from the trunk */ |
| 2255 | int closest; |
| 2256 | unsigned char *aData = pTrunk->aData; |
| 2257 | if( nearby>0 ){ |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2258 | int i, dist; |
| 2259 | closest = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2260 | dist = get4byte(&aData[8]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2261 | if( dist<0 ) dist = -dist; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2262 | for(i=1; i<k; i++){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2263 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2264 | if( d2<0 ) d2 = -d2; |
| 2265 | if( d2<dist ) closest = i; |
| 2266 | } |
| 2267 | }else{ |
| 2268 | closest = 0; |
| 2269 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2270 | *pPgno = get4byte(&aData[8+closest*4]); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2271 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d: %d more free pages\n", |
| 2272 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2273 | if( closest<k-1 ){ |
| 2274 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 2275 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2276 | put4byte(&aData[4], k-1); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2277 | rc = getPage(pBt, *pPgno, ppPage); |
| 2278 | releasePage(pTrunk); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2279 | if( rc==SQLITE_OK ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2280 | sqlite3pager_dont_rollback((*ppPage)->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2281 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2282 | } |
| 2283 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2284 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2285 | /* There are no pages on the freelist, so create a new page at the |
| 2286 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2287 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2288 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2289 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2290 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2291 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2292 | } |
| 2293 | return rc; |
| 2294 | } |
| 2295 | |
| 2296 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2297 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2298 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2299 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2300 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2301 | static int freePage(MemPage *pPage){ |
| 2302 | Btree *pBt = pPage->pBt; |
| 2303 | MemPage *pPage1 = pBt->pPage1; |
| 2304 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2305 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2306 | /* Prepare the page for freeing */ |
| 2307 | assert( pPage->pgno>1 ); |
| 2308 | pPage->isInit = 0; |
| 2309 | releasePage(pPage->pParent); |
| 2310 | pPage->pParent = 0; |
| 2311 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2312 | /* Increment the free page count on pPage1 */ |
| 2313 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2314 | if( rc ) return rc; |
| 2315 | n = get4byte(&pPage1->aData[36]); |
| 2316 | put4byte(&pPage1->aData[36], n+1); |
| 2317 | |
| 2318 | if( n==0 ){ |
| 2319 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2320 | rc = sqlite3pager_write(pPage->aData); |
| 2321 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2322 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2323 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2324 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2325 | }else{ |
| 2326 | /* Other free pages already exist. Retrive the first trunk page |
| 2327 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2328 | MemPage *pTrunk; |
| 2329 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2330 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2331 | k = get4byte(&pTrunk->aData[4]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2332 | if( k==pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2333 | /* The trunk is full. Turn the page being freed into a new |
| 2334 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2335 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2336 | if( rc ) return rc; |
| 2337 | put4byte(pPage->aData, pTrunk->pgno); |
| 2338 | put4byte(&pPage->aData[4], 0); |
| 2339 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2340 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 2341 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2342 | }else{ |
| 2343 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2344 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2345 | if( rc ) return rc; |
| 2346 | put4byte(&pTrunk->aData[4], k+1); |
| 2347 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2348 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2349 | TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2350 | } |
| 2351 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2352 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2353 | return rc; |
| 2354 | } |
| 2355 | |
| 2356 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2357 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2358 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2359 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 2360 | Btree *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2361 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2362 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2363 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2364 | |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2365 | parseCell(pPage, pCell, &info); |
| 2366 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2367 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2368 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2369 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2370 | while( ovflPgno!=0 ){ |
| 2371 | MemPage *pOvfl; |
| 2372 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2373 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2374 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2375 | rc = freePage(pOvfl); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2376 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2377 | sqlite3pager_unref(pOvfl->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2378 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2379 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2383 | ** Create the byte sequence used to represent a cell on page pPage |
| 2384 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 2385 | ** allocated and filled in as necessary. The calling procedure |
| 2386 | ** is responsible for making sure sufficient space has been allocated |
| 2387 | ** for pCell[]. |
| 2388 | ** |
| 2389 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 2390 | ** area. pCell might point to some temporary storage. The cell will |
| 2391 | ** be constructed in this temporary area then copied into pPage->aData |
| 2392 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2393 | */ |
| 2394 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2395 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2396 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2397 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2398 | const void *pData,int nData, /* The data */ |
| 2399 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2400 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2401 | int nPayload; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2402 | const void *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2403 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2404 | int spaceLeft; |
| 2405 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2406 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2407 | unsigned char *pPrior; |
| 2408 | unsigned char *pPayload; |
| 2409 | Btree *pBt = pPage->pBt; |
| 2410 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2411 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2412 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2413 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2414 | /* Fill in the header. */ |
| 2415 | nHeader = 2; |
| 2416 | if( !pPage->leaf ){ |
| 2417 | nHeader += 4; |
| 2418 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2419 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2420 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2421 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2422 | nData = 0; |
| 2423 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2424 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
| 2425 | parseCell(pPage, pCell, &info); |
| 2426 | assert( info.nHeader==nHeader ); |
| 2427 | assert( info.nKey==nKey ); |
| 2428 | assert( info.nData==nData ); |
| 2429 | |
| 2430 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2431 | nPayload = nData; |
| 2432 | if( pPage->intKey ){ |
| 2433 | pSrc = pData; |
| 2434 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2435 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2436 | }else{ |
| 2437 | nPayload += nKey; |
| 2438 | pSrc = pKey; |
| 2439 | nSrc = nKey; |
| 2440 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2441 | *pnSize = info.nSize; |
| 2442 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2443 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2444 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2445 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2446 | while( nPayload>0 ){ |
| 2447 | if( spaceLeft==0 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2448 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2449 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2450 | releasePage(pToRelease); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2451 | clearCell(pPage, pCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2452 | return rc; |
| 2453 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2454 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2455 | releasePage(pToRelease); |
| 2456 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2457 | pPrior = pOvfl->aData; |
| 2458 | put4byte(pPrior, 0); |
| 2459 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2460 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2461 | } |
| 2462 | n = nPayload; |
| 2463 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2464 | if( n>nSrc ) n = nSrc; |
| 2465 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2466 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2467 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2468 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2469 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2470 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2471 | if( nSrc==0 ){ |
| 2472 | nSrc = nData; |
| 2473 | pSrc = pData; |
| 2474 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2475 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 2476 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2477 | return SQLITE_OK; |
| 2478 | } |
| 2479 | |
| 2480 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2481 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2482 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2483 | ** pointer in the third argument. |
| 2484 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2485 | static void reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2486 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2487 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2488 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2489 | if( pgno==0 ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2490 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2491 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2492 | if( aData ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2493 | pThis = (MemPage*)&aData[pBt->usableSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2494 | if( pThis->isInit ){ |
| 2495 | if( pThis->pParent!=pNewParent ){ |
| 2496 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 2497 | pThis->pParent = pNewParent; |
| 2498 | if( pNewParent ) sqlite3pager_ref(pNewParent->aData); |
| 2499 | } |
| 2500 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2501 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2502 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2507 | ** Change the pParent pointer of all children of pPage to point back |
| 2508 | ** to pPage. |
| 2509 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2510 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2511 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2512 | ** |
| 2513 | ** This routine gets called after you memcpy() one page into |
| 2514 | ** another. |
| 2515 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2516 | static void reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2517 | int i; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2518 | Btree *pBt; |
| 2519 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2520 | if( pPage->leaf ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2521 | pBt = pPage->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2522 | for(i=0; i<pPage->nCell; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2523 | reparentPage(pBt, get4byte(&pPage->aCell[i][2]), pPage, i); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2524 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2525 | reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+6]), pPage, i); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2526 | pPage->idxShift = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | /* |
| 2530 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 2531 | ** The cell content is not freed or deallocated. It is assumed that |
| 2532 | ** the cell content has been copied someplace else. This routine just |
| 2533 | ** removes the reference to the cell from pPage. |
| 2534 | ** |
| 2535 | ** "sz" must be the number of bytes in the cell. |
| 2536 | ** |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2537 | ** Try to maintain the integrity of the linked list of cells. But if |
| 2538 | ** the cell being inserted does not fit on the page, this will not be |
| 2539 | ** possible. If the linked list is not maintained, then just update |
| 2540 | ** pPage->aCell[] and set the pPage->needRelink flag so that we will |
| 2541 | ** know to rebuild the linked list later. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2542 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2543 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2544 | int j, pc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2545 | u8 *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2546 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2547 | assert( sz==cellSize(pPage, pPage->aCell[idx]) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2548 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2549 | assert( pPage->aCell[idx]>=pPage->aData ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2550 | assert( pPage->aCell[idx]<=&pPage->aData[pPage->pBt->usableSize-sz] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2551 | data = pPage->aData; |
| 2552 | pc = Addr(pPage->aCell[idx]) - Addr(data); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2553 | assert( pc>pPage->hdrOffset && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2554 | freeSpace(pPage, pc, sz); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2555 | for(j=idx; j<pPage->nCell-1; j++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2556 | pPage->aCell[j] = pPage->aCell[j+1]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2557 | } |
| 2558 | pPage->nCell--; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2559 | if( !pPage->isOverfull && !pPage->needRelink ){ |
| 2560 | u8 *pPrev; |
| 2561 | if( idx==0 ){ |
| 2562 | pPrev = &data[pPage->hdrOffset+3]; |
| 2563 | }else{ |
| 2564 | pPrev = pPage->aCell[idx-1]; |
| 2565 | } |
| 2566 | if( idx<pPage->nCell ){ |
| 2567 | pc = Addr(pPage->aCell[idx]) - Addr(data); |
| 2568 | }else{ |
| 2569 | pc = 0; |
| 2570 | } |
| 2571 | put2byte(pPrev, pc); |
| 2572 | pageIntegrity(pPage); |
| 2573 | }else{ |
| 2574 | pPage->needRelink = 1; |
| 2575 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2576 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | /* |
| 2580 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 2581 | ** content of the cell. |
| 2582 | ** |
| 2583 | ** If the cell content will fit on the page, then put it there. If it |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2584 | ** will not fit and pTemp is not NULL, then make a copy of the content |
| 2585 | ** into pTemp, set pPage->aCell[i] point to pTemp, and set pPage->isOverfull. |
| 2586 | ** If the content will not fit and pTemp is NULL, then make pPage->aCell[i] |
| 2587 | ** point to pCell and set pPage->isOverfull. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2588 | ** |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2589 | ** Try to maintain the integrity of the linked list of cells. But if |
| 2590 | ** the cell being inserted does not fit on the page, this will not be |
| 2591 | ** possible. If the linked list is not maintained, then just update |
| 2592 | ** pPage->aCell[] and set the pPage->needRelink flag so that we will |
| 2593 | ** know to rebuild the linked list later. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2594 | */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2595 | static void insertCell( |
| 2596 | MemPage *pPage, /* Page into which we are copying */ |
| 2597 | int i, /* Which cell on pPage to insert after */ |
| 2598 | u8 *pCell, /* Text of the new cell to insert */ |
| 2599 | int sz, /* Bytes of data in pCell */ |
| 2600 | u8 *pTemp /* Temp storage space for pCell, if needed */ |
| 2601 | ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2602 | int idx, j; |
| 2603 | assert( i>=0 && i<=pPage->nCell ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2604 | assert( sz==cellSize(pPage, pCell) ); |
| 2605 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2606 | idx = pPage->needRelink ? 0 : allocateSpace(pPage, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2607 | resizeCellArray(pPage, pPage->nCell+1); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2608 | for(j=pPage->nCell; j>i; j--){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2609 | pPage->aCell[j] = pPage->aCell[j-1]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2610 | } |
| 2611 | pPage->nCell++; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2612 | if( idx<=0 ){ |
| 2613 | pPage->isOverfull = 1; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2614 | if( pTemp ){ |
| 2615 | memcpy(pTemp, pCell, sz); |
| 2616 | }else{ |
| 2617 | pTemp = pCell; |
| 2618 | } |
| 2619 | pPage->aCell[i] = pTemp; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2620 | }else{ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2621 | u8 *data = pPage->aData; |
| 2622 | memcpy(&data[idx], pCell, sz); |
| 2623 | pPage->aCell[i] = &data[idx]; |
| 2624 | } |
| 2625 | if( !pPage->isOverfull && !pPage->needRelink ){ |
| 2626 | u8 *pPrev; |
| 2627 | int pc; |
| 2628 | if( i==0 ){ |
| 2629 | pPrev = &pPage->aData[pPage->hdrOffset+3]; |
| 2630 | }else{ |
| 2631 | pPrev = pPage->aCell[i-1]; |
| 2632 | } |
| 2633 | pc = get2byte(pPrev); |
| 2634 | put2byte(pPrev, idx); |
| 2635 | put2byte(pPage->aCell[i], pc); |
| 2636 | pageIntegrity(pPage); |
| 2637 | }else{ |
| 2638 | pPage->needRelink = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2639 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2640 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2644 | ** Add a list of cells to a page. The page should be initially empty. |
| 2645 | ** The cells are guaranteed to fit on the page. |
| 2646 | */ |
| 2647 | static void assemblePage( |
| 2648 | MemPage *pPage, /* The page to be assemblied */ |
| 2649 | int nCell, /* The number of cells to add to this page */ |
| 2650 | u8 **apCell, /* Pointers to cell text */ |
| 2651 | int *aSize /* Sizes of the cells */ |
| 2652 | ){ |
| 2653 | int i; /* Loop counter */ |
| 2654 | int totalSize; /* Total size of all cells */ |
| 2655 | int hdr; /* Index of page header */ |
| 2656 | int pc, prevpc; /* Addresses of cells being inserted */ |
| 2657 | u8 *data; /* Data for the page */ |
| 2658 | |
| 2659 | assert( pPage->needRelink==0 ); |
| 2660 | assert( pPage->isOverfull==0 ); |
| 2661 | totalSize = 0; |
| 2662 | for(i=0; i<nCell; i++){ |
| 2663 | totalSize += aSize[i]; |
| 2664 | } |
| 2665 | assert( totalSize<=pPage->nFree ); |
| 2666 | assert( pPage->nCell==0 ); |
| 2667 | resizeCellArray(pPage, nCell); |
| 2668 | pc = allocateSpace(pPage, totalSize); |
| 2669 | data = pPage->aData; |
| 2670 | hdr = pPage->hdrOffset; |
| 2671 | prevpc = hdr+3; |
| 2672 | for(i=0; i<nCell; i++){ |
| 2673 | memcpy(data+pc, apCell[i], aSize[i]); |
| 2674 | put2byte(data+prevpc, pc); |
| 2675 | pPage->aCell[i] = data+pc; |
| 2676 | prevpc = pc; |
| 2677 | pc += aSize[i]; |
| 2678 | assert( pc<=pPage->pBt->usableSize ); |
| 2679 | } |
| 2680 | pPage->nCell = nCell; |
| 2681 | put2byte(data+prevpc, 0); |
| 2682 | } |
| 2683 | |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2684 | #if 0 /* Never Used */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2685 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2686 | ** Rebuild the linked list of cells on a page so that the cells |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2687 | ** occur in the order specified by the pPage->aCell[] array. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2688 | ** Invoke this routine once to repair damage after one or more |
| 2689 | ** invocations of either insertCell() or dropCell(). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2690 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2691 | static void relinkCellList(MemPage *pPage){ |
| 2692 | int i, idxFrom; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2693 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2694 | if( !pPage->needRelink ) return; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2695 | idxFrom = pPage->hdrOffset+3; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2696 | for(i=0; i<pPage->nCell; i++){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2697 | int idx = Addr(pPage->aCell[i]) - Addr(pPage->aData); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2698 | assert( idx>pPage->hdrOffset && idx<pPage->pBt->usableSize ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2699 | put2byte(&pPage->aData[idxFrom], idx); |
| 2700 | idxFrom = idx; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2701 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2702 | put2byte(&pPage->aData[idxFrom], 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2703 | pPage->needRelink = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2704 | } |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2705 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2706 | |
| 2707 | /* |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2708 | ** GCC does not define the offsetof() macro so we'll have to do it |
| 2709 | ** ourselves. |
| 2710 | */ |
| 2711 | #ifndef offsetof |
| 2712 | #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) |
| 2713 | #endif |
| 2714 | |
| 2715 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2716 | ** 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] | 2717 | ** pointers that point into pFrom->aData[] must be adjusted to point |
| 2718 | ** into pTo->aData[] instead. But some pFrom->aCell[] entries might |
| 2719 | ** not point to pFrom->aData[]. Those are unchanged. |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2720 | ** |
| 2721 | ** Over this operation completes, the meta data for pFrom is zeroed. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2722 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2723 | static void movePage(MemPage *pTo, MemPage *pFrom){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2724 | uptr from, to; |
| 2725 | int i; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2726 | int usableSize; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2727 | int ofst; |
| 2728 | |
| 2729 | assert( pTo->hdrOffset==0 ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2730 | assert( pFrom->isInit ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2731 | ofst = pFrom->hdrOffset; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2732 | usableSize = pFrom->pBt->usableSize; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2733 | sqliteFree(pTo->aCell); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2734 | memcpy(pTo->aData, &pFrom->aData[ofst], usableSize - ofst); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2735 | memcpy(pTo, pFrom, offsetof(MemPage, aData)); |
| 2736 | pFrom->isInit = 0; |
| 2737 | pFrom->aCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2738 | assert( pTo->aData[5]<155 ); |
| 2739 | pTo->aData[5] += ofst; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2740 | pTo->isOverfull = pFrom->isOverfull; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2741 | to = Addr(pTo->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2742 | from = Addr(&pFrom->aData[ofst]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2743 | for(i=0; i<pTo->nCell; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2744 | uptr x = Addr(pTo->aCell[i]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2745 | if( x>from && x<from+usableSize-ofst ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2746 | *((uptr*)&pTo->aCell[i]) = x + to - from; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2747 | } |
| 2748 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2752 | ** The following parameters determine how many adjacent pages get involved |
| 2753 | ** in a balancing operation. NN is the number of neighbors on either side |
| 2754 | ** of the page that participate in the balancing operation. NB is the |
| 2755 | ** total number of pages that participate, including the target page and |
| 2756 | ** NN neighbors on either side. |
| 2757 | ** |
| 2758 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 2759 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 2760 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 2761 | ** The value of NN appears to give the best results overall. |
| 2762 | */ |
| 2763 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 2764 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 2765 | |
| 2766 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2767 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2768 | ** 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] | 2769 | ** Usually one sibling on either side of pPage is used in the balancing, |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2770 | ** though both siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2771 | ** or last child of its parent. If pPage has fewer than 2*NN siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2772 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2773 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2774 | ** |
| 2775 | ** The number of siblings of pPage might be increased or decreased by |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2776 | ** one in an effort to keep pages nearly full but not over full. The root page |
| 2777 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2778 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2779 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2780 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2781 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2782 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2783 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2784 | ** 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] | 2785 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2786 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2787 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 2788 | ** might become overfull or underfull. If that happens, then this routine |
| 2789 | ** is called recursively on the parent. |
| 2790 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2791 | ** If this routine fails for any reason, it might leave the database |
| 2792 | ** in a corrupted state. So if this routine fails, the database should |
| 2793 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2794 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2795 | static int balance(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2796 | MemPage *pParent; /* The parent of pPage */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2797 | Btree *pBt; /* The whole database */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2798 | int nCell; /* Number of cells in aCell[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2799 | int nOld; /* Number of pages in apOld[] */ |
| 2800 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2801 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2802 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2803 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 2804 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2805 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2806 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2807 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2808 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 2809 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2810 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2811 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2812 | MemPage *extraUnref = 0; /* Unref this page if not zero */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2813 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 2814 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2815 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2816 | MemPage *apNew[NB+1]; /* pPage and up to NB siblings after balancing */ |
| 2817 | Pgno pgnoNew[NB+1]; /* Page numbers for each page in apNew[] */ |
| 2818 | int idxDiv[NB]; /* Indices of divider cells in pParent */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2819 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2820 | int cntNew[NB+1]; /* Index in aCell[] of cell after i-th page */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2821 | int szNew[NB+1]; /* Combined size of cells place on i-th page */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2822 | u8 *apCell[(MX_CELL+2)*NB]; /* All cells from pages being balanced */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2823 | int szCell[(MX_CELL+2)*NB]; /* Local size of all cells */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2824 | u8 aCopy[NB][MX_PAGE_SIZE+sizeof(MemPage)]; /* Space for apCopy[] */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2825 | u8 aSpace[MX_PAGE_SIZE*4]; /* Space to copies of divider cells */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2826 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2827 | /* |
| 2828 | ** Return without doing any work if pPage is neither overfull nor |
| 2829 | ** underfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2830 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2831 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2832 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2833 | pBt = pPage->pBt; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 2834 | if( !pPage->isOverfull && pPage->nFree<pBt->usableSize*2/3 |
| 2835 | && pPage->nCell>=2){ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2836 | assert( pPage->needRelink==0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2837 | return SQLITE_OK; |
| 2838 | } |
| 2839 | |
| 2840 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2841 | ** Find the parent of the page to be balanced. If there is no parent, |
| 2842 | ** it means this page is the root page and special rules apply. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2843 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2844 | pParent = pPage->pParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2845 | if( pParent==0 ){ |
| 2846 | Pgno pgnoChild; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2847 | MemPage *pChild; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2848 | assert( pPage->isInit ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2849 | if( pPage->nCell==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2850 | if( pPage->leaf ){ |
| 2851 | /* The table is completely empty */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2852 | assert( pPage->needRelink==0 ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2853 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2854 | }else{ |
| 2855 | /* The root page is empty but has one child. Transfer the |
| 2856 | ** information from that one child into the root page if it |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2857 | ** will fit. This reduces the depth of the tree by one. |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2858 | ** |
| 2859 | ** If the root page is page 1, it has less space available than |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2860 | ** its child (due to the 100 byte header that occurs at the beginning |
| 2861 | ** of the database fle), so it might not be able to hold all of the |
| 2862 | ** information currently contained in the child. If this is the |
| 2863 | ** case, then do not do the transfer. Leave page 1 empty except |
| 2864 | ** for the right-pointer to the child page. The child page becomes |
| 2865 | ** the virtual root of the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2866 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2867 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 2868 | assert( pgnoChild>0 && pgnoChild<=sqlite3pager_pagecount(pBt->pPager) ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2869 | rc = getPage(pBt, pgnoChild, &pChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2870 | if( rc ) return rc; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2871 | if( pPage->pgno==1 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2872 | rc = initPage(pChild, pPage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2873 | if( rc ) return rc; |
| 2874 | if( pChild->nFree>=100 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2875 | /* The child information will fit on the root page, so do the |
| 2876 | ** copy */ |
| 2877 | zeroPage(pPage, pChild->aData[0]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2878 | for(i=0; i<pChild->nCell; i++){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2879 | szCell[i] = cellSize(pChild, pChild->aCell[i]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2880 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2881 | assemblePage(pPage, pChild->nCell, pChild->aCell, szCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2882 | freePage(pChild); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2883 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2884 | }else{ |
| 2885 | /* The child has more information that will fit on the root. |
| 2886 | ** The tree is already balanced. Do nothing. */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2887 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2888 | } |
| 2889 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 2890 | memcpy(pPage->aData, pChild->aData, pBt->usableSize); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2891 | pPage->isInit = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2892 | pPage->pParent = 0; |
| 2893 | rc = initPage(pPage, 0); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2894 | assert( rc==SQLITE_OK ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2895 | freePage(pChild); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2896 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 2897 | pChild->pgno, pPage->pgno)); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2898 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2899 | reparentChildPages(pPage); |
| 2900 | releasePage(pChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2901 | } |
| 2902 | return SQLITE_OK; |
| 2903 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2904 | if( !pPage->isOverfull ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2905 | /* It is OK for the root page to be less than half full. |
| 2906 | */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 2907 | assert( pPage->needRelink==0 ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2908 | TRACE(("BALANCE: root page %d is low - no changes\n", pPage->pgno)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2909 | return SQLITE_OK; |
| 2910 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2911 | /* |
| 2912 | ** If we get to here, it means the root page is overfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2913 | ** When this happens, Create a new child page and copy the |
| 2914 | ** contents of the root into the child. Then make the root |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2915 | ** page an empty page with rightChild pointing to the new |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2916 | ** child. Then fall thru to the code below which will cause |
| 2917 | ** the overfull child page to be split. |
| 2918 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2919 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2920 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2921 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2922 | movePage(pChild, pPage); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2923 | assert( pChild->aData[0]==pPage->aData[pPage->hdrOffset] ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2924 | pChild->pParent = pPage; |
drh | 457f501 | 2004-05-09 01:35:05 +0000 | [diff] [blame] | 2925 | sqlite3pager_ref(pPage->aData); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2926 | pChild->idxParent = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2927 | pChild->isOverfull = 1; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 2928 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2929 | put4byte(&pPage->aData[pPage->hdrOffset+6], pChild->pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2930 | pParent = pPage; |
| 2931 | pPage = pChild; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2932 | extraUnref = pChild; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 2933 | TRACE(("BALANCE: copy root %d into %d and balance %d\n", |
| 2934 | pParent->pgno, pPage->pgno, pPage->pgno)); |
| 2935 | }else{ |
| 2936 | TRACE(("BALANCE: begin page %d child of %d\n", |
| 2937 | pPage->pgno, pParent->pgno)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2938 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2939 | rc = sqlite3pager_write(pParent->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2940 | if( rc ) return rc; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2941 | assert( pParent->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2942 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2943 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2944 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2945 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 2946 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2947 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2948 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2949 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2950 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2951 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2952 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2953 | if( get4byte(&pParent->aCell[idx][2])==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2954 | break; |
| 2955 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2956 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2957 | assert( idx<pParent->nCell |
| 2958 | || get4byte(&pParent->aData[pParent->hdrOffset+6])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 2959 | }else{ |
| 2960 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2961 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2962 | |
| 2963 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2964 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2965 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2966 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2967 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2968 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2969 | |
| 2970 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2971 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2972 | ** the siblings. An attempt is made to find NN siblings on either |
| 2973 | ** side of pPage. More siblings are taken from one side, however, if |
| 2974 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 2975 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2976 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2977 | nxDiv = idx - NN; |
| 2978 | if( nxDiv + NB > pParent->nCell ){ |
| 2979 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2980 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2981 | if( nxDiv<0 ){ |
| 2982 | nxDiv = 0; |
| 2983 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2984 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 2985 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2986 | if( k<pParent->nCell ){ |
| 2987 | idxDiv[i] = k; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2988 | apDiv[i] = pParent->aCell[k]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2989 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2990 | assert( !pParent->leaf ); |
| 2991 | pgnoOld[i] = get4byte(&apDiv[i][2]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2992 | }else if( k==pParent->nCell ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2993 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+6]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2994 | }else{ |
| 2995 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2996 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2997 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2998 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2999 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3000 | apCopy[i] = 0; |
| 3001 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3002 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3006 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 3007 | ** The rest of this function will use data from the copies rather |
| 3008 | ** that the original pages since the original pages will be in the |
| 3009 | ** process of being overwritten. |
| 3010 | */ |
| 3011 | for(i=0; i<nOld; i++){ |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3012 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i+1][-sizeof(MemPage)]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3013 | p->aData = &((u8*)p)[-pBt->usableSize]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3014 | p->aCell = 0; |
| 3015 | p->hdrOffset = 0; |
| 3016 | movePage(p, apOld[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | /* |
| 3020 | ** Load pointers to all cells on sibling pages and the divider cells |
| 3021 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3022 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 3023 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3024 | ** |
| 3025 | ** If the siblings are on leaf pages, then the child pointers of the |
| 3026 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3027 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3028 | ** child pointers. If siblings are not leaves, then all cell in |
| 3029 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 3030 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3031 | ** |
| 3032 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 3033 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3034 | */ |
| 3035 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3036 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3037 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3038 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3039 | MemPage *pOld = apCopy[i]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3040 | for(j=0; j<pOld->nCell; j++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3041 | apCell[nCell] = pOld->aCell[j]; |
| 3042 | szCell[nCell] = cellSize(pOld, apCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3043 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3044 | } |
| 3045 | if( i<nOld-1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3046 | int sz = cellSize(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3047 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3048 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 3049 | ** are duplicates of keys on the child pages. We need to remove |
| 3050 | ** the divider cells from pParent, but the dividers cells are not |
| 3051 | ** added to apCell[] because they are duplicates of child cells. |
| 3052 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3053 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3054 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3055 | u8 *pTemp; |
| 3056 | szCell[nCell] = sz; |
| 3057 | pTemp = &aSpace[iSpace]; |
| 3058 | iSpace += sz; |
| 3059 | assert( iSpace<=sizeof(aSpace) ); |
| 3060 | memcpy(pTemp, apDiv[i], sz); |
| 3061 | apCell[nCell] = pTemp+leafCorrection; |
| 3062 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3063 | szCell[nCell] -= leafCorrection; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3064 | assert( get4byte(pTemp+2)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3065 | if( !pOld->leaf ){ |
| 3066 | assert( leafCorrection==0 ); |
| 3067 | /* The right pointer of the child page pOld becomes the left |
| 3068 | ** pointer of the divider cell */ |
| 3069 | memcpy(&apCell[nCell][2], &pOld->aData[pOld->hdrOffset+6], 4); |
| 3070 | }else{ |
| 3071 | assert( leafCorrection==4 ); |
| 3072 | } |
| 3073 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3074 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3079 | ** Figure out the number of pages needed to hold all nCell cells. |
| 3080 | ** Store this number in "k". Also compute szNew[] which is the total |
| 3081 | ** 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] | 3082 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3083 | ** cntNew[k] should equal nCell. |
| 3084 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3085 | ** Values computed by this block: |
| 3086 | ** |
| 3087 | ** k: The total number of sibling pages |
| 3088 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 3089 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 3090 | ** the right of the i-th sibling page. |
| 3091 | ** usableSpace: Number of bytes of space available on each sibling. |
| 3092 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3093 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3094 | usableSpace = pBt->usableSize - 10 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3095 | for(subtotal=k=i=0; i<nCell; i++){ |
| 3096 | subtotal += szCell[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3097 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3098 | szNew[k] = subtotal - szCell[i]; |
| 3099 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3100 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3101 | subtotal = 0; |
| 3102 | k++; |
| 3103 | } |
| 3104 | } |
| 3105 | szNew[k] = subtotal; |
| 3106 | cntNew[k] = nCell; |
| 3107 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3108 | |
| 3109 | /* |
| 3110 | ** The packing computed by the previous block is biased toward the siblings |
| 3111 | ** on the left side. The left siblings are always nearly full, while the |
| 3112 | ** right-most sibling might be nearly empty. This block of code attempts |
| 3113 | ** to adjust the packing of siblings to get a better balance. |
| 3114 | ** |
| 3115 | ** This adjustment is more than an optimization. The packing above might |
| 3116 | ** be so out of balance as to be illegal. For example, the right-most |
| 3117 | ** sibling might be completely empty. This adjustment is not optional. |
| 3118 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3119 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3120 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 3121 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 3122 | int r; /* Index of right-most cell in left sibling */ |
| 3123 | int d; /* Index of first cell to the left of right sibling */ |
| 3124 | |
| 3125 | r = cntNew[i-1] - 1; |
| 3126 | d = r + 1 - leafData; |
| 3127 | while( szRight==0 || szRight+szCell[d]<=szLeft-szCell[r] ){ |
| 3128 | szRight += szCell[d]; |
| 3129 | szLeft -= szCell[r]; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3130 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3131 | r = cntNew[i-1] - 1; |
| 3132 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3133 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3134 | szNew[i] = szRight; |
| 3135 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3136 | } |
| 3137 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3138 | |
| 3139 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3140 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3141 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3142 | assert( pPage->pgno>1 ); |
| 3143 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3144 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3145 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3146 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3147 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3148 | pgnoNew[i] = pgnoOld[i]; |
| 3149 | apOld[i] = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3150 | sqlite3pager_write(pNew->aData); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3151 | }else{ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3152 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3153 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3154 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3155 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3156 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3157 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3160 | /* Free any old pages that were not reused as new pages. |
| 3161 | */ |
| 3162 | while( i<nOld ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3163 | rc = freePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3164 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3165 | releasePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3166 | apOld[i] = 0; |
| 3167 | i++; |
| 3168 | } |
| 3169 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3170 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3171 | ** Put the new pages in accending order. This helps to |
| 3172 | ** keep entries in the disk file in order so that a scan |
| 3173 | ** of the table is a linear scan through the file. That |
| 3174 | ** in turn helps the operating system to deliver pages |
| 3175 | ** from the disk more rapidly. |
| 3176 | ** |
| 3177 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3178 | ** n is never more than NB (a small constant), that should |
| 3179 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3180 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3181 | ** When NB==3, this one optimization makes the database |
| 3182 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3183 | */ |
| 3184 | for(i=0; i<k-1; i++){ |
| 3185 | int minV = pgnoNew[i]; |
| 3186 | int minI = i; |
| 3187 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 3188 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3189 | minI = j; |
| 3190 | minV = pgnoNew[j]; |
| 3191 | } |
| 3192 | } |
| 3193 | if( minI>i ){ |
| 3194 | int t; |
| 3195 | MemPage *pT; |
| 3196 | t = pgnoNew[i]; |
| 3197 | pT = apNew[i]; |
| 3198 | pgnoNew[i] = pgnoNew[minI]; |
| 3199 | apNew[i] = apNew[minI]; |
| 3200 | pgnoNew[minI] = t; |
| 3201 | apNew[minI] = pT; |
| 3202 | } |
| 3203 | } |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 3204 | TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d)\n", |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3205 | pgnoOld[0], |
| 3206 | nOld>=2 ? pgnoOld[1] : 0, |
| 3207 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 3208 | pgnoNew[0], szNew[0], |
| 3209 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 3210 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
| 3211 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3212 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3213 | |
| 3214 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3215 | ** Evenly distribute the data in apCell[] across the new pages. |
| 3216 | ** Insert divider cells into pParent as necessary. |
| 3217 | */ |
| 3218 | j = 0; |
| 3219 | for(i=0; i<nNew; i++){ |
| 3220 | MemPage *pNew = apNew[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3221 | assert( pNew->pgno==pgnoNew[i] ); |
| 3222 | resizeCellArray(pNew, cntNew[i] - j); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3223 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
| 3224 | j = cntNew[i]; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3225 | assert( pNew->nCell>0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3226 | assert( !pNew->isOverfull ); |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 3227 | assert( pNew->needRelink==0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3228 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3229 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3230 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3231 | int sz; |
| 3232 | pCell = apCell[j]; |
| 3233 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3234 | if( !pNew->leaf ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3235 | memcpy(&pNew->aData[6], pCell+2, 4); |
| 3236 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3237 | }else if( leafData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3238 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3239 | j--; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3240 | parseCell(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3241 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3242 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3243 | iSpace += sz; |
| 3244 | assert( iSpace<=sizeof(aSpace) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3245 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3246 | }else{ |
| 3247 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3248 | pTemp = &aSpace[iSpace]; |
| 3249 | iSpace += sz; |
| 3250 | assert( iSpace<=sizeof(aSpace) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3251 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3252 | insertCell(pParent, nxDiv, pCell, sz, pTemp); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3253 | put4byte(&pParent->aCell[nxDiv][2], pNew->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3254 | j++; |
| 3255 | nxDiv++; |
| 3256 | } |
| 3257 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3258 | assert( j==nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3259 | if( (pageFlags & PTF_LEAF)==0 ){ |
| 3260 | memcpy(&apNew[nNew-1]->aData[6], &apCopy[nOld-1]->aData[6], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3261 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3262 | if( nxDiv==pParent->nCell ){ |
| 3263 | /* Right-most sibling is the right-most child of pParent */ |
| 3264 | put4byte(&pParent->aData[pParent->hdrOffset+6], pgnoNew[nNew-1]); |
| 3265 | }else{ |
| 3266 | /* Right-most sibling is the left child of the first entry in pParent |
| 3267 | ** past the right-most divider entry */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3268 | put4byte(&pParent->aCell[nxDiv][2], pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | /* |
| 3272 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3273 | */ |
| 3274 | for(i=0; i<nNew; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3275 | reparentChildPages(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3276 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3277 | reparentChildPages(pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3278 | |
| 3279 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3280 | ** Balance the parent page. Note that the current page (pPage) might |
| 3281 | ** have been added to the freelist is it might no longer be initialized. |
| 3282 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3283 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3284 | assert( pParent->isInit ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3285 | /* assert( pPage->isInit ); // No! pPage might have been added to freelist */ |
| 3286 | /* pageIntegrity(pPage); // No! pPage might have been added to freelist */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3287 | rc = balance(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3288 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3289 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3290 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3291 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3292 | balance_cleanup: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3293 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3294 | releasePage(apOld[i]); |
| 3295 | if( apCopy[i] ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3296 | sqliteFree(apCopy[i]->aCell); |
| 3297 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3298 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3299 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3300 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3301 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3302 | releasePage(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3303 | releasePage(extraUnref); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3304 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 3305 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3306 | return rc; |
| 3307 | } |
| 3308 | |
| 3309 | /* |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3310 | ** This routine checks all cursors that point to the same table |
| 3311 | ** as pCur points to. If any of those cursors were opened with |
| 3312 | ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all |
| 3313 | ** cursors point to the same table were opened with wrFlag==1 |
| 3314 | ** then this routine returns SQLITE_OK. |
| 3315 | ** |
| 3316 | ** In addition to checking for read-locks (where a read-lock |
| 3317 | ** means a cursor opened with wrFlag==0) this routine also moves |
| 3318 | ** all cursors other than pCur so that they are pointing to the |
| 3319 | ** first Cell on root page. This is necessary because an insert |
| 3320 | ** or delete might change the number of cells on a page or delete |
| 3321 | ** a page entirely and we do not want to leave any cursors |
| 3322 | ** pointing to non-existant pages or cells. |
| 3323 | */ |
| 3324 | static int checkReadLocks(BtCursor *pCur){ |
| 3325 | BtCursor *p; |
| 3326 | assert( pCur->wrFlag ); |
| 3327 | for(p=pCur->pShared; p!=pCur; p=p->pShared){ |
| 3328 | assert( p ); |
| 3329 | assert( p->pgnoRoot==pCur->pgnoRoot ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3330 | assert( p->pPage->pgno==sqlite3pager_pagenumber(p->pPage->aData) ); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3331 | if( p->wrFlag==0 ) return SQLITE_LOCKED; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3332 | if( p->pPage->pgno!=p->pgnoRoot ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3333 | moveToRoot(p); |
| 3334 | } |
| 3335 | } |
| 3336 | return SQLITE_OK; |
| 3337 | } |
| 3338 | |
| 3339 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3340 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 3341 | ** 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] | 3342 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3343 | ** is left pointing at a random location. |
| 3344 | ** |
| 3345 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 3346 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3347 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3348 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3349 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3350 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 3351 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3352 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3353 | int rc; |
| 3354 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3355 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3356 | MemPage *pPage; |
| 3357 | Btree *pBt = pCur->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3358 | unsigned char *oldCell; |
| 3359 | unsigned char newCell[MX_CELL_SIZE]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3360 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3361 | if( pCur->status ){ |
| 3362 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3363 | } |
danielk1977 | e7c8d58 | 2004-05-13 13:38:52 +0000 | [diff] [blame] | 3364 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3365 | /* Must start a transaction before doing an insert */ |
| 3366 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3367 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3368 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3369 | if( !pCur->wrFlag ){ |
| 3370 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 3371 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3372 | if( checkReadLocks(pCur) ){ |
| 3373 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3374 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3375 | rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3376 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3377 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3378 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3379 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3380 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 3381 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 3382 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3383 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3384 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3385 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3386 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3387 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3388 | assert( szNew==cellSize(pPage, newCell) ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3389 | assert( szNew<=sizeof(newCell) ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3390 | if( loc==0 && pCur->isValid ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3391 | int szOld; |
| 3392 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3393 | oldCell = pPage->aCell[pCur->idx]; |
| 3394 | if( !pPage->leaf ){ |
| 3395 | memcpy(&newCell[2], &oldCell[2], 4); |
| 3396 | } |
| 3397 | szOld = cellSize(pPage, oldCell); |
| 3398 | rc = clearCell(pPage, oldCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3399 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3400 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3401 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3402 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3403 | pCur->idx++; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3404 | pCur->infoValid = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3405 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3406 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3407 | } |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3408 | insertCell(pPage, pCur->idx, newCell, szNew, 0); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3409 | rc = balance(pPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3410 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 3411 | /* fflush(stdout); */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3412 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3413 | return rc; |
| 3414 | } |
| 3415 | |
| 3416 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3417 | ** Delete the entry that the cursor is pointing to. The cursor |
| 3418 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3419 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3420 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3421 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3422 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3423 | int rc; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3424 | Pgno pgnoChild; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3425 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3426 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3427 | assert( pPage->isInit ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3428 | if( pCur->status ){ |
| 3429 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3430 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3431 | if( !pBt->inTrans ){ |
| 3432 | /* Must start a transaction before doing a delete */ |
| 3433 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3434 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3435 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3436 | if( pCur->idx >= pPage->nCell ){ |
| 3437 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 3438 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3439 | if( !pCur->wrFlag ){ |
| 3440 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 3441 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3442 | if( checkReadLocks(pCur) ){ |
| 3443 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 3444 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3445 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3446 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3447 | pCell = pPage->aCell[pCur->idx]; |
| 3448 | if( !pPage->leaf ){ |
| 3449 | pgnoChild = get4byte(&pCell[2]); |
| 3450 | } |
| 3451 | clearCell(pPage, pCell); |
| 3452 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3453 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3454 | ** 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] | 3455 | ** do something we will leave a hole on an internal page. |
| 3456 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 3457 | ** next Cell after the one to be deleted is guaranteed to exist and |
| 3458 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3459 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3460 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3461 | unsigned char *pNext; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3462 | int szNext; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3463 | int notUsed; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3464 | unsigned char tempCell[MX_CELL_SIZE]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3465 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3466 | getTempCursor(pCur, &leafCur); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3467 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3468 | if( rc!=SQLITE_OK ){ |
drh | 8a6ac0a | 2004-02-14 17:35:07 +0000 | [diff] [blame] | 3469 | if( rc!=SQLITE_NOMEM ) rc = SQLITE_CORRUPT; |
| 3470 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3471 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3472 | rc = sqlite3pager_write(leafCur.pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3473 | if( rc ) return rc; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3474 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 3475 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3476 | dropCell(pPage, pCur->idx, cellSize(pPage, pCell)); |
| 3477 | pNext = leafCur.pPage->aCell[leafCur.idx]; |
| 3478 | szNext = cellSize(leafCur.pPage, pNext); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3479 | assert( sizeof(tempCell)>=szNext+4 ); |
| 3480 | insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell); |
| 3481 | put4byte(pPage->aCell[pCur->idx]+2, pgnoChild); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3482 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3483 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3484 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 3485 | rc = balance(leafCur.pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3486 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3487 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3488 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 3489 | pCur->pgnoRoot, pPage->pgno)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3490 | dropCell(pPage, pCur->idx, cellSize(pPage, pCell)); |
| 3491 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3492 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3493 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3494 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3495 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3496 | |
| 3497 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 3498 | ** Create a new BTree table. Write into *piTable the page |
| 3499 | ** number for the root page of the new table. |
| 3500 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 3501 | ** The type of type is determined by the flags parameter. Only the |
| 3502 | ** following values of flags are currently in use. Other values for |
| 3503 | ** flags might not work: |
| 3504 | ** |
| 3505 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 3506 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3507 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3508 | int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3509 | MemPage *pRoot; |
| 3510 | Pgno pgnoRoot; |
| 3511 | int rc; |
| 3512 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3513 | /* Must start a transaction first */ |
| 3514 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3515 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3516 | if( pBt->readOnly ){ |
| 3517 | return SQLITE_READONLY; |
| 3518 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3519 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3520 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3521 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3522 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3523 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3524 | *piTable = (int)pgnoRoot; |
| 3525 | return SQLITE_OK; |
| 3526 | } |
| 3527 | |
| 3528 | /* |
| 3529 | ** Erase the given database page and all its children. Return |
| 3530 | ** the page to the freelist. |
| 3531 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3532 | static int clearDatabasePage( |
| 3533 | Btree *pBt, /* The BTree that contains the table */ |
| 3534 | Pgno pgno, /* Page number to clear */ |
| 3535 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 3536 | int freePageFlag /* Deallocate page if true */ |
| 3537 | ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3538 | MemPage *pPage; |
| 3539 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3540 | unsigned char *pCell; |
| 3541 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3542 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3543 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3544 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3545 | rc = sqlite3pager_write(pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3546 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3547 | for(i=0; i<pPage->nCell; i++){ |
| 3548 | pCell = pPage->aCell[i]; |
| 3549 | if( !pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3550 | rc = clearDatabasePage(pBt, get4byte(&pCell[2]), pPage->pParent, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3551 | if( rc ) return rc; |
| 3552 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3553 | rc = clearCell(pPage, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3554 | if( rc ) return rc; |
| 3555 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3556 | if( !pPage->leaf ){ |
| 3557 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[6]), pPage->pParent, 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3558 | if( rc ) return rc; |
| 3559 | } |
| 3560 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3561 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3562 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3563 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3564 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3565 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3566 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 3570 | ** Delete all information from a single table in the database. iTable is |
| 3571 | ** the page number of the root of the table. After this routine returns, |
| 3572 | ** the root page is empty, but still exists. |
| 3573 | ** |
| 3574 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 3575 | ** read cursors on the table. Open write cursors are moved to the |
| 3576 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3577 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3578 | int sqlite3BtreeClearTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3579 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3580 | BtCursor *pCur; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3581 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3582 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3583 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3584 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3585 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3586 | if( pCur->wrFlag==0 ) return SQLITE_LOCKED; |
| 3587 | moveToRoot(pCur); |
| 3588 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3589 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3590 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3591 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3592 | sqlite3BtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3593 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3594 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | /* |
| 3598 | ** Erase all information in a table and add the root of the table to |
| 3599 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame^] | 3600 | ** page 1) is never added to the freelist. |
| 3601 | ** |
| 3602 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 3603 | ** cursors on the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3604 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3605 | int sqlite3BtreeDropTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3606 | int rc; |
| 3607 | MemPage *pPage; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3608 | BtCursor *pCur; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3609 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3610 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3611 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3612 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 3613 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 3614 | return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */ |
| 3615 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3616 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3617 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3618 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3619 | rc = sqlite3BtreeClearTable(pBt, iTable); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3620 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3621 | if( iTable>1 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3622 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3623 | }else{ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3624 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3625 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3626 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3627 | return rc; |
| 3628 | } |
| 3629 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 3630 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3631 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3632 | ** Read the meta-information out of a database file. Meta[0] |
| 3633 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 3634 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 3635 | ** is read-only, the others are read/write. |
| 3636 | ** |
| 3637 | ** The schema layer numbers meta values differently. At the schema |
| 3638 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 3639 | ** free pages is not visible. So Cookie[0] is the same as Meta[1]. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3640 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3641 | int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3642 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3643 | unsigned char *pP1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3644 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3645 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3646 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3647 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3648 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3649 | sqlite3pager_unref(pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3650 | return SQLITE_OK; |
| 3651 | } |
| 3652 | |
| 3653 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3654 | ** Write meta-information back into the database. Meta[0] is |
| 3655 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3656 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3657 | int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3658 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3659 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3660 | assert( idx>=1 && idx<=15 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3661 | if( !pBt->inTrans ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 3662 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 3663 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3664 | assert( pBt->pPage1!=0 ); |
| 3665 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3666 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3667 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3668 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3669 | return SQLITE_OK; |
| 3670 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3671 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3672 | /* |
| 3673 | ** Return the flag byte at the beginning of the page that the cursor |
| 3674 | ** is currently pointing to. |
| 3675 | */ |
| 3676 | int sqlite3BtreeFlags(BtCursor *pCur){ |
| 3677 | MemPage *pPage = pCur->pPage; |
| 3678 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 3679 | } |
| 3680 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3681 | /****************************************************************************** |
| 3682 | ** The complete implementation of the BTree subsystem is above this line. |
| 3683 | ** All the code the follows is for testing and troubleshooting the BTree |
| 3684 | ** subsystem. None of the code that follows is used during normal operation. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3685 | ******************************************************************************/ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3686 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3687 | /* |
| 3688 | ** Print a disassembly of the given page on standard output. This routine |
| 3689 | ** is used for debugging and testing only. |
| 3690 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3691 | #ifdef SQLITE_TEST |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 3692 | int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3693 | int rc; |
| 3694 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3695 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3696 | int nFree; |
| 3697 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3698 | int hdr; |
| 3699 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3700 | char range[20]; |
| 3701 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3702 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3703 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3704 | if( rc ){ |
| 3705 | return rc; |
| 3706 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3707 | hdr = pPage->hdrOffset; |
| 3708 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3709 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3710 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3711 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3712 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 3713 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3714 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3715 | printf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
| 3716 | data[hdr], data[hdr+5], |
| 3717 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3718 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3719 | assert( hdr == (pgno==1 ? 100 : 0) ); |
| 3720 | idx = get2byte(&data[hdr+3]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3721 | while( idx>0 && idx<=pBt->usableSize ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3722 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3723 | Pgno child; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3724 | unsigned char *pCell = &data[idx]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3725 | int sz; |
| 3726 | |
| 3727 | pCell = &data[idx]; |
| 3728 | parseCell(pPage, pCell, &info); |
| 3729 | sz = info.nSize; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3730 | sprintf(range,"%d..%d", idx, idx+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3731 | if( pPage->leaf ){ |
| 3732 | child = 0; |
| 3733 | }else{ |
| 3734 | child = get4byte(&pCell[2]); |
| 3735 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3736 | sz = info.nData; |
| 3737 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3738 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3739 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3740 | for(j=0; j<sz; j++){ |
| 3741 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 3742 | } |
| 3743 | payload[sz] = 0; |
| 3744 | printf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3745 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 3746 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3747 | ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3748 | if( pPage->isInit && pPage->aCell[i]!=pCell ){ |
| 3749 | printf("**** aCell[%d] does not match on prior entry ****\n", i); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3750 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3751 | i++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3752 | idx = get2byte(pCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3753 | } |
| 3754 | if( idx!=0 ){ |
| 3755 | printf("ERROR: next cell index out of range: %d\n", idx); |
| 3756 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3757 | if( !pPage->leaf ){ |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 3758 | printf("right_child: %d\n", get4byte(&data[hdr+6])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3759 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3760 | nFree = 0; |
| 3761 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3762 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3763 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3764 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3765 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 3766 | nFree += sz; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3767 | printf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3768 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3769 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3770 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3771 | } |
| 3772 | if( idx!=0 ){ |
| 3773 | printf("ERROR: next freeblock index out of range: %d\n", idx); |
| 3774 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3775 | if( recursive && !pPage->leaf ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3776 | idx = get2byte(&data[hdr+3]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3777 | while( idx>0 && idx<pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3778 | unsigned char *pCell = &data[idx]; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3779 | sqlite3BtreePageDump(pBt, get4byte(&pCell[2]), 1); |
| 3780 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3781 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3782 | sqlite3BtreePageDump(pBt, get4byte(&data[hdr+6]), 1); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3783 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 3784 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 3785 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3786 | return SQLITE_OK; |
| 3787 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3788 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3789 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3790 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3791 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3792 | ** Fill aResult[] with information about the entry and page that the |
| 3793 | ** cursor is pointing to. |
| 3794 | ** |
| 3795 | ** aResult[0] = The page number |
| 3796 | ** aResult[1] = The entry number |
| 3797 | ** aResult[2] = Total number of entries on this page |
| 3798 | ** aResult[3] = Size of this entry |
| 3799 | ** aResult[4] = Number of free bytes on this page |
| 3800 | ** aResult[5] = Number of free blocks on the page |
| 3801 | ** aResult[6] = Page number of the left child of this entry |
| 3802 | ** aResult[7] = Page number of the right child for the whole page |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3803 | ** |
| 3804 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3805 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3806 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3807 | int cnt, idx; |
| 3808 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3809 | |
| 3810 | pageIntegrity(pPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3811 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3812 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3813 | assert( aResult[0]==pPage->pgno ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3814 | aResult[1] = pCur->idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3815 | aResult[2] = pPage->nCell; |
| 3816 | if( pCur->idx>=0 && pCur->idx<pPage->nCell ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3817 | aResult[3] = cellSize(pPage, pPage->aCell[pCur->idx]); |
| 3818 | aResult[6] = pPage->leaf ? 0 : get4byte(&pPage->aCell[pCur->idx][2]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3819 | }else{ |
| 3820 | aResult[3] = 0; |
| 3821 | aResult[6] = 0; |
| 3822 | } |
| 3823 | aResult[4] = pPage->nFree; |
| 3824 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3825 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3826 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3827 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3828 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3829 | } |
| 3830 | aResult[5] = cnt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3831 | aResult[7] = pPage->leaf ? 0 : get4byte(&pPage->aData[pPage->hdrOffset+6]); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3832 | return SQLITE_OK; |
| 3833 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3834 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3835 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3836 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3837 | ** Return the pager associated with a BTree. This routine is used for |
| 3838 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3839 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3840 | Pager *sqlite3BtreePager(Btree *pBt){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3841 | return pBt->pPager; |
| 3842 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3843 | |
| 3844 | /* |
| 3845 | ** This structure is passed around through all the sanity checking routines |
| 3846 | ** in order to keep track of some global state information. |
| 3847 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3848 | typedef struct IntegrityCk IntegrityCk; |
| 3849 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 3850 | Btree *pBt; /* The tree being checked out */ |
| 3851 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 3852 | int nPage; /* Number of pages in the database */ |
| 3853 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 3854 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3855 | }; |
| 3856 | |
| 3857 | /* |
| 3858 | ** Append a message to the error message string. |
| 3859 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3860 | static void checkAppendMsg(IntegrityCk *pCheck, char *zMsg1, char *zMsg2){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3861 | if( pCheck->zErrMsg ){ |
| 3862 | char *zOld = pCheck->zErrMsg; |
| 3863 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3864 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3865 | sqliteFree(zOld); |
| 3866 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 3867 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | /* |
| 3872 | ** Add 1 to the reference count for page iPage. If this is the second |
| 3873 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 3874 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 3875 | ** if this is the first reference to the page. |
| 3876 | ** |
| 3877 | ** Also check that the page number is in bounds. |
| 3878 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3879 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3880 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 3881 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3882 | char zBuf[100]; |
| 3883 | sprintf(zBuf, "invalid page number %d", iPage); |
| 3884 | checkAppendMsg(pCheck, zContext, zBuf); |
| 3885 | return 1; |
| 3886 | } |
| 3887 | if( pCheck->anRef[iPage]==1 ){ |
| 3888 | char zBuf[100]; |
| 3889 | sprintf(zBuf, "2nd reference to page %d", iPage); |
| 3890 | checkAppendMsg(pCheck, zContext, zBuf); |
| 3891 | return 1; |
| 3892 | } |
| 3893 | return (pCheck->anRef[iPage]++)>1; |
| 3894 | } |
| 3895 | |
| 3896 | /* |
| 3897 | ** Check the integrity of the freelist or of an overflow page list. |
| 3898 | ** Verify that the number of pages on the list is N. |
| 3899 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3900 | static void checkList( |
| 3901 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 3902 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 3903 | int iPage, /* Page number for first page in the list */ |
| 3904 | int N, /* Expected number of pages in the list */ |
| 3905 | char *zContext /* Context for error messages */ |
| 3906 | ){ |
| 3907 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3908 | int expected = N; |
| 3909 | int iFirst = iPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3910 | char zMsg[100]; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3911 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3912 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3913 | if( iPage<1 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3914 | sprintf(zMsg, "%d of %d pages missing from overflow list starting at %d", |
| 3915 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3916 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3917 | break; |
| 3918 | } |
| 3919 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3920 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3921 | sprintf(zMsg, "failed to get page %d", iPage); |
| 3922 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3923 | break; |
| 3924 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3925 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3926 | int n = get4byte(&pOvfl[4]); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3927 | for(i=0; i<n; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3928 | checkRef(pCheck, get4byte(&pOvfl[8+i*4]), zContext); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3929 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3930 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3931 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3932 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3933 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | /* |
| 3938 | ** Do various sanity checks on a single page of a tree. Return |
| 3939 | ** the tree depth. Root pages return 0. Parents of root pages |
| 3940 | ** return 1, and so forth. |
| 3941 | ** |
| 3942 | ** These checks are done: |
| 3943 | ** |
| 3944 | ** 1. Make sure that cells and freeblocks do not overlap |
| 3945 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3946 | ** NO 2. Make sure cell keys are in order. |
| 3947 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 3948 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3949 | ** 5. Check the integrity of overflow pages. |
| 3950 | ** 6. Recursively call checkTreePage on all children. |
| 3951 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3952 | ** 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] | 3953 | ** the root of the tree. |
| 3954 | */ |
| 3955 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3956 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3957 | int iPage, /* Page number of the page to check */ |
| 3958 | MemPage *pParent, /* Parent page */ |
| 3959 | char *zParentContext, /* Parent context */ |
| 3960 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3961 | int nLower, /* Number of characters in zLowerBound */ |
| 3962 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 3963 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3964 | ){ |
| 3965 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3966 | int i, rc, depth, d2, pgno, cnt; |
| 3967 | int hdr; |
| 3968 | u8 *data; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3969 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3970 | Btree *pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3971 | int maxLocal, usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3972 | char zMsg[100]; |
| 3973 | char zContext[100]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3974 | char hit[MX_PAGE_SIZE]; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3975 | |
| 3976 | /* Check that the page exists |
| 3977 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3978 | cur.pBt = pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3979 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3980 | if( iPage==0 ) return 0; |
| 3981 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3982 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3983 | sprintf(zMsg, "unable to get the page. error code=%d", rc); |
| 3984 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3985 | return 0; |
| 3986 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3987 | maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3988 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3989 | sprintf(zMsg, "initPage() returns error code %d", rc); |
| 3990 | checkAppendMsg(pCheck, zContext, zMsg); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3991 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3992 | return 0; |
| 3993 | } |
| 3994 | |
| 3995 | /* Check out all the cells. |
| 3996 | */ |
| 3997 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3998 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3999 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4000 | u8 *pCell; |
| 4001 | int sz; |
| 4002 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4003 | |
| 4004 | /* Check payload overflow pages |
| 4005 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4006 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4007 | pCell = pPage->aCell[i]; |
| 4008 | parseCell(pPage, pCell, &info); |
| 4009 | sz = info.nData; |
| 4010 | if( !pPage->intKey ) sz += info.nKey; |
| 4011 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4012 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4013 | checkList(pCheck, 0, get4byte(&pCell[info.iOverflow]),nPage,zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | /* Check sanity of left child page. |
| 4017 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4018 | if( !pPage->leaf ){ |
| 4019 | pgno = get4byte(&pCell[2]); |
| 4020 | d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0); |
| 4021 | if( i>0 && d2!=depth ){ |
| 4022 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 4023 | } |
| 4024 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4025 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4026 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4027 | if( !pPage->leaf ){ |
| 4028 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+6]); |
| 4029 | sprintf(zContext, "On page %d at right child: ", iPage); |
| 4030 | checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0); |
| 4031 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4032 | |
| 4033 | /* Check for complete coverage of the page |
| 4034 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4035 | memset(hit, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4036 | memset(hit, 1, pPage->hdrOffset+10-4*(pPage->leaf)); |
| 4037 | data = pPage->aData; |
| 4038 | hdr = pPage->hdrOffset; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4039 | for(cnt=0, i=get2byte(&data[hdr+3]); i>0 && i<usableSize && cnt<10000; cnt++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4040 | int size = cellSize(pPage, &data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4041 | int j; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4042 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 4043 | i = get2byte(&data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4044 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4045 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; cnt++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4046 | int size = get2byte(&data[i+2]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4047 | int j; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4048 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 4049 | i = get2byte(&data[i]); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4050 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4051 | for(i=cnt=0; i<usableSize; i++){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4052 | if( hit[i]==0 ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4053 | cnt++; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4054 | }else if( hit[i]>1 ){ |
| 4055 | sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage); |
| 4056 | checkAppendMsg(pCheck, zMsg, 0); |
| 4057 | break; |
| 4058 | } |
| 4059 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4060 | if( cnt!=data[hdr+5] ){ |
| 4061 | sprintf(zMsg, "Fragmented space is %d byte reported as %d on page %d", |
| 4062 | cnt, data[hdr+5], iPage); |
| 4063 | checkAppendMsg(pCheck, zMsg, 0); |
| 4064 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4065 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4066 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4067 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | /* |
| 4071 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 4072 | ** an array of pages numbers were each page number is the root page of |
| 4073 | ** a table. nRoot is the number of entries in aRoot. |
| 4074 | ** |
| 4075 | ** If everything checks out, this routine returns NULL. If something is |
| 4076 | ** amiss, an error message is written into memory obtained from malloc() |
| 4077 | ** and a pointer to that error message is returned. The calling function |
| 4078 | ** is responsible for freeing the error message when it is done. |
| 4079 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4080 | char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4081 | int i; |
| 4082 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4083 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4084 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4085 | nRef = *sqlite3pager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 4086 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 4087 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 4088 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4089 | sCheck.pBt = pBt; |
| 4090 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4091 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 4092 | if( sCheck.nPage==0 ){ |
| 4093 | unlockBtreeIfUnused(pBt); |
| 4094 | return 0; |
| 4095 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4096 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4097 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4098 | sCheck.zErrMsg = 0; |
| 4099 | |
| 4100 | /* Check the integrity of the freelist |
| 4101 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4102 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 4103 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4104 | |
| 4105 | /* Check all the tables. |
| 4106 | */ |
| 4107 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 4108 | if( aRoot[i]==0 ) continue; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 4109 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | /* Make sure every page in the file is referenced |
| 4113 | */ |
| 4114 | for(i=1; i<=sCheck.nPage; i++){ |
| 4115 | if( sCheck.anRef[i]==0 ){ |
| 4116 | char zBuf[100]; |
| 4117 | sprintf(zBuf, "Page %d is never used", i); |
| 4118 | checkAppendMsg(&sCheck, zBuf, 0); |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | /* Make sure this analysis did not leave any unref() pages |
| 4123 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4124 | unlockBtreeIfUnused(pBt); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4125 | if( nRef != *sqlite3pager_stats(pBt->pPager) ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4126 | char zBuf[100]; |
| 4127 | sprintf(zBuf, |
| 4128 | "Outstanding page count goes from %d to %d during this analysis", |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4129 | nRef, *sqlite3pager_stats(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4130 | ); |
| 4131 | checkAppendMsg(&sCheck, zBuf, 0); |
| 4132 | } |
| 4133 | |
| 4134 | /* Clean up and report errors. |
| 4135 | */ |
| 4136 | sqliteFree(sCheck.anRef); |
| 4137 | return sCheck.zErrMsg; |
| 4138 | } |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 4139 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4140 | /* |
| 4141 | ** Return the full pathname of the underlying database file. |
| 4142 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4143 | const char *sqlite3BtreeGetFilename(Btree *pBt){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4144 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4145 | return sqlite3pager_filename(pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4149 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 4150 | ** must be active for both files. |
| 4151 | ** |
| 4152 | ** The size of file pBtFrom may be reduced by this operation. |
| 4153 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4154 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4155 | int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4156 | int rc = SQLITE_OK; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4157 | Pgno i, nPage, nToPage; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4158 | |
| 4159 | if( !pBtTo->inTrans || !pBtFrom->inTrans ) return SQLITE_ERROR; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4160 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | 465407d | 2004-05-20 02:01:26 +0000 | [diff] [blame] | 4161 | memcpy(pBtTo->pPage1->aData, pBtFrom->pPage1->aData, pBtFrom->usableSize); |
| 4162 | rc = sqlite3pager_overwrite(pBtTo->pPager, 1, pBtFrom->pPage1->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4163 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 4164 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4165 | for(i=2; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4166 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4167 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4168 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4169 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4170 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4171 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4172 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4173 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 4174 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4175 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4176 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4177 | rc = sqlite3pager_write(pPage); |
| 4178 | sqlite3pager_unref(pPage); |
| 4179 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4180 | } |
| 4181 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4182 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 4183 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4184 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4185 | sqlite3BtreeRollback(pBtTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 4186 | } |
| 4187 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 4188 | } |