blob: 51c208f9368f3b8da3fb3660692270759302f15e [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*************************************************************************
drh4b238df2005-01-08 15:43:18 +000012** $Id: btree.c,v 1.228 2005/01/08 15:43:19 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 */
359 u8 status; /* Set to SQLITE_ABORT if cursors is invalidated */
drh365d68f2001-05-11 11:02:46 +0000360};
drh7e3b0a02001-04-28 16:52:40 +0000361
drha059ad02001-04-17 20:09:11 +0000362/*
drh66cbd152004-09-01 16:12:25 +0000363** Forward declaration
364*/
365static int checkReadLocks(Btree*,Pgno,BtCursor*);
366
367
368/*
drhab01f612004-05-22 02:55:23 +0000369** Read or write a two- and four-byte big-endian integer values.
drh0d316a42002-08-11 20:10:47 +0000370*/
drh9e572e62004-04-23 23:43:10 +0000371static u32 get2byte(unsigned char *p){
372 return (p[0]<<8) | p[1];
drh0d316a42002-08-11 20:10:47 +0000373}
drh9e572e62004-04-23 23:43:10 +0000374static u32 get4byte(unsigned char *p){
375 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
376}
drh9e572e62004-04-23 23:43:10 +0000377static void put2byte(unsigned char *p, u32 v){
378 p[0] = v>>8;
379 p[1] = v;
380}
381static void put4byte(unsigned char *p, u32 v){
382 p[0] = v>>24;
383 p[1] = v>>16;
384 p[2] = v>>8;
385 p[3] = v;
386}
drh6f11bef2004-05-13 01:12:56 +0000387
drh9e572e62004-04-23 23:43:10 +0000388/*
drhab01f612004-05-22 02:55:23 +0000389** Routines to read and write variable-length integers. These used to
390** be defined locally, but now we use the varint routines in the util.c
391** file.
drh9e572e62004-04-23 23:43:10 +0000392*/
drh6d2fb152004-05-14 16:50:06 +0000393#define getVarint sqlite3GetVarint
394#define getVarint32 sqlite3GetVarint32
395#define putVarint sqlite3PutVarint
drh0d316a42002-08-11 20:10:47 +0000396
danielk1977599fcba2004-11-08 07:13:13 +0000397/* The database page the PENDING_BYTE occupies. This page is never used.
398** TODO: This macro is very similary to PAGER_MJ_PGNO() in pager.c. They
399** should possibly be consolidated (presumably in pager.h).
400*/
401#define PENDING_BYTE_PAGE(pBt) ((PENDING_BYTE/(pBt)->pageSize)+1)
danielk1977afcdd022004-10-31 16:25:42 +0000402
danielk1977599fcba2004-11-08 07:13:13 +0000403#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000404/*
drh42cac6d2004-11-20 20:31:11 +0000405** These macros define the location of the pointer-map entry for a
406** database page. The first argument to each is the number of usable
407** bytes on each page of the database (often 1024). The second is the
408** page number to look up in the pointer map.
danielk1977afcdd022004-10-31 16:25:42 +0000409**
410** PTRMAP_PAGENO returns the database page number of the pointer-map
411** page that stores the required pointer. PTRMAP_PTROFFSET returns
412** the offset of the requested map entry.
413**
414** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
415** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
danielk1977599fcba2004-11-08 07:13:13 +0000416** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
417** this test.
danielk1977afcdd022004-10-31 16:25:42 +0000418*/
419#define PTRMAP_PAGENO(pgsz, pgno) (((pgno-2)/(pgsz/5+1))*(pgsz/5+1)+2)
420#define PTRMAP_PTROFFSET(pgsz, pgno) (((pgno-2)%(pgsz/5+1)-1)*5)
danielk1977a19df672004-11-03 11:37:07 +0000421#define PTRMAP_ISPAGE(pgsz, pgno) (PTRMAP_PAGENO(pgsz,pgno)==pgno)
422
danielk1977afcdd022004-10-31 16:25:42 +0000423/*
danielk1977687566d2004-11-02 12:56:41 +0000424** The pointer map is a lookup table that contains an entry for each database
425** page in the file except for page 1. In this context 'database page' refers
426** to any page that is not part of the pointer map itself. Each pointer map
427** entry consists of a single byte 'type' and a 4 byte page number. The
428** PTRMAP_XXX identifiers below are the valid types. The interpretation
429** of the page-number depends on the type, as follows:
danielk1977afcdd022004-10-31 16:25:42 +0000430**
danielk1977687566d2004-11-02 12:56:41 +0000431** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
432** used in this case.
danielk1977afcdd022004-10-31 16:25:42 +0000433**
danielk1977687566d2004-11-02 12:56:41 +0000434** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
435** is not used in this case.
436**
437** PTRMAP_OVERFLOW1: The database page is the first page in a list of
438** overflow pages. The page number identifies the page that
439** contains the cell with a pointer to this overflow page.
440**
441** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
442** overflow pages. The page-number identifies the previous
443** page in the overflow page list.
444**
445** PTRMAP_BTREE: The database page is a non-root btree page. The page number
446** identifies the parent page in the btree.
danielk1977afcdd022004-10-31 16:25:42 +0000447*/
danielk1977687566d2004-11-02 12:56:41 +0000448#define PTRMAP_ROOTPAGE 1
449#define PTRMAP_FREEPAGE 2
450#define PTRMAP_OVERFLOW1 3
451#define PTRMAP_OVERFLOW2 4
452#define PTRMAP_BTREE 5
danielk1977afcdd022004-10-31 16:25:42 +0000453
454/*
455** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000456**
457** This routine updates the pointer map entry for page number 'key'
458** so that it maps to type 'eType' and parent page number 'pgno'.
459** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000460*/
461static int ptrmapPut(Btree *pBt, Pgno key, u8 eType, Pgno pgno){
462 u8 *pPtrmap; /* The pointer map page */
463 Pgno iPtrmap; /* The pointer map page number */
464 int offset; /* Offset in pointer map page */
465 int rc;
466
danielk1977599fcba2004-11-08 07:13:13 +0000467 assert( key!=0 );
drh42cac6d2004-11-20 20:31:11 +0000468 iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000469 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
danielk1977687566d2004-11-02 12:56:41 +0000470 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000471 return rc;
472 }
drh42cac6d2004-11-20 20:31:11 +0000473 offset = PTRMAP_PTROFFSET(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000474
475 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=pgno ){
476 rc = sqlite3pager_write(pPtrmap);
477 if( rc!=0 ){
478 return rc;
479 }
480 pPtrmap[offset] = eType;
481 put4byte(&pPtrmap[offset+1], pgno);
482 }
483
484 sqlite3pager_unref(pPtrmap);
485 return SQLITE_OK;
486}
487
488/*
489** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000490**
491** This routine retrieves the pointer map entry for page 'key', writing
492** the type and parent page number to *pEType and *pPgno respectively.
493** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000494*/
495static int ptrmapGet(Btree *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
496 int iPtrmap; /* Pointer map page index */
497 u8 *pPtrmap; /* Pointer map page data */
498 int offset; /* Offset of entry in pointer map */
499 int rc;
500
drh42cac6d2004-11-20 20:31:11 +0000501 iPtrmap = PTRMAP_PAGENO(pBt->usableSize, key);
danielk1977afcdd022004-10-31 16:25:42 +0000502 rc = sqlite3pager_get(pBt->pPager, iPtrmap, (void **)&pPtrmap);
503 if( rc!=0 ){
504 return rc;
505 }
506
drh42cac6d2004-11-20 20:31:11 +0000507 offset = PTRMAP_PTROFFSET(pBt->usableSize, key);
danielk1977687566d2004-11-02 12:56:41 +0000508 if( pEType ) *pEType = pPtrmap[offset];
509 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000510
511 sqlite3pager_unref(pPtrmap);
512 return SQLITE_OK;
513}
514
515#endif /* SQLITE_OMIT_AUTOVACUUM */
516
drh0d316a42002-08-11 20:10:47 +0000517/*
drh271efa52004-05-30 19:19:05 +0000518** Given a btree page and a cell index (0 means the first cell on
519** the page, 1 means the second cell, and so forth) return a pointer
520** to the cell content.
521**
522** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000523*/
drh43605152004-05-29 21:46:49 +0000524static u8 *findCell(MemPage *pPage, int iCell){
525 u8 *data = pPage->aData;
526 assert( iCell>=0 );
527 assert( iCell<get2byte(&data[pPage->hdrOffset+3]) );
528 return data + get2byte(&data[pPage->cellOffset+2*iCell]);
529}
530
531/*
532** This a more complex version of findCell() that works for
533** pages that do contain overflow cells. See insert
534*/
535static u8 *findOverflowCell(MemPage *pPage, int iCell){
536 int i;
537 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000538 int k;
539 struct _OvflCell *pOvfl;
540 pOvfl = &pPage->aOvfl[i];
541 k = pOvfl->idx;
542 if( k<=iCell ){
543 if( k==iCell ){
544 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000545 }
546 iCell--;
547 }
548 }
549 return findCell(pPage, iCell);
550}
551
552/*
553** Parse a cell content block and fill in the CellInfo structure. There
554** are two versions of this function. parseCell() takes a cell index
555** as the second argument and parseCellPtr() takes a pointer to the
556** body of the cell as its second argument.
557*/
558static void parseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000559 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000560 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000561 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000562){
drh271efa52004-05-30 19:19:05 +0000563 int n; /* Number bytes in cell content header */
564 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000565
566 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000567 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000568 n = pPage->childPtrSize;
569 assert( n==4-4*pPage->leaf );
drh8b18dd42004-05-12 19:18:15 +0000570 if( pPage->hasData ){
drh271efa52004-05-30 19:19:05 +0000571 n += getVarint32(&pCell[n], &nPayload);
drh8b18dd42004-05-12 19:18:15 +0000572 }else{
drh271efa52004-05-30 19:19:05 +0000573 nPayload = 0;
drh3aac2dd2004-04-26 14:10:20 +0000574 }
danielk1977e0d4b062004-06-28 01:11:46 +0000575 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
drh6f11bef2004-05-13 01:12:56 +0000576 pInfo->nHeader = n;
drh271efa52004-05-30 19:19:05 +0000577 pInfo->nData = nPayload;
drh6f11bef2004-05-13 01:12:56 +0000578 if( !pPage->intKey ){
579 nPayload += pInfo->nKey;
580 }
drh271efa52004-05-30 19:19:05 +0000581 if( nPayload<=pPage->maxLocal ){
582 /* This is the (easy) common case where the entire payload fits
583 ** on the local page. No overflow is required.
584 */
585 int nSize; /* Total size of cell content in bytes */
drh6f11bef2004-05-13 01:12:56 +0000586 pInfo->nLocal = nPayload;
587 pInfo->iOverflow = 0;
drh271efa52004-05-30 19:19:05 +0000588 nSize = nPayload + n;
589 if( nSize<4 ){
590 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000591 }
drh271efa52004-05-30 19:19:05 +0000592 pInfo->nSize = nSize;
drh6f11bef2004-05-13 01:12:56 +0000593 }else{
drh271efa52004-05-30 19:19:05 +0000594 /* If the payload will not fit completely on the local page, we have
595 ** to decide how much to store locally and how much to spill onto
596 ** overflow pages. The strategy is to minimize the amount of unused
597 ** space on overflow pages while keeping the amount of local storage
598 ** in between minLocal and maxLocal.
599 **
600 ** Warning: changing the way overflow payload is distributed in any
601 ** way will result in an incompatible file format.
602 */
603 int minLocal; /* Minimum amount of payload held locally */
604 int maxLocal; /* Maximum amount of payload held locally */
605 int surplus; /* Overflow payload available for local storage */
606
607 minLocal = pPage->minLocal;
608 maxLocal = pPage->maxLocal;
609 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000610 if( surplus <= maxLocal ){
611 pInfo->nLocal = surplus;
612 }else{
613 pInfo->nLocal = minLocal;
614 }
615 pInfo->iOverflow = pInfo->nLocal + n;
616 pInfo->nSize = pInfo->iOverflow + 4;
617 }
drh3aac2dd2004-04-26 14:10:20 +0000618}
drh43605152004-05-29 21:46:49 +0000619static void parseCell(
620 MemPage *pPage, /* Page containing the cell */
621 int iCell, /* The cell index. First cell is 0 */
622 CellInfo *pInfo /* Fill in this structure */
623){
624 parseCellPtr(pPage, findCell(pPage, iCell), pInfo);
625}
drh3aac2dd2004-04-26 14:10:20 +0000626
627/*
drh43605152004-05-29 21:46:49 +0000628** Compute the total number of bytes that a Cell needs in the cell
629** data area of the btree-page. The return number includes the cell
630** data header and the local payload, but not any overflow page or
631** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +0000632*/
danielk1977bc6ada42004-06-30 08:20:16 +0000633#ifndef NDEBUG
drh43605152004-05-29 21:46:49 +0000634static int cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +0000635 CellInfo info;
drh43605152004-05-29 21:46:49 +0000636 parseCell(pPage, iCell, &info);
637 return info.nSize;
638}
danielk1977bc6ada42004-06-30 08:20:16 +0000639#endif
drh43605152004-05-29 21:46:49 +0000640static int cellSizePtr(MemPage *pPage, u8 *pCell){
641 CellInfo info;
642 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +0000643 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +0000644}
645
646/*
drhda200cc2004-05-09 11:51:38 +0000647** Do sanity checking on a page. Throw an exception if anything is
648** not right.
649**
650** This routine is used for internal error checking only. It is omitted
651** from most builds.
652*/
653#if defined(BTREE_DEBUG) && !defined(NDEBUG) && 0
654static void _pageIntegrity(MemPage *pPage){
drhb6f41482004-05-14 01:58:11 +0000655 int usableSize;
drhda200cc2004-05-09 11:51:38 +0000656 u8 *data;
drh43605152004-05-29 21:46:49 +0000657 int i, j, idx, c, pc, hdr, nFree;
658 int cellOffset;
659 int nCell, cellLimit;
drh2e38c322004-09-03 18:38:44 +0000660 u8 *used;
drhda200cc2004-05-09 11:51:38 +0000661
drh2e38c322004-09-03 18:38:44 +0000662 used = sqliteMallocRaw( pPage->pBt->pageSize );
663 if( used==0 ) return;
drhb6f41482004-05-14 01:58:11 +0000664 usableSize = pPage->pBt->usableSize;
drh887dc4c2004-10-22 16:22:57 +0000665 assert( pPage->aData==&((unsigned char*)pPage)[-pPage->pBt->psAligned] );
drhda200cc2004-05-09 11:51:38 +0000666 hdr = pPage->hdrOffset;
667 assert( hdr==(pPage->pgno==1 ? 100 : 0) );
668 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
669 c = pPage->aData[hdr];
670 if( pPage->isInit ){
671 assert( pPage->leaf == ((c & PTF_LEAF)!=0) );
672 assert( pPage->zeroData == ((c & PTF_ZERODATA)!=0) );
drh8b18dd42004-05-12 19:18:15 +0000673 assert( pPage->leafData == ((c & PTF_LEAFDATA)!=0) );
674 assert( pPage->intKey == ((c & (PTF_INTKEY|PTF_LEAFDATA))!=0) );
675 assert( pPage->hasData ==
676 !(pPage->zeroData || (!pPage->leaf && pPage->leafData)) );
drh43605152004-05-29 21:46:49 +0000677 assert( pPage->cellOffset==pPage->hdrOffset+12-4*pPage->leaf );
678 assert( pPage->nCell = get2byte(&pPage->aData[hdr+3]) );
drhda200cc2004-05-09 11:51:38 +0000679 }
680 data = pPage->aData;
drhb6f41482004-05-14 01:58:11 +0000681 memset(used, 0, usableSize);
drhda200cc2004-05-09 11:51:38 +0000682 for(i=0; i<hdr+10-pPage->leaf*4; i++) used[i] = 1;
683 nFree = 0;
684 pc = get2byte(&data[hdr+1]);
685 while( pc ){
686 int size;
drhb6f41482004-05-14 01:58:11 +0000687 assert( pc>0 && pc<usableSize-4 );
drhda200cc2004-05-09 11:51:38 +0000688 size = get2byte(&data[pc+2]);
drhb6f41482004-05-14 01:58:11 +0000689 assert( pc+size<=usableSize );
drhda200cc2004-05-09 11:51:38 +0000690 nFree += size;
691 for(i=pc; i<pc+size; i++){
692 assert( used[i]==0 );
693 used[i] = 1;
694 }
695 pc = get2byte(&data[pc]);
696 }
drhda200cc2004-05-09 11:51:38 +0000697 idx = 0;
drh43605152004-05-29 21:46:49 +0000698 nCell = get2byte(&data[hdr+3]);
699 cellLimit = get2byte(&data[hdr+5]);
700 assert( pPage->isInit==0
701 || pPage->nFree==nFree+data[hdr+7]+cellLimit-(cellOffset+2*nCell) );
702 cellOffset = pPage->cellOffset;
703 for(i=0; i<nCell; i++){
drhda200cc2004-05-09 11:51:38 +0000704 int size;
drh43605152004-05-29 21:46:49 +0000705 pc = get2byte(&data[cellOffset+2*i]);
drhb6f41482004-05-14 01:58:11 +0000706 assert( pc>0 && pc<usableSize-4 );
drhda200cc2004-05-09 11:51:38 +0000707 size = cellSize(pPage, &data[pc]);
drhb6f41482004-05-14 01:58:11 +0000708 assert( pc+size<=usableSize );
drh43605152004-05-29 21:46:49 +0000709 for(j=pc; j<pc+size; j++){
710 assert( used[j]==0 );
711 used[j] = 1;
drhda200cc2004-05-09 11:51:38 +0000712 }
drhda200cc2004-05-09 11:51:38 +0000713 }
drh43605152004-05-29 21:46:49 +0000714 for(i=cellOffset+2*nCell; i<cellimit; i++){
715 assert( used[i]==0 );
716 used[i] = 1;
717 }
drhda200cc2004-05-09 11:51:38 +0000718 nFree = 0;
drhb6f41482004-05-14 01:58:11 +0000719 for(i=0; i<usableSize; i++){
drhda200cc2004-05-09 11:51:38 +0000720 assert( used[i]<=1 );
721 if( used[i]==0 ) nFree++;
722 }
drh43605152004-05-29 21:46:49 +0000723 assert( nFree==data[hdr+7] );
drh2e38c322004-09-03 18:38:44 +0000724 sqliteFree(used);
drhda200cc2004-05-09 11:51:38 +0000725}
726#define pageIntegrity(X) _pageIntegrity(X)
727#else
728# define pageIntegrity(X)
729#endif
730
731/*
drh72f82862001-05-24 21:06:34 +0000732** Defragment the page given. All Cells are moved to the
733** beginning of the page and all free space is collected
734** into one big FreeBlk at the end of the page.
drh365d68f2001-05-11 11:02:46 +0000735*/
drh2e38c322004-09-03 18:38:44 +0000736static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +0000737 int i; /* Loop counter */
738 int pc; /* Address of a i-th cell */
739 int addr; /* Offset of first byte after cell pointer array */
740 int hdr; /* Offset to the page header */
741 int size; /* Size of a cell */
742 int usableSize; /* Number of usable bytes on a page */
743 int cellOffset; /* Offset to the cell pointer array */
744 int brk; /* Offset to the cell content area */
745 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +0000746 unsigned char *data; /* The page data */
747 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +0000748
drha34b6762004-05-07 13:30:42 +0000749 assert( sqlite3pager_iswriteable(pPage->aData) );
drh9e572e62004-04-23 23:43:10 +0000750 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000751 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +0000752 assert( pPage->nOverflow==0 );
drh2e38c322004-09-03 18:38:44 +0000753 temp = sqliteMalloc( pPage->pBt->pageSize );
754 if( temp==0 ) return SQLITE_NOMEM;
drh43605152004-05-29 21:46:49 +0000755 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +0000756 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000757 cellOffset = pPage->cellOffset;
758 nCell = pPage->nCell;
759 assert( nCell==get2byte(&data[hdr+3]) );
760 usableSize = pPage->pBt->usableSize;
761 brk = get2byte(&data[hdr+5]);
762 memcpy(&temp[brk], &data[brk], usableSize - brk);
763 brk = usableSize;
764 for(i=0; i<nCell; i++){
765 u8 *pAddr; /* The i-th cell pointer */
766 pAddr = &data[cellOffset + i*2];
767 pc = get2byte(pAddr);
768 assert( pc<pPage->pBt->usableSize );
769 size = cellSizePtr(pPage, &temp[pc]);
770 brk -= size;
771 memcpy(&data[brk], &temp[pc], size);
772 put2byte(pAddr, brk);
drh2af926b2001-05-15 00:39:25 +0000773 }
drh43605152004-05-29 21:46:49 +0000774 assert( brk>=cellOffset+2*nCell );
775 put2byte(&data[hdr+5], brk);
776 data[hdr+1] = 0;
777 data[hdr+2] = 0;
778 data[hdr+7] = 0;
779 addr = cellOffset+2*nCell;
780 memset(&data[addr], 0, brk-addr);
drh2e38c322004-09-03 18:38:44 +0000781 sqliteFree(temp);
782 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +0000783}
784
drha059ad02001-04-17 20:09:11 +0000785/*
drh43605152004-05-29 21:46:49 +0000786** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +0000787**
drh9e572e62004-04-23 23:43:10 +0000788** Return the index into pPage->aData[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000789** the new allocation. Or return 0 if there is not enough free
790** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000791**
drh72f82862001-05-24 21:06:34 +0000792** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000793** nBytes of contiguous free space, then this routine automatically
794** calls defragementPage() to consolidate all free space before
795** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000796*/
drh9e572e62004-04-23 23:43:10 +0000797static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +0000798 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +0000799 int size;
drh24cd67e2004-05-10 16:18:47 +0000800 int nFrag;
drh43605152004-05-29 21:46:49 +0000801 int top;
802 int nCell;
803 int cellOffset;
drh9e572e62004-04-23 23:43:10 +0000804 unsigned char *data;
drh43605152004-05-29 21:46:49 +0000805
drh9e572e62004-04-23 23:43:10 +0000806 data = pPage->aData;
drha34b6762004-05-07 13:30:42 +0000807 assert( sqlite3pager_iswriteable(data) );
drh9e572e62004-04-23 23:43:10 +0000808 assert( pPage->pBt );
809 if( nByte<4 ) nByte = 4;
drh43605152004-05-29 21:46:49 +0000810 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
811 pPage->nFree -= nByte;
drh9e572e62004-04-23 23:43:10 +0000812 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000813
814 nFrag = data[hdr+7];
815 if( nFrag<60 ){
816 /* Search the freelist looking for a slot big enough to satisfy the
817 ** space request. */
818 addr = hdr+1;
819 while( (pc = get2byte(&data[addr]))>0 ){
820 size = get2byte(&data[pc+2]);
821 if( size>=nByte ){
822 if( size<nByte+4 ){
823 memcpy(&data[addr], &data[pc], 2);
824 data[hdr+7] = nFrag + size - nByte;
825 return pc;
826 }else{
827 put2byte(&data[pc+2], size-nByte);
828 return pc + size - nByte;
829 }
830 }
831 addr = pc;
drh9e572e62004-04-23 23:43:10 +0000832 }
833 }
drh43605152004-05-29 21:46:49 +0000834
835 /* Allocate memory from the gap in between the cell pointer array
836 ** and the cell content area.
837 */
838 top = get2byte(&data[hdr+5]);
839 nCell = get2byte(&data[hdr+3]);
840 cellOffset = pPage->cellOffset;
841 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
drh2e38c322004-09-03 18:38:44 +0000842 if( defragmentPage(pPage) ) return 0;
drh43605152004-05-29 21:46:49 +0000843 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +0000844 }
drh43605152004-05-29 21:46:49 +0000845 top -= nByte;
846 assert( cellOffset + 2*nCell <= top );
847 put2byte(&data[hdr+5], top);
848 return top;
drh7e3b0a02001-04-28 16:52:40 +0000849}
850
851/*
drh9e572e62004-04-23 23:43:10 +0000852** Return a section of the pPage->aData to the freelist.
853** The first byte of the new free block is pPage->aDisk[start]
854** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +0000855**
856** Most of the effort here is involved in coalesing adjacent
857** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000858*/
drh9e572e62004-04-23 23:43:10 +0000859static void freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +0000860 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +0000861 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +0000862
drh9e572e62004-04-23 23:43:10 +0000863 assert( pPage->pBt!=0 );
drha34b6762004-05-07 13:30:42 +0000864 assert( sqlite3pager_iswriteable(data) );
drh9e572e62004-04-23 23:43:10 +0000865 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +0000866 assert( (start + size)<=pPage->pBt->usableSize );
drh9e572e62004-04-23 23:43:10 +0000867 if( size<4 ) size = 4;
868
869 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +0000870 hdr = pPage->hdrOffset;
871 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +0000872 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +0000873 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000874 assert( pbegin>addr );
875 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +0000876 }
drhb6f41482004-05-14 01:58:11 +0000877 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000878 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +0000879 put2byte(&data[addr], start);
880 put2byte(&data[start], pbegin);
881 put2byte(&data[start+2], size);
drh2af926b2001-05-15 00:39:25 +0000882 pPage->nFree += size;
drh9e572e62004-04-23 23:43:10 +0000883
884 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +0000885 addr = pPage->hdrOffset + 1;
886 while( (pbegin = get2byte(&data[addr]))>0 ){
drh9e572e62004-04-23 23:43:10 +0000887 int pnext, psize;
drh3aac2dd2004-04-26 14:10:20 +0000888 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +0000889 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +0000890 pnext = get2byte(&data[pbegin]);
891 psize = get2byte(&data[pbegin+2]);
892 if( pbegin + psize + 3 >= pnext && pnext>0 ){
893 int frag = pnext - (pbegin+psize);
drh43605152004-05-29 21:46:49 +0000894 assert( frag<=data[pPage->hdrOffset+7] );
895 data[pPage->hdrOffset+7] -= frag;
drh9e572e62004-04-23 23:43:10 +0000896 put2byte(&data[pbegin], get2byte(&data[pnext]));
897 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
898 }else{
drh3aac2dd2004-04-26 14:10:20 +0000899 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +0000900 }
901 }
drh7e3b0a02001-04-28 16:52:40 +0000902
drh43605152004-05-29 21:46:49 +0000903 /* If the cell content area begins with a freeblock, remove it. */
904 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
905 int top;
906 pbegin = get2byte(&data[hdr+1]);
907 memcpy(&data[hdr+1], &data[pbegin], 2);
908 top = get2byte(&data[hdr+5]);
909 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
drh4b70f112004-05-02 21:12:19 +0000910 }
drh4b70f112004-05-02 21:12:19 +0000911}
912
913/*
drh271efa52004-05-30 19:19:05 +0000914** Decode the flags byte (the first byte of the header) for a page
915** and initialize fields of the MemPage structure accordingly.
916*/
917static void decodeFlags(MemPage *pPage, int flagByte){
918 Btree *pBt; /* A copy of pPage->pBt */
919
920 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
921 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
922 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
923 pPage->leaf = (flagByte & PTF_LEAF)!=0;
924 pPage->childPtrSize = 4*(pPage->leaf==0);
925 pBt = pPage->pBt;
926 if( flagByte & PTF_LEAFDATA ){
927 pPage->leafData = 1;
928 pPage->maxLocal = pBt->maxLeaf;
929 pPage->minLocal = pBt->minLeaf;
930 }else{
931 pPage->leafData = 0;
932 pPage->maxLocal = pBt->maxLocal;
933 pPage->minLocal = pBt->minLocal;
934 }
935 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
936}
937
938/*
drh7e3b0a02001-04-28 16:52:40 +0000939** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000940**
drhbd03cae2001-06-02 02:40:57 +0000941** The pParent parameter must be a pointer to the MemPage which
drh9e572e62004-04-23 23:43:10 +0000942** is the parent of the page being initialized. The root of a
943** BTree has no parent and so for that page, pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000944**
drh72f82862001-05-24 21:06:34 +0000945** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +0000946** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +0000947** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
948** guarantee that the page is well-formed. It only shows that
949** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000950*/
drh9e572e62004-04-23 23:43:10 +0000951static int initPage(
drh3aac2dd2004-04-26 14:10:20 +0000952 MemPage *pPage, /* The page to be initialized */
drh9e572e62004-04-23 23:43:10 +0000953 MemPage *pParent /* The parent. Might be NULL */
954){
drh271efa52004-05-30 19:19:05 +0000955 int pc; /* Address of a freeblock within pPage->aData[] */
956 int i; /* Loop counter */
957 int hdr; /* Offset to beginning of page header */
958 u8 *data; /* Equal to pPage->aData */
drh2e38c322004-09-03 18:38:44 +0000959 Btree *pBt; /* The main btree structure */
drh271efa52004-05-30 19:19:05 +0000960 int usableSize; /* Amount of usable space on each page */
961 int cellOffset; /* Offset from start of page to first cell pointer */
962 int nFree; /* Number of unused bytes on the page */
963 int top; /* First byte of the cell content area */
drh2af926b2001-05-15 00:39:25 +0000964
drh2e38c322004-09-03 18:38:44 +0000965 pBt = pPage->pBt;
966 assert( pBt!=0 );
967 assert( pParent==0 || pParent->pBt==pBt );
drha34b6762004-05-07 13:30:42 +0000968 assert( pPage->pgno==sqlite3pager_pagenumber(pPage->aData) );
drh887dc4c2004-10-22 16:22:57 +0000969 assert( pPage->aData == &((unsigned char*)pPage)[-pBt->psAligned] );
drhee696e22004-08-30 16:52:17 +0000970 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
971 /* The parent page should never change unless the file is corrupt */
972 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
973 }
drh10617cd2004-05-14 15:27:27 +0000974 if( pPage->isInit ) return SQLITE_OK;
drhda200cc2004-05-09 11:51:38 +0000975 if( pPage->pParent==0 && pParent!=0 ){
976 pPage->pParent = pParent;
drha34b6762004-05-07 13:30:42 +0000977 sqlite3pager_ref(pParent->aData);
drh5e2f8b92001-05-28 00:41:15 +0000978 }
drhde647132004-05-07 17:57:49 +0000979 hdr = pPage->hdrOffset;
drha34b6762004-05-07 13:30:42 +0000980 data = pPage->aData;
drh271efa52004-05-30 19:19:05 +0000981 decodeFlags(pPage, data[hdr]);
drh43605152004-05-29 21:46:49 +0000982 pPage->nOverflow = 0;
drhc8629a12004-05-08 20:07:40 +0000983 pPage->idxShift = 0;
drh2e38c322004-09-03 18:38:44 +0000984 usableSize = pBt->usableSize;
drh43605152004-05-29 21:46:49 +0000985 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
986 top = get2byte(&data[hdr+5]);
987 pPage->nCell = get2byte(&data[hdr+3]);
drh2e38c322004-09-03 18:38:44 +0000988 if( pPage->nCell>MX_CELL(pBt) ){
drhee696e22004-08-30 16:52:17 +0000989 /* To many cells for a single page. The page must be corrupt */
990 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
991 }
992 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
993 /* All pages must have at least one cell, except for root pages */
994 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
995 }
drh9e572e62004-04-23 23:43:10 +0000996
997 /* Compute the total free space on the page */
drh9e572e62004-04-23 23:43:10 +0000998 pc = get2byte(&data[hdr+1]);
drh43605152004-05-29 21:46:49 +0000999 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
drh3add3672004-05-15 00:29:24 +00001000 i = 0;
drh9e572e62004-04-23 23:43:10 +00001001 while( pc>0 ){
1002 int next, size;
drhee696e22004-08-30 16:52:17 +00001003 if( pc>usableSize-4 ){
1004 /* Free block is off the page */
1005 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1006 }
1007 if( i++>SQLITE_MAX_PAGE_SIZE/4 ){
1008 /* The free block list forms an infinite loop */
1009 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1010 }
drh9e572e62004-04-23 23:43:10 +00001011 next = get2byte(&data[pc]);
1012 size = get2byte(&data[pc+2]);
drhee696e22004-08-30 16:52:17 +00001013 if( next>0 && next<=pc+size+3 ){
1014 /* Free blocks must be in accending order */
1015 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1016 }
drh3add3672004-05-15 00:29:24 +00001017 nFree += size;
drh9e572e62004-04-23 23:43:10 +00001018 pc = next;
1019 }
drh3add3672004-05-15 00:29:24 +00001020 pPage->nFree = nFree;
drhee696e22004-08-30 16:52:17 +00001021 if( nFree>=usableSize ){
1022 /* Free space cannot exceed total page size */
1023 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1024 }
drh9e572e62004-04-23 23:43:10 +00001025
drhde647132004-05-07 17:57:49 +00001026 pPage->isInit = 1;
drhda200cc2004-05-09 11:51:38 +00001027 pageIntegrity(pPage);
drh9e572e62004-04-23 23:43:10 +00001028 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001029}
1030
1031/*
drh8b2f49b2001-06-08 00:21:52 +00001032** Set up a raw page so that it looks like a database page holding
1033** no entries.
drhbd03cae2001-06-02 02:40:57 +00001034*/
drh9e572e62004-04-23 23:43:10 +00001035static void zeroPage(MemPage *pPage, int flags){
1036 unsigned char *data = pPage->aData;
1037 Btree *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00001038 int hdr = pPage->hdrOffset;
drh9e572e62004-04-23 23:43:10 +00001039 int first;
1040
drhda200cc2004-05-09 11:51:38 +00001041 assert( sqlite3pager_pagenumber(data)==pPage->pgno );
drh887dc4c2004-10-22 16:22:57 +00001042 assert( &data[pBt->psAligned] == (unsigned char*)pPage );
drha34b6762004-05-07 13:30:42 +00001043 assert( sqlite3pager_iswriteable(data) );
drhb6f41482004-05-14 01:58:11 +00001044 memset(&data[hdr], 0, pBt->usableSize - hdr);
drh9e572e62004-04-23 23:43:10 +00001045 data[hdr] = flags;
drh43605152004-05-29 21:46:49 +00001046 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
1047 memset(&data[hdr+1], 0, 4);
1048 data[hdr+7] = 0;
1049 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001050 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001051 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001052 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001053 pPage->cellOffset = first;
1054 pPage->nOverflow = 0;
drhda200cc2004-05-09 11:51:38 +00001055 pPage->idxShift = 0;
drh43605152004-05-29 21:46:49 +00001056 pPage->nCell = 0;
drhda200cc2004-05-09 11:51:38 +00001057 pPage->isInit = 1;
1058 pageIntegrity(pPage);
drhbd03cae2001-06-02 02:40:57 +00001059}
1060
1061/*
drh3aac2dd2004-04-26 14:10:20 +00001062** Get a page from the pager. Initialize the MemPage.pBt and
1063** MemPage.aData elements if needed.
1064*/
1065static int getPage(Btree *pBt, Pgno pgno, MemPage **ppPage){
1066 int rc;
1067 unsigned char *aData;
1068 MemPage *pPage;
drha34b6762004-05-07 13:30:42 +00001069 rc = sqlite3pager_get(pBt->pPager, pgno, (void**)&aData);
drh3aac2dd2004-04-26 14:10:20 +00001070 if( rc ) return rc;
drh887dc4c2004-10-22 16:22:57 +00001071 pPage = (MemPage*)&aData[pBt->psAligned];
drh3aac2dd2004-04-26 14:10:20 +00001072 pPage->aData = aData;
1073 pPage->pBt = pBt;
1074 pPage->pgno = pgno;
drhde647132004-05-07 17:57:49 +00001075 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
drh3aac2dd2004-04-26 14:10:20 +00001076 *ppPage = pPage;
1077 return SQLITE_OK;
1078}
1079
1080/*
drhde647132004-05-07 17:57:49 +00001081** Get a page from the pager and initialize it. This routine
1082** is just a convenience wrapper around separate calls to
1083** getPage() and initPage().
1084*/
1085static int getAndInitPage(
1086 Btree *pBt, /* The database file */
1087 Pgno pgno, /* Number of the page to get */
1088 MemPage **ppPage, /* Write the page pointer here */
1089 MemPage *pParent /* Parent of the page */
1090){
1091 int rc;
drhee696e22004-08-30 16:52:17 +00001092 if( pgno==0 ){
1093 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
1094 }
drhde647132004-05-07 17:57:49 +00001095 rc = getPage(pBt, pgno, ppPage);
drh10617cd2004-05-14 15:27:27 +00001096 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
drhde647132004-05-07 17:57:49 +00001097 rc = initPage(*ppPage, pParent);
1098 }
1099 return rc;
1100}
1101
1102/*
drh3aac2dd2004-04-26 14:10:20 +00001103** Release a MemPage. This should be called once for each prior
1104** call to getPage.
1105*/
drh4b70f112004-05-02 21:12:19 +00001106static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001107 if( pPage ){
1108 assert( pPage->aData );
1109 assert( pPage->pBt );
drh887dc4c2004-10-22 16:22:57 +00001110 assert( &pPage->aData[pPage->pBt->psAligned]==(unsigned char*)pPage );
drha34b6762004-05-07 13:30:42 +00001111 sqlite3pager_unref(pPage->aData);
drh3aac2dd2004-04-26 14:10:20 +00001112 }
1113}
1114
1115/*
drh72f82862001-05-24 21:06:34 +00001116** This routine is called when the reference count for a page
1117** reaches zero. We need to unref the pParent pointer when that
1118** happens.
1119*/
drhb6f41482004-05-14 01:58:11 +00001120static void pageDestructor(void *pData, int pageSize){
drh887dc4c2004-10-22 16:22:57 +00001121 MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
drh72f82862001-05-24 21:06:34 +00001122 if( pPage->pParent ){
1123 MemPage *pParent = pPage->pParent;
1124 pPage->pParent = 0;
drha34b6762004-05-07 13:30:42 +00001125 releasePage(pParent);
drh72f82862001-05-24 21:06:34 +00001126 }
drh3aac2dd2004-04-26 14:10:20 +00001127 pPage->isInit = 0;
drh72f82862001-05-24 21:06:34 +00001128}
1129
1130/*
drha6abd042004-06-09 17:37:22 +00001131** During a rollback, when the pager reloads information into the cache
1132** so that the cache is restored to its original state at the start of
1133** the transaction, for each page restored this routine is called.
1134**
1135** This routine needs to reset the extra data section at the end of the
1136** page to agree with the restored data.
1137*/
1138static void pageReinit(void *pData, int pageSize){
drh887dc4c2004-10-22 16:22:57 +00001139 MemPage *pPage = (MemPage*)&((char*)pData)[FORCE_ALIGNMENT(pageSize)];
drha6abd042004-06-09 17:37:22 +00001140 if( pPage->isInit ){
1141 pPage->isInit = 0;
1142 initPage(pPage, pPage->pParent);
1143 }
1144}
1145
1146/*
drhad3e0102004-09-03 23:32:18 +00001147** Open a database file.
1148**
drh382c0242001-10-06 16:33:02 +00001149** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001150** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001151** database file will be deleted when sqlite3BtreeClose() is called.
drha059ad02001-04-17 20:09:11 +00001152*/
drh23e11ca2004-05-04 17:27:28 +00001153int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001154 const char *zFilename, /* Name of the file containing the BTree database */
1155 Btree **ppBtree, /* Pointer to new Btree object written here */
drh90f5ecb2004-07-22 01:19:35 +00001156 int flags /* Options */
drh6019e162001-07-02 17:51:45 +00001157){
drha059ad02001-04-17 20:09:11 +00001158 Btree *pBt;
drha34b6762004-05-07 13:30:42 +00001159 int rc;
drh90f5ecb2004-07-22 01:19:35 +00001160 int nReserve;
1161 unsigned char zDbHeader[100];
drha059ad02001-04-17 20:09:11 +00001162
drhd62d3d02003-01-24 12:14:20 +00001163 /*
1164 ** The following asserts make sure that structures used by the btree are
1165 ** the right size. This is to guard against size changes that result
1166 ** when compiling on a different architecture.
1167 */
drh4a1c3802004-05-12 15:15:47 +00001168 assert( sizeof(i64)==8 );
drh9e572e62004-04-23 23:43:10 +00001169 assert( sizeof(u64)==8 );
drhd62d3d02003-01-24 12:14:20 +00001170 assert( sizeof(u32)==4 );
1171 assert( sizeof(u16)==2 );
1172 assert( sizeof(Pgno)==4 );
drhd62d3d02003-01-24 12:14:20 +00001173 assert( sizeof(ptr)==sizeof(char*) );
1174 assert( sizeof(uptr)==sizeof(ptr) );
1175
drha059ad02001-04-17 20:09:11 +00001176 pBt = sqliteMalloc( sizeof(*pBt) );
1177 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +00001178 *ppBtree = 0;
drha059ad02001-04-17 20:09:11 +00001179 return SQLITE_NOMEM;
1180 }
drh90f5ecb2004-07-22 01:19:35 +00001181 rc = sqlite3pager_open(&pBt->pPager, zFilename, EXTRA_SIZE,
1182 (flags & BTREE_OMIT_JOURNAL)==0);
drha059ad02001-04-17 20:09:11 +00001183 if( rc!=SQLITE_OK ){
drha34b6762004-05-07 13:30:42 +00001184 if( pBt->pPager ) sqlite3pager_close(pBt->pPager);
drha059ad02001-04-17 20:09:11 +00001185 sqliteFree(pBt);
1186 *ppBtree = 0;
1187 return rc;
1188 }
drha34b6762004-05-07 13:30:42 +00001189 sqlite3pager_set_destructor(pBt->pPager, pageDestructor);
drha6abd042004-06-09 17:37:22 +00001190 sqlite3pager_set_reiniter(pBt->pPager, pageReinit);
drha059ad02001-04-17 20:09:11 +00001191 pBt->pCursor = 0;
drha34b6762004-05-07 13:30:42 +00001192 pBt->pPage1 = 0;
1193 pBt->readOnly = sqlite3pager_isreadonly(pBt->pPager);
drh90f5ecb2004-07-22 01:19:35 +00001194 sqlite3pager_read_fileheader(pBt->pPager, sizeof(zDbHeader), zDbHeader);
1195 pBt->pageSize = get2byte(&zDbHeader[16]);
1196 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE ){
1197 pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE;
1198 pBt->maxEmbedFrac = 64; /* 25% */
1199 pBt->minEmbedFrac = 32; /* 12.5% */
1200 pBt->minLeafFrac = 32; /* 12.5% */
drheee46cf2004-11-06 00:02:48 +00001201#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197703aded42004-11-22 05:26:27 +00001202 /* If the magic name ":memory:" will create an in-memory database, then
1203 ** do not set the auto-vacuum flag, even if SQLITE_DEFAULT_AUTOVACUUM
1204 ** is true. On the other hand, if SQLITE_OMIT_MEMORYDB has been defined,
1205 ** then ":memory:" is just a regular file-name. Respect the auto-vacuum
1206 ** default in this case.
1207 */
1208#ifndef SQLITE_OMIT_MEMORYDB
danielk1977951af802004-11-05 15:45:09 +00001209 if( zFilename && strcmp(zFilename,":memory:") ){
danielk197703aded42004-11-22 05:26:27 +00001210#else
1211 if( zFilename ){
1212#endif
danielk1977951af802004-11-05 15:45:09 +00001213 pBt->autoVacuum = SQLITE_DEFAULT_AUTOVACUUM;
1214 }
drheee46cf2004-11-06 00:02:48 +00001215#endif
drh90f5ecb2004-07-22 01:19:35 +00001216 nReserve = 0;
1217 }else{
1218 nReserve = zDbHeader[20];
1219 pBt->maxEmbedFrac = zDbHeader[21];
1220 pBt->minEmbedFrac = zDbHeader[22];
1221 pBt->minLeafFrac = zDbHeader[23];
1222 pBt->pageSizeFixed = 1;
danielk1977951af802004-11-05 15:45:09 +00001223#ifndef SQLITE_OMIT_AUTOVACUUM
1224 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1225#endif
drh90f5ecb2004-07-22 01:19:35 +00001226 }
1227 pBt->usableSize = pBt->pageSize - nReserve;
drh887dc4c2004-10-22 16:22:57 +00001228 pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001229 sqlite3pager_set_pagesize(pBt->pPager, pBt->pageSize);
drha059ad02001-04-17 20:09:11 +00001230 *ppBtree = pBt;
1231 return SQLITE_OK;
1232}
1233
1234/*
1235** Close an open database and invalidate all cursors.
1236*/
drh3aac2dd2004-04-26 14:10:20 +00001237int sqlite3BtreeClose(Btree *pBt){
drha059ad02001-04-17 20:09:11 +00001238 while( pBt->pCursor ){
drh3aac2dd2004-04-26 14:10:20 +00001239 sqlite3BtreeCloseCursor(pBt->pCursor);
drha059ad02001-04-17 20:09:11 +00001240 }
drha34b6762004-05-07 13:30:42 +00001241 sqlite3pager_close(pBt->pPager);
drha059ad02001-04-17 20:09:11 +00001242 sqliteFree(pBt);
1243 return SQLITE_OK;
1244}
1245
1246/*
drh90f5ecb2004-07-22 01:19:35 +00001247** Change the busy handler callback function.
1248*/
1249int sqlite3BtreeSetBusyHandler(Btree *pBt, BusyHandler *pHandler){
1250 sqlite3pager_set_busyhandler(pBt->pPager, pHandler);
1251 return SQLITE_OK;
1252}
1253
1254/*
drhda47d772002-12-02 04:25:19 +00001255** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001256**
1257** The maximum number of cache pages is set to the absolute
1258** value of mxPage. If mxPage is negative, the pager will
1259** operate asynchronously - it will not stop to do fsync()s
1260** to insure data is written to the disk surface before
1261** continuing. Transactions still work if synchronous is off,
1262** and the database cannot be corrupted if this program
1263** crashes. But if the operating system crashes or there is
1264** an abrupt power failure when synchronous is off, the database
1265** could be left in an inconsistent and unrecoverable state.
1266** Synchronous is on by default so database corruption is not
1267** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001268*/
drh23e11ca2004-05-04 17:27:28 +00001269int sqlite3BtreeSetCacheSize(Btree *pBt, int mxPage){
drha34b6762004-05-07 13:30:42 +00001270 sqlite3pager_set_cachesize(pBt->pPager, mxPage);
drhf57b14a2001-09-14 18:54:08 +00001271 return SQLITE_OK;
1272}
1273
1274/*
drh973b6e32003-02-12 14:09:42 +00001275** Change the way data is synced to disk in order to increase or decrease
1276** how well the database resists damage due to OS crashes and power
1277** failures. Level 1 is the same as asynchronous (no syncs() occur and
1278** there is a high probability of damage) Level 2 is the default. There
1279** is a very low but non-zero probability of damage. Level 3 reduces the
1280** probability of damage to near zero but with a write performance reduction.
1281*/
drh3aac2dd2004-04-26 14:10:20 +00001282int sqlite3BtreeSetSafetyLevel(Btree *pBt, int level){
drha34b6762004-05-07 13:30:42 +00001283 sqlite3pager_set_safety_level(pBt->pPager, level);
drh973b6e32003-02-12 14:09:42 +00001284 return SQLITE_OK;
1285}
1286
1287/*
drh90f5ecb2004-07-22 01:19:35 +00001288** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001289**
1290** The page size must be a power of 2 between 512 and 65536. If the page
1291** size supplied does not meet this constraint then the page size is not
1292** changed.
1293**
1294** Page sizes are constrained to be a power of two so that the region
1295** of the database file used for locking (beginning at PENDING_BYTE,
1296** the first byte past the 1GB boundary, 0x40000000) needs to occur
1297** at the beginning of a page.
drh90f5ecb2004-07-22 01:19:35 +00001298*/
1299int sqlite3BtreeSetPageSize(Btree *pBt, int pageSize, int nReserve){
1300 if( pBt->pageSizeFixed ){
1301 return SQLITE_READONLY;
1302 }
1303 if( nReserve<0 ){
1304 nReserve = pBt->pageSize - pBt->usableSize;
1305 }
drh06f50212004-11-02 14:24:33 +00001306 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1307 ((pageSize-1)&pageSize)==0 ){
drh90f5ecb2004-07-22 01:19:35 +00001308 pBt->pageSize = pageSize;
drh887dc4c2004-10-22 16:22:57 +00001309 pBt->psAligned = FORCE_ALIGNMENT(pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001310 sqlite3pager_set_pagesize(pBt->pPager, pageSize);
1311 }
1312 pBt->usableSize = pBt->pageSize - nReserve;
1313 return SQLITE_OK;
1314}
1315
1316/*
1317** Return the currently defined page size
1318*/
1319int sqlite3BtreeGetPageSize(Btree *pBt){
1320 return pBt->pageSize;
1321}
drh2011d5f2004-07-22 02:40:37 +00001322int sqlite3BtreeGetReserve(Btree *pBt){
1323 return pBt->pageSize - pBt->usableSize;
1324}
drh90f5ecb2004-07-22 01:19:35 +00001325
1326/*
danielk1977951af802004-11-05 15:45:09 +00001327** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1328** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1329** is disabled. The default value for the auto-vacuum property is
1330** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1331*/
1332int sqlite3BtreeSetAutoVacuum(Btree *pBt, int autoVacuum){
1333#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001334 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001335#else
1336 if( pBt->pageSizeFixed ){
1337 return SQLITE_READONLY;
1338 }
1339 pBt->autoVacuum = (autoVacuum?1:0);
1340 return SQLITE_OK;
1341#endif
1342}
1343
1344/*
1345** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1346** enabled 1 is returned. Otherwise 0.
1347*/
1348int sqlite3BtreeGetAutoVacuum(Btree *pBt){
1349#ifdef SQLITE_OMIT_AUTOVACUUM
1350 return 0;
1351#else
1352 return pBt->autoVacuum;
1353#endif
1354}
1355
1356
1357/*
drha34b6762004-05-07 13:30:42 +00001358** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001359** also acquire a readlock on that file.
1360**
1361** SQLITE_OK is returned on success. If the file is not a
1362** well-formed database file, then SQLITE_CORRUPT is returned.
1363** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
1364** is returned if we run out of memory. SQLITE_PROTOCOL is returned
1365** if there is a locking protocol violation.
1366*/
1367static int lockBtree(Btree *pBt){
1368 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001369 MemPage *pPage1;
drha34b6762004-05-07 13:30:42 +00001370 if( pBt->pPage1 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001371 rc = getPage(pBt, 1, &pPage1);
drh306dc212001-05-21 13:45:10 +00001372 if( rc!=SQLITE_OK ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00001373
drh306dc212001-05-21 13:45:10 +00001374
1375 /* Do some checking to help insure the file we opened really is
1376 ** a valid database file.
1377 */
drhb6f41482004-05-14 01:58:11 +00001378 rc = SQLITE_NOTADB;
drha34b6762004-05-07 13:30:42 +00001379 if( sqlite3pager_pagecount(pBt->pPager)>0 ){
drhb6f41482004-05-14 01:58:11 +00001380 u8 *page1 = pPage1->aData;
1381 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001382 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001383 }
drhb6f41482004-05-14 01:58:11 +00001384 if( page1[18]>1 || page1[19]>1 ){
1385 goto page1_init_failed;
1386 }
1387 pBt->pageSize = get2byte(&page1[16]);
1388 pBt->usableSize = pBt->pageSize - page1[20];
1389 if( pBt->usableSize<500 ){
1390 goto page1_init_failed;
1391 }
drh887dc4c2004-10-22 16:22:57 +00001392 pBt->psAligned = FORCE_ALIGNMENT(pBt->pageSize);
drhb6f41482004-05-14 01:58:11 +00001393 pBt->maxEmbedFrac = page1[21];
1394 pBt->minEmbedFrac = page1[22];
1395 pBt->minLeafFrac = page1[23];
drh306dc212001-05-21 13:45:10 +00001396 }
drhb6f41482004-05-14 01:58:11 +00001397
1398 /* maxLocal is the maximum amount of payload to store locally for
1399 ** a cell. Make sure it is small enough so that at least minFanout
1400 ** cells can will fit on one page. We assume a 10-byte page header.
1401 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001402 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001403 ** 4-byte child pointer
1404 ** 9-byte nKey value
1405 ** 4-byte nData value
1406 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001407 ** So a cell consists of a 2-byte poiner, a header which is as much as
1408 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1409 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001410 */
drh43605152004-05-29 21:46:49 +00001411 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1412 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1413 pBt->maxLeaf = pBt->usableSize - 35;
1414 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
drhb6f41482004-05-14 01:58:11 +00001415 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1416 goto page1_init_failed;
1417 }
drh2e38c322004-09-03 18:38:44 +00001418 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001419 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001420 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001421
drh72f82862001-05-24 21:06:34 +00001422page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001423 releasePage(pPage1);
1424 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001425 return rc;
drh306dc212001-05-21 13:45:10 +00001426}
1427
1428/*
drhb8ca3072001-12-05 00:21:20 +00001429** If there are no outstanding cursors and we are not in the middle
1430** of a transaction but there is a read lock on the database, then
1431** this routine unrefs the first page of the database file which
1432** has the effect of releasing the read lock.
1433**
1434** If there are any outstanding cursors, this routine is a no-op.
1435**
1436** If there is a transaction in progress, this routine is a no-op.
1437*/
1438static void unlockBtreeIfUnused(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00001439 if( pBt->inTrans==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
drh51c6d962004-06-06 00:42:25 +00001440 if( pBt->pPage1->aData==0 ){
1441 MemPage *pPage = pBt->pPage1;
drh887dc4c2004-10-22 16:22:57 +00001442 pPage->aData = &((char*)pPage)[-pBt->psAligned];
drh51c6d962004-06-06 00:42:25 +00001443 pPage->pBt = pBt;
1444 pPage->pgno = 1;
1445 }
drh3aac2dd2004-04-26 14:10:20 +00001446 releasePage(pBt->pPage1);
1447 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001448 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001449 }
1450}
1451
1452/*
drh9e572e62004-04-23 23:43:10 +00001453** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001454** file.
drh8b2f49b2001-06-08 00:21:52 +00001455*/
1456static int newDatabase(Btree *pBt){
drh9e572e62004-04-23 23:43:10 +00001457 MemPage *pP1;
1458 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001459 int rc;
drhde647132004-05-07 17:57:49 +00001460 if( sqlite3pager_pagecount(pBt->pPager)>0 ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00001461 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001462 assert( pP1!=0 );
1463 data = pP1->aData;
drha34b6762004-05-07 13:30:42 +00001464 rc = sqlite3pager_write(data);
drh8b2f49b2001-06-08 00:21:52 +00001465 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001466 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1467 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001468 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001469 data[18] = 1;
1470 data[19] = 1;
drhb6f41482004-05-14 01:58:11 +00001471 data[20] = pBt->pageSize - pBt->usableSize;
1472 data[21] = pBt->maxEmbedFrac;
1473 data[22] = pBt->minEmbedFrac;
1474 data[23] = pBt->minLeafFrac;
1475 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00001476 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00001477 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00001478#ifndef SQLITE_OMIT_AUTOVACUUM
1479 if( pBt->autoVacuum ){
1480 put4byte(&data[36 + 4*4], 1);
1481 }
1482#endif
drh8b2f49b2001-06-08 00:21:52 +00001483 return SQLITE_OK;
1484}
1485
1486/*
danielk1977ee5741e2004-05-31 10:01:34 +00001487** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00001488** is started if the second argument is nonzero, otherwise a read-
1489** transaction. If the second argument is 2 or more and exclusive
1490** transaction is started, meaning that no other process is allowed
1491** to access the database. A preexisting transaction may not be
1492** upgrade to exclusive by calling this routine a second time - the
1493** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00001494**
danielk1977ee5741e2004-05-31 10:01:34 +00001495** A write-transaction must be started before attempting any
1496** changes to the database. None of the following routines
1497** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00001498**
drh23e11ca2004-05-04 17:27:28 +00001499** sqlite3BtreeCreateTable()
1500** sqlite3BtreeCreateIndex()
1501** sqlite3BtreeClearTable()
1502** sqlite3BtreeDropTable()
1503** sqlite3BtreeInsert()
1504** sqlite3BtreeDelete()
1505** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00001506**
1507** If wrflag is true, then nMaster specifies the maximum length of
1508** a master journal file name supplied later via sqlite3BtreeSync().
1509** This is so that appropriate space can be allocated in the journal file
1510** when it is created..
drha059ad02001-04-17 20:09:11 +00001511*/
danielk197740b38dc2004-06-26 08:38:24 +00001512int sqlite3BtreeBeginTrans(Btree *pBt, int wrflag){
danielk1977ee5741e2004-05-31 10:01:34 +00001513 int rc = SQLITE_OK;
1514
1515 /* If the btree is already in a write-transaction, or it
1516 ** is already in a read-transaction and a read-transaction
1517 ** is requested, this is a no-op.
1518 */
1519 if( pBt->inTrans==TRANS_WRITE ||
1520 (pBt->inTrans==TRANS_READ && !wrflag) ){
1521 return SQLITE_OK;
1522 }
1523 if( pBt->readOnly && wrflag ){
1524 return SQLITE_READONLY;
1525 }
1526
drh3aac2dd2004-04-26 14:10:20 +00001527 if( pBt->pPage1==0 ){
drh7e3b0a02001-04-28 16:52:40 +00001528 rc = lockBtree(pBt);
danielk1977ee5741e2004-05-31 10:01:34 +00001529 }
1530
1531 if( rc==SQLITE_OK && wrflag ){
drh684917c2004-10-05 02:41:42 +00001532 rc = sqlite3pager_begin(pBt->pPage1->aData, wrflag>1);
danielk1977ee5741e2004-05-31 10:01:34 +00001533 if( rc==SQLITE_OK ){
1534 rc = newDatabase(pBt);
drh8c42ca92001-06-22 19:15:00 +00001535 }
drha059ad02001-04-17 20:09:11 +00001536 }
danielk1977ee5741e2004-05-31 10:01:34 +00001537
drhf74b8d92002-09-01 23:20:45 +00001538 if( rc==SQLITE_OK ){
danielk1977ee5741e2004-05-31 10:01:34 +00001539 pBt->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
1540 if( wrflag ) pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001541 }else{
1542 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001543 }
drhb8ca3072001-12-05 00:21:20 +00001544 return rc;
drha059ad02001-04-17 20:09:11 +00001545}
1546
1547/*
danielk1977687566d2004-11-02 12:56:41 +00001548** The TRACE macro will print high-level status information about the
1549** btree operation when the global variable sqlite3_btree_trace is
1550** enabled.
1551*/
1552#if SQLITE_TEST
1553# define TRACE(X) if( sqlite3_btree_trace )\
1554 { sqlite3DebugPrintf X; fflush(stdout); }
1555#else
1556# define TRACE(X)
1557#endif
1558int sqlite3_btree_trace=0; /* True to enable tracing */
1559
1560#ifndef SQLITE_OMIT_AUTOVACUUM
1561
1562/*
1563** Set the pointer-map entries for all children of page pPage. Also, if
1564** pPage contains cells that point to overflow pages, set the pointer
1565** map entries for the overflow pages as well.
1566*/
1567static int setChildPtrmaps(MemPage *pPage){
1568 int i; /* Counter variable */
1569 int nCell; /* Number of cells in page pPage */
1570 int rc = SQLITE_OK; /* Return code */
1571 Btree *pBt = pPage->pBt;
1572 int isInitOrig = pPage->isInit;
1573 Pgno pgno = pPage->pgno;
1574
1575 initPage(pPage, 0);
1576 nCell = pPage->nCell;
1577
1578 for(i=0; i<nCell; i++){
1579 CellInfo info;
1580 u8 *pCell = findCell(pPage, i);
1581
1582 parseCellPtr(pPage, pCell, &info);
1583 if( info.iOverflow ){
1584 Pgno ovflPgno = get4byte(&pCell[info.iOverflow]);
1585 rc = ptrmapPut(pBt, ovflPgno, PTRMAP_OVERFLOW1, pgno);
1586 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1587 }
1588 if( !pPage->leaf ){
1589 Pgno childPgno = get4byte(pCell);
1590 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1591 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1592 }
1593 }
1594
1595 if( !pPage->leaf ){
1596 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
1597 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1598 }
1599
1600set_child_ptrmaps_out:
1601 pPage->isInit = isInitOrig;
1602 return rc;
1603}
1604
1605/*
1606** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
1607** page, is a pointer to page iFrom. Modify this pointer so that it points to
1608** iTo. Parameter eType describes the type of pointer to be modified, as
1609** follows:
1610**
1611** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
1612** page of pPage.
1613**
1614** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
1615** page pointed to by one of the cells on pPage.
1616**
1617** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
1618** overflow page in the list.
1619*/
1620static void modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
1621
1622 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00001623 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977687566d2004-11-02 12:56:41 +00001624 assert( get4byte(pPage->aData)==iFrom );
danielk1977f78fc082004-11-02 14:40:32 +00001625 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00001626 }else{
1627 int isInitOrig = pPage->isInit;
1628 int i;
1629 int nCell;
1630
1631 initPage(pPage, 0);
1632 nCell = pPage->nCell;
1633
danielk1977687566d2004-11-02 12:56:41 +00001634 for(i=0; i<nCell; i++){
1635 u8 *pCell = findCell(pPage, i);
1636 if( eType==PTRMAP_OVERFLOW1 ){
1637 CellInfo info;
1638 parseCellPtr(pPage, pCell, &info);
1639 if( info.iOverflow ){
1640 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
1641 put4byte(&pCell[info.iOverflow], iTo);
1642 break;
1643 }
1644 }
1645 }else{
1646 if( get4byte(pCell)==iFrom ){
1647 put4byte(pCell, iTo);
1648 break;
1649 }
1650 }
1651 }
1652
1653 if( i==nCell ){
1654 assert( eType==PTRMAP_BTREE );
1655 assert( get4byte(&pPage->aData[pPage->hdrOffset+8])==iFrom );
1656 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
1657 }
1658
1659 pPage->isInit = isInitOrig;
1660 }
1661}
1662
danielk1977003ba062004-11-04 02:57:33 +00001663
1664static int relocatePage(
1665 Btree *pBt,
1666 MemPage *pDbPage,
1667 u8 eType,
1668 Pgno iPtrPage,
1669 Pgno iFreePage
1670){
1671 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
1672 Pgno iDbPage = pDbPage->pgno;
1673 Pager *pPager = pBt->pPager;
1674 int rc;
1675
danielk1977a0bf2652004-11-04 14:30:04 +00001676 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
1677 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
danielk1977003ba062004-11-04 02:57:33 +00001678
1679 /* Move page iDbPage from it's current location to page number iFreePage */
1680 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
1681 iDbPage, iFreePage, iPtrPage, eType));
1682 rc = sqlite3pager_movepage(pPager, pDbPage->aData, iFreePage);
1683 if( rc!=SQLITE_OK ){
1684 return rc;
1685 }
1686 pDbPage->pgno = iFreePage;
1687
1688 /* If pDbPage was a btree-page, then it may have child pages and/or cells
1689 ** that point to overflow pages. The pointer map entries for all these
1690 ** pages need to be changed.
1691 **
1692 ** If pDbPage is an overflow page, then the first 4 bytes may store a
1693 ** pointer to a subsequent overflow page. If this is the case, then
1694 ** the pointer map needs to be updated for the subsequent overflow page.
1695 */
danielk1977a0bf2652004-11-04 14:30:04 +00001696 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00001697 rc = setChildPtrmaps(pDbPage);
1698 if( rc!=SQLITE_OK ){
1699 return rc;
1700 }
1701 }else{
1702 Pgno nextOvfl = get4byte(pDbPage->aData);
1703 if( nextOvfl!=0 ){
1704 assert( nextOvfl<=sqlite3pager_pagecount(pPager) );
1705 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
1706 if( rc!=SQLITE_OK ){
1707 return rc;
1708 }
1709 }
1710 }
1711
1712 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
1713 ** that it points at iFreePage. Also fix the pointer map entry for
1714 ** iPtrPage.
1715 */
danielk1977a0bf2652004-11-04 14:30:04 +00001716 if( eType!=PTRMAP_ROOTPAGE ){
1717 rc = getPage(pBt, iPtrPage, &pPtrPage);
1718 if( rc!=SQLITE_OK ){
1719 return rc;
1720 }
1721 rc = sqlite3pager_write(pPtrPage->aData);
1722 if( rc!=SQLITE_OK ){
1723 releasePage(pPtrPage);
1724 return rc;
1725 }
1726 modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
1727 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
danielk1977003ba062004-11-04 02:57:33 +00001728 releasePage(pPtrPage);
danielk1977003ba062004-11-04 02:57:33 +00001729 }
danielk1977003ba062004-11-04 02:57:33 +00001730 return rc;
1731}
1732
danielk1977687566d2004-11-02 12:56:41 +00001733/* Forward declaration required by autoVacuumCommit(). */
danielk1977cb1a7eb2004-11-05 12:27:02 +00001734static int allocatePage(Btree *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00001735
1736/*
1737** This routine is called prior to sqlite3pager_commit when a transaction
1738** is commited for an auto-vacuum database.
1739*/
danielk1977d761c0c2004-11-05 16:37:02 +00001740static int autoVacuumCommit(Btree *pBt, Pgno *nTrunc){
danielk1977687566d2004-11-02 12:56:41 +00001741 Pager *pPager = pBt->pPager;
1742 Pgno nFreeList; /* Number of pages remaining on the free-list. */
danielk1977a19df672004-11-03 11:37:07 +00001743 int nPtrMap; /* Number of pointer-map pages deallocated */
1744 Pgno origSize; /* Pages in the database file */
1745 Pgno finSize; /* Pages in the database file after truncation */
danielk1977687566d2004-11-02 12:56:41 +00001746 int rc; /* Return code */
1747 u8 eType;
danielk1977a19df672004-11-03 11:37:07 +00001748 int pgsz = pBt->pageSize; /* Page size for this database */
danielk1977687566d2004-11-02 12:56:41 +00001749 Pgno iDbPage; /* The database page to move */
danielk1977687566d2004-11-02 12:56:41 +00001750 MemPage *pDbMemPage = 0; /* "" */
1751 Pgno iPtrPage; /* The page that contains a pointer to iDbPage */
danielk1977687566d2004-11-02 12:56:41 +00001752 Pgno iFreePage; /* The free-list page to move iDbPage to */
1753 MemPage *pFreeMemPage = 0; /* "" */
1754
1755#ifndef NDEBUG
1756 int nRef = *sqlite3pager_stats(pPager);
1757#endif
1758
1759 assert( pBt->autoVacuum );
danielk1977a19df672004-11-03 11:37:07 +00001760 assert( 0==PTRMAP_ISPAGE(pgsz, sqlite3pager_pagecount(pPager)) );
danielk1977687566d2004-11-02 12:56:41 +00001761
1762 /* Figure out how many free-pages are in the database. If there are no
1763 ** free pages, then auto-vacuum is a no-op.
1764 */
1765 nFreeList = get4byte(&pBt->pPage1->aData[36]);
danielk1977a19df672004-11-03 11:37:07 +00001766 if( nFreeList==0 ){
danielk1977d761c0c2004-11-05 16:37:02 +00001767 *nTrunc = 0;
danielk1977a19df672004-11-03 11:37:07 +00001768 return SQLITE_OK;
1769 }
danielk1977687566d2004-11-02 12:56:41 +00001770
danielk1977a19df672004-11-03 11:37:07 +00001771 origSize = sqlite3pager_pagecount(pPager);
1772 nPtrMap = (nFreeList-origSize+PTRMAP_PAGENO(pgsz, origSize)+pgsz/5)/(pgsz/5);
1773 finSize = origSize - nFreeList - nPtrMap;
danielk1977599fcba2004-11-08 07:13:13 +00001774 if( origSize>PENDING_BYTE_PAGE(pBt) && finSize<=PENDING_BYTE_PAGE(pBt) ){
1775 finSize--;
drh42cac6d2004-11-20 20:31:11 +00001776 if( PTRMAP_ISPAGE(pBt->usableSize, finSize) ){
danielk1977599fcba2004-11-08 07:13:13 +00001777 finSize--;
1778 }
1779 }
danielk1977a19df672004-11-03 11:37:07 +00001780 TRACE(("AUTOVACUUM: Begin (db size %d->%d)\n", origSize, finSize));
danielk1977687566d2004-11-02 12:56:41 +00001781
danielk1977a19df672004-11-03 11:37:07 +00001782 /* Variable 'finSize' will be the size of the file in pages after
danielk1977687566d2004-11-02 12:56:41 +00001783 ** the auto-vacuum has completed (the current file size minus the number
1784 ** of pages on the free list). Loop through the pages that lie beyond
1785 ** this mark, and if they are not already on the free list, move them
danielk1977a19df672004-11-03 11:37:07 +00001786 ** to a free page earlier in the file (somewhere before finSize).
danielk1977687566d2004-11-02 12:56:41 +00001787 */
danielk1977a19df672004-11-03 11:37:07 +00001788 for( iDbPage=finSize+1; iDbPage<=origSize; iDbPage++ ){
danielk1977599fcba2004-11-08 07:13:13 +00001789 /* If iDbPage is a pointer map page, or the pending-byte page, skip it. */
1790 if( PTRMAP_ISPAGE(pgsz, iDbPage) || iDbPage==PENDING_BYTE_PAGE(pBt) ){
1791 continue;
1792 }
1793
danielk1977687566d2004-11-02 12:56:41 +00001794 rc = ptrmapGet(pBt, iDbPage, &eType, &iPtrPage);
1795 if( rc!=SQLITE_OK ) goto autovacuum_out;
1796 assert( eType!=PTRMAP_ROOTPAGE );
1797
danielk1977599fcba2004-11-08 07:13:13 +00001798 /* If iDbPage is free, do not swap it. */
1799 if( eType==PTRMAP_FREEPAGE ){
danielk1977687566d2004-11-02 12:56:41 +00001800 continue;
1801 }
1802 rc = getPage(pBt, iDbPage, &pDbMemPage);
1803 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00001804
1805 /* Find the next page in the free-list that is not already at the end
1806 ** of the file. A page can be pulled off the free list using the
1807 ** allocatePage() routine.
1808 */
1809 do{
1810 if( pFreeMemPage ){
1811 releasePage(pFreeMemPage);
1812 pFreeMemPage = 0;
1813 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00001814 rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
danielk1977687566d2004-11-02 12:56:41 +00001815 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977a19df672004-11-03 11:37:07 +00001816 assert( iFreePage<=origSize );
1817 }while( iFreePage>finSize );
danielk1977687566d2004-11-02 12:56:41 +00001818 releasePage(pFreeMemPage);
1819 pFreeMemPage = 0;
danielk1977687566d2004-11-02 12:56:41 +00001820
danielk1977003ba062004-11-04 02:57:33 +00001821 rc = relocatePage(pBt, pDbMemPage, eType, iPtrPage, iFreePage);
danielk1977687566d2004-11-02 12:56:41 +00001822 releasePage(pDbMemPage);
danielk1977687566d2004-11-02 12:56:41 +00001823 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977687566d2004-11-02 12:56:41 +00001824 }
1825
1826 /* The entire free-list has been swapped to the end of the file. So
danielk1977a19df672004-11-03 11:37:07 +00001827 ** truncate the database file to finSize pages and consider the
danielk1977687566d2004-11-02 12:56:41 +00001828 ** free-list empty.
1829 */
1830 rc = sqlite3pager_write(pBt->pPage1->aData);
1831 if( rc!=SQLITE_OK ) goto autovacuum_out;
1832 put4byte(&pBt->pPage1->aData[32], 0);
1833 put4byte(&pBt->pPage1->aData[36], 0);
danielk1977687566d2004-11-02 12:56:41 +00001834 if( rc!=SQLITE_OK ) goto autovacuum_out;
danielk1977d761c0c2004-11-05 16:37:02 +00001835 *nTrunc = finSize;
danielk1977687566d2004-11-02 12:56:41 +00001836
1837autovacuum_out:
danielk1977687566d2004-11-02 12:56:41 +00001838 assert( nRef==*sqlite3pager_stats(pPager) );
1839 if( rc!=SQLITE_OK ){
1840 sqlite3pager_rollback(pPager);
1841 }
1842 return rc;
1843}
1844#endif
1845
1846/*
drh2aa679f2001-06-25 02:11:07 +00001847** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00001848**
1849** This will release the write lock on the database file. If there
1850** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00001851*/
drh3aac2dd2004-04-26 14:10:20 +00001852int sqlite3BtreeCommit(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00001853 int rc = SQLITE_OK;
1854 if( pBt->inTrans==TRANS_WRITE ){
1855 rc = sqlite3pager_commit(pBt->pPager);
1856 }
1857 pBt->inTrans = TRANS_NONE;
drh3aac2dd2004-04-26 14:10:20 +00001858 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00001859 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001860 return rc;
1861}
1862
danielk1977fbcd5852004-06-15 02:44:18 +00001863#ifndef NDEBUG
1864/*
1865** Return the number of write-cursors open on this handle. This is for use
1866** in assert() expressions, so it is only compiled if NDEBUG is not
1867** defined.
1868*/
1869static int countWriteCursors(Btree *pBt){
1870 BtCursor *pCur;
1871 int r = 0;
1872 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1873 if( pCur->wrFlag ) r++;
1874 }
1875 return r;
1876}
1877#endif
1878
1879#if 0
drha059ad02001-04-17 20:09:11 +00001880/*
drhc39e0002004-05-07 23:50:57 +00001881** Invalidate all cursors
1882*/
1883static void invalidateCursors(Btree *pBt){
1884 BtCursor *pCur;
1885 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1886 MemPage *pPage = pCur->pPage;
drhda200cc2004-05-09 11:51:38 +00001887 if( pPage /* && !pPage->isInit */ ){
1888 pageIntegrity(pPage);
drhc39e0002004-05-07 23:50:57 +00001889 releasePage(pPage);
1890 pCur->pPage = 0;
1891 pCur->isValid = 0;
1892 pCur->status = SQLITE_ABORT;
1893 }
1894 }
1895}
danielk1977fbcd5852004-06-15 02:44:18 +00001896#endif
drhc39e0002004-05-07 23:50:57 +00001897
drhda200cc2004-05-09 11:51:38 +00001898#ifdef SQLITE_TEST
1899/*
1900** Print debugging information about all cursors to standard output.
1901*/
1902void sqlite3BtreeCursorList(Btree *pBt){
1903 BtCursor *pCur;
1904 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
1905 MemPage *pPage = pCur->pPage;
1906 char *zMode = pCur->wrFlag ? "rw" : "ro";
drhfe63d1c2004-09-08 20:13:04 +00001907 sqlite3DebugPrintf("CURSOR %p rooted at %4d(%s) currently at %d.%d%s\n",
1908 pCur, pCur->pgnoRoot, zMode,
drhda200cc2004-05-09 11:51:38 +00001909 pPage ? pPage->pgno : 0, pCur->idx,
1910 pCur->isValid ? "" : " eof"
1911 );
1912 }
1913}
1914#endif
1915
drhc39e0002004-05-07 23:50:57 +00001916/*
drhecdc7532001-09-23 02:35:53 +00001917** Rollback the transaction in progress. All cursors will be
1918** invalided by this operation. Any attempt to use a cursor
1919** that was open at the beginning of this operation will result
1920** in an error.
drh5e00f6c2001-09-13 13:46:56 +00001921**
1922** This will release the write lock on the database file. If there
1923** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00001924*/
drh3aac2dd2004-04-26 14:10:20 +00001925int sqlite3BtreeRollback(Btree *pBt){
danielk1977cfe9a692004-06-16 12:00:29 +00001926 int rc = SQLITE_OK;
drh24cd67e2004-05-10 16:18:47 +00001927 MemPage *pPage1;
danielk1977ee5741e2004-05-31 10:01:34 +00001928 if( pBt->inTrans==TRANS_WRITE ){
drh24cd67e2004-05-10 16:18:47 +00001929 rc = sqlite3pager_rollback(pBt->pPager);
1930 /* The rollback may have destroyed the pPage1->aData value. So
1931 ** call getPage() on page 1 again to make sure pPage1->aData is
1932 ** set correctly. */
1933 if( getPage(pBt, 1, &pPage1)==SQLITE_OK ){
1934 releasePage(pPage1);
1935 }
danielk1977fbcd5852004-06-15 02:44:18 +00001936 assert( countWriteCursors(pBt)==0 );
drh24cd67e2004-05-10 16:18:47 +00001937 }
danielk1977ee5741e2004-05-31 10:01:34 +00001938 pBt->inTrans = TRANS_NONE;
1939 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00001940 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001941 return rc;
1942}
1943
1944/*
drhab01f612004-05-22 02:55:23 +00001945** Start a statement subtransaction. The subtransaction can
1946** can be rolled back independently of the main transaction.
1947** You must start a transaction before starting a subtransaction.
1948** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00001949** commits or rolls back.
1950**
drhab01f612004-05-22 02:55:23 +00001951** Only one subtransaction may be active at a time. It is an error to try
1952** to start a new subtransaction if another subtransaction is already active.
1953**
1954** Statement subtransactions are used around individual SQL statements
1955** that are contained within a BEGIN...COMMIT block. If a constraint
1956** error occurs within the statement, the effect of that one statement
1957** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00001958*/
drh3aac2dd2004-04-26 14:10:20 +00001959int sqlite3BtreeBeginStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00001960 int rc;
danielk1977ee5741e2004-05-31 10:01:34 +00001961 if( (pBt->inTrans!=TRANS_WRITE) || pBt->inStmt ){
drhf74b8d92002-09-01 23:20:45 +00001962 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh0d65dc02002-02-03 00:56:09 +00001963 }
drha34b6762004-05-07 13:30:42 +00001964 rc = pBt->readOnly ? SQLITE_OK : sqlite3pager_stmt_begin(pBt->pPager);
drh3aac2dd2004-04-26 14:10:20 +00001965 pBt->inStmt = 1;
drh663fc632002-02-02 18:49:19 +00001966 return rc;
1967}
1968
1969
1970/*
drhab01f612004-05-22 02:55:23 +00001971** Commit the statment subtransaction currently in progress. If no
1972** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00001973*/
drh3aac2dd2004-04-26 14:10:20 +00001974int sqlite3BtreeCommitStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00001975 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001976 if( pBt->inStmt && !pBt->readOnly ){
drha34b6762004-05-07 13:30:42 +00001977 rc = sqlite3pager_stmt_commit(pBt->pPager);
drh663fc632002-02-02 18:49:19 +00001978 }else{
1979 rc = SQLITE_OK;
1980 }
drh3aac2dd2004-04-26 14:10:20 +00001981 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00001982 return rc;
1983}
1984
1985/*
drhab01f612004-05-22 02:55:23 +00001986** Rollback the active statement subtransaction. If no subtransaction
1987** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00001988**
drhab01f612004-05-22 02:55:23 +00001989** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00001990** to use a cursor that was open at the beginning of this operation
1991** will result in an error.
1992*/
drh3aac2dd2004-04-26 14:10:20 +00001993int sqlite3BtreeRollbackStmt(Btree *pBt){
drh663fc632002-02-02 18:49:19 +00001994 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001995 if( pBt->inStmt==0 || pBt->readOnly ) return SQLITE_OK;
drha34b6762004-05-07 13:30:42 +00001996 rc = sqlite3pager_stmt_rollback(pBt->pPager);
danielk1977fbcd5852004-06-15 02:44:18 +00001997 assert( countWriteCursors(pBt)==0 );
drh3aac2dd2004-04-26 14:10:20 +00001998 pBt->inStmt = 0;
drh663fc632002-02-02 18:49:19 +00001999 return rc;
2000}
2001
2002/*
drh3aac2dd2004-04-26 14:10:20 +00002003** Default key comparison function to be used if no comparison function
2004** is specified on the sqlite3BtreeCursor() call.
2005*/
2006static int dfltCompare(
2007 void *NotUsed, /* User data is not used */
2008 int n1, const void *p1, /* First key to compare */
2009 int n2, const void *p2 /* Second key to compare */
2010){
2011 int c;
2012 c = memcmp(p1, p2, n1<n2 ? n1 : n2);
2013 if( c==0 ){
2014 c = n1 - n2;
2015 }
2016 return c;
2017}
2018
2019/*
drh8b2f49b2001-06-08 00:21:52 +00002020** Create a new cursor for the BTree whose root is on the page
2021** iTable. The act of acquiring a cursor gets a read lock on
2022** the database file.
drh1bee3d72001-10-15 00:44:35 +00002023**
2024** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002025** If wrFlag==1, then the cursor can be used for reading or for
2026** writing if other conditions for writing are also met. These
2027** are the conditions that must be met in order for writing to
2028** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002029**
drhf74b8d92002-09-01 23:20:45 +00002030** 1: The cursor must have been opened with wrFlag==1
2031**
2032** 2: No other cursors may be open with wrFlag==0 on the same table
2033**
2034** 3: The database must be writable (not on read-only media)
2035**
2036** 4: There must be an active transaction.
2037**
2038** Condition 2 warrants further discussion. If any cursor is opened
2039** on a table with wrFlag==0, that prevents all other cursors from
2040** writing to that table. This is a kind of "read-lock". When a cursor
2041** is opened with wrFlag==0 it is guaranteed that the table will not
2042** change as long as the cursor is open. This allows the cursor to
2043** do a sequential scan of the table without having to worry about
2044** entries being inserted or deleted during the scan. Cursors should
2045** be opened with wrFlag==0 only if this read-lock property is needed.
2046** That is to say, cursors should be opened with wrFlag==0 only if they
drh23e11ca2004-05-04 17:27:28 +00002047** intend to use the sqlite3BtreeNext() system call. All other cursors
drhf74b8d92002-09-01 23:20:45 +00002048** should be opened with wrFlag==1 even if they never really intend
2049** to write.
2050**
drh6446c4d2001-12-15 14:22:18 +00002051** No checking is done to make sure that page iTable really is the
2052** root page of a b-tree. If it is not, then the cursor acquired
2053** will not work correctly.
drh3aac2dd2004-04-26 14:10:20 +00002054**
2055** The comparison function must be logically the same for every cursor
2056** on a particular table. Changing the comparison function will result
2057** in incorrect operations. If the comparison function is NULL, a
2058** default comparison function is used. The comparison function is
2059** always ignored for INTKEY tables.
drha059ad02001-04-17 20:09:11 +00002060*/
drh3aac2dd2004-04-26 14:10:20 +00002061int sqlite3BtreeCursor(
2062 Btree *pBt, /* The btree */
2063 int iTable, /* Root page of table to open */
2064 int wrFlag, /* 1 to write. 0 read-only */
2065 int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */
2066 void *pArg, /* First arg to xCompare() */
2067 BtCursor **ppCur /* Write new cursor here */
2068){
drha059ad02001-04-17 20:09:11 +00002069 int rc;
drh8dcd7ca2004-08-08 19:43:29 +00002070 BtCursor *pCur;
drhecdc7532001-09-23 02:35:53 +00002071
drh8dcd7ca2004-08-08 19:43:29 +00002072 *ppCur = 0;
2073 if( wrFlag ){
drh8dcd7ca2004-08-08 19:43:29 +00002074 if( pBt->readOnly ){
2075 return SQLITE_READONLY;
2076 }
2077 if( checkReadLocks(pBt, iTable, 0) ){
2078 return SQLITE_LOCKED;
2079 }
drha0c9a112004-03-10 13:42:37 +00002080 }
drh4b70f112004-05-02 21:12:19 +00002081 if( pBt->pPage1==0 ){
drha059ad02001-04-17 20:09:11 +00002082 rc = lockBtree(pBt);
2083 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002084 return rc;
2085 }
2086 }
drheafe05b2004-06-13 00:54:01 +00002087 pCur = sqliteMallocRaw( sizeof(*pCur) );
drha059ad02001-04-17 20:09:11 +00002088 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +00002089 rc = SQLITE_NOMEM;
2090 goto create_cursor_exception;
2091 }
drh8b2f49b2001-06-08 00:21:52 +00002092 pCur->pgnoRoot = (Pgno)iTable;
drh24cd67e2004-05-10 16:18:47 +00002093 if( iTable==1 && sqlite3pager_pagecount(pBt->pPager)==0 ){
2094 rc = SQLITE_EMPTY;
drheafe05b2004-06-13 00:54:01 +00002095 pCur->pPage = 0;
drh24cd67e2004-05-10 16:18:47 +00002096 goto create_cursor_exception;
2097 }
danielk1977369f27e2004-06-15 11:40:04 +00002098 pCur->pPage = 0; /* For exit-handler, in case getAndInitPage() fails. */
drhde647132004-05-07 17:57:49 +00002099 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
drhbd03cae2001-06-02 02:40:57 +00002100 if( rc!=SQLITE_OK ){
2101 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002102 }
drh3aac2dd2004-04-26 14:10:20 +00002103 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2104 pCur->pArg = pArg;
drh14acc042001-06-10 19:56:58 +00002105 pCur->pBt = pBt;
drhecdc7532001-09-23 02:35:53 +00002106 pCur->wrFlag = wrFlag;
drh14acc042001-06-10 19:56:58 +00002107 pCur->idx = 0;
drh59eb6762004-06-13 23:07:04 +00002108 memset(&pCur->info, 0, sizeof(pCur->info));
drha059ad02001-04-17 20:09:11 +00002109 pCur->pNext = pBt->pCursor;
2110 if( pCur->pNext ){
2111 pCur->pNext->pPrev = pCur;
2112 }
drh14acc042001-06-10 19:56:58 +00002113 pCur->pPrev = 0;
drha059ad02001-04-17 20:09:11 +00002114 pBt->pCursor = pCur;
drhc39e0002004-05-07 23:50:57 +00002115 pCur->isValid = 0;
2116 pCur->status = SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00002117 *ppCur = pCur;
2118 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00002119
2120create_cursor_exception:
drhbd03cae2001-06-02 02:40:57 +00002121 if( pCur ){
drh3aac2dd2004-04-26 14:10:20 +00002122 releasePage(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00002123 sqliteFree(pCur);
2124 }
drh5e00f6c2001-09-13 13:46:56 +00002125 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002126 return rc;
drha059ad02001-04-17 20:09:11 +00002127}
2128
drh7a224de2004-06-02 01:22:02 +00002129#if 0 /* Not Used */
drhd3d39e92004-05-20 22:16:29 +00002130/*
2131** Change the value of the comparison function used by a cursor.
2132*/
danielk1977bf3b7212004-05-18 10:06:24 +00002133void sqlite3BtreeSetCompare(
drhd3d39e92004-05-20 22:16:29 +00002134 BtCursor *pCur, /* The cursor to whose comparison function is changed */
2135 int(*xCmp)(void*,int,const void*,int,const void*), /* New comparison func */
2136 void *pArg /* First argument to xCmp() */
danielk1977bf3b7212004-05-18 10:06:24 +00002137){
2138 pCur->xCompare = xCmp ? xCmp : dfltCompare;
2139 pCur->pArg = pArg;
2140}
drh7a224de2004-06-02 01:22:02 +00002141#endif
danielk1977bf3b7212004-05-18 10:06:24 +00002142
drha059ad02001-04-17 20:09:11 +00002143/*
drh5e00f6c2001-09-13 13:46:56 +00002144** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002145** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002146*/
drh3aac2dd2004-04-26 14:10:20 +00002147int sqlite3BtreeCloseCursor(BtCursor *pCur){
drha059ad02001-04-17 20:09:11 +00002148 Btree *pBt = pCur->pBt;
drha059ad02001-04-17 20:09:11 +00002149 if( pCur->pPrev ){
2150 pCur->pPrev->pNext = pCur->pNext;
2151 }else{
2152 pBt->pCursor = pCur->pNext;
2153 }
2154 if( pCur->pNext ){
2155 pCur->pNext->pPrev = pCur->pPrev;
2156 }
drh3aac2dd2004-04-26 14:10:20 +00002157 releasePage(pCur->pPage);
drh5e00f6c2001-09-13 13:46:56 +00002158 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00002159 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +00002160 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002161}
2162
drh7e3b0a02001-04-28 16:52:40 +00002163/*
drh5e2f8b92001-05-28 00:41:15 +00002164** Make a temporary cursor by filling in the fields of pTempCur.
2165** The temporary cursor is not on the cursor list for the Btree.
2166*/
drh14acc042001-06-10 19:56:58 +00002167static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +00002168 memcpy(pTempCur, pCur, sizeof(*pCur));
2169 pTempCur->pNext = 0;
2170 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00002171 if( pTempCur->pPage ){
drha34b6762004-05-07 13:30:42 +00002172 sqlite3pager_ref(pTempCur->pPage->aData);
drhecdc7532001-09-23 02:35:53 +00002173 }
drh5e2f8b92001-05-28 00:41:15 +00002174}
2175
2176/*
drhbd03cae2001-06-02 02:40:57 +00002177** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002178** function above.
2179*/
drh14acc042001-06-10 19:56:58 +00002180static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +00002181 if( pCur->pPage ){
drha34b6762004-05-07 13:30:42 +00002182 sqlite3pager_unref(pCur->pPage->aData);
drhecdc7532001-09-23 02:35:53 +00002183 }
drh5e2f8b92001-05-28 00:41:15 +00002184}
2185
2186/*
drh9188b382004-05-14 21:12:22 +00002187** Make sure the BtCursor.info field of the given cursor is valid.
drhab01f612004-05-22 02:55:23 +00002188** If it is not already valid, call parseCell() to fill it in.
2189**
2190** BtCursor.info is a cache of the information in the current cell.
2191** Using this cache reduces the number of calls to parseCell().
drh9188b382004-05-14 21:12:22 +00002192*/
2193static void getCellInfo(BtCursor *pCur){
drh271efa52004-05-30 19:19:05 +00002194 if( pCur->info.nSize==0 ){
drh3a41a3f2004-05-30 02:14:17 +00002195 parseCell(pCur->pPage, pCur->idx, &pCur->info);
drh9188b382004-05-14 21:12:22 +00002196 }else{
2197#ifndef NDEBUG
2198 CellInfo info;
drh51c6d962004-06-06 00:42:25 +00002199 memset(&info, 0, sizeof(info));
drh3a41a3f2004-05-30 02:14:17 +00002200 parseCell(pCur->pPage, pCur->idx, &info);
drh9188b382004-05-14 21:12:22 +00002201 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
2202#endif
2203 }
2204}
2205
2206/*
drh3aac2dd2004-04-26 14:10:20 +00002207** Set *pSize to the size of the buffer needed to hold the value of
2208** the key for the current entry. If the cursor is not pointing
2209** to a valid entry, *pSize is set to 0.
2210**
drh4b70f112004-05-02 21:12:19 +00002211** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00002212** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00002213*/
drh4a1c3802004-05-12 15:15:47 +00002214int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
danielk1977299b1872004-11-22 10:02:10 +00002215 if( !pCur->isValid ){
drh72f82862001-05-24 21:06:34 +00002216 *pSize = 0;
2217 }else{
drh9188b382004-05-14 21:12:22 +00002218 getCellInfo(pCur);
2219 *pSize = pCur->info.nKey;
drh72f82862001-05-24 21:06:34 +00002220 }
2221 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002222}
drh2af926b2001-05-15 00:39:25 +00002223
drh72f82862001-05-24 21:06:34 +00002224/*
drh0e1c19e2004-05-11 00:58:56 +00002225** Set *pSize to the number of bytes of data in the entry the
2226** cursor currently points to. Always return SQLITE_OK.
2227** Failure is not possible. If the cursor is not currently
2228** pointing to an entry (which can happen, for example, if
2229** the database is empty) then *pSize is set to 0.
2230*/
2231int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
danielk1977299b1872004-11-22 10:02:10 +00002232 if( !pCur->isValid ){
danielk197796fc5fe2004-05-13 11:34:16 +00002233 /* Not pointing at a valid entry - set *pSize to 0. */
drh0e1c19e2004-05-11 00:58:56 +00002234 *pSize = 0;
2235 }else{
drh9188b382004-05-14 21:12:22 +00002236 getCellInfo(pCur);
2237 *pSize = pCur->info.nData;
drh0e1c19e2004-05-11 00:58:56 +00002238 }
2239 return SQLITE_OK;
2240}
2241
2242/*
drh72f82862001-05-24 21:06:34 +00002243** Read payload information from the entry that the pCur cursor is
2244** pointing to. Begin reading the payload at "offset" and read
2245** a total of "amt" bytes. Put the result in zBuf.
2246**
2247** This routine does not make a distinction between key and data.
drhab01f612004-05-22 02:55:23 +00002248** It just reads bytes from the payload area. Data might appear
2249** on the main page or be scattered out on multiple overflow pages.
drh72f82862001-05-24 21:06:34 +00002250*/
drh3aac2dd2004-04-26 14:10:20 +00002251static int getPayload(
2252 BtCursor *pCur, /* Cursor pointing to entry to read from */
2253 int offset, /* Begin reading this far into payload */
2254 int amt, /* Read this many bytes */
2255 unsigned char *pBuf, /* Write the bytes into this buffer */
2256 int skipKey /* offset begins at data if this is true */
2257){
2258 unsigned char *aPayload;
drh2af926b2001-05-15 00:39:25 +00002259 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +00002260 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002261 MemPage *pPage;
2262 Btree *pBt;
drh6f11bef2004-05-13 01:12:56 +00002263 int ovflSize;
drhfa1a98a2004-05-14 19:08:17 +00002264 u32 nKey;
drh3aac2dd2004-04-26 14:10:20 +00002265
drh72f82862001-05-24 21:06:34 +00002266 assert( pCur!=0 && pCur->pPage!=0 );
drhc39e0002004-05-07 23:50:57 +00002267 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002268 pBt = pCur->pBt;
2269 pPage = pCur->pPage;
drhda200cc2004-05-09 11:51:38 +00002270 pageIntegrity(pPage);
drh3aac2dd2004-04-26 14:10:20 +00002271 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00002272 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00002273 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00002274 aPayload += pCur->info.nHeader;
drh3aac2dd2004-04-26 14:10:20 +00002275 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00002276 nKey = 0;
2277 }else{
2278 nKey = pCur->info.nKey;
drh3aac2dd2004-04-26 14:10:20 +00002279 }
2280 assert( offset>=0 );
2281 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00002282 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00002283 }
drhfa1a98a2004-05-14 19:08:17 +00002284 if( offset+amt > nKey+pCur->info.nData ){
drha34b6762004-05-07 13:30:42 +00002285 return SQLITE_ERROR;
drh3aac2dd2004-04-26 14:10:20 +00002286 }
drhfa1a98a2004-05-14 19:08:17 +00002287 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00002288 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00002289 if( a+offset>pCur->info.nLocal ){
2290 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00002291 }
drha34b6762004-05-07 13:30:42 +00002292 memcpy(pBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +00002293 if( a==amt ){
2294 return SQLITE_OK;
2295 }
drh2aa679f2001-06-25 02:11:07 +00002296 offset = 0;
drha34b6762004-05-07 13:30:42 +00002297 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00002298 amt -= a;
drhdd793422001-06-28 01:54:48 +00002299 }else{
drhfa1a98a2004-05-14 19:08:17 +00002300 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00002301 }
danielk1977cfe9a692004-06-16 12:00:29 +00002302 ovflSize = pBt->usableSize - 4;
drhbd03cae2001-06-02 02:40:57 +00002303 if( amt>0 ){
drhfa1a98a2004-05-14 19:08:17 +00002304 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977cfe9a692004-06-16 12:00:29 +00002305 while( amt>0 && nextPage ){
2306 rc = sqlite3pager_get(pBt->pPager, nextPage, (void**)&aPayload);
2307 if( rc!=0 ){
2308 return rc;
drh2af926b2001-05-15 00:39:25 +00002309 }
danielk1977cfe9a692004-06-16 12:00:29 +00002310 nextPage = get4byte(aPayload);
2311 if( offset<ovflSize ){
2312 int a = amt;
2313 if( a + offset > ovflSize ){
2314 a = ovflSize - offset;
2315 }
2316 memcpy(pBuf, &aPayload[offset+4], a);
2317 offset = 0;
2318 amt -= a;
2319 pBuf += a;
2320 }else{
2321 offset -= ovflSize;
2322 }
2323 sqlite3pager_unref(aPayload);
drh2af926b2001-05-15 00:39:25 +00002324 }
drh2af926b2001-05-15 00:39:25 +00002325 }
danielk1977cfe9a692004-06-16 12:00:29 +00002326
drha7fcb052001-12-14 15:09:55 +00002327 if( amt>0 ){
drhee696e22004-08-30 16:52:17 +00002328 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
drha7fcb052001-12-14 15:09:55 +00002329 }
2330 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00002331}
2332
drh72f82862001-05-24 21:06:34 +00002333/*
drh3aac2dd2004-04-26 14:10:20 +00002334** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002335** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002336** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00002337**
drh3aac2dd2004-04-26 14:10:20 +00002338** Return SQLITE_OK on success or an error code if anything goes
2339** wrong. An error is returned if "offset+amt" is larger than
2340** the available payload.
drh72f82862001-05-24 21:06:34 +00002341*/
drha34b6762004-05-07 13:30:42 +00002342int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
danielk1977299b1872004-11-22 10:02:10 +00002343 if( !pCur->isValid ){
drhc39e0002004-05-07 23:50:57 +00002344 return pCur->status;
drh3aac2dd2004-04-26 14:10:20 +00002345 }
drhc39e0002004-05-07 23:50:57 +00002346 assert( pCur->pPage!=0 );
2347 assert( pCur->pPage->intKey==0 );
2348 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh3aac2dd2004-04-26 14:10:20 +00002349 return getPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
2350}
2351
2352/*
drh3aac2dd2004-04-26 14:10:20 +00002353** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00002354** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00002355** begins at "offset".
2356**
2357** Return SQLITE_OK on success or an error code if anything goes
2358** wrong. An error is returned if "offset+amt" is larger than
2359** the available payload.
drh72f82862001-05-24 21:06:34 +00002360*/
drh3aac2dd2004-04-26 14:10:20 +00002361int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
danielk1977299b1872004-11-22 10:02:10 +00002362 if( !pCur->isValid ){
drhc39e0002004-05-07 23:50:57 +00002363 return pCur->status ? pCur->status : SQLITE_INTERNAL;
2364 }
drh8c1238a2003-01-02 14:43:55 +00002365 assert( pCur->pPage!=0 );
drhc39e0002004-05-07 23:50:57 +00002366 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh3aac2dd2004-04-26 14:10:20 +00002367 return getPayload(pCur, offset, amt, pBuf, 1);
drh2af926b2001-05-15 00:39:25 +00002368}
2369
drh72f82862001-05-24 21:06:34 +00002370/*
drh0e1c19e2004-05-11 00:58:56 +00002371** Return a pointer to payload information from the entry that the
2372** pCur cursor is pointing to. The pointer is to the beginning of
2373** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00002374** skipKey==1. The number of bytes of available key/data is written
2375** into *pAmt. If *pAmt==0, then the value returned will not be
2376** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00002377**
2378** This routine is an optimization. It is common for the entire key
2379** and data to fit on the local page and for there to be no overflow
2380** pages. When that is so, this routine can be used to access the
2381** key and data without making a copy. If the key and/or data spills
2382** onto overflow pages, then getPayload() must be used to reassembly
2383** the key/data and copy it into a preallocated buffer.
2384**
2385** The pointer returned by this routine looks directly into the cached
2386** page of the database. The data might change or move the next time
2387** any btree routine is called.
2388*/
2389static const unsigned char *fetchPayload(
2390 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00002391 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00002392 int skipKey /* read beginning at data if this is true */
2393){
2394 unsigned char *aPayload;
2395 MemPage *pPage;
2396 Btree *pBt;
drhfa1a98a2004-05-14 19:08:17 +00002397 u32 nKey;
2398 int nLocal;
drh0e1c19e2004-05-11 00:58:56 +00002399
2400 assert( pCur!=0 && pCur->pPage!=0 );
2401 assert( pCur->isValid );
2402 pBt = pCur->pBt;
2403 pPage = pCur->pPage;
2404 pageIntegrity(pPage);
2405 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh9188b382004-05-14 21:12:22 +00002406 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00002407 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00002408 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00002409 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00002410 nKey = 0;
2411 }else{
2412 nKey = pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00002413 }
drh0e1c19e2004-05-11 00:58:56 +00002414 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00002415 aPayload += nKey;
2416 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00002417 }else{
drhfa1a98a2004-05-14 19:08:17 +00002418 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00002419 if( nLocal>nKey ){
2420 nLocal = nKey;
2421 }
drh0e1c19e2004-05-11 00:58:56 +00002422 }
drhe51c44f2004-05-30 20:46:09 +00002423 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00002424 return aPayload;
2425}
2426
2427
2428/*
drhe51c44f2004-05-30 20:46:09 +00002429** For the entry that cursor pCur is point to, return as
2430** many bytes of the key or data as are available on the local
2431** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00002432**
2433** The pointer returned is ephemeral. The key/data may move
2434** or be destroyed on the next call to any Btree routine.
2435**
2436** These routines is used to get quick access to key and data
2437** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00002438*/
drhe51c44f2004-05-30 20:46:09 +00002439const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
2440 return (const void*)fetchPayload(pCur, pAmt, 0);
drh0e1c19e2004-05-11 00:58:56 +00002441}
drhe51c44f2004-05-30 20:46:09 +00002442const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
2443 return (const void*)fetchPayload(pCur, pAmt, 1);
drh0e1c19e2004-05-11 00:58:56 +00002444}
2445
2446
2447/*
drh8178a752003-01-05 21:41:40 +00002448** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00002449** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00002450*/
drh3aac2dd2004-04-26 14:10:20 +00002451static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00002452 int rc;
2453 MemPage *pNewPage;
drh3aac2dd2004-04-26 14:10:20 +00002454 MemPage *pOldPage;
drh0d316a42002-08-11 20:10:47 +00002455 Btree *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00002456
drhc39e0002004-05-07 23:50:57 +00002457 assert( pCur->isValid );
drhde647132004-05-07 17:57:49 +00002458 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00002459 if( rc ) return rc;
drhda200cc2004-05-09 11:51:38 +00002460 pageIntegrity(pNewPage);
drh428ae8c2003-01-04 16:48:09 +00002461 pNewPage->idxParent = pCur->idx;
drh3aac2dd2004-04-26 14:10:20 +00002462 pOldPage = pCur->pPage;
2463 pOldPage->idxShift = 0;
2464 releasePage(pOldPage);
drh72f82862001-05-24 21:06:34 +00002465 pCur->pPage = pNewPage;
2466 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00002467 pCur->info.nSize = 0;
drh4be295b2003-12-16 03:44:47 +00002468 if( pNewPage->nCell<1 ){
drhee696e22004-08-30 16:52:17 +00002469 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
drh4be295b2003-12-16 03:44:47 +00002470 }
drh72f82862001-05-24 21:06:34 +00002471 return SQLITE_OK;
2472}
2473
2474/*
drh8856d6a2004-04-29 14:42:46 +00002475** Return true if the page is the virtual root of its table.
2476**
2477** The virtual root page is the root page for most tables. But
2478** for the table rooted on page 1, sometime the real root page
2479** is empty except for the right-pointer. In such cases the
2480** virtual root page is the page that the right-pointer of page
2481** 1 is pointing to.
2482*/
2483static int isRootPage(MemPage *pPage){
2484 MemPage *pParent = pPage->pParent;
drhda200cc2004-05-09 11:51:38 +00002485 if( pParent==0 ) return 1;
2486 if( pParent->pgno>1 ) return 0;
2487 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
drh8856d6a2004-04-29 14:42:46 +00002488 return 0;
2489}
2490
2491/*
drh5e2f8b92001-05-28 00:41:15 +00002492** Move the cursor up to the parent page.
2493**
2494** pCur->idx is set to the cell index that contains the pointer
2495** to the page we are coming from. If we are coming from the
2496** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00002497** the largest cell index.
drh72f82862001-05-24 21:06:34 +00002498*/
drh8178a752003-01-05 21:41:40 +00002499static void moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00002500 Pgno oldPgno;
2501 MemPage *pParent;
drh8178a752003-01-05 21:41:40 +00002502 MemPage *pPage;
drh428ae8c2003-01-04 16:48:09 +00002503 int idxParent;
drh3aac2dd2004-04-26 14:10:20 +00002504
drhc39e0002004-05-07 23:50:57 +00002505 assert( pCur->isValid );
drh8178a752003-01-05 21:41:40 +00002506 pPage = pCur->pPage;
2507 assert( pPage!=0 );
drh8856d6a2004-04-29 14:42:46 +00002508 assert( !isRootPage(pPage) );
drhda200cc2004-05-09 11:51:38 +00002509 pageIntegrity(pPage);
drh8178a752003-01-05 21:41:40 +00002510 pParent = pPage->pParent;
2511 assert( pParent!=0 );
drhda200cc2004-05-09 11:51:38 +00002512 pageIntegrity(pParent);
drh8178a752003-01-05 21:41:40 +00002513 idxParent = pPage->idxParent;
drha34b6762004-05-07 13:30:42 +00002514 sqlite3pager_ref(pParent->aData);
drh3aac2dd2004-04-26 14:10:20 +00002515 oldPgno = pPage->pgno;
2516 releasePage(pPage);
drh72f82862001-05-24 21:06:34 +00002517 pCur->pPage = pParent;
drh271efa52004-05-30 19:19:05 +00002518 pCur->info.nSize = 0;
drh428ae8c2003-01-04 16:48:09 +00002519 assert( pParent->idxShift==0 );
drh43605152004-05-29 21:46:49 +00002520 pCur->idx = idxParent;
drh72f82862001-05-24 21:06:34 +00002521}
2522
2523/*
2524** Move the cursor to the root page
2525*/
drh5e2f8b92001-05-28 00:41:15 +00002526static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00002527 MemPage *pRoot;
drhbd03cae2001-06-02 02:40:57 +00002528 int rc;
drh0d316a42002-08-11 20:10:47 +00002529 Btree *pBt = pCur->pBt;
drhbd03cae2001-06-02 02:40:57 +00002530
drhde647132004-05-07 17:57:49 +00002531 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0);
drhc39e0002004-05-07 23:50:57 +00002532 if( rc ){
2533 pCur->isValid = 0;
2534 return rc;
2535 }
drh3aac2dd2004-04-26 14:10:20 +00002536 releasePage(pCur->pPage);
drhda200cc2004-05-09 11:51:38 +00002537 pageIntegrity(pRoot);
drh3aac2dd2004-04-26 14:10:20 +00002538 pCur->pPage = pRoot;
drh72f82862001-05-24 21:06:34 +00002539 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00002540 pCur->info.nSize = 0;
drh8856d6a2004-04-29 14:42:46 +00002541 if( pRoot->nCell==0 && !pRoot->leaf ){
2542 Pgno subpage;
2543 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00002544 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00002545 assert( subpage>0 );
drh3644f082004-05-10 18:45:09 +00002546 pCur->isValid = 1;
drh4b70f112004-05-02 21:12:19 +00002547 rc = moveToChild(pCur, subpage);
drh8856d6a2004-04-29 14:42:46 +00002548 }
drhc39e0002004-05-07 23:50:57 +00002549 pCur->isValid = pCur->pPage->nCell>0;
drh8856d6a2004-04-29 14:42:46 +00002550 return rc;
drh72f82862001-05-24 21:06:34 +00002551}
drh2af926b2001-05-15 00:39:25 +00002552
drh5e2f8b92001-05-28 00:41:15 +00002553/*
2554** Move the cursor down to the left-most leaf entry beneath the
2555** entry to which it is currently pointing.
2556*/
2557static int moveToLeftmost(BtCursor *pCur){
2558 Pgno pgno;
2559 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002560 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00002561
drhc39e0002004-05-07 23:50:57 +00002562 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002563 while( !(pPage = pCur->pPage)->leaf ){
drha34b6762004-05-07 13:30:42 +00002564 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00002565 pgno = get4byte(findCell(pPage, pCur->idx));
drh8178a752003-01-05 21:41:40 +00002566 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00002567 if( rc ) return rc;
2568 }
2569 return SQLITE_OK;
2570}
2571
drh2dcc9aa2002-12-04 13:40:25 +00002572/*
2573** Move the cursor down to the right-most leaf entry beneath the
2574** page to which it is currently pointing. Notice the difference
2575** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
2576** finds the left-most entry beneath the *entry* whereas moveToRightmost()
2577** finds the right-most entry beneath the *page*.
2578*/
2579static int moveToRightmost(BtCursor *pCur){
2580 Pgno pgno;
2581 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002582 MemPage *pPage;
drh2dcc9aa2002-12-04 13:40:25 +00002583
drhc39e0002004-05-07 23:50:57 +00002584 assert( pCur->isValid );
drh3aac2dd2004-04-26 14:10:20 +00002585 while( !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:49 +00002586 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh3aac2dd2004-04-26 14:10:20 +00002587 pCur->idx = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00002588 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00002589 if( rc ) return rc;
2590 }
drh3aac2dd2004-04-26 14:10:20 +00002591 pCur->idx = pPage->nCell - 1;
drh271efa52004-05-30 19:19:05 +00002592 pCur->info.nSize = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002593 return SQLITE_OK;
2594}
2595
drh5e00f6c2001-09-13 13:46:56 +00002596/* Move the cursor to the first entry in the table. Return SQLITE_OK
2597** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00002598** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00002599*/
drh3aac2dd2004-04-26 14:10:20 +00002600int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00002601 int rc;
drhc39e0002004-05-07 23:50:57 +00002602 if( pCur->status ){
2603 return pCur->status;
2604 }
drh5e00f6c2001-09-13 13:46:56 +00002605 rc = moveToRoot(pCur);
2606 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002607 if( pCur->isValid==0 ){
2608 assert( pCur->pPage->nCell==0 );
drh5e00f6c2001-09-13 13:46:56 +00002609 *pRes = 1;
2610 return SQLITE_OK;
2611 }
drhc39e0002004-05-07 23:50:57 +00002612 assert( pCur->pPage->nCell>0 );
drh5e00f6c2001-09-13 13:46:56 +00002613 *pRes = 0;
2614 rc = moveToLeftmost(pCur);
2615 return rc;
2616}
drh5e2f8b92001-05-28 00:41:15 +00002617
drh9562b552002-02-19 15:00:07 +00002618/* Move the cursor to the last entry in the table. Return SQLITE_OK
2619** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00002620** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00002621*/
drh3aac2dd2004-04-26 14:10:20 +00002622int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00002623 int rc;
drhc39e0002004-05-07 23:50:57 +00002624 if( pCur->status ){
2625 return pCur->status;
2626 }
drh9562b552002-02-19 15:00:07 +00002627 rc = moveToRoot(pCur);
2628 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002629 if( pCur->isValid==0 ){
2630 assert( pCur->pPage->nCell==0 );
drh9562b552002-02-19 15:00:07 +00002631 *pRes = 1;
2632 return SQLITE_OK;
2633 }
drhc39e0002004-05-07 23:50:57 +00002634 assert( pCur->isValid );
drh9562b552002-02-19 15:00:07 +00002635 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002636 rc = moveToRightmost(pCur);
drh9562b552002-02-19 15:00:07 +00002637 return rc;
2638}
2639
drh3aac2dd2004-04-26 14:10:20 +00002640/* Move the cursor so that it points to an entry near pKey/nKey.
drh72f82862001-05-24 21:06:34 +00002641** Return a success code.
2642**
drh3aac2dd2004-04-26 14:10:20 +00002643** For INTKEY tables, only the nKey parameter is used. pKey is
2644** ignored. For other tables, nKey is the number of bytes of data
2645** in nKey. The comparison function specified when the cursor was
2646** created is used to compare keys.
2647**
drh5e2f8b92001-05-28 00:41:15 +00002648** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00002649** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00002650** were present. The cursor might point to an entry that comes
2651** before or after the key.
2652**
drhbd03cae2001-06-02 02:40:57 +00002653** The result of comparing the key with the entry to which the
drhab01f612004-05-22 02:55:23 +00002654** cursor is written to *pRes if pRes!=NULL. The meaning of
drhbd03cae2001-06-02 02:40:57 +00002655** this value is as follows:
2656**
2657** *pRes<0 The cursor is left pointing at an entry that
drh1a844c32002-12-04 22:29:28 +00002658** is smaller than pKey or if the table is empty
2659** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00002660**
2661** *pRes==0 The cursor is left pointing at an entry that
2662** exactly matches pKey.
2663**
2664** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00002665** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00002666*/
drh4a1c3802004-05-12 15:15:47 +00002667int sqlite3BtreeMoveto(BtCursor *pCur, const void *pKey, i64 nKey, int *pRes){
drh72f82862001-05-24 21:06:34 +00002668 int rc;
drhc39e0002004-05-07 23:50:57 +00002669
2670 if( pCur->status ){
2671 return pCur->status;
2672 }
drh5e2f8b92001-05-28 00:41:15 +00002673 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00002674 if( rc ) return rc;
drhc39e0002004-05-07 23:50:57 +00002675 assert( pCur->pPage );
2676 assert( pCur->pPage->isInit );
2677 if( pCur->isValid==0 ){
drhf328bc82004-05-10 23:29:49 +00002678 *pRes = -1;
drhc39e0002004-05-07 23:50:57 +00002679 assert( pCur->pPage->nCell==0 );
2680 return SQLITE_OK;
2681 }
drh72f82862001-05-24 21:06:34 +00002682 for(;;){
2683 int lwr, upr;
2684 Pgno chldPg;
2685 MemPage *pPage = pCur->pPage;
drh1a844c32002-12-04 22:29:28 +00002686 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00002687 lwr = 0;
2688 upr = pPage->nCell-1;
drhda200cc2004-05-09 11:51:38 +00002689 pageIntegrity(pPage);
drh72f82862001-05-24 21:06:34 +00002690 while( lwr<=upr ){
danielk197713adf8a2004-06-03 16:08:41 +00002691 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00002692 i64 nCellKey;
drh72f82862001-05-24 21:06:34 +00002693 pCur->idx = (lwr+upr)/2;
drh271efa52004-05-30 19:19:05 +00002694 pCur->info.nSize = 0;
drhde647132004-05-07 17:57:49 +00002695 sqlite3BtreeKeySize(pCur, &nCellKey);
drh3aac2dd2004-04-26 14:10:20 +00002696 if( pPage->intKey ){
2697 if( nCellKey<nKey ){
2698 c = -1;
2699 }else if( nCellKey>nKey ){
2700 c = +1;
2701 }else{
2702 c = 0;
2703 }
drh3aac2dd2004-04-26 14:10:20 +00002704 }else{
drhe51c44f2004-05-30 20:46:09 +00002705 int available;
danielk197713adf8a2004-06-03 16:08:41 +00002706 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drhe51c44f2004-05-30 20:46:09 +00002707 if( available>=nCellKey ){
2708 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
2709 }else{
2710 pCellKey = sqliteMallocRaw( nCellKey );
2711 if( pCellKey==0 ) return SQLITE_NOMEM;
danielk197713adf8a2004-06-03 16:08:41 +00002712 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
drhe51c44f2004-05-30 20:46:09 +00002713 c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey);
2714 sqliteFree(pCellKey);
2715 if( rc ) return rc;
2716 }
drh3aac2dd2004-04-26 14:10:20 +00002717 }
drh72f82862001-05-24 21:06:34 +00002718 if( c==0 ){
drh8b18dd42004-05-12 19:18:15 +00002719 if( pPage->leafData && !pPage->leaf ){
drhfc70e6f2004-05-12 21:11:27 +00002720 lwr = pCur->idx;
2721 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00002722 break;
2723 }else{
drh8b18dd42004-05-12 19:18:15 +00002724 if( pRes ) *pRes = 0;
2725 return SQLITE_OK;
2726 }
drh72f82862001-05-24 21:06:34 +00002727 }
2728 if( c<0 ){
2729 lwr = pCur->idx+1;
2730 }else{
2731 upr = pCur->idx-1;
2732 }
2733 }
2734 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00002735 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00002736 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00002737 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00002738 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00002739 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00002740 }else{
drh43605152004-05-29 21:46:49 +00002741 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00002742 }
2743 if( chldPg==0 ){
drhc39e0002004-05-07 23:50:57 +00002744 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00002745 if( pRes ) *pRes = c;
2746 return SQLITE_OK;
2747 }
drh428ae8c2003-01-04 16:48:09 +00002748 pCur->idx = lwr;
drh271efa52004-05-30 19:19:05 +00002749 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00002750 rc = moveToChild(pCur, chldPg);
drhc39e0002004-05-07 23:50:57 +00002751 if( rc ){
2752 return rc;
2753 }
drh72f82862001-05-24 21:06:34 +00002754 }
drhbd03cae2001-06-02 02:40:57 +00002755 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00002756}
2757
2758/*
drhc39e0002004-05-07 23:50:57 +00002759** Return TRUE if the cursor is not pointing at an entry of the table.
2760**
2761** TRUE will be returned after a call to sqlite3BtreeNext() moves
2762** past the last entry in the table or sqlite3BtreePrev() moves past
2763** the first entry. TRUE is also returned if the table is empty.
2764*/
2765int sqlite3BtreeEof(BtCursor *pCur){
2766 return pCur->isValid==0;
2767}
2768
2769/*
drhbd03cae2001-06-02 02:40:57 +00002770** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00002771** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00002772** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00002773** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00002774*/
drh3aac2dd2004-04-26 14:10:20 +00002775int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00002776 int rc;
drh8178a752003-01-05 21:41:40 +00002777 MemPage *pPage = pCur->pPage;
drh8b18dd42004-05-12 19:18:15 +00002778
drh8c1238a2003-01-02 14:43:55 +00002779 assert( pRes!=0 );
drhc39e0002004-05-07 23:50:57 +00002780 if( pCur->isValid==0 ){
drh8c1238a2003-01-02 14:43:55 +00002781 *pRes = 1;
drhc39e0002004-05-07 23:50:57 +00002782 return SQLITE_OK;
drhecdc7532001-09-23 02:35:53 +00002783 }
drh8178a752003-01-05 21:41:40 +00002784 assert( pPage->isInit );
drh8178a752003-01-05 21:41:40 +00002785 assert( pCur->idx<pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00002786
drh72f82862001-05-24 21:06:34 +00002787 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00002788 pCur->info.nSize = 0;
drh8178a752003-01-05 21:41:40 +00002789 if( pCur->idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00002790 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00002791 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00002792 if( rc ) return rc;
2793 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00002794 *pRes = 0;
2795 return rc;
drh72f82862001-05-24 21:06:34 +00002796 }
drh5e2f8b92001-05-28 00:41:15 +00002797 do{
drh8856d6a2004-04-29 14:42:46 +00002798 if( isRootPage(pPage) ){
drh8c1238a2003-01-02 14:43:55 +00002799 *pRes = 1;
drhc39e0002004-05-07 23:50:57 +00002800 pCur->isValid = 0;
drh5e2f8b92001-05-28 00:41:15 +00002801 return SQLITE_OK;
2802 }
drh8178a752003-01-05 21:41:40 +00002803 moveToParent(pCur);
2804 pPage = pCur->pPage;
2805 }while( pCur->idx>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00002806 *pRes = 0;
drh8b18dd42004-05-12 19:18:15 +00002807 if( pPage->leafData ){
2808 rc = sqlite3BtreeNext(pCur, pRes);
2809 }else{
2810 rc = SQLITE_OK;
2811 }
2812 return rc;
drh8178a752003-01-05 21:41:40 +00002813 }
2814 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00002815 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00002816 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00002817 }
drh5e2f8b92001-05-28 00:41:15 +00002818 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00002819 return rc;
drh72f82862001-05-24 21:06:34 +00002820}
2821
drh3b7511c2001-05-26 13:15:44 +00002822/*
drh2dcc9aa2002-12-04 13:40:25 +00002823** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00002824** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00002825** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00002826** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00002827*/
drh3aac2dd2004-04-26 14:10:20 +00002828int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00002829 int rc;
2830 Pgno pgno;
drh8178a752003-01-05 21:41:40 +00002831 MemPage *pPage;
drhc39e0002004-05-07 23:50:57 +00002832 if( pCur->isValid==0 ){
2833 *pRes = 1;
2834 return SQLITE_OK;
2835 }
danielk19776a43f9b2004-11-16 04:57:24 +00002836
drh8178a752003-01-05 21:41:40 +00002837 pPage = pCur->pPage;
drh8178a752003-01-05 21:41:40 +00002838 assert( pPage->isInit );
drh2dcc9aa2002-12-04 13:40:25 +00002839 assert( pCur->idx>=0 );
drha34b6762004-05-07 13:30:42 +00002840 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00002841 pgno = get4byte( findCell(pPage, pCur->idx) );
drh8178a752003-01-05 21:41:40 +00002842 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00002843 if( rc ) return rc;
2844 rc = moveToRightmost(pCur);
2845 }else{
2846 while( pCur->idx==0 ){
drh8856d6a2004-04-29 14:42:46 +00002847 if( isRootPage(pPage) ){
drhc39e0002004-05-07 23:50:57 +00002848 pCur->isValid = 0;
2849 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00002850 return SQLITE_OK;
2851 }
drh8178a752003-01-05 21:41:40 +00002852 moveToParent(pCur);
2853 pPage = pCur->pPage;
drh2dcc9aa2002-12-04 13:40:25 +00002854 }
2855 pCur->idx--;
drh271efa52004-05-30 19:19:05 +00002856 pCur->info.nSize = 0;
drh8237d452004-11-22 19:07:09 +00002857 if( pPage->leafData && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00002858 rc = sqlite3BtreePrevious(pCur, pRes);
2859 }else{
2860 rc = SQLITE_OK;
2861 }
drh2dcc9aa2002-12-04 13:40:25 +00002862 }
drh8178a752003-01-05 21:41:40 +00002863 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00002864 return rc;
2865}
2866
2867/*
drh3b7511c2001-05-26 13:15:44 +00002868** Allocate a new page from the database file.
2869**
drha34b6762004-05-07 13:30:42 +00002870** The new page is marked as dirty. (In other words, sqlite3pager_write()
drh3b7511c2001-05-26 13:15:44 +00002871** has already been called on the new page.) The new page has also
2872** been referenced and the calling routine is responsible for calling
drha34b6762004-05-07 13:30:42 +00002873** sqlite3pager_unref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00002874**
2875** SQLITE_OK is returned on success. Any other return value indicates
2876** an error. *ppPage and *pPgno are undefined in the event of an error.
drha34b6762004-05-07 13:30:42 +00002877** Do not invoke sqlite3pager_unref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00002878**
drh199e3cf2002-07-18 11:01:47 +00002879** If the "nearby" parameter is not 0, then a (feeble) effort is made to
2880** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00002881** attempt to keep related pages close to each other in the database file,
2882** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00002883**
2884** If the "exact" parameter is not 0, and the page-number nearby exists
2885** anywhere on the free-list, then it is guarenteed to be returned. This
2886** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00002887*/
danielk1977cb1a7eb2004-11-05 12:27:02 +00002888static int allocatePage(
2889 Btree *pBt,
2890 MemPage **ppPage,
2891 Pgno *pPgno,
2892 Pgno nearby,
2893 u8 exact
2894){
drh3aac2dd2004-04-26 14:10:20 +00002895 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00002896 int rc;
drh3aac2dd2004-04-26 14:10:20 +00002897 int n; /* Number of pages on the freelist */
2898 int k; /* Number of leaves on the trunk of the freelist */
drh30e58752002-03-02 20:41:57 +00002899
drh3aac2dd2004-04-26 14:10:20 +00002900 pPage1 = pBt->pPage1;
2901 n = get4byte(&pPage1->aData[36]);
2902 if( n>0 ){
drh91025292004-05-03 19:49:32 +00002903 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00002904 MemPage *pTrunk = 0;
2905 Pgno iTrunk;
2906 MemPage *pPrevTrunk = 0;
2907 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
2908
2909 /* If the 'exact' parameter was true and a query of the pointer-map
2910 ** shows that the page 'nearby' is somewhere on the free-list, then
2911 ** the entire-list will be searched for that page.
2912 */
2913#ifndef SQLITE_OMIT_AUTOVACUUM
2914 if( exact ){
2915 u8 eType;
2916 assert( nearby>0 );
2917 assert( pBt->autoVacuum );
2918 rc = ptrmapGet(pBt, nearby, &eType, 0);
2919 if( rc ) return rc;
2920 if( eType==PTRMAP_FREEPAGE ){
2921 searchList = 1;
2922 }
2923 *pPgno = nearby;
2924 }
2925#endif
2926
2927 /* Decrement the free-list count by 1. Set iTrunk to the index of the
2928 ** first free-list trunk page. iPrevTrunk is initially 1.
2929 */
drha34b6762004-05-07 13:30:42 +00002930 rc = sqlite3pager_write(pPage1->aData);
drh3b7511c2001-05-26 13:15:44 +00002931 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00002932 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00002933
2934 /* The code within this loop is run only once if the 'searchList' variable
2935 ** is not true. Otherwise, it runs once for each trunk-page on the
2936 ** free-list until the page 'nearby' is located.
2937 */
2938 do {
2939 pPrevTrunk = pTrunk;
2940 if( pPrevTrunk ){
2941 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00002942 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00002943 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00002944 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00002945 rc = getPage(pBt, iTrunk, &pTrunk);
2946 if( rc ){
2947 releasePage(pPrevTrunk);
2948 return rc;
2949 }
2950
2951 /* TODO: This should move to after the loop? */
2952 rc = sqlite3pager_write(pTrunk->aData);
2953 if( rc ){
2954 releasePage(pTrunk);
2955 releasePage(pPrevTrunk);
2956 return rc;
2957 }
2958
2959 k = get4byte(&pTrunk->aData[4]);
2960 if( k==0 && !searchList ){
2961 /* The trunk has no leaves and the list is not being searched.
2962 ** So extract the trunk page itself and use it as the newly
2963 ** allocated page */
2964 assert( pPrevTrunk==0 );
2965 *pPgno = iTrunk;
2966 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
2967 *ppPage = pTrunk;
2968 pTrunk = 0;
2969 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
2970 }else if( k>pBt->usableSize/4 - 8 ){
2971 /* Value of k is out of range. Database corruption */
drhee696e22004-08-30 16:52:17 +00002972 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
danielk1977cb1a7eb2004-11-05 12:27:02 +00002973#ifndef SQLITE_OMIT_AUTOVACUUM
2974 }else if( searchList && nearby==iTrunk ){
2975 /* The list is being searched and this trunk page is the page
2976 ** to allocate, regardless of whether it has leaves.
2977 */
2978 assert( *pPgno==iTrunk );
2979 *ppPage = pTrunk;
2980 searchList = 0;
2981 if( k==0 ){
2982 if( !pPrevTrunk ){
2983 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
2984 }else{
2985 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
2986 }
2987 }else{
2988 /* The trunk page is required by the caller but it contains
2989 ** pointers to free-list leaves. The first leaf becomes a trunk
2990 ** page in this case.
2991 */
2992 MemPage *pNewTrunk;
2993 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
2994 rc = getPage(pBt, iNewTrunk, &pNewTrunk);
2995 if( rc!=SQLITE_OK ){
2996 releasePage(pTrunk);
2997 releasePage(pPrevTrunk);
2998 return rc;
2999 }
3000 rc = sqlite3pager_write(pNewTrunk->aData);
3001 if( rc!=SQLITE_OK ){
3002 releasePage(pNewTrunk);
3003 releasePage(pTrunk);
3004 releasePage(pPrevTrunk);
3005 return rc;
3006 }
3007 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
3008 put4byte(&pNewTrunk->aData[4], k-1);
3009 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
3010 if( !pPrevTrunk ){
3011 put4byte(&pPage1->aData[32], iNewTrunk);
3012 }else{
3013 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
3014 }
3015 releasePage(pNewTrunk);
3016 }
3017 pTrunk = 0;
3018 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
3019#endif
3020 }else{
3021 /* Extract a leaf from the trunk */
3022 int closest;
3023 Pgno iPage;
3024 unsigned char *aData = pTrunk->aData;
3025 if( nearby>0 ){
3026 int i, dist;
3027 closest = 0;
3028 dist = get4byte(&aData[8]) - nearby;
3029 if( dist<0 ) dist = -dist;
3030 for(i=1; i<k; i++){
3031 int d2 = get4byte(&aData[8+i*4]) - nearby;
3032 if( d2<0 ) d2 = -d2;
3033 if( d2<dist ){
3034 closest = i;
3035 dist = d2;
3036 }
3037 }
3038 }else{
3039 closest = 0;
3040 }
3041
3042 iPage = get4byte(&aData[8+closest*4]);
3043 if( !searchList || iPage==nearby ){
3044 *pPgno = iPage;
3045 if( *pPgno>sqlite3pager_pagecount(pBt->pPager) ){
3046 /* Free page off the end of the file */
3047 return SQLITE_CORRUPT; /* bkpt-CORRUPT */
3048 }
3049 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
3050 ": %d more free pages\n",
3051 *pPgno, closest+1, k, pTrunk->pgno, n-1));
3052 if( closest<k-1 ){
3053 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
3054 }
3055 put4byte(&aData[4], k-1);
3056 rc = getPage(pBt, *pPgno, ppPage);
3057 if( rc==SQLITE_OK ){
3058 sqlite3pager_dont_rollback((*ppPage)->aData);
3059 rc = sqlite3pager_write((*ppPage)->aData);
3060 }
3061 searchList = 0;
3062 }
drhee696e22004-08-30 16:52:17 +00003063 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00003064 releasePage(pPrevTrunk);
3065 }while( searchList );
3066 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003067 }else{
drh3aac2dd2004-04-26 14:10:20 +00003068 /* There are no pages on the freelist, so create a new page at the
3069 ** end of the file */
drha34b6762004-05-07 13:30:42 +00003070 *pPgno = sqlite3pager_pagecount(pBt->pPager) + 1;
danielk1977afcdd022004-10-31 16:25:42 +00003071
3072#ifndef SQLITE_OMIT_AUTOVACUUM
drh42cac6d2004-11-20 20:31:11 +00003073 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt->usableSize, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00003074 /* If *pPgno refers to a pointer-map page, allocate two new pages
3075 ** at the end of the file instead of one. The first allocated page
3076 ** becomes a new pointer-map page, the second is used by the caller.
3077 */
3078 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00003079 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00003080 (*pPgno)++;
3081 }
3082#endif
3083
danielk1977599fcba2004-11-08 07:13:13 +00003084 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00003085 rc = getPage(pBt, *pPgno, ppPage);
drh3b7511c2001-05-26 13:15:44 +00003086 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00003087 rc = sqlite3pager_write((*ppPage)->aData);
drh3a4c1412004-05-09 20:40:11 +00003088 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00003089 }
danielk1977599fcba2004-11-08 07:13:13 +00003090
3091 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh3b7511c2001-05-26 13:15:44 +00003092 return rc;
3093}
3094
3095/*
drh3aac2dd2004-04-26 14:10:20 +00003096** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00003097**
drha34b6762004-05-07 13:30:42 +00003098** sqlite3pager_unref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00003099*/
drh3aac2dd2004-04-26 14:10:20 +00003100static int freePage(MemPage *pPage){
3101 Btree *pBt = pPage->pBt;
3102 MemPage *pPage1 = pBt->pPage1;
3103 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00003104
drh3aac2dd2004-04-26 14:10:20 +00003105 /* Prepare the page for freeing */
3106 assert( pPage->pgno>1 );
3107 pPage->isInit = 0;
3108 releasePage(pPage->pParent);
3109 pPage->pParent = 0;
3110
drha34b6762004-05-07 13:30:42 +00003111 /* Increment the free page count on pPage1 */
3112 rc = sqlite3pager_write(pPage1->aData);
drh3aac2dd2004-04-26 14:10:20 +00003113 if( rc ) return rc;
3114 n = get4byte(&pPage1->aData[36]);
3115 put4byte(&pPage1->aData[36], n+1);
3116
danielk1977687566d2004-11-02 12:56:41 +00003117#ifndef SQLITE_OMIT_AUTOVACUUM
3118 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00003119 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00003120 */
3121 if( pBt->autoVacuum ){
3122 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
danielk1977a64a0352004-11-05 01:45:13 +00003123 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00003124 }
3125#endif
3126
drh3aac2dd2004-04-26 14:10:20 +00003127 if( n==0 ){
3128 /* This is the first free page */
drhda200cc2004-05-09 11:51:38 +00003129 rc = sqlite3pager_write(pPage->aData);
3130 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003131 memset(pPage->aData, 0, 8);
drha34b6762004-05-07 13:30:42 +00003132 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003133 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003134 }else{
3135 /* Other free pages already exist. Retrive the first trunk page
3136 ** of the freelist and find out how many leaves it has. */
drha34b6762004-05-07 13:30:42 +00003137 MemPage *pTrunk;
3138 rc = getPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003139 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003140 k = get4byte(&pTrunk->aData[4]);
drhee696e22004-08-30 16:52:17 +00003141 if( k>=pBt->usableSize/4 - 8 ){
drh3aac2dd2004-04-26 14:10:20 +00003142 /* The trunk is full. Turn the page being freed into a new
3143 ** trunk page with no leaves. */
drha34b6762004-05-07 13:30:42 +00003144 rc = sqlite3pager_write(pPage->aData);
drh3aac2dd2004-04-26 14:10:20 +00003145 if( rc ) return rc;
3146 put4byte(pPage->aData, pTrunk->pgno);
3147 put4byte(&pPage->aData[4], 0);
3148 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003149 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
3150 pPage->pgno, pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003151 }else{
3152 /* Add the newly freed page as a leaf on the current trunk */
drha34b6762004-05-07 13:30:42 +00003153 rc = sqlite3pager_write(pTrunk->aData);
drh3aac2dd2004-04-26 14:10:20 +00003154 if( rc ) return rc;
3155 put4byte(&pTrunk->aData[4], k+1);
3156 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drha34b6762004-05-07 13:30:42 +00003157 sqlite3pager_dont_write(pBt->pPager, pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00003158 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00003159 }
3160 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00003161 }
drh3b7511c2001-05-26 13:15:44 +00003162 return rc;
3163}
3164
3165/*
drh3aac2dd2004-04-26 14:10:20 +00003166** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00003167*/
drh3aac2dd2004-04-26 14:10:20 +00003168static int clearCell(MemPage *pPage, unsigned char *pCell){
3169 Btree *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00003170 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00003171 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00003172 int rc;
drh3b7511c2001-05-26 13:15:44 +00003173
drh43605152004-05-29 21:46:49 +00003174 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003175 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00003176 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00003177 }
drh6f11bef2004-05-13 01:12:56 +00003178 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh3aac2dd2004-04-26 14:10:20 +00003179 while( ovflPgno!=0 ){
3180 MemPage *pOvfl;
3181 rc = getPage(pBt, ovflPgno, &pOvfl);
drh3b7511c2001-05-26 13:15:44 +00003182 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003183 ovflPgno = get4byte(pOvfl->aData);
drha34b6762004-05-07 13:30:42 +00003184 rc = freePage(pOvfl);
drhbd03cae2001-06-02 02:40:57 +00003185 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00003186 sqlite3pager_unref(pOvfl->aData);
drh3b7511c2001-05-26 13:15:44 +00003187 }
drh5e2f8b92001-05-28 00:41:15 +00003188 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00003189}
3190
3191/*
drh91025292004-05-03 19:49:32 +00003192** Create the byte sequence used to represent a cell on page pPage
3193** and write that byte sequence into pCell[]. Overflow pages are
3194** allocated and filled in as necessary. The calling procedure
3195** is responsible for making sure sufficient space has been allocated
3196** for pCell[].
3197**
3198** Note that pCell does not necessary need to point to the pPage->aData
3199** area. pCell might point to some temporary storage. The cell will
3200** be constructed in this temporary area then copied into pPage->aData
3201** later.
drh3b7511c2001-05-26 13:15:44 +00003202*/
3203static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00003204 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00003205 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00003206 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00003207 const void *pData,int nData, /* The data */
3208 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00003209){
drh3b7511c2001-05-26 13:15:44 +00003210 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00003211 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00003212 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00003213 int spaceLeft;
3214 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00003215 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00003216 unsigned char *pPrior;
3217 unsigned char *pPayload;
3218 Btree *pBt = pPage->pBt;
3219 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00003220 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00003221 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00003222
drh91025292004-05-03 19:49:32 +00003223 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00003224 nHeader = 0;
drh91025292004-05-03 19:49:32 +00003225 if( !pPage->leaf ){
3226 nHeader += 4;
3227 }
drh8b18dd42004-05-12 19:18:15 +00003228 if( pPage->hasData ){
drh91025292004-05-03 19:49:32 +00003229 nHeader += putVarint(&pCell[nHeader], nData);
drh6f11bef2004-05-13 01:12:56 +00003230 }else{
drh91025292004-05-03 19:49:32 +00003231 nData = 0;
3232 }
drh6f11bef2004-05-13 01:12:56 +00003233 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh43605152004-05-29 21:46:49 +00003234 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00003235 assert( info.nHeader==nHeader );
3236 assert( info.nKey==nKey );
3237 assert( info.nData==nData );
3238
3239 /* Fill in the payload */
drh3aac2dd2004-04-26 14:10:20 +00003240 nPayload = nData;
3241 if( pPage->intKey ){
3242 pSrc = pData;
3243 nSrc = nData;
drh91025292004-05-03 19:49:32 +00003244 nData = 0;
drh3aac2dd2004-04-26 14:10:20 +00003245 }else{
3246 nPayload += nKey;
3247 pSrc = pKey;
3248 nSrc = nKey;
3249 }
drh6f11bef2004-05-13 01:12:56 +00003250 *pnSize = info.nSize;
3251 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00003252 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00003253 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00003254
drh3b7511c2001-05-26 13:15:44 +00003255 while( nPayload>0 ){
3256 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00003257#ifndef SQLITE_OMIT_AUTOVACUUM
3258 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
3259#endif
danielk1977cb1a7eb2004-11-05 12:27:02 +00003260 rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00003261#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00003262 /* If the database supports auto-vacuum, and the second or subsequent
3263 ** overflow page is being allocated, add an entry to the pointer-map
3264 ** for that page now. The entry for the first overflow page will be
3265 ** added later, by the insertCell() routine.
danielk1977afcdd022004-10-31 16:25:42 +00003266 */
danielk1977a19df672004-11-03 11:37:07 +00003267 if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
3268 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap);
danielk1977afcdd022004-10-31 16:25:42 +00003269 }
3270#endif
drh3b7511c2001-05-26 13:15:44 +00003271 if( rc ){
drh9b171272004-05-08 02:03:22 +00003272 releasePage(pToRelease);
drh3aac2dd2004-04-26 14:10:20 +00003273 clearCell(pPage, pCell);
drh3b7511c2001-05-26 13:15:44 +00003274 return rc;
3275 }
drh3aac2dd2004-04-26 14:10:20 +00003276 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00003277 releasePage(pToRelease);
3278 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00003279 pPrior = pOvfl->aData;
3280 put4byte(pPrior, 0);
3281 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00003282 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00003283 }
3284 n = nPayload;
3285 if( n>spaceLeft ) n = spaceLeft;
drh3aac2dd2004-04-26 14:10:20 +00003286 if( n>nSrc ) n = nSrc;
3287 memcpy(pPayload, pSrc, n);
drh3b7511c2001-05-26 13:15:44 +00003288 nPayload -= n;
drhde647132004-05-07 17:57:49 +00003289 pPayload += n;
drh9b171272004-05-08 02:03:22 +00003290 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00003291 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00003292 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00003293 if( nSrc==0 ){
3294 nSrc = nData;
3295 pSrc = pData;
3296 }
drhdd793422001-06-28 01:54:48 +00003297 }
drh9b171272004-05-08 02:03:22 +00003298 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00003299 return SQLITE_OK;
3300}
3301
3302/*
drhbd03cae2001-06-02 02:40:57 +00003303** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00003304** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00003305** pointer in the third argument.
3306*/
danielk1977afcdd022004-10-31 16:25:42 +00003307static int reparentPage(Btree *pBt, Pgno pgno, MemPage *pNewParent, int idx){
drhbd03cae2001-06-02 02:40:57 +00003308 MemPage *pThis;
drh4b70f112004-05-02 21:12:19 +00003309 unsigned char *aData;
drhbd03cae2001-06-02 02:40:57 +00003310
danielk1977afcdd022004-10-31 16:25:42 +00003311 if( pgno==0 ) return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00003312 assert( pBt->pPager!=0 );
drha34b6762004-05-07 13:30:42 +00003313 aData = sqlite3pager_lookup(pBt->pPager, pgno);
drhda200cc2004-05-09 11:51:38 +00003314 if( aData ){
drh887dc4c2004-10-22 16:22:57 +00003315 pThis = (MemPage*)&aData[pBt->psAligned];
drh31276532004-09-27 12:20:52 +00003316 assert( pThis->aData==aData );
drhda200cc2004-05-09 11:51:38 +00003317 if( pThis->isInit ){
3318 if( pThis->pParent!=pNewParent ){
3319 if( pThis->pParent ) sqlite3pager_unref(pThis->pParent->aData);
3320 pThis->pParent = pNewParent;
3321 if( pNewParent ) sqlite3pager_ref(pNewParent->aData);
3322 }
3323 pThis->idxParent = idx;
drhdd793422001-06-28 01:54:48 +00003324 }
drha34b6762004-05-07 13:30:42 +00003325 sqlite3pager_unref(aData);
drhbd03cae2001-06-02 02:40:57 +00003326 }
danielk1977afcdd022004-10-31 16:25:42 +00003327
3328#ifndef SQLITE_OMIT_AUTOVACUUM
3329 if( pBt->autoVacuum ){
3330 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
3331 }
3332#endif
3333 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00003334}
3335
3336/*
drh4b70f112004-05-02 21:12:19 +00003337** Change the pParent pointer of all children of pPage to point back
3338** to pPage.
3339**
drhbd03cae2001-06-02 02:40:57 +00003340** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00003341** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00003342**
3343** This routine gets called after you memcpy() one page into
3344** another.
3345*/
danielk1977afcdd022004-10-31 16:25:42 +00003346static int reparentChildPages(MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00003347 int i;
danielk1977afcdd022004-10-31 16:25:42 +00003348 Btree *pBt = pPage->pBt;
3349 int rc = SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00003350
danielk1977afcdd022004-10-31 16:25:42 +00003351#ifdef SQLITE_OMIT_AUTOVACUUM
3352 if( pPage->leaf ) return SQLITE_OK;
3353#else
3354 if( !pBt->autoVacuum && pPage->leaf ) return SQLITE_OK;
3355#endif
3356
drhbd03cae2001-06-02 02:40:57 +00003357 for(i=0; i<pPage->nCell; i++){
danielk1977afcdd022004-10-31 16:25:42 +00003358 u8 *pCell = findCell(pPage, i);
3359 if( !pPage->leaf ){
3360 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
3361 if( rc!=SQLITE_OK ) return rc;
3362 }
3363#ifndef SQLITE_OMIT_AUTOVACUUM
3364 /* If the database supports auto-vacuum, then check each cell to see
3365 ** if it contains a pointer to an overflow page. If so, then the
3366 ** pointer-map must be updated accordingly.
3367 **
3368 ** TODO: This looks like quite an expensive thing to do. Investigate.
3369 */
3370 if( pBt->autoVacuum ){
3371 CellInfo info;
3372 parseCellPtr(pPage, pCell, &info);
danielk1977e80463b2004-11-03 03:01:16 +00003373 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
danielk1977afcdd022004-10-31 16:25:42 +00003374 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
danielk1977687566d2004-11-02 12:56:41 +00003375 rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977afcdd022004-10-31 16:25:42 +00003376 if( rc!=SQLITE_OK ) return rc;
3377 }
3378 }
3379#endif
drhbd03cae2001-06-02 02:40:57 +00003380 }
danielk1977afcdd022004-10-31 16:25:42 +00003381 if( !pPage->leaf ){
3382 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
3383 pPage, i);
3384 pPage->idxShift = 0;
3385 }
3386 return rc;
drh14acc042001-06-10 19:56:58 +00003387}
3388
3389/*
3390** Remove the i-th cell from pPage. This routine effects pPage only.
3391** The cell content is not freed or deallocated. It is assumed that
3392** the cell content has been copied someplace else. This routine just
3393** removes the reference to the cell from pPage.
3394**
3395** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00003396*/
drh4b70f112004-05-02 21:12:19 +00003397static void dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00003398 int i; /* Loop counter */
3399 int pc; /* Offset to cell content of cell being deleted */
3400 u8 *data; /* pPage->aData */
3401 u8 *ptr; /* Used to move bytes around within data[] */
3402
drh8c42ca92001-06-22 19:15:00 +00003403 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00003404 assert( sz==cellSize(pPage, idx) );
drha34b6762004-05-07 13:30:42 +00003405 assert( sqlite3pager_iswriteable(pPage->aData) );
drhda200cc2004-05-09 11:51:38 +00003406 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00003407 ptr = &data[pPage->cellOffset + 2*idx];
3408 pc = get2byte(ptr);
3409 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
drhde647132004-05-07 17:57:49 +00003410 freeSpace(pPage, pc, sz);
drh43605152004-05-29 21:46:49 +00003411 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
3412 ptr[0] = ptr[2];
3413 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00003414 }
3415 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00003416 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
3417 pPage->nFree += 2;
drh428ae8c2003-01-04 16:48:09 +00003418 pPage->idxShift = 1;
drh14acc042001-06-10 19:56:58 +00003419}
3420
3421/*
3422** Insert a new cell on pPage at cell index "i". pCell points to the
3423** content of the cell.
3424**
3425** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00003426** will not fit, then make a copy of the cell content into pTemp if
3427** pTemp is not null. Regardless of pTemp, allocate a new entry
3428** in pPage->aOvfl[] and make it point to the cell content (either
3429** in pTemp or the original pCell) and also record its index.
3430** Allocating a new entry in pPage->aCell[] implies that
3431** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00003432**
3433** If nSkip is non-zero, then do not copy the first nSkip bytes of the
3434** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00003435** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00003436** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00003437*/
danielk1977e80463b2004-11-03 03:01:16 +00003438static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00003439 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00003440 int i, /* New cell becomes the i-th cell of the page */
3441 u8 *pCell, /* Content of the new cell */
3442 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00003443 u8 *pTemp, /* Temp storage space for pCell, if needed */
3444 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00003445){
drh43605152004-05-29 21:46:49 +00003446 int idx; /* Where to write new cell content in data[] */
3447 int j; /* Loop counter */
3448 int top; /* First byte of content for any cell in data[] */
3449 int end; /* First byte past the last cell pointer in data[] */
3450 int ins; /* Index in data[] where new cell pointer is inserted */
3451 int hdr; /* Offset into data[] of the page header */
3452 int cellOffset; /* Address of first cell pointer in data[] */
3453 u8 *data; /* The content of the whole page */
3454 u8 *ptr; /* Used for moving information around in data[] */
3455
3456 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
3457 assert( sz==cellSizePtr(pPage, pCell) );
drha34b6762004-05-07 13:30:42 +00003458 assert( sqlite3pager_iswriteable(pPage->aData) );
drh43605152004-05-29 21:46:49 +00003459 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00003460 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00003461 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00003462 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00003463 }
drh43605152004-05-29 21:46:49 +00003464 j = pPage->nOverflow++;
3465 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
3466 pPage->aOvfl[j].pCell = pCell;
3467 pPage->aOvfl[j].idx = i;
3468 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00003469 }else{
drh43605152004-05-29 21:46:49 +00003470 data = pPage->aData;
3471 hdr = pPage->hdrOffset;
3472 top = get2byte(&data[hdr+5]);
3473 cellOffset = pPage->cellOffset;
3474 end = cellOffset + 2*pPage->nCell + 2;
3475 ins = cellOffset + 2*i;
3476 if( end > top - sz ){
3477 defragmentPage(pPage);
3478 top = get2byte(&data[hdr+5]);
3479 assert( end + sz <= top );
3480 }
3481 idx = allocateSpace(pPage, sz);
3482 assert( idx>0 );
3483 assert( end <= get2byte(&data[hdr+5]) );
3484 pPage->nCell++;
3485 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00003486 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00003487 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
3488 ptr[0] = ptr[-2];
3489 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00003490 }
drh43605152004-05-29 21:46:49 +00003491 put2byte(&data[ins], idx);
3492 put2byte(&data[hdr+3], pPage->nCell);
3493 pPage->idxShift = 1;
drhda200cc2004-05-09 11:51:38 +00003494 pageIntegrity(pPage);
danielk1977a19df672004-11-03 11:37:07 +00003495#ifndef SQLITE_OMIT_AUTOVACUUM
3496 if( pPage->pBt->autoVacuum ){
3497 /* The cell may contain a pointer to an overflow page. If so, write
3498 ** the entry for the overflow page into the pointer map.
3499 */
3500 CellInfo info;
3501 parseCellPtr(pPage, pCell, &info);
3502 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
3503 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
3504 int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
3505 if( rc!=SQLITE_OK ) return rc;
3506 }
3507 }
3508#endif
drh14acc042001-06-10 19:56:58 +00003509 }
danielk1977e80463b2004-11-03 03:01:16 +00003510
danielk1977e80463b2004-11-03 03:01:16 +00003511 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00003512}
3513
3514/*
drhfa1a98a2004-05-14 19:08:17 +00003515** Add a list of cells to a page. The page should be initially empty.
3516** The cells are guaranteed to fit on the page.
3517*/
3518static void assemblePage(
3519 MemPage *pPage, /* The page to be assemblied */
3520 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00003521 u8 **apCell, /* Pointers to cell bodies */
drhfa1a98a2004-05-14 19:08:17 +00003522 int *aSize /* Sizes of the cells */
3523){
3524 int i; /* Loop counter */
3525 int totalSize; /* Total size of all cells */
3526 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00003527 int cellptr; /* Address of next cell pointer */
3528 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00003529 u8 *data; /* Data for the page */
3530
drh43605152004-05-29 21:46:49 +00003531 assert( pPage->nOverflow==0 );
drhfa1a98a2004-05-14 19:08:17 +00003532 totalSize = 0;
3533 for(i=0; i<nCell; i++){
3534 totalSize += aSize[i];
3535 }
drh43605152004-05-29 21:46:49 +00003536 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00003537 assert( pPage->nCell==0 );
drh43605152004-05-29 21:46:49 +00003538 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00003539 data = pPage->aData;
3540 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00003541 put2byte(&data[hdr+3], nCell);
3542 cellbody = allocateSpace(pPage, totalSize);
3543 assert( cellbody>0 );
3544 assert( pPage->nFree >= 2*nCell );
3545 pPage->nFree -= 2*nCell;
drhfa1a98a2004-05-14 19:08:17 +00003546 for(i=0; i<nCell; i++){
drh43605152004-05-29 21:46:49 +00003547 put2byte(&data[cellptr], cellbody);
3548 memcpy(&data[cellbody], apCell[i], aSize[i]);
3549 cellptr += 2;
3550 cellbody += aSize[i];
drhfa1a98a2004-05-14 19:08:17 +00003551 }
drh43605152004-05-29 21:46:49 +00003552 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00003553 pPage->nCell = nCell;
drhfa1a98a2004-05-14 19:08:17 +00003554}
3555
drh14acc042001-06-10 19:56:58 +00003556/*
drhc3b70572003-01-04 19:44:07 +00003557** The following parameters determine how many adjacent pages get involved
3558** in a balancing operation. NN is the number of neighbors on either side
3559** of the page that participate in the balancing operation. NB is the
3560** total number of pages that participate, including the target page and
3561** NN neighbors on either side.
3562**
3563** The minimum value of NN is 1 (of course). Increasing NN above 1
3564** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
3565** in exchange for a larger degradation in INSERT and UPDATE performance.
3566** The value of NN appears to give the best results overall.
3567*/
3568#define NN 1 /* Number of neighbors on either side of pPage */
3569#define NB (NN*2+1) /* Total pages involved in the balance */
3570
drh43605152004-05-29 21:46:49 +00003571/* Forward reference */
3572static int balance(MemPage*);
3573
drhc3b70572003-01-04 19:44:07 +00003574/*
drhab01f612004-05-22 02:55:23 +00003575** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00003576** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00003577** Usually NN siblings on either side of pPage is used in the balancing,
3578** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00003579** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00003580** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00003581** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00003582**
drh0c6cc4e2004-06-15 02:13:26 +00003583** The number of siblings of pPage might be increased or decreased by one or
3584** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00003585** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00003586** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00003587** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00003588** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00003589**
drh8b2f49b2001-06-08 00:21:52 +00003590** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00003591** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00003592** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00003593** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00003594**
drh8c42ca92001-06-22 19:15:00 +00003595** In the course of balancing the siblings of pPage, the parent of pPage
3596** might become overfull or underfull. If that happens, then this routine
3597** is called recursively on the parent.
3598**
drh5e00f6c2001-09-13 13:46:56 +00003599** If this routine fails for any reason, it might leave the database
3600** in a corrupted state. So if this routine fails, the database should
3601** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00003602*/
drh43605152004-05-29 21:46:49 +00003603static int balance_nonroot(MemPage *pPage){
drh8b2f49b2001-06-08 00:21:52 +00003604 MemPage *pParent; /* The parent of pPage */
drh4b70f112004-05-02 21:12:19 +00003605 Btree *pBt; /* The whole database */
danielk1977cfe9a692004-06-16 12:00:29 +00003606 int nCell = 0; /* Number of cells in aCell[] */
drh8b2f49b2001-06-08 00:21:52 +00003607 int nOld; /* Number of pages in apOld[] */
3608 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00003609 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00003610 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00003611 int idx; /* Index of pPage in pParent->aCell[] */
3612 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00003613 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00003614 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00003615 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00003616 int usableSpace; /* Bytes in pPage beyond the header */
3617 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00003618 int subtotal; /* Subtotal of bytes in cells on one page */
drhb6f41482004-05-14 01:58:11 +00003619 int iSpace = 0; /* First unused byte of aSpace[] */
drh2e38c322004-09-03 18:38:44 +00003620 int mxCellPerPage; /* Maximum number of cells in one page */
drhc3b70572003-01-04 19:44:07 +00003621 MemPage *apOld[NB]; /* pPage and up to two siblings */
3622 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00003623 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00003624 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
3625 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drhc3b70572003-01-04 19:44:07 +00003626 int idxDiv[NB]; /* Indices of divider cells in pParent */
drh4b70f112004-05-02 21:12:19 +00003627 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00003628 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
3629 int szNew[NB+2]; /* Combined size of cells place on i-th page */
drh2e38c322004-09-03 18:38:44 +00003630 u8 **apCell; /* All cells begin balanced */
3631 int *szCell; /* Local size of all cells in apCell[] */
3632 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
3633 u8 *aSpace; /* Space to hold copies of dividers cells */
drh8b2f49b2001-06-08 00:21:52 +00003634
drh14acc042001-06-10 19:56:58 +00003635 /*
drh43605152004-05-29 21:46:49 +00003636 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00003637 */
drh3a4c1412004-05-09 20:40:11 +00003638 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00003639 assert( sqlite3pager_iswriteable(pPage->aData) );
drh4b70f112004-05-02 21:12:19 +00003640 pBt = pPage->pBt;
drh14acc042001-06-10 19:56:58 +00003641 pParent = pPage->pParent;
drh43605152004-05-29 21:46:49 +00003642 sqlite3pager_write(pParent->aData);
3643 assert( pParent );
3644 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00003645
3646 /*
3647 ** Allocate space for memory structures
3648 */
3649 mxCellPerPage = MX_CELL(pBt);
3650 apCell = sqliteMallocRaw(
3651 (mxCellPerPage+2)*NB*(sizeof(u8*)+sizeof(int))
3652 + sizeof(MemPage)*NB
drh887dc4c2004-10-22 16:22:57 +00003653 + pBt->psAligned*(5+NB)
drh2e38c322004-09-03 18:38:44 +00003654 );
3655 if( apCell==0 ){
3656 return SQLITE_NOMEM;
3657 }
3658 szCell = (int*)&apCell[(mxCellPerPage+2)*NB];
3659 aCopy[0] = (u8*)&szCell[(mxCellPerPage+2)*NB];
3660 for(i=1; i<NB; i++){
drh887dc4c2004-10-22 16:22:57 +00003661 aCopy[i] = &aCopy[i-1][pBt->psAligned+sizeof(MemPage)];
drh2e38c322004-09-03 18:38:44 +00003662 }
drh887dc4c2004-10-22 16:22:57 +00003663 aSpace = &aCopy[NB-1][pBt->psAligned+sizeof(MemPage)];
drh14acc042001-06-10 19:56:58 +00003664
drh8b2f49b2001-06-08 00:21:52 +00003665 /*
drh4b70f112004-05-02 21:12:19 +00003666 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00003667 ** to pPage. The "idx" variable is the index of that cell. If pPage
3668 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00003669 */
drhbb49aba2003-01-04 18:53:27 +00003670 if( pParent->idxShift ){
drha34b6762004-05-07 13:30:42 +00003671 Pgno pgno;
drh4b70f112004-05-02 21:12:19 +00003672 pgno = pPage->pgno;
drha34b6762004-05-07 13:30:42 +00003673 assert( pgno==sqlite3pager_pagenumber(pPage->aData) );
drhbb49aba2003-01-04 18:53:27 +00003674 for(idx=0; idx<pParent->nCell; idx++){
drh43605152004-05-29 21:46:49 +00003675 if( get4byte(findCell(pParent, idx))==pgno ){
drhbb49aba2003-01-04 18:53:27 +00003676 break;
3677 }
drh8b2f49b2001-06-08 00:21:52 +00003678 }
drh4b70f112004-05-02 21:12:19 +00003679 assert( idx<pParent->nCell
drh43605152004-05-29 21:46:49 +00003680 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
drhbb49aba2003-01-04 18:53:27 +00003681 }else{
3682 idx = pPage->idxParent;
drh8b2f49b2001-06-08 00:21:52 +00003683 }
drh8b2f49b2001-06-08 00:21:52 +00003684
3685 /*
drh14acc042001-06-10 19:56:58 +00003686 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00003687 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00003688 */
drh14acc042001-06-10 19:56:58 +00003689 nOld = nNew = 0;
drha34b6762004-05-07 13:30:42 +00003690 sqlite3pager_ref(pParent->aData);
drh14acc042001-06-10 19:56:58 +00003691
3692 /*
drh4b70f112004-05-02 21:12:19 +00003693 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00003694 ** the siblings. An attempt is made to find NN siblings on either
3695 ** side of pPage. More siblings are taken from one side, however, if
3696 ** pPage there are fewer than NN siblings on the other side. If pParent
3697 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00003698 */
drhc3b70572003-01-04 19:44:07 +00003699 nxDiv = idx - NN;
3700 if( nxDiv + NB > pParent->nCell ){
3701 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00003702 }
drhc3b70572003-01-04 19:44:07 +00003703 if( nxDiv<0 ){
3704 nxDiv = 0;
3705 }
drh8b2f49b2001-06-08 00:21:52 +00003706 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00003707 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00003708 if( k<pParent->nCell ){
3709 idxDiv[i] = k;
drh43605152004-05-29 21:46:49 +00003710 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00003711 nDiv++;
drha34b6762004-05-07 13:30:42 +00003712 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00003713 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00003714 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00003715 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00003716 }else{
3717 break;
drh8b2f49b2001-06-08 00:21:52 +00003718 }
drhde647132004-05-07 17:57:49 +00003719 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00003720 if( rc ) goto balance_cleanup;
drh428ae8c2003-01-04 16:48:09 +00003721 apOld[i]->idxParent = k;
drh91025292004-05-03 19:49:32 +00003722 apCopy[i] = 0;
3723 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00003724 nOld++;
drh8b2f49b2001-06-08 00:21:52 +00003725 }
3726
3727 /*
drh14acc042001-06-10 19:56:58 +00003728 ** Make copies of the content of pPage and its siblings into aOld[].
3729 ** The rest of this function will use data from the copies rather
3730 ** that the original pages since the original pages will be in the
3731 ** process of being overwritten.
3732 */
3733 for(i=0; i<nOld; i++){
drh887dc4c2004-10-22 16:22:57 +00003734 MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->psAligned];
3735 p->aData = &((u8*)p)[-pBt->psAligned];
3736 memcpy(p->aData, apOld[i]->aData, pBt->psAligned + sizeof(MemPage));
3737 p->aData = &((u8*)p)[-pBt->psAligned];
drh14acc042001-06-10 19:56:58 +00003738 }
3739
3740 /*
3741 ** Load pointers to all cells on sibling pages and the divider cells
3742 ** into the local apCell[] array. Make copies of the divider cells
drhb6f41482004-05-14 01:58:11 +00003743 ** into space obtained form aSpace[] and remove the the divider Cells
3744 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00003745 **
3746 ** If the siblings are on leaf pages, then the child pointers of the
3747 ** divider cells are stripped from the cells before they are copied
drh96f5b762004-05-16 16:24:36 +00003748 ** into aSpace[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00003749 ** child pointers. If siblings are not leaves, then all cell in
3750 ** apCell[] include child pointers. Either way, all cells in apCell[]
3751 ** are alike.
drh96f5b762004-05-16 16:24:36 +00003752 **
3753 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
3754 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00003755 */
3756 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00003757 leafCorrection = pPage->leaf*4;
drh8b18dd42004-05-12 19:18:15 +00003758 leafData = pPage->leafData && pPage->leaf;
drh8b2f49b2001-06-08 00:21:52 +00003759 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00003760 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00003761 int limit = pOld->nCell+pOld->nOverflow;
3762 for(j=0; j<limit; j++){
3763 apCell[nCell] = findOverflowCell(pOld, j);
3764 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
drh14acc042001-06-10 19:56:58 +00003765 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00003766 }
3767 if( i<nOld-1 ){
drh43605152004-05-29 21:46:49 +00003768 int sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00003769 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00003770 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
3771 ** are duplicates of keys on the child pages. We need to remove
3772 ** the divider cells from pParent, but the dividers cells are not
3773 ** added to apCell[] because they are duplicates of child cells.
3774 */
drh8b18dd42004-05-12 19:18:15 +00003775 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00003776 }else{
drhb6f41482004-05-14 01:58:11 +00003777 u8 *pTemp;
3778 szCell[nCell] = sz;
3779 pTemp = &aSpace[iSpace];
3780 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00003781 assert( iSpace<=pBt->psAligned*5 );
drhb6f41482004-05-14 01:58:11 +00003782 memcpy(pTemp, apDiv[i], sz);
3783 apCell[nCell] = pTemp+leafCorrection;
3784 dropCell(pParent, nxDiv, sz);
drh8b18dd42004-05-12 19:18:15 +00003785 szCell[nCell] -= leafCorrection;
drh43605152004-05-29 21:46:49 +00003786 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00003787 if( !pOld->leaf ){
3788 assert( leafCorrection==0 );
3789 /* The right pointer of the child page pOld becomes the left
3790 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00003791 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00003792 }else{
3793 assert( leafCorrection==4 );
3794 }
3795 nCell++;
drh4b70f112004-05-02 21:12:19 +00003796 }
drh8b2f49b2001-06-08 00:21:52 +00003797 }
3798 }
3799
3800 /*
drh6019e162001-07-02 17:51:45 +00003801 ** Figure out the number of pages needed to hold all nCell cells.
3802 ** Store this number in "k". Also compute szNew[] which is the total
3803 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00003804 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00003805 ** cntNew[k] should equal nCell.
3806 **
drh96f5b762004-05-16 16:24:36 +00003807 ** Values computed by this block:
3808 **
3809 ** k: The total number of sibling pages
3810 ** szNew[i]: Spaced used on the i-th sibling page.
3811 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
3812 ** the right of the i-th sibling page.
3813 ** usableSpace: Number of bytes of space available on each sibling.
3814 **
drh8b2f49b2001-06-08 00:21:52 +00003815 */
drh43605152004-05-29 21:46:49 +00003816 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00003817 for(subtotal=k=i=0; i<nCell; i++){
drh43605152004-05-29 21:46:49 +00003818 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00003819 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00003820 szNew[k] = subtotal - szCell[i];
3821 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00003822 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00003823 subtotal = 0;
3824 k++;
3825 }
3826 }
3827 szNew[k] = subtotal;
3828 cntNew[k] = nCell;
3829 k++;
drh96f5b762004-05-16 16:24:36 +00003830
3831 /*
3832 ** The packing computed by the previous block is biased toward the siblings
3833 ** on the left side. The left siblings are always nearly full, while the
3834 ** right-most sibling might be nearly empty. This block of code attempts
3835 ** to adjust the packing of siblings to get a better balance.
3836 **
3837 ** This adjustment is more than an optimization. The packing above might
3838 ** be so out of balance as to be illegal. For example, the right-most
3839 ** sibling might be completely empty. This adjustment is not optional.
3840 */
drh6019e162001-07-02 17:51:45 +00003841 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00003842 int szRight = szNew[i]; /* Size of sibling on the right */
3843 int szLeft = szNew[i-1]; /* Size of sibling on the left */
3844 int r; /* Index of right-most cell in left sibling */
3845 int d; /* Index of first cell to the left of right sibling */
3846
3847 r = cntNew[i-1] - 1;
3848 d = r + 1 - leafData;
drh43605152004-05-29 21:46:49 +00003849 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
3850 szRight += szCell[d] + 2;
3851 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00003852 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00003853 r = cntNew[i-1] - 1;
3854 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00003855 }
drh96f5b762004-05-16 16:24:36 +00003856 szNew[i] = szRight;
3857 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00003858 }
3859 assert( cntNew[0]>0 );
drh8b2f49b2001-06-08 00:21:52 +00003860
3861 /*
drh6b308672002-07-08 02:16:37 +00003862 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00003863 */
drh4b70f112004-05-02 21:12:19 +00003864 assert( pPage->pgno>1 );
3865 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00003866 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00003867 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00003868 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00003869 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00003870 pgnoNew[i] = pgnoOld[i];
3871 apOld[i] = 0;
drhda200cc2004-05-09 11:51:38 +00003872 sqlite3pager_write(pNew->aData);
drh6b308672002-07-08 02:16:37 +00003873 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00003874 rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00003875 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00003876 apNew[i] = pNew;
drh6b308672002-07-08 02:16:37 +00003877 }
drh14acc042001-06-10 19:56:58 +00003878 nNew++;
drhda200cc2004-05-09 11:51:38 +00003879 zeroPage(pNew, pageFlags);
drh8b2f49b2001-06-08 00:21:52 +00003880 }
3881
danielk1977299b1872004-11-22 10:02:10 +00003882 /* Free any old pages that were not reused as new pages.
3883 */
3884 while( i<nOld ){
3885 rc = freePage(apOld[i]);
3886 if( rc ) goto balance_cleanup;
3887 releasePage(apOld[i]);
3888 apOld[i] = 0;
3889 i++;
3890 }
3891
drh8b2f49b2001-06-08 00:21:52 +00003892 /*
drhf9ffac92002-03-02 19:00:31 +00003893 ** Put the new pages in accending order. This helps to
3894 ** keep entries in the disk file in order so that a scan
3895 ** of the table is a linear scan through the file. That
3896 ** in turn helps the operating system to deliver pages
3897 ** from the disk more rapidly.
3898 **
3899 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00003900 ** n is never more than NB (a small constant), that should
3901 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00003902 **
drhc3b70572003-01-04 19:44:07 +00003903 ** When NB==3, this one optimization makes the database
3904 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00003905 */
3906 for(i=0; i<k-1; i++){
3907 int minV = pgnoNew[i];
3908 int minI = i;
3909 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00003910 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00003911 minI = j;
3912 minV = pgnoNew[j];
3913 }
3914 }
3915 if( minI>i ){
3916 int t;
3917 MemPage *pT;
3918 t = pgnoNew[i];
3919 pT = apNew[i];
3920 pgnoNew[i] = pgnoNew[minI];
3921 apNew[i] = apNew[minI];
3922 pgnoNew[minI] = t;
3923 apNew[minI] = pT;
3924 }
3925 }
drha2fce642004-06-05 00:01:44 +00003926 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00003927 pgnoOld[0],
3928 nOld>=2 ? pgnoOld[1] : 0,
3929 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00003930 pgnoNew[0], szNew[0],
3931 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
3932 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00003933 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
3934 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00003935
drhf9ffac92002-03-02 19:00:31 +00003936 /*
drh14acc042001-06-10 19:56:58 +00003937 ** Evenly distribute the data in apCell[] across the new pages.
3938 ** Insert divider cells into pParent as necessary.
3939 */
3940 j = 0;
3941 for(i=0; i<nNew; i++){
3942 MemPage *pNew = apNew[i];
drh4b70f112004-05-02 21:12:19 +00003943 assert( pNew->pgno==pgnoNew[i] );
drhfa1a98a2004-05-14 19:08:17 +00003944 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
3945 j = cntNew[i];
drh6019e162001-07-02 17:51:45 +00003946 assert( pNew->nCell>0 );
drh43605152004-05-29 21:46:49 +00003947 assert( pNew->nOverflow==0 );
drh14acc042001-06-10 19:56:58 +00003948 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00003949 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00003950 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00003951 int sz;
3952 pCell = apCell[j];
3953 sz = szCell[j] + leafCorrection;
drh4b70f112004-05-02 21:12:19 +00003954 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00003955 memcpy(&pNew->aData[8], pCell, 4);
drh24cd67e2004-05-10 16:18:47 +00003956 pTemp = 0;
drh8b18dd42004-05-12 19:18:15 +00003957 }else if( leafData ){
drh6f11bef2004-05-13 01:12:56 +00003958 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00003959 j--;
drh43605152004-05-29 21:46:49 +00003960 parseCellPtr(pNew, apCell[j], &info);
drhb6f41482004-05-14 01:58:11 +00003961 pCell = &aSpace[iSpace];
drh6f11bef2004-05-13 01:12:56 +00003962 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, &sz);
drhb6f41482004-05-14 01:58:11 +00003963 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00003964 assert( iSpace<=pBt->psAligned*5 );
drh8b18dd42004-05-12 19:18:15 +00003965 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00003966 }else{
3967 pCell -= 4;
drhb6f41482004-05-14 01:58:11 +00003968 pTemp = &aSpace[iSpace];
3969 iSpace += sz;
drh887dc4c2004-10-22 16:22:57 +00003970 assert( iSpace<=pBt->psAligned*5 );
drh4b70f112004-05-02 21:12:19 +00003971 }
danielk1977a3ad5e72005-01-07 08:56:44 +00003972 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00003973 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh43605152004-05-29 21:46:49 +00003974 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
drh14acc042001-06-10 19:56:58 +00003975 j++;
3976 nxDiv++;
3977 }
3978 }
drh6019e162001-07-02 17:51:45 +00003979 assert( j==nCell );
drh4b70f112004-05-02 21:12:19 +00003980 if( (pageFlags & PTF_LEAF)==0 ){
drh43605152004-05-29 21:46:49 +00003981 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
drh14acc042001-06-10 19:56:58 +00003982 }
drh43605152004-05-29 21:46:49 +00003983 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00003984 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00003985 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00003986 }else{
3987 /* Right-most sibling is the left child of the first entry in pParent
3988 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00003989 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00003990 }
3991
3992 /*
3993 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00003994 */
3995 for(i=0; i<nNew; i++){
danielk1977afcdd022004-10-31 16:25:42 +00003996 rc = reparentChildPages(apNew[i]);
3997 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00003998 }
danielk1977afcdd022004-10-31 16:25:42 +00003999 rc = reparentChildPages(pParent);
4000 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00004001
4002 /*
drh3a4c1412004-05-09 20:40:11 +00004003 ** Balance the parent page. Note that the current page (pPage) might
4004 ** have been added to the freelist is it might no longer be initialized.
4005 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00004006 */
drhda200cc2004-05-09 11:51:38 +00004007 assert( pParent->isInit );
drh3a4c1412004-05-09 20:40:11 +00004008 /* assert( pPage->isInit ); // No! pPage might have been added to freelist */
4009 /* pageIntegrity(pPage); // No! pPage might have been added to freelist */
drh4b70f112004-05-02 21:12:19 +00004010 rc = balance(pParent);
drhda200cc2004-05-09 11:51:38 +00004011
drh8b2f49b2001-06-08 00:21:52 +00004012 /*
drh14acc042001-06-10 19:56:58 +00004013 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00004014 */
drh14acc042001-06-10 19:56:58 +00004015balance_cleanup:
drh2e38c322004-09-03 18:38:44 +00004016 sqliteFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00004017 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00004018 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00004019 }
drh14acc042001-06-10 19:56:58 +00004020 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00004021 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00004022 }
drh91025292004-05-03 19:49:32 +00004023 releasePage(pParent);
drh3a4c1412004-05-09 20:40:11 +00004024 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
4025 pPage->pgno, nOld, nNew, nCell));
drh8b2f49b2001-06-08 00:21:52 +00004026 return rc;
4027}
4028
4029/*
drh43605152004-05-29 21:46:49 +00004030** This routine is called for the root page of a btree when the root
4031** page contains no cells. This is an opportunity to make the tree
4032** shallower by one level.
4033*/
4034static int balance_shallower(MemPage *pPage){
4035 MemPage *pChild; /* The only child page of pPage */
4036 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00004037 int rc = SQLITE_OK; /* Return code from subprocedures */
4038 Btree *pBt; /* The main BTree structure */
4039 int mxCellPerPage; /* Maximum number of cells per page */
4040 u8 **apCell; /* All cells from pages being balanced */
4041 int *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00004042
4043 assert( pPage->pParent==0 );
4044 assert( pPage->nCell==0 );
drh2e38c322004-09-03 18:38:44 +00004045 pBt = pPage->pBt;
4046 mxCellPerPage = MX_CELL(pBt);
4047 apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) );
4048 if( apCell==0 ) return SQLITE_NOMEM;
4049 szCell = (int*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00004050 if( pPage->leaf ){
4051 /* The table is completely empty */
4052 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
4053 }else{
4054 /* The root page is empty but has one child. Transfer the
4055 ** information from that one child into the root page if it
4056 ** will fit. This reduces the depth of the tree by one.
4057 **
4058 ** If the root page is page 1, it has less space available than
4059 ** its child (due to the 100 byte header that occurs at the beginning
4060 ** of the database fle), so it might not be able to hold all of the
4061 ** information currently contained in the child. If this is the
4062 ** case, then do not do the transfer. Leave page 1 empty except
4063 ** for the right-pointer to the child page. The child page becomes
4064 ** the virtual root of the tree.
4065 */
4066 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
4067 assert( pgnoChild>0 );
4068 assert( pgnoChild<=sqlite3pager_pagecount(pPage->pBt->pPager) );
4069 rc = getPage(pPage->pBt, pgnoChild, &pChild);
drh2e38c322004-09-03 18:38:44 +00004070 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004071 if( pPage->pgno==1 ){
4072 rc = initPage(pChild, pPage);
drh2e38c322004-09-03 18:38:44 +00004073 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004074 assert( pChild->nOverflow==0 );
4075 if( pChild->nFree>=100 ){
4076 /* The child information will fit on the root page, so do the
4077 ** copy */
4078 int i;
4079 zeroPage(pPage, pChild->aData[0]);
4080 for(i=0; i<pChild->nCell; i++){
4081 apCell[i] = findCell(pChild,i);
4082 szCell[i] = cellSizePtr(pChild, apCell[i]);
4083 }
4084 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00004085 /* Copy the right-pointer of the child to the parent. */
4086 put4byte(&pPage->aData[pPage->hdrOffset+8],
4087 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh43605152004-05-29 21:46:49 +00004088 freePage(pChild);
4089 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
4090 }else{
4091 /* The child has more information that will fit on the root.
4092 ** The tree is already balanced. Do nothing. */
4093 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
4094 }
4095 }else{
4096 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
4097 pPage->isInit = 0;
4098 pPage->pParent = 0;
4099 rc = initPage(pPage, 0);
4100 assert( rc==SQLITE_OK );
4101 freePage(pChild);
4102 TRACE(("BALANCE: transfer child %d into root %d\n",
4103 pChild->pgno, pPage->pgno));
4104 }
danielk1977afcdd022004-10-31 16:25:42 +00004105 rc = reparentChildPages(pPage);
4106 if( rc!=SQLITE_OK ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00004107 releasePage(pChild);
4108 }
drh2e38c322004-09-03 18:38:44 +00004109end_shallow_balance:
4110 sqliteFree(apCell);
4111 return rc;
drh43605152004-05-29 21:46:49 +00004112}
4113
4114
4115/*
4116** The root page is overfull
4117**
4118** When this happens, Create a new child page and copy the
4119** contents of the root into the child. Then make the root
4120** page an empty page with rightChild pointing to the new
4121** child. Finally, call balance_internal() on the new child
4122** to cause it to split.
4123*/
4124static int balance_deeper(MemPage *pPage){
4125 int rc; /* Return value from subprocedures */
4126 MemPage *pChild; /* Pointer to a new child page */
4127 Pgno pgnoChild; /* Page number of the new child page */
4128 Btree *pBt; /* The BTree */
4129 int usableSize; /* Total usable size of a page */
4130 u8 *data; /* Content of the parent page */
4131 u8 *cdata; /* Content of the child page */
4132 int hdr; /* Offset to page header in parent */
4133 int brk; /* Offset to content of first cell in parent */
4134
4135 assert( pPage->pParent==0 );
4136 assert( pPage->nOverflow>0 );
4137 pBt = pPage->pBt;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004138 rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00004139 if( rc ) return rc;
4140 assert( sqlite3pager_iswriteable(pChild->aData) );
4141 usableSize = pBt->usableSize;
4142 data = pPage->aData;
4143 hdr = pPage->hdrOffset;
4144 brk = get2byte(&data[hdr+5]);
4145 cdata = pChild->aData;
4146 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
4147 memcpy(&cdata[brk], &data[brk], usableSize-brk);
danielk1977c7dc7532004-11-17 10:22:03 +00004148 assert( pChild->isInit==0 );
drh43605152004-05-29 21:46:49 +00004149 rc = initPage(pChild, pPage);
4150 if( rc ) return rc;
4151 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
4152 pChild->nOverflow = pPage->nOverflow;
4153 if( pChild->nOverflow ){
4154 pChild->nFree = 0;
4155 }
4156 assert( pChild->nCell==pPage->nCell );
4157 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
4158 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
4159 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
4160 rc = balance_nonroot(pChild);
4161 releasePage(pChild);
4162 return rc;
4163}
4164
4165/*
4166** Decide if the page pPage needs to be balanced. If balancing is
4167** required, call the appropriate balancing routine.
4168*/
4169static int balance(MemPage *pPage){
4170 int rc = SQLITE_OK;
4171 if( pPage->pParent==0 ){
4172 if( pPage->nOverflow>0 ){
4173 rc = balance_deeper(pPage);
4174 }
danielk1977687566d2004-11-02 12:56:41 +00004175 if( rc==SQLITE_OK && pPage->nCell==0 ){
drh43605152004-05-29 21:46:49 +00004176 rc = balance_shallower(pPage);
4177 }
4178 }else{
4179 if( pPage->nOverflow>0 || pPage->nFree>pPage->pBt->usableSize*2/3 ){
4180 rc = balance_nonroot(pPage);
4181 }
4182 }
4183 return rc;
4184}
4185
4186/*
drh8dcd7ca2004-08-08 19:43:29 +00004187** This routine checks all cursors that point to table pgnoRoot.
4188** If any of those cursors other than pExclude were opened with
drhf74b8d92002-09-01 23:20:45 +00004189** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
drh8dcd7ca2004-08-08 19:43:29 +00004190** cursors that point to pgnoRoot were opened with wrFlag==1
drhf74b8d92002-09-01 23:20:45 +00004191** then this routine returns SQLITE_OK.
danielk1977299b1872004-11-22 10:02:10 +00004192**
4193** In addition to checking for read-locks (where a read-lock
4194** means a cursor opened with wrFlag==0) this routine also moves
4195** all cursors other than pExclude so that they are pointing to the
4196** first Cell on root page. This is necessary because an insert
4197** or delete might change the number of cells on a page or delete
4198** a page entirely and we do not want to leave any cursors
4199** pointing to non-existant pages or cells.
drhf74b8d92002-09-01 23:20:45 +00004200*/
drh8dcd7ca2004-08-08 19:43:29 +00004201static int checkReadLocks(Btree *pBt, Pgno pgnoRoot, BtCursor *pExclude){
danielk1977299b1872004-11-22 10:02:10 +00004202 BtCursor *p;
4203 for(p=pBt->pCursor; p; p=p->pNext){
4204 if( p->pgnoRoot!=pgnoRoot || p==pExclude ) continue;
4205 if( p->wrFlag==0 ) return SQLITE_LOCKED;
4206 if( p->pPage->pgno!=p->pgnoRoot ){
4207 moveToRoot(p);
4208 }
4209 }
drhf74b8d92002-09-01 23:20:45 +00004210 return SQLITE_OK;
4211}
4212
4213/*
drh3b7511c2001-05-26 13:15:44 +00004214** Insert a new record into the BTree. The key is given by (pKey,nKey)
4215** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00004216** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00004217** is left pointing at a random location.
4218**
4219** For an INTKEY table, only the nKey value of the key is used. pKey is
4220** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00004221*/
drh3aac2dd2004-04-26 14:10:20 +00004222int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00004223 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00004224 const void *pKey, i64 nKey, /* The key of the new record */
drh5c4d9702001-08-20 00:33:58 +00004225 const void *pData, int nData /* The data of the new record */
drh3b7511c2001-05-26 13:15:44 +00004226){
drh3b7511c2001-05-26 13:15:44 +00004227 int rc;
4228 int loc;
drh14acc042001-06-10 19:56:58 +00004229 int szNew;
drh3b7511c2001-05-26 13:15:44 +00004230 MemPage *pPage;
4231 Btree *pBt = pCur->pBt;
drha34b6762004-05-07 13:30:42 +00004232 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00004233 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00004234
drhc39e0002004-05-07 23:50:57 +00004235 if( pCur->status ){
4236 return pCur->status; /* A rollback destroyed this cursor */
drhecdc7532001-09-23 02:35:53 +00004237 }
danielk1977ee5741e2004-05-31 10:01:34 +00004238 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004239 /* Must start a transaction before doing an insert */
4240 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004241 }
drhf74b8d92002-09-01 23:20:45 +00004242 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00004243 if( !pCur->wrFlag ){
4244 return SQLITE_PERM; /* Cursor not open for writing */
4245 }
drh8dcd7ca2004-08-08 19:43:29 +00004246 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00004247 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
4248 }
drh3aac2dd2004-04-26 14:10:20 +00004249 rc = sqlite3BtreeMoveto(pCur, pKey, nKey, &loc);
drh3b7511c2001-05-26 13:15:44 +00004250 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00004251 pPage = pCur->pPage;
drh4a1c3802004-05-12 15:15:47 +00004252 assert( pPage->intKey || nKey>=0 );
drh8b18dd42004-05-12 19:18:15 +00004253 assert( pPage->leaf || !pPage->leafData );
drh3a4c1412004-05-09 20:40:11 +00004254 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
4255 pCur->pgnoRoot, nKey, nData, pPage->pgno,
4256 loc==0 ? "overwrite" : "new entry"));
drh7aa128d2002-06-21 13:09:16 +00004257 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00004258 rc = sqlite3pager_write(pPage->aData);
drhbd03cae2001-06-02 02:40:57 +00004259 if( rc ) return rc;
drh2e38c322004-09-03 18:38:44 +00004260 newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
4261 if( newCell==0 ) return SQLITE_NOMEM;
drha34b6762004-05-07 13:30:42 +00004262 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, &szNew);
drh2e38c322004-09-03 18:38:44 +00004263 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00004264 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00004265 assert( szNew<=MX_CELL_SIZE(pBt) );
drhf328bc82004-05-10 23:29:49 +00004266 if( loc==0 && pCur->isValid ){
drha34b6762004-05-07 13:30:42 +00004267 int szOld;
4268 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004269 oldCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00004270 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004271 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00004272 }
drh43605152004-05-29 21:46:49 +00004273 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00004274 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00004275 if( rc ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00004276 dropCell(pPage, pCur->idx, szOld);
drh7c717f72001-06-24 20:39:41 +00004277 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00004278 assert( pPage->leaf );
drh14acc042001-06-10 19:56:58 +00004279 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00004280 pCur->info.nSize = 0;
drh14acc042001-06-10 19:56:58 +00004281 }else{
drh4b70f112004-05-02 21:12:19 +00004282 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00004283 }
danielk1977a3ad5e72005-01-07 08:56:44 +00004284 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
danielk1977e80463b2004-11-03 03:01:16 +00004285 if( rc!=SQLITE_OK ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00004286 rc = balance(pPage);
drh23e11ca2004-05-04 17:27:28 +00004287 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
drh3fc190c2001-09-14 03:24:23 +00004288 /* fflush(stdout); */
danielk1977299b1872004-11-22 10:02:10 +00004289 if( rc==SQLITE_OK ){
4290 moveToRoot(pCur);
4291 }
drh2e38c322004-09-03 18:38:44 +00004292end_insert:
4293 sqliteFree(newCell);
drh5e2f8b92001-05-28 00:41:15 +00004294 return rc;
4295}
4296
4297/*
drh4b70f112004-05-02 21:12:19 +00004298** Delete the entry that the cursor is pointing to. The cursor
4299** is left pointing at a random location.
drh3b7511c2001-05-26 13:15:44 +00004300*/
drh3aac2dd2004-04-26 14:10:20 +00004301int sqlite3BtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00004302 MemPage *pPage = pCur->pPage;
drh4b70f112004-05-02 21:12:19 +00004303 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00004304 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00004305 Pgno pgnoChild = 0;
drh0d316a42002-08-11 20:10:47 +00004306 Btree *pBt = pCur->pBt;
drh8b2f49b2001-06-08 00:21:52 +00004307
drh7aa128d2002-06-21 13:09:16 +00004308 assert( pPage->isInit );
drhc39e0002004-05-07 23:50:57 +00004309 if( pCur->status ){
4310 return pCur->status; /* A rollback destroyed this cursor */
drhecdc7532001-09-23 02:35:53 +00004311 }
danielk1977ee5741e2004-05-31 10:01:34 +00004312 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004313 /* Must start a transaction before doing a delete */
4314 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004315 }
drhf74b8d92002-09-01 23:20:45 +00004316 assert( !pBt->readOnly );
drhbd03cae2001-06-02 02:40:57 +00004317 if( pCur->idx >= pPage->nCell ){
4318 return SQLITE_ERROR; /* The cursor is not pointing to anything */
4319 }
drhecdc7532001-09-23 02:35:53 +00004320 if( !pCur->wrFlag ){
4321 return SQLITE_PERM; /* Did not open this cursor for writing */
4322 }
drh8dcd7ca2004-08-08 19:43:29 +00004323 if( checkReadLocks(pBt, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00004324 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
4325 }
drha34b6762004-05-07 13:30:42 +00004326 rc = sqlite3pager_write(pPage->aData);
drhbd03cae2001-06-02 02:40:57 +00004327 if( rc ) return rc;
danielk1977e6efa742004-11-10 11:55:10 +00004328
4329 /* Locate the cell within it's page and leave pCell pointing to the
4330 ** data. The clearCell() call frees any overflow pages associated with the
4331 ** cell. The cell itself is still intact.
4332 */
danielk1977299b1872004-11-22 10:02:10 +00004333 pCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00004334 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004335 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00004336 }
4337 clearCell(pPage, pCell);
danielk1977e6efa742004-11-10 11:55:10 +00004338
drh4b70f112004-05-02 21:12:19 +00004339 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00004340 /*
drh5e00f6c2001-09-13 13:46:56 +00004341 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00004342 ** do something we will leave a hole on an internal page.
4343 ** We have to fill the hole by moving in a cell from a leaf. The
4344 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00004345 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00004346 */
drh14acc042001-06-10 19:56:58 +00004347 BtCursor leafCur;
drh4b70f112004-05-02 21:12:19 +00004348 unsigned char *pNext;
drh14acc042001-06-10 19:56:58 +00004349 int szNext;
danielk1977299b1872004-11-22 10:02:10 +00004350 int notUsed;
drh2e38c322004-09-03 18:38:44 +00004351 unsigned char *tempCell;
drh8b18dd42004-05-12 19:18:15 +00004352 assert( !pPage->leafData );
drh14acc042001-06-10 19:56:58 +00004353 getTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00004354 rc = sqlite3BtreeNext(&leafCur, &notUsed);
drh14acc042001-06-10 19:56:58 +00004355 if( rc!=SQLITE_OK ){
drhee696e22004-08-30 16:52:17 +00004356 if( rc!=SQLITE_NOMEM ){
4357 rc = SQLITE_CORRUPT; /* bkpt-CORRUPT */
4358 }
danielk1977299b1872004-11-22 10:02:10 +00004359 return rc;
drh5e2f8b92001-05-28 00:41:15 +00004360 }
drha34b6762004-05-07 13:30:42 +00004361 rc = sqlite3pager_write(leafCur.pPage->aData);
danielk1977299b1872004-11-22 10:02:10 +00004362 if( rc ) return rc;
4363 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
4364 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
4365 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
drh43605152004-05-29 21:46:49 +00004366 pNext = findCell(leafCur.pPage, leafCur.idx);
4367 szNext = cellSizePtr(leafCur.pPage, pNext);
drh2e38c322004-09-03 18:38:44 +00004368 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
4369 tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) );
danielk1977299b1872004-11-22 10:02:10 +00004370 if( tempCell==0 ) return SQLITE_NOMEM;
danielk1977a3ad5e72005-01-07 08:56:44 +00004371 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
danielk1977299b1872004-11-22 10:02:10 +00004372 if( rc!=SQLITE_OK ) return rc;
4373 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
drh4b70f112004-05-02 21:12:19 +00004374 rc = balance(pPage);
drh2e38c322004-09-03 18:38:44 +00004375 sqliteFree(tempCell);
danielk1977299b1872004-11-22 10:02:10 +00004376 if( rc ) return rc;
drh4b70f112004-05-02 21:12:19 +00004377 dropCell(leafCur.pPage, leafCur.idx, szNext);
4378 rc = balance(leafCur.pPage);
drh8c42ca92001-06-22 19:15:00 +00004379 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00004380 }else{
danielk1977299b1872004-11-22 10:02:10 +00004381 TRACE(("DELETE: table=%d delete from leaf %d\n",
4382 pCur->pgnoRoot, pPage->pgno));
4383 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
drh4b70f112004-05-02 21:12:19 +00004384 rc = balance(pPage);
drh5e2f8b92001-05-28 00:41:15 +00004385 }
danielk1977299b1872004-11-22 10:02:10 +00004386 moveToRoot(pCur);
drh5e2f8b92001-05-28 00:41:15 +00004387 return rc;
drh3b7511c2001-05-26 13:15:44 +00004388}
drh8b2f49b2001-06-08 00:21:52 +00004389
4390/*
drhc6b52df2002-01-04 03:09:29 +00004391** Create a new BTree table. Write into *piTable the page
4392** number for the root page of the new table.
4393**
drhab01f612004-05-22 02:55:23 +00004394** The type of type is determined by the flags parameter. Only the
4395** following values of flags are currently in use. Other values for
4396** flags might not work:
4397**
4398** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
4399** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00004400*/
drh3aac2dd2004-04-26 14:10:20 +00004401int sqlite3BtreeCreateTable(Btree *pBt, int *piTable, int flags){
drh8b2f49b2001-06-08 00:21:52 +00004402 MemPage *pRoot;
4403 Pgno pgnoRoot;
4404 int rc;
danielk1977ee5741e2004-05-31 10:01:34 +00004405 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004406 /* Must start a transaction first */
4407 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004408 }
drh5df72a52002-06-06 23:16:05 +00004409 if( pBt->readOnly ){
4410 return SQLITE_READONLY;
4411 }
danielk1977e6efa742004-11-10 11:55:10 +00004412
4413 /* It is illegal to create a table if any cursors are open on the
4414 ** database. This is because in auto-vacuum mode the backend may
4415 ** need to move a database page to make room for the new root-page.
4416 ** If an open cursor was using the page a problem would occur.
4417 */
4418 if( pBt->pCursor ){
4419 return SQLITE_LOCKED;
4420 }
4421
danielk1977003ba062004-11-04 02:57:33 +00004422#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977cb1a7eb2004-11-05 12:27:02 +00004423 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drh8b2f49b2001-06-08 00:21:52 +00004424 if( rc ) return rc;
danielk1977003ba062004-11-04 02:57:33 +00004425#else
danielk1977687566d2004-11-02 12:56:41 +00004426 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00004427 Pgno pgnoMove; /* Move a page here to make room for the root-page */
4428 MemPage *pPageMove; /* The page to move to. */
4429
danielk1977003ba062004-11-04 02:57:33 +00004430 /* Read the value of meta[3] from the database to determine where the
4431 ** root page of the new table should go. meta[3] is the largest root-page
4432 ** created so far, so the new root-page is (meta[3]+1).
4433 */
4434 rc = sqlite3BtreeGetMeta(pBt, 4, &pgnoRoot);
4435 if( rc!=SQLITE_OK ) return rc;
4436 pgnoRoot++;
4437
danielk1977599fcba2004-11-08 07:13:13 +00004438 /* The new root-page may not be allocated on a pointer-map page, or the
4439 ** PENDING_BYTE page.
4440 */
drh42cac6d2004-11-20 20:31:11 +00004441 if( pgnoRoot==PTRMAP_PAGENO(pBt->usableSize, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00004442 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00004443 pgnoRoot++;
4444 }
4445 assert( pgnoRoot>=3 );
4446
4447 /* Allocate a page. The page that currently resides at pgnoRoot will
4448 ** be moved to the allocated page (unless the allocated page happens
4449 ** to reside at pgnoRoot).
4450 */
danielk1977cb1a7eb2004-11-05 12:27:02 +00004451 rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00004452 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00004453 return rc;
4454 }
danielk1977003ba062004-11-04 02:57:33 +00004455
4456 if( pgnoMove!=pgnoRoot ){
4457 u8 eType;
4458 Pgno iPtrPage;
4459
4460 releasePage(pPageMove);
4461 rc = getPage(pBt, pgnoRoot, &pRoot);
4462 if( rc!=SQLITE_OK ){
4463 return rc;
4464 }
4465 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
danielk1977a0bf2652004-11-04 14:30:04 +00004466 assert( eType!=PTRMAP_ROOTPAGE );
danielk1977a64a0352004-11-05 01:45:13 +00004467 assert( eType!=PTRMAP_FREEPAGE );
danielk1977003ba062004-11-04 02:57:33 +00004468 if( rc!=SQLITE_OK ){
4469 releasePage(pRoot);
4470 return rc;
4471 }
4472 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
4473 releasePage(pRoot);
4474 if( rc!=SQLITE_OK ){
4475 return rc;
4476 }
4477 rc = getPage(pBt, pgnoRoot, &pRoot);
4478 if( rc!=SQLITE_OK ){
4479 return rc;
4480 }
4481 rc = sqlite3pager_write(pRoot->aData);
4482 if( rc!=SQLITE_OK ){
4483 releasePage(pRoot);
4484 return rc;
4485 }
4486 }else{
4487 pRoot = pPageMove;
4488 }
4489
danielk197742741be2005-01-08 12:42:39 +00004490 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00004491 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
4492 if( rc ){
4493 releasePage(pRoot);
4494 return rc;
4495 }
4496 rc = sqlite3BtreeUpdateMeta(pBt, 4, pgnoRoot);
4497 if( rc ){
4498 releasePage(pRoot);
4499 return rc;
4500 }
danielk197742741be2005-01-08 12:42:39 +00004501
danielk1977003ba062004-11-04 02:57:33 +00004502 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004503 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00004504 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00004505 }
4506#endif
drha34b6762004-05-07 13:30:42 +00004507 assert( sqlite3pager_iswriteable(pRoot->aData) );
drhde647132004-05-07 17:57:49 +00004508 zeroPage(pRoot, flags | PTF_LEAF);
drha34b6762004-05-07 13:30:42 +00004509 sqlite3pager_unref(pRoot->aData);
drh8b2f49b2001-06-08 00:21:52 +00004510 *piTable = (int)pgnoRoot;
4511 return SQLITE_OK;
4512}
4513
4514/*
4515** Erase the given database page and all its children. Return
4516** the page to the freelist.
4517*/
drh4b70f112004-05-02 21:12:19 +00004518static int clearDatabasePage(
4519 Btree *pBt, /* The BTree that contains the table */
4520 Pgno pgno, /* Page number to clear */
4521 MemPage *pParent, /* Parent page. NULL for the root */
4522 int freePageFlag /* Deallocate page if true */
4523){
drh8b2f49b2001-06-08 00:21:52 +00004524 MemPage *pPage;
4525 int rc;
drh4b70f112004-05-02 21:12:19 +00004526 unsigned char *pCell;
4527 int i;
drh8b2f49b2001-06-08 00:21:52 +00004528
drhde647132004-05-07 17:57:49 +00004529 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
drh8b2f49b2001-06-08 00:21:52 +00004530 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00004531 rc = sqlite3pager_write(pPage->aData);
drh6019e162001-07-02 17:51:45 +00004532 if( rc ) return rc;
drh4b70f112004-05-02 21:12:19 +00004533 for(i=0; i<pPage->nCell; i++){
drh43605152004-05-29 21:46:49 +00004534 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00004535 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004536 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
drh8b2f49b2001-06-08 00:21:52 +00004537 if( rc ) return rc;
4538 }
drh4b70f112004-05-02 21:12:19 +00004539 rc = clearCell(pPage, pCell);
drh8b2f49b2001-06-08 00:21:52 +00004540 if( rc ) return rc;
4541 }
drha34b6762004-05-07 13:30:42 +00004542 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004543 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
drh2aa679f2001-06-25 02:11:07 +00004544 if( rc ) return rc;
4545 }
4546 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00004547 rc = freePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00004548 }else{
drh3a4c1412004-05-09 20:40:11 +00004549 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00004550 }
drh4b70f112004-05-02 21:12:19 +00004551 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00004552 return rc;
drh8b2f49b2001-06-08 00:21:52 +00004553}
4554
4555/*
drhab01f612004-05-22 02:55:23 +00004556** Delete all information from a single table in the database. iTable is
4557** the page number of the root of the table. After this routine returns,
4558** the root page is empty, but still exists.
4559**
4560** This routine will fail with SQLITE_LOCKED if there are any open
4561** read cursors on the table. Open write cursors are moved to the
4562** root of the table.
drh8b2f49b2001-06-08 00:21:52 +00004563*/
drh3aac2dd2004-04-26 14:10:20 +00004564int sqlite3BtreeClearTable(Btree *pBt, int iTable){
drh8b2f49b2001-06-08 00:21:52 +00004565 int rc;
drhf74b8d92002-09-01 23:20:45 +00004566 BtCursor *pCur;
danielk1977ee5741e2004-05-31 10:01:34 +00004567 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004568 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004569 }
drhf74b8d92002-09-01 23:20:45 +00004570 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
4571 if( pCur->pgnoRoot==(Pgno)iTable ){
4572 if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
4573 moveToRoot(pCur);
4574 }
drhecdc7532001-09-23 02:35:53 +00004575 }
drha34b6762004-05-07 13:30:42 +00004576 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
drh8b2f49b2001-06-08 00:21:52 +00004577 if( rc ){
drh3aac2dd2004-04-26 14:10:20 +00004578 sqlite3BtreeRollback(pBt);
drh8b2f49b2001-06-08 00:21:52 +00004579 }
drh8c42ca92001-06-22 19:15:00 +00004580 return rc;
drh8b2f49b2001-06-08 00:21:52 +00004581}
4582
4583/*
4584** Erase all information in a table and add the root of the table to
4585** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00004586** page 1) is never added to the freelist.
4587**
4588** This routine will fail with SQLITE_LOCKED if there are any open
4589** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00004590**
4591** If AUTOVACUUM is enabled and the page at iTable is not the last
4592** root page in the database file, then the last root page
4593** in the database file is moved into the slot formerly occupied by
4594** iTable and that last slot formerly occupied by the last root page
4595** is added to the freelist instead of iTable. In this say, all
4596** root pages are kept at the beginning of the database file, which
4597** is necessary for AUTOVACUUM to work right. *piMoved is set to the
4598** page number that used to be the last root page in the file before
4599** the move. If no page gets moved, *piMoved is set to 0.
4600** The last root page is recorded in meta[3] and the value of
4601** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00004602*/
danielk1977a0bf2652004-11-04 14:30:04 +00004603int sqlite3BtreeDropTable(Btree *pBt, int iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00004604 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00004605 MemPage *pPage = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00004606
danielk1977ee5741e2004-05-31 10:01:34 +00004607 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004608 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00004609 }
danielk1977a0bf2652004-11-04 14:30:04 +00004610
danielk1977e6efa742004-11-10 11:55:10 +00004611 /* It is illegal to drop a table if any cursors are open on the
4612 ** database. This is because in auto-vacuum mode the backend may
4613 ** need to move another root-page to fill a gap left by the deleted
4614 ** root page. If an open cursor was using this page a problem would
4615 ** occur.
4616 */
4617 if( pBt->pCursor ){
4618 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00004619 }
danielk1977a0bf2652004-11-04 14:30:04 +00004620
drha34b6762004-05-07 13:30:42 +00004621 rc = getPage(pBt, (Pgno)iTable, &pPage);
drh2aa679f2001-06-25 02:11:07 +00004622 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004623 rc = sqlite3BtreeClearTable(pBt, iTable);
drh2aa679f2001-06-25 02:11:07 +00004624 if( rc ) return rc;
danielk1977a0bf2652004-11-04 14:30:04 +00004625
drh205f48e2004-11-05 00:43:11 +00004626 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00004627
drh4b70f112004-05-02 21:12:19 +00004628 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004629#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00004630 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00004631 releasePage(pPage);
4632#else
4633 if( pBt->autoVacuum ){
4634 Pgno maxRootPgno;
4635 rc = sqlite3BtreeGetMeta(pBt, 4, &maxRootPgno);
4636 if( rc!=SQLITE_OK ){
4637 releasePage(pPage);
4638 return rc;
4639 }
4640
4641 if( iTable==maxRootPgno ){
4642 /* If the table being dropped is the table with the largest root-page
4643 ** number in the database, put the root page on the free list.
4644 */
4645 rc = freePage(pPage);
4646 releasePage(pPage);
4647 if( rc!=SQLITE_OK ){
4648 return rc;
4649 }
4650 }else{
4651 /* The table being dropped does not have the largest root-page
4652 ** number in the database. So move the page that does into the
4653 ** gap left by the deleted root-page.
4654 */
4655 MemPage *pMove;
4656 releasePage(pPage);
4657 rc = getPage(pBt, maxRootPgno, &pMove);
4658 if( rc!=SQLITE_OK ){
4659 return rc;
4660 }
4661 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
4662 releasePage(pMove);
4663 if( rc!=SQLITE_OK ){
4664 return rc;
4665 }
4666 rc = getPage(pBt, maxRootPgno, &pMove);
4667 if( rc!=SQLITE_OK ){
4668 return rc;
4669 }
4670 rc = freePage(pMove);
4671 releasePage(pMove);
4672 if( rc!=SQLITE_OK ){
4673 return rc;
4674 }
4675 *piMoved = maxRootPgno;
4676 }
4677
danielk1977599fcba2004-11-08 07:13:13 +00004678 /* Set the new 'max-root-page' value in the database header. This
4679 ** is the old value less one, less one more if that happens to
4680 ** be a root-page number, less one again if that is the
4681 ** PENDING_BYTE_PAGE.
4682 */
danielk197787a6e732004-11-05 12:58:25 +00004683 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00004684 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
4685 maxRootPgno--;
4686 }
drh42cac6d2004-11-20 20:31:11 +00004687 if( maxRootPgno==PTRMAP_PAGENO(pBt->usableSize, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00004688 maxRootPgno--;
4689 }
danielk1977599fcba2004-11-08 07:13:13 +00004690 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
4691
danielk197787a6e732004-11-05 12:58:25 +00004692 rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004693 }else{
4694 rc = freePage(pPage);
4695 releasePage(pPage);
4696 }
4697#endif
drh2aa679f2001-06-25 02:11:07 +00004698 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004699 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00004700 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00004701 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00004702 }
drh8b2f49b2001-06-08 00:21:52 +00004703 return rc;
4704}
4705
drh001bbcb2003-03-19 03:14:00 +00004706
drh8b2f49b2001-06-08 00:21:52 +00004707/*
drh23e11ca2004-05-04 17:27:28 +00004708** Read the meta-information out of a database file. Meta[0]
4709** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00004710** through meta[15] are available for use by higher layers. Meta[0]
4711** is read-only, the others are read/write.
4712**
4713** The schema layer numbers meta values differently. At the schema
4714** layer (and the SetCookie and ReadCookie opcodes) the number of
4715** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00004716*/
drh3aac2dd2004-04-26 14:10:20 +00004717int sqlite3BtreeGetMeta(Btree *pBt, int idx, u32 *pMeta){
drh8b2f49b2001-06-08 00:21:52 +00004718 int rc;
drh4b70f112004-05-02 21:12:19 +00004719 unsigned char *pP1;
drh8b2f49b2001-06-08 00:21:52 +00004720
drh23e11ca2004-05-04 17:27:28 +00004721 assert( idx>=0 && idx<=15 );
drha34b6762004-05-07 13:30:42 +00004722 rc = sqlite3pager_get(pBt->pPager, 1, (void**)&pP1);
drh8b2f49b2001-06-08 00:21:52 +00004723 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00004724 *pMeta = get4byte(&pP1[36 + idx*4]);
drha34b6762004-05-07 13:30:42 +00004725 sqlite3pager_unref(pP1);
drhae157872004-08-14 19:20:09 +00004726
danielk1977599fcba2004-11-08 07:13:13 +00004727 /* If autovacuumed is disabled in this build but we are trying to
4728 ** access an autovacuumed database, then make the database readonly.
4729 */
danielk1977003ba062004-11-04 02:57:33 +00004730#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00004731 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00004732#endif
drhae157872004-08-14 19:20:09 +00004733
drh8b2f49b2001-06-08 00:21:52 +00004734 return SQLITE_OK;
4735}
4736
4737/*
drh23e11ca2004-05-04 17:27:28 +00004738** Write meta-information back into the database. Meta[0] is
4739** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00004740*/
drh3aac2dd2004-04-26 14:10:20 +00004741int sqlite3BtreeUpdateMeta(Btree *pBt, int idx, u32 iMeta){
drh4b70f112004-05-02 21:12:19 +00004742 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00004743 int rc;
drh23e11ca2004-05-04 17:27:28 +00004744 assert( idx>=1 && idx<=15 );
danielk1977ee5741e2004-05-31 10:01:34 +00004745 if( pBt->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00004746 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh5df72a52002-06-06 23:16:05 +00004747 }
drhde647132004-05-07 17:57:49 +00004748 assert( pBt->pPage1!=0 );
4749 pP1 = pBt->pPage1->aData;
drha34b6762004-05-07 13:30:42 +00004750 rc = sqlite3pager_write(pP1);
drh4b70f112004-05-02 21:12:19 +00004751 if( rc ) return rc;
drh23e11ca2004-05-04 17:27:28 +00004752 put4byte(&pP1[36 + idx*4], iMeta);
drh8b2f49b2001-06-08 00:21:52 +00004753 return SQLITE_OK;
4754}
drh8c42ca92001-06-22 19:15:00 +00004755
drhf328bc82004-05-10 23:29:49 +00004756/*
4757** Return the flag byte at the beginning of the page that the cursor
4758** is currently pointing to.
4759*/
4760int sqlite3BtreeFlags(BtCursor *pCur){
4761 MemPage *pPage = pCur->pPage;
4762 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
4763}
4764
drh8c42ca92001-06-22 19:15:00 +00004765/*
4766** Print a disassembly of the given page on standard output. This routine
4767** is used for debugging and testing only.
4768*/
drhaaab5722002-02-19 13:39:21 +00004769#ifdef SQLITE_TEST
danielk1977c7dc7532004-11-17 10:22:03 +00004770static int btreePageDump(Btree *pBt, int pgno, int recursive, MemPage *pParent){
drh8c42ca92001-06-22 19:15:00 +00004771 int rc;
4772 MemPage *pPage;
drhc8629a12004-05-08 20:07:40 +00004773 int i, j, c;
drh8c42ca92001-06-22 19:15:00 +00004774 int nFree;
4775 u16 idx;
drhab9f7f12004-05-08 10:56:11 +00004776 int hdr;
drh43605152004-05-29 21:46:49 +00004777 int nCell;
drha2fce642004-06-05 00:01:44 +00004778 int isInit;
drhab9f7f12004-05-08 10:56:11 +00004779 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00004780 char range[20];
4781 unsigned char payload[20];
drhab9f7f12004-05-08 10:56:11 +00004782
drh4b70f112004-05-02 21:12:19 +00004783 rc = getPage(pBt, (Pgno)pgno, &pPage);
drha2fce642004-06-05 00:01:44 +00004784 isInit = pPage->isInit;
4785 if( pPage->isInit==0 ){
danielk1977c7dc7532004-11-17 10:22:03 +00004786 initPage(pPage, pParent);
drha2fce642004-06-05 00:01:44 +00004787 }
drh8c42ca92001-06-22 19:15:00 +00004788 if( rc ){
4789 return rc;
4790 }
drhab9f7f12004-05-08 10:56:11 +00004791 hdr = pPage->hdrOffset;
4792 data = pPage->aData;
drhc8629a12004-05-08 20:07:40 +00004793 c = data[hdr];
drh8b18dd42004-05-12 19:18:15 +00004794 pPage->intKey = (c & (PTF_INTKEY|PTF_LEAFDATA))!=0;
drhc8629a12004-05-08 20:07:40 +00004795 pPage->zeroData = (c & PTF_ZERODATA)!=0;
drh8b18dd42004-05-12 19:18:15 +00004796 pPage->leafData = (c & PTF_LEAFDATA)!=0;
drhc8629a12004-05-08 20:07:40 +00004797 pPage->leaf = (c & PTF_LEAF)!=0;
drh8b18dd42004-05-12 19:18:15 +00004798 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
drh43605152004-05-29 21:46:49 +00004799 nCell = get2byte(&data[hdr+3]);
drhfe63d1c2004-09-08 20:13:04 +00004800 sqlite3DebugPrintf("PAGE %d: flags=0x%02x frag=%d parent=%d\n", pgno,
drh43605152004-05-29 21:46:49 +00004801 data[hdr], data[hdr+7],
drhda200cc2004-05-09 11:51:38 +00004802 (pPage->isInit && pPage->pParent) ? pPage->pParent->pgno : 0);
drhab9f7f12004-05-08 10:56:11 +00004803 assert( hdr == (pgno==1 ? 100 : 0) );
drh43605152004-05-29 21:46:49 +00004804 idx = hdr + 12 - pPage->leaf*4;
4805 for(i=0; i<nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00004806 CellInfo info;
drh4b70f112004-05-02 21:12:19 +00004807 Pgno child;
drh43605152004-05-29 21:46:49 +00004808 unsigned char *pCell;
drh6f11bef2004-05-13 01:12:56 +00004809 int sz;
drh43605152004-05-29 21:46:49 +00004810 int addr;
drh6f11bef2004-05-13 01:12:56 +00004811
drh43605152004-05-29 21:46:49 +00004812 addr = get2byte(&data[idx + 2*i]);
4813 pCell = &data[addr];
4814 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004815 sz = info.nSize;
drh43605152004-05-29 21:46:49 +00004816 sprintf(range,"%d..%d", addr, addr+sz-1);
drh4b70f112004-05-02 21:12:19 +00004817 if( pPage->leaf ){
4818 child = 0;
4819 }else{
drh43605152004-05-29 21:46:49 +00004820 child = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00004821 }
drh6f11bef2004-05-13 01:12:56 +00004822 sz = info.nData;
4823 if( !pPage->intKey ) sz += info.nKey;
drh8c42ca92001-06-22 19:15:00 +00004824 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
drh6f11bef2004-05-13 01:12:56 +00004825 memcpy(payload, &pCell[info.nHeader], sz);
drh8c42ca92001-06-22 19:15:00 +00004826 for(j=0; j<sz; j++){
4827 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
4828 }
4829 payload[sz] = 0;
drhfe63d1c2004-09-08 20:13:04 +00004830 sqlite3DebugPrintf(
drh6f11bef2004-05-13 01:12:56 +00004831 "cell %2d: i=%-10s chld=%-4d nk=%-4lld nd=%-4d payload=%s\n",
4832 i, range, child, info.nKey, info.nData, payload
drh8c42ca92001-06-22 19:15:00 +00004833 );
drh8c42ca92001-06-22 19:15:00 +00004834 }
drh4b70f112004-05-02 21:12:19 +00004835 if( !pPage->leaf ){
drhfe63d1c2004-09-08 20:13:04 +00004836 sqlite3DebugPrintf("right_child: %d\n", get4byte(&data[hdr+8]));
drh4b70f112004-05-02 21:12:19 +00004837 }
drh8c42ca92001-06-22 19:15:00 +00004838 nFree = 0;
4839 i = 0;
drhab9f7f12004-05-08 10:56:11 +00004840 idx = get2byte(&data[hdr+1]);
drhb6f41482004-05-14 01:58:11 +00004841 while( idx>0 && idx<pPage->pBt->usableSize ){
drhab9f7f12004-05-08 10:56:11 +00004842 int sz = get2byte(&data[idx+2]);
drh4b70f112004-05-02 21:12:19 +00004843 sprintf(range,"%d..%d", idx, idx+sz-1);
4844 nFree += sz;
drhfe63d1c2004-09-08 20:13:04 +00004845 sqlite3DebugPrintf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
drh4b70f112004-05-02 21:12:19 +00004846 i, range, sz, nFree);
drhab9f7f12004-05-08 10:56:11 +00004847 idx = get2byte(&data[idx]);
drh2aa679f2001-06-25 02:11:07 +00004848 i++;
drh8c42ca92001-06-22 19:15:00 +00004849 }
4850 if( idx!=0 ){
drhfe63d1c2004-09-08 20:13:04 +00004851 sqlite3DebugPrintf("ERROR: next freeblock index out of range: %d\n", idx);
drh8c42ca92001-06-22 19:15:00 +00004852 }
drha34b6762004-05-07 13:30:42 +00004853 if( recursive && !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00004854 for(i=0; i<nCell; i++){
4855 unsigned char *pCell = findCell(pPage, i);
danielk1977c7dc7532004-11-17 10:22:03 +00004856 btreePageDump(pBt, get4byte(pCell), 1, pPage);
drha34b6762004-05-07 13:30:42 +00004857 idx = get2byte(pCell);
drh6019e162001-07-02 17:51:45 +00004858 }
danielk1977c7dc7532004-11-17 10:22:03 +00004859 btreePageDump(pBt, get4byte(&data[hdr+8]), 1, pPage);
drh6019e162001-07-02 17:51:45 +00004860 }
drha2fce642004-06-05 00:01:44 +00004861 pPage->isInit = isInit;
drhab9f7f12004-05-08 10:56:11 +00004862 sqlite3pager_unref(data);
drh3644f082004-05-10 18:45:09 +00004863 fflush(stdout);
drh8c42ca92001-06-22 19:15:00 +00004864 return SQLITE_OK;
4865}
danielk1977c7dc7532004-11-17 10:22:03 +00004866int sqlite3BtreePageDump(Btree *pBt, int pgno, int recursive){
4867 return btreePageDump(pBt, pgno, recursive, 0);
4868}
drhaaab5722002-02-19 13:39:21 +00004869#endif
drh8c42ca92001-06-22 19:15:00 +00004870
drhaaab5722002-02-19 13:39:21 +00004871#ifdef SQLITE_TEST
drh8c42ca92001-06-22 19:15:00 +00004872/*
drh2aa679f2001-06-25 02:11:07 +00004873** Fill aResult[] with information about the entry and page that the
4874** cursor is pointing to.
4875**
4876** aResult[0] = The page number
4877** aResult[1] = The entry number
4878** aResult[2] = Total number of entries on this page
drh3e27c022004-07-23 00:01:38 +00004879** aResult[3] = Cell size (local payload + header)
drh2aa679f2001-06-25 02:11:07 +00004880** aResult[4] = Number of free bytes on this page
4881** aResult[5] = Number of free blocks on the page
drh3e27c022004-07-23 00:01:38 +00004882** aResult[6] = Total payload size (local + overflow)
4883** aResult[7] = Header size in bytes
4884** aResult[8] = Local payload size
4885** aResult[9] = Parent page number
drh5eddca62001-06-30 21:53:53 +00004886**
4887** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00004888*/
drh3e27c022004-07-23 00:01:38 +00004889int sqlite3BtreeCursorInfo(BtCursor *pCur, int *aResult, int upCnt){
drh2aa679f2001-06-25 02:11:07 +00004890 int cnt, idx;
4891 MemPage *pPage = pCur->pPage;
drh3e27c022004-07-23 00:01:38 +00004892 BtCursor tmpCur;
drhda200cc2004-05-09 11:51:38 +00004893
4894 pageIntegrity(pPage);
drh4b70f112004-05-02 21:12:19 +00004895 assert( pPage->isInit );
drh3e27c022004-07-23 00:01:38 +00004896 getTempCursor(pCur, &tmpCur);
4897 while( upCnt-- ){
4898 moveToParent(&tmpCur);
4899 }
4900 pPage = tmpCur.pPage;
4901 pageIntegrity(pPage);
drha34b6762004-05-07 13:30:42 +00004902 aResult[0] = sqlite3pager_pagenumber(pPage->aData);
drh91025292004-05-03 19:49:32 +00004903 assert( aResult[0]==pPage->pgno );
drh3e27c022004-07-23 00:01:38 +00004904 aResult[1] = tmpCur.idx;
drh2aa679f2001-06-25 02:11:07 +00004905 aResult[2] = pPage->nCell;
drh3e27c022004-07-23 00:01:38 +00004906 if( tmpCur.idx>=0 && tmpCur.idx<pPage->nCell ){
4907 getCellInfo(&tmpCur);
4908 aResult[3] = tmpCur.info.nSize;
4909 aResult[6] = tmpCur.info.nData;
4910 aResult[7] = tmpCur.info.nHeader;
4911 aResult[8] = tmpCur.info.nLocal;
drh2aa679f2001-06-25 02:11:07 +00004912 }else{
4913 aResult[3] = 0;
4914 aResult[6] = 0;
drh3e27c022004-07-23 00:01:38 +00004915 aResult[7] = 0;
4916 aResult[8] = 0;
drh2aa679f2001-06-25 02:11:07 +00004917 }
4918 aResult[4] = pPage->nFree;
4919 cnt = 0;
drh4b70f112004-05-02 21:12:19 +00004920 idx = get2byte(&pPage->aData[pPage->hdrOffset+1]);
drhb6f41482004-05-14 01:58:11 +00004921 while( idx>0 && idx<pPage->pBt->usableSize ){
drh2aa679f2001-06-25 02:11:07 +00004922 cnt++;
drh4b70f112004-05-02 21:12:19 +00004923 idx = get2byte(&pPage->aData[idx]);
drh2aa679f2001-06-25 02:11:07 +00004924 }
4925 aResult[5] = cnt;
drh3e27c022004-07-23 00:01:38 +00004926 if( pPage->pParent==0 || isRootPage(pPage) ){
4927 aResult[9] = 0;
4928 }else{
4929 aResult[9] = pPage->pParent->pgno;
4930 }
4931 releaseTempCursor(&tmpCur);
drh8c42ca92001-06-22 19:15:00 +00004932 return SQLITE_OK;
4933}
drhaaab5722002-02-19 13:39:21 +00004934#endif
drhdd793422001-06-28 01:54:48 +00004935
drhdd793422001-06-28 01:54:48 +00004936/*
drh5eddca62001-06-30 21:53:53 +00004937** Return the pager associated with a BTree. This routine is used for
4938** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00004939*/
drh3aac2dd2004-04-26 14:10:20 +00004940Pager *sqlite3BtreePager(Btree *pBt){
drhdd793422001-06-28 01:54:48 +00004941 return pBt->pPager;
4942}
drh5eddca62001-06-30 21:53:53 +00004943
4944/*
4945** This structure is passed around through all the sanity checking routines
4946** in order to keep track of some global state information.
4947*/
drhaaab5722002-02-19 13:39:21 +00004948typedef struct IntegrityCk IntegrityCk;
4949struct IntegrityCk {
drh100569d2001-10-02 13:01:48 +00004950 Btree *pBt; /* The tree being checked out */
4951 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
4952 int nPage; /* Number of pages in the database */
4953 int *anRef; /* Number of times each page is referenced */
drh100569d2001-10-02 13:01:48 +00004954 char *zErrMsg; /* An error message. NULL of no errors seen. */
drh5eddca62001-06-30 21:53:53 +00004955};
4956
drhb7f91642004-10-31 02:22:47 +00004957#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00004958/*
4959** Append a message to the error message string.
4960*/
drh2e38c322004-09-03 18:38:44 +00004961static void checkAppendMsg(
4962 IntegrityCk *pCheck,
4963 char *zMsg1,
4964 const char *zFormat,
4965 ...
4966){
4967 va_list ap;
4968 char *zMsg2;
4969 va_start(ap, zFormat);
4970 zMsg2 = sqlite3VMPrintf(zFormat, ap);
4971 va_end(ap);
4972 if( zMsg1==0 ) zMsg1 = "";
drh5eddca62001-06-30 21:53:53 +00004973 if( pCheck->zErrMsg ){
4974 char *zOld = pCheck->zErrMsg;
4975 pCheck->zErrMsg = 0;
danielk19774adee202004-05-08 08:23:19 +00004976 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00004977 sqliteFree(zOld);
4978 }else{
danielk19774adee202004-05-08 08:23:19 +00004979 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00004980 }
drh2e38c322004-09-03 18:38:44 +00004981 sqliteFree(zMsg2);
drh5eddca62001-06-30 21:53:53 +00004982}
drhb7f91642004-10-31 02:22:47 +00004983#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00004984
drhb7f91642004-10-31 02:22:47 +00004985#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00004986/*
4987** Add 1 to the reference count for page iPage. If this is the second
4988** reference to the page, add an error message to pCheck->zErrMsg.
4989** Return 1 if there are 2 ore more references to the page and 0 if
4990** if this is the first reference to the page.
4991**
4992** Also check that the page number is in bounds.
4993*/
drhaaab5722002-02-19 13:39:21 +00004994static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00004995 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00004996 if( iPage>pCheck->nPage || iPage<0 ){
drh2e38c322004-09-03 18:38:44 +00004997 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00004998 return 1;
4999 }
5000 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00005001 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00005002 return 1;
5003 }
5004 return (pCheck->anRef[iPage]++)>1;
5005}
5006
danielk1977afcdd022004-10-31 16:25:42 +00005007#ifndef SQLITE_OMIT_AUTOVACUUM
5008/*
5009** Check that the entry in the pointer-map for page iChild maps to
5010** page iParent, pointer type ptrType. If not, append an error message
5011** to pCheck.
5012*/
5013static void checkPtrmap(
5014 IntegrityCk *pCheck, /* Integrity check context */
5015 Pgno iChild, /* Child page number */
5016 u8 eType, /* Expected pointer map type */
5017 Pgno iParent, /* Expected pointer map parent page number */
5018 char *zContext /* Context description (used for error msg) */
5019){
5020 int rc;
5021 u8 ePtrmapType;
5022 Pgno iPtrmapParent;
5023
5024 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
5025 if( rc!=SQLITE_OK ){
5026 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
5027 return;
5028 }
5029
5030 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
5031 checkAppendMsg(pCheck, zContext,
5032 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
5033 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
5034 }
5035}
5036#endif
5037
drh5eddca62001-06-30 21:53:53 +00005038/*
5039** Check the integrity of the freelist or of an overflow page list.
5040** Verify that the number of pages on the list is N.
5041*/
drh30e58752002-03-02 20:41:57 +00005042static void checkList(
5043 IntegrityCk *pCheck, /* Integrity checking context */
5044 int isFreeList, /* True for a freelist. False for overflow page list */
5045 int iPage, /* Page number for first page in the list */
5046 int N, /* Expected number of pages in the list */
5047 char *zContext /* Context for error messages */
5048){
5049 int i;
drh3a4c1412004-05-09 20:40:11 +00005050 int expected = N;
5051 int iFirst = iPage;
drh30e58752002-03-02 20:41:57 +00005052 while( N-- > 0 ){
drh4b70f112004-05-02 21:12:19 +00005053 unsigned char *pOvfl;
drh5eddca62001-06-30 21:53:53 +00005054 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00005055 checkAppendMsg(pCheck, zContext,
5056 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00005057 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00005058 break;
5059 }
5060 if( checkRef(pCheck, iPage, zContext) ) break;
drha34b6762004-05-07 13:30:42 +00005061 if( sqlite3pager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
drh2e38c322004-09-03 18:38:44 +00005062 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00005063 break;
5064 }
drh30e58752002-03-02 20:41:57 +00005065 if( isFreeList ){
drh4b70f112004-05-02 21:12:19 +00005066 int n = get4byte(&pOvfl[4]);
danielk1977687566d2004-11-02 12:56:41 +00005067#ifndef SQLITE_OMIT_AUTOVACUUM
5068 if( pCheck->pBt->autoVacuum ){
5069 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
5070 }
5071#endif
drh855eb1c2004-08-31 13:45:11 +00005072 if( n>pCheck->pBt->usableSize/4-8 ){
drh2e38c322004-09-03 18:38:44 +00005073 checkAppendMsg(pCheck, zContext,
5074 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00005075 N--;
5076 }else{
5077 for(i=0; i<n; i++){
danielk1977687566d2004-11-02 12:56:41 +00005078 Pgno iFreePage = get4byte(&pOvfl[8+i*4]);
5079#ifndef SQLITE_OMIT_AUTOVACUUM
5080 if( pCheck->pBt->autoVacuum ){
5081 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
5082 }
5083#endif
5084 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00005085 }
5086 N -= n;
drh30e58752002-03-02 20:41:57 +00005087 }
drh30e58752002-03-02 20:41:57 +00005088 }
danielk1977afcdd022004-10-31 16:25:42 +00005089#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00005090 else{
5091 /* If this database supports auto-vacuum and iPage is not the last
5092 ** page in this overflow list, check that the pointer-map entry for
5093 ** the following page matches iPage.
5094 */
5095 if( pCheck->pBt->autoVacuum && N>0 ){
5096 i = get4byte(pOvfl);
5097 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
5098 }
danielk1977afcdd022004-10-31 16:25:42 +00005099 }
5100#endif
drh4b70f112004-05-02 21:12:19 +00005101 iPage = get4byte(pOvfl);
drha34b6762004-05-07 13:30:42 +00005102 sqlite3pager_unref(pOvfl);
drh5eddca62001-06-30 21:53:53 +00005103 }
5104}
drhb7f91642004-10-31 02:22:47 +00005105#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00005106
drhb7f91642004-10-31 02:22:47 +00005107#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005108/*
5109** Do various sanity checks on a single page of a tree. Return
5110** the tree depth. Root pages return 0. Parents of root pages
5111** return 1, and so forth.
5112**
5113** These checks are done:
5114**
5115** 1. Make sure that cells and freeblocks do not overlap
5116** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00005117** NO 2. Make sure cell keys are in order.
5118** NO 3. Make sure no key is less than or equal to zLowerBound.
5119** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00005120** 5. Check the integrity of overflow pages.
5121** 6. Recursively call checkTreePage on all children.
5122** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00005123** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00005124** the root of the tree.
5125*/
5126static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00005127 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00005128 int iPage, /* Page number of the page to check */
5129 MemPage *pParent, /* Parent page */
5130 char *zParentContext, /* Parent context */
5131 char *zLowerBound, /* All keys should be greater than this, if not NULL */
drh1bffb9c2002-02-03 17:37:36 +00005132 int nLower, /* Number of characters in zLowerBound */
5133 char *zUpperBound, /* All keys should be less than this, if not NULL */
5134 int nUpper /* Number of characters in zUpperBound */
drh5eddca62001-06-30 21:53:53 +00005135){
5136 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00005137 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00005138 int hdr, cellStart;
5139 int nCell;
drhda200cc2004-05-09 11:51:38 +00005140 u8 *data;
drh5eddca62001-06-30 21:53:53 +00005141 BtCursor cur;
drh0d316a42002-08-11 20:10:47 +00005142 Btree *pBt;
drhb6f41482004-05-14 01:58:11 +00005143 int maxLocal, usableSize;
drh5eddca62001-06-30 21:53:53 +00005144 char zContext[100];
drh2e38c322004-09-03 18:38:44 +00005145 char *hit;
drh5eddca62001-06-30 21:53:53 +00005146
danielk1977ef73ee92004-11-06 12:26:07 +00005147 sprintf(zContext, "Page %d: ", iPage);
5148
drh5eddca62001-06-30 21:53:53 +00005149 /* Check that the page exists
5150 */
drh0d316a42002-08-11 20:10:47 +00005151 cur.pBt = pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00005152 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00005153 if( iPage==0 ) return 0;
5154 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh4b70f112004-05-02 21:12:19 +00005155 if( (rc = getPage(pBt, (Pgno)iPage, &pPage))!=0 ){
drh2e38c322004-09-03 18:38:44 +00005156 checkAppendMsg(pCheck, zContext,
5157 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00005158 return 0;
5159 }
drh6f11bef2004-05-13 01:12:56 +00005160 maxLocal = pPage->leafData ? pBt->maxLeaf : pBt->maxLocal;
drh4b70f112004-05-02 21:12:19 +00005161 if( (rc = initPage(pPage, pParent))!=0 ){
drh2e38c322004-09-03 18:38:44 +00005162 checkAppendMsg(pCheck, zContext, "initPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00005163 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00005164 return 0;
5165 }
5166
5167 /* Check out all the cells.
5168 */
5169 depth = 0;
drh5eddca62001-06-30 21:53:53 +00005170 cur.pPage = pPage;
drh5eddca62001-06-30 21:53:53 +00005171 for(i=0; i<pPage->nCell; i++){
drh6f11bef2004-05-13 01:12:56 +00005172 u8 *pCell;
5173 int sz;
5174 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00005175
5176 /* Check payload overflow pages
5177 */
drh3a4c1412004-05-09 20:40:11 +00005178 sprintf(zContext, "On tree page %d cell %d: ", iPage, i);
drh43605152004-05-29 21:46:49 +00005179 pCell = findCell(pPage,i);
5180 parseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00005181 sz = info.nData;
5182 if( !pPage->intKey ) sz += info.nKey;
5183 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00005184 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00005185 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
5186#ifndef SQLITE_OMIT_AUTOVACUUM
5187 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00005188 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00005189 }
5190#endif
5191 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00005192 }
5193
5194 /* Check sanity of left child page.
5195 */
drhda200cc2004-05-09 11:51:38 +00005196 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005197 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00005198#ifndef SQLITE_OMIT_AUTOVACUUM
5199 if( pBt->autoVacuum ){
5200 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
5201 }
5202#endif
drhda200cc2004-05-09 11:51:38 +00005203 d2 = checkTreePage(pCheck,pgno,pPage,zContext,0,0,0,0);
5204 if( i>0 && d2!=depth ){
5205 checkAppendMsg(pCheck, zContext, "Child page depth differs");
5206 }
5207 depth = d2;
drh5eddca62001-06-30 21:53:53 +00005208 }
drh5eddca62001-06-30 21:53:53 +00005209 }
drhda200cc2004-05-09 11:51:38 +00005210 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005211 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drhda200cc2004-05-09 11:51:38 +00005212 sprintf(zContext, "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00005213#ifndef SQLITE_OMIT_AUTOVACUUM
5214 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00005215 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00005216 }
5217#endif
drhda200cc2004-05-09 11:51:38 +00005218 checkTreePage(pCheck, pgno, pPage, zContext,0,0,0,0);
5219 }
drh5eddca62001-06-30 21:53:53 +00005220
5221 /* Check for complete coverage of the page
5222 */
drhda200cc2004-05-09 11:51:38 +00005223 data = pPage->aData;
5224 hdr = pPage->hdrOffset;
drh2e38c322004-09-03 18:38:44 +00005225 hit = sqliteMalloc( usableSize );
5226 if( hit ){
5227 memset(hit, 1, get2byte(&data[hdr+5]));
5228 nCell = get2byte(&data[hdr+3]);
5229 cellStart = hdr + 12 - 4*pPage->leaf;
5230 for(i=0; i<nCell; i++){
5231 int pc = get2byte(&data[cellStart+i*2]);
5232 int size = cellSizePtr(pPage, &data[pc]);
5233 int j;
5234 for(j=pc+size-1; j>=pc; j--) hit[j]++;
5235 }
5236 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
5237 cnt++){
5238 int size = get2byte(&data[i+2]);
5239 int j;
5240 for(j=i+size-1; j>=i; j--) hit[j]++;
5241 i = get2byte(&data[i]);
5242 }
5243 for(i=cnt=0; i<usableSize; i++){
5244 if( hit[i]==0 ){
5245 cnt++;
5246 }else if( hit[i]>1 ){
5247 checkAppendMsg(pCheck, 0,
5248 "Multiple uses for byte %d of page %d", i, iPage);
5249 break;
5250 }
5251 }
5252 if( cnt!=data[hdr+7] ){
5253 checkAppendMsg(pCheck, 0,
5254 "Fragmented space is %d byte reported as %d on page %d",
5255 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00005256 }
5257 }
drh2e38c322004-09-03 18:38:44 +00005258 sqliteFree(hit);
drh6019e162001-07-02 17:51:45 +00005259
drh4b70f112004-05-02 21:12:19 +00005260 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00005261 return depth+1;
drh5eddca62001-06-30 21:53:53 +00005262}
drhb7f91642004-10-31 02:22:47 +00005263#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00005264
drhb7f91642004-10-31 02:22:47 +00005265#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00005266/*
5267** This routine does a complete check of the given BTree file. aRoot[] is
5268** an array of pages numbers were each page number is the root page of
5269** a table. nRoot is the number of entries in aRoot.
5270**
5271** If everything checks out, this routine returns NULL. If something is
5272** amiss, an error message is written into memory obtained from malloc()
5273** and a pointer to that error message is returned. The calling function
5274** is responsible for freeing the error message when it is done.
5275*/
drh3aac2dd2004-04-26 14:10:20 +00005276char *sqlite3BtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){
drh5eddca62001-06-30 21:53:53 +00005277 int i;
5278 int nRef;
drhaaab5722002-02-19 13:39:21 +00005279 IntegrityCk sCheck;
drh5eddca62001-06-30 21:53:53 +00005280
drha34b6762004-05-07 13:30:42 +00005281 nRef = *sqlite3pager_stats(pBt->pPager);
drhefc251d2001-07-01 22:12:01 +00005282 if( lockBtree(pBt)!=SQLITE_OK ){
5283 return sqliteStrDup("Unable to acquire a read lock on the database");
5284 }
drh5eddca62001-06-30 21:53:53 +00005285 sCheck.pBt = pBt;
5286 sCheck.pPager = pBt->pPager;
drha34b6762004-05-07 13:30:42 +00005287 sCheck.nPage = sqlite3pager_pagecount(sCheck.pPager);
drh0de8c112002-07-06 16:32:14 +00005288 if( sCheck.nPage==0 ){
5289 unlockBtreeIfUnused(pBt);
5290 return 0;
5291 }
drh8c1238a2003-01-02 14:43:55 +00005292 sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
drhda200cc2004-05-09 11:51:38 +00005293 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00005294 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00005295 if( i<=sCheck.nPage ){
5296 sCheck.anRef[i] = 1;
5297 }
drh5eddca62001-06-30 21:53:53 +00005298 sCheck.zErrMsg = 0;
5299
5300 /* Check the integrity of the freelist
5301 */
drha34b6762004-05-07 13:30:42 +00005302 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
5303 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00005304
5305 /* Check all the tables.
5306 */
5307 for(i=0; i<nRoot; i++){
drh4ff6dfa2002-03-03 23:06:00 +00005308 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00005309#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00005310 if( pBt->autoVacuum && aRoot[i]>1 ){
5311 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
5312 }
5313#endif
drh1bffb9c2002-02-03 17:37:36 +00005314 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0);
drh5eddca62001-06-30 21:53:53 +00005315 }
5316
5317 /* Make sure every page in the file is referenced
5318 */
5319 for(i=1; i<=sCheck.nPage; i++){
danielk1977afcdd022004-10-31 16:25:42 +00005320#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00005321 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00005322 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00005323 }
danielk1977afcdd022004-10-31 16:25:42 +00005324#else
5325 /* If the database supports auto-vacuum, make sure no tables contain
5326 ** references to pointer-map pages.
5327 */
5328 if( sCheck.anRef[i]==0 &&
drh42cac6d2004-11-20 20:31:11 +00005329 (PTRMAP_PAGENO(pBt->usableSize, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00005330 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
5331 }
5332 if( sCheck.anRef[i]!=0 &&
drh42cac6d2004-11-20 20:31:11 +00005333 (PTRMAP_PAGENO(pBt->usableSize, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00005334 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
5335 }
5336#endif
drh5eddca62001-06-30 21:53:53 +00005337 }
5338
5339 /* Make sure this analysis did not leave any unref() pages
5340 */
drh5e00f6c2001-09-13 13:46:56 +00005341 unlockBtreeIfUnused(pBt);
drha34b6762004-05-07 13:30:42 +00005342 if( nRef != *sqlite3pager_stats(pBt->pPager) ){
drh2e38c322004-09-03 18:38:44 +00005343 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00005344 "Outstanding page count goes from %d to %d during this analysis",
drha34b6762004-05-07 13:30:42 +00005345 nRef, *sqlite3pager_stats(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00005346 );
drh5eddca62001-06-30 21:53:53 +00005347 }
5348
5349 /* Clean up and report errors.
5350 */
5351 sqliteFree(sCheck.anRef);
5352 return sCheck.zErrMsg;
5353}
drhb7f91642004-10-31 02:22:47 +00005354#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00005355
drh73509ee2003-04-06 20:44:45 +00005356/*
5357** Return the full pathname of the underlying database file.
5358*/
drh3aac2dd2004-04-26 14:10:20 +00005359const char *sqlite3BtreeGetFilename(Btree *pBt){
drh73509ee2003-04-06 20:44:45 +00005360 assert( pBt->pPager!=0 );
drha34b6762004-05-07 13:30:42 +00005361 return sqlite3pager_filename(pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00005362}
5363
5364/*
danielk19775865e3d2004-06-14 06:03:57 +00005365** Return the pathname of the directory that contains the database file.
5366*/
5367const char *sqlite3BtreeGetDirname(Btree *pBt){
5368 assert( pBt->pPager!=0 );
5369 return sqlite3pager_dirname(pBt->pPager);
5370}
5371
5372/*
5373** Return the pathname of the journal file for this database. The return
5374** value of this routine is the same regardless of whether the journal file
5375** has been created or not.
5376*/
5377const char *sqlite3BtreeGetJournalname(Btree *pBt){
5378 assert( pBt->pPager!=0 );
5379 return sqlite3pager_journalname(pBt->pPager);
5380}
5381
drhb7f91642004-10-31 02:22:47 +00005382#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00005383/*
drhf7c57532003-04-25 13:22:51 +00005384** Copy the complete content of pBtFrom into pBtTo. A transaction
5385** must be active for both files.
5386**
5387** The size of file pBtFrom may be reduced by this operation.
drh43605152004-05-29 21:46:49 +00005388** If anything goes wrong, the transaction on pBtFrom is rolled back.
drh73509ee2003-04-06 20:44:45 +00005389*/
drh3aac2dd2004-04-26 14:10:20 +00005390int sqlite3BtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){
drhf7c57532003-04-25 13:22:51 +00005391 int rc = SQLITE_OK;
drh2e6d11b2003-04-25 15:37:57 +00005392 Pgno i, nPage, nToPage;
drhf7c57532003-04-25 13:22:51 +00005393
danielk1977ee5741e2004-05-31 10:01:34 +00005394 if( pBtTo->inTrans!=TRANS_WRITE || pBtFrom->inTrans!=TRANS_WRITE ){
5395 return SQLITE_ERROR;
5396 }
drhf7c57532003-04-25 13:22:51 +00005397 if( pBtTo->pCursor ) return SQLITE_BUSY;
drha34b6762004-05-07 13:30:42 +00005398 nToPage = sqlite3pager_pagecount(pBtTo->pPager);
5399 nPage = sqlite3pager_pagecount(pBtFrom->pPager);
danielk1977369f27e2004-06-15 11:40:04 +00005400 for(i=1; rc==SQLITE_OK && i<=nPage; i++){
drhf7c57532003-04-25 13:22:51 +00005401 void *pPage;
drha34b6762004-05-07 13:30:42 +00005402 rc = sqlite3pager_get(pBtFrom->pPager, i, &pPage);
drhf7c57532003-04-25 13:22:51 +00005403 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005404 rc = sqlite3pager_overwrite(pBtTo->pPager, i, pPage);
drh2e6d11b2003-04-25 15:37:57 +00005405 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005406 sqlite3pager_unref(pPage);
drhf7c57532003-04-25 13:22:51 +00005407 }
drh2e6d11b2003-04-25 15:37:57 +00005408 for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
5409 void *pPage;
drha34b6762004-05-07 13:30:42 +00005410 rc = sqlite3pager_get(pBtTo->pPager, i, &pPage);
drh2e6d11b2003-04-25 15:37:57 +00005411 if( rc ) break;
drha34b6762004-05-07 13:30:42 +00005412 rc = sqlite3pager_write(pPage);
5413 sqlite3pager_unref(pPage);
5414 sqlite3pager_dont_write(pBtTo->pPager, i);
drh2e6d11b2003-04-25 15:37:57 +00005415 }
5416 if( !rc && nPage<nToPage ){
drha34b6762004-05-07 13:30:42 +00005417 rc = sqlite3pager_truncate(pBtTo->pPager, nPage);
drh2e6d11b2003-04-25 15:37:57 +00005418 }
drhf7c57532003-04-25 13:22:51 +00005419 if( rc ){
drh3aac2dd2004-04-26 14:10:20 +00005420 sqlite3BtreeRollback(pBtTo);
drhf7c57532003-04-25 13:22:51 +00005421 }
5422 return rc;
drh73509ee2003-04-06 20:44:45 +00005423}
drhb7f91642004-10-31 02:22:47 +00005424#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00005425
5426/*
5427** Return non-zero if a transaction is active.
5428*/
5429int sqlite3BtreeIsInTrans(Btree *pBt){
danielk1977ee5741e2004-05-31 10:01:34 +00005430 return (pBt && (pBt->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00005431}
5432
5433/*
5434** Return non-zero if a statement transaction is active.
5435*/
5436int sqlite3BtreeIsInStmt(Btree *pBt){
5437 return (pBt && pBt->inStmt);
5438}
danielk197713adf8a2004-06-03 16:08:41 +00005439
5440/*
5441** This call is a no-op if no write-transaction is currently active on pBt.
5442**
5443** Otherwise, sync the database file for the btree pBt. zMaster points to
5444** the name of a master journal file that should be written into the
5445** individual journal file, or is NULL, indicating no master journal file
5446** (single database transaction).
5447**
5448** When this is called, the master journal should already have been
5449** created, populated with this journal pointer and synced to disk.
5450**
5451** Once this is routine has returned, the only thing required to commit
5452** the write-transaction for this database file is to delete the journal.
5453*/
5454int sqlite3BtreeSync(Btree *pBt, const char *zMaster){
5455 if( pBt->inTrans==TRANS_WRITE ){
danielk1977687566d2004-11-02 12:56:41 +00005456#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977d761c0c2004-11-05 16:37:02 +00005457 Pgno nTrunc = 0;
danielk1977687566d2004-11-02 12:56:41 +00005458 if( pBt->autoVacuum ){
danielk1977d761c0c2004-11-05 16:37:02 +00005459 int rc = autoVacuumCommit(pBt, &nTrunc);
danielk1977687566d2004-11-02 12:56:41 +00005460 if( rc!=SQLITE_OK ) return rc;
5461 }
danielk1977d761c0c2004-11-05 16:37:02 +00005462 return sqlite3pager_sync(pBt->pPager, zMaster, nTrunc);
danielk1977687566d2004-11-02 12:56:41 +00005463#endif
danielk1977d761c0c2004-11-05 16:37:02 +00005464 return sqlite3pager_sync(pBt->pPager, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00005465 }
5466 return SQLITE_OK;
5467}