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