blob: 2028c896ecd2f649648b8654c6dea50e420e2f4c [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*************************************************************************
drha7fcb052001-12-14 15:09:55 +000012** $Id: btree.c,v 1.43 2001/12/14 15:09:57 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/*
drh365d68f2001-05-11 11:02:46 +000058** Forward declarations of structures used only in this file.
59*/
drhbd03cae2001-06-02 02:40:57 +000060typedef struct PageOne PageOne;
drh2af926b2001-05-15 00:39:25 +000061typedef struct MemPage MemPage;
drh365d68f2001-05-11 11:02:46 +000062typedef struct PageHdr PageHdr;
63typedef struct Cell Cell;
drh3b7511c2001-05-26 13:15:44 +000064typedef struct CellHdr CellHdr;
drh365d68f2001-05-11 11:02:46 +000065typedef struct FreeBlk FreeBlk;
drh2af926b2001-05-15 00:39:25 +000066typedef struct OverflowPage OverflowPage;
67
68/*
69** All structures on a database page are aligned to 4-byte boundries.
70** This routine rounds up a number of bytes to the next multiple of 4.
drh306dc212001-05-21 13:45:10 +000071**
72** This might need to change for computer architectures that require
73** and 8-byte alignment boundry for structures.
drh2af926b2001-05-15 00:39:25 +000074*/
75#define ROUNDUP(X) ((X+3) & ~3)
drha059ad02001-04-17 20:09:11 +000076
drh08ed44e2001-04-29 23:32:55 +000077/*
drhbd03cae2001-06-02 02:40:57 +000078** This is a magic string that appears at the beginning of every
drh8c42ca92001-06-22 19:15:00 +000079** SQLite database in order to identify the file as a real database.
drh08ed44e2001-04-29 23:32:55 +000080*/
drhbd03cae2001-06-02 02:40:57 +000081static const char zMagicHeader[] =
drh80ff32f2001-11-04 18:32:46 +000082 "** This file contains an SQLite 2.1 database **";
drhbd03cae2001-06-02 02:40:57 +000083#define MAGIC_SIZE (sizeof(zMagicHeader))
drh08ed44e2001-04-29 23:32:55 +000084
85/*
drh5e00f6c2001-09-13 13:46:56 +000086** This is a magic integer also used to test the integrity of the database
drh8c42ca92001-06-22 19:15:00 +000087** file. This integer is used in addition to the string above so that
88** if the file is written on a little-endian architecture and read
89** on a big-endian architectures (or vice versa) we can detect the
90** problem.
91**
92** The number used was obtained at random and has no special
drhb19a2bc2001-09-16 00:13:26 +000093** significance other than the fact that it represents a different
94** integer on little-endian and big-endian machines.
drh8c42ca92001-06-22 19:15:00 +000095*/
96#define MAGIC 0xdae37528
97
98/*
drhbd03cae2001-06-02 02:40:57 +000099** The first page of the database file contains a magic header string
100** to identify the file as an SQLite database file. It also contains
101** a pointer to the first free page of the file. Page 2 contains the
drh8b2f49b2001-06-08 00:21:52 +0000102** root of the principle BTree. The file might contain other BTrees
103** rooted on pages above 2.
104**
105** The first page also contains SQLITE_N_BTREE_META integers that
106** can be used by higher-level routines.
drh08ed44e2001-04-29 23:32:55 +0000107**
drhbd03cae2001-06-02 02:40:57 +0000108** Remember that pages are numbered beginning with 1. (See pager.c
109** for additional information.) Page 0 does not exist and a page
110** number of 0 is used to mean "no such page".
111*/
112struct PageOne {
113 char zMagic[MAGIC_SIZE]; /* String that identifies the file as a database */
drh8c42ca92001-06-22 19:15:00 +0000114 int iMagic; /* Integer to verify correct byte order */
115 Pgno freeList; /* First free page in a list of all free pages */
drh2aa679f2001-06-25 02:11:07 +0000116 int nFree; /* Number of pages on the free list */
117 int aMeta[SQLITE_N_BTREE_META-1]; /* User defined integers */
drhbd03cae2001-06-02 02:40:57 +0000118};
119
120/*
121** Each database page has a header that is an instance of this
122** structure.
drh08ed44e2001-04-29 23:32:55 +0000123**
drh8b2f49b2001-06-08 00:21:52 +0000124** PageHdr.firstFree is 0 if there is no free space on this page.
drh14acc042001-06-10 19:56:58 +0000125** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a
drh8b2f49b2001-06-08 00:21:52 +0000126** FreeBlk structure that describes the first block of free space.
127** All free space is defined by a linked list of FreeBlk structures.
drh08ed44e2001-04-29 23:32:55 +0000128**
drh8b2f49b2001-06-08 00:21:52 +0000129** Data is stored in a linked list of Cell structures. PageHdr.firstCell
drh14acc042001-06-10 19:56:58 +0000130** is the index into MemPage.u.aDisk[] of the first cell on the page. The
drh306dc212001-05-21 13:45:10 +0000131** Cells are kept in sorted order.
drh8b2f49b2001-06-08 00:21:52 +0000132**
133** A Cell contains all information about a database entry and a pointer
134** to a child page that contains other entries less than itself. In
135** other words, the i-th Cell contains both Ptr(i) and Key(i). The
136** right-most pointer of the page is contained in PageHdr.rightChild.
drh08ed44e2001-04-29 23:32:55 +0000137*/
drh365d68f2001-05-11 11:02:46 +0000138struct PageHdr {
drh5e2f8b92001-05-28 00:41:15 +0000139 Pgno rightChild; /* Child page that comes after all cells on this page */
drh14acc042001-06-10 19:56:58 +0000140 u16 firstCell; /* Index in MemPage.u.aDisk[] of the first cell */
141 u16 firstFree; /* Index in MemPage.u.aDisk[] of the first free block */
drh365d68f2001-05-11 11:02:46 +0000142};
drh306dc212001-05-21 13:45:10 +0000143
drh3b7511c2001-05-26 13:15:44 +0000144/*
145** Entries on a page of the database are called "Cells". Each Cell
146** has a header and data. This structure defines the header. The
drhbd03cae2001-06-02 02:40:57 +0000147** key and data (collectively the "payload") follow this header on
148** the database page.
149**
150** A definition of the complete Cell structure is given below. The
drh8c42ca92001-06-22 19:15:00 +0000151** header for the cell must be defined first in order to do some
drhbd03cae2001-06-02 02:40:57 +0000152** of the sizing #defines that follow.
drh3b7511c2001-05-26 13:15:44 +0000153*/
154struct CellHdr {
drh5e2f8b92001-05-28 00:41:15 +0000155 Pgno leftChild; /* Child page that comes before this cell */
drh3b7511c2001-05-26 13:15:44 +0000156 u16 nKey; /* Number of bytes in the key */
drh14acc042001-06-10 19:56:58 +0000157 u16 iNext; /* Index in MemPage.u.aDisk[] of next cell in sorted order */
drh58a11682001-11-10 13:51:08 +0000158 u8 nKeyHi; /* Upper 8 bits of key size for keys larger than 64K bytes */
159 u8 nDataHi; /* Upper 8 bits of data size when the size is more than 64K */
drh80ff32f2001-11-04 18:32:46 +0000160 u16 nData; /* Number of bytes of data */
drh8c42ca92001-06-22 19:15:00 +0000161};
drh58a11682001-11-10 13:51:08 +0000162
163/*
164** The key and data size are split into a lower 16-bit segment and an
165** upper 8-bit segment in order to pack them together into a smaller
166** space. The following macros reassembly a key or data size back
167** into an integer.
168*/
drh80ff32f2001-11-04 18:32:46 +0000169#define NKEY(h) (h.nKey + h.nKeyHi*65536)
170#define NDATA(h) (h.nData + h.nDataHi*65536)
drh3b7511c2001-05-26 13:15:44 +0000171
172/*
173** The minimum size of a complete Cell. The Cell must contain a header
drhbd03cae2001-06-02 02:40:57 +0000174** and at least 4 bytes of payload.
drh3b7511c2001-05-26 13:15:44 +0000175*/
176#define MIN_CELL_SIZE (sizeof(CellHdr)+4)
177
178/*
179** The maximum number of database entries that can be held in a single
180** page of the database.
181*/
182#define MX_CELL ((SQLITE_PAGE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)
183
184/*
drh6019e162001-07-02 17:51:45 +0000185** The amount of usable space on a single page of the BTree. This is the
186** page size minus the overhead of the page header.
187*/
188#define USABLE_SPACE (SQLITE_PAGE_SIZE - sizeof(PageHdr))
189
190/*
drh8c42ca92001-06-22 19:15:00 +0000191** The maximum amount of payload (in bytes) that can be stored locally for
192** a database entry. If the entry contains more data than this, the
drh3b7511c2001-05-26 13:15:44 +0000193** extra goes onto overflow pages.
drhbd03cae2001-06-02 02:40:57 +0000194**
195** This number is chosen so that at least 4 cells will fit on every page.
drh3b7511c2001-05-26 13:15:44 +0000196*/
drh6019e162001-07-02 17:51:45 +0000197#define MX_LOCAL_PAYLOAD ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3)
drh3b7511c2001-05-26 13:15:44 +0000198
drh306dc212001-05-21 13:45:10 +0000199/*
200** Data on a database page is stored as a linked list of Cell structures.
drh5e2f8b92001-05-28 00:41:15 +0000201** Both the key and the data are stored in aPayload[]. The key always comes
202** first. The aPayload[] field grows as necessary to hold the key and data,
drh306dc212001-05-21 13:45:10 +0000203** up to a maximum of MX_LOCAL_PAYLOAD bytes. If the size of the key and
drh3b7511c2001-05-26 13:15:44 +0000204** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the
205** page number of the first overflow page.
206**
207** Though this structure is fixed in size, the Cell on the database
drhbd03cae2001-06-02 02:40:57 +0000208** page varies in size. Every cell has a CellHdr and at least 4 bytes
drh3b7511c2001-05-26 13:15:44 +0000209** of payload space. Additional payload bytes (up to the maximum of
210** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as
211** needed.
drh306dc212001-05-21 13:45:10 +0000212*/
drh365d68f2001-05-11 11:02:46 +0000213struct Cell {
drh5e2f8b92001-05-28 00:41:15 +0000214 CellHdr h; /* The cell header */
215 char aPayload[MX_LOCAL_PAYLOAD]; /* Key and data */
216 Pgno ovfl; /* The first overflow page */
drh365d68f2001-05-11 11:02:46 +0000217};
drh306dc212001-05-21 13:45:10 +0000218
219/*
220** Free space on a page is remembered using a linked list of the FreeBlk
221** structures. Space on a database page is allocated in increments of
drh72f82862001-05-24 21:06:34 +0000222** at least 4 bytes and is always aligned to a 4-byte boundry. The
drh8b2f49b2001-06-08 00:21:52 +0000223** linked list of FreeBlks is always kept in order by address.
drh306dc212001-05-21 13:45:10 +0000224*/
drh365d68f2001-05-11 11:02:46 +0000225struct FreeBlk {
drh72f82862001-05-24 21:06:34 +0000226 u16 iSize; /* Number of bytes in this block of free space */
drh14acc042001-06-10 19:56:58 +0000227 u16 iNext; /* Index in MemPage.u.aDisk[] of the next free block */
drh365d68f2001-05-11 11:02:46 +0000228};
drh306dc212001-05-21 13:45:10 +0000229
230/*
drh14acc042001-06-10 19:56:58 +0000231** The number of bytes of payload that will fit on a single overflow page.
drh3b7511c2001-05-26 13:15:44 +0000232*/
233#define OVERFLOW_SIZE (SQLITE_PAGE_SIZE-sizeof(Pgno))
234
235/*
drh306dc212001-05-21 13:45:10 +0000236** When the key and data for a single entry in the BTree will not fit in
drh8c42ca92001-06-22 19:15:00 +0000237** the MX_LOCAL_PAYLOAD bytes of space available on the database page,
drh8b2f49b2001-06-08 00:21:52 +0000238** then all extra bytes are written to a linked list of overflow pages.
drh306dc212001-05-21 13:45:10 +0000239** Each overflow page is an instance of the following structure.
240**
241** Unused pages in the database are also represented by instances of
drhbd03cae2001-06-02 02:40:57 +0000242** the OverflowPage structure. The PageOne.freeList field is the
drh306dc212001-05-21 13:45:10 +0000243** page number of the first page in a linked list of unused database
244** pages.
245*/
drh2af926b2001-05-15 00:39:25 +0000246struct OverflowPage {
drh14acc042001-06-10 19:56:58 +0000247 Pgno iNext;
drh5e2f8b92001-05-28 00:41:15 +0000248 char aPayload[OVERFLOW_SIZE];
drh7e3b0a02001-04-28 16:52:40 +0000249};
drh7e3b0a02001-04-28 16:52:40 +0000250
251/*
252** For every page in the database file, an instance of the following structure
drh14acc042001-06-10 19:56:58 +0000253** is stored in memory. The u.aDisk[] array contains the raw bits read from
drhbd03cae2001-06-02 02:40:57 +0000254** the disk. The rest is auxiliary information that held in memory only. The
255** auxiliary info is only valid for regular database pages - it is not
256** used for overflow pages and pages on the freelist.
drh306dc212001-05-21 13:45:10 +0000257**
drhbd03cae2001-06-02 02:40:57 +0000258** Of particular interest in the auxiliary info is the apCell[] entry. Each
drh14acc042001-06-10 19:56:58 +0000259** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are
drh306dc212001-05-21 13:45:10 +0000260** put in this array so that they can be accessed in constant time, rather
drhbd03cae2001-06-02 02:40:57 +0000261** than in linear time which would be needed if we had to walk the linked
262** list on every access.
drh72f82862001-05-24 21:06:34 +0000263**
drh14acc042001-06-10 19:56:58 +0000264** Note that apCell[] contains enough space to hold up to two more Cells
265** than can possibly fit on one page. In the steady state, every apCell[]
266** points to memory inside u.aDisk[]. But in the middle of an insert
267** operation, some apCell[] entries may temporarily point to data space
268** outside of u.aDisk[]. This is a transient situation that is quickly
269** resolved. But while it is happening, it is possible for a database
270** page to hold as many as two more cells than it might otherwise hold.
drh18b81e52001-11-01 13:52:52 +0000271** The extra two entries in apCell[] are an allowance for this situation.
drh14acc042001-06-10 19:56:58 +0000272**
drh72f82862001-05-24 21:06:34 +0000273** The pParent field points back to the parent page. This allows us to
274** walk up the BTree from any leaf to the root. Care must be taken to
275** unref() the parent page pointer when this page is no longer referenced.
drhbd03cae2001-06-02 02:40:57 +0000276** The pageDestructor() routine handles that chore.
drh7e3b0a02001-04-28 16:52:40 +0000277*/
278struct MemPage {
drh14acc042001-06-10 19:56:58 +0000279 union {
280 char aDisk[SQLITE_PAGE_SIZE]; /* Page data stored on disk */
281 PageHdr hdr; /* Overlay page header */
282 } u;
drh5e2f8b92001-05-28 00:41:15 +0000283 int isInit; /* True if auxiliary data is initialized */
drh72f82862001-05-24 21:06:34 +0000284 MemPage *pParent; /* The parent of this page. NULL for root */
drh14acc042001-06-10 19:56:58 +0000285 int nFree; /* Number of free bytes in u.aDisk[] */
drh306dc212001-05-21 13:45:10 +0000286 int nCell; /* Number of entries on this page */
drh14acc042001-06-10 19:56:58 +0000287 int isOverfull; /* Some apCell[] points outside u.aDisk[] */
288 Cell *apCell[MX_CELL+2]; /* All data entires in sorted order */
drh8c42ca92001-06-22 19:15:00 +0000289};
drh7e3b0a02001-04-28 16:52:40 +0000290
291/*
drh3b7511c2001-05-26 13:15:44 +0000292** The in-memory image of a disk page has the auxiliary information appended
293** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
294** that extra information.
295*/
296#define EXTRA_SIZE (sizeof(MemPage)-SQLITE_PAGE_SIZE)
297
298/*
drha059ad02001-04-17 20:09:11 +0000299** Everything we need to know about an open database
300*/
301struct Btree {
302 Pager *pPager; /* The page cache */
drh306dc212001-05-21 13:45:10 +0000303 BtCursor *pCursor; /* A list of all open cursors */
drhbd03cae2001-06-02 02:40:57 +0000304 PageOne *page1; /* First page of the database */
drh306dc212001-05-21 13:45:10 +0000305 int inTrans; /* True if a transaction is in progress */
drhecdc7532001-09-23 02:35:53 +0000306 Hash locks; /* Key: root page number. Data: lock count */
drha059ad02001-04-17 20:09:11 +0000307};
308typedef Btree Bt;
309
drh365d68f2001-05-11 11:02:46 +0000310/*
311** A cursor is a pointer to a particular entry in the BTree.
312** The entry is identified by its MemPage and the index in
drh5e2f8b92001-05-28 00:41:15 +0000313** MemPage.apCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000314*/
drh72f82862001-05-24 21:06:34 +0000315struct BtCursor {
drh5e2f8b92001-05-28 00:41:15 +0000316 Btree *pBt; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000317 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drh8b2f49b2001-06-08 00:21:52 +0000318 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000319 MemPage *pPage; /* Page that contains the entry */
drh8c42ca92001-06-22 19:15:00 +0000320 int idx; /* Index of the entry in pPage->apCell[] */
drhecdc7532001-09-23 02:35:53 +0000321 u8 wrFlag; /* True if writable */
drh5e2f8b92001-05-28 00:41:15 +0000322 u8 bSkipNext; /* sqliteBtreeNext() is no-op if true */
323 u8 iMatch; /* compare result from last sqliteBtreeMoveto() */
drh365d68f2001-05-11 11:02:46 +0000324};
drh7e3b0a02001-04-28 16:52:40 +0000325
drha059ad02001-04-17 20:09:11 +0000326/*
drh3b7511c2001-05-26 13:15:44 +0000327** Compute the total number of bytes that a Cell needs on the main
drh5e2f8b92001-05-28 00:41:15 +0000328** database page. The number returned includes the Cell header,
329** local payload storage, and the pointer to overflow pages (if
drh8c42ca92001-06-22 19:15:00 +0000330** applicable). Additional space allocated on overflow pages
drhbd03cae2001-06-02 02:40:57 +0000331** is NOT included in the value returned from this routine.
drh3b7511c2001-05-26 13:15:44 +0000332*/
333static int cellSize(Cell *pCell){
drh80ff32f2001-11-04 18:32:46 +0000334 int n = NKEY(pCell->h) + NDATA(pCell->h);
drh3b7511c2001-05-26 13:15:44 +0000335 if( n>MX_LOCAL_PAYLOAD ){
336 n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
337 }else{
338 n = ROUNDUP(n);
339 }
340 n += sizeof(CellHdr);
341 return n;
342}
343
344/*
drh72f82862001-05-24 21:06:34 +0000345** Defragment the page given. All Cells are moved to the
346** beginning of the page and all free space is collected
347** into one big FreeBlk at the end of the page.
drh365d68f2001-05-11 11:02:46 +0000348*/
349static void defragmentPage(MemPage *pPage){
drh14acc042001-06-10 19:56:58 +0000350 int pc, i, n;
drh2af926b2001-05-15 00:39:25 +0000351 FreeBlk *pFBlk;
352 char newPage[SQLITE_PAGE_SIZE];
353
drh6019e162001-07-02 17:51:45 +0000354 assert( sqlitepager_iswriteable(pPage) );
drhbd03cae2001-06-02 02:40:57 +0000355 pc = sizeof(PageHdr);
drh14acc042001-06-10 19:56:58 +0000356 pPage->u.hdr.firstCell = pc;
357 memcpy(newPage, pPage->u.aDisk, pc);
drh2af926b2001-05-15 00:39:25 +0000358 for(i=0; i<pPage->nCell; i++){
drh2aa679f2001-06-25 02:11:07 +0000359 Cell *pCell = pPage->apCell[i];
drh8c42ca92001-06-22 19:15:00 +0000360
361 /* This routine should never be called on an overfull page. The
362 ** following asserts verify that constraint. */
drh7c717f72001-06-24 20:39:41 +0000363 assert( Addr(pCell) > Addr(pPage) );
364 assert( Addr(pCell) < Addr(pPage) + SQLITE_PAGE_SIZE );
drh8c42ca92001-06-22 19:15:00 +0000365
drh3b7511c2001-05-26 13:15:44 +0000366 n = cellSize(pCell);
drh2aa679f2001-06-25 02:11:07 +0000367 pCell->h.iNext = pc + n;
drh2af926b2001-05-15 00:39:25 +0000368 memcpy(&newPage[pc], pCell, n);
drh14acc042001-06-10 19:56:58 +0000369 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc];
drh2af926b2001-05-15 00:39:25 +0000370 pc += n;
371 }
drh72f82862001-05-24 21:06:34 +0000372 assert( pPage->nFree==SQLITE_PAGE_SIZE-pc );
drh14acc042001-06-10 19:56:58 +0000373 memcpy(pPage->u.aDisk, newPage, pc);
drh2aa679f2001-06-25 02:11:07 +0000374 if( pPage->nCell>0 ){
375 pPage->apCell[pPage->nCell-1]->h.iNext = 0;
376 }
drh8c42ca92001-06-22 19:15:00 +0000377 pFBlk = (FreeBlk*)&pPage->u.aDisk[pc];
drh2af926b2001-05-15 00:39:25 +0000378 pFBlk->iSize = SQLITE_PAGE_SIZE - pc;
379 pFBlk->iNext = 0;
drh14acc042001-06-10 19:56:58 +0000380 pPage->u.hdr.firstFree = pc;
drh2af926b2001-05-15 00:39:25 +0000381 memset(&pFBlk[1], 0, SQLITE_PAGE_SIZE - pc - sizeof(FreeBlk));
drh365d68f2001-05-11 11:02:46 +0000382}
383
drha059ad02001-04-17 20:09:11 +0000384/*
drh8b2f49b2001-06-08 00:21:52 +0000385** Allocate nByte bytes of space on a page. nByte must be a
386** multiple of 4.
drhbd03cae2001-06-02 02:40:57 +0000387**
drh14acc042001-06-10 19:56:58 +0000388** Return the index into pPage->u.aDisk[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000389** the new allocation. Or return 0 if there is not enough free
390** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000391**
drh72f82862001-05-24 21:06:34 +0000392** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000393** nBytes of contiguous free space, then this routine automatically
394** calls defragementPage() to consolidate all free space before
395** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000396*/
drhbd03cae2001-06-02 02:40:57 +0000397static int allocateSpace(MemPage *pPage, int nByte){
drh2af926b2001-05-15 00:39:25 +0000398 FreeBlk *p;
399 u16 *pIdx;
400 int start;
drh8c42ca92001-06-22 19:15:00 +0000401 int cnt = 0;
drh72f82862001-05-24 21:06:34 +0000402
drh6019e162001-07-02 17:51:45 +0000403 assert( sqlitepager_iswriteable(pPage) );
drh5e2f8b92001-05-28 00:41:15 +0000404 assert( nByte==ROUNDUP(nByte) );
drh14acc042001-06-10 19:56:58 +0000405 if( pPage->nFree<nByte || pPage->isOverfull ) return 0;
406 pIdx = &pPage->u.hdr.firstFree;
407 p = (FreeBlk*)&pPage->u.aDisk[*pIdx];
drh2af926b2001-05-15 00:39:25 +0000408 while( p->iSize<nByte ){
drh8c42ca92001-06-22 19:15:00 +0000409 assert( cnt++ < SQLITE_PAGE_SIZE/4 );
drh2af926b2001-05-15 00:39:25 +0000410 if( p->iNext==0 ){
411 defragmentPage(pPage);
drh14acc042001-06-10 19:56:58 +0000412 pIdx = &pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000413 }else{
414 pIdx = &p->iNext;
415 }
drh14acc042001-06-10 19:56:58 +0000416 p = (FreeBlk*)&pPage->u.aDisk[*pIdx];
drh2af926b2001-05-15 00:39:25 +0000417 }
418 if( p->iSize==nByte ){
419 start = *pIdx;
420 *pIdx = p->iNext;
421 }else{
drh8c42ca92001-06-22 19:15:00 +0000422 FreeBlk *pNew;
drh72f82862001-05-24 21:06:34 +0000423 start = *pIdx;
drh8c42ca92001-06-22 19:15:00 +0000424 pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte];
drh72f82862001-05-24 21:06:34 +0000425 pNew->iNext = p->iNext;
426 pNew->iSize = p->iSize - nByte;
427 *pIdx = start + nByte;
drh2af926b2001-05-15 00:39:25 +0000428 }
429 pPage->nFree -= nByte;
430 return start;
drh7e3b0a02001-04-28 16:52:40 +0000431}
432
433/*
drh14acc042001-06-10 19:56:58 +0000434** Return a section of the MemPage.u.aDisk[] to the freelist.
435** The first byte of the new free block is pPage->u.aDisk[start]
436** and the size of the block is "size" bytes. Size must be
437** a multiple of 4.
drh306dc212001-05-21 13:45:10 +0000438**
439** Most of the effort here is involved in coalesing adjacent
440** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000441*/
442static void freeSpace(MemPage *pPage, int start, int size){
drh2af926b2001-05-15 00:39:25 +0000443 int end = start + size;
444 u16 *pIdx, idx;
445 FreeBlk *pFBlk;
446 FreeBlk *pNew;
447 FreeBlk *pNext;
448
drh6019e162001-07-02 17:51:45 +0000449 assert( sqlitepager_iswriteable(pPage) );
drh2af926b2001-05-15 00:39:25 +0000450 assert( size == ROUNDUP(size) );
451 assert( start == ROUNDUP(start) );
drh14acc042001-06-10 19:56:58 +0000452 pIdx = &pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000453 idx = *pIdx;
454 while( idx!=0 && idx<start ){
drh14acc042001-06-10 19:56:58 +0000455 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000456 if( idx + pFBlk->iSize == start ){
457 pFBlk->iSize += size;
458 if( idx + pFBlk->iSize == pFBlk->iNext ){
drh8c42ca92001-06-22 19:15:00 +0000459 pNext = (FreeBlk*)&pPage->u.aDisk[pFBlk->iNext];
drh2af926b2001-05-15 00:39:25 +0000460 pFBlk->iSize += pNext->iSize;
461 pFBlk->iNext = pNext->iNext;
462 }
463 pPage->nFree += size;
464 return;
465 }
466 pIdx = &pFBlk->iNext;
467 idx = *pIdx;
468 }
drh14acc042001-06-10 19:56:58 +0000469 pNew = (FreeBlk*)&pPage->u.aDisk[start];
drh2af926b2001-05-15 00:39:25 +0000470 if( idx != end ){
471 pNew->iSize = size;
472 pNew->iNext = idx;
473 }else{
drh14acc042001-06-10 19:56:58 +0000474 pNext = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000475 pNew->iSize = size + pNext->iSize;
476 pNew->iNext = pNext->iNext;
477 }
478 *pIdx = start;
479 pPage->nFree += size;
drh7e3b0a02001-04-28 16:52:40 +0000480}
481
482/*
483** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000484**
drhbd03cae2001-06-02 02:40:57 +0000485** The pParent parameter must be a pointer to the MemPage which
486** is the parent of the page being initialized. The root of the
drh8b2f49b2001-06-08 00:21:52 +0000487** BTree (usually page 2) has no parent and so for that page,
488** pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000489**
drh72f82862001-05-24 21:06:34 +0000490** Return SQLITE_OK on success. If we see that the page does
491** not contained a well-formed database page, then return
492** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
493** guarantee that the page is well-formed. It only shows that
494** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000495*/
drh72f82862001-05-24 21:06:34 +0000496static int initPage(MemPage *pPage, Pgno pgnoThis, MemPage *pParent){
drh14acc042001-06-10 19:56:58 +0000497 int idx; /* An index into pPage->u.aDisk[] */
498 Cell *pCell; /* A pointer to a Cell in pPage->u.aDisk[] */
499 FreeBlk *pFBlk; /* A pointer to a free block in pPage->u.aDisk[] */
drh5e2f8b92001-05-28 00:41:15 +0000500 int sz; /* The size of a Cell in bytes */
501 int freeSpace; /* Amount of free space on the page */
drh2af926b2001-05-15 00:39:25 +0000502
drh5e2f8b92001-05-28 00:41:15 +0000503 if( pPage->pParent ){
504 assert( pPage->pParent==pParent );
505 return SQLITE_OK;
506 }
507 if( pParent ){
508 pPage->pParent = pParent;
509 sqlitepager_ref(pParent);
510 }
511 if( pPage->isInit ) return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000512 pPage->isInit = 1;
drh7e3b0a02001-04-28 16:52:40 +0000513 pPage->nCell = 0;
drh6019e162001-07-02 17:51:45 +0000514 freeSpace = USABLE_SPACE;
drh14acc042001-06-10 19:56:58 +0000515 idx = pPage->u.hdr.firstCell;
drh7e3b0a02001-04-28 16:52:40 +0000516 while( idx!=0 ){
drh8c42ca92001-06-22 19:15:00 +0000517 if( idx>SQLITE_PAGE_SIZE-MIN_CELL_SIZE ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000518 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh8c42ca92001-06-22 19:15:00 +0000519 if( idx!=ROUNDUP(idx) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000520 pCell = (Cell*)&pPage->u.aDisk[idx];
drh5e2f8b92001-05-28 00:41:15 +0000521 sz = cellSize(pCell);
522 if( idx+sz > SQLITE_PAGE_SIZE ) goto page_format_error;
523 freeSpace -= sz;
524 pPage->apCell[pPage->nCell++] = pCell;
drh3b7511c2001-05-26 13:15:44 +0000525 idx = pCell->h.iNext;
drh2af926b2001-05-15 00:39:25 +0000526 }
527 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +0000528 idx = pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000529 while( idx!=0 ){
530 if( idx>SQLITE_PAGE_SIZE-sizeof(FreeBlk) ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000531 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000532 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000533 pPage->nFree += pFBlk->iSize;
drh7c717f72001-06-24 20:39:41 +0000534 if( pFBlk->iNext>0 && pFBlk->iNext <= idx ) goto page_format_error;
drh2af926b2001-05-15 00:39:25 +0000535 idx = pFBlk->iNext;
drh7e3b0a02001-04-28 16:52:40 +0000536 }
drh8b2f49b2001-06-08 00:21:52 +0000537 if( pPage->nCell==0 && pPage->nFree==0 ){
538 /* As a special case, an uninitialized root page appears to be
539 ** an empty database */
540 return SQLITE_OK;
541 }
drh5e2f8b92001-05-28 00:41:15 +0000542 if( pPage->nFree!=freeSpace ) goto page_format_error;
drh7e3b0a02001-04-28 16:52:40 +0000543 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +0000544
545page_format_error:
546 return SQLITE_CORRUPT;
drh7e3b0a02001-04-28 16:52:40 +0000547}
548
549/*
drh8b2f49b2001-06-08 00:21:52 +0000550** Set up a raw page so that it looks like a database page holding
551** no entries.
drhbd03cae2001-06-02 02:40:57 +0000552*/
553static void zeroPage(MemPage *pPage){
554 PageHdr *pHdr;
555 FreeBlk *pFBlk;
drh6019e162001-07-02 17:51:45 +0000556 assert( sqlitepager_iswriteable(pPage) );
drhbd03cae2001-06-02 02:40:57 +0000557 memset(pPage, 0, SQLITE_PAGE_SIZE);
drh14acc042001-06-10 19:56:58 +0000558 pHdr = &pPage->u.hdr;
drhbd03cae2001-06-02 02:40:57 +0000559 pHdr->firstCell = 0;
560 pHdr->firstFree = sizeof(*pHdr);
561 pFBlk = (FreeBlk*)&pHdr[1];
562 pFBlk->iNext = 0;
563 pFBlk->iSize = SQLITE_PAGE_SIZE - sizeof(*pHdr);
drh8c42ca92001-06-22 19:15:00 +0000564 pPage->nFree = pFBlk->iSize;
565 pPage->nCell = 0;
566 pPage->isOverfull = 0;
drhbd03cae2001-06-02 02:40:57 +0000567}
568
569/*
drh72f82862001-05-24 21:06:34 +0000570** This routine is called when the reference count for a page
571** reaches zero. We need to unref the pParent pointer when that
572** happens.
573*/
574static void pageDestructor(void *pData){
575 MemPage *pPage = (MemPage*)pData;
576 if( pPage->pParent ){
577 MemPage *pParent = pPage->pParent;
578 pPage->pParent = 0;
579 sqlitepager_unref(pParent);
580 }
581}
582
583/*
drh306dc212001-05-21 13:45:10 +0000584** Open a new database.
585**
586** Actually, this routine just sets up the internal data structures
drh72f82862001-05-24 21:06:34 +0000587** for accessing the database. We do not open the database file
588** until the first page is loaded.
drh382c0242001-10-06 16:33:02 +0000589**
590** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +0000591** a new database with a random name is created. This randomly named
592** database file will be deleted when sqliteBtreeClose() is called.
drha059ad02001-04-17 20:09:11 +0000593*/
drh6019e162001-07-02 17:51:45 +0000594int sqliteBtreeOpen(
595 const char *zFilename, /* Name of the file containing the BTree database */
596 int mode, /* Not currently used */
597 int nCache, /* How many pages in the page cache */
598 Btree **ppBtree /* Pointer to new Btree object written here */
599){
drha059ad02001-04-17 20:09:11 +0000600 Btree *pBt;
drh8c42ca92001-06-22 19:15:00 +0000601 int rc;
drha059ad02001-04-17 20:09:11 +0000602
603 pBt = sqliteMalloc( sizeof(*pBt) );
604 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +0000605 *ppBtree = 0;
drha059ad02001-04-17 20:09:11 +0000606 return SQLITE_NOMEM;
607 }
drh6019e162001-07-02 17:51:45 +0000608 if( nCache<10 ) nCache = 10;
609 rc = sqlitepager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE);
drha059ad02001-04-17 20:09:11 +0000610 if( rc!=SQLITE_OK ){
611 if( pBt->pPager ) sqlitepager_close(pBt->pPager);
612 sqliteFree(pBt);
613 *ppBtree = 0;
614 return rc;
615 }
drh72f82862001-05-24 21:06:34 +0000616 sqlitepager_set_destructor(pBt->pPager, pageDestructor);
drha059ad02001-04-17 20:09:11 +0000617 pBt->pCursor = 0;
618 pBt->page1 = 0;
drhecdc7532001-09-23 02:35:53 +0000619 sqliteHashInit(&pBt->locks, SQLITE_HASH_INT, 0);
drha059ad02001-04-17 20:09:11 +0000620 *ppBtree = pBt;
621 return SQLITE_OK;
622}
623
624/*
625** Close an open database and invalidate all cursors.
626*/
627int sqliteBtreeClose(Btree *pBt){
628 while( pBt->pCursor ){
629 sqliteBtreeCloseCursor(pBt->pCursor);
630 }
631 sqlitepager_close(pBt->pPager);
drhecdc7532001-09-23 02:35:53 +0000632 sqliteHashClear(&pBt->locks);
drha059ad02001-04-17 20:09:11 +0000633 sqliteFree(pBt);
634 return SQLITE_OK;
635}
636
637/*
drhf57b14a2001-09-14 18:54:08 +0000638** Change the number of pages in the cache.
639*/
640int sqliteBtreeSetCacheSize(Btree *pBt, int mxPage){
641 sqlitepager_set_cachesize(pBt->pPager, mxPage);
642 return SQLITE_OK;
643}
644
645/*
drh306dc212001-05-21 13:45:10 +0000646** Get a reference to page1 of the database file. This will
647** also acquire a readlock on that file.
648**
649** SQLITE_OK is returned on success. If the file is not a
650** well-formed database file, then SQLITE_CORRUPT is returned.
651** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
652** is returned if we run out of memory. SQLITE_PROTOCOL is returned
653** if there is a locking protocol violation.
654*/
655static int lockBtree(Btree *pBt){
656 int rc;
657 if( pBt->page1 ) return SQLITE_OK;
drh8c42ca92001-06-22 19:15:00 +0000658 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1);
drh306dc212001-05-21 13:45:10 +0000659 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +0000660
661 /* Do some checking to help insure the file we opened really is
662 ** a valid database file.
663 */
664 if( sqlitepager_pagecount(pBt->pPager)>0 ){
drhbd03cae2001-06-02 02:40:57 +0000665 PageOne *pP1 = pBt->page1;
drh8c42ca92001-06-22 19:15:00 +0000666 if( strcmp(pP1->zMagic,zMagicHeader)!=0 || pP1->iMagic!=MAGIC ){
drh306dc212001-05-21 13:45:10 +0000667 rc = SQLITE_CORRUPT;
drh72f82862001-05-24 21:06:34 +0000668 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +0000669 }
670 }
671 return rc;
672
drh72f82862001-05-24 21:06:34 +0000673page1_init_failed:
drh306dc212001-05-21 13:45:10 +0000674 sqlitepager_unref(pBt->page1);
675 pBt->page1 = 0;
drh72f82862001-05-24 21:06:34 +0000676 return rc;
drh306dc212001-05-21 13:45:10 +0000677}
678
679/*
drhb8ca3072001-12-05 00:21:20 +0000680** If there are no outstanding cursors and we are not in the middle
681** of a transaction but there is a read lock on the database, then
682** this routine unrefs the first page of the database file which
683** has the effect of releasing the read lock.
684**
685** If there are any outstanding cursors, this routine is a no-op.
686**
687** If there is a transaction in progress, this routine is a no-op.
688*/
689static void unlockBtreeIfUnused(Btree *pBt){
690 if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){
691 sqlitepager_unref(pBt->page1);
692 pBt->page1 = 0;
693 pBt->inTrans = 0;
694 }
695}
696
697/*
drh8c42ca92001-06-22 19:15:00 +0000698** Create a new database by initializing the first two pages of the
699** file.
drh8b2f49b2001-06-08 00:21:52 +0000700*/
701static int newDatabase(Btree *pBt){
702 MemPage *pRoot;
703 PageOne *pP1;
drh8c42ca92001-06-22 19:15:00 +0000704 int rc;
drh7c717f72001-06-24 20:39:41 +0000705 if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK;
drh8b2f49b2001-06-08 00:21:52 +0000706 pP1 = pBt->page1;
707 rc = sqlitepager_write(pBt->page1);
708 if( rc ) return rc;
drh8c42ca92001-06-22 19:15:00 +0000709 rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot);
drh8b2f49b2001-06-08 00:21:52 +0000710 if( rc ) return rc;
711 rc = sqlitepager_write(pRoot);
712 if( rc ){
713 sqlitepager_unref(pRoot);
714 return rc;
715 }
716 strcpy(pP1->zMagic, zMagicHeader);
drh8c42ca92001-06-22 19:15:00 +0000717 pP1->iMagic = MAGIC;
drh8b2f49b2001-06-08 00:21:52 +0000718 zeroPage(pRoot);
719 sqlitepager_unref(pRoot);
720 return SQLITE_OK;
721}
722
723/*
drh72f82862001-05-24 21:06:34 +0000724** Attempt to start a new transaction.
drh8b2f49b2001-06-08 00:21:52 +0000725**
726** A transaction must be started before attempting any changes
727** to the database. None of the following routines will work
728** unless a transaction is started first:
729**
730** sqliteBtreeCreateTable()
731** sqliteBtreeClearTable()
732** sqliteBtreeDropTable()
733** sqliteBtreeInsert()
734** sqliteBtreeDelete()
735** sqliteBtreeUpdateMeta()
drha059ad02001-04-17 20:09:11 +0000736*/
737int sqliteBtreeBeginTrans(Btree *pBt){
738 int rc;
739 if( pBt->inTrans ) return SQLITE_ERROR;
740 if( pBt->page1==0 ){
drh7e3b0a02001-04-28 16:52:40 +0000741 rc = lockBtree(pBt);
drh8c42ca92001-06-22 19:15:00 +0000742 if( rc!=SQLITE_OK ){
743 return rc;
744 }
drha059ad02001-04-17 20:09:11 +0000745 }
drhb8ca3072001-12-05 00:21:20 +0000746 if( sqlitepager_isreadonly(pBt->pPager) ){
747 return SQLITE_READONLY;
748 }
749 rc = sqlitepager_write(pBt->page1);
750 if( rc==SQLITE_OK ){
drh5e00f6c2001-09-13 13:46:56 +0000751 rc = newDatabase(pBt);
drha059ad02001-04-17 20:09:11 +0000752 }
drhb8ca3072001-12-05 00:21:20 +0000753 if( rc==SQLITE_OK ){
754 pBt->inTrans = 1;
755 }else{
756 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000757 }
drhb8ca3072001-12-05 00:21:20 +0000758 return rc;
drha059ad02001-04-17 20:09:11 +0000759}
760
761/*
drh2aa679f2001-06-25 02:11:07 +0000762** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +0000763**
764** This will release the write lock on the database file. If there
765** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +0000766*/
767int sqliteBtreeCommit(Btree *pBt){
768 int rc;
drh2aa679f2001-06-25 02:11:07 +0000769 if( pBt->inTrans==0 ) return SQLITE_ERROR;
drha059ad02001-04-17 20:09:11 +0000770 rc = sqlitepager_commit(pBt->pPager);
drh7c717f72001-06-24 20:39:41 +0000771 pBt->inTrans = 0;
drh5e00f6c2001-09-13 13:46:56 +0000772 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000773 return rc;
774}
775
776/*
drhecdc7532001-09-23 02:35:53 +0000777** Rollback the transaction in progress. All cursors will be
778** invalided by this operation. Any attempt to use a cursor
779** that was open at the beginning of this operation will result
780** in an error.
drh5e00f6c2001-09-13 13:46:56 +0000781**
782** This will release the write lock on the database file. If there
783** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +0000784*/
785int sqliteBtreeRollback(Btree *pBt){
786 int rc;
drhecdc7532001-09-23 02:35:53 +0000787 BtCursor *pCur;
drh7c717f72001-06-24 20:39:41 +0000788 if( pBt->inTrans==0 ) return SQLITE_OK;
789 pBt->inTrans = 0;
drhecdc7532001-09-23 02:35:53 +0000790 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
791 if( pCur->pPage ){
792 sqlitepager_unref(pCur->pPage);
793 pCur->pPage = 0;
794 }
795 }
drha059ad02001-04-17 20:09:11 +0000796 rc = sqlitepager_rollback(pBt->pPager);
drh5e00f6c2001-09-13 13:46:56 +0000797 unlockBtreeIfUnused(pBt);
drha059ad02001-04-17 20:09:11 +0000798 return rc;
799}
800
801/*
drh8b2f49b2001-06-08 00:21:52 +0000802** Create a new cursor for the BTree whose root is on the page
803** iTable. The act of acquiring a cursor gets a read lock on
804** the database file.
drh1bee3d72001-10-15 00:44:35 +0000805**
806** If wrFlag==0, then the cursor can only be used for reading.
807** If wrFlag==1, then the cursor can be used for reading or writing.
808** A read/write cursor requires exclusive access to its table. There
809** cannot be two or more cursors open on the same table is any one of
810** cursors is a read/write cursor. But there can be two or more
811** read-only cursors open on the same table.
drha059ad02001-04-17 20:09:11 +0000812*/
drhecdc7532001-09-23 02:35:53 +0000813int sqliteBtreeCursor(Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur){
drha059ad02001-04-17 20:09:11 +0000814 int rc;
815 BtCursor *pCur;
drh5a2c2c22001-11-21 02:21:11 +0000816 ptr nLock;
drhecdc7532001-09-23 02:35:53 +0000817
drha059ad02001-04-17 20:09:11 +0000818 if( pBt->page1==0 ){
819 rc = lockBtree(pBt);
820 if( rc!=SQLITE_OK ){
821 *ppCur = 0;
822 return rc;
823 }
824 }
825 pCur = sqliteMalloc( sizeof(*pCur) );
826 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +0000827 rc = SQLITE_NOMEM;
828 goto create_cursor_exception;
829 }
drh8b2f49b2001-06-08 00:21:52 +0000830 pCur->pgnoRoot = (Pgno)iTable;
drh8c42ca92001-06-22 19:15:00 +0000831 rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +0000832 if( rc!=SQLITE_OK ){
833 goto create_cursor_exception;
834 }
drh8b2f49b2001-06-08 00:21:52 +0000835 rc = initPage(pCur->pPage, pCur->pgnoRoot, 0);
drhbd03cae2001-06-02 02:40:57 +0000836 if( rc!=SQLITE_OK ){
837 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +0000838 }
drh5a2c2c22001-11-21 02:21:11 +0000839 nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable);
drhecdc7532001-09-23 02:35:53 +0000840 if( nLock<0 || (nLock>0 && wrFlag) ){
841 rc = SQLITE_LOCKED;
842 goto create_cursor_exception;
843 }
844 nLock = wrFlag ? -1 : nLock+1;
845 sqliteHashInsert(&pBt->locks, 0, iTable, (void*)nLock);
drh14acc042001-06-10 19:56:58 +0000846 pCur->pBt = pBt;
drhecdc7532001-09-23 02:35:53 +0000847 pCur->wrFlag = wrFlag;
drh14acc042001-06-10 19:56:58 +0000848 pCur->idx = 0;
drha059ad02001-04-17 20:09:11 +0000849 pCur->pNext = pBt->pCursor;
850 if( pCur->pNext ){
851 pCur->pNext->pPrev = pCur;
852 }
drh14acc042001-06-10 19:56:58 +0000853 pCur->pPrev = 0;
drha059ad02001-04-17 20:09:11 +0000854 pBt->pCursor = pCur;
drh2af926b2001-05-15 00:39:25 +0000855 *ppCur = pCur;
856 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +0000857
858create_cursor_exception:
859 *ppCur = 0;
860 if( pCur ){
861 if( pCur->pPage ) sqlitepager_unref(pCur->pPage);
862 sqliteFree(pCur);
863 }
drh5e00f6c2001-09-13 13:46:56 +0000864 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +0000865 return rc;
drha059ad02001-04-17 20:09:11 +0000866}
867
868/*
drh5e00f6c2001-09-13 13:46:56 +0000869** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +0000870** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +0000871*/
872int sqliteBtreeCloseCursor(BtCursor *pCur){
drh5a2c2c22001-11-21 02:21:11 +0000873 ptr nLock;
drha059ad02001-04-17 20:09:11 +0000874 Btree *pBt = pCur->pBt;
drha059ad02001-04-17 20:09:11 +0000875 if( pCur->pPrev ){
876 pCur->pPrev->pNext = pCur->pNext;
877 }else{
878 pBt->pCursor = pCur->pNext;
879 }
880 if( pCur->pNext ){
881 pCur->pNext->pPrev = pCur->pPrev;
882 }
drhecdc7532001-09-23 02:35:53 +0000883 if( pCur->pPage ){
884 sqlitepager_unref(pCur->pPage);
885 }
drh5e00f6c2001-09-13 13:46:56 +0000886 unlockBtreeIfUnused(pBt);
drh5a2c2c22001-11-21 02:21:11 +0000887 nLock = (ptr)sqliteHashFind(&pBt->locks, 0, pCur->pgnoRoot);
drh6d4abfb2001-10-22 02:58:08 +0000888 assert( nLock!=0 || sqlite_malloc_failed );
drhecdc7532001-09-23 02:35:53 +0000889 nLock = nLock<0 ? 0 : nLock-1;
890 sqliteHashInsert(&pBt->locks, 0, pCur->pgnoRoot, (void*)nLock);
drha059ad02001-04-17 20:09:11 +0000891 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +0000892 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +0000893}
894
drh7e3b0a02001-04-28 16:52:40 +0000895/*
drh5e2f8b92001-05-28 00:41:15 +0000896** Make a temporary cursor by filling in the fields of pTempCur.
897** The temporary cursor is not on the cursor list for the Btree.
898*/
drh14acc042001-06-10 19:56:58 +0000899static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +0000900 memcpy(pTempCur, pCur, sizeof(*pCur));
901 pTempCur->pNext = 0;
902 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +0000903 if( pTempCur->pPage ){
904 sqlitepager_ref(pTempCur->pPage);
905 }
drh5e2f8b92001-05-28 00:41:15 +0000906}
907
908/*
drhbd03cae2001-06-02 02:40:57 +0000909** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +0000910** function above.
911*/
drh14acc042001-06-10 19:56:58 +0000912static void releaseTempCursor(BtCursor *pCur){
drhecdc7532001-09-23 02:35:53 +0000913 if( pCur->pPage ){
914 sqlitepager_unref(pCur->pPage);
915 }
drh5e2f8b92001-05-28 00:41:15 +0000916}
917
918/*
drhbd03cae2001-06-02 02:40:57 +0000919** Set *pSize to the number of bytes of key in the entry the
920** cursor currently points to. Always return SQLITE_OK.
921** Failure is not possible. If the cursor is not currently
922** pointing to an entry (which can happen, for example, if
923** the database is empty) then *pSize is set to 0.
drh7e3b0a02001-04-28 16:52:40 +0000924*/
drh72f82862001-05-24 21:06:34 +0000925int sqliteBtreeKeySize(BtCursor *pCur, int *pSize){
drh2af926b2001-05-15 00:39:25 +0000926 Cell *pCell;
927 MemPage *pPage;
928
929 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +0000930 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh72f82862001-05-24 21:06:34 +0000931 *pSize = 0;
932 }else{
drh5e2f8b92001-05-28 00:41:15 +0000933 pCell = pPage->apCell[pCur->idx];
drh80ff32f2001-11-04 18:32:46 +0000934 *pSize = NKEY(pCell->h);
drh72f82862001-05-24 21:06:34 +0000935 }
936 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +0000937}
drh2af926b2001-05-15 00:39:25 +0000938
drh72f82862001-05-24 21:06:34 +0000939/*
940** Read payload information from the entry that the pCur cursor is
941** pointing to. Begin reading the payload at "offset" and read
942** a total of "amt" bytes. Put the result in zBuf.
943**
944** This routine does not make a distinction between key and data.
945** It just reads bytes from the payload area.
946*/
drh2af926b2001-05-15 00:39:25 +0000947static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){
drh5e2f8b92001-05-28 00:41:15 +0000948 char *aPayload;
drh2af926b2001-05-15 00:39:25 +0000949 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +0000950 int rc;
drh72f82862001-05-24 21:06:34 +0000951 assert( pCur!=0 && pCur->pPage!=0 );
drh8c42ca92001-06-22 19:15:00 +0000952 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
953 aPayload = pCur->pPage->apCell[pCur->idx]->aPayload;
drh2af926b2001-05-15 00:39:25 +0000954 if( offset<MX_LOCAL_PAYLOAD ){
955 int a = amt;
956 if( a+offset>MX_LOCAL_PAYLOAD ){
957 a = MX_LOCAL_PAYLOAD - offset;
958 }
drh5e2f8b92001-05-28 00:41:15 +0000959 memcpy(zBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +0000960 if( a==amt ){
961 return SQLITE_OK;
962 }
drh2aa679f2001-06-25 02:11:07 +0000963 offset = 0;
drh2af926b2001-05-15 00:39:25 +0000964 zBuf += a;
965 amt -= a;
drhdd793422001-06-28 01:54:48 +0000966 }else{
967 offset -= MX_LOCAL_PAYLOAD;
drhbd03cae2001-06-02 02:40:57 +0000968 }
969 if( amt>0 ){
drh8c42ca92001-06-22 19:15:00 +0000970 nextPage = pCur->pPage->apCell[pCur->idx]->ovfl;
drh2af926b2001-05-15 00:39:25 +0000971 }
972 while( amt>0 && nextPage ){
973 OverflowPage *pOvfl;
drh8c42ca92001-06-22 19:15:00 +0000974 rc = sqlitepager_get(pCur->pBt->pPager, nextPage, (void**)&pOvfl);
drh2af926b2001-05-15 00:39:25 +0000975 if( rc!=0 ){
976 return rc;
977 }
drh14acc042001-06-10 19:56:58 +0000978 nextPage = pOvfl->iNext;
drh2af926b2001-05-15 00:39:25 +0000979 if( offset<OVERFLOW_SIZE ){
980 int a = amt;
981 if( a + offset > OVERFLOW_SIZE ){
982 a = OVERFLOW_SIZE - offset;
983 }
drh5e2f8b92001-05-28 00:41:15 +0000984 memcpy(zBuf, &pOvfl->aPayload[offset], a);
drh2aa679f2001-06-25 02:11:07 +0000985 offset = 0;
drh2af926b2001-05-15 00:39:25 +0000986 amt -= a;
987 zBuf += a;
drh2aa679f2001-06-25 02:11:07 +0000988 }else{
989 offset -= OVERFLOW_SIZE;
drh2af926b2001-05-15 00:39:25 +0000990 }
991 sqlitepager_unref(pOvfl);
992 }
drha7fcb052001-12-14 15:09:55 +0000993 if( amt>0 ){
994 return SQLITE_CORRUPT;
995 }
996 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +0000997}
998
drh72f82862001-05-24 21:06:34 +0000999/*
drh5e00f6c2001-09-13 13:46:56 +00001000** Read part of the key associated with cursor pCur. A maximum
drh72f82862001-05-24 21:06:34 +00001001** of "amt" bytes will be transfered into zBuf[]. The transfer
drh5e00f6c2001-09-13 13:46:56 +00001002** begins at "offset". The number of bytes actually read is
1003** returned. The amount returned will be smaller than the
1004** amount requested if there are not enough bytes in the key
1005** to satisfy the request.
drh72f82862001-05-24 21:06:34 +00001006*/
1007int sqliteBtreeKey(BtCursor *pCur, int offset, int amt, char *zBuf){
1008 Cell *pCell;
1009 MemPage *pPage;
drha059ad02001-04-17 20:09:11 +00001010
drh5e00f6c2001-09-13 13:46:56 +00001011 if( amt<0 ) return 0;
1012 if( offset<0 ) return 0;
1013 if( amt==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001014 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001015 if( pPage==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001016 if( pCur->idx >= pPage->nCell ){
drh5e00f6c2001-09-13 13:46:56 +00001017 return 0;
drh72f82862001-05-24 21:06:34 +00001018 }
drh5e2f8b92001-05-28 00:41:15 +00001019 pCell = pPage->apCell[pCur->idx];
drh80ff32f2001-11-04 18:32:46 +00001020 if( amt+offset > NKEY(pCell->h) ){
1021 amt = NKEY(pCell->h) - offset;
drh5e00f6c2001-09-13 13:46:56 +00001022 if( amt<=0 ){
1023 return 0;
1024 }
drhbd03cae2001-06-02 02:40:57 +00001025 }
drh5e00f6c2001-09-13 13:46:56 +00001026 getPayload(pCur, offset, amt, zBuf);
1027 return amt;
drh72f82862001-05-24 21:06:34 +00001028}
1029
1030/*
drhbd03cae2001-06-02 02:40:57 +00001031** Set *pSize to the number of bytes of data in the entry the
1032** cursor currently points to. Always return SQLITE_OK.
1033** Failure is not possible. If the cursor is not currently
1034** pointing to an entry (which can happen, for example, if
1035** the database is empty) then *pSize is set to 0.
drh72f82862001-05-24 21:06:34 +00001036*/
1037int sqliteBtreeDataSize(BtCursor *pCur, int *pSize){
1038 Cell *pCell;
1039 MemPage *pPage;
1040
1041 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001042 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh72f82862001-05-24 21:06:34 +00001043 *pSize = 0;
1044 }else{
drh5e2f8b92001-05-28 00:41:15 +00001045 pCell = pPage->apCell[pCur->idx];
drh80ff32f2001-11-04 18:32:46 +00001046 *pSize = NDATA(pCell->h);
drh72f82862001-05-24 21:06:34 +00001047 }
1048 return SQLITE_OK;
1049}
1050
1051/*
drh5e00f6c2001-09-13 13:46:56 +00001052** Read part of the data associated with cursor pCur. A maximum
drh72f82862001-05-24 21:06:34 +00001053** of "amt" bytes will be transfered into zBuf[]. The transfer
drh5e00f6c2001-09-13 13:46:56 +00001054** begins at "offset". The number of bytes actually read is
1055** returned. The amount returned will be smaller than the
1056** amount requested if there are not enough bytes in the data
1057** to satisfy the request.
drh72f82862001-05-24 21:06:34 +00001058*/
1059int sqliteBtreeData(BtCursor *pCur, int offset, int amt, char *zBuf){
1060 Cell *pCell;
1061 MemPage *pPage;
1062
drh5e00f6c2001-09-13 13:46:56 +00001063 if( amt<0 ) return 0;
1064 if( offset<0 ) return 0;
1065 if( amt==0 ) return 0;
drh72f82862001-05-24 21:06:34 +00001066 pPage = pCur->pPage;
drhecdc7532001-09-23 02:35:53 +00001067 if( pPage==0 || pCur->idx >= pPage->nCell ){
drh5e00f6c2001-09-13 13:46:56 +00001068 return 0;
drh72f82862001-05-24 21:06:34 +00001069 }
drh5e2f8b92001-05-28 00:41:15 +00001070 pCell = pPage->apCell[pCur->idx];
drh80ff32f2001-11-04 18:32:46 +00001071 if( amt+offset > NDATA(pCell->h) ){
1072 amt = NDATA(pCell->h) - offset;
drh5e00f6c2001-09-13 13:46:56 +00001073 if( amt<=0 ){
1074 return 0;
1075 }
drhbd03cae2001-06-02 02:40:57 +00001076 }
drh80ff32f2001-11-04 18:32:46 +00001077 getPayload(pCur, offset + NKEY(pCell->h), amt, zBuf);
drh5e00f6c2001-09-13 13:46:56 +00001078 return amt;
drh72f82862001-05-24 21:06:34 +00001079}
drha059ad02001-04-17 20:09:11 +00001080
drh2af926b2001-05-15 00:39:25 +00001081/*
drh8721ce42001-11-07 14:22:00 +00001082** Compare an external key against the key on the entry that pCur points to.
1083**
1084** The external key is pKey and is nKey bytes long. The last nIgnore bytes
1085** of the key associated with pCur are ignored, as if they do not exist.
1086** (The normal case is for nIgnore to be zero in which case the entire
1087** internal key is used in the comparison.)
1088**
1089** The comparison result is written to *pRes as follows:
drh2af926b2001-05-15 00:39:25 +00001090**
drh717e6402001-09-27 03:22:32 +00001091** *pRes<0 This means pCur<pKey
1092**
1093** *pRes==0 This means pCur==pKey for all nKey bytes
1094**
1095** *pRes>0 This means pCur>pKey
1096**
drh8721ce42001-11-07 14:22:00 +00001097** When one key is an exact prefix of the other, the shorter key is
1098** considered less than the longer one. In order to be equal the
1099** keys must be exactly the same length. (The length of the pCur key
1100** is the actual key length minus nIgnore bytes.)
drh2af926b2001-05-15 00:39:25 +00001101*/
drh717e6402001-09-27 03:22:32 +00001102int sqliteBtreeKeyCompare(
drh8721ce42001-11-07 14:22:00 +00001103 BtCursor *pCur, /* Pointer to entry to compare against */
1104 const void *pKey, /* Key to compare against entry that pCur points to */
1105 int nKey, /* Number of bytes in pKey */
1106 int nIgnore, /* Ignore this many bytes at the end of pCur */
1107 int *pResult /* Write the result here */
drh5c4d9702001-08-20 00:33:58 +00001108){
drh2af926b2001-05-15 00:39:25 +00001109 Pgno nextPage;
drh8721ce42001-11-07 14:22:00 +00001110 int n, c, rc, nLocal;
drh2af926b2001-05-15 00:39:25 +00001111 Cell *pCell;
drh717e6402001-09-27 03:22:32 +00001112 const char *zKey = (const char*)pKey;
drh2af926b2001-05-15 00:39:25 +00001113
1114 assert( pCur->pPage );
1115 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drhbd03cae2001-06-02 02:40:57 +00001116 pCell = pCur->pPage->apCell[pCur->idx];
drh8721ce42001-11-07 14:22:00 +00001117 nLocal = NKEY(pCell->h) - nIgnore;
1118 if( nLocal<0 ) nLocal = 0;
1119 n = nKey<nLocal ? nKey : nLocal;
drh2af926b2001-05-15 00:39:25 +00001120 if( n>MX_LOCAL_PAYLOAD ){
1121 n = MX_LOCAL_PAYLOAD;
1122 }
drh717e6402001-09-27 03:22:32 +00001123 c = memcmp(pCell->aPayload, zKey, n);
drh2af926b2001-05-15 00:39:25 +00001124 if( c!=0 ){
1125 *pResult = c;
1126 return SQLITE_OK;
1127 }
drh717e6402001-09-27 03:22:32 +00001128 zKey += n;
drh2af926b2001-05-15 00:39:25 +00001129 nKey -= n;
drh8721ce42001-11-07 14:22:00 +00001130 nLocal -= n;
drh3b7511c2001-05-26 13:15:44 +00001131 nextPage = pCell->ovfl;
drh8721ce42001-11-07 14:22:00 +00001132 while( nKey>0 && nLocal>0 ){
drh2af926b2001-05-15 00:39:25 +00001133 OverflowPage *pOvfl;
1134 if( nextPage==0 ){
1135 return SQLITE_CORRUPT;
1136 }
drh8c42ca92001-06-22 19:15:00 +00001137 rc = sqlitepager_get(pCur->pBt->pPager, nextPage, (void**)&pOvfl);
drh72f82862001-05-24 21:06:34 +00001138 if( rc ){
drh2af926b2001-05-15 00:39:25 +00001139 return rc;
1140 }
drh14acc042001-06-10 19:56:58 +00001141 nextPage = pOvfl->iNext;
drh8721ce42001-11-07 14:22:00 +00001142 n = nKey<nLocal ? nKey : nLocal;
drh2af926b2001-05-15 00:39:25 +00001143 if( n>OVERFLOW_SIZE ){
1144 n = OVERFLOW_SIZE;
1145 }
drh717e6402001-09-27 03:22:32 +00001146 c = memcmp(pOvfl->aPayload, zKey, n);
drh2af926b2001-05-15 00:39:25 +00001147 sqlitepager_unref(pOvfl);
1148 if( c!=0 ){
1149 *pResult = c;
1150 return SQLITE_OK;
1151 }
1152 nKey -= n;
drh8721ce42001-11-07 14:22:00 +00001153 nLocal -= n;
drh717e6402001-09-27 03:22:32 +00001154 zKey += n;
drh2af926b2001-05-15 00:39:25 +00001155 }
drh717e6402001-09-27 03:22:32 +00001156 if( c==0 ){
drh8721ce42001-11-07 14:22:00 +00001157 c = nLocal - nKey;
drh717e6402001-09-27 03:22:32 +00001158 }
drh2af926b2001-05-15 00:39:25 +00001159 *pResult = c;
1160 return SQLITE_OK;
1161}
1162
drh72f82862001-05-24 21:06:34 +00001163/*
1164** Move the cursor down to a new child page.
1165*/
drh5e2f8b92001-05-28 00:41:15 +00001166static int moveToChild(BtCursor *pCur, int newPgno){
drh72f82862001-05-24 21:06:34 +00001167 int rc;
1168 MemPage *pNewPage;
1169
drh8c42ca92001-06-22 19:15:00 +00001170 rc = sqlitepager_get(pCur->pBt->pPager, newPgno, (void**)&pNewPage);
drh6019e162001-07-02 17:51:45 +00001171 if( rc ) return rc;
1172 rc = initPage(pNewPage, newPgno, pCur->pPage);
1173 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001174 sqlitepager_unref(pCur->pPage);
1175 pCur->pPage = pNewPage;
1176 pCur->idx = 0;
1177 return SQLITE_OK;
1178}
1179
1180/*
drh5e2f8b92001-05-28 00:41:15 +00001181** Move the cursor up to the parent page.
1182**
1183** pCur->idx is set to the cell index that contains the pointer
1184** to the page we are coming from. If we are coming from the
1185** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00001186** the largest cell index.
drh72f82862001-05-24 21:06:34 +00001187*/
drh5e2f8b92001-05-28 00:41:15 +00001188static int moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001189 Pgno oldPgno;
1190 MemPage *pParent;
drh8c42ca92001-06-22 19:15:00 +00001191 int i;
drh72f82862001-05-24 21:06:34 +00001192 pParent = pCur->pPage->pParent;
drhbd03cae2001-06-02 02:40:57 +00001193 if( pParent==0 ) return SQLITE_INTERNAL;
drh72f82862001-05-24 21:06:34 +00001194 oldPgno = sqlitepager_pagenumber(pCur->pPage);
drh72f82862001-05-24 21:06:34 +00001195 sqlitepager_ref(pParent);
1196 sqlitepager_unref(pCur->pPage);
1197 pCur->pPage = pParent;
drh8c42ca92001-06-22 19:15:00 +00001198 pCur->idx = pParent->nCell;
1199 for(i=0; i<pParent->nCell; i++){
1200 if( pParent->apCell[i]->h.leftChild==oldPgno ){
drh72f82862001-05-24 21:06:34 +00001201 pCur->idx = i;
1202 break;
1203 }
1204 }
drh5e2f8b92001-05-28 00:41:15 +00001205 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00001206}
1207
1208/*
1209** Move the cursor to the root page
1210*/
drh5e2f8b92001-05-28 00:41:15 +00001211static int moveToRoot(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001212 MemPage *pNew;
drhbd03cae2001-06-02 02:40:57 +00001213 int rc;
1214
drh8c42ca92001-06-22 19:15:00 +00001215 rc = sqlitepager_get(pCur->pBt->pPager, pCur->pgnoRoot, (void**)&pNew);
drhbd03cae2001-06-02 02:40:57 +00001216 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00001217 rc = initPage(pNew, pCur->pgnoRoot, 0);
1218 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001219 sqlitepager_unref(pCur->pPage);
1220 pCur->pPage = pNew;
1221 pCur->idx = 0;
1222 return SQLITE_OK;
1223}
drh2af926b2001-05-15 00:39:25 +00001224
drh5e2f8b92001-05-28 00:41:15 +00001225/*
1226** Move the cursor down to the left-most leaf entry beneath the
1227** entry to which it is currently pointing.
1228*/
1229static int moveToLeftmost(BtCursor *pCur){
1230 Pgno pgno;
1231 int rc;
1232
1233 while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
1234 rc = moveToChild(pCur, pgno);
1235 if( rc ) return rc;
1236 }
1237 return SQLITE_OK;
1238}
1239
drh5e00f6c2001-09-13 13:46:56 +00001240/* Move the cursor to the first entry in the table. Return SQLITE_OK
1241** on success. Set *pRes to 0 if the cursor actually points to something
1242** or set *pRes to 1 if the table is empty and there is no first element.
1243*/
1244int sqliteBtreeFirst(BtCursor *pCur, int *pRes){
1245 int rc;
drhecdc7532001-09-23 02:35:53 +00001246 if( pCur->pPage==0 ) return SQLITE_ABORT;
drh5e00f6c2001-09-13 13:46:56 +00001247 rc = moveToRoot(pCur);
1248 if( rc ) return rc;
1249 if( pCur->pPage->nCell==0 ){
1250 *pRes = 1;
1251 return SQLITE_OK;
1252 }
1253 *pRes = 0;
1254 rc = moveToLeftmost(pCur);
1255 return rc;
1256}
drh5e2f8b92001-05-28 00:41:15 +00001257
drha059ad02001-04-17 20:09:11 +00001258/* Move the cursor so that it points to an entry near pKey.
drh72f82862001-05-24 21:06:34 +00001259** Return a success code.
1260**
drh5e2f8b92001-05-28 00:41:15 +00001261** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00001262** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00001263** were present. The cursor might point to an entry that comes
1264** before or after the key.
1265**
drhbd03cae2001-06-02 02:40:57 +00001266** The result of comparing the key with the entry to which the
1267** cursor is left pointing is stored in pCur->iMatch. The same
1268** value is also written to *pRes if pRes!=NULL. The meaning of
1269** this value is as follows:
1270**
1271** *pRes<0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001272** is smaller than pKey.
drhbd03cae2001-06-02 02:40:57 +00001273**
1274** *pRes==0 The cursor is left pointing at an entry that
1275** exactly matches pKey.
1276**
1277** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001278** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00001279*/
drh5c4d9702001-08-20 00:33:58 +00001280int sqliteBtreeMoveto(BtCursor *pCur, const void *pKey, int nKey, int *pRes){
drh72f82862001-05-24 21:06:34 +00001281 int rc;
drhecdc7532001-09-23 02:35:53 +00001282 if( pCur->pPage==0 ) return SQLITE_ABORT;
drh7c717f72001-06-24 20:39:41 +00001283 pCur->bSkipNext = 0;
drh5e2f8b92001-05-28 00:41:15 +00001284 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00001285 if( rc ) return rc;
1286 for(;;){
1287 int lwr, upr;
1288 Pgno chldPg;
1289 MemPage *pPage = pCur->pPage;
drh8b2f49b2001-06-08 00:21:52 +00001290 int c = -1;
drh72f82862001-05-24 21:06:34 +00001291 lwr = 0;
1292 upr = pPage->nCell-1;
1293 while( lwr<=upr ){
drh72f82862001-05-24 21:06:34 +00001294 pCur->idx = (lwr+upr)/2;
drh8721ce42001-11-07 14:22:00 +00001295 rc = sqliteBtreeKeyCompare(pCur, pKey, nKey, 0, &c);
drh72f82862001-05-24 21:06:34 +00001296 if( rc ) return rc;
1297 if( c==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001298 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001299 if( pRes ) *pRes = 0;
1300 return SQLITE_OK;
1301 }
1302 if( c<0 ){
1303 lwr = pCur->idx+1;
1304 }else{
1305 upr = pCur->idx-1;
1306 }
1307 }
1308 assert( lwr==upr+1 );
1309 if( lwr>=pPage->nCell ){
drh14acc042001-06-10 19:56:58 +00001310 chldPg = pPage->u.hdr.rightChild;
drh72f82862001-05-24 21:06:34 +00001311 }else{
drh5e2f8b92001-05-28 00:41:15 +00001312 chldPg = pPage->apCell[lwr]->h.leftChild;
drh72f82862001-05-24 21:06:34 +00001313 }
1314 if( chldPg==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001315 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001316 if( pRes ) *pRes = c;
1317 return SQLITE_OK;
1318 }
drh5e2f8b92001-05-28 00:41:15 +00001319 rc = moveToChild(pCur, chldPg);
drh72f82862001-05-24 21:06:34 +00001320 if( rc ) return rc;
1321 }
drhbd03cae2001-06-02 02:40:57 +00001322 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00001323}
1324
1325/*
drhbd03cae2001-06-02 02:40:57 +00001326** Advance the cursor to the next entry in the database. If
1327** successful and pRes!=NULL then set *pRes=0. If the cursor
1328** was already pointing to the last entry in the database before
1329** this routine was called, then set *pRes=1 if pRes!=NULL.
drh72f82862001-05-24 21:06:34 +00001330*/
1331int sqliteBtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00001332 int rc;
drhecdc7532001-09-23 02:35:53 +00001333 if( pCur->pPage==0 ){
drh1bee3d72001-10-15 00:44:35 +00001334 if( pRes ) *pRes = 1;
drhecdc7532001-09-23 02:35:53 +00001335 return SQLITE_ABORT;
1336 }
drhf5bf0a72001-11-23 00:24:12 +00001337 if( pCur->bSkipNext && pCur->idx<pCur->pPage->nCell ){
drh5e2f8b92001-05-28 00:41:15 +00001338 pCur->bSkipNext = 0;
drh72f82862001-05-24 21:06:34 +00001339 if( pRes ) *pRes = 0;
1340 return SQLITE_OK;
1341 }
drh72f82862001-05-24 21:06:34 +00001342 pCur->idx++;
drh5e2f8b92001-05-28 00:41:15 +00001343 if( pCur->idx>=pCur->pPage->nCell ){
drh8c42ca92001-06-22 19:15:00 +00001344 if( pCur->pPage->u.hdr.rightChild ){
1345 rc = moveToChild(pCur, pCur->pPage->u.hdr.rightChild);
drh5e2f8b92001-05-28 00:41:15 +00001346 if( rc ) return rc;
1347 rc = moveToLeftmost(pCur);
1348 if( rc ) return rc;
1349 if( pRes ) *pRes = 0;
drh72f82862001-05-24 21:06:34 +00001350 return SQLITE_OK;
1351 }
drh5e2f8b92001-05-28 00:41:15 +00001352 do{
drh8c42ca92001-06-22 19:15:00 +00001353 if( pCur->pPage->pParent==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001354 if( pRes ) *pRes = 1;
1355 return SQLITE_OK;
1356 }
1357 rc = moveToParent(pCur);
1358 if( rc ) return rc;
1359 }while( pCur->idx>=pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00001360 if( pRes ) *pRes = 0;
1361 return SQLITE_OK;
1362 }
drh5e2f8b92001-05-28 00:41:15 +00001363 rc = moveToLeftmost(pCur);
1364 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001365 if( pRes ) *pRes = 0;
1366 return SQLITE_OK;
1367}
1368
drh3b7511c2001-05-26 13:15:44 +00001369/*
1370** Allocate a new page from the database file.
1371**
1372** The new page is marked as dirty. (In other words, sqlitepager_write()
1373** has already been called on the new page.) The new page has also
1374** been referenced and the calling routine is responsible for calling
1375** sqlitepager_unref() on the new page when it is done.
1376**
1377** SQLITE_OK is returned on success. Any other return value indicates
1378** an error. *ppPage and *pPgno are undefined in the event of an error.
1379** Do not invoke sqlitepager_unref() on *ppPage if an error is returned.
1380*/
1381static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno){
drhbd03cae2001-06-02 02:40:57 +00001382 PageOne *pPage1 = pBt->page1;
drh8c42ca92001-06-22 19:15:00 +00001383 int rc;
drh3b7511c2001-05-26 13:15:44 +00001384 if( pPage1->freeList ){
1385 OverflowPage *pOvfl;
1386 rc = sqlitepager_write(pPage1);
1387 if( rc ) return rc;
1388 *pPgno = pPage1->freeList;
drh8c42ca92001-06-22 19:15:00 +00001389 rc = sqlitepager_get(pBt->pPager, pPage1->freeList, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001390 if( rc ) return rc;
1391 rc = sqlitepager_write(pOvfl);
1392 if( rc ){
1393 sqlitepager_unref(pOvfl);
1394 return rc;
1395 }
drh14acc042001-06-10 19:56:58 +00001396 pPage1->freeList = pOvfl->iNext;
drh2aa679f2001-06-25 02:11:07 +00001397 pPage1->nFree--;
drh3b7511c2001-05-26 13:15:44 +00001398 *ppPage = (MemPage*)pOvfl;
1399 }else{
drh2aa679f2001-06-25 02:11:07 +00001400 *pPgno = sqlitepager_pagecount(pBt->pPager) + 1;
drh8c42ca92001-06-22 19:15:00 +00001401 rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
drh3b7511c2001-05-26 13:15:44 +00001402 if( rc ) return rc;
1403 rc = sqlitepager_write(*ppPage);
1404 }
1405 return rc;
1406}
1407
1408/*
1409** Add a page of the database file to the freelist. Either pgno or
1410** pPage but not both may be 0.
drh5e2f8b92001-05-28 00:41:15 +00001411**
drhdd793422001-06-28 01:54:48 +00001412** sqlitepager_unref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00001413*/
1414static int freePage(Btree *pBt, void *pPage, Pgno pgno){
drhbd03cae2001-06-02 02:40:57 +00001415 PageOne *pPage1 = pBt->page1;
drh3b7511c2001-05-26 13:15:44 +00001416 OverflowPage *pOvfl = (OverflowPage*)pPage;
1417 int rc;
drhdd793422001-06-28 01:54:48 +00001418 int needUnref = 0;
1419 MemPage *pMemPage;
drh8b2f49b2001-06-08 00:21:52 +00001420
drh3b7511c2001-05-26 13:15:44 +00001421 if( pgno==0 ){
1422 assert( pOvfl!=0 );
1423 pgno = sqlitepager_pagenumber(pOvfl);
1424 }
drh2aa679f2001-06-25 02:11:07 +00001425 assert( pgno>2 );
drh3b7511c2001-05-26 13:15:44 +00001426 rc = sqlitepager_write(pPage1);
1427 if( rc ){
1428 return rc;
1429 }
1430 if( pOvfl==0 ){
1431 assert( pgno>0 );
drh8c42ca92001-06-22 19:15:00 +00001432 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001433 if( rc ) return rc;
drhdd793422001-06-28 01:54:48 +00001434 needUnref = 1;
drh3b7511c2001-05-26 13:15:44 +00001435 }
1436 rc = sqlitepager_write(pOvfl);
1437 if( rc ){
drhdd793422001-06-28 01:54:48 +00001438 if( needUnref ) sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001439 return rc;
1440 }
drh14acc042001-06-10 19:56:58 +00001441 pOvfl->iNext = pPage1->freeList;
drh3b7511c2001-05-26 13:15:44 +00001442 pPage1->freeList = pgno;
drh2aa679f2001-06-25 02:11:07 +00001443 pPage1->nFree++;
drh5e2f8b92001-05-28 00:41:15 +00001444 memset(pOvfl->aPayload, 0, OVERFLOW_SIZE);
drhdd793422001-06-28 01:54:48 +00001445 pMemPage = (MemPage*)pPage;
1446 pMemPage->isInit = 0;
1447 if( pMemPage->pParent ){
1448 sqlitepager_unref(pMemPage->pParent);
1449 pMemPage->pParent = 0;
1450 }
1451 if( needUnref ) rc = sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001452 return rc;
1453}
1454
1455/*
1456** Erase all the data out of a cell. This involves returning overflow
1457** pages back the freelist.
1458*/
1459static int clearCell(Btree *pBt, Cell *pCell){
1460 Pager *pPager = pBt->pPager;
1461 OverflowPage *pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001462 Pgno ovfl, nextOvfl;
1463 int rc;
1464
drh80ff32f2001-11-04 18:32:46 +00001465 if( NKEY(pCell->h) + NDATA(pCell->h) <= MX_LOCAL_PAYLOAD ){
drh5e2f8b92001-05-28 00:41:15 +00001466 return SQLITE_OK;
1467 }
drh3b7511c2001-05-26 13:15:44 +00001468 ovfl = pCell->ovfl;
1469 pCell->ovfl = 0;
1470 while( ovfl ){
drh8c42ca92001-06-22 19:15:00 +00001471 rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001472 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001473 nextOvfl = pOvfl->iNext;
drhbd03cae2001-06-02 02:40:57 +00001474 rc = freePage(pBt, pOvfl, ovfl);
1475 if( rc ) return rc;
drhdd793422001-06-28 01:54:48 +00001476 sqlitepager_unref(pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001477 ovfl = nextOvfl;
drh3b7511c2001-05-26 13:15:44 +00001478 }
drh5e2f8b92001-05-28 00:41:15 +00001479 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00001480}
1481
1482/*
1483** Create a new cell from key and data. Overflow pages are allocated as
1484** necessary and linked to this cell.
1485*/
1486static int fillInCell(
1487 Btree *pBt, /* The whole Btree. Needed to allocate pages */
1488 Cell *pCell, /* Populate this Cell structure */
drh5c4d9702001-08-20 00:33:58 +00001489 const void *pKey, int nKey, /* The key */
1490 const void *pData,int nData /* The data */
drh3b7511c2001-05-26 13:15:44 +00001491){
drhdd793422001-06-28 01:54:48 +00001492 OverflowPage *pOvfl, *pPrior;
drh3b7511c2001-05-26 13:15:44 +00001493 Pgno *pNext;
1494 int spaceLeft;
drh8c42ca92001-06-22 19:15:00 +00001495 int n, rc;
drh3b7511c2001-05-26 13:15:44 +00001496 int nPayload;
drh5c4d9702001-08-20 00:33:58 +00001497 const char *pPayload;
drh3b7511c2001-05-26 13:15:44 +00001498 char *pSpace;
1499
drh5e2f8b92001-05-28 00:41:15 +00001500 pCell->h.leftChild = 0;
drh80ff32f2001-11-04 18:32:46 +00001501 pCell->h.nKey = nKey & 0xffff;
1502 pCell->h.nKeyHi = nKey >> 16;
1503 pCell->h.nData = nData & 0xffff;
1504 pCell->h.nDataHi = nData >> 16;
drh3b7511c2001-05-26 13:15:44 +00001505 pCell->h.iNext = 0;
1506
1507 pNext = &pCell->ovfl;
drh5e2f8b92001-05-28 00:41:15 +00001508 pSpace = pCell->aPayload;
drh3b7511c2001-05-26 13:15:44 +00001509 spaceLeft = MX_LOCAL_PAYLOAD;
1510 pPayload = pKey;
1511 pKey = 0;
1512 nPayload = nKey;
drhdd793422001-06-28 01:54:48 +00001513 pPrior = 0;
drh3b7511c2001-05-26 13:15:44 +00001514 while( nPayload>0 ){
1515 if( spaceLeft==0 ){
drh8c42ca92001-06-22 19:15:00 +00001516 rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext);
drh3b7511c2001-05-26 13:15:44 +00001517 if( rc ){
1518 *pNext = 0;
drhdd793422001-06-28 01:54:48 +00001519 }
1520 if( pPrior ) sqlitepager_unref(pPrior);
1521 if( rc ){
drh5e2f8b92001-05-28 00:41:15 +00001522 clearCell(pBt, pCell);
drh3b7511c2001-05-26 13:15:44 +00001523 return rc;
1524 }
drhdd793422001-06-28 01:54:48 +00001525 pPrior = pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001526 spaceLeft = OVERFLOW_SIZE;
drh5e2f8b92001-05-28 00:41:15 +00001527 pSpace = pOvfl->aPayload;
drh8c42ca92001-06-22 19:15:00 +00001528 pNext = &pOvfl->iNext;
drh3b7511c2001-05-26 13:15:44 +00001529 }
1530 n = nPayload;
1531 if( n>spaceLeft ) n = spaceLeft;
1532 memcpy(pSpace, pPayload, n);
1533 nPayload -= n;
1534 if( nPayload==0 && pData ){
1535 pPayload = pData;
1536 nPayload = nData;
1537 pData = 0;
1538 }else{
1539 pPayload += n;
1540 }
1541 spaceLeft -= n;
1542 pSpace += n;
1543 }
drhdd793422001-06-28 01:54:48 +00001544 *pNext = 0;
1545 if( pPrior ){
1546 sqlitepager_unref(pPrior);
1547 }
drh3b7511c2001-05-26 13:15:44 +00001548 return SQLITE_OK;
1549}
1550
1551/*
drhbd03cae2001-06-02 02:40:57 +00001552** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00001553** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00001554** pointer in the third argument.
1555*/
1556static void reparentPage(Pager *pPager, Pgno pgno, MemPage *pNewParent){
1557 MemPage *pThis;
1558
drhdd793422001-06-28 01:54:48 +00001559 if( pgno==0 ) return;
1560 assert( pPager!=0 );
drhbd03cae2001-06-02 02:40:57 +00001561 pThis = sqlitepager_lookup(pPager, pgno);
drh6019e162001-07-02 17:51:45 +00001562 if( pThis && pThis->isInit ){
drhdd793422001-06-28 01:54:48 +00001563 if( pThis->pParent!=pNewParent ){
1564 if( pThis->pParent ) sqlitepager_unref(pThis->pParent);
1565 pThis->pParent = pNewParent;
1566 if( pNewParent ) sqlitepager_ref(pNewParent);
1567 }
1568 sqlitepager_unref(pThis);
drhbd03cae2001-06-02 02:40:57 +00001569 }
1570}
1571
1572/*
1573** Reparent all children of the given page to be the given page.
1574** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00001575** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00001576**
1577** This routine gets called after you memcpy() one page into
1578** another.
1579*/
drh8c42ca92001-06-22 19:15:00 +00001580static void reparentChildPages(Pager *pPager, MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00001581 int i;
1582 for(i=0; i<pPage->nCell; i++){
drh8c42ca92001-06-22 19:15:00 +00001583 reparentPage(pPager, pPage->apCell[i]->h.leftChild, pPage);
drhbd03cae2001-06-02 02:40:57 +00001584 }
drh14acc042001-06-10 19:56:58 +00001585 reparentPage(pPager, pPage->u.hdr.rightChild, pPage);
1586}
1587
1588/*
1589** Remove the i-th cell from pPage. This routine effects pPage only.
1590** The cell content is not freed or deallocated. It is assumed that
1591** the cell content has been copied someplace else. This routine just
1592** removes the reference to the cell from pPage.
1593**
1594** "sz" must be the number of bytes in the cell.
1595**
1596** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001597** Only the pPage->apCell[] array is important. The relinkCellList()
1598** routine will be called soon after this routine in order to rebuild
1599** the linked list.
drh14acc042001-06-10 19:56:58 +00001600*/
drh8c42ca92001-06-22 19:15:00 +00001601static void dropCell(MemPage *pPage, int idx, int sz){
drh14acc042001-06-10 19:56:58 +00001602 int j;
drh8c42ca92001-06-22 19:15:00 +00001603 assert( idx>=0 && idx<pPage->nCell );
1604 assert( sz==cellSize(pPage->apCell[idx]) );
drh6019e162001-07-02 17:51:45 +00001605 assert( sqlitepager_iswriteable(pPage) );
drh7c717f72001-06-24 20:39:41 +00001606 freeSpace(pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz);
1607 for(j=idx; j<pPage->nCell-1; j++){
drh14acc042001-06-10 19:56:58 +00001608 pPage->apCell[j] = pPage->apCell[j+1];
1609 }
1610 pPage->nCell--;
1611}
1612
1613/*
1614** Insert a new cell on pPage at cell index "i". pCell points to the
1615** content of the cell.
1616**
1617** If the cell content will fit on the page, then put it there. If it
1618** will not fit, then just make pPage->apCell[i] point to the content
1619** and set pPage->isOverfull.
1620**
1621** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001622** Only the pPage->apCell[] array is important. The relinkCellList()
1623** routine will be called soon after this routine in order to rebuild
1624** the linked list.
drh14acc042001-06-10 19:56:58 +00001625*/
1626static void insertCell(MemPage *pPage, int i, Cell *pCell, int sz){
1627 int idx, j;
1628 assert( i>=0 && i<=pPage->nCell );
1629 assert( sz==cellSize(pCell) );
drh6019e162001-07-02 17:51:45 +00001630 assert( sqlitepager_iswriteable(pPage) );
drh2aa679f2001-06-25 02:11:07 +00001631 idx = allocateSpace(pPage, sz);
drh14acc042001-06-10 19:56:58 +00001632 for(j=pPage->nCell; j>i; j--){
1633 pPage->apCell[j] = pPage->apCell[j-1];
1634 }
1635 pPage->nCell++;
drh14acc042001-06-10 19:56:58 +00001636 if( idx<=0 ){
1637 pPage->isOverfull = 1;
1638 pPage->apCell[i] = pCell;
1639 }else{
1640 memcpy(&pPage->u.aDisk[idx], pCell, sz);
drh8c42ca92001-06-22 19:15:00 +00001641 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx];
drh14acc042001-06-10 19:56:58 +00001642 }
1643}
1644
1645/*
1646** Rebuild the linked list of cells on a page so that the cells
drh8c42ca92001-06-22 19:15:00 +00001647** occur in the order specified by the pPage->apCell[] array.
1648** Invoke this routine once to repair damage after one or more
1649** invocations of either insertCell() or dropCell().
drh14acc042001-06-10 19:56:58 +00001650*/
1651static void relinkCellList(MemPage *pPage){
1652 int i;
1653 u16 *pIdx;
drh6019e162001-07-02 17:51:45 +00001654 assert( sqlitepager_iswriteable(pPage) );
drh14acc042001-06-10 19:56:58 +00001655 pIdx = &pPage->u.hdr.firstCell;
1656 for(i=0; i<pPage->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001657 int idx = Addr(pPage->apCell[i]) - Addr(pPage);
drh8c42ca92001-06-22 19:15:00 +00001658 assert( idx>0 && idx<SQLITE_PAGE_SIZE );
drh14acc042001-06-10 19:56:58 +00001659 *pIdx = idx;
1660 pIdx = &pPage->apCell[i]->h.iNext;
1661 }
1662 *pIdx = 0;
1663}
1664
1665/*
1666** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[]
drh5e00f6c2001-09-13 13:46:56 +00001667** pointers that point into pFrom->u.aDisk[] must be adjusted to point
drhdd793422001-06-28 01:54:48 +00001668** into pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might
drh14acc042001-06-10 19:56:58 +00001669** not point to pFrom->u.aDisk[]. Those are unchanged.
1670*/
1671static void copyPage(MemPage *pTo, MemPage *pFrom){
1672 uptr from, to;
1673 int i;
1674 memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_PAGE_SIZE);
drhdd793422001-06-28 01:54:48 +00001675 pTo->pParent = 0;
drh14acc042001-06-10 19:56:58 +00001676 pTo->isInit = 1;
1677 pTo->nCell = pFrom->nCell;
1678 pTo->nFree = pFrom->nFree;
1679 pTo->isOverfull = pFrom->isOverfull;
drh7c717f72001-06-24 20:39:41 +00001680 to = Addr(pTo);
1681 from = Addr(pFrom);
drh14acc042001-06-10 19:56:58 +00001682 for(i=0; i<pTo->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001683 uptr x = Addr(pFrom->apCell[i]);
drh8c42ca92001-06-22 19:15:00 +00001684 if( x>from && x<from+SQLITE_PAGE_SIZE ){
1685 *((uptr*)&pTo->apCell[i]) = x + to - from;
drhdd793422001-06-28 01:54:48 +00001686 }else{
1687 pTo->apCell[i] = pFrom->apCell[i];
drh14acc042001-06-10 19:56:58 +00001688 }
1689 }
drhbd03cae2001-06-02 02:40:57 +00001690}
1691
1692/*
drh8b2f49b2001-06-08 00:21:52 +00001693** This routine redistributes Cells on pPage and up to two siblings
1694** of pPage so that all pages have about the same amount of free space.
drh14acc042001-06-10 19:56:58 +00001695** Usually one sibling on either side of pPage is used in the balancing,
drh8b2f49b2001-06-08 00:21:52 +00001696** though both siblings might come from one side if pPage is the first
1697** or last child of its parent. If pPage has fewer than two siblings
1698** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00001699** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00001700**
1701** The number of siblings of pPage might be increased or decreased by
drh8c42ca92001-06-22 19:15:00 +00001702** one in an effort to keep pages between 66% and 100% full. The root page
1703** is special and is allowed to be less than 66% full. If pPage is
1704** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00001705** or decreased by one, as necessary, to keep the root page from being
1706** overfull or empty.
1707**
drh14acc042001-06-10 19:56:58 +00001708** This routine calls relinkCellList() on its input page regardless of
1709** whether or not it does any real balancing. Client routines will typically
1710** invoke insertCell() or dropCell() before calling this routine, so we
1711** need to call relinkCellList() to clean up the mess that those other
1712** routines left behind.
1713**
1714** pCur is left pointing to the same cell as when this routine was called
drh8c42ca92001-06-22 19:15:00 +00001715** even if that cell gets moved to a different page. pCur may be NULL.
1716** Set the pCur parameter to NULL if you do not care about keeping track
1717** of a cell as that will save this routine the work of keeping track of it.
drh14acc042001-06-10 19:56:58 +00001718**
drh8b2f49b2001-06-08 00:21:52 +00001719** Note that when this routine is called, some of the Cells on pPage
drh14acc042001-06-10 19:56:58 +00001720** might not actually be stored in pPage->u.aDisk[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00001721** if the page is overfull. Part of the job of this routine is to
drh14acc042001-06-10 19:56:58 +00001722** make sure all Cells for pPage once again fit in pPage->u.aDisk[].
1723**
drh8c42ca92001-06-22 19:15:00 +00001724** In the course of balancing the siblings of pPage, the parent of pPage
1725** might become overfull or underfull. If that happens, then this routine
1726** is called recursively on the parent.
1727**
drh5e00f6c2001-09-13 13:46:56 +00001728** If this routine fails for any reason, it might leave the database
1729** in a corrupted state. So if this routine fails, the database should
1730** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00001731*/
drh14acc042001-06-10 19:56:58 +00001732static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
drh8b2f49b2001-06-08 00:21:52 +00001733 MemPage *pParent; /* The parent of pPage */
drh14acc042001-06-10 19:56:58 +00001734 MemPage *apOld[3]; /* pPage and up to two siblings */
drh8b2f49b2001-06-08 00:21:52 +00001735 Pgno pgnoOld[3]; /* Page numbers for each page in apOld[] */
drh14acc042001-06-10 19:56:58 +00001736 MemPage *apNew[4]; /* pPage and up to 3 siblings after balancing */
1737 Pgno pgnoNew[4]; /* Page numbers for each page in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00001738 int idxDiv[3]; /* Indices of divider cells in pParent */
1739 Cell *apDiv[3]; /* Divider cells in pParent */
1740 int nCell; /* Number of cells in apCell[] */
1741 int nOld; /* Number of pages in apOld[] */
1742 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00001743 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00001744 int i, j, k; /* Loop counters */
1745 int idx; /* Index of pPage in pParent->apCell[] */
1746 int nxDiv; /* Next divider slot in pParent->apCell[] */
1747 int rc; /* The return code */
1748 int iCur; /* apCell[iCur] is the cell of the cursor */
drh5edc3122001-09-13 21:53:09 +00001749 MemPage *pOldCurPage; /* The cursor originally points to this page */
drh8c42ca92001-06-22 19:15:00 +00001750 int totalSize; /* Total bytes for all cells */
drh6019e162001-07-02 17:51:45 +00001751 int subtotal; /* Subtotal of bytes in cells on one page */
1752 int cntNew[4]; /* Index in apCell[] of cell after i-th page */
1753 int szNew[4]; /* Combined size of cells place on i-th page */
drh9ca7d3b2001-06-28 11:50:21 +00001754 MemPage *extraUnref = 0; /* A page that needs to be unref-ed */
drh8c42ca92001-06-22 19:15:00 +00001755 Pgno pgno; /* Page number */
drh14acc042001-06-10 19:56:58 +00001756 Cell *apCell[MX_CELL*3+5]; /* All cells from pages being balanceed */
1757 int szCell[MX_CELL*3+5]; /* Local size of all cells */
1758 Cell aTemp[2]; /* Temporary holding area for apDiv[] */
1759 MemPage aOld[3]; /* Temporary copies of pPage and its siblings */
drh8b2f49b2001-06-08 00:21:52 +00001760
drh14acc042001-06-10 19:56:58 +00001761 /*
1762 ** Return without doing any work if pPage is neither overfull nor
1763 ** underfull.
drh8b2f49b2001-06-08 00:21:52 +00001764 */
drh6019e162001-07-02 17:51:45 +00001765 assert( sqlitepager_iswriteable(pPage) );
drha1b351a2001-09-14 16:42:12 +00001766 if( !pPage->isOverfull && pPage->nFree<SQLITE_PAGE_SIZE/2
1767 && pPage->nCell>=2){
drh14acc042001-06-10 19:56:58 +00001768 relinkCellList(pPage);
drh8b2f49b2001-06-08 00:21:52 +00001769 return SQLITE_OK;
1770 }
1771
1772 /*
drh14acc042001-06-10 19:56:58 +00001773 ** Find the parent of the page to be balanceed.
1774 ** If there is no parent, it means this page is the root page and
drh8b2f49b2001-06-08 00:21:52 +00001775 ** special rules apply.
1776 */
drh14acc042001-06-10 19:56:58 +00001777 pParent = pPage->pParent;
drh8b2f49b2001-06-08 00:21:52 +00001778 if( pParent==0 ){
1779 Pgno pgnoChild;
drh8c42ca92001-06-22 19:15:00 +00001780 MemPage *pChild;
drh8b2f49b2001-06-08 00:21:52 +00001781 if( pPage->nCell==0 ){
drh14acc042001-06-10 19:56:58 +00001782 if( pPage->u.hdr.rightChild ){
1783 /*
1784 ** The root page is empty. Copy the one child page
drh8b2f49b2001-06-08 00:21:52 +00001785 ** into the root page and return. This reduces the depth
1786 ** of the BTree by one.
1787 */
drh14acc042001-06-10 19:56:58 +00001788 pgnoChild = pPage->u.hdr.rightChild;
drh8c42ca92001-06-22 19:15:00 +00001789 rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild);
drh8b2f49b2001-06-08 00:21:52 +00001790 if( rc ) return rc;
1791 memcpy(pPage, pChild, SQLITE_PAGE_SIZE);
1792 pPage->isInit = 0;
drh6019e162001-07-02 17:51:45 +00001793 rc = initPage(pPage, sqlitepager_pagenumber(pPage), 0);
1794 assert( rc==SQLITE_OK );
drh8b2f49b2001-06-08 00:21:52 +00001795 reparentChildPages(pBt->pPager, pPage);
drh5edc3122001-09-13 21:53:09 +00001796 if( pCur && pCur->pPage==pChild ){
1797 sqlitepager_unref(pChild);
1798 pCur->pPage = pPage;
1799 sqlitepager_ref(pPage);
1800 }
drh8b2f49b2001-06-08 00:21:52 +00001801 freePage(pBt, pChild, pgnoChild);
1802 sqlitepager_unref(pChild);
drhefc251d2001-07-01 22:12:01 +00001803 }else{
1804 relinkCellList(pPage);
drh8b2f49b2001-06-08 00:21:52 +00001805 }
1806 return SQLITE_OK;
1807 }
drh14acc042001-06-10 19:56:58 +00001808 if( !pPage->isOverfull ){
drh8b2f49b2001-06-08 00:21:52 +00001809 /* It is OK for the root page to be less than half full.
1810 */
drh14acc042001-06-10 19:56:58 +00001811 relinkCellList(pPage);
drh8b2f49b2001-06-08 00:21:52 +00001812 return SQLITE_OK;
1813 }
drh14acc042001-06-10 19:56:58 +00001814 /*
1815 ** If we get to here, it means the root page is overfull.
drh8b2f49b2001-06-08 00:21:52 +00001816 ** When this happens, Create a new child page and copy the
1817 ** contents of the root into the child. Then make the root
drh14acc042001-06-10 19:56:58 +00001818 ** page an empty page with rightChild pointing to the new
drh8b2f49b2001-06-08 00:21:52 +00001819 ** child. Then fall thru to the code below which will cause
1820 ** the overfull child page to be split.
1821 */
drh14acc042001-06-10 19:56:58 +00001822 rc = sqlitepager_write(pPage);
1823 if( rc ) return rc;
drh8b2f49b2001-06-08 00:21:52 +00001824 rc = allocatePage(pBt, &pChild, &pgnoChild);
1825 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00001826 assert( sqlitepager_iswriteable(pChild) );
drh14acc042001-06-10 19:56:58 +00001827 copyPage(pChild, pPage);
1828 pChild->pParent = pPage;
drhdd793422001-06-28 01:54:48 +00001829 sqlitepager_ref(pPage);
drh14acc042001-06-10 19:56:58 +00001830 pChild->isOverfull = 1;
drh5edc3122001-09-13 21:53:09 +00001831 if( pCur && pCur->pPage==pPage ){
1832 sqlitepager_unref(pPage);
drh14acc042001-06-10 19:56:58 +00001833 pCur->pPage = pChild;
drh9ca7d3b2001-06-28 11:50:21 +00001834 }else{
1835 extraUnref = pChild;
drh8b2f49b2001-06-08 00:21:52 +00001836 }
drh8b2f49b2001-06-08 00:21:52 +00001837 zeroPage(pPage);
drh14acc042001-06-10 19:56:58 +00001838 pPage->u.hdr.rightChild = pgnoChild;
drh8b2f49b2001-06-08 00:21:52 +00001839 pParent = pPage;
1840 pPage = pChild;
drh8b2f49b2001-06-08 00:21:52 +00001841 }
drh6019e162001-07-02 17:51:45 +00001842 rc = sqlitepager_write(pParent);
1843 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001844
drh8b2f49b2001-06-08 00:21:52 +00001845 /*
drh14acc042001-06-10 19:56:58 +00001846 ** Find the Cell in the parent page whose h.leftChild points back
1847 ** to pPage. The "idx" variable is the index of that cell. If pPage
1848 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00001849 */
1850 idx = -1;
1851 pgno = sqlitepager_pagenumber(pPage);
1852 for(i=0; i<pParent->nCell; i++){
1853 if( pParent->apCell[i]->h.leftChild==pgno ){
1854 idx = i;
1855 break;
1856 }
1857 }
drhdd793422001-06-28 01:54:48 +00001858 if( idx<0 && pParent->u.hdr.rightChild==pgno ){
1859 idx = pParent->nCell;
drh8b2f49b2001-06-08 00:21:52 +00001860 }
1861 if( idx<0 ){
drh14acc042001-06-10 19:56:58 +00001862 return SQLITE_CORRUPT;
drh8b2f49b2001-06-08 00:21:52 +00001863 }
1864
1865 /*
drh14acc042001-06-10 19:56:58 +00001866 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00001867 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00001868 */
drh14acc042001-06-10 19:56:58 +00001869 nOld = nNew = 0;
1870 sqlitepager_ref(pParent);
1871
1872 /*
1873 ** Find sibling pages to pPage and the Cells in pParent that divide
1874 ** the siblings. An attempt is made to find one sibling on either
1875 ** side of pPage. Both siblings are taken from one side, however, if
1876 ** pPage is either the first or last child of its parent. If pParent
1877 ** has 3 or fewer children then all children of pParent are taken.
1878 */
1879 if( idx==pParent->nCell ){
1880 nxDiv = idx - 2;
drh8b2f49b2001-06-08 00:21:52 +00001881 }else{
drh14acc042001-06-10 19:56:58 +00001882 nxDiv = idx - 1;
drh8b2f49b2001-06-08 00:21:52 +00001883 }
drh14acc042001-06-10 19:56:58 +00001884 if( nxDiv<0 ) nxDiv = 0;
drh8b2f49b2001-06-08 00:21:52 +00001885 nDiv = 0;
drh14acc042001-06-10 19:56:58 +00001886 for(i=0, k=nxDiv; i<3; i++, k++){
1887 if( k<pParent->nCell ){
1888 idxDiv[i] = k;
1889 apDiv[i] = pParent->apCell[k];
drh8b2f49b2001-06-08 00:21:52 +00001890 nDiv++;
1891 pgnoOld[i] = apDiv[i]->h.leftChild;
drh14acc042001-06-10 19:56:58 +00001892 }else if( k==pParent->nCell ){
drh8c42ca92001-06-22 19:15:00 +00001893 pgnoOld[i] = pParent->u.hdr.rightChild;
drh14acc042001-06-10 19:56:58 +00001894 }else{
1895 break;
drh8b2f49b2001-06-08 00:21:52 +00001896 }
drh8c42ca92001-06-22 19:15:00 +00001897 rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]);
drh14acc042001-06-10 19:56:58 +00001898 if( rc ) goto balance_cleanup;
drh6019e162001-07-02 17:51:45 +00001899 rc = initPage(apOld[i], pgnoOld[i], pParent);
1900 if( rc ) goto balance_cleanup;
drh14acc042001-06-10 19:56:58 +00001901 nOld++;
drh8b2f49b2001-06-08 00:21:52 +00001902 }
1903
1904 /*
drh14acc042001-06-10 19:56:58 +00001905 ** Set iCur to be the index in apCell[] of the cell that the cursor
1906 ** is pointing to. We will need this later on in order to keep the
drh5edc3122001-09-13 21:53:09 +00001907 ** cursor pointing at the same cell. If pCur points to a page that
1908 ** has no involvement with this rebalancing, then set iCur to a large
1909 ** number so that the iCur==j tests always fail in the main cell
1910 ** distribution loop below.
drh14acc042001-06-10 19:56:58 +00001911 */
1912 if( pCur ){
drh5edc3122001-09-13 21:53:09 +00001913 iCur = 0;
1914 for(i=0; i<nOld; i++){
1915 if( pCur->pPage==apOld[i] ){
1916 iCur += pCur->idx;
1917 break;
1918 }
1919 iCur += apOld[i]->nCell;
1920 if( i<nOld-1 && pCur->pPage==pParent && pCur->idx==idxDiv[i] ){
1921 break;
1922 }
1923 iCur++;
drh14acc042001-06-10 19:56:58 +00001924 }
drh5edc3122001-09-13 21:53:09 +00001925 pOldCurPage = pCur->pPage;
drh14acc042001-06-10 19:56:58 +00001926 }
1927
1928 /*
1929 ** Make copies of the content of pPage and its siblings into aOld[].
1930 ** The rest of this function will use data from the copies rather
1931 ** that the original pages since the original pages will be in the
1932 ** process of being overwritten.
1933 */
1934 for(i=0; i<nOld; i++){
1935 copyPage(&aOld[i], apOld[i]);
1936 rc = freePage(pBt, apOld[i], pgnoOld[i]);
1937 if( rc ) goto balance_cleanup;
drhdd793422001-06-28 01:54:48 +00001938 sqlitepager_unref(apOld[i]);
drh14acc042001-06-10 19:56:58 +00001939 apOld[i] = &aOld[i];
1940 }
1941
1942 /*
1943 ** Load pointers to all cells on sibling pages and the divider cells
1944 ** into the local apCell[] array. Make copies of the divider cells
1945 ** into aTemp[] and remove the the divider Cells from pParent.
drh8b2f49b2001-06-08 00:21:52 +00001946 */
1947 nCell = 0;
1948 for(i=0; i<nOld; i++){
1949 MemPage *pOld = apOld[i];
1950 for(j=0; j<pOld->nCell; j++){
drh14acc042001-06-10 19:56:58 +00001951 apCell[nCell] = pOld->apCell[j];
1952 szCell[nCell] = cellSize(apCell[nCell]);
1953 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00001954 }
1955 if( i<nOld-1 ){
drh14acc042001-06-10 19:56:58 +00001956 szCell[nCell] = cellSize(apDiv[i]);
drh8c42ca92001-06-22 19:15:00 +00001957 memcpy(&aTemp[i], apDiv[i], szCell[nCell]);
drh14acc042001-06-10 19:56:58 +00001958 apCell[nCell] = &aTemp[i];
1959 dropCell(pParent, nxDiv, szCell[nCell]);
1960 assert( apCell[nCell]->h.leftChild==pgnoOld[i] );
1961 apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild;
1962 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00001963 }
1964 }
1965
1966 /*
drh6019e162001-07-02 17:51:45 +00001967 ** Figure out the number of pages needed to hold all nCell cells.
1968 ** Store this number in "k". Also compute szNew[] which is the total
1969 ** size of all cells on the i-th page and cntNew[] which is the index
1970 ** in apCell[] of the cell that divides path i from path i+1.
1971 ** cntNew[k] should equal nCell.
1972 **
1973 ** This little patch of code is critical for keeping the tree
1974 ** balanced.
drh8b2f49b2001-06-08 00:21:52 +00001975 */
1976 totalSize = 0;
1977 for(i=0; i<nCell; i++){
drh14acc042001-06-10 19:56:58 +00001978 totalSize += szCell[i];
drh8b2f49b2001-06-08 00:21:52 +00001979 }
drh6019e162001-07-02 17:51:45 +00001980 for(subtotal=k=i=0; i<nCell; i++){
1981 subtotal += szCell[i];
1982 if( subtotal > USABLE_SPACE ){
1983 szNew[k] = subtotal - szCell[i];
1984 cntNew[k] = i;
1985 subtotal = 0;
1986 k++;
1987 }
1988 }
1989 szNew[k] = subtotal;
1990 cntNew[k] = nCell;
1991 k++;
1992 for(i=k-1; i>0; i--){
1993 while( szNew[i]<USABLE_SPACE/2 ){
1994 cntNew[i-1]--;
1995 assert( cntNew[i-1]>0 );
1996 szNew[i] += szCell[cntNew[i-1]];
1997 szNew[i-1] -= szCell[cntNew[i-1]-1];
1998 }
1999 }
2000 assert( cntNew[0]>0 );
drh8b2f49b2001-06-08 00:21:52 +00002001
2002 /*
drh6019e162001-07-02 17:51:45 +00002003 ** Allocate k new pages
drh8b2f49b2001-06-08 00:21:52 +00002004 */
drh14acc042001-06-10 19:56:58 +00002005 for(i=0; i<k; i++){
drh8b2f49b2001-06-08 00:21:52 +00002006 rc = allocatePage(pBt, &apNew[i], &pgnoNew[i]);
drh14acc042001-06-10 19:56:58 +00002007 if( rc ) goto balance_cleanup;
2008 nNew++;
drh8b2f49b2001-06-08 00:21:52 +00002009 zeroPage(apNew[i]);
drh6019e162001-07-02 17:51:45 +00002010 apNew[i]->isInit = 1;
drh8b2f49b2001-06-08 00:21:52 +00002011 }
2012
2013 /*
drh14acc042001-06-10 19:56:58 +00002014 ** Evenly distribute the data in apCell[] across the new pages.
2015 ** Insert divider cells into pParent as necessary.
2016 */
2017 j = 0;
2018 for(i=0; i<nNew; i++){
2019 MemPage *pNew = apNew[i];
drh6019e162001-07-02 17:51:45 +00002020 while( j<cntNew[i] ){
2021 assert( pNew->nFree>=szCell[j] );
drh14acc042001-06-10 19:56:58 +00002022 if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; }
2023 insertCell(pNew, pNew->nCell, apCell[j], szCell[j]);
2024 j++;
2025 }
drh6019e162001-07-02 17:51:45 +00002026 assert( pNew->nCell>0 );
drh14acc042001-06-10 19:56:58 +00002027 assert( !pNew->isOverfull );
2028 relinkCellList(pNew);
2029 if( i<nNew-1 && j<nCell ){
2030 pNew->u.hdr.rightChild = apCell[j]->h.leftChild;
2031 apCell[j]->h.leftChild = pgnoNew[i];
2032 if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; }
2033 insertCell(pParent, nxDiv, apCell[j], szCell[j]);
2034 j++;
2035 nxDiv++;
2036 }
2037 }
drh6019e162001-07-02 17:51:45 +00002038 assert( j==nCell );
drh14acc042001-06-10 19:56:58 +00002039 apNew[nNew-1]->u.hdr.rightChild = apOld[nOld-1]->u.hdr.rightChild;
2040 if( nxDiv==pParent->nCell ){
2041 pParent->u.hdr.rightChild = pgnoNew[nNew-1];
2042 }else{
2043 pParent->apCell[nxDiv]->h.leftChild = pgnoNew[nNew-1];
2044 }
2045 if( pCur ){
drh3fc190c2001-09-14 03:24:23 +00002046 if( j<=iCur && pCur->pPage==pParent && pCur->idx>idxDiv[nOld-1] ){
2047 assert( pCur->pPage==pOldCurPage );
2048 pCur->idx += nNew - nOld;
2049 }else{
2050 assert( pOldCurPage!=0 );
2051 sqlitepager_ref(pCur->pPage);
2052 sqlitepager_unref(pOldCurPage);
2053 }
drh14acc042001-06-10 19:56:58 +00002054 }
2055
2056 /*
2057 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00002058 */
2059 for(i=0; i<nNew; i++){
drh14acc042001-06-10 19:56:58 +00002060 reparentChildPages(pBt->pPager, apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00002061 }
drh14acc042001-06-10 19:56:58 +00002062 reparentChildPages(pBt->pPager, pParent);
drh8b2f49b2001-06-08 00:21:52 +00002063
2064 /*
drh14acc042001-06-10 19:56:58 +00002065 ** balance the parent page.
drh8b2f49b2001-06-08 00:21:52 +00002066 */
drh5edc3122001-09-13 21:53:09 +00002067 rc = balance(pBt, pParent, pCur);
drh8b2f49b2001-06-08 00:21:52 +00002068
2069 /*
drh14acc042001-06-10 19:56:58 +00002070 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00002071 */
drh14acc042001-06-10 19:56:58 +00002072balance_cleanup:
drh9ca7d3b2001-06-28 11:50:21 +00002073 if( extraUnref ){
2074 sqlitepager_unref(extraUnref);
2075 }
drh8b2f49b2001-06-08 00:21:52 +00002076 for(i=0; i<nOld; i++){
drhdd793422001-06-28 01:54:48 +00002077 if( apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00002078 }
drh14acc042001-06-10 19:56:58 +00002079 for(i=0; i<nNew; i++){
2080 sqlitepager_unref(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00002081 }
drh14acc042001-06-10 19:56:58 +00002082 if( pCur && pCur->pPage==0 ){
2083 pCur->pPage = pParent;
2084 pCur->idx = 0;
2085 }else{
2086 sqlitepager_unref(pParent);
drh8b2f49b2001-06-08 00:21:52 +00002087 }
2088 return rc;
2089}
2090
2091/*
drh3b7511c2001-05-26 13:15:44 +00002092** Insert a new record into the BTree. The key is given by (pKey,nKey)
2093** and the data is given by (pData,nData). The cursor is used only to
2094** define what database the record should be inserted into. The cursor
drh14acc042001-06-10 19:56:58 +00002095** is left pointing at the new record.
drh3b7511c2001-05-26 13:15:44 +00002096*/
2097int sqliteBtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00002098 BtCursor *pCur, /* Insert data into the table of this cursor */
drhbe0072d2001-09-13 14:46:09 +00002099 const void *pKey, int nKey, /* The key of the new record */
drh5c4d9702001-08-20 00:33:58 +00002100 const void *pData, int nData /* The data of the new record */
drh3b7511c2001-05-26 13:15:44 +00002101){
2102 Cell newCell;
2103 int rc;
2104 int loc;
drh14acc042001-06-10 19:56:58 +00002105 int szNew;
drh3b7511c2001-05-26 13:15:44 +00002106 MemPage *pPage;
2107 Btree *pBt = pCur->pBt;
2108
drhecdc7532001-09-23 02:35:53 +00002109 if( pCur->pPage==0 ){
2110 return SQLITE_ABORT; /* A rollback destroyed this cursor */
2111 }
drh5edc3122001-09-13 21:53:09 +00002112 if( !pCur->pBt->inTrans || nKey+nData==0 ){
drh8b2f49b2001-06-08 00:21:52 +00002113 return SQLITE_ERROR; /* Must start a transaction first */
2114 }
drhecdc7532001-09-23 02:35:53 +00002115 if( !pCur->wrFlag ){
2116 return SQLITE_PERM; /* Cursor not open for writing */
2117 }
drh14acc042001-06-10 19:56:58 +00002118 rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc);
drh3b7511c2001-05-26 13:15:44 +00002119 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002120 pPage = pCur->pPage;
2121 rc = sqlitepager_write(pPage);
drhbd03cae2001-06-02 02:40:57 +00002122 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00002123 rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData);
2124 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002125 szNew = cellSize(&newCell);
drh3b7511c2001-05-26 13:15:44 +00002126 if( loc==0 ){
drh14acc042001-06-10 19:56:58 +00002127 newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild;
2128 rc = clearCell(pBt, pPage->apCell[pCur->idx]);
drh5e2f8b92001-05-28 00:41:15 +00002129 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002130 dropCell(pPage, pCur->idx, cellSize(pPage->apCell[pCur->idx]));
drh7c717f72001-06-24 20:39:41 +00002131 }else if( loc<0 && pPage->nCell>0 ){
drh14acc042001-06-10 19:56:58 +00002132 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
2133 pCur->idx++;
2134 }else{
2135 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
drh3b7511c2001-05-26 13:15:44 +00002136 }
drh7c717f72001-06-24 20:39:41 +00002137 insertCell(pPage, pCur->idx, &newCell, szNew);
drh14acc042001-06-10 19:56:58 +00002138 rc = balance(pCur->pBt, pPage, pCur);
drh3fc190c2001-09-14 03:24:23 +00002139 /* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
2140 /* fflush(stdout); */
drh5e2f8b92001-05-28 00:41:15 +00002141 return rc;
2142}
2143
2144/*
drhbd03cae2001-06-02 02:40:57 +00002145** Delete the entry that the cursor is pointing to.
drh5e2f8b92001-05-28 00:41:15 +00002146**
drhbd03cae2001-06-02 02:40:57 +00002147** The cursor is left pointing at either the next or the previous
2148** entry. If the cursor is left pointing to the next entry, then
2149** the pCur->bSkipNext flag is set which forces the next call to
2150** sqliteBtreeNext() to be a no-op. That way, you can always call
2151** sqliteBtreeNext() after a delete and the cursor will be left
2152** pointing to the first entry after the deleted entry.
drh3b7511c2001-05-26 13:15:44 +00002153*/
2154int sqliteBtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00002155 MemPage *pPage = pCur->pPage;
2156 Cell *pCell;
2157 int rc;
drh8c42ca92001-06-22 19:15:00 +00002158 Pgno pgnoChild;
drh8b2f49b2001-06-08 00:21:52 +00002159
drhecdc7532001-09-23 02:35:53 +00002160 if( pCur->pPage==0 ){
2161 return SQLITE_ABORT; /* A rollback destroyed this cursor */
2162 }
drh8b2f49b2001-06-08 00:21:52 +00002163 if( !pCur->pBt->inTrans ){
2164 return SQLITE_ERROR; /* Must start a transaction first */
2165 }
drhbd03cae2001-06-02 02:40:57 +00002166 if( pCur->idx >= pPage->nCell ){
2167 return SQLITE_ERROR; /* The cursor is not pointing to anything */
2168 }
drhecdc7532001-09-23 02:35:53 +00002169 if( !pCur->wrFlag ){
2170 return SQLITE_PERM; /* Did not open this cursor for writing */
2171 }
drhbd03cae2001-06-02 02:40:57 +00002172 rc = sqlitepager_write(pPage);
2173 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00002174 pCell = pPage->apCell[pCur->idx];
drh14acc042001-06-10 19:56:58 +00002175 pgnoChild = pCell->h.leftChild;
drh8c42ca92001-06-22 19:15:00 +00002176 clearCell(pCur->pBt, pCell);
drh14acc042001-06-10 19:56:58 +00002177 if( pgnoChild ){
2178 /*
drh5e00f6c2001-09-13 13:46:56 +00002179 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00002180 ** do something we will leave a hole on an internal page.
2181 ** We have to fill the hole by moving in a cell from a leaf. The
2182 ** next Cell after the one to be deleted is guaranteed to exist and
2183 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00002184 */
drh14acc042001-06-10 19:56:58 +00002185 BtCursor leafCur;
2186 Cell *pNext;
2187 int szNext;
2188 getTempCursor(pCur, &leafCur);
2189 rc = sqliteBtreeNext(&leafCur, 0);
2190 if( rc!=SQLITE_OK ){
2191 return SQLITE_CORRUPT;
drh5e2f8b92001-05-28 00:41:15 +00002192 }
drh6019e162001-07-02 17:51:45 +00002193 rc = sqlitepager_write(leafCur.pPage);
2194 if( rc ) return rc;
drh9ca7d3b2001-06-28 11:50:21 +00002195 dropCell(pPage, pCur->idx, cellSize(pCell));
drh8c42ca92001-06-22 19:15:00 +00002196 pNext = leafCur.pPage->apCell[leafCur.idx];
drh14acc042001-06-10 19:56:58 +00002197 szNext = cellSize(pNext);
drh8c42ca92001-06-22 19:15:00 +00002198 pNext->h.leftChild = pgnoChild;
drh14acc042001-06-10 19:56:58 +00002199 insertCell(pPage, pCur->idx, pNext, szNext);
2200 rc = balance(pCur->pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00002201 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00002202 pCur->bSkipNext = 1;
drh14acc042001-06-10 19:56:58 +00002203 dropCell(leafCur.pPage, leafCur.idx, szNext);
drhf5bf0a72001-11-23 00:24:12 +00002204 rc = balance(pCur->pBt, leafCur.pPage, pCur);
drh8c42ca92001-06-22 19:15:00 +00002205 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00002206 }else{
drh9ca7d3b2001-06-28 11:50:21 +00002207 dropCell(pPage, pCur->idx, cellSize(pCell));
drh5edc3122001-09-13 21:53:09 +00002208 if( pCur->idx>=pPage->nCell ){
2209 pCur->idx = pPage->nCell-1;
drhf5bf0a72001-11-23 00:24:12 +00002210 if( pCur->idx<0 ){
2211 pCur->idx = 0;
2212 pCur->bSkipNext = 1;
2213 }else{
2214 pCur->bSkipNext = 0;
2215 }
drh6019e162001-07-02 17:51:45 +00002216 }else{
2217 pCur->bSkipNext = 1;
2218 }
drh14acc042001-06-10 19:56:58 +00002219 rc = balance(pCur->pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00002220 }
drh5e2f8b92001-05-28 00:41:15 +00002221 return rc;
drh3b7511c2001-05-26 13:15:44 +00002222}
drh8b2f49b2001-06-08 00:21:52 +00002223
2224/*
2225** Create a new BTree in the same file. Write into *piTable the index
2226** of the root page of the new table.
2227*/
2228int sqliteBtreeCreateTable(Btree *pBt, int *piTable){
2229 MemPage *pRoot;
2230 Pgno pgnoRoot;
2231 int rc;
2232 if( !pBt->inTrans ){
2233 return SQLITE_ERROR; /* Must start a transaction first */
2234 }
2235 rc = allocatePage(pBt, &pRoot, &pgnoRoot);
2236 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00002237 assert( sqlitepager_iswriteable(pRoot) );
drh8b2f49b2001-06-08 00:21:52 +00002238 zeroPage(pRoot);
2239 sqlitepager_unref(pRoot);
2240 *piTable = (int)pgnoRoot;
2241 return SQLITE_OK;
2242}
2243
2244/*
2245** Erase the given database page and all its children. Return
2246** the page to the freelist.
2247*/
drh2aa679f2001-06-25 02:11:07 +00002248static int clearDatabasePage(Btree *pBt, Pgno pgno, int freePageFlag){
drh8b2f49b2001-06-08 00:21:52 +00002249 MemPage *pPage;
2250 int rc;
drh8b2f49b2001-06-08 00:21:52 +00002251 Cell *pCell;
2252 int idx;
2253
drh8c42ca92001-06-22 19:15:00 +00002254 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage);
drh8b2f49b2001-06-08 00:21:52 +00002255 if( rc ) return rc;
drh6019e162001-07-02 17:51:45 +00002256 rc = sqlitepager_write(pPage);
2257 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002258 idx = pPage->u.hdr.firstCell;
drh8b2f49b2001-06-08 00:21:52 +00002259 while( idx>0 ){
drh14acc042001-06-10 19:56:58 +00002260 pCell = (Cell*)&pPage->u.aDisk[idx];
drh8b2f49b2001-06-08 00:21:52 +00002261 idx = pCell->h.iNext;
2262 if( pCell->h.leftChild ){
drh2aa679f2001-06-25 02:11:07 +00002263 rc = clearDatabasePage(pBt, pCell->h.leftChild, 1);
drh8b2f49b2001-06-08 00:21:52 +00002264 if( rc ) return rc;
2265 }
drh8c42ca92001-06-22 19:15:00 +00002266 rc = clearCell(pBt, pCell);
drh8b2f49b2001-06-08 00:21:52 +00002267 if( rc ) return rc;
2268 }
drh2aa679f2001-06-25 02:11:07 +00002269 if( pPage->u.hdr.rightChild ){
2270 rc = clearDatabasePage(pBt, pPage->u.hdr.rightChild, 1);
2271 if( rc ) return rc;
2272 }
2273 if( freePageFlag ){
2274 rc = freePage(pBt, pPage, pgno);
2275 }else{
2276 zeroPage(pPage);
2277 }
drhdd793422001-06-28 01:54:48 +00002278 sqlitepager_unref(pPage);
drh2aa679f2001-06-25 02:11:07 +00002279 return rc;
drh8b2f49b2001-06-08 00:21:52 +00002280}
2281
2282/*
2283** Delete all information from a single table in the database.
2284*/
2285int sqliteBtreeClearTable(Btree *pBt, int iTable){
2286 int rc;
drh5a2c2c22001-11-21 02:21:11 +00002287 ptr nLock;
drh8b2f49b2001-06-08 00:21:52 +00002288 if( !pBt->inTrans ){
2289 return SQLITE_ERROR; /* Must start a transaction first */
2290 }
drh5a2c2c22001-11-21 02:21:11 +00002291 nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable);
drhecdc7532001-09-23 02:35:53 +00002292 if( nLock ){
2293 return SQLITE_LOCKED;
2294 }
drh2aa679f2001-06-25 02:11:07 +00002295 rc = clearDatabasePage(pBt, (Pgno)iTable, 0);
drh8b2f49b2001-06-08 00:21:52 +00002296 if( rc ){
2297 sqliteBtreeRollback(pBt);
drh8b2f49b2001-06-08 00:21:52 +00002298 }
drh8c42ca92001-06-22 19:15:00 +00002299 return rc;
drh8b2f49b2001-06-08 00:21:52 +00002300}
2301
2302/*
2303** Erase all information in a table and add the root of the table to
2304** the freelist. Except, the root of the principle table (the one on
2305** page 2) is never added to the freelist.
2306*/
2307int sqliteBtreeDropTable(Btree *pBt, int iTable){
2308 int rc;
2309 MemPage *pPage;
2310 if( !pBt->inTrans ){
2311 return SQLITE_ERROR; /* Must start a transaction first */
2312 }
drh8c42ca92001-06-22 19:15:00 +00002313 rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
drh2aa679f2001-06-25 02:11:07 +00002314 if( rc ) return rc;
2315 rc = sqliteBtreeClearTable(pBt, iTable);
2316 if( rc ) return rc;
2317 if( iTable>2 ){
2318 rc = freePage(pBt, pPage, iTable);
2319 }else{
2320 zeroPage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00002321 }
drhdd793422001-06-28 01:54:48 +00002322 sqlitepager_unref(pPage);
drh8b2f49b2001-06-08 00:21:52 +00002323 return rc;
2324}
2325
2326/*
2327** Read the meta-information out of a database file.
2328*/
2329int sqliteBtreeGetMeta(Btree *pBt, int *aMeta){
2330 PageOne *pP1;
2331 int rc;
2332
drh8c42ca92001-06-22 19:15:00 +00002333 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1);
drh8b2f49b2001-06-08 00:21:52 +00002334 if( rc ) return rc;
drh2aa679f2001-06-25 02:11:07 +00002335 aMeta[0] = pP1->nFree;
2336 memcpy(&aMeta[1], pP1->aMeta, sizeof(pP1->aMeta));
drh8b2f49b2001-06-08 00:21:52 +00002337 sqlitepager_unref(pP1);
2338 return SQLITE_OK;
2339}
2340
2341/*
2342** Write meta-information back into the database.
2343*/
2344int sqliteBtreeUpdateMeta(Btree *pBt, int *aMeta){
2345 PageOne *pP1;
2346 int rc;
2347 if( !pBt->inTrans ){
2348 return SQLITE_ERROR; /* Must start a transaction first */
2349 }
2350 pP1 = pBt->page1;
2351 rc = sqlitepager_write(pP1);
drh2aa679f2001-06-25 02:11:07 +00002352 if( rc ) return rc;
2353 memcpy(pP1->aMeta, &aMeta[1], sizeof(pP1->aMeta));
drh8b2f49b2001-06-08 00:21:52 +00002354 return SQLITE_OK;
2355}
drh8c42ca92001-06-22 19:15:00 +00002356
drh5eddca62001-06-30 21:53:53 +00002357/******************************************************************************
2358** The complete implementation of the BTree subsystem is above this line.
2359** All the code the follows is for testing and troubleshooting the BTree
2360** subsystem. None of the code that follows is used during normal operation.
2361** All of the following code is omitted unless the library is compiled with
2362** the -DSQLITE_TEST=1 compiler option.
2363******************************************************************************/
drh5edc3122001-09-13 21:53:09 +00002364#if 1
drh5eddca62001-06-30 21:53:53 +00002365
drh8c42ca92001-06-22 19:15:00 +00002366/*
2367** Print a disassembly of the given page on standard output. This routine
2368** is used for debugging and testing only.
2369*/
drh6019e162001-07-02 17:51:45 +00002370int sqliteBtreePageDump(Btree *pBt, int pgno, int recursive){
drh8c42ca92001-06-22 19:15:00 +00002371 int rc;
2372 MemPage *pPage;
2373 int i, j;
2374 int nFree;
2375 u16 idx;
2376 char range[20];
2377 unsigned char payload[20];
2378 rc = sqlitepager_get(pBt->pPager, (Pgno)pgno, (void**)&pPage);
2379 if( rc ){
2380 return rc;
2381 }
drh6019e162001-07-02 17:51:45 +00002382 if( recursive ) printf("PAGE %d:\n", pgno);
drh8c42ca92001-06-22 19:15:00 +00002383 i = 0;
2384 idx = pPage->u.hdr.firstCell;
2385 while( idx>0 && idx<=SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){
2386 Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
2387 int sz = cellSize(pCell);
2388 sprintf(range,"%d..%d", idx, idx+sz-1);
drh80ff32f2001-11-04 18:32:46 +00002389 sz = NKEY(pCell->h) + NDATA(pCell->h);
drh8c42ca92001-06-22 19:15:00 +00002390 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
2391 memcpy(payload, pCell->aPayload, sz);
2392 for(j=0; j<sz; j++){
2393 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
2394 }
2395 payload[sz] = 0;
2396 printf(
drh6019e162001-07-02 17:51:45 +00002397 "cell %2d: i=%-10s chld=%-4d nk=%-4d nd=%-4d payload=%s\n",
drh80ff32f2001-11-04 18:32:46 +00002398 i, range, (int)pCell->h.leftChild, NKEY(pCell->h), NDATA(pCell->h),
drh2aa679f2001-06-25 02:11:07 +00002399 payload
drh8c42ca92001-06-22 19:15:00 +00002400 );
drh6019e162001-07-02 17:51:45 +00002401 if( pPage->isInit && pPage->apCell[i]!=pCell ){
drh2aa679f2001-06-25 02:11:07 +00002402 printf("**** apCell[%d] does not match on prior entry ****\n", i);
2403 }
drh7c717f72001-06-24 20:39:41 +00002404 i++;
drh8c42ca92001-06-22 19:15:00 +00002405 idx = pCell->h.iNext;
2406 }
2407 if( idx!=0 ){
2408 printf("ERROR: next cell index out of range: %d\n", idx);
2409 }
2410 printf("right_child: %d\n", pPage->u.hdr.rightChild);
2411 nFree = 0;
2412 i = 0;
2413 idx = pPage->u.hdr.firstFree;
2414 while( idx>0 && idx<SQLITE_PAGE_SIZE ){
2415 FreeBlk *p = (FreeBlk*)&pPage->u.aDisk[idx];
2416 sprintf(range,"%d..%d", idx, idx+p->iSize-1);
2417 nFree += p->iSize;
2418 printf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
2419 i, range, p->iSize, nFree);
2420 idx = p->iNext;
drh2aa679f2001-06-25 02:11:07 +00002421 i++;
drh8c42ca92001-06-22 19:15:00 +00002422 }
2423 if( idx!=0 ){
2424 printf("ERROR: next freeblock index out of range: %d\n", idx);
2425 }
drh6019e162001-07-02 17:51:45 +00002426 if( recursive && pPage->u.hdr.rightChild!=0 ){
2427 idx = pPage->u.hdr.firstCell;
2428 while( idx>0 && idx<SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){
2429 Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
2430 sqliteBtreePageDump(pBt, pCell->h.leftChild, 1);
2431 idx = pCell->h.iNext;
2432 }
2433 sqliteBtreePageDump(pBt, pPage->u.hdr.rightChild, 1);
2434 }
drh8c42ca92001-06-22 19:15:00 +00002435 sqlitepager_unref(pPage);
2436 return SQLITE_OK;
2437}
drh8c42ca92001-06-22 19:15:00 +00002438
drh8c42ca92001-06-22 19:15:00 +00002439/*
drh2aa679f2001-06-25 02:11:07 +00002440** Fill aResult[] with information about the entry and page that the
2441** cursor is pointing to.
2442**
2443** aResult[0] = The page number
2444** aResult[1] = The entry number
2445** aResult[2] = Total number of entries on this page
2446** aResult[3] = Size of this entry
2447** aResult[4] = Number of free bytes on this page
2448** aResult[5] = Number of free blocks on the page
2449** aResult[6] = Page number of the left child of this entry
2450** aResult[7] = Page number of the right child for the whole page
drh5eddca62001-06-30 21:53:53 +00002451**
2452** This routine is used for testing and debugging only.
drh8c42ca92001-06-22 19:15:00 +00002453*/
2454int sqliteBtreeCursorDump(BtCursor *pCur, int *aResult){
drh2aa679f2001-06-25 02:11:07 +00002455 int cnt, idx;
2456 MemPage *pPage = pCur->pPage;
2457 aResult[0] = sqlitepager_pagenumber(pPage);
drh8c42ca92001-06-22 19:15:00 +00002458 aResult[1] = pCur->idx;
drh2aa679f2001-06-25 02:11:07 +00002459 aResult[2] = pPage->nCell;
2460 if( pCur->idx>=0 && pCur->idx<pPage->nCell ){
2461 aResult[3] = cellSize(pPage->apCell[pCur->idx]);
2462 aResult[6] = pPage->apCell[pCur->idx]->h.leftChild;
2463 }else{
2464 aResult[3] = 0;
2465 aResult[6] = 0;
2466 }
2467 aResult[4] = pPage->nFree;
2468 cnt = 0;
2469 idx = pPage->u.hdr.firstFree;
2470 while( idx>0 && idx<SQLITE_PAGE_SIZE ){
2471 cnt++;
2472 idx = ((FreeBlk*)&pPage->u.aDisk[idx])->iNext;
2473 }
2474 aResult[5] = cnt;
2475 aResult[7] = pPage->u.hdr.rightChild;
drh8c42ca92001-06-22 19:15:00 +00002476 return SQLITE_OK;
2477}
drhdd793422001-06-28 01:54:48 +00002478
drhdd793422001-06-28 01:54:48 +00002479/*
drh5eddca62001-06-30 21:53:53 +00002480** Return the pager associated with a BTree. This routine is used for
2481** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00002482*/
2483Pager *sqliteBtreePager(Btree *pBt){
2484 return pBt->pPager;
2485}
drh5eddca62001-06-30 21:53:53 +00002486
2487/*
2488** This structure is passed around through all the sanity checking routines
2489** in order to keep track of some global state information.
2490*/
2491typedef struct SanityCheck SanityCheck;
2492struct SanityCheck {
drh100569d2001-10-02 13:01:48 +00002493 Btree *pBt; /* The tree being checked out */
2494 Pager *pPager; /* The associated pager. Also accessible by pBt->pPager */
2495 int nPage; /* Number of pages in the database */
2496 int *anRef; /* Number of times each page is referenced */
2497 int nTreePage; /* Number of BTree pages */
2498 int nByte; /* Number of bytes of data stored on BTree pages */
2499 char *zErrMsg; /* An error message. NULL of no errors seen. */
drh5eddca62001-06-30 21:53:53 +00002500};
2501
2502/*
2503** Append a message to the error message string.
2504*/
2505static void checkAppendMsg(SanityCheck *pCheck, char *zMsg1, char *zMsg2){
2506 if( pCheck->zErrMsg ){
2507 char *zOld = pCheck->zErrMsg;
2508 pCheck->zErrMsg = 0;
2509 sqliteSetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, 0);
2510 sqliteFree(zOld);
2511 }else{
2512 sqliteSetString(&pCheck->zErrMsg, zMsg1, zMsg2, 0);
2513 }
2514}
2515
2516/*
2517** Add 1 to the reference count for page iPage. If this is the second
2518** reference to the page, add an error message to pCheck->zErrMsg.
2519** Return 1 if there are 2 ore more references to the page and 0 if
2520** if this is the first reference to the page.
2521**
2522** Also check that the page number is in bounds.
2523*/
2524static int checkRef(SanityCheck *pCheck, int iPage, char *zContext){
2525 if( iPage==0 ) return 1;
2526 if( iPage>pCheck->nPage ){
2527 char zBuf[100];
2528 sprintf(zBuf, "invalid page number %d", iPage);
2529 checkAppendMsg(pCheck, zContext, zBuf);
2530 return 1;
2531 }
2532 if( pCheck->anRef[iPage]==1 ){
2533 char zBuf[100];
2534 sprintf(zBuf, "2nd reference to page %d", iPage);
2535 checkAppendMsg(pCheck, zContext, zBuf);
2536 return 1;
2537 }
2538 return (pCheck->anRef[iPage]++)>1;
2539}
2540
2541/*
2542** Check the integrity of the freelist or of an overflow page list.
2543** Verify that the number of pages on the list is N.
2544*/
2545static void checkList(SanityCheck *pCheck, int iPage, int N, char *zContext){
2546 char zMsg[100];
2547 while( N-- ){
2548 OverflowPage *pOvfl;
2549 if( iPage<1 ){
2550 sprintf(zMsg, "%d pages missing from overflow list", N+1);
2551 checkAppendMsg(pCheck, zContext, zMsg);
2552 break;
2553 }
2554 if( checkRef(pCheck, iPage, zContext) ) break;
2555 if( sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
2556 sprintf(zMsg, "failed to get page %d", iPage);
2557 checkAppendMsg(pCheck, zContext, zMsg);
2558 break;
2559 }
2560 iPage = (int)pOvfl->iNext;
2561 sqlitepager_unref(pOvfl);
2562 }
2563}
2564
2565/*
2566** Do various sanity checks on a single page of a tree. Return
2567** the tree depth. Root pages return 0. Parents of root pages
2568** return 1, and so forth.
2569**
2570** These checks are done:
2571**
2572** 1. Make sure that cells and freeblocks do not overlap
2573** but combine to completely cover the page.
2574** 2. Make sure cell keys are in order.
2575** 3. Make sure no key is less than or equal to zLowerBound.
2576** 4. Make sure no key is greater than or equal to zUpperBound.
2577** 5. Check the integrity of overflow pages.
2578** 6. Recursively call checkTreePage on all children.
2579** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00002580** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00002581** the root of the tree.
2582*/
2583static int checkTreePage(
2584 SanityCheck *pCheck, /* Context for the sanity check */
2585 int iPage, /* Page number of the page to check */
2586 MemPage *pParent, /* Parent page */
2587 char *zParentContext, /* Parent context */
2588 char *zLowerBound, /* All keys should be greater than this, if not NULL */
2589 char *zUpperBound /* All keys should be less than this, if not NULL */
2590){
2591 MemPage *pPage;
2592 int i, rc, depth, d2, pgno;
2593 char *zKey1, *zKey2;
2594 BtCursor cur;
2595 char zMsg[100];
2596 char zContext[100];
2597 char hit[SQLITE_PAGE_SIZE];
2598
2599 /* Check that the page exists
2600 */
2601 if( iPage==0 ) return 0;
2602 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
2603 sprintf(zContext, "On tree page %d: ", iPage);
2604 if( (rc = sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pPage))!=0 ){
2605 sprintf(zMsg, "unable to get the page. error code=%d", rc);
2606 checkAppendMsg(pCheck, zContext, zMsg);
2607 return 0;
2608 }
2609 if( (rc = initPage(pPage, (Pgno)iPage, pParent))!=0 ){
2610 sprintf(zMsg, "initPage() returns error code %d", rc);
2611 checkAppendMsg(pCheck, zContext, zMsg);
2612 sqlitepager_unref(pPage);
2613 return 0;
2614 }
2615
2616 /* Check out all the cells.
2617 */
2618 depth = 0;
2619 zKey1 = zLowerBound ? sqliteStrDup(zLowerBound) : 0;
2620 cur.pPage = pPage;
2621 cur.pBt = pCheck->pBt;
2622 for(i=0; i<pPage->nCell; i++){
2623 Cell *pCell = pPage->apCell[i];
2624 int sz;
2625
2626 /* Check payload overflow pages
2627 */
drh80ff32f2001-11-04 18:32:46 +00002628 sz = NKEY(pCell->h) + NDATA(pCell->h);
drh5eddca62001-06-30 21:53:53 +00002629 sprintf(zContext, "On page %d cell %d: ", iPage, i);
2630 if( sz>MX_LOCAL_PAYLOAD ){
2631 int nPage = (sz - MX_LOCAL_PAYLOAD + OVERFLOW_SIZE - 1)/OVERFLOW_SIZE;
2632 checkList(pCheck, pCell->ovfl, nPage, zContext);
2633 }
2634
2635 /* Check that keys are in the right order
2636 */
2637 cur.idx = i;
drh80ff32f2001-11-04 18:32:46 +00002638 zKey2 = sqliteMalloc( NKEY(pCell->h)+1 );
2639 getPayload(&cur, 0, NKEY(pCell->h), zKey2);
drh5eddca62001-06-30 21:53:53 +00002640 if( zKey1 && strcmp(zKey1,zKey2)>=0 ){
2641 checkAppendMsg(pCheck, zContext, "Key is out of order");
2642 }
2643
2644 /* Check sanity of left child page.
2645 */
2646 pgno = (int)pCell->h.leftChild;
2647 d2 = checkTreePage(pCheck, pgno, pPage, zContext, zKey1, zKey2);
2648 if( i>0 && d2!=depth ){
2649 checkAppendMsg(pCheck, zContext, "Child page depth differs");
2650 }
2651 depth = d2;
2652 sqliteFree(zKey1);
2653 zKey1 = zKey2;
2654 }
2655 pgno = pPage->u.hdr.rightChild;
2656 sprintf(zContext, "On page %d at right child: ", iPage);
2657 checkTreePage(pCheck, pgno, pPage, zContext, zKey1, zUpperBound);
2658 sqliteFree(zKey1);
2659
2660 /* Check for complete coverage of the page
2661 */
2662 memset(hit, 0, sizeof(hit));
2663 memset(hit, 1, sizeof(PageHdr));
2664 for(i=pPage->u.hdr.firstCell; i>0 && i<SQLITE_PAGE_SIZE; ){
2665 Cell *pCell = (Cell*)&pPage->u.aDisk[i];
2666 int j;
2667 for(j=i+cellSize(pCell)-1; j>=i; j--) hit[j]++;
2668 i = pCell->h.iNext;
2669 }
2670 for(i=pPage->u.hdr.firstFree; i>0 && i<SQLITE_PAGE_SIZE; ){
2671 FreeBlk *pFBlk = (FreeBlk*)&pPage->u.aDisk[i];
2672 int j;
2673 for(j=i+pFBlk->iSize-1; j>=i; j--) hit[j]++;
2674 i = pFBlk->iNext;
2675 }
2676 for(i=0; i<SQLITE_PAGE_SIZE; i++){
2677 if( hit[i]==0 ){
2678 sprintf(zMsg, "Unused space at byte %d of page %d", i, iPage);
2679 checkAppendMsg(pCheck, zMsg, 0);
2680 break;
2681 }else if( hit[i]>1 ){
2682 sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage);
2683 checkAppendMsg(pCheck, zMsg, 0);
2684 break;
2685 }
2686 }
2687
2688 /* Check that free space is kept to a minimum
2689 */
drh6019e162001-07-02 17:51:45 +00002690#if 0
2691 if( pParent && pParent->nCell>2 && pPage->nFree>3*SQLITE_PAGE_SIZE/4 ){
drh5eddca62001-06-30 21:53:53 +00002692 sprintf(zMsg, "free space (%d) greater than max (%d)", pPage->nFree,
2693 SQLITE_PAGE_SIZE/3);
2694 checkAppendMsg(pCheck, zContext, zMsg);
2695 }
drh6019e162001-07-02 17:51:45 +00002696#endif
2697
2698 /* Update freespace totals.
2699 */
2700 pCheck->nTreePage++;
2701 pCheck->nByte += USABLE_SPACE - pPage->nFree;
drh5eddca62001-06-30 21:53:53 +00002702
2703 sqlitepager_unref(pPage);
2704 return depth;
2705}
2706
2707/*
2708** This routine does a complete check of the given BTree file. aRoot[] is
2709** an array of pages numbers were each page number is the root page of
2710** a table. nRoot is the number of entries in aRoot.
2711**
2712** If everything checks out, this routine returns NULL. If something is
2713** amiss, an error message is written into memory obtained from malloc()
2714** and a pointer to that error message is returned. The calling function
2715** is responsible for freeing the error message when it is done.
2716*/
2717char *sqliteBtreeSanityCheck(Btree *pBt, int *aRoot, int nRoot){
2718 int i;
2719 int nRef;
2720 SanityCheck sCheck;
2721
2722 nRef = *sqlitepager_stats(pBt->pPager);
drhefc251d2001-07-01 22:12:01 +00002723 if( lockBtree(pBt)!=SQLITE_OK ){
2724 return sqliteStrDup("Unable to acquire a read lock on the database");
2725 }
drh5eddca62001-06-30 21:53:53 +00002726 sCheck.pBt = pBt;
2727 sCheck.pPager = pBt->pPager;
2728 sCheck.nPage = sqlitepager_pagecount(sCheck.pPager);
2729 sCheck.anRef = sqliteMalloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
2730 sCheck.anRef[1] = 1;
2731 for(i=2; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
2732 sCheck.zErrMsg = 0;
2733
2734 /* Check the integrity of the freelist
2735 */
2736 checkList(&sCheck, pBt->page1->freeList, pBt->page1->nFree,"Main freelist: ");
2737
2738 /* Check all the tables.
2739 */
2740 for(i=0; i<nRoot; i++){
2741 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0, 0);
2742 }
2743
2744 /* Make sure every page in the file is referenced
2745 */
2746 for(i=1; i<=sCheck.nPage; i++){
2747 if( sCheck.anRef[i]==0 ){
2748 char zBuf[100];
2749 sprintf(zBuf, "Page %d is never used", i);
2750 checkAppendMsg(&sCheck, zBuf, 0);
2751 }
2752 }
2753
2754 /* Make sure this analysis did not leave any unref() pages
2755 */
drh5e00f6c2001-09-13 13:46:56 +00002756 unlockBtreeIfUnused(pBt);
drh5eddca62001-06-30 21:53:53 +00002757 if( nRef != *sqlitepager_stats(pBt->pPager) ){
2758 char zBuf[100];
2759 sprintf(zBuf,
2760 "Outstanding page count goes from %d to %d during this analysis",
2761 nRef, *sqlitepager_stats(pBt->pPager)
2762 );
2763 checkAppendMsg(&sCheck, zBuf, 0);
2764 }
2765
2766 /* Clean up and report errors.
2767 */
2768 sqliteFree(sCheck.anRef);
2769 return sCheck.zErrMsg;
2770}
2771
2772#endif /* SQLITE_TEST */