drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.331 2006/11/30 13:05:29 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
| 15 | ** For a detailed discussion of BTrees, refer to |
| 16 | ** |
| 17 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 18 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 19 | ** Publishing Company, Reading, Massachusetts. |
| 20 | ** |
| 21 | ** The basic idea is that each page of the file contains N database |
| 22 | ** entries and N+1 pointers to subpages. |
| 23 | ** |
| 24 | ** ---------------------------------------------------------------- |
| 25 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) | |
| 26 | ** ---------------------------------------------------------------- |
| 27 | ** |
| 28 | ** All of the keys on the page that Ptr(0) points to have values less |
| 29 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
| 30 | ** values greater than Key(0) and less than Key(1). All of the keys |
| 31 | ** on Ptr(N+1) and its subpages have values greater than Key(N). And |
| 32 | ** so forth. |
| 33 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 34 | ** Finding a particular key requires reading O(log(M)) pages from the |
| 35 | ** disk where M is the number of entries in the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** In this implementation, a single file can hold one or more separate |
| 38 | ** BTrees. Each BTree is identified by the index of its root page. The |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 39 | ** key and data for any entry are combined to form the "payload". A |
| 40 | ** fixed amount of payload can be carried directly on the database |
| 41 | ** page. If the payload is larger than the preset amount then surplus |
| 42 | ** bytes are stored on overflow pages. The payload for an entry |
| 43 | ** and the preceding pointer are combined to form a "Cell". Each |
| 44 | ** page has a small header which contains the Ptr(N+1) pointer and other |
| 45 | ** information such as the size of key and data. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 46 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 47 | ** FORMAT DETAILS |
| 48 | ** |
| 49 | ** The file is divided into pages. The first page is called page 1, |
| 50 | ** the second is page 2, and so forth. A page number of zero indicates |
| 51 | ** "no such page". The page size can be anything between 512 and 65536. |
| 52 | ** Each page can be either a btree page, a freelist page or an overflow |
| 53 | ** page. |
| 54 | ** |
| 55 | ** The first page is always a btree page. The first 100 bytes of the first |
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 | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 214 | /* Round up a number to the next larger multiple of 8. This is used |
| 215 | ** to force 8-byte alignment on 64-bit architectures. |
| 216 | */ |
| 217 | #define ROUND8(x) ((x+7)&~7) |
| 218 | |
| 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; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 233 | typedef struct BtLock BtLock; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 234 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 235 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 236 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 237 | ** SQLite database in order to identify the file as a real database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 238 | ** |
| 239 | ** You can change this value at compile-time by specifying a |
| 240 | ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The |
| 241 | ** header must be exactly 16 bytes including the zero-terminator so |
| 242 | ** the string itself should be 15 characters long. If you change |
| 243 | ** the header, then your custom library will not be able to read |
| 244 | ** databases generated by the standard tools and the standard tools |
| 245 | ** will not be able to read databases created by your custom library. |
| 246 | */ |
| 247 | #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */ |
| 248 | # define SQLITE_FILE_HEADER "SQLite format 3" |
| 249 | #endif |
| 250 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 251 | |
| 252 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 253 | ** Page type flags. An ORed combination of these flags appear as the |
| 254 | ** first byte of every BTree page. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 255 | */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 256 | #define PTF_INTKEY 0x01 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 257 | #define PTF_ZERODATA 0x02 |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 258 | #define PTF_LEAFDATA 0x04 |
| 259 | #define PTF_LEAF 0x08 |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 260 | |
| 261 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 262 | ** As each page of the file is loaded into memory, an instance of the following |
| 263 | ** structure is appended and initialized to zero. This structure stores |
| 264 | ** information about the page that is decoded from the raw file page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 265 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 266 | ** The pParent field points back to the parent page. This allows us to |
| 267 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 268 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 269 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 270 | */ |
| 271 | struct MemPage { |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 272 | u8 isInit; /* True if previously initialized. MUST BE FIRST! */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 273 | u8 idxShift; /* True if Cell indices have changed */ |
| 274 | u8 nOverflow; /* Number of overflow cell bodies in aCell[] */ |
| 275 | u8 intKey; /* True if intkey flag is set */ |
| 276 | u8 leaf; /* True if leaf flag is set */ |
| 277 | u8 zeroData; /* True if table stores keys only */ |
| 278 | u8 leafData; /* True if tables stores data on leaves only */ |
| 279 | u8 hasData; /* True if this page stores data */ |
| 280 | u8 hdrOffset; /* 100 for page 1. 0 otherwise */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 281 | u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 282 | u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */ |
| 283 | u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 284 | u16 cellOffset; /* Index in aData of first cell pointer */ |
| 285 | u16 idxParent; /* Index in parent of this node */ |
| 286 | u16 nFree; /* Number of free bytes on the page */ |
| 287 | u16 nCell; /* Number of cells on this page, local and ovfl */ |
| 288 | struct _OvflCell { /* Cells that will not fit on aData[] */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 289 | u8 *pCell; /* Pointers to the body of the overflow cell */ |
| 290 | u16 idx; /* Insert this cell before idx-th non-overflow cell */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 291 | } aOvfl[5]; |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 292 | BtShared *pBt; /* Pointer back to BTree structure */ |
| 293 | u8 *aData; /* Pointer back to the start of the page */ |
| 294 | Pgno pgno; /* Page number for this page */ |
| 295 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 296 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 297 | |
| 298 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 299 | ** The in-memory image of a disk page has the auxiliary information appended |
| 300 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 301 | ** that extra information. |
| 302 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 303 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 304 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 305 | /* Btree handle */ |
| 306 | struct Btree { |
| 307 | sqlite3 *pSqlite; |
| 308 | BtShared *pBt; |
| 309 | u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ |
| 310 | }; |
| 311 | |
| 312 | /* |
| 313 | ** Btree.inTrans may take one of the following values. |
| 314 | ** |
| 315 | ** If the shared-data extension is enabled, there may be multiple users |
| 316 | ** of the Btree structure. At most one of these may open a write transaction, |
| 317 | ** but any number may have active read transactions. Variable Btree.pDb |
| 318 | ** points to the handle that owns any current write-transaction. |
| 319 | */ |
| 320 | #define TRANS_NONE 0 |
| 321 | #define TRANS_READ 1 |
| 322 | #define TRANS_WRITE 2 |
| 323 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 324 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 325 | ** Everything we need to know about an open database |
| 326 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 327 | struct BtShared { |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 328 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 329 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 330 | MemPage *pPage1; /* First page of the database */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 331 | u8 inStmt; /* True if we are in a statement subtransaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 332 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 333 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 334 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 335 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 336 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 337 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 338 | u8 autoVacuum; /* True if database supports auto-vacuum */ |
| 339 | #endif |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 340 | u16 pageSize; /* Total number of bytes on a page */ |
| 341 | u16 usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 342 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 343 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 344 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 345 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 346 | BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 347 | u8 inTransaction; /* Transaction state */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 348 | int nRef; /* Number of references to this structure */ |
| 349 | int nTransaction; /* Number of open transactions (read + write) */ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 350 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 351 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 352 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 353 | BtLock *pLock; /* List of locks held on this shared-btree struct */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 354 | BtShared *pNext; /* Next in ThreadData.pBtree linked list */ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 355 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 356 | }; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 359 | ** An instance of the following structure is used to hold information |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 360 | ** about a cell. The parseCellPtr() function fills in this structure |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 361 | ** based on information extract from the raw disk page. |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 362 | */ |
| 363 | typedef struct CellInfo CellInfo; |
| 364 | struct CellInfo { |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 365 | u8 *pCell; /* Pointer to the start of cell content */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 366 | i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 367 | u32 nData; /* Number of bytes of data */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 368 | u16 nHeader; /* Size of the cell content header in bytes */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 369 | u16 nLocal; /* Amount of payload held locally */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 370 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 371 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | /* |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 375 | ** A cursor is a pointer to a particular entry in the BTree. |
| 376 | ** The entry is identified by its MemPage and the index in |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 377 | ** MemPage.aCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 378 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 379 | struct BtCursor { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 380 | Btree *pBtree; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 381 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 382 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 383 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 384 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 385 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 386 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 387 | CellInfo info; /* A parse of the cell we are pointing at */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 388 | u8 wrFlag; /* True if writable */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 389 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
drh | 4eeb1ff | 2006-03-23 14:03:00 +0000 | [diff] [blame] | 390 | void *pKey; /* Saved key that was cursor's last known position */ |
| 391 | i64 nKey; /* Size of pKey, or last integer key */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 392 | int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 393 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 394 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 395 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 396 | ** Potential values for BtCursor.eState. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 397 | ** |
| 398 | ** CURSOR_VALID: |
| 399 | ** Cursor points to a valid entry. getPayload() etc. may be called. |
| 400 | ** |
| 401 | ** CURSOR_INVALID: |
| 402 | ** Cursor does not point to a valid entry. This can happen (for example) |
| 403 | ** because the table is empty or because BtreeCursorFirst() has not been |
| 404 | ** called. |
| 405 | ** |
| 406 | ** CURSOR_REQUIRESEEK: |
| 407 | ** The table that this cursor was opened on still exists, but has been |
| 408 | ** modified since the cursor was last used. The cursor position is saved |
| 409 | ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 410 | ** this state, restoreOrClearCursorPosition() can be called to attempt to |
| 411 | ** seek the cursor to the saved position. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 412 | */ |
| 413 | #define CURSOR_INVALID 0 |
| 414 | #define CURSOR_VALID 1 |
| 415 | #define CURSOR_REQUIRESEEK 2 |
| 416 | |
| 417 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 418 | ** The TRACE macro will print high-level status information about the |
| 419 | ** btree operation when the global variable sqlite3_btree_trace is |
| 420 | ** enabled. |
| 421 | */ |
| 422 | #if SQLITE_TEST |
| 423 | # define TRACE(X) if( sqlite3_btree_trace )\ |
| 424 | { sqlite3DebugPrintf X; fflush(stdout); } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 425 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 426 | #else |
| 427 | # define TRACE(X) |
| 428 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 429 | |
| 430 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 431 | ** Forward declaration |
| 432 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 433 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 434 | |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 435 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 436 | ** Read or write a two- and four-byte big-endian integer values. |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 437 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 438 | static u32 get2byte(unsigned char *p){ |
| 439 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 440 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 441 | static u32 get4byte(unsigned char *p){ |
| 442 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 443 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 444 | static void put2byte(unsigned char *p, u32 v){ |
| 445 | p[0] = v>>8; |
| 446 | p[1] = v; |
| 447 | } |
| 448 | static void put4byte(unsigned char *p, u32 v){ |
| 449 | p[0] = v>>24; |
| 450 | p[1] = v>>16; |
| 451 | p[2] = v>>8; |
| 452 | p[3] = v; |
| 453 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 454 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 455 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 456 | ** Routines to read and write variable-length integers. These used to |
| 457 | ** be defined locally, but now we use the varint routines in the util.c |
| 458 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 459 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 460 | #define getVarint sqlite3GetVarint |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 461 | /* #define getVarint32 sqlite3GetVarint32 */ |
| 462 | #define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B)) |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 463 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 464 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 465 | /* The database page the PENDING_BYTE occupies. This page is never used. |
| 466 | ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They |
| 467 | ** should possibly be consolidated (presumably in pager.h). |
drh | fe9a914 | 2006-03-14 12:59:10 +0000 | [diff] [blame] | 468 | ** |
| 469 | ** If disk I/O is omitted (meaning that the database is stored purely |
| 470 | ** in memory) then there is no pending byte. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 471 | */ |
drh | fe9a914 | 2006-03-14 12:59:10 +0000 | [diff] [blame] | 472 | #ifdef SQLITE_OMIT_DISKIO |
| 473 | # define PENDING_BYTE_PAGE(pBt) 0x7fffffff |
| 474 | #else |
| 475 | # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) |
| 476 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 477 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 478 | /* |
| 479 | ** A linked list of the following structures is stored at BtShared.pLock. |
| 480 | ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor |
| 481 | ** is opened on the table with root page BtShared.iTable. Locks are removed |
| 482 | ** from this list when a transaction is committed or rolled back, or when |
| 483 | ** a btree handle is closed. |
| 484 | */ |
| 485 | struct BtLock { |
| 486 | Btree *pBtree; /* Btree handle holding this lock */ |
| 487 | Pgno iTable; /* Root page of table */ |
| 488 | u8 eLock; /* READ_LOCK or WRITE_LOCK */ |
| 489 | BtLock *pNext; /* Next in BtShared.pLock list */ |
| 490 | }; |
| 491 | |
| 492 | /* Candidate values for BtLock.eLock */ |
| 493 | #define READ_LOCK 1 |
| 494 | #define WRITE_LOCK 2 |
| 495 | |
| 496 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 497 | /* |
| 498 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 499 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 500 | ** shared-cache table level locks. If the library is compiled with the |
| 501 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 502 | ** of each BtShared structure and so this locking is not necessary. |
| 503 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 504 | */ |
| 505 | #define queryTableLock(a,b,c) SQLITE_OK |
| 506 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 507 | #define unlockAllTables(a) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 508 | #else |
| 509 | |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 510 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 511 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 512 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 513 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 514 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 515 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 516 | */ |
| 517 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 518 | BtShared *pBt = p->pBt; |
| 519 | BtLock *pIter; |
| 520 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 521 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 522 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 523 | return SQLITE_OK; |
| 524 | } |
| 525 | |
| 526 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 527 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 528 | ** set, it is unconditionally granted - even if there are write-locks |
| 529 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 530 | ** is not considered. |
| 531 | ** |
| 532 | ** In function lockTable(), if a read-lock is demanded and the |
| 533 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 534 | ** (BtShared.pLock). |
| 535 | ** |
| 536 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 537 | ** not create or respect table locks. The locking procedure for a |
| 538 | ** write-cursor does not change. |
| 539 | */ |
| 540 | if( |
| 541 | !p->pSqlite || |
| 542 | 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) || |
| 543 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 544 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 545 | ){ |
| 546 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 547 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 548 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 549 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 550 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | return SQLITE_OK; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 558 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 559 | ** WRITE_LOCK. |
| 560 | ** |
| 561 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 562 | ** SQLITE_NOMEM may also be returned. |
| 563 | */ |
| 564 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 565 | BtShared *pBt = p->pBt; |
| 566 | BtLock *pLock = 0; |
| 567 | BtLock *pIter; |
| 568 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 569 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 570 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 571 | return SQLITE_OK; |
| 572 | } |
| 573 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 574 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 575 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 576 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 577 | ** return early without adding an entry to the BtShared.pLock list. See |
| 578 | ** comment in function queryTableLock() for more info on handling |
| 579 | ** the ReadUncommitted flag. |
| 580 | */ |
| 581 | if( |
| 582 | (p->pSqlite) && |
| 583 | (p->pSqlite->flags&SQLITE_ReadUncommitted) && |
| 584 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 585 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 586 | ){ |
| 587 | return SQLITE_OK; |
| 588 | } |
| 589 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 590 | /* First search the list for an existing lock on this table. */ |
| 591 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 592 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 593 | pLock = pIter; |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /* If the above search did not find a BtLock struct associating Btree p |
| 599 | ** with table iTable, allocate one and link it into the list. |
| 600 | */ |
| 601 | if( !pLock ){ |
| 602 | pLock = (BtLock *)sqliteMalloc(sizeof(BtLock)); |
| 603 | if( !pLock ){ |
| 604 | return SQLITE_NOMEM; |
| 605 | } |
| 606 | pLock->iTable = iTable; |
| 607 | pLock->pBtree = p; |
| 608 | pLock->pNext = pBt->pLock; |
| 609 | pBt->pLock = pLock; |
| 610 | } |
| 611 | |
| 612 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 613 | ** and the requested lock. This means if a write-lock was already held |
| 614 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 615 | */ |
| 616 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 617 | if( eLock>pLock->eLock ){ |
| 618 | pLock->eLock = eLock; |
| 619 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 620 | |
| 621 | return SQLITE_OK; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 626 | ** procedure) held by Btree handle p. |
| 627 | */ |
| 628 | static void unlockAllTables(Btree *p){ |
| 629 | BtLock **ppIter = &p->pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 630 | |
| 631 | /* If the shared-cache extension is not enabled, there should be no |
| 632 | ** locks in the BtShared.pLock list, making this procedure a no-op. Assert |
| 633 | ** that this is the case. |
| 634 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 635 | assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 636 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 637 | while( *ppIter ){ |
| 638 | BtLock *pLock = *ppIter; |
| 639 | if( pLock->pBtree==p ){ |
| 640 | *ppIter = pLock->pNext; |
| 641 | sqliteFree(pLock); |
| 642 | }else{ |
| 643 | ppIter = &pLock->pNext; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 648 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 649 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 650 | |
| 651 | /* |
| 652 | ** Save the current cursor position in the variables BtCursor.nKey |
| 653 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 654 | */ |
| 655 | static int saveCursorPosition(BtCursor *pCur){ |
| 656 | int rc; |
| 657 | |
| 658 | assert( CURSOR_VALID==pCur->eState ); |
| 659 | assert( 0==pCur->pKey ); |
| 660 | |
| 661 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 662 | |
| 663 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 664 | ** stores the integer key in pCur->nKey. In this case this value is |
| 665 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 666 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 667 | ** data. |
| 668 | */ |
| 669 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
| 670 | void *pKey = sqliteMalloc(pCur->nKey); |
| 671 | if( pKey ){ |
| 672 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 673 | if( rc==SQLITE_OK ){ |
| 674 | pCur->pKey = pKey; |
| 675 | }else{ |
| 676 | sqliteFree(pKey); |
| 677 | } |
| 678 | }else{ |
| 679 | rc = SQLITE_NOMEM; |
| 680 | } |
| 681 | } |
| 682 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 683 | |
| 684 | if( rc==SQLITE_OK ){ |
| 685 | releasePage(pCur->pPage); |
| 686 | pCur->pPage = 0; |
| 687 | pCur->eState = CURSOR_REQUIRESEEK; |
| 688 | } |
| 689 | |
| 690 | return rc; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | ** Save the positions of all cursors except pExcept open on the table |
| 695 | ** with root-page iRoot. Usually, this is called just before cursor |
| 696 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 697 | */ |
| 698 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 699 | BtCursor *p; |
| 700 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 701 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 702 | p->eState==CURSOR_VALID ){ |
| 703 | int rc = saveCursorPosition(p); |
| 704 | if( SQLITE_OK!=rc ){ |
| 705 | return rc; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | return SQLITE_OK; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 714 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 715 | ** saved position info stored by saveCursorPosition(), so there can be |
| 716 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 717 | ** saveCursorPosition(). |
| 718 | ** |
| 719 | ** If the second argument argument - doSeek - is false, then instead of |
| 720 | ** returning the cursor to it's saved position, any saved position is deleted |
| 721 | ** and the cursor state set to CURSOR_INVALID. |
| 722 | */ |
| 723 | static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){ |
| 724 | int rc = SQLITE_OK; |
| 725 | assert( pCur->eState==CURSOR_REQUIRESEEK ); |
| 726 | pCur->eState = CURSOR_INVALID; |
| 727 | if( doSeek ){ |
| 728 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip); |
| 729 | } |
| 730 | if( rc==SQLITE_OK ){ |
| 731 | sqliteFree(pCur->pKey); |
| 732 | pCur->pKey = 0; |
| 733 | assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState ); |
| 734 | } |
| 735 | return rc; |
| 736 | } |
| 737 | |
| 738 | #define restoreOrClearCursorPosition(p,x) \ |
| 739 | (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK) |
| 740 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 741 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 742 | /* |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 743 | ** These macros define the location of the pointer-map entry for a |
| 744 | ** database page. The first argument to each is the number of usable |
| 745 | ** bytes on each page of the database (often 1024). The second is the |
| 746 | ** page number to look up in the pointer map. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 747 | ** |
| 748 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
| 749 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
| 750 | ** the offset of the requested map entry. |
| 751 | ** |
| 752 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
| 753 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 754 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
| 755 | ** this test. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 756 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 757 | #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) |
| 758 | #define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1)) |
| 759 | #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) |
| 760 | |
| 761 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
| 762 | int nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 763 | int iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 764 | int ret = (iPtrMap*nPagesPerMapPage) + 2; |
| 765 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 766 | ret++; |
| 767 | } |
| 768 | return ret; |
| 769 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 770 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 771 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 772 | ** The pointer map is a lookup table that identifies the parent page for |
| 773 | ** each child page in the database file. The parent page is the page that |
| 774 | ** contains a pointer to the child. Every page in the database contains |
| 775 | ** 0 or 1 parent pages. (In this context 'database page' refers |
| 776 | ** to any page that is not part of the pointer map itself.) Each pointer map |
| 777 | ** entry consists of a single byte 'type' and a 4 byte parent page number. |
| 778 | ** The PTRMAP_XXX identifiers below are the valid types. |
| 779 | ** |
| 780 | ** The purpose of the pointer map is to facility moving pages from one |
| 781 | ** position in the file to another as part of autovacuum. When a page |
| 782 | ** is moved, the pointer in its parent must be updated to point to the |
| 783 | ** new location. The pointer map is used to locate the parent page quickly. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 784 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 785 | ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not |
| 786 | ** used in this case. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 787 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 788 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
| 789 | ** is not used in this case. |
| 790 | ** |
| 791 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
| 792 | ** overflow pages. The page number identifies the page that |
| 793 | ** contains the cell with a pointer to this overflow page. |
| 794 | ** |
| 795 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
| 796 | ** overflow pages. The page-number identifies the previous |
| 797 | ** page in the overflow page list. |
| 798 | ** |
| 799 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
| 800 | ** identifies the parent page in the btree. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 801 | */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 802 | #define PTRMAP_ROOTPAGE 1 |
| 803 | #define PTRMAP_FREEPAGE 2 |
| 804 | #define PTRMAP_OVERFLOW1 3 |
| 805 | #define PTRMAP_OVERFLOW2 4 |
| 806 | #define PTRMAP_BTREE 5 |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 807 | |
| 808 | /* |
| 809 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 810 | ** |
| 811 | ** This routine updates the pointer map entry for page number 'key' |
| 812 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 813 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 814 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 815 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 816 | u8 *pPtrmap; /* The pointer map page */ |
| 817 | Pgno iPtrmap; /* The pointer map page number */ |
| 818 | int offset; /* Offset in pointer map page */ |
| 819 | int rc; |
| 820 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 821 | /* The master-journal page number must never be used as a pointer map page */ |
| 822 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 823 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 824 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 825 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 826 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 827 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 828 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 829 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 830 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 831 | return rc; |
| 832 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 833 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 834 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 835 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 836 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 837 | rc = sqlite3pager_write(pPtrmap); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 838 | if( rc==SQLITE_OK ){ |
| 839 | pPtrmap[offset] = eType; |
| 840 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 841 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | sqlite3pager_unref(pPtrmap); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 845 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* |
| 849 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 850 | ** |
| 851 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 852 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 853 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 854 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 855 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 856 | int iPtrmap; /* Pointer map page index */ |
| 857 | u8 *pPtrmap; /* Pointer map page data */ |
| 858 | int offset; /* Offset of entry in pointer map */ |
| 859 | int rc; |
| 860 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 861 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 862 | rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap); |
| 863 | if( rc!=0 ){ |
| 864 | return rc; |
| 865 | } |
| 866 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 867 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 868 | assert( pEType!=0 ); |
| 869 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 870 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 871 | |
| 872 | sqlite3pager_unref(pPtrmap); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 873 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 874 | return SQLITE_OK; |
| 875 | } |
| 876 | |
| 877 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 878 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 879 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 880 | ** Given a btree page and a cell index (0 means the first cell on |
| 881 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 882 | ** to the cell content. |
| 883 | ** |
| 884 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 885 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 886 | static u8 *findCell(MemPage *pPage, int iCell){ |
| 887 | u8 *data = pPage->aData; |
| 888 | assert( iCell>=0 ); |
| 889 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
| 890 | return data + get2byte(&data[pPage->cellOffset+2*iCell]); |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | ** This a more complex version of findCell() that works for |
| 895 | ** pages that do contain overflow cells. See insert |
| 896 | */ |
| 897 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 898 | int i; |
| 899 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 900 | int k; |
| 901 | struct _OvflCell *pOvfl; |
| 902 | pOvfl = &pPage->aOvfl[i]; |
| 903 | k = pOvfl->idx; |
| 904 | if( k<=iCell ){ |
| 905 | if( k==iCell ){ |
| 906 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 907 | } |
| 908 | iCell--; |
| 909 | } |
| 910 | } |
| 911 | return findCell(pPage, iCell); |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 916 | ** are two versions of this function. parseCell() takes a cell index |
| 917 | ** as the second argument and parseCellPtr() takes a pointer to the |
| 918 | ** body of the cell as its second argument. |
| 919 | */ |
| 920 | static void parseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 921 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 922 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 923 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 924 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 925 | int n; /* Number bytes in cell content header */ |
| 926 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 927 | |
| 928 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 929 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 930 | n = pPage->childPtrSize; |
| 931 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 932 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 933 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 934 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 935 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 936 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 937 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 938 | if( pPage->intKey ){ |
| 939 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 940 | }else{ |
| 941 | u32 x; |
| 942 | n += getVarint32(&pCell[n], &x); |
| 943 | pInfo->nKey = x; |
| 944 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 945 | } |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 946 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 947 | if( nPayload<=pPage->maxLocal ){ |
| 948 | /* This is the (easy) common case where the entire payload fits |
| 949 | ** on the local page. No overflow is required. |
| 950 | */ |
| 951 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 952 | pInfo->nLocal = nPayload; |
| 953 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 954 | nSize = nPayload + n; |
| 955 | if( nSize<4 ){ |
| 956 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 957 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 958 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 959 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 960 | /* If the payload will not fit completely on the local page, we have |
| 961 | ** to decide how much to store locally and how much to spill onto |
| 962 | ** overflow pages. The strategy is to minimize the amount of unused |
| 963 | ** space on overflow pages while keeping the amount of local storage |
| 964 | ** in between minLocal and maxLocal. |
| 965 | ** |
| 966 | ** Warning: changing the way overflow payload is distributed in any |
| 967 | ** way will result in an incompatible file format. |
| 968 | */ |
| 969 | int minLocal; /* Minimum amount of payload held locally */ |
| 970 | int maxLocal; /* Maximum amount of payload held locally */ |
| 971 | int surplus; /* Overflow payload available for local storage */ |
| 972 | |
| 973 | minLocal = pPage->minLocal; |
| 974 | maxLocal = pPage->maxLocal; |
| 975 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 976 | if( surplus <= maxLocal ){ |
| 977 | pInfo->nLocal = surplus; |
| 978 | }else{ |
| 979 | pInfo->nLocal = minLocal; |
| 980 | } |
| 981 | pInfo->iOverflow = pInfo->nLocal + n; |
| 982 | pInfo->nSize = pInfo->iOverflow + 4; |
| 983 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 984 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 985 | static void parseCell( |
| 986 | MemPage *pPage, /* Page containing the cell */ |
| 987 | int iCell, /* The cell index. First cell is 0 */ |
| 988 | CellInfo *pInfo /* Fill in this structure */ |
| 989 | ){ |
| 990 | parseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 991 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 994 | ** Compute the total number of bytes that a Cell needs in the cell |
| 995 | ** data area of the btree-page. The return number includes the cell |
| 996 | ** data header and the local payload, but not any overflow page or |
| 997 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 998 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 999 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1000 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1001 | CellInfo info; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1002 | parseCell(pPage, iCell, &info); |
| 1003 | return info.nSize; |
| 1004 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1005 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1006 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 1007 | CellInfo info; |
| 1008 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1009 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1012 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1013 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1014 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1015 | ** to an overflow page, insert an entry into the pointer-map |
| 1016 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1017 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1018 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1019 | if( pCell ){ |
| 1020 | CellInfo info; |
| 1021 | parseCellPtr(pPage, pCell, &info); |
| 1022 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 1023 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 1024 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 1025 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1026 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1027 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1028 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1029 | /* |
| 1030 | ** If the cell with index iCell on page pPage contains a pointer |
| 1031 | ** to an overflow page, insert an entry into the pointer-map |
| 1032 | ** for the overflow page. |
| 1033 | */ |
| 1034 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 1035 | u8 *pCell; |
| 1036 | pCell = findOverflowCell(pPage, iCell); |
| 1037 | return ptrmapPutOvflPtr(pPage, pCell); |
| 1038 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1039 | #endif |
| 1040 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1041 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1042 | /* A bunch of assert() statements to check the transaction state variables |
| 1043 | ** of handle p (type Btree*) are internally consistent. |
| 1044 | */ |
| 1045 | #define btreeIntegrity(p) \ |
| 1046 | assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \ |
| 1047 | assert( p->pBt->nTransaction<=p->pBt->nRef ); \ |
| 1048 | assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ |
| 1049 | assert( p->pBt->inTransaction>=p->inTrans ); |
| 1050 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1051 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1052 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 1053 | ** end of the page and all free space is collected into one |
| 1054 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1055 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1056 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1057 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1058 | int i; /* Loop counter */ |
| 1059 | int pc; /* Address of a i-th cell */ |
| 1060 | int addr; /* Offset of first byte after cell pointer array */ |
| 1061 | int hdr; /* Offset to the page header */ |
| 1062 | int size; /* Size of a cell */ |
| 1063 | int usableSize; /* Number of usable bytes on a page */ |
| 1064 | int cellOffset; /* Offset to the cell pointer array */ |
| 1065 | int brk; /* Offset to the cell content area */ |
| 1066 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1067 | unsigned char *data; /* The page data */ |
| 1068 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1069 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1070 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1071 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1072 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1073 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1074 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 1075 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1076 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1077 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1078 | cellOffset = pPage->cellOffset; |
| 1079 | nCell = pPage->nCell; |
| 1080 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 1081 | usableSize = pPage->pBt->usableSize; |
| 1082 | brk = get2byte(&data[hdr+5]); |
| 1083 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 1084 | brk = usableSize; |
| 1085 | for(i=0; i<nCell; i++){ |
| 1086 | u8 *pAddr; /* The i-th cell pointer */ |
| 1087 | pAddr = &data[cellOffset + i*2]; |
| 1088 | pc = get2byte(pAddr); |
| 1089 | assert( pc<pPage->pBt->usableSize ); |
| 1090 | size = cellSizePtr(pPage, &temp[pc]); |
| 1091 | brk -= size; |
| 1092 | memcpy(&data[brk], &temp[pc], size); |
| 1093 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1094 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1095 | assert( brk>=cellOffset+2*nCell ); |
| 1096 | put2byte(&data[hdr+5], brk); |
| 1097 | data[hdr+1] = 0; |
| 1098 | data[hdr+2] = 0; |
| 1099 | data[hdr+7] = 0; |
| 1100 | addr = cellOffset+2*nCell; |
| 1101 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1102 | sqliteFree(temp); |
| 1103 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1106 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1107 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1108 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1109 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1110 | ** the new allocation. Or return 0 if there is not enough free |
| 1111 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1112 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1113 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1114 | ** nBytes of contiguous free space, then this routine automatically |
| 1115 | ** calls defragementPage() to consolidate all free space before |
| 1116 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1117 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1118 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1119 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1120 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1121 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1122 | int top; |
| 1123 | int nCell; |
| 1124 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1125 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1126 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1127 | data = pPage->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1128 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1129 | assert( pPage->pBt ); |
| 1130 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1131 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 1132 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1133 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1134 | |
| 1135 | nFrag = data[hdr+7]; |
| 1136 | if( nFrag<60 ){ |
| 1137 | /* Search the freelist looking for a slot big enough to satisfy the |
| 1138 | ** space request. */ |
| 1139 | addr = hdr+1; |
| 1140 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 1141 | size = get2byte(&data[pc+2]); |
| 1142 | if( size>=nByte ){ |
| 1143 | if( size<nByte+4 ){ |
| 1144 | memcpy(&data[addr], &data[pc], 2); |
| 1145 | data[hdr+7] = nFrag + size - nByte; |
| 1146 | return pc; |
| 1147 | }else{ |
| 1148 | put2byte(&data[pc+2], size-nByte); |
| 1149 | return pc + size - nByte; |
| 1150 | } |
| 1151 | } |
| 1152 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* Allocate memory from the gap in between the cell pointer array |
| 1157 | ** and the cell content area. |
| 1158 | */ |
| 1159 | top = get2byte(&data[hdr+5]); |
| 1160 | nCell = get2byte(&data[hdr+3]); |
| 1161 | cellOffset = pPage->cellOffset; |
| 1162 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1163 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1164 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1165 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1166 | top -= nByte; |
| 1167 | assert( cellOffset + 2*nCell <= top ); |
| 1168 | put2byte(&data[hdr+5], top); |
| 1169 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1173 | ** Return a section of the pPage->aData to the freelist. |
| 1174 | ** The first byte of the new free block is pPage->aDisk[start] |
| 1175 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1176 | ** |
| 1177 | ** Most of the effort here is involved in coalesing adjacent |
| 1178 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1179 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1180 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1181 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1182 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1183 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1184 | assert( pPage->pBt!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1185 | assert( sqlite3pager_iswriteable(data) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1186 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1187 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1188 | if( size<4 ) size = 4; |
| 1189 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 1190 | #ifdef SQLITE_SECURE_DELETE |
| 1191 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 1192 | ** option is enabled at compile-time */ |
| 1193 | memset(&data[start], 0, size); |
| 1194 | #endif |
| 1195 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1196 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1197 | hdr = pPage->hdrOffset; |
| 1198 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1199 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1200 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1201 | assert( pbegin>addr ); |
| 1202 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1203 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1204 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1205 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1206 | put2byte(&data[addr], start); |
| 1207 | put2byte(&data[start], pbegin); |
| 1208 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1209 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1210 | |
| 1211 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1212 | addr = pPage->hdrOffset + 1; |
| 1213 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1214 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1215 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1216 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1217 | pnext = get2byte(&data[pbegin]); |
| 1218 | psize = get2byte(&data[pbegin+2]); |
| 1219 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 1220 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1221 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 1222 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1223 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 1224 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 1225 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1226 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1229 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1230 | /* If the cell content area begins with a freeblock, remove it. */ |
| 1231 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 1232 | int top; |
| 1233 | pbegin = get2byte(&data[hdr+1]); |
| 1234 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 1235 | top = get2byte(&data[hdr+5]); |
| 1236 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1237 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1241 | ** Decode the flags byte (the first byte of the header) for a page |
| 1242 | ** and initialize fields of the MemPage structure accordingly. |
| 1243 | */ |
| 1244 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1245 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1246 | |
| 1247 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 1248 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 1249 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 1250 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 1251 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 1252 | pBt = pPage->pBt; |
| 1253 | if( flagByte & PTF_LEAFDATA ){ |
| 1254 | pPage->leafData = 1; |
| 1255 | pPage->maxLocal = pBt->maxLeaf; |
| 1256 | pPage->minLocal = pBt->minLeaf; |
| 1257 | }else{ |
| 1258 | pPage->leafData = 0; |
| 1259 | pPage->maxLocal = pBt->maxLocal; |
| 1260 | pPage->minLocal = pBt->minLocal; |
| 1261 | } |
| 1262 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 1263 | } |
| 1264 | |
| 1265 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1266 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1267 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1268 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1269 | ** is the parent of the page being initialized. The root of a |
| 1270 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1271 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1272 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1273 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1274 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1275 | ** guarantee that the page is well-formed. It only shows that |
| 1276 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1277 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1278 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1279 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1280 | MemPage *pParent /* The parent. Might be NULL */ |
| 1281 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1282 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1283 | int hdr; /* Offset to beginning of page header */ |
| 1284 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1285 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1286 | int usableSize; /* Amount of usable space on each page */ |
| 1287 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 1288 | int nFree; /* Number of unused bytes on the page */ |
| 1289 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1290 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1291 | pBt = pPage->pBt; |
| 1292 | assert( pBt!=0 ); |
| 1293 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1294 | assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1295 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1296 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 1297 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1298 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1299 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1300 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1301 | if( pPage->pParent==0 && pParent!=0 ){ |
| 1302 | pPage->pParent = pParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1303 | sqlite3pager_ref(pParent->aData); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1304 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1305 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1306 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1307 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1308 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 1309 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1310 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1311 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1312 | top = get2byte(&data[hdr+5]); |
| 1313 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1314 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1315 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1316 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1317 | } |
| 1318 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 1319 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1320 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1321 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1322 | |
| 1323 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1324 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1325 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1326 | while( pc>0 ){ |
| 1327 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1328 | if( pc>usableSize-4 ){ |
| 1329 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1330 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1331 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1332 | next = get2byte(&data[pc]); |
| 1333 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1334 | if( next>0 && next<=pc+size+3 ){ |
| 1335 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1336 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1337 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1338 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1339 | pc = next; |
| 1340 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1341 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1342 | if( nFree>=usableSize ){ |
| 1343 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1344 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1345 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1346 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1347 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1348 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1352 | ** Set up a raw page so that it looks like a database page holding |
| 1353 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1354 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1355 | static void zeroPage(MemPage *pPage, int flags){ |
| 1356 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1357 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1358 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1359 | int first; |
| 1360 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1361 | assert( sqlite3pager_pagenumber(data)==pPage->pgno ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1362 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1363 | assert( sqlite3pager_iswriteable(data) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1364 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1365 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1366 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1367 | memset(&data[hdr+1], 0, 4); |
| 1368 | data[hdr+7] = 0; |
| 1369 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1370 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1371 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1372 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1373 | pPage->cellOffset = first; |
| 1374 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1375 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1376 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1377 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1381 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1382 | ** MemPage.aData elements if needed. |
| 1383 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1384 | static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1385 | int rc; |
| 1386 | unsigned char *aData; |
| 1387 | MemPage *pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1388 | rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1389 | if( rc ) return rc; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1390 | pPage = (MemPage*)&aData[pBt->pageSize]; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1391 | pPage->aData = aData; |
| 1392 | pPage->pBt = pBt; |
| 1393 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1394 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1395 | *ppPage = pPage; |
| 1396 | return SQLITE_OK; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1400 | ** Get a page from the pager and initialize it. This routine |
| 1401 | ** is just a convenience wrapper around separate calls to |
| 1402 | ** getPage() and initPage(). |
| 1403 | */ |
| 1404 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1405 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1406 | Pgno pgno, /* Number of the page to get */ |
| 1407 | MemPage **ppPage, /* Write the page pointer here */ |
| 1408 | MemPage *pParent /* Parent of the page */ |
| 1409 | ){ |
| 1410 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1411 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1412 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1413 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1414 | rc = getPage(pBt, pgno, ppPage); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1415 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1416 | rc = initPage(*ppPage, pParent); |
| 1417 | } |
| 1418 | return rc; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1422 | ** Release a MemPage. This should be called once for each prior |
| 1423 | ** call to getPage. |
| 1424 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1425 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1426 | if( pPage ){ |
| 1427 | assert( pPage->aData ); |
| 1428 | assert( pPage->pBt ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1429 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1430 | sqlite3pager_unref(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1435 | ** This routine is called when the reference count for a page |
| 1436 | ** reaches zero. We need to unref the pParent pointer when that |
| 1437 | ** happens. |
| 1438 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1439 | static void pageDestructor(void *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1440 | MemPage *pPage; |
| 1441 | assert( (pageSize & 7)==0 ); |
| 1442 | pPage = (MemPage*)&((char*)pData)[pageSize]; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1443 | if( pPage->pParent ){ |
| 1444 | MemPage *pParent = pPage->pParent; |
| 1445 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1446 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1447 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1448 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1452 | ** During a rollback, when the pager reloads information into the cache |
| 1453 | ** so that the cache is restored to its original state at the start of |
| 1454 | ** the transaction, for each page restored this routine is called. |
| 1455 | ** |
| 1456 | ** This routine needs to reset the extra data section at the end of the |
| 1457 | ** page to agree with the restored data. |
| 1458 | */ |
| 1459 | static void pageReinit(void *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1460 | MemPage *pPage; |
| 1461 | assert( (pageSize & 7)==0 ); |
| 1462 | pPage = (MemPage*)&((char*)pData)[pageSize]; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1463 | if( pPage->isInit ){ |
| 1464 | pPage->isInit = 0; |
| 1465 | initPage(pPage, pPage->pParent); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1470 | ** Open a database file. |
| 1471 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1472 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1473 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1474 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1475 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1476 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1477 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1478 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1479 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1480 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1481 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1482 | BtShared *pBt; /* Shared part of btree structure */ |
| 1483 | Btree *p; /* Handle to return */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1484 | int rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1485 | int nReserve; |
| 1486 | unsigned char zDbHeader[100]; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1487 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1488 | const ThreadData *pTsdro; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1489 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1492 | ** false for a file-based database. This symbol is only required if |
| 1493 | ** either of the shared-data or autovacuum features are compiled |
| 1494 | ** into the library. |
| 1495 | */ |
| 1496 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1497 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1498 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1499 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1500 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | #endif |
| 1503 | |
| 1504 | p = sqliteMalloc(sizeof(Btree)); |
| 1505 | if( !p ){ |
| 1506 | return SQLITE_NOMEM; |
| 1507 | } |
| 1508 | p->inTrans = TRANS_NONE; |
| 1509 | p->pSqlite = pSqlite; |
| 1510 | |
| 1511 | /* Try to find an existing Btree structure opened on zFilename. */ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1512 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1513 | pTsdro = sqlite3ThreadDataReadOnly(); |
| 1514 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1515 | char *zFullPathname = sqlite3OsFullPathname(zFilename); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1516 | if( !zFullPathname ){ |
| 1517 | sqliteFree(p); |
| 1518 | return SQLITE_NOMEM; |
| 1519 | } |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1520 | for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1521 | assert( pBt->nRef>0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1522 | if( 0==strcmp(zFullPathname, sqlite3pager_filename(pBt->pPager)) ){ |
| 1523 | p->pBt = pBt; |
| 1524 | *ppBtree = p; |
| 1525 | pBt->nRef++; |
| 1526 | sqliteFree(zFullPathname); |
| 1527 | return SQLITE_OK; |
| 1528 | } |
| 1529 | } |
| 1530 | sqliteFree(zFullPathname); |
| 1531 | } |
| 1532 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1533 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1534 | /* |
| 1535 | ** The following asserts make sure that structures used by the btree are |
| 1536 | ** the right size. This is to guard against size changes that result |
| 1537 | ** when compiling on a different architecture. |
| 1538 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 1539 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1540 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1541 | assert( sizeof(u32)==4 ); |
| 1542 | assert( sizeof(u16)==2 ); |
| 1543 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1544 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1545 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1546 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1547 | *ppBtree = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1548 | sqliteFree(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1549 | return SQLITE_NOMEM; |
| 1550 | } |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 1551 | rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1552 | if( rc==SQLITE_OK ){ |
| 1553 | rc = sqlite3pager_read_fileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1554 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1555 | if( rc!=SQLITE_OK ){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1556 | if( pBt->pPager ){ |
| 1557 | sqlite3pager_close(pBt->pPager); |
| 1558 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1559 | sqliteFree(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1560 | sqliteFree(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1561 | *ppBtree = 0; |
| 1562 | return rc; |
| 1563 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1564 | p->pBt = pBt; |
| 1565 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1566 | sqlite3pager_set_destructor(pBt->pPager, pageDestructor); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1567 | sqlite3pager_set_reiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1568 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1569 | pBt->pPage1 = 0; |
| 1570 | pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1571 | pBt->pageSize = get2byte(&zDbHeader[16]); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1572 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1573 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1574 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1575 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1576 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1577 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1578 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1579 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1580 | ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM |
| 1581 | ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, |
| 1582 | ** then ":memory:" is just a regular file-name. Respect the auto-vacuum |
| 1583 | ** default in this case. |
| 1584 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1585 | if( zFilename && !isMemdb ){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1586 | pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM; |
| 1587 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1588 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1589 | nReserve = 0; |
| 1590 | }else{ |
| 1591 | nReserve = zDbHeader[20]; |
| 1592 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1593 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1594 | pBt->minLeafFrac = zDbHeader[23]; |
| 1595 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1596 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1597 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1598 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1599 | } |
| 1600 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1601 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1602 | sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1603 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1604 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1605 | /* Add the new btree to the linked list starting at ThreadData.pBtree. |
| 1606 | ** There is no chance that a malloc() may fail inside of the |
| 1607 | ** sqlite3ThreadData() call, as the ThreadData structure must have already |
| 1608 | ** been allocated for pTsdro->useSharedData to be non-zero. |
| 1609 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1610 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
| 1611 | pBt->pNext = pTsdro->pBtree; |
| 1612 | sqlite3ThreadData()->pBtree = pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1613 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1614 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1615 | pBt->nRef = 1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1616 | *ppBtree = p; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1617 | return SQLITE_OK; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | ** Close an open database and invalidate all cursors. |
| 1622 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1623 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1624 | BtShared *pBt = p->pBt; |
| 1625 | BtCursor *pCur; |
| 1626 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1627 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1628 | ThreadData *pTsd; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1631 | /* Close all cursors opened via this handle. */ |
| 1632 | pCur = pBt->pCursor; |
| 1633 | while( pCur ){ |
| 1634 | BtCursor *pTmp = pCur; |
| 1635 | pCur = pCur->pNext; |
| 1636 | if( pTmp->pBtree==p ){ |
| 1637 | sqlite3BtreeCloseCursor(pTmp); |
| 1638 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1639 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1640 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1641 | /* Rollback any active transaction and free the handle structure. |
| 1642 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1643 | ** this handle. |
| 1644 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1645 | sqlite3BtreeRollback(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1646 | sqliteFree(p); |
| 1647 | |
| 1648 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1649 | /* If there are still other outstanding references to the shared-btree |
| 1650 | ** structure, return now. The remainder of this procedure cleans |
| 1651 | ** up the shared-btree. |
| 1652 | */ |
| 1653 | assert( pBt->nRef>0 ); |
| 1654 | pBt->nRef--; |
| 1655 | if( pBt->nRef ){ |
| 1656 | return SQLITE_OK; |
| 1657 | } |
| 1658 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1659 | /* Remove the shared-btree from the thread wide list. Call |
| 1660 | ** ThreadDataReadOnly() and then cast away the const property of the |
| 1661 | ** pointer to avoid allocating thread data if it is not really required. |
| 1662 | */ |
| 1663 | pTsd = (ThreadData *)sqlite3ThreadDataReadOnly(); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1664 | if( pTsd->pBtree==pBt ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1665 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1666 | pTsd->pBtree = pBt->pNext; |
| 1667 | }else{ |
| 1668 | BtShared *pPrev; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1669 | for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1670 | if( pPrev ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1671 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1672 | pPrev->pNext = pBt->pNext; |
| 1673 | } |
| 1674 | } |
| 1675 | #endif |
| 1676 | |
| 1677 | /* Close the pager and free the shared-btree structure */ |
| 1678 | assert( !pBt->pCursor ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1679 | sqlite3pager_close(pBt->pPager); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1680 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1681 | pBt->xFreeSchema(pBt->pSchema); |
| 1682 | } |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 1683 | sqliteFree(pBt->pSchema); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1684 | sqliteFree(pBt); |
| 1685 | return SQLITE_OK; |
| 1686 | } |
| 1687 | |
| 1688 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1689 | ** Change the busy handler callback function. |
| 1690 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1691 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1692 | BtShared *pBt = p->pBt; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1693 | pBt->pBusyHandler = pHandler; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1694 | sqlite3pager_set_busyhandler(pBt->pPager, pHandler); |
| 1695 | return SQLITE_OK; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1699 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1700 | ** |
| 1701 | ** The maximum number of cache pages is set to the absolute |
| 1702 | ** value of mxPage. If mxPage is negative, the pager will |
| 1703 | ** operate asynchronously - it will not stop to do fsync()s |
| 1704 | ** to insure data is written to the disk surface before |
| 1705 | ** continuing. Transactions still work if synchronous is off, |
| 1706 | ** and the database cannot be corrupted if this program |
| 1707 | ** crashes. But if the operating system crashes or there is |
| 1708 | ** an abrupt power failure when synchronous is off, the database |
| 1709 | ** could be left in an inconsistent and unrecoverable state. |
| 1710 | ** Synchronous is on by default so database corruption is not |
| 1711 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1712 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1713 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1714 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1715 | sqlite3pager_set_cachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1716 | return SQLITE_OK; |
| 1717 | } |
| 1718 | |
| 1719 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1720 | ** Change the way data is synced to disk in order to increase or decrease |
| 1721 | ** how well the database resists damage due to OS crashes and power |
| 1722 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1723 | ** there is a high probability of damage) Level 2 is the default. There |
| 1724 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1725 | ** probability of damage to near zero but with a write performance reduction. |
| 1726 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1727 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1728 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1729 | BtShared *pBt = p->pBt; |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1730 | sqlite3pager_set_safety_level(pBt->pPager, level, fullSync); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1731 | return SQLITE_OK; |
| 1732 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1733 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1734 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1735 | /* |
| 1736 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1737 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1738 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1739 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1740 | BtShared *pBt = p->pBt; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1741 | assert( pBt && pBt->pPager ); |
| 1742 | return sqlite3pager_nosync(pBt->pPager); |
| 1743 | } |
| 1744 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1745 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1746 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1747 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1748 | ** |
| 1749 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1750 | ** size supplied does not meet this constraint then the page size is not |
| 1751 | ** changed. |
| 1752 | ** |
| 1753 | ** Page sizes are constrained to be a power of two so that the region |
| 1754 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1755 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1756 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1757 | ** |
| 1758 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1759 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1760 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1761 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
| 1762 | BtShared *pBt = p->pBt; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1763 | if( pBt->pageSizeFixed ){ |
| 1764 | return SQLITE_READONLY; |
| 1765 | } |
| 1766 | if( nReserve<0 ){ |
| 1767 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1768 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1769 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1770 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1771 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1772 | assert( !pBt->pPage1 && !pBt->pCursor ); |
drh | 1c7880e | 2005-05-20 20:01:55 +0000 | [diff] [blame] | 1773 | pBt->pageSize = sqlite3pager_set_pagesize(pBt->pPager, pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1774 | } |
| 1775 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1776 | return SQLITE_OK; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | ** Return the currently defined page size |
| 1781 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1782 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1783 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1784 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1785 | int sqlite3BtreeGetReserve(Btree *p){ |
| 1786 | return p->pBt->pageSize - p->pBt->usableSize; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1787 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1788 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1789 | |
| 1790 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1791 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1792 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1793 | ** is disabled. The default value for the auto-vacuum property is |
| 1794 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1795 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1796 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
| 1797 | BtShared *pBt = p->pBt;; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1798 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1799 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1800 | #else |
| 1801 | if( pBt->pageSizeFixed ){ |
| 1802 | return SQLITE_READONLY; |
| 1803 | } |
| 1804 | pBt->autoVacuum = (autoVacuum?1:0); |
| 1805 | return SQLITE_OK; |
| 1806 | #endif |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1811 | ** enabled 1 is returned. Otherwise 0. |
| 1812 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1813 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1814 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 1815 | return 0; |
| 1816 | #else |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1817 | return p->pBt->autoVacuum; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1818 | #endif |
| 1819 | } |
| 1820 | |
| 1821 | |
| 1822 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1823 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1824 | ** also acquire a readlock on that file. |
| 1825 | ** |
| 1826 | ** SQLITE_OK is returned on success. If the file is not a |
| 1827 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1828 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1829 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1830 | ** if there is a locking protocol violation. |
| 1831 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1832 | static int lockBtree(BtShared *pBt){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1833 | int rc, pageSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1834 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1835 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1836 | rc = getPage(pBt, 1, &pPage1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1837 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1838 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1839 | |
| 1840 | /* Do some checking to help insure the file we opened really is |
| 1841 | ** a valid database file. |
| 1842 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1843 | rc = SQLITE_NOTADB; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1844 | if( sqlite3pager_pagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1845 | u8 *page1 = pPage1->aData; |
| 1846 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1847 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1848 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1849 | if( page1[18]>1 || page1[19]>1 ){ |
| 1850 | goto page1_init_failed; |
| 1851 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1852 | pageSize = get2byte(&page1[16]); |
| 1853 | if( ((pageSize-1)&pageSize)!=0 ){ |
| 1854 | goto page1_init_failed; |
| 1855 | } |
| 1856 | assert( (pageSize & 7)==0 ); |
| 1857 | pBt->pageSize = pageSize; |
| 1858 | pBt->usableSize = pageSize - page1[20]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1859 | if( pBt->usableSize<500 ){ |
| 1860 | goto page1_init_failed; |
| 1861 | } |
| 1862 | pBt->maxEmbedFrac = page1[21]; |
| 1863 | pBt->minEmbedFrac = page1[22]; |
| 1864 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1865 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1866 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
| 1867 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1868 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1869 | |
| 1870 | /* maxLocal is the maximum amount of payload to store locally for |
| 1871 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1872 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1873 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1874 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1875 | ** 4-byte child pointer |
| 1876 | ** 9-byte nKey value |
| 1877 | ** 4-byte nData value |
| 1878 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1879 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1880 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1881 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1882 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1883 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1884 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1885 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1886 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1887 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1888 | goto page1_init_failed; |
| 1889 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1890 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1891 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1892 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1893 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1894 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1895 | releasePage(pPage1); |
| 1896 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1897 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1901 | ** This routine works like lockBtree() except that it also invokes the |
| 1902 | ** busy callback if there is lock contention. |
| 1903 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1904 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1905 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1906 | if( pRef->inTrans==TRANS_NONE ){ |
| 1907 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1908 | btreeIntegrity(pRef); |
| 1909 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1910 | pRef->pBt->inTransaction = inTransaction; |
| 1911 | pRef->inTrans = TRANS_NONE; |
| 1912 | if( rc==SQLITE_OK ){ |
| 1913 | pRef->pBt->nTransaction--; |
| 1914 | } |
| 1915 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1916 | } |
| 1917 | return rc; |
| 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1922 | ** If there are no outstanding cursors and we are not in the middle |
| 1923 | ** of a transaction but there is a read lock on the database, then |
| 1924 | ** this routine unrefs the first page of the database file which |
| 1925 | ** has the effect of releasing the read lock. |
| 1926 | ** |
| 1927 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1928 | ** |
| 1929 | ** If there is a transaction in progress, this routine is a no-op. |
| 1930 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1931 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 1932 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1933 | if( pBt->pPage1->aData==0 ){ |
| 1934 | MemPage *pPage = pBt->pPage1; |
drh | 2646da7 | 2005-12-09 20:02:05 +0000 | [diff] [blame] | 1935 | pPage->aData = &((u8*)pPage)[-pBt->pageSize]; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1936 | pPage->pBt = pBt; |
| 1937 | pPage->pgno = 1; |
| 1938 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1939 | releasePage(pBt->pPage1); |
| 1940 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1941 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1946 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1947 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1948 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1949 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1950 | MemPage *pP1; |
| 1951 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1952 | int rc; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1953 | if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1954 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1955 | assert( pP1!=0 ); |
| 1956 | data = pP1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1957 | rc = sqlite3pager_write(data); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1958 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1959 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1960 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1961 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1962 | data[18] = 1; |
| 1963 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1964 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1965 | data[21] = pBt->maxEmbedFrac; |
| 1966 | data[22] = pBt->minEmbedFrac; |
| 1967 | data[23] = pBt->minLeafFrac; |
| 1968 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1969 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1970 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1971 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1972 | if( pBt->autoVacuum ){ |
| 1973 | put4byte(&data[36 + 4*4], 1); |
| 1974 | } |
| 1975 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1976 | return SQLITE_OK; |
| 1977 | } |
| 1978 | |
| 1979 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1980 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1981 | ** is started if the second argument is nonzero, otherwise a read- |
| 1982 | ** transaction. If the second argument is 2 or more and exclusive |
| 1983 | ** transaction is started, meaning that no other process is allowed |
| 1984 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1985 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1986 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1987 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1988 | ** A write-transaction must be started before attempting any |
| 1989 | ** changes to the database. None of the following routines |
| 1990 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1991 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1992 | ** sqlite3BtreeCreateTable() |
| 1993 | ** sqlite3BtreeCreateIndex() |
| 1994 | ** sqlite3BtreeClearTable() |
| 1995 | ** sqlite3BtreeDropTable() |
| 1996 | ** sqlite3BtreeInsert() |
| 1997 | ** sqlite3BtreeDelete() |
| 1998 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1999 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2000 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2001 | ** and the database was previously unlocked, then invoke the busy handler |
| 2002 | ** if there is one. But if there was previously a read-lock, do not |
| 2003 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2004 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2005 | ** |
| 2006 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2007 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2008 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2009 | ** One or the other of the two processes must give way or there can be |
| 2010 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2011 | ** when A already has a read lock, we encourage A to give up and let B |
| 2012 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2013 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2014 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 2015 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2016 | int rc = SQLITE_OK; |
| 2017 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2018 | btreeIntegrity(p); |
| 2019 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2020 | /* If the btree is already in a write-transaction, or it |
| 2021 | ** is already in a read-transaction and a read-transaction |
| 2022 | ** is requested, this is a no-op. |
| 2023 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2024 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2025 | return SQLITE_OK; |
| 2026 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2027 | |
| 2028 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2029 | if( pBt->readOnly && wrflag ){ |
| 2030 | return SQLITE_READONLY; |
| 2031 | } |
| 2032 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2033 | /* If another database handle has already opened a write transaction |
| 2034 | ** on this shared-btree structure and a second write transaction is |
| 2035 | ** requested, return SQLITE_BUSY. |
| 2036 | */ |
| 2037 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
| 2038 | return SQLITE_BUSY; |
| 2039 | } |
| 2040 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2041 | do { |
| 2042 | if( pBt->pPage1==0 ){ |
| 2043 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2044 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2045 | |
| 2046 | if( rc==SQLITE_OK && wrflag ){ |
| 2047 | rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1); |
| 2048 | if( rc==SQLITE_OK ){ |
| 2049 | rc = newDatabase(pBt); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2054 | if( wrflag ) pBt->inStmt = 0; |
| 2055 | }else{ |
| 2056 | unlockBtreeIfUnused(pBt); |
| 2057 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2058 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2059 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2060 | |
| 2061 | if( rc==SQLITE_OK ){ |
| 2062 | if( p->inTrans==TRANS_NONE ){ |
| 2063 | pBt->nTransaction++; |
| 2064 | } |
| 2065 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2066 | if( p->inTrans>pBt->inTransaction ){ |
| 2067 | pBt->inTransaction = p->inTrans; |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | btreeIntegrity(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2072 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2075 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2076 | |
| 2077 | /* |
| 2078 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2079 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2080 | ** map entries for the overflow pages as well. |
| 2081 | */ |
| 2082 | static int setChildPtrmaps(MemPage *pPage){ |
| 2083 | int i; /* Counter variable */ |
| 2084 | int nCell; /* Number of cells in page pPage */ |
| 2085 | int rc = SQLITE_OK; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2086 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2087 | int isInitOrig = pPage->isInit; |
| 2088 | Pgno pgno = pPage->pgno; |
| 2089 | |
| 2090 | initPage(pPage, 0); |
| 2091 | nCell = pPage->nCell; |
| 2092 | |
| 2093 | for(i=0; i<nCell; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2094 | u8 *pCell = findCell(pPage, i); |
| 2095 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2096 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2097 | if( rc!=SQLITE_OK ){ |
| 2098 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2099 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2100 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2101 | if( !pPage->leaf ){ |
| 2102 | Pgno childPgno = get4byte(pCell); |
| 2103 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2104 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | if( !pPage->leaf ){ |
| 2109 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2110 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2111 | } |
| 2112 | |
| 2113 | set_child_ptrmaps_out: |
| 2114 | pPage->isInit = isInitOrig; |
| 2115 | return rc; |
| 2116 | } |
| 2117 | |
| 2118 | /* |
| 2119 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2120 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2121 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2122 | ** follows: |
| 2123 | ** |
| 2124 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2125 | ** page of pPage. |
| 2126 | ** |
| 2127 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2128 | ** page pointed to by one of the cells on pPage. |
| 2129 | ** |
| 2130 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2131 | ** overflow page in the list. |
| 2132 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2133 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2134 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2135 | /* 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] | 2136 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2137 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2138 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2139 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2140 | }else{ |
| 2141 | int isInitOrig = pPage->isInit; |
| 2142 | int i; |
| 2143 | int nCell; |
| 2144 | |
| 2145 | initPage(pPage, 0); |
| 2146 | nCell = pPage->nCell; |
| 2147 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2148 | for(i=0; i<nCell; i++){ |
| 2149 | u8 *pCell = findCell(pPage, i); |
| 2150 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2151 | CellInfo info; |
| 2152 | parseCellPtr(pPage, pCell, &info); |
| 2153 | if( info.iOverflow ){ |
| 2154 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2155 | put4byte(&pCell[info.iOverflow], iTo); |
| 2156 | break; |
| 2157 | } |
| 2158 | } |
| 2159 | }else{ |
| 2160 | if( get4byte(pCell)==iFrom ){ |
| 2161 | put4byte(pCell, iTo); |
| 2162 | break; |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2168 | if( eType!=PTRMAP_BTREE || |
| 2169 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2170 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2171 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2172 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2173 | } |
| 2174 | |
| 2175 | pPage->isInit = isInitOrig; |
| 2176 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2177 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2180 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2181 | /* |
| 2182 | ** Move the open database page pDbPage to location iFreePage in the |
| 2183 | ** database. The pDbPage reference remains valid. |
| 2184 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2185 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2186 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2187 | MemPage *pDbPage, /* Open page to move */ |
| 2188 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2189 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 2190 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2191 | ){ |
| 2192 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2193 | Pgno iDbPage = pDbPage->pgno; |
| 2194 | Pager *pPager = pBt->pPager; |
| 2195 | int rc; |
| 2196 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2197 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2198 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2199 | |
| 2200 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 2201 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2202 | iDbPage, iFreePage, iPtrPage, eType)); |
| 2203 | rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage); |
| 2204 | if( rc!=SQLITE_OK ){ |
| 2205 | return rc; |
| 2206 | } |
| 2207 | pDbPage->pgno = iFreePage; |
| 2208 | |
| 2209 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2210 | ** that point to overflow pages. The pointer map entries for all these |
| 2211 | ** pages need to be changed. |
| 2212 | ** |
| 2213 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2214 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2215 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2216 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2217 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2218 | rc = setChildPtrmaps(pDbPage); |
| 2219 | if( rc!=SQLITE_OK ){ |
| 2220 | return rc; |
| 2221 | } |
| 2222 | }else{ |
| 2223 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2224 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2225 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2226 | if( rc!=SQLITE_OK ){ |
| 2227 | return rc; |
| 2228 | } |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2233 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2234 | ** iPtrPage. |
| 2235 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2236 | if( eType!=PTRMAP_ROOTPAGE ){ |
| 2237 | rc = getPage(pBt, iPtrPage, &pPtrPage); |
| 2238 | if( rc!=SQLITE_OK ){ |
| 2239 | return rc; |
| 2240 | } |
| 2241 | rc = sqlite3pager_write(pPtrPage->aData); |
| 2242 | if( rc!=SQLITE_OK ){ |
| 2243 | releasePage(pPtrPage); |
| 2244 | return rc; |
| 2245 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2246 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2247 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2248 | if( rc==SQLITE_OK ){ |
| 2249 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2250 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2251 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2252 | return rc; |
| 2253 | } |
| 2254 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2255 | /* Forward declaration required by autoVacuumCommit(). */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2256 | static int allocatePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2257 | |
| 2258 | /* |
| 2259 | ** This routine is called prior to sqlite3pager_commit when a transaction |
| 2260 | ** is commited for an auto-vacuum database. |
| 2261 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2262 | static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2263 | Pager *pPager = pBt->pPager; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2264 | Pgno nFreeList; /* Number of pages remaining on the free-list. */ |
| 2265 | int nPtrMap; /* Number of pointer-map pages deallocated */ |
| 2266 | Pgno origSize; /* Pages in the database file */ |
| 2267 | Pgno finSize; /* Pages in the database file after truncation */ |
| 2268 | int rc; /* Return code */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2269 | u8 eType; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2270 | int pgsz = pBt->pageSize; /* Page size for this database */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2271 | Pgno iDbPage; /* The database page to move */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2272 | MemPage *pDbMemPage = 0; /* "" */ |
| 2273 | Pgno iPtrPage; /* The page that contains a pointer to iDbPage */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2274 | Pgno iFreePage; /* The free-list page to move iDbPage to */ |
| 2275 | MemPage *pFreeMemPage = 0; /* "" */ |
| 2276 | |
| 2277 | #ifndef NDEBUG |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2278 | int nRef = sqlite3pager_refcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2279 | #endif |
| 2280 | |
| 2281 | assert( pBt->autoVacuum ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2282 | if( PTRMAP_ISPAGE(pBt, sqlite3pager_pagecount(pPager)) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2283 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2284 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2285 | |
| 2286 | /* Figure out how many free-pages are in the database. If there are no |
| 2287 | ** free pages, then auto-vacuum is a no-op. |
| 2288 | */ |
| 2289 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2290 | if( nFreeList==0 ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 2291 | *nTrunc = 0; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2292 | return SQLITE_OK; |
| 2293 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2294 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2295 | /* This block figures out how many pages there are in the database |
| 2296 | ** now (variable origSize), and how many there will be after the |
| 2297 | ** truncation (variable finSize). |
| 2298 | ** |
| 2299 | ** The final size is the original size, less the number of free pages |
| 2300 | ** in the database, less any pointer-map pages that will no longer |
| 2301 | ** be required, less 1 if the pending-byte page was part of the database |
| 2302 | ** but is not after the truncation. |
| 2303 | **/ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2304 | origSize = sqlite3pager_pagecount(pPager); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2305 | if( origSize==PENDING_BYTE_PAGE(pBt) ){ |
| 2306 | origSize--; |
| 2307 | } |
| 2308 | nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pBt, origSize)+pgsz/5)/(pgsz/5); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2309 | finSize = origSize - nFreeList - nPtrMap; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2310 | if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2311 | finSize--; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2312 | } |
| 2313 | while( PTRMAP_ISPAGE(pBt, finSize) || finSize==PENDING_BYTE_PAGE(pBt) ){ |
| 2314 | finSize--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2315 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2316 | TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize)); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2317 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2318 | /* Variable 'finSize' will be the size of the file in pages after |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2319 | ** the auto-vacuum has completed (the current file size minus the number |
| 2320 | ** of pages on the free list). Loop through the pages that lie beyond |
| 2321 | ** 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] | 2322 | ** to a free page earlier in the file (somewhere before finSize). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2323 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2324 | for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2325 | /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2326 | if( PTRMAP_ISPAGE(pBt, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2327 | continue; |
| 2328 | } |
| 2329 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2330 | rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage); |
| 2331 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 2332 | if( eType==PTRMAP_ROOTPAGE ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2333 | rc = SQLITE_CORRUPT_BKPT; |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 2334 | goto autovacuum_out; |
| 2335 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2336 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2337 | /* If iDbPage is free, do not swap it. */ |
| 2338 | if( eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2339 | continue; |
| 2340 | } |
| 2341 | rc = getPage(pBt, iDbPage, &pDbMemPage); |
| 2342 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2343 | |
| 2344 | /* Find the next page in the free-list that is not already at the end |
| 2345 | ** of the file. A page can be pulled off the free list using the |
| 2346 | ** allocatePage() routine. |
| 2347 | */ |
| 2348 | do{ |
| 2349 | if( pFreeMemPage ){ |
| 2350 | releasePage(pFreeMemPage); |
| 2351 | pFreeMemPage = 0; |
| 2352 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2353 | rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2354 | if( rc!=SQLITE_OK ){ |
| 2355 | releasePage(pDbMemPage); |
| 2356 | goto autovacuum_out; |
| 2357 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2358 | assert( iFreePage<=origSize ); |
| 2359 | }while( iFreePage>finSize ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2360 | releasePage(pFreeMemPage); |
| 2361 | pFreeMemPage = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2362 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2363 | /* Relocate the page into the body of the file. Note that although the |
| 2364 | ** page has moved within the database file, the pDbMemPage pointer |
| 2365 | ** remains valid. This means that this function can run without |
| 2366 | ** invalidating cursors open on the btree. This is important in |
| 2367 | ** shared-cache mode. |
| 2368 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2369 | rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2370 | releasePage(pDbMemPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2371 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /* 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] | 2375 | ** truncate the database file to finSize pages and consider the |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2376 | ** free-list empty. |
| 2377 | */ |
| 2378 | rc = sqlite3pager_write(pBt->pPage1->aData); |
| 2379 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 2380 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2381 | put4byte(&pBt->pPage1->aData[36], 0); |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 2382 | *nTrunc = finSize; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2383 | assert( finSize!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2384 | |
| 2385 | autovacuum_out: |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 2386 | assert( nRef==sqlite3pager_refcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2387 | if( rc!=SQLITE_OK ){ |
| 2388 | sqlite3pager_rollback(pPager); |
| 2389 | } |
| 2390 | return rc; |
| 2391 | } |
| 2392 | #endif |
| 2393 | |
| 2394 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2395 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2396 | ** |
| 2397 | ** This will release the write lock on the database file. If there |
| 2398 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2399 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2400 | int sqlite3BtreeCommit(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2401 | BtShared *pBt = p->pBt; |
| 2402 | |
| 2403 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2404 | |
| 2405 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2406 | ** transaction and set the shared state to TRANS_READ. |
| 2407 | */ |
| 2408 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2409 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2410 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2411 | assert( pBt->nTransaction>0 ); |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2412 | rc = sqlite3pager_commit(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2413 | if( rc!=SQLITE_OK ){ |
| 2414 | return rc; |
| 2415 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2416 | pBt->inTransaction = TRANS_READ; |
| 2417 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2418 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2419 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2422 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2423 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2424 | ** will unlock the pager. |
| 2425 | */ |
| 2426 | if( p->inTrans!=TRANS_NONE ){ |
| 2427 | pBt->nTransaction--; |
| 2428 | if( 0==pBt->nTransaction ){ |
| 2429 | pBt->inTransaction = TRANS_NONE; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2434 | ** the pager if this call closed the only read or write transaction. |
| 2435 | */ |
| 2436 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2437 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2438 | |
| 2439 | btreeIntegrity(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2440 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2443 | #ifndef NDEBUG |
| 2444 | /* |
| 2445 | ** Return the number of write-cursors open on this handle. This is for use |
| 2446 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2447 | ** defined. |
| 2448 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2449 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2450 | BtCursor *pCur; |
| 2451 | int r = 0; |
| 2452 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2453 | if( pCur->wrFlag ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2454 | } |
| 2455 | return r; |
| 2456 | } |
| 2457 | #endif |
| 2458 | |
drh | 77bba59 | 2006-08-13 18:39:26 +0000 | [diff] [blame] | 2459 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | ** Print debugging information about all cursors to standard output. |
| 2462 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2463 | void sqlite3BtreeCursorList(Btree *p){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2464 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2465 | BtShared *pBt = p->pBt; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2466 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 2467 | MemPage *pPage = pCur->pPage; |
| 2468 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 2469 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 2470 | pCur, pCur->pgnoRoot, zMode, |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2471 | pPage ? pPage->pgno : 0, pCur->idx, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2472 | (pCur->eState==CURSOR_VALID) ? "" : " eof" |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2473 | ); |
| 2474 | } |
| 2475 | } |
| 2476 | #endif |
| 2477 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2478 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2479 | ** Rollback the transaction in progress. All cursors will be |
| 2480 | ** invalided by this operation. Any attempt to use a cursor |
| 2481 | ** that was open at the beginning of this operation will result |
| 2482 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2483 | ** |
| 2484 | ** This will release the write lock on the database file. If there |
| 2485 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2486 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2487 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2488 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2489 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2490 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2491 | |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2492 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2493 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2494 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2495 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2496 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2497 | ** the result of a constraint, malloc() failure or IO error) then |
| 2498 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2499 | ** we cannot simply return the error to the caller. Instead, abort |
| 2500 | ** all queries that may be using any of the cursors that failed to save. |
| 2501 | */ |
| 2502 | while( pBt->pCursor ){ |
| 2503 | sqlite3 *db = pBt->pCursor->pBtree->pSqlite; |
| 2504 | if( db ){ |
| 2505 | sqlite3AbortOtherActiveVdbes(db, 0); |
| 2506 | } |
| 2507 | } |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2508 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2509 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2510 | btreeIntegrity(p); |
| 2511 | unlockAllTables(p); |
| 2512 | |
| 2513 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2514 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2515 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2516 | assert( TRANS_WRITE==pBt->inTransaction ); |
| 2517 | rc2 = sqlite3pager_rollback(pBt->pPager); |
| 2518 | if( rc2!=SQLITE_OK ){ |
| 2519 | rc = rc2; |
| 2520 | } |
| 2521 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2522 | /* The rollback may have destroyed the pPage1->aData value. So |
| 2523 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 2524 | ** set correctly. */ |
| 2525 | if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){ |
| 2526 | releasePage(pPage1); |
| 2527 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2528 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2529 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2530 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2531 | |
| 2532 | if( p->inTrans!=TRANS_NONE ){ |
| 2533 | assert( pBt->nTransaction>0 ); |
| 2534 | pBt->nTransaction--; |
| 2535 | if( 0==pBt->nTransaction ){ |
| 2536 | pBt->inTransaction = TRANS_NONE; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2541 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2542 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2543 | |
| 2544 | btreeIntegrity(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2545 | return rc; |
| 2546 | } |
| 2547 | |
| 2548 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2549 | ** Start a statement subtransaction. The subtransaction can |
| 2550 | ** can be rolled back independently of the main transaction. |
| 2551 | ** You must start a transaction before starting a subtransaction. |
| 2552 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2553 | ** commits or rolls back. |
| 2554 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2555 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2556 | ** to start a new subtransaction if another subtransaction is already active. |
| 2557 | ** |
| 2558 | ** Statement subtransactions are used around individual SQL statements |
| 2559 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2560 | ** error occurs within the statement, the effect of that one statement |
| 2561 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2562 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2563 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2564 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2565 | BtShared *pBt = p->pBt; |
| 2566 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2567 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2568 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2569 | assert( pBt->inTransaction==TRANS_WRITE ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2570 | rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2571 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2572 | return rc; |
| 2573 | } |
| 2574 | |
| 2575 | |
| 2576 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2577 | ** Commit the statment subtransaction currently in progress. If no |
| 2578 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2579 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2580 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2581 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2582 | BtShared *pBt = p->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2583 | if( pBt->inStmt && !pBt->readOnly ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2584 | rc = sqlite3pager_stmt_commit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2585 | }else{ |
| 2586 | rc = SQLITE_OK; |
| 2587 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2588 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2589 | return rc; |
| 2590 | } |
| 2591 | |
| 2592 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2593 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2594 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2595 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2596 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2597 | ** to use a cursor that was open at the beginning of this operation |
| 2598 | ** will result in an error. |
| 2599 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2600 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2601 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2602 | BtShared *pBt = p->pBt; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2603 | sqlite3MallocDisallow(); |
| 2604 | if( pBt->inStmt && !pBt->readOnly ){ |
| 2605 | rc = sqlite3pager_stmt_rollback(pBt->pPager); |
| 2606 | assert( countWriteCursors(pBt)==0 ); |
| 2607 | pBt->inStmt = 0; |
| 2608 | } |
| 2609 | sqlite3MallocAllow(); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2610 | return rc; |
| 2611 | } |
| 2612 | |
| 2613 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2614 | ** Default key comparison function to be used if no comparison function |
| 2615 | ** is specified on the sqlite3BtreeCursor() call. |
| 2616 | */ |
| 2617 | static int dfltCompare( |
| 2618 | void *NotUsed, /* User data is not used */ |
| 2619 | int n1, const void *p1, /* First key to compare */ |
| 2620 | int n2, const void *p2 /* Second key to compare */ |
| 2621 | ){ |
| 2622 | int c; |
| 2623 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2624 | if( c==0 ){ |
| 2625 | c = n1 - n2; |
| 2626 | } |
| 2627 | return c; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2631 | ** Create a new cursor for the BTree whose root is on the page |
| 2632 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2633 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2634 | ** |
| 2635 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2636 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2637 | ** writing if other conditions for writing are also met. These |
| 2638 | ** are the conditions that must be met in order for writing to |
| 2639 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2640 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2641 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2642 | ** |
| 2643 | ** 2: No other cursors may be open with wrFlag==0 on the same table |
| 2644 | ** |
| 2645 | ** 3: The database must be writable (not on read-only media) |
| 2646 | ** |
| 2647 | ** 4: There must be an active transaction. |
| 2648 | ** |
| 2649 | ** Condition 2 warrants further discussion. If any cursor is opened |
| 2650 | ** on a table with wrFlag==0, that prevents all other cursors from |
| 2651 | ** writing to that table. This is a kind of "read-lock". When a cursor |
| 2652 | ** is opened with wrFlag==0 it is guaranteed that the table will not |
| 2653 | ** change as long as the cursor is open. This allows the cursor to |
| 2654 | ** do a sequential scan of the table without having to worry about |
| 2655 | ** entries being inserted or deleted during the scan. Cursors should |
| 2656 | ** be opened with wrFlag==0 only if this read-lock property is needed. |
| 2657 | ** 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] | 2658 | ** intend to use the sqlite3BtreeNext() system call. All other cursors |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2659 | ** should be opened with wrFlag==1 even if they never really intend |
| 2660 | ** to write. |
| 2661 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2662 | ** No checking is done to make sure that page iTable really is the |
| 2663 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2664 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2665 | ** |
| 2666 | ** The comparison function must be logically the same for every cursor |
| 2667 | ** on a particular table. Changing the comparison function will result |
| 2668 | ** in incorrect operations. If the comparison function is NULL, a |
| 2669 | ** default comparison function is used. The comparison function is |
| 2670 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2671 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2672 | int sqlite3BtreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2673 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2674 | int iTable, /* Root page of table to open */ |
| 2675 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2676 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2677 | void *pArg, /* First arg to xCompare() */ |
| 2678 | BtCursor **ppCur /* Write new cursor here */ |
| 2679 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2680 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2681 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2682 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2683 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2684 | *ppCur = 0; |
| 2685 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2686 | if( pBt->readOnly ){ |
| 2687 | return SQLITE_READONLY; |
| 2688 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2689 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2690 | return SQLITE_LOCKED; |
| 2691 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2692 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2693 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2694 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2695 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2696 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2697 | return rc; |
| 2698 | } |
| 2699 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2700 | pCur = sqliteMalloc( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2701 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2702 | rc = SQLITE_NOMEM; |
| 2703 | goto create_cursor_exception; |
| 2704 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2705 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2706 | if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){ |
| 2707 | rc = SQLITE_EMPTY; |
| 2708 | goto create_cursor_exception; |
| 2709 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2710 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2711 | if( rc!=SQLITE_OK ){ |
| 2712 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2713 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2714 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2715 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2716 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2717 | ** output argument to this function). |
| 2718 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2719 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2720 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2721 | pCur->pBtree = p; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2722 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2723 | pCur->pNext = pBt->pCursor; |
| 2724 | if( pCur->pNext ){ |
| 2725 | pCur->pNext->pPrev = pCur; |
| 2726 | } |
| 2727 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2728 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2729 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2730 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2731 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2732 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2733 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2734 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2735 | sqliteFree(pCur); |
| 2736 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2737 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2738 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2741 | #if 0 /* Not Used */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2742 | /* |
| 2743 | ** Change the value of the comparison function used by a cursor. |
| 2744 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2745 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2746 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 2747 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 2748 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2749 | ){ |
| 2750 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2751 | pCur->pArg = pArg; |
| 2752 | } |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2753 | #endif |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2754 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2755 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2756 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2757 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2758 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2759 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2760 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2761 | restoreOrClearCursorPosition(pCur, 0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2762 | if( pCur->pPrev ){ |
| 2763 | pCur->pPrev->pNext = pCur->pNext; |
| 2764 | }else{ |
| 2765 | pBt->pCursor = pCur->pNext; |
| 2766 | } |
| 2767 | if( pCur->pNext ){ |
| 2768 | pCur->pNext->pPrev = pCur->pPrev; |
| 2769 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2770 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2771 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2772 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2773 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2776 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2777 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2778 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2779 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2780 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2781 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2782 | pTempCur->pNext = 0; |
| 2783 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2784 | if( pTempCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2785 | sqlite3pager_ref(pTempCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2786 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2790 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2791 | ** function above. |
| 2792 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2793 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2794 | if( pCur->pPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2795 | sqlite3pager_unref(pCur->pPage->aData); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2796 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2800 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2801 | ** If it is not already valid, call parseCell() to fill it in. |
| 2802 | ** |
| 2803 | ** BtCursor.info is a cache of the information in the current cell. |
| 2804 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2805 | */ |
| 2806 | static void getCellInfo(BtCursor *pCur){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2807 | if( pCur->info.nSize==0 ){ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2808 | parseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2809 | }else{ |
| 2810 | #ifndef NDEBUG |
| 2811 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2812 | memset(&info, 0, sizeof(info)); |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2813 | parseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2814 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 2815 | #endif |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2820 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2821 | ** the key for the current entry. If the cursor is not pointing |
| 2822 | ** to a valid entry, *pSize is set to 0. |
| 2823 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2824 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2825 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2826 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2827 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2828 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2829 | if( rc==SQLITE_OK ){ |
| 2830 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2831 | if( pCur->eState==CURSOR_INVALID ){ |
| 2832 | *pSize = 0; |
| 2833 | }else{ |
| 2834 | getCellInfo(pCur); |
| 2835 | *pSize = pCur->info.nKey; |
| 2836 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2837 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2838 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2839 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2840 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2841 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2842 | ** Set *pSize to the number of bytes of data in the entry the |
| 2843 | ** cursor currently points to. Always return SQLITE_OK. |
| 2844 | ** Failure is not possible. If the cursor is not currently |
| 2845 | ** pointing to an entry (which can happen, for example, if |
| 2846 | ** the database is empty) then *pSize is set to 0. |
| 2847 | */ |
| 2848 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2849 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2850 | if( rc==SQLITE_OK ){ |
| 2851 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2852 | if( pCur->eState==CURSOR_INVALID ){ |
| 2853 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2854 | *pSize = 0; |
| 2855 | }else{ |
| 2856 | getCellInfo(pCur); |
| 2857 | *pSize = pCur->info.nData; |
| 2858 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2859 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2860 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2864 | ** Read payload information from the entry that the pCur cursor is |
| 2865 | ** pointing to. Begin reading the payload at "offset" and read |
| 2866 | ** a total of "amt" bytes. Put the result in zBuf. |
| 2867 | ** |
| 2868 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2869 | ** It just reads bytes from the payload area. Data might appear |
| 2870 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2871 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2872 | static int getPayload( |
| 2873 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2874 | int offset, /* Begin reading this far into payload */ |
| 2875 | int amt, /* Read this many bytes */ |
| 2876 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 2877 | int skipKey /* offset begins at data if this is true */ |
| 2878 | ){ |
| 2879 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2880 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2881 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2882 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2883 | BtShared *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2884 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2885 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2886 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2887 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2888 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2889 | pBt = pCur->pBtree->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2890 | pPage = pCur->pPage; |
| 2891 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2892 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 2893 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2894 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2895 | nKey = 0; |
| 2896 | }else{ |
| 2897 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2898 | } |
| 2899 | assert( offset>=0 ); |
| 2900 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2901 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2902 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2903 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2904 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2905 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2906 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2907 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2908 | if( a+offset>pCur->info.nLocal ){ |
| 2909 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2910 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2911 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2912 | if( a==amt ){ |
| 2913 | return SQLITE_OK; |
| 2914 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2915 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2916 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2917 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2918 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2919 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2920 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2921 | ovflSize = pBt->usableSize - 4; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2922 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2923 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2924 | while( amt>0 && nextPage ){ |
| 2925 | rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload); |
| 2926 | if( rc!=0 ){ |
| 2927 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2928 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2929 | nextPage = get4byte(aPayload); |
| 2930 | if( offset<ovflSize ){ |
| 2931 | int a = amt; |
| 2932 | if( a + offset > ovflSize ){ |
| 2933 | a = ovflSize - offset; |
| 2934 | } |
| 2935 | memcpy(pBuf, &aPayload[offset+4], a); |
| 2936 | offset = 0; |
| 2937 | amt -= a; |
| 2938 | pBuf += a; |
| 2939 | }else{ |
| 2940 | offset -= ovflSize; |
| 2941 | } |
| 2942 | sqlite3pager_unref(aPayload); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2943 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2944 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2945 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2946 | if( amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2947 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2948 | } |
| 2949 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2952 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2953 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2954 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2955 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2956 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2957 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2958 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2959 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2960 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2961 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2962 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2963 | if( rc==SQLITE_OK ){ |
| 2964 | assert( pCur->eState==CURSOR_VALID ); |
| 2965 | assert( pCur->pPage!=0 ); |
| 2966 | if( pCur->pPage->intKey ){ |
| 2967 | return SQLITE_CORRUPT_BKPT; |
| 2968 | } |
| 2969 | assert( pCur->pPage->intKey==0 ); |
| 2970 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
| 2971 | rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 2972 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2973 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2977 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2978 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2979 | ** begins at "offset". |
| 2980 | ** |
| 2981 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2982 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2983 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2984 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2985 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2986 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2987 | if( rc==SQLITE_OK ){ |
| 2988 | assert( pCur->eState==CURSOR_VALID ); |
| 2989 | assert( pCur->pPage!=0 ); |
| 2990 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
| 2991 | rc = getPayload(pCur, offset, amt, pBuf, 1); |
| 2992 | } |
| 2993 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2996 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2997 | ** Return a pointer to payload information from the entry that the |
| 2998 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 2999 | ** 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] | 3000 | ** skipKey==1. The number of bytes of available key/data is written |
| 3001 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3002 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3003 | ** |
| 3004 | ** This routine is an optimization. It is common for the entire key |
| 3005 | ** and data to fit on the local page and for there to be no overflow |
| 3006 | ** pages. When that is so, this routine can be used to access the |
| 3007 | ** key and data without making a copy. If the key and/or data spills |
| 3008 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 3009 | ** the key/data and copy it into a preallocated buffer. |
| 3010 | ** |
| 3011 | ** The pointer returned by this routine looks directly into the cached |
| 3012 | ** page of the database. The data might change or move the next time |
| 3013 | ** any btree routine is called. |
| 3014 | */ |
| 3015 | static const unsigned char *fetchPayload( |
| 3016 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3017 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3018 | int skipKey /* read beginning at data if this is true */ |
| 3019 | ){ |
| 3020 | unsigned char *aPayload; |
| 3021 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3022 | u32 nKey; |
| 3023 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3024 | |
| 3025 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3026 | assert( pCur->eState==CURSOR_VALID ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3027 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3028 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3029 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3030 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3031 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3032 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3033 | nKey = 0; |
| 3034 | }else{ |
| 3035 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3036 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3037 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3038 | aPayload += nKey; |
| 3039 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3040 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3041 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3042 | if( nLocal>nKey ){ |
| 3043 | nLocal = nKey; |
| 3044 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3045 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3046 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3047 | return aPayload; |
| 3048 | } |
| 3049 | |
| 3050 | |
| 3051 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3052 | ** For the entry that cursor pCur is point to, return as |
| 3053 | ** many bytes of the key or data as are available on the local |
| 3054 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3055 | ** |
| 3056 | ** The pointer returned is ephemeral. The key/data may move |
| 3057 | ** or be destroyed on the next call to any Btree routine. |
| 3058 | ** |
| 3059 | ** These routines is used to get quick access to key and data |
| 3060 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3061 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3062 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3063 | if( pCur->eState==CURSOR_VALID ){ |
| 3064 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3065 | } |
| 3066 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3067 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3068 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3069 | if( pCur->eState==CURSOR_VALID ){ |
| 3070 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3071 | } |
| 3072 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | |
| 3076 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3077 | ** 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] | 3078 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3079 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3080 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3081 | int rc; |
| 3082 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3083 | MemPage *pOldPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3084 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3085 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3086 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3087 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3088 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3089 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3090 | pOldPage = pCur->pPage; |
| 3091 | pOldPage->idxShift = 0; |
| 3092 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3093 | pCur->pPage = pNewPage; |
| 3094 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3095 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3096 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3097 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3098 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3099 | return SQLITE_OK; |
| 3100 | } |
| 3101 | |
| 3102 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3103 | ** Return true if the page is the virtual root of its table. |
| 3104 | ** |
| 3105 | ** The virtual root page is the root page for most tables. But |
| 3106 | ** for the table rooted on page 1, sometime the real root page |
| 3107 | ** is empty except for the right-pointer. In such cases the |
| 3108 | ** virtual root page is the page that the right-pointer of page |
| 3109 | ** 1 is pointing to. |
| 3110 | */ |
| 3111 | static int isRootPage(MemPage *pPage){ |
| 3112 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3113 | if( pParent==0 ) return 1; |
| 3114 | if( pParent->pgno>1 ) return 0; |
| 3115 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3116 | return 0; |
| 3117 | } |
| 3118 | |
| 3119 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3120 | ** Move the cursor up to the parent page. |
| 3121 | ** |
| 3122 | ** pCur->idx is set to the cell index that contains the pointer |
| 3123 | ** to the page we are coming from. If we are coming from the |
| 3124 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3125 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3126 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3127 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3128 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3129 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3130 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3131 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3132 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3133 | pPage = pCur->pPage; |
| 3134 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3135 | assert( !isRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3136 | pParent = pPage->pParent; |
| 3137 | assert( pParent!=0 ); |
| 3138 | idxParent = pPage->idxParent; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3139 | sqlite3pager_ref(pParent->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3140 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3141 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3142 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3143 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3144 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | /* |
| 3148 | ** Move the cursor to the root page |
| 3149 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3150 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3151 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3152 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3153 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3154 | |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3155 | restoreOrClearCursorPosition(pCur, 0); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3156 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3157 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3158 | assert( pRoot->isInit ); |
| 3159 | }else{ |
| 3160 | if( |
| 3161 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3162 | ){ |
| 3163 | pCur->eState = CURSOR_INVALID; |
| 3164 | return rc; |
| 3165 | } |
| 3166 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3167 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3168 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3169 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3170 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3171 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3172 | Pgno subpage; |
| 3173 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3174 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3175 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3176 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3177 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3178 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3179 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3180 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3181 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3182 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3183 | /* |
| 3184 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3185 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3186 | ** |
| 3187 | ** The left-most leaf is the one with the smallest key - the first |
| 3188 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3189 | */ |
| 3190 | static int moveToLeftmost(BtCursor *pCur){ |
| 3191 | Pgno pgno; |
| 3192 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3193 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3194 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3195 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3196 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3197 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3198 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3199 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3200 | if( rc ) return rc; |
| 3201 | } |
| 3202 | return SQLITE_OK; |
| 3203 | } |
| 3204 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3205 | /* |
| 3206 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3207 | ** page to which it is currently pointing. Notice the difference |
| 3208 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3209 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3210 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3211 | ** |
| 3212 | ** The right-most entry is the one with the largest key - the last |
| 3213 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3214 | */ |
| 3215 | static int moveToRightmost(BtCursor *pCur){ |
| 3216 | Pgno pgno; |
| 3217 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3218 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3219 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3220 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3221 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3222 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3223 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3224 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3225 | if( rc ) return rc; |
| 3226 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3227 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3228 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3229 | return SQLITE_OK; |
| 3230 | } |
| 3231 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3232 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3233 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3234 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3235 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3236 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3237 | int rc; |
| 3238 | rc = moveToRoot(pCur); |
| 3239 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3240 | if( pCur->eState==CURSOR_INVALID ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3241 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3242 | *pRes = 1; |
| 3243 | return SQLITE_OK; |
| 3244 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3245 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3246 | *pRes = 0; |
| 3247 | rc = moveToLeftmost(pCur); |
| 3248 | return rc; |
| 3249 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3250 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3251 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3252 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3253 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3254 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3255 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3256 | int rc; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3257 | rc = moveToRoot(pCur); |
| 3258 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3259 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3260 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3261 | *pRes = 1; |
| 3262 | return SQLITE_OK; |
| 3263 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3264 | assert( pCur->eState==CURSOR_VALID ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3265 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3266 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3267 | return rc; |
| 3268 | } |
| 3269 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3270 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3271 | ** Return a success code. |
| 3272 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3273 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3274 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3275 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3276 | ** created is used to compare keys. |
| 3277 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3278 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3279 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3280 | ** were present. The cursor might point to an entry that comes |
| 3281 | ** before or after the key. |
| 3282 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3283 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3284 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3285 | ** this value is as follows: |
| 3286 | ** |
| 3287 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3288 | ** is smaller than pKey or if the table is empty |
| 3289 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3290 | ** |
| 3291 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3292 | ** exactly matches pKey. |
| 3293 | ** |
| 3294 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3295 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3296 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3297 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3298 | int rc; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3299 | int tryRightmost; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3300 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3301 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3302 | assert( pCur->pPage ); |
| 3303 | assert( pCur->pPage->isInit ); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3304 | tryRightmost = pCur->pPage->intKey; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3305 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3306 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3307 | assert( pCur->pPage->nCell==0 ); |
| 3308 | return SQLITE_OK; |
| 3309 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame^] | 3310 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3311 | int lwr, upr; |
| 3312 | Pgno chldPg; |
| 3313 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3314 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3315 | lwr = 0; |
| 3316 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3317 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3318 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3319 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3320 | while( lwr<=upr ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3321 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3322 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3323 | pCur->idx = (lwr+upr)/2; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3324 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3325 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3326 | u8 *pCell; |
| 3327 | if( tryRightmost ){ |
| 3328 | pCur->idx = upr; |
| 3329 | } |
| 3330 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3331 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3332 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3333 | pCell += getVarint32(pCell, &dummy); |
| 3334 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3335 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3336 | if( nCellKey<nKey ){ |
| 3337 | c = -1; |
| 3338 | }else if( nCellKey>nKey ){ |
| 3339 | c = +1; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3340 | tryRightmost = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3341 | }else{ |
| 3342 | c = 0; |
| 3343 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3344 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3345 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3346 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3347 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3348 | if( available>=nCellKey ){ |
| 3349 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3350 | }else{ |
| 3351 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 3352 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3353 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3354 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3355 | sqliteFree(pCellKey); |
| 3356 | if( rc ) return rc; |
| 3357 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3358 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3359 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3360 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3361 | lwr = pCur->idx; |
| 3362 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3363 | break; |
| 3364 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3365 | if( pRes ) *pRes = 0; |
| 3366 | return SQLITE_OK; |
| 3367 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3368 | } |
| 3369 | if( c<0 ){ |
| 3370 | lwr = pCur->idx+1; |
| 3371 | }else{ |
| 3372 | upr = pCur->idx-1; |
| 3373 | } |
| 3374 | } |
| 3375 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3376 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3377 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3378 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3379 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3380 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3381 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3382 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3383 | } |
| 3384 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3385 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3386 | if( pRes ) *pRes = c; |
| 3387 | return SQLITE_OK; |
| 3388 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3389 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3390 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3391 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3392 | if( rc ){ |
| 3393 | return rc; |
| 3394 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3395 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3396 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3400 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3401 | ** |
| 3402 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3403 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3404 | ** the first entry. TRUE is also returned if the table is empty. |
| 3405 | */ |
| 3406 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3407 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3408 | ** have been deleted? This API will need to change to return an error code |
| 3409 | ** as well as the boolean result value. |
| 3410 | */ |
| 3411 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3415 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3416 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3417 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3418 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3419 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3420 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3421 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3422 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3423 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3424 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3425 | rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3426 | if( rc!=SQLITE_OK ){ |
| 3427 | return rc; |
| 3428 | } |
| 3429 | if( pCur->skip>0 ){ |
| 3430 | pCur->skip = 0; |
| 3431 | *pRes = 0; |
| 3432 | return SQLITE_OK; |
| 3433 | } |
| 3434 | pCur->skip = 0; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3435 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3436 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3437 | assert( pRes!=0 ); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3438 | pPage = pCur->pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3439 | if( CURSOR_INVALID==pCur->eState ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3440 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3441 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3442 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3443 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3444 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3445 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3446 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3447 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3448 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3449 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3450 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3451 | if( rc ) return rc; |
| 3452 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3453 | *pRes = 0; |
| 3454 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3455 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3456 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3457 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3458 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3459 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3460 | return SQLITE_OK; |
| 3461 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3462 | moveToParent(pCur); |
| 3463 | pPage = pCur->pPage; |
| 3464 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3465 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3466 | if( pPage->leafData ){ |
| 3467 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3468 | }else{ |
| 3469 | rc = SQLITE_OK; |
| 3470 | } |
| 3471 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3472 | } |
| 3473 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3474 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3475 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3476 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3477 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3478 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3481 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3482 | ** 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] | 3483 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3484 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3485 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3486 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3487 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3488 | int rc; |
| 3489 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3490 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3491 | |
| 3492 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3493 | rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3494 | if( rc!=SQLITE_OK ){ |
| 3495 | return rc; |
| 3496 | } |
| 3497 | if( pCur->skip<0 ){ |
| 3498 | pCur->skip = 0; |
| 3499 | *pRes = 0; |
| 3500 | return SQLITE_OK; |
| 3501 | } |
| 3502 | pCur->skip = 0; |
| 3503 | #endif |
| 3504 | |
| 3505 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3506 | *pRes = 1; |
| 3507 | return SQLITE_OK; |
| 3508 | } |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3509 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3510 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3511 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3512 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3513 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3514 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3515 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3516 | if( rc ) return rc; |
| 3517 | rc = moveToRightmost(pCur); |
| 3518 | }else{ |
| 3519 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3520 | if( isRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3521 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3522 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3523 | return SQLITE_OK; |
| 3524 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3525 | moveToParent(pCur); |
| 3526 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3527 | } |
| 3528 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3529 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3530 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3531 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3532 | }else{ |
| 3533 | rc = SQLITE_OK; |
| 3534 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3535 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3536 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3537 | return rc; |
| 3538 | } |
| 3539 | |
| 3540 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3541 | ** Allocate a new page from the database file. |
| 3542 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3543 | ** The new page is marked as dirty. (In other words, sqlite3pager_write() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3544 | ** has already been called on the new page.) The new page has also |
| 3545 | ** been referenced and the calling routine is responsible for calling |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3546 | ** sqlite3pager_unref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3547 | ** |
| 3548 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3549 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3550 | ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3551 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3552 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3553 | ** 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] | 3554 | ** attempt to keep related pages close to each other in the database file, |
| 3555 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3556 | ** |
| 3557 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3558 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3559 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3560 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3561 | static int allocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3562 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3563 | MemPage **ppPage, |
| 3564 | Pgno *pPgno, |
| 3565 | Pgno nearby, |
| 3566 | u8 exact |
| 3567 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3568 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3569 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3570 | int n; /* Number of pages on the freelist */ |
| 3571 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3572 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3573 | pPage1 = pBt->pPage1; |
| 3574 | n = get4byte(&pPage1->aData[36]); |
| 3575 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3576 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3577 | MemPage *pTrunk = 0; |
| 3578 | Pgno iTrunk; |
| 3579 | MemPage *pPrevTrunk = 0; |
| 3580 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3581 | |
| 3582 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3583 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3584 | ** the entire-list will be searched for that page. |
| 3585 | */ |
| 3586 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3587 | if( exact ){ |
| 3588 | u8 eType; |
| 3589 | assert( nearby>0 ); |
| 3590 | assert( pBt->autoVacuum ); |
| 3591 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3592 | if( rc ) return rc; |
| 3593 | if( eType==PTRMAP_FREEPAGE ){ |
| 3594 | searchList = 1; |
| 3595 | } |
| 3596 | *pPgno = nearby; |
| 3597 | } |
| 3598 | #endif |
| 3599 | |
| 3600 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3601 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3602 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3603 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3604 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3605 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3606 | |
| 3607 | /* The code within this loop is run only once if the 'searchList' variable |
| 3608 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3609 | ** free-list until the page 'nearby' is located. |
| 3610 | */ |
| 3611 | do { |
| 3612 | pPrevTrunk = pTrunk; |
| 3613 | if( pPrevTrunk ){ |
| 3614 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3615 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3616 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3617 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3618 | rc = getPage(pBt, iTrunk, &pTrunk); |
| 3619 | if( rc ){ |
| 3620 | releasePage(pPrevTrunk); |
| 3621 | return rc; |
| 3622 | } |
| 3623 | |
| 3624 | /* TODO: This should move to after the loop? */ |
| 3625 | rc = sqlite3pager_write(pTrunk->aData); |
| 3626 | if( rc ){ |
| 3627 | releasePage(pTrunk); |
| 3628 | releasePage(pPrevTrunk); |
| 3629 | return rc; |
| 3630 | } |
| 3631 | |
| 3632 | k = get4byte(&pTrunk->aData[4]); |
| 3633 | if( k==0 && !searchList ){ |
| 3634 | /* The trunk has no leaves and the list is not being searched. |
| 3635 | ** So extract the trunk page itself and use it as the newly |
| 3636 | ** allocated page */ |
| 3637 | assert( pPrevTrunk==0 ); |
| 3638 | *pPgno = iTrunk; |
| 3639 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3640 | *ppPage = pTrunk; |
| 3641 | pTrunk = 0; |
| 3642 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3643 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3644 | /* Value of k is out of range. Database corruption */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3645 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3646 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3647 | }else if( searchList && nearby==iTrunk ){ |
| 3648 | /* The list is being searched and this trunk page is the page |
| 3649 | ** to allocate, regardless of whether it has leaves. |
| 3650 | */ |
| 3651 | assert( *pPgno==iTrunk ); |
| 3652 | *ppPage = pTrunk; |
| 3653 | searchList = 0; |
| 3654 | if( k==0 ){ |
| 3655 | if( !pPrevTrunk ){ |
| 3656 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3657 | }else{ |
| 3658 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3659 | } |
| 3660 | }else{ |
| 3661 | /* The trunk page is required by the caller but it contains |
| 3662 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3663 | ** page in this case. |
| 3664 | */ |
| 3665 | MemPage *pNewTrunk; |
| 3666 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
| 3667 | rc = getPage(pBt, iNewTrunk, &pNewTrunk); |
| 3668 | if( rc!=SQLITE_OK ){ |
| 3669 | releasePage(pTrunk); |
| 3670 | releasePage(pPrevTrunk); |
| 3671 | return rc; |
| 3672 | } |
| 3673 | rc = sqlite3pager_write(pNewTrunk->aData); |
| 3674 | if( rc!=SQLITE_OK ){ |
| 3675 | releasePage(pNewTrunk); |
| 3676 | releasePage(pTrunk); |
| 3677 | releasePage(pPrevTrunk); |
| 3678 | return rc; |
| 3679 | } |
| 3680 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3681 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3682 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
| 3683 | if( !pPrevTrunk ){ |
| 3684 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3685 | }else{ |
| 3686 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3687 | } |
| 3688 | releasePage(pNewTrunk); |
| 3689 | } |
| 3690 | pTrunk = 0; |
| 3691 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3692 | #endif |
| 3693 | }else{ |
| 3694 | /* Extract a leaf from the trunk */ |
| 3695 | int closest; |
| 3696 | Pgno iPage; |
| 3697 | unsigned char *aData = pTrunk->aData; |
| 3698 | if( nearby>0 ){ |
| 3699 | int i, dist; |
| 3700 | closest = 0; |
| 3701 | dist = get4byte(&aData[8]) - nearby; |
| 3702 | if( dist<0 ) dist = -dist; |
| 3703 | for(i=1; i<k; i++){ |
| 3704 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3705 | if( d2<0 ) d2 = -d2; |
| 3706 | if( d2<dist ){ |
| 3707 | closest = i; |
| 3708 | dist = d2; |
| 3709 | } |
| 3710 | } |
| 3711 | }else{ |
| 3712 | closest = 0; |
| 3713 | } |
| 3714 | |
| 3715 | iPage = get4byte(&aData[8+closest*4]); |
| 3716 | if( !searchList || iPage==nearby ){ |
| 3717 | *pPgno = iPage; |
| 3718 | if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
| 3719 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3720 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3721 | } |
| 3722 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3723 | ": %d more free pages\n", |
| 3724 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3725 | if( closest<k-1 ){ |
| 3726 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3727 | } |
| 3728 | put4byte(&aData[4], k-1); |
| 3729 | rc = getPage(pBt, *pPgno, ppPage); |
| 3730 | if( rc==SQLITE_OK ){ |
| 3731 | sqlite3pager_dont_rollback((*ppPage)->aData); |
| 3732 | rc = sqlite3pager_write((*ppPage)->aData); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3733 | if( rc!=SQLITE_OK ){ |
| 3734 | releasePage(*ppPage); |
| 3735 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3736 | } |
| 3737 | searchList = 0; |
| 3738 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3739 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3740 | releasePage(pPrevTrunk); |
| 3741 | }while( searchList ); |
| 3742 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3743 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3744 | /* There are no pages on the freelist, so create a new page at the |
| 3745 | ** end of the file */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3746 | *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3747 | |
| 3748 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 3749 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3750 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3751 | ** at the end of the file instead of one. The first allocated page |
| 3752 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3753 | */ |
| 3754 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3755 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3756 | (*pPgno)++; |
| 3757 | } |
| 3758 | #endif |
| 3759 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3760 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3761 | rc = getPage(pBt, *pPgno, ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3762 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3763 | rc = sqlite3pager_write((*ppPage)->aData); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3764 | if( rc!=SQLITE_OK ){ |
| 3765 | releasePage(*ppPage); |
| 3766 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3767 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3768 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3769 | |
| 3770 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3771 | return rc; |
| 3772 | } |
| 3773 | |
| 3774 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3775 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3776 | ** |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3777 | ** sqlite3pager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3778 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3779 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3780 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3781 | MemPage *pPage1 = pBt->pPage1; |
| 3782 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3783 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3784 | /* Prepare the page for freeing */ |
| 3785 | assert( pPage->pgno>1 ); |
| 3786 | pPage->isInit = 0; |
| 3787 | releasePage(pPage->pParent); |
| 3788 | pPage->pParent = 0; |
| 3789 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3790 | /* Increment the free page count on pPage1 */ |
| 3791 | rc = sqlite3pager_write(pPage1->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3792 | if( rc ) return rc; |
| 3793 | n = get4byte(&pPage1->aData[36]); |
| 3794 | put4byte(&pPage1->aData[36], n+1); |
| 3795 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3796 | #ifdef SQLITE_SECURE_DELETE |
| 3797 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 3798 | ** always fully overwrite deleted information with zeros. |
| 3799 | */ |
| 3800 | rc = sqlite3pager_write(pPage->aData); |
| 3801 | if( rc ) return rc; |
| 3802 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 3803 | #endif |
| 3804 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3805 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3806 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3807 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3808 | */ |
| 3809 | if( pBt->autoVacuum ){ |
| 3810 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3811 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3812 | } |
| 3813 | #endif |
| 3814 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3815 | if( n==0 ){ |
| 3816 | /* This is the first free page */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3817 | rc = sqlite3pager_write(pPage->aData); |
| 3818 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3819 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3820 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3821 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3822 | }else{ |
| 3823 | /* Other free pages already exist. Retrive the first trunk page |
| 3824 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3825 | MemPage *pTrunk; |
| 3826 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3827 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3828 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3829 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3830 | /* The trunk is full. Turn the page being freed into a new |
| 3831 | ** trunk page with no leaves. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3832 | rc = sqlite3pager_write(pPage->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3833 | if( rc ) return rc; |
| 3834 | put4byte(pPage->aData, pTrunk->pgno); |
| 3835 | put4byte(&pPage->aData[4], 0); |
| 3836 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3837 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3838 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3839 | }else{ |
| 3840 | /* Add the newly freed page as a leaf on the current trunk */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3841 | rc = sqlite3pager_write(pTrunk->aData); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3842 | if( rc ) return rc; |
| 3843 | put4byte(&pTrunk->aData[4], k+1); |
| 3844 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3845 | #ifndef SQLITE_SECURE_DELETE |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3846 | sqlite3pager_dont_write(pBt->pPager, pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3847 | #endif |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3848 | 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] | 3849 | } |
| 3850 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3851 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3852 | return rc; |
| 3853 | } |
| 3854 | |
| 3855 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3856 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3857 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3858 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3859 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3860 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3861 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3862 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3863 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3864 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3865 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3866 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3867 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3868 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3869 | while( ovflPgno!=0 ){ |
| 3870 | MemPage *pOvfl; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 3871 | if( ovflPgno>sqlite3pager_pagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3872 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 3873 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3874 | rc = getPage(pBt, ovflPgno, &pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3875 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3876 | ovflPgno = get4byte(pOvfl->aData); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3877 | rc = freePage(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3878 | sqlite3pager_unref(pOvfl->aData); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3879 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3880 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3881 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3885 | ** Create the byte sequence used to represent a cell on page pPage |
| 3886 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3887 | ** allocated and filled in as necessary. The calling procedure |
| 3888 | ** is responsible for making sure sufficient space has been allocated |
| 3889 | ** for pCell[]. |
| 3890 | ** |
| 3891 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3892 | ** area. pCell might point to some temporary storage. The cell will |
| 3893 | ** be constructed in this temporary area then copied into pPage->aData |
| 3894 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3895 | */ |
| 3896 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3897 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3898 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3899 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3900 | const void *pData,int nData, /* The data */ |
| 3901 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3902 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3903 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3904 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3905 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3906 | int spaceLeft; |
| 3907 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3908 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3909 | unsigned char *pPrior; |
| 3910 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3911 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3912 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3913 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3914 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3915 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3916 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3917 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3918 | if( !pPage->leaf ){ |
| 3919 | nHeader += 4; |
| 3920 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3921 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3922 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3923 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3924 | nData = 0; |
| 3925 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3926 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3927 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3928 | assert( info.nHeader==nHeader ); |
| 3929 | assert( info.nKey==nKey ); |
| 3930 | assert( info.nData==nData ); |
| 3931 | |
| 3932 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3933 | nPayload = nData; |
| 3934 | if( pPage->intKey ){ |
| 3935 | pSrc = pData; |
| 3936 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3937 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3938 | }else{ |
| 3939 | nPayload += nKey; |
| 3940 | pSrc = pKey; |
| 3941 | nSrc = nKey; |
| 3942 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3943 | *pnSize = info.nSize; |
| 3944 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3945 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3946 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3947 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3948 | while( nPayload>0 ){ |
| 3949 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3950 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3951 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 3952 | #endif |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3953 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3954 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3955 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3956 | ** overflow page is being allocated, add an entry to the pointer-map |
| 3957 | ** for that page now. The entry for the first overflow page will be |
| 3958 | ** added later, by the insertCell() routine. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3959 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3960 | if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ |
| 3961 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3962 | } |
| 3963 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3964 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3965 | releasePage(pToRelease); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 3966 | /* clearCell(pPage, pCell); */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3967 | return rc; |
| 3968 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3969 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3970 | releasePage(pToRelease); |
| 3971 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3972 | pPrior = pOvfl->aData; |
| 3973 | put4byte(pPrior, 0); |
| 3974 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3975 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3976 | } |
| 3977 | n = nPayload; |
| 3978 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3979 | if( n>nSrc ) n = nSrc; |
drh | ff3b170 | 2006-03-11 12:04:18 +0000 | [diff] [blame] | 3980 | assert( pSrc ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3981 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3982 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3983 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3984 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3985 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3986 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3987 | if( nSrc==0 ){ |
| 3988 | nSrc = nData; |
| 3989 | pSrc = pData; |
| 3990 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3991 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3992 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3993 | return SQLITE_OK; |
| 3994 | } |
| 3995 | |
| 3996 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3997 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3998 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3999 | ** pointer in the third argument. |
| 4000 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4001 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4002 | MemPage *pThis; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4003 | unsigned char *aData; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4004 | |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4005 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4006 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4007 | assert( pBt->pPager!=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4008 | aData = sqlite3pager_lookup(pBt->pPager, pgno); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4009 | if( aData ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4010 | pThis = (MemPage*)&aData[pBt->pageSize]; |
drh | 3127653 | 2004-09-27 12:20:52 +0000 | [diff] [blame] | 4011 | assert( pThis->aData==aData ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4012 | if( pThis->isInit ){ |
| 4013 | if( pThis->pParent!=pNewParent ){ |
| 4014 | if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData); |
| 4015 | pThis->pParent = pNewParent; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4016 | sqlite3pager_ref(pNewParent->aData); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4017 | } |
| 4018 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4019 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4020 | sqlite3pager_unref(aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4021 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4022 | |
| 4023 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4024 | if( pBt->autoVacuum ){ |
| 4025 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4026 | } |
| 4027 | #endif |
| 4028 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4031 | |
| 4032 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4033 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4034 | ** Change the pParent pointer of all children of pPage to point back |
| 4035 | ** to pPage. |
| 4036 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4037 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4038 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4039 | ** |
| 4040 | ** This routine gets called after you memcpy() one page into |
| 4041 | ** another. |
| 4042 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4043 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4044 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4045 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4046 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4047 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4048 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4049 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4050 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4051 | u8 *pCell = findCell(pPage, i); |
| 4052 | if( !pPage->leaf ){ |
| 4053 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4054 | if( rc!=SQLITE_OK ) return rc; |
| 4055 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4056 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4057 | if( !pPage->leaf ){ |
| 4058 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4059 | pPage, i); |
| 4060 | pPage->idxShift = 0; |
| 4061 | } |
| 4062 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | /* |
| 4066 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4067 | ** The cell content is not freed or deallocated. It is assumed that |
| 4068 | ** the cell content has been copied someplace else. This routine just |
| 4069 | ** removes the reference to the cell from pPage. |
| 4070 | ** |
| 4071 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4072 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4073 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4074 | int i; /* Loop counter */ |
| 4075 | int pc; /* Offset to cell content of cell being deleted */ |
| 4076 | u8 *data; /* pPage->aData */ |
| 4077 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4078 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4079 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4080 | assert( sz==cellSize(pPage, idx) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4081 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4082 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4083 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4084 | pc = get2byte(ptr); |
| 4085 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4086 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4087 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4088 | ptr[0] = ptr[2]; |
| 4089 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4090 | } |
| 4091 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4092 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4093 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4094 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* |
| 4098 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4099 | ** content of the cell. |
| 4100 | ** |
| 4101 | ** 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] | 4102 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4103 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4104 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4105 | ** in pTemp or the original pCell) and also record its index. |
| 4106 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4107 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4108 | ** |
| 4109 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4110 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4111 | ** 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] | 4112 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4113 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4114 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4115 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4116 | int i, /* New cell becomes the i-th cell of the page */ |
| 4117 | u8 *pCell, /* Content of the new cell */ |
| 4118 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4119 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4120 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4121 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4122 | int idx; /* Where to write new cell content in data[] */ |
| 4123 | int j; /* Loop counter */ |
| 4124 | int top; /* First byte of content for any cell in data[] */ |
| 4125 | int end; /* First byte past the last cell pointer in data[] */ |
| 4126 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4127 | int hdr; /* Offset into data[] of the page header */ |
| 4128 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4129 | u8 *data; /* The content of the whole page */ |
| 4130 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4131 | |
| 4132 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4133 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4134 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4135 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4136 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4137 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4138 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4139 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4140 | j = pPage->nOverflow++; |
| 4141 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4142 | pPage->aOvfl[j].pCell = pCell; |
| 4143 | pPage->aOvfl[j].idx = i; |
| 4144 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4145 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4146 | data = pPage->aData; |
| 4147 | hdr = pPage->hdrOffset; |
| 4148 | top = get2byte(&data[hdr+5]); |
| 4149 | cellOffset = pPage->cellOffset; |
| 4150 | end = cellOffset + 2*pPage->nCell + 2; |
| 4151 | ins = cellOffset + 2*i; |
| 4152 | if( end > top - sz ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4153 | int rc = defragmentPage(pPage); |
| 4154 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4155 | top = get2byte(&data[hdr+5]); |
| 4156 | assert( end + sz <= top ); |
| 4157 | } |
| 4158 | idx = allocateSpace(pPage, sz); |
| 4159 | assert( idx>0 ); |
| 4160 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4161 | pPage->nCell++; |
| 4162 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4163 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4164 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4165 | ptr[0] = ptr[-2]; |
| 4166 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4167 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4168 | put2byte(&data[ins], idx); |
| 4169 | put2byte(&data[hdr+3], pPage->nCell); |
| 4170 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4171 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4172 | if( pPage->pBt->autoVacuum ){ |
| 4173 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4174 | ** the entry for the overflow page into the pointer map. |
| 4175 | */ |
| 4176 | CellInfo info; |
| 4177 | parseCellPtr(pPage, pCell, &info); |
| 4178 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4179 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 4180 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 4181 | if( rc!=SQLITE_OK ) return rc; |
| 4182 | } |
| 4183 | } |
| 4184 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4185 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4186 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4187 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4191 | ** Add a list of cells to a page. The page should be initially empty. |
| 4192 | ** The cells are guaranteed to fit on the page. |
| 4193 | */ |
| 4194 | static void assemblePage( |
| 4195 | MemPage *pPage, /* The page to be assemblied */ |
| 4196 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4197 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4198 | int *aSize /* Sizes of the cells */ |
| 4199 | ){ |
| 4200 | int i; /* Loop counter */ |
| 4201 | int totalSize; /* Total size of all cells */ |
| 4202 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4203 | int cellptr; /* Address of next cell pointer */ |
| 4204 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4205 | u8 *data; /* Data for the page */ |
| 4206 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4207 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4208 | totalSize = 0; |
| 4209 | for(i=0; i<nCell; i++){ |
| 4210 | totalSize += aSize[i]; |
| 4211 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4212 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4213 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4214 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4215 | data = pPage->aData; |
| 4216 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4217 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4218 | if( nCell ){ |
| 4219 | cellbody = allocateSpace(pPage, totalSize); |
| 4220 | assert( cellbody>0 ); |
| 4221 | assert( pPage->nFree >= 2*nCell ); |
| 4222 | pPage->nFree -= 2*nCell; |
| 4223 | for(i=0; i<nCell; i++){ |
| 4224 | put2byte(&data[cellptr], cellbody); |
| 4225 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4226 | cellptr += 2; |
| 4227 | cellbody += aSize[i]; |
| 4228 | } |
| 4229 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4230 | } |
| 4231 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4234 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4235 | ** The following parameters determine how many adjacent pages get involved |
| 4236 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4237 | ** of the page that participate in the balancing operation. NB is the |
| 4238 | ** total number of pages that participate, including the target page and |
| 4239 | ** NN neighbors on either side. |
| 4240 | ** |
| 4241 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4242 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4243 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4244 | ** The value of NN appears to give the best results overall. |
| 4245 | */ |
| 4246 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4247 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4248 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4249 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4250 | static int balance(MemPage*, int); |
| 4251 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4252 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4253 | /* |
| 4254 | ** This version of balance() handles the common special case where |
| 4255 | ** a new entry is being inserted on the extreme right-end of the |
| 4256 | ** tree, in other words, when the new entry will become the largest |
| 4257 | ** entry in the tree. |
| 4258 | ** |
| 4259 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4260 | ** a new page to the right-hand side and put the one new entry in |
| 4261 | ** that page. This leaves the right side of the tree somewhat |
| 4262 | ** unbalanced. But odds are that we will be inserting new entries |
| 4263 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4264 | ** fill up. On average. |
| 4265 | ** |
| 4266 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4267 | ** pParent is its parent. pPage must have a single overflow entry |
| 4268 | ** which is also the right-most entry on the page. |
| 4269 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4270 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4271 | int rc; |
| 4272 | MemPage *pNew; |
| 4273 | Pgno pgnoNew; |
| 4274 | u8 *pCell; |
| 4275 | int szCell; |
| 4276 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4277 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4278 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4279 | int parentSize; /* Size of new divider cell */ |
| 4280 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4281 | |
| 4282 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4283 | ** into it. Then remove the overflow cell from pPage. |
| 4284 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4285 | rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4286 | if( rc!=SQLITE_OK ){ |
| 4287 | return rc; |
| 4288 | } |
| 4289 | pCell = pPage->aOvfl[0].pCell; |
| 4290 | szCell = cellSizePtr(pPage, pCell); |
| 4291 | zeroPage(pNew, pPage->aData[0]); |
| 4292 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4293 | pPage->nOverflow = 0; |
| 4294 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4295 | /* Set the parent of the newly allocated page to pParent. */ |
| 4296 | pNew->pParent = pParent; |
| 4297 | sqlite3pager_ref(pParent->aData); |
| 4298 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4299 | /* pPage is currently the right-child of pParent. Change this |
| 4300 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4301 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4302 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4303 | assert( pPage->nCell>0 ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4304 | parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info); |
| 4305 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize); |
| 4306 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4307 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4308 | } |
| 4309 | assert( parentSize<64 ); |
| 4310 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4311 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4312 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4313 | } |
| 4314 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4315 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4316 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4317 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4318 | /* If this is an auto-vacuum database, update the pointer map |
| 4319 | ** with entries for the new page, and any pointer from the |
| 4320 | ** cell on the page to an overflow page. |
| 4321 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4322 | if( pBt->autoVacuum ){ |
| 4323 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 4324 | if( rc!=SQLITE_OK ){ |
| 4325 | return rc; |
| 4326 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4327 | rc = ptrmapPutOvfl(pNew, 0); |
| 4328 | if( rc!=SQLITE_OK ){ |
| 4329 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4330 | } |
| 4331 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4332 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4333 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4334 | /* Release the reference to the new page and balance the parent page, |
| 4335 | ** in case the divider cell inserted caused it to become overfull. |
| 4336 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4337 | releasePage(pNew); |
| 4338 | return balance(pParent, 0); |
| 4339 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4340 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4341 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4342 | /* |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4343 | ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine |
| 4344 | ** if the database supports auto-vacuum or not. Because it is used |
| 4345 | ** within an expression that is an argument to another macro |
| 4346 | ** (sqliteMallocRaw), it is not possible to use conditional compilation. |
| 4347 | ** So, this macro is defined instead. |
| 4348 | */ |
| 4349 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4350 | #define ISAUTOVACUUM (pBt->autoVacuum) |
| 4351 | #else |
| 4352 | #define ISAUTOVACUUM 0 |
| 4353 | #endif |
| 4354 | |
| 4355 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4356 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4357 | ** 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] | 4358 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4359 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4360 | ** 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] | 4361 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4362 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4363 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4364 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4365 | ** 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] | 4366 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4367 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4368 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4369 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4370 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4371 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4372 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4373 | ** 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] | 4374 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4375 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4376 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4377 | ** might become overfull or underfull. If that happens, then this routine |
| 4378 | ** is called recursively on the parent. |
| 4379 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4380 | ** If this routine fails for any reason, it might leave the database |
| 4381 | ** in a corrupted state. So if this routine fails, the database should |
| 4382 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4383 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4384 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4385 | MemPage *pParent; /* The parent of pPage */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4386 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4387 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4388 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4389 | int nOld; /* Number of pages in apOld[] */ |
| 4390 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4391 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4392 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4393 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4394 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4395 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4396 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4397 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4398 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4399 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4400 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4401 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4402 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4403 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4404 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4405 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4406 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4407 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4408 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4409 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4410 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4411 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4412 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4413 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4414 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4415 | u8 *aFrom = 0; |
| 4416 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4417 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4418 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4419 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4420 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4421 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4422 | assert( sqlite3pager_iswriteable(pPage->aData) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4423 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4424 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4425 | assert( pParent ); |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4426 | if( SQLITE_OK!=(rc = sqlite3pager_write(pParent->aData)) ){ |
| 4427 | return rc; |
| 4428 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4429 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4430 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4431 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4432 | /* |
| 4433 | ** A special case: If a new entry has just been inserted into a |
| 4434 | ** table (that is, a btree with integer keys and all data at the leaves) |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4435 | ** and the new entry is the right-most entry in the tree (it has the |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4436 | ** largest key) then use the special balance_quick() routine for |
| 4437 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4438 | ** packing of data in the common case. |
| 4439 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4440 | if( pPage->leaf && |
| 4441 | pPage->intKey && |
| 4442 | pPage->leafData && |
| 4443 | pPage->nOverflow==1 && |
| 4444 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4445 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4446 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4447 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4448 | /* |
| 4449 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4450 | ** they are not full and no new page is required. |
| 4451 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4452 | return balance_quick(pPage, pParent); |
| 4453 | } |
| 4454 | #endif |
| 4455 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4456 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4457 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4458 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4459 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4460 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4461 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4462 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4463 | pgno = pPage->pgno; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4464 | assert( pgno==sqlite3pager_pagenumber(pPage->aData) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4465 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4466 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4467 | break; |
| 4468 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4469 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4470 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4471 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4472 | }else{ |
| 4473 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4474 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4475 | |
| 4476 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4477 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4478 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4479 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4480 | nOld = nNew = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4481 | sqlite3pager_ref(pParent->aData); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4482 | |
| 4483 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4484 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4485 | ** the siblings. An attempt is made to find NN siblings on either |
| 4486 | ** side of pPage. More siblings are taken from one side, however, if |
| 4487 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4488 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4489 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4490 | nxDiv = idx - NN; |
| 4491 | if( nxDiv + NB > pParent->nCell ){ |
| 4492 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4493 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4494 | if( nxDiv<0 ){ |
| 4495 | nxDiv = 0; |
| 4496 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4497 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4498 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4499 | if( k<pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4500 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4501 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4502 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4503 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4504 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4505 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4506 | }else{ |
| 4507 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4508 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4509 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4510 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4511 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4512 | apCopy[i] = 0; |
| 4513 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4514 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4515 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4516 | } |
| 4517 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4518 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4519 | ** alignment */ |
| 4520 | nMaxCells = (nMaxCells + 1)&~1; |
| 4521 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4522 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4523 | ** Allocate space for memory structures |
| 4524 | */ |
| 4525 | apCell = sqliteMallocRaw( |
| 4526 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4527 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4528 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4529 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4530 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4531 | ); |
| 4532 | if( apCell==0 ){ |
| 4533 | rc = SQLITE_NOMEM; |
| 4534 | goto balance_cleanup; |
| 4535 | } |
| 4536 | szCell = (int*)&apCell[nMaxCells]; |
| 4537 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4538 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4539 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4540 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4541 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4542 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4543 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4544 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4545 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4546 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4547 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4548 | } |
| 4549 | #endif |
| 4550 | |
| 4551 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4552 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4553 | ** The rest of this function will use data from the copies rather |
| 4554 | ** that the original pages since the original pages will be in the |
| 4555 | ** process of being overwritten. |
| 4556 | */ |
| 4557 | for(i=0; i<nOld; i++){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4558 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4559 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 4560 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 4561 | /* The memcpy() above changes the value of p->aData so we have to |
| 4562 | ** set it again. */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4563 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | /* |
| 4567 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4568 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4569 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4570 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4571 | ** |
| 4572 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4573 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4574 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4575 | ** child pointers. If siblings are not leaves, then all cell in |
| 4576 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4577 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4578 | ** |
| 4579 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4580 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4581 | */ |
| 4582 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4583 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4584 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4585 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4586 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4587 | int limit = pOld->nCell+pOld->nOverflow; |
| 4588 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4589 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4590 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4591 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4592 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4593 | if( pBt->autoVacuum ){ |
| 4594 | int a; |
| 4595 | aFrom[nCell] = i; |
| 4596 | for(a=0; a<pOld->nOverflow; a++){ |
| 4597 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4598 | aFrom[nCell] = 0xFF; |
| 4599 | break; |
| 4600 | } |
| 4601 | } |
| 4602 | } |
| 4603 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4604 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4605 | } |
| 4606 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4607 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4608 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4609 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4610 | ** are duplicates of keys on the child pages. We need to remove |
| 4611 | ** the divider cells from pParent, but the dividers cells are not |
| 4612 | ** added to apCell[] because they are duplicates of child cells. |
| 4613 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4614 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4615 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4616 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4617 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4618 | szCell[nCell] = sz; |
| 4619 | pTemp = &aSpace[iSpace]; |
| 4620 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4621 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4622 | memcpy(pTemp, apDiv[i], sz); |
| 4623 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4624 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4625 | if( pBt->autoVacuum ){ |
| 4626 | aFrom[nCell] = 0xFF; |
| 4627 | } |
| 4628 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4629 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4630 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4631 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4632 | if( !pOld->leaf ){ |
| 4633 | assert( leafCorrection==0 ); |
| 4634 | /* The right pointer of the child page pOld becomes the left |
| 4635 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4636 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4637 | }else{ |
| 4638 | assert( leafCorrection==4 ); |
| 4639 | } |
| 4640 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4641 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4642 | } |
| 4643 | } |
| 4644 | |
| 4645 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4646 | ** Figure out the number of pages needed to hold all nCell cells. |
| 4647 | ** Store this number in "k". Also compute szNew[] which is the total |
| 4648 | ** 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] | 4649 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4650 | ** cntNew[k] should equal nCell. |
| 4651 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4652 | ** Values computed by this block: |
| 4653 | ** |
| 4654 | ** k: The total number of sibling pages |
| 4655 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 4656 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 4657 | ** the right of the i-th sibling page. |
| 4658 | ** usableSpace: Number of bytes of space available on each sibling. |
| 4659 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4660 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4661 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4662 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4663 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4664 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4665 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4666 | szNew[k] = subtotal - szCell[i]; |
| 4667 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4668 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4669 | subtotal = 0; |
| 4670 | k++; |
| 4671 | } |
| 4672 | } |
| 4673 | szNew[k] = subtotal; |
| 4674 | cntNew[k] = nCell; |
| 4675 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4676 | |
| 4677 | /* |
| 4678 | ** The packing computed by the previous block is biased toward the siblings |
| 4679 | ** on the left side. The left siblings are always nearly full, while the |
| 4680 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4681 | ** to adjust the packing of siblings to get a better balance. |
| 4682 | ** |
| 4683 | ** This adjustment is more than an optimization. The packing above might |
| 4684 | ** be so out of balance as to be illegal. For example, the right-most |
| 4685 | ** sibling might be completely empty. This adjustment is not optional. |
| 4686 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4687 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4688 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4689 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4690 | int r; /* Index of right-most cell in left sibling */ |
| 4691 | int d; /* Index of first cell to the left of right sibling */ |
| 4692 | |
| 4693 | r = cntNew[i-1] - 1; |
| 4694 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4695 | assert( d<nMaxCells ); |
| 4696 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4697 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4698 | szRight += szCell[d] + 2; |
| 4699 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4700 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4701 | r = cntNew[i-1] - 1; |
| 4702 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4703 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4704 | szNew[i] = szRight; |
| 4705 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4706 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4707 | |
| 4708 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 4709 | ** a virtual root page. A virtual root page is when the real root |
| 4710 | ** page is page 1 and we are the only child of that page. |
| 4711 | */ |
| 4712 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4713 | |
| 4714 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4715 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4716 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4717 | assert( pPage->pgno>1 ); |
| 4718 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4719 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4720 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4721 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4722 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4723 | pgnoNew[i] = pgnoOld[i]; |
| 4724 | apOld[i] = 0; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4725 | rc = sqlite3pager_write(pNew->aData); |
| 4726 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4727 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4728 | assert( i>0 ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4729 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4730 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4731 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4732 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4733 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4734 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4737 | /* Free any old pages that were not reused as new pages. |
| 4738 | */ |
| 4739 | while( i<nOld ){ |
| 4740 | rc = freePage(apOld[i]); |
| 4741 | if( rc ) goto balance_cleanup; |
| 4742 | releasePage(apOld[i]); |
| 4743 | apOld[i] = 0; |
| 4744 | i++; |
| 4745 | } |
| 4746 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4747 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4748 | ** Put the new pages in accending order. This helps to |
| 4749 | ** keep entries in the disk file in order so that a scan |
| 4750 | ** of the table is a linear scan through the file. That |
| 4751 | ** in turn helps the operating system to deliver pages |
| 4752 | ** from the disk more rapidly. |
| 4753 | ** |
| 4754 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4755 | ** n is never more than NB (a small constant), that should |
| 4756 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4757 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4758 | ** When NB==3, this one optimization makes the database |
| 4759 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4760 | */ |
| 4761 | for(i=0; i<k-1; i++){ |
| 4762 | int minV = pgnoNew[i]; |
| 4763 | int minI = i; |
| 4764 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 4765 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4766 | minI = j; |
| 4767 | minV = pgnoNew[j]; |
| 4768 | } |
| 4769 | } |
| 4770 | if( minI>i ){ |
| 4771 | int t; |
| 4772 | MemPage *pT; |
| 4773 | t = pgnoNew[i]; |
| 4774 | pT = apNew[i]; |
| 4775 | pgnoNew[i] = pgnoNew[minI]; |
| 4776 | apNew[i] = apNew[minI]; |
| 4777 | pgnoNew[minI] = t; |
| 4778 | apNew[minI] = pT; |
| 4779 | } |
| 4780 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4781 | 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] | 4782 | pgnoOld[0], |
| 4783 | nOld>=2 ? pgnoOld[1] : 0, |
| 4784 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 4785 | pgnoNew[0], szNew[0], |
| 4786 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 4787 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4788 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 4789 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4790 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4791 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4792 | ** Evenly distribute the data in apCell[] across the new pages. |
| 4793 | ** Insert divider cells into pParent as necessary. |
| 4794 | */ |
| 4795 | j = 0; |
| 4796 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4797 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4798 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 4799 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4800 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4801 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4802 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4803 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4804 | |
| 4805 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4806 | /* If this is an auto-vacuum database, update the pointer map entries |
| 4807 | ** that point to the siblings that were rearranged. These can be: left |
| 4808 | ** children of cells, the right-child of the page, or overflow pages |
| 4809 | ** pointed to by cells. |
| 4810 | */ |
| 4811 | if( pBt->autoVacuum ){ |
| 4812 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4813 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4814 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4815 | rc = ptrmapPutOvfl(pNew, k-j); |
| 4816 | if( rc!=SQLITE_OK ){ |
| 4817 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | } |
| 4821 | } |
| 4822 | #endif |
| 4823 | |
| 4824 | j = cntNew[i]; |
| 4825 | |
| 4826 | /* If the sibling page assembled above was not the right-most sibling, |
| 4827 | ** insert a divider cell into the parent page. |
| 4828 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4829 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4830 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4831 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4832 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4833 | |
| 4834 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4835 | pCell = apCell[j]; |
| 4836 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4837 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4838 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4839 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4840 | }else if( leafData ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4841 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 4842 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 4843 | ** cell consists of the integer key for the right-most cell of |
| 4844 | ** the sibling-page assembled above only. |
| 4845 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4846 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4847 | j--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4848 | parseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4849 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4850 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4851 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4852 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4853 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4854 | }else{ |
| 4855 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4856 | pTemp = &aSpace[iSpace]; |
| 4857 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4858 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4859 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4860 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4861 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4862 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4863 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4864 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 4865 | ** then update the pointer map with an entry for the overflow page |
| 4866 | ** that the cell just inserted points to (if any). |
| 4867 | */ |
| 4868 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4869 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 4870 | if( rc!=SQLITE_OK ){ |
| 4871 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4872 | } |
| 4873 | } |
| 4874 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4875 | j++; |
| 4876 | nxDiv++; |
| 4877 | } |
| 4878 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4879 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4880 | assert( nOld>0 ); |
| 4881 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4882 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4883 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4884 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4885 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4886 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4887 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4888 | }else{ |
| 4889 | /* Right-most sibling is the left child of the first entry in pParent |
| 4890 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4891 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | /* |
| 4895 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4896 | */ |
| 4897 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4898 | rc = reparentChildPages(apNew[i]); |
| 4899 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4900 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4901 | rc = reparentChildPages(pParent); |
| 4902 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4903 | |
| 4904 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4905 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4906 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4907 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4908 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4909 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4910 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4911 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4912 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4913 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4914 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4915 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4916 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4917 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4918 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4919 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4920 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4921 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4922 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4923 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4924 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4925 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4926 | return rc; |
| 4927 | } |
| 4928 | |
| 4929 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4930 | ** This routine is called for the root page of a btree when the root |
| 4931 | ** page contains no cells. This is an opportunity to make the tree |
| 4932 | ** shallower by one level. |
| 4933 | */ |
| 4934 | static int balance_shallower(MemPage *pPage){ |
| 4935 | MemPage *pChild; /* The only child page of pPage */ |
| 4936 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4937 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4938 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4939 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4940 | u8 **apCell; /* All cells from pages being balanced */ |
| 4941 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4942 | |
| 4943 | assert( pPage->pParent==0 ); |
| 4944 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4945 | pBt = pPage->pBt; |
| 4946 | mxCellPerPage = MX_CELL(pBt); |
| 4947 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4948 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4949 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4950 | if( pPage->leaf ){ |
| 4951 | /* The table is completely empty */ |
| 4952 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4953 | }else{ |
| 4954 | /* The root page is empty but has one child. Transfer the |
| 4955 | ** information from that one child into the root page if it |
| 4956 | ** will fit. This reduces the depth of the tree by one. |
| 4957 | ** |
| 4958 | ** If the root page is page 1, it has less space available than |
| 4959 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4960 | ** of the database fle), so it might not be able to hold all of the |
| 4961 | ** information currently contained in the child. If this is the |
| 4962 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4963 | ** for the right-pointer to the child page. The child page becomes |
| 4964 | ** the virtual root of the tree. |
| 4965 | */ |
| 4966 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4967 | assert( pgnoChild>0 ); |
| 4968 | assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) ); |
| 4969 | rc = getPage(pPage->pBt, pgnoChild, &pChild); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4970 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4971 | if( pPage->pgno==1 ){ |
| 4972 | rc = initPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4973 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4974 | assert( pChild->nOverflow==0 ); |
| 4975 | if( pChild->nFree>=100 ){ |
| 4976 | /* The child information will fit on the root page, so do the |
| 4977 | ** copy */ |
| 4978 | int i; |
| 4979 | zeroPage(pPage, pChild->aData[0]); |
| 4980 | for(i=0; i<pChild->nCell; i++){ |
| 4981 | apCell[i] = findCell(pChild,i); |
| 4982 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 4983 | } |
| 4984 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4985 | /* Copy the right-pointer of the child to the parent. */ |
| 4986 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 4987 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4988 | freePage(pChild); |
| 4989 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 4990 | }else{ |
| 4991 | /* The child has more information that will fit on the root. |
| 4992 | ** The tree is already balanced. Do nothing. */ |
| 4993 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 4994 | } |
| 4995 | }else{ |
| 4996 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 4997 | pPage->isInit = 0; |
| 4998 | pPage->pParent = 0; |
| 4999 | rc = initPage(pPage, 0); |
| 5000 | assert( rc==SQLITE_OK ); |
| 5001 | freePage(pChild); |
| 5002 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5003 | pChild->pgno, pPage->pgno)); |
| 5004 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5005 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5006 | assert( pPage->nOverflow==0 ); |
| 5007 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5008 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5009 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5010 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5011 | rc = ptrmapPutOvfl(pPage, i); |
| 5012 | if( rc!=SQLITE_OK ){ |
| 5013 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5014 | } |
| 5015 | } |
| 5016 | } |
| 5017 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5018 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5019 | releasePage(pChild); |
| 5020 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5021 | end_shallow_balance: |
| 5022 | sqliteFree(apCell); |
| 5023 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | |
| 5027 | /* |
| 5028 | ** The root page is overfull |
| 5029 | ** |
| 5030 | ** When this happens, Create a new child page and copy the |
| 5031 | ** contents of the root into the child. Then make the root |
| 5032 | ** page an empty page with rightChild pointing to the new |
| 5033 | ** child. Finally, call balance_internal() on the new child |
| 5034 | ** to cause it to split. |
| 5035 | */ |
| 5036 | static int balance_deeper(MemPage *pPage){ |
| 5037 | int rc; /* Return value from subprocedures */ |
| 5038 | MemPage *pChild; /* Pointer to a new child page */ |
| 5039 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5040 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5041 | int usableSize; /* Total usable size of a page */ |
| 5042 | u8 *data; /* Content of the parent page */ |
| 5043 | u8 *cdata; /* Content of the child page */ |
| 5044 | int hdr; /* Offset to page header in parent */ |
| 5045 | int brk; /* Offset to content of first cell in parent */ |
| 5046 | |
| 5047 | assert( pPage->pParent==0 ); |
| 5048 | assert( pPage->nOverflow>0 ); |
| 5049 | pBt = pPage->pBt; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5050 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5051 | if( rc ) return rc; |
| 5052 | assert( sqlite3pager_iswriteable(pChild->aData) ); |
| 5053 | usableSize = pBt->usableSize; |
| 5054 | data = pPage->aData; |
| 5055 | hdr = pPage->hdrOffset; |
| 5056 | brk = get2byte(&data[hdr+5]); |
| 5057 | cdata = pChild->aData; |
| 5058 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5059 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5060 | assert( pChild->isInit==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5061 | rc = initPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5062 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5063 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5064 | pChild->nOverflow = pPage->nOverflow; |
| 5065 | if( pChild->nOverflow ){ |
| 5066 | pChild->nFree = 0; |
| 5067 | } |
| 5068 | assert( pChild->nCell==pPage->nCell ); |
| 5069 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5070 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5071 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5072 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5073 | if( pBt->autoVacuum ){ |
| 5074 | int i; |
| 5075 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5076 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5077 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5078 | rc = ptrmapPutOvfl(pChild, i); |
| 5079 | if( rc!=SQLITE_OK ){ |
| 5080 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5084 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5085 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5086 | |
| 5087 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5088 | releasePage(pChild); |
| 5089 | return rc; |
| 5090 | } |
| 5091 | |
| 5092 | /* |
| 5093 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5094 | ** required, call the appropriate balancing routine. |
| 5095 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5096 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5097 | int rc = SQLITE_OK; |
| 5098 | if( pPage->pParent==0 ){ |
| 5099 | if( pPage->nOverflow>0 ){ |
| 5100 | rc = balance_deeper(pPage); |
| 5101 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5102 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5103 | rc = balance_shallower(pPage); |
| 5104 | } |
| 5105 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5106 | if( pPage->nOverflow>0 || |
| 5107 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5108 | rc = balance_nonroot(pPage); |
| 5109 | } |
| 5110 | } |
| 5111 | return rc; |
| 5112 | } |
| 5113 | |
| 5114 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5115 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5116 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5117 | ** database connection (a database connection that shares the pager |
| 5118 | ** cache with the current connection) and that other connection |
| 5119 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5120 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5121 | ** |
| 5122 | ** In addition to checking for read-locks (where a read-lock |
| 5123 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5124 | ** all cursors write cursors so that they are pointing to the |
| 5125 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5126 | ** or delete might change the number of cells on a page or delete |
| 5127 | ** a page entirely and we do not want to leave any cursors |
| 5128 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5129 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5130 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5131 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5132 | BtShared *pBt = pBtree->pBt; |
| 5133 | sqlite3 *db = pBtree->pSqlite; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5134 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5135 | if( p==pExclude ) continue; |
| 5136 | if( p->eState!=CURSOR_VALID ) continue; |
| 5137 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5138 | if( p->wrFlag==0 ){ |
| 5139 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5140 | if( dbOther==0 || |
| 5141 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5142 | return SQLITE_LOCKED; |
| 5143 | } |
| 5144 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5145 | moveToRoot(p); |
| 5146 | } |
| 5147 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5148 | return SQLITE_OK; |
| 5149 | } |
| 5150 | |
| 5151 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5152 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5153 | ** 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] | 5154 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5155 | ** is left pointing at a random location. |
| 5156 | ** |
| 5157 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5158 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5159 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5160 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5161 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5162 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5163 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5164 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5165 | int rc; |
| 5166 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5167 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5168 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5169 | BtShared *pBt = pCur->pBtree->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5170 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5171 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5172 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5173 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5174 | /* Must start a transaction before doing an insert */ |
| 5175 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5176 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5177 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5178 | if( !pCur->wrFlag ){ |
| 5179 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5180 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5181 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5182 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5183 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5184 | |
| 5185 | /* Save the positions of any other cursors open on this table */ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5186 | restoreOrClearCursorPosition(pCur, 0); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5187 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5188 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
| 5189 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc)) |
| 5190 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5191 | return rc; |
| 5192 | } |
| 5193 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5194 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5195 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5196 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5197 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5198 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5199 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5200 | assert( pPage->isInit ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5201 | rc = sqlite3pager_write(pPage->aData); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5202 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5203 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5204 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5205 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5206 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5207 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5208 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5209 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5210 | int szOld; |
| 5211 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5212 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5213 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5214 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5215 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5216 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5217 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5218 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5219 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5220 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5221 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5222 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5223 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5224 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5225 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5226 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5227 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5228 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5229 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5230 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5231 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5232 | if( rc==SQLITE_OK ){ |
| 5233 | moveToRoot(pCur); |
| 5234 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5235 | end_insert: |
| 5236 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5237 | return rc; |
| 5238 | } |
| 5239 | |
| 5240 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5241 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5242 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5243 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5244 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5245 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5246 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5247 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5248 | Pgno pgnoChild = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5249 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5250 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5251 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5252 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5253 | /* Must start a transaction before doing a delete */ |
| 5254 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5255 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5256 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5257 | if( pCur->idx >= pPage->nCell ){ |
| 5258 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5259 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5260 | if( !pCur->wrFlag ){ |
| 5261 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5262 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5263 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5264 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5265 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5266 | |
| 5267 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5268 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
| 5269 | ** open on the same table. Then call sqlite3pager_write() on the page |
| 5270 | ** that the entry will be deleted from. |
| 5271 | */ |
| 5272 | if( |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5273 | (rc = restoreOrClearCursorPosition(pCur, 1))!=0 || |
| 5274 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
| 5275 | (rc = sqlite3pager_write(pPage->aData))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5276 | ){ |
| 5277 | return rc; |
| 5278 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5279 | |
| 5280 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5281 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5282 | ** cell. The cell itself is still intact. |
| 5283 | */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5284 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5285 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5286 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5287 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5288 | rc = clearCell(pPage, pCell); |
| 5289 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5290 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5291 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5292 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5293 | ** 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] | 5294 | ** do something we will leave a hole on an internal page. |
| 5295 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5296 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5297 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5298 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5299 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5300 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5301 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5302 | ** initialized before use. Adding an extra initialization |
| 5303 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5304 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5305 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5306 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5307 | getTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5308 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5309 | if( rc!=SQLITE_OK ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5310 | if( rc!=SQLITE_NOMEM ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5311 | rc = SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5312 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5313 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5314 | if( rc==SQLITE_OK ){ |
| 5315 | rc = sqlite3pager_write(leafCur.pPage->aData); |
| 5316 | } |
| 5317 | if( rc==SQLITE_OK ){ |
| 5318 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5319 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5320 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
| 5321 | pNext = findCell(leafCur.pPage, leafCur.idx); |
| 5322 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5323 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 5324 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5325 | if( tempCell==0 ){ |
| 5326 | rc = SQLITE_NOMEM; |
| 5327 | } |
| 5328 | } |
| 5329 | if( rc==SQLITE_OK ){ |
| 5330 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5331 | } |
| 5332 | if( rc==SQLITE_OK ){ |
| 5333 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5334 | rc = balance(pPage, 0); |
| 5335 | } |
| 5336 | if( rc==SQLITE_OK ){ |
| 5337 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5338 | rc = balance(leafCur.pPage, 0); |
| 5339 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5340 | sqliteFree(tempCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5341 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5342 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5343 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5344 | pCur->pgnoRoot, pPage->pgno)); |
| 5345 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5346 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5347 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5348 | if( rc==SQLITE_OK ){ |
| 5349 | moveToRoot(pCur); |
| 5350 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5351 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5352 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5353 | |
| 5354 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5355 | ** Create a new BTree table. Write into *piTable the page |
| 5356 | ** number for the root page of the new table. |
| 5357 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5358 | ** The type of type is determined by the flags parameter. Only the |
| 5359 | ** following values of flags are currently in use. Other values for |
| 5360 | ** flags might not work: |
| 5361 | ** |
| 5362 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5363 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5364 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5365 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5366 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5367 | MemPage *pRoot; |
| 5368 | Pgno pgnoRoot; |
| 5369 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5370 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5371 | /* Must start a transaction first */ |
| 5372 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5373 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5374 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5375 | |
| 5376 | /* It is illegal to create a table if any cursors are open on the |
| 5377 | ** database. This is because in auto-vacuum mode the backend may |
| 5378 | ** need to move a database page to make room for the new root-page. |
| 5379 | ** If an open cursor was using the page a problem would occur. |
| 5380 | */ |
| 5381 | if( pBt->pCursor ){ |
| 5382 | return SQLITE_LOCKED; |
| 5383 | } |
| 5384 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5385 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5386 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5387 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5388 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5389 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5390 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5391 | MemPage *pPageMove; /* The page to move to. */ |
| 5392 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5393 | /* Read the value of meta[3] from the database to determine where the |
| 5394 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5395 | ** created so far, so the new root-page is (meta[3]+1). |
| 5396 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5397 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5398 | if( rc!=SQLITE_OK ) return rc; |
| 5399 | pgnoRoot++; |
| 5400 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5401 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5402 | ** PENDING_BYTE page. |
| 5403 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5404 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5405 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5406 | pgnoRoot++; |
| 5407 | } |
| 5408 | assert( pgnoRoot>=3 ); |
| 5409 | |
| 5410 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5411 | ** be moved to the allocated page (unless the allocated page happens |
| 5412 | ** to reside at pgnoRoot). |
| 5413 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5414 | rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5415 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5416 | return rc; |
| 5417 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5418 | |
| 5419 | if( pgnoMove!=pgnoRoot ){ |
| 5420 | u8 eType; |
| 5421 | Pgno iPtrPage; |
| 5422 | |
| 5423 | releasePage(pPageMove); |
| 5424 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 5425 | if( rc!=SQLITE_OK ){ |
| 5426 | return rc; |
| 5427 | } |
| 5428 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5429 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5430 | releasePage(pRoot); |
| 5431 | return rc; |
| 5432 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5433 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5434 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5435 | rc = sqlite3pager_write(pRoot->aData); |
| 5436 | if( rc!=SQLITE_OK ){ |
| 5437 | releasePage(pRoot); |
| 5438 | return rc; |
| 5439 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5440 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5441 | releasePage(pRoot); |
| 5442 | if( rc!=SQLITE_OK ){ |
| 5443 | return rc; |
| 5444 | } |
| 5445 | rc = getPage(pBt, pgnoRoot, &pRoot); |
| 5446 | if( rc!=SQLITE_OK ){ |
| 5447 | return rc; |
| 5448 | } |
| 5449 | rc = sqlite3pager_write(pRoot->aData); |
| 5450 | if( rc!=SQLITE_OK ){ |
| 5451 | releasePage(pRoot); |
| 5452 | return rc; |
| 5453 | } |
| 5454 | }else{ |
| 5455 | pRoot = pPageMove; |
| 5456 | } |
| 5457 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5458 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5459 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5460 | if( rc ){ |
| 5461 | releasePage(pRoot); |
| 5462 | return rc; |
| 5463 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5464 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5465 | if( rc ){ |
| 5466 | releasePage(pRoot); |
| 5467 | return rc; |
| 5468 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5469 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5470 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5471 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5472 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5473 | } |
| 5474 | #endif |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5475 | assert( sqlite3pager_iswriteable(pRoot->aData) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5476 | zeroPage(pRoot, flags | PTF_LEAF); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5477 | sqlite3pager_unref(pRoot->aData); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5478 | *piTable = (int)pgnoRoot; |
| 5479 | return SQLITE_OK; |
| 5480 | } |
| 5481 | |
| 5482 | /* |
| 5483 | ** Erase the given database page and all its children. Return |
| 5484 | ** the page to the freelist. |
| 5485 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5486 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5487 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5488 | Pgno pgno, /* Page number to clear */ |
| 5489 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5490 | int freePageFlag /* Deallocate page if true */ |
| 5491 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5492 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5493 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5494 | unsigned char *pCell; |
| 5495 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5496 | |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5497 | if( pgno>sqlite3pager_pagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5498 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5499 | } |
| 5500 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5501 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5502 | if( rc ) goto cleardatabasepage_out; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5503 | rc = sqlite3pager_write(pPage->aData); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5504 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5505 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5506 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5507 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5508 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5509 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5510 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5511 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5512 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5513 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5514 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5515 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5516 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5517 | } |
| 5518 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5519 | rc = freePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5520 | }else{ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5521 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5522 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5523 | |
| 5524 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5525 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5526 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5530 | ** Delete all information from a single table in the database. iTable is |
| 5531 | ** the page number of the root of the table. After this routine returns, |
| 5532 | ** the root page is empty, but still exists. |
| 5533 | ** |
| 5534 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5535 | ** read cursors on the table. Open write cursors are moved to the |
| 5536 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5537 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5538 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5539 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5540 | BtShared *pBt = p->pBt; |
| 5541 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5542 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5543 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5544 | rc = checkReadLocks(p, iTable, 0); |
| 5545 | if( rc ){ |
| 5546 | return rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5547 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5548 | |
| 5549 | /* Save the position of all cursors open on this table */ |
| 5550 | if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5551 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5552 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5553 | |
| 5554 | return clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | /* |
| 5558 | ** Erase all information in a table and add the root of the table to |
| 5559 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5560 | ** page 1) is never added to the freelist. |
| 5561 | ** |
| 5562 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5563 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5564 | ** |
| 5565 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5566 | ** root page in the database file, then the last root page |
| 5567 | ** in the database file is moved into the slot formerly occupied by |
| 5568 | ** iTable and that last slot formerly occupied by the last root page |
| 5569 | ** is added to the freelist instead of iTable. In this say, all |
| 5570 | ** root pages are kept at the beginning of the database file, which |
| 5571 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5572 | ** page number that used to be the last root page in the file before |
| 5573 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5574 | ** The last root page is recorded in meta[3] and the value of |
| 5575 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5576 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5577 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5578 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5579 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5580 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5581 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5582 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5583 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5584 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5585 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5586 | /* It is illegal to drop a table if any cursors are open on the |
| 5587 | ** database. This is because in auto-vacuum mode the backend may |
| 5588 | ** need to move another root-page to fill a gap left by the deleted |
| 5589 | ** root page. If an open cursor was using this page a problem would |
| 5590 | ** occur. |
| 5591 | */ |
| 5592 | if( pBt->pCursor ){ |
| 5593 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5594 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5595 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5596 | rc = getPage(pBt, (Pgno)iTable, &pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5597 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5598 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5599 | if( rc ){ |
| 5600 | releasePage(pPage); |
| 5601 | return rc; |
| 5602 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5603 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5604 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5605 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5606 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5607 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5608 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5609 | releasePage(pPage); |
| 5610 | #else |
| 5611 | if( pBt->autoVacuum ){ |
| 5612 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5613 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5614 | if( rc!=SQLITE_OK ){ |
| 5615 | releasePage(pPage); |
| 5616 | return rc; |
| 5617 | } |
| 5618 | |
| 5619 | if( iTable==maxRootPgno ){ |
| 5620 | /* If the table being dropped is the table with the largest root-page |
| 5621 | ** number in the database, put the root page on the free list. |
| 5622 | */ |
| 5623 | rc = freePage(pPage); |
| 5624 | releasePage(pPage); |
| 5625 | if( rc!=SQLITE_OK ){ |
| 5626 | return rc; |
| 5627 | } |
| 5628 | }else{ |
| 5629 | /* The table being dropped does not have the largest root-page |
| 5630 | ** number in the database. So move the page that does into the |
| 5631 | ** gap left by the deleted root-page. |
| 5632 | */ |
| 5633 | MemPage *pMove; |
| 5634 | releasePage(pPage); |
| 5635 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 5636 | if( rc!=SQLITE_OK ){ |
| 5637 | return rc; |
| 5638 | } |
| 5639 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 5640 | releasePage(pMove); |
| 5641 | if( rc!=SQLITE_OK ){ |
| 5642 | return rc; |
| 5643 | } |
| 5644 | rc = getPage(pBt, maxRootPgno, &pMove); |
| 5645 | if( rc!=SQLITE_OK ){ |
| 5646 | return rc; |
| 5647 | } |
| 5648 | rc = freePage(pMove); |
| 5649 | releasePage(pMove); |
| 5650 | if( rc!=SQLITE_OK ){ |
| 5651 | return rc; |
| 5652 | } |
| 5653 | *piMoved = maxRootPgno; |
| 5654 | } |
| 5655 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5656 | /* Set the new 'max-root-page' value in the database header. This |
| 5657 | ** is the old value less one, less one more if that happens to |
| 5658 | ** be a root-page number, less one again if that is the |
| 5659 | ** PENDING_BYTE_PAGE. |
| 5660 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5661 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5662 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 5663 | maxRootPgno--; |
| 5664 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5665 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5666 | maxRootPgno--; |
| 5667 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5668 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 5669 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5670 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5671 | }else{ |
| 5672 | rc = freePage(pPage); |
| 5673 | releasePage(pPage); |
| 5674 | } |
| 5675 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5676 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5677 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5678 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5679 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5680 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5681 | return rc; |
| 5682 | } |
| 5683 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5684 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5685 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5686 | ** Read the meta-information out of a database file. Meta[0] |
| 5687 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5688 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 5689 | ** is read-only, the others are read/write. |
| 5690 | ** |
| 5691 | ** The schema layer numbers meta values differently. At the schema |
| 5692 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 5693 | ** 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] | 5694 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5695 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5696 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5697 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5698 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5699 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5700 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 5701 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 5702 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 5703 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 5704 | */ |
| 5705 | rc = queryTableLock(p, 1, READ_LOCK); |
| 5706 | if( rc!=SQLITE_OK ){ |
| 5707 | return rc; |
| 5708 | } |
| 5709 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5710 | assert( idx>=0 && idx<=15 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5711 | rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5712 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5713 | *pMeta = get4byte(&pP1[36 + idx*4]); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5714 | sqlite3pager_unref(pP1); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5715 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5716 | /* If autovacuumed is disabled in this build but we are trying to |
| 5717 | ** access an autovacuumed database, then make the database readonly. |
| 5718 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5719 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5720 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5721 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5722 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5723 | /* Grab the read-lock on page 1. */ |
| 5724 | rc = lockTable(p, 1, READ_LOCK); |
| 5725 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
| 5728 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5729 | ** Write meta-information back into the database. Meta[0] is |
| 5730 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5731 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5732 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 5733 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5734 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5735 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5736 | assert( idx>=1 && idx<=15 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5737 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5738 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5739 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5740 | assert( pBt->pPage1!=0 ); |
| 5741 | pP1 = pBt->pPage1->aData; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5742 | rc = sqlite3pager_write(pP1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5743 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5744 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5745 | return SQLITE_OK; |
| 5746 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5747 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5748 | /* |
| 5749 | ** Return the flag byte at the beginning of the page that the cursor |
| 5750 | ** is currently pointing to. |
| 5751 | */ |
| 5752 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5753 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5754 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5755 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5756 | MemPage *pPage = pCur->pPage; |
| 5757 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 5758 | } |
| 5759 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5760 | #ifdef SQLITE_DEBUG |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5761 | /* |
| 5762 | ** Print a disassembly of the given page on standard output. This routine |
| 5763 | ** is used for debugging and testing only. |
| 5764 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5765 | static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5766 | int rc; |
| 5767 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5768 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5769 | int nFree; |
| 5770 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5771 | int hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5772 | int nCell; |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5773 | int isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5774 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5775 | char range[20]; |
| 5776 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5777 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5778 | rc = getPage(pBt, (Pgno)pgno, &pPage); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5779 | isInit = pPage->isInit; |
| 5780 | if( pPage->isInit==0 ){ |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5781 | initPage(pPage, pParent); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5782 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5783 | if( rc ){ |
| 5784 | return rc; |
| 5785 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5786 | hdr = pPage->hdrOffset; |
| 5787 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5788 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5789 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5790 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5791 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5792 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5793 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5794 | nCell = get2byte(&data[hdr+3]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5795 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5796 | data[hdr], data[hdr+7], |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5797 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5798 | assert( hdr == (pgno==1 ? 100 : 0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5799 | idx = hdr + 12 - pPage->leaf*4; |
| 5800 | for(i=0; i<nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5801 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5802 | Pgno child; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5803 | unsigned char *pCell; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5804 | int sz; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5805 | int addr; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5806 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5807 | addr = get2byte(&data[idx + 2*i]); |
| 5808 | pCell = &data[addr]; |
| 5809 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5810 | sz = info.nSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5811 | sprintf(range,"%d..%d", addr, addr+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5812 | if( pPage->leaf ){ |
| 5813 | child = 0; |
| 5814 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5815 | child = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5816 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5817 | sz = info.nData; |
| 5818 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5819 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5820 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5821 | for(j=0; j<sz; j++){ |
| 5822 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 5823 | } |
| 5824 | payload[sz] = 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5825 | sqlite3DebugPrintf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5826 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 5827 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5828 | ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5829 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5830 | if( !pPage->leaf ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5831 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5832 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5833 | nFree = 0; |
| 5834 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5835 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5836 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5837 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5838 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 5839 | nFree += sz; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5840 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5841 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5842 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5843 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5844 | } |
| 5845 | if( idx!=0 ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5846 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5847 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5848 | if( recursive && !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5849 | for(i=0; i<nCell; i++){ |
| 5850 | unsigned char *pCell = findCell(pPage, i); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5851 | btreePageDump(pBt, get4byte(pCell), 1, pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5852 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5853 | } |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5854 | btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5855 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5856 | pPage->isInit = isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5857 | sqlite3pager_unref(data); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 5858 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5859 | return SQLITE_OK; |
| 5860 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5861 | int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){ |
| 5862 | return btreePageDump(p->pBt, pgno, recursive, 0); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5863 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5864 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5865 | |
drh | 77bba59 | 2006-08-13 18:39:26 +0000 | [diff] [blame] | 5866 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5867 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5868 | ** Fill aResult[] with information about the entry and page that the |
| 5869 | ** cursor is pointing to. |
| 5870 | ** |
| 5871 | ** aResult[0] = The page number |
| 5872 | ** aResult[1] = The entry number |
| 5873 | ** aResult[2] = Total number of entries on this page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5874 | ** aResult[3] = Cell size (local payload + header) |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5875 | ** aResult[4] = Number of free bytes on this page |
| 5876 | ** aResult[5] = Number of free blocks on the page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5877 | ** aResult[6] = Total payload size (local + overflow) |
| 5878 | ** aResult[7] = Header size in bytes |
| 5879 | ** aResult[8] = Local payload size |
| 5880 | ** aResult[9] = Parent page number |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5881 | ** |
| 5882 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5883 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5884 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5885 | int cnt, idx; |
| 5886 | MemPage *pPage = pCur->pPage; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5887 | BtCursor tmpCur; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5888 | |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5889 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5890 | if( rc!=SQLITE_OK ){ |
| 5891 | return rc; |
| 5892 | } |
| 5893 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5894 | assert( pPage->isInit ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5895 | getTempCursor(pCur, &tmpCur); |
| 5896 | while( upCnt-- ){ |
| 5897 | moveToParent(&tmpCur); |
| 5898 | } |
| 5899 | pPage = tmpCur.pPage; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5900 | aResult[0] = sqlite3pager_pagenumber(pPage->aData); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5901 | assert( aResult[0]==pPage->pgno ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5902 | aResult[1] = tmpCur.idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5903 | aResult[2] = pPage->nCell; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5904 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 5905 | getCellInfo(&tmpCur); |
| 5906 | aResult[3] = tmpCur.info.nSize; |
| 5907 | aResult[6] = tmpCur.info.nData; |
| 5908 | aResult[7] = tmpCur.info.nHeader; |
| 5909 | aResult[8] = tmpCur.info.nLocal; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5910 | }else{ |
| 5911 | aResult[3] = 0; |
| 5912 | aResult[6] = 0; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5913 | aResult[7] = 0; |
| 5914 | aResult[8] = 0; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5915 | } |
| 5916 | aResult[4] = pPage->nFree; |
| 5917 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5918 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5919 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5920 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5921 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5922 | } |
| 5923 | aResult[5] = cnt; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5924 | if( pPage->pParent==0 || isRootPage(pPage) ){ |
| 5925 | aResult[9] = 0; |
| 5926 | }else{ |
| 5927 | aResult[9] = pPage->pParent->pgno; |
| 5928 | } |
| 5929 | releaseTempCursor(&tmpCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5930 | return SQLITE_OK; |
| 5931 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5932 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5933 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5934 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5935 | ** Return the pager associated with a BTree. This routine is used for |
| 5936 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5937 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5938 | Pager *sqlite3BtreePager(Btree *p){ |
| 5939 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5940 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5941 | |
| 5942 | /* |
| 5943 | ** This structure is passed around through all the sanity checking routines |
| 5944 | ** in order to keep track of some global state information. |
| 5945 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5946 | typedef struct IntegrityCk IntegrityCk; |
| 5947 | struct IntegrityCk { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5948 | BtShared *pBt; /* The tree being checked out */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 5949 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 5950 | int nPage; /* Number of pages in the database */ |
| 5951 | int *anRef; /* Number of times each page is referenced */ |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 5952 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5953 | }; |
| 5954 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5955 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5956 | /* |
| 5957 | ** Append a message to the error message string. |
| 5958 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5959 | static void checkAppendMsg( |
| 5960 | IntegrityCk *pCheck, |
| 5961 | char *zMsg1, |
| 5962 | const char *zFormat, |
| 5963 | ... |
| 5964 | ){ |
| 5965 | va_list ap; |
| 5966 | char *zMsg2; |
| 5967 | va_start(ap, zFormat); |
| 5968 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 5969 | va_end(ap); |
| 5970 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5971 | if( pCheck->zErrMsg ){ |
| 5972 | char *zOld = pCheck->zErrMsg; |
| 5973 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5974 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5975 | sqliteFree(zOld); |
| 5976 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5977 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5978 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5979 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5980 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5981 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5982 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5983 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5984 | /* |
| 5985 | ** Add 1 to the reference count for page iPage. If this is the second |
| 5986 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 5987 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 5988 | ** if this is the first reference to the page. |
| 5989 | ** |
| 5990 | ** Also check that the page number is in bounds. |
| 5991 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5992 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5993 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5994 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5995 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5996 | return 1; |
| 5997 | } |
| 5998 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5999 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6000 | return 1; |
| 6001 | } |
| 6002 | return (pCheck->anRef[iPage]++)>1; |
| 6003 | } |
| 6004 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6005 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6006 | /* |
| 6007 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6008 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6009 | ** to pCheck. |
| 6010 | */ |
| 6011 | static void checkPtrmap( |
| 6012 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6013 | Pgno iChild, /* Child page number */ |
| 6014 | u8 eType, /* Expected pointer map type */ |
| 6015 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6016 | char *zContext /* Context description (used for error msg) */ |
| 6017 | ){ |
| 6018 | int rc; |
| 6019 | u8 ePtrmapType; |
| 6020 | Pgno iPtrmapParent; |
| 6021 | |
| 6022 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6023 | if( rc!=SQLITE_OK ){ |
| 6024 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6025 | return; |
| 6026 | } |
| 6027 | |
| 6028 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6029 | checkAppendMsg(pCheck, zContext, |
| 6030 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6031 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6032 | } |
| 6033 | } |
| 6034 | #endif |
| 6035 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6036 | /* |
| 6037 | ** Check the integrity of the freelist or of an overflow page list. |
| 6038 | ** Verify that the number of pages on the list is N. |
| 6039 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6040 | static void checkList( |
| 6041 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6042 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6043 | int iPage, /* Page number for first page in the list */ |
| 6044 | int N, /* Expected number of pages in the list */ |
| 6045 | char *zContext /* Context for error messages */ |
| 6046 | ){ |
| 6047 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6048 | int expected = N; |
| 6049 | int iFirst = iPage; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6050 | while( N-- > 0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6051 | unsigned char *pOvfl; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6052 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6053 | checkAppendMsg(pCheck, zContext, |
| 6054 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6055 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6056 | break; |
| 6057 | } |
| 6058 | if( checkRef(pCheck, iPage, zContext) ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6059 | if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6060 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6061 | break; |
| 6062 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6063 | if( isFreeList ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6064 | int n = get4byte(&pOvfl[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6065 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6066 | if( pCheck->pBt->autoVacuum ){ |
| 6067 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6068 | } |
| 6069 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6070 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6071 | checkAppendMsg(pCheck, zContext, |
| 6072 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6073 | N--; |
| 6074 | }else{ |
| 6075 | for(i=0; i<n; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6076 | Pgno iFreePage = get4byte(&pOvfl[8+i*4]); |
| 6077 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6078 | if( pCheck->pBt->autoVacuum ){ |
| 6079 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6080 | } |
| 6081 | #endif |
| 6082 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6083 | } |
| 6084 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6085 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6086 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6087 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6088 | else{ |
| 6089 | /* If this database supports auto-vacuum and iPage is not the last |
| 6090 | ** page in this overflow list, check that the pointer-map entry for |
| 6091 | ** the following page matches iPage. |
| 6092 | */ |
| 6093 | if( pCheck->pBt->autoVacuum && N>0 ){ |
| 6094 | i = get4byte(pOvfl); |
| 6095 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6096 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6097 | } |
| 6098 | #endif |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6099 | iPage = get4byte(pOvfl); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6100 | sqlite3pager_unref(pOvfl); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6101 | } |
| 6102 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6103 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6104 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6105 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6106 | /* |
| 6107 | ** Do various sanity checks on a single page of a tree. Return |
| 6108 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6109 | ** return 1, and so forth. |
| 6110 | ** |
| 6111 | ** These checks are done: |
| 6112 | ** |
| 6113 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6114 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6115 | ** NO 2. Make sure cell keys are in order. |
| 6116 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6117 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6118 | ** 5. Check the integrity of overflow pages. |
| 6119 | ** 6. Recursively call checkTreePage on all children. |
| 6120 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6121 | ** 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] | 6122 | ** the root of the tree. |
| 6123 | */ |
| 6124 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6125 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6126 | int iPage, /* Page number of the page to check */ |
| 6127 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6128 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6129 | ){ |
| 6130 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6131 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6132 | int hdr, cellStart; |
| 6133 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6134 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6135 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6136 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6137 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6138 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6139 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6140 | sprintf(zContext, "Page %d: ", iPage); |
| 6141 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6142 | /* Check that the page exists |
| 6143 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6144 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6145 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6146 | if( iPage==0 ) return 0; |
| 6147 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6148 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6149 | checkAppendMsg(pCheck, zContext, |
| 6150 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6151 | return 0; |
| 6152 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6153 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6154 | checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6155 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6156 | return 0; |
| 6157 | } |
| 6158 | |
| 6159 | /* Check out all the cells. |
| 6160 | */ |
| 6161 | depth = 0; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6162 | for(i=0; i<pPage->nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6163 | u8 *pCell; |
| 6164 | int sz; |
| 6165 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6166 | |
| 6167 | /* Check payload overflow pages |
| 6168 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6169 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6170 | pCell = findCell(pPage,i); |
| 6171 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6172 | sz = info.nData; |
| 6173 | if( !pPage->intKey ) sz += info.nKey; |
| 6174 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6175 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6176 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6177 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6178 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6179 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6180 | } |
| 6181 | #endif |
| 6182 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6183 | } |
| 6184 | |
| 6185 | /* Check sanity of left child page. |
| 6186 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6187 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6188 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6189 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6190 | if( pBt->autoVacuum ){ |
| 6191 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6192 | } |
| 6193 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6194 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6195 | if( i>0 && d2!=depth ){ |
| 6196 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6197 | } |
| 6198 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6199 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6200 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6201 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6202 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6203 | sprintf(zContext, "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6204 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6205 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6206 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6207 | } |
| 6208 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6209 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6210 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6211 | |
| 6212 | /* Check for complete coverage of the page |
| 6213 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6214 | data = pPage->aData; |
| 6215 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6216 | hit = sqliteMalloc( usableSize ); |
| 6217 | if( hit ){ |
| 6218 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6219 | nCell = get2byte(&data[hdr+3]); |
| 6220 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6221 | for(i=0; i<nCell; i++){ |
| 6222 | int pc = get2byte(&data[cellStart+i*2]); |
| 6223 | int size = cellSizePtr(pPage, &data[pc]); |
| 6224 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6225 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6226 | checkAppendMsg(pCheck, 0, |
| 6227 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6228 | }else{ |
| 6229 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6230 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6231 | } |
| 6232 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6233 | cnt++){ |
| 6234 | int size = get2byte(&data[i+2]); |
| 6235 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6236 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6237 | checkAppendMsg(pCheck, 0, |
| 6238 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6239 | }else{ |
| 6240 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6241 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6242 | i = get2byte(&data[i]); |
| 6243 | } |
| 6244 | for(i=cnt=0; i<usableSize; i++){ |
| 6245 | if( hit[i]==0 ){ |
| 6246 | cnt++; |
| 6247 | }else if( hit[i]>1 ){ |
| 6248 | checkAppendMsg(pCheck, 0, |
| 6249 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6250 | break; |
| 6251 | } |
| 6252 | } |
| 6253 | if( cnt!=data[hdr+7] ){ |
| 6254 | checkAppendMsg(pCheck, 0, |
| 6255 | "Fragmented space is %d byte reported as %d on page %d", |
| 6256 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6257 | } |
| 6258 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6259 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6260 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6261 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6262 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6263 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6264 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6265 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6266 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6267 | /* |
| 6268 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6269 | ** an array of pages numbers were each page number is the root page of |
| 6270 | ** a table. nRoot is the number of entries in aRoot. |
| 6271 | ** |
| 6272 | ** If everything checks out, this routine returns NULL. If something is |
| 6273 | ** amiss, an error message is written into memory obtained from malloc() |
| 6274 | ** and a pointer to that error message is returned. The calling function |
| 6275 | ** is responsible for freeing the error message when it is done. |
| 6276 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6277 | char *sqlite3BtreeIntegrityCheck(Btree *p, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6278 | int i; |
| 6279 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6280 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6281 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6282 | |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6283 | nRef = sqlite3pager_refcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6284 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6285 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 6286 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6287 | sCheck.pBt = pBt; |
| 6288 | sCheck.pPager = pBt->pPager; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6289 | sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6290 | if( sCheck.nPage==0 ){ |
| 6291 | unlockBtreeIfUnused(pBt); |
| 6292 | return 0; |
| 6293 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 6294 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6295 | if( !sCheck.anRef ){ |
| 6296 | unlockBtreeIfUnused(pBt); |
| 6297 | return sqlite3MPrintf("Unable to malloc %d bytes", |
| 6298 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6299 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6300 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6301 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6302 | if( i<=sCheck.nPage ){ |
| 6303 | sCheck.anRef[i] = 1; |
| 6304 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6305 | sCheck.zErrMsg = 0; |
| 6306 | |
| 6307 | /* Check the integrity of the freelist |
| 6308 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6309 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6310 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6311 | |
| 6312 | /* Check all the tables. |
| 6313 | */ |
| 6314 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6315 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6316 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6317 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6318 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6319 | } |
| 6320 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6321 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6322 | } |
| 6323 | |
| 6324 | /* Make sure every page in the file is referenced |
| 6325 | */ |
| 6326 | for(i=1; i<=sCheck.nPage; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6327 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6328 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6329 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6330 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6331 | #else |
| 6332 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6333 | ** references to pointer-map pages. |
| 6334 | */ |
| 6335 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6336 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6337 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6338 | } |
| 6339 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6340 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6341 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6342 | } |
| 6343 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | /* Make sure this analysis did not leave any unref() pages |
| 6347 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6348 | unlockBtreeIfUnused(pBt); |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6349 | if( nRef != sqlite3pager_refcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6350 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6351 | "Outstanding page count goes from %d to %d during this analysis", |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 6352 | nRef, sqlite3pager_refcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6353 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | /* Clean up and report errors. |
| 6357 | */ |
| 6358 | sqliteFree(sCheck.anRef); |
| 6359 | return sCheck.zErrMsg; |
| 6360 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6361 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6362 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6363 | /* |
| 6364 | ** Return the full pathname of the underlying database file. |
| 6365 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6366 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6367 | assert( p->pBt->pPager!=0 ); |
| 6368 | return sqlite3pager_filename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6369 | } |
| 6370 | |
| 6371 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6372 | ** Return the pathname of the directory that contains the database file. |
| 6373 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6374 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6375 | assert( p->pBt->pPager!=0 ); |
| 6376 | return sqlite3pager_dirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6377 | } |
| 6378 | |
| 6379 | /* |
| 6380 | ** Return the pathname of the journal file for this database. The return |
| 6381 | ** value of this routine is the same regardless of whether the journal file |
| 6382 | ** has been created or not. |
| 6383 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6384 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6385 | assert( p->pBt->pPager!=0 ); |
| 6386 | return sqlite3pager_journalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6387 | } |
| 6388 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6389 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6390 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6391 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6392 | ** must be active for both files. |
| 6393 | ** |
| 6394 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6395 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6396 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6397 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6398 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6399 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6400 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6401 | BtShared *pBtTo = pTo->pBt; |
| 6402 | BtShared *pBtFrom = pFrom->pBt; |
| 6403 | |
| 6404 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6405 | return SQLITE_ERROR; |
| 6406 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6407 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6408 | nToPage = sqlite3pager_pagecount(pBtTo->pPager); |
| 6409 | nPage = sqlite3pager_pagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6410 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6411 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6412 | void *pPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6413 | if( i==iSkip ) continue; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6414 | rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6415 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6416 | rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6417 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6418 | sqlite3pager_unref(pPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6419 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6420 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
| 6421 | void *pPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6422 | if( i==iSkip ) continue; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6423 | rc = sqlite3pager_get(pBtTo->pPager, i, &pPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6424 | if( rc ) break; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6425 | rc = sqlite3pager_write(pPage); |
| 6426 | sqlite3pager_unref(pPage); |
| 6427 | sqlite3pager_dont_write(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6428 | } |
| 6429 | if( !rc && nPage<nToPage ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6430 | rc = sqlite3pager_truncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6431 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6432 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6433 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6434 | } |
| 6435 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6436 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6437 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6438 | |
| 6439 | /* |
| 6440 | ** Return non-zero if a transaction is active. |
| 6441 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6442 | int sqlite3BtreeIsInTrans(Btree *p){ |
| 6443 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6444 | } |
| 6445 | |
| 6446 | /* |
| 6447 | ** Return non-zero if a statement transaction is active. |
| 6448 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6449 | int sqlite3BtreeIsInStmt(Btree *p){ |
| 6450 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6451 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6452 | |
| 6453 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6454 | ** Return non-zero if a read (or write) transaction is active. |
| 6455 | */ |
| 6456 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
| 6457 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6458 | } |
| 6459 | |
| 6460 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6461 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 6462 | ** |
| 6463 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 6464 | ** the name of a master journal file that should be written into the |
| 6465 | ** individual journal file, or is NULL, indicating no master journal file |
| 6466 | ** (single database transaction). |
| 6467 | ** |
| 6468 | ** When this is called, the master journal should already have been |
| 6469 | ** created, populated with this journal pointer and synced to disk. |
| 6470 | ** |
| 6471 | ** Once this is routine has returned, the only thing required to commit |
| 6472 | ** the write-transaction for this database file is to delete the journal. |
| 6473 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6474 | int sqlite3BtreeSync(Btree *p, const char *zMaster){ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6475 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6476 | if( p->inTrans==TRANS_WRITE ){ |
| 6477 | BtShared *pBt = p->pBt; |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 6478 | Pgno nTrunc = 0; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6479 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6480 | if( pBt->autoVacuum ){ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6481 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 6482 | if( rc!=SQLITE_OK ){ |
| 6483 | return rc; |
| 6484 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6485 | } |
| 6486 | #endif |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6487 | rc = sqlite3pager_sync(pBt->pPager, zMaster, nTrunc); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6488 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6489 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6490 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6491 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6492 | /* |
| 6493 | ** This function returns a pointer to a blob of memory associated with |
| 6494 | ** a single shared-btree. The memory is used by client code for it's own |
| 6495 | ** purposes (for example, to store a high-level schema associated with |
| 6496 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6497 | ** |
| 6498 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6499 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6500 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6501 | ** of memory returned. |
| 6502 | ** |
| 6503 | ** Just before the shared-btree is closed, the function passed as the |
| 6504 | ** xFree argument when the memory allocation was made is invoked on the |
| 6505 | ** blob of allocated memory. This function should not call sqliteFree() |
| 6506 | ** on the memory, the btree layer does that. |
| 6507 | */ |
| 6508 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6509 | BtShared *pBt = p->pBt; |
| 6510 | if( !pBt->pSchema ){ |
| 6511 | pBt->pSchema = sqliteMalloc(nBytes); |
| 6512 | pBt->xFreeSchema = xFree; |
| 6513 | } |
| 6514 | return pBt->pSchema; |
| 6515 | } |
| 6516 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6517 | /* |
| 6518 | ** Return true if another user of the same shared btree as the argument |
| 6519 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6520 | */ |
| 6521 | int sqlite3BtreeSchemaLocked(Btree *p){ |
| 6522 | return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6523 | } |
| 6524 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6525 | |
| 6526 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6527 | /* |
| 6528 | ** Obtain a lock on the table whose root page is iTab. The |
| 6529 | ** lock is a write lock if isWritelock is true or a read lock |
| 6530 | ** if it is false. |
| 6531 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6532 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6533 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6534 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6535 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6536 | if( rc==SQLITE_OK ){ |
| 6537 | rc = lockTable(p, iTab, lockType); |
| 6538 | } |
| 6539 | return rc; |
| 6540 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6541 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6542 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6543 | /* |
| 6544 | ** The following debugging interface has to be in this file (rather |
| 6545 | ** than in, for example, test1.c) so that it can get access to |
| 6546 | ** the definition of BtShared. |
| 6547 | */ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 6548 | #if defined(SQLITE_DEBUG) && defined(TCLSH) |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6549 | #include <tcl.h> |
| 6550 | int sqlite3_shared_cache_report( |
| 6551 | void * clientData, |
| 6552 | Tcl_Interp *interp, |
| 6553 | int objc, |
| 6554 | Tcl_Obj *CONST objv[] |
| 6555 | ){ |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6556 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6557 | const ThreadData *pTd = sqlite3ThreadDataReadOnly(); |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6558 | if( pTd->useSharedData ){ |
| 6559 | BtShared *pBt; |
| 6560 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 6561 | for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){ |
| 6562 | const char *zFile = sqlite3pager_filename(pBt->pPager); |
| 6563 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1)); |
| 6564 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef)); |
| 6565 | } |
| 6566 | Tcl_SetObjResult(interp, pRet); |
| 6567 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6568 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6569 | return TCL_OK; |
| 6570 | } |
| 6571 | #endif |