drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.341 2007/03/19 17:44:27 danielk1977 Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
| 15 | ** For a detailed discussion of BTrees, refer to |
| 16 | ** |
| 17 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 18 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 19 | ** Publishing Company, Reading, Massachusetts. |
| 20 | ** |
| 21 | ** The basic idea is that each page of the file contains N database |
| 22 | ** entries and N+1 pointers to subpages. |
| 23 | ** |
| 24 | ** ---------------------------------------------------------------- |
| 25 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) | |
| 26 | ** ---------------------------------------------------------------- |
| 27 | ** |
| 28 | ** All of the keys on the page that Ptr(0) points to have values less |
| 29 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
| 30 | ** values greater than Key(0) and less than Key(1). All of the keys |
| 31 | ** on Ptr(N+1) and its subpages have values greater than Key(N). And |
| 32 | ** so forth. |
| 33 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 34 | ** Finding a particular key requires reading O(log(M)) pages from the |
| 35 | ** disk where M is the number of entries in the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** In this implementation, a single file can hold one or more separate |
| 38 | ** BTrees. Each BTree is identified by the index of its root page. The |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 39 | ** key and data for any entry are combined to form the "payload". A |
| 40 | ** fixed amount of payload can be carried directly on the database |
| 41 | ** page. If the payload is larger than the preset amount then surplus |
| 42 | ** bytes are stored on overflow pages. The payload for an entry |
| 43 | ** and the preceding pointer are combined to form a "Cell". Each |
| 44 | ** page has a small header which contains the Ptr(N+1) pointer and other |
| 45 | ** information such as the size of key and data. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 46 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 47 | ** FORMAT DETAILS |
| 48 | ** |
| 49 | ** The file is divided into pages. The first page is called page 1, |
| 50 | ** the second is page 2, and so forth. A page number of zero indicates |
| 51 | ** "no such page". The page size can be anything between 512 and 65536. |
| 52 | ** Each page can be either a btree page, a freelist page or an overflow |
| 53 | ** page. |
| 54 | ** |
| 55 | ** The first page is always a btree page. The first 100 bytes of the first |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 56 | ** page contain a special header (the "file header") that describes the file. |
| 57 | ** The format of the file header is as follows: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 58 | ** |
| 59 | ** OFFSET SIZE DESCRIPTION |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 60 | ** 0 16 Header string: "SQLite format 3\000" |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 61 | ** 16 2 Page size in bytes. |
| 62 | ** 18 1 File format write version |
| 63 | ** 19 1 File format read version |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 64 | ** 20 1 Bytes of unused space at the end of each page |
| 65 | ** 21 1 Max embedded payload fraction |
| 66 | ** 22 1 Min embedded payload fraction |
| 67 | ** 23 1 Min leaf payload fraction |
| 68 | ** 24 4 File change counter |
| 69 | ** 28 4 Reserved for future use |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 70 | ** 32 4 First freelist page |
| 71 | ** 36 4 Number of freelist pages in the file |
| 72 | ** 40 60 15 4-byte meta values passed to higher layers |
| 73 | ** |
| 74 | ** All of the integer values are big-endian (most significant byte first). |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 75 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 76 | ** The file change counter is incremented when the database is changed more |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 77 | ** than once within the same second. This counter, together with the |
| 78 | ** modification time of the file, allows other processes to know |
| 79 | ** when the file has changed and thus when they need to flush their |
| 80 | ** cache. |
| 81 | ** |
| 82 | ** The max embedded payload fraction is the amount of the total usable |
| 83 | ** space in a page that can be consumed by a single cell for standard |
| 84 | ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default |
| 85 | ** is to limit the maximum cell size so that at least 4 cells will fit |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 86 | ** on one page. Thus the default max embedded payload fraction is 64. |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 87 | ** |
| 88 | ** If the payload for a cell is larger than the max payload, then extra |
| 89 | ** payload is spilled to overflow pages. Once an overflow page is allocated, |
| 90 | ** as many bytes as possible are moved into the overflow pages without letting |
| 91 | ** the cell size drop below the min embedded payload fraction. |
| 92 | ** |
| 93 | ** The min leaf payload fraction is like the min embedded payload fraction |
| 94 | ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum |
| 95 | ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it |
| 96 | ** not specified in the header. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 97 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 98 | ** Each btree pages is divided into three sections: The header, the |
| 99 | ** cell pointer array, and the cell area area. Page 1 also has a 100-byte |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 100 | ** file header that occurs before the page header. |
| 101 | ** |
| 102 | ** |----------------| |
| 103 | ** | file header | 100 bytes. Page 1 only. |
| 104 | ** |----------------| |
| 105 | ** | page header | 8 bytes for leaves. 12 bytes for interior nodes |
| 106 | ** |----------------| |
| 107 | ** | cell pointer | | 2 bytes per cell. Sorted order. |
| 108 | ** | array | | Grows downward |
| 109 | ** | | v |
| 110 | ** |----------------| |
| 111 | ** | unallocated | |
| 112 | ** | space | |
| 113 | ** |----------------| ^ Grows upwards |
| 114 | ** | cell content | | Arbitrary order interspersed with freeblocks. |
| 115 | ** | area | | and free space fragments. |
| 116 | ** |----------------| |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 117 | ** |
| 118 | ** The page headers looks like this: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 119 | ** |
| 120 | ** OFFSET SIZE DESCRIPTION |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 121 | ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 122 | ** 1 2 byte offset to the first freeblock |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 123 | ** 3 2 number of cells on this page |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 124 | ** 5 2 first byte of the cell content area |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 125 | ** 7 1 number of fragmented free bytes |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 126 | ** 8 4 Right child (the Ptr(N+1) value). Omitted on leaves. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 127 | ** |
| 128 | ** The flags define the format of this btree page. The leaf flag means that |
| 129 | ** this page has no children. The zerodata flag means that this page carries |
drh | 44f87bd | 2004-09-27 13:19:51 +0000 | [diff] [blame] | 130 | ** only keys and no data. The intkey flag means that the key is a integer |
| 131 | ** which is stored in the key size entry of the cell header rather than in |
| 132 | ** the payload area. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 133 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 134 | ** The cell pointer array begins on the first byte after the page header. |
| 135 | ** The cell pointer array contains zero or more 2-byte numbers which are |
| 136 | ** offsets from the beginning of the page to the cell content in the cell |
| 137 | ** content area. The cell pointers occur in sorted order. The system strives |
| 138 | ** to keep free space after the last cell pointer so that new cells can |
drh | 44f87bd | 2004-09-27 13:19:51 +0000 | [diff] [blame] | 139 | ** be easily added without having to defragment the page. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 140 | ** |
| 141 | ** Cell content is stored at the very end of the page and grows toward the |
| 142 | ** beginning of the page. |
| 143 | ** |
| 144 | ** Unused space within the cell content area is collected into a linked list of |
| 145 | ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset |
| 146 | ** to the first freeblock is given in the header. Freeblocks occur in |
| 147 | ** increasing order. Because a freeblock must be at least 4 bytes in size, |
| 148 | ** any group of 3 or fewer unused bytes in the cell content area cannot |
| 149 | ** exist on the freeblock chain. A group of 3 or fewer free bytes is called |
| 150 | ** a fragment. The total number of bytes in all fragments is recorded. |
| 151 | ** in the page header at offset 7. |
| 152 | ** |
| 153 | ** SIZE DESCRIPTION |
| 154 | ** 2 Byte offset of the next freeblock |
| 155 | ** 2 Bytes in this freeblock |
| 156 | ** |
| 157 | ** Cells are of variable length. Cells are stored in the cell content area at |
| 158 | ** the end of the page. Pointers to the cells are in the cell pointer array |
| 159 | ** that immediately follows the page header. Cells is not necessarily |
| 160 | ** contiguous or in order, but cell pointers are contiguous and in order. |
| 161 | ** |
| 162 | ** Cell content makes use of variable length integers. A variable |
| 163 | ** length integer is 1 to 9 bytes where the lower 7 bits of each |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 164 | ** byte are used. The integer consists of all bytes that have bit 8 set and |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 165 | ** the first byte with bit 8 clear. The most significant byte of the integer |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 166 | ** appears first. A variable-length integer may not be more than 9 bytes long. |
| 167 | ** As a special case, all 8 bytes of the 9th byte are used as data. This |
| 168 | ** allows a 64-bit integer to be encoded in 9 bytes. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 169 | ** |
| 170 | ** 0x00 becomes 0x00000000 |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 171 | ** 0x7f becomes 0x0000007f |
| 172 | ** 0x81 0x00 becomes 0x00000080 |
| 173 | ** 0x82 0x00 becomes 0x00000100 |
| 174 | ** 0x80 0x7f becomes 0x0000007f |
| 175 | ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678 |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 176 | ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081 |
| 177 | ** |
| 178 | ** Variable length integers are used for rowids and to hold the number of |
| 179 | ** bytes of key and data in a btree cell. |
| 180 | ** |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 181 | ** The content of a cell looks like this: |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 182 | ** |
| 183 | ** SIZE DESCRIPTION |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 184 | ** 4 Page number of the left child. Omitted if leaf flag is set. |
| 185 | ** var Number of bytes of data. Omitted if the zerodata flag is set. |
| 186 | ** var Number of bytes of key. Or the key itself if intkey flag is set. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 187 | ** * Payload |
| 188 | ** 4 First page of the overflow chain. Omitted if no overflow |
| 189 | ** |
| 190 | ** Overflow pages form a linked list. Each page except the last is completely |
| 191 | ** filled with data (pagesize - 4 bytes). The last page can have as little |
| 192 | ** as 1 byte of data. |
| 193 | ** |
| 194 | ** SIZE DESCRIPTION |
| 195 | ** 4 Page number of next overflow page |
| 196 | ** * Data |
| 197 | ** |
| 198 | ** Freelist pages come in two subtypes: trunk pages and leaf pages. The |
| 199 | ** file header points to first in a linked list of trunk page. Each trunk |
| 200 | ** page points to multiple leaf pages. The content of a leaf page is |
| 201 | ** unspecified. A trunk page looks like this: |
| 202 | ** |
| 203 | ** SIZE DESCRIPTION |
| 204 | ** 4 Page number of next trunk page |
| 205 | ** 4 Number of leaf pointers on this page |
| 206 | ** * zero or more pages numbers of leaves |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 207 | */ |
| 208 | #include "sqliteInt.h" |
| 209 | #include "pager.h" |
| 210 | #include "btree.h" |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 211 | #include "os.h" |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 212 | #include <assert.h> |
| 213 | |
drh | 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 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 294 | DbPage *pDbPage; /* Pager page handle */ |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 295 | Pgno pgno; /* Page number for this page */ |
| 296 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 297 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 298 | |
| 299 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 300 | ** The in-memory image of a disk page has the auxiliary information appended |
| 301 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 302 | ** that extra information. |
| 303 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 304 | #define EXTRA_SIZE sizeof(MemPage) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 305 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 306 | /* Btree handle */ |
| 307 | struct Btree { |
| 308 | sqlite3 *pSqlite; |
| 309 | BtShared *pBt; |
| 310 | u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */ |
| 311 | }; |
| 312 | |
| 313 | /* |
| 314 | ** Btree.inTrans may take one of the following values. |
| 315 | ** |
| 316 | ** If the shared-data extension is enabled, there may be multiple users |
| 317 | ** of the Btree structure. At most one of these may open a write transaction, |
| 318 | ** but any number may have active read transactions. Variable Btree.pDb |
| 319 | ** points to the handle that owns any current write-transaction. |
| 320 | */ |
| 321 | #define TRANS_NONE 0 |
| 322 | #define TRANS_READ 1 |
| 323 | #define TRANS_WRITE 2 |
| 324 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 325 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 326 | ** Everything we need to know about an open database |
| 327 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 328 | struct BtShared { |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 329 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 330 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 331 | MemPage *pPage1; /* First page of the database */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 332 | u8 inStmt; /* True if we are in a statement subtransaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 333 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 334 | u8 maxEmbedFrac; /* Maximum payload as % of total page size */ |
| 335 | u8 minEmbedFrac; /* Minimum payload as % of total page size */ |
| 336 | u8 minLeafFrac; /* Minimum leaf payload as % of total page size */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 337 | u8 pageSizeFixed; /* True if the page size can no longer be changed */ |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 338 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 339 | u8 autoVacuum; /* True if database supports auto-vacuum */ |
| 340 | #endif |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 341 | u16 pageSize; /* Total number of bytes on a page */ |
| 342 | u16 usableSize; /* Number of usable bytes on each page */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 343 | int maxLocal; /* Maximum local payload in non-LEAFDATA tables */ |
| 344 | int minLocal; /* Minimum local payload in non-LEAFDATA tables */ |
| 345 | int maxLeaf; /* Maximum local payload in a LEAFDATA table */ |
| 346 | int minLeaf; /* Minimum local payload in a LEAFDATA table */ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 347 | BusyHandler *pBusyHandler; /* Callback for when there is lock contention */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 348 | u8 inTransaction; /* Transaction state */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 349 | int nRef; /* Number of references to this structure */ |
| 350 | int nTransaction; /* Number of open transactions (read + write) */ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 351 | void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */ |
| 352 | void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */ |
| 353 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 354 | BtLock *pLock; /* List of locks held on this shared-btree struct */ |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 355 | BtShared *pNext; /* Next in ThreadData.pBtree linked list */ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 356 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 357 | }; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 358 | |
| 359 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 360 | ** An instance of the following structure is used to hold information |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 361 | ** about a cell. The parseCellPtr() function fills in this structure |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 362 | ** based on information extract from the raw disk page. |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 363 | */ |
| 364 | typedef struct CellInfo CellInfo; |
| 365 | struct CellInfo { |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 366 | u8 *pCell; /* Pointer to the start of cell content */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 367 | i64 nKey; /* The key for INTKEY tables, or number of bytes in key */ |
| 368 | u32 nData; /* Number of bytes of data */ |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 369 | u32 nPayload; /* Total amount of payload */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 370 | u16 nHeader; /* Size of the cell content header in bytes */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 371 | u16 nLocal; /* Amount of payload held locally */ |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 372 | u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 373 | u16 nSize; /* Size of the cell content on the main b-tree page */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | /* |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 377 | ** A cursor is a pointer to a particular entry in the BTree. |
| 378 | ** The entry is identified by its MemPage and the index in |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 379 | ** MemPage.aCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 380 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 381 | struct BtCursor { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 382 | Btree *pBtree; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 383 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 384 | int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */ |
| 385 | void *pArg; /* First arg to xCompare() */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 386 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 387 | MemPage *pPage; /* Page that contains the entry */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 388 | int idx; /* Index of the entry in pPage->aCell[] */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 389 | CellInfo info; /* A parse of the cell we are pointing at */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 390 | u8 wrFlag; /* True if writable */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 391 | u8 eState; /* One of the CURSOR_XXX constants (see below) */ |
drh | 4eeb1ff | 2006-03-23 14:03:00 +0000 | [diff] [blame] | 392 | void *pKey; /* Saved key that was cursor's last known position */ |
| 393 | i64 nKey; /* Size of pKey, or last integer key */ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 394 | int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 395 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 396 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 397 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 398 | ** Potential values for BtCursor.eState. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 399 | ** |
| 400 | ** CURSOR_VALID: |
| 401 | ** Cursor points to a valid entry. getPayload() etc. may be called. |
| 402 | ** |
| 403 | ** CURSOR_INVALID: |
| 404 | ** Cursor does not point to a valid entry. This can happen (for example) |
| 405 | ** because the table is empty or because BtreeCursorFirst() has not been |
| 406 | ** called. |
| 407 | ** |
| 408 | ** CURSOR_REQUIRESEEK: |
| 409 | ** The table that this cursor was opened on still exists, but has been |
| 410 | ** modified since the cursor was last used. The cursor position is saved |
| 411 | ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in |
danielk1977 | 955de52 | 2006-02-10 02:27:42 +0000 | [diff] [blame] | 412 | ** this state, restoreOrClearCursorPosition() can be called to attempt to |
| 413 | ** seek the cursor to the saved position. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 414 | */ |
| 415 | #define CURSOR_INVALID 0 |
| 416 | #define CURSOR_VALID 1 |
| 417 | #define CURSOR_REQUIRESEEK 2 |
| 418 | |
| 419 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 420 | ** The TRACE macro will print high-level status information about the |
| 421 | ** btree operation when the global variable sqlite3_btree_trace is |
| 422 | ** enabled. |
| 423 | */ |
| 424 | #if SQLITE_TEST |
| 425 | # define TRACE(X) if( sqlite3_btree_trace )\ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 426 | /* { sqlite3DebugPrintf X; fflush(stdout); } */ \ |
| 427 | { printf X; fflush(stdout); } |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 428 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 429 | #else |
| 430 | # define TRACE(X) |
| 431 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 434 | ** Forward declaration |
| 435 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 436 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 437 | |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 438 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 439 | ** Read or write a two- and four-byte big-endian integer values. |
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 get2byte(unsigned char *p){ |
| 442 | return (p[0]<<8) | p[1]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 443 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 444 | static u32 get4byte(unsigned char *p){ |
| 445 | return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 446 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 447 | static void put2byte(unsigned char *p, u32 v){ |
| 448 | p[0] = v>>8; |
| 449 | p[1] = v; |
| 450 | } |
| 451 | static void put4byte(unsigned char *p, u32 v){ |
| 452 | p[0] = v>>24; |
| 453 | p[1] = v>>16; |
| 454 | p[2] = v>>8; |
| 455 | p[3] = v; |
| 456 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 457 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 458 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 459 | ** Routines to read and write variable-length integers. These used to |
| 460 | ** be defined locally, but now we use the varint routines in the util.c |
| 461 | ** file. |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 462 | */ |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 463 | #define getVarint sqlite3GetVarint |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 464 | /* #define getVarint32 sqlite3GetVarint32 */ |
| 465 | #define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B)) |
drh | 6d2fb15 | 2004-05-14 16:50:06 +0000 | [diff] [blame] | 466 | #define putVarint sqlite3PutVarint |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 467 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 468 | /* The database page the PENDING_BYTE occupies. This page is never used. |
| 469 | ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They |
| 470 | ** should possibly be consolidated (presumably in pager.h). |
drh | fe9a914 | 2006-03-14 12:59:10 +0000 | [diff] [blame] | 471 | ** |
| 472 | ** If disk I/O is omitted (meaning that the database is stored purely |
| 473 | ** in memory) then there is no pending byte. |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 474 | */ |
drh | fe9a914 | 2006-03-14 12:59:10 +0000 | [diff] [blame] | 475 | #ifdef SQLITE_OMIT_DISKIO |
| 476 | # define PENDING_BYTE_PAGE(pBt) 0x7fffffff |
| 477 | #else |
| 478 | # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1) |
| 479 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 480 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 481 | /* |
| 482 | ** A linked list of the following structures is stored at BtShared.pLock. |
| 483 | ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor |
| 484 | ** is opened on the table with root page BtShared.iTable. Locks are removed |
| 485 | ** from this list when a transaction is committed or rolled back, or when |
| 486 | ** a btree handle is closed. |
| 487 | */ |
| 488 | struct BtLock { |
| 489 | Btree *pBtree; /* Btree handle holding this lock */ |
| 490 | Pgno iTable; /* Root page of table */ |
| 491 | u8 eLock; /* READ_LOCK or WRITE_LOCK */ |
| 492 | BtLock *pNext; /* Next in BtShared.pLock list */ |
| 493 | }; |
| 494 | |
| 495 | /* Candidate values for BtLock.eLock */ |
| 496 | #define READ_LOCK 1 |
| 497 | #define WRITE_LOCK 2 |
| 498 | |
| 499 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 500 | /* |
| 501 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 502 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 503 | ** shared-cache table level locks. If the library is compiled with the |
| 504 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 505 | ** of each BtShared structure and so this locking is not necessary. |
| 506 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 507 | */ |
| 508 | #define queryTableLock(a,b,c) SQLITE_OK |
| 509 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 510 | #define unlockAllTables(a) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 511 | #else |
| 512 | |
danielk1977 | e725929 | 2006-01-13 06:33:23 +0000 | [diff] [blame] | 513 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 514 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 515 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 516 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 517 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 518 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 519 | */ |
| 520 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 521 | BtShared *pBt = p->pBt; |
| 522 | BtLock *pIter; |
| 523 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 524 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 525 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 526 | return SQLITE_OK; |
| 527 | } |
| 528 | |
| 529 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 530 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 531 | ** set, it is unconditionally granted - even if there are write-locks |
| 532 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 533 | ** is not considered. |
| 534 | ** |
| 535 | ** In function lockTable(), if a read-lock is demanded and the |
| 536 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 537 | ** (BtShared.pLock). |
| 538 | ** |
| 539 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 540 | ** not create or respect table locks. The locking procedure for a |
| 541 | ** write-cursor does not change. |
| 542 | */ |
| 543 | if( |
| 544 | !p->pSqlite || |
| 545 | 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) || |
| 546 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 547 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 548 | ){ |
| 549 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 550 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 551 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 552 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 553 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | return SQLITE_OK; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 561 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 562 | ** WRITE_LOCK. |
| 563 | ** |
| 564 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 565 | ** SQLITE_NOMEM may also be returned. |
| 566 | */ |
| 567 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 568 | BtShared *pBt = p->pBt; |
| 569 | BtLock *pLock = 0; |
| 570 | BtLock *pIter; |
| 571 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 572 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 573 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 574 | return SQLITE_OK; |
| 575 | } |
| 576 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 577 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 578 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 579 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 580 | ** return early without adding an entry to the BtShared.pLock list. See |
| 581 | ** comment in function queryTableLock() for more info on handling |
| 582 | ** the ReadUncommitted flag. |
| 583 | */ |
| 584 | if( |
| 585 | (p->pSqlite) && |
| 586 | (p->pSqlite->flags&SQLITE_ReadUncommitted) && |
| 587 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 588 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 589 | ){ |
| 590 | return SQLITE_OK; |
| 591 | } |
| 592 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 593 | /* First search the list for an existing lock on this table. */ |
| 594 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 595 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 596 | pLock = pIter; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* If the above search did not find a BtLock struct associating Btree p |
| 602 | ** with table iTable, allocate one and link it into the list. |
| 603 | */ |
| 604 | if( !pLock ){ |
| 605 | pLock = (BtLock *)sqliteMalloc(sizeof(BtLock)); |
| 606 | if( !pLock ){ |
| 607 | return SQLITE_NOMEM; |
| 608 | } |
| 609 | pLock->iTable = iTable; |
| 610 | pLock->pBtree = p; |
| 611 | pLock->pNext = pBt->pLock; |
| 612 | pBt->pLock = pLock; |
| 613 | } |
| 614 | |
| 615 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 616 | ** and the requested lock. This means if a write-lock was already held |
| 617 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 618 | */ |
| 619 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 620 | if( eLock>pLock->eLock ){ |
| 621 | pLock->eLock = eLock; |
| 622 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 623 | |
| 624 | return SQLITE_OK; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 629 | ** procedure) held by Btree handle p. |
| 630 | */ |
| 631 | static void unlockAllTables(Btree *p){ |
| 632 | BtLock **ppIter = &p->pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 633 | |
| 634 | /* If the shared-cache extension is not enabled, there should be no |
| 635 | ** locks in the BtShared.pLock list, making this procedure a no-op. Assert |
| 636 | ** that this is the case. |
| 637 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 638 | assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 639 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 640 | while( *ppIter ){ |
| 641 | BtLock *pLock = *ppIter; |
| 642 | if( pLock->pBtree==p ){ |
| 643 | *ppIter = pLock->pNext; |
| 644 | sqliteFree(pLock); |
| 645 | }else{ |
| 646 | ppIter = &pLock->pNext; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 651 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 652 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 653 | |
| 654 | /* |
| 655 | ** Save the current cursor position in the variables BtCursor.nKey |
| 656 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 657 | */ |
| 658 | static int saveCursorPosition(BtCursor *pCur){ |
| 659 | int rc; |
| 660 | |
| 661 | assert( CURSOR_VALID==pCur->eState ); |
| 662 | assert( 0==pCur->pKey ); |
| 663 | |
| 664 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 665 | |
| 666 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 667 | ** stores the integer key in pCur->nKey. In this case this value is |
| 668 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 669 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 670 | ** data. |
| 671 | */ |
| 672 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
| 673 | void *pKey = sqliteMalloc(pCur->nKey); |
| 674 | if( pKey ){ |
| 675 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 676 | if( rc==SQLITE_OK ){ |
| 677 | pCur->pKey = pKey; |
| 678 | }else{ |
| 679 | sqliteFree(pKey); |
| 680 | } |
| 681 | }else{ |
| 682 | rc = SQLITE_NOMEM; |
| 683 | } |
| 684 | } |
| 685 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 686 | |
| 687 | if( rc==SQLITE_OK ){ |
| 688 | releasePage(pCur->pPage); |
| 689 | pCur->pPage = 0; |
| 690 | pCur->eState = CURSOR_REQUIRESEEK; |
| 691 | } |
| 692 | |
| 693 | return rc; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | ** Save the positions of all cursors except pExcept open on the table |
| 698 | ** with root-page iRoot. Usually, this is called just before cursor |
| 699 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 700 | */ |
| 701 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 702 | BtCursor *p; |
| 703 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 704 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 705 | p->eState==CURSOR_VALID ){ |
| 706 | int rc = saveCursorPosition(p); |
| 707 | if( SQLITE_OK!=rc ){ |
| 708 | return rc; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | return SQLITE_OK; |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 717 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 718 | ** saved position info stored by saveCursorPosition(), so there can be |
| 719 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 720 | ** saveCursorPosition(). |
| 721 | ** |
| 722 | ** If the second argument argument - doSeek - is false, then instead of |
| 723 | ** returning the cursor to it's saved position, any saved position is deleted |
| 724 | ** and the cursor state set to CURSOR_INVALID. |
| 725 | */ |
| 726 | static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){ |
| 727 | int rc = SQLITE_OK; |
| 728 | assert( pCur->eState==CURSOR_REQUIRESEEK ); |
| 729 | pCur->eState = CURSOR_INVALID; |
| 730 | if( doSeek ){ |
| 731 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip); |
| 732 | } |
| 733 | if( rc==SQLITE_OK ){ |
| 734 | sqliteFree(pCur->pKey); |
| 735 | pCur->pKey = 0; |
| 736 | assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState ); |
| 737 | } |
| 738 | return rc; |
| 739 | } |
| 740 | |
| 741 | #define restoreOrClearCursorPosition(p,x) \ |
| 742 | (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK) |
| 743 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 744 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 745 | /* |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 746 | ** These macros define the location of the pointer-map entry for a |
| 747 | ** database page. The first argument to each is the number of usable |
| 748 | ** bytes on each page of the database (often 1024). The second is the |
| 749 | ** page number to look up in the pointer map. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 750 | ** |
| 751 | ** PTRMAP_PAGENO returns the database page number of the pointer-map |
| 752 | ** page that stores the required pointer. PTRMAP_PTROFFSET returns |
| 753 | ** the offset of the requested map entry. |
| 754 | ** |
| 755 | ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page, |
| 756 | ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 757 | ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements |
| 758 | ** this test. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 759 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 760 | #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno) |
| 761 | #define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1)) |
| 762 | #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno)) |
| 763 | |
| 764 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
| 765 | int nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 766 | int iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 767 | int ret = (iPtrMap*nPagesPerMapPage) + 2; |
| 768 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 769 | ret++; |
| 770 | } |
| 771 | return ret; |
| 772 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 773 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 774 | /* |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 775 | ** The pointer map is a lookup table that identifies the parent page for |
| 776 | ** each child page in the database file. The parent page is the page that |
| 777 | ** contains a pointer to the child. Every page in the database contains |
| 778 | ** 0 or 1 parent pages. (In this context 'database page' refers |
| 779 | ** to any page that is not part of the pointer map itself.) Each pointer map |
| 780 | ** entry consists of a single byte 'type' and a 4 byte parent page number. |
| 781 | ** The PTRMAP_XXX identifiers below are the valid types. |
| 782 | ** |
| 783 | ** The purpose of the pointer map is to facility moving pages from one |
| 784 | ** position in the file to another as part of autovacuum. When a page |
| 785 | ** is moved, the pointer in its parent must be updated to point to the |
| 786 | ** new location. The pointer map is used to locate the parent page quickly. |
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_ROOTPAGE: The database page is a root-page. The page-number is not |
| 789 | ** used in this case. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 790 | ** |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 791 | ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number |
| 792 | ** is not used in this case. |
| 793 | ** |
| 794 | ** PTRMAP_OVERFLOW1: The database page is the first page in a list of |
| 795 | ** overflow pages. The page number identifies the page that |
| 796 | ** contains the cell with a pointer to this overflow page. |
| 797 | ** |
| 798 | ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of |
| 799 | ** overflow pages. The page-number identifies the previous |
| 800 | ** page in the overflow page list. |
| 801 | ** |
| 802 | ** PTRMAP_BTREE: The database page is a non-root btree page. The page number |
| 803 | ** identifies the parent page in the btree. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 804 | */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 805 | #define PTRMAP_ROOTPAGE 1 |
| 806 | #define PTRMAP_FREEPAGE 2 |
| 807 | #define PTRMAP_OVERFLOW1 3 |
| 808 | #define PTRMAP_OVERFLOW2 4 |
| 809 | #define PTRMAP_BTREE 5 |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 810 | |
| 811 | /* |
| 812 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 813 | ** |
| 814 | ** This routine updates the pointer map entry for page number 'key' |
| 815 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 816 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 817 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 818 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 819 | DbPage *pDbPage; /* The pointer map page */ |
| 820 | u8 *pPtrmap; /* The pointer map data */ |
| 821 | Pgno iPtrmap; /* The pointer map page number */ |
| 822 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 823 | int rc; |
| 824 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 825 | /* The master-journal page number must never be used as a pointer map page */ |
| 826 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 827 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 828 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 829 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 830 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 831 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 832 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 833 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 834 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 835 | return rc; |
| 836 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 837 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 838 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 839 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 840 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 841 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 842 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 843 | if( rc==SQLITE_OK ){ |
| 844 | pPtrmap[offset] = eType; |
| 845 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 846 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 847 | } |
| 848 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 849 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 850 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 855 | ** |
| 856 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 857 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 858 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 859 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 860 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 861 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 862 | int iPtrmap; /* Pointer map page index */ |
| 863 | u8 *pPtrmap; /* Pointer map page data */ |
| 864 | int offset; /* Offset of entry in pointer map */ |
| 865 | int rc; |
| 866 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 867 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 868 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 869 | if( rc!=0 ){ |
| 870 | return rc; |
| 871 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 872 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 873 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 874 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 875 | assert( pEType!=0 ); |
| 876 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 877 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 878 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 879 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 880 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 881 | return SQLITE_OK; |
| 882 | } |
| 883 | |
| 884 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 885 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 886 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 887 | ** Given a btree page and a cell index (0 means the first cell on |
| 888 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 889 | ** to the cell content. |
| 890 | ** |
| 891 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 892 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 893 | static u8 *findCell(MemPage *pPage, int iCell){ |
| 894 | u8 *data = pPage->aData; |
| 895 | assert( iCell>=0 ); |
| 896 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
| 897 | return data + get2byte(&data[pPage->cellOffset+2*iCell]); |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | ** This a more complex version of findCell() that works for |
| 902 | ** pages that do contain overflow cells. See insert |
| 903 | */ |
| 904 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 905 | int i; |
| 906 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 907 | int k; |
| 908 | struct _OvflCell *pOvfl; |
| 909 | pOvfl = &pPage->aOvfl[i]; |
| 910 | k = pOvfl->idx; |
| 911 | if( k<=iCell ){ |
| 912 | if( k==iCell ){ |
| 913 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 914 | } |
| 915 | iCell--; |
| 916 | } |
| 917 | } |
| 918 | return findCell(pPage, iCell); |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | ** Parse a cell content block and fill in the CellInfo structure. There |
| 923 | ** are two versions of this function. parseCell() takes a cell index |
| 924 | ** as the second argument and parseCellPtr() takes a pointer to the |
| 925 | ** body of the cell as its second argument. |
| 926 | */ |
| 927 | static void parseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 928 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 929 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 930 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 931 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 932 | int n; /* Number bytes in cell content header */ |
| 933 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 934 | |
| 935 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 936 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 937 | n = pPage->childPtrSize; |
| 938 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 939 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 940 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 941 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 942 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 943 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 944 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 945 | if( pPage->intKey ){ |
| 946 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 947 | }else{ |
| 948 | u32 x; |
| 949 | n += getVarint32(&pCell[n], &x); |
| 950 | pInfo->nKey = x; |
| 951 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 952 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 953 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 954 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 955 | if( nPayload<=pPage->maxLocal ){ |
| 956 | /* This is the (easy) common case where the entire payload fits |
| 957 | ** on the local page. No overflow is required. |
| 958 | */ |
| 959 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 960 | pInfo->nLocal = nPayload; |
| 961 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 962 | nSize = nPayload + n; |
| 963 | if( nSize<4 ){ |
| 964 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 965 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 966 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 967 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 968 | /* If the payload will not fit completely on the local page, we have |
| 969 | ** to decide how much to store locally and how much to spill onto |
| 970 | ** overflow pages. The strategy is to minimize the amount of unused |
| 971 | ** space on overflow pages while keeping the amount of local storage |
| 972 | ** in between minLocal and maxLocal. |
| 973 | ** |
| 974 | ** Warning: changing the way overflow payload is distributed in any |
| 975 | ** way will result in an incompatible file format. |
| 976 | */ |
| 977 | int minLocal; /* Minimum amount of payload held locally */ |
| 978 | int maxLocal; /* Maximum amount of payload held locally */ |
| 979 | int surplus; /* Overflow payload available for local storage */ |
| 980 | |
| 981 | minLocal = pPage->minLocal; |
| 982 | maxLocal = pPage->maxLocal; |
| 983 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 984 | if( surplus <= maxLocal ){ |
| 985 | pInfo->nLocal = surplus; |
| 986 | }else{ |
| 987 | pInfo->nLocal = minLocal; |
| 988 | } |
| 989 | pInfo->iOverflow = pInfo->nLocal + n; |
| 990 | pInfo->nSize = pInfo->iOverflow + 4; |
| 991 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 992 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 993 | static void parseCell( |
| 994 | MemPage *pPage, /* Page containing the cell */ |
| 995 | int iCell, /* The cell index. First cell is 0 */ |
| 996 | CellInfo *pInfo /* Fill in this structure */ |
| 997 | ){ |
| 998 | parseCellPtr(pPage, findCell(pPage, iCell), pInfo); |
| 999 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1000 | |
| 1001 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1002 | ** Compute the total number of bytes that a Cell needs in the cell |
| 1003 | ** data area of the btree-page. The return number includes the cell |
| 1004 | ** data header and the local payload, but not any overflow page or |
| 1005 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1006 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1007 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1008 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1009 | CellInfo info; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1010 | parseCell(pPage, iCell, &info); |
| 1011 | return info.nSize; |
| 1012 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1013 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1014 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 1015 | CellInfo info; |
| 1016 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 1017 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1020 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1021 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1022 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1023 | ** to an overflow page, insert an entry into the pointer-map |
| 1024 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1025 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1026 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1027 | if( pCell ){ |
| 1028 | CellInfo info; |
| 1029 | parseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 1030 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1031 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 1032 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 1033 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 1034 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1035 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1036 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1037 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1038 | /* |
| 1039 | ** If the cell with index iCell on page pPage contains a pointer |
| 1040 | ** to an overflow page, insert an entry into the pointer-map |
| 1041 | ** for the overflow page. |
| 1042 | */ |
| 1043 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 1044 | u8 *pCell; |
| 1045 | pCell = findOverflowCell(pPage, iCell); |
| 1046 | return ptrmapPutOvflPtr(pPage, pCell); |
| 1047 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 1048 | #endif |
| 1049 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1050 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1051 | /* A bunch of assert() statements to check the transaction state variables |
| 1052 | ** of handle p (type Btree*) are internally consistent. |
| 1053 | */ |
| 1054 | #define btreeIntegrity(p) \ |
| 1055 | assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \ |
| 1056 | assert( p->pBt->nTransaction<=p->pBt->nRef ); \ |
| 1057 | assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \ |
| 1058 | assert( p->pBt->inTransaction>=p->inTrans ); |
| 1059 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1060 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1061 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 1062 | ** end of the page and all free space is collected into one |
| 1063 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 1064 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1065 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1066 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1067 | int i; /* Loop counter */ |
| 1068 | int pc; /* Address of a i-th cell */ |
| 1069 | int addr; /* Offset of first byte after cell pointer array */ |
| 1070 | int hdr; /* Offset to the page header */ |
| 1071 | int size; /* Size of a cell */ |
| 1072 | int usableSize; /* Number of usable bytes on a page */ |
| 1073 | int cellOffset; /* Offset to the cell pointer array */ |
| 1074 | int brk; /* Offset to the cell content area */ |
| 1075 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1076 | unsigned char *data; /* The page data */ |
| 1077 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1078 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1079 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1080 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1081 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1082 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1083 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 1084 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1085 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1086 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1087 | cellOffset = pPage->cellOffset; |
| 1088 | nCell = pPage->nCell; |
| 1089 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 1090 | usableSize = pPage->pBt->usableSize; |
| 1091 | brk = get2byte(&data[hdr+5]); |
| 1092 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 1093 | brk = usableSize; |
| 1094 | for(i=0; i<nCell; i++){ |
| 1095 | u8 *pAddr; /* The i-th cell pointer */ |
| 1096 | pAddr = &data[cellOffset + i*2]; |
| 1097 | pc = get2byte(pAddr); |
| 1098 | assert( pc<pPage->pBt->usableSize ); |
| 1099 | size = cellSizePtr(pPage, &temp[pc]); |
| 1100 | brk -= size; |
| 1101 | memcpy(&data[brk], &temp[pc], size); |
| 1102 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1103 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1104 | assert( brk>=cellOffset+2*nCell ); |
| 1105 | put2byte(&data[hdr+5], brk); |
| 1106 | data[hdr+1] = 0; |
| 1107 | data[hdr+2] = 0; |
| 1108 | data[hdr+7] = 0; |
| 1109 | addr = cellOffset+2*nCell; |
| 1110 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1111 | sqliteFree(temp); |
| 1112 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1115 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1116 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1117 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1118 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1119 | ** the new allocation. Or return 0 if there is not enough free |
| 1120 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1121 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1122 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1123 | ** nBytes of contiguous free space, then this routine automatically |
| 1124 | ** calls defragementPage() to consolidate all free space before |
| 1125 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1126 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1127 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1128 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1129 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 1130 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1131 | int top; |
| 1132 | int nCell; |
| 1133 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1134 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1135 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1136 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1137 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1138 | assert( pPage->pBt ); |
| 1139 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1140 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 1141 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1142 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1143 | |
| 1144 | nFrag = data[hdr+7]; |
| 1145 | if( nFrag<60 ){ |
| 1146 | /* Search the freelist looking for a slot big enough to satisfy the |
| 1147 | ** space request. */ |
| 1148 | addr = hdr+1; |
| 1149 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 1150 | size = get2byte(&data[pc+2]); |
| 1151 | if( size>=nByte ){ |
| 1152 | if( size<nByte+4 ){ |
| 1153 | memcpy(&data[addr], &data[pc], 2); |
| 1154 | data[hdr+7] = nFrag + size - nByte; |
| 1155 | return pc; |
| 1156 | }else{ |
| 1157 | put2byte(&data[pc+2], size-nByte); |
| 1158 | return pc + size - nByte; |
| 1159 | } |
| 1160 | } |
| 1161 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1164 | |
| 1165 | /* Allocate memory from the gap in between the cell pointer array |
| 1166 | ** and the cell content area. |
| 1167 | */ |
| 1168 | top = get2byte(&data[hdr+5]); |
| 1169 | nCell = get2byte(&data[hdr+3]); |
| 1170 | cellOffset = pPage->cellOffset; |
| 1171 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1172 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1173 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1174 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1175 | top -= nByte; |
| 1176 | assert( cellOffset + 2*nCell <= top ); |
| 1177 | put2byte(&data[hdr+5], top); |
| 1178 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1182 | ** Return a section of the pPage->aData to the freelist. |
| 1183 | ** The first byte of the new free block is pPage->aDisk[start] |
| 1184 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1185 | ** |
| 1186 | ** Most of the effort here is involved in coalesing adjacent |
| 1187 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1188 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1189 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1190 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1191 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1192 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1193 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1194 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1195 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1196 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1197 | if( size<4 ) size = 4; |
| 1198 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 1199 | #ifdef SQLITE_SECURE_DELETE |
| 1200 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 1201 | ** option is enabled at compile-time */ |
| 1202 | memset(&data[start], 0, size); |
| 1203 | #endif |
| 1204 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1205 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1206 | hdr = pPage->hdrOffset; |
| 1207 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1208 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1209 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1210 | assert( pbegin>addr ); |
| 1211 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1212 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1213 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1214 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1215 | put2byte(&data[addr], start); |
| 1216 | put2byte(&data[start], pbegin); |
| 1217 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1218 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1221 | addr = pPage->hdrOffset + 1; |
| 1222 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1223 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1224 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1225 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1226 | pnext = get2byte(&data[pbegin]); |
| 1227 | psize = get2byte(&data[pbegin+2]); |
| 1228 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 1229 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1230 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 1231 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1232 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 1233 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 1234 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1235 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1236 | } |
| 1237 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1238 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1239 | /* If the cell content area begins with a freeblock, remove it. */ |
| 1240 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 1241 | int top; |
| 1242 | pbegin = get2byte(&data[hdr+1]); |
| 1243 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 1244 | top = get2byte(&data[hdr+5]); |
| 1245 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1246 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1250 | ** Decode the flags byte (the first byte of the header) for a page |
| 1251 | ** and initialize fields of the MemPage structure accordingly. |
| 1252 | */ |
| 1253 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1254 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1255 | |
| 1256 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 1257 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 1258 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 1259 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 1260 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 1261 | pBt = pPage->pBt; |
| 1262 | if( flagByte & PTF_LEAFDATA ){ |
| 1263 | pPage->leafData = 1; |
| 1264 | pPage->maxLocal = pBt->maxLeaf; |
| 1265 | pPage->minLocal = pBt->minLeaf; |
| 1266 | }else{ |
| 1267 | pPage->leafData = 0; |
| 1268 | pPage->maxLocal = pBt->maxLocal; |
| 1269 | pPage->minLocal = pBt->minLocal; |
| 1270 | } |
| 1271 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 1272 | } |
| 1273 | |
| 1274 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1275 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1276 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1277 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1278 | ** is the parent of the page being initialized. The root of a |
| 1279 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1280 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1281 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1282 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1283 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 1284 | ** guarantee that the page is well-formed. It only shows that |
| 1285 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1286 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1287 | static int initPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1288 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1289 | MemPage *pParent /* The parent. Might be NULL */ |
| 1290 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1291 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1292 | int hdr; /* Offset to beginning of page header */ |
| 1293 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1294 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1295 | int usableSize; /* Amount of usable space on each page */ |
| 1296 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 1297 | int nFree; /* Number of unused bytes on the page */ |
| 1298 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1299 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1300 | pBt = pPage->pBt; |
| 1301 | assert( pBt!=0 ); |
| 1302 | assert( pParent==0 || pParent->pBt==pBt ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1303 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1304 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1305 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 1306 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1307 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1308 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1309 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1310 | if( pPage->pParent==0 && pParent!=0 ){ |
| 1311 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1312 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1313 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1314 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1315 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1316 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1317 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 1318 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1319 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1320 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 1321 | top = get2byte(&data[hdr+5]); |
| 1322 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1323 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1324 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1325 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1326 | } |
| 1327 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 1328 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1329 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1330 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1331 | |
| 1332 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1333 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1334 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1335 | while( pc>0 ){ |
| 1336 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1337 | if( pc>usableSize-4 ){ |
| 1338 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1339 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1340 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1341 | next = get2byte(&data[pc]); |
| 1342 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1343 | if( next>0 && next<=pc+size+3 ){ |
| 1344 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1345 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1346 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1347 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1348 | pc = next; |
| 1349 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 1350 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1351 | if( nFree>=usableSize ){ |
| 1352 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1353 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1354 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1355 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1356 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1357 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1361 | ** Set up a raw page so that it looks like a database page holding |
| 1362 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1363 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1364 | static void zeroPage(MemPage *pPage, int flags){ |
| 1365 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1366 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1367 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1368 | int first; |
| 1369 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1370 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1371 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1372 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1373 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1374 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1375 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 1376 | memset(&data[hdr+1], 0, 4); |
| 1377 | data[hdr+7] = 0; |
| 1378 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1379 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 1380 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1381 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1382 | pPage->cellOffset = first; |
| 1383 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1384 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1385 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1386 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1390 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1391 | ** MemPage.aData elements if needed. |
| 1392 | */ |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 1393 | static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage, int clrFlag){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1394 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1395 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1396 | DbPage *pDbPage; |
| 1397 | |
| 1398 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, clrFlag); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1399 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1400 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1401 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1402 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1403 | pPage->pBt = pBt; |
| 1404 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1405 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1406 | *ppPage = pPage; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 1407 | if( clrFlag ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1408 | sqlite3PagerDontRollback(pPage->pDbPage); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 1409 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1410 | return SQLITE_OK; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1414 | ** Get a page from the pager and initialize it. This routine |
| 1415 | ** is just a convenience wrapper around separate calls to |
| 1416 | ** getPage() and initPage(). |
| 1417 | */ |
| 1418 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1419 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1420 | Pgno pgno, /* Number of the page to get */ |
| 1421 | MemPage **ppPage, /* Write the page pointer here */ |
| 1422 | MemPage *pParent /* Parent of the page */ |
| 1423 | ){ |
| 1424 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1425 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1426 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1427 | } |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 1428 | rc = getPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1429 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1430 | rc = initPage(*ppPage, pParent); |
| 1431 | } |
| 1432 | return rc; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1436 | ** Release a MemPage. This should be called once for each prior |
| 1437 | ** call to getPage. |
| 1438 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1439 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1440 | if( pPage ){ |
| 1441 | assert( pPage->aData ); |
| 1442 | assert( pPage->pBt ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1443 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1444 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1449 | ** This routine is called when the reference count for a page |
| 1450 | ** reaches zero. We need to unref the pParent pointer when that |
| 1451 | ** happens. |
| 1452 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1453 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1454 | MemPage *pPage; |
| 1455 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1456 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1457 | if( pPage->pParent ){ |
| 1458 | MemPage *pParent = pPage->pParent; |
| 1459 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1460 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1461 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1462 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1466 | ** During a rollback, when the pager reloads information into the cache |
| 1467 | ** so that the cache is restored to its original state at the start of |
| 1468 | ** the transaction, for each page restored this routine is called. |
| 1469 | ** |
| 1470 | ** This routine needs to reset the extra data section at the end of the |
| 1471 | ** page to agree with the restored data. |
| 1472 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1473 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1474 | MemPage *pPage; |
| 1475 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1476 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1477 | if( pPage->isInit ){ |
| 1478 | pPage->isInit = 0; |
| 1479 | initPage(pPage, pPage->pParent); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1484 | ** Open a database file. |
| 1485 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1486 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1487 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1488 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1489 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1490 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1491 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1492 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1493 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1494 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1495 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1496 | BtShared *pBt; /* Shared part of btree structure */ |
| 1497 | Btree *p; /* Handle to return */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1498 | int rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1499 | int nReserve; |
| 1500 | unsigned char zDbHeader[100]; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1501 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1502 | const ThreadData *pTsdro; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1503 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1504 | |
| 1505 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1506 | ** false for a file-based database. This symbol is only required if |
| 1507 | ** either of the shared-data or autovacuum features are compiled |
| 1508 | ** into the library. |
| 1509 | */ |
| 1510 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1511 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1512 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1513 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1514 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1515 | #endif |
| 1516 | #endif |
| 1517 | |
| 1518 | p = sqliteMalloc(sizeof(Btree)); |
| 1519 | if( !p ){ |
| 1520 | return SQLITE_NOMEM; |
| 1521 | } |
| 1522 | p->inTrans = TRANS_NONE; |
| 1523 | p->pSqlite = pSqlite; |
| 1524 | |
| 1525 | /* Try to find an existing Btree structure opened on zFilename. */ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1526 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1527 | pTsdro = sqlite3ThreadDataReadOnly(); |
| 1528 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1529 | char *zFullPathname = sqlite3OsFullPathname(zFilename); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1530 | if( !zFullPathname ){ |
| 1531 | sqliteFree(p); |
| 1532 | return SQLITE_NOMEM; |
| 1533 | } |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1534 | for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1535 | assert( pBt->nRef>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1536 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1537 | p->pBt = pBt; |
| 1538 | *ppBtree = p; |
| 1539 | pBt->nRef++; |
| 1540 | sqliteFree(zFullPathname); |
| 1541 | return SQLITE_OK; |
| 1542 | } |
| 1543 | } |
| 1544 | sqliteFree(zFullPathname); |
| 1545 | } |
| 1546 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1547 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1548 | /* |
| 1549 | ** The following asserts make sure that structures used by the btree are |
| 1550 | ** the right size. This is to guard against size changes that result |
| 1551 | ** when compiling on a different architecture. |
| 1552 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 1553 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1554 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1555 | assert( sizeof(u32)==4 ); |
| 1556 | assert( sizeof(u16)==2 ); |
| 1557 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1558 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1559 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1560 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1561 | *ppBtree = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1562 | sqliteFree(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1563 | return SQLITE_NOMEM; |
| 1564 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1565 | rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1566 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1567 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1568 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1569 | if( rc!=SQLITE_OK ){ |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1570 | if( pBt->pPager ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1571 | sqlite3PagerClose(pBt->pPager); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1572 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1573 | sqliteFree(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1574 | sqliteFree(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1575 | *ppBtree = 0; |
| 1576 | return rc; |
| 1577 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1578 | p->pBt = pBt; |
| 1579 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1580 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1581 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1582 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1583 | pBt->pPage1 = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1584 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1585 | pBt->pageSize = get2byte(&zDbHeader[16]); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1586 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1587 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1588 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1589 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1590 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1591 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1592 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1593 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1594 | ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM |
| 1595 | ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined, |
| 1596 | ** then ":memory:" is just a regular file-name. Respect the auto-vacuum |
| 1597 | ** default in this case. |
| 1598 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1599 | if( zFilename && !isMemdb ){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1600 | pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM; |
| 1601 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1602 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1603 | nReserve = 0; |
| 1604 | }else{ |
| 1605 | nReserve = zDbHeader[20]; |
| 1606 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1607 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1608 | pBt->minLeafFrac = zDbHeader[23]; |
| 1609 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1610 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1611 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1612 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1613 | } |
| 1614 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1615 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1616 | sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1617 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1618 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1619 | /* Add the new btree to the linked list starting at ThreadData.pBtree. |
| 1620 | ** There is no chance that a malloc() may fail inside of the |
| 1621 | ** sqlite3ThreadData() call, as the ThreadData structure must have already |
| 1622 | ** been allocated for pTsdro->useSharedData to be non-zero. |
| 1623 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1624 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
| 1625 | pBt->pNext = pTsdro->pBtree; |
| 1626 | sqlite3ThreadData()->pBtree = pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1627 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1628 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1629 | pBt->nRef = 1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1630 | *ppBtree = p; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1631 | return SQLITE_OK; |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | ** Close an open database and invalidate all cursors. |
| 1636 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1637 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1638 | BtShared *pBt = p->pBt; |
| 1639 | BtCursor *pCur; |
| 1640 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1641 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1642 | ThreadData *pTsd; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1643 | #endif |
| 1644 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1645 | /* Close all cursors opened via this handle. */ |
| 1646 | pCur = pBt->pCursor; |
| 1647 | while( pCur ){ |
| 1648 | BtCursor *pTmp = pCur; |
| 1649 | pCur = pCur->pNext; |
| 1650 | if( pTmp->pBtree==p ){ |
| 1651 | sqlite3BtreeCloseCursor(pTmp); |
| 1652 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1653 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1654 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1655 | /* Rollback any active transaction and free the handle structure. |
| 1656 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1657 | ** this handle. |
| 1658 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1659 | sqlite3BtreeRollback(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1660 | sqliteFree(p); |
| 1661 | |
| 1662 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1663 | /* If there are still other outstanding references to the shared-btree |
| 1664 | ** structure, return now. The remainder of this procedure cleans |
| 1665 | ** up the shared-btree. |
| 1666 | */ |
| 1667 | assert( pBt->nRef>0 ); |
| 1668 | pBt->nRef--; |
| 1669 | if( pBt->nRef ){ |
| 1670 | return SQLITE_OK; |
| 1671 | } |
| 1672 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1673 | /* Remove the shared-btree from the thread wide list. Call |
| 1674 | ** ThreadDataReadOnly() and then cast away the const property of the |
| 1675 | ** pointer to avoid allocating thread data if it is not really required. |
| 1676 | */ |
| 1677 | pTsd = (ThreadData *)sqlite3ThreadDataReadOnly(); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1678 | if( pTsd->pBtree==pBt ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1679 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1680 | pTsd->pBtree = pBt->pNext; |
| 1681 | }else{ |
| 1682 | BtShared *pPrev; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1683 | for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1684 | if( pPrev ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1685 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1686 | pPrev->pNext = pBt->pNext; |
| 1687 | } |
| 1688 | } |
| 1689 | #endif |
| 1690 | |
| 1691 | /* Close the pager and free the shared-btree structure */ |
| 1692 | assert( !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1693 | sqlite3PagerClose(pBt->pPager); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1694 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1695 | pBt->xFreeSchema(pBt->pSchema); |
| 1696 | } |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 1697 | sqliteFree(pBt->pSchema); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1698 | sqliteFree(pBt); |
| 1699 | return SQLITE_OK; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1703 | ** Change the busy handler callback function. |
| 1704 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1705 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1706 | BtShared *pBt = p->pBt; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1707 | pBt->pBusyHandler = pHandler; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1708 | sqlite3PagerSetBusyhandler(pBt->pPager, pHandler); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1709 | return SQLITE_OK; |
| 1710 | } |
| 1711 | |
| 1712 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1713 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1714 | ** |
| 1715 | ** The maximum number of cache pages is set to the absolute |
| 1716 | ** value of mxPage. If mxPage is negative, the pager will |
| 1717 | ** operate asynchronously - it will not stop to do fsync()s |
| 1718 | ** to insure data is written to the disk surface before |
| 1719 | ** continuing. Transactions still work if synchronous is off, |
| 1720 | ** and the database cannot be corrupted if this program |
| 1721 | ** crashes. But if the operating system crashes or there is |
| 1722 | ** an abrupt power failure when synchronous is off, the database |
| 1723 | ** could be left in an inconsistent and unrecoverable state. |
| 1724 | ** Synchronous is on by default so database corruption is not |
| 1725 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1726 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1727 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1728 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1729 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1730 | return SQLITE_OK; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1734 | ** Change the way data is synced to disk in order to increase or decrease |
| 1735 | ** how well the database resists damage due to OS crashes and power |
| 1736 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1737 | ** there is a high probability of damage) Level 2 is the default. There |
| 1738 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1739 | ** probability of damage to near zero but with a write performance reduction. |
| 1740 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1741 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1742 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1743 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1744 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1745 | return SQLITE_OK; |
| 1746 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1747 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1748 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1749 | /* |
| 1750 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1751 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1752 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1753 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1754 | BtShared *pBt = p->pBt; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1755 | assert( pBt && pBt->pPager ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1756 | return sqlite3PagerNosync(pBt->pPager); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1759 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1760 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1761 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1762 | ** |
| 1763 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1764 | ** size supplied does not meet this constraint then the page size is not |
| 1765 | ** changed. |
| 1766 | ** |
| 1767 | ** Page sizes are constrained to be a power of two so that the region |
| 1768 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1769 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1770 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1771 | ** |
| 1772 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1773 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1774 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1775 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
| 1776 | BtShared *pBt = p->pBt; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1777 | if( pBt->pageSizeFixed ){ |
| 1778 | return SQLITE_READONLY; |
| 1779 | } |
| 1780 | if( nReserve<0 ){ |
| 1781 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1782 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1783 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1784 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1785 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1786 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1787 | pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1788 | } |
| 1789 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1790 | return SQLITE_OK; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | ** Return the currently defined page size |
| 1795 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1796 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1797 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1798 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1799 | int sqlite3BtreeGetReserve(Btree *p){ |
| 1800 | return p->pBt->pageSize - p->pBt->usableSize; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1801 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1802 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1803 | |
| 1804 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1805 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1806 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1807 | ** is disabled. The default value for the auto-vacuum property is |
| 1808 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1809 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1810 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
| 1811 | BtShared *pBt = p->pBt;; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1812 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1813 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1814 | #else |
| 1815 | if( pBt->pageSizeFixed ){ |
| 1816 | return SQLITE_READONLY; |
| 1817 | } |
| 1818 | pBt->autoVacuum = (autoVacuum?1:0); |
| 1819 | return SQLITE_OK; |
| 1820 | #endif |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1825 | ** enabled 1 is returned. Otherwise 0. |
| 1826 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1827 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1828 | #ifdef SQLITE_OMIT_AUTOVACUUM |
| 1829 | return 0; |
| 1830 | #else |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1831 | return p->pBt->autoVacuum; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1832 | #endif |
| 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1837 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1838 | ** also acquire a readlock on that file. |
| 1839 | ** |
| 1840 | ** SQLITE_OK is returned on success. If the file is not a |
| 1841 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1842 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 1843 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 1844 | ** if there is a locking protocol violation. |
| 1845 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1846 | static int lockBtree(BtShared *pBt){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1847 | int rc, pageSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1848 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1849 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 1850 | rc = getPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1851 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1852 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1853 | |
| 1854 | /* Do some checking to help insure the file we opened really is |
| 1855 | ** a valid database file. |
| 1856 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1857 | rc = SQLITE_NOTADB; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1858 | if( sqlite3PagerPagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1859 | u8 *page1 = pPage1->aData; |
| 1860 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1861 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1862 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1863 | if( page1[18]>1 || page1[19]>1 ){ |
| 1864 | goto page1_init_failed; |
| 1865 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1866 | pageSize = get2byte(&page1[16]); |
| 1867 | if( ((pageSize-1)&pageSize)!=0 ){ |
| 1868 | goto page1_init_failed; |
| 1869 | } |
| 1870 | assert( (pageSize & 7)==0 ); |
| 1871 | pBt->pageSize = pageSize; |
| 1872 | pBt->usableSize = pageSize - page1[20]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1873 | if( pBt->usableSize<500 ){ |
| 1874 | goto page1_init_failed; |
| 1875 | } |
| 1876 | pBt->maxEmbedFrac = page1[21]; |
| 1877 | pBt->minEmbedFrac = page1[22]; |
| 1878 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1879 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1880 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
| 1881 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1882 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1883 | |
| 1884 | /* maxLocal is the maximum amount of payload to store locally for |
| 1885 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1886 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1887 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1888 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1889 | ** 4-byte child pointer |
| 1890 | ** 9-byte nKey value |
| 1891 | ** 4-byte nData value |
| 1892 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1893 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1894 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1895 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1896 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1897 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1898 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1899 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1900 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1901 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1902 | goto page1_init_failed; |
| 1903 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1904 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1905 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1906 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1907 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1908 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1909 | releasePage(pPage1); |
| 1910 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1911 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| 1914 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1915 | ** This routine works like lockBtree() except that it also invokes the |
| 1916 | ** busy callback if there is lock contention. |
| 1917 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1918 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1919 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1920 | if( pRef->inTrans==TRANS_NONE ){ |
| 1921 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1922 | btreeIntegrity(pRef); |
| 1923 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1924 | pRef->pBt->inTransaction = inTransaction; |
| 1925 | pRef->inTrans = TRANS_NONE; |
| 1926 | if( rc==SQLITE_OK ){ |
| 1927 | pRef->pBt->nTransaction--; |
| 1928 | } |
| 1929 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1930 | } |
| 1931 | return rc; |
| 1932 | } |
| 1933 | |
| 1934 | |
| 1935 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1936 | ** If there are no outstanding cursors and we are not in the middle |
| 1937 | ** of a transaction but there is a read lock on the database, then |
| 1938 | ** this routine unrefs the first page of the database file which |
| 1939 | ** has the effect of releasing the read lock. |
| 1940 | ** |
| 1941 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1942 | ** |
| 1943 | ** If there is a transaction in progress, this routine is a no-op. |
| 1944 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1945 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 1946 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1947 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1948 | if( pBt->pPage1->aData==0 ){ |
| 1949 | MemPage *pPage = pBt->pPage1; |
| 1950 | pPage->aData = &((u8*)pPage)[-pBt->pageSize]; |
| 1951 | pPage->pBt = pBt; |
| 1952 | pPage->pgno = 1; |
| 1953 | } |
| 1954 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1955 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1956 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1957 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1962 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1963 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1964 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1965 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1966 | MemPage *pP1; |
| 1967 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1968 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1969 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1970 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1971 | assert( pP1!=0 ); |
| 1972 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 1973 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1974 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1975 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1976 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1977 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1978 | data[18] = 1; |
| 1979 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1980 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1981 | data[21] = pBt->maxEmbedFrac; |
| 1982 | data[22] = pBt->minEmbedFrac; |
| 1983 | data[23] = pBt->minLeafFrac; |
| 1984 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1985 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1986 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1987 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1988 | if( pBt->autoVacuum ){ |
| 1989 | put4byte(&data[36 + 4*4], 1); |
| 1990 | } |
| 1991 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1992 | return SQLITE_OK; |
| 1993 | } |
| 1994 | |
| 1995 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1996 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1997 | ** is started if the second argument is nonzero, otherwise a read- |
| 1998 | ** transaction. If the second argument is 2 or more and exclusive |
| 1999 | ** transaction is started, meaning that no other process is allowed |
| 2000 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2001 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 2002 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2003 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2004 | ** A write-transaction must be started before attempting any |
| 2005 | ** changes to the database. None of the following routines |
| 2006 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2007 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 2008 | ** sqlite3BtreeCreateTable() |
| 2009 | ** sqlite3BtreeCreateIndex() |
| 2010 | ** sqlite3BtreeClearTable() |
| 2011 | ** sqlite3BtreeDropTable() |
| 2012 | ** sqlite3BtreeInsert() |
| 2013 | ** sqlite3BtreeDelete() |
| 2014 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 2015 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2016 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 2017 | ** and the database was previously unlocked, then invoke the busy handler |
| 2018 | ** if there is one. But if there was previously a read-lock, do not |
| 2019 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 2020 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 2021 | ** |
| 2022 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 2023 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 2024 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 2025 | ** One or the other of the two processes must give way or there can be |
| 2026 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 2027 | ** when A already has a read lock, we encourage A to give up and let B |
| 2028 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2029 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2030 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 2031 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2032 | int rc = SQLITE_OK; |
| 2033 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2034 | btreeIntegrity(p); |
| 2035 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2036 | /* If the btree is already in a write-transaction, or it |
| 2037 | ** is already in a read-transaction and a read-transaction |
| 2038 | ** is requested, this is a no-op. |
| 2039 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2040 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2041 | return SQLITE_OK; |
| 2042 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2043 | |
| 2044 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2045 | if( pBt->readOnly && wrflag ){ |
| 2046 | return SQLITE_READONLY; |
| 2047 | } |
| 2048 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2049 | /* If another database handle has already opened a write transaction |
| 2050 | ** on this shared-btree structure and a second write transaction is |
| 2051 | ** requested, return SQLITE_BUSY. |
| 2052 | */ |
| 2053 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
| 2054 | return SQLITE_BUSY; |
| 2055 | } |
| 2056 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2057 | do { |
| 2058 | if( pBt->pPage1==0 ){ |
| 2059 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2060 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2061 | |
| 2062 | if( rc==SQLITE_OK && wrflag ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2063 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2064 | if( rc==SQLITE_OK ){ |
| 2065 | rc = newDatabase(pBt); |
| 2066 | } |
| 2067 | } |
| 2068 | |
| 2069 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 2070 | if( wrflag ) pBt->inStmt = 0; |
| 2071 | }else{ |
| 2072 | unlockBtreeIfUnused(pBt); |
| 2073 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2074 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 2075 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2076 | |
| 2077 | if( rc==SQLITE_OK ){ |
| 2078 | if( p->inTrans==TRANS_NONE ){ |
| 2079 | pBt->nTransaction++; |
| 2080 | } |
| 2081 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 2082 | if( p->inTrans>pBt->inTransaction ){ |
| 2083 | pBt->inTransaction = p->inTrans; |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | btreeIntegrity(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 2088 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2091 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2092 | |
| 2093 | /* |
| 2094 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 2095 | ** pPage contains cells that point to overflow pages, set the pointer |
| 2096 | ** map entries for the overflow pages as well. |
| 2097 | */ |
| 2098 | static int setChildPtrmaps(MemPage *pPage){ |
| 2099 | int i; /* Counter variable */ |
| 2100 | int nCell; /* Number of cells in page pPage */ |
| 2101 | int rc = SQLITE_OK; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2102 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2103 | int isInitOrig = pPage->isInit; |
| 2104 | Pgno pgno = pPage->pgno; |
| 2105 | |
| 2106 | initPage(pPage, 0); |
| 2107 | nCell = pPage->nCell; |
| 2108 | |
| 2109 | for(i=0; i<nCell; i++){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2110 | u8 *pCell = findCell(pPage, i); |
| 2111 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2112 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 2113 | if( rc!=SQLITE_OK ){ |
| 2114 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2115 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 2116 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2117 | if( !pPage->leaf ){ |
| 2118 | Pgno childPgno = get4byte(pCell); |
| 2119 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2120 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | if( !pPage->leaf ){ |
| 2125 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2126 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2127 | } |
| 2128 | |
| 2129 | set_child_ptrmaps_out: |
| 2130 | pPage->isInit = isInitOrig; |
| 2131 | return rc; |
| 2132 | } |
| 2133 | |
| 2134 | /* |
| 2135 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2136 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2137 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2138 | ** follows: |
| 2139 | ** |
| 2140 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2141 | ** page of pPage. |
| 2142 | ** |
| 2143 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2144 | ** page pointed to by one of the cells on pPage. |
| 2145 | ** |
| 2146 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2147 | ** overflow page in the list. |
| 2148 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2149 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2150 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2151 | /* 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] | 2152 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2153 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2154 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2155 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2156 | }else{ |
| 2157 | int isInitOrig = pPage->isInit; |
| 2158 | int i; |
| 2159 | int nCell; |
| 2160 | |
| 2161 | initPage(pPage, 0); |
| 2162 | nCell = pPage->nCell; |
| 2163 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2164 | for(i=0; i<nCell; i++){ |
| 2165 | u8 *pCell = findCell(pPage, i); |
| 2166 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2167 | CellInfo info; |
| 2168 | parseCellPtr(pPage, pCell, &info); |
| 2169 | if( info.iOverflow ){ |
| 2170 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2171 | put4byte(&pCell[info.iOverflow], iTo); |
| 2172 | break; |
| 2173 | } |
| 2174 | } |
| 2175 | }else{ |
| 2176 | if( get4byte(pCell)==iFrom ){ |
| 2177 | put4byte(pCell, iTo); |
| 2178 | break; |
| 2179 | } |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2184 | if( eType!=PTRMAP_BTREE || |
| 2185 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2186 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2187 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2188 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2189 | } |
| 2190 | |
| 2191 | pPage->isInit = isInitOrig; |
| 2192 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2193 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2196 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2197 | /* |
| 2198 | ** Move the open database page pDbPage to location iFreePage in the |
| 2199 | ** database. The pDbPage reference remains valid. |
| 2200 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2201 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2202 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2203 | MemPage *pDbPage, /* Open page to move */ |
| 2204 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2205 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 2206 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2207 | ){ |
| 2208 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2209 | Pgno iDbPage = pDbPage->pgno; |
| 2210 | Pager *pPager = pBt->pPager; |
| 2211 | int rc; |
| 2212 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2213 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2214 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2215 | |
| 2216 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 2217 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2218 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2219 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2220 | if( rc!=SQLITE_OK ){ |
| 2221 | return rc; |
| 2222 | } |
| 2223 | pDbPage->pgno = iFreePage; |
| 2224 | |
| 2225 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2226 | ** that point to overflow pages. The pointer map entries for all these |
| 2227 | ** pages need to be changed. |
| 2228 | ** |
| 2229 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2230 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2231 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2232 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2233 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2234 | rc = setChildPtrmaps(pDbPage); |
| 2235 | if( rc!=SQLITE_OK ){ |
| 2236 | return rc; |
| 2237 | } |
| 2238 | }else{ |
| 2239 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2240 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2241 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2242 | if( rc!=SQLITE_OK ){ |
| 2243 | return rc; |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2249 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2250 | ** iPtrPage. |
| 2251 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2252 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 2253 | rc = getPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2254 | if( rc!=SQLITE_OK ){ |
| 2255 | return rc; |
| 2256 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2257 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2258 | if( rc!=SQLITE_OK ){ |
| 2259 | releasePage(pPtrPage); |
| 2260 | return rc; |
| 2261 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2262 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2263 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2264 | if( rc==SQLITE_OK ){ |
| 2265 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2266 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2267 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2268 | return rc; |
| 2269 | } |
| 2270 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2271 | /* Forward declaration required by autoVacuumCommit(). */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2272 | static int allocatePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2273 | |
| 2274 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2275 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2276 | ** is commited for an auto-vacuum database. |
| 2277 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2278 | static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2279 | Pager *pPager = pBt->pPager; |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2280 | Pgno nFreeList; /* Number of pages remaining on the free-list. */ |
| 2281 | int nPtrMap; /* Number of pointer-map pages deallocated */ |
| 2282 | Pgno origSize; /* Pages in the database file */ |
| 2283 | Pgno finSize; /* Pages in the database file after truncation */ |
| 2284 | int rc; /* Return code */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2285 | u8 eType; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2286 | int pgsz = pBt->pageSize; /* Page size for this database */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2287 | Pgno iDbPage; /* The database page to move */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2288 | MemPage *pDbMemPage = 0; /* "" */ |
| 2289 | Pgno iPtrPage; /* The page that contains a pointer to iDbPage */ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2290 | Pgno iFreePage; /* The free-list page to move iDbPage to */ |
| 2291 | MemPage *pFreeMemPage = 0; /* "" */ |
| 2292 | |
| 2293 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2294 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2295 | #endif |
| 2296 | |
| 2297 | assert( pBt->autoVacuum ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2298 | if( PTRMAP_ISPAGE(pBt, sqlite3PagerPagecount(pPager)) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2299 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2300 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2301 | |
| 2302 | /* Figure out how many free-pages are in the database. If there are no |
| 2303 | ** free pages, then auto-vacuum is a no-op. |
| 2304 | */ |
| 2305 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2306 | if( nFreeList==0 ){ |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 2307 | *nTrunc = 0; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2308 | return SQLITE_OK; |
| 2309 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2310 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2311 | /* This block figures out how many pages there are in the database |
| 2312 | ** now (variable origSize), and how many there will be after the |
| 2313 | ** truncation (variable finSize). |
| 2314 | ** |
| 2315 | ** The final size is the original size, less the number of free pages |
| 2316 | ** in the database, less any pointer-map pages that will no longer |
| 2317 | ** be required, less 1 if the pending-byte page was part of the database |
| 2318 | ** but is not after the truncation. |
| 2319 | **/ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2320 | origSize = sqlite3PagerPagecount(pPager); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2321 | if( origSize==PENDING_BYTE_PAGE(pBt) ){ |
| 2322 | origSize--; |
| 2323 | } |
| 2324 | nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pBt, origSize)+pgsz/5)/(pgsz/5); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2325 | finSize = origSize - nFreeList - nPtrMap; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2326 | if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2327 | finSize--; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2328 | } |
| 2329 | while( PTRMAP_ISPAGE(pBt, finSize) || finSize==PENDING_BYTE_PAGE(pBt) ){ |
| 2330 | finSize--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2331 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2332 | TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize)); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2333 | |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2334 | /* Variable 'finSize' will be the size of the file in pages after |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2335 | ** the auto-vacuum has completed (the current file size minus the number |
| 2336 | ** of pages on the free list). Loop through the pages that lie beyond |
| 2337 | ** 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] | 2338 | ** to a free page earlier in the file (somewhere before finSize). |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2339 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2340 | for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2341 | /* 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] | 2342 | if( PTRMAP_ISPAGE(pBt, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2343 | continue; |
| 2344 | } |
| 2345 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2346 | rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage); |
| 2347 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 2348 | if( eType==PTRMAP_ROOTPAGE ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2349 | rc = SQLITE_CORRUPT_BKPT; |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 2350 | goto autovacuum_out; |
| 2351 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2352 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 2353 | /* If iDbPage is free, do not swap it. */ |
| 2354 | if( eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2355 | continue; |
| 2356 | } |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 2357 | rc = getPage(pBt, iDbPage, &pDbMemPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2358 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2359 | |
| 2360 | /* Find the next page in the free-list that is not already at the end |
| 2361 | ** of the file. A page can be pulled off the free list using the |
| 2362 | ** allocatePage() routine. |
| 2363 | */ |
| 2364 | do{ |
| 2365 | if( pFreeMemPage ){ |
| 2366 | releasePage(pFreeMemPage); |
| 2367 | pFreeMemPage = 0; |
| 2368 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 2369 | rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2370 | if( rc!=SQLITE_OK ){ |
| 2371 | releasePage(pDbMemPage); |
| 2372 | goto autovacuum_out; |
| 2373 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 2374 | assert( iFreePage<=origSize ); |
| 2375 | }while( iFreePage>finSize ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2376 | releasePage(pFreeMemPage); |
| 2377 | pFreeMemPage = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2378 | |
danielk1977 | e501b89 | 2006-01-09 06:29:47 +0000 | [diff] [blame] | 2379 | /* Relocate the page into the body of the file. Note that although the |
| 2380 | ** page has moved within the database file, the pDbMemPage pointer |
| 2381 | ** remains valid. This means that this function can run without |
| 2382 | ** invalidating cursors open on the btree. This is important in |
| 2383 | ** shared-cache mode. |
| 2384 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2385 | rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2386 | releasePage(pDbMemPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2387 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | /* 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] | 2391 | ** truncate the database file to finSize pages and consider the |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2392 | ** free-list empty. |
| 2393 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2394 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2395 | if( rc!=SQLITE_OK ) goto autovacuum_out; |
| 2396 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2397 | put4byte(&pBt->pPage1->aData[36], 0); |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 2398 | *nTrunc = finSize; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 2399 | assert( finSize!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2400 | |
| 2401 | autovacuum_out: |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2402 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2403 | if( rc!=SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2404 | sqlite3PagerRollback(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2405 | } |
| 2406 | return rc; |
| 2407 | } |
| 2408 | #endif |
| 2409 | |
| 2410 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2411 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2412 | ** |
| 2413 | ** This will release the write lock on the database file. If there |
| 2414 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2415 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2416 | int sqlite3BtreeCommit(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2417 | BtShared *pBt = p->pBt; |
| 2418 | |
| 2419 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2422 | ** transaction and set the shared state to TRANS_READ. |
| 2423 | */ |
| 2424 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2425 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2426 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2427 | assert( pBt->nTransaction>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2428 | rc = sqlite3PagerCommit(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2429 | if( rc!=SQLITE_OK ){ |
| 2430 | return rc; |
| 2431 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2432 | pBt->inTransaction = TRANS_READ; |
| 2433 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2434 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2435 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2436 | |
| 2437 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2438 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2439 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2440 | ** will unlock the pager. |
| 2441 | */ |
| 2442 | if( p->inTrans!=TRANS_NONE ){ |
| 2443 | pBt->nTransaction--; |
| 2444 | if( 0==pBt->nTransaction ){ |
| 2445 | pBt->inTransaction = TRANS_NONE; |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2450 | ** the pager if this call closed the only read or write transaction. |
| 2451 | */ |
| 2452 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2453 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2454 | |
| 2455 | btreeIntegrity(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2456 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2459 | #ifndef NDEBUG |
| 2460 | /* |
| 2461 | ** Return the number of write-cursors open on this handle. This is for use |
| 2462 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2463 | ** defined. |
| 2464 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2465 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2466 | BtCursor *pCur; |
| 2467 | int r = 0; |
| 2468 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2469 | if( pCur->wrFlag ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2470 | } |
| 2471 | return r; |
| 2472 | } |
| 2473 | #endif |
| 2474 | |
drh | 77bba59 | 2006-08-13 18:39:26 +0000 | [diff] [blame] | 2475 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2476 | /* |
| 2477 | ** Print debugging information about all cursors to standard output. |
| 2478 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2479 | void sqlite3BtreeCursorList(Btree *p){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2480 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2481 | BtShared *pBt = p->pBt; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2482 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 2483 | MemPage *pPage = pCur->pPage; |
| 2484 | char *zMode = pCur->wrFlag ? "rw" : "ro"; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 2485 | sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n", |
| 2486 | pCur, pCur->pgnoRoot, zMode, |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2487 | pPage ? pPage->pgno : 0, pCur->idx, |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2488 | (pCur->eState==CURSOR_VALID) ? "" : " eof" |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2489 | ); |
| 2490 | } |
| 2491 | } |
| 2492 | #endif |
| 2493 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2494 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2495 | ** Rollback the transaction in progress. All cursors will be |
| 2496 | ** invalided by this operation. Any attempt to use a cursor |
| 2497 | ** that was open at the beginning of this operation will result |
| 2498 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2499 | ** |
| 2500 | ** This will release the write lock on the database file. If there |
| 2501 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2502 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2503 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2504 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2505 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2506 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2507 | |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2508 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2509 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2510 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2511 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2512 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2513 | ** the result of a constraint, malloc() failure or IO error) then |
| 2514 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2515 | ** we cannot simply return the error to the caller. Instead, abort |
| 2516 | ** all queries that may be using any of the cursors that failed to save. |
| 2517 | */ |
| 2518 | while( pBt->pCursor ){ |
| 2519 | sqlite3 *db = pBt->pCursor->pBtree->pSqlite; |
| 2520 | if( db ){ |
| 2521 | sqlite3AbortOtherActiveVdbes(db, 0); |
| 2522 | } |
| 2523 | } |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2524 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2525 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2526 | btreeIntegrity(p); |
| 2527 | unlockAllTables(p); |
| 2528 | |
| 2529 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2530 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2531 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2532 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2533 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2534 | if( rc2!=SQLITE_OK ){ |
| 2535 | rc = rc2; |
| 2536 | } |
| 2537 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2538 | /* The rollback may have destroyed the pPage1->aData value. So |
| 2539 | ** call getPage() on page 1 again to make sure pPage1->aData is |
| 2540 | ** set correctly. */ |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 2541 | if( getPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2542 | releasePage(pPage1); |
| 2543 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2544 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2545 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2546 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2547 | |
| 2548 | if( p->inTrans!=TRANS_NONE ){ |
| 2549 | assert( pBt->nTransaction>0 ); |
| 2550 | pBt->nTransaction--; |
| 2551 | if( 0==pBt->nTransaction ){ |
| 2552 | pBt->inTransaction = TRANS_NONE; |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2557 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2558 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2559 | |
| 2560 | btreeIntegrity(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2561 | return rc; |
| 2562 | } |
| 2563 | |
| 2564 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2565 | ** Start a statement subtransaction. The subtransaction can |
| 2566 | ** can be rolled back independently of the main transaction. |
| 2567 | ** You must start a transaction before starting a subtransaction. |
| 2568 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2569 | ** commits or rolls back. |
| 2570 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2571 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2572 | ** to start a new subtransaction if another subtransaction is already active. |
| 2573 | ** |
| 2574 | ** Statement subtransactions are used around individual SQL statements |
| 2575 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2576 | ** error occurs within the statement, the effect of that one statement |
| 2577 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2578 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2579 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2580 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2581 | BtShared *pBt = p->pBt; |
| 2582 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2583 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2584 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2585 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2586 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2587 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2588 | return rc; |
| 2589 | } |
| 2590 | |
| 2591 | |
| 2592 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2593 | ** Commit the statment subtransaction currently in progress. If no |
| 2594 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2595 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2596 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2597 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2598 | BtShared *pBt = p->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2599 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2600 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2601 | }else{ |
| 2602 | rc = SQLITE_OK; |
| 2603 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2604 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2605 | return rc; |
| 2606 | } |
| 2607 | |
| 2608 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2609 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2610 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2611 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2612 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2613 | ** to use a cursor that was open at the beginning of this operation |
| 2614 | ** will result in an error. |
| 2615 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2616 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2617 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2618 | BtShared *pBt = p->pBt; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2619 | sqlite3MallocDisallow(); |
| 2620 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2621 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2622 | assert( countWriteCursors(pBt)==0 ); |
| 2623 | pBt->inStmt = 0; |
| 2624 | } |
| 2625 | sqlite3MallocAllow(); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2626 | return rc; |
| 2627 | } |
| 2628 | |
| 2629 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2630 | ** Default key comparison function to be used if no comparison function |
| 2631 | ** is specified on the sqlite3BtreeCursor() call. |
| 2632 | */ |
| 2633 | static int dfltCompare( |
| 2634 | void *NotUsed, /* User data is not used */ |
| 2635 | int n1, const void *p1, /* First key to compare */ |
| 2636 | int n2, const void *p2 /* Second key to compare */ |
| 2637 | ){ |
| 2638 | int c; |
| 2639 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2640 | if( c==0 ){ |
| 2641 | c = n1 - n2; |
| 2642 | } |
| 2643 | return c; |
| 2644 | } |
| 2645 | |
| 2646 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2647 | ** Create a new cursor for the BTree whose root is on the page |
| 2648 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2649 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2650 | ** |
| 2651 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2652 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2653 | ** writing if other conditions for writing are also met. These |
| 2654 | ** are the conditions that must be met in order for writing to |
| 2655 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2656 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2657 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2658 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2659 | ** 2: Other database connections that share the same pager cache |
| 2660 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2661 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2662 | ** the changes made by this write cursor would be visible to |
| 2663 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2664 | ** |
| 2665 | ** 3: The database must be writable (not on read-only media) |
| 2666 | ** |
| 2667 | ** 4: There must be an active transaction. |
| 2668 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2669 | ** No checking is done to make sure that page iTable really is the |
| 2670 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2671 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2672 | ** |
| 2673 | ** The comparison function must be logically the same for every cursor |
| 2674 | ** on a particular table. Changing the comparison function will result |
| 2675 | ** in incorrect operations. If the comparison function is NULL, a |
| 2676 | ** default comparison function is used. The comparison function is |
| 2677 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2678 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2679 | int sqlite3BtreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2680 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2681 | int iTable, /* Root page of table to open */ |
| 2682 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2683 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2684 | void *pArg, /* First arg to xCompare() */ |
| 2685 | BtCursor **ppCur /* Write new cursor here */ |
| 2686 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2687 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2688 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2689 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2690 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2691 | *ppCur = 0; |
| 2692 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2693 | if( pBt->readOnly ){ |
| 2694 | return SQLITE_READONLY; |
| 2695 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2696 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2697 | return SQLITE_LOCKED; |
| 2698 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2699 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2700 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2701 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2702 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2703 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2704 | return rc; |
| 2705 | } |
| 2706 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2707 | pCur = sqliteMalloc( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2708 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2709 | rc = SQLITE_NOMEM; |
| 2710 | goto create_cursor_exception; |
| 2711 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2712 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2713 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2714 | rc = SQLITE_EMPTY; |
| 2715 | goto create_cursor_exception; |
| 2716 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2717 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2718 | if( rc!=SQLITE_OK ){ |
| 2719 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2720 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2721 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2722 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2723 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2724 | ** output argument to this function). |
| 2725 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2726 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2727 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2728 | pCur->pBtree = p; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2729 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2730 | pCur->pNext = pBt->pCursor; |
| 2731 | if( pCur->pNext ){ |
| 2732 | pCur->pNext->pPrev = pCur; |
| 2733 | } |
| 2734 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2735 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2736 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2737 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2738 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2739 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2740 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2741 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2742 | sqliteFree(pCur); |
| 2743 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2744 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2745 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2748 | #if 0 /* Not Used */ |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2749 | /* |
| 2750 | ** Change the value of the comparison function used by a cursor. |
| 2751 | */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2752 | void sqlite3BtreeSetCompare( |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 2753 | BtCursor *pCur, /* The cursor to whose comparison function is changed */ |
| 2754 | int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */ |
| 2755 | void *pArg /* First argument to xCmp() */ |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2756 | ){ |
| 2757 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2758 | pCur->pArg = pArg; |
| 2759 | } |
drh | 7a224de | 2004-06-02 01:22:02 +0000 | [diff] [blame] | 2760 | #endif |
danielk1977 | bf3b721 | 2004-05-18 10:06:24 +0000 | [diff] [blame] | 2761 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2762 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2763 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2764 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2765 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2766 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2767 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2768 | restoreOrClearCursorPosition(pCur, 0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2769 | if( pCur->pPrev ){ |
| 2770 | pCur->pPrev->pNext = pCur->pNext; |
| 2771 | }else{ |
| 2772 | pBt->pCursor = pCur->pNext; |
| 2773 | } |
| 2774 | if( pCur->pNext ){ |
| 2775 | pCur->pNext->pPrev = pCur->pPrev; |
| 2776 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2777 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2778 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2779 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2780 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2783 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2784 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2785 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2786 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2787 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2788 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2789 | pTempCur->pNext = 0; |
| 2790 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2791 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2792 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2793 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2797 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2798 | ** function above. |
| 2799 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2800 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2801 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2802 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2803 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /* |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2807 | ** Make sure the BtCursor.info field of the given cursor is valid. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2808 | ** If it is not already valid, call parseCell() to fill it in. |
| 2809 | ** |
| 2810 | ** BtCursor.info is a cache of the information in the current cell. |
| 2811 | ** Using this cache reduces the number of calls to parseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2812 | */ |
| 2813 | static void getCellInfo(BtCursor *pCur){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2814 | if( pCur->info.nSize==0 ){ |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2815 | parseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2816 | }else{ |
| 2817 | #ifndef NDEBUG |
| 2818 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2819 | memset(&info, 0, sizeof(info)); |
drh | 3a41a3f | 2004-05-30 02:14:17 +0000 | [diff] [blame] | 2820 | parseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2821 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
| 2822 | #endif |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2827 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2828 | ** the key for the current entry. If the cursor is not pointing |
| 2829 | ** to a valid entry, *pSize is set to 0. |
| 2830 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2831 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2832 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2833 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2834 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2835 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2836 | if( rc==SQLITE_OK ){ |
| 2837 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2838 | if( pCur->eState==CURSOR_INVALID ){ |
| 2839 | *pSize = 0; |
| 2840 | }else{ |
| 2841 | getCellInfo(pCur); |
| 2842 | *pSize = pCur->info.nKey; |
| 2843 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2844 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2845 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2846 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2847 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2848 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2849 | ** Set *pSize to the number of bytes of data in the entry the |
| 2850 | ** cursor currently points to. Always return SQLITE_OK. |
| 2851 | ** Failure is not possible. If the cursor is not currently |
| 2852 | ** pointing to an entry (which can happen, for example, if |
| 2853 | ** the database is empty) then *pSize is set to 0. |
| 2854 | */ |
| 2855 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2856 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2857 | if( rc==SQLITE_OK ){ |
| 2858 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2859 | if( pCur->eState==CURSOR_INVALID ){ |
| 2860 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2861 | *pSize = 0; |
| 2862 | }else{ |
| 2863 | getCellInfo(pCur); |
| 2864 | *pSize = pCur->info.nData; |
| 2865 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2866 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2867 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2871 | ** Read payload information from the entry that the pCur cursor is |
| 2872 | ** pointing to. Begin reading the payload at "offset" and read |
| 2873 | ** a total of "amt" bytes. Put the result in zBuf. |
| 2874 | ** |
| 2875 | ** This routine does not make a distinction between key and data. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2876 | ** It just reads bytes from the payload area. Data might appear |
| 2877 | ** on the main page or be scattered out on multiple overflow pages. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2878 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2879 | static int getPayload( |
| 2880 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2881 | int offset, /* Begin reading this far into payload */ |
| 2882 | int amt, /* Read this many bytes */ |
| 2883 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
| 2884 | int skipKey /* offset begins at data if this is true */ |
| 2885 | ){ |
| 2886 | unsigned char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2887 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2888 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2889 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2890 | BtShared *pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 2891 | int ovflSize; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2892 | u32 nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2893 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2894 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2895 | assert( pCur->eState==CURSOR_VALID ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2896 | pBt = pCur->pBtree->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2897 | pPage = pCur->pPage; |
| 2898 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2899 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 2900 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2901 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2902 | nKey = 0; |
| 2903 | }else{ |
| 2904 | nKey = pCur->info.nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2905 | } |
| 2906 | assert( offset>=0 ); |
| 2907 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2908 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2909 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2910 | if( offset+amt > nKey+pCur->info.nData ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2911 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2912 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2913 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2914 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2915 | if( a+offset>pCur->info.nLocal ){ |
| 2916 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2917 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2918 | memcpy(pBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2919 | if( a==amt ){ |
| 2920 | return SQLITE_OK; |
| 2921 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2922 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2923 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2924 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2925 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2926 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2927 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2928 | ovflSize = pBt->usableSize - 4; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2929 | if( amt>0 ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2930 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2931 | while( amt>0 && nextPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2932 | DbPage *pDbPage; |
| 2933 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2934 | if( rc!=0 ){ |
| 2935 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2936 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2937 | aPayload = sqlite3PagerGetData(pDbPage); |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2938 | nextPage = get4byte(aPayload); |
| 2939 | if( offset<ovflSize ){ |
| 2940 | int a = amt; |
| 2941 | if( a + offset > ovflSize ){ |
| 2942 | a = ovflSize - offset; |
| 2943 | } |
| 2944 | memcpy(pBuf, &aPayload[offset+4], a); |
| 2945 | offset = 0; |
| 2946 | amt -= a; |
| 2947 | pBuf += a; |
| 2948 | }else{ |
| 2949 | offset -= ovflSize; |
| 2950 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 2951 | sqlite3PagerUnref(pDbPage); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2952 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2953 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2954 | |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2955 | if( amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2956 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2957 | } |
| 2958 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2961 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2962 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2963 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2964 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2965 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2966 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2967 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2968 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2969 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2970 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2971 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2972 | if( rc==SQLITE_OK ){ |
| 2973 | assert( pCur->eState==CURSOR_VALID ); |
| 2974 | assert( pCur->pPage!=0 ); |
| 2975 | if( pCur->pPage->intKey ){ |
| 2976 | return SQLITE_CORRUPT_BKPT; |
| 2977 | } |
| 2978 | assert( pCur->pPage->intKey==0 ); |
| 2979 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
| 2980 | rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 2981 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2982 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2986 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2987 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2988 | ** begins at "offset". |
| 2989 | ** |
| 2990 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2991 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2992 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2993 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2994 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 2995 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2996 | if( rc==SQLITE_OK ){ |
| 2997 | assert( pCur->eState==CURSOR_VALID ); |
| 2998 | assert( pCur->pPage!=0 ); |
| 2999 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
| 3000 | rc = getPayload(pCur, offset, amt, pBuf, 1); |
| 3001 | } |
| 3002 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3005 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3006 | ** Return a pointer to payload information from the entry that the |
| 3007 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3008 | ** 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] | 3009 | ** skipKey==1. The number of bytes of available key/data is written |
| 3010 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3011 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3012 | ** |
| 3013 | ** This routine is an optimization. It is common for the entire key |
| 3014 | ** and data to fit on the local page and for there to be no overflow |
| 3015 | ** pages. When that is so, this routine can be used to access the |
| 3016 | ** key and data without making a copy. If the key and/or data spills |
| 3017 | ** onto overflow pages, then getPayload() must be used to reassembly |
| 3018 | ** the key/data and copy it into a preallocated buffer. |
| 3019 | ** |
| 3020 | ** The pointer returned by this routine looks directly into the cached |
| 3021 | ** page of the database. The data might change or move the next time |
| 3022 | ** any btree routine is called. |
| 3023 | */ |
| 3024 | static const unsigned char *fetchPayload( |
| 3025 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3026 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3027 | int skipKey /* read beginning at data if this is true */ |
| 3028 | ){ |
| 3029 | unsigned char *aPayload; |
| 3030 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3031 | u32 nKey; |
| 3032 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3033 | |
| 3034 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3035 | assert( pCur->eState==CURSOR_VALID ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3036 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3037 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 3038 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3039 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3040 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3041 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3042 | nKey = 0; |
| 3043 | }else{ |
| 3044 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3045 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3046 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3047 | aPayload += nKey; |
| 3048 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3049 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3050 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3051 | if( nLocal>nKey ){ |
| 3052 | nLocal = nKey; |
| 3053 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3054 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3055 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3056 | return aPayload; |
| 3057 | } |
| 3058 | |
| 3059 | |
| 3060 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3061 | ** For the entry that cursor pCur is point to, return as |
| 3062 | ** many bytes of the key or data as are available on the local |
| 3063 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3064 | ** |
| 3065 | ** The pointer returned is ephemeral. The key/data may move |
| 3066 | ** or be destroyed on the next call to any Btree routine. |
| 3067 | ** |
| 3068 | ** These routines is used to get quick access to key and data |
| 3069 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3070 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3071 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3072 | if( pCur->eState==CURSOR_VALID ){ |
| 3073 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3074 | } |
| 3075 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3076 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3077 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3078 | if( pCur->eState==CURSOR_VALID ){ |
| 3079 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3080 | } |
| 3081 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | |
| 3085 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3086 | ** 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] | 3087 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3088 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3089 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3090 | int rc; |
| 3091 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3092 | MemPage *pOldPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3093 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3094 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3095 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3096 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3097 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3098 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3099 | pOldPage = pCur->pPage; |
| 3100 | pOldPage->idxShift = 0; |
| 3101 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3102 | pCur->pPage = pNewPage; |
| 3103 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3104 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3105 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3106 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3107 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3108 | return SQLITE_OK; |
| 3109 | } |
| 3110 | |
| 3111 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3112 | ** Return true if the page is the virtual root of its table. |
| 3113 | ** |
| 3114 | ** The virtual root page is the root page for most tables. But |
| 3115 | ** for the table rooted on page 1, sometime the real root page |
| 3116 | ** is empty except for the right-pointer. In such cases the |
| 3117 | ** virtual root page is the page that the right-pointer of page |
| 3118 | ** 1 is pointing to. |
| 3119 | */ |
| 3120 | static int isRootPage(MemPage *pPage){ |
| 3121 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3122 | if( pParent==0 ) return 1; |
| 3123 | if( pParent->pgno>1 ) return 0; |
| 3124 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3129 | ** Move the cursor up to the parent page. |
| 3130 | ** |
| 3131 | ** pCur->idx is set to the cell index that contains the pointer |
| 3132 | ** to the page we are coming from. If we are coming from the |
| 3133 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3134 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3135 | */ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3136 | static void moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3137 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3138 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3139 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3140 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3141 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3142 | pPage = pCur->pPage; |
| 3143 | assert( pPage!=0 ); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3144 | assert( !isRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3145 | pParent = pPage->pParent; |
| 3146 | assert( pParent!=0 ); |
| 3147 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3148 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3149 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3150 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3151 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3152 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3153 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /* |
| 3157 | ** Move the cursor to the root page |
| 3158 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3159 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3160 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3161 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3162 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3163 | |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3164 | restoreOrClearCursorPosition(pCur, 0); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3165 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3166 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3167 | assert( pRoot->isInit ); |
| 3168 | }else{ |
| 3169 | if( |
| 3170 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3171 | ){ |
| 3172 | pCur->eState = CURSOR_INVALID; |
| 3173 | return rc; |
| 3174 | } |
| 3175 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3176 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3177 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3178 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3179 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3180 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3181 | Pgno subpage; |
| 3182 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3183 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3184 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3185 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3186 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3187 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3188 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3189 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3190 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3191 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3192 | /* |
| 3193 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3194 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3195 | ** |
| 3196 | ** The left-most leaf is the one with the smallest key - the first |
| 3197 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3198 | */ |
| 3199 | static int moveToLeftmost(BtCursor *pCur){ |
| 3200 | Pgno pgno; |
| 3201 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3202 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3203 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3204 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3205 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3206 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3207 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3208 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3209 | if( rc ) return rc; |
| 3210 | } |
| 3211 | return SQLITE_OK; |
| 3212 | } |
| 3213 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3214 | /* |
| 3215 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3216 | ** page to which it is currently pointing. Notice the difference |
| 3217 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3218 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3219 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3220 | ** |
| 3221 | ** The right-most entry is the one with the largest key - the last |
| 3222 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3223 | */ |
| 3224 | static int moveToRightmost(BtCursor *pCur){ |
| 3225 | Pgno pgno; |
| 3226 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3227 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3228 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3229 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3230 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3231 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3232 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3233 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3234 | if( rc ) return rc; |
| 3235 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3236 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3237 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3238 | return SQLITE_OK; |
| 3239 | } |
| 3240 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3241 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3242 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3243 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3244 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3245 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3246 | int rc; |
| 3247 | rc = moveToRoot(pCur); |
| 3248 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3249 | if( pCur->eState==CURSOR_INVALID ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3250 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3251 | *pRes = 1; |
| 3252 | return SQLITE_OK; |
| 3253 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3254 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3255 | *pRes = 0; |
| 3256 | rc = moveToLeftmost(pCur); |
| 3257 | return rc; |
| 3258 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3259 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3260 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3261 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3262 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3263 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3264 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3265 | int rc; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3266 | rc = moveToRoot(pCur); |
| 3267 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3268 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3269 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3270 | *pRes = 1; |
| 3271 | return SQLITE_OK; |
| 3272 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3273 | assert( pCur->eState==CURSOR_VALID ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3274 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3275 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3276 | return rc; |
| 3277 | } |
| 3278 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3279 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3280 | ** Return a success code. |
| 3281 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3282 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3283 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3284 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3285 | ** created is used to compare keys. |
| 3286 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3287 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3288 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3289 | ** were present. The cursor might point to an entry that comes |
| 3290 | ** before or after the key. |
| 3291 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3292 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3293 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3294 | ** this value is as follows: |
| 3295 | ** |
| 3296 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3297 | ** is smaller than pKey or if the table is empty |
| 3298 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3299 | ** |
| 3300 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3301 | ** exactly matches pKey. |
| 3302 | ** |
| 3303 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3304 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3305 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3306 | int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3307 | int rc; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3308 | int tryRightmost; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3309 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3310 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3311 | assert( pCur->pPage ); |
| 3312 | assert( pCur->pPage->isInit ); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3313 | tryRightmost = pCur->pPage->intKey; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3314 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3315 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3316 | assert( pCur->pPage->nCell==0 ); |
| 3317 | return SQLITE_OK; |
| 3318 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3319 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3320 | int lwr, upr; |
| 3321 | Pgno chldPg; |
| 3322 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3323 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3324 | lwr = 0; |
| 3325 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3326 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3327 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3328 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3329 | while( lwr<=upr ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3330 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3331 | i64 nCellKey; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3332 | pCur->idx = (lwr+upr)/2; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3333 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3334 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3335 | u8 *pCell; |
| 3336 | if( tryRightmost ){ |
| 3337 | pCur->idx = upr; |
| 3338 | } |
| 3339 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3340 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3341 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3342 | pCell += getVarint32(pCell, &dummy); |
| 3343 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3344 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3345 | if( nCellKey<nKey ){ |
| 3346 | c = -1; |
| 3347 | }else if( nCellKey>nKey ){ |
| 3348 | c = +1; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3349 | tryRightmost = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3350 | }else{ |
| 3351 | c = 0; |
| 3352 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3353 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3354 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3355 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3356 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3357 | if( available>=nCellKey ){ |
| 3358 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3359 | }else{ |
| 3360 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 3361 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3362 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3363 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3364 | sqliteFree(pCellKey); |
| 3365 | if( rc ) return rc; |
| 3366 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3367 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3368 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3369 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3370 | lwr = pCur->idx; |
| 3371 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3372 | break; |
| 3373 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3374 | if( pRes ) *pRes = 0; |
| 3375 | return SQLITE_OK; |
| 3376 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3377 | } |
| 3378 | if( c<0 ){ |
| 3379 | lwr = pCur->idx+1; |
| 3380 | }else{ |
| 3381 | upr = pCur->idx-1; |
| 3382 | } |
| 3383 | } |
| 3384 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3385 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3386 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3387 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3388 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3389 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3390 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3391 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3392 | } |
| 3393 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3394 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3395 | if( pRes ) *pRes = c; |
| 3396 | return SQLITE_OK; |
| 3397 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3398 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3399 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3400 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3401 | if( rc ){ |
| 3402 | return rc; |
| 3403 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3404 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3405 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3409 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3410 | ** |
| 3411 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3412 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3413 | ** the first entry. TRUE is also returned if the table is empty. |
| 3414 | */ |
| 3415 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3416 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3417 | ** have been deleted? This API will need to change to return an error code |
| 3418 | ** as well as the boolean result value. |
| 3419 | */ |
| 3420 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3424 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3425 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3426 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3427 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3428 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3429 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3430 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3431 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3432 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3433 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3434 | rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3435 | if( rc!=SQLITE_OK ){ |
| 3436 | return rc; |
| 3437 | } |
| 3438 | if( pCur->skip>0 ){ |
| 3439 | pCur->skip = 0; |
| 3440 | *pRes = 0; |
| 3441 | return SQLITE_OK; |
| 3442 | } |
| 3443 | pCur->skip = 0; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3444 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3445 | |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3446 | assert( pRes!=0 ); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3447 | pPage = pCur->pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3448 | if( CURSOR_INVALID==pCur->eState ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3449 | *pRes = 1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3450 | return SQLITE_OK; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 3451 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3452 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3453 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3454 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3455 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3456 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3457 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3458 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3459 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3460 | if( rc ) return rc; |
| 3461 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3462 | *pRes = 0; |
| 3463 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3464 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3465 | do{ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3466 | if( isRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3467 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3468 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3469 | return SQLITE_OK; |
| 3470 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3471 | moveToParent(pCur); |
| 3472 | pPage = pCur->pPage; |
| 3473 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3474 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3475 | if( pPage->leafData ){ |
| 3476 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3477 | }else{ |
| 3478 | rc = SQLITE_OK; |
| 3479 | } |
| 3480 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3481 | } |
| 3482 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3483 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3484 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3485 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3486 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3487 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3490 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3491 | ** 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] | 3492 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3493 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3494 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3495 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3496 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3497 | int rc; |
| 3498 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3499 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3500 | |
| 3501 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3502 | rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3503 | if( rc!=SQLITE_OK ){ |
| 3504 | return rc; |
| 3505 | } |
| 3506 | if( pCur->skip<0 ){ |
| 3507 | pCur->skip = 0; |
| 3508 | *pRes = 0; |
| 3509 | return SQLITE_OK; |
| 3510 | } |
| 3511 | pCur->skip = 0; |
| 3512 | #endif |
| 3513 | |
| 3514 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3515 | *pRes = 1; |
| 3516 | return SQLITE_OK; |
| 3517 | } |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3518 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3519 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3520 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3521 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3522 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3523 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3524 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3525 | if( rc ) return rc; |
| 3526 | rc = moveToRightmost(pCur); |
| 3527 | }else{ |
| 3528 | while( pCur->idx==0 ){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3529 | if( isRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3530 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3531 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3532 | return SQLITE_OK; |
| 3533 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3534 | moveToParent(pCur); |
| 3535 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3536 | } |
| 3537 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3538 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3539 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3540 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3541 | }else{ |
| 3542 | rc = SQLITE_OK; |
| 3543 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3544 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3545 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3546 | return rc; |
| 3547 | } |
| 3548 | |
| 3549 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3550 | ** Allocate a new page from the database file. |
| 3551 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3552 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3553 | ** has already been called on the new page.) The new page has also |
| 3554 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3555 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3556 | ** |
| 3557 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3558 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3559 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3560 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3561 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3562 | ** 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] | 3563 | ** attempt to keep related pages close to each other in the database file, |
| 3564 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3565 | ** |
| 3566 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3567 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3568 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3569 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3570 | static int allocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3571 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3572 | MemPage **ppPage, |
| 3573 | Pgno *pPgno, |
| 3574 | Pgno nearby, |
| 3575 | u8 exact |
| 3576 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3577 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3578 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3579 | int n; /* Number of pages on the freelist */ |
| 3580 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3581 | MemPage *pTrunk = 0; |
| 3582 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3583 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3584 | pPage1 = pBt->pPage1; |
| 3585 | n = get4byte(&pPage1->aData[36]); |
| 3586 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3587 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3588 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3589 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3590 | |
| 3591 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3592 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3593 | ** the entire-list will be searched for that page. |
| 3594 | */ |
| 3595 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3596 | if( exact ){ |
| 3597 | u8 eType; |
| 3598 | assert( nearby>0 ); |
| 3599 | assert( pBt->autoVacuum ); |
| 3600 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3601 | if( rc ) return rc; |
| 3602 | if( eType==PTRMAP_FREEPAGE ){ |
| 3603 | searchList = 1; |
| 3604 | } |
| 3605 | *pPgno = nearby; |
| 3606 | } |
| 3607 | #endif |
| 3608 | |
| 3609 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3610 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3611 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3612 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3613 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3614 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3615 | |
| 3616 | /* The code within this loop is run only once if the 'searchList' variable |
| 3617 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3618 | ** free-list until the page 'nearby' is located. |
| 3619 | */ |
| 3620 | do { |
| 3621 | pPrevTrunk = pTrunk; |
| 3622 | if( pPrevTrunk ){ |
| 3623 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3624 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3625 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3626 | } |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3627 | rc = getPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3628 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3629 | pTrunk = 0; |
| 3630 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | k = get4byte(&pTrunk->aData[4]); |
| 3634 | if( k==0 && !searchList ){ |
| 3635 | /* The trunk has no leaves and the list is not being searched. |
| 3636 | ** So extract the trunk page itself and use it as the newly |
| 3637 | ** allocated page */ |
| 3638 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3639 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3640 | if( rc ){ |
| 3641 | goto end_allocate_page; |
| 3642 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3643 | *pPgno = iTrunk; |
| 3644 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3645 | *ppPage = pTrunk; |
| 3646 | pTrunk = 0; |
| 3647 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3648 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3649 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3650 | rc = SQLITE_CORRUPT_BKPT; |
| 3651 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3652 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3653 | }else if( searchList && nearby==iTrunk ){ |
| 3654 | /* The list is being searched and this trunk page is the page |
| 3655 | ** to allocate, regardless of whether it has leaves. |
| 3656 | */ |
| 3657 | assert( *pPgno==iTrunk ); |
| 3658 | *ppPage = pTrunk; |
| 3659 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3660 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3661 | if( rc ){ |
| 3662 | goto end_allocate_page; |
| 3663 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3664 | if( k==0 ){ |
| 3665 | if( !pPrevTrunk ){ |
| 3666 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3667 | }else{ |
| 3668 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3669 | } |
| 3670 | }else{ |
| 3671 | /* The trunk page is required by the caller but it contains |
| 3672 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3673 | ** page in this case. |
| 3674 | */ |
| 3675 | MemPage *pNewTrunk; |
| 3676 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3677 | rc = getPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3678 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3679 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3680 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3681 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3682 | if( rc!=SQLITE_OK ){ |
| 3683 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3684 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3685 | } |
| 3686 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3687 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3688 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3689 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3690 | if( !pPrevTrunk ){ |
| 3691 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3692 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3693 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3694 | if( rc ){ |
| 3695 | goto end_allocate_page; |
| 3696 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3697 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3698 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3699 | } |
| 3700 | pTrunk = 0; |
| 3701 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3702 | #endif |
| 3703 | }else{ |
| 3704 | /* Extract a leaf from the trunk */ |
| 3705 | int closest; |
| 3706 | Pgno iPage; |
| 3707 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3708 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3709 | if( rc ){ |
| 3710 | goto end_allocate_page; |
| 3711 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3712 | if( nearby>0 ){ |
| 3713 | int i, dist; |
| 3714 | closest = 0; |
| 3715 | dist = get4byte(&aData[8]) - nearby; |
| 3716 | if( dist<0 ) dist = -dist; |
| 3717 | for(i=1; i<k; i++){ |
| 3718 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3719 | if( d2<0 ) d2 = -d2; |
| 3720 | if( d2<dist ){ |
| 3721 | closest = i; |
| 3722 | dist = d2; |
| 3723 | } |
| 3724 | } |
| 3725 | }else{ |
| 3726 | closest = 0; |
| 3727 | } |
| 3728 | |
| 3729 | iPage = get4byte(&aData[8+closest*4]); |
| 3730 | if( !searchList || iPage==nearby ){ |
| 3731 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3732 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3733 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3734 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3735 | } |
| 3736 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3737 | ": %d more free pages\n", |
| 3738 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3739 | if( closest<k-1 ){ |
| 3740 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3741 | } |
| 3742 | put4byte(&aData[4], k-1); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3743 | rc = getPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3744 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3745 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3746 | if( rc!=SQLITE_OK ){ |
| 3747 | releasePage(*ppPage); |
| 3748 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3749 | } |
| 3750 | searchList = 0; |
| 3751 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3752 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3753 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3754 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3755 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3756 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3757 | /* There are no pages on the freelist, so create a new page at the |
| 3758 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3759 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3760 | |
| 3761 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 3762 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3763 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3764 | ** at the end of the file instead of one. The first allocated page |
| 3765 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3766 | */ |
| 3767 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3768 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3769 | (*pPgno)++; |
| 3770 | } |
| 3771 | #endif |
| 3772 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3773 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3774 | rc = getPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3775 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3776 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3777 | if( rc!=SQLITE_OK ){ |
| 3778 | releasePage(*ppPage); |
| 3779 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3780 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3781 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3782 | |
| 3783 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3784 | |
| 3785 | end_allocate_page: |
| 3786 | releasePage(pTrunk); |
| 3787 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3788 | return rc; |
| 3789 | } |
| 3790 | |
| 3791 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3792 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3793 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3794 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3795 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3796 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3797 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3798 | MemPage *pPage1 = pBt->pPage1; |
| 3799 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3800 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3801 | /* Prepare the page for freeing */ |
| 3802 | assert( pPage->pgno>1 ); |
| 3803 | pPage->isInit = 0; |
| 3804 | releasePage(pPage->pParent); |
| 3805 | pPage->pParent = 0; |
| 3806 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3807 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3808 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3809 | if( rc ) return rc; |
| 3810 | n = get4byte(&pPage1->aData[36]); |
| 3811 | put4byte(&pPage1->aData[36], n+1); |
| 3812 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3813 | #ifdef SQLITE_SECURE_DELETE |
| 3814 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 3815 | ** always fully overwrite deleted information with zeros. |
| 3816 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3817 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3818 | if( rc ) return rc; |
| 3819 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 3820 | #endif |
| 3821 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3822 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3823 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3824 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3825 | */ |
| 3826 | if( pBt->autoVacuum ){ |
| 3827 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3828 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3829 | } |
| 3830 | #endif |
| 3831 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3832 | if( n==0 ){ |
| 3833 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3834 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3835 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3836 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3837 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3838 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3839 | }else{ |
| 3840 | /* Other free pages already exist. Retrive the first trunk page |
| 3841 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3842 | MemPage *pTrunk; |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 3843 | rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3844 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3845 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3846 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3847 | /* The trunk is full. Turn the page being freed into a new |
| 3848 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3849 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3850 | if( rc ) return rc; |
| 3851 | put4byte(pPage->aData, pTrunk->pgno); |
| 3852 | put4byte(&pPage->aData[4], 0); |
| 3853 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3854 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3855 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3856 | }else{ |
| 3857 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3858 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3859 | if( rc ) return rc; |
| 3860 | put4byte(&pTrunk->aData[4], k+1); |
| 3861 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3862 | #ifndef SQLITE_SECURE_DELETE |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3863 | sqlite3PagerDontWrite(pBt->pPager, pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3864 | #endif |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3865 | 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] | 3866 | } |
| 3867 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3868 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3869 | return rc; |
| 3870 | } |
| 3871 | |
| 3872 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3873 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3874 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3875 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3876 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3877 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3878 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3879 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3880 | int nOvfl; |
| 3881 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3882 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3883 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3884 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3885 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3886 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3887 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3888 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 3889 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 3890 | assert( ovflPgno==0 || nOvfl>0 ); |
| 3891 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3892 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3893 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3894 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 3895 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 3896 | rc = getPage(pBt, ovflPgno, &pOvfl, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3897 | if( rc ) return rc; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 3898 | if( nOvfl ){ |
| 3899 | ovflPgno = get4byte(pOvfl->aData); |
| 3900 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3901 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 3902 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3903 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3904 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3905 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3909 | ** Create the byte sequence used to represent a cell on page pPage |
| 3910 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3911 | ** allocated and filled in as necessary. The calling procedure |
| 3912 | ** is responsible for making sure sufficient space has been allocated |
| 3913 | ** for pCell[]. |
| 3914 | ** |
| 3915 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3916 | ** area. pCell might point to some temporary storage. The cell will |
| 3917 | ** be constructed in this temporary area then copied into pPage->aData |
| 3918 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3919 | */ |
| 3920 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3921 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3922 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3923 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3924 | const void *pData,int nData, /* The data */ |
| 3925 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3926 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3927 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3928 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3929 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3930 | int spaceLeft; |
| 3931 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3932 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3933 | unsigned char *pPrior; |
| 3934 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3935 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3936 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3937 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3938 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3939 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3940 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3941 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3942 | if( !pPage->leaf ){ |
| 3943 | nHeader += 4; |
| 3944 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3945 | if( pPage->hasData ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3946 | nHeader += putVarint(&pCell[nHeader], nData); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3947 | }else{ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3948 | nData = 0; |
| 3949 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3950 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3951 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3952 | assert( info.nHeader==nHeader ); |
| 3953 | assert( info.nKey==nKey ); |
| 3954 | assert( info.nData==nData ); |
| 3955 | |
| 3956 | /* Fill in the payload */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3957 | nPayload = nData; |
| 3958 | if( pPage->intKey ){ |
| 3959 | pSrc = pData; |
| 3960 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3961 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3962 | }else{ |
| 3963 | nPayload += nKey; |
| 3964 | pSrc = pKey; |
| 3965 | nSrc = nKey; |
| 3966 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3967 | *pnSize = info.nSize; |
| 3968 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3969 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3970 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3971 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3972 | while( nPayload>0 ){ |
| 3973 | if( spaceLeft==0 ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3974 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3975 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
| 3976 | #endif |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3977 | rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3978 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3979 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3980 | ** overflow page is being allocated, add an entry to the pointer-map |
| 3981 | ** for that page now. The entry for the first overflow page will be |
| 3982 | ** added later, by the insertCell() routine. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3983 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3984 | if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ |
| 3985 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3986 | } |
| 3987 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3988 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3989 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3990 | return rc; |
| 3991 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3992 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3993 | releasePage(pToRelease); |
| 3994 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3995 | pPrior = pOvfl->aData; |
| 3996 | put4byte(pPrior, 0); |
| 3997 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3998 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3999 | } |
| 4000 | n = nPayload; |
| 4001 | if( n>spaceLeft ) n = spaceLeft; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4002 | if( n>nSrc ) n = nSrc; |
drh | ff3b170 | 2006-03-11 12:04:18 +0000 | [diff] [blame] | 4003 | assert( pSrc ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4004 | memcpy(pPayload, pSrc, n); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4005 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4006 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4007 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4008 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4009 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4010 | if( nSrc==0 ){ |
| 4011 | nSrc = nData; |
| 4012 | pSrc = pData; |
| 4013 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4014 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4015 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4016 | return SQLITE_OK; |
| 4017 | } |
| 4018 | |
| 4019 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4020 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4021 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4022 | ** pointer in the third argument. |
| 4023 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4024 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4025 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4026 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4027 | |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4028 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4029 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4030 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4031 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4032 | if( pDbPage ){ |
| 4033 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4034 | if( pThis->isInit ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4035 | assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4036 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4037 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4038 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4039 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4040 | } |
| 4041 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4042 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4043 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4044 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4045 | |
| 4046 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4047 | if( pBt->autoVacuum ){ |
| 4048 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4049 | } |
| 4050 | #endif |
| 4051 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4054 | |
| 4055 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4056 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4057 | ** Change the pParent pointer of all children of pPage to point back |
| 4058 | ** to pPage. |
| 4059 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4060 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4061 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4062 | ** |
| 4063 | ** This routine gets called after you memcpy() one page into |
| 4064 | ** another. |
| 4065 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4066 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4067 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4068 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4069 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4070 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4071 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4072 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4073 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4074 | u8 *pCell = findCell(pPage, i); |
| 4075 | if( !pPage->leaf ){ |
| 4076 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4077 | if( rc!=SQLITE_OK ) return rc; |
| 4078 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4079 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4080 | if( !pPage->leaf ){ |
| 4081 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4082 | pPage, i); |
| 4083 | pPage->idxShift = 0; |
| 4084 | } |
| 4085 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4090 | ** The cell content is not freed or deallocated. It is assumed that |
| 4091 | ** the cell content has been copied someplace else. This routine just |
| 4092 | ** removes the reference to the cell from pPage. |
| 4093 | ** |
| 4094 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4095 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4096 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4097 | int i; /* Loop counter */ |
| 4098 | int pc; /* Offset to cell content of cell being deleted */ |
| 4099 | u8 *data; /* pPage->aData */ |
| 4100 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4101 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4102 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4103 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4104 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4105 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4106 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4107 | pc = get2byte(ptr); |
| 4108 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4109 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4110 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4111 | ptr[0] = ptr[2]; |
| 4112 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4113 | } |
| 4114 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4115 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4116 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4117 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | /* |
| 4121 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4122 | ** content of the cell. |
| 4123 | ** |
| 4124 | ** 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] | 4125 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4126 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4127 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4128 | ** in pTemp or the original pCell) and also record its index. |
| 4129 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4130 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4131 | ** |
| 4132 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4133 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4134 | ** 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] | 4135 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4136 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4137 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4138 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4139 | int i, /* New cell becomes the i-th cell of the page */ |
| 4140 | u8 *pCell, /* Content of the new cell */ |
| 4141 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4142 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4143 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4144 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4145 | int idx; /* Where to write new cell content in data[] */ |
| 4146 | int j; /* Loop counter */ |
| 4147 | int top; /* First byte of content for any cell in data[] */ |
| 4148 | int end; /* First byte past the last cell pointer in data[] */ |
| 4149 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4150 | int hdr; /* Offset into data[] of the page header */ |
| 4151 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4152 | u8 *data; /* The content of the whole page */ |
| 4153 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4154 | |
| 4155 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4156 | assert( sz==cellSizePtr(pPage, pCell) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4157 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4158 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4159 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4160 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4161 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4162 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4163 | j = pPage->nOverflow++; |
| 4164 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4165 | pPage->aOvfl[j].pCell = pCell; |
| 4166 | pPage->aOvfl[j].idx = i; |
| 4167 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4168 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4169 | data = pPage->aData; |
| 4170 | hdr = pPage->hdrOffset; |
| 4171 | top = get2byte(&data[hdr+5]); |
| 4172 | cellOffset = pPage->cellOffset; |
| 4173 | end = cellOffset + 2*pPage->nCell + 2; |
| 4174 | ins = cellOffset + 2*i; |
| 4175 | if( end > top - sz ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4176 | int rc = defragmentPage(pPage); |
| 4177 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4178 | top = get2byte(&data[hdr+5]); |
| 4179 | assert( end + sz <= top ); |
| 4180 | } |
| 4181 | idx = allocateSpace(pPage, sz); |
| 4182 | assert( idx>0 ); |
| 4183 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4184 | pPage->nCell++; |
| 4185 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4186 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4187 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4188 | ptr[0] = ptr[-2]; |
| 4189 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4190 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4191 | put2byte(&data[ins], idx); |
| 4192 | put2byte(&data[hdr+3], pPage->nCell); |
| 4193 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4194 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4195 | if( pPage->pBt->autoVacuum ){ |
| 4196 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4197 | ** the entry for the overflow page into the pointer map. |
| 4198 | */ |
| 4199 | CellInfo info; |
| 4200 | parseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4201 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4202 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4203 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 4204 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 4205 | if( rc!=SQLITE_OK ) return rc; |
| 4206 | } |
| 4207 | } |
| 4208 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4209 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4210 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4211 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4215 | ** Add a list of cells to a page. The page should be initially empty. |
| 4216 | ** The cells are guaranteed to fit on the page. |
| 4217 | */ |
| 4218 | static void assemblePage( |
| 4219 | MemPage *pPage, /* The page to be assemblied */ |
| 4220 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4221 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4222 | int *aSize /* Sizes of the cells */ |
| 4223 | ){ |
| 4224 | int i; /* Loop counter */ |
| 4225 | int totalSize; /* Total size of all cells */ |
| 4226 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4227 | int cellptr; /* Address of next cell pointer */ |
| 4228 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4229 | u8 *data; /* Data for the page */ |
| 4230 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4231 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4232 | totalSize = 0; |
| 4233 | for(i=0; i<nCell; i++){ |
| 4234 | totalSize += aSize[i]; |
| 4235 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4236 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4237 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4238 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4239 | data = pPage->aData; |
| 4240 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4241 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4242 | if( nCell ){ |
| 4243 | cellbody = allocateSpace(pPage, totalSize); |
| 4244 | assert( cellbody>0 ); |
| 4245 | assert( pPage->nFree >= 2*nCell ); |
| 4246 | pPage->nFree -= 2*nCell; |
| 4247 | for(i=0; i<nCell; i++){ |
| 4248 | put2byte(&data[cellptr], cellbody); |
| 4249 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4250 | cellptr += 2; |
| 4251 | cellbody += aSize[i]; |
| 4252 | } |
| 4253 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4254 | } |
| 4255 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4256 | } |
| 4257 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4258 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4259 | ** The following parameters determine how many adjacent pages get involved |
| 4260 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4261 | ** of the page that participate in the balancing operation. NB is the |
| 4262 | ** total number of pages that participate, including the target page and |
| 4263 | ** NN neighbors on either side. |
| 4264 | ** |
| 4265 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4266 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4267 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4268 | ** The value of NN appears to give the best results overall. |
| 4269 | */ |
| 4270 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4271 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4272 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4273 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4274 | static int balance(MemPage*, int); |
| 4275 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4276 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4277 | /* |
| 4278 | ** This version of balance() handles the common special case where |
| 4279 | ** a new entry is being inserted on the extreme right-end of the |
| 4280 | ** tree, in other words, when the new entry will become the largest |
| 4281 | ** entry in the tree. |
| 4282 | ** |
| 4283 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4284 | ** a new page to the right-hand side and put the one new entry in |
| 4285 | ** that page. This leaves the right side of the tree somewhat |
| 4286 | ** unbalanced. But odds are that we will be inserting new entries |
| 4287 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4288 | ** fill up. On average. |
| 4289 | ** |
| 4290 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4291 | ** pParent is its parent. pPage must have a single overflow entry |
| 4292 | ** which is also the right-most entry on the page. |
| 4293 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4294 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4295 | int rc; |
| 4296 | MemPage *pNew; |
| 4297 | Pgno pgnoNew; |
| 4298 | u8 *pCell; |
| 4299 | int szCell; |
| 4300 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4301 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4302 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4303 | int parentSize; /* Size of new divider cell */ |
| 4304 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4305 | |
| 4306 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4307 | ** into it. Then remove the overflow cell from pPage. |
| 4308 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4309 | rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4310 | if( rc!=SQLITE_OK ){ |
| 4311 | return rc; |
| 4312 | } |
| 4313 | pCell = pPage->aOvfl[0].pCell; |
| 4314 | szCell = cellSizePtr(pPage, pCell); |
| 4315 | zeroPage(pNew, pPage->aData[0]); |
| 4316 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4317 | pPage->nOverflow = 0; |
| 4318 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4319 | /* Set the parent of the newly allocated page to pParent. */ |
| 4320 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4321 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4322 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4323 | /* pPage is currently the right-child of pParent. Change this |
| 4324 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4325 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4326 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4327 | assert( pPage->nCell>0 ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4328 | parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info); |
| 4329 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize); |
| 4330 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4331 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4332 | } |
| 4333 | assert( parentSize<64 ); |
| 4334 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4335 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4336 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4337 | } |
| 4338 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4339 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4340 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4341 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4342 | /* If this is an auto-vacuum database, update the pointer map |
| 4343 | ** with entries for the new page, and any pointer from the |
| 4344 | ** cell on the page to an overflow page. |
| 4345 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4346 | if( pBt->autoVacuum ){ |
| 4347 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 4348 | if( rc!=SQLITE_OK ){ |
| 4349 | return rc; |
| 4350 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4351 | rc = ptrmapPutOvfl(pNew, 0); |
| 4352 | if( rc!=SQLITE_OK ){ |
| 4353 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4354 | } |
| 4355 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4356 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4357 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4358 | /* Release the reference to the new page and balance the parent page, |
| 4359 | ** in case the divider cell inserted caused it to become overfull. |
| 4360 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4361 | releasePage(pNew); |
| 4362 | return balance(pParent, 0); |
| 4363 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4364 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4365 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4366 | /* |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4367 | ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine |
| 4368 | ** if the database supports auto-vacuum or not. Because it is used |
| 4369 | ** within an expression that is an argument to another macro |
| 4370 | ** (sqliteMallocRaw), it is not possible to use conditional compilation. |
| 4371 | ** So, this macro is defined instead. |
| 4372 | */ |
| 4373 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4374 | #define ISAUTOVACUUM (pBt->autoVacuum) |
| 4375 | #else |
| 4376 | #define ISAUTOVACUUM 0 |
| 4377 | #endif |
| 4378 | |
| 4379 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4380 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4381 | ** 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] | 4382 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4383 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4384 | ** 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] | 4385 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4386 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4387 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4388 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4389 | ** 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] | 4390 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4391 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4392 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4393 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4394 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4395 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4396 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4397 | ** 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] | 4398 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4399 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4400 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4401 | ** might become overfull or underfull. If that happens, then this routine |
| 4402 | ** is called recursively on the parent. |
| 4403 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4404 | ** If this routine fails for any reason, it might leave the database |
| 4405 | ** in a corrupted state. So if this routine fails, the database should |
| 4406 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4407 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4408 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4409 | MemPage *pParent; /* The parent of pPage */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4410 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4411 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4412 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4413 | int nOld; /* Number of pages in apOld[] */ |
| 4414 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4415 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4416 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4417 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4418 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4419 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4420 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4421 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4422 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4423 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4424 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4425 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4426 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4427 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4428 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4429 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4430 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4431 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4432 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4433 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4434 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4435 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4436 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4437 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4438 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4439 | u8 *aFrom = 0; |
| 4440 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4441 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4442 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4443 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4444 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4445 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4446 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4447 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4448 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4449 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4450 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4451 | return rc; |
| 4452 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4453 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4454 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4455 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4456 | /* |
| 4457 | ** A special case: If a new entry has just been inserted into a |
| 4458 | ** 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] | 4459 | ** 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] | 4460 | ** largest key) then use the special balance_quick() routine for |
| 4461 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4462 | ** packing of data in the common case. |
| 4463 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4464 | if( pPage->leaf && |
| 4465 | pPage->intKey && |
| 4466 | pPage->leafData && |
| 4467 | pPage->nOverflow==1 && |
| 4468 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4469 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4470 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4471 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4472 | /* |
| 4473 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4474 | ** they are not full and no new page is required. |
| 4475 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4476 | return balance_quick(pPage, pParent); |
| 4477 | } |
| 4478 | #endif |
| 4479 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4480 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4481 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4482 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4483 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4484 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4485 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4486 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4487 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4488 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4489 | for(idx=0; idx<pParent->nCell; idx++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4490 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4491 | break; |
| 4492 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4493 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4494 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4495 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4496 | }else{ |
| 4497 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4498 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4499 | |
| 4500 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4501 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4502 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4503 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4504 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4505 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4506 | |
| 4507 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4508 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4509 | ** the siblings. An attempt is made to find NN siblings on either |
| 4510 | ** side of pPage. More siblings are taken from one side, however, if |
| 4511 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4512 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4513 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4514 | nxDiv = idx - NN; |
| 4515 | if( nxDiv + NB > pParent->nCell ){ |
| 4516 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4517 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4518 | if( nxDiv<0 ){ |
| 4519 | nxDiv = 0; |
| 4520 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4521 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4522 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4523 | if( k<pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4524 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4525 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4526 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4527 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4528 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4529 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4530 | }else{ |
| 4531 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4532 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4533 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4534 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4535 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4536 | apCopy[i] = 0; |
| 4537 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4538 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4539 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4542 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4543 | ** alignment */ |
| 4544 | nMaxCells = (nMaxCells + 1)&~1; |
| 4545 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4546 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4547 | ** Allocate space for memory structures |
| 4548 | */ |
| 4549 | apCell = sqliteMallocRaw( |
| 4550 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4551 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4552 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4553 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4554 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4555 | ); |
| 4556 | if( apCell==0 ){ |
| 4557 | rc = SQLITE_NOMEM; |
| 4558 | goto balance_cleanup; |
| 4559 | } |
| 4560 | szCell = (int*)&apCell[nMaxCells]; |
| 4561 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4562 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4563 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4564 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4565 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4566 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4567 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4568 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4569 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4570 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4571 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4572 | } |
| 4573 | #endif |
| 4574 | |
| 4575 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4576 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4577 | ** The rest of this function will use data from the copies rather |
| 4578 | ** that the original pages since the original pages will be in the |
| 4579 | ** process of being overwritten. |
| 4580 | */ |
| 4581 | for(i=0; i<nOld; i++){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4582 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4583 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 4584 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 4585 | /* The memcpy() above changes the value of p->aData so we have to |
| 4586 | ** set it again. */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4587 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | /* |
| 4591 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4592 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4593 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4594 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4595 | ** |
| 4596 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4597 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4598 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4599 | ** child pointers. If siblings are not leaves, then all cell in |
| 4600 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4601 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4602 | ** |
| 4603 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4604 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4605 | */ |
| 4606 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4607 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4608 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4609 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4610 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4611 | int limit = pOld->nCell+pOld->nOverflow; |
| 4612 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4613 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4614 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4615 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4616 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4617 | if( pBt->autoVacuum ){ |
| 4618 | int a; |
| 4619 | aFrom[nCell] = i; |
| 4620 | for(a=0; a<pOld->nOverflow; a++){ |
| 4621 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4622 | aFrom[nCell] = 0xFF; |
| 4623 | break; |
| 4624 | } |
| 4625 | } |
| 4626 | } |
| 4627 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4628 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4629 | } |
| 4630 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4631 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4632 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4633 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4634 | ** are duplicates of keys on the child pages. We need to remove |
| 4635 | ** the divider cells from pParent, but the dividers cells are not |
| 4636 | ** added to apCell[] because they are duplicates of child cells. |
| 4637 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4638 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4639 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4640 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4641 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4642 | szCell[nCell] = sz; |
| 4643 | pTemp = &aSpace[iSpace]; |
| 4644 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4645 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4646 | memcpy(pTemp, apDiv[i], sz); |
| 4647 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4648 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4649 | if( pBt->autoVacuum ){ |
| 4650 | aFrom[nCell] = 0xFF; |
| 4651 | } |
| 4652 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4653 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4654 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4655 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4656 | if( !pOld->leaf ){ |
| 4657 | assert( leafCorrection==0 ); |
| 4658 | /* The right pointer of the child page pOld becomes the left |
| 4659 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4660 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4661 | }else{ |
| 4662 | assert( leafCorrection==4 ); |
| 4663 | } |
| 4664 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4665 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4670 | ** Figure out the number of pages needed to hold all nCell cells. |
| 4671 | ** Store this number in "k". Also compute szNew[] which is the total |
| 4672 | ** 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] | 4673 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4674 | ** cntNew[k] should equal nCell. |
| 4675 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4676 | ** Values computed by this block: |
| 4677 | ** |
| 4678 | ** k: The total number of sibling pages |
| 4679 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 4680 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 4681 | ** the right of the i-th sibling page. |
| 4682 | ** usableSpace: Number of bytes of space available on each sibling. |
| 4683 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4684 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4685 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4686 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4687 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4688 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4689 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4690 | szNew[k] = subtotal - szCell[i]; |
| 4691 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4692 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4693 | subtotal = 0; |
| 4694 | k++; |
| 4695 | } |
| 4696 | } |
| 4697 | szNew[k] = subtotal; |
| 4698 | cntNew[k] = nCell; |
| 4699 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4700 | |
| 4701 | /* |
| 4702 | ** The packing computed by the previous block is biased toward the siblings |
| 4703 | ** on the left side. The left siblings are always nearly full, while the |
| 4704 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4705 | ** to adjust the packing of siblings to get a better balance. |
| 4706 | ** |
| 4707 | ** This adjustment is more than an optimization. The packing above might |
| 4708 | ** be so out of balance as to be illegal. For example, the right-most |
| 4709 | ** sibling might be completely empty. This adjustment is not optional. |
| 4710 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4711 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4712 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4713 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4714 | int r; /* Index of right-most cell in left sibling */ |
| 4715 | int d; /* Index of first cell to the left of right sibling */ |
| 4716 | |
| 4717 | r = cntNew[i-1] - 1; |
| 4718 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4719 | assert( d<nMaxCells ); |
| 4720 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4721 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4722 | szRight += szCell[d] + 2; |
| 4723 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4724 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4725 | r = cntNew[i-1] - 1; |
| 4726 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4727 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4728 | szNew[i] = szRight; |
| 4729 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4730 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4731 | |
| 4732 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 4733 | ** a virtual root page. A virtual root page is when the real root |
| 4734 | ** page is page 1 and we are the only child of that page. |
| 4735 | */ |
| 4736 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4737 | |
| 4738 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4739 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4740 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4741 | assert( pPage->pgno>1 ); |
| 4742 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4743 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4744 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4745 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4746 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4747 | pgnoNew[i] = pgnoOld[i]; |
| 4748 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4749 | rc = sqlite3PagerWrite(pNew->pDbPage); |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4750 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4751 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4752 | assert( i>0 ); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4753 | rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4754 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4755 | apNew[i] = pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4756 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4757 | nNew++; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4758 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4761 | /* Free any old pages that were not reused as new pages. |
| 4762 | */ |
| 4763 | while( i<nOld ){ |
| 4764 | rc = freePage(apOld[i]); |
| 4765 | if( rc ) goto balance_cleanup; |
| 4766 | releasePage(apOld[i]); |
| 4767 | apOld[i] = 0; |
| 4768 | i++; |
| 4769 | } |
| 4770 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4771 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4772 | ** Put the new pages in accending order. This helps to |
| 4773 | ** keep entries in the disk file in order so that a scan |
| 4774 | ** of the table is a linear scan through the file. That |
| 4775 | ** in turn helps the operating system to deliver pages |
| 4776 | ** from the disk more rapidly. |
| 4777 | ** |
| 4778 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4779 | ** n is never more than NB (a small constant), that should |
| 4780 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4781 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4782 | ** When NB==3, this one optimization makes the database |
| 4783 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4784 | */ |
| 4785 | for(i=0; i<k-1; i++){ |
| 4786 | int minV = pgnoNew[i]; |
| 4787 | int minI = i; |
| 4788 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 4789 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4790 | minI = j; |
| 4791 | minV = pgnoNew[j]; |
| 4792 | } |
| 4793 | } |
| 4794 | if( minI>i ){ |
| 4795 | int t; |
| 4796 | MemPage *pT; |
| 4797 | t = pgnoNew[i]; |
| 4798 | pT = apNew[i]; |
| 4799 | pgnoNew[i] = pgnoNew[minI]; |
| 4800 | apNew[i] = apNew[minI]; |
| 4801 | pgnoNew[minI] = t; |
| 4802 | apNew[minI] = pT; |
| 4803 | } |
| 4804 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4805 | 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] | 4806 | pgnoOld[0], |
| 4807 | nOld>=2 ? pgnoOld[1] : 0, |
| 4808 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 4809 | pgnoNew[0], szNew[0], |
| 4810 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 4811 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4812 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 4813 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4814 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4815 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4816 | ** Evenly distribute the data in apCell[] across the new pages. |
| 4817 | ** Insert divider cells into pParent as necessary. |
| 4818 | */ |
| 4819 | j = 0; |
| 4820 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4821 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4822 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 4823 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4824 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4825 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4826 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4827 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4828 | |
| 4829 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4830 | /* If this is an auto-vacuum database, update the pointer map entries |
| 4831 | ** that point to the siblings that were rearranged. These can be: left |
| 4832 | ** children of cells, the right-child of the page, or overflow pages |
| 4833 | ** pointed to by cells. |
| 4834 | */ |
| 4835 | if( pBt->autoVacuum ){ |
| 4836 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4837 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4838 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4839 | rc = ptrmapPutOvfl(pNew, k-j); |
| 4840 | if( rc!=SQLITE_OK ){ |
| 4841 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | } |
| 4845 | } |
| 4846 | #endif |
| 4847 | |
| 4848 | j = cntNew[i]; |
| 4849 | |
| 4850 | /* If the sibling page assembled above was not the right-most sibling, |
| 4851 | ** insert a divider cell into the parent page. |
| 4852 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4853 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4854 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4855 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4856 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4857 | |
| 4858 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4859 | pCell = apCell[j]; |
| 4860 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4861 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4862 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4863 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4864 | }else if( leafData ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4865 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 4866 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 4867 | ** cell consists of the integer key for the right-most cell of |
| 4868 | ** the sibling-page assembled above only. |
| 4869 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4870 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4871 | j--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4872 | parseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4873 | pCell = &aSpace[iSpace]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4874 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4875 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4876 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4877 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4878 | }else{ |
| 4879 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4880 | pTemp = &aSpace[iSpace]; |
| 4881 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4882 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4883 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4884 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4885 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4886 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4887 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4888 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 4889 | ** then update the pointer map with an entry for the overflow page |
| 4890 | ** that the cell just inserted points to (if any). |
| 4891 | */ |
| 4892 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4893 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 4894 | if( rc!=SQLITE_OK ){ |
| 4895 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4899 | j++; |
| 4900 | nxDiv++; |
| 4901 | } |
| 4902 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4903 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4904 | assert( nOld>0 ); |
| 4905 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4906 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4907 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4908 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4909 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4910 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4911 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4912 | }else{ |
| 4913 | /* Right-most sibling is the left child of the first entry in pParent |
| 4914 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4915 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | /* |
| 4919 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4920 | */ |
| 4921 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4922 | rc = reparentChildPages(apNew[i]); |
| 4923 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4924 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4925 | rc = reparentChildPages(pParent); |
| 4926 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4927 | |
| 4928 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4929 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4930 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4931 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4932 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4933 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4934 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4935 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4936 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4937 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4938 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4939 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4940 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4941 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4942 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4943 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4944 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4945 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4946 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4947 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4948 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4949 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4950 | return rc; |
| 4951 | } |
| 4952 | |
| 4953 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4954 | ** This routine is called for the root page of a btree when the root |
| 4955 | ** page contains no cells. This is an opportunity to make the tree |
| 4956 | ** shallower by one level. |
| 4957 | */ |
| 4958 | static int balance_shallower(MemPage *pPage){ |
| 4959 | MemPage *pChild; /* The only child page of pPage */ |
| 4960 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4961 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4962 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4963 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4964 | u8 **apCell; /* All cells from pages being balanced */ |
| 4965 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4966 | |
| 4967 | assert( pPage->pParent==0 ); |
| 4968 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4969 | pBt = pPage->pBt; |
| 4970 | mxCellPerPage = MX_CELL(pBt); |
| 4971 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4972 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4973 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4974 | if( pPage->leaf ){ |
| 4975 | /* The table is completely empty */ |
| 4976 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4977 | }else{ |
| 4978 | /* The root page is empty but has one child. Transfer the |
| 4979 | ** information from that one child into the root page if it |
| 4980 | ** will fit. This reduces the depth of the tree by one. |
| 4981 | ** |
| 4982 | ** If the root page is page 1, it has less space available than |
| 4983 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4984 | ** of the database fle), so it might not be able to hold all of the |
| 4985 | ** information currently contained in the child. If this is the |
| 4986 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4987 | ** for the right-pointer to the child page. The child page becomes |
| 4988 | ** the virtual root of the tree. |
| 4989 | */ |
| 4990 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4991 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 4992 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 4993 | rc = getPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4994 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4995 | if( pPage->pgno==1 ){ |
| 4996 | rc = initPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4997 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4998 | assert( pChild->nOverflow==0 ); |
| 4999 | if( pChild->nFree>=100 ){ |
| 5000 | /* The child information will fit on the root page, so do the |
| 5001 | ** copy */ |
| 5002 | int i; |
| 5003 | zeroPage(pPage, pChild->aData[0]); |
| 5004 | for(i=0; i<pChild->nCell; i++){ |
| 5005 | apCell[i] = findCell(pChild,i); |
| 5006 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5007 | } |
| 5008 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5009 | /* Copy the right-pointer of the child to the parent. */ |
| 5010 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5011 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5012 | freePage(pChild); |
| 5013 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5014 | }else{ |
| 5015 | /* The child has more information that will fit on the root. |
| 5016 | ** The tree is already balanced. Do nothing. */ |
| 5017 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5018 | } |
| 5019 | }else{ |
| 5020 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5021 | pPage->isInit = 0; |
| 5022 | pPage->pParent = 0; |
| 5023 | rc = initPage(pPage, 0); |
| 5024 | assert( rc==SQLITE_OK ); |
| 5025 | freePage(pChild); |
| 5026 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5027 | pChild->pgno, pPage->pgno)); |
| 5028 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5029 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5030 | assert( pPage->nOverflow==0 ); |
| 5031 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5032 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5033 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5034 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5035 | rc = ptrmapPutOvfl(pPage, i); |
| 5036 | if( rc!=SQLITE_OK ){ |
| 5037 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5042 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5043 | releasePage(pChild); |
| 5044 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5045 | end_shallow_balance: |
| 5046 | sqliteFree(apCell); |
| 5047 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | |
| 5051 | /* |
| 5052 | ** The root page is overfull |
| 5053 | ** |
| 5054 | ** When this happens, Create a new child page and copy the |
| 5055 | ** contents of the root into the child. Then make the root |
| 5056 | ** page an empty page with rightChild pointing to the new |
| 5057 | ** child. Finally, call balance_internal() on the new child |
| 5058 | ** to cause it to split. |
| 5059 | */ |
| 5060 | static int balance_deeper(MemPage *pPage){ |
| 5061 | int rc; /* Return value from subprocedures */ |
| 5062 | MemPage *pChild; /* Pointer to a new child page */ |
| 5063 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5064 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5065 | int usableSize; /* Total usable size of a page */ |
| 5066 | u8 *data; /* Content of the parent page */ |
| 5067 | u8 *cdata; /* Content of the child page */ |
| 5068 | int hdr; /* Offset to page header in parent */ |
| 5069 | int brk; /* Offset to content of first cell in parent */ |
| 5070 | |
| 5071 | assert( pPage->pParent==0 ); |
| 5072 | assert( pPage->nOverflow>0 ); |
| 5073 | pBt = pPage->pBt; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5074 | rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5075 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5076 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5077 | usableSize = pBt->usableSize; |
| 5078 | data = pPage->aData; |
| 5079 | hdr = pPage->hdrOffset; |
| 5080 | brk = get2byte(&data[hdr+5]); |
| 5081 | cdata = pChild->aData; |
| 5082 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5083 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5084 | assert( pChild->isInit==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5085 | rc = initPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5086 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5087 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5088 | pChild->nOverflow = pPage->nOverflow; |
| 5089 | if( pChild->nOverflow ){ |
| 5090 | pChild->nFree = 0; |
| 5091 | } |
| 5092 | assert( pChild->nCell==pPage->nCell ); |
| 5093 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5094 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5095 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5096 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5097 | if( pBt->autoVacuum ){ |
| 5098 | int i; |
| 5099 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5100 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5101 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5102 | rc = ptrmapPutOvfl(pChild, i); |
| 5103 | if( rc!=SQLITE_OK ){ |
| 5104 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } |
| 5107 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5108 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5109 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5110 | |
| 5111 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5112 | releasePage(pChild); |
| 5113 | return rc; |
| 5114 | } |
| 5115 | |
| 5116 | /* |
| 5117 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5118 | ** required, call the appropriate balancing routine. |
| 5119 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5120 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5121 | int rc = SQLITE_OK; |
| 5122 | if( pPage->pParent==0 ){ |
| 5123 | if( pPage->nOverflow>0 ){ |
| 5124 | rc = balance_deeper(pPage); |
| 5125 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5126 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5127 | rc = balance_shallower(pPage); |
| 5128 | } |
| 5129 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5130 | if( pPage->nOverflow>0 || |
| 5131 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5132 | rc = balance_nonroot(pPage); |
| 5133 | } |
| 5134 | } |
| 5135 | return rc; |
| 5136 | } |
| 5137 | |
| 5138 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5139 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5140 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5141 | ** database connection (a database connection that shares the pager |
| 5142 | ** cache with the current connection) and that other connection |
| 5143 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5144 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5145 | ** |
| 5146 | ** In addition to checking for read-locks (where a read-lock |
| 5147 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5148 | ** all cursors write cursors so that they are pointing to the |
| 5149 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5150 | ** or delete might change the number of cells on a page or delete |
| 5151 | ** a page entirely and we do not want to leave any cursors |
| 5152 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5153 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5154 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5155 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5156 | BtShared *pBt = pBtree->pBt; |
| 5157 | sqlite3 *db = pBtree->pSqlite; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5158 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5159 | if( p==pExclude ) continue; |
| 5160 | if( p->eState!=CURSOR_VALID ) continue; |
| 5161 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5162 | if( p->wrFlag==0 ){ |
| 5163 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5164 | if( dbOther==0 || |
| 5165 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5166 | return SQLITE_LOCKED; |
| 5167 | } |
| 5168 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5169 | moveToRoot(p); |
| 5170 | } |
| 5171 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5172 | return SQLITE_OK; |
| 5173 | } |
| 5174 | |
| 5175 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5176 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5177 | ** 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] | 5178 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5179 | ** is left pointing at a random location. |
| 5180 | ** |
| 5181 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5182 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5183 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5184 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5185 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5186 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5187 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5188 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5189 | int rc; |
| 5190 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5191 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5192 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5193 | BtShared *pBt = pCur->pBtree->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5194 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5195 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5196 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5197 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5198 | /* Must start a transaction before doing an insert */ |
| 5199 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5200 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5201 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5202 | if( !pCur->wrFlag ){ |
| 5203 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5204 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5205 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5206 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5207 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5208 | |
| 5209 | /* Save the positions of any other cursors open on this table */ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5210 | restoreOrClearCursorPosition(pCur, 0); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5211 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5212 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
| 5213 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc)) |
| 5214 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5215 | return rc; |
| 5216 | } |
| 5217 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5218 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5219 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5220 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5221 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5222 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5223 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5224 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5225 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5226 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5227 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5228 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5229 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5230 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5231 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5232 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5233 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5234 | int szOld; |
| 5235 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5236 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5237 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5238 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5239 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5240 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5241 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5242 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5243 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5244 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5245 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5246 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5247 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5248 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5249 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5250 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5251 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5252 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5253 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5254 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5255 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5256 | if( rc==SQLITE_OK ){ |
| 5257 | moveToRoot(pCur); |
| 5258 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5259 | end_insert: |
| 5260 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5261 | return rc; |
| 5262 | } |
| 5263 | |
| 5264 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5265 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5266 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5267 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5268 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5269 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5270 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5271 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5272 | Pgno pgnoChild = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5273 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5274 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5275 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5276 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5277 | /* Must start a transaction before doing a delete */ |
| 5278 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5279 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5280 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5281 | if( pCur->idx >= pPage->nCell ){ |
| 5282 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5283 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5284 | if( !pCur->wrFlag ){ |
| 5285 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5286 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5287 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5288 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5289 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5290 | |
| 5291 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5292 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5293 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5294 | ** that the entry will be deleted from. |
| 5295 | */ |
| 5296 | if( |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5297 | (rc = restoreOrClearCursorPosition(pCur, 1))!=0 || |
| 5298 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5299 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5300 | ){ |
| 5301 | return rc; |
| 5302 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5303 | |
| 5304 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5305 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5306 | ** cell. The cell itself is still intact. |
| 5307 | */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5308 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5309 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5310 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5311 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5312 | rc = clearCell(pPage, pCell); |
| 5313 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5314 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5315 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5316 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5317 | ** 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] | 5318 | ** do something we will leave a hole on an internal page. |
| 5319 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5320 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5321 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5322 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5323 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5324 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5325 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5326 | ** initialized before use. Adding an extra initialization |
| 5327 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5328 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5329 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5330 | assert( !pPage->leafData ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5331 | getTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5332 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5333 | if( rc!=SQLITE_OK ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5334 | if( rc!=SQLITE_NOMEM ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5335 | rc = SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5336 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5337 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5338 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5339 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5340 | } |
| 5341 | if( rc==SQLITE_OK ){ |
| 5342 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5343 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5344 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
| 5345 | pNext = findCell(leafCur.pPage, leafCur.idx); |
| 5346 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5347 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 5348 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5349 | if( tempCell==0 ){ |
| 5350 | rc = SQLITE_NOMEM; |
| 5351 | } |
| 5352 | } |
| 5353 | if( rc==SQLITE_OK ){ |
| 5354 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5355 | } |
| 5356 | if( rc==SQLITE_OK ){ |
| 5357 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5358 | rc = balance(pPage, 0); |
| 5359 | } |
| 5360 | if( rc==SQLITE_OK ){ |
| 5361 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5362 | rc = balance(leafCur.pPage, 0); |
| 5363 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5364 | sqliteFree(tempCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5365 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5366 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5367 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5368 | pCur->pgnoRoot, pPage->pgno)); |
| 5369 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5370 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5371 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5372 | if( rc==SQLITE_OK ){ |
| 5373 | moveToRoot(pCur); |
| 5374 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5375 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5376 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5377 | |
| 5378 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5379 | ** Create a new BTree table. Write into *piTable the page |
| 5380 | ** number for the root page of the new table. |
| 5381 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5382 | ** The type of type is determined by the flags parameter. Only the |
| 5383 | ** following values of flags are currently in use. Other values for |
| 5384 | ** flags might not work: |
| 5385 | ** |
| 5386 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5387 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5388 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5389 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5390 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5391 | MemPage *pRoot; |
| 5392 | Pgno pgnoRoot; |
| 5393 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5394 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5395 | /* Must start a transaction first */ |
| 5396 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5397 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5398 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5399 | |
| 5400 | /* It is illegal to create a table if any cursors are open on the |
| 5401 | ** database. This is because in auto-vacuum mode the backend may |
| 5402 | ** need to move a database page to make room for the new root-page. |
| 5403 | ** If an open cursor was using the page a problem would occur. |
| 5404 | */ |
| 5405 | if( pBt->pCursor ){ |
| 5406 | return SQLITE_LOCKED; |
| 5407 | } |
| 5408 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5409 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5410 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5411 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5412 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5413 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5414 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5415 | MemPage *pPageMove; /* The page to move to. */ |
| 5416 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5417 | /* Read the value of meta[3] from the database to determine where the |
| 5418 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5419 | ** created so far, so the new root-page is (meta[3]+1). |
| 5420 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5421 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5422 | if( rc!=SQLITE_OK ) return rc; |
| 5423 | pgnoRoot++; |
| 5424 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5425 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5426 | ** PENDING_BYTE page. |
| 5427 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5428 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5429 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5430 | pgnoRoot++; |
| 5431 | } |
| 5432 | assert( pgnoRoot>=3 ); |
| 5433 | |
| 5434 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5435 | ** be moved to the allocated page (unless the allocated page happens |
| 5436 | ** to reside at pgnoRoot). |
| 5437 | */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5438 | rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5439 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5440 | return rc; |
| 5441 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5442 | |
| 5443 | if( pgnoMove!=pgnoRoot ){ |
| 5444 | u8 eType; |
| 5445 | Pgno iPtrPage; |
| 5446 | |
| 5447 | releasePage(pPageMove); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5448 | rc = getPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5449 | if( rc!=SQLITE_OK ){ |
| 5450 | return rc; |
| 5451 | } |
| 5452 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5453 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5454 | releasePage(pRoot); |
| 5455 | return rc; |
| 5456 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5457 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5458 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5459 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5460 | if( rc!=SQLITE_OK ){ |
| 5461 | releasePage(pRoot); |
| 5462 | return rc; |
| 5463 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5464 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5465 | releasePage(pRoot); |
| 5466 | if( rc!=SQLITE_OK ){ |
| 5467 | return rc; |
| 5468 | } |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5469 | rc = getPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5470 | if( rc!=SQLITE_OK ){ |
| 5471 | return rc; |
| 5472 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5473 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5474 | if( rc!=SQLITE_OK ){ |
| 5475 | releasePage(pRoot); |
| 5476 | return rc; |
| 5477 | } |
| 5478 | }else{ |
| 5479 | pRoot = pPageMove; |
| 5480 | } |
| 5481 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5482 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5483 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5484 | if( rc ){ |
| 5485 | releasePage(pRoot); |
| 5486 | return rc; |
| 5487 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5488 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5489 | if( rc ){ |
| 5490 | releasePage(pRoot); |
| 5491 | return rc; |
| 5492 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5493 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5494 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 5495 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5496 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5497 | } |
| 5498 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5499 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5500 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5501 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5502 | *piTable = (int)pgnoRoot; |
| 5503 | return SQLITE_OK; |
| 5504 | } |
| 5505 | |
| 5506 | /* |
| 5507 | ** Erase the given database page and all its children. Return |
| 5508 | ** the page to the freelist. |
| 5509 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5510 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5511 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5512 | Pgno pgno, /* Page number to clear */ |
| 5513 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5514 | int freePageFlag /* Deallocate page if true */ |
| 5515 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5516 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5517 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5518 | unsigned char *pCell; |
| 5519 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5520 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5521 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5522 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5525 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5526 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5527 | for(i=0; i<pPage->nCell; i++){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5528 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5529 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5530 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5531 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5532 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5533 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5534 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5535 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5536 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5537 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5538 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5539 | } |
| 5540 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5541 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5542 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5543 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5544 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5545 | |
| 5546 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5547 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5548 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5552 | ** Delete all information from a single table in the database. iTable is |
| 5553 | ** the page number of the root of the table. After this routine returns, |
| 5554 | ** the root page is empty, but still exists. |
| 5555 | ** |
| 5556 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5557 | ** read cursors on the table. Open write cursors are moved to the |
| 5558 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5559 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5560 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5561 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5562 | BtShared *pBt = p->pBt; |
| 5563 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5564 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5565 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5566 | rc = checkReadLocks(p, iTable, 0); |
| 5567 | if( rc ){ |
| 5568 | return rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5569 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5570 | |
| 5571 | /* Save the position of all cursors open on this table */ |
| 5572 | if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5573 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5574 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5575 | |
| 5576 | return clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | /* |
| 5580 | ** Erase all information in a table and add the root of the table to |
| 5581 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5582 | ** page 1) is never added to the freelist. |
| 5583 | ** |
| 5584 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5585 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5586 | ** |
| 5587 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5588 | ** root page in the database file, then the last root page |
| 5589 | ** in the database file is moved into the slot formerly occupied by |
| 5590 | ** iTable and that last slot formerly occupied by the last root page |
| 5591 | ** is added to the freelist instead of iTable. In this say, all |
| 5592 | ** root pages are kept at the beginning of the database file, which |
| 5593 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5594 | ** page number that used to be the last root page in the file before |
| 5595 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5596 | ** The last root page is recorded in meta[3] and the value of |
| 5597 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5598 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5599 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5600 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5601 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5602 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5603 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5604 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5605 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5606 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5607 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5608 | /* It is illegal to drop a table if any cursors are open on the |
| 5609 | ** database. This is because in auto-vacuum mode the backend may |
| 5610 | ** need to move another root-page to fill a gap left by the deleted |
| 5611 | ** root page. If an open cursor was using this page a problem would |
| 5612 | ** occur. |
| 5613 | */ |
| 5614 | if( pBt->pCursor ){ |
| 5615 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5616 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5617 | |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5618 | rc = getPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5619 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5620 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5621 | if( rc ){ |
| 5622 | releasePage(pPage); |
| 5623 | return rc; |
| 5624 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5625 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5626 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5627 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5628 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5629 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5630 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5631 | releasePage(pPage); |
| 5632 | #else |
| 5633 | if( pBt->autoVacuum ){ |
| 5634 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5635 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5636 | if( rc!=SQLITE_OK ){ |
| 5637 | releasePage(pPage); |
| 5638 | return rc; |
| 5639 | } |
| 5640 | |
| 5641 | if( iTable==maxRootPgno ){ |
| 5642 | /* If the table being dropped is the table with the largest root-page |
| 5643 | ** number in the database, put the root page on the free list. |
| 5644 | */ |
| 5645 | rc = freePage(pPage); |
| 5646 | releasePage(pPage); |
| 5647 | if( rc!=SQLITE_OK ){ |
| 5648 | return rc; |
| 5649 | } |
| 5650 | }else{ |
| 5651 | /* The table being dropped does not have the largest root-page |
| 5652 | ** number in the database. So move the page that does into the |
| 5653 | ** gap left by the deleted root-page. |
| 5654 | */ |
| 5655 | MemPage *pMove; |
| 5656 | releasePage(pPage); |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5657 | rc = getPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5658 | if( rc!=SQLITE_OK ){ |
| 5659 | return rc; |
| 5660 | } |
| 5661 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 5662 | releasePage(pMove); |
| 5663 | if( rc!=SQLITE_OK ){ |
| 5664 | return rc; |
| 5665 | } |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5666 | rc = getPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5667 | if( rc!=SQLITE_OK ){ |
| 5668 | return rc; |
| 5669 | } |
| 5670 | rc = freePage(pMove); |
| 5671 | releasePage(pMove); |
| 5672 | if( rc!=SQLITE_OK ){ |
| 5673 | return rc; |
| 5674 | } |
| 5675 | *piMoved = maxRootPgno; |
| 5676 | } |
| 5677 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5678 | /* Set the new 'max-root-page' value in the database header. This |
| 5679 | ** is the old value less one, less one more if that happens to |
| 5680 | ** be a root-page number, less one again if that is the |
| 5681 | ** PENDING_BYTE_PAGE. |
| 5682 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5683 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5684 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 5685 | maxRootPgno--; |
| 5686 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5687 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5688 | maxRootPgno--; |
| 5689 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5690 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 5691 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5692 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5693 | }else{ |
| 5694 | rc = freePage(pPage); |
| 5695 | releasePage(pPage); |
| 5696 | } |
| 5697 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5698 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5699 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5700 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5701 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5702 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5703 | return rc; |
| 5704 | } |
| 5705 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5706 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5707 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5708 | ** Read the meta-information out of a database file. Meta[0] |
| 5709 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5710 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 5711 | ** is read-only, the others are read/write. |
| 5712 | ** |
| 5713 | ** The schema layer numbers meta values differently. At the schema |
| 5714 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 5715 | ** 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] | 5716 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5717 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5718 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5719 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5720 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5721 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5722 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5723 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 5724 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 5725 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 5726 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 5727 | */ |
| 5728 | rc = queryTableLock(p, 1, READ_LOCK); |
| 5729 | if( rc!=SQLITE_OK ){ |
| 5730 | return rc; |
| 5731 | } |
| 5732 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5733 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5734 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5735 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5736 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5737 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5738 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5739 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5740 | /* If autovacuumed is disabled in this build but we are trying to |
| 5741 | ** access an autovacuumed database, then make the database readonly. |
| 5742 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5743 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5744 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5745 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5746 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5747 | /* Grab the read-lock on page 1. */ |
| 5748 | rc = lockTable(p, 1, READ_LOCK); |
| 5749 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5753 | ** Write meta-information back into the database. Meta[0] is |
| 5754 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5755 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5756 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 5757 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5758 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5759 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5760 | assert( idx>=1 && idx<=15 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5761 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5762 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5763 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5764 | assert( pBt->pPage1!=0 ); |
| 5765 | pP1 = pBt->pPage1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5766 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5767 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5768 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5769 | return SQLITE_OK; |
| 5770 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5771 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5772 | /* |
| 5773 | ** Return the flag byte at the beginning of the page that the cursor |
| 5774 | ** is currently pointing to. |
| 5775 | */ |
| 5776 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5777 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5778 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5779 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5780 | MemPage *pPage = pCur->pPage; |
| 5781 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 5782 | } |
| 5783 | |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 5784 | #ifdef SQLITE_DEBUG |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5785 | /* |
| 5786 | ** Print a disassembly of the given page on standard output. This routine |
| 5787 | ** is used for debugging and testing only. |
| 5788 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5789 | static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5790 | int rc; |
| 5791 | MemPage *pPage; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5792 | int i, j, c; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5793 | int nFree; |
| 5794 | u16 idx; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5795 | int hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5796 | int nCell; |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5797 | int isInit; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5798 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5799 | char range[20]; |
| 5800 | unsigned char payload[20]; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5801 | |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 5802 | rc = getPage(pBt, (Pgno)pgno, &pPage, 0); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5803 | isInit = pPage->isInit; |
| 5804 | if( pPage->isInit==0 ){ |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5805 | initPage(pPage, pParent); |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5806 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5807 | if( rc ){ |
| 5808 | return rc; |
| 5809 | } |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5810 | hdr = pPage->hdrOffset; |
| 5811 | data = pPage->aData; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5812 | c = data[hdr]; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5813 | pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5814 | pPage->zeroData = (c & PTF_ZERODATA)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5815 | pPage->leafData = (c & PTF_LEAFDATA)!=0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 5816 | pPage->leaf = (c & PTF_LEAF)!=0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5817 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5818 | nCell = get2byte(&data[hdr+3]); |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5819 | sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno, |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5820 | data[hdr], data[hdr+7], |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5821 | (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5822 | assert( hdr == (pgno==1 ? 100 : 0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5823 | idx = hdr + 12 - pPage->leaf*4; |
| 5824 | for(i=0; i<nCell; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5825 | CellInfo info; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5826 | Pgno child; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5827 | unsigned char *pCell; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5828 | int sz; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5829 | int addr; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5830 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5831 | addr = get2byte(&data[idx + 2*i]); |
| 5832 | pCell = &data[addr]; |
| 5833 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5834 | sz = info.nSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5835 | sprintf(range,"%d..%d", addr, addr+sz-1); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5836 | if( pPage->leaf ){ |
| 5837 | child = 0; |
| 5838 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5839 | child = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5840 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5841 | sz = info.nData; |
| 5842 | if( !pPage->intKey ) sz += info.nKey; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5843 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5844 | memcpy(payload, &pCell[info.nHeader], sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5845 | for(j=0; j<sz; j++){ |
| 5846 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 5847 | } |
| 5848 | payload[sz] = 0; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5849 | sqlite3DebugPrintf( |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5850 | "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n", |
| 5851 | i, range, child, info.nKey, info.nData, payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5852 | ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5853 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5854 | if( !pPage->leaf ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5855 | sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5856 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5857 | nFree = 0; |
| 5858 | i = 0; |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5859 | idx = get2byte(&data[hdr+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5860 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5861 | int sz = get2byte(&data[idx+2]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5862 | sprintf(range,"%d..%d", idx, idx+sz-1); |
| 5863 | nFree += sz; |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5864 | sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5865 | i, range, sz, nFree); |
drh | ab9f7f1 | 2004-05-08 10:56:11 +0000 | [diff] [blame] | 5866 | idx = get2byte(&data[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5867 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5868 | } |
| 5869 | if( idx!=0 ){ |
drh | fe63d1c | 2004-09-08 20:13:04 +0000 | [diff] [blame] | 5870 | sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5871 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5872 | if( recursive && !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5873 | for(i=0; i<nCell; i++){ |
| 5874 | unsigned char *pCell = findCell(pPage, i); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5875 | btreePageDump(pBt, get4byte(pCell), 1, pPage); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5876 | idx = get2byte(pCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5877 | } |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5878 | btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5879 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5880 | pPage->isInit = isInit; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5881 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3644f08 | 2004-05-10 18:45:09 +0000 | [diff] [blame] | 5882 | fflush(stdout); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5883 | return SQLITE_OK; |
| 5884 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5885 | int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){ |
| 5886 | return btreePageDump(p->pBt, pgno, recursive, 0); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5887 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5888 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5889 | |
drh | 77bba59 | 2006-08-13 18:39:26 +0000 | [diff] [blame] | 5890 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5891 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5892 | ** Fill aResult[] with information about the entry and page that the |
| 5893 | ** cursor is pointing to. |
| 5894 | ** |
| 5895 | ** aResult[0] = The page number |
| 5896 | ** aResult[1] = The entry number |
| 5897 | ** aResult[2] = Total number of entries on this page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5898 | ** aResult[3] = Cell size (local payload + header) |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5899 | ** aResult[4] = Number of free bytes on this page |
| 5900 | ** aResult[5] = Number of free blocks on the page |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5901 | ** aResult[6] = Total payload size (local + overflow) |
| 5902 | ** aResult[7] = Header size in bytes |
| 5903 | ** aResult[8] = Local payload size |
| 5904 | ** aResult[9] = Parent page number |
drh | 50c6706 | 2007-02-10 19:22:35 +0000 | [diff] [blame] | 5905 | ** aResult[10]= Page number of the first overflow page |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5906 | ** |
| 5907 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5908 | */ |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5909 | int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5910 | int cnt, idx; |
| 5911 | MemPage *pPage = pCur->pPage; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5912 | BtCursor tmpCur; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5913 | |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5914 | int rc = restoreOrClearCursorPosition(pCur, 1); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5915 | if( rc!=SQLITE_OK ){ |
| 5916 | return rc; |
| 5917 | } |
| 5918 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5919 | assert( pPage->isInit ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5920 | getTempCursor(pCur, &tmpCur); |
| 5921 | while( upCnt-- ){ |
| 5922 | moveToParent(&tmpCur); |
| 5923 | } |
| 5924 | pPage = tmpCur.pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 5925 | aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5926 | assert( aResult[0]==pPage->pgno ); |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5927 | aResult[1] = tmpCur.idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5928 | aResult[2] = pPage->nCell; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5929 | if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){ |
| 5930 | getCellInfo(&tmpCur); |
| 5931 | aResult[3] = tmpCur.info.nSize; |
| 5932 | aResult[6] = tmpCur.info.nData; |
| 5933 | aResult[7] = tmpCur.info.nHeader; |
| 5934 | aResult[8] = tmpCur.info.nLocal; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5935 | }else{ |
| 5936 | aResult[3] = 0; |
| 5937 | aResult[6] = 0; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5938 | aResult[7] = 0; |
| 5939 | aResult[8] = 0; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5940 | } |
| 5941 | aResult[4] = pPage->nFree; |
| 5942 | cnt = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5943 | idx = get2byte(&pPage->aData[pPage->hdrOffset+1]); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5944 | while( idx>0 && idx<pPage->pBt->usableSize ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5945 | cnt++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5946 | idx = get2byte(&pPage->aData[idx]); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5947 | } |
| 5948 | aResult[5] = cnt; |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5949 | if( pPage->pParent==0 || isRootPage(pPage) ){ |
| 5950 | aResult[9] = 0; |
| 5951 | }else{ |
| 5952 | aResult[9] = pPage->pParent->pgno; |
| 5953 | } |
drh | 50c6706 | 2007-02-10 19:22:35 +0000 | [diff] [blame] | 5954 | if( tmpCur.info.iOverflow ){ |
| 5955 | aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]); |
| 5956 | }else{ |
| 5957 | aResult[10] = 0; |
| 5958 | } |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 5959 | releaseTempCursor(&tmpCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5960 | return SQLITE_OK; |
| 5961 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5962 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5963 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5964 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5965 | ** Return the pager associated with a BTree. This routine is used for |
| 5966 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5967 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5968 | Pager *sqlite3BtreePager(Btree *p){ |
| 5969 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5970 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5971 | |
| 5972 | /* |
| 5973 | ** This structure is passed around through all the sanity checking routines |
| 5974 | ** in order to keep track of some global state information. |
| 5975 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5976 | typedef struct IntegrityCk IntegrityCk; |
| 5977 | struct IntegrityCk { |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5978 | BtShared *pBt; /* The tree being checked out */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5979 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 5980 | int nPage; /* Number of pages in the database */ |
| 5981 | int *anRef; /* Number of times each page is referenced */ |
| 5982 | int mxErr; /* Stop accumulating errors when this reaches zero */ |
| 5983 | char *zErrMsg; /* An error message. NULL if no errors seen. */ |
| 5984 | int nErr; /* Number of messages written to zErrMsg so far */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5985 | }; |
| 5986 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5987 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5988 | /* |
| 5989 | ** Append a message to the error message string. |
| 5990 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5991 | static void checkAppendMsg( |
| 5992 | IntegrityCk *pCheck, |
| 5993 | char *zMsg1, |
| 5994 | const char *zFormat, |
| 5995 | ... |
| 5996 | ){ |
| 5997 | va_list ap; |
| 5998 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5999 | if( !pCheck->mxErr ) return; |
| 6000 | pCheck->mxErr--; |
| 6001 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6002 | va_start(ap, zFormat); |
| 6003 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 6004 | va_end(ap); |
| 6005 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6006 | if( pCheck->zErrMsg ){ |
| 6007 | char *zOld = pCheck->zErrMsg; |
| 6008 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6009 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6010 | sqliteFree(zOld); |
| 6011 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6012 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6013 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6014 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6015 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6016 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6017 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6018 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6019 | /* |
| 6020 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6021 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6022 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6023 | ** if this is the first reference to the page. |
| 6024 | ** |
| 6025 | ** Also check that the page number is in bounds. |
| 6026 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6027 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6028 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6029 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6030 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6031 | return 1; |
| 6032 | } |
| 6033 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6034 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6035 | return 1; |
| 6036 | } |
| 6037 | return (pCheck->anRef[iPage]++)>1; |
| 6038 | } |
| 6039 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6040 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6041 | /* |
| 6042 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6043 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6044 | ** to pCheck. |
| 6045 | */ |
| 6046 | static void checkPtrmap( |
| 6047 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6048 | Pgno iChild, /* Child page number */ |
| 6049 | u8 eType, /* Expected pointer map type */ |
| 6050 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6051 | char *zContext /* Context description (used for error msg) */ |
| 6052 | ){ |
| 6053 | int rc; |
| 6054 | u8 ePtrmapType; |
| 6055 | Pgno iPtrmapParent; |
| 6056 | |
| 6057 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6058 | if( rc!=SQLITE_OK ){ |
| 6059 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6060 | return; |
| 6061 | } |
| 6062 | |
| 6063 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6064 | checkAppendMsg(pCheck, zContext, |
| 6065 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6066 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6067 | } |
| 6068 | } |
| 6069 | #endif |
| 6070 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6071 | /* |
| 6072 | ** Check the integrity of the freelist or of an overflow page list. |
| 6073 | ** Verify that the number of pages on the list is N. |
| 6074 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6075 | static void checkList( |
| 6076 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6077 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6078 | int iPage, /* Page number for first page in the list */ |
| 6079 | int N, /* Expected number of pages in the list */ |
| 6080 | char *zContext /* Context for error messages */ |
| 6081 | ){ |
| 6082 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6083 | int expected = N; |
| 6084 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6085 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6086 | DbPage *pOvflPage; |
| 6087 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6088 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6089 | checkAppendMsg(pCheck, zContext, |
| 6090 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6091 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6092 | break; |
| 6093 | } |
| 6094 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6095 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6096 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6097 | break; |
| 6098 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6099 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6100 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6101 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6102 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6103 | if( pCheck->pBt->autoVacuum ){ |
| 6104 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6105 | } |
| 6106 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6107 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6108 | checkAppendMsg(pCheck, zContext, |
| 6109 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6110 | N--; |
| 6111 | }else{ |
| 6112 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6113 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6114 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6115 | if( pCheck->pBt->autoVacuum ){ |
| 6116 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6117 | } |
| 6118 | #endif |
| 6119 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6120 | } |
| 6121 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6122 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6123 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6124 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6125 | else{ |
| 6126 | /* If this database supports auto-vacuum and iPage is not the last |
| 6127 | ** page in this overflow list, check that the pointer-map entry for |
| 6128 | ** the following page matches iPage. |
| 6129 | */ |
| 6130 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6131 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6132 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6133 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6134 | } |
| 6135 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6136 | iPage = get4byte(pOvflData); |
| 6137 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6138 | } |
| 6139 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6140 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6141 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6142 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6143 | /* |
| 6144 | ** Do various sanity checks on a single page of a tree. Return |
| 6145 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6146 | ** return 1, and so forth. |
| 6147 | ** |
| 6148 | ** These checks are done: |
| 6149 | ** |
| 6150 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6151 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6152 | ** NO 2. Make sure cell keys are in order. |
| 6153 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6154 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6155 | ** 5. Check the integrity of overflow pages. |
| 6156 | ** 6. Recursively call checkTreePage on all children. |
| 6157 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6158 | ** 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] | 6159 | ** the root of the tree. |
| 6160 | */ |
| 6161 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6162 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6163 | int iPage, /* Page number of the page to check */ |
| 6164 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6165 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6166 | ){ |
| 6167 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6168 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6169 | int hdr, cellStart; |
| 6170 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6171 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6172 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6173 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6174 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6175 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6176 | |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6177 | sprintf(zContext, "Page %d: ", iPage); |
| 6178 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6179 | /* Check that the page exists |
| 6180 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6181 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6182 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6183 | if( iPage==0 ) return 0; |
| 6184 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 0787db6 | 2007-03-04 13:15:27 +0000 | [diff] [blame] | 6185 | if( (rc = getPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6186 | checkAppendMsg(pCheck, zContext, |
| 6187 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6188 | return 0; |
| 6189 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6190 | if( (rc = initPage(pPage, pParent))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6191 | checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6192 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6193 | return 0; |
| 6194 | } |
| 6195 | |
| 6196 | /* Check out all the cells. |
| 6197 | */ |
| 6198 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6199 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6200 | u8 *pCell; |
| 6201 | int sz; |
| 6202 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6203 | |
| 6204 | /* Check payload overflow pages |
| 6205 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6206 | sprintf(zContext, "On tree page %d cell %d: ", iPage, i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6207 | pCell = findCell(pPage,i); |
| 6208 | parseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6209 | sz = info.nData; |
| 6210 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6211 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6212 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6213 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6214 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6215 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6216 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6217 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6218 | } |
| 6219 | #endif |
| 6220 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | /* Check sanity of left child page. |
| 6224 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6225 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6226 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6227 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6228 | if( pBt->autoVacuum ){ |
| 6229 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6230 | } |
| 6231 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6232 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6233 | if( i>0 && d2!=depth ){ |
| 6234 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6235 | } |
| 6236 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6237 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6238 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6239 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6240 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6241 | sprintf(zContext, "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6242 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6243 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6244 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6245 | } |
| 6246 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6247 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6248 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6249 | |
| 6250 | /* Check for complete coverage of the page |
| 6251 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6252 | data = pPage->aData; |
| 6253 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6254 | hit = sqliteMalloc( usableSize ); |
| 6255 | if( hit ){ |
| 6256 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6257 | nCell = get2byte(&data[hdr+3]); |
| 6258 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6259 | for(i=0; i<nCell; i++){ |
| 6260 | int pc = get2byte(&data[cellStart+i*2]); |
| 6261 | int size = cellSizePtr(pPage, &data[pc]); |
| 6262 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6263 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6264 | checkAppendMsg(pCheck, 0, |
| 6265 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6266 | }else{ |
| 6267 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6268 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6269 | } |
| 6270 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6271 | cnt++){ |
| 6272 | int size = get2byte(&data[i+2]); |
| 6273 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6274 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6275 | checkAppendMsg(pCheck, 0, |
| 6276 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6277 | }else{ |
| 6278 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6279 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6280 | i = get2byte(&data[i]); |
| 6281 | } |
| 6282 | for(i=cnt=0; i<usableSize; i++){ |
| 6283 | if( hit[i]==0 ){ |
| 6284 | cnt++; |
| 6285 | }else if( hit[i]>1 ){ |
| 6286 | checkAppendMsg(pCheck, 0, |
| 6287 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6288 | break; |
| 6289 | } |
| 6290 | } |
| 6291 | if( cnt!=data[hdr+7] ){ |
| 6292 | checkAppendMsg(pCheck, 0, |
| 6293 | "Fragmented space is %d byte reported as %d on page %d", |
| 6294 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6295 | } |
| 6296 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6297 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6298 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6299 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6300 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6301 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6302 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6303 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6304 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6305 | /* |
| 6306 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6307 | ** an array of pages numbers were each page number is the root page of |
| 6308 | ** a table. nRoot is the number of entries in aRoot. |
| 6309 | ** |
| 6310 | ** If everything checks out, this routine returns NULL. If something is |
| 6311 | ** amiss, an error message is written into memory obtained from malloc() |
| 6312 | ** and a pointer to that error message is returned. The calling function |
| 6313 | ** is responsible for freeing the error message when it is done. |
| 6314 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6315 | char *sqlite3BtreeIntegrityCheck( |
| 6316 | Btree *p, /* The btree to be checked */ |
| 6317 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6318 | int nRoot, /* Number of entries in aRoot[] */ |
| 6319 | int mxErr, /* Stop reporting errors after this many */ |
| 6320 | int *pnErr /* Write number of errors seen to this variable */ |
| 6321 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6322 | int i; |
| 6323 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6324 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6325 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6326 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6327 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6328 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6329 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 6330 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6331 | sCheck.pBt = pBt; |
| 6332 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6333 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6334 | sCheck.mxErr = mxErr; |
| 6335 | sCheck.nErr = 0; |
| 6336 | *pnErr = 0; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6337 | if( sCheck.nPage==0 ){ |
| 6338 | unlockBtreeIfUnused(pBt); |
| 6339 | return 0; |
| 6340 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 6341 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6342 | if( !sCheck.anRef ){ |
| 6343 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6344 | *pnErr = 1; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6345 | return sqlite3MPrintf("Unable to malloc %d bytes", |
| 6346 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6347 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6348 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6349 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6350 | if( i<=sCheck.nPage ){ |
| 6351 | sCheck.anRef[i] = 1; |
| 6352 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6353 | sCheck.zErrMsg = 0; |
| 6354 | |
| 6355 | /* Check the integrity of the freelist |
| 6356 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6357 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6358 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6359 | |
| 6360 | /* Check all the tables. |
| 6361 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6362 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6363 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6364 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6365 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6366 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6367 | } |
| 6368 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6369 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
| 6372 | /* Make sure every page in the file is referenced |
| 6373 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6374 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6375 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6376 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6377 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6378 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6379 | #else |
| 6380 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6381 | ** references to pointer-map pages. |
| 6382 | */ |
| 6383 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6384 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6385 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6386 | } |
| 6387 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6388 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6389 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6390 | } |
| 6391 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | /* Make sure this analysis did not leave any unref() pages |
| 6395 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6396 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6397 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6398 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6399 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6400 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6401 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | /* Clean up and report errors. |
| 6405 | */ |
| 6406 | sqliteFree(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6407 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6408 | return sCheck.zErrMsg; |
| 6409 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6410 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6411 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6412 | /* |
| 6413 | ** Return the full pathname of the underlying database file. |
| 6414 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6415 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6416 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6417 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6421 | ** Return the pathname of the directory that contains the database file. |
| 6422 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6423 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6424 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6425 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
| 6428 | /* |
| 6429 | ** Return the pathname of the journal file for this database. The return |
| 6430 | ** value of this routine is the same regardless of whether the journal file |
| 6431 | ** has been created or not. |
| 6432 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6433 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6434 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6435 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6438 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6439 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6440 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6441 | ** must be active for both files. |
| 6442 | ** |
| 6443 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6444 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6445 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6446 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6447 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6448 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6449 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6450 | BtShared *pBtTo = pTo->pBt; |
| 6451 | BtShared *pBtFrom = pFrom->pBt; |
| 6452 | |
| 6453 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6454 | return SQLITE_ERROR; |
| 6455 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6456 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6457 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6458 | nPage = sqlite3PagerPagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6459 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6460 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6461 | DbPage *pDbPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6462 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6463 | rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6464 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6465 | rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage)); |
| 6466 | sqlite3PagerUnref(pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6467 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6468 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6469 | DbPage *pDbPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6470 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6471 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6472 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6473 | rc = sqlite3PagerWrite(pDbPage); |
| 6474 | sqlite3PagerUnref(pDbPage); |
| 6475 | sqlite3PagerDontWrite(pBtTo->pPager, i); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6476 | } |
| 6477 | if( !rc && nPage<nToPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6478 | rc = sqlite3PagerTruncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6479 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6480 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6481 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6482 | } |
| 6483 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6484 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6485 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6486 | |
| 6487 | /* |
| 6488 | ** Return non-zero if a transaction is active. |
| 6489 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6490 | int sqlite3BtreeIsInTrans(Btree *p){ |
| 6491 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | /* |
| 6495 | ** Return non-zero if a statement transaction is active. |
| 6496 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6497 | int sqlite3BtreeIsInStmt(Btree *p){ |
| 6498 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6499 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6500 | |
| 6501 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6502 | ** Return non-zero if a read (or write) transaction is active. |
| 6503 | */ |
| 6504 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
| 6505 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6506 | } |
| 6507 | |
| 6508 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6509 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 6510 | ** |
| 6511 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 6512 | ** the name of a master journal file that should be written into the |
| 6513 | ** individual journal file, or is NULL, indicating no master journal file |
| 6514 | ** (single database transaction). |
| 6515 | ** |
| 6516 | ** When this is called, the master journal should already have been |
| 6517 | ** created, populated with this journal pointer and synced to disk. |
| 6518 | ** |
| 6519 | ** Once this is routine has returned, the only thing required to commit |
| 6520 | ** the write-transaction for this database file is to delete the journal. |
| 6521 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6522 | int sqlite3BtreeSync(Btree *p, const char *zMaster){ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6523 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6524 | if( p->inTrans==TRANS_WRITE ){ |
| 6525 | BtShared *pBt = p->pBt; |
danielk1977 | d761c0c | 2004-11-05 16:37:02 +0000 | [diff] [blame] | 6526 | Pgno nTrunc = 0; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6527 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6528 | if( pBt->autoVacuum ){ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6529 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 6530 | if( rc!=SQLITE_OK ){ |
| 6531 | return rc; |
| 6532 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6533 | } |
| 6534 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6535 | rc = sqlite3PagerSync(pBt->pPager, zMaster, nTrunc); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6536 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6537 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6538 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6539 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6540 | /* |
| 6541 | ** This function returns a pointer to a blob of memory associated with |
| 6542 | ** a single shared-btree. The memory is used by client code for it's own |
| 6543 | ** purposes (for example, to store a high-level schema associated with |
| 6544 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6545 | ** |
| 6546 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6547 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6548 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6549 | ** of memory returned. |
| 6550 | ** |
| 6551 | ** Just before the shared-btree is closed, the function passed as the |
| 6552 | ** xFree argument when the memory allocation was made is invoked on the |
| 6553 | ** blob of allocated memory. This function should not call sqliteFree() |
| 6554 | ** on the memory, the btree layer does that. |
| 6555 | */ |
| 6556 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6557 | BtShared *pBt = p->pBt; |
| 6558 | if( !pBt->pSchema ){ |
| 6559 | pBt->pSchema = sqliteMalloc(nBytes); |
| 6560 | pBt->xFreeSchema = xFree; |
| 6561 | } |
| 6562 | return pBt->pSchema; |
| 6563 | } |
| 6564 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6565 | /* |
| 6566 | ** Return true if another user of the same shared btree as the argument |
| 6567 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6568 | */ |
| 6569 | int sqlite3BtreeSchemaLocked(Btree *p){ |
| 6570 | return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6571 | } |
| 6572 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6573 | |
| 6574 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6575 | /* |
| 6576 | ** Obtain a lock on the table whose root page is iTab. The |
| 6577 | ** lock is a write lock if isWritelock is true or a read lock |
| 6578 | ** if it is false. |
| 6579 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6580 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6581 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6582 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6583 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6584 | if( rc==SQLITE_OK ){ |
| 6585 | rc = lockTable(p, iTab, lockType); |
| 6586 | } |
| 6587 | return rc; |
| 6588 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6589 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6590 | |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6591 | /* |
| 6592 | ** The following debugging interface has to be in this file (rather |
| 6593 | ** than in, for example, test1.c) so that it can get access to |
| 6594 | ** the definition of BtShared. |
| 6595 | */ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 6596 | #if defined(SQLITE_DEBUG) && defined(TCLSH) |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6597 | #include <tcl.h> |
| 6598 | int sqlite3_shared_cache_report( |
| 6599 | void * clientData, |
| 6600 | Tcl_Interp *interp, |
| 6601 | int objc, |
| 6602 | Tcl_Obj *CONST objv[] |
| 6603 | ){ |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6604 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 6605 | const ThreadData *pTd = sqlite3ThreadDataReadOnly(); |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6606 | if( pTd->useSharedData ){ |
| 6607 | BtShared *pBt; |
| 6608 | Tcl_Obj *pRet = Tcl_NewObj(); |
| 6609 | for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame^] | 6610 | const char *zFile = sqlite3PagerFilename(pBt->pPager); |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6611 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1)); |
| 6612 | Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef)); |
| 6613 | } |
| 6614 | Tcl_SetObjResult(interp, pRet); |
| 6615 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6616 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6617 | return TCL_OK; |
| 6618 | } |
| 6619 | #endif |