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