1434
|
1 /*
|
|
2 ** 2004 April 6
|
|
3 **
|
|
4 ** The author disclaims copyright to this source code. In place of
|
|
5 ** a legal notice, here is a blessing:
|
|
6 **
|
|
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.
|
|
10 **
|
|
11 *************************************************************************
|
|
12 ** $Id: btree.c,v 1.324 2006/04/04 01:54:55 drh Exp $
|
|
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 **
|
|
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.
|
|
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". A
|
|
40 ** fixed amount of payload can be carried directly on the database
|
|
41 ** page. If the payload is larger than the preset amount then surplus
|
|
42 ** bytes are stored on overflow pages. The payload for an entry
|
|
43 ** and the preceding pointer are combined to form a "Cell". Each
|
|
44 ** page has a small header which contains the Ptr(N+1) pointer and other
|
|
45 ** information such as the size of key and data.
|
|
46 **
|
|
47 ** FORMAT DETAILS
|
|
48 **
|
|
49 ** The file is divided into pages. The first page is called page 1,
|
|
50 ** the second is page 2, and so forth. A page number of zero indicates
|
|
51 ** "no such page". The page size can be anything between 512 and 65536.
|
|
52 ** Each page can be either a btree page, a freelist page or an overflow
|
|
53 ** page.
|
|
54 **
|
|
55 ** The first page is always a btree page. The first 100 bytes of the first
|
|
56 ** page contain a special header (the "file header") that describes the file.
|
|
57 ** The format of the file header is as follows:
|
|
58 **
|
|
59 ** OFFSET SIZE DESCRIPTION
|
|
60 ** 0 16 Header string: "SQLite format 3\000"
|
|
61 ** 16 2 Page size in bytes.
|
|
62 ** 18 1 File format write version
|
|
63 ** 19 1 File format read version
|
|
64 ** 20 1 Bytes of unused space at the end of each page
|
|
65 ** 21 1 Max embedded payload fraction
|
|
66 ** 22 1 Min embedded payload fraction
|
|
67 ** 23 1 Min leaf payload fraction
|
|
68 ** 24 4 File change counter
|
|
69 ** 28 4 Reserved for future use
|
|
70 ** 32 4 First freelist page
|
|
71 ** 36 4 Number of freelist pages in the file
|
|
72 ** 40 60 15 4-byte meta values passed to higher layers
|
|
73 **
|
|
74 ** All of the integer values are big-endian (most significant byte first).
|
|
75 **
|
|
76 ** The file change counter is incremented when the database is changed more
|
|
77 ** than once within the same second. This counter, together with the
|
|
78 ** modification time of the file, allows other processes to know
|
|
79 ** when the file has changed and thus when they need to flush their
|
|
80 ** cache.
|
|
81 **
|
|
82 ** The max embedded payload fraction is the amount of the total usable
|
|
83 ** space in a page that can be consumed by a single cell for standard
|
|
84 ** B-tree (non-LEAFDATA) tables. A value of 255 means 100%. The default
|
|
85 ** is to limit the maximum cell size so that at least 4 cells will fit
|
|
86 ** on one page. Thus the default max embedded payload fraction is 64.
|
|
87 **
|
|
88 ** If the payload for a cell is larger than the max payload, then extra
|
|
89 ** payload is spilled to overflow pages. Once an overflow page is allocated,
|
|
90 ** as many bytes as possible are moved into the overflow pages without letting
|
|
91 ** the cell size drop below the min embedded payload fraction.
|
|
92 **
|
|
93 ** The min leaf payload fraction is like the min embedded payload fraction
|
|
94 ** except that it applies to leaf nodes in a LEAFDATA tree. The maximum
|
|
95 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
|
|
96 ** not specified in the header.
|
|
97 **
|
|
98 ** Each btree pages is divided into three sections: The header, the
|
|
99 ** cell pointer array, and the cell area area. Page 1 also has a 100-byte
|
|
100 ** file header that occurs before the page header.
|
|
101 **
|
|
102 ** |----------------|
|
|
103 ** | file header | 100 bytes. Page 1 only.
|
|
104 ** |----------------|
|
|
105 ** | page header | 8 bytes for leaves. 12 bytes for interior nodes
|
|
106 ** |----------------|
|
|
107 ** | cell pointer | | 2 bytes per cell. Sorted order.
|
|
108 ** | array | | Grows downward
|
|
109 ** | | v
|
|
110 ** |----------------|
|
|
111 ** | unallocated |
|
|
112 ** | space |
|
|
113 ** |----------------| ^ Grows upwards
|
|
114 ** | cell content | | Arbitrary order interspersed with freeblocks.
|
|
115 ** | area | | and free space fragments.
|
|
116 ** |----------------|
|
|
117 **
|
|
118 ** The page headers looks like this:
|
|
119 **
|
|
120 ** OFFSET SIZE DESCRIPTION
|
|
121 ** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
|
|
122 ** 1 2 byte offset to the first freeblock
|
|
123 ** 3 2 number of cells on this page
|
|
124 ** 5 2 first byte of the cell content area
|
|
125 ** 7 1 number of fragmented free bytes
|
|
126 ** 8 4 Right child (the Ptr(N+1) value). Omitted on leaves.
|
|
127 **
|
|
128 ** The flags define the format of this btree page. The leaf flag means that
|
|
129 ** this page has no children. The zerodata flag means that this page carries
|
|
130 ** only keys and no data. The intkey flag means that the key is a integer
|
|
131 ** which is stored in the key size entry of the cell header rather than in
|
|
132 ** the payload area.
|
|
133 **
|
|
134 ** The cell pointer array begins on the first byte after the page header.
|
|
135 ** The cell pointer array contains zero or more 2-byte numbers which are
|
|
136 ** offsets from the beginning of the page to the cell content in the cell
|
|
137 ** content area. The cell pointers occur in sorted order. The system strives
|
|
138 ** to keep free space after the last cell pointer so that new cells can
|
|
139 ** be easily added without having to defragment the page.
|
|
140 **
|
|
141 ** Cell content is stored at the very end of the page and grows toward the
|
|
142 ** beginning of the page.
|
|
143 **
|
|
144 ** Unused space within the cell content area is collected into a linked list of
|
|
145 ** freeblocks. Each freeblock is at least 4 bytes in size. The byte offset
|
|
146 ** to the first freeblock is given in the header. Freeblocks occur in
|
|
147 ** increasing order. Because a freeblock must be at least 4 bytes in size,
|
|
148 ** any group of 3 or fewer unused bytes in the cell content area cannot
|
|
149 ** exist on the freeblock chain. A group of 3 or fewer free bytes is called
|
|
150 ** a fragment. The total number of bytes in all fragments is recorded.
|
|
151 ** in the page header at offset 7.
|
|
152 **
|
|
153 ** SIZE DESCRIPTION
|
|
154 ** 2 Byte offset of the next freeblock
|
|
155 ** 2 Bytes in this freeblock
|
|
156 **
|
|
157 ** Cells are of variable length. Cells are stored in the cell content area at
|
|
158 ** the end of the page. Pointers to the cells are in the cell pointer array
|
|
159 ** that immediately follows the page header. Cells is not necessarily
|
|
160 ** contiguous or in order, but cell pointers are contiguous and in order.
|
|
161 **
|
|
162 ** Cell content makes use of variable length integers. A variable
|
|
163 ** length integer is 1 to 9 bytes where the lower 7 bits of each
|
|
164 ** byte are used. The integer consists of all bytes that have bit 8 set and
|
|
165 ** the first byte with bit 8 clear. The most significant byte of the integer
|
|
166 ** appears first. A variable-length integer may not be more than 9 bytes long.
|
|
167 ** As a special case, all 8 bytes of the 9th byte are used as data. This
|
|
168 ** allows a 64-bit integer to be encoded in 9 bytes.
|
|
169 **
|
|
170 ** 0x00 becomes 0x00000000
|
|
171 ** 0x7f becomes 0x0000007f
|
|
172 ** 0x81 0x00 becomes 0x00000080
|
|
173 ** 0x82 0x00 becomes 0x00000100
|
|
174 ** 0x80 0x7f becomes 0x0000007f
|
|
175 ** 0x8a 0x91 0xd1 0xac 0x78 becomes 0x12345678
|
|
176 ** 0x81 0x81 0x81 0x81 0x01 becomes 0x10204081
|
|
177 **
|
|
178 ** Variable length integers are used for rowids and to hold the number of
|
|
179 ** bytes of key and data in a btree cell.
|
|
180 **
|
|
181 ** The content of a cell looks like this:
|
|
182 **
|
|
183 ** SIZE DESCRIPTION
|
|
184 ** 4 Page number of the left child. Omitted if leaf flag is set.
|
|
185 ** var Number of bytes of data. Omitted if the zerodata flag is set.
|
|
186 ** var Number of bytes of key. Or the key itself if intkey flag is set.
|
|
187 ** * Payload
|
|
188 ** 4 First page of the overflow chain. Omitted if no overflow
|
|
189 **
|
|
190 ** Overflow pages form a linked list. Each page except the last is completely
|
|
191 ** filled with data (pagesize - 4 bytes). The last page can have as little
|
|
192 ** as 1 byte of data.
|
|
193 **
|
|
194 ** SIZE DESCRIPTION
|
|
195 ** 4 Page number of next overflow page
|
|
196 ** * Data
|
|
197 **
|
|
198 ** Freelist pages come in two subtypes: trunk pages and leaf pages. The
|
|
199 ** file header points to first in a linked list of trunk page. Each trunk
|
|
200 ** page points to multiple leaf pages. The content of a leaf page is
|
|
201 ** unspecified. A trunk page looks like this:
|
|
202 **
|
|
203 ** SIZE DESCRIPTION
|
|
204 ** 4 Page number of next trunk page
|
|
205 ** 4 Number of leaf pointers on this page
|
|
206 ** * zero or more pages numbers of leaves
|
|
207 */
|
|
208 #include "sqliteInt.h"
|
|
209 #include "pager.h"
|
|
210 #include "btree.h"
|
|
211 #include "os.h"
|
|
212 #include <assert.h>
|
|
213
|
|
214 /* Round up a number to the next larger multiple of 8. This is used
|
|
215 ** to force 8-byte alignment on 64-bit architectures.
|
|
216 */
|
|
217 #define ROUND8(x) ((x+7)&~7)
|
|
218
|
|
219
|
|
220 /* The following value is the maximum cell size assuming a maximum page
|
|
221 ** size give above.
|
|
222 */
|
|
223 #define MX_CELL_SIZE(pBt) (pBt->pageSize-8)
|
|
224
|
|
225 /* The maximum number of cells on a single page of the database. This
|
|
226 ** assumes a minimum cell size of 3 bytes. Such small cells will be
|
|
227 ** exceedingly rare, but they are possible.
|
|
228 */
|
|
229 #define MX_CELL(pBt) ((pBt->pageSize-8)/3)
|
|
230
|
|
231 /* Forward declarations */
|
|
232 typedef struct MemPage MemPage;
|
|
233 typedef struct BtLock BtLock;
|
|
234
|
|
235 /*
|
|
236 ** This is a magic string that appears at the beginning of every
|
|
237 ** SQLite database in order to identify the file as a real database.
|
|
238 **
|
|
239 ** You can change this value at compile-time by specifying a
|
|
240 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line. The
|
|
241 ** header must be exactly 16 bytes including the zero-terminator so
|
|
242 ** the string itself should be 15 characters long. If you change
|
|
243 ** the header, then your custom library will not be able to read
|
|
244 ** databases generated by the standard tools and the standard tools
|
|
245 ** will not be able to read databases created by your custom library.
|
|
246 */
|
|
247 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
|
|
248 # define SQLITE_FILE_HEADER "SQLite format 3"
|
|
249 #endif
|
|
250 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
|
|
251
|
|
252 /*
|
|
253 ** Page type flags. An ORed combination of these flags appear as the
|
|
254 ** first byte of every BTree page.
|
|
255 */
|
|
256 #define PTF_INTKEY 0x01
|
|
257 #define PTF_ZERODATA 0x02
|
|
258 #define PTF_LEAFDATA 0x04
|
|
259 #define PTF_LEAF 0x08
|
|
260
|
|
261 /*
|
|
262 ** As each page of the file is loaded into memory, an instance of the following
|
|
263 ** structure is appended and initialized to zero. This structure stores
|
|
264 ** information about the page that is decoded from the raw file page.
|
|
265 **
|
|
266 ** The pParent field points back to the parent page. This allows us to
|
|
267 ** walk up the BTree from any leaf to the root. Care must be taken to
|
|
268 ** unref() the parent page pointer when this page is no longer referenced.
|
|
269 ** The pageDestructor() routine handles that chore.
|
|
270 */
|
|
271 struct MemPage {
|
|
272 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
|
|
273 u8 idxShift; /* True if Cell indices have changed */
|
|
274 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
|
|
275 u8 intKey; /* True if intkey flag is set */
|
|
276 u8 leaf; /* True if leaf flag is set */
|
|
277 u8 zeroData; /* True if table stores keys only */
|
|
278 u8 leafData; /* True if tables stores data on leaves only */
|
|
279 u8 hasData; /* True if this page stores data */
|
|
280 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
|
|
281 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
|
|
282 u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */
|
|
283 u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */
|
|
284 u16 cellOffset; /* Index in aData of first cell pointer */
|
|
285 u16 idxParent; /* Index in parent of this node */
|
|
286 u16 nFree; /* Number of free bytes on the page */
|
|
287 u16 nCell; /* Number of cells on this page, local and ovfl */
|
|
288 struct _OvflCell { /* Cells that will not fit on aData[] */
|
|
289 u8 *pCell; /* Pointers to the body of the overflow cell */
|
|
290 u16 idx; /* Insert this cell before idx-th non-overflow cell */
|
|
291 } aOvfl[5];
|
|
292 BtShared *pBt; /* Pointer back to BTree structure */
|
|
293 u8 *aData; /* Pointer back to the start of the page */
|
|
294 Pgno pgno; /* Page number for this page */
|
|
295 MemPage *pParent; /* The parent of this page. NULL for root */
|
|
296 };
|
|
297
|
|
298 /*
|
|
299 ** The in-memory image of a disk page has the auxiliary information appended
|
|
300 ** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
|
|
301 ** that extra information.
|
|
302 */
|
|
303 #define EXTRA_SIZE sizeof(MemPage)
|
|
304
|
|
305 /* Btree handle */
|
|
306 struct Btree {
|
|
307 sqlite3 *pSqlite;
|
|
308 BtShared *pBt;
|
|
309 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
|
|
310 };
|
|
311
|
|
312 /*
|
|
313 ** Btree.inTrans may take one of the following values.
|
|
314 **
|
|
315 ** If the shared-data extension is enabled, there may be multiple users
|
|
316 ** of the Btree structure. At most one of these may open a write transaction,
|
|
317 ** but any number may have active read transactions. Variable Btree.pDb
|
|
318 ** points to the handle that owns any current write-transaction.
|
|
319 */
|
|
320 #define TRANS_NONE 0
|
|
321 #define TRANS_READ 1
|
|
322 #define TRANS_WRITE 2
|
|
323
|
|
324 /*
|
|
325 ** Everything we need to know about an open database
|
|
326 */
|
|
327 struct BtShared {
|
|
328 Pager *pPager; /* The page cache */
|
|
329 BtCursor *pCursor; /* A list of all open cursors */
|
|
330 MemPage *pPage1; /* First page of the database */
|
|
331 u8 inStmt; /* True if we are in a statement subtransaction */
|
|
332 u8 readOnly; /* True if the underlying file is readonly */
|
|
333 u8 maxEmbedFrac; /* Maximum payload as % of total page size */
|
|
334 u8 minEmbedFrac; /* Minimum payload as % of total page size */
|
|
335 u8 minLeafFrac; /* Minimum leaf payload as % of total page size */
|
|
336 u8 pageSizeFixed; /* True if the page size can no longer be changed */
|
|
337 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
338 u8 autoVacuum; /* True if database supports auto-vacuum */
|
|
339 #endif
|
|
340 u16 pageSize; /* Total number of bytes on a page */
|
|
341 u16 usableSize; /* Number of usable bytes on each page */
|
|
342 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
|
|
343 int minLocal; /* Minimum local payload in non-LEAFDATA tables */
|
|
344 int maxLeaf; /* Maximum local payload in a LEAFDATA table */
|
|
345 int minLeaf; /* Minimum local payload in a LEAFDATA table */
|
|
346 BusyHandler *pBusyHandler; /* Callback for when there is lock contention */
|
|
347 u8 inTransaction; /* Transaction state */
|
|
348 int nRef; /* Number of references to this structure */
|
|
349 int nTransaction; /* Number of open transactions (read + write) */
|
|
350 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
|
|
351 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
|
|
352 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
353 BtLock *pLock; /* List of locks held on this shared-btree struct */
|
|
354 BtShared *pNext; /* Next in ThreadData.pBtree linked list */
|
|
355 #endif
|
|
356 };
|
|
357
|
|
358 /*
|
|
359 ** An instance of the following structure is used to hold information
|
|
360 ** about a cell. The parseCellPtr() function fills in this structure
|
|
361 ** based on information extract from the raw disk page.
|
|
362 */
|
|
363 typedef struct CellInfo CellInfo;
|
|
364 struct CellInfo {
|
|
365 u8 *pCell; /* Pointer to the start of cell content */
|
|
366 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
|
|
367 u32 nData; /* Number of bytes of data */
|
|
368 u16 nHeader; /* Size of the cell content header in bytes */
|
|
369 u16 nLocal; /* Amount of payload held locally */
|
|
370 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
|
|
371 u16 nSize; /* Size of the cell content on the main b-tree page */
|
|
372 };
|
|
373
|
|
374 /*
|
|
375 ** A cursor is a pointer to a particular entry in the BTree.
|
|
376 ** The entry is identified by its MemPage and the index in
|
|
377 ** MemPage.aCell[] of the entry.
|
|
378 */
|
|
379 struct BtCursor {
|
|
380 Btree *pBtree; /* The Btree to which this cursor belongs */
|
|
381 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
|
|
382 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
|
|
383 void *pArg; /* First arg to xCompare() */
|
|
384 Pgno pgnoRoot; /* The root page of this tree */
|
|
385 MemPage *pPage; /* Page that contains the entry */
|
|
386 int idx; /* Index of the entry in pPage->aCell[] */
|
|
387 CellInfo info; /* A parse of the cell we are pointing at */
|
|
388 u8 wrFlag; /* True if writable */
|
|
389 u8 eState; /* One of the CURSOR_XXX constants (see below) */
|
|
390 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
391 void *pKey; /* Saved key that was cursor's last known position */
|
|
392 i64 nKey; /* Size of pKey, or last integer key */
|
|
393 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
|
|
394 #endif
|
|
395 };
|
|
396
|
|
397 /*
|
|
398 ** Potential values for BtCursor.eState. The first two values (VALID and
|
|
399 ** INVALID) may occur in any build. The third (REQUIRESEEK) may only occur
|
|
400 ** if sqlite was compiled without the OMIT_SHARED_CACHE symbol defined.
|
|
401 **
|
|
402 ** CURSOR_VALID:
|
|
403 ** Cursor points to a valid entry. getPayload() etc. may be called.
|
|
404 **
|
|
405 ** CURSOR_INVALID:
|
|
406 ** Cursor does not point to a valid entry. This can happen (for example)
|
|
407 ** because the table is empty or because BtreeCursorFirst() has not been
|
|
408 ** called.
|
|
409 **
|
|
410 ** CURSOR_REQUIRESEEK:
|
|
411 ** The table that this cursor was opened on still exists, but has been
|
|
412 ** modified since the cursor was last used. The cursor position is saved
|
|
413 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
|
|
414 ** this state, restoreOrClearCursorPosition() can be called to attempt to
|
|
415 ** seek the cursor to the saved position.
|
|
416 */
|
|
417 #define CURSOR_INVALID 0
|
|
418 #define CURSOR_VALID 1
|
|
419 #define CURSOR_REQUIRESEEK 2
|
|
420
|
|
421 /*
|
|
422 ** The TRACE macro will print high-level status information about the
|
|
423 ** btree operation when the global variable sqlite3_btree_trace is
|
|
424 ** enabled.
|
|
425 */
|
|
426 #if SQLITE_TEST
|
|
427 # define TRACE(X) if( sqlite3_btree_trace )\
|
|
428 { sqlite3DebugPrintf X; fflush(stdout); }
|
|
429 #else
|
|
430 # define TRACE(X)
|
|
431 #endif
|
|
432 int sqlite3_btree_trace=0; /* True to enable tracing */
|
|
433
|
|
434 /*
|
|
435 ** Forward declaration
|
|
436 */
|
|
437 static int checkReadLocks(BtShared*,Pgno,BtCursor*);
|
|
438
|
|
439 /*
|
|
440 ** Read or write a two- and four-byte big-endian integer values.
|
|
441 */
|
|
442 static u32 get2byte(unsigned char *p){
|
|
443 return (p[0]<<8) | p[1];
|
|
444 }
|
|
445 static u32 get4byte(unsigned char *p){
|
|
446 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
|
|
447 }
|
|
448 static void put2byte(unsigned char *p, u32 v){
|
|
449 p[0] = v>>8;
|
|
450 p[1] = v;
|
|
451 }
|
|
452 static void put4byte(unsigned char *p, u32 v){
|
|
453 p[0] = v>>24;
|
|
454 p[1] = v>>16;
|
|
455 p[2] = v>>8;
|
|
456 p[3] = v;
|
|
457 }
|
|
458
|
|
459 /*
|
|
460 ** Routines to read and write variable-length integers. These used to
|
|
461 ** be defined locally, but now we use the varint routines in the util.c
|
|
462 ** file.
|
|
463 */
|
|
464 #define getVarint sqlite3GetVarint
|
|
465 /* #define getVarint32 sqlite3GetVarint32 */
|
|
466 #define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
|
|
467 #define putVarint sqlite3PutVarint
|
|
468
|
|
469 /* The database page the PENDING_BYTE occupies. This page is never used.
|
|
470 ** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
|
|
471 ** should possibly be consolidated (presumably in pager.h).
|
|
472 **
|
|
473 ** If disk I/O is omitted (meaning that the database is stored purely
|
|
474 ** in memory) then there is no pending byte.
|
|
475 */
|
|
476 #ifdef SQLITE_OMIT_DISKIO
|
|
477 # define PENDING_BYTE_PAGE(pBt) 0x7fffffff
|
|
478 #else
|
|
479 # define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
|
|
480 #endif
|
|
481
|
|
482 /*
|
|
483 ** A linked list of the following structures is stored at BtShared.pLock.
|
|
484 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
|
|
485 ** is opened on the table with root page BtShared.iTable. Locks are removed
|
|
486 ** from this list when a transaction is committed or rolled back, or when
|
|
487 ** a btree handle is closed.
|
|
488 */
|
|
489 struct BtLock {
|
|
490 Btree *pBtree; /* Btree handle holding this lock */
|
|
491 Pgno iTable; /* Root page of table */
|
|
492 u8 eLock; /* READ_LOCK or WRITE_LOCK */
|
|
493 BtLock *pNext; /* Next in BtShared.pLock list */
|
|
494 };
|
|
495
|
|
496 /* Candidate values for BtLock.eLock */
|
|
497 #define READ_LOCK 1
|
|
498 #define WRITE_LOCK 2
|
|
499
|
|
500 #ifdef SQLITE_OMIT_SHARED_CACHE
|
|
501 /*
|
|
502 ** The functions queryTableLock(), lockTable() and unlockAllTables()
|
|
503 ** manipulate entries in the BtShared.pLock linked list used to store
|
|
504 ** shared-cache table level locks. If the library is compiled with the
|
|
505 ** shared-cache feature disabled, then there is only ever one user
|
|
506 ** of each BtShared structure and so this locking is not necessary.
|
|
507 ** So define the lock related functions as no-ops.
|
|
508 */
|
|
509 #define queryTableLock(a,b,c) SQLITE_OK
|
|
510 #define lockTable(a,b,c) SQLITE_OK
|
|
511 #define unlockAllTables(a)
|
|
512 #define restoreOrClearCursorPosition(a,b) SQLITE_OK
|
|
513 #define saveAllCursors(a,b,c) SQLITE_OK
|
|
514
|
|
515 #else
|
|
516
|
|
517 static void releasePage(MemPage *pPage);
|
|
518
|
|
519 /*
|
|
520 ** Save the current cursor position in the variables BtCursor.nKey
|
|
521 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
|
|
522 */
|
|
523 static int saveCursorPosition(BtCursor *pCur){
|
|
524 int rc;
|
|
525
|
|
526 assert( CURSOR_VALID==pCur->eState );
|
|
527 assert( 0==pCur->pKey );
|
|
528
|
|
529 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
|
|
530
|
|
531 /* If this is an intKey table, then the above call to BtreeKeySize()
|
|
532 ** stores the integer key in pCur->nKey. In this case this value is
|
|
533 ** all that is required. Otherwise, if pCur is not open on an intKey
|
|
534 ** table, then malloc space for and store the pCur->nKey bytes of key
|
|
535 ** data.
|
|
536 */
|
|
537 if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
|
|
538 void *pKey = sqliteMalloc(pCur->nKey);
|
|
539 if( pKey ){
|
|
540 rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
|
|
541 if( rc==SQLITE_OK ){
|
|
542 pCur->pKey = pKey;
|
|
543 }else{
|
|
544 sqliteFree(pKey);
|
|
545 }
|
|
546 }else{
|
|
547 rc = SQLITE_NOMEM;
|
|
548 }
|
|
549 }
|
|
550 assert( !pCur->pPage->intKey || !pCur->pKey );
|
|
551
|
|
552 if( rc==SQLITE_OK ){
|
|
553 releasePage(pCur->pPage);
|
|
554 pCur->pPage = 0;
|
|
555 pCur->eState = CURSOR_REQUIRESEEK;
|
|
556 }
|
|
557
|
|
558 return rc;
|
|
559 }
|
|
560
|
|
561 /*
|
|
562 ** Save the positions of all cursors except pExcept open on the table
|
|
563 ** with root-page iRoot. Usually, this is called just before cursor
|
|
564 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
|
|
565 */
|
|
566 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
|
|
567 BtCursor *p;
|
|
568 if( sqlite3ThreadDataReadOnly()->useSharedData ){
|
|
569 for(p=pBt->pCursor; p; p=p->pNext){
|
|
570 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
|
|
571 p->eState==CURSOR_VALID ){
|
|
572 int rc = saveCursorPosition(p);
|
|
573 if( SQLITE_OK!=rc ){
|
|
574 return rc;
|
|
575 }
|
|
576 }
|
|
577 }
|
|
578 }
|
|
579 return SQLITE_OK;
|
|
580 }
|
|
581
|
|
582 /*
|
|
583 ** Restore the cursor to the position it was in (or as close to as possible)
|
|
584 ** when saveCursorPosition() was called. Note that this call deletes the
|
|
585 ** saved position info stored by saveCursorPosition(), so there can be
|
|
586 ** at most one effective restoreOrClearCursorPosition() call after each
|
|
587 ** saveCursorPosition().
|
|
588 **
|
|
589 ** If the second argument argument - doSeek - is false, then instead of
|
|
590 ** returning the cursor to it's saved position, any saved position is deleted
|
|
591 ** and the cursor state set to CURSOR_INVALID.
|
|
592 */
|
|
593 static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){
|
|
594 int rc = SQLITE_OK;
|
|
595 assert( sqlite3ThreadDataReadOnly()->useSharedData );
|
|
596 assert( pCur->eState==CURSOR_REQUIRESEEK );
|
|
597 pCur->eState = CURSOR_INVALID;
|
|
598 if( doSeek ){
|
|
599 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, &pCur->skip);
|
|
600 }
|
|
601 if( rc==SQLITE_OK ){
|
|
602 sqliteFree(pCur->pKey);
|
|
603 pCur->pKey = 0;
|
|
604 assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
|
|
605 }
|
|
606 return rc;
|
|
607 }
|
|
608
|
|
609 #define restoreOrClearCursorPosition(p,x) \
|
|
610 (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK)
|
|
611
|
|
612 /*
|
|
613 ** Query to see if btree handle p may obtain a lock of type eLock
|
|
614 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
|
|
615 ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
|
|
616 ** SQLITE_LOCKED if not.
|
|
617 */
|
|
618 static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
|
|
619 BtShared *pBt = p->pBt;
|
|
620 BtLock *pIter;
|
|
621
|
|
622 /* This is a no-op if the shared-cache is not enabled */
|
|
623 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
|
|
624 return SQLITE_OK;
|
|
625 }
|
|
626
|
|
627 /* This (along with lockTable()) is where the ReadUncommitted flag is
|
|
628 ** dealt with. If the caller is querying for a read-lock and the flag is
|
|
629 ** set, it is unconditionally granted - even if there are write-locks
|
|
630 ** on the table. If a write-lock is requested, the ReadUncommitted flag
|
|
631 ** is not considered.
|
|
632 **
|
|
633 ** In function lockTable(), if a read-lock is demanded and the
|
|
634 ** ReadUncommitted flag is set, no entry is added to the locks list
|
|
635 ** (BtShared.pLock).
|
|
636 **
|
|
637 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
|
|
638 ** not create or respect table locks. The locking procedure for a
|
|
639 ** write-cursor does not change.
|
|
640 */
|
|
641 if(
|
|
642 !p->pSqlite ||
|
|
643 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) ||
|
|
644 eLock==WRITE_LOCK ||
|
|
645 iTab==MASTER_ROOT
|
|
646 ){
|
|
647 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
|
|
648 if( pIter->pBtree!=p && pIter->iTable==iTab &&
|
|
649 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
|
|
650 return SQLITE_LOCKED;
|
|
651 }
|
|
652 }
|
|
653 }
|
|
654 return SQLITE_OK;
|
|
655 }
|
|
656
|
|
657 /*
|
|
658 ** Add a lock on the table with root-page iTable to the shared-btree used
|
|
659 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
|
|
660 ** WRITE_LOCK.
|
|
661 **
|
|
662 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
|
|
663 ** SQLITE_NOMEM may also be returned.
|
|
664 */
|
|
665 static int lockTable(Btree *p, Pgno iTable, u8 eLock){
|
|
666 BtShared *pBt = p->pBt;
|
|
667 BtLock *pLock = 0;
|
|
668 BtLock *pIter;
|
|
669
|
|
670 /* This is a no-op if the shared-cache is not enabled */
|
|
671 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
|
|
672 return SQLITE_OK;
|
|
673 }
|
|
674
|
|
675 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
|
|
676
|
|
677 /* If the read-uncommitted flag is set and a read-lock is requested,
|
|
678 ** return early without adding an entry to the BtShared.pLock list. See
|
|
679 ** comment in function queryTableLock() for more info on handling
|
|
680 ** the ReadUncommitted flag.
|
|
681 */
|
|
682 if(
|
|
683 (p->pSqlite) &&
|
|
684 (p->pSqlite->flags&SQLITE_ReadUncommitted) &&
|
|
685 (eLock==READ_LOCK) &&
|
|
686 iTable!=MASTER_ROOT
|
|
687 ){
|
|
688 return SQLITE_OK;
|
|
689 }
|
|
690
|
|
691 /* First search the list for an existing lock on this table. */
|
|
692 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
|
|
693 if( pIter->iTable==iTable && pIter->pBtree==p ){
|
|
694 pLock = pIter;
|
|
695 break;
|
|
696 }
|
|
697 }
|
|
698
|
|
699 /* If the above search did not find a BtLock struct associating Btree p
|
|
700 ** with table iTable, allocate one and link it into the list.
|
|
701 */
|
|
702 if( !pLock ){
|
|
703 pLock = (BtLock *)sqliteMalloc(sizeof(BtLock));
|
|
704 if( !pLock ){
|
|
705 return SQLITE_NOMEM;
|
|
706 }
|
|
707 pLock->iTable = iTable;
|
|
708 pLock->pBtree = p;
|
|
709 pLock->pNext = pBt->pLock;
|
|
710 pBt->pLock = pLock;
|
|
711 }
|
|
712
|
|
713 /* Set the BtLock.eLock variable to the maximum of the current lock
|
|
714 ** and the requested lock. This means if a write-lock was already held
|
|
715 ** and a read-lock requested, we don't incorrectly downgrade the lock.
|
|
716 */
|
|
717 assert( WRITE_LOCK>READ_LOCK );
|
|
718 if( eLock>pLock->eLock ){
|
|
719 pLock->eLock = eLock;
|
|
720 }
|
|
721
|
|
722 return SQLITE_OK;
|
|
723 }
|
|
724
|
|
725 /*
|
|
726 ** Release all the table locks (locks obtained via calls to the lockTable()
|
|
727 ** procedure) held by Btree handle p.
|
|
728 */
|
|
729 static void unlockAllTables(Btree *p){
|
|
730 BtLock **ppIter = &p->pBt->pLock;
|
|
731
|
|
732 /* If the shared-cache extension is not enabled, there should be no
|
|
733 ** locks in the BtShared.pLock list, making this procedure a no-op. Assert
|
|
734 ** that this is the case.
|
|
735 */
|
|
736 assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter );
|
|
737
|
|
738 while( *ppIter ){
|
|
739 BtLock *pLock = *ppIter;
|
|
740 if( pLock->pBtree==p ){
|
|
741 *ppIter = pLock->pNext;
|
|
742 sqliteFree(pLock);
|
|
743 }else{
|
|
744 ppIter = &pLock->pNext;
|
|
745 }
|
|
746 }
|
|
747 }
|
|
748 #endif /* SQLITE_OMIT_SHARED_CACHE */
|
|
749
|
|
750 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
751 /*
|
|
752 ** These macros define the location of the pointer-map entry for a
|
|
753 ** database page. The first argument to each is the number of usable
|
|
754 ** bytes on each page of the database (often 1024). The second is the
|
|
755 ** page number to look up in the pointer map.
|
|
756 **
|
|
757 ** PTRMAP_PAGENO returns the database page number of the pointer-map
|
|
758 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
|
|
759 ** the offset of the requested map entry.
|
|
760 **
|
|
761 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
|
|
762 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
|
|
763 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
|
|
764 ** this test.
|
|
765 */
|
|
766 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
|
|
767 #define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
|
|
768 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
|
|
769
|
|
770 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
|
|
771 int nPagesPerMapPage = (pBt->usableSize/5)+1;
|
|
772 int iPtrMap = (pgno-2)/nPagesPerMapPage;
|
|
773 int ret = (iPtrMap*nPagesPerMapPage) + 2;
|
|
774 if( ret==PENDING_BYTE_PAGE(pBt) ){
|
|
775 ret++;
|
|
776 }
|
|
777 return ret;
|
|
778 }
|
|
779
|
|
780 /*
|
|
781 ** The pointer map is a lookup table that identifies the parent page for
|
|
782 ** each child page in the database file. The parent page is the page that
|
|
783 ** contains a pointer to the child. Every page in the database contains
|
|
784 ** 0 or 1 parent pages. (In this context 'database page' refers
|
|
785 ** to any page that is not part of the pointer map itself.) Each pointer map
|
|
786 ** entry consists of a single byte 'type' and a 4 byte parent page number.
|
|
787 ** The PTRMAP_XXX identifiers below are the valid types.
|
|
788 **
|
|
789 ** The purpose of the pointer map is to facility moving pages from one
|
|
790 ** position in the file to another as part of autovacuum. When a page
|
|
791 ** is moved, the pointer in its parent must be updated to point to the
|
|
792 ** new location. The pointer map is used to locate the parent page quickly.
|
|
793 **
|
|
794 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
|
|
795 ** used in this case.
|
|
796 **
|
|
797 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
|
|
798 ** is not used in this case.
|
|
799 **
|
|
800 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
|
|
801 ** overflow pages. The page number identifies the page that
|
|
802 ** contains the cell with a pointer to this overflow page.
|
|
803 **
|
|
804 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
|
|
805 ** overflow pages. The page-number identifies the previous
|
|
806 ** page in the overflow page list.
|
|
807 **
|
|
808 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
|
|
809 ** identifies the parent page in the btree.
|
|
810 */
|
|
811 #define PTRMAP_ROOTPAGE 1
|
|
812 #define PTRMAP_FREEPAGE 2
|
|
813 #define PTRMAP_OVERFLOW1 3
|
|
814 #define PTRMAP_OVERFLOW2 4
|
|
815 #define PTRMAP_BTREE 5
|
|
816
|
|
817 /*
|
|
818 ** Write an entry into the pointer map.
|
|
819 **
|
|
820 ** This routine updates the pointer map entry for page number 'key'
|
|
821 ** so that it maps to type 'eType' and parent page number 'pgno'.
|
|
822 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
|
|
823 */
|
|
824 static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
|
|
825 u8 *pPtrmap; /* The pointer map page */
|
|
826 Pgno iPtrmap; /* The pointer map page number */
|
|
827 int offset; /* Offset in pointer map page */
|
|
828 int rc;
|
|
829
|
|
830 /* The master-journal page number must never be used as a pointer map page */
|
|
831 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
|
|
832
|
|
833 assert( pBt->autoVacuum );
|
|
834 if( key==0 ){
|
|
835 return SQLITE_CORRUPT_BKPT;
|
|
836 }
|
|
837 iPtrmap = PTRMAP_PAGENO(pBt, key);
|
|
838 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
|
|
839 if( rc!=SQLITE_OK ){
|
|
840 return rc;
|
|
841 }
|
|
842 offset = PTRMAP_PTROFFSET(pBt, key);
|
|
843
|
|
844 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
|
|
845 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
|
|
846 rc = sqlite3pager_write(pPtrmap);
|
|
847 if( rc==SQLITE_OK ){
|
|
848 pPtrmap[offset] = eType;
|
|
849 put4byte(&pPtrmap[offset+1], parent);
|
|
850 }
|
|
851 }
|
|
852
|
|
853 sqlite3pager_unref(pPtrmap);
|
|
854 return rc;
|
|
855 }
|
|
856
|
|
857 /*
|
|
858 ** Read an entry from the pointer map.
|
|
859 **
|
|
860 ** This routine retrieves the pointer map entry for page 'key', writing
|
|
861 ** the type and parent page number to *pEType and *pPgno respectively.
|
|
862 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
|
|
863 */
|
|
864 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
|
|
865 int iPtrmap; /* Pointer map page index */
|
|
866 u8 *pPtrmap; /* Pointer map page data */
|
|
867 int offset; /* Offset of entry in pointer map */
|
|
868 int rc;
|
|
869
|
|
870 iPtrmap = PTRMAP_PAGENO(pBt, key);
|
|
871 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
|
|
872 if( rc!=0 ){
|
|
873 return rc;
|
|
874 }
|
|
875
|
|
876 offset = PTRMAP_PTROFFSET(pBt, key);
|
|
877 assert( pEType!=0 );
|
|
878 *pEType = pPtrmap[offset];
|
|
879 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
|
|
880
|
|
881 sqlite3pager_unref(pPtrmap);
|
|
882 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
|
|
883 return SQLITE_OK;
|
|
884 }
|
|
885
|
|
886 #endif /* SQLITE_OMIT_AUTOVACUUM */
|
|
887
|
|
888 /*
|
|
889 ** Given a btree page and a cell index (0 means the first cell on
|
|
890 ** the page, 1 means the second cell, and so forth) return a pointer
|
|
891 ** to the cell content.
|
|
892 **
|
|
893 ** This routine works only for pages that do not contain overflow cells.
|
|
894 */
|
|
895 static u8 *findCell(MemPage *pPage, int iCell){
|
|
896 u8 *data = pPage->aData;
|
|
897 assert( iCell>=0 );
|
|
898 assert( iCell<get2byte(&data[pPage->hdrOffset+3]) );
|
|
899 return data + get2byte(&data[pPage->cellOffset+2*iCell]);
|
|
900 }
|
|
901
|
|
902 /*
|
|
903 ** This a more complex version of findCell() that works for
|
|
904 ** pages that do contain overflow cells. See insert
|
|
905 */
|
|
906 static u8 *findOverflowCell(MemPage *pPage, int iCell){
|
|
907 int i;
|
|
908 for(i=pPage->nOverflow-1; i>=0; i--){
|
|
909 int k;
|
|
910 struct _OvflCell *pOvfl;
|
|
911 pOvfl = &pPage->aOvfl[i];
|
|
912 k = pOvfl->idx;
|
|
913 if( k<=iCell ){
|
|
914 if( k==iCell ){
|
|
915 return pOvfl->pCell;
|
|
916 }
|
|
917 iCell--;
|
|
918 }
|
|
919 }
|
|
920 return findCell(pPage, iCell);
|
|
921 }
|
|
922
|
|
923 /*
|
|
924 ** Parse a cell content block and fill in the CellInfo structure. There
|
|
925 ** are two versions of this function. parseCell() takes a cell index
|
|
926 ** as the second argument and parseCellPtr() takes a pointer to the
|
|
927 ** body of the cell as its second argument.
|
|
928 */
|
|
929 static void parseCellPtr(
|
|
930 MemPage *pPage, /* Page containing the cell */
|
|
931 u8 *pCell, /* Pointer to the cell text. */
|
|
932 CellInfo *pInfo /* Fill in this structure */
|
|
933 ){
|
|
934 int n; /* Number bytes in cell content header */
|
|
935 u32 nPayload; /* Number of bytes of cell payload */
|
|
936
|
|
937 pInfo->pCell = pCell;
|
|
938 assert( pPage->leaf==0 || pPage->leaf==1 );
|
|
939 n = pPage->childPtrSize;
|
|
940 assert( n==4-4*pPage->leaf );
|
|
941 if( pPage->hasData ){
|
|
942 n += getVarint32(&pCell[n], &nPayload);
|
|
943 }else{
|
|
944 nPayload = 0;
|
|
945 }
|
|
946 pInfo->nData = nPayload;
|
|
947 if( pPage->intKey ){
|
|
948 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
|
|
949 }else{
|
|
950 u32 x;
|
|
951 n += getVarint32(&pCell[n], &x);
|
|
952 pInfo->nKey = x;
|
|
953 nPayload += x;
|
|
954 }
|
|
955 pInfo->nHeader = n;
|
|
956 if( nPayload<=pPage->maxLocal ){
|
|
957 /* This is the (easy) common case where the entire payload fits
|
|
958 ** on the local page. No overflow is required.
|
|
959 */
|
|
960 int nSize; /* Total size of cell content in bytes */
|
|
961 pInfo->nLocal = nPayload;
|
|
962 pInfo->iOverflow = 0;
|
|
963 nSize = nPayload + n;
|
|
964 if( nSize<4 ){
|
|
965 nSize = 4; /* Minimum cell size is 4 */
|
|
966 }
|
|
967 pInfo->nSize = nSize;
|
|
968 }else{
|
|
969 /* If the payload will not fit completely on the local page, we have
|
|
970 ** to decide how much to store locally and how much to spill onto
|
|
971 ** overflow pages. The strategy is to minimize the amount of unused
|
|
972 ** space on overflow pages while keeping the amount of local storage
|
|
973 ** in between minLocal and maxLocal.
|
|
974 **
|
|
975 ** Warning: changing the way overflow payload is distributed in any
|
|
976 ** way will result in an incompatible file format.
|
|
977 */
|
|
978 int minLocal; /* Minimum amount of payload held locally */
|
|
979 int maxLocal; /* Maximum amount of payload held locally */
|
|
980 int surplus; /* Overflow payload available for local storage */
|
|
981
|
|
982 minLocal = pPage->minLocal;
|
|
983 maxLocal = pPage->maxLocal;
|
|
984 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
|
|
985 if( surplus <= maxLocal ){
|
|
986 pInfo->nLocal = surplus;
|
|
987 }else{
|
|
988 pInfo->nLocal = minLocal;
|
|
989 }
|
|
990 pInfo->iOverflow = pInfo->nLocal + n;
|
|
991 pInfo->nSize = pInfo->iOverflow + 4;
|
|
992 }
|
|
993 }
|
|
994 static void parseCell(
|
|
995 MemPage *pPage, /* Page containing the cell */
|
|
996 int iCell, /* The cell index. First cell is 0 */
|
|
997 CellInfo *pInfo /* Fill in this structure */
|
|
998 ){
|
|
999 parseCellPtr(pPage, findCell(pPage, iCell), pInfo);
|
|
1000 }
|
|
1001
|
|
1002 /*
|
|
1003 ** Compute the total number of bytes that a Cell needs in the cell
|
|
1004 ** data area of the btree-page. The return number includes the cell
|
|
1005 ** data header and the local payload, but not any overflow page or
|
|
1006 ** the space used by the cell pointer.
|
|
1007 */
|
|
1008 #ifndef NDEBUG
|
|
1009 static int cellSize(MemPage *pPage, int iCell){
|
|
1010 CellInfo info;
|
|
1011 parseCell(pPage, iCell, &info);
|
|
1012 return info.nSize;
|
|
1013 }
|
|
1014 #endif
|
|
1015 static int cellSizePtr(MemPage *pPage, u8 *pCell){
|
|
1016 CellInfo info;
|
|
1017 parseCellPtr(pPage, pCell, &info);
|
|
1018 return info.nSize;
|
|
1019 }
|
|
1020
|
|
1021 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
1022 /*
|
|
1023 ** If the cell pCell, part of page pPage contains a pointer
|
|
1024 ** to an overflow page, insert an entry into the pointer-map
|
|
1025 ** for the overflow page.
|
|
1026 */
|
|
1027 static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
|
|
1028 if( pCell ){
|
|
1029 CellInfo info;
|
|
1030 parseCellPtr(pPage, pCell, &info);
|
|
1031 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
|
|
1032 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
|
|
1033 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
|
|
1034 }
|
|
1035 }
|
|
1036 return SQLITE_OK;
|
|
1037 }
|
|
1038 /*
|
|
1039 ** If the cell with index iCell on page pPage contains a pointer
|
|
1040 ** to an overflow page, insert an entry into the pointer-map
|
|
1041 ** for the overflow page.
|
|
1042 */
|
|
1043 static int ptrmapPutOvfl(MemPage *pPage, int iCell){
|
|
1044 u8 *pCell;
|
|
1045 pCell = findOverflowCell(pPage, iCell);
|
|
1046 return ptrmapPutOvflPtr(pPage, pCell);
|
|
1047 }
|
|
1048 #endif
|
|
1049
|
|
1050
|
|
1051 /*
|
|
1052 ** Do sanity checking on a page. Throw an exception if anything is
|
|
1053 ** not right.
|
|
1054 **
|
|
1055 ** This routine is used for internal error checking only. It is omitted
|
|
1056 ** from most builds.
|
|
1057 */
|
|
1058 #if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0
|
|
1059 static void _pageIntegrity(MemPage *pPage){
|
|
1060 int usableSize;
|
|
1061 u8 *data;
|
|
1062 int i, j, idx, c, pc, hdr, nFree;
|
|
1063 int cellOffset;
|
|
1064 int nCell, cellLimit;
|
|
1065 u8 *used;
|
|
1066
|
|
1067 used = sqliteMallocRaw( pPage->pBt->pageSize );
|
|
1068 if( used==0 ) return;
|
|
1069 usableSize = pPage->pBt->usableSize;
|
|
1070 assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->pageSize] );
|
|
1071 hdr = pPage->hdrOffset;
|
|
1072 assert( hdr==(pPage->pgno==1 ? 100 : 0) );
|
|
1073 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
|
|
1074 c = pPage->aData[hdr];
|
|
1075 if( pPage->isInit ){
|
|
1076 assert( pPage->leaf == ((c & PTF_LEAF)!=0) );
|
|
1077 assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) );
|
|
1078 assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) );
|
|
1079 assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) );
|
|
1080 assert( pPage->hasData ==
|
|
1081 !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) );
|
|
1082 assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf );
|
|
1083 assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) );
|
|
1084 }
|
|
1085 data = pPage->aData;
|
|
1086 memset(used, 0, usableSize);
|
|
1087 for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1;
|
|
1088 nFree = 0;
|
|
1089 pc = get2byte(&data[hdr+1]);
|
|
1090 while( pc ){
|
|
1091 int size;
|
|
1092 assert( pc>0 && pc<usableSize-4 );
|
|
1093 size = get2byte(&data[pc+2]);
|
|
1094 assert( pc+size<=usableSize );
|
|
1095 nFree += size;
|
|
1096 for(i=pc; i<pc+size; i++){
|
|
1097 assert( used[i]==0 );
|
|
1098 used[i] = 1;
|
|
1099 }
|
|
1100 pc = get2byte(&data[pc]);
|
|
1101 }
|
|
1102 idx = 0;
|
|
1103 nCell = get2byte(&data[hdr+3]);
|
|
1104 cellLimit = get2byte(&data[hdr+5]);
|
|
1105 assert( pPage->isInit==0
|
|
1106 || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) );
|
|
1107 cellOffset = pPage->cellOffset;
|
|
1108 for(i=0; i<nCell; i++){
|
|
1109 int size;
|
|
1110 pc = get2byte(&data[cellOffset+2*i]);
|
|
1111 assert( pc>0 && pc<usableSize-4 );
|
|
1112 size = cellSize(pPage, &data[pc]);
|
|
1113 assert( pc+size<=usableSize );
|
|
1114 for(j=pc; j<pc+size; j++){
|
|
1115 assert( used[j]==0 );
|
|
1116 used[j] = 1;
|
|
1117 }
|
|
1118 }
|
|
1119 for(i=cellOffset+2*nCell; i<cellimit; i++){
|
|
1120 assert( used[i]==0 );
|
|
1121 used[i] = 1;
|
|
1122 }
|
|
1123 nFree = 0;
|
|
1124 for(i=0; i<usableSize; i++){
|
|
1125 assert( used[i]<=1 );
|
|
1126 if( used[i]==0 ) nFree++;
|
|
1127 }
|
|
1128 assert( nFree==data[hdr+7] );
|
|
1129 sqliteFree(used);
|
|
1130 }
|
|
1131 #define pageIntegrity(X) _pageIntegrity(X)
|
|
1132 #else
|
|
1133 # define pageIntegrity(X)
|
|
1134 #endif
|
|
1135
|
|
1136 /* A bunch of assert() statements to check the transaction state variables
|
|
1137 ** of handle p (type Btree*) are internally consistent.
|
|
1138 */
|
|
1139 #define btreeIntegrity(p) \
|
|
1140 assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \
|
|
1141 assert( p->pBt->nTransaction<=p->pBt->nRef ); \
|
|
1142 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
|
|
1143 assert( p->pBt->inTransaction>=p->inTrans );
|
|
1144
|
|
1145 /*
|
|
1146 ** Defragment the page given. All Cells are moved to the
|
|
1147 ** end of the page and all free space is collected into one
|
|
1148 ** big FreeBlk that occurs in between the header and cell
|
|
1149 ** pointer array and the cell content area.
|
|
1150 */
|
|
1151 static int defragmentPage(MemPage *pPage){
|
|
1152 int i; /* Loop counter */
|
|
1153 int pc; /* Address of a i-th cell */
|
|
1154 int addr; /* Offset of first byte after cell pointer array */
|
|
1155 int hdr; /* Offset to the page header */
|
|
1156 int size; /* Size of a cell */
|
|
1157 int usableSize; /* Number of usable bytes on a page */
|
|
1158 int cellOffset; /* Offset to the cell pointer array */
|
|
1159 int brk; /* Offset to the cell content area */
|
|
1160 int nCell; /* Number of cells on the page */
|
|
1161 unsigned char *data; /* The page data */
|
|
1162 unsigned char *temp; /* Temp area for cell content */
|
|
1163
|
|
1164 assert( sqlite3pager_iswriteable(pPage->aData) );
|
|
1165 assert( pPage->pBt!=0 );
|
|
1166 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
|
|
1167 assert( pPage->nOverflow==0 );
|
|
1168 temp = sqliteMalloc( pPage->pBt->pageSize );
|
|
1169 if( temp==0 ) return SQLITE_NOMEM;
|
|
1170 data = pPage->aData;
|
|
1171 hdr = pPage->hdrOffset;
|
|
1172 cellOffset = pPage->cellOffset;
|
|
1173 nCell = pPage->nCell;
|
|
1174 assert( nCell==get2byte(&data[hdr+3]) );
|
|
1175 usableSize = pPage->pBt->usableSize;
|
|
1176 brk = get2byte(&data[hdr+5]);
|
|
1177 memcpy(&temp[brk], &data[brk], usableSize - brk);
|
|
1178 brk = usableSize;
|
|
1179 for(i=0; i<nCell; i++){
|
|
1180 u8 *pAddr; /* The i-th cell pointer */
|
|
1181 pAddr = &data[cellOffset + i*2];
|
|
1182 pc = get2byte(pAddr);
|
|
1183 assert( pc<pPage->pBt->usableSize );
|
|
1184 size = cellSizePtr(pPage, &temp[pc]);
|
|
1185 brk -= size;
|
|
1186 memcpy(&data[brk], &temp[pc], size);
|
|
1187 put2byte(pAddr, brk);
|
|
1188 }
|
|
1189 assert( brk>=cellOffset+2*nCell );
|
|
1190 put2byte(&data[hdr+5], brk);
|
|
1191 data[hdr+1] = 0;
|
|
1192 data[hdr+2] = 0;
|
|
1193 data[hdr+7] = 0;
|
|
1194 addr = cellOffset+2*nCell;
|
|
1195 memset(&data[addr], 0, brk-addr);
|
|
1196 sqliteFree(temp);
|
|
1197 return SQLITE_OK;
|
|
1198 }
|
|
1199
|
|
1200 /*
|
|
1201 ** Allocate nByte bytes of space on a page.
|
|
1202 **
|
|
1203 ** Return the index into pPage->aData[] of the first byte of
|
|
1204 ** the new allocation. Or return 0 if there is not enough free
|
|
1205 ** space on the page to satisfy the allocation request.
|
|
1206 **
|
|
1207 ** If the page contains nBytes of free space but does not contain
|
|
1208 ** nBytes of contiguous free space, then this routine automatically
|
|
1209 ** calls defragementPage() to consolidate all free space before
|
|
1210 ** allocating the new chunk.
|
|
1211 */
|
|
1212 static int allocateSpace(MemPage *pPage, int nByte){
|
|
1213 int addr, pc, hdr;
|
|
1214 int size;
|
|
1215 int nFrag;
|
|
1216 int top;
|
|
1217 int nCell;
|
|
1218 int cellOffset;
|
|
1219 unsigned char *data;
|
|
1220
|
|
1221 data = pPage->aData;
|
|
1222 assert( sqlite3pager_iswriteable(data) );
|
|
1223 assert( pPage->pBt );
|
|
1224 if( nByte<4 ) nByte = 4;
|
|
1225 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
|
|
1226 pPage->nFree -= nByte;
|
|
1227 hdr = pPage->hdrOffset;
|
|
1228
|
|
1229 nFrag = data[hdr+7];
|
|
1230 if( nFrag<60 ){
|
|
1231 /* Search the freelist looking for a slot big enough to satisfy the
|
|
1232 ** space request. */
|
|
1233 addr = hdr+1;
|
|
1234 while( (pc = get2byte(&data[addr]))>0 ){
|
|
1235 size = get2byte(&data[pc+2]);
|
|
1236 if( size>=nByte ){
|
|
1237 if( size<nByte+4 ){
|
|
1238 memcpy(&data[addr], &data[pc], 2);
|
|
1239 data[hdr+7] = nFrag + size - nByte;
|
|
1240 return pc;
|
|
1241 }else{
|
|
1242 put2byte(&data[pc+2], size-nByte);
|
|
1243 return pc + size - nByte;
|
|
1244 }
|
|
1245 }
|
|
1246 addr = pc;
|
|
1247 }
|
|
1248 }
|
|
1249
|
|
1250 /* Allocate memory from the gap in between the cell pointer array
|
|
1251 ** and the cell content area.
|
|
1252 */
|
|
1253 top = get2byte(&data[hdr+5]);
|
|
1254 nCell = get2byte(&data[hdr+3]);
|
|
1255 cellOffset = pPage->cellOffset;
|
|
1256 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
|
|
1257 if( defragmentPage(pPage) ) return 0;
|
|
1258 top = get2byte(&data[hdr+5]);
|
|
1259 }
|
|
1260 top -= nByte;
|
|
1261 assert( cellOffset + 2*nCell <= top );
|
|
1262 put2byte(&data[hdr+5], top);
|
|
1263 return top;
|
|
1264 }
|
|
1265
|
|
1266 /*
|
|
1267 ** Return a section of the pPage->aData to the freelist.
|
|
1268 ** The first byte of the new free block is pPage->aDisk[start]
|
|
1269 ** and the size of the block is "size" bytes.
|
|
1270 **
|
|
1271 ** Most of the effort here is involved in coalesing adjacent
|
|
1272 ** free blocks into a single big free block.
|
|
1273 */
|
|
1274 static void freeSpace(MemPage *pPage, int start, int size){
|
|
1275 int addr, pbegin, hdr;
|
|
1276 unsigned char *data = pPage->aData;
|
|
1277
|
|
1278 assert( pPage->pBt!=0 );
|
|
1279 assert( sqlite3pager_iswriteable(data) );
|
|
1280 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
|
|
1281 assert( (start + size)<=pPage->pBt->usableSize );
|
|
1282 if( size<4 ) size = 4;
|
|
1283
|
|
1284 #ifdef SQLITE_SECURE_DELETE
|
|
1285 /* Overwrite deleted information with zeros when the SECURE_DELETE
|
|
1286 ** option is enabled at compile-time */
|
|
1287 memset(&data[start], 0, size);
|
|
1288 #endif
|
|
1289
|
|
1290 /* Add the space back into the linked list of freeblocks */
|
|
1291 hdr = pPage->hdrOffset;
|
|
1292 addr = hdr + 1;
|
|
1293 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
|
|
1294 assert( pbegin<=pPage->pBt->usableSize-4 );
|
|
1295 assert( pbegin>addr );
|
|
1296 addr = pbegin;
|
|
1297 }
|
|
1298 assert( pbegin<=pPage->pBt->usableSize-4 );
|
|
1299 assert( pbegin>addr || pbegin==0 );
|
|
1300 put2byte(&data[addr], start);
|
|
1301 put2byte(&data[start], pbegin);
|
|
1302 put2byte(&data[start+2], size);
|
|
1303 pPage->nFree += size;
|
|
1304
|
|
1305 /* Coalesce adjacent free blocks */
|
|
1306 addr = pPage->hdrOffset + 1;
|
|
1307 while( (pbegin = get2byte(&data[addr]))>0 ){
|
|
1308 int pnext, psize;
|
|
1309 assert( pbegin>addr );
|
|
1310 assert( pbegin<=pPage->pBt->usableSize-4 );
|
|
1311 pnext = get2byte(&data[pbegin]);
|
|
1312 psize = get2byte(&data[pbegin+2]);
|
|
1313 if( pbegin + psize + 3 >= pnext && pnext>0 ){
|
|
1314 int frag = pnext - (pbegin+psize);
|
|
1315 assert( frag<=data[pPage->hdrOffset+7] );
|
|
1316 data[pPage->hdrOffset+7] -= frag;
|
|
1317 put2byte(&data[pbegin], get2byte(&data[pnext]));
|
|
1318 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
|
|
1319 }else{
|
|
1320 addr = pbegin;
|
|
1321 }
|
|
1322 }
|
|
1323
|
|
1324 /* If the cell content area begins with a freeblock, remove it. */
|
|
1325 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
|
|
1326 int top;
|
|
1327 pbegin = get2byte(&data[hdr+1]);
|
|
1328 memcpy(&data[hdr+1], &data[pbegin], 2);
|
|
1329 top = get2byte(&data[hdr+5]);
|
|
1330 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
|
|
1331 }
|
|
1332 }
|
|
1333
|
|
1334 /*
|
|
1335 ** Decode the flags byte (the first byte of the header) for a page
|
|
1336 ** and initialize fields of the MemPage structure accordingly.
|
|
1337 */
|
|
1338 static void decodeFlags(MemPage *pPage, int flagByte){
|
|
1339 BtShared *pBt; /* A copy of pPage->pBt */
|
|
1340
|
|
1341 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
|
|
1342 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
|
|
1343 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
|
|
1344 pPage->leaf = (flagByte & PTF_LEAF)!=0;
|
|
1345 pPage->childPtrSize = 4*(pPage->leaf==0);
|
|
1346 pBt = pPage->pBt;
|
|
1347 if( flagByte & PTF_LEAFDATA ){
|
|
1348 pPage->leafData = 1;
|
|
1349 pPage->maxLocal = pBt->maxLeaf;
|
|
1350 pPage->minLocal = pBt->minLeaf;
|
|
1351 }else{
|
|
1352 pPage->leafData = 0;
|
|
1353 pPage->maxLocal = pBt->maxLocal;
|
|
1354 pPage->minLocal = pBt->minLocal;
|
|
1355 }
|
|
1356 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
|
|
1357 }
|
|
1358
|
|
1359 /*
|
|
1360 ** Initialize the auxiliary information for a disk block.
|
|
1361 **
|
|
1362 ** The pParent parameter must be a pointer to the MemPage which
|
|
1363 ** is the parent of the page being initialized. The root of a
|
|
1364 ** BTree has no parent and so for that page, pParent==NULL.
|
|
1365 **
|
|
1366 ** Return SQLITE_OK on success. If we see that the page does
|
|
1367 ** not contain a well-formed database page, then return
|
|
1368 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
|
|
1369 ** guarantee that the page is well-formed. It only shows that
|
|
1370 ** we failed to detect any corruption.
|
|
1371 */
|
|
1372 static int initPage(
|
|
1373 MemPage *pPage, /* The page to be initialized */
|
|
1374 MemPage *pParent /* The parent. Might be NULL */
|
|
1375 ){
|
|
1376 int pc; /* Address of a freeblock within pPage->aData[] */
|
|
1377 int hdr; /* Offset to beginning of page header */
|
|
1378 u8 *data; /* Equal to pPage->aData */
|
|
1379 BtShared *pBt; /* The main btree structure */
|
|
1380 int usableSize; /* Amount of usable space on each page */
|
|
1381 int cellOffset; /* Offset from start of page to first cell pointer */
|
|
1382 int nFree; /* Number of unused bytes on the page */
|
|
1383 int top; /* First byte of the cell content area */
|
|
1384
|
|
1385 pBt = pPage->pBt;
|
|
1386 assert( pBt!=0 );
|
|
1387 assert( pParent==0 || pParent->pBt==pBt );
|
|
1388 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
|
|
1389 assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] );
|
|
1390 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
|
|
1391 /* The parent page should never change unless the file is corrupt */
|
|
1392 return SQLITE_CORRUPT_BKPT;
|
|
1393 }
|
|
1394 if( pPage->isInit ) return SQLITE_OK;
|
|
1395 if( pPage->pParent==0 && pParent!=0 ){
|
|
1396 pPage->pParent = pParent;
|
|
1397 sqlite3pager_ref(pParent->aData);
|
|
1398 }
|
|
1399 hdr = pPage->hdrOffset;
|
|
1400 data = pPage->aData;
|
|
1401 decodeFlags(pPage, data[hdr]);
|
|
1402 pPage->nOverflow = 0;
|
|
1403 pPage->idxShift = 0;
|
|
1404 usableSize = pBt->usableSize;
|
|
1405 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
|
|
1406 top = get2byte(&data[hdr+5]);
|
|
1407 pPage->nCell = get2byte(&data[hdr+3]);
|
|
1408 if( pPage->nCell>MX_CELL(pBt) ){
|
|
1409 /* To many cells for a single page. The page must be corrupt */
|
|
1410 return SQLITE_CORRUPT_BKPT;
|
|
1411 }
|
|
1412 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
|
|
1413 /* All pages must have at least one cell, except for root pages */
|
|
1414 return SQLITE_CORRUPT_BKPT;
|
|
1415 }
|
|
1416
|
|
1417 /* Compute the total free space on the page */
|
|
1418 pc = get2byte(&data[hdr+1]);
|
|
1419 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
|
|
1420 while( pc>0 ){
|
|
1421 int next, size;
|
|
1422 if( pc>usableSize-4 ){
|
|
1423 /* Free block is off the page */
|
|
1424 return SQLITE_CORRUPT_BKPT;
|
|
1425 }
|
|
1426 next = get2byte(&data[pc]);
|
|
1427 size = get2byte(&data[pc+2]);
|
|
1428 if( next>0 && next<=pc+size+3 ){
|
|
1429 /* Free blocks must be in accending order */
|
|
1430 return SQLITE_CORRUPT_BKPT;
|
|
1431 }
|
|
1432 nFree += size;
|
|
1433 pc = next;
|
|
1434 }
|
|
1435 pPage->nFree = nFree;
|
|
1436 if( nFree>=usableSize ){
|
|
1437 /* Free space cannot exceed total page size */
|
|
1438 return SQLITE_CORRUPT_BKPT;
|
|
1439 }
|
|
1440
|
|
1441 pPage->isInit = 1;
|
|
1442 pageIntegrity(pPage);
|
|
1443 return SQLITE_OK;
|
|
1444 }
|
|
1445
|
|
1446 /*
|
|
1447 ** Set up a raw page so that it looks like a database page holding
|
|
1448 ** no entries.
|
|
1449 */
|
|
1450 static void zeroPage(MemPage *pPage, int flags){
|
|
1451 unsigned char *data = pPage->aData;
|
|
1452 BtShared *pBt = pPage->pBt;
|
|
1453 int hdr = pPage->hdrOffset;
|
|
1454 int first;
|
|
1455
|
|
1456 assert( sqlite3pager_pagenumber(data)==pPage->pgno );
|
|
1457 assert( &data[pBt->pageSize] == (unsigned char*)pPage );
|
|
1458 assert( sqlite3pager_iswriteable(data) );
|
|
1459 memset(&data[hdr], 0, pBt->usableSize - hdr);
|
|
1460 data[hdr] = flags;
|
|
1461 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
|
|
1462 memset(&data[hdr+1], 0, 4);
|
|
1463 data[hdr+7] = 0;
|
|
1464 put2byte(&data[hdr+5], pBt->usableSize);
|
|
1465 pPage->nFree = pBt->usableSize - first;
|
|
1466 decodeFlags(pPage, flags);
|
|
1467 pPage->hdrOffset = hdr;
|
|
1468 pPage->cellOffset = first;
|
|
1469 pPage->nOverflow = 0;
|
|
1470 pPage->idxShift = 0;
|
|
1471 pPage->nCell = 0;
|
|
1472 pPage->isInit = 1;
|
|
1473 pageIntegrity(pPage);
|
|
1474 }
|
|
1475
|
|
1476 /*
|
|
1477 ** Get a page from the pager. Initialize the MemPage.pBt and
|
|
1478 ** MemPage.aData elements if needed.
|
|
1479 */
|
|
1480 static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage){
|
|
1481 int rc;
|
|
1482 unsigned char *aData;
|
|
1483 MemPage *pPage;
|
|
1484 rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData);
|
|
1485 if( rc ) return rc;
|
|
1486 pPage = (MemPage*)&aData[pBt->pageSize];
|
|
1487 pPage->aData = aData;
|
|
1488 pPage->pBt = pBt;
|
|
1489 pPage->pgno = pgno;
|
|
1490 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
|
|
1491 *ppPage = pPage;
|
|
1492 return SQLITE_OK;
|
|
1493 }
|
|
1494
|
|
1495 /*
|
|
1496 ** Get a page from the pager and initialize it. This routine
|
|
1497 ** is just a convenience wrapper around separate calls to
|
|
1498 ** getPage() and initPage().
|
|
1499 */
|
|
1500 static int getAndInitPage(
|
|
1501 BtShared *pBt, /* The database file */
|
|
1502 Pgno pgno, /* Number of the page to get */
|
|
1503 MemPage **ppPage, /* Write the page pointer here */
|
|
1504 MemPage *pParent /* Parent of the page */
|
|
1505 ){
|
|
1506 int rc;
|
|
1507 if( pgno==0 ){
|
|
1508 return SQLITE_CORRUPT_BKPT;
|
|
1509 }
|
|
1510 rc = getPage(pBt, pgno, ppPage);
|
|
1511 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
|
|
1512 rc = initPage(*ppPage, pParent);
|
|
1513 }
|
|
1514 return rc;
|
|
1515 }
|
|
1516
|
|
1517 /*
|
|
1518 ** Release a MemPage. This should be called once for each prior
|
|
1519 ** call to getPage.
|
|
1520 */
|
|
1521 static void releasePage(MemPage *pPage){
|
|
1522 if( pPage ){
|
|
1523 assert( pPage->aData );
|
|
1524 assert( pPage->pBt );
|
|
1525 assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage );
|
|
1526 sqlite3pager_unref(pPage->aData);
|
|
1527 }
|
|
1528 }
|
|
1529
|
|
1530 /*
|
|
1531 ** This routine is called when the reference count for a page
|
|
1532 ** reaches zero. We need to unref the pParent pointer when that
|
|
1533 ** happens.
|
|
1534 */
|
|
1535 static void pageDestructor(void *pData, int pageSize){
|
|
1536 MemPage *pPage;
|
|
1537 assert( (pageSize & 7)==0 );
|
|
1538 pPage = (MemPage*)&((char*)pData)[pageSize];
|
|
1539 if( pPage->pParent ){
|
|
1540 MemPage *pParent = pPage->pParent;
|
|
1541 pPage->pParent = 0;
|
|
1542 releasePage(pParent);
|
|
1543 }
|
|
1544 pPage->isInit = 0;
|
|
1545 }
|
|
1546
|
|
1547 /*
|
|
1548 ** During a rollback, when the pager reloads information into the cache
|
|
1549 ** so that the cache is restored to its original state at the start of
|
|
1550 ** the transaction, for each page restored this routine is called.
|
|
1551 **
|
|
1552 ** This routine needs to reset the extra data section at the end of the
|
|
1553 ** page to agree with the restored data.
|
|
1554 */
|
|
1555 static void pageReinit(void *pData, int pageSize){
|
|
1556 MemPage *pPage;
|
|
1557 assert( (pageSize & 7)==0 );
|
|
1558 pPage = (MemPage*)&((char*)pData)[pageSize];
|
|
1559 if( pPage->isInit ){
|
|
1560 pPage->isInit = 0;
|
|
1561 initPage(pPage, pPage->pParent);
|
|
1562 }
|
|
1563 }
|
|
1564
|
|
1565 /*
|
|
1566 ** Open a database file.
|
|
1567 **
|
|
1568 ** zFilename is the name of the database file. If zFilename is NULL
|
|
1569 ** a new database with a random name is created. This randomly named
|
|
1570 ** database file will be deleted when sqlite3BtreeClose() is called.
|
|
1571 */
|
|
1572 int sqlite3BtreeOpen(
|
|
1573 const char *zFilename, /* Name of the file containing the BTree database */
|
|
1574 sqlite3 *pSqlite, /* Associated database handle */
|
|
1575 Btree **ppBtree, /* Pointer to new Btree object written here */
|
|
1576 int flags /* Options */
|
|
1577 ){
|
|
1578 BtShared *pBt; /* Shared part of btree structure */
|
|
1579 Btree *p; /* Handle to return */
|
|
1580 int rc;
|
|
1581 int nReserve;
|
|
1582 unsigned char zDbHeader[100];
|
|
1583 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
|
|
1584 const ThreadData *pTsdro;
|
|
1585 #endif
|
|
1586
|
|
1587 /* Set the variable isMemdb to true for an in-memory database, or
|
|
1588 ** false for a file-based database. This symbol is only required if
|
|
1589 ** either of the shared-data or autovacuum features are compiled
|
|
1590 ** into the library.
|
|
1591 */
|
|
1592 #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
|
|
1593 #ifdef SQLITE_OMIT_MEMORYDB
|
|
1594 const int isMemdb = !zFilename;
|
|
1595 #else
|
|
1596 const int isMemdb = !zFilename || (strcmp(zFilename, ":memory:")?0:1);
|
|
1597 #endif
|
|
1598 #endif
|
|
1599
|
|
1600 p = sqliteMalloc(sizeof(Btree));
|
|
1601 if( !p ){
|
|
1602 return SQLITE_NOMEM;
|
|
1603 }
|
|
1604 p->inTrans = TRANS_NONE;
|
|
1605 p->pSqlite = pSqlite;
|
|
1606
|
|
1607 /* Try to find an existing Btree structure opened on zFilename. */
|
|
1608 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
|
|
1609 pTsdro = sqlite3ThreadDataReadOnly();
|
|
1610 if( pTsdro->useSharedData && zFilename && !isMemdb ){
|
|
1611 char *zFullPathname = sqlite3OsFullPathname(zFilename);
|
|
1612 if( !zFullPathname ){
|
|
1613 sqliteFree(p);
|
|
1614 return SQLITE_NOMEM;
|
|
1615 }
|
|
1616 for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){
|
|
1617 assert( pBt->nRef>0 );
|
|
1618 if( 0==strcmp(zFullPathname, sqlite3pager_filename(pBt->pPager)) ){
|
|
1619 p->pBt = pBt;
|
|
1620 *ppBtree = p;
|
|
1621 pBt->nRef++;
|
|
1622 sqliteFree(zFullPathname);
|
|
1623 return SQLITE_OK;
|
|
1624 }
|
|
1625 }
|
|
1626 sqliteFree(zFullPathname);
|
|
1627 }
|
|
1628 #endif
|
|
1629
|
|
1630 /*
|
|
1631 ** The following asserts make sure that structures used by the btree are
|
|
1632 ** the right size. This is to guard against size changes that result
|
|
1633 ** when compiling on a different architecture.
|
|
1634 */
|
|
1635 assert( sizeof(i64)==8 || sizeof(i64)==4 );
|
|
1636 assert( sizeof(u64)==8 || sizeof(u64)==4 );
|
|
1637 assert( sizeof(u32)==4 );
|
|
1638 assert( sizeof(u16)==2 );
|
|
1639 assert( sizeof(Pgno)==4 );
|
|
1640
|
|
1641 pBt = sqliteMalloc( sizeof(*pBt) );
|
|
1642 if( pBt==0 ){
|
|
1643 *ppBtree = 0;
|
|
1644 sqliteFree(p);
|
|
1645 return SQLITE_NOMEM;
|
|
1646 }
|
|
1647 rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, flags);
|
|
1648 if( rc!=SQLITE_OK ){
|
|
1649 if( pBt->pPager ) sqlite3pager_close(pBt->pPager);
|
|
1650 sqliteFree(pBt);
|
|
1651 sqliteFree(p);
|
|
1652 *ppBtree = 0;
|
|
1653 return rc;
|
|
1654 }
|
|
1655 p->pBt = pBt;
|
|
1656
|
|
1657 sqlite3pager_set_destructor(pBt->pPager, pageDestructor);
|
|
1658 sqlite3pager_set_reiniter(pBt->pPager, pageReinit);
|
|
1659 pBt->pCursor = 0;
|
|
1660 pBt->pPage1 = 0;
|
|
1661 pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager);
|
|
1662 sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader);
|
|
1663 pBt->pageSize = get2byte(&zDbHeader[16]);
|
|
1664 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
|
|
1665 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
|
|
1666 pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
|
|
1667 pBt->maxEmbedFrac = 64; /* 25% */
|
|
1668 pBt->minEmbedFrac = 32; /* 12.5% */
|
|
1669 pBt->minLeafFrac = 32; /* 12.5% */
|
|
1670 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
1671 /* If the magic name ":memory:" will create an in-memory database, then
|
|
1672 ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM
|
|
1673 ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined,
|
|
1674 ** then ":memory:" is just a regular file-name. Respect the auto-vacuum
|
|
1675 ** default in this case.
|
|
1676 */
|
|
1677 if( zFilename && !isMemdb ){
|
|
1678 pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM;
|
|
1679 }
|
|
1680 #endif
|
|
1681 nReserve = 0;
|
|
1682 }else{
|
|
1683 nReserve = zDbHeader[20];
|
|
1684 pBt->maxEmbedFrac = zDbHeader[21];
|
|
1685 pBt->minEmbedFrac = zDbHeader[22];
|
|
1686 pBt->minLeafFrac = zDbHeader[23];
|
|
1687 pBt->pageSizeFixed = 1;
|
|
1688 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
1689 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
|
|
1690 #endif
|
|
1691 }
|
|
1692 pBt->usableSize = pBt->pageSize - nReserve;
|
|
1693 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
|
|
1694 sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
|
|
1695
|
|
1696 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
|
|
1697 /* Add the new btree to the linked list starting at ThreadData.pBtree.
|
|
1698 ** There is no chance that a malloc() may fail inside of the
|
|
1699 ** sqlite3ThreadData() call, as the ThreadData structure must have already
|
|
1700 ** been allocated for pTsdro->useSharedData to be non-zero.
|
|
1701 */
|
|
1702 if( pTsdro->useSharedData && zFilename && !isMemdb ){
|
|
1703 pBt->pNext = pTsdro->pBtree;
|
|
1704 sqlite3ThreadData()->pBtree = pBt;
|
|
1705 }
|
|
1706 #endif
|
|
1707 pBt->nRef = 1;
|
|
1708 *ppBtree = p;
|
|
1709 return SQLITE_OK;
|
|
1710 }
|
|
1711
|
|
1712 /*
|
|
1713 ** Close an open database and invalidate all cursors.
|
|
1714 */
|
|
1715 int sqlite3BtreeClose(Btree *p){
|
|
1716 BtShared *pBt = p->pBt;
|
|
1717 BtCursor *pCur;
|
|
1718
|
|
1719 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
1720 ThreadData *pTsd;
|
|
1721 #endif
|
|
1722
|
|
1723 /* Close all cursors opened via this handle. */
|
|
1724 pCur = pBt->pCursor;
|
|
1725 while( pCur ){
|
|
1726 BtCursor *pTmp = pCur;
|
|
1727 pCur = pCur->pNext;
|
|
1728 if( pTmp->pBtree==p ){
|
|
1729 sqlite3BtreeCloseCursor(pTmp);
|
|
1730 }
|
|
1731 }
|
|
1732
|
|
1733 /* Rollback any active transaction and free the handle structure.
|
|
1734 ** The call to sqlite3BtreeRollback() drops any table-locks held by
|
|
1735 ** this handle.
|
|
1736 */
|
|
1737 sqlite3BtreeRollback(p);
|
|
1738 sqliteFree(p);
|
|
1739
|
|
1740 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
1741 /* If there are still other outstanding references to the shared-btree
|
|
1742 ** structure, return now. The remainder of this procedure cleans
|
|
1743 ** up the shared-btree.
|
|
1744 */
|
|
1745 assert( pBt->nRef>0 );
|
|
1746 pBt->nRef--;
|
|
1747 if( pBt->nRef ){
|
|
1748 return SQLITE_OK;
|
|
1749 }
|
|
1750
|
|
1751 /* Remove the shared-btree from the thread wide list. Call
|
|
1752 ** ThreadDataReadOnly() and then cast away the const property of the
|
|
1753 ** pointer to avoid allocating thread data if it is not really required.
|
|
1754 */
|
|
1755 pTsd = (ThreadData *)sqlite3ThreadDataReadOnly();
|
|
1756 if( pTsd->pBtree==pBt ){
|
|
1757 assert( pTsd==sqlite3ThreadData() );
|
|
1758 pTsd->pBtree = pBt->pNext;
|
|
1759 }else{
|
|
1760 BtShared *pPrev;
|
|
1761 for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){}
|
|
1762 if( pPrev ){
|
|
1763 assert( pTsd==sqlite3ThreadData() );
|
|
1764 pPrev->pNext = pBt->pNext;
|
|
1765 }
|
|
1766 }
|
|
1767 #endif
|
|
1768
|
|
1769 /* Close the pager and free the shared-btree structure */
|
|
1770 assert( !pBt->pCursor );
|
|
1771 sqlite3pager_close(pBt->pPager);
|
|
1772 if( pBt->xFreeSchema && pBt->pSchema ){
|
|
1773 pBt->xFreeSchema(pBt->pSchema);
|
|
1774 }
|
|
1775 sqliteFree(pBt->pSchema);
|
|
1776 sqliteFree(pBt);
|
|
1777 return SQLITE_OK;
|
|
1778 }
|
|
1779
|
|
1780 /*
|
|
1781 ** Change the busy handler callback function.
|
|
1782 */
|
|
1783 int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
|
|
1784 BtShared *pBt = p->pBt;
|
|
1785 pBt->pBusyHandler = pHandler;
|
|
1786 sqlite3pager_set_busyhandler(pBt->pPager, pHandler);
|
|
1787 return SQLITE_OK;
|
|
1788 }
|
|
1789
|
|
1790 /*
|
|
1791 ** Change the limit on the number of pages allowed in the cache.
|
|
1792 **
|
|
1793 ** The maximum number of cache pages is set to the absolute
|
|
1794 ** value of mxPage. If mxPage is negative, the pager will
|
|
1795 ** operate asynchronously - it will not stop to do fsync()s
|
|
1796 ** to insure data is written to the disk surface before
|
|
1797 ** continuing. Transactions still work if synchronous is off,
|
|
1798 ** and the database cannot be corrupted if this program
|
|
1799 ** crashes. But if the operating system crashes or there is
|
|
1800 ** an abrupt power failure when synchronous is off, the database
|
|
1801 ** could be left in an inconsistent and unrecoverable state.
|
|
1802 ** Synchronous is on by default so database corruption is not
|
|
1803 ** normally a worry.
|
|
1804 */
|
|
1805 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
|
|
1806 BtShared *pBt = p->pBt;
|
|
1807 sqlite3pager_set_cachesize(pBt->pPager, mxPage);
|
|
1808 return SQLITE_OK;
|
|
1809 }
|
|
1810
|
|
1811 /*
|
|
1812 ** Change the way data is synced to disk in order to increase or decrease
|
|
1813 ** how well the database resists damage due to OS crashes and power
|
|
1814 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
|
|
1815 ** there is a high probability of damage) Level 2 is the default. There
|
|
1816 ** is a very low but non-zero probability of damage. Level 3 reduces the
|
|
1817 ** probability of damage to near zero but with a write performance reduction.
|
|
1818 */
|
|
1819 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
|
|
1820 int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
|
|
1821 BtShared *pBt = p->pBt;
|
|
1822 sqlite3pager_set_safety_level(pBt->pPager, level, fullSync);
|
|
1823 return SQLITE_OK;
|
|
1824 }
|
|
1825 #endif
|
|
1826
|
|
1827 /*
|
|
1828 ** Return TRUE if the given btree is set to safety level 1. In other
|
|
1829 ** words, return TRUE if no sync() occurs on the disk files.
|
|
1830 */
|
|
1831 int sqlite3BtreeSyncDisabled(Btree *p){
|
|
1832 BtShared *pBt = p->pBt;
|
|
1833 assert( pBt && pBt->pPager );
|
|
1834 return sqlite3pager_nosync(pBt->pPager);
|
|
1835 }
|
|
1836
|
|
1837 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
|
|
1838 /*
|
|
1839 ** Change the default pages size and the number of reserved bytes per page.
|
|
1840 **
|
|
1841 ** The page size must be a power of 2 between 512 and 65536. If the page
|
|
1842 ** size supplied does not meet this constraint then the page size is not
|
|
1843 ** changed.
|
|
1844 **
|
|
1845 ** Page sizes are constrained to be a power of two so that the region
|
|
1846 ** of the database file used for locking (beginning at PENDING_BYTE,
|
|
1847 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
|
|
1848 ** at the beginning of a page.
|
|
1849 **
|
|
1850 ** If parameter nReserve is less than zero, then the number of reserved
|
|
1851 ** bytes per page is left unchanged.
|
|
1852 */
|
|
1853 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
|
|
1854 BtShared *pBt = p->pBt;
|
|
1855 if( pBt->pageSizeFixed ){
|
|
1856 return SQLITE_READONLY;
|
|
1857 }
|
|
1858 if( nReserve<0 ){
|
|
1859 nReserve = pBt->pageSize - pBt->usableSize;
|
|
1860 }
|
|
1861 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
|
|
1862 ((pageSize-1)&pageSize)==0 ){
|
|
1863 assert( (pageSize & 7)==0 );
|
|
1864 assert( !pBt->pPage1 && !pBt->pCursor );
|
|
1865 pBt->pageSize = sqlite3pager_set_pagesize(pBt->pPager, pageSize);
|
|
1866 }
|
|
1867 pBt->usableSize = pBt->pageSize - nReserve;
|
|
1868 return SQLITE_OK;
|
|
1869 }
|
|
1870
|
|
1871 /*
|
|
1872 ** Return the currently defined page size
|
|
1873 */
|
|
1874 int sqlite3BtreeGetPageSize(Btree *p){
|
|
1875 return p->pBt->pageSize;
|
|
1876 }
|
|
1877 int sqlite3BtreeGetReserve(Btree *p){
|
|
1878 return p->pBt->pageSize - p->pBt->usableSize;
|
|
1879 }
|
|
1880 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
|
|
1881
|
|
1882 /*
|
|
1883 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
|
|
1884 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
|
|
1885 ** is disabled. The default value for the auto-vacuum property is
|
|
1886 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
|
|
1887 */
|
|
1888 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
|
|
1889 BtShared *pBt = p->pBt;;
|
|
1890 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
1891 return SQLITE_READONLY;
|
|
1892 #else
|
|
1893 if( pBt->pageSizeFixed ){
|
|
1894 return SQLITE_READONLY;
|
|
1895 }
|
|
1896 pBt->autoVacuum = (autoVacuum?1:0);
|
|
1897 return SQLITE_OK;
|
|
1898 #endif
|
|
1899 }
|
|
1900
|
|
1901 /*
|
|
1902 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
|
|
1903 ** enabled 1 is returned. Otherwise 0.
|
|
1904 */
|
|
1905 int sqlite3BtreeGetAutoVacuum(Btree *p){
|
|
1906 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
1907 return 0;
|
|
1908 #else
|
|
1909 return p->pBt->autoVacuum;
|
|
1910 #endif
|
|
1911 }
|
|
1912
|
|
1913
|
|
1914 /*
|
|
1915 ** Get a reference to pPage1 of the database file. This will
|
|
1916 ** also acquire a readlock on that file.
|
|
1917 **
|
|
1918 ** SQLITE_OK is returned on success. If the file is not a
|
|
1919 ** well-formed database file, then SQLITE_CORRUPT is returned.
|
|
1920 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
|
|
1921 ** is returned if we run out of memory. SQLITE_PROTOCOL is returned
|
|
1922 ** if there is a locking protocol violation.
|
|
1923 */
|
|
1924 static int lockBtree(BtShared *pBt){
|
|
1925 int rc, pageSize;
|
|
1926 MemPage *pPage1;
|
|
1927 if( pBt->pPage1 ) return SQLITE_OK;
|
|
1928 rc = getPage(pBt, 1, &pPage1);
|
|
1929 if( rc!=SQLITE_OK ) return rc;
|
|
1930
|
|
1931
|
|
1932 /* Do some checking to help insure the file we opened really is
|
|
1933 ** a valid database file.
|
|
1934 */
|
|
1935 rc = SQLITE_NOTADB;
|
|
1936 if( sqlite3pager_pagecount(pBt->pPager)>0 ){
|
|
1937 u8 *page1 = pPage1->aData;
|
|
1938 if( memcmp(page1, zMagicHeader, 16)!=0 ){
|
|
1939 goto page1_init_failed;
|
|
1940 }
|
|
1941 if( page1[18]>1 || page1[19]>1 ){
|
|
1942 goto page1_init_failed;
|
|
1943 }
|
|
1944 pageSize = get2byte(&page1[16]);
|
|
1945 if( ((pageSize-1)&pageSize)!=0 ){
|
|
1946 goto page1_init_failed;
|
|
1947 }
|
|
1948 assert( (pageSize & 7)==0 );
|
|
1949 pBt->pageSize = pageSize;
|
|
1950 pBt->usableSize = pageSize - page1[20];
|
|
1951 if( pBt->usableSize<500 ){
|
|
1952 goto page1_init_failed;
|
|
1953 }
|
|
1954 pBt->maxEmbedFrac = page1[21];
|
|
1955 pBt->minEmbedFrac = page1[22];
|
|
1956 pBt->minLeafFrac = page1[23];
|
|
1957 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
1958 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
|
|
1959 #endif
|
|
1960 }
|
|
1961
|
|
1962 /* maxLocal is the maximum amount of payload to store locally for
|
|
1963 ** a cell. Make sure it is small enough so that at least minFanout
|
|
1964 ** cells can will fit on one page. We assume a 10-byte page header.
|
|
1965 ** Besides the payload, the cell must store:
|
|
1966 ** 2-byte pointer to the cell
|
|
1967 ** 4-byte child pointer
|
|
1968 ** 9-byte nKey value
|
|
1969 ** 4-byte nData value
|
|
1970 ** 4-byte overflow page pointer
|
|
1971 ** So a cell consists of a 2-byte poiner, a header which is as much as
|
|
1972 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
|
|
1973 ** page pointer.
|
|
1974 */
|
|
1975 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
|
|
1976 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
|
|
1977 pBt->maxLeaf = pBt->usableSize - 35;
|
|
1978 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
|
|
1979 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
|
|
1980 goto page1_init_failed;
|
|
1981 }
|
|
1982 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
|
|
1983 pBt->pPage1 = pPage1;
|
|
1984 return SQLITE_OK;
|
|
1985
|
|
1986 page1_init_failed:
|
|
1987 releasePage(pPage1);
|
|
1988 pBt->pPage1 = 0;
|
|
1989 return rc;
|
|
1990 }
|
|
1991
|
|
1992 /*
|
|
1993 ** This routine works like lockBtree() except that it also invokes the
|
|
1994 ** busy callback if there is lock contention.
|
|
1995 */
|
|
1996 static int lockBtreeWithRetry(Btree *pRef){
|
|
1997 int rc = SQLITE_OK;
|
|
1998 if( pRef->inTrans==TRANS_NONE ){
|
|
1999 u8 inTransaction = pRef->pBt->inTransaction;
|
|
2000 btreeIntegrity(pRef);
|
|
2001 rc = sqlite3BtreeBeginTrans(pRef, 0);
|
|
2002 pRef->pBt->inTransaction = inTransaction;
|
|
2003 pRef->inTrans = TRANS_NONE;
|
|
2004 if( rc==SQLITE_OK ){
|
|
2005 pRef->pBt->nTransaction--;
|
|
2006 }
|
|
2007 btreeIntegrity(pRef);
|
|
2008 }
|
|
2009 return rc;
|
|
2010 }
|
|
2011
|
|
2012
|
|
2013 /*
|
|
2014 ** If there are no outstanding cursors and we are not in the middle
|
|
2015 ** of a transaction but there is a read lock on the database, then
|
|
2016 ** this routine unrefs the first page of the database file which
|
|
2017 ** has the effect of releasing the read lock.
|
|
2018 **
|
|
2019 ** If there are any outstanding cursors, this routine is a no-op.
|
|
2020 **
|
|
2021 ** If there is a transaction in progress, this routine is a no-op.
|
|
2022 */
|
|
2023 static void unlockBtreeIfUnused(BtShared *pBt){
|
|
2024 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
|
|
2025 if( pBt->pPage1->aData==0 ){
|
|
2026 MemPage *pPage = pBt->pPage1;
|
|
2027 pPage->aData = &((u8*)pPage)[-pBt->pageSize];
|
|
2028 pPage->pBt = pBt;
|
|
2029 pPage->pgno = 1;
|
|
2030 }
|
|
2031 releasePage(pBt->pPage1);
|
|
2032 pBt->pPage1 = 0;
|
|
2033 pBt->inStmt = 0;
|
|
2034 }
|
|
2035 }
|
|
2036
|
|
2037 /*
|
|
2038 ** Create a new database by initializing the first page of the
|
|
2039 ** file.
|
|
2040 */
|
|
2041 static int newDatabase(BtShared *pBt){
|
|
2042 MemPage *pP1;
|
|
2043 unsigned char *data;
|
|
2044 int rc;
|
|
2045 if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK;
|
|
2046 pP1 = pBt->pPage1;
|
|
2047 assert( pP1!=0 );
|
|
2048 data = pP1->aData;
|
|
2049 rc = sqlite3pager_write(data);
|
|
2050 if( rc ) return rc;
|
|
2051 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
|
|
2052 assert( sizeof(zMagicHeader)==16 );
|
|
2053 put2byte(&data[16], pBt->pageSize);
|
|
2054 data[18] = 1;
|
|
2055 data[19] = 1;
|
|
2056 data[20] = pBt->pageSize - pBt->usableSize;
|
|
2057 data[21] = pBt->maxEmbedFrac;
|
|
2058 data[22] = pBt->minEmbedFrac;
|
|
2059 data[23] = pBt->minLeafFrac;
|
|
2060 memset(&data[24], 0, 100-24);
|
|
2061 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
|
|
2062 pBt->pageSizeFixed = 1;
|
|
2063 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
2064 if( pBt->autoVacuum ){
|
|
2065 put4byte(&data[36 + 4*4], 1);
|
|
2066 }
|
|
2067 #endif
|
|
2068 return SQLITE_OK;
|
|
2069 }
|
|
2070
|
|
2071 /*
|
|
2072 ** Attempt to start a new transaction. A write-transaction
|
|
2073 ** is started if the second argument is nonzero, otherwise a read-
|
|
2074 ** transaction. If the second argument is 2 or more and exclusive
|
|
2075 ** transaction is started, meaning that no other process is allowed
|
|
2076 ** to access the database. A preexisting transaction may not be
|
|
2077 ** upgraded to exclusive by calling this routine a second time - the
|
|
2078 ** exclusivity flag only works for a new transaction.
|
|
2079 **
|
|
2080 ** A write-transaction must be started before attempting any
|
|
2081 ** changes to the database. None of the following routines
|
|
2082 ** will work unless a transaction is started first:
|
|
2083 **
|
|
2084 ** sqlite3BtreeCreateTable()
|
|
2085 ** sqlite3BtreeCreateIndex()
|
|
2086 ** sqlite3BtreeClearTable()
|
|
2087 ** sqlite3BtreeDropTable()
|
|
2088 ** sqlite3BtreeInsert()
|
|
2089 ** sqlite3BtreeDelete()
|
|
2090 ** sqlite3BtreeUpdateMeta()
|
|
2091 **
|
|
2092 ** If an initial attempt to acquire the lock fails because of lock contention
|
|
2093 ** and the database was previously unlocked, then invoke the busy handler
|
|
2094 ** if there is one. But if there was previously a read-lock, do not
|
|
2095 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
|
|
2096 ** returned when there is already a read-lock in order to avoid a deadlock.
|
|
2097 **
|
|
2098 ** Suppose there are two processes A and B. A has a read lock and B has
|
|
2099 ** a reserved lock. B tries to promote to exclusive but is blocked because
|
|
2100 ** of A's read lock. A tries to promote to reserved but is blocked by B.
|
|
2101 ** One or the other of the two processes must give way or there can be
|
|
2102 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback
|
|
2103 ** when A already has a read lock, we encourage A to give up and let B
|
|
2104 ** proceed.
|
|
2105 */
|
|
2106 int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
|
|
2107 BtShared *pBt = p->pBt;
|
|
2108 int rc = SQLITE_OK;
|
|
2109
|
|
2110 btreeIntegrity(p);
|
|
2111
|
|
2112 /* If the btree is already in a write-transaction, or it
|
|
2113 ** is already in a read-transaction and a read-transaction
|
|
2114 ** is requested, this is a no-op.
|
|
2115 */
|
|
2116 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
|
|
2117 return SQLITE_OK;
|
|
2118 }
|
|
2119
|
|
2120 /* Write transactions are not possible on a read-only database */
|
|
2121 if( pBt->readOnly && wrflag ){
|
|
2122 return SQLITE_READONLY;
|
|
2123 }
|
|
2124
|
|
2125 /* If another database handle has already opened a write transaction
|
|
2126 ** on this shared-btree structure and a second write transaction is
|
|
2127 ** requested, return SQLITE_BUSY.
|
|
2128 */
|
|
2129 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
|
|
2130 return SQLITE_BUSY;
|
|
2131 }
|
|
2132
|
|
2133 do {
|
|
2134 if( pBt->pPage1==0 ){
|
|
2135 rc = lockBtree(pBt);
|
|
2136 }
|
|
2137
|
|
2138 if( rc==SQLITE_OK && wrflag ){
|
|
2139 rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1);
|
|
2140 if( rc==SQLITE_OK ){
|
|
2141 rc = newDatabase(pBt);
|
|
2142 }
|
|
2143 }
|
|
2144
|
|
2145 if( rc==SQLITE_OK ){
|
|
2146 if( wrflag ) pBt->inStmt = 0;
|
|
2147 }else{
|
|
2148 unlockBtreeIfUnused(pBt);
|
|
2149 }
|
|
2150 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
|
|
2151 sqlite3InvokeBusyHandler(pBt->pBusyHandler) );
|
|
2152
|
|
2153 if( rc==SQLITE_OK ){
|
|
2154 if( p->inTrans==TRANS_NONE ){
|
|
2155 pBt->nTransaction++;
|
|
2156 }
|
|
2157 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
|
|
2158 if( p->inTrans>pBt->inTransaction ){
|
|
2159 pBt->inTransaction = p->inTrans;
|
|
2160 }
|
|
2161 }
|
|
2162
|
|
2163 btreeIntegrity(p);
|
|
2164 return rc;
|
|
2165 }
|
|
2166
|
|
2167 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
2168
|
|
2169 /*
|
|
2170 ** Set the pointer-map entries for all children of page pPage. Also, if
|
|
2171 ** pPage contains cells that point to overflow pages, set the pointer
|
|
2172 ** map entries for the overflow pages as well.
|
|
2173 */
|
|
2174 static int setChildPtrmaps(MemPage *pPage){
|
|
2175 int i; /* Counter variable */
|
|
2176 int nCell; /* Number of cells in page pPage */
|
|
2177 int rc = SQLITE_OK; /* Return code */
|
|
2178 BtShared *pBt = pPage->pBt;
|
|
2179 int isInitOrig = pPage->isInit;
|
|
2180 Pgno pgno = pPage->pgno;
|
|
2181
|
|
2182 initPage(pPage, 0);
|
|
2183 nCell = pPage->nCell;
|
|
2184
|
|
2185 for(i=0; i<nCell; i++){
|
|
2186 u8 *pCell = findCell(pPage, i);
|
|
2187
|
|
2188 rc = ptrmapPutOvflPtr(pPage, pCell);
|
|
2189 if( rc!=SQLITE_OK ){
|
|
2190 goto set_child_ptrmaps_out;
|
|
2191 }
|
|
2192
|
|
2193 if( !pPage->leaf ){
|
|
2194 Pgno childPgno = get4byte(pCell);
|
|
2195 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
|
|
2196 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
|
|
2197 }
|
|
2198 }
|
|
2199
|
|
2200 if( !pPage->leaf ){
|
|
2201 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
|
|
2202 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
|
|
2203 }
|
|
2204
|
|
2205 set_child_ptrmaps_out:
|
|
2206 pPage->isInit = isInitOrig;
|
|
2207 return rc;
|
|
2208 }
|
|
2209
|
|
2210 /*
|
|
2211 ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
|
|
2212 ** page, is a pointer to page iFrom. Modify this pointer so that it points to
|
|
2213 ** iTo. Parameter eType describes the type of pointer to be modified, as
|
|
2214 ** follows:
|
|
2215 **
|
|
2216 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
|
|
2217 ** page of pPage.
|
|
2218 **
|
|
2219 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
|
|
2220 ** page pointed to by one of the cells on pPage.
|
|
2221 **
|
|
2222 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
|
|
2223 ** overflow page in the list.
|
|
2224 */
|
|
2225 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
|
|
2226 if( eType==PTRMAP_OVERFLOW2 ){
|
|
2227 /* The pointer is always the first 4 bytes of the page in this case. */
|
|
2228 if( get4byte(pPage->aData)!=iFrom ){
|
|
2229 return SQLITE_CORRUPT_BKPT;
|
|
2230 }
|
|
2231 put4byte(pPage->aData, iTo);
|
|
2232 }else{
|
|
2233 int isInitOrig = pPage->isInit;
|
|
2234 int i;
|
|
2235 int nCell;
|
|
2236
|
|
2237 initPage(pPage, 0);
|
|
2238 nCell = pPage->nCell;
|
|
2239
|
|
2240 for(i=0; i<nCell; i++){
|
|
2241 u8 *pCell = findCell(pPage, i);
|
|
2242 if( eType==PTRMAP_OVERFLOW1 ){
|
|
2243 CellInfo info;
|
|
2244 parseCellPtr(pPage, pCell, &info);
|
|
2245 if( info.iOverflow ){
|
|
2246 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
|
|
2247 put4byte(&pCell[info.iOverflow], iTo);
|
|
2248 break;
|
|
2249 }
|
|
2250 }
|
|
2251 }else{
|
|
2252 if( get4byte(pCell)==iFrom ){
|
|
2253 put4byte(pCell, iTo);
|
|
2254 break;
|
|
2255 }
|
|
2256 }
|
|
2257 }
|
|
2258
|
|
2259 if( i==nCell ){
|
|
2260 if( eType!=PTRMAP_BTREE ||
|
|
2261 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
|
|
2262 return SQLITE_CORRUPT_BKPT;
|
|
2263 }
|
|
2264 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
|
|
2265 }
|
|
2266
|
|
2267 pPage->isInit = isInitOrig;
|
|
2268 }
|
|
2269 return SQLITE_OK;
|
|
2270 }
|
|
2271
|
|
2272
|
|
2273 /*
|
|
2274 ** Move the open database page pDbPage to location iFreePage in the
|
|
2275 ** database. The pDbPage reference remains valid.
|
|
2276 */
|
|
2277 static int relocatePage(
|
|
2278 BtShared *pBt, /* Btree */
|
|
2279 MemPage *pDbPage, /* Open page to move */
|
|
2280 u8 eType, /* Pointer map 'type' entry for pDbPage */
|
|
2281 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
|
|
2282 Pgno iFreePage /* The location to move pDbPage to */
|
|
2283 ){
|
|
2284 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
|
|
2285 Pgno iDbPage = pDbPage->pgno;
|
|
2286 Pager *pPager = pBt->pPager;
|
|
2287 int rc;
|
|
2288
|
|
2289 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
|
|
2290 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
|
|
2291
|
|
2292 /* Move page iDbPage from it's current location to page number iFreePage */
|
|
2293 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
|
|
2294 iDbPage, iFreePage, iPtrPage, eType));
|
|
2295 rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage);
|
|
2296 if( rc!=SQLITE_OK ){
|
|
2297 return rc;
|
|
2298 }
|
|
2299 pDbPage->pgno = iFreePage;
|
|
2300
|
|
2301 /* If pDbPage was a btree-page, then it may have child pages and/or cells
|
|
2302 ** that point to overflow pages. The pointer map entries for all these
|
|
2303 ** pages need to be changed.
|
|
2304 **
|
|
2305 ** If pDbPage is an overflow page, then the first 4 bytes may store a
|
|
2306 ** pointer to a subsequent overflow page. If this is the case, then
|
|
2307 ** the pointer map needs to be updated for the subsequent overflow page.
|
|
2308 */
|
|
2309 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
|
|
2310 rc = setChildPtrmaps(pDbPage);
|
|
2311 if( rc!=SQLITE_OK ){
|
|
2312 return rc;
|
|
2313 }
|
|
2314 }else{
|
|
2315 Pgno nextOvfl = get4byte(pDbPage->aData);
|
|
2316 if( nextOvfl!=0 ){
|
|
2317 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
|
|
2318 if( rc!=SQLITE_OK ){
|
|
2319 return rc;
|
|
2320 }
|
|
2321 }
|
|
2322 }
|
|
2323
|
|
2324 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
|
|
2325 ** that it points at iFreePage. Also fix the pointer map entry for
|
|
2326 ** iPtrPage.
|
|
2327 */
|
|
2328 if( eType!=PTRMAP_ROOTPAGE ){
|
|
2329 rc = getPage(pBt, iPtrPage, &pPtrPage);
|
|
2330 if( rc!=SQLITE_OK ){
|
|
2331 return rc;
|
|
2332 }
|
|
2333 rc = sqlite3pager_write(pPtrPage->aData);
|
|
2334 if( rc!=SQLITE_OK ){
|
|
2335 releasePage(pPtrPage);
|
|
2336 return rc;
|
|
2337 }
|
|
2338 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
|
|
2339 releasePage(pPtrPage);
|
|
2340 if( rc==SQLITE_OK ){
|
|
2341 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
|
|
2342 }
|
|
2343 }
|
|
2344 return rc;
|
|
2345 }
|
|
2346
|
|
2347 /* Forward declaration required by autoVacuumCommit(). */
|
|
2348 static int allocatePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
|
|
2349
|
|
2350 /*
|
|
2351 ** This routine is called prior to sqlite3pager_commit when a transaction
|
|
2352 ** is commited for an auto-vacuum database.
|
|
2353 */
|
|
2354 static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){
|
|
2355 Pager *pPager = pBt->pPager;
|
|
2356 Pgno nFreeList; /* Number of pages remaining on the free-list. */
|
|
2357 int nPtrMap; /* Number of pointer-map pages deallocated */
|
|
2358 Pgno origSize; /* Pages in the database file */
|
|
2359 Pgno finSize; /* Pages in the database file after truncation */
|
|
2360 int rc; /* Return code */
|
|
2361 u8 eType;
|
|
2362 int pgsz = pBt->pageSize; /* Page size for this database */
|
|
2363 Pgno iDbPage; /* The database page to move */
|
|
2364 MemPage *pDbMemPage = 0; /* "" */
|
|
2365 Pgno iPtrPage; /* The page that contains a pointer to iDbPage */
|
|
2366 Pgno iFreePage; /* The free-list page to move iDbPage to */
|
|
2367 MemPage *pFreeMemPage = 0; /* "" */
|
|
2368
|
|
2369 #ifndef NDEBUG
|
|
2370 int nRef = *sqlite3pager_stats(pPager);
|
|
2371 #endif
|
|
2372
|
|
2373 assert( pBt->autoVacuum );
|
|
2374 if( PTRMAP_ISPAGE(pBt, sqlite3pager_pagecount(pPager)) ){
|
|
2375 return SQLITE_CORRUPT_BKPT;
|
|
2376 }
|
|
2377
|
|
2378 /* Figure out how many free-pages are in the database. If there are no
|
|
2379 ** free pages, then auto-vacuum is a no-op.
|
|
2380 */
|
|
2381 nFreeList = get4byte(&pBt->pPage1->aData[36]);
|
|
2382 if( nFreeList==0 ){
|
|
2383 *nTrunc = 0;
|
|
2384 return SQLITE_OK;
|
|
2385 }
|
|
2386
|
|
2387 /* This block figures out how many pages there are in the database
|
|
2388 ** now (variable origSize), and how many there will be after the
|
|
2389 ** truncation (variable finSize).
|
|
2390 **
|
|
2391 ** The final size is the original size, less the number of free pages
|
|
2392 ** in the database, less any pointer-map pages that will no longer
|
|
2393 ** be required, less 1 if the pending-byte page was part of the database
|
|
2394 ** but is not after the truncation.
|
|
2395 **/
|
|
2396 origSize = sqlite3pager_pagecount(pPager);
|
|
2397 if( origSize==PENDING_BYTE_PAGE(pBt) ){
|
|
2398 origSize--;
|
|
2399 }
|
|
2400 nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pBt, origSize)+pgsz/5)/(pgsz/5);
|
|
2401 finSize = origSize - nFreeList - nPtrMap;
|
|
2402 if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){
|
|
2403 finSize--;
|
|
2404 }
|
|
2405 while( PTRMAP_ISPAGE(pBt, finSize) || finSize==PENDING_BYTE_PAGE(pBt) ){
|
|
2406 finSize--;
|
|
2407 }
|
|
2408 TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize));
|
|
2409
|
|
2410 /* Variable 'finSize' will be the size of the file in pages after
|
|
2411 ** the auto-vacuum has completed (the current file size minus the number
|
|
2412 ** of pages on the free list). Loop through the pages that lie beyond
|
|
2413 ** this mark, and if they are not already on the free list, move them
|
|
2414 ** to a free page earlier in the file (somewhere before finSize).
|
|
2415 */
|
|
2416 for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){
|
|
2417 /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */
|
|
2418 if( PTRMAP_ISPAGE(pBt, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){
|
|
2419 continue;
|
|
2420 }
|
|
2421
|
|
2422 rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage);
|
|
2423 if( rc!=SQLITE_OK ) goto autovacuum_out;
|
|
2424 if( eType==PTRMAP_ROOTPAGE ){
|
|
2425 rc = SQLITE_CORRUPT_BKPT;
|
|
2426 goto autovacuum_out;
|
|
2427 }
|
|
2428
|
|
2429 /* If iDbPage is free, do not swap it. */
|
|
2430 if( eType==PTRMAP_FREEPAGE ){
|
|
2431 continue;
|
|
2432 }
|
|
2433 rc = getPage(pBt, iDbPage, &pDbMemPage);
|
|
2434 if( rc!=SQLITE_OK ) goto autovacuum_out;
|
|
2435
|
|
2436 /* Find the next page in the free-list that is not already at the end
|
|
2437 ** of the file. A page can be pulled off the free list using the
|
|
2438 ** allocatePage() routine.
|
|
2439 */
|
|
2440 do{
|
|
2441 if( pFreeMemPage ){
|
|
2442 releasePage(pFreeMemPage);
|
|
2443 pFreeMemPage = 0;
|
|
2444 }
|
|
2445 rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
|
|
2446 if( rc!=SQLITE_OK ){
|
|
2447 releasePage(pDbMemPage);
|
|
2448 goto autovacuum_out;
|
|
2449 }
|
|
2450 assert( iFreePage<=origSize );
|
|
2451 }while( iFreePage>finSize );
|
|
2452 releasePage(pFreeMemPage);
|
|
2453 pFreeMemPage = 0;
|
|
2454
|
|
2455 /* Relocate the page into the body of the file. Note that although the
|
|
2456 ** page has moved within the database file, the pDbMemPage pointer
|
|
2457 ** remains valid. This means that this function can run without
|
|
2458 ** invalidating cursors open on the btree. This is important in
|
|
2459 ** shared-cache mode.
|
|
2460 */
|
|
2461 rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage);
|
|
2462 releasePage(pDbMemPage);
|
|
2463 if( rc!=SQLITE_OK ) goto autovacuum_out;
|
|
2464 }
|
|
2465
|
|
2466 /* The entire free-list has been swapped to the end of the file. So
|
|
2467 ** truncate the database file to finSize pages and consider the
|
|
2468 ** free-list empty.
|
|
2469 */
|
|
2470 rc = sqlite3pager_write(pBt->pPage1->aData);
|
|
2471 if( rc!=SQLITE_OK ) goto autovacuum_out;
|
|
2472 put4byte(&pBt->pPage1->aData[32], 0);
|
|
2473 put4byte(&pBt->pPage1->aData[36], 0);
|
|
2474 *nTrunc = finSize;
|
|
2475 assert( finSize!=PENDING_BYTE_PAGE(pBt) );
|
|
2476
|
|
2477 autovacuum_out:
|
|
2478 assert( nRef==*sqlite3pager_stats(pPager) );
|
|
2479 if( rc!=SQLITE_OK ){
|
|
2480 sqlite3pager_rollback(pPager);
|
|
2481 }
|
|
2482 return rc;
|
|
2483 }
|
|
2484 #endif
|
|
2485
|
|
2486 /*
|
|
2487 ** Commit the transaction currently in progress.
|
|
2488 **
|
|
2489 ** This will release the write lock on the database file. If there
|
|
2490 ** are no active cursors, it also releases the read lock.
|
|
2491 */
|
|
2492 int sqlite3BtreeCommit(Btree *p){
|
|
2493 BtShared *pBt = p->pBt;
|
|
2494
|
|
2495 btreeIntegrity(p);
|
|
2496
|
|
2497 /* If the handle has a write-transaction open, commit the shared-btrees
|
|
2498 ** transaction and set the shared state to TRANS_READ.
|
|
2499 */
|
|
2500 if( p->inTrans==TRANS_WRITE ){
|
|
2501 int rc;
|
|
2502 assert( pBt->inTransaction==TRANS_WRITE );
|
|
2503 assert( pBt->nTransaction>0 );
|
|
2504 rc = sqlite3pager_commit(pBt->pPager);
|
|
2505 if( rc!=SQLITE_OK ){
|
|
2506 return rc;
|
|
2507 }
|
|
2508 pBt->inTransaction = TRANS_READ;
|
|
2509 pBt->inStmt = 0;
|
|
2510 }
|
|
2511 unlockAllTables(p);
|
|
2512
|
|
2513 /* If the handle has any kind of transaction open, decrement the transaction
|
|
2514 ** count of the shared btree. If the transaction count reaches 0, set
|
|
2515 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
|
|
2516 ** will unlock the pager.
|
|
2517 */
|
|
2518 if( p->inTrans!=TRANS_NONE ){
|
|
2519 pBt->nTransaction--;
|
|
2520 if( 0==pBt->nTransaction ){
|
|
2521 pBt->inTransaction = TRANS_NONE;
|
|
2522 }
|
|
2523 }
|
|
2524
|
|
2525 /* Set the handles current transaction state to TRANS_NONE and unlock
|
|
2526 ** the pager if this call closed the only read or write transaction.
|
|
2527 */
|
|
2528 p->inTrans = TRANS_NONE;
|
|
2529 unlockBtreeIfUnused(pBt);
|
|
2530
|
|
2531 btreeIntegrity(p);
|
|
2532 return SQLITE_OK;
|
|
2533 }
|
|
2534
|
|
2535 #ifndef NDEBUG
|
|
2536 /*
|
|
2537 ** Return the number of write-cursors open on this handle. This is for use
|
|
2538 ** in assert() expressions, so it is only compiled if NDEBUG is not
|
|
2539 ** defined.
|
|
2540 */
|
|
2541 static int countWriteCursors(BtShared *pBt){
|
|
2542 BtCursor *pCur;
|
|
2543 int r = 0;
|
|
2544 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
|
|
2545 if( pCur->wrFlag ) r++;
|
|
2546 }
|
|
2547 return r;
|
|
2548 }
|
|
2549 #endif
|
|
2550
|
|
2551 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
|
|
2552 /*
|
|
2553 ** Print debugging information about all cursors to standard output.
|
|
2554 */
|
|
2555 void sqlite3BtreeCursorList(Btree *p){
|
|
2556 BtCursor *pCur;
|
|
2557 BtShared *pBt = p->pBt;
|
|
2558 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
|
|
2559 MemPage *pPage = pCur->pPage;
|
|
2560 char *zMode = pCur->wrFlag ? "rw" : "ro";
|
|
2561 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
|
|
2562 pCur, pCur->pgnoRoot, zMode,
|
|
2563 pPage ? pPage->pgno : 0, pCur->idx,
|
|
2564 (pCur->eState==CURSOR_VALID) ? "" : " eof"
|
|
2565 );
|
|
2566 }
|
|
2567 }
|
|
2568 #endif
|
|
2569
|
|
2570 /*
|
|
2571 ** Rollback the transaction in progress. All cursors will be
|
|
2572 ** invalided by this operation. Any attempt to use a cursor
|
|
2573 ** that was open at the beginning of this operation will result
|
|
2574 ** in an error.
|
|
2575 **
|
|
2576 ** This will release the write lock on the database file. If there
|
|
2577 ** are no active cursors, it also releases the read lock.
|
|
2578 */
|
|
2579 int sqlite3BtreeRollback(Btree *p){
|
|
2580 int rc;
|
|
2581 BtShared *pBt = p->pBt;
|
|
2582 MemPage *pPage1;
|
|
2583
|
|
2584 rc = saveAllCursors(pBt, 0, 0);
|
|
2585 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
2586 if( rc!=SQLITE_OK ){
|
|
2587 /* This is a horrible situation. An IO or malloc() error occured whilst
|
|
2588 ** trying to save cursor positions. If this is an automatic rollback (as
|
|
2589 ** the result of a constraint, malloc() failure or IO error) then
|
|
2590 ** the cache may be internally inconsistent (not contain valid trees) so
|
|
2591 ** we cannot simply return the error to the caller. Instead, abort
|
|
2592 ** all queries that may be using any of the cursors that failed to save.
|
|
2593 */
|
|
2594 while( pBt->pCursor ){
|
|
2595 sqlite3 *db = pBt->pCursor->pBtree->pSqlite;
|
|
2596 if( db ){
|
|
2597 sqlite3AbortOtherActiveVdbes(db, 0);
|
|
2598 }
|
|
2599 }
|
|
2600 }
|
|
2601 #endif
|
|
2602 btreeIntegrity(p);
|
|
2603 unlockAllTables(p);
|
|
2604
|
|
2605 if( p->inTrans==TRANS_WRITE ){
|
|
2606 int rc2;
|
|
2607
|
|
2608 assert( TRANS_WRITE==pBt->inTransaction );
|
|
2609 rc2 = sqlite3pager_rollback(pBt->pPager);
|
|
2610 if( rc2!=SQLITE_OK ){
|
|
2611 rc = rc2;
|
|
2612 }
|
|
2613
|
|
2614 /* The rollback may have destroyed the pPage1->aData value. So
|
|
2615 ** call getPage() on page 1 again to make sure pPage1->aData is
|
|
2616 ** set correctly. */
|
|
2617 if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){
|
|
2618 releasePage(pPage1);
|
|
2619 }
|
|
2620 assert( countWriteCursors(pBt)==0 );
|
|
2621 pBt->inTransaction = TRANS_READ;
|
|
2622 }
|
|
2623
|
|
2624 if( p->inTrans!=TRANS_NONE ){
|
|
2625 assert( pBt->nTransaction>0 );
|
|
2626 pBt->nTransaction--;
|
|
2627 if( 0==pBt->nTransaction ){
|
|
2628 pBt->inTransaction = TRANS_NONE;
|
|
2629 }
|
|
2630 }
|
|
2631
|
|
2632 p->inTrans = TRANS_NONE;
|
|
2633 pBt->inStmt = 0;
|
|
2634 unlockBtreeIfUnused(pBt);
|
|
2635
|
|
2636 btreeIntegrity(p);
|
|
2637 return rc;
|
|
2638 }
|
|
2639
|
|
2640 /*
|
|
2641 ** Start a statement subtransaction. The subtransaction can
|
|
2642 ** can be rolled back independently of the main transaction.
|
|
2643 ** You must start a transaction before starting a subtransaction.
|
|
2644 ** The subtransaction is ended automatically if the main transaction
|
|
2645 ** commits or rolls back.
|
|
2646 **
|
|
2647 ** Only one subtransaction may be active at a time. It is an error to try
|
|
2648 ** to start a new subtransaction if another subtransaction is already active.
|
|
2649 **
|
|
2650 ** Statement subtransactions are used around individual SQL statements
|
|
2651 ** that are contained within a BEGIN...COMMIT block. If a constraint
|
|
2652 ** error occurs within the statement, the effect of that one statement
|
|
2653 ** can be rolled back without having to rollback the entire transaction.
|
|
2654 */
|
|
2655 int sqlite3BtreeBeginStmt(Btree *p){
|
|
2656 int rc;
|
|
2657 BtShared *pBt = p->pBt;
|
|
2658 if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
|
|
2659 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
2660 }
|
|
2661 assert( pBt->inTransaction==TRANS_WRITE );
|
|
2662 rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager);
|
|
2663 pBt->inStmt = 1;
|
|
2664 return rc;
|
|
2665 }
|
|
2666
|
|
2667
|
|
2668 /*
|
|
2669 ** Commit the statment subtransaction currently in progress. If no
|
|
2670 ** subtransaction is active, this is a no-op.
|
|
2671 */
|
|
2672 int sqlite3BtreeCommitStmt(Btree *p){
|
|
2673 int rc;
|
|
2674 BtShared *pBt = p->pBt;
|
|
2675 if( pBt->inStmt && !pBt->readOnly ){
|
|
2676 rc = sqlite3pager_stmt_commit(pBt->pPager);
|
|
2677 }else{
|
|
2678 rc = SQLITE_OK;
|
|
2679 }
|
|
2680 pBt->inStmt = 0;
|
|
2681 return rc;
|
|
2682 }
|
|
2683
|
|
2684 /*
|
|
2685 ** Rollback the active statement subtransaction. If no subtransaction
|
|
2686 ** is active this routine is a no-op.
|
|
2687 **
|
|
2688 ** All cursors will be invalidated by this operation. Any attempt
|
|
2689 ** to use a cursor that was open at the beginning of this operation
|
|
2690 ** will result in an error.
|
|
2691 */
|
|
2692 int sqlite3BtreeRollbackStmt(Btree *p){
|
|
2693 int rc = SQLITE_OK;
|
|
2694 BtShared *pBt = p->pBt;
|
|
2695 sqlite3MallocDisallow();
|
|
2696 if( pBt->inStmt && !pBt->readOnly ){
|
|
2697 rc = sqlite3pager_stmt_rollback(pBt->pPager);
|
|
2698 assert( countWriteCursors(pBt)==0 );
|
|
2699 pBt->inStmt = 0;
|
|
2700 }
|
|
2701 sqlite3MallocAllow();
|
|
2702 return rc;
|
|
2703 }
|
|
2704
|
|
2705 /*
|
|
2706 ** Default key comparison function to be used if no comparison function
|
|
2707 ** is specified on the sqlite3BtreeCursor() call.
|
|
2708 */
|
|
2709 static int dfltCompare(
|
|
2710 void *NotUsed, /* User data is not used */
|
|
2711 int n1, const void *p1, /* First key to compare */
|
|
2712 int n2, const void *p2 /* Second key to compare */
|
|
2713 ){
|
|
2714 int c;
|
|
2715 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
|
|
2716 if( c==0 ){
|
|
2717 c = n1 - n2;
|
|
2718 }
|
|
2719 return c;
|
|
2720 }
|
|
2721
|
|
2722 /*
|
|
2723 ** Create a new cursor for the BTree whose root is on the page
|
|
2724 ** iTable. The act of acquiring a cursor gets a read lock on
|
|
2725 ** the database file.
|
|
2726 **
|
|
2727 ** If wrFlag==0, then the cursor can only be used for reading.
|
|
2728 ** If wrFlag==1, then the cursor can be used for reading or for
|
|
2729 ** writing if other conditions for writing are also met. These
|
|
2730 ** are the conditions that must be met in order for writing to
|
|
2731 ** be allowed:
|
|
2732 **
|
|
2733 ** 1: The cursor must have been opened with wrFlag==1
|
|
2734 **
|
|
2735 ** 2: No other cursors may be open with wrFlag==0 on the same table
|
|
2736 **
|
|
2737 ** 3: The database must be writable (not on read-only media)
|
|
2738 **
|
|
2739 ** 4: There must be an active transaction.
|
|
2740 **
|
|
2741 ** Condition 2 warrants further discussion. If any cursor is opened
|
|
2742 ** on a table with wrFlag==0, that prevents all other cursors from
|
|
2743 ** writing to that table. This is a kind of "read-lock". When a cursor
|
|
2744 ** is opened with wrFlag==0 it is guaranteed that the table will not
|
|
2745 ** change as long as the cursor is open. This allows the cursor to
|
|
2746 ** do a sequential scan of the table without having to worry about
|
|
2747 ** entries being inserted or deleted during the scan. Cursors should
|
|
2748 ** be opened with wrFlag==0 only if this read-lock property is needed.
|
|
2749 ** That is to say, cursors should be opened with wrFlag==0 only if they
|
|
2750 ** intend to use the sqlite3BtreeNext() system call. All other cursors
|
|
2751 ** should be opened with wrFlag==1 even if they never really intend
|
|
2752 ** to write.
|
|
2753 **
|
|
2754 ** No checking is done to make sure that page iTable really is the
|
|
2755 ** root page of a b-tree. If it is not, then the cursor acquired
|
|
2756 ** will not work correctly.
|
|
2757 **
|
|
2758 ** The comparison function must be logically the same for every cursor
|
|
2759 ** on a particular table. Changing the comparison function will result
|
|
2760 ** in incorrect operations. If the comparison function is NULL, a
|
|
2761 ** default comparison function is used. The comparison function is
|
|
2762 ** always ignored for INTKEY tables.
|
|
2763 */
|
|
2764 int sqlite3BtreeCursor(
|
|
2765 Btree *p, /* The btree */
|
|
2766 int iTable, /* Root page of table to open */
|
|
2767 int wrFlag, /* 1 to write. 0 read-only */
|
|
2768 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
|
|
2769 void *pArg, /* First arg to xCompare() */
|
|
2770 BtCursor **ppCur /* Write new cursor here */
|
|
2771 ){
|
|
2772 int rc;
|
|
2773 BtCursor *pCur;
|
|
2774 BtShared *pBt = p->pBt;
|
|
2775
|
|
2776 *ppCur = 0;
|
|
2777 if( wrFlag ){
|
|
2778 if( pBt->readOnly ){
|
|
2779 return SQLITE_READONLY;
|
|
2780 }
|
|
2781 if( checkReadLocks(pBt, iTable, 0) ){
|
|
2782 return SQLITE_LOCKED;
|
|
2783 }
|
|
2784 }
|
|
2785
|
|
2786 if( pBt->pPage1==0 ){
|
|
2787 rc = lockBtreeWithRetry(p);
|
|
2788 if( rc!=SQLITE_OK ){
|
|
2789 return rc;
|
|
2790 }
|
|
2791 }
|
|
2792 pCur = sqliteMalloc( sizeof(*pCur) );
|
|
2793 if( pCur==0 ){
|
|
2794 rc = SQLITE_NOMEM;
|
|
2795 goto create_cursor_exception;
|
|
2796 }
|
|
2797 pCur->pgnoRoot = (Pgno)iTable;
|
|
2798 if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){
|
|
2799 rc = SQLITE_EMPTY;
|
|
2800 goto create_cursor_exception;
|
|
2801 }
|
|
2802 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
|
|
2803 if( rc!=SQLITE_OK ){
|
|
2804 goto create_cursor_exception;
|
|
2805 }
|
|
2806
|
|
2807 /* Now that no other errors can occur, finish filling in the BtCursor
|
|
2808 ** variables, link the cursor into the BtShared list and set *ppCur (the
|
|
2809 ** output argument to this function).
|
|
2810 */
|
|
2811 pCur->xCompare = xCmp ? xCmp : dfltCompare;
|
|
2812 pCur->pArg = pArg;
|
|
2813 pCur->pBtree = p;
|
|
2814 pCur->wrFlag = wrFlag;
|
|
2815 pCur->pNext = pBt->pCursor;
|
|
2816 if( pCur->pNext ){
|
|
2817 pCur->pNext->pPrev = pCur;
|
|
2818 }
|
|
2819 pBt->pCursor = pCur;
|
|
2820 pCur->eState = CURSOR_INVALID;
|
|
2821 *ppCur = pCur;
|
|
2822
|
|
2823 return SQLITE_OK;
|
|
2824 create_cursor_exception:
|
|
2825 if( pCur ){
|
|
2826 releasePage(pCur->pPage);
|
|
2827 sqliteFree(pCur);
|
|
2828 }
|
|
2829 unlockBtreeIfUnused(pBt);
|
|
2830 return rc;
|
|
2831 }
|
|
2832
|
|
2833 #if 0 /* Not Used */
|
|
2834 /*
|
|
2835 ** Change the value of the comparison function used by a cursor.
|
|
2836 */
|
|
2837 void sqlite3BtreeSetCompare(
|
|
2838 BtCursor *pCur, /* The cursor to whose comparison function is changed */
|
|
2839 int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */
|
|
2840 void *pArg /* First argument to xCmp() */
|
|
2841 ){
|
|
2842 pCur->xCompare = xCmp ? xCmp : dfltCompare;
|
|
2843 pCur->pArg = pArg;
|
|
2844 }
|
|
2845 #endif
|
|
2846
|
|
2847 /*
|
|
2848 ** Close a cursor. The read lock on the database file is released
|
|
2849 ** when the last cursor is closed.
|
|
2850 */
|
|
2851 int sqlite3BtreeCloseCursor(BtCursor *pCur){
|
|
2852 BtShared *pBt = pCur->pBtree->pBt;
|
|
2853 restoreOrClearCursorPosition(pCur, 0);
|
|
2854 if( pCur->pPrev ){
|
|
2855 pCur->pPrev->pNext = pCur->pNext;
|
|
2856 }else{
|
|
2857 pBt->pCursor = pCur->pNext;
|
|
2858 }
|
|
2859 if( pCur->pNext ){
|
|
2860 pCur->pNext->pPrev = pCur->pPrev;
|
|
2861 }
|
|
2862 releasePage(pCur->pPage);
|
|
2863 unlockBtreeIfUnused(pBt);
|
|
2864 sqliteFree(pCur);
|
|
2865 return SQLITE_OK;
|
|
2866 }
|
|
2867
|
|
2868 /*
|
|
2869 ** Make a temporary cursor by filling in the fields of pTempCur.
|
|
2870 ** The temporary cursor is not on the cursor list for the Btree.
|
|
2871 */
|
|
2872 static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
|
|
2873 memcpy(pTempCur, pCur, sizeof(*pCur));
|
|
2874 pTempCur->pNext = 0;
|
|
2875 pTempCur->pPrev = 0;
|
|
2876 if( pTempCur->pPage ){
|
|
2877 sqlite3pager_ref(pTempCur->pPage->aData);
|
|
2878 }
|
|
2879 }
|
|
2880
|
|
2881 /*
|
|
2882 ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
|
|
2883 ** function above.
|
|
2884 */
|
|
2885 static void releaseTempCursor(BtCursor *pCur){
|
|
2886 if( pCur->pPage ){
|
|
2887 sqlite3pager_unref(pCur->pPage->aData);
|
|
2888 }
|
|
2889 }
|
|
2890
|
|
2891 /*
|
|
2892 ** Make sure the BtCursor.info field of the given cursor is valid.
|
|
2893 ** If it is not already valid, call parseCell() to fill it in.
|
|
2894 **
|
|
2895 ** BtCursor.info is a cache of the information in the current cell.
|
|
2896 ** Using this cache reduces the number of calls to parseCell().
|
|
2897 */
|
|
2898 static void getCellInfo(BtCursor *pCur){
|
|
2899 if( pCur->info.nSize==0 ){
|
|
2900 parseCell(pCur->pPage, pCur->idx, &pCur->info);
|
|
2901 }else{
|
|
2902 #ifndef NDEBUG
|
|
2903 CellInfo info;
|
|
2904 memset(&info, 0, sizeof(info));
|
|
2905 parseCell(pCur->pPage, pCur->idx, &info);
|
|
2906 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
|
|
2907 #endif
|
|
2908 }
|
|
2909 }
|
|
2910
|
|
2911 /*
|
|
2912 ** Set *pSize to the size of the buffer needed to hold the value of
|
|
2913 ** the key for the current entry. If the cursor is not pointing
|
|
2914 ** to a valid entry, *pSize is set to 0.
|
|
2915 **
|
|
2916 ** For a table with the INTKEY flag set, this routine returns the key
|
|
2917 ** itself, not the number of bytes in the key.
|
|
2918 */
|
|
2919 int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
|
|
2920 int rc = restoreOrClearCursorPosition(pCur, 1);
|
|
2921 if( rc==SQLITE_OK ){
|
|
2922 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
|
|
2923 if( pCur->eState==CURSOR_INVALID ){
|
|
2924 *pSize = 0;
|
|
2925 }else{
|
|
2926 getCellInfo(pCur);
|
|
2927 *pSize = pCur->info.nKey;
|
|
2928 }
|
|
2929 }
|
|
2930 return rc;
|
|
2931 }
|
|
2932
|
|
2933 /*
|
|
2934 ** Set *pSize to the number of bytes of data in the entry the
|
|
2935 ** cursor currently points to. Always return SQLITE_OK.
|
|
2936 ** Failure is not possible. If the cursor is not currently
|
|
2937 ** pointing to an entry (which can happen, for example, if
|
|
2938 ** the database is empty) then *pSize is set to 0.
|
|
2939 */
|
|
2940 int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
|
|
2941 int rc = restoreOrClearCursorPosition(pCur, 1);
|
|
2942 if( rc==SQLITE_OK ){
|
|
2943 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
|
|
2944 if( pCur->eState==CURSOR_INVALID ){
|
|
2945 /* Not pointing at a valid entry - set *pSize to 0. */
|
|
2946 *pSize = 0;
|
|
2947 }else{
|
|
2948 getCellInfo(pCur);
|
|
2949 *pSize = pCur->info.nData;
|
|
2950 }
|
|
2951 }
|
|
2952 return rc;
|
|
2953 }
|
|
2954
|
|
2955 /*
|
|
2956 ** Read payload information from the entry that the pCur cursor is
|
|
2957 ** pointing to. Begin reading the payload at "offset" and read
|
|
2958 ** a total of "amt" bytes. Put the result in zBuf.
|
|
2959 **
|
|
2960 ** This routine does not make a distinction between key and data.
|
|
2961 ** It just reads bytes from the payload area. Data might appear
|
|
2962 ** on the main page or be scattered out on multiple overflow pages.
|
|
2963 */
|
|
2964 static int getPayload(
|
|
2965 BtCursor *pCur, /* Cursor pointing to entry to read from */
|
|
2966 int offset, /* Begin reading this far into payload */
|
|
2967 int amt, /* Read this many bytes */
|
|
2968 unsigned char *pBuf, /* Write the bytes into this buffer */
|
|
2969 int skipKey /* offset begins at data if this is true */
|
|
2970 ){
|
|
2971 unsigned char *aPayload;
|
|
2972 Pgno nextPage;
|
|
2973 int rc;
|
|
2974 MemPage *pPage;
|
|
2975 BtShared *pBt;
|
|
2976 int ovflSize;
|
|
2977 u32 nKey;
|
|
2978
|
|
2979 assert( pCur!=0 && pCur->pPage!=0 );
|
|
2980 assert( pCur->eState==CURSOR_VALID );
|
|
2981 pBt = pCur->pBtree->pBt;
|
|
2982 pPage = pCur->pPage;
|
|
2983 pageIntegrity(pPage);
|
|
2984 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
|
|
2985 getCellInfo(pCur);
|
|
2986 aPayload = pCur->info.pCell + pCur->info.nHeader;
|
|
2987 if( pPage->intKey ){
|
|
2988 nKey = 0;
|
|
2989 }else{
|
|
2990 nKey = pCur->info.nKey;
|
|
2991 }
|
|
2992 assert( offset>=0 );
|
|
2993 if( skipKey ){
|
|
2994 offset += nKey;
|
|
2995 }
|
|
2996 if( offset+amt > nKey+pCur->info.nData ){
|
|
2997 return SQLITE_ERROR;
|
|
2998 }
|
|
2999 if( offset<pCur->info.nLocal ){
|
|
3000 int a = amt;
|
|
3001 if( a+offset>pCur->info.nLocal ){
|
|
3002 a = pCur->info.nLocal - offset;
|
|
3003 }
|
|
3004 memcpy(pBuf, &aPayload[offset], a);
|
|
3005 if( a==amt ){
|
|
3006 return SQLITE_OK;
|
|
3007 }
|
|
3008 offset = 0;
|
|
3009 pBuf += a;
|
|
3010 amt -= a;
|
|
3011 }else{
|
|
3012 offset -= pCur->info.nLocal;
|
|
3013 }
|
|
3014 ovflSize = pBt->usableSize - 4;
|
|
3015 if( amt>0 ){
|
|
3016 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
|
|
3017 while( amt>0 && nextPage ){
|
|
3018 rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload);
|
|
3019 if( rc!=0 ){
|
|
3020 return rc;
|
|
3021 }
|
|
3022 nextPage = get4byte(aPayload);
|
|
3023 if( offset<ovflSize ){
|
|
3024 int a = amt;
|
|
3025 if( a + offset > ovflSize ){
|
|
3026 a = ovflSize - offset;
|
|
3027 }
|
|
3028 memcpy(pBuf, &aPayload[offset+4], a);
|
|
3029 offset = 0;
|
|
3030 amt -= a;
|
|
3031 pBuf += a;
|
|
3032 }else{
|
|
3033 offset -= ovflSize;
|
|
3034 }
|
|
3035 sqlite3pager_unref(aPayload);
|
|
3036 }
|
|
3037 }
|
|
3038
|
|
3039 if( amt>0 ){
|
|
3040 return SQLITE_CORRUPT_BKPT;
|
|
3041 }
|
|
3042 return SQLITE_OK;
|
|
3043 }
|
|
3044
|
|
3045 /*
|
|
3046 ** Read part of the key associated with cursor pCur. Exactly
|
|
3047 ** "amt" bytes will be transfered into pBuf[]. The transfer
|
|
3048 ** begins at "offset".
|
|
3049 **
|
|
3050 ** Return SQLITE_OK on success or an error code if anything goes
|
|
3051 ** wrong. An error is returned if "offset+amt" is larger than
|
|
3052 ** the available payload.
|
|
3053 */
|
|
3054 int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
|
|
3055 int rc = restoreOrClearCursorPosition(pCur, 1);
|
|
3056 if( rc==SQLITE_OK ){
|
|
3057 assert( pCur->eState==CURSOR_VALID );
|
|
3058 assert( pCur->pPage!=0 );
|
|
3059 if( pCur->pPage->intKey ){
|
|
3060 return SQLITE_CORRUPT_BKPT;
|
|
3061 }
|
|
3062 assert( pCur->pPage->intKey==0 );
|
|
3063 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
|
|
3064 rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
|
|
3065 }
|
|
3066 return rc;
|
|
3067 }
|
|
3068
|
|
3069 /*
|
|
3070 ** Read part of the data associated with cursor pCur. Exactly
|
|
3071 ** "amt" bytes will be transfered into pBuf[]. The transfer
|
|
3072 ** begins at "offset".
|
|
3073 **
|
|
3074 ** Return SQLITE_OK on success or an error code if anything goes
|
|
3075 ** wrong. An error is returned if "offset+amt" is larger than
|
|
3076 ** the available payload.
|
|
3077 */
|
|
3078 int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
|
|
3079 int rc = restoreOrClearCursorPosition(pCur, 1);
|
|
3080 if( rc==SQLITE_OK ){
|
|
3081 assert( pCur->eState==CURSOR_VALID );
|
|
3082 assert( pCur->pPage!=0 );
|
|
3083 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
|
|
3084 rc = getPayload(pCur, offset, amt, pBuf, 1);
|
|
3085 }
|
|
3086 return rc;
|
|
3087 }
|
|
3088
|
|
3089 /*
|
|
3090 ** Return a pointer to payload information from the entry that the
|
|
3091 ** pCur cursor is pointing to. The pointer is to the beginning of
|
|
3092 ** the key if skipKey==0 and it points to the beginning of data if
|
|
3093 ** skipKey==1. The number of bytes of available key/data is written
|
|
3094 ** into *pAmt. If *pAmt==0, then the value returned will not be
|
|
3095 ** a valid pointer.
|
|
3096 **
|
|
3097 ** This routine is an optimization. It is common for the entire key
|
|
3098 ** and data to fit on the local page and for there to be no overflow
|
|
3099 ** pages. When that is so, this routine can be used to access the
|
|
3100 ** key and data without making a copy. If the key and/or data spills
|
|
3101 ** onto overflow pages, then getPayload() must be used to reassembly
|
|
3102 ** the key/data and copy it into a preallocated buffer.
|
|
3103 **
|
|
3104 ** The pointer returned by this routine looks directly into the cached
|
|
3105 ** page of the database. The data might change or move the next time
|
|
3106 ** any btree routine is called.
|
|
3107 */
|
|
3108 static const unsigned char *fetchPayload(
|
|
3109 BtCursor *pCur, /* Cursor pointing to entry to read from */
|
|
3110 int *pAmt, /* Write the number of available bytes here */
|
|
3111 int skipKey /* read beginning at data if this is true */
|
|
3112 ){
|
|
3113 unsigned char *aPayload;
|
|
3114 MemPage *pPage;
|
|
3115 u32 nKey;
|
|
3116 int nLocal;
|
|
3117
|
|
3118 assert( pCur!=0 && pCur->pPage!=0 );
|
|
3119 assert( pCur->eState==CURSOR_VALID );
|
|
3120 pPage = pCur->pPage;
|
|
3121 pageIntegrity(pPage);
|
|
3122 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
|
|
3123 getCellInfo(pCur);
|
|
3124 aPayload = pCur->info.pCell;
|
|
3125 aPayload += pCur->info.nHeader;
|
|
3126 if( pPage->intKey ){
|
|
3127 nKey = 0;
|
|
3128 }else{
|
|
3129 nKey = pCur->info.nKey;
|
|
3130 }
|
|
3131 if( skipKey ){
|
|
3132 aPayload += nKey;
|
|
3133 nLocal = pCur->info.nLocal - nKey;
|
|
3134 }else{
|
|
3135 nLocal = pCur->info.nLocal;
|
|
3136 if( nLocal>nKey ){
|
|
3137 nLocal = nKey;
|
|
3138 }
|
|
3139 }
|
|
3140 *pAmt = nLocal;
|
|
3141 return aPayload;
|
|
3142 }
|
|
3143
|
|
3144
|
|
3145 /*
|
|
3146 ** For the entry that cursor pCur is point to, return as
|
|
3147 ** many bytes of the key or data as are available on the local
|
|
3148 ** b-tree page. Write the number of available bytes into *pAmt.
|
|
3149 **
|
|
3150 ** The pointer returned is ephemeral. The key/data may move
|
|
3151 ** or be destroyed on the next call to any Btree routine.
|
|
3152 **
|
|
3153 ** These routines is used to get quick access to key and data
|
|
3154 ** in the common case where no overflow pages are used.
|
|
3155 */
|
|
3156 const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
|
|
3157 if( pCur->eState==CURSOR_VALID ){
|
|
3158 return (const void*)fetchPayload(pCur, pAmt, 0);
|
|
3159 }
|
|
3160 return 0;
|
|
3161 }
|
|
3162 const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
|
|
3163 if( pCur->eState==CURSOR_VALID ){
|
|
3164 return (const void*)fetchPayload(pCur, pAmt, 1);
|
|
3165 }
|
|
3166 return 0;
|
|
3167 }
|
|
3168
|
|
3169
|
|
3170 /*
|
|
3171 ** Move the cursor down to a new child page. The newPgno argument is the
|
|
3172 ** page number of the child page to move to.
|
|
3173 */
|
|
3174 static int moveToChild(BtCursor *pCur, u32 newPgno){
|
|
3175 int rc;
|
|
3176 MemPage *pNewPage;
|
|
3177 MemPage *pOldPage;
|
|
3178 BtShared *pBt = pCur->pBtree->pBt;
|
|
3179
|
|
3180 assert( pCur->eState==CURSOR_VALID );
|
|
3181 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
|
|
3182 if( rc ) return rc;
|
|
3183 pageIntegrity(pNewPage);
|
|
3184 pNewPage->idxParent = pCur->idx;
|
|
3185 pOldPage = pCur->pPage;
|
|
3186 pOldPage->idxShift = 0;
|
|
3187 releasePage(pOldPage);
|
|
3188 pCur->pPage = pNewPage;
|
|
3189 pCur->idx = 0;
|
|
3190 pCur->info.nSize = 0;
|
|
3191 if( pNewPage->nCell<1 ){
|
|
3192 return SQLITE_CORRUPT_BKPT;
|
|
3193 }
|
|
3194 return SQLITE_OK;
|
|
3195 }
|
|
3196
|
|
3197 /*
|
|
3198 ** Return true if the page is the virtual root of its table.
|
|
3199 **
|
|
3200 ** The virtual root page is the root page for most tables. But
|
|
3201 ** for the table rooted on page 1, sometime the real root page
|
|
3202 ** is empty except for the right-pointer. In such cases the
|
|
3203 ** virtual root page is the page that the right-pointer of page
|
|
3204 ** 1 is pointing to.
|
|
3205 */
|
|
3206 static int isRootPage(MemPage *pPage){
|
|
3207 MemPage *pParent = pPage->pParent;
|
|
3208 if( pParent==0 ) return 1;
|
|
3209 if( pParent->pgno>1 ) return 0;
|
|
3210 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
|
|
3211 return 0;
|
|
3212 }
|
|
3213
|
|
3214 /*
|
|
3215 ** Move the cursor up to the parent page.
|
|
3216 **
|
|
3217 ** pCur->idx is set to the cell index that contains the pointer
|
|
3218 ** to the page we are coming from. If we are coming from the
|
|
3219 ** right-most child page then pCur->idx is set to one more than
|
|
3220 ** the largest cell index.
|
|
3221 */
|
|
3222 static void moveToParent(BtCursor *pCur){
|
|
3223 MemPage *pParent;
|
|
3224 MemPage *pPage;
|
|
3225 int idxParent;
|
|
3226
|
|
3227 assert( pCur->eState==CURSOR_VALID );
|
|
3228 pPage = pCur->pPage;
|
|
3229 assert( pPage!=0 );
|
|
3230 assert( !isRootPage(pPage) );
|
|
3231 pageIntegrity(pPage);
|
|
3232 pParent = pPage->pParent;
|
|
3233 assert( pParent!=0 );
|
|
3234 pageIntegrity(pParent);
|
|
3235 idxParent = pPage->idxParent;
|
|
3236 sqlite3pager_ref(pParent->aData);
|
|
3237 releasePage(pPage);
|
|
3238 pCur->pPage = pParent;
|
|
3239 pCur->info.nSize = 0;
|
|
3240 assert( pParent->idxShift==0 );
|
|
3241 pCur->idx = idxParent;
|
|
3242 }
|
|
3243
|
|
3244 /*
|
|
3245 ** Move the cursor to the root page
|
|
3246 */
|
|
3247 static int moveToRoot(BtCursor *pCur){
|
|
3248 MemPage *pRoot;
|
|
3249 int rc = SQLITE_OK;
|
|
3250 BtShared *pBt = pCur->pBtree->pBt;
|
|
3251
|
|
3252 restoreOrClearCursorPosition(pCur, 0);
|
|
3253 pRoot = pCur->pPage;
|
|
3254 if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
|
|
3255 assert( pRoot->isInit );
|
|
3256 }else{
|
|
3257 if(
|
|
3258 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
|
|
3259 ){
|
|
3260 pCur->eState = CURSOR_INVALID;
|
|
3261 return rc;
|
|
3262 }
|
|
3263 releasePage(pCur->pPage);
|
|
3264 pageIntegrity(pRoot);
|
|
3265 pCur->pPage = pRoot;
|
|
3266 }
|
|
3267 pCur->idx = 0;
|
|
3268 pCur->info.nSize = 0;
|
|
3269 if( pRoot->nCell==0 && !pRoot->leaf ){
|
|
3270 Pgno subpage;
|
|
3271 assert( pRoot->pgno==1 );
|
|
3272 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
|
|
3273 assert( subpage>0 );
|
|
3274 pCur->eState = CURSOR_VALID;
|
|
3275 rc = moveToChild(pCur, subpage);
|
|
3276 }
|
|
3277 pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
|
|
3278 return rc;
|
|
3279 }
|
|
3280
|
|
3281 /*
|
|
3282 ** Move the cursor down to the left-most leaf entry beneath the
|
|
3283 ** entry to which it is currently pointing.
|
|
3284 **
|
|
3285 ** The left-most leaf is the one with the smallest key - the first
|
|
3286 ** in ascending order.
|
|
3287 */
|
|
3288 static int moveToLeftmost(BtCursor *pCur){
|
|
3289 Pgno pgno;
|
|
3290 int rc;
|
|
3291 MemPage *pPage;
|
|
3292
|
|
3293 assert( pCur->eState==CURSOR_VALID );
|
|
3294 while( !(pPage = pCur->pPage)->leaf ){
|
|
3295 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
|
|
3296 pgno = get4byte(findCell(pPage, pCur->idx));
|
|
3297 rc = moveToChild(pCur, pgno);
|
|
3298 if( rc ) return rc;
|
|
3299 }
|
|
3300 return SQLITE_OK;
|
|
3301 }
|
|
3302
|
|
3303 /*
|
|
3304 ** Move the cursor down to the right-most leaf entry beneath the
|
|
3305 ** page to which it is currently pointing. Notice the difference
|
|
3306 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
|
|
3307 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
|
|
3308 ** finds the right-most entry beneath the *page*.
|
|
3309 **
|
|
3310 ** The right-most entry is the one with the largest key - the last
|
|
3311 ** key in ascending order.
|
|
3312 */
|
|
3313 static int moveToRightmost(BtCursor *pCur){
|
|
3314 Pgno pgno;
|
|
3315 int rc;
|
|
3316 MemPage *pPage;
|
|
3317
|
|
3318 assert( pCur->eState==CURSOR_VALID );
|
|
3319 while( !(pPage = pCur->pPage)->leaf ){
|
|
3320 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
|
|
3321 pCur->idx = pPage->nCell;
|
|
3322 rc = moveToChild(pCur, pgno);
|
|
3323 if( rc ) return rc;
|
|
3324 }
|
|
3325 pCur->idx = pPage->nCell - 1;
|
|
3326 pCur->info.nSize = 0;
|
|
3327 return SQLITE_OK;
|
|
3328 }
|
|
3329
|
|
3330 /* Move the cursor to the first entry in the table. Return SQLITE_OK
|
|
3331 ** on success. Set *pRes to 0 if the cursor actually points to something
|
|
3332 ** or set *pRes to 1 if the table is empty.
|
|
3333 */
|
|
3334 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
|
|
3335 int rc;
|
|
3336 rc = moveToRoot(pCur);
|
|
3337 if( rc ) return rc;
|
|
3338 if( pCur->eState==CURSOR_INVALID ){
|
|
3339 assert( pCur->pPage->nCell==0 );
|
|
3340 *pRes = 1;
|
|
3341 return SQLITE_OK;
|
|
3342 }
|
|
3343 assert( pCur->pPage->nCell>0 );
|
|
3344 *pRes = 0;
|
|
3345 rc = moveToLeftmost(pCur);
|
|
3346 return rc;
|
|
3347 }
|
|
3348
|
|
3349 /* Move the cursor to the last entry in the table. Return SQLITE_OK
|
|
3350 ** on success. Set *pRes to 0 if the cursor actually points to something
|
|
3351 ** or set *pRes to 1 if the table is empty.
|
|
3352 */
|
|
3353 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
|
|
3354 int rc;
|
|
3355 rc = moveToRoot(pCur);
|
|
3356 if( rc ) return rc;
|
|
3357 if( CURSOR_INVALID==pCur->eState ){
|
|
3358 assert( pCur->pPage->nCell==0 );
|
|
3359 *pRes = 1;
|
|
3360 return SQLITE_OK;
|
|
3361 }
|
|
3362 assert( pCur->eState==CURSOR_VALID );
|
|
3363 *pRes = 0;
|
|
3364 rc = moveToRightmost(pCur);
|
|
3365 return rc;
|
|
3366 }
|
|
3367
|
|
3368 /* Move the cursor so that it points to an entry near pKey/nKey.
|
|
3369 ** Return a success code.
|
|
3370 **
|
|
3371 ** For INTKEY tables, only the nKey parameter is used. pKey is
|
|
3372 ** ignored. For other tables, nKey is the number of bytes of data
|
|
3373 ** in pKey. The comparison function specified when the cursor was
|
|
3374 ** created is used to compare keys.
|
|
3375 **
|
|
3376 ** If an exact match is not found, then the cursor is always
|
|
3377 ** left pointing at a leaf page which would hold the entry if it
|
|
3378 ** were present. The cursor might point to an entry that comes
|
|
3379 ** before or after the key.
|
|
3380 **
|
|
3381 ** The result of comparing the key with the entry to which the
|
|
3382 ** cursor is written to *pRes if pRes!=NULL. The meaning of
|
|
3383 ** this value is as follows:
|
|
3384 **
|
|
3385 ** *pRes<0 The cursor is left pointing at an entry that
|
|
3386 ** is smaller than pKey or if the table is empty
|
|
3387 ** and the cursor is therefore left point to nothing.
|
|
3388 **
|
|
3389 ** *pRes==0 The cursor is left pointing at an entry that
|
|
3390 ** exactly matches pKey.
|
|
3391 **
|
|
3392 ** *pRes>0 The cursor is left pointing at an entry that
|
|
3393 ** is larger than pKey.
|
|
3394 */
|
|
3395 int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){
|
|
3396 int rc;
|
|
3397 int tryRightmost;
|
|
3398 rc = moveToRoot(pCur);
|
|
3399 if( rc ) return rc;
|
|
3400 assert( pCur->pPage );
|
|
3401 assert( pCur->pPage->isInit );
|
|
3402 tryRightmost = pCur->pPage->intKey;
|
|
3403 if( pCur->eState==CURSOR_INVALID ){
|
|
3404 *pRes = -1;
|
|
3405 assert( pCur->pPage->nCell==0 );
|
|
3406 return SQLITE_OK;
|
|
3407 }
|
|
3408 for(;;){
|
|
3409 int lwr, upr;
|
|
3410 Pgno chldPg;
|
|
3411 MemPage *pPage = pCur->pPage;
|
|
3412 int c = -1; /* pRes return if table is empty must be -1 */
|
|
3413 lwr = 0;
|
|
3414 upr = pPage->nCell-1;
|
|
3415 if( !pPage->intKey && pKey==0 ){
|
|
3416 return SQLITE_CORRUPT_BKPT;
|
|
3417 }
|
|
3418 pageIntegrity(pPage);
|
|
3419 while( lwr<=upr ){
|
|
3420 void *pCellKey;
|
|
3421 i64 nCellKey;
|
|
3422 pCur->idx = (lwr+upr)/2;
|
|
3423 pCur->info.nSize = 0;
|
|
3424 if( pPage->intKey ){
|
|
3425 u8 *pCell;
|
|
3426 if( tryRightmost ){
|
|
3427 pCur->idx = upr;
|
|
3428 }
|
|
3429 pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
|
|
3430 if( pPage->hasData ){
|
|
3431 u32 dummy;
|
|
3432 pCell += getVarint32(pCell, &dummy);
|
|
3433 }
|
|
3434 getVarint(pCell, (u64 *)&nCellKey);
|
|
3435 if( nCellKey<nKey ){
|
|
3436 c = -1;
|
|
3437 }else if( nCellKey>nKey ){
|
|
3438 c = +1;
|
|
3439 tryRightmost = 0;
|
|
3440 }else{
|
|
3441 c = 0;
|
|
3442 }
|
|
3443 }else{
|
|
3444 int available;
|
|
3445 pCellKey = (void *)fetchPayload(pCur, &available, 0);
|
|
3446 nCellKey = pCur->info.nKey;
|
|
3447 if( available>=nCellKey ){
|
|
3448 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
|
|
3449 }else{
|
|
3450 pCellKey = sqliteMallocRaw( nCellKey );
|
|
3451 if( pCellKey==0 ) return SQLITE_NOMEM;
|
|
3452 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
|
|
3453 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
|
|
3454 sqliteFree(pCellKey);
|
|
3455 if( rc ) return rc;
|
|
3456 }
|
|
3457 }
|
|
3458 if( c==0 ){
|
|
3459 if( pPage->leafData && !pPage->leaf ){
|
|
3460 lwr = pCur->idx;
|
|
3461 upr = lwr - 1;
|
|
3462 break;
|
|
3463 }else{
|
|
3464 if( pRes ) *pRes = 0;
|
|
3465 return SQLITE_OK;
|
|
3466 }
|
|
3467 }
|
|
3468 if( c<0 ){
|
|
3469 lwr = pCur->idx+1;
|
|
3470 }else{
|
|
3471 upr = pCur->idx-1;
|
|
3472 }
|
|
3473 }
|
|
3474 assert( lwr==upr+1 );
|
|
3475 assert( pPage->isInit );
|
|
3476 if( pPage->leaf ){
|
|
3477 chldPg = 0;
|
|
3478 }else if( lwr>=pPage->nCell ){
|
|
3479 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
|
|
3480 }else{
|
|
3481 chldPg = get4byte(findCell(pPage, lwr));
|
|
3482 }
|
|
3483 if( chldPg==0 ){
|
|
3484 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
|
|
3485 if( pRes ) *pRes = c;
|
|
3486 return SQLITE_OK;
|
|
3487 }
|
|
3488 pCur->idx = lwr;
|
|
3489 pCur->info.nSize = 0;
|
|
3490 rc = moveToChild(pCur, chldPg);
|
|
3491 if( rc ){
|
|
3492 return rc;
|
|
3493 }
|
|
3494 }
|
|
3495 /* NOT REACHED */
|
|
3496 }
|
|
3497
|
|
3498 /*
|
|
3499 ** Return TRUE if the cursor is not pointing at an entry of the table.
|
|
3500 **
|
|
3501 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
|
|
3502 ** past the last entry in the table or sqlite3BtreePrev() moves past
|
|
3503 ** the first entry. TRUE is also returned if the table is empty.
|
|
3504 */
|
|
3505 int sqlite3BtreeEof(BtCursor *pCur){
|
|
3506 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
|
|
3507 ** have been deleted? This API will need to change to return an error code
|
|
3508 ** as well as the boolean result value.
|
|
3509 */
|
|
3510 return (CURSOR_VALID!=pCur->eState);
|
|
3511 }
|
|
3512
|
|
3513 /*
|
|
3514 ** Advance the cursor to the next entry in the database. If
|
|
3515 ** successful then set *pRes=0. If the cursor
|
|
3516 ** was already pointing to the last entry in the database before
|
|
3517 ** this routine was called, then set *pRes=1.
|
|
3518 */
|
|
3519 int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
|
|
3520 int rc;
|
|
3521 MemPage *pPage;
|
|
3522
|
|
3523 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
3524 rc = restoreOrClearCursorPosition(pCur, 1);
|
|
3525 if( rc!=SQLITE_OK ){
|
|
3526 return rc;
|
|
3527 }
|
|
3528 if( pCur->skip>0 ){
|
|
3529 pCur->skip = 0;
|
|
3530 *pRes = 0;
|
|
3531 return SQLITE_OK;
|
|
3532 }
|
|
3533 pCur->skip = 0;
|
|
3534 #endif
|
|
3535
|
|
3536 assert( pRes!=0 );
|
|
3537 pPage = pCur->pPage;
|
|
3538 if( CURSOR_INVALID==pCur->eState ){
|
|
3539 *pRes = 1;
|
|
3540 return SQLITE_OK;
|
|
3541 }
|
|
3542 assert( pPage->isInit );
|
|
3543 assert( pCur->idx<pPage->nCell );
|
|
3544
|
|
3545 pCur->idx++;
|
|
3546 pCur->info.nSize = 0;
|
|
3547 if( pCur->idx>=pPage->nCell ){
|
|
3548 if( !pPage->leaf ){
|
|
3549 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
|
|
3550 if( rc ) return rc;
|
|
3551 rc = moveToLeftmost(pCur);
|
|
3552 *pRes = 0;
|
|
3553 return rc;
|
|
3554 }
|
|
3555 do{
|
|
3556 if( isRootPage(pPage) ){
|
|
3557 *pRes = 1;
|
|
3558 pCur->eState = CURSOR_INVALID;
|
|
3559 return SQLITE_OK;
|
|
3560 }
|
|
3561 moveToParent(pCur);
|
|
3562 pPage = pCur->pPage;
|
|
3563 }while( pCur->idx>=pPage->nCell );
|
|
3564 *pRes = 0;
|
|
3565 if( pPage->leafData ){
|
|
3566 rc = sqlite3BtreeNext(pCur, pRes);
|
|
3567 }else{
|
|
3568 rc = SQLITE_OK;
|
|
3569 }
|
|
3570 return rc;
|
|
3571 }
|
|
3572 *pRes = 0;
|
|
3573 if( pPage->leaf ){
|
|
3574 return SQLITE_OK;
|
|
3575 }
|
|
3576 rc = moveToLeftmost(pCur);
|
|
3577 return rc;
|
|
3578 }
|
|
3579
|
|
3580 /*
|
|
3581 ** Step the cursor to the back to the previous entry in the database. If
|
|
3582 ** successful then set *pRes=0. If the cursor
|
|
3583 ** was already pointing to the first entry in the database before
|
|
3584 ** this routine was called, then set *pRes=1.
|
|
3585 */
|
|
3586 int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
|
|
3587 int rc;
|
|
3588 Pgno pgno;
|
|
3589 MemPage *pPage;
|
|
3590
|
|
3591 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
3592 rc = restoreOrClearCursorPosition(pCur, 1);
|
|
3593 if( rc!=SQLITE_OK ){
|
|
3594 return rc;
|
|
3595 }
|
|
3596 if( pCur->skip<0 ){
|
|
3597 pCur->skip = 0;
|
|
3598 *pRes = 0;
|
|
3599 return SQLITE_OK;
|
|
3600 }
|
|
3601 pCur->skip = 0;
|
|
3602 #endif
|
|
3603
|
|
3604 if( CURSOR_INVALID==pCur->eState ){
|
|
3605 *pRes = 1;
|
|
3606 return SQLITE_OK;
|
|
3607 }
|
|
3608
|
|
3609 pPage = pCur->pPage;
|
|
3610 assert( pPage->isInit );
|
|
3611 assert( pCur->idx>=0 );
|
|
3612 if( !pPage->leaf ){
|
|
3613 pgno = get4byte( findCell(pPage, pCur->idx) );
|
|
3614 rc = moveToChild(pCur, pgno);
|
|
3615 if( rc ) return rc;
|
|
3616 rc = moveToRightmost(pCur);
|
|
3617 }else{
|
|
3618 while( pCur->idx==0 ){
|
|
3619 if( isRootPage(pPage) ){
|
|
3620 pCur->eState = CURSOR_INVALID;
|
|
3621 *pRes = 1;
|
|
3622 return SQLITE_OK;
|
|
3623 }
|
|
3624 moveToParent(pCur);
|
|
3625 pPage = pCur->pPage;
|
|
3626 }
|
|
3627 pCur->idx--;
|
|
3628 pCur->info.nSize = 0;
|
|
3629 if( pPage->leafData && !pPage->leaf ){
|
|
3630 rc = sqlite3BtreePrevious(pCur, pRes);
|
|
3631 }else{
|
|
3632 rc = SQLITE_OK;
|
|
3633 }
|
|
3634 }
|
|
3635 *pRes = 0;
|
|
3636 return rc;
|
|
3637 }
|
|
3638
|
|
3639 /*
|
|
3640 ** Allocate a new page from the database file.
|
|
3641 **
|
|
3642 ** The new page is marked as dirty. (In other words, sqlite3pager_write()
|
|
3643 ** has already been called on the new page.) The new page has also
|
|
3644 ** been referenced and the calling routine is responsible for calling
|
|
3645 ** sqlite3pager_unref() on the new page when it is done.
|
|
3646 **
|
|
3647 ** SQLITE_OK is returned on success. Any other return value indicates
|
|
3648 ** an error. *ppPage and *pPgno are undefined in the event of an error.
|
|
3649 ** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned.
|
|
3650 **
|
|
3651 ** If the "nearby" parameter is not 0, then a (feeble) effort is made to
|
|
3652 ** locate a page close to the page number "nearby". This can be used in an
|
|
3653 ** attempt to keep related pages close to each other in the database file,
|
|
3654 ** which in turn can make database access faster.
|
|
3655 **
|
|
3656 ** If the "exact" parameter is not 0, and the page-number nearby exists
|
|
3657 ** anywhere on the free-list, then it is guarenteed to be returned. This
|
|
3658 ** is only used by auto-vacuum databases when allocating a new table.
|
|
3659 */
|
|
3660 static int allocatePage(
|
|
3661 BtShared *pBt,
|
|
3662 MemPage **ppPage,
|
|
3663 Pgno *pPgno,
|
|
3664 Pgno nearby,
|
|
3665 u8 exact
|
|
3666 ){
|
|
3667 MemPage *pPage1;
|
|
3668 int rc;
|
|
3669 int n; /* Number of pages on the freelist */
|
|
3670 int k; /* Number of leaves on the trunk of the freelist */
|
|
3671
|
|
3672 pPage1 = pBt->pPage1;
|
|
3673 n = get4byte(&pPage1->aData[36]);
|
|
3674 if( n>0 ){
|
|
3675 /* There are pages on the freelist. Reuse one of those pages. */
|
|
3676 MemPage *pTrunk = 0;
|
|
3677 Pgno iTrunk;
|
|
3678 MemPage *pPrevTrunk = 0;
|
|
3679 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
|
|
3680
|
|
3681 /* If the 'exact' parameter was true and a query of the pointer-map
|
|
3682 ** shows that the page 'nearby' is somewhere on the free-list, then
|
|
3683 ** the entire-list will be searched for that page.
|
|
3684 */
|
|
3685 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
3686 if( exact ){
|
|
3687 u8 eType;
|
|
3688 assert( nearby>0 );
|
|
3689 assert( pBt->autoVacuum );
|
|
3690 rc = ptrmapGet(pBt, nearby, &eType, 0);
|
|
3691 if( rc ) return rc;
|
|
3692 if( eType==PTRMAP_FREEPAGE ){
|
|
3693 searchList = 1;
|
|
3694 }
|
|
3695 *pPgno = nearby;
|
|
3696 }
|
|
3697 #endif
|
|
3698
|
|
3699 /* Decrement the free-list count by 1. Set iTrunk to the index of the
|
|
3700 ** first free-list trunk page. iPrevTrunk is initially 1.
|
|
3701 */
|
|
3702 rc = sqlite3pager_write(pPage1->aData);
|
|
3703 if( rc ) return rc;
|
|
3704 put4byte(&pPage1->aData[36], n-1);
|
|
3705
|
|
3706 /* The code within this loop is run only once if the 'searchList' variable
|
|
3707 ** is not true. Otherwise, it runs once for each trunk-page on the
|
|
3708 ** free-list until the page 'nearby' is located.
|
|
3709 */
|
|
3710 do {
|
|
3711 pPrevTrunk = pTrunk;
|
|
3712 if( pPrevTrunk ){
|
|
3713 iTrunk = get4byte(&pPrevTrunk->aData[0]);
|
|
3714 }else{
|
|
3715 iTrunk = get4byte(&pPage1->aData[32]);
|
|
3716 }
|
|
3717 rc = getPage(pBt, iTrunk, &pTrunk);
|
|
3718 if( rc ){
|
|
3719 releasePage(pPrevTrunk);
|
|
3720 return rc;
|
|
3721 }
|
|
3722
|
|
3723 /* TODO: This should move to after the loop? */
|
|
3724 rc = sqlite3pager_write(pTrunk->aData);
|
|
3725 if( rc ){
|
|
3726 releasePage(pTrunk);
|
|
3727 releasePage(pPrevTrunk);
|
|
3728 return rc;
|
|
3729 }
|
|
3730
|
|
3731 k = get4byte(&pTrunk->aData[4]);
|
|
3732 if( k==0 && !searchList ){
|
|
3733 /* The trunk has no leaves and the list is not being searched.
|
|
3734 ** So extract the trunk page itself and use it as the newly
|
|
3735 ** allocated page */
|
|
3736 assert( pPrevTrunk==0 );
|
|
3737 *pPgno = iTrunk;
|
|
3738 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
|
|
3739 *ppPage = pTrunk;
|
|
3740 pTrunk = 0;
|
|
3741 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
|
|
3742 }else if( k>pBt->usableSize/4 - 8 ){
|
|
3743 /* Value of k is out of range. Database corruption */
|
|
3744 return SQLITE_CORRUPT_BKPT;
|
|
3745 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
3746 }else if( searchList && nearby==iTrunk ){
|
|
3747 /* The list is being searched and this trunk page is the page
|
|
3748 ** to allocate, regardless of whether it has leaves.
|
|
3749 */
|
|
3750 assert( *pPgno==iTrunk );
|
|
3751 *ppPage = pTrunk;
|
|
3752 searchList = 0;
|
|
3753 if( k==0 ){
|
|
3754 if( !pPrevTrunk ){
|
|
3755 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
|
|
3756 }else{
|
|
3757 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
|
|
3758 }
|
|
3759 }else{
|
|
3760 /* The trunk page is required by the caller but it contains
|
|
3761 ** pointers to free-list leaves. The first leaf becomes a trunk
|
|
3762 ** page in this case.
|
|
3763 */
|
|
3764 MemPage *pNewTrunk;
|
|
3765 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
|
|
3766 rc = getPage(pBt, iNewTrunk, &pNewTrunk);
|
|
3767 if( rc!=SQLITE_OK ){
|
|
3768 releasePage(pTrunk);
|
|
3769 releasePage(pPrevTrunk);
|
|
3770 return rc;
|
|
3771 }
|
|
3772 rc = sqlite3pager_write(pNewTrunk->aData);
|
|
3773 if( rc!=SQLITE_OK ){
|
|
3774 releasePage(pNewTrunk);
|
|
3775 releasePage(pTrunk);
|
|
3776 releasePage(pPrevTrunk);
|
|
3777 return rc;
|
|
3778 }
|
|
3779 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
|
|
3780 put4byte(&pNewTrunk->aData[4], k-1);
|
|
3781 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
|
|
3782 if( !pPrevTrunk ){
|
|
3783 put4byte(&pPage1->aData[32], iNewTrunk);
|
|
3784 }else{
|
|
3785 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
|
|
3786 }
|
|
3787 releasePage(pNewTrunk);
|
|
3788 }
|
|
3789 pTrunk = 0;
|
|
3790 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
|
|
3791 #endif
|
|
3792 }else{
|
|
3793 /* Extract a leaf from the trunk */
|
|
3794 int closest;
|
|
3795 Pgno iPage;
|
|
3796 unsigned char *aData = pTrunk->aData;
|
|
3797 if( nearby>0 ){
|
|
3798 int i, dist;
|
|
3799 closest = 0;
|
|
3800 dist = get4byte(&aData[8]) - nearby;
|
|
3801 if( dist<0 ) dist = -dist;
|
|
3802 for(i=1; i<k; i++){
|
|
3803 int d2 = get4byte(&aData[8+i*4]) - nearby;
|
|
3804 if( d2<0 ) d2 = -d2;
|
|
3805 if( d2<dist ){
|
|
3806 closest = i;
|
|
3807 dist = d2;
|
|
3808 }
|
|
3809 }
|
|
3810 }else{
|
|
3811 closest = 0;
|
|
3812 }
|
|
3813
|
|
3814 iPage = get4byte(&aData[8+closest*4]);
|
|
3815 if( !searchList || iPage==nearby ){
|
|
3816 *pPgno = iPage;
|
|
3817 if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){
|
|
3818 /* Free page off the end of the file */
|
|
3819 return SQLITE_CORRUPT_BKPT;
|
|
3820 }
|
|
3821 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
|
|
3822 ": %d more free pages\n",
|
|
3823 *pPgno, closest+1, k, pTrunk->pgno, n-1));
|
|
3824 if( closest<k-1 ){
|
|
3825 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
|
|
3826 }
|
|
3827 put4byte(&aData[4], k-1);
|
|
3828 rc = getPage(pBt, *pPgno, ppPage);
|
|
3829 if( rc==SQLITE_OK ){
|
|
3830 sqlite3pager_dont_rollback((*ppPage)->aData);
|
|
3831 rc = sqlite3pager_write((*ppPage)->aData);
|
|
3832 if( rc!=SQLITE_OK ){
|
|
3833 releasePage(*ppPage);
|
|
3834 }
|
|
3835 }
|
|
3836 searchList = 0;
|
|
3837 }
|
|
3838 }
|
|
3839 releasePage(pPrevTrunk);
|
|
3840 }while( searchList );
|
|
3841 releasePage(pTrunk);
|
|
3842 }else{
|
|
3843 /* There are no pages on the freelist, so create a new page at the
|
|
3844 ** end of the file */
|
|
3845 *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1;
|
|
3846
|
|
3847 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
3848 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
|
|
3849 /* If *pPgno refers to a pointer-map page, allocate two new pages
|
|
3850 ** at the end of the file instead of one. The first allocated page
|
|
3851 ** becomes a new pointer-map page, the second is used by the caller.
|
|
3852 */
|
|
3853 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
|
|
3854 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
|
|
3855 (*pPgno)++;
|
|
3856 }
|
|
3857 #endif
|
|
3858
|
|
3859 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
|
|
3860 rc = getPage(pBt, *pPgno, ppPage);
|
|
3861 if( rc ) return rc;
|
|
3862 rc = sqlite3pager_write((*ppPage)->aData);
|
|
3863 if( rc!=SQLITE_OK ){
|
|
3864 releasePage(*ppPage);
|
|
3865 }
|
|
3866 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
|
|
3867 }
|
|
3868
|
|
3869 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
|
|
3870 return rc;
|
|
3871 }
|
|
3872
|
|
3873 /*
|
|
3874 ** Add a page of the database file to the freelist.
|
|
3875 **
|
|
3876 ** sqlite3pager_unref() is NOT called for pPage.
|
|
3877 */
|
|
3878 static int freePage(MemPage *pPage){
|
|
3879 BtShared *pBt = pPage->pBt;
|
|
3880 MemPage *pPage1 = pBt->pPage1;
|
|
3881 int rc, n, k;
|
|
3882
|
|
3883 /* Prepare the page for freeing */
|
|
3884 assert( pPage->pgno>1 );
|
|
3885 pPage->isInit = 0;
|
|
3886 releasePage(pPage->pParent);
|
|
3887 pPage->pParent = 0;
|
|
3888
|
|
3889 /* Increment the free page count on pPage1 */
|
|
3890 rc = sqlite3pager_write(pPage1->aData);
|
|
3891 if( rc ) return rc;
|
|
3892 n = get4byte(&pPage1->aData[36]);
|
|
3893 put4byte(&pPage1->aData[36], n+1);
|
|
3894
|
|
3895 #ifdef SQLITE_SECURE_DELETE
|
|
3896 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
|
|
3897 ** always fully overwrite deleted information with zeros.
|
|
3898 */
|
|
3899 rc = sqlite3pager_write(pPage->aData);
|
|
3900 if( rc ) return rc;
|
|
3901 memset(pPage->aData, 0, pPage->pBt->pageSize);
|
|
3902 #endif
|
|
3903
|
|
3904 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
3905 /* If the database supports auto-vacuum, write an entry in the pointer-map
|
|
3906 ** to indicate that the page is free.
|
|
3907 */
|
|
3908 if( pBt->autoVacuum ){
|
|
3909 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
|
|
3910 if( rc ) return rc;
|
|
3911 }
|
|
3912 #endif
|
|
3913
|
|
3914 if( n==0 ){
|
|
3915 /* This is the first free page */
|
|
3916 rc = sqlite3pager_write(pPage->aData);
|
|
3917 if( rc ) return rc;
|
|
3918 memset(pPage->aData, 0, 8);
|
|
3919 put4byte(&pPage1->aData[32], pPage->pgno);
|
|
3920 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
|
|
3921 }else{
|
|
3922 /* Other free pages already exist. Retrive the first trunk page
|
|
3923 ** of the freelist and find out how many leaves it has. */
|
|
3924 MemPage *pTrunk;
|
|
3925 rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk);
|
|
3926 if( rc ) return rc;
|
|
3927 k = get4byte(&pTrunk->aData[4]);
|
|
3928 if( k>=pBt->usableSize/4 - 8 ){
|
|
3929 /* The trunk is full. Turn the page being freed into a new
|
|
3930 ** trunk page with no leaves. */
|
|
3931 rc = sqlite3pager_write(pPage->aData);
|
|
3932 if( rc ) return rc;
|
|
3933 put4byte(pPage->aData, pTrunk->pgno);
|
|
3934 put4byte(&pPage->aData[4], 0);
|
|
3935 put4byte(&pPage1->aData[32], pPage->pgno);
|
|
3936 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
|
|
3937 pPage->pgno, pTrunk->pgno));
|
|
3938 }else{
|
|
3939 /* Add the newly freed page as a leaf on the current trunk */
|
|
3940 rc = sqlite3pager_write(pTrunk->aData);
|
|
3941 if( rc ) return rc;
|
|
3942 put4byte(&pTrunk->aData[4], k+1);
|
|
3943 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
|
|
3944 #ifndef SQLITE_SECURE_DELETE
|
|
3945 sqlite3pager_dont_write(pBt->pPager, pPage->pgno);
|
|
3946 #endif
|
|
3947 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
|
|
3948 }
|
|
3949 releasePage(pTrunk);
|
|
3950 }
|
|
3951 return rc;
|
|
3952 }
|
|
3953
|
|
3954 /*
|
|
3955 ** Free any overflow pages associated with the given Cell.
|
|
3956 */
|
|
3957 static int clearCell(MemPage *pPage, unsigned char *pCell){
|
|
3958 BtShared *pBt = pPage->pBt;
|
|
3959 CellInfo info;
|
|
3960 Pgno ovflPgno;
|
|
3961 int rc;
|
|
3962
|
|
3963 parseCellPtr(pPage, pCell, &info);
|
|
3964 if( info.iOverflow==0 ){
|
|
3965 return SQLITE_OK; /* No overflow pages. Return without doing anything */
|
|
3966 }
|
|
3967 ovflPgno = get4byte(&pCell[info.iOverflow]);
|
|
3968 while( ovflPgno!=0 ){
|
|
3969 MemPage *pOvfl;
|
|
3970 if( ovflPgno>sqlite3pager_pagecount(pBt->pPager) ){
|
|
3971 return SQLITE_CORRUPT_BKPT;
|
|
3972 }
|
|
3973 rc = getPage(pBt, ovflPgno, &pOvfl);
|
|
3974 if( rc ) return rc;
|
|
3975 ovflPgno = get4byte(pOvfl->aData);
|
|
3976 rc = freePage(pOvfl);
|
|
3977 sqlite3pager_unref(pOvfl->aData);
|
|
3978 if( rc ) return rc;
|
|
3979 }
|
|
3980 return SQLITE_OK;
|
|
3981 }
|
|
3982
|
|
3983 /*
|
|
3984 ** Create the byte sequence used to represent a cell on page pPage
|
|
3985 ** and write that byte sequence into pCell[]. Overflow pages are
|
|
3986 ** allocated and filled in as necessary. The calling procedure
|
|
3987 ** is responsible for making sure sufficient space has been allocated
|
|
3988 ** for pCell[].
|
|
3989 **
|
|
3990 ** Note that pCell does not necessary need to point to the pPage->aData
|
|
3991 ** area. pCell might point to some temporary storage. The cell will
|
|
3992 ** be constructed in this temporary area then copied into pPage->aData
|
|
3993 ** later.
|
|
3994 */
|
|
3995 static int fillInCell(
|
|
3996 MemPage *pPage, /* The page that contains the cell */
|
|
3997 unsigned char *pCell, /* Complete text of the cell */
|
|
3998 const void *pKey, i64 nKey, /* The key */
|
|
3999 const void *pData,int nData, /* The data */
|
|
4000 int *pnSize /* Write cell size here */
|
|
4001 ){
|
|
4002 int nPayload;
|
|
4003 const u8 *pSrc;
|
|
4004 int nSrc, n, rc;
|
|
4005 int spaceLeft;
|
|
4006 MemPage *pOvfl = 0;
|
|
4007 MemPage *pToRelease = 0;
|
|
4008 unsigned char *pPrior;
|
|
4009 unsigned char *pPayload;
|
|
4010 BtShared *pBt = pPage->pBt;
|
|
4011 Pgno pgnoOvfl = 0;
|
|
4012 int nHeader;
|
|
4013 CellInfo info;
|
|
4014
|
|
4015 /* Fill in the header. */
|
|
4016 nHeader = 0;
|
|
4017 if( !pPage->leaf ){
|
|
4018 nHeader += 4;
|
|
4019 }
|
|
4020 if( pPage->hasData ){
|
|
4021 nHeader += putVarint(&pCell[nHeader], nData);
|
|
4022 }else{
|
|
4023 nData = 0;
|
|
4024 }
|
|
4025 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
|
|
4026 parseCellPtr(pPage, pCell, &info);
|
|
4027 assert( info.nHeader==nHeader );
|
|
4028 assert( info.nKey==nKey );
|
|
4029 assert( info.nData==nData );
|
|
4030
|
|
4031 /* Fill in the payload */
|
|
4032 nPayload = nData;
|
|
4033 if( pPage->intKey ){
|
|
4034 pSrc = pData;
|
|
4035 nSrc = nData;
|
|
4036 nData = 0;
|
|
4037 }else{
|
|
4038 nPayload += nKey;
|
|
4039 pSrc = pKey;
|
|
4040 nSrc = nKey;
|
|
4041 }
|
|
4042 *pnSize = info.nSize;
|
|
4043 spaceLeft = info.nLocal;
|
|
4044 pPayload = &pCell[nHeader];
|
|
4045 pPrior = &pCell[info.iOverflow];
|
|
4046
|
|
4047 while( nPayload>0 ){
|
|
4048 if( spaceLeft==0 ){
|
|
4049 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4050 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
|
|
4051 #endif
|
|
4052 rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
|
|
4053 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4054 /* If the database supports auto-vacuum, and the second or subsequent
|
|
4055 ** overflow page is being allocated, add an entry to the pointer-map
|
|
4056 ** for that page now. The entry for the first overflow page will be
|
|
4057 ** added later, by the insertCell() routine.
|
|
4058 */
|
|
4059 if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
|
|
4060 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap);
|
|
4061 }
|
|
4062 #endif
|
|
4063 if( rc ){
|
|
4064 releasePage(pToRelease);
|
|
4065 /* clearCell(pPage, pCell); */
|
|
4066 return rc;
|
|
4067 }
|
|
4068 put4byte(pPrior, pgnoOvfl);
|
|
4069 releasePage(pToRelease);
|
|
4070 pToRelease = pOvfl;
|
|
4071 pPrior = pOvfl->aData;
|
|
4072 put4byte(pPrior, 0);
|
|
4073 pPayload = &pOvfl->aData[4];
|
|
4074 spaceLeft = pBt->usableSize - 4;
|
|
4075 }
|
|
4076 n = nPayload;
|
|
4077 if( n>spaceLeft ) n = spaceLeft;
|
|
4078 if( n>nSrc ) n = nSrc;
|
|
4079 assert( pSrc );
|
|
4080 memcpy(pPayload, pSrc, n);
|
|
4081 nPayload -= n;
|
|
4082 pPayload += n;
|
|
4083 pSrc += n;
|
|
4084 nSrc -= n;
|
|
4085 spaceLeft -= n;
|
|
4086 if( nSrc==0 ){
|
|
4087 nSrc = nData;
|
|
4088 pSrc = pData;
|
|
4089 }
|
|
4090 }
|
|
4091 releasePage(pToRelease);
|
|
4092 return SQLITE_OK;
|
|
4093 }
|
|
4094
|
|
4095 /*
|
|
4096 ** Change the MemPage.pParent pointer on the page whose number is
|
|
4097 ** given in the second argument so that MemPage.pParent holds the
|
|
4098 ** pointer in the third argument.
|
|
4099 */
|
|
4100 static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
|
|
4101 MemPage *pThis;
|
|
4102 unsigned char *aData;
|
|
4103
|
|
4104 assert( pNewParent!=0 );
|
|
4105 if( pgno==0 ) return SQLITE_OK;
|
|
4106 assert( pBt->pPager!=0 );
|
|
4107 aData = sqlite3pager_lookup(pBt->pPager, pgno);
|
|
4108 if( aData ){
|
|
4109 pThis = (MemPage*)&aData[pBt->pageSize];
|
|
4110 assert( pThis->aData==aData );
|
|
4111 if( pThis->isInit ){
|
|
4112 if( pThis->pParent!=pNewParent ){
|
|
4113 if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData);
|
|
4114 pThis->pParent = pNewParent;
|
|
4115 sqlite3pager_ref(pNewParent->aData);
|
|
4116 }
|
|
4117 pThis->idxParent = idx;
|
|
4118 }
|
|
4119 sqlite3pager_unref(aData);
|
|
4120 }
|
|
4121
|
|
4122 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4123 if( pBt->autoVacuum ){
|
|
4124 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
|
|
4125 }
|
|
4126 #endif
|
|
4127 return SQLITE_OK;
|
|
4128 }
|
|
4129
|
|
4130
|
|
4131
|
|
4132 /*
|
|
4133 ** Change the pParent pointer of all children of pPage to point back
|
|
4134 ** to pPage.
|
|
4135 **
|
|
4136 ** In other words, for every child of pPage, invoke reparentPage()
|
|
4137 ** to make sure that each child knows that pPage is its parent.
|
|
4138 **
|
|
4139 ** This routine gets called after you memcpy() one page into
|
|
4140 ** another.
|
|
4141 */
|
|
4142 static int reparentChildPages(MemPage *pPage){
|
|
4143 int i;
|
|
4144 BtShared *pBt = pPage->pBt;
|
|
4145 int rc = SQLITE_OK;
|
|
4146
|
|
4147 if( pPage->leaf ) return SQLITE_OK;
|
|
4148
|
|
4149 for(i=0; i<pPage->nCell; i++){
|
|
4150 u8 *pCell = findCell(pPage, i);
|
|
4151 if( !pPage->leaf ){
|
|
4152 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
|
|
4153 if( rc!=SQLITE_OK ) return rc;
|
|
4154 }
|
|
4155 }
|
|
4156 if( !pPage->leaf ){
|
|
4157 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
|
|
4158 pPage, i);
|
|
4159 pPage->idxShift = 0;
|
|
4160 }
|
|
4161 return rc;
|
|
4162 }
|
|
4163
|
|
4164 /*
|
|
4165 ** Remove the i-th cell from pPage. This routine effects pPage only.
|
|
4166 ** The cell content is not freed or deallocated. It is assumed that
|
|
4167 ** the cell content has been copied someplace else. This routine just
|
|
4168 ** removes the reference to the cell from pPage.
|
|
4169 **
|
|
4170 ** "sz" must be the number of bytes in the cell.
|
|
4171 */
|
|
4172 static void dropCell(MemPage *pPage, int idx, int sz){
|
|
4173 int i; /* Loop counter */
|
|
4174 int pc; /* Offset to cell content of cell being deleted */
|
|
4175 u8 *data; /* pPage->aData */
|
|
4176 u8 *ptr; /* Used to move bytes around within data[] */
|
|
4177
|
|
4178 assert( idx>=0 && idx<pPage->nCell );
|
|
4179 assert( sz==cellSize(pPage, idx) );
|
|
4180 assert( sqlite3pager_iswriteable(pPage->aData) );
|
|
4181 data = pPage->aData;
|
|
4182 ptr = &data[pPage->cellOffset + 2*idx];
|
|
4183 pc = get2byte(ptr);
|
|
4184 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
|
|
4185 freeSpace(pPage, pc, sz);
|
|
4186 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
|
|
4187 ptr[0] = ptr[2];
|
|
4188 ptr[1] = ptr[3];
|
|
4189 }
|
|
4190 pPage->nCell--;
|
|
4191 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
|
|
4192 pPage->nFree += 2;
|
|
4193 pPage->idxShift = 1;
|
|
4194 }
|
|
4195
|
|
4196 /*
|
|
4197 ** Insert a new cell on pPage at cell index "i". pCell points to the
|
|
4198 ** content of the cell.
|
|
4199 **
|
|
4200 ** If the cell content will fit on the page, then put it there. If it
|
|
4201 ** will not fit, then make a copy of the cell content into pTemp if
|
|
4202 ** pTemp is not null. Regardless of pTemp, allocate a new entry
|
|
4203 ** in pPage->aOvfl[] and make it point to the cell content (either
|
|
4204 ** in pTemp or the original pCell) and also record its index.
|
|
4205 ** Allocating a new entry in pPage->aCell[] implies that
|
|
4206 ** pPage->nOverflow is incremented.
|
|
4207 **
|
|
4208 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
|
|
4209 ** cell. The caller will overwrite them after this function returns. If
|
|
4210 ** nSkip is non-zero, then pCell may not point to an invalid memory location
|
|
4211 ** (but pCell+nSkip is always valid).
|
|
4212 */
|
|
4213 static int insertCell(
|
|
4214 MemPage *pPage, /* Page into which we are copying */
|
|
4215 int i, /* New cell becomes the i-th cell of the page */
|
|
4216 u8 *pCell, /* Content of the new cell */
|
|
4217 int sz, /* Bytes of content in pCell */
|
|
4218 u8 *pTemp, /* Temp storage space for pCell, if needed */
|
|
4219 u8 nSkip /* Do not write the first nSkip bytes of the cell */
|
|
4220 ){
|
|
4221 int idx; /* Where to write new cell content in data[] */
|
|
4222 int j; /* Loop counter */
|
|
4223 int top; /* First byte of content for any cell in data[] */
|
|
4224 int end; /* First byte past the last cell pointer in data[] */
|
|
4225 int ins; /* Index in data[] where new cell pointer is inserted */
|
|
4226 int hdr; /* Offset into data[] of the page header */
|
|
4227 int cellOffset; /* Address of first cell pointer in data[] */
|
|
4228 u8 *data; /* The content of the whole page */
|
|
4229 u8 *ptr; /* Used for moving information around in data[] */
|
|
4230
|
|
4231 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
|
|
4232 assert( sz==cellSizePtr(pPage, pCell) );
|
|
4233 assert( sqlite3pager_iswriteable(pPage->aData) );
|
|
4234 if( pPage->nOverflow || sz+2>pPage->nFree ){
|
|
4235 if( pTemp ){
|
|
4236 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
|
|
4237 pCell = pTemp;
|
|
4238 }
|
|
4239 j = pPage->nOverflow++;
|
|
4240 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
|
|
4241 pPage->aOvfl[j].pCell = pCell;
|
|
4242 pPage->aOvfl[j].idx = i;
|
|
4243 pPage->nFree = 0;
|
|
4244 }else{
|
|
4245 data = pPage->aData;
|
|
4246 hdr = pPage->hdrOffset;
|
|
4247 top = get2byte(&data[hdr+5]);
|
|
4248 cellOffset = pPage->cellOffset;
|
|
4249 end = cellOffset + 2*pPage->nCell + 2;
|
|
4250 ins = cellOffset + 2*i;
|
|
4251 if( end > top - sz ){
|
|
4252 int rc = defragmentPage(pPage);
|
|
4253 if( rc!=SQLITE_OK ) return rc;
|
|
4254 top = get2byte(&data[hdr+5]);
|
|
4255 assert( end + sz <= top );
|
|
4256 }
|
|
4257 idx = allocateSpace(pPage, sz);
|
|
4258 assert( idx>0 );
|
|
4259 assert( end <= get2byte(&data[hdr+5]) );
|
|
4260 pPage->nCell++;
|
|
4261 pPage->nFree -= 2;
|
|
4262 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
|
|
4263 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
|
|
4264 ptr[0] = ptr[-2];
|
|
4265 ptr[1] = ptr[-1];
|
|
4266 }
|
|
4267 put2byte(&data[ins], idx);
|
|
4268 put2byte(&data[hdr+3], pPage->nCell);
|
|
4269 pPage->idxShift = 1;
|
|
4270 pageIntegrity(pPage);
|
|
4271 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4272 if( pPage->pBt->autoVacuum ){
|
|
4273 /* The cell may contain a pointer to an overflow page. If so, write
|
|
4274 ** the entry for the overflow page into the pointer map.
|
|
4275 */
|
|
4276 CellInfo info;
|
|
4277 parseCellPtr(pPage, pCell, &info);
|
|
4278 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
|
|
4279 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
|
|
4280 int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
|
|
4281 if( rc!=SQLITE_OK ) return rc;
|
|
4282 }
|
|
4283 }
|
|
4284 #endif
|
|
4285 }
|
|
4286
|
|
4287 return SQLITE_OK;
|
|
4288 }
|
|
4289
|
|
4290 /*
|
|
4291 ** Add a list of cells to a page. The page should be initially empty.
|
|
4292 ** The cells are guaranteed to fit on the page.
|
|
4293 */
|
|
4294 static void assemblePage(
|
|
4295 MemPage *pPage, /* The page to be assemblied */
|
|
4296 int nCell, /* The number of cells to add to this page */
|
|
4297 u8 **apCell, /* Pointers to cell bodies */
|
|
4298 int *aSize /* Sizes of the cells */
|
|
4299 ){
|
|
4300 int i; /* Loop counter */
|
|
4301 int totalSize; /* Total size of all cells */
|
|
4302 int hdr; /* Index of page header */
|
|
4303 int cellptr; /* Address of next cell pointer */
|
|
4304 int cellbody; /* Address of next cell body */
|
|
4305 u8 *data; /* Data for the page */
|
|
4306
|
|
4307 assert( pPage->nOverflow==0 );
|
|
4308 totalSize = 0;
|
|
4309 for(i=0; i<nCell; i++){
|
|
4310 totalSize += aSize[i];
|
|
4311 }
|
|
4312 assert( totalSize+2*nCell<=pPage->nFree );
|
|
4313 assert( pPage->nCell==0 );
|
|
4314 cellptr = pPage->cellOffset;
|
|
4315 data = pPage->aData;
|
|
4316 hdr = pPage->hdrOffset;
|
|
4317 put2byte(&data[hdr+3], nCell);
|
|
4318 if( nCell ){
|
|
4319 cellbody = allocateSpace(pPage, totalSize);
|
|
4320 assert( cellbody>0 );
|
|
4321 assert( pPage->nFree >= 2*nCell );
|
|
4322 pPage->nFree -= 2*nCell;
|
|
4323 for(i=0; i<nCell; i++){
|
|
4324 put2byte(&data[cellptr], cellbody);
|
|
4325 memcpy(&data[cellbody], apCell[i], aSize[i]);
|
|
4326 cellptr += 2;
|
|
4327 cellbody += aSize[i];
|
|
4328 }
|
|
4329 assert( cellbody==pPage->pBt->usableSize );
|
|
4330 }
|
|
4331 pPage->nCell = nCell;
|
|
4332 }
|
|
4333
|
|
4334 /*
|
|
4335 ** The following parameters determine how many adjacent pages get involved
|
|
4336 ** in a balancing operation. NN is the number of neighbors on either side
|
|
4337 ** of the page that participate in the balancing operation. NB is the
|
|
4338 ** total number of pages that participate, including the target page and
|
|
4339 ** NN neighbors on either side.
|
|
4340 **
|
|
4341 ** The minimum value of NN is 1 (of course). Increasing NN above 1
|
|
4342 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
|
|
4343 ** in exchange for a larger degradation in INSERT and UPDATE performance.
|
|
4344 ** The value of NN appears to give the best results overall.
|
|
4345 */
|
|
4346 #define NN 1 /* Number of neighbors on either side of pPage */
|
|
4347 #define NB (NN*2+1) /* Total pages involved in the balance */
|
|
4348
|
|
4349 /* Forward reference */
|
|
4350 static int balance(MemPage*, int);
|
|
4351
|
|
4352 #ifndef SQLITE_OMIT_QUICKBALANCE
|
|
4353 /*
|
|
4354 ** This version of balance() handles the common special case where
|
|
4355 ** a new entry is being inserted on the extreme right-end of the
|
|
4356 ** tree, in other words, when the new entry will become the largest
|
|
4357 ** entry in the tree.
|
|
4358 **
|
|
4359 ** Instead of trying balance the 3 right-most leaf pages, just add
|
|
4360 ** a new page to the right-hand side and put the one new entry in
|
|
4361 ** that page. This leaves the right side of the tree somewhat
|
|
4362 ** unbalanced. But odds are that we will be inserting new entries
|
|
4363 ** at the end soon afterwards so the nearly empty page will quickly
|
|
4364 ** fill up. On average.
|
|
4365 **
|
|
4366 ** pPage is the leaf page which is the right-most page in the tree.
|
|
4367 ** pParent is its parent. pPage must have a single overflow entry
|
|
4368 ** which is also the right-most entry on the page.
|
|
4369 */
|
|
4370 static int balance_quick(MemPage *pPage, MemPage *pParent){
|
|
4371 int rc;
|
|
4372 MemPage *pNew;
|
|
4373 Pgno pgnoNew;
|
|
4374 u8 *pCell;
|
|
4375 int szCell;
|
|
4376 CellInfo info;
|
|
4377 BtShared *pBt = pPage->pBt;
|
|
4378 int parentIdx = pParent->nCell; /* pParent new divider cell index */
|
|
4379 int parentSize; /* Size of new divider cell */
|
|
4380 u8 parentCell[64]; /* Space for the new divider cell */
|
|
4381
|
|
4382 /* Allocate a new page. Insert the overflow cell from pPage
|
|
4383 ** into it. Then remove the overflow cell from pPage.
|
|
4384 */
|
|
4385 rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0);
|
|
4386 if( rc!=SQLITE_OK ){
|
|
4387 return rc;
|
|
4388 }
|
|
4389 pCell = pPage->aOvfl[0].pCell;
|
|
4390 szCell = cellSizePtr(pPage, pCell);
|
|
4391 zeroPage(pNew, pPage->aData[0]);
|
|
4392 assemblePage(pNew, 1, &pCell, &szCell);
|
|
4393 pPage->nOverflow = 0;
|
|
4394
|
|
4395 /* Set the parent of the newly allocated page to pParent. */
|
|
4396 pNew->pParent = pParent;
|
|
4397 sqlite3pager_ref(pParent->aData);
|
|
4398
|
|
4399 /* pPage is currently the right-child of pParent. Change this
|
|
4400 ** so that the right-child is the new page allocated above and
|
|
4401 ** pPage is the next-to-right child.
|
|
4402 */
|
|
4403 assert( pPage->nCell>0 );
|
|
4404 parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info);
|
|
4405 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize);
|
|
4406 if( rc!=SQLITE_OK ){
|
|
4407 return rc;
|
|
4408 }
|
|
4409 assert( parentSize<64 );
|
|
4410 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
|
|
4411 if( rc!=SQLITE_OK ){
|
|
4412 return rc;
|
|
4413 }
|
|
4414 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
|
|
4415 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
|
|
4416
|
|
4417 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4418 /* If this is an auto-vacuum database, update the pointer map
|
|
4419 ** with entries for the new page, and any pointer from the
|
|
4420 ** cell on the page to an overflow page.
|
|
4421 */
|
|
4422 if( pBt->autoVacuum ){
|
|
4423 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
|
|
4424 if( rc!=SQLITE_OK ){
|
|
4425 return rc;
|
|
4426 }
|
|
4427 rc = ptrmapPutOvfl(pNew, 0);
|
|
4428 if( rc!=SQLITE_OK ){
|
|
4429 return rc;
|
|
4430 }
|
|
4431 }
|
|
4432 #endif
|
|
4433
|
|
4434 /* Release the reference to the new page and balance the parent page,
|
|
4435 ** in case the divider cell inserted caused it to become overfull.
|
|
4436 */
|
|
4437 releasePage(pNew);
|
|
4438 return balance(pParent, 0);
|
|
4439 }
|
|
4440 #endif /* SQLITE_OMIT_QUICKBALANCE */
|
|
4441
|
|
4442 /*
|
|
4443 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
|
|
4444 ** if the database supports auto-vacuum or not. Because it is used
|
|
4445 ** within an expression that is an argument to another macro
|
|
4446 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
|
|
4447 ** So, this macro is defined instead.
|
|
4448 */
|
|
4449 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4450 #define ISAUTOVACUUM (pBt->autoVacuum)
|
|
4451 #else
|
|
4452 #define ISAUTOVACUUM 0
|
|
4453 #endif
|
|
4454
|
|
4455 /*
|
|
4456 ** This routine redistributes Cells on pPage and up to NN*2 siblings
|
|
4457 ** of pPage so that all pages have about the same amount of free space.
|
|
4458 ** Usually NN siblings on either side of pPage is used in the balancing,
|
|
4459 ** though more siblings might come from one side if pPage is the first
|
|
4460 ** or last child of its parent. If pPage has fewer than 2*NN siblings
|
|
4461 ** (something which can only happen if pPage is the root page or a
|
|
4462 ** child of root) then all available siblings participate in the balancing.
|
|
4463 **
|
|
4464 ** The number of siblings of pPage might be increased or decreased by one or
|
|
4465 ** two in an effort to keep pages nearly full but not over full. The root page
|
|
4466 ** is special and is allowed to be nearly empty. If pPage is
|
|
4467 ** the root page, then the depth of the tree might be increased
|
|
4468 ** or decreased by one, as necessary, to keep the root page from being
|
|
4469 ** overfull or completely empty.
|
|
4470 **
|
|
4471 ** Note that when this routine is called, some of the Cells on pPage
|
|
4472 ** might not actually be stored in pPage->aData[]. This can happen
|
|
4473 ** if the page is overfull. Part of the job of this routine is to
|
|
4474 ** make sure all Cells for pPage once again fit in pPage->aData[].
|
|
4475 **
|
|
4476 ** In the course of balancing the siblings of pPage, the parent of pPage
|
|
4477 ** might become overfull or underfull. If that happens, then this routine
|
|
4478 ** is called recursively on the parent.
|
|
4479 **
|
|
4480 ** If this routine fails for any reason, it might leave the database
|
|
4481 ** in a corrupted state. So if this routine fails, the database should
|
|
4482 ** be rolled back.
|
|
4483 */
|
|
4484 static int balance_nonroot(MemPage *pPage){
|
|
4485 MemPage *pParent; /* The parent of pPage */
|
|
4486 BtShared *pBt; /* The whole database */
|
|
4487 int nCell = 0; /* Number of cells in apCell[] */
|
|
4488 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
|
|
4489 int nOld; /* Number of pages in apOld[] */
|
|
4490 int nNew; /* Number of pages in apNew[] */
|
|
4491 int nDiv; /* Number of cells in apDiv[] */
|
|
4492 int i, j, k; /* Loop counters */
|
|
4493 int idx; /* Index of pPage in pParent->aCell[] */
|
|
4494 int nxDiv; /* Next divider slot in pParent->aCell[] */
|
|
4495 int rc; /* The return code */
|
|
4496 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
|
|
4497 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
|
|
4498 int usableSpace; /* Bytes in pPage beyond the header */
|
|
4499 int pageFlags; /* Value of pPage->aData[0] */
|
|
4500 int subtotal; /* Subtotal of bytes in cells on one page */
|
|
4501 int iSpace = 0; /* First unused byte of aSpace[] */
|
|
4502 MemPage *apOld[NB]; /* pPage and up to two siblings */
|
|
4503 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
|
|
4504 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
|
|
4505 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
|
|
4506 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
|
|
4507 u8 *apDiv[NB]; /* Divider cells in pParent */
|
|
4508 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
|
|
4509 int szNew[NB+2]; /* Combined size of cells place on i-th page */
|
|
4510 u8 **apCell = 0; /* All cells begin balanced */
|
|
4511 int *szCell; /* Local size of all cells in apCell[] */
|
|
4512 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
|
|
4513 u8 *aSpace; /* Space to hold copies of dividers cells */
|
|
4514 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4515 u8 *aFrom = 0;
|
|
4516 #endif
|
|
4517
|
|
4518 /*
|
|
4519 ** Find the parent page.
|
|
4520 */
|
|
4521 assert( pPage->isInit );
|
|
4522 assert( sqlite3pager_iswriteable(pPage->aData) );
|
|
4523 pBt = pPage->pBt;
|
|
4524 pParent = pPage->pParent;
|
|
4525 assert( pParent );
|
|
4526 if( SQLITE_OK!=(rc = sqlite3pager_write(pParent->aData)) ){
|
|
4527 return rc;
|
|
4528 }
|
|
4529 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
|
|
4530
|
|
4531 #ifndef SQLITE_OMIT_QUICKBALANCE
|
|
4532 /*
|
|
4533 ** A special case: If a new entry has just been inserted into a
|
|
4534 ** table (that is, a btree with integer keys and all data at the leaves)
|
|
4535 ** and the new entry is the right-most entry in the tree (it has the
|
|
4536 ** largest key) then use the special balance_quick() routine for
|
|
4537 ** balancing. balance_quick() is much faster and results in a tighter
|
|
4538 ** packing of data in the common case.
|
|
4539 */
|
|
4540 if( pPage->leaf &&
|
|
4541 pPage->intKey &&
|
|
4542 pPage->leafData &&
|
|
4543 pPage->nOverflow==1 &&
|
|
4544 pPage->aOvfl[0].idx==pPage->nCell &&
|
|
4545 pPage->pParent->pgno!=1 &&
|
|
4546 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
|
|
4547 ){
|
|
4548 /*
|
|
4549 ** TODO: Check the siblings to the left of pPage. It may be that
|
|
4550 ** they are not full and no new page is required.
|
|
4551 */
|
|
4552 return balance_quick(pPage, pParent);
|
|
4553 }
|
|
4554 #endif
|
|
4555
|
|
4556 /*
|
|
4557 ** Find the cell in the parent page whose left child points back
|
|
4558 ** to pPage. The "idx" variable is the index of that cell. If pPage
|
|
4559 ** is the rightmost child of pParent then set idx to pParent->nCell
|
|
4560 */
|
|
4561 if( pParent->idxShift ){
|
|
4562 Pgno pgno;
|
|
4563 pgno = pPage->pgno;
|
|
4564 assert( pgno==sqlite3pager_pagenumber(pPage->aData) );
|
|
4565 for(idx=0; idx<pParent->nCell; idx++){
|
|
4566 if( get4byte(findCell(pParent, idx))==pgno ){
|
|
4567 break;
|
|
4568 }
|
|
4569 }
|
|
4570 assert( idx<pParent->nCell
|
|
4571 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
|
|
4572 }else{
|
|
4573 idx = pPage->idxParent;
|
|
4574 }
|
|
4575
|
|
4576 /*
|
|
4577 ** Initialize variables so that it will be safe to jump
|
|
4578 ** directly to balance_cleanup at any moment.
|
|
4579 */
|
|
4580 nOld = nNew = 0;
|
|
4581 sqlite3pager_ref(pParent->aData);
|
|
4582
|
|
4583 /*
|
|
4584 ** Find sibling pages to pPage and the cells in pParent that divide
|
|
4585 ** the siblings. An attempt is made to find NN siblings on either
|
|
4586 ** side of pPage. More siblings are taken from one side, however, if
|
|
4587 ** pPage there are fewer than NN siblings on the other side. If pParent
|
|
4588 ** has NB or fewer children then all children of pParent are taken.
|
|
4589 */
|
|
4590 nxDiv = idx - NN;
|
|
4591 if( nxDiv + NB > pParent->nCell ){
|
|
4592 nxDiv = pParent->nCell - NB + 1;
|
|
4593 }
|
|
4594 if( nxDiv<0 ){
|
|
4595 nxDiv = 0;
|
|
4596 }
|
|
4597 nDiv = 0;
|
|
4598 for(i=0, k=nxDiv; i<NB; i++, k++){
|
|
4599 if( k<pParent->nCell ){
|
|
4600 apDiv[i] = findCell(pParent, k);
|
|
4601 nDiv++;
|
|
4602 assert( !pParent->leaf );
|
|
4603 pgnoOld[i] = get4byte(apDiv[i]);
|
|
4604 }else if( k==pParent->nCell ){
|
|
4605 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
|
|
4606 }else{
|
|
4607 break;
|
|
4608 }
|
|
4609 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
|
|
4610 if( rc ) goto balance_cleanup;
|
|
4611 apOld[i]->idxParent = k;
|
|
4612 apCopy[i] = 0;
|
|
4613 assert( i==nOld );
|
|
4614 nOld++;
|
|
4615 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
|
|
4616 }
|
|
4617
|
|
4618 /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
|
|
4619 ** alignment */
|
|
4620 nMaxCells = (nMaxCells + 1)&~1;
|
|
4621
|
|
4622 /*
|
|
4623 ** Allocate space for memory structures
|
|
4624 */
|
|
4625 apCell = sqliteMallocRaw(
|
|
4626 nMaxCells*sizeof(u8*) /* apCell */
|
|
4627 + nMaxCells*sizeof(int) /* szCell */
|
|
4628 + ROUND8(sizeof(MemPage))*NB /* aCopy */
|
|
4629 + pBt->pageSize*(5+NB) /* aSpace */
|
|
4630 + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
|
|
4631 );
|
|
4632 if( apCell==0 ){
|
|
4633 rc = SQLITE_NOMEM;
|
|
4634 goto balance_cleanup;
|
|
4635 }
|
|
4636 szCell = (int*)&apCell[nMaxCells];
|
|
4637 aCopy[0] = (u8*)&szCell[nMaxCells];
|
|
4638 assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
|
|
4639 for(i=1; i<NB; i++){
|
|
4640 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
|
|
4641 assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
|
|
4642 }
|
|
4643 aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
|
|
4644 assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
|
|
4645 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4646 if( pBt->autoVacuum ){
|
|
4647 aFrom = &aSpace[5*pBt->pageSize];
|
|
4648 }
|
|
4649 #endif
|
|
4650
|
|
4651 /*
|
|
4652 ** Make copies of the content of pPage and its siblings into aOld[].
|
|
4653 ** The rest of this function will use data from the copies rather
|
|
4654 ** that the original pages since the original pages will be in the
|
|
4655 ** process of being overwritten.
|
|
4656 */
|
|
4657 for(i=0; i<nOld; i++){
|
|
4658 MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
|
|
4659 p->aData = &((u8*)p)[-pBt->pageSize];
|
|
4660 memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage));
|
|
4661 /* The memcpy() above changes the value of p->aData so we have to
|
|
4662 ** set it again. */
|
|
4663 p->aData = &((u8*)p)[-pBt->pageSize];
|
|
4664 }
|
|
4665
|
|
4666 /*
|
|
4667 ** Load pointers to all cells on sibling pages and the divider cells
|
|
4668 ** into the local apCell[] array. Make copies of the divider cells
|
|
4669 ** into space obtained form aSpace[] and remove the the divider Cells
|
|
4670 ** from pParent.
|
|
4671 **
|
|
4672 ** If the siblings are on leaf pages, then the child pointers of the
|
|
4673 ** divider cells are stripped from the cells before they are copied
|
|
4674 ** into aSpace[]. In this way, all cells in apCell[] are without
|
|
4675 ** child pointers. If siblings are not leaves, then all cell in
|
|
4676 ** apCell[] include child pointers. Either way, all cells in apCell[]
|
|
4677 ** are alike.
|
|
4678 **
|
|
4679 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
|
|
4680 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
|
|
4681 */
|
|
4682 nCell = 0;
|
|
4683 leafCorrection = pPage->leaf*4;
|
|
4684 leafData = pPage->leafData && pPage->leaf;
|
|
4685 for(i=0; i<nOld; i++){
|
|
4686 MemPage *pOld = apCopy[i];
|
|
4687 int limit = pOld->nCell+pOld->nOverflow;
|
|
4688 for(j=0; j<limit; j++){
|
|
4689 assert( nCell<nMaxCells );
|
|
4690 apCell[nCell] = findOverflowCell(pOld, j);
|
|
4691 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
|
|
4692 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4693 if( pBt->autoVacuum ){
|
|
4694 int a;
|
|
4695 aFrom[nCell] = i;
|
|
4696 for(a=0; a<pOld->nOverflow; a++){
|
|
4697 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
|
|
4698 aFrom[nCell] = 0xFF;
|
|
4699 break;
|
|
4700 }
|
|
4701 }
|
|
4702 }
|
|
4703 #endif
|
|
4704 nCell++;
|
|
4705 }
|
|
4706 if( i<nOld-1 ){
|
|
4707 int sz = cellSizePtr(pParent, apDiv[i]);
|
|
4708 if( leafData ){
|
|
4709 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
|
|
4710 ** are duplicates of keys on the child pages. We need to remove
|
|
4711 ** the divider cells from pParent, but the dividers cells are not
|
|
4712 ** added to apCell[] because they are duplicates of child cells.
|
|
4713 */
|
|
4714 dropCell(pParent, nxDiv, sz);
|
|
4715 }else{
|
|
4716 u8 *pTemp;
|
|
4717 assert( nCell<nMaxCells );
|
|
4718 szCell[nCell] = sz;
|
|
4719 pTemp = &aSpace[iSpace];
|
|
4720 iSpace += sz;
|
|
4721 assert( iSpace<=pBt->pageSize*5 );
|
|
4722 memcpy(pTemp, apDiv[i], sz);
|
|
4723 apCell[nCell] = pTemp+leafCorrection;
|
|
4724 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4725 if( pBt->autoVacuum ){
|
|
4726 aFrom[nCell] = 0xFF;
|
|
4727 }
|
|
4728 #endif
|
|
4729 dropCell(pParent, nxDiv, sz);
|
|
4730 szCell[nCell] -= leafCorrection;
|
|
4731 assert( get4byte(pTemp)==pgnoOld[i] );
|
|
4732 if( !pOld->leaf ){
|
|
4733 assert( leafCorrection==0 );
|
|
4734 /* The right pointer of the child page pOld becomes the left
|
|
4735 ** pointer of the divider cell */
|
|
4736 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
|
|
4737 }else{
|
|
4738 assert( leafCorrection==4 );
|
|
4739 }
|
|
4740 nCell++;
|
|
4741 }
|
|
4742 }
|
|
4743 }
|
|
4744
|
|
4745 /*
|
|
4746 ** Figure out the number of pages needed to hold all nCell cells.
|
|
4747 ** Store this number in "k". Also compute szNew[] which is the total
|
|
4748 ** size of all cells on the i-th page and cntNew[] which is the index
|
|
4749 ** in apCell[] of the cell that divides page i from page i+1.
|
|
4750 ** cntNew[k] should equal nCell.
|
|
4751 **
|
|
4752 ** Values computed by this block:
|
|
4753 **
|
|
4754 ** k: The total number of sibling pages
|
|
4755 ** szNew[i]: Spaced used on the i-th sibling page.
|
|
4756 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
|
|
4757 ** the right of the i-th sibling page.
|
|
4758 ** usableSpace: Number of bytes of space available on each sibling.
|
|
4759 **
|
|
4760 */
|
|
4761 usableSpace = pBt->usableSize - 12 + leafCorrection;
|
|
4762 for(subtotal=k=i=0; i<nCell; i++){
|
|
4763 assert( i<nMaxCells );
|
|
4764 subtotal += szCell[i] + 2;
|
|
4765 if( subtotal > usableSpace ){
|
|
4766 szNew[k] = subtotal - szCell[i];
|
|
4767 cntNew[k] = i;
|
|
4768 if( leafData ){ i--; }
|
|
4769 subtotal = 0;
|
|
4770 k++;
|
|
4771 }
|
|
4772 }
|
|
4773 szNew[k] = subtotal;
|
|
4774 cntNew[k] = nCell;
|
|
4775 k++;
|
|
4776
|
|
4777 /*
|
|
4778 ** The packing computed by the previous block is biased toward the siblings
|
|
4779 ** on the left side. The left siblings are always nearly full, while the
|
|
4780 ** right-most sibling might be nearly empty. This block of code attempts
|
|
4781 ** to adjust the packing of siblings to get a better balance.
|
|
4782 **
|
|
4783 ** This adjustment is more than an optimization. The packing above might
|
|
4784 ** be so out of balance as to be illegal. For example, the right-most
|
|
4785 ** sibling might be completely empty. This adjustment is not optional.
|
|
4786 */
|
|
4787 for(i=k-1; i>0; i--){
|
|
4788 int szRight = szNew[i]; /* Size of sibling on the right */
|
|
4789 int szLeft = szNew[i-1]; /* Size of sibling on the left */
|
|
4790 int r; /* Index of right-most cell in left sibling */
|
|
4791 int d; /* Index of first cell to the left of right sibling */
|
|
4792
|
|
4793 r = cntNew[i-1] - 1;
|
|
4794 d = r + 1 - leafData;
|
|
4795 assert( d<nMaxCells );
|
|
4796 assert( r<nMaxCells );
|
|
4797 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
|
|
4798 szRight += szCell[d] + 2;
|
|
4799 szLeft -= szCell[r] + 2;
|
|
4800 cntNew[i-1]--;
|
|
4801 r = cntNew[i-1] - 1;
|
|
4802 d = r + 1 - leafData;
|
|
4803 }
|
|
4804 szNew[i] = szRight;
|
|
4805 szNew[i-1] = szLeft;
|
|
4806 }
|
|
4807
|
|
4808 /* Either we found one or more cells (cntnew[0])>0) or we are the
|
|
4809 ** a virtual root page. A virtual root page is when the real root
|
|
4810 ** page is page 1 and we are the only child of that page.
|
|
4811 */
|
|
4812 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
|
|
4813
|
|
4814 /*
|
|
4815 ** Allocate k new pages. Reuse old pages where possible.
|
|
4816 */
|
|
4817 assert( pPage->pgno>1 );
|
|
4818 pageFlags = pPage->aData[0];
|
|
4819 for(i=0; i<k; i++){
|
|
4820 MemPage *pNew;
|
|
4821 if( i<nOld ){
|
|
4822 pNew = apNew[i] = apOld[i];
|
|
4823 pgnoNew[i] = pgnoOld[i];
|
|
4824 apOld[i] = 0;
|
|
4825 rc = sqlite3pager_write(pNew->aData);
|
|
4826 if( rc ) goto balance_cleanup;
|
|
4827 }else{
|
|
4828 assert( i>0 );
|
|
4829 rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
|
|
4830 if( rc ) goto balance_cleanup;
|
|
4831 apNew[i] = pNew;
|
|
4832 }
|
|
4833 nNew++;
|
|
4834 zeroPage(pNew, pageFlags);
|
|
4835 }
|
|
4836
|
|
4837 /* Free any old pages that were not reused as new pages.
|
|
4838 */
|
|
4839 while( i<nOld ){
|
|
4840 rc = freePage(apOld[i]);
|
|
4841 if( rc ) goto balance_cleanup;
|
|
4842 releasePage(apOld[i]);
|
|
4843 apOld[i] = 0;
|
|
4844 i++;
|
|
4845 }
|
|
4846
|
|
4847 /*
|
|
4848 ** Put the new pages in accending order. This helps to
|
|
4849 ** keep entries in the disk file in order so that a scan
|
|
4850 ** of the table is a linear scan through the file. That
|
|
4851 ** in turn helps the operating system to deliver pages
|
|
4852 ** from the disk more rapidly.
|
|
4853 **
|
|
4854 ** An O(n^2) insertion sort algorithm is used, but since
|
|
4855 ** n is never more than NB (a small constant), that should
|
|
4856 ** not be a problem.
|
|
4857 **
|
|
4858 ** When NB==3, this one optimization makes the database
|
|
4859 ** about 25% faster for large insertions and deletions.
|
|
4860 */
|
|
4861 for(i=0; i<k-1; i++){
|
|
4862 int minV = pgnoNew[i];
|
|
4863 int minI = i;
|
|
4864 for(j=i+1; j<k; j++){
|
|
4865 if( pgnoNew[j]<(unsigned)minV ){
|
|
4866 minI = j;
|
|
4867 minV = pgnoNew[j];
|
|
4868 }
|
|
4869 }
|
|
4870 if( minI>i ){
|
|
4871 int t;
|
|
4872 MemPage *pT;
|
|
4873 t = pgnoNew[i];
|
|
4874 pT = apNew[i];
|
|
4875 pgnoNew[i] = pgnoNew[minI];
|
|
4876 apNew[i] = apNew[minI];
|
|
4877 pgnoNew[minI] = t;
|
|
4878 apNew[minI] = pT;
|
|
4879 }
|
|
4880 }
|
|
4881 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
|
|
4882 pgnoOld[0],
|
|
4883 nOld>=2 ? pgnoOld[1] : 0,
|
|
4884 nOld>=3 ? pgnoOld[2] : 0,
|
|
4885 pgnoNew[0], szNew[0],
|
|
4886 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
|
|
4887 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
|
|
4888 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
|
|
4889 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
|
|
4890
|
|
4891 /*
|
|
4892 ** Evenly distribute the data in apCell[] across the new pages.
|
|
4893 ** Insert divider cells into pParent as necessary.
|
|
4894 */
|
|
4895 j = 0;
|
|
4896 for(i=0; i<nNew; i++){
|
|
4897 /* Assemble the new sibling page. */
|
|
4898 MemPage *pNew = apNew[i];
|
|
4899 assert( j<nMaxCells );
|
|
4900 assert( pNew->pgno==pgnoNew[i] );
|
|
4901 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
|
|
4902 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
|
|
4903 assert( pNew->nOverflow==0 );
|
|
4904
|
|
4905 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4906 /* If this is an auto-vacuum database, update the pointer map entries
|
|
4907 ** that point to the siblings that were rearranged. These can be: left
|
|
4908 ** children of cells, the right-child of the page, or overflow pages
|
|
4909 ** pointed to by cells.
|
|
4910 */
|
|
4911 if( pBt->autoVacuum ){
|
|
4912 for(k=j; k<cntNew[i]; k++){
|
|
4913 assert( k<nMaxCells );
|
|
4914 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
|
|
4915 rc = ptrmapPutOvfl(pNew, k-j);
|
|
4916 if( rc!=SQLITE_OK ){
|
|
4917 goto balance_cleanup;
|
|
4918 }
|
|
4919 }
|
|
4920 }
|
|
4921 }
|
|
4922 #endif
|
|
4923
|
|
4924 j = cntNew[i];
|
|
4925
|
|
4926 /* If the sibling page assembled above was not the right-most sibling,
|
|
4927 ** insert a divider cell into the parent page.
|
|
4928 */
|
|
4929 if( i<nNew-1 && j<nCell ){
|
|
4930 u8 *pCell;
|
|
4931 u8 *pTemp;
|
|
4932 int sz;
|
|
4933
|
|
4934 assert( j<nMaxCells );
|
|
4935 pCell = apCell[j];
|
|
4936 sz = szCell[j] + leafCorrection;
|
|
4937 if( !pNew->leaf ){
|
|
4938 memcpy(&pNew->aData[8], pCell, 4);
|
|
4939 pTemp = 0;
|
|
4940 }else if( leafData ){
|
|
4941 /* If the tree is a leaf-data tree, and the siblings are leaves,
|
|
4942 ** then there is no divider cell in apCell[]. Instead, the divider
|
|
4943 ** cell consists of the integer key for the right-most cell of
|
|
4944 ** the sibling-page assembled above only.
|
|
4945 */
|
|
4946 CellInfo info;
|
|
4947 j--;
|
|
4948 parseCellPtr(pNew, apCell[j], &info);
|
|
4949 pCell = &aSpace[iSpace];
|
|
4950 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
|
|
4951 iSpace += sz;
|
|
4952 assert( iSpace<=pBt->pageSize*5 );
|
|
4953 pTemp = 0;
|
|
4954 }else{
|
|
4955 pCell -= 4;
|
|
4956 pTemp = &aSpace[iSpace];
|
|
4957 iSpace += sz;
|
|
4958 assert( iSpace<=pBt->pageSize*5 );
|
|
4959 }
|
|
4960 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
|
|
4961 if( rc!=SQLITE_OK ) goto balance_cleanup;
|
|
4962 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
|
|
4963 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
4964 /* If this is an auto-vacuum database, and not a leaf-data tree,
|
|
4965 ** then update the pointer map with an entry for the overflow page
|
|
4966 ** that the cell just inserted points to (if any).
|
|
4967 */
|
|
4968 if( pBt->autoVacuum && !leafData ){
|
|
4969 rc = ptrmapPutOvfl(pParent, nxDiv);
|
|
4970 if( rc!=SQLITE_OK ){
|
|
4971 goto balance_cleanup;
|
|
4972 }
|
|
4973 }
|
|
4974 #endif
|
|
4975 j++;
|
|
4976 nxDiv++;
|
|
4977 }
|
|
4978 }
|
|
4979 assert( j==nCell );
|
|
4980 assert( nOld>0 );
|
|
4981 assert( nNew>0 );
|
|
4982 if( (pageFlags & PTF_LEAF)==0 ){
|
|
4983 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
|
|
4984 }
|
|
4985 if( nxDiv==pParent->nCell+pParent->nOverflow ){
|
|
4986 /* Right-most sibling is the right-most child of pParent */
|
|
4987 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
|
|
4988 }else{
|
|
4989 /* Right-most sibling is the left child of the first entry in pParent
|
|
4990 ** past the right-most divider entry */
|
|
4991 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
|
|
4992 }
|
|
4993
|
|
4994 /*
|
|
4995 ** Reparent children of all cells.
|
|
4996 */
|
|
4997 for(i=0; i<nNew; i++){
|
|
4998 rc = reparentChildPages(apNew[i]);
|
|
4999 if( rc!=SQLITE_OK ) goto balance_cleanup;
|
|
5000 }
|
|
5001 rc = reparentChildPages(pParent);
|
|
5002 if( rc!=SQLITE_OK ) goto balance_cleanup;
|
|
5003
|
|
5004 /*
|
|
5005 ** Balance the parent page. Note that the current page (pPage) might
|
|
5006 ** have been added to the freelist so it might no longer be initialized.
|
|
5007 ** But the parent page will always be initialized.
|
|
5008 */
|
|
5009 assert( pParent->isInit );
|
|
5010 /* assert( pPage->isInit ); // No! pPage might have been added to freelist */
|
|
5011 /* pageIntegrity(pPage); // No! pPage might have been added to freelist */
|
|
5012 rc = balance(pParent, 0);
|
|
5013
|
|
5014 /*
|
|
5015 ** Cleanup before returning.
|
|
5016 */
|
|
5017 balance_cleanup:
|
|
5018 sqliteFree(apCell);
|
|
5019 for(i=0; i<nOld; i++){
|
|
5020 releasePage(apOld[i]);
|
|
5021 }
|
|
5022 for(i=0; i<nNew; i++){
|
|
5023 releasePage(apNew[i]);
|
|
5024 }
|
|
5025 releasePage(pParent);
|
|
5026 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
|
|
5027 pPage->pgno, nOld, nNew, nCell));
|
|
5028 return rc;
|
|
5029 }
|
|
5030
|
|
5031 /*
|
|
5032 ** This routine is called for the root page of a btree when the root
|
|
5033 ** page contains no cells. This is an opportunity to make the tree
|
|
5034 ** shallower by one level.
|
|
5035 */
|
|
5036 static int balance_shallower(MemPage *pPage){
|
|
5037 MemPage *pChild; /* The only child page of pPage */
|
|
5038 Pgno pgnoChild; /* Page number for pChild */
|
|
5039 int rc = SQLITE_OK; /* Return code from subprocedures */
|
|
5040 BtShared *pBt; /* The main BTree structure */
|
|
5041 int mxCellPerPage; /* Maximum number of cells per page */
|
|
5042 u8 **apCell; /* All cells from pages being balanced */
|
|
5043 int *szCell; /* Local size of all cells */
|
|
5044
|
|
5045 assert( pPage->pParent==0 );
|
|
5046 assert( pPage->nCell==0 );
|
|
5047 pBt = pPage->pBt;
|
|
5048 mxCellPerPage = MX_CELL(pBt);
|
|
5049 apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
|
|
5050 if( apCell==0 ) return SQLITE_NOMEM;
|
|
5051 szCell = (int*)&apCell[mxCellPerPage];
|
|
5052 if( pPage->leaf ){
|
|
5053 /* The table is completely empty */
|
|
5054 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
|
|
5055 }else{
|
|
5056 /* The root page is empty but has one child. Transfer the
|
|
5057 ** information from that one child into the root page if it
|
|
5058 ** will fit. This reduces the depth of the tree by one.
|
|
5059 **
|
|
5060 ** If the root page is page 1, it has less space available than
|
|
5061 ** its child (due to the 100 byte header that occurs at the beginning
|
|
5062 ** of the database fle), so it might not be able to hold all of the
|
|
5063 ** information currently contained in the child. If this is the
|
|
5064 ** case, then do not do the transfer. Leave page 1 empty except
|
|
5065 ** for the right-pointer to the child page. The child page becomes
|
|
5066 ** the virtual root of the tree.
|
|
5067 */
|
|
5068 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
|
|
5069 assert( pgnoChild>0 );
|
|
5070 assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) );
|
|
5071 rc = getPage(pPage->pBt, pgnoChild, &pChild);
|
|
5072 if( rc ) goto end_shallow_balance;
|
|
5073 if( pPage->pgno==1 ){
|
|
5074 rc = initPage(pChild, pPage);
|
|
5075 if( rc ) goto end_shallow_balance;
|
|
5076 assert( pChild->nOverflow==0 );
|
|
5077 if( pChild->nFree>=100 ){
|
|
5078 /* The child information will fit on the root page, so do the
|
|
5079 ** copy */
|
|
5080 int i;
|
|
5081 zeroPage(pPage, pChild->aData[0]);
|
|
5082 for(i=0; i<pChild->nCell; i++){
|
|
5083 apCell[i] = findCell(pChild,i);
|
|
5084 szCell[i] = cellSizePtr(pChild, apCell[i]);
|
|
5085 }
|
|
5086 assemblePage(pPage, pChild->nCell, apCell, szCell);
|
|
5087 /* Copy the right-pointer of the child to the parent. */
|
|
5088 put4byte(&pPage->aData[pPage->hdrOffset+8],
|
|
5089 get4byte(&pChild->aData[pChild->hdrOffset+8]));
|
|
5090 freePage(pChild);
|
|
5091 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
|
|
5092 }else{
|
|
5093 /* The child has more information that will fit on the root.
|
|
5094 ** The tree is already balanced. Do nothing. */
|
|
5095 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
|
|
5096 }
|
|
5097 }else{
|
|
5098 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
|
|
5099 pPage->isInit = 0;
|
|
5100 pPage->pParent = 0;
|
|
5101 rc = initPage(pPage, 0);
|
|
5102 assert( rc==SQLITE_OK );
|
|
5103 freePage(pChild);
|
|
5104 TRACE(("BALANCE: transfer child %d into root %d\n",
|
|
5105 pChild->pgno, pPage->pgno));
|
|
5106 }
|
|
5107 rc = reparentChildPages(pPage);
|
|
5108 assert( pPage->nOverflow==0 );
|
|
5109 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
5110 if( pBt->autoVacuum ){
|
|
5111 int i;
|
|
5112 for(i=0; i<pPage->nCell; i++){
|
|
5113 rc = ptrmapPutOvfl(pPage, i);
|
|
5114 if( rc!=SQLITE_OK ){
|
|
5115 goto end_shallow_balance;
|
|
5116 }
|
|
5117 }
|
|
5118 }
|
|
5119 #endif
|
|
5120 if( rc!=SQLITE_OK ) goto end_shallow_balance;
|
|
5121 releasePage(pChild);
|
|
5122 }
|
|
5123 end_shallow_balance:
|
|
5124 sqliteFree(apCell);
|
|
5125 return rc;
|
|
5126 }
|
|
5127
|
|
5128
|
|
5129 /*
|
|
5130 ** The root page is overfull
|
|
5131 **
|
|
5132 ** When this happens, Create a new child page and copy the
|
|
5133 ** contents of the root into the child. Then make the root
|
|
5134 ** page an empty page with rightChild pointing to the new
|
|
5135 ** child. Finally, call balance_internal() on the new child
|
|
5136 ** to cause it to split.
|
|
5137 */
|
|
5138 static int balance_deeper(MemPage *pPage){
|
|
5139 int rc; /* Return value from subprocedures */
|
|
5140 MemPage *pChild; /* Pointer to a new child page */
|
|
5141 Pgno pgnoChild; /* Page number of the new child page */
|
|
5142 BtShared *pBt; /* The BTree */
|
|
5143 int usableSize; /* Total usable size of a page */
|
|
5144 u8 *data; /* Content of the parent page */
|
|
5145 u8 *cdata; /* Content of the child page */
|
|
5146 int hdr; /* Offset to page header in parent */
|
|
5147 int brk; /* Offset to content of first cell in parent */
|
|
5148
|
|
5149 assert( pPage->pParent==0 );
|
|
5150 assert( pPage->nOverflow>0 );
|
|
5151 pBt = pPage->pBt;
|
|
5152 rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
|
|
5153 if( rc ) return rc;
|
|
5154 assert( sqlite3pager_iswriteable(pChild->aData) );
|
|
5155 usableSize = pBt->usableSize;
|
|
5156 data = pPage->aData;
|
|
5157 hdr = pPage->hdrOffset;
|
|
5158 brk = get2byte(&data[hdr+5]);
|
|
5159 cdata = pChild->aData;
|
|
5160 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
|
|
5161 memcpy(&cdata[brk], &data[brk], usableSize-brk);
|
|
5162 assert( pChild->isInit==0 );
|
|
5163 rc = initPage(pChild, pPage);
|
|
5164 if( rc ) goto balancedeeper_out;
|
|
5165 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
|
|
5166 pChild->nOverflow = pPage->nOverflow;
|
|
5167 if( pChild->nOverflow ){
|
|
5168 pChild->nFree = 0;
|
|
5169 }
|
|
5170 assert( pChild->nCell==pPage->nCell );
|
|
5171 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
|
|
5172 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
|
|
5173 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
|
|
5174 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
5175 if( pBt->autoVacuum ){
|
|
5176 int i;
|
|
5177 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
|
|
5178 if( rc ) goto balancedeeper_out;
|
|
5179 for(i=0; i<pChild->nCell; i++){
|
|
5180 rc = ptrmapPutOvfl(pChild, i);
|
|
5181 if( rc!=SQLITE_OK ){
|
|
5182 return rc;
|
|
5183 }
|
|
5184 }
|
|
5185 }
|
|
5186 #endif
|
|
5187 rc = balance_nonroot(pChild);
|
|
5188
|
|
5189 balancedeeper_out:
|
|
5190 releasePage(pChild);
|
|
5191 return rc;
|
|
5192 }
|
|
5193
|
|
5194 /*
|
|
5195 ** Decide if the page pPage needs to be balanced. If balancing is
|
|
5196 ** required, call the appropriate balancing routine.
|
|
5197 */
|
|
5198 static int balance(MemPage *pPage, int insert){
|
|
5199 int rc = SQLITE_OK;
|
|
5200 if( pPage->pParent==0 ){
|
|
5201 if( pPage->nOverflow>0 ){
|
|
5202 rc = balance_deeper(pPage);
|
|
5203 }
|
|
5204 if( rc==SQLITE_OK && pPage->nCell==0 ){
|
|
5205 rc = balance_shallower(pPage);
|
|
5206 }
|
|
5207 }else{
|
|
5208 if( pPage->nOverflow>0 ||
|
|
5209 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
|
|
5210 rc = balance_nonroot(pPage);
|
|
5211 }
|
|
5212 }
|
|
5213 return rc;
|
|
5214 }
|
|
5215
|
|
5216 /*
|
|
5217 ** This routine checks all cursors that point to table pgnoRoot.
|
|
5218 ** If any of those cursors other than pExclude were opened with
|
|
5219 ** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
|
|
5220 ** cursors that point to pgnoRoot were opened with wrFlag==1
|
|
5221 ** then this routine returns SQLITE_OK.
|
|
5222 **
|
|
5223 ** In addition to checking for read-locks (where a read-lock
|
|
5224 ** means a cursor opened with wrFlag==0) this routine also moves
|
|
5225 ** all cursors other than pExclude so that they are pointing to the
|
|
5226 ** first Cell on root page. This is necessary because an insert
|
|
5227 ** or delete might change the number of cells on a page or delete
|
|
5228 ** a page entirely and we do not want to leave any cursors
|
|
5229 ** pointing to non-existant pages or cells.
|
|
5230 */
|
|
5231 static int checkReadLocks(BtShared *pBt, Pgno pgnoRoot, BtCursor *pExclude){
|
|
5232 BtCursor *p;
|
|
5233 for(p=pBt->pCursor; p; p=p->pNext){
|
|
5234 u32 flags = (p->pBtree->pSqlite ? p->pBtree->pSqlite->flags : 0);
|
|
5235 if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue;
|
|
5236 if( p->wrFlag==0 && flags&SQLITE_ReadUncommitted ) continue;
|
|
5237 if( p->wrFlag==0 ) return SQLITE_LOCKED;
|
|
5238 if( p->pPage->pgno!=p->pgnoRoot ){
|
|
5239 moveToRoot(p);
|
|
5240 }
|
|
5241 }
|
|
5242 return SQLITE_OK;
|
|
5243 }
|
|
5244
|
|
5245 /*
|
|
5246 ** Insert a new record into the BTree. The key is given by (pKey,nKey)
|
|
5247 ** and the data is given by (pData,nData). The cursor is used only to
|
|
5248 ** define what table the record should be inserted into. The cursor
|
|
5249 ** is left pointing at a random location.
|
|
5250 **
|
|
5251 ** For an INTKEY table, only the nKey value of the key is used. pKey is
|
|
5252 ** ignored. For a ZERODATA table, the pData and nData are both ignored.
|
|
5253 */
|
|
5254 int sqlite3BtreeInsert(
|
|
5255 BtCursor *pCur, /* Insert data into the table of this cursor */
|
|
5256 const void *pKey, i64 nKey, /* The key of the new record */
|
|
5257 const void *pData, int nData /* The data of the new record */
|
|
5258 ){
|
|
5259 int rc;
|
|
5260 int loc;
|
|
5261 int szNew;
|
|
5262 MemPage *pPage;
|
|
5263 BtShared *pBt = pCur->pBtree->pBt;
|
|
5264 unsigned char *oldCell;
|
|
5265 unsigned char *newCell = 0;
|
|
5266
|
|
5267 if( pBt->inTransaction!=TRANS_WRITE ){
|
|
5268 /* Must start a transaction before doing an insert */
|
|
5269 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5270 }
|
|
5271 assert( !pBt->readOnly );
|
|
5272 if( !pCur->wrFlag ){
|
|
5273 return SQLITE_PERM; /* Cursor not open for writing */
|
|
5274 }
|
|
5275 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
|
|
5276 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
|
|
5277 }
|
|
5278
|
|
5279 /* Save the positions of any other cursors open on this table */
|
|
5280 restoreOrClearCursorPosition(pCur, 0);
|
|
5281 if(
|
|
5282 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
|
|
5283 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc))
|
|
5284 ){
|
|
5285 return rc;
|
|
5286 }
|
|
5287
|
|
5288 pPage = pCur->pPage;
|
|
5289 assert( pPage->intKey || nKey>=0 );
|
|
5290 assert( pPage->leaf || !pPage->leafData );
|
|
5291 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
|
|
5292 pCur->pgnoRoot, nKey, nData, pPage->pgno,
|
|
5293 loc==0 ? "overwrite" : "new entry"));
|
|
5294 assert( pPage->isInit );
|
|
5295 rc = sqlite3pager_write(pPage->aData);
|
|
5296 if( rc ) return rc;
|
|
5297 newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
|
|
5298 if( newCell==0 ) return SQLITE_NOMEM;
|
|
5299 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew);
|
|
5300 if( rc ) goto end_insert;
|
|
5301 assert( szNew==cellSizePtr(pPage, newCell) );
|
|
5302 assert( szNew<=MX_CELL_SIZE(pBt) );
|
|
5303 if( loc==0 && CURSOR_VALID==pCur->eState ){
|
|
5304 int szOld;
|
|
5305 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
|
|
5306 oldCell = findCell(pPage, pCur->idx);
|
|
5307 if( !pPage->leaf ){
|
|
5308 memcpy(newCell, oldCell, 4);
|
|
5309 }
|
|
5310 szOld = cellSizePtr(pPage, oldCell);
|
|
5311 rc = clearCell(pPage, oldCell);
|
|
5312 if( rc ) goto end_insert;
|
|
5313 dropCell(pPage, pCur->idx, szOld);
|
|
5314 }else if( loc<0 && pPage->nCell>0 ){
|
|
5315 assert( pPage->leaf );
|
|
5316 pCur->idx++;
|
|
5317 pCur->info.nSize = 0;
|
|
5318 }else{
|
|
5319 assert( pPage->leaf );
|
|
5320 }
|
|
5321 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
|
|
5322 if( rc!=SQLITE_OK ) goto end_insert;
|
|
5323 rc = balance(pPage, 1);
|
|
5324 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
|
|
5325 /* fflush(stdout); */
|
|
5326 if( rc==SQLITE_OK ){
|
|
5327 moveToRoot(pCur);
|
|
5328 }
|
|
5329 end_insert:
|
|
5330 sqliteFree(newCell);
|
|
5331 return rc;
|
|
5332 }
|
|
5333
|
|
5334 /*
|
|
5335 ** Delete the entry that the cursor is pointing to. The cursor
|
|
5336 ** is left pointing at a random location.
|
|
5337 */
|
|
5338 int sqlite3BtreeDelete(BtCursor *pCur){
|
|
5339 MemPage *pPage = pCur->pPage;
|
|
5340 unsigned char *pCell;
|
|
5341 int rc;
|
|
5342 Pgno pgnoChild = 0;
|
|
5343 BtShared *pBt = pCur->pBtree->pBt;
|
|
5344
|
|
5345 assert( pPage->isInit );
|
|
5346 if( pBt->inTransaction!=TRANS_WRITE ){
|
|
5347 /* Must start a transaction before doing a delete */
|
|
5348 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5349 }
|
|
5350 assert( !pBt->readOnly );
|
|
5351 if( pCur->idx >= pPage->nCell ){
|
|
5352 return SQLITE_ERROR; /* The cursor is not pointing to anything */
|
|
5353 }
|
|
5354 if( !pCur->wrFlag ){
|
|
5355 return SQLITE_PERM; /* Did not open this cursor for writing */
|
|
5356 }
|
|
5357 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
|
|
5358 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
|
|
5359 }
|
|
5360
|
|
5361 /* Restore the current cursor position (a no-op if the cursor is not in
|
|
5362 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
|
|
5363 ** open on the same table. Then call sqlite3pager_write() on the page
|
|
5364 ** that the entry will be deleted from.
|
|
5365 */
|
|
5366 if(
|
|
5367 (rc = restoreOrClearCursorPosition(pCur, 1))!=0 ||
|
|
5368 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
|
|
5369 (rc = sqlite3pager_write(pPage->aData))!=0
|
|
5370 ){
|
|
5371 return rc;
|
|
5372 }
|
|
5373
|
|
5374 /* Locate the cell within it's page and leave pCell pointing to the
|
|
5375 ** data. The clearCell() call frees any overflow pages associated with the
|
|
5376 ** cell. The cell itself is still intact.
|
|
5377 */
|
|
5378 pCell = findCell(pPage, pCur->idx);
|
|
5379 if( !pPage->leaf ){
|
|
5380 pgnoChild = get4byte(pCell);
|
|
5381 }
|
|
5382 rc = clearCell(pPage, pCell);
|
|
5383 if( rc ) return rc;
|
|
5384
|
|
5385 if( !pPage->leaf ){
|
|
5386 /*
|
|
5387 ** The entry we are about to delete is not a leaf so if we do not
|
|
5388 ** do something we will leave a hole on an internal page.
|
|
5389 ** We have to fill the hole by moving in a cell from a leaf. The
|
|
5390 ** next Cell after the one to be deleted is guaranteed to exist and
|
|
5391 ** to be a leaf so we can use it.
|
|
5392 */
|
|
5393 BtCursor leafCur;
|
|
5394 unsigned char *pNext;
|
|
5395 int szNext; /* The compiler warning is wrong: szNext is always
|
|
5396 ** initialized before use. Adding an extra initialization
|
|
5397 ** to silence the compiler slows down the code. */
|
|
5398 int notUsed;
|
|
5399 unsigned char *tempCell = 0;
|
|
5400 assert( !pPage->leafData );
|
|
5401 getTempCursor(pCur, &leafCur);
|
|
5402 rc = sqlite3BtreeNext(&leafCur, ¬Used);
|
|
5403 if( rc!=SQLITE_OK ){
|
|
5404 if( rc!=SQLITE_NOMEM ){
|
|
5405 rc = SQLITE_CORRUPT_BKPT;
|
|
5406 }
|
|
5407 }
|
|
5408 if( rc==SQLITE_OK ){
|
|
5409 rc = sqlite3pager_write(leafCur.pPage->aData);
|
|
5410 }
|
|
5411 if( rc==SQLITE_OK ){
|
|
5412 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
|
|
5413 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
|
|
5414 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
|
|
5415 pNext = findCell(leafCur.pPage, leafCur.idx);
|
|
5416 szNext = cellSizePtr(leafCur.pPage, pNext);
|
|
5417 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
|
|
5418 tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
|
|
5419 if( tempCell==0 ){
|
|
5420 rc = SQLITE_NOMEM;
|
|
5421 }
|
|
5422 }
|
|
5423 if( rc==SQLITE_OK ){
|
|
5424 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
|
|
5425 }
|
|
5426 if( rc==SQLITE_OK ){
|
|
5427 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
|
|
5428 rc = balance(pPage, 0);
|
|
5429 }
|
|
5430 if( rc==SQLITE_OK ){
|
|
5431 dropCell(leafCur.pPage, leafCur.idx, szNext);
|
|
5432 rc = balance(leafCur.pPage, 0);
|
|
5433 }
|
|
5434 sqliteFree(tempCell);
|
|
5435 releaseTempCursor(&leafCur);
|
|
5436 }else{
|
|
5437 TRACE(("DELETE: table=%d delete from leaf %d\n",
|
|
5438 pCur->pgnoRoot, pPage->pgno));
|
|
5439 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
|
|
5440 rc = balance(pPage, 0);
|
|
5441 }
|
|
5442 if( rc==SQLITE_OK ){
|
|
5443 moveToRoot(pCur);
|
|
5444 }
|
|
5445 return rc;
|
|
5446 }
|
|
5447
|
|
5448 /*
|
|
5449 ** Create a new BTree table. Write into *piTable the page
|
|
5450 ** number for the root page of the new table.
|
|
5451 **
|
|
5452 ** The type of type is determined by the flags parameter. Only the
|
|
5453 ** following values of flags are currently in use. Other values for
|
|
5454 ** flags might not work:
|
|
5455 **
|
|
5456 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
|
|
5457 ** BTREE_ZERODATA Used for SQL indices
|
|
5458 */
|
|
5459 int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
|
|
5460 BtShared *pBt = p->pBt;
|
|
5461 MemPage *pRoot;
|
|
5462 Pgno pgnoRoot;
|
|
5463 int rc;
|
|
5464 if( pBt->inTransaction!=TRANS_WRITE ){
|
|
5465 /* Must start a transaction first */
|
|
5466 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5467 }
|
|
5468 assert( !pBt->readOnly );
|
|
5469
|
|
5470 /* It is illegal to create a table if any cursors are open on the
|
|
5471 ** database. This is because in auto-vacuum mode the backend may
|
|
5472 ** need to move a database page to make room for the new root-page.
|
|
5473 ** If an open cursor was using the page a problem would occur.
|
|
5474 */
|
|
5475 if( pBt->pCursor ){
|
|
5476 return SQLITE_LOCKED;
|
|
5477 }
|
|
5478
|
|
5479 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
5480 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
|
|
5481 if( rc ) return rc;
|
|
5482 #else
|
|
5483 if( pBt->autoVacuum ){
|
|
5484 Pgno pgnoMove; /* Move a page here to make room for the root-page */
|
|
5485 MemPage *pPageMove; /* The page to move to. */
|
|
5486
|
|
5487 /* Read the value of meta[3] from the database to determine where the
|
|
5488 ** root page of the new table should go. meta[3] is the largest root-page
|
|
5489 ** created so far, so the new root-page is (meta[3]+1).
|
|
5490 */
|
|
5491 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
|
|
5492 if( rc!=SQLITE_OK ) return rc;
|
|
5493 pgnoRoot++;
|
|
5494
|
|
5495 /* The new root-page may not be allocated on a pointer-map page, or the
|
|
5496 ** PENDING_BYTE page.
|
|
5497 */
|
|
5498 if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
|
|
5499 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
|
|
5500 pgnoRoot++;
|
|
5501 }
|
|
5502 assert( pgnoRoot>=3 );
|
|
5503
|
|
5504 /* Allocate a page. The page that currently resides at pgnoRoot will
|
|
5505 ** be moved to the allocated page (unless the allocated page happens
|
|
5506 ** to reside at pgnoRoot).
|
|
5507 */
|
|
5508 rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
|
|
5509 if( rc!=SQLITE_OK ){
|
|
5510 return rc;
|
|
5511 }
|
|
5512
|
|
5513 if( pgnoMove!=pgnoRoot ){
|
|
5514 u8 eType;
|
|
5515 Pgno iPtrPage;
|
|
5516
|
|
5517 releasePage(pPageMove);
|
|
5518 rc = getPage(pBt, pgnoRoot, &pRoot);
|
|
5519 if( rc!=SQLITE_OK ){
|
|
5520 return rc;
|
|
5521 }
|
|
5522 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
|
|
5523 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
|
|
5524 releasePage(pRoot);
|
|
5525 return rc;
|
|
5526 }
|
|
5527 assert( eType!=PTRMAP_ROOTPAGE );
|
|
5528 assert( eType!=PTRMAP_FREEPAGE );
|
|
5529 rc = sqlite3pager_write(pRoot->aData);
|
|
5530 if( rc!=SQLITE_OK ){
|
|
5531 releasePage(pRoot);
|
|
5532 return rc;
|
|
5533 }
|
|
5534 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
|
|
5535 releasePage(pRoot);
|
|
5536 if( rc!=SQLITE_OK ){
|
|
5537 return rc;
|
|
5538 }
|
|
5539 rc = getPage(pBt, pgnoRoot, &pRoot);
|
|
5540 if( rc!=SQLITE_OK ){
|
|
5541 return rc;
|
|
5542 }
|
|
5543 rc = sqlite3pager_write(pRoot->aData);
|
|
5544 if( rc!=SQLITE_OK ){
|
|
5545 releasePage(pRoot);
|
|
5546 return rc;
|
|
5547 }
|
|
5548 }else{
|
|
5549 pRoot = pPageMove;
|
|
5550 }
|
|
5551
|
|
5552 /* Update the pointer-map and meta-data with the new root-page number. */
|
|
5553 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
|
|
5554 if( rc ){
|
|
5555 releasePage(pRoot);
|
|
5556 return rc;
|
|
5557 }
|
|
5558 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
|
|
5559 if( rc ){
|
|
5560 releasePage(pRoot);
|
|
5561 return rc;
|
|
5562 }
|
|
5563
|
|
5564 }else{
|
|
5565 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
|
|
5566 if( rc ) return rc;
|
|
5567 }
|
|
5568 #endif
|
|
5569 assert( sqlite3pager_iswriteable(pRoot->aData) );
|
|
5570 zeroPage(pRoot, flags | PTF_LEAF);
|
|
5571 sqlite3pager_unref(pRoot->aData);
|
|
5572 *piTable = (int)pgnoRoot;
|
|
5573 return SQLITE_OK;
|
|
5574 }
|
|
5575
|
|
5576 /*
|
|
5577 ** Erase the given database page and all its children. Return
|
|
5578 ** the page to the freelist.
|
|
5579 */
|
|
5580 static int clearDatabasePage(
|
|
5581 BtShared *pBt, /* The BTree that contains the table */
|
|
5582 Pgno pgno, /* Page number to clear */
|
|
5583 MemPage *pParent, /* Parent page. NULL for the root */
|
|
5584 int freePageFlag /* Deallocate page if true */
|
|
5585 ){
|
|
5586 MemPage *pPage = 0;
|
|
5587 int rc;
|
|
5588 unsigned char *pCell;
|
|
5589 int i;
|
|
5590
|
|
5591 if( pgno>sqlite3pager_pagecount(pBt->pPager) ){
|
|
5592 return SQLITE_CORRUPT_BKPT;
|
|
5593 }
|
|
5594
|
|
5595 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
|
|
5596 if( rc ) goto cleardatabasepage_out;
|
|
5597 rc = sqlite3pager_write(pPage->aData);
|
|
5598 if( rc ) goto cleardatabasepage_out;
|
|
5599 for(i=0; i<pPage->nCell; i++){
|
|
5600 pCell = findCell(pPage, i);
|
|
5601 if( !pPage->leaf ){
|
|
5602 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
|
|
5603 if( rc ) goto cleardatabasepage_out;
|
|
5604 }
|
|
5605 rc = clearCell(pPage, pCell);
|
|
5606 if( rc ) goto cleardatabasepage_out;
|
|
5607 }
|
|
5608 if( !pPage->leaf ){
|
|
5609 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
|
|
5610 if( rc ) goto cleardatabasepage_out;
|
|
5611 }
|
|
5612 if( freePageFlag ){
|
|
5613 rc = freePage(pPage);
|
|
5614 }else{
|
|
5615 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
|
|
5616 }
|
|
5617
|
|
5618 cleardatabasepage_out:
|
|
5619 releasePage(pPage);
|
|
5620 return rc;
|
|
5621 }
|
|
5622
|
|
5623 /*
|
|
5624 ** Delete all information from a single table in the database. iTable is
|
|
5625 ** the page number of the root of the table. After this routine returns,
|
|
5626 ** the root page is empty, but still exists.
|
|
5627 **
|
|
5628 ** This routine will fail with SQLITE_LOCKED if there are any open
|
|
5629 ** read cursors on the table. Open write cursors are moved to the
|
|
5630 ** root of the table.
|
|
5631 */
|
|
5632 int sqlite3BtreeClearTable(Btree *p, int iTable){
|
|
5633 int rc;
|
|
5634 BtCursor *pCur;
|
|
5635 BtShared *pBt = p->pBt;
|
|
5636 sqlite3 *db = p->pSqlite;
|
|
5637 if( p->inTrans!=TRANS_WRITE ){
|
|
5638 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5639 }
|
|
5640
|
|
5641 /* If this connection is not in read-uncommitted mode and currently has
|
|
5642 ** a read-cursor open on the table being cleared, return SQLITE_LOCKED.
|
|
5643 */
|
|
5644 if( 0==db || 0==(db->flags&SQLITE_ReadUncommitted) ){
|
|
5645 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
|
|
5646 if( pCur->pBtree==p && pCur->pgnoRoot==(Pgno)iTable ){
|
|
5647 if( 0==pCur->wrFlag ){
|
|
5648 return SQLITE_LOCKED;
|
|
5649 }
|
|
5650 moveToRoot(pCur);
|
|
5651 }
|
|
5652 }
|
|
5653 }
|
|
5654
|
|
5655 /* Save the position of all cursors open on this table */
|
|
5656 if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
|
|
5657 return rc;
|
|
5658 }
|
|
5659
|
|
5660 return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
|
|
5661 }
|
|
5662
|
|
5663 /*
|
|
5664 ** Erase all information in a table and add the root of the table to
|
|
5665 ** the freelist. Except, the root of the principle table (the one on
|
|
5666 ** page 1) is never added to the freelist.
|
|
5667 **
|
|
5668 ** This routine will fail with SQLITE_LOCKED if there are any open
|
|
5669 ** cursors on the table.
|
|
5670 **
|
|
5671 ** If AUTOVACUUM is enabled and the page at iTable is not the last
|
|
5672 ** root page in the database file, then the last root page
|
|
5673 ** in the database file is moved into the slot formerly occupied by
|
|
5674 ** iTable and that last slot formerly occupied by the last root page
|
|
5675 ** is added to the freelist instead of iTable. In this say, all
|
|
5676 ** root pages are kept at the beginning of the database file, which
|
|
5677 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the
|
|
5678 ** page number that used to be the last root page in the file before
|
|
5679 ** the move. If no page gets moved, *piMoved is set to 0.
|
|
5680 ** The last root page is recorded in meta[3] and the value of
|
|
5681 ** meta[3] is updated by this procedure.
|
|
5682 */
|
|
5683 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
|
|
5684 int rc;
|
|
5685 MemPage *pPage = 0;
|
|
5686 BtShared *pBt = p->pBt;
|
|
5687
|
|
5688 if( p->inTrans!=TRANS_WRITE ){
|
|
5689 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5690 }
|
|
5691
|
|
5692 /* It is illegal to drop a table if any cursors are open on the
|
|
5693 ** database. This is because in auto-vacuum mode the backend may
|
|
5694 ** need to move another root-page to fill a gap left by the deleted
|
|
5695 ** root page. If an open cursor was using this page a problem would
|
|
5696 ** occur.
|
|
5697 */
|
|
5698 if( pBt->pCursor ){
|
|
5699 return SQLITE_LOCKED;
|
|
5700 }
|
|
5701
|
|
5702 rc = getPage(pBt, (Pgno)iTable, &pPage);
|
|
5703 if( rc ) return rc;
|
|
5704 rc = sqlite3BtreeClearTable(p, iTable);
|
|
5705 if( rc ){
|
|
5706 releasePage(pPage);
|
|
5707 return rc;
|
|
5708 }
|
|
5709
|
|
5710 *piMoved = 0;
|
|
5711
|
|
5712 if( iTable>1 ){
|
|
5713 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
5714 rc = freePage(pPage);
|
|
5715 releasePage(pPage);
|
|
5716 #else
|
|
5717 if( pBt->autoVacuum ){
|
|
5718 Pgno maxRootPgno;
|
|
5719 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
|
|
5720 if( rc!=SQLITE_OK ){
|
|
5721 releasePage(pPage);
|
|
5722 return rc;
|
|
5723 }
|
|
5724
|
|
5725 if( iTable==maxRootPgno ){
|
|
5726 /* If the table being dropped is the table with the largest root-page
|
|
5727 ** number in the database, put the root page on the free list.
|
|
5728 */
|
|
5729 rc = freePage(pPage);
|
|
5730 releasePage(pPage);
|
|
5731 if( rc!=SQLITE_OK ){
|
|
5732 return rc;
|
|
5733 }
|
|
5734 }else{
|
|
5735 /* The table being dropped does not have the largest root-page
|
|
5736 ** number in the database. So move the page that does into the
|
|
5737 ** gap left by the deleted root-page.
|
|
5738 */
|
|
5739 MemPage *pMove;
|
|
5740 releasePage(pPage);
|
|
5741 rc = getPage(pBt, maxRootPgno, &pMove);
|
|
5742 if( rc!=SQLITE_OK ){
|
|
5743 return rc;
|
|
5744 }
|
|
5745 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
|
|
5746 releasePage(pMove);
|
|
5747 if( rc!=SQLITE_OK ){
|
|
5748 return rc;
|
|
5749 }
|
|
5750 rc = getPage(pBt, maxRootPgno, &pMove);
|
|
5751 if( rc!=SQLITE_OK ){
|
|
5752 return rc;
|
|
5753 }
|
|
5754 rc = freePage(pMove);
|
|
5755 releasePage(pMove);
|
|
5756 if( rc!=SQLITE_OK ){
|
|
5757 return rc;
|
|
5758 }
|
|
5759 *piMoved = maxRootPgno;
|
|
5760 }
|
|
5761
|
|
5762 /* Set the new 'max-root-page' value in the database header. This
|
|
5763 ** is the old value less one, less one more if that happens to
|
|
5764 ** be a root-page number, less one again if that is the
|
|
5765 ** PENDING_BYTE_PAGE.
|
|
5766 */
|
|
5767 maxRootPgno--;
|
|
5768 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
|
|
5769 maxRootPgno--;
|
|
5770 }
|
|
5771 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
|
|
5772 maxRootPgno--;
|
|
5773 }
|
|
5774 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
|
|
5775
|
|
5776 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
|
|
5777 }else{
|
|
5778 rc = freePage(pPage);
|
|
5779 releasePage(pPage);
|
|
5780 }
|
|
5781 #endif
|
|
5782 }else{
|
|
5783 /* If sqlite3BtreeDropTable was called on page 1. */
|
|
5784 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
|
|
5785 releasePage(pPage);
|
|
5786 }
|
|
5787 return rc;
|
|
5788 }
|
|
5789
|
|
5790
|
|
5791 /*
|
|
5792 ** Read the meta-information out of a database file. Meta[0]
|
|
5793 ** is the number of free pages currently in the database. Meta[1]
|
|
5794 ** through meta[15] are available for use by higher layers. Meta[0]
|
|
5795 ** is read-only, the others are read/write.
|
|
5796 **
|
|
5797 ** The schema layer numbers meta values differently. At the schema
|
|
5798 ** layer (and the SetCookie and ReadCookie opcodes) the number of
|
|
5799 ** free pages is not visible. So Cookie[0] is the same as Meta[1].
|
|
5800 */
|
|
5801 int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
|
|
5802 int rc;
|
|
5803 unsigned char *pP1;
|
|
5804 BtShared *pBt = p->pBt;
|
|
5805
|
|
5806 /* Reading a meta-data value requires a read-lock on page 1 (and hence
|
|
5807 ** the sqlite_master table. We grab this lock regardless of whether or
|
|
5808 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
|
|
5809 ** 1 is treated as a special case by queryTableLock() and lockTable()).
|
|
5810 */
|
|
5811 rc = queryTableLock(p, 1, READ_LOCK);
|
|
5812 if( rc!=SQLITE_OK ){
|
|
5813 return rc;
|
|
5814 }
|
|
5815
|
|
5816 assert( idx>=0 && idx<=15 );
|
|
5817 rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1);
|
|
5818 if( rc ) return rc;
|
|
5819 *pMeta = get4byte(&pP1[36 + idx*4]);
|
|
5820 sqlite3pager_unref(pP1);
|
|
5821
|
|
5822 /* If autovacuumed is disabled in this build but we are trying to
|
|
5823 ** access an autovacuumed database, then make the database readonly.
|
|
5824 */
|
|
5825 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
5826 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
|
|
5827 #endif
|
|
5828
|
|
5829 /* Grab the read-lock on page 1. */
|
|
5830 rc = lockTable(p, 1, READ_LOCK);
|
|
5831 return rc;
|
|
5832 }
|
|
5833
|
|
5834 /*
|
|
5835 ** Write meta-information back into the database. Meta[0] is
|
|
5836 ** read-only and may not be written.
|
|
5837 */
|
|
5838 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
|
|
5839 BtShared *pBt = p->pBt;
|
|
5840 unsigned char *pP1;
|
|
5841 int rc;
|
|
5842 assert( idx>=1 && idx<=15 );
|
|
5843 if( p->inTrans!=TRANS_WRITE ){
|
|
5844 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
|
|
5845 }
|
|
5846 assert( pBt->pPage1!=0 );
|
|
5847 pP1 = pBt->pPage1->aData;
|
|
5848 rc = sqlite3pager_write(pP1);
|
|
5849 if( rc ) return rc;
|
|
5850 put4byte(&pP1[36 + idx*4], iMeta);
|
|
5851 return SQLITE_OK;
|
|
5852 }
|
|
5853
|
|
5854 /*
|
|
5855 ** Return the flag byte at the beginning of the page that the cursor
|
|
5856 ** is currently pointing to.
|
|
5857 */
|
|
5858 int sqlite3BtreeFlags(BtCursor *pCur){
|
|
5859 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
|
|
5860 ** restoreOrClearCursorPosition() here.
|
|
5861 */
|
|
5862 MemPage *pPage = pCur->pPage;
|
|
5863 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
|
|
5864 }
|
|
5865
|
|
5866 #ifdef SQLITE_DEBUG
|
|
5867 /*
|
|
5868 ** Print a disassembly of the given page on standard output. This routine
|
|
5869 ** is used for debugging and testing only.
|
|
5870 */
|
|
5871 static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){
|
|
5872 int rc;
|
|
5873 MemPage *pPage;
|
|
5874 int i, j, c;
|
|
5875 int nFree;
|
|
5876 u16 idx;
|
|
5877 int hdr;
|
|
5878 int nCell;
|
|
5879 int isInit;
|
|
5880 unsigned char *data;
|
|
5881 char range[20];
|
|
5882 unsigned char payload[20];
|
|
5883
|
|
5884 rc = getPage(pBt, (Pgno)pgno, &pPage);
|
|
5885 isInit = pPage->isInit;
|
|
5886 if( pPage->isInit==0 ){
|
|
5887 initPage(pPage, pParent);
|
|
5888 }
|
|
5889 if( rc ){
|
|
5890 return rc;
|
|
5891 }
|
|
5892 hdr = pPage->hdrOffset;
|
|
5893 data = pPage->aData;
|
|
5894 c = data[hdr];
|
|
5895 pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
|
|
5896 pPage->zeroData = (c & PTF_ZERODATA)!=0;
|
|
5897 pPage->leafData = (c & PTF_LEAFDATA)!=0;
|
|
5898 pPage->leaf = (c & PTF_LEAF)!=0;
|
|
5899 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
|
|
5900 nCell = get2byte(&data[hdr+3]);
|
|
5901 sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
|
|
5902 data[hdr], data[hdr+7],
|
|
5903 (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
|
|
5904 assert( hdr == (pgno==1 ? 100 : 0) );
|
|
5905 idx = hdr + 12 - pPage->leaf*4;
|
|
5906 for(i=0; i<nCell; i++){
|
|
5907 CellInfo info;
|
|
5908 Pgno child;
|
|
5909 unsigned char *pCell;
|
|
5910 int sz;
|
|
5911 int addr;
|
|
5912
|
|
5913 addr = get2byte(&data[idx + 2*i]);
|
|
5914 pCell = &data[addr];
|
|
5915 parseCellPtr(pPage, pCell, &info);
|
|
5916 sz = info.nSize;
|
|
5917 sprintf(range,"%d..%d", addr, addr+sz-1);
|
|
5918 if( pPage->leaf ){
|
|
5919 child = 0;
|
|
5920 }else{
|
|
5921 child = get4byte(pCell);
|
|
5922 }
|
|
5923 sz = info.nData;
|
|
5924 if( !pPage->intKey ) sz += info.nKey;
|
|
5925 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
|
|
5926 memcpy(payload, &pCell[info.nHeader], sz);
|
|
5927 for(j=0; j<sz; j++){
|
|
5928 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
|
|
5929 }
|
|
5930 payload[sz] = 0;
|
|
5931 sqlite3DebugPrintf(
|
|
5932 "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
|
|
5933 i, range, child, info.nKey, info.nData, payload
|
|
5934 );
|
|
5935 }
|
|
5936 if( !pPage->leaf ){
|
|
5937 sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
|
|
5938 }
|
|
5939 nFree = 0;
|
|
5940 i = 0;
|
|
5941 idx = get2byte(&data[hdr+1]);
|
|
5942 while( idx>0 && idx<pPage->pBt->usableSize ){
|
|
5943 int sz = get2byte(&data[idx+2]);
|
|
5944 sprintf(range,"%d..%d", idx, idx+sz-1);
|
|
5945 nFree += sz;
|
|
5946 sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
|
|
5947 i, range, sz, nFree);
|
|
5948 idx = get2byte(&data[idx]);
|
|
5949 i++;
|
|
5950 }
|
|
5951 if( idx!=0 ){
|
|
5952 sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
|
|
5953 }
|
|
5954 if( recursive && !pPage->leaf ){
|
|
5955 for(i=0; i<nCell; i++){
|
|
5956 unsigned char *pCell = findCell(pPage, i);
|
|
5957 btreePageDump(pBt, get4byte(pCell), 1, pPage);
|
|
5958 idx = get2byte(pCell);
|
|
5959 }
|
|
5960 btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
|
|
5961 }
|
|
5962 pPage->isInit = isInit;
|
|
5963 sqlite3pager_unref(data);
|
|
5964 fflush(stdout);
|
|
5965 return SQLITE_OK;
|
|
5966 }
|
|
5967 int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){
|
|
5968 return btreePageDump(p->pBt, pgno, recursive, 0);
|
|
5969 }
|
|
5970 #endif
|
|
5971
|
|
5972 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
|
|
5973 /*
|
|
5974 ** Fill aResult[] with information about the entry and page that the
|
|
5975 ** cursor is pointing to.
|
|
5976 **
|
|
5977 ** aResult[0] = The page number
|
|
5978 ** aResult[1] = The entry number
|
|
5979 ** aResult[2] = Total number of entries on this page
|
|
5980 ** aResult[3] = Cell size (local payload + header)
|
|
5981 ** aResult[4] = Number of free bytes on this page
|
|
5982 ** aResult[5] = Number of free blocks on the page
|
|
5983 ** aResult[6] = Total payload size (local + overflow)
|
|
5984 ** aResult[7] = Header size in bytes
|
|
5985 ** aResult[8] = Local payload size
|
|
5986 ** aResult[9] = Parent page number
|
|
5987 **
|
|
5988 ** This routine is used for testing and debugging only.
|
|
5989 */
|
|
5990 int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
|
|
5991 int cnt, idx;
|
|
5992 MemPage *pPage = pCur->pPage;
|
|
5993 BtCursor tmpCur;
|
|
5994
|
|
5995 int rc = restoreOrClearCursorPosition(pCur, 1);
|
|
5996 if( rc!=SQLITE_OK ){
|
|
5997 return rc;
|
|
5998 }
|
|
5999
|
|
6000 pageIntegrity(pPage);
|
|
6001 assert( pPage->isInit );
|
|
6002 getTempCursor(pCur, &tmpCur);
|
|
6003 while( upCnt-- ){
|
|
6004 moveToParent(&tmpCur);
|
|
6005 }
|
|
6006 pPage = tmpCur.pPage;
|
|
6007 pageIntegrity(pPage);
|
|
6008 aResult[0] = sqlite3pager_pagenumber(pPage->aData);
|
|
6009 assert( aResult[0]==pPage->pgno );
|
|
6010 aResult[1] = tmpCur.idx;
|
|
6011 aResult[2] = pPage->nCell;
|
|
6012 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
|
|
6013 getCellInfo(&tmpCur);
|
|
6014 aResult[3] = tmpCur.info.nSize;
|
|
6015 aResult[6] = tmpCur.info.nData;
|
|
6016 aResult[7] = tmpCur.info.nHeader;
|
|
6017 aResult[8] = tmpCur.info.nLocal;
|
|
6018 }else{
|
|
6019 aResult[3] = 0;
|
|
6020 aResult[6] = 0;
|
|
6021 aResult[7] = 0;
|
|
6022 aResult[8] = 0;
|
|
6023 }
|
|
6024 aResult[4] = pPage->nFree;
|
|
6025 cnt = 0;
|
|
6026 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
|
|
6027 while( idx>0 && idx<pPage->pBt->usableSize ){
|
|
6028 cnt++;
|
|
6029 idx = get2byte(&pPage->aData[idx]);
|
|
6030 }
|
|
6031 aResult[5] = cnt;
|
|
6032 if( pPage->pParent==0 || isRootPage(pPage) ){
|
|
6033 aResult[9] = 0;
|
|
6034 }else{
|
|
6035 aResult[9] = pPage->pParent->pgno;
|
|
6036 }
|
|
6037 releaseTempCursor(&tmpCur);
|
|
6038 return SQLITE_OK;
|
|
6039 }
|
|
6040 #endif
|
|
6041
|
|
6042 /*
|
|
6043 ** Return the pager associated with a BTree. This routine is used for
|
|
6044 ** testing and debugging only.
|
|
6045 */
|
|
6046 Pager *sqlite3BtreePager(Btree *p){
|
|
6047 return p->pBt->pPager;
|
|
6048 }
|
|
6049
|
|
6050 /*
|
|
6051 ** This structure is passed around through all the sanity checking routines
|
|
6052 ** in order to keep track of some global state information.
|
|
6053 */
|
|
6054 typedef struct IntegrityCk IntegrityCk;
|
|
6055 struct IntegrityCk {
|
|
6056 BtShared *pBt; /* The tree being checked out */
|
|
6057 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
|
|
6058 int nPage; /* Number of pages in the database */
|
|
6059 int *anRef; /* Number of times each page is referenced */
|
|
6060 char *zErrMsg; /* An error message. NULL of no errors seen. */
|
|
6061 };
|
|
6062
|
|
6063 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
|
|
6064 /*
|
|
6065 ** Append a message to the error message string.
|
|
6066 */
|
|
6067 static void checkAppendMsg(
|
|
6068 IntegrityCk *pCheck,
|
|
6069 char *zMsg1,
|
|
6070 const char *zFormat,
|
|
6071 ...
|
|
6072 ){
|
|
6073 va_list ap;
|
|
6074 char *zMsg2;
|
|
6075 va_start(ap, zFormat);
|
|
6076 zMsg2 = sqlite3VMPrintf(zFormat, ap);
|
|
6077 va_end(ap);
|
|
6078 if( zMsg1==0 ) zMsg1 = "";
|
|
6079 if( pCheck->zErrMsg ){
|
|
6080 char *zOld = pCheck->zErrMsg;
|
|
6081 pCheck->zErrMsg = 0;
|
|
6082 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
|
|
6083 sqliteFree(zOld);
|
|
6084 }else{
|
|
6085 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
|
|
6086 }
|
|
6087 sqliteFree(zMsg2);
|
|
6088 }
|
|
6089 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
|
|
6090
|
|
6091 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
|
|
6092 /*
|
|
6093 ** Add 1 to the reference count for page iPage. If this is the second
|
|
6094 ** reference to the page, add an error message to pCheck->zErrMsg.
|
|
6095 ** Return 1 if there are 2 ore more references to the page and 0 if
|
|
6096 ** if this is the first reference to the page.
|
|
6097 **
|
|
6098 ** Also check that the page number is in bounds.
|
|
6099 */
|
|
6100 static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
|
|
6101 if( iPage==0 ) return 1;
|
|
6102 if( iPage>pCheck->nPage || iPage<0 ){
|
|
6103 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
|
|
6104 return 1;
|
|
6105 }
|
|
6106 if( pCheck->anRef[iPage]==1 ){
|
|
6107 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
|
|
6108 return 1;
|
|
6109 }
|
|
6110 return (pCheck->anRef[iPage]++)>1;
|
|
6111 }
|
|
6112
|
|
6113 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6114 /*
|
|
6115 ** Check that the entry in the pointer-map for page iChild maps to
|
|
6116 ** page iParent, pointer type ptrType. If not, append an error message
|
|
6117 ** to pCheck.
|
|
6118 */
|
|
6119 static void checkPtrmap(
|
|
6120 IntegrityCk *pCheck, /* Integrity check context */
|
|
6121 Pgno iChild, /* Child page number */
|
|
6122 u8 eType, /* Expected pointer map type */
|
|
6123 Pgno iParent, /* Expected pointer map parent page number */
|
|
6124 char *zContext /* Context description (used for error msg) */
|
|
6125 ){
|
|
6126 int rc;
|
|
6127 u8 ePtrmapType;
|
|
6128 Pgno iPtrmapParent;
|
|
6129
|
|
6130 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
|
|
6131 if( rc!=SQLITE_OK ){
|
|
6132 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
|
|
6133 return;
|
|
6134 }
|
|
6135
|
|
6136 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
|
|
6137 checkAppendMsg(pCheck, zContext,
|
|
6138 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
|
|
6139 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
|
|
6140 }
|
|
6141 }
|
|
6142 #endif
|
|
6143
|
|
6144 /*
|
|
6145 ** Check the integrity of the freelist or of an overflow page list.
|
|
6146 ** Verify that the number of pages on the list is N.
|
|
6147 */
|
|
6148 static void checkList(
|
|
6149 IntegrityCk *pCheck, /* Integrity checking context */
|
|
6150 int isFreeList, /* True for a freelist. False for overflow page list */
|
|
6151 int iPage, /* Page number for first page in the list */
|
|
6152 int N, /* Expected number of pages in the list */
|
|
6153 char *zContext /* Context for error messages */
|
|
6154 ){
|
|
6155 int i;
|
|
6156 int expected = N;
|
|
6157 int iFirst = iPage;
|
|
6158 while( N-- > 0 ){
|
|
6159 unsigned char *pOvfl;
|
|
6160 if( iPage<1 ){
|
|
6161 checkAppendMsg(pCheck, zContext,
|
|
6162 "%d of %d pages missing from overflow list starting at %d",
|
|
6163 N+1, expected, iFirst);
|
|
6164 break;
|
|
6165 }
|
|
6166 if( checkRef(pCheck, iPage, zContext) ) break;
|
|
6167 if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
|
|
6168 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
|
|
6169 break;
|
|
6170 }
|
|
6171 if( isFreeList ){
|
|
6172 int n = get4byte(&pOvfl[4]);
|
|
6173 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6174 if( pCheck->pBt->autoVacuum ){
|
|
6175 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
|
|
6176 }
|
|
6177 #endif
|
|
6178 if( n>pCheck->pBt->usableSize/4-8 ){
|
|
6179 checkAppendMsg(pCheck, zContext,
|
|
6180 "freelist leaf count too big on page %d", iPage);
|
|
6181 N--;
|
|
6182 }else{
|
|
6183 for(i=0; i<n; i++){
|
|
6184 Pgno iFreePage = get4byte(&pOvfl[8+i*4]);
|
|
6185 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6186 if( pCheck->pBt->autoVacuum ){
|
|
6187 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
|
|
6188 }
|
|
6189 #endif
|
|
6190 checkRef(pCheck, iFreePage, zContext);
|
|
6191 }
|
|
6192 N -= n;
|
|
6193 }
|
|
6194 }
|
|
6195 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6196 else{
|
|
6197 /* If this database supports auto-vacuum and iPage is not the last
|
|
6198 ** page in this overflow list, check that the pointer-map entry for
|
|
6199 ** the following page matches iPage.
|
|
6200 */
|
|
6201 if( pCheck->pBt->autoVacuum && N>0 ){
|
|
6202 i = get4byte(pOvfl);
|
|
6203 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
|
|
6204 }
|
|
6205 }
|
|
6206 #endif
|
|
6207 iPage = get4byte(pOvfl);
|
|
6208 sqlite3pager_unref(pOvfl);
|
|
6209 }
|
|
6210 }
|
|
6211 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
|
|
6212
|
|
6213 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
|
|
6214 /*
|
|
6215 ** Do various sanity checks on a single page of a tree. Return
|
|
6216 ** the tree depth. Root pages return 0. Parents of root pages
|
|
6217 ** return 1, and so forth.
|
|
6218 **
|
|
6219 ** These checks are done:
|
|
6220 **
|
|
6221 ** 1. Make sure that cells and freeblocks do not overlap
|
|
6222 ** but combine to completely cover the page.
|
|
6223 ** NO 2. Make sure cell keys are in order.
|
|
6224 ** NO 3. Make sure no key is less than or equal to zLowerBound.
|
|
6225 ** NO 4. Make sure no key is greater than or equal to zUpperBound.
|
|
6226 ** 5. Check the integrity of overflow pages.
|
|
6227 ** 6. Recursively call checkTreePage on all children.
|
|
6228 ** 7. Verify that the depth of all children is the same.
|
|
6229 ** 8. Make sure this page is at least 33% full or else it is
|
|
6230 ** the root of the tree.
|
|
6231 */
|
|
6232 static int checkTreePage(
|
|
6233 IntegrityCk *pCheck, /* Context for the sanity check */
|
|
6234 int iPage, /* Page number of the page to check */
|
|
6235 MemPage *pParent, /* Parent page */
|
|
6236 char *zParentContext /* Parent context */
|
|
6237 ){
|
|
6238 MemPage *pPage;
|
|
6239 int i, rc, depth, d2, pgno, cnt;
|
|
6240 int hdr, cellStart;
|
|
6241 int nCell;
|
|
6242 u8 *data;
|
|
6243 BtShared *pBt;
|
|
6244 int usableSize;
|
|
6245 char zContext[100];
|
|
6246 char *hit;
|
|
6247
|
|
6248 sprintf(zContext, "Page %d: ", iPage);
|
|
6249
|
|
6250 /* Check that the page exists
|
|
6251 */
|
|
6252 pBt = pCheck->pBt;
|
|
6253 usableSize = pBt->usableSize;
|
|
6254 if( iPage==0 ) return 0;
|
|
6255 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
|
|
6256 if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){
|
|
6257 checkAppendMsg(pCheck, zContext,
|
|
6258 "unable to get the page. error code=%d", rc);
|
|
6259 return 0;
|
|
6260 }
|
|
6261 if( (rc = initPage(pPage, pParent))!=0 ){
|
|
6262 checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc);
|
|
6263 releasePage(pPage);
|
|
6264 return 0;
|
|
6265 }
|
|
6266
|
|
6267 /* Check out all the cells.
|
|
6268 */
|
|
6269 depth = 0;
|
|
6270 for(i=0; i<pPage->nCell; i++){
|
|
6271 u8 *pCell;
|
|
6272 int sz;
|
|
6273 CellInfo info;
|
|
6274
|
|
6275 /* Check payload overflow pages
|
|
6276 */
|
|
6277 sprintf(zContext, "On tree page %d cell %d: ", iPage, i);
|
|
6278 pCell = findCell(pPage,i);
|
|
6279 parseCellPtr(pPage, pCell, &info);
|
|
6280 sz = info.nData;
|
|
6281 if( !pPage->intKey ) sz += info.nKey;
|
|
6282 if( sz>info.nLocal ){
|
|
6283 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
|
|
6284 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
|
|
6285 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6286 if( pBt->autoVacuum ){
|
|
6287 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
|
|
6288 }
|
|
6289 #endif
|
|
6290 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
|
|
6291 }
|
|
6292
|
|
6293 /* Check sanity of left child page.
|
|
6294 */
|
|
6295 if( !pPage->leaf ){
|
|
6296 pgno = get4byte(pCell);
|
|
6297 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6298 if( pBt->autoVacuum ){
|
|
6299 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
|
|
6300 }
|
|
6301 #endif
|
|
6302 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
|
|
6303 if( i>0 && d2!=depth ){
|
|
6304 checkAppendMsg(pCheck, zContext, "Child page depth differs");
|
|
6305 }
|
|
6306 depth = d2;
|
|
6307 }
|
|
6308 }
|
|
6309 if( !pPage->leaf ){
|
|
6310 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
|
|
6311 sprintf(zContext, "On page %d at right child: ", iPage);
|
|
6312 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6313 if( pBt->autoVacuum ){
|
|
6314 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
|
|
6315 }
|
|
6316 #endif
|
|
6317 checkTreePage(pCheck, pgno, pPage, zContext);
|
|
6318 }
|
|
6319
|
|
6320 /* Check for complete coverage of the page
|
|
6321 */
|
|
6322 data = pPage->aData;
|
|
6323 hdr = pPage->hdrOffset;
|
|
6324 hit = sqliteMalloc( usableSize );
|
|
6325 if( hit ){
|
|
6326 memset(hit, 1, get2byte(&data[hdr+5]));
|
|
6327 nCell = get2byte(&data[hdr+3]);
|
|
6328 cellStart = hdr + 12 - 4*pPage->leaf;
|
|
6329 for(i=0; i<nCell; i++){
|
|
6330 int pc = get2byte(&data[cellStart+i*2]);
|
|
6331 int size = cellSizePtr(pPage, &data[pc]);
|
|
6332 int j;
|
|
6333 if( (pc+size-1)>=usableSize || pc<0 ){
|
|
6334 checkAppendMsg(pCheck, 0,
|
|
6335 "Corruption detected in cell %d on page %d",i,iPage,0);
|
|
6336 }else{
|
|
6337 for(j=pc+size-1; j>=pc; j--) hit[j]++;
|
|
6338 }
|
|
6339 }
|
|
6340 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
|
|
6341 cnt++){
|
|
6342 int size = get2byte(&data[i+2]);
|
|
6343 int j;
|
|
6344 if( (i+size-1)>=usableSize || i<0 ){
|
|
6345 checkAppendMsg(pCheck, 0,
|
|
6346 "Corruption detected in cell %d on page %d",i,iPage,0);
|
|
6347 }else{
|
|
6348 for(j=i+size-1; j>=i; j--) hit[j]++;
|
|
6349 }
|
|
6350 i = get2byte(&data[i]);
|
|
6351 }
|
|
6352 for(i=cnt=0; i<usableSize; i++){
|
|
6353 if( hit[i]==0 ){
|
|
6354 cnt++;
|
|
6355 }else if( hit[i]>1 ){
|
|
6356 checkAppendMsg(pCheck, 0,
|
|
6357 "Multiple uses for byte %d of page %d", i, iPage);
|
|
6358 break;
|
|
6359 }
|
|
6360 }
|
|
6361 if( cnt!=data[hdr+7] ){
|
|
6362 checkAppendMsg(pCheck, 0,
|
|
6363 "Fragmented space is %d byte reported as %d on page %d",
|
|
6364 cnt, data[hdr+7], iPage);
|
|
6365 }
|
|
6366 }
|
|
6367 sqliteFree(hit);
|
|
6368
|
|
6369 releasePage(pPage);
|
|
6370 return depth+1;
|
|
6371 }
|
|
6372 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
|
|
6373
|
|
6374 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
|
|
6375 /*
|
|
6376 ** This routine does a complete check of the given BTree file. aRoot[] is
|
|
6377 ** an array of pages numbers were each page number is the root page of
|
|
6378 ** a table. nRoot is the number of entries in aRoot.
|
|
6379 **
|
|
6380 ** If everything checks out, this routine returns NULL. If something is
|
|
6381 ** amiss, an error message is written into memory obtained from malloc()
|
|
6382 ** and a pointer to that error message is returned. The calling function
|
|
6383 ** is responsible for freeing the error message when it is done.
|
|
6384 */
|
|
6385 char *sqlite3BtreeIntegrityCheck(Btree *p, int *aRoot, int nRoot){
|
|
6386 int i;
|
|
6387 int nRef;
|
|
6388 IntegrityCk sCheck;
|
|
6389 BtShared *pBt = p->pBt;
|
|
6390
|
|
6391 nRef = *sqlite3pager_stats(pBt->pPager);
|
|
6392 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
|
|
6393 return sqliteStrDup("Unable to acquire a read lock on the database");
|
|
6394 }
|
|
6395 sCheck.pBt = pBt;
|
|
6396 sCheck.pPager = pBt->pPager;
|
|
6397 sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager);
|
|
6398 if( sCheck.nPage==0 ){
|
|
6399 unlockBtreeIfUnused(pBt);
|
|
6400 return 0;
|
|
6401 }
|
|
6402 sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
|
|
6403 if( !sCheck.anRef ){
|
|
6404 unlockBtreeIfUnused(pBt);
|
|
6405 return sqlite3MPrintf("Unable to malloc %d bytes",
|
|
6406 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
|
|
6407 }
|
|
6408 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
|
|
6409 i = PENDING_BYTE_PAGE(pBt);
|
|
6410 if( i<=sCheck.nPage ){
|
|
6411 sCheck.anRef[i] = 1;
|
|
6412 }
|
|
6413 sCheck.zErrMsg = 0;
|
|
6414
|
|
6415 /* Check the integrity of the freelist
|
|
6416 */
|
|
6417 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
|
|
6418 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
|
|
6419
|
|
6420 /* Check all the tables.
|
|
6421 */
|
|
6422 for(i=0; i<nRoot; i++){
|
|
6423 if( aRoot[i]==0 ) continue;
|
|
6424 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6425 if( pBt->autoVacuum && aRoot[i]>1 ){
|
|
6426 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
|
|
6427 }
|
|
6428 #endif
|
|
6429 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
|
|
6430 }
|
|
6431
|
|
6432 /* Make sure every page in the file is referenced
|
|
6433 */
|
|
6434 for(i=1; i<=sCheck.nPage; i++){
|
|
6435 #ifdef SQLITE_OMIT_AUTOVACUUM
|
|
6436 if( sCheck.anRef[i]==0 ){
|
|
6437 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
|
|
6438 }
|
|
6439 #else
|
|
6440 /* If the database supports auto-vacuum, make sure no tables contain
|
|
6441 ** references to pointer-map pages.
|
|
6442 */
|
|
6443 if( sCheck.anRef[i]==0 &&
|
|
6444 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
|
|
6445 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
|
|
6446 }
|
|
6447 if( sCheck.anRef[i]!=0 &&
|
|
6448 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
|
|
6449 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
|
|
6450 }
|
|
6451 #endif
|
|
6452 }
|
|
6453
|
|
6454 /* Make sure this analysis did not leave any unref() pages
|
|
6455 */
|
|
6456 unlockBtreeIfUnused(pBt);
|
|
6457 if( nRef != *sqlite3pager_stats(pBt->pPager) ){
|
|
6458 checkAppendMsg(&sCheck, 0,
|
|
6459 "Outstanding page count goes from %d to %d during this analysis",
|
|
6460 nRef, *sqlite3pager_stats(pBt->pPager)
|
|
6461 );
|
|
6462 }
|
|
6463
|
|
6464 /* Clean up and report errors.
|
|
6465 */
|
|
6466 sqliteFree(sCheck.anRef);
|
|
6467 return sCheck.zErrMsg;
|
|
6468 }
|
|
6469 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
|
|
6470
|
|
6471 /*
|
|
6472 ** Return the full pathname of the underlying database file.
|
|
6473 */
|
|
6474 const char *sqlite3BtreeGetFilename(Btree *p){
|
|
6475 assert( p->pBt->pPager!=0 );
|
|
6476 return sqlite3pager_filename(p->pBt->pPager);
|
|
6477 }
|
|
6478
|
|
6479 /*
|
|
6480 ** Return the pathname of the directory that contains the database file.
|
|
6481 */
|
|
6482 const char *sqlite3BtreeGetDirname(Btree *p){
|
|
6483 assert( p->pBt->pPager!=0 );
|
|
6484 return sqlite3pager_dirname(p->pBt->pPager);
|
|
6485 }
|
|
6486
|
|
6487 /*
|
|
6488 ** Return the pathname of the journal file for this database. The return
|
|
6489 ** value of this routine is the same regardless of whether the journal file
|
|
6490 ** has been created or not.
|
|
6491 */
|
|
6492 const char *sqlite3BtreeGetJournalname(Btree *p){
|
|
6493 assert( p->pBt->pPager!=0 );
|
|
6494 return sqlite3pager_journalname(p->pBt->pPager);
|
|
6495 }
|
|
6496
|
|
6497 #ifndef SQLITE_OMIT_VACUUM
|
|
6498 /*
|
|
6499 ** Copy the complete content of pBtFrom into pBtTo. A transaction
|
|
6500 ** must be active for both files.
|
|
6501 **
|
|
6502 ** The size of file pBtFrom may be reduced by this operation.
|
|
6503 ** If anything goes wrong, the transaction on pBtFrom is rolled back.
|
|
6504 */
|
|
6505 int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
|
|
6506 int rc = SQLITE_OK;
|
|
6507 Pgno i, nPage, nToPage, iSkip;
|
|
6508
|
|
6509 BtShared *pBtTo = pTo->pBt;
|
|
6510 BtShared *pBtFrom = pFrom->pBt;
|
|
6511
|
|
6512 if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
|
|
6513 return SQLITE_ERROR;
|
|
6514 }
|
|
6515 if( pBtTo->pCursor ) return SQLITE_BUSY;
|
|
6516 nToPage = sqlite3pager_pagecount(pBtTo->pPager);
|
|
6517 nPage = sqlite3pager_pagecount(pBtFrom->pPager);
|
|
6518 iSkip = PENDING_BYTE_PAGE(pBtTo);
|
|
6519 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
|
|
6520 void *pPage;
|
|
6521 if( i==iSkip ) continue;
|
|
6522 rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage);
|
|
6523 if( rc ) break;
|
|
6524 rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage);
|
|
6525 if( rc ) break;
|
|
6526 sqlite3pager_unref(pPage);
|
|
6527 }
|
|
6528 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
|
|
6529 void *pPage;
|
|
6530 if( i==iSkip ) continue;
|
|
6531 rc = sqlite3pager_get(pBtTo->pPager, i, &pPage);
|
|
6532 if( rc ) break;
|
|
6533 rc = sqlite3pager_write(pPage);
|
|
6534 sqlite3pager_unref(pPage);
|
|
6535 sqlite3pager_dont_write(pBtTo->pPager, i);
|
|
6536 }
|
|
6537 if( !rc && nPage<nToPage ){
|
|
6538 rc = sqlite3pager_truncate(pBtTo->pPager, nPage);
|
|
6539 }
|
|
6540 if( rc ){
|
|
6541 sqlite3BtreeRollback(pTo);
|
|
6542 }
|
|
6543 return rc;
|
|
6544 }
|
|
6545 #endif /* SQLITE_OMIT_VACUUM */
|
|
6546
|
|
6547 /*
|
|
6548 ** Return non-zero if a transaction is active.
|
|
6549 */
|
|
6550 int sqlite3BtreeIsInTrans(Btree *p){
|
|
6551 return (p && (p->inTrans==TRANS_WRITE));
|
|
6552 }
|
|
6553
|
|
6554 /*
|
|
6555 ** Return non-zero if a statement transaction is active.
|
|
6556 */
|
|
6557 int sqlite3BtreeIsInStmt(Btree *p){
|
|
6558 return (p->pBt && p->pBt->inStmt);
|
|
6559 }
|
|
6560
|
|
6561 /*
|
|
6562 ** This call is a no-op if no write-transaction is currently active on pBt.
|
|
6563 **
|
|
6564 ** Otherwise, sync the database file for the btree pBt. zMaster points to
|
|
6565 ** the name of a master journal file that should be written into the
|
|
6566 ** individual journal file, or is NULL, indicating no master journal file
|
|
6567 ** (single database transaction).
|
|
6568 **
|
|
6569 ** When this is called, the master journal should already have been
|
|
6570 ** created, populated with this journal pointer and synced to disk.
|
|
6571 **
|
|
6572 ** Once this is routine has returned, the only thing required to commit
|
|
6573 ** the write-transaction for this database file is to delete the journal.
|
|
6574 */
|
|
6575 int sqlite3BtreeSync(Btree *p, const char *zMaster){
|
|
6576 int rc = SQLITE_OK;
|
|
6577 if( p->inTrans==TRANS_WRITE ){
|
|
6578 BtShared *pBt = p->pBt;
|
|
6579 Pgno nTrunc = 0;
|
|
6580 #ifndef SQLITE_OMIT_AUTOVACUUM
|
|
6581 if( pBt->autoVacuum ){
|
|
6582 rc = autoVacuumCommit(pBt, &nTrunc);
|
|
6583 if( rc!=SQLITE_OK ){
|
|
6584 return rc;
|
|
6585 }
|
|
6586 }
|
|
6587 #endif
|
|
6588 rc = sqlite3pager_sync(pBt->pPager, zMaster, nTrunc);
|
|
6589 }
|
|
6590 return rc;
|
|
6591 }
|
|
6592
|
|
6593 /*
|
|
6594 ** This function returns a pointer to a blob of memory associated with
|
|
6595 ** a single shared-btree. The memory is used by client code for it's own
|
|
6596 ** purposes (for example, to store a high-level schema associated with
|
|
6597 ** the shared-btree). The btree layer manages reference counting issues.
|
|
6598 **
|
|
6599 ** The first time this is called on a shared-btree, nBytes bytes of memory
|
|
6600 ** are allocated, zeroed, and returned to the caller. For each subsequent
|
|
6601 ** call the nBytes parameter is ignored and a pointer to the same blob
|
|
6602 ** of memory returned.
|
|
6603 **
|
|
6604 ** Just before the shared-btree is closed, the function passed as the
|
|
6605 ** xFree argument when the memory allocation was made is invoked on the
|
|
6606 ** blob of allocated memory. This function should not call sqliteFree()
|
|
6607 ** on the memory, the btree layer does that.
|
|
6608 */
|
|
6609 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
|
|
6610 BtShared *pBt = p->pBt;
|
|
6611 if( !pBt->pSchema ){
|
|
6612 pBt->pSchema = sqliteMalloc(nBytes);
|
|
6613 pBt->xFreeSchema = xFree;
|
|
6614 }
|
|
6615 return pBt->pSchema;
|
|
6616 }
|
|
6617
|
|
6618 /*
|
|
6619 ** Return true if another user of the same shared btree as the argument
|
|
6620 ** handle holds an exclusive lock on the sqlite_master table.
|
|
6621 */
|
|
6622 int sqlite3BtreeSchemaLocked(Btree *p){
|
|
6623 return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
|
|
6624 }
|
|
6625
|
|
6626
|
|
6627 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
6628 /*
|
|
6629 ** Obtain a lock on the table whose root page is iTab. The
|
|
6630 ** lock is a write lock if isWritelock is true or a read lock
|
|
6631 ** if it is false.
|
|
6632 */
|
|
6633 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
|
|
6634 int rc = SQLITE_OK;
|
|
6635 u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
|
|
6636 rc = queryTableLock(p, iTab, lockType);
|
|
6637 if( rc==SQLITE_OK ){
|
|
6638 rc = lockTable(p, iTab, lockType);
|
|
6639 }
|
|
6640 return rc;
|
|
6641 }
|
|
6642 #endif
|
|
6643
|
|
6644 /*
|
|
6645 ** The following debugging interface has to be in this file (rather
|
|
6646 ** than in, for example, test1.c) so that it can get access to
|
|
6647 ** the definition of BtShared.
|
|
6648 */
|
|
6649 #if defined(SQLITE_DEBUG) && defined(TCLSH)
|
|
6650 #include <tcl.h>
|
|
6651 int sqlite3_shared_cache_report(
|
|
6652 void * clientData,
|
|
6653 Tcl_Interp *interp,
|
|
6654 int objc,
|
|
6655 Tcl_Obj *CONST objv[]
|
|
6656 ){
|
|
6657 #ifndef SQLITE_OMIT_SHARED_CACHE
|
|
6658 const ThreadData *pTd = sqlite3ThreadDataReadOnly();
|
|
6659 if( pTd->useSharedData ){
|
|
6660 BtShared *pBt;
|
|
6661 Tcl_Obj *pRet = Tcl_NewObj();
|
|
6662 for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){
|
|
6663 const char *zFile = sqlite3pager_filename(pBt->pPager);
|
|
6664 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
|
|
6665 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
|
|
6666 }
|
|
6667 Tcl_SetObjResult(interp, pRet);
|
|
6668 }
|
|
6669 #endif
|
|
6670 return TCL_OK;
|
|
6671 }
|
|
6672 #endif
|