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 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.247 2005/02/12 08:59:56 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 */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 359 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 360 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 361 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 362 | ** The TRACE macro will print high-level status information about the |
| 363 | ** btree operation when the global variable sqlite3_btree_trace is |
| 364 | ** enabled. |
| 365 | */ |
| 366 | #if SQLITE_TEST |
| 367 | # define TRACE(X) if( sqlite3_btree_trace )\ |
| 368 | { sqlite3DebugPrintf X; fflush(stdout); } |
| 369 | #else |
| 370 | # define TRACE(X) |
| 371 | #endif |
| 372 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
| 373 | |
| 374 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 375 | ** Forward declaration |
| 376 | */ |
| 377 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
| 378 | |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 379 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 380 | ** Read or write a two- and four-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 381 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 382 | static u32 get2byte(unsigned char *p){ |
| 383 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 384 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 385 | static u32 get4byte(unsigned char *p){ |
| 386 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 387 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 388 | static void put2byte(unsigned char *p, u32 v){ |
| 389 | p[0] = v>>8; |
| 390 | p[1] = v; |
| 391 | } |
| 392 | static void put4byte(unsigned char *p, u32 v){ |
| 393 | p[0] = v>>24; |
| 394 | p[1] = v>>16; |
| 395 | p[2] = v>>8; |
| 396 | p[3] = v; |
| 397 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 398 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 399 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 400 | ** Routines to read and write variable-length integers. These used to |
| 401 | ** be defined locally, but now we use the varint routines in the util.c |
| 402 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 403 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 404 | #define getVarint sqlite3GetVarint |
| 405 | #define getVarint32 sqlite3GetVarint32 |
| 406 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 407 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 408 | /* The database page the PENDING_BYTE occupies. This page is never used. |
| 409 | ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They |
| 410 | ** should possibly be consolidated (presumably in pager.h). |
| 411 | */ |
| 412 | #define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 413 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 414 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 415 | /* |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 416 | ** These macros define the location of the pointer-map entry for a |
| 417 | ** database page. The first argument to each is the number of usable |
| 418 | ** bytes on each page of the database (often 1024). The second is the |
| 419 | ** page number to look up in the pointer map. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 420 | ** |
| 421 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
| 422 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
| 423 | ** the offset of the requested map entry. |
| 424 | ** |
| 425 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
| 426 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 427 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
| 428 | ** this test. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 429 | */ |
| 430 | #define PTRMAP_PAGENO(pgsz, pgno) (((pgno-2)/(pgsz/5+1))*(pgsz/5+1)+2) |
| 431 | #define PTRMAP_PTROFFSET(pgsz, pgno) (((pgno-2)%(pgsz/5+1)-1)*5) |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 432 | #define PTRMAP_ISPAGE(pgsz, pgno) (PTRMAP_PAGENO(pgsz,pgno)==pgno) |
| 433 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 434 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 435 | ** The pointer map is a lookup table that identifies the parent page for |
| 436 | ** each child page in the database file. The parent page is the page that |
| 437 | ** contains a pointer to the child. Every page in the database contains |
| 438 | ** 0 or 1 parent pages. (In this context 'database page' refers |
| 439 | ** to any page that is not part of the pointer map itself.) Each pointer map |
| 440 | ** entry consists of a single byte 'type' and a 4 byte parent page number. |
| 441 | ** The PTRMAP_XXX identifiers below are the valid types. |
| 442 | ** |
| 443 | ** The purpose of the pointer map is to facility moving pages from one |
| 444 | ** position in the file to another as part of autovacuum. When a page |
| 445 | ** is moved, the pointer in its parent must be updated to point to the |
| 446 | ** new location. The pointer map is used to locate the parent page quickly. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 447 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 448 | ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not |
| 449 | ** used in this case. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 450 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 451 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
| 452 | ** is not used in this case. |
| 453 | ** |
| 454 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
| 455 | ** overflow pages. The page number identifies the page that |
| 456 | ** contains the cell with a pointer to this overflow page. |
| 457 | ** |
| 458 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
| 459 | ** overflow pages. The page-number identifies the previous |
| 460 | ** page in the overflow page list. |
| 461 | ** |
| 462 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
| 463 | ** identifies the parent page in the btree. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 464 | */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 465 | #define PTRMAP_ROOTPAGE 1 |
| 466 | #define PTRMAP_FREEPAGE 2 |
| 467 | #define PTRMAP_OVERFLOW1 3 |
| 468 | #define PTRMAP_OVERFLOW2 4 |
| 469 | #define PTRMAP_BTREE 5 |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 473 | ** |
| 474 | ** This routine updates the pointer map entry for page number 'key' |
| 475 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 476 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 477 | */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 478 | static int ptrmapPut(Btree *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 479 | u8 *pPtrmap; /* The pointer map page */ |
| 480 | Pgno iPtrmap; /* The pointer map page number */ |
| 481 | int offset; /* Offset in pointer map page */ |
| 482 | int rc; |
| 483 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 484 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 485 | if( key==0 ){ |
| 486 | return SQLITE_CORRUPT; |
| 487 | } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 488 | iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 489 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 490 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 491 | return rc; |
| 492 | } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 493 | offset = PTRMAP_PTROFFSET(pBt->usableSize, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 494 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 495 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 496 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 497 | rc = sqlite3pager_write(pPtrmap); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 498 | if( rc==SQLITE_OK ){ |
| 499 | pPtrmap[offset] = eType; |
| 500 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 501 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | sqlite3pager_unref(pPtrmap); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 505 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 510 | ** |
| 511 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 512 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 513 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 514 | */ |
| 515 | static int ptrmapGet(Btree *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
| 516 | int iPtrmap; /* Pointer map page index */ |
| 517 | u8 *pPtrmap; /* Pointer map page data */ |
| 518 | int offset; /* Offset of entry in pointer map */ |
| 519 | int rc; |
| 520 | |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 521 | iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 522 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
| 523 | if( rc!=0 ){ |
| 524 | return rc; |
| 525 | } |
| 526 | |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 527 | offset = PTRMAP_PTROFFSET(pBt->usableSize, key); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 528 | if( pEType ) *pEType = pPtrmap[offset]; |
| 529 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 530 | |
| 531 | sqlite3pager_unref(pPtrmap); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 532 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 533 | return SQLITE_OK; |
| 534 | } |
| 535 | |
| 536 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 537 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 538 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 539 | ** Given a btree page and a cell index (0 means the first cell on |
| 540 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 541 | ** to the cell content. |
| 542 | ** |
| 543 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 544 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 545 | static u8 *findCell(MemPage *pPage, int iCell){ |
| 546 | u8 *data = pPage->aData; |
| 547 | assert( iCell>=0 ); |
| 548 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
| 549 | return data + get2byte(&data[pPage->cellOffset+2*iCell]); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | ** This a more complex version of findCell() that works for |
| 554 | ** pages that do contain overflow cells. See insert |
| 555 | */ |
| 556 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 557 | int i; |
| 558 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 559 | int k; |
| 560 | struct _OvflCell *pOvfl; |
| 561 | pOvfl = &pPage->aOvfl[i]; |
| 562 | k = pOvfl->idx; |
| 563 | if( k<=iCell ){ |
| 564 | if( k==iCell ){ |
| 565 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 566 | } |
| 567 | iCell--; |
| 568 | } |
| 569 | } |
| 570 | return findCell(pPage, iCell); |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 575 | ** are two versions of this function. parseCell() takes a cell index |
| 576 | ** as the second argument and parseCellPtr() takes a pointer to the |
| 577 | ** body of the cell as its second argument. |
| 578 | */ |
| 579 | static void parseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 580 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 581 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 582 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 583 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 584 | int n; /* Number bytes in cell content header */ |
| 585 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 586 | |
| 587 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 588 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 589 | n = pPage->childPtrSize; |
| 590 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 591 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 592 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 593 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 594 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 595 | } |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 596 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 597 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 598 | pInfo->nData = nPayload; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 599 | if( !pPage->intKey ){ |
| 600 | nPayload += pInfo->nKey; |
| 601 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 602 | if( nPayload<=pPage->maxLocal ){ |
| 603 | /* This is the (easy) common case where the entire payload fits |
| 604 | ** on the local page. No overflow is required. |
| 605 | */ |
| 606 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 607 | pInfo->nLocal = nPayload; |
| 608 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 609 | nSize = nPayload + n; |
| 610 | if( nSize<4 ){ |
| 611 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 612 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 613 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 614 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 615 | /* If the payload will not fit completely on the local page, we have |
| 616 | ** to decide how much to store locally and how much to spill onto |
| 617 | ** overflow pages. The strategy is to minimize the amount of unused |
| 618 | ** space on overflow pages while keeping the amount of local storage |
| 619 | ** in between minLocal and maxLocal. |
| 620 | ** |
| 621 | ** Warning: changing the way overflow payload is distributed in any |
| 622 | ** way will result in an incompatible file format. |
| 623 | */ |
| 624 | int minLocal; /* Minimum amount of payload held locally */ |
| 625 | int maxLocal; /* Maximum amount of payload held locally */ |
| 626 | int surplus; /* Overflow payload available for local storage */ |
| 627 | |
| 628 | minLocal = pPage->minLocal; |
| 629 | maxLocal = pPage->maxLocal; |
| 630 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 631 | if( surplus <= maxLocal ){ |
| 632 | pInfo->nLocal = surplus; |
| 633 | }else{ |
| 634 | pInfo->nLocal = minLocal; |
| 635 | } |
| 636 | pInfo->iOverflow = pInfo->nLocal + n; |
| 637 | pInfo->nSize = pInfo->iOverflow + 4; |
| 638 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 639 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 640 | static void parseCell( |
| 641 | MemPage *pPage, /* Page containing the cell */ |
| 642 | int iCell, /* The cell index. First cell is 0 */ |
| 643 | CellInfo *pInfo /* Fill in this structure */ |
| 644 | ){ |
| 645 | parseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 646 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 647 | |
| 648 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 649 | ** Compute the total number of bytes that a Cell needs in the cell |
| 650 | ** data area of the btree-page. The return number includes the cell |
| 651 | ** data header and the local payload, but not any overflow page or |
| 652 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 653 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 654 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 655 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 656 | CellInfo info; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 657 | parseCell(pPage, iCell, &info); |
| 658 | return info.nSize; |
| 659 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 660 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 661 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 662 | CellInfo info; |
| 663 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 664 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 665 | } |
| 666 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 667 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 668 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 669 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 670 | ** to an overflow page, insert an entry into the pointer-map |
| 671 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 672 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 673 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 674 | if( pCell ){ |
| 675 | CellInfo info; |
| 676 | parseCellPtr(pPage, pCell, &info); |
| 677 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 678 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 679 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 680 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 681 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 682 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 683 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 684 | /* |
| 685 | ** If the cell with index iCell on page pPage contains a pointer |
| 686 | ** to an overflow page, insert an entry into the pointer-map |
| 687 | ** for the overflow page. |
| 688 | */ |
| 689 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 690 | u8 *pCell; |
| 691 | pCell = findOverflowCell(pPage, iCell); |
| 692 | return ptrmapPutOvflPtr(pPage, pCell); |
| 693 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 694 | #endif |
| 695 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 696 | |
| 697 | /* |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 698 | ** Do sanity checking on a page. Throw an exception if anything is |
| 699 | ** not right. |
| 700 | ** |
| 701 | ** This routine is used for internal error checking only. It is omitted |
| 702 | ** from most builds. |
| 703 | */ |
| 704 | #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0 |
| 705 | static void _pageIntegrity(MemPage *pPage){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 706 | int usableSize; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 707 | u8 *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 708 | int i, j, idx, c, pc, hdr, nFree; |
| 709 | int cellOffset; |
| 710 | int nCell, cellLimit; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 711 | u8 *used; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 712 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 713 | used = sqliteMallocRaw( pPage->pBt->pageSize ); |
| 714 | if( used==0 ) return; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 715 | usableSize = pPage->pBt->usableSize; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 716 | assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->psAligned] ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 717 | hdr = pPage->hdrOffset; |
| 718 | assert( hdr==(pPage->pgno==1 ? 100 : 0) ); |
| 719 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
| 720 | c = pPage->aData[hdr]; |
| 721 | if( pPage->isInit ){ |
| 722 | assert( pPage->leaf == ((c & PTF_LEAF)!=0) ); |
| 723 | assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 724 | assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) ); |
| 725 | assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) ); |
| 726 | assert( pPage->hasData == |
| 727 | !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 728 | assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf ); |
| 729 | assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 730 | } |
| 731 | data = pPage->aData; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 732 | memset(used, 0, usableSize); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 733 | for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1; |
| 734 | nFree = 0; |
| 735 | pc = get2byte(&data[hdr+1]); |
| 736 | while( pc ){ |
| 737 | int size; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 738 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 739 | size = get2byte(&data[pc+2]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 740 | assert( pc+size<=usableSize ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 741 | nFree += size; |
| 742 | for(i=pc; i<pc+size; i++){ |
| 743 | assert( used[i]==0 ); |
| 744 | used[i] = 1; |
| 745 | } |
| 746 | pc = get2byte(&data[pc]); |
| 747 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 748 | idx = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 749 | nCell = get2byte(&data[hdr+3]); |
| 750 | cellLimit = get2byte(&data[hdr+5]); |
| 751 | assert( pPage->isInit==0 |
| 752 | || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) ); |
| 753 | cellOffset = pPage->cellOffset; |
| 754 | for(i=0; i<nCell; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 755 | int size; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 756 | pc = get2byte(&data[cellOffset+2*i]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 757 | assert( pc>0 && pc<usableSize-4 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 758 | size = cellSize(pPage, &data[pc]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 759 | assert( pc+size<=usableSize ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 760 | for(j=pc; j<pc+size; j++){ |
| 761 | assert( used[j]==0 ); |
| 762 | used[j] = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 763 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 764 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 765 | for(i=cellOffset+2*nCell; i<cellimit; i++){ |
| 766 | assert( used[i]==0 ); |
| 767 | used[i] = 1; |
| 768 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 769 | nFree = 0; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 770 | for(i=0; i<usableSize; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 771 | assert( used[i]<=1 ); |
| 772 | if( used[i]==0 ) nFree++; |
| 773 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 774 | assert( nFree==data[hdr+7] ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 775 | sqliteFree(used); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 776 | } |
| 777 | #define pageIntegrity(X) _pageIntegrity(X) |
| 778 | #else |
| 779 | # define pageIntegrity(X) |
| 780 | #endif |
| 781 | |
| 782 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 783 | ** Defragment the page given. All Cells are moved to the |
| 784 | ** beginning of the page and all free space is collected |
| 785 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 786 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 787 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 788 | int i; /* Loop counter */ |
| 789 | int pc; /* Address of a i-th cell */ |
| 790 | int addr; /* Offset of first byte after cell pointer array */ |
| 791 | int hdr; /* Offset to the page header */ |
| 792 | int size; /* Size of a cell */ |
| 793 | int usableSize; /* Number of usable bytes on a page */ |
| 794 | int cellOffset; /* Offset to the cell pointer array */ |
| 795 | int brk; /* Offset to the cell content area */ |
| 796 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 797 | unsigned char *data; /* The page data */ |
| 798 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 799 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 800 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 801 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 802 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 803 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 804 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 805 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 806 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 807 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 808 | cellOffset = pPage->cellOffset; |
| 809 | nCell = pPage->nCell; |
| 810 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 811 | usableSize = pPage->pBt->usableSize; |
| 812 | brk = get2byte(&data[hdr+5]); |
| 813 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 814 | brk = usableSize; |
| 815 | for(i=0; i<nCell; i++){ |
| 816 | u8 *pAddr; /* The i-th cell pointer */ |
| 817 | pAddr = &data[cellOffset + i*2]; |
| 818 | pc = get2byte(pAddr); |
| 819 | assert( pc<pPage->pBt->usableSize ); |
| 820 | size = cellSizePtr(pPage, &temp[pc]); |
| 821 | brk -= size; |
| 822 | memcpy(&data[brk], &temp[pc], size); |
| 823 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 824 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 825 | assert( brk>=cellOffset+2*nCell ); |
| 826 | put2byte(&data[hdr+5], brk); |
| 827 | data[hdr+1] = 0; |
| 828 | data[hdr+2] = 0; |
| 829 | data[hdr+7] = 0; |
| 830 | addr = cellOffset+2*nCell; |
| 831 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 832 | sqliteFree(temp); |
| 833 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 834 | } |
| 835 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 836 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 837 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 838 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 839 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 840 | ** the new allocation. Or return 0 if there is not enough free |
| 841 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 842 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 843 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 844 | ** nBytes of contiguous free space, then this routine automatically |
| 845 | ** calls defragementPage() to consolidate all free space before |
| 846 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 847 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 848 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 849 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 850 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 851 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 852 | int top; |
| 853 | int nCell; |
| 854 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 855 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 856 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 857 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 858 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 859 | assert( pPage->pBt ); |
| 860 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 861 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 862 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 863 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 864 | |
| 865 | nFrag = data[hdr+7]; |
| 866 | if( nFrag<60 ){ |
| 867 | /* Search the freelist looking for a slot big enough to satisfy the |
| 868 | ** space request. */ |
| 869 | addr = hdr+1; |
| 870 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 871 | size = get2byte(&data[pc+2]); |
| 872 | if( size>=nByte ){ |
| 873 | if( size<nByte+4 ){ |
| 874 | memcpy(&data[addr], &data[pc], 2); |
| 875 | data[hdr+7] = nFrag + size - nByte; |
| 876 | return pc; |
| 877 | }else{ |
| 878 | put2byte(&data[pc+2], size-nByte); |
| 879 | return pc + size - nByte; |
| 880 | } |
| 881 | } |
| 882 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 885 | |
| 886 | /* Allocate memory from the gap in between the cell pointer array |
| 887 | ** and the cell content area. |
| 888 | */ |
| 889 | top = get2byte(&data[hdr+5]); |
| 890 | nCell = get2byte(&data[hdr+3]); |
| 891 | cellOffset = pPage->cellOffset; |
| 892 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 893 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 894 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 895 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 896 | top -= nByte; |
| 897 | assert( cellOffset + 2*nCell <= top ); |
| 898 | put2byte(&data[hdr+5], top); |
| 899 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 903 | ** Return a section of the pPage->aData to the freelist. |
| 904 | ** The first byte of the new free block is pPage->aDisk[start] |
| 905 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 906 | ** |
| 907 | ** Most of the effort here is involved in coalesing adjacent |
| 908 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 909 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 910 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 911 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 912 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 913 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 914 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 915 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 916 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 917 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 918 | if( size<4 ) size = 4; |
| 919 | |
| 920 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 921 | hdr = pPage->hdrOffset; |
| 922 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 923 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 924 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 925 | assert( pbegin>addr ); |
| 926 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 927 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 928 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 929 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 930 | put2byte(&data[addr], start); |
| 931 | put2byte(&data[start], pbegin); |
| 932 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 933 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 934 | |
| 935 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 936 | addr = pPage->hdrOffset + 1; |
| 937 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 938 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 939 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 940 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 941 | pnext = get2byte(&data[pbegin]); |
| 942 | psize = get2byte(&data[pbegin+2]); |
| 943 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 944 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 945 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 946 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 947 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 948 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 949 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 950 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 953 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 954 | /* If the cell content area begins with a freeblock, remove it. */ |
| 955 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 956 | int top; |
| 957 | pbegin = get2byte(&data[hdr+1]); |
| 958 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 959 | top = get2byte(&data[hdr+5]); |
| 960 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 961 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 965 | ** Decode the flags byte (the first byte of the header) for a page |
| 966 | ** and initialize fields of the MemPage structure accordingly. |
| 967 | */ |
| 968 | static void decodeFlags(MemPage *pPage, int flagByte){ |
| 969 | Btree *pBt; /* A copy of pPage->pBt */ |
| 970 | |
| 971 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 972 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 973 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 974 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 975 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 976 | pBt = pPage->pBt; |
| 977 | if( flagByte & PTF_LEAFDATA ){ |
| 978 | pPage->leafData = 1; |
| 979 | pPage->maxLocal = pBt->maxLeaf; |
| 980 | pPage->minLocal = pBt->minLeaf; |
| 981 | }else{ |
| 982 | pPage->leafData = 0; |
| 983 | pPage->maxLocal = pBt->maxLocal; |
| 984 | pPage->minLocal = pBt->minLocal; |
| 985 | } |
| 986 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 987 | } |
| 988 | |
| 989 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 990 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 991 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 992 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 993 | ** is the parent of the page being initialized. The root of a |
| 994 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 995 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 996 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 997 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 998 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 999 | ** guarantee that the page is well-formed. It only shows that |
| 1000 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1001 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1002 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1003 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1004 | MemPage *pParent /* The parent. Might be NULL */ |
| 1005 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1006 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1007 | int hdr; /* Offset to beginning of page header */ |
| 1008 | u8 *data; /* Equal to pPage->aData */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1009 | Btree *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1010 | int usableSize; /* Amount of usable space on each page */ |
| 1011 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 1012 | int nFree; /* Number of unused bytes on the page */ |
| 1013 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1014 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1015 | pBt = pPage->pBt; |
| 1016 | assert( pBt!=0 ); |
| 1017 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1018 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1019 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->psAligned] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1020 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 1021 | /* The parent page should never change unless the file is corrupt */ |
| 1022 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1023 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1024 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1025 | if( pPage->pParent==0 && pParent!=0 ){ |
| 1026 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1027 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1028 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1029 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1030 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1031 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1032 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 1033 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1034 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1035 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1036 | top = get2byte(&data[hdr+5]); |
| 1037 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1038 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1039 | /* To many cells for a single page. The page must be corrupt */ |
| 1040 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1041 | } |
| 1042 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 1043 | /* All pages must have at least one cell, except for root pages */ |
| 1044 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1045 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1048 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1049 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1050 | while( pc>0 ){ |
| 1051 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1052 | if( pc>usableSize-4 ){ |
| 1053 | /* Free block is off the page */ |
| 1054 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1055 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1056 | next = get2byte(&data[pc]); |
| 1057 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1058 | if( next>0 && next<=pc+size+3 ){ |
| 1059 | /* Free blocks must be in accending order */ |
| 1060 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1061 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1062 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1063 | pc = next; |
| 1064 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1065 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1066 | if( nFree>=usableSize ){ |
| 1067 | /* Free space cannot exceed total page size */ |
| 1068 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1069 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1070 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1071 | pPage->isInit = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1072 | pageIntegrity(pPage); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1073 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1077 | ** Set up a raw page so that it looks like a database page holding |
| 1078 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1079 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1080 | static void zeroPage(MemPage *pPage, int flags){ |
| 1081 | unsigned char *data = pPage->aData; |
| 1082 | Btree *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1083 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1084 | int first; |
| 1085 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1086 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1087 | assert( &data[pBt->psAligned] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1088 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1089 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1090 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1091 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1092 | memset(&data[hdr+1], 0, 4); |
| 1093 | data[hdr+7] = 0; |
| 1094 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1095 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1096 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1097 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1098 | pPage->cellOffset = first; |
| 1099 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1100 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1101 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1102 | pPage->isInit = 1; |
| 1103 | pageIntegrity(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1107 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1108 | ** MemPage.aData elements if needed. |
| 1109 | */ |
| 1110 | static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){ |
| 1111 | int rc; |
| 1112 | unsigned char *aData; |
| 1113 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1114 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1115 | if( rc ) return rc; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1116 | pPage = (MemPage*)&aData[pBt->psAligned]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1117 | pPage->aData = aData; |
| 1118 | pPage->pBt = pBt; |
| 1119 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1120 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1121 | *ppPage = pPage; |
| 1122 | return SQLITE_OK; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1126 | ** Get a page from the pager and initialize it. This routine |
| 1127 | ** is just a convenience wrapper around separate calls to |
| 1128 | ** getPage() and initPage(). |
| 1129 | */ |
| 1130 | static int getAndInitPage( |
| 1131 | Btree *pBt, /* The database file */ |
| 1132 | Pgno pgno, /* Number of the page to get */ |
| 1133 | MemPage **ppPage, /* Write the page pointer here */ |
| 1134 | MemPage *pParent /* Parent of the page */ |
| 1135 | ){ |
| 1136 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1137 | if( pgno==0 ){ |
| 1138 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 1139 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1140 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1141 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1142 | rc = initPage(*ppPage, pParent); |
| 1143 | } |
| 1144 | return rc; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1148 | ** Release a MemPage. This should be called once for each prior |
| 1149 | ** call to getPage. |
| 1150 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1151 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1152 | if( pPage ){ |
| 1153 | assert( pPage->aData ); |
| 1154 | assert( pPage->pBt ); |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1155 | assert( &pPage->aData[pPage->pBt->psAligned]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1156 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1161 | ** This routine is called when the reference count for a page |
| 1162 | ** reaches zero. We need to unref the pParent pointer when that |
| 1163 | ** happens. |
| 1164 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1165 | static void pageDestructor(void *pData, int pageSize){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1166 | MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)]; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1167 | if( pPage->pParent ){ |
| 1168 | MemPage *pParent = pPage->pParent; |
| 1169 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1170 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1171 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1172 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1176 | ** During a rollback, when the pager reloads information into the cache |
| 1177 | ** so that the cache is restored to its original state at the start of |
| 1178 | ** the transaction, for each page restored this routine is called. |
| 1179 | ** |
| 1180 | ** This routine needs to reset the extra data section at the end of the |
| 1181 | ** page to agree with the restored data. |
| 1182 | */ |
| 1183 | static void pageReinit(void *pData, int pageSize){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1184 | MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)]; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1185 | if( pPage->isInit ){ |
| 1186 | pPage->isInit = 0; |
| 1187 | initPage(pPage, pPage->pParent); |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1192 | ** Open a database file. |
| 1193 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1194 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1195 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1196 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1197 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1198 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1199 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 1200 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1201 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1202 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1203 | Btree *pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1204 | int rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1205 | int nReserve; |
| 1206 | unsigned char zDbHeader[100]; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1207 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1208 | /* |
| 1209 | ** The following asserts make sure that structures used by the btree are |
| 1210 | ** the right size. This is to guard against size changes that result |
| 1211 | ** when compiling on a different architecture. |
| 1212 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 1213 | assert( sizeof(i64)==8 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1214 | assert( sizeof(u64)==8 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1215 | assert( sizeof(u32)==4 ); |
| 1216 | assert( sizeof(u16)==2 ); |
| 1217 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1218 | assert( sizeof(ptr)==sizeof(char*) ); |
| 1219 | assert( sizeof(uptr)==sizeof(ptr) ); |
| 1220 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1221 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1222 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1223 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1224 | return SQLITE_NOMEM; |
| 1225 | } |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1226 | rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1227 | if( rc!=SQLITE_OK ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1228 | if( pBt->pPager ) sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1229 | sqliteFree(pBt); |
| 1230 | *ppBtree = 0; |
| 1231 | return rc; |
| 1232 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1233 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1234 | sqlite3pager_set_reiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1235 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1236 | pBt->pPage1 = 0; |
| 1237 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1238 | sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader); |
| 1239 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1240 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){ |
| 1241 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1242 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1243 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1244 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1245 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1246 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1247 | ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM |
| 1248 | ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, |
| 1249 | ** then ":memory:" is just a regular file-name. Respect the auto-vacuum |
| 1250 | ** default in this case. |
| 1251 | */ |
| 1252 | #ifndef SQLITE_OMIT_MEMORYDB |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1253 | if( zFilename && strcmp(zFilename,":memory:") ){ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1254 | #else |
| 1255 | if( zFilename ){ |
| 1256 | #endif |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1257 | pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM; |
| 1258 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1259 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1260 | nReserve = 0; |
| 1261 | }else{ |
| 1262 | nReserve = zDbHeader[20]; |
| 1263 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1264 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1265 | pBt->minLeafFrac = zDbHeader[23]; |
| 1266 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1267 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1268 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1269 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1270 | } |
| 1271 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1272 | pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1273 | sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1274 | *ppBtree = pBt; |
| 1275 | return SQLITE_OK; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | ** Close an open database and invalidate all cursors. |
| 1280 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1281 | int sqlite3BtreeClose(Btree *pBt){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1282 | while( pBt->pCursor ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1283 | sqlite3BtreeCloseCursor(pBt->pCursor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1284 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1285 | sqlite3pager_close(pBt->pPager); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1286 | sqliteFree(pBt); |
| 1287 | return SQLITE_OK; |
| 1288 | } |
| 1289 | |
| 1290 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1291 | ** Change the busy handler callback function. |
| 1292 | */ |
| 1293 | int sqlite3BtreeSetBusyHandler(Btree *pBt, BusyHandler *pHandler){ |
| 1294 | sqlite3pager_set_busyhandler(pBt->pPager, pHandler); |
| 1295 | return SQLITE_OK; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1299 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1300 | ** |
| 1301 | ** The maximum number of cache pages is set to the absolute |
| 1302 | ** value of mxPage. If mxPage is negative, the pager will |
| 1303 | ** operate asynchronously - it will not stop to do fsync()s |
| 1304 | ** to insure data is written to the disk surface before |
| 1305 | ** continuing. Transactions still work if synchronous is off, |
| 1306 | ** and the database cannot be corrupted if this program |
| 1307 | ** crashes. But if the operating system crashes or there is |
| 1308 | ** an abrupt power failure when synchronous is off, the database |
| 1309 | ** could be left in an inconsistent and unrecoverable state. |
| 1310 | ** Synchronous is on by default so database corruption is not |
| 1311 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1312 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1313 | int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1314 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1315 | return SQLITE_OK; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1319 | ** Change the way data is synced to disk in order to increase or decrease |
| 1320 | ** how well the database resists damage due to OS crashes and power |
| 1321 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1322 | ** there is a high probability of damage) Level 2 is the default. There |
| 1323 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1324 | ** probability of damage to near zero but with a write performance reduction. |
| 1325 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1326 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1327 | int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1328 | sqlite3pager_set_safety_level(pBt->pPager, level); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1329 | return SQLITE_OK; |
| 1330 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1331 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1332 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1333 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1334 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1335 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1336 | ** |
| 1337 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1338 | ** size supplied does not meet this constraint then the page size is not |
| 1339 | ** changed. |
| 1340 | ** |
| 1341 | ** Page sizes are constrained to be a power of two so that the region |
| 1342 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1343 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1344 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1345 | ** |
| 1346 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1347 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1348 | */ |
| 1349 | int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){ |
| 1350 | if( pBt->pageSizeFixed ){ |
| 1351 | return SQLITE_READONLY; |
| 1352 | } |
| 1353 | if( nReserve<0 ){ |
| 1354 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1355 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1356 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1357 | ((pageSize-1)&pageSize)==0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1358 | pBt->pageSize = pageSize; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1359 | pBt->psAligned = FORCE_ALIGNMENT(pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1360 | sqlite3pager_set_pagesize(pBt->pPager, pageSize); |
| 1361 | } |
| 1362 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1363 | return SQLITE_OK; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | ** Return the currently defined page size |
| 1368 | */ |
| 1369 | int sqlite3BtreeGetPageSize(Btree *pBt){ |
| 1370 | return pBt->pageSize; |
| 1371 | } |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1372 | int sqlite3BtreeGetReserve(Btree *pBt){ |
| 1373 | return pBt->pageSize - pBt->usableSize; |
| 1374 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1375 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1378 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1379 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1380 | ** is disabled. The default value for the auto-vacuum property is |
| 1381 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1382 | */ |
| 1383 | int sqlite3BtreeSetAutoVacuum(Btree *pBt, int autoVacuum){ |
| 1384 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1385 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1386 | #else |
| 1387 | if( pBt->pageSizeFixed ){ |
| 1388 | return SQLITE_READONLY; |
| 1389 | } |
| 1390 | pBt->autoVacuum = (autoVacuum?1:0); |
| 1391 | return SQLITE_OK; |
| 1392 | #endif |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1397 | ** enabled 1 is returned. Otherwise 0. |
| 1398 | */ |
| 1399 | int sqlite3BtreeGetAutoVacuum(Btree *pBt){ |
| 1400 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 1401 | return 0; |
| 1402 | #else |
| 1403 | return pBt->autoVacuum; |
| 1404 | #endif |
| 1405 | } |
| 1406 | |
| 1407 | |
| 1408 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1409 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1410 | ** also acquire a readlock on that file. |
| 1411 | ** |
| 1412 | ** SQLITE_OK is returned on success. If the file is not a |
| 1413 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1414 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1415 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1416 | ** if there is a locking protocol violation. |
| 1417 | */ |
| 1418 | static int lockBtree(Btree *pBt){ |
| 1419 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1420 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1421 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1422 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1423 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1424 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1425 | |
| 1426 | /* Do some checking to help insure the file we opened really is |
| 1427 | ** a valid database file. |
| 1428 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1429 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1430 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1431 | u8 *page1 = pPage1->aData; |
| 1432 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1433 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1434 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1435 | if( page1[18]>1 || page1[19]>1 ){ |
| 1436 | goto page1_init_failed; |
| 1437 | } |
| 1438 | pBt->pageSize = get2byte(&page1[16]); |
| 1439 | pBt->usableSize = pBt->pageSize - page1[20]; |
| 1440 | if( pBt->usableSize<500 ){ |
| 1441 | goto page1_init_failed; |
| 1442 | } |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1443 | pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1444 | pBt->maxEmbedFrac = page1[21]; |
| 1445 | pBt->minEmbedFrac = page1[22]; |
| 1446 | pBt->minLeafFrac = page1[23]; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1447 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* maxLocal is the maximum amount of payload to store locally for |
| 1450 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1451 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1452 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1453 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1454 | ** 4-byte child pointer |
| 1455 | ** 9-byte nKey value |
| 1456 | ** 4-byte nData value |
| 1457 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1458 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1459 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1460 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1461 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1462 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1463 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1464 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1465 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1466 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1467 | goto page1_init_failed; |
| 1468 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1469 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1470 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1471 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1472 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1473 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1474 | releasePage(pPage1); |
| 1475 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1476 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1480 | ** If there are no outstanding cursors and we are not in the middle |
| 1481 | ** of a transaction but there is a read lock on the database, then |
| 1482 | ** this routine unrefs the first page of the database file which |
| 1483 | ** has the effect of releasing the read lock. |
| 1484 | ** |
| 1485 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1486 | ** |
| 1487 | ** If there is a transaction in progress, this routine is a no-op. |
| 1488 | */ |
| 1489 | static void unlockBtreeIfUnused(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1490 | if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1491 | if( pBt->pPage1->aData==0 ){ |
| 1492 | MemPage *pPage = pBt->pPage1; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 1493 | pPage->aData = &((char*)pPage)[-pBt->psAligned]; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1494 | pPage->pBt = pBt; |
| 1495 | pPage->pgno = 1; |
| 1496 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1497 | releasePage(pBt->pPage1); |
| 1498 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1499 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1504 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1505 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1506 | */ |
| 1507 | static int newDatabase(Btree *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1508 | MemPage *pP1; |
| 1509 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1510 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1511 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1512 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1513 | assert( pP1!=0 ); |
| 1514 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1515 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1516 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1517 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1518 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1519 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1520 | data[18] = 1; |
| 1521 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1522 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1523 | data[21] = pBt->maxEmbedFrac; |
| 1524 | data[22] = pBt->minEmbedFrac; |
| 1525 | data[23] = pBt->minLeafFrac; |
| 1526 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1527 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1528 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1529 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1530 | if( pBt->autoVacuum ){ |
| 1531 | put4byte(&data[36 + 4*4], 1); |
| 1532 | } |
| 1533 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1534 | return SQLITE_OK; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1538 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1539 | ** is started if the second argument is nonzero, otherwise a read- |
| 1540 | ** transaction. If the second argument is 2 or more and exclusive |
| 1541 | ** transaction is started, meaning that no other process is allowed |
| 1542 | ** to access the database. A preexisting transaction may not be |
| 1543 | ** upgrade to exclusive by calling this routine a second time - the |
| 1544 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1545 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1546 | ** A write-transaction must be started before attempting any |
| 1547 | ** changes to the database. None of the following routines |
| 1548 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1549 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1550 | ** sqlite3BtreeCreateTable() |
| 1551 | ** sqlite3BtreeCreateIndex() |
| 1552 | ** sqlite3BtreeClearTable() |
| 1553 | ** sqlite3BtreeDropTable() |
| 1554 | ** sqlite3BtreeInsert() |
| 1555 | ** sqlite3BtreeDelete() |
| 1556 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1557 | ** |
| 1558 | ** If wrflag is true, then nMaster specifies the maximum length of |
| 1559 | ** a master journal file name supplied later via sqlite3BtreeSync(). |
| 1560 | ** This is so that appropriate space can be allocated in the journal file |
| 1561 | ** when it is created.. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1562 | */ |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 1563 | int sqlite3BtreeBeginTrans(Btree *pBt, int wrflag){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1564 | int rc = SQLITE_OK; |
| 1565 | |
| 1566 | /* If the btree is already in a write-transaction, or it |
| 1567 | ** is already in a read-transaction and a read-transaction |
| 1568 | ** is requested, this is a no-op. |
| 1569 | */ |
| 1570 | if( pBt->inTrans==TRANS_WRITE || |
| 1571 | (pBt->inTrans==TRANS_READ && !wrflag) ){ |
| 1572 | return SQLITE_OK; |
| 1573 | } |
| 1574 | if( pBt->readOnly && wrflag ){ |
| 1575 | return SQLITE_READONLY; |
| 1576 | } |
| 1577 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1578 | if( pBt->pPage1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1579 | rc = lockBtree(pBt); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | if( rc==SQLITE_OK && wrflag ){ |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1583 | rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1584 | if( rc==SQLITE_OK ){ |
| 1585 | rc = newDatabase(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1586 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1587 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1588 | |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 1589 | if( rc==SQLITE_OK ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1590 | pBt->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1591 | if( wrflag ) pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1592 | }else{ |
| 1593 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1594 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1595 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1598 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1599 | |
| 1600 | /* |
| 1601 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1602 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1603 | ** map entries for the overflow pages as well. |
| 1604 | */ |
| 1605 | static int setChildPtrmaps(MemPage *pPage){ |
| 1606 | int i; /* Counter variable */ |
| 1607 | int nCell; /* Number of cells in page pPage */ |
| 1608 | int rc = SQLITE_OK; /* Return code */ |
| 1609 | Btree *pBt = pPage->pBt; |
| 1610 | int isInitOrig = pPage->isInit; |
| 1611 | Pgno pgno = pPage->pgno; |
| 1612 | |
| 1613 | initPage(pPage, 0); |
| 1614 | nCell = pPage->nCell; |
| 1615 | |
| 1616 | for(i=0; i<nCell; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1617 | u8 *pCell = findCell(pPage, i); |
| 1618 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1619 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1620 | if( rc!=SQLITE_OK ){ |
| 1621 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1622 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1623 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1624 | if( !pPage->leaf ){ |
| 1625 | Pgno childPgno = get4byte(pCell); |
| 1626 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1627 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | if( !pPage->leaf ){ |
| 1632 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1633 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1634 | } |
| 1635 | |
| 1636 | set_child_ptrmaps_out: |
| 1637 | pPage->isInit = isInitOrig; |
| 1638 | return rc; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1643 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1644 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1645 | ** follows: |
| 1646 | ** |
| 1647 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1648 | ** page of pPage. |
| 1649 | ** |
| 1650 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1651 | ** page pointed to by one of the cells on pPage. |
| 1652 | ** |
| 1653 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1654 | ** overflow page in the list. |
| 1655 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1656 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1657 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1658 | /* The pointer is always the first 4 bytes of the page in this case. */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1659 | if( get4byte(pPage->aData)!=iFrom ){ |
| 1660 | return SQLITE_CORRUPT; |
| 1661 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1662 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1663 | }else{ |
| 1664 | int isInitOrig = pPage->isInit; |
| 1665 | int i; |
| 1666 | int nCell; |
| 1667 | |
| 1668 | initPage(pPage, 0); |
| 1669 | nCell = pPage->nCell; |
| 1670 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1671 | for(i=0; i<nCell; i++){ |
| 1672 | u8 *pCell = findCell(pPage, i); |
| 1673 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1674 | CellInfo info; |
| 1675 | parseCellPtr(pPage, pCell, &info); |
| 1676 | if( info.iOverflow ){ |
| 1677 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1678 | put4byte(&pCell[info.iOverflow], iTo); |
| 1679 | break; |
| 1680 | } |
| 1681 | } |
| 1682 | }else{ |
| 1683 | if( get4byte(pCell)==iFrom ){ |
| 1684 | put4byte(pCell, iTo); |
| 1685 | break; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1691 | if( eType!=PTRMAP_BTREE || |
| 1692 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
| 1693 | return SQLITE_CORRUPT; |
| 1694 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1695 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1696 | } |
| 1697 | |
| 1698 | pPage->isInit = isInitOrig; |
| 1699 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1700 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1703 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1704 | /* |
| 1705 | ** Move the open database page pDbPage to location iFreePage in the |
| 1706 | ** database. The pDbPage reference remains valid. |
| 1707 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1708 | static int relocatePage( |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1709 | Btree *pBt, /* Btree */ |
| 1710 | MemPage *pDbPage, /* Open page to move */ |
| 1711 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 1712 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 1713 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1714 | ){ |
| 1715 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 1716 | Pgno iDbPage = pDbPage->pgno; |
| 1717 | Pager *pPager = pBt->pPager; |
| 1718 | int rc; |
| 1719 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1720 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 1721 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1722 | |
| 1723 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 1724 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 1725 | iDbPage, iFreePage, iPtrPage, eType)); |
| 1726 | rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage); |
| 1727 | if( rc!=SQLITE_OK ){ |
| 1728 | return rc; |
| 1729 | } |
| 1730 | pDbPage->pgno = iFreePage; |
| 1731 | |
| 1732 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 1733 | ** that point to overflow pages. The pointer map entries for all these |
| 1734 | ** pages need to be changed. |
| 1735 | ** |
| 1736 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 1737 | ** pointer to a subsequent overflow page. If this is the case, then |
| 1738 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 1739 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1740 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1741 | rc = setChildPtrmaps(pDbPage); |
| 1742 | if( rc!=SQLITE_OK ){ |
| 1743 | return rc; |
| 1744 | } |
| 1745 | }else{ |
| 1746 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 1747 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1748 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 1749 | if( rc!=SQLITE_OK ){ |
| 1750 | return rc; |
| 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 1756 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 1757 | ** iPtrPage. |
| 1758 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1759 | if( eType!=PTRMAP_ROOTPAGE ){ |
| 1760 | rc = getPage(pBt, iPtrPage, &pPtrPage); |
| 1761 | if( rc!=SQLITE_OK ){ |
| 1762 | return rc; |
| 1763 | } |
| 1764 | rc = sqlite3pager_write(pPtrPage->aData); |
| 1765 | if( rc!=SQLITE_OK ){ |
| 1766 | releasePage(pPtrPage); |
| 1767 | return rc; |
| 1768 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1769 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1770 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1771 | if( rc==SQLITE_OK ){ |
| 1772 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 1773 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1774 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1775 | return rc; |
| 1776 | } |
| 1777 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1778 | /* Forward declaration required by autoVacuumCommit(). */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 1779 | static int allocatePage(Btree *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1780 | |
| 1781 | /* |
| 1782 | ** This routine is called prior to sqlite3pager_commit when a transaction |
| 1783 | ** is commited for an auto-vacuum database. |
| 1784 | */ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1785 | static int autoVacuumCommit(Btree *pBt, Pgno *nTrunc){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1786 | Pager *pPager = pBt->pPager; |
| 1787 | Pgno nFreeList; /* Number of pages remaining on the free-list. */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1788 | int nPtrMap; /* Number of pointer-map pages deallocated */ |
| 1789 | Pgno origSize; /* Pages in the database file */ |
| 1790 | Pgno finSize; /* Pages in the database file after truncation */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1791 | int rc; /* Return code */ |
| 1792 | u8 eType; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1793 | int pgsz = pBt->pageSize; /* Page size for this database */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1794 | Pgno iDbPage; /* The database page to move */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1795 | MemPage *pDbMemPage = 0; /* "" */ |
| 1796 | Pgno iPtrPage; /* The page that contains a pointer to iDbPage */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1797 | Pgno iFreePage; /* The free-list page to move iDbPage to */ |
| 1798 | MemPage *pFreeMemPage = 0; /* "" */ |
| 1799 | |
| 1800 | #ifndef NDEBUG |
| 1801 | int nRef = *sqlite3pager_stats(pPager); |
| 1802 | #endif |
| 1803 | |
| 1804 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1805 | if( PTRMAP_ISPAGE(pgsz, sqlite3pager_pagecount(pPager)) ){ |
| 1806 | return SQLITE_CORRUPT; |
| 1807 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* Figure out how many free-pages are in the database. If there are no |
| 1810 | ** free pages, then auto-vacuum is a no-op. |
| 1811 | */ |
| 1812 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1813 | if( nFreeList==0 ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1814 | *nTrunc = 0; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1815 | return SQLITE_OK; |
| 1816 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1817 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1818 | origSize = sqlite3pager_pagecount(pPager); |
| 1819 | nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pgsz, origSize)+pgsz/5)/(pgsz/5); |
| 1820 | finSize = origSize - nFreeList - nPtrMap; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 1821 | if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){ |
| 1822 | finSize--; |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 1823 | if( PTRMAP_ISPAGE(pBt->usableSize, finSize) ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 1824 | finSize--; |
| 1825 | } |
| 1826 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1827 | TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize)); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1828 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1829 | /* Variable 'finSize' will be the size of the file in pages after |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1830 | ** the auto-vacuum has completed (the current file size minus the number |
| 1831 | ** of pages on the free list). Loop through the pages that lie beyond |
| 1832 | ** 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] | 1833 | ** to a free page earlier in the file (somewhere before finSize). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1834 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1835 | for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 1836 | /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */ |
| 1837 | if( PTRMAP_ISPAGE(pgsz, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){ |
| 1838 | continue; |
| 1839 | } |
| 1840 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1841 | rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage); |
| 1842 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 1843 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 1844 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 1845 | /* If iDbPage is free, do not swap it. */ |
| 1846 | if( eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1847 | continue; |
| 1848 | } |
| 1849 | rc = getPage(pBt, iDbPage, &pDbMemPage); |
| 1850 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1851 | |
| 1852 | /* Find the next page in the free-list that is not already at the end |
| 1853 | ** of the file. A page can be pulled off the free list using the |
| 1854 | ** allocatePage() routine. |
| 1855 | */ |
| 1856 | do{ |
| 1857 | if( pFreeMemPage ){ |
| 1858 | releasePage(pFreeMemPage); |
| 1859 | pFreeMemPage = 0; |
| 1860 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 1861 | rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1862 | if( rc!=SQLITE_OK ){ |
| 1863 | releasePage(pDbMemPage); |
| 1864 | goto autovacuum_out; |
| 1865 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 1866 | assert( iFreePage<=origSize ); |
| 1867 | }while( iFreePage>finSize ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1868 | releasePage(pFreeMemPage); |
| 1869 | pFreeMemPage = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1870 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1871 | rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1872 | releasePage(pDbMemPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1873 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | /* 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] | 1877 | ** truncate the database file to finSize pages and consider the |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1878 | ** free-list empty. |
| 1879 | */ |
| 1880 | rc = sqlite3pager_write(pBt->pPage1->aData); |
| 1881 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 1882 | put4byte(&pBt->pPage1->aData[32], 0); |
| 1883 | put4byte(&pBt->pPage1->aData[36], 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1884 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 1885 | *nTrunc = finSize; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1886 | |
| 1887 | autovacuum_out: |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1888 | assert( nRef==*sqlite3pager_stats(pPager) ); |
| 1889 | if( rc!=SQLITE_OK ){ |
| 1890 | sqlite3pager_rollback(pPager); |
| 1891 | } |
| 1892 | return rc; |
| 1893 | } |
| 1894 | #endif |
| 1895 | |
| 1896 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1897 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1898 | ** |
| 1899 | ** This will release the write lock on the database file. If there |
| 1900 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1901 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1902 | int sqlite3BtreeCommit(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1903 | int rc = SQLITE_OK; |
| 1904 | if( pBt->inTrans==TRANS_WRITE ){ |
| 1905 | rc = sqlite3pager_commit(pBt->pPager); |
| 1906 | } |
| 1907 | pBt->inTrans = TRANS_NONE; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1908 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1909 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1910 | return rc; |
| 1911 | } |
| 1912 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1913 | #ifndef NDEBUG |
| 1914 | /* |
| 1915 | ** Return the number of write-cursors open on this handle. This is for use |
| 1916 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 1917 | ** defined. |
| 1918 | */ |
| 1919 | static int countWriteCursors(Btree *pBt){ |
| 1920 | BtCursor *pCur; |
| 1921 | int r = 0; |
| 1922 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1923 | if( pCur->wrFlag ) r++; |
| 1924 | } |
| 1925 | return r; |
| 1926 | } |
| 1927 | #endif |
| 1928 | |
| 1929 | #if 0 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1930 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1931 | ** Invalidate all cursors |
| 1932 | */ |
| 1933 | static void invalidateCursors(Btree *pBt){ |
| 1934 | BtCursor *pCur; |
| 1935 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1936 | MemPage *pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1937 | if( pPage /* && !pPage->isInit */ ){ |
| 1938 | pageIntegrity(pPage); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1939 | releasePage(pPage); |
| 1940 | pCur->pPage = 0; |
| 1941 | pCur->isValid = 0; |
| 1942 | pCur->status = SQLITE_ABORT; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1946 | #endif |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1947 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1948 | #ifdef SQLITE_TEST |
| 1949 | /* |
| 1950 | ** Print debugging information about all cursors to standard output. |
| 1951 | */ |
| 1952 | void sqlite3BtreeCursorList(Btree *pBt){ |
| 1953 | BtCursor *pCur; |
| 1954 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 1955 | MemPage *pPage = pCur->pPage; |
| 1956 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 1957 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 1958 | pCur, pCur->pgnoRoot, zMode, |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1959 | pPage ? pPage->pgno : 0, pCur->idx, |
| 1960 | pCur->isValid ? "" : " eof" |
| 1961 | ); |
| 1962 | } |
| 1963 | } |
| 1964 | #endif |
| 1965 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 1966 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1967 | ** Rollback the transaction in progress. All cursors will be |
| 1968 | ** invalided by this operation. Any attempt to use a cursor |
| 1969 | ** that was open at the beginning of this operation will result |
| 1970 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1971 | ** |
| 1972 | ** This will release the write lock on the database file. If there |
| 1973 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1974 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1975 | int sqlite3BtreeRollback(Btree *pBt){ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1976 | int rc = SQLITE_OK; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1977 | MemPage *pPage1; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1978 | if( pBt->inTrans==TRANS_WRITE ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1979 | rc = sqlite3pager_rollback(pBt->pPager); |
| 1980 | /* The rollback may have destroyed the pPage1->aData value. So |
| 1981 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 1982 | ** set correctly. */ |
| 1983 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 1984 | releasePage(pPage1); |
| 1985 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 1986 | assert( countWriteCursors(pBt)==0 ); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1987 | } |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1988 | pBt->inTrans = TRANS_NONE; |
| 1989 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1990 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1991 | return rc; |
| 1992 | } |
| 1993 | |
| 1994 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 1995 | ** Start a statement subtransaction. The subtransaction can |
| 1996 | ** can be rolled back independently of the main transaction. |
| 1997 | ** You must start a transaction before starting a subtransaction. |
| 1998 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 1999 | ** commits or rolls back. |
| 2000 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2001 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2002 | ** to start a new subtransaction if another subtransaction is already active. |
| 2003 | ** |
| 2004 | ** Statement subtransactions are used around individual SQL statements |
| 2005 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2006 | ** error occurs within the statement, the effect of that one statement |
| 2007 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2008 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2009 | int sqlite3BtreeBeginStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2010 | int rc; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2011 | if( (pBt->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2012 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2013 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2014 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2015 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2016 | return rc; |
| 2017 | } |
| 2018 | |
| 2019 | |
| 2020 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2021 | ** Commit the statment subtransaction currently in progress. If no |
| 2022 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2023 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2024 | int sqlite3BtreeCommitStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2025 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2026 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2027 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2028 | }else{ |
| 2029 | rc = SQLITE_OK; |
| 2030 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2031 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2032 | return rc; |
| 2033 | } |
| 2034 | |
| 2035 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2036 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2037 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2038 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2039 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2040 | ** to use a cursor that was open at the beginning of this operation |
| 2041 | ** will result in an error. |
| 2042 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2043 | int sqlite3BtreeRollbackStmt(Btree *pBt){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2044 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2045 | if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2046 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2047 | assert( countWriteCursors(pBt)==0 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2048 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2049 | return rc; |
| 2050 | } |
| 2051 | |
| 2052 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2053 | ** Default key comparison function to be used if no comparison function |
| 2054 | ** is specified on the sqlite3BtreeCursor() call. |
| 2055 | */ |
| 2056 | static int dfltCompare( |
| 2057 | void *NotUsed, /* User data is not used */ |
| 2058 | int n1, const void *p1, /* First key to compare */ |
| 2059 | int n2, const void *p2 /* Second key to compare */ |
| 2060 | ){ |
| 2061 | int c; |
| 2062 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2063 | if( c==0 ){ |
| 2064 | c = n1 - n2; |
| 2065 | } |
| 2066 | return c; |
| 2067 | } |
| 2068 | |
| 2069 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2070 | ** Create a new cursor for the BTree whose root is on the page |
| 2071 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2072 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2073 | ** |
| 2074 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2075 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2076 | ** writing if other conditions for writing are also met. These |
| 2077 | ** are the conditions that must be met in order for writing to |
| 2078 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2079 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2080 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2081 | ** |
| 2082 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 2083 | ** |
| 2084 | ** 3: The database must be writable (not on read-only media) |
| 2085 | ** |
| 2086 | ** 4: There must be an active transaction. |
| 2087 | ** |
| 2088 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 2089 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 2090 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 2091 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 2092 | ** change as long as the cursor is open. This allows the cursor to |
| 2093 | ** do a sequential scan of the table without having to worry about |
| 2094 | ** entries being inserted or deleted during the scan. Cursors should |
| 2095 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 2096 | ** 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] | 2097 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2098 | ** should be opened with wrFlag==1 even if they never really intend |
| 2099 | ** to write. |
| 2100 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2101 | ** No checking is done to make sure that page iTable really is the |
| 2102 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2103 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2104 | ** |
| 2105 | ** The comparison function must be logically the same for every cursor |
| 2106 | ** on a particular table. Changing the comparison function will result |
| 2107 | ** in incorrect operations. If the comparison function is NULL, a |
| 2108 | ** default comparison function is used. The comparison function is |
| 2109 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2110 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2111 | int sqlite3BtreeCursor( |
| 2112 | Btree *pBt, /* The btree */ |
| 2113 | int iTable, /* Root page of table to open */ |
| 2114 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2115 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2116 | void *pArg, /* First arg to xCompare() */ |
| 2117 | BtCursor **ppCur /* Write new cursor here */ |
| 2118 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2119 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2120 | BtCursor *pCur; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2121 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2122 | *ppCur = 0; |
| 2123 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2124 | if( pBt->readOnly ){ |
| 2125 | return SQLITE_READONLY; |
| 2126 | } |
| 2127 | if( checkReadLocks(pBt, iTable, 0) ){ |
| 2128 | return SQLITE_LOCKED; |
| 2129 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2130 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2131 | if( pBt->pPage1==0 ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2132 | rc = lockBtree(pBt); |
| 2133 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2134 | return rc; |
| 2135 | } |
| 2136 | } |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 2137 | pCur = sqliteMallocRaw( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2138 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2139 | rc = SQLITE_NOMEM; |
| 2140 | goto create_cursor_exception; |
| 2141 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2142 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2143 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 2144 | rc = SQLITE_EMPTY; |
drh | eafe05b | 2004-06-13 00:54:01 +0000 | [diff] [blame] | 2145 | pCur->pPage = 0; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2146 | goto create_cursor_exception; |
| 2147 | } |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 2148 | pCur->pPage = 0; /* For exit-handler, in case getAndInitPage() fails. */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2149 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2150 | if( rc!=SQLITE_OK ){ |
| 2151 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2152 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2153 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2154 | pCur->pArg = pArg; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2155 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2156 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2157 | pCur->idx = 0; |
drh | 59eb676 | 2004-06-13 23:07:04 +0000 | [diff] [blame] | 2158 | memset(&pCur->info, 0, sizeof(pCur->info)); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2159 | pCur->pNext = pBt->pCursor; |
| 2160 | if( pCur->pNext ){ |
| 2161 | pCur->pNext->pPrev = pCur; |
| 2162 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2163 | pCur->pPrev = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2164 | pBt->pCursor = pCur; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2165 | pCur->isValid = 0; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2166 | *ppCur = pCur; |
| 2167 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2168 | |
| 2169 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2170 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2171 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2172 | sqliteFree(pCur); |
| 2173 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2174 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2175 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2178 | #if 0 /* Not Used */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2179 | /* |
| 2180 | ** Change the value of the comparison function used by a cursor. |
| 2181 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2182 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2183 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 2184 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 2185 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2186 | ){ |
| 2187 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2188 | pCur->pArg = pArg; |
| 2189 | } |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2190 | #endif |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2191 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2192 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2193 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2194 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2195 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2196 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2197 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2198 | if( pCur->pPrev ){ |
| 2199 | pCur->pPrev->pNext = pCur->pNext; |
| 2200 | }else{ |
| 2201 | pBt->pCursor = pCur->pNext; |
| 2202 | } |
| 2203 | if( pCur->pNext ){ |
| 2204 | pCur->pNext->pPrev = pCur->pPrev; |
| 2205 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2206 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2207 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2208 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2209 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2212 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2213 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2214 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2215 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2216 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2217 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2218 | pTempCur->pNext = 0; |
| 2219 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2220 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2221 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2222 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2226 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2227 | ** function above. |
| 2228 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2229 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2230 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2231 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2232 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2236 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2237 | ** If it is not already valid, call parseCell() to fill it in. |
| 2238 | ** |
| 2239 | ** BtCursor.info is a cache of the information in the current cell. |
| 2240 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2241 | */ |
| 2242 | static void getCellInfo(BtCursor *pCur){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2243 | if( pCur->info.nSize==0 ){ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2244 | parseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2245 | }else{ |
| 2246 | #ifndef NDEBUG |
| 2247 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2248 | memset(&info, 0, sizeof(info)); |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2249 | parseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2250 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 2251 | #endif |
| 2252 | } |
| 2253 | } |
| 2254 | |
| 2255 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2256 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2257 | ** the key for the current entry. If the cursor is not pointing |
| 2258 | ** to a valid entry, *pSize is set to 0. |
| 2259 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2260 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2261 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2262 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2263 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 2264 | if( !pCur->isValid ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2265 | *pSize = 0; |
| 2266 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2267 | getCellInfo(pCur); |
| 2268 | *pSize = pCur->info.nKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2269 | } |
| 2270 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2271 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2272 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2273 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2274 | ** Set *pSize to the number of bytes of data in the entry the |
| 2275 | ** cursor currently points to. Always return SQLITE_OK. |
| 2276 | ** Failure is not possible. If the cursor is not currently |
| 2277 | ** pointing to an entry (which can happen, for example, if |
| 2278 | ** the database is empty) then *pSize is set to 0. |
| 2279 | */ |
| 2280 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 2281 | if( !pCur->isValid ){ |
danielk1977 | 96fc5fe | 2004-05-13 11:34:16 +0000 | [diff] [blame] | 2282 | /* Not pointing at a valid entry - set *pSize to 0. */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2283 | *pSize = 0; |
| 2284 | }else{ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2285 | getCellInfo(pCur); |
| 2286 | *pSize = pCur->info.nData; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2287 | } |
| 2288 | return SQLITE_OK; |
| 2289 | } |
| 2290 | |
| 2291 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2292 | ** Read payload information from the entry that the pCur cursor is |
| 2293 | ** pointing to. Begin reading the payload at "offset" and read |
| 2294 | ** a total of "amt" bytes. Put the result in zBuf. |
| 2295 | ** |
| 2296 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2297 | ** It just reads bytes from the payload area. Data might appear |
| 2298 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2299 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2300 | static int getPayload( |
| 2301 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2302 | int offset, /* Begin reading this far into payload */ |
| 2303 | int amt, /* Read this many bytes */ |
| 2304 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 2305 | int skipKey /* offset begins at data if this is true */ |
| 2306 | ){ |
| 2307 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2308 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2309 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2310 | MemPage *pPage; |
| 2311 | Btree *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2312 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2313 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2314 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2315 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2316 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2317 | pBt = pCur->pBt; |
| 2318 | pPage = pCur->pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2319 | pageIntegrity(pPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2320 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2321 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2322 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2323 | aPayload += pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2324 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2325 | nKey = 0; |
| 2326 | }else{ |
| 2327 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2328 | } |
| 2329 | assert( offset>=0 ); |
| 2330 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2331 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2332 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2333 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2334 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2335 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2336 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2337 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2338 | if( a+offset>pCur->info.nLocal ){ |
| 2339 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2340 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2341 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2342 | if( a==amt ){ |
| 2343 | return SQLITE_OK; |
| 2344 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2345 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2346 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2347 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2348 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2349 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2350 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2351 | ovflSize = pBt->usableSize - 4; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2352 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2353 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2354 | while( amt>0 && nextPage ){ |
| 2355 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
| 2356 | if( rc!=0 ){ |
| 2357 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2358 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2359 | nextPage = get4byte(aPayload); |
| 2360 | if( offset<ovflSize ){ |
| 2361 | int a = amt; |
| 2362 | if( a + offset > ovflSize ){ |
| 2363 | a = ovflSize - offset; |
| 2364 | } |
| 2365 | memcpy(pBuf, &aPayload[offset+4], a); |
| 2366 | offset = 0; |
| 2367 | amt -= a; |
| 2368 | pBuf += a; |
| 2369 | }else{ |
| 2370 | offset -= ovflSize; |
| 2371 | } |
| 2372 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2373 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2374 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2375 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2376 | if( amt>0 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2377 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2378 | } |
| 2379 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2382 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2383 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2384 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2385 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2386 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2387 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2388 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2389 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2390 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2391 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2392 | assert( pCur->isValid ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2393 | assert( pCur->pPage!=0 ); |
| 2394 | assert( pCur->pPage->intKey==0 ); |
| 2395 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2396 | return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
| 2397 | } |
| 2398 | |
| 2399 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2400 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2401 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2402 | ** begins at "offset". |
| 2403 | ** |
| 2404 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2405 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2406 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2407 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2408 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 2409 | assert( pCur->isValid ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2410 | assert( pCur->pPage!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2411 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2412 | return getPayload(pCur, offset, amt, pBuf, 1); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2415 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2416 | ** Return a pointer to payload information from the entry that the |
| 2417 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 2418 | ** 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] | 2419 | ** skipKey==1. The number of bytes of available key/data is written |
| 2420 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 2421 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2422 | ** |
| 2423 | ** This routine is an optimization. It is common for the entire key |
| 2424 | ** and data to fit on the local page and for there to be no overflow |
| 2425 | ** pages. When that is so, this routine can be used to access the |
| 2426 | ** key and data without making a copy. If the key and/or data spills |
| 2427 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 2428 | ** the key/data and copy it into a preallocated buffer. |
| 2429 | ** |
| 2430 | ** The pointer returned by this routine looks directly into the cached |
| 2431 | ** page of the database. The data might change or move the next time |
| 2432 | ** any btree routine is called. |
| 2433 | */ |
| 2434 | static const unsigned char *fetchPayload( |
| 2435 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2436 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2437 | int skipKey /* read beginning at data if this is true */ |
| 2438 | ){ |
| 2439 | unsigned char *aPayload; |
| 2440 | MemPage *pPage; |
| 2441 | Btree *pBt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2442 | u32 nKey; |
| 2443 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2444 | |
| 2445 | assert( pCur!=0 && pCur->pPage!=0 ); |
| 2446 | assert( pCur->isValid ); |
| 2447 | pBt = pCur->pBt; |
| 2448 | pPage = pCur->pPage; |
| 2449 | pageIntegrity(pPage); |
| 2450 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2451 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2452 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2453 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2454 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2455 | nKey = 0; |
| 2456 | }else{ |
| 2457 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2458 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2459 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2460 | aPayload += nKey; |
| 2461 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2462 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2463 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2464 | if( nLocal>nKey ){ |
| 2465 | nLocal = nKey; |
| 2466 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2467 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2468 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2469 | return aPayload; |
| 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2474 | ** For the entry that cursor pCur is point to, return as |
| 2475 | ** many bytes of the key or data as are available on the local |
| 2476 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2477 | ** |
| 2478 | ** The pointer returned is ephemeral. The key/data may move |
| 2479 | ** or be destroyed on the next call to any Btree routine. |
| 2480 | ** |
| 2481 | ** These routines is used to get quick access to key and data |
| 2482 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2483 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2484 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
| 2485 | return (const void*)fetchPayload(pCur, pAmt, 0); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2486 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2487 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
| 2488 | return (const void*)fetchPayload(pCur, pAmt, 1); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | |
| 2492 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2493 | ** 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] | 2494 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2495 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2496 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2497 | int rc; |
| 2498 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2499 | MemPage *pOldPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2500 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2501 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2502 | assert( pCur->isValid ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2503 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2504 | if( rc ) return rc; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2505 | pageIntegrity(pNewPage); |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2506 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2507 | pOldPage = pCur->pPage; |
| 2508 | pOldPage->idxShift = 0; |
| 2509 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2510 | pCur->pPage = pNewPage; |
| 2511 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2512 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2513 | if( pNewPage->nCell<1 ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 2514 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2515 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2516 | return SQLITE_OK; |
| 2517 | } |
| 2518 | |
| 2519 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2520 | ** Return true if the page is the virtual root of its table. |
| 2521 | ** |
| 2522 | ** The virtual root page is the root page for most tables. But |
| 2523 | ** for the table rooted on page 1, sometime the real root page |
| 2524 | ** is empty except for the right-pointer. In such cases the |
| 2525 | ** virtual root page is the page that the right-pointer of page |
| 2526 | ** 1 is pointing to. |
| 2527 | */ |
| 2528 | static int isRootPage(MemPage *pPage){ |
| 2529 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2530 | if( pParent==0 ) return 1; |
| 2531 | if( pParent->pgno>1 ) return 0; |
| 2532 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2537 | ** Move the cursor up to the parent page. |
| 2538 | ** |
| 2539 | ** pCur->idx is set to the cell index that contains the pointer |
| 2540 | ** to the page we are coming from. If we are coming from the |
| 2541 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2542 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2543 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2544 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2545 | Pgno oldPgno; |
| 2546 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2547 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2548 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2549 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2550 | assert( pCur->isValid ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2551 | pPage = pCur->pPage; |
| 2552 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2553 | assert( !isRootPage(pPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2554 | pageIntegrity(pPage); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2555 | pParent = pPage->pParent; |
| 2556 | assert( pParent!=0 ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2557 | pageIntegrity(pParent); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2558 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2559 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2560 | oldPgno = pPage->pgno; |
| 2561 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2562 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2563 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2564 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2565 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | ** Move the cursor to the root page |
| 2570 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2571 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2572 | MemPage *pRoot; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2573 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2574 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2575 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2576 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2577 | if( rc ){ |
| 2578 | pCur->isValid = 0; |
| 2579 | return rc; |
| 2580 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2581 | releasePage(pCur->pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2582 | pageIntegrity(pRoot); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2583 | pCur->pPage = pRoot; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2584 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2585 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2586 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 2587 | Pgno subpage; |
| 2588 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2589 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2590 | assert( subpage>0 ); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 2591 | pCur->isValid = 1; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2592 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2593 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2594 | pCur->isValid = pCur->pPage->nCell>0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2595 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2596 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2597 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2598 | /* |
| 2599 | ** Move the cursor down to the left-most leaf entry beneath the |
| 2600 | ** entry to which it is currently pointing. |
| 2601 | */ |
| 2602 | static int moveToLeftmost(BtCursor *pCur){ |
| 2603 | Pgno pgno; |
| 2604 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2605 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2606 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2607 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2608 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2609 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2610 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2611 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2612 | if( rc ) return rc; |
| 2613 | } |
| 2614 | return SQLITE_OK; |
| 2615 | } |
| 2616 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2617 | /* |
| 2618 | ** Move the cursor down to the right-most leaf entry beneath the |
| 2619 | ** page to which it is currently pointing. Notice the difference |
| 2620 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 2621 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 2622 | ** finds the right-most entry beneath the *page*. |
| 2623 | */ |
| 2624 | static int moveToRightmost(BtCursor *pCur){ |
| 2625 | Pgno pgno; |
| 2626 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2627 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2628 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2629 | assert( pCur->isValid ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2630 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2631 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2632 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2633 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2634 | if( rc ) return rc; |
| 2635 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2636 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2637 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2638 | return SQLITE_OK; |
| 2639 | } |
| 2640 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2641 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 2642 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2643 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2644 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2645 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2646 | int rc; |
| 2647 | rc = moveToRoot(pCur); |
| 2648 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2649 | if( pCur->isValid==0 ){ |
| 2650 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2651 | *pRes = 1; |
| 2652 | return SQLITE_OK; |
| 2653 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2654 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2655 | *pRes = 0; |
| 2656 | rc = moveToLeftmost(pCur); |
| 2657 | return rc; |
| 2658 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2659 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2660 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 2661 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 2662 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2663 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2664 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2665 | int rc; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2666 | rc = moveToRoot(pCur); |
| 2667 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2668 | if( pCur->isValid==0 ){ |
| 2669 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2670 | *pRes = 1; |
| 2671 | return SQLITE_OK; |
| 2672 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2673 | assert( pCur->isValid ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2674 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2675 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 2676 | return rc; |
| 2677 | } |
| 2678 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2679 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2680 | ** Return a success code. |
| 2681 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2682 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 2683 | ** ignored. For other tables, nKey is the number of bytes of data |
| 2684 | ** in nKey. The comparison function specified when the cursor was |
| 2685 | ** created is used to compare keys. |
| 2686 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2687 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2688 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2689 | ** were present. The cursor might point to an entry that comes |
| 2690 | ** before or after the key. |
| 2691 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2692 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2693 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2694 | ** this value is as follows: |
| 2695 | ** |
| 2696 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2697 | ** is smaller than pKey or if the table is empty |
| 2698 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2699 | ** |
| 2700 | ** *pRes==0 The cursor is left pointing at an entry that |
| 2701 | ** exactly matches pKey. |
| 2702 | ** |
| 2703 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2704 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2705 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2706 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2707 | int rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2708 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2709 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2710 | assert( pCur->pPage ); |
| 2711 | assert( pCur->pPage->isInit ); |
| 2712 | if( pCur->isValid==0 ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 2713 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2714 | assert( pCur->pPage->nCell==0 ); |
| 2715 | return SQLITE_OK; |
| 2716 | } |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 2717 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2718 | int lwr, upr; |
| 2719 | Pgno chldPg; |
| 2720 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 2721 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2722 | lwr = 0; |
| 2723 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 2724 | if( !pPage->intKey && pKey==0 ){ |
| 2725 | return SQLITE_CORRUPT; |
| 2726 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2727 | pageIntegrity(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2728 | while( lwr<=upr ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2729 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2730 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2731 | pCur->idx = (lwr+upr)/2; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2732 | pCur->info.nSize = 0; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2733 | sqlite3BtreeKeySize(pCur, &nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2734 | if( pPage->intKey ){ |
| 2735 | if( nCellKey<nKey ){ |
| 2736 | c = -1; |
| 2737 | }else if( nCellKey>nKey ){ |
| 2738 | c = +1; |
| 2739 | }else{ |
| 2740 | c = 0; |
| 2741 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2742 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2743 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2744 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2745 | if( available>=nCellKey ){ |
| 2746 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2747 | }else{ |
| 2748 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 2749 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2750 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2751 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 2752 | sqliteFree(pCellKey); |
| 2753 | if( rc ) return rc; |
| 2754 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2755 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2756 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2757 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 2758 | lwr = pCur->idx; |
| 2759 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2762 | if( pRes ) *pRes = 0; |
| 2763 | return SQLITE_OK; |
| 2764 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2765 | } |
| 2766 | if( c<0 ){ |
| 2767 | lwr = pCur->idx+1; |
| 2768 | }else{ |
| 2769 | upr = pCur->idx-1; |
| 2770 | } |
| 2771 | } |
| 2772 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2773 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2774 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2775 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2776 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2777 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2778 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2779 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2780 | } |
| 2781 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2782 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2783 | if( pRes ) *pRes = c; |
| 2784 | return SQLITE_OK; |
| 2785 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2786 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2787 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2788 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2789 | if( rc ){ |
| 2790 | return rc; |
| 2791 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2792 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2793 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2797 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 2798 | ** |
| 2799 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 2800 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 2801 | ** the first entry. TRUE is also returned if the table is empty. |
| 2802 | */ |
| 2803 | int sqlite3BtreeEof(BtCursor *pCur){ |
| 2804 | return pCur->isValid==0; |
| 2805 | } |
| 2806 | |
| 2807 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2808 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2809 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2810 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2811 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2812 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2813 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2814 | int rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2815 | MemPage *pPage = pCur->pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2816 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2817 | assert( pRes!=0 ); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2818 | if( pCur->isValid==0 ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2819 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2820 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2821 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2822 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2823 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 2824 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2825 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2826 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2827 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2828 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2829 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2830 | if( rc ) return rc; |
| 2831 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2832 | *pRes = 0; |
| 2833 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2834 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2835 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2836 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2837 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2838 | pCur->isValid = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2839 | return SQLITE_OK; |
| 2840 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2841 | moveToParent(pCur); |
| 2842 | pPage = pCur->pPage; |
| 2843 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2844 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2845 | if( pPage->leafData ){ |
| 2846 | rc = sqlite3BtreeNext(pCur, pRes); |
| 2847 | }else{ |
| 2848 | rc = SQLITE_OK; |
| 2849 | } |
| 2850 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2851 | } |
| 2852 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2853 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2854 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2855 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2856 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2857 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2860 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2861 | ** 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] | 2862 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2863 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2864 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2865 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2866 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2867 | int rc; |
| 2868 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2869 | MemPage *pPage; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2870 | if( pCur->isValid==0 ){ |
| 2871 | *pRes = 1; |
| 2872 | return SQLITE_OK; |
| 2873 | } |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 2874 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2875 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2876 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2877 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2878 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2879 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2880 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2881 | if( rc ) return rc; |
| 2882 | rc = moveToRightmost(pCur); |
| 2883 | }else{ |
| 2884 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2885 | if( isRootPage(pPage) ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2886 | pCur->isValid = 0; |
| 2887 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2888 | return SQLITE_OK; |
| 2889 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2890 | moveToParent(pCur); |
| 2891 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2892 | } |
| 2893 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2894 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 2895 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 2896 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 2897 | }else{ |
| 2898 | rc = SQLITE_OK; |
| 2899 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2900 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2901 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 2902 | return rc; |
| 2903 | } |
| 2904 | |
| 2905 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2906 | ** Allocate a new page from the database file. |
| 2907 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2908 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2909 | ** has already been called on the new page.) The new page has also |
| 2910 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2911 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2912 | ** |
| 2913 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 2914 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2915 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2916 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 2917 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 2918 | ** 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] | 2919 | ** attempt to keep related pages close to each other in the database file, |
| 2920 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2921 | ** |
| 2922 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 2923 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 2924 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2925 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2926 | static int allocatePage( |
| 2927 | Btree *pBt, |
| 2928 | MemPage **ppPage, |
| 2929 | Pgno *pPgno, |
| 2930 | Pgno nearby, |
| 2931 | u8 exact |
| 2932 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2933 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2934 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2935 | int n; /* Number of pages on the freelist */ |
| 2936 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2937 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2938 | pPage1 = pBt->pPage1; |
| 2939 | n = get4byte(&pPage1->aData[36]); |
| 2940 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 2941 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2942 | MemPage *pTrunk = 0; |
| 2943 | Pgno iTrunk; |
| 2944 | MemPage *pPrevTrunk = 0; |
| 2945 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 2946 | |
| 2947 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 2948 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 2949 | ** the entire-list will be searched for that page. |
| 2950 | */ |
| 2951 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2952 | if( exact ){ |
| 2953 | u8 eType; |
| 2954 | assert( nearby>0 ); |
| 2955 | assert( pBt->autoVacuum ); |
| 2956 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 2957 | if( rc ) return rc; |
| 2958 | if( eType==PTRMAP_FREEPAGE ){ |
| 2959 | searchList = 1; |
| 2960 | } |
| 2961 | *pPgno = nearby; |
| 2962 | } |
| 2963 | #endif |
| 2964 | |
| 2965 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 2966 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 2967 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2968 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2969 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2970 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2971 | |
| 2972 | /* The code within this loop is run only once if the 'searchList' variable |
| 2973 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 2974 | ** free-list until the page 'nearby' is located. |
| 2975 | */ |
| 2976 | do { |
| 2977 | pPrevTrunk = pTrunk; |
| 2978 | if( pPrevTrunk ){ |
| 2979 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2980 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2981 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2982 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2983 | rc = getPage(pBt, iTrunk, &pTrunk); |
| 2984 | if( rc ){ |
| 2985 | releasePage(pPrevTrunk); |
| 2986 | return rc; |
| 2987 | } |
| 2988 | |
| 2989 | /* TODO: This should move to after the loop? */ |
| 2990 | rc = sqlite3pager_write(pTrunk->aData); |
| 2991 | if( rc ){ |
| 2992 | releasePage(pTrunk); |
| 2993 | releasePage(pPrevTrunk); |
| 2994 | return rc; |
| 2995 | } |
| 2996 | |
| 2997 | k = get4byte(&pTrunk->aData[4]); |
| 2998 | if( k==0 && !searchList ){ |
| 2999 | /* The trunk has no leaves and the list is not being searched. |
| 3000 | ** So extract the trunk page itself and use it as the newly |
| 3001 | ** allocated page */ |
| 3002 | assert( pPrevTrunk==0 ); |
| 3003 | *pPgno = iTrunk; |
| 3004 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3005 | *ppPage = pTrunk; |
| 3006 | pTrunk = 0; |
| 3007 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3008 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3009 | /* Value of k is out of range. Database corruption */ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3010 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3011 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3012 | }else if( searchList && nearby==iTrunk ){ |
| 3013 | /* The list is being searched and this trunk page is the page |
| 3014 | ** to allocate, regardless of whether it has leaves. |
| 3015 | */ |
| 3016 | assert( *pPgno==iTrunk ); |
| 3017 | *ppPage = pTrunk; |
| 3018 | searchList = 0; |
| 3019 | if( k==0 ){ |
| 3020 | if( !pPrevTrunk ){ |
| 3021 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3022 | }else{ |
| 3023 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3024 | } |
| 3025 | }else{ |
| 3026 | /* The trunk page is required by the caller but it contains |
| 3027 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3028 | ** page in this case. |
| 3029 | */ |
| 3030 | MemPage *pNewTrunk; |
| 3031 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
| 3032 | rc = getPage(pBt, iNewTrunk, &pNewTrunk); |
| 3033 | if( rc!=SQLITE_OK ){ |
| 3034 | releasePage(pTrunk); |
| 3035 | releasePage(pPrevTrunk); |
| 3036 | return rc; |
| 3037 | } |
| 3038 | rc = sqlite3pager_write(pNewTrunk->aData); |
| 3039 | if( rc!=SQLITE_OK ){ |
| 3040 | releasePage(pNewTrunk); |
| 3041 | releasePage(pTrunk); |
| 3042 | releasePage(pPrevTrunk); |
| 3043 | return rc; |
| 3044 | } |
| 3045 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3046 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3047 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
| 3048 | if( !pPrevTrunk ){ |
| 3049 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3050 | }else{ |
| 3051 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3052 | } |
| 3053 | releasePage(pNewTrunk); |
| 3054 | } |
| 3055 | pTrunk = 0; |
| 3056 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3057 | #endif |
| 3058 | }else{ |
| 3059 | /* Extract a leaf from the trunk */ |
| 3060 | int closest; |
| 3061 | Pgno iPage; |
| 3062 | unsigned char *aData = pTrunk->aData; |
| 3063 | if( nearby>0 ){ |
| 3064 | int i, dist; |
| 3065 | closest = 0; |
| 3066 | dist = get4byte(&aData[8]) - nearby; |
| 3067 | if( dist<0 ) dist = -dist; |
| 3068 | for(i=1; i<k; i++){ |
| 3069 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3070 | if( d2<0 ) d2 = -d2; |
| 3071 | if( d2<dist ){ |
| 3072 | closest = i; |
| 3073 | dist = d2; |
| 3074 | } |
| 3075 | } |
| 3076 | }else{ |
| 3077 | closest = 0; |
| 3078 | } |
| 3079 | |
| 3080 | iPage = get4byte(&aData[8+closest*4]); |
| 3081 | if( !searchList || iPage==nearby ){ |
| 3082 | *pPgno = iPage; |
| 3083 | if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 3084 | /* Free page off the end of the file */ |
| 3085 | return SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 3086 | } |
| 3087 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3088 | ": %d more free pages\n", |
| 3089 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3090 | if( closest<k-1 ){ |
| 3091 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3092 | } |
| 3093 | put4byte(&aData[4], k-1); |
| 3094 | rc = getPage(pBt, *pPgno, ppPage); |
| 3095 | if( rc==SQLITE_OK ){ |
| 3096 | sqlite3pager_dont_rollback((*ppPage)->aData); |
| 3097 | rc = sqlite3pager_write((*ppPage)->aData); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3098 | if( rc!=SQLITE_OK ){ |
| 3099 | releasePage(*ppPage); |
| 3100 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3101 | } |
| 3102 | searchList = 0; |
| 3103 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3104 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3105 | releasePage(pPrevTrunk); |
| 3106 | }while( searchList ); |
| 3107 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3108 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3109 | /* There are no pages on the freelist, so create a new page at the |
| 3110 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3111 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3112 | |
| 3113 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 3114 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt->usableSize, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3115 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3116 | ** at the end of the file instead of one. The first allocated page |
| 3117 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3118 | */ |
| 3119 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3120 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3121 | (*pPgno)++; |
| 3122 | } |
| 3123 | #endif |
| 3124 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3125 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3126 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3127 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3128 | rc = sqlite3pager_write((*ppPage)->aData); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3129 | if( rc!=SQLITE_OK ){ |
| 3130 | releasePage(*ppPage); |
| 3131 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3132 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3133 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3134 | |
| 3135 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3136 | return rc; |
| 3137 | } |
| 3138 | |
| 3139 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3140 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3141 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3142 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3143 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3144 | static int freePage(MemPage *pPage){ |
| 3145 | Btree *pBt = pPage->pBt; |
| 3146 | MemPage *pPage1 = pBt->pPage1; |
| 3147 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3148 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3149 | /* Prepare the page for freeing */ |
| 3150 | assert( pPage->pgno>1 ); |
| 3151 | pPage->isInit = 0; |
| 3152 | releasePage(pPage->pParent); |
| 3153 | pPage->pParent = 0; |
| 3154 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3155 | /* Increment the free page count on pPage1 */ |
| 3156 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3157 | if( rc ) return rc; |
| 3158 | n = get4byte(&pPage1->aData[36]); |
| 3159 | put4byte(&pPage1->aData[36], n+1); |
| 3160 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3161 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3162 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3163 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3164 | */ |
| 3165 | if( pBt->autoVacuum ){ |
| 3166 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3167 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3168 | } |
| 3169 | #endif |
| 3170 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3171 | if( n==0 ){ |
| 3172 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3173 | rc = sqlite3pager_write(pPage->aData); |
| 3174 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3175 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3176 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3177 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3178 | }else{ |
| 3179 | /* Other free pages already exist. Retrive the first trunk page |
| 3180 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3181 | MemPage *pTrunk; |
| 3182 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3183 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3184 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3185 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3186 | /* The trunk is full. Turn the page being freed into a new |
| 3187 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3188 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3189 | if( rc ) return rc; |
| 3190 | put4byte(pPage->aData, pTrunk->pgno); |
| 3191 | put4byte(&pPage->aData[4], 0); |
| 3192 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3193 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3194 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3195 | }else{ |
| 3196 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3197 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3198 | if( rc ) return rc; |
| 3199 | put4byte(&pTrunk->aData[4], k+1); |
| 3200 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3201 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3202 | 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] | 3203 | } |
| 3204 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3205 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3206 | return rc; |
| 3207 | } |
| 3208 | |
| 3209 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3210 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3211 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3212 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
| 3213 | Btree *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3214 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3215 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3216 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3217 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3218 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3219 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3220 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3221 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3222 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3223 | while( ovflPgno!=0 ){ |
| 3224 | MemPage *pOvfl; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame^] | 3225 | if( ovflPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 3226 | return SQLITE_CORRUPT; |
| 3227 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3228 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3229 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3230 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3231 | rc = freePage(pOvfl); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3232 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3233 | sqlite3pager_unref(pOvfl->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3234 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3235 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3239 | ** Create the byte sequence used to represent a cell on page pPage |
| 3240 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3241 | ** allocated and filled in as necessary. The calling procedure |
| 3242 | ** is responsible for making sure sufficient space has been allocated |
| 3243 | ** for pCell[]. |
| 3244 | ** |
| 3245 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3246 | ** area. pCell might point to some temporary storage. The cell will |
| 3247 | ** be constructed in this temporary area then copied into pPage->aData |
| 3248 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3249 | */ |
| 3250 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3251 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3252 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3253 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3254 | const void *pData,int nData, /* The data */ |
| 3255 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3256 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3257 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3258 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3259 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3260 | int spaceLeft; |
| 3261 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3262 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3263 | unsigned char *pPrior; |
| 3264 | unsigned char *pPayload; |
| 3265 | Btree *pBt = pPage->pBt; |
| 3266 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3267 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3268 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3269 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3270 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3271 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3272 | if( !pPage->leaf ){ |
| 3273 | nHeader += 4; |
| 3274 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3275 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3276 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3277 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3278 | nData = 0; |
| 3279 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3280 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3281 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3282 | assert( info.nHeader==nHeader ); |
| 3283 | assert( info.nKey==nKey ); |
| 3284 | assert( info.nData==nData ); |
| 3285 | |
| 3286 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3287 | nPayload = nData; |
| 3288 | if( pPage->intKey ){ |
| 3289 | pSrc = pData; |
| 3290 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3291 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3292 | }else{ |
| 3293 | nPayload += nKey; |
| 3294 | pSrc = pKey; |
| 3295 | nSrc = nKey; |
| 3296 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3297 | *pnSize = info.nSize; |
| 3298 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3299 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3300 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3301 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3302 | while( nPayload>0 ){ |
| 3303 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3304 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3305 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 3306 | #endif |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3307 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3308 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3309 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3310 | ** overflow page is being allocated, add an entry to the pointer-map |
| 3311 | ** for that page now. The entry for the first overflow page will be |
| 3312 | ** added later, by the insertCell() routine. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3313 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3314 | if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ |
| 3315 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3316 | } |
| 3317 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3318 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3319 | releasePage(pToRelease); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3320 | /* clearCell(pPage, pCell); */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3321 | return rc; |
| 3322 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3323 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3324 | releasePage(pToRelease); |
| 3325 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3326 | pPrior = pOvfl->aData; |
| 3327 | put4byte(pPrior, 0); |
| 3328 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3329 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3330 | } |
| 3331 | n = nPayload; |
| 3332 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3333 | if( n>nSrc ) n = nSrc; |
| 3334 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3335 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3336 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3337 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3338 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3339 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3340 | if( nSrc==0 ){ |
| 3341 | nSrc = nData; |
| 3342 | pSrc = pData; |
| 3343 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3344 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3345 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3346 | return SQLITE_OK; |
| 3347 | } |
| 3348 | |
| 3349 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3350 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3351 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3352 | ** pointer in the third argument. |
| 3353 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3354 | static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3355 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3356 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3357 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3358 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3359 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3360 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3361 | if( aData ){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3362 | pThis = (MemPage*)&aData[pBt->psAligned]; |
drh | 3127653 | 2004-09-27 12:20:52 +0000 | [diff] [blame] | 3363 | assert( pThis->aData==aData ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3364 | if( pThis->isInit ){ |
| 3365 | if( pThis->pParent!=pNewParent ){ |
| 3366 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 3367 | pThis->pParent = pNewParent; |
| 3368 | if( pNewParent ) sqlite3pager_ref(pNewParent->aData); |
| 3369 | } |
| 3370 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3371 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3372 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3373 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3374 | |
| 3375 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3376 | if( pBt->autoVacuum ){ |
| 3377 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 3378 | } |
| 3379 | #endif |
| 3380 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3383 | |
| 3384 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3385 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3386 | ** Change the pParent pointer of all children of pPage to point back |
| 3387 | ** to pPage. |
| 3388 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3389 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3390 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3391 | ** |
| 3392 | ** This routine gets called after you memcpy() one page into |
| 3393 | ** another. |
| 3394 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3395 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3396 | int i; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3397 | Btree *pBt = pPage->pBt; |
| 3398 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3399 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3400 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3401 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3402 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3403 | u8 *pCell = findCell(pPage, i); |
| 3404 | if( !pPage->leaf ){ |
| 3405 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 3406 | if( rc!=SQLITE_OK ) return rc; |
| 3407 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3408 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3409 | if( !pPage->leaf ){ |
| 3410 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 3411 | pPage, i); |
| 3412 | pPage->idxShift = 0; |
| 3413 | } |
| 3414 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* |
| 3418 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 3419 | ** The cell content is not freed or deallocated. It is assumed that |
| 3420 | ** the cell content has been copied someplace else. This routine just |
| 3421 | ** removes the reference to the cell from pPage. |
| 3422 | ** |
| 3423 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3424 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3425 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3426 | int i; /* Loop counter */ |
| 3427 | int pc; /* Offset to cell content of cell being deleted */ |
| 3428 | u8 *data; /* pPage->aData */ |
| 3429 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 3430 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3431 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3432 | assert( sz==cellSize(pPage, idx) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3433 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3434 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3435 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 3436 | pc = get2byte(ptr); |
| 3437 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3438 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3439 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 3440 | ptr[0] = ptr[2]; |
| 3441 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3442 | } |
| 3443 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3444 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 3445 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3446 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /* |
| 3450 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 3451 | ** content of the cell. |
| 3452 | ** |
| 3453 | ** 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] | 3454 | ** will not fit, then make a copy of the cell content into pTemp if |
| 3455 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 3456 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 3457 | ** in pTemp or the original pCell) and also record its index. |
| 3458 | ** Allocating a new entry in pPage->aCell[] implies that |
| 3459 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 3460 | ** |
| 3461 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 3462 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 3463 | ** nSkip is non-zero, then pCell may not point to an invalid memory location |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 3464 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3465 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3466 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3467 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3468 | int i, /* New cell becomes the i-th cell of the page */ |
| 3469 | u8 *pCell, /* Content of the new cell */ |
| 3470 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 3471 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 3472 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3473 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3474 | int idx; /* Where to write new cell content in data[] */ |
| 3475 | int j; /* Loop counter */ |
| 3476 | int top; /* First byte of content for any cell in data[] */ |
| 3477 | int end; /* First byte past the last cell pointer in data[] */ |
| 3478 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 3479 | int hdr; /* Offset into data[] of the page header */ |
| 3480 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 3481 | u8 *data; /* The content of the whole page */ |
| 3482 | u8 *ptr; /* Used for moving information around in data[] */ |
| 3483 | |
| 3484 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 3485 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3486 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3487 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3488 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 3489 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3490 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 3491 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3492 | j = pPage->nOverflow++; |
| 3493 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 3494 | pPage->aOvfl[j].pCell = pCell; |
| 3495 | pPage->aOvfl[j].idx = i; |
| 3496 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3497 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3498 | data = pPage->aData; |
| 3499 | hdr = pPage->hdrOffset; |
| 3500 | top = get2byte(&data[hdr+5]); |
| 3501 | cellOffset = pPage->cellOffset; |
| 3502 | end = cellOffset + 2*pPage->nCell + 2; |
| 3503 | ins = cellOffset + 2*i; |
| 3504 | if( end > top - sz ){ |
| 3505 | defragmentPage(pPage); |
| 3506 | top = get2byte(&data[hdr+5]); |
| 3507 | assert( end + sz <= top ); |
| 3508 | } |
| 3509 | idx = allocateSpace(pPage, sz); |
| 3510 | assert( idx>0 ); |
| 3511 | assert( end <= get2byte(&data[hdr+5]) ); |
| 3512 | pPage->nCell++; |
| 3513 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 3514 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3515 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 3516 | ptr[0] = ptr[-2]; |
| 3517 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3518 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3519 | put2byte(&data[ins], idx); |
| 3520 | put2byte(&data[hdr+3], pPage->nCell); |
| 3521 | pPage->idxShift = 1; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3522 | pageIntegrity(pPage); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3523 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3524 | if( pPage->pBt->autoVacuum ){ |
| 3525 | /* The cell may contain a pointer to an overflow page. If so, write |
| 3526 | ** the entry for the overflow page into the pointer map. |
| 3527 | */ |
| 3528 | CellInfo info; |
| 3529 | parseCellPtr(pPage, pCell, &info); |
| 3530 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 3531 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 3532 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 3533 | if( rc!=SQLITE_OK ) return rc; |
| 3534 | } |
| 3535 | } |
| 3536 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3537 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3538 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 3539 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3543 | ** Add a list of cells to a page. The page should be initially empty. |
| 3544 | ** The cells are guaranteed to fit on the page. |
| 3545 | */ |
| 3546 | static void assemblePage( |
| 3547 | MemPage *pPage, /* The page to be assemblied */ |
| 3548 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3549 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3550 | int *aSize /* Sizes of the cells */ |
| 3551 | ){ |
| 3552 | int i; /* Loop counter */ |
| 3553 | int totalSize; /* Total size of all cells */ |
| 3554 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3555 | int cellptr; /* Address of next cell pointer */ |
| 3556 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3557 | u8 *data; /* Data for the page */ |
| 3558 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3559 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3560 | totalSize = 0; |
| 3561 | for(i=0; i<nCell; i++){ |
| 3562 | totalSize += aSize[i]; |
| 3563 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3564 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3565 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3566 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3567 | data = pPage->aData; |
| 3568 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3569 | put2byte(&data[hdr+3], nCell); |
| 3570 | cellbody = allocateSpace(pPage, totalSize); |
| 3571 | assert( cellbody>0 ); |
| 3572 | assert( pPage->nFree >= 2*nCell ); |
| 3573 | pPage->nFree -= 2*nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3574 | for(i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3575 | put2byte(&data[cellptr], cellbody); |
| 3576 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 3577 | cellptr += 2; |
| 3578 | cellbody += aSize[i]; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3579 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3580 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3581 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3582 | } |
| 3583 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3584 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3585 | ** The following parameters determine how many adjacent pages get involved |
| 3586 | ** in a balancing operation. NN is the number of neighbors on either side |
| 3587 | ** of the page that participate in the balancing operation. NB is the |
| 3588 | ** total number of pages that participate, including the target page and |
| 3589 | ** NN neighbors on either side. |
| 3590 | ** |
| 3591 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 3592 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 3593 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 3594 | ** The value of NN appears to give the best results overall. |
| 3595 | */ |
| 3596 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 3597 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 3598 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3599 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3600 | static int balance(MemPage*, int); |
| 3601 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 3602 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 3603 | /* |
| 3604 | ** This version of balance() handles the common special case where |
| 3605 | ** a new entry is being inserted on the extreme right-end of the |
| 3606 | ** tree, in other words, when the new entry will become the largest |
| 3607 | ** entry in the tree. |
| 3608 | ** |
| 3609 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 3610 | ** a new page to the right-hand side and put the one new entry in |
| 3611 | ** that page. This leaves the right side of the tree somewhat |
| 3612 | ** unbalanced. But odds are that we will be inserting new entries |
| 3613 | ** at the end soon afterwards so the nearly empty page will quickly |
| 3614 | ** fill up. On average. |
| 3615 | ** |
| 3616 | ** pPage is the leaf page which is the right-most page in the tree. |
| 3617 | ** pParent is its parent. pPage must have a single overflow entry |
| 3618 | ** which is also the right-most entry on the page. |
| 3619 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3620 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 3621 | int rc; |
| 3622 | MemPage *pNew; |
| 3623 | Pgno pgnoNew; |
| 3624 | u8 *pCell; |
| 3625 | int szCell; |
| 3626 | CellInfo info; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3627 | Btree *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3628 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 3629 | int parentSize; /* Size of new divider cell */ |
| 3630 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* Allocate a new page. Insert the overflow cell from pPage |
| 3633 | ** into it. Then remove the overflow cell from pPage. |
| 3634 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3635 | rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3636 | if( rc!=SQLITE_OK ){ |
| 3637 | return rc; |
| 3638 | } |
| 3639 | pCell = pPage->aOvfl[0].pCell; |
| 3640 | szCell = cellSizePtr(pPage, pCell); |
| 3641 | zeroPage(pNew, pPage->aData[0]); |
| 3642 | assemblePage(pNew, 1, &pCell, &szCell); |
| 3643 | pPage->nOverflow = 0; |
| 3644 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3645 | /* Set the parent of the newly allocated page to pParent. */ |
| 3646 | pNew->pParent = pParent; |
| 3647 | sqlite3pager_ref(pParent->aData); |
| 3648 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3649 | /* pPage is currently the right-child of pParent. Change this |
| 3650 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3651 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3652 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3653 | assert( pPage->nCell>0 ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3654 | parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info); |
| 3655 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize); |
| 3656 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3657 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3658 | } |
| 3659 | assert( parentSize<64 ); |
| 3660 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 3661 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3662 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3663 | } |
| 3664 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 3665 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 3666 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3667 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3668 | /* If this is an auto-vacuum database, update the pointer map |
| 3669 | ** with entries for the new page, and any pointer from the |
| 3670 | ** cell on the page to an overflow page. |
| 3671 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3672 | if( pBt->autoVacuum ){ |
| 3673 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 3674 | if( rc!=SQLITE_OK ){ |
| 3675 | return rc; |
| 3676 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3677 | rc = ptrmapPutOvfl(pNew, 0); |
| 3678 | if( rc!=SQLITE_OK ){ |
| 3679 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3680 | } |
| 3681 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3682 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3683 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 3684 | /* Release the reference to the new page and balance the parent page, |
| 3685 | ** in case the divider cell inserted caused it to become overfull. |
| 3686 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3687 | releasePage(pNew); |
| 3688 | return balance(pParent, 0); |
| 3689 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 3690 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3691 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3692 | /* |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3693 | ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine |
| 3694 | ** if the database supports auto-vacuum or not. Because it is used |
| 3695 | ** within an expression that is an argument to another macro |
| 3696 | ** (sqliteMallocRaw), it is not possible to use conditional compilation. |
| 3697 | ** So, this macro is defined instead. |
| 3698 | */ |
| 3699 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3700 | #define ISAUTOVACUUM (pBt->autoVacuum) |
| 3701 | #else |
| 3702 | #define ISAUTOVACUUM 0 |
| 3703 | #endif |
| 3704 | |
| 3705 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3706 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3707 | ** 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] | 3708 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 3709 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3710 | ** 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] | 3711 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3712 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3713 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 3714 | ** The number of siblings of pPage might be increased or decreased by one or |
| 3715 | ** 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] | 3716 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3717 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3718 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3719 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3720 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3721 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3722 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3723 | ** 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] | 3724 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3725 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3726 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 3727 | ** might become overfull or underfull. If that happens, then this routine |
| 3728 | ** is called recursively on the parent. |
| 3729 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3730 | ** If this routine fails for any reason, it might leave the database |
| 3731 | ** in a corrupted state. So if this routine fails, the database should |
| 3732 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3733 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3734 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3735 | MemPage *pParent; /* The parent of pPage */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3736 | Btree *pBt; /* The whole database */ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3737 | int nCell = 0; /* Number of cells in aCell[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3738 | int nOld; /* Number of pages in apOld[] */ |
| 3739 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3740 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3741 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3742 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 3743 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3744 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3745 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3746 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3747 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 3748 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3749 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3750 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3751 | int mxCellPerPage; /* Maximum number of cells in one page */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3752 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 3753 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3754 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3755 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 3756 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3757 | int idxDiv[NB]; /* Indices of divider cells in pParent */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3758 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 3759 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 3760 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3761 | u8 **apCell; /* All cells begin balanced */ |
| 3762 | int *szCell; /* Local size of all cells in apCell[] */ |
| 3763 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 3764 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 3765 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3766 | u8 *aFrom = 0; |
| 3767 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3768 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3769 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3770 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3771 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3772 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3773 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3774 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3775 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3776 | sqlite3pager_write(pParent->aData); |
| 3777 | assert( pParent ); |
| 3778 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3779 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 3780 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 3781 | /* |
| 3782 | ** A special case: If a new entry has just been inserted into a |
| 3783 | ** table (that is, a btree with integer keys and all data at the leaves) |
| 3784 | ** an the new entry is the right-most entry in the tree (it has the |
| 3785 | ** largest key) then use the special balance_quick() routine for |
| 3786 | ** balancing. balance_quick() is much faster and results in a tighter |
| 3787 | ** packing of data in the common case. |
| 3788 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3789 | if( pPage->leaf && |
| 3790 | pPage->intKey && |
| 3791 | pPage->leafData && |
| 3792 | pPage->nOverflow==1 && |
| 3793 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3794 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3795 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 3796 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3797 | /* |
| 3798 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 3799 | ** they are not full and no new page is required. |
| 3800 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 3801 | return balance_quick(pPage, pParent); |
| 3802 | } |
| 3803 | #endif |
| 3804 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3805 | /* |
| 3806 | ** Allocate space for memory structures |
| 3807 | */ |
| 3808 | mxCellPerPage = MX_CELL(pBt); |
| 3809 | apCell = sqliteMallocRaw( |
| 3810 | (mxCellPerPage+2)*NB*(sizeof(u8*)+sizeof(int)) |
| 3811 | + sizeof(MemPage)*NB |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3812 | + pBt->psAligned*(5+NB) |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3813 | + (ISAUTOVACUUM ? (mxCellPerPage+2)*NN*2 : 0) |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3814 | ); |
| 3815 | if( apCell==0 ){ |
| 3816 | return SQLITE_NOMEM; |
| 3817 | } |
| 3818 | szCell = (int*)&apCell[(mxCellPerPage+2)*NB]; |
| 3819 | aCopy[0] = (u8*)&szCell[(mxCellPerPage+2)*NB]; |
| 3820 | for(i=1; i<NB; i++){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3821 | aCopy[i] = &aCopy[i-1][pBt->psAligned+sizeof(MemPage)]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 3822 | } |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3823 | aSpace = &aCopy[NB-1][pBt->psAligned+sizeof(MemPage)]; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3824 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3825 | if( pBt->autoVacuum ){ |
| 3826 | aFrom = &aSpace[5*pBt->psAligned]; |
| 3827 | } |
| 3828 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3829 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3830 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3831 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3832 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 3833 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3834 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3835 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3836 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3837 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3838 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3839 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3840 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3841 | break; |
| 3842 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3843 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3844 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3845 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 3846 | }else{ |
| 3847 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3848 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3849 | |
| 3850 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3851 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 3852 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3853 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3854 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3855 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3856 | |
| 3857 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3858 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3859 | ** the siblings. An attempt is made to find NN siblings on either |
| 3860 | ** side of pPage. More siblings are taken from one side, however, if |
| 3861 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 3862 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3863 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3864 | nxDiv = idx - NN; |
| 3865 | if( nxDiv + NB > pParent->nCell ){ |
| 3866 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3867 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3868 | if( nxDiv<0 ){ |
| 3869 | nxDiv = 0; |
| 3870 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3871 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 3872 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3873 | if( k<pParent->nCell ){ |
| 3874 | idxDiv[i] = k; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3875 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3876 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3877 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3878 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3879 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3880 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3881 | }else{ |
| 3882 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3883 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3884 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3885 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3886 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3887 | apCopy[i] = 0; |
| 3888 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3889 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3893 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 3894 | ** The rest of this function will use data from the copies rather |
| 3895 | ** that the original pages since the original pages will be in the |
| 3896 | ** process of being overwritten. |
| 3897 | */ |
| 3898 | for(i=0; i<nOld; i++){ |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3899 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned]; |
| 3900 | p->aData = &((u8*)p)[-pBt->psAligned]; |
| 3901 | memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage)); |
| 3902 | p->aData = &((u8*)p)[-pBt->psAligned]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | /* |
| 3906 | ** Load pointers to all cells on sibling pages and the divider cells |
| 3907 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3908 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 3909 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3910 | ** |
| 3911 | ** If the siblings are on leaf pages, then the child pointers of the |
| 3912 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3913 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3914 | ** child pointers. If siblings are not leaves, then all cell in |
| 3915 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 3916 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3917 | ** |
| 3918 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 3919 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3920 | */ |
| 3921 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3922 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3923 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3924 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3925 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3926 | int limit = pOld->nCell+pOld->nOverflow; |
| 3927 | for(j=0; j<limit; j++){ |
| 3928 | apCell[nCell] = findOverflowCell(pOld, j); |
| 3929 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3930 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3931 | if( pBt->autoVacuum ){ |
| 3932 | int a; |
| 3933 | aFrom[nCell] = i; |
| 3934 | for(a=0; a<pOld->nOverflow; a++){ |
| 3935 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 3936 | aFrom[nCell] = 0xFF; |
| 3937 | break; |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3942 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3943 | } |
| 3944 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3945 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3946 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3947 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 3948 | ** are duplicates of keys on the child pages. We need to remove |
| 3949 | ** the divider cells from pParent, but the dividers cells are not |
| 3950 | ** added to apCell[] because they are duplicates of child cells. |
| 3951 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3952 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3953 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3954 | u8 *pTemp; |
| 3955 | szCell[nCell] = sz; |
| 3956 | pTemp = &aSpace[iSpace]; |
| 3957 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 3958 | assert( iSpace<=pBt->psAligned*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3959 | memcpy(pTemp, apDiv[i], sz); |
| 3960 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3961 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3962 | if( pBt->autoVacuum ){ |
| 3963 | aFrom[nCell] = 0xFF; |
| 3964 | } |
| 3965 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3966 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3967 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3968 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3969 | if( !pOld->leaf ){ |
| 3970 | assert( leafCorrection==0 ); |
| 3971 | /* The right pointer of the child page pOld becomes the left |
| 3972 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3973 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3974 | }else{ |
| 3975 | assert( leafCorrection==4 ); |
| 3976 | } |
| 3977 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3978 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3983 | ** Figure out the number of pages needed to hold all nCell cells. |
| 3984 | ** Store this number in "k". Also compute szNew[] which is the total |
| 3985 | ** 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] | 3986 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3987 | ** cntNew[k] should equal nCell. |
| 3988 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 3989 | ** Values computed by this block: |
| 3990 | ** |
| 3991 | ** k: The total number of sibling pages |
| 3992 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 3993 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 3994 | ** the right of the i-th sibling page. |
| 3995 | ** usableSpace: Number of bytes of space available on each sibling. |
| 3996 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3997 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3998 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3999 | for(subtotal=k=i=0; i<nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4000 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4001 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4002 | szNew[k] = subtotal - szCell[i]; |
| 4003 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4004 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4005 | subtotal = 0; |
| 4006 | k++; |
| 4007 | } |
| 4008 | } |
| 4009 | szNew[k] = subtotal; |
| 4010 | cntNew[k] = nCell; |
| 4011 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4012 | |
| 4013 | /* |
| 4014 | ** The packing computed by the previous block is biased toward the siblings |
| 4015 | ** on the left side. The left siblings are always nearly full, while the |
| 4016 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4017 | ** to adjust the packing of siblings to get a better balance. |
| 4018 | ** |
| 4019 | ** This adjustment is more than an optimization. The packing above might |
| 4020 | ** be so out of balance as to be illegal. For example, the right-most |
| 4021 | ** sibling might be completely empty. This adjustment is not optional. |
| 4022 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4023 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4024 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4025 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4026 | int r; /* Index of right-most cell in left sibling */ |
| 4027 | int d; /* Index of first cell to the left of right sibling */ |
| 4028 | |
| 4029 | r = cntNew[i-1] - 1; |
| 4030 | d = r + 1 - leafData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4031 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4032 | szRight += szCell[d] + 2; |
| 4033 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4034 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4035 | r = cntNew[i-1] - 1; |
| 4036 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4037 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4038 | szNew[i] = szRight; |
| 4039 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4040 | } |
| 4041 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4042 | |
| 4043 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4044 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4045 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4046 | assert( pPage->pgno>1 ); |
| 4047 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4048 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4049 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4050 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4051 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4052 | pgnoNew[i] = pgnoOld[i]; |
| 4053 | apOld[i] = 0; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4054 | rc = sqlite3pager_write(pNew->aData); |
| 4055 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4056 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4057 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4058 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4059 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4060 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4061 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4062 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4065 | /* Free any old pages that were not reused as new pages. |
| 4066 | */ |
| 4067 | while( i<nOld ){ |
| 4068 | rc = freePage(apOld[i]); |
| 4069 | if( rc ) goto balance_cleanup; |
| 4070 | releasePage(apOld[i]); |
| 4071 | apOld[i] = 0; |
| 4072 | i++; |
| 4073 | } |
| 4074 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4075 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4076 | ** Put the new pages in accending order. This helps to |
| 4077 | ** keep entries in the disk file in order so that a scan |
| 4078 | ** of the table is a linear scan through the file. That |
| 4079 | ** in turn helps the operating system to deliver pages |
| 4080 | ** from the disk more rapidly. |
| 4081 | ** |
| 4082 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4083 | ** n is never more than NB (a small constant), that should |
| 4084 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4085 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4086 | ** When NB==3, this one optimization makes the database |
| 4087 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4088 | */ |
| 4089 | for(i=0; i<k-1; i++){ |
| 4090 | int minV = pgnoNew[i]; |
| 4091 | int minI = i; |
| 4092 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 4093 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4094 | minI = j; |
| 4095 | minV = pgnoNew[j]; |
| 4096 | } |
| 4097 | } |
| 4098 | if( minI>i ){ |
| 4099 | int t; |
| 4100 | MemPage *pT; |
| 4101 | t = pgnoNew[i]; |
| 4102 | pT = apNew[i]; |
| 4103 | pgnoNew[i] = pgnoNew[minI]; |
| 4104 | apNew[i] = apNew[minI]; |
| 4105 | pgnoNew[minI] = t; |
| 4106 | apNew[minI] = pT; |
| 4107 | } |
| 4108 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4109 | 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] | 4110 | pgnoOld[0], |
| 4111 | nOld>=2 ? pgnoOld[1] : 0, |
| 4112 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 4113 | pgnoNew[0], szNew[0], |
| 4114 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 4115 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4116 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 4117 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4118 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4119 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4120 | ** Evenly distribute the data in apCell[] across the new pages. |
| 4121 | ** Insert divider cells into pParent as necessary. |
| 4122 | */ |
| 4123 | j = 0; |
| 4124 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4125 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4126 | MemPage *pNew = apNew[i]; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4127 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4128 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4129 | assert( pNew->nCell>0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4130 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4131 | |
| 4132 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4133 | /* If this is an auto-vacuum database, update the pointer map entries |
| 4134 | ** that point to the siblings that were rearranged. These can be: left |
| 4135 | ** children of cells, the right-child of the page, or overflow pages |
| 4136 | ** pointed to by cells. |
| 4137 | */ |
| 4138 | if( pBt->autoVacuum ){ |
| 4139 | for(k=j; k<cntNew[i]; k++){ |
| 4140 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4141 | rc = ptrmapPutOvfl(pNew, k-j); |
| 4142 | if( rc!=SQLITE_OK ){ |
| 4143 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | #endif |
| 4149 | |
| 4150 | j = cntNew[i]; |
| 4151 | |
| 4152 | /* If the sibling page assembled above was not the right-most sibling, |
| 4153 | ** insert a divider cell into the parent page. |
| 4154 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4155 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4156 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4157 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4158 | int sz; |
| 4159 | pCell = apCell[j]; |
| 4160 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4161 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4162 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4163 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4164 | }else if( leafData ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4165 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 4166 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 4167 | ** cell consists of the integer key for the right-most cell of |
| 4168 | ** the sibling-page assembled above only. |
| 4169 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4170 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4171 | j--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4172 | parseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4173 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4174 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4175 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 4176 | assert( iSpace<=pBt->psAligned*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4177 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4178 | }else{ |
| 4179 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4180 | pTemp = &aSpace[iSpace]; |
| 4181 | iSpace += sz; |
drh | 887dc4c | 2004-10-22 16:22:57 +0000 | [diff] [blame] | 4182 | assert( iSpace<=pBt->psAligned*5 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4183 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4184 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4185 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4186 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4187 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4188 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 4189 | ** then update the pointer map with an entry for the overflow page |
| 4190 | ** that the cell just inserted points to (if any). |
| 4191 | */ |
| 4192 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4193 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 4194 | if( rc!=SQLITE_OK ){ |
| 4195 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4199 | j++; |
| 4200 | nxDiv++; |
| 4201 | } |
| 4202 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4203 | assert( j==nCell ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4204 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4205 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4206 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4207 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4208 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4209 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4210 | }else{ |
| 4211 | /* Right-most sibling is the left child of the first entry in pParent |
| 4212 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4213 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | /* |
| 4217 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4218 | */ |
| 4219 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4220 | rc = reparentChildPages(apNew[i]); |
| 4221 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4222 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4223 | rc = reparentChildPages(pParent); |
| 4224 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4225 | |
| 4226 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4227 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4228 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4229 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4230 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4231 | assert( pParent->isInit ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4232 | /* assert( pPage->isInit ); // No! pPage might have been added to freelist */ |
| 4233 | /* pageIntegrity(pPage); // No! pPage might have been added to freelist */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4234 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4235 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4236 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4237 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4238 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4239 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4240 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4241 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4242 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4243 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4244 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4245 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4246 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4247 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4248 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4249 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4250 | return rc; |
| 4251 | } |
| 4252 | |
| 4253 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4254 | ** This routine is called for the root page of a btree when the root |
| 4255 | ** page contains no cells. This is an opportunity to make the tree |
| 4256 | ** shallower by one level. |
| 4257 | */ |
| 4258 | static int balance_shallower(MemPage *pPage){ |
| 4259 | MemPage *pChild; /* The only child page of pPage */ |
| 4260 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4261 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
| 4262 | Btree *pBt; /* The main BTree structure */ |
| 4263 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4264 | u8 **apCell; /* All cells from pages being balanced */ |
| 4265 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4266 | |
| 4267 | assert( pPage->pParent==0 ); |
| 4268 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4269 | pBt = pPage->pBt; |
| 4270 | mxCellPerPage = MX_CELL(pBt); |
| 4271 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4272 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4273 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4274 | if( pPage->leaf ){ |
| 4275 | /* The table is completely empty */ |
| 4276 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4277 | }else{ |
| 4278 | /* The root page is empty but has one child. Transfer the |
| 4279 | ** information from that one child into the root page if it |
| 4280 | ** will fit. This reduces the depth of the tree by one. |
| 4281 | ** |
| 4282 | ** If the root page is page 1, it has less space available than |
| 4283 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4284 | ** of the database fle), so it might not be able to hold all of the |
| 4285 | ** information currently contained in the child. If this is the |
| 4286 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4287 | ** for the right-pointer to the child page. The child page becomes |
| 4288 | ** the virtual root of the tree. |
| 4289 | */ |
| 4290 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4291 | assert( pgnoChild>0 ); |
| 4292 | assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) ); |
| 4293 | rc = getPage(pPage->pBt, pgnoChild, &pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4294 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4295 | if( pPage->pgno==1 ){ |
| 4296 | rc = initPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4297 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4298 | assert( pChild->nOverflow==0 ); |
| 4299 | if( pChild->nFree>=100 ){ |
| 4300 | /* The child information will fit on the root page, so do the |
| 4301 | ** copy */ |
| 4302 | int i; |
| 4303 | zeroPage(pPage, pChild->aData[0]); |
| 4304 | for(i=0; i<pChild->nCell; i++){ |
| 4305 | apCell[i] = findCell(pChild,i); |
| 4306 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 4307 | } |
| 4308 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4309 | /* Copy the right-pointer of the child to the parent. */ |
| 4310 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 4311 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4312 | freePage(pChild); |
| 4313 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 4314 | }else{ |
| 4315 | /* The child has more information that will fit on the root. |
| 4316 | ** The tree is already balanced. Do nothing. */ |
| 4317 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 4318 | } |
| 4319 | }else{ |
| 4320 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 4321 | pPage->isInit = 0; |
| 4322 | pPage->pParent = 0; |
| 4323 | rc = initPage(pPage, 0); |
| 4324 | assert( rc==SQLITE_OK ); |
| 4325 | freePage(pChild); |
| 4326 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 4327 | pChild->pgno, pPage->pgno)); |
| 4328 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4329 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4330 | assert( pPage->nOverflow==0 ); |
| 4331 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4332 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4333 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4334 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4335 | rc = ptrmapPutOvfl(pPage, i); |
| 4336 | if( rc!=SQLITE_OK ){ |
| 4337 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | } |
| 4341 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4342 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4343 | releasePage(pChild); |
| 4344 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4345 | end_shallow_balance: |
| 4346 | sqliteFree(apCell); |
| 4347 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | |
| 4351 | /* |
| 4352 | ** The root page is overfull |
| 4353 | ** |
| 4354 | ** When this happens, Create a new child page and copy the |
| 4355 | ** contents of the root into the child. Then make the root |
| 4356 | ** page an empty page with rightChild pointing to the new |
| 4357 | ** child. Finally, call balance_internal() on the new child |
| 4358 | ** to cause it to split. |
| 4359 | */ |
| 4360 | static int balance_deeper(MemPage *pPage){ |
| 4361 | int rc; /* Return value from subprocedures */ |
| 4362 | MemPage *pChild; /* Pointer to a new child page */ |
| 4363 | Pgno pgnoChild; /* Page number of the new child page */ |
| 4364 | Btree *pBt; /* The BTree */ |
| 4365 | int usableSize; /* Total usable size of a page */ |
| 4366 | u8 *data; /* Content of the parent page */ |
| 4367 | u8 *cdata; /* Content of the child page */ |
| 4368 | int hdr; /* Offset to page header in parent */ |
| 4369 | int brk; /* Offset to content of first cell in parent */ |
| 4370 | |
| 4371 | assert( pPage->pParent==0 ); |
| 4372 | assert( pPage->nOverflow>0 ); |
| 4373 | pBt = pPage->pBt; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4374 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4375 | if( rc ) return rc; |
| 4376 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
| 4377 | usableSize = pBt->usableSize; |
| 4378 | data = pPage->aData; |
| 4379 | hdr = pPage->hdrOffset; |
| 4380 | brk = get2byte(&data[hdr+5]); |
| 4381 | cdata = pChild->aData; |
| 4382 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 4383 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 4384 | assert( pChild->isInit==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4385 | rc = initPage(pChild, pPage); |
| 4386 | if( rc ) return rc; |
| 4387 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 4388 | pChild->nOverflow = pPage->nOverflow; |
| 4389 | if( pChild->nOverflow ){ |
| 4390 | pChild->nFree = 0; |
| 4391 | } |
| 4392 | assert( pChild->nCell==pPage->nCell ); |
| 4393 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 4394 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 4395 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4396 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4397 | if( pBt->autoVacuum ){ |
| 4398 | int i; |
| 4399 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
| 4400 | if( rc ) return rc; |
| 4401 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4402 | rc = ptrmapPutOvfl(pChild, i); |
| 4403 | if( rc!=SQLITE_OK ){ |
| 4404 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4408 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4409 | rc = balance_nonroot(pChild); |
| 4410 | releasePage(pChild); |
| 4411 | return rc; |
| 4412 | } |
| 4413 | |
| 4414 | /* |
| 4415 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 4416 | ** required, call the appropriate balancing routine. |
| 4417 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4418 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4419 | int rc = SQLITE_OK; |
| 4420 | if( pPage->pParent==0 ){ |
| 4421 | if( pPage->nOverflow>0 ){ |
| 4422 | rc = balance_deeper(pPage); |
| 4423 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4424 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4425 | rc = balance_shallower(pPage); |
| 4426 | } |
| 4427 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4428 | if( pPage->nOverflow>0 || |
| 4429 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4430 | rc = balance_nonroot(pPage); |
| 4431 | } |
| 4432 | } |
| 4433 | return rc; |
| 4434 | } |
| 4435 | |
| 4436 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4437 | ** This routine checks all cursors that point to table pgnoRoot. |
| 4438 | ** If any of those cursors other than pExclude were opened with |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4439 | ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4440 | ** cursors that point to pgnoRoot were opened with wrFlag==1 |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4441 | ** then this routine returns SQLITE_OK. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4442 | ** |
| 4443 | ** In addition to checking for read-locks (where a read-lock |
| 4444 | ** means a cursor opened with wrFlag==0) this routine also moves |
| 4445 | ** all cursors other than pExclude so that they are pointing to the |
| 4446 | ** first Cell on root page. This is necessary because an insert |
| 4447 | ** or delete might change the number of cells on a page or delete |
| 4448 | ** a page entirely and we do not want to leave any cursors |
| 4449 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4450 | */ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4451 | static int checkReadLocks(Btree *pBt, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4452 | BtCursor *p; |
| 4453 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 4454 | if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue; |
| 4455 | if( p->wrFlag==0 ) return SQLITE_LOCKED; |
| 4456 | if( p->pPage->pgno!=p->pgnoRoot ){ |
| 4457 | moveToRoot(p); |
| 4458 | } |
| 4459 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4460 | return SQLITE_OK; |
| 4461 | } |
| 4462 | |
| 4463 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4464 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 4465 | ** 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] | 4466 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4467 | ** is left pointing at a random location. |
| 4468 | ** |
| 4469 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 4470 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4471 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4472 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 4473 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4474 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 4475 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4476 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4477 | int rc; |
| 4478 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4479 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4480 | MemPage *pPage; |
| 4481 | Btree *pBt = pCur->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4482 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4483 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4484 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4485 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4486 | /* Must start a transaction before doing an insert */ |
| 4487 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4488 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4489 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4490 | if( !pCur->wrFlag ){ |
| 4491 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 4492 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4493 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4494 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 4495 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4496 | rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4497 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4498 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4499 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4500 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4501 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 4502 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 4503 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 4504 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4505 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4506 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4507 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 4508 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4509 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4510 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4511 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4512 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 4513 | if( loc==0 && pCur->isValid ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4514 | int szOld; |
| 4515 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4516 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4517 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4518 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4519 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4520 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4521 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4522 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4523 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 4524 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4525 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4526 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 4527 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4528 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4529 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4530 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4531 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4532 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4533 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4534 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 4535 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4536 | if( rc==SQLITE_OK ){ |
| 4537 | moveToRoot(pCur); |
| 4538 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4539 | end_insert: |
| 4540 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4541 | return rc; |
| 4542 | } |
| 4543 | |
| 4544 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4545 | ** Delete the entry that the cursor is pointing to. The cursor |
| 4546 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4547 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4548 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4549 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4550 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4551 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 4552 | Pgno pgnoChild = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 4553 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4554 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 4555 | assert( pPage->isInit ); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4556 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4557 | /* Must start a transaction before doing a delete */ |
| 4558 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4559 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4560 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4561 | if( pCur->idx >= pPage->nCell ){ |
| 4562 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 4563 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4564 | if( !pCur->wrFlag ){ |
| 4565 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 4566 | } |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 4567 | if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4568 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 4569 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4570 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4571 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4572 | |
| 4573 | /* Locate the cell within it's page and leave pCell pointing to the |
| 4574 | ** data. The clearCell() call frees any overflow pages associated with the |
| 4575 | ** cell. The cell itself is still intact. |
| 4576 | */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4577 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4578 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4579 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4580 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4581 | rc = clearCell(pPage, pCell); |
| 4582 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4583 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4584 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4585 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4586 | ** 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] | 4587 | ** do something we will leave a hole on an internal page. |
| 4588 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 4589 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4590 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4591 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4592 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4593 | unsigned char *pNext; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4594 | int szNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4595 | int notUsed; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4596 | unsigned char *tempCell; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4597 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4598 | getTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4599 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4600 | if( rc!=SQLITE_OK ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4601 | if( rc!=SQLITE_NOMEM ){ |
| 4602 | rc = SQLITE_CORRUPT; /* bkpt-CORRUPT */ |
| 4603 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4604 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4605 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4606 | rc = sqlite3pager_write(leafCur.pPage->aData); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4607 | if( rc ) return rc; |
| 4608 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 4609 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 4610 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4611 | pNext = findCell(leafCur.pPage, leafCur.idx); |
| 4612 | szNext = cellSizePtr(leafCur.pPage, pNext); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4613 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 4614 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4615 | if( tempCell==0 ) return SQLITE_NOMEM; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4616 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4617 | if( rc!=SQLITE_OK ) return rc; |
| 4618 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4619 | rc = balance(pPage, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4620 | sqliteFree(tempCell); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4621 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4622 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4623 | rc = balance(leafCur.pPage, 0); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4624 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4625 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4626 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 4627 | pCur->pgnoRoot, pPage->pgno)); |
| 4628 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4629 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4630 | } |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4631 | moveToRoot(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4632 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4633 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4634 | |
| 4635 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 4636 | ** Create a new BTree table. Write into *piTable the page |
| 4637 | ** number for the root page of the new table. |
| 4638 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4639 | ** The type of type is determined by the flags parameter. Only the |
| 4640 | ** following values of flags are currently in use. Other values for |
| 4641 | ** flags might not work: |
| 4642 | ** |
| 4643 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 4644 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4645 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4646 | int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4647 | MemPage *pRoot; |
| 4648 | Pgno pgnoRoot; |
| 4649 | int rc; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4650 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4651 | /* Must start a transaction first */ |
| 4652 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4653 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4654 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4655 | |
| 4656 | /* It is illegal to create a table if any cursors are open on the |
| 4657 | ** database. This is because in auto-vacuum mode the backend may |
| 4658 | ** need to move a database page to make room for the new root-page. |
| 4659 | ** If an open cursor was using the page a problem would occur. |
| 4660 | */ |
| 4661 | if( pBt->pCursor ){ |
| 4662 | return SQLITE_LOCKED; |
| 4663 | } |
| 4664 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4665 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4666 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4667 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4668 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4669 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4670 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 4671 | MemPage *pPageMove; /* The page to move to. */ |
| 4672 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4673 | /* Read the value of meta[3] from the database to determine where the |
| 4674 | ** root page of the new table should go. meta[3] is the largest root-page |
| 4675 | ** created so far, so the new root-page is (meta[3]+1). |
| 4676 | */ |
| 4677 | rc = sqlite3BtreeGetMeta(pBt, 4, &pgnoRoot); |
| 4678 | if( rc!=SQLITE_OK ) return rc; |
| 4679 | pgnoRoot++; |
| 4680 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4681 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 4682 | ** PENDING_BYTE page. |
| 4683 | */ |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 4684 | if( pgnoRoot==PTRMAP_PAGENO(pBt->usableSize, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4685 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4686 | pgnoRoot++; |
| 4687 | } |
| 4688 | assert( pgnoRoot>=3 ); |
| 4689 | |
| 4690 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 4691 | ** be moved to the allocated page (unless the allocated page happens |
| 4692 | ** to reside at pgnoRoot). |
| 4693 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4694 | rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4695 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4696 | return rc; |
| 4697 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4698 | |
| 4699 | if( pgnoMove!=pgnoRoot ){ |
| 4700 | u8 eType; |
| 4701 | Pgno iPtrPage; |
| 4702 | |
| 4703 | releasePage(pPageMove); |
| 4704 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 4705 | if( rc!=SQLITE_OK ){ |
| 4706 | return rc; |
| 4707 | } |
| 4708 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4709 | assert( eType!=PTRMAP_ROOTPAGE ); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4710 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4711 | if( rc!=SQLITE_OK ){ |
| 4712 | releasePage(pRoot); |
| 4713 | return rc; |
| 4714 | } |
| 4715 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 4716 | releasePage(pRoot); |
| 4717 | if( rc!=SQLITE_OK ){ |
| 4718 | return rc; |
| 4719 | } |
| 4720 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 4721 | if( rc!=SQLITE_OK ){ |
| 4722 | return rc; |
| 4723 | } |
| 4724 | rc = sqlite3pager_write(pRoot->aData); |
| 4725 | if( rc!=SQLITE_OK ){ |
| 4726 | releasePage(pRoot); |
| 4727 | return rc; |
| 4728 | } |
| 4729 | }else{ |
| 4730 | pRoot = pPageMove; |
| 4731 | } |
| 4732 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4733 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4734 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 4735 | if( rc ){ |
| 4736 | releasePage(pRoot); |
| 4737 | return rc; |
| 4738 | } |
| 4739 | rc = sqlite3BtreeUpdateMeta(pBt, 4, pgnoRoot); |
| 4740 | if( rc ){ |
| 4741 | releasePage(pRoot); |
| 4742 | return rc; |
| 4743 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 4744 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4745 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4746 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4747 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4748 | } |
| 4749 | #endif |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4750 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4751 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4752 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4753 | *piTable = (int)pgnoRoot; |
| 4754 | return SQLITE_OK; |
| 4755 | } |
| 4756 | |
| 4757 | /* |
| 4758 | ** Erase the given database page and all its children. Return |
| 4759 | ** the page to the freelist. |
| 4760 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4761 | static int clearDatabasePage( |
| 4762 | Btree *pBt, /* The BTree that contains the table */ |
| 4763 | Pgno pgno, /* Page number to clear */ |
| 4764 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 4765 | int freePageFlag /* Deallocate page if true */ |
| 4766 | ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4767 | MemPage *pPage; |
| 4768 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4769 | unsigned char *pCell; |
| 4770 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4771 | |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame^] | 4772 | if( pgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 4773 | return SQLITE_CORRUPT; |
| 4774 | } |
| 4775 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4776 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4777 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4778 | rc = sqlite3pager_write(pPage->aData); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4779 | if( rc ) return rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4780 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4781 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4782 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4783 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4784 | if( rc ) return rc; |
| 4785 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4786 | rc = clearCell(pPage, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4787 | if( rc ) return rc; |
| 4788 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4789 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4790 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4791 | if( rc ) return rc; |
| 4792 | } |
| 4793 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4794 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4795 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4796 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4797 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4798 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4799 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4803 | ** Delete all information from a single table in the database. iTable is |
| 4804 | ** the page number of the root of the table. After this routine returns, |
| 4805 | ** the root page is empty, but still exists. |
| 4806 | ** |
| 4807 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 4808 | ** read cursors on the table. Open write cursors are moved to the |
| 4809 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4810 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4811 | int sqlite3BtreeClearTable(Btree *pBt, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4812 | int rc; |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4813 | BtCursor *pCur; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4814 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4815 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4816 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4817 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 4818 | if( pCur->pgnoRoot==(Pgno)iTable ){ |
| 4819 | if( pCur->wrFlag==0 ) return SQLITE_LOCKED; |
| 4820 | moveToRoot(pCur); |
| 4821 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 4822 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4823 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4824 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4825 | sqlite3BtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4826 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4827 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | /* |
| 4831 | ** Erase all information in a table and add the root of the table to |
| 4832 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4833 | ** page 1) is never added to the freelist. |
| 4834 | ** |
| 4835 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 4836 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4837 | ** |
| 4838 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 4839 | ** root page in the database file, then the last root page |
| 4840 | ** in the database file is moved into the slot formerly occupied by |
| 4841 | ** iTable and that last slot formerly occupied by the last root page |
| 4842 | ** is added to the freelist instead of iTable. In this say, all |
| 4843 | ** root pages are kept at the beginning of the database file, which |
| 4844 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 4845 | ** page number that used to be the last root page in the file before |
| 4846 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 4847 | ** The last root page is recorded in meta[3] and the value of |
| 4848 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4849 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4850 | int sqlite3BtreeDropTable(Btree *pBt, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4851 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4852 | MemPage *pPage = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4853 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4854 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4855 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4856 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4857 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 4858 | /* It is illegal to drop a table if any cursors are open on the |
| 4859 | ** database. This is because in auto-vacuum mode the backend may |
| 4860 | ** need to move another root-page to fill a gap left by the deleted |
| 4861 | ** root page. If an open cursor was using this page a problem would |
| 4862 | ** occur. |
| 4863 | */ |
| 4864 | if( pBt->pCursor ){ |
| 4865 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 4866 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4867 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4868 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4869 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4870 | rc = sqlite3BtreeClearTable(pBt, iTable); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4871 | if( rc ) return rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4872 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 4873 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4874 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4875 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4876 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4877 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4878 | releasePage(pPage); |
| 4879 | #else |
| 4880 | if( pBt->autoVacuum ){ |
| 4881 | Pgno maxRootPgno; |
| 4882 | rc = sqlite3BtreeGetMeta(pBt, 4, &maxRootPgno); |
| 4883 | if( rc!=SQLITE_OK ){ |
| 4884 | releasePage(pPage); |
| 4885 | return rc; |
| 4886 | } |
| 4887 | |
| 4888 | if( iTable==maxRootPgno ){ |
| 4889 | /* If the table being dropped is the table with the largest root-page |
| 4890 | ** number in the database, put the root page on the free list. |
| 4891 | */ |
| 4892 | rc = freePage(pPage); |
| 4893 | releasePage(pPage); |
| 4894 | if( rc!=SQLITE_OK ){ |
| 4895 | return rc; |
| 4896 | } |
| 4897 | }else{ |
| 4898 | /* The table being dropped does not have the largest root-page |
| 4899 | ** number in the database. So move the page that does into the |
| 4900 | ** gap left by the deleted root-page. |
| 4901 | */ |
| 4902 | MemPage *pMove; |
| 4903 | releasePage(pPage); |
| 4904 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 4905 | if( rc!=SQLITE_OK ){ |
| 4906 | return rc; |
| 4907 | } |
| 4908 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 4909 | releasePage(pMove); |
| 4910 | if( rc!=SQLITE_OK ){ |
| 4911 | return rc; |
| 4912 | } |
| 4913 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 4914 | if( rc!=SQLITE_OK ){ |
| 4915 | return rc; |
| 4916 | } |
| 4917 | rc = freePage(pMove); |
| 4918 | releasePage(pMove); |
| 4919 | if( rc!=SQLITE_OK ){ |
| 4920 | return rc; |
| 4921 | } |
| 4922 | *piMoved = maxRootPgno; |
| 4923 | } |
| 4924 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4925 | /* Set the new 'max-root-page' value in the database header. This |
| 4926 | ** is the old value less one, less one more if that happens to |
| 4927 | ** be a root-page number, less one again if that is the |
| 4928 | ** PENDING_BYTE_PAGE. |
| 4929 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4930 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4931 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4932 | maxRootPgno--; |
| 4933 | } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 4934 | if( maxRootPgno==PTRMAP_PAGENO(pBt->usableSize, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4935 | maxRootPgno--; |
| 4936 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4937 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 4938 | |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 4939 | rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4940 | }else{ |
| 4941 | rc = freePage(pPage); |
| 4942 | releasePage(pPage); |
| 4943 | } |
| 4944 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 4945 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4946 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4947 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 4948 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4949 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4950 | return rc; |
| 4951 | } |
| 4952 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 4953 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4954 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4955 | ** Read the meta-information out of a database file. Meta[0] |
| 4956 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 4957 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 4958 | ** is read-only, the others are read/write. |
| 4959 | ** |
| 4960 | ** The schema layer numbers meta values differently. At the schema |
| 4961 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 4962 | ** 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] | 4963 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4964 | int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4965 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4966 | unsigned char *pP1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4967 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4968 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4969 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4970 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4971 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4972 | sqlite3pager_unref(pP1); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4973 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4974 | /* If autovacuumed is disabled in this build but we are trying to |
| 4975 | ** access an autovacuumed database, then make the database readonly. |
| 4976 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4977 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4978 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 4979 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 4980 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4981 | return SQLITE_OK; |
| 4982 | } |
| 4983 | |
| 4984 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4985 | ** Write meta-information back into the database. Meta[0] is |
| 4986 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4987 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4988 | int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4989 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4990 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4991 | assert( idx>=1 && idx<=15 ); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 4992 | if( pBt->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 4993 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 4994 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4995 | assert( pBt->pPage1!=0 ); |
| 4996 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4997 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4998 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 4999 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5000 | return SQLITE_OK; |
| 5001 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5002 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5003 | /* |
| 5004 | ** Return the flag byte at the beginning of the page that the cursor |
| 5005 | ** is currently pointing to. |
| 5006 | */ |
| 5007 | int sqlite3BtreeFlags(BtCursor *pCur){ |
| 5008 | MemPage *pPage = pCur->pPage; |
| 5009 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 5010 | } |
| 5011 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5012 | #ifdef SQLITE_DEBUG |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5013 | /* |
| 5014 | ** Print a disassembly of the given page on standard output. This routine |
| 5015 | ** is used for debugging and testing only. |
| 5016 | */ |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5017 | static int btreePageDump(Btree *pBt, int pgno, int recursive, MemPage *pParent){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5018 | int rc; |
| 5019 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5020 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5021 | int nFree; |
| 5022 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5023 | int hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5024 | int nCell; |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5025 | int isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5026 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5027 | char range[20]; |
| 5028 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5029 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5030 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5031 | isInit = pPage->isInit; |
| 5032 | if( pPage->isInit==0 ){ |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5033 | initPage(pPage, pParent); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5034 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5035 | if( rc ){ |
| 5036 | return rc; |
| 5037 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5038 | hdr = pPage->hdrOffset; |
| 5039 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5040 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5041 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5042 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5043 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5044 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5045 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5046 | nCell = get2byte(&data[hdr+3]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5047 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5048 | data[hdr], data[hdr+7], |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5049 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5050 | assert( hdr == (pgno==1 ? 100 : 0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5051 | idx = hdr + 12 - pPage->leaf*4; |
| 5052 | for(i=0; i<nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5053 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5054 | Pgno child; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5055 | unsigned char *pCell; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5056 | int sz; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5057 | int addr; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5058 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5059 | addr = get2byte(&data[idx + 2*i]); |
| 5060 | pCell = &data[addr]; |
| 5061 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5062 | sz = info.nSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5063 | sprintf(range,"%d..%d", addr, addr+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5064 | if( pPage->leaf ){ |
| 5065 | child = 0; |
| 5066 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5067 | child = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5068 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5069 | sz = info.nData; |
| 5070 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5071 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5072 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5073 | for(j=0; j<sz; j++){ |
| 5074 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 5075 | } |
| 5076 | payload[sz] = 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5077 | sqlite3DebugPrintf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5078 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 5079 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5080 | ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5081 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5082 | if( !pPage->leaf ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5083 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5084 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5085 | nFree = 0; |
| 5086 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5087 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5088 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5089 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5090 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 5091 | nFree += sz; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5092 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5093 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5094 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5095 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5096 | } |
| 5097 | if( idx!=0 ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5098 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5099 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5100 | if( recursive && !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5101 | for(i=0; i<nCell; i++){ |
| 5102 | unsigned char *pCell = findCell(pPage, i); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5103 | btreePageDump(pBt, get4byte(pCell), 1, pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5104 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5105 | } |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5106 | btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5107 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5108 | pPage->isInit = isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5109 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 5110 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5111 | return SQLITE_OK; |
| 5112 | } |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5113 | int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){ |
| 5114 | return btreePageDump(pBt, pgno, recursive, 0); |
| 5115 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5116 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5117 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5118 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5119 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5120 | ** Fill aResult[] with information about the entry and page that the |
| 5121 | ** cursor is pointing to. |
| 5122 | ** |
| 5123 | ** aResult[0] = The page number |
| 5124 | ** aResult[1] = The entry number |
| 5125 | ** aResult[2] = Total number of entries on this page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5126 | ** aResult[3] = Cell size (local payload + header) |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5127 | ** aResult[4] = Number of free bytes on this page |
| 5128 | ** aResult[5] = Number of free blocks on the page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5129 | ** aResult[6] = Total payload size (local + overflow) |
| 5130 | ** aResult[7] = Header size in bytes |
| 5131 | ** aResult[8] = Local payload size |
| 5132 | ** aResult[9] = Parent page number |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5133 | ** |
| 5134 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5135 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5136 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5137 | int cnt, idx; |
| 5138 | MemPage *pPage = pCur->pPage; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5139 | BtCursor tmpCur; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5140 | |
| 5141 | pageIntegrity(pPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5142 | assert( pPage->isInit ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5143 | getTempCursor(pCur, &tmpCur); |
| 5144 | while( upCnt-- ){ |
| 5145 | moveToParent(&tmpCur); |
| 5146 | } |
| 5147 | pPage = tmpCur.pPage; |
| 5148 | pageIntegrity(pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5149 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5150 | assert( aResult[0]==pPage->pgno ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5151 | aResult[1] = tmpCur.idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5152 | aResult[2] = pPage->nCell; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5153 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 5154 | getCellInfo(&tmpCur); |
| 5155 | aResult[3] = tmpCur.info.nSize; |
| 5156 | aResult[6] = tmpCur.info.nData; |
| 5157 | aResult[7] = tmpCur.info.nHeader; |
| 5158 | aResult[8] = tmpCur.info.nLocal; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5159 | }else{ |
| 5160 | aResult[3] = 0; |
| 5161 | aResult[6] = 0; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5162 | aResult[7] = 0; |
| 5163 | aResult[8] = 0; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5164 | } |
| 5165 | aResult[4] = pPage->nFree; |
| 5166 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5167 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5168 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5169 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5170 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5171 | } |
| 5172 | aResult[5] = cnt; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5173 | if( pPage->pParent==0 || isRootPage(pPage) ){ |
| 5174 | aResult[9] = 0; |
| 5175 | }else{ |
| 5176 | aResult[9] = pPage->pParent->pgno; |
| 5177 | } |
| 5178 | releaseTempCursor(&tmpCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5179 | return SQLITE_OK; |
| 5180 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5181 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5182 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5183 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5184 | ** Return the pager associated with a BTree. This routine is used for |
| 5185 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5186 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5187 | Pager *sqlite3BtreePager(Btree *pBt){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5188 | return pBt->pPager; |
| 5189 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5190 | |
| 5191 | /* |
| 5192 | ** This structure is passed around through all the sanity checking routines |
| 5193 | ** in order to keep track of some global state information. |
| 5194 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5195 | typedef struct IntegrityCk IntegrityCk; |
| 5196 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 5197 | Btree *pBt; /* The tree being checked out */ |
| 5198 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 5199 | int nPage; /* Number of pages in the database */ |
| 5200 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 5201 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5202 | }; |
| 5203 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5204 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5205 | /* |
| 5206 | ** Append a message to the error message string. |
| 5207 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5208 | static void checkAppendMsg( |
| 5209 | IntegrityCk *pCheck, |
| 5210 | char *zMsg1, |
| 5211 | const char *zFormat, |
| 5212 | ... |
| 5213 | ){ |
| 5214 | va_list ap; |
| 5215 | char *zMsg2; |
| 5216 | va_start(ap, zFormat); |
| 5217 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 5218 | va_end(ap); |
| 5219 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5220 | if( pCheck->zErrMsg ){ |
| 5221 | char *zOld = pCheck->zErrMsg; |
| 5222 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5223 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5224 | sqliteFree(zOld); |
| 5225 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5226 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5227 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5228 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5229 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5230 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5231 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5232 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5233 | /* |
| 5234 | ** Add 1 to the reference count for page iPage. If this is the second |
| 5235 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 5236 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 5237 | ** if this is the first reference to the page. |
| 5238 | ** |
| 5239 | ** Also check that the page number is in bounds. |
| 5240 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5241 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5242 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5243 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5244 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5245 | return 1; |
| 5246 | } |
| 5247 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5248 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5249 | return 1; |
| 5250 | } |
| 5251 | return (pCheck->anRef[iPage]++)>1; |
| 5252 | } |
| 5253 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5254 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5255 | /* |
| 5256 | ** Check that the entry in the pointer-map for page iChild maps to |
| 5257 | ** page iParent, pointer type ptrType. If not, append an error message |
| 5258 | ** to pCheck. |
| 5259 | */ |
| 5260 | static void checkPtrmap( |
| 5261 | IntegrityCk *pCheck, /* Integrity check context */ |
| 5262 | Pgno iChild, /* Child page number */ |
| 5263 | u8 eType, /* Expected pointer map type */ |
| 5264 | Pgno iParent, /* Expected pointer map parent page number */ |
| 5265 | char *zContext /* Context description (used for error msg) */ |
| 5266 | ){ |
| 5267 | int rc; |
| 5268 | u8 ePtrmapType; |
| 5269 | Pgno iPtrmapParent; |
| 5270 | |
| 5271 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 5272 | if( rc!=SQLITE_OK ){ |
| 5273 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 5274 | return; |
| 5275 | } |
| 5276 | |
| 5277 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 5278 | checkAppendMsg(pCheck, zContext, |
| 5279 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 5280 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 5281 | } |
| 5282 | } |
| 5283 | #endif |
| 5284 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5285 | /* |
| 5286 | ** Check the integrity of the freelist or of an overflow page list. |
| 5287 | ** Verify that the number of pages on the list is N. |
| 5288 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5289 | static void checkList( |
| 5290 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 5291 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 5292 | int iPage, /* Page number for first page in the list */ |
| 5293 | int N, /* Expected number of pages in the list */ |
| 5294 | char *zContext /* Context for error messages */ |
| 5295 | ){ |
| 5296 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5297 | int expected = N; |
| 5298 | int iFirst = iPage; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5299 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5300 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5301 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5302 | checkAppendMsg(pCheck, zContext, |
| 5303 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5304 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5305 | break; |
| 5306 | } |
| 5307 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5308 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5309 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5310 | break; |
| 5311 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5312 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5313 | int n = get4byte(&pOvfl[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5314 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5315 | if( pCheck->pBt->autoVacuum ){ |
| 5316 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 5317 | } |
| 5318 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 5319 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5320 | checkAppendMsg(pCheck, zContext, |
| 5321 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5322 | N--; |
| 5323 | }else{ |
| 5324 | for(i=0; i<n; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5325 | Pgno iFreePage = get4byte(&pOvfl[8+i*4]); |
| 5326 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5327 | if( pCheck->pBt->autoVacuum ){ |
| 5328 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 5329 | } |
| 5330 | #endif |
| 5331 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5332 | } |
| 5333 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5334 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5335 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5336 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5337 | else{ |
| 5338 | /* If this database supports auto-vacuum and iPage is not the last |
| 5339 | ** page in this overflow list, check that the pointer-map entry for |
| 5340 | ** the following page matches iPage. |
| 5341 | */ |
| 5342 | if( pCheck->pBt->autoVacuum && N>0 ){ |
| 5343 | i = get4byte(pOvfl); |
| 5344 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 5345 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5346 | } |
| 5347 | #endif |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5348 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5349 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5350 | } |
| 5351 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5352 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5353 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5354 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5355 | /* |
| 5356 | ** Do various sanity checks on a single page of a tree. Return |
| 5357 | ** the tree depth. Root pages return 0. Parents of root pages |
| 5358 | ** return 1, and so forth. |
| 5359 | ** |
| 5360 | ** These checks are done: |
| 5361 | ** |
| 5362 | ** 1. Make sure that cells and freeblocks do not overlap |
| 5363 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5364 | ** NO 2. Make sure cell keys are in order. |
| 5365 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 5366 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5367 | ** 5. Check the integrity of overflow pages. |
| 5368 | ** 6. Recursively call checkTreePage on all children. |
| 5369 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5370 | ** 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] | 5371 | ** the root of the tree. |
| 5372 | */ |
| 5373 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5374 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5375 | int iPage, /* Page number of the page to check */ |
| 5376 | MemPage *pParent, /* Parent page */ |
| 5377 | char *zParentContext, /* Parent context */ |
| 5378 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 5379 | int nLower, /* Number of characters in zLowerBound */ |
| 5380 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 5381 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5382 | ){ |
| 5383 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5384 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5385 | int hdr, cellStart; |
| 5386 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5387 | u8 *data; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5388 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 5389 | Btree *pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5390 | int maxLocal, usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5391 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5392 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5393 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5394 | sprintf(zContext, "Page %d: ", iPage); |
| 5395 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5396 | /* Check that the page exists |
| 5397 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 5398 | cur.pBt = pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5399 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5400 | if( iPage==0 ) return 0; |
| 5401 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5402 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5403 | checkAppendMsg(pCheck, zContext, |
| 5404 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5405 | return 0; |
| 5406 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5407 | maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5408 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5409 | checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5410 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5411 | return 0; |
| 5412 | } |
| 5413 | |
| 5414 | /* Check out all the cells. |
| 5415 | */ |
| 5416 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5417 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5418 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5419 | u8 *pCell; |
| 5420 | int sz; |
| 5421 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5422 | |
| 5423 | /* Check payload overflow pages |
| 5424 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5425 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5426 | pCell = findCell(pPage,i); |
| 5427 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5428 | sz = info.nData; |
| 5429 | if( !pPage->intKey ) sz += info.nKey; |
| 5430 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5431 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5432 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 5433 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5434 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5435 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5436 | } |
| 5437 | #endif |
| 5438 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | /* Check sanity of left child page. |
| 5442 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5443 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5444 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5445 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5446 | if( pBt->autoVacuum ){ |
| 5447 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 5448 | } |
| 5449 | #endif |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5450 | d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0); |
| 5451 | if( i>0 && d2!=depth ){ |
| 5452 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 5453 | } |
| 5454 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5455 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5456 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5457 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5458 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5459 | sprintf(zContext, "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5460 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5461 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5462 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5463 | } |
| 5464 | #endif |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5465 | checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0); |
| 5466 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5467 | |
| 5468 | /* Check for complete coverage of the page |
| 5469 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5470 | data = pPage->aData; |
| 5471 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5472 | hit = sqliteMalloc( usableSize ); |
| 5473 | if( hit ){ |
| 5474 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 5475 | nCell = get2byte(&data[hdr+3]); |
| 5476 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 5477 | for(i=0; i<nCell; i++){ |
| 5478 | int pc = get2byte(&data[cellStart+i*2]); |
| 5479 | int size = cellSizePtr(pPage, &data[pc]); |
| 5480 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 5481 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 5482 | checkAppendMsg(pCheck, 0, |
| 5483 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 5484 | }else{ |
| 5485 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 5486 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5487 | } |
| 5488 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 5489 | cnt++){ |
| 5490 | int size = get2byte(&data[i+2]); |
| 5491 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 5492 | if( (i+size-1)>=usableSize || i<0 ){ |
| 5493 | checkAppendMsg(pCheck, 0, |
| 5494 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 5495 | }else{ |
| 5496 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 5497 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5498 | i = get2byte(&data[i]); |
| 5499 | } |
| 5500 | for(i=cnt=0; i<usableSize; i++){ |
| 5501 | if( hit[i]==0 ){ |
| 5502 | cnt++; |
| 5503 | }else if( hit[i]>1 ){ |
| 5504 | checkAppendMsg(pCheck, 0, |
| 5505 | "Multiple uses for byte %d of page %d", i, iPage); |
| 5506 | break; |
| 5507 | } |
| 5508 | } |
| 5509 | if( cnt!=data[hdr+7] ){ |
| 5510 | checkAppendMsg(pCheck, 0, |
| 5511 | "Fragmented space is %d byte reported as %d on page %d", |
| 5512 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5513 | } |
| 5514 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5515 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5516 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5517 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5518 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5519 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5520 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5521 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5522 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5523 | /* |
| 5524 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 5525 | ** an array of pages numbers were each page number is the root page of |
| 5526 | ** a table. nRoot is the number of entries in aRoot. |
| 5527 | ** |
| 5528 | ** If everything checks out, this routine returns NULL. If something is |
| 5529 | ** amiss, an error message is written into memory obtained from malloc() |
| 5530 | ** and a pointer to that error message is returned. The calling function |
| 5531 | ** is responsible for freeing the error message when it is done. |
| 5532 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5533 | char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5534 | int i; |
| 5535 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5536 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5537 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5538 | nRef = *sqlite3pager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 5539 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 5540 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 5541 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5542 | sCheck.pBt = pBt; |
| 5543 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5544 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5545 | if( sCheck.nPage==0 ){ |
| 5546 | unlockBtreeIfUnused(pBt); |
| 5547 | return 0; |
| 5548 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 5549 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5550 | if( !sCheck.anRef ){ |
| 5551 | unlockBtreeIfUnused(pBt); |
| 5552 | return sqlite3MPrintf("Unable to malloc %d bytes", |
| 5553 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 5554 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5555 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 5556 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 5557 | if( i<=sCheck.nPage ){ |
| 5558 | sCheck.anRef[i] = 1; |
| 5559 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5560 | sCheck.zErrMsg = 0; |
| 5561 | |
| 5562 | /* Check the integrity of the freelist |
| 5563 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5564 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 5565 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* Check all the tables. |
| 5568 | */ |
| 5569 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 5570 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5571 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5572 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 5573 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 5574 | } |
| 5575 | #endif |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 5576 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /* Make sure every page in the file is referenced |
| 5580 | */ |
| 5581 | for(i=1; i<=sCheck.nPage; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5582 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5583 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5584 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5585 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5586 | #else |
| 5587 | /* If the database supports auto-vacuum, make sure no tables contain |
| 5588 | ** references to pointer-map pages. |
| 5589 | */ |
| 5590 | if( sCheck.anRef[i]==0 && |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 5591 | (PTRMAP_PAGENO(pBt->usableSize, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5592 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 5593 | } |
| 5594 | if( sCheck.anRef[i]!=0 && |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 5595 | (PTRMAP_PAGENO(pBt->usableSize, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5596 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 5597 | } |
| 5598 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | /* Make sure this analysis did not leave any unref() pages |
| 5602 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5603 | unlockBtreeIfUnused(pBt); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5604 | if( nRef != *sqlite3pager_stats(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5605 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5606 | "Outstanding page count goes from %d to %d during this analysis", |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5607 | nRef, *sqlite3pager_stats(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5608 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | /* Clean up and report errors. |
| 5612 | */ |
| 5613 | sqliteFree(sCheck.anRef); |
| 5614 | return sCheck.zErrMsg; |
| 5615 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5616 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 5617 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5618 | /* |
| 5619 | ** Return the full pathname of the underlying database file. |
| 5620 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5621 | const char *sqlite3BtreeGetFilename(Btree *pBt){ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5622 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5623 | return sqlite3pager_filename(pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5627 | ** Return the pathname of the directory that contains the database file. |
| 5628 | */ |
| 5629 | const char *sqlite3BtreeGetDirname(Btree *pBt){ |
| 5630 | assert( pBt->pPager!=0 ); |
| 5631 | return sqlite3pager_dirname(pBt->pPager); |
| 5632 | } |
| 5633 | |
| 5634 | /* |
| 5635 | ** Return the pathname of the journal file for this database. The return |
| 5636 | ** value of this routine is the same regardless of whether the journal file |
| 5637 | ** has been created or not. |
| 5638 | */ |
| 5639 | const char *sqlite3BtreeGetJournalname(Btree *pBt){ |
| 5640 | assert( pBt->pPager!=0 ); |
| 5641 | return sqlite3pager_journalname(pBt->pPager); |
| 5642 | } |
| 5643 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5644 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 5645 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5646 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 5647 | ** must be active for both files. |
| 5648 | ** |
| 5649 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5650 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5651 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5652 | int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5653 | int rc = SQLITE_OK; |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5654 | Pgno i, nPage, nToPage; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5655 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 5656 | if( pBtTo->inTrans!=TRANS_WRITE || pBtFrom->inTrans!=TRANS_WRITE ){ |
| 5657 | return SQLITE_ERROR; |
| 5658 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5659 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5660 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 5661 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 5662 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5663 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5664 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5665 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5666 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5667 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5668 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5669 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5670 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 5671 | void *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5672 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5673 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5674 | rc = sqlite3pager_write(pPage); |
| 5675 | sqlite3pager_unref(pPage); |
| 5676 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5677 | } |
| 5678 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5679 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 5680 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5681 | if( rc ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5682 | sqlite3BtreeRollback(pBtTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 5683 | } |
| 5684 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 5685 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5686 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5687 | |
| 5688 | /* |
| 5689 | ** Return non-zero if a transaction is active. |
| 5690 | */ |
| 5691 | int sqlite3BtreeIsInTrans(Btree *pBt){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 5692 | return (pBt && (pBt->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | /* |
| 5696 | ** Return non-zero if a statement transaction is active. |
| 5697 | */ |
| 5698 | int sqlite3BtreeIsInStmt(Btree *pBt){ |
| 5699 | return (pBt && pBt->inStmt); |
| 5700 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 5701 | |
| 5702 | /* |
| 5703 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 5704 | ** |
| 5705 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 5706 | ** the name of a master journal file that should be written into the |
| 5707 | ** individual journal file, or is NULL, indicating no master journal file |
| 5708 | ** (single database transaction). |
| 5709 | ** |
| 5710 | ** When this is called, the master journal should already have been |
| 5711 | ** created, populated with this journal pointer and synced to disk. |
| 5712 | ** |
| 5713 | ** Once this is routine has returned, the only thing required to commit |
| 5714 | ** the write-transaction for this database file is to delete the journal. |
| 5715 | */ |
| 5716 | int sqlite3BtreeSync(Btree *pBt, const char *zMaster){ |
| 5717 | if( pBt->inTrans==TRANS_WRITE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5718 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5719 | Pgno nTrunc = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5720 | if( pBt->autoVacuum ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5721 | int rc = autoVacuumCommit(pBt, &nTrunc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5722 | if( rc!=SQLITE_OK ) return rc; |
| 5723 | } |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5724 | return sqlite3pager_sync(pBt->pPager, zMaster, nTrunc); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5725 | #endif |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 5726 | return sqlite3pager_sync(pBt->pPager, zMaster, 0); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 5727 | } |
| 5728 | return SQLITE_OK; |
| 5729 | } |