blob: 412e48a05dbe3786c2686056d38afbcc17105c34 [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*************************************************************************
danielk1977dddbcdc2007-04-26 14:42:34 +000012** $Id: btree.c,v 1.359 2007/04/26 14:42:35 danielk1977 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
danielk1977dddbcdc2007-04-26 14:42:34 +0000339 u8 autoVacuum; /* True if auto-vacuum is enabled */
340 u8 incrVacuum; /* True if incr-vacuum is enabled */
341 Pgno nTrunc; /* Non-zero if the db will be truncated (incr vacuum) */
drh057cd3a2005-02-15 16:23:02 +0000342#endif
drha2fce642004-06-05 00:01:44 +0000343 u16 pageSize; /* Total number of bytes on a page */
344 u16 usableSize; /* Number of usable bytes on each page */
drh6f11bef2004-05-13 01:12:56 +0000345 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
346 int minLocal; /* Minimum local payload in non-LEAFDATA tables */
347 int maxLeaf; /* Maximum local payload in a LEAFDATA table */
348 int minLeaf; /* Minimum local payload in a LEAFDATA table */
drhb8ef32c2005-03-14 02:01:49 +0000349 BusyHandler *pBusyHandler; /* Callback for when there is lock contention */
danielk1977aef0bf62005-12-30 16:28:01 +0000350 u8 inTransaction; /* Transaction state */
danielk1977aef0bf62005-12-30 16:28:01 +0000351 int nRef; /* Number of references to this structure */
352 int nTransaction; /* Number of open transactions (read + write) */
danielk19772e94d4d2006-01-09 05:36:27 +0000353 void *pSchema; /* Pointer to space allocated by sqlite3BtreeSchema() */
354 void (*xFreeSchema)(void*); /* Destructor for BtShared.pSchema */
355#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000356 BtLock *pLock; /* List of locks held on this shared-btree struct */
danielk1977e501b892006-01-09 06:29:47 +0000357 BtShared *pNext; /* Next in ThreadData.pBtree linked list */
danielk19772e94d4d2006-01-09 05:36:27 +0000358#endif
drha059ad02001-04-17 20:09:11 +0000359};
danielk1977ee5741e2004-05-31 10:01:34 +0000360
361/*
drhfa1a98a2004-05-14 19:08:17 +0000362** An instance of the following structure is used to hold information
drh271efa52004-05-30 19:19:05 +0000363** about a cell. The parseCellPtr() function fills in this structure
drhab01f612004-05-22 02:55:23 +0000364** based on information extract from the raw disk page.
drhfa1a98a2004-05-14 19:08:17 +0000365*/
366typedef struct CellInfo CellInfo;
367struct CellInfo {
drh43605152004-05-29 21:46:49 +0000368 u8 *pCell; /* Pointer to the start of cell content */
drhfa1a98a2004-05-14 19:08:17 +0000369 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
370 u32 nData; /* Number of bytes of data */
drh72365832007-03-06 15:53:44 +0000371 u32 nPayload; /* Total amount of payload */
drh271efa52004-05-30 19:19:05 +0000372 u16 nHeader; /* Size of the cell content header in bytes */
drhfa1a98a2004-05-14 19:08:17 +0000373 u16 nLocal; /* Amount of payload held locally */
drhab01f612004-05-22 02:55:23 +0000374 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
drh271efa52004-05-30 19:19:05 +0000375 u16 nSize; /* Size of the cell content on the main b-tree page */
drhfa1a98a2004-05-14 19:08:17 +0000376};
377
378/*
drh365d68f2001-05-11 11:02:46 +0000379** A cursor is a pointer to a particular entry in the BTree.
380** The entry is identified by its MemPage and the index in
drha34b6762004-05-07 13:30:42 +0000381** MemPage.aCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000382*/
drh72f82862001-05-24 21:06:34 +0000383struct BtCursor {
danielk1977aef0bf62005-12-30 16:28:01 +0000384 Btree *pBtree; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000385 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drh3aac2dd2004-04-26 14:10:20 +0000386 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
387 void *pArg; /* First arg to xCompare() */
drh8b2f49b2001-06-08 00:21:52 +0000388 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000389 MemPage *pPage; /* Page that contains the entry */
drh3aac2dd2004-04-26 14:10:20 +0000390 int idx; /* Index of the entry in pPage->aCell[] */
drhfa1a98a2004-05-14 19:08:17 +0000391 CellInfo info; /* A parse of the cell we are pointing at */
drhecdc7532001-09-23 02:35:53 +0000392 u8 wrFlag; /* True if writable */
danielk1977da184232006-01-05 11:34:32 +0000393 u8 eState; /* One of the CURSOR_XXX constants (see below) */
drh4eeb1ff2006-03-23 14:03:00 +0000394 void *pKey; /* Saved key that was cursor's last known position */
395 i64 nKey; /* Size of pKey, or last integer key */
danielk1977da184232006-01-05 11:34:32 +0000396 int skip; /* (skip<0) -> Prev() is a no-op. (skip>0) -> Next() is */
drh365d68f2001-05-11 11:02:46 +0000397};
drh7e3b0a02001-04-28 16:52:40 +0000398
drha059ad02001-04-17 20:09:11 +0000399/*
drh980b1a72006-08-16 16:42:48 +0000400** Potential values for BtCursor.eState.
danielk1977da184232006-01-05 11:34:32 +0000401**
402** CURSOR_VALID:
403** Cursor points to a valid entry. getPayload() etc. may be called.
404**
405** CURSOR_INVALID:
406** Cursor does not point to a valid entry. This can happen (for example)
407** because the table is empty or because BtreeCursorFirst() has not been
408** called.
409**
410** CURSOR_REQUIRESEEK:
411** The table that this cursor was opened on still exists, but has been
412** modified since the cursor was last used. The cursor position is saved
413** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
danielk1977955de522006-02-10 02:27:42 +0000414** this state, restoreOrClearCursorPosition() can be called to attempt to
415** seek the cursor to the saved position.
danielk1977da184232006-01-05 11:34:32 +0000416*/
417#define CURSOR_INVALID 0
418#define CURSOR_VALID 1
419#define CURSOR_REQUIRESEEK 2
420
421/*
drh615ae552005-01-16 23:21:00 +0000422** The TRACE macro will print high-level status information about the
423** btree operation when the global variable sqlite3_btree_trace is
424** enabled.
425*/
426#if SQLITE_TEST
427# define TRACE(X) if( sqlite3_btree_trace )\
drhd3627af2006-12-18 18:34:51 +0000428/* { sqlite3DebugPrintf X; fflush(stdout); } */ \
429{ printf X; fflush(stdout); }
drh0f7eb612006-08-08 13:51:43 +0000430int sqlite3_btree_trace=0; /* True to enable tracing */
drh615ae552005-01-16 23:21:00 +0000431#else
432# define TRACE(X)
433#endif
drh615ae552005-01-16 23:21:00 +0000434
435/*
drh66cbd152004-09-01 16:12:25 +0000436** Forward declaration
437*/
drh980b1a72006-08-16 16:42:48 +0000438static int checkReadLocks(Btree*,Pgno,BtCursor*);
drh66cbd152004-09-01 16:12:25 +0000439
drh66cbd152004-09-01 16:12:25 +0000440/*
drhab01f612004-05-22 02:55:23 +0000441** Read or write a two- and four-byte big-endian integer values.
drh0d316a42002-08-11 20:10:47 +0000442*/
drh9e572e62004-04-23 23:43:10 +0000443static u32 get2byte(unsigned char *p){
444 return (p[0]<<8) | p[1];
drh0d316a42002-08-11 20:10:47 +0000445}
drh9e572e62004-04-23 23:43:10 +0000446static u32 get4byte(unsigned char *p){
447 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
448}
drh9e572e62004-04-23 23:43:10 +0000449static void put2byte(unsigned char *p, u32 v){
450 p[0] = v>>8;
451 p[1] = v;
452}
453static void put4byte(unsigned char *p, u32 v){
454 p[0] = v>>24;
455 p[1] = v>>16;
456 p[2] = v>>8;
457 p[3] = v;
458}
drh6f11bef2004-05-13 01:12:56 +0000459
drh9e572e62004-04-23 23:43:10 +0000460/*
drhab01f612004-05-22 02:55:23 +0000461** Routines to read and write variable-length integers. These used to
462** be defined locally, but now we use the varint routines in the util.c
463** file.
drh9e572e62004-04-23 23:43:10 +0000464*/
drh6d2fb152004-05-14 16:50:06 +0000465#define getVarint sqlite3GetVarint
drh504b6982006-01-22 21:52:56 +0000466/* #define getVarint32 sqlite3GetVarint32 */
467#define getVarint32(A,B) ((*B=*(A))<=0x7f?1:sqlite3GetVarint32(A,B))
drh6d2fb152004-05-14 16:50:06 +0000468#define putVarint sqlite3PutVarint
drh0d316a42002-08-11 20:10:47 +0000469
danielk1977599fcba2004-11-08 07:13:13 +0000470/* The database page the PENDING_BYTE occupies. This page is never used.
471** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
472** should possibly be consolidated (presumably in pager.h).
drhfe9a9142006-03-14 12:59:10 +0000473**
474** If disk I/O is omitted (meaning that the database is stored purely
475** in memory) then there is no pending byte.
danielk1977599fcba2004-11-08 07:13:13 +0000476*/
drhfe9a9142006-03-14 12:59:10 +0000477#ifdef SQLITE_OMIT_DISKIO
478# define PENDING_BYTE_PAGE(pBt) 0x7fffffff
479#else
480# define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
481#endif
danielk1977afcdd022004-10-31 16:25:42 +0000482
danielk1977aef0bf62005-12-30 16:28:01 +0000483/*
484** A linked list of the following structures is stored at BtShared.pLock.
485** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
486** is opened on the table with root page BtShared.iTable. Locks are removed
487** from this list when a transaction is committed or rolled back, or when
488** a btree handle is closed.
489*/
490struct BtLock {
491 Btree *pBtree; /* Btree handle holding this lock */
492 Pgno iTable; /* Root page of table */
493 u8 eLock; /* READ_LOCK or WRITE_LOCK */
494 BtLock *pNext; /* Next in BtShared.pLock list */
495};
496
497/* Candidate values for BtLock.eLock */
498#define READ_LOCK 1
499#define WRITE_LOCK 2
500
501#ifdef SQLITE_OMIT_SHARED_CACHE
502 /*
503 ** The functions queryTableLock(), lockTable() and unlockAllTables()
504 ** manipulate entries in the BtShared.pLock linked list used to store
505 ** shared-cache table level locks. If the library is compiled with the
506 ** shared-cache feature disabled, then there is only ever one user
danielk1977da184232006-01-05 11:34:32 +0000507 ** of each BtShared structure and so this locking is not necessary.
508 ** So define the lock related functions as no-ops.
danielk1977aef0bf62005-12-30 16:28:01 +0000509 */
510 #define queryTableLock(a,b,c) SQLITE_OK
511 #define lockTable(a,b,c) SQLITE_OK
danielk1977da184232006-01-05 11:34:32 +0000512 #define unlockAllTables(a)
danielk1977aef0bf62005-12-30 16:28:01 +0000513#else
514
danielk1977da184232006-01-05 11:34:32 +0000515/*
danielk1977aef0bf62005-12-30 16:28:01 +0000516** Query to see if btree handle p may obtain a lock of type eLock
517** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
518** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
danielk1977c87d34d2006-01-06 13:00:28 +0000519** SQLITE_LOCKED if not.
danielk1977aef0bf62005-12-30 16:28:01 +0000520*/
521static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
522 BtShared *pBt = p->pBt;
523 BtLock *pIter;
524
danielk1977da184232006-01-05 11:34:32 +0000525 /* This is a no-op if the shared-cache is not enabled */
drh6f7adc82006-01-11 21:41:20 +0000526 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
danielk1977da184232006-01-05 11:34:32 +0000527 return SQLITE_OK;
528 }
529
530 /* This (along with lockTable()) is where the ReadUncommitted flag is
531 ** dealt with. If the caller is querying for a read-lock and the flag is
532 ** set, it is unconditionally granted - even if there are write-locks
533 ** on the table. If a write-lock is requested, the ReadUncommitted flag
534 ** is not considered.
535 **
536 ** In function lockTable(), if a read-lock is demanded and the
537 ** ReadUncommitted flag is set, no entry is added to the locks list
538 ** (BtShared.pLock).
539 **
540 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
541 ** not create or respect table locks. The locking procedure for a
542 ** write-cursor does not change.
543 */
544 if(
545 !p->pSqlite ||
546 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) ||
547 eLock==WRITE_LOCK ||
drh47ded162006-01-06 01:42:58 +0000548 iTab==MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000549 ){
550 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
551 if( pIter->pBtree!=p && pIter->iTable==iTab &&
552 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
danielk1977c87d34d2006-01-06 13:00:28 +0000553 return SQLITE_LOCKED;
danielk1977da184232006-01-05 11:34:32 +0000554 }
danielk1977aef0bf62005-12-30 16:28:01 +0000555 }
556 }
557 return SQLITE_OK;
558}
559
560/*
561** Add a lock on the table with root-page iTable to the shared-btree used
562** by Btree handle p. Parameter eLock must be either READ_LOCK or
563** WRITE_LOCK.
564**
565** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
566** SQLITE_NOMEM may also be returned.
567*/
568static int lockTable(Btree *p, Pgno iTable, u8 eLock){
569 BtShared *pBt = p->pBt;
570 BtLock *pLock = 0;
571 BtLock *pIter;
572
danielk1977da184232006-01-05 11:34:32 +0000573 /* This is a no-op if the shared-cache is not enabled */
drh6f7adc82006-01-11 21:41:20 +0000574 if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){
danielk1977da184232006-01-05 11:34:32 +0000575 return SQLITE_OK;
576 }
577
danielk1977aef0bf62005-12-30 16:28:01 +0000578 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
579
danielk1977da184232006-01-05 11:34:32 +0000580 /* If the read-uncommitted flag is set and a read-lock is requested,
581 ** return early without adding an entry to the BtShared.pLock list. See
582 ** comment in function queryTableLock() for more info on handling
583 ** the ReadUncommitted flag.
584 */
585 if(
586 (p->pSqlite) &&
587 (p->pSqlite->flags&SQLITE_ReadUncommitted) &&
588 (eLock==READ_LOCK) &&
drh47ded162006-01-06 01:42:58 +0000589 iTable!=MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000590 ){
591 return SQLITE_OK;
592 }
593
danielk1977aef0bf62005-12-30 16:28:01 +0000594 /* First search the list for an existing lock on this table. */
595 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
596 if( pIter->iTable==iTable && pIter->pBtree==p ){
597 pLock = pIter;
598 break;
599 }
600 }
601
602 /* If the above search did not find a BtLock struct associating Btree p
603 ** with table iTable, allocate one and link it into the list.
604 */
605 if( !pLock ){
606 pLock = (BtLock *)sqliteMalloc(sizeof(BtLock));
607 if( !pLock ){
608 return SQLITE_NOMEM;
609 }
610 pLock->iTable = iTable;
611 pLock->pBtree = p;
612 pLock->pNext = pBt->pLock;
613 pBt->pLock = pLock;
614 }
615
616 /* Set the BtLock.eLock variable to the maximum of the current lock
617 ** and the requested lock. This means if a write-lock was already held
618 ** and a read-lock requested, we don't incorrectly downgrade the lock.
619 */
620 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53 +0000621 if( eLock>pLock->eLock ){
622 pLock->eLock = eLock;
623 }
danielk1977aef0bf62005-12-30 16:28:01 +0000624
625 return SQLITE_OK;
626}
627
628/*
629** Release all the table locks (locks obtained via calls to the lockTable()
630** procedure) held by Btree handle p.
631*/
632static void unlockAllTables(Btree *p){
633 BtLock **ppIter = &p->pBt->pLock;
danielk1977da184232006-01-05 11:34:32 +0000634
635 /* If the shared-cache extension is not enabled, there should be no
636 ** locks in the BtShared.pLock list, making this procedure a no-op. Assert
637 ** that this is the case.
638 */
drh6f7adc82006-01-11 21:41:20 +0000639 assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter );
danielk1977da184232006-01-05 11:34:32 +0000640
danielk1977aef0bf62005-12-30 16:28:01 +0000641 while( *ppIter ){
642 BtLock *pLock = *ppIter;
643 if( pLock->pBtree==p ){
644 *ppIter = pLock->pNext;
645 sqliteFree(pLock);
646 }else{
647 ppIter = &pLock->pNext;
648 }
649 }
650}
651#endif /* SQLITE_OMIT_SHARED_CACHE */
652
drh980b1a72006-08-16 16:42:48 +0000653static void releasePage(MemPage *pPage); /* Forward reference */
654
655/*
656** Save the current cursor position in the variables BtCursor.nKey
657** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
658*/
659static int saveCursorPosition(BtCursor *pCur){
660 int rc;
661
662 assert( CURSOR_VALID==pCur->eState );
663 assert( 0==pCur->pKey );
664
665 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
666
667 /* If this is an intKey table, then the above call to BtreeKeySize()
668 ** stores the integer key in pCur->nKey. In this case this value is
669 ** all that is required. Otherwise, if pCur is not open on an intKey
670 ** table, then malloc space for and store the pCur->nKey bytes of key
671 ** data.
672 */
673 if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
674 void *pKey = sqliteMalloc(pCur->nKey);
675 if( pKey ){
676 rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
677 if( rc==SQLITE_OK ){
678 pCur->pKey = pKey;
679 }else{
680 sqliteFree(pKey);
681 }
682 }else{
683 rc = SQLITE_NOMEM;
684 }
685 }
686 assert( !pCur->pPage->intKey || !pCur->pKey );
687
688 if( rc==SQLITE_OK ){
689 releasePage(pCur->pPage);
690 pCur->pPage = 0;
691 pCur->eState = CURSOR_REQUIRESEEK;
692 }
693
694 return rc;
695}
696
697/*
698** Save the positions of all cursors except pExcept open on the table
699** with root-page iRoot. Usually, this is called just before cursor
700** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
701*/
702static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
703 BtCursor *p;
704 for(p=pBt->pCursor; p; p=p->pNext){
705 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
706 p->eState==CURSOR_VALID ){
707 int rc = saveCursorPosition(p);
708 if( SQLITE_OK!=rc ){
709 return rc;
710 }
711 }
712 }
713 return SQLITE_OK;
714}
715
716/*
drhbf700f32007-03-31 02:36:44 +0000717** Clear the current cursor position.
718*/
719static void clearCursorPosition(BtCursor *pCur){
720 sqliteFree(pCur->pKey);
721 pCur->pKey = 0;
722 pCur->eState = CURSOR_INVALID;
723}
724
725/*
drh980b1a72006-08-16 16:42:48 +0000726** Restore the cursor to the position it was in (or as close to as possible)
727** when saveCursorPosition() was called. Note that this call deletes the
728** saved position info stored by saveCursorPosition(), so there can be
729** at most one effective restoreOrClearCursorPosition() call after each
730** saveCursorPosition().
731**
732** If the second argument argument - doSeek - is false, then instead of
733** returning the cursor to it's saved position, any saved position is deleted
734** and the cursor state set to CURSOR_INVALID.
735*/
drhbf700f32007-03-31 02:36:44 +0000736static int restoreOrClearCursorPositionX(BtCursor *pCur){
737 int rc;
drh980b1a72006-08-16 16:42:48 +0000738 assert( pCur->eState==CURSOR_REQUIRESEEK );
739 pCur->eState = CURSOR_INVALID;
drhbf700f32007-03-31 02:36:44 +0000740 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
drh980b1a72006-08-16 16:42:48 +0000741 if( rc==SQLITE_OK ){
742 sqliteFree(pCur->pKey);
743 pCur->pKey = 0;
drhbf700f32007-03-31 02:36:44 +0000744 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
drh980b1a72006-08-16 16:42:48 +0000745 }
746 return rc;
747}
748
drhbf700f32007-03-31 02:36:44 +0000749#define restoreOrClearCursorPosition(p) \
750 (p->eState==CURSOR_REQUIRESEEK?restoreOrClearCursorPositionX(p):SQLITE_OK)
drh980b1a72006-08-16 16:42:48 +0000751
danielk1977599fcba2004-11-08 07:13:13 +0000752#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000753/*
drh42cac6d2004-11-20 20:31:11 +0000754** These macros define the location of the pointer-map entry for a
755** database page. The first argument to each is the number of usable
756** bytes on each page of the database (often 1024). The second is the
757** page number to look up in the pointer map.
danielk1977afcdd022004-10-31 16:25:42 +0000758**
759** PTRMAP_PAGENO returns the database page number of the pointer-map
760** page that stores the required pointer. PTRMAP_PTROFFSET returns
761** the offset of the requested map entry.
762**
763** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
764** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
danielk1977599fcba2004-11-08 07:13:13 +0000765** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
766** this test.
danielk1977afcdd022004-10-31 16:25:42 +0000767*/
danielk1977266664d2006-02-10 08:24:21 +0000768#define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
769#define PTRMAP_PTROFFSET(pBt, pgno) (5*(pgno-ptrmapPageno(pBt, pgno)-1))
770#define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
771
772static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
773 int nPagesPerMapPage = (pBt->usableSize/5)+1;
774 int iPtrMap = (pgno-2)/nPagesPerMapPage;
775 int ret = (iPtrMap*nPagesPerMapPage) + 2;
776 if( ret==PENDING_BYTE_PAGE(pBt) ){
777 ret++;
778 }
779 return ret;
780}
danielk1977a19df672004-11-03 11:37:07 +0000781
danielk1977afcdd022004-10-31 16:25:42 +0000782/*
drh615ae552005-01-16 23:21:00 +0000783** The pointer map is a lookup table that identifies the parent page for
784** each child page in the database file. The parent page is the page that
785** contains a pointer to the child. Every page in the database contains
786** 0 or 1 parent pages. (In this context 'database page' refers
787** to any page that is not part of the pointer map itself.) Each pointer map
788** entry consists of a single byte 'type' and a 4 byte parent page number.
789** The PTRMAP_XXX identifiers below are the valid types.
790**
791** The purpose of the pointer map is to facility moving pages from one
792** position in the file to another as part of autovacuum. When a page
793** is moved, the pointer in its parent must be updated to point to the
794** new location. The pointer map is used to locate the parent page quickly.
danielk1977afcdd022004-10-31 16:25:42 +0000795**
danielk1977687566d2004-11-02 12:56:41 +0000796** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
797** used in this case.
danielk1977afcdd022004-10-31 16:25:42 +0000798**
danielk1977687566d2004-11-02 12:56:41 +0000799** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
800** is not used in this case.
801**
802** PTRMAP_OVERFLOW1: The database page is the first page in a list of
803** overflow pages. The page number identifies the page that
804** contains the cell with a pointer to this overflow page.
805**
806** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
807** overflow pages. The page-number identifies the previous
808** page in the overflow page list.
809**
810** PTRMAP_BTREE: The database page is a non-root btree page. The page number
811** identifies the parent page in the btree.
danielk1977afcdd022004-10-31 16:25:42 +0000812*/
danielk1977687566d2004-11-02 12:56:41 +0000813#define PTRMAP_ROOTPAGE 1
814#define PTRMAP_FREEPAGE 2
815#define PTRMAP_OVERFLOW1 3
816#define PTRMAP_OVERFLOW2 4
817#define PTRMAP_BTREE 5
danielk1977afcdd022004-10-31 16:25:42 +0000818
819/*
820** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000821**
822** This routine updates the pointer map entry for page number 'key'
823** so that it maps to type 'eType' and parent page number 'pgno'.
824** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000825*/
danielk1977aef0bf62005-12-30 16:28:01 +0000826static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
danielk19773b8a05f2007-03-19 17:44:26 +0000827 DbPage *pDbPage; /* The pointer map page */
828 u8 *pPtrmap; /* The pointer map data */
829 Pgno iPtrmap; /* The pointer map page number */
830 int offset; /* Offset in pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000831 int rc;
832
danielk1977266664d2006-02-10 08:24:21 +0000833 /* The master-journal page number must never be used as a pointer map page */
834 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
835
danielk1977ac11ee62005-01-15 12:45:51 +0000836 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000837 if( key==0 ){
drh49285702005-09-17 15:20:26 +0000838 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +0000839 }
danielk1977266664d2006-02-10 08:24:21 +0000840 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000841 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977687566d2004-11-02 12:56:41 +0000842 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000843 return rc;
844 }
danielk1977266664d2006-02-10 08:24:21 +0000845 offset = PTRMAP_PTROFFSET(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000846 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000847
drh615ae552005-01-16 23:21:00 +0000848 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
849 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk19773b8a05f2007-03-19 17:44:26 +0000850 rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000851 if( rc==SQLITE_OK ){
852 pPtrmap[offset] = eType;
853 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000854 }
danielk1977afcdd022004-10-31 16:25:42 +0000855 }
856
danielk19773b8a05f2007-03-19 17:44:26 +0000857 sqlite3PagerUnref(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000858 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000859}
860
861/*
862** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000863**
864** This routine retrieves the pointer map entry for page 'key', writing
865** the type and parent page number to *pEType and *pPgno respectively.
866** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000867*/
danielk1977aef0bf62005-12-30 16:28:01 +0000868static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:26 +0000869 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000870 int iPtrmap; /* Pointer map page index */
871 u8 *pPtrmap; /* Pointer map page data */
872 int offset; /* Offset of entry in pointer map */
873 int rc;
874
danielk1977266664d2006-02-10 08:24:21 +0000875 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000876 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000877 if( rc!=0 ){
878 return rc;
879 }
danielk19773b8a05f2007-03-19 17:44:26 +0000880 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000881
danielk1977266664d2006-02-10 08:24:21 +0000882 offset = PTRMAP_PTROFFSET(pBt, key);
drh43617e92006-03-06 20:55:46 +0000883 assert( pEType!=0 );
884 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:41 +0000885 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000886
danielk19773b8a05f2007-03-19 17:44:26 +0000887 sqlite3PagerUnref(pDbPage);
drh49285702005-09-17 15:20:26 +0000888 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
danielk1977afcdd022004-10-31 16:25:42 +0000889 return SQLITE_OK;
890}
891
892#endif /* SQLITE_OMIT_AUTOVACUUM */
893
drh0d316a42002-08-11 20:10:47 +0000894/*
drh271efa52004-05-30 19:19:05 +0000895** Given a btree page and a cell index (0 means the first cell on
896** the page, 1 means the second cell, and so forth) return a pointer
897** to the cell content.
898**
899** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000900*/
drh43605152004-05-29 21:46:49 +0000901static u8 *findCell(MemPage *pPage, int iCell){
902 u8 *data = pPage->aData;
903 assert( iCell>=0 );
904 assert( iCell<get2byte(&data[pPage->hdrOffset+3]) );
905 return data + get2byte(&data[pPage->cellOffset+2*iCell]);
906}
907
908/*
909** This a more complex version of findCell() that works for
910** pages that do contain overflow cells. See insert
911*/
912static u8 *findOverflowCell(MemPage *pPage, int iCell){
913 int i;
914 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000915 int k;
916 struct _OvflCell *pOvfl;
917 pOvfl = &pPage->aOvfl[i];
918 k = pOvfl->idx;
919 if( k<=iCell ){
920 if( k==iCell ){
921 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000922 }
923 iCell--;
924 }
925 }
926 return findCell(pPage, iCell);
927}
928
929/*
930** Parse a cell content block and fill in the CellInfo structure. There
931** are two versions of this function. parseCell() takes a cell index
932** as the second argument and parseCellPtr() takes a pointer to the
933** body of the cell as its second argument.
934*/
935static void parseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000936 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000937 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000938 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000939){
drh271efa52004-05-30 19:19:05 +0000940 int n; /* Number bytes in cell content header */
941 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000942
943 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000944 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000945 n = pPage->childPtrSize;
946 assert( n==4-4*pPage->leaf );
drh8b18dd42004-05-12 19:18:15 +0000947 if( pPage->hasData ){
drh271efa52004-05-30 19:19:05 +0000948 n += getVarint32(&pCell[n], &nPayload);
drh8b18dd42004-05-12 19:18:15 +0000949 }else{
drh271efa52004-05-30 19:19:05 +0000950 nPayload = 0;
drh3aac2dd2004-04-26 14:10:20 +0000951 }
drh271efa52004-05-30 19:19:05 +0000952 pInfo->nData = nPayload;
drh504b6982006-01-22 21:52:56 +0000953 if( pPage->intKey ){
954 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
955 }else{
956 u32 x;
957 n += getVarint32(&pCell[n], &x);
958 pInfo->nKey = x;
959 nPayload += x;
drh6f11bef2004-05-13 01:12:56 +0000960 }
drh72365832007-03-06 15:53:44 +0000961 pInfo->nPayload = nPayload;
drh504b6982006-01-22 21:52:56 +0000962 pInfo->nHeader = n;
drh271efa52004-05-30 19:19:05 +0000963 if( nPayload<=pPage->maxLocal ){
964 /* This is the (easy) common case where the entire payload fits
965 ** on the local page. No overflow is required.
966 */
967 int nSize; /* Total size of cell content in bytes */
drh6f11bef2004-05-13 01:12:56 +0000968 pInfo->nLocal = nPayload;
969 pInfo->iOverflow = 0;
drh271efa52004-05-30 19:19:05 +0000970 nSize = nPayload + n;
971 if( nSize<4 ){
972 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000973 }
drh271efa52004-05-30 19:19:05 +0000974 pInfo->nSize = nSize;
drh6f11bef2004-05-13 01:12:56 +0000975 }else{
drh271efa52004-05-30 19:19:05 +0000976 /* If the payload will not fit completely on the local page, we have
977 ** to decide how much to store locally and how much to spill onto
978 ** overflow pages. The strategy is to minimize the amount of unused
979 ** space on overflow pages while keeping the amount of local storage
980 ** in between minLocal and maxLocal.
981 **
982 ** Warning: changing the way overflow payload is distributed in any
983 ** way will result in an incompatible file format.
984 */
985 int minLocal; /* Minimum amount of payload held locally */
986 int maxLocal; /* Maximum amount of payload held locally */
987 int surplus; /* Overflow payload available for local storage */
988
989 minLocal = pPage->minLocal;
990 maxLocal = pPage->maxLocal;
991 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000992 if( surplus <= maxLocal ){
993 pInfo->nLocal = surplus;
994 }else{
995 pInfo->nLocal = minLocal;
996 }
997 pInfo->iOverflow = pInfo->nLocal + n;
998 pInfo->nSize = pInfo->iOverflow + 4;
999 }
drh3aac2dd2004-04-26 14:10:20 +00001000}
drh43605152004-05-29 21:46:49 +00001001static void parseCell(
1002 MemPage *pPage, /* Page containing the cell */
1003 int iCell, /* The cell index. First cell is 0 */
1004 CellInfo *pInfo /* Fill in this structure */
1005){
1006 parseCellPtr(pPage, findCell(pPage, iCell), pInfo);
1007}
drh3aac2dd2004-04-26 14:10:20 +00001008
1009/*
drh43605152004-05-29 21:46:49 +00001010** Compute the total number of bytes that a Cell needs in the cell
1011** data area of the btree-page. The return number includes the cell
1012** data header and the local payload, but not any overflow page or
1013** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +00001014*/
danielk1977bc6ada42004-06-30 08:20:16 +00001015#ifndef NDEBUG
drh43605152004-05-29 21:46:49 +00001016static int cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +00001017 CellInfo info;
drh43605152004-05-29 21:46:49 +00001018 parseCell(pPage, iCell, &info);
1019 return info.nSize;
1020}
danielk1977bc6ada42004-06-30 08:20:16 +00001021#endif
drh43605152004-05-29 21:46:49 +00001022static int cellSizePtr(MemPage *pPage, u8 *pCell){
1023 CellInfo info;
1024 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00001025 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +00001026}
1027
danielk197779a40da2005-01-16 08:00:01 +00001028#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +00001029/*
danielk197726836652005-01-17 01:33:13 +00001030** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +00001031** to an overflow page, insert an entry into the pointer-map
1032** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +00001033*/
danielk197726836652005-01-17 01:33:13 +00001034static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
danielk197779a40da2005-01-16 08:00:01 +00001035 if( pCell ){
1036 CellInfo info;
1037 parseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00001038 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk197779a40da2005-01-16 08:00:01 +00001039 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
1040 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
1041 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
1042 }
danielk1977ac11ee62005-01-15 12:45:51 +00001043 }
danielk197779a40da2005-01-16 08:00:01 +00001044 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +00001045}
danielk197726836652005-01-17 01:33:13 +00001046/*
1047** If the cell with index iCell on page pPage contains a pointer
1048** to an overflow page, insert an entry into the pointer-map
1049** for the overflow page.
1050*/
1051static int ptrmapPutOvfl(MemPage *pPage, int iCell){
1052 u8 *pCell;
1053 pCell = findOverflowCell(pPage, iCell);
1054 return ptrmapPutOvflPtr(pPage, pCell);
1055}
danielk197779a40da2005-01-16 08:00:01 +00001056#endif
1057
danielk1977ac11ee62005-01-15 12:45:51 +00001058
danielk1977aef0bf62005-12-30 16:28:01 +00001059/* A bunch of assert() statements to check the transaction state variables
1060** of handle p (type Btree*) are internally consistent.
1061*/
1062#define btreeIntegrity(p) \
1063 assert( p->inTrans!=TRANS_NONE || p->pBt->nTransaction<p->pBt->nRef ); \
1064 assert( p->pBt->nTransaction<=p->pBt->nRef ); \
1065 assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
1066 assert( p->pBt->inTransaction>=p->inTrans );
1067
drhda200cc2004-05-09 11:51:38 +00001068/*
drh72f82862001-05-24 21:06:34 +00001069** Defragment the page given. All Cells are moved to the
drh3a4a2d42005-11-24 14:24:28 +00001070** end of the page and all free space is collected into one
1071** big FreeBlk that occurs in between the header and cell
drh31beae92005-11-24 14:34:36 +00001072** pointer array and the cell content area.
drh365d68f2001-05-11 11:02:46 +00001073*/
drh2e38c322004-09-03 18:38:44 +00001074static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +00001075 int i; /* Loop counter */
1076 int pc; /* Address of a i-th cell */
1077 int addr; /* Offset of first byte after cell pointer array */
1078 int hdr; /* Offset to the page header */
1079 int size; /* Size of a cell */
1080 int usableSize; /* Number of usable bytes on a page */
1081 int cellOffset; /* Offset to the cell pointer array */
1082 int brk; /* Offset to the cell content area */
1083 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +00001084 unsigned char *data; /* The page data */
1085 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +00001086
danielk19773b8a05f2007-03-19 17:44:26 +00001087 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001088 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +00001089 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +00001090 assert( pPage->nOverflow==0 );
drh2e38c322004-09-03 18:38:44 +00001091 temp = sqliteMalloc( pPage->pBt->pageSize );
1092 if( temp==0 ) return SQLITE_NOMEM;
drh43605152004-05-29 21:46:49 +00001093 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +00001094 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00001095 cellOffset = pPage->cellOffset;
1096 nCell = pPage->nCell;
1097 assert( nCell==get2byte(&data[hdr+3]) );
1098 usableSize = pPage->pBt->usableSize;
1099 brk = get2byte(&data[hdr+5]);
1100 memcpy(&temp[brk], &data[brk], usableSize - brk);
1101 brk = usableSize;
1102 for(i=0; i<nCell; i++){
1103 u8 *pAddr; /* The i-th cell pointer */
1104 pAddr = &data[cellOffset + i*2];
1105 pc = get2byte(pAddr);
1106 assert( pc<pPage->pBt->usableSize );
1107 size = cellSizePtr(pPage, &temp[pc]);
1108 brk -= size;
1109 memcpy(&data[brk], &temp[pc], size);
1110 put2byte(pAddr, brk);
drh2af926b2001-05-15 00:39:25 +00001111 }
drh43605152004-05-29 21:46:49 +00001112 assert( brk>=cellOffset+2*nCell );
1113 put2byte(&data[hdr+5], brk);
1114 data[hdr+1] = 0;
1115 data[hdr+2] = 0;
1116 data[hdr+7] = 0;
1117 addr = cellOffset+2*nCell;
1118 memset(&data[addr], 0, brk-addr);
drh2e38c322004-09-03 18:38:44 +00001119 sqliteFree(temp);
1120 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +00001121}
1122
drha059ad02001-04-17 20:09:11 +00001123/*
drh43605152004-05-29 21:46:49 +00001124** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +00001125**
drh9e572e62004-04-23 23:43:10 +00001126** Return the index into pPage->aData[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +00001127** the new allocation. Or return 0 if there is not enough free
1128** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +00001129**
drh72f82862001-05-24 21:06:34 +00001130** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +00001131** nBytes of contiguous free space, then this routine automatically
1132** calls defragementPage() to consolidate all free space before
1133** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +00001134*/
drh9e572e62004-04-23 23:43:10 +00001135static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +00001136 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +00001137 int size;
drh24cd67e2004-05-10 16:18:47 +00001138 int nFrag;
drh43605152004-05-29 21:46:49 +00001139 int top;
1140 int nCell;
1141 int cellOffset;
drh9e572e62004-04-23 23:43:10 +00001142 unsigned char *data;
drh43605152004-05-29 21:46:49 +00001143
drh9e572e62004-04-23 23:43:10 +00001144 data = pPage->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001145 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001146 assert( pPage->pBt );
1147 if( nByte<4 ) nByte = 4;
drh43605152004-05-29 21:46:49 +00001148 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
1149 pPage->nFree -= nByte;
drh9e572e62004-04-23 23:43:10 +00001150 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00001151
1152 nFrag = data[hdr+7];
1153 if( nFrag<60 ){
1154 /* Search the freelist looking for a slot big enough to satisfy the
1155 ** space request. */
1156 addr = hdr+1;
1157 while( (pc = get2byte(&data[addr]))>0 ){
1158 size = get2byte(&data[pc+2]);
1159 if( size>=nByte ){
1160 if( size<nByte+4 ){
1161 memcpy(&data[addr], &data[pc], 2);
1162 data[hdr+7] = nFrag + size - nByte;
1163 return pc;
1164 }else{
1165 put2byte(&data[pc+2], size-nByte);
1166 return pc + size - nByte;
1167 }
1168 }
1169 addr = pc;
drh9e572e62004-04-23 23:43:10 +00001170 }
1171 }
drh43605152004-05-29 21:46:49 +00001172
1173 /* Allocate memory from the gap in between the cell pointer array
1174 ** and the cell content area.
1175 */
1176 top = get2byte(&data[hdr+5]);
1177 nCell = get2byte(&data[hdr+3]);
1178 cellOffset = pPage->cellOffset;
1179 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
drh2e38c322004-09-03 18:38:44 +00001180 if( defragmentPage(pPage) ) return 0;
drh43605152004-05-29 21:46:49 +00001181 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +00001182 }
drh43605152004-05-29 21:46:49 +00001183 top -= nByte;
1184 assert( cellOffset + 2*nCell <= top );
1185 put2byte(&data[hdr+5], top);
1186 return top;
drh7e3b0a02001-04-28 16:52:40 +00001187}
1188
1189/*
drh9e572e62004-04-23 23:43:10 +00001190** Return a section of the pPage->aData to the freelist.
1191** The first byte of the new free block is pPage->aDisk[start]
1192** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +00001193**
1194** Most of the effort here is involved in coalesing adjacent
1195** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +00001196*/
drh9e572e62004-04-23 23:43:10 +00001197static void freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +00001198 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +00001199 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +00001200
drh9e572e62004-04-23 23:43:10 +00001201 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001202 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +00001203 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +00001204 assert( (start + size)<=pPage->pBt->usableSize );
drh9e572e62004-04-23 23:43:10 +00001205 if( size<4 ) size = 4;
1206
drhfcce93f2006-02-22 03:08:32 +00001207#ifdef SQLITE_SECURE_DELETE
1208 /* Overwrite deleted information with zeros when the SECURE_DELETE
1209 ** option is enabled at compile-time */
1210 memset(&data[start], 0, size);
1211#endif
1212
drh9e572e62004-04-23 23:43:10 +00001213 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +00001214 hdr = pPage->hdrOffset;
1215 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +00001216 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +00001217 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +00001218 assert( pbegin>addr );
1219 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +00001220 }
drhb6f41482004-05-14 01:58:11 +00001221 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +00001222 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +00001223 put2byte(&data[addr], start);
1224 put2byte(&data[start], pbegin);
1225 put2byte(&data[start+2], size);
drh2af926b2001-05-15 00:39:25 +00001226 pPage->nFree += size;
drh9e572e62004-04-23 23:43:10 +00001227
1228 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +00001229 addr = pPage->hdrOffset + 1;
1230 while( (pbegin = get2byte(&data[addr]))>0 ){
drh9e572e62004-04-23 23:43:10 +00001231 int pnext, psize;
drh3aac2dd2004-04-26 14:10:20 +00001232 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +00001233 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +00001234 pnext = get2byte(&data[pbegin]);
1235 psize = get2byte(&data[pbegin+2]);
1236 if( pbegin + psize + 3 >= pnext && pnext>0 ){
1237 int frag = pnext - (pbegin+psize);
drh43605152004-05-29 21:46:49 +00001238 assert( frag<=data[pPage->hdrOffset+7] );
1239 data[pPage->hdrOffset+7] -= frag;
drh9e572e62004-04-23 23:43:10 +00001240 put2byte(&data[pbegin], get2byte(&data[pnext]));
1241 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
1242 }else{
drh3aac2dd2004-04-26 14:10:20 +00001243 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +00001244 }
1245 }
drh7e3b0a02001-04-28 16:52:40 +00001246
drh43605152004-05-29 21:46:49 +00001247 /* If the cell content area begins with a freeblock, remove it. */
1248 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
1249 int top;
1250 pbegin = get2byte(&data[hdr+1]);
1251 memcpy(&data[hdr+1], &data[pbegin], 2);
1252 top = get2byte(&data[hdr+5]);
1253 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
drh4b70f112004-05-02 21:12:19 +00001254 }
drh4b70f112004-05-02 21:12:19 +00001255}
1256
1257/*
drh271efa52004-05-30 19:19:05 +00001258** Decode the flags byte (the first byte of the header) for a page
1259** and initialize fields of the MemPage structure accordingly.
1260*/
1261static void decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:01 +00001262 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:05 +00001263
1264 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
1265 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
1266 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
1267 pPage->leaf = (flagByte & PTF_LEAF)!=0;
1268 pPage->childPtrSize = 4*(pPage->leaf==0);
1269 pBt = pPage->pBt;
1270 if( flagByte & PTF_LEAFDATA ){
1271 pPage->leafData = 1;
1272 pPage->maxLocal = pBt->maxLeaf;
1273 pPage->minLocal = pBt->minLeaf;
1274 }else{
1275 pPage->leafData = 0;
1276 pPage->maxLocal = pBt->maxLocal;
1277 pPage->minLocal = pBt->minLocal;
1278 }
1279 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
1280}
1281
1282/*
drh7e3b0a02001-04-28 16:52:40 +00001283** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +00001284**
drhbd03cae2001-06-02 02:40:57 +00001285** The pParent parameter must be a pointer to the MemPage which
drh9e572e62004-04-23 23:43:10 +00001286** is the parent of the page being initialized. The root of a
1287** BTree has no parent and so for that page, pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +00001288**
drh72f82862001-05-24 21:06:34 +00001289** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +00001290** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +00001291** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
1292** guarantee that the page is well-formed. It only shows that
1293** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +00001294*/
drh9e572e62004-04-23 23:43:10 +00001295static int initPage(
drh3aac2dd2004-04-26 14:10:20 +00001296 MemPage *pPage, /* The page to be initialized */
drh9e572e62004-04-23 23:43:10 +00001297 MemPage *pParent /* The parent. Might be NULL */
1298){
drh271efa52004-05-30 19:19:05 +00001299 int pc; /* Address of a freeblock within pPage->aData[] */
drh271efa52004-05-30 19:19:05 +00001300 int hdr; /* Offset to beginning of page header */
1301 u8 *data; /* Equal to pPage->aData */
danielk1977aef0bf62005-12-30 16:28:01 +00001302 BtShared *pBt; /* The main btree structure */
drh271efa52004-05-30 19:19:05 +00001303 int usableSize; /* Amount of usable space on each page */
1304 int cellOffset; /* Offset from start of page to first cell pointer */
1305 int nFree; /* Number of unused bytes on the page */
1306 int top; /* First byte of the cell content area */
drh2af926b2001-05-15 00:39:25 +00001307
drh2e38c322004-09-03 18:38:44 +00001308 pBt = pPage->pBt;
1309 assert( pBt!=0 );
1310 assert( pParent==0 || pParent->pBt==pBt );
danielk19773b8a05f2007-03-19 17:44:26 +00001311 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drh07d183d2005-05-01 22:52:42 +00001312 assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] );
drhee696e22004-08-30 16:52:17 +00001313 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
1314 /* The parent page should never change unless the file is corrupt */
drh49285702005-09-17 15:20:26 +00001315 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001316 }
drh10617cd2004-05-14 15:27:27 +00001317 if( pPage->isInit ) return SQLITE_OK;
drhda200cc2004-05-09 11:51:38 +00001318 if( pPage->pParent==0 && pParent!=0 ){
1319 pPage->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +00001320 sqlite3PagerRef(pParent->pDbPage);
drh5e2f8b92001-05-28 00:41:15 +00001321 }
drhde647132004-05-07 17:57:49 +00001322 hdr = pPage->hdrOffset;
drha34b6762004-05-07 13:30:42 +00001323 data = pPage->aData;
drh271efa52004-05-30 19:19:05 +00001324 decodeFlags(pPage, data[hdr]);
drh43605152004-05-29 21:46:49 +00001325 pPage->nOverflow = 0;
drhc8629a12004-05-08 20:07:40 +00001326 pPage->idxShift = 0;
drh2e38c322004-09-03 18:38:44 +00001327 usableSize = pBt->usableSize;
drh43605152004-05-29 21:46:49 +00001328 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1329 top = get2byte(&data[hdr+5]);
1330 pPage->nCell = get2byte(&data[hdr+3]);
drh2e38c322004-09-03 18:38:44 +00001331 if( pPage->nCell>MX_CELL(pBt) ){
drhee696e22004-08-30 16:52:17 +00001332 /* To many cells for a single page. The page must be corrupt */
drh49285702005-09-17 15:20:26 +00001333 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001334 }
1335 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
1336 /* All pages must have at least one cell, except for root pages */
drh49285702005-09-17 15:20:26 +00001337 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001338 }
drh9e572e62004-04-23 23:43:10 +00001339
1340 /* Compute the total free space on the page */
drh9e572e62004-04-23 23:43:10 +00001341 pc = get2byte(&data[hdr+1]);
drh43605152004-05-29 21:46:49 +00001342 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
drh9e572e62004-04-23 23:43:10 +00001343 while( pc>0 ){
1344 int next, size;
drhee696e22004-08-30 16:52:17 +00001345 if( pc>usableSize-4 ){
1346 /* Free block is off the page */
drh49285702005-09-17 15:20:26 +00001347 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001348 }
drh9e572e62004-04-23 23:43:10 +00001349 next = get2byte(&data[pc]);
1350 size = get2byte(&data[pc+2]);
drhee696e22004-08-30 16:52:17 +00001351 if( next>0 && next<=pc+size+3 ){
1352 /* Free blocks must be in accending order */
drh49285702005-09-17 15:20:26 +00001353 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001354 }
drh3add3672004-05-15 00:29:24 +00001355 nFree += size;
drh9e572e62004-04-23 23:43:10 +00001356 pc = next;
1357 }
drh3add3672004-05-15 00:29:24 +00001358 pPage->nFree = nFree;
drhee696e22004-08-30 16:52:17 +00001359 if( nFree>=usableSize ){
1360 /* Free space cannot exceed total page size */
drh49285702005-09-17 15:20:26 +00001361 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001362 }
drh9e572e62004-04-23 23:43:10 +00001363
drhde647132004-05-07 17:57:49 +00001364 pPage->isInit = 1;
drh9e572e62004-04-23 23:43:10 +00001365 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001366}
1367
1368/*
drh8b2f49b2001-06-08 00:21:52 +00001369** Set up a raw page so that it looks like a database page holding
1370** no entries.
drhbd03cae2001-06-02 02:40:57 +00001371*/
drh9e572e62004-04-23 23:43:10 +00001372static void zeroPage(MemPage *pPage, int flags){
1373 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:01 +00001374 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00001375 int hdr = pPage->hdrOffset;
drh9e572e62004-04-23 23:43:10 +00001376 int first;
1377
danielk19773b8a05f2007-03-19 17:44:26 +00001378 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
drh07d183d2005-05-01 22:52:42 +00001379 assert( &data[pBt->pageSize] == (unsigned char*)pPage );
danielk19773b8a05f2007-03-19 17:44:26 +00001380 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drhb6f41482004-05-14 01:58:11 +00001381 memset(&data[hdr], 0, pBt->usableSize - hdr);
drh9e572e62004-04-23 23:43:10 +00001382 data[hdr] = flags;
drh43605152004-05-29 21:46:49 +00001383 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
1384 memset(&data[hdr+1], 0, 4);
1385 data[hdr+7] = 0;
1386 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001387 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001388 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001389 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001390 pPage->cellOffset = first;
1391 pPage->nOverflow = 0;
drhda200cc2004-05-09 11:51:38 +00001392 pPage->idxShift = 0;
drh43605152004-05-29 21:46:49 +00001393 pPage->nCell = 0;
drhda200cc2004-05-09 11:51:38 +00001394 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:57 +00001395}
1396
1397/*
drh3aac2dd2004-04-26 14:10:20 +00001398** Get a page from the pager. Initialize the MemPage.pBt and
1399** MemPage.aData elements if needed.
drh538f5702007-04-13 02:14:30 +00001400**
1401** If the noContent flag is set, it means that we do not care about
1402** the content of the page at this time. So do not go to the disk
1403** to fetch the content. Just fill in the content with zeros for now.
1404** If in the future we call sqlite3PagerWrite() on this page, that
1405** means we have started to be concerned about content and the disk
1406** read should occur at that point.
drh3aac2dd2004-04-26 14:10:20 +00001407*/
drh538f5702007-04-13 02:14:30 +00001408static int getPage(BtShared *pBt, Pgno pgno, MemPage **ppPage, int noContent){
drh3aac2dd2004-04-26 14:10:20 +00001409 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001410 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00001411 DbPage *pDbPage;
1412
drh538f5702007-04-13 02:14:30 +00001413 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
drh3aac2dd2004-04-26 14:10:20 +00001414 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001415 pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1416 pPage->aData = sqlite3PagerGetData(pDbPage);
1417 pPage->pDbPage = pDbPage;
drh3aac2dd2004-04-26 14:10:20 +00001418 pPage->pBt = pBt;
1419 pPage->pgno = pgno;
drhde647132004-05-07 17:57:49 +00001420 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
drh3aac2dd2004-04-26 14:10:20 +00001421 *ppPage = pPage;
1422 return SQLITE_OK;
1423}
1424
1425/*
drhde647132004-05-07 17:57:49 +00001426** Get a page from the pager and initialize it. This routine
1427** is just a convenience wrapper around separate calls to
1428** getPage() and initPage().
1429*/
1430static int getAndInitPage(
danielk1977aef0bf62005-12-30 16:28:01 +00001431 BtShared *pBt, /* The database file */
drhde647132004-05-07 17:57:49 +00001432 Pgno pgno, /* Number of the page to get */
1433 MemPage **ppPage, /* Write the page pointer here */
1434 MemPage *pParent /* Parent of the page */
1435){
1436 int rc;
drhee696e22004-08-30 16:52:17 +00001437 if( pgno==0 ){
drh49285702005-09-17 15:20:26 +00001438 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001439 }
drh0787db62007-03-04 13:15:27 +00001440 rc = getPage(pBt, pgno, ppPage, 0);
drh10617cd2004-05-14 15:27:27 +00001441 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
drhde647132004-05-07 17:57:49 +00001442 rc = initPage(*ppPage, pParent);
1443 }
1444 return rc;
1445}
1446
1447/*
drh3aac2dd2004-04-26 14:10:20 +00001448** Release a MemPage. This should be called once for each prior
1449** call to getPage.
1450*/
drh4b70f112004-05-02 21:12:19 +00001451static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001452 if( pPage ){
1453 assert( pPage->aData );
1454 assert( pPage->pBt );
drh07d183d2005-05-01 22:52:42 +00001455 assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage );
danielk19773b8a05f2007-03-19 17:44:26 +00001456 sqlite3PagerUnref(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00001457 }
1458}
1459
1460/*
drh72f82862001-05-24 21:06:34 +00001461** This routine is called when the reference count for a page
1462** reaches zero. We need to unref the pParent pointer when that
1463** happens.
1464*/
danielk19773b8a05f2007-03-19 17:44:26 +00001465static void pageDestructor(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001466 MemPage *pPage;
1467 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001468 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drh72f82862001-05-24 21:06:34 +00001469 if( pPage->pParent ){
1470 MemPage *pParent = pPage->pParent;
1471 pPage->pParent = 0;
drha34b6762004-05-07 13:30:42 +00001472 releasePage(pParent);
drh72f82862001-05-24 21:06:34 +00001473 }
drh3aac2dd2004-04-26 14:10:20 +00001474 pPage->isInit = 0;
drh72f82862001-05-24 21:06:34 +00001475}
1476
1477/*
drha6abd042004-06-09 17:37:22 +00001478** During a rollback, when the pager reloads information into the cache
1479** so that the cache is restored to its original state at the start of
1480** the transaction, for each page restored this routine is called.
1481**
1482** This routine needs to reset the extra data section at the end of the
1483** page to agree with the restored data.
1484*/
danielk19773b8a05f2007-03-19 17:44:26 +00001485static void pageReinit(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001486 MemPage *pPage;
1487 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001488 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drha6abd042004-06-09 17:37:22 +00001489 if( pPage->isInit ){
1490 pPage->isInit = 0;
1491 initPage(pPage, pPage->pParent);
1492 }
1493}
1494
1495/*
drhad3e0102004-09-03 23:32:18 +00001496** Open a database file.
1497**
drh382c0242001-10-06 16:33:02 +00001498** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001499** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001500** database file will be deleted when sqlite3BtreeClose() is called.
drha059ad02001-04-17 20:09:11 +00001501*/
drh23e11ca2004-05-04 17:27:28 +00001502int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001503 const char *zFilename, /* Name of the file containing the BTree database */
danielk1977aef0bf62005-12-30 16:28:01 +00001504 sqlite3 *pSqlite, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:20 +00001505 Btree **ppBtree, /* Pointer to new Btree object written here */
drh90f5ecb2004-07-22 01:19:35 +00001506 int flags /* Options */
drh6019e162001-07-02 17:51:45 +00001507){
danielk1977aef0bf62005-12-30 16:28:01 +00001508 BtShared *pBt; /* Shared part of btree structure */
1509 Btree *p; /* Handle to return */
danielk1977dddbcdc2007-04-26 14:42:34 +00001510 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00001511 int nReserve;
1512 unsigned char zDbHeader[100];
drh6f7adc82006-01-11 21:41:20 +00001513#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1514 const ThreadData *pTsdro;
danielk1977da184232006-01-05 11:34:32 +00001515#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001516
1517 /* Set the variable isMemdb to true for an in-memory database, or
1518 ** false for a file-based database. This symbol is only required if
1519 ** either of the shared-data or autovacuum features are compiled
1520 ** into the library.
1521 */
1522#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1523 #ifdef SQLITE_OMIT_MEMORYDB
drh980b1a72006-08-16 16:42:48 +00001524 const int isMemdb = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001525 #else
drh980b1a72006-08-16 16:42:48 +00001526 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
danielk1977aef0bf62005-12-30 16:28:01 +00001527 #endif
1528#endif
1529
1530 p = sqliteMalloc(sizeof(Btree));
1531 if( !p ){
1532 return SQLITE_NOMEM;
1533 }
1534 p->inTrans = TRANS_NONE;
1535 p->pSqlite = pSqlite;
1536
1537 /* Try to find an existing Btree structure opened on zFilename. */
drh198bf392006-01-06 21:52:49 +00001538#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drh6f7adc82006-01-11 21:41:20 +00001539 pTsdro = sqlite3ThreadDataReadOnly();
1540 if( pTsdro->useSharedData && zFilename && !isMemdb ){
drh66560ad2006-01-06 14:32:19 +00001541 char *zFullPathname = sqlite3OsFullPathname(zFilename);
danielk1977aef0bf62005-12-30 16:28:01 +00001542 if( !zFullPathname ){
1543 sqliteFree(p);
1544 return SQLITE_NOMEM;
1545 }
drh6f7adc82006-01-11 21:41:20 +00001546 for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){
danielk1977b82e7ed2006-01-11 14:09:31 +00001547 assert( pBt->nRef>0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001548 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){
danielk1977aef0bf62005-12-30 16:28:01 +00001549 p->pBt = pBt;
1550 *ppBtree = p;
1551 pBt->nRef++;
1552 sqliteFree(zFullPathname);
1553 return SQLITE_OK;
1554 }
1555 }
1556 sqliteFree(zFullPathname);
1557 }
1558#endif
drha059ad02001-04-17 20:09:11 +00001559
drhd62d3d02003-01-24 12:14:20 +00001560 /*
1561 ** The following asserts make sure that structures used by the btree are
1562 ** the right size. This is to guard against size changes that result
1563 ** when compiling on a different architecture.
1564 */
drh9b8f4472006-04-04 01:54:55 +00001565 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1566 assert( sizeof(u64)==8 || sizeof(u64)==4 );
drhd62d3d02003-01-24 12:14:20 +00001567 assert( sizeof(u32)==4 );
1568 assert( sizeof(u16)==2 );
1569 assert( sizeof(Pgno)==4 );
drhd62d3d02003-01-24 12:14:20 +00001570
drha059ad02001-04-17 20:09:11 +00001571 pBt = sqliteMalloc( sizeof(*pBt) );
1572 if( pBt==0 ){
danielk1977dddbcdc2007-04-26 14:42:34 +00001573 rc = SQLITE_NOMEM;
1574 goto btree_open_out;
drha059ad02001-04-17 20:09:11 +00001575 }
danielk19773b8a05f2007-03-19 17:44:26 +00001576 rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags);
drh551b7732006-11-06 21:20:25 +00001577 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00001578 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
drh551b7732006-11-06 21:20:25 +00001579 }
drha059ad02001-04-17 20:09:11 +00001580 if( rc!=SQLITE_OK ){
danielk1977dddbcdc2007-04-26 14:42:34 +00001581 goto btree_open_out;
drha059ad02001-04-17 20:09:11 +00001582 }
danielk1977aef0bf62005-12-30 16:28:01 +00001583 p->pBt = pBt;
1584
danielk19773b8a05f2007-03-19 17:44:26 +00001585 sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
1586 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
drha059ad02001-04-17 20:09:11 +00001587 pBt->pCursor = 0;
drha34b6762004-05-07 13:30:42 +00001588 pBt->pPage1 = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00001589 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
drh90f5ecb2004-07-22 01:19:35 +00001590 pBt->pageSize = get2byte(&zDbHeader[16]);
drh07d183d2005-05-01 22:52:42 +00001591 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1592 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
drh90f5ecb2004-07-22 01:19:35 +00001593 pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
1594 pBt->maxEmbedFrac = 64; /* 25% */
1595 pBt->minEmbedFrac = 32; /* 12.5% */
1596 pBt->minLeafFrac = 32; /* 12.5% */
drheee46cf2004-11-06 00:02:48 +00001597#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197703aded42004-11-22 05:26:27 +00001598 /* If the magic name ":memory:" will create an in-memory database, then
danielk1977dddbcdc2007-04-26 14:42:34 +00001599 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1600 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1601 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1602 ** regular file-name. In this case the auto-vacuum applies as per normal.
danielk197703aded42004-11-22 05:26:27 +00001603 */
danielk1977aef0bf62005-12-30 16:28:01 +00001604 if( zFilename && !isMemdb ){
danielk1977dddbcdc2007-04-26 14:42:34 +00001605 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1606 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
danielk1977951af802004-11-05 15:45:09 +00001607 }
drheee46cf2004-11-06 00:02:48 +00001608#endif
drh90f5ecb2004-07-22 01:19:35 +00001609 nReserve = 0;
1610 }else{
1611 nReserve = zDbHeader[20];
1612 pBt->maxEmbedFrac = zDbHeader[21];
1613 pBt->minEmbedFrac = zDbHeader[22];
1614 pBt->minLeafFrac = zDbHeader[23];
1615 pBt->pageSizeFixed = 1;
danielk1977951af802004-11-05 15:45:09 +00001616#ifndef SQLITE_OMIT_AUTOVACUUM
1617 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1618#endif
drh90f5ecb2004-07-22 01:19:35 +00001619 }
1620 pBt->usableSize = pBt->pageSize - nReserve;
drh07d183d2005-05-01 22:52:42 +00001621 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
danielk19773b8a05f2007-03-19 17:44:26 +00001622 sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize);
danielk1977aef0bf62005-12-30 16:28:01 +00001623
drhcfed7bc2006-03-13 14:28:05 +00001624#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
danielk197754f01982006-01-18 15:25:17 +00001625 /* Add the new btree to the linked list starting at ThreadData.pBtree.
1626 ** There is no chance that a malloc() may fail inside of the
1627 ** sqlite3ThreadData() call, as the ThreadData structure must have already
1628 ** been allocated for pTsdro->useSharedData to be non-zero.
1629 */
drh6f7adc82006-01-11 21:41:20 +00001630 if( pTsdro->useSharedData && zFilename && !isMemdb ){
1631 pBt->pNext = pTsdro->pBtree;
1632 sqlite3ThreadData()->pBtree = pBt;
danielk1977aef0bf62005-12-30 16:28:01 +00001633 }
danielk1977aef0bf62005-12-30 16:28:01 +00001634#endif
danielk1977da184232006-01-05 11:34:32 +00001635 pBt->nRef = 1;
danielk1977aef0bf62005-12-30 16:28:01 +00001636 *ppBtree = p;
danielk1977dddbcdc2007-04-26 14:42:34 +00001637
1638btree_open_out:
1639 if( rc!=SQLITE_OK ){
1640 if( pBt && pBt->pPager ){
1641 sqlite3PagerClose(pBt->pPager);
1642 }
1643 sqliteFree(pBt);
1644 sqliteFree(p);
1645 *ppBtree = 0;
1646 }
1647 return rc;
drha059ad02001-04-17 20:09:11 +00001648}
1649
1650/*
1651** Close an open database and invalidate all cursors.
1652*/
danielk1977aef0bf62005-12-30 16:28:01 +00001653int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00001654 BtShared *pBt = p->pBt;
1655 BtCursor *pCur;
1656
danielk1977da184232006-01-05 11:34:32 +00001657#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00001658 ThreadData *pTsd;
danielk1977da184232006-01-05 11:34:32 +00001659#endif
1660
danielk1977aef0bf62005-12-30 16:28:01 +00001661 /* Close all cursors opened via this handle. */
1662 pCur = pBt->pCursor;
1663 while( pCur ){
1664 BtCursor *pTmp = pCur;
1665 pCur = pCur->pNext;
1666 if( pTmp->pBtree==p ){
1667 sqlite3BtreeCloseCursor(pTmp);
1668 }
drha059ad02001-04-17 20:09:11 +00001669 }
danielk1977aef0bf62005-12-30 16:28:01 +00001670
danielk19778d34dfd2006-01-24 16:37:57 +00001671 /* Rollback any active transaction and free the handle structure.
1672 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1673 ** this handle.
1674 */
danielk1977b597f742006-01-15 11:39:18 +00001675 sqlite3BtreeRollback(p);
danielk1977aef0bf62005-12-30 16:28:01 +00001676 sqliteFree(p);
1677
1678#ifndef SQLITE_OMIT_SHARED_CACHE
1679 /* If there are still other outstanding references to the shared-btree
1680 ** structure, return now. The remainder of this procedure cleans
1681 ** up the shared-btree.
1682 */
1683 assert( pBt->nRef>0 );
1684 pBt->nRef--;
1685 if( pBt->nRef ){
1686 return SQLITE_OK;
1687 }
1688
danielk197754f01982006-01-18 15:25:17 +00001689 /* Remove the shared-btree from the thread wide list. Call
1690 ** ThreadDataReadOnly() and then cast away the const property of the
1691 ** pointer to avoid allocating thread data if it is not really required.
1692 */
1693 pTsd = (ThreadData *)sqlite3ThreadDataReadOnly();
danielk1977aef0bf62005-12-30 16:28:01 +00001694 if( pTsd->pBtree==pBt ){
danielk197754f01982006-01-18 15:25:17 +00001695 assert( pTsd==sqlite3ThreadData() );
danielk1977aef0bf62005-12-30 16:28:01 +00001696 pTsd->pBtree = pBt->pNext;
1697 }else{
1698 BtShared *pPrev;
drh74161702006-02-24 02:53:49 +00001699 for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){}
danielk1977aef0bf62005-12-30 16:28:01 +00001700 if( pPrev ){
danielk197754f01982006-01-18 15:25:17 +00001701 assert( pTsd==sqlite3ThreadData() );
danielk1977aef0bf62005-12-30 16:28:01 +00001702 pPrev->pNext = pBt->pNext;
1703 }
1704 }
1705#endif
1706
1707 /* Close the pager and free the shared-btree structure */
1708 assert( !pBt->pCursor );
danielk19773b8a05f2007-03-19 17:44:26 +00001709 sqlite3PagerClose(pBt->pPager);
danielk1977da184232006-01-05 11:34:32 +00001710 if( pBt->xFreeSchema && pBt->pSchema ){
1711 pBt->xFreeSchema(pBt->pSchema);
1712 }
danielk1977de0fe3e2006-01-06 06:33:12 +00001713 sqliteFree(pBt->pSchema);
drha059ad02001-04-17 20:09:11 +00001714 sqliteFree(pBt);
1715 return SQLITE_OK;
1716}
1717
1718/*
drh90f5ecb2004-07-22 01:19:35 +00001719** Change the busy handler callback function.
1720*/
danielk1977aef0bf62005-12-30 16:28:01 +00001721int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){
1722 BtShared *pBt = p->pBt;
drhb8ef32c2005-03-14 02:01:49 +00001723 pBt->pBusyHandler = pHandler;
danielk19773b8a05f2007-03-19 17:44:26 +00001724 sqlite3PagerSetBusyhandler(pBt->pPager, pHandler);
drh90f5ecb2004-07-22 01:19:35 +00001725 return SQLITE_OK;
1726}
1727
1728/*
drhda47d772002-12-02 04:25:19 +00001729** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001730**
1731** The maximum number of cache pages is set to the absolute
1732** value of mxPage. If mxPage is negative, the pager will
1733** operate asynchronously - it will not stop to do fsync()s
1734** to insure data is written to the disk surface before
1735** continuing. Transactions still work if synchronous is off,
1736** and the database cannot be corrupted if this program
1737** crashes. But if the operating system crashes or there is
1738** an abrupt power failure when synchronous is off, the database
1739** could be left in an inconsistent and unrecoverable state.
1740** Synchronous is on by default so database corruption is not
1741** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001742*/
danielk1977aef0bf62005-12-30 16:28:01 +00001743int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1744 BtShared *pBt = p->pBt;
danielk19773b8a05f2007-03-19 17:44:26 +00001745 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001746 return SQLITE_OK;
1747}
1748
1749/*
drh973b6e32003-02-12 14:09:42 +00001750** Change the way data is synced to disk in order to increase or decrease
1751** how well the database resists damage due to OS crashes and power
1752** failures. Level 1 is the same as asynchronous (no syncs() occur and
1753** there is a high probability of damage) Level 2 is the default. There
1754** is a very low but non-zero probability of damage. Level 3 reduces the
1755** probability of damage to near zero but with a write performance reduction.
1756*/
danielk197793758c82005-01-21 08:13:14 +00001757#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001758int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
danielk1977aef0bf62005-12-30 16:28:01 +00001759 BtShared *pBt = p->pBt;
danielk19773b8a05f2007-03-19 17:44:26 +00001760 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
drh973b6e32003-02-12 14:09:42 +00001761 return SQLITE_OK;
1762}
danielk197793758c82005-01-21 08:13:14 +00001763#endif
drh973b6e32003-02-12 14:09:42 +00001764
drh2c8997b2005-08-27 16:36:48 +00001765/*
1766** Return TRUE if the given btree is set to safety level 1. In other
1767** words, return TRUE if no sync() occurs on the disk files.
1768*/
danielk1977aef0bf62005-12-30 16:28:01 +00001769int sqlite3BtreeSyncDisabled(Btree *p){
1770 BtShared *pBt = p->pBt;
drh2c8997b2005-08-27 16:36:48 +00001771 assert( pBt && pBt->pPager );
danielk19773b8a05f2007-03-19 17:44:26 +00001772 return sqlite3PagerNosync(pBt->pPager);
drh2c8997b2005-08-27 16:36:48 +00001773}
1774
danielk1977576ec6b2005-01-21 11:55:25 +00001775#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001776/*
drh90f5ecb2004-07-22 01:19:35 +00001777** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001778**
1779** The page size must be a power of 2 between 512 and 65536. If the page
1780** size supplied does not meet this constraint then the page size is not
1781** changed.
1782**
1783** Page sizes are constrained to be a power of two so that the region
1784** of the database file used for locking (beginning at PENDING_BYTE,
1785** the first byte past the 1GB boundary, 0x40000000) needs to occur
1786** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001787**
1788** If parameter nReserve is less than zero, then the number of reserved
1789** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001790*/
danielk1977aef0bf62005-12-30 16:28:01 +00001791int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
1792 BtShared *pBt = p->pBt;
drh90f5ecb2004-07-22 01:19:35 +00001793 if( pBt->pageSizeFixed ){
1794 return SQLITE_READONLY;
1795 }
1796 if( nReserve<0 ){
1797 nReserve = pBt->pageSize - pBt->usableSize;
1798 }
drh06f50212004-11-02 14:24:33 +00001799 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1800 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:42 +00001801 assert( (pageSize & 7)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00001802 assert( !pBt->pPage1 && !pBt->pCursor );
danielk19773b8a05f2007-03-19 17:44:26 +00001803 pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001804 }
1805 pBt->usableSize = pBt->pageSize - nReserve;
1806 return SQLITE_OK;
1807}
1808
1809/*
1810** Return the currently defined page size
1811*/
danielk1977aef0bf62005-12-30 16:28:01 +00001812int sqlite3BtreeGetPageSize(Btree *p){
1813 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001814}
danielk1977aef0bf62005-12-30 16:28:01 +00001815int sqlite3BtreeGetReserve(Btree *p){
1816 return p->pBt->pageSize - p->pBt->usableSize;
drh2011d5f2004-07-22 02:40:37 +00001817}
danielk1977576ec6b2005-01-21 11:55:25 +00001818#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001819
1820/*
danielk1977951af802004-11-05 15:45:09 +00001821** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1822** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1823** is disabled. The default value for the auto-vacuum property is
1824** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1825*/
danielk1977aef0bf62005-12-30 16:28:01 +00001826int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
danielk1977951af802004-11-05 15:45:09 +00001827#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001828 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001829#else
danielk1977dddbcdc2007-04-26 14:42:34 +00001830 BtShared *pBt = p->pBt;
1831 int av = (autoVacuum?1:0);
1832 int iv = (autoVacuum==BTREE_AUTOVACUUM_INCR?1:0);
1833 if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
danielk1977951af802004-11-05 15:45:09 +00001834 return SQLITE_READONLY;
1835 }
danielk1977dddbcdc2007-04-26 14:42:34 +00001836 pBt->autoVacuum = av;
1837 pBt->incrVacuum = iv;
danielk1977951af802004-11-05 15:45:09 +00001838 return SQLITE_OK;
1839#endif
1840}
1841
1842/*
1843** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1844** enabled 1 is returned. Otherwise 0.
1845*/
danielk1977aef0bf62005-12-30 16:28:01 +00001846int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:09 +00001847#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001848 return BTREE_AUTOVACUUM_NONE;
danielk1977951af802004-11-05 15:45:09 +00001849#else
danielk1977dddbcdc2007-04-26 14:42:34 +00001850 return (
1851 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1852 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1853 BTREE_AUTOVACUUM_INCR
1854 );
danielk1977951af802004-11-05 15:45:09 +00001855#endif
1856}
1857
1858
1859/*
drha34b6762004-05-07 13:30:42 +00001860** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001861** also acquire a readlock on that file.
1862**
1863** SQLITE_OK is returned on success. If the file is not a
1864** well-formed database file, then SQLITE_CORRUPT is returned.
1865** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
drh4f0ee682007-03-30 20:43:40 +00001866** is returned if we run out of memory.
drh306dc212001-05-21 13:45:10 +00001867*/
danielk1977aef0bf62005-12-30 16:28:01 +00001868static int lockBtree(BtShared *pBt){
drh07d183d2005-05-01 22:52:42 +00001869 int rc, pageSize;
drh3aac2dd2004-04-26 14:10:20 +00001870 MemPage *pPage1;
drha34b6762004-05-07 13:30:42 +00001871 if( pBt->pPage1 ) return SQLITE_OK;
drh0787db62007-03-04 13:15:27 +00001872 rc = getPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:10 +00001873 if( rc!=SQLITE_OK ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00001874
drh306dc212001-05-21 13:45:10 +00001875
1876 /* Do some checking to help insure the file we opened really is
1877 ** a valid database file.
1878 */
drhb6f41482004-05-14 01:58:11 +00001879 rc = SQLITE_NOTADB;
danielk19773b8a05f2007-03-19 17:44:26 +00001880 if( sqlite3PagerPagecount(pBt->pPager)>0 ){
drhb6f41482004-05-14 01:58:11 +00001881 u8 *page1 = pPage1->aData;
1882 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001883 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001884 }
drh309169a2007-04-24 17:27:51 +00001885 if( page1[18]>1 ){
1886 pBt->readOnly = 1;
1887 }
1888 if( page1[19]>1 ){
drhb6f41482004-05-14 01:58:11 +00001889 goto page1_init_failed;
1890 }
drh07d183d2005-05-01 22:52:42 +00001891 pageSize = get2byte(&page1[16]);
drh15926592007-04-06 15:02:13 +00001892 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ){
drh07d183d2005-05-01 22:52:42 +00001893 goto page1_init_failed;
1894 }
1895 assert( (pageSize & 7)==0 );
1896 pBt->pageSize = pageSize;
1897 pBt->usableSize = pageSize - page1[20];
drhb6f41482004-05-14 01:58:11 +00001898 if( pBt->usableSize<500 ){
1899 goto page1_init_failed;
1900 }
1901 pBt->maxEmbedFrac = page1[21];
1902 pBt->minEmbedFrac = page1[22];
1903 pBt->minLeafFrac = page1[23];
drh057cd3a2005-02-15 16:23:02 +00001904#ifndef SQLITE_OMIT_AUTOVACUUM
1905 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
1906#endif
drh306dc212001-05-21 13:45:10 +00001907 }
drhb6f41482004-05-14 01:58:11 +00001908
1909 /* maxLocal is the maximum amount of payload to store locally for
1910 ** a cell. Make sure it is small enough so that at least minFanout
1911 ** cells can will fit on one page. We assume a 10-byte page header.
1912 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001913 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001914 ** 4-byte child pointer
1915 ** 9-byte nKey value
1916 ** 4-byte nData value
1917 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001918 ** So a cell consists of a 2-byte poiner, a header which is as much as
1919 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1920 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001921 */
drh43605152004-05-29 21:46:49 +00001922 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1923 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1924 pBt->maxLeaf = pBt->usableSize - 35;
1925 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
drhb6f41482004-05-14 01:58:11 +00001926 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1927 goto page1_init_failed;
1928 }
drh2e38c322004-09-03 18:38:44 +00001929 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001930 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001931 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001932
drh72f82862001-05-24 21:06:34 +00001933page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001934 releasePage(pPage1);
1935 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001936 return rc;
drh306dc212001-05-21 13:45:10 +00001937}
1938
1939/*
drhb8ef32c2005-03-14 02:01:49 +00001940** This routine works like lockBtree() except that it also invokes the
1941** busy callback if there is lock contention.
1942*/
danielk1977aef0bf62005-12-30 16:28:01 +00001943static int lockBtreeWithRetry(Btree *pRef){
drhb8ef32c2005-03-14 02:01:49 +00001944 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00001945 if( pRef->inTrans==TRANS_NONE ){
1946 u8 inTransaction = pRef->pBt->inTransaction;
1947 btreeIntegrity(pRef);
1948 rc = sqlite3BtreeBeginTrans(pRef, 0);
1949 pRef->pBt->inTransaction = inTransaction;
1950 pRef->inTrans = TRANS_NONE;
1951 if( rc==SQLITE_OK ){
1952 pRef->pBt->nTransaction--;
1953 }
1954 btreeIntegrity(pRef);
drhb8ef32c2005-03-14 02:01:49 +00001955 }
1956 return rc;
1957}
1958
1959
1960/*
drhb8ca3072001-12-05 00:21:20 +00001961** If there are no outstanding cursors and we are not in the middle
1962** of a transaction but there is a read lock on the database, then
1963** this routine unrefs the first page of the database file which
1964** has the effect of releasing the read lock.
1965**
1966** If there are any outstanding cursors, this routine is a no-op.
1967**
1968** If there is a transaction in progress, this routine is a no-op.
1969*/
danielk1977aef0bf62005-12-30 16:28:01 +00001970static void unlockBtreeIfUnused(BtShared *pBt){
1971 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00001972 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
drh24c9a2e2007-01-05 02:00:47 +00001973 if( pBt->pPage1->aData==0 ){
1974 MemPage *pPage = pBt->pPage1;
1975 pPage->aData = &((u8*)pPage)[-pBt->pageSize];
1976 pPage->pBt = pBt;
1977 pPage->pgno = 1;
1978 }
1979 releasePage(pBt->pPage1);
drh51c6d962004-06-06 00:42:25 +00001980 }
drh3aac2dd2004-04-26 14:10:20 +00001981 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001982 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001983 }
1984}
1985
1986/*
drh9e572e62004-04-23 23:43:10 +00001987** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001988** file.
drh8b2f49b2001-06-08 00:21:52 +00001989*/
danielk1977aef0bf62005-12-30 16:28:01 +00001990static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:10 +00001991 MemPage *pP1;
1992 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001993 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001994 if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001995 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001996 assert( pP1!=0 );
1997 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001998 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00001999 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00002000 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
2001 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00002002 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00002003 data[18] = 1;
2004 data[19] = 1;
drhb6f41482004-05-14 01:58:11 +00002005 data[20] = pBt->pageSize - pBt->usableSize;
2006 data[21] = pBt->maxEmbedFrac;
2007 data[22] = pBt->minEmbedFrac;
2008 data[23] = pBt->minLeafFrac;
2009 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00002010 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00002011 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00002012#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00002013 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
2014 put4byte(&data[36 + 4*4], pBt->autoVacuum);
danielk1977003ba062004-11-04 02:57:33 +00002015#endif
drh8b2f49b2001-06-08 00:21:52 +00002016 return SQLITE_OK;
2017}
2018
2019/*
danielk1977ee5741e2004-05-31 10:01:34 +00002020** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00002021** is started if the second argument is nonzero, otherwise a read-
2022** transaction. If the second argument is 2 or more and exclusive
2023** transaction is started, meaning that no other process is allowed
2024** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:49 +00002025** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:42 +00002026** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00002027**
danielk1977ee5741e2004-05-31 10:01:34 +00002028** A write-transaction must be started before attempting any
2029** changes to the database. None of the following routines
2030** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00002031**
drh23e11ca2004-05-04 17:27:28 +00002032** sqlite3BtreeCreateTable()
2033** sqlite3BtreeCreateIndex()
2034** sqlite3BtreeClearTable()
2035** sqlite3BtreeDropTable()
2036** sqlite3BtreeInsert()
2037** sqlite3BtreeDelete()
2038** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00002039**
drhb8ef32c2005-03-14 02:01:49 +00002040** If an initial attempt to acquire the lock fails because of lock contention
2041** and the database was previously unlocked, then invoke the busy handler
2042** if there is one. But if there was previously a read-lock, do not
2043** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
2044** returned when there is already a read-lock in order to avoid a deadlock.
2045**
2046** Suppose there are two processes A and B. A has a read lock and B has
2047** a reserved lock. B tries to promote to exclusive but is blocked because
2048** of A's read lock. A tries to promote to reserved but is blocked by B.
2049** One or the other of the two processes must give way or there can be
2050** no progress. By returning SQLITE_BUSY and not invoking the busy callback
2051** when A already has a read lock, we encourage A to give up and let B
2052** proceed.
drha059ad02001-04-17 20:09:11 +00002053*/
danielk1977aef0bf62005-12-30 16:28:01 +00002054int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
2055 BtShared *pBt = p->pBt;
danielk1977ee5741e2004-05-31 10:01:34 +00002056 int rc = SQLITE_OK;
2057
danielk1977aef0bf62005-12-30 16:28:01 +00002058 btreeIntegrity(p);
2059
danielk1977ee5741e2004-05-31 10:01:34 +00002060 /* If the btree is already in a write-transaction, or it
2061 ** is already in a read-transaction and a read-transaction
2062 ** is requested, this is a no-op.
2063 */
danielk1977aef0bf62005-12-30 16:28:01 +00002064 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
danielk1977ee5741e2004-05-31 10:01:34 +00002065 return SQLITE_OK;
2066 }
drhb8ef32c2005-03-14 02:01:49 +00002067
2068 /* Write transactions are not possible on a read-only database */
danielk1977ee5741e2004-05-31 10:01:34 +00002069 if( pBt->readOnly && wrflag ){
2070 return SQLITE_READONLY;
2071 }
2072
danielk1977aef0bf62005-12-30 16:28:01 +00002073 /* If another database handle has already opened a write transaction
2074 ** on this shared-btree structure and a second write transaction is
2075 ** requested, return SQLITE_BUSY.
2076 */
2077 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
2078 return SQLITE_BUSY;
2079 }
2080
drhb8ef32c2005-03-14 02:01:49 +00002081 do {
2082 if( pBt->pPage1==0 ){
2083 rc = lockBtree(pBt);
drh8c42ca92001-06-22 19:15:00 +00002084 }
drh309169a2007-04-24 17:27:51 +00002085
drhb8ef32c2005-03-14 02:01:49 +00002086 if( rc==SQLITE_OK && wrflag ){
drh309169a2007-04-24 17:27:51 +00002087 if( pBt->readOnly ){
2088 rc = SQLITE_READONLY;
2089 }else{
2090 rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
2091 if( rc==SQLITE_OK ){
2092 rc = newDatabase(pBt);
2093 }
drhb8ef32c2005-03-14 02:01:49 +00002094 }
2095 }
2096
2097 if( rc==SQLITE_OK ){
drhb8ef32c2005-03-14 02:01:49 +00002098 if( wrflag ) pBt->inStmt = 0;
2099 }else{
2100 unlockBtreeIfUnused(pBt);
2101 }
danielk1977aef0bf62005-12-30 16:28:01 +00002102 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
drha4afb652005-07-09 02:16:02 +00002103 sqlite3InvokeBusyHandler(pBt->pBusyHandler) );
danielk1977aef0bf62005-12-30 16:28:01 +00002104
2105 if( rc==SQLITE_OK ){
2106 if( p->inTrans==TRANS_NONE ){
2107 pBt->nTransaction++;
2108 }
2109 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
2110 if( p->inTrans>pBt->inTransaction ){
2111 pBt->inTransaction = p->inTrans;
2112 }
2113 }
2114
2115 btreeIntegrity(p);
drhb8ca3072001-12-05 00:21:20 +00002116 return rc;
drha059ad02001-04-17 20:09:11 +00002117}
2118
danielk1977687566d2004-11-02 12:56:41 +00002119#ifndef SQLITE_OMIT_AUTOVACUUM
2120
2121/*
2122** Set the pointer-map entries for all children of page pPage. Also, if
2123** pPage contains cells that point to overflow pages, set the pointer
2124** map entries for the overflow pages as well.
2125*/
2126static int setChildPtrmaps(MemPage *pPage){
2127 int i; /* Counter variable */
2128 int nCell; /* Number of cells in page pPage */
2129 int rc = SQLITE_OK; /* Return code */
danielk1977aef0bf62005-12-30 16:28:01 +00002130 BtShared *pBt = pPage->pBt;
danielk1977687566d2004-11-02 12:56:41 +00002131 int isInitOrig = pPage->isInit;
2132 Pgno pgno = pPage->pgno;
2133
2134 initPage(pPage, 0);
2135 nCell = pPage->nCell;
2136
2137 for(i=0; i<nCell; i++){
danielk1977687566d2004-11-02 12:56:41 +00002138 u8 *pCell = findCell(pPage, i);
2139
danielk197726836652005-01-17 01:33:13 +00002140 rc = ptrmapPutOvflPtr(pPage, pCell);
2141 if( rc!=SQLITE_OK ){
2142 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002143 }
danielk197726836652005-01-17 01:33:13 +00002144
danielk1977687566d2004-11-02 12:56:41 +00002145 if( !pPage->leaf ){
2146 Pgno childPgno = get4byte(pCell);
2147 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2148 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
2149 }
2150 }
2151
2152 if( !pPage->leaf ){
2153 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2154 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2155 }
2156
2157set_child_ptrmaps_out:
2158 pPage->isInit = isInitOrig;
2159 return rc;
2160}
2161
2162/*
2163** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
2164** page, is a pointer to page iFrom. Modify this pointer so that it points to
2165** iTo. Parameter eType describes the type of pointer to be modified, as
2166** follows:
2167**
2168** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2169** page of pPage.
2170**
2171** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2172** page pointed to by one of the cells on pPage.
2173**
2174** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2175** overflow page in the list.
2176*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00002177static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
danielk1977687566d2004-11-02 12:56:41 +00002178 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00002179 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00002180 if( get4byte(pPage->aData)!=iFrom ){
drh49285702005-09-17 15:20:26 +00002181 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002182 }
danielk1977f78fc082004-11-02 14:40:32 +00002183 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00002184 }else{
2185 int isInitOrig = pPage->isInit;
2186 int i;
2187 int nCell;
2188
2189 initPage(pPage, 0);
2190 nCell = pPage->nCell;
2191
danielk1977687566d2004-11-02 12:56:41 +00002192 for(i=0; i<nCell; i++){
2193 u8 *pCell = findCell(pPage, i);
2194 if( eType==PTRMAP_OVERFLOW1 ){
2195 CellInfo info;
2196 parseCellPtr(pPage, pCell, &info);
2197 if( info.iOverflow ){
2198 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
2199 put4byte(&pCell[info.iOverflow], iTo);
2200 break;
2201 }
2202 }
2203 }else{
2204 if( get4byte(pCell)==iFrom ){
2205 put4byte(pCell, iTo);
2206 break;
2207 }
2208 }
2209 }
2210
2211 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00002212 if( eType!=PTRMAP_BTREE ||
2213 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
drh49285702005-09-17 15:20:26 +00002214 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002215 }
danielk1977687566d2004-11-02 12:56:41 +00002216 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2217 }
2218
2219 pPage->isInit = isInitOrig;
2220 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002221 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002222}
2223
danielk1977003ba062004-11-04 02:57:33 +00002224
danielk19777701e812005-01-10 12:59:51 +00002225/*
2226** Move the open database page pDbPage to location iFreePage in the
2227** database. The pDbPage reference remains valid.
2228*/
danielk1977003ba062004-11-04 02:57:33 +00002229static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:01 +00002230 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:51 +00002231 MemPage *pDbPage, /* Open page to move */
2232 u8 eType, /* Pointer map 'type' entry for pDbPage */
2233 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
2234 Pgno iFreePage /* The location to move pDbPage to */
danielk1977003ba062004-11-04 02:57:33 +00002235){
2236 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2237 Pgno iDbPage = pDbPage->pgno;
2238 Pager *pPager = pBt->pPager;
2239 int rc;
2240
danielk1977a0bf2652004-11-04 14:30:04 +00002241 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2242 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
danielk1977003ba062004-11-04 02:57:33 +00002243
2244 /* Move page iDbPage from it's current location to page number iFreePage */
2245 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2246 iDbPage, iFreePage, iPtrPage, eType));
danielk19773b8a05f2007-03-19 17:44:26 +00002247 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
danielk1977003ba062004-11-04 02:57:33 +00002248 if( rc!=SQLITE_OK ){
2249 return rc;
2250 }
2251 pDbPage->pgno = iFreePage;
2252
2253 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2254 ** that point to overflow pages. The pointer map entries for all these
2255 ** pages need to be changed.
2256 **
2257 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2258 ** pointer to a subsequent overflow page. If this is the case, then
2259 ** the pointer map needs to be updated for the subsequent overflow page.
2260 */
danielk1977a0bf2652004-11-04 14:30:04 +00002261 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00002262 rc = setChildPtrmaps(pDbPage);
2263 if( rc!=SQLITE_OK ){
2264 return rc;
2265 }
2266 }else{
2267 Pgno nextOvfl = get4byte(pDbPage->aData);
2268 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00002269 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2270 if( rc!=SQLITE_OK ){
2271 return rc;
2272 }
2273 }
2274 }
2275
2276 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2277 ** that it points at iFreePage. Also fix the pointer map entry for
2278 ** iPtrPage.
2279 */
danielk1977a0bf2652004-11-04 14:30:04 +00002280 if( eType!=PTRMAP_ROOTPAGE ){
drh0787db62007-03-04 13:15:27 +00002281 rc = getPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00002282 if( rc!=SQLITE_OK ){
2283 return rc;
2284 }
danielk19773b8a05f2007-03-19 17:44:26 +00002285 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:04 +00002286 if( rc!=SQLITE_OK ){
2287 releasePage(pPtrPage);
2288 return rc;
2289 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002290 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00002291 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00002292 if( rc==SQLITE_OK ){
2293 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2294 }
danielk1977003ba062004-11-04 02:57:33 +00002295 }
danielk1977003ba062004-11-04 02:57:33 +00002296 return rc;
2297}
2298
danielk1977dddbcdc2007-04-26 14:42:34 +00002299/* Forward declaration required by incrVacuumStep(). */
drh4f0c5872007-03-26 22:05:01 +00002300static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00002301
2302/*
danielk1977dddbcdc2007-04-26 14:42:34 +00002303** Perform a single step of an incremental-vacuum. If successful,
2304** return SQLITE_OK. If there is no work to do (and therefore no
2305** point in calling this function again), return SQLITE_DONE.
2306**
2307** More specificly, this function attempts to re-organize the
2308** database so that the last page of the file currently in use
2309** is no longer in use.
2310**
2311** If the nFin parameter is non-zero, the implementation assumes
2312** that the caller will keep calling incrVacuumStep() until
2313** it returns SQLITE_DONE or an error, and that nFin is the
2314** number of pages the database file will contain after this
2315** process is complete.
2316*/
2317static int incrVacuumStep(BtShared *pBt, Pgno nFin){
2318 Pgno iLastPg; /* Last page in the database */
2319 Pgno nFreeList; /* Number of pages still on the free-list */
2320
2321 iLastPg = pBt->nTrunc;
2322 if( iLastPg==0 ){
2323 iLastPg = sqlite3PagerPagecount(pBt->pPager);
2324 }
2325
2326 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
2327 int rc;
2328 u8 eType;
2329 Pgno iPtrPage;
2330
2331 nFreeList = get4byte(&pBt->pPage1->aData[36]);
2332 if( nFreeList==0 || nFin==iLastPg ){
2333 return SQLITE_DONE;
2334 }
2335
2336 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
2337 if( rc!=SQLITE_OK ){
2338 return rc;
2339 }
2340 if( eType==PTRMAP_ROOTPAGE ){
2341 return SQLITE_CORRUPT_BKPT;
2342 }
2343
2344 if( eType==PTRMAP_FREEPAGE ){
2345 if( nFin==0 ){
2346 /* Remove the page from the files free-list. This is not required
2347 ** if nFin is non-zero. In this case, the free-list will be
2348 ** truncated to zero after this function returns, so it doesn't
2349 ** matter if it still contains some garbage entries.
2350 */
2351 Pgno iFreePg;
2352 MemPage *pFreePg;
2353 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
2354 if( rc!=SQLITE_OK ){
2355 return rc;
2356 }
2357 assert( iFreePg==iLastPg );
2358 releasePage(pFreePg);
2359 }
2360 } else {
2361 Pgno iFreePg; /* Index of free page to move pLastPg to */
2362 MemPage *pLastPg;
2363
2364 rc = getPage(pBt, iLastPg, &pLastPg, 0);
2365 if( rc!=SQLITE_OK ){
2366 return rc;
2367 }
2368
2369 do {
2370 MemPage *pFreePg;
2371 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
2372 if( rc!=SQLITE_OK ){
2373 releasePage(pLastPg);
2374 return rc;
2375 }
2376 releasePage(pFreePg);
2377 }while( nFin!=0 && iFreePg>nFin );
2378 assert( iFreePg<iLastPg );
2379
2380 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg);
2381 releasePage(pLastPg);
2382 if( rc!=SQLITE_OK ){
2383 return rc;
2384 }
2385 }
2386 }
2387
2388 pBt->nTrunc = iLastPg - 1;
2389 while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
2390 pBt->nTrunc--;
2391 }
2392 return SQLITE_OK;
2393}
2394
2395/*
2396** A write-transaction must be opened before calling this function.
2397** It performs a single unit of work towards an incremental vacuum.
2398**
2399** If the incremental vacuum is finished after this function has run,
2400** SQLITE_DONE is returned. If it is not finished, but no error occured,
2401** SQLITE_OK is returned. Otherwise an SQLite error code.
2402*/
2403int sqlite3BtreeIncrVacuum(Btree *p){
2404 BtShared *pBt = p->pBt;
2405
2406 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
2407 if( !pBt->autoVacuum ){
2408 return SQLITE_DONE;
2409 }
2410
2411 return incrVacuumStep(p->pBt, 0);
2412}
2413
2414/*
danielk19773b8a05f2007-03-19 17:44:26 +00002415** This routine is called prior to sqlite3PagerCommit when a transaction
danielk1977687566d2004-11-02 12:56:41 +00002416** is commited for an auto-vacuum database.
danielk197724168722007-04-02 05:07:47 +00002417**
2418** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
2419** the database file should be truncated to during the commit process.
2420** i.e. the database has been reorganized so that only the first *pnTrunc
2421** pages are in use.
danielk1977687566d2004-11-02 12:56:41 +00002422*/
danielk197724168722007-04-02 05:07:47 +00002423static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
danielk1977dddbcdc2007-04-26 14:42:34 +00002424 int rc = SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002425 Pager *pPager = pBt->pPager;
danielk1977687566d2004-11-02 12:56:41 +00002426#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00002427 int nRef = sqlite3PagerRefcount(pPager);
danielk1977687566d2004-11-02 12:56:41 +00002428#endif
2429
danielk19773b8a05f2007-03-19 17:44:26 +00002430 if( PTRMAP_ISPAGE(pBt, sqlite3PagerPagecount(pPager)) ){
drh49285702005-09-17 15:20:26 +00002431 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002432 }
danielk1977687566d2004-11-02 12:56:41 +00002433
danielk1977dddbcdc2007-04-26 14:42:34 +00002434 assert(pBt->autoVacuum);
2435 if( !pBt->incrVacuum ){
2436 Pgno nFin = 0;
danielk1977687566d2004-11-02 12:56:41 +00002437
danielk1977dddbcdc2007-04-26 14:42:34 +00002438 if( pBt->nTrunc==0 ){
2439 Pgno nFree;
2440 Pgno nPtrmap;
2441 const int pgsz = pBt->pageSize;
2442 Pgno nOrig = sqlite3PagerPagecount(pBt->pPager);
2443 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
2444 nOrig--;
danielk1977687566d2004-11-02 12:56:41 +00002445 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002446 nFree = get4byte(&pBt->pPage1->aData[36]);
2447 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
2448 nFin = nOrig - nFree - nPtrmap;
2449 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
2450 nFin--;
danielk1977ac11ee62005-01-15 12:45:51 +00002451 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002452 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
2453 nFin--;
2454 }
2455 }
danielk1977687566d2004-11-02 12:56:41 +00002456
danielk1977dddbcdc2007-04-26 14:42:34 +00002457 while( rc==SQLITE_OK ){
2458 rc = incrVacuumStep(pBt, nFin);
2459 }
2460 if( rc==SQLITE_DONE ){
2461 assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
2462 rc = SQLITE_OK;
2463 if( pBt->nTrunc ){
2464 sqlite3PagerWrite(pBt->pPage1->pDbPage);
2465 put4byte(&pBt->pPage1->aData[32], 0);
2466 put4byte(&pBt->pPage1->aData[36], 0);
2467 pBt->nTrunc = nFin;
2468 }
2469 }
2470 if( rc!=SQLITE_OK ){
2471 sqlite3PagerRollback(pPager);
2472 }
danielk1977687566d2004-11-02 12:56:41 +00002473 }
2474
danielk1977dddbcdc2007-04-26 14:42:34 +00002475 if( rc==SQLITE_OK ){
2476 *pnTrunc = pBt->nTrunc;
2477 pBt->nTrunc = 0;
2478 }
danielk19773b8a05f2007-03-19 17:44:26 +00002479 assert( nRef==sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002480 return rc;
2481}
danielk1977dddbcdc2007-04-26 14:42:34 +00002482
danielk1977687566d2004-11-02 12:56:41 +00002483#endif
2484
2485/*
drh80e35f42007-03-30 14:06:34 +00002486** This routine does the first phase of a two-phase commit. This routine
2487** causes a rollback journal to be created (if it does not already exist)
2488** and populated with enough information so that if a power loss occurs
2489** the database can be restored to its original state by playing back
2490** the journal. Then the contents of the journal are flushed out to
2491** the disk. After the journal is safely on oxide, the changes to the
2492** database are written into the database file and flushed to oxide.
2493** At the end of this call, the rollback journal still exists on the
2494** disk and we are still holding all locks, so the transaction has not
2495** committed. See sqlite3BtreeCommit() for the second phase of the
2496** commit process.
2497**
2498** This call is a no-op if no write-transaction is currently active on pBt.
2499**
2500** Otherwise, sync the database file for the btree pBt. zMaster points to
2501** the name of a master journal file that should be written into the
2502** individual journal file, or is NULL, indicating no master journal file
2503** (single database transaction).
2504**
2505** When this is called, the master journal should already have been
2506** created, populated with this journal pointer and synced to disk.
2507**
2508** Once this is routine has returned, the only thing required to commit
2509** the write-transaction for this database file is to delete the journal.
2510*/
2511int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
2512 int rc = SQLITE_OK;
2513 if( p->inTrans==TRANS_WRITE ){
2514 BtShared *pBt = p->pBt;
2515 Pgno nTrunc = 0;
2516#ifndef SQLITE_OMIT_AUTOVACUUM
2517 if( pBt->autoVacuum ){
2518 rc = autoVacuumCommit(pBt, &nTrunc);
2519 if( rc!=SQLITE_OK ){
2520 return rc;
2521 }
2522 }
2523#endif
2524 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc);
2525 }
2526 return rc;
2527}
2528
2529/*
drh2aa679f2001-06-25 02:11:07 +00002530** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00002531**
drh6e345992007-03-30 11:12:08 +00002532** This routine implements the second phase of a 2-phase commit. The
2533** sqlite3BtreeSync() routine does the first phase and should be invoked
2534** prior to calling this routine. The sqlite3BtreeSync() routine did
2535** all the work of writing information out to disk and flushing the
2536** contents so that they are written onto the disk platter. All this
2537** routine has to do is delete or truncate the rollback journal
2538** (which causes the transaction to commit) and drop locks.
2539**
drh5e00f6c2001-09-13 13:46:56 +00002540** This will release the write lock on the database file. If there
2541** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002542*/
drh80e35f42007-03-30 14:06:34 +00002543int sqlite3BtreeCommitPhaseTwo(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00002544 BtShared *pBt = p->pBt;
2545
2546 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002547
2548 /* If the handle has a write-transaction open, commit the shared-btrees
2549 ** transaction and set the shared state to TRANS_READ.
2550 */
2551 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:47 +00002552 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002553 assert( pBt->inTransaction==TRANS_WRITE );
2554 assert( pBt->nTransaction>0 );
drh80e35f42007-03-30 14:06:34 +00002555 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
danielk19777f7bc662006-01-23 13:47:47 +00002556 if( rc!=SQLITE_OK ){
2557 return rc;
2558 }
danielk1977aef0bf62005-12-30 16:28:01 +00002559 pBt->inTransaction = TRANS_READ;
2560 pBt->inStmt = 0;
danielk1977ee5741e2004-05-31 10:01:34 +00002561 }
danielk19777f7bc662006-01-23 13:47:47 +00002562 unlockAllTables(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002563
2564 /* If the handle has any kind of transaction open, decrement the transaction
2565 ** count of the shared btree. If the transaction count reaches 0, set
2566 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2567 ** will unlock the pager.
2568 */
2569 if( p->inTrans!=TRANS_NONE ){
2570 pBt->nTransaction--;
2571 if( 0==pBt->nTransaction ){
2572 pBt->inTransaction = TRANS_NONE;
2573 }
2574 }
2575
2576 /* Set the handles current transaction state to TRANS_NONE and unlock
2577 ** the pager if this call closed the only read or write transaction.
2578 */
2579 p->inTrans = TRANS_NONE;
drh5e00f6c2001-09-13 13:46:56 +00002580 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002581
2582 btreeIntegrity(p);
danielk19777f7bc662006-01-23 13:47:47 +00002583 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002584}
2585
drh80e35f42007-03-30 14:06:34 +00002586/*
2587** Do both phases of a commit.
2588*/
2589int sqlite3BtreeCommit(Btree *p){
2590 int rc;
2591 rc = sqlite3BtreeCommitPhaseOne(p, 0);
2592 if( rc==SQLITE_OK ){
2593 rc = sqlite3BtreeCommitPhaseTwo(p);
2594 }
2595 return rc;
2596}
2597
danielk1977fbcd5852004-06-15 02:44:18 +00002598#ifndef NDEBUG
2599/*
2600** Return the number of write-cursors open on this handle. This is for use
2601** in assert() expressions, so it is only compiled if NDEBUG is not
2602** defined.
2603*/
danielk1977aef0bf62005-12-30 16:28:01 +00002604static int countWriteCursors(BtShared *pBt){
danielk1977fbcd5852004-06-15 02:44:18 +00002605 BtCursor *pCur;
2606 int r = 0;
2607 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
danielk1977aef0bf62005-12-30 16:28:01 +00002608 if( pCur->wrFlag ) r++;
danielk1977fbcd5852004-06-15 02:44:18 +00002609 }
2610 return r;
2611}
2612#endif
2613
drh77bba592006-08-13 18:39:26 +00002614#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drhda200cc2004-05-09 11:51:38 +00002615/*
2616** Print debugging information about all cursors to standard output.
2617*/
danielk1977aef0bf62005-12-30 16:28:01 +00002618void sqlite3BtreeCursorList(Btree *p){
drhda200cc2004-05-09 11:51:38 +00002619 BtCursor *pCur;
danielk1977aef0bf62005-12-30 16:28:01 +00002620 BtShared *pBt = p->pBt;
drhda200cc2004-05-09 11:51:38 +00002621 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
2622 MemPage *pPage = pCur->pPage;
2623 char *zMode = pCur->wrFlag ? "rw" : "ro";
drhfe63d1c2004-09-08 20:13:04 +00002624 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
2625 pCur, pCur->pgnoRoot, zMode,
drhda200cc2004-05-09 11:51:38 +00002626 pPage ? pPage->pgno : 0, pCur->idx,
danielk1977da184232006-01-05 11:34:32 +00002627 (pCur->eState==CURSOR_VALID) ? "" : " eof"
drhda200cc2004-05-09 11:51:38 +00002628 );
2629 }
2630}
2631#endif
2632
drhc39e0002004-05-07 23:50:57 +00002633/*
drhecdc7532001-09-23 02:35:53 +00002634** Rollback the transaction in progress. All cursors will be
2635** invalided by this operation. Any attempt to use a cursor
2636** that was open at the beginning of this operation will result
2637** in an error.
drh5e00f6c2001-09-13 13:46:56 +00002638**
2639** This will release the write lock on the database file. If there
2640** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002641*/
danielk1977aef0bf62005-12-30 16:28:01 +00002642int sqlite3BtreeRollback(Btree *p){
danielk19778d34dfd2006-01-24 16:37:57 +00002643 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002644 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:47 +00002645 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:01 +00002646
danielk19772b8c13e2006-01-24 14:21:24 +00002647 rc = saveAllCursors(pBt, 0, 0);
danielk19778d34dfd2006-01-24 16:37:57 +00002648#ifndef SQLITE_OMIT_SHARED_CACHE
danielk19772b8c13e2006-01-24 14:21:24 +00002649 if( rc!=SQLITE_OK ){
danielk19778d34dfd2006-01-24 16:37:57 +00002650 /* This is a horrible situation. An IO or malloc() error occured whilst
2651 ** trying to save cursor positions. If this is an automatic rollback (as
2652 ** the result of a constraint, malloc() failure or IO error) then
2653 ** the cache may be internally inconsistent (not contain valid trees) so
2654 ** we cannot simply return the error to the caller. Instead, abort
2655 ** all queries that may be using any of the cursors that failed to save.
2656 */
2657 while( pBt->pCursor ){
2658 sqlite3 *db = pBt->pCursor->pBtree->pSqlite;
2659 if( db ){
2660 sqlite3AbortOtherActiveVdbes(db, 0);
2661 }
2662 }
danielk19772b8c13e2006-01-24 14:21:24 +00002663 }
danielk19778d34dfd2006-01-24 16:37:57 +00002664#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002665 btreeIntegrity(p);
2666 unlockAllTables(p);
2667
2668 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:57 +00002669 int rc2;
danielk1977aef0bf62005-12-30 16:28:01 +00002670
danielk1977dddbcdc2007-04-26 14:42:34 +00002671#ifndef SQLITE_OMIT_AUTOVACUUM
2672 pBt->nTrunc = 0;
2673#endif
2674
danielk19778d34dfd2006-01-24 16:37:57 +00002675 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:26 +00002676 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:57 +00002677 if( rc2!=SQLITE_OK ){
2678 rc = rc2;
2679 }
2680
drh24cd67e2004-05-10 16:18:47 +00002681 /* The rollback may have destroyed the pPage1->aData value. So
2682 ** call getPage() on page 1 again to make sure pPage1->aData is
2683 ** set correctly. */
drh0787db62007-03-04 13:15:27 +00002684 if( getPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh24cd67e2004-05-10 16:18:47 +00002685 releasePage(pPage1);
2686 }
danielk1977fbcd5852004-06-15 02:44:18 +00002687 assert( countWriteCursors(pBt)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00002688 pBt->inTransaction = TRANS_READ;
drh24cd67e2004-05-10 16:18:47 +00002689 }
danielk1977aef0bf62005-12-30 16:28:01 +00002690
2691 if( p->inTrans!=TRANS_NONE ){
2692 assert( pBt->nTransaction>0 );
2693 pBt->nTransaction--;
2694 if( 0==pBt->nTransaction ){
2695 pBt->inTransaction = TRANS_NONE;
2696 }
2697 }
2698
2699 p->inTrans = TRANS_NONE;
danielk1977ee5741e2004-05-31 10:01:34 +00002700 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00002701 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002702
2703 btreeIntegrity(p);
drha059ad02001-04-17 20:09:11 +00002704 return rc;
2705}
2706
2707/*
drhab01f612004-05-22 02:55:23 +00002708** Start a statement subtransaction. The subtransaction can
2709** can be rolled back independently of the main transaction.
2710** You must start a transaction before starting a subtransaction.
2711** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00002712** commits or rolls back.
2713**
drhab01f612004-05-22 02:55:23 +00002714** Only one subtransaction may be active at a time. It is an error to try
2715** to start a new subtransaction if another subtransaction is already active.
2716**
2717** Statement subtransactions are used around individual SQL statements
2718** that are contained within a BEGIN...COMMIT block. If a constraint
2719** error occurs within the statement, the effect of that one statement
2720** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002721*/
danielk1977aef0bf62005-12-30 16:28:01 +00002722int sqlite3BtreeBeginStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002723 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002724 BtShared *pBt = p->pBt;
2725 if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
drhf74b8d92002-09-01 23:20:45 +00002726 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh0d65dc02002-02-03 00:56:09 +00002727 }
danielk1977aef0bf62005-12-30 16:28:01 +00002728 assert( pBt->inTransaction==TRANS_WRITE );
danielk19773b8a05f2007-03-19 17:44:26 +00002729 rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
drh3aac2dd2004-04-26 14:10:20 +00002730 pBt->inStmt = 1;
drh663fc632002-02-02 18:49:19 +00002731 return rc;
2732}
2733
2734
2735/*
drhab01f612004-05-22 02:55:23 +00002736** Commit the statment subtransaction currently in progress. If no
2737** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002738*/
danielk1977aef0bf62005-12-30 16:28:01 +00002739int sqlite3BtreeCommitStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002740 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002741 BtShared *pBt = p->pBt;
drh3aac2dd2004-04-26 14:10:20 +00002742 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002743 rc = sqlite3PagerStmtCommit(pBt->pPager);
drh663fc632002-02-02 18:49:19 +00002744 }else{
2745 rc = SQLITE_OK;
2746 }
drh3aac2dd2004-04-26 14:10:20 +00002747 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00002748 return rc;
2749}
2750
2751/*
drhab01f612004-05-22 02:55:23 +00002752** Rollback the active statement subtransaction. If no subtransaction
2753** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002754**
drhab01f612004-05-22 02:55:23 +00002755** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002756** to use a cursor that was open at the beginning of this operation
2757** will result in an error.
2758*/
danielk1977aef0bf62005-12-30 16:28:01 +00002759int sqlite3BtreeRollbackStmt(Btree *p){
danielk197797a227c2006-01-20 16:32:04 +00002760 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00002761 BtShared *pBt = p->pBt;
danielk197797a227c2006-01-20 16:32:04 +00002762 sqlite3MallocDisallow();
2763 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002764 rc = sqlite3PagerStmtRollback(pBt->pPager);
danielk197797a227c2006-01-20 16:32:04 +00002765 assert( countWriteCursors(pBt)==0 );
2766 pBt->inStmt = 0;
2767 }
2768 sqlite3MallocAllow();
drh663fc632002-02-02 18:49:19 +00002769 return rc;
2770}
2771
2772/*
drh3aac2dd2004-04-26 14:10:20 +00002773** Default key comparison function to be used if no comparison function
2774** is specified on the sqlite3BtreeCursor() call.
2775*/
2776static int dfltCompare(
2777 void *NotUsed, /* User data is not used */
2778 int n1, const void *p1, /* First key to compare */
2779 int n2, const void *p2 /* Second key to compare */
2780){
2781 int c;
2782 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
2783 if( c==0 ){
2784 c = n1 - n2;
2785 }
2786 return c;
2787}
2788
2789/*
drh8b2f49b2001-06-08 00:21:52 +00002790** Create a new cursor for the BTree whose root is on the page
2791** iTable. The act of acquiring a cursor gets a read lock on
2792** the database file.
drh1bee3d72001-10-15 00:44:35 +00002793**
2794** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002795** If wrFlag==1, then the cursor can be used for reading or for
2796** writing if other conditions for writing are also met. These
2797** are the conditions that must be met in order for writing to
2798** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002799**
drhf74b8d92002-09-01 23:20:45 +00002800** 1: The cursor must have been opened with wrFlag==1
2801**
drhfe5d71d2007-03-19 11:54:10 +00002802** 2: Other database connections that share the same pager cache
2803** but which are not in the READ_UNCOMMITTED state may not have
2804** cursors open with wrFlag==0 on the same table. Otherwise
2805** the changes made by this write cursor would be visible to
2806** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:45 +00002807**
2808** 3: The database must be writable (not on read-only media)
2809**
2810** 4: There must be an active transaction.
2811**
drh6446c4d2001-12-15 14:22:18 +00002812** No checking is done to make sure that page iTable really is the
2813** root page of a b-tree. If it is not, then the cursor acquired
2814** will not work correctly.
drh3aac2dd2004-04-26 14:10:20 +00002815**
2816** The comparison function must be logically the same for every cursor
2817** on a particular table. Changing the comparison function will result
2818** in incorrect operations. If the comparison function is NULL, a
2819** default comparison function is used. The comparison function is
2820** always ignored for INTKEY tables.
drha059ad02001-04-17 20:09:11 +00002821*/
drh3aac2dd2004-04-26 14:10:20 +00002822int sqlite3BtreeCursor(
danielk1977aef0bf62005-12-30 16:28:01 +00002823 Btree *p, /* The btree */
drh3aac2dd2004-04-26 14:10:20 +00002824 int iTable, /* Root page of table to open */
2825 int wrFlag, /* 1 to write. 0 read-only */
2826 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2827 void *pArg, /* First arg to xCompare() */
2828 BtCursor **ppCur /* Write new cursor here */
2829){
drha059ad02001-04-17 20:09:11 +00002830 int rc;
drh8dcd7ca2004-08-08 19:43:29 +00002831 BtCursor *pCur;
danielk1977aef0bf62005-12-30 16:28:01 +00002832 BtShared *pBt = p->pBt;
drhecdc7532001-09-23 02:35:53 +00002833
drh8dcd7ca2004-08-08 19:43:29 +00002834 *ppCur = 0;
2835 if( wrFlag ){
drh8dcd7ca2004-08-08 19:43:29 +00002836 if( pBt->readOnly ){
2837 return SQLITE_READONLY;
2838 }
drh980b1a72006-08-16 16:42:48 +00002839 if( checkReadLocks(p, iTable, 0) ){
drh8dcd7ca2004-08-08 19:43:29 +00002840 return SQLITE_LOCKED;
2841 }
drha0c9a112004-03-10 13:42:37 +00002842 }
danielk1977aef0bf62005-12-30 16:28:01 +00002843
drh4b70f112004-05-02 21:12:19 +00002844 if( pBt->pPage1==0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002845 rc = lockBtreeWithRetry(p);
drha059ad02001-04-17 20:09:11 +00002846 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002847 return rc;
2848 }
drh1831f182007-04-24 17:35:59 +00002849 if( pBt->readOnly && wrFlag ){
2850 return SQLITE_READONLY;
2851 }
drha059ad02001-04-17 20:09:11 +00002852 }
danielk1977da184232006-01-05 11:34:32 +00002853 pCur = sqliteMalloc( sizeof(*pCur) );
drha059ad02001-04-17 20:09:11 +00002854 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +00002855 rc = SQLITE_NOMEM;
2856 goto create_cursor_exception;
2857 }
drh8b2f49b2001-06-08 00:21:52 +00002858 pCur->pgnoRoot = (Pgno)iTable;
danielk19773b8a05f2007-03-19 17:44:26 +00002859 if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){
drh24cd67e2004-05-10 16:18:47 +00002860 rc = SQLITE_EMPTY;
2861 goto create_cursor_exception;
2862 }
drhde647132004-05-07 17:57:49 +00002863 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
drhbd03cae2001-06-02 02:40:57 +00002864 if( rc!=SQLITE_OK ){
2865 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002866 }
danielk1977aef0bf62005-12-30 16:28:01 +00002867
danielk1977aef0bf62005-12-30 16:28:01 +00002868 /* Now that no other errors can occur, finish filling in the BtCursor
2869 ** variables, link the cursor into the BtShared list and set *ppCur (the
2870 ** output argument to this function).
2871 */
drh3aac2dd2004-04-26 14:10:20 +00002872 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2873 pCur->pArg = pArg;
danielk1977aef0bf62005-12-30 16:28:01 +00002874 pCur->pBtree = p;
drhecdc7532001-09-23 02:35:53 +00002875 pCur->wrFlag = wrFlag;
drha059ad02001-04-17 20:09:11 +00002876 pCur->pNext = pBt->pCursor;
2877 if( pCur->pNext ){
2878 pCur->pNext->pPrev = pCur;
2879 }
2880 pBt->pCursor = pCur;
danielk1977da184232006-01-05 11:34:32 +00002881 pCur->eState = CURSOR_INVALID;
drh2af926b2001-05-15 00:39:25 +00002882 *ppCur = pCur;
drhbd03cae2001-06-02 02:40:57 +00002883
danielk1977aef0bf62005-12-30 16:28:01 +00002884 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00002885create_cursor_exception:
drhbd03cae2001-06-02 02:40:57 +00002886 if( pCur ){
drh3aac2dd2004-04-26 14:10:20 +00002887 releasePage(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00002888 sqliteFree(pCur);
2889 }
drh5e00f6c2001-09-13 13:46:56 +00002890 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002891 return rc;
drha059ad02001-04-17 20:09:11 +00002892}
2893
drh7a224de2004-06-02 01:22:02 +00002894#if 0 /* Not Used */
drhd3d39e92004-05-20 22:16:29 +00002895/*
2896** Change the value of the comparison function used by a cursor.
2897*/
danielk1977bf3b7212004-05-18 10:06:24 +00002898void sqlite3BtreeSetCompare(
drhd3d39e92004-05-20 22:16:29 +00002899 BtCursor *pCur, /* The cursor to whose comparison function is changed */
2900 int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */
2901 void *pArg /* First argument to xCmp() */
danielk1977bf3b7212004-05-18 10:06:24 +00002902){
2903 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2904 pCur->pArg = pArg;
2905}
drh7a224de2004-06-02 01:22:02 +00002906#endif
danielk1977bf3b7212004-05-18 10:06:24 +00002907
drha059ad02001-04-17 20:09:11 +00002908/*
drh5e00f6c2001-09-13 13:46:56 +00002909** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002910** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002911*/
drh3aac2dd2004-04-26 14:10:20 +00002912int sqlite3BtreeCloseCursor(BtCursor *pCur){
danielk1977aef0bf62005-12-30 16:28:01 +00002913 BtShared *pBt = pCur->pBtree->pBt;
drhbf700f32007-03-31 02:36:44 +00002914 clearCursorPosition(pCur);
drha059ad02001-04-17 20:09:11 +00002915 if( pCur->pPrev ){
2916 pCur->pPrev->pNext = pCur->pNext;
2917 }else{
2918 pBt->pCursor = pCur->pNext;
2919 }
2920 if( pCur->pNext ){
2921 pCur->pNext->pPrev = pCur->pPrev;
2922 }
drh3aac2dd2004-04-26 14:10:20 +00002923 releasePage(pCur->pPage);
drh5e00f6c2001-09-13 13:46:56 +00002924 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00002925 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +00002926 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002927}
2928
drh7e3b0a02001-04-28 16:52:40 +00002929/*
drh5e2f8b92001-05-28 00:41:15 +00002930** Make a temporary cursor by filling in the fields of pTempCur.
2931** The temporary cursor is not on the cursor list for the Btree.
2932*/
drh14acc042001-06-10 19:56:58 +00002933static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +00002934 memcpy(pTempCur, pCur, sizeof(*pCur));
2935 pTempCur->pNext = 0;
2936 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00002937 if( pTempCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002938 sqlite3PagerRef(pTempCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002939 }
drh5e2f8b92001-05-28 00:41:15 +00002940}
2941
2942/*
drhbd03cae2001-06-02 02:40:57 +00002943** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002944** function above.
2945*/
drh14acc042001-06-10 19:56:58 +00002946static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +00002947 if( pCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002948 sqlite3PagerUnref(pCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002949 }
drh5e2f8b92001-05-28 00:41:15 +00002950}
2951
2952/*
drh9188b382004-05-14 21:12:22 +00002953** Make sure the BtCursor.info field of the given cursor is valid.
drhab01f612004-05-22 02:55:23 +00002954** If it is not already valid, call parseCell() to fill it in.
2955**
2956** BtCursor.info is a cache of the information in the current cell.
2957** Using this cache reduces the number of calls to parseCell().
drh9188b382004-05-14 21:12:22 +00002958*/
2959static void getCellInfo(BtCursor *pCur){
drh271efa52004-05-30 19:19:05 +00002960 if( pCur->info.nSize==0 ){
drh3a41a3f2004-05-30 02:14:17 +00002961 parseCell(pCur->pPage, pCur->idx, &pCur->info);
drh9188b382004-05-14 21:12:22 +00002962 }else{
2963#ifndef NDEBUG
2964 CellInfo info;
drh51c6d962004-06-06 00:42:25 +00002965 memset(&info, 0, sizeof(info));
drh3a41a3f2004-05-30 02:14:17 +00002966 parseCell(pCur->pPage, pCur->idx, &info);
drh9188b382004-05-14 21:12:22 +00002967 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
2968#endif
2969 }
2970}
2971
2972/*
drh3aac2dd2004-04-26 14:10:20 +00002973** Set *pSize to the size of the buffer needed to hold the value of
2974** the key for the current entry. If the cursor is not pointing
2975** to a valid entry, *pSize is set to 0.
2976**
drh4b70f112004-05-02 21:12:19 +00002977** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00002978** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00002979*/
drh4a1c3802004-05-12 15:15:47 +00002980int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
drhbf700f32007-03-31 02:36:44 +00002981 int rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00002982 if( rc==SQLITE_OK ){
2983 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2984 if( pCur->eState==CURSOR_INVALID ){
2985 *pSize = 0;
2986 }else{
2987 getCellInfo(pCur);
2988 *pSize = pCur->info.nKey;
2989 }
drh72f82862001-05-24 21:06:34 +00002990 }
danielk1977da184232006-01-05 11:34:32 +00002991 return rc;
drha059ad02001-04-17 20:09:11 +00002992}
drh2af926b2001-05-15 00:39:25 +00002993
drh72f82862001-05-24 21:06:34 +00002994/*
drh0e1c19e2004-05-11 00:58:56 +00002995** Set *pSize to the number of bytes of data in the entry the
2996** cursor currently points to. Always return SQLITE_OK.
2997** Failure is not possible. If the cursor is not currently
2998** pointing to an entry (which can happen, for example, if
2999** the database is empty) then *pSize is set to 0.
3000*/
3001int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
drhbf700f32007-03-31 02:36:44 +00003002 int rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003003 if( rc==SQLITE_OK ){
3004 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
3005 if( pCur->eState==CURSOR_INVALID ){
3006 /* Not pointing at a valid entry - set *pSize to 0. */
3007 *pSize = 0;
3008 }else{
3009 getCellInfo(pCur);
3010 *pSize = pCur->info.nData;
3011 }
drh0e1c19e2004-05-11 00:58:56 +00003012 }
danielk1977da184232006-01-05 11:34:32 +00003013 return rc;
drh0e1c19e2004-05-11 00:58:56 +00003014}
3015
3016/*
drh72f82862001-05-24 21:06:34 +00003017** Read payload information from the entry that the pCur cursor is
3018** pointing to. Begin reading the payload at "offset" and read
3019** a total of "amt" bytes. Put the result in zBuf.
3020**
3021** This routine does not make a distinction between key and data.
drhab01f612004-05-22 02:55:23 +00003022** It just reads bytes from the payload area. Data might appear
3023** on the main page or be scattered out on multiple overflow pages.
drh72f82862001-05-24 21:06:34 +00003024*/
drh3aac2dd2004-04-26 14:10:20 +00003025static int getPayload(
3026 BtCursor *pCur, /* Cursor pointing to entry to read from */
3027 int offset, /* Begin reading this far into payload */
3028 int amt, /* Read this many bytes */
3029 unsigned char *pBuf, /* Write the bytes into this buffer */
3030 int skipKey /* offset begins at data if this is true */
3031){
3032 unsigned char *aPayload;
drh2af926b2001-05-15 00:39:25 +00003033 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +00003034 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003035 MemPage *pPage;
danielk1977aef0bf62005-12-30 16:28:01 +00003036 BtShared *pBt;
drh6f11bef2004-05-13 01:12:56 +00003037 int ovflSize;
drhfa1a98a2004-05-14 19:08:17 +00003038 u32 nKey;
drh3aac2dd2004-04-26 14:10:20 +00003039
drh72f82862001-05-24 21:06:34 +00003040 assert( pCur!=0 && pCur->pPage!=0 );
danielk1977da184232006-01-05 11:34:32 +00003041 assert( pCur->eState==CURSOR_VALID );
danielk1977aef0bf62005-12-30 16:28:01 +00003042 pBt = pCur->pBtree->pBt;
drh3aac2dd2004-04-26 14:10:20 +00003043 pPage = pCur->pPage;
3044 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00003045 getCellInfo(pCur);
drh366fda62006-01-13 02:35:09 +00003046 aPayload = pCur->info.pCell + pCur->info.nHeader;
drh3aac2dd2004-04-26 14:10:20 +00003047 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003048 nKey = 0;
3049 }else{
3050 nKey = pCur->info.nKey;
drh3aac2dd2004-04-26 14:10:20 +00003051 }
3052 assert( offset>=0 );
3053 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003054 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00003055 }
drhfa1a98a2004-05-14 19:08:17 +00003056 if( offset+amt > nKey+pCur->info.nData ){
drha34b6762004-05-07 13:30:42 +00003057 return SQLITE_ERROR;
drh3aac2dd2004-04-26 14:10:20 +00003058 }
drhfa1a98a2004-05-14 19:08:17 +00003059 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00003060 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00003061 if( a+offset>pCur->info.nLocal ){
3062 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00003063 }
drha34b6762004-05-07 13:30:42 +00003064 memcpy(pBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +00003065 if( a==amt ){
3066 return SQLITE_OK;
3067 }
drh2aa679f2001-06-25 02:11:07 +00003068 offset = 0;
drha34b6762004-05-07 13:30:42 +00003069 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00003070 amt -= a;
drhdd793422001-06-28 01:54:48 +00003071 }else{
drhfa1a98a2004-05-14 19:08:17 +00003072 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00003073 }
danielk1977cfe9a692004-06-16 12:00:29 +00003074 ovflSize = pBt->usableSize - 4;
drhbd03cae2001-06-02 02:40:57 +00003075 if( amt>0 ){
drhfa1a98a2004-05-14 19:08:17 +00003076 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977cfe9a692004-06-16 12:00:29 +00003077 while( amt>0 && nextPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00003078 DbPage *pDbPage;
3079 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
danielk1977cfe9a692004-06-16 12:00:29 +00003080 if( rc!=0 ){
3081 return rc;
drh2af926b2001-05-15 00:39:25 +00003082 }
danielk19773b8a05f2007-03-19 17:44:26 +00003083 aPayload = sqlite3PagerGetData(pDbPage);
danielk1977cfe9a692004-06-16 12:00:29 +00003084 nextPage = get4byte(aPayload);
3085 if( offset<ovflSize ){
3086 int a = amt;
3087 if( a + offset > ovflSize ){
3088 a = ovflSize - offset;
3089 }
3090 memcpy(pBuf, &aPayload[offset+4], a);
3091 offset = 0;
3092 amt -= a;
3093 pBuf += a;
3094 }else{
3095 offset -= ovflSize;
3096 }
danielk19773b8a05f2007-03-19 17:44:26 +00003097 sqlite3PagerUnref(pDbPage);
drh2af926b2001-05-15 00:39:25 +00003098 }
drh2af926b2001-05-15 00:39:25 +00003099 }
danielk1977cfe9a692004-06-16 12:00:29 +00003100
drha7fcb052001-12-14 15:09:55 +00003101 if( amt>0 ){
drh49285702005-09-17 15:20:26 +00003102 return SQLITE_CORRUPT_BKPT;
drha7fcb052001-12-14 15:09:55 +00003103 }
3104 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00003105}
3106
drh72f82862001-05-24 21:06:34 +00003107/*
drh3aac2dd2004-04-26 14:10:20 +00003108** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003109** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003110** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00003111**
drh3aac2dd2004-04-26 14:10:20 +00003112** Return SQLITE_OK on success or an error code if anything goes
3113** wrong. An error is returned if "offset+amt" is larger than
3114** the available payload.
drh72f82862001-05-24 21:06:34 +00003115*/
drha34b6762004-05-07 13:30:42 +00003116int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhbf700f32007-03-31 02:36:44 +00003117 int rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003118 if( rc==SQLITE_OK ){
3119 assert( pCur->eState==CURSOR_VALID );
3120 assert( pCur->pPage!=0 );
3121 if( pCur->pPage->intKey ){
3122 return SQLITE_CORRUPT_BKPT;
3123 }
3124 assert( pCur->pPage->intKey==0 );
3125 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3126 rc = getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
drh6575a222005-03-10 17:06:34 +00003127 }
danielk1977da184232006-01-05 11:34:32 +00003128 return rc;
drh3aac2dd2004-04-26 14:10:20 +00003129}
3130
3131/*
drh3aac2dd2004-04-26 14:10:20 +00003132** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003133** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003134** begins at "offset".
3135**
3136** Return SQLITE_OK on success or an error code if anything goes
3137** wrong. An error is returned if "offset+amt" is larger than
3138** the available payload.
drh72f82862001-05-24 21:06:34 +00003139*/
drh3aac2dd2004-04-26 14:10:20 +00003140int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhbf700f32007-03-31 02:36:44 +00003141 int rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003142 if( rc==SQLITE_OK ){
3143 assert( pCur->eState==CURSOR_VALID );
3144 assert( pCur->pPage!=0 );
3145 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
3146 rc = getPayload(pCur, offset, amt, pBuf, 1);
3147 }
3148 return rc;
drh2af926b2001-05-15 00:39:25 +00003149}
3150
drh72f82862001-05-24 21:06:34 +00003151/*
drh0e1c19e2004-05-11 00:58:56 +00003152** Return a pointer to payload information from the entry that the
3153** pCur cursor is pointing to. The pointer is to the beginning of
3154** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00003155** skipKey==1. The number of bytes of available key/data is written
3156** into *pAmt. If *pAmt==0, then the value returned will not be
3157** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00003158**
3159** This routine is an optimization. It is common for the entire key
3160** and data to fit on the local page and for there to be no overflow
3161** pages. When that is so, this routine can be used to access the
3162** key and data without making a copy. If the key and/or data spills
3163** onto overflow pages, then getPayload() must be used to reassembly
3164** the key/data and copy it into a preallocated buffer.
3165**
3166** The pointer returned by this routine looks directly into the cached
3167** page of the database. The data might change or move the next time
3168** any btree routine is called.
3169*/
3170static const unsigned char *fetchPayload(
3171 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00003172 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00003173 int skipKey /* read beginning at data if this is true */
3174){
3175 unsigned char *aPayload;
3176 MemPage *pPage;
drhfa1a98a2004-05-14 19:08:17 +00003177 u32 nKey;
3178 int nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003179
3180 assert( pCur!=0 && pCur->pPage!=0 );
danielk1977da184232006-01-05 11:34:32 +00003181 assert( pCur->eState==CURSOR_VALID );
drh0e1c19e2004-05-11 00:58:56 +00003182 pPage = pCur->pPage;
drh0e1c19e2004-05-11 00:58:56 +00003183 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00003184 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00003185 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00003186 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00003187 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003188 nKey = 0;
3189 }else{
3190 nKey = pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00003191 }
drh0e1c19e2004-05-11 00:58:56 +00003192 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003193 aPayload += nKey;
3194 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00003195 }else{
drhfa1a98a2004-05-14 19:08:17 +00003196 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00003197 if( nLocal>nKey ){
3198 nLocal = nKey;
3199 }
drh0e1c19e2004-05-11 00:58:56 +00003200 }
drhe51c44f2004-05-30 20:46:09 +00003201 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003202 return aPayload;
3203}
3204
3205
3206/*
drhe51c44f2004-05-30 20:46:09 +00003207** For the entry that cursor pCur is point to, return as
3208** many bytes of the key or data as are available on the local
3209** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00003210**
3211** The pointer returned is ephemeral. The key/data may move
3212** or be destroyed on the next call to any Btree routine.
3213**
3214** These routines is used to get quick access to key and data
3215** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00003216*/
drhe51c44f2004-05-30 20:46:09 +00003217const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
danielk1977da184232006-01-05 11:34:32 +00003218 if( pCur->eState==CURSOR_VALID ){
3219 return (const void*)fetchPayload(pCur, pAmt, 0);
3220 }
3221 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003222}
drhe51c44f2004-05-30 20:46:09 +00003223const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
danielk1977da184232006-01-05 11:34:32 +00003224 if( pCur->eState==CURSOR_VALID ){
3225 return (const void*)fetchPayload(pCur, pAmt, 1);
3226 }
3227 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003228}
3229
3230
3231/*
drh8178a752003-01-05 21:41:40 +00003232** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00003233** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00003234*/
drh3aac2dd2004-04-26 14:10:20 +00003235static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00003236 int rc;
3237 MemPage *pNewPage;
drh3aac2dd2004-04-26 14:10:20 +00003238 MemPage *pOldPage;
danielk1977aef0bf62005-12-30 16:28:01 +00003239 BtShared *pBt = pCur->pBtree->pBt;
drh72f82862001-05-24 21:06:34 +00003240
danielk1977da184232006-01-05 11:34:32 +00003241 assert( pCur->eState==CURSOR_VALID );
drhde647132004-05-07 17:57:49 +00003242 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00003243 if( rc ) return rc;
drh428ae8c2003-01-04 16:48:09 +00003244 pNewPage->idxParent = pCur->idx;
drh3aac2dd2004-04-26 14:10:20 +00003245 pOldPage = pCur->pPage;
3246 pOldPage->idxShift = 0;
3247 releasePage(pOldPage);
drh72f82862001-05-24 21:06:34 +00003248 pCur->pPage = pNewPage;
3249 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003250 pCur->info.nSize = 0;
drh4be295b2003-12-16 03:44:47 +00003251 if( pNewPage->nCell<1 ){
drh49285702005-09-17 15:20:26 +00003252 return SQLITE_CORRUPT_BKPT;
drh4be295b2003-12-16 03:44:47 +00003253 }
drh72f82862001-05-24 21:06:34 +00003254 return SQLITE_OK;
3255}
3256
3257/*
drh8856d6a2004-04-29 14:42:46 +00003258** Return true if the page is the virtual root of its table.
3259**
3260** The virtual root page is the root page for most tables. But
3261** for the table rooted on page 1, sometime the real root page
3262** is empty except for the right-pointer. In such cases the
3263** virtual root page is the page that the right-pointer of page
3264** 1 is pointing to.
3265*/
3266static int isRootPage(MemPage *pPage){
3267 MemPage *pParent = pPage->pParent;
drhda200cc2004-05-09 11:51:38 +00003268 if( pParent==0 ) return 1;
3269 if( pParent->pgno>1 ) return 0;
3270 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
drh8856d6a2004-04-29 14:42:46 +00003271 return 0;
3272}
3273
3274/*
drh5e2f8b92001-05-28 00:41:15 +00003275** Move the cursor up to the parent page.
3276**
3277** pCur->idx is set to the cell index that contains the pointer
3278** to the page we are coming from. If we are coming from the
3279** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00003280** the largest cell index.
drh72f82862001-05-24 21:06:34 +00003281*/
drh8178a752003-01-05 21:41:40 +00003282static void moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00003283 MemPage *pParent;
drh8178a752003-01-05 21:41:40 +00003284 MemPage *pPage;
drh428ae8c2003-01-04 16:48:09 +00003285 int idxParent;
drh3aac2dd2004-04-26 14:10:20 +00003286
danielk1977da184232006-01-05 11:34:32 +00003287 assert( pCur->eState==CURSOR_VALID );
drh8178a752003-01-05 21:41:40 +00003288 pPage = pCur->pPage;
3289 assert( pPage!=0 );
drh8856d6a2004-04-29 14:42:46 +00003290 assert( !isRootPage(pPage) );
drh8178a752003-01-05 21:41:40 +00003291 pParent = pPage->pParent;
3292 assert( pParent!=0 );
3293 idxParent = pPage->idxParent;
danielk19773b8a05f2007-03-19 17:44:26 +00003294 sqlite3PagerRef(pParent->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003295 releasePage(pPage);
drh72f82862001-05-24 21:06:34 +00003296 pCur->pPage = pParent;
drh271efa52004-05-30 19:19:05 +00003297 pCur->info.nSize = 0;
drh428ae8c2003-01-04 16:48:09 +00003298 assert( pParent->idxShift==0 );
drh43605152004-05-29 21:46:49 +00003299 pCur->idx = idxParent;
drh72f82862001-05-24 21:06:34 +00003300}
3301
3302/*
3303** Move the cursor to the root page
3304*/
drh5e2f8b92001-05-28 00:41:15 +00003305static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00003306 MemPage *pRoot;
drh777e4c42006-01-13 04:31:58 +00003307 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00003308 BtShared *pBt = pCur->pBtree->pBt;
drhbd03cae2001-06-02 02:40:57 +00003309
drhbf700f32007-03-31 02:36:44 +00003310 if( pCur->eState==CURSOR_REQUIRESEEK ){
3311 clearCursorPosition(pCur);
3312 }
drh777e4c42006-01-13 04:31:58 +00003313 pRoot = pCur->pPage;
danielk197797a227c2006-01-20 16:32:04 +00003314 if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
drh777e4c42006-01-13 04:31:58 +00003315 assert( pRoot->isInit );
3316 }else{
3317 if(
3318 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
3319 ){
3320 pCur->eState = CURSOR_INVALID;
3321 return rc;
3322 }
3323 releasePage(pCur->pPage);
drh777e4c42006-01-13 04:31:58 +00003324 pCur->pPage = pRoot;
drhc39e0002004-05-07 23:50:57 +00003325 }
drh72f82862001-05-24 21:06:34 +00003326 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003327 pCur->info.nSize = 0;
drh8856d6a2004-04-29 14:42:46 +00003328 if( pRoot->nCell==0 && !pRoot->leaf ){
3329 Pgno subpage;
3330 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00003331 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00003332 assert( subpage>0 );
danielk1977da184232006-01-05 11:34:32 +00003333 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:19 +00003334 rc = moveToChild(pCur, subpage);
drh8856d6a2004-04-29 14:42:46 +00003335 }
danielk1977da184232006-01-05 11:34:32 +00003336 pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
drh8856d6a2004-04-29 14:42:46 +00003337 return rc;
drh72f82862001-05-24 21:06:34 +00003338}
drh2af926b2001-05-15 00:39:25 +00003339
drh5e2f8b92001-05-28 00:41:15 +00003340/*
3341** Move the cursor down to the left-most leaf entry beneath the
3342** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:58 +00003343**
3344** The left-most leaf is the one with the smallest key - the first
3345** in ascending order.
drh5e2f8b92001-05-28 00:41:15 +00003346*/
3347static int moveToLeftmost(BtCursor *pCur){
3348 Pgno pgno;
3349 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003350 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00003351
danielk1977da184232006-01-05 11:34:32 +00003352 assert( pCur->eState==CURSOR_VALID );
drh3aac2dd2004-04-26 14:10:20 +00003353 while( !(pPage = pCur->pPage)->leaf ){
drha34b6762004-05-07 13:30:42 +00003354 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00003355 pgno = get4byte(findCell(pPage, pCur->idx));
drh8178a752003-01-05 21:41:40 +00003356 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00003357 if( rc ) return rc;
3358 }
3359 return SQLITE_OK;
3360}
3361
drh2dcc9aa2002-12-04 13:40:25 +00003362/*
3363** Move the cursor down to the right-most leaf entry beneath the
3364** page to which it is currently pointing. Notice the difference
3365** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3366** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3367** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:58 +00003368**
3369** The right-most entry is the one with the largest key - the last
3370** key in ascending order.
drh2dcc9aa2002-12-04 13:40:25 +00003371*/
3372static int moveToRightmost(BtCursor *pCur){
3373 Pgno pgno;
3374 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003375 MemPage *pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003376
danielk1977da184232006-01-05 11:34:32 +00003377 assert( pCur->eState==CURSOR_VALID );
drh3aac2dd2004-04-26 14:10:20 +00003378 while( !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:49 +00003379 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh3aac2dd2004-04-26 14:10:20 +00003380 pCur->idx = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00003381 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003382 if( rc ) return rc;
3383 }
drh3aac2dd2004-04-26 14:10:20 +00003384 pCur->idx = pPage->nCell - 1;
drh271efa52004-05-30 19:19:05 +00003385 pCur->info.nSize = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003386 return SQLITE_OK;
3387}
3388
drh5e00f6c2001-09-13 13:46:56 +00003389/* Move the cursor to the first entry in the table. Return SQLITE_OK
3390** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003391** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00003392*/
drh3aac2dd2004-04-26 14:10:20 +00003393int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00003394 int rc;
3395 rc = moveToRoot(pCur);
3396 if( rc ) return rc;
danielk1977da184232006-01-05 11:34:32 +00003397 if( pCur->eState==CURSOR_INVALID ){
drhc39e0002004-05-07 23:50:57 +00003398 assert( pCur->pPage->nCell==0 );
drh5e00f6c2001-09-13 13:46:56 +00003399 *pRes = 1;
3400 return SQLITE_OK;
3401 }
drhc39e0002004-05-07 23:50:57 +00003402 assert( pCur->pPage->nCell>0 );
drh5e00f6c2001-09-13 13:46:56 +00003403 *pRes = 0;
3404 rc = moveToLeftmost(pCur);
3405 return rc;
3406}
drh5e2f8b92001-05-28 00:41:15 +00003407
drh9562b552002-02-19 15:00:07 +00003408/* Move the cursor to the last entry in the table. Return SQLITE_OK
3409** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003410** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00003411*/
drh3aac2dd2004-04-26 14:10:20 +00003412int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00003413 int rc;
drh9562b552002-02-19 15:00:07 +00003414 rc = moveToRoot(pCur);
3415 if( rc ) return rc;
danielk1977da184232006-01-05 11:34:32 +00003416 if( CURSOR_INVALID==pCur->eState ){
drhc39e0002004-05-07 23:50:57 +00003417 assert( pCur->pPage->nCell==0 );
drh9562b552002-02-19 15:00:07 +00003418 *pRes = 1;
3419 return SQLITE_OK;
3420 }
danielk1977da184232006-01-05 11:34:32 +00003421 assert( pCur->eState==CURSOR_VALID );
drh9562b552002-02-19 15:00:07 +00003422 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003423 rc = moveToRightmost(pCur);
drh9562b552002-02-19 15:00:07 +00003424 return rc;
3425}
3426
drh3aac2dd2004-04-26 14:10:20 +00003427/* Move the cursor so that it points to an entry near pKey/nKey.
drh72f82862001-05-24 21:06:34 +00003428** Return a success code.
3429**
drh3aac2dd2004-04-26 14:10:20 +00003430** For INTKEY tables, only the nKey parameter is used. pKey is
3431** ignored. For other tables, nKey is the number of bytes of data
drh0b2f3162005-12-21 18:36:45 +00003432** in pKey. The comparison function specified when the cursor was
drh3aac2dd2004-04-26 14:10:20 +00003433** created is used to compare keys.
3434**
drh5e2f8b92001-05-28 00:41:15 +00003435** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00003436** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00003437** were present. The cursor might point to an entry that comes
3438** before or after the key.
3439**
drhbd03cae2001-06-02 02:40:57 +00003440** The result of comparing the key with the entry to which the
drhab01f612004-05-22 02:55:23 +00003441** cursor is written to *pRes if pRes!=NULL. The meaning of
drhbd03cae2001-06-02 02:40:57 +00003442** this value is as follows:
3443**
3444** *pRes<0 The cursor is left pointing at an entry that
drh1a844c32002-12-04 22:29:28 +00003445** is smaller than pKey or if the table is empty
3446** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00003447**
3448** *pRes==0 The cursor is left pointing at an entry that
3449** exactly matches pKey.
3450**
3451** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00003452** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00003453*/
drhe4d90812007-03-29 05:51:49 +00003454int sqlite3BtreeMoveto(
3455 BtCursor *pCur, /* The cursor to be moved */
3456 const void *pKey, /* The key content for indices. Not used by tables */
3457 i64 nKey, /* Size of pKey. Or the key for tables */
3458 int biasRight, /* If true, bias the search to the high end */
3459 int *pRes /* Search result flag */
3460){
drh72f82862001-05-24 21:06:34 +00003461 int rc;
drh5e2f8b92001-05-28 00:41:15 +00003462 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00003463 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00003464 assert( pCur->pPage );
3465 assert( pCur->pPage->isInit );
danielk1977da184232006-01-05 11:34:32 +00003466 if( pCur->eState==CURSOR_INVALID ){
drhf328bc82004-05-10 23:29:49 +00003467 *pRes = -1;
drhc39e0002004-05-07 23:50:57 +00003468 assert( pCur->pPage->nCell==0 );
3469 return SQLITE_OK;
3470 }
drh14684382006-11-30 13:05:29 +00003471 for(;;){
drh72f82862001-05-24 21:06:34 +00003472 int lwr, upr;
3473 Pgno chldPg;
3474 MemPage *pPage = pCur->pPage;
drh1a844c32002-12-04 22:29:28 +00003475 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00003476 lwr = 0;
3477 upr = pPage->nCell-1;
drh4eec4c12005-01-21 00:22:37 +00003478 if( !pPage->intKey && pKey==0 ){
drh49285702005-09-17 15:20:26 +00003479 return SQLITE_CORRUPT_BKPT;
drh4eec4c12005-01-21 00:22:37 +00003480 }
drhe4d90812007-03-29 05:51:49 +00003481 if( biasRight ){
3482 pCur->idx = upr;
3483 }else{
3484 pCur->idx = (upr+lwr)/2;
3485 }
drhf1d68b32007-03-29 04:43:26 +00003486 if( lwr<=upr ) for(;;){
danielk197713adf8a2004-06-03 16:08:41 +00003487 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00003488 i64 nCellKey;
drh366fda62006-01-13 02:35:09 +00003489 pCur->info.nSize = 0;
drh3aac2dd2004-04-26 14:10:20 +00003490 if( pPage->intKey ){
drh777e4c42006-01-13 04:31:58 +00003491 u8 *pCell;
drh777e4c42006-01-13 04:31:58 +00003492 pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
drhd172f862006-01-12 15:01:15 +00003493 if( pPage->hasData ){
danielk1977bab45c62006-01-16 15:14:27 +00003494 u32 dummy;
drhd172f862006-01-12 15:01:15 +00003495 pCell += getVarint32(pCell, &dummy);
3496 }
danielk1977bab45c62006-01-16 15:14:27 +00003497 getVarint(pCell, (u64 *)&nCellKey);
drh3aac2dd2004-04-26 14:10:20 +00003498 if( nCellKey<nKey ){
3499 c = -1;
3500 }else if( nCellKey>nKey ){
3501 c = +1;
3502 }else{
3503 c = 0;
3504 }
drh3aac2dd2004-04-26 14:10:20 +00003505 }else{
drhe51c44f2004-05-30 20:46:09 +00003506 int available;
danielk197713adf8a2004-06-03 16:08:41 +00003507 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drh366fda62006-01-13 02:35:09 +00003508 nCellKey = pCur->info.nKey;
drhe51c44f2004-05-30 20:46:09 +00003509 if( available>=nCellKey ){
3510 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3511 }else{
3512 pCellKey = sqliteMallocRaw( nCellKey );
3513 if( pCellKey==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00003514 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
drhe51c44f2004-05-30 20:46:09 +00003515 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
3516 sqliteFree(pCellKey);
3517 if( rc ) return rc;
3518 }
drh3aac2dd2004-04-26 14:10:20 +00003519 }
drh72f82862001-05-24 21:06:34 +00003520 if( c==0 ){
drh8b18dd42004-05-12 19:18:15 +00003521 if( pPage->leafData && !pPage->leaf ){
drhfc70e6f2004-05-12 21:11:27 +00003522 lwr = pCur->idx;
3523 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00003524 break;
3525 }else{
drh8b18dd42004-05-12 19:18:15 +00003526 if( pRes ) *pRes = 0;
3527 return SQLITE_OK;
3528 }
drh72f82862001-05-24 21:06:34 +00003529 }
3530 if( c<0 ){
3531 lwr = pCur->idx+1;
3532 }else{
3533 upr = pCur->idx-1;
3534 }
drhf1d68b32007-03-29 04:43:26 +00003535 if( lwr>upr ){
3536 break;
3537 }
3538 pCur->idx = (lwr+upr)/2;
drh72f82862001-05-24 21:06:34 +00003539 }
3540 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00003541 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00003542 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00003543 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00003544 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00003545 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00003546 }else{
drh43605152004-05-29 21:46:49 +00003547 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00003548 }
3549 if( chldPg==0 ){
drhc39e0002004-05-07 23:50:57 +00003550 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00003551 if( pRes ) *pRes = c;
3552 return SQLITE_OK;
3553 }
drh428ae8c2003-01-04 16:48:09 +00003554 pCur->idx = lwr;
drh271efa52004-05-30 19:19:05 +00003555 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00003556 rc = moveToChild(pCur, chldPg);
drhc39e0002004-05-07 23:50:57 +00003557 if( rc ){
3558 return rc;
3559 }
drh72f82862001-05-24 21:06:34 +00003560 }
drhbd03cae2001-06-02 02:40:57 +00003561 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00003562}
3563
3564/*
drhc39e0002004-05-07 23:50:57 +00003565** Return TRUE if the cursor is not pointing at an entry of the table.
3566**
3567** TRUE will be returned after a call to sqlite3BtreeNext() moves
3568** past the last entry in the table or sqlite3BtreePrev() moves past
3569** the first entry. TRUE is also returned if the table is empty.
3570*/
3571int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00003572 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
3573 ** have been deleted? This API will need to change to return an error code
3574 ** as well as the boolean result value.
3575 */
3576 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:57 +00003577}
3578
3579/*
drhbd03cae2001-06-02 02:40:57 +00003580** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00003581** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00003582** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00003583** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00003584*/
drh3aac2dd2004-04-26 14:10:20 +00003585int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00003586 int rc;
danielk197797a227c2006-01-20 16:32:04 +00003587 MemPage *pPage;
drh8b18dd42004-05-12 19:18:15 +00003588
drhbf700f32007-03-31 02:36:44 +00003589 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003590 if( rc!=SQLITE_OK ){
3591 return rc;
3592 }
drh8c4d3a62007-04-06 01:03:32 +00003593 assert( pRes!=0 );
3594 pPage = pCur->pPage;
3595 if( CURSOR_INVALID==pCur->eState ){
3596 *pRes = 1;
3597 return SQLITE_OK;
3598 }
danielk1977da184232006-01-05 11:34:32 +00003599 if( pCur->skip>0 ){
3600 pCur->skip = 0;
3601 *pRes = 0;
3602 return SQLITE_OK;
3603 }
3604 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00003605
drh8178a752003-01-05 21:41:40 +00003606 assert( pPage->isInit );
drh8178a752003-01-05 21:41:40 +00003607 assert( pCur->idx<pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00003608
drh72f82862001-05-24 21:06:34 +00003609 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00003610 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00003611 if( pCur->idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00003612 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003613 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00003614 if( rc ) return rc;
3615 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003616 *pRes = 0;
3617 return rc;
drh72f82862001-05-24 21:06:34 +00003618 }
drh5e2f8b92001-05-28 00:41:15 +00003619 do{
drh8856d6a2004-04-29 14:42:46 +00003620 if( isRootPage(pPage) ){
drh8c1238a2003-01-02 14:43:55 +00003621 *pRes = 1;
danielk1977da184232006-01-05 11:34:32 +00003622 pCur->eState = CURSOR_INVALID;
drh5e2f8b92001-05-28 00:41:15 +00003623 return SQLITE_OK;
3624 }
drh8178a752003-01-05 21:41:40 +00003625 moveToParent(pCur);
3626 pPage = pCur->pPage;
3627 }while( pCur->idx>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00003628 *pRes = 0;
drh8b18dd42004-05-12 19:18:15 +00003629 if( pPage->leafData ){
3630 rc = sqlite3BtreeNext(pCur, pRes);
3631 }else{
3632 rc = SQLITE_OK;
3633 }
3634 return rc;
drh8178a752003-01-05 21:41:40 +00003635 }
3636 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00003637 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00003638 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00003639 }
drh5e2f8b92001-05-28 00:41:15 +00003640 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003641 return rc;
drh72f82862001-05-24 21:06:34 +00003642}
3643
drh3b7511c2001-05-26 13:15:44 +00003644/*
drh2dcc9aa2002-12-04 13:40:25 +00003645** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00003646** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00003647** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00003648** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00003649*/
drh3aac2dd2004-04-26 14:10:20 +00003650int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00003651 int rc;
3652 Pgno pgno;
drh8178a752003-01-05 21:41:40 +00003653 MemPage *pPage;
danielk1977da184232006-01-05 11:34:32 +00003654
drhbf700f32007-03-31 02:36:44 +00003655 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003656 if( rc!=SQLITE_OK ){
3657 return rc;
3658 }
drh8c4d3a62007-04-06 01:03:32 +00003659 if( CURSOR_INVALID==pCur->eState ){
3660 *pRes = 1;
3661 return SQLITE_OK;
3662 }
danielk1977da184232006-01-05 11:34:32 +00003663 if( pCur->skip<0 ){
3664 pCur->skip = 0;
3665 *pRes = 0;
3666 return SQLITE_OK;
3667 }
3668 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00003669
drh8178a752003-01-05 21:41:40 +00003670 pPage = pCur->pPage;
drh8178a752003-01-05 21:41:40 +00003671 assert( pPage->isInit );
drh2dcc9aa2002-12-04 13:40:25 +00003672 assert( pCur->idx>=0 );
drha34b6762004-05-07 13:30:42 +00003673 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003674 pgno = get4byte( findCell(pPage, pCur->idx) );
drh8178a752003-01-05 21:41:40 +00003675 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003676 if( rc ) return rc;
3677 rc = moveToRightmost(pCur);
3678 }else{
3679 while( pCur->idx==0 ){
drh8856d6a2004-04-29 14:42:46 +00003680 if( isRootPage(pPage) ){
danielk1977da184232006-01-05 11:34:32 +00003681 pCur->eState = CURSOR_INVALID;
drhc39e0002004-05-07 23:50:57 +00003682 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00003683 return SQLITE_OK;
3684 }
drh8178a752003-01-05 21:41:40 +00003685 moveToParent(pCur);
3686 pPage = pCur->pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003687 }
3688 pCur->idx--;
drh271efa52004-05-30 19:19:05 +00003689 pCur->info.nSize = 0;
drh8237d452004-11-22 19:07:09 +00003690 if( pPage->leafData && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00003691 rc = sqlite3BtreePrevious(pCur, pRes);
3692 }else{
3693 rc = SQLITE_OK;
3694 }
drh2dcc9aa2002-12-04 13:40:25 +00003695 }
drh8178a752003-01-05 21:41:40 +00003696 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003697 return rc;
3698}
3699
3700/*
drh3b7511c2001-05-26 13:15:44 +00003701** Allocate a new page from the database file.
3702**
danielk19773b8a05f2007-03-19 17:44:26 +00003703** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:44 +00003704** has already been called on the new page.) The new page has also
3705** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:26 +00003706** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00003707**
3708** SQLITE_OK is returned on success. Any other return value indicates
3709** an error. *ppPage and *pPgno are undefined in the event of an error.
danielk19773b8a05f2007-03-19 17:44:26 +00003710** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00003711**
drh199e3cf2002-07-18 11:01:47 +00003712** If the "nearby" parameter is not 0, then a (feeble) effort is made to
3713** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00003714** attempt to keep related pages close to each other in the database file,
3715** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00003716**
3717** If the "exact" parameter is not 0, and the page-number nearby exists
3718** anywhere on the free-list, then it is guarenteed to be returned. This
3719** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00003720*/
drh4f0c5872007-03-26 22:05:01 +00003721static int allocateBtreePage(
danielk1977aef0bf62005-12-30 16:28:01 +00003722 BtShared *pBt,
danielk1977cb1a7eb2004-11-05 12:27:02 +00003723 MemPage **ppPage,
3724 Pgno *pPgno,
3725 Pgno nearby,
3726 u8 exact
3727){
drh3aac2dd2004-04-26 14:10:20 +00003728 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00003729 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003730 int n; /* Number of pages on the freelist */
3731 int k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:51 +00003732 MemPage *pTrunk = 0;
3733 MemPage *pPrevTrunk = 0;
drh30e58752002-03-02 20:41:57 +00003734
drh3aac2dd2004-04-26 14:10:20 +00003735 pPage1 = pBt->pPage1;
3736 n = get4byte(&pPage1->aData[36]);
3737 if( n>0 ){
drh91025292004-05-03 19:49:32 +00003738 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00003739 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003740 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
3741
3742 /* If the 'exact' parameter was true and a query of the pointer-map
3743 ** shows that the page 'nearby' is somewhere on the free-list, then
3744 ** the entire-list will be searched for that page.
3745 */
3746#ifndef SQLITE_OMIT_AUTOVACUUM
3747 if( exact ){
3748 u8 eType;
3749 assert( nearby>0 );
3750 assert( pBt->autoVacuum );
3751 rc = ptrmapGet(pBt, nearby, &eType, 0);
3752 if( rc ) return rc;
3753 if( eType==PTRMAP_FREEPAGE ){
3754 searchList = 1;
3755 }
3756 *pPgno = nearby;
3757 }
3758#endif
3759
3760 /* Decrement the free-list count by 1. Set iTrunk to the index of the
3761 ** first free-list trunk page. iPrevTrunk is initially 1.
3762 */
danielk19773b8a05f2007-03-19 17:44:26 +00003763 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:44 +00003764 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003765 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003766
3767 /* The code within this loop is run only once if the 'searchList' variable
3768 ** is not true. Otherwise, it runs once for each trunk-page on the
3769 ** free-list until the page 'nearby' is located.
3770 */
3771 do {
3772 pPrevTrunk = pTrunk;
3773 if( pPrevTrunk ){
3774 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00003775 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00003776 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00003777 }
drh0787db62007-03-04 13:15:27 +00003778 rc = getPage(pBt, iTrunk, &pTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003779 if( rc ){
drhd3627af2006-12-18 18:34:51 +00003780 pTrunk = 0;
3781 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003782 }
3783
3784 k = get4byte(&pTrunk->aData[4]);
3785 if( k==0 && !searchList ){
3786 /* The trunk has no leaves and the list is not being searched.
3787 ** So extract the trunk page itself and use it as the newly
3788 ** allocated page */
3789 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003790 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003791 if( rc ){
3792 goto end_allocate_page;
3793 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003794 *pPgno = iTrunk;
3795 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3796 *ppPage = pTrunk;
3797 pTrunk = 0;
3798 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3799 }else if( k>pBt->usableSize/4 - 8 ){
3800 /* Value of k is out of range. Database corruption */
drhd3627af2006-12-18 18:34:51 +00003801 rc = SQLITE_CORRUPT_BKPT;
3802 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003803#ifndef SQLITE_OMIT_AUTOVACUUM
3804 }else if( searchList && nearby==iTrunk ){
3805 /* The list is being searched and this trunk page is the page
3806 ** to allocate, regardless of whether it has leaves.
3807 */
3808 assert( *pPgno==iTrunk );
3809 *ppPage = pTrunk;
3810 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003811 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003812 if( rc ){
3813 goto end_allocate_page;
3814 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003815 if( k==0 ){
3816 if( !pPrevTrunk ){
3817 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3818 }else{
3819 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
3820 }
3821 }else{
3822 /* The trunk page is required by the caller but it contains
3823 ** pointers to free-list leaves. The first leaf becomes a trunk
3824 ** page in this case.
3825 */
3826 MemPage *pNewTrunk;
3827 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
drh0787db62007-03-04 13:15:27 +00003828 rc = getPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003829 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:51 +00003830 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003831 }
danielk19773b8a05f2007-03-19 17:44:26 +00003832 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003833 if( rc!=SQLITE_OK ){
3834 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:51 +00003835 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003836 }
3837 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
3838 put4byte(&pNewTrunk->aData[4], k-1);
3839 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:51 +00003840 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003841 if( !pPrevTrunk ){
3842 put4byte(&pPage1->aData[32], iNewTrunk);
3843 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00003844 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003845 if( rc ){
3846 goto end_allocate_page;
3847 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003848 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
3849 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003850 }
3851 pTrunk = 0;
3852 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3853#endif
3854 }else{
3855 /* Extract a leaf from the trunk */
3856 int closest;
3857 Pgno iPage;
3858 unsigned char *aData = pTrunk->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00003859 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00003860 if( rc ){
3861 goto end_allocate_page;
3862 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003863 if( nearby>0 ){
3864 int i, dist;
3865 closest = 0;
3866 dist = get4byte(&aData[8]) - nearby;
3867 if( dist<0 ) dist = -dist;
3868 for(i=1; i<k; i++){
3869 int d2 = get4byte(&aData[8+i*4]) - nearby;
3870 if( d2<0 ) d2 = -d2;
3871 if( d2<dist ){
3872 closest = i;
3873 dist = d2;
3874 }
3875 }
3876 }else{
3877 closest = 0;
3878 }
3879
3880 iPage = get4byte(&aData[8+closest*4]);
3881 if( !searchList || iPage==nearby ){
3882 *pPgno = iPage;
danielk19773b8a05f2007-03-19 17:44:26 +00003883 if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00003884 /* Free page off the end of the file */
drh49285702005-09-17 15:20:26 +00003885 return SQLITE_CORRUPT_BKPT;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003886 }
3887 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
3888 ": %d more free pages\n",
3889 *pPgno, closest+1, k, pTrunk->pgno, n-1));
3890 if( closest<k-1 ){
3891 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
3892 }
3893 put4byte(&aData[4], k-1);
drh0787db62007-03-04 13:15:27 +00003894 rc = getPage(pBt, *pPgno, ppPage, 1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003895 if( rc==SQLITE_OK ){
drh538f5702007-04-13 02:14:30 +00003896 sqlite3PagerDontRollback((*ppPage)->pDbPage);
danielk19773b8a05f2007-03-19 17:44:26 +00003897 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00003898 if( rc!=SQLITE_OK ){
3899 releasePage(*ppPage);
3900 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003901 }
3902 searchList = 0;
3903 }
drhee696e22004-08-30 16:52:17 +00003904 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003905 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:51 +00003906 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003907 }while( searchList );
drh3b7511c2001-05-26 13:15:44 +00003908 }else{
drh3aac2dd2004-04-26 14:10:20 +00003909 /* There are no pages on the freelist, so create a new page at the
3910 ** end of the file */
danielk19773b8a05f2007-03-19 17:44:26 +00003911 *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1;
danielk1977afcdd022004-10-31 16:25:42 +00003912
3913#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00003914 if( pBt->nTrunc ){
3915 /* An incr-vacuum has already run within this transaction. So the
3916 ** page to allocate is not from the physical end of the file, but
3917 ** at pBt->nTrunc.
3918 */
3919 *pPgno = pBt->nTrunc+1;
3920 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
3921 (*pPgno)++;
3922 }
3923 }
danielk1977266664d2006-02-10 08:24:21 +00003924 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00003925 /* If *pPgno refers to a pointer-map page, allocate two new pages
3926 ** at the end of the file instead of one. The first allocated page
3927 ** becomes a new pointer-map page, the second is used by the caller.
3928 */
3929 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00003930 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00003931 (*pPgno)++;
3932 }
danielk1977dddbcdc2007-04-26 14:42:34 +00003933 if( pBt->nTrunc ){
3934 pBt->nTrunc = *pPgno;
3935 }
danielk1977afcdd022004-10-31 16:25:42 +00003936#endif
3937
danielk1977599fcba2004-11-08 07:13:13 +00003938 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh0787db62007-03-04 13:15:27 +00003939 rc = getPage(pBt, *pPgno, ppPage, 0);
drh3b7511c2001-05-26 13:15:44 +00003940 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00003941 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00003942 if( rc!=SQLITE_OK ){
3943 releasePage(*ppPage);
3944 }
drh3a4c1412004-05-09 20:40:11 +00003945 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00003946 }
danielk1977599fcba2004-11-08 07:13:13 +00003947
3948 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:51 +00003949
3950end_allocate_page:
3951 releasePage(pTrunk);
3952 releasePage(pPrevTrunk);
drh3b7511c2001-05-26 13:15:44 +00003953 return rc;
3954}
3955
3956/*
drh3aac2dd2004-04-26 14:10:20 +00003957** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00003958**
danielk19773b8a05f2007-03-19 17:44:26 +00003959** sqlite3PagerUnref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00003960*/
drh3aac2dd2004-04-26 14:10:20 +00003961static int freePage(MemPage *pPage){
danielk1977aef0bf62005-12-30 16:28:01 +00003962 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00003963 MemPage *pPage1 = pBt->pPage1;
3964 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00003965
drh3aac2dd2004-04-26 14:10:20 +00003966 /* Prepare the page for freeing */
3967 assert( pPage->pgno>1 );
3968 pPage->isInit = 0;
3969 releasePage(pPage->pParent);
3970 pPage->pParent = 0;
3971
drha34b6762004-05-07 13:30:42 +00003972 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:26 +00003973 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003974 if( rc ) return rc;
3975 n = get4byte(&pPage1->aData[36]);
3976 put4byte(&pPage1->aData[36], n+1);
3977
drhfcce93f2006-02-22 03:08:32 +00003978#ifdef SQLITE_SECURE_DELETE
3979 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
3980 ** always fully overwrite deleted information with zeros.
3981 */
danielk19773b8a05f2007-03-19 17:44:26 +00003982 rc = sqlite3PagerWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00003983 if( rc ) return rc;
3984 memset(pPage->aData, 0, pPage->pBt->pageSize);
3985#endif
3986
danielk1977687566d2004-11-02 12:56:41 +00003987#ifndef SQLITE_OMIT_AUTOVACUUM
3988 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00003989 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00003990 */
3991 if( pBt->autoVacuum ){
3992 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
danielk1977a64a0352004-11-05 01:45:13 +00003993 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00003994 }
3995#endif
3996
drh3aac2dd2004-04-26 14:10:20 +00003997 if( n==0 ){
3998 /* This is the first free page */
danielk19773b8a05f2007-03-19 17:44:26 +00003999 rc = sqlite3PagerWrite(pPage->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004000 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004001 memset(pPage->aData, 0, 8);
drha34b6762004-05-07 13:30:42 +00004002 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00004003 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004004 }else{
4005 /* Other free pages already exist. Retrive the first trunk page
4006 ** of the freelist and find out how many leaves it has. */
drha34b6762004-05-07 13:30:42 +00004007 MemPage *pTrunk;
drh0787db62007-03-04 13:15:27 +00004008 rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
drh3b7511c2001-05-26 13:15:44 +00004009 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004010 k = get4byte(&pTrunk->aData[4]);
drhee696e22004-08-30 16:52:17 +00004011 if( k>=pBt->usableSize/4 - 8 ){
drh3aac2dd2004-04-26 14:10:20 +00004012 /* The trunk is full. Turn the page being freed into a new
4013 ** trunk page with no leaves. */
danielk19773b8a05f2007-03-19 17:44:26 +00004014 rc = sqlite3PagerWrite(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00004015 if( rc ) return rc;
4016 put4byte(pPage->aData, pTrunk->pgno);
4017 put4byte(&pPage->aData[4], 0);
4018 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00004019 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
4020 pPage->pgno, pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004021 }else{
4022 /* Add the newly freed page as a leaf on the current trunk */
danielk19773b8a05f2007-03-19 17:44:26 +00004023 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhf5345442007-04-09 12:45:02 +00004024 if( rc==SQLITE_OK ){
4025 put4byte(&pTrunk->aData[4], k+1);
4026 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drhfcce93f2006-02-22 03:08:32 +00004027#ifndef SQLITE_SECURE_DELETE
drh538f5702007-04-13 02:14:30 +00004028 sqlite3PagerDontWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00004029#endif
drhf5345442007-04-09 12:45:02 +00004030 }
drh3a4c1412004-05-09 20:40:11 +00004031 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004032 }
4033 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00004034 }
drh3b7511c2001-05-26 13:15:44 +00004035 return rc;
4036}
4037
4038/*
drh3aac2dd2004-04-26 14:10:20 +00004039** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00004040*/
drh3aac2dd2004-04-26 14:10:20 +00004041static int clearCell(MemPage *pPage, unsigned char *pCell){
danielk1977aef0bf62005-12-30 16:28:01 +00004042 BtShared *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00004043 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00004044 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00004045 int rc;
drh94440812007-03-06 11:42:19 +00004046 int nOvfl;
4047 int ovflPageSize;
drh3b7511c2001-05-26 13:15:44 +00004048
drh43605152004-05-29 21:46:49 +00004049 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004050 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00004051 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00004052 }
drh6f11bef2004-05-13 01:12:56 +00004053 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh94440812007-03-06 11:42:19 +00004054 ovflPageSize = pBt->usableSize - 4;
drh72365832007-03-06 15:53:44 +00004055 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
4056 assert( ovflPgno==0 || nOvfl>0 );
4057 while( nOvfl-- ){
drh3aac2dd2004-04-26 14:10:20 +00004058 MemPage *pOvfl;
danielk19773b8a05f2007-03-19 17:44:26 +00004059 if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00004060 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00004061 }
drh538f5702007-04-13 02:14:30 +00004062 rc = getPage(pBt, ovflPgno, &pOvfl, nOvfl==0);
drh3b7511c2001-05-26 13:15:44 +00004063 if( rc ) return rc;
drh72365832007-03-06 15:53:44 +00004064 if( nOvfl ){
4065 ovflPgno = get4byte(pOvfl->aData);
4066 }
drha34b6762004-05-07 13:30:42 +00004067 rc = freePage(pOvfl);
danielk19773b8a05f2007-03-19 17:44:26 +00004068 sqlite3PagerUnref(pOvfl->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00004069 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00004070 }
drh5e2f8b92001-05-28 00:41:15 +00004071 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00004072}
4073
4074/*
drh91025292004-05-03 19:49:32 +00004075** Create the byte sequence used to represent a cell on page pPage
4076** and write that byte sequence into pCell[]. Overflow pages are
4077** allocated and filled in as necessary. The calling procedure
4078** is responsible for making sure sufficient space has been allocated
4079** for pCell[].
4080**
4081** Note that pCell does not necessary need to point to the pPage->aData
4082** area. pCell might point to some temporary storage. The cell will
4083** be constructed in this temporary area then copied into pPage->aData
4084** later.
drh3b7511c2001-05-26 13:15:44 +00004085*/
4086static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00004087 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00004088 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00004089 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00004090 const void *pData,int nData, /* The data */
4091 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00004092){
drh3b7511c2001-05-26 13:15:44 +00004093 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00004094 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00004095 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00004096 int spaceLeft;
4097 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00004098 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00004099 unsigned char *pPrior;
4100 unsigned char *pPayload;
danielk1977aef0bf62005-12-30 16:28:01 +00004101 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00004102 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00004103 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00004104 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00004105
drh91025292004-05-03 19:49:32 +00004106 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00004107 nHeader = 0;
drh91025292004-05-03 19:49:32 +00004108 if( !pPage->leaf ){
4109 nHeader += 4;
4110 }
drh8b18dd42004-05-12 19:18:15 +00004111 if( pPage->hasData ){
drh91025292004-05-03 19:49:32 +00004112 nHeader += putVarint(&pCell[nHeader], nData);
drh6f11bef2004-05-13 01:12:56 +00004113 }else{
drh91025292004-05-03 19:49:32 +00004114 nData = 0;
4115 }
drh6f11bef2004-05-13 01:12:56 +00004116 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh43605152004-05-29 21:46:49 +00004117 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004118 assert( info.nHeader==nHeader );
4119 assert( info.nKey==nKey );
4120 assert( info.nData==nData );
4121
4122 /* Fill in the payload */
drh3aac2dd2004-04-26 14:10:20 +00004123 nPayload = nData;
4124 if( pPage->intKey ){
4125 pSrc = pData;
4126 nSrc = nData;
drh91025292004-05-03 19:49:32 +00004127 nData = 0;
drh3aac2dd2004-04-26 14:10:20 +00004128 }else{
4129 nPayload += nKey;
4130 pSrc = pKey;
4131 nSrc = nKey;
4132 }
drh6f11bef2004-05-13 01:12:56 +00004133 *pnSize = info.nSize;
4134 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00004135 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00004136 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00004137
drh3b7511c2001-05-26 13:15:44 +00004138 while( nPayload>0 ){
4139 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00004140#ifndef SQLITE_OMIT_AUTOVACUUM
4141 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
4142#endif
drh4f0c5872007-03-26 22:05:01 +00004143 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00004144#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00004145 /* If the database supports auto-vacuum, and the second or subsequent
4146 ** overflow page is being allocated, add an entry to the pointer-map
4147 ** for that page now. The entry for the first overflow page will be
4148 ** added later, by the insertCell() routine.
danielk1977afcdd022004-10-31 16:25:42 +00004149 */
danielk1977a19df672004-11-03 11:37:07 +00004150 if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
4151 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap);
danielk1977afcdd022004-10-31 16:25:42 +00004152 }
4153#endif
drh3b7511c2001-05-26 13:15:44 +00004154 if( rc ){
drh9b171272004-05-08 02:03:22 +00004155 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004156 return rc;
4157 }
drh3aac2dd2004-04-26 14:10:20 +00004158 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00004159 releasePage(pToRelease);
4160 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00004161 pPrior = pOvfl->aData;
4162 put4byte(pPrior, 0);
4163 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00004164 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00004165 }
4166 n = nPayload;
4167 if( n>spaceLeft ) n = spaceLeft;
drh3aac2dd2004-04-26 14:10:20 +00004168 if( n>nSrc ) n = nSrc;
drhff3b1702006-03-11 12:04:18 +00004169 assert( pSrc );
drh3aac2dd2004-04-26 14:10:20 +00004170 memcpy(pPayload, pSrc, n);
drh3b7511c2001-05-26 13:15:44 +00004171 nPayload -= n;
drhde647132004-05-07 17:57:49 +00004172 pPayload += n;
drh9b171272004-05-08 02:03:22 +00004173 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00004174 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00004175 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00004176 if( nSrc==0 ){
4177 nSrc = nData;
4178 pSrc = pData;
4179 }
drhdd793422001-06-28 01:54:48 +00004180 }
drh9b171272004-05-08 02:03:22 +00004181 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004182 return SQLITE_OK;
4183}
4184
4185/*
drhbd03cae2001-06-02 02:40:57 +00004186** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00004187** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00004188** pointer in the third argument.
4189*/
danielk1977aef0bf62005-12-30 16:28:01 +00004190static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
drhbd03cae2001-06-02 02:40:57 +00004191 MemPage *pThis;
danielk19773b8a05f2007-03-19 17:44:26 +00004192 DbPage *pDbPage;
drhbd03cae2001-06-02 02:40:57 +00004193
drh43617e92006-03-06 20:55:46 +00004194 assert( pNewParent!=0 );
danielk1977afcdd022004-10-31 16:25:42 +00004195 if( pgno==0 ) return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004196 assert( pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004197 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
4198 if( pDbPage ){
4199 pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
drhda200cc2004-05-09 11:51:38 +00004200 if( pThis->isInit ){
danielk19773b8a05f2007-03-19 17:44:26 +00004201 assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) );
drhda200cc2004-05-09 11:51:38 +00004202 if( pThis->pParent!=pNewParent ){
danielk19773b8a05f2007-03-19 17:44:26 +00004203 if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004204 pThis->pParent = pNewParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004205 sqlite3PagerRef(pNewParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004206 }
4207 pThis->idxParent = idx;
drhdd793422001-06-28 01:54:48 +00004208 }
danielk19773b8a05f2007-03-19 17:44:26 +00004209 sqlite3PagerUnref(pDbPage);
drhbd03cae2001-06-02 02:40:57 +00004210 }
danielk1977afcdd022004-10-31 16:25:42 +00004211
4212#ifndef SQLITE_OMIT_AUTOVACUUM
4213 if( pBt->autoVacuum ){
4214 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
4215 }
4216#endif
4217 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00004218}
4219
danielk1977ac11ee62005-01-15 12:45:51 +00004220
4221
drhbd03cae2001-06-02 02:40:57 +00004222/*
drh4b70f112004-05-02 21:12:19 +00004223** Change the pParent pointer of all children of pPage to point back
4224** to pPage.
4225**
drhbd03cae2001-06-02 02:40:57 +00004226** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00004227** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00004228**
4229** This routine gets called after you memcpy() one page into
4230** another.
4231*/
danielk1977afcdd022004-10-31 16:25:42 +00004232static int reparentChildPages(MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00004233 int i;
danielk1977aef0bf62005-12-30 16:28:01 +00004234 BtShared *pBt = pPage->pBt;
danielk1977afcdd022004-10-31 16:25:42 +00004235 int rc = SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004236
danielk1977afcdd022004-10-31 16:25:42 +00004237 if( pPage->leaf ) return SQLITE_OK;
danielk1977afcdd022004-10-31 16:25:42 +00004238
drhbd03cae2001-06-02 02:40:57 +00004239 for(i=0; i<pPage->nCell; i++){
danielk1977afcdd022004-10-31 16:25:42 +00004240 u8 *pCell = findCell(pPage, i);
4241 if( !pPage->leaf ){
4242 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
4243 if( rc!=SQLITE_OK ) return rc;
4244 }
drhbd03cae2001-06-02 02:40:57 +00004245 }
danielk1977afcdd022004-10-31 16:25:42 +00004246 if( !pPage->leaf ){
4247 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
4248 pPage, i);
4249 pPage->idxShift = 0;
4250 }
4251 return rc;
drh14acc042001-06-10 19:56:58 +00004252}
4253
4254/*
4255** Remove the i-th cell from pPage. This routine effects pPage only.
4256** The cell content is not freed or deallocated. It is assumed that
4257** the cell content has been copied someplace else. This routine just
4258** removes the reference to the cell from pPage.
4259**
4260** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00004261*/
drh4b70f112004-05-02 21:12:19 +00004262static void dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00004263 int i; /* Loop counter */
4264 int pc; /* Offset to cell content of cell being deleted */
4265 u8 *data; /* pPage->aData */
4266 u8 *ptr; /* Used to move bytes around within data[] */
4267
drh8c42ca92001-06-22 19:15:00 +00004268 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004269 assert( sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:26 +00004270 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drhda200cc2004-05-09 11:51:38 +00004271 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00004272 ptr = &data[pPage->cellOffset + 2*idx];
4273 pc = get2byte(ptr);
4274 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
drhde647132004-05-07 17:57:49 +00004275 freeSpace(pPage, pc, sz);
drh43605152004-05-29 21:46:49 +00004276 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4277 ptr[0] = ptr[2];
4278 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00004279 }
4280 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00004281 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4282 pPage->nFree += 2;
drh428ae8c2003-01-04 16:48:09 +00004283 pPage->idxShift = 1;
drh14acc042001-06-10 19:56:58 +00004284}
4285
4286/*
4287** Insert a new cell on pPage at cell index "i". pCell points to the
4288** content of the cell.
4289**
4290** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00004291** will not fit, then make a copy of the cell content into pTemp if
4292** pTemp is not null. Regardless of pTemp, allocate a new entry
4293** in pPage->aOvfl[] and make it point to the cell content (either
4294** in pTemp or the original pCell) and also record its index.
4295** Allocating a new entry in pPage->aCell[] implies that
4296** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00004297**
4298** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4299** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00004300** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00004301** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00004302*/
danielk1977e80463b2004-11-03 03:01:16 +00004303static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00004304 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00004305 int i, /* New cell becomes the i-th cell of the page */
4306 u8 *pCell, /* Content of the new cell */
4307 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00004308 u8 *pTemp, /* Temp storage space for pCell, if needed */
4309 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00004310){
drh43605152004-05-29 21:46:49 +00004311 int idx; /* Where to write new cell content in data[] */
4312 int j; /* Loop counter */
4313 int top; /* First byte of content for any cell in data[] */
4314 int end; /* First byte past the last cell pointer in data[] */
4315 int ins; /* Index in data[] where new cell pointer is inserted */
4316 int hdr; /* Offset into data[] of the page header */
4317 int cellOffset; /* Address of first cell pointer in data[] */
4318 u8 *data; /* The content of the whole page */
4319 u8 *ptr; /* Used for moving information around in data[] */
4320
4321 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
4322 assert( sz==cellSizePtr(pPage, pCell) );
danielk19773b8a05f2007-03-19 17:44:26 +00004323 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004324 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00004325 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00004326 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004327 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00004328 }
drh43605152004-05-29 21:46:49 +00004329 j = pPage->nOverflow++;
4330 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
4331 pPage->aOvfl[j].pCell = pCell;
4332 pPage->aOvfl[j].idx = i;
4333 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00004334 }else{
drh43605152004-05-29 21:46:49 +00004335 data = pPage->aData;
4336 hdr = pPage->hdrOffset;
4337 top = get2byte(&data[hdr+5]);
4338 cellOffset = pPage->cellOffset;
4339 end = cellOffset + 2*pPage->nCell + 2;
4340 ins = cellOffset + 2*i;
4341 if( end > top - sz ){
danielk19776b456a22005-03-21 04:04:02 +00004342 int rc = defragmentPage(pPage);
4343 if( rc!=SQLITE_OK ) return rc;
drh43605152004-05-29 21:46:49 +00004344 top = get2byte(&data[hdr+5]);
4345 assert( end + sz <= top );
4346 }
4347 idx = allocateSpace(pPage, sz);
4348 assert( idx>0 );
4349 assert( end <= get2byte(&data[hdr+5]) );
4350 pPage->nCell++;
4351 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00004352 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004353 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4354 ptr[0] = ptr[-2];
4355 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00004356 }
drh43605152004-05-29 21:46:49 +00004357 put2byte(&data[ins], idx);
4358 put2byte(&data[hdr+3], pPage->nCell);
4359 pPage->idxShift = 1;
danielk1977a19df672004-11-03 11:37:07 +00004360#ifndef SQLITE_OMIT_AUTOVACUUM
4361 if( pPage->pBt->autoVacuum ){
4362 /* The cell may contain a pointer to an overflow page. If so, write
4363 ** the entry for the overflow page into the pointer map.
4364 */
4365 CellInfo info;
4366 parseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00004367 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk1977a19df672004-11-03 11:37:07 +00004368 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4369 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
4370 int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
4371 if( rc!=SQLITE_OK ) return rc;
4372 }
4373 }
4374#endif
drh14acc042001-06-10 19:56:58 +00004375 }
danielk1977e80463b2004-11-03 03:01:16 +00004376
danielk1977e80463b2004-11-03 03:01:16 +00004377 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004378}
4379
4380/*
drhfa1a98a2004-05-14 19:08:17 +00004381** Add a list of cells to a page. The page should be initially empty.
4382** The cells are guaranteed to fit on the page.
4383*/
4384static void assemblePage(
4385 MemPage *pPage, /* The page to be assemblied */
4386 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00004387 u8 **apCell, /* Pointers to cell bodies */
drhfa1a98a2004-05-14 19:08:17 +00004388 int *aSize /* Sizes of the cells */
4389){
4390 int i; /* Loop counter */
4391 int totalSize; /* Total size of all cells */
4392 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00004393 int cellptr; /* Address of next cell pointer */
4394 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00004395 u8 *data; /* Data for the page */
4396
drh43605152004-05-29 21:46:49 +00004397 assert( pPage->nOverflow==0 );
drhfa1a98a2004-05-14 19:08:17 +00004398 totalSize = 0;
4399 for(i=0; i<nCell; i++){
4400 totalSize += aSize[i];
4401 }
drh43605152004-05-29 21:46:49 +00004402 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00004403 assert( pPage->nCell==0 );
drh43605152004-05-29 21:46:49 +00004404 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00004405 data = pPage->aData;
4406 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00004407 put2byte(&data[hdr+3], nCell);
drh09d0deb2005-08-02 17:13:09 +00004408 if( nCell ){
4409 cellbody = allocateSpace(pPage, totalSize);
4410 assert( cellbody>0 );
4411 assert( pPage->nFree >= 2*nCell );
4412 pPage->nFree -= 2*nCell;
4413 for(i=0; i<nCell; i++){
4414 put2byte(&data[cellptr], cellbody);
4415 memcpy(&data[cellbody], apCell[i], aSize[i]);
4416 cellptr += 2;
4417 cellbody += aSize[i];
4418 }
4419 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00004420 }
4421 pPage->nCell = nCell;
drhfa1a98a2004-05-14 19:08:17 +00004422}
4423
drh14acc042001-06-10 19:56:58 +00004424/*
drhc3b70572003-01-04 19:44:07 +00004425** The following parameters determine how many adjacent pages get involved
4426** in a balancing operation. NN is the number of neighbors on either side
4427** of the page that participate in the balancing operation. NB is the
4428** total number of pages that participate, including the target page and
4429** NN neighbors on either side.
4430**
4431** The minimum value of NN is 1 (of course). Increasing NN above 1
4432** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4433** in exchange for a larger degradation in INSERT and UPDATE performance.
4434** The value of NN appears to give the best results overall.
4435*/
4436#define NN 1 /* Number of neighbors on either side of pPage */
4437#define NB (NN*2+1) /* Total pages involved in the balance */
4438
drh43605152004-05-29 21:46:49 +00004439/* Forward reference */
danielk1977ac245ec2005-01-14 13:50:11 +00004440static int balance(MemPage*, int);
4441
drh615ae552005-01-16 23:21:00 +00004442#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004443/*
4444** This version of balance() handles the common special case where
4445** a new entry is being inserted on the extreme right-end of the
4446** tree, in other words, when the new entry will become the largest
4447** entry in the tree.
4448**
4449** Instead of trying balance the 3 right-most leaf pages, just add
4450** a new page to the right-hand side and put the one new entry in
4451** that page. This leaves the right side of the tree somewhat
4452** unbalanced. But odds are that we will be inserting new entries
4453** at the end soon afterwards so the nearly empty page will quickly
4454** fill up. On average.
4455**
4456** pPage is the leaf page which is the right-most page in the tree.
4457** pParent is its parent. pPage must have a single overflow entry
4458** which is also the right-most entry on the page.
4459*/
danielk1977ac245ec2005-01-14 13:50:11 +00004460static int balance_quick(MemPage *pPage, MemPage *pParent){
4461 int rc;
4462 MemPage *pNew;
4463 Pgno pgnoNew;
4464 u8 *pCell;
4465 int szCell;
4466 CellInfo info;
danielk1977aef0bf62005-12-30 16:28:01 +00004467 BtShared *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00004468 int parentIdx = pParent->nCell; /* pParent new divider cell index */
4469 int parentSize; /* Size of new divider cell */
4470 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00004471
4472 /* Allocate a new page. Insert the overflow cell from pPage
4473 ** into it. Then remove the overflow cell from pPage.
4474 */
drh4f0c5872007-03-26 22:05:01 +00004475 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977ac245ec2005-01-14 13:50:11 +00004476 if( rc!=SQLITE_OK ){
4477 return rc;
4478 }
4479 pCell = pPage->aOvfl[0].pCell;
4480 szCell = cellSizePtr(pPage, pCell);
4481 zeroPage(pNew, pPage->aData[0]);
4482 assemblePage(pNew, 1, &pCell, &szCell);
4483 pPage->nOverflow = 0;
4484
danielk197779a40da2005-01-16 08:00:01 +00004485 /* Set the parent of the newly allocated page to pParent. */
4486 pNew->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004487 sqlite3PagerRef(pParent->pDbPage);
danielk197779a40da2005-01-16 08:00:01 +00004488
danielk1977ac245ec2005-01-14 13:50:11 +00004489 /* pPage is currently the right-child of pParent. Change this
4490 ** so that the right-child is the new page allocated above and
danielk197779a40da2005-01-16 08:00:01 +00004491 ** pPage is the next-to-right child.
danielk1977ac245ec2005-01-14 13:50:11 +00004492 */
danielk1977ac11ee62005-01-15 12:45:51 +00004493 assert( pPage->nCell>0 );
danielk1977ac245ec2005-01-14 13:50:11 +00004494 parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info);
4495 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize);
4496 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004497 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004498 }
4499 assert( parentSize<64 );
4500 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
4501 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004502 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004503 }
4504 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
4505 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4506
danielk197779a40da2005-01-16 08:00:01 +00004507#ifndef SQLITE_OMIT_AUTOVACUUM
4508 /* If this is an auto-vacuum database, update the pointer map
4509 ** with entries for the new page, and any pointer from the
4510 ** cell on the page to an overflow page.
4511 */
danielk1977ac11ee62005-01-15 12:45:51 +00004512 if( pBt->autoVacuum ){
4513 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
4514 if( rc!=SQLITE_OK ){
4515 return rc;
4516 }
danielk197779a40da2005-01-16 08:00:01 +00004517 rc = ptrmapPutOvfl(pNew, 0);
4518 if( rc!=SQLITE_OK ){
4519 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00004520 }
4521 }
danielk197779a40da2005-01-16 08:00:01 +00004522#endif
danielk1977ac11ee62005-01-15 12:45:51 +00004523
danielk197779a40da2005-01-16 08:00:01 +00004524 /* Release the reference to the new page and balance the parent page,
4525 ** in case the divider cell inserted caused it to become overfull.
4526 */
danielk1977ac245ec2005-01-14 13:50:11 +00004527 releasePage(pNew);
4528 return balance(pParent, 0);
4529}
drh615ae552005-01-16 23:21:00 +00004530#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00004531
drhc3b70572003-01-04 19:44:07 +00004532/*
danielk1977ac11ee62005-01-15 12:45:51 +00004533** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
4534** if the database supports auto-vacuum or not. Because it is used
4535** within an expression that is an argument to another macro
4536** (sqliteMallocRaw), it is not possible to use conditional compilation.
4537** So, this macro is defined instead.
4538*/
4539#ifndef SQLITE_OMIT_AUTOVACUUM
4540#define ISAUTOVACUUM (pBt->autoVacuum)
4541#else
4542#define ISAUTOVACUUM 0
4543#endif
4544
4545/*
drhab01f612004-05-22 02:55:23 +00004546** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00004547** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00004548** Usually NN siblings on either side of pPage is used in the balancing,
4549** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00004550** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00004551** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00004552** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00004553**
drh0c6cc4e2004-06-15 02:13:26 +00004554** The number of siblings of pPage might be increased or decreased by one or
4555** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00004556** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00004557** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00004558** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00004559** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00004560**
drh8b2f49b2001-06-08 00:21:52 +00004561** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00004562** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00004563** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00004564** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00004565**
drh8c42ca92001-06-22 19:15:00 +00004566** In the course of balancing the siblings of pPage, the parent of pPage
4567** might become overfull or underfull. If that happens, then this routine
4568** is called recursively on the parent.
4569**
drh5e00f6c2001-09-13 13:46:56 +00004570** If this routine fails for any reason, it might leave the database
4571** in a corrupted state. So if this routine fails, the database should
4572** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00004573*/
drh43605152004-05-29 21:46:49 +00004574static int balance_nonroot(MemPage *pPage){
drh8b2f49b2001-06-08 00:21:52 +00004575 MemPage *pParent; /* The parent of pPage */
danielk1977aef0bf62005-12-30 16:28:01 +00004576 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:07 +00004577 int nCell = 0; /* Number of cells in apCell[] */
4578 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
drh8b2f49b2001-06-08 00:21:52 +00004579 int nOld; /* Number of pages in apOld[] */
4580 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00004581 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00004582 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00004583 int idx; /* Index of pPage in pParent->aCell[] */
4584 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00004585 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00004586 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00004587 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00004588 int usableSpace; /* Bytes in pPage beyond the header */
4589 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00004590 int subtotal; /* Subtotal of bytes in cells on one page */
drhb6f41482004-05-14 01:58:11 +00004591 int iSpace = 0; /* First unused byte of aSpace[] */
drhc3b70572003-01-04 19:44:07 +00004592 MemPage *apOld[NB]; /* pPage and up to two siblings */
4593 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00004594 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00004595 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
4596 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drh4b70f112004-05-02 21:12:19 +00004597 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00004598 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
4599 int szNew[NB+2]; /* Combined size of cells place on i-th page */
danielk197750f059b2005-03-29 02:54:03 +00004600 u8 **apCell = 0; /* All cells begin balanced */
drh2e38c322004-09-03 18:38:44 +00004601 int *szCell; /* Local size of all cells in apCell[] */
4602 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
4603 u8 *aSpace; /* Space to hold copies of dividers cells */
danielk19774e17d142005-01-16 09:06:33 +00004604#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00004605 u8 *aFrom = 0;
4606#endif
drh8b2f49b2001-06-08 00:21:52 +00004607
drh14acc042001-06-10 19:56:58 +00004608 /*
drh43605152004-05-29 21:46:49 +00004609 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00004610 */
drh3a4c1412004-05-09 20:40:11 +00004611 assert( pPage->isInit );
danielk19773b8a05f2007-03-19 17:44:26 +00004612 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh4b70f112004-05-02 21:12:19 +00004613 pBt = pPage->pBt;
drh14acc042001-06-10 19:56:58 +00004614 pParent = pPage->pParent;
drh43605152004-05-29 21:46:49 +00004615 assert( pParent );
danielk19773b8a05f2007-03-19 17:44:26 +00004616 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
danielk197707cb5602006-01-20 10:55:05 +00004617 return rc;
4618 }
drh43605152004-05-29 21:46:49 +00004619 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00004620
drh615ae552005-01-16 23:21:00 +00004621#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004622 /*
4623 ** A special case: If a new entry has just been inserted into a
4624 ** table (that is, a btree with integer keys and all data at the leaves)
drh09d0deb2005-08-02 17:13:09 +00004625 ** and the new entry is the right-most entry in the tree (it has the
drhf222e712005-01-14 22:55:49 +00004626 ** largest key) then use the special balance_quick() routine for
4627 ** balancing. balance_quick() is much faster and results in a tighter
4628 ** packing of data in the common case.
4629 */
danielk1977ac245ec2005-01-14 13:50:11 +00004630 if( pPage->leaf &&
4631 pPage->intKey &&
4632 pPage->leafData &&
4633 pPage->nOverflow==1 &&
4634 pPage->aOvfl[0].idx==pPage->nCell &&
danielk1977ac11ee62005-01-15 12:45:51 +00004635 pPage->pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00004636 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
4637 ){
danielk1977ac11ee62005-01-15 12:45:51 +00004638 /*
4639 ** TODO: Check the siblings to the left of pPage. It may be that
4640 ** they are not full and no new page is required.
4641 */
danielk1977ac245ec2005-01-14 13:50:11 +00004642 return balance_quick(pPage, pParent);
4643 }
4644#endif
4645
drh2e38c322004-09-03 18:38:44 +00004646 /*
drh4b70f112004-05-02 21:12:19 +00004647 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00004648 ** to pPage. The "idx" variable is the index of that cell. If pPage
4649 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00004650 */
drhbb49aba2003-01-04 18:53:27 +00004651 if( pParent->idxShift ){
drha34b6762004-05-07 13:30:42 +00004652 Pgno pgno;
drh4b70f112004-05-02 21:12:19 +00004653 pgno = pPage->pgno;
danielk19773b8a05f2007-03-19 17:44:26 +00004654 assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbb49aba2003-01-04 18:53:27 +00004655 for(idx=0; idx<pParent->nCell; idx++){
drh43605152004-05-29 21:46:49 +00004656 if( get4byte(findCell(pParent, idx))==pgno ){
drhbb49aba2003-01-04 18:53:27 +00004657 break;
4658 }
drh8b2f49b2001-06-08 00:21:52 +00004659 }
drh4b70f112004-05-02 21:12:19 +00004660 assert( idx<pParent->nCell
drh43605152004-05-29 21:46:49 +00004661 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
drhbb49aba2003-01-04 18:53:27 +00004662 }else{
4663 idx = pPage->idxParent;
drh8b2f49b2001-06-08 00:21:52 +00004664 }
drh8b2f49b2001-06-08 00:21:52 +00004665
4666 /*
drh14acc042001-06-10 19:56:58 +00004667 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00004668 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00004669 */
drh14acc042001-06-10 19:56:58 +00004670 nOld = nNew = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004671 sqlite3PagerRef(pParent->pDbPage);
drh14acc042001-06-10 19:56:58 +00004672
4673 /*
drh4b70f112004-05-02 21:12:19 +00004674 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00004675 ** the siblings. An attempt is made to find NN siblings on either
4676 ** side of pPage. More siblings are taken from one side, however, if
4677 ** pPage there are fewer than NN siblings on the other side. If pParent
4678 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00004679 */
drhc3b70572003-01-04 19:44:07 +00004680 nxDiv = idx - NN;
4681 if( nxDiv + NB > pParent->nCell ){
4682 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00004683 }
drhc3b70572003-01-04 19:44:07 +00004684 if( nxDiv<0 ){
4685 nxDiv = 0;
4686 }
drh8b2f49b2001-06-08 00:21:52 +00004687 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00004688 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00004689 if( k<pParent->nCell ){
drh43605152004-05-29 21:46:49 +00004690 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00004691 nDiv++;
drha34b6762004-05-07 13:30:42 +00004692 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00004693 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00004694 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00004695 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00004696 }else{
4697 break;
drh8b2f49b2001-06-08 00:21:52 +00004698 }
drhde647132004-05-07 17:57:49 +00004699 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00004700 if( rc ) goto balance_cleanup;
drh428ae8c2003-01-04 16:48:09 +00004701 apOld[i]->idxParent = k;
drh91025292004-05-03 19:49:32 +00004702 apCopy[i] = 0;
4703 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00004704 nOld++;
danielk1977634f2982005-03-28 08:44:07 +00004705 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
drh8b2f49b2001-06-08 00:21:52 +00004706 }
4707
drh8d97f1f2005-05-05 18:14:13 +00004708 /* Make nMaxCells a multiple of 2 in order to preserve 8-byte
4709 ** alignment */
4710 nMaxCells = (nMaxCells + 1)&~1;
4711
drh8b2f49b2001-06-08 00:21:52 +00004712 /*
danielk1977634f2982005-03-28 08:44:07 +00004713 ** Allocate space for memory structures
4714 */
4715 apCell = sqliteMallocRaw(
4716 nMaxCells*sizeof(u8*) /* apCell */
4717 + nMaxCells*sizeof(int) /* szCell */
drhc96d8532005-05-03 12:30:33 +00004718 + ROUND8(sizeof(MemPage))*NB /* aCopy */
drh07d183d2005-05-01 22:52:42 +00004719 + pBt->pageSize*(5+NB) /* aSpace */
drhc96d8532005-05-03 12:30:33 +00004720 + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
danielk1977634f2982005-03-28 08:44:07 +00004721 );
4722 if( apCell==0 ){
4723 rc = SQLITE_NOMEM;
4724 goto balance_cleanup;
4725 }
4726 szCell = (int*)&apCell[nMaxCells];
4727 aCopy[0] = (u8*)&szCell[nMaxCells];
drhc96d8532005-05-03 12:30:33 +00004728 assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004729 for(i=1; i<NB; i++){
drhc96d8532005-05-03 12:30:33 +00004730 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4731 assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004732 }
drhc96d8532005-05-03 12:30:33 +00004733 aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4734 assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004735#ifndef SQLITE_OMIT_AUTOVACUUM
4736 if( pBt->autoVacuum ){
drh07d183d2005-05-01 22:52:42 +00004737 aFrom = &aSpace[5*pBt->pageSize];
danielk1977634f2982005-03-28 08:44:07 +00004738 }
4739#endif
4740
4741 /*
drh14acc042001-06-10 19:56:58 +00004742 ** Make copies of the content of pPage and its siblings into aOld[].
4743 ** The rest of this function will use data from the copies rather
4744 ** that the original pages since the original pages will be in the
4745 ** process of being overwritten.
4746 */
4747 for(i=0; i<nOld; i++){
drh07d183d2005-05-01 22:52:42 +00004748 MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize];
drh07d183d2005-05-01 22:52:42 +00004749 p->aData = &((u8*)p)[-pBt->pageSize];
4750 memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage));
4751 /* The memcpy() above changes the value of p->aData so we have to
4752 ** set it again. */
drh07d183d2005-05-01 22:52:42 +00004753 p->aData = &((u8*)p)[-pBt->pageSize];
drh14acc042001-06-10 19:56:58 +00004754 }
4755
4756 /*
4757 ** Load pointers to all cells on sibling pages and the divider cells
4758 ** into the local apCell[] array. Make copies of the divider cells
drhb6f41482004-05-14 01:58:11 +00004759 ** into space obtained form aSpace[] and remove the the divider Cells
4760 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00004761 **
4762 ** If the siblings are on leaf pages, then the child pointers of the
4763 ** divider cells are stripped from the cells before they are copied
drh96f5b762004-05-16 16:24:36 +00004764 ** into aSpace[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00004765 ** child pointers. If siblings are not leaves, then all cell in
4766 ** apCell[] include child pointers. Either way, all cells in apCell[]
4767 ** are alike.
drh96f5b762004-05-16 16:24:36 +00004768 **
4769 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
4770 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00004771 */
4772 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00004773 leafCorrection = pPage->leaf*4;
drh8b18dd42004-05-12 19:18:15 +00004774 leafData = pPage->leafData && pPage->leaf;
drh8b2f49b2001-06-08 00:21:52 +00004775 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00004776 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00004777 int limit = pOld->nCell+pOld->nOverflow;
4778 for(j=0; j<limit; j++){
danielk1977634f2982005-03-28 08:44:07 +00004779 assert( nCell<nMaxCells );
drh43605152004-05-29 21:46:49 +00004780 apCell[nCell] = findOverflowCell(pOld, j);
4781 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk1977ac11ee62005-01-15 12:45:51 +00004782#ifndef SQLITE_OMIT_AUTOVACUUM
4783 if( pBt->autoVacuum ){
4784 int a;
4785 aFrom[nCell] = i;
4786 for(a=0; a<pOld->nOverflow; a++){
4787 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
4788 aFrom[nCell] = 0xFF;
4789 break;
4790 }
4791 }
4792 }
4793#endif
drh14acc042001-06-10 19:56:58 +00004794 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00004795 }
4796 if( i<nOld-1 ){
drh43605152004-05-29 21:46:49 +00004797 int sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00004798 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00004799 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
4800 ** are duplicates of keys on the child pages. We need to remove
4801 ** the divider cells from pParent, but the dividers cells are not
4802 ** added to apCell[] because they are duplicates of child cells.
4803 */
drh8b18dd42004-05-12 19:18:15 +00004804 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00004805 }else{
drhb6f41482004-05-14 01:58:11 +00004806 u8 *pTemp;
danielk1977634f2982005-03-28 08:44:07 +00004807 assert( nCell<nMaxCells );
drhb6f41482004-05-14 01:58:11 +00004808 szCell[nCell] = sz;
4809 pTemp = &aSpace[iSpace];
4810 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00004811 assert( iSpace<=pBt->pageSize*5 );
drhb6f41482004-05-14 01:58:11 +00004812 memcpy(pTemp, apDiv[i], sz);
4813 apCell[nCell] = pTemp+leafCorrection;
danielk1977ac11ee62005-01-15 12:45:51 +00004814#ifndef SQLITE_OMIT_AUTOVACUUM
4815 if( pBt->autoVacuum ){
4816 aFrom[nCell] = 0xFF;
4817 }
4818#endif
drhb6f41482004-05-14 01:58:11 +00004819 dropCell(pParent, nxDiv, sz);
drh8b18dd42004-05-12 19:18:15 +00004820 szCell[nCell] -= leafCorrection;
drh43605152004-05-29 21:46:49 +00004821 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00004822 if( !pOld->leaf ){
4823 assert( leafCorrection==0 );
4824 /* The right pointer of the child page pOld becomes the left
4825 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00004826 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00004827 }else{
4828 assert( leafCorrection==4 );
4829 }
4830 nCell++;
drh4b70f112004-05-02 21:12:19 +00004831 }
drh8b2f49b2001-06-08 00:21:52 +00004832 }
4833 }
4834
4835 /*
drh6019e162001-07-02 17:51:45 +00004836 ** Figure out the number of pages needed to hold all nCell cells.
4837 ** Store this number in "k". Also compute szNew[] which is the total
4838 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00004839 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00004840 ** cntNew[k] should equal nCell.
4841 **
drh96f5b762004-05-16 16:24:36 +00004842 ** Values computed by this block:
4843 **
4844 ** k: The total number of sibling pages
4845 ** szNew[i]: Spaced used on the i-th sibling page.
4846 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
4847 ** the right of the i-th sibling page.
4848 ** usableSpace: Number of bytes of space available on each sibling.
4849 **
drh8b2f49b2001-06-08 00:21:52 +00004850 */
drh43605152004-05-29 21:46:49 +00004851 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00004852 for(subtotal=k=i=0; i<nCell; i++){
danielk1977634f2982005-03-28 08:44:07 +00004853 assert( i<nMaxCells );
drh43605152004-05-29 21:46:49 +00004854 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00004855 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00004856 szNew[k] = subtotal - szCell[i];
4857 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00004858 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00004859 subtotal = 0;
4860 k++;
4861 }
4862 }
4863 szNew[k] = subtotal;
4864 cntNew[k] = nCell;
4865 k++;
drh96f5b762004-05-16 16:24:36 +00004866
4867 /*
4868 ** The packing computed by the previous block is biased toward the siblings
4869 ** on the left side. The left siblings are always nearly full, while the
4870 ** right-most sibling might be nearly empty. This block of code attempts
4871 ** to adjust the packing of siblings to get a better balance.
4872 **
4873 ** This adjustment is more than an optimization. The packing above might
4874 ** be so out of balance as to be illegal. For example, the right-most
4875 ** sibling might be completely empty. This adjustment is not optional.
4876 */
drh6019e162001-07-02 17:51:45 +00004877 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00004878 int szRight = szNew[i]; /* Size of sibling on the right */
4879 int szLeft = szNew[i-1]; /* Size of sibling on the left */
4880 int r; /* Index of right-most cell in left sibling */
4881 int d; /* Index of first cell to the left of right sibling */
4882
4883 r = cntNew[i-1] - 1;
4884 d = r + 1 - leafData;
danielk1977634f2982005-03-28 08:44:07 +00004885 assert( d<nMaxCells );
4886 assert( r<nMaxCells );
drh43605152004-05-29 21:46:49 +00004887 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
4888 szRight += szCell[d] + 2;
4889 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00004890 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00004891 r = cntNew[i-1] - 1;
4892 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00004893 }
drh96f5b762004-05-16 16:24:36 +00004894 szNew[i] = szRight;
4895 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00004896 }
drh09d0deb2005-08-02 17:13:09 +00004897
4898 /* Either we found one or more cells (cntnew[0])>0) or we are the
4899 ** a virtual root page. A virtual root page is when the real root
4900 ** page is page 1 and we are the only child of that page.
4901 */
4902 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
drh8b2f49b2001-06-08 00:21:52 +00004903
4904 /*
drh6b308672002-07-08 02:16:37 +00004905 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00004906 */
drh4b70f112004-05-02 21:12:19 +00004907 assert( pPage->pgno>1 );
4908 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00004909 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00004910 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00004911 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00004912 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00004913 pgnoNew[i] = pgnoOld[i];
4914 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004915 rc = sqlite3PagerWrite(pNew->pDbPage);
drhf5345442007-04-09 12:45:02 +00004916 nNew++;
danielk197728129562005-01-11 10:25:06 +00004917 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00004918 }else{
drh7aa8f852006-03-28 00:24:44 +00004919 assert( i>0 );
drh4f0c5872007-03-26 22:05:01 +00004920 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00004921 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00004922 apNew[i] = pNew;
drhf5345442007-04-09 12:45:02 +00004923 nNew++;
drh6b308672002-07-08 02:16:37 +00004924 }
drhda200cc2004-05-09 11:51:38 +00004925 zeroPage(pNew, pageFlags);
drh8b2f49b2001-06-08 00:21:52 +00004926 }
4927
danielk1977299b1872004-11-22 10:02:10 +00004928 /* Free any old pages that were not reused as new pages.
4929 */
4930 while( i<nOld ){
4931 rc = freePage(apOld[i]);
4932 if( rc ) goto balance_cleanup;
4933 releasePage(apOld[i]);
4934 apOld[i] = 0;
4935 i++;
4936 }
4937
drh8b2f49b2001-06-08 00:21:52 +00004938 /*
drhf9ffac92002-03-02 19:00:31 +00004939 ** Put the new pages in accending order. This helps to
4940 ** keep entries in the disk file in order so that a scan
4941 ** of the table is a linear scan through the file. That
4942 ** in turn helps the operating system to deliver pages
4943 ** from the disk more rapidly.
4944 **
4945 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00004946 ** n is never more than NB (a small constant), that should
4947 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00004948 **
drhc3b70572003-01-04 19:44:07 +00004949 ** When NB==3, this one optimization makes the database
4950 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00004951 */
4952 for(i=0; i<k-1; i++){
4953 int minV = pgnoNew[i];
4954 int minI = i;
4955 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00004956 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00004957 minI = j;
4958 minV = pgnoNew[j];
4959 }
4960 }
4961 if( minI>i ){
4962 int t;
4963 MemPage *pT;
4964 t = pgnoNew[i];
4965 pT = apNew[i];
4966 pgnoNew[i] = pgnoNew[minI];
4967 apNew[i] = apNew[minI];
4968 pgnoNew[minI] = t;
4969 apNew[minI] = pT;
4970 }
4971 }
drha2fce642004-06-05 00:01:44 +00004972 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00004973 pgnoOld[0],
4974 nOld>=2 ? pgnoOld[1] : 0,
4975 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00004976 pgnoNew[0], szNew[0],
4977 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
4978 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00004979 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
4980 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00004981
drhf9ffac92002-03-02 19:00:31 +00004982 /*
drh14acc042001-06-10 19:56:58 +00004983 ** Evenly distribute the data in apCell[] across the new pages.
4984 ** Insert divider cells into pParent as necessary.
4985 */
4986 j = 0;
4987 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00004988 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00004989 MemPage *pNew = apNew[i];
drh19642e52005-03-29 13:17:45 +00004990 assert( j<nMaxCells );
drh4b70f112004-05-02 21:12:19 +00004991 assert( pNew->pgno==pgnoNew[i] );
drhfa1a98a2004-05-14 19:08:17 +00004992 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh09d0deb2005-08-02 17:13:09 +00004993 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
drh43605152004-05-29 21:46:49 +00004994 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00004995
4996#ifndef SQLITE_OMIT_AUTOVACUUM
4997 /* If this is an auto-vacuum database, update the pointer map entries
4998 ** that point to the siblings that were rearranged. These can be: left
4999 ** children of cells, the right-child of the page, or overflow pages
5000 ** pointed to by cells.
5001 */
5002 if( pBt->autoVacuum ){
5003 for(k=j; k<cntNew[i]; k++){
danielk1977634f2982005-03-28 08:44:07 +00005004 assert( k<nMaxCells );
danielk1977ac11ee62005-01-15 12:45:51 +00005005 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00005006 rc = ptrmapPutOvfl(pNew, k-j);
5007 if( rc!=SQLITE_OK ){
5008 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005009 }
5010 }
5011 }
5012 }
5013#endif
5014
5015 j = cntNew[i];
5016
5017 /* If the sibling page assembled above was not the right-most sibling,
5018 ** insert a divider cell into the parent page.
5019 */
drh14acc042001-06-10 19:56:58 +00005020 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00005021 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00005022 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00005023 int sz;
danielk1977634f2982005-03-28 08:44:07 +00005024
5025 assert( j<nMaxCells );
drh8b18dd42004-05-12 19:18:15 +00005026 pCell = apCell[j];
5027 sz = szCell[j] + leafCorrection;
drh4b70f112004-05-02 21:12:19 +00005028 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00005029 memcpy(&pNew->aData[8], pCell, 4);
drh24cd67e2004-05-10 16:18:47 +00005030 pTemp = 0;
drh8b18dd42004-05-12 19:18:15 +00005031 }else if( leafData ){
danielk1977ac11ee62005-01-15 12:45:51 +00005032 /* If the tree is a leaf-data tree, and the siblings are leaves,
5033 ** then there is no divider cell in apCell[]. Instead, the divider
5034 ** cell consists of the integer key for the right-most cell of
5035 ** the sibling-page assembled above only.
5036 */
drh6f11bef2004-05-13 01:12:56 +00005037 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00005038 j--;
drh43605152004-05-29 21:46:49 +00005039 parseCellPtr(pNew, apCell[j], &info);
drhb6f41482004-05-14 01:58:11 +00005040 pCell = &aSpace[iSpace];
drh6f11bef2004-05-13 01:12:56 +00005041 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
drhb6f41482004-05-14 01:58:11 +00005042 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00005043 assert( iSpace<=pBt->pageSize*5 );
drh8b18dd42004-05-12 19:18:15 +00005044 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00005045 }else{
5046 pCell -= 4;
drhb6f41482004-05-14 01:58:11 +00005047 pTemp = &aSpace[iSpace];
5048 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00005049 assert( iSpace<=pBt->pageSize*5 );
drh4b70f112004-05-02 21:12:19 +00005050 }
danielk1977a3ad5e72005-01-07 08:56:44 +00005051 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00005052 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh43605152004-05-29 21:46:49 +00005053 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +00005054#ifndef SQLITE_OMIT_AUTOVACUUM
5055 /* If this is an auto-vacuum database, and not a leaf-data tree,
5056 ** then update the pointer map with an entry for the overflow page
5057 ** that the cell just inserted points to (if any).
5058 */
5059 if( pBt->autoVacuum && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00005060 rc = ptrmapPutOvfl(pParent, nxDiv);
5061 if( rc!=SQLITE_OK ){
5062 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005063 }
5064 }
5065#endif
drh14acc042001-06-10 19:56:58 +00005066 j++;
5067 nxDiv++;
5068 }
5069 }
drh6019e162001-07-02 17:51:45 +00005070 assert( j==nCell );
drh7aa8f852006-03-28 00:24:44 +00005071 assert( nOld>0 );
5072 assert( nNew>0 );
drh4b70f112004-05-02 21:12:19 +00005073 if( (pageFlags & PTF_LEAF)==0 ){
drh43605152004-05-29 21:46:49 +00005074 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
drh14acc042001-06-10 19:56:58 +00005075 }
drh43605152004-05-29 21:46:49 +00005076 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00005077 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00005078 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00005079 }else{
5080 /* Right-most sibling is the left child of the first entry in pParent
5081 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00005082 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00005083 }
5084
5085 /*
5086 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00005087 */
5088 for(i=0; i<nNew; i++){
danielk1977afcdd022004-10-31 16:25:42 +00005089 rc = reparentChildPages(apNew[i]);
5090 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00005091 }
danielk1977afcdd022004-10-31 16:25:42 +00005092 rc = reparentChildPages(pParent);
5093 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00005094
5095 /*
drh3a4c1412004-05-09 20:40:11 +00005096 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00005097 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00005098 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00005099 */
drhda200cc2004-05-09 11:51:38 +00005100 assert( pParent->isInit );
danielk1977ac245ec2005-01-14 13:50:11 +00005101 rc = balance(pParent, 0);
drhda200cc2004-05-09 11:51:38 +00005102
drh8b2f49b2001-06-08 00:21:52 +00005103 /*
drh14acc042001-06-10 19:56:58 +00005104 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00005105 */
drh14acc042001-06-10 19:56:58 +00005106balance_cleanup:
drh2e38c322004-09-03 18:38:44 +00005107 sqliteFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00005108 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00005109 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00005110 }
drh14acc042001-06-10 19:56:58 +00005111 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00005112 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00005113 }
drh91025292004-05-03 19:49:32 +00005114 releasePage(pParent);
drh3a4c1412004-05-09 20:40:11 +00005115 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
5116 pPage->pgno, nOld, nNew, nCell));
drh8b2f49b2001-06-08 00:21:52 +00005117 return rc;
5118}
5119
5120/*
drh43605152004-05-29 21:46:49 +00005121** This routine is called for the root page of a btree when the root
5122** page contains no cells. This is an opportunity to make the tree
5123** shallower by one level.
5124*/
5125static int balance_shallower(MemPage *pPage){
5126 MemPage *pChild; /* The only child page of pPage */
5127 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00005128 int rc = SQLITE_OK; /* Return code from subprocedures */
danielk1977aef0bf62005-12-30 16:28:01 +00005129 BtShared *pBt; /* The main BTree structure */
drh2e38c322004-09-03 18:38:44 +00005130 int mxCellPerPage; /* Maximum number of cells per page */
5131 u8 **apCell; /* All cells from pages being balanced */
5132 int *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00005133
5134 assert( pPage->pParent==0 );
5135 assert( pPage->nCell==0 );
drh2e38c322004-09-03 18:38:44 +00005136 pBt = pPage->pBt;
5137 mxCellPerPage = MX_CELL(pBt);
5138 apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
5139 if( apCell==0 ) return SQLITE_NOMEM;
5140 szCell = (int*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00005141 if( pPage->leaf ){
5142 /* The table is completely empty */
5143 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
5144 }else{
5145 /* The root page is empty but has one child. Transfer the
5146 ** information from that one child into the root page if it
5147 ** will fit. This reduces the depth of the tree by one.
5148 **
5149 ** If the root page is page 1, it has less space available than
5150 ** its child (due to the 100 byte header that occurs at the beginning
5151 ** of the database fle), so it might not be able to hold all of the
5152 ** information currently contained in the child. If this is the
5153 ** case, then do not do the transfer. Leave page 1 empty except
5154 ** for the right-pointer to the child page. The child page becomes
5155 ** the virtual root of the tree.
5156 */
5157 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5158 assert( pgnoChild>0 );
danielk19773b8a05f2007-03-19 17:44:26 +00005159 assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) );
drh0787db62007-03-04 13:15:27 +00005160 rc = getPage(pPage->pBt, pgnoChild, &pChild, 0);
drh2e38c322004-09-03 18:38:44 +00005161 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005162 if( pPage->pgno==1 ){
5163 rc = initPage(pChild, pPage);
drh2e38c322004-09-03 18:38:44 +00005164 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005165 assert( pChild->nOverflow==0 );
5166 if( pChild->nFree>=100 ){
5167 /* The child information will fit on the root page, so do the
5168 ** copy */
5169 int i;
5170 zeroPage(pPage, pChild->aData[0]);
5171 for(i=0; i<pChild->nCell; i++){
5172 apCell[i] = findCell(pChild,i);
5173 szCell[i] = cellSizePtr(pChild, apCell[i]);
5174 }
5175 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00005176 /* Copy the right-pointer of the child to the parent. */
5177 put4byte(&pPage->aData[pPage->hdrOffset+8],
5178 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh43605152004-05-29 21:46:49 +00005179 freePage(pChild);
5180 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5181 }else{
5182 /* The child has more information that will fit on the root.
5183 ** The tree is already balanced. Do nothing. */
5184 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5185 }
5186 }else{
5187 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5188 pPage->isInit = 0;
5189 pPage->pParent = 0;
5190 rc = initPage(pPage, 0);
5191 assert( rc==SQLITE_OK );
5192 freePage(pChild);
5193 TRACE(("BALANCE: transfer child %d into root %d\n",
5194 pChild->pgno, pPage->pgno));
5195 }
danielk1977afcdd022004-10-31 16:25:42 +00005196 rc = reparentChildPages(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00005197 assert( pPage->nOverflow==0 );
5198#ifndef SQLITE_OMIT_AUTOVACUUM
5199 if( pBt->autoVacuum ){
danielk1977aac0a382005-01-16 11:07:06 +00005200 int i;
danielk1977ac11ee62005-01-15 12:45:51 +00005201 for(i=0; i<pPage->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005202 rc = ptrmapPutOvfl(pPage, i);
5203 if( rc!=SQLITE_OK ){
5204 goto end_shallow_balance;
danielk1977ac11ee62005-01-15 12:45:51 +00005205 }
5206 }
5207 }
5208#endif
danielk1977afcdd022004-10-31 16:25:42 +00005209 if( rc!=SQLITE_OK ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005210 releasePage(pChild);
5211 }
drh2e38c322004-09-03 18:38:44 +00005212end_shallow_balance:
5213 sqliteFree(apCell);
5214 return rc;
drh43605152004-05-29 21:46:49 +00005215}
5216
5217
5218/*
5219** The root page is overfull
5220**
5221** When this happens, Create a new child page and copy the
5222** contents of the root into the child. Then make the root
5223** page an empty page with rightChild pointing to the new
5224** child. Finally, call balance_internal() on the new child
5225** to cause it to split.
5226*/
5227static int balance_deeper(MemPage *pPage){
5228 int rc; /* Return value from subprocedures */
5229 MemPage *pChild; /* Pointer to a new child page */
5230 Pgno pgnoChild; /* Page number of the new child page */
danielk1977aef0bf62005-12-30 16:28:01 +00005231 BtShared *pBt; /* The BTree */
drh43605152004-05-29 21:46:49 +00005232 int usableSize; /* Total usable size of a page */
5233 u8 *data; /* Content of the parent page */
5234 u8 *cdata; /* Content of the child page */
5235 int hdr; /* Offset to page header in parent */
5236 int brk; /* Offset to content of first cell in parent */
5237
5238 assert( pPage->pParent==0 );
5239 assert( pPage->nOverflow>0 );
5240 pBt = pPage->pBt;
drh4f0c5872007-03-26 22:05:01 +00005241 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00005242 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005243 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
drh43605152004-05-29 21:46:49 +00005244 usableSize = pBt->usableSize;
5245 data = pPage->aData;
5246 hdr = pPage->hdrOffset;
5247 brk = get2byte(&data[hdr+5]);
5248 cdata = pChild->aData;
5249 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
5250 memcpy(&cdata[brk], &data[brk], usableSize-brk);
danielk1977c7dc7532004-11-17 10:22:03 +00005251 assert( pChild->isInit==0 );
drh43605152004-05-29 21:46:49 +00005252 rc = initPage(pChild, pPage);
danielk19776b456a22005-03-21 04:04:02 +00005253 if( rc ) goto balancedeeper_out;
drh43605152004-05-29 21:46:49 +00005254 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
5255 pChild->nOverflow = pPage->nOverflow;
5256 if( pChild->nOverflow ){
5257 pChild->nFree = 0;
5258 }
5259 assert( pChild->nCell==pPage->nCell );
5260 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5261 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5262 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
danielk19774e17d142005-01-16 09:06:33 +00005263#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00005264 if( pBt->autoVacuum ){
5265 int i;
5266 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
danielk19776b456a22005-03-21 04:04:02 +00005267 if( rc ) goto balancedeeper_out;
danielk1977ac11ee62005-01-15 12:45:51 +00005268 for(i=0; i<pChild->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005269 rc = ptrmapPutOvfl(pChild, i);
5270 if( rc!=SQLITE_OK ){
5271 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00005272 }
5273 }
5274 }
danielk19774e17d142005-01-16 09:06:33 +00005275#endif
drh43605152004-05-29 21:46:49 +00005276 rc = balance_nonroot(pChild);
danielk19776b456a22005-03-21 04:04:02 +00005277
5278balancedeeper_out:
drh43605152004-05-29 21:46:49 +00005279 releasePage(pChild);
5280 return rc;
5281}
5282
5283/*
5284** Decide if the page pPage needs to be balanced. If balancing is
5285** required, call the appropriate balancing routine.
5286*/
danielk1977ac245ec2005-01-14 13:50:11 +00005287static int balance(MemPage *pPage, int insert){
drh43605152004-05-29 21:46:49 +00005288 int rc = SQLITE_OK;
5289 if( pPage->pParent==0 ){
5290 if( pPage->nOverflow>0 ){
5291 rc = balance_deeper(pPage);
5292 }
danielk1977687566d2004-11-02 12:56:41 +00005293 if( rc==SQLITE_OK && pPage->nCell==0 ){
drh43605152004-05-29 21:46:49 +00005294 rc = balance_shallower(pPage);
5295 }
5296 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00005297 if( pPage->nOverflow>0 ||
5298 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
drh43605152004-05-29 21:46:49 +00005299 rc = balance_nonroot(pPage);
5300 }
5301 }
5302 return rc;
5303}
5304
5305/*
drh8dcd7ca2004-08-08 19:43:29 +00005306** This routine checks all cursors that point to table pgnoRoot.
drh980b1a72006-08-16 16:42:48 +00005307** If any of those cursors were opened with wrFlag==0 in a different
5308** database connection (a database connection that shares the pager
5309** cache with the current connection) and that other connection
5310** is not in the ReadUncommmitted state, then this routine returns
5311** SQLITE_LOCKED.
danielk1977299b1872004-11-22 10:02:10 +00005312**
5313** In addition to checking for read-locks (where a read-lock
5314** means a cursor opened with wrFlag==0) this routine also moves
drh980b1a72006-08-16 16:42:48 +00005315** all cursors write cursors so that they are pointing to the
5316** first Cell on the root page. This is necessary because an insert
danielk1977299b1872004-11-22 10:02:10 +00005317** or delete might change the number of cells on a page or delete
5318** a page entirely and we do not want to leave any cursors
5319** pointing to non-existant pages or cells.
drhf74b8d92002-09-01 23:20:45 +00005320*/
drh980b1a72006-08-16 16:42:48 +00005321static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
danielk1977299b1872004-11-22 10:02:10 +00005322 BtCursor *p;
drh980b1a72006-08-16 16:42:48 +00005323 BtShared *pBt = pBtree->pBt;
5324 sqlite3 *db = pBtree->pSqlite;
danielk1977299b1872004-11-22 10:02:10 +00005325 for(p=pBt->pCursor; p; p=p->pNext){
drh980b1a72006-08-16 16:42:48 +00005326 if( p==pExclude ) continue;
5327 if( p->eState!=CURSOR_VALID ) continue;
5328 if( p->pgnoRoot!=pgnoRoot ) continue;
5329 if( p->wrFlag==0 ){
5330 sqlite3 *dbOther = p->pBtree->pSqlite;
5331 if( dbOther==0 ||
5332 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5333 return SQLITE_LOCKED;
5334 }
5335 }else if( p->pPage->pgno!=p->pgnoRoot ){
danielk1977299b1872004-11-22 10:02:10 +00005336 moveToRoot(p);
5337 }
5338 }
drhf74b8d92002-09-01 23:20:45 +00005339 return SQLITE_OK;
5340}
5341
5342/*
drh3b7511c2001-05-26 13:15:44 +00005343** Insert a new record into the BTree. The key is given by (pKey,nKey)
5344** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00005345** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00005346** is left pointing at a random location.
5347**
5348** For an INTKEY table, only the nKey value of the key is used. pKey is
5349** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00005350*/
drh3aac2dd2004-04-26 14:10:20 +00005351int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00005352 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00005353 const void *pKey, i64 nKey, /* The key of the new record */
drhe4d90812007-03-29 05:51:49 +00005354 const void *pData, int nData, /* The data of the new record */
5355 int appendBias /* True if this is likely an append */
drh3b7511c2001-05-26 13:15:44 +00005356){
drh3b7511c2001-05-26 13:15:44 +00005357 int rc;
5358 int loc;
drh14acc042001-06-10 19:56:58 +00005359 int szNew;
drh3b7511c2001-05-26 13:15:44 +00005360 MemPage *pPage;
danielk1977aef0bf62005-12-30 16:28:01 +00005361 BtShared *pBt = pCur->pBtree->pBt;
drha34b6762004-05-07 13:30:42 +00005362 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00005363 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00005364
danielk1977aef0bf62005-12-30 16:28:01 +00005365 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005366 /* Must start a transaction before doing an insert */
5367 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005368 }
drhf74b8d92002-09-01 23:20:45 +00005369 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00005370 if( !pCur->wrFlag ){
5371 return SQLITE_PERM; /* Cursor not open for writing */
5372 }
drh980b1a72006-08-16 16:42:48 +00005373 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005374 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5375 }
danielk1977da184232006-01-05 11:34:32 +00005376
5377 /* Save the positions of any other cursors open on this table */
drhbf700f32007-03-31 02:36:44 +00005378 clearCursorPosition(pCur);
danielk19772e94d4d2006-01-09 05:36:27 +00005379 if(
danielk19772e94d4d2006-01-09 05:36:27 +00005380 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
drhe4d90812007-03-29 05:51:49 +00005381 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
danielk19772e94d4d2006-01-09 05:36:27 +00005382 ){
danielk1977da184232006-01-05 11:34:32 +00005383 return rc;
5384 }
5385
drh14acc042001-06-10 19:56:58 +00005386 pPage = pCur->pPage;
drh4a1c3802004-05-12 15:15:47 +00005387 assert( pPage->intKey || nKey>=0 );
drh8b18dd42004-05-12 19:18:15 +00005388 assert( pPage->leaf || !pPage->leafData );
drh3a4c1412004-05-09 20:40:11 +00005389 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5390 pCur->pgnoRoot, nKey, nData, pPage->pgno,
5391 loc==0 ? "overwrite" : "new entry"));
drh7aa128d2002-06-21 13:09:16 +00005392 assert( pPage->isInit );
danielk19773b8a05f2007-03-19 17:44:26 +00005393 rc = sqlite3PagerWrite(pPage->pDbPage);
drhbd03cae2001-06-02 02:40:57 +00005394 if( rc ) return rc;
drh2e38c322004-09-03 18:38:44 +00005395 newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
5396 if( newCell==0 ) return SQLITE_NOMEM;
drha34b6762004-05-07 13:30:42 +00005397 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew);
drh2e38c322004-09-03 18:38:44 +00005398 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00005399 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00005400 assert( szNew<=MX_CELL_SIZE(pBt) );
danielk1977da184232006-01-05 11:34:32 +00005401 if( loc==0 && CURSOR_VALID==pCur->eState ){
drha34b6762004-05-07 13:30:42 +00005402 int szOld;
5403 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00005404 oldCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005405 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005406 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00005407 }
drh43605152004-05-29 21:46:49 +00005408 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00005409 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00005410 if( rc ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00005411 dropCell(pPage, pCur->idx, szOld);
drh7c717f72001-06-24 20:39:41 +00005412 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00005413 assert( pPage->leaf );
drh14acc042001-06-10 19:56:58 +00005414 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00005415 pCur->info.nSize = 0;
drh14acc042001-06-10 19:56:58 +00005416 }else{
drh4b70f112004-05-02 21:12:19 +00005417 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00005418 }
danielk1977a3ad5e72005-01-07 08:56:44 +00005419 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
danielk1977e80463b2004-11-03 03:01:16 +00005420 if( rc!=SQLITE_OK ) goto end_insert;
danielk1977ac245ec2005-01-14 13:50:11 +00005421 rc = balance(pPage, 1);
drh23e11ca2004-05-04 17:27:28 +00005422 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
drh3fc190c2001-09-14 03:24:23 +00005423 /* fflush(stdout); */
danielk1977299b1872004-11-22 10:02:10 +00005424 if( rc==SQLITE_OK ){
5425 moveToRoot(pCur);
5426 }
drh2e38c322004-09-03 18:38:44 +00005427end_insert:
5428 sqliteFree(newCell);
drh5e2f8b92001-05-28 00:41:15 +00005429 return rc;
5430}
5431
5432/*
drh4b70f112004-05-02 21:12:19 +00005433** Delete the entry that the cursor is pointing to. The cursor
5434** is left pointing at a random location.
drh3b7511c2001-05-26 13:15:44 +00005435*/
drh3aac2dd2004-04-26 14:10:20 +00005436int sqlite3BtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00005437 MemPage *pPage = pCur->pPage;
drh4b70f112004-05-02 21:12:19 +00005438 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00005439 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00005440 Pgno pgnoChild = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00005441 BtShared *pBt = pCur->pBtree->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005442
drh7aa128d2002-06-21 13:09:16 +00005443 assert( pPage->isInit );
danielk1977aef0bf62005-12-30 16:28:01 +00005444 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005445 /* Must start a transaction before doing a delete */
5446 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005447 }
drhf74b8d92002-09-01 23:20:45 +00005448 assert( !pBt->readOnly );
drhbd03cae2001-06-02 02:40:57 +00005449 if( pCur->idx >= pPage->nCell ){
5450 return SQLITE_ERROR; /* The cursor is not pointing to anything */
5451 }
drhecdc7532001-09-23 02:35:53 +00005452 if( !pCur->wrFlag ){
5453 return SQLITE_PERM; /* Did not open this cursor for writing */
5454 }
drh980b1a72006-08-16 16:42:48 +00005455 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005456 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5457 }
danielk1977da184232006-01-05 11:34:32 +00005458
5459 /* Restore the current cursor position (a no-op if the cursor is not in
5460 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
danielk19773b8a05f2007-03-19 17:44:26 +00005461 ** open on the same table. Then call sqlite3PagerWrite() on the page
danielk1977da184232006-01-05 11:34:32 +00005462 ** that the entry will be deleted from.
5463 */
5464 if(
drhbf700f32007-03-31 02:36:44 +00005465 (rc = restoreOrClearCursorPosition(pCur))!=0 ||
drhd1167392006-01-23 13:00:35 +00005466 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
danielk19773b8a05f2007-03-19 17:44:26 +00005467 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
danielk1977da184232006-01-05 11:34:32 +00005468 ){
5469 return rc;
5470 }
danielk1977e6efa742004-11-10 11:55:10 +00005471
5472 /* Locate the cell within it's page and leave pCell pointing to the
5473 ** data. The clearCell() call frees any overflow pages associated with the
5474 ** cell. The cell itself is still intact.
5475 */
danielk1977299b1872004-11-22 10:02:10 +00005476 pCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005477 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005478 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005479 }
danielk197728129562005-01-11 10:25:06 +00005480 rc = clearCell(pPage, pCell);
5481 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:10 +00005482
drh4b70f112004-05-02 21:12:19 +00005483 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00005484 /*
drh5e00f6c2001-09-13 13:46:56 +00005485 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00005486 ** do something we will leave a hole on an internal page.
5487 ** We have to fill the hole by moving in a cell from a leaf. The
5488 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00005489 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00005490 */
drh14acc042001-06-10 19:56:58 +00005491 BtCursor leafCur;
drh4b70f112004-05-02 21:12:19 +00005492 unsigned char *pNext;
drh02afc862006-01-20 18:10:57 +00005493 int szNext; /* The compiler warning is wrong: szNext is always
5494 ** initialized before use. Adding an extra initialization
5495 ** to silence the compiler slows down the code. */
danielk1977299b1872004-11-22 10:02:10 +00005496 int notUsed;
danielk19776b456a22005-03-21 04:04:02 +00005497 unsigned char *tempCell = 0;
drh8b18dd42004-05-12 19:18:15 +00005498 assert( !pPage->leafData );
drh14acc042001-06-10 19:56:58 +00005499 getTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00005500 rc = sqlite3BtreeNext(&leafCur, &notUsed);
danielk19776b456a22005-03-21 04:04:02 +00005501 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00005502 rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00005503 }
5504 if( rc==SQLITE_OK ){
5505 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
5506 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
5507 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
5508 pNext = findCell(leafCur.pPage, leafCur.idx);
5509 szNext = cellSizePtr(leafCur.pPage, pNext);
5510 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
5511 tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
5512 if( tempCell==0 ){
5513 rc = SQLITE_NOMEM;
5514 }
5515 }
5516 if( rc==SQLITE_OK ){
5517 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
5518 }
5519 if( rc==SQLITE_OK ){
5520 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
5521 rc = balance(pPage, 0);
5522 }
5523 if( rc==SQLITE_OK ){
5524 dropCell(leafCur.pPage, leafCur.idx, szNext);
5525 rc = balance(leafCur.pPage, 0);
5526 }
drh2e38c322004-09-03 18:38:44 +00005527 sqliteFree(tempCell);
drh8c42ca92001-06-22 19:15:00 +00005528 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00005529 }else{
danielk1977299b1872004-11-22 10:02:10 +00005530 TRACE(("DELETE: table=%d delete from leaf %d\n",
5531 pCur->pgnoRoot, pPage->pgno));
5532 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
danielk1977ac245ec2005-01-14 13:50:11 +00005533 rc = balance(pPage, 0);
drh5e2f8b92001-05-28 00:41:15 +00005534 }
danielk19776b456a22005-03-21 04:04:02 +00005535 if( rc==SQLITE_OK ){
5536 moveToRoot(pCur);
5537 }
drh5e2f8b92001-05-28 00:41:15 +00005538 return rc;
drh3b7511c2001-05-26 13:15:44 +00005539}
drh8b2f49b2001-06-08 00:21:52 +00005540
5541/*
drhc6b52df2002-01-04 03:09:29 +00005542** Create a new BTree table. Write into *piTable the page
5543** number for the root page of the new table.
5544**
drhab01f612004-05-22 02:55:23 +00005545** The type of type is determined by the flags parameter. Only the
5546** following values of flags are currently in use. Other values for
5547** flags might not work:
5548**
5549** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
5550** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00005551*/
danielk1977aef0bf62005-12-30 16:28:01 +00005552int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
5553 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005554 MemPage *pRoot;
5555 Pgno pgnoRoot;
5556 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005557 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005558 /* Must start a transaction first */
5559 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005560 }
danielk197728129562005-01-11 10:25:06 +00005561 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00005562
5563 /* It is illegal to create a table if any cursors are open on the
5564 ** database. This is because in auto-vacuum mode the backend may
5565 ** need to move a database page to make room for the new root-page.
5566 ** If an open cursor was using the page a problem would occur.
5567 */
5568 if( pBt->pCursor ){
5569 return SQLITE_LOCKED;
5570 }
5571
danielk1977003ba062004-11-04 02:57:33 +00005572#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:01 +00005573 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drh8b2f49b2001-06-08 00:21:52 +00005574 if( rc ) return rc;
danielk1977003ba062004-11-04 02:57:33 +00005575#else
danielk1977687566d2004-11-02 12:56:41 +00005576 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00005577 Pgno pgnoMove; /* Move a page here to make room for the root-page */
5578 MemPage *pPageMove; /* The page to move to. */
5579
danielk1977003ba062004-11-04 02:57:33 +00005580 /* Read the value of meta[3] from the database to determine where the
5581 ** root page of the new table should go. meta[3] is the largest root-page
5582 ** created so far, so the new root-page is (meta[3]+1).
5583 */
danielk1977aef0bf62005-12-30 16:28:01 +00005584 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00005585 if( rc!=SQLITE_OK ) return rc;
5586 pgnoRoot++;
5587
danielk1977599fcba2004-11-08 07:13:13 +00005588 /* The new root-page may not be allocated on a pointer-map page, or the
5589 ** PENDING_BYTE page.
5590 */
danielk1977266664d2006-02-10 08:24:21 +00005591 if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00005592 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00005593 pgnoRoot++;
5594 }
5595 assert( pgnoRoot>=3 );
5596
5597 /* Allocate a page. The page that currently resides at pgnoRoot will
5598 ** be moved to the allocated page (unless the allocated page happens
5599 ** to reside at pgnoRoot).
5600 */
drh4f0c5872007-03-26 22:05:01 +00005601 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00005602 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00005603 return rc;
5604 }
danielk1977003ba062004-11-04 02:57:33 +00005605
5606 if( pgnoMove!=pgnoRoot ){
danielk1977f35843b2007-04-07 15:03:17 +00005607 /* pgnoRoot is the page that will be used for the root-page of
5608 ** the new table (assuming an error did not occur). But we were
5609 ** allocated pgnoMove. If required (i.e. if it was not allocated
5610 ** by extending the file), the current page at position pgnoMove
5611 ** is already journaled.
5612 */
danielk1977003ba062004-11-04 02:57:33 +00005613 u8 eType;
5614 Pgno iPtrPage;
5615
5616 releasePage(pPageMove);
danielk1977f35843b2007-04-07 15:03:17 +00005617
5618 /* Move the page currently at pgnoRoot to pgnoMove. */
drh0787db62007-03-04 13:15:27 +00005619 rc = getPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005620 if( rc!=SQLITE_OK ){
5621 return rc;
5622 }
5623 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drhccae6022005-02-26 17:31:26 +00005624 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00005625 releasePage(pRoot);
5626 return rc;
5627 }
drhccae6022005-02-26 17:31:26 +00005628 assert( eType!=PTRMAP_ROOTPAGE );
5629 assert( eType!=PTRMAP_FREEPAGE );
danielk19773b8a05f2007-03-19 17:44:26 +00005630 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk19775fd057a2005-03-09 13:09:43 +00005631 if( rc!=SQLITE_OK ){
5632 releasePage(pRoot);
5633 return rc;
5634 }
danielk1977003ba062004-11-04 02:57:33 +00005635 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
5636 releasePage(pRoot);
danielk1977f35843b2007-04-07 15:03:17 +00005637
5638 /* Obtain the page at pgnoRoot */
danielk1977003ba062004-11-04 02:57:33 +00005639 if( rc!=SQLITE_OK ){
5640 return rc;
5641 }
drh0787db62007-03-04 13:15:27 +00005642 rc = getPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005643 if( rc!=SQLITE_OK ){
5644 return rc;
5645 }
danielk19773b8a05f2007-03-19 17:44:26 +00005646 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:33 +00005647 if( rc!=SQLITE_OK ){
5648 releasePage(pRoot);
5649 return rc;
5650 }
5651 }else{
5652 pRoot = pPageMove;
5653 }
5654
danielk197742741be2005-01-08 12:42:39 +00005655 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00005656 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
5657 if( rc ){
5658 releasePage(pRoot);
5659 return rc;
5660 }
danielk1977aef0bf62005-12-30 16:28:01 +00005661 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00005662 if( rc ){
5663 releasePage(pRoot);
5664 return rc;
5665 }
danielk197742741be2005-01-08 12:42:39 +00005666
danielk1977003ba062004-11-04 02:57:33 +00005667 }else{
drh4f0c5872007-03-26 22:05:01 +00005668 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00005669 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00005670 }
5671#endif
danielk19773b8a05f2007-03-19 17:44:26 +00005672 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhde647132004-05-07 17:57:49 +00005673 zeroPage(pRoot, flags | PTF_LEAF);
danielk19773b8a05f2007-03-19 17:44:26 +00005674 sqlite3PagerUnref(pRoot->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00005675 *piTable = (int)pgnoRoot;
5676 return SQLITE_OK;
5677}
5678
5679/*
5680** Erase the given database page and all its children. Return
5681** the page to the freelist.
5682*/
drh4b70f112004-05-02 21:12:19 +00005683static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:01 +00005684 BtShared *pBt, /* The BTree that contains the table */
drh4b70f112004-05-02 21:12:19 +00005685 Pgno pgno, /* Page number to clear */
5686 MemPage *pParent, /* Parent page. NULL for the root */
5687 int freePageFlag /* Deallocate page if true */
5688){
danielk19776b456a22005-03-21 04:04:02 +00005689 MemPage *pPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00005690 int rc;
drh4b70f112004-05-02 21:12:19 +00005691 unsigned char *pCell;
5692 int i;
drh8b2f49b2001-06-08 00:21:52 +00005693
danielk19773b8a05f2007-03-19 17:44:26 +00005694 if( pgno>sqlite3PagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00005695 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00005696 }
5697
drhde647132004-05-07 17:57:49 +00005698 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
danielk19776b456a22005-03-21 04:04:02 +00005699 if( rc ) goto cleardatabasepage_out;
drh4b70f112004-05-02 21:12:19 +00005700 for(i=0; i<pPage->nCell; i++){
drh43605152004-05-29 21:46:49 +00005701 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00005702 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005703 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00005704 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00005705 }
drh4b70f112004-05-02 21:12:19 +00005706 rc = clearCell(pPage, pCell);
danielk19776b456a22005-03-21 04:04:02 +00005707 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00005708 }
drha34b6762004-05-07 13:30:42 +00005709 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005710 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00005711 if( rc ) goto cleardatabasepage_out;
drh2aa679f2001-06-25 02:11:07 +00005712 }
5713 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00005714 rc = freePage(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00005715 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
drh3a4c1412004-05-09 20:40:11 +00005716 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00005717 }
danielk19776b456a22005-03-21 04:04:02 +00005718
5719cleardatabasepage_out:
drh4b70f112004-05-02 21:12:19 +00005720 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00005721 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005722}
5723
5724/*
drhab01f612004-05-22 02:55:23 +00005725** Delete all information from a single table in the database. iTable is
5726** the page number of the root of the table. After this routine returns,
5727** the root page is empty, but still exists.
5728**
5729** This routine will fail with SQLITE_LOCKED if there are any open
5730** read cursors on the table. Open write cursors are moved to the
5731** root of the table.
drh8b2f49b2001-06-08 00:21:52 +00005732*/
danielk1977aef0bf62005-12-30 16:28:01 +00005733int sqlite3BtreeClearTable(Btree *p, int iTable){
drh8b2f49b2001-06-08 00:21:52 +00005734 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005735 BtShared *pBt = p->pBt;
5736 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005737 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005738 }
drh980b1a72006-08-16 16:42:48 +00005739 rc = checkReadLocks(p, iTable, 0);
5740 if( rc ){
5741 return rc;
drhecdc7532001-09-23 02:35:53 +00005742 }
danielk1977ed429312006-01-19 08:43:31 +00005743
5744 /* Save the position of all cursors open on this table */
5745 if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
5746 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005747 }
danielk1977ed429312006-01-19 08:43:31 +00005748
5749 return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
drh8b2f49b2001-06-08 00:21:52 +00005750}
5751
5752/*
5753** Erase all information in a table and add the root of the table to
5754** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00005755** page 1) is never added to the freelist.
5756**
5757** This routine will fail with SQLITE_LOCKED if there are any open
5758** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00005759**
5760** If AUTOVACUUM is enabled and the page at iTable is not the last
5761** root page in the database file, then the last root page
5762** in the database file is moved into the slot formerly occupied by
5763** iTable and that last slot formerly occupied by the last root page
5764** is added to the freelist instead of iTable. In this say, all
5765** root pages are kept at the beginning of the database file, which
5766** is necessary for AUTOVACUUM to work right. *piMoved is set to the
5767** page number that used to be the last root page in the file before
5768** the move. If no page gets moved, *piMoved is set to 0.
5769** The last root page is recorded in meta[3] and the value of
5770** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00005771*/
danielk1977aef0bf62005-12-30 16:28:01 +00005772int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00005773 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00005774 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00005775 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:04 +00005776
danielk1977aef0bf62005-12-30 16:28:01 +00005777 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005778 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00005779 }
danielk1977a0bf2652004-11-04 14:30:04 +00005780
danielk1977e6efa742004-11-10 11:55:10 +00005781 /* It is illegal to drop a table if any cursors are open on the
5782 ** database. This is because in auto-vacuum mode the backend may
5783 ** need to move another root-page to fill a gap left by the deleted
5784 ** root page. If an open cursor was using this page a problem would
5785 ** occur.
5786 */
5787 if( pBt->pCursor ){
5788 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00005789 }
danielk1977a0bf2652004-11-04 14:30:04 +00005790
drh0787db62007-03-04 13:15:27 +00005791 rc = getPage(pBt, (Pgno)iTable, &pPage, 0);
drh2aa679f2001-06-25 02:11:07 +00005792 if( rc ) return rc;
danielk1977aef0bf62005-12-30 16:28:01 +00005793 rc = sqlite3BtreeClearTable(p, iTable);
danielk19776b456a22005-03-21 04:04:02 +00005794 if( rc ){
5795 releasePage(pPage);
5796 return rc;
5797 }
danielk1977a0bf2652004-11-04 14:30:04 +00005798
drh205f48e2004-11-05 00:43:11 +00005799 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00005800
drh4b70f112004-05-02 21:12:19 +00005801 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00005802#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00005803 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00005804 releasePage(pPage);
5805#else
5806 if( pBt->autoVacuum ){
5807 Pgno maxRootPgno;
danielk1977aef0bf62005-12-30 16:28:01 +00005808 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00005809 if( rc!=SQLITE_OK ){
5810 releasePage(pPage);
5811 return rc;
5812 }
5813
5814 if( iTable==maxRootPgno ){
5815 /* If the table being dropped is the table with the largest root-page
5816 ** number in the database, put the root page on the free list.
5817 */
5818 rc = freePage(pPage);
5819 releasePage(pPage);
5820 if( rc!=SQLITE_OK ){
5821 return rc;
5822 }
5823 }else{
5824 /* The table being dropped does not have the largest root-page
5825 ** number in the database. So move the page that does into the
5826 ** gap left by the deleted root-page.
5827 */
5828 MemPage *pMove;
5829 releasePage(pPage);
drh0787db62007-03-04 13:15:27 +00005830 rc = getPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00005831 if( rc!=SQLITE_OK ){
5832 return rc;
5833 }
5834 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
5835 releasePage(pMove);
5836 if( rc!=SQLITE_OK ){
5837 return rc;
5838 }
drh0787db62007-03-04 13:15:27 +00005839 rc = getPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00005840 if( rc!=SQLITE_OK ){
5841 return rc;
5842 }
5843 rc = freePage(pMove);
5844 releasePage(pMove);
5845 if( rc!=SQLITE_OK ){
5846 return rc;
5847 }
5848 *piMoved = maxRootPgno;
5849 }
5850
danielk1977599fcba2004-11-08 07:13:13 +00005851 /* Set the new 'max-root-page' value in the database header. This
5852 ** is the old value less one, less one more if that happens to
5853 ** be a root-page number, less one again if that is the
5854 ** PENDING_BYTE_PAGE.
5855 */
danielk197787a6e732004-11-05 12:58:25 +00005856 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00005857 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
5858 maxRootPgno--;
5859 }
danielk1977266664d2006-02-10 08:24:21 +00005860 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00005861 maxRootPgno--;
5862 }
danielk1977599fcba2004-11-08 07:13:13 +00005863 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
5864
danielk1977aef0bf62005-12-30 16:28:01 +00005865 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00005866 }else{
5867 rc = freePage(pPage);
5868 releasePage(pPage);
5869 }
5870#endif
drh2aa679f2001-06-25 02:11:07 +00005871 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00005872 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00005873 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00005874 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00005875 }
drh8b2f49b2001-06-08 00:21:52 +00005876 return rc;
5877}
5878
drh001bbcb2003-03-19 03:14:00 +00005879
drh8b2f49b2001-06-08 00:21:52 +00005880/*
drh23e11ca2004-05-04 17:27:28 +00005881** Read the meta-information out of a database file. Meta[0]
5882** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00005883** through meta[15] are available for use by higher layers. Meta[0]
5884** is read-only, the others are read/write.
5885**
5886** The schema layer numbers meta values differently. At the schema
5887** layer (and the SetCookie and ReadCookie opcodes) the number of
5888** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00005889*/
danielk1977aef0bf62005-12-30 16:28:01 +00005890int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
danielk19773b8a05f2007-03-19 17:44:26 +00005891 DbPage *pDbPage;
drh8b2f49b2001-06-08 00:21:52 +00005892 int rc;
drh4b70f112004-05-02 21:12:19 +00005893 unsigned char *pP1;
danielk1977aef0bf62005-12-30 16:28:01 +00005894 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005895
danielk1977da184232006-01-05 11:34:32 +00005896 /* Reading a meta-data value requires a read-lock on page 1 (and hence
5897 ** the sqlite_master table. We grab this lock regardless of whether or
5898 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
5899 ** 1 is treated as a special case by queryTableLock() and lockTable()).
5900 */
5901 rc = queryTableLock(p, 1, READ_LOCK);
5902 if( rc!=SQLITE_OK ){
5903 return rc;
5904 }
5905
drh23e11ca2004-05-04 17:27:28 +00005906 assert( idx>=0 && idx<=15 );
danielk19773b8a05f2007-03-19 17:44:26 +00005907 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00005908 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005909 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
drh23e11ca2004-05-04 17:27:28 +00005910 *pMeta = get4byte(&pP1[36 + idx*4]);
danielk19773b8a05f2007-03-19 17:44:26 +00005911 sqlite3PagerUnref(pDbPage);
drhae157872004-08-14 19:20:09 +00005912
danielk1977599fcba2004-11-08 07:13:13 +00005913 /* If autovacuumed is disabled in this build but we are trying to
5914 ** access an autovacuumed database, then make the database readonly.
5915 */
danielk1977003ba062004-11-04 02:57:33 +00005916#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00005917 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00005918#endif
drhae157872004-08-14 19:20:09 +00005919
danielk1977da184232006-01-05 11:34:32 +00005920 /* Grab the read-lock on page 1. */
5921 rc = lockTable(p, 1, READ_LOCK);
5922 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005923}
5924
5925/*
drh23e11ca2004-05-04 17:27:28 +00005926** Write meta-information back into the database. Meta[0] is
5927** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00005928*/
danielk1977aef0bf62005-12-30 16:28:01 +00005929int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
5930 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:19 +00005931 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00005932 int rc;
drh23e11ca2004-05-04 17:27:28 +00005933 assert( idx>=1 && idx<=15 );
danielk1977aef0bf62005-12-30 16:28:01 +00005934 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005935 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh5df72a52002-06-06 23:16:05 +00005936 }
drhde647132004-05-07 17:57:49 +00005937 assert( pBt->pPage1!=0 );
5938 pP1 = pBt->pPage1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00005939 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
drh4b70f112004-05-02 21:12:19 +00005940 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00005941 put4byte(&pP1[36 + idx*4], iMeta);
drh8b2f49b2001-06-08 00:21:52 +00005942 return SQLITE_OK;
5943}
drh8c42ca92001-06-22 19:15:00 +00005944
drhf328bc82004-05-10 23:29:49 +00005945/*
5946** Return the flag byte at the beginning of the page that the cursor
5947** is currently pointing to.
5948*/
5949int sqlite3BtreeFlags(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00005950 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
drh777e4c42006-01-13 04:31:58 +00005951 ** restoreOrClearCursorPosition() here.
danielk1977da184232006-01-05 11:34:32 +00005952 */
drhf328bc82004-05-10 23:29:49 +00005953 MemPage *pPage = pCur->pPage;
5954 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
5955}
5956
danielk1977b5402fb2005-01-12 07:15:04 +00005957#ifdef SQLITE_DEBUG
drh8c42ca92001-06-22 19:15:00 +00005958/*
5959** Print a disassembly of the given page on standard output. This routine
5960** is used for debugging and testing only.
5961*/
danielk1977aef0bf62005-12-30 16:28:01 +00005962static int btreePageDump(BtShared *pBt, int pgno, int recursive, MemPage *pParent){
drh8c42ca92001-06-22 19:15:00 +00005963 int rc;
5964 MemPage *pPage;
drhc8629a12004-05-08 20:07:40 +00005965 int i, j, c;
drh8c42ca92001-06-22 19:15:00 +00005966 int nFree;
5967 u16 idx;
drhab9f7f12004-05-08 10:56:11 +00005968 int hdr;
drh43605152004-05-29 21:46:49 +00005969 int nCell;
drha2fce642004-06-05 00:01:44 +00005970 int isInit;
drhab9f7f12004-05-08 10:56:11 +00005971 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00005972 char range[20];
5973 unsigned char payload[20];
drhab9f7f12004-05-08 10:56:11 +00005974
drh0787db62007-03-04 13:15:27 +00005975 rc = getPage(pBt, (Pgno)pgno, &pPage, 0);
drha2fce642004-06-05 00:01:44 +00005976 isInit = pPage->isInit;
5977 if( pPage->isInit==0 ){
danielk1977c7dc7532004-11-17 10:22:03 +00005978 initPage(pPage, pParent);
drha2fce642004-06-05 00:01:44 +00005979 }
drh8c42ca92001-06-22 19:15:00 +00005980 if( rc ){
5981 return rc;
5982 }
drhab9f7f12004-05-08 10:56:11 +00005983 hdr = pPage->hdrOffset;
5984 data = pPage->aData;
drhc8629a12004-05-08 20:07:40 +00005985 c = data[hdr];
drh8b18dd42004-05-12 19:18:15 +00005986 pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
drhc8629a12004-05-08 20:07:40 +00005987 pPage->zeroData = (c & PTF_ZERODATA)!=0;
drh8b18dd42004-05-12 19:18:15 +00005988 pPage->leafData = (c & PTF_LEAFDATA)!=0;
drhc8629a12004-05-08 20:07:40 +00005989 pPage->leaf = (c & PTF_LEAF)!=0;
drh8b18dd42004-05-12 19:18:15 +00005990 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
drh43605152004-05-29 21:46:49 +00005991 nCell = get2byte(&data[hdr+3]);
drhfe63d1c2004-09-08 20:13:04 +00005992 sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
drh43605152004-05-29 21:46:49 +00005993 data[hdr], data[hdr+7],
drhda200cc2004-05-09 11:51:38 +00005994 (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
drhab9f7f12004-05-08 10:56:11 +00005995 assert( hdr == (pgno==1 ? 100 : 0) );
drh43605152004-05-29 21:46:49 +00005996 idx = hdr + 12 - pPage->leaf*4;
5997 for(i=0; i<nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00005998 CellInfo info;
drh4b70f112004-05-02 21:12:19 +00005999 Pgno child;
drh43605152004-05-29 21:46:49 +00006000 unsigned char *pCell;
drh6f11bef2004-05-13 01:12:56 +00006001 int sz;
drh43605152004-05-29 21:46:49 +00006002 int addr;
drh6f11bef2004-05-13 01:12:56 +00006003
drh43605152004-05-29 21:46:49 +00006004 addr = get2byte(&data[idx + 2*i]);
6005 pCell = &data[addr];
6006 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006007 sz = info.nSize;
drh43605152004-05-29 21:46:49 +00006008 sprintf(range,"%d..%d", addr, addr+sz-1);
drh4b70f112004-05-02 21:12:19 +00006009 if( pPage->leaf ){
6010 child = 0;
6011 }else{
drh43605152004-05-29 21:46:49 +00006012 child = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00006013 }
drh6f11bef2004-05-13 01:12:56 +00006014 sz = info.nData;
6015 if( !pPage->intKey ) sz += info.nKey;
drh8c42ca92001-06-22 19:15:00 +00006016 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
drh6f11bef2004-05-13 01:12:56 +00006017 memcpy(payload, &pCell[info.nHeader], sz);
drh8c42ca92001-06-22 19:15:00 +00006018 for(j=0; j<sz; j++){
6019 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
6020 }
6021 payload[sz] = 0;
drhfe63d1c2004-09-08 20:13:04 +00006022 sqlite3DebugPrintf(
drh6f11bef2004-05-13 01:12:56 +00006023 "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
6024 i, range, child, info.nKey, info.nData, payload
drh8c42ca92001-06-22 19:15:00 +00006025 );
drh8c42ca92001-06-22 19:15:00 +00006026 }
drh4b70f112004-05-02 21:12:19 +00006027 if( !pPage->leaf ){
drhfe63d1c2004-09-08 20:13:04 +00006028 sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
drh4b70f112004-05-02 21:12:19 +00006029 }
drh8c42ca92001-06-22 19:15:00 +00006030 nFree = 0;
6031 i = 0;
drhab9f7f12004-05-08 10:56:11 +00006032 idx = get2byte(&data[hdr+1]);
drhb6f41482004-05-14 01:58:11 +00006033 while( idx>0 && idx<pPage->pBt->usableSize ){
drhab9f7f12004-05-08 10:56:11 +00006034 int sz = get2byte(&data[idx+2]);
drh4b70f112004-05-02 21:12:19 +00006035 sprintf(range,"%d..%d", idx, idx+sz-1);
6036 nFree += sz;
drhfe63d1c2004-09-08 20:13:04 +00006037 sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
drh4b70f112004-05-02 21:12:19 +00006038 i, range, sz, nFree);
drhab9f7f12004-05-08 10:56:11 +00006039 idx = get2byte(&data[idx]);
drh2aa679f2001-06-25 02:11:07 +00006040 i++;
drh8c42ca92001-06-22 19:15:00 +00006041 }
6042 if( idx!=0 ){
drhfe63d1c2004-09-08 20:13:04 +00006043 sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
drh8c42ca92001-06-22 19:15:00 +00006044 }
drha34b6762004-05-07 13:30:42 +00006045 if( recursive && !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006046 for(i=0; i<nCell; i++){
6047 unsigned char *pCell = findCell(pPage, i);
danielk1977c7dc7532004-11-17 10:22:03 +00006048 btreePageDump(pBt, get4byte(pCell), 1, pPage);
drha34b6762004-05-07 13:30:42 +00006049 idx = get2byte(pCell);
drh6019e162001-07-02 17:51:45 +00006050 }
danielk1977c7dc7532004-11-17 10:22:03 +00006051 btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
drh6019e162001-07-02 17:51:45 +00006052 }
drha2fce642004-06-05 00:01:44 +00006053 pPage->isInit = isInit;
danielk19773b8a05f2007-03-19 17:44:26 +00006054 sqlite3PagerUnref(pPage->pDbPage);
drh3644f082004-05-10 18:45:09 +00006055 fflush(stdout);
drh8c42ca92001-06-22 19:15:00 +00006056 return SQLITE_OK;
6057}
danielk1977aef0bf62005-12-30 16:28:01 +00006058int sqlite3BtreePageDump(Btree *p, int pgno, int recursive){
6059 return btreePageDump(p->pBt, pgno, recursive, 0);
danielk1977c7dc7532004-11-17 10:22:03 +00006060}
drhaaab5722002-02-19 13:39:21 +00006061#endif
drh8c42ca92001-06-22 19:15:00 +00006062
drh77bba592006-08-13 18:39:26 +00006063#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drh8c42ca92001-06-22 19:15:00 +00006064/*
drh2aa679f2001-06-25 02:11:07 +00006065** Fill aResult[] with information about the entry and page that the
6066** cursor is pointing to.
6067**
6068** aResult[0] = The page number
6069** aResult[1] = The entry number
6070** aResult[2] = Total number of entries on this page
drh3e27c022004-07-23 00:01:38 +00006071** aResult[3] = Cell size (local payload + header)
drh2aa679f2001-06-25 02:11:07 +00006072** aResult[4] = Number of free bytes on this page
6073** aResult[5] = Number of free blocks on the page
drh3e27c022004-07-23 00:01:38 +00006074** aResult[6] = Total payload size (local + overflow)
6075** aResult[7] = Header size in bytes
6076** aResult[8] = Local payload size
6077** aResult[9] = Parent page number
drh50c67062007-02-10 19:22:35 +00006078** aResult[10]= Page number of the first overflow page
drh5eddca62001-06-30 21:53:53 +00006079**
6080** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00006081*/
drh3e27c022004-07-23 00:01:38 +00006082int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
drh2aa679f2001-06-25 02:11:07 +00006083 int cnt, idx;
6084 MemPage *pPage = pCur->pPage;
drh3e27c022004-07-23 00:01:38 +00006085 BtCursor tmpCur;
drhda200cc2004-05-09 11:51:38 +00006086
drhbf700f32007-03-31 02:36:44 +00006087 int rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00006088 if( rc!=SQLITE_OK ){
6089 return rc;
6090 }
6091
drh4b70f112004-05-02 21:12:19 +00006092 assert( pPage->isInit );
drh3e27c022004-07-23 00:01:38 +00006093 getTempCursor(pCur, &tmpCur);
6094 while( upCnt-- ){
6095 moveToParent(&tmpCur);
6096 }
6097 pPage = tmpCur.pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00006098 aResult[0] = sqlite3PagerPagenumber(pPage->pDbPage);
drh91025292004-05-03 19:49:32 +00006099 assert( aResult[0]==pPage->pgno );
drh3e27c022004-07-23 00:01:38 +00006100 aResult[1] = tmpCur.idx;
drh2aa679f2001-06-25 02:11:07 +00006101 aResult[2] = pPage->nCell;
drh3e27c022004-07-23 00:01:38 +00006102 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
6103 getCellInfo(&tmpCur);
6104 aResult[3] = tmpCur.info.nSize;
6105 aResult[6] = tmpCur.info.nData;
6106 aResult[7] = tmpCur.info.nHeader;
6107 aResult[8] = tmpCur.info.nLocal;
drh2aa679f2001-06-25 02:11:07 +00006108 }else{
6109 aResult[3] = 0;
6110 aResult[6] = 0;
drh3e27c022004-07-23 00:01:38 +00006111 aResult[7] = 0;
6112 aResult[8] = 0;
drh2aa679f2001-06-25 02:11:07 +00006113 }
6114 aResult[4] = pPage->nFree;
6115 cnt = 0;
drh4b70f112004-05-02 21:12:19 +00006116 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
drhb6f41482004-05-14 01:58:11 +00006117 while( idx>0 && idx<pPage->pBt->usableSize ){
drh2aa679f2001-06-25 02:11:07 +00006118 cnt++;
drh4b70f112004-05-02 21:12:19 +00006119 idx = get2byte(&pPage->aData[idx]);
drh2aa679f2001-06-25 02:11:07 +00006120 }
6121 aResult[5] = cnt;
drh3e27c022004-07-23 00:01:38 +00006122 if( pPage->pParent==0 || isRootPage(pPage) ){
6123 aResult[9] = 0;
6124 }else{
6125 aResult[9] = pPage->pParent->pgno;
6126 }
drh50c67062007-02-10 19:22:35 +00006127 if( tmpCur.info.iOverflow ){
6128 aResult[10] = get4byte(&tmpCur.info.pCell[tmpCur.info.iOverflow]);
6129 }else{
6130 aResult[10] = 0;
6131 }
drh3e27c022004-07-23 00:01:38 +00006132 releaseTempCursor(&tmpCur);
drh8c42ca92001-06-22 19:15:00 +00006133 return SQLITE_OK;
6134}
drhaaab5722002-02-19 13:39:21 +00006135#endif
drhdd793422001-06-28 01:54:48 +00006136
drhdd793422001-06-28 01:54:48 +00006137/*
drh5eddca62001-06-30 21:53:53 +00006138** Return the pager associated with a BTree. This routine is used for
6139** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00006140*/
danielk1977aef0bf62005-12-30 16:28:01 +00006141Pager *sqlite3BtreePager(Btree *p){
6142 return p->pBt->pPager;
drhdd793422001-06-28 01:54:48 +00006143}
drh5eddca62001-06-30 21:53:53 +00006144
6145/*
6146** This structure is passed around through all the sanity checking routines
6147** in order to keep track of some global state information.
6148*/
drhaaab5722002-02-19 13:39:21 +00006149typedef struct IntegrityCk IntegrityCk;
6150struct IntegrityCk {
danielk1977aef0bf62005-12-30 16:28:01 +00006151 BtShared *pBt; /* The tree being checked out */
drh1dcdbc02007-01-27 02:24:54 +00006152 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
6153 int nPage; /* Number of pages in the database */
6154 int *anRef; /* Number of times each page is referenced */
6155 int mxErr; /* Stop accumulating errors when this reaches zero */
6156 char *zErrMsg; /* An error message. NULL if no errors seen. */
6157 int nErr; /* Number of messages written to zErrMsg so far */
drh5eddca62001-06-30 21:53:53 +00006158};
6159
drhb7f91642004-10-31 02:22:47 +00006160#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006161/*
6162** Append a message to the error message string.
6163*/
drh2e38c322004-09-03 18:38:44 +00006164static void checkAppendMsg(
6165 IntegrityCk *pCheck,
6166 char *zMsg1,
6167 const char *zFormat,
6168 ...
6169){
6170 va_list ap;
6171 char *zMsg2;
drh1dcdbc02007-01-27 02:24:54 +00006172 if( !pCheck->mxErr ) return;
6173 pCheck->mxErr--;
6174 pCheck->nErr++;
drh2e38c322004-09-03 18:38:44 +00006175 va_start(ap, zFormat);
6176 zMsg2 = sqlite3VMPrintf(zFormat, ap);
6177 va_end(ap);
6178 if( zMsg1==0 ) zMsg1 = "";
drh5eddca62001-06-30 21:53:53 +00006179 if( pCheck->zErrMsg ){
6180 char *zOld = pCheck->zErrMsg;
6181 pCheck->zErrMsg = 0;
danielk19774adee202004-05-08 08:23:19 +00006182 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00006183 sqliteFree(zOld);
6184 }else{
danielk19774adee202004-05-08 08:23:19 +00006185 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00006186 }
drh2e38c322004-09-03 18:38:44 +00006187 sqliteFree(zMsg2);
drh5eddca62001-06-30 21:53:53 +00006188}
drhb7f91642004-10-31 02:22:47 +00006189#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006190
drhb7f91642004-10-31 02:22:47 +00006191#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006192/*
6193** Add 1 to the reference count for page iPage. If this is the second
6194** reference to the page, add an error message to pCheck->zErrMsg.
6195** Return 1 if there are 2 ore more references to the page and 0 if
6196** if this is the first reference to the page.
6197**
6198** Also check that the page number is in bounds.
6199*/
drhaaab5722002-02-19 13:39:21 +00006200static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00006201 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00006202 if( iPage>pCheck->nPage || iPage<0 ){
drh2e38c322004-09-03 18:38:44 +00006203 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006204 return 1;
6205 }
6206 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00006207 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006208 return 1;
6209 }
6210 return (pCheck->anRef[iPage]++)>1;
6211}
6212
danielk1977afcdd022004-10-31 16:25:42 +00006213#ifndef SQLITE_OMIT_AUTOVACUUM
6214/*
6215** Check that the entry in the pointer-map for page iChild maps to
6216** page iParent, pointer type ptrType. If not, append an error message
6217** to pCheck.
6218*/
6219static void checkPtrmap(
6220 IntegrityCk *pCheck, /* Integrity check context */
6221 Pgno iChild, /* Child page number */
6222 u8 eType, /* Expected pointer map type */
6223 Pgno iParent, /* Expected pointer map parent page number */
6224 char *zContext /* Context description (used for error msg) */
6225){
6226 int rc;
6227 u8 ePtrmapType;
6228 Pgno iPtrmapParent;
6229
6230 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6231 if( rc!=SQLITE_OK ){
6232 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6233 return;
6234 }
6235
6236 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6237 checkAppendMsg(pCheck, zContext,
6238 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6239 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6240 }
6241}
6242#endif
6243
drh5eddca62001-06-30 21:53:53 +00006244/*
6245** Check the integrity of the freelist or of an overflow page list.
6246** Verify that the number of pages on the list is N.
6247*/
drh30e58752002-03-02 20:41:57 +00006248static void checkList(
6249 IntegrityCk *pCheck, /* Integrity checking context */
6250 int isFreeList, /* True for a freelist. False for overflow page list */
6251 int iPage, /* Page number for first page in the list */
6252 int N, /* Expected number of pages in the list */
6253 char *zContext /* Context for error messages */
6254){
6255 int i;
drh3a4c1412004-05-09 20:40:11 +00006256 int expected = N;
6257 int iFirst = iPage;
drh1dcdbc02007-01-27 02:24:54 +00006258 while( N-- > 0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:26 +00006259 DbPage *pOvflPage;
6260 unsigned char *pOvflData;
drh5eddca62001-06-30 21:53:53 +00006261 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00006262 checkAppendMsg(pCheck, zContext,
6263 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00006264 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00006265 break;
6266 }
6267 if( checkRef(pCheck, iPage, zContext) ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006268 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
drh2e38c322004-09-03 18:38:44 +00006269 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006270 break;
6271 }
danielk19773b8a05f2007-03-19 17:44:26 +00006272 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:57 +00006273 if( isFreeList ){
danielk19773b8a05f2007-03-19 17:44:26 +00006274 int n = get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:41 +00006275#ifndef SQLITE_OMIT_AUTOVACUUM
6276 if( pCheck->pBt->autoVacuum ){
6277 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6278 }
6279#endif
drh855eb1c2004-08-31 13:45:11 +00006280 if( n>pCheck->pBt->usableSize/4-8 ){
drh2e38c322004-09-03 18:38:44 +00006281 checkAppendMsg(pCheck, zContext,
6282 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00006283 N--;
6284 }else{
6285 for(i=0; i<n; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006286 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:41 +00006287#ifndef SQLITE_OMIT_AUTOVACUUM
6288 if( pCheck->pBt->autoVacuum ){
6289 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6290 }
6291#endif
6292 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00006293 }
6294 N -= n;
drh30e58752002-03-02 20:41:57 +00006295 }
drh30e58752002-03-02 20:41:57 +00006296 }
danielk1977afcdd022004-10-31 16:25:42 +00006297#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006298 else{
6299 /* If this database supports auto-vacuum and iPage is not the last
6300 ** page in this overflow list, check that the pointer-map entry for
6301 ** the following page matches iPage.
6302 */
6303 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00006304 i = get4byte(pOvflData);
danielk1977687566d2004-11-02 12:56:41 +00006305 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6306 }
danielk1977afcdd022004-10-31 16:25:42 +00006307 }
6308#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006309 iPage = get4byte(pOvflData);
6310 sqlite3PagerUnref(pOvflPage);
drh5eddca62001-06-30 21:53:53 +00006311 }
6312}
drhb7f91642004-10-31 02:22:47 +00006313#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006314
drhb7f91642004-10-31 02:22:47 +00006315#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006316/*
6317** Do various sanity checks on a single page of a tree. Return
6318** the tree depth. Root pages return 0. Parents of root pages
6319** return 1, and so forth.
6320**
6321** These checks are done:
6322**
6323** 1. Make sure that cells and freeblocks do not overlap
6324** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00006325** NO 2. Make sure cell keys are in order.
6326** NO 3. Make sure no key is less than or equal to zLowerBound.
6327** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00006328** 5. Check the integrity of overflow pages.
6329** 6. Recursively call checkTreePage on all children.
6330** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00006331** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00006332** the root of the tree.
6333*/
6334static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00006335 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00006336 int iPage, /* Page number of the page to check */
6337 MemPage *pParent, /* Parent page */
drh74161702006-02-24 02:53:49 +00006338 char *zParentContext /* Parent context */
drh5eddca62001-06-30 21:53:53 +00006339){
6340 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00006341 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00006342 int hdr, cellStart;
6343 int nCell;
drhda200cc2004-05-09 11:51:38 +00006344 u8 *data;
danielk1977aef0bf62005-12-30 16:28:01 +00006345 BtShared *pBt;
drh4f26bb62005-09-08 14:17:20 +00006346 int usableSize;
drh5eddca62001-06-30 21:53:53 +00006347 char zContext[100];
drh2e38c322004-09-03 18:38:44 +00006348 char *hit;
drh5eddca62001-06-30 21:53:53 +00006349
danielk1977ef73ee92004-11-06 12:26:07 +00006350 sprintf(zContext, "Page %d: ", iPage);
6351
drh5eddca62001-06-30 21:53:53 +00006352 /* Check that the page exists
6353 */
drhd9cb6ac2005-10-20 07:28:17 +00006354 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00006355 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00006356 if( iPage==0 ) return 0;
6357 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh0787db62007-03-04 13:15:27 +00006358 if( (rc = getPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
drh2e38c322004-09-03 18:38:44 +00006359 checkAppendMsg(pCheck, zContext,
6360 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00006361 return 0;
6362 }
drh4b70f112004-05-02 21:12:19 +00006363 if( (rc = initPage(pPage, pParent))!=0 ){
drh2e38c322004-09-03 18:38:44 +00006364 checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00006365 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00006366 return 0;
6367 }
6368
6369 /* Check out all the cells.
6370 */
6371 depth = 0;
drh1dcdbc02007-01-27 02:24:54 +00006372 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
drh6f11bef2004-05-13 01:12:56 +00006373 u8 *pCell;
6374 int sz;
6375 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00006376
6377 /* Check payload overflow pages
6378 */
drh3a4c1412004-05-09 20:40:11 +00006379 sprintf(zContext, "On tree page %d cell %d: ", iPage, i);
drh43605152004-05-29 21:46:49 +00006380 pCell = findCell(pPage,i);
6381 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006382 sz = info.nData;
6383 if( !pPage->intKey ) sz += info.nKey;
drh72365832007-03-06 15:53:44 +00006384 assert( sz==info.nPayload );
drh6f11bef2004-05-13 01:12:56 +00006385 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00006386 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00006387 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6388#ifndef SQLITE_OMIT_AUTOVACUUM
6389 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006390 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00006391 }
6392#endif
6393 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00006394 }
6395
6396 /* Check sanity of left child page.
6397 */
drhda200cc2004-05-09 11:51:38 +00006398 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006399 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00006400#ifndef SQLITE_OMIT_AUTOVACUUM
6401 if( pBt->autoVacuum ){
6402 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
6403 }
6404#endif
drh74161702006-02-24 02:53:49 +00006405 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
drhda200cc2004-05-09 11:51:38 +00006406 if( i>0 && d2!=depth ){
6407 checkAppendMsg(pCheck, zContext, "Child page depth differs");
6408 }
6409 depth = d2;
drh5eddca62001-06-30 21:53:53 +00006410 }
drh5eddca62001-06-30 21:53:53 +00006411 }
drhda200cc2004-05-09 11:51:38 +00006412 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006413 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drhda200cc2004-05-09 11:51:38 +00006414 sprintf(zContext, "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00006415#ifndef SQLITE_OMIT_AUTOVACUUM
6416 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006417 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00006418 }
6419#endif
drh74161702006-02-24 02:53:49 +00006420 checkTreePage(pCheck, pgno, pPage, zContext);
drhda200cc2004-05-09 11:51:38 +00006421 }
drh5eddca62001-06-30 21:53:53 +00006422
6423 /* Check for complete coverage of the page
6424 */
drhda200cc2004-05-09 11:51:38 +00006425 data = pPage->aData;
6426 hdr = pPage->hdrOffset;
drh2e38c322004-09-03 18:38:44 +00006427 hit = sqliteMalloc( usableSize );
6428 if( hit ){
6429 memset(hit, 1, get2byte(&data[hdr+5]));
6430 nCell = get2byte(&data[hdr+3]);
6431 cellStart = hdr + 12 - 4*pPage->leaf;
6432 for(i=0; i<nCell; i++){
6433 int pc = get2byte(&data[cellStart+i*2]);
6434 int size = cellSizePtr(pPage, &data[pc]);
6435 int j;
danielk19777701e812005-01-10 12:59:51 +00006436 if( (pc+size-1)>=usableSize || pc<0 ){
6437 checkAppendMsg(pCheck, 0,
6438 "Corruption detected in cell %d on page %d",i,iPage,0);
6439 }else{
6440 for(j=pc+size-1; j>=pc; j--) hit[j]++;
6441 }
drh2e38c322004-09-03 18:38:44 +00006442 }
6443 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
6444 cnt++){
6445 int size = get2byte(&data[i+2]);
6446 int j;
danielk19777701e812005-01-10 12:59:51 +00006447 if( (i+size-1)>=usableSize || i<0 ){
6448 checkAppendMsg(pCheck, 0,
6449 "Corruption detected in cell %d on page %d",i,iPage,0);
6450 }else{
6451 for(j=i+size-1; j>=i; j--) hit[j]++;
6452 }
drh2e38c322004-09-03 18:38:44 +00006453 i = get2byte(&data[i]);
6454 }
6455 for(i=cnt=0; i<usableSize; i++){
6456 if( hit[i]==0 ){
6457 cnt++;
6458 }else if( hit[i]>1 ){
6459 checkAppendMsg(pCheck, 0,
6460 "Multiple uses for byte %d of page %d", i, iPage);
6461 break;
6462 }
6463 }
6464 if( cnt!=data[hdr+7] ){
6465 checkAppendMsg(pCheck, 0,
6466 "Fragmented space is %d byte reported as %d on page %d",
6467 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00006468 }
6469 }
drh2e38c322004-09-03 18:38:44 +00006470 sqliteFree(hit);
drh6019e162001-07-02 17:51:45 +00006471
drh4b70f112004-05-02 21:12:19 +00006472 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00006473 return depth+1;
drh5eddca62001-06-30 21:53:53 +00006474}
drhb7f91642004-10-31 02:22:47 +00006475#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006476
drhb7f91642004-10-31 02:22:47 +00006477#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006478/*
6479** This routine does a complete check of the given BTree file. aRoot[] is
6480** an array of pages numbers were each page number is the root page of
6481** a table. nRoot is the number of entries in aRoot.
6482**
6483** If everything checks out, this routine returns NULL. If something is
6484** amiss, an error message is written into memory obtained from malloc()
6485** and a pointer to that error message is returned. The calling function
6486** is responsible for freeing the error message when it is done.
6487*/
drh1dcdbc02007-01-27 02:24:54 +00006488char *sqlite3BtreeIntegrityCheck(
6489 Btree *p, /* The btree to be checked */
6490 int *aRoot, /* An array of root pages numbers for individual trees */
6491 int nRoot, /* Number of entries in aRoot[] */
6492 int mxErr, /* Stop reporting errors after this many */
6493 int *pnErr /* Write number of errors seen to this variable */
6494){
drh5eddca62001-06-30 21:53:53 +00006495 int i;
6496 int nRef;
drhaaab5722002-02-19 13:39:21 +00006497 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:01 +00006498 BtShared *pBt = p->pBt;
drh5eddca62001-06-30 21:53:53 +00006499
danielk19773b8a05f2007-03-19 17:44:26 +00006500 nRef = sqlite3PagerRefcount(pBt->pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00006501 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
drhefc251d2001-07-01 22:12:01 +00006502 return sqliteStrDup("Unable to acquire a read lock on the database");
6503 }
drh5eddca62001-06-30 21:53:53 +00006504 sCheck.pBt = pBt;
6505 sCheck.pPager = pBt->pPager;
danielk19773b8a05f2007-03-19 17:44:26 +00006506 sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager);
drh1dcdbc02007-01-27 02:24:54 +00006507 sCheck.mxErr = mxErr;
6508 sCheck.nErr = 0;
6509 *pnErr = 0;
drh0de8c112002-07-06 16:32:14 +00006510 if( sCheck.nPage==0 ){
6511 unlockBtreeIfUnused(pBt);
6512 return 0;
6513 }
drh8c1238a2003-01-02 14:43:55 +00006514 sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00006515 if( !sCheck.anRef ){
6516 unlockBtreeIfUnused(pBt);
drh1dcdbc02007-01-27 02:24:54 +00006517 *pnErr = 1;
danielk1977ac245ec2005-01-14 13:50:11 +00006518 return sqlite3MPrintf("Unable to malloc %d bytes",
6519 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
6520 }
drhda200cc2004-05-09 11:51:38 +00006521 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00006522 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00006523 if( i<=sCheck.nPage ){
6524 sCheck.anRef[i] = 1;
6525 }
drh5eddca62001-06-30 21:53:53 +00006526 sCheck.zErrMsg = 0;
6527
6528 /* Check the integrity of the freelist
6529 */
drha34b6762004-05-07 13:30:42 +00006530 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
6531 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00006532
6533 /* Check all the tables.
6534 */
drh1dcdbc02007-01-27 02:24:54 +00006535 for(i=0; i<nRoot && sCheck.mxErr; i++){
drh4ff6dfa2002-03-03 23:06:00 +00006536 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00006537#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006538 if( pBt->autoVacuum && aRoot[i]>1 ){
6539 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
6540 }
6541#endif
drh74161702006-02-24 02:53:49 +00006542 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
drh5eddca62001-06-30 21:53:53 +00006543 }
6544
6545 /* Make sure every page in the file is referenced
6546 */
drh1dcdbc02007-01-27 02:24:54 +00006547 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:42 +00006548#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00006549 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00006550 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00006551 }
danielk1977afcdd022004-10-31 16:25:42 +00006552#else
6553 /* If the database supports auto-vacuum, make sure no tables contain
6554 ** references to pointer-map pages.
6555 */
6556 if( sCheck.anRef[i]==0 &&
danielk1977266664d2006-02-10 08:24:21 +00006557 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006558 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
6559 }
6560 if( sCheck.anRef[i]!=0 &&
danielk1977266664d2006-02-10 08:24:21 +00006561 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006562 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
6563 }
6564#endif
drh5eddca62001-06-30 21:53:53 +00006565 }
6566
6567 /* Make sure this analysis did not leave any unref() pages
6568 */
drh5e00f6c2001-09-13 13:46:56 +00006569 unlockBtreeIfUnused(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +00006570 if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
drh2e38c322004-09-03 18:38:44 +00006571 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00006572 "Outstanding page count goes from %d to %d during this analysis",
danielk19773b8a05f2007-03-19 17:44:26 +00006573 nRef, sqlite3PagerRefcount(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00006574 );
drh5eddca62001-06-30 21:53:53 +00006575 }
6576
6577 /* Clean up and report errors.
6578 */
6579 sqliteFree(sCheck.anRef);
drh1dcdbc02007-01-27 02:24:54 +00006580 *pnErr = sCheck.nErr;
drh5eddca62001-06-30 21:53:53 +00006581 return sCheck.zErrMsg;
6582}
drhb7f91642004-10-31 02:22:47 +00006583#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00006584
drh73509ee2003-04-06 20:44:45 +00006585/*
6586** Return the full pathname of the underlying database file.
6587*/
danielk1977aef0bf62005-12-30 16:28:01 +00006588const char *sqlite3BtreeGetFilename(Btree *p){
6589 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006590 return sqlite3PagerFilename(p->pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00006591}
6592
6593/*
danielk19775865e3d2004-06-14 06:03:57 +00006594** Return the pathname of the directory that contains the database file.
6595*/
danielk1977aef0bf62005-12-30 16:28:01 +00006596const char *sqlite3BtreeGetDirname(Btree *p){
6597 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006598 return sqlite3PagerDirname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006599}
6600
6601/*
6602** Return the pathname of the journal file for this database. The return
6603** value of this routine is the same regardless of whether the journal file
6604** has been created or not.
6605*/
danielk1977aef0bf62005-12-30 16:28:01 +00006606const char *sqlite3BtreeGetJournalname(Btree *p){
6607 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006608 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006609}
6610
drhb7f91642004-10-31 02:22:47 +00006611#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00006612/*
drhf7c57532003-04-25 13:22:51 +00006613** Copy the complete content of pBtFrom into pBtTo. A transaction
6614** must be active for both files.
6615**
6616** The size of file pBtFrom may be reduced by this operation.
drh43605152004-05-29 21:46:49 +00006617** If anything goes wrong, the transaction on pBtFrom is rolled back.
drh73509ee2003-04-06 20:44:45 +00006618*/
danielk1977aef0bf62005-12-30 16:28:01 +00006619int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
drhf7c57532003-04-25 13:22:51 +00006620 int rc = SQLITE_OK;
drh50f2f432005-09-16 11:32:18 +00006621 Pgno i, nPage, nToPage, iSkip;
drhf7c57532003-04-25 13:22:51 +00006622
danielk1977aef0bf62005-12-30 16:28:01 +00006623 BtShared *pBtTo = pTo->pBt;
6624 BtShared *pBtFrom = pFrom->pBt;
6625
6626 if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
danielk1977ee5741e2004-05-31 10:01:34 +00006627 return SQLITE_ERROR;
6628 }
drhf7c57532003-04-25 13:22:51 +00006629 if( pBtTo->pCursor ) return SQLITE_BUSY;
danielk19773b8a05f2007-03-19 17:44:26 +00006630 nToPage = sqlite3PagerPagecount(pBtTo->pPager);
6631 nPage = sqlite3PagerPagecount(pBtFrom->pPager);
drh50f2f432005-09-16 11:32:18 +00006632 iSkip = PENDING_BYTE_PAGE(pBtTo);
danielk1977369f27e2004-06-15 11:40:04 +00006633 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006634 DbPage *pDbPage;
drh50f2f432005-09-16 11:32:18 +00006635 if( i==iSkip ) continue;
danielk19773b8a05f2007-03-19 17:44:26 +00006636 rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage);
drhf7c57532003-04-25 13:22:51 +00006637 if( rc ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006638 rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage));
6639 sqlite3PagerUnref(pDbPage);
drhf7c57532003-04-25 13:22:51 +00006640 }
drh538f5702007-04-13 02:14:30 +00006641
6642 /* If the file is shrinking, journal the pages that are being truncated
6643 ** so that they can be rolled back if the commit fails.
6644 */
drh2e6d11b2003-04-25 15:37:57 +00006645 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006646 DbPage *pDbPage;
drh49285702005-09-17 15:20:26 +00006647 if( i==iSkip ) continue;
danielk19773b8a05f2007-03-19 17:44:26 +00006648 rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
drh2e6d11b2003-04-25 15:37:57 +00006649 if( rc ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006650 rc = sqlite3PagerWrite(pDbPage);
drh538f5702007-04-13 02:14:30 +00006651 sqlite3PagerDontWrite(pDbPage);
6652 /* Yeah. It seems wierd to call DontWrite() right after Write(). But
6653 ** that is because the names of those procedures do not exactly
6654 ** represent what they do. Write() really means "put this page in the
6655 ** rollback journal and mark it as dirty so that it will be written
6656 ** to the database file later." DontWrite() undoes the second part of
6657 ** that and prevents the page from being written to the database. The
6658 ** page is still on the rollback journal, though. And that is the whole
6659 ** point of this loop: to put pages on the rollback journal. */
danielk19773b8a05f2007-03-19 17:44:26 +00006660 sqlite3PagerUnref(pDbPage);
drh2e6d11b2003-04-25 15:37:57 +00006661 }
6662 if( !rc && nPage<nToPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00006663 rc = sqlite3PagerTruncate(pBtTo->pPager, nPage);
drh2e6d11b2003-04-25 15:37:57 +00006664 }
drh538f5702007-04-13 02:14:30 +00006665
drhf7c57532003-04-25 13:22:51 +00006666 if( rc ){
danielk1977aef0bf62005-12-30 16:28:01 +00006667 sqlite3BtreeRollback(pTo);
drhf7c57532003-04-25 13:22:51 +00006668 }
6669 return rc;
drh73509ee2003-04-06 20:44:45 +00006670}
drhb7f91642004-10-31 02:22:47 +00006671#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00006672
6673/*
6674** Return non-zero if a transaction is active.
6675*/
danielk1977aef0bf62005-12-30 16:28:01 +00006676int sqlite3BtreeIsInTrans(Btree *p){
6677 return (p && (p->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00006678}
6679
6680/*
6681** Return non-zero if a statement transaction is active.
6682*/
danielk1977aef0bf62005-12-30 16:28:01 +00006683int sqlite3BtreeIsInStmt(Btree *p){
6684 return (p->pBt && p->pBt->inStmt);
danielk19771d850a72004-05-31 08:26:49 +00006685}
danielk197713adf8a2004-06-03 16:08:41 +00006686
6687/*
danielk19772372c2b2006-06-27 16:34:56 +00006688** Return non-zero if a read (or write) transaction is active.
6689*/
6690int sqlite3BtreeIsInReadTrans(Btree *p){
6691 return (p && (p->inTrans!=TRANS_NONE));
6692}
6693
6694/*
danielk1977da184232006-01-05 11:34:32 +00006695** This function returns a pointer to a blob of memory associated with
6696** a single shared-btree. The memory is used by client code for it's own
6697** purposes (for example, to store a high-level schema associated with
6698** the shared-btree). The btree layer manages reference counting issues.
6699**
6700** The first time this is called on a shared-btree, nBytes bytes of memory
6701** are allocated, zeroed, and returned to the caller. For each subsequent
6702** call the nBytes parameter is ignored and a pointer to the same blob
6703** of memory returned.
6704**
6705** Just before the shared-btree is closed, the function passed as the
6706** xFree argument when the memory allocation was made is invoked on the
6707** blob of allocated memory. This function should not call sqliteFree()
6708** on the memory, the btree layer does that.
6709*/
6710void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
6711 BtShared *pBt = p->pBt;
6712 if( !pBt->pSchema ){
6713 pBt->pSchema = sqliteMalloc(nBytes);
6714 pBt->xFreeSchema = xFree;
6715 }
6716 return pBt->pSchema;
6717}
6718
danielk1977c87d34d2006-01-06 13:00:28 +00006719/*
6720** Return true if another user of the same shared btree as the argument
6721** handle holds an exclusive lock on the sqlite_master table.
6722*/
6723int sqlite3BtreeSchemaLocked(Btree *p){
6724 return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
6725}
6726
drha154dcd2006-03-22 22:10:07 +00006727
6728#ifndef SQLITE_OMIT_SHARED_CACHE
6729/*
6730** Obtain a lock on the table whose root page is iTab. The
6731** lock is a write lock if isWritelock is true or a read lock
6732** if it is false.
6733*/
danielk1977c00da102006-01-07 13:21:04 +00006734int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:27 +00006735 int rc = SQLITE_OK;
danielk1977c00da102006-01-07 13:21:04 +00006736 u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK);
danielk19772e94d4d2006-01-09 05:36:27 +00006737 rc = queryTableLock(p, iTab, lockType);
danielk1977c00da102006-01-07 13:21:04 +00006738 if( rc==SQLITE_OK ){
6739 rc = lockTable(p, iTab, lockType);
6740 }
6741 return rc;
6742}
drha154dcd2006-03-22 22:10:07 +00006743#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00006744
drh6f7adc82006-01-11 21:41:20 +00006745/*
6746** The following debugging interface has to be in this file (rather
6747** than in, for example, test1.c) so that it can get access to
6748** the definition of BtShared.
6749*/
danielk197707cb5602006-01-20 10:55:05 +00006750#if defined(SQLITE_DEBUG) && defined(TCLSH)
danielk1977b82e7ed2006-01-11 14:09:31 +00006751#include <tcl.h>
6752int sqlite3_shared_cache_report(
6753 void * clientData,
6754 Tcl_Interp *interp,
6755 int objc,
6756 Tcl_Obj *CONST objv[]
6757){
drha154dcd2006-03-22 22:10:07 +00006758#ifndef SQLITE_OMIT_SHARED_CACHE
drh6f7adc82006-01-11 21:41:20 +00006759 const ThreadData *pTd = sqlite3ThreadDataReadOnly();
danielk1977b82e7ed2006-01-11 14:09:31 +00006760 if( pTd->useSharedData ){
6761 BtShared *pBt;
6762 Tcl_Obj *pRet = Tcl_NewObj();
6763 for(pBt=pTd->pBtree; pBt; pBt=pBt->pNext){
danielk19773b8a05f2007-03-19 17:44:26 +00006764 const char *zFile = sqlite3PagerFilename(pBt->pPager);
danielk1977b82e7ed2006-01-11 14:09:31 +00006765 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewStringObj(zFile, -1));
6766 Tcl_ListObjAppendElement(interp, pRet, Tcl_NewIntObj(pBt->nRef));
6767 }
6768 Tcl_SetObjResult(interp, pRet);
6769 }
drha154dcd2006-03-22 22:10:07 +00006770#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00006771 return TCL_OK;
6772}
6773#endif