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