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