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