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