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