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 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.214 2004/11/08 07:13:14 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 |
drh | 44f87bd | 2004-09-27 13:19:51 +0000 | [diff] [blame] | 130 | ** only keys and no data. The intkey flag means that the key is a integer |
| 131 | ** which is stored in the key size entry of the cell header rather than in |
| 132 | ** the payload area. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 133 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 134 | ** The cell pointer array begins on the first byte after the page header. |
| 135 | ** The cell pointer array contains zero or more 2-byte numbers which are |
| 136 | ** offsets from the beginning of the page to the cell content in the cell |
| 137 | ** content area. The cell pointers occur in sorted order. The system strives |
| 138 | ** to keep free space after the last cell pointer so that new cells can |
drh | 44f87bd | 2004-09-27 13:19:51 +0000 | [diff] [blame] | 139 | ** be easily added without having to defragment the page. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 140 | ** |
| 141 | ** Cell content is stored at the very end of the page and grows toward the |
| 142 | ** beginning of the page. |
| 143 | ** |
| 144 | ** Unused space within the cell content area is collected into a linked list of |
| 145 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 146 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 147 | ** increasing order. Because a freeblock must be at least 4 bytes in size, |
| 148 | ** any group of 3 or fewer unused bytes in the cell content area cannot |
| 149 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
| 150 | ** a fragment. The total number of bytes in all fragments is recorded. |
| 151 | ** in the page header at offset 7. |
| 152 | ** |
| 153 | ** SIZE DESCRIPTION |
| 154 | ** 2 Byte offset of the next freeblock |
| 155 | ** 2 Bytes in this freeblock |
| 156 | ** |
| 157 | ** Cells are of variable length. Cells are stored in the cell content area at |
| 158 | ** the end of the page. Pointers to the cells are in the cell pointer array |
| 159 | ** that immediately follows the page header. Cells is not necessarily |
| 160 | ** contiguous or in order, but cell pointers are contiguous and in order. |
| 161 | ** |
| 162 | ** Cell content makes use of variable length integers. A variable |
| 163 | ** 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] | 164 | ** 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] | 165 | ** 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] | 166 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
| 167 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
| 168 | ** allows a 64-bit integer to be encoded in 9 bytes. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 169 | ** |
| 170 | ** 0x00 becomes 0x00000000 |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 171 | ** 0x7f becomes 0x0000007f |
| 172 | ** 0x81 0x00 becomes 0x00000080 |
| 173 | ** 0x82 0x00 becomes 0x00000100 |
| 174 | ** 0x80 0x7f becomes 0x0000007f |
| 175 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 176 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 177 | ** |
| 178 | ** Variable length integers are used for rowids and to hold the number of |
| 179 | ** bytes of key and data in a btree cell. |
| 180 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 181 | ** The content of a cell looks like this: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 182 | ** |
| 183 | ** SIZE DESCRIPTION |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 184 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 185 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 186 | ** 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] | 187 | ** * Payload |
| 188 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 189 | ** |
| 190 | ** Overflow pages form a linked list. Each page except the last is completely |
| 191 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 192 | ** as 1 byte of data. |
| 193 | ** |
| 194 | ** SIZE DESCRIPTION |
| 195 | ** 4 Page number of next overflow page |
| 196 | ** * Data |
| 197 | ** |
| 198 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
| 199 | ** file header points to first in a linked list of trunk page. Each trunk |
| 200 | ** page points to multiple leaf pages. The content of a leaf page is |
| 201 | ** unspecified. A trunk page looks like this: |
| 202 | ** |
| 203 | ** SIZE DESCRIPTION |
| 204 | ** 4 Page number of next trunk page |
| 205 | ** 4 Number of leaf pointers on this page |
| 206 | ** * zero or more pages numbers of leaves |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 207 | */ |
| 208 | #include "sqliteInt.h" |
| 209 | #include "pager.h" |
| 210 | #include "btree.h" |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 211 | #include "os.h" |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 212 | #include <assert.h> |
| 213 | |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 214 | /* |
| 215 | ** This macro rounds values up so that if the value is an address it |
| 216 | ** is guaranteed to be an address that is aligned to an 8-byte boundary. |
| 217 | */ |
| 218 | #define FORCE_ALIGNMENT(X) (((X)+7)&~7) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 219 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 220 | /* The following value is the maximum cell size assuming a maximum page |
| 221 | ** size give above. |
| 222 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 223 | #define MX_CELL_SIZE(pBt) (pBt->pageSize-8) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 224 | |
| 225 | /* The maximum number of cells on a single page of the database. This |
| 226 | ** assumes a minimum cell size of 3 bytes. Such small cells will be |
| 227 | ** exceedingly rare, but they are possible. |
| 228 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 229 | #define MX_CELL(pBt) ((pBt->pageSize-8)/3) |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 230 | |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 231 | /* Forward declarations */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 232 | typedef struct MemPage MemPage; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 233 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 234 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 235 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 236 | ** SQLite database in order to identify the file as a real database. |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 237 | ** 123456789 123456 */ |
| 238 | static const char zMagicHeader[] = "SQLite format 3"; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 239 | |
| 240 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 241 | ** Page type flags. An ORed combination of these flags appear as the |
| 242 | ** first byte of every BTree page. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 243 | */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 244 | #define PTF_INTKEY 0x01 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 245 | #define PTF_ZERODATA 0x02 |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 246 | #define PTF_LEAFDATA 0x04 |
| 247 | #define PTF_LEAF 0x08 |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 250 | ** As each page of the file is loaded into memory, an instance of the following |
| 251 | ** structure is appended and initialized to zero. This structure stores |
| 252 | ** information about the page that is decoded from the raw file page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 253 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 254 | ** The pParent field points back to the parent page. This allows us to |
| 255 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 256 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 257 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 258 | */ |
| 259 | struct MemPage { |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 260 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 261 | u8 idxShift; /* True if Cell indices have changed */ |
| 262 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 263 | u8 intKey; /* True if intkey flag is set */ |
| 264 | u8 leaf; /* True if leaf flag is set */ |
| 265 | u8 zeroData; /* True if table stores keys only */ |
| 266 | u8 leafData; /* True if tables stores data on leaves only */ |
| 267 | u8 hasData; /* True if this page stores data */ |
| 268 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 269 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 270 | u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ |
| 271 | u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 272 | u16 cellOffset; /* Index in aData of first cell pointer */ |
| 273 | u16 idxParent; /* Index in parent of this node */ |
| 274 | u16 nFree; /* Number of free bytes on the page */ |
| 275 | u16 nCell; /* Number of cells on this page, local and ovfl */ |
| 276 | struct _OvflCell { /* Cells that will not fit on aData[] */ |
| 277 | u8 *pCell; /* Pointers to the body of the overflow cell */ |
| 278 | u16 idx; /* Insert this cell before idx-th non-overflow cell */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 279 | } aOvfl[5]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 280 | struct Btree *pBt; /* Pointer back to BTree structure */ |
| 281 | u8 *aData; /* Pointer back to the start of the page */ |
| 282 | Pgno pgno; /* Page number for this page */ |
| 283 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 284 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 285 | |
| 286 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 287 | ** The in-memory image of a disk page has the auxiliary information appended |
| 288 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 289 | ** that extra information. |
| 290 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 291 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 292 | |
| 293 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 294 | ** Everything we need to know about an open database |
| 295 | */ |
| 296 | struct Btree { |
| 297 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 298 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 299 | MemPage *pPage1; /* First page of the database */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 300 | u8 inTrans; /* True if a transaction is in progress */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 301 | u8 inStmt; /* True if we are in a statement subtransaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 302 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 303 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 304 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 305 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 306 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 307 | u16 pageSize; /* Total number of bytes on a page */ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 308 | u16 psAligned; /* pageSize rounded up to a multiple of 8 */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 309 | u16 usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 310 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 311 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 312 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 313 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 314 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 315 | u8 autoVacuum; /* True if database supports auto-vacuum */ |
| 316 | #endif |
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 | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 351 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 352 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 353 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 354 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 355 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 356 | CellInfo info; /* A parse of the cell we are pointing at */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 357 | u8 wrFlag; /* True if writable */ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 358 | u8 isValid; /* TRUE if points to a valid entry */ |
| 359 | u8 status; /* Set to SQLITE_ABORT if cursors is invalidated */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 360 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 361 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 362 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 363 | ** Forward declaration |
| 364 | */ |
| 365 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
| 366 | |
| 367 | |
| 368 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 369 | ** Read or write a two- and four-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 370 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 371 | static u32 get2byte(unsigned char *p){ |
| 372 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 373 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 374 | static u32 get4byte(unsigned char *p){ |
| 375 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 376 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 377 | static void put2byte(unsigned char *p, u32 v){ |
| 378 | p[0] = v>>8; |
| 379 | p[1] = v; |
| 380 | } |
| 381 | static void put4byte(unsigned char *p, u32 v){ |
| 382 | p[0] = v>>24; |
| 383 | p[1] = v>>16; |
| 384 | p[2] = v>>8; |
| 385 | p[3] = v; |
| 386 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 387 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 388 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 389 | ** Routines to read and write variable-length integers. These used to |
| 390 | ** be defined locally, but now we use the varint routines in the util.c |
| 391 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 392 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 393 | #define getVarint sqlite3GetVarint |
| 394 | #define getVarint32 sqlite3GetVarint32 |
| 395 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 396 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 397 | /* The database page the PENDING_BYTE occupies. This page is never used. |
| 398 | ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They |
| 399 | ** should possibly be consolidated (presumably in pager.h). |
| 400 | */ |
| 401 | #define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 402 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 403 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 404 | /* |
| 405 | ** These two macros define the location of the pointer-map entry for a |
| 406 | ** database page. The first argument to each is the page size used |
| 407 | ** by the database (often 1024). The second is the page number to look |
| 408 | ** up in the pointer map. |
| 409 | ** |
| 410 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
| 411 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
| 412 | ** the offset of the requested map entry. |
| 413 | ** |
| 414 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
| 415 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 416 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
| 417 | ** this test. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 418 | */ |
| 419 | #define PTRMAP_PAGENO(pgsz, pgno) (((pgno-2)/(pgsz/5+1))*(pgsz/5+1)+2) |
| 420 | #define PTRMAP_PTROFFSET(pgsz, pgno) (((pgno-2)%(pgsz/5+1)-1)*5) |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 421 | #define PTRMAP_ISPAGE(pgsz, pgno) (PTRMAP_PAGENO(pgsz,pgno)==pgno) |
| 422 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 423 | /* |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 424 | ** The pointer map is a lookup table that contains an entry for each database |
| 425 | ** page in the file except for page 1. In this context 'database page' refers |
| 426 | ** to any page that is not part of the pointer map itself. Each pointer map |
| 427 | ** entry consists of a single byte 'type' and a 4 byte page number. The |
| 428 | ** PTRMAP_XXX identifiers below are the valid types. The interpretation |
| 429 | ** of the page-number depends on the type, as follows: |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 430 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 431 | ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not |
| 432 | ** used in this case. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 433 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 434 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
| 435 | ** is not used in this case. |
| 436 | ** |
| 437 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
| 438 | ** overflow pages. The page number identifies the page that |
| 439 | ** contains the cell with a pointer to this overflow page. |
| 440 | ** |
| 441 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
| 442 | ** overflow pages. The page-number identifies the previous |
| 443 | ** page in the overflow page list. |
| 444 | ** |
| 445 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
| 446 | ** identifies the parent page in the btree. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 447 | */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 448 | #define PTRMAP_ROOTPAGE 1 |
| 449 | #define PTRMAP_FREEPAGE 2 |
| 450 | #define PTRMAP_OVERFLOW1 3 |
| 451 | #define PTRMAP_OVERFLOW2 4 |
| 452 | #define PTRMAP_BTREE 5 |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 456 | ** |
| 457 | ** This routine updates the pointer map entry for page number 'key' |
| 458 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 459 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 460 | */ |
| 461 | static int ptrmapPut(Btree *pBt, Pgno key, u8 eType, Pgno pgno){ |
| 462 | u8 *pPtrmap; /* The pointer map page */ |
| 463 | Pgno iPtrmap; /* The pointer map page number */ |
| 464 | int offset; /* Offset in pointer map page */ |
| 465 | int rc; |
| 466 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 467 | assert( key!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 468 | iPtrmap = PTRMAP_PAGENO(pBt->pageSize, key); |
| 469 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 470 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 471 | return rc; |
| 472 | } |
| 473 | offset = PTRMAP_PTROFFSET(pBt->pageSize, key); |
| 474 | |
| 475 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=pgno ){ |
| 476 | rc = sqlite3pager_write(pPtrmap); |
| 477 | if( rc!=0 ){ |
| 478 | return rc; |
| 479 | } |
| 480 | pPtrmap[offset] = eType; |
| 481 | put4byte(&pPtrmap[offset+1], pgno); |
| 482 | } |
| 483 | |
| 484 | sqlite3pager_unref(pPtrmap); |
| 485 | return SQLITE_OK; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 490 | ** |
| 491 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 492 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 493 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 494 | */ |
| 495 | static int ptrmapGet(Btree *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
| 496 | int iPtrmap; /* Pointer map page index */ |
| 497 | u8 *pPtrmap; /* Pointer map page data */ |
| 498 | int offset; /* Offset of entry in pointer map */ |
| 499 | int rc; |
| 500 | |
| 501 | iPtrmap = PTRMAP_PAGENO(pBt->pageSize, key); |
| 502 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
| 503 | if( rc!=0 ){ |
| 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | offset = PTRMAP_PTROFFSET(pBt->pageSize, key); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 508 | if( pEType ) *pEType = pPtrmap[offset]; |
| 509 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 510 | |
| 511 | sqlite3pager_unref(pPtrmap); |
| 512 | return SQLITE_OK; |
| 513 | } |
| 514 | |
| 515 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 516 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 517 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 518 | ** Given a btree page and a cell index (0 means the first cell on |
| 519 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 520 | ** to the cell content. |
| 521 | ** |
| 522 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 523 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 524 | static u8 *findCell(MemPage *pPage, int iCell){ |
| 525 | u8 *data = pPage->aData; |
| 526 | assert( iCell>=0 ); |
| 527 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
| 528 | return data + get2byte(&data[pPage->cellOffset+2*iCell]); |
| 529 | } |
| 530 | |
| 531 | /* |
| 532 | ** This a more complex version of findCell() that works for |
| 533 | ** pages that do contain overflow cells. See insert |
| 534 | */ |
| 535 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 536 | int i; |
| 537 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 538 | int k; |
| 539 | struct _OvflCell *pOvfl; |
| 540 | pOvfl = &pPage->aOvfl[i]; |
| 541 | k = pOvfl->idx; |
| 542 | if( k<=iCell ){ |
| 543 | if( k==iCell ){ |
| 544 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 545 | } |
| 546 | iCell--; |
| 547 | } |
| 548 | } |
| 549 | return findCell(pPage, iCell); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 554 | ** are two versions of this function. parseCell() takes a cell index |
| 555 | ** as the second argument and parseCellPtr() takes a pointer to the |
| 556 | ** body of the cell as its second argument. |
| 557 | */ |
| 558 | static void parseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 559 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 560 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 561 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 562 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 563 | int n; /* Number bytes in cell content header */ |
| 564 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 565 | |
| 566 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 567 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 568 | n = pPage->childPtrSize; |
| 569 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 570 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 571 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 572 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 573 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 574 | } |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 575 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 576 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 577 | pInfo->nData = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 578 | if( !pPage->intKey ){ |
| 579 | nPayload += pInfo->nKey; |
| 580 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 581 | if( nPayload<=pPage->maxLocal ){ |
| 582 | /* This is the (easy) common case where the entire payload fits |
| 583 | ** on the local page. No overflow is required. |
| 584 | */ |
| 585 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 586 | pInfo->nLocal = nPayload; |
| 587 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 588 | nSize = nPayload + n; |
| 589 | if( nSize<4 ){ |
| 590 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 591 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 592 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 593 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 594 | /* If the payload will not fit completely on the local page, we have |
| 595 | ** to decide how much to store locally and how much to spill onto |
| 596 | ** overflow pages. The strategy is to minimize the amount of unused |
| 597 | ** space on overflow pages while keeping the amount of local storage |
| 598 | ** in between minLocal and maxLocal. |
| 599 | ** |
| 600 | ** Warning: changing the way overflow payload is distributed in any |
| 601 | ** way will result in an incompatible file format. |
| 602 | */ |
| 603 | int minLocal; /* Minimum amount of payload held locally */ |
| 604 | int maxLocal; /* Maximum amount of payload held locally */ |
| 605 | int surplus; /* Overflow payload available for local storage */ |
| 606 | |
| 607 | minLocal = pPage->minLocal; |
| 608 | maxLocal = pPage->maxLocal; |
| 609 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 610 | if( surplus <= maxLocal ){ |
| 611 | pInfo->nLocal = surplus; |
| 612 | }else{ |
| 613 | pInfo->nLocal = minLocal; |
| 614 | } |
| 615 | pInfo->iOverflow = pInfo->nLocal + n; |
| 616 | pInfo->nSize = pInfo->iOverflow + 4; |
| 617 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 618 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 619 | static void parseCell( |
| 620 | MemPage *pPage, /* Page containing the cell */ |
| 621 | int iCell, /* The cell index. First cell is 0 */ |
| 622 | CellInfo *pInfo /* Fill in this structure */ |
| 623 | ){ |
| 624 | parseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 625 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 626 | |
| 627 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 628 | ** Compute the total number of bytes that a Cell needs in the cell |
| 629 | ** data area of the btree-page. The return number includes the cell |
| 630 | ** data header and the local payload, but not any overflow page or |
| 631 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 632 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 633 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 634 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 635 | CellInfo info; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 636 | parseCell(pPage, iCell, &info); |
| 637 | return info.nSize; |
| 638 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 639 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 640 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 641 | CellInfo info; |
| 642 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 643 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 647 | ** Do sanity checking on a page. Throw an exception if anything is |
| 648 | ** not right. |
| 649 | ** |
| 650 | ** This routine is used for internal error checking only. It is omitted |
| 651 | ** from most builds. |
| 652 | */ |
| 653 | #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0 |
| 654 | static void _pageIntegrity(MemPage *pPage){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 655 | int usableSize; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 656 | u8 *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 657 | int i, j, idx, c, pc, hdr, nFree; |
| 658 | int cellOffset; |
| 659 | int nCell, cellLimit; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 660 | u8 *used; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 661 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 662 | used = sqliteMallocRaw( pPage->pBt->pageSize ); |
| 663 | if( used==0 ) return; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 664 | usableSize = pPage->pBt->usableSize; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 665 | assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->psAligned] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 666 | hdr = pPage->hdrOffset; |
| 667 | assert( hdr==(pPage->pgno==1 ? 100 : 0) ); |
| 668 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
| 669 | c = pPage->aData[hdr]; |
| 670 | if( pPage->isInit ){ |
| 671 | assert( pPage->leaf == ((c & PTF_LEAF)!=0) ); |
| 672 | assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 673 | assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) ); |
| 674 | assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) ); |
| 675 | assert( pPage->hasData == |
| 676 | !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 677 | assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf ); |
| 678 | assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 679 | } |
| 680 | data = pPage->aData; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 681 | memset(used, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 682 | for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1; |
| 683 | nFree = 0; |
| 684 | pc = get2byte(&data[hdr+1]); |
| 685 | while( pc ){ |
| 686 | int size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 687 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 688 | size = get2byte(&data[pc+2]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 689 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 690 | nFree += size; |
| 691 | for(i=pc; i<pc+size; i++){ |
| 692 | assert( used[i]==0 ); |
| 693 | used[i] = 1; |
| 694 | } |
| 695 | pc = get2byte(&data[pc]); |
| 696 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 697 | idx = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 698 | nCell = get2byte(&data[hdr+3]); |
| 699 | cellLimit = get2byte(&data[hdr+5]); |
| 700 | assert( pPage->isInit==0 |
| 701 | || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) ); |
| 702 | cellOffset = pPage->cellOffset; |
| 703 | for(i=0; i<nCell; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 704 | int size; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 705 | pc = get2byte(&data[cellOffset+2*i]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 706 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 707 | size = cellSize(pPage, &data[pc]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 708 | assert( pc+size<=usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 709 | for(j=pc; j<pc+size; j++){ |
| 710 | assert( used[j]==0 ); |
| 711 | used[j] = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 712 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 713 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 714 | for(i=cellOffset+2*nCell; i<cellimit; i++){ |
| 715 | assert( used[i]==0 ); |
| 716 | used[i] = 1; |
| 717 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 718 | nFree = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 719 | for(i=0; i<usableSize; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 720 | assert( used[i]<=1 ); |
| 721 | if( used[i]==0 ) nFree++; |
| 722 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 723 | assert( nFree==data[hdr+7] ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 724 | sqliteFree(used); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 725 | } |
| 726 | #define pageIntegrity(X) _pageIntegrity(X) |
| 727 | #else |
| 728 | # define pageIntegrity(X) |
| 729 | #endif |
| 730 | |
| 731 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 732 | ** Defragment the page given. All Cells are moved to the |
| 733 | ** beginning of the page and all free space is collected |
| 734 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 735 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 736 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 737 | int i; /* Loop counter */ |
| 738 | int pc; /* Address of a i-th cell */ |
| 739 | int addr; /* Offset of first byte after cell pointer array */ |
| 740 | int hdr; /* Offset to the page header */ |
| 741 | int size; /* Size of a cell */ |
| 742 | int usableSize; /* Number of usable bytes on a page */ |
| 743 | int cellOffset; /* Offset to the cell pointer array */ |
| 744 | int brk; /* Offset to the cell content area */ |
| 745 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 746 | unsigned char *data; /* The page data */ |
| 747 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 748 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 749 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 750 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 751 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 752 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 753 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 754 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 755 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 756 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 757 | cellOffset = pPage->cellOffset; |
| 758 | nCell = pPage->nCell; |
| 759 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 760 | usableSize = pPage->pBt->usableSize; |
| 761 | brk = get2byte(&data[hdr+5]); |
| 762 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 763 | brk = usableSize; |
| 764 | for(i=0; i<nCell; i++){ |
| 765 | u8 *pAddr; /* The i-th cell pointer */ |
| 766 | pAddr = &data[cellOffset + i*2]; |
| 767 | pc = get2byte(pAddr); |
| 768 | assert( pc<pPage->pBt->usableSize ); |
| 769 | size = cellSizePtr(pPage, &temp[pc]); |
| 770 | brk -= size; |
| 771 | memcpy(&data[brk], &temp[pc], size); |
| 772 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 773 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 774 | assert( brk>=cellOffset+2*nCell ); |
| 775 | put2byte(&data[hdr+5], brk); |
| 776 | data[hdr+1] = 0; |
| 777 | data[hdr+2] = 0; |
| 778 | data[hdr+7] = 0; |
| 779 | addr = cellOffset+2*nCell; |
| 780 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 781 | sqliteFree(temp); |
| 782 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 783 | } |
| 784 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 785 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 786 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 787 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 788 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 789 | ** the new allocation. Or return 0 if there is not enough free |
| 790 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 791 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 792 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 793 | ** nBytes of contiguous free space, then this routine automatically |
| 794 | ** calls defragementPage() to consolidate all free space before |
| 795 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 796 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 797 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 798 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 799 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 800 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 801 | int top; |
| 802 | int nCell; |
| 803 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 804 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 805 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 806 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 807 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 808 | assert( pPage->pBt ); |
| 809 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 810 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 811 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 812 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 813 | |
| 814 | nFrag = data[hdr+7]; |
| 815 | if( nFrag<60 ){ |
| 816 | /* Search the freelist looking for a slot big enough to satisfy the |
| 817 | ** space request. */ |
| 818 | addr = hdr+1; |
| 819 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 820 | size = get2byte(&data[pc+2]); |
| 821 | if( size>=nByte ){ |
| 822 | if( size<nByte+4 ){ |
| 823 | memcpy(&data[addr], &data[pc], 2); |
| 824 | data[hdr+7] = nFrag + size - nByte; |
| 825 | return pc; |
| 826 | }else{ |
| 827 | put2byte(&data[pc+2], size-nByte); |
| 828 | return pc + size - nByte; |
| 829 | } |
| 830 | } |
| 831 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 834 | |
| 835 | /* Allocate memory from the gap in between the cell pointer array |
| 836 | ** and the cell content area. |
| 837 | */ |
| 838 | top = get2byte(&data[hdr+5]); |
| 839 | nCell = get2byte(&data[hdr+3]); |
| 840 | cellOffset = pPage->cellOffset; |
| 841 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 842 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 843 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 844 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 845 | top -= nByte; |
| 846 | assert( cellOffset + 2*nCell <= top ); |
| 847 | put2byte(&data[hdr+5], top); |
| 848 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 852 | ** Return a section of the pPage->aData to the freelist. |
| 853 | ** The first byte of the new free block is pPage->aDisk[start] |
| 854 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 855 | ** |
| 856 | ** Most of the effort here is involved in coalesing adjacent |
| 857 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 858 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 859 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 860 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 861 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 862 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 863 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 864 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 865 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 866 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 867 | if( size<4 ) size = 4; |
| 868 | |
| 869 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 870 | hdr = pPage->hdrOffset; |
| 871 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 872 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 873 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 874 | assert( pbegin>addr ); |
| 875 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 876 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 877 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 878 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 879 | put2byte(&data[addr], start); |
| 880 | put2byte(&data[start], pbegin); |
| 881 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 882 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 883 | |
| 884 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 885 | addr = pPage->hdrOffset + 1; |
| 886 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 887 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 888 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 889 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 890 | pnext = get2byte(&data[pbegin]); |
| 891 | psize = get2byte(&data[pbegin+2]); |
| 892 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 893 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 894 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 895 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 896 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 897 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 898 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 899 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 902 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 903 | /* If the cell content area begins with a freeblock, remove it. */ |
| 904 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 905 | int top; |
| 906 | pbegin = get2byte(&data[hdr+1]); |
| 907 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 908 | top = get2byte(&data[hdr+5]); |
| 909 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 910 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 914 | ** Decode the flags byte (the first byte of the header) for a page |
| 915 | ** and initialize fields of the MemPage structure accordingly. |
| 916 | */ |
| 917 | static void decodeFlags(MemPage *pPage, int flagByte){ |
| 918 | Btree *pBt; /* A copy of pPage->pBt */ |
| 919 | |
| 920 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 921 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 922 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 923 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 924 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 925 | pBt = pPage->pBt; |
| 926 | if( flagByte & PTF_LEAFDATA ){ |
| 927 | pPage->leafData = 1; |
| 928 | pPage->maxLocal = pBt->maxLeaf; |
| 929 | pPage->minLocal = pBt->minLeaf; |
| 930 | }else{ |
| 931 | pPage->leafData = 0; |
| 932 | pPage->maxLocal = pBt->maxLocal; |
| 933 | pPage->minLocal = pBt->minLocal; |
| 934 | } |
| 935 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 936 | } |
| 937 | |
| 938 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 939 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 940 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 941 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 942 | ** is the parent of the page being initialized. The root of a |
| 943 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 944 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 945 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 946 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 947 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 948 | ** guarantee that the page is well-formed. It only shows that |
| 949 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 950 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 951 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 952 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 953 | MemPage *pParent /* The parent. Might be NULL */ |
| 954 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 955 | int pc; /* Address of a freeblock within pPage->aData[] */ |
| 956 | int i; /* Loop counter */ |
| 957 | int hdr; /* Offset to beginning of page header */ |
| 958 | u8 *data; /* Equal to pPage->aData */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 959 | Btree *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 960 | int usableSize; /* Amount of usable space on each page */ |
| 961 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 962 | int nFree; /* Number of unused bytes on the page */ |
| 963 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 964 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 965 | pBt = pPage->pBt; |
| 966 | assert( pBt!=0 ); |
| 967 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 968 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 969 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->psAligned] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 970 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 971 | /* The parent page should never change unless the file is corrupt */ |
| 972 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 973 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 974 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 975 | if( pPage->pParent==0 && pParent!=0 ){ |
| 976 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 977 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 978 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 979 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 980 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 981 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 982 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 983 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 984 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 985 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 986 | top = get2byte(&data[hdr+5]); |
| 987 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 988 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 989 | /* To many cells for a single page. The page must be corrupt */ |
| 990 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 991 | } |
| 992 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 993 | /* All pages must have at least one cell, except for root pages */ |
| 994 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 995 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 996 | |
| 997 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 998 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 999 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1000 | i = 0; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1001 | while( pc>0 ){ |
| 1002 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1003 | if( pc>usableSize-4 ){ |
| 1004 | /* Free block is off the page */ |
| 1005 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1006 | } |
| 1007 | if( i++>SQLITE_MAX_PAGE_SIZE/4 ){ |
| 1008 | /* The free block list forms an infinite loop */ |
| 1009 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1010 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1011 | next = get2byte(&data[pc]); |
| 1012 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1013 | if( next>0 && next<=pc+size+3 ){ |
| 1014 | /* Free blocks must be in accending order */ |
| 1015 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1016 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1017 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1018 | pc = next; |
| 1019 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1020 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1021 | if( nFree>=usableSize ){ |
| 1022 | /* Free space cannot exceed total page size */ |
| 1023 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1024 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1025 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1026 | pPage->isInit = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1027 | pageIntegrity(pPage); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1028 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1032 | ** Set up a raw page so that it looks like a database page holding |
| 1033 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1034 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1035 | static void zeroPage(MemPage *pPage, int flags){ |
| 1036 | unsigned char *data = pPage->aData; |
| 1037 | Btree *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1038 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1039 | int first; |
| 1040 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1041 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1042 | assert( &data[pBt->psAligned] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1043 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1044 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1045 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1046 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1047 | memset(&data[hdr+1], 0, 4); |
| 1048 | data[hdr+7] = 0; |
| 1049 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1050 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1051 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1052 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1053 | pPage->cellOffset = first; |
| 1054 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1055 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1056 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1057 | pPage->isInit = 1; |
| 1058 | pageIntegrity(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1062 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1063 | ** MemPage.aData elements if needed. |
| 1064 | */ |
| 1065 | static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){ |
| 1066 | int rc; |
| 1067 | unsigned char *aData; |
| 1068 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1069 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1070 | if( rc ) return rc; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1071 | pPage = (MemPage*)&aData[pBt->psAligned]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1072 | pPage->aData = aData; |
| 1073 | pPage->pBt = pBt; |
| 1074 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1075 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1076 | *ppPage = pPage; |
| 1077 | return SQLITE_OK; |
| 1078 | } |
| 1079 | |
| 1080 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1081 | ** Get a page from the pager and initialize it. This routine |
| 1082 | ** is just a convenience wrapper around separate calls to |
| 1083 | ** getPage() and initPage(). |
| 1084 | */ |
| 1085 | static int getAndInitPage( |
| 1086 | Btree *pBt, /* The database file */ |
| 1087 | Pgno pgno, /* Number of the page to get */ |
| 1088 | MemPage **ppPage, /* Write the page pointer here */ |
| 1089 | MemPage *pParent /* Parent of the page */ |
| 1090 | ){ |
| 1091 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1092 | if( pgno==0 ){ |
| 1093 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1094 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1095 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1096 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1097 | rc = initPage(*ppPage, pParent); |
| 1098 | } |
| 1099 | return rc; |
| 1100 | } |
| 1101 | |
| 1102 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1103 | ** Release a MemPage. This should be called once for each prior |
| 1104 | ** call to getPage. |
| 1105 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1106 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1107 | if( pPage ){ |
| 1108 | assert( pPage->aData ); |
| 1109 | assert( pPage->pBt ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1110 | assert( &pPage->aData[pPage->pBt->psAligned]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1111 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1116 | ** This routine is called when the reference count for a page |
| 1117 | ** reaches zero. We need to unref the pParent pointer when that |
| 1118 | ** happens. |
| 1119 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1120 | static void pageDestructor(void *pData, int pageSize){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1121 | MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)]; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1122 | if( pPage->pParent ){ |
| 1123 | MemPage *pParent = pPage->pParent; |
| 1124 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1125 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1126 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1127 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1131 | ** During a rollback, when the pager reloads information into the cache |
| 1132 | ** so that the cache is restored to its original state at the start of |
| 1133 | ** the transaction, for each page restored this routine is called. |
| 1134 | ** |
| 1135 | ** This routine needs to reset the extra data section at the end of the |
| 1136 | ** page to agree with the restored data. |
| 1137 | */ |
| 1138 | static void pageReinit(void *pData, int pageSize){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1139 | MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)]; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1140 | if( pPage->isInit ){ |
| 1141 | pPage->isInit = 0; |
| 1142 | initPage(pPage, pPage->pParent); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1147 | ** Open a database file. |
| 1148 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1149 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1150 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1151 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1152 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1153 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1154 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 1155 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1156 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1157 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1158 | Btree *pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1159 | int rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1160 | int nReserve; |
| 1161 | unsigned char zDbHeader[100]; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1162 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1163 | /* |
| 1164 | ** The following asserts make sure that structures used by the btree are |
| 1165 | ** the right size. This is to guard against size changes that result |
| 1166 | ** when compiling on a different architecture. |
| 1167 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1168 | assert( sizeof(i64)==8 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1169 | assert( sizeof(u64)==8 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1170 | assert( sizeof(u32)==4 ); |
| 1171 | assert( sizeof(u16)==2 ); |
| 1172 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1173 | assert( sizeof(ptr)==sizeof(char*) ); |
| 1174 | assert( sizeof(uptr)==sizeof(ptr) ); |
| 1175 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1176 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1177 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1178 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1179 | return SQLITE_NOMEM; |
| 1180 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1181 | rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, |
| 1182 | (flags & BTREE_OMIT_JOURNAL)==0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1183 | if( rc!=SQLITE_OK ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1184 | if( pBt->pPager ) sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1185 | sqliteFree(pBt); |
| 1186 | *ppBtree = 0; |
| 1187 | return rc; |
| 1188 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1189 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1190 | sqlite3pager_set_reiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1191 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1192 | pBt->pPage1 = 0; |
| 1193 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1194 | sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader); |
| 1195 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1196 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){ |
| 1197 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1198 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1199 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1200 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1201 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1202 | if( zFilename && strcmp(zFilename,":memory:") ){ |
| 1203 | pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM; |
| 1204 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1205 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1206 | nReserve = 0; |
| 1207 | }else{ |
| 1208 | nReserve = zDbHeader[20]; |
| 1209 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1210 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1211 | pBt->minLeafFrac = zDbHeader[23]; |
| 1212 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1213 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1214 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1215 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1216 | } |
| 1217 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1218 | pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1219 | sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1220 | *ppBtree = pBt; |
| 1221 | return SQLITE_OK; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | ** Close an open database and invalidate all cursors. |
| 1226 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1227 | int sqlite3BtreeClose(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1228 | while( pBt->pCursor ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1229 | sqlite3BtreeCloseCursor(pBt->pCursor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1230 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1231 | sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1232 | sqliteFree(pBt); |
| 1233 | return SQLITE_OK; |
| 1234 | } |
| 1235 | |
| 1236 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1237 | ** Change the busy handler callback function. |
| 1238 | */ |
| 1239 | int sqlite3BtreeSetBusyHandler(Btree *pBt, BusyHandler *pHandler){ |
| 1240 | sqlite3pager_set_busyhandler(pBt->pPager, pHandler); |
| 1241 | return SQLITE_OK; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1245 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1246 | ** |
| 1247 | ** The maximum number of cache pages is set to the absolute |
| 1248 | ** value of mxPage. If mxPage is negative, the pager will |
| 1249 | ** operate asynchronously - it will not stop to do fsync()s |
| 1250 | ** to insure data is written to the disk surface before |
| 1251 | ** continuing. Transactions still work if synchronous is off, |
| 1252 | ** and the database cannot be corrupted if this program |
| 1253 | ** crashes. But if the operating system crashes or there is |
| 1254 | ** an abrupt power failure when synchronous is off, the database |
| 1255 | ** could be left in an inconsistent and unrecoverable state. |
| 1256 | ** Synchronous is on by default so database corruption is not |
| 1257 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1258 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1259 | int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1260 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1261 | return SQLITE_OK; |
| 1262 | } |
| 1263 | |
| 1264 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1265 | ** Change the way data is synced to disk in order to increase or decrease |
| 1266 | ** how well the database resists damage due to OS crashes and power |
| 1267 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1268 | ** there is a high probability of damage) Level 2 is the default. There |
| 1269 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1270 | ** probability of damage to near zero but with a write performance reduction. |
| 1271 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1272 | int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1273 | sqlite3pager_set_safety_level(pBt->pPager, level); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1274 | return SQLITE_OK; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1278 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1279 | ** |
| 1280 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1281 | ** size supplied does not meet this constraint then the page size is not |
| 1282 | ** changed. |
| 1283 | ** |
| 1284 | ** Page sizes are constrained to be a power of two so that the region |
| 1285 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1286 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1287 | ** at the beginning of a page. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1288 | */ |
| 1289 | int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){ |
| 1290 | if( pBt->pageSizeFixed ){ |
| 1291 | return SQLITE_READONLY; |
| 1292 | } |
| 1293 | if( nReserve<0 ){ |
| 1294 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1295 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1296 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1297 | ((pageSize-1)&pageSize)==0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1298 | pBt->pageSize = pageSize; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1299 | pBt->psAligned = FORCE_ALIGNMENT(pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1300 | sqlite3pager_set_pagesize(pBt->pPager, pageSize); |
| 1301 | } |
| 1302 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1303 | return SQLITE_OK; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | ** Return the currently defined page size |
| 1308 | */ |
| 1309 | int sqlite3BtreeGetPageSize(Btree *pBt){ |
| 1310 | return pBt->pageSize; |
| 1311 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1312 | int sqlite3BtreeGetReserve(Btree *pBt){ |
| 1313 | return pBt->pageSize - pBt->usableSize; |
| 1314 | } |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1317 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1318 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1319 | ** is disabled. The default value for the auto-vacuum property is |
| 1320 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1321 | */ |
| 1322 | int sqlite3BtreeSetAutoVacuum(Btree *pBt, int autoVacuum){ |
| 1323 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1324 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1325 | #else |
| 1326 | if( pBt->pageSizeFixed ){ |
| 1327 | return SQLITE_READONLY; |
| 1328 | } |
| 1329 | pBt->autoVacuum = (autoVacuum?1:0); |
| 1330 | return SQLITE_OK; |
| 1331 | #endif |
| 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1336 | ** enabled 1 is returned. Otherwise 0. |
| 1337 | */ |
| 1338 | int sqlite3BtreeGetAutoVacuum(Btree *pBt){ |
| 1339 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 1340 | return 0; |
| 1341 | #else |
| 1342 | return pBt->autoVacuum; |
| 1343 | #endif |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1348 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1349 | ** also acquire a readlock on that file. |
| 1350 | ** |
| 1351 | ** SQLITE_OK is returned on success. If the file is not a |
| 1352 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1353 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1354 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1355 | ** if there is a locking protocol violation. |
| 1356 | */ |
| 1357 | static int lockBtree(Btree *pBt){ |
| 1358 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1359 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1360 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1361 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1362 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1363 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* Do some checking to help insure the file we opened really is |
| 1366 | ** a valid database file. |
| 1367 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1368 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1369 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1370 | u8 *page1 = pPage1->aData; |
| 1371 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1372 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1373 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1374 | if( page1[18]>1 || page1[19]>1 ){ |
| 1375 | goto page1_init_failed; |
| 1376 | } |
| 1377 | pBt->pageSize = get2byte(&page1[16]); |
| 1378 | pBt->usableSize = pBt->pageSize - page1[20]; |
| 1379 | if( pBt->usableSize<500 ){ |
| 1380 | goto page1_init_failed; |
| 1381 | } |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1382 | pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1383 | pBt->maxEmbedFrac = page1[21]; |
| 1384 | pBt->minEmbedFrac = page1[22]; |
| 1385 | pBt->minLeafFrac = page1[23]; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1386 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1387 | |
| 1388 | /* maxLocal is the maximum amount of payload to store locally for |
| 1389 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1390 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1391 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1392 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1393 | ** 4-byte child pointer |
| 1394 | ** 9-byte nKey value |
| 1395 | ** 4-byte nData value |
| 1396 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1397 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1398 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1399 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1400 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1401 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1402 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1403 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1404 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1405 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1406 | goto page1_init_failed; |
| 1407 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1408 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1409 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1410 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1411 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1412 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1413 | releasePage(pPage1); |
| 1414 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1415 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1419 | ** If there are no outstanding cursors and we are not in the middle |
| 1420 | ** of a transaction but there is a read lock on the database, then |
| 1421 | ** this routine unrefs the first page of the database file which |
| 1422 | ** has the effect of releasing the read lock. |
| 1423 | ** |
| 1424 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1425 | ** |
| 1426 | ** If there is a transaction in progress, this routine is a no-op. |
| 1427 | */ |
| 1428 | static void unlockBtreeIfUnused(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1429 | if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1430 | if( pBt->pPage1->aData==0 ){ |
| 1431 | MemPage *pPage = pBt->pPage1; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1432 | pPage->aData = &((char*)pPage)[-pBt->psAligned]; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1433 | pPage->pBt = pBt; |
| 1434 | pPage->pgno = 1; |
| 1435 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1436 | releasePage(pBt->pPage1); |
| 1437 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1438 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1443 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1444 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1445 | */ |
| 1446 | static int newDatabase(Btree *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1447 | MemPage *pP1; |
| 1448 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1449 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1450 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1451 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1452 | assert( pP1!=0 ); |
| 1453 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1454 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1455 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1456 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1457 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1458 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1459 | data[18] = 1; |
| 1460 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1461 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1462 | data[21] = pBt->maxEmbedFrac; |
| 1463 | data[22] = pBt->minEmbedFrac; |
| 1464 | data[23] = pBt->minLeafFrac; |
| 1465 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1466 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1467 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1468 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1469 | if( pBt->autoVacuum ){ |
| 1470 | put4byte(&data[36 + 4*4], 1); |
| 1471 | } |
| 1472 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1473 | return SQLITE_OK; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1477 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1478 | ** is started if the second argument is nonzero, otherwise a read- |
| 1479 | ** transaction. If the second argument is 2 or more and exclusive |
| 1480 | ** transaction is started, meaning that no other process is allowed |
| 1481 | ** to access the database. A preexisting transaction may not be |
| 1482 | ** upgrade to exclusive by calling this routine a second time - the |
| 1483 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1484 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1485 | ** A write-transaction must be started before attempting any |
| 1486 | ** changes to the database. None of the following routines |
| 1487 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1488 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1489 | ** sqlite3BtreeCreateTable() |
| 1490 | ** sqlite3BtreeCreateIndex() |
| 1491 | ** sqlite3BtreeClearTable() |
| 1492 | ** sqlite3BtreeDropTable() |
| 1493 | ** sqlite3BtreeInsert() |
| 1494 | ** sqlite3BtreeDelete() |
| 1495 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1496 | ** |
| 1497 | ** If wrflag is true, then nMaster specifies the maximum length of |
| 1498 | ** a master journal file name supplied later via sqlite3BtreeSync(). |
| 1499 | ** This is so that appropriate space can be allocated in the journal file |
| 1500 | ** when it is created.. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1501 | */ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1502 | int sqlite3BtreeBeginTrans(Btree *pBt, int wrflag){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1503 | int rc = SQLITE_OK; |
| 1504 | |
| 1505 | /* If the btree is already in a write-transaction, or it |
| 1506 | ** is already in a read-transaction and a read-transaction |
| 1507 | ** is requested, this is a no-op. |
| 1508 | */ |
| 1509 | if( pBt->inTrans==TRANS_WRITE || |
| 1510 | (pBt->inTrans==TRANS_READ && !wrflag) ){ |
| 1511 | return SQLITE_OK; |
| 1512 | } |
| 1513 | if( pBt->readOnly && wrflag ){ |
| 1514 | return SQLITE_READONLY; |
| 1515 | } |
| 1516 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1517 | if( pBt->pPage1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1518 | rc = lockBtree(pBt); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | if( rc==SQLITE_OK && wrflag ){ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1522 | rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1523 | if( rc==SQLITE_OK ){ |
| 1524 | rc = newDatabase(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1525 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1526 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1527 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1528 | if( rc==SQLITE_OK ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1529 | pBt->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1530 | if( wrflag ) pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1531 | }else{ |
| 1532 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1533 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1534 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | /* |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1538 | ** The TRACE macro will print high-level status information about the |
| 1539 | ** btree operation when the global variable sqlite3_btree_trace is |
| 1540 | ** enabled. |
| 1541 | */ |
| 1542 | #if SQLITE_TEST |
| 1543 | # define TRACE(X) if( sqlite3_btree_trace )\ |
| 1544 | { sqlite3DebugPrintf X; fflush(stdout); } |
| 1545 | #else |
| 1546 | # define TRACE(X) |
| 1547 | #endif |
| 1548 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
| 1549 | |
| 1550 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1551 | |
| 1552 | /* |
| 1553 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1554 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1555 | ** map entries for the overflow pages as well. |
| 1556 | */ |
| 1557 | static int setChildPtrmaps(MemPage *pPage){ |
| 1558 | int i; /* Counter variable */ |
| 1559 | int nCell; /* Number of cells in page pPage */ |
| 1560 | int rc = SQLITE_OK; /* Return code */ |
| 1561 | Btree *pBt = pPage->pBt; |
| 1562 | int isInitOrig = pPage->isInit; |
| 1563 | Pgno pgno = pPage->pgno; |
| 1564 | |
| 1565 | initPage(pPage, 0); |
| 1566 | nCell = pPage->nCell; |
| 1567 | |
| 1568 | for(i=0; i<nCell; i++){ |
| 1569 | CellInfo info; |
| 1570 | u8 *pCell = findCell(pPage, i); |
| 1571 | |
| 1572 | parseCellPtr(pPage, pCell, &info); |
| 1573 | if( info.iOverflow ){ |
| 1574 | Pgno ovflPgno = get4byte(&pCell[info.iOverflow]); |
| 1575 | rc = ptrmapPut(pBt, ovflPgno, PTRMAP_OVERFLOW1, pgno); |
| 1576 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1577 | } |
| 1578 | if( !pPage->leaf ){ |
| 1579 | Pgno childPgno = get4byte(pCell); |
| 1580 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1581 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | if( !pPage->leaf ){ |
| 1586 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1587 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1588 | } |
| 1589 | |
| 1590 | set_child_ptrmaps_out: |
| 1591 | pPage->isInit = isInitOrig; |
| 1592 | return rc; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1597 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1598 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1599 | ** follows: |
| 1600 | ** |
| 1601 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1602 | ** page of pPage. |
| 1603 | ** |
| 1604 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1605 | ** page pointed to by one of the cells on pPage. |
| 1606 | ** |
| 1607 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1608 | ** overflow page in the list. |
| 1609 | */ |
| 1610 | static void modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
| 1611 | |
| 1612 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1613 | /* The pointer is always the first 4 bytes of the page in this case. */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1614 | assert( get4byte(pPage->aData)==iFrom ); |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1615 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1616 | }else{ |
| 1617 | int isInitOrig = pPage->isInit; |
| 1618 | int i; |
| 1619 | int nCell; |
| 1620 | |
| 1621 | initPage(pPage, 0); |
| 1622 | nCell = pPage->nCell; |
| 1623 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1624 | for(i=0; i<nCell; i++){ |
| 1625 | u8 *pCell = findCell(pPage, i); |
| 1626 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1627 | CellInfo info; |
| 1628 | parseCellPtr(pPage, pCell, &info); |
| 1629 | if( info.iOverflow ){ |
| 1630 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1631 | put4byte(&pCell[info.iOverflow], iTo); |
| 1632 | break; |
| 1633 | } |
| 1634 | } |
| 1635 | }else{ |
| 1636 | if( get4byte(pCell)==iFrom ){ |
| 1637 | put4byte(pCell, iTo); |
| 1638 | break; |
| 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | if( i==nCell ){ |
| 1644 | assert( eType==PTRMAP_BTREE ); |
| 1645 | assert( get4byte(&pPage->aData[pPage->hdrOffset+8])==iFrom ); |
| 1646 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1647 | } |
| 1648 | |
| 1649 | pPage->isInit = isInitOrig; |
| 1650 | } |
| 1651 | } |
| 1652 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1653 | |
| 1654 | static int relocatePage( |
| 1655 | Btree *pBt, |
| 1656 | MemPage *pDbPage, |
| 1657 | u8 eType, |
| 1658 | Pgno iPtrPage, |
| 1659 | Pgno iFreePage |
| 1660 | ){ |
| 1661 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 1662 | Pgno iDbPage = pDbPage->pgno; |
| 1663 | Pager *pPager = pBt->pPager; |
| 1664 | int rc; |
| 1665 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1666 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 1667 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1668 | |
| 1669 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 1670 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 1671 | iDbPage, iFreePage, iPtrPage, eType)); |
| 1672 | rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage); |
| 1673 | if( rc!=SQLITE_OK ){ |
| 1674 | return rc; |
| 1675 | } |
| 1676 | pDbPage->pgno = iFreePage; |
| 1677 | |
| 1678 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 1679 | ** that point to overflow pages. The pointer map entries for all these |
| 1680 | ** pages need to be changed. |
| 1681 | ** |
| 1682 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 1683 | ** pointer to a subsequent overflow page. If this is the case, then |
| 1684 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 1685 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1686 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1687 | rc = setChildPtrmaps(pDbPage); |
| 1688 | if( rc!=SQLITE_OK ){ |
| 1689 | return rc; |
| 1690 | } |
| 1691 | }else{ |
| 1692 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 1693 | if( nextOvfl!=0 ){ |
| 1694 | assert( nextOvfl<=sqlite3pager_pagecount(pPager) ); |
| 1695 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 1696 | if( rc!=SQLITE_OK ){ |
| 1697 | return rc; |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 1703 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 1704 | ** iPtrPage. |
| 1705 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1706 | if( eType!=PTRMAP_ROOTPAGE ){ |
| 1707 | rc = getPage(pBt, iPtrPage, &pPtrPage); |
| 1708 | if( rc!=SQLITE_OK ){ |
| 1709 | return rc; |
| 1710 | } |
| 1711 | rc = sqlite3pager_write(pPtrPage->aData); |
| 1712 | if( rc!=SQLITE_OK ){ |
| 1713 | releasePage(pPtrPage); |
| 1714 | return rc; |
| 1715 | } |
| 1716 | modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
| 1717 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1718 | releasePage(pPtrPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1719 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1720 | return rc; |
| 1721 | } |
| 1722 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1723 | /* Forward declaration required by autoVacuumCommit(). */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 1724 | static int allocatePage(Btree *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1725 | |
| 1726 | /* |
| 1727 | ** This routine is called prior to sqlite3pager_commit when a transaction |
| 1728 | ** is commited for an auto-vacuum database. |
| 1729 | */ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1730 | static int autoVacuumCommit(Btree *pBt, Pgno *nTrunc){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1731 | Pager *pPager = pBt->pPager; |
| 1732 | Pgno nFreeList; /* Number of pages remaining on the free-list. */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1733 | int nPtrMap; /* Number of pointer-map pages deallocated */ |
| 1734 | Pgno origSize; /* Pages in the database file */ |
| 1735 | Pgno finSize; /* Pages in the database file after truncation */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1736 | int rc; /* Return code */ |
| 1737 | u8 eType; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1738 | int pgsz = pBt->pageSize; /* Page size for this database */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1739 | Pgno iDbPage; /* The database page to move */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1740 | MemPage *pDbMemPage = 0; /* "" */ |
| 1741 | Pgno iPtrPage; /* The page that contains a pointer to iDbPage */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1742 | Pgno iFreePage; /* The free-list page to move iDbPage to */ |
| 1743 | MemPage *pFreeMemPage = 0; /* "" */ |
| 1744 | |
| 1745 | #ifndef NDEBUG |
| 1746 | int nRef = *sqlite3pager_stats(pPager); |
| 1747 | #endif |
| 1748 | |
| 1749 | assert( pBt->autoVacuum ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1750 | assert( 0==PTRMAP_ISPAGE(pgsz, sqlite3pager_pagecount(pPager)) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1751 | |
| 1752 | /* Figure out how many free-pages are in the database. If there are no |
| 1753 | ** free pages, then auto-vacuum is a no-op. |
| 1754 | */ |
| 1755 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1756 | if( nFreeList==0 ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1757 | *nTrunc = 0; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1758 | return SQLITE_OK; |
| 1759 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1760 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1761 | origSize = sqlite3pager_pagecount(pPager); |
| 1762 | nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pgsz, origSize)+pgsz/5)/(pgsz/5); |
| 1763 | finSize = origSize - nFreeList - nPtrMap; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 1764 | if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){ |
| 1765 | finSize--; |
| 1766 | if( PTRMAP_ISPAGE(pBt->pageSize, finSize) ){ |
| 1767 | finSize--; |
| 1768 | } |
| 1769 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1770 | TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize)); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1771 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1772 | /* Variable 'finSize' will be the size of the file in pages after |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1773 | ** the auto-vacuum has completed (the current file size minus the number |
| 1774 | ** of pages on the free list). Loop through the pages that lie beyond |
| 1775 | ** this mark, and if they are not already on the free list, move them |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1776 | ** to a free page earlier in the file (somewhere before finSize). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1777 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1778 | for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 1779 | /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */ |
| 1780 | if( PTRMAP_ISPAGE(pgsz, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){ |
| 1781 | continue; |
| 1782 | } |
| 1783 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1784 | rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage); |
| 1785 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 1786 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 1787 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 1788 | /* If iDbPage is free, do not swap it. */ |
| 1789 | if( eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1790 | continue; |
| 1791 | } |
| 1792 | rc = getPage(pBt, iDbPage, &pDbMemPage); |
| 1793 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1794 | |
| 1795 | /* Find the next page in the free-list that is not already at the end |
| 1796 | ** of the file. A page can be pulled off the free list using the |
| 1797 | ** allocatePage() routine. |
| 1798 | */ |
| 1799 | do{ |
| 1800 | if( pFreeMemPage ){ |
| 1801 | releasePage(pFreeMemPage); |
| 1802 | pFreeMemPage = 0; |
| 1803 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 1804 | rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1805 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1806 | assert( iFreePage<=origSize ); |
| 1807 | }while( iFreePage>finSize ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1808 | releasePage(pFreeMemPage); |
| 1809 | pFreeMemPage = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1810 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1811 | rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1812 | releasePage(pDbMemPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1813 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /* The entire free-list has been swapped to the end of the file. So |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1817 | ** truncate the database file to finSize pages and consider the |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1818 | ** free-list empty. |
| 1819 | */ |
| 1820 | rc = sqlite3pager_write(pBt->pPage1->aData); |
| 1821 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 1822 | put4byte(&pBt->pPage1->aData[32], 0); |
| 1823 | put4byte(&pBt->pPage1->aData[36], 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1824 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1825 | *nTrunc = finSize; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1826 | |
| 1827 | autovacuum_out: |
| 1828 | /* TODO: A goto autovacuum_out; will fail to call releasePage() on |
| 1829 | ** outstanding references. Fix. |
| 1830 | */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1831 | assert( nRef==*sqlite3pager_stats(pPager) ); |
| 1832 | if( rc!=SQLITE_OK ){ |
| 1833 | sqlite3pager_rollback(pPager); |
| 1834 | } |
| 1835 | return rc; |
| 1836 | } |
| 1837 | #endif |
| 1838 | |
| 1839 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1840 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1841 | ** |
| 1842 | ** This will release the write lock on the database file. If there |
| 1843 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1844 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1845 | int sqlite3BtreeCommit(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1846 | int rc = SQLITE_OK; |
| 1847 | if( pBt->inTrans==TRANS_WRITE ){ |
| 1848 | rc = sqlite3pager_commit(pBt->pPager); |
| 1849 | } |
| 1850 | pBt->inTrans = TRANS_NONE; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1851 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1852 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1853 | return rc; |
| 1854 | } |
| 1855 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1856 | #ifndef NDEBUG |
| 1857 | /* |
| 1858 | ** Return the number of write-cursors open on this handle. This is for use |
| 1859 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 1860 | ** defined. |
| 1861 | */ |
| 1862 | static int countWriteCursors(Btree *pBt){ |
| 1863 | BtCursor *pCur; |
| 1864 | int r = 0; |
| 1865 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1866 | if( pCur->wrFlag ) r++; |
| 1867 | } |
| 1868 | return r; |
| 1869 | } |
| 1870 | #endif |
| 1871 | |
| 1872 | #if 0 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1873 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1874 | ** Invalidate all cursors |
| 1875 | */ |
| 1876 | static void invalidateCursors(Btree *pBt){ |
| 1877 | BtCursor *pCur; |
| 1878 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1879 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1880 | if( pPage /* && !pPage->isInit */ ){ |
| 1881 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1882 | releasePage(pPage); |
| 1883 | pCur->pPage = 0; |
| 1884 | pCur->isValid = 0; |
| 1885 | pCur->status = SQLITE_ABORT; |
| 1886 | } |
| 1887 | } |
| 1888 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1889 | #endif |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1890 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1891 | #ifdef SQLITE_TEST |
| 1892 | /* |
| 1893 | ** Print debugging information about all cursors to standard output. |
| 1894 | */ |
| 1895 | void sqlite3BtreeCursorList(Btree *pBt){ |
| 1896 | BtCursor *pCur; |
| 1897 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1898 | MemPage *pPage = pCur->pPage; |
| 1899 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 1900 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 1901 | pCur, pCur->pgnoRoot, zMode, |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1902 | pPage ? pPage->pgno : 0, pCur->idx, |
| 1903 | pCur->isValid ? "" : " eof" |
| 1904 | ); |
| 1905 | } |
| 1906 | } |
| 1907 | #endif |
| 1908 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1909 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1910 | ** Rollback the transaction in progress. All cursors will be |
| 1911 | ** invalided by this operation. Any attempt to use a cursor |
| 1912 | ** that was open at the beginning of this operation will result |
| 1913 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1914 | ** |
| 1915 | ** This will release the write lock on the database file. If there |
| 1916 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1917 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1918 | int sqlite3BtreeRollback(Btree *pBt){ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1919 | int rc = SQLITE_OK; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1920 | MemPage *pPage1; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1921 | if( pBt->inTrans==TRANS_WRITE ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1922 | rc = sqlite3pager_rollback(pBt->pPager); |
| 1923 | /* The rollback may have destroyed the pPage1->aData value. So |
| 1924 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 1925 | ** set correctly. */ |
| 1926 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 1927 | releasePage(pPage1); |
| 1928 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1929 | assert( countWriteCursors(pBt)==0 ); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1930 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1931 | pBt->inTrans = TRANS_NONE; |
| 1932 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1933 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1934 | return rc; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1938 | ** Start a statement subtransaction. The subtransaction can |
| 1939 | ** can be rolled back independently of the main transaction. |
| 1940 | ** You must start a transaction before starting a subtransaction. |
| 1941 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1942 | ** commits or rolls back. |
| 1943 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1944 | ** Only one subtransaction may be active at a time. It is an error to try |
| 1945 | ** to start a new subtransaction if another subtransaction is already active. |
| 1946 | ** |
| 1947 | ** Statement subtransactions are used around individual SQL statements |
| 1948 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 1949 | ** error occurs within the statement, the effect of that one statement |
| 1950 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1951 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1952 | int sqlite3BtreeBeginStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1953 | int rc; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1954 | if( (pBt->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1955 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 1956 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1957 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1958 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1959 | return rc; |
| 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1964 | ** Commit the statment subtransaction currently in progress. If no |
| 1965 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1966 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1967 | int sqlite3BtreeCommitStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1968 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1969 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1970 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1971 | }else{ |
| 1972 | rc = SQLITE_OK; |
| 1973 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1974 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1975 | return rc; |
| 1976 | } |
| 1977 | |
| 1978 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1979 | ** Rollback the active statement subtransaction. If no subtransaction |
| 1980 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1981 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1982 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1983 | ** to use a cursor that was open at the beginning of this operation |
| 1984 | ** will result in an error. |
| 1985 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1986 | int sqlite3BtreeRollbackStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1987 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1988 | if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1989 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1990 | assert( countWriteCursors(pBt)==0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1991 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1992 | return rc; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1996 | ** Default key comparison function to be used if no comparison function |
| 1997 | ** is specified on the sqlite3BtreeCursor() call. |
| 1998 | */ |
| 1999 | static int dfltCompare( |
| 2000 | void *NotUsed, /* User data is not used */ |
| 2001 | int n1, const void *p1, /* First key to compare */ |
| 2002 | int n2, const void *p2 /* Second key to compare */ |
| 2003 | ){ |
| 2004 | int c; |
| 2005 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2006 | if( c==0 ){ |
| 2007 | c = n1 - n2; |
| 2008 | } |
| 2009 | return c; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2013 | ** Create a new cursor for the BTree whose root is on the page |
| 2014 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2015 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2016 | ** |
| 2017 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2018 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2019 | ** writing if other conditions for writing are also met. These |
| 2020 | ** are the conditions that must be met in order for writing to |
| 2021 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2022 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2023 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2024 | ** |
| 2025 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 2026 | ** |
| 2027 | ** 3: The database must be writable (not on read-only media) |
| 2028 | ** |
| 2029 | ** 4: There must be an active transaction. |
| 2030 | ** |
| 2031 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 2032 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 2033 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 2034 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 2035 | ** change as long as the cursor is open. This allows the cursor to |
| 2036 | ** do a sequential scan of the table without having to worry about |
| 2037 | ** entries being inserted or deleted during the scan. Cursors should |
| 2038 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 2039 | ** 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] | 2040 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2041 | ** should be opened with wrFlag==1 even if they never really intend |
| 2042 | ** to write. |
| 2043 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2044 | ** No checking is done to make sure that page iTable really is the |
| 2045 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2046 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2047 | ** |
| 2048 | ** The comparison function must be logically the same for every cursor |
| 2049 | ** on a particular table. Changing the comparison function will result |
| 2050 | ** in incorrect operations. If the comparison function is NULL, a |
| 2051 | ** default comparison function is used. The comparison function is |
| 2052 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2053 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2054 | int sqlite3BtreeCursor( |
| 2055 | Btree *pBt, /* The btree */ |
| 2056 | int iTable, /* Root page of table to open */ |
| 2057 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2058 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2059 | void *pArg, /* First arg to xCompare() */ |
| 2060 | BtCursor **ppCur /* Write new cursor here */ |
| 2061 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2062 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2063 | BtCursor *pCur; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2064 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2065 | *ppCur = 0; |
| 2066 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2067 | if( pBt->readOnly ){ |
| 2068 | return SQLITE_READONLY; |
| 2069 | } |
| 2070 | if( checkReadLocks(pBt, iTable, 0) ){ |
| 2071 | return SQLITE_LOCKED; |
| 2072 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2073 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2074 | if( pBt->pPage1==0 ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2075 | rc = lockBtree(pBt); |
| 2076 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2077 | return rc; |
| 2078 | } |
| 2079 | } |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 2080 | pCur = sqliteMallocRaw( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2081 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2082 | rc = SQLITE_NOMEM; |
| 2083 | goto create_cursor_exception; |
| 2084 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2085 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2086 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 2087 | rc = SQLITE_EMPTY; |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 2088 | pCur->pPage = 0; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2089 | goto create_cursor_exception; |
| 2090 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2091 | pCur->pPage = 0; /* For exit-handler, in case getAndInitPage() fails. */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2092 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2093 | if( rc!=SQLITE_OK ){ |
| 2094 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2095 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2096 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2097 | pCur->pArg = pArg; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2098 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2099 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2100 | pCur->idx = 0; |
drh | 59eb676 | 2004-06-13 23:07:04 +0000 | [diff] [blame] | 2101 | memset(&pCur->info, 0, sizeof(pCur->info)); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2102 | pCur->pNext = pBt->pCursor; |
| 2103 | if( pCur->pNext ){ |
| 2104 | pCur->pNext->pPrev = pCur; |
| 2105 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2106 | pCur->pPrev = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2107 | pBt->pCursor = pCur; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2108 | pCur->isValid = 0; |
| 2109 | pCur->status = SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2110 | *ppCur = pCur; |
| 2111 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2112 | |
| 2113 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2114 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2115 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2116 | sqliteFree(pCur); |
| 2117 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2118 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2119 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2122 | #if 0 /* Not Used */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2123 | /* |
| 2124 | ** Change the value of the comparison function used by a cursor. |
| 2125 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2126 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2127 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 2128 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 2129 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2130 | ){ |
| 2131 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2132 | pCur->pArg = pArg; |
| 2133 | } |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2134 | #endif |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2135 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2136 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2137 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2138 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2139 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2140 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2141 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2142 | if( pCur->pPrev ){ |
| 2143 | pCur->pPrev->pNext = pCur->pNext; |
| 2144 | }else{ |
| 2145 | pBt->pCursor = pCur->pNext; |
| 2146 | } |
| 2147 | if( pCur->pNext ){ |
| 2148 | pCur->pNext->pPrev = pCur->pPrev; |
| 2149 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2150 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2151 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2152 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2153 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2156 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2157 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2158 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2159 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2160 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2161 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2162 | pTempCur->pNext = 0; |
| 2163 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2164 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2165 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2166 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2170 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2171 | ** function above. |
| 2172 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2173 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2174 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2175 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2176 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2180 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2181 | ** If it is not already valid, call parseCell() to fill it in. |
| 2182 | ** |
| 2183 | ** BtCursor.info is a cache of the information in the current cell. |
| 2184 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2185 | */ |
| 2186 | static void getCellInfo(BtCursor *pCur){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2187 | if( pCur->info.nSize==0 ){ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2188 | parseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2189 | }else{ |
| 2190 | #ifndef NDEBUG |
| 2191 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2192 | memset(&info, 0, sizeof(info)); |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2193 | parseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2194 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 2195 | #endif |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2200 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2201 | ** the key for the current entry. If the cursor is not pointing |
| 2202 | ** to a valid entry, *pSize is set to 0. |
| 2203 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2204 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2205 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2206 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2207 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2208 | if( !pCur->isValid ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2209 | *pSize = 0; |
| 2210 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2211 | getCellInfo(pCur); |
| 2212 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2213 | } |
| 2214 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2215 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2216 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2217 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2218 | ** Set *pSize to the number of bytes of data in the entry the |
| 2219 | ** cursor currently points to. Always return SQLITE_OK. |
| 2220 | ** Failure is not possible. If the cursor is not currently |
| 2221 | ** pointing to an entry (which can happen, for example, if |
| 2222 | ** the database is empty) then *pSize is set to 0. |
| 2223 | */ |
| 2224 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2225 | if( !pCur->isValid ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2226 | /* Not pointing at a valid entry - set *pSize to 0. */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2227 | *pSize = 0; |
| 2228 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2229 | getCellInfo(pCur); |
| 2230 | *pSize = pCur->info.nData; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2231 | } |
| 2232 | return SQLITE_OK; |
| 2233 | } |
| 2234 | |
| 2235 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2236 | ** Read payload information from the entry that the pCur cursor is |
| 2237 | ** pointing to. Begin reading the payload at "offset" and read |
| 2238 | ** a total of "amt" bytes. Put the result in zBuf. |
| 2239 | ** |
| 2240 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2241 | ** It just reads bytes from the payload area. Data might appear |
| 2242 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2243 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2244 | static int getPayload( |
| 2245 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2246 | int offset, /* Begin reading this far into payload */ |
| 2247 | int amt, /* Read this many bytes */ |
| 2248 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 2249 | int skipKey /* offset begins at data if this is true */ |
| 2250 | ){ |
| 2251 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2252 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2253 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2254 | MemPage *pPage; |
| 2255 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2256 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2257 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2258 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2259 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2260 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2261 | pBt = pCur->pBt; |
| 2262 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2263 | pageIntegrity(pPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2264 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2265 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2266 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2267 | aPayload += pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2268 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2269 | nKey = 0; |
| 2270 | }else{ |
| 2271 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2272 | } |
| 2273 | assert( offset>=0 ); |
| 2274 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2275 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2276 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2277 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2278 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2279 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2280 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2281 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2282 | if( a+offset>pCur->info.nLocal ){ |
| 2283 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2284 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2285 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2286 | if( a==amt ){ |
| 2287 | return SQLITE_OK; |
| 2288 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2289 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2290 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2291 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2292 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2293 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2294 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2295 | ovflSize = pBt->usableSize - 4; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2296 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2297 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2298 | while( amt>0 && nextPage ){ |
| 2299 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
| 2300 | if( rc!=0 ){ |
| 2301 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2302 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2303 | nextPage = get4byte(aPayload); |
| 2304 | if( offset<ovflSize ){ |
| 2305 | int a = amt; |
| 2306 | if( a + offset > ovflSize ){ |
| 2307 | a = ovflSize - offset; |
| 2308 | } |
| 2309 | memcpy(pBuf, &aPayload[offset+4], a); |
| 2310 | offset = 0; |
| 2311 | amt -= a; |
| 2312 | pBuf += a; |
| 2313 | }else{ |
| 2314 | offset -= ovflSize; |
| 2315 | } |
| 2316 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2317 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2318 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2319 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2320 | if( amt>0 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2321 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2322 | } |
| 2323 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2326 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2327 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2328 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2329 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2330 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2331 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2332 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2333 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2334 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2335 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2336 | if( pCur->isValid==0 ){ |
| 2337 | return pCur->status; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2338 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2339 | assert( pCur->pPage!=0 ); |
| 2340 | assert( pCur->pPage->intKey==0 ); |
| 2341 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2342 | return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 2343 | } |
| 2344 | |
| 2345 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2346 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2347 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2348 | ** begins at "offset". |
| 2349 | ** |
| 2350 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2351 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2352 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2353 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2354 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2355 | if( !pCur->isValid ){ |
| 2356 | return pCur->status ? pCur->status : SQLITE_INTERNAL; |
| 2357 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2358 | assert( pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2359 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2360 | return getPayload(pCur, offset, amt, pBuf, 1); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2363 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2364 | ** Return a pointer to payload information from the entry that the |
| 2365 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 2366 | ** 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] | 2367 | ** skipKey==1. The number of bytes of available key/data is written |
| 2368 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 2369 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2370 | ** |
| 2371 | ** This routine is an optimization. It is common for the entire key |
| 2372 | ** and data to fit on the local page and for there to be no overflow |
| 2373 | ** pages. When that is so, this routine can be used to access the |
| 2374 | ** key and data without making a copy. If the key and/or data spills |
| 2375 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 2376 | ** the key/data and copy it into a preallocated buffer. |
| 2377 | ** |
| 2378 | ** The pointer returned by this routine looks directly into the cached |
| 2379 | ** page of the database. The data might change or move the next time |
| 2380 | ** any btree routine is called. |
| 2381 | */ |
| 2382 | static const unsigned char *fetchPayload( |
| 2383 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2384 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2385 | int skipKey /* read beginning at data if this is true */ |
| 2386 | ){ |
| 2387 | unsigned char *aPayload; |
| 2388 | MemPage *pPage; |
| 2389 | Btree *pBt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2390 | u32 nKey; |
| 2391 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2392 | |
| 2393 | assert( pCur!=0 && pCur->pPage!=0 ); |
| 2394 | assert( pCur->isValid ); |
| 2395 | pBt = pCur->pBt; |
| 2396 | pPage = pCur->pPage; |
| 2397 | pageIntegrity(pPage); |
| 2398 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2399 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2400 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2401 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2402 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2403 | nKey = 0; |
| 2404 | }else{ |
| 2405 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2406 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2407 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2408 | aPayload += nKey; |
| 2409 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2410 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2411 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2412 | if( nLocal>nKey ){ |
| 2413 | nLocal = nKey; |
| 2414 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2415 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2416 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2417 | return aPayload; |
| 2418 | } |
| 2419 | |
| 2420 | |
| 2421 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2422 | ** For the entry that cursor pCur is point to, return as |
| 2423 | ** many bytes of the key or data as are available on the local |
| 2424 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2425 | ** |
| 2426 | ** The pointer returned is ephemeral. The key/data may move |
| 2427 | ** or be destroyed on the next call to any Btree routine. |
| 2428 | ** |
| 2429 | ** These routines is used to get quick access to key and data |
| 2430 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2431 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2432 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
| 2433 | return (const void*)fetchPayload(pCur, pAmt, 0); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2434 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2435 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
| 2436 | return (const void*)fetchPayload(pCur, pAmt, 1); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | |
| 2440 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2441 | ** 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] | 2442 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2443 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2444 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2445 | int rc; |
| 2446 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2447 | MemPage *pOldPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2448 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2449 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2450 | assert( pCur->isValid ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2451 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2452 | if( rc ) return rc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2453 | pageIntegrity(pNewPage); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2454 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2455 | pOldPage = pCur->pPage; |
| 2456 | pOldPage->idxShift = 0; |
| 2457 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2458 | pCur->pPage = pNewPage; |
| 2459 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2460 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2461 | if( pNewPage->nCell<1 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2462 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2463 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2464 | return SQLITE_OK; |
| 2465 | } |
| 2466 | |
| 2467 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2468 | ** Return true if the page is the virtual root of its table. |
| 2469 | ** |
| 2470 | ** The virtual root page is the root page for most tables. But |
| 2471 | ** for the table rooted on page 1, sometime the real root page |
| 2472 | ** is empty except for the right-pointer. In such cases the |
| 2473 | ** virtual root page is the page that the right-pointer of page |
| 2474 | ** 1 is pointing to. |
| 2475 | */ |
| 2476 | static int isRootPage(MemPage *pPage){ |
| 2477 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2478 | if( pParent==0 ) return 1; |
| 2479 | if( pParent->pgno>1 ) return 0; |
| 2480 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2481 | return 0; |
| 2482 | } |
| 2483 | |
| 2484 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2485 | ** Move the cursor up to the parent page. |
| 2486 | ** |
| 2487 | ** pCur->idx is set to the cell index that contains the pointer |
| 2488 | ** to the page we are coming from. If we are coming from the |
| 2489 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2490 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2491 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2492 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2493 | Pgno oldPgno; |
| 2494 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2495 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2496 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2497 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2498 | assert( pCur->isValid ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2499 | pPage = pCur->pPage; |
| 2500 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2501 | assert( !isRootPage(pPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2502 | pageIntegrity(pPage); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2503 | pParent = pPage->pParent; |
| 2504 | assert( pParent!=0 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2505 | pageIntegrity(pParent); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2506 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2507 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2508 | oldPgno = pPage->pgno; |
| 2509 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2510 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2511 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2512 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2513 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | ** Move the cursor to the root page |
| 2518 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2519 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2520 | MemPage *pRoot; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2521 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2522 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2523 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2524 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2525 | if( rc ){ |
| 2526 | pCur->isValid = 0; |
| 2527 | return rc; |
| 2528 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2529 | releasePage(pCur->pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2530 | pageIntegrity(pRoot); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2531 | pCur->pPage = pRoot; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2532 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2533 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2534 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 2535 | Pgno subpage; |
| 2536 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2537 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2538 | assert( subpage>0 ); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 2539 | pCur->isValid = 1; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2540 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2541 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2542 | pCur->isValid = pCur->pPage->nCell>0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2543 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2544 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2545 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2546 | /* |
| 2547 | ** Move the cursor down to the left-most leaf entry beneath the |
| 2548 | ** entry to which it is currently pointing. |
| 2549 | */ |
| 2550 | static int moveToLeftmost(BtCursor *pCur){ |
| 2551 | Pgno pgno; |
| 2552 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2553 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2554 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2555 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2556 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2557 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2558 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2559 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2560 | if( rc ) return rc; |
| 2561 | } |
| 2562 | return SQLITE_OK; |
| 2563 | } |
| 2564 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2565 | /* |
| 2566 | ** Move the cursor down to the right-most leaf entry beneath the |
| 2567 | ** page to which it is currently pointing. Notice the difference |
| 2568 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 2569 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 2570 | ** finds the right-most entry beneath the *page*. |
| 2571 | */ |
| 2572 | static int moveToRightmost(BtCursor *pCur){ |
| 2573 | Pgno pgno; |
| 2574 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2575 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2576 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2577 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2578 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2579 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2580 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2581 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2582 | if( rc ) return rc; |
| 2583 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2584 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2585 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2586 | return SQLITE_OK; |
| 2587 | } |
| 2588 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2589 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 2590 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2591 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2592 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2593 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2594 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2595 | if( pCur->status ){ |
| 2596 | return pCur->status; |
| 2597 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2598 | rc = moveToRoot(pCur); |
| 2599 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2600 | if( pCur->isValid==0 ){ |
| 2601 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2602 | *pRes = 1; |
| 2603 | return SQLITE_OK; |
| 2604 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2605 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2606 | *pRes = 0; |
| 2607 | rc = moveToLeftmost(pCur); |
| 2608 | return rc; |
| 2609 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2610 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2611 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 2612 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2613 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2614 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2615 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2616 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2617 | if( pCur->status ){ |
| 2618 | return pCur->status; |
| 2619 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2620 | rc = moveToRoot(pCur); |
| 2621 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2622 | if( pCur->isValid==0 ){ |
| 2623 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2624 | *pRes = 1; |
| 2625 | return SQLITE_OK; |
| 2626 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2627 | assert( pCur->isValid ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2628 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2629 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2630 | return rc; |
| 2631 | } |
| 2632 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2633 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2634 | ** Return a success code. |
| 2635 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2636 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 2637 | ** ignored. For other tables, nKey is the number of bytes of data |
| 2638 | ** in nKey. The comparison function specified when the cursor was |
| 2639 | ** created is used to compare keys. |
| 2640 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2641 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2642 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2643 | ** were present. The cursor might point to an entry that comes |
| 2644 | ** before or after the key. |
| 2645 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2646 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2647 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2648 | ** this value is as follows: |
| 2649 | ** |
| 2650 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2651 | ** is smaller than pKey or if the table is empty |
| 2652 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2653 | ** |
| 2654 | ** *pRes==0 The cursor is left pointing at an entry that |
| 2655 | ** exactly matches pKey. |
| 2656 | ** |
| 2657 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2658 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2659 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2660 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2661 | int rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2662 | |
| 2663 | if( pCur->status ){ |
| 2664 | return pCur->status; |
| 2665 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2666 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2667 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2668 | assert( pCur->pPage ); |
| 2669 | assert( pCur->pPage->isInit ); |
| 2670 | if( pCur->isValid==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2671 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2672 | assert( pCur->pPage->nCell==0 ); |
| 2673 | return SQLITE_OK; |
| 2674 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2675 | for(;;){ |
| 2676 | int lwr, upr; |
| 2677 | Pgno chldPg; |
| 2678 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2679 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2680 | lwr = 0; |
| 2681 | upr = pPage->nCell-1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2682 | pageIntegrity(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2683 | while( lwr<=upr ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2684 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2685 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2686 | pCur->idx = (lwr+upr)/2; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2687 | pCur->info.nSize = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2688 | sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2689 | if( pPage->intKey ){ |
| 2690 | if( nCellKey<nKey ){ |
| 2691 | c = -1; |
| 2692 | }else if( nCellKey>nKey ){ |
| 2693 | c = +1; |
| 2694 | }else{ |
| 2695 | c = 0; |
| 2696 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2697 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2698 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2699 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2700 | if( available>=nCellKey ){ |
| 2701 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2702 | }else{ |
| 2703 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 2704 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2705 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2706 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2707 | sqliteFree(pCellKey); |
| 2708 | if( rc ) return rc; |
| 2709 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2710 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2711 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2712 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 2713 | lwr = pCur->idx; |
| 2714 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2715 | break; |
| 2716 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2717 | if( pRes ) *pRes = 0; |
| 2718 | return SQLITE_OK; |
| 2719 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2720 | } |
| 2721 | if( c<0 ){ |
| 2722 | lwr = pCur->idx+1; |
| 2723 | }else{ |
| 2724 | upr = pCur->idx-1; |
| 2725 | } |
| 2726 | } |
| 2727 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2728 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2729 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2730 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2731 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2732 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2733 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2734 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2735 | } |
| 2736 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2737 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2738 | if( pRes ) *pRes = c; |
| 2739 | return SQLITE_OK; |
| 2740 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2741 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2742 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2743 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2744 | if( rc ){ |
| 2745 | return rc; |
| 2746 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2747 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2748 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2752 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 2753 | ** |
| 2754 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 2755 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 2756 | ** the first entry. TRUE is also returned if the table is empty. |
| 2757 | */ |
| 2758 | int sqlite3BtreeEof(BtCursor *pCur){ |
| 2759 | return pCur->isValid==0; |
| 2760 | } |
| 2761 | |
| 2762 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2763 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2764 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2765 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2766 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2767 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2768 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2769 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2770 | MemPage *pPage = pCur->pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2771 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2772 | assert( pRes!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2773 | if( pCur->isValid==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2774 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2775 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2776 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2777 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2778 | assert( pCur->idx<pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2779 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2780 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2781 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2782 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2783 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2784 | if( rc ) return rc; |
| 2785 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2786 | *pRes = 0; |
| 2787 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2788 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2789 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2790 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2791 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2792 | pCur->isValid = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2793 | return SQLITE_OK; |
| 2794 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2795 | moveToParent(pCur); |
| 2796 | pPage = pCur->pPage; |
| 2797 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2798 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2799 | if( pPage->leafData ){ |
| 2800 | rc = sqlite3BtreeNext(pCur, pRes); |
| 2801 | }else{ |
| 2802 | rc = SQLITE_OK; |
| 2803 | } |
| 2804 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2805 | } |
| 2806 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2807 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2808 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2809 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2810 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2811 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2814 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2815 | ** 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] | 2816 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2817 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2818 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2819 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2820 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2821 | int rc; |
| 2822 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2823 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2824 | if( pCur->isValid==0 ){ |
| 2825 | *pRes = 1; |
| 2826 | return SQLITE_OK; |
| 2827 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2828 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2829 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2830 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2831 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2832 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2833 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2834 | if( rc ) return rc; |
| 2835 | rc = moveToRightmost(pCur); |
| 2836 | }else{ |
| 2837 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2838 | if( isRootPage(pPage) ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2839 | pCur->isValid = 0; |
| 2840 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2841 | return SQLITE_OK; |
| 2842 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2843 | moveToParent(pCur); |
| 2844 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2845 | } |
| 2846 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2847 | pCur->info.nSize = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2848 | if( pPage->leafData ){ |
| 2849 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 2850 | }else{ |
| 2851 | rc = SQLITE_OK; |
| 2852 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2853 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2854 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2855 | return rc; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2859 | ** Allocate a new page from the database file. |
| 2860 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2861 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2862 | ** has already been called on the new page.) The new page has also |
| 2863 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2864 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2865 | ** |
| 2866 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 2867 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2868 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2869 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2870 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 2871 | ** 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] | 2872 | ** attempt to keep related pages close to each other in the database file, |
| 2873 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2874 | ** |
| 2875 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 2876 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 2877 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2878 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2879 | static int allocatePage( |
| 2880 | Btree *pBt, |
| 2881 | MemPage **ppPage, |
| 2882 | Pgno *pPgno, |
| 2883 | Pgno nearby, |
| 2884 | u8 exact |
| 2885 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2886 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2887 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2888 | int n; /* Number of pages on the freelist */ |
| 2889 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2890 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2891 | pPage1 = pBt->pPage1; |
| 2892 | n = get4byte(&pPage1->aData[36]); |
| 2893 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2894 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2895 | MemPage *pTrunk = 0; |
| 2896 | Pgno iTrunk; |
| 2897 | MemPage *pPrevTrunk = 0; |
| 2898 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 2899 | |
| 2900 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 2901 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 2902 | ** the entire-list will be searched for that page. |
| 2903 | */ |
| 2904 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2905 | if( exact ){ |
| 2906 | u8 eType; |
| 2907 | assert( nearby>0 ); |
| 2908 | assert( pBt->autoVacuum ); |
| 2909 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 2910 | if( rc ) return rc; |
| 2911 | if( eType==PTRMAP_FREEPAGE ){ |
| 2912 | searchList = 1; |
| 2913 | } |
| 2914 | *pPgno = nearby; |
| 2915 | } |
| 2916 | #endif |
| 2917 | |
| 2918 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 2919 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 2920 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2921 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2922 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2923 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* The code within this loop is run only once if the 'searchList' variable |
| 2926 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 2927 | ** free-list until the page 'nearby' is located. |
| 2928 | */ |
| 2929 | do { |
| 2930 | pPrevTrunk = pTrunk; |
| 2931 | if( pPrevTrunk ){ |
| 2932 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2933 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2934 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2935 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2936 | rc = getPage(pBt, iTrunk, &pTrunk); |
| 2937 | if( rc ){ |
| 2938 | releasePage(pPrevTrunk); |
| 2939 | return rc; |
| 2940 | } |
| 2941 | |
| 2942 | /* TODO: This should move to after the loop? */ |
| 2943 | rc = sqlite3pager_write(pTrunk->aData); |
| 2944 | if( rc ){ |
| 2945 | releasePage(pTrunk); |
| 2946 | releasePage(pPrevTrunk); |
| 2947 | return rc; |
| 2948 | } |
| 2949 | |
| 2950 | k = get4byte(&pTrunk->aData[4]); |
| 2951 | if( k==0 && !searchList ){ |
| 2952 | /* The trunk has no leaves and the list is not being searched. |
| 2953 | ** So extract the trunk page itself and use it as the newly |
| 2954 | ** allocated page */ |
| 2955 | assert( pPrevTrunk==0 ); |
| 2956 | *pPgno = iTrunk; |
| 2957 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 2958 | *ppPage = pTrunk; |
| 2959 | pTrunk = 0; |
| 2960 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 2961 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 2962 | /* Value of k is out of range. Database corruption */ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2963 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2964 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2965 | }else if( searchList && nearby==iTrunk ){ |
| 2966 | /* The list is being searched and this trunk page is the page |
| 2967 | ** to allocate, regardless of whether it has leaves. |
| 2968 | */ |
| 2969 | assert( *pPgno==iTrunk ); |
| 2970 | *ppPage = pTrunk; |
| 2971 | searchList = 0; |
| 2972 | if( k==0 ){ |
| 2973 | if( !pPrevTrunk ){ |
| 2974 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 2975 | }else{ |
| 2976 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 2977 | } |
| 2978 | }else{ |
| 2979 | /* The trunk page is required by the caller but it contains |
| 2980 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 2981 | ** page in this case. |
| 2982 | */ |
| 2983 | MemPage *pNewTrunk; |
| 2984 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
| 2985 | rc = getPage(pBt, iNewTrunk, &pNewTrunk); |
| 2986 | if( rc!=SQLITE_OK ){ |
| 2987 | releasePage(pTrunk); |
| 2988 | releasePage(pPrevTrunk); |
| 2989 | return rc; |
| 2990 | } |
| 2991 | rc = sqlite3pager_write(pNewTrunk->aData); |
| 2992 | if( rc!=SQLITE_OK ){ |
| 2993 | releasePage(pNewTrunk); |
| 2994 | releasePage(pTrunk); |
| 2995 | releasePage(pPrevTrunk); |
| 2996 | return rc; |
| 2997 | } |
| 2998 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 2999 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3000 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
| 3001 | if( !pPrevTrunk ){ |
| 3002 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3003 | }else{ |
| 3004 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3005 | } |
| 3006 | releasePage(pNewTrunk); |
| 3007 | } |
| 3008 | pTrunk = 0; |
| 3009 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3010 | #endif |
| 3011 | }else{ |
| 3012 | /* Extract a leaf from the trunk */ |
| 3013 | int closest; |
| 3014 | Pgno iPage; |
| 3015 | unsigned char *aData = pTrunk->aData; |
| 3016 | if( nearby>0 ){ |
| 3017 | int i, dist; |
| 3018 | closest = 0; |
| 3019 | dist = get4byte(&aData[8]) - nearby; |
| 3020 | if( dist<0 ) dist = -dist; |
| 3021 | for(i=1; i<k; i++){ |
| 3022 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3023 | if( d2<0 ) d2 = -d2; |
| 3024 | if( d2<dist ){ |
| 3025 | closest = i; |
| 3026 | dist = d2; |
| 3027 | } |
| 3028 | } |
| 3029 | }else{ |
| 3030 | closest = 0; |
| 3031 | } |
| 3032 | |
| 3033 | iPage = get4byte(&aData[8+closest*4]); |
| 3034 | if( !searchList || iPage==nearby ){ |
| 3035 | *pPgno = iPage; |
| 3036 | if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 3037 | /* Free page off the end of the file */ |
| 3038 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 3039 | } |
| 3040 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3041 | ": %d more free pages\n", |
| 3042 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3043 | if( closest<k-1 ){ |
| 3044 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3045 | } |
| 3046 | put4byte(&aData[4], k-1); |
| 3047 | rc = getPage(pBt, *pPgno, ppPage); |
| 3048 | if( rc==SQLITE_OK ){ |
| 3049 | sqlite3pager_dont_rollback((*ppPage)->aData); |
| 3050 | rc = sqlite3pager_write((*ppPage)->aData); |
| 3051 | } |
| 3052 | searchList = 0; |
| 3053 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3054 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3055 | releasePage(pPrevTrunk); |
| 3056 | }while( searchList ); |
| 3057 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3058 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3059 | /* There are no pages on the freelist, so create a new page at the |
| 3060 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3061 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3062 | |
| 3063 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3064 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt->pageSize, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3065 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3066 | ** at the end of the file instead of one. The first allocated page |
| 3067 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3068 | */ |
| 3069 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 3070 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3071 | (*pPgno)++; |
| 3072 | } |
| 3073 | #endif |
| 3074 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 3075 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3076 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3077 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3078 | rc = sqlite3pager_write((*ppPage)->aData); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3079 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3080 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 3081 | |
| 3082 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3083 | return rc; |
| 3084 | } |
| 3085 | |
| 3086 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3087 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3088 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3089 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3090 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3091 | static int freePage(MemPage *pPage){ |
| 3092 | Btree *pBt = pPage->pBt; |
| 3093 | MemPage *pPage1 = pBt->pPage1; |
| 3094 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3095 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3096 | /* Prepare the page for freeing */ |
| 3097 | assert( pPage->pgno>1 ); |
| 3098 | pPage->isInit = 0; |
| 3099 | releasePage(pPage->pParent); |
| 3100 | pPage->pParent = 0; |
| 3101 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3102 | /* Increment the free page count on pPage1 */ |
| 3103 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3104 | if( rc ) return rc; |
| 3105 | n = get4byte(&pPage1->aData[36]); |
| 3106 | put4byte(&pPage1->aData[36], n+1); |
| 3107 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3108 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3109 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3110 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3111 | */ |
| 3112 | if( pBt->autoVacuum ){ |
| 3113 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3114 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3115 | } |
| 3116 | #endif |
| 3117 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3118 | if( n==0 ){ |
| 3119 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3120 | rc = sqlite3pager_write(pPage->aData); |
| 3121 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3122 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3123 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3124 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3125 | }else{ |
| 3126 | /* Other free pages already exist. Retrive the first trunk page |
| 3127 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3128 | MemPage *pTrunk; |
| 3129 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3130 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3131 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3132 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3133 | /* The trunk is full. Turn the page being freed into a new |
| 3134 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3135 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3136 | if( rc ) return rc; |
| 3137 | put4byte(pPage->aData, pTrunk->pgno); |
| 3138 | put4byte(&pPage->aData[4], 0); |
| 3139 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3140 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3141 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3142 | }else{ |
| 3143 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3144 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3145 | if( rc ) return rc; |
| 3146 | put4byte(&pTrunk->aData[4], k+1); |
| 3147 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3148 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3149 | 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] | 3150 | } |
| 3151 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3152 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3153 | return rc; |
| 3154 | } |
| 3155 | |
| 3156 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3157 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3158 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3159 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 3160 | Btree *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3161 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3162 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3163 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3164 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3165 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3166 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3167 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3168 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3169 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3170 | while( ovflPgno!=0 ){ |
| 3171 | MemPage *pOvfl; |
| 3172 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3173 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3174 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3175 | rc = freePage(pOvfl); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3176 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3177 | sqlite3pager_unref(pOvfl->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3178 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3179 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3183 | ** Create the byte sequence used to represent a cell on page pPage |
| 3184 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3185 | ** allocated and filled in as necessary. The calling procedure |
| 3186 | ** is responsible for making sure sufficient space has been allocated |
| 3187 | ** for pCell[]. |
| 3188 | ** |
| 3189 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3190 | ** area. pCell might point to some temporary storage. The cell will |
| 3191 | ** be constructed in this temporary area then copied into pPage->aData |
| 3192 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3193 | */ |
| 3194 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3195 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3196 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3197 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3198 | const void *pData,int nData, /* The data */ |
| 3199 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3200 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3201 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3202 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3203 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3204 | int spaceLeft; |
| 3205 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3206 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3207 | unsigned char *pPrior; |
| 3208 | unsigned char *pPayload; |
| 3209 | Btree *pBt = pPage->pBt; |
| 3210 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3211 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3212 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3213 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3214 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3215 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3216 | if( !pPage->leaf ){ |
| 3217 | nHeader += 4; |
| 3218 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3219 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3220 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3221 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3222 | nData = 0; |
| 3223 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3224 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3225 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3226 | assert( info.nHeader==nHeader ); |
| 3227 | assert( info.nKey==nKey ); |
| 3228 | assert( info.nData==nData ); |
| 3229 | |
| 3230 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3231 | nPayload = nData; |
| 3232 | if( pPage->intKey ){ |
| 3233 | pSrc = pData; |
| 3234 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3235 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3236 | }else{ |
| 3237 | nPayload += nKey; |
| 3238 | pSrc = pKey; |
| 3239 | nSrc = nKey; |
| 3240 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3241 | *pnSize = info.nSize; |
| 3242 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3243 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3244 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3245 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3246 | while( nPayload>0 ){ |
| 3247 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3248 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3249 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 3250 | #endif |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3251 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3252 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3253 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3254 | ** overflow page is being allocated, add an entry to the pointer-map |
| 3255 | ** for that page now. The entry for the first overflow page will be |
| 3256 | ** added later, by the insertCell() routine. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3257 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3258 | if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ |
| 3259 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3260 | } |
| 3261 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3262 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3263 | releasePage(pToRelease); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3264 | clearCell(pPage, pCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3265 | return rc; |
| 3266 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3267 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3268 | releasePage(pToRelease); |
| 3269 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3270 | pPrior = pOvfl->aData; |
| 3271 | put4byte(pPrior, 0); |
| 3272 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3273 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3274 | } |
| 3275 | n = nPayload; |
| 3276 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3277 | if( n>nSrc ) n = nSrc; |
| 3278 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3279 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3280 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3281 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3282 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3283 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3284 | if( nSrc==0 ){ |
| 3285 | nSrc = nData; |
| 3286 | pSrc = pData; |
| 3287 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3288 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3289 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3290 | return SQLITE_OK; |
| 3291 | } |
| 3292 | |
| 3293 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3294 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3295 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3296 | ** pointer in the third argument. |
| 3297 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3298 | static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3299 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3300 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3301 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3302 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3303 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3304 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3305 | if( aData ){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3306 | pThis = (MemPage*)&aData[pBt->psAligned]; |
drh | 3127653 | 2004-09-27 12:20:52 +0000 | [diff] [blame] | 3307 | assert( pThis->aData==aData ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3308 | if( pThis->isInit ){ |
| 3309 | if( pThis->pParent!=pNewParent ){ |
| 3310 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 3311 | pThis->pParent = pNewParent; |
| 3312 | if( pNewParent ) sqlite3pager_ref(pNewParent->aData); |
| 3313 | } |
| 3314 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3315 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3316 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3317 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3318 | |
| 3319 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3320 | if( pBt->autoVacuum ){ |
| 3321 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 3322 | } |
| 3323 | #endif |
| 3324 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3328 | ** Change the pParent pointer of all children of pPage to point back |
| 3329 | ** to pPage. |
| 3330 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3331 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3332 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3333 | ** |
| 3334 | ** This routine gets called after you memcpy() one page into |
| 3335 | ** another. |
| 3336 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3337 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3338 | int i; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3339 | Btree *pBt = pPage->pBt; |
| 3340 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3341 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3342 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 3343 | if( pPage->leaf ) return SQLITE_OK; |
| 3344 | #else |
| 3345 | if( !pBt->autoVacuum && pPage->leaf ) return SQLITE_OK; |
| 3346 | #endif |
| 3347 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3348 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3349 | u8 *pCell = findCell(pPage, i); |
| 3350 | if( !pPage->leaf ){ |
| 3351 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 3352 | if( rc!=SQLITE_OK ) return rc; |
| 3353 | } |
| 3354 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3355 | /* If the database supports auto-vacuum, then check each cell to see |
| 3356 | ** if it contains a pointer to an overflow page. If so, then the |
| 3357 | ** pointer-map must be updated accordingly. |
| 3358 | ** |
| 3359 | ** TODO: This looks like quite an expensive thing to do. Investigate. |
| 3360 | */ |
| 3361 | if( pBt->autoVacuum ){ |
| 3362 | CellInfo info; |
| 3363 | parseCellPtr(pPage, pCell, &info); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3364 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3365 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3366 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3367 | if( rc!=SQLITE_OK ) return rc; |
| 3368 | } |
| 3369 | } |
| 3370 | #endif |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3371 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3372 | if( !pPage->leaf ){ |
| 3373 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 3374 | pPage, i); |
| 3375 | pPage->idxShift = 0; |
| 3376 | } |
| 3377 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | /* |
| 3381 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 3382 | ** The cell content is not freed or deallocated. It is assumed that |
| 3383 | ** the cell content has been copied someplace else. This routine just |
| 3384 | ** removes the reference to the cell from pPage. |
| 3385 | ** |
| 3386 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3387 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3388 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3389 | int i; /* Loop counter */ |
| 3390 | int pc; /* Offset to cell content of cell being deleted */ |
| 3391 | u8 *data; /* pPage->aData */ |
| 3392 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 3393 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3394 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3395 | assert( sz==cellSize(pPage, idx) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3396 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3397 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3398 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 3399 | pc = get2byte(ptr); |
| 3400 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3401 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3402 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 3403 | ptr[0] = ptr[2]; |
| 3404 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3405 | } |
| 3406 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3407 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 3408 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3409 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | /* |
| 3413 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 3414 | ** content of the cell. |
| 3415 | ** |
| 3416 | ** 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] | 3417 | ** will not fit, then make a copy of the cell content into pTemp if |
| 3418 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 3419 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 3420 | ** in pTemp or the original pCell) and also record its index. |
| 3421 | ** Allocating a new entry in pPage->aCell[] implies that |
| 3422 | ** pPage->nOverflow is incremented. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3423 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3424 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3425 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3426 | int i, /* New cell becomes the i-th cell of the page */ |
| 3427 | u8 *pCell, /* Content of the new cell */ |
| 3428 | int sz, /* Bytes of content in pCell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3429 | u8 *pTemp /* Temp storage space for pCell, if needed */ |
| 3430 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3431 | int idx; /* Where to write new cell content in data[] */ |
| 3432 | int j; /* Loop counter */ |
| 3433 | int top; /* First byte of content for any cell in data[] */ |
| 3434 | int end; /* First byte past the last cell pointer in data[] */ |
| 3435 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 3436 | int hdr; /* Offset into data[] of the page header */ |
| 3437 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 3438 | u8 *data; /* The content of the whole page */ |
| 3439 | u8 *ptr; /* Used for moving information around in data[] */ |
| 3440 | |
| 3441 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 3442 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3443 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3444 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3445 | if( pTemp ){ |
| 3446 | memcpy(pTemp, pCell, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3447 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3448 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3449 | j = pPage->nOverflow++; |
| 3450 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 3451 | pPage->aOvfl[j].pCell = pCell; |
| 3452 | pPage->aOvfl[j].idx = i; |
| 3453 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3454 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3455 | data = pPage->aData; |
| 3456 | hdr = pPage->hdrOffset; |
| 3457 | top = get2byte(&data[hdr+5]); |
| 3458 | cellOffset = pPage->cellOffset; |
| 3459 | end = cellOffset + 2*pPage->nCell + 2; |
| 3460 | ins = cellOffset + 2*i; |
| 3461 | if( end > top - sz ){ |
| 3462 | defragmentPage(pPage); |
| 3463 | top = get2byte(&data[hdr+5]); |
| 3464 | assert( end + sz <= top ); |
| 3465 | } |
| 3466 | idx = allocateSpace(pPage, sz); |
| 3467 | assert( idx>0 ); |
| 3468 | assert( end <= get2byte(&data[hdr+5]) ); |
| 3469 | pPage->nCell++; |
| 3470 | pPage->nFree -= 2; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3471 | memcpy(&data[idx], pCell, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3472 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 3473 | ptr[0] = ptr[-2]; |
| 3474 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3475 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3476 | put2byte(&data[ins], idx); |
| 3477 | put2byte(&data[hdr+3], pPage->nCell); |
| 3478 | pPage->idxShift = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3479 | pageIntegrity(pPage); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3480 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3481 | if( pPage->pBt->autoVacuum ){ |
| 3482 | /* The cell may contain a pointer to an overflow page. If so, write |
| 3483 | ** the entry for the overflow page into the pointer map. |
| 3484 | */ |
| 3485 | CellInfo info; |
| 3486 | parseCellPtr(pPage, pCell, &info); |
| 3487 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 3488 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 3489 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 3490 | if( rc!=SQLITE_OK ) return rc; |
| 3491 | } |
| 3492 | } |
| 3493 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3494 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3495 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3496 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3500 | ** Add a list of cells to a page. The page should be initially empty. |
| 3501 | ** The cells are guaranteed to fit on the page. |
| 3502 | */ |
| 3503 | static void assemblePage( |
| 3504 | MemPage *pPage, /* The page to be assemblied */ |
| 3505 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3506 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3507 | int *aSize /* Sizes of the cells */ |
| 3508 | ){ |
| 3509 | int i; /* Loop counter */ |
| 3510 | int totalSize; /* Total size of all cells */ |
| 3511 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3512 | int cellptr; /* Address of next cell pointer */ |
| 3513 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3514 | u8 *data; /* Data for the page */ |
| 3515 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3516 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3517 | totalSize = 0; |
| 3518 | for(i=0; i<nCell; i++){ |
| 3519 | totalSize += aSize[i]; |
| 3520 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3521 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3522 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3523 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3524 | data = pPage->aData; |
| 3525 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3526 | put2byte(&data[hdr+3], nCell); |
| 3527 | cellbody = allocateSpace(pPage, totalSize); |
| 3528 | assert( cellbody>0 ); |
| 3529 | assert( pPage->nFree >= 2*nCell ); |
| 3530 | pPage->nFree -= 2*nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3531 | for(i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3532 | put2byte(&data[cellptr], cellbody); |
| 3533 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 3534 | cellptr += 2; |
| 3535 | cellbody += aSize[i]; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3536 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3537 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3538 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3541 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3542 | ** The following parameters determine how many adjacent pages get involved |
| 3543 | ** in a balancing operation. NN is the number of neighbors on either side |
| 3544 | ** of the page that participate in the balancing operation. NB is the |
| 3545 | ** total number of pages that participate, including the target page and |
| 3546 | ** NN neighbors on either side. |
| 3547 | ** |
| 3548 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 3549 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 3550 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 3551 | ** The value of NN appears to give the best results overall. |
| 3552 | */ |
| 3553 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 3554 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 3555 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3556 | /* Forward reference */ |
| 3557 | static int balance(MemPage*); |
| 3558 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3559 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3560 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3561 | ** 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] | 3562 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 3563 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3564 | ** 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] | 3565 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3566 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3567 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 3568 | ** The number of siblings of pPage might be increased or decreased by one or |
| 3569 | ** 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] | 3570 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3571 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3572 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3573 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3574 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3575 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3576 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3577 | ** 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] | 3578 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3579 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3580 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 3581 | ** might become overfull or underfull. If that happens, then this routine |
| 3582 | ** is called recursively on the parent. |
| 3583 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3584 | ** If this routine fails for any reason, it might leave the database |
| 3585 | ** in a corrupted state. So if this routine fails, the database should |
| 3586 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3587 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3588 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3589 | MemPage *pParent; /* The parent of pPage */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3590 | Btree *pBt; /* The whole database */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3591 | int nCell = 0; /* Number of cells in aCell[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3592 | int nOld; /* Number of pages in apOld[] */ |
| 3593 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3594 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3595 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3596 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 3597 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3598 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3599 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3600 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3601 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 3602 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3603 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3604 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3605 | int mxCellPerPage; /* Maximum number of cells in one page */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3606 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 3607 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3608 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3609 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 3610 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3611 | int idxDiv[NB]; /* Indices of divider cells in pParent */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3612 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3613 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 3614 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3615 | u8 **apCell; /* All cells begin balanced */ |
| 3616 | int *szCell; /* Local size of all cells in apCell[] */ |
| 3617 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 3618 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3619 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3620 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3621 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3622 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3623 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3624 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3625 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3626 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3627 | sqlite3pager_write(pParent->aData); |
| 3628 | assert( pParent ); |
| 3629 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3630 | |
| 3631 | /* |
| 3632 | ** Allocate space for memory structures |
| 3633 | */ |
| 3634 | mxCellPerPage = MX_CELL(pBt); |
| 3635 | apCell = sqliteMallocRaw( |
| 3636 | (mxCellPerPage+2)*NB*(sizeof(u8*)+sizeof(int)) |
| 3637 | + sizeof(MemPage)*NB |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3638 | + pBt->psAligned*(5+NB) |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3639 | ); |
| 3640 | if( apCell==0 ){ |
| 3641 | return SQLITE_NOMEM; |
| 3642 | } |
| 3643 | szCell = (int*)&apCell[(mxCellPerPage+2)*NB]; |
| 3644 | aCopy[0] = (u8*)&szCell[(mxCellPerPage+2)*NB]; |
| 3645 | for(i=1; i<NB; i++){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3646 | aCopy[i] = &aCopy[i-1][pBt->psAligned+sizeof(MemPage)]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3647 | } |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3648 | aSpace = &aCopy[NB-1][pBt->psAligned+sizeof(MemPage)]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3649 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3650 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3651 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3652 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 3653 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3654 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3655 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3656 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3657 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3658 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3659 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3660 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3663 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3664 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3665 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3666 | }else{ |
| 3667 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3668 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3669 | |
| 3670 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3671 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3672 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3673 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3674 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3675 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3676 | |
| 3677 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3678 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3679 | ** the siblings. An attempt is made to find NN siblings on either |
| 3680 | ** side of pPage. More siblings are taken from one side, however, if |
| 3681 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 3682 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3683 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3684 | nxDiv = idx - NN; |
| 3685 | if( nxDiv + NB > pParent->nCell ){ |
| 3686 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3687 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3688 | if( nxDiv<0 ){ |
| 3689 | nxDiv = 0; |
| 3690 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3691 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3692 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3693 | if( k<pParent->nCell ){ |
| 3694 | idxDiv[i] = k; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3695 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3696 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3697 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3698 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3699 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3700 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3701 | }else{ |
| 3702 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3703 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3704 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3705 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3706 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3707 | apCopy[i] = 0; |
| 3708 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3709 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3713 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 3714 | ** The rest of this function will use data from the copies rather |
| 3715 | ** that the original pages since the original pages will be in the |
| 3716 | ** process of being overwritten. |
| 3717 | */ |
| 3718 | for(i=0; i<nOld; i++){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3719 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned]; |
| 3720 | p->aData = &((u8*)p)[-pBt->psAligned]; |
| 3721 | memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage)); |
| 3722 | p->aData = &((u8*)p)[-pBt->psAligned]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | /* |
| 3726 | ** Load pointers to all cells on sibling pages and the divider cells |
| 3727 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3728 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 3729 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3730 | ** |
| 3731 | ** If the siblings are on leaf pages, then the child pointers of the |
| 3732 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3733 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3734 | ** child pointers. If siblings are not leaves, then all cell in |
| 3735 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 3736 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3737 | ** |
| 3738 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 3739 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3740 | */ |
| 3741 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3742 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3743 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3744 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3745 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3746 | int limit = pOld->nCell+pOld->nOverflow; |
| 3747 | for(j=0; j<limit; j++){ |
| 3748 | apCell[nCell] = findOverflowCell(pOld, j); |
| 3749 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3750 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3751 | } |
| 3752 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3753 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3754 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3755 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 3756 | ** are duplicates of keys on the child pages. We need to remove |
| 3757 | ** the divider cells from pParent, but the dividers cells are not |
| 3758 | ** added to apCell[] because they are duplicates of child cells. |
| 3759 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3760 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3761 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3762 | u8 *pTemp; |
| 3763 | szCell[nCell] = sz; |
| 3764 | pTemp = &aSpace[iSpace]; |
| 3765 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3766 | assert( iSpace<=pBt->psAligned*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3767 | memcpy(pTemp, apDiv[i], sz); |
| 3768 | apCell[nCell] = pTemp+leafCorrection; |
| 3769 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3770 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3771 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3772 | if( !pOld->leaf ){ |
| 3773 | assert( leafCorrection==0 ); |
| 3774 | /* The right pointer of the child page pOld becomes the left |
| 3775 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3776 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3777 | }else{ |
| 3778 | assert( leafCorrection==4 ); |
| 3779 | } |
| 3780 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3781 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3786 | ** Figure out the number of pages needed to hold all nCell cells. |
| 3787 | ** Store this number in "k". Also compute szNew[] which is the total |
| 3788 | ** 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] | 3789 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3790 | ** cntNew[k] should equal nCell. |
| 3791 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3792 | ** Values computed by this block: |
| 3793 | ** |
| 3794 | ** k: The total number of sibling pages |
| 3795 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 3796 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 3797 | ** the right of the i-th sibling page. |
| 3798 | ** usableSpace: Number of bytes of space available on each sibling. |
| 3799 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3800 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3801 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3802 | for(subtotal=k=i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3803 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3804 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3805 | szNew[k] = subtotal - szCell[i]; |
| 3806 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3807 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3808 | subtotal = 0; |
| 3809 | k++; |
| 3810 | } |
| 3811 | } |
| 3812 | szNew[k] = subtotal; |
| 3813 | cntNew[k] = nCell; |
| 3814 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3815 | |
| 3816 | /* |
| 3817 | ** The packing computed by the previous block is biased toward the siblings |
| 3818 | ** on the left side. The left siblings are always nearly full, while the |
| 3819 | ** right-most sibling might be nearly empty. This block of code attempts |
| 3820 | ** to adjust the packing of siblings to get a better balance. |
| 3821 | ** |
| 3822 | ** This adjustment is more than an optimization. The packing above might |
| 3823 | ** be so out of balance as to be illegal. For example, the right-most |
| 3824 | ** sibling might be completely empty. This adjustment is not optional. |
| 3825 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3826 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3827 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 3828 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 3829 | int r; /* Index of right-most cell in left sibling */ |
| 3830 | int d; /* Index of first cell to the left of right sibling */ |
| 3831 | |
| 3832 | r = cntNew[i-1] - 1; |
| 3833 | d = r + 1 - leafData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3834 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 3835 | szRight += szCell[d] + 2; |
| 3836 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3837 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3838 | r = cntNew[i-1] - 1; |
| 3839 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3840 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3841 | szNew[i] = szRight; |
| 3842 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3843 | } |
| 3844 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3845 | |
| 3846 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3847 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3848 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3849 | assert( pPage->pgno>1 ); |
| 3850 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3851 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3852 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3853 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3854 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3855 | pgnoNew[i] = pgnoOld[i]; |
| 3856 | apOld[i] = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3857 | sqlite3pager_write(pNew->aData); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3858 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3859 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3860 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3861 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3862 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3863 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3864 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3867 | /* Free any old pages that were not reused as new pages. |
| 3868 | */ |
| 3869 | while( i<nOld ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3870 | rc = freePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3871 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3872 | releasePage(apOld[i]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 3873 | apOld[i] = 0; |
| 3874 | i++; |
| 3875 | } |
| 3876 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3877 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3878 | ** Put the new pages in accending order. This helps to |
| 3879 | ** keep entries in the disk file in order so that a scan |
| 3880 | ** of the table is a linear scan through the file. That |
| 3881 | ** in turn helps the operating system to deliver pages |
| 3882 | ** from the disk more rapidly. |
| 3883 | ** |
| 3884 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3885 | ** n is never more than NB (a small constant), that should |
| 3886 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3887 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3888 | ** When NB==3, this one optimization makes the database |
| 3889 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3890 | */ |
| 3891 | for(i=0; i<k-1; i++){ |
| 3892 | int minV = pgnoNew[i]; |
| 3893 | int minI = i; |
| 3894 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 3895 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3896 | minI = j; |
| 3897 | minV = pgnoNew[j]; |
| 3898 | } |
| 3899 | } |
| 3900 | if( minI>i ){ |
| 3901 | int t; |
| 3902 | MemPage *pT; |
| 3903 | t = pgnoNew[i]; |
| 3904 | pT = apNew[i]; |
| 3905 | pgnoNew[i] = pgnoNew[minI]; |
| 3906 | apNew[i] = apNew[minI]; |
| 3907 | pgnoNew[minI] = t; |
| 3908 | apNew[minI] = pT; |
| 3909 | } |
| 3910 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3911 | 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] | 3912 | pgnoOld[0], |
| 3913 | nOld>=2 ? pgnoOld[1] : 0, |
| 3914 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 3915 | pgnoNew[0], szNew[0], |
| 3916 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 3917 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3918 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 3919 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3920 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 3921 | |
| 3922 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3923 | ** Evenly distribute the data in apCell[] across the new pages. |
| 3924 | ** Insert divider cells into pParent as necessary. |
| 3925 | */ |
| 3926 | j = 0; |
| 3927 | for(i=0; i<nNew; i++){ |
| 3928 | MemPage *pNew = apNew[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3929 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3930 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
| 3931 | j = cntNew[i]; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3932 | assert( pNew->nCell>0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3933 | assert( pNew->nOverflow==0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3934 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3935 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3936 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3937 | int sz; |
| 3938 | pCell = apCell[j]; |
| 3939 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3940 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3941 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3942 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3943 | }else if( leafData ){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3944 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3945 | j--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3946 | parseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3947 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3948 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3949 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3950 | assert( iSpace<=pBt->psAligned*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3951 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3952 | }else{ |
| 3953 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3954 | pTemp = &aSpace[iSpace]; |
| 3955 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3956 | assert( iSpace<=pBt->psAligned*5 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3957 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3958 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp); |
| 3959 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3960 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3961 | j++; |
| 3962 | nxDiv++; |
| 3963 | } |
| 3964 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3965 | assert( j==nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3966 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3967 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3968 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3969 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3970 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3971 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3972 | }else{ |
| 3973 | /* Right-most sibling is the left child of the first entry in pParent |
| 3974 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3975 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
| 3978 | /* |
| 3979 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3980 | */ |
| 3981 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3982 | rc = reparentChildPages(apNew[i]); |
| 3983 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3984 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3985 | rc = reparentChildPages(pParent); |
| 3986 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3987 | |
| 3988 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3989 | ** Balance the parent page. Note that the current page (pPage) might |
| 3990 | ** have been added to the freelist is it might no longer be initialized. |
| 3991 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3992 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3993 | assert( pParent->isInit ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3994 | /* assert( pPage->isInit ); // No! pPage might have been added to freelist */ |
| 3995 | /* pageIntegrity(pPage); // No! pPage might have been added to freelist */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3996 | rc = balance(pParent); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3997 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3998 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3999 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4000 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4001 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4002 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4003 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4004 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4005 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4006 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4007 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4008 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4009 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4010 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4011 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4012 | return rc; |
| 4013 | } |
| 4014 | |
| 4015 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4016 | ** This routine is called for the root page of a btree when the root |
| 4017 | ** page contains no cells. This is an opportunity to make the tree |
| 4018 | ** shallower by one level. |
| 4019 | */ |
| 4020 | static int balance_shallower(MemPage *pPage){ |
| 4021 | MemPage *pChild; /* The only child page of pPage */ |
| 4022 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4023 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
| 4024 | Btree *pBt; /* The main BTree structure */ |
| 4025 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4026 | u8 **apCell; /* All cells from pages being balanced */ |
| 4027 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4028 | |
| 4029 | assert( pPage->pParent==0 ); |
| 4030 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4031 | pBt = pPage->pBt; |
| 4032 | mxCellPerPage = MX_CELL(pBt); |
| 4033 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4034 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4035 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4036 | if( pPage->leaf ){ |
| 4037 | /* The table is completely empty */ |
| 4038 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4039 | }else{ |
| 4040 | /* The root page is empty but has one child. Transfer the |
| 4041 | ** information from that one child into the root page if it |
| 4042 | ** will fit. This reduces the depth of the tree by one. |
| 4043 | ** |
| 4044 | ** If the root page is page 1, it has less space available than |
| 4045 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4046 | ** of the database fle), so it might not be able to hold all of the |
| 4047 | ** information currently contained in the child. If this is the |
| 4048 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4049 | ** for the right-pointer to the child page. The child page becomes |
| 4050 | ** the virtual root of the tree. |
| 4051 | */ |
| 4052 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4053 | assert( pgnoChild>0 ); |
| 4054 | assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) ); |
| 4055 | rc = getPage(pPage->pBt, pgnoChild, &pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4056 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4057 | if( pPage->pgno==1 ){ |
| 4058 | rc = initPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4059 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4060 | assert( pChild->nOverflow==0 ); |
| 4061 | if( pChild->nFree>=100 ){ |
| 4062 | /* The child information will fit on the root page, so do the |
| 4063 | ** copy */ |
| 4064 | int i; |
| 4065 | zeroPage(pPage, pChild->aData[0]); |
| 4066 | for(i=0; i<pChild->nCell; i++){ |
| 4067 | apCell[i] = findCell(pChild,i); |
| 4068 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 4069 | } |
| 4070 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
| 4071 | freePage(pChild); |
| 4072 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 4073 | }else{ |
| 4074 | /* The child has more information that will fit on the root. |
| 4075 | ** The tree is already balanced. Do nothing. */ |
| 4076 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 4077 | } |
| 4078 | }else{ |
| 4079 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 4080 | pPage->isInit = 0; |
| 4081 | pPage->pParent = 0; |
| 4082 | rc = initPage(pPage, 0); |
| 4083 | assert( rc==SQLITE_OK ); |
| 4084 | freePage(pChild); |
| 4085 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 4086 | pChild->pgno, pPage->pgno)); |
| 4087 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4088 | rc = reparentChildPages(pPage); |
| 4089 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4090 | releasePage(pChild); |
| 4091 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4092 | end_shallow_balance: |
| 4093 | sqliteFree(apCell); |
| 4094 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | |
| 4098 | /* |
| 4099 | ** The root page is overfull |
| 4100 | ** |
| 4101 | ** When this happens, Create a new child page and copy the |
| 4102 | ** contents of the root into the child. Then make the root |
| 4103 | ** page an empty page with rightChild pointing to the new |
| 4104 | ** child. Finally, call balance_internal() on the new child |
| 4105 | ** to cause it to split. |
| 4106 | */ |
| 4107 | static int balance_deeper(MemPage *pPage){ |
| 4108 | int rc; /* Return value from subprocedures */ |
| 4109 | MemPage *pChild; /* Pointer to a new child page */ |
| 4110 | Pgno pgnoChild; /* Page number of the new child page */ |
| 4111 | Btree *pBt; /* The BTree */ |
| 4112 | int usableSize; /* Total usable size of a page */ |
| 4113 | u8 *data; /* Content of the parent page */ |
| 4114 | u8 *cdata; /* Content of the child page */ |
| 4115 | int hdr; /* Offset to page header in parent */ |
| 4116 | int brk; /* Offset to content of first cell in parent */ |
| 4117 | |
| 4118 | assert( pPage->pParent==0 ); |
| 4119 | assert( pPage->nOverflow>0 ); |
| 4120 | pBt = pPage->pBt; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4121 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4122 | if( rc ) return rc; |
| 4123 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
| 4124 | usableSize = pBt->usableSize; |
| 4125 | data = pPage->aData; |
| 4126 | hdr = pPage->hdrOffset; |
| 4127 | brk = get2byte(&data[hdr+5]); |
| 4128 | cdata = pChild->aData; |
| 4129 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 4130 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
| 4131 | rc = initPage(pChild, pPage); |
| 4132 | if( rc ) return rc; |
| 4133 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 4134 | pChild->nOverflow = pPage->nOverflow; |
| 4135 | if( pChild->nOverflow ){ |
| 4136 | pChild->nFree = 0; |
| 4137 | } |
| 4138 | assert( pChild->nCell==pPage->nCell ); |
| 4139 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 4140 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 4141 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
| 4142 | rc = balance_nonroot(pChild); |
| 4143 | releasePage(pChild); |
| 4144 | return rc; |
| 4145 | } |
| 4146 | |
| 4147 | /* |
| 4148 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 4149 | ** required, call the appropriate balancing routine. |
| 4150 | */ |
| 4151 | static int balance(MemPage *pPage){ |
| 4152 | int rc = SQLITE_OK; |
| 4153 | if( pPage->pParent==0 ){ |
| 4154 | if( pPage->nOverflow>0 ){ |
| 4155 | rc = balance_deeper(pPage); |
| 4156 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4157 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4158 | rc = balance_shallower(pPage); |
| 4159 | } |
| 4160 | }else{ |
| 4161 | if( pPage->nOverflow>0 || pPage->nFree>pPage->pBt->usableSize*2/3 ){ |
| 4162 | rc = balance_nonroot(pPage); |
| 4163 | } |
| 4164 | } |
| 4165 | return rc; |
| 4166 | } |
| 4167 | |
| 4168 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4169 | ** This routine checks all cursors that point to table pgnoRoot. |
| 4170 | ** If any of those cursors other than pExclude were opened with |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4171 | ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4172 | ** cursors that point to pgnoRoot were opened with wrFlag==1 |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4173 | ** then this routine returns SQLITE_OK. |
| 4174 | ** |
| 4175 | ** In addition to checking for read-locks (where a read-lock |
| 4176 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4177 | ** all cursors other than pExclude so that they are pointing to the |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4178 | ** first Cell on root page. This is necessary because an insert |
| 4179 | ** or delete might change the number of cells on a page or delete |
| 4180 | ** a page entirely and we do not want to leave any cursors |
| 4181 | ** pointing to non-existant pages or cells. |
| 4182 | */ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4183 | static int checkReadLocks(Btree *pBt, Pgno pgnoRoot, BtCursor *pExclude){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4184 | BtCursor *p; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4185 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 4186 | if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4187 | if( p->wrFlag==0 ) return SQLITE_LOCKED; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4188 | if( p->pPage->pgno!=p->pgnoRoot ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4189 | moveToRoot(p); |
| 4190 | } |
| 4191 | } |
| 4192 | return SQLITE_OK; |
| 4193 | } |
| 4194 | |
| 4195 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4196 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 4197 | ** 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] | 4198 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4199 | ** is left pointing at a random location. |
| 4200 | ** |
| 4201 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 4202 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4203 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4204 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 4205 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4206 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 4207 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4208 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4209 | int rc; |
| 4210 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4211 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4212 | MemPage *pPage; |
| 4213 | Btree *pBt = pCur->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4214 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4215 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4216 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4217 | if( pCur->status ){ |
| 4218 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4219 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4220 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4221 | /* Must start a transaction before doing an insert */ |
| 4222 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4223 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4224 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4225 | if( !pCur->wrFlag ){ |
| 4226 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 4227 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4228 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4229 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 4230 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4231 | rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4232 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4233 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4234 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4235 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4236 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 4237 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 4238 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 4239 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4240 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4241 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4242 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 4243 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4244 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4245 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4246 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4247 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4248 | if( loc==0 && pCur->isValid ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4249 | int szOld; |
| 4250 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4251 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4252 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4253 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4254 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4255 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4256 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4257 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4258 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 4259 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4260 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4261 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4262 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4263 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4264 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4265 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4266 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0); |
| 4267 | if( rc!=SQLITE_OK ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4268 | rc = balance(pPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4269 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4270 | /* fflush(stdout); */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4271 | if( rc==SQLITE_OK ){ |
| 4272 | moveToRoot(pCur); |
| 4273 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4274 | end_insert: |
| 4275 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4276 | return rc; |
| 4277 | } |
| 4278 | |
| 4279 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4280 | ** Delete the entry that the cursor is pointing to. The cursor |
| 4281 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4282 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4283 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4284 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4285 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4286 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4287 | Pgno pgnoChild = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 4288 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4289 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 4290 | assert( pPage->isInit ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 4291 | if( pCur->status ){ |
| 4292 | return pCur->status; /* A rollback destroyed this cursor */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4293 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4294 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4295 | /* Must start a transaction before doing a delete */ |
| 4296 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4297 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4298 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4299 | if( pCur->idx >= pPage->nCell ){ |
| 4300 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 4301 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4302 | if( !pCur->wrFlag ){ |
| 4303 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 4304 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4305 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4306 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 4307 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4308 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4309 | if( rc ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4310 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4311 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4312 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4313 | } |
| 4314 | clearCell(pPage, pCell); |
| 4315 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4316 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4317 | ** 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] | 4318 | ** do something we will leave a hole on an internal page. |
| 4319 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 4320 | ** next Cell after the one to be deleted is guaranteed to exist and |
| 4321 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4322 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4323 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4324 | unsigned char *pNext; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4325 | int szNext; |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 4326 | int notUsed; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4327 | unsigned char *tempCell; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4328 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4329 | getTempCursor(pCur, &leafCur); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4330 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4331 | if( rc!=SQLITE_OK ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4332 | if( rc!=SQLITE_NOMEM ){ |
| 4333 | rc = SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 4334 | } |
drh | 8a6ac0a | 2004-02-14 17:35:07 +0000 | [diff] [blame] | 4335 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4336 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4337 | rc = sqlite3pager_write(leafCur.pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4338 | if( rc ) return rc; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4339 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 4340 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4341 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
| 4342 | pNext = findCell(leafCur.pPage, leafCur.idx); |
| 4343 | szNext = cellSizePtr(leafCur.pPage, pNext); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4344 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 4345 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 4346 | if( tempCell==0 ) return SQLITE_NOMEM; |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4347 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell); |
| 4348 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4349 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4350 | rc = balance(pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4351 | sqliteFree(tempCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4352 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4353 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 4354 | rc = balance(leafCur.pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4355 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4356 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4357 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 4358 | pCur->pgnoRoot, pPage->pgno)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4359 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4360 | rc = balance(pPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4361 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4362 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4363 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4364 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4365 | |
| 4366 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4367 | ** Create a new BTree table. Write into *piTable the page |
| 4368 | ** number for the root page of the new table. |
| 4369 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4370 | ** The type of type is determined by the flags parameter. Only the |
| 4371 | ** following values of flags are currently in use. Other values for |
| 4372 | ** flags might not work: |
| 4373 | ** |
| 4374 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 4375 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4376 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4377 | int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4378 | MemPage *pRoot; |
| 4379 | Pgno pgnoRoot; |
| 4380 | int rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4381 | /* TODO: Disallow schema modifications if there are open cursors */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4382 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4383 | /* Must start a transaction first */ |
| 4384 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4385 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 4386 | if( pBt->readOnly ){ |
| 4387 | return SQLITE_READONLY; |
| 4388 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4389 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4390 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4391 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4392 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4393 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4394 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 4395 | MemPage *pPageMove; /* The page to move to. */ |
| 4396 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4397 | /* Read the value of meta[3] from the database to determine where the |
| 4398 | ** root page of the new table should go. meta[3] is the largest root-page |
| 4399 | ** created so far, so the new root-page is (meta[3]+1). |
| 4400 | */ |
| 4401 | rc = sqlite3BtreeGetMeta(pBt, 4, &pgnoRoot); |
| 4402 | if( rc!=SQLITE_OK ) return rc; |
| 4403 | pgnoRoot++; |
| 4404 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 4405 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 4406 | ** PENDING_BYTE page. |
| 4407 | */ |
| 4408 | if( pgnoRoot==PTRMAP_PAGENO(pBt->pageSize, pgnoRoot) || |
| 4409 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4410 | pgnoRoot++; |
| 4411 | } |
| 4412 | assert( pgnoRoot>=3 ); |
| 4413 | |
| 4414 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 4415 | ** be moved to the allocated page (unless the allocated page happens |
| 4416 | ** to reside at pgnoRoot). |
| 4417 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4418 | rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4419 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4420 | return rc; |
| 4421 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4422 | |
| 4423 | if( pgnoMove!=pgnoRoot ){ |
| 4424 | u8 eType; |
| 4425 | Pgno iPtrPage; |
| 4426 | |
| 4427 | releasePage(pPageMove); |
| 4428 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 4429 | if( rc!=SQLITE_OK ){ |
| 4430 | return rc; |
| 4431 | } |
| 4432 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4433 | assert( eType!=PTRMAP_ROOTPAGE ); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4434 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4435 | if( rc!=SQLITE_OK ){ |
| 4436 | releasePage(pRoot); |
| 4437 | return rc; |
| 4438 | } |
| 4439 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 4440 | releasePage(pRoot); |
| 4441 | if( rc!=SQLITE_OK ){ |
| 4442 | return rc; |
| 4443 | } |
| 4444 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 4445 | if( rc!=SQLITE_OK ){ |
| 4446 | return rc; |
| 4447 | } |
| 4448 | rc = sqlite3pager_write(pRoot->aData); |
| 4449 | if( rc!=SQLITE_OK ){ |
| 4450 | releasePage(pRoot); |
| 4451 | return rc; |
| 4452 | } |
| 4453 | }else{ |
| 4454 | pRoot = pPageMove; |
| 4455 | } |
| 4456 | |
| 4457 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 4458 | if( rc ){ |
| 4459 | releasePage(pRoot); |
| 4460 | return rc; |
| 4461 | } |
| 4462 | rc = sqlite3BtreeUpdateMeta(pBt, 4, pgnoRoot); |
| 4463 | if( rc ){ |
| 4464 | releasePage(pRoot); |
| 4465 | return rc; |
| 4466 | } |
| 4467 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4468 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4469 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4470 | } |
| 4471 | #endif |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4472 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4473 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4474 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4475 | *piTable = (int)pgnoRoot; |
| 4476 | return SQLITE_OK; |
| 4477 | } |
| 4478 | |
| 4479 | /* |
| 4480 | ** Erase the given database page and all its children. Return |
| 4481 | ** the page to the freelist. |
| 4482 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4483 | static int clearDatabasePage( |
| 4484 | Btree *pBt, /* The BTree that contains the table */ |
| 4485 | Pgno pgno, /* Page number to clear */ |
| 4486 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 4487 | int freePageFlag /* Deallocate page if true */ |
| 4488 | ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4489 | MemPage *pPage; |
| 4490 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4491 | unsigned char *pCell; |
| 4492 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4493 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4494 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4495 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4496 | rc = sqlite3pager_write(pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4497 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4498 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4499 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4500 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4501 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4502 | if( rc ) return rc; |
| 4503 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4504 | rc = clearCell(pPage, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4505 | if( rc ) return rc; |
| 4506 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4507 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4508 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4509 | if( rc ) return rc; |
| 4510 | } |
| 4511 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4512 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4513 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4514 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4515 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4516 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4517 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4521 | ** Delete all information from a single table in the database. iTable is |
| 4522 | ** the page number of the root of the table. After this routine returns, |
| 4523 | ** the root page is empty, but still exists. |
| 4524 | ** |
| 4525 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 4526 | ** read cursors on the table. Open write cursors are moved to the |
| 4527 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4528 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4529 | int sqlite3BtreeClearTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4530 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4531 | BtCursor *pCur; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4532 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4533 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4534 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4535 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 4536 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 4537 | if( pCur->wrFlag==0 ) return SQLITE_LOCKED; |
| 4538 | moveToRoot(pCur); |
| 4539 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4540 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4541 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4542 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4543 | sqlite3BtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4544 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4545 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* |
| 4549 | ** Erase all information in a table and add the root of the table to |
| 4550 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4551 | ** page 1) is never added to the freelist. |
| 4552 | ** |
| 4553 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 4554 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4555 | ** |
| 4556 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 4557 | ** root page in the database file, then the last root page |
| 4558 | ** in the database file is moved into the slot formerly occupied by |
| 4559 | ** iTable and that last slot formerly occupied by the last root page |
| 4560 | ** is added to the freelist instead of iTable. In this say, all |
| 4561 | ** root pages are kept at the beginning of the database file, which |
| 4562 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 4563 | ** page number that used to be the last root page in the file before |
| 4564 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 4565 | ** The last root page is recorded in meta[3] and the value of |
| 4566 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4567 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4568 | int sqlite3BtreeDropTable(Btree *pBt, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4569 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4570 | MemPage *pPage = 0; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4571 | BtCursor *pCur; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4572 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4573 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4574 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4575 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4576 | |
| 4577 | /* TODO: Disallow schema modifications if there are open cursors */ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4578 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 4579 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 4580 | return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */ |
| 4581 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 4582 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4583 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4584 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4585 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4586 | rc = sqlite3BtreeClearTable(pBt, iTable); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4587 | if( rc ) return rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4588 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4589 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4590 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4591 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4592 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4593 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4594 | releasePage(pPage); |
| 4595 | #else |
| 4596 | if( pBt->autoVacuum ){ |
| 4597 | Pgno maxRootPgno; |
| 4598 | rc = sqlite3BtreeGetMeta(pBt, 4, &maxRootPgno); |
| 4599 | if( rc!=SQLITE_OK ){ |
| 4600 | releasePage(pPage); |
| 4601 | return rc; |
| 4602 | } |
| 4603 | |
| 4604 | if( iTable==maxRootPgno ){ |
| 4605 | /* If the table being dropped is the table with the largest root-page |
| 4606 | ** number in the database, put the root page on the free list. |
| 4607 | */ |
| 4608 | rc = freePage(pPage); |
| 4609 | releasePage(pPage); |
| 4610 | if( rc!=SQLITE_OK ){ |
| 4611 | return rc; |
| 4612 | } |
| 4613 | }else{ |
| 4614 | /* The table being dropped does not have the largest root-page |
| 4615 | ** number in the database. So move the page that does into the |
| 4616 | ** gap left by the deleted root-page. |
| 4617 | */ |
| 4618 | MemPage *pMove; |
| 4619 | releasePage(pPage); |
| 4620 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 4621 | if( rc!=SQLITE_OK ){ |
| 4622 | return rc; |
| 4623 | } |
| 4624 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 4625 | releasePage(pMove); |
| 4626 | if( rc!=SQLITE_OK ){ |
| 4627 | return rc; |
| 4628 | } |
| 4629 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 4630 | if( rc!=SQLITE_OK ){ |
| 4631 | return rc; |
| 4632 | } |
| 4633 | rc = freePage(pMove); |
| 4634 | releasePage(pMove); |
| 4635 | if( rc!=SQLITE_OK ){ |
| 4636 | return rc; |
| 4637 | } |
| 4638 | *piMoved = maxRootPgno; |
| 4639 | } |
| 4640 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 4641 | /* Set the new 'max-root-page' value in the database header. This |
| 4642 | ** is the old value less one, less one more if that happens to |
| 4643 | ** be a root-page number, less one again if that is the |
| 4644 | ** PENDING_BYTE_PAGE. |
| 4645 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4646 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 4647 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4648 | maxRootPgno--; |
| 4649 | } |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4650 | if( maxRootPgno==PTRMAP_PAGENO(pBt->pageSize, maxRootPgno) ){ |
| 4651 | maxRootPgno--; |
| 4652 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 4653 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 4654 | |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4655 | rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4656 | }else{ |
| 4657 | rc = freePage(pPage); |
| 4658 | releasePage(pPage); |
| 4659 | } |
| 4660 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4661 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4662 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4663 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4664 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4665 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4666 | return rc; |
| 4667 | } |
| 4668 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4669 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4670 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4671 | ** Read the meta-information out of a database file. Meta[0] |
| 4672 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4673 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 4674 | ** is read-only, the others are read/write. |
| 4675 | ** |
| 4676 | ** The schema layer numbers meta values differently. At the schema |
| 4677 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 4678 | ** 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] | 4679 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4680 | int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4681 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4682 | unsigned char *pP1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4683 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4684 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4685 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4686 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4687 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4688 | sqlite3pager_unref(pP1); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4689 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame^] | 4690 | /* If autovacuumed is disabled in this build but we are trying to |
| 4691 | ** access an autovacuumed database, then make the database readonly. |
| 4692 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4693 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4694 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4695 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4696 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4697 | return SQLITE_OK; |
| 4698 | } |
| 4699 | |
| 4700 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4701 | ** Write meta-information back into the database. Meta[0] is |
| 4702 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4703 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4704 | int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4705 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4706 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4707 | assert( idx>=1 && idx<=15 ); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4708 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4709 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 4710 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4711 | assert( pBt->pPage1!=0 ); |
| 4712 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4713 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4714 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4715 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4716 | return SQLITE_OK; |
| 4717 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4718 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4719 | /* |
| 4720 | ** Return the flag byte at the beginning of the page that the cursor |
| 4721 | ** is currently pointing to. |
| 4722 | */ |
| 4723 | int sqlite3BtreeFlags(BtCursor *pCur){ |
| 4724 | MemPage *pPage = pCur->pPage; |
| 4725 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 4726 | } |
| 4727 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4728 | /* |
| 4729 | ** Print a disassembly of the given page on standard output. This routine |
| 4730 | ** is used for debugging and testing only. |
| 4731 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4732 | #ifdef SQLITE_TEST |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4733 | int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4734 | int rc; |
| 4735 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 4736 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4737 | int nFree; |
| 4738 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4739 | int hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4740 | int nCell; |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4741 | int isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4742 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4743 | char range[20]; |
| 4744 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4745 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4746 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4747 | isInit = pPage->isInit; |
| 4748 | if( pPage->isInit==0 ){ |
| 4749 | initPage(pPage, 0); |
| 4750 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4751 | if( rc ){ |
| 4752 | return rc; |
| 4753 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4754 | hdr = pPage->hdrOffset; |
| 4755 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 4756 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4757 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 4758 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4759 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 4760 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4761 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4762 | nCell = get2byte(&data[hdr+3]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 4763 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4764 | data[hdr], data[hdr+7], |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4765 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4766 | assert( hdr == (pgno==1 ? 100 : 0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4767 | idx = hdr + 12 - pPage->leaf*4; |
| 4768 | for(i=0; i<nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4769 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4770 | Pgno child; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4771 | unsigned char *pCell; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4772 | int sz; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4773 | int addr; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4774 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4775 | addr = get2byte(&data[idx + 2*i]); |
| 4776 | pCell = &data[addr]; |
| 4777 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4778 | sz = info.nSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4779 | sprintf(range,"%d..%d", addr, addr+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4780 | if( pPage->leaf ){ |
| 4781 | child = 0; |
| 4782 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4783 | child = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4784 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4785 | sz = info.nData; |
| 4786 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4787 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4788 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4789 | for(j=0; j<sz; j++){ |
| 4790 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 4791 | } |
| 4792 | payload[sz] = 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 4793 | sqlite3DebugPrintf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4794 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 4795 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4796 | ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4797 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4798 | if( !pPage->leaf ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 4799 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4800 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4801 | nFree = 0; |
| 4802 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4803 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4804 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4805 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4806 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 4807 | nFree += sz; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 4808 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4809 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4810 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4811 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4812 | } |
| 4813 | if( idx!=0 ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 4814 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4815 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4816 | if( recursive && !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4817 | for(i=0; i<nCell; i++){ |
| 4818 | unsigned char *pCell = findCell(pPage, i); |
| 4819 | sqlite3BtreePageDump(pBt, get4byte(pCell), 1); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4820 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4821 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4822 | sqlite3BtreePageDump(pBt, get4byte(&data[hdr+8]), 1); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4823 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4824 | pPage->isInit = isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 4825 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 4826 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4827 | return SQLITE_OK; |
| 4828 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4829 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4830 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4831 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4832 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4833 | ** Fill aResult[] with information about the entry and page that the |
| 4834 | ** cursor is pointing to. |
| 4835 | ** |
| 4836 | ** aResult[0] = The page number |
| 4837 | ** aResult[1] = The entry number |
| 4838 | ** aResult[2] = Total number of entries on this page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4839 | ** aResult[3] = Cell size (local payload + header) |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4840 | ** aResult[4] = Number of free bytes on this page |
| 4841 | ** aResult[5] = Number of free blocks on the page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4842 | ** aResult[6] = Total payload size (local + overflow) |
| 4843 | ** aResult[7] = Header size in bytes |
| 4844 | ** aResult[8] = Local payload size |
| 4845 | ** aResult[9] = Parent page number |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4846 | ** |
| 4847 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4848 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4849 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4850 | int cnt, idx; |
| 4851 | MemPage *pPage = pCur->pPage; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4852 | BtCursor tmpCur; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4853 | |
| 4854 | pageIntegrity(pPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4855 | assert( pPage->isInit ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4856 | getTempCursor(pCur, &tmpCur); |
| 4857 | while( upCnt-- ){ |
| 4858 | moveToParent(&tmpCur); |
| 4859 | } |
| 4860 | pPage = tmpCur.pPage; |
| 4861 | pageIntegrity(pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4862 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4863 | assert( aResult[0]==pPage->pgno ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4864 | aResult[1] = tmpCur.idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4865 | aResult[2] = pPage->nCell; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4866 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 4867 | getCellInfo(&tmpCur); |
| 4868 | aResult[3] = tmpCur.info.nSize; |
| 4869 | aResult[6] = tmpCur.info.nData; |
| 4870 | aResult[7] = tmpCur.info.nHeader; |
| 4871 | aResult[8] = tmpCur.info.nLocal; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4872 | }else{ |
| 4873 | aResult[3] = 0; |
| 4874 | aResult[6] = 0; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4875 | aResult[7] = 0; |
| 4876 | aResult[8] = 0; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4877 | } |
| 4878 | aResult[4] = pPage->nFree; |
| 4879 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4880 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4881 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4882 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4883 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4884 | } |
| 4885 | aResult[5] = cnt; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 4886 | if( pPage->pParent==0 || isRootPage(pPage) ){ |
| 4887 | aResult[9] = 0; |
| 4888 | }else{ |
| 4889 | aResult[9] = pPage->pParent->pgno; |
| 4890 | } |
| 4891 | releaseTempCursor(&tmpCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4892 | return SQLITE_OK; |
| 4893 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4894 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4895 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4896 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4897 | ** Return the pager associated with a BTree. This routine is used for |
| 4898 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4899 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4900 | Pager *sqlite3BtreePager(Btree *pBt){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4901 | return pBt->pPager; |
| 4902 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4903 | |
| 4904 | /* |
| 4905 | ** This structure is passed around through all the sanity checking routines |
| 4906 | ** in order to keep track of some global state information. |
| 4907 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4908 | typedef struct IntegrityCk IntegrityCk; |
| 4909 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 4910 | Btree *pBt; /* The tree being checked out */ |
| 4911 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 4912 | int nPage; /* Number of pages in the database */ |
| 4913 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 4914 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4915 | }; |
| 4916 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4917 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4918 | /* |
| 4919 | ** Append a message to the error message string. |
| 4920 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4921 | static void checkAppendMsg( |
| 4922 | IntegrityCk *pCheck, |
| 4923 | char *zMsg1, |
| 4924 | const char *zFormat, |
| 4925 | ... |
| 4926 | ){ |
| 4927 | va_list ap; |
| 4928 | char *zMsg2; |
| 4929 | va_start(ap, zFormat); |
| 4930 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 4931 | va_end(ap); |
| 4932 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4933 | if( pCheck->zErrMsg ){ |
| 4934 | char *zOld = pCheck->zErrMsg; |
| 4935 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4936 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4937 | sqliteFree(zOld); |
| 4938 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 4939 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4940 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4941 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4942 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4943 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4944 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 4945 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4946 | /* |
| 4947 | ** Add 1 to the reference count for page iPage. If this is the second |
| 4948 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 4949 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 4950 | ** if this is the first reference to the page. |
| 4951 | ** |
| 4952 | ** Also check that the page number is in bounds. |
| 4953 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 4954 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4955 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 4956 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4957 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4958 | return 1; |
| 4959 | } |
| 4960 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4961 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4962 | return 1; |
| 4963 | } |
| 4964 | return (pCheck->anRef[iPage]++)>1; |
| 4965 | } |
| 4966 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4967 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4968 | /* |
| 4969 | ** Check that the entry in the pointer-map for page iChild maps to |
| 4970 | ** page iParent, pointer type ptrType. If not, append an error message |
| 4971 | ** to pCheck. |
| 4972 | */ |
| 4973 | static void checkPtrmap( |
| 4974 | IntegrityCk *pCheck, /* Integrity check context */ |
| 4975 | Pgno iChild, /* Child page number */ |
| 4976 | u8 eType, /* Expected pointer map type */ |
| 4977 | Pgno iParent, /* Expected pointer map parent page number */ |
| 4978 | char *zContext /* Context description (used for error msg) */ |
| 4979 | ){ |
| 4980 | int rc; |
| 4981 | u8 ePtrmapType; |
| 4982 | Pgno iPtrmapParent; |
| 4983 | |
| 4984 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 4985 | if( rc!=SQLITE_OK ){ |
| 4986 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 4987 | return; |
| 4988 | } |
| 4989 | |
| 4990 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 4991 | checkAppendMsg(pCheck, zContext, |
| 4992 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 4993 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 4994 | } |
| 4995 | } |
| 4996 | #endif |
| 4997 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 4998 | /* |
| 4999 | ** Check the integrity of the freelist or of an overflow page list. |
| 5000 | ** Verify that the number of pages on the list is N. |
| 5001 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5002 | static void checkList( |
| 5003 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 5004 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 5005 | int iPage, /* Page number for first page in the list */ |
| 5006 | int N, /* Expected number of pages in the list */ |
| 5007 | char *zContext /* Context for error messages */ |
| 5008 | ){ |
| 5009 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5010 | int expected = N; |
| 5011 | int iFirst = iPage; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5012 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5013 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5014 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5015 | checkAppendMsg(pCheck, zContext, |
| 5016 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5017 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | } |
| 5020 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5021 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5022 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5023 | break; |
| 5024 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5025 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5026 | int n = get4byte(&pOvfl[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5027 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5028 | if( pCheck->pBt->autoVacuum ){ |
| 5029 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 5030 | } |
| 5031 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 5032 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5033 | checkAppendMsg(pCheck, zContext, |
| 5034 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5035 | N--; |
| 5036 | }else{ |
| 5037 | for(i=0; i<n; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5038 | Pgno iFreePage = get4byte(&pOvfl[8+i*4]); |
| 5039 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5040 | if( pCheck->pBt->autoVacuum ){ |
| 5041 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 5042 | } |
| 5043 | #endif |
| 5044 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5045 | } |
| 5046 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5047 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5048 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5049 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5050 | else{ |
| 5051 | /* If this database supports auto-vacuum and iPage is not the last |
| 5052 | ** page in this overflow list, check that the pointer-map entry for |
| 5053 | ** the following page matches iPage. |
| 5054 | */ |
| 5055 | if( pCheck->pBt->autoVacuum && N>0 ){ |
| 5056 | i = get4byte(pOvfl); |
| 5057 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 5058 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5059 | } |
| 5060 | #endif |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5061 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5062 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5063 | } |
| 5064 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5065 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5066 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5067 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5068 | /* |
| 5069 | ** Do various sanity checks on a single page of a tree. Return |
| 5070 | ** the tree depth. Root pages return 0. Parents of root pages |
| 5071 | ** return 1, and so forth. |
| 5072 | ** |
| 5073 | ** These checks are done: |
| 5074 | ** |
| 5075 | ** 1. Make sure that cells and freeblocks do not overlap |
| 5076 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5077 | ** NO 2. Make sure cell keys are in order. |
| 5078 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 5079 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5080 | ** 5. Check the integrity of overflow pages. |
| 5081 | ** 6. Recursively call checkTreePage on all children. |
| 5082 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5083 | ** 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] | 5084 | ** the root of the tree. |
| 5085 | */ |
| 5086 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5087 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5088 | int iPage, /* Page number of the page to check */ |
| 5089 | MemPage *pParent, /* Parent page */ |
| 5090 | char *zParentContext, /* Parent context */ |
| 5091 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 5092 | int nLower, /* Number of characters in zLowerBound */ |
| 5093 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 5094 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5095 | ){ |
| 5096 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5097 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5098 | int hdr, cellStart; |
| 5099 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5100 | u8 *data; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5101 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 5102 | Btree *pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5103 | int maxLocal, usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5104 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5105 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5106 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5107 | sprintf(zContext, "Page %d: ", iPage); |
| 5108 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5109 | /* Check that the page exists |
| 5110 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 5111 | cur.pBt = pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5112 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5113 | if( iPage==0 ) return 0; |
| 5114 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5115 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5116 | checkAppendMsg(pCheck, zContext, |
| 5117 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5118 | return 0; |
| 5119 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5120 | maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5121 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5122 | checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5123 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5124 | return 0; |
| 5125 | } |
| 5126 | |
| 5127 | /* Check out all the cells. |
| 5128 | */ |
| 5129 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5130 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5131 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5132 | u8 *pCell; |
| 5133 | int sz; |
| 5134 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5135 | |
| 5136 | /* Check payload overflow pages |
| 5137 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5138 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5139 | pCell = findCell(pPage,i); |
| 5140 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5141 | sz = info.nData; |
| 5142 | if( !pPage->intKey ) sz += info.nKey; |
| 5143 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5144 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5145 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 5146 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5147 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5148 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5149 | } |
| 5150 | #endif |
| 5151 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | /* Check sanity of left child page. |
| 5155 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5156 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5157 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5158 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5159 | if( pBt->autoVacuum ){ |
| 5160 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 5161 | } |
| 5162 | #endif |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5163 | d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0); |
| 5164 | if( i>0 && d2!=depth ){ |
| 5165 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 5166 | } |
| 5167 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5168 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5169 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5170 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5171 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5172 | sprintf(zContext, "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5173 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5174 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5175 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5176 | } |
| 5177 | #endif |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5178 | checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0); |
| 5179 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5180 | |
| 5181 | /* Check for complete coverage of the page |
| 5182 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5183 | data = pPage->aData; |
| 5184 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5185 | hit = sqliteMalloc( usableSize ); |
| 5186 | if( hit ){ |
| 5187 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 5188 | nCell = get2byte(&data[hdr+3]); |
| 5189 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 5190 | for(i=0; i<nCell; i++){ |
| 5191 | int pc = get2byte(&data[cellStart+i*2]); |
| 5192 | int size = cellSizePtr(pPage, &data[pc]); |
| 5193 | int j; |
| 5194 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 5195 | } |
| 5196 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 5197 | cnt++){ |
| 5198 | int size = get2byte(&data[i+2]); |
| 5199 | int j; |
| 5200 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 5201 | i = get2byte(&data[i]); |
| 5202 | } |
| 5203 | for(i=cnt=0; i<usableSize; i++){ |
| 5204 | if( hit[i]==0 ){ |
| 5205 | cnt++; |
| 5206 | }else if( hit[i]>1 ){ |
| 5207 | checkAppendMsg(pCheck, 0, |
| 5208 | "Multiple uses for byte %d of page %d", i, iPage); |
| 5209 | break; |
| 5210 | } |
| 5211 | } |
| 5212 | if( cnt!=data[hdr+7] ){ |
| 5213 | checkAppendMsg(pCheck, 0, |
| 5214 | "Fragmented space is %d byte reported as %d on page %d", |
| 5215 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5216 | } |
| 5217 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5218 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5219 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5220 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5221 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5222 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5223 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5224 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5225 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5226 | /* |
| 5227 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 5228 | ** an array of pages numbers were each page number is the root page of |
| 5229 | ** a table. nRoot is the number of entries in aRoot. |
| 5230 | ** |
| 5231 | ** If everything checks out, this routine returns NULL. If something is |
| 5232 | ** amiss, an error message is written into memory obtained from malloc() |
| 5233 | ** and a pointer to that error message is returned. The calling function |
| 5234 | ** is responsible for freeing the error message when it is done. |
| 5235 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5236 | char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5237 | int i; |
| 5238 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5239 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5240 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5241 | nRef = *sqlite3pager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 5242 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 5243 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 5244 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5245 | sCheck.pBt = pBt; |
| 5246 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5247 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5248 | if( sCheck.nPage==0 ){ |
| 5249 | unlockBtreeIfUnused(pBt); |
| 5250 | return 0; |
| 5251 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 5252 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5253 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 5254 | i = PENDING_BYTE/pBt->pageSize + 1; |
| 5255 | if( i<=sCheck.nPage ){ |
| 5256 | sCheck.anRef[i] = 1; |
| 5257 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5258 | sCheck.zErrMsg = 0; |
| 5259 | |
| 5260 | /* Check the integrity of the freelist |
| 5261 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5262 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 5263 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5264 | |
| 5265 | /* Check all the tables. |
| 5266 | */ |
| 5267 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 5268 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5269 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5270 | /* Note: This is temporary code for use during development of auto-vacuum. */ |
| 5271 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 5272 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 5273 | } |
| 5274 | #endif |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 5275 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | /* Make sure every page in the file is referenced |
| 5279 | */ |
| 5280 | for(i=1; i<=sCheck.nPage; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5281 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5282 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5283 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5284 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5285 | #else |
| 5286 | /* If the database supports auto-vacuum, make sure no tables contain |
| 5287 | ** references to pointer-map pages. |
| 5288 | */ |
| 5289 | if( sCheck.anRef[i]==0 && |
| 5290 | (PTRMAP_PAGENO(pBt->pageSize, i)!=i || !pBt->autoVacuum) ){ |
| 5291 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 5292 | } |
| 5293 | if( sCheck.anRef[i]!=0 && |
| 5294 | (PTRMAP_PAGENO(pBt->pageSize, i)==i && pBt->autoVacuum) ){ |
| 5295 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 5296 | } |
| 5297 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | /* Make sure this analysis did not leave any unref() pages |
| 5301 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5302 | unlockBtreeIfUnused(pBt); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5303 | if( nRef != *sqlite3pager_stats(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5304 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5305 | "Outstanding page count goes from %d to %d during this analysis", |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5306 | nRef, *sqlite3pager_stats(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5307 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | /* Clean up and report errors. |
| 5311 | */ |
| 5312 | sqliteFree(sCheck.anRef); |
| 5313 | return sCheck.zErrMsg; |
| 5314 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5315 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 5316 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5317 | /* |
| 5318 | ** Return the full pathname of the underlying database file. |
| 5319 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5320 | const char *sqlite3BtreeGetFilename(Btree *pBt){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5321 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5322 | return sqlite3pager_filename(pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5326 | ** Return the pathname of the directory that contains the database file. |
| 5327 | */ |
| 5328 | const char *sqlite3BtreeGetDirname(Btree *pBt){ |
| 5329 | assert( pBt->pPager!=0 ); |
| 5330 | return sqlite3pager_dirname(pBt->pPager); |
| 5331 | } |
| 5332 | |
| 5333 | /* |
| 5334 | ** Return the pathname of the journal file for this database. The return |
| 5335 | ** value of this routine is the same regardless of whether the journal file |
| 5336 | ** has been created or not. |
| 5337 | */ |
| 5338 | const char *sqlite3BtreeGetJournalname(Btree *pBt){ |
| 5339 | assert( pBt->pPager!=0 ); |
| 5340 | return sqlite3pager_journalname(pBt->pPager); |
| 5341 | } |
| 5342 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5343 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5344 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5345 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 5346 | ** must be active for both files. |
| 5347 | ** |
| 5348 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5349 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5350 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5351 | int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5352 | int rc = SQLITE_OK; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5353 | Pgno i, nPage, nToPage; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5354 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 5355 | if( pBtTo->inTrans!=TRANS_WRITE || pBtFrom->inTrans!=TRANS_WRITE ){ |
| 5356 | return SQLITE_ERROR; |
| 5357 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5358 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5359 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 5360 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 5361 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5362 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5363 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5364 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5365 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5366 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5367 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5368 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5369 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 5370 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5371 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5372 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5373 | rc = sqlite3pager_write(pPage); |
| 5374 | sqlite3pager_unref(pPage); |
| 5375 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5376 | } |
| 5377 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5378 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5379 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5380 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5381 | sqlite3BtreeRollback(pBtTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5382 | } |
| 5383 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5384 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5385 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5386 | |
| 5387 | /* |
| 5388 | ** Return non-zero if a transaction is active. |
| 5389 | */ |
| 5390 | int sqlite3BtreeIsInTrans(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 5391 | return (pBt && (pBt->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | /* |
| 5395 | ** Return non-zero if a statement transaction is active. |
| 5396 | */ |
| 5397 | int sqlite3BtreeIsInStmt(Btree *pBt){ |
| 5398 | return (pBt && pBt->inStmt); |
| 5399 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 5400 | |
| 5401 | /* |
| 5402 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 5403 | ** |
| 5404 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 5405 | ** the name of a master journal file that should be written into the |
| 5406 | ** individual journal file, or is NULL, indicating no master journal file |
| 5407 | ** (single database transaction). |
| 5408 | ** |
| 5409 | ** When this is called, the master journal should already have been |
| 5410 | ** created, populated with this journal pointer and synced to disk. |
| 5411 | ** |
| 5412 | ** Once this is routine has returned, the only thing required to commit |
| 5413 | ** the write-transaction for this database file is to delete the journal. |
| 5414 | */ |
| 5415 | int sqlite3BtreeSync(Btree *pBt, const char *zMaster){ |
| 5416 | if( pBt->inTrans==TRANS_WRITE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5417 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5418 | Pgno nTrunc = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5419 | if( pBt->autoVacuum ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5420 | int rc = autoVacuumCommit(pBt, &nTrunc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5421 | if( rc!=SQLITE_OK ) return rc; |
| 5422 | } |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5423 | return sqlite3pager_sync(pBt->pPager, zMaster, nTrunc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5424 | #endif |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5425 | return sqlite3pager_sync(pBt->pPager, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 5426 | } |
| 5427 | return SQLITE_OK; |
| 5428 | } |