blob: 4cb6bda99aec8d419286a319d11dec5008abd70c [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*************************************************************************
drh7bec5052005-02-06 02:45:41 +000012** $Id: btree.c,v 1.246 2005/02/06 02:45:42 drh Exp $
drh8b2f49b2001-06-08 00:21:52 +000013**
14** This file implements a external (disk-based) database using BTrees.
15** For a detailed discussion of BTrees, refer to
16**
17** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
18** "Sorting And Searching", pages 473-480. Addison-Wesley
19** Publishing Company, Reading, Massachusetts.
20**
21** The basic idea is that each page of the file contains N database
22** entries and N+1 pointers to subpages.
23**
24** ----------------------------------------------------------------
25** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) |
26** ----------------------------------------------------------------
27**
28** All of the keys on the page that Ptr(0) points to have values less
29** than Key(0). All of the keys on page Ptr(1) and its subpages have
30** values greater than Key(0) and less than Key(1). All of the keys
31** on Ptr(N+1) and its subpages have values greater than Key(N). And
32** so forth.
33**
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
44** page has a small header which contains the Ptr(N+1) pointer and other
45** information such as the size of key and data.
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
drh271efa52004-05-30 19:19:05 +0000126** 8 4 Right child (the Ptr(N+1) 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
drh887dc4c2004-10-22 16:22:57 +0000214/*
215** This macro rounds values up so that if the value is an address it
216** is guaranteed to be an address that is aligned to an 8-byte boundary.
217*/
218#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
drh4b70f112004-05-02 21:12:19 +0000219
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;
paulb95a8862003-04-01 21:16:41 +0000233
drh8c42ca92001-06-22 19:15:00 +0000234/*
drhbd03cae2001-06-02 02:40:57 +0000235** This is a magic string that appears at the beginning of every
drh8c42ca92001-06-22 19:15:00 +0000236** SQLite database in order to identify the file as a real database.
drhde647132004-05-07 17:57:49 +0000237** 123456789 123456 */
238static const char zMagicHeader[] = "SQLite format 3";
drh08ed44e2001-04-29 23:32:55 +0000239
240/*
drh4b70f112004-05-02 21:12:19 +0000241** Page type flags. An ORed combination of these flags appear as the
242** first byte of every BTree page.
drh8c42ca92001-06-22 19:15:00 +0000243*/
drhde647132004-05-07 17:57:49 +0000244#define PTF_INTKEY 0x01
drh9e572e62004-04-23 23:43:10 +0000245#define PTF_ZERODATA 0x02
drh8b18dd42004-05-12 19:18:15 +0000246#define PTF_LEAFDATA 0x04
247#define PTF_LEAF 0x08
drh8c42ca92001-06-22 19:15:00 +0000248
249/*
drh9e572e62004-04-23 23:43:10 +0000250** As each page of the file is loaded into memory, an instance of the following
251** structure is appended and initialized to zero. This structure stores
252** information about the page that is decoded from the raw file page.
drh14acc042001-06-10 19:56:58 +0000253**
drh72f82862001-05-24 21:06:34 +0000254** The pParent field points back to the parent page. This allows us to
255** walk up the BTree from any leaf to the root. Care must be taken to
256** unref() the parent page pointer when this page is no longer referenced.
drhbd03cae2001-06-02 02:40:57 +0000257** The pageDestructor() routine handles that chore.
drh7e3b0a02001-04-28 16:52:40 +0000258*/
259struct MemPage {
drha6abd042004-06-09 17:37:22 +0000260 u8 isInit; /* True if previously initialized. MUST BE FIRST! */
drh43605152004-05-29 21:46:49 +0000261 u8 idxShift; /* True if Cell indices have changed */
262 u8 nOverflow; /* Number of overflow cell bodies in aCell[] */
263 u8 intKey; /* True if intkey flag is set */
264 u8 leaf; /* True if leaf flag is set */
265 u8 zeroData; /* True if table stores keys only */
266 u8 leafData; /* True if tables stores data on leaves only */
267 u8 hasData; /* True if this page stores data */
268 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
drh271efa52004-05-30 19:19:05 +0000269 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
drha2fce642004-06-05 00:01:44 +0000270 u16 maxLocal; /* Copy of Btree.maxLocal or Btree.maxLeaf */
271 u16 minLocal; /* Copy of Btree.minLocal or Btree.minLeaf */
drh43605152004-05-29 21:46:49 +0000272 u16 cellOffset; /* Index in aData of first cell pointer */
273 u16 idxParent; /* Index in parent of this node */
274 u16 nFree; /* Number of free bytes on the page */
275 u16 nCell; /* Number of cells on this page, local and ovfl */
276 struct _OvflCell { /* Cells that will not fit on aData[] */
277 u8 *pCell; /* Pointers to the body of the overflow cell */
278 u16 idx; /* Insert this cell before idx-th non-overflow cell */
drha2fce642004-06-05 00:01:44 +0000279 } aOvfl[5];
drh43605152004-05-29 21:46:49 +0000280 struct Btree *pBt; /* Pointer back to BTree structure */
281 u8 *aData; /* Pointer back to the start of the page */
282 Pgno pgno; /* Page number for this page */
283 MemPage *pParent; /* The parent of this page. NULL for root */
drh8c42ca92001-06-22 19:15:00 +0000284};
drh7e3b0a02001-04-28 16:52:40 +0000285
286/*
drh3b7511c2001-05-26 13:15:44 +0000287** The in-memory image of a disk page has the auxiliary information appended
288** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
289** that extra information.
290*/
drh3aac2dd2004-04-26 14:10:20 +0000291#define EXTRA_SIZE sizeof(MemPage)
drh3b7511c2001-05-26 13:15:44 +0000292
293/*
drha059ad02001-04-17 20:09:11 +0000294** Everything we need to know about an open database
295*/
296struct Btree {
297 Pager *pPager; /* The page cache */
drh306dc212001-05-21 13:45:10 +0000298 BtCursor *pCursor; /* A list of all open cursors */
drh3aac2dd2004-04-26 14:10:20 +0000299 MemPage *pPage1; /* First page of the database */
drh663fc632002-02-02 18:49:19 +0000300 u8 inTrans; /* True if a transaction is in progress */
drhab01f612004-05-22 02:55:23 +0000301 u8 inStmt; /* True if we are in a statement subtransaction */
drh5df72a52002-06-06 23:16:05 +0000302 u8 readOnly; /* True if the underlying file is readonly */
drhab01f612004-05-22 02:55:23 +0000303 u8 maxEmbedFrac; /* Maximum payload as % of total page size */
304 u8 minEmbedFrac; /* Minimum payload as % of total page size */
305 u8 minLeafFrac; /* Minimum leaf payload as % of total page size */
drh90f5ecb2004-07-22 01:19:35 +0000306 u8 pageSizeFixed; /* True if the page size can no longer be changed */
drha2fce642004-06-05 00:01:44 +0000307 u16 pageSize; /* Total number of bytes on a page */
drh887dc4c2004-10-22 16:22:57 +0000308 u16 psAligned; /* pageSize rounded up to a multiple of 8 */
drha2fce642004-06-05 00:01:44 +0000309 u16 usableSize; /* Number of usable bytes on each page */
drh6f11bef2004-05-13 01:12:56 +0000310 int maxLocal; /* Maximum local payload in non-LEAFDATA tables */
311 int minLocal; /* Minimum local payload in non-LEAFDATA tables */
312 int maxLeaf; /* Maximum local payload in a LEAFDATA table */
313 int minLeaf; /* Minimum local payload in a LEAFDATA table */
danielk1977afcdd022004-10-31 16:25:42 +0000314#ifndef SQLITE_OMIT_AUTOVACUUM
315 u8 autoVacuum; /* True if database supports auto-vacuum */
316#endif
drha059ad02001-04-17 20:09:11 +0000317};
318typedef Btree Bt;
319
drh365d68f2001-05-11 11:02:46 +0000320/*
danielk1977ee5741e2004-05-31 10:01:34 +0000321** Btree.inTrans may take one of the following values.
322*/
323#define TRANS_NONE 0
324#define TRANS_READ 1
325#define TRANS_WRITE 2
326
327/*
drhfa1a98a2004-05-14 19:08:17 +0000328** An instance of the following structure is used to hold information
drh271efa52004-05-30 19:19:05 +0000329** about a cell. The parseCellPtr() function fills in this structure
drhab01f612004-05-22 02:55:23 +0000330** based on information extract from the raw disk page.
drhfa1a98a2004-05-14 19:08:17 +0000331*/
332typedef struct CellInfo CellInfo;
333struct CellInfo {
drh43605152004-05-29 21:46:49 +0000334 u8 *pCell; /* Pointer to the start of cell content */
drhfa1a98a2004-05-14 19:08:17 +0000335 i64 nKey; /* The key for INTKEY tables, or number of bytes in key */
336 u32 nData; /* Number of bytes of data */
drh271efa52004-05-30 19:19:05 +0000337 u16 nHeader; /* Size of the cell content header in bytes */
drhfa1a98a2004-05-14 19:08:17 +0000338 u16 nLocal; /* Amount of payload held locally */
drhab01f612004-05-22 02:55:23 +0000339 u16 iOverflow; /* Offset to overflow page number. Zero if no overflow */
drh271efa52004-05-30 19:19:05 +0000340 u16 nSize; /* Size of the cell content on the main b-tree page */
drhfa1a98a2004-05-14 19:08:17 +0000341};
342
343/*
drh365d68f2001-05-11 11:02:46 +0000344** A cursor is a pointer to a particular entry in the BTree.
345** The entry is identified by its MemPage and the index in
drha34b6762004-05-07 13:30:42 +0000346** MemPage.aCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000347*/
drh72f82862001-05-24 21:06:34 +0000348struct BtCursor {
drh5e2f8b92001-05-28 00:41:15 +0000349 Btree *pBt; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000350 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drh3aac2dd2004-04-26 14:10:20 +0000351 int (*xCompare)(void*,int,const void*,int,const void*); /* Key comp func */
352 void *pArg; /* First arg to xCompare() */
drh8b2f49b2001-06-08 00:21:52 +0000353 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000354 MemPage *pPage; /* Page that contains the entry */
drh3aac2dd2004-04-26 14:10:20 +0000355 int idx; /* Index of the entry in pPage->aCell[] */
drhfa1a98a2004-05-14 19:08:17 +0000356 CellInfo info; /* A parse of the cell we are pointing at */
drhecdc7532001-09-23 02:35:53 +0000357 u8 wrFlag; /* True if writable */
drhc39e0002004-05-07 23:50:57 +0000358 u8 isValid; /* TRUE if points to a valid entry */
drh365d68f2001-05-11 11:02:46 +0000359};
drh7e3b0a02001-04-28 16:52:40 +0000360
drha059ad02001-04-17 20:09:11 +0000361/*
drh615ae552005-01-16 23:21:00 +0000362** The TRACE macro will print high-level status information about the
363** btree operation when the global variable sqlite3_btree_trace is
364** enabled.
365*/
366#if SQLITE_TEST
367# define TRACE(X) if( sqlite3_btree_trace )\
368 { sqlite3DebugPrintf X; fflush(stdout); }
369#else
370# define TRACE(X)
371#endif
372int sqlite3_btree_trace=0; /* True to enable tracing */
373
374/*
drh66cbd152004-09-01 16:12:25 +0000375** Forward declaration
376*/
377static int checkReadLocks(Btree*,Pgno,BtCursor*);
378
drh66cbd152004-09-01 16:12:25 +0000379/*
drhab01f612004-05-22 02:55:23 +0000380** Read or write a two- and four-byte big-endian integer values.
drh0d316a42002-08-11 20:10:47 +0000381*/
drh9e572e62004-04-23 23:43:10 +0000382static u32 get2byte(unsigned char *p){
383 return (p[0]<<8) | p[1];
drh0d316a42002-08-11 20:10:47 +0000384}
drh9e572e62004-04-23 23:43:10 +0000385static u32 get4byte(unsigned char *p){
386 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
387}
drh9e572e62004-04-23 23:43:10 +0000388static void put2byte(unsigned char *p, u32 v){
389 p[0] = v>>8;
390 p[1] = v;
391}
392static void put4byte(unsigned char *p, u32 v){
393 p[0] = v>>24;
394 p[1] = v>>16;
395 p[2] = v>>8;
396 p[3] = v;
397}
drh6f11bef2004-05-13 01:12:56 +0000398
drh9e572e62004-04-23 23:43:10 +0000399/*
drhab01f612004-05-22 02:55:23 +0000400** Routines to read and write variable-length integers. These used to
401** be defined locally, but now we use the varint routines in the util.c
402** file.
drh9e572e62004-04-23 23:43:10 +0000403*/
drh6d2fb152004-05-14 16:50:06 +0000404#define getVarint sqlite3GetVarint
405#define getVarint32 sqlite3GetVarint32
406#define putVarint sqlite3PutVarint
drh0d316a42002-08-11 20:10:47 +0000407
danielk1977599fcba2004-11-08 07:13:13 +0000408/* The database page the PENDING_BYTE occupies. This page is never used.
409** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
410** should possibly be consolidated (presumably in pager.h).
411*/
412#define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
danielk1977afcdd022004-10-31 16:25:42 +0000413
danielk1977599fcba2004-11-08 07:13:13 +0000414#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000415/*
drh42cac6d2004-11-20 20:31:11 +0000416** These macros define the location of the pointer-map entry for a
417** database page. The first argument to each is the number of usable
418** bytes on each page of the database (often 1024). The second is the
419** page number to look up in the pointer map.
danielk1977afcdd022004-10-31 16:25:42 +0000420**
421** PTRMAP_PAGENO returns the database page number of the pointer-map
422** page that stores the required pointer. PTRMAP_PTROFFSET returns
423** the offset of the requested map entry.
424**
425** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
426** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
danielk1977599fcba2004-11-08 07:13:13 +0000427** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
428** this test.
danielk1977afcdd022004-10-31 16:25:42 +0000429*/
430#define PTRMAP_PAGENO(pgsz, pgno) (((pgno-2)/(pgsz/5+1))*(pgsz/5+1)+2)
431#define PTRMAP_PTROFFSET(pgsz, pgno) (((pgno-2)%(pgsz/5+1)-1)*5)
danielk1977a19df672004-11-03 11:37:07 +0000432#define PTRMAP_ISPAGE(pgsz, pgno) (PTRMAP_PAGENO(pgsz,pgno)==pgno)
433
danielk1977afcdd022004-10-31 16:25:42 +0000434/*
drh615ae552005-01-16 23:21:00 +0000435** The pointer map is a lookup table that identifies the parent page for
436** each child page in the database file. The parent page is the page that
437** contains a pointer to the child. Every page in the database contains
438** 0 or 1 parent pages. (In this context 'database page' refers
439** to any page that is not part of the pointer map itself.) Each pointer map
440** entry consists of a single byte 'type' and a 4 byte parent page number.
441** The PTRMAP_XXX identifiers below are the valid types.
442**
443** The purpose of the pointer map is to facility moving pages from one
444** position in the file to another as part of autovacuum. When a page
445** is moved, the pointer in its parent must be updated to point to the
446** new location. The pointer map is used to locate the parent page quickly.
danielk1977afcdd022004-10-31 16:25:42 +0000447**
danielk1977687566d2004-11-02 12:56:41 +0000448** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
449** used in this case.
danielk1977afcdd022004-10-31 16:25:42 +0000450**
danielk1977687566d2004-11-02 12:56:41 +0000451** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
452** is not used in this case.
453**
454** PTRMAP_OVERFLOW1: The database page is the first page in a list of
455** overflow pages. The page number identifies the page that
456** contains the cell with a pointer to this overflow page.
457**
458** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
459** overflow pages. The page-number identifies the previous
460** page in the overflow page list.
461**
462** PTRMAP_BTREE: The database page is a non-root btree page. The page number
463** identifies the parent page in the btree.
danielk1977afcdd022004-10-31 16:25:42 +0000464*/
danielk1977687566d2004-11-02 12:56:41 +0000465#define PTRMAP_ROOTPAGE 1
466#define PTRMAP_FREEPAGE 2
467#define PTRMAP_OVERFLOW1 3
468#define PTRMAP_OVERFLOW2 4
469#define PTRMAP_BTREE 5
danielk1977afcdd022004-10-31 16:25:42 +0000470
471/*
472** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000473**
474** This routine updates the pointer map entry for page number 'key'
475** so that it maps to type 'eType' and parent page number 'pgno'.
476** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000477*/
drh615ae552005-01-16 23:21:00 +0000478static int ptrmapPut(Btree *pBt, Pgno key, u8 eType, Pgno parent){
danielk1977afcdd022004-10-31 16:25:42 +0000479 u8 *pPtrmap; /* The pointer map page */
480 Pgno iPtrmap; /* The pointer map page number */
481 int offset; /* Offset in pointer map page */
482 int rc;
483
danielk1977ac11ee62005-01-15 12:45:51 +0000484 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000485 if( key==0 ){
486 return SQLITE_CORRUPT;
487 }
drh42cac6d2004-11-20 20:31:11 +0000488 iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000489 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
danielk1977687566d2004-11-02 12:56:41 +0000490 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000491 return rc;
492 }
drh42cac6d2004-11-20 20:31:11 +0000493 offset = PTRMAP_PTROFFSET(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000494
drh615ae552005-01-16 23:21:00 +0000495 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
496 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk1977afcdd022004-10-31 16:25:42 +0000497 rc = sqlite3pager_write(pPtrmap);
danielk19775558a8a2005-01-17 07:53:44 +0000498 if( rc==SQLITE_OK ){
499 pPtrmap[offset] = eType;
500 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000501 }
danielk1977afcdd022004-10-31 16:25:42 +0000502 }
503
504 sqlite3pager_unref(pPtrmap);
danielk19775558a8a2005-01-17 07:53:44 +0000505 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000506}
507
508/*
509** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000510**
511** This routine retrieves the pointer map entry for page 'key', writing
512** the type and parent page number to *pEType and *pPgno respectively.
513** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000514*/
515static int ptrmapGet(Btree *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
516 int iPtrmap; /* Pointer map page index */
517 u8 *pPtrmap; /* Pointer map page data */
518 int offset; /* Offset of entry in pointer map */
519 int rc;
520
drh42cac6d2004-11-20 20:31:11 +0000521 iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000522 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
523 if( rc!=0 ){
524 return rc;
525 }
526
drh42cac6d2004-11-20 20:31:11 +0000527 offset = PTRMAP_PTROFFSET(pBt->usableSize, key);
danielk1977687566d2004-11-02 12:56:41 +0000528 if( pEType ) *pEType = pPtrmap[offset];
529 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000530
531 sqlite3pager_unref(pPtrmap);
danielk1977fdb7cdb2005-01-17 02:12:18 +0000532 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT;
danielk1977afcdd022004-10-31 16:25:42 +0000533 return SQLITE_OK;
534}
535
536#endif /* SQLITE_OMIT_AUTOVACUUM */
537
drh0d316a42002-08-11 20:10:47 +0000538/*
drh271efa52004-05-30 19:19:05 +0000539** Given a btree page and a cell index (0 means the first cell on
540** the page, 1 means the second cell, and so forth) return a pointer
541** to the cell content.
542**
543** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000544*/
drh43605152004-05-29 21:46:49 +0000545static u8 *findCell(MemPage *pPage, int iCell){
546 u8 *data = pPage->aData;
547 assert( iCell>=0 );
548 assert( iCell<get2byte(&data[pPage->hdrOffset+3]) );
549 return data + get2byte(&data[pPage->cellOffset+2*iCell]);
550}
551
552/*
553** This a more complex version of findCell() that works for
554** pages that do contain overflow cells. See insert
555*/
556static u8 *findOverflowCell(MemPage *pPage, int iCell){
557 int i;
558 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000559 int k;
560 struct _OvflCell *pOvfl;
561 pOvfl = &pPage->aOvfl[i];
562 k = pOvfl->idx;
563 if( k<=iCell ){
564 if( k==iCell ){
565 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000566 }
567 iCell--;
568 }
569 }
570 return findCell(pPage, iCell);
571}
572
573/*
574** Parse a cell content block and fill in the CellInfo structure. There
575** are two versions of this function. parseCell() takes a cell index
576** as the second argument and parseCellPtr() takes a pointer to the
577** body of the cell as its second argument.
578*/
579static void parseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000580 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000581 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000582 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000583){
drh271efa52004-05-30 19:19:05 +0000584 int n; /* Number bytes in cell content header */
585 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000586
587 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000588 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000589 n = pPage->childPtrSize;
590 assert( n==4-4*pPage->leaf );
drh8b18dd42004-05-12 19:18:15 +0000591 if( pPage->hasData ){
drh271efa52004-05-30 19:19:05 +0000592 n += getVarint32(&pCell[n], &nPayload);
drh8b18dd42004-05-12 19:18:15 +0000593 }else{
drh271efa52004-05-30 19:19:05 +0000594 nPayload = 0;
drh3aac2dd2004-04-26 14:10:20 +0000595 }
danielk1977e0d4b062004-06-28 01:11:46 +0000596 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
drh6f11bef2004-05-13 01:12:56 +0000597 pInfo->nHeader = n;
drh271efa52004-05-30 19:19:05 +0000598 pInfo->nData = nPayload;
drh6f11bef2004-05-13 01:12:56 +0000599 if( !pPage->intKey ){
600 nPayload += pInfo->nKey;
601 }
drh271efa52004-05-30 19:19:05 +0000602 if( nPayload<=pPage->maxLocal ){
603 /* This is the (easy) common case where the entire payload fits
604 ** on the local page. No overflow is required.
605 */
606 int nSize; /* Total size of cell content in bytes */
drh6f11bef2004-05-13 01:12:56 +0000607 pInfo->nLocal = nPayload;
608 pInfo->iOverflow = 0;
drh271efa52004-05-30 19:19:05 +0000609 nSize = nPayload + n;
610 if( nSize<4 ){
611 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000612 }
drh271efa52004-05-30 19:19:05 +0000613 pInfo->nSize = nSize;
drh6f11bef2004-05-13 01:12:56 +0000614 }else{
drh271efa52004-05-30 19:19:05 +0000615 /* If the payload will not fit completely on the local page, we have
616 ** to decide how much to store locally and how much to spill onto
617 ** overflow pages. The strategy is to minimize the amount of unused
618 ** space on overflow pages while keeping the amount of local storage
619 ** in between minLocal and maxLocal.
620 **
621 ** Warning: changing the way overflow payload is distributed in any
622 ** way will result in an incompatible file format.
623 */
624 int minLocal; /* Minimum amount of payload held locally */
625 int maxLocal; /* Maximum amount of payload held locally */
626 int surplus; /* Overflow payload available for local storage */
627
628 minLocal = pPage->minLocal;
629 maxLocal = pPage->maxLocal;
630 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000631 if( surplus <= maxLocal ){
632 pInfo->nLocal = surplus;
633 }else{
634 pInfo->nLocal = minLocal;
635 }
636 pInfo->iOverflow = pInfo->nLocal + n;
637 pInfo->nSize = pInfo->iOverflow + 4;
638 }
drh3aac2dd2004-04-26 14:10:20 +0000639}
drh43605152004-05-29 21:46:49 +0000640static void parseCell(
641 MemPage *pPage, /* Page containing the cell */
642 int iCell, /* The cell index. First cell is 0 */
643 CellInfo *pInfo /* Fill in this structure */
644){
645 parseCellPtr(pPage, findCell(pPage, iCell), pInfo);
646}
drh3aac2dd2004-04-26 14:10:20 +0000647
648/*
drh43605152004-05-29 21:46:49 +0000649** Compute the total number of bytes that a Cell needs in the cell
650** data area of the btree-page. The return number includes the cell
651** data header and the local payload, but not any overflow page or
652** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +0000653*/
danielk1977bc6ada42004-06-30 08:20:16 +0000654#ifndef NDEBUG
drh43605152004-05-29 21:46:49 +0000655static int cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +0000656 CellInfo info;
drh43605152004-05-29 21:46:49 +0000657 parseCell(pPage, iCell, &info);
658 return info.nSize;
659}
danielk1977bc6ada42004-06-30 08:20:16 +0000660#endif
drh43605152004-05-29 21:46:49 +0000661static int cellSizePtr(MemPage *pPage, u8 *pCell){
662 CellInfo info;
663 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +0000664 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +0000665}
666
danielk197779a40da2005-01-16 08:00:01 +0000667#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +0000668/*
danielk197726836652005-01-17 01:33:13 +0000669** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +0000670** to an overflow page, insert an entry into the pointer-map
671** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +0000672*/
danielk197726836652005-01-17 01:33:13 +0000673static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
danielk197779a40da2005-01-16 08:00:01 +0000674 if( pCell ){
675 CellInfo info;
676 parseCellPtr(pPage, pCell, &info);
677 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
678 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
679 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
680 }
danielk1977ac11ee62005-01-15 12:45:51 +0000681 }
danielk197779a40da2005-01-16 08:00:01 +0000682 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +0000683}
danielk197726836652005-01-17 01:33:13 +0000684/*
685** If the cell with index iCell on page pPage contains a pointer
686** to an overflow page, insert an entry into the pointer-map
687** for the overflow page.
688*/
689static int ptrmapPutOvfl(MemPage *pPage, int iCell){
690 u8 *pCell;
691 pCell = findOverflowCell(pPage, iCell);
692 return ptrmapPutOvflPtr(pPage, pCell);
693}
danielk197779a40da2005-01-16 08:00:01 +0000694#endif
695
danielk1977ac11ee62005-01-15 12:45:51 +0000696
697/*
drhda200cc2004-05-09 11:51:38 +0000698** Do sanity checking on a page. Throw an exception if anything is
699** not right.
700**
701** This routine is used for internal error checking only. It is omitted
702** from most builds.
703*/
704#if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0
705static void _pageIntegrity(MemPage *pPage){
drhb6f41482004-05-14 01:58:11 +0000706 int usableSize;
drhda200cc2004-05-09 11:51:38 +0000707 u8 *data;
drh43605152004-05-29 21:46:49 +0000708 int i, j, idx, c, pc, hdr, nFree;
709 int cellOffset;
710 int nCell, cellLimit;
drh2e38c322004-09-03 18:38:44 +0000711 u8 *used;
drhda200cc2004-05-09 11:51:38 +0000712
drh2e38c322004-09-03 18:38:44 +0000713 used = sqliteMallocRaw( pPage->pBt->pageSize );
714 if( used==0 ) return;
drhb6f41482004-05-14 01:58:11 +0000715 usableSize = pPage->pBt->usableSize;
drh887dc4c2004-10-22 16:22:57 +0000716 assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->psAligned] );
drhda200cc2004-05-09 11:51:38 +0000717 hdr = pPage->hdrOffset;
718 assert( hdr==(pPage->pgno==1 ? 100 : 0) );
719 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
720 c = pPage->aData[hdr];
721 if( pPage->isInit ){
722 assert( pPage->leaf == ((c & PTF_LEAF)!=0) );
723 assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) );
drh8b18dd42004-05-12 19:18:15 +0000724 assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) );
725 assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) );
726 assert( pPage->hasData ==
727 !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) );
drh43605152004-05-29 21:46:49 +0000728 assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf );
729 assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) );
drhda200cc2004-05-09 11:51:38 +0000730 }
731 data = pPage->aData;
drhb6f41482004-05-14 01:58:11 +0000732 memset(used, 0, usableSize);
drhda200cc2004-05-09 11:51:38 +0000733 for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1;
734 nFree = 0;
735 pc = get2byte(&data[hdr+1]);
736 while( pc ){
737 int size;
drhb6f41482004-05-14 01:58:11 +0000738 assert( pc>0 && pc<usableSize-4 );
drhda200cc2004-05-09 11:51:38 +0000739 size = get2byte(&data[pc+2]);
drhb6f41482004-05-14 01:58:11 +0000740 assert( pc+size<=usableSize );
drhda200cc2004-05-09 11:51:38 +0000741 nFree += size;
742 for(i=pc; i<pc+size; i++){
743 assert( used[i]==0 );
744 used[i] = 1;
745 }
746 pc = get2byte(&data[pc]);
747 }
drhda200cc2004-05-09 11:51:38 +0000748 idx = 0;
drh43605152004-05-29 21:46:49 +0000749 nCell = get2byte(&data[hdr+3]);
750 cellLimit = get2byte(&data[hdr+5]);
751 assert( pPage->isInit==0
752 || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) );
753 cellOffset = pPage->cellOffset;
754 for(i=0; i<nCell; i++){
drhda200cc2004-05-09 11:51:38 +0000755 int size;
drh43605152004-05-29 21:46:49 +0000756 pc = get2byte(&data[cellOffset+2*i]);
drhb6f41482004-05-14 01:58:11 +0000757 assert( pc>0 && pc<usableSize-4 );
drhda200cc2004-05-09 11:51:38 +0000758 size = cellSize(pPage, &data[pc]);
drhb6f41482004-05-14 01:58:11 +0000759 assert( pc+size<=usableSize );
drh43605152004-05-29 21:46:49 +0000760 for(j=pc; j<pc+size; j++){
761 assert( used[j]==0 );
762 used[j] = 1;
drhda200cc2004-05-09 11:51:38 +0000763 }
drhda200cc2004-05-09 11:51:38 +0000764 }
drh43605152004-05-29 21:46:49 +0000765 for(i=cellOffset+2*nCell; i<cellimit; i++){
766 assert( used[i]==0 );
767 used[i] = 1;
768 }
drhda200cc2004-05-09 11:51:38 +0000769 nFree = 0;
drhb6f41482004-05-14 01:58:11 +0000770 for(i=0; i<usableSize; i++){
drhda200cc2004-05-09 11:51:38 +0000771 assert( used[i]<=1 );
772 if( used[i]==0 ) nFree++;
773 }
drh43605152004-05-29 21:46:49 +0000774 assert( nFree==data[hdr+7] );
drh2e38c322004-09-03 18:38:44 +0000775 sqliteFree(used);
drhda200cc2004-05-09 11:51:38 +0000776}
777#define pageIntegrity(X) _pageIntegrity(X)
778#else
779# define pageIntegrity(X)
780#endif
781
782/*
drh72f82862001-05-24 21:06:34 +0000783** Defragment the page given. All Cells are moved to the
784** beginning of the page and all free space is collected
785** into one big FreeBlk at the end of the page.
drh365d68f2001-05-11 11:02:46 +0000786*/
drh2e38c322004-09-03 18:38:44 +0000787static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +0000788 int i; /* Loop counter */
789 int pc; /* Address of a i-th cell */
790 int addr; /* Offset of first byte after cell pointer array */
791 int hdr; /* Offset to the page header */
792 int size; /* Size of a cell */
793 int usableSize; /* Number of usable bytes on a page */
794 int cellOffset; /* Offset to the cell pointer array */
795 int brk; /* Offset to the cell content area */
796 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +0000797 unsigned char *data; /* The page data */
798 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +0000799
drha34b6762004-05-07 13:30:42 +0000800 assert( sqlite3pager_iswriteable(pPage->aData) );
drh9e572e62004-04-23 23:43:10 +0000801 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000802 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +0000803 assert( pPage->nOverflow==0 );
drh2e38c322004-09-03 18:38:44 +0000804 temp = sqliteMalloc( pPage->pBt->pageSize );
805 if( temp==0 ) return SQLITE_NOMEM;
drh43605152004-05-29 21:46:49 +0000806 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +0000807 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000808 cellOffset = pPage->cellOffset;
809 nCell = pPage->nCell;
810 assert( nCell==get2byte(&data[hdr+3]) );
811 usableSize = pPage->pBt->usableSize;
812 brk = get2byte(&data[hdr+5]);
813 memcpy(&temp[brk], &data[brk], usableSize - brk);
814 brk = usableSize;
815 for(i=0; i<nCell; i++){
816 u8 *pAddr; /* The i-th cell pointer */
817 pAddr = &data[cellOffset + i*2];
818 pc = get2byte(pAddr);
819 assert( pc<pPage->pBt->usableSize );
820 size = cellSizePtr(pPage, &temp[pc]);
821 brk -= size;
822 memcpy(&data[brk], &temp[pc], size);
823 put2byte(pAddr, brk);
drh2af926b2001-05-15 00:39:25 +0000824 }
drh43605152004-05-29 21:46:49 +0000825 assert( brk>=cellOffset+2*nCell );
826 put2byte(&data[hdr+5], brk);
827 data[hdr+1] = 0;
828 data[hdr+2] = 0;
829 data[hdr+7] = 0;
830 addr = cellOffset+2*nCell;
831 memset(&data[addr], 0, brk-addr);
drh2e38c322004-09-03 18:38:44 +0000832 sqliteFree(temp);
833 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +0000834}
835
drha059ad02001-04-17 20:09:11 +0000836/*
drh43605152004-05-29 21:46:49 +0000837** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +0000838**
drh9e572e62004-04-23 23:43:10 +0000839** Return the index into pPage->aData[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000840** the new allocation. Or return 0 if there is not enough free
841** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000842**
drh72f82862001-05-24 21:06:34 +0000843** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000844** nBytes of contiguous free space, then this routine automatically
845** calls defragementPage() to consolidate all free space before
846** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000847*/
drh9e572e62004-04-23 23:43:10 +0000848static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +0000849 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +0000850 int size;
drh24cd67e2004-05-10 16:18:47 +0000851 int nFrag;
drh43605152004-05-29 21:46:49 +0000852 int top;
853 int nCell;
854 int cellOffset;
drh9e572e62004-04-23 23:43:10 +0000855 unsigned char *data;
drh43605152004-05-29 21:46:49 +0000856
drh9e572e62004-04-23 23:43:10 +0000857 data = pPage->aData;
drha34b6762004-05-07 13:30:42 +0000858 assert( sqlite3pager_iswriteable(data) );
drh9e572e62004-04-23 23:43:10 +0000859 assert( pPage->pBt );
860 if( nByte<4 ) nByte = 4;
drh43605152004-05-29 21:46:49 +0000861 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
862 pPage->nFree -= nByte;
drh9e572e62004-04-23 23:43:10 +0000863 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000864
865 nFrag = data[hdr+7];
866 if( nFrag<60 ){
867 /* Search the freelist looking for a slot big enough to satisfy the
868 ** space request. */
869 addr = hdr+1;
870 while( (pc = get2byte(&data[addr]))>0 ){
871 size = get2byte(&data[pc+2]);
872 if( size>=nByte ){
873 if( size<nByte+4 ){
874 memcpy(&data[addr], &data[pc], 2);
875 data[hdr+7] = nFrag + size - nByte;
876 return pc;
877 }else{
878 put2byte(&data[pc+2], size-nByte);
879 return pc + size - nByte;
880 }
881 }
882 addr = pc;
drh9e572e62004-04-23 23:43:10 +0000883 }
884 }
drh43605152004-05-29 21:46:49 +0000885
886 /* Allocate memory from the gap in between the cell pointer array
887 ** and the cell content area.
888 */
889 top = get2byte(&data[hdr+5]);
890 nCell = get2byte(&data[hdr+3]);
891 cellOffset = pPage->cellOffset;
892 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
drh2e38c322004-09-03 18:38:44 +0000893 if( defragmentPage(pPage) ) return 0;
drh43605152004-05-29 21:46:49 +0000894 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +0000895 }
drh43605152004-05-29 21:46:49 +0000896 top -= nByte;
897 assert( cellOffset + 2*nCell <= top );
898 put2byte(&data[hdr+5], top);
899 return top;
drh7e3b0a02001-04-28 16:52:40 +0000900}
901
902/*
drh9e572e62004-04-23 23:43:10 +0000903** Return a section of the pPage->aData to the freelist.
904** The first byte of the new free block is pPage->aDisk[start]
905** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +0000906**
907** Most of the effort here is involved in coalesing adjacent
908** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000909*/
drh9e572e62004-04-23 23:43:10 +0000910static void freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +0000911 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +0000912 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +0000913
drh9e572e62004-04-23 23:43:10 +0000914 assert( pPage->pBt!=0 );
drha34b6762004-05-07 13:30:42 +0000915 assert( sqlite3pager_iswriteable(data) );
drh9e572e62004-04-23 23:43:10 +0000916 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +0000917 assert( (start + size)<=pPage->pBt->usableSize );
drh9e572e62004-04-23 23:43:10 +0000918 if( size<4 ) size = 4;
919
920 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +0000921 hdr = pPage->hdrOffset;
922 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +0000923 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +0000924 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000925 assert( pbegin>addr );
926 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +0000927 }
drhb6f41482004-05-14 01:58:11 +0000928 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000929 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +0000930 put2byte(&data[addr], start);
931 put2byte(&data[start], pbegin);
932 put2byte(&data[start+2], size);
drh2af926b2001-05-15 00:39:25 +0000933 pPage->nFree += size;
drh9e572e62004-04-23 23:43:10 +0000934
935 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +0000936 addr = pPage->hdrOffset + 1;
937 while( (pbegin = get2byte(&data[addr]))>0 ){
drh9e572e62004-04-23 23:43:10 +0000938 int pnext, psize;
drh3aac2dd2004-04-26 14:10:20 +0000939 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +0000940 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +0000941 pnext = get2byte(&data[pbegin]);
942 psize = get2byte(&data[pbegin+2]);
943 if( pbegin + psize + 3 >= pnext && pnext>0 ){
944 int frag = pnext - (pbegin+psize);
drh43605152004-05-29 21:46:49 +0000945 assert( frag<=data[pPage->hdrOffset+7] );
946 data[pPage->hdrOffset+7] -= frag;
drh9e572e62004-04-23 23:43:10 +0000947 put2byte(&data[pbegin], get2byte(&data[pnext]));
948 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
949 }else{
drh3aac2dd2004-04-26 14:10:20 +0000950 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +0000951 }
952 }
drh7e3b0a02001-04-28 16:52:40 +0000953
drh43605152004-05-29 21:46:49 +0000954 /* If the cell content area begins with a freeblock, remove it. */
955 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
956 int top;
957 pbegin = get2byte(&data[hdr+1]);
958 memcpy(&data[hdr+1], &data[pbegin], 2);
959 top = get2byte(&data[hdr+5]);
960 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
drh4b70f112004-05-02 21:12:19 +0000961 }
drh4b70f112004-05-02 21:12:19 +0000962}
963
964/*
drh271efa52004-05-30 19:19:05 +0000965** Decode the flags byte (the first byte of the header) for a page
966** and initialize fields of the MemPage structure accordingly.
967*/
968static void decodeFlags(MemPage *pPage, int flagByte){
969 Btree *pBt; /* A copy of pPage->pBt */
970
971 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
972 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
973 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
974 pPage->leaf = (flagByte & PTF_LEAF)!=0;
975 pPage->childPtrSize = 4*(pPage->leaf==0);
976 pBt = pPage->pBt;
977 if( flagByte & PTF_LEAFDATA ){
978 pPage->leafData = 1;
979 pPage->maxLocal = pBt->maxLeaf;
980 pPage->minLocal = pBt->minLeaf;
981 }else{
982 pPage->leafData = 0;
983 pPage->maxLocal = pBt->maxLocal;
984 pPage->minLocal = pBt->minLocal;
985 }
986 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
987}
988
989/*
drh7e3b0a02001-04-28 16:52:40 +0000990** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000991**
drhbd03cae2001-06-02 02:40:57 +0000992** The pParent parameter must be a pointer to the MemPage which
drh9e572e62004-04-23 23:43:10 +0000993** is the parent of the page being initialized. The root of a
994** BTree has no parent and so for that page, pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000995**
drh72f82862001-05-24 21:06:34 +0000996** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +0000997** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +0000998** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
999** guarantee that the page is well-formed. It only shows that
1000** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +00001001*/
drh9e572e62004-04-23 23:43:10 +00001002static int initPage(
drh3aac2dd2004-04-26 14:10:20 +00001003 MemPage *pPage, /* The page to be initialized */
drh9e572e62004-04-23 23:43:10 +00001004 MemPage *pParent /* The parent. Might be NULL */
1005){
drh271efa52004-05-30 19:19:05 +00001006 int pc; /* Address of a freeblock within pPage->aData[] */
drh271efa52004-05-30 19:19:05 +00001007 int hdr; /* Offset to beginning of page header */
1008 u8 *data; /* Equal to pPage->aData */
drh2e38c322004-09-03 18:38:44 +00001009 Btree *pBt; /* The main btree structure */
drh271efa52004-05-30 19:19:05 +00001010 int usableSize; /* Amount of usable space on each page */
1011 int cellOffset; /* Offset from start of page to first cell pointer */
1012 int nFree; /* Number of unused bytes on the page */
1013 int top; /* First byte of the cell content area */
drh2af926b2001-05-15 00:39:25 +00001014
drh2e38c322004-09-03 18:38:44 +00001015 pBt = pPage->pBt;
1016 assert( pBt!=0 );
1017 assert( pParent==0 || pParent->pBt==pBt );
drha34b6762004-05-07 13:30:42 +00001018 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
drh887dc4c2004-10-22 16:22:57 +00001019 assert( pPage->aData == &((unsigned char*)pPage)[-pBt->psAligned] );
drhee696e22004-08-30 16:52:17 +00001020 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
1021 /* The parent page should never change unless the file is corrupt */
1022 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1023 }
drh10617cd2004-05-14 15:27:27 +00001024 if( pPage->isInit ) return SQLITE_OK;
drhda200cc2004-05-09 11:51:38 +00001025 if( pPage->pParent==0 && pParent!=0 ){
1026 pPage->pParent = pParent;
drha34b6762004-05-07 13:30:42 +00001027 sqlite3pager_ref(pParent->aData);
drh5e2f8b92001-05-28 00:41:15 +00001028 }
drhde647132004-05-07 17:57:49 +00001029 hdr = pPage->hdrOffset;
drha34b6762004-05-07 13:30:42 +00001030 data = pPage->aData;
drh271efa52004-05-30 19:19:05 +00001031 decodeFlags(pPage, data[hdr]);
drh43605152004-05-29 21:46:49 +00001032 pPage->nOverflow = 0;
drhc8629a12004-05-08 20:07:40 +00001033 pPage->idxShift = 0;
drh2e38c322004-09-03 18:38:44 +00001034 usableSize = pBt->usableSize;
drh43605152004-05-29 21:46:49 +00001035 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
1036 top = get2byte(&data[hdr+5]);
1037 pPage->nCell = get2byte(&data[hdr+3]);
drh2e38c322004-09-03 18:38:44 +00001038 if( pPage->nCell>MX_CELL(pBt) ){
drhee696e22004-08-30 16:52:17 +00001039 /* To many cells for a single page. The page must be corrupt */
1040 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1041 }
1042 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
1043 /* All pages must have at least one cell, except for root pages */
1044 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1045 }
drh9e572e62004-04-23 23:43:10 +00001046
1047 /* Compute the total free space on the page */
drh9e572e62004-04-23 23:43:10 +00001048 pc = get2byte(&data[hdr+1]);
drh43605152004-05-29 21:46:49 +00001049 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
drh9e572e62004-04-23 23:43:10 +00001050 while( pc>0 ){
1051 int next, size;
drhee696e22004-08-30 16:52:17 +00001052 if( pc>usableSize-4 ){
1053 /* Free block is off the page */
1054 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1055 }
drh9e572e62004-04-23 23:43:10 +00001056 next = get2byte(&data[pc]);
1057 size = get2byte(&data[pc+2]);
drhee696e22004-08-30 16:52:17 +00001058 if( next>0 && next<=pc+size+3 ){
1059 /* Free blocks must be in accending order */
1060 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1061 }
drh3add3672004-05-15 00:29:24 +00001062 nFree += size;
drh9e572e62004-04-23 23:43:10 +00001063 pc = next;
1064 }
drh3add3672004-05-15 00:29:24 +00001065 pPage->nFree = nFree;
drhee696e22004-08-30 16:52:17 +00001066 if( nFree>=usableSize ){
1067 /* Free space cannot exceed total page size */
1068 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1069 }
drh9e572e62004-04-23 23:43:10 +00001070
drhde647132004-05-07 17:57:49 +00001071 pPage->isInit = 1;
drhda200cc2004-05-09 11:51:38 +00001072 pageIntegrity(pPage);
drh9e572e62004-04-23 23:43:10 +00001073 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001074}
1075
1076/*
drh8b2f49b2001-06-08 00:21:52 +00001077** Set up a raw page so that it looks like a database page holding
1078** no entries.
drhbd03cae2001-06-02 02:40:57 +00001079*/
drh9e572e62004-04-23 23:43:10 +00001080static void zeroPage(MemPage *pPage, int flags){
1081 unsigned char *data = pPage->aData;
1082 Btree *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00001083 int hdr = pPage->hdrOffset;
drh9e572e62004-04-23 23:43:10 +00001084 int first;
1085
drhda200cc2004-05-09 11:51:38 +00001086 assert( sqlite3pager_pagenumber(data)==pPage->pgno );
drh887dc4c2004-10-22 16:22:57 +00001087 assert( &data[pBt->psAligned] == (unsigned char*)pPage );
drha34b6762004-05-07 13:30:42 +00001088 assert( sqlite3pager_iswriteable(data) );
drhb6f41482004-05-14 01:58:11 +00001089 memset(&data[hdr], 0, pBt->usableSize - hdr);
drh9e572e62004-04-23 23:43:10 +00001090 data[hdr] = flags;
drh43605152004-05-29 21:46:49 +00001091 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
1092 memset(&data[hdr+1], 0, 4);
1093 data[hdr+7] = 0;
1094 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001095 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001096 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001097 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001098 pPage->cellOffset = first;
1099 pPage->nOverflow = 0;
drhda200cc2004-05-09 11:51:38 +00001100 pPage->idxShift = 0;
drh43605152004-05-29 21:46:49 +00001101 pPage->nCell = 0;
drhda200cc2004-05-09 11:51:38 +00001102 pPage->isInit = 1;
1103 pageIntegrity(pPage);
drhbd03cae2001-06-02 02:40:57 +00001104}
1105
1106/*
drh3aac2dd2004-04-26 14:10:20 +00001107** Get a page from the pager. Initialize the MemPage.pBt and
1108** MemPage.aData elements if needed.
1109*/
1110static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){
1111 int rc;
1112 unsigned char *aData;
1113 MemPage *pPage;
drha34b6762004-05-07 13:30:42 +00001114 rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData);
drh3aac2dd2004-04-26 14:10:20 +00001115 if( rc ) return rc;
drh887dc4c2004-10-22 16:22:57 +00001116 pPage = (MemPage*)&aData[pBt->psAligned];
drh3aac2dd2004-04-26 14:10:20 +00001117 pPage->aData = aData;
1118 pPage->pBt = pBt;
1119 pPage->pgno = pgno;
drhde647132004-05-07 17:57:49 +00001120 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
drh3aac2dd2004-04-26 14:10:20 +00001121 *ppPage = pPage;
1122 return SQLITE_OK;
1123}
1124
1125/*
drhde647132004-05-07 17:57:49 +00001126** Get a page from the pager and initialize it. This routine
1127** is just a convenience wrapper around separate calls to
1128** getPage() and initPage().
1129*/
1130static int getAndInitPage(
1131 Btree *pBt, /* The database file */
1132 Pgno pgno, /* Number of the page to get */
1133 MemPage **ppPage, /* Write the page pointer here */
1134 MemPage *pParent /* Parent of the page */
1135){
1136 int rc;
drhee696e22004-08-30 16:52:17 +00001137 if( pgno==0 ){
1138 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1139 }
drhde647132004-05-07 17:57:49 +00001140 rc = getPage(pBt, pgno, ppPage);
drh10617cd2004-05-14 15:27:27 +00001141 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
drhde647132004-05-07 17:57:49 +00001142 rc = initPage(*ppPage, pParent);
1143 }
1144 return rc;
1145}
1146
1147/*
drh3aac2dd2004-04-26 14:10:20 +00001148** Release a MemPage. This should be called once for each prior
1149** call to getPage.
1150*/
drh4b70f112004-05-02 21:12:19 +00001151static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001152 if( pPage ){
1153 assert( pPage->aData );
1154 assert( pPage->pBt );
drh887dc4c2004-10-22 16:22:57 +00001155 assert( &pPage->aData[pPage->pBt->psAligned]==(unsigned char*)pPage );
drha34b6762004-05-07 13:30:42 +00001156 sqlite3pager_unref(pPage->aData);
drh3aac2dd2004-04-26 14:10:20 +00001157 }
1158}
1159
1160/*
drh72f82862001-05-24 21:06:34 +00001161** This routine is called when the reference count for a page
1162** reaches zero. We need to unref the pParent pointer when that
1163** happens.
1164*/
drhb6f41482004-05-14 01:58:11 +00001165static void pageDestructor(void *pData, int pageSize){
drh887dc4c2004-10-22 16:22:57 +00001166 MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
drh72f82862001-05-24 21:06:34 +00001167 if( pPage->pParent ){
1168 MemPage *pParent = pPage->pParent;
1169 pPage->pParent = 0;
drha34b6762004-05-07 13:30:42 +00001170 releasePage(pParent);
drh72f82862001-05-24 21:06:34 +00001171 }
drh3aac2dd2004-04-26 14:10:20 +00001172 pPage->isInit = 0;
drh72f82862001-05-24 21:06:34 +00001173}
1174
1175/*
drha6abd042004-06-09 17:37:22 +00001176** During a rollback, when the pager reloads information into the cache
1177** so that the cache is restored to its original state at the start of
1178** the transaction, for each page restored this routine is called.
1179**
1180** This routine needs to reset the extra data section at the end of the
1181** page to agree with the restored data.
1182*/
1183static void pageReinit(void *pData, int pageSize){
drh887dc4c2004-10-22 16:22:57 +00001184 MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
drha6abd042004-06-09 17:37:22 +00001185 if( pPage->isInit ){
1186 pPage->isInit = 0;
1187 initPage(pPage, pPage->pParent);
1188 }
1189}
1190
1191/*
drhad3e0102004-09-03 23:32:18 +00001192** Open a database file.
1193**
drh382c0242001-10-06 16:33:02 +00001194** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001195** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001196** database file will be deleted when sqlite3BtreeClose() is called.
drha059ad02001-04-17 20:09:11 +00001197*/
drh23e11ca2004-05-04 17:27:28 +00001198int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001199 const char *zFilename, /* Name of the file containing the BTree database */
1200 Btree **ppBtree, /* Pointer to new Btree object written here */
drh90f5ecb2004-07-22 01:19:35 +00001201 int flags /* Options */
drh6019e162001-07-02 17:51:45 +00001202){
drha059ad02001-04-17 20:09:11 +00001203 Btree *pBt;
drha34b6762004-05-07 13:30:42 +00001204 int rc;
drh90f5ecb2004-07-22 01:19:35 +00001205 int nReserve;
1206 unsigned char zDbHeader[100];
drha059ad02001-04-17 20:09:11 +00001207
drhd62d3d02003-01-24 12:14:20 +00001208 /*
1209 ** The following asserts make sure that structures used by the btree are
1210 ** the right size. This is to guard against size changes that result
1211 ** when compiling on a different architecture.
1212 */
drh4a1c3802004-05-12 15:15:47 +00001213 assert( sizeof(i64)==8 );
drh9e572e62004-04-23 23:43:10 +00001214 assert( sizeof(u64)==8 );
drhd62d3d02003-01-24 12:14:20 +00001215 assert( sizeof(u32)==4 );
1216 assert( sizeof(u16)==2 );
1217 assert( sizeof(Pgno)==4 );
drhd62d3d02003-01-24 12:14:20 +00001218 assert( sizeof(ptr)==sizeof(char*) );
1219 assert( sizeof(uptr)==sizeof(ptr) );
1220
drha059ad02001-04-17 20:09:11 +00001221 pBt = sqliteMalloc( sizeof(*pBt) );
1222 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +00001223 *ppBtree = 0;
drha059ad02001-04-17 20:09:11 +00001224 return SQLITE_NOMEM;
1225 }
drh7bec5052005-02-06 02:45:41 +00001226 rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE, flags);
drha059ad02001-04-17 20:09:11 +00001227 if( rc!=SQLITE_OK ){
drha34b6762004-05-07 13:30:42 +00001228 if( pBt->pPager ) sqlite3pager_close(pBt->pPager);
drha059ad02001-04-17 20:09:11 +00001229 sqliteFree(pBt);
1230 *ppBtree = 0;
1231 return rc;
1232 }
drha34b6762004-05-07 13:30:42 +00001233 sqlite3pager_set_destructor(pBt->pPager, pageDestructor);
drha6abd042004-06-09 17:37:22 +00001234 sqlite3pager_set_reiniter(pBt->pPager, pageReinit);
drha059ad02001-04-17 20:09:11 +00001235 pBt->pCursor = 0;
drha34b6762004-05-07 13:30:42 +00001236 pBt->pPage1 = 0;
1237 pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager);
drh90f5ecb2004-07-22 01:19:35 +00001238 sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader);
1239 pBt->pageSize = get2byte(&zDbHeader[16]);
1240 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){
1241 pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
1242 pBt->maxEmbedFrac = 64; /* 25% */
1243 pBt->minEmbedFrac = 32; /* 12.5% */
1244 pBt->minLeafFrac = 32; /* 12.5% */
drheee46cf2004-11-06 00:02:48 +00001245#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197703aded42004-11-22 05:26:27 +00001246 /* If the magic name ":memory:" will create an in-memory database, then
1247 ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM
1248 ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined,
1249 ** then ":memory:" is just a regular file-name. Respect the auto-vacuum
1250 ** default in this case.
1251 */
1252#ifndef SQLITE_OMIT_MEMORYDB
danielk1977951af802004-11-05 15:45:09 +00001253 if( zFilename && strcmp(zFilename,":memory:") ){
danielk197703aded42004-11-22 05:26:27 +00001254#else
1255 if( zFilename ){
1256#endif
danielk1977951af802004-11-05 15:45:09 +00001257 pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM;
1258 }
drheee46cf2004-11-06 00:02:48 +00001259#endif
drh90f5ecb2004-07-22 01:19:35 +00001260 nReserve = 0;
1261 }else{
1262 nReserve = zDbHeader[20];
1263 pBt->maxEmbedFrac = zDbHeader[21];
1264 pBt->minEmbedFrac = zDbHeader[22];
1265 pBt->minLeafFrac = zDbHeader[23];
1266 pBt->pageSizeFixed = 1;
danielk1977951af802004-11-05 15:45:09 +00001267#ifndef SQLITE_OMIT_AUTOVACUUM
1268 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1269#endif
drh90f5ecb2004-07-22 01:19:35 +00001270 }
1271 pBt->usableSize = pBt->pageSize - nReserve;
drh887dc4c2004-10-22 16:22:57 +00001272 pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001273 sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
drha059ad02001-04-17 20:09:11 +00001274 *ppBtree = pBt;
1275 return SQLITE_OK;
1276}
1277
1278/*
1279** Close an open database and invalidate all cursors.
1280*/
drh3aac2dd2004-04-26 14:10:20 +00001281int sqlite3BtreeClose(Btree *pBt){
drha059ad02001-04-17 20:09:11 +00001282 while( pBt->pCursor ){
drh3aac2dd2004-04-26 14:10:20 +00001283 sqlite3BtreeCloseCursor(pBt->pCursor);
drha059ad02001-04-17 20:09:11 +00001284 }
drha34b6762004-05-07 13:30:42 +00001285 sqlite3pager_close(pBt->pPager);
drha059ad02001-04-17 20:09:11 +00001286 sqliteFree(pBt);
1287 return SQLITE_OK;
1288}
1289
1290/*
drh90f5ecb2004-07-22 01:19:35 +00001291** Change the busy handler callback function.
1292*/
1293int sqlite3BtreeSetBusyHandler(Btree *pBt, BusyHandler *pHandler){
1294 sqlite3pager_set_busyhandler(pBt->pPager, pHandler);
1295 return SQLITE_OK;
1296}
1297
1298/*
drhda47d772002-12-02 04:25:19 +00001299** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001300**
1301** The maximum number of cache pages is set to the absolute
1302** value of mxPage. If mxPage is negative, the pager will
1303** operate asynchronously - it will not stop to do fsync()s
1304** to insure data is written to the disk surface before
1305** continuing. Transactions still work if synchronous is off,
1306** and the database cannot be corrupted if this program
1307** crashes. But if the operating system crashes or there is
1308** an abrupt power failure when synchronous is off, the database
1309** could be left in an inconsistent and unrecoverable state.
1310** Synchronous is on by default so database corruption is not
1311** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001312*/
drh23e11ca2004-05-04 17:27:28 +00001313int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){
drha34b6762004-05-07 13:30:42 +00001314 sqlite3pager_set_cachesize(pBt->pPager, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001315 return SQLITE_OK;
1316}
1317
1318/*
drh973b6e32003-02-12 14:09:42 +00001319** Change the way data is synced to disk in order to increase or decrease
1320** how well the database resists damage due to OS crashes and power
1321** failures. Level 1 is the same as asynchronous (no syncs() occur and
1322** there is a high probability of damage) Level 2 is the default. There
1323** is a very low but non-zero probability of damage. Level 3 reduces the
1324** probability of damage to near zero but with a write performance reduction.
1325*/
danielk197793758c82005-01-21 08:13:14 +00001326#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drh3aac2dd2004-04-26 14:10:20 +00001327int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){
drha34b6762004-05-07 13:30:42 +00001328 sqlite3pager_set_safety_level(pBt->pPager, level);
drh973b6e32003-02-12 14:09:42 +00001329 return SQLITE_OK;
1330}
danielk197793758c82005-01-21 08:13:14 +00001331#endif
drh973b6e32003-02-12 14:09:42 +00001332
danielk1977576ec6b2005-01-21 11:55:25 +00001333#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001334/*
drh90f5ecb2004-07-22 01:19:35 +00001335** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001336**
1337** The page size must be a power of 2 between 512 and 65536. If the page
1338** size supplied does not meet this constraint then the page size is not
1339** changed.
1340**
1341** Page sizes are constrained to be a power of two so that the region
1342** of the database file used for locking (beginning at PENDING_BYTE,
1343** the first byte past the 1GB boundary, 0x40000000) needs to occur
1344** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001345**
1346** If parameter nReserve is less than zero, then the number of reserved
1347** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001348*/
1349int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){
1350 if( pBt->pageSizeFixed ){
1351 return SQLITE_READONLY;
1352 }
1353 if( nReserve<0 ){
1354 nReserve = pBt->pageSize - pBt->usableSize;
1355 }
drh06f50212004-11-02 14:24:33 +00001356 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1357 ((pageSize-1)&pageSize)==0 ){
drh90f5ecb2004-07-22 01:19:35 +00001358 pBt->pageSize = pageSize;
drh887dc4c2004-10-22 16:22:57 +00001359 pBt->psAligned = FORCE_ALIGNMENT(pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001360 sqlite3pager_set_pagesize(pBt->pPager, pageSize);
1361 }
1362 pBt->usableSize = pBt->pageSize - nReserve;
1363 return SQLITE_OK;
1364}
1365
1366/*
1367** Return the currently defined page size
1368*/
1369int sqlite3BtreeGetPageSize(Btree *pBt){
1370 return pBt->pageSize;
1371}
drh2011d5f2004-07-22 02:40:37 +00001372int sqlite3BtreeGetReserve(Btree *pBt){
1373 return pBt->pageSize - pBt->usableSize;
1374}
danielk1977576ec6b2005-01-21 11:55:25 +00001375#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001376
1377/*
danielk1977951af802004-11-05 15:45:09 +00001378** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1379** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1380** is disabled. The default value for the auto-vacuum property is
1381** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1382*/
1383int sqlite3BtreeSetAutoVacuum(Btree *pBt, int autoVacuum){
1384#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001385 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001386#else
1387 if( pBt->pageSizeFixed ){
1388 return SQLITE_READONLY;
1389 }
1390 pBt->autoVacuum = (autoVacuum?1:0);
1391 return SQLITE_OK;
1392#endif
1393}
1394
1395/*
1396** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1397** enabled 1 is returned. Otherwise 0.
1398*/
1399int sqlite3BtreeGetAutoVacuum(Btree *pBt){
1400#ifdef SQLITE_OMIT_AUTOVACUUM
1401 return 0;
1402#else
1403 return pBt->autoVacuum;
1404#endif
1405}
1406
1407
1408/*
drha34b6762004-05-07 13:30:42 +00001409** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001410** also acquire a readlock on that file.
1411**
1412** SQLITE_OK is returned on success. If the file is not a
1413** well-formed database file, then SQLITE_CORRUPT is returned.
1414** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
1415** is returned if we run out of memory. SQLITE_PROTOCOL is returned
1416** if there is a locking protocol violation.
1417*/
1418static int lockBtree(Btree *pBt){
1419 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001420 MemPage *pPage1;
drha34b6762004-05-07 13:30:42 +00001421 if( pBt->pPage1 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001422 rc = getPage(pBt, 1, &pPage1);
drh306dc212001-05-21 13:45:10 +00001423 if( rc!=SQLITE_OK ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00001424
drh306dc212001-05-21 13:45:10 +00001425
1426 /* Do some checking to help insure the file we opened really is
1427 ** a valid database file.
1428 */
drhb6f41482004-05-14 01:58:11 +00001429 rc = SQLITE_NOTADB;
drha34b6762004-05-07 13:30:42 +00001430 if( sqlite3pager_pagecount(pBt->pPager)>0 ){
drhb6f41482004-05-14 01:58:11 +00001431 u8 *page1 = pPage1->aData;
1432 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001433 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001434 }
drhb6f41482004-05-14 01:58:11 +00001435 if( page1[18]>1 || page1[19]>1 ){
1436 goto page1_init_failed;
1437 }
1438 pBt->pageSize = get2byte(&page1[16]);
1439 pBt->usableSize = pBt->pageSize - page1[20];
1440 if( pBt->usableSize<500 ){
1441 goto page1_init_failed;
1442 }
drh887dc4c2004-10-22 16:22:57 +00001443 pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
drhb6f41482004-05-14 01:58:11 +00001444 pBt->maxEmbedFrac = page1[21];
1445 pBt->minEmbedFrac = page1[22];
1446 pBt->minLeafFrac = page1[23];
drh306dc212001-05-21 13:45:10 +00001447 }
drhb6f41482004-05-14 01:58:11 +00001448
1449 /* maxLocal is the maximum amount of payload to store locally for
1450 ** a cell. Make sure it is small enough so that at least minFanout
1451 ** cells can will fit on one page. We assume a 10-byte page header.
1452 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001453 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001454 ** 4-byte child pointer
1455 ** 9-byte nKey value
1456 ** 4-byte nData value
1457 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001458 ** So a cell consists of a 2-byte poiner, a header which is as much as
1459 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1460 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001461 */
drh43605152004-05-29 21:46:49 +00001462 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1463 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1464 pBt->maxLeaf = pBt->usableSize - 35;
1465 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
drhb6f41482004-05-14 01:58:11 +00001466 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1467 goto page1_init_failed;
1468 }
drh2e38c322004-09-03 18:38:44 +00001469 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001470 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001471 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001472
drh72f82862001-05-24 21:06:34 +00001473page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001474 releasePage(pPage1);
1475 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001476 return rc;
drh306dc212001-05-21 13:45:10 +00001477}
1478
1479/*
drhb8ca3072001-12-05 00:21:20 +00001480** If there are no outstanding cursors and we are not in the middle
1481** of a transaction but there is a read lock on the database, then
1482** this routine unrefs the first page of the database file which
1483** has the effect of releasing the read lock.
1484**
1485** If there are any outstanding cursors, this routine is a no-op.
1486**
1487** If there is a transaction in progress, this routine is a no-op.
1488*/
1489static void unlockBtreeIfUnused(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00001490 if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
drh51c6d962004-06-06 00:42:25 +00001491 if( pBt->pPage1->aData==0 ){
1492 MemPage *pPage = pBt->pPage1;
drh887dc4c2004-10-22 16:22:57 +00001493 pPage->aData = &((char*)pPage)[-pBt->psAligned];
drh51c6d962004-06-06 00:42:25 +00001494 pPage->pBt = pBt;
1495 pPage->pgno = 1;
1496 }
drh3aac2dd2004-04-26 14:10:20 +00001497 releasePage(pBt->pPage1);
1498 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001499 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001500 }
1501}
1502
1503/*
drh9e572e62004-04-23 23:43:10 +00001504** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001505** file.
drh8b2f49b2001-06-08 00:21:52 +00001506*/
1507static int newDatabase(Btree *pBt){
drh9e572e62004-04-23 23:43:10 +00001508 MemPage *pP1;
1509 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001510 int rc;
drhde647132004-05-07 17:57:49 +00001511 if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001512 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001513 assert( pP1!=0 );
1514 data = pP1->aData;
drha34b6762004-05-07 13:30:42 +00001515 rc = sqlite3pager_write(data);
drh8b2f49b2001-06-08 00:21:52 +00001516 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001517 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1518 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001519 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001520 data[18] = 1;
1521 data[19] = 1;
drhb6f41482004-05-14 01:58:11 +00001522 data[20] = pBt->pageSize - pBt->usableSize;
1523 data[21] = pBt->maxEmbedFrac;
1524 data[22] = pBt->minEmbedFrac;
1525 data[23] = pBt->minLeafFrac;
1526 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00001527 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00001528 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00001529#ifndef SQLITE_OMIT_AUTOVACUUM
1530 if( pBt->autoVacuum ){
1531 put4byte(&data[36 + 4*4], 1);
1532 }
1533#endif
drh8b2f49b2001-06-08 00:21:52 +00001534 return SQLITE_OK;
1535}
1536
1537/*
danielk1977ee5741e2004-05-31 10:01:34 +00001538** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00001539** is started if the second argument is nonzero, otherwise a read-
1540** transaction. If the second argument is 2 or more and exclusive
1541** transaction is started, meaning that no other process is allowed
1542** to access the database. A preexisting transaction may not be
1543** upgrade to exclusive by calling this routine a second time - the
1544** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00001545**
danielk1977ee5741e2004-05-31 10:01:34 +00001546** A write-transaction must be started before attempting any
1547** changes to the database. None of the following routines
1548** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00001549**
drh23e11ca2004-05-04 17:27:28 +00001550** sqlite3BtreeCreateTable()
1551** sqlite3BtreeCreateIndex()
1552** sqlite3BtreeClearTable()
1553** sqlite3BtreeDropTable()
1554** sqlite3BtreeInsert()
1555** sqlite3BtreeDelete()
1556** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00001557**
1558** If wrflag is true, then nMaster specifies the maximum length of
1559** a master journal file name supplied later via sqlite3BtreeSync().
1560** This is so that appropriate space can be allocated in the journal file
1561** when it is created..
drha059ad02001-04-17 20:09:11 +00001562*/
danielk197740b38dc2004-06-26 08:38:24 +00001563int sqlite3BtreeBeginTrans(Btree *pBt, int wrflag){
danielk1977ee5741e2004-05-31 10:01:34 +00001564 int rc = SQLITE_OK;
1565
1566 /* If the btree is already in a write-transaction, or it
1567 ** is already in a read-transaction and a read-transaction
1568 ** is requested, this is a no-op.
1569 */
1570 if( pBt->inTrans==TRANS_WRITE ||
1571 (pBt->inTrans==TRANS_READ && !wrflag) ){
1572 return SQLITE_OK;
1573 }
1574 if( pBt->readOnly && wrflag ){
1575 return SQLITE_READONLY;
1576 }
1577
drh3aac2dd2004-04-26 14:10:20 +00001578 if( pBt->pPage1==0 ){
drh7e3b0a02001-04-28 16:52:40 +00001579 rc = lockBtree(pBt);
danielk1977ee5741e2004-05-31 10:01:34 +00001580 }
1581
1582 if( rc==SQLITE_OK && wrflag ){
drh684917c2004-10-05 02:41:42 +00001583 rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1);
danielk1977ee5741e2004-05-31 10:01:34 +00001584 if( rc==SQLITE_OK ){
1585 rc = newDatabase(pBt);
drh8c42ca92001-06-22 19:15:00 +00001586 }
drha059ad02001-04-17 20:09:11 +00001587 }
danielk1977ee5741e2004-05-31 10:01:34 +00001588
drhf74b8d92002-09-01 23:20:45 +00001589 if( rc==SQLITE_OK ){
danielk1977ee5741e2004-05-31 10:01:34 +00001590 pBt->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
1591 if( wrflag ) pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001592 }else{
1593 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001594 }
drhb8ca3072001-12-05 00:21:20 +00001595 return rc;
drha059ad02001-04-17 20:09:11 +00001596}
1597
danielk1977687566d2004-11-02 12:56:41 +00001598#ifndef SQLITE_OMIT_AUTOVACUUM
1599
1600/*
1601** Set the pointer-map entries for all children of page pPage. Also, if
1602** pPage contains cells that point to overflow pages, set the pointer
1603** map entries for the overflow pages as well.
1604*/
1605static int setChildPtrmaps(MemPage *pPage){
1606 int i; /* Counter variable */
1607 int nCell; /* Number of cells in page pPage */
1608 int rc = SQLITE_OK; /* Return code */
1609 Btree *pBt = pPage->pBt;
1610 int isInitOrig = pPage->isInit;
1611 Pgno pgno = pPage->pgno;
1612
1613 initPage(pPage, 0);
1614 nCell = pPage->nCell;
1615
1616 for(i=0; i<nCell; i++){
danielk1977687566d2004-11-02 12:56:41 +00001617 u8 *pCell = findCell(pPage, i);
1618
danielk197726836652005-01-17 01:33:13 +00001619 rc = ptrmapPutOvflPtr(pPage, pCell);
1620 if( rc!=SQLITE_OK ){
1621 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00001622 }
danielk197726836652005-01-17 01:33:13 +00001623
danielk1977687566d2004-11-02 12:56:41 +00001624 if( !pPage->leaf ){
1625 Pgno childPgno = get4byte(pCell);
1626 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1627 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1628 }
1629 }
1630
1631 if( !pPage->leaf ){
1632 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1633 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1634 }
1635
1636set_child_ptrmaps_out:
1637 pPage->isInit = isInitOrig;
1638 return rc;
1639}
1640
1641/*
1642** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
1643** page, is a pointer to page iFrom. Modify this pointer so that it points to
1644** iTo. Parameter eType describes the type of pointer to be modified, as
1645** follows:
1646**
1647** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
1648** page of pPage.
1649**
1650** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
1651** page pointed to by one of the cells on pPage.
1652**
1653** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
1654** overflow page in the list.
1655*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00001656static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
danielk1977687566d2004-11-02 12:56:41 +00001657 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00001658 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00001659 if( get4byte(pPage->aData)!=iFrom ){
1660 return SQLITE_CORRUPT;
1661 }
danielk1977f78fc082004-11-02 14:40:32 +00001662 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00001663 }else{
1664 int isInitOrig = pPage->isInit;
1665 int i;
1666 int nCell;
1667
1668 initPage(pPage, 0);
1669 nCell = pPage->nCell;
1670
danielk1977687566d2004-11-02 12:56:41 +00001671 for(i=0; i<nCell; i++){
1672 u8 *pCell = findCell(pPage, i);
1673 if( eType==PTRMAP_OVERFLOW1 ){
1674 CellInfo info;
1675 parseCellPtr(pPage, pCell, &info);
1676 if( info.iOverflow ){
1677 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
1678 put4byte(&pCell[info.iOverflow], iTo);
1679 break;
1680 }
1681 }
1682 }else{
1683 if( get4byte(pCell)==iFrom ){
1684 put4byte(pCell, iTo);
1685 break;
1686 }
1687 }
1688 }
1689
1690 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00001691 if( eType!=PTRMAP_BTREE ||
1692 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
1693 return SQLITE_CORRUPT;
1694 }
danielk1977687566d2004-11-02 12:56:41 +00001695 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
1696 }
1697
1698 pPage->isInit = isInitOrig;
1699 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00001700 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00001701}
1702
danielk1977003ba062004-11-04 02:57:33 +00001703
danielk19777701e812005-01-10 12:59:51 +00001704/*
1705** Move the open database page pDbPage to location iFreePage in the
1706** database. The pDbPage reference remains valid.
1707*/
danielk1977003ba062004-11-04 02:57:33 +00001708static int relocatePage(
danielk19777701e812005-01-10 12:59:51 +00001709 Btree *pBt, /* Btree */
1710 MemPage *pDbPage, /* Open page to move */
1711 u8 eType, /* Pointer map 'type' entry for pDbPage */
1712 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
1713 Pgno iFreePage /* The location to move pDbPage to */
danielk1977003ba062004-11-04 02:57:33 +00001714){
1715 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
1716 Pgno iDbPage = pDbPage->pgno;
1717 Pager *pPager = pBt->pPager;
1718 int rc;
1719
danielk1977a0bf2652004-11-04 14:30:04 +00001720 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
1721 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
danielk1977003ba062004-11-04 02:57:33 +00001722
1723 /* Move page iDbPage from it's current location to page number iFreePage */
1724 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
1725 iDbPage, iFreePage, iPtrPage, eType));
1726 rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage);
1727 if( rc!=SQLITE_OK ){
1728 return rc;
1729 }
1730 pDbPage->pgno = iFreePage;
1731
1732 /* If pDbPage was a btree-page, then it may have child pages and/or cells
1733 ** that point to overflow pages. The pointer map entries for all these
1734 ** pages need to be changed.
1735 **
1736 ** If pDbPage is an overflow page, then the first 4 bytes may store a
1737 ** pointer to a subsequent overflow page. If this is the case, then
1738 ** the pointer map needs to be updated for the subsequent overflow page.
1739 */
danielk1977a0bf2652004-11-04 14:30:04 +00001740 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00001741 rc = setChildPtrmaps(pDbPage);
1742 if( rc!=SQLITE_OK ){
1743 return rc;
1744 }
1745 }else{
1746 Pgno nextOvfl = get4byte(pDbPage->aData);
1747 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00001748 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
1749 if( rc!=SQLITE_OK ){
1750 return rc;
1751 }
1752 }
1753 }
1754
1755 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
1756 ** that it points at iFreePage. Also fix the pointer map entry for
1757 ** iPtrPage.
1758 */
danielk1977a0bf2652004-11-04 14:30:04 +00001759 if( eType!=PTRMAP_ROOTPAGE ){
1760 rc = getPage(pBt, iPtrPage, &pPtrPage);
1761 if( rc!=SQLITE_OK ){
1762 return rc;
1763 }
1764 rc = sqlite3pager_write(pPtrPage->aData);
1765 if( rc!=SQLITE_OK ){
1766 releasePage(pPtrPage);
1767 return rc;
1768 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00001769 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00001770 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00001771 if( rc==SQLITE_OK ){
1772 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
1773 }
danielk1977003ba062004-11-04 02:57:33 +00001774 }
danielk1977003ba062004-11-04 02:57:33 +00001775 return rc;
1776}
1777
danielk1977687566d2004-11-02 12:56:41 +00001778/* Forward declaration required by autoVacuumCommit(). */
danielk1977cb1a7eb2004-11-05 12:27:02 +00001779static int allocatePage(Btree *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00001780
1781/*
1782** This routine is called prior to sqlite3pager_commit when a transaction
1783** is commited for an auto-vacuum database.
1784*/
danielk1977d761c0c2004-11-05 16:37:02 +00001785static int autoVacuumCommit(Btree *pBt, Pgno *nTrunc){
danielk1977687566d2004-11-02 12:56:41 +00001786 Pager *pPager = pBt->pPager;
1787 Pgno nFreeList; /* Number of pages remaining on the free-list. */
danielk1977a19df672004-11-03 11:37:07 +00001788 int nPtrMap; /* Number of pointer-map pages deallocated */
1789 Pgno origSize; /* Pages in the database file */
1790 Pgno finSize; /* Pages in the database file after truncation */
danielk1977687566d2004-11-02 12:56:41 +00001791 int rc; /* Return code */
1792 u8 eType;
danielk1977a19df672004-11-03 11:37:07 +00001793 int pgsz = pBt->pageSize; /* Page size for this database */
danielk1977687566d2004-11-02 12:56:41 +00001794 Pgno iDbPage; /* The database page to move */
danielk1977687566d2004-11-02 12:56:41 +00001795 MemPage *pDbMemPage = 0; /* "" */
1796 Pgno iPtrPage; /* The page that contains a pointer to iDbPage */
danielk1977687566d2004-11-02 12:56:41 +00001797 Pgno iFreePage; /* The free-list page to move iDbPage to */
1798 MemPage *pFreeMemPage = 0; /* "" */
1799
1800#ifndef NDEBUG
1801 int nRef = *sqlite3pager_stats(pPager);
1802#endif
1803
1804 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +00001805 if( PTRMAP_ISPAGE(pgsz, sqlite3pager_pagecount(pPager)) ){
1806 return SQLITE_CORRUPT;
1807 }
danielk1977687566d2004-11-02 12:56:41 +00001808
1809 /* Figure out how many free-pages are in the database. If there are no
1810 ** free pages, then auto-vacuum is a no-op.
1811 */
1812 nFreeList = get4byte(&pBt->pPage1->aData[36]);
danielk1977a19df672004-11-03 11:37:07 +00001813 if( nFreeList==0 ){
danielk1977d761c0c2004-11-05 16:37:02 +00001814 *nTrunc = 0;
danielk1977a19df672004-11-03 11:37:07 +00001815 return SQLITE_OK;
1816 }
danielk1977687566d2004-11-02 12:56:41 +00001817
danielk1977a19df672004-11-03 11:37:07 +00001818 origSize = sqlite3pager_pagecount(pPager);
1819 nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pgsz, origSize)+pgsz/5)/(pgsz/5);
1820 finSize = origSize - nFreeList - nPtrMap;
danielk1977599fcba2004-11-08 07:13:13 +00001821 if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){
1822 finSize--;
drh42cac6d2004-11-20 20:31:11 +00001823 if( PTRMAP_ISPAGE(pBt->usableSize, finSize) ){
danielk1977599fcba2004-11-08 07:13:13 +00001824 finSize--;
1825 }
1826 }
danielk1977a19df672004-11-03 11:37:07 +00001827 TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize));
danielk1977687566d2004-11-02 12:56:41 +00001828
danielk1977a19df672004-11-03 11:37:07 +00001829 /* Variable 'finSize' will be the size of the file in pages after
danielk1977687566d2004-11-02 12:56:41 +00001830 ** the auto-vacuum has completed (the current file size minus the number
1831 ** of pages on the free list). Loop through the pages that lie beyond
1832 ** this mark, and if they are not already on the free list, move them
danielk1977a19df672004-11-03 11:37:07 +00001833 ** to a free page earlier in the file (somewhere before finSize).
danielk1977687566d2004-11-02 12:56:41 +00001834 */
danielk1977a19df672004-11-03 11:37:07 +00001835 for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){
danielk1977599fcba2004-11-08 07:13:13 +00001836 /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */
1837 if( PTRMAP_ISPAGE(pgsz, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){
1838 continue;
1839 }
1840
danielk1977687566d2004-11-02 12:56:41 +00001841 rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage);
1842 if( rc!=SQLITE_OK ) goto autovacuum_out;
1843 assert( eType!=PTRMAP_ROOTPAGE );
1844
danielk1977599fcba2004-11-08 07:13:13 +00001845 /* If iDbPage is free, do not swap it. */
1846 if( eType==PTRMAP_FREEPAGE ){
danielk1977687566d2004-11-02 12:56:41 +00001847 continue;
1848 }
1849 rc = getPage(pBt, iDbPage, &pDbMemPage);
1850 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00001851
1852 /* Find the next page in the free-list that is not already at the end
1853 ** of the file. A page can be pulled off the free list using the
1854 ** allocatePage() routine.
1855 */
1856 do{
1857 if( pFreeMemPage ){
1858 releasePage(pFreeMemPage);
1859 pFreeMemPage = 0;
1860 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00001861 rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
danielk1977ac11ee62005-01-15 12:45:51 +00001862 if( rc!=SQLITE_OK ){
1863 releasePage(pDbMemPage);
1864 goto autovacuum_out;
1865 }
danielk1977a19df672004-11-03 11:37:07 +00001866 assert( iFreePage<=origSize );
1867 }while( iFreePage>finSize );
danielk1977687566d2004-11-02 12:56:41 +00001868 releasePage(pFreeMemPage);
1869 pFreeMemPage = 0;
danielk1977687566d2004-11-02 12:56:41 +00001870
danielk1977003ba062004-11-04 02:57:33 +00001871 rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage);
danielk1977687566d2004-11-02 12:56:41 +00001872 releasePage(pDbMemPage);
danielk1977687566d2004-11-02 12:56:41 +00001873 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00001874 }
1875
1876 /* The entire free-list has been swapped to the end of the file. So
danielk1977a19df672004-11-03 11:37:07 +00001877 ** truncate the database file to finSize pages and consider the
danielk1977687566d2004-11-02 12:56:41 +00001878 ** free-list empty.
1879 */
1880 rc = sqlite3pager_write(pBt->pPage1->aData);
1881 if( rc!=SQLITE_OK ) goto autovacuum_out;
1882 put4byte(&pBt->pPage1->aData[32], 0);
1883 put4byte(&pBt->pPage1->aData[36], 0);
danielk1977687566d2004-11-02 12:56:41 +00001884 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977d761c0c2004-11-05 16:37:02 +00001885 *nTrunc = finSize;
danielk1977687566d2004-11-02 12:56:41 +00001886
1887autovacuum_out:
danielk1977687566d2004-11-02 12:56:41 +00001888 assert( nRef==*sqlite3pager_stats(pPager) );
1889 if( rc!=SQLITE_OK ){
1890 sqlite3pager_rollback(pPager);
1891 }
1892 return rc;
1893}
1894#endif
1895
1896/*
drh2aa679f2001-06-25 02:11:07 +00001897** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00001898**
1899** This will release the write lock on the database file. If there
1900** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00001901*/
drh3aac2dd2004-04-26 14:10:20 +00001902int sqlite3BtreeCommit(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00001903 int rc = SQLITE_OK;
1904 if( pBt->inTrans==TRANS_WRITE ){
1905 rc = sqlite3pager_commit(pBt->pPager);
1906 }
1907 pBt->inTrans = TRANS_NONE;
drh3aac2dd2004-04-26 14:10:20 +00001908 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00001909 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001910 return rc;
1911}
1912
danielk1977fbcd5852004-06-15 02:44:18 +00001913#ifndef NDEBUG
1914/*
1915** Return the number of write-cursors open on this handle. This is for use
1916** in assert() expressions, so it is only compiled if NDEBUG is not
1917** defined.
1918*/
1919static int countWriteCursors(Btree *pBt){
1920 BtCursor *pCur;
1921 int r = 0;
1922 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1923 if( pCur->wrFlag ) r++;
1924 }
1925 return r;
1926}
1927#endif
1928
1929#if 0
drha059ad02001-04-17 20:09:11 +00001930/*
drhc39e0002004-05-07 23:50:57 +00001931** Invalidate all cursors
1932*/
1933static void invalidateCursors(Btree *pBt){
1934 BtCursor *pCur;
1935 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1936 MemPage *pPage = pCur->pPage;
drhda200cc2004-05-09 11:51:38 +00001937 if( pPage /* && !pPage->isInit */ ){
1938 pageIntegrity(pPage);
drhc39e0002004-05-07 23:50:57 +00001939 releasePage(pPage);
1940 pCur->pPage = 0;
1941 pCur->isValid = 0;
1942 pCur->status = SQLITE_ABORT;
1943 }
1944 }
1945}
danielk1977fbcd5852004-06-15 02:44:18 +00001946#endif
drhc39e0002004-05-07 23:50:57 +00001947
drhda200cc2004-05-09 11:51:38 +00001948#ifdef SQLITE_TEST
1949/*
1950** Print debugging information about all cursors to standard output.
1951*/
1952void sqlite3BtreeCursorList(Btree *pBt){
1953 BtCursor *pCur;
1954 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1955 MemPage *pPage = pCur->pPage;
1956 char *zMode = pCur->wrFlag ? "rw" : "ro";
drhfe63d1c2004-09-08 20:13:04 +00001957 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
1958 pCur, pCur->pgnoRoot, zMode,
drhda200cc2004-05-09 11:51:38 +00001959 pPage ? pPage->pgno : 0, pCur->idx,
1960 pCur->isValid ? "" : " eof"
1961 );
1962 }
1963}
1964#endif
1965
drhc39e0002004-05-07 23:50:57 +00001966/*
drhecdc7532001-09-23 02:35:53 +00001967** Rollback the transaction in progress. All cursors will be
1968** invalided by this operation. Any attempt to use a cursor
1969** that was open at the beginning of this operation will result
1970** in an error.
drh5e00f6c2001-09-13 13:46:56 +00001971**
1972** This will release the write lock on the database file. If there
1973** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00001974*/
drh3aac2dd2004-04-26 14:10:20 +00001975int sqlite3BtreeRollback(Btree *pBt){
danielk1977cfe9a692004-06-16 12:00:29 +00001976 int rc = SQLITE_OK;
drh24cd67e2004-05-10 16:18:47 +00001977 MemPage *pPage1;
danielk1977ee5741e2004-05-31 10:01:34 +00001978 if( pBt->inTrans==TRANS_WRITE ){
drh24cd67e2004-05-10 16:18:47 +00001979 rc = sqlite3pager_rollback(pBt->pPager);
1980 /* The rollback may have destroyed the pPage1->aData value. So
1981 ** call getPage() on page 1 again to make sure pPage1->aData is
1982 ** set correctly. */
1983 if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){
1984 releasePage(pPage1);
1985 }
danielk1977fbcd5852004-06-15 02:44:18 +00001986 assert( countWriteCursors(pBt)==0 );
drh24cd67e2004-05-10 16:18:47 +00001987 }
danielk1977ee5741e2004-05-31 10:01:34 +00001988 pBt->inTrans = TRANS_NONE;
1989 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00001990 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001991 return rc;
1992}
1993
1994/*
drhab01f612004-05-22 02:55:23 +00001995** Start a statement subtransaction. The subtransaction can
1996** can be rolled back independently of the main transaction.
1997** You must start a transaction before starting a subtransaction.
1998** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00001999** commits or rolls back.
2000**
drhab01f612004-05-22 02:55:23 +00002001** Only one subtransaction may be active at a time. It is an error to try
2002** to start a new subtransaction if another subtransaction is already active.
2003**
2004** Statement subtransactions are used around individual SQL statements
2005** that are contained within a BEGIN...COMMIT block. If a constraint
2006** error occurs within the statement, the effect of that one statement
2007** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002008*/
drh3aac2dd2004-04-26 14:10:20 +00002009int sqlite3BtreeBeginStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00002010 int rc;
danielk1977ee5741e2004-05-31 10:01:34 +00002011 if( (pBt->inTrans!=TRANS_WRITE) || pBt->inStmt ){
drhf74b8d92002-09-01 23:20:45 +00002012 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh0d65dc02002-02-03 00:56:09 +00002013 }
drha34b6762004-05-07 13:30:42 +00002014 rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager);
drh3aac2dd2004-04-26 14:10:20 +00002015 pBt->inStmt = 1;
drh663fc632002-02-02 18:49:19 +00002016 return rc;
2017}
2018
2019
2020/*
drhab01f612004-05-22 02:55:23 +00002021** Commit the statment subtransaction currently in progress. If no
2022** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002023*/
drh3aac2dd2004-04-26 14:10:20 +00002024int sqlite3BtreeCommitStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00002025 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002026 if( pBt->inStmt && !pBt->readOnly ){
drha34b6762004-05-07 13:30:42 +00002027 rc = sqlite3pager_stmt_commit(pBt->pPager);
drh663fc632002-02-02 18:49:19 +00002028 }else{
2029 rc = SQLITE_OK;
2030 }
drh3aac2dd2004-04-26 14:10:20 +00002031 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00002032 return rc;
2033}
2034
2035/*
drhab01f612004-05-22 02:55:23 +00002036** Rollback the active statement subtransaction. If no subtransaction
2037** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002038**
drhab01f612004-05-22 02:55:23 +00002039** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002040** to use a cursor that was open at the beginning of this operation
2041** will result in an error.
2042*/
drh3aac2dd2004-04-26 14:10:20 +00002043int sqlite3BtreeRollbackStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00002044 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002045 if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK;
drha34b6762004-05-07 13:30:42 +00002046 rc = sqlite3pager_stmt_rollback(pBt->pPager);
danielk1977fbcd5852004-06-15 02:44:18 +00002047 assert( countWriteCursors(pBt)==0 );
drh3aac2dd2004-04-26 14:10:20 +00002048 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00002049 return rc;
2050}
2051
2052/*
drh3aac2dd2004-04-26 14:10:20 +00002053** Default key comparison function to be used if no comparison function
2054** is specified on the sqlite3BtreeCursor() call.
2055*/
2056static int dfltCompare(
2057 void *NotUsed, /* User data is not used */
2058 int n1, const void *p1, /* First key to compare */
2059 int n2, const void *p2 /* Second key to compare */
2060){
2061 int c;
2062 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
2063 if( c==0 ){
2064 c = n1 - n2;
2065 }
2066 return c;
2067}
2068
2069/*
drh8b2f49b2001-06-08 00:21:52 +00002070** Create a new cursor for the BTree whose root is on the page
2071** iTable. The act of acquiring a cursor gets a read lock on
2072** the database file.
drh1bee3d72001-10-15 00:44:35 +00002073**
2074** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002075** If wrFlag==1, then the cursor can be used for reading or for
2076** writing if other conditions for writing are also met. These
2077** are the conditions that must be met in order for writing to
2078** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002079**
drhf74b8d92002-09-01 23:20:45 +00002080** 1: The cursor must have been opened with wrFlag==1
2081**
2082** 2: No other cursors may be open with wrFlag==0 on the same table
2083**
2084** 3: The database must be writable (not on read-only media)
2085**
2086** 4: There must be an active transaction.
2087**
2088** Condition 2 warrants further discussion. If any cursor is opened
2089** on a table with wrFlag==0, that prevents all other cursors from
2090** writing to that table. This is a kind of "read-lock". When a cursor
2091** is opened with wrFlag==0 it is guaranteed that the table will not
2092** change as long as the cursor is open. This allows the cursor to
2093** do a sequential scan of the table without having to worry about
2094** entries being inserted or deleted during the scan. Cursors should
2095** be opened with wrFlag==0 only if this read-lock property is needed.
2096** That is to say, cursors should be opened with wrFlag==0 only if they
drh23e11ca2004-05-04 17:27:28 +00002097** intend to use the sqlite3BtreeNext() system call. All other cursors
drhf74b8d92002-09-01 23:20:45 +00002098** should be opened with wrFlag==1 even if they never really intend
2099** to write.
2100**
drh6446c4d2001-12-15 14:22:18 +00002101** No checking is done to make sure that page iTable really is the
2102** root page of a b-tree. If it is not, then the cursor acquired
2103** will not work correctly.
drh3aac2dd2004-04-26 14:10:20 +00002104**
2105** The comparison function must be logically the same for every cursor
2106** on a particular table. Changing the comparison function will result
2107** in incorrect operations. If the comparison function is NULL, a
2108** default comparison function is used. The comparison function is
2109** always ignored for INTKEY tables.
drha059ad02001-04-17 20:09:11 +00002110*/
drh3aac2dd2004-04-26 14:10:20 +00002111int sqlite3BtreeCursor(
2112 Btree *pBt, /* The btree */
2113 int iTable, /* Root page of table to open */
2114 int wrFlag, /* 1 to write. 0 read-only */
2115 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2116 void *pArg, /* First arg to xCompare() */
2117 BtCursor **ppCur /* Write new cursor here */
2118){
drha059ad02001-04-17 20:09:11 +00002119 int rc;
drh8dcd7ca2004-08-08 19:43:29 +00002120 BtCursor *pCur;
drhecdc7532001-09-23 02:35:53 +00002121
drh8dcd7ca2004-08-08 19:43:29 +00002122 *ppCur = 0;
2123 if( wrFlag ){
drh8dcd7ca2004-08-08 19:43:29 +00002124 if( pBt->readOnly ){
2125 return SQLITE_READONLY;
2126 }
2127 if( checkReadLocks(pBt, iTable, 0) ){
2128 return SQLITE_LOCKED;
2129 }
drha0c9a112004-03-10 13:42:37 +00002130 }
drh4b70f112004-05-02 21:12:19 +00002131 if( pBt->pPage1==0 ){
drha059ad02001-04-17 20:09:11 +00002132 rc = lockBtree(pBt);
2133 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002134 return rc;
2135 }
2136 }
drheafe05b2004-06-13 00:54:01 +00002137 pCur = sqliteMallocRaw( sizeof(*pCur) );
drha059ad02001-04-17 20:09:11 +00002138 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +00002139 rc = SQLITE_NOMEM;
2140 goto create_cursor_exception;
2141 }
drh8b2f49b2001-06-08 00:21:52 +00002142 pCur->pgnoRoot = (Pgno)iTable;
drh24cd67e2004-05-10 16:18:47 +00002143 if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){
2144 rc = SQLITE_EMPTY;
drheafe05b2004-06-13 00:54:01 +00002145 pCur->pPage = 0;
drh24cd67e2004-05-10 16:18:47 +00002146 goto create_cursor_exception;
2147 }
danielk1977369f27e2004-06-15 11:40:04 +00002148 pCur->pPage = 0; /* For exit-handler, in case getAndInitPage() fails. */
drhde647132004-05-07 17:57:49 +00002149 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
drhbd03cae2001-06-02 02:40:57 +00002150 if( rc!=SQLITE_OK ){
2151 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002152 }
drh3aac2dd2004-04-26 14:10:20 +00002153 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2154 pCur->pArg = pArg;
drh14acc042001-06-10 19:56:58 +00002155 pCur->pBt = pBt;
drhecdc7532001-09-23 02:35:53 +00002156 pCur->wrFlag = wrFlag;
drh14acc042001-06-10 19:56:58 +00002157 pCur->idx = 0;
drh59eb6762004-06-13 23:07:04 +00002158 memset(&pCur->info, 0, sizeof(pCur->info));
drha059ad02001-04-17 20:09:11 +00002159 pCur->pNext = pBt->pCursor;
2160 if( pCur->pNext ){
2161 pCur->pNext->pPrev = pCur;
2162 }
drh14acc042001-06-10 19:56:58 +00002163 pCur->pPrev = 0;
drha059ad02001-04-17 20:09:11 +00002164 pBt->pCursor = pCur;
drhc39e0002004-05-07 23:50:57 +00002165 pCur->isValid = 0;
drh2af926b2001-05-15 00:39:25 +00002166 *ppCur = pCur;
2167 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00002168
2169create_cursor_exception:
drhbd03cae2001-06-02 02:40:57 +00002170 if( pCur ){
drh3aac2dd2004-04-26 14:10:20 +00002171 releasePage(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00002172 sqliteFree(pCur);
2173 }
drh5e00f6c2001-09-13 13:46:56 +00002174 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002175 return rc;
drha059ad02001-04-17 20:09:11 +00002176}
2177
drh7a224de2004-06-02 01:22:02 +00002178#if 0 /* Not Used */
drhd3d39e92004-05-20 22:16:29 +00002179/*
2180** Change the value of the comparison function used by a cursor.
2181*/
danielk1977bf3b7212004-05-18 10:06:24 +00002182void sqlite3BtreeSetCompare(
drhd3d39e92004-05-20 22:16:29 +00002183 BtCursor *pCur, /* The cursor to whose comparison function is changed */
2184 int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */
2185 void *pArg /* First argument to xCmp() */
danielk1977bf3b7212004-05-18 10:06:24 +00002186){
2187 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2188 pCur->pArg = pArg;
2189}
drh7a224de2004-06-02 01:22:02 +00002190#endif
danielk1977bf3b7212004-05-18 10:06:24 +00002191
drha059ad02001-04-17 20:09:11 +00002192/*
drh5e00f6c2001-09-13 13:46:56 +00002193** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002194** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002195*/
drh3aac2dd2004-04-26 14:10:20 +00002196int sqlite3BtreeCloseCursor(BtCursor *pCur){
drha059ad02001-04-17 20:09:11 +00002197 Btree *pBt = pCur->pBt;
drha059ad02001-04-17 20:09:11 +00002198 if( pCur->pPrev ){
2199 pCur->pPrev->pNext = pCur->pNext;
2200 }else{
2201 pBt->pCursor = pCur->pNext;
2202 }
2203 if( pCur->pNext ){
2204 pCur->pNext->pPrev = pCur->pPrev;
2205 }
drh3aac2dd2004-04-26 14:10:20 +00002206 releasePage(pCur->pPage);
drh5e00f6c2001-09-13 13:46:56 +00002207 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00002208 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +00002209 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002210}
2211
drh7e3b0a02001-04-28 16:52:40 +00002212/*
drh5e2f8b92001-05-28 00:41:15 +00002213** Make a temporary cursor by filling in the fields of pTempCur.
2214** The temporary cursor is not on the cursor list for the Btree.
2215*/
drh14acc042001-06-10 19:56:58 +00002216static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +00002217 memcpy(pTempCur, pCur, sizeof(*pCur));
2218 pTempCur->pNext = 0;
2219 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00002220 if( pTempCur->pPage ){
drha34b6762004-05-07 13:30:42 +00002221 sqlite3pager_ref(pTempCur->pPage->aData);
drhecdc7532001-09-23 02:35:53 +00002222 }
drh5e2f8b92001-05-28 00:41:15 +00002223}
2224
2225/*
drhbd03cae2001-06-02 02:40:57 +00002226** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002227** function above.
2228*/
drh14acc042001-06-10 19:56:58 +00002229static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +00002230 if( pCur->pPage ){
drha34b6762004-05-07 13:30:42 +00002231 sqlite3pager_unref(pCur->pPage->aData);
drhecdc7532001-09-23 02:35:53 +00002232 }
drh5e2f8b92001-05-28 00:41:15 +00002233}
2234
2235/*
drh9188b382004-05-14 21:12:22 +00002236** Make sure the BtCursor.info field of the given cursor is valid.
drhab01f612004-05-22 02:55:23 +00002237** If it is not already valid, call parseCell() to fill it in.
2238**
2239** BtCursor.info is a cache of the information in the current cell.
2240** Using this cache reduces the number of calls to parseCell().
drh9188b382004-05-14 21:12:22 +00002241*/
2242static void getCellInfo(BtCursor *pCur){
drh271efa52004-05-30 19:19:05 +00002243 if( pCur->info.nSize==0 ){
drh3a41a3f2004-05-30 02:14:17 +00002244 parseCell(pCur->pPage, pCur->idx, &pCur->info);
drh9188b382004-05-14 21:12:22 +00002245 }else{
2246#ifndef NDEBUG
2247 CellInfo info;
drh51c6d962004-06-06 00:42:25 +00002248 memset(&info, 0, sizeof(info));
drh3a41a3f2004-05-30 02:14:17 +00002249 parseCell(pCur->pPage, pCur->idx, &info);
drh9188b382004-05-14 21:12:22 +00002250 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
2251#endif
2252 }
2253}
2254
2255/*
drh3aac2dd2004-04-26 14:10:20 +00002256** Set *pSize to the size of the buffer needed to hold the value of
2257** the key for the current entry. If the cursor is not pointing
2258** to a valid entry, *pSize is set to 0.
2259**
drh4b70f112004-05-02 21:12:19 +00002260** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00002261** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00002262*/
drh4a1c3802004-05-12 15:15:47 +00002263int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
danielk1977299b1872004-11-22 10:02:10 +00002264 if( !pCur->isValid ){
drh72f82862001-05-24 21:06:34 +00002265 *pSize = 0;
2266 }else{
drh9188b382004-05-14 21:12:22 +00002267 getCellInfo(pCur);
2268 *pSize = pCur->info.nKey;
drh72f82862001-05-24 21:06:34 +00002269 }
2270 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002271}
drh2af926b2001-05-15 00:39:25 +00002272
drh72f82862001-05-24 21:06:34 +00002273/*
drh0e1c19e2004-05-11 00:58:56 +00002274** Set *pSize to the number of bytes of data in the entry the
2275** cursor currently points to. Always return SQLITE_OK.
2276** Failure is not possible. If the cursor is not currently
2277** pointing to an entry (which can happen, for example, if
2278** the database is empty) then *pSize is set to 0.
2279*/
2280int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
danielk1977299b1872004-11-22 10:02:10 +00002281 if( !pCur->isValid ){
danielk197796fc5fe2004-05-13 11:34:16 +00002282 /* Not pointing at a valid entry - set *pSize to 0. */
drh0e1c19e2004-05-11 00:58:56 +00002283 *pSize = 0;
2284 }else{
drh9188b382004-05-14 21:12:22 +00002285 getCellInfo(pCur);
2286 *pSize = pCur->info.nData;
drh0e1c19e2004-05-11 00:58:56 +00002287 }
2288 return SQLITE_OK;
2289}
2290
2291/*
drh72f82862001-05-24 21:06:34 +00002292** Read payload information from the entry that the pCur cursor is
2293** pointing to. Begin reading the payload at "offset" and read
2294** a total of "amt" bytes. Put the result in zBuf.
2295**
2296** This routine does not make a distinction between key and data.
drhab01f612004-05-22 02:55:23 +00002297** It just reads bytes from the payload area. Data might appear
2298** on the main page or be scattered out on multiple overflow pages.
drh72f82862001-05-24 21:06:34 +00002299*/
drh3aac2dd2004-04-26 14:10:20 +00002300static int getPayload(
2301 BtCursor *pCur, /* Cursor pointing to entry to read from */
2302 int offset, /* Begin reading this far into payload */
2303 int amt, /* Read this many bytes */
2304 unsigned char *pBuf, /* Write the bytes into this buffer */
2305 int skipKey /* offset begins at data if this is true */
2306){
2307 unsigned char *aPayload;
drh2af926b2001-05-15 00:39:25 +00002308 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +00002309 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002310 MemPage *pPage;
2311 Btree *pBt;
drh6f11bef2004-05-13 01:12:56 +00002312 int ovflSize;
drhfa1a98a2004-05-14 19:08:17 +00002313 u32 nKey;
drh3aac2dd2004-04-26 14:10:20 +00002314
drh72f82862001-05-24 21:06:34 +00002315 assert( pCur!=0 && pCur->pPage!=0 );
drhc39e0002004-05-07 23:50:57 +00002316 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002317 pBt = pCur->pBt;
2318 pPage = pCur->pPage;
drhda200cc2004-05-09 11:51:38 +00002319 pageIntegrity(pPage);
drh3aac2dd2004-04-26 14:10:20 +00002320 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00002321 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00002322 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00002323 aPayload += pCur->info.nHeader;
drh3aac2dd2004-04-26 14:10:20 +00002324 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00002325 nKey = 0;
2326 }else{
2327 nKey = pCur->info.nKey;
drh3aac2dd2004-04-26 14:10:20 +00002328 }
2329 assert( offset>=0 );
2330 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00002331 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00002332 }
drhfa1a98a2004-05-14 19:08:17 +00002333 if( offset+amt > nKey+pCur->info.nData ){
drha34b6762004-05-07 13:30:42 +00002334 return SQLITE_ERROR;
drh3aac2dd2004-04-26 14:10:20 +00002335 }
drhfa1a98a2004-05-14 19:08:17 +00002336 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00002337 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00002338 if( a+offset>pCur->info.nLocal ){
2339 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00002340 }
drha34b6762004-05-07 13:30:42 +00002341 memcpy(pBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +00002342 if( a==amt ){
2343 return SQLITE_OK;
2344 }
drh2aa679f2001-06-25 02:11:07 +00002345 offset = 0;
drha34b6762004-05-07 13:30:42 +00002346 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00002347 amt -= a;
drhdd793422001-06-28 01:54:48 +00002348 }else{
drhfa1a98a2004-05-14 19:08:17 +00002349 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00002350 }
danielk1977cfe9a692004-06-16 12:00:29 +00002351 ovflSize = pBt->usableSize - 4;
drhbd03cae2001-06-02 02:40:57 +00002352 if( amt>0 ){
drhfa1a98a2004-05-14 19:08:17 +00002353 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977cfe9a692004-06-16 12:00:29 +00002354 while( amt>0 && nextPage ){
2355 rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload);
2356 if( rc!=0 ){
2357 return rc;
drh2af926b2001-05-15 00:39:25 +00002358 }
danielk1977cfe9a692004-06-16 12:00:29 +00002359 nextPage = get4byte(aPayload);
2360 if( offset<ovflSize ){
2361 int a = amt;
2362 if( a + offset > ovflSize ){
2363 a = ovflSize - offset;
2364 }
2365 memcpy(pBuf, &aPayload[offset+4], a);
2366 offset = 0;
2367 amt -= a;
2368 pBuf += a;
2369 }else{
2370 offset -= ovflSize;
2371 }
2372 sqlite3pager_unref(aPayload);
drh2af926b2001-05-15 00:39:25 +00002373 }
drh2af926b2001-05-15 00:39:25 +00002374 }
danielk1977cfe9a692004-06-16 12:00:29 +00002375
drha7fcb052001-12-14 15:09:55 +00002376 if( amt>0 ){
drhee696e22004-08-30 16:52:17 +00002377 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
drha7fcb052001-12-14 15:09:55 +00002378 }
2379 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00002380}
2381
drh72f82862001-05-24 21:06:34 +00002382/*
drh3aac2dd2004-04-26 14:10:20 +00002383** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002384** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002385** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00002386**
drh3aac2dd2004-04-26 14:10:20 +00002387** Return SQLITE_OK on success or an error code if anything goes
2388** wrong. An error is returned if "offset+amt" is larger than
2389** the available payload.
drh72f82862001-05-24 21:06:34 +00002390*/
drha34b6762004-05-07 13:30:42 +00002391int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
danielk197728129562005-01-11 10:25:06 +00002392 assert( pCur->isValid );
drhc39e0002004-05-07 23:50:57 +00002393 assert( pCur->pPage!=0 );
2394 assert( pCur->pPage->intKey==0 );
2395 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh3aac2dd2004-04-26 14:10:20 +00002396 return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
2397}
2398
2399/*
drh3aac2dd2004-04-26 14:10:20 +00002400** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002401** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002402** begins at "offset".
2403**
2404** Return SQLITE_OK on success or an error code if anything goes
2405** wrong. An error is returned if "offset+amt" is larger than
2406** the available payload.
drh72f82862001-05-24 21:06:34 +00002407*/
drh3aac2dd2004-04-26 14:10:20 +00002408int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
danielk197728129562005-01-11 10:25:06 +00002409 assert( pCur->isValid );
drh8c1238a2003-01-02 14:43:55 +00002410 assert( pCur->pPage!=0 );
drhc39e0002004-05-07 23:50:57 +00002411 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh3aac2dd2004-04-26 14:10:20 +00002412 return getPayload(pCur, offset, amt, pBuf, 1);
drh2af926b2001-05-15 00:39:25 +00002413}
2414
drh72f82862001-05-24 21:06:34 +00002415/*
drh0e1c19e2004-05-11 00:58:56 +00002416** Return a pointer to payload information from the entry that the
2417** pCur cursor is pointing to. The pointer is to the beginning of
2418** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00002419** skipKey==1. The number of bytes of available key/data is written
2420** into *pAmt. If *pAmt==0, then the value returned will not be
2421** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00002422**
2423** This routine is an optimization. It is common for the entire key
2424** and data to fit on the local page and for there to be no overflow
2425** pages. When that is so, this routine can be used to access the
2426** key and data without making a copy. If the key and/or data spills
2427** onto overflow pages, then getPayload() must be used to reassembly
2428** the key/data and copy it into a preallocated buffer.
2429**
2430** The pointer returned by this routine looks directly into the cached
2431** page of the database. The data might change or move the next time
2432** any btree routine is called.
2433*/
2434static const unsigned char *fetchPayload(
2435 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00002436 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00002437 int skipKey /* read beginning at data if this is true */
2438){
2439 unsigned char *aPayload;
2440 MemPage *pPage;
2441 Btree *pBt;
drhfa1a98a2004-05-14 19:08:17 +00002442 u32 nKey;
2443 int nLocal;
drh0e1c19e2004-05-11 00:58:56 +00002444
2445 assert( pCur!=0 && pCur->pPage!=0 );
2446 assert( pCur->isValid );
2447 pBt = pCur->pBt;
2448 pPage = pCur->pPage;
2449 pageIntegrity(pPage);
2450 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00002451 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00002452 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00002453 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00002454 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00002455 nKey = 0;
2456 }else{
2457 nKey = pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00002458 }
drh0e1c19e2004-05-11 00:58:56 +00002459 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00002460 aPayload += nKey;
2461 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00002462 }else{
drhfa1a98a2004-05-14 19:08:17 +00002463 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00002464 if( nLocal>nKey ){
2465 nLocal = nKey;
2466 }
drh0e1c19e2004-05-11 00:58:56 +00002467 }
drhe51c44f2004-05-30 20:46:09 +00002468 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00002469 return aPayload;
2470}
2471
2472
2473/*
drhe51c44f2004-05-30 20:46:09 +00002474** For the entry that cursor pCur is point to, return as
2475** many bytes of the key or data as are available on the local
2476** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00002477**
2478** The pointer returned is ephemeral. The key/data may move
2479** or be destroyed on the next call to any Btree routine.
2480**
2481** These routines is used to get quick access to key and data
2482** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00002483*/
drhe51c44f2004-05-30 20:46:09 +00002484const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
2485 return (const void*)fetchPayload(pCur, pAmt, 0);
drh0e1c19e2004-05-11 00:58:56 +00002486}
drhe51c44f2004-05-30 20:46:09 +00002487const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
2488 return (const void*)fetchPayload(pCur, pAmt, 1);
drh0e1c19e2004-05-11 00:58:56 +00002489}
2490
2491
2492/*
drh8178a752003-01-05 21:41:40 +00002493** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00002494** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00002495*/
drh3aac2dd2004-04-26 14:10:20 +00002496static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00002497 int rc;
2498 MemPage *pNewPage;
drh3aac2dd2004-04-26 14:10:20 +00002499 MemPage *pOldPage;
drh0d316a42002-08-11 20:10:47 +00002500 Btree *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00002501
drhc39e0002004-05-07 23:50:57 +00002502 assert( pCur->isValid );
drhde647132004-05-07 17:57:49 +00002503 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00002504 if( rc ) return rc;
drhda200cc2004-05-09 11:51:38 +00002505 pageIntegrity(pNewPage);
drh428ae8c2003-01-04 16:48:09 +00002506 pNewPage->idxParent = pCur->idx;
drh3aac2dd2004-04-26 14:10:20 +00002507 pOldPage = pCur->pPage;
2508 pOldPage->idxShift = 0;
2509 releasePage(pOldPage);
drh72f82862001-05-24 21:06:34 +00002510 pCur->pPage = pNewPage;
2511 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00002512 pCur->info.nSize = 0;
drh4be295b2003-12-16 03:44:47 +00002513 if( pNewPage->nCell<1 ){
drhee696e22004-08-30 16:52:17 +00002514 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
drh4be295b2003-12-16 03:44:47 +00002515 }
drh72f82862001-05-24 21:06:34 +00002516 return SQLITE_OK;
2517}
2518
2519/*
drh8856d6a2004-04-29 14:42:46 +00002520** Return true if the page is the virtual root of its table.
2521**
2522** The virtual root page is the root page for most tables. But
2523** for the table rooted on page 1, sometime the real root page
2524** is empty except for the right-pointer. In such cases the
2525** virtual root page is the page that the right-pointer of page
2526** 1 is pointing to.
2527*/
2528static int isRootPage(MemPage *pPage){
2529 MemPage *pParent = pPage->pParent;
drhda200cc2004-05-09 11:51:38 +00002530 if( pParent==0 ) return 1;
2531 if( pParent->pgno>1 ) return 0;
2532 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
drh8856d6a2004-04-29 14:42:46 +00002533 return 0;
2534}
2535
2536/*
drh5e2f8b92001-05-28 00:41:15 +00002537** Move the cursor up to the parent page.
2538**
2539** pCur->idx is set to the cell index that contains the pointer
2540** to the page we are coming from. If we are coming from the
2541** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00002542** the largest cell index.
drh72f82862001-05-24 21:06:34 +00002543*/
drh8178a752003-01-05 21:41:40 +00002544static void moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00002545 Pgno oldPgno;
2546 MemPage *pParent;
drh8178a752003-01-05 21:41:40 +00002547 MemPage *pPage;
drh428ae8c2003-01-04 16:48:09 +00002548 int idxParent;
drh3aac2dd2004-04-26 14:10:20 +00002549
drhc39e0002004-05-07 23:50:57 +00002550 assert( pCur->isValid );
drh8178a752003-01-05 21:41:40 +00002551 pPage = pCur->pPage;
2552 assert( pPage!=0 );
drh8856d6a2004-04-29 14:42:46 +00002553 assert( !isRootPage(pPage) );
drhda200cc2004-05-09 11:51:38 +00002554 pageIntegrity(pPage);
drh8178a752003-01-05 21:41:40 +00002555 pParent = pPage->pParent;
2556 assert( pParent!=0 );
drhda200cc2004-05-09 11:51:38 +00002557 pageIntegrity(pParent);
drh8178a752003-01-05 21:41:40 +00002558 idxParent = pPage->idxParent;
drha34b6762004-05-07 13:30:42 +00002559 sqlite3pager_ref(pParent->aData);
drh3aac2dd2004-04-26 14:10:20 +00002560 oldPgno = pPage->pgno;
2561 releasePage(pPage);
drh72f82862001-05-24 21:06:34 +00002562 pCur->pPage = pParent;
drh271efa52004-05-30 19:19:05 +00002563 pCur->info.nSize = 0;
drh428ae8c2003-01-04 16:48:09 +00002564 assert( pParent->idxShift==0 );
drh43605152004-05-29 21:46:49 +00002565 pCur->idx = idxParent;
drh72f82862001-05-24 21:06:34 +00002566}
2567
2568/*
2569** Move the cursor to the root page
2570*/
drh5e2f8b92001-05-28 00:41:15 +00002571static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00002572 MemPage *pRoot;
drhbd03cae2001-06-02 02:40:57 +00002573 int rc;
drh0d316a42002-08-11 20:10:47 +00002574 Btree *pBt = pCur->pBt;
drhbd03cae2001-06-02 02:40:57 +00002575
drhde647132004-05-07 17:57:49 +00002576 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0);
drhc39e0002004-05-07 23:50:57 +00002577 if( rc ){
2578 pCur->isValid = 0;
2579 return rc;
2580 }
drh3aac2dd2004-04-26 14:10:20 +00002581 releasePage(pCur->pPage);
drhda200cc2004-05-09 11:51:38 +00002582 pageIntegrity(pRoot);
drh3aac2dd2004-04-26 14:10:20 +00002583 pCur->pPage = pRoot;
drh72f82862001-05-24 21:06:34 +00002584 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00002585 pCur->info.nSize = 0;
drh8856d6a2004-04-29 14:42:46 +00002586 if( pRoot->nCell==0 && !pRoot->leaf ){
2587 Pgno subpage;
2588 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00002589 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00002590 assert( subpage>0 );
drh3644f082004-05-10 18:45:09 +00002591 pCur->isValid = 1;
drh4b70f112004-05-02 21:12:19 +00002592 rc = moveToChild(pCur, subpage);
drh8856d6a2004-04-29 14:42:46 +00002593 }
drhc39e0002004-05-07 23:50:57 +00002594 pCur->isValid = pCur->pPage->nCell>0;
drh8856d6a2004-04-29 14:42:46 +00002595 return rc;
drh72f82862001-05-24 21:06:34 +00002596}
drh2af926b2001-05-15 00:39:25 +00002597
drh5e2f8b92001-05-28 00:41:15 +00002598/*
2599** Move the cursor down to the left-most leaf entry beneath the
2600** entry to which it is currently pointing.
2601*/
2602static int moveToLeftmost(BtCursor *pCur){
2603 Pgno pgno;
2604 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002605 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00002606
drhc39e0002004-05-07 23:50:57 +00002607 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002608 while( !(pPage = pCur->pPage)->leaf ){
drha34b6762004-05-07 13:30:42 +00002609 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00002610 pgno = get4byte(findCell(pPage, pCur->idx));
drh8178a752003-01-05 21:41:40 +00002611 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00002612 if( rc ) return rc;
2613 }
2614 return SQLITE_OK;
2615}
2616
drh2dcc9aa2002-12-04 13:40:25 +00002617/*
2618** Move the cursor down to the right-most leaf entry beneath the
2619** page to which it is currently pointing. Notice the difference
2620** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
2621** finds the left-most entry beneath the *entry* whereas moveToRightmost()
2622** finds the right-most entry beneath the *page*.
2623*/
2624static int moveToRightmost(BtCursor *pCur){
2625 Pgno pgno;
2626 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002627 MemPage *pPage;
drh2dcc9aa2002-12-04 13:40:25 +00002628
drhc39e0002004-05-07 23:50:57 +00002629 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002630 while( !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:49 +00002631 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh3aac2dd2004-04-26 14:10:20 +00002632 pCur->idx = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00002633 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00002634 if( rc ) return rc;
2635 }
drh3aac2dd2004-04-26 14:10:20 +00002636 pCur->idx = pPage->nCell - 1;
drh271efa52004-05-30 19:19:05 +00002637 pCur->info.nSize = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002638 return SQLITE_OK;
2639}
2640
drh5e00f6c2001-09-13 13:46:56 +00002641/* Move the cursor to the first entry in the table. Return SQLITE_OK
2642** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00002643** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00002644*/
drh3aac2dd2004-04-26 14:10:20 +00002645int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00002646 int rc;
2647 rc = moveToRoot(pCur);
2648 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002649 if( pCur->isValid==0 ){
2650 assert( pCur->pPage->nCell==0 );
drh5e00f6c2001-09-13 13:46:56 +00002651 *pRes = 1;
2652 return SQLITE_OK;
2653 }
drhc39e0002004-05-07 23:50:57 +00002654 assert( pCur->pPage->nCell>0 );
drh5e00f6c2001-09-13 13:46:56 +00002655 *pRes = 0;
2656 rc = moveToLeftmost(pCur);
2657 return rc;
2658}
drh5e2f8b92001-05-28 00:41:15 +00002659
drh9562b552002-02-19 15:00:07 +00002660/* Move the cursor to the last entry in the table. Return SQLITE_OK
2661** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00002662** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00002663*/
drh3aac2dd2004-04-26 14:10:20 +00002664int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00002665 int rc;
drh9562b552002-02-19 15:00:07 +00002666 rc = moveToRoot(pCur);
2667 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002668 if( pCur->isValid==0 ){
2669 assert( pCur->pPage->nCell==0 );
drh9562b552002-02-19 15:00:07 +00002670 *pRes = 1;
2671 return SQLITE_OK;
2672 }
drhc39e0002004-05-07 23:50:57 +00002673 assert( pCur->isValid );
drh9562b552002-02-19 15:00:07 +00002674 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002675 rc = moveToRightmost(pCur);
drh9562b552002-02-19 15:00:07 +00002676 return rc;
2677}
2678
drh3aac2dd2004-04-26 14:10:20 +00002679/* Move the cursor so that it points to an entry near pKey/nKey.
drh72f82862001-05-24 21:06:34 +00002680** Return a success code.
2681**
drh3aac2dd2004-04-26 14:10:20 +00002682** For INTKEY tables, only the nKey parameter is used. pKey is
2683** ignored. For other tables, nKey is the number of bytes of data
2684** in nKey. The comparison function specified when the cursor was
2685** created is used to compare keys.
2686**
drh5e2f8b92001-05-28 00:41:15 +00002687** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00002688** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00002689** were present. The cursor might point to an entry that comes
2690** before or after the key.
2691**
drhbd03cae2001-06-02 02:40:57 +00002692** The result of comparing the key with the entry to which the
drhab01f612004-05-22 02:55:23 +00002693** cursor is written to *pRes if pRes!=NULL. The meaning of
drhbd03cae2001-06-02 02:40:57 +00002694** this value is as follows:
2695**
2696** *pRes<0 The cursor is left pointing at an entry that
drh1a844c32002-12-04 22:29:28 +00002697** is smaller than pKey or if the table is empty
2698** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00002699**
2700** *pRes==0 The cursor is left pointing at an entry that
2701** exactly matches pKey.
2702**
2703** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00002704** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00002705*/
drh4a1c3802004-05-12 15:15:47 +00002706int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){
drh72f82862001-05-24 21:06:34 +00002707 int rc;
drh5e2f8b92001-05-28 00:41:15 +00002708 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00002709 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002710 assert( pCur->pPage );
2711 assert( pCur->pPage->isInit );
2712 if( pCur->isValid==0 ){
drhf328bc82004-05-10 23:29:49 +00002713 *pRes = -1;
drhc39e0002004-05-07 23:50:57 +00002714 assert( pCur->pPage->nCell==0 );
2715 return SQLITE_OK;
2716 }
drh4eec4c12005-01-21 00:22:37 +00002717 for(;;){
drh72f82862001-05-24 21:06:34 +00002718 int lwr, upr;
2719 Pgno chldPg;
2720 MemPage *pPage = pCur->pPage;
drh1a844c32002-12-04 22:29:28 +00002721 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00002722 lwr = 0;
2723 upr = pPage->nCell-1;
drh4eec4c12005-01-21 00:22:37 +00002724 if( !pPage->intKey && pKey==0 ){
2725 return SQLITE_CORRUPT;
2726 }
drhda200cc2004-05-09 11:51:38 +00002727 pageIntegrity(pPage);
drh72f82862001-05-24 21:06:34 +00002728 while( lwr<=upr ){
danielk197713adf8a2004-06-03 16:08:41 +00002729 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00002730 i64 nCellKey;
drh72f82862001-05-24 21:06:34 +00002731 pCur->idx = (lwr+upr)/2;
drh271efa52004-05-30 19:19:05 +00002732 pCur->info.nSize = 0;
drhde647132004-05-07 17:57:49 +00002733 sqlite3BtreeKeySize(pCur, &nCellKey);
drh3aac2dd2004-04-26 14:10:20 +00002734 if( pPage->intKey ){
2735 if( nCellKey<nKey ){
2736 c = -1;
2737 }else if( nCellKey>nKey ){
2738 c = +1;
2739 }else{
2740 c = 0;
2741 }
drh3aac2dd2004-04-26 14:10:20 +00002742 }else{
drhe51c44f2004-05-30 20:46:09 +00002743 int available;
danielk197713adf8a2004-06-03 16:08:41 +00002744 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drhe51c44f2004-05-30 20:46:09 +00002745 if( available>=nCellKey ){
2746 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
2747 }else{
2748 pCellKey = sqliteMallocRaw( nCellKey );
2749 if( pCellKey==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00002750 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
drhe51c44f2004-05-30 20:46:09 +00002751 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
2752 sqliteFree(pCellKey);
2753 if( rc ) return rc;
2754 }
drh3aac2dd2004-04-26 14:10:20 +00002755 }
drh72f82862001-05-24 21:06:34 +00002756 if( c==0 ){
drh8b18dd42004-05-12 19:18:15 +00002757 if( pPage->leafData && !pPage->leaf ){
drhfc70e6f2004-05-12 21:11:27 +00002758 lwr = pCur->idx;
2759 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00002760 break;
2761 }else{
drh8b18dd42004-05-12 19:18:15 +00002762 if( pRes ) *pRes = 0;
2763 return SQLITE_OK;
2764 }
drh72f82862001-05-24 21:06:34 +00002765 }
2766 if( c<0 ){
2767 lwr = pCur->idx+1;
2768 }else{
2769 upr = pCur->idx-1;
2770 }
2771 }
2772 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00002773 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00002774 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00002775 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00002776 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00002777 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00002778 }else{
drh43605152004-05-29 21:46:49 +00002779 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00002780 }
2781 if( chldPg==0 ){
drhc39e0002004-05-07 23:50:57 +00002782 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00002783 if( pRes ) *pRes = c;
2784 return SQLITE_OK;
2785 }
drh428ae8c2003-01-04 16:48:09 +00002786 pCur->idx = lwr;
drh271efa52004-05-30 19:19:05 +00002787 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00002788 rc = moveToChild(pCur, chldPg);
drhc39e0002004-05-07 23:50:57 +00002789 if( rc ){
2790 return rc;
2791 }
drh72f82862001-05-24 21:06:34 +00002792 }
drhbd03cae2001-06-02 02:40:57 +00002793 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00002794}
2795
2796/*
drhc39e0002004-05-07 23:50:57 +00002797** Return TRUE if the cursor is not pointing at an entry of the table.
2798**
2799** TRUE will be returned after a call to sqlite3BtreeNext() moves
2800** past the last entry in the table or sqlite3BtreePrev() moves past
2801** the first entry. TRUE is also returned if the table is empty.
2802*/
2803int sqlite3BtreeEof(BtCursor *pCur){
2804 return pCur->isValid==0;
2805}
2806
2807/*
drhbd03cae2001-06-02 02:40:57 +00002808** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00002809** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00002810** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00002811** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00002812*/
drh3aac2dd2004-04-26 14:10:20 +00002813int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00002814 int rc;
drh8178a752003-01-05 21:41:40 +00002815 MemPage *pPage = pCur->pPage;
drh8b18dd42004-05-12 19:18:15 +00002816
drh8c1238a2003-01-02 14:43:55 +00002817 assert( pRes!=0 );
drhc39e0002004-05-07 23:50:57 +00002818 if( pCur->isValid==0 ){
drh8c1238a2003-01-02 14:43:55 +00002819 *pRes = 1;
drhc39e0002004-05-07 23:50:57 +00002820 return SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +00002821 }
drh8178a752003-01-05 21:41:40 +00002822 assert( pPage->isInit );
drh8178a752003-01-05 21:41:40 +00002823 assert( pCur->idx<pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00002824
drh72f82862001-05-24 21:06:34 +00002825 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00002826 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00002827 if( pCur->idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00002828 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00002829 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00002830 if( rc ) return rc;
2831 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00002832 *pRes = 0;
2833 return rc;
drh72f82862001-05-24 21:06:34 +00002834 }
drh5e2f8b92001-05-28 00:41:15 +00002835 do{
drh8856d6a2004-04-29 14:42:46 +00002836 if( isRootPage(pPage) ){
drh8c1238a2003-01-02 14:43:55 +00002837 *pRes = 1;
drhc39e0002004-05-07 23:50:57 +00002838 pCur->isValid = 0;
drh5e2f8b92001-05-28 00:41:15 +00002839 return SQLITE_OK;
2840 }
drh8178a752003-01-05 21:41:40 +00002841 moveToParent(pCur);
2842 pPage = pCur->pPage;
2843 }while( pCur->idx>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00002844 *pRes = 0;
drh8b18dd42004-05-12 19:18:15 +00002845 if( pPage->leafData ){
2846 rc = sqlite3BtreeNext(pCur, pRes);
2847 }else{
2848 rc = SQLITE_OK;
2849 }
2850 return rc;
drh8178a752003-01-05 21:41:40 +00002851 }
2852 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00002853 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00002854 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00002855 }
drh5e2f8b92001-05-28 00:41:15 +00002856 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00002857 return rc;
drh72f82862001-05-24 21:06:34 +00002858}
2859
drh3b7511c2001-05-26 13:15:44 +00002860/*
drh2dcc9aa2002-12-04 13:40:25 +00002861** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00002862** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00002863** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00002864** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00002865*/
drh3aac2dd2004-04-26 14:10:20 +00002866int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00002867 int rc;
2868 Pgno pgno;
drh8178a752003-01-05 21:41:40 +00002869 MemPage *pPage;
drhc39e0002004-05-07 23:50:57 +00002870 if( pCur->isValid==0 ){
2871 *pRes = 1;
2872 return SQLITE_OK;
2873 }
danielk19776a43f9b2004-11-16 04:57:24 +00002874
drh8178a752003-01-05 21:41:40 +00002875 pPage = pCur->pPage;
drh8178a752003-01-05 21:41:40 +00002876 assert( pPage->isInit );
drh2dcc9aa2002-12-04 13:40:25 +00002877 assert( pCur->idx>=0 );
drha34b6762004-05-07 13:30:42 +00002878 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00002879 pgno = get4byte( findCell(pPage, pCur->idx) );
drh8178a752003-01-05 21:41:40 +00002880 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00002881 if( rc ) return rc;
2882 rc = moveToRightmost(pCur);
2883 }else{
2884 while( pCur->idx==0 ){
drh8856d6a2004-04-29 14:42:46 +00002885 if( isRootPage(pPage) ){
drhc39e0002004-05-07 23:50:57 +00002886 pCur->isValid = 0;
2887 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00002888 return SQLITE_OK;
2889 }
drh8178a752003-01-05 21:41:40 +00002890 moveToParent(pCur);
2891 pPage = pCur->pPage;
drh2dcc9aa2002-12-04 13:40:25 +00002892 }
2893 pCur->idx--;
drh271efa52004-05-30 19:19:05 +00002894 pCur->info.nSize = 0;
drh8237d452004-11-22 19:07:09 +00002895 if( pPage->leafData && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00002896 rc = sqlite3BtreePrevious(pCur, pRes);
2897 }else{
2898 rc = SQLITE_OK;
2899 }
drh2dcc9aa2002-12-04 13:40:25 +00002900 }
drh8178a752003-01-05 21:41:40 +00002901 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002902 return rc;
2903}
2904
2905/*
drh3b7511c2001-05-26 13:15:44 +00002906** Allocate a new page from the database file.
2907**
drha34b6762004-05-07 13:30:42 +00002908** The new page is marked as dirty. (In other words, sqlite3pager_write()
drh3b7511c2001-05-26 13:15:44 +00002909** has already been called on the new page.) The new page has also
2910** been referenced and the calling routine is responsible for calling
drha34b6762004-05-07 13:30:42 +00002911** sqlite3pager_unref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00002912**
2913** SQLITE_OK is returned on success. Any other return value indicates
2914** an error. *ppPage and *pPgno are undefined in the event of an error.
drha34b6762004-05-07 13:30:42 +00002915** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00002916**
drh199e3cf2002-07-18 11:01:47 +00002917** If the "nearby" parameter is not 0, then a (feeble) effort is made to
2918** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00002919** attempt to keep related pages close to each other in the database file,
2920** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00002921**
2922** If the "exact" parameter is not 0, and the page-number nearby exists
2923** anywhere on the free-list, then it is guarenteed to be returned. This
2924** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00002925*/
danielk1977cb1a7eb2004-11-05 12:27:02 +00002926static int allocatePage(
2927 Btree *pBt,
2928 MemPage **ppPage,
2929 Pgno *pPgno,
2930 Pgno nearby,
2931 u8 exact
2932){
drh3aac2dd2004-04-26 14:10:20 +00002933 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00002934 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002935 int n; /* Number of pages on the freelist */
2936 int k; /* Number of leaves on the trunk of the freelist */
drh30e58752002-03-02 20:41:57 +00002937
drh3aac2dd2004-04-26 14:10:20 +00002938 pPage1 = pBt->pPage1;
2939 n = get4byte(&pPage1->aData[36]);
2940 if( n>0 ){
drh91025292004-05-03 19:49:32 +00002941 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00002942 MemPage *pTrunk = 0;
2943 Pgno iTrunk;
2944 MemPage *pPrevTrunk = 0;
2945 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
2946
2947 /* If the 'exact' parameter was true and a query of the pointer-map
2948 ** shows that the page 'nearby' is somewhere on the free-list, then
2949 ** the entire-list will be searched for that page.
2950 */
2951#ifndef SQLITE_OMIT_AUTOVACUUM
2952 if( exact ){
2953 u8 eType;
2954 assert( nearby>0 );
2955 assert( pBt->autoVacuum );
2956 rc = ptrmapGet(pBt, nearby, &eType, 0);
2957 if( rc ) return rc;
2958 if( eType==PTRMAP_FREEPAGE ){
2959 searchList = 1;
2960 }
2961 *pPgno = nearby;
2962 }
2963#endif
2964
2965 /* Decrement the free-list count by 1. Set iTrunk to the index of the
2966 ** first free-list trunk page. iPrevTrunk is initially 1.
2967 */
drha34b6762004-05-07 13:30:42 +00002968 rc = sqlite3pager_write(pPage1->aData);
drh3b7511c2001-05-26 13:15:44 +00002969 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00002970 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00002971
2972 /* The code within this loop is run only once if the 'searchList' variable
2973 ** is not true. Otherwise, it runs once for each trunk-page on the
2974 ** free-list until the page 'nearby' is located.
2975 */
2976 do {
2977 pPrevTrunk = pTrunk;
2978 if( pPrevTrunk ){
2979 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00002980 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00002981 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00002982 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00002983 rc = getPage(pBt, iTrunk, &pTrunk);
2984 if( rc ){
2985 releasePage(pPrevTrunk);
2986 return rc;
2987 }
2988
2989 /* TODO: This should move to after the loop? */
2990 rc = sqlite3pager_write(pTrunk->aData);
2991 if( rc ){
2992 releasePage(pTrunk);
2993 releasePage(pPrevTrunk);
2994 return rc;
2995 }
2996
2997 k = get4byte(&pTrunk->aData[4]);
2998 if( k==0 && !searchList ){
2999 /* The trunk has no leaves and the list is not being searched.
3000 ** So extract the trunk page itself and use it as the newly
3001 ** allocated page */
3002 assert( pPrevTrunk==0 );
3003 *pPgno = iTrunk;
3004 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3005 *ppPage = pTrunk;
3006 pTrunk = 0;
3007 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3008 }else if( k>pBt->usableSize/4 - 8 ){
3009 /* Value of k is out of range. Database corruption */
drhee696e22004-08-30 16:52:17 +00003010 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
danielk1977cb1a7eb2004-11-05 12:27:02 +00003011#ifndef SQLITE_OMIT_AUTOVACUUM
3012 }else if( searchList && nearby==iTrunk ){
3013 /* The list is being searched and this trunk page is the page
3014 ** to allocate, regardless of whether it has leaves.
3015 */
3016 assert( *pPgno==iTrunk );
3017 *ppPage = pTrunk;
3018 searchList = 0;
3019 if( k==0 ){
3020 if( !pPrevTrunk ){
3021 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
3022 }else{
3023 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
3024 }
3025 }else{
3026 /* The trunk page is required by the caller but it contains
3027 ** pointers to free-list leaves. The first leaf becomes a trunk
3028 ** page in this case.
3029 */
3030 MemPage *pNewTrunk;
3031 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
3032 rc = getPage(pBt, iNewTrunk, &pNewTrunk);
3033 if( rc!=SQLITE_OK ){
3034 releasePage(pTrunk);
3035 releasePage(pPrevTrunk);
3036 return rc;
3037 }
3038 rc = sqlite3pager_write(pNewTrunk->aData);
3039 if( rc!=SQLITE_OK ){
3040 releasePage(pNewTrunk);
3041 releasePage(pTrunk);
3042 releasePage(pPrevTrunk);
3043 return rc;
3044 }
3045 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
3046 put4byte(&pNewTrunk->aData[4], k-1);
3047 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
3048 if( !pPrevTrunk ){
3049 put4byte(&pPage1->aData[32], iNewTrunk);
3050 }else{
3051 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
3052 }
3053 releasePage(pNewTrunk);
3054 }
3055 pTrunk = 0;
3056 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3057#endif
3058 }else{
3059 /* Extract a leaf from the trunk */
3060 int closest;
3061 Pgno iPage;
3062 unsigned char *aData = pTrunk->aData;
3063 if( nearby>0 ){
3064 int i, dist;
3065 closest = 0;
3066 dist = get4byte(&aData[8]) - nearby;
3067 if( dist<0 ) dist = -dist;
3068 for(i=1; i<k; i++){
3069 int d2 = get4byte(&aData[8+i*4]) - nearby;
3070 if( d2<0 ) d2 = -d2;
3071 if( d2<dist ){
3072 closest = i;
3073 dist = d2;
3074 }
3075 }
3076 }else{
3077 closest = 0;
3078 }
3079
3080 iPage = get4byte(&aData[8+closest*4]);
3081 if( !searchList || iPage==nearby ){
3082 *pPgno = iPage;
3083 if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){
3084 /* Free page off the end of the file */
3085 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
3086 }
3087 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
3088 ": %d more free pages\n",
3089 *pPgno, closest+1, k, pTrunk->pgno, n-1));
3090 if( closest<k-1 ){
3091 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
3092 }
3093 put4byte(&aData[4], k-1);
3094 rc = getPage(pBt, *pPgno, ppPage);
3095 if( rc==SQLITE_OK ){
3096 sqlite3pager_dont_rollback((*ppPage)->aData);
3097 rc = sqlite3pager_write((*ppPage)->aData);
danielk1977aac0a382005-01-16 11:07:06 +00003098 if( rc!=SQLITE_OK ){
3099 releasePage(*ppPage);
3100 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003101 }
3102 searchList = 0;
3103 }
drhee696e22004-08-30 16:52:17 +00003104 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003105 releasePage(pPrevTrunk);
3106 }while( searchList );
3107 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003108 }else{
drh3aac2dd2004-04-26 14:10:20 +00003109 /* There are no pages on the freelist, so create a new page at the
3110 ** end of the file */
drha34b6762004-05-07 13:30:42 +00003111 *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1;
danielk1977afcdd022004-10-31 16:25:42 +00003112
3113#ifndef SQLITE_OMIT_AUTOVACUUM
drh42cac6d2004-11-20 20:31:11 +00003114 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt->usableSize, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00003115 /* If *pPgno refers to a pointer-map page, allocate two new pages
3116 ** at the end of the file instead of one. The first allocated page
3117 ** becomes a new pointer-map page, the second is used by the caller.
3118 */
3119 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00003120 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00003121 (*pPgno)++;
3122 }
3123#endif
3124
danielk1977599fcba2004-11-08 07:13:13 +00003125 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00003126 rc = getPage(pBt, *pPgno, ppPage);
drh3b7511c2001-05-26 13:15:44 +00003127 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00003128 rc = sqlite3pager_write((*ppPage)->aData);
danielk1977aac0a382005-01-16 11:07:06 +00003129 if( rc!=SQLITE_OK ){
3130 releasePage(*ppPage);
3131 }
drh3a4c1412004-05-09 20:40:11 +00003132 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00003133 }
danielk1977599fcba2004-11-08 07:13:13 +00003134
3135 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh3b7511c2001-05-26 13:15:44 +00003136 return rc;
3137}
3138
3139/*
drh3aac2dd2004-04-26 14:10:20 +00003140** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00003141**
drha34b6762004-05-07 13:30:42 +00003142** sqlite3pager_unref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00003143*/
drh3aac2dd2004-04-26 14:10:20 +00003144static int freePage(MemPage *pPage){
3145 Btree *pBt = pPage->pBt;
3146 MemPage *pPage1 = pBt->pPage1;
3147 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00003148
drh3aac2dd2004-04-26 14:10:20 +00003149 /* Prepare the page for freeing */
3150 assert( pPage->pgno>1 );
3151 pPage->isInit = 0;
3152 releasePage(pPage->pParent);
3153 pPage->pParent = 0;
3154
drha34b6762004-05-07 13:30:42 +00003155 /* Increment the free page count on pPage1 */
3156 rc = sqlite3pager_write(pPage1->aData);
drh3aac2dd2004-04-26 14:10:20 +00003157 if( rc ) return rc;
3158 n = get4byte(&pPage1->aData[36]);
3159 put4byte(&pPage1->aData[36], n+1);
3160
danielk1977687566d2004-11-02 12:56:41 +00003161#ifndef SQLITE_OMIT_AUTOVACUUM
3162 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00003163 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00003164 */
3165 if( pBt->autoVacuum ){
3166 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
danielk1977a64a0352004-11-05 01:45:13 +00003167 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00003168 }
3169#endif
3170
drh3aac2dd2004-04-26 14:10:20 +00003171 if( n==0 ){
3172 /* This is the first free page */
drhda200cc2004-05-09 11:51:38 +00003173 rc = sqlite3pager_write(pPage->aData);
3174 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003175 memset(pPage->aData, 0, 8);
drha34b6762004-05-07 13:30:42 +00003176 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003177 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003178 }else{
3179 /* Other free pages already exist. Retrive the first trunk page
3180 ** of the freelist and find out how many leaves it has. */
drha34b6762004-05-07 13:30:42 +00003181 MemPage *pTrunk;
3182 rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003183 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003184 k = get4byte(&pTrunk->aData[4]);
drhee696e22004-08-30 16:52:17 +00003185 if( k>=pBt->usableSize/4 - 8 ){
drh3aac2dd2004-04-26 14:10:20 +00003186 /* The trunk is full. Turn the page being freed into a new
3187 ** trunk page with no leaves. */
drha34b6762004-05-07 13:30:42 +00003188 rc = sqlite3pager_write(pPage->aData);
drh3aac2dd2004-04-26 14:10:20 +00003189 if( rc ) return rc;
3190 put4byte(pPage->aData, pTrunk->pgno);
3191 put4byte(&pPage->aData[4], 0);
3192 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003193 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
3194 pPage->pgno, pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003195 }else{
3196 /* Add the newly freed page as a leaf on the current trunk */
drha34b6762004-05-07 13:30:42 +00003197 rc = sqlite3pager_write(pTrunk->aData);
drh3aac2dd2004-04-26 14:10:20 +00003198 if( rc ) return rc;
3199 put4byte(&pTrunk->aData[4], k+1);
3200 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drha34b6762004-05-07 13:30:42 +00003201 sqlite3pager_dont_write(pBt->pPager, pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003202 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003203 }
3204 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003205 }
drh3b7511c2001-05-26 13:15:44 +00003206 return rc;
3207}
3208
3209/*
drh3aac2dd2004-04-26 14:10:20 +00003210** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00003211*/
drh3aac2dd2004-04-26 14:10:20 +00003212static int clearCell(MemPage *pPage, unsigned char *pCell){
3213 Btree *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00003214 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00003215 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00003216 int rc;
drh3b7511c2001-05-26 13:15:44 +00003217
drh43605152004-05-29 21:46:49 +00003218 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003219 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00003220 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00003221 }
drh6f11bef2004-05-13 01:12:56 +00003222 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh3aac2dd2004-04-26 14:10:20 +00003223 while( ovflPgno!=0 ){
3224 MemPage *pOvfl;
3225 rc = getPage(pBt, ovflPgno, &pOvfl);
drh3b7511c2001-05-26 13:15:44 +00003226 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003227 ovflPgno = get4byte(pOvfl->aData);
drha34b6762004-05-07 13:30:42 +00003228 rc = freePage(pOvfl);
drhbd03cae2001-06-02 02:40:57 +00003229 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00003230 sqlite3pager_unref(pOvfl->aData);
drh3b7511c2001-05-26 13:15:44 +00003231 }
drh5e2f8b92001-05-28 00:41:15 +00003232 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00003233}
3234
3235/*
drh91025292004-05-03 19:49:32 +00003236** Create the byte sequence used to represent a cell on page pPage
3237** and write that byte sequence into pCell[]. Overflow pages are
3238** allocated and filled in as necessary. The calling procedure
3239** is responsible for making sure sufficient space has been allocated
3240** for pCell[].
3241**
3242** Note that pCell does not necessary need to point to the pPage->aData
3243** area. pCell might point to some temporary storage. The cell will
3244** be constructed in this temporary area then copied into pPage->aData
3245** later.
drh3b7511c2001-05-26 13:15:44 +00003246*/
3247static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00003248 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00003249 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00003250 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00003251 const void *pData,int nData, /* The data */
3252 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00003253){
drh3b7511c2001-05-26 13:15:44 +00003254 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00003255 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00003256 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00003257 int spaceLeft;
3258 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00003259 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00003260 unsigned char *pPrior;
3261 unsigned char *pPayload;
3262 Btree *pBt = pPage->pBt;
3263 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00003264 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00003265 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00003266
drh91025292004-05-03 19:49:32 +00003267 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00003268 nHeader = 0;
drh91025292004-05-03 19:49:32 +00003269 if( !pPage->leaf ){
3270 nHeader += 4;
3271 }
drh8b18dd42004-05-12 19:18:15 +00003272 if( pPage->hasData ){
drh91025292004-05-03 19:49:32 +00003273 nHeader += putVarint(&pCell[nHeader], nData);
drh6f11bef2004-05-13 01:12:56 +00003274 }else{
drh91025292004-05-03 19:49:32 +00003275 nData = 0;
3276 }
drh6f11bef2004-05-13 01:12:56 +00003277 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh43605152004-05-29 21:46:49 +00003278 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003279 assert( info.nHeader==nHeader );
3280 assert( info.nKey==nKey );
3281 assert( info.nData==nData );
3282
3283 /* Fill in the payload */
drh3aac2dd2004-04-26 14:10:20 +00003284 nPayload = nData;
3285 if( pPage->intKey ){
3286 pSrc = pData;
3287 nSrc = nData;
drh91025292004-05-03 19:49:32 +00003288 nData = 0;
drh3aac2dd2004-04-26 14:10:20 +00003289 }else{
3290 nPayload += nKey;
3291 pSrc = pKey;
3292 nSrc = nKey;
3293 }
drh6f11bef2004-05-13 01:12:56 +00003294 *pnSize = info.nSize;
3295 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00003296 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00003297 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00003298
drh3b7511c2001-05-26 13:15:44 +00003299 while( nPayload>0 ){
3300 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00003301#ifndef SQLITE_OMIT_AUTOVACUUM
3302 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
3303#endif
danielk1977cb1a7eb2004-11-05 12:27:02 +00003304 rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00003305#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00003306 /* If the database supports auto-vacuum, and the second or subsequent
3307 ** overflow page is being allocated, add an entry to the pointer-map
3308 ** for that page now. The entry for the first overflow page will be
3309 ** added later, by the insertCell() routine.
danielk1977afcdd022004-10-31 16:25:42 +00003310 */
danielk1977a19df672004-11-03 11:37:07 +00003311 if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
3312 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap);
danielk1977afcdd022004-10-31 16:25:42 +00003313 }
3314#endif
drh3b7511c2001-05-26 13:15:44 +00003315 if( rc ){
drh9b171272004-05-08 02:03:22 +00003316 releasePage(pToRelease);
danielk197728129562005-01-11 10:25:06 +00003317 /* clearCell(pPage, pCell); */
drh3b7511c2001-05-26 13:15:44 +00003318 return rc;
3319 }
drh3aac2dd2004-04-26 14:10:20 +00003320 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00003321 releasePage(pToRelease);
3322 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00003323 pPrior = pOvfl->aData;
3324 put4byte(pPrior, 0);
3325 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00003326 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00003327 }
3328 n = nPayload;
3329 if( n>spaceLeft ) n = spaceLeft;
drh3aac2dd2004-04-26 14:10:20 +00003330 if( n>nSrc ) n = nSrc;
3331 memcpy(pPayload, pSrc, n);
drh3b7511c2001-05-26 13:15:44 +00003332 nPayload -= n;
drhde647132004-05-07 17:57:49 +00003333 pPayload += n;
drh9b171272004-05-08 02:03:22 +00003334 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00003335 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00003336 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00003337 if( nSrc==0 ){
3338 nSrc = nData;
3339 pSrc = pData;
3340 }
drhdd793422001-06-28 01:54:48 +00003341 }
drh9b171272004-05-08 02:03:22 +00003342 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00003343 return SQLITE_OK;
3344}
3345
3346/*
drhbd03cae2001-06-02 02:40:57 +00003347** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00003348** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00003349** pointer in the third argument.
3350*/
danielk1977afcdd022004-10-31 16:25:42 +00003351static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){
drhbd03cae2001-06-02 02:40:57 +00003352 MemPage *pThis;
drh4b70f112004-05-02 21:12:19 +00003353 unsigned char *aData;
drhbd03cae2001-06-02 02:40:57 +00003354
danielk1977afcdd022004-10-31 16:25:42 +00003355 if( pgno==0 ) return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00003356 assert( pBt->pPager!=0 );
drha34b6762004-05-07 13:30:42 +00003357 aData = sqlite3pager_lookup(pBt->pPager, pgno);
drhda200cc2004-05-09 11:51:38 +00003358 if( aData ){
drh887dc4c2004-10-22 16:22:57 +00003359 pThis = (MemPage*)&aData[pBt->psAligned];
drh31276532004-09-27 12:20:52 +00003360 assert( pThis->aData==aData );
drhda200cc2004-05-09 11:51:38 +00003361 if( pThis->isInit ){
3362 if( pThis->pParent!=pNewParent ){
3363 if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData);
3364 pThis->pParent = pNewParent;
3365 if( pNewParent ) sqlite3pager_ref(pNewParent->aData);
3366 }
3367 pThis->idxParent = idx;
drhdd793422001-06-28 01:54:48 +00003368 }
drha34b6762004-05-07 13:30:42 +00003369 sqlite3pager_unref(aData);
drhbd03cae2001-06-02 02:40:57 +00003370 }
danielk1977afcdd022004-10-31 16:25:42 +00003371
3372#ifndef SQLITE_OMIT_AUTOVACUUM
3373 if( pBt->autoVacuum ){
3374 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
3375 }
3376#endif
3377 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00003378}
3379
danielk1977ac11ee62005-01-15 12:45:51 +00003380
3381
drhbd03cae2001-06-02 02:40:57 +00003382/*
drh4b70f112004-05-02 21:12:19 +00003383** Change the pParent pointer of all children of pPage to point back
3384** to pPage.
3385**
drhbd03cae2001-06-02 02:40:57 +00003386** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00003387** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00003388**
3389** This routine gets called after you memcpy() one page into
3390** another.
3391*/
danielk1977afcdd022004-10-31 16:25:42 +00003392static int reparentChildPages(MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00003393 int i;
danielk1977afcdd022004-10-31 16:25:42 +00003394 Btree *pBt = pPage->pBt;
3395 int rc = SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00003396
danielk1977afcdd022004-10-31 16:25:42 +00003397 if( pPage->leaf ) return SQLITE_OK;
danielk1977afcdd022004-10-31 16:25:42 +00003398
drhbd03cae2001-06-02 02:40:57 +00003399 for(i=0; i<pPage->nCell; i++){
danielk1977afcdd022004-10-31 16:25:42 +00003400 u8 *pCell = findCell(pPage, i);
3401 if( !pPage->leaf ){
3402 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
3403 if( rc!=SQLITE_OK ) return rc;
3404 }
drhbd03cae2001-06-02 02:40:57 +00003405 }
danielk1977afcdd022004-10-31 16:25:42 +00003406 if( !pPage->leaf ){
3407 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
3408 pPage, i);
3409 pPage->idxShift = 0;
3410 }
3411 return rc;
drh14acc042001-06-10 19:56:58 +00003412}
3413
3414/*
3415** Remove the i-th cell from pPage. This routine effects pPage only.
3416** The cell content is not freed or deallocated. It is assumed that
3417** the cell content has been copied someplace else. This routine just
3418** removes the reference to the cell from pPage.
3419**
3420** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00003421*/
drh4b70f112004-05-02 21:12:19 +00003422static void dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00003423 int i; /* Loop counter */
3424 int pc; /* Offset to cell content of cell being deleted */
3425 u8 *data; /* pPage->aData */
3426 u8 *ptr; /* Used to move bytes around within data[] */
3427
drh8c42ca92001-06-22 19:15:00 +00003428 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00003429 assert( sz==cellSize(pPage, idx) );
drha34b6762004-05-07 13:30:42 +00003430 assert( sqlite3pager_iswriteable(pPage->aData) );
drhda200cc2004-05-09 11:51:38 +00003431 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00003432 ptr = &data[pPage->cellOffset + 2*idx];
3433 pc = get2byte(ptr);
3434 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
drhde647132004-05-07 17:57:49 +00003435 freeSpace(pPage, pc, sz);
drh43605152004-05-29 21:46:49 +00003436 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
3437 ptr[0] = ptr[2];
3438 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00003439 }
3440 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00003441 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
3442 pPage->nFree += 2;
drh428ae8c2003-01-04 16:48:09 +00003443 pPage->idxShift = 1;
drh14acc042001-06-10 19:56:58 +00003444}
3445
3446/*
3447** Insert a new cell on pPage at cell index "i". pCell points to the
3448** content of the cell.
3449**
3450** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00003451** will not fit, then make a copy of the cell content into pTemp if
3452** pTemp is not null. Regardless of pTemp, allocate a new entry
3453** in pPage->aOvfl[] and make it point to the cell content (either
3454** in pTemp or the original pCell) and also record its index.
3455** Allocating a new entry in pPage->aCell[] implies that
3456** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00003457**
3458** If nSkip is non-zero, then do not copy the first nSkip bytes of the
3459** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00003460** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00003461** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00003462*/
danielk1977e80463b2004-11-03 03:01:16 +00003463static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00003464 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00003465 int i, /* New cell becomes the i-th cell of the page */
3466 u8 *pCell, /* Content of the new cell */
3467 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00003468 u8 *pTemp, /* Temp storage space for pCell, if needed */
3469 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00003470){
drh43605152004-05-29 21:46:49 +00003471 int idx; /* Where to write new cell content in data[] */
3472 int j; /* Loop counter */
3473 int top; /* First byte of content for any cell in data[] */
3474 int end; /* First byte past the last cell pointer in data[] */
3475 int ins; /* Index in data[] where new cell pointer is inserted */
3476 int hdr; /* Offset into data[] of the page header */
3477 int cellOffset; /* Address of first cell pointer in data[] */
3478 u8 *data; /* The content of the whole page */
3479 u8 *ptr; /* Used for moving information around in data[] */
3480
3481 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
3482 assert( sz==cellSizePtr(pPage, pCell) );
drha34b6762004-05-07 13:30:42 +00003483 assert( sqlite3pager_iswriteable(pPage->aData) );
drh43605152004-05-29 21:46:49 +00003484 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00003485 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00003486 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00003487 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00003488 }
drh43605152004-05-29 21:46:49 +00003489 j = pPage->nOverflow++;
3490 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
3491 pPage->aOvfl[j].pCell = pCell;
3492 pPage->aOvfl[j].idx = i;
3493 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00003494 }else{
drh43605152004-05-29 21:46:49 +00003495 data = pPage->aData;
3496 hdr = pPage->hdrOffset;
3497 top = get2byte(&data[hdr+5]);
3498 cellOffset = pPage->cellOffset;
3499 end = cellOffset + 2*pPage->nCell + 2;
3500 ins = cellOffset + 2*i;
3501 if( end > top - sz ){
3502 defragmentPage(pPage);
3503 top = get2byte(&data[hdr+5]);
3504 assert( end + sz <= top );
3505 }
3506 idx = allocateSpace(pPage, sz);
3507 assert( idx>0 );
3508 assert( end <= get2byte(&data[hdr+5]) );
3509 pPage->nCell++;
3510 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00003511 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00003512 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
3513 ptr[0] = ptr[-2];
3514 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00003515 }
drh43605152004-05-29 21:46:49 +00003516 put2byte(&data[ins], idx);
3517 put2byte(&data[hdr+3], pPage->nCell);
3518 pPage->idxShift = 1;
drhda200cc2004-05-09 11:51:38 +00003519 pageIntegrity(pPage);
danielk1977a19df672004-11-03 11:37:07 +00003520#ifndef SQLITE_OMIT_AUTOVACUUM
3521 if( pPage->pBt->autoVacuum ){
3522 /* The cell may contain a pointer to an overflow page. If so, write
3523 ** the entry for the overflow page into the pointer map.
3524 */
3525 CellInfo info;
3526 parseCellPtr(pPage, pCell, &info);
3527 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
3528 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
3529 int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
3530 if( rc!=SQLITE_OK ) return rc;
3531 }
3532 }
3533#endif
drh14acc042001-06-10 19:56:58 +00003534 }
danielk1977e80463b2004-11-03 03:01:16 +00003535
danielk1977e80463b2004-11-03 03:01:16 +00003536 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00003537}
3538
3539/*
drhfa1a98a2004-05-14 19:08:17 +00003540** Add a list of cells to a page. The page should be initially empty.
3541** The cells are guaranteed to fit on the page.
3542*/
3543static void assemblePage(
3544 MemPage *pPage, /* The page to be assemblied */
3545 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00003546 u8 **apCell, /* Pointers to cell bodies */
drhfa1a98a2004-05-14 19:08:17 +00003547 int *aSize /* Sizes of the cells */
3548){
3549 int i; /* Loop counter */
3550 int totalSize; /* Total size of all cells */
3551 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00003552 int cellptr; /* Address of next cell pointer */
3553 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00003554 u8 *data; /* Data for the page */
3555
drh43605152004-05-29 21:46:49 +00003556 assert( pPage->nOverflow==0 );
drhfa1a98a2004-05-14 19:08:17 +00003557 totalSize = 0;
3558 for(i=0; i<nCell; i++){
3559 totalSize += aSize[i];
3560 }
drh43605152004-05-29 21:46:49 +00003561 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00003562 assert( pPage->nCell==0 );
drh43605152004-05-29 21:46:49 +00003563 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00003564 data = pPage->aData;
3565 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00003566 put2byte(&data[hdr+3], nCell);
3567 cellbody = allocateSpace(pPage, totalSize);
3568 assert( cellbody>0 );
3569 assert( pPage->nFree >= 2*nCell );
3570 pPage->nFree -= 2*nCell;
drhfa1a98a2004-05-14 19:08:17 +00003571 for(i=0; i<nCell; i++){
drh43605152004-05-29 21:46:49 +00003572 put2byte(&data[cellptr], cellbody);
3573 memcpy(&data[cellbody], apCell[i], aSize[i]);
3574 cellptr += 2;
3575 cellbody += aSize[i];
drhfa1a98a2004-05-14 19:08:17 +00003576 }
drh43605152004-05-29 21:46:49 +00003577 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00003578 pPage->nCell = nCell;
drhfa1a98a2004-05-14 19:08:17 +00003579}
3580
drh14acc042001-06-10 19:56:58 +00003581/*
drhc3b70572003-01-04 19:44:07 +00003582** The following parameters determine how many adjacent pages get involved
3583** in a balancing operation. NN is the number of neighbors on either side
3584** of the page that participate in the balancing operation. NB is the
3585** total number of pages that participate, including the target page and
3586** NN neighbors on either side.
3587**
3588** The minimum value of NN is 1 (of course). Increasing NN above 1
3589** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
3590** in exchange for a larger degradation in INSERT and UPDATE performance.
3591** The value of NN appears to give the best results overall.
3592*/
3593#define NN 1 /* Number of neighbors on either side of pPage */
3594#define NB (NN*2+1) /* Total pages involved in the balance */
3595
drh43605152004-05-29 21:46:49 +00003596/* Forward reference */
danielk1977ac245ec2005-01-14 13:50:11 +00003597static int balance(MemPage*, int);
3598
drh615ae552005-01-16 23:21:00 +00003599#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00003600/*
3601** This version of balance() handles the common special case where
3602** a new entry is being inserted on the extreme right-end of the
3603** tree, in other words, when the new entry will become the largest
3604** entry in the tree.
3605**
3606** Instead of trying balance the 3 right-most leaf pages, just add
3607** a new page to the right-hand side and put the one new entry in
3608** that page. This leaves the right side of the tree somewhat
3609** unbalanced. But odds are that we will be inserting new entries
3610** at the end soon afterwards so the nearly empty page will quickly
3611** fill up. On average.
3612**
3613** pPage is the leaf page which is the right-most page in the tree.
3614** pParent is its parent. pPage must have a single overflow entry
3615** which is also the right-most entry on the page.
3616*/
danielk1977ac245ec2005-01-14 13:50:11 +00003617static int balance_quick(MemPage *pPage, MemPage *pParent){
3618 int rc;
3619 MemPage *pNew;
3620 Pgno pgnoNew;
3621 u8 *pCell;
3622 int szCell;
3623 CellInfo info;
danielk1977ac11ee62005-01-15 12:45:51 +00003624 Btree *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00003625 int parentIdx = pParent->nCell; /* pParent new divider cell index */
3626 int parentSize; /* Size of new divider cell */
3627 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00003628
3629 /* Allocate a new page. Insert the overflow cell from pPage
3630 ** into it. Then remove the overflow cell from pPage.
3631 */
danielk1977ac11ee62005-01-15 12:45:51 +00003632 rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977ac245ec2005-01-14 13:50:11 +00003633 if( rc!=SQLITE_OK ){
3634 return rc;
3635 }
3636 pCell = pPage->aOvfl[0].pCell;
3637 szCell = cellSizePtr(pPage, pCell);
3638 zeroPage(pNew, pPage->aData[0]);
3639 assemblePage(pNew, 1, &pCell, &szCell);
3640 pPage->nOverflow = 0;
3641
danielk197779a40da2005-01-16 08:00:01 +00003642 /* Set the parent of the newly allocated page to pParent. */
3643 pNew->pParent = pParent;
3644 sqlite3pager_ref(pParent->aData);
3645
danielk1977ac245ec2005-01-14 13:50:11 +00003646 /* pPage is currently the right-child of pParent. Change this
3647 ** so that the right-child is the new page allocated above and
danielk197779a40da2005-01-16 08:00:01 +00003648 ** pPage is the next-to-right child.
danielk1977ac245ec2005-01-14 13:50:11 +00003649 */
danielk1977ac11ee62005-01-15 12:45:51 +00003650 assert( pPage->nCell>0 );
danielk1977ac245ec2005-01-14 13:50:11 +00003651 parseCellPtr(pPage, findCell(pPage, pPage->nCell-1), &info);
3652 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, &parentSize);
3653 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00003654 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00003655 }
3656 assert( parentSize<64 );
3657 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
3658 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00003659 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00003660 }
3661 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
3662 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
3663
danielk197779a40da2005-01-16 08:00:01 +00003664#ifndef SQLITE_OMIT_AUTOVACUUM
3665 /* If this is an auto-vacuum database, update the pointer map
3666 ** with entries for the new page, and any pointer from the
3667 ** cell on the page to an overflow page.
3668 */
danielk1977ac11ee62005-01-15 12:45:51 +00003669 if( pBt->autoVacuum ){
3670 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
3671 if( rc!=SQLITE_OK ){
3672 return rc;
3673 }
danielk197779a40da2005-01-16 08:00:01 +00003674 rc = ptrmapPutOvfl(pNew, 0);
3675 if( rc!=SQLITE_OK ){
3676 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00003677 }
3678 }
danielk197779a40da2005-01-16 08:00:01 +00003679#endif
danielk1977ac11ee62005-01-15 12:45:51 +00003680
danielk197779a40da2005-01-16 08:00:01 +00003681 /* Release the reference to the new page and balance the parent page,
3682 ** in case the divider cell inserted caused it to become overfull.
3683 */
danielk1977ac245ec2005-01-14 13:50:11 +00003684 releasePage(pNew);
3685 return balance(pParent, 0);
3686}
drh615ae552005-01-16 23:21:00 +00003687#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00003688
drhc3b70572003-01-04 19:44:07 +00003689/*
danielk1977ac11ee62005-01-15 12:45:51 +00003690** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
3691** if the database supports auto-vacuum or not. Because it is used
3692** within an expression that is an argument to another macro
3693** (sqliteMallocRaw), it is not possible to use conditional compilation.
3694** So, this macro is defined instead.
3695*/
3696#ifndef SQLITE_OMIT_AUTOVACUUM
3697#define ISAUTOVACUUM (pBt->autoVacuum)
3698#else
3699#define ISAUTOVACUUM 0
3700#endif
3701
3702/*
drhab01f612004-05-22 02:55:23 +00003703** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00003704** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00003705** Usually NN siblings on either side of pPage is used in the balancing,
3706** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00003707** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00003708** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00003709** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00003710**
drh0c6cc4e2004-06-15 02:13:26 +00003711** The number of siblings of pPage might be increased or decreased by one or
3712** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00003713** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00003714** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00003715** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00003716** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00003717**
drh8b2f49b2001-06-08 00:21:52 +00003718** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00003719** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00003720** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00003721** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00003722**
drh8c42ca92001-06-22 19:15:00 +00003723** In the course of balancing the siblings of pPage, the parent of pPage
3724** might become overfull or underfull. If that happens, then this routine
3725** is called recursively on the parent.
3726**
drh5e00f6c2001-09-13 13:46:56 +00003727** If this routine fails for any reason, it might leave the database
3728** in a corrupted state. So if this routine fails, the database should
3729** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00003730*/
drh43605152004-05-29 21:46:49 +00003731static int balance_nonroot(MemPage *pPage){
drh8b2f49b2001-06-08 00:21:52 +00003732 MemPage *pParent; /* The parent of pPage */
drh4b70f112004-05-02 21:12:19 +00003733 Btree *pBt; /* The whole database */
danielk1977cfe9a692004-06-16 12:00:29 +00003734 int nCell = 0; /* Number of cells in aCell[] */
drh8b2f49b2001-06-08 00:21:52 +00003735 int nOld; /* Number of pages in apOld[] */
3736 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00003737 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00003738 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00003739 int idx; /* Index of pPage in pParent->aCell[] */
3740 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00003741 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00003742 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00003743 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00003744 int usableSpace; /* Bytes in pPage beyond the header */
3745 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00003746 int subtotal; /* Subtotal of bytes in cells on one page */
drhb6f41482004-05-14 01:58:11 +00003747 int iSpace = 0; /* First unused byte of aSpace[] */
drh2e38c322004-09-03 18:38:44 +00003748 int mxCellPerPage; /* Maximum number of cells in one page */
drhc3b70572003-01-04 19:44:07 +00003749 MemPage *apOld[NB]; /* pPage and up to two siblings */
3750 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00003751 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00003752 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
3753 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drhc3b70572003-01-04 19:44:07 +00003754 int idxDiv[NB]; /* Indices of divider cells in pParent */
drh4b70f112004-05-02 21:12:19 +00003755 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00003756 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
3757 int szNew[NB+2]; /* Combined size of cells place on i-th page */
drh2e38c322004-09-03 18:38:44 +00003758 u8 **apCell; /* All cells begin balanced */
3759 int *szCell; /* Local size of all cells in apCell[] */
3760 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
3761 u8 *aSpace; /* Space to hold copies of dividers cells */
danielk19774e17d142005-01-16 09:06:33 +00003762#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00003763 u8 *aFrom = 0;
3764#endif
drh8b2f49b2001-06-08 00:21:52 +00003765
drh14acc042001-06-10 19:56:58 +00003766 /*
drh43605152004-05-29 21:46:49 +00003767 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00003768 */
drh3a4c1412004-05-09 20:40:11 +00003769 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00003770 assert( sqlite3pager_iswriteable(pPage->aData) );
drh4b70f112004-05-02 21:12:19 +00003771 pBt = pPage->pBt;
drh14acc042001-06-10 19:56:58 +00003772 pParent = pPage->pParent;
drh43605152004-05-29 21:46:49 +00003773 sqlite3pager_write(pParent->aData);
3774 assert( pParent );
3775 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00003776
drh615ae552005-01-16 23:21:00 +00003777#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00003778 /*
3779 ** A special case: If a new entry has just been inserted into a
3780 ** table (that is, a btree with integer keys and all data at the leaves)
3781 ** an the new entry is the right-most entry in the tree (it has the
3782 ** largest key) then use the special balance_quick() routine for
3783 ** balancing. balance_quick() is much faster and results in a tighter
3784 ** packing of data in the common case.
3785 */
danielk1977ac245ec2005-01-14 13:50:11 +00003786 if( pPage->leaf &&
3787 pPage->intKey &&
3788 pPage->leafData &&
3789 pPage->nOverflow==1 &&
3790 pPage->aOvfl[0].idx==pPage->nCell &&
danielk1977ac11ee62005-01-15 12:45:51 +00003791 pPage->pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00003792 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
3793 ){
danielk1977ac11ee62005-01-15 12:45:51 +00003794 /*
3795 ** TODO: Check the siblings to the left of pPage. It may be that
3796 ** they are not full and no new page is required.
3797 */
danielk1977ac245ec2005-01-14 13:50:11 +00003798 return balance_quick(pPage, pParent);
3799 }
3800#endif
3801
drh2e38c322004-09-03 18:38:44 +00003802 /*
3803 ** Allocate space for memory structures
3804 */
3805 mxCellPerPage = MX_CELL(pBt);
3806 apCell = sqliteMallocRaw(
3807 (mxCellPerPage+2)*NB*(sizeof(u8*)+sizeof(int))
3808 + sizeof(MemPage)*NB
drh887dc4c2004-10-22 16:22:57 +00003809 + pBt->psAligned*(5+NB)
danielk1977ac11ee62005-01-15 12:45:51 +00003810 + (ISAUTOVACUUM ? (mxCellPerPage+2)*NN*2 : 0)
drh2e38c322004-09-03 18:38:44 +00003811 );
3812 if( apCell==0 ){
3813 return SQLITE_NOMEM;
3814 }
3815 szCell = (int*)&apCell[(mxCellPerPage+2)*NB];
3816 aCopy[0] = (u8*)&szCell[(mxCellPerPage+2)*NB];
3817 for(i=1; i<NB; i++){
drh887dc4c2004-10-22 16:22:57 +00003818 aCopy[i] = &aCopy[i-1][pBt->psAligned+sizeof(MemPage)];
drh2e38c322004-09-03 18:38:44 +00003819 }
drh887dc4c2004-10-22 16:22:57 +00003820 aSpace = &aCopy[NB-1][pBt->psAligned+sizeof(MemPage)];
danielk1977ac11ee62005-01-15 12:45:51 +00003821#ifndef SQLITE_OMIT_AUTOVACUUM
3822 if( pBt->autoVacuum ){
3823 aFrom = &aSpace[5*pBt->psAligned];
3824 }
3825#endif
drh14acc042001-06-10 19:56:58 +00003826
drh8b2f49b2001-06-08 00:21:52 +00003827 /*
drh4b70f112004-05-02 21:12:19 +00003828 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00003829 ** to pPage. The "idx" variable is the index of that cell. If pPage
3830 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00003831 */
drhbb49aba2003-01-04 18:53:27 +00003832 if( pParent->idxShift ){
drha34b6762004-05-07 13:30:42 +00003833 Pgno pgno;
drh4b70f112004-05-02 21:12:19 +00003834 pgno = pPage->pgno;
drha34b6762004-05-07 13:30:42 +00003835 assert( pgno==sqlite3pager_pagenumber(pPage->aData) );
drhbb49aba2003-01-04 18:53:27 +00003836 for(idx=0; idx<pParent->nCell; idx++){
drh43605152004-05-29 21:46:49 +00003837 if( get4byte(findCell(pParent, idx))==pgno ){
drhbb49aba2003-01-04 18:53:27 +00003838 break;
3839 }
drh8b2f49b2001-06-08 00:21:52 +00003840 }
drh4b70f112004-05-02 21:12:19 +00003841 assert( idx<pParent->nCell
drh43605152004-05-29 21:46:49 +00003842 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
drhbb49aba2003-01-04 18:53:27 +00003843 }else{
3844 idx = pPage->idxParent;
drh8b2f49b2001-06-08 00:21:52 +00003845 }
drh8b2f49b2001-06-08 00:21:52 +00003846
3847 /*
drh14acc042001-06-10 19:56:58 +00003848 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00003849 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00003850 */
drh14acc042001-06-10 19:56:58 +00003851 nOld = nNew = 0;
drha34b6762004-05-07 13:30:42 +00003852 sqlite3pager_ref(pParent->aData);
drh14acc042001-06-10 19:56:58 +00003853
3854 /*
drh4b70f112004-05-02 21:12:19 +00003855 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00003856 ** the siblings. An attempt is made to find NN siblings on either
3857 ** side of pPage. More siblings are taken from one side, however, if
3858 ** pPage there are fewer than NN siblings on the other side. If pParent
3859 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00003860 */
drhc3b70572003-01-04 19:44:07 +00003861 nxDiv = idx - NN;
3862 if( nxDiv + NB > pParent->nCell ){
3863 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00003864 }
drhc3b70572003-01-04 19:44:07 +00003865 if( nxDiv<0 ){
3866 nxDiv = 0;
3867 }
drh8b2f49b2001-06-08 00:21:52 +00003868 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00003869 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00003870 if( k<pParent->nCell ){
3871 idxDiv[i] = k;
drh43605152004-05-29 21:46:49 +00003872 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00003873 nDiv++;
drha34b6762004-05-07 13:30:42 +00003874 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00003875 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00003876 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00003877 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00003878 }else{
3879 break;
drh8b2f49b2001-06-08 00:21:52 +00003880 }
drhde647132004-05-07 17:57:49 +00003881 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00003882 if( rc ) goto balance_cleanup;
drh428ae8c2003-01-04 16:48:09 +00003883 apOld[i]->idxParent = k;
drh91025292004-05-03 19:49:32 +00003884 apCopy[i] = 0;
3885 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00003886 nOld++;
drh8b2f49b2001-06-08 00:21:52 +00003887 }
3888
3889 /*
drh14acc042001-06-10 19:56:58 +00003890 ** Make copies of the content of pPage and its siblings into aOld[].
3891 ** The rest of this function will use data from the copies rather
3892 ** that the original pages since the original pages will be in the
3893 ** process of being overwritten.
3894 */
3895 for(i=0; i<nOld; i++){
drh887dc4c2004-10-22 16:22:57 +00003896 MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned];
3897 p->aData = &((u8*)p)[-pBt->psAligned];
3898 memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage));
3899 p->aData = &((u8*)p)[-pBt->psAligned];
drh14acc042001-06-10 19:56:58 +00003900 }
3901
3902 /*
3903 ** Load pointers to all cells on sibling pages and the divider cells
3904 ** into the local apCell[] array. Make copies of the divider cells
drhb6f41482004-05-14 01:58:11 +00003905 ** into space obtained form aSpace[] and remove the the divider Cells
3906 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00003907 **
3908 ** If the siblings are on leaf pages, then the child pointers of the
3909 ** divider cells are stripped from the cells before they are copied
drh96f5b762004-05-16 16:24:36 +00003910 ** into aSpace[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00003911 ** child pointers. If siblings are not leaves, then all cell in
3912 ** apCell[] include child pointers. Either way, all cells in apCell[]
3913 ** are alike.
drh96f5b762004-05-16 16:24:36 +00003914 **
3915 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
3916 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00003917 */
3918 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00003919 leafCorrection = pPage->leaf*4;
drh8b18dd42004-05-12 19:18:15 +00003920 leafData = pPage->leafData && pPage->leaf;
drh8b2f49b2001-06-08 00:21:52 +00003921 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00003922 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00003923 int limit = pOld->nCell+pOld->nOverflow;
3924 for(j=0; j<limit; j++){
3925 apCell[nCell] = findOverflowCell(pOld, j);
3926 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk1977ac11ee62005-01-15 12:45:51 +00003927#ifndef SQLITE_OMIT_AUTOVACUUM
3928 if( pBt->autoVacuum ){
3929 int a;
3930 aFrom[nCell] = i;
3931 for(a=0; a<pOld->nOverflow; a++){
3932 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
3933 aFrom[nCell] = 0xFF;
3934 break;
3935 }
3936 }
3937 }
3938#endif
drh14acc042001-06-10 19:56:58 +00003939 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00003940 }
3941 if( i<nOld-1 ){
drh43605152004-05-29 21:46:49 +00003942 int sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00003943 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00003944 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
3945 ** are duplicates of keys on the child pages. We need to remove
3946 ** the divider cells from pParent, but the dividers cells are not
3947 ** added to apCell[] because they are duplicates of child cells.
3948 */
drh8b18dd42004-05-12 19:18:15 +00003949 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00003950 }else{
drhb6f41482004-05-14 01:58:11 +00003951 u8 *pTemp;
3952 szCell[nCell] = sz;
3953 pTemp = &aSpace[iSpace];
3954 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00003955 assert( iSpace<=pBt->psAligned*5 );
drhb6f41482004-05-14 01:58:11 +00003956 memcpy(pTemp, apDiv[i], sz);
3957 apCell[nCell] = pTemp+leafCorrection;
danielk1977ac11ee62005-01-15 12:45:51 +00003958#ifndef SQLITE_OMIT_AUTOVACUUM
3959 if( pBt->autoVacuum ){
3960 aFrom[nCell] = 0xFF;
3961 }
3962#endif
drhb6f41482004-05-14 01:58:11 +00003963 dropCell(pParent, nxDiv, sz);
drh8b18dd42004-05-12 19:18:15 +00003964 szCell[nCell] -= leafCorrection;
drh43605152004-05-29 21:46:49 +00003965 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00003966 if( !pOld->leaf ){
3967 assert( leafCorrection==0 );
3968 /* The right pointer of the child page pOld becomes the left
3969 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00003970 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00003971 }else{
3972 assert( leafCorrection==4 );
3973 }
3974 nCell++;
drh4b70f112004-05-02 21:12:19 +00003975 }
drh8b2f49b2001-06-08 00:21:52 +00003976 }
3977 }
3978
3979 /*
drh6019e162001-07-02 17:51:45 +00003980 ** Figure out the number of pages needed to hold all nCell cells.
3981 ** Store this number in "k". Also compute szNew[] which is the total
3982 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00003983 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00003984 ** cntNew[k] should equal nCell.
3985 **
drh96f5b762004-05-16 16:24:36 +00003986 ** Values computed by this block:
3987 **
3988 ** k: The total number of sibling pages
3989 ** szNew[i]: Spaced used on the i-th sibling page.
3990 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
3991 ** the right of the i-th sibling page.
3992 ** usableSpace: Number of bytes of space available on each sibling.
3993 **
drh8b2f49b2001-06-08 00:21:52 +00003994 */
drh43605152004-05-29 21:46:49 +00003995 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00003996 for(subtotal=k=i=0; i<nCell; i++){
drh43605152004-05-29 21:46:49 +00003997 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00003998 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00003999 szNew[k] = subtotal - szCell[i];
4000 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00004001 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00004002 subtotal = 0;
4003 k++;
4004 }
4005 }
4006 szNew[k] = subtotal;
4007 cntNew[k] = nCell;
4008 k++;
drh96f5b762004-05-16 16:24:36 +00004009
4010 /*
4011 ** The packing computed by the previous block is biased toward the siblings
4012 ** on the left side. The left siblings are always nearly full, while the
4013 ** right-most sibling might be nearly empty. This block of code attempts
4014 ** to adjust the packing of siblings to get a better balance.
4015 **
4016 ** This adjustment is more than an optimization. The packing above might
4017 ** be so out of balance as to be illegal. For example, the right-most
4018 ** sibling might be completely empty. This adjustment is not optional.
4019 */
drh6019e162001-07-02 17:51:45 +00004020 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00004021 int szRight = szNew[i]; /* Size of sibling on the right */
4022 int szLeft = szNew[i-1]; /* Size of sibling on the left */
4023 int r; /* Index of right-most cell in left sibling */
4024 int d; /* Index of first cell to the left of right sibling */
4025
4026 r = cntNew[i-1] - 1;
4027 d = r + 1 - leafData;
drh43605152004-05-29 21:46:49 +00004028 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
4029 szRight += szCell[d] + 2;
4030 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00004031 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00004032 r = cntNew[i-1] - 1;
4033 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00004034 }
drh96f5b762004-05-16 16:24:36 +00004035 szNew[i] = szRight;
4036 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00004037 }
4038 assert( cntNew[0]>0 );
drh8b2f49b2001-06-08 00:21:52 +00004039
4040 /*
drh6b308672002-07-08 02:16:37 +00004041 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00004042 */
drh4b70f112004-05-02 21:12:19 +00004043 assert( pPage->pgno>1 );
4044 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00004045 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00004046 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00004047 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00004048 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00004049 pgnoNew[i] = pgnoOld[i];
4050 apOld[i] = 0;
danielk197728129562005-01-11 10:25:06 +00004051 rc = sqlite3pager_write(pNew->aData);
4052 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00004053 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004054 rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00004055 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00004056 apNew[i] = pNew;
drh6b308672002-07-08 02:16:37 +00004057 }
drh14acc042001-06-10 19:56:58 +00004058 nNew++;
drhda200cc2004-05-09 11:51:38 +00004059 zeroPage(pNew, pageFlags);
drh8b2f49b2001-06-08 00:21:52 +00004060 }
4061
danielk1977299b1872004-11-22 10:02:10 +00004062 /* Free any old pages that were not reused as new pages.
4063 */
4064 while( i<nOld ){
4065 rc = freePage(apOld[i]);
4066 if( rc ) goto balance_cleanup;
4067 releasePage(apOld[i]);
4068 apOld[i] = 0;
4069 i++;
4070 }
4071
drh8b2f49b2001-06-08 00:21:52 +00004072 /*
drhf9ffac92002-03-02 19:00:31 +00004073 ** Put the new pages in accending order. This helps to
4074 ** keep entries in the disk file in order so that a scan
4075 ** of the table is a linear scan through the file. That
4076 ** in turn helps the operating system to deliver pages
4077 ** from the disk more rapidly.
4078 **
4079 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00004080 ** n is never more than NB (a small constant), that should
4081 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00004082 **
drhc3b70572003-01-04 19:44:07 +00004083 ** When NB==3, this one optimization makes the database
4084 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00004085 */
4086 for(i=0; i<k-1; i++){
4087 int minV = pgnoNew[i];
4088 int minI = i;
4089 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00004090 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00004091 minI = j;
4092 minV = pgnoNew[j];
4093 }
4094 }
4095 if( minI>i ){
4096 int t;
4097 MemPage *pT;
4098 t = pgnoNew[i];
4099 pT = apNew[i];
4100 pgnoNew[i] = pgnoNew[minI];
4101 apNew[i] = apNew[minI];
4102 pgnoNew[minI] = t;
4103 apNew[minI] = pT;
4104 }
4105 }
drha2fce642004-06-05 00:01:44 +00004106 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00004107 pgnoOld[0],
4108 nOld>=2 ? pgnoOld[1] : 0,
4109 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00004110 pgnoNew[0], szNew[0],
4111 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
4112 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00004113 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
4114 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00004115
drhf9ffac92002-03-02 19:00:31 +00004116 /*
drh14acc042001-06-10 19:56:58 +00004117 ** Evenly distribute the data in apCell[] across the new pages.
4118 ** Insert divider cells into pParent as necessary.
4119 */
4120 j = 0;
4121 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00004122 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00004123 MemPage *pNew = apNew[i];
drh4b70f112004-05-02 21:12:19 +00004124 assert( pNew->pgno==pgnoNew[i] );
drhfa1a98a2004-05-14 19:08:17 +00004125 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh6019e162001-07-02 17:51:45 +00004126 assert( pNew->nCell>0 );
drh43605152004-05-29 21:46:49 +00004127 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00004128
4129#ifndef SQLITE_OMIT_AUTOVACUUM
4130 /* If this is an auto-vacuum database, update the pointer map entries
4131 ** that point to the siblings that were rearranged. These can be: left
4132 ** children of cells, the right-child of the page, or overflow pages
4133 ** pointed to by cells.
4134 */
4135 if( pBt->autoVacuum ){
4136 for(k=j; k<cntNew[i]; k++){
4137 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00004138 rc = ptrmapPutOvfl(pNew, k-j);
4139 if( rc!=SQLITE_OK ){
4140 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00004141 }
4142 }
4143 }
4144 }
4145#endif
4146
4147 j = cntNew[i];
4148
4149 /* If the sibling page assembled above was not the right-most sibling,
4150 ** insert a divider cell into the parent page.
4151 */
drh14acc042001-06-10 19:56:58 +00004152 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00004153 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00004154 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00004155 int sz;
4156 pCell = apCell[j];
4157 sz = szCell[j] + leafCorrection;
drh4b70f112004-05-02 21:12:19 +00004158 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00004159 memcpy(&pNew->aData[8], pCell, 4);
drh24cd67e2004-05-10 16:18:47 +00004160 pTemp = 0;
drh8b18dd42004-05-12 19:18:15 +00004161 }else if( leafData ){
danielk1977ac11ee62005-01-15 12:45:51 +00004162 /* If the tree is a leaf-data tree, and the siblings are leaves,
4163 ** then there is no divider cell in apCell[]. Instead, the divider
4164 ** cell consists of the integer key for the right-most cell of
4165 ** the sibling-page assembled above only.
4166 */
drh6f11bef2004-05-13 01:12:56 +00004167 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00004168 j--;
drh43605152004-05-29 21:46:49 +00004169 parseCellPtr(pNew, apCell[j], &info);
drhb6f41482004-05-14 01:58:11 +00004170 pCell = &aSpace[iSpace];
drh6f11bef2004-05-13 01:12:56 +00004171 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
drhb6f41482004-05-14 01:58:11 +00004172 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00004173 assert( iSpace<=pBt->psAligned*5 );
drh8b18dd42004-05-12 19:18:15 +00004174 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00004175 }else{
4176 pCell -= 4;
drhb6f41482004-05-14 01:58:11 +00004177 pTemp = &aSpace[iSpace];
4178 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00004179 assert( iSpace<=pBt->psAligned*5 );
drh4b70f112004-05-02 21:12:19 +00004180 }
danielk1977a3ad5e72005-01-07 08:56:44 +00004181 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00004182 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh43605152004-05-29 21:46:49 +00004183 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +00004184#ifndef SQLITE_OMIT_AUTOVACUUM
4185 /* If this is an auto-vacuum database, and not a leaf-data tree,
4186 ** then update the pointer map with an entry for the overflow page
4187 ** that the cell just inserted points to (if any).
4188 */
4189 if( pBt->autoVacuum && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00004190 rc = ptrmapPutOvfl(pParent, nxDiv);
4191 if( rc!=SQLITE_OK ){
4192 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00004193 }
4194 }
4195#endif
drh14acc042001-06-10 19:56:58 +00004196 j++;
4197 nxDiv++;
4198 }
4199 }
drh6019e162001-07-02 17:51:45 +00004200 assert( j==nCell );
drh4b70f112004-05-02 21:12:19 +00004201 if( (pageFlags & PTF_LEAF)==0 ){
drh43605152004-05-29 21:46:49 +00004202 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
drh14acc042001-06-10 19:56:58 +00004203 }
drh43605152004-05-29 21:46:49 +00004204 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00004205 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00004206 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00004207 }else{
4208 /* Right-most sibling is the left child of the first entry in pParent
4209 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00004210 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00004211 }
4212
4213 /*
4214 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00004215 */
4216 for(i=0; i<nNew; i++){
danielk1977afcdd022004-10-31 16:25:42 +00004217 rc = reparentChildPages(apNew[i]);
4218 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00004219 }
danielk1977afcdd022004-10-31 16:25:42 +00004220 rc = reparentChildPages(pParent);
4221 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00004222
4223 /*
drh3a4c1412004-05-09 20:40:11 +00004224 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00004225 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00004226 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00004227 */
drhda200cc2004-05-09 11:51:38 +00004228 assert( pParent->isInit );
drh3a4c1412004-05-09 20:40:11 +00004229 /* assert( pPage->isInit ); // No! pPage might have been added to freelist */
4230 /* pageIntegrity(pPage); // No! pPage might have been added to freelist */
danielk1977ac245ec2005-01-14 13:50:11 +00004231 rc = balance(pParent, 0);
drhda200cc2004-05-09 11:51:38 +00004232
drh8b2f49b2001-06-08 00:21:52 +00004233 /*
drh14acc042001-06-10 19:56:58 +00004234 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00004235 */
drh14acc042001-06-10 19:56:58 +00004236balance_cleanup:
drh2e38c322004-09-03 18:38:44 +00004237 sqliteFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00004238 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00004239 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00004240 }
drh14acc042001-06-10 19:56:58 +00004241 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00004242 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00004243 }
drh91025292004-05-03 19:49:32 +00004244 releasePage(pParent);
drh3a4c1412004-05-09 20:40:11 +00004245 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
4246 pPage->pgno, nOld, nNew, nCell));
drh8b2f49b2001-06-08 00:21:52 +00004247 return rc;
4248}
4249
4250/*
drh43605152004-05-29 21:46:49 +00004251** This routine is called for the root page of a btree when the root
4252** page contains no cells. This is an opportunity to make the tree
4253** shallower by one level.
4254*/
4255static int balance_shallower(MemPage *pPage){
4256 MemPage *pChild; /* The only child page of pPage */
4257 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00004258 int rc = SQLITE_OK; /* Return code from subprocedures */
4259 Btree *pBt; /* The main BTree structure */
4260 int mxCellPerPage; /* Maximum number of cells per page */
4261 u8 **apCell; /* All cells from pages being balanced */
4262 int *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00004263
4264 assert( pPage->pParent==0 );
4265 assert( pPage->nCell==0 );
drh2e38c322004-09-03 18:38:44 +00004266 pBt = pPage->pBt;
4267 mxCellPerPage = MX_CELL(pBt);
4268 apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
4269 if( apCell==0 ) return SQLITE_NOMEM;
4270 szCell = (int*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00004271 if( pPage->leaf ){
4272 /* The table is completely empty */
4273 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
4274 }else{
4275 /* The root page is empty but has one child. Transfer the
4276 ** information from that one child into the root page if it
4277 ** will fit. This reduces the depth of the tree by one.
4278 **
4279 ** If the root page is page 1, it has less space available than
4280 ** its child (due to the 100 byte header that occurs at the beginning
4281 ** of the database fle), so it might not be able to hold all of the
4282 ** information currently contained in the child. If this is the
4283 ** case, then do not do the transfer. Leave page 1 empty except
4284 ** for the right-pointer to the child page. The child page becomes
4285 ** the virtual root of the tree.
4286 */
4287 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
4288 assert( pgnoChild>0 );
4289 assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) );
4290 rc = getPage(pPage->pBt, pgnoChild, &pChild);
drh2e38c322004-09-03 18:38:44 +00004291 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004292 if( pPage->pgno==1 ){
4293 rc = initPage(pChild, pPage);
drh2e38c322004-09-03 18:38:44 +00004294 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004295 assert( pChild->nOverflow==0 );
4296 if( pChild->nFree>=100 ){
4297 /* The child information will fit on the root page, so do the
4298 ** copy */
4299 int i;
4300 zeroPage(pPage, pChild->aData[0]);
4301 for(i=0; i<pChild->nCell; i++){
4302 apCell[i] = findCell(pChild,i);
4303 szCell[i] = cellSizePtr(pChild, apCell[i]);
4304 }
4305 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00004306 /* Copy the right-pointer of the child to the parent. */
4307 put4byte(&pPage->aData[pPage->hdrOffset+8],
4308 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh43605152004-05-29 21:46:49 +00004309 freePage(pChild);
4310 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
4311 }else{
4312 /* The child has more information that will fit on the root.
4313 ** The tree is already balanced. Do nothing. */
4314 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
4315 }
4316 }else{
4317 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
4318 pPage->isInit = 0;
4319 pPage->pParent = 0;
4320 rc = initPage(pPage, 0);
4321 assert( rc==SQLITE_OK );
4322 freePage(pChild);
4323 TRACE(("BALANCE: transfer child %d into root %d\n",
4324 pChild->pgno, pPage->pgno));
4325 }
danielk1977afcdd022004-10-31 16:25:42 +00004326 rc = reparentChildPages(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00004327 assert( pPage->nOverflow==0 );
4328#ifndef SQLITE_OMIT_AUTOVACUUM
4329 if( pBt->autoVacuum ){
danielk1977aac0a382005-01-16 11:07:06 +00004330 int i;
danielk1977ac11ee62005-01-15 12:45:51 +00004331 for(i=0; i<pPage->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00004332 rc = ptrmapPutOvfl(pPage, i);
4333 if( rc!=SQLITE_OK ){
4334 goto end_shallow_balance;
danielk1977ac11ee62005-01-15 12:45:51 +00004335 }
4336 }
4337 }
4338#endif
danielk1977afcdd022004-10-31 16:25:42 +00004339 if( rc!=SQLITE_OK ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004340 releasePage(pChild);
4341 }
drh2e38c322004-09-03 18:38:44 +00004342end_shallow_balance:
4343 sqliteFree(apCell);
4344 return rc;
drh43605152004-05-29 21:46:49 +00004345}
4346
4347
4348/*
4349** The root page is overfull
4350**
4351** When this happens, Create a new child page and copy the
4352** contents of the root into the child. Then make the root
4353** page an empty page with rightChild pointing to the new
4354** child. Finally, call balance_internal() on the new child
4355** to cause it to split.
4356*/
4357static int balance_deeper(MemPage *pPage){
4358 int rc; /* Return value from subprocedures */
4359 MemPage *pChild; /* Pointer to a new child page */
4360 Pgno pgnoChild; /* Page number of the new child page */
4361 Btree *pBt; /* The BTree */
4362 int usableSize; /* Total usable size of a page */
4363 u8 *data; /* Content of the parent page */
4364 u8 *cdata; /* Content of the child page */
4365 int hdr; /* Offset to page header in parent */
4366 int brk; /* Offset to content of first cell in parent */
4367
4368 assert( pPage->pParent==0 );
4369 assert( pPage->nOverflow>0 );
4370 pBt = pPage->pBt;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004371 rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00004372 if( rc ) return rc;
4373 assert( sqlite3pager_iswriteable(pChild->aData) );
4374 usableSize = pBt->usableSize;
4375 data = pPage->aData;
4376 hdr = pPage->hdrOffset;
4377 brk = get2byte(&data[hdr+5]);
4378 cdata = pChild->aData;
4379 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
4380 memcpy(&cdata[brk], &data[brk], usableSize-brk);
danielk1977c7dc7532004-11-17 10:22:03 +00004381 assert( pChild->isInit==0 );
drh43605152004-05-29 21:46:49 +00004382 rc = initPage(pChild, pPage);
4383 if( rc ) return rc;
4384 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
4385 pChild->nOverflow = pPage->nOverflow;
4386 if( pChild->nOverflow ){
4387 pChild->nFree = 0;
4388 }
4389 assert( pChild->nCell==pPage->nCell );
4390 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
4391 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
4392 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
danielk19774e17d142005-01-16 09:06:33 +00004393#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00004394 if( pBt->autoVacuum ){
4395 int i;
4396 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
4397 if( rc ) return rc;
4398 for(i=0; i<pChild->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00004399 rc = ptrmapPutOvfl(pChild, i);
4400 if( rc!=SQLITE_OK ){
4401 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00004402 }
4403 }
4404 }
danielk19774e17d142005-01-16 09:06:33 +00004405#endif
drh43605152004-05-29 21:46:49 +00004406 rc = balance_nonroot(pChild);
4407 releasePage(pChild);
4408 return rc;
4409}
4410
4411/*
4412** Decide if the page pPage needs to be balanced. If balancing is
4413** required, call the appropriate balancing routine.
4414*/
danielk1977ac245ec2005-01-14 13:50:11 +00004415static int balance(MemPage *pPage, int insert){
drh43605152004-05-29 21:46:49 +00004416 int rc = SQLITE_OK;
4417 if( pPage->pParent==0 ){
4418 if( pPage->nOverflow>0 ){
4419 rc = balance_deeper(pPage);
4420 }
danielk1977687566d2004-11-02 12:56:41 +00004421 if( rc==SQLITE_OK && pPage->nCell==0 ){
drh43605152004-05-29 21:46:49 +00004422 rc = balance_shallower(pPage);
4423 }
4424 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00004425 if( pPage->nOverflow>0 ||
4426 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
drh43605152004-05-29 21:46:49 +00004427 rc = balance_nonroot(pPage);
4428 }
4429 }
4430 return rc;
4431}
4432
4433/*
drh8dcd7ca2004-08-08 19:43:29 +00004434** This routine checks all cursors that point to table pgnoRoot.
4435** If any of those cursors other than pExclude were opened with
drhf74b8d92002-09-01 23:20:45 +00004436** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
drh8dcd7ca2004-08-08 19:43:29 +00004437** cursors that point to pgnoRoot were opened with wrFlag==1
drhf74b8d92002-09-01 23:20:45 +00004438** then this routine returns SQLITE_OK.
danielk1977299b1872004-11-22 10:02:10 +00004439**
4440** In addition to checking for read-locks (where a read-lock
4441** means a cursor opened with wrFlag==0) this routine also moves
4442** all cursors other than pExclude so that they are pointing to the
4443** first Cell on root page. This is necessary because an insert
4444** or delete might change the number of cells on a page or delete
4445** a page entirely and we do not want to leave any cursors
4446** pointing to non-existant pages or cells.
drhf74b8d92002-09-01 23:20:45 +00004447*/
drh8dcd7ca2004-08-08 19:43:29 +00004448static int checkReadLocks(Btree *pBt, Pgno pgnoRoot, BtCursor *pExclude){
danielk1977299b1872004-11-22 10:02:10 +00004449 BtCursor *p;
4450 for(p=pBt->pCursor; p; p=p->pNext){
4451 if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue;
4452 if( p->wrFlag==0 ) return SQLITE_LOCKED;
4453 if( p->pPage->pgno!=p->pgnoRoot ){
4454 moveToRoot(p);
4455 }
4456 }
drhf74b8d92002-09-01 23:20:45 +00004457 return SQLITE_OK;
4458}
4459
4460/*
drh3b7511c2001-05-26 13:15:44 +00004461** Insert a new record into the BTree. The key is given by (pKey,nKey)
4462** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00004463** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00004464** is left pointing at a random location.
4465**
4466** For an INTKEY table, only the nKey value of the key is used. pKey is
4467** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00004468*/
drh3aac2dd2004-04-26 14:10:20 +00004469int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00004470 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00004471 const void *pKey, i64 nKey, /* The key of the new record */
drh5c4d9702001-08-20 00:33:58 +00004472 const void *pData, int nData /* The data of the new record */
drh3b7511c2001-05-26 13:15:44 +00004473){
drh3b7511c2001-05-26 13:15:44 +00004474 int rc;
4475 int loc;
drh14acc042001-06-10 19:56:58 +00004476 int szNew;
drh3b7511c2001-05-26 13:15:44 +00004477 MemPage *pPage;
4478 Btree *pBt = pCur->pBt;
drha34b6762004-05-07 13:30:42 +00004479 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00004480 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00004481
danielk1977ee5741e2004-05-31 10:01:34 +00004482 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004483 /* Must start a transaction before doing an insert */
4484 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004485 }
drhf74b8d92002-09-01 23:20:45 +00004486 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00004487 if( !pCur->wrFlag ){
4488 return SQLITE_PERM; /* Cursor not open for writing */
4489 }
drh8dcd7ca2004-08-08 19:43:29 +00004490 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00004491 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
4492 }
drh3aac2dd2004-04-26 14:10:20 +00004493 rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc);
drh3b7511c2001-05-26 13:15:44 +00004494 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00004495 pPage = pCur->pPage;
drh4a1c3802004-05-12 15:15:47 +00004496 assert( pPage->intKey || nKey>=0 );
drh8b18dd42004-05-12 19:18:15 +00004497 assert( pPage->leaf || !pPage->leafData );
drh3a4c1412004-05-09 20:40:11 +00004498 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
4499 pCur->pgnoRoot, nKey, nData, pPage->pgno,
4500 loc==0 ? "overwrite" : "new entry"));
drh7aa128d2002-06-21 13:09:16 +00004501 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00004502 rc = sqlite3pager_write(pPage->aData);
drhbd03cae2001-06-02 02:40:57 +00004503 if( rc ) return rc;
drh2e38c322004-09-03 18:38:44 +00004504 newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
4505 if( newCell==0 ) return SQLITE_NOMEM;
drha34b6762004-05-07 13:30:42 +00004506 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew);
drh2e38c322004-09-03 18:38:44 +00004507 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00004508 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00004509 assert( szNew<=MX_CELL_SIZE(pBt) );
drhf328bc82004-05-10 23:29:49 +00004510 if( loc==0 && pCur->isValid ){
drha34b6762004-05-07 13:30:42 +00004511 int szOld;
4512 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004513 oldCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00004514 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004515 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00004516 }
drh43605152004-05-29 21:46:49 +00004517 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00004518 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00004519 if( rc ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00004520 dropCell(pPage, pCur->idx, szOld);
drh7c717f72001-06-24 20:39:41 +00004521 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00004522 assert( pPage->leaf );
drh14acc042001-06-10 19:56:58 +00004523 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00004524 pCur->info.nSize = 0;
drh14acc042001-06-10 19:56:58 +00004525 }else{
drh4b70f112004-05-02 21:12:19 +00004526 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00004527 }
danielk1977a3ad5e72005-01-07 08:56:44 +00004528 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
danielk1977e80463b2004-11-03 03:01:16 +00004529 if( rc!=SQLITE_OK ) goto end_insert;
danielk1977ac245ec2005-01-14 13:50:11 +00004530 rc = balance(pPage, 1);
drh23e11ca2004-05-04 17:27:28 +00004531 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
drh3fc190c2001-09-14 03:24:23 +00004532 /* fflush(stdout); */
danielk1977299b1872004-11-22 10:02:10 +00004533 if( rc==SQLITE_OK ){
4534 moveToRoot(pCur);
4535 }
drh2e38c322004-09-03 18:38:44 +00004536end_insert:
4537 sqliteFree(newCell);
drh5e2f8b92001-05-28 00:41:15 +00004538 return rc;
4539}
4540
4541/*
drh4b70f112004-05-02 21:12:19 +00004542** Delete the entry that the cursor is pointing to. The cursor
4543** is left pointing at a random location.
drh3b7511c2001-05-26 13:15:44 +00004544*/
drh3aac2dd2004-04-26 14:10:20 +00004545int sqlite3BtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00004546 MemPage *pPage = pCur->pPage;
drh4b70f112004-05-02 21:12:19 +00004547 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00004548 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00004549 Pgno pgnoChild = 0;
drh0d316a42002-08-11 20:10:47 +00004550 Btree *pBt = pCur->pBt;
drh8b2f49b2001-06-08 00:21:52 +00004551
drh7aa128d2002-06-21 13:09:16 +00004552 assert( pPage->isInit );
danielk1977ee5741e2004-05-31 10:01:34 +00004553 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004554 /* Must start a transaction before doing a delete */
4555 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004556 }
drhf74b8d92002-09-01 23:20:45 +00004557 assert( !pBt->readOnly );
drhbd03cae2001-06-02 02:40:57 +00004558 if( pCur->idx >= pPage->nCell ){
4559 return SQLITE_ERROR; /* The cursor is not pointing to anything */
4560 }
drhecdc7532001-09-23 02:35:53 +00004561 if( !pCur->wrFlag ){
4562 return SQLITE_PERM; /* Did not open this cursor for writing */
4563 }
drh8dcd7ca2004-08-08 19:43:29 +00004564 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00004565 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
4566 }
drha34b6762004-05-07 13:30:42 +00004567 rc = sqlite3pager_write(pPage->aData);
drhbd03cae2001-06-02 02:40:57 +00004568 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:10 +00004569
4570 /* Locate the cell within it's page and leave pCell pointing to the
4571 ** data. The clearCell() call frees any overflow pages associated with the
4572 ** cell. The cell itself is still intact.
4573 */
danielk1977299b1872004-11-22 10:02:10 +00004574 pCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00004575 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004576 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00004577 }
danielk197728129562005-01-11 10:25:06 +00004578 rc = clearCell(pPage, pCell);
4579 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:10 +00004580
drh4b70f112004-05-02 21:12:19 +00004581 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00004582 /*
drh5e00f6c2001-09-13 13:46:56 +00004583 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00004584 ** do something we will leave a hole on an internal page.
4585 ** We have to fill the hole by moving in a cell from a leaf. The
4586 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00004587 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00004588 */
drh14acc042001-06-10 19:56:58 +00004589 BtCursor leafCur;
drh4b70f112004-05-02 21:12:19 +00004590 unsigned char *pNext;
drh14acc042001-06-10 19:56:58 +00004591 int szNext;
danielk1977299b1872004-11-22 10:02:10 +00004592 int notUsed;
drh2e38c322004-09-03 18:38:44 +00004593 unsigned char *tempCell;
drh8b18dd42004-05-12 19:18:15 +00004594 assert( !pPage->leafData );
drh14acc042001-06-10 19:56:58 +00004595 getTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00004596 rc = sqlite3BtreeNext(&leafCur, &notUsed);
drh14acc042001-06-10 19:56:58 +00004597 if( rc!=SQLITE_OK ){
drhee696e22004-08-30 16:52:17 +00004598 if( rc!=SQLITE_NOMEM ){
4599 rc = SQLITE_CORRUPT; /* bkpt-CORRUPT */
4600 }
danielk1977299b1872004-11-22 10:02:10 +00004601 return rc;
drh5e2f8b92001-05-28 00:41:15 +00004602 }
drha34b6762004-05-07 13:30:42 +00004603 rc = sqlite3pager_write(leafCur.pPage->aData);
danielk1977299b1872004-11-22 10:02:10 +00004604 if( rc ) return rc;
4605 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
4606 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
4607 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
drh43605152004-05-29 21:46:49 +00004608 pNext = findCell(leafCur.pPage, leafCur.idx);
4609 szNext = cellSizePtr(leafCur.pPage, pNext);
drh2e38c322004-09-03 18:38:44 +00004610 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
4611 tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
danielk1977299b1872004-11-22 10:02:10 +00004612 if( tempCell==0 ) return SQLITE_NOMEM;
danielk1977a3ad5e72005-01-07 08:56:44 +00004613 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
danielk1977299b1872004-11-22 10:02:10 +00004614 if( rc!=SQLITE_OK ) return rc;
4615 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
danielk1977ac245ec2005-01-14 13:50:11 +00004616 rc = balance(pPage, 0);
drh2e38c322004-09-03 18:38:44 +00004617 sqliteFree(tempCell);
danielk1977299b1872004-11-22 10:02:10 +00004618 if( rc ) return rc;
drh4b70f112004-05-02 21:12:19 +00004619 dropCell(leafCur.pPage, leafCur.idx, szNext);
danielk1977ac245ec2005-01-14 13:50:11 +00004620 rc = balance(leafCur.pPage, 0);
drh8c42ca92001-06-22 19:15:00 +00004621 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00004622 }else{
danielk1977299b1872004-11-22 10:02:10 +00004623 TRACE(("DELETE: table=%d delete from leaf %d\n",
4624 pCur->pgnoRoot, pPage->pgno));
4625 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
danielk1977ac245ec2005-01-14 13:50:11 +00004626 rc = balance(pPage, 0);
drh5e2f8b92001-05-28 00:41:15 +00004627 }
danielk1977299b1872004-11-22 10:02:10 +00004628 moveToRoot(pCur);
drh5e2f8b92001-05-28 00:41:15 +00004629 return rc;
drh3b7511c2001-05-26 13:15:44 +00004630}
drh8b2f49b2001-06-08 00:21:52 +00004631
4632/*
drhc6b52df2002-01-04 03:09:29 +00004633** Create a new BTree table. Write into *piTable the page
4634** number for the root page of the new table.
4635**
drhab01f612004-05-22 02:55:23 +00004636** The type of type is determined by the flags parameter. Only the
4637** following values of flags are currently in use. Other values for
4638** flags might not work:
4639**
4640** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
4641** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00004642*/
drh3aac2dd2004-04-26 14:10:20 +00004643int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){
drh8b2f49b2001-06-08 00:21:52 +00004644 MemPage *pRoot;
4645 Pgno pgnoRoot;
4646 int rc;
danielk1977ee5741e2004-05-31 10:01:34 +00004647 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004648 /* Must start a transaction first */
4649 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004650 }
danielk197728129562005-01-11 10:25:06 +00004651 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00004652
4653 /* It is illegal to create a table if any cursors are open on the
4654 ** database. This is because in auto-vacuum mode the backend may
4655 ** need to move a database page to make room for the new root-page.
4656 ** If an open cursor was using the page a problem would occur.
4657 */
4658 if( pBt->pCursor ){
4659 return SQLITE_LOCKED;
4660 }
4661
danielk1977003ba062004-11-04 02:57:33 +00004662#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977cb1a7eb2004-11-05 12:27:02 +00004663 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drh8b2f49b2001-06-08 00:21:52 +00004664 if( rc ) return rc;
danielk1977003ba062004-11-04 02:57:33 +00004665#else
danielk1977687566d2004-11-02 12:56:41 +00004666 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00004667 Pgno pgnoMove; /* Move a page here to make room for the root-page */
4668 MemPage *pPageMove; /* The page to move to. */
4669
danielk1977003ba062004-11-04 02:57:33 +00004670 /* Read the value of meta[3] from the database to determine where the
4671 ** root page of the new table should go. meta[3] is the largest root-page
4672 ** created so far, so the new root-page is (meta[3]+1).
4673 */
4674 rc = sqlite3BtreeGetMeta(pBt, 4, &pgnoRoot);
4675 if( rc!=SQLITE_OK ) return rc;
4676 pgnoRoot++;
4677
danielk1977599fcba2004-11-08 07:13:13 +00004678 /* The new root-page may not be allocated on a pointer-map page, or the
4679 ** PENDING_BYTE page.
4680 */
drh42cac6d2004-11-20 20:31:11 +00004681 if( pgnoRoot==PTRMAP_PAGENO(pBt->usableSize, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00004682 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00004683 pgnoRoot++;
4684 }
4685 assert( pgnoRoot>=3 );
4686
4687 /* Allocate a page. The page that currently resides at pgnoRoot will
4688 ** be moved to the allocated page (unless the allocated page happens
4689 ** to reside at pgnoRoot).
4690 */
danielk1977cb1a7eb2004-11-05 12:27:02 +00004691 rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00004692 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00004693 return rc;
4694 }
danielk1977003ba062004-11-04 02:57:33 +00004695
4696 if( pgnoMove!=pgnoRoot ){
4697 u8 eType;
4698 Pgno iPtrPage;
4699
4700 releasePage(pPageMove);
4701 rc = getPage(pBt, pgnoRoot, &pRoot);
4702 if( rc!=SQLITE_OK ){
4703 return rc;
4704 }
4705 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
danielk1977a0bf2652004-11-04 14:30:04 +00004706 assert( eType!=PTRMAP_ROOTPAGE );
danielk1977a64a0352004-11-05 01:45:13 +00004707 assert( eType!=PTRMAP_FREEPAGE );
danielk1977003ba062004-11-04 02:57:33 +00004708 if( rc!=SQLITE_OK ){
4709 releasePage(pRoot);
4710 return rc;
4711 }
4712 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
4713 releasePage(pRoot);
4714 if( rc!=SQLITE_OK ){
4715 return rc;
4716 }
4717 rc = getPage(pBt, pgnoRoot, &pRoot);
4718 if( rc!=SQLITE_OK ){
4719 return rc;
4720 }
4721 rc = sqlite3pager_write(pRoot->aData);
4722 if( rc!=SQLITE_OK ){
4723 releasePage(pRoot);
4724 return rc;
4725 }
4726 }else{
4727 pRoot = pPageMove;
4728 }
4729
danielk197742741be2005-01-08 12:42:39 +00004730 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00004731 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
4732 if( rc ){
4733 releasePage(pRoot);
4734 return rc;
4735 }
4736 rc = sqlite3BtreeUpdateMeta(pBt, 4, pgnoRoot);
4737 if( rc ){
4738 releasePage(pRoot);
4739 return rc;
4740 }
danielk197742741be2005-01-08 12:42:39 +00004741
danielk1977003ba062004-11-04 02:57:33 +00004742 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004743 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00004744 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00004745 }
4746#endif
drha34b6762004-05-07 13:30:42 +00004747 assert( sqlite3pager_iswriteable(pRoot->aData) );
drhde647132004-05-07 17:57:49 +00004748 zeroPage(pRoot, flags | PTF_LEAF);
drha34b6762004-05-07 13:30:42 +00004749 sqlite3pager_unref(pRoot->aData);
drh8b2f49b2001-06-08 00:21:52 +00004750 *piTable = (int)pgnoRoot;
4751 return SQLITE_OK;
4752}
4753
4754/*
4755** Erase the given database page and all its children. Return
4756** the page to the freelist.
4757*/
drh4b70f112004-05-02 21:12:19 +00004758static int clearDatabasePage(
4759 Btree *pBt, /* The BTree that contains the table */
4760 Pgno pgno, /* Page number to clear */
4761 MemPage *pParent, /* Parent page. NULL for the root */
4762 int freePageFlag /* Deallocate page if true */
4763){
drh8b2f49b2001-06-08 00:21:52 +00004764 MemPage *pPage;
4765 int rc;
drh4b70f112004-05-02 21:12:19 +00004766 unsigned char *pCell;
4767 int i;
drh8b2f49b2001-06-08 00:21:52 +00004768
drhde647132004-05-07 17:57:49 +00004769 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
drh8b2f49b2001-06-08 00:21:52 +00004770 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00004771 rc = sqlite3pager_write(pPage->aData);
drh6019e162001-07-02 17:51:45 +00004772 if( rc ) return rc;
drh4b70f112004-05-02 21:12:19 +00004773 for(i=0; i<pPage->nCell; i++){
drh43605152004-05-29 21:46:49 +00004774 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00004775 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004776 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
drh8b2f49b2001-06-08 00:21:52 +00004777 if( rc ) return rc;
4778 }
drh4b70f112004-05-02 21:12:19 +00004779 rc = clearCell(pPage, pCell);
drh8b2f49b2001-06-08 00:21:52 +00004780 if( rc ) return rc;
4781 }
drha34b6762004-05-07 13:30:42 +00004782 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004783 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
drh2aa679f2001-06-25 02:11:07 +00004784 if( rc ) return rc;
4785 }
4786 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00004787 rc = freePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00004788 }else{
drh3a4c1412004-05-09 20:40:11 +00004789 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00004790 }
drh4b70f112004-05-02 21:12:19 +00004791 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00004792 return rc;
drh8b2f49b2001-06-08 00:21:52 +00004793}
4794
4795/*
drhab01f612004-05-22 02:55:23 +00004796** Delete all information from a single table in the database. iTable is
4797** the page number of the root of the table. After this routine returns,
4798** the root page is empty, but still exists.
4799**
4800** This routine will fail with SQLITE_LOCKED if there are any open
4801** read cursors on the table. Open write cursors are moved to the
4802** root of the table.
drh8b2f49b2001-06-08 00:21:52 +00004803*/
drh3aac2dd2004-04-26 14:10:20 +00004804int sqlite3BtreeClearTable(Btree *pBt, int iTable){
drh8b2f49b2001-06-08 00:21:52 +00004805 int rc;
drhf74b8d92002-09-01 23:20:45 +00004806 BtCursor *pCur;
danielk1977ee5741e2004-05-31 10:01:34 +00004807 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004808 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004809 }
drhf74b8d92002-09-01 23:20:45 +00004810 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
4811 if( pCur->pgnoRoot==(Pgno)iTable ){
4812 if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
4813 moveToRoot(pCur);
4814 }
drhecdc7532001-09-23 02:35:53 +00004815 }
drha34b6762004-05-07 13:30:42 +00004816 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
drh8b2f49b2001-06-08 00:21:52 +00004817 if( rc ){
drh3aac2dd2004-04-26 14:10:20 +00004818 sqlite3BtreeRollback(pBt);
drh8b2f49b2001-06-08 00:21:52 +00004819 }
drh8c42ca92001-06-22 19:15:00 +00004820 return rc;
drh8b2f49b2001-06-08 00:21:52 +00004821}
4822
4823/*
4824** Erase all information in a table and add the root of the table to
4825** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00004826** page 1) is never added to the freelist.
4827**
4828** This routine will fail with SQLITE_LOCKED if there are any open
4829** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00004830**
4831** If AUTOVACUUM is enabled and the page at iTable is not the last
4832** root page in the database file, then the last root page
4833** in the database file is moved into the slot formerly occupied by
4834** iTable and that last slot formerly occupied by the last root page
4835** is added to the freelist instead of iTable. In this say, all
4836** root pages are kept at the beginning of the database file, which
4837** is necessary for AUTOVACUUM to work right. *piMoved is set to the
4838** page number that used to be the last root page in the file before
4839** the move. If no page gets moved, *piMoved is set to 0.
4840** The last root page is recorded in meta[3] and the value of
4841** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00004842*/
danielk1977a0bf2652004-11-04 14:30:04 +00004843int sqlite3BtreeDropTable(Btree *pBt, int iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00004844 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00004845 MemPage *pPage = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00004846
danielk1977ee5741e2004-05-31 10:01:34 +00004847 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004848 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004849 }
danielk1977a0bf2652004-11-04 14:30:04 +00004850
danielk1977e6efa742004-11-10 11:55:10 +00004851 /* It is illegal to drop a table if any cursors are open on the
4852 ** database. This is because in auto-vacuum mode the backend may
4853 ** need to move another root-page to fill a gap left by the deleted
4854 ** root page. If an open cursor was using this page a problem would
4855 ** occur.
4856 */
4857 if( pBt->pCursor ){
4858 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00004859 }
danielk1977a0bf2652004-11-04 14:30:04 +00004860
drha34b6762004-05-07 13:30:42 +00004861 rc = getPage(pBt, (Pgno)iTable, &pPage);
drh2aa679f2001-06-25 02:11:07 +00004862 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004863 rc = sqlite3BtreeClearTable(pBt, iTable);
drh2aa679f2001-06-25 02:11:07 +00004864 if( rc ) return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00004865
drh205f48e2004-11-05 00:43:11 +00004866 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00004867
drh4b70f112004-05-02 21:12:19 +00004868 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004869#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00004870 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00004871 releasePage(pPage);
4872#else
4873 if( pBt->autoVacuum ){
4874 Pgno maxRootPgno;
4875 rc = sqlite3BtreeGetMeta(pBt, 4, &maxRootPgno);
4876 if( rc!=SQLITE_OK ){
4877 releasePage(pPage);
4878 return rc;
4879 }
4880
4881 if( iTable==maxRootPgno ){
4882 /* If the table being dropped is the table with the largest root-page
4883 ** number in the database, put the root page on the free list.
4884 */
4885 rc = freePage(pPage);
4886 releasePage(pPage);
4887 if( rc!=SQLITE_OK ){
4888 return rc;
4889 }
4890 }else{
4891 /* The table being dropped does not have the largest root-page
4892 ** number in the database. So move the page that does into the
4893 ** gap left by the deleted root-page.
4894 */
4895 MemPage *pMove;
4896 releasePage(pPage);
4897 rc = getPage(pBt, maxRootPgno, &pMove);
4898 if( rc!=SQLITE_OK ){
4899 return rc;
4900 }
4901 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
4902 releasePage(pMove);
4903 if( rc!=SQLITE_OK ){
4904 return rc;
4905 }
4906 rc = getPage(pBt, maxRootPgno, &pMove);
4907 if( rc!=SQLITE_OK ){
4908 return rc;
4909 }
4910 rc = freePage(pMove);
4911 releasePage(pMove);
4912 if( rc!=SQLITE_OK ){
4913 return rc;
4914 }
4915 *piMoved = maxRootPgno;
4916 }
4917
danielk1977599fcba2004-11-08 07:13:13 +00004918 /* Set the new 'max-root-page' value in the database header. This
4919 ** is the old value less one, less one more if that happens to
4920 ** be a root-page number, less one again if that is the
4921 ** PENDING_BYTE_PAGE.
4922 */
danielk197787a6e732004-11-05 12:58:25 +00004923 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00004924 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
4925 maxRootPgno--;
4926 }
drh42cac6d2004-11-20 20:31:11 +00004927 if( maxRootPgno==PTRMAP_PAGENO(pBt->usableSize, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00004928 maxRootPgno--;
4929 }
danielk1977599fcba2004-11-08 07:13:13 +00004930 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
4931
danielk197787a6e732004-11-05 12:58:25 +00004932 rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004933 }else{
4934 rc = freePage(pPage);
4935 releasePage(pPage);
4936 }
4937#endif
drh2aa679f2001-06-25 02:11:07 +00004938 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004939 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00004940 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00004941 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00004942 }
drh8b2f49b2001-06-08 00:21:52 +00004943 return rc;
4944}
4945
drh001bbcb2003-03-19 03:14:00 +00004946
drh8b2f49b2001-06-08 00:21:52 +00004947/*
drh23e11ca2004-05-04 17:27:28 +00004948** Read the meta-information out of a database file. Meta[0]
4949** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00004950** through meta[15] are available for use by higher layers. Meta[0]
4951** is read-only, the others are read/write.
4952**
4953** The schema layer numbers meta values differently. At the schema
4954** layer (and the SetCookie and ReadCookie opcodes) the number of
4955** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00004956*/
drh3aac2dd2004-04-26 14:10:20 +00004957int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){
drh8b2f49b2001-06-08 00:21:52 +00004958 int rc;
drh4b70f112004-05-02 21:12:19 +00004959 unsigned char *pP1;
drh8b2f49b2001-06-08 00:21:52 +00004960
drh23e11ca2004-05-04 17:27:28 +00004961 assert( idx>=0 && idx<=15 );
drha34b6762004-05-07 13:30:42 +00004962 rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1);
drh8b2f49b2001-06-08 00:21:52 +00004963 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00004964 *pMeta = get4byte(&pP1[36 + idx*4]);
drha34b6762004-05-07 13:30:42 +00004965 sqlite3pager_unref(pP1);
drhae157872004-08-14 19:20:09 +00004966
danielk1977599fcba2004-11-08 07:13:13 +00004967 /* If autovacuumed is disabled in this build but we are trying to
4968 ** access an autovacuumed database, then make the database readonly.
4969 */
danielk1977003ba062004-11-04 02:57:33 +00004970#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00004971 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00004972#endif
drhae157872004-08-14 19:20:09 +00004973
drh8b2f49b2001-06-08 00:21:52 +00004974 return SQLITE_OK;
4975}
4976
4977/*
drh23e11ca2004-05-04 17:27:28 +00004978** Write meta-information back into the database. Meta[0] is
4979** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00004980*/
drh3aac2dd2004-04-26 14:10:20 +00004981int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){
drh4b70f112004-05-02 21:12:19 +00004982 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00004983 int rc;
drh23e11ca2004-05-04 17:27:28 +00004984 assert( idx>=1 && idx<=15 );
danielk1977ee5741e2004-05-31 10:01:34 +00004985 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004986 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh5df72a52002-06-06 23:16:05 +00004987 }
drhde647132004-05-07 17:57:49 +00004988 assert( pBt->pPage1!=0 );
4989 pP1 = pBt->pPage1->aData;
drha34b6762004-05-07 13:30:42 +00004990 rc = sqlite3pager_write(pP1);
drh4b70f112004-05-02 21:12:19 +00004991 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00004992 put4byte(&pP1[36 + idx*4], iMeta);
drh8b2f49b2001-06-08 00:21:52 +00004993 return SQLITE_OK;
4994}
drh8c42ca92001-06-22 19:15:00 +00004995
drhf328bc82004-05-10 23:29:49 +00004996/*
4997** Return the flag byte at the beginning of the page that the cursor
4998** is currently pointing to.
4999*/
5000int sqlite3BtreeFlags(BtCursor *pCur){
5001 MemPage *pPage = pCur->pPage;
5002 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
5003}
5004
danielk1977b5402fb2005-01-12 07:15:04 +00005005#ifdef SQLITE_DEBUG
drh8c42ca92001-06-22 19:15:00 +00005006/*
5007** Print a disassembly of the given page on standard output. This routine
5008** is used for debugging and testing only.
5009*/
danielk1977c7dc7532004-11-17 10:22:03 +00005010static int btreePageDump(Btree *pBt, int pgno, int recursive, MemPage *pParent){
drh8c42ca92001-06-22 19:15:00 +00005011 int rc;
5012 MemPage *pPage;
drhc8629a12004-05-08 20:07:40 +00005013 int i, j, c;
drh8c42ca92001-06-22 19:15:00 +00005014 int nFree;
5015 u16 idx;
drhab9f7f12004-05-08 10:56:11 +00005016 int hdr;
drh43605152004-05-29 21:46:49 +00005017 int nCell;
drha2fce642004-06-05 00:01:44 +00005018 int isInit;
drhab9f7f12004-05-08 10:56:11 +00005019 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00005020 char range[20];
5021 unsigned char payload[20];
drhab9f7f12004-05-08 10:56:11 +00005022
drh4b70f112004-05-02 21:12:19 +00005023 rc = getPage(pBt, (Pgno)pgno, &pPage);
drha2fce642004-06-05 00:01:44 +00005024 isInit = pPage->isInit;
5025 if( pPage->isInit==0 ){
danielk1977c7dc7532004-11-17 10:22:03 +00005026 initPage(pPage, pParent);
drha2fce642004-06-05 00:01:44 +00005027 }
drh8c42ca92001-06-22 19:15:00 +00005028 if( rc ){
5029 return rc;
5030 }
drhab9f7f12004-05-08 10:56:11 +00005031 hdr = pPage->hdrOffset;
5032 data = pPage->aData;
drhc8629a12004-05-08 20:07:40 +00005033 c = data[hdr];
drh8b18dd42004-05-12 19:18:15 +00005034 pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
drhc8629a12004-05-08 20:07:40 +00005035 pPage->zeroData = (c & PTF_ZERODATA)!=0;
drh8b18dd42004-05-12 19:18:15 +00005036 pPage->leafData = (c & PTF_LEAFDATA)!=0;
drhc8629a12004-05-08 20:07:40 +00005037 pPage->leaf = (c & PTF_LEAF)!=0;
drh8b18dd42004-05-12 19:18:15 +00005038 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
drh43605152004-05-29 21:46:49 +00005039 nCell = get2byte(&data[hdr+3]);
drhfe63d1c2004-09-08 20:13:04 +00005040 sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
drh43605152004-05-29 21:46:49 +00005041 data[hdr], data[hdr+7],
drhda200cc2004-05-09 11:51:38 +00005042 (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
drhab9f7f12004-05-08 10:56:11 +00005043 assert( hdr == (pgno==1 ? 100 : 0) );
drh43605152004-05-29 21:46:49 +00005044 idx = hdr + 12 - pPage->leaf*4;
5045 for(i=0; i<nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00005046 CellInfo info;
drh4b70f112004-05-02 21:12:19 +00005047 Pgno child;
drh43605152004-05-29 21:46:49 +00005048 unsigned char *pCell;
drh6f11bef2004-05-13 01:12:56 +00005049 int sz;
drh43605152004-05-29 21:46:49 +00005050 int addr;
drh6f11bef2004-05-13 01:12:56 +00005051
drh43605152004-05-29 21:46:49 +00005052 addr = get2byte(&data[idx + 2*i]);
5053 pCell = &data[addr];
5054 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00005055 sz = info.nSize;
drh43605152004-05-29 21:46:49 +00005056 sprintf(range,"%d..%d", addr, addr+sz-1);
drh4b70f112004-05-02 21:12:19 +00005057 if( pPage->leaf ){
5058 child = 0;
5059 }else{
drh43605152004-05-29 21:46:49 +00005060 child = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005061 }
drh6f11bef2004-05-13 01:12:56 +00005062 sz = info.nData;
5063 if( !pPage->intKey ) sz += info.nKey;
drh8c42ca92001-06-22 19:15:00 +00005064 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
drh6f11bef2004-05-13 01:12:56 +00005065 memcpy(payload, &pCell[info.nHeader], sz);
drh8c42ca92001-06-22 19:15:00 +00005066 for(j=0; j<sz; j++){
5067 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
5068 }
5069 payload[sz] = 0;
drhfe63d1c2004-09-08 20:13:04 +00005070 sqlite3DebugPrintf(
drh6f11bef2004-05-13 01:12:56 +00005071 "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
5072 i, range, child, info.nKey, info.nData, payload
drh8c42ca92001-06-22 19:15:00 +00005073 );
drh8c42ca92001-06-22 19:15:00 +00005074 }
drh4b70f112004-05-02 21:12:19 +00005075 if( !pPage->leaf ){
drhfe63d1c2004-09-08 20:13:04 +00005076 sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
drh4b70f112004-05-02 21:12:19 +00005077 }
drh8c42ca92001-06-22 19:15:00 +00005078 nFree = 0;
5079 i = 0;
drhab9f7f12004-05-08 10:56:11 +00005080 idx = get2byte(&data[hdr+1]);
drhb6f41482004-05-14 01:58:11 +00005081 while( idx>0 && idx<pPage->pBt->usableSize ){
drhab9f7f12004-05-08 10:56:11 +00005082 int sz = get2byte(&data[idx+2]);
drh4b70f112004-05-02 21:12:19 +00005083 sprintf(range,"%d..%d", idx, idx+sz-1);
5084 nFree += sz;
drhfe63d1c2004-09-08 20:13:04 +00005085 sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
drh4b70f112004-05-02 21:12:19 +00005086 i, range, sz, nFree);
drhab9f7f12004-05-08 10:56:11 +00005087 idx = get2byte(&data[idx]);
drh2aa679f2001-06-25 02:11:07 +00005088 i++;
drh8c42ca92001-06-22 19:15:00 +00005089 }
5090 if( idx!=0 ){
drhfe63d1c2004-09-08 20:13:04 +00005091 sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
drh8c42ca92001-06-22 19:15:00 +00005092 }
drha34b6762004-05-07 13:30:42 +00005093 if( recursive && !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005094 for(i=0; i<nCell; i++){
5095 unsigned char *pCell = findCell(pPage, i);
danielk1977c7dc7532004-11-17 10:22:03 +00005096 btreePageDump(pBt, get4byte(pCell), 1, pPage);
drha34b6762004-05-07 13:30:42 +00005097 idx = get2byte(pCell);
drh6019e162001-07-02 17:51:45 +00005098 }
danielk1977c7dc7532004-11-17 10:22:03 +00005099 btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
drh6019e162001-07-02 17:51:45 +00005100 }
drha2fce642004-06-05 00:01:44 +00005101 pPage->isInit = isInit;
drhab9f7f12004-05-08 10:56:11 +00005102 sqlite3pager_unref(data);
drh3644f082004-05-10 18:45:09 +00005103 fflush(stdout);
drh8c42ca92001-06-22 19:15:00 +00005104 return SQLITE_OK;
5105}
danielk1977c7dc7532004-11-17 10:22:03 +00005106int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){
5107 return btreePageDump(pBt, pgno, recursive, 0);
5108}
drhaaab5722002-02-19 13:39:21 +00005109#endif
drh8c42ca92001-06-22 19:15:00 +00005110
drhaaab5722002-02-19 13:39:21 +00005111#ifdef SQLITE_TEST
drh8c42ca92001-06-22 19:15:00 +00005112/*
drh2aa679f2001-06-25 02:11:07 +00005113** Fill aResult[] with information about the entry and page that the
5114** cursor is pointing to.
5115**
5116** aResult[0] = The page number
5117** aResult[1] = The entry number
5118** aResult[2] = Total number of entries on this page
drh3e27c022004-07-23 00:01:38 +00005119** aResult[3] = Cell size (local payload + header)
drh2aa679f2001-06-25 02:11:07 +00005120** aResult[4] = Number of free bytes on this page
5121** aResult[5] = Number of free blocks on the page
drh3e27c022004-07-23 00:01:38 +00005122** aResult[6] = Total payload size (local + overflow)
5123** aResult[7] = Header size in bytes
5124** aResult[8] = Local payload size
5125** aResult[9] = Parent page number
drh5eddca62001-06-30 21:53:53 +00005126**
5127** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00005128*/
drh3e27c022004-07-23 00:01:38 +00005129int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
drh2aa679f2001-06-25 02:11:07 +00005130 int cnt, idx;
5131 MemPage *pPage = pCur->pPage;
drh3e27c022004-07-23 00:01:38 +00005132 BtCursor tmpCur;
drhda200cc2004-05-09 11:51:38 +00005133
5134 pageIntegrity(pPage);
drh4b70f112004-05-02 21:12:19 +00005135 assert( pPage->isInit );
drh3e27c022004-07-23 00:01:38 +00005136 getTempCursor(pCur, &tmpCur);
5137 while( upCnt-- ){
5138 moveToParent(&tmpCur);
5139 }
5140 pPage = tmpCur.pPage;
5141 pageIntegrity(pPage);
drha34b6762004-05-07 13:30:42 +00005142 aResult[0] = sqlite3pager_pagenumber(pPage->aData);
drh91025292004-05-03 19:49:32 +00005143 assert( aResult[0]==pPage->pgno );
drh3e27c022004-07-23 00:01:38 +00005144 aResult[1] = tmpCur.idx;
drh2aa679f2001-06-25 02:11:07 +00005145 aResult[2] = pPage->nCell;
drh3e27c022004-07-23 00:01:38 +00005146 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
5147 getCellInfo(&tmpCur);
5148 aResult[3] = tmpCur.info.nSize;
5149 aResult[6] = tmpCur.info.nData;
5150 aResult[7] = tmpCur.info.nHeader;
5151 aResult[8] = tmpCur.info.nLocal;
drh2aa679f2001-06-25 02:11:07 +00005152 }else{
5153 aResult[3] = 0;
5154 aResult[6] = 0;
drh3e27c022004-07-23 00:01:38 +00005155 aResult[7] = 0;
5156 aResult[8] = 0;
drh2aa679f2001-06-25 02:11:07 +00005157 }
5158 aResult[4] = pPage->nFree;
5159 cnt = 0;
drh4b70f112004-05-02 21:12:19 +00005160 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
drhb6f41482004-05-14 01:58:11 +00005161 while( idx>0 && idx<pPage->pBt->usableSize ){
drh2aa679f2001-06-25 02:11:07 +00005162 cnt++;
drh4b70f112004-05-02 21:12:19 +00005163 idx = get2byte(&pPage->aData[idx]);
drh2aa679f2001-06-25 02:11:07 +00005164 }
5165 aResult[5] = cnt;
drh3e27c022004-07-23 00:01:38 +00005166 if( pPage->pParent==0 || isRootPage(pPage) ){
5167 aResult[9] = 0;
5168 }else{
5169 aResult[9] = pPage->pParent->pgno;
5170 }
5171 releaseTempCursor(&tmpCur);
drh8c42ca92001-06-22 19:15:00 +00005172 return SQLITE_OK;
5173}
drhaaab5722002-02-19 13:39:21 +00005174#endif
drhdd793422001-06-28 01:54:48 +00005175
drhdd793422001-06-28 01:54:48 +00005176/*
drh5eddca62001-06-30 21:53:53 +00005177** Return the pager associated with a BTree. This routine is used for
5178** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00005179*/
drh3aac2dd2004-04-26 14:10:20 +00005180Pager *sqlite3BtreePager(Btree *pBt){
drhdd793422001-06-28 01:54:48 +00005181 return pBt->pPager;
5182}
drh5eddca62001-06-30 21:53:53 +00005183
5184/*
5185** This structure is passed around through all the sanity checking routines
5186** in order to keep track of some global state information.
5187*/
drhaaab5722002-02-19 13:39:21 +00005188typedef struct IntegrityCk IntegrityCk;
5189struct IntegrityCk {
drh100569d2001-10-02 13:01:48 +00005190 Btree *pBt; /* The tree being checked out */
5191 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
5192 int nPage; /* Number of pages in the database */
5193 int *anRef; /* Number of times each page is referenced */
drh100569d2001-10-02 13:01:48 +00005194 char *zErrMsg; /* An error message. NULL of no errors seen. */
drh5eddca62001-06-30 21:53:53 +00005195};
5196
drhb7f91642004-10-31 02:22:47 +00005197#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005198/*
5199** Append a message to the error message string.
5200*/
drh2e38c322004-09-03 18:38:44 +00005201static void checkAppendMsg(
5202 IntegrityCk *pCheck,
5203 char *zMsg1,
5204 const char *zFormat,
5205 ...
5206){
5207 va_list ap;
5208 char *zMsg2;
5209 va_start(ap, zFormat);
5210 zMsg2 = sqlite3VMPrintf(zFormat, ap);
5211 va_end(ap);
5212 if( zMsg1==0 ) zMsg1 = "";
drh5eddca62001-06-30 21:53:53 +00005213 if( pCheck->zErrMsg ){
5214 char *zOld = pCheck->zErrMsg;
5215 pCheck->zErrMsg = 0;
danielk19774adee202004-05-08 08:23:19 +00005216 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00005217 sqliteFree(zOld);
5218 }else{
danielk19774adee202004-05-08 08:23:19 +00005219 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00005220 }
drh2e38c322004-09-03 18:38:44 +00005221 sqliteFree(zMsg2);
drh5eddca62001-06-30 21:53:53 +00005222}
drhb7f91642004-10-31 02:22:47 +00005223#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00005224
drhb7f91642004-10-31 02:22:47 +00005225#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005226/*
5227** Add 1 to the reference count for page iPage. If this is the second
5228** reference to the page, add an error message to pCheck->zErrMsg.
5229** Return 1 if there are 2 ore more references to the page and 0 if
5230** if this is the first reference to the page.
5231**
5232** Also check that the page number is in bounds.
5233*/
drhaaab5722002-02-19 13:39:21 +00005234static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00005235 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00005236 if( iPage>pCheck->nPage || iPage<0 ){
drh2e38c322004-09-03 18:38:44 +00005237 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00005238 return 1;
5239 }
5240 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00005241 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00005242 return 1;
5243 }
5244 return (pCheck->anRef[iPage]++)>1;
5245}
5246
danielk1977afcdd022004-10-31 16:25:42 +00005247#ifndef SQLITE_OMIT_AUTOVACUUM
5248/*
5249** Check that the entry in the pointer-map for page iChild maps to
5250** page iParent, pointer type ptrType. If not, append an error message
5251** to pCheck.
5252*/
5253static void checkPtrmap(
5254 IntegrityCk *pCheck, /* Integrity check context */
5255 Pgno iChild, /* Child page number */
5256 u8 eType, /* Expected pointer map type */
5257 Pgno iParent, /* Expected pointer map parent page number */
5258 char *zContext /* Context description (used for error msg) */
5259){
5260 int rc;
5261 u8 ePtrmapType;
5262 Pgno iPtrmapParent;
5263
5264 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
5265 if( rc!=SQLITE_OK ){
5266 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
5267 return;
5268 }
5269
5270 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
5271 checkAppendMsg(pCheck, zContext,
5272 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
5273 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
5274 }
5275}
5276#endif
5277
drh5eddca62001-06-30 21:53:53 +00005278/*
5279** Check the integrity of the freelist or of an overflow page list.
5280** Verify that the number of pages on the list is N.
5281*/
drh30e58752002-03-02 20:41:57 +00005282static void checkList(
5283 IntegrityCk *pCheck, /* Integrity checking context */
5284 int isFreeList, /* True for a freelist. False for overflow page list */
5285 int iPage, /* Page number for first page in the list */
5286 int N, /* Expected number of pages in the list */
5287 char *zContext /* Context for error messages */
5288){
5289 int i;
drh3a4c1412004-05-09 20:40:11 +00005290 int expected = N;
5291 int iFirst = iPage;
drh30e58752002-03-02 20:41:57 +00005292 while( N-- > 0 ){
drh4b70f112004-05-02 21:12:19 +00005293 unsigned char *pOvfl;
drh5eddca62001-06-30 21:53:53 +00005294 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00005295 checkAppendMsg(pCheck, zContext,
5296 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00005297 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00005298 break;
5299 }
5300 if( checkRef(pCheck, iPage, zContext) ) break;
drha34b6762004-05-07 13:30:42 +00005301 if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
drh2e38c322004-09-03 18:38:44 +00005302 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00005303 break;
5304 }
drh30e58752002-03-02 20:41:57 +00005305 if( isFreeList ){
drh4b70f112004-05-02 21:12:19 +00005306 int n = get4byte(&pOvfl[4]);
danielk1977687566d2004-11-02 12:56:41 +00005307#ifndef SQLITE_OMIT_AUTOVACUUM
5308 if( pCheck->pBt->autoVacuum ){
5309 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
5310 }
5311#endif
drh855eb1c2004-08-31 13:45:11 +00005312 if( n>pCheck->pBt->usableSize/4-8 ){
drh2e38c322004-09-03 18:38:44 +00005313 checkAppendMsg(pCheck, zContext,
5314 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00005315 N--;
5316 }else{
5317 for(i=0; i<n; i++){
danielk1977687566d2004-11-02 12:56:41 +00005318 Pgno iFreePage = get4byte(&pOvfl[8+i*4]);
5319#ifndef SQLITE_OMIT_AUTOVACUUM
5320 if( pCheck->pBt->autoVacuum ){
5321 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
5322 }
5323#endif
5324 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00005325 }
5326 N -= n;
drh30e58752002-03-02 20:41:57 +00005327 }
drh30e58752002-03-02 20:41:57 +00005328 }
danielk1977afcdd022004-10-31 16:25:42 +00005329#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00005330 else{
5331 /* If this database supports auto-vacuum and iPage is not the last
5332 ** page in this overflow list, check that the pointer-map entry for
5333 ** the following page matches iPage.
5334 */
5335 if( pCheck->pBt->autoVacuum && N>0 ){
5336 i = get4byte(pOvfl);
5337 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
5338 }
danielk1977afcdd022004-10-31 16:25:42 +00005339 }
5340#endif
drh4b70f112004-05-02 21:12:19 +00005341 iPage = get4byte(pOvfl);
drha34b6762004-05-07 13:30:42 +00005342 sqlite3pager_unref(pOvfl);
drh5eddca62001-06-30 21:53:53 +00005343 }
5344}
drhb7f91642004-10-31 02:22:47 +00005345#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00005346
drhb7f91642004-10-31 02:22:47 +00005347#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005348/*
5349** Do various sanity checks on a single page of a tree. Return
5350** the tree depth. Root pages return 0. Parents of root pages
5351** return 1, and so forth.
5352**
5353** These checks are done:
5354**
5355** 1. Make sure that cells and freeblocks do not overlap
5356** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00005357** NO 2. Make sure cell keys are in order.
5358** NO 3. Make sure no key is less than or equal to zLowerBound.
5359** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00005360** 5. Check the integrity of overflow pages.
5361** 6. Recursively call checkTreePage on all children.
5362** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00005363** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00005364** the root of the tree.
5365*/
5366static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00005367 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00005368 int iPage, /* Page number of the page to check */
5369 MemPage *pParent, /* Parent page */
5370 char *zParentContext, /* Parent context */
5371 char *zLowerBound, /* All keys should be greater than this, if not NULL */
drh1bffb9c2002-02-03 17:37:36 +00005372 int nLower, /* Number of characters in zLowerBound */
5373 char *zUpperBound, /* All keys should be less than this, if not NULL */
5374 int nUpper /* Number of characters in zUpperBound */
drh5eddca62001-06-30 21:53:53 +00005375){
5376 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00005377 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00005378 int hdr, cellStart;
5379 int nCell;
drhda200cc2004-05-09 11:51:38 +00005380 u8 *data;
drh5eddca62001-06-30 21:53:53 +00005381 BtCursor cur;
drh0d316a42002-08-11 20:10:47 +00005382 Btree *pBt;
drhb6f41482004-05-14 01:58:11 +00005383 int maxLocal, usableSize;
drh5eddca62001-06-30 21:53:53 +00005384 char zContext[100];
drh2e38c322004-09-03 18:38:44 +00005385 char *hit;
drh5eddca62001-06-30 21:53:53 +00005386
danielk1977ef73ee92004-11-06 12:26:07 +00005387 sprintf(zContext, "Page %d: ", iPage);
5388
drh5eddca62001-06-30 21:53:53 +00005389 /* Check that the page exists
5390 */
drh0d316a42002-08-11 20:10:47 +00005391 cur.pBt = pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00005392 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00005393 if( iPage==0 ) return 0;
5394 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh4b70f112004-05-02 21:12:19 +00005395 if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){
drh2e38c322004-09-03 18:38:44 +00005396 checkAppendMsg(pCheck, zContext,
5397 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00005398 return 0;
5399 }
drh6f11bef2004-05-13 01:12:56 +00005400 maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal;
drh4b70f112004-05-02 21:12:19 +00005401 if( (rc = initPage(pPage, pParent))!=0 ){
drh2e38c322004-09-03 18:38:44 +00005402 checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00005403 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00005404 return 0;
5405 }
5406
5407 /* Check out all the cells.
5408 */
5409 depth = 0;
drh5eddca62001-06-30 21:53:53 +00005410 cur.pPage = pPage;
drh5eddca62001-06-30 21:53:53 +00005411 for(i=0; i<pPage->nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00005412 u8 *pCell;
5413 int sz;
5414 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00005415
5416 /* Check payload overflow pages
5417 */
drh3a4c1412004-05-09 20:40:11 +00005418 sprintf(zContext, "On tree page %d cell %d: ", iPage, i);
drh43605152004-05-29 21:46:49 +00005419 pCell = findCell(pPage,i);
5420 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00005421 sz = info.nData;
5422 if( !pPage->intKey ) sz += info.nKey;
5423 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00005424 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00005425 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
5426#ifndef SQLITE_OMIT_AUTOVACUUM
5427 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00005428 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00005429 }
5430#endif
5431 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00005432 }
5433
5434 /* Check sanity of left child page.
5435 */
drhda200cc2004-05-09 11:51:38 +00005436 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005437 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00005438#ifndef SQLITE_OMIT_AUTOVACUUM
5439 if( pBt->autoVacuum ){
5440 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
5441 }
5442#endif
drhda200cc2004-05-09 11:51:38 +00005443 d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0);
5444 if( i>0 && d2!=depth ){
5445 checkAppendMsg(pCheck, zContext, "Child page depth differs");
5446 }
5447 depth = d2;
drh5eddca62001-06-30 21:53:53 +00005448 }
drh5eddca62001-06-30 21:53:53 +00005449 }
drhda200cc2004-05-09 11:51:38 +00005450 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005451 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drhda200cc2004-05-09 11:51:38 +00005452 sprintf(zContext, "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00005453#ifndef SQLITE_OMIT_AUTOVACUUM
5454 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00005455 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00005456 }
5457#endif
drhda200cc2004-05-09 11:51:38 +00005458 checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0);
5459 }
drh5eddca62001-06-30 21:53:53 +00005460
5461 /* Check for complete coverage of the page
5462 */
drhda200cc2004-05-09 11:51:38 +00005463 data = pPage->aData;
5464 hdr = pPage->hdrOffset;
drh2e38c322004-09-03 18:38:44 +00005465 hit = sqliteMalloc( usableSize );
5466 if( hit ){
5467 memset(hit, 1, get2byte(&data[hdr+5]));
5468 nCell = get2byte(&data[hdr+3]);
5469 cellStart = hdr + 12 - 4*pPage->leaf;
5470 for(i=0; i<nCell; i++){
5471 int pc = get2byte(&data[cellStart+i*2]);
5472 int size = cellSizePtr(pPage, &data[pc]);
5473 int j;
danielk19777701e812005-01-10 12:59:51 +00005474 if( (pc+size-1)>=usableSize || pc<0 ){
5475 checkAppendMsg(pCheck, 0,
5476 "Corruption detected in cell %d on page %d",i,iPage,0);
5477 }else{
5478 for(j=pc+size-1; j>=pc; j--) hit[j]++;
5479 }
drh2e38c322004-09-03 18:38:44 +00005480 }
5481 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
5482 cnt++){
5483 int size = get2byte(&data[i+2]);
5484 int j;
danielk19777701e812005-01-10 12:59:51 +00005485 if( (i+size-1)>=usableSize || i<0 ){
5486 checkAppendMsg(pCheck, 0,
5487 "Corruption detected in cell %d on page %d",i,iPage,0);
5488 }else{
5489 for(j=i+size-1; j>=i; j--) hit[j]++;
5490 }
drh2e38c322004-09-03 18:38:44 +00005491 i = get2byte(&data[i]);
5492 }
5493 for(i=cnt=0; i<usableSize; i++){
5494 if( hit[i]==0 ){
5495 cnt++;
5496 }else if( hit[i]>1 ){
5497 checkAppendMsg(pCheck, 0,
5498 "Multiple uses for byte %d of page %d", i, iPage);
5499 break;
5500 }
5501 }
5502 if( cnt!=data[hdr+7] ){
5503 checkAppendMsg(pCheck, 0,
5504 "Fragmented space is %d byte reported as %d on page %d",
5505 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00005506 }
5507 }
drh2e38c322004-09-03 18:38:44 +00005508 sqliteFree(hit);
drh6019e162001-07-02 17:51:45 +00005509
drh4b70f112004-05-02 21:12:19 +00005510 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00005511 return depth+1;
drh5eddca62001-06-30 21:53:53 +00005512}
drhb7f91642004-10-31 02:22:47 +00005513#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00005514
drhb7f91642004-10-31 02:22:47 +00005515#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005516/*
5517** This routine does a complete check of the given BTree file. aRoot[] is
5518** an array of pages numbers were each page number is the root page of
5519** a table. nRoot is the number of entries in aRoot.
5520**
5521** If everything checks out, this routine returns NULL. If something is
5522** amiss, an error message is written into memory obtained from malloc()
5523** and a pointer to that error message is returned. The calling function
5524** is responsible for freeing the error message when it is done.
5525*/
drh3aac2dd2004-04-26 14:10:20 +00005526char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){
drh5eddca62001-06-30 21:53:53 +00005527 int i;
5528 int nRef;
drhaaab5722002-02-19 13:39:21 +00005529 IntegrityCk sCheck;
drh5eddca62001-06-30 21:53:53 +00005530
drha34b6762004-05-07 13:30:42 +00005531 nRef = *sqlite3pager_stats(pBt->pPager);
drhefc251d2001-07-01 22:12:01 +00005532 if( lockBtree(pBt)!=SQLITE_OK ){
5533 return sqliteStrDup("Unable to acquire a read lock on the database");
5534 }
drh5eddca62001-06-30 21:53:53 +00005535 sCheck.pBt = pBt;
5536 sCheck.pPager = pBt->pPager;
drha34b6762004-05-07 13:30:42 +00005537 sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager);
drh0de8c112002-07-06 16:32:14 +00005538 if( sCheck.nPage==0 ){
5539 unlockBtreeIfUnused(pBt);
5540 return 0;
5541 }
drh8c1238a2003-01-02 14:43:55 +00005542 sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00005543 if( !sCheck.anRef ){
5544 unlockBtreeIfUnused(pBt);
5545 return sqlite3MPrintf("Unable to malloc %d bytes",
5546 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
5547 }
drhda200cc2004-05-09 11:51:38 +00005548 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00005549 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00005550 if( i<=sCheck.nPage ){
5551 sCheck.anRef[i] = 1;
5552 }
drh5eddca62001-06-30 21:53:53 +00005553 sCheck.zErrMsg = 0;
5554
5555 /* Check the integrity of the freelist
5556 */
drha34b6762004-05-07 13:30:42 +00005557 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
5558 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00005559
5560 /* Check all the tables.
5561 */
5562 for(i=0; i<nRoot; i++){
drh4ff6dfa2002-03-03 23:06:00 +00005563 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00005564#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00005565 if( pBt->autoVacuum && aRoot[i]>1 ){
5566 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
5567 }
5568#endif
drh1bffb9c2002-02-03 17:37:36 +00005569 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0);
drh5eddca62001-06-30 21:53:53 +00005570 }
5571
5572 /* Make sure every page in the file is referenced
5573 */
5574 for(i=1; i<=sCheck.nPage; i++){
danielk1977afcdd022004-10-31 16:25:42 +00005575#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00005576 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00005577 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00005578 }
danielk1977afcdd022004-10-31 16:25:42 +00005579#else
5580 /* If the database supports auto-vacuum, make sure no tables contain
5581 ** references to pointer-map pages.
5582 */
5583 if( sCheck.anRef[i]==0 &&
drh42cac6d2004-11-20 20:31:11 +00005584 (PTRMAP_PAGENO(pBt->usableSize, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00005585 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
5586 }
5587 if( sCheck.anRef[i]!=0 &&
drh42cac6d2004-11-20 20:31:11 +00005588 (PTRMAP_PAGENO(pBt->usableSize, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00005589 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
5590 }
5591#endif
drh5eddca62001-06-30 21:53:53 +00005592 }
5593
5594 /* Make sure this analysis did not leave any unref() pages
5595 */
drh5e00f6c2001-09-13 13:46:56 +00005596 unlockBtreeIfUnused(pBt);
drha34b6762004-05-07 13:30:42 +00005597 if( nRef != *sqlite3pager_stats(pBt->pPager) ){
drh2e38c322004-09-03 18:38:44 +00005598 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00005599 "Outstanding page count goes from %d to %d during this analysis",
drha34b6762004-05-07 13:30:42 +00005600 nRef, *sqlite3pager_stats(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00005601 );
drh5eddca62001-06-30 21:53:53 +00005602 }
5603
5604 /* Clean up and report errors.
5605 */
5606 sqliteFree(sCheck.anRef);
5607 return sCheck.zErrMsg;
5608}
drhb7f91642004-10-31 02:22:47 +00005609#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00005610
drh73509ee2003-04-06 20:44:45 +00005611/*
5612** Return the full pathname of the underlying database file.
5613*/
drh3aac2dd2004-04-26 14:10:20 +00005614const char *sqlite3BtreeGetFilename(Btree *pBt){
drh73509ee2003-04-06 20:44:45 +00005615 assert( pBt->pPager!=0 );
drha34b6762004-05-07 13:30:42 +00005616 return sqlite3pager_filename(pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00005617}
5618
5619/*
danielk19775865e3d2004-06-14 06:03:57 +00005620** Return the pathname of the directory that contains the database file.
5621*/
5622const char *sqlite3BtreeGetDirname(Btree *pBt){
5623 assert( pBt->pPager!=0 );
5624 return sqlite3pager_dirname(pBt->pPager);
5625}
5626
5627/*
5628** Return the pathname of the journal file for this database. The return
5629** value of this routine is the same regardless of whether the journal file
5630** has been created or not.
5631*/
5632const char *sqlite3BtreeGetJournalname(Btree *pBt){
5633 assert( pBt->pPager!=0 );
5634 return sqlite3pager_journalname(pBt->pPager);
5635}
5636
drhb7f91642004-10-31 02:22:47 +00005637#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00005638/*
drhf7c57532003-04-25 13:22:51 +00005639** Copy the complete content of pBtFrom into pBtTo. A transaction
5640** must be active for both files.
5641**
5642** The size of file pBtFrom may be reduced by this operation.
drh43605152004-05-29 21:46:49 +00005643** If anything goes wrong, the transaction on pBtFrom is rolled back.
drh73509ee2003-04-06 20:44:45 +00005644*/
drh3aac2dd2004-04-26 14:10:20 +00005645int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){
drhf7c57532003-04-25 13:22:51 +00005646 int rc = SQLITE_OK;
drh2e6d11b2003-04-25 15:37:57 +00005647 Pgno i, nPage, nToPage;
drhf7c57532003-04-25 13:22:51 +00005648
danielk1977ee5741e2004-05-31 10:01:34 +00005649 if( pBtTo->inTrans!=TRANS_WRITE || pBtFrom->inTrans!=TRANS_WRITE ){
5650 return SQLITE_ERROR;
5651 }
drhf7c57532003-04-25 13:22:51 +00005652 if( pBtTo->pCursor ) return SQLITE_BUSY;
drha34b6762004-05-07 13:30:42 +00005653 nToPage = sqlite3pager_pagecount(pBtTo->pPager);
5654 nPage = sqlite3pager_pagecount(pBtFrom->pPager);
danielk1977369f27e2004-06-15 11:40:04 +00005655 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
drhf7c57532003-04-25 13:22:51 +00005656 void *pPage;
drha34b6762004-05-07 13:30:42 +00005657 rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage);
drhf7c57532003-04-25 13:22:51 +00005658 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005659 rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage);
drh2e6d11b2003-04-25 15:37:57 +00005660 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005661 sqlite3pager_unref(pPage);
drhf7c57532003-04-25 13:22:51 +00005662 }
drh2e6d11b2003-04-25 15:37:57 +00005663 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
5664 void *pPage;
drha34b6762004-05-07 13:30:42 +00005665 rc = sqlite3pager_get(pBtTo->pPager, i, &pPage);
drh2e6d11b2003-04-25 15:37:57 +00005666 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005667 rc = sqlite3pager_write(pPage);
5668 sqlite3pager_unref(pPage);
5669 sqlite3pager_dont_write(pBtTo->pPager, i);
drh2e6d11b2003-04-25 15:37:57 +00005670 }
5671 if( !rc && nPage<nToPage ){
drha34b6762004-05-07 13:30:42 +00005672 rc = sqlite3pager_truncate(pBtTo->pPager, nPage);
drh2e6d11b2003-04-25 15:37:57 +00005673 }
drhf7c57532003-04-25 13:22:51 +00005674 if( rc ){
drh3aac2dd2004-04-26 14:10:20 +00005675 sqlite3BtreeRollback(pBtTo);
drhf7c57532003-04-25 13:22:51 +00005676 }
5677 return rc;
drh73509ee2003-04-06 20:44:45 +00005678}
drhb7f91642004-10-31 02:22:47 +00005679#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00005680
5681/*
5682** Return non-zero if a transaction is active.
5683*/
5684int sqlite3BtreeIsInTrans(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00005685 return (pBt && (pBt->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00005686}
5687
5688/*
5689** Return non-zero if a statement transaction is active.
5690*/
5691int sqlite3BtreeIsInStmt(Btree *pBt){
5692 return (pBt && pBt->inStmt);
5693}
danielk197713adf8a2004-06-03 16:08:41 +00005694
5695/*
5696** This call is a no-op if no write-transaction is currently active on pBt.
5697**
5698** Otherwise, sync the database file for the btree pBt. zMaster points to
5699** the name of a master journal file that should be written into the
5700** individual journal file, or is NULL, indicating no master journal file
5701** (single database transaction).
5702**
5703** When this is called, the master journal should already have been
5704** created, populated with this journal pointer and synced to disk.
5705**
5706** Once this is routine has returned, the only thing required to commit
5707** the write-transaction for this database file is to delete the journal.
5708*/
5709int sqlite3BtreeSync(Btree *pBt, const char *zMaster){
5710 if( pBt->inTrans==TRANS_WRITE ){
danielk1977687566d2004-11-02 12:56:41 +00005711#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977d761c0c2004-11-05 16:37:02 +00005712 Pgno nTrunc = 0;
danielk1977687566d2004-11-02 12:56:41 +00005713 if( pBt->autoVacuum ){
danielk1977d761c0c2004-11-05 16:37:02 +00005714 int rc = autoVacuumCommit(pBt, &nTrunc);
danielk1977687566d2004-11-02 12:56:41 +00005715 if( rc!=SQLITE_OK ) return rc;
5716 }
danielk1977d761c0c2004-11-05 16:37:02 +00005717 return sqlite3pager_sync(pBt->pPager, zMaster, nTrunc);
danielk1977687566d2004-11-02 12:56:41 +00005718#endif
danielk1977d761c0c2004-11-05 16:37:02 +00005719 return sqlite3pager_sync(pBt->pPager, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00005720 }
5721 return SQLITE_OK;
5722}