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