blob: b4666da4091931e25ffbed956ad35f4a81ad861b [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
2** Copyright (c) 2001 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
drh7c717f72001-06-24 20:39:41 +000024** $Id: btree.c,v 1.14 2001/06/24 20:39:41 drh Exp $
drh8b2f49b2001-06-08 00:21:52 +000025**
26** This file implements a external (disk-based) database using BTrees.
27** For a detailed discussion of BTrees, refer to
28**
29** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
30** "Sorting And Searching", pages 473-480. Addison-Wesley
31** Publishing Company, Reading, Massachusetts.
32**
33** The basic idea is that each page of the file contains N database
34** entries and N+1 pointers to subpages.
35**
36** ----------------------------------------------------------------
37** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) |
38** ----------------------------------------------------------------
39**
40** All of the keys on the page that Ptr(0) points to have values less
41** than Key(0). All of the keys on page Ptr(1) and its subpages have
42** values greater than Key(0) and less than Key(1). All of the keys
43** on Ptr(N+1) and its subpages have values greater than Key(N). And
44** so forth.
45**
46** Finding a particular key requires reading O(log(M)) pages from the file
47** where M is the number of entries in the tree.
48**
49** In this implementation, a single file can hold one or more separate
50** BTrees. Each BTree is identified by the index of its root page. The
51** key and data for any entry are combined to form the "payload". Up to
52** MX_LOCAL_PAYLOAD bytes of payload can be carried directly on the
53** database page. If the payload is larger than MX_LOCAL_PAYLOAD bytes
54** then surplus bytes are stored on overflow pages. The payload for an
55** entry and the preceding pointer are combined to form a "Cell". Each
56** page has a smaller header which contains the Ptr(N+1) pointer.
57**
58** The first page of the file contains a magic string used to verify that
59** the file really is a valid BTree database, a pointer to a list of unused
60** pages in the file, and some meta information. The root of the first
61** BTree begins on page 2 of the file. (Pages are numbered beginning with
62** 1, not 0.) Thus a minimum database contains 2 pages.
drha059ad02001-04-17 20:09:11 +000063*/
64#include "sqliteInt.h"
65#include "pager.h"
66#include "btree.h"
67#include <assert.h>
68
drh2af926b2001-05-15 00:39:25 +000069
drh2af926b2001-05-15 00:39:25 +000070/*
71** Primitive data types. u32 must be 4 bytes and u16 must be 2 bytes.
drh14acc042001-06-10 19:56:58 +000072** The uptr type must be big enough to hold a pointer.
drh306dc212001-05-21 13:45:10 +000073** Change these typedefs when porting to new architectures.
drh2af926b2001-05-15 00:39:25 +000074*/
drh14acc042001-06-10 19:56:58 +000075typedef unsigned int uptr;
drh8c42ca92001-06-22 19:15:00 +000076/* typedef unsigned int u32; -- already defined in sqliteInt.h */
drh365d68f2001-05-11 11:02:46 +000077typedef unsigned short int u16;
drh5e2f8b92001-05-28 00:41:15 +000078typedef unsigned char u8;
drh365d68f2001-05-11 11:02:46 +000079
80/*
drh8c42ca92001-06-22 19:15:00 +000081** This macro casts a pointer to an integer. Useful for doing
82** pointer arithmetic.
83*/
drh7c717f72001-06-24 20:39:41 +000084#define Addr(X) ((uptr)X)
drh8c42ca92001-06-22 19:15:00 +000085
86/*
drh365d68f2001-05-11 11:02:46 +000087** Forward declarations of structures used only in this file.
88*/
drhbd03cae2001-06-02 02:40:57 +000089typedef struct PageOne PageOne;
drh2af926b2001-05-15 00:39:25 +000090typedef struct MemPage MemPage;
drh365d68f2001-05-11 11:02:46 +000091typedef struct PageHdr PageHdr;
92typedef struct Cell Cell;
drh3b7511c2001-05-26 13:15:44 +000093typedef struct CellHdr CellHdr;
drh365d68f2001-05-11 11:02:46 +000094typedef struct FreeBlk FreeBlk;
drh2af926b2001-05-15 00:39:25 +000095typedef struct OverflowPage OverflowPage;
96
97/*
98** All structures on a database page are aligned to 4-byte boundries.
99** This routine rounds up a number of bytes to the next multiple of 4.
drh306dc212001-05-21 13:45:10 +0000100**
101** This might need to change for computer architectures that require
102** and 8-byte alignment boundry for structures.
drh2af926b2001-05-15 00:39:25 +0000103*/
104#define ROUNDUP(X) ((X+3) & ~3)
drha059ad02001-04-17 20:09:11 +0000105
drh08ed44e2001-04-29 23:32:55 +0000106/*
drhbd03cae2001-06-02 02:40:57 +0000107** This is a magic string that appears at the beginning of every
drh8c42ca92001-06-22 19:15:00 +0000108** SQLite database in order to identify the file as a real database.
drh08ed44e2001-04-29 23:32:55 +0000109*/
drhbd03cae2001-06-02 02:40:57 +0000110static const char zMagicHeader[] =
drh8c42ca92001-06-22 19:15:00 +0000111 "** This file contains an SQLite 2.0 database **";
drhbd03cae2001-06-02 02:40:57 +0000112#define MAGIC_SIZE (sizeof(zMagicHeader))
drh08ed44e2001-04-29 23:32:55 +0000113
114/*
drh8c42ca92001-06-22 19:15:00 +0000115** This is a magic integer also used to the integrety of the database
116** file. This integer is used in addition to the string above so that
117** if the file is written on a little-endian architecture and read
118** on a big-endian architectures (or vice versa) we can detect the
119** problem.
120**
121** The number used was obtained at random and has no special
122** significance.
123*/
124#define MAGIC 0xdae37528
125
126/*
drhbd03cae2001-06-02 02:40:57 +0000127** The first page of the database file contains a magic header string
128** to identify the file as an SQLite database file. It also contains
129** a pointer to the first free page of the file. Page 2 contains the
drh8b2f49b2001-06-08 00:21:52 +0000130** root of the principle BTree. The file might contain other BTrees
131** rooted on pages above 2.
132**
133** The first page also contains SQLITE_N_BTREE_META integers that
134** can be used by higher-level routines.
drh08ed44e2001-04-29 23:32:55 +0000135**
drhbd03cae2001-06-02 02:40:57 +0000136** Remember that pages are numbered beginning with 1. (See pager.c
137** for additional information.) Page 0 does not exist and a page
138** number of 0 is used to mean "no such page".
139*/
140struct PageOne {
141 char zMagic[MAGIC_SIZE]; /* String that identifies the file as a database */
drh8c42ca92001-06-22 19:15:00 +0000142 int iMagic; /* Integer to verify correct byte order */
143 Pgno freeList; /* First free page in a list of all free pages */
drh8b2f49b2001-06-08 00:21:52 +0000144 int aMeta[SQLITE_N_BTREE_META]; /* User defined integers */
drhbd03cae2001-06-02 02:40:57 +0000145};
146
147/*
148** Each database page has a header that is an instance of this
149** structure.
drh08ed44e2001-04-29 23:32:55 +0000150**
drh8b2f49b2001-06-08 00:21:52 +0000151** PageHdr.firstFree is 0 if there is no free space on this page.
drh14acc042001-06-10 19:56:58 +0000152** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a
drh8b2f49b2001-06-08 00:21:52 +0000153** FreeBlk structure that describes the first block of free space.
154** All free space is defined by a linked list of FreeBlk structures.
drh08ed44e2001-04-29 23:32:55 +0000155**
drh8b2f49b2001-06-08 00:21:52 +0000156** Data is stored in a linked list of Cell structures. PageHdr.firstCell
drh14acc042001-06-10 19:56:58 +0000157** is the index into MemPage.u.aDisk[] of the first cell on the page. The
drh306dc212001-05-21 13:45:10 +0000158** Cells are kept in sorted order.
drh8b2f49b2001-06-08 00:21:52 +0000159**
160** A Cell contains all information about a database entry and a pointer
161** to a child page that contains other entries less than itself. In
162** other words, the i-th Cell contains both Ptr(i) and Key(i). The
163** right-most pointer of the page is contained in PageHdr.rightChild.
drh08ed44e2001-04-29 23:32:55 +0000164*/
drh365d68f2001-05-11 11:02:46 +0000165struct PageHdr {
drh5e2f8b92001-05-28 00:41:15 +0000166 Pgno rightChild; /* Child page that comes after all cells on this page */
drh14acc042001-06-10 19:56:58 +0000167 u16 firstCell; /* Index in MemPage.u.aDisk[] of the first cell */
168 u16 firstFree; /* Index in MemPage.u.aDisk[] of the first free block */
drh365d68f2001-05-11 11:02:46 +0000169};
drh306dc212001-05-21 13:45:10 +0000170
drh3b7511c2001-05-26 13:15:44 +0000171/*
172** Entries on a page of the database are called "Cells". Each Cell
173** has a header and data. This structure defines the header. The
drhbd03cae2001-06-02 02:40:57 +0000174** key and data (collectively the "payload") follow this header on
175** the database page.
176**
177** A definition of the complete Cell structure is given below. The
drh8c42ca92001-06-22 19:15:00 +0000178** header for the cell must be defined first in order to do some
drhbd03cae2001-06-02 02:40:57 +0000179** of the sizing #defines that follow.
drh3b7511c2001-05-26 13:15:44 +0000180*/
181struct CellHdr {
drh5e2f8b92001-05-28 00:41:15 +0000182 Pgno leftChild; /* Child page that comes before this cell */
drh3b7511c2001-05-26 13:15:44 +0000183 u16 nKey; /* Number of bytes in the key */
drh14acc042001-06-10 19:56:58 +0000184 u16 iNext; /* Index in MemPage.u.aDisk[] of next cell in sorted order */
drh3b7511c2001-05-26 13:15:44 +0000185 u32 nData; /* Number of bytes of data */
drh8c42ca92001-06-22 19:15:00 +0000186};
drh3b7511c2001-05-26 13:15:44 +0000187
188/*
189** The minimum size of a complete Cell. The Cell must contain a header
drhbd03cae2001-06-02 02:40:57 +0000190** and at least 4 bytes of payload.
drh3b7511c2001-05-26 13:15:44 +0000191*/
192#define MIN_CELL_SIZE (sizeof(CellHdr)+4)
193
194/*
195** The maximum number of database entries that can be held in a single
196** page of the database.
197*/
198#define MX_CELL ((SQLITE_PAGE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)
199
200/*
drh8c42ca92001-06-22 19:15:00 +0000201** The maximum amount of payload (in bytes) that can be stored locally for
202** a database entry. If the entry contains more data than this, the
drh3b7511c2001-05-26 13:15:44 +0000203** extra goes onto overflow pages.
drhbd03cae2001-06-02 02:40:57 +0000204**
205** This number is chosen so that at least 4 cells will fit on every page.
drh3b7511c2001-05-26 13:15:44 +0000206*/
207#define MX_LOCAL_PAYLOAD \
208 ((SQLITE_PAGE_SIZE-sizeof(PageHdr))/4-(sizeof(CellHdr)+sizeof(Pgno)))
209
drh306dc212001-05-21 13:45:10 +0000210/*
211** Data on a database page is stored as a linked list of Cell structures.
drh5e2f8b92001-05-28 00:41:15 +0000212** Both the key and the data are stored in aPayload[]. The key always comes
213** first. The aPayload[] field grows as necessary to hold the key and data,
drh306dc212001-05-21 13:45:10 +0000214** up to a maximum of MX_LOCAL_PAYLOAD bytes. If the size of the key and
drh3b7511c2001-05-26 13:15:44 +0000215** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the
216** page number of the first overflow page.
217**
218** Though this structure is fixed in size, the Cell on the database
drhbd03cae2001-06-02 02:40:57 +0000219** page varies in size. Every cell has a CellHdr and at least 4 bytes
drh3b7511c2001-05-26 13:15:44 +0000220** of payload space. Additional payload bytes (up to the maximum of
221** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as
222** needed.
drh306dc212001-05-21 13:45:10 +0000223*/
drh365d68f2001-05-11 11:02:46 +0000224struct Cell {
drh5e2f8b92001-05-28 00:41:15 +0000225 CellHdr h; /* The cell header */
226 char aPayload[MX_LOCAL_PAYLOAD]; /* Key and data */
227 Pgno ovfl; /* The first overflow page */
drh365d68f2001-05-11 11:02:46 +0000228};
drh306dc212001-05-21 13:45:10 +0000229
230/*
231** Free space on a page is remembered using a linked list of the FreeBlk
232** structures. Space on a database page is allocated in increments of
drh72f82862001-05-24 21:06:34 +0000233** at least 4 bytes and is always aligned to a 4-byte boundry. The
drh8b2f49b2001-06-08 00:21:52 +0000234** linked list of FreeBlks is always kept in order by address.
drh306dc212001-05-21 13:45:10 +0000235*/
drh365d68f2001-05-11 11:02:46 +0000236struct FreeBlk {
drh72f82862001-05-24 21:06:34 +0000237 u16 iSize; /* Number of bytes in this block of free space */
drh14acc042001-06-10 19:56:58 +0000238 u16 iNext; /* Index in MemPage.u.aDisk[] of the next free block */
drh365d68f2001-05-11 11:02:46 +0000239};
drh306dc212001-05-21 13:45:10 +0000240
241/*
drh14acc042001-06-10 19:56:58 +0000242** The number of bytes of payload that will fit on a single overflow page.
drh3b7511c2001-05-26 13:15:44 +0000243*/
244#define OVERFLOW_SIZE (SQLITE_PAGE_SIZE-sizeof(Pgno))
245
246/*
drh306dc212001-05-21 13:45:10 +0000247** When the key and data for a single entry in the BTree will not fit in
drh8c42ca92001-06-22 19:15:00 +0000248** the MX_LOCAL_PAYLOAD bytes of space available on the database page,
drh8b2f49b2001-06-08 00:21:52 +0000249** then all extra bytes are written to a linked list of overflow pages.
drh306dc212001-05-21 13:45:10 +0000250** Each overflow page is an instance of the following structure.
251**
252** Unused pages in the database are also represented by instances of
drhbd03cae2001-06-02 02:40:57 +0000253** the OverflowPage structure. The PageOne.freeList field is the
drh306dc212001-05-21 13:45:10 +0000254** page number of the first page in a linked list of unused database
255** pages.
256*/
drh2af926b2001-05-15 00:39:25 +0000257struct OverflowPage {
drh14acc042001-06-10 19:56:58 +0000258 Pgno iNext;
drh5e2f8b92001-05-28 00:41:15 +0000259 char aPayload[OVERFLOW_SIZE];
drh7e3b0a02001-04-28 16:52:40 +0000260};
drh7e3b0a02001-04-28 16:52:40 +0000261
262/*
263** For every page in the database file, an instance of the following structure
drh14acc042001-06-10 19:56:58 +0000264** is stored in memory. The u.aDisk[] array contains the raw bits read from
drhbd03cae2001-06-02 02:40:57 +0000265** the disk. The rest is auxiliary information that held in memory only. The
266** auxiliary info is only valid for regular database pages - it is not
267** used for overflow pages and pages on the freelist.
drh306dc212001-05-21 13:45:10 +0000268**
drhbd03cae2001-06-02 02:40:57 +0000269** Of particular interest in the auxiliary info is the apCell[] entry. Each
drh14acc042001-06-10 19:56:58 +0000270** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are
drh306dc212001-05-21 13:45:10 +0000271** put in this array so that they can be accessed in constant time, rather
drhbd03cae2001-06-02 02:40:57 +0000272** than in linear time which would be needed if we had to walk the linked
273** list on every access.
drh72f82862001-05-24 21:06:34 +0000274**
drh14acc042001-06-10 19:56:58 +0000275** Note that apCell[] contains enough space to hold up to two more Cells
276** than can possibly fit on one page. In the steady state, every apCell[]
277** points to memory inside u.aDisk[]. But in the middle of an insert
278** operation, some apCell[] entries may temporarily point to data space
279** outside of u.aDisk[]. This is a transient situation that is quickly
280** resolved. But while it is happening, it is possible for a database
281** page to hold as many as two more cells than it might otherwise hold.
282** The extra too entries in apCell[] are an allowance for this situation.
283**
drh72f82862001-05-24 21:06:34 +0000284** The pParent field points back to the parent page. This allows us to
285** walk up the BTree from any leaf to the root. Care must be taken to
286** unref() the parent page pointer when this page is no longer referenced.
drhbd03cae2001-06-02 02:40:57 +0000287** The pageDestructor() routine handles that chore.
drh7e3b0a02001-04-28 16:52:40 +0000288*/
289struct MemPage {
drh14acc042001-06-10 19:56:58 +0000290 union {
291 char aDisk[SQLITE_PAGE_SIZE]; /* Page data stored on disk */
292 PageHdr hdr; /* Overlay page header */
293 } u;
drh5e2f8b92001-05-28 00:41:15 +0000294 int isInit; /* True if auxiliary data is initialized */
drh72f82862001-05-24 21:06:34 +0000295 MemPage *pParent; /* The parent of this page. NULL for root */
drh14acc042001-06-10 19:56:58 +0000296 int nFree; /* Number of free bytes in u.aDisk[] */
drh306dc212001-05-21 13:45:10 +0000297 int nCell; /* Number of entries on this page */
drh14acc042001-06-10 19:56:58 +0000298 int isOverfull; /* Some apCell[] points outside u.aDisk[] */
299 Cell *apCell[MX_CELL+2]; /* All data entires in sorted order */
drh8c42ca92001-06-22 19:15:00 +0000300};
drh7e3b0a02001-04-28 16:52:40 +0000301
302/*
drh3b7511c2001-05-26 13:15:44 +0000303** The in-memory image of a disk page has the auxiliary information appended
304** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
305** that extra information.
306*/
307#define EXTRA_SIZE (sizeof(MemPage)-SQLITE_PAGE_SIZE)
308
309/*
drha059ad02001-04-17 20:09:11 +0000310** Everything we need to know about an open database
311*/
312struct Btree {
313 Pager *pPager; /* The page cache */
drh306dc212001-05-21 13:45:10 +0000314 BtCursor *pCursor; /* A list of all open cursors */
drhbd03cae2001-06-02 02:40:57 +0000315 PageOne *page1; /* First page of the database */
drh306dc212001-05-21 13:45:10 +0000316 int inTrans; /* True if a transaction is in progress */
drha059ad02001-04-17 20:09:11 +0000317};
318typedef Btree Bt;
319
drh365d68f2001-05-11 11:02:46 +0000320/*
321** A cursor is a pointer to a particular entry in the BTree.
322** The entry is identified by its MemPage and the index in
drh5e2f8b92001-05-28 00:41:15 +0000323** MemPage.apCell[] of the entry.
drh365d68f2001-05-11 11:02:46 +0000324*/
drh72f82862001-05-24 21:06:34 +0000325struct BtCursor {
drh5e2f8b92001-05-28 00:41:15 +0000326 Btree *pBt; /* The Btree to which this cursor belongs */
drh14acc042001-06-10 19:56:58 +0000327 BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
drh8b2f49b2001-06-08 00:21:52 +0000328 Pgno pgnoRoot; /* The root page of this tree */
drh5e2f8b92001-05-28 00:41:15 +0000329 MemPage *pPage; /* Page that contains the entry */
drh8c42ca92001-06-22 19:15:00 +0000330 int idx; /* Index of the entry in pPage->apCell[] */
drh5e2f8b92001-05-28 00:41:15 +0000331 u8 bSkipNext; /* sqliteBtreeNext() is no-op if true */
332 u8 iMatch; /* compare result from last sqliteBtreeMoveto() */
drh365d68f2001-05-11 11:02:46 +0000333};
drh7e3b0a02001-04-28 16:52:40 +0000334
drha059ad02001-04-17 20:09:11 +0000335/*
drh3b7511c2001-05-26 13:15:44 +0000336** Compute the total number of bytes that a Cell needs on the main
drh5e2f8b92001-05-28 00:41:15 +0000337** database page. The number returned includes the Cell header,
338** local payload storage, and the pointer to overflow pages (if
drh8c42ca92001-06-22 19:15:00 +0000339** applicable). Additional space allocated on overflow pages
drhbd03cae2001-06-02 02:40:57 +0000340** is NOT included in the value returned from this routine.
drh3b7511c2001-05-26 13:15:44 +0000341*/
342static int cellSize(Cell *pCell){
343 int n = pCell->h.nKey + pCell->h.nData;
344 if( n>MX_LOCAL_PAYLOAD ){
345 n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
346 }else{
347 n = ROUNDUP(n);
348 }
349 n += sizeof(CellHdr);
350 return n;
351}
352
353/*
drh72f82862001-05-24 21:06:34 +0000354** Defragment the page given. All Cells are moved to the
355** beginning of the page and all free space is collected
356** into one big FreeBlk at the end of the page.
drh365d68f2001-05-11 11:02:46 +0000357*/
358static void defragmentPage(MemPage *pPage){
drh14acc042001-06-10 19:56:58 +0000359 int pc, i, n;
drh2af926b2001-05-15 00:39:25 +0000360 FreeBlk *pFBlk;
361 char newPage[SQLITE_PAGE_SIZE];
362
drhbd03cae2001-06-02 02:40:57 +0000363 pc = sizeof(PageHdr);
drh14acc042001-06-10 19:56:58 +0000364 pPage->u.hdr.firstCell = pc;
365 memcpy(newPage, pPage->u.aDisk, pc);
drh2af926b2001-05-15 00:39:25 +0000366 for(i=0; i<pPage->nCell; i++){
drh8c42ca92001-06-22 19:15:00 +0000367 Cell *pCell = (Cell*)&pPage->apCell[i];
368
369 /* This routine should never be called on an overfull page. The
370 ** following asserts verify that constraint. */
drh7c717f72001-06-24 20:39:41 +0000371 assert( Addr(pCell) > Addr(pPage) );
372 assert( Addr(pCell) < Addr(pPage) + SQLITE_PAGE_SIZE );
drh8c42ca92001-06-22 19:15:00 +0000373
drh3b7511c2001-05-26 13:15:44 +0000374 n = cellSize(pCell);
drh14acc042001-06-10 19:56:58 +0000375 pCell->h.iNext = i<pPage->nCell-1 ? pc + n : 0;
drh2af926b2001-05-15 00:39:25 +0000376 memcpy(&newPage[pc], pCell, n);
drh14acc042001-06-10 19:56:58 +0000377 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc];
drh2af926b2001-05-15 00:39:25 +0000378 pc += n;
379 }
drh72f82862001-05-24 21:06:34 +0000380 assert( pPage->nFree==SQLITE_PAGE_SIZE-pc );
drh14acc042001-06-10 19:56:58 +0000381 memcpy(pPage->u.aDisk, newPage, pc);
drh8c42ca92001-06-22 19:15:00 +0000382 pFBlk = (FreeBlk*)&pPage->u.aDisk[pc];
drh2af926b2001-05-15 00:39:25 +0000383 pFBlk->iSize = SQLITE_PAGE_SIZE - pc;
384 pFBlk->iNext = 0;
drh14acc042001-06-10 19:56:58 +0000385 pPage->u.hdr.firstFree = pc;
drh2af926b2001-05-15 00:39:25 +0000386 memset(&pFBlk[1], 0, SQLITE_PAGE_SIZE - pc - sizeof(FreeBlk));
drh365d68f2001-05-11 11:02:46 +0000387}
388
drha059ad02001-04-17 20:09:11 +0000389/*
drh8b2f49b2001-06-08 00:21:52 +0000390** Allocate nByte bytes of space on a page. nByte must be a
391** multiple of 4.
drhbd03cae2001-06-02 02:40:57 +0000392**
drh14acc042001-06-10 19:56:58 +0000393** Return the index into pPage->u.aDisk[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000394** the new allocation. Or return 0 if there is not enough free
395** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000396**
drh72f82862001-05-24 21:06:34 +0000397** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000398** nBytes of contiguous free space, then this routine automatically
399** calls defragementPage() to consolidate all free space before
400** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000401*/
drhbd03cae2001-06-02 02:40:57 +0000402static int allocateSpace(MemPage *pPage, int nByte){
drh2af926b2001-05-15 00:39:25 +0000403 FreeBlk *p;
404 u16 *pIdx;
405 int start;
drh8c42ca92001-06-22 19:15:00 +0000406 int cnt = 0;
drh72f82862001-05-24 21:06:34 +0000407
drh5e2f8b92001-05-28 00:41:15 +0000408 assert( nByte==ROUNDUP(nByte) );
drh14acc042001-06-10 19:56:58 +0000409 if( pPage->nFree<nByte || pPage->isOverfull ) return 0;
410 pIdx = &pPage->u.hdr.firstFree;
411 p = (FreeBlk*)&pPage->u.aDisk[*pIdx];
drh2af926b2001-05-15 00:39:25 +0000412 while( p->iSize<nByte ){
drh8c42ca92001-06-22 19:15:00 +0000413 assert( cnt++ < SQLITE_PAGE_SIZE/4 );
drh2af926b2001-05-15 00:39:25 +0000414 if( p->iNext==0 ){
415 defragmentPage(pPage);
drh14acc042001-06-10 19:56:58 +0000416 pIdx = &pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000417 }else{
418 pIdx = &p->iNext;
419 }
drh14acc042001-06-10 19:56:58 +0000420 p = (FreeBlk*)&pPage->u.aDisk[*pIdx];
drh2af926b2001-05-15 00:39:25 +0000421 }
422 if( p->iSize==nByte ){
423 start = *pIdx;
424 *pIdx = p->iNext;
425 }else{
drh8c42ca92001-06-22 19:15:00 +0000426 FreeBlk *pNew;
drh72f82862001-05-24 21:06:34 +0000427 start = *pIdx;
drh8c42ca92001-06-22 19:15:00 +0000428 pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte];
drh72f82862001-05-24 21:06:34 +0000429 pNew->iNext = p->iNext;
430 pNew->iSize = p->iSize - nByte;
431 *pIdx = start + nByte;
drh2af926b2001-05-15 00:39:25 +0000432 }
433 pPage->nFree -= nByte;
434 return start;
drh7e3b0a02001-04-28 16:52:40 +0000435}
436
437/*
drh14acc042001-06-10 19:56:58 +0000438** Return a section of the MemPage.u.aDisk[] to the freelist.
439** The first byte of the new free block is pPage->u.aDisk[start]
440** and the size of the block is "size" bytes. Size must be
441** a multiple of 4.
drh306dc212001-05-21 13:45:10 +0000442**
443** Most of the effort here is involved in coalesing adjacent
444** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000445*/
446static void freeSpace(MemPage *pPage, int start, int size){
drh2af926b2001-05-15 00:39:25 +0000447 int end = start + size;
448 u16 *pIdx, idx;
449 FreeBlk *pFBlk;
450 FreeBlk *pNew;
451 FreeBlk *pNext;
452
453 assert( size == ROUNDUP(size) );
454 assert( start == ROUNDUP(start) );
drh14acc042001-06-10 19:56:58 +0000455 pIdx = &pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000456 idx = *pIdx;
457 while( idx!=0 && idx<start ){
drh14acc042001-06-10 19:56:58 +0000458 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000459 if( idx + pFBlk->iSize == start ){
460 pFBlk->iSize += size;
461 if( idx + pFBlk->iSize == pFBlk->iNext ){
drh8c42ca92001-06-22 19:15:00 +0000462 pNext = (FreeBlk*)&pPage->u.aDisk[pFBlk->iNext];
drh2af926b2001-05-15 00:39:25 +0000463 pFBlk->iSize += pNext->iSize;
464 pFBlk->iNext = pNext->iNext;
465 }
466 pPage->nFree += size;
467 return;
468 }
469 pIdx = &pFBlk->iNext;
470 idx = *pIdx;
471 }
drh14acc042001-06-10 19:56:58 +0000472 pNew = (FreeBlk*)&pPage->u.aDisk[start];
drh2af926b2001-05-15 00:39:25 +0000473 if( idx != end ){
474 pNew->iSize = size;
475 pNew->iNext = idx;
476 }else{
drh14acc042001-06-10 19:56:58 +0000477 pNext = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000478 pNew->iSize = size + pNext->iSize;
479 pNew->iNext = pNext->iNext;
480 }
481 *pIdx = start;
482 pPage->nFree += size;
drh7e3b0a02001-04-28 16:52:40 +0000483}
484
485/*
486** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000487**
drhbd03cae2001-06-02 02:40:57 +0000488** The pParent parameter must be a pointer to the MemPage which
489** is the parent of the page being initialized. The root of the
drh8b2f49b2001-06-08 00:21:52 +0000490** BTree (usually page 2) has no parent and so for that page,
491** pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000492**
drh72f82862001-05-24 21:06:34 +0000493** Return SQLITE_OK on success. If we see that the page does
494** not contained a well-formed database page, then return
495** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
496** guarantee that the page is well-formed. It only shows that
497** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000498*/
drh72f82862001-05-24 21:06:34 +0000499static int initPage(MemPage *pPage, Pgno pgnoThis, MemPage *pParent){
drh14acc042001-06-10 19:56:58 +0000500 int idx; /* An index into pPage->u.aDisk[] */
501 Cell *pCell; /* A pointer to a Cell in pPage->u.aDisk[] */
502 FreeBlk *pFBlk; /* A pointer to a free block in pPage->u.aDisk[] */
drh5e2f8b92001-05-28 00:41:15 +0000503 int sz; /* The size of a Cell in bytes */
504 int freeSpace; /* Amount of free space on the page */
drh2af926b2001-05-15 00:39:25 +0000505
drh5e2f8b92001-05-28 00:41:15 +0000506 if( pPage->pParent ){
507 assert( pPage->pParent==pParent );
508 return SQLITE_OK;
509 }
510 if( pParent ){
511 pPage->pParent = pParent;
512 sqlitepager_ref(pParent);
513 }
514 if( pPage->isInit ) return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000515 pPage->isInit = 1;
drh7e3b0a02001-04-28 16:52:40 +0000516 pPage->nCell = 0;
drhbd03cae2001-06-02 02:40:57 +0000517 freeSpace = SQLITE_PAGE_SIZE - sizeof(PageHdr);
drh14acc042001-06-10 19:56:58 +0000518 idx = pPage->u.hdr.firstCell;
drh7e3b0a02001-04-28 16:52:40 +0000519 while( idx!=0 ){
drh8c42ca92001-06-22 19:15:00 +0000520 if( idx>SQLITE_PAGE_SIZE-MIN_CELL_SIZE ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000521 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh8c42ca92001-06-22 19:15:00 +0000522 if( idx!=ROUNDUP(idx) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000523 pCell = (Cell*)&pPage->u.aDisk[idx];
drh5e2f8b92001-05-28 00:41:15 +0000524 sz = cellSize(pCell);
525 if( idx+sz > SQLITE_PAGE_SIZE ) goto page_format_error;
526 freeSpace -= sz;
527 pPage->apCell[pPage->nCell++] = pCell;
drh3b7511c2001-05-26 13:15:44 +0000528 idx = pCell->h.iNext;
drh2af926b2001-05-15 00:39:25 +0000529 }
530 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +0000531 idx = pPage->u.hdr.firstFree;
drh2af926b2001-05-15 00:39:25 +0000532 while( idx!=0 ){
533 if( idx>SQLITE_PAGE_SIZE-sizeof(FreeBlk) ) goto page_format_error;
drhbd03cae2001-06-02 02:40:57 +0000534 if( idx<sizeof(PageHdr) ) goto page_format_error;
drh14acc042001-06-10 19:56:58 +0000535 pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
drh2af926b2001-05-15 00:39:25 +0000536 pPage->nFree += pFBlk->iSize;
drh7c717f72001-06-24 20:39:41 +0000537 if( pFBlk->iNext>0 && pFBlk->iNext <= idx ) goto page_format_error;
drh2af926b2001-05-15 00:39:25 +0000538 idx = pFBlk->iNext;
drh7e3b0a02001-04-28 16:52:40 +0000539 }
drh8b2f49b2001-06-08 00:21:52 +0000540 if( pPage->nCell==0 && pPage->nFree==0 ){
541 /* As a special case, an uninitialized root page appears to be
542 ** an empty database */
543 return SQLITE_OK;
544 }
drh5e2f8b92001-05-28 00:41:15 +0000545 if( pPage->nFree!=freeSpace ) goto page_format_error;
drh7e3b0a02001-04-28 16:52:40 +0000546 return SQLITE_OK;
drh2af926b2001-05-15 00:39:25 +0000547
548page_format_error:
549 return SQLITE_CORRUPT;
drh7e3b0a02001-04-28 16:52:40 +0000550}
551
552/*
drh8b2f49b2001-06-08 00:21:52 +0000553** Set up a raw page so that it looks like a database page holding
554** no entries.
drhbd03cae2001-06-02 02:40:57 +0000555*/
556static void zeroPage(MemPage *pPage){
557 PageHdr *pHdr;
558 FreeBlk *pFBlk;
559 memset(pPage, 0, SQLITE_PAGE_SIZE);
drh14acc042001-06-10 19:56:58 +0000560 pHdr = &pPage->u.hdr;
drhbd03cae2001-06-02 02:40:57 +0000561 pHdr->firstCell = 0;
562 pHdr->firstFree = sizeof(*pHdr);
563 pFBlk = (FreeBlk*)&pHdr[1];
564 pFBlk->iNext = 0;
565 pFBlk->iSize = SQLITE_PAGE_SIZE - sizeof(*pHdr);
drh8c42ca92001-06-22 19:15:00 +0000566 pPage->nFree = pFBlk->iSize;
567 pPage->nCell = 0;
568 pPage->isOverfull = 0;
drhbd03cae2001-06-02 02:40:57 +0000569}
570
571/*
drh72f82862001-05-24 21:06:34 +0000572** This routine is called when the reference count for a page
573** reaches zero. We need to unref the pParent pointer when that
574** happens.
575*/
576static void pageDestructor(void *pData){
577 MemPage *pPage = (MemPage*)pData;
578 if( pPage->pParent ){
579 MemPage *pParent = pPage->pParent;
580 pPage->pParent = 0;
581 sqlitepager_unref(pParent);
582 }
583}
584
585/*
drh306dc212001-05-21 13:45:10 +0000586** Open a new database.
587**
588** Actually, this routine just sets up the internal data structures
drh72f82862001-05-24 21:06:34 +0000589** for accessing the database. We do not open the database file
590** until the first page is loaded.
drha059ad02001-04-17 20:09:11 +0000591*/
592int sqliteBtreeOpen(const char *zFilename, int mode, Btree **ppBtree){
593 Btree *pBt;
drh8c42ca92001-06-22 19:15:00 +0000594 int rc;
drha059ad02001-04-17 20:09:11 +0000595
596 pBt = sqliteMalloc( sizeof(*pBt) );
597 if( pBt==0 ){
drh8c42ca92001-06-22 19:15:00 +0000598 *ppBtree = 0;
drha059ad02001-04-17 20:09:11 +0000599 return SQLITE_NOMEM;
600 }
drh8c42ca92001-06-22 19:15:00 +0000601 rc = sqlitepager_open(&pBt->pPager, zFilename, 100, EXTRA_SIZE);
drha059ad02001-04-17 20:09:11 +0000602 if( rc!=SQLITE_OK ){
603 if( pBt->pPager ) sqlitepager_close(pBt->pPager);
604 sqliteFree(pBt);
605 *ppBtree = 0;
606 return rc;
607 }
drh72f82862001-05-24 21:06:34 +0000608 sqlitepager_set_destructor(pBt->pPager, pageDestructor);
drha059ad02001-04-17 20:09:11 +0000609 pBt->pCursor = 0;
610 pBt->page1 = 0;
611 *ppBtree = pBt;
612 return SQLITE_OK;
613}
614
615/*
616** Close an open database and invalidate all cursors.
617*/
618int sqliteBtreeClose(Btree *pBt){
619 while( pBt->pCursor ){
620 sqliteBtreeCloseCursor(pBt->pCursor);
621 }
622 sqlitepager_close(pBt->pPager);
623 sqliteFree(pBt);
624 return SQLITE_OK;
625}
626
627/*
drh306dc212001-05-21 13:45:10 +0000628** Get a reference to page1 of the database file. This will
629** also acquire a readlock on that file.
630**
631** SQLITE_OK is returned on success. If the file is not a
632** well-formed database file, then SQLITE_CORRUPT is returned.
633** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
634** is returned if we run out of memory. SQLITE_PROTOCOL is returned
635** if there is a locking protocol violation.
636*/
637static int lockBtree(Btree *pBt){
638 int rc;
639 if( pBt->page1 ) return SQLITE_OK;
drh8c42ca92001-06-22 19:15:00 +0000640 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1);
drh306dc212001-05-21 13:45:10 +0000641 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +0000642
643 /* Do some checking to help insure the file we opened really is
644 ** a valid database file.
645 */
646 if( sqlitepager_pagecount(pBt->pPager)>0 ){
drhbd03cae2001-06-02 02:40:57 +0000647 PageOne *pP1 = pBt->page1;
drh8c42ca92001-06-22 19:15:00 +0000648 if( strcmp(pP1->zMagic,zMagicHeader)!=0 || pP1->iMagic!=MAGIC ){
drh306dc212001-05-21 13:45:10 +0000649 rc = SQLITE_CORRUPT;
drh72f82862001-05-24 21:06:34 +0000650 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +0000651 }
652 }
653 return rc;
654
drh72f82862001-05-24 21:06:34 +0000655page1_init_failed:
drh306dc212001-05-21 13:45:10 +0000656 sqlitepager_unref(pBt->page1);
657 pBt->page1 = 0;
drh72f82862001-05-24 21:06:34 +0000658 return rc;
drh306dc212001-05-21 13:45:10 +0000659}
660
661/*
drh8c42ca92001-06-22 19:15:00 +0000662** Create a new database by initializing the first two pages of the
663** file.
drh8b2f49b2001-06-08 00:21:52 +0000664*/
665static int newDatabase(Btree *pBt){
666 MemPage *pRoot;
667 PageOne *pP1;
drh8c42ca92001-06-22 19:15:00 +0000668 int rc;
drh7c717f72001-06-24 20:39:41 +0000669 if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK;
drh8b2f49b2001-06-08 00:21:52 +0000670 pP1 = pBt->page1;
671 rc = sqlitepager_write(pBt->page1);
672 if( rc ) return rc;
drh8c42ca92001-06-22 19:15:00 +0000673 rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot);
drh8b2f49b2001-06-08 00:21:52 +0000674 if( rc ) return rc;
675 rc = sqlitepager_write(pRoot);
676 if( rc ){
677 sqlitepager_unref(pRoot);
678 return rc;
679 }
680 strcpy(pP1->zMagic, zMagicHeader);
drh8c42ca92001-06-22 19:15:00 +0000681 pP1->iMagic = MAGIC;
drh8b2f49b2001-06-08 00:21:52 +0000682 zeroPage(pRoot);
683 sqlitepager_unref(pRoot);
684 return SQLITE_OK;
685}
686
687/*
drh72f82862001-05-24 21:06:34 +0000688** Attempt to start a new transaction.
drh8b2f49b2001-06-08 00:21:52 +0000689**
690** A transaction must be started before attempting any changes
691** to the database. None of the following routines will work
692** unless a transaction is started first:
693**
694** sqliteBtreeCreateTable()
695** sqliteBtreeClearTable()
696** sqliteBtreeDropTable()
697** sqliteBtreeInsert()
698** sqliteBtreeDelete()
699** sqliteBtreeUpdateMeta()
drha059ad02001-04-17 20:09:11 +0000700*/
701int sqliteBtreeBeginTrans(Btree *pBt){
702 int rc;
703 if( pBt->inTrans ) return SQLITE_ERROR;
704 if( pBt->page1==0 ){
drh7e3b0a02001-04-28 16:52:40 +0000705 rc = lockBtree(pBt);
drh8c42ca92001-06-22 19:15:00 +0000706 if( rc!=SQLITE_OK ){
707 return rc;
708 }
drha059ad02001-04-17 20:09:11 +0000709 }
710 rc = sqlitepager_write(pBt->page1);
drh8c42ca92001-06-22 19:15:00 +0000711 if( rc!=SQLITE_OK ){
712 return rc;
drha059ad02001-04-17 20:09:11 +0000713 }
drh8c42ca92001-06-22 19:15:00 +0000714 pBt->inTrans = 1;
715 rc = newDatabase(pBt);
716 return rc;
drha059ad02001-04-17 20:09:11 +0000717}
718
719/*
drha059ad02001-04-17 20:09:11 +0000720** Remove the last reference to the database file. This will
721** remove the read lock.
722*/
723static void unlockBtree(Btree *pBt){
drh7c717f72001-06-24 20:39:41 +0000724 if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){
drha059ad02001-04-17 20:09:11 +0000725 sqlitepager_unref(pBt->page1);
726 pBt->page1 = 0;
727 pBt->inTrans = 0;
728 }
729}
730
731/*
732** Commit the transaction currently in progress. All cursors
733** must be closed before this routine is called.
734*/
735int sqliteBtreeCommit(Btree *pBt){
736 int rc;
drh7c717f72001-06-24 20:39:41 +0000737 if( pBt->pCursor!=0 || pBt->inTrans==0 ) return SQLITE_ERROR;
drha059ad02001-04-17 20:09:11 +0000738 rc = sqlitepager_commit(pBt->pPager);
drh7c717f72001-06-24 20:39:41 +0000739 pBt->inTrans = 0;
drha059ad02001-04-17 20:09:11 +0000740 unlockBtree(pBt);
741 return rc;
742}
743
744/*
745** Rollback the transaction in progress. All cursors must be
746** closed before this routine is called.
747*/
748int sqliteBtreeRollback(Btree *pBt){
749 int rc;
drh72f82862001-05-24 21:06:34 +0000750 if( pBt->pCursor!=0 ) return SQLITE_ERROR;
drh7c717f72001-06-24 20:39:41 +0000751 if( pBt->inTrans==0 ) return SQLITE_OK;
752 pBt->inTrans = 0;
drha059ad02001-04-17 20:09:11 +0000753 rc = sqlitepager_rollback(pBt->pPager);
754 unlockBtree(pBt);
755 return rc;
756}
757
758/*
drh8b2f49b2001-06-08 00:21:52 +0000759** Create a new cursor for the BTree whose root is on the page
760** iTable. The act of acquiring a cursor gets a read lock on
761** the database file.
drha059ad02001-04-17 20:09:11 +0000762*/
drh8b2f49b2001-06-08 00:21:52 +0000763int sqliteBtreeCursor(Btree *pBt, int iTable, BtCursor **ppCur){
drha059ad02001-04-17 20:09:11 +0000764 int rc;
765 BtCursor *pCur;
766 if( pBt->page1==0 ){
767 rc = lockBtree(pBt);
768 if( rc!=SQLITE_OK ){
769 *ppCur = 0;
770 return rc;
771 }
772 }
773 pCur = sqliteMalloc( sizeof(*pCur) );
774 if( pCur==0 ){
drhbd03cae2001-06-02 02:40:57 +0000775 rc = SQLITE_NOMEM;
776 goto create_cursor_exception;
777 }
drh8b2f49b2001-06-08 00:21:52 +0000778 pCur->pgnoRoot = (Pgno)iTable;
drh8c42ca92001-06-22 19:15:00 +0000779 rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +0000780 if( rc!=SQLITE_OK ){
781 goto create_cursor_exception;
782 }
drh8b2f49b2001-06-08 00:21:52 +0000783 rc = initPage(pCur->pPage, pCur->pgnoRoot, 0);
drhbd03cae2001-06-02 02:40:57 +0000784 if( rc!=SQLITE_OK ){
785 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +0000786 }
drh14acc042001-06-10 19:56:58 +0000787 pCur->pBt = pBt;
788 pCur->idx = 0;
drha059ad02001-04-17 20:09:11 +0000789 pCur->pNext = pBt->pCursor;
790 if( pCur->pNext ){
791 pCur->pNext->pPrev = pCur;
792 }
drh14acc042001-06-10 19:56:58 +0000793 pCur->pPrev = 0;
drha059ad02001-04-17 20:09:11 +0000794 pBt->pCursor = pCur;
drh2af926b2001-05-15 00:39:25 +0000795 *ppCur = pCur;
796 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +0000797
798create_cursor_exception:
799 *ppCur = 0;
800 if( pCur ){
801 if( pCur->pPage ) sqlitepager_unref(pCur->pPage);
802 sqliteFree(pCur);
803 }
drh8c42ca92001-06-22 19:15:00 +0000804 unlockBtree(pBt);
drhbd03cae2001-06-02 02:40:57 +0000805 return rc;
drha059ad02001-04-17 20:09:11 +0000806}
807
808/*
drhbd03cae2001-06-02 02:40:57 +0000809** Close a cursor. The lock on the database file is released
810** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +0000811*/
812int sqliteBtreeCloseCursor(BtCursor *pCur){
813 Btree *pBt = pCur->pBt;
drha059ad02001-04-17 20:09:11 +0000814 if( pCur->pPrev ){
815 pCur->pPrev->pNext = pCur->pNext;
816 }else{
817 pBt->pCursor = pCur->pNext;
818 }
819 if( pCur->pNext ){
820 pCur->pNext->pPrev = pCur->pPrev;
821 }
drh2af926b2001-05-15 00:39:25 +0000822 sqlitepager_unref(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +0000823 unlockBtree(pBt);
drha059ad02001-04-17 20:09:11 +0000824 sqliteFree(pCur);
drh8c42ca92001-06-22 19:15:00 +0000825 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +0000826}
827
drh7e3b0a02001-04-28 16:52:40 +0000828/*
drh5e2f8b92001-05-28 00:41:15 +0000829** Make a temporary cursor by filling in the fields of pTempCur.
830** The temporary cursor is not on the cursor list for the Btree.
831*/
drh14acc042001-06-10 19:56:58 +0000832static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh5e2f8b92001-05-28 00:41:15 +0000833 memcpy(pTempCur, pCur, sizeof(*pCur));
834 pTempCur->pNext = 0;
835 pTempCur->pPrev = 0;
836 sqlitepager_ref(pTempCur->pPage);
837}
838
839/*
drhbd03cae2001-06-02 02:40:57 +0000840** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +0000841** function above.
842*/
drh14acc042001-06-10 19:56:58 +0000843static void releaseTempCursor(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +0000844 sqlitepager_unref(pCur->pPage);
845}
846
847/*
drhbd03cae2001-06-02 02:40:57 +0000848** Set *pSize to the number of bytes of key in the entry the
849** cursor currently points to. Always return SQLITE_OK.
850** Failure is not possible. If the cursor is not currently
851** pointing to an entry (which can happen, for example, if
852** the database is empty) then *pSize is set to 0.
drh7e3b0a02001-04-28 16:52:40 +0000853*/
drh72f82862001-05-24 21:06:34 +0000854int sqliteBtreeKeySize(BtCursor *pCur, int *pSize){
drh2af926b2001-05-15 00:39:25 +0000855 Cell *pCell;
856 MemPage *pPage;
857
858 pPage = pCur->pPage;
drh72f82862001-05-24 21:06:34 +0000859 assert( pPage!=0 );
860 if( pCur->idx >= pPage->nCell ){
861 *pSize = 0;
862 }else{
drh5e2f8b92001-05-28 00:41:15 +0000863 pCell = pPage->apCell[pCur->idx];
drh8c42ca92001-06-22 19:15:00 +0000864 *pSize = pCell->h.nKey;
drh72f82862001-05-24 21:06:34 +0000865 }
866 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +0000867}
drh2af926b2001-05-15 00:39:25 +0000868
drh72f82862001-05-24 21:06:34 +0000869/*
870** Read payload information from the entry that the pCur cursor is
871** pointing to. Begin reading the payload at "offset" and read
872** a total of "amt" bytes. Put the result in zBuf.
873**
874** This routine does not make a distinction between key and data.
875** It just reads bytes from the payload area.
876*/
drh2af926b2001-05-15 00:39:25 +0000877static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){
drh5e2f8b92001-05-28 00:41:15 +0000878 char *aPayload;
drh2af926b2001-05-15 00:39:25 +0000879 Pgno nextPage;
drh8c42ca92001-06-22 19:15:00 +0000880 int rc;
drh72f82862001-05-24 21:06:34 +0000881 assert( pCur!=0 && pCur->pPage!=0 );
drh8c42ca92001-06-22 19:15:00 +0000882 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
883 aPayload = pCur->pPage->apCell[pCur->idx]->aPayload;
drh2af926b2001-05-15 00:39:25 +0000884 if( offset<MX_LOCAL_PAYLOAD ){
885 int a = amt;
886 if( a+offset>MX_LOCAL_PAYLOAD ){
887 a = MX_LOCAL_PAYLOAD - offset;
888 }
drh5e2f8b92001-05-28 00:41:15 +0000889 memcpy(zBuf, &aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +0000890 if( a==amt ){
891 return SQLITE_OK;
892 }
893 offset += a;
894 zBuf += a;
895 amt -= a;
drhbd03cae2001-06-02 02:40:57 +0000896 }
897 if( amt>0 ){
drh8c42ca92001-06-22 19:15:00 +0000898 nextPage = pCur->pPage->apCell[pCur->idx]->ovfl;
drh2af926b2001-05-15 00:39:25 +0000899 }
900 while( amt>0 && nextPage ){
901 OverflowPage *pOvfl;
drh8c42ca92001-06-22 19:15:00 +0000902 rc = sqlitepager_get(pCur->pBt->pPager, nextPage, (void**)&pOvfl);
drh2af926b2001-05-15 00:39:25 +0000903 if( rc!=0 ){
904 return rc;
905 }
drh14acc042001-06-10 19:56:58 +0000906 nextPage = pOvfl->iNext;
drh2af926b2001-05-15 00:39:25 +0000907 if( offset<OVERFLOW_SIZE ){
908 int a = amt;
909 if( a + offset > OVERFLOW_SIZE ){
910 a = OVERFLOW_SIZE - offset;
911 }
drh5e2f8b92001-05-28 00:41:15 +0000912 memcpy(zBuf, &pOvfl->aPayload[offset], a);
drh2af926b2001-05-15 00:39:25 +0000913 amt -= a;
914 zBuf += a;
915 }
drhbd03cae2001-06-02 02:40:57 +0000916 offset -= OVERFLOW_SIZE;
drh2af926b2001-05-15 00:39:25 +0000917 sqlitepager_unref(pOvfl);
918 }
919 return amt==0 ? SQLITE_OK : SQLITE_CORRUPT;
920}
921
drh72f82862001-05-24 21:06:34 +0000922/*
923** Read part of the key associated with cursor pCur. A total
924** of "amt" bytes will be transfered into zBuf[]. The transfer
925** begins at "offset". If the key does not contain enough data
926** to satisfy the request, no data is fetched and this routine
927** returns SQLITE_ERROR.
928*/
929int sqliteBtreeKey(BtCursor *pCur, int offset, int amt, char *zBuf){
930 Cell *pCell;
931 MemPage *pPage;
drha059ad02001-04-17 20:09:11 +0000932
drh72f82862001-05-24 21:06:34 +0000933 if( amt<0 ) return SQLITE_ERROR;
934 if( offset<0 ) return SQLITE_ERROR;
935 if( amt==0 ) return SQLITE_OK;
936 pPage = pCur->pPage;
937 assert( pPage!=0 );
938 if( pCur->idx >= pPage->nCell ){
939 return SQLITE_ERROR;
940 }
drh5e2f8b92001-05-28 00:41:15 +0000941 pCell = pPage->apCell[pCur->idx];
drh3b7511c2001-05-26 13:15:44 +0000942 if( amt+offset > pCell->h.nKey ){
drhbd03cae2001-06-02 02:40:57 +0000943 return SQLITE_ERROR;
944 }
drh72f82862001-05-24 21:06:34 +0000945 return getPayload(pCur, offset, amt, zBuf);
946}
947
948/*
drhbd03cae2001-06-02 02:40:57 +0000949** Set *pSize to the number of bytes of data in the entry the
950** cursor currently points to. Always return SQLITE_OK.
951** Failure is not possible. If the cursor is not currently
952** pointing to an entry (which can happen, for example, if
953** the database is empty) then *pSize is set to 0.
drh72f82862001-05-24 21:06:34 +0000954*/
955int sqliteBtreeDataSize(BtCursor *pCur, int *pSize){
956 Cell *pCell;
957 MemPage *pPage;
958
959 pPage = pCur->pPage;
960 assert( pPage!=0 );
961 if( pCur->idx >= pPage->nCell ){
962 *pSize = 0;
963 }else{
drh5e2f8b92001-05-28 00:41:15 +0000964 pCell = pPage->apCell[pCur->idx];
drh3b7511c2001-05-26 13:15:44 +0000965 *pSize = pCell->h.nData;
drh72f82862001-05-24 21:06:34 +0000966 }
967 return SQLITE_OK;
968}
969
970/*
971** Read part of the data associated with cursor pCur. A total
972** of "amt" bytes will be transfered into zBuf[]. The transfer
973** begins at "offset". If the size of the data in the record
974** is insufficent to satisfy this request then no data is read
975** and this routine returns SQLITE_ERROR.
976*/
977int sqliteBtreeData(BtCursor *pCur, int offset, int amt, char *zBuf){
978 Cell *pCell;
979 MemPage *pPage;
980
981 if( amt<0 ) return SQLITE_ERROR;
982 if( offset<0 ) return SQLITE_ERROR;
983 if( amt==0 ) return SQLITE_OK;
984 pPage = pCur->pPage;
985 assert( pPage!=0 );
986 if( pCur->idx >= pPage->nCell ){
987 return SQLITE_ERROR;
988 }
drh5e2f8b92001-05-28 00:41:15 +0000989 pCell = pPage->apCell[pCur->idx];
drhbd03cae2001-06-02 02:40:57 +0000990 if( amt+offset > pCell->h.nData ){
991 return SQLITE_ERROR;
992 }
drh3b7511c2001-05-26 13:15:44 +0000993 return getPayload(pCur, offset + pCell->h.nKey, amt, zBuf);
drh72f82862001-05-24 21:06:34 +0000994}
drha059ad02001-04-17 20:09:11 +0000995
drh2af926b2001-05-15 00:39:25 +0000996/*
997** Compare the key for the entry that pCur points to against the
998** given key (pKey,nKeyOrig). Put the comparison result in *pResult.
999** The result is negative if pCur<pKey, zero if they are equal and
1000** positive if pCur>pKey.
1001**
1002** SQLITE_OK is returned on success. If part of the cursor key
1003** is on overflow pages and we are unable to access those overflow
1004** pages, then some other value might be returned to indicate the
1005** reason for the error.
1006*/
1007static int compareKey(BtCursor *pCur, char *pKey, int nKeyOrig, int *pResult){
1008 Pgno nextPage;
1009 int nKey = nKeyOrig;
drh8c42ca92001-06-22 19:15:00 +00001010 int n, c, rc;
drh2af926b2001-05-15 00:39:25 +00001011 Cell *pCell;
1012
1013 assert( pCur->pPage );
1014 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drhbd03cae2001-06-02 02:40:57 +00001015 pCell = pCur->pPage->apCell[pCur->idx];
drh3b7511c2001-05-26 13:15:44 +00001016 if( nKey > pCell->h.nKey ){
1017 nKey = pCell->h.nKey;
drh2af926b2001-05-15 00:39:25 +00001018 }
1019 n = nKey;
1020 if( n>MX_LOCAL_PAYLOAD ){
1021 n = MX_LOCAL_PAYLOAD;
1022 }
drh5e2f8b92001-05-28 00:41:15 +00001023 c = memcmp(pCell->aPayload, pKey, n);
drh2af926b2001-05-15 00:39:25 +00001024 if( c!=0 ){
1025 *pResult = c;
1026 return SQLITE_OK;
1027 }
1028 pKey += n;
1029 nKey -= n;
drh3b7511c2001-05-26 13:15:44 +00001030 nextPage = pCell->ovfl;
drh2af926b2001-05-15 00:39:25 +00001031 while( nKey>0 ){
1032 OverflowPage *pOvfl;
1033 if( nextPage==0 ){
1034 return SQLITE_CORRUPT;
1035 }
drh8c42ca92001-06-22 19:15:00 +00001036 rc = sqlitepager_get(pCur->pBt->pPager, nextPage, (void**)&pOvfl);
drh72f82862001-05-24 21:06:34 +00001037 if( rc ){
drh2af926b2001-05-15 00:39:25 +00001038 return rc;
1039 }
drh14acc042001-06-10 19:56:58 +00001040 nextPage = pOvfl->iNext;
drh2af926b2001-05-15 00:39:25 +00001041 n = nKey;
1042 if( n>OVERFLOW_SIZE ){
1043 n = OVERFLOW_SIZE;
1044 }
drh5e2f8b92001-05-28 00:41:15 +00001045 c = memcmp(pOvfl->aPayload, pKey, n);
drh2af926b2001-05-15 00:39:25 +00001046 sqlitepager_unref(pOvfl);
1047 if( c!=0 ){
1048 *pResult = c;
1049 return SQLITE_OK;
1050 }
1051 nKey -= n;
1052 pKey += n;
1053 }
drh3b7511c2001-05-26 13:15:44 +00001054 c = pCell->h.nKey - nKeyOrig;
drh2af926b2001-05-15 00:39:25 +00001055 *pResult = c;
1056 return SQLITE_OK;
1057}
1058
drh72f82862001-05-24 21:06:34 +00001059/*
1060** Move the cursor down to a new child page.
1061*/
drh5e2f8b92001-05-28 00:41:15 +00001062static int moveToChild(BtCursor *pCur, int newPgno){
drh72f82862001-05-24 21:06:34 +00001063 int rc;
1064 MemPage *pNewPage;
1065
drh8c42ca92001-06-22 19:15:00 +00001066 rc = sqlitepager_get(pCur->pBt->pPager, newPgno, (void**)&pNewPage);
drh72f82862001-05-24 21:06:34 +00001067 if( rc ){
1068 return rc;
1069 }
drh5e2f8b92001-05-28 00:41:15 +00001070 initPage(pNewPage, newPgno, pCur->pPage);
drh72f82862001-05-24 21:06:34 +00001071 sqlitepager_unref(pCur->pPage);
1072 pCur->pPage = pNewPage;
1073 pCur->idx = 0;
1074 return SQLITE_OK;
1075}
1076
1077/*
drh5e2f8b92001-05-28 00:41:15 +00001078** Move the cursor up to the parent page.
1079**
1080** pCur->idx is set to the cell index that contains the pointer
1081** to the page we are coming from. If we are coming from the
1082** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00001083** the largest cell index.
drh72f82862001-05-24 21:06:34 +00001084*/
drh5e2f8b92001-05-28 00:41:15 +00001085static int moveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001086 Pgno oldPgno;
1087 MemPage *pParent;
drh8c42ca92001-06-22 19:15:00 +00001088 int i;
drh72f82862001-05-24 21:06:34 +00001089 pParent = pCur->pPage->pParent;
drhbd03cae2001-06-02 02:40:57 +00001090 if( pParent==0 ) return SQLITE_INTERNAL;
drh72f82862001-05-24 21:06:34 +00001091 oldPgno = sqlitepager_pagenumber(pCur->pPage);
drh72f82862001-05-24 21:06:34 +00001092 sqlitepager_ref(pParent);
1093 sqlitepager_unref(pCur->pPage);
1094 pCur->pPage = pParent;
drh8c42ca92001-06-22 19:15:00 +00001095 pCur->idx = pParent->nCell;
1096 for(i=0; i<pParent->nCell; i++){
1097 if( pParent->apCell[i]->h.leftChild==oldPgno ){
drh72f82862001-05-24 21:06:34 +00001098 pCur->idx = i;
1099 break;
1100 }
1101 }
drh5e2f8b92001-05-28 00:41:15 +00001102 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00001103}
1104
1105/*
1106** Move the cursor to the root page
1107*/
drh5e2f8b92001-05-28 00:41:15 +00001108static int moveToRoot(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00001109 MemPage *pNew;
drhbd03cae2001-06-02 02:40:57 +00001110 int rc;
1111
drh8c42ca92001-06-22 19:15:00 +00001112 rc = sqlitepager_get(pCur->pBt->pPager, pCur->pgnoRoot, (void**)&pNew);
drhbd03cae2001-06-02 02:40:57 +00001113 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001114 sqlitepager_unref(pCur->pPage);
1115 pCur->pPage = pNew;
1116 pCur->idx = 0;
1117 return SQLITE_OK;
1118}
drh2af926b2001-05-15 00:39:25 +00001119
drh5e2f8b92001-05-28 00:41:15 +00001120/*
1121** Move the cursor down to the left-most leaf entry beneath the
1122** entry to which it is currently pointing.
1123*/
1124static int moveToLeftmost(BtCursor *pCur){
1125 Pgno pgno;
1126 int rc;
1127
1128 while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
1129 rc = moveToChild(pCur, pgno);
1130 if( rc ) return rc;
1131 }
1132 return SQLITE_OK;
1133}
1134
1135
drha059ad02001-04-17 20:09:11 +00001136/* Move the cursor so that it points to an entry near pKey.
drh72f82862001-05-24 21:06:34 +00001137** Return a success code.
1138**
drh5e2f8b92001-05-28 00:41:15 +00001139** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00001140** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00001141** were present. The cursor might point to an entry that comes
1142** before or after the key.
1143**
drhbd03cae2001-06-02 02:40:57 +00001144** The result of comparing the key with the entry to which the
1145** cursor is left pointing is stored in pCur->iMatch. The same
1146** value is also written to *pRes if pRes!=NULL. The meaning of
1147** this value is as follows:
1148**
1149** *pRes<0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001150** is smaller than pKey.
drhbd03cae2001-06-02 02:40:57 +00001151**
1152** *pRes==0 The cursor is left pointing at an entry that
1153** exactly matches pKey.
1154**
1155** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00001156** is larger than pKey.
drha059ad02001-04-17 20:09:11 +00001157*/
drh72f82862001-05-24 21:06:34 +00001158int sqliteBtreeMoveto(BtCursor *pCur, void *pKey, int nKey, int *pRes){
1159 int rc;
drh7c717f72001-06-24 20:39:41 +00001160 pCur->bSkipNext = 0;
drh5e2f8b92001-05-28 00:41:15 +00001161 rc = moveToRoot(pCur);
drh72f82862001-05-24 21:06:34 +00001162 if( rc ) return rc;
1163 for(;;){
1164 int lwr, upr;
1165 Pgno chldPg;
1166 MemPage *pPage = pCur->pPage;
drh8b2f49b2001-06-08 00:21:52 +00001167 int c = -1;
drh72f82862001-05-24 21:06:34 +00001168 lwr = 0;
1169 upr = pPage->nCell-1;
1170 while( lwr<=upr ){
drh72f82862001-05-24 21:06:34 +00001171 pCur->idx = (lwr+upr)/2;
1172 rc = compareKey(pCur, pKey, nKey, &c);
1173 if( rc ) return rc;
1174 if( c==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001175 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001176 if( pRes ) *pRes = 0;
1177 return SQLITE_OK;
1178 }
1179 if( c<0 ){
1180 lwr = pCur->idx+1;
1181 }else{
1182 upr = pCur->idx-1;
1183 }
1184 }
1185 assert( lwr==upr+1 );
1186 if( lwr>=pPage->nCell ){
drh14acc042001-06-10 19:56:58 +00001187 chldPg = pPage->u.hdr.rightChild;
drh72f82862001-05-24 21:06:34 +00001188 }else{
drh5e2f8b92001-05-28 00:41:15 +00001189 chldPg = pPage->apCell[lwr]->h.leftChild;
drh72f82862001-05-24 21:06:34 +00001190 }
1191 if( chldPg==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001192 pCur->iMatch = c;
drh72f82862001-05-24 21:06:34 +00001193 if( pRes ) *pRes = c;
1194 return SQLITE_OK;
1195 }
drh5e2f8b92001-05-28 00:41:15 +00001196 rc = moveToChild(pCur, chldPg);
drh72f82862001-05-24 21:06:34 +00001197 if( rc ) return rc;
1198 }
drhbd03cae2001-06-02 02:40:57 +00001199 /* NOT REACHED */
drh72f82862001-05-24 21:06:34 +00001200}
1201
1202/*
drhbd03cae2001-06-02 02:40:57 +00001203** Advance the cursor to the next entry in the database. If
1204** successful and pRes!=NULL then set *pRes=0. If the cursor
1205** was already pointing to the last entry in the database before
1206** this routine was called, then set *pRes=1 if pRes!=NULL.
drh72f82862001-05-24 21:06:34 +00001207*/
1208int sqliteBtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00001209 int rc;
drh5e2f8b92001-05-28 00:41:15 +00001210 if( pCur->bSkipNext ){
1211 pCur->bSkipNext = 0;
drh72f82862001-05-24 21:06:34 +00001212 if( pRes ) *pRes = 0;
1213 return SQLITE_OK;
1214 }
drh72f82862001-05-24 21:06:34 +00001215 pCur->idx++;
drh5e2f8b92001-05-28 00:41:15 +00001216 if( pCur->idx>=pCur->pPage->nCell ){
drh8c42ca92001-06-22 19:15:00 +00001217 if( pCur->pPage->u.hdr.rightChild ){
1218 rc = moveToChild(pCur, pCur->pPage->u.hdr.rightChild);
drh5e2f8b92001-05-28 00:41:15 +00001219 if( rc ) return rc;
1220 rc = moveToLeftmost(pCur);
1221 if( rc ) return rc;
1222 if( pRes ) *pRes = 0;
drh72f82862001-05-24 21:06:34 +00001223 return SQLITE_OK;
1224 }
drh5e2f8b92001-05-28 00:41:15 +00001225 do{
drh8c42ca92001-06-22 19:15:00 +00001226 if( pCur->pPage->pParent==0 ){
drh5e2f8b92001-05-28 00:41:15 +00001227 if( pRes ) *pRes = 1;
1228 return SQLITE_OK;
1229 }
1230 rc = moveToParent(pCur);
1231 if( rc ) return rc;
1232 }while( pCur->idx>=pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00001233 if( pRes ) *pRes = 0;
1234 return SQLITE_OK;
1235 }
drh5e2f8b92001-05-28 00:41:15 +00001236 rc = moveToLeftmost(pCur);
1237 if( rc ) return rc;
drh72f82862001-05-24 21:06:34 +00001238 if( pRes ) *pRes = 0;
1239 return SQLITE_OK;
1240}
1241
drh3b7511c2001-05-26 13:15:44 +00001242/*
1243** Allocate a new page from the database file.
1244**
1245** The new page is marked as dirty. (In other words, sqlitepager_write()
1246** has already been called on the new page.) The new page has also
1247** been referenced and the calling routine is responsible for calling
1248** sqlitepager_unref() on the new page when it is done.
1249**
1250** SQLITE_OK is returned on success. Any other return value indicates
1251** an error. *ppPage and *pPgno are undefined in the event of an error.
1252** Do not invoke sqlitepager_unref() on *ppPage if an error is returned.
1253*/
1254static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno){
drhbd03cae2001-06-02 02:40:57 +00001255 PageOne *pPage1 = pBt->page1;
drh8c42ca92001-06-22 19:15:00 +00001256 int rc;
drh3b7511c2001-05-26 13:15:44 +00001257 if( pPage1->freeList ){
1258 OverflowPage *pOvfl;
1259 rc = sqlitepager_write(pPage1);
1260 if( rc ) return rc;
1261 *pPgno = pPage1->freeList;
drh8c42ca92001-06-22 19:15:00 +00001262 rc = sqlitepager_get(pBt->pPager, pPage1->freeList, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001263 if( rc ) return rc;
1264 rc = sqlitepager_write(pOvfl);
1265 if( rc ){
1266 sqlitepager_unref(pOvfl);
1267 return rc;
1268 }
drh14acc042001-06-10 19:56:58 +00001269 pPage1->freeList = pOvfl->iNext;
drh3b7511c2001-05-26 13:15:44 +00001270 *ppPage = (MemPage*)pOvfl;
1271 }else{
1272 *pPgno = sqlitepager_pagecount(pBt->pPager);
drh8c42ca92001-06-22 19:15:00 +00001273 rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
drh3b7511c2001-05-26 13:15:44 +00001274 if( rc ) return rc;
1275 rc = sqlitepager_write(*ppPage);
1276 }
1277 return rc;
1278}
1279
1280/*
1281** Add a page of the database file to the freelist. Either pgno or
1282** pPage but not both may be 0.
drh5e2f8b92001-05-28 00:41:15 +00001283**
1284** sqlitepager_unref() is NOT called for pPage. The calling routine
1285** needs to do that.
drh3b7511c2001-05-26 13:15:44 +00001286*/
1287static int freePage(Btree *pBt, void *pPage, Pgno pgno){
drhbd03cae2001-06-02 02:40:57 +00001288 PageOne *pPage1 = pBt->page1;
drh3b7511c2001-05-26 13:15:44 +00001289 OverflowPage *pOvfl = (OverflowPage*)pPage;
1290 int rc;
1291 int needOvflUnref = 0;
drh8b2f49b2001-06-08 00:21:52 +00001292
drh3b7511c2001-05-26 13:15:44 +00001293 if( pgno==0 ){
1294 assert( pOvfl!=0 );
1295 pgno = sqlitepager_pagenumber(pOvfl);
1296 }
1297 rc = sqlitepager_write(pPage1);
1298 if( rc ){
1299 return rc;
1300 }
1301 if( pOvfl==0 ){
1302 assert( pgno>0 );
drh8c42ca92001-06-22 19:15:00 +00001303 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001304 if( rc ) return rc;
1305 needOvflUnref = 1;
1306 }
1307 rc = sqlitepager_write(pOvfl);
1308 if( rc ){
1309 if( needOvflUnref ) sqlitepager_unref(pOvfl);
1310 return rc;
1311 }
drh14acc042001-06-10 19:56:58 +00001312 pOvfl->iNext = pPage1->freeList;
drh3b7511c2001-05-26 13:15:44 +00001313 pPage1->freeList = pgno;
drh5e2f8b92001-05-28 00:41:15 +00001314 memset(pOvfl->aPayload, 0, OVERFLOW_SIZE);
drh8c42ca92001-06-22 19:15:00 +00001315 ((MemPage*)pPage)->isInit = 0;
1316 assert( ((MemPage*)pPage)->pParent==0 );
drh3b7511c2001-05-26 13:15:44 +00001317 rc = sqlitepager_unref(pOvfl);
1318 return rc;
1319}
1320
1321/*
1322** Erase all the data out of a cell. This involves returning overflow
1323** pages back the freelist.
1324*/
1325static int clearCell(Btree *pBt, Cell *pCell){
1326 Pager *pPager = pBt->pPager;
1327 OverflowPage *pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001328 Pgno ovfl, nextOvfl;
1329 int rc;
1330
drh5e2f8b92001-05-28 00:41:15 +00001331 if( pCell->h.nKey + pCell->h.nData <= MX_LOCAL_PAYLOAD ){
1332 return SQLITE_OK;
1333 }
drh3b7511c2001-05-26 13:15:44 +00001334 ovfl = pCell->ovfl;
1335 pCell->ovfl = 0;
1336 while( ovfl ){
drh8c42ca92001-06-22 19:15:00 +00001337 rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl);
drh3b7511c2001-05-26 13:15:44 +00001338 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001339 nextOvfl = pOvfl->iNext;
drhbd03cae2001-06-02 02:40:57 +00001340 rc = freePage(pBt, pOvfl, ovfl);
1341 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00001342 ovfl = nextOvfl;
1343 sqlitepager_unref(pOvfl);
1344 }
drh5e2f8b92001-05-28 00:41:15 +00001345 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00001346}
1347
1348/*
1349** Create a new cell from key and data. Overflow pages are allocated as
1350** necessary and linked to this cell.
1351*/
1352static int fillInCell(
1353 Btree *pBt, /* The whole Btree. Needed to allocate pages */
1354 Cell *pCell, /* Populate this Cell structure */
1355 void *pKey, int nKey, /* The key */
1356 void *pData,int nData /* The data */
1357){
drh8c42ca92001-06-22 19:15:00 +00001358 OverflowPage *pOvfl;
drh3b7511c2001-05-26 13:15:44 +00001359 Pgno *pNext;
1360 int spaceLeft;
drh8c42ca92001-06-22 19:15:00 +00001361 int n, rc;
drh3b7511c2001-05-26 13:15:44 +00001362 int nPayload;
1363 char *pPayload;
1364 char *pSpace;
1365
drh5e2f8b92001-05-28 00:41:15 +00001366 pCell->h.leftChild = 0;
drh3b7511c2001-05-26 13:15:44 +00001367 pCell->h.nKey = nKey;
1368 pCell->h.nData = nData;
1369 pCell->h.iNext = 0;
1370
1371 pNext = &pCell->ovfl;
drh5e2f8b92001-05-28 00:41:15 +00001372 pSpace = pCell->aPayload;
drh3b7511c2001-05-26 13:15:44 +00001373 spaceLeft = MX_LOCAL_PAYLOAD;
1374 pPayload = pKey;
1375 pKey = 0;
1376 nPayload = nKey;
1377 while( nPayload>0 ){
1378 if( spaceLeft==0 ){
drh8c42ca92001-06-22 19:15:00 +00001379 rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext);
drh3b7511c2001-05-26 13:15:44 +00001380 if( rc ){
1381 *pNext = 0;
drh5e2f8b92001-05-28 00:41:15 +00001382 clearCell(pBt, pCell);
drh3b7511c2001-05-26 13:15:44 +00001383 return rc;
1384 }
1385 spaceLeft = OVERFLOW_SIZE;
drh5e2f8b92001-05-28 00:41:15 +00001386 pSpace = pOvfl->aPayload;
drh8c42ca92001-06-22 19:15:00 +00001387 pNext = &pOvfl->iNext;
drh3b7511c2001-05-26 13:15:44 +00001388 }
1389 n = nPayload;
1390 if( n>spaceLeft ) n = spaceLeft;
1391 memcpy(pSpace, pPayload, n);
1392 nPayload -= n;
1393 if( nPayload==0 && pData ){
1394 pPayload = pData;
1395 nPayload = nData;
1396 pData = 0;
1397 }else{
1398 pPayload += n;
1399 }
1400 spaceLeft -= n;
1401 pSpace += n;
1402 }
1403 return SQLITE_OK;
1404}
1405
1406/*
drhbd03cae2001-06-02 02:40:57 +00001407** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00001408** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00001409** pointer in the third argument.
1410*/
1411static void reparentPage(Pager *pPager, Pgno pgno, MemPage *pNewParent){
1412 MemPage *pThis;
1413
1414 assert( pPager!=0 && pgno!=0 );
1415 pThis = sqlitepager_lookup(pPager, pgno);
1416 if( pThis && pThis->pParent!=pNewParent ){
1417 if( pThis->pParent ) sqlitepager_unref(pThis->pParent);
1418 pThis->pParent = pNewParent;
1419 if( pNewParent ) sqlitepager_ref(pNewParent);
1420 }
1421}
1422
1423/*
1424** Reparent all children of the given page to be the given page.
1425** In other words, for every child of pPage, invoke reparentPage()
1426** to make sure that child knows that pPage is its parent.
1427**
1428** This routine gets called after you memcpy() one page into
1429** another.
1430*/
drh8c42ca92001-06-22 19:15:00 +00001431static void reparentChildPages(Pager *pPager, MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00001432 int i;
1433 for(i=0; i<pPage->nCell; i++){
drh8c42ca92001-06-22 19:15:00 +00001434 reparentPage(pPager, pPage->apCell[i]->h.leftChild, pPage);
drhbd03cae2001-06-02 02:40:57 +00001435 }
drh14acc042001-06-10 19:56:58 +00001436 reparentPage(pPager, pPage->u.hdr.rightChild, pPage);
1437}
1438
1439/*
1440** Remove the i-th cell from pPage. This routine effects pPage only.
1441** The cell content is not freed or deallocated. It is assumed that
1442** the cell content has been copied someplace else. This routine just
1443** removes the reference to the cell from pPage.
1444**
1445** "sz" must be the number of bytes in the cell.
1446**
1447** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001448** Only the pPage->apCell[] array is important. The relinkCellList()
1449** routine will be called soon after this routine in order to rebuild
1450** the linked list.
drh14acc042001-06-10 19:56:58 +00001451*/
drh8c42ca92001-06-22 19:15:00 +00001452static void dropCell(MemPage *pPage, int idx, int sz){
drh14acc042001-06-10 19:56:58 +00001453 int j;
drh8c42ca92001-06-22 19:15:00 +00001454 assert( idx>=0 && idx<pPage->nCell );
1455 assert( sz==cellSize(pPage->apCell[idx]) );
drh7c717f72001-06-24 20:39:41 +00001456 freeSpace(pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz);
1457 for(j=idx; j<pPage->nCell-1; j++){
drh14acc042001-06-10 19:56:58 +00001458 pPage->apCell[j] = pPage->apCell[j+1];
1459 }
1460 pPage->nCell--;
1461}
1462
1463/*
1464** Insert a new cell on pPage at cell index "i". pCell points to the
1465** content of the cell.
1466**
1467** If the cell content will fit on the page, then put it there. If it
1468** will not fit, then just make pPage->apCell[i] point to the content
1469** and set pPage->isOverfull.
1470**
1471** Do not bother maintaining the integrity of the linked list of Cells.
drh8c42ca92001-06-22 19:15:00 +00001472** Only the pPage->apCell[] array is important. The relinkCellList()
1473** routine will be called soon after this routine in order to rebuild
1474** the linked list.
drh14acc042001-06-10 19:56:58 +00001475*/
1476static void insertCell(MemPage *pPage, int i, Cell *pCell, int sz){
1477 int idx, j;
1478 assert( i>=0 && i<=pPage->nCell );
1479 assert( sz==cellSize(pCell) );
1480 for(j=pPage->nCell; j>i; j--){
1481 pPage->apCell[j] = pPage->apCell[j-1];
1482 }
1483 pPage->nCell++;
1484 idx = allocateSpace(pPage, sz);
1485 if( idx<=0 ){
1486 pPage->isOverfull = 1;
1487 pPage->apCell[i] = pCell;
1488 }else{
1489 memcpy(&pPage->u.aDisk[idx], pCell, sz);
drh8c42ca92001-06-22 19:15:00 +00001490 pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx];
drh14acc042001-06-10 19:56:58 +00001491 }
1492}
1493
1494/*
1495** Rebuild the linked list of cells on a page so that the cells
drh8c42ca92001-06-22 19:15:00 +00001496** occur in the order specified by the pPage->apCell[] array.
1497** Invoke this routine once to repair damage after one or more
1498** invocations of either insertCell() or dropCell().
drh14acc042001-06-10 19:56:58 +00001499*/
1500static void relinkCellList(MemPage *pPage){
1501 int i;
1502 u16 *pIdx;
1503 pIdx = &pPage->u.hdr.firstCell;
1504 for(i=0; i<pPage->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001505 int idx = Addr(pPage->apCell[i]) - Addr(pPage);
drh8c42ca92001-06-22 19:15:00 +00001506 assert( idx>0 && idx<SQLITE_PAGE_SIZE );
drh14acc042001-06-10 19:56:58 +00001507 *pIdx = idx;
1508 pIdx = &pPage->apCell[i]->h.iNext;
1509 }
1510 *pIdx = 0;
1511}
1512
1513/*
1514** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[]
1515** pointers that point intto pFrom->u.aDisk[] must be adjusted to point
1516** intto pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might
1517** not point to pFrom->u.aDisk[]. Those are unchanged.
1518*/
1519static void copyPage(MemPage *pTo, MemPage *pFrom){
1520 uptr from, to;
1521 int i;
1522 memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_PAGE_SIZE);
1523 pTo->pParent = pFrom->pParent;
1524 pTo->isInit = 1;
1525 pTo->nCell = pFrom->nCell;
1526 pTo->nFree = pFrom->nFree;
1527 pTo->isOverfull = pFrom->isOverfull;
drh7c717f72001-06-24 20:39:41 +00001528 to = Addr(pTo);
1529 from = Addr(pFrom);
drh14acc042001-06-10 19:56:58 +00001530 for(i=0; i<pTo->nCell; i++){
drh7c717f72001-06-24 20:39:41 +00001531 uptr x = Addr(pFrom->apCell[i]);
drh8c42ca92001-06-22 19:15:00 +00001532 if( x>from && x<from+SQLITE_PAGE_SIZE ){
1533 *((uptr*)&pTo->apCell[i]) = x + to - from;
drh14acc042001-06-10 19:56:58 +00001534 }
1535 }
drhbd03cae2001-06-02 02:40:57 +00001536}
1537
1538/*
drh8b2f49b2001-06-08 00:21:52 +00001539** This routine redistributes Cells on pPage and up to two siblings
1540** of pPage so that all pages have about the same amount of free space.
drh14acc042001-06-10 19:56:58 +00001541** Usually one sibling on either side of pPage is used in the balancing,
drh8b2f49b2001-06-08 00:21:52 +00001542** though both siblings might come from one side if pPage is the first
1543** or last child of its parent. If pPage has fewer than two siblings
1544** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00001545** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00001546**
1547** The number of siblings of pPage might be increased or decreased by
drh8c42ca92001-06-22 19:15:00 +00001548** one in an effort to keep pages between 66% and 100% full. The root page
1549** is special and is allowed to be less than 66% full. If pPage is
1550** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00001551** or decreased by one, as necessary, to keep the root page from being
1552** overfull or empty.
1553**
drh14acc042001-06-10 19:56:58 +00001554** This routine calls relinkCellList() on its input page regardless of
1555** whether or not it does any real balancing. Client routines will typically
1556** invoke insertCell() or dropCell() before calling this routine, so we
1557** need to call relinkCellList() to clean up the mess that those other
1558** routines left behind.
1559**
1560** pCur is left pointing to the same cell as when this routine was called
drh8c42ca92001-06-22 19:15:00 +00001561** even if that cell gets moved to a different page. pCur may be NULL.
1562** Set the pCur parameter to NULL if you do not care about keeping track
1563** of a cell as that will save this routine the work of keeping track of it.
drh14acc042001-06-10 19:56:58 +00001564**
drh8b2f49b2001-06-08 00:21:52 +00001565** Note that when this routine is called, some of the Cells on pPage
drh14acc042001-06-10 19:56:58 +00001566** might not actually be stored in pPage->u.aDisk[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00001567** if the page is overfull. Part of the job of this routine is to
drh14acc042001-06-10 19:56:58 +00001568** make sure all Cells for pPage once again fit in pPage->u.aDisk[].
1569**
drh8c42ca92001-06-22 19:15:00 +00001570** In the course of balancing the siblings of pPage, the parent of pPage
1571** might become overfull or underfull. If that happens, then this routine
1572** is called recursively on the parent.
1573**
drh14acc042001-06-10 19:56:58 +00001574** If this routine fails for any reason, it means the database may have
1575** been left in a corrupted state and should be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00001576*/
drh14acc042001-06-10 19:56:58 +00001577static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
drh8b2f49b2001-06-08 00:21:52 +00001578 MemPage *pParent; /* The parent of pPage */
drh14acc042001-06-10 19:56:58 +00001579 MemPage *apOld[3]; /* pPage and up to two siblings */
drh8b2f49b2001-06-08 00:21:52 +00001580 Pgno pgnoOld[3]; /* Page numbers for each page in apOld[] */
drh14acc042001-06-10 19:56:58 +00001581 MemPage *apNew[4]; /* pPage and up to 3 siblings after balancing */
1582 Pgno pgnoNew[4]; /* Page numbers for each page in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00001583 int idxDiv[3]; /* Indices of divider cells in pParent */
1584 Cell *apDiv[3]; /* Divider cells in pParent */
1585 int nCell; /* Number of cells in apCell[] */
1586 int nOld; /* Number of pages in apOld[] */
1587 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00001588 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00001589 int i, j, k; /* Loop counters */
1590 int idx; /* Index of pPage in pParent->apCell[] */
1591 int nxDiv; /* Next divider slot in pParent->apCell[] */
1592 int rc; /* The return code */
1593 int iCur; /* apCell[iCur] is the cell of the cursor */
1594 int usedPerPage; /* Memory needed for each page */
1595 int freePerPage; /* Average free space per page */
drh8c42ca92001-06-22 19:15:00 +00001596 int totalSize; /* Total bytes for all cells */
1597 Pgno pgno; /* Page number */
drh14acc042001-06-10 19:56:58 +00001598 Cell *apCell[MX_CELL*3+5]; /* All cells from pages being balanceed */
1599 int szCell[MX_CELL*3+5]; /* Local size of all cells */
1600 Cell aTemp[2]; /* Temporary holding area for apDiv[] */
1601 MemPage aOld[3]; /* Temporary copies of pPage and its siblings */
drh8b2f49b2001-06-08 00:21:52 +00001602
drh14acc042001-06-10 19:56:58 +00001603 /*
1604 ** Return without doing any work if pPage is neither overfull nor
1605 ** underfull.
drh8b2f49b2001-06-08 00:21:52 +00001606 */
drh14acc042001-06-10 19:56:58 +00001607 if( !pPage->isOverfull && pPage->nFree<SQLITE_PAGE_SIZE/2 ){
1608 relinkCellList(pPage);
drh8b2f49b2001-06-08 00:21:52 +00001609 return SQLITE_OK;
1610 }
1611
1612 /*
drh14acc042001-06-10 19:56:58 +00001613 ** Find the parent of the page to be balanceed.
1614 ** If there is no parent, it means this page is the root page and
drh8b2f49b2001-06-08 00:21:52 +00001615 ** special rules apply.
1616 */
drh14acc042001-06-10 19:56:58 +00001617 pParent = pPage->pParent;
drh8b2f49b2001-06-08 00:21:52 +00001618 if( pParent==0 ){
1619 Pgno pgnoChild;
drh8c42ca92001-06-22 19:15:00 +00001620 MemPage *pChild;
drh8b2f49b2001-06-08 00:21:52 +00001621 if( pPage->nCell==0 ){
drh14acc042001-06-10 19:56:58 +00001622 if( pPage->u.hdr.rightChild ){
1623 /*
1624 ** The root page is empty. Copy the one child page
drh8b2f49b2001-06-08 00:21:52 +00001625 ** into the root page and return. This reduces the depth
1626 ** of the BTree by one.
1627 */
drh14acc042001-06-10 19:56:58 +00001628 rc = sqlitepager_write(pPage);
1629 if( rc ) return rc;
1630 pgnoChild = pPage->u.hdr.rightChild;
drh8c42ca92001-06-22 19:15:00 +00001631 rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild);
drh8b2f49b2001-06-08 00:21:52 +00001632 if( rc ) return rc;
1633 memcpy(pPage, pChild, SQLITE_PAGE_SIZE);
1634 pPage->isInit = 0;
1635 initPage(pPage, sqlitepager_pagenumber(pPage), 0);
1636 reparentChildPages(pBt->pPager, pPage);
1637 freePage(pBt, pChild, pgnoChild);
1638 sqlitepager_unref(pChild);
1639 }
1640 return SQLITE_OK;
1641 }
drh14acc042001-06-10 19:56:58 +00001642 if( !pPage->isOverfull ){
drh8b2f49b2001-06-08 00:21:52 +00001643 /* It is OK for the root page to be less than half full.
1644 */
drh14acc042001-06-10 19:56:58 +00001645 relinkCellList(pPage);
drh8b2f49b2001-06-08 00:21:52 +00001646 return SQLITE_OK;
1647 }
drh14acc042001-06-10 19:56:58 +00001648 /*
1649 ** If we get to here, it means the root page is overfull.
drh8b2f49b2001-06-08 00:21:52 +00001650 ** When this happens, Create a new child page and copy the
1651 ** contents of the root into the child. Then make the root
drh14acc042001-06-10 19:56:58 +00001652 ** page an empty page with rightChild pointing to the new
drh8b2f49b2001-06-08 00:21:52 +00001653 ** child. Then fall thru to the code below which will cause
1654 ** the overfull child page to be split.
1655 */
drh14acc042001-06-10 19:56:58 +00001656 rc = sqlitepager_write(pPage);
1657 if( rc ) return rc;
drh8b2f49b2001-06-08 00:21:52 +00001658 rc = allocatePage(pBt, &pChild, &pgnoChild);
1659 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001660 copyPage(pChild, pPage);
1661 pChild->pParent = pPage;
1662 pChild->isOverfull = 1;
1663 if( pCur ){
1664 sqlitepager_ref(pChild);
1665 sqlitepager_unref(pCur->pPage);
1666 pCur->pPage = pChild;
drh8b2f49b2001-06-08 00:21:52 +00001667 }
drh8b2f49b2001-06-08 00:21:52 +00001668 zeroPage(pPage);
drh14acc042001-06-10 19:56:58 +00001669 pPage->u.hdr.rightChild = pgnoChild;
drh8b2f49b2001-06-08 00:21:52 +00001670 pParent = pPage;
1671 pPage = pChild;
drh14acc042001-06-10 19:56:58 +00001672 }else{
1673 rc = sqlitepager_write(pPage);
1674 if( rc ) return rc;
drh8b2f49b2001-06-08 00:21:52 +00001675 }
drh14acc042001-06-10 19:56:58 +00001676
drh8b2f49b2001-06-08 00:21:52 +00001677 /*
drh14acc042001-06-10 19:56:58 +00001678 ** Find the Cell in the parent page whose h.leftChild points back
1679 ** to pPage. The "idx" variable is the index of that cell. If pPage
1680 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00001681 */
1682 idx = -1;
1683 pgno = sqlitepager_pagenumber(pPage);
1684 for(i=0; i<pParent->nCell; i++){
1685 if( pParent->apCell[i]->h.leftChild==pgno ){
1686 idx = i;
1687 break;
1688 }
1689 }
drh14acc042001-06-10 19:56:58 +00001690 if( idx<0 && pPage->u.hdr.rightChild==pgno ){
drh8b2f49b2001-06-08 00:21:52 +00001691 idx = pPage->nCell;
1692 }
1693 if( idx<0 ){
drh14acc042001-06-10 19:56:58 +00001694 return SQLITE_CORRUPT;
drh8b2f49b2001-06-08 00:21:52 +00001695 }
1696
1697 /*
drh14acc042001-06-10 19:56:58 +00001698 ** Initialize variables so that it will be safe to jump
1699 ** directory to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00001700 */
drh14acc042001-06-10 19:56:58 +00001701 nOld = nNew = 0;
1702 sqlitepager_ref(pParent);
1703
1704 /*
1705 ** Find sibling pages to pPage and the Cells in pParent that divide
1706 ** the siblings. An attempt is made to find one sibling on either
1707 ** side of pPage. Both siblings are taken from one side, however, if
1708 ** pPage is either the first or last child of its parent. If pParent
1709 ** has 3 or fewer children then all children of pParent are taken.
1710 */
1711 if( idx==pParent->nCell ){
1712 nxDiv = idx - 2;
drh8b2f49b2001-06-08 00:21:52 +00001713 }else{
drh14acc042001-06-10 19:56:58 +00001714 nxDiv = idx - 1;
drh8b2f49b2001-06-08 00:21:52 +00001715 }
drh14acc042001-06-10 19:56:58 +00001716 if( nxDiv<0 ) nxDiv = 0;
drh8b2f49b2001-06-08 00:21:52 +00001717 nDiv = 0;
drh14acc042001-06-10 19:56:58 +00001718 for(i=0, k=nxDiv; i<3; i++, k++){
1719 if( k<pParent->nCell ){
1720 idxDiv[i] = k;
1721 apDiv[i] = pParent->apCell[k];
drh8b2f49b2001-06-08 00:21:52 +00001722 nDiv++;
1723 pgnoOld[i] = apDiv[i]->h.leftChild;
drh14acc042001-06-10 19:56:58 +00001724 }else if( k==pParent->nCell ){
drh8c42ca92001-06-22 19:15:00 +00001725 pgnoOld[i] = pParent->u.hdr.rightChild;
drh14acc042001-06-10 19:56:58 +00001726 }else{
1727 break;
drh8b2f49b2001-06-08 00:21:52 +00001728 }
drh8c42ca92001-06-22 19:15:00 +00001729 rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]);
drh14acc042001-06-10 19:56:58 +00001730 if( rc ) goto balance_cleanup;
1731 nOld++;
drh8b2f49b2001-06-08 00:21:52 +00001732 }
1733
1734 /*
drh14acc042001-06-10 19:56:58 +00001735 ** Set iCur to be the index in apCell[] of the cell that the cursor
1736 ** is pointing to. We will need this later on in order to keep the
1737 ** cursor pointing at the same cell.
1738 */
1739 if( pCur ){
1740 iCur = pCur->idx;
1741 for(i=0; idxDiv[i]<idx; i++){
1742 iCur += apOld[i]->nCell + 1;
1743 }
1744 sqlitepager_unref(pCur->pPage);
1745 pCur->pPage = 0;
1746 }
1747
1748 /*
1749 ** Make copies of the content of pPage and its siblings into aOld[].
1750 ** The rest of this function will use data from the copies rather
1751 ** that the original pages since the original pages will be in the
1752 ** process of being overwritten.
1753 */
1754 for(i=0; i<nOld; i++){
1755 copyPage(&aOld[i], apOld[i]);
1756 rc = freePage(pBt, apOld[i], pgnoOld[i]);
1757 if( rc ) goto balance_cleanup;
1758 apOld[i] = &aOld[i];
1759 }
1760
1761 /*
1762 ** Load pointers to all cells on sibling pages and the divider cells
1763 ** into the local apCell[] array. Make copies of the divider cells
1764 ** into aTemp[] and remove the the divider Cells from pParent.
drh8b2f49b2001-06-08 00:21:52 +00001765 */
1766 nCell = 0;
1767 for(i=0; i<nOld; i++){
1768 MemPage *pOld = apOld[i];
1769 for(j=0; j<pOld->nCell; j++){
drh14acc042001-06-10 19:56:58 +00001770 apCell[nCell] = pOld->apCell[j];
1771 szCell[nCell] = cellSize(apCell[nCell]);
1772 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00001773 }
1774 if( i<nOld-1 ){
drh14acc042001-06-10 19:56:58 +00001775 szCell[nCell] = cellSize(apDiv[i]);
drh8c42ca92001-06-22 19:15:00 +00001776 memcpy(&aTemp[i], apDiv[i], szCell[nCell]);
drh14acc042001-06-10 19:56:58 +00001777 apCell[nCell] = &aTemp[i];
1778 dropCell(pParent, nxDiv, szCell[nCell]);
1779 assert( apCell[nCell]->h.leftChild==pgnoOld[i] );
1780 apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild;
1781 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00001782 }
1783 }
1784
1785 /*
drh14acc042001-06-10 19:56:58 +00001786 ** Estimate the number of pages needed. Record this number in "k"
1787 ** for now. It will get transferred to nNew as we allocate the
1788 ** new pages.
drh8b2f49b2001-06-08 00:21:52 +00001789 */
1790 totalSize = 0;
1791 for(i=0; i<nCell; i++){
drh14acc042001-06-10 19:56:58 +00001792 totalSize += szCell[i];
drh8b2f49b2001-06-08 00:21:52 +00001793 }
drh14acc042001-06-10 19:56:58 +00001794 k = (totalSize + (SQLITE_PAGE_SIZE - sizeof(PageHdr) - 1)) /
drh8b2f49b2001-06-08 00:21:52 +00001795 (SQLITE_PAGE_SIZE - sizeof(PageHdr));
drh14acc042001-06-10 19:56:58 +00001796 usedPerPage = (totalSize+k-1)/k;
1797 freePerPage = SQLITE_PAGE_SIZE - usedPerPage;
drh8b2f49b2001-06-08 00:21:52 +00001798
1799
1800 /*
1801 ** Allocate new pages
1802 */
drh14acc042001-06-10 19:56:58 +00001803 for(i=0; i<k; i++){
drh8b2f49b2001-06-08 00:21:52 +00001804 rc = allocatePage(pBt, &apNew[i], &pgnoNew[i]);
drh14acc042001-06-10 19:56:58 +00001805 if( rc ) goto balance_cleanup;
1806 nNew++;
drh8b2f49b2001-06-08 00:21:52 +00001807 zeroPage(apNew[i]);
1808 }
1809
1810 /*
drh14acc042001-06-10 19:56:58 +00001811 ** Evenly distribute the data in apCell[] across the new pages.
1812 ** Insert divider cells into pParent as necessary.
1813 */
1814 j = 0;
1815 for(i=0; i<nNew; i++){
1816 MemPage *pNew = apNew[i];
1817 while( j<nCell && pNew->nFree<freePerPage && szCell[j]<=pNew->nFree ){
1818 if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; }
1819 insertCell(pNew, pNew->nCell, apCell[j], szCell[j]);
1820 j++;
1821 }
1822 assert( !pNew->isOverfull );
1823 relinkCellList(pNew);
1824 if( i<nNew-1 && j<nCell ){
1825 pNew->u.hdr.rightChild = apCell[j]->h.leftChild;
1826 apCell[j]->h.leftChild = pgnoNew[i];
1827 if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; }
1828 insertCell(pParent, nxDiv, apCell[j], szCell[j]);
1829 j++;
1830 nxDiv++;
1831 }
1832 }
1833 apNew[nNew-1]->u.hdr.rightChild = apOld[nOld-1]->u.hdr.rightChild;
1834 if( nxDiv==pParent->nCell ){
1835 pParent->u.hdr.rightChild = pgnoNew[nNew-1];
1836 }else{
1837 pParent->apCell[nxDiv]->h.leftChild = pgnoNew[nNew-1];
1838 }
1839 if( pCur ){
1840 assert( pCur->pPage!=0 );
1841 sqlitepager_ref(pCur->pPage);
1842 }
1843
1844 /*
1845 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00001846 */
1847 for(i=0; i<nNew; i++){
drh14acc042001-06-10 19:56:58 +00001848 reparentChildPages(pBt->pPager, apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00001849 }
drh14acc042001-06-10 19:56:58 +00001850 reparentChildPages(pBt->pPager, pParent);
drh8b2f49b2001-06-08 00:21:52 +00001851
1852 /*
drh14acc042001-06-10 19:56:58 +00001853 ** balance the parent page.
drh8b2f49b2001-06-08 00:21:52 +00001854 */
drh14acc042001-06-10 19:56:58 +00001855 rc = balance(pBt, pParent, 0);
drh8b2f49b2001-06-08 00:21:52 +00001856
1857 /*
drh14acc042001-06-10 19:56:58 +00001858 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00001859 */
drh14acc042001-06-10 19:56:58 +00001860balance_cleanup:
drh8b2f49b2001-06-08 00:21:52 +00001861 for(i=0; i<nOld; i++){
drh14acc042001-06-10 19:56:58 +00001862 sqlitepager_unref(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00001863 }
drh14acc042001-06-10 19:56:58 +00001864 for(i=0; i<nNew; i++){
1865 sqlitepager_unref(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00001866 }
drh14acc042001-06-10 19:56:58 +00001867 if( pCur && pCur->pPage==0 ){
1868 pCur->pPage = pParent;
1869 pCur->idx = 0;
1870 }else{
1871 sqlitepager_unref(pParent);
drh8b2f49b2001-06-08 00:21:52 +00001872 }
1873 return rc;
1874}
1875
1876/*
drh3b7511c2001-05-26 13:15:44 +00001877** Insert a new record into the BTree. The key is given by (pKey,nKey)
1878** and the data is given by (pData,nData). The cursor is used only to
1879** define what database the record should be inserted into. The cursor
drh14acc042001-06-10 19:56:58 +00001880** is left pointing at the new record.
drh3b7511c2001-05-26 13:15:44 +00001881*/
1882int sqliteBtreeInsert(
1883 BtCursor *pCur, /* Insert data into the table of this cursor */
1884 void *pKey, int nKey, /* The key of the new record */
1885 void *pData, int nData /* The data of the new record */
1886){
1887 Cell newCell;
1888 int rc;
1889 int loc;
drh14acc042001-06-10 19:56:58 +00001890 int szNew;
drh3b7511c2001-05-26 13:15:44 +00001891 MemPage *pPage;
1892 Btree *pBt = pCur->pBt;
1893
drh8b2f49b2001-06-08 00:21:52 +00001894 if( !pCur->pBt->inTrans ){
1895 return SQLITE_ERROR; /* Must start a transaction first */
1896 }
drh14acc042001-06-10 19:56:58 +00001897 rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc);
drh3b7511c2001-05-26 13:15:44 +00001898 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001899 pPage = pCur->pPage;
1900 rc = sqlitepager_write(pPage);
drhbd03cae2001-06-02 02:40:57 +00001901 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00001902 rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData);
1903 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001904 szNew = cellSize(&newCell);
drh3b7511c2001-05-26 13:15:44 +00001905 if( loc==0 ){
drh14acc042001-06-10 19:56:58 +00001906 newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild;
1907 rc = clearCell(pBt, pPage->apCell[pCur->idx]);
drh5e2f8b92001-05-28 00:41:15 +00001908 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00001909 dropCell(pPage, pCur->idx, cellSize(pPage->apCell[pCur->idx]));
drh7c717f72001-06-24 20:39:41 +00001910 }else if( loc<0 && pPage->nCell>0 ){
drh14acc042001-06-10 19:56:58 +00001911 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
1912 pCur->idx++;
1913 }else{
1914 assert( pPage->u.hdr.rightChild==0 ); /* Must be a leaf page */
drh3b7511c2001-05-26 13:15:44 +00001915 }
drh7c717f72001-06-24 20:39:41 +00001916 insertCell(pPage, pCur->idx, &newCell, szNew);
drh14acc042001-06-10 19:56:58 +00001917 rc = balance(pCur->pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00001918 return rc;
1919}
1920
1921/*
drhbd03cae2001-06-02 02:40:57 +00001922** Delete the entry that the cursor is pointing to.
drh5e2f8b92001-05-28 00:41:15 +00001923**
drhbd03cae2001-06-02 02:40:57 +00001924** The cursor is left pointing at either the next or the previous
1925** entry. If the cursor is left pointing to the next entry, then
1926** the pCur->bSkipNext flag is set which forces the next call to
1927** sqliteBtreeNext() to be a no-op. That way, you can always call
1928** sqliteBtreeNext() after a delete and the cursor will be left
1929** pointing to the first entry after the deleted entry.
drh3b7511c2001-05-26 13:15:44 +00001930*/
1931int sqliteBtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00001932 MemPage *pPage = pCur->pPage;
1933 Cell *pCell;
1934 int rc;
drh8c42ca92001-06-22 19:15:00 +00001935 Pgno pgnoChild;
drh8b2f49b2001-06-08 00:21:52 +00001936
1937 if( !pCur->pBt->inTrans ){
1938 return SQLITE_ERROR; /* Must start a transaction first */
1939 }
drhbd03cae2001-06-02 02:40:57 +00001940 if( pCur->idx >= pPage->nCell ){
1941 return SQLITE_ERROR; /* The cursor is not pointing to anything */
1942 }
1943 rc = sqlitepager_write(pPage);
1944 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00001945 pCell = pPage->apCell[pCur->idx];
drh14acc042001-06-10 19:56:58 +00001946 pgnoChild = pCell->h.leftChild;
drh8c42ca92001-06-22 19:15:00 +00001947 clearCell(pCur->pBt, pCell);
drh14acc042001-06-10 19:56:58 +00001948 dropCell(pPage, pCur->idx, cellSize(pCell));
1949 if( pgnoChild ){
1950 /*
1951 ** If the entry we just deleted is not a leaf, then we've left a
drh8c42ca92001-06-22 19:15:00 +00001952 ** hole in an internal page. We have to fill the hole by moving
1953 ** in a cell from a leaf. The next Cell after the one just deleted
drh14acc042001-06-10 19:56:58 +00001954 ** is guaranteed to exist and to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00001955 */
drh14acc042001-06-10 19:56:58 +00001956 BtCursor leafCur;
1957 Cell *pNext;
1958 int szNext;
1959 getTempCursor(pCur, &leafCur);
1960 rc = sqliteBtreeNext(&leafCur, 0);
1961 if( rc!=SQLITE_OK ){
1962 return SQLITE_CORRUPT;
drh5e2f8b92001-05-28 00:41:15 +00001963 }
drh8c42ca92001-06-22 19:15:00 +00001964 pNext = leafCur.pPage->apCell[leafCur.idx];
drh14acc042001-06-10 19:56:58 +00001965 szNext = cellSize(pNext);
drh8c42ca92001-06-22 19:15:00 +00001966 pNext->h.leftChild = pgnoChild;
drh14acc042001-06-10 19:56:58 +00001967 insertCell(pPage, pCur->idx, pNext, szNext);
1968 rc = balance(pCur->pBt, pPage, pCur);
drh5e2f8b92001-05-28 00:41:15 +00001969 if( rc ) return rc;
drh5e2f8b92001-05-28 00:41:15 +00001970 pCur->bSkipNext = 1;
drh14acc042001-06-10 19:56:58 +00001971 dropCell(leafCur.pPage, leafCur.idx, szNext);
1972 rc = balance(pCur->pBt, leafCur.pPage, 0);
drh8c42ca92001-06-22 19:15:00 +00001973 releaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00001974 }else{
drh14acc042001-06-10 19:56:58 +00001975 rc = balance(pCur->pBt, pPage, pCur);
1976 pCur->bSkipNext = 1;
drh5e2f8b92001-05-28 00:41:15 +00001977 }
drh5e2f8b92001-05-28 00:41:15 +00001978 return rc;
drh3b7511c2001-05-26 13:15:44 +00001979}
drh8b2f49b2001-06-08 00:21:52 +00001980
1981/*
1982** Create a new BTree in the same file. Write into *piTable the index
1983** of the root page of the new table.
1984*/
1985int sqliteBtreeCreateTable(Btree *pBt, int *piTable){
1986 MemPage *pRoot;
1987 Pgno pgnoRoot;
1988 int rc;
1989 if( !pBt->inTrans ){
1990 return SQLITE_ERROR; /* Must start a transaction first */
1991 }
1992 rc = allocatePage(pBt, &pRoot, &pgnoRoot);
1993 if( rc ) return rc;
1994 sqlitepager_write(pRoot);
1995 zeroPage(pRoot);
1996 sqlitepager_unref(pRoot);
1997 *piTable = (int)pgnoRoot;
1998 return SQLITE_OK;
1999}
2000
2001/*
2002** Erase the given database page and all its children. Return
2003** the page to the freelist.
2004*/
2005static int clearDatabasePage(Btree *pBt, Pgno pgno){
2006 MemPage *pPage;
2007 int rc;
drh8b2f49b2001-06-08 00:21:52 +00002008 Cell *pCell;
2009 int idx;
2010
drh8c42ca92001-06-22 19:15:00 +00002011 rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage);
drh8b2f49b2001-06-08 00:21:52 +00002012 if( rc ) return rc;
drh14acc042001-06-10 19:56:58 +00002013 idx = pPage->u.hdr.firstCell;
drh8b2f49b2001-06-08 00:21:52 +00002014 while( idx>0 ){
drh14acc042001-06-10 19:56:58 +00002015 pCell = (Cell*)&pPage->u.aDisk[idx];
drh8b2f49b2001-06-08 00:21:52 +00002016 idx = pCell->h.iNext;
2017 if( pCell->h.leftChild ){
2018 rc = clearDatabasePage(pBt, pCell->h.leftChild);
2019 if( rc ) return rc;
2020 }
drh8c42ca92001-06-22 19:15:00 +00002021 rc = clearCell(pBt, pCell);
drh8b2f49b2001-06-08 00:21:52 +00002022 if( rc ) return rc;
2023 }
drh8c42ca92001-06-22 19:15:00 +00002024 rc = clearDatabasePage(pBt, pPage->u.hdr.rightChild);
2025 if( rc ) return rc;
drh8b2f49b2001-06-08 00:21:52 +00002026 return freePage(pBt, pPage, pgno);
2027}
2028
2029/*
2030** Delete all information from a single table in the database.
2031*/
2032int sqliteBtreeClearTable(Btree *pBt, int iTable){
2033 int rc;
2034 if( !pBt->inTrans ){
2035 return SQLITE_ERROR; /* Must start a transaction first */
2036 }
2037 rc = clearDatabasePage(pBt, (Pgno)iTable);
2038 if( rc ){
2039 sqliteBtreeRollback(pBt);
drh8b2f49b2001-06-08 00:21:52 +00002040 }
drh8c42ca92001-06-22 19:15:00 +00002041 return rc;
drh8b2f49b2001-06-08 00:21:52 +00002042}
2043
2044/*
2045** Erase all information in a table and add the root of the table to
2046** the freelist. Except, the root of the principle table (the one on
2047** page 2) is never added to the freelist.
2048*/
2049int sqliteBtreeDropTable(Btree *pBt, int iTable){
2050 int rc;
2051 MemPage *pPage;
2052 if( !pBt->inTrans ){
2053 return SQLITE_ERROR; /* Must start a transaction first */
2054 }
drh8c42ca92001-06-22 19:15:00 +00002055 rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
drh8b2f49b2001-06-08 00:21:52 +00002056 if( rc==SQLITE_OK ){
2057 rc = sqliteBtreeClearTable(pBt, iTable);
2058 }
2059 if( rc==SQLITE_OK && iTable!=2 ){
2060 rc = freePage(pBt, pPage, (Pgno)iTable);
2061 }
2062 sqlitepager_unref(pPage);
2063 return rc;
2064}
2065
2066/*
2067** Read the meta-information out of a database file.
2068*/
2069int sqliteBtreeGetMeta(Btree *pBt, int *aMeta){
2070 PageOne *pP1;
2071 int rc;
2072
drh8c42ca92001-06-22 19:15:00 +00002073 rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1);
drh8b2f49b2001-06-08 00:21:52 +00002074 if( rc ) return rc;
2075 memcpy(aMeta, pP1->aMeta, sizeof(pP1->aMeta));
2076 sqlitepager_unref(pP1);
2077 return SQLITE_OK;
2078}
2079
2080/*
2081** Write meta-information back into the database.
2082*/
2083int sqliteBtreeUpdateMeta(Btree *pBt, int *aMeta){
2084 PageOne *pP1;
2085 int rc;
2086 if( !pBt->inTrans ){
2087 return SQLITE_ERROR; /* Must start a transaction first */
2088 }
2089 pP1 = pBt->page1;
2090 rc = sqlitepager_write(pP1);
2091 if( rc ) return rc;
2092 memcpy(pP1->aMeta, aMeta, sizeof(pP1->aMeta));
2093 return SQLITE_OK;
2094}
drh8c42ca92001-06-22 19:15:00 +00002095
2096#ifdef SQLITE_TEST
2097/*
2098** Print a disassembly of the given page on standard output. This routine
2099** is used for debugging and testing only.
2100*/
2101int sqliteBtreePageDump(Btree *pBt, int pgno){
2102 int rc;
2103 MemPage *pPage;
2104 int i, j;
2105 int nFree;
2106 u16 idx;
2107 char range[20];
2108 unsigned char payload[20];
2109 rc = sqlitepager_get(pBt->pPager, (Pgno)pgno, (void**)&pPage);
2110 if( rc ){
2111 return rc;
2112 }
2113 i = 0;
2114 idx = pPage->u.hdr.firstCell;
2115 while( idx>0 && idx<=SQLITE_PAGE_SIZE-MIN_CELL_SIZE ){
2116 Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
2117 int sz = cellSize(pCell);
2118 sprintf(range,"%d..%d", idx, idx+sz-1);
2119 if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
2120 memcpy(payload, pCell->aPayload, sz);
2121 for(j=0; j<sz; j++){
2122 if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
2123 }
2124 payload[sz] = 0;
2125 printf(
2126 "cell %2d: i=%-10s chld=%-4d nk=%-3d nd=%-3d payload=%s\n",
2127 i, range, (int)pCell->h.leftChild, pCell->h.nKey, pCell->h.nData,
2128 pCell->aPayload
2129 );
drh7c717f72001-06-24 20:39:41 +00002130 i++;
drh8c42ca92001-06-22 19:15:00 +00002131 idx = pCell->h.iNext;
2132 }
2133 if( idx!=0 ){
2134 printf("ERROR: next cell index out of range: %d\n", idx);
2135 }
2136 printf("right_child: %d\n", pPage->u.hdr.rightChild);
2137 nFree = 0;
2138 i = 0;
2139 idx = pPage->u.hdr.firstFree;
2140 while( idx>0 && idx<SQLITE_PAGE_SIZE ){
2141 FreeBlk *p = (FreeBlk*)&pPage->u.aDisk[idx];
2142 sprintf(range,"%d..%d", idx, idx+p->iSize-1);
2143 nFree += p->iSize;
2144 printf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
2145 i, range, p->iSize, nFree);
2146 idx = p->iNext;
2147 }
2148 if( idx!=0 ){
2149 printf("ERROR: next freeblock index out of range: %d\n", idx);
2150 }
2151 sqlitepager_unref(pPage);
2152 return SQLITE_OK;
2153}
2154#endif
2155
2156#ifdef SQLITE_TEST
2157/*
2158** Put the page number and index of a cursor into aResult[0] and aResult[1]
2159** This routine is used for debugging and testing only.
2160*/
2161int sqliteBtreeCursorDump(BtCursor *pCur, int *aResult){
2162 aResult[0] = sqlitepager_pagenumber(pCur->pPage);
2163 aResult[1] = pCur->idx;
2164 return SQLITE_OK;
2165}
2166#endif