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