drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.70 2002/08/13 00:01:17 drh Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
| 15 | ** For a detailed discussion of BTrees, refer to |
| 16 | ** |
| 17 | ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: |
| 18 | ** "Sorting And Searching", pages 473-480. Addison-Wesley |
| 19 | ** Publishing Company, Reading, Massachusetts. |
| 20 | ** |
| 21 | ** The basic idea is that each page of the file contains N database |
| 22 | ** entries and N+1 pointers to subpages. |
| 23 | ** |
| 24 | ** ---------------------------------------------------------------- |
| 25 | ** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) | |
| 26 | ** ---------------------------------------------------------------- |
| 27 | ** |
| 28 | ** All of the keys on the page that Ptr(0) points to have values less |
| 29 | ** than Key(0). All of the keys on page Ptr(1) and its subpages have |
| 30 | ** values greater than Key(0) and less than Key(1). All of the keys |
| 31 | ** on Ptr(N+1) and its subpages have values greater than Key(N). And |
| 32 | ** so forth. |
| 33 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 34 | ** Finding a particular key requires reading O(log(M)) pages from the |
| 35 | ** disk where M is the number of entries in the tree. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 36 | ** |
| 37 | ** In this implementation, a single file can hold one or more separate |
| 38 | ** BTrees. Each BTree is identified by the index of its root page. The |
| 39 | ** key and data for any entry are combined to form the "payload". Up to |
| 40 | ** MX_LOCAL_PAYLOAD bytes of payload can be carried directly on the |
| 41 | ** database page. If the payload is larger than MX_LOCAL_PAYLOAD bytes |
| 42 | ** then surplus bytes are stored on overflow pages. The payload for an |
| 43 | ** entry and the preceding pointer are combined to form a "Cell". Each |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 44 | ** page has a small header which contains the Ptr(N+1) pointer. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 45 | ** |
| 46 | ** The first page of the file contains a magic string used to verify that |
| 47 | ** the file really is a valid BTree database, a pointer to a list of unused |
| 48 | ** pages in the file, and some meta information. The root of the first |
| 49 | ** BTree begins on page 2 of the file. (Pages are numbered beginning with |
| 50 | ** 1, not 0.) Thus a minimum database contains 2 pages. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 51 | */ |
| 52 | #include "sqliteInt.h" |
| 53 | #include "pager.h" |
| 54 | #include "btree.h" |
| 55 | #include <assert.h> |
| 56 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 57 | /* |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 58 | ** Macros used for byteswapping. B is a pointer to the Btree |
| 59 | ** structure. This is needed to access the Btree.needSwab boolean |
| 60 | ** in order to tell if byte swapping is needed or not. |
| 61 | ** X is an unsigned integer. SWAB16 byte swaps a 16-bit integer. |
| 62 | ** SWAB32 byteswaps a 32-bit integer. |
| 63 | */ |
| 64 | #define SWAB16(B,X) ((B)->needSwab? swab16(X) : (X)) |
| 65 | #define SWAB32(B,X) ((B)->needSwab? swab32(X) : (X)) |
| 66 | #define SWAB_ADD(B,X,A) \ |
| 67 | if((B)->needSwab){ X=swab32(swab32(X)+A); }else{ X += (A); } |
| 68 | |
| 69 | /* |
| 70 | ** The following global variable - available only if SQLITE_TEST is |
| 71 | ** defined - is used to determine whether new databases are created in |
| 72 | ** native byte order or in non-native byte order. Non-native byte order |
| 73 | ** databases are created for testing purposes only. Under normal operation, |
| 74 | ** only native byte-order databases should be created, but we should be |
| 75 | ** able to read or write existing databases regardless of the byteorder. |
| 76 | */ |
| 77 | #ifdef SQLITE_TEST |
| 78 | int btree_native_byte_order = 1; |
drh | 74587e5 | 2002-08-13 00:01:16 +0000 | [diff] [blame^] | 79 | #else |
| 80 | # define btree_native_byte_order 1 |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
| 83 | /* |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 84 | ** Forward declarations of structures used only in this file. |
| 85 | */ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 86 | typedef struct PageOne PageOne; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 87 | typedef struct MemPage MemPage; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 88 | typedef struct PageHdr PageHdr; |
| 89 | typedef struct Cell Cell; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 90 | typedef struct CellHdr CellHdr; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 91 | typedef struct FreeBlk FreeBlk; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 92 | typedef struct OverflowPage OverflowPage; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 93 | typedef struct FreelistInfo FreelistInfo; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | ** All structures on a database page are aligned to 4-byte boundries. |
| 97 | ** This routine rounds up a number of bytes to the next multiple of 4. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 98 | ** |
| 99 | ** This might need to change for computer architectures that require |
| 100 | ** and 8-byte alignment boundry for structures. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 101 | */ |
| 102 | #define ROUNDUP(X) ((X+3) & ~3) |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 103 | |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 104 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 105 | ** This is a magic string that appears at the beginning of every |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 106 | ** SQLite database in order to identify the file as a real database. |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 107 | */ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 108 | static const char zMagicHeader[] = |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 109 | "** This file contains an SQLite 2.1 database **"; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 110 | #define MAGIC_SIZE (sizeof(zMagicHeader)) |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 113 | ** This is a magic integer also used to test the integrity of the database |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 114 | ** file. This integer is used in addition to the string above so that |
| 115 | ** if the file is written on a little-endian architecture and read |
| 116 | ** on a big-endian architectures (or vice versa) we can detect the |
| 117 | ** problem. |
| 118 | ** |
| 119 | ** The number used was obtained at random and has no special |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 120 | ** significance other than the fact that it represents a different |
| 121 | ** integer on little-endian and big-endian machines. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 122 | */ |
| 123 | #define MAGIC 0xdae37528 |
| 124 | |
| 125 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 126 | ** The first page of the database file contains a magic header string |
| 127 | ** to identify the file as an SQLite database file. It also contains |
| 128 | ** a pointer to the first free page of the file. Page 2 contains the |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 129 | ** root of the principle BTree. The file might contain other BTrees |
| 130 | ** rooted on pages above 2. |
| 131 | ** |
| 132 | ** The first page also contains SQLITE_N_BTREE_META integers that |
| 133 | ** can be used by higher-level routines. |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 134 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 135 | ** Remember that pages are numbered beginning with 1. (See pager.c |
| 136 | ** for additional information.) Page 0 does not exist and a page |
| 137 | ** number of 0 is used to mean "no such page". |
| 138 | */ |
| 139 | struct PageOne { |
| 140 | char zMagic[MAGIC_SIZE]; /* String that identifies the file as a database */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 141 | int iMagic; /* Integer to verify correct byte order */ |
| 142 | Pgno freeList; /* First free page in a list of all free pages */ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 143 | int nFree; /* Number of pages on the free list */ |
| 144 | int aMeta[SQLITE_N_BTREE_META-1]; /* User defined integers */ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /* |
| 148 | ** Each database page has a header that is an instance of this |
| 149 | ** structure. |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 150 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 151 | ** PageHdr.firstFree is 0 if there is no free space on this page. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 152 | ** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 153 | ** FreeBlk structure that describes the first block of free space. |
| 154 | ** All free space is defined by a linked list of FreeBlk structures. |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 155 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 156 | ** Data is stored in a linked list of Cell structures. PageHdr.firstCell |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 157 | ** is the index into MemPage.u.aDisk[] of the first cell on the page. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 158 | ** Cells are kept in sorted order. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 159 | ** |
| 160 | ** A Cell contains all information about a database entry and a pointer |
| 161 | ** to a child page that contains other entries less than itself. In |
| 162 | ** other words, the i-th Cell contains both Ptr(i) and Key(i). The |
| 163 | ** right-most pointer of the page is contained in PageHdr.rightChild. |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 164 | */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 165 | struct PageHdr { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 166 | Pgno rightChild; /* Child page that comes after all cells on this page */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 167 | u16 firstCell; /* Index in MemPage.u.aDisk[] of the first cell */ |
| 168 | u16 firstFree; /* Index in MemPage.u.aDisk[] of the first free block */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 169 | }; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 170 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 171 | /* |
| 172 | ** Entries on a page of the database are called "Cells". Each Cell |
| 173 | ** has a header and data. This structure defines the header. The |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 174 | ** key and data (collectively the "payload") follow this header on |
| 175 | ** the database page. |
| 176 | ** |
| 177 | ** A definition of the complete Cell structure is given below. The |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 178 | ** header for the cell must be defined first in order to do some |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 179 | ** of the sizing #defines that follow. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 180 | */ |
| 181 | struct CellHdr { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 182 | Pgno leftChild; /* Child page that comes before this cell */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 183 | u16 nKey; /* Number of bytes in the key */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 184 | u16 iNext; /* Index in MemPage.u.aDisk[] of next cell in sorted order */ |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 185 | u8 nKeyHi; /* Upper 8 bits of key size for keys larger than 64K bytes */ |
| 186 | u8 nDataHi; /* Upper 8 bits of data size when the size is more than 64K */ |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 187 | u16 nData; /* Number of bytes of data */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 188 | }; |
drh | 58a1168 | 2001-11-10 13:51:08 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | ** The key and data size are split into a lower 16-bit segment and an |
| 192 | ** upper 8-bit segment in order to pack them together into a smaller |
| 193 | ** space. The following macros reassembly a key or data size back |
| 194 | ** into an integer. |
| 195 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 196 | #define NKEY(b,h) (SWAB16(b,h.nKey) + h.nKeyHi*65536) |
| 197 | #define NDATA(b,h) (SWAB16(b,h.nData) + h.nDataHi*65536) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | ** The minimum size of a complete Cell. The Cell must contain a header |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 201 | ** and at least 4 bytes of payload. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 202 | */ |
| 203 | #define MIN_CELL_SIZE (sizeof(CellHdr)+4) |
| 204 | |
| 205 | /* |
| 206 | ** The maximum number of database entries that can be held in a single |
| 207 | ** page of the database. |
| 208 | */ |
| 209 | #define MX_CELL ((SQLITE_PAGE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE) |
| 210 | |
| 211 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 212 | ** The amount of usable space on a single page of the BTree. This is the |
| 213 | ** page size minus the overhead of the page header. |
| 214 | */ |
| 215 | #define USABLE_SPACE (SQLITE_PAGE_SIZE - sizeof(PageHdr)) |
| 216 | |
| 217 | /* |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 218 | ** The maximum amount of payload (in bytes) that can be stored locally for |
| 219 | ** a database entry. If the entry contains more data than this, the |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 220 | ** extra goes onto overflow pages. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 221 | ** |
| 222 | ** This number is chosen so that at least 4 cells will fit on every page. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 223 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 224 | #define MX_LOCAL_PAYLOAD ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3) |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 225 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 226 | /* |
| 227 | ** Data on a database page is stored as a linked list of Cell structures. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 228 | ** Both the key and the data are stored in aPayload[]. The key always comes |
| 229 | ** first. The aPayload[] field grows as necessary to hold the key and data, |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 230 | ** up to a maximum of MX_LOCAL_PAYLOAD bytes. If the size of the key and |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 231 | ** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the |
| 232 | ** page number of the first overflow page. |
| 233 | ** |
| 234 | ** Though this structure is fixed in size, the Cell on the database |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 235 | ** page varies in size. Every cell has a CellHdr and at least 4 bytes |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 236 | ** of payload space. Additional payload bytes (up to the maximum of |
| 237 | ** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as |
| 238 | ** needed. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 239 | */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 240 | struct Cell { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 241 | CellHdr h; /* The cell header */ |
| 242 | char aPayload[MX_LOCAL_PAYLOAD]; /* Key and data */ |
| 243 | Pgno ovfl; /* The first overflow page */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 244 | }; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | ** Free space on a page is remembered using a linked list of the FreeBlk |
| 248 | ** structures. Space on a database page is allocated in increments of |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 249 | ** at least 4 bytes and is always aligned to a 4-byte boundry. The |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 250 | ** linked list of FreeBlks is always kept in order by address. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 251 | */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 252 | struct FreeBlk { |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 253 | u16 iSize; /* Number of bytes in this block of free space */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 254 | u16 iNext; /* Index in MemPage.u.aDisk[] of the next free block */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 255 | }; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 256 | |
| 257 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 258 | ** The number of bytes of payload that will fit on a single overflow page. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 259 | */ |
| 260 | #define OVERFLOW_SIZE (SQLITE_PAGE_SIZE-sizeof(Pgno)) |
| 261 | |
| 262 | /* |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 263 | ** When the key and data for a single entry in the BTree will not fit in |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 264 | ** the MX_LOCAL_PAYLOAD bytes of space available on the database page, |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 265 | ** then all extra bytes are written to a linked list of overflow pages. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 266 | ** Each overflow page is an instance of the following structure. |
| 267 | ** |
| 268 | ** Unused pages in the database are also represented by instances of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 269 | ** the OverflowPage structure. The PageOne.freeList field is the |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 270 | ** page number of the first page in a linked list of unused database |
| 271 | ** pages. |
| 272 | */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 273 | struct OverflowPage { |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 274 | Pgno iNext; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 275 | char aPayload[OVERFLOW_SIZE]; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 276 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 277 | |
| 278 | /* |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 279 | ** The PageOne.freeList field points to a linked list of overflow pages |
| 280 | ** hold information about free pages. The aPayload section of each |
| 281 | ** overflow page contains an instance of the following structure. The |
| 282 | ** aFree[] array holds the page number of nFree unused pages in the disk |
| 283 | ** file. |
| 284 | */ |
| 285 | struct FreelistInfo { |
| 286 | int nFree; |
| 287 | Pgno aFree[(OVERFLOW_SIZE-sizeof(int))/sizeof(Pgno)]; |
| 288 | }; |
| 289 | |
| 290 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 291 | ** For every page in the database file, an instance of the following structure |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 292 | ** is stored in memory. The u.aDisk[] array contains the raw bits read from |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 293 | ** the disk. The rest is auxiliary information held in memory only. The |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 294 | ** auxiliary info is only valid for regular database pages - it is not |
| 295 | ** used for overflow pages and pages on the freelist. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 296 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 297 | ** Of particular interest in the auxiliary info is the apCell[] entry. Each |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 298 | ** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 299 | ** put in this array so that they can be accessed in constant time, rather |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 300 | ** than in linear time which would be needed if we had to walk the linked |
| 301 | ** list on every access. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 302 | ** |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 303 | ** Note that apCell[] contains enough space to hold up to two more Cells |
| 304 | ** than can possibly fit on one page. In the steady state, every apCell[] |
| 305 | ** points to memory inside u.aDisk[]. But in the middle of an insert |
| 306 | ** operation, some apCell[] entries may temporarily point to data space |
| 307 | ** outside of u.aDisk[]. This is a transient situation that is quickly |
| 308 | ** resolved. But while it is happening, it is possible for a database |
| 309 | ** page to hold as many as two more cells than it might otherwise hold. |
drh | 18b81e5 | 2001-11-01 13:52:52 +0000 | [diff] [blame] | 310 | ** The extra two entries in apCell[] are an allowance for this situation. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 311 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 312 | ** The pParent field points back to the parent page. This allows us to |
| 313 | ** walk up the BTree from any leaf to the root. Care must be taken to |
| 314 | ** unref() the parent page pointer when this page is no longer referenced. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 315 | ** The pageDestructor() routine handles that chore. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 316 | */ |
| 317 | struct MemPage { |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 318 | union { |
| 319 | char aDisk[SQLITE_PAGE_SIZE]; /* Page data stored on disk */ |
| 320 | PageHdr hdr; /* Overlay page header */ |
| 321 | } u; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 322 | int isInit; /* True if auxiliary data is initialized */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 323 | MemPage *pParent; /* The parent of this page. NULL for root */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 324 | int nFree; /* Number of free bytes in u.aDisk[] */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 325 | int nCell; /* Number of entries on this page */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 326 | int isOverfull; /* Some apCell[] points outside u.aDisk[] */ |
| 327 | Cell *apCell[MX_CELL+2]; /* All data entires in sorted order */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 328 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 329 | |
| 330 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 331 | ** The in-memory image of a disk page has the auxiliary information appended |
| 332 | ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold |
| 333 | ** that extra information. |
| 334 | */ |
| 335 | #define EXTRA_SIZE (sizeof(MemPage)-SQLITE_PAGE_SIZE) |
| 336 | |
| 337 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 338 | ** Everything we need to know about an open database |
| 339 | */ |
| 340 | struct Btree { |
| 341 | Pager *pPager; /* The page cache */ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 342 | BtCursor *pCursor; /* A list of all open cursors */ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 343 | PageOne *page1; /* First page of the database */ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 344 | u8 inTrans; /* True if a transaction is in progress */ |
| 345 | u8 inCkpt; /* True if there is a checkpoint on the transaction */ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 346 | u8 readOnly; /* True if the underlying file is readonly */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 347 | u8 needSwab; /* Need to byte-swapping */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 348 | Hash locks; /* Key: root page number. Data: lock count */ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 349 | }; |
| 350 | typedef Btree Bt; |
| 351 | |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 352 | /* |
| 353 | ** A cursor is a pointer to a particular entry in the BTree. |
| 354 | ** The entry is identified by its MemPage and the index in |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 355 | ** MemPage.apCell[] of the entry. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 356 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 357 | struct BtCursor { |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 358 | Btree *pBt; /* The Btree to which this cursor belongs */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 359 | BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 360 | Pgno pgnoRoot; /* The root page of this tree */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 361 | MemPage *pPage; /* Page that contains the entry */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 362 | int idx; /* Index of the entry in pPage->apCell[] */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 363 | u8 wrFlag; /* True if writable */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 364 | u8 bSkipNext; /* sqliteBtreeNext() is no-op if true */ |
| 365 | u8 iMatch; /* compare result from last sqliteBtreeMoveto() */ |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 366 | }; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 367 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 368 | /* |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 369 | ** Routines for byte swapping. |
| 370 | */ |
| 371 | u16 swab16(u16 x){ |
| 372 | return ((x & 0xff)<<8) | ((x>>8)&0xff); |
| 373 | } |
| 374 | u32 swab32(u32 x){ |
| 375 | return ((x & 0xff)<<24) | ((x & 0xff00)<<8) | |
| 376 | ((x>>8) & 0xff00) | ((x>>24)&0xff); |
| 377 | } |
| 378 | |
| 379 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 380 | ** Compute the total number of bytes that a Cell needs on the main |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 381 | ** database page. The number returned includes the Cell header, |
| 382 | ** local payload storage, and the pointer to overflow pages (if |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 383 | ** applicable). Additional space allocated on overflow pages |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 384 | ** is NOT included in the value returned from this routine. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 385 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 386 | static int cellSize(Btree *pBt, Cell *pCell){ |
| 387 | int n = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 388 | if( n>MX_LOCAL_PAYLOAD ){ |
| 389 | n = MX_LOCAL_PAYLOAD + sizeof(Pgno); |
| 390 | }else{ |
| 391 | n = ROUNDUP(n); |
| 392 | } |
| 393 | n += sizeof(CellHdr); |
| 394 | return n; |
| 395 | } |
| 396 | |
| 397 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 398 | ** Defragment the page given. All Cells are moved to the |
| 399 | ** beginning of the page and all free space is collected |
| 400 | ** into one big FreeBlk at the end of the page. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 401 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 402 | static void defragmentPage(Btree *pBt, MemPage *pPage){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 403 | int pc, i, n; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 404 | FreeBlk *pFBlk; |
| 405 | char newPage[SQLITE_PAGE_SIZE]; |
| 406 | |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 407 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 408 | assert( pPage->isInit ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 409 | pc = sizeof(PageHdr); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 410 | pPage->u.hdr.firstCell = SWAB16(pBt, pc); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 411 | memcpy(newPage, pPage->u.aDisk, pc); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 412 | for(i=0; i<pPage->nCell; i++){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 413 | Cell *pCell = pPage->apCell[i]; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 414 | |
| 415 | /* This routine should never be called on an overfull page. The |
| 416 | ** following asserts verify that constraint. */ |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 417 | assert( Addr(pCell) > Addr(pPage) ); |
| 418 | assert( Addr(pCell) < Addr(pPage) + SQLITE_PAGE_SIZE ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 419 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 420 | n = cellSize(pBt, pCell); |
| 421 | pCell->h.iNext = SWAB16(pBt, pc + n); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 422 | memcpy(&newPage[pc], pCell, n); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 423 | pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc]; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 424 | pc += n; |
| 425 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 426 | assert( pPage->nFree==SQLITE_PAGE_SIZE-pc ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 427 | memcpy(pPage->u.aDisk, newPage, pc); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 428 | if( pPage->nCell>0 ){ |
| 429 | pPage->apCell[pPage->nCell-1]->h.iNext = 0; |
| 430 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 431 | pFBlk = (FreeBlk*)&pPage->u.aDisk[pc]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 432 | pFBlk->iSize = SWAB16(pBt, SQLITE_PAGE_SIZE - pc); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 433 | pFBlk->iNext = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 434 | pPage->u.hdr.firstFree = SWAB16(pBt, pc); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 435 | memset(&pFBlk[1], 0, SQLITE_PAGE_SIZE - pc - sizeof(FreeBlk)); |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 436 | } |
| 437 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 438 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 439 | ** Allocate nByte bytes of space on a page. nByte must be a |
| 440 | ** multiple of 4. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 441 | ** |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 442 | ** Return the index into pPage->u.aDisk[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 443 | ** the new allocation. Or return 0 if there is not enough free |
| 444 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 445 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 446 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 447 | ** nBytes of contiguous free space, then this routine automatically |
| 448 | ** calls defragementPage() to consolidate all free space before |
| 449 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 450 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 451 | static int allocateSpace(Btree *pBt, MemPage *pPage, int nByte){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 452 | FreeBlk *p; |
| 453 | u16 *pIdx; |
| 454 | int start; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 455 | int cnt = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 456 | int iSize; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 457 | |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 458 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 459 | assert( nByte==ROUNDUP(nByte) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 460 | assert( pPage->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 461 | if( pPage->nFree<nByte || pPage->isOverfull ) return 0; |
| 462 | pIdx = &pPage->u.hdr.firstFree; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 463 | p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)]; |
| 464 | while( (iSize = SWAB16(pBt, p->iSize))<nByte ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 465 | assert( cnt++ < SQLITE_PAGE_SIZE/4 ); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 466 | if( p->iNext==0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 467 | defragmentPage(pBt, pPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 468 | pIdx = &pPage->u.hdr.firstFree; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 469 | }else{ |
| 470 | pIdx = &p->iNext; |
| 471 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 472 | p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)]; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 473 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 474 | if( iSize==nByte ){ |
| 475 | start = SWAB16(pBt, *pIdx); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 476 | *pIdx = p->iNext; |
| 477 | }else{ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 478 | FreeBlk *pNew; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 479 | start = SWAB16(pBt, *pIdx); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 480 | pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte]; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 481 | pNew->iNext = p->iNext; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 482 | pNew->iSize = SWAB16(pBt, iSize - nByte); |
| 483 | *pIdx = SWAB16(pBt, start + nByte); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 484 | } |
| 485 | pPage->nFree -= nByte; |
| 486 | return start; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 490 | ** Return a section of the MemPage.u.aDisk[] to the freelist. |
| 491 | ** The first byte of the new free block is pPage->u.aDisk[start] |
| 492 | ** and the size of the block is "size" bytes. Size must be |
| 493 | ** a multiple of 4. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 494 | ** |
| 495 | ** Most of the effort here is involved in coalesing adjacent |
| 496 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 497 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 498 | static void freeSpace(Btree *pBt, MemPage *pPage, int start, int size){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 499 | int end = start + size; |
| 500 | u16 *pIdx, idx; |
| 501 | FreeBlk *pFBlk; |
| 502 | FreeBlk *pNew; |
| 503 | FreeBlk *pNext; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 504 | int iSize; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 505 | |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 506 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 507 | assert( size == ROUNDUP(size) ); |
| 508 | assert( start == ROUNDUP(start) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 509 | assert( pPage->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 510 | pIdx = &pPage->u.hdr.firstFree; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 511 | idx = SWAB16(pBt, *pIdx); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 512 | while( idx!=0 && idx<start ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 513 | pFBlk = (FreeBlk*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 514 | iSize = SWAB16(pBt, pFBlk->iSize); |
| 515 | if( idx + iSize == start ){ |
| 516 | pFBlk->iSize = SWAB16(pBt, iSize + size); |
| 517 | if( idx + iSize + size == SWAB16(pBt, pFBlk->iNext) ){ |
| 518 | pNext = (FreeBlk*)&pPage->u.aDisk[idx + iSize + size]; |
| 519 | if( pBt->needSwab ){ |
| 520 | pFBlk->iSize = swab16(swab16(pNext->iSize)+iSize+size); |
| 521 | }else{ |
| 522 | pFBlk->iSize += pNext->iSize; |
| 523 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 524 | pFBlk->iNext = pNext->iNext; |
| 525 | } |
| 526 | pPage->nFree += size; |
| 527 | return; |
| 528 | } |
| 529 | pIdx = &pFBlk->iNext; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 530 | idx = SWAB16(pBt, *pIdx); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 531 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 532 | pNew = (FreeBlk*)&pPage->u.aDisk[start]; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 533 | if( idx != end ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 534 | pNew->iSize = SWAB16(pBt, size); |
| 535 | pNew->iNext = SWAB16(pBt, idx); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 536 | }else{ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 537 | pNext = (FreeBlk*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 538 | pNew->iSize = SWAB16(pBt, size + SWAB16(pBt, pNext->iSize)); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 539 | pNew->iNext = pNext->iNext; |
| 540 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 541 | *pIdx = SWAB16(pBt, start); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 542 | pPage->nFree += size; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /* |
| 546 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 547 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 548 | ** The pParent parameter must be a pointer to the MemPage which |
| 549 | ** is the parent of the page being initialized. The root of the |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 550 | ** BTree (usually page 2) has no parent and so for that page, |
| 551 | ** pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 552 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 553 | ** Return SQLITE_OK on success. If we see that the page does |
| 554 | ** not contained a well-formed database page, then return |
| 555 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 556 | ** guarantee that the page is well-formed. It only shows that |
| 557 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 558 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 559 | static int initPage(Bt *pBt, MemPage *pPage, Pgno pgnoThis, MemPage *pParent){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 560 | int idx; /* An index into pPage->u.aDisk[] */ |
| 561 | Cell *pCell; /* A pointer to a Cell in pPage->u.aDisk[] */ |
| 562 | FreeBlk *pFBlk; /* A pointer to a free block in pPage->u.aDisk[] */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 563 | int sz; /* The size of a Cell in bytes */ |
| 564 | int freeSpace; /* Amount of free space on the page */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 565 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 566 | if( pPage->pParent ){ |
| 567 | assert( pPage->pParent==pParent ); |
| 568 | return SQLITE_OK; |
| 569 | } |
| 570 | if( pParent ){ |
| 571 | pPage->pParent = pParent; |
| 572 | sqlitepager_ref(pParent); |
| 573 | } |
| 574 | if( pPage->isInit ) return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 575 | pPage->isInit = 1; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 576 | pPage->nCell = 0; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 577 | freeSpace = USABLE_SPACE; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 578 | idx = SWAB16(pBt, pPage->u.hdr.firstCell); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 579 | while( idx!=0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 580 | if( idx>SQLITE_PAGE_SIZE-MIN_CELL_SIZE ) goto page_format_error; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 581 | if( idx<sizeof(PageHdr) ) goto page_format_error; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 582 | if( idx!=ROUNDUP(idx) ) goto page_format_error; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 583 | pCell = (Cell*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 584 | sz = cellSize(pBt, pCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 585 | if( idx+sz > SQLITE_PAGE_SIZE ) goto page_format_error; |
| 586 | freeSpace -= sz; |
| 587 | pPage->apCell[pPage->nCell++] = pCell; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 588 | idx = SWAB16(pBt, pCell->h.iNext); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 589 | } |
| 590 | pPage->nFree = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 591 | idx = SWAB16(pBt, pPage->u.hdr.firstFree); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 592 | while( idx!=0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 593 | int iNext; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 594 | if( idx>SQLITE_PAGE_SIZE-sizeof(FreeBlk) ) goto page_format_error; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 595 | if( idx<sizeof(PageHdr) ) goto page_format_error; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 596 | pFBlk = (FreeBlk*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 597 | pPage->nFree += SWAB16(pBt, pFBlk->iSize); |
| 598 | iNext = SWAB16(pBt, pFBlk->iNext); |
| 599 | if( iNext>0 && iNext <= idx ) goto page_format_error; |
| 600 | idx = iNext; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 601 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 602 | if( pPage->nCell==0 && pPage->nFree==0 ){ |
| 603 | /* As a special case, an uninitialized root page appears to be |
| 604 | ** an empty database */ |
| 605 | return SQLITE_OK; |
| 606 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 607 | if( pPage->nFree!=freeSpace ) goto page_format_error; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 608 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 609 | |
| 610 | page_format_error: |
| 611 | return SQLITE_CORRUPT; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 615 | ** Set up a raw page so that it looks like a database page holding |
| 616 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 617 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 618 | static void zeroPage(Btree *pBt, MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 619 | PageHdr *pHdr; |
| 620 | FreeBlk *pFBlk; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 621 | assert( sqlitepager_iswriteable(pPage) ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 622 | memset(pPage, 0, SQLITE_PAGE_SIZE); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 623 | pHdr = &pPage->u.hdr; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 624 | pHdr->firstCell = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 625 | pHdr->firstFree = SWAB16(pBt, sizeof(*pHdr)); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 626 | pFBlk = (FreeBlk*)&pHdr[1]; |
| 627 | pFBlk->iNext = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 628 | pPage->nFree = SQLITE_PAGE_SIZE - sizeof(*pHdr); |
| 629 | pFBlk->iSize = SWAB16(pBt, pPage->nFree); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 630 | pPage->nCell = 0; |
| 631 | pPage->isOverfull = 0; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 635 | ** This routine is called when the reference count for a page |
| 636 | ** reaches zero. We need to unref the pParent pointer when that |
| 637 | ** happens. |
| 638 | */ |
| 639 | static void pageDestructor(void *pData){ |
| 640 | MemPage *pPage = (MemPage*)pData; |
| 641 | if( pPage->pParent ){ |
| 642 | MemPage *pParent = pPage->pParent; |
| 643 | pPage->pParent = 0; |
| 644 | sqlitepager_unref(pParent); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /* |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 649 | ** Open a new database. |
| 650 | ** |
| 651 | ** Actually, this routine just sets up the internal data structures |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 652 | ** for accessing the database. We do not open the database file |
| 653 | ** until the first page is loaded. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 654 | ** |
| 655 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 656 | ** a new database with a random name is created. This randomly named |
| 657 | ** database file will be deleted when sqliteBtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 658 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 659 | int sqliteBtreeOpen( |
| 660 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 661 | int mode, /* Not currently used */ |
| 662 | int nCache, /* How many pages in the page cache */ |
| 663 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 664 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 665 | Btree *pBt; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 666 | int rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 667 | |
| 668 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 669 | if( pBt==0 ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 670 | *ppBtree = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 671 | return SQLITE_NOMEM; |
| 672 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 673 | if( nCache<10 ) nCache = 10; |
| 674 | rc = sqlitepager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 675 | if( rc!=SQLITE_OK ){ |
| 676 | if( pBt->pPager ) sqlitepager_close(pBt->pPager); |
| 677 | sqliteFree(pBt); |
| 678 | *ppBtree = 0; |
| 679 | return rc; |
| 680 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 681 | sqlitepager_set_destructor(pBt->pPager, pageDestructor); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 682 | pBt->pCursor = 0; |
| 683 | pBt->page1 = 0; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 684 | pBt->readOnly = sqlitepager_isreadonly(pBt->pPager); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 685 | sqliteHashInit(&pBt->locks, SQLITE_HASH_INT, 0); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 686 | *ppBtree = pBt; |
| 687 | return SQLITE_OK; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | ** Close an open database and invalidate all cursors. |
| 692 | */ |
| 693 | int sqliteBtreeClose(Btree *pBt){ |
| 694 | while( pBt->pCursor ){ |
| 695 | sqliteBtreeCloseCursor(pBt->pCursor); |
| 696 | } |
| 697 | sqlitepager_close(pBt->pPager); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 698 | sqliteHashClear(&pBt->locks); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 699 | sqliteFree(pBt); |
| 700 | return SQLITE_OK; |
| 701 | } |
| 702 | |
| 703 | /* |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 704 | ** Change the limit on the number of pages allowed the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 705 | ** |
| 706 | ** The maximum number of cache pages is set to the absolute |
| 707 | ** value of mxPage. If mxPage is negative, the pager will |
| 708 | ** operate asynchronously - it will not stop to do fsync()s |
| 709 | ** to insure data is written to the disk surface before |
| 710 | ** continuing. Transactions still work if synchronous is off, |
| 711 | ** and the database cannot be corrupted if this program |
| 712 | ** crashes. But if the operating system crashes or there is |
| 713 | ** an abrupt power failure when synchronous is off, the database |
| 714 | ** could be left in an inconsistent and unrecoverable state. |
| 715 | ** Synchronous is on by default so database corruption is not |
| 716 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 717 | */ |
| 718 | int sqliteBtreeSetCacheSize(Btree *pBt, int mxPage){ |
| 719 | sqlitepager_set_cachesize(pBt->pPager, mxPage); |
| 720 | return SQLITE_OK; |
| 721 | } |
| 722 | |
| 723 | /* |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 724 | ** Get a reference to page1 of the database file. This will |
| 725 | ** also acquire a readlock on that file. |
| 726 | ** |
| 727 | ** SQLITE_OK is returned on success. If the file is not a |
| 728 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 729 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
| 730 | ** is returned if we run out of memory. SQLITE_PROTOCOL is returned |
| 731 | ** if there is a locking protocol violation. |
| 732 | */ |
| 733 | static int lockBtree(Btree *pBt){ |
| 734 | int rc; |
| 735 | if( pBt->page1 ) return SQLITE_OK; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 736 | rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 737 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 738 | |
| 739 | /* Do some checking to help insure the file we opened really is |
| 740 | ** a valid database file. |
| 741 | */ |
| 742 | if( sqlitepager_pagecount(pBt->pPager)>0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 743 | PageOne *pP1 = pBt->page1; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 744 | if( strcmp(pP1->zMagic,zMagicHeader)!=0 || |
| 745 | (pP1->iMagic!=MAGIC && swab32(pP1->iMagic)!=MAGIC) ){ |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 746 | rc = SQLITE_CORRUPT; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 747 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 748 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 749 | pBt->needSwab = pP1->iMagic!=MAGIC; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 750 | } |
| 751 | return rc; |
| 752 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 753 | page1_init_failed: |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 754 | sqlitepager_unref(pBt->page1); |
| 755 | pBt->page1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 756 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 760 | ** If there are no outstanding cursors and we are not in the middle |
| 761 | ** of a transaction but there is a read lock on the database, then |
| 762 | ** this routine unrefs the first page of the database file which |
| 763 | ** has the effect of releasing the read lock. |
| 764 | ** |
| 765 | ** If there are any outstanding cursors, this routine is a no-op. |
| 766 | ** |
| 767 | ** If there is a transaction in progress, this routine is a no-op. |
| 768 | */ |
| 769 | static void unlockBtreeIfUnused(Btree *pBt){ |
| 770 | if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){ |
| 771 | sqlitepager_unref(pBt->page1); |
| 772 | pBt->page1 = 0; |
| 773 | pBt->inTrans = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 774 | pBt->inCkpt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | /* |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 779 | ** Create a new database by initializing the first two pages of the |
| 780 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 781 | */ |
| 782 | static int newDatabase(Btree *pBt){ |
| 783 | MemPage *pRoot; |
| 784 | PageOne *pP1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 785 | int rc; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 786 | if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 787 | pP1 = pBt->page1; |
| 788 | rc = sqlitepager_write(pBt->page1); |
| 789 | if( rc ) return rc; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 790 | rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 791 | if( rc ) return rc; |
| 792 | rc = sqlitepager_write(pRoot); |
| 793 | if( rc ){ |
| 794 | sqlitepager_unref(pRoot); |
| 795 | return rc; |
| 796 | } |
| 797 | strcpy(pP1->zMagic, zMagicHeader); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 798 | if( btree_native_byte_order ){ |
| 799 | pP1->iMagic = MAGIC; |
| 800 | pBt->needSwab = 0; |
| 801 | }else{ |
| 802 | pP1->iMagic = swab32(MAGIC); |
| 803 | pBt->needSwab = 1; |
| 804 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 805 | zeroPage(pBt, pRoot); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 806 | sqlitepager_unref(pRoot); |
| 807 | return SQLITE_OK; |
| 808 | } |
| 809 | |
| 810 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 811 | ** Attempt to start a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 812 | ** |
| 813 | ** A transaction must be started before attempting any changes |
| 814 | ** to the database. None of the following routines will work |
| 815 | ** unless a transaction is started first: |
| 816 | ** |
| 817 | ** sqliteBtreeCreateTable() |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 818 | ** sqliteBtreeCreateIndex() |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 819 | ** sqliteBtreeClearTable() |
| 820 | ** sqliteBtreeDropTable() |
| 821 | ** sqliteBtreeInsert() |
| 822 | ** sqliteBtreeDelete() |
| 823 | ** sqliteBtreeUpdateMeta() |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 824 | */ |
| 825 | int sqliteBtreeBeginTrans(Btree *pBt){ |
| 826 | int rc; |
| 827 | if( pBt->inTrans ) return SQLITE_ERROR; |
| 828 | if( pBt->page1==0 ){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 829 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 830 | if( rc!=SQLITE_OK ){ |
| 831 | return rc; |
| 832 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 833 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 834 | if( pBt->readOnly ){ |
| 835 | rc = SQLITE_OK; |
| 836 | }else{ |
| 837 | rc = sqlitepager_begin(pBt->page1); |
| 838 | if( rc==SQLITE_OK ){ |
| 839 | rc = newDatabase(pBt); |
| 840 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 841 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 842 | if( rc==SQLITE_OK ){ |
| 843 | pBt->inTrans = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 844 | pBt->inCkpt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 845 | }else{ |
| 846 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 847 | } |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 848 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 852 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 853 | ** |
| 854 | ** This will release the write lock on the database file. If there |
| 855 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 856 | */ |
| 857 | int sqliteBtreeCommit(Btree *pBt){ |
| 858 | int rc; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 859 | if( pBt->inTrans==0 ) return SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 860 | rc = pBt->readOnly ? SQLITE_OK : sqlitepager_commit(pBt->pPager); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 861 | pBt->inTrans = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 862 | pBt->inCkpt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 863 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 864 | return rc; |
| 865 | } |
| 866 | |
| 867 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 868 | ** Rollback the transaction in progress. All cursors will be |
| 869 | ** invalided by this operation. Any attempt to use a cursor |
| 870 | ** that was open at the beginning of this operation will result |
| 871 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 872 | ** |
| 873 | ** This will release the write lock on the database file. If there |
| 874 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 875 | */ |
| 876 | int sqliteBtreeRollback(Btree *pBt){ |
| 877 | int rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 878 | BtCursor *pCur; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 879 | if( pBt->inTrans==0 ) return SQLITE_OK; |
| 880 | pBt->inTrans = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 881 | pBt->inCkpt = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 882 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 883 | if( pCur->pPage ){ |
| 884 | sqlitepager_unref(pCur->pPage); |
| 885 | pCur->pPage = 0; |
| 886 | } |
| 887 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 888 | rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 889 | unlockBtreeIfUnused(pBt); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 890 | return rc; |
| 891 | } |
| 892 | |
| 893 | /* |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 894 | ** Set the checkpoint for the current transaction. The checkpoint serves |
| 895 | ** as a sub-transaction that can be rolled back independently of the |
| 896 | ** main transaction. You must start a transaction before starting a |
| 897 | ** checkpoint. The checkpoint is ended automatically if the transaction |
| 898 | ** commits or rolls back. |
| 899 | ** |
| 900 | ** Only one checkpoint may be active at a time. It is an error to try |
| 901 | ** to start a new checkpoint if another checkpoint is already active. |
| 902 | */ |
| 903 | int sqliteBtreeBeginCkpt(Btree *pBt){ |
| 904 | int rc; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 905 | if( !pBt->inTrans || pBt->inCkpt ){ |
| 906 | return SQLITE_ERROR; |
| 907 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 908 | rc = pBt->readOnly ? SQLITE_OK : sqlitepager_ckpt_begin(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 909 | pBt->inCkpt = 1; |
| 910 | return rc; |
| 911 | } |
| 912 | |
| 913 | |
| 914 | /* |
| 915 | ** Commit a checkpoint to transaction currently in progress. If no |
| 916 | ** checkpoint is active, this is a no-op. |
| 917 | */ |
| 918 | int sqliteBtreeCommitCkpt(Btree *pBt){ |
| 919 | int rc; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 920 | if( pBt->inCkpt && !pBt->readOnly ){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 921 | rc = sqlitepager_ckpt_commit(pBt->pPager); |
| 922 | }else{ |
| 923 | rc = SQLITE_OK; |
| 924 | } |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 925 | pBt->inCkpt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 926 | return rc; |
| 927 | } |
| 928 | |
| 929 | /* |
| 930 | ** Rollback the checkpoint to the current transaction. If there |
| 931 | ** is no active checkpoint or transaction, this routine is a no-op. |
| 932 | ** |
| 933 | ** All cursors will be invalided by this operation. Any attempt |
| 934 | ** to use a cursor that was open at the beginning of this operation |
| 935 | ** will result in an error. |
| 936 | */ |
| 937 | int sqliteBtreeRollbackCkpt(Btree *pBt){ |
| 938 | int rc; |
| 939 | BtCursor *pCur; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 940 | if( pBt->inCkpt==0 || pBt->readOnly ) return SQLITE_OK; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 941 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
| 942 | if( pCur->pPage ){ |
| 943 | sqlitepager_unref(pCur->pPage); |
| 944 | pCur->pPage = 0; |
| 945 | } |
| 946 | } |
| 947 | rc = sqlitepager_ckpt_rollback(pBt->pPager); |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 948 | pBt->inCkpt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 949 | return rc; |
| 950 | } |
| 951 | |
| 952 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 953 | ** Create a new cursor for the BTree whose root is on the page |
| 954 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 955 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 956 | ** |
| 957 | ** If wrFlag==0, then the cursor can only be used for reading. |
| 958 | ** If wrFlag==1, then the cursor can be used for reading or writing. |
| 959 | ** A read/write cursor requires exclusive access to its table. There |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 960 | ** cannot be two or more cursors open on the same table if any one of |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 961 | ** cursors is a read/write cursor. But there can be two or more |
| 962 | ** read-only cursors open on the same table. |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 963 | ** |
| 964 | ** No checking is done to make sure that page iTable really is the |
| 965 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 966 | ** will not work correctly. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 967 | */ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 968 | int sqliteBtreeCursor(Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 969 | int rc; |
| 970 | BtCursor *pCur; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 971 | ptr nLock; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 972 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 973 | if( pBt->page1==0 ){ |
| 974 | rc = lockBtree(pBt); |
| 975 | if( rc!=SQLITE_OK ){ |
| 976 | *ppCur = 0; |
| 977 | return rc; |
| 978 | } |
| 979 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 980 | if( wrFlag && pBt->readOnly ){ |
| 981 | *ppCur = 0; |
| 982 | return SQLITE_READONLY; |
| 983 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 984 | pCur = sqliteMalloc( sizeof(*pCur) ); |
| 985 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 986 | rc = SQLITE_NOMEM; |
| 987 | goto create_cursor_exception; |
| 988 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 989 | pCur->pgnoRoot = (Pgno)iTable; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 990 | rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 991 | if( rc!=SQLITE_OK ){ |
| 992 | goto create_cursor_exception; |
| 993 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 994 | rc = initPage(pBt, pCur->pPage, pCur->pgnoRoot, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 995 | if( rc!=SQLITE_OK ){ |
| 996 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 997 | } |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 998 | nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 999 | if( nLock<0 || (nLock>0 && wrFlag) ){ |
| 1000 | rc = SQLITE_LOCKED; |
| 1001 | goto create_cursor_exception; |
| 1002 | } |
| 1003 | nLock = wrFlag ? -1 : nLock+1; |
| 1004 | sqliteHashInsert(&pBt->locks, 0, iTable, (void*)nLock); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1005 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1006 | pCur->wrFlag = wrFlag; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1007 | pCur->idx = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1008 | pCur->pNext = pBt->pCursor; |
| 1009 | if( pCur->pNext ){ |
| 1010 | pCur->pNext->pPrev = pCur; |
| 1011 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1012 | pCur->pPrev = 0; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1013 | pBt->pCursor = pCur; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1014 | *ppCur = pCur; |
| 1015 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1016 | |
| 1017 | create_cursor_exception: |
| 1018 | *ppCur = 0; |
| 1019 | if( pCur ){ |
| 1020 | if( pCur->pPage ) sqlitepager_unref(pCur->pPage); |
| 1021 | sqliteFree(pCur); |
| 1022 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1023 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1024 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1028 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1029 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1030 | */ |
| 1031 | int sqliteBtreeCloseCursor(BtCursor *pCur){ |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 1032 | ptr nLock; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1033 | Btree *pBt = pCur->pBt; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1034 | if( pCur->pPrev ){ |
| 1035 | pCur->pPrev->pNext = pCur->pNext; |
| 1036 | }else{ |
| 1037 | pBt->pCursor = pCur->pNext; |
| 1038 | } |
| 1039 | if( pCur->pNext ){ |
| 1040 | pCur->pNext->pPrev = pCur->pPrev; |
| 1041 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1042 | if( pCur->pPage ){ |
| 1043 | sqlitepager_unref(pCur->pPage); |
| 1044 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1045 | unlockBtreeIfUnused(pBt); |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 1046 | nLock = (ptr)sqliteHashFind(&pBt->locks, 0, pCur->pgnoRoot); |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 1047 | assert( nLock!=0 || sqlite_malloc_failed ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1048 | nLock = nLock<0 ? 0 : nLock-1; |
| 1049 | sqliteHashInsert(&pBt->locks, 0, pCur->pgnoRoot, (void*)nLock); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1050 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1051 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1054 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1055 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 1056 | ** The temporary cursor is not on the cursor list for the Btree. |
| 1057 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1058 | static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1059 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 1060 | pTempCur->pNext = 0; |
| 1061 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1062 | if( pTempCur->pPage ){ |
| 1063 | sqlitepager_ref(pTempCur->pPage); |
| 1064 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1068 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1069 | ** function above. |
| 1070 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1071 | static void releaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1072 | if( pCur->pPage ){ |
| 1073 | sqlitepager_unref(pCur->pPage); |
| 1074 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1078 | ** Set *pSize to the number of bytes of key in the entry the |
| 1079 | ** cursor currently points to. Always return SQLITE_OK. |
| 1080 | ** Failure is not possible. If the cursor is not currently |
| 1081 | ** pointing to an entry (which can happen, for example, if |
| 1082 | ** the database is empty) then *pSize is set to 0. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 1083 | */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1084 | int sqliteBtreeKeySize(BtCursor *pCur, int *pSize){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1085 | Cell *pCell; |
| 1086 | MemPage *pPage; |
| 1087 | |
| 1088 | pPage = pCur->pPage; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1089 | if( pPage==0 || pCur->idx >= pPage->nCell ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1090 | *pSize = 0; |
| 1091 | }else{ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1092 | pCell = pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1093 | *pSize = NKEY(pCur->pBt, pCell->h); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1094 | } |
| 1095 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1096 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1097 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | ** Read payload information from the entry that the pCur cursor is |
| 1100 | ** pointing to. Begin reading the payload at "offset" and read |
| 1101 | ** a total of "amt" bytes. Put the result in zBuf. |
| 1102 | ** |
| 1103 | ** This routine does not make a distinction between key and data. |
| 1104 | ** It just reads bytes from the payload area. |
| 1105 | */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1106 | static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1107 | char *aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1108 | Pgno nextPage; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1109 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1110 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1111 | assert( pCur!=0 && pCur->pPage!=0 ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1112 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
| 1113 | aPayload = pCur->pPage->apCell[pCur->idx]->aPayload; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1114 | if( offset<MX_LOCAL_PAYLOAD ){ |
| 1115 | int a = amt; |
| 1116 | if( a+offset>MX_LOCAL_PAYLOAD ){ |
| 1117 | a = MX_LOCAL_PAYLOAD - offset; |
| 1118 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1119 | memcpy(zBuf, &aPayload[offset], a); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1120 | if( a==amt ){ |
| 1121 | return SQLITE_OK; |
| 1122 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1123 | offset = 0; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1124 | zBuf += a; |
| 1125 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1126 | }else{ |
| 1127 | offset -= MX_LOCAL_PAYLOAD; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1128 | } |
| 1129 | if( amt>0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1130 | nextPage = SWAB32(pBt, pCur->pPage->apCell[pCur->idx]->ovfl); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1131 | } |
| 1132 | while( amt>0 && nextPage ){ |
| 1133 | OverflowPage *pOvfl; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1134 | rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1135 | if( rc!=0 ){ |
| 1136 | return rc; |
| 1137 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1138 | nextPage = SWAB32(pBt, pOvfl->iNext); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1139 | if( offset<OVERFLOW_SIZE ){ |
| 1140 | int a = amt; |
| 1141 | if( a + offset > OVERFLOW_SIZE ){ |
| 1142 | a = OVERFLOW_SIZE - offset; |
| 1143 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1144 | memcpy(zBuf, &pOvfl->aPayload[offset], a); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1145 | offset = 0; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1146 | amt -= a; |
| 1147 | zBuf += a; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1148 | }else{ |
| 1149 | offset -= OVERFLOW_SIZE; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1150 | } |
| 1151 | sqlitepager_unref(pOvfl); |
| 1152 | } |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 1153 | if( amt>0 ){ |
| 1154 | return SQLITE_CORRUPT; |
| 1155 | } |
| 1156 | return SQLITE_OK; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1159 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1160 | ** Read part of the key associated with cursor pCur. A maximum |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1161 | ** of "amt" bytes will be transfered into zBuf[]. The transfer |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1162 | ** begins at "offset". The number of bytes actually read is |
| 1163 | ** returned. The amount returned will be smaller than the |
| 1164 | ** amount requested if there are not enough bytes in the key |
| 1165 | ** to satisfy the request. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1166 | */ |
| 1167 | int sqliteBtreeKey(BtCursor *pCur, int offset, int amt, char *zBuf){ |
| 1168 | Cell *pCell; |
| 1169 | MemPage *pPage; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1170 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1171 | if( amt<0 ) return 0; |
| 1172 | if( offset<0 ) return 0; |
| 1173 | if( amt==0 ) return 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1174 | pPage = pCur->pPage; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1175 | if( pPage==0 ) return 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1176 | if( pCur->idx >= pPage->nCell ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1177 | return 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1178 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1179 | pCell = pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1180 | if( amt+offset > NKEY(pCur->pBt, pCell->h) ){ |
| 1181 | amt = NKEY(pCur->pBt, pCell->h) - offset; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1182 | if( amt<=0 ){ |
| 1183 | return 0; |
| 1184 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1185 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1186 | getPayload(pCur, offset, amt, zBuf); |
| 1187 | return amt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1191 | ** Set *pSize to the number of bytes of data in the entry the |
| 1192 | ** cursor currently points to. Always return SQLITE_OK. |
| 1193 | ** Failure is not possible. If the cursor is not currently |
| 1194 | ** pointing to an entry (which can happen, for example, if |
| 1195 | ** the database is empty) then *pSize is set to 0. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1196 | */ |
| 1197 | int sqliteBtreeDataSize(BtCursor *pCur, int *pSize){ |
| 1198 | Cell *pCell; |
| 1199 | MemPage *pPage; |
| 1200 | |
| 1201 | pPage = pCur->pPage; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1202 | if( pPage==0 || pCur->idx >= pPage->nCell ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1203 | *pSize = 0; |
| 1204 | }else{ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1205 | pCell = pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1206 | *pSize = NDATA(pCur->pBt, pCell->h); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1207 | } |
| 1208 | return SQLITE_OK; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1212 | ** Read part of the data associated with cursor pCur. A maximum |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1213 | ** of "amt" bytes will be transfered into zBuf[]. The transfer |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1214 | ** begins at "offset". The number of bytes actually read is |
| 1215 | ** returned. The amount returned will be smaller than the |
| 1216 | ** amount requested if there are not enough bytes in the data |
| 1217 | ** to satisfy the request. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1218 | */ |
| 1219 | int sqliteBtreeData(BtCursor *pCur, int offset, int amt, char *zBuf){ |
| 1220 | Cell *pCell; |
| 1221 | MemPage *pPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1222 | int nData; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1223 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1224 | if( amt<0 ) return 0; |
| 1225 | if( offset<0 ) return 0; |
| 1226 | if( amt==0 ) return 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1227 | pPage = pCur->pPage; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1228 | if( pPage==0 || pCur->idx >= pPage->nCell ){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1229 | return 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1230 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1231 | pCell = pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1232 | nData = NDATA(pCur->pBt, pCell->h); |
| 1233 | if( amt+offset > nData ){ |
| 1234 | amt = nData - offset; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1235 | if( amt<=0 ){ |
| 1236 | return 0; |
| 1237 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1238 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1239 | getPayload(pCur, offset + NKEY(pCur->pBt, pCell->h), amt, zBuf); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1240 | return amt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1241 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1242 | |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1243 | /* |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1244 | ** Compare an external key against the key on the entry that pCur points to. |
| 1245 | ** |
| 1246 | ** The external key is pKey and is nKey bytes long. The last nIgnore bytes |
| 1247 | ** of the key associated with pCur are ignored, as if they do not exist. |
| 1248 | ** (The normal case is for nIgnore to be zero in which case the entire |
| 1249 | ** internal key is used in the comparison.) |
| 1250 | ** |
| 1251 | ** The comparison result is written to *pRes as follows: |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1252 | ** |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1253 | ** *pRes<0 This means pCur<pKey |
| 1254 | ** |
| 1255 | ** *pRes==0 This means pCur==pKey for all nKey bytes |
| 1256 | ** |
| 1257 | ** *pRes>0 This means pCur>pKey |
| 1258 | ** |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1259 | ** When one key is an exact prefix of the other, the shorter key is |
| 1260 | ** considered less than the longer one. In order to be equal the |
| 1261 | ** keys must be exactly the same length. (The length of the pCur key |
| 1262 | ** is the actual key length minus nIgnore bytes.) |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1263 | */ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1264 | int sqliteBtreeKeyCompare( |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1265 | BtCursor *pCur, /* Pointer to entry to compare against */ |
| 1266 | const void *pKey, /* Key to compare against entry that pCur points to */ |
| 1267 | int nKey, /* Number of bytes in pKey */ |
| 1268 | int nIgnore, /* Ignore this many bytes at the end of pCur */ |
| 1269 | int *pResult /* Write the result here */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1270 | ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1271 | Pgno nextPage; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1272 | int n, c, rc, nLocal; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1273 | Cell *pCell; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1274 | Btree *pBt = pCur->pBt; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1275 | const char *zKey = (const char*)pKey; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1276 | |
| 1277 | assert( pCur->pPage ); |
| 1278 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1279 | pCell = pCur->pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1280 | nLocal = NKEY(pBt, pCell->h) - nIgnore; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1281 | if( nLocal<0 ) nLocal = 0; |
| 1282 | n = nKey<nLocal ? nKey : nLocal; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1283 | if( n>MX_LOCAL_PAYLOAD ){ |
| 1284 | n = MX_LOCAL_PAYLOAD; |
| 1285 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1286 | c = memcmp(pCell->aPayload, zKey, n); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1287 | if( c!=0 ){ |
| 1288 | *pResult = c; |
| 1289 | return SQLITE_OK; |
| 1290 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1291 | zKey += n; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1292 | nKey -= n; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1293 | nLocal -= n; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1294 | nextPage = SWAB32(pBt, pCell->ovfl); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1295 | while( nKey>0 && nLocal>0 ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1296 | OverflowPage *pOvfl; |
| 1297 | if( nextPage==0 ){ |
| 1298 | return SQLITE_CORRUPT; |
| 1299 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1300 | rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1301 | if( rc ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1302 | return rc; |
| 1303 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1304 | nextPage = SWAB32(pBt, pOvfl->iNext); |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1305 | n = nKey<nLocal ? nKey : nLocal; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1306 | if( n>OVERFLOW_SIZE ){ |
| 1307 | n = OVERFLOW_SIZE; |
| 1308 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1309 | c = memcmp(pOvfl->aPayload, zKey, n); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1310 | sqlitepager_unref(pOvfl); |
| 1311 | if( c!=0 ){ |
| 1312 | *pResult = c; |
| 1313 | return SQLITE_OK; |
| 1314 | } |
| 1315 | nKey -= n; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1316 | nLocal -= n; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1317 | zKey += n; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1318 | } |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1319 | if( c==0 ){ |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1320 | c = nLocal - nKey; |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 1321 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1322 | *pResult = c; |
| 1323 | return SQLITE_OK; |
| 1324 | } |
| 1325 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1326 | /* |
| 1327 | ** Move the cursor down to a new child page. |
| 1328 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1329 | static int moveToChild(BtCursor *pCur, int newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1330 | int rc; |
| 1331 | MemPage *pNewPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1332 | Btree *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1333 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1334 | rc = sqlitepager_get(pBt->pPager, newPgno, (void**)&pNewPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1335 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1336 | rc = initPage(pBt, pNewPage, newPgno, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1337 | if( rc ) return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1338 | sqlitepager_unref(pCur->pPage); |
| 1339 | pCur->pPage = pNewPage; |
| 1340 | pCur->idx = 0; |
| 1341 | return SQLITE_OK; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1345 | ** Move the cursor up to the parent page. |
| 1346 | ** |
| 1347 | ** pCur->idx is set to the cell index that contains the pointer |
| 1348 | ** to the page we are coming from. If we are coming from the |
| 1349 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1350 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1351 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1352 | static int moveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1353 | Pgno oldPgno; |
| 1354 | MemPage *pParent; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1355 | int i; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1356 | pParent = pCur->pPage->pParent; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1357 | if( pParent==0 ) return SQLITE_INTERNAL; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1358 | oldPgno = sqlitepager_pagenumber(pCur->pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1359 | sqlitepager_ref(pParent); |
| 1360 | sqlitepager_unref(pCur->pPage); |
| 1361 | pCur->pPage = pParent; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1362 | pCur->idx = pParent->nCell; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1363 | oldPgno = SWAB32(pCur->pBt, oldPgno); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1364 | for(i=0; i<pParent->nCell; i++){ |
| 1365 | if( pParent->apCell[i]->h.leftChild==oldPgno ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1366 | pCur->idx = i; |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1370 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | ** Move the cursor to the root page |
| 1375 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1376 | static int moveToRoot(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1377 | MemPage *pNew; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1378 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1379 | Btree *pBt = pCur->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1380 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1381 | rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pNew); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1382 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1383 | rc = initPage(pBt, pNew, pCur->pgnoRoot, 0); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1384 | if( rc ) return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1385 | sqlitepager_unref(pCur->pPage); |
| 1386 | pCur->pPage = pNew; |
| 1387 | pCur->idx = 0; |
| 1388 | return SQLITE_OK; |
| 1389 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 1390 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1391 | /* |
| 1392 | ** Move the cursor down to the left-most leaf entry beneath the |
| 1393 | ** entry to which it is currently pointing. |
| 1394 | */ |
| 1395 | static int moveToLeftmost(BtCursor *pCur){ |
| 1396 | Pgno pgno; |
| 1397 | int rc; |
| 1398 | |
| 1399 | while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1400 | rc = moveToChild(pCur, SWAB32(pCur->pBt, pgno)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1401 | if( rc ) return rc; |
| 1402 | } |
| 1403 | return SQLITE_OK; |
| 1404 | } |
| 1405 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1406 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 1407 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1408 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1409 | */ |
| 1410 | int sqliteBtreeFirst(BtCursor *pCur, int *pRes){ |
| 1411 | int rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1412 | if( pCur->pPage==0 ) return SQLITE_ABORT; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1413 | rc = moveToRoot(pCur); |
| 1414 | if( rc ) return rc; |
| 1415 | if( pCur->pPage->nCell==0 ){ |
| 1416 | *pRes = 1; |
| 1417 | return SQLITE_OK; |
| 1418 | } |
| 1419 | *pRes = 0; |
| 1420 | rc = moveToLeftmost(pCur); |
drh | 0ce92ed | 2001-12-15 02:47:28 +0000 | [diff] [blame] | 1421 | pCur->bSkipNext = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1422 | return rc; |
| 1423 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1424 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1425 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 1426 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 1427 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1428 | */ |
| 1429 | int sqliteBtreeLast(BtCursor *pCur, int *pRes){ |
| 1430 | int rc; |
| 1431 | Pgno pgno; |
| 1432 | if( pCur->pPage==0 ) return SQLITE_ABORT; |
| 1433 | rc = moveToRoot(pCur); |
| 1434 | if( rc ) return rc; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 1435 | assert( pCur->pPage->isInit ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1436 | if( pCur->pPage->nCell==0 ){ |
| 1437 | *pRes = 1; |
| 1438 | return SQLITE_OK; |
| 1439 | } |
| 1440 | *pRes = 0; |
| 1441 | while( (pgno = pCur->pPage->u.hdr.rightChild)!=0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1442 | rc = moveToChild(pCur, SWAB32(pCur->pBt, pgno)); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 1443 | if( rc ) return rc; |
| 1444 | } |
| 1445 | pCur->idx = pCur->pPage->nCell-1; |
| 1446 | pCur->bSkipNext = 0; |
| 1447 | return rc; |
| 1448 | } |
| 1449 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1450 | /* Move the cursor so that it points to an entry near pKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1451 | ** Return a success code. |
| 1452 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1453 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1454 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1455 | ** were present. The cursor might point to an entry that comes |
| 1456 | ** before or after the key. |
| 1457 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1458 | ** The result of comparing the key with the entry to which the |
| 1459 | ** cursor is left pointing is stored in pCur->iMatch. The same |
| 1460 | ** value is also written to *pRes if pRes!=NULL. The meaning of |
| 1461 | ** this value is as follows: |
| 1462 | ** |
| 1463 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1464 | ** is smaller than pKey. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1465 | ** |
| 1466 | ** *pRes==0 The cursor is left pointing at an entry that |
| 1467 | ** exactly matches pKey. |
| 1468 | ** |
| 1469 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1470 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1471 | */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1472 | int sqliteBtreeMoveto(BtCursor *pCur, const void *pKey, int nKey, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1473 | int rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1474 | if( pCur->pPage==0 ) return SQLITE_ABORT; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1475 | pCur->bSkipNext = 0; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1476 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1477 | if( rc ) return rc; |
| 1478 | for(;;){ |
| 1479 | int lwr, upr; |
| 1480 | Pgno chldPg; |
| 1481 | MemPage *pPage = pCur->pPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1482 | int c = -1; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1483 | lwr = 0; |
| 1484 | upr = pPage->nCell-1; |
| 1485 | while( lwr<=upr ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1486 | pCur->idx = (lwr+upr)/2; |
drh | 8721ce4 | 2001-11-07 14:22:00 +0000 | [diff] [blame] | 1487 | rc = sqliteBtreeKeyCompare(pCur, pKey, nKey, 0, &c); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1488 | if( rc ) return rc; |
| 1489 | if( c==0 ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1490 | pCur->iMatch = c; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1491 | if( pRes ) *pRes = 0; |
| 1492 | return SQLITE_OK; |
| 1493 | } |
| 1494 | if( c<0 ){ |
| 1495 | lwr = pCur->idx+1; |
| 1496 | }else{ |
| 1497 | upr = pCur->idx-1; |
| 1498 | } |
| 1499 | } |
| 1500 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 1501 | assert( pPage->isInit ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1502 | if( lwr>=pPage->nCell ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1503 | chldPg = pPage->u.hdr.rightChild; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1504 | }else{ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1505 | chldPg = pPage->apCell[lwr]->h.leftChild; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1506 | } |
| 1507 | if( chldPg==0 ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1508 | pCur->iMatch = c; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1509 | if( pRes ) *pRes = c; |
| 1510 | return SQLITE_OK; |
| 1511 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1512 | rc = moveToChild(pCur, SWAB32(pCur->pBt, chldPg)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1513 | if( rc ) return rc; |
| 1514 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1515 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1519 | ** Advance the cursor to the next entry in the database. If |
| 1520 | ** successful and pRes!=NULL then set *pRes=0. If the cursor |
| 1521 | ** was already pointing to the last entry in the database before |
| 1522 | ** this routine was called, then set *pRes=1 if pRes!=NULL. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1523 | */ |
| 1524 | int sqliteBtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1525 | int rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1526 | if( pCur->pPage==0 ){ |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1527 | if( pRes ) *pRes = 1; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 1528 | return SQLITE_ABORT; |
| 1529 | } |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 1530 | assert( pCur->pPage->isInit ); |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame] | 1531 | if( pCur->bSkipNext && pCur->idx<pCur->pPage->nCell ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1532 | pCur->bSkipNext = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1533 | if( pRes ) *pRes = 0; |
| 1534 | return SQLITE_OK; |
| 1535 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1536 | pCur->idx++; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1537 | if( pCur->idx>=pCur->pPage->nCell ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1538 | if( pCur->pPage->u.hdr.rightChild ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1539 | rc = moveToChild(pCur, SWAB32(pCur->pBt, pCur->pPage->u.hdr.rightChild)); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1540 | if( rc ) return rc; |
| 1541 | rc = moveToLeftmost(pCur); |
| 1542 | if( rc ) return rc; |
| 1543 | if( pRes ) *pRes = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1544 | return SQLITE_OK; |
| 1545 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1546 | do{ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1547 | if( pCur->pPage->pParent==0 ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1548 | if( pRes ) *pRes = 1; |
| 1549 | return SQLITE_OK; |
| 1550 | } |
| 1551 | rc = moveToParent(pCur); |
| 1552 | if( rc ) return rc; |
| 1553 | }while( pCur->idx>=pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1554 | if( pRes ) *pRes = 0; |
| 1555 | return SQLITE_OK; |
| 1556 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1557 | rc = moveToLeftmost(pCur); |
| 1558 | if( rc ) return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1559 | if( pRes ) *pRes = 0; |
| 1560 | return SQLITE_OK; |
| 1561 | } |
| 1562 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1563 | /* |
| 1564 | ** Allocate a new page from the database file. |
| 1565 | ** |
| 1566 | ** The new page is marked as dirty. (In other words, sqlitepager_write() |
| 1567 | ** has already been called on the new page.) The new page has also |
| 1568 | ** been referenced and the calling routine is responsible for calling |
| 1569 | ** sqlitepager_unref() on the new page when it is done. |
| 1570 | ** |
| 1571 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 1572 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
| 1573 | ** Do not invoke sqlitepager_unref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 1574 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 1575 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 1576 | ** 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] | 1577 | ** attempt to keep related pages close to each other in the database file, |
| 1578 | ** which in turn can make database access faster. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1579 | */ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 1580 | static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1581 | PageOne *pPage1 = pBt->page1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1582 | int rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1583 | if( pPage1->freeList ){ |
| 1584 | OverflowPage *pOvfl; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1585 | FreelistInfo *pInfo; |
| 1586 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1587 | rc = sqlitepager_write(pPage1); |
| 1588 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1589 | SWAB_ADD(pBt, pPage1->nFree, -1); |
| 1590 | rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList), |
| 1591 | (void**)&pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1592 | if( rc ) return rc; |
| 1593 | rc = sqlitepager_write(pOvfl); |
| 1594 | if( rc ){ |
| 1595 | sqlitepager_unref(pOvfl); |
| 1596 | return rc; |
| 1597 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1598 | pInfo = (FreelistInfo*)pOvfl->aPayload; |
| 1599 | if( pInfo->nFree==0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1600 | *pPgno = SWAB32(pBt, pPage1->freeList); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1601 | pPage1->freeList = pOvfl->iNext; |
| 1602 | *ppPage = (MemPage*)pOvfl; |
| 1603 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1604 | int closest, n; |
| 1605 | n = SWAB32(pBt, pInfo->nFree); |
| 1606 | if( n>1 && nearby>0 ){ |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 1607 | int i, dist; |
| 1608 | closest = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1609 | dist = SWAB32(pBt, pInfo->aFree[0]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 1610 | if( dist<0 ) dist = -dist; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1611 | for(i=1; i<n; i++){ |
| 1612 | int d2 = SWAB32(pBt, pInfo->aFree[i]) - nearby; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 1613 | if( d2<0 ) d2 = -d2; |
| 1614 | if( d2<dist ) closest = i; |
| 1615 | } |
| 1616 | }else{ |
| 1617 | closest = 0; |
| 1618 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1619 | SWAB_ADD(pBt, pInfo->nFree, -1); |
| 1620 | *pPgno = SWAB32(pBt, pInfo->aFree[closest]); |
| 1621 | pInfo->aFree[closest] = pInfo->aFree[n-1]; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1622 | rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage); |
| 1623 | sqlitepager_unref(pOvfl); |
| 1624 | if( rc==SQLITE_OK ){ |
| 1625 | sqlitepager_dont_rollback(*ppPage); |
| 1626 | rc = sqlitepager_write(*ppPage); |
| 1627 | } |
| 1628 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1629 | }else{ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1630 | *pPgno = sqlitepager_pagecount(pBt->pPager) + 1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1631 | rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1632 | if( rc ) return rc; |
| 1633 | rc = sqlitepager_write(*ppPage); |
| 1634 | } |
| 1635 | return rc; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | ** Add a page of the database file to the freelist. Either pgno or |
| 1640 | ** pPage but not both may be 0. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1641 | ** |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1642 | ** sqlitepager_unref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1643 | */ |
| 1644 | static int freePage(Btree *pBt, void *pPage, Pgno pgno){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1645 | PageOne *pPage1 = pBt->page1; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1646 | OverflowPage *pOvfl = (OverflowPage*)pPage; |
| 1647 | int rc; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1648 | int needUnref = 0; |
| 1649 | MemPage *pMemPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1650 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1651 | if( pgno==0 ){ |
| 1652 | assert( pOvfl!=0 ); |
| 1653 | pgno = sqlitepager_pagenumber(pOvfl); |
| 1654 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 1655 | assert( pgno>2 ); |
drh | 193a6b4 | 2002-07-07 16:52:46 +0000 | [diff] [blame] | 1656 | pMemPage = (MemPage*)pPage; |
| 1657 | pMemPage->isInit = 0; |
| 1658 | if( pMemPage->pParent ){ |
| 1659 | sqlitepager_unref(pMemPage->pParent); |
| 1660 | pMemPage->pParent = 0; |
| 1661 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1662 | rc = sqlitepager_write(pPage1); |
| 1663 | if( rc ){ |
| 1664 | return rc; |
| 1665 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1666 | SWAB_ADD(pBt, pPage1->nFree, 1); |
| 1667 | if( pPage1->nFree!=0 && pPage1->freeList!=0 ){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1668 | OverflowPage *pFreeIdx; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1669 | rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList), |
| 1670 | (void**)&pFreeIdx); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1671 | if( rc==SQLITE_OK ){ |
| 1672 | FreelistInfo *pInfo = (FreelistInfo*)pFreeIdx->aPayload; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1673 | int n = SWAB32(pBt, pInfo->nFree); |
| 1674 | if( n<(sizeof(pInfo->aFree)/sizeof(pInfo->aFree[0])) ){ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1675 | rc = sqlitepager_write(pFreeIdx); |
| 1676 | if( rc==SQLITE_OK ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1677 | pInfo->aFree[n] = SWAB32(pBt, pgno); |
| 1678 | SWAB_ADD(pBt, pInfo->nFree, 1); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 1679 | sqlitepager_unref(pFreeIdx); |
| 1680 | sqlitepager_dont_write(pBt->pPager, pgno); |
| 1681 | return rc; |
| 1682 | } |
| 1683 | } |
| 1684 | sqlitepager_unref(pFreeIdx); |
| 1685 | } |
| 1686 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1687 | if( pOvfl==0 ){ |
| 1688 | assert( pgno>0 ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1689 | rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1690 | if( rc ) return rc; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1691 | needUnref = 1; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1692 | } |
| 1693 | rc = sqlitepager_write(pOvfl); |
| 1694 | if( rc ){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1695 | if( needUnref ) sqlitepager_unref(pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1696 | return rc; |
| 1697 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1698 | pOvfl->iNext = pPage1->freeList; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1699 | pPage1->freeList = SWAB32(pBt, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1700 | memset(pOvfl->aPayload, 0, OVERFLOW_SIZE); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1701 | if( needUnref ) rc = sqlitepager_unref(pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1702 | return rc; |
| 1703 | } |
| 1704 | |
| 1705 | /* |
| 1706 | ** Erase all the data out of a cell. This involves returning overflow |
| 1707 | ** pages back the freelist. |
| 1708 | */ |
| 1709 | static int clearCell(Btree *pBt, Cell *pCell){ |
| 1710 | Pager *pPager = pBt->pPager; |
| 1711 | OverflowPage *pOvfl; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1712 | Pgno ovfl, nextOvfl; |
| 1713 | int rc; |
| 1714 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1715 | if( NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h) <= MX_LOCAL_PAYLOAD ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1716 | return SQLITE_OK; |
| 1717 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1718 | ovfl = SWAB32(pBt, pCell->ovfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1719 | pCell->ovfl = 0; |
| 1720 | while( ovfl ){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1721 | rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1722 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1723 | nextOvfl = SWAB32(pBt, pOvfl->iNext); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1724 | rc = freePage(pBt, pOvfl, ovfl); |
| 1725 | if( rc ) return rc; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1726 | sqlitepager_unref(pOvfl); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1727 | ovfl = nextOvfl; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1728 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1729 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | ** Create a new cell from key and data. Overflow pages are allocated as |
| 1734 | ** necessary and linked to this cell. |
| 1735 | */ |
| 1736 | static int fillInCell( |
| 1737 | Btree *pBt, /* The whole Btree. Needed to allocate pages */ |
| 1738 | Cell *pCell, /* Populate this Cell structure */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1739 | const void *pKey, int nKey, /* The key */ |
| 1740 | const void *pData,int nData /* The data */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1741 | ){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1742 | OverflowPage *pOvfl, *pPrior; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1743 | Pgno *pNext; |
| 1744 | int spaceLeft; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1745 | int n, rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1746 | int nPayload; |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 1747 | const char *pPayload; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1748 | char *pSpace; |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 1749 | Pgno nearby = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1750 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1751 | pCell->h.leftChild = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1752 | pCell->h.nKey = SWAB16(pBt, nKey & 0xffff); |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 1753 | pCell->h.nKeyHi = nKey >> 16; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1754 | pCell->h.nData = SWAB16(pBt, nData & 0xffff); |
drh | 80ff32f | 2001-11-04 18:32:46 +0000 | [diff] [blame] | 1755 | pCell->h.nDataHi = nData >> 16; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1756 | pCell->h.iNext = 0; |
| 1757 | |
| 1758 | pNext = &pCell->ovfl; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1759 | pSpace = pCell->aPayload; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1760 | spaceLeft = MX_LOCAL_PAYLOAD; |
| 1761 | pPayload = pKey; |
| 1762 | pKey = 0; |
| 1763 | nPayload = nKey; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1764 | pPrior = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1765 | while( nPayload>0 ){ |
| 1766 | if( spaceLeft==0 ){ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 1767 | rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext, nearby); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1768 | if( rc ){ |
| 1769 | *pNext = 0; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 1770 | }else{ |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 1771 | nearby = *pNext; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1772 | } |
| 1773 | if( pPrior ) sqlitepager_unref(pPrior); |
| 1774 | if( rc ){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1775 | clearCell(pBt, pCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1776 | return rc; |
| 1777 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1778 | if( pBt->needSwab ) *pNext = swab32(*pNext); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1779 | pPrior = pOvfl; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1780 | spaceLeft = OVERFLOW_SIZE; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 1781 | pSpace = pOvfl->aPayload; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1782 | pNext = &pOvfl->iNext; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1783 | } |
| 1784 | n = nPayload; |
| 1785 | if( n>spaceLeft ) n = spaceLeft; |
| 1786 | memcpy(pSpace, pPayload, n); |
| 1787 | nPayload -= n; |
| 1788 | if( nPayload==0 && pData ){ |
| 1789 | pPayload = pData; |
| 1790 | nPayload = nData; |
| 1791 | pData = 0; |
| 1792 | }else{ |
| 1793 | pPayload += n; |
| 1794 | } |
| 1795 | spaceLeft -= n; |
| 1796 | pSpace += n; |
| 1797 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1798 | *pNext = 0; |
| 1799 | if( pPrior ){ |
| 1800 | sqlitepager_unref(pPrior); |
| 1801 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 1802 | return SQLITE_OK; |
| 1803 | } |
| 1804 | |
| 1805 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1806 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1807 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1808 | ** pointer in the third argument. |
| 1809 | */ |
| 1810 | static void reparentPage(Pager *pPager, Pgno pgno, MemPage *pNewParent){ |
| 1811 | MemPage *pThis; |
| 1812 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1813 | if( pgno==0 ) return; |
| 1814 | assert( pPager!=0 ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1815 | pThis = sqlitepager_lookup(pPager, pgno); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1816 | if( pThis && pThis->isInit ){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1817 | if( pThis->pParent!=pNewParent ){ |
| 1818 | if( pThis->pParent ) sqlitepager_unref(pThis->pParent); |
| 1819 | pThis->pParent = pNewParent; |
| 1820 | if( pNewParent ) sqlitepager_ref(pNewParent); |
| 1821 | } |
| 1822 | sqlitepager_unref(pThis); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | /* |
| 1827 | ** Reparent all children of the given page to be the given page. |
| 1828 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1829 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1830 | ** |
| 1831 | ** This routine gets called after you memcpy() one page into |
| 1832 | ** another. |
| 1833 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1834 | static void reparentChildPages(Btree *pBt, MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1835 | int i; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1836 | Pager *pPager = pBt->pPager; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1837 | for(i=0; i<pPage->nCell; i++){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1838 | reparentPage(pPager, SWAB32(pBt, pPage->apCell[i]->h.leftChild), pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1839 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1840 | reparentPage(pPager, SWAB32(pBt, pPage->u.hdr.rightChild), pPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 1845 | ** The cell content is not freed or deallocated. It is assumed that |
| 1846 | ** the cell content has been copied someplace else. This routine just |
| 1847 | ** removes the reference to the cell from pPage. |
| 1848 | ** |
| 1849 | ** "sz" must be the number of bytes in the cell. |
| 1850 | ** |
| 1851 | ** Do not bother maintaining the integrity of the linked list of Cells. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1852 | ** Only the pPage->apCell[] array is important. The relinkCellList() |
| 1853 | ** routine will be called soon after this routine in order to rebuild |
| 1854 | ** the linked list. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1855 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1856 | static void dropCell(Btree *pBt, MemPage *pPage, int idx, int sz){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1857 | int j; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1858 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1859 | assert( sz==cellSize(pBt, pPage->apCell[idx]) ); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1860 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1861 | freeSpace(pBt, pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1862 | for(j=idx; j<pPage->nCell-1; j++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1863 | pPage->apCell[j] = pPage->apCell[j+1]; |
| 1864 | } |
| 1865 | pPage->nCell--; |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 1870 | ** content of the cell. |
| 1871 | ** |
| 1872 | ** If the cell content will fit on the page, then put it there. If it |
| 1873 | ** will not fit, then just make pPage->apCell[i] point to the content |
| 1874 | ** and set pPage->isOverfull. |
| 1875 | ** |
| 1876 | ** Do not bother maintaining the integrity of the linked list of Cells. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1877 | ** Only the pPage->apCell[] array is important. The relinkCellList() |
| 1878 | ** routine will be called soon after this routine in order to rebuild |
| 1879 | ** the linked list. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1880 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1881 | static void insertCell(Btree *pBt, MemPage *pPage, int i, Cell *pCell, int sz){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1882 | int idx, j; |
| 1883 | assert( i>=0 && i<=pPage->nCell ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1884 | assert( sz==cellSize(pBt, pCell) ); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1885 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1886 | idx = allocateSpace(pBt, pPage, sz); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1887 | for(j=pPage->nCell; j>i; j--){ |
| 1888 | pPage->apCell[j] = pPage->apCell[j-1]; |
| 1889 | } |
| 1890 | pPage->nCell++; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1891 | if( idx<=0 ){ |
| 1892 | pPage->isOverfull = 1; |
| 1893 | pPage->apCell[i] = pCell; |
| 1894 | }else{ |
| 1895 | memcpy(&pPage->u.aDisk[idx], pCell, sz); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1896 | pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | ** Rebuild the linked list of cells on a page so that the cells |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1902 | ** occur in the order specified by the pPage->apCell[] array. |
| 1903 | ** Invoke this routine once to repair damage after one or more |
| 1904 | ** invocations of either insertCell() or dropCell(). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1905 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1906 | static void relinkCellList(Btree *pBt, MemPage *pPage){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1907 | int i; |
| 1908 | u16 *pIdx; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1909 | assert( sqlitepager_iswriteable(pPage) ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1910 | pIdx = &pPage->u.hdr.firstCell; |
| 1911 | for(i=0; i<pPage->nCell; i++){ |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1912 | int idx = Addr(pPage->apCell[i]) - Addr(pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1913 | assert( idx>0 && idx<SQLITE_PAGE_SIZE ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 1914 | *pIdx = SWAB16(pBt, idx); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1915 | pIdx = &pPage->apCell[i]->h.iNext; |
| 1916 | } |
| 1917 | *pIdx = 0; |
| 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | ** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[] |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1922 | ** pointers that point into pFrom->u.aDisk[] must be adjusted to point |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1923 | ** into pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1924 | ** not point to pFrom->u.aDisk[]. Those are unchanged. |
| 1925 | */ |
| 1926 | static void copyPage(MemPage *pTo, MemPage *pFrom){ |
| 1927 | uptr from, to; |
| 1928 | int i; |
| 1929 | memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_PAGE_SIZE); |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1930 | pTo->pParent = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1931 | pTo->isInit = 1; |
| 1932 | pTo->nCell = pFrom->nCell; |
| 1933 | pTo->nFree = pFrom->nFree; |
| 1934 | pTo->isOverfull = pFrom->isOverfull; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1935 | to = Addr(pTo); |
| 1936 | from = Addr(pFrom); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1937 | for(i=0; i<pTo->nCell; i++){ |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 1938 | uptr x = Addr(pFrom->apCell[i]); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1939 | if( x>from && x<from+SQLITE_PAGE_SIZE ){ |
| 1940 | *((uptr*)&pTo->apCell[i]) = x + to - from; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1941 | }else{ |
| 1942 | pTo->apCell[i] = pFrom->apCell[i]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1948 | ** This routine redistributes Cells on pPage and up to two siblings |
| 1949 | ** of pPage so that all pages have about the same amount of free space. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1950 | ** Usually one sibling on either side of pPage is used in the balancing, |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1951 | ** though both siblings might come from one side if pPage is the first |
| 1952 | ** or last child of its parent. If pPage has fewer than two siblings |
| 1953 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1954 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1955 | ** |
| 1956 | ** The number of siblings of pPage might be increased or decreased by |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1957 | ** one in an effort to keep pages between 66% and 100% full. The root page |
| 1958 | ** is special and is allowed to be less than 66% full. If pPage is |
| 1959 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1960 | ** or decreased by one, as necessary, to keep the root page from being |
| 1961 | ** overfull or empty. |
| 1962 | ** |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1963 | ** This routine calls relinkCellList() on its input page regardless of |
| 1964 | ** whether or not it does any real balancing. Client routines will typically |
| 1965 | ** invoke insertCell() or dropCell() before calling this routine, so we |
| 1966 | ** need to call relinkCellList() to clean up the mess that those other |
| 1967 | ** routines left behind. |
| 1968 | ** |
| 1969 | ** pCur is left pointing to the same cell as when this routine was called |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1970 | ** even if that cell gets moved to a different page. pCur may be NULL. |
| 1971 | ** Set the pCur parameter to NULL if you do not care about keeping track |
| 1972 | ** of a cell as that will save this routine the work of keeping track of it. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1973 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1974 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1975 | ** might not actually be stored in pPage->u.aDisk[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1976 | ** if the page is overfull. Part of the job of this routine is to |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1977 | ** make sure all Cells for pPage once again fit in pPage->u.aDisk[]. |
| 1978 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1979 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 1980 | ** might become overfull or underfull. If that happens, then this routine |
| 1981 | ** is called recursively on the parent. |
| 1982 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1983 | ** If this routine fails for any reason, it might leave the database |
| 1984 | ** in a corrupted state. So if this routine fails, the database should |
| 1985 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1986 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1987 | static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1988 | MemPage *pParent; /* The parent of pPage */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1989 | MemPage *apOld[3]; /* pPage and up to two siblings */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1990 | Pgno pgnoOld[3]; /* Page numbers for each page in apOld[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1991 | MemPage *apNew[4]; /* pPage and up to 3 siblings after balancing */ |
| 1992 | Pgno pgnoNew[4]; /* Page numbers for each page in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1993 | int idxDiv[3]; /* Indices of divider cells in pParent */ |
| 1994 | Cell *apDiv[3]; /* Divider cells in pParent */ |
| 1995 | int nCell; /* Number of cells in apCell[] */ |
| 1996 | int nOld; /* Number of pages in apOld[] */ |
| 1997 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1998 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 1999 | int i, j, k; /* Loop counters */ |
| 2000 | int idx; /* Index of pPage in pParent->apCell[] */ |
| 2001 | int nxDiv; /* Next divider slot in pParent->apCell[] */ |
| 2002 | int rc; /* The return code */ |
| 2003 | int iCur; /* apCell[iCur] is the cell of the cursor */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2004 | MemPage *pOldCurPage; /* The cursor originally points to this page */ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2005 | int totalSize; /* Total bytes for all cells */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2006 | int subtotal; /* Subtotal of bytes in cells on one page */ |
| 2007 | int cntNew[4]; /* Index in apCell[] of cell after i-th page */ |
| 2008 | int szNew[4]; /* Combined size of cells place on i-th page */ |
drh | 9ca7d3b | 2001-06-28 11:50:21 +0000 | [diff] [blame] | 2009 | MemPage *extraUnref = 0; /* A page that needs to be unref-ed */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2010 | Pgno pgno, swabPgno; /* Page number */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2011 | Cell *apCell[MX_CELL*3+5]; /* All cells from pages being balanceed */ |
| 2012 | int szCell[MX_CELL*3+5]; /* Local size of all cells */ |
| 2013 | Cell aTemp[2]; /* Temporary holding area for apDiv[] */ |
| 2014 | MemPage aOld[3]; /* Temporary copies of pPage and its siblings */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2015 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2016 | /* |
| 2017 | ** Return without doing any work if pPage is neither overfull nor |
| 2018 | ** underfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2019 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2020 | assert( sqlitepager_iswriteable(pPage) ); |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 2021 | if( !pPage->isOverfull && pPage->nFree<SQLITE_PAGE_SIZE/2 |
| 2022 | && pPage->nCell>=2){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2023 | relinkCellList(pBt, pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2024 | return SQLITE_OK; |
| 2025 | } |
| 2026 | |
| 2027 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2028 | ** Find the parent of the page to be balanceed. |
| 2029 | ** If there is no parent, it means this page is the root page and |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2030 | ** special rules apply. |
| 2031 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2032 | pParent = pPage->pParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2033 | if( pParent==0 ){ |
| 2034 | Pgno pgnoChild; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2035 | MemPage *pChild; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2036 | assert( pPage->isInit ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2037 | if( pPage->nCell==0 ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2038 | if( pPage->u.hdr.rightChild ){ |
| 2039 | /* |
| 2040 | ** The root page is empty. Copy the one child page |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2041 | ** into the root page and return. This reduces the depth |
| 2042 | ** of the BTree by one. |
| 2043 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2044 | pgnoChild = SWAB32(pBt, pPage->u.hdr.rightChild); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2045 | rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2046 | if( rc ) return rc; |
| 2047 | memcpy(pPage, pChild, SQLITE_PAGE_SIZE); |
| 2048 | pPage->isInit = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2049 | rc = initPage(pBt, pPage, sqlitepager_pagenumber(pPage), 0); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2050 | assert( rc==SQLITE_OK ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2051 | reparentChildPages(pBt, pPage); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2052 | if( pCur && pCur->pPage==pChild ){ |
| 2053 | sqlitepager_unref(pChild); |
| 2054 | pCur->pPage = pPage; |
| 2055 | sqlitepager_ref(pPage); |
| 2056 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2057 | freePage(pBt, pChild, pgnoChild); |
| 2058 | sqlitepager_unref(pChild); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 2059 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2060 | relinkCellList(pBt, pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2061 | } |
| 2062 | return SQLITE_OK; |
| 2063 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2064 | if( !pPage->isOverfull ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2065 | /* It is OK for the root page to be less than half full. |
| 2066 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2067 | relinkCellList(pBt, pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2068 | return SQLITE_OK; |
| 2069 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2070 | /* |
| 2071 | ** If we get to here, it means the root page is overfull. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2072 | ** When this happens, Create a new child page and copy the |
| 2073 | ** contents of the root into the child. Then make the root |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2074 | ** page an empty page with rightChild pointing to the new |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2075 | ** child. Then fall thru to the code below which will cause |
| 2076 | ** the overfull child page to be split. |
| 2077 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2078 | rc = sqlitepager_write(pPage); |
| 2079 | if( rc ) return rc; |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2080 | rc = allocatePage(pBt, &pChild, &pgnoChild, sqlitepager_pagenumber(pPage)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2081 | if( rc ) return rc; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2082 | assert( sqlitepager_iswriteable(pChild) ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2083 | copyPage(pChild, pPage); |
| 2084 | pChild->pParent = pPage; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2085 | sqlitepager_ref(pPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2086 | pChild->isOverfull = 1; |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2087 | if( pCur && pCur->pPage==pPage ){ |
| 2088 | sqlitepager_unref(pPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2089 | pCur->pPage = pChild; |
drh | 9ca7d3b | 2001-06-28 11:50:21 +0000 | [diff] [blame] | 2090 | }else{ |
| 2091 | extraUnref = pChild; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2092 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2093 | zeroPage(pBt, pPage); |
| 2094 | pPage->u.hdr.rightChild = SWAB32(pBt, pgnoChild); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2095 | pParent = pPage; |
| 2096 | pPage = pChild; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2097 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2098 | rc = sqlitepager_write(pParent); |
| 2099 | if( rc ) return rc; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2100 | assert( pParent->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2101 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2102 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2103 | ** Find the Cell in the parent page whose h.leftChild points back |
| 2104 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 2105 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2106 | */ |
| 2107 | idx = -1; |
| 2108 | pgno = sqlitepager_pagenumber(pPage); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2109 | swabPgno = SWAB32(pBt, pgno); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2110 | for(i=0; i<pParent->nCell; i++){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2111 | if( pParent->apCell[i]->h.leftChild==swabPgno ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2112 | idx = i; |
| 2113 | break; |
| 2114 | } |
| 2115 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2116 | if( idx<0 && pParent->u.hdr.rightChild==swabPgno ){ |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2117 | idx = pParent->nCell; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2118 | } |
| 2119 | if( idx<0 ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2120 | return SQLITE_CORRUPT; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2124 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2125 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2126 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2127 | nOld = nNew = 0; |
| 2128 | sqlitepager_ref(pParent); |
| 2129 | |
| 2130 | /* |
| 2131 | ** Find sibling pages to pPage and the Cells in pParent that divide |
| 2132 | ** the siblings. An attempt is made to find one sibling on either |
| 2133 | ** side of pPage. Both siblings are taken from one side, however, if |
| 2134 | ** pPage is either the first or last child of its parent. If pParent |
| 2135 | ** has 3 or fewer children then all children of pParent are taken. |
| 2136 | */ |
| 2137 | if( idx==pParent->nCell ){ |
| 2138 | nxDiv = idx - 2; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2139 | }else{ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2140 | nxDiv = idx - 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2141 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2142 | if( nxDiv<0 ) nxDiv = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2143 | nDiv = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2144 | for(i=0, k=nxDiv; i<3; i++, k++){ |
| 2145 | if( k<pParent->nCell ){ |
| 2146 | idxDiv[i] = k; |
| 2147 | apDiv[i] = pParent->apCell[k]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2148 | nDiv++; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2149 | pgnoOld[i] = SWAB32(pBt, apDiv[i]->h.leftChild); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2150 | }else if( k==pParent->nCell ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2151 | pgnoOld[i] = SWAB32(pBt, pParent->u.hdr.rightChild); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2152 | }else{ |
| 2153 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2154 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2155 | rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2156 | if( rc ) goto balance_cleanup; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2157 | rc = initPage(pBt, apOld[i], pgnoOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2158 | if( rc ) goto balance_cleanup; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2159 | nOld++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2163 | ** Set iCur to be the index in apCell[] of the cell that the cursor |
| 2164 | ** is pointing to. We will need this later on in order to keep the |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2165 | ** cursor pointing at the same cell. If pCur points to a page that |
| 2166 | ** has no involvement with this rebalancing, then set iCur to a large |
| 2167 | ** number so that the iCur==j tests always fail in the main cell |
| 2168 | ** distribution loop below. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2169 | */ |
| 2170 | if( pCur ){ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2171 | iCur = 0; |
| 2172 | for(i=0; i<nOld; i++){ |
| 2173 | if( pCur->pPage==apOld[i] ){ |
| 2174 | iCur += pCur->idx; |
| 2175 | break; |
| 2176 | } |
| 2177 | iCur += apOld[i]->nCell; |
| 2178 | if( i<nOld-1 && pCur->pPage==pParent && pCur->idx==idxDiv[i] ){ |
| 2179 | break; |
| 2180 | } |
| 2181 | iCur++; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2182 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2183 | pOldCurPage = pCur->pPage; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 2188 | ** The rest of this function will use data from the copies rather |
| 2189 | ** that the original pages since the original pages will be in the |
| 2190 | ** process of being overwritten. |
| 2191 | */ |
| 2192 | for(i=0; i<nOld; i++){ |
| 2193 | copyPage(&aOld[i], apOld[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | ** Load pointers to all cells on sibling pages and the divider cells |
| 2198 | ** into the local apCell[] array. Make copies of the divider cells |
| 2199 | ** into aTemp[] and remove the the divider Cells from pParent. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2200 | */ |
| 2201 | nCell = 0; |
| 2202 | for(i=0; i<nOld; i++){ |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2203 | MemPage *pOld = &aOld[i]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2204 | for(j=0; j<pOld->nCell; j++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2205 | apCell[nCell] = pOld->apCell[j]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2206 | szCell[nCell] = cellSize(pBt, apCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2207 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2208 | } |
| 2209 | if( i<nOld-1 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2210 | szCell[nCell] = cellSize(pBt, apDiv[i]); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2211 | memcpy(&aTemp[i], apDiv[i], szCell[nCell]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2212 | apCell[nCell] = &aTemp[i]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2213 | dropCell(pBt, pParent, nxDiv, szCell[nCell]); |
| 2214 | assert( SWAB32(pBt, apCell[nCell]->h.leftChild)==pgnoOld[i] ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2215 | apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild; |
| 2216 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2221 | ** Figure out the number of pages needed to hold all nCell cells. |
| 2222 | ** Store this number in "k". Also compute szNew[] which is the total |
| 2223 | ** size of all cells on the i-th page and cntNew[] which is the index |
| 2224 | ** in apCell[] of the cell that divides path i from path i+1. |
| 2225 | ** cntNew[k] should equal nCell. |
| 2226 | ** |
| 2227 | ** This little patch of code is critical for keeping the tree |
| 2228 | ** balanced. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2229 | */ |
| 2230 | totalSize = 0; |
| 2231 | for(i=0; i<nCell; i++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2232 | totalSize += szCell[i]; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2233 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2234 | for(subtotal=k=i=0; i<nCell; i++){ |
| 2235 | subtotal += szCell[i]; |
| 2236 | if( subtotal > USABLE_SPACE ){ |
| 2237 | szNew[k] = subtotal - szCell[i]; |
| 2238 | cntNew[k] = i; |
| 2239 | subtotal = 0; |
| 2240 | k++; |
| 2241 | } |
| 2242 | } |
| 2243 | szNew[k] = subtotal; |
| 2244 | cntNew[k] = nCell; |
| 2245 | k++; |
| 2246 | for(i=k-1; i>0; i--){ |
| 2247 | while( szNew[i]<USABLE_SPACE/2 ){ |
| 2248 | cntNew[i-1]--; |
| 2249 | assert( cntNew[i-1]>0 ); |
| 2250 | szNew[i] += szCell[cntNew[i-1]]; |
| 2251 | szNew[i-1] -= szCell[cntNew[i-1]-1]; |
| 2252 | } |
| 2253 | } |
| 2254 | assert( cntNew[0]>0 ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2255 | |
| 2256 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2257 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2258 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2259 | for(i=0; i<k; i++){ |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2260 | if( i<nOld ){ |
| 2261 | apNew[i] = apOld[i]; |
| 2262 | pgnoNew[i] = pgnoOld[i]; |
| 2263 | apOld[i] = 0; |
| 2264 | sqlitepager_write(apNew[i]); |
| 2265 | }else{ |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2266 | rc = allocatePage(pBt, &apNew[i], &pgnoNew[i], pgnoNew[i-1]); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2267 | if( rc ) goto balance_cleanup; |
| 2268 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2269 | nNew++; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2270 | zeroPage(pBt, apNew[i]); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2271 | apNew[i]->isInit = 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2274 | /* Free any old pages that were not reused as new pages. |
| 2275 | */ |
| 2276 | while( i<nOld ){ |
| 2277 | rc = freePage(pBt, apOld[i], pgnoOld[i]); |
| 2278 | if( rc ) goto balance_cleanup; |
| 2279 | sqlitepager_unref(apOld[i]); |
| 2280 | apOld[i] = 0; |
| 2281 | i++; |
| 2282 | } |
| 2283 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2284 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 2285 | ** Put the new pages in accending order. This helps to |
| 2286 | ** keep entries in the disk file in order so that a scan |
| 2287 | ** of the table is a linear scan through the file. That |
| 2288 | ** in turn helps the operating system to deliver pages |
| 2289 | ** from the disk more rapidly. |
| 2290 | ** |
| 2291 | ** An O(n^2) insertion sort algorithm is used, but since |
| 2292 | ** n is never more than 3, that should not be a problem. |
| 2293 | ** |
| 2294 | ** This one optimization makes the database about 25% |
| 2295 | ** faster for large insertions and deletions. |
| 2296 | */ |
| 2297 | for(i=0; i<k-1; i++){ |
| 2298 | int minV = pgnoNew[i]; |
| 2299 | int minI = i; |
| 2300 | for(j=i+1; j<k; j++){ |
| 2301 | if( pgnoNew[j]<minV ){ |
| 2302 | minI = j; |
| 2303 | minV = pgnoNew[j]; |
| 2304 | } |
| 2305 | } |
| 2306 | if( minI>i ){ |
| 2307 | int t; |
| 2308 | MemPage *pT; |
| 2309 | t = pgnoNew[i]; |
| 2310 | pT = apNew[i]; |
| 2311 | pgnoNew[i] = pgnoNew[minI]; |
| 2312 | apNew[i] = apNew[minI]; |
| 2313 | pgnoNew[minI] = t; |
| 2314 | apNew[minI] = pT; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2319 | ** Evenly distribute the data in apCell[] across the new pages. |
| 2320 | ** Insert divider cells into pParent as necessary. |
| 2321 | */ |
| 2322 | j = 0; |
| 2323 | for(i=0; i<nNew; i++){ |
| 2324 | MemPage *pNew = apNew[i]; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2325 | while( j<cntNew[i] ){ |
| 2326 | assert( pNew->nFree>=szCell[j] ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2327 | if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2328 | insertCell(pBt, pNew, pNew->nCell, apCell[j], szCell[j]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2329 | j++; |
| 2330 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2331 | assert( pNew->nCell>0 ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2332 | assert( !pNew->isOverfull ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2333 | relinkCellList(pBt, pNew); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2334 | if( i<nNew-1 && j<nCell ){ |
| 2335 | pNew->u.hdr.rightChild = apCell[j]->h.leftChild; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2336 | apCell[j]->h.leftChild = SWAB32(pBt, pgnoNew[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2337 | if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2338 | insertCell(pBt, pParent, nxDiv, apCell[j], szCell[j]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2339 | j++; |
| 2340 | nxDiv++; |
| 2341 | } |
| 2342 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2343 | assert( j==nCell ); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2344 | apNew[nNew-1]->u.hdr.rightChild = aOld[nOld-1].u.hdr.rightChild; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2345 | if( nxDiv==pParent->nCell ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2346 | pParent->u.hdr.rightChild = SWAB32(pBt, pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2347 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2348 | pParent->apCell[nxDiv]->h.leftChild = SWAB32(pBt, pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2349 | } |
| 2350 | if( pCur ){ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2351 | if( j<=iCur && pCur->pPage==pParent && pCur->idx>idxDiv[nOld-1] ){ |
| 2352 | assert( pCur->pPage==pOldCurPage ); |
| 2353 | pCur->idx += nNew - nOld; |
| 2354 | }else{ |
| 2355 | assert( pOldCurPage!=0 ); |
| 2356 | sqlitepager_ref(pCur->pPage); |
| 2357 | sqlitepager_unref(pOldCurPage); |
| 2358 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | /* |
| 2362 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2363 | */ |
| 2364 | for(i=0; i<nNew; i++){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2365 | reparentChildPages(pBt, apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2366 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2367 | reparentChildPages(pBt, pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2368 | |
| 2369 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2370 | ** balance the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2371 | */ |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2372 | rc = balance(pBt, pParent, pCur); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2373 | |
| 2374 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2375 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2376 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2377 | balance_cleanup: |
drh | 9ca7d3b | 2001-06-28 11:50:21 +0000 | [diff] [blame] | 2378 | if( extraUnref ){ |
| 2379 | sqlitepager_unref(extraUnref); |
| 2380 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2381 | for(i=0; i<nOld; i++){ |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 2382 | if( apOld[i]!=0 && apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2383 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2384 | for(i=0; i<nNew; i++){ |
| 2385 | sqlitepager_unref(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2386 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2387 | if( pCur && pCur->pPage==0 ){ |
| 2388 | pCur->pPage = pParent; |
| 2389 | pCur->idx = 0; |
| 2390 | }else{ |
| 2391 | sqlitepager_unref(pParent); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2392 | } |
| 2393 | return rc; |
| 2394 | } |
| 2395 | |
| 2396 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2397 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 2398 | ** and the data is given by (pData,nData). The cursor is used only to |
| 2399 | ** define what database the record should be inserted into. The cursor |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2400 | ** is left pointing at the new record. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2401 | */ |
| 2402 | int sqliteBtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2403 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 2404 | const void *pKey, int nKey, /* The key of the new record */ |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 2405 | const void *pData, int nData /* The data of the new record */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2406 | ){ |
| 2407 | Cell newCell; |
| 2408 | int rc; |
| 2409 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2410 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2411 | MemPage *pPage; |
| 2412 | Btree *pBt = pCur->pBt; |
| 2413 | |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2414 | if( pCur->pPage==0 ){ |
| 2415 | return SQLITE_ABORT; /* A rollback destroyed this cursor */ |
| 2416 | } |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2417 | if( !pCur->pBt->inTrans || nKey+nData==0 ){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2418 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2419 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2420 | if( !pCur->wrFlag ){ |
| 2421 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 2422 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2423 | rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2424 | if( rc ) return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2425 | pPage = pCur->pPage; |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2426 | assert( pPage->isInit ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2427 | rc = sqlitepager_write(pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2428 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2429 | rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData); |
| 2430 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2431 | szNew = cellSize(pBt, &newCell); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2432 | if( loc==0 ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2433 | newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild; |
| 2434 | rc = clearCell(pBt, pPage->apCell[pCur->idx]); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2435 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2436 | dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pPage->apCell[pCur->idx])); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2437 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2438 | assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */ |
| 2439 | pCur->idx++; |
| 2440 | }else{ |
| 2441 | assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2442 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2443 | insertCell(pBt, pPage, pCur->idx, &newCell, szNew); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2444 | rc = balance(pCur->pBt, pPage, pCur); |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 2445 | /* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
| 2446 | /* fflush(stdout); */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2447 | return rc; |
| 2448 | } |
| 2449 | |
| 2450 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2451 | ** Delete the entry that the cursor is pointing to. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2452 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2453 | ** The cursor is left pointing at either the next or the previous |
| 2454 | ** entry. If the cursor is left pointing to the next entry, then |
| 2455 | ** the pCur->bSkipNext flag is set which forces the next call to |
| 2456 | ** sqliteBtreeNext() to be a no-op. That way, you can always call |
| 2457 | ** sqliteBtreeNext() after a delete and the cursor will be left |
| 2458 | ** pointing to the first entry after the deleted entry. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2459 | */ |
| 2460 | int sqliteBtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2461 | MemPage *pPage = pCur->pPage; |
| 2462 | Cell *pCell; |
| 2463 | int rc; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2464 | Pgno pgnoChild; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2465 | Btree *pBt = pCur->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2466 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2467 | assert( pPage->isInit ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2468 | if( pCur->pPage==0 ){ |
| 2469 | return SQLITE_ABORT; /* A rollback destroyed this cursor */ |
| 2470 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2471 | if( !pCur->pBt->inTrans ){ |
| 2472 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2473 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2474 | if( pCur->idx >= pPage->nCell ){ |
| 2475 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 2476 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2477 | if( !pCur->wrFlag ){ |
| 2478 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 2479 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2480 | rc = sqlitepager_write(pPage); |
| 2481 | if( rc ) return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2482 | pCell = pPage->apCell[pCur->idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2483 | pgnoChild = SWAB32(pBt, pCell->h.leftChild); |
| 2484 | clearCell(pBt, pCell); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2485 | if( pgnoChild ){ |
| 2486 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2487 | ** 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] | 2488 | ** do something we will leave a hole on an internal page. |
| 2489 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 2490 | ** next Cell after the one to be deleted is guaranteed to exist and |
| 2491 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2492 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2493 | BtCursor leafCur; |
| 2494 | Cell *pNext; |
| 2495 | int szNext; |
| 2496 | getTempCursor(pCur, &leafCur); |
| 2497 | rc = sqliteBtreeNext(&leafCur, 0); |
| 2498 | if( rc!=SQLITE_OK ){ |
| 2499 | return SQLITE_CORRUPT; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2500 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2501 | rc = sqlitepager_write(leafCur.pPage); |
| 2502 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2503 | dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell)); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2504 | pNext = leafCur.pPage->apCell[leafCur.idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2505 | szNext = cellSize(pBt, pNext); |
| 2506 | pNext->h.leftChild = SWAB32(pBt, pgnoChild); |
| 2507 | insertCell(pBt, pPage, pCur->idx, pNext, szNext); |
| 2508 | rc = balance(pBt, pPage, pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2509 | if( rc ) return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2510 | pCur->bSkipNext = 1; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2511 | dropCell(pBt, leafCur.pPage, leafCur.idx, szNext); |
| 2512 | rc = balance(pBt, leafCur.pPage, pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2513 | releaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2514 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2515 | dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell)); |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 2516 | if( pCur->idx>=pPage->nCell ){ |
| 2517 | pCur->idx = pPage->nCell-1; |
drh | f5bf0a7 | 2001-11-23 00:24:12 +0000 | [diff] [blame] | 2518 | if( pCur->idx<0 ){ |
| 2519 | pCur->idx = 0; |
| 2520 | pCur->bSkipNext = 1; |
| 2521 | }else{ |
| 2522 | pCur->bSkipNext = 0; |
| 2523 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2524 | }else{ |
| 2525 | pCur->bSkipNext = 1; |
| 2526 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2527 | rc = balance(pBt, pPage, pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2528 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2529 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 2530 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2531 | |
| 2532 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2533 | ** Create a new BTree table. Write into *piTable the page |
| 2534 | ** number for the root page of the new table. |
| 2535 | ** |
| 2536 | ** In the current implementation, BTree tables and BTree indices are the |
| 2537 | ** the same. But in the future, we may change this so that BTree tables |
| 2538 | ** are restricted to having a 4-byte integer key and arbitrary data and |
| 2539 | ** BTree indices are restricted to having an arbitrary key and no data. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2540 | */ |
| 2541 | int sqliteBtreeCreateTable(Btree *pBt, int *piTable){ |
| 2542 | MemPage *pRoot; |
| 2543 | Pgno pgnoRoot; |
| 2544 | int rc; |
| 2545 | if( !pBt->inTrans ){ |
| 2546 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2547 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 2548 | if( pBt->readOnly ){ |
| 2549 | return SQLITE_READONLY; |
| 2550 | } |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 2551 | rc = allocatePage(pBt, &pRoot, &pgnoRoot, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2552 | if( rc ) return rc; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2553 | assert( sqlitepager_iswriteable(pRoot) ); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2554 | zeroPage(pBt, pRoot); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2555 | sqlitepager_unref(pRoot); |
| 2556 | *piTable = (int)pgnoRoot; |
| 2557 | return SQLITE_OK; |
| 2558 | } |
| 2559 | |
| 2560 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2561 | ** Create a new BTree index. Write into *piTable the page |
| 2562 | ** number for the root page of the new index. |
| 2563 | ** |
| 2564 | ** In the current implementation, BTree tables and BTree indices are the |
| 2565 | ** the same. But in the future, we may change this so that BTree tables |
| 2566 | ** are restricted to having a 4-byte integer key and arbitrary data and |
| 2567 | ** BTree indices are restricted to having an arbitrary key and no data. |
| 2568 | */ |
| 2569 | int sqliteBtreeCreateIndex(Btree *pBt, int *piIndex){ |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 2570 | return sqliteBtreeCreateTable(pBt, piIndex); |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2574 | ** Erase the given database page and all its children. Return |
| 2575 | ** the page to the freelist. |
| 2576 | */ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2577 | static int clearDatabasePage(Btree *pBt, Pgno pgno, int freePageFlag){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2578 | MemPage *pPage; |
| 2579 | int rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2580 | Cell *pCell; |
| 2581 | int idx; |
| 2582 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2583 | rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2584 | if( rc ) return rc; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2585 | rc = sqlitepager_write(pPage); |
| 2586 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2587 | rc = initPage(pBt, pPage, pgno, 0); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 2588 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2589 | idx = SWAB16(pBt, pPage->u.hdr.firstCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2590 | while( idx>0 ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 2591 | pCell = (Cell*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2592 | idx = SWAB16(pBt, pCell->h.iNext); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2593 | if( pCell->h.leftChild ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2594 | rc = clearDatabasePage(pBt, SWAB32(pBt, pCell->h.leftChild), 1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2595 | if( rc ) return rc; |
| 2596 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2597 | rc = clearCell(pBt, pCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2598 | if( rc ) return rc; |
| 2599 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2600 | if( pPage->u.hdr.rightChild ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2601 | rc = clearDatabasePage(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2602 | if( rc ) return rc; |
| 2603 | } |
| 2604 | if( freePageFlag ){ |
| 2605 | rc = freePage(pBt, pPage, pgno); |
| 2606 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2607 | zeroPage(pBt, pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2608 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2609 | sqlitepager_unref(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2610 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | /* |
| 2614 | ** Delete all information from a single table in the database. |
| 2615 | */ |
| 2616 | int sqliteBtreeClearTable(Btree *pBt, int iTable){ |
| 2617 | int rc; |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 2618 | ptr nLock; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2619 | if( !pBt->inTrans ){ |
| 2620 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2621 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 2622 | if( pBt->readOnly ){ |
| 2623 | return SQLITE_READONLY; |
| 2624 | } |
drh | 5a2c2c2 | 2001-11-21 02:21:11 +0000 | [diff] [blame] | 2625 | nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2626 | if( nLock ){ |
| 2627 | return SQLITE_LOCKED; |
| 2628 | } |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2629 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2630 | if( rc ){ |
| 2631 | sqliteBtreeRollback(pBt); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2632 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2633 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* |
| 2637 | ** Erase all information in a table and add the root of the table to |
| 2638 | ** the freelist. Except, the root of the principle table (the one on |
| 2639 | ** page 2) is never added to the freelist. |
| 2640 | */ |
| 2641 | int sqliteBtreeDropTable(Btree *pBt, int iTable){ |
| 2642 | int rc; |
| 2643 | MemPage *pPage; |
| 2644 | if( !pBt->inTrans ){ |
| 2645 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2646 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 2647 | if( pBt->readOnly ){ |
| 2648 | return SQLITE_READONLY; |
| 2649 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2650 | rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2651 | if( rc ) return rc; |
| 2652 | rc = sqliteBtreeClearTable(pBt, iTable); |
| 2653 | if( rc ) return rc; |
| 2654 | if( iTable>2 ){ |
| 2655 | rc = freePage(pBt, pPage, iTable); |
| 2656 | }else{ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2657 | zeroPage(pBt, pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2658 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2659 | sqlitepager_unref(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2660 | return rc; |
| 2661 | } |
| 2662 | |
| 2663 | /* |
| 2664 | ** Read the meta-information out of a database file. |
| 2665 | */ |
| 2666 | int sqliteBtreeGetMeta(Btree *pBt, int *aMeta){ |
| 2667 | PageOne *pP1; |
| 2668 | int rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2669 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2670 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2671 | rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2672 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2673 | aMeta[0] = SWAB32(pBt, pP1->nFree); |
| 2674 | for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){ |
| 2675 | aMeta[i+1] = SWAB32(pBt, pP1->aMeta[i]); |
| 2676 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2677 | sqlitepager_unref(pP1); |
| 2678 | return SQLITE_OK; |
| 2679 | } |
| 2680 | |
| 2681 | /* |
| 2682 | ** Write meta-information back into the database. |
| 2683 | */ |
| 2684 | int sqliteBtreeUpdateMeta(Btree *pBt, int *aMeta){ |
| 2685 | PageOne *pP1; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2686 | int rc, i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2687 | if( !pBt->inTrans ){ |
| 2688 | return SQLITE_ERROR; /* Must start a transaction first */ |
| 2689 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 2690 | if( pBt->readOnly ){ |
| 2691 | return SQLITE_READONLY; |
| 2692 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2693 | pP1 = pBt->page1; |
| 2694 | rc = sqlitepager_write(pP1); |
drh | 9adf9ac | 2002-05-15 11:44:13 +0000 | [diff] [blame] | 2695 | if( rc ) return rc; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2696 | for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){ |
| 2697 | pP1->aMeta[i] = SWAB32(pBt, aMeta[i+1]); |
| 2698 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2699 | return SQLITE_OK; |
| 2700 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2701 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2702 | /****************************************************************************** |
| 2703 | ** The complete implementation of the BTree subsystem is above this line. |
| 2704 | ** All the code the follows is for testing and troubleshooting the BTree |
| 2705 | ** subsystem. None of the code that follows is used during normal operation. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2706 | ******************************************************************************/ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2707 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2708 | /* |
| 2709 | ** Print a disassembly of the given page on standard output. This routine |
| 2710 | ** is used for debugging and testing only. |
| 2711 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2712 | #ifdef SQLITE_TEST |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2713 | int sqliteBtreePageDump(Btree *pBt, int pgno, int recursive){ |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2714 | int rc; |
| 2715 | MemPage *pPage; |
| 2716 | int i, j; |
| 2717 | int nFree; |
| 2718 | u16 idx; |
| 2719 | char range[20]; |
| 2720 | unsigned char payload[20]; |
| 2721 | rc = sqlitepager_get(pBt->pPager, (Pgno)pgno, (void**)&pPage); |
| 2722 | if( rc ){ |
| 2723 | return rc; |
| 2724 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2725 | if( recursive ) printf("PAGE %d:\n", pgno); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2726 | i = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2727 | idx = SWAB16(pBt, pPage->u.hdr.firstCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2728 | while( idx>0 && idx<=SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){ |
| 2729 | Cell *pCell = (Cell*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2730 | int sz = cellSize(pBt, pCell); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2731 | sprintf(range,"%d..%d", idx, idx+sz-1); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2732 | sz = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2733 | if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1; |
| 2734 | memcpy(payload, pCell->aPayload, sz); |
| 2735 | for(j=0; j<sz; j++){ |
| 2736 | if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.'; |
| 2737 | } |
| 2738 | payload[sz] = 0; |
| 2739 | printf( |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2740 | "cell %2d: i=%-10s chld=%-4d nk=%-4d nd=%-4d payload=%s\n", |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2741 | i, range, (int)pCell->h.leftChild, |
| 2742 | NKEY(pBt, pCell->h), NDATA(pBt, pCell->h), |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2743 | payload |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2744 | ); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2745 | if( pPage->isInit && pPage->apCell[i]!=pCell ){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2746 | printf("**** apCell[%d] does not match on prior entry ****\n", i); |
| 2747 | } |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 2748 | i++; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2749 | idx = SWAB16(pBt, pCell->h.iNext); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2750 | } |
| 2751 | if( idx!=0 ){ |
| 2752 | printf("ERROR: next cell index out of range: %d\n", idx); |
| 2753 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2754 | printf("right_child: %d\n", SWAB32(pBt, pPage->u.hdr.rightChild)); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2755 | nFree = 0; |
| 2756 | i = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2757 | idx = SWAB16(pBt, pPage->u.hdr.firstFree); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2758 | while( idx>0 && idx<SQLITE_PAGE_SIZE ){ |
| 2759 | FreeBlk *p = (FreeBlk*)&pPage->u.aDisk[idx]; |
| 2760 | sprintf(range,"%d..%d", idx, idx+p->iSize-1); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2761 | nFree += SWAB16(pBt, p->iSize); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2762 | printf("freeblock %2d: i=%-10s size=%-4d total=%d\n", |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2763 | i, range, SWAB16(pBt, p->iSize), nFree); |
| 2764 | idx = SWAB16(pBt, p->iNext); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2765 | i++; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2766 | } |
| 2767 | if( idx!=0 ){ |
| 2768 | printf("ERROR: next freeblock index out of range: %d\n", idx); |
| 2769 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2770 | if( recursive && pPage->u.hdr.rightChild!=0 ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2771 | idx = SWAB16(pBt, pPage->u.hdr.firstCell); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2772 | while( idx>0 && idx<SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){ |
| 2773 | Cell *pCell = (Cell*)&pPage->u.aDisk[idx]; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2774 | sqliteBtreePageDump(pBt, SWAB32(pBt, pCell->h.leftChild), 1); |
| 2775 | idx = SWAB16(pBt, pCell->h.iNext); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2776 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2777 | sqliteBtreePageDump(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2778 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2779 | sqlitepager_unref(pPage); |
| 2780 | return SQLITE_OK; |
| 2781 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2782 | #endif |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2783 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2784 | #ifdef SQLITE_TEST |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2785 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2786 | ** Fill aResult[] with information about the entry and page that the |
| 2787 | ** cursor is pointing to. |
| 2788 | ** |
| 2789 | ** aResult[0] = The page number |
| 2790 | ** aResult[1] = The entry number |
| 2791 | ** aResult[2] = Total number of entries on this page |
| 2792 | ** aResult[3] = Size of this entry |
| 2793 | ** aResult[4] = Number of free bytes on this page |
| 2794 | ** aResult[5] = Number of free blocks on the page |
| 2795 | ** aResult[6] = Page number of the left child of this entry |
| 2796 | ** aResult[7] = Page number of the right child for the whole page |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2797 | ** |
| 2798 | ** This routine is used for testing and debugging only. |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2799 | */ |
| 2800 | int sqliteBtreeCursorDump(BtCursor *pCur, int *aResult){ |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2801 | int cnt, idx; |
| 2802 | MemPage *pPage = pCur->pPage; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2803 | Btree *pBt = pCur->pBt; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2804 | aResult[0] = sqlitepager_pagenumber(pPage); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2805 | aResult[1] = pCur->idx; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2806 | aResult[2] = pPage->nCell; |
| 2807 | if( pCur->idx>=0 && pCur->idx<pPage->nCell ){ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2808 | aResult[3] = cellSize(pBt, pPage->apCell[pCur->idx]); |
| 2809 | aResult[6] = SWAB32(pBt, pPage->apCell[pCur->idx]->h.leftChild); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2810 | }else{ |
| 2811 | aResult[3] = 0; |
| 2812 | aResult[6] = 0; |
| 2813 | } |
| 2814 | aResult[4] = pPage->nFree; |
| 2815 | cnt = 0; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2816 | idx = SWAB16(pBt, pPage->u.hdr.firstFree); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2817 | while( idx>0 && idx<SQLITE_PAGE_SIZE ){ |
| 2818 | cnt++; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2819 | idx = SWAB16(pBt, ((FreeBlk*)&pPage->u.aDisk[idx])->iNext); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2820 | } |
| 2821 | aResult[5] = cnt; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2822 | aResult[7] = SWAB32(pBt, pPage->u.hdr.rightChild); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2823 | return SQLITE_OK; |
| 2824 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2825 | #endif |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2826 | |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2827 | #ifdef SQLITE_TEST |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2828 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2829 | ** Return the pager associated with a BTree. This routine is used for |
| 2830 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2831 | */ |
| 2832 | Pager *sqliteBtreePager(Btree *pBt){ |
| 2833 | return pBt->pPager; |
| 2834 | } |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2835 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2836 | |
| 2837 | /* |
| 2838 | ** This structure is passed around through all the sanity checking routines |
| 2839 | ** in order to keep track of some global state information. |
| 2840 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2841 | typedef struct IntegrityCk IntegrityCk; |
| 2842 | struct IntegrityCk { |
drh | 100569d | 2001-10-02 13:01:48 +0000 | [diff] [blame] | 2843 | Btree *pBt; /* The tree being checked out */ |
| 2844 | Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */ |
| 2845 | int nPage; /* Number of pages in the database */ |
| 2846 | int *anRef; /* Number of times each page is referenced */ |
| 2847 | int nTreePage; /* Number of BTree pages */ |
| 2848 | int nByte; /* Number of bytes of data stored on BTree pages */ |
| 2849 | char *zErrMsg; /* An error message. NULL of no errors seen. */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2850 | }; |
| 2851 | |
| 2852 | /* |
| 2853 | ** Append a message to the error message string. |
| 2854 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2855 | static void checkAppendMsg(IntegrityCk *pCheck, char *zMsg1, char *zMsg2){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2856 | if( pCheck->zErrMsg ){ |
| 2857 | char *zOld = pCheck->zErrMsg; |
| 2858 | pCheck->zErrMsg = 0; |
| 2859 | sqliteSetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, 0); |
| 2860 | sqliteFree(zOld); |
| 2861 | }else{ |
| 2862 | sqliteSetString(&pCheck->zErrMsg, zMsg1, zMsg2, 0); |
| 2863 | } |
| 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | ** Add 1 to the reference count for page iPage. If this is the second |
| 2868 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 2869 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 2870 | ** if this is the first reference to the page. |
| 2871 | ** |
| 2872 | ** Also check that the page number is in bounds. |
| 2873 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2874 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2875 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 2876 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2877 | char zBuf[100]; |
| 2878 | sprintf(zBuf, "invalid page number %d", iPage); |
| 2879 | checkAppendMsg(pCheck, zContext, zBuf); |
| 2880 | return 1; |
| 2881 | } |
| 2882 | if( pCheck->anRef[iPage]==1 ){ |
| 2883 | char zBuf[100]; |
| 2884 | sprintf(zBuf, "2nd reference to page %d", iPage); |
| 2885 | checkAppendMsg(pCheck, zContext, zBuf); |
| 2886 | return 1; |
| 2887 | } |
| 2888 | return (pCheck->anRef[iPage]++)>1; |
| 2889 | } |
| 2890 | |
| 2891 | /* |
| 2892 | ** Check the integrity of the freelist or of an overflow page list. |
| 2893 | ** Verify that the number of pages on the list is N. |
| 2894 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2895 | static void checkList( |
| 2896 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 2897 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 2898 | int iPage, /* Page number for first page in the list */ |
| 2899 | int N, /* Expected number of pages in the list */ |
| 2900 | char *zContext /* Context for error messages */ |
| 2901 | ){ |
| 2902 | int i; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2903 | char zMsg[100]; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2904 | while( N-- > 0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2905 | OverflowPage *pOvfl; |
| 2906 | if( iPage<1 ){ |
| 2907 | sprintf(zMsg, "%d pages missing from overflow list", N+1); |
| 2908 | checkAppendMsg(pCheck, zContext, zMsg); |
| 2909 | break; |
| 2910 | } |
| 2911 | if( checkRef(pCheck, iPage, zContext) ) break; |
| 2912 | if( sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){ |
| 2913 | sprintf(zMsg, "failed to get page %d", iPage); |
| 2914 | checkAppendMsg(pCheck, zContext, zMsg); |
| 2915 | break; |
| 2916 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2917 | if( isFreeList ){ |
| 2918 | FreelistInfo *pInfo = (FreelistInfo*)pOvfl->aPayload; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2919 | int n = SWAB32(pCheck->pBt, pInfo->nFree); |
| 2920 | for(i=0; i<n; i++){ |
| 2921 | checkRef(pCheck, SWAB32(pCheck->pBt, pInfo->aFree[i]), zMsg); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2922 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2923 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 2924 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2925 | iPage = SWAB32(pCheck->pBt, pOvfl->iNext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2926 | sqlitepager_unref(pOvfl); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | /* |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2931 | ** Return negative if zKey1<zKey2. |
| 2932 | ** Return zero if zKey1==zKey2. |
| 2933 | ** Return positive if zKey1>zKey2. |
| 2934 | */ |
| 2935 | static int keyCompare( |
| 2936 | const char *zKey1, int nKey1, |
| 2937 | const char *zKey2, int nKey2 |
| 2938 | ){ |
| 2939 | int min = nKey1>nKey2 ? nKey2 : nKey1; |
| 2940 | int c = memcmp(zKey1, zKey2, min); |
| 2941 | if( c==0 ){ |
| 2942 | c = nKey1 - nKey2; |
| 2943 | } |
| 2944 | return c; |
| 2945 | } |
| 2946 | |
| 2947 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2948 | ** Do various sanity checks on a single page of a tree. Return |
| 2949 | ** the tree depth. Root pages return 0. Parents of root pages |
| 2950 | ** return 1, and so forth. |
| 2951 | ** |
| 2952 | ** These checks are done: |
| 2953 | ** |
| 2954 | ** 1. Make sure that cells and freeblocks do not overlap |
| 2955 | ** but combine to completely cover the page. |
| 2956 | ** 2. Make sure cell keys are in order. |
| 2957 | ** 3. Make sure no key is less than or equal to zLowerBound. |
| 2958 | ** 4. Make sure no key is greater than or equal to zUpperBound. |
| 2959 | ** 5. Check the integrity of overflow pages. |
| 2960 | ** 6. Recursively call checkTreePage on all children. |
| 2961 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2962 | ** 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] | 2963 | ** the root of the tree. |
| 2964 | */ |
| 2965 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 2966 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2967 | int iPage, /* Page number of the page to check */ |
| 2968 | MemPage *pParent, /* Parent page */ |
| 2969 | char *zParentContext, /* Parent context */ |
| 2970 | char *zLowerBound, /* All keys should be greater than this, if not NULL */ |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2971 | int nLower, /* Number of characters in zLowerBound */ |
| 2972 | char *zUpperBound, /* All keys should be less than this, if not NULL */ |
| 2973 | int nUpper /* Number of characters in zUpperBound */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2974 | ){ |
| 2975 | MemPage *pPage; |
| 2976 | int i, rc, depth, d2, pgno; |
| 2977 | char *zKey1, *zKey2; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 2978 | int nKey1, nKey2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2979 | BtCursor cur; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2980 | Btree *pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2981 | char zMsg[100]; |
| 2982 | char zContext[100]; |
| 2983 | char hit[SQLITE_PAGE_SIZE]; |
| 2984 | |
| 2985 | /* Check that the page exists |
| 2986 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2987 | cur.pBt = pBt = pCheck->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2988 | if( iPage==0 ) return 0; |
| 2989 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
| 2990 | sprintf(zContext, "On tree page %d: ", iPage); |
| 2991 | if( (rc = sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pPage))!=0 ){ |
| 2992 | sprintf(zMsg, "unable to get the page. error code=%d", rc); |
| 2993 | checkAppendMsg(pCheck, zContext, zMsg); |
| 2994 | return 0; |
| 2995 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 2996 | if( (rc = initPage(pBt, pPage, (Pgno)iPage, pParent))!=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 2997 | sprintf(zMsg, "initPage() returns error code %d", rc); |
| 2998 | checkAppendMsg(pCheck, zContext, zMsg); |
| 2999 | sqlitepager_unref(pPage); |
| 3000 | return 0; |
| 3001 | } |
| 3002 | |
| 3003 | /* Check out all the cells. |
| 3004 | */ |
| 3005 | depth = 0; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3006 | if( zLowerBound ){ |
| 3007 | zKey1 = sqliteMalloc( nLower+1 ); |
| 3008 | memcpy(zKey1, zLowerBound, nLower); |
| 3009 | zKey1[nLower] = 0; |
| 3010 | }else{ |
| 3011 | zKey1 = 0; |
| 3012 | } |
| 3013 | nKey1 = nLower; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3014 | cur.pPage = pPage; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3015 | for(i=0; i<pPage->nCell; i++){ |
| 3016 | Cell *pCell = pPage->apCell[i]; |
| 3017 | int sz; |
| 3018 | |
| 3019 | /* Check payload overflow pages |
| 3020 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3021 | nKey2 = NKEY(pBt, pCell->h); |
| 3022 | sz = nKey2 + NDATA(pBt, pCell->h); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3023 | sprintf(zContext, "On page %d cell %d: ", iPage, i); |
| 3024 | if( sz>MX_LOCAL_PAYLOAD ){ |
| 3025 | int nPage = (sz - MX_LOCAL_PAYLOAD + OVERFLOW_SIZE - 1)/OVERFLOW_SIZE; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3026 | checkList(pCheck, 0, SWAB32(pBt, pCell->ovfl), nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | /* Check that keys are in the right order |
| 3030 | */ |
| 3031 | cur.idx = i; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3032 | zKey2 = sqliteMalloc( nKey2+1 ); |
| 3033 | getPayload(&cur, 0, nKey2, zKey2); |
| 3034 | if( zKey1 && keyCompare(zKey1, nKey1, zKey2, nKey2)>=0 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3035 | checkAppendMsg(pCheck, zContext, "Key is out of order"); |
| 3036 | } |
| 3037 | |
| 3038 | /* Check sanity of left child page. |
| 3039 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3040 | pgno = SWAB32(pBt, pCell->h.leftChild); |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3041 | d2 = checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zKey2,nKey2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3042 | if( i>0 && d2!=depth ){ |
| 3043 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 3044 | } |
| 3045 | depth = d2; |
| 3046 | sqliteFree(zKey1); |
| 3047 | zKey1 = zKey2; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3048 | nKey1 = nKey2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3049 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3050 | pgno = SWAB32(pBt, pPage->u.hdr.rightChild); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3051 | sprintf(zContext, "On page %d at right child: ", iPage); |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3052 | checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zUpperBound,nUpper); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3053 | sqliteFree(zKey1); |
| 3054 | |
| 3055 | /* Check for complete coverage of the page |
| 3056 | */ |
| 3057 | memset(hit, 0, sizeof(hit)); |
| 3058 | memset(hit, 1, sizeof(PageHdr)); |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3059 | for(i=SWAB16(pBt, pPage->u.hdr.firstCell); i>0 && i<SQLITE_PAGE_SIZE; ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3060 | Cell *pCell = (Cell*)&pPage->u.aDisk[i]; |
| 3061 | int j; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3062 | for(j=i+cellSize(pBt, pCell)-1; j>=i; j--) hit[j]++; |
| 3063 | i = SWAB16(pBt, pCell->h.iNext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3064 | } |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3065 | for(i=SWAB16(pBt,pPage->u.hdr.firstFree); i>0 && i<SQLITE_PAGE_SIZE; ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3066 | FreeBlk *pFBlk = (FreeBlk*)&pPage->u.aDisk[i]; |
| 3067 | int j; |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3068 | for(j=i+SWAB16(pBt,pFBlk->iSize)-1; j>=i; j--) hit[j]++; |
| 3069 | i = SWAB16(pBt,pFBlk->iNext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3070 | } |
| 3071 | for(i=0; i<SQLITE_PAGE_SIZE; i++){ |
| 3072 | if( hit[i]==0 ){ |
| 3073 | sprintf(zMsg, "Unused space at byte %d of page %d", i, iPage); |
| 3074 | checkAppendMsg(pCheck, zMsg, 0); |
| 3075 | break; |
| 3076 | }else if( hit[i]>1 ){ |
| 3077 | sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage); |
| 3078 | checkAppendMsg(pCheck, zMsg, 0); |
| 3079 | break; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | /* Check that free space is kept to a minimum |
| 3084 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3085 | #if 0 |
| 3086 | if( pParent && pParent->nCell>2 && pPage->nFree>3*SQLITE_PAGE_SIZE/4 ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3087 | sprintf(zMsg, "free space (%d) greater than max (%d)", pPage->nFree, |
| 3088 | SQLITE_PAGE_SIZE/3); |
| 3089 | checkAppendMsg(pCheck, zContext, zMsg); |
| 3090 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3091 | #endif |
| 3092 | |
| 3093 | /* Update freespace totals. |
| 3094 | */ |
| 3095 | pCheck->nTreePage++; |
| 3096 | pCheck->nByte += USABLE_SPACE - pPage->nFree; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3097 | |
| 3098 | sqlitepager_unref(pPage); |
| 3099 | return depth; |
| 3100 | } |
| 3101 | |
| 3102 | /* |
| 3103 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 3104 | ** an array of pages numbers were each page number is the root page of |
| 3105 | ** a table. nRoot is the number of entries in aRoot. |
| 3106 | ** |
| 3107 | ** If everything checks out, this routine returns NULL. If something is |
| 3108 | ** amiss, an error message is written into memory obtained from malloc() |
| 3109 | ** and a pointer to that error message is returned. The calling function |
| 3110 | ** is responsible for freeing the error message when it is done. |
| 3111 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3112 | char *sqliteBtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3113 | int i; |
| 3114 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 3115 | IntegrityCk sCheck; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3116 | |
| 3117 | nRef = *sqlitepager_stats(pBt->pPager); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 3118 | if( lockBtree(pBt)!=SQLITE_OK ){ |
| 3119 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 3120 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3121 | sCheck.pBt = pBt; |
| 3122 | sCheck.pPager = pBt->pPager; |
| 3123 | sCheck.nPage = sqlitepager_pagecount(sCheck.pPager); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 3124 | if( sCheck.nPage==0 ){ |
| 3125 | unlockBtreeIfUnused(pBt); |
| 3126 | return 0; |
| 3127 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3128 | sCheck.anRef = sqliteMalloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
| 3129 | sCheck.anRef[1] = 1; |
| 3130 | for(i=2; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
| 3131 | sCheck.zErrMsg = 0; |
| 3132 | |
| 3133 | /* Check the integrity of the freelist |
| 3134 | */ |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 3135 | checkList(&sCheck, 1, SWAB32(pBt, pBt->page1->freeList), |
| 3136 | SWAB32(pBt, pBt->page1->nFree), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3137 | |
| 3138 | /* Check all the tables. |
| 3139 | */ |
| 3140 | for(i=0; i<nRoot; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 3141 | if( aRoot[i]==0 ) continue; |
drh | 1bffb9c | 2002-02-03 17:37:36 +0000 | [diff] [blame] | 3142 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | /* Make sure every page in the file is referenced |
| 3146 | */ |
| 3147 | for(i=1; i<=sCheck.nPage; i++){ |
| 3148 | if( sCheck.anRef[i]==0 ){ |
| 3149 | char zBuf[100]; |
| 3150 | sprintf(zBuf, "Page %d is never used", i); |
| 3151 | checkAppendMsg(&sCheck, zBuf, 0); |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | /* Make sure this analysis did not leave any unref() pages |
| 3156 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3157 | unlockBtreeIfUnused(pBt); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 3158 | if( nRef != *sqlitepager_stats(pBt->pPager) ){ |
| 3159 | char zBuf[100]; |
| 3160 | sprintf(zBuf, |
| 3161 | "Outstanding page count goes from %d to %d during this analysis", |
| 3162 | nRef, *sqlitepager_stats(pBt->pPager) |
| 3163 | ); |
| 3164 | checkAppendMsg(&sCheck, zBuf, 0); |
| 3165 | } |
| 3166 | |
| 3167 | /* Clean up and report errors. |
| 3168 | */ |
| 3169 | sqliteFree(sCheck.anRef); |
| 3170 | return sCheck.zErrMsg; |
| 3171 | } |