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