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