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