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