blob: fe103ddec9b4efed62eee876be9d1f68d356e313 [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
drh9e572e62004-04-23 23:43:10 +00002** 2004 April 6
drha059ad02001-04-17 20:09:11 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drha059ad02001-04-17 20:09:11 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drha059ad02001-04-17 20:09:11 +000010**
11*************************************************************************
drh6e345992007-03-30 11:12:08 +000012** $Id: btree.c,v 1.346 2007/03/30 11:12:08 drh Exp $
drh8b2f49b2001-06-08 00:21:52 +000013**
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** ----------------------------------------------------------------
drha4124a02007-03-27 14:05:22 +000025** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
drh8b2f49b2001-06-08 00:21:52 +000026** ----------------------------------------------------------------
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
drha4124a02007-03-27 14:05:22 +000031** on Ptr(N) and its subpages have values greater than Key(N-1). And
drh8b2f49b2001-06-08 00:21:52 +000032** so forth.
33**
drh5e00f6c2001-09-13 13:46:56 +000034** Finding a particular key requires reading O(log(M)) pages from the
35** disk where M is the number of entries in the tree.
drh8b2f49b2001-06-08 00:21:52 +000036**
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
drh9e572e62004-04-23 23:43:10 +000039** 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
drha4124a02007-03-27 14:05:22 +000044** page has a small header which contains the Ptr(N) pointer and other
drh9e572e62004-04-23 23:43:10 +000045** information such as the size of key and data.
drh8b2f49b2001-06-08 00:21:52 +000046**
drh9e572e62004-04-23 23:43:10 +000047** 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
drh271efa52004-05-30 19:19:05 +000056** page contain a special header (the "file header") that describes the file.
57** The format of the file header is as follows:
drh9e572e62004-04-23 23:43:10 +000058**
59** OFFSET SIZE DESCRIPTION
drhde647132004-05-07 17:57:49 +000060** 0 16 Header string: "SQLite format 3\000"
drh9e572e62004-04-23 23:43:10 +000061** 16 2 Page size in bytes.
62** 18 1 File format write version
63** 19 1 File format read version
drh6f11bef2004-05-13 01:12:56 +000064** 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
drh9e572e62004-04-23 23:43:10 +000070** 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).
drh6f11bef2004-05-13 01:12:56 +000075**
drhab01f612004-05-22 02:55:23 +000076** The file change counter is incremented when the database is changed more
drh6f11bef2004-05-13 01:12:56 +000077** 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
drhab01f612004-05-22 02:55:23 +000086** on one page. Thus the default max embedded payload fraction is 64.
drh6f11bef2004-05-13 01:12:56 +000087**
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.
drh9e572e62004-04-23 23:43:10 +000097**
drh43605152004-05-29 21:46:49 +000098** 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
drh271efa52004-05-30 19:19:05 +0000100** 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** |----------------|
drh43605152004-05-29 21:46:49 +0000117**
118** The page headers looks like this:
drh9e572e62004-04-23 23:43:10 +0000119**
120** OFFSET SIZE DESCRIPTION
drh6f11bef2004-05-13 01:12:56 +0000121** 0 1 Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
drh9e572e62004-04-23 23:43:10 +0000122** 1 2 byte offset to the first freeblock
drh43605152004-05-29 21:46:49 +0000123** 3 2 number of cells on this page
drh271efa52004-05-30 19:19:05 +0000124** 5 2 first byte of the cell content area
drh43605152004-05-29 21:46:49 +0000125** 7 1 number of fragmented free bytes
drha4124a02007-03-27 14:05:22 +0000126** 8 4 Right child (the Ptr(N) value). Omitted on leaves.
drh9e572e62004-04-23 23:43:10 +0000127**
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
drh44f87bd2004-09-27 13:19:51 +0000130** 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.
drh9e572e62004-04-23 23:43:10 +0000133**
drh43605152004-05-29 21:46:49 +0000134** 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
drh44f87bd2004-09-27 13:19:51 +0000139** be easily added without having to defragment the page.
drh43605152004-05-29 21:46:49 +0000140**
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
drh9e572e62004-04-23 23:43:10 +0000164** byte are used. The integer consists of all bytes that have bit 8 set and
drh6f11bef2004-05-13 01:12:56 +0000165** the first byte with bit 8 clear. The most significant byte of the integer
drhab01f612004-05-22 02:55:23 +0000166** 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.
drh9e572e62004-04-23 23:43:10 +0000169**
170** 0x00 becomes 0x00000000
drh6f11bef2004-05-13 01:12:56 +0000171** 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
drh9e572e62004-04-23 23:43:10 +0000176** 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**
drh43605152004-05-29 21:46:49 +0000181** The content of a cell looks like this:
drh9e572e62004-04-23 23:43:10 +0000182**
183** SIZE DESCRIPTION
drh3aac2dd2004-04-26 14:10:20 +0000184** 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.
drh9e572e62004-04-23 23:43:10 +0000187** * 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
drha059ad02001-04-17 20:09:11 +0000207*/
208#include "sqliteInt.h"
209#include "pager.h"
210#include "btree.h"
drh1f595712004-06-15 01:40:29 +0000211#include "os.h"
drha059ad02001-04-17 20:09:11 +0000212#include <assert.h>
213
drhc96d8532005-05-03 12:30:33 +0000214/* 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
drh4b70f112004-05-02 21:12:19 +0000220/* The following value is the maximum cell size assuming a maximum page
221** size give above.
222*/
drh2e38c322004-09-03 18:38:44 +0000223#define MX_CELL_SIZE(pBt) (pBt->pageSize-8)
drh4b70f112004-05-02 21:12:19 +0000224
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*/
drh2e38c322004-09-03 18:38:44 +0000229#define MX_CELL(pBt) ((pBt->pageSize-8)/3)
drh4b70f112004-05-02 21:12:19 +0000230
paulb95a8862003-04-01 21:16:41 +0000231/* Forward declarations */
drh3aac2dd2004-04-26 14:10:20 +0000232typedef struct MemPage MemPage;
danielk1977aef0bf62005-12-30 16:28:01 +0000233typedef struct BtLock BtLock;
paulb95a8862003-04-01 21:16:41 +0000234
drh8c42ca92001-06-22 19:15:00 +0000235/*
drhbd03cae2001-06-02 02:40:57 +0000236** This is a magic string that appears at the beginning of every
drh8c42ca92001-06-22 19:15:00 +0000237** SQLite database in order to identify the file as a real database.
drh556b2a22005-06-14 16:04:05 +0000238**
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
250static const char zMagicHeader[] = SQLITE_FILE_HEADER;
drh08ed44e2001-04-29 23:32:55 +0000251
252/*
drh4b70f112004-05-02 21:12:19 +0000253** Page type flags. An ORed combination of these flags appear as the
254** first byte of every BTree page.
drh8c42ca92001-06-22 19:15:00 +0000255*/
drhde647132004-05-07 17:57:49 +0000256#define PTF_INTKEY 0x01
drh9e572e62004-04-23 23:43:10 +0000257#define PTF_ZERODATA 0x02
drh8b18dd42004-05-12 19:18:15 +0000258#define PTF_LEAFDATA 0x04
259#define PTF_LEAF 0x08
drh8c42ca92001-06-22 19:15:00 +0000260
261/*
drh9e572e62004-04-23 23:43:10 +0000262** 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.
drh14acc042001-06-10 19:56:58 +0000265**
drh72f82862001-05-24 21:06:34 +0000266** 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.
drhbd03cae2001-06-02 02:40:57 +0000269** The pageDestructor() routine handles that chore.
drh7e3b0a02001-04-28 16:52:40 +0000270*/
271struct MemPage {
drha6abd042004-06-09 17:37:22 +0000272 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
drh43605152004-05-29 21:46:49 +0000273 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 */
drh271efa52004-05-30 19:19:05 +0000281 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
drha2fce642004-06-05 00:01:44 +0000282 u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */
283 u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */
drh43605152004-05-29 21:46:49 +0000284 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[] */
danielk1977aef0bf62005-12-30 16:28:01 +0000289 u8 *pCell; /* Pointers to the body of the overflow cell */
290 u16 idx; /* Insert this cell before idx-th non-overflow cell */
drha2fce642004-06-05 00:01:44 +0000291 } aOvfl[5];
drh47ded162006-01-06 01:42:58 +0000292 BtShared *pBt; /* Pointer back to BTree structure */
293 u8 *aData; /* Pointer back to the start of the page */
danielk19773b8a05f2007-03-19 17:44:26 +0000294 DbPage *pDbPage; /* Pager page handle */
drh47ded162006-01-06 01:42:58 +0000295 Pgno pgno; /* Page number for this page */
296 MemPage *pParent; /* The parent of this page. NULL for root */
drh8c42ca92001-06-22 19:15:00 +0000297};
drh7e3b0a02001-04-28 16:52:40 +0000298
299/*
drh3b7511c2001-05-26 13:15:44 +0000300** The in-memory image of a disk page has the auxiliary information appended
301** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
302** that extra information.
303*/
drh3aac2dd2004-04-26 14:10:20 +0000304#define EXTRA_SIZE sizeof(MemPage)
drh3b7511c2001-05-26 13:15:44 +0000305
danielk1977aef0bf62005-12-30 16:28:01 +0000306/* Btree handle */
307struct Btree {
308 sqlite3 *pSqlite;
309 BtShared *pBt;
310 u8 inTrans; /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
311};
312
313/*
314** Btree.inTrans may take one of the following values.
315**
316** If the shared-data extension is enabled, there may be multiple users
317** of the Btree structure. At most one of these may open a write transaction,
318** but any number may have active read transactions. Variable Btree.pDb
319** points to the handle that owns any current write-transaction.
320*/
321#define TRANS_NONE 0
322#define TRANS_READ 1
323#define TRANS_WRITE 2
324
drh3b7511c2001-05-26 13:15:44 +0000325/*
drha059ad02001-04-17 20:09:11 +0000326** Everything we need to know about an open database
327*/
danielk1977aef0bf62005-12-30 16:28:01 +0000328struct BtShared {
drha059ad02001-04-17 20:09:11 +0000329 Pager *pPager; /* The page cache */
drh306dc212001-05-21 13:45:10 +0000330 BtCursor *pCursor; /* A list of all open cursors */
drh3aac2dd2004-04-26 14:10:20 +0000331 MemPage *pPage1; /* First page of the database */
drhab01f612004-05-22 02:55:23 +0000332 u8 inStmt; /* True if we are in a statement subtransaction */
drh5df72a52002-06-06 23:16:05 +0000333 u8 readOnly; /* True if the underlying file is readonly */
drhab01f612004-05-22 02:55:23 +0000334 u8 maxEmbedFrac; /* Maximum payload as % of total page size */
335 u8 minEmbedFrac; /* Minimum payload as % of total page size */
336 u8 minLeafFrac; /* Minimum leaf payload as % of total page size */
drh90f5ecb2004-07-22 01:19:35 +0000337 u8 pageSizeFixed; /* True if the page size can no longer be changed */
drh057cd3a2005-02-15 16:23:02 +0000338#ifndef SQLITE_OMIT_AUTOVACUUM
339 u8 autoVacuum; /* True if database supports auto-vacuum */
340#endif
drha2fce642004-06-05 00:01:44 +0000341 u16 pageSize; /* Total number of bytes on a page */
342 u16 usableSize; /* Number of usable bytes on each page */
drh6f11bef2004-05-13 01:12:56 +0000343 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
344 int minLocal; /* Minimum local payload in non-LEAFDATA tables */
345 int maxLeaf; /* Maximum local payload in a LEAFDATA table */
346 int minLeaf; /* Minimum local payload in a LEAFDATA table */
drhb8ef32c2005-03-14 02:01:49 +0000347 BusyHandler *pBusyHandler; /* Callback for when there is lock contention */
danielk1977aef0bf62005-12-30 16:28:01 +0000348 u8 inTransaction; /* Transaction state */
danielk1977aef0bf62005-12-30 16:28:01 +0000349 int nRef; /* Number of references to this structure */
350 int nTransaction; /* Number of open transactions (read + write) */
danielk19772e94d4d2006-01-09 05:36:27 +0000351 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
352 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
353#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000354 BtLock *pLock; /* List of locks held on this shared-btree struct */
danielk1977e501b892006-01-09 06:29:47 +0000355 BtShared *pNext; /* Next in ThreadData.pBtree linked list */
danielk19772e94d4d2006-01-09 05:36:27 +0000356#endif
drha059ad02001-04-17 20:09:11 +0000357};
danielk1977ee5741e2004-05-31 10:01:34 +0000358
359/*
drhfa1a98a2004-05-14 19:08:17 +0000360** An instance of the following structure is used to hold information
drh271efa52004-05-30 19:19:05 +0000361** about a cell. The parseCellPtr() function fills in this structure
drhab01f612004-05-22 02:55:23 +0000362** based on information extract from the raw disk page.
drhfa1a98a2004-05-14 19:08:17 +0000363*/
364typedef struct CellInfo CellInfo;
365struct CellInfo {
drh43605152004-05-29 21:46:49 +0000366 u8 *pCell; /* Pointer to the start of cell content */
drhfa1a98a2004-05-14 19:08:17 +0000367 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
368 u32 nData; /* Number of bytes of data */
drh72365832007-03-06 15:53:44 +0000369 u32 nPayload; /* Total amount of payload */
drh271efa52004-05-30 19:19:05 +0000370 u16 nHeader; /* Size of the cell content header in bytes */
drhfa1a98a2004-05-14 19:08:17 +0000371 u16 nLocal; /* Amount of payload held locally */
drhab01f612004-05-22 02:55:23 +0000372 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
drh271efa52004-05-30 19:19:05 +0000373 u16 nSize; /* Size of the cell content on the main b-tree page */
drhfa1a98a2004-05-14 19:08:17 +0000374};
375
376/*
drh365d68f2001-05-11 11:02:46 +0000377** A cursor is a pointer to a particular entry in the BTree.
378** The entry is identified by its MemPage and the index in
drha34b6762004-05-07 13:30:42 +0000379** MemPage.aCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000380*/
drh72f82862001-05-24 21:06:34 +0000381struct BtCursor {
danielk1977aef0bf62005-12-30 16:28:01 +0000382 Btree *pBtree; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000383 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drh3aac2dd2004-04-26 14:10:20 +0000384 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
385 void *pArg; /* First arg to xCompare() */
drh8b2f49b2001-06-08 00:21:52 +0000386 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000387 MemPage *pPage; /* Page that contains the entry */
drh3aac2dd2004-04-26 14:10:20 +0000388 int idx; /* Index of the entry in pPage->aCell[] */
drhfa1a98a2004-05-14 19:08:17 +0000389 CellInfo info; /* A parse of the cell we are pointing at */
drhecdc7532001-09-23 02:35:53 +0000390 u8 wrFlag; /* True if writable */
danielk1977da184232006-01-05 11:34:32 +0000391 u8 eState; /* One of the CURSOR_XXX constants (see below) */
drh4eeb1ff2006-03-23 14:03:00 +0000392 void *pKey; /* Saved key that was cursor's last known position */
393 i64 nKey; /* Size of pKey, or last integer key */
danielk1977da184232006-01-05 11:34:32 +0000394 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
drh365d68f2001-05-11 11:02:46 +0000395};
drh7e3b0a02001-04-28 16:52:40 +0000396
drha059ad02001-04-17 20:09:11 +0000397/*
drh980b1a72006-08-16 16:42:48 +0000398** Potential values for BtCursor.eState.
danielk1977da184232006-01-05 11:34:32 +0000399**
400** CURSOR_VALID:
401** Cursor points to a valid entry. getPayload() etc. may be called.
402**
403** CURSOR_INVALID:
404** Cursor does not point to a valid entry. This can happen (for example)
405** because the table is empty or because BtreeCursorFirst() has not been
406** called.
407**
408** CURSOR_REQUIRESEEK:
409** The table that this cursor was opened on still exists, but has been
410** modified since the cursor was last used. The cursor position is saved
411** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
danielk1977955de522006-02-10 02:27:42 +0000412** this state, restoreOrClearCursorPosition() can be called to attempt to
413** seek the cursor to the saved position.
danielk1977da184232006-01-05 11:34:32 +0000414*/
415#define CURSOR_INVALID 0
416#define CURSOR_VALID 1
417#define CURSOR_REQUIRESEEK 2
418
419/*
drh615ae552005-01-16 23:21:00 +0000420** The TRACE macro will print high-level status information about the
421** btree operation when the global variable sqlite3_btree_trace is
422** enabled.
423*/
424#if SQLITE_TEST
425# define TRACE(X) if( sqlite3_btree_trace )\
drhd3627af2006-12-18 18:34:51 +0000426/* { sqlite3DebugPrintf X; fflush(stdout); } */ \
427{ printf X; fflush(stdout); }
drh0f7eb612006-08-08 13:51:43 +0000428int sqlite3_btree_trace=0; /* True to enable tracing */
drh615ae552005-01-16 23:21:00 +0000429#else
430# define TRACE(X)
431#endif
drh615ae552005-01-16 23:21:00 +0000432
433/*
drh66cbd152004-09-01 16:12:25 +0000434** Forward declaration
435*/
drh980b1a72006-08-16 16:42:48 +0000436static int checkReadLocks(Btree*,Pgno,BtCursor*);
drh66cbd152004-09-01 16:12:25 +0000437
drh66cbd152004-09-01 16:12:25 +0000438/*
drhab01f612004-05-22 02:55:23 +0000439** Read or write a two- and four-byte big-endian integer values.
drh0d316a42002-08-11 20:10:47 +0000440*/
drh9e572e62004-04-23 23:43:10 +0000441static u32 get2byte(unsigned char *p){
442 return (p[0]<<8) | p[1];
drh0d316a42002-08-11 20:10:47 +0000443}
drh9e572e62004-04-23 23:43:10 +0000444static u32 get4byte(unsigned char *p){
445 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
446}
drh9e572e62004-04-23 23:43:10 +0000447static void put2byte(unsigned char *p, u32 v){
448 p[0] = v>>8;
449 p[1] = v;
450}
451static void put4byte(unsigned char *p, u32 v){
452 p[0] = v>>24;
453 p[1] = v>>16;
454 p[2] = v>>8;
455 p[3] = v;
456}
drh6f11bef2004-05-13 01:12:56 +0000457
drh9e572e62004-04-23 23:43:10 +0000458/*
drhab01f612004-05-22 02:55:23 +0000459** Routines to read and write variable-length integers. These used to
460** be defined locally, but now we use the varint routines in the util.c
461** file.
drh9e572e62004-04-23 23:43:10 +0000462*/
drh6d2fb152004-05-14 16:50:06 +0000463#define getVarint sqlite3GetVarint
drh504b6982006-01-22 21:52:56 +0000464/* #define getVarint32 sqlite3GetVarint32 */
465#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
drh6d2fb152004-05-14 16:50:06 +0000466#define putVarint sqlite3PutVarint
drh0d316a42002-08-11 20:10:47 +0000467
danielk1977599fcba2004-11-08 07:13:13 +0000468/* The database page the PENDING_BYTE occupies. This page is never used.
469** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
470** should possibly be consolidated (presumably in pager.h).
drhfe9a9142006-03-14 12:59:10 +0000471**
472** If disk I/O is omitted (meaning that the database is stored purely
473** in memory) then there is no pending byte.
danielk1977599fcba2004-11-08 07:13:13 +0000474*/
drhfe9a9142006-03-14 12:59:10 +0000475#ifdef SQLITE_OMIT_DISKIO
476# define PENDING_BYTE_PAGE(pBt) 0x7fffffff
477#else
478# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
479#endif
danielk1977afcdd022004-10-31 16:25:42 +0000480
danielk1977aef0bf62005-12-30 16:28:01 +0000481/*
482** A linked list of the following structures is stored at BtShared.pLock.
483** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
484** is opened on the table with root page BtShared.iTable. Locks are removed
485** from this list when a transaction is committed or rolled back, or when
486** a btree handle is closed.
487*/
488struct BtLock {
489 Btree *pBtree; /* Btree handle holding this lock */
490 Pgno iTable; /* Root page of table */
491 u8 eLock; /* READ_LOCK or WRITE_LOCK */
492 BtLock *pNext; /* Next in BtShared.pLock list */
493};
494
495/* Candidate values for BtLock.eLock */
496#define READ_LOCK 1
497#define WRITE_LOCK 2
498
499#ifdef SQLITE_OMIT_SHARED_CACHE
500 /*
501 ** The functions queryTableLock(), lockTable() and unlockAllTables()
502 ** manipulate entries in the BtShared.pLock linked list used to store
503 ** shared-cache table level locks. If the library is compiled with the
504 ** shared-cache feature disabled, then there is only ever one user
danielk1977da184232006-01-05 11:34:32 +0000505 ** of each BtShared structure and so this locking is not necessary.
506 ** So define the lock related functions as no-ops.
danielk1977aef0bf62005-12-30 16:28:01 +0000507 */
508 #define queryTableLock(a,b,c) SQLITE_OK
509 #define lockTable(a,b,c) SQLITE_OK
danielk1977da184232006-01-05 11:34:32 +0000510 #define unlockAllTables(a)
danielk1977aef0bf62005-12-30 16:28:01 +0000511#else
512
danielk1977e7259292006-01-13 06:33:23 +0000513
danielk1977da184232006-01-05 11:34:32 +0000514/*
danielk1977aef0bf62005-12-30 16:28:01 +0000515** Query to see if btree handle p may obtain a lock of type eLock
516** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
517** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
danielk1977c87d34d2006-01-06 13:00:28 +0000518** SQLITE_LOCKED if not.
danielk1977aef0bf62005-12-30 16:28:01 +0000519*/
520static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
521 BtShared *pBt = p->pBt;
522 BtLock *pIter;
523
danielk1977da184232006-01-05 11:34:32 +0000524 /* This is a no-op if the shared-cache is not enabled */
drh6f7adc82006-01-11 21:41:20 +0000525 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
danielk1977da184232006-01-05 11:34:32 +0000526 return SQLITE_OK;
527 }
528
529 /* This (along with lockTable()) is where the ReadUncommitted flag is
530 ** dealt with. If the caller is querying for a read-lock and the flag is
531 ** set, it is unconditionally granted - even if there are write-locks
532 ** on the table. If a write-lock is requested, the ReadUncommitted flag
533 ** is not considered.
534 **
535 ** In function lockTable(), if a read-lock is demanded and the
536 ** ReadUncommitted flag is set, no entry is added to the locks list
537 ** (BtShared.pLock).
538 **
539 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
540 ** not create or respect table locks. The locking procedure for a
541 ** write-cursor does not change.
542 */
543 if(
544 !p->pSqlite ||
545 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) ||
546 eLock==WRITE_LOCK ||
drh47ded162006-01-06 01:42:58 +0000547 iTab==MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000548 ){
549 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
550 if( pIter->pBtree!=p && pIter->iTable==iTab &&
551 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
danielk1977c87d34d2006-01-06 13:00:28 +0000552 return SQLITE_LOCKED;
danielk1977da184232006-01-05 11:34:32 +0000553 }
danielk1977aef0bf62005-12-30 16:28:01 +0000554 }
555 }
556 return SQLITE_OK;
557}
558
559/*
560** Add a lock on the table with root-page iTable to the shared-btree used
561** by Btree handle p. Parameter eLock must be either READ_LOCK or
562** WRITE_LOCK.
563**
564** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
565** SQLITE_NOMEM may also be returned.
566*/
567static int lockTable(Btree *p, Pgno iTable, u8 eLock){
568 BtShared *pBt = p->pBt;
569 BtLock *pLock = 0;
570 BtLock *pIter;
571
danielk1977da184232006-01-05 11:34:32 +0000572 /* This is a no-op if the shared-cache is not enabled */
drh6f7adc82006-01-11 21:41:20 +0000573 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
danielk1977da184232006-01-05 11:34:32 +0000574 return SQLITE_OK;
575 }
576
danielk1977aef0bf62005-12-30 16:28:01 +0000577 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
578
danielk1977da184232006-01-05 11:34:32 +0000579 /* If the read-uncommitted flag is set and a read-lock is requested,
580 ** return early without adding an entry to the BtShared.pLock list. See
581 ** comment in function queryTableLock() for more info on handling
582 ** the ReadUncommitted flag.
583 */
584 if(
585 (p->pSqlite) &&
586 (p->pSqlite->flags&SQLITE_ReadUncommitted) &&
587 (eLock==READ_LOCK) &&
drh47ded162006-01-06 01:42:58 +0000588 iTable!=MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000589 ){
590 return SQLITE_OK;
591 }
592
danielk1977aef0bf62005-12-30 16:28:01 +0000593 /* First search the list for an existing lock on this table. */
594 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
595 if( pIter->iTable==iTable && pIter->pBtree==p ){
596 pLock = pIter;
597 break;
598 }
599 }
600
601 /* If the above search did not find a BtLock struct associating Btree p
602 ** with table iTable, allocate one and link it into the list.
603 */
604 if( !pLock ){
605 pLock = (BtLock *)sqliteMalloc(sizeof(BtLock));
606 if( !pLock ){
607 return SQLITE_NOMEM;
608 }
609 pLock->iTable = iTable;
610 pLock->pBtree = p;
611 pLock->pNext = pBt->pLock;
612 pBt->pLock = pLock;
613 }
614
615 /* Set the BtLock.eLock variable to the maximum of the current lock
616 ** and the requested lock. This means if a write-lock was already held
617 ** and a read-lock requested, we don't incorrectly downgrade the lock.
618 */
619 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53 +0000620 if( eLock>pLock->eLock ){
621 pLock->eLock = eLock;
622 }
danielk1977aef0bf62005-12-30 16:28:01 +0000623
624 return SQLITE_OK;
625}
626
627/*
628** Release all the table locks (locks obtained via calls to the lockTable()
629** procedure) held by Btree handle p.
630*/
631static void unlockAllTables(Btree *p){
632 BtLock **ppIter = &p->pBt->pLock;
danielk1977da184232006-01-05 11:34:32 +0000633
634 /* If the shared-cache extension is not enabled, there should be no
635 ** locks in the BtShared.pLock list, making this procedure a no-op. Assert
636 ** that this is the case.
637 */
drh6f7adc82006-01-11 21:41:20 +0000638 assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter );
danielk1977da184232006-01-05 11:34:32 +0000639
danielk1977aef0bf62005-12-30 16:28:01 +0000640 while( *ppIter ){
641 BtLock *pLock = *ppIter;
642 if( pLock->pBtree==p ){
643 *ppIter = pLock->pNext;
644 sqliteFree(pLock);
645 }else{
646 ppIter = &pLock->pNext;
647 }
648 }
649}
650#endif /* SQLITE_OMIT_SHARED_CACHE */
651
drh980b1a72006-08-16 16:42:48 +0000652static void releasePage(MemPage *pPage); /* Forward reference */
653
654/*
655** Save the current cursor position in the variables BtCursor.nKey
656** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
657*/
658static int saveCursorPosition(BtCursor *pCur){
659 int rc;
660
661 assert( CURSOR_VALID==pCur->eState );
662 assert( 0==pCur->pKey );
663
664 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
665
666 /* If this is an intKey table, then the above call to BtreeKeySize()
667 ** stores the integer key in pCur->nKey. In this case this value is
668 ** all that is required. Otherwise, if pCur is not open on an intKey
669 ** table, then malloc space for and store the pCur->nKey bytes of key
670 ** data.
671 */
672 if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
673 void *pKey = sqliteMalloc(pCur->nKey);
674 if( pKey ){
675 rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
676 if( rc==SQLITE_OK ){
677 pCur->pKey = pKey;
678 }else{
679 sqliteFree(pKey);
680 }
681 }else{
682 rc = SQLITE_NOMEM;
683 }
684 }
685 assert( !pCur->pPage->intKey || !pCur->pKey );
686
687 if( rc==SQLITE_OK ){
688 releasePage(pCur->pPage);
689 pCur->pPage = 0;
690 pCur->eState = CURSOR_REQUIRESEEK;
691 }
692
693 return rc;
694}
695
696/*
697** Save the positions of all cursors except pExcept open on the table
698** with root-page iRoot. Usually, this is called just before cursor
699** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
700*/
701static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
702 BtCursor *p;
703 for(p=pBt->pCursor; p; p=p->pNext){
704 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
705 p->eState==CURSOR_VALID ){
706 int rc = saveCursorPosition(p);
707 if( SQLITE_OK!=rc ){
708 return rc;
709 }
710 }
711 }
712 return SQLITE_OK;
713}
714
715/*
716** Restore the cursor to the position it was in (or as close to as possible)
717** when saveCursorPosition() was called. Note that this call deletes the
718** saved position info stored by saveCursorPosition(), so there can be
719** at most one effective restoreOrClearCursorPosition() call after each
720** saveCursorPosition().
721**
722** If the second argument argument - doSeek - is false, then instead of
723** returning the cursor to it's saved position, any saved position is deleted
724** and the cursor state set to CURSOR_INVALID.
725*/
726static int restoreOrClearCursorPositionX(BtCursor *pCur, int doSeek){
727 int rc = SQLITE_OK;
728 assert( pCur->eState==CURSOR_REQUIRESEEK );
729 pCur->eState = CURSOR_INVALID;
730 if( doSeek ){
drhe4d90812007-03-29 05:51:49 +0000731 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
drh980b1a72006-08-16 16:42:48 +0000732 }
733 if( rc==SQLITE_OK ){
734 sqliteFree(pCur->pKey);
735 pCur->pKey = 0;
736 assert( CURSOR_VALID==pCur->eState || CURSOR_INVALID==pCur->eState );
737 }
738 return rc;
739}
740
741#define restoreOrClearCursorPosition(p,x) \
742 (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p,x):SQLITE_OK)
743
danielk1977599fcba2004-11-08 07:13:13 +0000744#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000745/*
drh42cac6d2004-11-20 20:31:11 +0000746** These macros define the location of the pointer-map entry for a
747** database page. The first argument to each is the number of usable
748** bytes on each page of the database (often 1024). The second is the
749** page number to look up in the pointer map.
danielk1977afcdd022004-10-31 16:25:42 +0000750**
751** PTRMAP_PAGENO returns the database page number of the pointer-map
752** page that stores the required pointer. PTRMAP_PTROFFSET returns
753** the offset of the requested map entry.
754**
755** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
756** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
danielk1977599fcba2004-11-08 07:13:13 +0000757** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
758** this test.
danielk1977afcdd022004-10-31 16:25:42 +0000759*/
danielk1977266664d2006-02-10 08:24:21 +0000760#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
761#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
762#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
763
764static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
765 int nPagesPerMapPage = (pBt->usableSize/5)+1;
766 int iPtrMap = (pgno-2)/nPagesPerMapPage;
767 int ret = (iPtrMap*nPagesPerMapPage) + 2;
768 if( ret==PENDING_BYTE_PAGE(pBt) ){
769 ret++;
770 }
771 return ret;
772}
danielk1977a19df672004-11-03 11:37:07 +0000773
danielk1977afcdd022004-10-31 16:25:42 +0000774/*
drh615ae552005-01-16 23:21:00 +0000775** The pointer map is a lookup table that identifies the parent page for
776** each child page in the database file. The parent page is the page that
777** contains a pointer to the child. Every page in the database contains
778** 0 or 1 parent pages. (In this context 'database page' refers
779** to any page that is not part of the pointer map itself.) Each pointer map
780** entry consists of a single byte 'type' and a 4 byte parent page number.
781** The PTRMAP_XXX identifiers below are the valid types.
782**
783** The purpose of the pointer map is to facility moving pages from one
784** position in the file to another as part of autovacuum. When a page
785** is moved, the pointer in its parent must be updated to point to the
786** new location. The pointer map is used to locate the parent page quickly.
danielk1977afcdd022004-10-31 16:25:42 +0000787**
danielk1977687566d2004-11-02 12:56:41 +0000788** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
789** used in this case.
danielk1977afcdd022004-10-31 16:25:42 +0000790**
danielk1977687566d2004-11-02 12:56:41 +0000791** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
792** is not used in this case.
793**
794** PTRMAP_OVERFLOW1: The database page is the first page in a list of
795** overflow pages. The page number identifies the page that
796** contains the cell with a pointer to this overflow page.
797**
798** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
799** overflow pages. The page-number identifies the previous
800** page in the overflow page list.
801**
802** PTRMAP_BTREE: The database page is a non-root btree page. The page number
803** identifies the parent page in the btree.
danielk1977afcdd022004-10-31 16:25:42 +0000804*/
danielk1977687566d2004-11-02 12:56:41 +0000805#define PTRMAP_ROOTPAGE 1
806#define PTRMAP_FREEPAGE 2
807#define PTRMAP_OVERFLOW1 3
808#define PTRMAP_OVERFLOW2 4
809#define PTRMAP_BTREE 5
danielk1977afcdd022004-10-31 16:25:42 +0000810
811/*
812** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000813**
814** This routine updates the pointer map entry for page number 'key'
815** so that it maps to type 'eType' and parent page number 'pgno'.
816** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000817*/
danielk1977aef0bf62005-12-30 16:28:01 +0000818static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
danielk19773b8a05f2007-03-19 17:44:26 +0000819 DbPage *pDbPage; /* The pointer map page */
820 u8 *pPtrmap; /* The pointer map data */
821 Pgno iPtrmap; /* The pointer map page number */
822 int offset; /* Offset in pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000823 int rc;
824
danielk1977266664d2006-02-10 08:24:21 +0000825 /* The master-journal page number must never be used as a pointer map page */
826 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
827
danielk1977ac11ee62005-01-15 12:45:51 +0000828 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000829 if( key==0 ){
drh49285702005-09-17 15:20:26 +0000830 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +0000831 }
danielk1977266664d2006-02-10 08:24:21 +0000832 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000833 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977687566d2004-11-02 12:56:41 +0000834 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000835 return rc;
836 }
danielk1977266664d2006-02-10 08:24:21 +0000837 offset = PTRMAP_PTROFFSET(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000838 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000839
drh615ae552005-01-16 23:21:00 +0000840 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
841 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk19773b8a05f2007-03-19 17:44:26 +0000842 rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000843 if( rc==SQLITE_OK ){
844 pPtrmap[offset] = eType;
845 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000846 }
danielk1977afcdd022004-10-31 16:25:42 +0000847 }
848
danielk19773b8a05f2007-03-19 17:44:26 +0000849 sqlite3PagerUnref(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000850 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000851}
852
853/*
854** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000855**
856** This routine retrieves the pointer map entry for page 'key', writing
857** the type and parent page number to *pEType and *pPgno respectively.
858** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000859*/
danielk1977aef0bf62005-12-30 16:28:01 +0000860static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:26 +0000861 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000862 int iPtrmap; /* Pointer map page index */
863 u8 *pPtrmap; /* Pointer map page data */
864 int offset; /* Offset of entry in pointer map */
865 int rc;
866
danielk1977266664d2006-02-10 08:24:21 +0000867 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000868 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000869 if( rc!=0 ){
870 return rc;
871 }
danielk19773b8a05f2007-03-19 17:44:26 +0000872 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000873
danielk1977266664d2006-02-10 08:24:21 +0000874 offset = PTRMAP_PTROFFSET(pBt, key);
drh43617e92006-03-06 20:55:46 +0000875 assert( pEType!=0 );
876 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:41 +0000877 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000878
danielk19773b8a05f2007-03-19 17:44:26 +0000879 sqlite3PagerUnref(pDbPage);
drh49285702005-09-17 15:20:26 +0000880 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
danielk1977afcdd022004-10-31 16:25:42 +0000881 return SQLITE_OK;
882}
883
884#endif /* SQLITE_OMIT_AUTOVACUUM */
885
drh0d316a42002-08-11 20:10:47 +0000886/*
drh271efa52004-05-30 19:19:05 +0000887** Given a btree page and a cell index (0 means the first cell on
888** the page, 1 means the second cell, and so forth) return a pointer
889** to the cell content.
890**
891** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000892*/
drh43605152004-05-29 21:46:49 +0000893static u8 *findCell(MemPage *pPage, int iCell){
894 u8 *data = pPage->aData;
895 assert( iCell>=0 );
896 assert( iCell<get2byte(&data[pPage->hdrOffset+3]) );
897 return data + get2byte(&data[pPage->cellOffset+2*iCell]);
898}
899
900/*
901** This a more complex version of findCell() that works for
902** pages that do contain overflow cells. See insert
903*/
904static u8 *findOverflowCell(MemPage *pPage, int iCell){
905 int i;
906 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000907 int k;
908 struct _OvflCell *pOvfl;
909 pOvfl = &pPage->aOvfl[i];
910 k = pOvfl->idx;
911 if( k<=iCell ){
912 if( k==iCell ){
913 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000914 }
915 iCell--;
916 }
917 }
918 return findCell(pPage, iCell);
919}
920
921/*
922** Parse a cell content block and fill in the CellInfo structure. There
923** are two versions of this function. parseCell() takes a cell index
924** as the second argument and parseCellPtr() takes a pointer to the
925** body of the cell as its second argument.
926*/
927static void parseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000928 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000929 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000930 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000931){
drh271efa52004-05-30 19:19:05 +0000932 int n; /* Number bytes in cell content header */
933 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000934
935 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000936 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000937 n = pPage->childPtrSize;
938 assert( n==4-4*pPage->leaf );
drh8b18dd42004-05-12 19:18:15 +0000939 if( pPage->hasData ){
drh271efa52004-05-30 19:19:05 +0000940 n += getVarint32(&pCell[n], &nPayload);
drh8b18dd42004-05-12 19:18:15 +0000941 }else{
drh271efa52004-05-30 19:19:05 +0000942 nPayload = 0;
drh3aac2dd2004-04-26 14:10:20 +0000943 }
drh271efa52004-05-30 19:19:05 +0000944 pInfo->nData = nPayload;
drh504b6982006-01-22 21:52:56 +0000945 if( pPage->intKey ){
946 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
947 }else{
948 u32 x;
949 n += getVarint32(&pCell[n], &x);
950 pInfo->nKey = x;
951 nPayload += x;
drh6f11bef2004-05-13 01:12:56 +0000952 }
drh72365832007-03-06 15:53:44 +0000953 pInfo->nPayload = nPayload;
drh504b6982006-01-22 21:52:56 +0000954 pInfo->nHeader = n;
drh271efa52004-05-30 19:19:05 +0000955 if( nPayload<=pPage->maxLocal ){
956 /* This is the (easy) common case where the entire payload fits
957 ** on the local page. No overflow is required.
958 */
959 int nSize; /* Total size of cell content in bytes */
drh6f11bef2004-05-13 01:12:56 +0000960 pInfo->nLocal = nPayload;
961 pInfo->iOverflow = 0;
drh271efa52004-05-30 19:19:05 +0000962 nSize = nPayload + n;
963 if( nSize<4 ){
964 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000965 }
drh271efa52004-05-30 19:19:05 +0000966 pInfo->nSize = nSize;
drh6f11bef2004-05-13 01:12:56 +0000967 }else{
drh271efa52004-05-30 19:19:05 +0000968 /* If the payload will not fit completely on the local page, we have
969 ** to decide how much to store locally and how much to spill onto
970 ** overflow pages. The strategy is to minimize the amount of unused
971 ** space on overflow pages while keeping the amount of local storage
972 ** in between minLocal and maxLocal.
973 **
974 ** Warning: changing the way overflow payload is distributed in any
975 ** way will result in an incompatible file format.
976 */
977 int minLocal; /* Minimum amount of payload held locally */
978 int maxLocal; /* Maximum amount of payload held locally */
979 int surplus; /* Overflow payload available for local storage */
980
981 minLocal = pPage->minLocal;
982 maxLocal = pPage->maxLocal;
983 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000984 if( surplus <= maxLocal ){
985 pInfo->nLocal = surplus;
986 }else{
987 pInfo->nLocal = minLocal;
988 }
989 pInfo->iOverflow = pInfo->nLocal + n;
990 pInfo->nSize = pInfo->iOverflow + 4;
991 }
drh3aac2dd2004-04-26 14:10:20 +0000992}
drh43605152004-05-29 21:46:49 +0000993static void parseCell(
994 MemPage *pPage, /* Page containing the cell */
995 int iCell, /* The cell index. First cell is 0 */
996 CellInfo *pInfo /* Fill in this structure */
997){
998 parseCellPtr(pPage, findCell(pPage, iCell), pInfo);
999}
drh3aac2dd2004-04-26 14:10:20 +00001000
1001/*
drh43605152004-05-29 21:46:49 +00001002** Compute the total number of bytes that a Cell needs in the cell
1003** data area of the btree-page. The return number includes the cell
1004** data header and the local payload, but not any overflow page or
1005** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +00001006*/
danielk1977bc6ada42004-06-30 08:20:16 +00001007#ifndef NDEBUG
drh43605152004-05-29 21:46:49 +00001008static int cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +00001009 CellInfo info;
drh43605152004-05-29 21:46:49 +00001010 parseCell(pPage, iCell, &info);
1011 return info.nSize;
1012}
danielk1977bc6ada42004-06-30 08:20:16 +00001013#endif
drh43605152004-05-29 21:46:49 +00001014static int cellSizePtr(MemPage *pPage, u8 *pCell){
1015 CellInfo info;
1016 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00001017 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +00001018}
1019
danielk197779a40da2005-01-16 08:00:01 +00001020#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +00001021/*
danielk197726836652005-01-17 01:33:13 +00001022** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +00001023** to an overflow page, insert an entry into the pointer-map
1024** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +00001025*/
danielk197726836652005-01-17 01:33:13 +00001026static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
danielk197779a40da2005-01-16 08:00:01 +00001027 if( pCell ){
1028 CellInfo info;
1029 parseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00001030 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk197779a40da2005-01-16 08:00:01 +00001031 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 }
danielk1977ac11ee62005-01-15 12:45:51 +00001035 }
danielk197779a40da2005-01-16 08:00:01 +00001036 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +00001037}
danielk197726836652005-01-17 01:33:13 +00001038/*
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*/
1043static int ptrmapPutOvfl(MemPage *pPage, int iCell){
1044 u8 *pCell;
1045 pCell = findOverflowCell(pPage, iCell);
1046 return ptrmapPutOvflPtr(pPage, pCell);
1047}
danielk197779a40da2005-01-16 08:00:01 +00001048#endif
1049
danielk1977ac11ee62005-01-15 12:45:51 +00001050
danielk1977aef0bf62005-12-30 16:28:01 +00001051/* A bunch of assert() statements to check the transaction state variables
1052** of handle p (type Btree*) are internally consistent.
1053*/
1054#define btreeIntegrity(p) \
1055 assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \
1056 assert( p->pBt->nTransaction<=p->pBt->nRef ); \
1057 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
1058 assert( p->pBt->inTransaction>=p->inTrans );
1059
drhda200cc2004-05-09 11:51:38 +00001060/*
drh72f82862001-05-24 21:06:34 +00001061** Defragment the page given. All Cells are moved to the
drh3a4a2d42005-11-24 14:24:28 +00001062** end of the page and all free space is collected into one
1063** big FreeBlk that occurs in between the header and cell
drh31beae92005-11-24 14:34:36 +00001064** pointer array and the cell content area.
drh365d68f2001-05-11 11:02:46 +00001065*/
drh2e38c322004-09-03 18:38:44 +00001066static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +00001067 int i; /* Loop counter */
1068 int pc; /* Address of a i-th cell */
1069 int addr; /* Offset of first byte after cell pointer array */
1070 int hdr; /* Offset to the page header */
1071 int size; /* Size of a cell */
1072 int usableSize; /* Number of usable bytes on a page */
1073 int cellOffset; /* Offset to the cell pointer array */
1074 int brk; /* Offset to the cell content area */
1075 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +00001076 unsigned char *data; /* The page data */
1077 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +00001078
danielk19773b8a05f2007-03-19 17:44:26 +00001079 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001080 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +00001081 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +00001082 assert( pPage->nOverflow==0 );
drh2e38c322004-09-03 18:38:44 +00001083 temp = sqliteMalloc( pPage->pBt->pageSize );
1084 if( temp==0 ) return SQLITE_NOMEM;
drh43605152004-05-29 21:46:49 +00001085 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +00001086 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00001087 cellOffset = pPage->cellOffset;
1088 nCell = pPage->nCell;
1089 assert( nCell==get2byte(&data[hdr+3]) );
1090 usableSize = pPage->pBt->usableSize;
1091 brk = get2byte(&data[hdr+5]);
1092 memcpy(&temp[brk], &data[brk], usableSize - brk);
1093 brk = usableSize;
1094 for(i=0; i<nCell; i++){
1095 u8 *pAddr; /* The i-th cell pointer */
1096 pAddr = &data[cellOffset + i*2];
1097 pc = get2byte(pAddr);
1098 assert( pc<pPage->pBt->usableSize );
1099 size = cellSizePtr(pPage, &temp[pc]);
1100 brk -= size;
1101 memcpy(&data[brk], &temp[pc], size);
1102 put2byte(pAddr, brk);
drh2af926b2001-05-15 00:39:25 +00001103 }
drh43605152004-05-29 21:46:49 +00001104 assert( brk>=cellOffset+2*nCell );
1105 put2byte(&data[hdr+5], brk);
1106 data[hdr+1] = 0;
1107 data[hdr+2] = 0;
1108 data[hdr+7] = 0;
1109 addr = cellOffset+2*nCell;
1110 memset(&data[addr], 0, brk-addr);
drh2e38c322004-09-03 18:38:44 +00001111 sqliteFree(temp);
1112 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +00001113}
1114
drha059ad02001-04-17 20:09:11 +00001115/*
drh43605152004-05-29 21:46:49 +00001116** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +00001117**
drh9e572e62004-04-23 23:43:10 +00001118** Return the index into pPage->aData[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +00001119** the new allocation. Or return 0 if there is not enough free
1120** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +00001121**
drh72f82862001-05-24 21:06:34 +00001122** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +00001123** nBytes of contiguous free space, then this routine automatically
1124** calls defragementPage() to consolidate all free space before
1125** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +00001126*/
drh9e572e62004-04-23 23:43:10 +00001127static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +00001128 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +00001129 int size;
drh24cd67e2004-05-10 16:18:47 +00001130 int nFrag;
drh43605152004-05-29 21:46:49 +00001131 int top;
1132 int nCell;
1133 int cellOffset;
drh9e572e62004-04-23 23:43:10 +00001134 unsigned char *data;
drh43605152004-05-29 21:46:49 +00001135
drh9e572e62004-04-23 23:43:10 +00001136 data = pPage->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001137 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001138 assert( pPage->pBt );
1139 if( nByte<4 ) nByte = 4;
drh43605152004-05-29 21:46:49 +00001140 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
1141 pPage->nFree -= nByte;
drh9e572e62004-04-23 23:43:10 +00001142 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00001143
1144 nFrag = data[hdr+7];
1145 if( nFrag<60 ){
1146 /* Search the freelist looking for a slot big enough to satisfy the
1147 ** space request. */
1148 addr = hdr+1;
1149 while( (pc = get2byte(&data[addr]))>0 ){
1150 size = get2byte(&data[pc+2]);
1151 if( size>=nByte ){
1152 if( size<nByte+4 ){
1153 memcpy(&data[addr], &data[pc], 2);
1154 data[hdr+7] = nFrag + size - nByte;
1155 return pc;
1156 }else{
1157 put2byte(&data[pc+2], size-nByte);
1158 return pc + size - nByte;
1159 }
1160 }
1161 addr = pc;
drh9e572e62004-04-23 23:43:10 +00001162 }
1163 }
drh43605152004-05-29 21:46:49 +00001164
1165 /* Allocate memory from the gap in between the cell pointer array
1166 ** and the cell content area.
1167 */
1168 top = get2byte(&data[hdr+5]);
1169 nCell = get2byte(&data[hdr+3]);
1170 cellOffset = pPage->cellOffset;
1171 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
drh2e38c322004-09-03 18:38:44 +00001172 if( defragmentPage(pPage) ) return 0;
drh43605152004-05-29 21:46:49 +00001173 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +00001174 }
drh43605152004-05-29 21:46:49 +00001175 top -= nByte;
1176 assert( cellOffset + 2*nCell <= top );
1177 put2byte(&data[hdr+5], top);
1178 return top;
drh7e3b0a02001-04-28 16:52:40 +00001179}
1180
1181/*
drh9e572e62004-04-23 23:43:10 +00001182** Return a section of the pPage->aData to the freelist.
1183** The first byte of the new free block is pPage->aDisk[start]
1184** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +00001185**
1186** Most of the effort here is involved in coalesing adjacent
1187** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +00001188*/
drh9e572e62004-04-23 23:43:10 +00001189static void freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +00001190 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +00001191 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +00001192
drh9e572e62004-04-23 23:43:10 +00001193 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001194 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001195 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +00001196 assert( (start + size)<=pPage->pBt->usableSize );
drh9e572e62004-04-23 23:43:10 +00001197 if( size<4 ) size = 4;
1198
drhfcce93f2006-02-22 03:08:32 +00001199#ifdef SQLITE_SECURE_DELETE
1200 /* Overwrite deleted information with zeros when the SECURE_DELETE
1201 ** option is enabled at compile-time */
1202 memset(&data[start], 0, size);
1203#endif
1204
drh9e572e62004-04-23 23:43:10 +00001205 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +00001206 hdr = pPage->hdrOffset;
1207 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +00001208 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +00001209 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +00001210 assert( pbegin>addr );
1211 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +00001212 }
drhb6f41482004-05-14 01:58:11 +00001213 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +00001214 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +00001215 put2byte(&data[addr], start);
1216 put2byte(&data[start], pbegin);
1217 put2byte(&data[start+2], size);
drh2af926b2001-05-15 00:39:25 +00001218 pPage->nFree += size;
drh9e572e62004-04-23 23:43:10 +00001219
1220 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +00001221 addr = pPage->hdrOffset + 1;
1222 while( (pbegin = get2byte(&data[addr]))>0 ){
drh9e572e62004-04-23 23:43:10 +00001223 int pnext, psize;
drh3aac2dd2004-04-26 14:10:20 +00001224 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +00001225 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +00001226 pnext = get2byte(&data[pbegin]);
1227 psize = get2byte(&data[pbegin+2]);
1228 if( pbegin + psize + 3 >= pnext && pnext>0 ){
1229 int frag = pnext - (pbegin+psize);
drh43605152004-05-29 21:46:49 +00001230 assert( frag<=data[pPage->hdrOffset+7] );
1231 data[pPage->hdrOffset+7] -= frag;
drh9e572e62004-04-23 23:43:10 +00001232 put2byte(&data[pbegin], get2byte(&data[pnext]));
1233 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
1234 }else{
drh3aac2dd2004-04-26 14:10:20 +00001235 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +00001236 }
1237 }
drh7e3b0a02001-04-28 16:52:40 +00001238
drh43605152004-05-29 21:46:49 +00001239 /* If the cell content area begins with a freeblock, remove it. */
1240 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
1241 int top;
1242 pbegin = get2byte(&data[hdr+1]);
1243 memcpy(&data[hdr+1], &data[pbegin], 2);
1244 top = get2byte(&data[hdr+5]);
1245 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
drh4b70f112004-05-02 21:12:19 +00001246 }
drh4b70f112004-05-02 21:12:19 +00001247}
1248
1249/*
drh271efa52004-05-30 19:19:05 +00001250** Decode the flags byte (the first byte of the header) for a page
1251** and initialize fields of the MemPage structure accordingly.
1252*/
1253static void decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:01 +00001254 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:05 +00001255
1256 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1257 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
1258 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
1259 pPage->leaf = (flagByte & PTF_LEAF)!=0;
1260 pPage->childPtrSize = 4*(pPage->leaf==0);
1261 pBt = pPage->pBt;
1262 if( flagByte & PTF_LEAFDATA ){
1263 pPage->leafData = 1;
1264 pPage->maxLocal = pBt->maxLeaf;
1265 pPage->minLocal = pBt->minLeaf;
1266 }else{
1267 pPage->leafData = 0;
1268 pPage->maxLocal = pBt->maxLocal;
1269 pPage->minLocal = pBt->minLocal;
1270 }
1271 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
1272}
1273
1274/*
drh7e3b0a02001-04-28 16:52:40 +00001275** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +00001276**
drhbd03cae2001-06-02 02:40:57 +00001277** The pParent parameter must be a pointer to the MemPage which
drh9e572e62004-04-23 23:43:10 +00001278** is the parent of the page being initialized. The root of a
1279** BTree has no parent and so for that page, pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +00001280**
drh72f82862001-05-24 21:06:34 +00001281** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +00001282** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +00001283** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1284** guarantee that the page is well-formed. It only shows that
1285** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +00001286*/
drh9e572e62004-04-23 23:43:10 +00001287static int initPage(
drh3aac2dd2004-04-26 14:10:20 +00001288 MemPage *pPage, /* The page to be initialized */
drh9e572e62004-04-23 23:43:10 +00001289 MemPage *pParent /* The parent. Might be NULL */
1290){
drh271efa52004-05-30 19:19:05 +00001291 int pc; /* Address of a freeblock within pPage->aData[] */
drh271efa52004-05-30 19:19:05 +00001292 int hdr; /* Offset to beginning of page header */
1293 u8 *data; /* Equal to pPage->aData */
danielk1977aef0bf62005-12-30 16:28:01 +00001294 BtShared *pBt; /* The main btree structure */
drh271efa52004-05-30 19:19:05 +00001295 int usableSize; /* Amount of usable space on each page */
1296 int cellOffset; /* Offset from start of page to first cell pointer */
1297 int nFree; /* Number of unused bytes on the page */
1298 int top; /* First byte of the cell content area */
drh2af926b2001-05-15 00:39:25 +00001299
drh2e38c322004-09-03 18:38:44 +00001300 pBt = pPage->pBt;
1301 assert( pBt!=0 );
1302 assert( pParent==0 || pParent->pBt==pBt );
danielk19773b8a05f2007-03-19 17:44:26 +00001303 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drh07d183d2005-05-01 22:52:42 +00001304 assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] );
drhee696e22004-08-30 16:52:17 +00001305 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
1306 /* The parent page should never change unless the file is corrupt */
drh49285702005-09-17 15:20:26 +00001307 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001308 }
drh10617cd2004-05-14 15:27:27 +00001309 if( pPage->isInit ) return SQLITE_OK;
drhda200cc2004-05-09 11:51:38 +00001310 if( pPage->pParent==0 && pParent!=0 ){
1311 pPage->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +00001312 sqlite3PagerRef(pParent->pDbPage);
drh5e2f8b92001-05-28 00:41:15 +00001313 }
drhde647132004-05-07 17:57:49 +00001314 hdr = pPage->hdrOffset;
drha34b6762004-05-07 13:30:42 +00001315 data = pPage->aData;
drh271efa52004-05-30 19:19:05 +00001316 decodeFlags(pPage, data[hdr]);
drh43605152004-05-29 21:46:49 +00001317 pPage->nOverflow = 0;
drhc8629a12004-05-08 20:07:40 +00001318 pPage->idxShift = 0;
drh2e38c322004-09-03 18:38:44 +00001319 usableSize = pBt->usableSize;
drh43605152004-05-29 21:46:49 +00001320 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1321 top = get2byte(&data[hdr+5]);
1322 pPage->nCell = get2byte(&data[hdr+3]);
drh2e38c322004-09-03 18:38:44 +00001323 if( pPage->nCell>MX_CELL(pBt) ){
drhee696e22004-08-30 16:52:17 +00001324 /* To many cells for a single page. The page must be corrupt */
drh49285702005-09-17 15:20:26 +00001325 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001326 }
1327 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
1328 /* All pages must have at least one cell, except for root pages */
drh49285702005-09-17 15:20:26 +00001329 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001330 }
drh9e572e62004-04-23 23:43:10 +00001331
1332 /* Compute the total free space on the page */
drh9e572e62004-04-23 23:43:10 +00001333 pc = get2byte(&data[hdr+1]);
drh43605152004-05-29 21:46:49 +00001334 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
drh9e572e62004-04-23 23:43:10 +00001335 while( pc>0 ){
1336 int next, size;
drhee696e22004-08-30 16:52:17 +00001337 if( pc>usableSize-4 ){
1338 /* Free block is off the page */
drh49285702005-09-17 15:20:26 +00001339 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001340 }
drh9e572e62004-04-23 23:43:10 +00001341 next = get2byte(&data[pc]);
1342 size = get2byte(&data[pc+2]);
drhee696e22004-08-30 16:52:17 +00001343 if( next>0 && next<=pc+size+3 ){
1344 /* Free blocks must be in accending order */
drh49285702005-09-17 15:20:26 +00001345 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001346 }
drh3add3672004-05-15 00:29:24 +00001347 nFree += size;
drh9e572e62004-04-23 23:43:10 +00001348 pc = next;
1349 }
drh3add3672004-05-15 00:29:24 +00001350 pPage->nFree = nFree;
drhee696e22004-08-30 16:52:17 +00001351 if( nFree>=usableSize ){
1352 /* Free space cannot exceed total page size */
drh49285702005-09-17 15:20:26 +00001353 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001354 }
drh9e572e62004-04-23 23:43:10 +00001355
drhde647132004-05-07 17:57:49 +00001356 pPage->isInit = 1;
drh9e572e62004-04-23 23:43:10 +00001357 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001358}
1359
1360/*
drh8b2f49b2001-06-08 00:21:52 +00001361** Set up a raw page so that it looks like a database page holding
1362** no entries.
drhbd03cae2001-06-02 02:40:57 +00001363*/
drh9e572e62004-04-23 23:43:10 +00001364static void zeroPage(MemPage *pPage, int flags){
1365 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:01 +00001366 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00001367 int hdr = pPage->hdrOffset;
drh9e572e62004-04-23 23:43:10 +00001368 int first;
1369
danielk19773b8a05f2007-03-19 17:44:26 +00001370 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
drh07d183d2005-05-01 22:52:42 +00001371 assert( &data[pBt->pageSize] == (unsigned char*)pPage );
danielk19773b8a05f2007-03-19 17:44:26 +00001372 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drhb6f41482004-05-14 01:58:11 +00001373 memset(&data[hdr], 0, pBt->usableSize - hdr);
drh9e572e62004-04-23 23:43:10 +00001374 data[hdr] = flags;
drh43605152004-05-29 21:46:49 +00001375 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
1376 memset(&data[hdr+1], 0, 4);
1377 data[hdr+7] = 0;
1378 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001379 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001380 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001381 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001382 pPage->cellOffset = first;
1383 pPage->nOverflow = 0;
drhda200cc2004-05-09 11:51:38 +00001384 pPage->idxShift = 0;
drh43605152004-05-29 21:46:49 +00001385 pPage->nCell = 0;
drhda200cc2004-05-09 11:51:38 +00001386 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:57 +00001387}
1388
1389/*
drh3aac2dd2004-04-26 14:10:20 +00001390** Get a page from the pager. Initialize the MemPage.pBt and
1391** MemPage.aData elements if needed.
1392*/
drh0787db62007-03-04 13:15:27 +00001393static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage, int clrFlag){
drh3aac2dd2004-04-26 14:10:20 +00001394 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001395 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00001396 DbPage *pDbPage;
1397
1398 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, clrFlag);
drh3aac2dd2004-04-26 14:10:20 +00001399 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001400 pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1401 pPage->aData = sqlite3PagerGetData(pDbPage);
1402 pPage->pDbPage = pDbPage;
drh3aac2dd2004-04-26 14:10:20 +00001403 pPage->pBt = pBt;
1404 pPage->pgno = pgno;
drhde647132004-05-07 17:57:49 +00001405 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
drh3aac2dd2004-04-26 14:10:20 +00001406 *ppPage = pPage;
drh94440812007-03-06 11:42:19 +00001407 if( clrFlag ){
danielk19773b8a05f2007-03-19 17:44:26 +00001408 sqlite3PagerDontRollback(pPage->pDbPage);
drh94440812007-03-06 11:42:19 +00001409 }
drh3aac2dd2004-04-26 14:10:20 +00001410 return SQLITE_OK;
1411}
1412
1413/*
drhde647132004-05-07 17:57:49 +00001414** Get a page from the pager and initialize it. This routine
1415** is just a convenience wrapper around separate calls to
1416** getPage() and initPage().
1417*/
1418static int getAndInitPage(
danielk1977aef0bf62005-12-30 16:28:01 +00001419 BtShared *pBt, /* The database file */
drhde647132004-05-07 17:57:49 +00001420 Pgno pgno, /* Number of the page to get */
1421 MemPage **ppPage, /* Write the page pointer here */
1422 MemPage *pParent /* Parent of the page */
1423){
1424 int rc;
drhee696e22004-08-30 16:52:17 +00001425 if( pgno==0 ){
drh49285702005-09-17 15:20:26 +00001426 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001427 }
drh0787db62007-03-04 13:15:27 +00001428 rc = getPage(pBt, pgno, ppPage, 0);
drh10617cd2004-05-14 15:27:27 +00001429 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
drhde647132004-05-07 17:57:49 +00001430 rc = initPage(*ppPage, pParent);
1431 }
1432 return rc;
1433}
1434
1435/*
drh3aac2dd2004-04-26 14:10:20 +00001436** Release a MemPage. This should be called once for each prior
1437** call to getPage.
1438*/
drh4b70f112004-05-02 21:12:19 +00001439static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001440 if( pPage ){
1441 assert( pPage->aData );
1442 assert( pPage->pBt );
drh07d183d2005-05-01 22:52:42 +00001443 assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage );
danielk19773b8a05f2007-03-19 17:44:26 +00001444 sqlite3PagerUnref(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00001445 }
1446}
1447
1448/*
drh72f82862001-05-24 21:06:34 +00001449** This routine is called when the reference count for a page
1450** reaches zero. We need to unref the pParent pointer when that
1451** happens.
1452*/
danielk19773b8a05f2007-03-19 17:44:26 +00001453static void pageDestructor(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001454 MemPage *pPage;
1455 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001456 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drh72f82862001-05-24 21:06:34 +00001457 if( pPage->pParent ){
1458 MemPage *pParent = pPage->pParent;
1459 pPage->pParent = 0;
drha34b6762004-05-07 13:30:42 +00001460 releasePage(pParent);
drh72f82862001-05-24 21:06:34 +00001461 }
drh3aac2dd2004-04-26 14:10:20 +00001462 pPage->isInit = 0;
drh72f82862001-05-24 21:06:34 +00001463}
1464
1465/*
drha6abd042004-06-09 17:37:22 +00001466** During a rollback, when the pager reloads information into the cache
1467** so that the cache is restored to its original state at the start of
1468** the transaction, for each page restored this routine is called.
1469**
1470** This routine needs to reset the extra data section at the end of the
1471** page to agree with the restored data.
1472*/
danielk19773b8a05f2007-03-19 17:44:26 +00001473static void pageReinit(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001474 MemPage *pPage;
1475 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001476 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drha6abd042004-06-09 17:37:22 +00001477 if( pPage->isInit ){
1478 pPage->isInit = 0;
1479 initPage(pPage, pPage->pParent);
1480 }
1481}
1482
1483/*
drhad3e0102004-09-03 23:32:18 +00001484** Open a database file.
1485**
drh382c0242001-10-06 16:33:02 +00001486** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001487** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001488** database file will be deleted when sqlite3BtreeClose() is called.
drha059ad02001-04-17 20:09:11 +00001489*/
drh23e11ca2004-05-04 17:27:28 +00001490int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001491 const char *zFilename, /* Name of the file containing the BTree database */
danielk1977aef0bf62005-12-30 16:28:01 +00001492 sqlite3 *pSqlite, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:20 +00001493 Btree **ppBtree, /* Pointer to new Btree object written here */
drh90f5ecb2004-07-22 01:19:35 +00001494 int flags /* Options */
drh6019e162001-07-02 17:51:45 +00001495){
danielk1977aef0bf62005-12-30 16:28:01 +00001496 BtShared *pBt; /* Shared part of btree structure */
1497 Btree *p; /* Handle to return */
drha34b6762004-05-07 13:30:42 +00001498 int rc;
drh90f5ecb2004-07-22 01:19:35 +00001499 int nReserve;
1500 unsigned char zDbHeader[100];
drh6f7adc82006-01-11 21:41:20 +00001501#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1502 const ThreadData *pTsdro;
danielk1977da184232006-01-05 11:34:32 +00001503#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001504
1505 /* Set the variable isMemdb to true for an in-memory database, or
1506 ** false for a file-based database. This symbol is only required if
1507 ** either of the shared-data or autovacuum features are compiled
1508 ** into the library.
1509 */
1510#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1511 #ifdef SQLITE_OMIT_MEMORYDB
drh980b1a72006-08-16 16:42:48 +00001512 const int isMemdb = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001513 #else
drh980b1a72006-08-16 16:42:48 +00001514 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
danielk1977aef0bf62005-12-30 16:28:01 +00001515 #endif
1516#endif
1517
1518 p = sqliteMalloc(sizeof(Btree));
1519 if( !p ){
1520 return SQLITE_NOMEM;
1521 }
1522 p->inTrans = TRANS_NONE;
1523 p->pSqlite = pSqlite;
1524
1525 /* Try to find an existing Btree structure opened on zFilename. */
drh198bf392006-01-06 21:52:49 +00001526#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drh6f7adc82006-01-11 21:41:20 +00001527 pTsdro = sqlite3ThreadDataReadOnly();
1528 if( pTsdro->useSharedData && zFilename && !isMemdb ){
drh66560ad2006-01-06 14:32:19 +00001529 char *zFullPathname = sqlite3OsFullPathname(zFilename);
danielk1977aef0bf62005-12-30 16:28:01 +00001530 if( !zFullPathname ){
1531 sqliteFree(p);
1532 return SQLITE_NOMEM;
1533 }
drh6f7adc82006-01-11 21:41:20 +00001534 for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){
danielk1977b82e7ed2006-01-11 14:09:31 +00001535 assert( pBt->nRef>0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001536 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){
danielk1977aef0bf62005-12-30 16:28:01 +00001537 p->pBt = pBt;
1538 *ppBtree = p;
1539 pBt->nRef++;
1540 sqliteFree(zFullPathname);
1541 return SQLITE_OK;
1542 }
1543 }
1544 sqliteFree(zFullPathname);
1545 }
1546#endif
drha059ad02001-04-17 20:09:11 +00001547
drhd62d3d02003-01-24 12:14:20 +00001548 /*
1549 ** The following asserts make sure that structures used by the btree are
1550 ** the right size. This is to guard against size changes that result
1551 ** when compiling on a different architecture.
1552 */
drh9b8f4472006-04-04 01:54:55 +00001553 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1554 assert( sizeof(u64)==8 || sizeof(u64)==4 );
drhd62d3d02003-01-24 12:14:20 +00001555 assert( sizeof(u32)==4 );
1556 assert( sizeof(u16)==2 );
1557 assert( sizeof(Pgno)==4 );
drhd62d3d02003-01-24 12:14:20 +00001558
drha059ad02001-04-17 20:09:11 +00001559 pBt = sqliteMalloc( sizeof(*pBt) );
1560 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +00001561 *ppBtree = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001562 sqliteFree(p);
drha059ad02001-04-17 20:09:11 +00001563 return SQLITE_NOMEM;
1564 }
danielk19773b8a05f2007-03-19 17:44:26 +00001565 rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags);
drh551b7732006-11-06 21:20:25 +00001566 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00001567 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
drh551b7732006-11-06 21:20:25 +00001568 }
drha059ad02001-04-17 20:09:11 +00001569 if( rc!=SQLITE_OK ){
drh551b7732006-11-06 21:20:25 +00001570 if( pBt->pPager ){
danielk19773b8a05f2007-03-19 17:44:26 +00001571 sqlite3PagerClose(pBt->pPager);
drh551b7732006-11-06 21:20:25 +00001572 }
drha059ad02001-04-17 20:09:11 +00001573 sqliteFree(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00001574 sqliteFree(p);
drha059ad02001-04-17 20:09:11 +00001575 *ppBtree = 0;
1576 return rc;
1577 }
danielk1977aef0bf62005-12-30 16:28:01 +00001578 p->pBt = pBt;
1579
danielk19773b8a05f2007-03-19 17:44:26 +00001580 sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
1581 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
drha059ad02001-04-17 20:09:11 +00001582 pBt->pCursor = 0;
drha34b6762004-05-07 13:30:42 +00001583 pBt->pPage1 = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00001584 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
drh90f5ecb2004-07-22 01:19:35 +00001585 pBt->pageSize = get2byte(&zDbHeader[16]);
drh07d183d2005-05-01 22:52:42 +00001586 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1587 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
drh90f5ecb2004-07-22 01:19:35 +00001588 pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
1589 pBt->maxEmbedFrac = 64; /* 25% */
1590 pBt->minEmbedFrac = 32; /* 12.5% */
1591 pBt->minLeafFrac = 32; /* 12.5% */
drheee46cf2004-11-06 00:02:48 +00001592#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197703aded42004-11-22 05:26:27 +00001593 /* If the magic name ":memory:" will create an in-memory database, then
1594 ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM
1595 ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined,
1596 ** then ":memory:" is just a regular file-name. Respect the auto-vacuum
1597 ** default in this case.
1598 */
danielk1977aef0bf62005-12-30 16:28:01 +00001599 if( zFilename && !isMemdb ){
danielk1977951af802004-11-05 15:45:09 +00001600 pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM;
1601 }
drheee46cf2004-11-06 00:02:48 +00001602#endif
drh90f5ecb2004-07-22 01:19:35 +00001603 nReserve = 0;
1604 }else{
1605 nReserve = zDbHeader[20];
1606 pBt->maxEmbedFrac = zDbHeader[21];
1607 pBt->minEmbedFrac = zDbHeader[22];
1608 pBt->minLeafFrac = zDbHeader[23];
1609 pBt->pageSizeFixed = 1;
danielk1977951af802004-11-05 15:45:09 +00001610#ifndef SQLITE_OMIT_AUTOVACUUM
1611 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1612#endif
drh90f5ecb2004-07-22 01:19:35 +00001613 }
1614 pBt->usableSize = pBt->pageSize - nReserve;
drh07d183d2005-05-01 22:52:42 +00001615 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
danielk19773b8a05f2007-03-19 17:44:26 +00001616 sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize);
danielk1977aef0bf62005-12-30 16:28:01 +00001617
drhcfed7bc2006-03-13 14:28:05 +00001618#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
danielk197754f01982006-01-18 15:25:17 +00001619 /* Add the new btree to the linked list starting at ThreadData.pBtree.
1620 ** There is no chance that a malloc() may fail inside of the
1621 ** sqlite3ThreadData() call, as the ThreadData structure must have already
1622 ** been allocated for pTsdro->useSharedData to be non-zero.
1623 */
drh6f7adc82006-01-11 21:41:20 +00001624 if( pTsdro->useSharedData && zFilename && !isMemdb ){
1625 pBt->pNext = pTsdro->pBtree;
1626 sqlite3ThreadData()->pBtree = pBt;
danielk1977aef0bf62005-12-30 16:28:01 +00001627 }
danielk1977aef0bf62005-12-30 16:28:01 +00001628#endif
danielk1977da184232006-01-05 11:34:32 +00001629 pBt->nRef = 1;
danielk1977aef0bf62005-12-30 16:28:01 +00001630 *ppBtree = p;
drha059ad02001-04-17 20:09:11 +00001631 return SQLITE_OK;
1632}
1633
1634/*
1635** Close an open database and invalidate all cursors.
1636*/
danielk1977aef0bf62005-12-30 16:28:01 +00001637int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00001638 BtShared *pBt = p->pBt;
1639 BtCursor *pCur;
1640
danielk1977da184232006-01-05 11:34:32 +00001641#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00001642 ThreadData *pTsd;
danielk1977da184232006-01-05 11:34:32 +00001643#endif
1644
danielk1977aef0bf62005-12-30 16:28:01 +00001645 /* Close all cursors opened via this handle. */
1646 pCur = pBt->pCursor;
1647 while( pCur ){
1648 BtCursor *pTmp = pCur;
1649 pCur = pCur->pNext;
1650 if( pTmp->pBtree==p ){
1651 sqlite3BtreeCloseCursor(pTmp);
1652 }
drha059ad02001-04-17 20:09:11 +00001653 }
danielk1977aef0bf62005-12-30 16:28:01 +00001654
danielk19778d34dfd2006-01-24 16:37:57 +00001655 /* Rollback any active transaction and free the handle structure.
1656 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1657 ** this handle.
1658 */
danielk1977b597f742006-01-15 11:39:18 +00001659 sqlite3BtreeRollback(p);
danielk1977aef0bf62005-12-30 16:28:01 +00001660 sqliteFree(p);
1661
1662#ifndef SQLITE_OMIT_SHARED_CACHE
1663 /* If there are still other outstanding references to the shared-btree
1664 ** structure, return now. The remainder of this procedure cleans
1665 ** up the shared-btree.
1666 */
1667 assert( pBt->nRef>0 );
1668 pBt->nRef--;
1669 if( pBt->nRef ){
1670 return SQLITE_OK;
1671 }
1672
danielk197754f01982006-01-18 15:25:17 +00001673 /* Remove the shared-btree from the thread wide list. Call
1674 ** ThreadDataReadOnly() and then cast away the const property of the
1675 ** pointer to avoid allocating thread data if it is not really required.
1676 */
1677 pTsd = (ThreadData *)sqlite3ThreadDataReadOnly();
danielk1977aef0bf62005-12-30 16:28:01 +00001678 if( pTsd->pBtree==pBt ){
danielk197754f01982006-01-18 15:25:17 +00001679 assert( pTsd==sqlite3ThreadData() );
danielk1977aef0bf62005-12-30 16:28:01 +00001680 pTsd->pBtree = pBt->pNext;
1681 }else{
1682 BtShared *pPrev;
drh74161702006-02-24 02:53:49 +00001683 for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){}
danielk1977aef0bf62005-12-30 16:28:01 +00001684 if( pPrev ){
danielk197754f01982006-01-18 15:25:17 +00001685 assert( pTsd==sqlite3ThreadData() );
danielk1977aef0bf62005-12-30 16:28:01 +00001686 pPrev->pNext = pBt->pNext;
1687 }
1688 }
1689#endif
1690
1691 /* Close the pager and free the shared-btree structure */
1692 assert( !pBt->pCursor );
danielk19773b8a05f2007-03-19 17:44:26 +00001693 sqlite3PagerClose(pBt->pPager);
danielk1977da184232006-01-05 11:34:32 +00001694 if( pBt->xFreeSchema && pBt->pSchema ){
1695 pBt->xFreeSchema(pBt->pSchema);
1696 }
danielk1977de0fe3e2006-01-06 06:33:12 +00001697 sqliteFree(pBt->pSchema);
drha059ad02001-04-17 20:09:11 +00001698 sqliteFree(pBt);
1699 return SQLITE_OK;
1700}
1701
1702/*
drh90f5ecb2004-07-22 01:19:35 +00001703** Change the busy handler callback function.
1704*/
danielk1977aef0bf62005-12-30 16:28:01 +00001705int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
1706 BtShared *pBt = p->pBt;
drhb8ef32c2005-03-14 02:01:49 +00001707 pBt->pBusyHandler = pHandler;
danielk19773b8a05f2007-03-19 17:44:26 +00001708 sqlite3PagerSetBusyhandler(pBt->pPager, pHandler);
drh90f5ecb2004-07-22 01:19:35 +00001709 return SQLITE_OK;
1710}
1711
1712/*
drhda47d772002-12-02 04:25:19 +00001713** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001714**
1715** The maximum number of cache pages is set to the absolute
1716** value of mxPage. If mxPage is negative, the pager will
1717** operate asynchronously - it will not stop to do fsync()s
1718** to insure data is written to the disk surface before
1719** continuing. Transactions still work if synchronous is off,
1720** and the database cannot be corrupted if this program
1721** crashes. But if the operating system crashes or there is
1722** an abrupt power failure when synchronous is off, the database
1723** could be left in an inconsistent and unrecoverable state.
1724** Synchronous is on by default so database corruption is not
1725** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001726*/
danielk1977aef0bf62005-12-30 16:28:01 +00001727int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1728 BtShared *pBt = p->pBt;
danielk19773b8a05f2007-03-19 17:44:26 +00001729 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001730 return SQLITE_OK;
1731}
1732
1733/*
drh973b6e32003-02-12 14:09:42 +00001734** Change the way data is synced to disk in order to increase or decrease
1735** how well the database resists damage due to OS crashes and power
1736** failures. Level 1 is the same as asynchronous (no syncs() occur and
1737** there is a high probability of damage) Level 2 is the default. There
1738** is a very low but non-zero probability of damage. Level 3 reduces the
1739** probability of damage to near zero but with a write performance reduction.
1740*/
danielk197793758c82005-01-21 08:13:14 +00001741#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001742int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
danielk1977aef0bf62005-12-30 16:28:01 +00001743 BtShared *pBt = p->pBt;
danielk19773b8a05f2007-03-19 17:44:26 +00001744 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
drh973b6e32003-02-12 14:09:42 +00001745 return SQLITE_OK;
1746}
danielk197793758c82005-01-21 08:13:14 +00001747#endif
drh973b6e32003-02-12 14:09:42 +00001748
drh2c8997b2005-08-27 16:36:48 +00001749/*
1750** Return TRUE if the given btree is set to safety level 1. In other
1751** words, return TRUE if no sync() occurs on the disk files.
1752*/
danielk1977aef0bf62005-12-30 16:28:01 +00001753int sqlite3BtreeSyncDisabled(Btree *p){
1754 BtShared *pBt = p->pBt;
drh2c8997b2005-08-27 16:36:48 +00001755 assert( pBt && pBt->pPager );
danielk19773b8a05f2007-03-19 17:44:26 +00001756 return sqlite3PagerNosync(pBt->pPager);
drh2c8997b2005-08-27 16:36:48 +00001757}
1758
danielk1977576ec6b2005-01-21 11:55:25 +00001759#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001760/*
drh90f5ecb2004-07-22 01:19:35 +00001761** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001762**
1763** The page size must be a power of 2 between 512 and 65536. If the page
1764** size supplied does not meet this constraint then the page size is not
1765** changed.
1766**
1767** Page sizes are constrained to be a power of two so that the region
1768** of the database file used for locking (beginning at PENDING_BYTE,
1769** the first byte past the 1GB boundary, 0x40000000) needs to occur
1770** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001771**
1772** If parameter nReserve is less than zero, then the number of reserved
1773** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001774*/
danielk1977aef0bf62005-12-30 16:28:01 +00001775int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
1776 BtShared *pBt = p->pBt;
drh90f5ecb2004-07-22 01:19:35 +00001777 if( pBt->pageSizeFixed ){
1778 return SQLITE_READONLY;
1779 }
1780 if( nReserve<0 ){
1781 nReserve = pBt->pageSize - pBt->usableSize;
1782 }
drh06f50212004-11-02 14:24:33 +00001783 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1784 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:42 +00001785 assert( (pageSize & 7)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00001786 assert( !pBt->pPage1 && !pBt->pCursor );
danielk19773b8a05f2007-03-19 17:44:26 +00001787 pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001788 }
1789 pBt->usableSize = pBt->pageSize - nReserve;
1790 return SQLITE_OK;
1791}
1792
1793/*
1794** Return the currently defined page size
1795*/
danielk1977aef0bf62005-12-30 16:28:01 +00001796int sqlite3BtreeGetPageSize(Btree *p){
1797 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001798}
danielk1977aef0bf62005-12-30 16:28:01 +00001799int sqlite3BtreeGetReserve(Btree *p){
1800 return p->pBt->pageSize - p->pBt->usableSize;
drh2011d5f2004-07-22 02:40:37 +00001801}
danielk1977576ec6b2005-01-21 11:55:25 +00001802#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001803
1804/*
danielk1977951af802004-11-05 15:45:09 +00001805** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1806** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1807** is disabled. The default value for the auto-vacuum property is
1808** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1809*/
danielk1977aef0bf62005-12-30 16:28:01 +00001810int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
1811 BtShared *pBt = p->pBt;;
danielk1977951af802004-11-05 15:45:09 +00001812#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001813 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001814#else
1815 if( pBt->pageSizeFixed ){
1816 return SQLITE_READONLY;
1817 }
1818 pBt->autoVacuum = (autoVacuum?1:0);
1819 return SQLITE_OK;
1820#endif
1821}
1822
1823/*
1824** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1825** enabled 1 is returned. Otherwise 0.
1826*/
danielk1977aef0bf62005-12-30 16:28:01 +00001827int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:09 +00001828#ifdef SQLITE_OMIT_AUTOVACUUM
1829 return 0;
1830#else
danielk1977aef0bf62005-12-30 16:28:01 +00001831 return p->pBt->autoVacuum;
danielk1977951af802004-11-05 15:45:09 +00001832#endif
1833}
1834
1835
1836/*
drha34b6762004-05-07 13:30:42 +00001837** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001838** also acquire a readlock on that file.
1839**
1840** SQLITE_OK is returned on success. If the file is not a
1841** well-formed database file, then SQLITE_CORRUPT is returned.
1842** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
1843** is returned if we run out of memory. SQLITE_PROTOCOL is returned
1844** if there is a locking protocol violation.
1845*/
danielk1977aef0bf62005-12-30 16:28:01 +00001846static int lockBtree(BtShared *pBt){
drh07d183d2005-05-01 22:52:42 +00001847 int rc, pageSize;
drh3aac2dd2004-04-26 14:10:20 +00001848 MemPage *pPage1;
drha34b6762004-05-07 13:30:42 +00001849 if( pBt->pPage1 ) return SQLITE_OK;
drh0787db62007-03-04 13:15:27 +00001850 rc = getPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:10 +00001851 if( rc!=SQLITE_OK ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00001852
drh306dc212001-05-21 13:45:10 +00001853
1854 /* Do some checking to help insure the file we opened really is
1855 ** a valid database file.
1856 */
drhb6f41482004-05-14 01:58:11 +00001857 rc = SQLITE_NOTADB;
danielk19773b8a05f2007-03-19 17:44:26 +00001858 if( sqlite3PagerPagecount(pBt->pPager)>0 ){
drhb6f41482004-05-14 01:58:11 +00001859 u8 *page1 = pPage1->aData;
1860 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001861 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001862 }
drhb6f41482004-05-14 01:58:11 +00001863 if( page1[18]>1 || page1[19]>1 ){
1864 goto page1_init_failed;
1865 }
drh07d183d2005-05-01 22:52:42 +00001866 pageSize = get2byte(&page1[16]);
1867 if( ((pageSize-1)&pageSize)!=0 ){
1868 goto page1_init_failed;
1869 }
1870 assert( (pageSize & 7)==0 );
1871 pBt->pageSize = pageSize;
1872 pBt->usableSize = pageSize - page1[20];
drhb6f41482004-05-14 01:58:11 +00001873 if( pBt->usableSize<500 ){
1874 goto page1_init_failed;
1875 }
1876 pBt->maxEmbedFrac = page1[21];
1877 pBt->minEmbedFrac = page1[22];
1878 pBt->minLeafFrac = page1[23];
drh057cd3a2005-02-15 16:23:02 +00001879#ifndef SQLITE_OMIT_AUTOVACUUM
1880 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
1881#endif
drh306dc212001-05-21 13:45:10 +00001882 }
drhb6f41482004-05-14 01:58:11 +00001883
1884 /* maxLocal is the maximum amount of payload to store locally for
1885 ** a cell. Make sure it is small enough so that at least minFanout
1886 ** cells can will fit on one page. We assume a 10-byte page header.
1887 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001888 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001889 ** 4-byte child pointer
1890 ** 9-byte nKey value
1891 ** 4-byte nData value
1892 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001893 ** So a cell consists of a 2-byte poiner, a header which is as much as
1894 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1895 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001896 */
drh43605152004-05-29 21:46:49 +00001897 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1898 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1899 pBt->maxLeaf = pBt->usableSize - 35;
1900 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
drhb6f41482004-05-14 01:58:11 +00001901 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1902 goto page1_init_failed;
1903 }
drh2e38c322004-09-03 18:38:44 +00001904 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001905 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001906 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001907
drh72f82862001-05-24 21:06:34 +00001908page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001909 releasePage(pPage1);
1910 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001911 return rc;
drh306dc212001-05-21 13:45:10 +00001912}
1913
1914/*
drhb8ef32c2005-03-14 02:01:49 +00001915** This routine works like lockBtree() except that it also invokes the
1916** busy callback if there is lock contention.
1917*/
danielk1977aef0bf62005-12-30 16:28:01 +00001918static int lockBtreeWithRetry(Btree *pRef){
drhb8ef32c2005-03-14 02:01:49 +00001919 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00001920 if( pRef->inTrans==TRANS_NONE ){
1921 u8 inTransaction = pRef->pBt->inTransaction;
1922 btreeIntegrity(pRef);
1923 rc = sqlite3BtreeBeginTrans(pRef, 0);
1924 pRef->pBt->inTransaction = inTransaction;
1925 pRef->inTrans = TRANS_NONE;
1926 if( rc==SQLITE_OK ){
1927 pRef->pBt->nTransaction--;
1928 }
1929 btreeIntegrity(pRef);
drhb8ef32c2005-03-14 02:01:49 +00001930 }
1931 return rc;
1932}
1933
1934
1935/*
drhb8ca3072001-12-05 00:21:20 +00001936** If there are no outstanding cursors and we are not in the middle
1937** of a transaction but there is a read lock on the database, then
1938** this routine unrefs the first page of the database file which
1939** has the effect of releasing the read lock.
1940**
1941** If there are any outstanding cursors, this routine is a no-op.
1942**
1943** If there is a transaction in progress, this routine is a no-op.
1944*/
danielk1977aef0bf62005-12-30 16:28:01 +00001945static void unlockBtreeIfUnused(BtShared *pBt){
1946 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00001947 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
drh24c9a2e2007-01-05 02:00:47 +00001948 if( pBt->pPage1->aData==0 ){
1949 MemPage *pPage = pBt->pPage1;
1950 pPage->aData = &((u8*)pPage)[-pBt->pageSize];
1951 pPage->pBt = pBt;
1952 pPage->pgno = 1;
1953 }
1954 releasePage(pBt->pPage1);
drh51c6d962004-06-06 00:42:25 +00001955 }
drh3aac2dd2004-04-26 14:10:20 +00001956 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001957 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001958 }
1959}
1960
1961/*
drh9e572e62004-04-23 23:43:10 +00001962** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001963** file.
drh8b2f49b2001-06-08 00:21:52 +00001964*/
danielk1977aef0bf62005-12-30 16:28:01 +00001965static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:10 +00001966 MemPage *pP1;
1967 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001968 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001969 if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001970 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001971 assert( pP1!=0 );
1972 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001973 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00001974 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001975 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1976 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001977 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001978 data[18] = 1;
1979 data[19] = 1;
drhb6f41482004-05-14 01:58:11 +00001980 data[20] = pBt->pageSize - pBt->usableSize;
1981 data[21] = pBt->maxEmbedFrac;
1982 data[22] = pBt->minEmbedFrac;
1983 data[23] = pBt->minLeafFrac;
1984 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00001985 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00001986 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00001987#ifndef SQLITE_OMIT_AUTOVACUUM
1988 if( pBt->autoVacuum ){
1989 put4byte(&data[36 + 4*4], 1);
1990 }
1991#endif
drh8b2f49b2001-06-08 00:21:52 +00001992 return SQLITE_OK;
1993}
1994
1995/*
danielk1977ee5741e2004-05-31 10:01:34 +00001996** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00001997** is started if the second argument is nonzero, otherwise a read-
1998** transaction. If the second argument is 2 or more and exclusive
1999** transaction is started, meaning that no other process is allowed
2000** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:49 +00002001** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:42 +00002002** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00002003**
danielk1977ee5741e2004-05-31 10:01:34 +00002004** A write-transaction must be started before attempting any
2005** changes to the database. None of the following routines
2006** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00002007**
drh23e11ca2004-05-04 17:27:28 +00002008** sqlite3BtreeCreateTable()
2009** sqlite3BtreeCreateIndex()
2010** sqlite3BtreeClearTable()
2011** sqlite3BtreeDropTable()
2012** sqlite3BtreeInsert()
2013** sqlite3BtreeDelete()
2014** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00002015**
drhb8ef32c2005-03-14 02:01:49 +00002016** If an initial attempt to acquire the lock fails because of lock contention
2017** and the database was previously unlocked, then invoke the busy handler
2018** if there is one. But if there was previously a read-lock, do not
2019** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
2020** returned when there is already a read-lock in order to avoid a deadlock.
2021**
2022** Suppose there are two processes A and B. A has a read lock and B has
2023** a reserved lock. B tries to promote to exclusive but is blocked because
2024** of A's read lock. A tries to promote to reserved but is blocked by B.
2025** One or the other of the two processes must give way or there can be
2026** no progress. By returning SQLITE_BUSY and not invoking the busy callback
2027** when A already has a read lock, we encourage A to give up and let B
2028** proceed.
drha059ad02001-04-17 20:09:11 +00002029*/
danielk1977aef0bf62005-12-30 16:28:01 +00002030int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
2031 BtShared *pBt = p->pBt;
danielk1977ee5741e2004-05-31 10:01:34 +00002032 int rc = SQLITE_OK;
2033
danielk1977aef0bf62005-12-30 16:28:01 +00002034 btreeIntegrity(p);
2035
danielk1977ee5741e2004-05-31 10:01:34 +00002036 /* If the btree is already in a write-transaction, or it
2037 ** is already in a read-transaction and a read-transaction
2038 ** is requested, this is a no-op.
2039 */
danielk1977aef0bf62005-12-30 16:28:01 +00002040 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
danielk1977ee5741e2004-05-31 10:01:34 +00002041 return SQLITE_OK;
2042 }
drhb8ef32c2005-03-14 02:01:49 +00002043
2044 /* Write transactions are not possible on a read-only database */
danielk1977ee5741e2004-05-31 10:01:34 +00002045 if( pBt->readOnly && wrflag ){
2046 return SQLITE_READONLY;
2047 }
2048
danielk1977aef0bf62005-12-30 16:28:01 +00002049 /* If another database handle has already opened a write transaction
2050 ** on this shared-btree structure and a second write transaction is
2051 ** requested, return SQLITE_BUSY.
2052 */
2053 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
2054 return SQLITE_BUSY;
2055 }
2056
drhb8ef32c2005-03-14 02:01:49 +00002057 do {
2058 if( pBt->pPage1==0 ){
2059 rc = lockBtree(pBt);
drh8c42ca92001-06-22 19:15:00 +00002060 }
drhb8ef32c2005-03-14 02:01:49 +00002061
2062 if( rc==SQLITE_OK && wrflag ){
danielk19773b8a05f2007-03-19 17:44:26 +00002063 rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
drhb8ef32c2005-03-14 02:01:49 +00002064 if( rc==SQLITE_OK ){
2065 rc = newDatabase(pBt);
2066 }
2067 }
2068
2069 if( rc==SQLITE_OK ){
drhb8ef32c2005-03-14 02:01:49 +00002070 if( wrflag ) pBt->inStmt = 0;
2071 }else{
2072 unlockBtreeIfUnused(pBt);
2073 }
danielk1977aef0bf62005-12-30 16:28:01 +00002074 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
drha4afb652005-07-09 02:16:02 +00002075 sqlite3InvokeBusyHandler(pBt->pBusyHandler) );
danielk1977aef0bf62005-12-30 16:28:01 +00002076
2077 if( rc==SQLITE_OK ){
2078 if( p->inTrans==TRANS_NONE ){
2079 pBt->nTransaction++;
2080 }
2081 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
2082 if( p->inTrans>pBt->inTransaction ){
2083 pBt->inTransaction = p->inTrans;
2084 }
2085 }
2086
2087 btreeIntegrity(p);
drhb8ca3072001-12-05 00:21:20 +00002088 return rc;
drha059ad02001-04-17 20:09:11 +00002089}
2090
danielk1977687566d2004-11-02 12:56:41 +00002091#ifndef SQLITE_OMIT_AUTOVACUUM
2092
2093/*
2094** Set the pointer-map entries for all children of page pPage. Also, if
2095** pPage contains cells that point to overflow pages, set the pointer
2096** map entries for the overflow pages as well.
2097*/
2098static int setChildPtrmaps(MemPage *pPage){
2099 int i; /* Counter variable */
2100 int nCell; /* Number of cells in page pPage */
2101 int rc = SQLITE_OK; /* Return code */
danielk1977aef0bf62005-12-30 16:28:01 +00002102 BtShared *pBt = pPage->pBt;
danielk1977687566d2004-11-02 12:56:41 +00002103 int isInitOrig = pPage->isInit;
2104 Pgno pgno = pPage->pgno;
2105
2106 initPage(pPage, 0);
2107 nCell = pPage->nCell;
2108
2109 for(i=0; i<nCell; i++){
danielk1977687566d2004-11-02 12:56:41 +00002110 u8 *pCell = findCell(pPage, i);
2111
danielk197726836652005-01-17 01:33:13 +00002112 rc = ptrmapPutOvflPtr(pPage, pCell);
2113 if( rc!=SQLITE_OK ){
2114 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002115 }
danielk197726836652005-01-17 01:33:13 +00002116
danielk1977687566d2004-11-02 12:56:41 +00002117 if( !pPage->leaf ){
2118 Pgno childPgno = get4byte(pCell);
2119 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2120 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
2121 }
2122 }
2123
2124 if( !pPage->leaf ){
2125 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2126 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2127 }
2128
2129set_child_ptrmaps_out:
2130 pPage->isInit = isInitOrig;
2131 return rc;
2132}
2133
2134/*
2135** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
2136** page, is a pointer to page iFrom. Modify this pointer so that it points to
2137** iTo. Parameter eType describes the type of pointer to be modified, as
2138** follows:
2139**
2140** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2141** page of pPage.
2142**
2143** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2144** page pointed to by one of the cells on pPage.
2145**
2146** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2147** overflow page in the list.
2148*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00002149static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
danielk1977687566d2004-11-02 12:56:41 +00002150 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00002151 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00002152 if( get4byte(pPage->aData)!=iFrom ){
drh49285702005-09-17 15:20:26 +00002153 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002154 }
danielk1977f78fc082004-11-02 14:40:32 +00002155 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00002156 }else{
2157 int isInitOrig = pPage->isInit;
2158 int i;
2159 int nCell;
2160
2161 initPage(pPage, 0);
2162 nCell = pPage->nCell;
2163
danielk1977687566d2004-11-02 12:56:41 +00002164 for(i=0; i<nCell; i++){
2165 u8 *pCell = findCell(pPage, i);
2166 if( eType==PTRMAP_OVERFLOW1 ){
2167 CellInfo info;
2168 parseCellPtr(pPage, pCell, &info);
2169 if( info.iOverflow ){
2170 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
2171 put4byte(&pCell[info.iOverflow], iTo);
2172 break;
2173 }
2174 }
2175 }else{
2176 if( get4byte(pCell)==iFrom ){
2177 put4byte(pCell, iTo);
2178 break;
2179 }
2180 }
2181 }
2182
2183 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00002184 if( eType!=PTRMAP_BTREE ||
2185 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
drh49285702005-09-17 15:20:26 +00002186 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002187 }
danielk1977687566d2004-11-02 12:56:41 +00002188 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2189 }
2190
2191 pPage->isInit = isInitOrig;
2192 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002193 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002194}
2195
danielk1977003ba062004-11-04 02:57:33 +00002196
danielk19777701e812005-01-10 12:59:51 +00002197/*
2198** Move the open database page pDbPage to location iFreePage in the
2199** database. The pDbPage reference remains valid.
2200*/
danielk1977003ba062004-11-04 02:57:33 +00002201static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:01 +00002202 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:51 +00002203 MemPage *pDbPage, /* Open page to move */
2204 u8 eType, /* Pointer map 'type' entry for pDbPage */
2205 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
2206 Pgno iFreePage /* The location to move pDbPage to */
danielk1977003ba062004-11-04 02:57:33 +00002207){
2208 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2209 Pgno iDbPage = pDbPage->pgno;
2210 Pager *pPager = pBt->pPager;
2211 int rc;
2212
danielk1977a0bf2652004-11-04 14:30:04 +00002213 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2214 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
danielk1977003ba062004-11-04 02:57:33 +00002215
2216 /* Move page iDbPage from it's current location to page number iFreePage */
2217 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2218 iDbPage, iFreePage, iPtrPage, eType));
danielk19773b8a05f2007-03-19 17:44:26 +00002219 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
danielk1977003ba062004-11-04 02:57:33 +00002220 if( rc!=SQLITE_OK ){
2221 return rc;
2222 }
2223 pDbPage->pgno = iFreePage;
2224
2225 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2226 ** that point to overflow pages. The pointer map entries for all these
2227 ** pages need to be changed.
2228 **
2229 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2230 ** pointer to a subsequent overflow page. If this is the case, then
2231 ** the pointer map needs to be updated for the subsequent overflow page.
2232 */
danielk1977a0bf2652004-11-04 14:30:04 +00002233 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00002234 rc = setChildPtrmaps(pDbPage);
2235 if( rc!=SQLITE_OK ){
2236 return rc;
2237 }
2238 }else{
2239 Pgno nextOvfl = get4byte(pDbPage->aData);
2240 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00002241 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2242 if( rc!=SQLITE_OK ){
2243 return rc;
2244 }
2245 }
2246 }
2247
2248 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2249 ** that it points at iFreePage. Also fix the pointer map entry for
2250 ** iPtrPage.
2251 */
danielk1977a0bf2652004-11-04 14:30:04 +00002252 if( eType!=PTRMAP_ROOTPAGE ){
drh0787db62007-03-04 13:15:27 +00002253 rc = getPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00002254 if( rc!=SQLITE_OK ){
2255 return rc;
2256 }
danielk19773b8a05f2007-03-19 17:44:26 +00002257 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:04 +00002258 if( rc!=SQLITE_OK ){
2259 releasePage(pPtrPage);
2260 return rc;
2261 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002262 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00002263 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00002264 if( rc==SQLITE_OK ){
2265 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2266 }
danielk1977003ba062004-11-04 02:57:33 +00002267 }
danielk1977003ba062004-11-04 02:57:33 +00002268 return rc;
2269}
2270
danielk1977687566d2004-11-02 12:56:41 +00002271/* Forward declaration required by autoVacuumCommit(). */
drh4f0c5872007-03-26 22:05:01 +00002272static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00002273
2274/*
danielk19773b8a05f2007-03-19 17:44:26 +00002275** This routine is called prior to sqlite3PagerCommit when a transaction
danielk1977687566d2004-11-02 12:56:41 +00002276** is commited for an auto-vacuum database.
2277*/
danielk1977aef0bf62005-12-30 16:28:01 +00002278static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){
danielk1977687566d2004-11-02 12:56:41 +00002279 Pager *pPager = pBt->pPager;
danielk1977e501b892006-01-09 06:29:47 +00002280 Pgno nFreeList; /* Number of pages remaining on the free-list. */
2281 int nPtrMap; /* Number of pointer-map pages deallocated */
2282 Pgno origSize; /* Pages in the database file */
2283 Pgno finSize; /* Pages in the database file after truncation */
2284 int rc; /* Return code */
danielk1977687566d2004-11-02 12:56:41 +00002285 u8 eType;
danielk1977a19df672004-11-03 11:37:07 +00002286 int pgsz = pBt->pageSize; /* Page size for this database */
danielk1977687566d2004-11-02 12:56:41 +00002287 Pgno iDbPage; /* The database page to move */
danielk1977687566d2004-11-02 12:56:41 +00002288 MemPage *pDbMemPage = 0; /* "" */
2289 Pgno iPtrPage; /* The page that contains a pointer to iDbPage */
danielk1977687566d2004-11-02 12:56:41 +00002290 Pgno iFreePage; /* The free-list page to move iDbPage to */
2291 MemPage *pFreeMemPage = 0; /* "" */
2292
2293#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00002294 int nRef = sqlite3PagerRefcount(pPager);
danielk1977687566d2004-11-02 12:56:41 +00002295#endif
2296
2297 assert( pBt->autoVacuum );
danielk19773b8a05f2007-03-19 17:44:26 +00002298 if( PTRMAP_ISPAGE(pBt, sqlite3PagerPagecount(pPager)) ){
drh49285702005-09-17 15:20:26 +00002299 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002300 }
danielk1977687566d2004-11-02 12:56:41 +00002301
2302 /* Figure out how many free-pages are in the database. If there are no
2303 ** free pages, then auto-vacuum is a no-op.
2304 */
2305 nFreeList = get4byte(&pBt->pPage1->aData[36]);
danielk1977a19df672004-11-03 11:37:07 +00002306 if( nFreeList==0 ){
danielk1977d761c0c2004-11-05 16:37:02 +00002307 *nTrunc = 0;
danielk1977a19df672004-11-03 11:37:07 +00002308 return SQLITE_OK;
2309 }
danielk1977687566d2004-11-02 12:56:41 +00002310
danielk1977266664d2006-02-10 08:24:21 +00002311 /* This block figures out how many pages there are in the database
2312 ** now (variable origSize), and how many there will be after the
2313 ** truncation (variable finSize).
2314 **
2315 ** The final size is the original size, less the number of free pages
2316 ** in the database, less any pointer-map pages that will no longer
2317 ** be required, less 1 if the pending-byte page was part of the database
2318 ** but is not after the truncation.
2319 **/
danielk19773b8a05f2007-03-19 17:44:26 +00002320 origSize = sqlite3PagerPagecount(pPager);
danielk1977266664d2006-02-10 08:24:21 +00002321 if( origSize==PENDING_BYTE_PAGE(pBt) ){
2322 origSize--;
2323 }
2324 nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pBt, origSize)+pgsz/5)/(pgsz/5);
danielk1977a19df672004-11-03 11:37:07 +00002325 finSize = origSize - nFreeList - nPtrMap;
danielk1977266664d2006-02-10 08:24:21 +00002326 if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){
danielk1977599fcba2004-11-08 07:13:13 +00002327 finSize--;
danielk1977266664d2006-02-10 08:24:21 +00002328 }
2329 while( PTRMAP_ISPAGE(pBt, finSize) || finSize==PENDING_BYTE_PAGE(pBt) ){
2330 finSize--;
danielk1977599fcba2004-11-08 07:13:13 +00002331 }
danielk1977a19df672004-11-03 11:37:07 +00002332 TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize));
danielk1977687566d2004-11-02 12:56:41 +00002333
danielk1977a19df672004-11-03 11:37:07 +00002334 /* Variable 'finSize' will be the size of the file in pages after
danielk1977687566d2004-11-02 12:56:41 +00002335 ** the auto-vacuum has completed (the current file size minus the number
2336 ** of pages on the free list). Loop through the pages that lie beyond
2337 ** this mark, and if they are not already on the free list, move them
danielk1977a19df672004-11-03 11:37:07 +00002338 ** to a free page earlier in the file (somewhere before finSize).
danielk1977687566d2004-11-02 12:56:41 +00002339 */
danielk1977a19df672004-11-03 11:37:07 +00002340 for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){
danielk1977599fcba2004-11-08 07:13:13 +00002341 /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */
danielk1977266664d2006-02-10 08:24:21 +00002342 if( PTRMAP_ISPAGE(pBt, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){
danielk1977599fcba2004-11-08 07:13:13 +00002343 continue;
2344 }
2345
danielk1977687566d2004-11-02 12:56:41 +00002346 rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage);
2347 if( rc!=SQLITE_OK ) goto autovacuum_out;
drhccae6022005-02-26 17:31:26 +00002348 if( eType==PTRMAP_ROOTPAGE ){
drh49285702005-09-17 15:20:26 +00002349 rc = SQLITE_CORRUPT_BKPT;
drhccae6022005-02-26 17:31:26 +00002350 goto autovacuum_out;
2351 }
danielk1977687566d2004-11-02 12:56:41 +00002352
danielk1977599fcba2004-11-08 07:13:13 +00002353 /* If iDbPage is free, do not swap it. */
2354 if( eType==PTRMAP_FREEPAGE ){
danielk1977687566d2004-11-02 12:56:41 +00002355 continue;
2356 }
drh0787db62007-03-04 13:15:27 +00002357 rc = getPage(pBt, iDbPage, &pDbMemPage, 0);
danielk1977687566d2004-11-02 12:56:41 +00002358 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00002359
2360 /* Find the next page in the free-list that is not already at the end
2361 ** of the file. A page can be pulled off the free list using the
drh4f0c5872007-03-26 22:05:01 +00002362 ** allocateBtreePage() routine.
danielk1977687566d2004-11-02 12:56:41 +00002363 */
2364 do{
2365 if( pFreeMemPage ){
2366 releasePage(pFreeMemPage);
2367 pFreeMemPage = 0;
2368 }
drh4f0c5872007-03-26 22:05:01 +00002369 rc = allocateBtreePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
danielk1977ac11ee62005-01-15 12:45:51 +00002370 if( rc!=SQLITE_OK ){
2371 releasePage(pDbMemPage);
2372 goto autovacuum_out;
2373 }
danielk1977a19df672004-11-03 11:37:07 +00002374 assert( iFreePage<=origSize );
2375 }while( iFreePage>finSize );
danielk1977687566d2004-11-02 12:56:41 +00002376 releasePage(pFreeMemPage);
2377 pFreeMemPage = 0;
danielk1977687566d2004-11-02 12:56:41 +00002378
danielk1977e501b892006-01-09 06:29:47 +00002379 /* Relocate the page into the body of the file. Note that although the
2380 ** page has moved within the database file, the pDbMemPage pointer
2381 ** remains valid. This means that this function can run without
2382 ** invalidating cursors open on the btree. This is important in
2383 ** shared-cache mode.
2384 */
danielk1977003ba062004-11-04 02:57:33 +00002385 rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage);
danielk1977687566d2004-11-02 12:56:41 +00002386 releasePage(pDbMemPage);
danielk1977687566d2004-11-02 12:56:41 +00002387 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00002388 }
2389
2390 /* The entire free-list has been swapped to the end of the file. So
danielk1977a19df672004-11-03 11:37:07 +00002391 ** truncate the database file to finSize pages and consider the
danielk1977687566d2004-11-02 12:56:41 +00002392 ** free-list empty.
2393 */
danielk19773b8a05f2007-03-19 17:44:26 +00002394 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
danielk1977687566d2004-11-02 12:56:41 +00002395 if( rc!=SQLITE_OK ) goto autovacuum_out;
2396 put4byte(&pBt->pPage1->aData[32], 0);
2397 put4byte(&pBt->pPage1->aData[36], 0);
danielk1977d761c0c2004-11-05 16:37:02 +00002398 *nTrunc = finSize;
danielk1977266664d2006-02-10 08:24:21 +00002399 assert( finSize!=PENDING_BYTE_PAGE(pBt) );
danielk1977687566d2004-11-02 12:56:41 +00002400
2401autovacuum_out:
danielk19773b8a05f2007-03-19 17:44:26 +00002402 assert( nRef==sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002403 if( rc!=SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00002404 sqlite3PagerRollback(pPager);
danielk1977687566d2004-11-02 12:56:41 +00002405 }
2406 return rc;
2407}
2408#endif
2409
2410/*
drh2aa679f2001-06-25 02:11:07 +00002411** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00002412**
drh6e345992007-03-30 11:12:08 +00002413** This routine implements the second phase of a 2-phase commit. The
2414** sqlite3BtreeSync() routine does the first phase and should be invoked
2415** prior to calling this routine. The sqlite3BtreeSync() routine did
2416** all the work of writing information out to disk and flushing the
2417** contents so that they are written onto the disk platter. All this
2418** routine has to do is delete or truncate the rollback journal
2419** (which causes the transaction to commit) and drop locks.
2420**
drh5e00f6c2001-09-13 13:46:56 +00002421** This will release the write lock on the database file. If there
2422** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002423*/
danielk1977aef0bf62005-12-30 16:28:01 +00002424int sqlite3BtreeCommit(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00002425 BtShared *pBt = p->pBt;
2426
2427 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002428
2429 /* If the handle has a write-transaction open, commit the shared-btrees
2430 ** transaction and set the shared state to TRANS_READ.
2431 */
2432 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:47 +00002433 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002434 assert( pBt->inTransaction==TRANS_WRITE );
2435 assert( pBt->nTransaction>0 );
danielk19773b8a05f2007-03-19 17:44:26 +00002436 rc = sqlite3PagerCommit(pBt->pPager);
danielk19777f7bc662006-01-23 13:47:47 +00002437 if( rc!=SQLITE_OK ){
2438 return rc;
2439 }
danielk1977aef0bf62005-12-30 16:28:01 +00002440 pBt->inTransaction = TRANS_READ;
2441 pBt->inStmt = 0;
danielk1977ee5741e2004-05-31 10:01:34 +00002442 }
danielk19777f7bc662006-01-23 13:47:47 +00002443 unlockAllTables(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002444
2445 /* If the handle has any kind of transaction open, decrement the transaction
2446 ** count of the shared btree. If the transaction count reaches 0, set
2447 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2448 ** will unlock the pager.
2449 */
2450 if( p->inTrans!=TRANS_NONE ){
2451 pBt->nTransaction--;
2452 if( 0==pBt->nTransaction ){
2453 pBt->inTransaction = TRANS_NONE;
2454 }
2455 }
2456
2457 /* Set the handles current transaction state to TRANS_NONE and unlock
2458 ** the pager if this call closed the only read or write transaction.
2459 */
2460 p->inTrans = TRANS_NONE;
drh5e00f6c2001-09-13 13:46:56 +00002461 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002462
2463 btreeIntegrity(p);
danielk19777f7bc662006-01-23 13:47:47 +00002464 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002465}
2466
danielk1977fbcd5852004-06-15 02:44:18 +00002467#ifndef NDEBUG
2468/*
2469** Return the number of write-cursors open on this handle. This is for use
2470** in assert() expressions, so it is only compiled if NDEBUG is not
2471** defined.
2472*/
danielk1977aef0bf62005-12-30 16:28:01 +00002473static int countWriteCursors(BtShared *pBt){
danielk1977fbcd5852004-06-15 02:44:18 +00002474 BtCursor *pCur;
2475 int r = 0;
2476 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
danielk1977aef0bf62005-12-30 16:28:01 +00002477 if( pCur->wrFlag ) r++;
danielk1977fbcd5852004-06-15 02:44:18 +00002478 }
2479 return r;
2480}
2481#endif
2482
drh77bba592006-08-13 18:39:26 +00002483#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhda200cc2004-05-09 11:51:38 +00002484/*
2485** Print debugging information about all cursors to standard output.
2486*/
danielk1977aef0bf62005-12-30 16:28:01 +00002487void sqlite3BtreeCursorList(Btree *p){
drhda200cc2004-05-09 11:51:38 +00002488 BtCursor *pCur;
danielk1977aef0bf62005-12-30 16:28:01 +00002489 BtShared *pBt = p->pBt;
drhda200cc2004-05-09 11:51:38 +00002490 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
2491 MemPage *pPage = pCur->pPage;
2492 char *zMode = pCur->wrFlag ? "rw" : "ro";
drhfe63d1c2004-09-08 20:13:04 +00002493 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
2494 pCur, pCur->pgnoRoot, zMode,
drhda200cc2004-05-09 11:51:38 +00002495 pPage ? pPage->pgno : 0, pCur->idx,
danielk1977da184232006-01-05 11:34:32 +00002496 (pCur->eState==CURSOR_VALID) ? "" : " eof"
drhda200cc2004-05-09 11:51:38 +00002497 );
2498 }
2499}
2500#endif
2501
drhc39e0002004-05-07 23:50:57 +00002502/*
drhecdc7532001-09-23 02:35:53 +00002503** Rollback the transaction in progress. All cursors will be
2504** invalided by this operation. Any attempt to use a cursor
2505** that was open at the beginning of this operation will result
2506** in an error.
drh5e00f6c2001-09-13 13:46:56 +00002507**
2508** This will release the write lock on the database file. If there
2509** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002510*/
danielk1977aef0bf62005-12-30 16:28:01 +00002511int sqlite3BtreeRollback(Btree *p){
danielk19778d34dfd2006-01-24 16:37:57 +00002512 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002513 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:47 +00002514 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:01 +00002515
danielk19772b8c13e2006-01-24 14:21:24 +00002516 rc = saveAllCursors(pBt, 0, 0);
danielk19778d34dfd2006-01-24 16:37:57 +00002517#ifndef SQLITE_OMIT_SHARED_CACHE
danielk19772b8c13e2006-01-24 14:21:24 +00002518 if( rc!=SQLITE_OK ){
danielk19778d34dfd2006-01-24 16:37:57 +00002519 /* This is a horrible situation. An IO or malloc() error occured whilst
2520 ** trying to save cursor positions. If this is an automatic rollback (as
2521 ** the result of a constraint, malloc() failure or IO error) then
2522 ** the cache may be internally inconsistent (not contain valid trees) so
2523 ** we cannot simply return the error to the caller. Instead, abort
2524 ** all queries that may be using any of the cursors that failed to save.
2525 */
2526 while( pBt->pCursor ){
2527 sqlite3 *db = pBt->pCursor->pBtree->pSqlite;
2528 if( db ){
2529 sqlite3AbortOtherActiveVdbes(db, 0);
2530 }
2531 }
danielk19772b8c13e2006-01-24 14:21:24 +00002532 }
danielk19778d34dfd2006-01-24 16:37:57 +00002533#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002534 btreeIntegrity(p);
2535 unlockAllTables(p);
2536
2537 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:57 +00002538 int rc2;
danielk1977aef0bf62005-12-30 16:28:01 +00002539
danielk19778d34dfd2006-01-24 16:37:57 +00002540 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:26 +00002541 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:57 +00002542 if( rc2!=SQLITE_OK ){
2543 rc = rc2;
2544 }
2545
drh24cd67e2004-05-10 16:18:47 +00002546 /* The rollback may have destroyed the pPage1->aData value. So
2547 ** call getPage() on page 1 again to make sure pPage1->aData is
2548 ** set correctly. */
drh0787db62007-03-04 13:15:27 +00002549 if( getPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh24cd67e2004-05-10 16:18:47 +00002550 releasePage(pPage1);
2551 }
danielk1977fbcd5852004-06-15 02:44:18 +00002552 assert( countWriteCursors(pBt)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00002553 pBt->inTransaction = TRANS_READ;
drh24cd67e2004-05-10 16:18:47 +00002554 }
danielk1977aef0bf62005-12-30 16:28:01 +00002555
2556 if( p->inTrans!=TRANS_NONE ){
2557 assert( pBt->nTransaction>0 );
2558 pBt->nTransaction--;
2559 if( 0==pBt->nTransaction ){
2560 pBt->inTransaction = TRANS_NONE;
2561 }
2562 }
2563
2564 p->inTrans = TRANS_NONE;
danielk1977ee5741e2004-05-31 10:01:34 +00002565 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00002566 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002567
2568 btreeIntegrity(p);
drha059ad02001-04-17 20:09:11 +00002569 return rc;
2570}
2571
2572/*
drhab01f612004-05-22 02:55:23 +00002573** Start a statement subtransaction. The subtransaction can
2574** can be rolled back independently of the main transaction.
2575** You must start a transaction before starting a subtransaction.
2576** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00002577** commits or rolls back.
2578**
drhab01f612004-05-22 02:55:23 +00002579** Only one subtransaction may be active at a time. It is an error to try
2580** to start a new subtransaction if another subtransaction is already active.
2581**
2582** Statement subtransactions are used around individual SQL statements
2583** that are contained within a BEGIN...COMMIT block. If a constraint
2584** error occurs within the statement, the effect of that one statement
2585** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002586*/
danielk1977aef0bf62005-12-30 16:28:01 +00002587int sqlite3BtreeBeginStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002588 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002589 BtShared *pBt = p->pBt;
2590 if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
drhf74b8d92002-09-01 23:20:45 +00002591 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh0d65dc02002-02-03 00:56:09 +00002592 }
danielk1977aef0bf62005-12-30 16:28:01 +00002593 assert( pBt->inTransaction==TRANS_WRITE );
danielk19773b8a05f2007-03-19 17:44:26 +00002594 rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
drh3aac2dd2004-04-26 14:10:20 +00002595 pBt->inStmt = 1;
drh663fc632002-02-02 18:49:19 +00002596 return rc;
2597}
2598
2599
2600/*
drhab01f612004-05-22 02:55:23 +00002601** Commit the statment subtransaction currently in progress. If no
2602** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002603*/
danielk1977aef0bf62005-12-30 16:28:01 +00002604int sqlite3BtreeCommitStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002605 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002606 BtShared *pBt = p->pBt;
drh3aac2dd2004-04-26 14:10:20 +00002607 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002608 rc = sqlite3PagerStmtCommit(pBt->pPager);
drh663fc632002-02-02 18:49:19 +00002609 }else{
2610 rc = SQLITE_OK;
2611 }
drh3aac2dd2004-04-26 14:10:20 +00002612 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00002613 return rc;
2614}
2615
2616/*
drhab01f612004-05-22 02:55:23 +00002617** Rollback the active statement subtransaction. If no subtransaction
2618** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002619**
drhab01f612004-05-22 02:55:23 +00002620** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002621** to use a cursor that was open at the beginning of this operation
2622** will result in an error.
2623*/
danielk1977aef0bf62005-12-30 16:28:01 +00002624int sqlite3BtreeRollbackStmt(Btree *p){
danielk197797a227c2006-01-20 16:32:04 +00002625 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00002626 BtShared *pBt = p->pBt;
danielk197797a227c2006-01-20 16:32:04 +00002627 sqlite3MallocDisallow();
2628 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002629 rc = sqlite3PagerStmtRollback(pBt->pPager);
danielk197797a227c2006-01-20 16:32:04 +00002630 assert( countWriteCursors(pBt)==0 );
2631 pBt->inStmt = 0;
2632 }
2633 sqlite3MallocAllow();
drh663fc632002-02-02 18:49:19 +00002634 return rc;
2635}
2636
2637/*
drh3aac2dd2004-04-26 14:10:20 +00002638** Default key comparison function to be used if no comparison function
2639** is specified on the sqlite3BtreeCursor() call.
2640*/
2641static int dfltCompare(
2642 void *NotUsed, /* User data is not used */
2643 int n1, const void *p1, /* First key to compare */
2644 int n2, const void *p2 /* Second key to compare */
2645){
2646 int c;
2647 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
2648 if( c==0 ){
2649 c = n1 - n2;
2650 }
2651 return c;
2652}
2653
2654/*
drh8b2f49b2001-06-08 00:21:52 +00002655** Create a new cursor for the BTree whose root is on the page
2656** iTable. The act of acquiring a cursor gets a read lock on
2657** the database file.
drh1bee3d72001-10-15 00:44:35 +00002658**
2659** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002660** If wrFlag==1, then the cursor can be used for reading or for
2661** writing if other conditions for writing are also met. These
2662** are the conditions that must be met in order for writing to
2663** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002664**
drhf74b8d92002-09-01 23:20:45 +00002665** 1: The cursor must have been opened with wrFlag==1
2666**
drhfe5d71d2007-03-19 11:54:10 +00002667** 2: Other database connections that share the same pager cache
2668** but which are not in the READ_UNCOMMITTED state may not have
2669** cursors open with wrFlag==0 on the same table. Otherwise
2670** the changes made by this write cursor would be visible to
2671** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:45 +00002672**
2673** 3: The database must be writable (not on read-only media)
2674**
2675** 4: There must be an active transaction.
2676**
drh6446c4d2001-12-15 14:22:18 +00002677** No checking is done to make sure that page iTable really is the
2678** root page of a b-tree. If it is not, then the cursor acquired
2679** will not work correctly.
drh3aac2dd2004-04-26 14:10:20 +00002680**
2681** The comparison function must be logically the same for every cursor
2682** on a particular table. Changing the comparison function will result
2683** in incorrect operations. If the comparison function is NULL, a
2684** default comparison function is used. The comparison function is
2685** always ignored for INTKEY tables.
drha059ad02001-04-17 20:09:11 +00002686*/
drh3aac2dd2004-04-26 14:10:20 +00002687int sqlite3BtreeCursor(
danielk1977aef0bf62005-12-30 16:28:01 +00002688 Btree *p, /* The btree */
drh3aac2dd2004-04-26 14:10:20 +00002689 int iTable, /* Root page of table to open */
2690 int wrFlag, /* 1 to write. 0 read-only */
2691 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2692 void *pArg, /* First arg to xCompare() */
2693 BtCursor **ppCur /* Write new cursor here */
2694){
drha059ad02001-04-17 20:09:11 +00002695 int rc;
drh8dcd7ca2004-08-08 19:43:29 +00002696 BtCursor *pCur;
danielk1977aef0bf62005-12-30 16:28:01 +00002697 BtShared *pBt = p->pBt;
drhecdc7532001-09-23 02:35:53 +00002698
drh8dcd7ca2004-08-08 19:43:29 +00002699 *ppCur = 0;
2700 if( wrFlag ){
drh8dcd7ca2004-08-08 19:43:29 +00002701 if( pBt->readOnly ){
2702 return SQLITE_READONLY;
2703 }
drh980b1a72006-08-16 16:42:48 +00002704 if( checkReadLocks(p, iTable, 0) ){
drh8dcd7ca2004-08-08 19:43:29 +00002705 return SQLITE_LOCKED;
2706 }
drha0c9a112004-03-10 13:42:37 +00002707 }
danielk1977aef0bf62005-12-30 16:28:01 +00002708
drh4b70f112004-05-02 21:12:19 +00002709 if( pBt->pPage1==0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002710 rc = lockBtreeWithRetry(p);
drha059ad02001-04-17 20:09:11 +00002711 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002712 return rc;
2713 }
2714 }
danielk1977da184232006-01-05 11:34:32 +00002715 pCur = sqliteMalloc( sizeof(*pCur) );
drha059ad02001-04-17 20:09:11 +00002716 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +00002717 rc = SQLITE_NOMEM;
2718 goto create_cursor_exception;
2719 }
drh8b2f49b2001-06-08 00:21:52 +00002720 pCur->pgnoRoot = (Pgno)iTable;
danielk19773b8a05f2007-03-19 17:44:26 +00002721 if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
drh24cd67e2004-05-10 16:18:47 +00002722 rc = SQLITE_EMPTY;
2723 goto create_cursor_exception;
2724 }
drhde647132004-05-07 17:57:49 +00002725 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
drhbd03cae2001-06-02 02:40:57 +00002726 if( rc!=SQLITE_OK ){
2727 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002728 }
danielk1977aef0bf62005-12-30 16:28:01 +00002729
danielk1977aef0bf62005-12-30 16:28:01 +00002730 /* Now that no other errors can occur, finish filling in the BtCursor
2731 ** variables, link the cursor into the BtShared list and set *ppCur (the
2732 ** output argument to this function).
2733 */
drh3aac2dd2004-04-26 14:10:20 +00002734 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2735 pCur->pArg = pArg;
danielk1977aef0bf62005-12-30 16:28:01 +00002736 pCur->pBtree = p;
drhecdc7532001-09-23 02:35:53 +00002737 pCur->wrFlag = wrFlag;
drha059ad02001-04-17 20:09:11 +00002738 pCur->pNext = pBt->pCursor;
2739 if( pCur->pNext ){
2740 pCur->pNext->pPrev = pCur;
2741 }
2742 pBt->pCursor = pCur;
danielk1977da184232006-01-05 11:34:32 +00002743 pCur->eState = CURSOR_INVALID;
drh2af926b2001-05-15 00:39:25 +00002744 *ppCur = pCur;
drhbd03cae2001-06-02 02:40:57 +00002745
danielk1977aef0bf62005-12-30 16:28:01 +00002746 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00002747create_cursor_exception:
drhbd03cae2001-06-02 02:40:57 +00002748 if( pCur ){
drh3aac2dd2004-04-26 14:10:20 +00002749 releasePage(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00002750 sqliteFree(pCur);
2751 }
drh5e00f6c2001-09-13 13:46:56 +00002752 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002753 return rc;
drha059ad02001-04-17 20:09:11 +00002754}
2755
drh7a224de2004-06-02 01:22:02 +00002756#if 0 /* Not Used */
drhd3d39e92004-05-20 22:16:29 +00002757/*
2758** Change the value of the comparison function used by a cursor.
2759*/
danielk1977bf3b7212004-05-18 10:06:24 +00002760void sqlite3BtreeSetCompare(
drhd3d39e92004-05-20 22:16:29 +00002761 BtCursor *pCur, /* The cursor to whose comparison function is changed */
2762 int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */
2763 void *pArg /* First argument to xCmp() */
danielk1977bf3b7212004-05-18 10:06:24 +00002764){
2765 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2766 pCur->pArg = pArg;
2767}
drh7a224de2004-06-02 01:22:02 +00002768#endif
danielk1977bf3b7212004-05-18 10:06:24 +00002769
drha059ad02001-04-17 20:09:11 +00002770/*
drh5e00f6c2001-09-13 13:46:56 +00002771** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002772** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002773*/
drh3aac2dd2004-04-26 14:10:20 +00002774int sqlite3BtreeCloseCursor(BtCursor *pCur){
danielk1977aef0bf62005-12-30 16:28:01 +00002775 BtShared *pBt = pCur->pBtree->pBt;
drh777e4c42006-01-13 04:31:58 +00002776 restoreOrClearCursorPosition(pCur, 0);
drha059ad02001-04-17 20:09:11 +00002777 if( pCur->pPrev ){
2778 pCur->pPrev->pNext = pCur->pNext;
2779 }else{
2780 pBt->pCursor = pCur->pNext;
2781 }
2782 if( pCur->pNext ){
2783 pCur->pNext->pPrev = pCur->pPrev;
2784 }
drh3aac2dd2004-04-26 14:10:20 +00002785 releasePage(pCur->pPage);
drh5e00f6c2001-09-13 13:46:56 +00002786 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00002787 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +00002788 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002789}
2790
drh7e3b0a02001-04-28 16:52:40 +00002791/*
drh5e2f8b92001-05-28 00:41:15 +00002792** Make a temporary cursor by filling in the fields of pTempCur.
2793** The temporary cursor is not on the cursor list for the Btree.
2794*/
drh14acc042001-06-10 19:56:58 +00002795static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +00002796 memcpy(pTempCur, pCur, sizeof(*pCur));
2797 pTempCur->pNext = 0;
2798 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00002799 if( pTempCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002800 sqlite3PagerRef(pTempCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002801 }
drh5e2f8b92001-05-28 00:41:15 +00002802}
2803
2804/*
drhbd03cae2001-06-02 02:40:57 +00002805** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002806** function above.
2807*/
drh14acc042001-06-10 19:56:58 +00002808static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +00002809 if( pCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002810 sqlite3PagerUnref(pCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002811 }
drh5e2f8b92001-05-28 00:41:15 +00002812}
2813
2814/*
drh9188b382004-05-14 21:12:22 +00002815** Make sure the BtCursor.info field of the given cursor is valid.
drhab01f612004-05-22 02:55:23 +00002816** If it is not already valid, call parseCell() to fill it in.
2817**
2818** BtCursor.info is a cache of the information in the current cell.
2819** Using this cache reduces the number of calls to parseCell().
drh9188b382004-05-14 21:12:22 +00002820*/
2821static void getCellInfo(BtCursor *pCur){
drh271efa52004-05-30 19:19:05 +00002822 if( pCur->info.nSize==0 ){
drh3a41a3f2004-05-30 02:14:17 +00002823 parseCell(pCur->pPage, pCur->idx, &pCur->info);
drh9188b382004-05-14 21:12:22 +00002824 }else{
2825#ifndef NDEBUG
2826 CellInfo info;
drh51c6d962004-06-06 00:42:25 +00002827 memset(&info, 0, sizeof(info));
drh3a41a3f2004-05-30 02:14:17 +00002828 parseCell(pCur->pPage, pCur->idx, &info);
drh9188b382004-05-14 21:12:22 +00002829 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
2830#endif
2831 }
2832}
2833
2834/*
drh3aac2dd2004-04-26 14:10:20 +00002835** Set *pSize to the size of the buffer needed to hold the value of
2836** the key for the current entry. If the cursor is not pointing
2837** to a valid entry, *pSize is set to 0.
2838**
drh4b70f112004-05-02 21:12:19 +00002839** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00002840** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00002841*/
drh4a1c3802004-05-12 15:15:47 +00002842int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
drh777e4c42006-01-13 04:31:58 +00002843 int rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00002844 if( rc==SQLITE_OK ){
2845 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2846 if( pCur->eState==CURSOR_INVALID ){
2847 *pSize = 0;
2848 }else{
2849 getCellInfo(pCur);
2850 *pSize = pCur->info.nKey;
2851 }
drh72f82862001-05-24 21:06:34 +00002852 }
danielk1977da184232006-01-05 11:34:32 +00002853 return rc;
drha059ad02001-04-17 20:09:11 +00002854}
drh2af926b2001-05-15 00:39:25 +00002855
drh72f82862001-05-24 21:06:34 +00002856/*
drh0e1c19e2004-05-11 00:58:56 +00002857** Set *pSize to the number of bytes of data in the entry the
2858** cursor currently points to. Always return SQLITE_OK.
2859** Failure is not possible. If the cursor is not currently
2860** pointing to an entry (which can happen, for example, if
2861** the database is empty) then *pSize is set to 0.
2862*/
2863int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
drh777e4c42006-01-13 04:31:58 +00002864 int rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00002865 if( rc==SQLITE_OK ){
2866 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2867 if( pCur->eState==CURSOR_INVALID ){
2868 /* Not pointing at a valid entry - set *pSize to 0. */
2869 *pSize = 0;
2870 }else{
2871 getCellInfo(pCur);
2872 *pSize = pCur->info.nData;
2873 }
drh0e1c19e2004-05-11 00:58:56 +00002874 }
danielk1977da184232006-01-05 11:34:32 +00002875 return rc;
drh0e1c19e2004-05-11 00:58:56 +00002876}
2877
2878/*
drh72f82862001-05-24 21:06:34 +00002879** Read payload information from the entry that the pCur cursor is
2880** pointing to. Begin reading the payload at "offset" and read
2881** a total of "amt" bytes. Put the result in zBuf.
2882**
2883** This routine does not make a distinction between key and data.
drhab01f612004-05-22 02:55:23 +00002884** It just reads bytes from the payload area. Data might appear
2885** on the main page or be scattered out on multiple overflow pages.
drh72f82862001-05-24 21:06:34 +00002886*/
drh3aac2dd2004-04-26 14:10:20 +00002887static int getPayload(
2888 BtCursor *pCur, /* Cursor pointing to entry to read from */
2889 int offset, /* Begin reading this far into payload */
2890 int amt, /* Read this many bytes */
2891 unsigned char *pBuf, /* Write the bytes into this buffer */
2892 int skipKey /* offset begins at data if this is true */
2893){
2894 unsigned char *aPayload;
drh2af926b2001-05-15 00:39:25 +00002895 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +00002896 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002897 MemPage *pPage;
danielk1977aef0bf62005-12-30 16:28:01 +00002898 BtShared *pBt;
drh6f11bef2004-05-13 01:12:56 +00002899 int ovflSize;
drhfa1a98a2004-05-14 19:08:17 +00002900 u32 nKey;
drh3aac2dd2004-04-26 14:10:20 +00002901
drh72f82862001-05-24 21:06:34 +00002902 assert( pCur!=0 && pCur->pPage!=0 );
danielk1977da184232006-01-05 11:34:32 +00002903 assert( pCur->eState==CURSOR_VALID );
danielk1977aef0bf62005-12-30 16:28:01 +00002904 pBt = pCur->pBtree->pBt;
drh3aac2dd2004-04-26 14:10:20 +00002905 pPage = pCur->pPage;
2906 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00002907 getCellInfo(pCur);
drh366fda62006-01-13 02:35:09 +00002908 aPayload = pCur->info.pCell + pCur->info.nHeader;
drh3aac2dd2004-04-26 14:10:20 +00002909 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00002910 nKey = 0;
2911 }else{
2912 nKey = pCur->info.nKey;
drh3aac2dd2004-04-26 14:10:20 +00002913 }
2914 assert( offset>=0 );
2915 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00002916 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00002917 }
drhfa1a98a2004-05-14 19:08:17 +00002918 if( offset+amt > nKey+pCur->info.nData ){
drha34b6762004-05-07 13:30:42 +00002919 return SQLITE_ERROR;
drh3aac2dd2004-04-26 14:10:20 +00002920 }
drhfa1a98a2004-05-14 19:08:17 +00002921 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00002922 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00002923 if( a+offset>pCur->info.nLocal ){
2924 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00002925 }
drha34b6762004-05-07 13:30:42 +00002926 memcpy(pBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +00002927 if( a==amt ){
2928 return SQLITE_OK;
2929 }
drh2aa679f2001-06-25 02:11:07 +00002930 offset = 0;
drha34b6762004-05-07 13:30:42 +00002931 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00002932 amt -= a;
drhdd793422001-06-28 01:54:48 +00002933 }else{
drhfa1a98a2004-05-14 19:08:17 +00002934 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00002935 }
danielk1977cfe9a692004-06-16 12:00:29 +00002936 ovflSize = pBt->usableSize - 4;
drhbd03cae2001-06-02 02:40:57 +00002937 if( amt>0 ){
drhfa1a98a2004-05-14 19:08:17 +00002938 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977cfe9a692004-06-16 12:00:29 +00002939 while( amt>0 && nextPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002940 DbPage *pDbPage;
2941 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
danielk1977cfe9a692004-06-16 12:00:29 +00002942 if( rc!=0 ){
2943 return rc;
drh2af926b2001-05-15 00:39:25 +00002944 }
danielk19773b8a05f2007-03-19 17:44:26 +00002945 aPayload = sqlite3PagerGetData(pDbPage);
danielk1977cfe9a692004-06-16 12:00:29 +00002946 nextPage = get4byte(aPayload);
2947 if( offset<ovflSize ){
2948 int a = amt;
2949 if( a + offset > ovflSize ){
2950 a = ovflSize - offset;
2951 }
2952 memcpy(pBuf, &aPayload[offset+4], a);
2953 offset = 0;
2954 amt -= a;
2955 pBuf += a;
2956 }else{
2957 offset -= ovflSize;
2958 }
danielk19773b8a05f2007-03-19 17:44:26 +00002959 sqlite3PagerUnref(pDbPage);
drh2af926b2001-05-15 00:39:25 +00002960 }
drh2af926b2001-05-15 00:39:25 +00002961 }
danielk1977cfe9a692004-06-16 12:00:29 +00002962
drha7fcb052001-12-14 15:09:55 +00002963 if( amt>0 ){
drh49285702005-09-17 15:20:26 +00002964 return SQLITE_CORRUPT_BKPT;
drha7fcb052001-12-14 15:09:55 +00002965 }
2966 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00002967}
2968
drh72f82862001-05-24 21:06:34 +00002969/*
drh3aac2dd2004-04-26 14:10:20 +00002970** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002971** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002972** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00002973**
drh3aac2dd2004-04-26 14:10:20 +00002974** Return SQLITE_OK on success or an error code if anything goes
2975** wrong. An error is returned if "offset+amt" is larger than
2976** the available payload.
drh72f82862001-05-24 21:06:34 +00002977*/
drha34b6762004-05-07 13:30:42 +00002978int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drh777e4c42006-01-13 04:31:58 +00002979 int rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00002980 if( rc==SQLITE_OK ){
2981 assert( pCur->eState==CURSOR_VALID );
2982 assert( pCur->pPage!=0 );
2983 if( pCur->pPage->intKey ){
2984 return SQLITE_CORRUPT_BKPT;
2985 }
2986 assert( pCur->pPage->intKey==0 );
2987 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
2988 rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
drh6575a222005-03-10 17:06:34 +00002989 }
danielk1977da184232006-01-05 11:34:32 +00002990 return rc;
drh3aac2dd2004-04-26 14:10:20 +00002991}
2992
2993/*
drh3aac2dd2004-04-26 14:10:20 +00002994** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002995** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002996** begins at "offset".
2997**
2998** Return SQLITE_OK on success or an error code if anything goes
2999** wrong. An error is returned if "offset+amt" is larger than
3000** the available payload.
drh72f82862001-05-24 21:06:34 +00003001*/
drh3aac2dd2004-04-26 14:10:20 +00003002int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drh777e4c42006-01-13 04:31:58 +00003003 int rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00003004 if( rc==SQLITE_OK ){
3005 assert( pCur->eState==CURSOR_VALID );
3006 assert( pCur->pPage!=0 );
3007 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3008 rc = getPayload(pCur, offset, amt, pBuf, 1);
3009 }
3010 return rc;
drh2af926b2001-05-15 00:39:25 +00003011}
3012
drh72f82862001-05-24 21:06:34 +00003013/*
drh0e1c19e2004-05-11 00:58:56 +00003014** Return a pointer to payload information from the entry that the
3015** pCur cursor is pointing to. The pointer is to the beginning of
3016** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00003017** skipKey==1. The number of bytes of available key/data is written
3018** into *pAmt. If *pAmt==0, then the value returned will not be
3019** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00003020**
3021** This routine is an optimization. It is common for the entire key
3022** and data to fit on the local page and for there to be no overflow
3023** pages. When that is so, this routine can be used to access the
3024** key and data without making a copy. If the key and/or data spills
3025** onto overflow pages, then getPayload() must be used to reassembly
3026** the key/data and copy it into a preallocated buffer.
3027**
3028** The pointer returned by this routine looks directly into the cached
3029** page of the database. The data might change or move the next time
3030** any btree routine is called.
3031*/
3032static const unsigned char *fetchPayload(
3033 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00003034 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00003035 int skipKey /* read beginning at data if this is true */
3036){
3037 unsigned char *aPayload;
3038 MemPage *pPage;
drhfa1a98a2004-05-14 19:08:17 +00003039 u32 nKey;
3040 int nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003041
3042 assert( pCur!=0 && pCur->pPage!=0 );
danielk1977da184232006-01-05 11:34:32 +00003043 assert( pCur->eState==CURSOR_VALID );
drh0e1c19e2004-05-11 00:58:56 +00003044 pPage = pCur->pPage;
drh0e1c19e2004-05-11 00:58:56 +00003045 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00003046 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00003047 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00003048 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00003049 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003050 nKey = 0;
3051 }else{
3052 nKey = pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00003053 }
drh0e1c19e2004-05-11 00:58:56 +00003054 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003055 aPayload += nKey;
3056 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00003057 }else{
drhfa1a98a2004-05-14 19:08:17 +00003058 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00003059 if( nLocal>nKey ){
3060 nLocal = nKey;
3061 }
drh0e1c19e2004-05-11 00:58:56 +00003062 }
drhe51c44f2004-05-30 20:46:09 +00003063 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003064 return aPayload;
3065}
3066
3067
3068/*
drhe51c44f2004-05-30 20:46:09 +00003069** For the entry that cursor pCur is point to, return as
3070** many bytes of the key or data as are available on the local
3071** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00003072**
3073** The pointer returned is ephemeral. The key/data may move
3074** or be destroyed on the next call to any Btree routine.
3075**
3076** These routines is used to get quick access to key and data
3077** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00003078*/
drhe51c44f2004-05-30 20:46:09 +00003079const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
danielk1977da184232006-01-05 11:34:32 +00003080 if( pCur->eState==CURSOR_VALID ){
3081 return (const void*)fetchPayload(pCur, pAmt, 0);
3082 }
3083 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003084}
drhe51c44f2004-05-30 20:46:09 +00003085const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
danielk1977da184232006-01-05 11:34:32 +00003086 if( pCur->eState==CURSOR_VALID ){
3087 return (const void*)fetchPayload(pCur, pAmt, 1);
3088 }
3089 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003090}
3091
3092
3093/*
drh8178a752003-01-05 21:41:40 +00003094** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00003095** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00003096*/
drh3aac2dd2004-04-26 14:10:20 +00003097static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00003098 int rc;
3099 MemPage *pNewPage;
drh3aac2dd2004-04-26 14:10:20 +00003100 MemPage *pOldPage;
danielk1977aef0bf62005-12-30 16:28:01 +00003101 BtShared *pBt = pCur->pBtree->pBt;
drh72f82862001-05-24 21:06:34 +00003102
danielk1977da184232006-01-05 11:34:32 +00003103 assert( pCur->eState==CURSOR_VALID );
drhde647132004-05-07 17:57:49 +00003104 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00003105 if( rc ) return rc;
drh428ae8c2003-01-04 16:48:09 +00003106 pNewPage->idxParent = pCur->idx;
drh3aac2dd2004-04-26 14:10:20 +00003107 pOldPage = pCur->pPage;
3108 pOldPage->idxShift = 0;
3109 releasePage(pOldPage);
drh72f82862001-05-24 21:06:34 +00003110 pCur->pPage = pNewPage;
3111 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003112 pCur->info.nSize = 0;
drh4be295b2003-12-16 03:44:47 +00003113 if( pNewPage->nCell<1 ){
drh49285702005-09-17 15:20:26 +00003114 return SQLITE_CORRUPT_BKPT;
drh4be295b2003-12-16 03:44:47 +00003115 }
drh72f82862001-05-24 21:06:34 +00003116 return SQLITE_OK;
3117}
3118
3119/*
drh8856d6a2004-04-29 14:42:46 +00003120** Return true if the page is the virtual root of its table.
3121**
3122** The virtual root page is the root page for most tables. But
3123** for the table rooted on page 1, sometime the real root page
3124** is empty except for the right-pointer. In such cases the
3125** virtual root page is the page that the right-pointer of page
3126** 1 is pointing to.
3127*/
3128static int isRootPage(MemPage *pPage){
3129 MemPage *pParent = pPage->pParent;
drhda200cc2004-05-09 11:51:38 +00003130 if( pParent==0 ) return 1;
3131 if( pParent->pgno>1 ) return 0;
3132 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
drh8856d6a2004-04-29 14:42:46 +00003133 return 0;
3134}
3135
3136/*
drh5e2f8b92001-05-28 00:41:15 +00003137** Move the cursor up to the parent page.
3138**
3139** pCur->idx is set to the cell index that contains the pointer
3140** to the page we are coming from. If we are coming from the
3141** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00003142** the largest cell index.
drh72f82862001-05-24 21:06:34 +00003143*/
drh8178a752003-01-05 21:41:40 +00003144static void moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00003145 MemPage *pParent;
drh8178a752003-01-05 21:41:40 +00003146 MemPage *pPage;
drh428ae8c2003-01-04 16:48:09 +00003147 int idxParent;
drh3aac2dd2004-04-26 14:10:20 +00003148
danielk1977da184232006-01-05 11:34:32 +00003149 assert( pCur->eState==CURSOR_VALID );
drh8178a752003-01-05 21:41:40 +00003150 pPage = pCur->pPage;
3151 assert( pPage!=0 );
drh8856d6a2004-04-29 14:42:46 +00003152 assert( !isRootPage(pPage) );
drh8178a752003-01-05 21:41:40 +00003153 pParent = pPage->pParent;
3154 assert( pParent!=0 );
3155 idxParent = pPage->idxParent;
danielk19773b8a05f2007-03-19 17:44:26 +00003156 sqlite3PagerRef(pParent->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003157 releasePage(pPage);
drh72f82862001-05-24 21:06:34 +00003158 pCur->pPage = pParent;
drh271efa52004-05-30 19:19:05 +00003159 pCur->info.nSize = 0;
drh428ae8c2003-01-04 16:48:09 +00003160 assert( pParent->idxShift==0 );
drh43605152004-05-29 21:46:49 +00003161 pCur->idx = idxParent;
drh72f82862001-05-24 21:06:34 +00003162}
3163
3164/*
3165** Move the cursor to the root page
3166*/
drh5e2f8b92001-05-28 00:41:15 +00003167static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00003168 MemPage *pRoot;
drh777e4c42006-01-13 04:31:58 +00003169 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00003170 BtShared *pBt = pCur->pBtree->pBt;
drhbd03cae2001-06-02 02:40:57 +00003171
drh777e4c42006-01-13 04:31:58 +00003172 restoreOrClearCursorPosition(pCur, 0);
drh777e4c42006-01-13 04:31:58 +00003173 pRoot = pCur->pPage;
danielk197797a227c2006-01-20 16:32:04 +00003174 if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
drh777e4c42006-01-13 04:31:58 +00003175 assert( pRoot->isInit );
3176 }else{
3177 if(
3178 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
3179 ){
3180 pCur->eState = CURSOR_INVALID;
3181 return rc;
3182 }
3183 releasePage(pCur->pPage);
drh777e4c42006-01-13 04:31:58 +00003184 pCur->pPage = pRoot;
drhc39e0002004-05-07 23:50:57 +00003185 }
drh72f82862001-05-24 21:06:34 +00003186 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003187 pCur->info.nSize = 0;
drh8856d6a2004-04-29 14:42:46 +00003188 if( pRoot->nCell==0 && !pRoot->leaf ){
3189 Pgno subpage;
3190 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00003191 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00003192 assert( subpage>0 );
danielk1977da184232006-01-05 11:34:32 +00003193 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:19 +00003194 rc = moveToChild(pCur, subpage);
drh8856d6a2004-04-29 14:42:46 +00003195 }
danielk1977da184232006-01-05 11:34:32 +00003196 pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
drh8856d6a2004-04-29 14:42:46 +00003197 return rc;
drh72f82862001-05-24 21:06:34 +00003198}
drh2af926b2001-05-15 00:39:25 +00003199
drh5e2f8b92001-05-28 00:41:15 +00003200/*
3201** Move the cursor down to the left-most leaf entry beneath the
3202** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:58 +00003203**
3204** The left-most leaf is the one with the smallest key - the first
3205** in ascending order.
drh5e2f8b92001-05-28 00:41:15 +00003206*/
3207static int moveToLeftmost(BtCursor *pCur){
3208 Pgno pgno;
3209 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003210 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00003211
danielk1977da184232006-01-05 11:34:32 +00003212 assert( pCur->eState==CURSOR_VALID );
drh3aac2dd2004-04-26 14:10:20 +00003213 while( !(pPage = pCur->pPage)->leaf ){
drha34b6762004-05-07 13:30:42 +00003214 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00003215 pgno = get4byte(findCell(pPage, pCur->idx));
drh8178a752003-01-05 21:41:40 +00003216 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00003217 if( rc ) return rc;
3218 }
3219 return SQLITE_OK;
3220}
3221
drh2dcc9aa2002-12-04 13:40:25 +00003222/*
3223** Move the cursor down to the right-most leaf entry beneath the
3224** page to which it is currently pointing. Notice the difference
3225** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3226** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3227** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:58 +00003228**
3229** The right-most entry is the one with the largest key - the last
3230** key in ascending order.
drh2dcc9aa2002-12-04 13:40:25 +00003231*/
3232static int moveToRightmost(BtCursor *pCur){
3233 Pgno pgno;
3234 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003235 MemPage *pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003236
danielk1977da184232006-01-05 11:34:32 +00003237 assert( pCur->eState==CURSOR_VALID );
drh3aac2dd2004-04-26 14:10:20 +00003238 while( !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:49 +00003239 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh3aac2dd2004-04-26 14:10:20 +00003240 pCur->idx = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00003241 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003242 if( rc ) return rc;
3243 }
drh3aac2dd2004-04-26 14:10:20 +00003244 pCur->idx = pPage->nCell - 1;
drh271efa52004-05-30 19:19:05 +00003245 pCur->info.nSize = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003246 return SQLITE_OK;
3247}
3248
drh5e00f6c2001-09-13 13:46:56 +00003249/* Move the cursor to the first entry in the table. Return SQLITE_OK
3250** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003251** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00003252*/
drh3aac2dd2004-04-26 14:10:20 +00003253int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00003254 int rc;
3255 rc = moveToRoot(pCur);
3256 if( rc ) return rc;
danielk1977da184232006-01-05 11:34:32 +00003257 if( pCur->eState==CURSOR_INVALID ){
drhc39e0002004-05-07 23:50:57 +00003258 assert( pCur->pPage->nCell==0 );
drh5e00f6c2001-09-13 13:46:56 +00003259 *pRes = 1;
3260 return SQLITE_OK;
3261 }
drhc39e0002004-05-07 23:50:57 +00003262 assert( pCur->pPage->nCell>0 );
drh5e00f6c2001-09-13 13:46:56 +00003263 *pRes = 0;
3264 rc = moveToLeftmost(pCur);
3265 return rc;
3266}
drh5e2f8b92001-05-28 00:41:15 +00003267
drh9562b552002-02-19 15:00:07 +00003268/* Move the cursor to the last entry in the table. Return SQLITE_OK
3269** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003270** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00003271*/
drh3aac2dd2004-04-26 14:10:20 +00003272int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00003273 int rc;
drh9562b552002-02-19 15:00:07 +00003274 rc = moveToRoot(pCur);
3275 if( rc ) return rc;
danielk1977da184232006-01-05 11:34:32 +00003276 if( CURSOR_INVALID==pCur->eState ){
drhc39e0002004-05-07 23:50:57 +00003277 assert( pCur->pPage->nCell==0 );
drh9562b552002-02-19 15:00:07 +00003278 *pRes = 1;
3279 return SQLITE_OK;
3280 }
danielk1977da184232006-01-05 11:34:32 +00003281 assert( pCur->eState==CURSOR_VALID );
drh9562b552002-02-19 15:00:07 +00003282 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003283 rc = moveToRightmost(pCur);
drh9562b552002-02-19 15:00:07 +00003284 return rc;
3285}
3286
drh3aac2dd2004-04-26 14:10:20 +00003287/* Move the cursor so that it points to an entry near pKey/nKey.
drh72f82862001-05-24 21:06:34 +00003288** Return a success code.
3289**
drh3aac2dd2004-04-26 14:10:20 +00003290** For INTKEY tables, only the nKey parameter is used. pKey is
3291** ignored. For other tables, nKey is the number of bytes of data
drh0b2f3162005-12-21 18:36:45 +00003292** in pKey. The comparison function specified when the cursor was
drh3aac2dd2004-04-26 14:10:20 +00003293** created is used to compare keys.
3294**
drh5e2f8b92001-05-28 00:41:15 +00003295** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00003296** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00003297** were present. The cursor might point to an entry that comes
3298** before or after the key.
3299**
drhbd03cae2001-06-02 02:40:57 +00003300** The result of comparing the key with the entry to which the
drhab01f612004-05-22 02:55:23 +00003301** cursor is written to *pRes if pRes!=NULL. The meaning of
drhbd03cae2001-06-02 02:40:57 +00003302** this value is as follows:
3303**
3304** *pRes<0 The cursor is left pointing at an entry that
drh1a844c32002-12-04 22:29:28 +00003305** is smaller than pKey or if the table is empty
3306** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00003307**
3308** *pRes==0 The cursor is left pointing at an entry that
3309** exactly matches pKey.
3310**
3311** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00003312** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00003313*/
drhe4d90812007-03-29 05:51:49 +00003314int sqlite3BtreeMoveto(
3315 BtCursor *pCur, /* The cursor to be moved */
3316 const void *pKey, /* The key content for indices. Not used by tables */
3317 i64 nKey, /* Size of pKey. Or the key for tables */
3318 int biasRight, /* If true, bias the search to the high end */
3319 int *pRes /* Search result flag */
3320){
drh72f82862001-05-24 21:06:34 +00003321 int rc;
drh5e2f8b92001-05-28 00:41:15 +00003322 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00003323 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00003324 assert( pCur->pPage );
3325 assert( pCur->pPage->isInit );
danielk1977da184232006-01-05 11:34:32 +00003326 if( pCur->eState==CURSOR_INVALID ){
drhf328bc82004-05-10 23:29:49 +00003327 *pRes = -1;
drhc39e0002004-05-07 23:50:57 +00003328 assert( pCur->pPage->nCell==0 );
3329 return SQLITE_OK;
3330 }
drh14684382006-11-30 13:05:29 +00003331 for(;;){
drh72f82862001-05-24 21:06:34 +00003332 int lwr, upr;
3333 Pgno chldPg;
3334 MemPage *pPage = pCur->pPage;
drh1a844c32002-12-04 22:29:28 +00003335 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00003336 lwr = 0;
3337 upr = pPage->nCell-1;
drh4eec4c12005-01-21 00:22:37 +00003338 if( !pPage->intKey && pKey==0 ){
drh49285702005-09-17 15:20:26 +00003339 return SQLITE_CORRUPT_BKPT;
drh4eec4c12005-01-21 00:22:37 +00003340 }
drhe4d90812007-03-29 05:51:49 +00003341 if( biasRight ){
3342 pCur->idx = upr;
3343 }else{
3344 pCur->idx = (upr+lwr)/2;
3345 }
drhf1d68b32007-03-29 04:43:26 +00003346 if( lwr<=upr ) for(;;){
danielk197713adf8a2004-06-03 16:08:41 +00003347 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00003348 i64 nCellKey;
drh366fda62006-01-13 02:35:09 +00003349 pCur->info.nSize = 0;
drh3aac2dd2004-04-26 14:10:20 +00003350 if( pPage->intKey ){
drh777e4c42006-01-13 04:31:58 +00003351 u8 *pCell;
drh777e4c42006-01-13 04:31:58 +00003352 pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
drhd172f862006-01-12 15:01:15 +00003353 if( pPage->hasData ){
danielk1977bab45c62006-01-16 15:14:27 +00003354 u32 dummy;
drhd172f862006-01-12 15:01:15 +00003355 pCell += getVarint32(pCell, &dummy);
3356 }
danielk1977bab45c62006-01-16 15:14:27 +00003357 getVarint(pCell, (u64 *)&nCellKey);
drh3aac2dd2004-04-26 14:10:20 +00003358 if( nCellKey<nKey ){
3359 c = -1;
3360 }else if( nCellKey>nKey ){
3361 c = +1;
3362 }else{
3363 c = 0;
3364 }
drh3aac2dd2004-04-26 14:10:20 +00003365 }else{
drhe51c44f2004-05-30 20:46:09 +00003366 int available;
danielk197713adf8a2004-06-03 16:08:41 +00003367 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drh366fda62006-01-13 02:35:09 +00003368 nCellKey = pCur->info.nKey;
drhe51c44f2004-05-30 20:46:09 +00003369 if( available>=nCellKey ){
3370 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3371 }else{
3372 pCellKey = sqliteMallocRaw( nCellKey );
3373 if( pCellKey==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00003374 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
drhe51c44f2004-05-30 20:46:09 +00003375 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3376 sqliteFree(pCellKey);
3377 if( rc ) return rc;
3378 }
drh3aac2dd2004-04-26 14:10:20 +00003379 }
drh72f82862001-05-24 21:06:34 +00003380 if( c==0 ){
drh8b18dd42004-05-12 19:18:15 +00003381 if( pPage->leafData && !pPage->leaf ){
drhfc70e6f2004-05-12 21:11:27 +00003382 lwr = pCur->idx;
3383 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00003384 break;
3385 }else{
drh8b18dd42004-05-12 19:18:15 +00003386 if( pRes ) *pRes = 0;
3387 return SQLITE_OK;
3388 }
drh72f82862001-05-24 21:06:34 +00003389 }
3390 if( c<0 ){
3391 lwr = pCur->idx+1;
3392 }else{
3393 upr = pCur->idx-1;
3394 }
drhf1d68b32007-03-29 04:43:26 +00003395 if( lwr>upr ){
3396 break;
3397 }
3398 pCur->idx = (lwr+upr)/2;
drh72f82862001-05-24 21:06:34 +00003399 }
3400 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00003401 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00003402 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00003403 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00003404 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00003405 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00003406 }else{
drh43605152004-05-29 21:46:49 +00003407 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00003408 }
3409 if( chldPg==0 ){
drhc39e0002004-05-07 23:50:57 +00003410 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00003411 if( pRes ) *pRes = c;
3412 return SQLITE_OK;
3413 }
drh428ae8c2003-01-04 16:48:09 +00003414 pCur->idx = lwr;
drh271efa52004-05-30 19:19:05 +00003415 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00003416 rc = moveToChild(pCur, chldPg);
drhc39e0002004-05-07 23:50:57 +00003417 if( rc ){
3418 return rc;
3419 }
drh72f82862001-05-24 21:06:34 +00003420 }
drhbd03cae2001-06-02 02:40:57 +00003421 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00003422}
3423
3424/*
drhc39e0002004-05-07 23:50:57 +00003425** Return TRUE if the cursor is not pointing at an entry of the table.
3426**
3427** TRUE will be returned after a call to sqlite3BtreeNext() moves
3428** past the last entry in the table or sqlite3BtreePrev() moves past
3429** the first entry. TRUE is also returned if the table is empty.
3430*/
3431int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00003432 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
3433 ** have been deleted? This API will need to change to return an error code
3434 ** as well as the boolean result value.
3435 */
3436 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:57 +00003437}
3438
3439/*
drhbd03cae2001-06-02 02:40:57 +00003440** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00003441** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00003442** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00003443** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00003444*/
drh3aac2dd2004-04-26 14:10:20 +00003445int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00003446 int rc;
danielk197797a227c2006-01-20 16:32:04 +00003447 MemPage *pPage;
drh8b18dd42004-05-12 19:18:15 +00003448
danielk1977da184232006-01-05 11:34:32 +00003449#ifndef SQLITE_OMIT_SHARED_CACHE
drh777e4c42006-01-13 04:31:58 +00003450 rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00003451 if( rc!=SQLITE_OK ){
3452 return rc;
3453 }
3454 if( pCur->skip>0 ){
3455 pCur->skip = 0;
3456 *pRes = 0;
3457 return SQLITE_OK;
3458 }
3459 pCur->skip = 0;
danielk197797a227c2006-01-20 16:32:04 +00003460#endif
danielk1977da184232006-01-05 11:34:32 +00003461
drh8c1238a2003-01-02 14:43:55 +00003462 assert( pRes!=0 );
danielk197797a227c2006-01-20 16:32:04 +00003463 pPage = pCur->pPage;
danielk1977da184232006-01-05 11:34:32 +00003464 if( CURSOR_INVALID==pCur->eState ){
drh8c1238a2003-01-02 14:43:55 +00003465 *pRes = 1;
drhc39e0002004-05-07 23:50:57 +00003466 return SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +00003467 }
drh8178a752003-01-05 21:41:40 +00003468 assert( pPage->isInit );
drh8178a752003-01-05 21:41:40 +00003469 assert( pCur->idx<pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00003470
drh72f82862001-05-24 21:06:34 +00003471 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00003472 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00003473 if( pCur->idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00003474 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003475 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00003476 if( rc ) return rc;
3477 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003478 *pRes = 0;
3479 return rc;
drh72f82862001-05-24 21:06:34 +00003480 }
drh5e2f8b92001-05-28 00:41:15 +00003481 do{
drh8856d6a2004-04-29 14:42:46 +00003482 if( isRootPage(pPage) ){
drh8c1238a2003-01-02 14:43:55 +00003483 *pRes = 1;
danielk1977da184232006-01-05 11:34:32 +00003484 pCur->eState = CURSOR_INVALID;
drh5e2f8b92001-05-28 00:41:15 +00003485 return SQLITE_OK;
3486 }
drh8178a752003-01-05 21:41:40 +00003487 moveToParent(pCur);
3488 pPage = pCur->pPage;
3489 }while( pCur->idx>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00003490 *pRes = 0;
drh8b18dd42004-05-12 19:18:15 +00003491 if( pPage->leafData ){
3492 rc = sqlite3BtreeNext(pCur, pRes);
3493 }else{
3494 rc = SQLITE_OK;
3495 }
3496 return rc;
drh8178a752003-01-05 21:41:40 +00003497 }
3498 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00003499 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00003500 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00003501 }
drh5e2f8b92001-05-28 00:41:15 +00003502 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003503 return rc;
drh72f82862001-05-24 21:06:34 +00003504}
3505
drh3b7511c2001-05-26 13:15:44 +00003506/*
drh2dcc9aa2002-12-04 13:40:25 +00003507** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00003508** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00003509** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00003510** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00003511*/
drh3aac2dd2004-04-26 14:10:20 +00003512int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00003513 int rc;
3514 Pgno pgno;
drh8178a752003-01-05 21:41:40 +00003515 MemPage *pPage;
danielk1977da184232006-01-05 11:34:32 +00003516
3517#ifndef SQLITE_OMIT_SHARED_CACHE
drh777e4c42006-01-13 04:31:58 +00003518 rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00003519 if( rc!=SQLITE_OK ){
3520 return rc;
3521 }
3522 if( pCur->skip<0 ){
3523 pCur->skip = 0;
3524 *pRes = 0;
3525 return SQLITE_OK;
3526 }
3527 pCur->skip = 0;
3528#endif
3529
3530 if( CURSOR_INVALID==pCur->eState ){
drhc39e0002004-05-07 23:50:57 +00003531 *pRes = 1;
3532 return SQLITE_OK;
3533 }
danielk19776a43f9b2004-11-16 04:57:24 +00003534
drh8178a752003-01-05 21:41:40 +00003535 pPage = pCur->pPage;
drh8178a752003-01-05 21:41:40 +00003536 assert( pPage->isInit );
drh2dcc9aa2002-12-04 13:40:25 +00003537 assert( pCur->idx>=0 );
drha34b6762004-05-07 13:30:42 +00003538 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003539 pgno = get4byte( findCell(pPage, pCur->idx) );
drh8178a752003-01-05 21:41:40 +00003540 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003541 if( rc ) return rc;
3542 rc = moveToRightmost(pCur);
3543 }else{
3544 while( pCur->idx==0 ){
drh8856d6a2004-04-29 14:42:46 +00003545 if( isRootPage(pPage) ){
danielk1977da184232006-01-05 11:34:32 +00003546 pCur->eState = CURSOR_INVALID;
drhc39e0002004-05-07 23:50:57 +00003547 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00003548 return SQLITE_OK;
3549 }
drh8178a752003-01-05 21:41:40 +00003550 moveToParent(pCur);
3551 pPage = pCur->pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003552 }
3553 pCur->idx--;
drh271efa52004-05-30 19:19:05 +00003554 pCur->info.nSize = 0;
drh8237d452004-11-22 19:07:09 +00003555 if( pPage->leafData && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00003556 rc = sqlite3BtreePrevious(pCur, pRes);
3557 }else{
3558 rc = SQLITE_OK;
3559 }
drh2dcc9aa2002-12-04 13:40:25 +00003560 }
drh8178a752003-01-05 21:41:40 +00003561 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003562 return rc;
3563}
3564
3565/*
drh3b7511c2001-05-26 13:15:44 +00003566** Allocate a new page from the database file.
3567**
danielk19773b8a05f2007-03-19 17:44:26 +00003568** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:44 +00003569** has already been called on the new page.) The new page has also
3570** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:26 +00003571** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00003572**
3573** SQLITE_OK is returned on success. Any other return value indicates
3574** an error. *ppPage and *pPgno are undefined in the event of an error.
danielk19773b8a05f2007-03-19 17:44:26 +00003575** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00003576**
drh199e3cf2002-07-18 11:01:47 +00003577** If the "nearby" parameter is not 0, then a (feeble) effort is made to
3578** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00003579** attempt to keep related pages close to each other in the database file,
3580** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00003581**
3582** If the "exact" parameter is not 0, and the page-number nearby exists
3583** anywhere on the free-list, then it is guarenteed to be returned. This
3584** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00003585*/
drh4f0c5872007-03-26 22:05:01 +00003586static int allocateBtreePage(
danielk1977aef0bf62005-12-30 16:28:01 +00003587 BtShared *pBt,
danielk1977cb1a7eb2004-11-05 12:27:02 +00003588 MemPage **ppPage,
3589 Pgno *pPgno,
3590 Pgno nearby,
3591 u8 exact
3592){
drh3aac2dd2004-04-26 14:10:20 +00003593 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00003594 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003595 int n; /* Number of pages on the freelist */
3596 int k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:51 +00003597 MemPage *pTrunk = 0;
3598 MemPage *pPrevTrunk = 0;
drh30e58752002-03-02 20:41:57 +00003599
drh3aac2dd2004-04-26 14:10:20 +00003600 pPage1 = pBt->pPage1;
3601 n = get4byte(&pPage1->aData[36]);
3602 if( n>0 ){
drh91025292004-05-03 19:49:32 +00003603 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00003604 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003605 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
3606
3607 /* If the 'exact' parameter was true and a query of the pointer-map
3608 ** shows that the page 'nearby' is somewhere on the free-list, then
3609 ** the entire-list will be searched for that page.
3610 */
3611#ifndef SQLITE_OMIT_AUTOVACUUM
3612 if( exact ){
3613 u8 eType;
3614 assert( nearby>0 );
3615 assert( pBt->autoVacuum );
3616 rc = ptrmapGet(pBt, nearby, &eType, 0);
3617 if( rc ) return rc;
3618 if( eType==PTRMAP_FREEPAGE ){
3619 searchList = 1;
3620 }
3621 *pPgno = nearby;
3622 }
3623#endif
3624
3625 /* Decrement the free-list count by 1. Set iTrunk to the index of the
3626 ** first free-list trunk page. iPrevTrunk is initially 1.
3627 */
danielk19773b8a05f2007-03-19 17:44:26 +00003628 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:44 +00003629 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003630 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003631
3632 /* The code within this loop is run only once if the 'searchList' variable
3633 ** is not true. Otherwise, it runs once for each trunk-page on the
3634 ** free-list until the page 'nearby' is located.
3635 */
3636 do {
3637 pPrevTrunk = pTrunk;
3638 if( pPrevTrunk ){
3639 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00003640 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00003641 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00003642 }
drh0787db62007-03-04 13:15:27 +00003643 rc = getPage(pBt, iTrunk, &pTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003644 if( rc ){
drhd3627af2006-12-18 18:34:51 +00003645 pTrunk = 0;
3646 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003647 }
3648
3649 k = get4byte(&pTrunk->aData[4]);
3650 if( k==0 && !searchList ){
3651 /* The trunk has no leaves and the list is not being searched.
3652 ** So extract the trunk page itself and use it as the newly
3653 ** allocated page */
3654 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003655 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003656 if( rc ){
3657 goto end_allocate_page;
3658 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003659 *pPgno = iTrunk;
3660 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3661 *ppPage = pTrunk;
3662 pTrunk = 0;
3663 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3664 }else if( k>pBt->usableSize/4 - 8 ){
3665 /* Value of k is out of range. Database corruption */
drhd3627af2006-12-18 18:34:51 +00003666 rc = SQLITE_CORRUPT_BKPT;
3667 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003668#ifndef SQLITE_OMIT_AUTOVACUUM
3669 }else if( searchList && nearby==iTrunk ){
3670 /* The list is being searched and this trunk page is the page
3671 ** to allocate, regardless of whether it has leaves.
3672 */
3673 assert( *pPgno==iTrunk );
3674 *ppPage = pTrunk;
3675 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003676 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003677 if( rc ){
3678 goto end_allocate_page;
3679 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003680 if( k==0 ){
3681 if( !pPrevTrunk ){
3682 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3683 }else{
3684 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
3685 }
3686 }else{
3687 /* The trunk page is required by the caller but it contains
3688 ** pointers to free-list leaves. The first leaf becomes a trunk
3689 ** page in this case.
3690 */
3691 MemPage *pNewTrunk;
3692 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
drh0787db62007-03-04 13:15:27 +00003693 rc = getPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003694 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:51 +00003695 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003696 }
danielk19773b8a05f2007-03-19 17:44:26 +00003697 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003698 if( rc!=SQLITE_OK ){
3699 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:51 +00003700 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003701 }
3702 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
3703 put4byte(&pNewTrunk->aData[4], k-1);
3704 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:51 +00003705 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003706 if( !pPrevTrunk ){
3707 put4byte(&pPage1->aData[32], iNewTrunk);
3708 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003709 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003710 if( rc ){
3711 goto end_allocate_page;
3712 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003713 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
3714 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003715 }
3716 pTrunk = 0;
3717 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3718#endif
3719 }else{
3720 /* Extract a leaf from the trunk */
3721 int closest;
3722 Pgno iPage;
3723 unsigned char *aData = pTrunk->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00003724 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003725 if( rc ){
3726 goto end_allocate_page;
3727 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003728 if( nearby>0 ){
3729 int i, dist;
3730 closest = 0;
3731 dist = get4byte(&aData[8]) - nearby;
3732 if( dist<0 ) dist = -dist;
3733 for(i=1; i<k; i++){
3734 int d2 = get4byte(&aData[8+i*4]) - nearby;
3735 if( d2<0 ) d2 = -d2;
3736 if( d2<dist ){
3737 closest = i;
3738 dist = d2;
3739 }
3740 }
3741 }else{
3742 closest = 0;
3743 }
3744
3745 iPage = get4byte(&aData[8+closest*4]);
3746 if( !searchList || iPage==nearby ){
3747 *pPgno = iPage;
danielk19773b8a05f2007-03-19 17:44:26 +00003748 if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00003749 /* Free page off the end of the file */
drh49285702005-09-17 15:20:26 +00003750 return SQLITE_CORRUPT_BKPT;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003751 }
3752 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
3753 ": %d more free pages\n",
3754 *pPgno, closest+1, k, pTrunk->pgno, n-1));
3755 if( closest<k-1 ){
3756 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
3757 }
3758 put4byte(&aData[4], k-1);
drh0787db62007-03-04 13:15:27 +00003759 rc = getPage(pBt, *pPgno, ppPage, 1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003760 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003761 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00003762 if( rc!=SQLITE_OK ){
3763 releasePage(*ppPage);
3764 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003765 }
3766 searchList = 0;
3767 }
drhee696e22004-08-30 16:52:17 +00003768 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003769 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:51 +00003770 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003771 }while( searchList );
drh3b7511c2001-05-26 13:15:44 +00003772 }else{
drh3aac2dd2004-04-26 14:10:20 +00003773 /* There are no pages on the freelist, so create a new page at the
3774 ** end of the file */
danielk19773b8a05f2007-03-19 17:44:26 +00003775 *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
danielk1977afcdd022004-10-31 16:25:42 +00003776
3777#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977266664d2006-02-10 08:24:21 +00003778 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00003779 /* If *pPgno refers to a pointer-map page, allocate two new pages
3780 ** at the end of the file instead of one. The first allocated page
3781 ** becomes a new pointer-map page, the second is used by the caller.
3782 */
3783 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00003784 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00003785 (*pPgno)++;
3786 }
3787#endif
3788
danielk1977599fcba2004-11-08 07:13:13 +00003789 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh0787db62007-03-04 13:15:27 +00003790 rc = getPage(pBt, *pPgno, ppPage, 0);
drh3b7511c2001-05-26 13:15:44 +00003791 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00003792 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00003793 if( rc!=SQLITE_OK ){
3794 releasePage(*ppPage);
3795 }
drh3a4c1412004-05-09 20:40:11 +00003796 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00003797 }
danielk1977599fcba2004-11-08 07:13:13 +00003798
3799 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:51 +00003800
3801end_allocate_page:
3802 releasePage(pTrunk);
3803 releasePage(pPrevTrunk);
drh3b7511c2001-05-26 13:15:44 +00003804 return rc;
3805}
3806
3807/*
drh3aac2dd2004-04-26 14:10:20 +00003808** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00003809**
danielk19773b8a05f2007-03-19 17:44:26 +00003810** sqlite3PagerUnref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00003811*/
drh3aac2dd2004-04-26 14:10:20 +00003812static int freePage(MemPage *pPage){
danielk1977aef0bf62005-12-30 16:28:01 +00003813 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00003814 MemPage *pPage1 = pBt->pPage1;
3815 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00003816
drh3aac2dd2004-04-26 14:10:20 +00003817 /* Prepare the page for freeing */
3818 assert( pPage->pgno>1 );
3819 pPage->isInit = 0;
3820 releasePage(pPage->pParent);
3821 pPage->pParent = 0;
3822
drha34b6762004-05-07 13:30:42 +00003823 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:26 +00003824 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003825 if( rc ) return rc;
3826 n = get4byte(&pPage1->aData[36]);
3827 put4byte(&pPage1->aData[36], n+1);
3828
drhfcce93f2006-02-22 03:08:32 +00003829#ifdef SQLITE_SECURE_DELETE
3830 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
3831 ** always fully overwrite deleted information with zeros.
3832 */
danielk19773b8a05f2007-03-19 17:44:26 +00003833 rc = sqlite3PagerWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00003834 if( rc ) return rc;
3835 memset(pPage->aData, 0, pPage->pBt->pageSize);
3836#endif
3837
danielk1977687566d2004-11-02 12:56:41 +00003838#ifndef SQLITE_OMIT_AUTOVACUUM
3839 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00003840 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00003841 */
3842 if( pBt->autoVacuum ){
3843 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
danielk1977a64a0352004-11-05 01:45:13 +00003844 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00003845 }
3846#endif
3847
drh3aac2dd2004-04-26 14:10:20 +00003848 if( n==0 ){
3849 /* This is the first free page */
danielk19773b8a05f2007-03-19 17:44:26 +00003850 rc = sqlite3PagerWrite(pPage->pDbPage);
drhda200cc2004-05-09 11:51:38 +00003851 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003852 memset(pPage->aData, 0, 8);
drha34b6762004-05-07 13:30:42 +00003853 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003854 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003855 }else{
3856 /* Other free pages already exist. Retrive the first trunk page
3857 ** of the freelist and find out how many leaves it has. */
drha34b6762004-05-07 13:30:42 +00003858 MemPage *pTrunk;
drh0787db62007-03-04 13:15:27 +00003859 rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
drh3b7511c2001-05-26 13:15:44 +00003860 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003861 k = get4byte(&pTrunk->aData[4]);
drhee696e22004-08-30 16:52:17 +00003862 if( k>=pBt->usableSize/4 - 8 ){
drh3aac2dd2004-04-26 14:10:20 +00003863 /* The trunk is full. Turn the page being freed into a new
3864 ** trunk page with no leaves. */
danielk19773b8a05f2007-03-19 17:44:26 +00003865 rc = sqlite3PagerWrite(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003866 if( rc ) return rc;
3867 put4byte(pPage->aData, pTrunk->pgno);
3868 put4byte(&pPage->aData[4], 0);
3869 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003870 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
3871 pPage->pgno, pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003872 }else{
3873 /* Add the newly freed page as a leaf on the current trunk */
danielk19773b8a05f2007-03-19 17:44:26 +00003874 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003875 if( rc ) return rc;
3876 put4byte(&pTrunk->aData[4], k+1);
3877 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drhfcce93f2006-02-22 03:08:32 +00003878#ifndef SQLITE_SECURE_DELETE
danielk19773b8a05f2007-03-19 17:44:26 +00003879 sqlite3PagerDontWrite(pBt->pPager, pPage->pgno);
drhfcce93f2006-02-22 03:08:32 +00003880#endif
drh3a4c1412004-05-09 20:40:11 +00003881 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003882 }
3883 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003884 }
drh3b7511c2001-05-26 13:15:44 +00003885 return rc;
3886}
3887
3888/*
drh3aac2dd2004-04-26 14:10:20 +00003889** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00003890*/
drh3aac2dd2004-04-26 14:10:20 +00003891static int clearCell(MemPage *pPage, unsigned char *pCell){
danielk1977aef0bf62005-12-30 16:28:01 +00003892 BtShared *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00003893 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00003894 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00003895 int rc;
drh94440812007-03-06 11:42:19 +00003896 int nOvfl;
3897 int ovflPageSize;
drh3b7511c2001-05-26 13:15:44 +00003898
drh43605152004-05-29 21:46:49 +00003899 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003900 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00003901 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00003902 }
drh6f11bef2004-05-13 01:12:56 +00003903 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh94440812007-03-06 11:42:19 +00003904 ovflPageSize = pBt->usableSize - 4;
drh72365832007-03-06 15:53:44 +00003905 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
3906 assert( ovflPgno==0 || nOvfl>0 );
3907 while( nOvfl-- ){
drh3aac2dd2004-04-26 14:10:20 +00003908 MemPage *pOvfl;
danielk19773b8a05f2007-03-19 17:44:26 +00003909 if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00003910 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00003911 }
drh72365832007-03-06 15:53:44 +00003912 rc = getPage(pBt, ovflPgno, &pOvfl, 0);
drh3b7511c2001-05-26 13:15:44 +00003913 if( rc ) return rc;
drh72365832007-03-06 15:53:44 +00003914 if( nOvfl ){
3915 ovflPgno = get4byte(pOvfl->aData);
3916 }
drha34b6762004-05-07 13:30:42 +00003917 rc = freePage(pOvfl);
danielk19773b8a05f2007-03-19 17:44:26 +00003918 sqlite3PagerUnref(pOvfl->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00003919 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00003920 }
drh5e2f8b92001-05-28 00:41:15 +00003921 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00003922}
3923
3924/*
drh91025292004-05-03 19:49:32 +00003925** Create the byte sequence used to represent a cell on page pPage
3926** and write that byte sequence into pCell[]. Overflow pages are
3927** allocated and filled in as necessary. The calling procedure
3928** is responsible for making sure sufficient space has been allocated
3929** for pCell[].
3930**
3931** Note that pCell does not necessary need to point to the pPage->aData
3932** area. pCell might point to some temporary storage. The cell will
3933** be constructed in this temporary area then copied into pPage->aData
3934** later.
drh3b7511c2001-05-26 13:15:44 +00003935*/
3936static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00003937 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00003938 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00003939 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00003940 const void *pData,int nData, /* The data */
3941 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00003942){
drh3b7511c2001-05-26 13:15:44 +00003943 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00003944 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00003945 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00003946 int spaceLeft;
3947 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00003948 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00003949 unsigned char *pPrior;
3950 unsigned char *pPayload;
danielk1977aef0bf62005-12-30 16:28:01 +00003951 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00003952 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00003953 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00003954 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00003955
drh91025292004-05-03 19:49:32 +00003956 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00003957 nHeader = 0;
drh91025292004-05-03 19:49:32 +00003958 if( !pPage->leaf ){
3959 nHeader += 4;
3960 }
drh8b18dd42004-05-12 19:18:15 +00003961 if( pPage->hasData ){
drh91025292004-05-03 19:49:32 +00003962 nHeader += putVarint(&pCell[nHeader], nData);
drh6f11bef2004-05-13 01:12:56 +00003963 }else{
drh91025292004-05-03 19:49:32 +00003964 nData = 0;
3965 }
drh6f11bef2004-05-13 01:12:56 +00003966 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh43605152004-05-29 21:46:49 +00003967 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003968 assert( info.nHeader==nHeader );
3969 assert( info.nKey==nKey );
3970 assert( info.nData==nData );
3971
3972 /* Fill in the payload */
drh3aac2dd2004-04-26 14:10:20 +00003973 nPayload = nData;
3974 if( pPage->intKey ){
3975 pSrc = pData;
3976 nSrc = nData;
drh91025292004-05-03 19:49:32 +00003977 nData = 0;
drh3aac2dd2004-04-26 14:10:20 +00003978 }else{
3979 nPayload += nKey;
3980 pSrc = pKey;
3981 nSrc = nKey;
3982 }
drh6f11bef2004-05-13 01:12:56 +00003983 *pnSize = info.nSize;
3984 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00003985 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00003986 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00003987
drh3b7511c2001-05-26 13:15:44 +00003988 while( nPayload>0 ){
3989 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00003990#ifndef SQLITE_OMIT_AUTOVACUUM
3991 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
3992#endif
drh4f0c5872007-03-26 22:05:01 +00003993 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00003994#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00003995 /* If the database supports auto-vacuum, and the second or subsequent
3996 ** overflow page is being allocated, add an entry to the pointer-map
3997 ** for that page now. The entry for the first overflow page will be
3998 ** added later, by the insertCell() routine.
danielk1977afcdd022004-10-31 16:25:42 +00003999 */
danielk1977a19df672004-11-03 11:37:07 +00004000 if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
4001 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap);
danielk1977afcdd022004-10-31 16:25:42 +00004002 }
4003#endif
drh3b7511c2001-05-26 13:15:44 +00004004 if( rc ){
drh9b171272004-05-08 02:03:22 +00004005 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004006 return rc;
4007 }
drh3aac2dd2004-04-26 14:10:20 +00004008 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00004009 releasePage(pToRelease);
4010 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00004011 pPrior = pOvfl->aData;
4012 put4byte(pPrior, 0);
4013 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00004014 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00004015 }
4016 n = nPayload;
4017 if( n>spaceLeft ) n = spaceLeft;
drh3aac2dd2004-04-26 14:10:20 +00004018 if( n>nSrc ) n = nSrc;
drhff3b1702006-03-11 12:04:18 +00004019 assert( pSrc );
drh3aac2dd2004-04-26 14:10:20 +00004020 memcpy(pPayload, pSrc, n);
drh3b7511c2001-05-26 13:15:44 +00004021 nPayload -= n;
drhde647132004-05-07 17:57:49 +00004022 pPayload += n;
drh9b171272004-05-08 02:03:22 +00004023 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00004024 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00004025 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00004026 if( nSrc==0 ){
4027 nSrc = nData;
4028 pSrc = pData;
4029 }
drhdd793422001-06-28 01:54:48 +00004030 }
drh9b171272004-05-08 02:03:22 +00004031 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004032 return SQLITE_OK;
4033}
4034
4035/*
drhbd03cae2001-06-02 02:40:57 +00004036** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00004037** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00004038** pointer in the third argument.
4039*/
danielk1977aef0bf62005-12-30 16:28:01 +00004040static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
drhbd03cae2001-06-02 02:40:57 +00004041 MemPage *pThis;
danielk19773b8a05f2007-03-19 17:44:26 +00004042 DbPage *pDbPage;
drhbd03cae2001-06-02 02:40:57 +00004043
drh43617e92006-03-06 20:55:46 +00004044 assert( pNewParent!=0 );
danielk1977afcdd022004-10-31 16:25:42 +00004045 if( pgno==0 ) return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004046 assert( pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004047 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
4048 if( pDbPage ){
4049 pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
drhda200cc2004-05-09 11:51:38 +00004050 if( pThis->isInit ){
danielk19773b8a05f2007-03-19 17:44:26 +00004051 assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) );
drhda200cc2004-05-09 11:51:38 +00004052 if( pThis->pParent!=pNewParent ){
danielk19773b8a05f2007-03-19 17:44:26 +00004053 if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004054 pThis->pParent = pNewParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004055 sqlite3PagerRef(pNewParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004056 }
4057 pThis->idxParent = idx;
drhdd793422001-06-28 01:54:48 +00004058 }
danielk19773b8a05f2007-03-19 17:44:26 +00004059 sqlite3PagerUnref(pDbPage);
drhbd03cae2001-06-02 02:40:57 +00004060 }
danielk1977afcdd022004-10-31 16:25:42 +00004061
4062#ifndef SQLITE_OMIT_AUTOVACUUM
4063 if( pBt->autoVacuum ){
4064 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
4065 }
4066#endif
4067 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00004068}
4069
danielk1977ac11ee62005-01-15 12:45:51 +00004070
4071
drhbd03cae2001-06-02 02:40:57 +00004072/*
drh4b70f112004-05-02 21:12:19 +00004073** Change the pParent pointer of all children of pPage to point back
4074** to pPage.
4075**
drhbd03cae2001-06-02 02:40:57 +00004076** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00004077** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00004078**
4079** This routine gets called after you memcpy() one page into
4080** another.
4081*/
danielk1977afcdd022004-10-31 16:25:42 +00004082static int reparentChildPages(MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00004083 int i;
danielk1977aef0bf62005-12-30 16:28:01 +00004084 BtShared *pBt = pPage->pBt;
danielk1977afcdd022004-10-31 16:25:42 +00004085 int rc = SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004086
danielk1977afcdd022004-10-31 16:25:42 +00004087 if( pPage->leaf ) return SQLITE_OK;
danielk1977afcdd022004-10-31 16:25:42 +00004088
drhbd03cae2001-06-02 02:40:57 +00004089 for(i=0; i<pPage->nCell; i++){
danielk1977afcdd022004-10-31 16:25:42 +00004090 u8 *pCell = findCell(pPage, i);
4091 if( !pPage->leaf ){
4092 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
4093 if( rc!=SQLITE_OK ) return rc;
4094 }
drhbd03cae2001-06-02 02:40:57 +00004095 }
danielk1977afcdd022004-10-31 16:25:42 +00004096 if( !pPage->leaf ){
4097 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
4098 pPage, i);
4099 pPage->idxShift = 0;
4100 }
4101 return rc;
drh14acc042001-06-10 19:56:58 +00004102}
4103
4104/*
4105** Remove the i-th cell from pPage. This routine effects pPage only.
4106** The cell content is not freed or deallocated. It is assumed that
4107** the cell content has been copied someplace else. This routine just
4108** removes the reference to the cell from pPage.
4109**
4110** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00004111*/
drh4b70f112004-05-02 21:12:19 +00004112static void dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00004113 int i; /* Loop counter */
4114 int pc; /* Offset to cell content of cell being deleted */
4115 u8 *data; /* pPage->aData */
4116 u8 *ptr; /* Used to move bytes around within data[] */
4117
drh8c42ca92001-06-22 19:15:00 +00004118 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004119 assert( sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:26 +00004120 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drhda200cc2004-05-09 11:51:38 +00004121 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00004122 ptr = &data[pPage->cellOffset + 2*idx];
4123 pc = get2byte(ptr);
4124 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
drhde647132004-05-07 17:57:49 +00004125 freeSpace(pPage, pc, sz);
drh43605152004-05-29 21:46:49 +00004126 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4127 ptr[0] = ptr[2];
4128 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00004129 }
4130 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00004131 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4132 pPage->nFree += 2;
drh428ae8c2003-01-04 16:48:09 +00004133 pPage->idxShift = 1;
drh14acc042001-06-10 19:56:58 +00004134}
4135
4136/*
4137** Insert a new cell on pPage at cell index "i". pCell points to the
4138** content of the cell.
4139**
4140** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00004141** will not fit, then make a copy of the cell content into pTemp if
4142** pTemp is not null. Regardless of pTemp, allocate a new entry
4143** in pPage->aOvfl[] and make it point to the cell content (either
4144** in pTemp or the original pCell) and also record its index.
4145** Allocating a new entry in pPage->aCell[] implies that
4146** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00004147**
4148** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4149** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00004150** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00004151** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00004152*/
danielk1977e80463b2004-11-03 03:01:16 +00004153static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00004154 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00004155 int i, /* New cell becomes the i-th cell of the page */
4156 u8 *pCell, /* Content of the new cell */
4157 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00004158 u8 *pTemp, /* Temp storage space for pCell, if needed */
4159 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00004160){
drh43605152004-05-29 21:46:49 +00004161 int idx; /* Where to write new cell content in data[] */
4162 int j; /* Loop counter */
4163 int top; /* First byte of content for any cell in data[] */
4164 int end; /* First byte past the last cell pointer in data[] */
4165 int ins; /* Index in data[] where new cell pointer is inserted */
4166 int hdr; /* Offset into data[] of the page header */
4167 int cellOffset; /* Address of first cell pointer in data[] */
4168 u8 *data; /* The content of the whole page */
4169 u8 *ptr; /* Used for moving information around in data[] */
4170
4171 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
4172 assert( sz==cellSizePtr(pPage, pCell) );
danielk19773b8a05f2007-03-19 17:44:26 +00004173 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004174 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00004175 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00004176 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004177 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00004178 }
drh43605152004-05-29 21:46:49 +00004179 j = pPage->nOverflow++;
4180 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
4181 pPage->aOvfl[j].pCell = pCell;
4182 pPage->aOvfl[j].idx = i;
4183 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00004184 }else{
drh43605152004-05-29 21:46:49 +00004185 data = pPage->aData;
4186 hdr = pPage->hdrOffset;
4187 top = get2byte(&data[hdr+5]);
4188 cellOffset = pPage->cellOffset;
4189 end = cellOffset + 2*pPage->nCell + 2;
4190 ins = cellOffset + 2*i;
4191 if( end > top - sz ){
danielk19776b456a22005-03-21 04:04:02 +00004192 int rc = defragmentPage(pPage);
4193 if( rc!=SQLITE_OK ) return rc;
drh43605152004-05-29 21:46:49 +00004194 top = get2byte(&data[hdr+5]);
4195 assert( end + sz <= top );
4196 }
4197 idx = allocateSpace(pPage, sz);
4198 assert( idx>0 );
4199 assert( end <= get2byte(&data[hdr+5]) );
4200 pPage->nCell++;
4201 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00004202 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004203 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4204 ptr[0] = ptr[-2];
4205 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00004206 }
drh43605152004-05-29 21:46:49 +00004207 put2byte(&data[ins], idx);
4208 put2byte(&data[hdr+3], pPage->nCell);
4209 pPage->idxShift = 1;
danielk1977a19df672004-11-03 11:37:07 +00004210#ifndef SQLITE_OMIT_AUTOVACUUM
4211 if( pPage->pBt->autoVacuum ){
4212 /* The cell may contain a pointer to an overflow page. If so, write
4213 ** the entry for the overflow page into the pointer map.
4214 */
4215 CellInfo info;
4216 parseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00004217 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk1977a19df672004-11-03 11:37:07 +00004218 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4219 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
4220 int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
4221 if( rc!=SQLITE_OK ) return rc;
4222 }
4223 }
4224#endif
drh14acc042001-06-10 19:56:58 +00004225 }
danielk1977e80463b2004-11-03 03:01:16 +00004226
danielk1977e80463b2004-11-03 03:01:16 +00004227 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004228}
4229
4230/*
drhfa1a98a2004-05-14 19:08:17 +00004231** Add a list of cells to a page. The page should be initially empty.
4232** The cells are guaranteed to fit on the page.
4233*/
4234static void assemblePage(
4235 MemPage *pPage, /* The page to be assemblied */
4236 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00004237 u8 **apCell, /* Pointers to cell bodies */
drhfa1a98a2004-05-14 19:08:17 +00004238 int *aSize /* Sizes of the cells */
4239){
4240 int i; /* Loop counter */
4241 int totalSize; /* Total size of all cells */
4242 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00004243 int cellptr; /* Address of next cell pointer */
4244 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00004245 u8 *data; /* Data for the page */
4246
drh43605152004-05-29 21:46:49 +00004247 assert( pPage->nOverflow==0 );
drhfa1a98a2004-05-14 19:08:17 +00004248 totalSize = 0;
4249 for(i=0; i<nCell; i++){
4250 totalSize += aSize[i];
4251 }
drh43605152004-05-29 21:46:49 +00004252 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00004253 assert( pPage->nCell==0 );
drh43605152004-05-29 21:46:49 +00004254 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00004255 data = pPage->aData;
4256 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00004257 put2byte(&data[hdr+3], nCell);
drh09d0deb2005-08-02 17:13:09 +00004258 if( nCell ){
4259 cellbody = allocateSpace(pPage, totalSize);
4260 assert( cellbody>0 );
4261 assert( pPage->nFree >= 2*nCell );
4262 pPage->nFree -= 2*nCell;
4263 for(i=0; i<nCell; i++){
4264 put2byte(&data[cellptr], cellbody);
4265 memcpy(&data[cellbody], apCell[i], aSize[i]);
4266 cellptr += 2;
4267 cellbody += aSize[i];
4268 }
4269 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00004270 }
4271 pPage->nCell = nCell;
drhfa1a98a2004-05-14 19:08:17 +00004272}
4273
drh14acc042001-06-10 19:56:58 +00004274/*
drhc3b70572003-01-04 19:44:07 +00004275** The following parameters determine how many adjacent pages get involved
4276** in a balancing operation. NN is the number of neighbors on either side
4277** of the page that participate in the balancing operation. NB is the
4278** total number of pages that participate, including the target page and
4279** NN neighbors on either side.
4280**
4281** The minimum value of NN is 1 (of course). Increasing NN above 1
4282** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4283** in exchange for a larger degradation in INSERT and UPDATE performance.
4284** The value of NN appears to give the best results overall.
4285*/
4286#define NN 1 /* Number of neighbors on either side of pPage */
4287#define NB (NN*2+1) /* Total pages involved in the balance */
4288
drh43605152004-05-29 21:46:49 +00004289/* Forward reference */
danielk1977ac245ec2005-01-14 13:50:11 +00004290static int balance(MemPage*, int);
4291
drh615ae552005-01-16 23:21:00 +00004292#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004293/*
4294** This version of balance() handles the common special case where
4295** a new entry is being inserted on the extreme right-end of the
4296** tree, in other words, when the new entry will become the largest
4297** entry in the tree.
4298**
4299** Instead of trying balance the 3 right-most leaf pages, just add
4300** a new page to the right-hand side and put the one new entry in
4301** that page. This leaves the right side of the tree somewhat
4302** unbalanced. But odds are that we will be inserting new entries
4303** at the end soon afterwards so the nearly empty page will quickly
4304** fill up. On average.
4305**
4306** pPage is the leaf page which is the right-most page in the tree.
4307** pParent is its parent. pPage must have a single overflow entry
4308** which is also the right-most entry on the page.
4309*/
danielk1977ac245ec2005-01-14 13:50:11 +00004310static int balance_quick(MemPage *pPage, MemPage *pParent){
4311 int rc;
4312 MemPage *pNew;
4313 Pgno pgnoNew;
4314 u8 *pCell;
4315 int szCell;
4316 CellInfo info;
danielk1977aef0bf62005-12-30 16:28:01 +00004317 BtShared *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00004318 int parentIdx = pParent->nCell; /* pParent new divider cell index */
4319 int parentSize; /* Size of new divider cell */
4320 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00004321
4322 /* Allocate a new page. Insert the overflow cell from pPage
4323 ** into it. Then remove the overflow cell from pPage.
4324 */
drh4f0c5872007-03-26 22:05:01 +00004325 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977ac245ec2005-01-14 13:50:11 +00004326 if( rc!=SQLITE_OK ){
4327 return rc;
4328 }
4329 pCell = pPage->aOvfl[0].pCell;
4330 szCell = cellSizePtr(pPage, pCell);
4331 zeroPage(pNew, pPage->aData[0]);
4332 assemblePage(pNew, 1, &pCell, &szCell);
4333 pPage->nOverflow = 0;
4334
danielk197779a40da2005-01-16 08:00:01 +00004335 /* Set the parent of the newly allocated page to pParent. */
4336 pNew->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004337 sqlite3PagerRef(pParent->pDbPage);
danielk197779a40da2005-01-16 08:00:01 +00004338
danielk1977ac245ec2005-01-14 13:50:11 +00004339 /* pPage is currently the right-child of pParent. Change this
4340 ** so that the right-child is the new page allocated above and
danielk197779a40da2005-01-16 08:00:01 +00004341 ** pPage is the next-to-right child.
danielk1977ac245ec2005-01-14 13:50:11 +00004342 */
danielk1977ac11ee62005-01-15 12:45:51 +00004343 assert( pPage->nCell>0 );
danielk1977ac245ec2005-01-14 13:50:11 +00004344 parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info);
4345 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize);
4346 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004347 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004348 }
4349 assert( parentSize<64 );
4350 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
4351 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004352 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004353 }
4354 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
4355 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4356
danielk197779a40da2005-01-16 08:00:01 +00004357#ifndef SQLITE_OMIT_AUTOVACUUM
4358 /* If this is an auto-vacuum database, update the pointer map
4359 ** with entries for the new page, and any pointer from the
4360 ** cell on the page to an overflow page.
4361 */
danielk1977ac11ee62005-01-15 12:45:51 +00004362 if( pBt->autoVacuum ){
4363 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
4364 if( rc!=SQLITE_OK ){
4365 return rc;
4366 }
danielk197779a40da2005-01-16 08:00:01 +00004367 rc = ptrmapPutOvfl(pNew, 0);
4368 if( rc!=SQLITE_OK ){
4369 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00004370 }
4371 }
danielk197779a40da2005-01-16 08:00:01 +00004372#endif
danielk1977ac11ee62005-01-15 12:45:51 +00004373
danielk197779a40da2005-01-16 08:00:01 +00004374 /* Release the reference to the new page and balance the parent page,
4375 ** in case the divider cell inserted caused it to become overfull.
4376 */
danielk1977ac245ec2005-01-14 13:50:11 +00004377 releasePage(pNew);
4378 return balance(pParent, 0);
4379}
drh615ae552005-01-16 23:21:00 +00004380#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00004381
drhc3b70572003-01-04 19:44:07 +00004382/*
danielk1977ac11ee62005-01-15 12:45:51 +00004383** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
4384** if the database supports auto-vacuum or not. Because it is used
4385** within an expression that is an argument to another macro
4386** (sqliteMallocRaw), it is not possible to use conditional compilation.
4387** So, this macro is defined instead.
4388*/
4389#ifndef SQLITE_OMIT_AUTOVACUUM
4390#define ISAUTOVACUUM (pBt->autoVacuum)
4391#else
4392#define ISAUTOVACUUM 0
4393#endif
4394
4395/*
drhab01f612004-05-22 02:55:23 +00004396** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00004397** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00004398** Usually NN siblings on either side of pPage is used in the balancing,
4399** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00004400** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00004401** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00004402** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00004403**
drh0c6cc4e2004-06-15 02:13:26 +00004404** The number of siblings of pPage might be increased or decreased by one or
4405** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00004406** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00004407** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00004408** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00004409** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00004410**
drh8b2f49b2001-06-08 00:21:52 +00004411** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00004412** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00004413** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00004414** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00004415**
drh8c42ca92001-06-22 19:15:00 +00004416** In the course of balancing the siblings of pPage, the parent of pPage
4417** might become overfull or underfull. If that happens, then this routine
4418** is called recursively on the parent.
4419**
drh5e00f6c2001-09-13 13:46:56 +00004420** If this routine fails for any reason, it might leave the database
4421** in a corrupted state. So if this routine fails, the database should
4422** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00004423*/
drh43605152004-05-29 21:46:49 +00004424static int balance_nonroot(MemPage *pPage){
drh8b2f49b2001-06-08 00:21:52 +00004425 MemPage *pParent; /* The parent of pPage */
danielk1977aef0bf62005-12-30 16:28:01 +00004426 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:07 +00004427 int nCell = 0; /* Number of cells in apCell[] */
4428 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
drh8b2f49b2001-06-08 00:21:52 +00004429 int nOld; /* Number of pages in apOld[] */
4430 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00004431 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00004432 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00004433 int idx; /* Index of pPage in pParent->aCell[] */
4434 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00004435 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00004436 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00004437 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00004438 int usableSpace; /* Bytes in pPage beyond the header */
4439 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00004440 int subtotal; /* Subtotal of bytes in cells on one page */
drhb6f41482004-05-14 01:58:11 +00004441 int iSpace = 0; /* First unused byte of aSpace[] */
drhc3b70572003-01-04 19:44:07 +00004442 MemPage *apOld[NB]; /* pPage and up to two siblings */
4443 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00004444 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00004445 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
4446 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drh4b70f112004-05-02 21:12:19 +00004447 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00004448 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
4449 int szNew[NB+2]; /* Combined size of cells place on i-th page */
danielk197750f059b2005-03-29 02:54:03 +00004450 u8 **apCell = 0; /* All cells begin balanced */
drh2e38c322004-09-03 18:38:44 +00004451 int *szCell; /* Local size of all cells in apCell[] */
4452 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
4453 u8 *aSpace; /* Space to hold copies of dividers cells */
danielk19774e17d142005-01-16 09:06:33 +00004454#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00004455 u8 *aFrom = 0;
4456#endif
drh8b2f49b2001-06-08 00:21:52 +00004457
drh14acc042001-06-10 19:56:58 +00004458 /*
drh43605152004-05-29 21:46:49 +00004459 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00004460 */
drh3a4c1412004-05-09 20:40:11 +00004461 assert( pPage->isInit );
danielk19773b8a05f2007-03-19 17:44:26 +00004462 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh4b70f112004-05-02 21:12:19 +00004463 pBt = pPage->pBt;
drh14acc042001-06-10 19:56:58 +00004464 pParent = pPage->pParent;
drh43605152004-05-29 21:46:49 +00004465 assert( pParent );
danielk19773b8a05f2007-03-19 17:44:26 +00004466 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
danielk197707cb5602006-01-20 10:55:05 +00004467 return rc;
4468 }
drh43605152004-05-29 21:46:49 +00004469 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00004470
drh615ae552005-01-16 23:21:00 +00004471#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004472 /*
4473 ** A special case: If a new entry has just been inserted into a
4474 ** table (that is, a btree with integer keys and all data at the leaves)
drh09d0deb2005-08-02 17:13:09 +00004475 ** and the new entry is the right-most entry in the tree (it has the
drhf222e712005-01-14 22:55:49 +00004476 ** largest key) then use the special balance_quick() routine for
4477 ** balancing. balance_quick() is much faster and results in a tighter
4478 ** packing of data in the common case.
4479 */
danielk1977ac245ec2005-01-14 13:50:11 +00004480 if( pPage->leaf &&
4481 pPage->intKey &&
4482 pPage->leafData &&
4483 pPage->nOverflow==1 &&
4484 pPage->aOvfl[0].idx==pPage->nCell &&
danielk1977ac11ee62005-01-15 12:45:51 +00004485 pPage->pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00004486 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
4487 ){
danielk1977ac11ee62005-01-15 12:45:51 +00004488 /*
4489 ** TODO: Check the siblings to the left of pPage. It may be that
4490 ** they are not full and no new page is required.
4491 */
danielk1977ac245ec2005-01-14 13:50:11 +00004492 return balance_quick(pPage, pParent);
4493 }
4494#endif
4495
drh2e38c322004-09-03 18:38:44 +00004496 /*
drh4b70f112004-05-02 21:12:19 +00004497 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00004498 ** to pPage. The "idx" variable is the index of that cell. If pPage
4499 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00004500 */
drhbb49aba2003-01-04 18:53:27 +00004501 if( pParent->idxShift ){
drha34b6762004-05-07 13:30:42 +00004502 Pgno pgno;
drh4b70f112004-05-02 21:12:19 +00004503 pgno = pPage->pgno;
danielk19773b8a05f2007-03-19 17:44:26 +00004504 assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbb49aba2003-01-04 18:53:27 +00004505 for(idx=0; idx<pParent->nCell; idx++){
drh43605152004-05-29 21:46:49 +00004506 if( get4byte(findCell(pParent, idx))==pgno ){
drhbb49aba2003-01-04 18:53:27 +00004507 break;
4508 }
drh8b2f49b2001-06-08 00:21:52 +00004509 }
drh4b70f112004-05-02 21:12:19 +00004510 assert( idx<pParent->nCell
drh43605152004-05-29 21:46:49 +00004511 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
drhbb49aba2003-01-04 18:53:27 +00004512 }else{
4513 idx = pPage->idxParent;
drh8b2f49b2001-06-08 00:21:52 +00004514 }
drh8b2f49b2001-06-08 00:21:52 +00004515
4516 /*
drh14acc042001-06-10 19:56:58 +00004517 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00004518 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00004519 */
drh14acc042001-06-10 19:56:58 +00004520 nOld = nNew = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004521 sqlite3PagerRef(pParent->pDbPage);
drh14acc042001-06-10 19:56:58 +00004522
4523 /*
drh4b70f112004-05-02 21:12:19 +00004524 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00004525 ** the siblings. An attempt is made to find NN siblings on either
4526 ** side of pPage. More siblings are taken from one side, however, if
4527 ** pPage there are fewer than NN siblings on the other side. If pParent
4528 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00004529 */
drhc3b70572003-01-04 19:44:07 +00004530 nxDiv = idx - NN;
4531 if( nxDiv + NB > pParent->nCell ){
4532 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00004533 }
drhc3b70572003-01-04 19:44:07 +00004534 if( nxDiv<0 ){
4535 nxDiv = 0;
4536 }
drh8b2f49b2001-06-08 00:21:52 +00004537 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00004538 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00004539 if( k<pParent->nCell ){
drh43605152004-05-29 21:46:49 +00004540 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00004541 nDiv++;
drha34b6762004-05-07 13:30:42 +00004542 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00004543 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00004544 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00004545 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00004546 }else{
4547 break;
drh8b2f49b2001-06-08 00:21:52 +00004548 }
drhde647132004-05-07 17:57:49 +00004549 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00004550 if( rc ) goto balance_cleanup;
drh428ae8c2003-01-04 16:48:09 +00004551 apOld[i]->idxParent = k;
drh91025292004-05-03 19:49:32 +00004552 apCopy[i] = 0;
4553 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00004554 nOld++;
danielk1977634f2982005-03-28 08:44:07 +00004555 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
drh8b2f49b2001-06-08 00:21:52 +00004556 }
4557
drh8d97f1f2005-05-05 18:14:13 +00004558 /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
4559 ** alignment */
4560 nMaxCells = (nMaxCells + 1)&~1;
4561
drh8b2f49b2001-06-08 00:21:52 +00004562 /*
danielk1977634f2982005-03-28 08:44:07 +00004563 ** Allocate space for memory structures
4564 */
4565 apCell = sqliteMallocRaw(
4566 nMaxCells*sizeof(u8*) /* apCell */
4567 + nMaxCells*sizeof(int) /* szCell */
drhc96d8532005-05-03 12:30:33 +00004568 + ROUND8(sizeof(MemPage))*NB /* aCopy */
drh07d183d2005-05-01 22:52:42 +00004569 + pBt->pageSize*(5+NB) /* aSpace */
drhc96d8532005-05-03 12:30:33 +00004570 + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
danielk1977634f2982005-03-28 08:44:07 +00004571 );
4572 if( apCell==0 ){
4573 rc = SQLITE_NOMEM;
4574 goto balance_cleanup;
4575 }
4576 szCell = (int*)&apCell[nMaxCells];
4577 aCopy[0] = (u8*)&szCell[nMaxCells];
drhc96d8532005-05-03 12:30:33 +00004578 assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004579 for(i=1; i<NB; i++){
drhc96d8532005-05-03 12:30:33 +00004580 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4581 assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004582 }
drhc96d8532005-05-03 12:30:33 +00004583 aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4584 assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004585#ifndef SQLITE_OMIT_AUTOVACUUM
4586 if( pBt->autoVacuum ){
drh07d183d2005-05-01 22:52:42 +00004587 aFrom = &aSpace[5*pBt->pageSize];
danielk1977634f2982005-03-28 08:44:07 +00004588 }
4589#endif
4590
4591 /*
drh14acc042001-06-10 19:56:58 +00004592 ** Make copies of the content of pPage and its siblings into aOld[].
4593 ** The rest of this function will use data from the copies rather
4594 ** that the original pages since the original pages will be in the
4595 ** process of being overwritten.
4596 */
4597 for(i=0; i<nOld; i++){
drh07d183d2005-05-01 22:52:42 +00004598 MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
drh07d183d2005-05-01 22:52:42 +00004599 p->aData = &((u8*)p)[-pBt->pageSize];
4600 memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage));
4601 /* The memcpy() above changes the value of p->aData so we have to
4602 ** set it again. */
drh07d183d2005-05-01 22:52:42 +00004603 p->aData = &((u8*)p)[-pBt->pageSize];
drh14acc042001-06-10 19:56:58 +00004604 }
4605
4606 /*
4607 ** Load pointers to all cells on sibling pages and the divider cells
4608 ** into the local apCell[] array. Make copies of the divider cells
drhb6f41482004-05-14 01:58:11 +00004609 ** into space obtained form aSpace[] and remove the the divider Cells
4610 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00004611 **
4612 ** If the siblings are on leaf pages, then the child pointers of the
4613 ** divider cells are stripped from the cells before they are copied
drh96f5b762004-05-16 16:24:36 +00004614 ** into aSpace[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00004615 ** child pointers. If siblings are not leaves, then all cell in
4616 ** apCell[] include child pointers. Either way, all cells in apCell[]
4617 ** are alike.
drh96f5b762004-05-16 16:24:36 +00004618 **
4619 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
4620 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00004621 */
4622 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00004623 leafCorrection = pPage->leaf*4;
drh8b18dd42004-05-12 19:18:15 +00004624 leafData = pPage->leafData && pPage->leaf;
drh8b2f49b2001-06-08 00:21:52 +00004625 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00004626 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00004627 int limit = pOld->nCell+pOld->nOverflow;
4628 for(j=0; j<limit; j++){
danielk1977634f2982005-03-28 08:44:07 +00004629 assert( nCell<nMaxCells );
drh43605152004-05-29 21:46:49 +00004630 apCell[nCell] = findOverflowCell(pOld, j);
4631 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk1977ac11ee62005-01-15 12:45:51 +00004632#ifndef SQLITE_OMIT_AUTOVACUUM
4633 if( pBt->autoVacuum ){
4634 int a;
4635 aFrom[nCell] = i;
4636 for(a=0; a<pOld->nOverflow; a++){
4637 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
4638 aFrom[nCell] = 0xFF;
4639 break;
4640 }
4641 }
4642 }
4643#endif
drh14acc042001-06-10 19:56:58 +00004644 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00004645 }
4646 if( i<nOld-1 ){
drh43605152004-05-29 21:46:49 +00004647 int sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00004648 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00004649 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
4650 ** are duplicates of keys on the child pages. We need to remove
4651 ** the divider cells from pParent, but the dividers cells are not
4652 ** added to apCell[] because they are duplicates of child cells.
4653 */
drh8b18dd42004-05-12 19:18:15 +00004654 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00004655 }else{
drhb6f41482004-05-14 01:58:11 +00004656 u8 *pTemp;
danielk1977634f2982005-03-28 08:44:07 +00004657 assert( nCell<nMaxCells );
drhb6f41482004-05-14 01:58:11 +00004658 szCell[nCell] = sz;
4659 pTemp = &aSpace[iSpace];
4660 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00004661 assert( iSpace<=pBt->pageSize*5 );
drhb6f41482004-05-14 01:58:11 +00004662 memcpy(pTemp, apDiv[i], sz);
4663 apCell[nCell] = pTemp+leafCorrection;
danielk1977ac11ee62005-01-15 12:45:51 +00004664#ifndef SQLITE_OMIT_AUTOVACUUM
4665 if( pBt->autoVacuum ){
4666 aFrom[nCell] = 0xFF;
4667 }
4668#endif
drhb6f41482004-05-14 01:58:11 +00004669 dropCell(pParent, nxDiv, sz);
drh8b18dd42004-05-12 19:18:15 +00004670 szCell[nCell] -= leafCorrection;
drh43605152004-05-29 21:46:49 +00004671 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00004672 if( !pOld->leaf ){
4673 assert( leafCorrection==0 );
4674 /* The right pointer of the child page pOld becomes the left
4675 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00004676 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00004677 }else{
4678 assert( leafCorrection==4 );
4679 }
4680 nCell++;
drh4b70f112004-05-02 21:12:19 +00004681 }
drh8b2f49b2001-06-08 00:21:52 +00004682 }
4683 }
4684
4685 /*
drh6019e162001-07-02 17:51:45 +00004686 ** Figure out the number of pages needed to hold all nCell cells.
4687 ** Store this number in "k". Also compute szNew[] which is the total
4688 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00004689 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00004690 ** cntNew[k] should equal nCell.
4691 **
drh96f5b762004-05-16 16:24:36 +00004692 ** Values computed by this block:
4693 **
4694 ** k: The total number of sibling pages
4695 ** szNew[i]: Spaced used on the i-th sibling page.
4696 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
4697 ** the right of the i-th sibling page.
4698 ** usableSpace: Number of bytes of space available on each sibling.
4699 **
drh8b2f49b2001-06-08 00:21:52 +00004700 */
drh43605152004-05-29 21:46:49 +00004701 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00004702 for(subtotal=k=i=0; i<nCell; i++){
danielk1977634f2982005-03-28 08:44:07 +00004703 assert( i<nMaxCells );
drh43605152004-05-29 21:46:49 +00004704 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00004705 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00004706 szNew[k] = subtotal - szCell[i];
4707 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00004708 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00004709 subtotal = 0;
4710 k++;
4711 }
4712 }
4713 szNew[k] = subtotal;
4714 cntNew[k] = nCell;
4715 k++;
drh96f5b762004-05-16 16:24:36 +00004716
4717 /*
4718 ** The packing computed by the previous block is biased toward the siblings
4719 ** on the left side. The left siblings are always nearly full, while the
4720 ** right-most sibling might be nearly empty. This block of code attempts
4721 ** to adjust the packing of siblings to get a better balance.
4722 **
4723 ** This adjustment is more than an optimization. The packing above might
4724 ** be so out of balance as to be illegal. For example, the right-most
4725 ** sibling might be completely empty. This adjustment is not optional.
4726 */
drh6019e162001-07-02 17:51:45 +00004727 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00004728 int szRight = szNew[i]; /* Size of sibling on the right */
4729 int szLeft = szNew[i-1]; /* Size of sibling on the left */
4730 int r; /* Index of right-most cell in left sibling */
4731 int d; /* Index of first cell to the left of right sibling */
4732
4733 r = cntNew[i-1] - 1;
4734 d = r + 1 - leafData;
danielk1977634f2982005-03-28 08:44:07 +00004735 assert( d<nMaxCells );
4736 assert( r<nMaxCells );
drh43605152004-05-29 21:46:49 +00004737 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
4738 szRight += szCell[d] + 2;
4739 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00004740 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00004741 r = cntNew[i-1] - 1;
4742 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00004743 }
drh96f5b762004-05-16 16:24:36 +00004744 szNew[i] = szRight;
4745 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00004746 }
drh09d0deb2005-08-02 17:13:09 +00004747
4748 /* Either we found one or more cells (cntnew[0])>0) or we are the
4749 ** a virtual root page. A virtual root page is when the real root
4750 ** page is page 1 and we are the only child of that page.
4751 */
4752 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
drh8b2f49b2001-06-08 00:21:52 +00004753
4754 /*
drh6b308672002-07-08 02:16:37 +00004755 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00004756 */
drh4b70f112004-05-02 21:12:19 +00004757 assert( pPage->pgno>1 );
4758 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00004759 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00004760 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00004761 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00004762 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00004763 pgnoNew[i] = pgnoOld[i];
4764 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004765 rc = sqlite3PagerWrite(pNew->pDbPage);
danielk197728129562005-01-11 10:25:06 +00004766 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00004767 }else{
drh7aa8f852006-03-28 00:24:44 +00004768 assert( i>0 );
drh4f0c5872007-03-26 22:05:01 +00004769 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00004770 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00004771 apNew[i] = pNew;
drh6b308672002-07-08 02:16:37 +00004772 }
drh14acc042001-06-10 19:56:58 +00004773 nNew++;
drhda200cc2004-05-09 11:51:38 +00004774 zeroPage(pNew, pageFlags);
drh8b2f49b2001-06-08 00:21:52 +00004775 }
4776
danielk1977299b1872004-11-22 10:02:10 +00004777 /* Free any old pages that were not reused as new pages.
4778 */
4779 while( i<nOld ){
4780 rc = freePage(apOld[i]);
4781 if( rc ) goto balance_cleanup;
4782 releasePage(apOld[i]);
4783 apOld[i] = 0;
4784 i++;
4785 }
4786
drh8b2f49b2001-06-08 00:21:52 +00004787 /*
drhf9ffac92002-03-02 19:00:31 +00004788 ** Put the new pages in accending order. This helps to
4789 ** keep entries in the disk file in order so that a scan
4790 ** of the table is a linear scan through the file. That
4791 ** in turn helps the operating system to deliver pages
4792 ** from the disk more rapidly.
4793 **
4794 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00004795 ** n is never more than NB (a small constant), that should
4796 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00004797 **
drhc3b70572003-01-04 19:44:07 +00004798 ** When NB==3, this one optimization makes the database
4799 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00004800 */
4801 for(i=0; i<k-1; i++){
4802 int minV = pgnoNew[i];
4803 int minI = i;
4804 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00004805 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00004806 minI = j;
4807 minV = pgnoNew[j];
4808 }
4809 }
4810 if( minI>i ){
4811 int t;
4812 MemPage *pT;
4813 t = pgnoNew[i];
4814 pT = apNew[i];
4815 pgnoNew[i] = pgnoNew[minI];
4816 apNew[i] = apNew[minI];
4817 pgnoNew[minI] = t;
4818 apNew[minI] = pT;
4819 }
4820 }
drha2fce642004-06-05 00:01:44 +00004821 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00004822 pgnoOld[0],
4823 nOld>=2 ? pgnoOld[1] : 0,
4824 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00004825 pgnoNew[0], szNew[0],
4826 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
4827 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00004828 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
4829 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00004830
drhf9ffac92002-03-02 19:00:31 +00004831 /*
drh14acc042001-06-10 19:56:58 +00004832 ** Evenly distribute the data in apCell[] across the new pages.
4833 ** Insert divider cells into pParent as necessary.
4834 */
4835 j = 0;
4836 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00004837 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00004838 MemPage *pNew = apNew[i];
drh19642e52005-03-29 13:17:45 +00004839 assert( j<nMaxCells );
drh4b70f112004-05-02 21:12:19 +00004840 assert( pNew->pgno==pgnoNew[i] );
drhfa1a98a2004-05-14 19:08:17 +00004841 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh09d0deb2005-08-02 17:13:09 +00004842 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
drh43605152004-05-29 21:46:49 +00004843 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00004844
4845#ifndef SQLITE_OMIT_AUTOVACUUM
4846 /* If this is an auto-vacuum database, update the pointer map entries
4847 ** that point to the siblings that were rearranged. These can be: left
4848 ** children of cells, the right-child of the page, or overflow pages
4849 ** pointed to by cells.
4850 */
4851 if( pBt->autoVacuum ){
4852 for(k=j; k<cntNew[i]; k++){
danielk1977634f2982005-03-28 08:44:07 +00004853 assert( k<nMaxCells );
danielk1977ac11ee62005-01-15 12:45:51 +00004854 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00004855 rc = ptrmapPutOvfl(pNew, k-j);
4856 if( rc!=SQLITE_OK ){
4857 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00004858 }
4859 }
4860 }
4861 }
4862#endif
4863
4864 j = cntNew[i];
4865
4866 /* If the sibling page assembled above was not the right-most sibling,
4867 ** insert a divider cell into the parent page.
4868 */
drh14acc042001-06-10 19:56:58 +00004869 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00004870 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00004871 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00004872 int sz;
danielk1977634f2982005-03-28 08:44:07 +00004873
4874 assert( j<nMaxCells );
drh8b18dd42004-05-12 19:18:15 +00004875 pCell = apCell[j];
4876 sz = szCell[j] + leafCorrection;
drh4b70f112004-05-02 21:12:19 +00004877 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00004878 memcpy(&pNew->aData[8], pCell, 4);
drh24cd67e2004-05-10 16:18:47 +00004879 pTemp = 0;
drh8b18dd42004-05-12 19:18:15 +00004880 }else if( leafData ){
danielk1977ac11ee62005-01-15 12:45:51 +00004881 /* If the tree is a leaf-data tree, and the siblings are leaves,
4882 ** then there is no divider cell in apCell[]. Instead, the divider
4883 ** cell consists of the integer key for the right-most cell of
4884 ** the sibling-page assembled above only.
4885 */
drh6f11bef2004-05-13 01:12:56 +00004886 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00004887 j--;
drh43605152004-05-29 21:46:49 +00004888 parseCellPtr(pNew, apCell[j], &info);
drhb6f41482004-05-14 01:58:11 +00004889 pCell = &aSpace[iSpace];
drh6f11bef2004-05-13 01:12:56 +00004890 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
drhb6f41482004-05-14 01:58:11 +00004891 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00004892 assert( iSpace<=pBt->pageSize*5 );
drh8b18dd42004-05-12 19:18:15 +00004893 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00004894 }else{
4895 pCell -= 4;
drhb6f41482004-05-14 01:58:11 +00004896 pTemp = &aSpace[iSpace];
4897 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00004898 assert( iSpace<=pBt->pageSize*5 );
drh4b70f112004-05-02 21:12:19 +00004899 }
danielk1977a3ad5e72005-01-07 08:56:44 +00004900 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00004901 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh43605152004-05-29 21:46:49 +00004902 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +00004903#ifndef SQLITE_OMIT_AUTOVACUUM
4904 /* If this is an auto-vacuum database, and not a leaf-data tree,
4905 ** then update the pointer map with an entry for the overflow page
4906 ** that the cell just inserted points to (if any).
4907 */
4908 if( pBt->autoVacuum && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00004909 rc = ptrmapPutOvfl(pParent, nxDiv);
4910 if( rc!=SQLITE_OK ){
4911 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00004912 }
4913 }
4914#endif
drh14acc042001-06-10 19:56:58 +00004915 j++;
4916 nxDiv++;
4917 }
4918 }
drh6019e162001-07-02 17:51:45 +00004919 assert( j==nCell );
drh7aa8f852006-03-28 00:24:44 +00004920 assert( nOld>0 );
4921 assert( nNew>0 );
drh4b70f112004-05-02 21:12:19 +00004922 if( (pageFlags & PTF_LEAF)==0 ){
drh43605152004-05-29 21:46:49 +00004923 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
drh14acc042001-06-10 19:56:58 +00004924 }
drh43605152004-05-29 21:46:49 +00004925 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00004926 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00004927 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00004928 }else{
4929 /* Right-most sibling is the left child of the first entry in pParent
4930 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00004931 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00004932 }
4933
4934 /*
4935 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00004936 */
4937 for(i=0; i<nNew; i++){
danielk1977afcdd022004-10-31 16:25:42 +00004938 rc = reparentChildPages(apNew[i]);
4939 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00004940 }
danielk1977afcdd022004-10-31 16:25:42 +00004941 rc = reparentChildPages(pParent);
4942 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00004943
4944 /*
drh3a4c1412004-05-09 20:40:11 +00004945 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00004946 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00004947 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00004948 */
drhda200cc2004-05-09 11:51:38 +00004949 assert( pParent->isInit );
danielk1977ac245ec2005-01-14 13:50:11 +00004950 rc = balance(pParent, 0);
drhda200cc2004-05-09 11:51:38 +00004951
drh8b2f49b2001-06-08 00:21:52 +00004952 /*
drh14acc042001-06-10 19:56:58 +00004953 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00004954 */
drh14acc042001-06-10 19:56:58 +00004955balance_cleanup:
drh2e38c322004-09-03 18:38:44 +00004956 sqliteFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00004957 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00004958 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00004959 }
drh14acc042001-06-10 19:56:58 +00004960 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00004961 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00004962 }
drh91025292004-05-03 19:49:32 +00004963 releasePage(pParent);
drh3a4c1412004-05-09 20:40:11 +00004964 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
4965 pPage->pgno, nOld, nNew, nCell));
drh8b2f49b2001-06-08 00:21:52 +00004966 return rc;
4967}
4968
4969/*
drh43605152004-05-29 21:46:49 +00004970** This routine is called for the root page of a btree when the root
4971** page contains no cells. This is an opportunity to make the tree
4972** shallower by one level.
4973*/
4974static int balance_shallower(MemPage *pPage){
4975 MemPage *pChild; /* The only child page of pPage */
4976 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00004977 int rc = SQLITE_OK; /* Return code from subprocedures */
danielk1977aef0bf62005-12-30 16:28:01 +00004978 BtShared *pBt; /* The main BTree structure */
drh2e38c322004-09-03 18:38:44 +00004979 int mxCellPerPage; /* Maximum number of cells per page */
4980 u8 **apCell; /* All cells from pages being balanced */
4981 int *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00004982
4983 assert( pPage->pParent==0 );
4984 assert( pPage->nCell==0 );
drh2e38c322004-09-03 18:38:44 +00004985 pBt = pPage->pBt;
4986 mxCellPerPage = MX_CELL(pBt);
4987 apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
4988 if( apCell==0 ) return SQLITE_NOMEM;
4989 szCell = (int*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00004990 if( pPage->leaf ){
4991 /* The table is completely empty */
4992 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
4993 }else{
4994 /* The root page is empty but has one child. Transfer the
4995 ** information from that one child into the root page if it
4996 ** will fit. This reduces the depth of the tree by one.
4997 **
4998 ** If the root page is page 1, it has less space available than
4999 ** its child (due to the 100 byte header that occurs at the beginning
5000 ** of the database fle), so it might not be able to hold all of the
5001 ** information currently contained in the child. If this is the
5002 ** case, then do not do the transfer. Leave page 1 empty except
5003 ** for the right-pointer to the child page. The child page becomes
5004 ** the virtual root of the tree.
5005 */
5006 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5007 assert( pgnoChild>0 );
danielk19773b8a05f2007-03-19 17:44:26 +00005008 assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
drh0787db62007-03-04 13:15:27 +00005009 rc = getPage(pPage->pBt, pgnoChild, &pChild, 0);
drh2e38c322004-09-03 18:38:44 +00005010 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005011 if( pPage->pgno==1 ){
5012 rc = initPage(pChild, pPage);
drh2e38c322004-09-03 18:38:44 +00005013 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005014 assert( pChild->nOverflow==0 );
5015 if( pChild->nFree>=100 ){
5016 /* The child information will fit on the root page, so do the
5017 ** copy */
5018 int i;
5019 zeroPage(pPage, pChild->aData[0]);
5020 for(i=0; i<pChild->nCell; i++){
5021 apCell[i] = findCell(pChild,i);
5022 szCell[i] = cellSizePtr(pChild, apCell[i]);
5023 }
5024 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00005025 /* Copy the right-pointer of the child to the parent. */
5026 put4byte(&pPage->aData[pPage->hdrOffset+8],
5027 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh43605152004-05-29 21:46:49 +00005028 freePage(pChild);
5029 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5030 }else{
5031 /* The child has more information that will fit on the root.
5032 ** The tree is already balanced. Do nothing. */
5033 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5034 }
5035 }else{
5036 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5037 pPage->isInit = 0;
5038 pPage->pParent = 0;
5039 rc = initPage(pPage, 0);
5040 assert( rc==SQLITE_OK );
5041 freePage(pChild);
5042 TRACE(("BALANCE: transfer child %d into root %d\n",
5043 pChild->pgno, pPage->pgno));
5044 }
danielk1977afcdd022004-10-31 16:25:42 +00005045 rc = reparentChildPages(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00005046 assert( pPage->nOverflow==0 );
5047#ifndef SQLITE_OMIT_AUTOVACUUM
5048 if( pBt->autoVacuum ){
danielk1977aac0a382005-01-16 11:07:06 +00005049 int i;
danielk1977ac11ee62005-01-15 12:45:51 +00005050 for(i=0; i<pPage->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005051 rc = ptrmapPutOvfl(pPage, i);
5052 if( rc!=SQLITE_OK ){
5053 goto end_shallow_balance;
danielk1977ac11ee62005-01-15 12:45:51 +00005054 }
5055 }
5056 }
5057#endif
danielk1977afcdd022004-10-31 16:25:42 +00005058 if( rc!=SQLITE_OK ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005059 releasePage(pChild);
5060 }
drh2e38c322004-09-03 18:38:44 +00005061end_shallow_balance:
5062 sqliteFree(apCell);
5063 return rc;
drh43605152004-05-29 21:46:49 +00005064}
5065
5066
5067/*
5068** The root page is overfull
5069**
5070** When this happens, Create a new child page and copy the
5071** contents of the root into the child. Then make the root
5072** page an empty page with rightChild pointing to the new
5073** child. Finally, call balance_internal() on the new child
5074** to cause it to split.
5075*/
5076static int balance_deeper(MemPage *pPage){
5077 int rc; /* Return value from subprocedures */
5078 MemPage *pChild; /* Pointer to a new child page */
5079 Pgno pgnoChild; /* Page number of the new child page */
danielk1977aef0bf62005-12-30 16:28:01 +00005080 BtShared *pBt; /* The BTree */
drh43605152004-05-29 21:46:49 +00005081 int usableSize; /* Total usable size of a page */
5082 u8 *data; /* Content of the parent page */
5083 u8 *cdata; /* Content of the child page */
5084 int hdr; /* Offset to page header in parent */
5085 int brk; /* Offset to content of first cell in parent */
5086
5087 assert( pPage->pParent==0 );
5088 assert( pPage->nOverflow>0 );
5089 pBt = pPage->pBt;
drh4f0c5872007-03-26 22:05:01 +00005090 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00005091 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005092 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
drh43605152004-05-29 21:46:49 +00005093 usableSize = pBt->usableSize;
5094 data = pPage->aData;
5095 hdr = pPage->hdrOffset;
5096 brk = get2byte(&data[hdr+5]);
5097 cdata = pChild->aData;
5098 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
5099 memcpy(&cdata[brk], &data[brk], usableSize-brk);
danielk1977c7dc7532004-11-17 10:22:03 +00005100 assert( pChild->isInit==0 );
drh43605152004-05-29 21:46:49 +00005101 rc = initPage(pChild, pPage);
danielk19776b456a22005-03-21 04:04:02 +00005102 if( rc ) goto balancedeeper_out;
drh43605152004-05-29 21:46:49 +00005103 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
5104 pChild->nOverflow = pPage->nOverflow;
5105 if( pChild->nOverflow ){
5106 pChild->nFree = 0;
5107 }
5108 assert( pChild->nCell==pPage->nCell );
5109 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5110 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5111 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
danielk19774e17d142005-01-16 09:06:33 +00005112#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00005113 if( pBt->autoVacuum ){
5114 int i;
5115 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
danielk19776b456a22005-03-21 04:04:02 +00005116 if( rc ) goto balancedeeper_out;
danielk1977ac11ee62005-01-15 12:45:51 +00005117 for(i=0; i<pChild->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005118 rc = ptrmapPutOvfl(pChild, i);
5119 if( rc!=SQLITE_OK ){
5120 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00005121 }
5122 }
5123 }
danielk19774e17d142005-01-16 09:06:33 +00005124#endif
drh43605152004-05-29 21:46:49 +00005125 rc = balance_nonroot(pChild);
danielk19776b456a22005-03-21 04:04:02 +00005126
5127balancedeeper_out:
drh43605152004-05-29 21:46:49 +00005128 releasePage(pChild);
5129 return rc;
5130}
5131
5132/*
5133** Decide if the page pPage needs to be balanced. If balancing is
5134** required, call the appropriate balancing routine.
5135*/
danielk1977ac245ec2005-01-14 13:50:11 +00005136static int balance(MemPage *pPage, int insert){
drh43605152004-05-29 21:46:49 +00005137 int rc = SQLITE_OK;
5138 if( pPage->pParent==0 ){
5139 if( pPage->nOverflow>0 ){
5140 rc = balance_deeper(pPage);
5141 }
danielk1977687566d2004-11-02 12:56:41 +00005142 if( rc==SQLITE_OK && pPage->nCell==0 ){
drh43605152004-05-29 21:46:49 +00005143 rc = balance_shallower(pPage);
5144 }
5145 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00005146 if( pPage->nOverflow>0 ||
5147 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
drh43605152004-05-29 21:46:49 +00005148 rc = balance_nonroot(pPage);
5149 }
5150 }
5151 return rc;
5152}
5153
5154/*
drh8dcd7ca2004-08-08 19:43:29 +00005155** This routine checks all cursors that point to table pgnoRoot.
drh980b1a72006-08-16 16:42:48 +00005156** If any of those cursors were opened with wrFlag==0 in a different
5157** database connection (a database connection that shares the pager
5158** cache with the current connection) and that other connection
5159** is not in the ReadUncommmitted state, then this routine returns
5160** SQLITE_LOCKED.
danielk1977299b1872004-11-22 10:02:10 +00005161**
5162** In addition to checking for read-locks (where a read-lock
5163** means a cursor opened with wrFlag==0) this routine also moves
drh980b1a72006-08-16 16:42:48 +00005164** all cursors write cursors so that they are pointing to the
5165** first Cell on the root page. This is necessary because an insert
danielk1977299b1872004-11-22 10:02:10 +00005166** or delete might change the number of cells on a page or delete
5167** a page entirely and we do not want to leave any cursors
5168** pointing to non-existant pages or cells.
drhf74b8d92002-09-01 23:20:45 +00005169*/
drh980b1a72006-08-16 16:42:48 +00005170static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
danielk1977299b1872004-11-22 10:02:10 +00005171 BtCursor *p;
drh980b1a72006-08-16 16:42:48 +00005172 BtShared *pBt = pBtree->pBt;
5173 sqlite3 *db = pBtree->pSqlite;
danielk1977299b1872004-11-22 10:02:10 +00005174 for(p=pBt->pCursor; p; p=p->pNext){
drh980b1a72006-08-16 16:42:48 +00005175 if( p==pExclude ) continue;
5176 if( p->eState!=CURSOR_VALID ) continue;
5177 if( p->pgnoRoot!=pgnoRoot ) continue;
5178 if( p->wrFlag==0 ){
5179 sqlite3 *dbOther = p->pBtree->pSqlite;
5180 if( dbOther==0 ||
5181 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5182 return SQLITE_LOCKED;
5183 }
5184 }else if( p->pPage->pgno!=p->pgnoRoot ){
danielk1977299b1872004-11-22 10:02:10 +00005185 moveToRoot(p);
5186 }
5187 }
drhf74b8d92002-09-01 23:20:45 +00005188 return SQLITE_OK;
5189}
5190
5191/*
drh3b7511c2001-05-26 13:15:44 +00005192** Insert a new record into the BTree. The key is given by (pKey,nKey)
5193** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00005194** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00005195** is left pointing at a random location.
5196**
5197** For an INTKEY table, only the nKey value of the key is used. pKey is
5198** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00005199*/
drh3aac2dd2004-04-26 14:10:20 +00005200int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00005201 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00005202 const void *pKey, i64 nKey, /* The key of the new record */
drhe4d90812007-03-29 05:51:49 +00005203 const void *pData, int nData, /* The data of the new record */
5204 int appendBias /* True if this is likely an append */
drh3b7511c2001-05-26 13:15:44 +00005205){
drh3b7511c2001-05-26 13:15:44 +00005206 int rc;
5207 int loc;
drh14acc042001-06-10 19:56:58 +00005208 int szNew;
drh3b7511c2001-05-26 13:15:44 +00005209 MemPage *pPage;
danielk1977aef0bf62005-12-30 16:28:01 +00005210 BtShared *pBt = pCur->pBtree->pBt;
drha34b6762004-05-07 13:30:42 +00005211 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00005212 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00005213
danielk1977aef0bf62005-12-30 16:28:01 +00005214 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005215 /* Must start a transaction before doing an insert */
5216 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005217 }
drhf74b8d92002-09-01 23:20:45 +00005218 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00005219 if( !pCur->wrFlag ){
5220 return SQLITE_PERM; /* Cursor not open for writing */
5221 }
drh980b1a72006-08-16 16:42:48 +00005222 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005223 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5224 }
danielk1977da184232006-01-05 11:34:32 +00005225
5226 /* Save the positions of any other cursors open on this table */
drh777e4c42006-01-13 04:31:58 +00005227 restoreOrClearCursorPosition(pCur, 0);
danielk19772e94d4d2006-01-09 05:36:27 +00005228 if(
danielk19772e94d4d2006-01-09 05:36:27 +00005229 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
drhe4d90812007-03-29 05:51:49 +00005230 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
danielk19772e94d4d2006-01-09 05:36:27 +00005231 ){
danielk1977da184232006-01-05 11:34:32 +00005232 return rc;
5233 }
5234
drh14acc042001-06-10 19:56:58 +00005235 pPage = pCur->pPage;
drh4a1c3802004-05-12 15:15:47 +00005236 assert( pPage->intKey || nKey>=0 );
drh8b18dd42004-05-12 19:18:15 +00005237 assert( pPage->leaf || !pPage->leafData );
drh3a4c1412004-05-09 20:40:11 +00005238 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5239 pCur->pgnoRoot, nKey, nData, pPage->pgno,
5240 loc==0 ? "overwrite" : "new entry"));
drh7aa128d2002-06-21 13:09:16 +00005241 assert( pPage->isInit );
danielk19773b8a05f2007-03-19 17:44:26 +00005242 rc = sqlite3PagerWrite(pPage->pDbPage);
drhbd03cae2001-06-02 02:40:57 +00005243 if( rc ) return rc;
drh2e38c322004-09-03 18:38:44 +00005244 newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
5245 if( newCell==0 ) return SQLITE_NOMEM;
drha34b6762004-05-07 13:30:42 +00005246 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew);
drh2e38c322004-09-03 18:38:44 +00005247 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00005248 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00005249 assert( szNew<=MX_CELL_SIZE(pBt) );
danielk1977da184232006-01-05 11:34:32 +00005250 if( loc==0 && CURSOR_VALID==pCur->eState ){
drha34b6762004-05-07 13:30:42 +00005251 int szOld;
5252 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00005253 oldCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005254 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005255 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00005256 }
drh43605152004-05-29 21:46:49 +00005257 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00005258 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00005259 if( rc ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00005260 dropCell(pPage, pCur->idx, szOld);
drh7c717f72001-06-24 20:39:41 +00005261 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00005262 assert( pPage->leaf );
drh14acc042001-06-10 19:56:58 +00005263 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00005264 pCur->info.nSize = 0;
drh14acc042001-06-10 19:56:58 +00005265 }else{
drh4b70f112004-05-02 21:12:19 +00005266 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00005267 }
danielk1977a3ad5e72005-01-07 08:56:44 +00005268 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
danielk1977e80463b2004-11-03 03:01:16 +00005269 if( rc!=SQLITE_OK ) goto end_insert;
danielk1977ac245ec2005-01-14 13:50:11 +00005270 rc = balance(pPage, 1);
drh23e11ca2004-05-04 17:27:28 +00005271 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
drh3fc190c2001-09-14 03:24:23 +00005272 /* fflush(stdout); */
danielk1977299b1872004-11-22 10:02:10 +00005273 if( rc==SQLITE_OK ){
5274 moveToRoot(pCur);
5275 }
drh2e38c322004-09-03 18:38:44 +00005276end_insert:
5277 sqliteFree(newCell);
drh5e2f8b92001-05-28 00:41:15 +00005278 return rc;
5279}
5280
5281/*
drh4b70f112004-05-02 21:12:19 +00005282** Delete the entry that the cursor is pointing to. The cursor
5283** is left pointing at a random location.
drh3b7511c2001-05-26 13:15:44 +00005284*/
drh3aac2dd2004-04-26 14:10:20 +00005285int sqlite3BtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00005286 MemPage *pPage = pCur->pPage;
drh4b70f112004-05-02 21:12:19 +00005287 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00005288 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00005289 Pgno pgnoChild = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00005290 BtShared *pBt = pCur->pBtree->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005291
drh7aa128d2002-06-21 13:09:16 +00005292 assert( pPage->isInit );
danielk1977aef0bf62005-12-30 16:28:01 +00005293 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005294 /* Must start a transaction before doing a delete */
5295 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005296 }
drhf74b8d92002-09-01 23:20:45 +00005297 assert( !pBt->readOnly );
drhbd03cae2001-06-02 02:40:57 +00005298 if( pCur->idx >= pPage->nCell ){
5299 return SQLITE_ERROR; /* The cursor is not pointing to anything */
5300 }
drhecdc7532001-09-23 02:35:53 +00005301 if( !pCur->wrFlag ){
5302 return SQLITE_PERM; /* Did not open this cursor for writing */
5303 }
drh980b1a72006-08-16 16:42:48 +00005304 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005305 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5306 }
danielk1977da184232006-01-05 11:34:32 +00005307
5308 /* Restore the current cursor position (a no-op if the cursor is not in
5309 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
danielk19773b8a05f2007-03-19 17:44:26 +00005310 ** open on the same table. Then call sqlite3PagerWrite() on the page
danielk1977da184232006-01-05 11:34:32 +00005311 ** that the entry will be deleted from.
5312 */
5313 if(
drhd1167392006-01-23 13:00:35 +00005314 (rc = restoreOrClearCursorPosition(pCur, 1))!=0 ||
5315 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
danielk19773b8a05f2007-03-19 17:44:26 +00005316 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
danielk1977da184232006-01-05 11:34:32 +00005317 ){
5318 return rc;
5319 }
danielk1977e6efa742004-11-10 11:55:10 +00005320
5321 /* Locate the cell within it's page and leave pCell pointing to the
5322 ** data. The clearCell() call frees any overflow pages associated with the
5323 ** cell. The cell itself is still intact.
5324 */
danielk1977299b1872004-11-22 10:02:10 +00005325 pCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005326 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005327 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005328 }
danielk197728129562005-01-11 10:25:06 +00005329 rc = clearCell(pPage, pCell);
5330 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:10 +00005331
drh4b70f112004-05-02 21:12:19 +00005332 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00005333 /*
drh5e00f6c2001-09-13 13:46:56 +00005334 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00005335 ** do something we will leave a hole on an internal page.
5336 ** We have to fill the hole by moving in a cell from a leaf. The
5337 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00005338 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00005339 */
drh14acc042001-06-10 19:56:58 +00005340 BtCursor leafCur;
drh4b70f112004-05-02 21:12:19 +00005341 unsigned char *pNext;
drh02afc862006-01-20 18:10:57 +00005342 int szNext; /* The compiler warning is wrong: szNext is always
5343 ** initialized before use. Adding an extra initialization
5344 ** to silence the compiler slows down the code. */
danielk1977299b1872004-11-22 10:02:10 +00005345 int notUsed;
danielk19776b456a22005-03-21 04:04:02 +00005346 unsigned char *tempCell = 0;
drh8b18dd42004-05-12 19:18:15 +00005347 assert( !pPage->leafData );
drh14acc042001-06-10 19:56:58 +00005348 getTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00005349 rc = sqlite3BtreeNext(&leafCur, &notUsed);
drh14acc042001-06-10 19:56:58 +00005350 if( rc!=SQLITE_OK ){
drhee696e22004-08-30 16:52:17 +00005351 if( rc!=SQLITE_NOMEM ){
drh49285702005-09-17 15:20:26 +00005352 rc = SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00005353 }
drh5e2f8b92001-05-28 00:41:15 +00005354 }
danielk19776b456a22005-03-21 04:04:02 +00005355 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00005356 rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00005357 }
5358 if( rc==SQLITE_OK ){
5359 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
5360 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
5361 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
5362 pNext = findCell(leafCur.pPage, leafCur.idx);
5363 szNext = cellSizePtr(leafCur.pPage, pNext);
5364 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
5365 tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
5366 if( tempCell==0 ){
5367 rc = SQLITE_NOMEM;
5368 }
5369 }
5370 if( rc==SQLITE_OK ){
5371 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
5372 }
5373 if( rc==SQLITE_OK ){
5374 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
5375 rc = balance(pPage, 0);
5376 }
5377 if( rc==SQLITE_OK ){
5378 dropCell(leafCur.pPage, leafCur.idx, szNext);
5379 rc = balance(leafCur.pPage, 0);
5380 }
drh2e38c322004-09-03 18:38:44 +00005381 sqliteFree(tempCell);
drh8c42ca92001-06-22 19:15:00 +00005382 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00005383 }else{
danielk1977299b1872004-11-22 10:02:10 +00005384 TRACE(("DELETE: table=%d delete from leaf %d\n",
5385 pCur->pgnoRoot, pPage->pgno));
5386 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
danielk1977ac245ec2005-01-14 13:50:11 +00005387 rc = balance(pPage, 0);
drh5e2f8b92001-05-28 00:41:15 +00005388 }
danielk19776b456a22005-03-21 04:04:02 +00005389 if( rc==SQLITE_OK ){
5390 moveToRoot(pCur);
5391 }
drh5e2f8b92001-05-28 00:41:15 +00005392 return rc;
drh3b7511c2001-05-26 13:15:44 +00005393}
drh8b2f49b2001-06-08 00:21:52 +00005394
5395/*
drhc6b52df2002-01-04 03:09:29 +00005396** Create a new BTree table. Write into *piTable the page
5397** number for the root page of the new table.
5398**
drhab01f612004-05-22 02:55:23 +00005399** The type of type is determined by the flags parameter. Only the
5400** following values of flags are currently in use. Other values for
5401** flags might not work:
5402**
5403** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
5404** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00005405*/
danielk1977aef0bf62005-12-30 16:28:01 +00005406int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
5407 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005408 MemPage *pRoot;
5409 Pgno pgnoRoot;
5410 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005411 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005412 /* Must start a transaction first */
5413 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005414 }
danielk197728129562005-01-11 10:25:06 +00005415 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00005416
5417 /* It is illegal to create a table if any cursors are open on the
5418 ** database. This is because in auto-vacuum mode the backend may
5419 ** need to move a database page to make room for the new root-page.
5420 ** If an open cursor was using the page a problem would occur.
5421 */
5422 if( pBt->pCursor ){
5423 return SQLITE_LOCKED;
5424 }
5425
danielk1977003ba062004-11-04 02:57:33 +00005426#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:01 +00005427 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drh8b2f49b2001-06-08 00:21:52 +00005428 if( rc ) return rc;
danielk1977003ba062004-11-04 02:57:33 +00005429#else
danielk1977687566d2004-11-02 12:56:41 +00005430 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00005431 Pgno pgnoMove; /* Move a page here to make room for the root-page */
5432 MemPage *pPageMove; /* The page to move to. */
5433
danielk1977003ba062004-11-04 02:57:33 +00005434 /* Read the value of meta[3] from the database to determine where the
5435 ** root page of the new table should go. meta[3] is the largest root-page
5436 ** created so far, so the new root-page is (meta[3]+1).
5437 */
danielk1977aef0bf62005-12-30 16:28:01 +00005438 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00005439 if( rc!=SQLITE_OK ) return rc;
5440 pgnoRoot++;
5441
danielk1977599fcba2004-11-08 07:13:13 +00005442 /* The new root-page may not be allocated on a pointer-map page, or the
5443 ** PENDING_BYTE page.
5444 */
danielk1977266664d2006-02-10 08:24:21 +00005445 if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00005446 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00005447 pgnoRoot++;
5448 }
5449 assert( pgnoRoot>=3 );
5450
5451 /* Allocate a page. The page that currently resides at pgnoRoot will
5452 ** be moved to the allocated page (unless the allocated page happens
5453 ** to reside at pgnoRoot).
5454 */
drh4f0c5872007-03-26 22:05:01 +00005455 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00005456 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00005457 return rc;
5458 }
danielk1977003ba062004-11-04 02:57:33 +00005459
5460 if( pgnoMove!=pgnoRoot ){
5461 u8 eType;
5462 Pgno iPtrPage;
5463
5464 releasePage(pPageMove);
drh0787db62007-03-04 13:15:27 +00005465 rc = getPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005466 if( rc!=SQLITE_OK ){
5467 return rc;
5468 }
5469 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drhccae6022005-02-26 17:31:26 +00005470 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00005471 releasePage(pRoot);
5472 return rc;
5473 }
drhccae6022005-02-26 17:31:26 +00005474 assert( eType!=PTRMAP_ROOTPAGE );
5475 assert( eType!=PTRMAP_FREEPAGE );
danielk19773b8a05f2007-03-19 17:44:26 +00005476 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk19775fd057a2005-03-09 13:09:43 +00005477 if( rc!=SQLITE_OK ){
5478 releasePage(pRoot);
5479 return rc;
5480 }
danielk1977003ba062004-11-04 02:57:33 +00005481 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
5482 releasePage(pRoot);
5483 if( rc!=SQLITE_OK ){
5484 return rc;
5485 }
drh0787db62007-03-04 13:15:27 +00005486 rc = getPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005487 if( rc!=SQLITE_OK ){
5488 return rc;
5489 }
danielk19773b8a05f2007-03-19 17:44:26 +00005490 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:33 +00005491 if( rc!=SQLITE_OK ){
5492 releasePage(pRoot);
5493 return rc;
5494 }
5495 }else{
5496 pRoot = pPageMove;
5497 }
5498
danielk197742741be2005-01-08 12:42:39 +00005499 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00005500 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
5501 if( rc ){
5502 releasePage(pRoot);
5503 return rc;
5504 }
danielk1977aef0bf62005-12-30 16:28:01 +00005505 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00005506 if( rc ){
5507 releasePage(pRoot);
5508 return rc;
5509 }
danielk197742741be2005-01-08 12:42:39 +00005510
danielk1977003ba062004-11-04 02:57:33 +00005511 }else{
drh4f0c5872007-03-26 22:05:01 +00005512 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00005513 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00005514 }
5515#endif
danielk19773b8a05f2007-03-19 17:44:26 +00005516 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhde647132004-05-07 17:57:49 +00005517 zeroPage(pRoot, flags | PTF_LEAF);
danielk19773b8a05f2007-03-19 17:44:26 +00005518 sqlite3PagerUnref(pRoot->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00005519 *piTable = (int)pgnoRoot;
5520 return SQLITE_OK;
5521}
5522
5523/*
5524** Erase the given database page and all its children. Return
5525** the page to the freelist.
5526*/
drh4b70f112004-05-02 21:12:19 +00005527static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:01 +00005528 BtShared *pBt, /* The BTree that contains the table */
drh4b70f112004-05-02 21:12:19 +00005529 Pgno pgno, /* Page number to clear */
5530 MemPage *pParent, /* Parent page. NULL for the root */
5531 int freePageFlag /* Deallocate page if true */
5532){
danielk19776b456a22005-03-21 04:04:02 +00005533 MemPage *pPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00005534 int rc;
drh4b70f112004-05-02 21:12:19 +00005535 unsigned char *pCell;
5536 int i;
drh8b2f49b2001-06-08 00:21:52 +00005537
danielk19773b8a05f2007-03-19 17:44:26 +00005538 if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00005539 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00005540 }
5541
drhde647132004-05-07 17:57:49 +00005542 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
danielk19776b456a22005-03-21 04:04:02 +00005543 if( rc ) goto cleardatabasepage_out;
drh4b70f112004-05-02 21:12:19 +00005544 for(i=0; i<pPage->nCell; i++){
drh43605152004-05-29 21:46:49 +00005545 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00005546 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005547 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00005548 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00005549 }
drh4b70f112004-05-02 21:12:19 +00005550 rc = clearCell(pPage, pCell);
danielk19776b456a22005-03-21 04:04:02 +00005551 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00005552 }
drha34b6762004-05-07 13:30:42 +00005553 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005554 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00005555 if( rc ) goto cleardatabasepage_out;
drh2aa679f2001-06-25 02:11:07 +00005556 }
5557 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00005558 rc = freePage(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00005559 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
drh3a4c1412004-05-09 20:40:11 +00005560 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00005561 }
danielk19776b456a22005-03-21 04:04:02 +00005562
5563cleardatabasepage_out:
drh4b70f112004-05-02 21:12:19 +00005564 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00005565 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005566}
5567
5568/*
drhab01f612004-05-22 02:55:23 +00005569** Delete all information from a single table in the database. iTable is
5570** the page number of the root of the table. After this routine returns,
5571** the root page is empty, but still exists.
5572**
5573** This routine will fail with SQLITE_LOCKED if there are any open
5574** read cursors on the table. Open write cursors are moved to the
5575** root of the table.
drh8b2f49b2001-06-08 00:21:52 +00005576*/
danielk1977aef0bf62005-12-30 16:28:01 +00005577int sqlite3BtreeClearTable(Btree *p, int iTable){
drh8b2f49b2001-06-08 00:21:52 +00005578 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005579 BtShared *pBt = p->pBt;
5580 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005581 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005582 }
drh980b1a72006-08-16 16:42:48 +00005583 rc = checkReadLocks(p, iTable, 0);
5584 if( rc ){
5585 return rc;
drhecdc7532001-09-23 02:35:53 +00005586 }
danielk1977ed429312006-01-19 08:43:31 +00005587
5588 /* Save the position of all cursors open on this table */
5589 if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
5590 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005591 }
danielk1977ed429312006-01-19 08:43:31 +00005592
5593 return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
drh8b2f49b2001-06-08 00:21:52 +00005594}
5595
5596/*
5597** Erase all information in a table and add the root of the table to
5598** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00005599** page 1) is never added to the freelist.
5600**
5601** This routine will fail with SQLITE_LOCKED if there are any open
5602** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00005603**
5604** If AUTOVACUUM is enabled and the page at iTable is not the last
5605** root page in the database file, then the last root page
5606** in the database file is moved into the slot formerly occupied by
5607** iTable and that last slot formerly occupied by the last root page
5608** is added to the freelist instead of iTable. In this say, all
5609** root pages are kept at the beginning of the database file, which
5610** is necessary for AUTOVACUUM to work right. *piMoved is set to the
5611** page number that used to be the last root page in the file before
5612** the move. If no page gets moved, *piMoved is set to 0.
5613** The last root page is recorded in meta[3] and the value of
5614** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00005615*/
danielk1977aef0bf62005-12-30 16:28:01 +00005616int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00005617 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00005618 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00005619 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:04 +00005620
danielk1977aef0bf62005-12-30 16:28:01 +00005621 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005622 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005623 }
danielk1977a0bf2652004-11-04 14:30:04 +00005624
danielk1977e6efa742004-11-10 11:55:10 +00005625 /* It is illegal to drop a table if any cursors are open on the
5626 ** database. This is because in auto-vacuum mode the backend may
5627 ** need to move another root-page to fill a gap left by the deleted
5628 ** root page. If an open cursor was using this page a problem would
5629 ** occur.
5630 */
5631 if( pBt->pCursor ){
5632 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00005633 }
danielk1977a0bf2652004-11-04 14:30:04 +00005634
drh0787db62007-03-04 13:15:27 +00005635 rc = getPage(pBt, (Pgno)iTable, &pPage, 0);
drh2aa679f2001-06-25 02:11:07 +00005636 if( rc ) return rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005637 rc = sqlite3BtreeClearTable(p, iTable);
danielk19776b456a22005-03-21 04:04:02 +00005638 if( rc ){
5639 releasePage(pPage);
5640 return rc;
5641 }
danielk1977a0bf2652004-11-04 14:30:04 +00005642
drh205f48e2004-11-05 00:43:11 +00005643 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00005644
drh4b70f112004-05-02 21:12:19 +00005645 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00005646#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00005647 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00005648 releasePage(pPage);
5649#else
5650 if( pBt->autoVacuum ){
5651 Pgno maxRootPgno;
danielk1977aef0bf62005-12-30 16:28:01 +00005652 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00005653 if( rc!=SQLITE_OK ){
5654 releasePage(pPage);
5655 return rc;
5656 }
5657
5658 if( iTable==maxRootPgno ){
5659 /* If the table being dropped is the table with the largest root-page
5660 ** number in the database, put the root page on the free list.
5661 */
5662 rc = freePage(pPage);
5663 releasePage(pPage);
5664 if( rc!=SQLITE_OK ){
5665 return rc;
5666 }
5667 }else{
5668 /* The table being dropped does not have the largest root-page
5669 ** number in the database. So move the page that does into the
5670 ** gap left by the deleted root-page.
5671 */
5672 MemPage *pMove;
5673 releasePage(pPage);
drh0787db62007-03-04 13:15:27 +00005674 rc = getPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00005675 if( rc!=SQLITE_OK ){
5676 return rc;
5677 }
5678 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
5679 releasePage(pMove);
5680 if( rc!=SQLITE_OK ){
5681 return rc;
5682 }
drh0787db62007-03-04 13:15:27 +00005683 rc = getPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00005684 if( rc!=SQLITE_OK ){
5685 return rc;
5686 }
5687 rc = freePage(pMove);
5688 releasePage(pMove);
5689 if( rc!=SQLITE_OK ){
5690 return rc;
5691 }
5692 *piMoved = maxRootPgno;
5693 }
5694
danielk1977599fcba2004-11-08 07:13:13 +00005695 /* Set the new 'max-root-page' value in the database header. This
5696 ** is the old value less one, less one more if that happens to
5697 ** be a root-page number, less one again if that is the
5698 ** PENDING_BYTE_PAGE.
5699 */
danielk197787a6e732004-11-05 12:58:25 +00005700 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00005701 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
5702 maxRootPgno--;
5703 }
danielk1977266664d2006-02-10 08:24:21 +00005704 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00005705 maxRootPgno--;
5706 }
danielk1977599fcba2004-11-08 07:13:13 +00005707 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
5708
danielk1977aef0bf62005-12-30 16:28:01 +00005709 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00005710 }else{
5711 rc = freePage(pPage);
5712 releasePage(pPage);
5713 }
5714#endif
drh2aa679f2001-06-25 02:11:07 +00005715 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00005716 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00005717 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00005718 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00005719 }
drh8b2f49b2001-06-08 00:21:52 +00005720 return rc;
5721}
5722
drh001bbcb2003-03-19 03:14:00 +00005723
drh8b2f49b2001-06-08 00:21:52 +00005724/*
drh23e11ca2004-05-04 17:27:28 +00005725** Read the meta-information out of a database file. Meta[0]
5726** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00005727** through meta[15] are available for use by higher layers. Meta[0]
5728** is read-only, the others are read/write.
5729**
5730** The schema layer numbers meta values differently. At the schema
5731** layer (and the SetCookie and ReadCookie opcodes) the number of
5732** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00005733*/
danielk1977aef0bf62005-12-30 16:28:01 +00005734int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
danielk19773b8a05f2007-03-19 17:44:26 +00005735 DbPage *pDbPage;
drh8b2f49b2001-06-08 00:21:52 +00005736 int rc;
drh4b70f112004-05-02 21:12:19 +00005737 unsigned char *pP1;
danielk1977aef0bf62005-12-30 16:28:01 +00005738 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005739
danielk1977da184232006-01-05 11:34:32 +00005740 /* Reading a meta-data value requires a read-lock on page 1 (and hence
5741 ** the sqlite_master table. We grab this lock regardless of whether or
5742 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
5743 ** 1 is treated as a special case by queryTableLock() and lockTable()).
5744 */
5745 rc = queryTableLock(p, 1, READ_LOCK);
5746 if( rc!=SQLITE_OK ){
5747 return rc;
5748 }
5749
drh23e11ca2004-05-04 17:27:28 +00005750 assert( idx>=0 && idx<=15 );
danielk19773b8a05f2007-03-19 17:44:26 +00005751 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00005752 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005753 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
drh23e11ca2004-05-04 17:27:28 +00005754 *pMeta = get4byte(&pP1[36 + idx*4]);
danielk19773b8a05f2007-03-19 17:44:26 +00005755 sqlite3PagerUnref(pDbPage);
drhae157872004-08-14 19:20:09 +00005756
danielk1977599fcba2004-11-08 07:13:13 +00005757 /* If autovacuumed is disabled in this build but we are trying to
5758 ** access an autovacuumed database, then make the database readonly.
5759 */
danielk1977003ba062004-11-04 02:57:33 +00005760#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00005761 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00005762#endif
drhae157872004-08-14 19:20:09 +00005763
danielk1977da184232006-01-05 11:34:32 +00005764 /* Grab the read-lock on page 1. */
5765 rc = lockTable(p, 1, READ_LOCK);
5766 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005767}
5768
5769/*
drh23e11ca2004-05-04 17:27:28 +00005770** Write meta-information back into the database. Meta[0] is
5771** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00005772*/
danielk1977aef0bf62005-12-30 16:28:01 +00005773int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
5774 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:19 +00005775 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00005776 int rc;
drh23e11ca2004-05-04 17:27:28 +00005777 assert( idx>=1 && idx<=15 );
danielk1977aef0bf62005-12-30 16:28:01 +00005778 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005779 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh5df72a52002-06-06 23:16:05 +00005780 }
drhde647132004-05-07 17:57:49 +00005781 assert( pBt->pPage1!=0 );
5782 pP1 = pBt->pPage1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00005783 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
drh4b70f112004-05-02 21:12:19 +00005784 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00005785 put4byte(&pP1[36 + idx*4], iMeta);
drh8b2f49b2001-06-08 00:21:52 +00005786 return SQLITE_OK;
5787}
drh8c42ca92001-06-22 19:15:00 +00005788
drhf328bc82004-05-10 23:29:49 +00005789/*
5790** Return the flag byte at the beginning of the page that the cursor
5791** is currently pointing to.
5792*/
5793int sqlite3BtreeFlags(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00005794 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
drh777e4c42006-01-13 04:31:58 +00005795 ** restoreOrClearCursorPosition() here.
danielk1977da184232006-01-05 11:34:32 +00005796 */
drhf328bc82004-05-10 23:29:49 +00005797 MemPage *pPage = pCur->pPage;
5798 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
5799}
5800
danielk1977b5402fb2005-01-12 07:15:04 +00005801#ifdef SQLITE_DEBUG
drh8c42ca92001-06-22 19:15:00 +00005802/*
5803** Print a disassembly of the given page on standard output. This routine
5804** is used for debugging and testing only.
5805*/
danielk1977aef0bf62005-12-30 16:28:01 +00005806static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){
drh8c42ca92001-06-22 19:15:00 +00005807 int rc;
5808 MemPage *pPage;
drhc8629a12004-05-08 20:07:40 +00005809 int i, j, c;
drh8c42ca92001-06-22 19:15:00 +00005810 int nFree;
5811 u16 idx;
drhab9f7f12004-05-08 10:56:11 +00005812 int hdr;
drh43605152004-05-29 21:46:49 +00005813 int nCell;
drha2fce642004-06-05 00:01:44 +00005814 int isInit;
drhab9f7f12004-05-08 10:56:11 +00005815 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00005816 char range[20];
5817 unsigned char payload[20];
drhab9f7f12004-05-08 10:56:11 +00005818
drh0787db62007-03-04 13:15:27 +00005819 rc = getPage(pBt, (Pgno)pgno, &pPage, 0);
drha2fce642004-06-05 00:01:44 +00005820 isInit = pPage->isInit;
5821 if( pPage->isInit==0 ){
danielk1977c7dc7532004-11-17 10:22:03 +00005822 initPage(pPage, pParent);
drha2fce642004-06-05 00:01:44 +00005823 }
drh8c42ca92001-06-22 19:15:00 +00005824 if( rc ){
5825 return rc;
5826 }
drhab9f7f12004-05-08 10:56:11 +00005827 hdr = pPage->hdrOffset;
5828 data = pPage->aData;
drhc8629a12004-05-08 20:07:40 +00005829 c = data[hdr];
drh8b18dd42004-05-12 19:18:15 +00005830 pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
drhc8629a12004-05-08 20:07:40 +00005831 pPage->zeroData = (c & PTF_ZERODATA)!=0;
drh8b18dd42004-05-12 19:18:15 +00005832 pPage->leafData = (c & PTF_LEAFDATA)!=0;
drhc8629a12004-05-08 20:07:40 +00005833 pPage->leaf = (c & PTF_LEAF)!=0;
drh8b18dd42004-05-12 19:18:15 +00005834 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
drh43605152004-05-29 21:46:49 +00005835 nCell = get2byte(&data[hdr+3]);
drhfe63d1c2004-09-08 20:13:04 +00005836 sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
drh43605152004-05-29 21:46:49 +00005837 data[hdr], data[hdr+7],
drhda200cc2004-05-09 11:51:38 +00005838 (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
drhab9f7f12004-05-08 10:56:11 +00005839 assert( hdr == (pgno==1 ? 100 : 0) );
drh43605152004-05-29 21:46:49 +00005840 idx = hdr + 12 - pPage->leaf*4;
5841 for(i=0; i<nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00005842 CellInfo info;
drh4b70f112004-05-02 21:12:19 +00005843 Pgno child;
drh43605152004-05-29 21:46:49 +00005844 unsigned char *pCell;
drh6f11bef2004-05-13 01:12:56 +00005845 int sz;
drh43605152004-05-29 21:46:49 +00005846 int addr;
drh6f11bef2004-05-13 01:12:56 +00005847
drh43605152004-05-29 21:46:49 +00005848 addr = get2byte(&data[idx + 2*i]);
5849 pCell = &data[addr];
5850 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00005851 sz = info.nSize;
drh43605152004-05-29 21:46:49 +00005852 sprintf(range,"%d..%d", addr, addr+sz-1);
drh4b70f112004-05-02 21:12:19 +00005853 if( pPage->leaf ){
5854 child = 0;
5855 }else{
drh43605152004-05-29 21:46:49 +00005856 child = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005857 }
drh6f11bef2004-05-13 01:12:56 +00005858 sz = info.nData;
5859 if( !pPage->intKey ) sz += info.nKey;
drh8c42ca92001-06-22 19:15:00 +00005860 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
drh6f11bef2004-05-13 01:12:56 +00005861 memcpy(payload, &pCell[info.nHeader], sz);
drh8c42ca92001-06-22 19:15:00 +00005862 for(j=0; j<sz; j++){
5863 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
5864 }
5865 payload[sz] = 0;
drhfe63d1c2004-09-08 20:13:04 +00005866 sqlite3DebugPrintf(
drh6f11bef2004-05-13 01:12:56 +00005867 "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
5868 i, range, child, info.nKey, info.nData, payload
drh8c42ca92001-06-22 19:15:00 +00005869 );
drh8c42ca92001-06-22 19:15:00 +00005870 }
drh4b70f112004-05-02 21:12:19 +00005871 if( !pPage->leaf ){
drhfe63d1c2004-09-08 20:13:04 +00005872 sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
drh4b70f112004-05-02 21:12:19 +00005873 }
drh8c42ca92001-06-22 19:15:00 +00005874 nFree = 0;
5875 i = 0;
drhab9f7f12004-05-08 10:56:11 +00005876 idx = get2byte(&data[hdr+1]);
drhb6f41482004-05-14 01:58:11 +00005877 while( idx>0 && idx<pPage->pBt->usableSize ){
drhab9f7f12004-05-08 10:56:11 +00005878 int sz = get2byte(&data[idx+2]);
drh4b70f112004-05-02 21:12:19 +00005879 sprintf(range,"%d..%d", idx, idx+sz-1);
5880 nFree += sz;
drhfe63d1c2004-09-08 20:13:04 +00005881 sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
drh4b70f112004-05-02 21:12:19 +00005882 i, range, sz, nFree);
drhab9f7f12004-05-08 10:56:11 +00005883 idx = get2byte(&data[idx]);
drh2aa679f2001-06-25 02:11:07 +00005884 i++;
drh8c42ca92001-06-22 19:15:00 +00005885 }
5886 if( idx!=0 ){
drhfe63d1c2004-09-08 20:13:04 +00005887 sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
drh8c42ca92001-06-22 19:15:00 +00005888 }
drha34b6762004-05-07 13:30:42 +00005889 if( recursive && !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005890 for(i=0; i<nCell; i++){
5891 unsigned char *pCell = findCell(pPage, i);
danielk1977c7dc7532004-11-17 10:22:03 +00005892 btreePageDump(pBt, get4byte(pCell), 1, pPage);
drha34b6762004-05-07 13:30:42 +00005893 idx = get2byte(pCell);
drh6019e162001-07-02 17:51:45 +00005894 }
danielk1977c7dc7532004-11-17 10:22:03 +00005895 btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
drh6019e162001-07-02 17:51:45 +00005896 }
drha2fce642004-06-05 00:01:44 +00005897 pPage->isInit = isInit;
danielk19773b8a05f2007-03-19 17:44:26 +00005898 sqlite3PagerUnref(pPage->pDbPage);
drh3644f082004-05-10 18:45:09 +00005899 fflush(stdout);
drh8c42ca92001-06-22 19:15:00 +00005900 return SQLITE_OK;
5901}
danielk1977aef0bf62005-12-30 16:28:01 +00005902int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){
5903 return btreePageDump(p->pBt, pgno, recursive, 0);
danielk1977c7dc7532004-11-17 10:22:03 +00005904}
drhaaab5722002-02-19 13:39:21 +00005905#endif
drh8c42ca92001-06-22 19:15:00 +00005906
drh77bba592006-08-13 18:39:26 +00005907#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drh8c42ca92001-06-22 19:15:00 +00005908/*
drh2aa679f2001-06-25 02:11:07 +00005909** Fill aResult[] with information about the entry and page that the
5910** cursor is pointing to.
5911**
5912** aResult[0] = The page number
5913** aResult[1] = The entry number
5914** aResult[2] = Total number of entries on this page
drh3e27c022004-07-23 00:01:38 +00005915** aResult[3] = Cell size (local payload + header)
drh2aa679f2001-06-25 02:11:07 +00005916** aResult[4] = Number of free bytes on this page
5917** aResult[5] = Number of free blocks on the page
drh3e27c022004-07-23 00:01:38 +00005918** aResult[6] = Total payload size (local + overflow)
5919** aResult[7] = Header size in bytes
5920** aResult[8] = Local payload size
5921** aResult[9] = Parent page number
drh50c67062007-02-10 19:22:35 +00005922** aResult[10]= Page number of the first overflow page
drh5eddca62001-06-30 21:53:53 +00005923**
5924** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00005925*/
drh3e27c022004-07-23 00:01:38 +00005926int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
drh2aa679f2001-06-25 02:11:07 +00005927 int cnt, idx;
5928 MemPage *pPage = pCur->pPage;
drh3e27c022004-07-23 00:01:38 +00005929 BtCursor tmpCur;
drhda200cc2004-05-09 11:51:38 +00005930
drh777e4c42006-01-13 04:31:58 +00005931 int rc = restoreOrClearCursorPosition(pCur, 1);
danielk1977da184232006-01-05 11:34:32 +00005932 if( rc!=SQLITE_OK ){
5933 return rc;
5934 }
5935
drh4b70f112004-05-02 21:12:19 +00005936 assert( pPage->isInit );
drh3e27c022004-07-23 00:01:38 +00005937 getTempCursor(pCur, &tmpCur);
5938 while( upCnt-- ){
5939 moveToParent(&tmpCur);
5940 }
5941 pPage = tmpCur.pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00005942 aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
drh91025292004-05-03 19:49:32 +00005943 assert( aResult[0]==pPage->pgno );
drh3e27c022004-07-23 00:01:38 +00005944 aResult[1] = tmpCur.idx;
drh2aa679f2001-06-25 02:11:07 +00005945 aResult[2] = pPage->nCell;
drh3e27c022004-07-23 00:01:38 +00005946 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
5947 getCellInfo(&tmpCur);
5948 aResult[3] = tmpCur.info.nSize;
5949 aResult[6] = tmpCur.info.nData;
5950 aResult[7] = tmpCur.info.nHeader;
5951 aResult[8] = tmpCur.info.nLocal;
drh2aa679f2001-06-25 02:11:07 +00005952 }else{
5953 aResult[3] = 0;
5954 aResult[6] = 0;
drh3e27c022004-07-23 00:01:38 +00005955 aResult[7] = 0;
5956 aResult[8] = 0;
drh2aa679f2001-06-25 02:11:07 +00005957 }
5958 aResult[4] = pPage->nFree;
5959 cnt = 0;
drh4b70f112004-05-02 21:12:19 +00005960 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
drhb6f41482004-05-14 01:58:11 +00005961 while( idx>0 && idx<pPage->pBt->usableSize ){
drh2aa679f2001-06-25 02:11:07 +00005962 cnt++;
drh4b70f112004-05-02 21:12:19 +00005963 idx = get2byte(&pPage->aData[idx]);
drh2aa679f2001-06-25 02:11:07 +00005964 }
5965 aResult[5] = cnt;
drh3e27c022004-07-23 00:01:38 +00005966 if( pPage->pParent==0 || isRootPage(pPage) ){
5967 aResult[9] = 0;
5968 }else{
5969 aResult[9] = pPage->pParent->pgno;
5970 }
drh50c67062007-02-10 19:22:35 +00005971 if( tmpCur.info.iOverflow ){
5972 aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
5973 }else{
5974 aResult[10] = 0;
5975 }
drh3e27c022004-07-23 00:01:38 +00005976 releaseTempCursor(&tmpCur);
drh8c42ca92001-06-22 19:15:00 +00005977 return SQLITE_OK;
5978}
drhaaab5722002-02-19 13:39:21 +00005979#endif
drhdd793422001-06-28 01:54:48 +00005980
drhdd793422001-06-28 01:54:48 +00005981/*
drh5eddca62001-06-30 21:53:53 +00005982** Return the pager associated with a BTree. This routine is used for
5983** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00005984*/
danielk1977aef0bf62005-12-30 16:28:01 +00005985Pager *sqlite3BtreePager(Btree *p){
5986 return p->pBt->pPager;
drhdd793422001-06-28 01:54:48 +00005987}
drh5eddca62001-06-30 21:53:53 +00005988
5989/*
5990** This structure is passed around through all the sanity checking routines
5991** in order to keep track of some global state information.
5992*/
drhaaab5722002-02-19 13:39:21 +00005993typedef struct IntegrityCk IntegrityCk;
5994struct IntegrityCk {
danielk1977aef0bf62005-12-30 16:28:01 +00005995 BtShared *pBt; /* The tree being checked out */
drh1dcdbc02007-01-27 02:24:54 +00005996 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
5997 int nPage; /* Number of pages in the database */
5998 int *anRef; /* Number of times each page is referenced */
5999 int mxErr; /* Stop accumulating errors when this reaches zero */
6000 char *zErrMsg; /* An error message. NULL if no errors seen. */
6001 int nErr; /* Number of messages written to zErrMsg so far */
drh5eddca62001-06-30 21:53:53 +00006002};
6003
drhb7f91642004-10-31 02:22:47 +00006004#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006005/*
6006** Append a message to the error message string.
6007*/
drh2e38c322004-09-03 18:38:44 +00006008static void checkAppendMsg(
6009 IntegrityCk *pCheck,
6010 char *zMsg1,
6011 const char *zFormat,
6012 ...
6013){
6014 va_list ap;
6015 char *zMsg2;
drh1dcdbc02007-01-27 02:24:54 +00006016 if( !pCheck->mxErr ) return;
6017 pCheck->mxErr--;
6018 pCheck->nErr++;
drh2e38c322004-09-03 18:38:44 +00006019 va_start(ap, zFormat);
6020 zMsg2 = sqlite3VMPrintf(zFormat, ap);
6021 va_end(ap);
6022 if( zMsg1==0 ) zMsg1 = "";
drh5eddca62001-06-30 21:53:53 +00006023 if( pCheck->zErrMsg ){
6024 char *zOld = pCheck->zErrMsg;
6025 pCheck->zErrMsg = 0;
danielk19774adee202004-05-08 08:23:19 +00006026 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00006027 sqliteFree(zOld);
6028 }else{
danielk19774adee202004-05-08 08:23:19 +00006029 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00006030 }
drh2e38c322004-09-03 18:38:44 +00006031 sqliteFree(zMsg2);
drh5eddca62001-06-30 21:53:53 +00006032}
drhb7f91642004-10-31 02:22:47 +00006033#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006034
drhb7f91642004-10-31 02:22:47 +00006035#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006036/*
6037** Add 1 to the reference count for page iPage. If this is the second
6038** reference to the page, add an error message to pCheck->zErrMsg.
6039** Return 1 if there are 2 ore more references to the page and 0 if
6040** if this is the first reference to the page.
6041**
6042** Also check that the page number is in bounds.
6043*/
drhaaab5722002-02-19 13:39:21 +00006044static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00006045 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00006046 if( iPage>pCheck->nPage || iPage<0 ){
drh2e38c322004-09-03 18:38:44 +00006047 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006048 return 1;
6049 }
6050 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00006051 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006052 return 1;
6053 }
6054 return (pCheck->anRef[iPage]++)>1;
6055}
6056
danielk1977afcdd022004-10-31 16:25:42 +00006057#ifndef SQLITE_OMIT_AUTOVACUUM
6058/*
6059** Check that the entry in the pointer-map for page iChild maps to
6060** page iParent, pointer type ptrType. If not, append an error message
6061** to pCheck.
6062*/
6063static void checkPtrmap(
6064 IntegrityCk *pCheck, /* Integrity check context */
6065 Pgno iChild, /* Child page number */
6066 u8 eType, /* Expected pointer map type */
6067 Pgno iParent, /* Expected pointer map parent page number */
6068 char *zContext /* Context description (used for error msg) */
6069){
6070 int rc;
6071 u8 ePtrmapType;
6072 Pgno iPtrmapParent;
6073
6074 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6075 if( rc!=SQLITE_OK ){
6076 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6077 return;
6078 }
6079
6080 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6081 checkAppendMsg(pCheck, zContext,
6082 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6083 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6084 }
6085}
6086#endif
6087
drh5eddca62001-06-30 21:53:53 +00006088/*
6089** Check the integrity of the freelist or of an overflow page list.
6090** Verify that the number of pages on the list is N.
6091*/
drh30e58752002-03-02 20:41:57 +00006092static void checkList(
6093 IntegrityCk *pCheck, /* Integrity checking context */
6094 int isFreeList, /* True for a freelist. False for overflow page list */
6095 int iPage, /* Page number for first page in the list */
6096 int N, /* Expected number of pages in the list */
6097 char *zContext /* Context for error messages */
6098){
6099 int i;
drh3a4c1412004-05-09 20:40:11 +00006100 int expected = N;
6101 int iFirst = iPage;
drh1dcdbc02007-01-27 02:24:54 +00006102 while( N-- > 0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:26 +00006103 DbPage *pOvflPage;
6104 unsigned char *pOvflData;
drh5eddca62001-06-30 21:53:53 +00006105 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00006106 checkAppendMsg(pCheck, zContext,
6107 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00006108 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00006109 break;
6110 }
6111 if( checkRef(pCheck, iPage, zContext) ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006112 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
drh2e38c322004-09-03 18:38:44 +00006113 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006114 break;
6115 }
danielk19773b8a05f2007-03-19 17:44:26 +00006116 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:57 +00006117 if( isFreeList ){
danielk19773b8a05f2007-03-19 17:44:26 +00006118 int n = get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:41 +00006119#ifndef SQLITE_OMIT_AUTOVACUUM
6120 if( pCheck->pBt->autoVacuum ){
6121 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6122 }
6123#endif
drh855eb1c2004-08-31 13:45:11 +00006124 if( n>pCheck->pBt->usableSize/4-8 ){
drh2e38c322004-09-03 18:38:44 +00006125 checkAppendMsg(pCheck, zContext,
6126 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00006127 N--;
6128 }else{
6129 for(i=0; i<n; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006130 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:41 +00006131#ifndef SQLITE_OMIT_AUTOVACUUM
6132 if( pCheck->pBt->autoVacuum ){
6133 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6134 }
6135#endif
6136 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00006137 }
6138 N -= n;
drh30e58752002-03-02 20:41:57 +00006139 }
drh30e58752002-03-02 20:41:57 +00006140 }
danielk1977afcdd022004-10-31 16:25:42 +00006141#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006142 else{
6143 /* If this database supports auto-vacuum and iPage is not the last
6144 ** page in this overflow list, check that the pointer-map entry for
6145 ** the following page matches iPage.
6146 */
6147 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00006148 i = get4byte(pOvflData);
danielk1977687566d2004-11-02 12:56:41 +00006149 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6150 }
danielk1977afcdd022004-10-31 16:25:42 +00006151 }
6152#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006153 iPage = get4byte(pOvflData);
6154 sqlite3PagerUnref(pOvflPage);
drh5eddca62001-06-30 21:53:53 +00006155 }
6156}
drhb7f91642004-10-31 02:22:47 +00006157#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006158
drhb7f91642004-10-31 02:22:47 +00006159#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006160/*
6161** Do various sanity checks on a single page of a tree. Return
6162** the tree depth. Root pages return 0. Parents of root pages
6163** return 1, and so forth.
6164**
6165** These checks are done:
6166**
6167** 1. Make sure that cells and freeblocks do not overlap
6168** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00006169** NO 2. Make sure cell keys are in order.
6170** NO 3. Make sure no key is less than or equal to zLowerBound.
6171** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00006172** 5. Check the integrity of overflow pages.
6173** 6. Recursively call checkTreePage on all children.
6174** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00006175** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00006176** the root of the tree.
6177*/
6178static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00006179 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00006180 int iPage, /* Page number of the page to check */
6181 MemPage *pParent, /* Parent page */
drh74161702006-02-24 02:53:49 +00006182 char *zParentContext /* Parent context */
drh5eddca62001-06-30 21:53:53 +00006183){
6184 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00006185 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00006186 int hdr, cellStart;
6187 int nCell;
drhda200cc2004-05-09 11:51:38 +00006188 u8 *data;
danielk1977aef0bf62005-12-30 16:28:01 +00006189 BtShared *pBt;
drh4f26bb62005-09-08 14:17:20 +00006190 int usableSize;
drh5eddca62001-06-30 21:53:53 +00006191 char zContext[100];
drh2e38c322004-09-03 18:38:44 +00006192 char *hit;
drh5eddca62001-06-30 21:53:53 +00006193
danielk1977ef73ee92004-11-06 12:26:07 +00006194 sprintf(zContext, "Page %d: ", iPage);
6195
drh5eddca62001-06-30 21:53:53 +00006196 /* Check that the page exists
6197 */
drhd9cb6ac2005-10-20 07:28:17 +00006198 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00006199 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00006200 if( iPage==0 ) return 0;
6201 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh0787db62007-03-04 13:15:27 +00006202 if( (rc = getPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
drh2e38c322004-09-03 18:38:44 +00006203 checkAppendMsg(pCheck, zContext,
6204 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00006205 return 0;
6206 }
drh4b70f112004-05-02 21:12:19 +00006207 if( (rc = initPage(pPage, pParent))!=0 ){
drh2e38c322004-09-03 18:38:44 +00006208 checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00006209 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00006210 return 0;
6211 }
6212
6213 /* Check out all the cells.
6214 */
6215 depth = 0;
drh1dcdbc02007-01-27 02:24:54 +00006216 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
drh6f11bef2004-05-13 01:12:56 +00006217 u8 *pCell;
6218 int sz;
6219 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00006220
6221 /* Check payload overflow pages
6222 */
drh3a4c1412004-05-09 20:40:11 +00006223 sprintf(zContext, "On tree page %d cell %d: ", iPage, i);
drh43605152004-05-29 21:46:49 +00006224 pCell = findCell(pPage,i);
6225 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006226 sz = info.nData;
6227 if( !pPage->intKey ) sz += info.nKey;
drh72365832007-03-06 15:53:44 +00006228 assert( sz==info.nPayload );
drh6f11bef2004-05-13 01:12:56 +00006229 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00006230 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00006231 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6232#ifndef SQLITE_OMIT_AUTOVACUUM
6233 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006234 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00006235 }
6236#endif
6237 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00006238 }
6239
6240 /* Check sanity of left child page.
6241 */
drhda200cc2004-05-09 11:51:38 +00006242 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006243 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00006244#ifndef SQLITE_OMIT_AUTOVACUUM
6245 if( pBt->autoVacuum ){
6246 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
6247 }
6248#endif
drh74161702006-02-24 02:53:49 +00006249 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
drhda200cc2004-05-09 11:51:38 +00006250 if( i>0 && d2!=depth ){
6251 checkAppendMsg(pCheck, zContext, "Child page depth differs");
6252 }
6253 depth = d2;
drh5eddca62001-06-30 21:53:53 +00006254 }
drh5eddca62001-06-30 21:53:53 +00006255 }
drhda200cc2004-05-09 11:51:38 +00006256 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006257 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drhda200cc2004-05-09 11:51:38 +00006258 sprintf(zContext, "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00006259#ifndef SQLITE_OMIT_AUTOVACUUM
6260 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006261 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00006262 }
6263#endif
drh74161702006-02-24 02:53:49 +00006264 checkTreePage(pCheck, pgno, pPage, zContext);
drhda200cc2004-05-09 11:51:38 +00006265 }
drh5eddca62001-06-30 21:53:53 +00006266
6267 /* Check for complete coverage of the page
6268 */
drhda200cc2004-05-09 11:51:38 +00006269 data = pPage->aData;
6270 hdr = pPage->hdrOffset;
drh2e38c322004-09-03 18:38:44 +00006271 hit = sqliteMalloc( usableSize );
6272 if( hit ){
6273 memset(hit, 1, get2byte(&data[hdr+5]));
6274 nCell = get2byte(&data[hdr+3]);
6275 cellStart = hdr + 12 - 4*pPage->leaf;
6276 for(i=0; i<nCell; i++){
6277 int pc = get2byte(&data[cellStart+i*2]);
6278 int size = cellSizePtr(pPage, &data[pc]);
6279 int j;
danielk19777701e812005-01-10 12:59:51 +00006280 if( (pc+size-1)>=usableSize || pc<0 ){
6281 checkAppendMsg(pCheck, 0,
6282 "Corruption detected in cell %d on page %d",i,iPage,0);
6283 }else{
6284 for(j=pc+size-1; j>=pc; j--) hit[j]++;
6285 }
drh2e38c322004-09-03 18:38:44 +00006286 }
6287 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
6288 cnt++){
6289 int size = get2byte(&data[i+2]);
6290 int j;
danielk19777701e812005-01-10 12:59:51 +00006291 if( (i+size-1)>=usableSize || i<0 ){
6292 checkAppendMsg(pCheck, 0,
6293 "Corruption detected in cell %d on page %d",i,iPage,0);
6294 }else{
6295 for(j=i+size-1; j>=i; j--) hit[j]++;
6296 }
drh2e38c322004-09-03 18:38:44 +00006297 i = get2byte(&data[i]);
6298 }
6299 for(i=cnt=0; i<usableSize; i++){
6300 if( hit[i]==0 ){
6301 cnt++;
6302 }else if( hit[i]>1 ){
6303 checkAppendMsg(pCheck, 0,
6304 "Multiple uses for byte %d of page %d", i, iPage);
6305 break;
6306 }
6307 }
6308 if( cnt!=data[hdr+7] ){
6309 checkAppendMsg(pCheck, 0,
6310 "Fragmented space is %d byte reported as %d on page %d",
6311 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00006312 }
6313 }
drh2e38c322004-09-03 18:38:44 +00006314 sqliteFree(hit);
drh6019e162001-07-02 17:51:45 +00006315
drh4b70f112004-05-02 21:12:19 +00006316 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00006317 return depth+1;
drh5eddca62001-06-30 21:53:53 +00006318}
drhb7f91642004-10-31 02:22:47 +00006319#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006320
drhb7f91642004-10-31 02:22:47 +00006321#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006322/*
6323** This routine does a complete check of the given BTree file. aRoot[] is
6324** an array of pages numbers were each page number is the root page of
6325** a table. nRoot is the number of entries in aRoot.
6326**
6327** If everything checks out, this routine returns NULL. If something is
6328** amiss, an error message is written into memory obtained from malloc()
6329** and a pointer to that error message is returned. The calling function
6330** is responsible for freeing the error message when it is done.
6331*/
drh1dcdbc02007-01-27 02:24:54 +00006332char *sqlite3BtreeIntegrityCheck(
6333 Btree *p, /* The btree to be checked */
6334 int *aRoot, /* An array of root pages numbers for individual trees */
6335 int nRoot, /* Number of entries in aRoot[] */
6336 int mxErr, /* Stop reporting errors after this many */
6337 int *pnErr /* Write number of errors seen to this variable */
6338){
drh5eddca62001-06-30 21:53:53 +00006339 int i;
6340 int nRef;
drhaaab5722002-02-19 13:39:21 +00006341 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:01 +00006342 BtShared *pBt = p->pBt;
drh5eddca62001-06-30 21:53:53 +00006343
danielk19773b8a05f2007-03-19 17:44:26 +00006344 nRef = sqlite3PagerRefcount(pBt->pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00006345 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
drhefc251d2001-07-01 22:12:01 +00006346 return sqliteStrDup("Unable to acquire a read lock on the database");
6347 }
drh5eddca62001-06-30 21:53:53 +00006348 sCheck.pBt = pBt;
6349 sCheck.pPager = pBt->pPager;
danielk19773b8a05f2007-03-19 17:44:26 +00006350 sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
drh1dcdbc02007-01-27 02:24:54 +00006351 sCheck.mxErr = mxErr;
6352 sCheck.nErr = 0;
6353 *pnErr = 0;
drh0de8c112002-07-06 16:32:14 +00006354 if( sCheck.nPage==0 ){
6355 unlockBtreeIfUnused(pBt);
6356 return 0;
6357 }
drh8c1238a2003-01-02 14:43:55 +00006358 sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00006359 if( !sCheck.anRef ){
6360 unlockBtreeIfUnused(pBt);
drh1dcdbc02007-01-27 02:24:54 +00006361 *pnErr = 1;
danielk1977ac245ec2005-01-14 13:50:11 +00006362 return sqlite3MPrintf("Unable to malloc %d bytes",
6363 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
6364 }
drhda200cc2004-05-09 11:51:38 +00006365 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00006366 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00006367 if( i<=sCheck.nPage ){
6368 sCheck.anRef[i] = 1;
6369 }
drh5eddca62001-06-30 21:53:53 +00006370 sCheck.zErrMsg = 0;
6371
6372 /* Check the integrity of the freelist
6373 */
drha34b6762004-05-07 13:30:42 +00006374 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
6375 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00006376
6377 /* Check all the tables.
6378 */
drh1dcdbc02007-01-27 02:24:54 +00006379 for(i=0; i<nRoot && sCheck.mxErr; i++){
drh4ff6dfa2002-03-03 23:06:00 +00006380 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00006381#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006382 if( pBt->autoVacuum && aRoot[i]>1 ){
6383 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
6384 }
6385#endif
drh74161702006-02-24 02:53:49 +00006386 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
drh5eddca62001-06-30 21:53:53 +00006387 }
6388
6389 /* Make sure every page in the file is referenced
6390 */
drh1dcdbc02007-01-27 02:24:54 +00006391 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:42 +00006392#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00006393 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00006394 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00006395 }
danielk1977afcdd022004-10-31 16:25:42 +00006396#else
6397 /* If the database supports auto-vacuum, make sure no tables contain
6398 ** references to pointer-map pages.
6399 */
6400 if( sCheck.anRef[i]==0 &&
danielk1977266664d2006-02-10 08:24:21 +00006401 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006402 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
6403 }
6404 if( sCheck.anRef[i]!=0 &&
danielk1977266664d2006-02-10 08:24:21 +00006405 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006406 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
6407 }
6408#endif
drh5eddca62001-06-30 21:53:53 +00006409 }
6410
6411 /* Make sure this analysis did not leave any unref() pages
6412 */
drh5e00f6c2001-09-13 13:46:56 +00006413 unlockBtreeIfUnused(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +00006414 if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
drh2e38c322004-09-03 18:38:44 +00006415 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00006416 "Outstanding page count goes from %d to %d during this analysis",
danielk19773b8a05f2007-03-19 17:44:26 +00006417 nRef, sqlite3PagerRefcount(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00006418 );
drh5eddca62001-06-30 21:53:53 +00006419 }
6420
6421 /* Clean up and report errors.
6422 */
6423 sqliteFree(sCheck.anRef);
drh1dcdbc02007-01-27 02:24:54 +00006424 *pnErr = sCheck.nErr;
drh5eddca62001-06-30 21:53:53 +00006425 return sCheck.zErrMsg;
6426}
drhb7f91642004-10-31 02:22:47 +00006427#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00006428
drh73509ee2003-04-06 20:44:45 +00006429/*
6430** Return the full pathname of the underlying database file.
6431*/
danielk1977aef0bf62005-12-30 16:28:01 +00006432const char *sqlite3BtreeGetFilename(Btree *p){
6433 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006434 return sqlite3PagerFilename(p->pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00006435}
6436
6437/*
danielk19775865e3d2004-06-14 06:03:57 +00006438** Return the pathname of the directory that contains the database file.
6439*/
danielk1977aef0bf62005-12-30 16:28:01 +00006440const char *sqlite3BtreeGetDirname(Btree *p){
6441 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006442 return sqlite3PagerDirname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006443}
6444
6445/*
6446** Return the pathname of the journal file for this database. The return
6447** value of this routine is the same regardless of whether the journal file
6448** has been created or not.
6449*/
danielk1977aef0bf62005-12-30 16:28:01 +00006450const char *sqlite3BtreeGetJournalname(Btree *p){
6451 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006452 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006453}
6454
drhb7f91642004-10-31 02:22:47 +00006455#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00006456/*
drhf7c57532003-04-25 13:22:51 +00006457** Copy the complete content of pBtFrom into pBtTo. A transaction
6458** must be active for both files.
6459**
6460** The size of file pBtFrom may be reduced by this operation.
drh43605152004-05-29 21:46:49 +00006461** If anything goes wrong, the transaction on pBtFrom is rolled back.
drh73509ee2003-04-06 20:44:45 +00006462*/
danielk1977aef0bf62005-12-30 16:28:01 +00006463int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
drhf7c57532003-04-25 13:22:51 +00006464 int rc = SQLITE_OK;
drh50f2f432005-09-16 11:32:18 +00006465 Pgno i, nPage, nToPage, iSkip;
drhf7c57532003-04-25 13:22:51 +00006466
danielk1977aef0bf62005-12-30 16:28:01 +00006467 BtShared *pBtTo = pTo->pBt;
6468 BtShared *pBtFrom = pFrom->pBt;
6469
6470 if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
danielk1977ee5741e2004-05-31 10:01:34 +00006471 return SQLITE_ERROR;
6472 }
drhf7c57532003-04-25 13:22:51 +00006473 if( pBtTo->pCursor ) return SQLITE_BUSY;
danielk19773b8a05f2007-03-19 17:44:26 +00006474 nToPage = sqlite3PagerPagecount(pBtTo->pPager);
6475 nPage = sqlite3PagerPagecount(pBtFrom->pPager);
drh50f2f432005-09-16 11:32:18 +00006476 iSkip = PENDING_BYTE_PAGE(pBtTo);
danielk1977369f27e2004-06-15 11:40:04 +00006477 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006478 DbPage *pDbPage;
drh50f2f432005-09-16 11:32:18 +00006479 if( i==iSkip ) continue;
danielk19773b8a05f2007-03-19 17:44:26 +00006480 rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage);
drhf7c57532003-04-25 13:22:51 +00006481 if( rc ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006482 rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage));
6483 sqlite3PagerUnref(pDbPage);
drhf7c57532003-04-25 13:22:51 +00006484 }
drh2e6d11b2003-04-25 15:37:57 +00006485 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006486 DbPage *pDbPage;
drh49285702005-09-17 15:20:26 +00006487 if( i==iSkip ) continue;
danielk19773b8a05f2007-03-19 17:44:26 +00006488 rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
drh2e6d11b2003-04-25 15:37:57 +00006489 if( rc ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006490 rc = sqlite3PagerWrite(pDbPage);
6491 sqlite3PagerUnref(pDbPage);
6492 sqlite3PagerDontWrite(pBtTo->pPager, i);
drh2e6d11b2003-04-25 15:37:57 +00006493 }
6494 if( !rc && nPage<nToPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00006495 rc = sqlite3PagerTruncate(pBtTo->pPager, nPage);
drh2e6d11b2003-04-25 15:37:57 +00006496 }
drhf7c57532003-04-25 13:22:51 +00006497 if( rc ){
danielk1977aef0bf62005-12-30 16:28:01 +00006498 sqlite3BtreeRollback(pTo);
drhf7c57532003-04-25 13:22:51 +00006499 }
6500 return rc;
drh73509ee2003-04-06 20:44:45 +00006501}
drhb7f91642004-10-31 02:22:47 +00006502#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00006503
6504/*
6505** Return non-zero if a transaction is active.
6506*/
danielk1977aef0bf62005-12-30 16:28:01 +00006507int sqlite3BtreeIsInTrans(Btree *p){
6508 return (p && (p->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00006509}
6510
6511/*
6512** Return non-zero if a statement transaction is active.
6513*/
danielk1977aef0bf62005-12-30 16:28:01 +00006514int sqlite3BtreeIsInStmt(Btree *p){
6515 return (p->pBt && p->pBt->inStmt);
danielk19771d850a72004-05-31 08:26:49 +00006516}
danielk197713adf8a2004-06-03 16:08:41 +00006517
6518/*
danielk19772372c2b2006-06-27 16:34:56 +00006519** Return non-zero if a read (or write) transaction is active.
6520*/
6521int sqlite3BtreeIsInReadTrans(Btree *p){
6522 return (p && (p->inTrans!=TRANS_NONE));
6523}
6524
6525/*
drh6e345992007-03-30 11:12:08 +00006526** This routine does the first phase of a 2-phase commit. This routine
6527** causes a rollback journal to be created (if it does not already exist)
6528** and populated with enough information so that if a power loss occurs
6529** the database can be restored to its original state by playing back
6530** the journal. Then the contents of the journal are flushed out to
6531** the disk. After the journal is safely on oxide, the changes to the
6532** database are written into the database file and flushed to oxide.
6533** At the end of this call, the rollback journal still exists on the
6534** disk and we are still holding all locks, so the transaction has not
6535** committed. See sqlite3BtreeCommit() for the second phase of the
6536** commit process.
6537**
danielk197713adf8a2004-06-03 16:08:41 +00006538** This call is a no-op if no write-transaction is currently active on pBt.
6539**
6540** Otherwise, sync the database file for the btree pBt. zMaster points to
6541** the name of a master journal file that should be written into the
6542** individual journal file, or is NULL, indicating no master journal file
6543** (single database transaction).
6544**
6545** When this is called, the master journal should already have been
6546** created, populated with this journal pointer and synced to disk.
6547**
6548** Once this is routine has returned, the only thing required to commit
6549** the write-transaction for this database file is to delete the journal.
6550*/
danielk1977aef0bf62005-12-30 16:28:01 +00006551int sqlite3BtreeSync(Btree *p, const char *zMaster){
danielk1977266664d2006-02-10 08:24:21 +00006552 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00006553 if( p->inTrans==TRANS_WRITE ){
6554 BtShared *pBt = p->pBt;
danielk1977d761c0c2004-11-05 16:37:02 +00006555 Pgno nTrunc = 0;
danielk1977266664d2006-02-10 08:24:21 +00006556#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006557 if( pBt->autoVacuum ){
danielk1977266664d2006-02-10 08:24:21 +00006558 rc = autoVacuumCommit(pBt, &nTrunc);
6559 if( rc!=SQLITE_OK ){
6560 return rc;
6561 }
danielk1977687566d2004-11-02 12:56:41 +00006562 }
6563#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006564 rc = sqlite3PagerSync(pBt->pPager, zMaster, nTrunc);
danielk197713adf8a2004-06-03 16:08:41 +00006565 }
danielk1977266664d2006-02-10 08:24:21 +00006566 return rc;
danielk197713adf8a2004-06-03 16:08:41 +00006567}
danielk1977aef0bf62005-12-30 16:28:01 +00006568
danielk1977da184232006-01-05 11:34:32 +00006569/*
6570** This function returns a pointer to a blob of memory associated with
6571** a single shared-btree. The memory is used by client code for it's own
6572** purposes (for example, to store a high-level schema associated with
6573** the shared-btree). The btree layer manages reference counting issues.
6574**
6575** The first time this is called on a shared-btree, nBytes bytes of memory
6576** are allocated, zeroed, and returned to the caller. For each subsequent
6577** call the nBytes parameter is ignored and a pointer to the same blob
6578** of memory returned.
6579**
6580** Just before the shared-btree is closed, the function passed as the
6581** xFree argument when the memory allocation was made is invoked on the
6582** blob of allocated memory. This function should not call sqliteFree()
6583** on the memory, the btree layer does that.
6584*/
6585void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
6586 BtShared *pBt = p->pBt;
6587 if( !pBt->pSchema ){
6588 pBt->pSchema = sqliteMalloc(nBytes);
6589 pBt->xFreeSchema = xFree;
6590 }
6591 return pBt->pSchema;
6592}
6593
danielk1977c87d34d2006-01-06 13:00:28 +00006594/*
6595** Return true if another user of the same shared btree as the argument
6596** handle holds an exclusive lock on the sqlite_master table.
6597*/
6598int sqlite3BtreeSchemaLocked(Btree *p){
6599 return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
6600}
6601
drha154dcd2006-03-22 22:10:07 +00006602
6603#ifndef SQLITE_OMIT_SHARED_CACHE
6604/*
6605** Obtain a lock on the table whose root page is iTab. The
6606** lock is a write lock if isWritelock is true or a read lock
6607** if it is false.
6608*/
danielk1977c00da102006-01-07 13:21:04 +00006609int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:27 +00006610 int rc = SQLITE_OK;
danielk1977c00da102006-01-07 13:21:04 +00006611 u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
danielk19772e94d4d2006-01-09 05:36:27 +00006612 rc = queryTableLock(p, iTab, lockType);
danielk1977c00da102006-01-07 13:21:04 +00006613 if( rc==SQLITE_OK ){
6614 rc = lockTable(p, iTab, lockType);
6615 }
6616 return rc;
6617}
drha154dcd2006-03-22 22:10:07 +00006618#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00006619
drh6f7adc82006-01-11 21:41:20 +00006620/*
6621** The following debugging interface has to be in this file (rather
6622** than in, for example, test1.c) so that it can get access to
6623** the definition of BtShared.
6624*/
danielk197707cb5602006-01-20 10:55:05 +00006625#if defined(SQLITE_DEBUG) && defined(TCLSH)
danielk1977b82e7ed2006-01-11 14:09:31 +00006626#include <tcl.h>
6627int sqlite3_shared_cache_report(
6628 void * clientData,
6629 Tcl_Interp *interp,
6630 int objc,
6631 Tcl_Obj *CONST objv[]
6632){
drha154dcd2006-03-22 22:10:07 +00006633#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00006634 const ThreadData *pTd = sqlite3ThreadDataReadOnly();
danielk1977b82e7ed2006-01-11 14:09:31 +00006635 if( pTd->useSharedData ){
6636 BtShared *pBt;
6637 Tcl_Obj *pRet = Tcl_NewObj();
6638 for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){
danielk19773b8a05f2007-03-19 17:44:26 +00006639 const char *zFile = sqlite3PagerFilename(pBt->pPager);
danielk1977b82e7ed2006-01-11 14:09:31 +00006640 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
6641 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
6642 }
6643 Tcl_SetObjResult(interp, pRet);
6644 }
drha154dcd2006-03-22 22:10:07 +00006645#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00006646 return TCL_OK;
6647}
6648#endif