blob: 8e599809a5b327594190248f576615251d5044b0 [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
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*************************************************************************
drhf74b8d92002-09-01 23:20:45 +000012** $Id: btree.c,v 1.72 2002/09/01 23:20:45 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
39** key and data for any entry are combined to form the "payload". Up to
40** MX_LOCAL_PAYLOAD bytes of payload can be carried directly on the
41** database page. If the payload is larger than MX_LOCAL_PAYLOAD bytes
42** then surplus bytes are stored on overflow pages. The payload for an
43** entry and the preceding pointer are combined to form a "Cell". Each
drhb19a2bc2001-09-16 00:13:26 +000044** page has a small header which contains the Ptr(N+1) pointer.
drh8b2f49b2001-06-08 00:21:52 +000045**
46** The first page of the file contains a magic string used to verify that
47** the file really is a valid BTree database, a pointer to a list of unused
48** pages in the file, and some meta information. The root of the first
49** BTree begins on page 2 of the file. (Pages are numbered beginning with
50** 1, not 0.) Thus a minimum database contains 2 pages.
drha059ad02001-04-17 20:09:11 +000051*/
52#include "sqliteInt.h"
53#include "pager.h"
54#include "btree.h"
55#include <assert.h>
56
drh8c42ca92001-06-22 19:15:00 +000057/*
drh0d316a42002-08-11 20:10:47 +000058** Macros used for byteswapping. B is a pointer to the Btree
59** structure. This is needed to access the Btree.needSwab boolean
60** in order to tell if byte swapping is needed or not.
61** X is an unsigned integer. SWAB16 byte swaps a 16-bit integer.
62** SWAB32 byteswaps a 32-bit integer.
63*/
64#define SWAB16(B,X) ((B)->needSwab? swab16(X) : (X))
65#define SWAB32(B,X) ((B)->needSwab? swab32(X) : (X))
66#define SWAB_ADD(B,X,A) \
67 if((B)->needSwab){ X=swab32(swab32(X)+A); }else{ X += (A); }
68
69/*
70** The following global variable - available only if SQLITE_TEST is
71** defined - is used to determine whether new databases are created in
72** native byte order or in non-native byte order. Non-native byte order
73** databases are created for testing purposes only. Under normal operation,
74** only native byte-order databases should be created, but we should be
75** able to read or write existing databases regardless of the byteorder.
76*/
77#ifdef SQLITE_TEST
78int btree_native_byte_order = 1;
drh74587e52002-08-13 00:01:16 +000079#else
80# define btree_native_byte_order 1
drh0d316a42002-08-11 20:10:47 +000081#endif
82
83/*
drh365d68f2001-05-11 11:02:46 +000084** Forward declarations of structures used only in this file.
85*/
drhbd03cae2001-06-02 02:40:57 +000086typedef struct PageOne PageOne;
drh2af926b2001-05-15 00:39:25 +000087typedef struct MemPage MemPage;
drh365d68f2001-05-11 11:02:46 +000088typedef struct PageHdr PageHdr;
89typedef struct Cell Cell;
drh3b7511c2001-05-26 13:15:44 +000090typedef struct CellHdr CellHdr;
drh365d68f2001-05-11 11:02:46 +000091typedef struct FreeBlk FreeBlk;
drh2af926b2001-05-15 00:39:25 +000092typedef struct OverflowPage OverflowPage;
drh30e58752002-03-02 20:41:57 +000093typedef struct FreelistInfo FreelistInfo;
drh2af926b2001-05-15 00:39:25 +000094
95/*
96** All structures on a database page are aligned to 4-byte boundries.
97** This routine rounds up a number of bytes to the next multiple of 4.
drh306dc212001-05-21 13:45:10 +000098**
99** This might need to change for computer architectures that require
100** and 8-byte alignment boundry for structures.
drh2af926b2001-05-15 00:39:25 +0000101*/
102#define ROUNDUP(X) ((X+3) & ~3)
drha059ad02001-04-17 20:09:11 +0000103
drh08ed44e2001-04-29 23:32:55 +0000104/*
drhbd03cae2001-06-02 02:40:57 +0000105** This is a magic string that appears at the beginning of every
drh8c42ca92001-06-22 19:15:00 +0000106** SQLite database in order to identify the file as a real database.
drh08ed44e2001-04-29 23:32:55 +0000107*/
drhbd03cae2001-06-02 02:40:57 +0000108static const char zMagicHeader[] =
drh80ff32f2001-11-04 18:32:46 +0000109 "** This file contains an SQLite 2.1 database **";
drhbd03cae2001-06-02 02:40:57 +0000110#define MAGIC_SIZE (sizeof(zMagicHeader))
drh08ed44e2001-04-29 23:32:55 +0000111
112/*
drh5e00f6c2001-09-13 13:46:56 +0000113** This is a magic integer also used to test the integrity of the database
drh8c42ca92001-06-22 19:15:00 +0000114** file. This integer is used in addition to the string above so that
115** if the file is written on a little-endian architecture and read
116** on a big-endian architectures (or vice versa) we can detect the
117** problem.
118**
119** The number used was obtained at random and has no special
drhb19a2bc2001-09-16 00:13:26 +0000120** significance other than the fact that it represents a different
121** integer on little-endian and big-endian machines.
drh8c42ca92001-06-22 19:15:00 +0000122*/
123#define MAGIC 0xdae37528
124
125/*
drhbd03cae2001-06-02 02:40:57 +0000126** The first page of the database file contains a magic header string
127** to identify the file as an SQLite database file. It also contains
128** a pointer to the first free page of the file. Page 2 contains the
drh8b2f49b2001-06-08 00:21:52 +0000129** root of the principle BTree. The file might contain other BTrees
130** rooted on pages above 2.
131**
132** The first page also contains SQLITE_N_BTREE_META integers that
133** can be used by higher-level routines.
drh08ed44e2001-04-29 23:32:55 +0000134**
drhbd03cae2001-06-02 02:40:57 +0000135** Remember that pages are numbered beginning with 1. (See pager.c
136** for additional information.) Page 0 does not exist and a page
137** number of 0 is used to mean "no such page".
138*/
139struct PageOne {
140 char zMagic[MAGIC_SIZE]; /* String that identifies the file as a database */
drh8c42ca92001-06-22 19:15:00 +0000141 int iMagic; /* Integer to verify correct byte order */
142 Pgno freeList; /* First free page in a list of all free pages */
drh2aa679f2001-06-25 02:11:07 +0000143 int nFree; /* Number of pages on the free list */
144 int aMeta[SQLITE_N_BTREE_META-1]; /* User defined integers */
drhbd03cae2001-06-02 02:40:57 +0000145};
146
147/*
148** Each database page has a header that is an instance of this
149** structure.
drh08ed44e2001-04-29 23:32:55 +0000150**
drh8b2f49b2001-06-08 00:21:52 +0000151** PageHdr.firstFree is 0 if there is no free space on this page.
drh14acc042001-06-10 19:56:58 +0000152** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a
drh8b2f49b2001-06-08 00:21:52 +0000153** FreeBlk structure that describes the first block of free space.
154** All free space is defined by a linked list of FreeBlk structures.
drh08ed44e2001-04-29 23:32:55 +0000155**
drh8b2f49b2001-06-08 00:21:52 +0000156** Data is stored in a linked list of Cell structures. PageHdr.firstCell
drh14acc042001-06-10 19:56:58 +0000157** is the index into MemPage.u.aDisk[] of the first cell on the page. The
drh306dc212001-05-21 13:45:10 +0000158** Cells are kept in sorted order.
drh8b2f49b2001-06-08 00:21:52 +0000159**
160** A Cell contains all information about a database entry and a pointer
161** to a child page that contains other entries less than itself. In
162** other words, the i-th Cell contains both Ptr(i) and Key(i). The
163** right-most pointer of the page is contained in PageHdr.rightChild.
drh08ed44e2001-04-29 23:32:55 +0000164*/
drh365d68f2001-05-11 11:02:46 +0000165struct PageHdr {
drh5e2f8b92001-05-28 00:41:15 +0000166 Pgno rightChild; /* Child page that comes after all cells on this page */
drh14acc042001-06-10 19:56:58 +0000167 u16 firstCell; /* Index in MemPage.u.aDisk[] of the first cell */
168 u16 firstFree; /* Index in MemPage.u.aDisk[] of the first free block */
drh365d68f2001-05-11 11:02:46 +0000169};
drh306dc212001-05-21 13:45:10 +0000170
drh3b7511c2001-05-26 13:15:44 +0000171/*
172** Entries on a page of the database are called "Cells". Each Cell
173** has a header and data. This structure defines the header. The
drhbd03cae2001-06-02 02:40:57 +0000174** key and data (collectively the "payload") follow this header on
175** the database page.
176**
177** A definition of the complete Cell structure is given below. The
drh8c42ca92001-06-22 19:15:00 +0000178** header for the cell must be defined first in order to do some
drhbd03cae2001-06-02 02:40:57 +0000179** of the sizing #defines that follow.
drh3b7511c2001-05-26 13:15:44 +0000180*/
181struct CellHdr {
drh5e2f8b92001-05-28 00:41:15 +0000182 Pgno leftChild; /* Child page that comes before this cell */
drh3b7511c2001-05-26 13:15:44 +0000183 u16 nKey; /* Number of bytes in the key */
drh14acc042001-06-10 19:56:58 +0000184 u16 iNext; /* Index in MemPage.u.aDisk[] of next cell in sorted order */
drh58a11682001-11-10 13:51:08 +0000185 u8 nKeyHi; /* Upper 8 bits of key size for keys larger than 64K bytes */
186 u8 nDataHi; /* Upper 8 bits of data size when the size is more than 64K */
drh80ff32f2001-11-04 18:32:46 +0000187 u16 nData; /* Number of bytes of data */
drh8c42ca92001-06-22 19:15:00 +0000188};
drh58a11682001-11-10 13:51:08 +0000189
190/*
191** The key and data size are split into a lower 16-bit segment and an
192** upper 8-bit segment in order to pack them together into a smaller
193** space. The following macros reassembly a key or data size back
194** into an integer.
195*/
drh0d316a42002-08-11 20:10:47 +0000196#define NKEY(b,h) (SWAB16(b,h.nKey) + h.nKeyHi*65536)
197#define NDATA(b,h) (SWAB16(b,h.nData) + h.nDataHi*65536)
drh3b7511c2001-05-26 13:15:44 +0000198
199/*
200** The minimum size of a complete Cell. The Cell must contain a header
drhbd03cae2001-06-02 02:40:57 +0000201** and at least 4 bytes of payload.
drh3b7511c2001-05-26 13:15:44 +0000202*/
203#define MIN_CELL_SIZE (sizeof(CellHdr)+4)
204
205/*
206** The maximum number of database entries that can be held in a single
207** page of the database.
208*/
209#define MX_CELL ((SQLITE_PAGE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)
210
211/*
drh6019e162001-07-02 17:51:45 +0000212** The amount of usable space on a single page of the BTree. This is the
213** page size minus the overhead of the page header.
214*/
215#define USABLE_SPACE (SQLITE_PAGE_SIZE - sizeof(PageHdr))
216
217/*
drh8c42ca92001-06-22 19:15:00 +0000218** The maximum amount of payload (in bytes) that can be stored locally for
219** a database entry. If the entry contains more data than this, the
drh3b7511c2001-05-26 13:15:44 +0000220** extra goes onto overflow pages.
drhbd03cae2001-06-02 02:40:57 +0000221**
222** This number is chosen so that at least 4 cells will fit on every page.
drh3b7511c2001-05-26 13:15:44 +0000223*/
drh6019e162001-07-02 17:51:45 +0000224#define MX_LOCAL_PAYLOAD ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3)
drh3b7511c2001-05-26 13:15:44 +0000225
drh306dc212001-05-21 13:45:10 +0000226/*
227** Data on a database page is stored as a linked list of Cell structures.
drh5e2f8b92001-05-28 00:41:15 +0000228** Both the key and the data are stored in aPayload[]. The key always comes
229** first. The aPayload[] field grows as necessary to hold the key and data,
drh306dc212001-05-21 13:45:10 +0000230** up to a maximum of MX_LOCAL_PAYLOAD bytes. If the size of the key and
drh3b7511c2001-05-26 13:15:44 +0000231** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the
232** page number of the first overflow page.
233**
234** Though this structure is fixed in size, the Cell on the database
drhbd03cae2001-06-02 02:40:57 +0000235** page varies in size. Every cell has a CellHdr and at least 4 bytes
drh3b7511c2001-05-26 13:15:44 +0000236** of payload space. Additional payload bytes (up to the maximum of
237** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as
238** needed.
drh306dc212001-05-21 13:45:10 +0000239*/
drh365d68f2001-05-11 11:02:46 +0000240struct Cell {
drh5e2f8b92001-05-28 00:41:15 +0000241 CellHdr h; /* The cell header */
242 char aPayload[MX_LOCAL_PAYLOAD]; /* Key and data */
243 Pgno ovfl; /* The first overflow page */
drh365d68f2001-05-11 11:02:46 +0000244};
drh306dc212001-05-21 13:45:10 +0000245
246/*
247** Free space on a page is remembered using a linked list of the FreeBlk
248** structures. Space on a database page is allocated in increments of
drh72f82862001-05-24 21:06:34 +0000249** at least 4 bytes and is always aligned to a 4-byte boundry. The
drh8b2f49b2001-06-08 00:21:52 +0000250** linked list of FreeBlks is always kept in order by address.
drh306dc212001-05-21 13:45:10 +0000251*/
drh365d68f2001-05-11 11:02:46 +0000252struct FreeBlk {
drh72f82862001-05-24 21:06:34 +0000253 u16 iSize; /* Number of bytes in this block of free space */
drh14acc042001-06-10 19:56:58 +0000254 u16 iNext; /* Index in MemPage.u.aDisk[] of the next free block */
drh365d68f2001-05-11 11:02:46 +0000255};
drh306dc212001-05-21 13:45:10 +0000256
257/*
drh14acc042001-06-10 19:56:58 +0000258** The number of bytes of payload that will fit on a single overflow page.
drh3b7511c2001-05-26 13:15:44 +0000259*/
260#define OVERFLOW_SIZE (SQLITE_PAGE_SIZE-sizeof(Pgno))
261
262/*
drh306dc212001-05-21 13:45:10 +0000263** When the key and data for a single entry in the BTree will not fit in
drh8c42ca92001-06-22 19:15:00 +0000264** the MX_LOCAL_PAYLOAD bytes of space available on the database page,
drh8b2f49b2001-06-08 00:21:52 +0000265** then all extra bytes are written to a linked list of overflow pages.
drh306dc212001-05-21 13:45:10 +0000266** Each overflow page is an instance of the following structure.
267**
268** Unused pages in the database are also represented by instances of
drhbd03cae2001-06-02 02:40:57 +0000269** the OverflowPage structure. The PageOne.freeList field is the
drh306dc212001-05-21 13:45:10 +0000270** page number of the first page in a linked list of unused database
271** pages.
272*/
drh2af926b2001-05-15 00:39:25 +0000273struct OverflowPage {
drh14acc042001-06-10 19:56:58 +0000274 Pgno iNext;
drh5e2f8b92001-05-28 00:41:15 +0000275 char aPayload[OVERFLOW_SIZE];
drh7e3b0a02001-04-28 16:52:40 +0000276};
drh7e3b0a02001-04-28 16:52:40 +0000277
278/*
drh30e58752002-03-02 20:41:57 +0000279** The PageOne.freeList field points to a linked list of overflow pages
280** hold information about free pages. The aPayload section of each
281** overflow page contains an instance of the following structure. The
282** aFree[] array holds the page number of nFree unused pages in the disk
283** file.
284*/
285struct FreelistInfo {
286 int nFree;
287 Pgno aFree[(OVERFLOW_SIZE-sizeof(int))/sizeof(Pgno)];
288};
289
290/*
drh7e3b0a02001-04-28 16:52:40 +0000291** For every page in the database file, an instance of the following structure
drh14acc042001-06-10 19:56:58 +0000292** is stored in memory. The u.aDisk[] array contains the raw bits read from
drh6446c4d2001-12-15 14:22:18 +0000293** the disk. The rest is auxiliary information held in memory only. The
drhbd03cae2001-06-02 02:40:57 +0000294** auxiliary info is only valid for regular database pages - it is not
295** used for overflow pages and pages on the freelist.
drh306dc212001-05-21 13:45:10 +0000296**
drhbd03cae2001-06-02 02:40:57 +0000297** Of particular interest in the auxiliary info is the apCell[] entry. Each
drh14acc042001-06-10 19:56:58 +0000298** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are
drh306dc212001-05-21 13:45:10 +0000299** put in this array so that they can be accessed in constant time, rather
drhbd03cae2001-06-02 02:40:57 +0000300** than in linear time which would be needed if we had to walk the linked
301** list on every access.
drh72f82862001-05-24 21:06:34 +0000302**
drh14acc042001-06-10 19:56:58 +0000303** Note that apCell[] contains enough space to hold up to two more Cells
304** than can possibly fit on one page. In the steady state, every apCell[]
305** points to memory inside u.aDisk[]. But in the middle of an insert
306** operation, some apCell[] entries may temporarily point to data space
307** outside of u.aDisk[]. This is a transient situation that is quickly
308** resolved. But while it is happening, it is possible for a database
309** page to hold as many as two more cells than it might otherwise hold.
drh18b81e52001-11-01 13:52:52 +0000310** The extra two entries in apCell[] are an allowance for this situation.
drh14acc042001-06-10 19:56:58 +0000311**
drh72f82862001-05-24 21:06:34 +0000312** The pParent field points back to the parent page. This allows us to
313** walk up the BTree from any leaf to the root. Care must be taken to
314** unref() the parent page pointer when this page is no longer referenced.
drhbd03cae2001-06-02 02:40:57 +0000315** The pageDestructor() routine handles that chore.
drh7e3b0a02001-04-28 16:52:40 +0000316*/
317struct MemPage {
drh14acc042001-06-10 19:56:58 +0000318 union {
319 char aDisk[SQLITE_PAGE_SIZE]; /* Page data stored on disk */
320 PageHdr hdr; /* Overlay page header */
321 } u;
drh5e2f8b92001-05-28 00:41:15 +0000322 int isInit; /* True if auxiliary data is initialized */
drh72f82862001-05-24 21:06:34 +0000323 MemPage *pParent; /* The parent of this page. NULL for root */
drh14acc042001-06-10 19:56:58 +0000324 int nFree; /* Number of free bytes in u.aDisk[] */
drh306dc212001-05-21 13:45:10 +0000325 int nCell; /* Number of entries on this page */
drh14acc042001-06-10 19:56:58 +0000326 int isOverfull; /* Some apCell[] points outside u.aDisk[] */
327 Cell *apCell[MX_CELL+2]; /* All data entires in sorted order */
drh8c42ca92001-06-22 19:15:00 +0000328};
drh7e3b0a02001-04-28 16:52:40 +0000329
330/*
drh3b7511c2001-05-26 13:15:44 +0000331** The in-memory image of a disk page has the auxiliary information appended
332** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
333** that extra information.
334*/
335#define EXTRA_SIZE (sizeof(MemPage)-SQLITE_PAGE_SIZE)
336
337/*
drha059ad02001-04-17 20:09:11 +0000338** Everything we need to know about an open database
339*/
340struct Btree {
341 Pager *pPager; /* The page cache */
drh306dc212001-05-21 13:45:10 +0000342 BtCursor *pCursor; /* A list of all open cursors */
drhbd03cae2001-06-02 02:40:57 +0000343 PageOne *page1; /* First page of the database */
drh663fc632002-02-02 18:49:19 +0000344 u8 inTrans; /* True if a transaction is in progress */
345 u8 inCkpt; /* True if there is a checkpoint on the transaction */
drh5df72a52002-06-06 23:16:05 +0000346 u8 readOnly; /* True if the underlying file is readonly */
drh0d316a42002-08-11 20:10:47 +0000347 u8 needSwab; /* Need to byte-swapping */
drha059ad02001-04-17 20:09:11 +0000348};
349typedef Btree Bt;
350
drh365d68f2001-05-11 11:02:46 +0000351/*
352** A cursor is a pointer to a particular entry in the BTree.
353** The entry is identified by its MemPage and the index in
drh5e2f8b92001-05-28 00:41:15 +0000354** MemPage.apCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000355*/
drh72f82862001-05-24 21:06:34 +0000356struct BtCursor {
drh5e2f8b92001-05-28 00:41:15 +0000357 Btree *pBt; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000358 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drhf74b8d92002-09-01 23:20:45 +0000359 BtCursor *pShared; /* Loop of cursors with the same root page */
drh8b2f49b2001-06-08 00:21:52 +0000360 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000361 MemPage *pPage; /* Page that contains the entry */
drh8c42ca92001-06-22 19:15:00 +0000362 int idx; /* Index of the entry in pPage->apCell[] */
drhecdc7532001-09-23 02:35:53 +0000363 u8 wrFlag; /* True if writable */
drh5e2f8b92001-05-28 00:41:15 +0000364 u8 bSkipNext; /* sqliteBtreeNext() is no-op if true */
365 u8 iMatch; /* compare result from last sqliteBtreeMoveto() */
drh365d68f2001-05-11 11:02:46 +0000366};
drh7e3b0a02001-04-28 16:52:40 +0000367
drha059ad02001-04-17 20:09:11 +0000368/*
drh0d316a42002-08-11 20:10:47 +0000369** Routines for byte swapping.
370*/
371u16 swab16(u16 x){
372 return ((x & 0xff)<<8) | ((x>>8)&0xff);
373}
374u32 swab32(u32 x){
375 return ((x & 0xff)<<24) | ((x & 0xff00)<<8) |
376 ((x>>8) & 0xff00) | ((x>>24)&0xff);
377}
378
379/*
drh3b7511c2001-05-26 13:15:44 +0000380** Compute the total number of bytes that a Cell needs on the main
drh5e2f8b92001-05-28 00:41:15 +0000381** database page. The number returned includes the Cell header,
382** local payload storage, and the pointer to overflow pages (if
drh8c42ca92001-06-22 19:15:00 +0000383** applicable). Additional space allocated on overflow pages
drhbd03cae2001-06-02 02:40:57 +0000384** is NOT included in the value returned from this routine.
drh3b7511c2001-05-26 13:15:44 +0000385*/
drh0d316a42002-08-11 20:10:47 +0000386static int cellSize(Btree *pBt, Cell *pCell){
387 int n = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h);
drh3b7511c2001-05-26 13:15:44 +0000388 if( n>MX_LOCAL_PAYLOAD ){
389 n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
390 }else{
391 n = ROUNDUP(n);
392 }
393 n += sizeof(CellHdr);
394 return n;
395}
396
397/*
drh72f82862001-05-24 21:06:34 +0000398** Defragment the page given. All Cells are moved to the
399** beginning of the page and all free space is collected
400** into one big FreeBlk at the end of the page.
drh365d68f2001-05-11 11:02:46 +0000401*/
drh0d316a42002-08-11 20:10:47 +0000402static void defragmentPage(Btree *pBt, MemPage *pPage){
drh14acc042001-06-10 19:56:58 +0000403 int pc, i, n;
drh2af926b2001-05-15 00:39:25 +0000404 FreeBlk *pFBlk;
405 char newPage[SQLITE_PAGE_SIZE];
406
drh6019e162001-07-02 17:51:45 +0000407 assert( sqlitepager_iswriteable(pPage) );
drh7aa128d2002-06-21 13:09:16 +0000408 assert( pPage->isInit );
drhbd03cae2001-06-02 02:40:57 +0000409 pc = sizeof(PageHdr);
drh0d316a42002-08-11 20:10:47 +0000410 pPage->u.hdr.firstCell = SWAB16(pBt, pc);
drh14acc042001-06-10 19:56:58 +0000411 memcpy(newPage, pPage->u.aDisk, pc);
drh2af926b2001-05-15 00:39:25 +0000412 for(i=0; i<pPage->nCell; i++){
drh2aa679f2001-06-25 02:11:07 +0000413 Cell *pCell = pPage->apCell[i];
drh8c42ca92001-06-22 19:15:00 +0000414
415 /* This routine should never be called on an overfull page. The
416 ** following asserts verify that constraint. */
drh7c717f72001-06-24 20:39:41 +0000417 assert( Addr(pCell) > Addr(pPage) );
418 assert( Addr(pCell) < Addr(pPage) + SQLITE_PAGE_SIZE );
drh8c42ca92001-06-22 19:15:00 +0000419
drh0d316a42002-08-11 20:10:47 +0000420 n = cellSize(pBt, pCell);
421 pCell->h.iNext = SWAB16(pBt, pc + n);
drh2af926b2001-05-15 00:39:25 +0000422 memcpy(&newPage[pc], pCell, n);
drh14acc042001-06-10 19:56:58 +0000423 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc];
drh2af926b2001-05-15 00:39:25 +0000424 pc += n;
425 }
drh72f82862001-05-24 21:06:34 +0000426 assert( pPage->nFree==SQLITE_PAGE_SIZE-pc );
drh14acc042001-06-10 19:56:58 +0000427 memcpy(pPage->u.aDisk, newPage, pc);
drh2aa679f2001-06-25 02:11:07 +0000428 if( pPage->nCell>0 ){
429 pPage->apCell[pPage->nCell-1]->h.iNext = 0;
430 }
drh8c42ca92001-06-22 19:15:00 +0000431 pFBlk = (FreeBlk*)&pPage->u.aDisk[pc];
drh0d316a42002-08-11 20:10:47 +0000432 pFBlk->iSize = SWAB16(pBt, SQLITE_PAGE_SIZE - pc);
drh2af926b2001-05-15 00:39:25 +0000433 pFBlk->iNext = 0;
drh0d316a42002-08-11 20:10:47 +0000434 pPage->u.hdr.firstFree = SWAB16(pBt, pc);
drh2af926b2001-05-15 00:39:25 +0000435 memset(&pFBlk[1], 0, SQLITE_PAGE_SIZE - pc - sizeof(FreeBlk));
drh365d68f2001-05-11 11:02:46 +0000436}
437
drha059ad02001-04-17 20:09:11 +0000438/*
drh8b2f49b2001-06-08 00:21:52 +0000439** Allocate nByte bytes of space on a page. nByte must be a
440** multiple of 4.
drhbd03cae2001-06-02 02:40:57 +0000441**
drh14acc042001-06-10 19:56:58 +0000442** Return the index into pPage->u.aDisk[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000443** the new allocation. Or return 0 if there is not enough free
444** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000445**
drh72f82862001-05-24 21:06:34 +0000446** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000447** nBytes of contiguous free space, then this routine automatically
448** calls defragementPage() to consolidate all free space before
449** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000450*/
drh0d316a42002-08-11 20:10:47 +0000451static int allocateSpace(Btree *pBt, MemPage *pPage, int nByte){
drh2af926b2001-05-15 00:39:25 +0000452 FreeBlk *p;
453 u16 *pIdx;
454 int start;
drh8c42ca92001-06-22 19:15:00 +0000455 int cnt = 0;
drh0d316a42002-08-11 20:10:47 +0000456 int iSize;
drh72f82862001-05-24 21:06:34 +0000457
drh6019e162001-07-02 17:51:45 +0000458 assert( sqlitepager_iswriteable(pPage) );
drh5e2f8b92001-05-28 00:41:15 +0000459 assert( nByte==ROUNDUP(nByte) );
drh7aa128d2002-06-21 13:09:16 +0000460 assert( pPage->isInit );
drh14acc042001-06-10 19:56:58 +0000461 if( pPage->nFree<nByte || pPage->isOverfull ) return 0;
462 pIdx = &pPage->u.hdr.firstFree;
drh0d316a42002-08-11 20:10:47 +0000463 p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
464 while( (iSize = SWAB16(pBt, p->iSize))<nByte ){
drh8c42ca92001-06-22 19:15:00 +0000465 assert( cnt++ < SQLITE_PAGE_SIZE/4 );
drh2af926b2001-05-15 00:39:25 +0000466 if( p->iNext==0 ){
drh0d316a42002-08-11 20:10:47 +0000467 defragmentPage(pBt, pPage);
drh14acc042001-06-10 19:56:58 +0000468 pIdx = &pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000469 }else{
470 pIdx = &p->iNext;
471 }
drh0d316a42002-08-11 20:10:47 +0000472 p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
drh2af926b2001-05-15 00:39:25 +0000473 }
drh0d316a42002-08-11 20:10:47 +0000474 if( iSize==nByte ){
475 start = SWAB16(pBt, *pIdx);
drh2af926b2001-05-15 00:39:25 +0000476 *pIdx = p->iNext;
477 }else{
drh8c42ca92001-06-22 19:15:00 +0000478 FreeBlk *pNew;
drh0d316a42002-08-11 20:10:47 +0000479 start = SWAB16(pBt, *pIdx);
drh8c42ca92001-06-22 19:15:00 +0000480 pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte];
drh72f82862001-05-24 21:06:34 +0000481 pNew->iNext = p->iNext;
drh0d316a42002-08-11 20:10:47 +0000482 pNew->iSize = SWAB16(pBt, iSize - nByte);
483 *pIdx = SWAB16(pBt, start + nByte);
drh2af926b2001-05-15 00:39:25 +0000484 }
485 pPage->nFree -= nByte;
486 return start;
drh7e3b0a02001-04-28 16:52:40 +0000487}
488
489/*
drh14acc042001-06-10 19:56:58 +0000490** Return a section of the MemPage.u.aDisk[] to the freelist.
491** The first byte of the new free block is pPage->u.aDisk[start]
492** and the size of the block is "size" bytes. Size must be
493** a multiple of 4.
drh306dc212001-05-21 13:45:10 +0000494**
495** Most of the effort here is involved in coalesing adjacent
496** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000497*/
drh0d316a42002-08-11 20:10:47 +0000498static void freeSpace(Btree *pBt, MemPage *pPage, int start, int size){
drh2af926b2001-05-15 00:39:25 +0000499 int end = start + size;
500 u16 *pIdx, idx;
501 FreeBlk *pFBlk;
502 FreeBlk *pNew;
503 FreeBlk *pNext;
drh0d316a42002-08-11 20:10:47 +0000504 int iSize;
drh2af926b2001-05-15 00:39:25 +0000505
drh6019e162001-07-02 17:51:45 +0000506 assert( sqlitepager_iswriteable(pPage) );
drh2af926b2001-05-15 00:39:25 +0000507 assert( size == ROUNDUP(size) );
508 assert( start == ROUNDUP(start) );
drh7aa128d2002-06-21 13:09:16 +0000509 assert( pPage->isInit );
drh14acc042001-06-10 19:56:58 +0000510 pIdx = &pPage->u.hdr.firstFree;
drh0d316a42002-08-11 20:10:47 +0000511 idx = SWAB16(pBt, *pIdx);
drh2af926b2001-05-15 00:39:25 +0000512 while( idx!=0 && idx<start ){
drh14acc042001-06-10 19:56:58 +0000513 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +0000514 iSize = SWAB16(pBt, pFBlk->iSize);
515 if( idx + iSize == start ){
516 pFBlk->iSize = SWAB16(pBt, iSize + size);
517 if( idx + iSize + size == SWAB16(pBt, pFBlk->iNext) ){
518 pNext = (FreeBlk*)&pPage->u.aDisk[idx + iSize + size];
519 if( pBt->needSwab ){
520 pFBlk->iSize = swab16(swab16(pNext->iSize)+iSize+size);
521 }else{
522 pFBlk->iSize += pNext->iSize;
523 }
drh2af926b2001-05-15 00:39:25 +0000524 pFBlk->iNext = pNext->iNext;
525 }
526 pPage->nFree += size;
527 return;
528 }
529 pIdx = &pFBlk->iNext;
drh0d316a42002-08-11 20:10:47 +0000530 idx = SWAB16(pBt, *pIdx);
drh2af926b2001-05-15 00:39:25 +0000531 }
drh14acc042001-06-10 19:56:58 +0000532 pNew = (FreeBlk*)&pPage->u.aDisk[start];
drh2af926b2001-05-15 00:39:25 +0000533 if( idx != end ){
drh0d316a42002-08-11 20:10:47 +0000534 pNew->iSize = SWAB16(pBt, size);
535 pNew->iNext = SWAB16(pBt, idx);
drh2af926b2001-05-15 00:39:25 +0000536 }else{
drh14acc042001-06-10 19:56:58 +0000537 pNext = (FreeBlk*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +0000538 pNew->iSize = SWAB16(pBt, size + SWAB16(pBt, pNext->iSize));
drh2af926b2001-05-15 00:39:25 +0000539 pNew->iNext = pNext->iNext;
540 }
drh0d316a42002-08-11 20:10:47 +0000541 *pIdx = SWAB16(pBt, start);
drh2af926b2001-05-15 00:39:25 +0000542 pPage->nFree += size;
drh7e3b0a02001-04-28 16:52:40 +0000543}
544
545/*
546** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000547**
drhbd03cae2001-06-02 02:40:57 +0000548** The pParent parameter must be a pointer to the MemPage which
549** is the parent of the page being initialized. The root of the
drh8b2f49b2001-06-08 00:21:52 +0000550** BTree (usually page 2) has no parent and so for that page,
551** pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000552**
drh72f82862001-05-24 21:06:34 +0000553** Return SQLITE_OK on success. If we see that the page does
554** not contained a well-formed database page, then return
555** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
556** guarantee that the page is well-formed. It only shows that
557** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000558*/
drh0d316a42002-08-11 20:10:47 +0000559static int initPage(Bt *pBt, MemPage *pPage, Pgno pgnoThis, MemPage *pParent){
drh14acc042001-06-10 19:56:58 +0000560 int idx; /* An index into pPage->u.aDisk[] */
561 Cell *pCell; /* A pointer to a Cell in pPage->u.aDisk[] */
562 FreeBlk *pFBlk; /* A pointer to a free block in pPage->u.aDisk[] */
drh5e2f8b92001-05-28 00:41:15 +0000563 int sz; /* The size of a Cell in bytes */
564 int freeSpace; /* Amount of free space on the page */
drh2af926b2001-05-15 00:39:25 +0000565
drh5e2f8b92001-05-28 00:41:15 +0000566 if( pPage->pParent ){
567 assert( pPage->pParent==pParent );
568 return SQLITE_OK;
569 }
570 if( pParent ){
571 pPage->pParent = pParent;
572 sqlitepager_ref(pParent);
573 }
574 if( pPage->isInit ) return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000575 pPage->isInit = 1;
drh7e3b0a02001-04-28 16:52:40 +0000576 pPage->nCell = 0;
drh6019e162001-07-02 17:51:45 +0000577 freeSpace = USABLE_SPACE;
drh0d316a42002-08-11 20:10:47 +0000578 idx = SWAB16(pBt, pPage->u.hdr.firstCell);
drh7e3b0a02001-04-28 16:52:40 +0000579 while( idx!=0 ){
drh8c42ca92001-06-22 19:15:00 +0000580 if( idx>SQLITE_PAGE_SIZE-MIN_CELL_SIZE ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000581 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh8c42ca92001-06-22 19:15:00 +0000582 if( idx!=ROUNDUP(idx) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000583 pCell = (Cell*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +0000584 sz = cellSize(pBt, pCell);
drh5e2f8b92001-05-28 00:41:15 +0000585 if( idx+sz > SQLITE_PAGE_SIZE ) goto page_format_error;
586 freeSpace -= sz;
587 pPage->apCell[pPage->nCell++] = pCell;
drh0d316a42002-08-11 20:10:47 +0000588 idx = SWAB16(pBt, pCell->h.iNext);
drh2af926b2001-05-15 00:39:25 +0000589 }
590 pPage->nFree = 0;
drh0d316a42002-08-11 20:10:47 +0000591 idx = SWAB16(pBt, pPage->u.hdr.firstFree);
drh2af926b2001-05-15 00:39:25 +0000592 while( idx!=0 ){
drh0d316a42002-08-11 20:10:47 +0000593 int iNext;
drh2af926b2001-05-15 00:39:25 +0000594 if( idx>SQLITE_PAGE_SIZE-sizeof(FreeBlk) ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000595 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000596 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +0000597 pPage->nFree += SWAB16(pBt, pFBlk->iSize);
598 iNext = SWAB16(pBt, pFBlk->iNext);
599 if( iNext>0 && iNext <= idx ) goto page_format_error;
600 idx = iNext;
drh7e3b0a02001-04-28 16:52:40 +0000601 }
drh8b2f49b2001-06-08 00:21:52 +0000602 if( pPage->nCell==0 && pPage->nFree==0 ){
603 /* As a special case, an uninitialized root page appears to be
604 ** an empty database */
605 return SQLITE_OK;
606 }
drh5e2f8b92001-05-28 00:41:15 +0000607 if( pPage->nFree!=freeSpace ) goto page_format_error;
drh7e3b0a02001-04-28 16:52:40 +0000608 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +0000609
610page_format_error:
611 return SQLITE_CORRUPT;
drh7e3b0a02001-04-28 16:52:40 +0000612}
613
614/*
drh8b2f49b2001-06-08 00:21:52 +0000615** Set up a raw page so that it looks like a database page holding
616** no entries.
drhbd03cae2001-06-02 02:40:57 +0000617*/
drh0d316a42002-08-11 20:10:47 +0000618static void zeroPage(Btree *pBt, MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +0000619 PageHdr *pHdr;
620 FreeBlk *pFBlk;
drh6019e162001-07-02 17:51:45 +0000621 assert( sqlitepager_iswriteable(pPage) );
drhbd03cae2001-06-02 02:40:57 +0000622 memset(pPage, 0, SQLITE_PAGE_SIZE);
drh14acc042001-06-10 19:56:58 +0000623 pHdr = &pPage->u.hdr;
drhbd03cae2001-06-02 02:40:57 +0000624 pHdr->firstCell = 0;
drh0d316a42002-08-11 20:10:47 +0000625 pHdr->firstFree = SWAB16(pBt, sizeof(*pHdr));
drhbd03cae2001-06-02 02:40:57 +0000626 pFBlk = (FreeBlk*)&pHdr[1];
627 pFBlk->iNext = 0;
drh0d316a42002-08-11 20:10:47 +0000628 pPage->nFree = SQLITE_PAGE_SIZE - sizeof(*pHdr);
629 pFBlk->iSize = SWAB16(pBt, pPage->nFree);
drh8c42ca92001-06-22 19:15:00 +0000630 pPage->nCell = 0;
631 pPage->isOverfull = 0;
drhbd03cae2001-06-02 02:40:57 +0000632}
633
634/*
drh72f82862001-05-24 21:06:34 +0000635** This routine is called when the reference count for a page
636** reaches zero. We need to unref the pParent pointer when that
637** happens.
638*/
639static void pageDestructor(void *pData){
640 MemPage *pPage = (MemPage*)pData;
641 if( pPage->pParent ){
642 MemPage *pParent = pPage->pParent;
643 pPage->pParent = 0;
644 sqlitepager_unref(pParent);
645 }
646}
647
648/*
drh306dc212001-05-21 13:45:10 +0000649** Open a new database.
650**
651** Actually, this routine just sets up the internal data structures
drh72f82862001-05-24 21:06:34 +0000652** for accessing the database. We do not open the database file
653** until the first page is loaded.
drh382c0242001-10-06 16:33:02 +0000654**
655** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +0000656** a new database with a random name is created. This randomly named
657** database file will be deleted when sqliteBtreeClose() is called.
drha059ad02001-04-17 20:09:11 +0000658*/
drh6019e162001-07-02 17:51:45 +0000659int sqliteBtreeOpen(
660 const char *zFilename, /* Name of the file containing the BTree database */
661 int mode, /* Not currently used */
662 int nCache, /* How many pages in the page cache */
663 Btree **ppBtree /* Pointer to new Btree object written here */
664){
drha059ad02001-04-17 20:09:11 +0000665 Btree *pBt;
drh8c42ca92001-06-22 19:15:00 +0000666 int rc;
drha059ad02001-04-17 20:09:11 +0000667
668 pBt = sqliteMalloc( sizeof(*pBt) );
669 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +0000670 *ppBtree = 0;
drha059ad02001-04-17 20:09:11 +0000671 return SQLITE_NOMEM;
672 }
drh6019e162001-07-02 17:51:45 +0000673 if( nCache<10 ) nCache = 10;
674 rc = sqlitepager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE);
drha059ad02001-04-17 20:09:11 +0000675 if( rc!=SQLITE_OK ){
676 if( pBt->pPager ) sqlitepager_close(pBt->pPager);
677 sqliteFree(pBt);
678 *ppBtree = 0;
679 return rc;
680 }
drh72f82862001-05-24 21:06:34 +0000681 sqlitepager_set_destructor(pBt->pPager, pageDestructor);
drha059ad02001-04-17 20:09:11 +0000682 pBt->pCursor = 0;
683 pBt->page1 = 0;
drh5df72a52002-06-06 23:16:05 +0000684 pBt->readOnly = sqlitepager_isreadonly(pBt->pPager);
drha059ad02001-04-17 20:09:11 +0000685 *ppBtree = pBt;
686 return SQLITE_OK;
687}
688
689/*
690** Close an open database and invalidate all cursors.
691*/
692int sqliteBtreeClose(Btree *pBt){
693 while( pBt->pCursor ){
694 sqliteBtreeCloseCursor(pBt->pCursor);
695 }
696 sqlitepager_close(pBt->pPager);
697 sqliteFree(pBt);
698 return SQLITE_OK;
699}
700
701/*
drh6446c4d2001-12-15 14:22:18 +0000702** Change the limit on the number of pages allowed the cache.
drhcd61c282002-03-06 22:01:34 +0000703**
704** The maximum number of cache pages is set to the absolute
705** value of mxPage. If mxPage is negative, the pager will
706** operate asynchronously - it will not stop to do fsync()s
707** to insure data is written to the disk surface before
708** continuing. Transactions still work if synchronous is off,
709** and the database cannot be corrupted if this program
710** crashes. But if the operating system crashes or there is
711** an abrupt power failure when synchronous is off, the database
712** could be left in an inconsistent and unrecoverable state.
713** Synchronous is on by default so database corruption is not
714** normally a worry.
drhf57b14a2001-09-14 18:54:08 +0000715*/
716int sqliteBtreeSetCacheSize(Btree *pBt, int mxPage){
717 sqlitepager_set_cachesize(pBt->pPager, mxPage);
718 return SQLITE_OK;
719}
720
721/*
drh306dc212001-05-21 13:45:10 +0000722** Get a reference to page1 of the database file. This will
723** also acquire a readlock on that file.
724**
725** SQLITE_OK is returned on success. If the file is not a
726** well-formed database file, then SQLITE_CORRUPT is returned.
727** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
728** is returned if we run out of memory. SQLITE_PROTOCOL is returned
729** if there is a locking protocol violation.
730*/
731static int lockBtree(Btree *pBt){
732 int rc;
733 if( pBt->page1 ) return SQLITE_OK;
drh8c42ca92001-06-22 19:15:00 +0000734 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1);
drh306dc212001-05-21 13:45:10 +0000735 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +0000736
737 /* Do some checking to help insure the file we opened really is
738 ** a valid database file.
739 */
740 if( sqlitepager_pagecount(pBt->pPager)>0 ){
drhbd03cae2001-06-02 02:40:57 +0000741 PageOne *pP1 = pBt->page1;
drh0d316a42002-08-11 20:10:47 +0000742 if( strcmp(pP1->zMagic,zMagicHeader)!=0 ||
743 (pP1->iMagic!=MAGIC && swab32(pP1->iMagic)!=MAGIC) ){
drh306dc212001-05-21 13:45:10 +0000744 rc = SQLITE_CORRUPT;
drh72f82862001-05-24 21:06:34 +0000745 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +0000746 }
drh0d316a42002-08-11 20:10:47 +0000747 pBt->needSwab = pP1->iMagic!=MAGIC;
drh306dc212001-05-21 13:45:10 +0000748 }
749 return rc;
750
drh72f82862001-05-24 21:06:34 +0000751page1_init_failed:
drh306dc212001-05-21 13:45:10 +0000752 sqlitepager_unref(pBt->page1);
753 pBt->page1 = 0;
drh72f82862001-05-24 21:06:34 +0000754 return rc;
drh306dc212001-05-21 13:45:10 +0000755}
756
757/*
drhb8ca3072001-12-05 00:21:20 +0000758** If there are no outstanding cursors and we are not in the middle
759** of a transaction but there is a read lock on the database, then
760** this routine unrefs the first page of the database file which
761** has the effect of releasing the read lock.
762**
763** If there are any outstanding cursors, this routine is a no-op.
764**
765** If there is a transaction in progress, this routine is a no-op.
766*/
767static void unlockBtreeIfUnused(Btree *pBt){
768 if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){
769 sqlitepager_unref(pBt->page1);
770 pBt->page1 = 0;
771 pBt->inTrans = 0;
drh663fc632002-02-02 18:49:19 +0000772 pBt->inCkpt = 0;
drhb8ca3072001-12-05 00:21:20 +0000773 }
774}
775
776/*
drh8c42ca92001-06-22 19:15:00 +0000777** Create a new database by initializing the first two pages of the
778** file.
drh8b2f49b2001-06-08 00:21:52 +0000779*/
780static int newDatabase(Btree *pBt){
781 MemPage *pRoot;
782 PageOne *pP1;
drh8c42ca92001-06-22 19:15:00 +0000783 int rc;
drh7c717f72001-06-24 20:39:41 +0000784 if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK;
drh8b2f49b2001-06-08 00:21:52 +0000785 pP1 = pBt->page1;
786 rc = sqlitepager_write(pBt->page1);
787 if( rc ) return rc;
drh8c42ca92001-06-22 19:15:00 +0000788 rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot);
drh8b2f49b2001-06-08 00:21:52 +0000789 if( rc ) return rc;
790 rc = sqlitepager_write(pRoot);
791 if( rc ){
792 sqlitepager_unref(pRoot);
793 return rc;
794 }
795 strcpy(pP1->zMagic, zMagicHeader);
drh0d316a42002-08-11 20:10:47 +0000796 if( btree_native_byte_order ){
797 pP1->iMagic = MAGIC;
798 pBt->needSwab = 0;
799 }else{
800 pP1->iMagic = swab32(MAGIC);
801 pBt->needSwab = 1;
802 }
drh0d316a42002-08-11 20:10:47 +0000803 zeroPage(pBt, pRoot);
drh8b2f49b2001-06-08 00:21:52 +0000804 sqlitepager_unref(pRoot);
805 return SQLITE_OK;
806}
807
808/*
drh72f82862001-05-24 21:06:34 +0000809** Attempt to start a new transaction.
drh8b2f49b2001-06-08 00:21:52 +0000810**
811** A transaction must be started before attempting any changes
812** to the database. None of the following routines will work
813** unless a transaction is started first:
814**
815** sqliteBtreeCreateTable()
drhc6b52df2002-01-04 03:09:29 +0000816** sqliteBtreeCreateIndex()
drh8b2f49b2001-06-08 00:21:52 +0000817** sqliteBtreeClearTable()
818** sqliteBtreeDropTable()
819** sqliteBtreeInsert()
820** sqliteBtreeDelete()
821** sqliteBtreeUpdateMeta()
drha059ad02001-04-17 20:09:11 +0000822*/
823int sqliteBtreeBeginTrans(Btree *pBt){
824 int rc;
825 if( pBt->inTrans ) return SQLITE_ERROR;
drhf74b8d92002-09-01 23:20:45 +0000826 if( pBt->readOnly ) return SQLITE_READONLY;
drha059ad02001-04-17 20:09:11 +0000827 if( pBt->page1==0 ){
drh7e3b0a02001-04-28 16:52:40 +0000828 rc = lockBtree(pBt);
drh8c42ca92001-06-22 19:15:00 +0000829 if( rc!=SQLITE_OK ){
830 return rc;
831 }
drha059ad02001-04-17 20:09:11 +0000832 }
drhf74b8d92002-09-01 23:20:45 +0000833 rc = sqlitepager_begin(pBt->page1);
834 if( rc==SQLITE_OK ){
835 rc = newDatabase(pBt);
drha059ad02001-04-17 20:09:11 +0000836 }
drhb8ca3072001-12-05 00:21:20 +0000837 if( rc==SQLITE_OK ){
838 pBt->inTrans = 1;
drh663fc632002-02-02 18:49:19 +0000839 pBt->inCkpt = 0;
drhb8ca3072001-12-05 00:21:20 +0000840 }else{
841 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000842 }
drhb8ca3072001-12-05 00:21:20 +0000843 return rc;
drha059ad02001-04-17 20:09:11 +0000844}
845
846/*
drh2aa679f2001-06-25 02:11:07 +0000847** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +0000848**
849** This will release the write lock on the database file. If there
850** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +0000851*/
852int sqliteBtreeCommit(Btree *pBt){
853 int rc;
drh5df72a52002-06-06 23:16:05 +0000854 rc = pBt->readOnly ? SQLITE_OK : sqlitepager_commit(pBt->pPager);
drh7c717f72001-06-24 20:39:41 +0000855 pBt->inTrans = 0;
drh663fc632002-02-02 18:49:19 +0000856 pBt->inCkpt = 0;
drh5e00f6c2001-09-13 13:46:56 +0000857 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000858 return rc;
859}
860
861/*
drhecdc7532001-09-23 02:35:53 +0000862** Rollback the transaction in progress. All cursors will be
863** invalided by this operation. Any attempt to use a cursor
864** that was open at the beginning of this operation will result
865** in an error.
drh5e00f6c2001-09-13 13:46:56 +0000866**
867** This will release the write lock on the database file. If there
868** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +0000869*/
870int sqliteBtreeRollback(Btree *pBt){
871 int rc;
drhecdc7532001-09-23 02:35:53 +0000872 BtCursor *pCur;
drh7c717f72001-06-24 20:39:41 +0000873 if( pBt->inTrans==0 ) return SQLITE_OK;
874 pBt->inTrans = 0;
drh663fc632002-02-02 18:49:19 +0000875 pBt->inCkpt = 0;
drhecdc7532001-09-23 02:35:53 +0000876 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
877 if( pCur->pPage ){
878 sqlitepager_unref(pCur->pPage);
879 pCur->pPage = 0;
880 }
881 }
drh5df72a52002-06-06 23:16:05 +0000882 rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager);
drh5e00f6c2001-09-13 13:46:56 +0000883 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000884 return rc;
885}
886
887/*
drh663fc632002-02-02 18:49:19 +0000888** Set the checkpoint for the current transaction. The checkpoint serves
889** as a sub-transaction that can be rolled back independently of the
890** main transaction. You must start a transaction before starting a
891** checkpoint. The checkpoint is ended automatically if the transaction
892** commits or rolls back.
893**
894** Only one checkpoint may be active at a time. It is an error to try
895** to start a new checkpoint if another checkpoint is already active.
896*/
897int sqliteBtreeBeginCkpt(Btree *pBt){
898 int rc;
drh0d65dc02002-02-03 00:56:09 +0000899 if( !pBt->inTrans || pBt->inCkpt ){
drhf74b8d92002-09-01 23:20:45 +0000900 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh0d65dc02002-02-03 00:56:09 +0000901 }
drh5df72a52002-06-06 23:16:05 +0000902 rc = pBt->readOnly ? SQLITE_OK : sqlitepager_ckpt_begin(pBt->pPager);
drh663fc632002-02-02 18:49:19 +0000903 pBt->inCkpt = 1;
904 return rc;
905}
906
907
908/*
909** Commit a checkpoint to transaction currently in progress. If no
910** checkpoint is active, this is a no-op.
911*/
912int sqliteBtreeCommitCkpt(Btree *pBt){
913 int rc;
drh5df72a52002-06-06 23:16:05 +0000914 if( pBt->inCkpt && !pBt->readOnly ){
drh663fc632002-02-02 18:49:19 +0000915 rc = sqlitepager_ckpt_commit(pBt->pPager);
916 }else{
917 rc = SQLITE_OK;
918 }
drh0d65dc02002-02-03 00:56:09 +0000919 pBt->inCkpt = 0;
drh663fc632002-02-02 18:49:19 +0000920 return rc;
921}
922
923/*
924** Rollback the checkpoint to the current transaction. If there
925** is no active checkpoint or transaction, this routine is a no-op.
926**
927** All cursors will be invalided by this operation. Any attempt
928** to use a cursor that was open at the beginning of this operation
929** will result in an error.
930*/
931int sqliteBtreeRollbackCkpt(Btree *pBt){
932 int rc;
933 BtCursor *pCur;
drh5df72a52002-06-06 23:16:05 +0000934 if( pBt->inCkpt==0 || pBt->readOnly ) return SQLITE_OK;
drh663fc632002-02-02 18:49:19 +0000935 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
936 if( pCur->pPage ){
937 sqlitepager_unref(pCur->pPage);
938 pCur->pPage = 0;
939 }
940 }
941 rc = sqlitepager_ckpt_rollback(pBt->pPager);
drh0d65dc02002-02-03 00:56:09 +0000942 pBt->inCkpt = 0;
drh663fc632002-02-02 18:49:19 +0000943 return rc;
944}
945
946/*
drh8b2f49b2001-06-08 00:21:52 +0000947** Create a new cursor for the BTree whose root is on the page
948** iTable. The act of acquiring a cursor gets a read lock on
949** the database file.
drh1bee3d72001-10-15 00:44:35 +0000950**
951** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +0000952** If wrFlag==1, then the cursor can be used for reading or for
953** writing if other conditions for writing are also met. These
954** are the conditions that must be met in order for writing to
955** be allowed:
drh6446c4d2001-12-15 14:22:18 +0000956**
drhf74b8d92002-09-01 23:20:45 +0000957** 1: The cursor must have been opened with wrFlag==1
958**
959** 2: No other cursors may be open with wrFlag==0 on the same table
960**
961** 3: The database must be writable (not on read-only media)
962**
963** 4: There must be an active transaction.
964**
965** Condition 2 warrants further discussion. If any cursor is opened
966** on a table with wrFlag==0, that prevents all other cursors from
967** writing to that table. This is a kind of "read-lock". When a cursor
968** is opened with wrFlag==0 it is guaranteed that the table will not
969** change as long as the cursor is open. This allows the cursor to
970** do a sequential scan of the table without having to worry about
971** entries being inserted or deleted during the scan. Cursors should
972** be opened with wrFlag==0 only if this read-lock property is needed.
973** That is to say, cursors should be opened with wrFlag==0 only if they
974** intend to use the sqliteBtreeNext() system call. All other cursors
975** should be opened with wrFlag==1 even if they never really intend
976** to write.
977**
drh6446c4d2001-12-15 14:22:18 +0000978** No checking is done to make sure that page iTable really is the
979** root page of a b-tree. If it is not, then the cursor acquired
980** will not work correctly.
drha059ad02001-04-17 20:09:11 +0000981*/
drhecdc7532001-09-23 02:35:53 +0000982int sqliteBtreeCursor(Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur){
drha059ad02001-04-17 20:09:11 +0000983 int rc;
drhf74b8d92002-09-01 23:20:45 +0000984 BtCursor *pCur, *pRing;
drhecdc7532001-09-23 02:35:53 +0000985
drha059ad02001-04-17 20:09:11 +0000986 if( pBt->page1==0 ){
987 rc = lockBtree(pBt);
988 if( rc!=SQLITE_OK ){
989 *ppCur = 0;
990 return rc;
991 }
992 }
993 pCur = sqliteMalloc( sizeof(*pCur) );
994 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +0000995 rc = SQLITE_NOMEM;
996 goto create_cursor_exception;
997 }
drh8b2f49b2001-06-08 00:21:52 +0000998 pCur->pgnoRoot = (Pgno)iTable;
drh8c42ca92001-06-22 19:15:00 +0000999 rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00001000 if( rc!=SQLITE_OK ){
1001 goto create_cursor_exception;
1002 }
drh0d316a42002-08-11 20:10:47 +00001003 rc = initPage(pBt, pCur->pPage, pCur->pgnoRoot, 0);
drhbd03cae2001-06-02 02:40:57 +00001004 if( rc!=SQLITE_OK ){
1005 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00001006 }
drh14acc042001-06-10 19:56:58 +00001007 pCur->pBt = pBt;
drhecdc7532001-09-23 02:35:53 +00001008 pCur->wrFlag = wrFlag;
drh14acc042001-06-10 19:56:58 +00001009 pCur->idx = 0;
drha059ad02001-04-17 20:09:11 +00001010 pCur->pNext = pBt->pCursor;
1011 if( pCur->pNext ){
1012 pCur->pNext->pPrev = pCur;
1013 }
drh14acc042001-06-10 19:56:58 +00001014 pCur->pPrev = 0;
drhf74b8d92002-09-01 23:20:45 +00001015 pRing = pBt->pCursor;
1016 while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; }
1017 if( pRing ){
1018 pCur->pShared = pRing->pShared;
1019 pRing->pShared = pCur;
1020 }else{
1021 pCur->pShared = pCur;
1022 }
drha059ad02001-04-17 20:09:11 +00001023 pBt->pCursor = pCur;
drh2af926b2001-05-15 00:39:25 +00001024 *ppCur = pCur;
1025 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00001026
1027create_cursor_exception:
1028 *ppCur = 0;
1029 if( pCur ){
1030 if( pCur->pPage ) sqlitepager_unref(pCur->pPage);
1031 sqliteFree(pCur);
1032 }
drh5e00f6c2001-09-13 13:46:56 +00001033 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00001034 return rc;
drha059ad02001-04-17 20:09:11 +00001035}
1036
1037/*
drh5e00f6c2001-09-13 13:46:56 +00001038** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00001039** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00001040*/
1041int sqliteBtreeCloseCursor(BtCursor *pCur){
1042 Btree *pBt = pCur->pBt;
drha059ad02001-04-17 20:09:11 +00001043 if( pCur->pPrev ){
1044 pCur->pPrev->pNext = pCur->pNext;
1045 }else{
1046 pBt->pCursor = pCur->pNext;
1047 }
1048 if( pCur->pNext ){
1049 pCur->pNext->pPrev = pCur->pPrev;
1050 }
drhecdc7532001-09-23 02:35:53 +00001051 if( pCur->pPage ){
1052 sqlitepager_unref(pCur->pPage);
1053 }
drhf74b8d92002-09-01 23:20:45 +00001054 if( pCur->pShared!=pCur ){
1055 BtCursor *pRing = pCur->pShared;
1056 while( pRing->pShared!=pCur ){ pRing = pRing->pShared; }
1057 pRing->pShared = pCur->pShared;
1058 }
drh5e00f6c2001-09-13 13:46:56 +00001059 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +00001060 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +00001061 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00001062}
1063
drh7e3b0a02001-04-28 16:52:40 +00001064/*
drh5e2f8b92001-05-28 00:41:15 +00001065** Make a temporary cursor by filling in the fields of pTempCur.
1066** The temporary cursor is not on the cursor list for the Btree.
1067*/
drh14acc042001-06-10 19:56:58 +00001068static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +00001069 memcpy(pTempCur, pCur, sizeof(*pCur));
1070 pTempCur->pNext = 0;
1071 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00001072 if( pTempCur->pPage ){
1073 sqlitepager_ref(pTempCur->pPage);
1074 }
drh5e2f8b92001-05-28 00:41:15 +00001075}
1076
1077/*
drhbd03cae2001-06-02 02:40:57 +00001078** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00001079** function above.
1080*/
drh14acc042001-06-10 19:56:58 +00001081static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +00001082 if( pCur->pPage ){
1083 sqlitepager_unref(pCur->pPage);
1084 }
drh5e2f8b92001-05-28 00:41:15 +00001085}
1086
1087/*
drhbd03cae2001-06-02 02:40:57 +00001088** Set *pSize to the number of bytes of key in the entry the
1089** cursor currently points to. Always return SQLITE_OK.
1090** Failure is not possible. If the cursor is not currently
1091** pointing to an entry (which can happen, for example, if
1092** the database is empty) then *pSize is set to 0.
drh7e3b0a02001-04-28 16:52:40 +00001093*/
drh72f82862001-05-24 21:06:34 +00001094int sqliteBtreeKeySize(BtCursor *pCur, int *pSize){
drh2af926b2001-05-15 00:39:25 +00001095 Cell *pCell;
1096 MemPage *pPage;
1097
1098 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001099 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh72f82862001-05-24 21:06:34 +00001100 *pSize = 0;
1101 }else{
drh5e2f8b92001-05-28 00:41:15 +00001102 pCell = pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00001103 *pSize = NKEY(pCur->pBt, pCell->h);
drh72f82862001-05-24 21:06:34 +00001104 }
1105 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00001106}
drh2af926b2001-05-15 00:39:25 +00001107
drh72f82862001-05-24 21:06:34 +00001108/*
1109** Read payload information from the entry that the pCur cursor is
1110** pointing to. Begin reading the payload at "offset" and read
1111** a total of "amt" bytes. Put the result in zBuf.
1112**
1113** This routine does not make a distinction between key and data.
1114** It just reads bytes from the payload area.
1115*/
drh2af926b2001-05-15 00:39:25 +00001116static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){
drh5e2f8b92001-05-28 00:41:15 +00001117 char *aPayload;
drh2af926b2001-05-15 00:39:25 +00001118 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +00001119 int rc;
drh0d316a42002-08-11 20:10:47 +00001120 Btree *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00001121 assert( pCur!=0 && pCur->pPage!=0 );
drh8c42ca92001-06-22 19:15:00 +00001122 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
1123 aPayload = pCur->pPage->apCell[pCur->idx]->aPayload;
drh2af926b2001-05-15 00:39:25 +00001124 if( offset<MX_LOCAL_PAYLOAD ){
1125 int a = amt;
1126 if( a+offset>MX_LOCAL_PAYLOAD ){
1127 a = MX_LOCAL_PAYLOAD - offset;
1128 }
drh5e2f8b92001-05-28 00:41:15 +00001129 memcpy(zBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +00001130 if( a==amt ){
1131 return SQLITE_OK;
1132 }
drh2aa679f2001-06-25 02:11:07 +00001133 offset = 0;
drh2af926b2001-05-15 00:39:25 +00001134 zBuf += a;
1135 amt -= a;
drhdd793422001-06-28 01:54:48 +00001136 }else{
1137 offset -= MX_LOCAL_PAYLOAD;
drhbd03cae2001-06-02 02:40:57 +00001138 }
1139 if( amt>0 ){
drh0d316a42002-08-11 20:10:47 +00001140 nextPage = SWAB32(pBt, pCur->pPage->apCell[pCur->idx]->ovfl);
drh2af926b2001-05-15 00:39:25 +00001141 }
1142 while( amt>0 && nextPage ){
1143 OverflowPage *pOvfl;
drh0d316a42002-08-11 20:10:47 +00001144 rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
drh2af926b2001-05-15 00:39:25 +00001145 if( rc!=0 ){
1146 return rc;
1147 }
drh0d316a42002-08-11 20:10:47 +00001148 nextPage = SWAB32(pBt, pOvfl->iNext);
drh2af926b2001-05-15 00:39:25 +00001149 if( offset<OVERFLOW_SIZE ){
1150 int a = amt;
1151 if( a + offset > OVERFLOW_SIZE ){
1152 a = OVERFLOW_SIZE - offset;
1153 }
drh5e2f8b92001-05-28 00:41:15 +00001154 memcpy(zBuf, &pOvfl->aPayload[offset], a);
drh2aa679f2001-06-25 02:11:07 +00001155 offset = 0;
drh2af926b2001-05-15 00:39:25 +00001156 amt -= a;
1157 zBuf += a;
drh2aa679f2001-06-25 02:11:07 +00001158 }else{
1159 offset -= OVERFLOW_SIZE;
drh2af926b2001-05-15 00:39:25 +00001160 }
1161 sqlitepager_unref(pOvfl);
1162 }
drha7fcb052001-12-14 15:09:55 +00001163 if( amt>0 ){
1164 return SQLITE_CORRUPT;
1165 }
1166 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +00001167}
1168
drh72f82862001-05-24 21:06:34 +00001169/*
drh5e00f6c2001-09-13 13:46:56 +00001170** Read part of the key associated with cursor pCur. A maximum
drh72f82862001-05-24 21:06:34 +00001171** of "amt" bytes will be transfered into zBuf[]. The transfer
drh5e00f6c2001-09-13 13:46:56 +00001172** begins at "offset". The number of bytes actually read is
1173** returned. The amount returned will be smaller than the
1174** amount requested if there are not enough bytes in the key
1175** to satisfy the request.
drh72f82862001-05-24 21:06:34 +00001176*/
1177int sqliteBtreeKey(BtCursor *pCur, int offset, int amt, char *zBuf){
1178 Cell *pCell;
1179 MemPage *pPage;
drha059ad02001-04-17 20:09:11 +00001180
drh5e00f6c2001-09-13 13:46:56 +00001181 if( amt<0 ) return 0;
1182 if( offset<0 ) return 0;
1183 if( amt==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001184 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001185 if( pPage==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001186 if( pCur->idx >= pPage->nCell ){
drh5e00f6c2001-09-13 13:46:56 +00001187 return 0;
drh72f82862001-05-24 21:06:34 +00001188 }
drh5e2f8b92001-05-28 00:41:15 +00001189 pCell = pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00001190 if( amt+offset > NKEY(pCur->pBt, pCell->h) ){
1191 amt = NKEY(pCur->pBt, pCell->h) - offset;
drh5e00f6c2001-09-13 13:46:56 +00001192 if( amt<=0 ){
1193 return 0;
1194 }
drhbd03cae2001-06-02 02:40:57 +00001195 }
drh5e00f6c2001-09-13 13:46:56 +00001196 getPayload(pCur, offset, amt, zBuf);
1197 return amt;
drh72f82862001-05-24 21:06:34 +00001198}
1199
1200/*
drhbd03cae2001-06-02 02:40:57 +00001201** Set *pSize to the number of bytes of data in the entry the
1202** cursor currently points to. Always return SQLITE_OK.
1203** Failure is not possible. If the cursor is not currently
1204** pointing to an entry (which can happen, for example, if
1205** the database is empty) then *pSize is set to 0.
drh72f82862001-05-24 21:06:34 +00001206*/
1207int sqliteBtreeDataSize(BtCursor *pCur, int *pSize){
1208 Cell *pCell;
1209 MemPage *pPage;
1210
1211 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001212 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh72f82862001-05-24 21:06:34 +00001213 *pSize = 0;
1214 }else{
drh5e2f8b92001-05-28 00:41:15 +00001215 pCell = pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00001216 *pSize = NDATA(pCur->pBt, pCell->h);
drh72f82862001-05-24 21:06:34 +00001217 }
1218 return SQLITE_OK;
1219}
1220
1221/*
drh5e00f6c2001-09-13 13:46:56 +00001222** Read part of the data associated with cursor pCur. A maximum
drh72f82862001-05-24 21:06:34 +00001223** of "amt" bytes will be transfered into zBuf[]. The transfer
drh5e00f6c2001-09-13 13:46:56 +00001224** begins at "offset". The number of bytes actually read is
1225** returned. The amount returned will be smaller than the
1226** amount requested if there are not enough bytes in the data
1227** to satisfy the request.
drh72f82862001-05-24 21:06:34 +00001228*/
1229int sqliteBtreeData(BtCursor *pCur, int offset, int amt, char *zBuf){
1230 Cell *pCell;
1231 MemPage *pPage;
drh0d316a42002-08-11 20:10:47 +00001232 int nData;
drh72f82862001-05-24 21:06:34 +00001233
drh5e00f6c2001-09-13 13:46:56 +00001234 if( amt<0 ) return 0;
1235 if( offset<0 ) return 0;
1236 if( amt==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001237 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001238 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh5e00f6c2001-09-13 13:46:56 +00001239 return 0;
drh72f82862001-05-24 21:06:34 +00001240 }
drh5e2f8b92001-05-28 00:41:15 +00001241 pCell = pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00001242 nData = NDATA(pCur->pBt, pCell->h);
1243 if( amt+offset > nData ){
1244 amt = nData - offset;
drh5e00f6c2001-09-13 13:46:56 +00001245 if( amt<=0 ){
1246 return 0;
1247 }
drhbd03cae2001-06-02 02:40:57 +00001248 }
drh0d316a42002-08-11 20:10:47 +00001249 getPayload(pCur, offset + NKEY(pCur->pBt, pCell->h), amt, zBuf);
drh5e00f6c2001-09-13 13:46:56 +00001250 return amt;
drh72f82862001-05-24 21:06:34 +00001251}
drha059ad02001-04-17 20:09:11 +00001252
drh2af926b2001-05-15 00:39:25 +00001253/*
drh8721ce42001-11-07 14:22:00 +00001254** Compare an external key against the key on the entry that pCur points to.
1255**
1256** The external key is pKey and is nKey bytes long. The last nIgnore bytes
1257** of the key associated with pCur are ignored, as if they do not exist.
1258** (The normal case is for nIgnore to be zero in which case the entire
1259** internal key is used in the comparison.)
1260**
1261** The comparison result is written to *pRes as follows:
drh2af926b2001-05-15 00:39:25 +00001262**
drh717e6402001-09-27 03:22:32 +00001263** *pRes<0 This means pCur<pKey
1264**
1265** *pRes==0 This means pCur==pKey for all nKey bytes
1266**
1267** *pRes>0 This means pCur>pKey
1268**
drh8721ce42001-11-07 14:22:00 +00001269** When one key is an exact prefix of the other, the shorter key is
1270** considered less than the longer one. In order to be equal the
1271** keys must be exactly the same length. (The length of the pCur key
1272** is the actual key length minus nIgnore bytes.)
drh2af926b2001-05-15 00:39:25 +00001273*/
drh717e6402001-09-27 03:22:32 +00001274int sqliteBtreeKeyCompare(
drh8721ce42001-11-07 14:22:00 +00001275 BtCursor *pCur, /* Pointer to entry to compare against */
1276 const void *pKey, /* Key to compare against entry that pCur points to */
1277 int nKey, /* Number of bytes in pKey */
1278 int nIgnore, /* Ignore this many bytes at the end of pCur */
1279 int *pResult /* Write the result here */
drh5c4d9702001-08-20 00:33:58 +00001280){
drh2af926b2001-05-15 00:39:25 +00001281 Pgno nextPage;
drh8721ce42001-11-07 14:22:00 +00001282 int n, c, rc, nLocal;
drh2af926b2001-05-15 00:39:25 +00001283 Cell *pCell;
drh0d316a42002-08-11 20:10:47 +00001284 Btree *pBt = pCur->pBt;
drh717e6402001-09-27 03:22:32 +00001285 const char *zKey = (const char*)pKey;
drh2af926b2001-05-15 00:39:25 +00001286
1287 assert( pCur->pPage );
1288 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drhbd03cae2001-06-02 02:40:57 +00001289 pCell = pCur->pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00001290 nLocal = NKEY(pBt, pCell->h) - nIgnore;
drh8721ce42001-11-07 14:22:00 +00001291 if( nLocal<0 ) nLocal = 0;
1292 n = nKey<nLocal ? nKey : nLocal;
drh2af926b2001-05-15 00:39:25 +00001293 if( n>MX_LOCAL_PAYLOAD ){
1294 n = MX_LOCAL_PAYLOAD;
1295 }
drh717e6402001-09-27 03:22:32 +00001296 c = memcmp(pCell->aPayload, zKey, n);
drh2af926b2001-05-15 00:39:25 +00001297 if( c!=0 ){
1298 *pResult = c;
1299 return SQLITE_OK;
1300 }
drh717e6402001-09-27 03:22:32 +00001301 zKey += n;
drh2af926b2001-05-15 00:39:25 +00001302 nKey -= n;
drh8721ce42001-11-07 14:22:00 +00001303 nLocal -= n;
drh0d316a42002-08-11 20:10:47 +00001304 nextPage = SWAB32(pBt, pCell->ovfl);
drh8721ce42001-11-07 14:22:00 +00001305 while( nKey>0 && nLocal>0 ){
drh2af926b2001-05-15 00:39:25 +00001306 OverflowPage *pOvfl;
1307 if( nextPage==0 ){
1308 return SQLITE_CORRUPT;
1309 }
drh0d316a42002-08-11 20:10:47 +00001310 rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
drh72f82862001-05-24 21:06:34 +00001311 if( rc ){
drh2af926b2001-05-15 00:39:25 +00001312 return rc;
1313 }
drh0d316a42002-08-11 20:10:47 +00001314 nextPage = SWAB32(pBt, pOvfl->iNext);
drh8721ce42001-11-07 14:22:00 +00001315 n = nKey<nLocal ? nKey : nLocal;
drh2af926b2001-05-15 00:39:25 +00001316 if( n>OVERFLOW_SIZE ){
1317 n = OVERFLOW_SIZE;
1318 }
drh717e6402001-09-27 03:22:32 +00001319 c = memcmp(pOvfl->aPayload, zKey, n);
drh2af926b2001-05-15 00:39:25 +00001320 sqlitepager_unref(pOvfl);
1321 if( c!=0 ){
1322 *pResult = c;
1323 return SQLITE_OK;
1324 }
1325 nKey -= n;
drh8721ce42001-11-07 14:22:00 +00001326 nLocal -= n;
drh717e6402001-09-27 03:22:32 +00001327 zKey += n;
drh2af926b2001-05-15 00:39:25 +00001328 }
drh717e6402001-09-27 03:22:32 +00001329 if( c==0 ){
drh8721ce42001-11-07 14:22:00 +00001330 c = nLocal - nKey;
drh717e6402001-09-27 03:22:32 +00001331 }
drh2af926b2001-05-15 00:39:25 +00001332 *pResult = c;
1333 return SQLITE_OK;
1334}
1335
drh72f82862001-05-24 21:06:34 +00001336/*
1337** Move the cursor down to a new child page.
1338*/
drh5e2f8b92001-05-28 00:41:15 +00001339static int moveToChild(BtCursor *pCur, int newPgno){
drh72f82862001-05-24 21:06:34 +00001340 int rc;
1341 MemPage *pNewPage;
drh0d316a42002-08-11 20:10:47 +00001342 Btree *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00001343
drh0d316a42002-08-11 20:10:47 +00001344 rc = sqlitepager_get(pBt->pPager, newPgno, (void**)&pNewPage);
drh6019e162001-07-02 17:51:45 +00001345 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00001346 rc = initPage(pBt, pNewPage, newPgno, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00001347 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001348 sqlitepager_unref(pCur->pPage);
1349 pCur->pPage = pNewPage;
1350 pCur->idx = 0;
1351 return SQLITE_OK;
1352}
1353
1354/*
drh5e2f8b92001-05-28 00:41:15 +00001355** Move the cursor up to the parent page.
1356**
1357** pCur->idx is set to the cell index that contains the pointer
1358** to the page we are coming from. If we are coming from the
1359** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00001360** the largest cell index.
drh72f82862001-05-24 21:06:34 +00001361*/
drh5e2f8b92001-05-28 00:41:15 +00001362static int moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001363 Pgno oldPgno;
1364 MemPage *pParent;
drh8c42ca92001-06-22 19:15:00 +00001365 int i;
drh72f82862001-05-24 21:06:34 +00001366 pParent = pCur->pPage->pParent;
drhbd03cae2001-06-02 02:40:57 +00001367 if( pParent==0 ) return SQLITE_INTERNAL;
drh72f82862001-05-24 21:06:34 +00001368 oldPgno = sqlitepager_pagenumber(pCur->pPage);
drh72f82862001-05-24 21:06:34 +00001369 sqlitepager_ref(pParent);
1370 sqlitepager_unref(pCur->pPage);
1371 pCur->pPage = pParent;
drh8c42ca92001-06-22 19:15:00 +00001372 pCur->idx = pParent->nCell;
drh0d316a42002-08-11 20:10:47 +00001373 oldPgno = SWAB32(pCur->pBt, oldPgno);
drh8c42ca92001-06-22 19:15:00 +00001374 for(i=0; i<pParent->nCell; i++){
1375 if( pParent->apCell[i]->h.leftChild==oldPgno ){
drh72f82862001-05-24 21:06:34 +00001376 pCur->idx = i;
1377 break;
1378 }
1379 }
drh5e2f8b92001-05-28 00:41:15 +00001380 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00001381}
1382
1383/*
1384** Move the cursor to the root page
1385*/
drh5e2f8b92001-05-28 00:41:15 +00001386static int moveToRoot(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001387 MemPage *pNew;
drhbd03cae2001-06-02 02:40:57 +00001388 int rc;
drh0d316a42002-08-11 20:10:47 +00001389 Btree *pBt = pCur->pBt;
drhbd03cae2001-06-02 02:40:57 +00001390
drh0d316a42002-08-11 20:10:47 +00001391 rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pNew);
drhbd03cae2001-06-02 02:40:57 +00001392 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00001393 rc = initPage(pBt, pNew, pCur->pgnoRoot, 0);
drh6019e162001-07-02 17:51:45 +00001394 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001395 sqlitepager_unref(pCur->pPage);
1396 pCur->pPage = pNew;
1397 pCur->idx = 0;
1398 return SQLITE_OK;
1399}
drh2af926b2001-05-15 00:39:25 +00001400
drh5e2f8b92001-05-28 00:41:15 +00001401/*
1402** Move the cursor down to the left-most leaf entry beneath the
1403** entry to which it is currently pointing.
1404*/
1405static int moveToLeftmost(BtCursor *pCur){
1406 Pgno pgno;
1407 int rc;
1408
1409 while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
drh0d316a42002-08-11 20:10:47 +00001410 rc = moveToChild(pCur, SWAB32(pCur->pBt, pgno));
drh5e2f8b92001-05-28 00:41:15 +00001411 if( rc ) return rc;
1412 }
1413 return SQLITE_OK;
1414}
1415
drh5e00f6c2001-09-13 13:46:56 +00001416/* Move the cursor to the first entry in the table. Return SQLITE_OK
1417** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00001418** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00001419*/
1420int sqliteBtreeFirst(BtCursor *pCur, int *pRes){
1421 int rc;
drhecdc7532001-09-23 02:35:53 +00001422 if( pCur->pPage==0 ) return SQLITE_ABORT;
drh5e00f6c2001-09-13 13:46:56 +00001423 rc = moveToRoot(pCur);
1424 if( rc ) return rc;
1425 if( pCur->pPage->nCell==0 ){
1426 *pRes = 1;
1427 return SQLITE_OK;
1428 }
1429 *pRes = 0;
1430 rc = moveToLeftmost(pCur);
drh0ce92ed2001-12-15 02:47:28 +00001431 pCur->bSkipNext = 0;
drh5e00f6c2001-09-13 13:46:56 +00001432 return rc;
1433}
drh5e2f8b92001-05-28 00:41:15 +00001434
drh9562b552002-02-19 15:00:07 +00001435/* Move the cursor to the last entry in the table. Return SQLITE_OK
1436** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00001437** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00001438*/
1439int sqliteBtreeLast(BtCursor *pCur, int *pRes){
1440 int rc;
1441 Pgno pgno;
1442 if( pCur->pPage==0 ) return SQLITE_ABORT;
1443 rc = moveToRoot(pCur);
1444 if( rc ) return rc;
drh7aa128d2002-06-21 13:09:16 +00001445 assert( pCur->pPage->isInit );
drh9562b552002-02-19 15:00:07 +00001446 if( pCur->pPage->nCell==0 ){
1447 *pRes = 1;
1448 return SQLITE_OK;
1449 }
1450 *pRes = 0;
1451 while( (pgno = pCur->pPage->u.hdr.rightChild)!=0 ){
drh0d316a42002-08-11 20:10:47 +00001452 rc = moveToChild(pCur, SWAB32(pCur->pBt, pgno));
drh9562b552002-02-19 15:00:07 +00001453 if( rc ) return rc;
1454 }
1455 pCur->idx = pCur->pPage->nCell-1;
1456 pCur->bSkipNext = 0;
1457 return rc;
1458}
1459
drha059ad02001-04-17 20:09:11 +00001460/* Move the cursor so that it points to an entry near pKey.
drh72f82862001-05-24 21:06:34 +00001461** Return a success code.
1462**
drh5e2f8b92001-05-28 00:41:15 +00001463** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00001464** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00001465** were present. The cursor might point to an entry that comes
1466** before or after the key.
1467**
drhbd03cae2001-06-02 02:40:57 +00001468** The result of comparing the key with the entry to which the
1469** cursor is left pointing is stored in pCur->iMatch. The same
1470** value is also written to *pRes if pRes!=NULL. The meaning of
1471** this value is as follows:
1472**
1473** *pRes<0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001474** is smaller than pKey.
drhbd03cae2001-06-02 02:40:57 +00001475**
1476** *pRes==0 The cursor is left pointing at an entry that
1477** exactly matches pKey.
1478**
1479** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001480** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00001481*/
drh5c4d9702001-08-20 00:33:58 +00001482int sqliteBtreeMoveto(BtCursor *pCur, const void *pKey, int nKey, int *pRes){
drh72f82862001-05-24 21:06:34 +00001483 int rc;
drhecdc7532001-09-23 02:35:53 +00001484 if( pCur->pPage==0 ) return SQLITE_ABORT;
drh7c717f72001-06-24 20:39:41 +00001485 pCur->bSkipNext = 0;
drh5e2f8b92001-05-28 00:41:15 +00001486 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00001487 if( rc ) return rc;
1488 for(;;){
1489 int lwr, upr;
1490 Pgno chldPg;
1491 MemPage *pPage = pCur->pPage;
drh8b2f49b2001-06-08 00:21:52 +00001492 int c = -1;
drh72f82862001-05-24 21:06:34 +00001493 lwr = 0;
1494 upr = pPage->nCell-1;
1495 while( lwr<=upr ){
drh72f82862001-05-24 21:06:34 +00001496 pCur->idx = (lwr+upr)/2;
drh8721ce42001-11-07 14:22:00 +00001497 rc = sqliteBtreeKeyCompare(pCur, pKey, nKey, 0, &c);
drh72f82862001-05-24 21:06:34 +00001498 if( rc ) return rc;
1499 if( c==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001500 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001501 if( pRes ) *pRes = 0;
1502 return SQLITE_OK;
1503 }
1504 if( c<0 ){
1505 lwr = pCur->idx+1;
1506 }else{
1507 upr = pCur->idx-1;
1508 }
1509 }
1510 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00001511 assert( pPage->isInit );
drh72f82862001-05-24 21:06:34 +00001512 if( lwr>=pPage->nCell ){
drh14acc042001-06-10 19:56:58 +00001513 chldPg = pPage->u.hdr.rightChild;
drh72f82862001-05-24 21:06:34 +00001514 }else{
drh5e2f8b92001-05-28 00:41:15 +00001515 chldPg = pPage->apCell[lwr]->h.leftChild;
drh72f82862001-05-24 21:06:34 +00001516 }
1517 if( chldPg==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001518 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001519 if( pRes ) *pRes = c;
1520 return SQLITE_OK;
1521 }
drh0d316a42002-08-11 20:10:47 +00001522 rc = moveToChild(pCur, SWAB32(pCur->pBt, chldPg));
drh72f82862001-05-24 21:06:34 +00001523 if( rc ) return rc;
1524 }
drhbd03cae2001-06-02 02:40:57 +00001525 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00001526}
1527
1528/*
drhbd03cae2001-06-02 02:40:57 +00001529** Advance the cursor to the next entry in the database. If
1530** successful and pRes!=NULL then set *pRes=0. If the cursor
1531** was already pointing to the last entry in the database before
1532** this routine was called, then set *pRes=1 if pRes!=NULL.
drh72f82862001-05-24 21:06:34 +00001533*/
1534int sqliteBtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00001535 int rc;
drhecdc7532001-09-23 02:35:53 +00001536 if( pCur->pPage==0 ){
drh1bee3d72001-10-15 00:44:35 +00001537 if( pRes ) *pRes = 1;
drhecdc7532001-09-23 02:35:53 +00001538 return SQLITE_ABORT;
1539 }
drh7aa128d2002-06-21 13:09:16 +00001540 assert( pCur->pPage->isInit );
drhf5bf0a72001-11-23 00:24:12 +00001541 if( pCur->bSkipNext && pCur->idx<pCur->pPage->nCell ){
drh5e2f8b92001-05-28 00:41:15 +00001542 pCur->bSkipNext = 0;
drh72f82862001-05-24 21:06:34 +00001543 if( pRes ) *pRes = 0;
1544 return SQLITE_OK;
1545 }
drh72f82862001-05-24 21:06:34 +00001546 pCur->idx++;
drh5e2f8b92001-05-28 00:41:15 +00001547 if( pCur->idx>=pCur->pPage->nCell ){
drh8c42ca92001-06-22 19:15:00 +00001548 if( pCur->pPage->u.hdr.rightChild ){
drh0d316a42002-08-11 20:10:47 +00001549 rc = moveToChild(pCur, SWAB32(pCur->pBt, pCur->pPage->u.hdr.rightChild));
drh5e2f8b92001-05-28 00:41:15 +00001550 if( rc ) return rc;
1551 rc = moveToLeftmost(pCur);
1552 if( rc ) return rc;
1553 if( pRes ) *pRes = 0;
drh72f82862001-05-24 21:06:34 +00001554 return SQLITE_OK;
1555 }
drh5e2f8b92001-05-28 00:41:15 +00001556 do{
drh8c42ca92001-06-22 19:15:00 +00001557 if( pCur->pPage->pParent==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001558 if( pRes ) *pRes = 1;
1559 return SQLITE_OK;
1560 }
1561 rc = moveToParent(pCur);
1562 if( rc ) return rc;
1563 }while( pCur->idx>=pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00001564 if( pRes ) *pRes = 0;
1565 return SQLITE_OK;
1566 }
drh5e2f8b92001-05-28 00:41:15 +00001567 rc = moveToLeftmost(pCur);
1568 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001569 if( pRes ) *pRes = 0;
1570 return SQLITE_OK;
1571}
1572
drh3b7511c2001-05-26 13:15:44 +00001573/*
1574** Allocate a new page from the database file.
1575**
1576** The new page is marked as dirty. (In other words, sqlitepager_write()
1577** has already been called on the new page.) The new page has also
1578** been referenced and the calling routine is responsible for calling
1579** sqlitepager_unref() on the new page when it is done.
1580**
1581** SQLITE_OK is returned on success. Any other return value indicates
1582** an error. *ppPage and *pPgno are undefined in the event of an error.
1583** Do not invoke sqlitepager_unref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00001584**
drh199e3cf2002-07-18 11:01:47 +00001585** If the "nearby" parameter is not 0, then a (feeble) effort is made to
1586** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00001587** attempt to keep related pages close to each other in the database file,
1588** which in turn can make database access faster.
drh3b7511c2001-05-26 13:15:44 +00001589*/
drh199e3cf2002-07-18 11:01:47 +00001590static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){
drhbd03cae2001-06-02 02:40:57 +00001591 PageOne *pPage1 = pBt->page1;
drh8c42ca92001-06-22 19:15:00 +00001592 int rc;
drh3b7511c2001-05-26 13:15:44 +00001593 if( pPage1->freeList ){
1594 OverflowPage *pOvfl;
drh30e58752002-03-02 20:41:57 +00001595 FreelistInfo *pInfo;
1596
drh3b7511c2001-05-26 13:15:44 +00001597 rc = sqlitepager_write(pPage1);
1598 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00001599 SWAB_ADD(pBt, pPage1->nFree, -1);
1600 rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
1601 (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001602 if( rc ) return rc;
1603 rc = sqlitepager_write(pOvfl);
1604 if( rc ){
1605 sqlitepager_unref(pOvfl);
1606 return rc;
1607 }
drh30e58752002-03-02 20:41:57 +00001608 pInfo = (FreelistInfo*)pOvfl->aPayload;
1609 if( pInfo->nFree==0 ){
drh0d316a42002-08-11 20:10:47 +00001610 *pPgno = SWAB32(pBt, pPage1->freeList);
drh30e58752002-03-02 20:41:57 +00001611 pPage1->freeList = pOvfl->iNext;
1612 *ppPage = (MemPage*)pOvfl;
1613 }else{
drh0d316a42002-08-11 20:10:47 +00001614 int closest, n;
1615 n = SWAB32(pBt, pInfo->nFree);
1616 if( n>1 && nearby>0 ){
drhbea00b92002-07-08 10:59:50 +00001617 int i, dist;
1618 closest = 0;
drh0d316a42002-08-11 20:10:47 +00001619 dist = SWAB32(pBt, pInfo->aFree[0]) - nearby;
drhbea00b92002-07-08 10:59:50 +00001620 if( dist<0 ) dist = -dist;
drh0d316a42002-08-11 20:10:47 +00001621 for(i=1; i<n; i++){
1622 int d2 = SWAB32(pBt, pInfo->aFree[i]) - nearby;
drhbea00b92002-07-08 10:59:50 +00001623 if( d2<0 ) d2 = -d2;
1624 if( d2<dist ) closest = i;
1625 }
1626 }else{
1627 closest = 0;
1628 }
drh0d316a42002-08-11 20:10:47 +00001629 SWAB_ADD(pBt, pInfo->nFree, -1);
1630 *pPgno = SWAB32(pBt, pInfo->aFree[closest]);
1631 pInfo->aFree[closest] = pInfo->aFree[n-1];
drh30e58752002-03-02 20:41:57 +00001632 rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
1633 sqlitepager_unref(pOvfl);
1634 if( rc==SQLITE_OK ){
1635 sqlitepager_dont_rollback(*ppPage);
1636 rc = sqlitepager_write(*ppPage);
1637 }
1638 }
drh3b7511c2001-05-26 13:15:44 +00001639 }else{
drh2aa679f2001-06-25 02:11:07 +00001640 *pPgno = sqlitepager_pagecount(pBt->pPager) + 1;
drh8c42ca92001-06-22 19:15:00 +00001641 rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
drh3b7511c2001-05-26 13:15:44 +00001642 if( rc ) return rc;
1643 rc = sqlitepager_write(*ppPage);
1644 }
1645 return rc;
1646}
1647
1648/*
1649** Add a page of the database file to the freelist. Either pgno or
1650** pPage but not both may be 0.
drh5e2f8b92001-05-28 00:41:15 +00001651**
drhdd793422001-06-28 01:54:48 +00001652** sqlitepager_unref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00001653*/
1654static int freePage(Btree *pBt, void *pPage, Pgno pgno){
drhbd03cae2001-06-02 02:40:57 +00001655 PageOne *pPage1 = pBt->page1;
drh3b7511c2001-05-26 13:15:44 +00001656 OverflowPage *pOvfl = (OverflowPage*)pPage;
1657 int rc;
drhdd793422001-06-28 01:54:48 +00001658 int needUnref = 0;
1659 MemPage *pMemPage;
drh8b2f49b2001-06-08 00:21:52 +00001660
drh3b7511c2001-05-26 13:15:44 +00001661 if( pgno==0 ){
1662 assert( pOvfl!=0 );
1663 pgno = sqlitepager_pagenumber(pOvfl);
1664 }
drh2aa679f2001-06-25 02:11:07 +00001665 assert( pgno>2 );
drh193a6b42002-07-07 16:52:46 +00001666 pMemPage = (MemPage*)pPage;
1667 pMemPage->isInit = 0;
1668 if( pMemPage->pParent ){
1669 sqlitepager_unref(pMemPage->pParent);
1670 pMemPage->pParent = 0;
1671 }
drh3b7511c2001-05-26 13:15:44 +00001672 rc = sqlitepager_write(pPage1);
1673 if( rc ){
1674 return rc;
1675 }
drh0d316a42002-08-11 20:10:47 +00001676 SWAB_ADD(pBt, pPage1->nFree, 1);
1677 if( pPage1->nFree!=0 && pPage1->freeList!=0 ){
drh30e58752002-03-02 20:41:57 +00001678 OverflowPage *pFreeIdx;
drh0d316a42002-08-11 20:10:47 +00001679 rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
1680 (void**)&pFreeIdx);
drh30e58752002-03-02 20:41:57 +00001681 if( rc==SQLITE_OK ){
1682 FreelistInfo *pInfo = (FreelistInfo*)pFreeIdx->aPayload;
drh0d316a42002-08-11 20:10:47 +00001683 int n = SWAB32(pBt, pInfo->nFree);
1684 if( n<(sizeof(pInfo->aFree)/sizeof(pInfo->aFree[0])) ){
drh30e58752002-03-02 20:41:57 +00001685 rc = sqlitepager_write(pFreeIdx);
1686 if( rc==SQLITE_OK ){
drh0d316a42002-08-11 20:10:47 +00001687 pInfo->aFree[n] = SWAB32(pBt, pgno);
1688 SWAB_ADD(pBt, pInfo->nFree, 1);
drh30e58752002-03-02 20:41:57 +00001689 sqlitepager_unref(pFreeIdx);
1690 sqlitepager_dont_write(pBt->pPager, pgno);
1691 return rc;
1692 }
1693 }
1694 sqlitepager_unref(pFreeIdx);
1695 }
1696 }
drh3b7511c2001-05-26 13:15:44 +00001697 if( pOvfl==0 ){
1698 assert( pgno>0 );
drh8c42ca92001-06-22 19:15:00 +00001699 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001700 if( rc ) return rc;
drhdd793422001-06-28 01:54:48 +00001701 needUnref = 1;
drh3b7511c2001-05-26 13:15:44 +00001702 }
1703 rc = sqlitepager_write(pOvfl);
1704 if( rc ){
drhdd793422001-06-28 01:54:48 +00001705 if( needUnref ) sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001706 return rc;
1707 }
drh14acc042001-06-10 19:56:58 +00001708 pOvfl->iNext = pPage1->freeList;
drh0d316a42002-08-11 20:10:47 +00001709 pPage1->freeList = SWAB32(pBt, pgno);
drh5e2f8b92001-05-28 00:41:15 +00001710 memset(pOvfl->aPayload, 0, OVERFLOW_SIZE);
drhdd793422001-06-28 01:54:48 +00001711 if( needUnref ) rc = sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001712 return rc;
1713}
1714
1715/*
1716** Erase all the data out of a cell. This involves returning overflow
1717** pages back the freelist.
1718*/
1719static int clearCell(Btree *pBt, Cell *pCell){
1720 Pager *pPager = pBt->pPager;
1721 OverflowPage *pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001722 Pgno ovfl, nextOvfl;
1723 int rc;
1724
drh0d316a42002-08-11 20:10:47 +00001725 if( NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h) <= MX_LOCAL_PAYLOAD ){
drh5e2f8b92001-05-28 00:41:15 +00001726 return SQLITE_OK;
1727 }
drh0d316a42002-08-11 20:10:47 +00001728 ovfl = SWAB32(pBt, pCell->ovfl);
drh3b7511c2001-05-26 13:15:44 +00001729 pCell->ovfl = 0;
1730 while( ovfl ){
drh8c42ca92001-06-22 19:15:00 +00001731 rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001732 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00001733 nextOvfl = SWAB32(pBt, pOvfl->iNext);
drhbd03cae2001-06-02 02:40:57 +00001734 rc = freePage(pBt, pOvfl, ovfl);
1735 if( rc ) return rc;
drhdd793422001-06-28 01:54:48 +00001736 sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001737 ovfl = nextOvfl;
drh3b7511c2001-05-26 13:15:44 +00001738 }
drh5e2f8b92001-05-28 00:41:15 +00001739 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00001740}
1741
1742/*
1743** Create a new cell from key and data. Overflow pages are allocated as
1744** necessary and linked to this cell.
1745*/
1746static int fillInCell(
1747 Btree *pBt, /* The whole Btree. Needed to allocate pages */
1748 Cell *pCell, /* Populate this Cell structure */
drh5c4d9702001-08-20 00:33:58 +00001749 const void *pKey, int nKey, /* The key */
1750 const void *pData,int nData /* The data */
drh3b7511c2001-05-26 13:15:44 +00001751){
drhdd793422001-06-28 01:54:48 +00001752 OverflowPage *pOvfl, *pPrior;
drh3b7511c2001-05-26 13:15:44 +00001753 Pgno *pNext;
1754 int spaceLeft;
drh8c42ca92001-06-22 19:15:00 +00001755 int n, rc;
drh3b7511c2001-05-26 13:15:44 +00001756 int nPayload;
drh5c4d9702001-08-20 00:33:58 +00001757 const char *pPayload;
drh3b7511c2001-05-26 13:15:44 +00001758 char *pSpace;
drh199e3cf2002-07-18 11:01:47 +00001759 Pgno nearby = 0;
drh3b7511c2001-05-26 13:15:44 +00001760
drh5e2f8b92001-05-28 00:41:15 +00001761 pCell->h.leftChild = 0;
drh0d316a42002-08-11 20:10:47 +00001762 pCell->h.nKey = SWAB16(pBt, nKey & 0xffff);
drh80ff32f2001-11-04 18:32:46 +00001763 pCell->h.nKeyHi = nKey >> 16;
drh0d316a42002-08-11 20:10:47 +00001764 pCell->h.nData = SWAB16(pBt, nData & 0xffff);
drh80ff32f2001-11-04 18:32:46 +00001765 pCell->h.nDataHi = nData >> 16;
drh3b7511c2001-05-26 13:15:44 +00001766 pCell->h.iNext = 0;
1767
1768 pNext = &pCell->ovfl;
drh5e2f8b92001-05-28 00:41:15 +00001769 pSpace = pCell->aPayload;
drh3b7511c2001-05-26 13:15:44 +00001770 spaceLeft = MX_LOCAL_PAYLOAD;
1771 pPayload = pKey;
1772 pKey = 0;
1773 nPayload = nKey;
drhdd793422001-06-28 01:54:48 +00001774 pPrior = 0;
drh3b7511c2001-05-26 13:15:44 +00001775 while( nPayload>0 ){
1776 if( spaceLeft==0 ){
drh199e3cf2002-07-18 11:01:47 +00001777 rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext, nearby);
drh3b7511c2001-05-26 13:15:44 +00001778 if( rc ){
1779 *pNext = 0;
drhbea00b92002-07-08 10:59:50 +00001780 }else{
drh199e3cf2002-07-18 11:01:47 +00001781 nearby = *pNext;
drhdd793422001-06-28 01:54:48 +00001782 }
1783 if( pPrior ) sqlitepager_unref(pPrior);
1784 if( rc ){
drh5e2f8b92001-05-28 00:41:15 +00001785 clearCell(pBt, pCell);
drh3b7511c2001-05-26 13:15:44 +00001786 return rc;
1787 }
drh0d316a42002-08-11 20:10:47 +00001788 if( pBt->needSwab ) *pNext = swab32(*pNext);
drhdd793422001-06-28 01:54:48 +00001789 pPrior = pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001790 spaceLeft = OVERFLOW_SIZE;
drh5e2f8b92001-05-28 00:41:15 +00001791 pSpace = pOvfl->aPayload;
drh8c42ca92001-06-22 19:15:00 +00001792 pNext = &pOvfl->iNext;
drh3b7511c2001-05-26 13:15:44 +00001793 }
1794 n = nPayload;
1795 if( n>spaceLeft ) n = spaceLeft;
1796 memcpy(pSpace, pPayload, n);
1797 nPayload -= n;
1798 if( nPayload==0 && pData ){
1799 pPayload = pData;
1800 nPayload = nData;
1801 pData = 0;
1802 }else{
1803 pPayload += n;
1804 }
1805 spaceLeft -= n;
1806 pSpace += n;
1807 }
drhdd793422001-06-28 01:54:48 +00001808 *pNext = 0;
1809 if( pPrior ){
1810 sqlitepager_unref(pPrior);
1811 }
drh3b7511c2001-05-26 13:15:44 +00001812 return SQLITE_OK;
1813}
1814
1815/*
drhbd03cae2001-06-02 02:40:57 +00001816** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00001817** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00001818** pointer in the third argument.
1819*/
1820static void reparentPage(Pager *pPager, Pgno pgno, MemPage *pNewParent){
1821 MemPage *pThis;
1822
drhdd793422001-06-28 01:54:48 +00001823 if( pgno==0 ) return;
1824 assert( pPager!=0 );
drhbd03cae2001-06-02 02:40:57 +00001825 pThis = sqlitepager_lookup(pPager, pgno);
drh6019e162001-07-02 17:51:45 +00001826 if( pThis && pThis->isInit ){
drhdd793422001-06-28 01:54:48 +00001827 if( pThis->pParent!=pNewParent ){
1828 if( pThis->pParent ) sqlitepager_unref(pThis->pParent);
1829 pThis->pParent = pNewParent;
1830 if( pNewParent ) sqlitepager_ref(pNewParent);
1831 }
1832 sqlitepager_unref(pThis);
drhbd03cae2001-06-02 02:40:57 +00001833 }
1834}
1835
1836/*
1837** Reparent all children of the given page to be the given page.
1838** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00001839** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00001840**
1841** This routine gets called after you memcpy() one page into
1842** another.
1843*/
drh0d316a42002-08-11 20:10:47 +00001844static void reparentChildPages(Btree *pBt, MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00001845 int i;
drh0d316a42002-08-11 20:10:47 +00001846 Pager *pPager = pBt->pPager;
drhbd03cae2001-06-02 02:40:57 +00001847 for(i=0; i<pPage->nCell; i++){
drh0d316a42002-08-11 20:10:47 +00001848 reparentPage(pPager, SWAB32(pBt, pPage->apCell[i]->h.leftChild), pPage);
drhbd03cae2001-06-02 02:40:57 +00001849 }
drh0d316a42002-08-11 20:10:47 +00001850 reparentPage(pPager, SWAB32(pBt, pPage->u.hdr.rightChild), pPage);
drh14acc042001-06-10 19:56:58 +00001851}
1852
1853/*
1854** Remove the i-th cell from pPage. This routine effects pPage only.
1855** The cell content is not freed or deallocated. It is assumed that
1856** the cell content has been copied someplace else. This routine just
1857** removes the reference to the cell from pPage.
1858**
1859** "sz" must be the number of bytes in the cell.
1860**
1861** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001862** Only the pPage->apCell[] array is important. The relinkCellList()
1863** routine will be called soon after this routine in order to rebuild
1864** the linked list.
drh14acc042001-06-10 19:56:58 +00001865*/
drh0d316a42002-08-11 20:10:47 +00001866static void dropCell(Btree *pBt, MemPage *pPage, int idx, int sz){
drh14acc042001-06-10 19:56:58 +00001867 int j;
drh8c42ca92001-06-22 19:15:00 +00001868 assert( idx>=0 && idx<pPage->nCell );
drh0d316a42002-08-11 20:10:47 +00001869 assert( sz==cellSize(pBt, pPage->apCell[idx]) );
drh6019e162001-07-02 17:51:45 +00001870 assert( sqlitepager_iswriteable(pPage) );
drh0d316a42002-08-11 20:10:47 +00001871 freeSpace(pBt, pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz);
drh7c717f72001-06-24 20:39:41 +00001872 for(j=idx; j<pPage->nCell-1; j++){
drh14acc042001-06-10 19:56:58 +00001873 pPage->apCell[j] = pPage->apCell[j+1];
1874 }
1875 pPage->nCell--;
1876}
1877
1878/*
1879** Insert a new cell on pPage at cell index "i". pCell points to the
1880** content of the cell.
1881**
1882** If the cell content will fit on the page, then put it there. If it
1883** will not fit, then just make pPage->apCell[i] point to the content
1884** and set pPage->isOverfull.
1885**
1886** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001887** Only the pPage->apCell[] array is important. The relinkCellList()
1888** routine will be called soon after this routine in order to rebuild
1889** the linked list.
drh14acc042001-06-10 19:56:58 +00001890*/
drh0d316a42002-08-11 20:10:47 +00001891static void insertCell(Btree *pBt, MemPage *pPage, int i, Cell *pCell, int sz){
drh14acc042001-06-10 19:56:58 +00001892 int idx, j;
1893 assert( i>=0 && i<=pPage->nCell );
drh0d316a42002-08-11 20:10:47 +00001894 assert( sz==cellSize(pBt, pCell) );
drh6019e162001-07-02 17:51:45 +00001895 assert( sqlitepager_iswriteable(pPage) );
drh0d316a42002-08-11 20:10:47 +00001896 idx = allocateSpace(pBt, pPage, sz);
drh14acc042001-06-10 19:56:58 +00001897 for(j=pPage->nCell; j>i; j--){
1898 pPage->apCell[j] = pPage->apCell[j-1];
1899 }
1900 pPage->nCell++;
drh14acc042001-06-10 19:56:58 +00001901 if( idx<=0 ){
1902 pPage->isOverfull = 1;
1903 pPage->apCell[i] = pCell;
1904 }else{
1905 memcpy(&pPage->u.aDisk[idx], pCell, sz);
drh8c42ca92001-06-22 19:15:00 +00001906 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx];
drh14acc042001-06-10 19:56:58 +00001907 }
1908}
1909
1910/*
1911** Rebuild the linked list of cells on a page so that the cells
drh8c42ca92001-06-22 19:15:00 +00001912** occur in the order specified by the pPage->apCell[] array.
1913** Invoke this routine once to repair damage after one or more
1914** invocations of either insertCell() or dropCell().
drh14acc042001-06-10 19:56:58 +00001915*/
drh0d316a42002-08-11 20:10:47 +00001916static void relinkCellList(Btree *pBt, MemPage *pPage){
drh14acc042001-06-10 19:56:58 +00001917 int i;
1918 u16 *pIdx;
drh6019e162001-07-02 17:51:45 +00001919 assert( sqlitepager_iswriteable(pPage) );
drh14acc042001-06-10 19:56:58 +00001920 pIdx = &pPage->u.hdr.firstCell;
1921 for(i=0; i<pPage->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001922 int idx = Addr(pPage->apCell[i]) - Addr(pPage);
drh8c42ca92001-06-22 19:15:00 +00001923 assert( idx>0 && idx<SQLITE_PAGE_SIZE );
drh0d316a42002-08-11 20:10:47 +00001924 *pIdx = SWAB16(pBt, idx);
drh14acc042001-06-10 19:56:58 +00001925 pIdx = &pPage->apCell[i]->h.iNext;
1926 }
1927 *pIdx = 0;
1928}
1929
1930/*
1931** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[]
drh5e00f6c2001-09-13 13:46:56 +00001932** pointers that point into pFrom->u.aDisk[] must be adjusted to point
drhdd793422001-06-28 01:54:48 +00001933** into pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might
drh14acc042001-06-10 19:56:58 +00001934** not point to pFrom->u.aDisk[]. Those are unchanged.
1935*/
1936static void copyPage(MemPage *pTo, MemPage *pFrom){
1937 uptr from, to;
1938 int i;
1939 memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_PAGE_SIZE);
drhdd793422001-06-28 01:54:48 +00001940 pTo->pParent = 0;
drh14acc042001-06-10 19:56:58 +00001941 pTo->isInit = 1;
1942 pTo->nCell = pFrom->nCell;
1943 pTo->nFree = pFrom->nFree;
1944 pTo->isOverfull = pFrom->isOverfull;
drh7c717f72001-06-24 20:39:41 +00001945 to = Addr(pTo);
1946 from = Addr(pFrom);
drh14acc042001-06-10 19:56:58 +00001947 for(i=0; i<pTo->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001948 uptr x = Addr(pFrom->apCell[i]);
drh8c42ca92001-06-22 19:15:00 +00001949 if( x>from && x<from+SQLITE_PAGE_SIZE ){
1950 *((uptr*)&pTo->apCell[i]) = x + to - from;
drhdd793422001-06-28 01:54:48 +00001951 }else{
1952 pTo->apCell[i] = pFrom->apCell[i];
drh14acc042001-06-10 19:56:58 +00001953 }
1954 }
drhbd03cae2001-06-02 02:40:57 +00001955}
1956
1957/*
drh8b2f49b2001-06-08 00:21:52 +00001958** This routine redistributes Cells on pPage and up to two siblings
1959** of pPage so that all pages have about the same amount of free space.
drh14acc042001-06-10 19:56:58 +00001960** Usually one sibling on either side of pPage is used in the balancing,
drh8b2f49b2001-06-08 00:21:52 +00001961** though both siblings might come from one side if pPage is the first
1962** or last child of its parent. If pPage has fewer than two siblings
1963** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00001964** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00001965**
1966** The number of siblings of pPage might be increased or decreased by
drh8c42ca92001-06-22 19:15:00 +00001967** one in an effort to keep pages between 66% and 100% full. The root page
1968** is special and is allowed to be less than 66% full. If pPage is
1969** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00001970** or decreased by one, as necessary, to keep the root page from being
1971** overfull or empty.
1972**
drh14acc042001-06-10 19:56:58 +00001973** This routine calls relinkCellList() on its input page regardless of
1974** whether or not it does any real balancing. Client routines will typically
1975** invoke insertCell() or dropCell() before calling this routine, so we
1976** need to call relinkCellList() to clean up the mess that those other
1977** routines left behind.
1978**
1979** pCur is left pointing to the same cell as when this routine was called
drh8c42ca92001-06-22 19:15:00 +00001980** even if that cell gets moved to a different page. pCur may be NULL.
1981** Set the pCur parameter to NULL if you do not care about keeping track
1982** of a cell as that will save this routine the work of keeping track of it.
drh14acc042001-06-10 19:56:58 +00001983**
drh8b2f49b2001-06-08 00:21:52 +00001984** Note that when this routine is called, some of the Cells on pPage
drh14acc042001-06-10 19:56:58 +00001985** might not actually be stored in pPage->u.aDisk[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00001986** if the page is overfull. Part of the job of this routine is to
drh14acc042001-06-10 19:56:58 +00001987** make sure all Cells for pPage once again fit in pPage->u.aDisk[].
1988**
drh8c42ca92001-06-22 19:15:00 +00001989** In the course of balancing the siblings of pPage, the parent of pPage
1990** might become overfull or underfull. If that happens, then this routine
1991** is called recursively on the parent.
1992**
drh5e00f6c2001-09-13 13:46:56 +00001993** If this routine fails for any reason, it might leave the database
1994** in a corrupted state. So if this routine fails, the database should
1995** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00001996*/
drh14acc042001-06-10 19:56:58 +00001997static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
drh8b2f49b2001-06-08 00:21:52 +00001998 MemPage *pParent; /* The parent of pPage */
drh14acc042001-06-10 19:56:58 +00001999 MemPage *apOld[3]; /* pPage and up to two siblings */
drh8b2f49b2001-06-08 00:21:52 +00002000 Pgno pgnoOld[3]; /* Page numbers for each page in apOld[] */
drh14acc042001-06-10 19:56:58 +00002001 MemPage *apNew[4]; /* pPage and up to 3 siblings after balancing */
2002 Pgno pgnoNew[4]; /* Page numbers for each page in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00002003 int idxDiv[3]; /* Indices of divider cells in pParent */
2004 Cell *apDiv[3]; /* Divider cells in pParent */
2005 int nCell; /* Number of cells in apCell[] */
2006 int nOld; /* Number of pages in apOld[] */
2007 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00002008 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00002009 int i, j, k; /* Loop counters */
2010 int idx; /* Index of pPage in pParent->apCell[] */
2011 int nxDiv; /* Next divider slot in pParent->apCell[] */
2012 int rc; /* The return code */
2013 int iCur; /* apCell[iCur] is the cell of the cursor */
drh5edc3122001-09-13 21:53:09 +00002014 MemPage *pOldCurPage; /* The cursor originally points to this page */
drh6019e162001-07-02 17:51:45 +00002015 int subtotal; /* Subtotal of bytes in cells on one page */
2016 int cntNew[4]; /* Index in apCell[] of cell after i-th page */
2017 int szNew[4]; /* Combined size of cells place on i-th page */
drh9ca7d3b2001-06-28 11:50:21 +00002018 MemPage *extraUnref = 0; /* A page that needs to be unref-ed */
drh0d316a42002-08-11 20:10:47 +00002019 Pgno pgno, swabPgno; /* Page number */
drh14acc042001-06-10 19:56:58 +00002020 Cell *apCell[MX_CELL*3+5]; /* All cells from pages being balanceed */
2021 int szCell[MX_CELL*3+5]; /* Local size of all cells */
2022 Cell aTemp[2]; /* Temporary holding area for apDiv[] */
2023 MemPage aOld[3]; /* Temporary copies of pPage and its siblings */
drh8b2f49b2001-06-08 00:21:52 +00002024
drh14acc042001-06-10 19:56:58 +00002025 /*
2026 ** Return without doing any work if pPage is neither overfull nor
2027 ** underfull.
drh8b2f49b2001-06-08 00:21:52 +00002028 */
drh6019e162001-07-02 17:51:45 +00002029 assert( sqlitepager_iswriteable(pPage) );
drha1b351a2001-09-14 16:42:12 +00002030 if( !pPage->isOverfull && pPage->nFree<SQLITE_PAGE_SIZE/2
2031 && pPage->nCell>=2){
drh0d316a42002-08-11 20:10:47 +00002032 relinkCellList(pBt, pPage);
drh8b2f49b2001-06-08 00:21:52 +00002033 return SQLITE_OK;
2034 }
2035
2036 /*
drh14acc042001-06-10 19:56:58 +00002037 ** Find the parent of the page to be balanceed.
2038 ** If there is no parent, it means this page is the root page and
drh8b2f49b2001-06-08 00:21:52 +00002039 ** special rules apply.
2040 */
drh14acc042001-06-10 19:56:58 +00002041 pParent = pPage->pParent;
drh8b2f49b2001-06-08 00:21:52 +00002042 if( pParent==0 ){
2043 Pgno pgnoChild;
drh8c42ca92001-06-22 19:15:00 +00002044 MemPage *pChild;
drh7aa128d2002-06-21 13:09:16 +00002045 assert( pPage->isInit );
drh8b2f49b2001-06-08 00:21:52 +00002046 if( pPage->nCell==0 ){
drh14acc042001-06-10 19:56:58 +00002047 if( pPage->u.hdr.rightChild ){
2048 /*
2049 ** The root page is empty. Copy the one child page
drh8b2f49b2001-06-08 00:21:52 +00002050 ** into the root page and return. This reduces the depth
2051 ** of the BTree by one.
2052 */
drh0d316a42002-08-11 20:10:47 +00002053 pgnoChild = SWAB32(pBt, pPage->u.hdr.rightChild);
drh8c42ca92001-06-22 19:15:00 +00002054 rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild);
drh8b2f49b2001-06-08 00:21:52 +00002055 if( rc ) return rc;
2056 memcpy(pPage, pChild, SQLITE_PAGE_SIZE);
2057 pPage->isInit = 0;
drh0d316a42002-08-11 20:10:47 +00002058 rc = initPage(pBt, pPage, sqlitepager_pagenumber(pPage), 0);
drh6019e162001-07-02 17:51:45 +00002059 assert( rc==SQLITE_OK );
drh0d316a42002-08-11 20:10:47 +00002060 reparentChildPages(pBt, pPage);
drh5edc3122001-09-13 21:53:09 +00002061 if( pCur && pCur->pPage==pChild ){
2062 sqlitepager_unref(pChild);
2063 pCur->pPage = pPage;
2064 sqlitepager_ref(pPage);
2065 }
drh8b2f49b2001-06-08 00:21:52 +00002066 freePage(pBt, pChild, pgnoChild);
2067 sqlitepager_unref(pChild);
drhefc251d2001-07-01 22:12:01 +00002068 }else{
drh0d316a42002-08-11 20:10:47 +00002069 relinkCellList(pBt, pPage);
drh8b2f49b2001-06-08 00:21:52 +00002070 }
2071 return SQLITE_OK;
2072 }
drh14acc042001-06-10 19:56:58 +00002073 if( !pPage->isOverfull ){
drh8b2f49b2001-06-08 00:21:52 +00002074 /* It is OK for the root page to be less than half full.
2075 */
drh0d316a42002-08-11 20:10:47 +00002076 relinkCellList(pBt, pPage);
drh8b2f49b2001-06-08 00:21:52 +00002077 return SQLITE_OK;
2078 }
drh14acc042001-06-10 19:56:58 +00002079 /*
2080 ** If we get to here, it means the root page is overfull.
drh8b2f49b2001-06-08 00:21:52 +00002081 ** When this happens, Create a new child page and copy the
2082 ** contents of the root into the child. Then make the root
drh14acc042001-06-10 19:56:58 +00002083 ** page an empty page with rightChild pointing to the new
drh8b2f49b2001-06-08 00:21:52 +00002084 ** child. Then fall thru to the code below which will cause
2085 ** the overfull child page to be split.
2086 */
drh14acc042001-06-10 19:56:58 +00002087 rc = sqlitepager_write(pPage);
2088 if( rc ) return rc;
drhbea00b92002-07-08 10:59:50 +00002089 rc = allocatePage(pBt, &pChild, &pgnoChild, sqlitepager_pagenumber(pPage));
drh8b2f49b2001-06-08 00:21:52 +00002090 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00002091 assert( sqlitepager_iswriteable(pChild) );
drh14acc042001-06-10 19:56:58 +00002092 copyPage(pChild, pPage);
2093 pChild->pParent = pPage;
drhdd793422001-06-28 01:54:48 +00002094 sqlitepager_ref(pPage);
drh14acc042001-06-10 19:56:58 +00002095 pChild->isOverfull = 1;
drh5edc3122001-09-13 21:53:09 +00002096 if( pCur && pCur->pPage==pPage ){
2097 sqlitepager_unref(pPage);
drh14acc042001-06-10 19:56:58 +00002098 pCur->pPage = pChild;
drh9ca7d3b2001-06-28 11:50:21 +00002099 }else{
2100 extraUnref = pChild;
drh8b2f49b2001-06-08 00:21:52 +00002101 }
drh0d316a42002-08-11 20:10:47 +00002102 zeroPage(pBt, pPage);
2103 pPage->u.hdr.rightChild = SWAB32(pBt, pgnoChild);
drh8b2f49b2001-06-08 00:21:52 +00002104 pParent = pPage;
2105 pPage = pChild;
drh8b2f49b2001-06-08 00:21:52 +00002106 }
drh6019e162001-07-02 17:51:45 +00002107 rc = sqlitepager_write(pParent);
2108 if( rc ) return rc;
drh7aa128d2002-06-21 13:09:16 +00002109 assert( pParent->isInit );
drh14acc042001-06-10 19:56:58 +00002110
drh8b2f49b2001-06-08 00:21:52 +00002111 /*
drh14acc042001-06-10 19:56:58 +00002112 ** Find the Cell in the parent page whose h.leftChild points back
2113 ** to pPage. The "idx" variable is the index of that cell. If pPage
2114 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00002115 */
2116 idx = -1;
2117 pgno = sqlitepager_pagenumber(pPage);
drh0d316a42002-08-11 20:10:47 +00002118 swabPgno = SWAB32(pBt, pgno);
drh8b2f49b2001-06-08 00:21:52 +00002119 for(i=0; i<pParent->nCell; i++){
drh0d316a42002-08-11 20:10:47 +00002120 if( pParent->apCell[i]->h.leftChild==swabPgno ){
drh8b2f49b2001-06-08 00:21:52 +00002121 idx = i;
2122 break;
2123 }
2124 }
drh0d316a42002-08-11 20:10:47 +00002125 if( idx<0 && pParent->u.hdr.rightChild==swabPgno ){
drhdd793422001-06-28 01:54:48 +00002126 idx = pParent->nCell;
drh8b2f49b2001-06-08 00:21:52 +00002127 }
2128 if( idx<0 ){
drh14acc042001-06-10 19:56:58 +00002129 return SQLITE_CORRUPT;
drh8b2f49b2001-06-08 00:21:52 +00002130 }
2131
2132 /*
drh14acc042001-06-10 19:56:58 +00002133 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00002134 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00002135 */
drh14acc042001-06-10 19:56:58 +00002136 nOld = nNew = 0;
2137 sqlitepager_ref(pParent);
2138
2139 /*
2140 ** Find sibling pages to pPage and the Cells in pParent that divide
2141 ** the siblings. An attempt is made to find one sibling on either
2142 ** side of pPage. Both siblings are taken from one side, however, if
2143 ** pPage is either the first or last child of its parent. If pParent
2144 ** has 3 or fewer children then all children of pParent are taken.
2145 */
2146 if( idx==pParent->nCell ){
2147 nxDiv = idx - 2;
drh8b2f49b2001-06-08 00:21:52 +00002148 }else{
drh14acc042001-06-10 19:56:58 +00002149 nxDiv = idx - 1;
drh8b2f49b2001-06-08 00:21:52 +00002150 }
drh14acc042001-06-10 19:56:58 +00002151 if( nxDiv<0 ) nxDiv = 0;
drh8b2f49b2001-06-08 00:21:52 +00002152 nDiv = 0;
drh14acc042001-06-10 19:56:58 +00002153 for(i=0, k=nxDiv; i<3; i++, k++){
2154 if( k<pParent->nCell ){
2155 idxDiv[i] = k;
2156 apDiv[i] = pParent->apCell[k];
drh8b2f49b2001-06-08 00:21:52 +00002157 nDiv++;
drh0d316a42002-08-11 20:10:47 +00002158 pgnoOld[i] = SWAB32(pBt, apDiv[i]->h.leftChild);
drh14acc042001-06-10 19:56:58 +00002159 }else if( k==pParent->nCell ){
drh0d316a42002-08-11 20:10:47 +00002160 pgnoOld[i] = SWAB32(pBt, pParent->u.hdr.rightChild);
drh14acc042001-06-10 19:56:58 +00002161 }else{
2162 break;
drh8b2f49b2001-06-08 00:21:52 +00002163 }
drh8c42ca92001-06-22 19:15:00 +00002164 rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]);
drh14acc042001-06-10 19:56:58 +00002165 if( rc ) goto balance_cleanup;
drh0d316a42002-08-11 20:10:47 +00002166 rc = initPage(pBt, apOld[i], pgnoOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00002167 if( rc ) goto balance_cleanup;
drh14acc042001-06-10 19:56:58 +00002168 nOld++;
drh8b2f49b2001-06-08 00:21:52 +00002169 }
2170
2171 /*
drh14acc042001-06-10 19:56:58 +00002172 ** Set iCur to be the index in apCell[] of the cell that the cursor
2173 ** is pointing to. We will need this later on in order to keep the
drh5edc3122001-09-13 21:53:09 +00002174 ** cursor pointing at the same cell. If pCur points to a page that
2175 ** has no involvement with this rebalancing, then set iCur to a large
2176 ** number so that the iCur==j tests always fail in the main cell
2177 ** distribution loop below.
drh14acc042001-06-10 19:56:58 +00002178 */
2179 if( pCur ){
drh5edc3122001-09-13 21:53:09 +00002180 iCur = 0;
2181 for(i=0; i<nOld; i++){
2182 if( pCur->pPage==apOld[i] ){
2183 iCur += pCur->idx;
2184 break;
2185 }
2186 iCur += apOld[i]->nCell;
2187 if( i<nOld-1 && pCur->pPage==pParent && pCur->idx==idxDiv[i] ){
2188 break;
2189 }
2190 iCur++;
drh14acc042001-06-10 19:56:58 +00002191 }
drh5edc3122001-09-13 21:53:09 +00002192 pOldCurPage = pCur->pPage;
drh14acc042001-06-10 19:56:58 +00002193 }
2194
2195 /*
2196 ** Make copies of the content of pPage and its siblings into aOld[].
2197 ** The rest of this function will use data from the copies rather
2198 ** that the original pages since the original pages will be in the
2199 ** process of being overwritten.
2200 */
2201 for(i=0; i<nOld; i++){
2202 copyPage(&aOld[i], apOld[i]);
drh14acc042001-06-10 19:56:58 +00002203 }
2204
2205 /*
2206 ** Load pointers to all cells on sibling pages and the divider cells
2207 ** into the local apCell[] array. Make copies of the divider cells
2208 ** into aTemp[] and remove the the divider Cells from pParent.
drh8b2f49b2001-06-08 00:21:52 +00002209 */
2210 nCell = 0;
2211 for(i=0; i<nOld; i++){
drh6b308672002-07-08 02:16:37 +00002212 MemPage *pOld = &aOld[i];
drh8b2f49b2001-06-08 00:21:52 +00002213 for(j=0; j<pOld->nCell; j++){
drh14acc042001-06-10 19:56:58 +00002214 apCell[nCell] = pOld->apCell[j];
drh0d316a42002-08-11 20:10:47 +00002215 szCell[nCell] = cellSize(pBt, apCell[nCell]);
drh14acc042001-06-10 19:56:58 +00002216 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00002217 }
2218 if( i<nOld-1 ){
drh0d316a42002-08-11 20:10:47 +00002219 szCell[nCell] = cellSize(pBt, apDiv[i]);
drh8c42ca92001-06-22 19:15:00 +00002220 memcpy(&aTemp[i], apDiv[i], szCell[nCell]);
drh14acc042001-06-10 19:56:58 +00002221 apCell[nCell] = &aTemp[i];
drh0d316a42002-08-11 20:10:47 +00002222 dropCell(pBt, pParent, nxDiv, szCell[nCell]);
2223 assert( SWAB32(pBt, apCell[nCell]->h.leftChild)==pgnoOld[i] );
drh14acc042001-06-10 19:56:58 +00002224 apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild;
2225 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00002226 }
2227 }
2228
2229 /*
drh6019e162001-07-02 17:51:45 +00002230 ** Figure out the number of pages needed to hold all nCell cells.
2231 ** Store this number in "k". Also compute szNew[] which is the total
2232 ** size of all cells on the i-th page and cntNew[] which is the index
2233 ** in apCell[] of the cell that divides path i from path i+1.
2234 ** cntNew[k] should equal nCell.
2235 **
2236 ** This little patch of code is critical for keeping the tree
2237 ** balanced.
drh8b2f49b2001-06-08 00:21:52 +00002238 */
drh6019e162001-07-02 17:51:45 +00002239 for(subtotal=k=i=0; i<nCell; i++){
2240 subtotal += szCell[i];
2241 if( subtotal > USABLE_SPACE ){
2242 szNew[k] = subtotal - szCell[i];
2243 cntNew[k] = i;
2244 subtotal = 0;
2245 k++;
2246 }
2247 }
2248 szNew[k] = subtotal;
2249 cntNew[k] = nCell;
2250 k++;
2251 for(i=k-1; i>0; i--){
2252 while( szNew[i]<USABLE_SPACE/2 ){
2253 cntNew[i-1]--;
2254 assert( cntNew[i-1]>0 );
2255 szNew[i] += szCell[cntNew[i-1]];
2256 szNew[i-1] -= szCell[cntNew[i-1]-1];
2257 }
2258 }
2259 assert( cntNew[0]>0 );
drh8b2f49b2001-06-08 00:21:52 +00002260
2261 /*
drh6b308672002-07-08 02:16:37 +00002262 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00002263 */
drh14acc042001-06-10 19:56:58 +00002264 for(i=0; i<k; i++){
drh6b308672002-07-08 02:16:37 +00002265 if( i<nOld ){
2266 apNew[i] = apOld[i];
2267 pgnoNew[i] = pgnoOld[i];
2268 apOld[i] = 0;
2269 sqlitepager_write(apNew[i]);
2270 }else{
drhbea00b92002-07-08 10:59:50 +00002271 rc = allocatePage(pBt, &apNew[i], &pgnoNew[i], pgnoNew[i-1]);
drh6b308672002-07-08 02:16:37 +00002272 if( rc ) goto balance_cleanup;
2273 }
drh14acc042001-06-10 19:56:58 +00002274 nNew++;
drh0d316a42002-08-11 20:10:47 +00002275 zeroPage(pBt, apNew[i]);
drh6019e162001-07-02 17:51:45 +00002276 apNew[i]->isInit = 1;
drh8b2f49b2001-06-08 00:21:52 +00002277 }
2278
drh6b308672002-07-08 02:16:37 +00002279 /* Free any old pages that were not reused as new pages.
2280 */
2281 while( i<nOld ){
2282 rc = freePage(pBt, apOld[i], pgnoOld[i]);
2283 if( rc ) goto balance_cleanup;
2284 sqlitepager_unref(apOld[i]);
2285 apOld[i] = 0;
2286 i++;
2287 }
2288
drh8b2f49b2001-06-08 00:21:52 +00002289 /*
drhf9ffac92002-03-02 19:00:31 +00002290 ** Put the new pages in accending order. This helps to
2291 ** keep entries in the disk file in order so that a scan
2292 ** of the table is a linear scan through the file. That
2293 ** in turn helps the operating system to deliver pages
2294 ** from the disk more rapidly.
2295 **
2296 ** An O(n^2) insertion sort algorithm is used, but since
2297 ** n is never more than 3, that should not be a problem.
2298 **
2299 ** This one optimization makes the database about 25%
2300 ** faster for large insertions and deletions.
2301 */
2302 for(i=0; i<k-1; i++){
2303 int minV = pgnoNew[i];
2304 int minI = i;
2305 for(j=i+1; j<k; j++){
2306 if( pgnoNew[j]<minV ){
2307 minI = j;
2308 minV = pgnoNew[j];
2309 }
2310 }
2311 if( minI>i ){
2312 int t;
2313 MemPage *pT;
2314 t = pgnoNew[i];
2315 pT = apNew[i];
2316 pgnoNew[i] = pgnoNew[minI];
2317 apNew[i] = apNew[minI];
2318 pgnoNew[minI] = t;
2319 apNew[minI] = pT;
2320 }
2321 }
2322
2323 /*
drh14acc042001-06-10 19:56:58 +00002324 ** Evenly distribute the data in apCell[] across the new pages.
2325 ** Insert divider cells into pParent as necessary.
2326 */
2327 j = 0;
2328 for(i=0; i<nNew; i++){
2329 MemPage *pNew = apNew[i];
drh6019e162001-07-02 17:51:45 +00002330 while( j<cntNew[i] ){
2331 assert( pNew->nFree>=szCell[j] );
drh14acc042001-06-10 19:56:58 +00002332 if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; }
drh0d316a42002-08-11 20:10:47 +00002333 insertCell(pBt, pNew, pNew->nCell, apCell[j], szCell[j]);
drh14acc042001-06-10 19:56:58 +00002334 j++;
2335 }
drh6019e162001-07-02 17:51:45 +00002336 assert( pNew->nCell>0 );
drh14acc042001-06-10 19:56:58 +00002337 assert( !pNew->isOverfull );
drh0d316a42002-08-11 20:10:47 +00002338 relinkCellList(pBt, pNew);
drh14acc042001-06-10 19:56:58 +00002339 if( i<nNew-1 && j<nCell ){
2340 pNew->u.hdr.rightChild = apCell[j]->h.leftChild;
drh0d316a42002-08-11 20:10:47 +00002341 apCell[j]->h.leftChild = SWAB32(pBt, pgnoNew[i]);
drh14acc042001-06-10 19:56:58 +00002342 if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; }
drh0d316a42002-08-11 20:10:47 +00002343 insertCell(pBt, pParent, nxDiv, apCell[j], szCell[j]);
drh14acc042001-06-10 19:56:58 +00002344 j++;
2345 nxDiv++;
2346 }
2347 }
drh6019e162001-07-02 17:51:45 +00002348 assert( j==nCell );
drh6b308672002-07-08 02:16:37 +00002349 apNew[nNew-1]->u.hdr.rightChild = aOld[nOld-1].u.hdr.rightChild;
drh14acc042001-06-10 19:56:58 +00002350 if( nxDiv==pParent->nCell ){
drh0d316a42002-08-11 20:10:47 +00002351 pParent->u.hdr.rightChild = SWAB32(pBt, pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00002352 }else{
drh0d316a42002-08-11 20:10:47 +00002353 pParent->apCell[nxDiv]->h.leftChild = SWAB32(pBt, pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00002354 }
2355 if( pCur ){
drh3fc190c2001-09-14 03:24:23 +00002356 if( j<=iCur && pCur->pPage==pParent && pCur->idx>idxDiv[nOld-1] ){
2357 assert( pCur->pPage==pOldCurPage );
2358 pCur->idx += nNew - nOld;
2359 }else{
2360 assert( pOldCurPage!=0 );
2361 sqlitepager_ref(pCur->pPage);
2362 sqlitepager_unref(pOldCurPage);
2363 }
drh14acc042001-06-10 19:56:58 +00002364 }
2365
2366 /*
2367 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00002368 */
2369 for(i=0; i<nNew; i++){
drh0d316a42002-08-11 20:10:47 +00002370 reparentChildPages(pBt, apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00002371 }
drh0d316a42002-08-11 20:10:47 +00002372 reparentChildPages(pBt, pParent);
drh8b2f49b2001-06-08 00:21:52 +00002373
2374 /*
drh14acc042001-06-10 19:56:58 +00002375 ** balance the parent page.
drh8b2f49b2001-06-08 00:21:52 +00002376 */
drh5edc3122001-09-13 21:53:09 +00002377 rc = balance(pBt, pParent, pCur);
drh8b2f49b2001-06-08 00:21:52 +00002378
2379 /*
drh14acc042001-06-10 19:56:58 +00002380 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00002381 */
drh14acc042001-06-10 19:56:58 +00002382balance_cleanup:
drh9ca7d3b2001-06-28 11:50:21 +00002383 if( extraUnref ){
2384 sqlitepager_unref(extraUnref);
2385 }
drh8b2f49b2001-06-08 00:21:52 +00002386 for(i=0; i<nOld; i++){
drh6b308672002-07-08 02:16:37 +00002387 if( apOld[i]!=0 && apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00002388 }
drh14acc042001-06-10 19:56:58 +00002389 for(i=0; i<nNew; i++){
2390 sqlitepager_unref(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00002391 }
drh14acc042001-06-10 19:56:58 +00002392 if( pCur && pCur->pPage==0 ){
2393 pCur->pPage = pParent;
2394 pCur->idx = 0;
2395 }else{
2396 sqlitepager_unref(pParent);
drh8b2f49b2001-06-08 00:21:52 +00002397 }
2398 return rc;
2399}
2400
2401/*
drhf74b8d92002-09-01 23:20:45 +00002402** This routine checks all cursors that point to the same table
2403** as pCur points to. If any of those cursors were opened with
2404** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
2405** cursors point to the same table were opened with wrFlag==1
2406** then this routine returns SQLITE_OK.
2407**
2408** In addition to checking for read-locks (where a read-lock
2409** means a cursor opened with wrFlag==0) this routine also moves
2410** all cursors other than pCur so that they are pointing to the
2411** first Cell on root page. This is necessary because an insert
2412** or delete might change the number of cells on a page or delete
2413** a page entirely and we do not want to leave any cursors
2414** pointing to non-existant pages or cells.
2415*/
2416static int checkReadLocks(BtCursor *pCur){
2417 BtCursor *p;
2418 assert( pCur->wrFlag );
2419 for(p=pCur->pShared; p!=pCur; p=p->pShared){
2420 assert( p );
2421 assert( p->pgnoRoot==pCur->pgnoRoot );
2422 if( p->wrFlag==0 ) return SQLITE_LOCKED;
2423 if( sqlitepager_pagenumber(p->pPage)!=p->pgnoRoot ){
2424 moveToRoot(p);
2425 }
2426 }
2427 return SQLITE_OK;
2428}
2429
2430/*
drh3b7511c2001-05-26 13:15:44 +00002431** Insert a new record into the BTree. The key is given by (pKey,nKey)
2432** and the data is given by (pData,nData). The cursor is used only to
2433** define what database the record should be inserted into. The cursor
drh14acc042001-06-10 19:56:58 +00002434** is left pointing at the new record.
drh3b7511c2001-05-26 13:15:44 +00002435*/
2436int sqliteBtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00002437 BtCursor *pCur, /* Insert data into the table of this cursor */
drhbe0072d2001-09-13 14:46:09 +00002438 const void *pKey, int nKey, /* The key of the new record */
drh5c4d9702001-08-20 00:33:58 +00002439 const void *pData, int nData /* The data of the new record */
drh3b7511c2001-05-26 13:15:44 +00002440){
2441 Cell newCell;
2442 int rc;
2443 int loc;
drh14acc042001-06-10 19:56:58 +00002444 int szNew;
drh3b7511c2001-05-26 13:15:44 +00002445 MemPage *pPage;
2446 Btree *pBt = pCur->pBt;
2447
drhecdc7532001-09-23 02:35:53 +00002448 if( pCur->pPage==0 ){
2449 return SQLITE_ABORT; /* A rollback destroyed this cursor */
2450 }
drhf74b8d92002-09-01 23:20:45 +00002451 if( !pBt->inTrans || nKey+nData==0 ){
2452 /* Must start a transaction before doing an insert */
2453 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00002454 }
drhf74b8d92002-09-01 23:20:45 +00002455 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00002456 if( !pCur->wrFlag ){
2457 return SQLITE_PERM; /* Cursor not open for writing */
2458 }
drhf74b8d92002-09-01 23:20:45 +00002459 if( checkReadLocks(pCur) ){
2460 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
2461 }
drh14acc042001-06-10 19:56:58 +00002462 rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc);
drh3b7511c2001-05-26 13:15:44 +00002463 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002464 pPage = pCur->pPage;
drh7aa128d2002-06-21 13:09:16 +00002465 assert( pPage->isInit );
drh14acc042001-06-10 19:56:58 +00002466 rc = sqlitepager_write(pPage);
drhbd03cae2001-06-02 02:40:57 +00002467 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00002468 rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData);
2469 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002470 szNew = cellSize(pBt, &newCell);
drh3b7511c2001-05-26 13:15:44 +00002471 if( loc==0 ){
drh14acc042001-06-10 19:56:58 +00002472 newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild;
2473 rc = clearCell(pBt, pPage->apCell[pCur->idx]);
drh5e2f8b92001-05-28 00:41:15 +00002474 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002475 dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pPage->apCell[pCur->idx]));
drh7c717f72001-06-24 20:39:41 +00002476 }else if( loc<0 && pPage->nCell>0 ){
drh14acc042001-06-10 19:56:58 +00002477 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
2478 pCur->idx++;
2479 }else{
2480 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
drh3b7511c2001-05-26 13:15:44 +00002481 }
drh0d316a42002-08-11 20:10:47 +00002482 insertCell(pBt, pPage, pCur->idx, &newCell, szNew);
drh14acc042001-06-10 19:56:58 +00002483 rc = balance(pCur->pBt, pPage, pCur);
drh3fc190c2001-09-14 03:24:23 +00002484 /* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
2485 /* fflush(stdout); */
drh5e2f8b92001-05-28 00:41:15 +00002486 return rc;
2487}
2488
2489/*
drhbd03cae2001-06-02 02:40:57 +00002490** Delete the entry that the cursor is pointing to.
drh5e2f8b92001-05-28 00:41:15 +00002491**
drhbd03cae2001-06-02 02:40:57 +00002492** The cursor is left pointing at either the next or the previous
2493** entry. If the cursor is left pointing to the next entry, then
2494** the pCur->bSkipNext flag is set which forces the next call to
2495** sqliteBtreeNext() to be a no-op. That way, you can always call
2496** sqliteBtreeNext() after a delete and the cursor will be left
2497** pointing to the first entry after the deleted entry.
drh3b7511c2001-05-26 13:15:44 +00002498*/
2499int sqliteBtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00002500 MemPage *pPage = pCur->pPage;
2501 Cell *pCell;
2502 int rc;
drh8c42ca92001-06-22 19:15:00 +00002503 Pgno pgnoChild;
drh0d316a42002-08-11 20:10:47 +00002504 Btree *pBt = pCur->pBt;
drh8b2f49b2001-06-08 00:21:52 +00002505
drh7aa128d2002-06-21 13:09:16 +00002506 assert( pPage->isInit );
drhecdc7532001-09-23 02:35:53 +00002507 if( pCur->pPage==0 ){
2508 return SQLITE_ABORT; /* A rollback destroyed this cursor */
2509 }
drhf74b8d92002-09-01 23:20:45 +00002510 if( !pBt->inTrans ){
2511 /* Must start a transaction before doing a delete */
2512 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00002513 }
drhf74b8d92002-09-01 23:20:45 +00002514 assert( !pBt->readOnly );
drhbd03cae2001-06-02 02:40:57 +00002515 if( pCur->idx >= pPage->nCell ){
2516 return SQLITE_ERROR; /* The cursor is not pointing to anything */
2517 }
drhecdc7532001-09-23 02:35:53 +00002518 if( !pCur->wrFlag ){
2519 return SQLITE_PERM; /* Did not open this cursor for writing */
2520 }
drhf74b8d92002-09-01 23:20:45 +00002521 if( checkReadLocks(pCur) ){
2522 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
2523 }
drhbd03cae2001-06-02 02:40:57 +00002524 rc = sqlitepager_write(pPage);
2525 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00002526 pCell = pPage->apCell[pCur->idx];
drh0d316a42002-08-11 20:10:47 +00002527 pgnoChild = SWAB32(pBt, pCell->h.leftChild);
2528 clearCell(pBt, pCell);
drh14acc042001-06-10 19:56:58 +00002529 if( pgnoChild ){
2530 /*
drh5e00f6c2001-09-13 13:46:56 +00002531 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00002532 ** do something we will leave a hole on an internal page.
2533 ** We have to fill the hole by moving in a cell from a leaf. The
2534 ** next Cell after the one to be deleted is guaranteed to exist and
2535 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00002536 */
drh14acc042001-06-10 19:56:58 +00002537 BtCursor leafCur;
2538 Cell *pNext;
2539 int szNext;
2540 getTempCursor(pCur, &leafCur);
2541 rc = sqliteBtreeNext(&leafCur, 0);
2542 if( rc!=SQLITE_OK ){
2543 return SQLITE_CORRUPT;
drh5e2f8b92001-05-28 00:41:15 +00002544 }
drh6019e162001-07-02 17:51:45 +00002545 rc = sqlitepager_write(leafCur.pPage);
2546 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002547 dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
drh8c42ca92001-06-22 19:15:00 +00002548 pNext = leafCur.pPage->apCell[leafCur.idx];
drh0d316a42002-08-11 20:10:47 +00002549 szNext = cellSize(pBt, pNext);
2550 pNext->h.leftChild = SWAB32(pBt, pgnoChild);
2551 insertCell(pBt, pPage, pCur->idx, pNext, szNext);
2552 rc = balance(pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00002553 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00002554 pCur->bSkipNext = 1;
drh0d316a42002-08-11 20:10:47 +00002555 dropCell(pBt, leafCur.pPage, leafCur.idx, szNext);
2556 rc = balance(pBt, leafCur.pPage, pCur);
drh8c42ca92001-06-22 19:15:00 +00002557 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00002558 }else{
drh0d316a42002-08-11 20:10:47 +00002559 dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
drh5edc3122001-09-13 21:53:09 +00002560 if( pCur->idx>=pPage->nCell ){
2561 pCur->idx = pPage->nCell-1;
drhf5bf0a72001-11-23 00:24:12 +00002562 if( pCur->idx<0 ){
2563 pCur->idx = 0;
2564 pCur->bSkipNext = 1;
2565 }else{
2566 pCur->bSkipNext = 0;
2567 }
drh6019e162001-07-02 17:51:45 +00002568 }else{
2569 pCur->bSkipNext = 1;
2570 }
drh0d316a42002-08-11 20:10:47 +00002571 rc = balance(pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00002572 }
drh5e2f8b92001-05-28 00:41:15 +00002573 return rc;
drh3b7511c2001-05-26 13:15:44 +00002574}
drh8b2f49b2001-06-08 00:21:52 +00002575
2576/*
drhc6b52df2002-01-04 03:09:29 +00002577** Create a new BTree table. Write into *piTable the page
2578** number for the root page of the new table.
2579**
2580** In the current implementation, BTree tables and BTree indices are the
2581** the same. But in the future, we may change this so that BTree tables
2582** are restricted to having a 4-byte integer key and arbitrary data and
2583** BTree indices are restricted to having an arbitrary key and no data.
drh8b2f49b2001-06-08 00:21:52 +00002584*/
2585int sqliteBtreeCreateTable(Btree *pBt, int *piTable){
2586 MemPage *pRoot;
2587 Pgno pgnoRoot;
2588 int rc;
2589 if( !pBt->inTrans ){
drhf74b8d92002-09-01 23:20:45 +00002590 /* Must start a transaction first */
2591 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00002592 }
drh5df72a52002-06-06 23:16:05 +00002593 if( pBt->readOnly ){
2594 return SQLITE_READONLY;
2595 }
drhbea00b92002-07-08 10:59:50 +00002596 rc = allocatePage(pBt, &pRoot, &pgnoRoot, 0);
drh8b2f49b2001-06-08 00:21:52 +00002597 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00002598 assert( sqlitepager_iswriteable(pRoot) );
drh0d316a42002-08-11 20:10:47 +00002599 zeroPage(pBt, pRoot);
drh8b2f49b2001-06-08 00:21:52 +00002600 sqlitepager_unref(pRoot);
2601 *piTable = (int)pgnoRoot;
2602 return SQLITE_OK;
2603}
2604
2605/*
drhc6b52df2002-01-04 03:09:29 +00002606** Create a new BTree index. Write into *piTable the page
2607** number for the root page of the new index.
2608**
2609** In the current implementation, BTree tables and BTree indices are the
2610** the same. But in the future, we may change this so that BTree tables
2611** are restricted to having a 4-byte integer key and arbitrary data and
2612** BTree indices are restricted to having an arbitrary key and no data.
2613*/
2614int sqliteBtreeCreateIndex(Btree *pBt, int *piIndex){
drh5df72a52002-06-06 23:16:05 +00002615 return sqliteBtreeCreateTable(pBt, piIndex);
drhc6b52df2002-01-04 03:09:29 +00002616}
2617
2618/*
drh8b2f49b2001-06-08 00:21:52 +00002619** Erase the given database page and all its children. Return
2620** the page to the freelist.
2621*/
drh2aa679f2001-06-25 02:11:07 +00002622static int clearDatabasePage(Btree *pBt, Pgno pgno, int freePageFlag){
drh8b2f49b2001-06-08 00:21:52 +00002623 MemPage *pPage;
2624 int rc;
drh8b2f49b2001-06-08 00:21:52 +00002625 Cell *pCell;
2626 int idx;
2627
drh8c42ca92001-06-22 19:15:00 +00002628 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage);
drh8b2f49b2001-06-08 00:21:52 +00002629 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00002630 rc = sqlitepager_write(pPage);
2631 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002632 rc = initPage(pBt, pPage, pgno, 0);
drh7aa128d2002-06-21 13:09:16 +00002633 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002634 idx = SWAB16(pBt, pPage->u.hdr.firstCell);
drh8b2f49b2001-06-08 00:21:52 +00002635 while( idx>0 ){
drh14acc042001-06-10 19:56:58 +00002636 pCell = (Cell*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +00002637 idx = SWAB16(pBt, pCell->h.iNext);
drh8b2f49b2001-06-08 00:21:52 +00002638 if( pCell->h.leftChild ){
drh0d316a42002-08-11 20:10:47 +00002639 rc = clearDatabasePage(pBt, SWAB32(pBt, pCell->h.leftChild), 1);
drh8b2f49b2001-06-08 00:21:52 +00002640 if( rc ) return rc;
2641 }
drh8c42ca92001-06-22 19:15:00 +00002642 rc = clearCell(pBt, pCell);
drh8b2f49b2001-06-08 00:21:52 +00002643 if( rc ) return rc;
2644 }
drh2aa679f2001-06-25 02:11:07 +00002645 if( pPage->u.hdr.rightChild ){
drh0d316a42002-08-11 20:10:47 +00002646 rc = clearDatabasePage(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1);
drh2aa679f2001-06-25 02:11:07 +00002647 if( rc ) return rc;
2648 }
2649 if( freePageFlag ){
2650 rc = freePage(pBt, pPage, pgno);
2651 }else{
drh0d316a42002-08-11 20:10:47 +00002652 zeroPage(pBt, pPage);
drh2aa679f2001-06-25 02:11:07 +00002653 }
drhdd793422001-06-28 01:54:48 +00002654 sqlitepager_unref(pPage);
drh2aa679f2001-06-25 02:11:07 +00002655 return rc;
drh8b2f49b2001-06-08 00:21:52 +00002656}
2657
2658/*
2659** Delete all information from a single table in the database.
2660*/
2661int sqliteBtreeClearTable(Btree *pBt, int iTable){
2662 int rc;
drhf74b8d92002-09-01 23:20:45 +00002663 BtCursor *pCur;
drh8b2f49b2001-06-08 00:21:52 +00002664 if( !pBt->inTrans ){
drhf74b8d92002-09-01 23:20:45 +00002665 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00002666 }
drhf74b8d92002-09-01 23:20:45 +00002667 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
2668 if( pCur->pgnoRoot==(Pgno)iTable ){
2669 if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
2670 moveToRoot(pCur);
2671 }
drhecdc7532001-09-23 02:35:53 +00002672 }
drh2aa679f2001-06-25 02:11:07 +00002673 rc = clearDatabasePage(pBt, (Pgno)iTable, 0);
drh8b2f49b2001-06-08 00:21:52 +00002674 if( rc ){
2675 sqliteBtreeRollback(pBt);
drh8b2f49b2001-06-08 00:21:52 +00002676 }
drh8c42ca92001-06-22 19:15:00 +00002677 return rc;
drh8b2f49b2001-06-08 00:21:52 +00002678}
2679
2680/*
2681** Erase all information in a table and add the root of the table to
2682** the freelist. Except, the root of the principle table (the one on
2683** page 2) is never added to the freelist.
2684*/
2685int sqliteBtreeDropTable(Btree *pBt, int iTable){
2686 int rc;
2687 MemPage *pPage;
drhf74b8d92002-09-01 23:20:45 +00002688 BtCursor *pCur;
drh8b2f49b2001-06-08 00:21:52 +00002689 if( !pBt->inTrans ){
drhf74b8d92002-09-01 23:20:45 +00002690 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00002691 }
drhf74b8d92002-09-01 23:20:45 +00002692 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
2693 if( pCur->pgnoRoot==(Pgno)iTable ){
2694 return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */
2695 }
drh5df72a52002-06-06 23:16:05 +00002696 }
drh8c42ca92001-06-22 19:15:00 +00002697 rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
drh2aa679f2001-06-25 02:11:07 +00002698 if( rc ) return rc;
2699 rc = sqliteBtreeClearTable(pBt, iTable);
2700 if( rc ) return rc;
2701 if( iTable>2 ){
2702 rc = freePage(pBt, pPage, iTable);
2703 }else{
drh0d316a42002-08-11 20:10:47 +00002704 zeroPage(pBt, pPage);
drh8b2f49b2001-06-08 00:21:52 +00002705 }
drhdd793422001-06-28 01:54:48 +00002706 sqlitepager_unref(pPage);
drh8b2f49b2001-06-08 00:21:52 +00002707 return rc;
2708}
2709
2710/*
2711** Read the meta-information out of a database file.
2712*/
2713int sqliteBtreeGetMeta(Btree *pBt, int *aMeta){
2714 PageOne *pP1;
2715 int rc;
drh0d316a42002-08-11 20:10:47 +00002716 int i;
drh8b2f49b2001-06-08 00:21:52 +00002717
drh8c42ca92001-06-22 19:15:00 +00002718 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1);
drh8b2f49b2001-06-08 00:21:52 +00002719 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002720 aMeta[0] = SWAB32(pBt, pP1->nFree);
2721 for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
2722 aMeta[i+1] = SWAB32(pBt, pP1->aMeta[i]);
2723 }
drh8b2f49b2001-06-08 00:21:52 +00002724 sqlitepager_unref(pP1);
2725 return SQLITE_OK;
2726}
2727
2728/*
2729** Write meta-information back into the database.
2730*/
2731int sqliteBtreeUpdateMeta(Btree *pBt, int *aMeta){
2732 PageOne *pP1;
drh0d316a42002-08-11 20:10:47 +00002733 int rc, i;
drh8b2f49b2001-06-08 00:21:52 +00002734 if( !pBt->inTrans ){
drhf74b8d92002-09-01 23:20:45 +00002735 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh5df72a52002-06-06 23:16:05 +00002736 }
drh8b2f49b2001-06-08 00:21:52 +00002737 pP1 = pBt->page1;
2738 rc = sqlitepager_write(pP1);
drh9adf9ac2002-05-15 11:44:13 +00002739 if( rc ) return rc;
drh0d316a42002-08-11 20:10:47 +00002740 for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
2741 pP1->aMeta[i] = SWAB32(pBt, aMeta[i+1]);
2742 }
drh8b2f49b2001-06-08 00:21:52 +00002743 return SQLITE_OK;
2744}
drh8c42ca92001-06-22 19:15:00 +00002745
drh5eddca62001-06-30 21:53:53 +00002746/******************************************************************************
2747** The complete implementation of the BTree subsystem is above this line.
2748** All the code the follows is for testing and troubleshooting the BTree
2749** subsystem. None of the code that follows is used during normal operation.
drh5eddca62001-06-30 21:53:53 +00002750******************************************************************************/
drh5eddca62001-06-30 21:53:53 +00002751
drh8c42ca92001-06-22 19:15:00 +00002752/*
2753** Print a disassembly of the given page on standard output. This routine
2754** is used for debugging and testing only.
2755*/
drhaaab5722002-02-19 13:39:21 +00002756#ifdef SQLITE_TEST
drh6019e162001-07-02 17:51:45 +00002757int sqliteBtreePageDump(Btree *pBt, int pgno, int recursive){
drh8c42ca92001-06-22 19:15:00 +00002758 int rc;
2759 MemPage *pPage;
2760 int i, j;
2761 int nFree;
2762 u16 idx;
2763 char range[20];
2764 unsigned char payload[20];
2765 rc = sqlitepager_get(pBt->pPager, (Pgno)pgno, (void**)&pPage);
2766 if( rc ){
2767 return rc;
2768 }
drh6019e162001-07-02 17:51:45 +00002769 if( recursive ) printf("PAGE %d:\n", pgno);
drh8c42ca92001-06-22 19:15:00 +00002770 i = 0;
drh0d316a42002-08-11 20:10:47 +00002771 idx = SWAB16(pBt, pPage->u.hdr.firstCell);
drh8c42ca92001-06-22 19:15:00 +00002772 while( idx>0 && idx<=SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){
2773 Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +00002774 int sz = cellSize(pBt, pCell);
drh8c42ca92001-06-22 19:15:00 +00002775 sprintf(range,"%d..%d", idx, idx+sz-1);
drh0d316a42002-08-11 20:10:47 +00002776 sz = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h);
drh8c42ca92001-06-22 19:15:00 +00002777 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
2778 memcpy(payload, pCell->aPayload, sz);
2779 for(j=0; j<sz; j++){
2780 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
2781 }
2782 payload[sz] = 0;
2783 printf(
drh6019e162001-07-02 17:51:45 +00002784 "cell %2d: i=%-10s chld=%-4d nk=%-4d nd=%-4d payload=%s\n",
drh0d316a42002-08-11 20:10:47 +00002785 i, range, (int)pCell->h.leftChild,
2786 NKEY(pBt, pCell->h), NDATA(pBt, pCell->h),
drh2aa679f2001-06-25 02:11:07 +00002787 payload
drh8c42ca92001-06-22 19:15:00 +00002788 );
drh6019e162001-07-02 17:51:45 +00002789 if( pPage->isInit && pPage->apCell[i]!=pCell ){
drh2aa679f2001-06-25 02:11:07 +00002790 printf("**** apCell[%d] does not match on prior entry ****\n", i);
2791 }
drh7c717f72001-06-24 20:39:41 +00002792 i++;
drh0d316a42002-08-11 20:10:47 +00002793 idx = SWAB16(pBt, pCell->h.iNext);
drh8c42ca92001-06-22 19:15:00 +00002794 }
2795 if( idx!=0 ){
2796 printf("ERROR: next cell index out of range: %d\n", idx);
2797 }
drh0d316a42002-08-11 20:10:47 +00002798 printf("right_child: %d\n", SWAB32(pBt, pPage->u.hdr.rightChild));
drh8c42ca92001-06-22 19:15:00 +00002799 nFree = 0;
2800 i = 0;
drh0d316a42002-08-11 20:10:47 +00002801 idx = SWAB16(pBt, pPage->u.hdr.firstFree);
drh8c42ca92001-06-22 19:15:00 +00002802 while( idx>0 && idx<SQLITE_PAGE_SIZE ){
2803 FreeBlk *p = (FreeBlk*)&pPage->u.aDisk[idx];
2804 sprintf(range,"%d..%d", idx, idx+p->iSize-1);
drh0d316a42002-08-11 20:10:47 +00002805 nFree += SWAB16(pBt, p->iSize);
drh8c42ca92001-06-22 19:15:00 +00002806 printf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
drh0d316a42002-08-11 20:10:47 +00002807 i, range, SWAB16(pBt, p->iSize), nFree);
2808 idx = SWAB16(pBt, p->iNext);
drh2aa679f2001-06-25 02:11:07 +00002809 i++;
drh8c42ca92001-06-22 19:15:00 +00002810 }
2811 if( idx!=0 ){
2812 printf("ERROR: next freeblock index out of range: %d\n", idx);
2813 }
drh6019e162001-07-02 17:51:45 +00002814 if( recursive && pPage->u.hdr.rightChild!=0 ){
drh0d316a42002-08-11 20:10:47 +00002815 idx = SWAB16(pBt, pPage->u.hdr.firstCell);
drh6019e162001-07-02 17:51:45 +00002816 while( idx>0 && idx<SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){
2817 Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
drh0d316a42002-08-11 20:10:47 +00002818 sqliteBtreePageDump(pBt, SWAB32(pBt, pCell->h.leftChild), 1);
2819 idx = SWAB16(pBt, pCell->h.iNext);
drh6019e162001-07-02 17:51:45 +00002820 }
drh0d316a42002-08-11 20:10:47 +00002821 sqliteBtreePageDump(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1);
drh6019e162001-07-02 17:51:45 +00002822 }
drh8c42ca92001-06-22 19:15:00 +00002823 sqlitepager_unref(pPage);
2824 return SQLITE_OK;
2825}
drhaaab5722002-02-19 13:39:21 +00002826#endif
drh8c42ca92001-06-22 19:15:00 +00002827
drhaaab5722002-02-19 13:39:21 +00002828#ifdef SQLITE_TEST
drh8c42ca92001-06-22 19:15:00 +00002829/*
drh2aa679f2001-06-25 02:11:07 +00002830** Fill aResult[] with information about the entry and page that the
2831** cursor is pointing to.
2832**
2833** aResult[0] = The page number
2834** aResult[1] = The entry number
2835** aResult[2] = Total number of entries on this page
2836** aResult[3] = Size of this entry
2837** aResult[4] = Number of free bytes on this page
2838** aResult[5] = Number of free blocks on the page
2839** aResult[6] = Page number of the left child of this entry
2840** aResult[7] = Page number of the right child for the whole page
drh5eddca62001-06-30 21:53:53 +00002841**
2842** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00002843*/
2844int sqliteBtreeCursorDump(BtCursor *pCur, int *aResult){
drh2aa679f2001-06-25 02:11:07 +00002845 int cnt, idx;
2846 MemPage *pPage = pCur->pPage;
drh0d316a42002-08-11 20:10:47 +00002847 Btree *pBt = pCur->pBt;
drh2aa679f2001-06-25 02:11:07 +00002848 aResult[0] = sqlitepager_pagenumber(pPage);
drh8c42ca92001-06-22 19:15:00 +00002849 aResult[1] = pCur->idx;
drh2aa679f2001-06-25 02:11:07 +00002850 aResult[2] = pPage->nCell;
2851 if( pCur->idx>=0 && pCur->idx<pPage->nCell ){
drh0d316a42002-08-11 20:10:47 +00002852 aResult[3] = cellSize(pBt, pPage->apCell[pCur->idx]);
2853 aResult[6] = SWAB32(pBt, pPage->apCell[pCur->idx]->h.leftChild);
drh2aa679f2001-06-25 02:11:07 +00002854 }else{
2855 aResult[3] = 0;
2856 aResult[6] = 0;
2857 }
2858 aResult[4] = pPage->nFree;
2859 cnt = 0;
drh0d316a42002-08-11 20:10:47 +00002860 idx = SWAB16(pBt, pPage->u.hdr.firstFree);
drh2aa679f2001-06-25 02:11:07 +00002861 while( idx>0 && idx<SQLITE_PAGE_SIZE ){
2862 cnt++;
drh0d316a42002-08-11 20:10:47 +00002863 idx = SWAB16(pBt, ((FreeBlk*)&pPage->u.aDisk[idx])->iNext);
drh2aa679f2001-06-25 02:11:07 +00002864 }
2865 aResult[5] = cnt;
drh0d316a42002-08-11 20:10:47 +00002866 aResult[7] = SWAB32(pBt, pPage->u.hdr.rightChild);
drh8c42ca92001-06-22 19:15:00 +00002867 return SQLITE_OK;
2868}
drhaaab5722002-02-19 13:39:21 +00002869#endif
drhdd793422001-06-28 01:54:48 +00002870
drhaaab5722002-02-19 13:39:21 +00002871#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002872/*
drh5eddca62001-06-30 21:53:53 +00002873** Return the pager associated with a BTree. This routine is used for
2874** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00002875*/
2876Pager *sqliteBtreePager(Btree *pBt){
2877 return pBt->pPager;
2878}
drhaaab5722002-02-19 13:39:21 +00002879#endif
drh5eddca62001-06-30 21:53:53 +00002880
2881/*
2882** This structure is passed around through all the sanity checking routines
2883** in order to keep track of some global state information.
2884*/
drhaaab5722002-02-19 13:39:21 +00002885typedef struct IntegrityCk IntegrityCk;
2886struct IntegrityCk {
drh100569d2001-10-02 13:01:48 +00002887 Btree *pBt; /* The tree being checked out */
2888 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
2889 int nPage; /* Number of pages in the database */
2890 int *anRef; /* Number of times each page is referenced */
2891 int nTreePage; /* Number of BTree pages */
2892 int nByte; /* Number of bytes of data stored on BTree pages */
2893 char *zErrMsg; /* An error message. NULL of no errors seen. */
drh5eddca62001-06-30 21:53:53 +00002894};
2895
2896/*
2897** Append a message to the error message string.
2898*/
drhaaab5722002-02-19 13:39:21 +00002899static void checkAppendMsg(IntegrityCk *pCheck, char *zMsg1, char *zMsg2){
drh5eddca62001-06-30 21:53:53 +00002900 if( pCheck->zErrMsg ){
2901 char *zOld = pCheck->zErrMsg;
2902 pCheck->zErrMsg = 0;
2903 sqliteSetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, 0);
2904 sqliteFree(zOld);
2905 }else{
2906 sqliteSetString(&pCheck->zErrMsg, zMsg1, zMsg2, 0);
2907 }
2908}
2909
2910/*
2911** Add 1 to the reference count for page iPage. If this is the second
2912** reference to the page, add an error message to pCheck->zErrMsg.
2913** Return 1 if there are 2 ore more references to the page and 0 if
2914** if this is the first reference to the page.
2915**
2916** Also check that the page number is in bounds.
2917*/
drhaaab5722002-02-19 13:39:21 +00002918static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00002919 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00002920 if( iPage>pCheck->nPage || iPage<0 ){
drh5eddca62001-06-30 21:53:53 +00002921 char zBuf[100];
2922 sprintf(zBuf, "invalid page number %d", iPage);
2923 checkAppendMsg(pCheck, zContext, zBuf);
2924 return 1;
2925 }
2926 if( pCheck->anRef[iPage]==1 ){
2927 char zBuf[100];
2928 sprintf(zBuf, "2nd reference to page %d", iPage);
2929 checkAppendMsg(pCheck, zContext, zBuf);
2930 return 1;
2931 }
2932 return (pCheck->anRef[iPage]++)>1;
2933}
2934
2935/*
2936** Check the integrity of the freelist or of an overflow page list.
2937** Verify that the number of pages on the list is N.
2938*/
drh30e58752002-03-02 20:41:57 +00002939static void checkList(
2940 IntegrityCk *pCheck, /* Integrity checking context */
2941 int isFreeList, /* True for a freelist. False for overflow page list */
2942 int iPage, /* Page number for first page in the list */
2943 int N, /* Expected number of pages in the list */
2944 char *zContext /* Context for error messages */
2945){
2946 int i;
drh5eddca62001-06-30 21:53:53 +00002947 char zMsg[100];
drh30e58752002-03-02 20:41:57 +00002948 while( N-- > 0 ){
drh5eddca62001-06-30 21:53:53 +00002949 OverflowPage *pOvfl;
2950 if( iPage<1 ){
2951 sprintf(zMsg, "%d pages missing from overflow list", N+1);
2952 checkAppendMsg(pCheck, zContext, zMsg);
2953 break;
2954 }
2955 if( checkRef(pCheck, iPage, zContext) ) break;
2956 if( sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
2957 sprintf(zMsg, "failed to get page %d", iPage);
2958 checkAppendMsg(pCheck, zContext, zMsg);
2959 break;
2960 }
drh30e58752002-03-02 20:41:57 +00002961 if( isFreeList ){
2962 FreelistInfo *pInfo = (FreelistInfo*)pOvfl->aPayload;
drh0d316a42002-08-11 20:10:47 +00002963 int n = SWAB32(pCheck->pBt, pInfo->nFree);
2964 for(i=0; i<n; i++){
2965 checkRef(pCheck, SWAB32(pCheck->pBt, pInfo->aFree[i]), zMsg);
drh30e58752002-03-02 20:41:57 +00002966 }
drh0d316a42002-08-11 20:10:47 +00002967 N -= n;
drh30e58752002-03-02 20:41:57 +00002968 }
drh0d316a42002-08-11 20:10:47 +00002969 iPage = SWAB32(pCheck->pBt, pOvfl->iNext);
drh5eddca62001-06-30 21:53:53 +00002970 sqlitepager_unref(pOvfl);
2971 }
2972}
2973
2974/*
drh1bffb9c2002-02-03 17:37:36 +00002975** Return negative if zKey1<zKey2.
2976** Return zero if zKey1==zKey2.
2977** Return positive if zKey1>zKey2.
2978*/
2979static int keyCompare(
2980 const char *zKey1, int nKey1,
2981 const char *zKey2, int nKey2
2982){
2983 int min = nKey1>nKey2 ? nKey2 : nKey1;
2984 int c = memcmp(zKey1, zKey2, min);
2985 if( c==0 ){
2986 c = nKey1 - nKey2;
2987 }
2988 return c;
2989}
2990
2991/*
drh5eddca62001-06-30 21:53:53 +00002992** Do various sanity checks on a single page of a tree. Return
2993** the tree depth. Root pages return 0. Parents of root pages
2994** return 1, and so forth.
2995**
2996** These checks are done:
2997**
2998** 1. Make sure that cells and freeblocks do not overlap
2999** but combine to completely cover the page.
3000** 2. Make sure cell keys are in order.
3001** 3. Make sure no key is less than or equal to zLowerBound.
3002** 4. Make sure no key is greater than or equal to zUpperBound.
3003** 5. Check the integrity of overflow pages.
3004** 6. Recursively call checkTreePage on all children.
3005** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00003006** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00003007** the root of the tree.
3008*/
3009static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00003010 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00003011 int iPage, /* Page number of the page to check */
3012 MemPage *pParent, /* Parent page */
3013 char *zParentContext, /* Parent context */
3014 char *zLowerBound, /* All keys should be greater than this, if not NULL */
drh1bffb9c2002-02-03 17:37:36 +00003015 int nLower, /* Number of characters in zLowerBound */
3016 char *zUpperBound, /* All keys should be less than this, if not NULL */
3017 int nUpper /* Number of characters in zUpperBound */
drh5eddca62001-06-30 21:53:53 +00003018){
3019 MemPage *pPage;
3020 int i, rc, depth, d2, pgno;
3021 char *zKey1, *zKey2;
drh1bffb9c2002-02-03 17:37:36 +00003022 int nKey1, nKey2;
drh5eddca62001-06-30 21:53:53 +00003023 BtCursor cur;
drh0d316a42002-08-11 20:10:47 +00003024 Btree *pBt;
drh5eddca62001-06-30 21:53:53 +00003025 char zMsg[100];
3026 char zContext[100];
3027 char hit[SQLITE_PAGE_SIZE];
3028
3029 /* Check that the page exists
3030 */
drh0d316a42002-08-11 20:10:47 +00003031 cur.pBt = pBt = pCheck->pBt;
drh5eddca62001-06-30 21:53:53 +00003032 if( iPage==0 ) return 0;
3033 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
3034 sprintf(zContext, "On tree page %d: ", iPage);
3035 if( (rc = sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pPage))!=0 ){
3036 sprintf(zMsg, "unable to get the page. error code=%d", rc);
3037 checkAppendMsg(pCheck, zContext, zMsg);
3038 return 0;
3039 }
drh0d316a42002-08-11 20:10:47 +00003040 if( (rc = initPage(pBt, pPage, (Pgno)iPage, pParent))!=0 ){
drh5eddca62001-06-30 21:53:53 +00003041 sprintf(zMsg, "initPage() returns error code %d", rc);
3042 checkAppendMsg(pCheck, zContext, zMsg);
3043 sqlitepager_unref(pPage);
3044 return 0;
3045 }
3046
3047 /* Check out all the cells.
3048 */
3049 depth = 0;
drh1bffb9c2002-02-03 17:37:36 +00003050 if( zLowerBound ){
3051 zKey1 = sqliteMalloc( nLower+1 );
3052 memcpy(zKey1, zLowerBound, nLower);
3053 zKey1[nLower] = 0;
3054 }else{
3055 zKey1 = 0;
3056 }
3057 nKey1 = nLower;
drh5eddca62001-06-30 21:53:53 +00003058 cur.pPage = pPage;
drh5eddca62001-06-30 21:53:53 +00003059 for(i=0; i<pPage->nCell; i++){
3060 Cell *pCell = pPage->apCell[i];
3061 int sz;
3062
3063 /* Check payload overflow pages
3064 */
drh0d316a42002-08-11 20:10:47 +00003065 nKey2 = NKEY(pBt, pCell->h);
3066 sz = nKey2 + NDATA(pBt, pCell->h);
drh5eddca62001-06-30 21:53:53 +00003067 sprintf(zContext, "On page %d cell %d: ", iPage, i);
3068 if( sz>MX_LOCAL_PAYLOAD ){
3069 int nPage = (sz - MX_LOCAL_PAYLOAD + OVERFLOW_SIZE - 1)/OVERFLOW_SIZE;
drh0d316a42002-08-11 20:10:47 +00003070 checkList(pCheck, 0, SWAB32(pBt, pCell->ovfl), nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00003071 }
3072
3073 /* Check that keys are in the right order
3074 */
3075 cur.idx = i;
drh1bffb9c2002-02-03 17:37:36 +00003076 zKey2 = sqliteMalloc( nKey2+1 );
3077 getPayload(&cur, 0, nKey2, zKey2);
3078 if( zKey1 && keyCompare(zKey1, nKey1, zKey2, nKey2)>=0 ){
drh5eddca62001-06-30 21:53:53 +00003079 checkAppendMsg(pCheck, zContext, "Key is out of order");
3080 }
3081
3082 /* Check sanity of left child page.
3083 */
drh0d316a42002-08-11 20:10:47 +00003084 pgno = SWAB32(pBt, pCell->h.leftChild);
drh1bffb9c2002-02-03 17:37:36 +00003085 d2 = checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zKey2,nKey2);
drh5eddca62001-06-30 21:53:53 +00003086 if( i>0 && d2!=depth ){
3087 checkAppendMsg(pCheck, zContext, "Child page depth differs");
3088 }
3089 depth = d2;
3090 sqliteFree(zKey1);
3091 zKey1 = zKey2;
drh1bffb9c2002-02-03 17:37:36 +00003092 nKey1 = nKey2;
drh5eddca62001-06-30 21:53:53 +00003093 }
drh0d316a42002-08-11 20:10:47 +00003094 pgno = SWAB32(pBt, pPage->u.hdr.rightChild);
drh5eddca62001-06-30 21:53:53 +00003095 sprintf(zContext, "On page %d at right child: ", iPage);
drh1bffb9c2002-02-03 17:37:36 +00003096 checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zUpperBound,nUpper);
drh5eddca62001-06-30 21:53:53 +00003097 sqliteFree(zKey1);
3098
3099 /* Check for complete coverage of the page
3100 */
3101 memset(hit, 0, sizeof(hit));
3102 memset(hit, 1, sizeof(PageHdr));
drh0d316a42002-08-11 20:10:47 +00003103 for(i=SWAB16(pBt, pPage->u.hdr.firstCell); i>0 && i<SQLITE_PAGE_SIZE; ){
drh5eddca62001-06-30 21:53:53 +00003104 Cell *pCell = (Cell*)&pPage->u.aDisk[i];
3105 int j;
drh0d316a42002-08-11 20:10:47 +00003106 for(j=i+cellSize(pBt, pCell)-1; j>=i; j--) hit[j]++;
3107 i = SWAB16(pBt, pCell->h.iNext);
drh5eddca62001-06-30 21:53:53 +00003108 }
drh0d316a42002-08-11 20:10:47 +00003109 for(i=SWAB16(pBt,pPage->u.hdr.firstFree); i>0 && i<SQLITE_PAGE_SIZE; ){
drh5eddca62001-06-30 21:53:53 +00003110 FreeBlk *pFBlk = (FreeBlk*)&pPage->u.aDisk[i];
3111 int j;
drh0d316a42002-08-11 20:10:47 +00003112 for(j=i+SWAB16(pBt,pFBlk->iSize)-1; j>=i; j--) hit[j]++;
3113 i = SWAB16(pBt,pFBlk->iNext);
drh5eddca62001-06-30 21:53:53 +00003114 }
3115 for(i=0; i<SQLITE_PAGE_SIZE; i++){
3116 if( hit[i]==0 ){
3117 sprintf(zMsg, "Unused space at byte %d of page %d", i, iPage);
3118 checkAppendMsg(pCheck, zMsg, 0);
3119 break;
3120 }else if( hit[i]>1 ){
3121 sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage);
3122 checkAppendMsg(pCheck, zMsg, 0);
3123 break;
3124 }
3125 }
3126
3127 /* Check that free space is kept to a minimum
3128 */
drh6019e162001-07-02 17:51:45 +00003129#if 0
3130 if( pParent && pParent->nCell>2 && pPage->nFree>3*SQLITE_PAGE_SIZE/4 ){
drh5eddca62001-06-30 21:53:53 +00003131 sprintf(zMsg, "free space (%d) greater than max (%d)", pPage->nFree,
3132 SQLITE_PAGE_SIZE/3);
3133 checkAppendMsg(pCheck, zContext, zMsg);
3134 }
drh6019e162001-07-02 17:51:45 +00003135#endif
3136
3137 /* Update freespace totals.
3138 */
3139 pCheck->nTreePage++;
3140 pCheck->nByte += USABLE_SPACE - pPage->nFree;
drh5eddca62001-06-30 21:53:53 +00003141
3142 sqlitepager_unref(pPage);
3143 return depth;
3144}
3145
3146/*
3147** This routine does a complete check of the given BTree file. aRoot[] is
3148** an array of pages numbers were each page number is the root page of
3149** a table. nRoot is the number of entries in aRoot.
3150**
3151** If everything checks out, this routine returns NULL. If something is
3152** amiss, an error message is written into memory obtained from malloc()
3153** and a pointer to that error message is returned. The calling function
3154** is responsible for freeing the error message when it is done.
3155*/
drhaaab5722002-02-19 13:39:21 +00003156char *sqliteBtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){
drh5eddca62001-06-30 21:53:53 +00003157 int i;
3158 int nRef;
drhaaab5722002-02-19 13:39:21 +00003159 IntegrityCk sCheck;
drh5eddca62001-06-30 21:53:53 +00003160
3161 nRef = *sqlitepager_stats(pBt->pPager);
drhefc251d2001-07-01 22:12:01 +00003162 if( lockBtree(pBt)!=SQLITE_OK ){
3163 return sqliteStrDup("Unable to acquire a read lock on the database");
3164 }
drh5eddca62001-06-30 21:53:53 +00003165 sCheck.pBt = pBt;
3166 sCheck.pPager = pBt->pPager;
3167 sCheck.nPage = sqlitepager_pagecount(sCheck.pPager);
drh0de8c112002-07-06 16:32:14 +00003168 if( sCheck.nPage==0 ){
3169 unlockBtreeIfUnused(pBt);
3170 return 0;
3171 }
drh5eddca62001-06-30 21:53:53 +00003172 sCheck.anRef = sqliteMalloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
3173 sCheck.anRef[1] = 1;
3174 for(i=2; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
3175 sCheck.zErrMsg = 0;
3176
3177 /* Check the integrity of the freelist
3178 */
drh0d316a42002-08-11 20:10:47 +00003179 checkList(&sCheck, 1, SWAB32(pBt, pBt->page1->freeList),
3180 SWAB32(pBt, pBt->page1->nFree), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00003181
3182 /* Check all the tables.
3183 */
3184 for(i=0; i<nRoot; i++){
drh4ff6dfa2002-03-03 23:06:00 +00003185 if( aRoot[i]==0 ) continue;
drh1bffb9c2002-02-03 17:37:36 +00003186 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0);
drh5eddca62001-06-30 21:53:53 +00003187 }
3188
3189 /* Make sure every page in the file is referenced
3190 */
3191 for(i=1; i<=sCheck.nPage; i++){
3192 if( sCheck.anRef[i]==0 ){
3193 char zBuf[100];
3194 sprintf(zBuf, "Page %d is never used", i);
3195 checkAppendMsg(&sCheck, zBuf, 0);
3196 }
3197 }
3198
3199 /* Make sure this analysis did not leave any unref() pages
3200 */
drh5e00f6c2001-09-13 13:46:56 +00003201 unlockBtreeIfUnused(pBt);
drh5eddca62001-06-30 21:53:53 +00003202 if( nRef != *sqlitepager_stats(pBt->pPager) ){
3203 char zBuf[100];
3204 sprintf(zBuf,
3205 "Outstanding page count goes from %d to %d during this analysis",
3206 nRef, *sqlitepager_stats(pBt->pPager)
3207 );
3208 checkAppendMsg(&sCheck, zBuf, 0);
3209 }
3210
3211 /* Clean up and report errors.
3212 */
3213 sqliteFree(sCheck.anRef);
3214 return sCheck.zErrMsg;
3215}