xref: /illumos-gate/usr/src/lib/libsqlite/src/btree.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate ** 2001 September 15
87c478bd9Sstevel@tonic-gate **
97c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
107c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
117c478bd9Sstevel@tonic-gate **
127c478bd9Sstevel@tonic-gate **    May you do good and not evil.
137c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
147c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
157c478bd9Sstevel@tonic-gate **
167c478bd9Sstevel@tonic-gate *************************************************************************
177c478bd9Sstevel@tonic-gate ** $Id: btree.c,v 1.103 2004/03/10 13:42:38 drh Exp $
187c478bd9Sstevel@tonic-gate **
197c478bd9Sstevel@tonic-gate ** This file implements a external (disk-based) database using BTrees.
207c478bd9Sstevel@tonic-gate ** For a detailed discussion of BTrees, refer to
217c478bd9Sstevel@tonic-gate **
227c478bd9Sstevel@tonic-gate **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
237c478bd9Sstevel@tonic-gate **     "Sorting And Searching", pages 473-480. Addison-Wesley
247c478bd9Sstevel@tonic-gate **     Publishing Company, Reading, Massachusetts.
257c478bd9Sstevel@tonic-gate **
267c478bd9Sstevel@tonic-gate ** The basic idea is that each page of the file contains N database
277c478bd9Sstevel@tonic-gate ** entries and N+1 pointers to subpages.
287c478bd9Sstevel@tonic-gate **
297c478bd9Sstevel@tonic-gate **   ----------------------------------------------------------------
307c478bd9Sstevel@tonic-gate **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) |
317c478bd9Sstevel@tonic-gate **   ----------------------------------------------------------------
327c478bd9Sstevel@tonic-gate **
337c478bd9Sstevel@tonic-gate ** All of the keys on the page that Ptr(0) points to have values less
347c478bd9Sstevel@tonic-gate ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
357c478bd9Sstevel@tonic-gate ** values greater than Key(0) and less than Key(1).  All of the keys
367c478bd9Sstevel@tonic-gate ** on Ptr(N+1) and its subpages have values greater than Key(N).  And
377c478bd9Sstevel@tonic-gate ** so forth.
387c478bd9Sstevel@tonic-gate **
39*1da57d55SToomas Soome ** Finding a particular key requires reading O(log(M)) pages from the
407c478bd9Sstevel@tonic-gate ** disk where M is the number of entries in the tree.
417c478bd9Sstevel@tonic-gate **
42*1da57d55SToomas Soome ** In this implementation, a single file can hold one or more separate
437c478bd9Sstevel@tonic-gate ** BTrees.  Each BTree is identified by the index of its root page.  The
447c478bd9Sstevel@tonic-gate ** key and data for any entry are combined to form the "payload".  Up to
457c478bd9Sstevel@tonic-gate ** MX_LOCAL_PAYLOAD bytes of payload can be carried directly on the
467c478bd9Sstevel@tonic-gate ** database page.  If the payload is larger than MX_LOCAL_PAYLOAD bytes
477c478bd9Sstevel@tonic-gate ** then surplus bytes are stored on overflow pages.  The payload for an
48*1da57d55SToomas Soome ** entry and the preceding pointer are combined to form a "Cell".  Each
497c478bd9Sstevel@tonic-gate ** page has a small header which contains the Ptr(N+1) pointer.
507c478bd9Sstevel@tonic-gate **
517c478bd9Sstevel@tonic-gate ** The first page of the file contains a magic string used to verify that
527c478bd9Sstevel@tonic-gate ** the file really is a valid BTree database, a pointer to a list of unused
537c478bd9Sstevel@tonic-gate ** pages in the file, and some meta information.  The root of the first
547c478bd9Sstevel@tonic-gate ** BTree begins on page 2 of the file.  (Pages are numbered beginning with
557c478bd9Sstevel@tonic-gate ** 1, not 0.)  Thus a minimum database contains 2 pages.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate #include "sqliteInt.h"
587c478bd9Sstevel@tonic-gate #include "pager.h"
597c478bd9Sstevel@tonic-gate #include "btree.h"
607c478bd9Sstevel@tonic-gate #include <assert.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* Forward declarations */
637c478bd9Sstevel@tonic-gate static BtOps sqliteBtreeOps;
647c478bd9Sstevel@tonic-gate static BtCursorOps sqliteBtreeCursorOps;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate ** Macros used for byteswapping.  B is a pointer to the Btree
687c478bd9Sstevel@tonic-gate ** structure.  This is needed to access the Btree.needSwab boolean
697c478bd9Sstevel@tonic-gate ** in order to tell if byte swapping is needed or not.
707c478bd9Sstevel@tonic-gate ** X is an unsigned integer.  SWAB16 byte swaps a 16-bit integer.
717c478bd9Sstevel@tonic-gate ** SWAB32 byteswaps a 32-bit integer.
727c478bd9Sstevel@tonic-gate */
737c478bd9Sstevel@tonic-gate #define SWAB16(B,X)   ((B)->needSwab? swab16((u16)X) : ((u16)X))
747c478bd9Sstevel@tonic-gate #define SWAB32(B,X)   ((B)->needSwab? swab32(X) : (X))
757c478bd9Sstevel@tonic-gate #define SWAB_ADD(B,X,A) \
767c478bd9Sstevel@tonic-gate    if((B)->needSwab){ X=swab32(swab32(X)+A); }else{ X += (A); }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate ** The following global variable - available only if SQLITE_TEST is
807c478bd9Sstevel@tonic-gate ** defined - is used to determine whether new databases are created in
817c478bd9Sstevel@tonic-gate ** native byte order or in non-native byte order.  Non-native byte order
827c478bd9Sstevel@tonic-gate ** databases are created for testing purposes only.  Under normal operation,
837c478bd9Sstevel@tonic-gate ** only native byte-order databases should be created, but we should be
847c478bd9Sstevel@tonic-gate ** able to read or write existing databases regardless of the byteorder.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate #ifdef SQLITE_TEST
877c478bd9Sstevel@tonic-gate int btree_native_byte_order = 1;
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate # define btree_native_byte_order 1
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate ** Forward declarations of structures used only in this file.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate typedef struct PageOne PageOne;
967c478bd9Sstevel@tonic-gate typedef struct MemPage MemPage;
977c478bd9Sstevel@tonic-gate typedef struct PageHdr PageHdr;
987c478bd9Sstevel@tonic-gate typedef struct Cell Cell;
997c478bd9Sstevel@tonic-gate typedef struct CellHdr CellHdr;
1007c478bd9Sstevel@tonic-gate typedef struct FreeBlk FreeBlk;
1017c478bd9Sstevel@tonic-gate typedef struct OverflowPage OverflowPage;
1027c478bd9Sstevel@tonic-gate typedef struct FreelistInfo FreelistInfo;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate ** All structures on a database page are aligned to 4-byte boundries.
1067c478bd9Sstevel@tonic-gate ** This routine rounds up a number of bytes to the next multiple of 4.
1077c478bd9Sstevel@tonic-gate **
1087c478bd9Sstevel@tonic-gate ** This might need to change for computer architectures that require
1097c478bd9Sstevel@tonic-gate ** and 8-byte alignment boundry for structures.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate #define ROUNDUP(X)  ((X+3) & ~3)
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate ** This is a magic string that appears at the beginning of every
1157c478bd9Sstevel@tonic-gate ** SQLite database in order to identify the file as a real database.
1167c478bd9Sstevel@tonic-gate */
117*1da57d55SToomas Soome static const char zMagicHeader[] =
1187c478bd9Sstevel@tonic-gate    "** This file contains an SQLite 2.1 database **";
1197c478bd9Sstevel@tonic-gate #define MAGIC_SIZE (sizeof(zMagicHeader))
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate ** This is a magic integer also used to test the integrity of the database
1237c478bd9Sstevel@tonic-gate ** file.  This integer is used in addition to the string above so that
1247c478bd9Sstevel@tonic-gate ** if the file is written on a little-endian architecture and read
1257c478bd9Sstevel@tonic-gate ** on a big-endian architectures (or vice versa) we can detect the
1267c478bd9Sstevel@tonic-gate ** problem.
1277c478bd9Sstevel@tonic-gate **
1287c478bd9Sstevel@tonic-gate ** The number used was obtained at random and has no special
1297c478bd9Sstevel@tonic-gate ** significance other than the fact that it represents a different
1307c478bd9Sstevel@tonic-gate ** integer on little-endian and big-endian machines.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate #define MAGIC 0xdae37528
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate ** The first page of the database file contains a magic header string
1367c478bd9Sstevel@tonic-gate ** to identify the file as an SQLite database file.  It also contains
1377c478bd9Sstevel@tonic-gate ** a pointer to the first free page of the file.  Page 2 contains the
1387c478bd9Sstevel@tonic-gate ** root of the principle BTree.  The file might contain other BTrees
1397c478bd9Sstevel@tonic-gate ** rooted on pages above 2.
1407c478bd9Sstevel@tonic-gate **
1417c478bd9Sstevel@tonic-gate ** The first page also contains SQLITE_N_BTREE_META integers that
1427c478bd9Sstevel@tonic-gate ** can be used by higher-level routines.
1437c478bd9Sstevel@tonic-gate **
1447c478bd9Sstevel@tonic-gate ** Remember that pages are numbered beginning with 1.  (See pager.c
1457c478bd9Sstevel@tonic-gate ** for additional information.)  Page 0 does not exist and a page
1467c478bd9Sstevel@tonic-gate ** number of 0 is used to mean "no such page".
1477c478bd9Sstevel@tonic-gate */
1487c478bd9Sstevel@tonic-gate struct PageOne {
1497c478bd9Sstevel@tonic-gate   char zMagic[MAGIC_SIZE]; /* String that identifies the file as a database */
1507c478bd9Sstevel@tonic-gate   int iMagic;              /* Integer to verify correct byte order */
1517c478bd9Sstevel@tonic-gate   Pgno freeList;           /* First free page in a list of all free pages */
1527c478bd9Sstevel@tonic-gate   int nFree;               /* Number of pages on the free list */
1537c478bd9Sstevel@tonic-gate   int aMeta[SQLITE_N_BTREE_META-1];  /* User defined integers */
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate ** Each database page has a header that is an instance of this
1587c478bd9Sstevel@tonic-gate ** structure.
1597c478bd9Sstevel@tonic-gate **
1607c478bd9Sstevel@tonic-gate ** PageHdr.firstFree is 0 if there is no free space on this page.
161*1da57d55SToomas Soome ** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a
162*1da57d55SToomas Soome ** FreeBlk structure that describes the first block of free space.
1637c478bd9Sstevel@tonic-gate ** All free space is defined by a linked list of FreeBlk structures.
1647c478bd9Sstevel@tonic-gate **
1657c478bd9Sstevel@tonic-gate ** Data is stored in a linked list of Cell structures.  PageHdr.firstCell
1667c478bd9Sstevel@tonic-gate ** is the index into MemPage.u.aDisk[] of the first cell on the page.  The
1677c478bd9Sstevel@tonic-gate ** Cells are kept in sorted order.
1687c478bd9Sstevel@tonic-gate **
1697c478bd9Sstevel@tonic-gate ** A Cell contains all information about a database entry and a pointer
1707c478bd9Sstevel@tonic-gate ** to a child page that contains other entries less than itself.  In
1717c478bd9Sstevel@tonic-gate ** other words, the i-th Cell contains both Ptr(i) and Key(i).  The
1727c478bd9Sstevel@tonic-gate ** right-most pointer of the page is contained in PageHdr.rightChild.
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate struct PageHdr {
1757c478bd9Sstevel@tonic-gate   Pgno rightChild;  /* Child page that comes after all cells on this page */
1767c478bd9Sstevel@tonic-gate   u16 firstCell;    /* Index in MemPage.u.aDisk[] of the first cell */
1777c478bd9Sstevel@tonic-gate   u16 firstFree;    /* Index in MemPage.u.aDisk[] of the first free block */
1787c478bd9Sstevel@tonic-gate };
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate ** Entries on a page of the database are called "Cells".  Each Cell
1827c478bd9Sstevel@tonic-gate ** has a header and data.  This structure defines the header.  The
1837c478bd9Sstevel@tonic-gate ** key and data (collectively the "payload") follow this header on
1847c478bd9Sstevel@tonic-gate ** the database page.
1857c478bd9Sstevel@tonic-gate **
1867c478bd9Sstevel@tonic-gate ** A definition of the complete Cell structure is given below.  The
1877c478bd9Sstevel@tonic-gate ** header for the cell must be defined first in order to do some
1887c478bd9Sstevel@tonic-gate ** of the sizing #defines that follow.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate struct CellHdr {
1917c478bd9Sstevel@tonic-gate   Pgno leftChild; /* Child page that comes before this cell */
1927c478bd9Sstevel@tonic-gate   u16 nKey;       /* Number of bytes in the key */
1937c478bd9Sstevel@tonic-gate   u16 iNext;      /* Index in MemPage.u.aDisk[] of next cell in sorted order */
1947c478bd9Sstevel@tonic-gate   u8 nKeyHi;      /* Upper 8 bits of key size for keys larger than 64K bytes */
1957c478bd9Sstevel@tonic-gate   u8 nDataHi;     /* Upper 8 bits of data size when the size is more than 64K */
1967c478bd9Sstevel@tonic-gate   u16 nData;      /* Number of bytes of data */
1977c478bd9Sstevel@tonic-gate };
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate ** The key and data size are split into a lower 16-bit segment and an
2017c478bd9Sstevel@tonic-gate ** upper 8-bit segment in order to pack them together into a smaller
2027c478bd9Sstevel@tonic-gate ** space.  The following macros reassembly a key or data size back
2037c478bd9Sstevel@tonic-gate ** into an integer.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate #define NKEY(b,h)  (SWAB16(b,h.nKey) + h.nKeyHi*65536)
2067c478bd9Sstevel@tonic-gate #define NDATA(b,h) (SWAB16(b,h.nData) + h.nDataHi*65536)
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate ** The minimum size of a complete Cell.  The Cell must contain a header
2107c478bd9Sstevel@tonic-gate ** and at least 4 bytes of payload.
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate #define MIN_CELL_SIZE  (sizeof(CellHdr)+4)
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate ** The maximum number of database entries that can be held in a single
216*1da57d55SToomas Soome ** page of the database.
2177c478bd9Sstevel@tonic-gate */
2187c478bd9Sstevel@tonic-gate #define MX_CELL ((SQLITE_USABLE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate ** The amount of usable space on a single page of the BTree.  This is the
2227c478bd9Sstevel@tonic-gate ** page size minus the overhead of the page header.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate #define USABLE_SPACE  (SQLITE_USABLE_SIZE - sizeof(PageHdr))
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate ** The maximum amount of payload (in bytes) that can be stored locally for
2287c478bd9Sstevel@tonic-gate ** a database entry.  If the entry contains more data than this, the
2297c478bd9Sstevel@tonic-gate ** extra goes onto overflow pages.
2307c478bd9Sstevel@tonic-gate **
2317c478bd9Sstevel@tonic-gate ** This number is chosen so that at least 4 cells will fit on every page.
2327c478bd9Sstevel@tonic-gate */
2337c478bd9Sstevel@tonic-gate #define MX_LOCAL_PAYLOAD ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3)
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate ** Data on a database page is stored as a linked list of Cell structures.
2377c478bd9Sstevel@tonic-gate ** Both the key and the data are stored in aPayload[].  The key always comes
2387c478bd9Sstevel@tonic-gate ** first.  The aPayload[] field grows as necessary to hold the key and data,
2397c478bd9Sstevel@tonic-gate ** up to a maximum of MX_LOCAL_PAYLOAD bytes.  If the size of the key and
2407c478bd9Sstevel@tonic-gate ** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the
2417c478bd9Sstevel@tonic-gate ** page number of the first overflow page.
2427c478bd9Sstevel@tonic-gate **
2437c478bd9Sstevel@tonic-gate ** Though this structure is fixed in size, the Cell on the database
2447c478bd9Sstevel@tonic-gate ** page varies in size.  Every cell has a CellHdr and at least 4 bytes
2457c478bd9Sstevel@tonic-gate ** of payload space.  Additional payload bytes (up to the maximum of
2467c478bd9Sstevel@tonic-gate ** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as
2477c478bd9Sstevel@tonic-gate ** needed.
2487c478bd9Sstevel@tonic-gate */
2497c478bd9Sstevel@tonic-gate struct Cell {
2507c478bd9Sstevel@tonic-gate   CellHdr h;                        /* The cell header */
2517c478bd9Sstevel@tonic-gate   char aPayload[MX_LOCAL_PAYLOAD];  /* Key and data */
2527c478bd9Sstevel@tonic-gate   Pgno ovfl;                        /* The first overflow page */
2537c478bd9Sstevel@tonic-gate };
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate ** Free space on a page is remembered using a linked list of the FreeBlk
2577c478bd9Sstevel@tonic-gate ** structures.  Space on a database page is allocated in increments of
2587c478bd9Sstevel@tonic-gate ** at least 4 bytes and is always aligned to a 4-byte boundry.  The
2597c478bd9Sstevel@tonic-gate ** linked list of FreeBlks is always kept in order by address.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate struct FreeBlk {
2627c478bd9Sstevel@tonic-gate   u16 iSize;      /* Number of bytes in this block of free space */
2637c478bd9Sstevel@tonic-gate   u16 iNext;      /* Index in MemPage.u.aDisk[] of the next free block */
2647c478bd9Sstevel@tonic-gate };
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate ** The number of bytes of payload that will fit on a single overflow page.
2687c478bd9Sstevel@tonic-gate */
2697c478bd9Sstevel@tonic-gate #define OVERFLOW_SIZE (SQLITE_USABLE_SIZE-sizeof(Pgno))
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate ** When the key and data for a single entry in the BTree will not fit in
2737c478bd9Sstevel@tonic-gate ** the MX_LOCAL_PAYLOAD bytes of space available on the database page,
2747c478bd9Sstevel@tonic-gate ** then all extra bytes are written to a linked list of overflow pages.
2757c478bd9Sstevel@tonic-gate ** Each overflow page is an instance of the following structure.
2767c478bd9Sstevel@tonic-gate **
2777c478bd9Sstevel@tonic-gate ** Unused pages in the database are also represented by instances of
2787c478bd9Sstevel@tonic-gate ** the OverflowPage structure.  The PageOne.freeList field is the
2797c478bd9Sstevel@tonic-gate ** page number of the first page in a linked list of unused database
2807c478bd9Sstevel@tonic-gate ** pages.
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate struct OverflowPage {
2837c478bd9Sstevel@tonic-gate   Pgno iNext;
2847c478bd9Sstevel@tonic-gate   char aPayload[OVERFLOW_SIZE];
2857c478bd9Sstevel@tonic-gate };
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate /*
2887c478bd9Sstevel@tonic-gate ** The PageOne.freeList field points to a linked list of overflow pages
2897c478bd9Sstevel@tonic-gate ** hold information about free pages.  The aPayload section of each
2907c478bd9Sstevel@tonic-gate ** overflow page contains an instance of the following structure.  The
2917c478bd9Sstevel@tonic-gate ** aFree[] array holds the page number of nFree unused pages in the disk
2927c478bd9Sstevel@tonic-gate ** file.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate struct FreelistInfo {
2957c478bd9Sstevel@tonic-gate   int nFree;
2967c478bd9Sstevel@tonic-gate   Pgno aFree[(OVERFLOW_SIZE-sizeof(int))/sizeof(Pgno)];
2977c478bd9Sstevel@tonic-gate };
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate ** For every page in the database file, an instance of the following structure
3017c478bd9Sstevel@tonic-gate ** is stored in memory.  The u.aDisk[] array contains the raw bits read from
3027c478bd9Sstevel@tonic-gate ** the disk.  The rest is auxiliary information held in memory only. The
3037c478bd9Sstevel@tonic-gate ** auxiliary info is only valid for regular database pages - it is not
3047c478bd9Sstevel@tonic-gate ** used for overflow pages and pages on the freelist.
3057c478bd9Sstevel@tonic-gate **
3067c478bd9Sstevel@tonic-gate ** Of particular interest in the auxiliary info is the apCell[] entry.  Each
3077c478bd9Sstevel@tonic-gate ** apCell[] entry is a pointer to a Cell structure in u.aDisk[].  The cells are
3087c478bd9Sstevel@tonic-gate ** put in this array so that they can be accessed in constant time, rather
309*1da57d55SToomas Soome ** than in linear time which would be needed if we had to walk the linked
3107c478bd9Sstevel@tonic-gate ** list on every access.
3117c478bd9Sstevel@tonic-gate **
3127c478bd9Sstevel@tonic-gate ** Note that apCell[] contains enough space to hold up to two more Cells
3137c478bd9Sstevel@tonic-gate ** than can possibly fit on one page.  In the steady state, every apCell[]
3147c478bd9Sstevel@tonic-gate ** points to memory inside u.aDisk[].  But in the middle of an insert
3157c478bd9Sstevel@tonic-gate ** operation, some apCell[] entries may temporarily point to data space
3167c478bd9Sstevel@tonic-gate ** outside of u.aDisk[].  This is a transient situation that is quickly
3177c478bd9Sstevel@tonic-gate ** resolved.  But while it is happening, it is possible for a database
3187c478bd9Sstevel@tonic-gate ** page to hold as many as two more cells than it might otherwise hold.
3197c478bd9Sstevel@tonic-gate ** The extra two entries in apCell[] are an allowance for this situation.
3207c478bd9Sstevel@tonic-gate **
3217c478bd9Sstevel@tonic-gate ** The pParent field points back to the parent page.  This allows us to
3227c478bd9Sstevel@tonic-gate ** walk up the BTree from any leaf to the root.  Care must be taken to
3237c478bd9Sstevel@tonic-gate ** unref() the parent page pointer when this page is no longer referenced.
3247c478bd9Sstevel@tonic-gate ** The pageDestructor() routine handles that chore.
3257c478bd9Sstevel@tonic-gate */
3267c478bd9Sstevel@tonic-gate struct MemPage {
3277c478bd9Sstevel@tonic-gate   union u_page_data {
3287c478bd9Sstevel@tonic-gate     char aDisk[SQLITE_PAGE_SIZE];  /* Page data stored on disk */
3297c478bd9Sstevel@tonic-gate     PageHdr hdr;                   /* Overlay page header */
3307c478bd9Sstevel@tonic-gate   } u;
3317c478bd9Sstevel@tonic-gate   u8 isInit;                     /* True if auxiliary data is initialized */
3327c478bd9Sstevel@tonic-gate   u8 idxShift;                   /* True if apCell[] indices have changed */
3337c478bd9Sstevel@tonic-gate   u8 isOverfull;                 /* Some apCell[] points outside u.aDisk[] */
3347c478bd9Sstevel@tonic-gate   MemPage *pParent;              /* The parent of this page.  NULL for root */
3357c478bd9Sstevel@tonic-gate   int idxParent;                 /* Index in pParent->apCell[] of this node */
3367c478bd9Sstevel@tonic-gate   int nFree;                     /* Number of free bytes in u.aDisk[] */
3377c478bd9Sstevel@tonic-gate   int nCell;                     /* Number of entries on this page */
3387c478bd9Sstevel@tonic-gate   Cell *apCell[MX_CELL+2];       /* All data entires in sorted order */
3397c478bd9Sstevel@tonic-gate };
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate ** The in-memory image of a disk page has the auxiliary information appended
3437c478bd9Sstevel@tonic-gate ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
3447c478bd9Sstevel@tonic-gate ** that extra information.
3457c478bd9Sstevel@tonic-gate */
3467c478bd9Sstevel@tonic-gate #define EXTRA_SIZE (sizeof(MemPage)-sizeof(union u_page_data))
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate ** Everything we need to know about an open database
3507c478bd9Sstevel@tonic-gate */
3517c478bd9Sstevel@tonic-gate struct Btree {
3527c478bd9Sstevel@tonic-gate   BtOps *pOps;          /* Function table */
3537c478bd9Sstevel@tonic-gate   Pager *pPager;        /* The page cache */
3547c478bd9Sstevel@tonic-gate   BtCursor *pCursor;    /* A list of all open cursors */
3557c478bd9Sstevel@tonic-gate   PageOne *page1;       /* First page of the database */
3567c478bd9Sstevel@tonic-gate   u8 inTrans;           /* True if a transaction is in progress */
3577c478bd9Sstevel@tonic-gate   u8 inCkpt;            /* True if there is a checkpoint on the transaction */
3587c478bd9Sstevel@tonic-gate   u8 readOnly;          /* True if the underlying file is readonly */
3597c478bd9Sstevel@tonic-gate   u8 needSwab;          /* Need to byte-swapping */
3607c478bd9Sstevel@tonic-gate };
3617c478bd9Sstevel@tonic-gate typedef Btree Bt;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate ** A cursor is a pointer to a particular entry in the BTree.
3657c478bd9Sstevel@tonic-gate ** The entry is identified by its MemPage and the index in
3667c478bd9Sstevel@tonic-gate ** MemPage.apCell[] of the entry.
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate struct BtCursor {
3697c478bd9Sstevel@tonic-gate   BtCursorOps *pOps;        /* Function table */
3707c478bd9Sstevel@tonic-gate   Btree *pBt;               /* The Btree to which this cursor belongs */
3717c478bd9Sstevel@tonic-gate   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
3727c478bd9Sstevel@tonic-gate   BtCursor *pShared;        /* Loop of cursors with the same root page */
3737c478bd9Sstevel@tonic-gate   Pgno pgnoRoot;            /* The root page of this tree */
3747c478bd9Sstevel@tonic-gate   MemPage *pPage;           /* Page that contains the entry */
3757c478bd9Sstevel@tonic-gate   int idx;                  /* Index of the entry in pPage->apCell[] */
3767c478bd9Sstevel@tonic-gate   u8 wrFlag;                /* True if writable */
3777c478bd9Sstevel@tonic-gate   u8 eSkip;                 /* Determines if next step operation is a no-op */
3787c478bd9Sstevel@tonic-gate   u8 iMatch;                /* compare result from last sqliteBtreeMoveto() */
3797c478bd9Sstevel@tonic-gate };
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate ** Legal values for BtCursor.eSkip.
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate #define SKIP_NONE     0   /* Always step the cursor */
3857c478bd9Sstevel@tonic-gate #define SKIP_NEXT     1   /* The next sqliteBtreeNext() is a no-op */
3867c478bd9Sstevel@tonic-gate #define SKIP_PREV     2   /* The next sqliteBtreePrevious() is a no-op */
3877c478bd9Sstevel@tonic-gate #define SKIP_INVALID  3   /* Calls to Next() and Previous() are invalid */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /* Forward declarations */
3907c478bd9Sstevel@tonic-gate static int fileBtreeCloseCursor(BtCursor *pCur);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate /*
3937c478bd9Sstevel@tonic-gate ** Routines for byte swapping.
3947c478bd9Sstevel@tonic-gate */
swab16(u16 x)3957c478bd9Sstevel@tonic-gate u16 swab16(u16 x){
3967c478bd9Sstevel@tonic-gate   return ((x & 0xff)<<8) | ((x>>8)&0xff);
3977c478bd9Sstevel@tonic-gate }
swab32(u32 x)3987c478bd9Sstevel@tonic-gate u32 swab32(u32 x){
3997c478bd9Sstevel@tonic-gate   return ((x & 0xff)<<24) | ((x & 0xff00)<<8) |
4007c478bd9Sstevel@tonic-gate          ((x>>8) & 0xff00) | ((x>>24)&0xff);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate /*
4047c478bd9Sstevel@tonic-gate ** Compute the total number of bytes that a Cell needs on the main
4057c478bd9Sstevel@tonic-gate ** database page.  The number returned includes the Cell header,
4067c478bd9Sstevel@tonic-gate ** local payload storage, and the pointer to overflow pages (if
4077c478bd9Sstevel@tonic-gate ** applicable).  Additional space allocated on overflow pages
4087c478bd9Sstevel@tonic-gate ** is NOT included in the value returned from this routine.
4097c478bd9Sstevel@tonic-gate */
cellSize(Btree * pBt,Cell * pCell)4107c478bd9Sstevel@tonic-gate static int cellSize(Btree *pBt, Cell *pCell){
4117c478bd9Sstevel@tonic-gate   int n = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h);
4127c478bd9Sstevel@tonic-gate   if( n>MX_LOCAL_PAYLOAD ){
4137c478bd9Sstevel@tonic-gate     n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
4147c478bd9Sstevel@tonic-gate   }else{
4157c478bd9Sstevel@tonic-gate     n = ROUNDUP(n);
4167c478bd9Sstevel@tonic-gate   }
4177c478bd9Sstevel@tonic-gate   n += sizeof(CellHdr);
4187c478bd9Sstevel@tonic-gate   return n;
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate ** Defragment the page given.  All Cells are moved to the
423*1da57d55SToomas Soome ** beginning of the page and all free space is collected
4247c478bd9Sstevel@tonic-gate ** into one big FreeBlk at the end of the page.
4257c478bd9Sstevel@tonic-gate */
defragmentPage(Btree * pBt,MemPage * pPage)4267c478bd9Sstevel@tonic-gate static void defragmentPage(Btree *pBt, MemPage *pPage){
4277c478bd9Sstevel@tonic-gate   int pc, i, n;
4287c478bd9Sstevel@tonic-gate   FreeBlk *pFBlk;
4297c478bd9Sstevel@tonic-gate   char newPage[SQLITE_USABLE_SIZE];
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
4327c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
4337c478bd9Sstevel@tonic-gate   pc = sizeof(PageHdr);
4347c478bd9Sstevel@tonic-gate   pPage->u.hdr.firstCell = SWAB16(pBt, pc);
4357c478bd9Sstevel@tonic-gate   memcpy(newPage, pPage->u.aDisk, pc);
4367c478bd9Sstevel@tonic-gate   for(i=0; i<pPage->nCell; i++){
4377c478bd9Sstevel@tonic-gate     Cell *pCell = pPage->apCell[i];
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate     /* This routine should never be called on an overfull page.  The
4407c478bd9Sstevel@tonic-gate     ** following asserts verify that constraint. */
4417c478bd9Sstevel@tonic-gate     assert( Addr(pCell) > Addr(pPage) );
4427c478bd9Sstevel@tonic-gate     assert( Addr(pCell) < Addr(pPage) + SQLITE_USABLE_SIZE );
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate     n = cellSize(pBt, pCell);
4457c478bd9Sstevel@tonic-gate     pCell->h.iNext = SWAB16(pBt, pc + n);
4467c478bd9Sstevel@tonic-gate     memcpy(&newPage[pc], pCell, n);
4477c478bd9Sstevel@tonic-gate     pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc];
4487c478bd9Sstevel@tonic-gate     pc += n;
4497c478bd9Sstevel@tonic-gate   }
4507c478bd9Sstevel@tonic-gate   assert( pPage->nFree==SQLITE_USABLE_SIZE-pc );
4517c478bd9Sstevel@tonic-gate   memcpy(pPage->u.aDisk, newPage, pc);
4527c478bd9Sstevel@tonic-gate   if( pPage->nCell>0 ){
4537c478bd9Sstevel@tonic-gate     pPage->apCell[pPage->nCell-1]->h.iNext = 0;
4547c478bd9Sstevel@tonic-gate   }
4557c478bd9Sstevel@tonic-gate   pFBlk = (FreeBlk*)&pPage->u.aDisk[pc];
4567c478bd9Sstevel@tonic-gate   pFBlk->iSize = SWAB16(pBt, SQLITE_USABLE_SIZE - pc);
4577c478bd9Sstevel@tonic-gate   pFBlk->iNext = 0;
4587c478bd9Sstevel@tonic-gate   pPage->u.hdr.firstFree = SWAB16(pBt, pc);
4597c478bd9Sstevel@tonic-gate   memset(&pFBlk[1], 0, SQLITE_USABLE_SIZE - pc - sizeof(FreeBlk));
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
463*1da57d55SToomas Soome ** Allocate nByte bytes of space on a page.  nByte must be a
4647c478bd9Sstevel@tonic-gate ** multiple of 4.
4657c478bd9Sstevel@tonic-gate **
4667c478bd9Sstevel@tonic-gate ** Return the index into pPage->u.aDisk[] of the first byte of
4677c478bd9Sstevel@tonic-gate ** the new allocation. Or return 0 if there is not enough free
4687c478bd9Sstevel@tonic-gate ** space on the page to satisfy the allocation request.
4697c478bd9Sstevel@tonic-gate **
4707c478bd9Sstevel@tonic-gate ** If the page contains nBytes of free space but does not contain
4717c478bd9Sstevel@tonic-gate ** nBytes of contiguous free space, then this routine automatically
472*1da57d55SToomas Soome ** calls defragementPage() to consolidate all free space before
4737c478bd9Sstevel@tonic-gate ** allocating the new chunk.
4747c478bd9Sstevel@tonic-gate */
allocateSpace(Btree * pBt,MemPage * pPage,int nByte)4757c478bd9Sstevel@tonic-gate static int allocateSpace(Btree *pBt, MemPage *pPage, int nByte){
4767c478bd9Sstevel@tonic-gate   FreeBlk *p;
4777c478bd9Sstevel@tonic-gate   u16 *pIdx;
4787c478bd9Sstevel@tonic-gate   int start;
4797c478bd9Sstevel@tonic-gate   int iSize;
4807c478bd9Sstevel@tonic-gate #ifndef NDEBUG
4817c478bd9Sstevel@tonic-gate   int cnt = 0;
4827c478bd9Sstevel@tonic-gate #endif
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
4857c478bd9Sstevel@tonic-gate   assert( nByte==ROUNDUP(nByte) );
4867c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
4877c478bd9Sstevel@tonic-gate   if( pPage->nFree<nByte || pPage->isOverfull ) return 0;
4887c478bd9Sstevel@tonic-gate   pIdx = &pPage->u.hdr.firstFree;
4897c478bd9Sstevel@tonic-gate   p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
4907c478bd9Sstevel@tonic-gate   while( (iSize = SWAB16(pBt, p->iSize))<nByte ){
4917c478bd9Sstevel@tonic-gate     assert( cnt++ < SQLITE_USABLE_SIZE/4 );
4927c478bd9Sstevel@tonic-gate     if( p->iNext==0 ){
4937c478bd9Sstevel@tonic-gate       defragmentPage(pBt, pPage);
4947c478bd9Sstevel@tonic-gate       pIdx = &pPage->u.hdr.firstFree;
4957c478bd9Sstevel@tonic-gate     }else{
4967c478bd9Sstevel@tonic-gate       pIdx = &p->iNext;
4977c478bd9Sstevel@tonic-gate     }
4987c478bd9Sstevel@tonic-gate     p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
4997c478bd9Sstevel@tonic-gate   }
5007c478bd9Sstevel@tonic-gate   if( iSize==nByte ){
5017c478bd9Sstevel@tonic-gate     start = SWAB16(pBt, *pIdx);
5027c478bd9Sstevel@tonic-gate     *pIdx = p->iNext;
5037c478bd9Sstevel@tonic-gate   }else{
5047c478bd9Sstevel@tonic-gate     FreeBlk *pNew;
5057c478bd9Sstevel@tonic-gate     start = SWAB16(pBt, *pIdx);
5067c478bd9Sstevel@tonic-gate     pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte];
5077c478bd9Sstevel@tonic-gate     pNew->iNext = p->iNext;
5087c478bd9Sstevel@tonic-gate     pNew->iSize = SWAB16(pBt, iSize - nByte);
5097c478bd9Sstevel@tonic-gate     *pIdx = SWAB16(pBt, start + nByte);
5107c478bd9Sstevel@tonic-gate   }
5117c478bd9Sstevel@tonic-gate   pPage->nFree -= nByte;
5127c478bd9Sstevel@tonic-gate   return start;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate ** Return a section of the MemPage.u.aDisk[] to the freelist.
5177c478bd9Sstevel@tonic-gate ** The first byte of the new free block is pPage->u.aDisk[start]
5187c478bd9Sstevel@tonic-gate ** and the size of the block is "size" bytes.  Size must be
5197c478bd9Sstevel@tonic-gate ** a multiple of 4.
5207c478bd9Sstevel@tonic-gate **
5217c478bd9Sstevel@tonic-gate ** Most of the effort here is involved in coalesing adjacent
5227c478bd9Sstevel@tonic-gate ** free blocks into a single big free block.
5237c478bd9Sstevel@tonic-gate */
freeSpace(Btree * pBt,MemPage * pPage,int start,int size)5247c478bd9Sstevel@tonic-gate static void freeSpace(Btree *pBt, MemPage *pPage, int start, int size){
5257c478bd9Sstevel@tonic-gate   int end = start + size;
5267c478bd9Sstevel@tonic-gate   u16 *pIdx, idx;
5277c478bd9Sstevel@tonic-gate   FreeBlk *pFBlk;
5287c478bd9Sstevel@tonic-gate   FreeBlk *pNew;
5297c478bd9Sstevel@tonic-gate   FreeBlk *pNext;
5307c478bd9Sstevel@tonic-gate   int iSize;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
5337c478bd9Sstevel@tonic-gate   assert( size == ROUNDUP(size) );
5347c478bd9Sstevel@tonic-gate   assert( start == ROUNDUP(start) );
5357c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
5367c478bd9Sstevel@tonic-gate   pIdx = &pPage->u.hdr.firstFree;
5377c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, *pIdx);
5387c478bd9Sstevel@tonic-gate   while( idx!=0 && idx<start ){
5397c478bd9Sstevel@tonic-gate     pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
5407c478bd9Sstevel@tonic-gate     iSize = SWAB16(pBt, pFBlk->iSize);
5417c478bd9Sstevel@tonic-gate     if( idx + iSize == start ){
5427c478bd9Sstevel@tonic-gate       pFBlk->iSize = SWAB16(pBt, iSize + size);
5437c478bd9Sstevel@tonic-gate       if( idx + iSize + size == SWAB16(pBt, pFBlk->iNext) ){
5447c478bd9Sstevel@tonic-gate         pNext = (FreeBlk*)&pPage->u.aDisk[idx + iSize + size];
5457c478bd9Sstevel@tonic-gate         if( pBt->needSwab ){
5467c478bd9Sstevel@tonic-gate           pFBlk->iSize = swab16((u16)swab16(pNext->iSize)+iSize+size);
5477c478bd9Sstevel@tonic-gate         }else{
5487c478bd9Sstevel@tonic-gate           pFBlk->iSize += pNext->iSize;
5497c478bd9Sstevel@tonic-gate         }
5507c478bd9Sstevel@tonic-gate         pFBlk->iNext = pNext->iNext;
5517c478bd9Sstevel@tonic-gate       }
5527c478bd9Sstevel@tonic-gate       pPage->nFree += size;
5537c478bd9Sstevel@tonic-gate       return;
5547c478bd9Sstevel@tonic-gate     }
5557c478bd9Sstevel@tonic-gate     pIdx = &pFBlk->iNext;
5567c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, *pIdx);
5577c478bd9Sstevel@tonic-gate   }
5587c478bd9Sstevel@tonic-gate   pNew = (FreeBlk*)&pPage->u.aDisk[start];
5597c478bd9Sstevel@tonic-gate   if( idx != end ){
5607c478bd9Sstevel@tonic-gate     pNew->iSize = SWAB16(pBt, size);
5617c478bd9Sstevel@tonic-gate     pNew->iNext = SWAB16(pBt, idx);
5627c478bd9Sstevel@tonic-gate   }else{
5637c478bd9Sstevel@tonic-gate     pNext = (FreeBlk*)&pPage->u.aDisk[idx];
5647c478bd9Sstevel@tonic-gate     pNew->iSize = SWAB16(pBt, size + SWAB16(pBt, pNext->iSize));
5657c478bd9Sstevel@tonic-gate     pNew->iNext = pNext->iNext;
5667c478bd9Sstevel@tonic-gate   }
5677c478bd9Sstevel@tonic-gate   *pIdx = SWAB16(pBt, start);
5687c478bd9Sstevel@tonic-gate   pPage->nFree += size;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate ** Initialize the auxiliary information for a disk block.
5737c478bd9Sstevel@tonic-gate **
5747c478bd9Sstevel@tonic-gate ** The pParent parameter must be a pointer to the MemPage which
5757c478bd9Sstevel@tonic-gate ** is the parent of the page being initialized.  The root of the
576*1da57d55SToomas Soome ** BTree (usually page 2) has no parent and so for that page,
5777c478bd9Sstevel@tonic-gate ** pParent==NULL.
5787c478bd9Sstevel@tonic-gate **
5797c478bd9Sstevel@tonic-gate ** Return SQLITE_OK on success.  If we see that the page does
580*1da57d55SToomas Soome ** not contain a well-formed database page, then return
5817c478bd9Sstevel@tonic-gate ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
5827c478bd9Sstevel@tonic-gate ** guarantee that the page is well-formed.  It only shows that
5837c478bd9Sstevel@tonic-gate ** we failed to detect any corruption.
5847c478bd9Sstevel@tonic-gate */
initPage(Bt * pBt,MemPage * pPage,Pgno pgnoThis,MemPage * pParent)5857c478bd9Sstevel@tonic-gate static int initPage(Bt *pBt, MemPage *pPage, Pgno pgnoThis, MemPage *pParent){
5867c478bd9Sstevel@tonic-gate   int idx;           /* An index into pPage->u.aDisk[] */
5877c478bd9Sstevel@tonic-gate   Cell *pCell;       /* A pointer to a Cell in pPage->u.aDisk[] */
5887c478bd9Sstevel@tonic-gate   FreeBlk *pFBlk;    /* A pointer to a free block in pPage->u.aDisk[] */
5897c478bd9Sstevel@tonic-gate   int sz;            /* The size of a Cell in bytes */
5907c478bd9Sstevel@tonic-gate   int freeSpace;     /* Amount of free space on the page */
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate   if( pPage->pParent ){
5937c478bd9Sstevel@tonic-gate     assert( pPage->pParent==pParent );
5947c478bd9Sstevel@tonic-gate     return SQLITE_OK;
5957c478bd9Sstevel@tonic-gate   }
5967c478bd9Sstevel@tonic-gate   if( pParent ){
5977c478bd9Sstevel@tonic-gate     pPage->pParent = pParent;
5987c478bd9Sstevel@tonic-gate     sqlitepager_ref(pParent);
5997c478bd9Sstevel@tonic-gate   }
6007c478bd9Sstevel@tonic-gate   if( pPage->isInit ) return SQLITE_OK;
6017c478bd9Sstevel@tonic-gate   pPage->isInit = 1;
6027c478bd9Sstevel@tonic-gate   pPage->nCell = 0;
6037c478bd9Sstevel@tonic-gate   freeSpace = USABLE_SPACE;
6047c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstCell);
6057c478bd9Sstevel@tonic-gate   while( idx!=0 ){
6067c478bd9Sstevel@tonic-gate     if( idx>SQLITE_USABLE_SIZE-MIN_CELL_SIZE ) goto page_format_error;
6077c478bd9Sstevel@tonic-gate     if( idx<sizeof(PageHdr) ) goto page_format_error;
6087c478bd9Sstevel@tonic-gate     if( idx!=ROUNDUP(idx) ) goto page_format_error;
6097c478bd9Sstevel@tonic-gate     pCell = (Cell*)&pPage->u.aDisk[idx];
6107c478bd9Sstevel@tonic-gate     sz = cellSize(pBt, pCell);
6117c478bd9Sstevel@tonic-gate     if( idx+sz > SQLITE_USABLE_SIZE ) goto page_format_error;
6127c478bd9Sstevel@tonic-gate     freeSpace -= sz;
6137c478bd9Sstevel@tonic-gate     pPage->apCell[pPage->nCell++] = pCell;
6147c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, pCell->h.iNext);
6157c478bd9Sstevel@tonic-gate   }
6167c478bd9Sstevel@tonic-gate   pPage->nFree = 0;
6177c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstFree);
6187c478bd9Sstevel@tonic-gate   while( idx!=0 ){
6197c478bd9Sstevel@tonic-gate     int iNext;
6207c478bd9Sstevel@tonic-gate     if( idx>SQLITE_USABLE_SIZE-sizeof(FreeBlk) ) goto page_format_error;
6217c478bd9Sstevel@tonic-gate     if( idx<sizeof(PageHdr) ) goto page_format_error;
6227c478bd9Sstevel@tonic-gate     pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
6237c478bd9Sstevel@tonic-gate     pPage->nFree += SWAB16(pBt, pFBlk->iSize);
6247c478bd9Sstevel@tonic-gate     iNext = SWAB16(pBt, pFBlk->iNext);
6257c478bd9Sstevel@tonic-gate     if( iNext>0 && iNext <= idx ) goto page_format_error;
6267c478bd9Sstevel@tonic-gate     idx = iNext;
6277c478bd9Sstevel@tonic-gate   }
6287c478bd9Sstevel@tonic-gate   if( pPage->nCell==0 && pPage->nFree==0 ){
6297c478bd9Sstevel@tonic-gate     /* As a special case, an uninitialized root page appears to be
6307c478bd9Sstevel@tonic-gate     ** an empty database */
6317c478bd9Sstevel@tonic-gate     return SQLITE_OK;
6327c478bd9Sstevel@tonic-gate   }
6337c478bd9Sstevel@tonic-gate   if( pPage->nFree!=freeSpace ) goto page_format_error;
6347c478bd9Sstevel@tonic-gate   return SQLITE_OK;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate page_format_error:
6377c478bd9Sstevel@tonic-gate   return SQLITE_CORRUPT;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate ** Set up a raw page so that it looks like a database page holding
6427c478bd9Sstevel@tonic-gate ** no entries.
6437c478bd9Sstevel@tonic-gate */
zeroPage(Btree * pBt,MemPage * pPage)6447c478bd9Sstevel@tonic-gate static void zeroPage(Btree *pBt, MemPage *pPage){
6457c478bd9Sstevel@tonic-gate   PageHdr *pHdr;
6467c478bd9Sstevel@tonic-gate   FreeBlk *pFBlk;
6477c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
6487c478bd9Sstevel@tonic-gate   memset(pPage, 0, SQLITE_USABLE_SIZE);
6497c478bd9Sstevel@tonic-gate   pHdr = &pPage->u.hdr;
6507c478bd9Sstevel@tonic-gate   pHdr->firstCell = 0;
6517c478bd9Sstevel@tonic-gate   pHdr->firstFree = SWAB16(pBt, sizeof(*pHdr));
6527c478bd9Sstevel@tonic-gate   pFBlk = (FreeBlk*)&pHdr[1];
6537c478bd9Sstevel@tonic-gate   pFBlk->iNext = 0;
6547c478bd9Sstevel@tonic-gate   pPage->nFree = SQLITE_USABLE_SIZE - sizeof(*pHdr);
6557c478bd9Sstevel@tonic-gate   pFBlk->iSize = SWAB16(pBt, pPage->nFree);
6567c478bd9Sstevel@tonic-gate   pPage->nCell = 0;
6577c478bd9Sstevel@tonic-gate   pPage->isOverfull = 0;
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate ** This routine is called when the reference count for a page
6627c478bd9Sstevel@tonic-gate ** reaches zero.  We need to unref the pParent pointer when that
6637c478bd9Sstevel@tonic-gate ** happens.
6647c478bd9Sstevel@tonic-gate */
pageDestructor(void * pData)6657c478bd9Sstevel@tonic-gate static void pageDestructor(void *pData){
6667c478bd9Sstevel@tonic-gate   MemPage *pPage = (MemPage*)pData;
6677c478bd9Sstevel@tonic-gate   if( pPage->pParent ){
6687c478bd9Sstevel@tonic-gate     MemPage *pParent = pPage->pParent;
6697c478bd9Sstevel@tonic-gate     pPage->pParent = 0;
6707c478bd9Sstevel@tonic-gate     sqlitepager_unref(pParent);
6717c478bd9Sstevel@tonic-gate   }
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate ** Open a new database.
6767c478bd9Sstevel@tonic-gate **
6777c478bd9Sstevel@tonic-gate ** Actually, this routine just sets up the internal data structures
678*1da57d55SToomas Soome ** for accessing the database.  We do not open the database file
6797c478bd9Sstevel@tonic-gate ** until the first page is loaded.
6807c478bd9Sstevel@tonic-gate **
6817c478bd9Sstevel@tonic-gate ** zFilename is the name of the database file.  If zFilename is NULL
6827c478bd9Sstevel@tonic-gate ** a new database with a random name is created.  This randomly named
6837c478bd9Sstevel@tonic-gate ** database file will be deleted when sqliteBtreeClose() is called.
6847c478bd9Sstevel@tonic-gate */
sqliteBtreeOpen(const char * zFilename,int omitJournal,int nCache,Btree ** ppBtree)6857c478bd9Sstevel@tonic-gate int sqliteBtreeOpen(
6867c478bd9Sstevel@tonic-gate   const char *zFilename,    /* Name of the file containing the BTree database */
6877c478bd9Sstevel@tonic-gate   int omitJournal,          /* if TRUE then do not journal this file */
6887c478bd9Sstevel@tonic-gate   int nCache,               /* How many pages in the page cache */
6897c478bd9Sstevel@tonic-gate   Btree **ppBtree           /* Pointer to new Btree object written here */
6907c478bd9Sstevel@tonic-gate ){
6917c478bd9Sstevel@tonic-gate   Btree *pBt;
6927c478bd9Sstevel@tonic-gate   int rc;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate   /*
6957c478bd9Sstevel@tonic-gate   ** The following asserts make sure that structures used by the btree are
6967c478bd9Sstevel@tonic-gate   ** the right size.  This is to guard against size changes that result
6977c478bd9Sstevel@tonic-gate   ** when compiling on a different architecture.
6987c478bd9Sstevel@tonic-gate   */
6997c478bd9Sstevel@tonic-gate   assert( sizeof(u32)==4 );
7007c478bd9Sstevel@tonic-gate   assert( sizeof(u16)==2 );
7017c478bd9Sstevel@tonic-gate   assert( sizeof(Pgno)==4 );
7027c478bd9Sstevel@tonic-gate   assert( sizeof(PageHdr)==8 );
7037c478bd9Sstevel@tonic-gate   assert( sizeof(CellHdr)==12 );
7047c478bd9Sstevel@tonic-gate   assert( sizeof(FreeBlk)==4 );
7057c478bd9Sstevel@tonic-gate   assert( sizeof(OverflowPage)==SQLITE_USABLE_SIZE );
7067c478bd9Sstevel@tonic-gate   assert( sizeof(FreelistInfo)==OVERFLOW_SIZE );
7077c478bd9Sstevel@tonic-gate   assert( sizeof(ptr)==sizeof(char*) );
7087c478bd9Sstevel@tonic-gate   assert( sizeof(uptr)==sizeof(ptr) );
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate   pBt = sqliteMalloc( sizeof(*pBt) );
7117c478bd9Sstevel@tonic-gate   if( pBt==0 ){
7127c478bd9Sstevel@tonic-gate     *ppBtree = 0;
7137c478bd9Sstevel@tonic-gate     return SQLITE_NOMEM;
7147c478bd9Sstevel@tonic-gate   }
7157c478bd9Sstevel@tonic-gate   if( nCache<10 ) nCache = 10;
7167c478bd9Sstevel@tonic-gate   rc = sqlitepager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE,
7177c478bd9Sstevel@tonic-gate                         !omitJournal);
7187c478bd9Sstevel@tonic-gate   if( rc!=SQLITE_OK ){
7197c478bd9Sstevel@tonic-gate     if( pBt->pPager ) sqlitepager_close(pBt->pPager);
7207c478bd9Sstevel@tonic-gate     sqliteFree(pBt);
7217c478bd9Sstevel@tonic-gate     *ppBtree = 0;
7227c478bd9Sstevel@tonic-gate     return rc;
7237c478bd9Sstevel@tonic-gate   }
7247c478bd9Sstevel@tonic-gate   sqlitepager_set_destructor(pBt->pPager, pageDestructor);
7257c478bd9Sstevel@tonic-gate   pBt->pCursor = 0;
7267c478bd9Sstevel@tonic-gate   pBt->page1 = 0;
7277c478bd9Sstevel@tonic-gate   pBt->readOnly = sqlitepager_isreadonly(pBt->pPager);
7287c478bd9Sstevel@tonic-gate   pBt->pOps = &sqliteBtreeOps;
7297c478bd9Sstevel@tonic-gate   *ppBtree = pBt;
7307c478bd9Sstevel@tonic-gate   return SQLITE_OK;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate ** Close an open database and invalidate all cursors.
7357c478bd9Sstevel@tonic-gate */
fileBtreeClose(Btree * pBt)7367c478bd9Sstevel@tonic-gate static int fileBtreeClose(Btree *pBt){
7377c478bd9Sstevel@tonic-gate   while( pBt->pCursor ){
7387c478bd9Sstevel@tonic-gate     fileBtreeCloseCursor(pBt->pCursor);
7397c478bd9Sstevel@tonic-gate   }
7407c478bd9Sstevel@tonic-gate   sqlitepager_close(pBt->pPager);
7417c478bd9Sstevel@tonic-gate   sqliteFree(pBt);
7427c478bd9Sstevel@tonic-gate   return SQLITE_OK;
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate ** Change the limit on the number of pages allowed in the cache.
7477c478bd9Sstevel@tonic-gate **
7487c478bd9Sstevel@tonic-gate ** The maximum number of cache pages is set to the absolute
7497c478bd9Sstevel@tonic-gate ** value of mxPage.  If mxPage is negative, the pager will
7507c478bd9Sstevel@tonic-gate ** operate asynchronously - it will not stop to do fsync()s
7517c478bd9Sstevel@tonic-gate ** to insure data is written to the disk surface before
7527c478bd9Sstevel@tonic-gate ** continuing.  Transactions still work if synchronous is off,
7537c478bd9Sstevel@tonic-gate ** and the database cannot be corrupted if this program
7547c478bd9Sstevel@tonic-gate ** crashes.  But if the operating system crashes or there is
7557c478bd9Sstevel@tonic-gate ** an abrupt power failure when synchronous is off, the database
7567c478bd9Sstevel@tonic-gate ** could be left in an inconsistent and unrecoverable state.
7577c478bd9Sstevel@tonic-gate ** Synchronous is on by default so database corruption is not
7587c478bd9Sstevel@tonic-gate ** normally a worry.
7597c478bd9Sstevel@tonic-gate */
fileBtreeSetCacheSize(Btree * pBt,int mxPage)7607c478bd9Sstevel@tonic-gate static int fileBtreeSetCacheSize(Btree *pBt, int mxPage){
7617c478bd9Sstevel@tonic-gate   sqlitepager_set_cachesize(pBt->pPager, mxPage);
7627c478bd9Sstevel@tonic-gate   return SQLITE_OK;
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate ** Change the way data is synced to disk in order to increase or decrease
7677c478bd9Sstevel@tonic-gate ** how well the database resists damage due to OS crashes and power
7687c478bd9Sstevel@tonic-gate ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
7697c478bd9Sstevel@tonic-gate ** there is a high probability of damage)  Level 2 is the default.  There
7707c478bd9Sstevel@tonic-gate ** is a very low but non-zero probability of damage.  Level 3 reduces the
7717c478bd9Sstevel@tonic-gate ** probability of damage to near zero but with a write performance reduction.
7727c478bd9Sstevel@tonic-gate */
fileBtreeSetSafetyLevel(Btree * pBt,int level)7737c478bd9Sstevel@tonic-gate static int fileBtreeSetSafetyLevel(Btree *pBt, int level){
7747c478bd9Sstevel@tonic-gate   sqlitepager_set_safety_level(pBt->pPager, level);
7757c478bd9Sstevel@tonic-gate   return SQLITE_OK;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate ** Get a reference to page1 of the database file.  This will
7807c478bd9Sstevel@tonic-gate ** also acquire a readlock on that file.
7817c478bd9Sstevel@tonic-gate **
7827c478bd9Sstevel@tonic-gate ** SQLITE_OK is returned on success.  If the file is not a
7837c478bd9Sstevel@tonic-gate ** well-formed database file, then SQLITE_CORRUPT is returned.
7847c478bd9Sstevel@tonic-gate ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
7857c478bd9Sstevel@tonic-gate ** is returned if we run out of memory.  SQLITE_PROTOCOL is returned
7867c478bd9Sstevel@tonic-gate ** if there is a locking protocol violation.
7877c478bd9Sstevel@tonic-gate */
lockBtree(Btree * pBt)7887c478bd9Sstevel@tonic-gate static int lockBtree(Btree *pBt){
7897c478bd9Sstevel@tonic-gate   int rc;
7907c478bd9Sstevel@tonic-gate   if( pBt->page1 ) return SQLITE_OK;
7917c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1);
7927c478bd9Sstevel@tonic-gate   if( rc!=SQLITE_OK ) return rc;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate   /* Do some checking to help insure the file we opened really is
795*1da57d55SToomas Soome   ** a valid database file.
7967c478bd9Sstevel@tonic-gate   */
7977c478bd9Sstevel@tonic-gate   if( sqlitepager_pagecount(pBt->pPager)>0 ){
7987c478bd9Sstevel@tonic-gate     PageOne *pP1 = pBt->page1;
7997c478bd9Sstevel@tonic-gate     if( strcmp(pP1->zMagic,zMagicHeader)!=0 ||
8007c478bd9Sstevel@tonic-gate           (pP1->iMagic!=MAGIC && swab32(pP1->iMagic)!=MAGIC) ){
8017c478bd9Sstevel@tonic-gate       rc = SQLITE_NOTADB;
8027c478bd9Sstevel@tonic-gate       goto page1_init_failed;
8037c478bd9Sstevel@tonic-gate     }
8047c478bd9Sstevel@tonic-gate     pBt->needSwab = pP1->iMagic!=MAGIC;
8057c478bd9Sstevel@tonic-gate   }
8067c478bd9Sstevel@tonic-gate   return rc;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate page1_init_failed:
8097c478bd9Sstevel@tonic-gate   sqlitepager_unref(pBt->page1);
8107c478bd9Sstevel@tonic-gate   pBt->page1 = 0;
8117c478bd9Sstevel@tonic-gate   return rc;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate ** If there are no outstanding cursors and we are not in the middle
8167c478bd9Sstevel@tonic-gate ** of a transaction but there is a read lock on the database, then
817*1da57d55SToomas Soome ** this routine unrefs the first page of the database file which
8187c478bd9Sstevel@tonic-gate ** has the effect of releasing the read lock.
8197c478bd9Sstevel@tonic-gate **
8207c478bd9Sstevel@tonic-gate ** If there are any outstanding cursors, this routine is a no-op.
8217c478bd9Sstevel@tonic-gate **
8227c478bd9Sstevel@tonic-gate ** If there is a transaction in progress, this routine is a no-op.
8237c478bd9Sstevel@tonic-gate */
unlockBtreeIfUnused(Btree * pBt)8247c478bd9Sstevel@tonic-gate static void unlockBtreeIfUnused(Btree *pBt){
8257c478bd9Sstevel@tonic-gate   if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){
8267c478bd9Sstevel@tonic-gate     sqlitepager_unref(pBt->page1);
8277c478bd9Sstevel@tonic-gate     pBt->page1 = 0;
8287c478bd9Sstevel@tonic-gate     pBt->inTrans = 0;
8297c478bd9Sstevel@tonic-gate     pBt->inCkpt = 0;
8307c478bd9Sstevel@tonic-gate   }
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate ** Create a new database by initializing the first two pages of the
8357c478bd9Sstevel@tonic-gate ** file.
8367c478bd9Sstevel@tonic-gate */
newDatabase(Btree * pBt)8377c478bd9Sstevel@tonic-gate static int newDatabase(Btree *pBt){
8387c478bd9Sstevel@tonic-gate   MemPage *pRoot;
8397c478bd9Sstevel@tonic-gate   PageOne *pP1;
8407c478bd9Sstevel@tonic-gate   int rc;
8417c478bd9Sstevel@tonic-gate   if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK;
8427c478bd9Sstevel@tonic-gate   pP1 = pBt->page1;
8437c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pBt->page1);
8447c478bd9Sstevel@tonic-gate   if( rc ) return rc;
8457c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot);
8467c478bd9Sstevel@tonic-gate   if( rc ) return rc;
8477c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pRoot);
8487c478bd9Sstevel@tonic-gate   if( rc ){
8497c478bd9Sstevel@tonic-gate     sqlitepager_unref(pRoot);
8507c478bd9Sstevel@tonic-gate     return rc;
8517c478bd9Sstevel@tonic-gate   }
8527c478bd9Sstevel@tonic-gate   strcpy(pP1->zMagic, zMagicHeader);
8537c478bd9Sstevel@tonic-gate   if( btree_native_byte_order ){
8547c478bd9Sstevel@tonic-gate     pP1->iMagic = MAGIC;
8557c478bd9Sstevel@tonic-gate     pBt->needSwab = 0;
8567c478bd9Sstevel@tonic-gate   }else{
8577c478bd9Sstevel@tonic-gate     pP1->iMagic = swab32(MAGIC);
8587c478bd9Sstevel@tonic-gate     pBt->needSwab = 1;
8597c478bd9Sstevel@tonic-gate   }
8607c478bd9Sstevel@tonic-gate   zeroPage(pBt, pRoot);
8617c478bd9Sstevel@tonic-gate   sqlitepager_unref(pRoot);
8627c478bd9Sstevel@tonic-gate   return SQLITE_OK;
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /*
8667c478bd9Sstevel@tonic-gate ** Attempt to start a new transaction.
8677c478bd9Sstevel@tonic-gate **
8687c478bd9Sstevel@tonic-gate ** A transaction must be started before attempting any changes
8697c478bd9Sstevel@tonic-gate ** to the database.  None of the following routines will work
8707c478bd9Sstevel@tonic-gate ** unless a transaction is started first:
8717c478bd9Sstevel@tonic-gate **
8727c478bd9Sstevel@tonic-gate **      sqliteBtreeCreateTable()
8737c478bd9Sstevel@tonic-gate **      sqliteBtreeCreateIndex()
8747c478bd9Sstevel@tonic-gate **      sqliteBtreeClearTable()
8757c478bd9Sstevel@tonic-gate **      sqliteBtreeDropTable()
8767c478bd9Sstevel@tonic-gate **      sqliteBtreeInsert()
8777c478bd9Sstevel@tonic-gate **      sqliteBtreeDelete()
8787c478bd9Sstevel@tonic-gate **      sqliteBtreeUpdateMeta()
8797c478bd9Sstevel@tonic-gate */
fileBtreeBeginTrans(Btree * pBt)8807c478bd9Sstevel@tonic-gate static int fileBtreeBeginTrans(Btree *pBt){
8817c478bd9Sstevel@tonic-gate   int rc;
8827c478bd9Sstevel@tonic-gate   if( pBt->inTrans ) return SQLITE_ERROR;
8837c478bd9Sstevel@tonic-gate   if( pBt->readOnly ) return SQLITE_READONLY;
8847c478bd9Sstevel@tonic-gate   if( pBt->page1==0 ){
8857c478bd9Sstevel@tonic-gate     rc = lockBtree(pBt);
8867c478bd9Sstevel@tonic-gate     if( rc!=SQLITE_OK ){
8877c478bd9Sstevel@tonic-gate       return rc;
8887c478bd9Sstevel@tonic-gate     }
8897c478bd9Sstevel@tonic-gate   }
8907c478bd9Sstevel@tonic-gate   rc = sqlitepager_begin(pBt->page1);
8917c478bd9Sstevel@tonic-gate   if( rc==SQLITE_OK ){
8927c478bd9Sstevel@tonic-gate     rc = newDatabase(pBt);
8937c478bd9Sstevel@tonic-gate   }
8947c478bd9Sstevel@tonic-gate   if( rc==SQLITE_OK ){
8957c478bd9Sstevel@tonic-gate     pBt->inTrans = 1;
8967c478bd9Sstevel@tonic-gate     pBt->inCkpt = 0;
8977c478bd9Sstevel@tonic-gate   }else{
8987c478bd9Sstevel@tonic-gate     unlockBtreeIfUnused(pBt);
8997c478bd9Sstevel@tonic-gate   }
9007c478bd9Sstevel@tonic-gate   return rc;
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate ** Commit the transaction currently in progress.
9057c478bd9Sstevel@tonic-gate **
9067c478bd9Sstevel@tonic-gate ** This will release the write lock on the database file.  If there
9077c478bd9Sstevel@tonic-gate ** are no active cursors, it also releases the read lock.
9087c478bd9Sstevel@tonic-gate */
fileBtreeCommit(Btree * pBt)9097c478bd9Sstevel@tonic-gate static int fileBtreeCommit(Btree *pBt){
9107c478bd9Sstevel@tonic-gate   int rc;
9117c478bd9Sstevel@tonic-gate   rc = pBt->readOnly ? SQLITE_OK : sqlitepager_commit(pBt->pPager);
9127c478bd9Sstevel@tonic-gate   pBt->inTrans = 0;
9137c478bd9Sstevel@tonic-gate   pBt->inCkpt = 0;
9147c478bd9Sstevel@tonic-gate   unlockBtreeIfUnused(pBt);
9157c478bd9Sstevel@tonic-gate   return rc;
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate ** Rollback the transaction in progress.  All cursors will be
9207c478bd9Sstevel@tonic-gate ** invalided by this operation.  Any attempt to use a cursor
9217c478bd9Sstevel@tonic-gate ** that was open at the beginning of this operation will result
9227c478bd9Sstevel@tonic-gate ** in an error.
9237c478bd9Sstevel@tonic-gate **
9247c478bd9Sstevel@tonic-gate ** This will release the write lock on the database file.  If there
9257c478bd9Sstevel@tonic-gate ** are no active cursors, it also releases the read lock.
9267c478bd9Sstevel@tonic-gate */
fileBtreeRollback(Btree * pBt)9277c478bd9Sstevel@tonic-gate static int fileBtreeRollback(Btree *pBt){
9287c478bd9Sstevel@tonic-gate   int rc;
9297c478bd9Sstevel@tonic-gate   BtCursor *pCur;
9307c478bd9Sstevel@tonic-gate   if( pBt->inTrans==0 ) return SQLITE_OK;
9317c478bd9Sstevel@tonic-gate   pBt->inTrans = 0;
9327c478bd9Sstevel@tonic-gate   pBt->inCkpt = 0;
9337c478bd9Sstevel@tonic-gate   rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager);
9347c478bd9Sstevel@tonic-gate   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
9357c478bd9Sstevel@tonic-gate     if( pCur->pPage && pCur->pPage->isInit==0 ){
9367c478bd9Sstevel@tonic-gate       sqlitepager_unref(pCur->pPage);
9377c478bd9Sstevel@tonic-gate       pCur->pPage = 0;
9387c478bd9Sstevel@tonic-gate     }
9397c478bd9Sstevel@tonic-gate   }
9407c478bd9Sstevel@tonic-gate   unlockBtreeIfUnused(pBt);
9417c478bd9Sstevel@tonic-gate   return rc;
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate ** Set the checkpoint for the current transaction.  The checkpoint serves
9467c478bd9Sstevel@tonic-gate ** as a sub-transaction that can be rolled back independently of the
9477c478bd9Sstevel@tonic-gate ** main transaction.  You must start a transaction before starting a
9487c478bd9Sstevel@tonic-gate ** checkpoint.  The checkpoint is ended automatically if the transaction
9497c478bd9Sstevel@tonic-gate ** commits or rolls back.
9507c478bd9Sstevel@tonic-gate **
9517c478bd9Sstevel@tonic-gate ** Only one checkpoint may be active at a time.  It is an error to try
9527c478bd9Sstevel@tonic-gate ** to start a new checkpoint if another checkpoint is already active.
9537c478bd9Sstevel@tonic-gate */
fileBtreeBeginCkpt(Btree * pBt)9547c478bd9Sstevel@tonic-gate static int fileBtreeBeginCkpt(Btree *pBt){
9557c478bd9Sstevel@tonic-gate   int rc;
9567c478bd9Sstevel@tonic-gate   if( !pBt->inTrans || pBt->inCkpt ){
9577c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
9587c478bd9Sstevel@tonic-gate   }
9597c478bd9Sstevel@tonic-gate   rc = pBt->readOnly ? SQLITE_OK : sqlitepager_ckpt_begin(pBt->pPager);
9607c478bd9Sstevel@tonic-gate   pBt->inCkpt = 1;
9617c478bd9Sstevel@tonic-gate   return rc;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate ** Commit a checkpoint to transaction currently in progress.  If no
9677c478bd9Sstevel@tonic-gate ** checkpoint is active, this is a no-op.
9687c478bd9Sstevel@tonic-gate */
fileBtreeCommitCkpt(Btree * pBt)9697c478bd9Sstevel@tonic-gate static int fileBtreeCommitCkpt(Btree *pBt){
9707c478bd9Sstevel@tonic-gate   int rc;
9717c478bd9Sstevel@tonic-gate   if( pBt->inCkpt && !pBt->readOnly ){
9727c478bd9Sstevel@tonic-gate     rc = sqlitepager_ckpt_commit(pBt->pPager);
9737c478bd9Sstevel@tonic-gate   }else{
9747c478bd9Sstevel@tonic-gate     rc = SQLITE_OK;
9757c478bd9Sstevel@tonic-gate   }
9767c478bd9Sstevel@tonic-gate   pBt->inCkpt = 0;
9777c478bd9Sstevel@tonic-gate   return rc;
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate ** Rollback the checkpoint to the current transaction.  If there
9827c478bd9Sstevel@tonic-gate ** is no active checkpoint or transaction, this routine is a no-op.
9837c478bd9Sstevel@tonic-gate **
9847c478bd9Sstevel@tonic-gate ** All cursors will be invalided by this operation.  Any attempt
9857c478bd9Sstevel@tonic-gate ** to use a cursor that was open at the beginning of this operation
9867c478bd9Sstevel@tonic-gate ** will result in an error.
9877c478bd9Sstevel@tonic-gate */
fileBtreeRollbackCkpt(Btree * pBt)9887c478bd9Sstevel@tonic-gate static int fileBtreeRollbackCkpt(Btree *pBt){
9897c478bd9Sstevel@tonic-gate   int rc;
9907c478bd9Sstevel@tonic-gate   BtCursor *pCur;
9917c478bd9Sstevel@tonic-gate   if( pBt->inCkpt==0 || pBt->readOnly ) return SQLITE_OK;
9927c478bd9Sstevel@tonic-gate   rc = sqlitepager_ckpt_rollback(pBt->pPager);
9937c478bd9Sstevel@tonic-gate   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
9947c478bd9Sstevel@tonic-gate     if( pCur->pPage && pCur->pPage->isInit==0 ){
9957c478bd9Sstevel@tonic-gate       sqlitepager_unref(pCur->pPage);
9967c478bd9Sstevel@tonic-gate       pCur->pPage = 0;
9977c478bd9Sstevel@tonic-gate     }
9987c478bd9Sstevel@tonic-gate   }
9997c478bd9Sstevel@tonic-gate   pBt->inCkpt = 0;
10007c478bd9Sstevel@tonic-gate   return rc;
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate /*
10047c478bd9Sstevel@tonic-gate ** Create a new cursor for the BTree whose root is on the page
1005*1da57d55SToomas Soome ** iTable.  The act of acquiring a cursor gets a read lock on
10067c478bd9Sstevel@tonic-gate ** the database file.
10077c478bd9Sstevel@tonic-gate **
10087c478bd9Sstevel@tonic-gate ** If wrFlag==0, then the cursor can only be used for reading.
10097c478bd9Sstevel@tonic-gate ** If wrFlag==1, then the cursor can be used for reading or for
10107c478bd9Sstevel@tonic-gate ** writing if other conditions for writing are also met.  These
10117c478bd9Sstevel@tonic-gate ** are the conditions that must be met in order for writing to
10127c478bd9Sstevel@tonic-gate ** be allowed:
10137c478bd9Sstevel@tonic-gate **
10147c478bd9Sstevel@tonic-gate ** 1:  The cursor must have been opened with wrFlag==1
10157c478bd9Sstevel@tonic-gate **
10167c478bd9Sstevel@tonic-gate ** 2:  No other cursors may be open with wrFlag==0 on the same table
10177c478bd9Sstevel@tonic-gate **
10187c478bd9Sstevel@tonic-gate ** 3:  The database must be writable (not on read-only media)
10197c478bd9Sstevel@tonic-gate **
10207c478bd9Sstevel@tonic-gate ** 4:  There must be an active transaction.
10217c478bd9Sstevel@tonic-gate **
10227c478bd9Sstevel@tonic-gate ** Condition 2 warrants further discussion.  If any cursor is opened
10237c478bd9Sstevel@tonic-gate ** on a table with wrFlag==0, that prevents all other cursors from
10247c478bd9Sstevel@tonic-gate ** writing to that table.  This is a kind of "read-lock".  When a cursor
10257c478bd9Sstevel@tonic-gate ** is opened with wrFlag==0 it is guaranteed that the table will not
10267c478bd9Sstevel@tonic-gate ** change as long as the cursor is open.  This allows the cursor to
10277c478bd9Sstevel@tonic-gate ** do a sequential scan of the table without having to worry about
10287c478bd9Sstevel@tonic-gate ** entries being inserted or deleted during the scan.  Cursors should
10297c478bd9Sstevel@tonic-gate ** be opened with wrFlag==0 only if this read-lock property is needed.
10307c478bd9Sstevel@tonic-gate ** That is to say, cursors should be opened with wrFlag==0 only if they
10317c478bd9Sstevel@tonic-gate ** intend to use the sqliteBtreeNext() system call.  All other cursors
10327c478bd9Sstevel@tonic-gate ** should be opened with wrFlag==1 even if they never really intend
10337c478bd9Sstevel@tonic-gate ** to write.
1034*1da57d55SToomas Soome **
10357c478bd9Sstevel@tonic-gate ** No checking is done to make sure that page iTable really is the
10367c478bd9Sstevel@tonic-gate ** root page of a b-tree.  If it is not, then the cursor acquired
10377c478bd9Sstevel@tonic-gate ** will not work correctly.
10387c478bd9Sstevel@tonic-gate */
1039*1da57d55SToomas Soome static
fileBtreeCursor(Btree * pBt,int iTable,int wrFlag,BtCursor ** ppCur)10407c478bd9Sstevel@tonic-gate int fileBtreeCursor(Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur){
10417c478bd9Sstevel@tonic-gate   int rc;
10427c478bd9Sstevel@tonic-gate   BtCursor *pCur, *pRing;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate   if( pBt->readOnly && wrFlag ){
10457c478bd9Sstevel@tonic-gate     *ppCur = 0;
10467c478bd9Sstevel@tonic-gate     return SQLITE_READONLY;
10477c478bd9Sstevel@tonic-gate   }
10487c478bd9Sstevel@tonic-gate   if( pBt->page1==0 ){
10497c478bd9Sstevel@tonic-gate     rc = lockBtree(pBt);
10507c478bd9Sstevel@tonic-gate     if( rc!=SQLITE_OK ){
10517c478bd9Sstevel@tonic-gate       *ppCur = 0;
10527c478bd9Sstevel@tonic-gate       return rc;
10537c478bd9Sstevel@tonic-gate     }
10547c478bd9Sstevel@tonic-gate   }
10557c478bd9Sstevel@tonic-gate   pCur = sqliteMalloc( sizeof(*pCur) );
10567c478bd9Sstevel@tonic-gate   if( pCur==0 ){
10577c478bd9Sstevel@tonic-gate     rc = SQLITE_NOMEM;
10587c478bd9Sstevel@tonic-gate     goto create_cursor_exception;
10597c478bd9Sstevel@tonic-gate   }
10607c478bd9Sstevel@tonic-gate   pCur->pgnoRoot = (Pgno)iTable;
10617c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage);
10627c478bd9Sstevel@tonic-gate   if( rc!=SQLITE_OK ){
10637c478bd9Sstevel@tonic-gate     goto create_cursor_exception;
10647c478bd9Sstevel@tonic-gate   }
10657c478bd9Sstevel@tonic-gate   rc = initPage(pBt, pCur->pPage, pCur->pgnoRoot, 0);
10667c478bd9Sstevel@tonic-gate   if( rc!=SQLITE_OK ){
10677c478bd9Sstevel@tonic-gate     goto create_cursor_exception;
10687c478bd9Sstevel@tonic-gate   }
10697c478bd9Sstevel@tonic-gate   pCur->pOps = &sqliteBtreeCursorOps;
10707c478bd9Sstevel@tonic-gate   pCur->pBt = pBt;
10717c478bd9Sstevel@tonic-gate   pCur->wrFlag = wrFlag;
10727c478bd9Sstevel@tonic-gate   pCur->idx = 0;
10737c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_INVALID;
10747c478bd9Sstevel@tonic-gate   pCur->pNext = pBt->pCursor;
10757c478bd9Sstevel@tonic-gate   if( pCur->pNext ){
10767c478bd9Sstevel@tonic-gate     pCur->pNext->pPrev = pCur;
10777c478bd9Sstevel@tonic-gate   }
10787c478bd9Sstevel@tonic-gate   pCur->pPrev = 0;
10797c478bd9Sstevel@tonic-gate   pRing = pBt->pCursor;
10807c478bd9Sstevel@tonic-gate   while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; }
10817c478bd9Sstevel@tonic-gate   if( pRing ){
10827c478bd9Sstevel@tonic-gate     pCur->pShared = pRing->pShared;
10837c478bd9Sstevel@tonic-gate     pRing->pShared = pCur;
10847c478bd9Sstevel@tonic-gate   }else{
10857c478bd9Sstevel@tonic-gate     pCur->pShared = pCur;
10867c478bd9Sstevel@tonic-gate   }
10877c478bd9Sstevel@tonic-gate   pBt->pCursor = pCur;
10887c478bd9Sstevel@tonic-gate   *ppCur = pCur;
10897c478bd9Sstevel@tonic-gate   return SQLITE_OK;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate create_cursor_exception:
10927c478bd9Sstevel@tonic-gate   *ppCur = 0;
10937c478bd9Sstevel@tonic-gate   if( pCur ){
10947c478bd9Sstevel@tonic-gate     if( pCur->pPage ) sqlitepager_unref(pCur->pPage);
10957c478bd9Sstevel@tonic-gate     sqliteFree(pCur);
10967c478bd9Sstevel@tonic-gate   }
10977c478bd9Sstevel@tonic-gate   unlockBtreeIfUnused(pBt);
10987c478bd9Sstevel@tonic-gate   return rc;
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate ** Close a cursor.  The read lock on the database file is released
11037c478bd9Sstevel@tonic-gate ** when the last cursor is closed.
11047c478bd9Sstevel@tonic-gate */
fileBtreeCloseCursor(BtCursor * pCur)11057c478bd9Sstevel@tonic-gate static int fileBtreeCloseCursor(BtCursor *pCur){
11067c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
11077c478bd9Sstevel@tonic-gate   if( pCur->pPrev ){
11087c478bd9Sstevel@tonic-gate     pCur->pPrev->pNext = pCur->pNext;
11097c478bd9Sstevel@tonic-gate   }else{
11107c478bd9Sstevel@tonic-gate     pBt->pCursor = pCur->pNext;
11117c478bd9Sstevel@tonic-gate   }
11127c478bd9Sstevel@tonic-gate   if( pCur->pNext ){
11137c478bd9Sstevel@tonic-gate     pCur->pNext->pPrev = pCur->pPrev;
11147c478bd9Sstevel@tonic-gate   }
11157c478bd9Sstevel@tonic-gate   if( pCur->pPage ){
11167c478bd9Sstevel@tonic-gate     sqlitepager_unref(pCur->pPage);
11177c478bd9Sstevel@tonic-gate   }
11187c478bd9Sstevel@tonic-gate   if( pCur->pShared!=pCur ){
11197c478bd9Sstevel@tonic-gate     BtCursor *pRing = pCur->pShared;
11207c478bd9Sstevel@tonic-gate     while( pRing->pShared!=pCur ){ pRing = pRing->pShared; }
11217c478bd9Sstevel@tonic-gate     pRing->pShared = pCur->pShared;
11227c478bd9Sstevel@tonic-gate   }
11237c478bd9Sstevel@tonic-gate   unlockBtreeIfUnused(pBt);
11247c478bd9Sstevel@tonic-gate   sqliteFree(pCur);
11257c478bd9Sstevel@tonic-gate   return SQLITE_OK;
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate /*
11297c478bd9Sstevel@tonic-gate ** Make a temporary cursor by filling in the fields of pTempCur.
11307c478bd9Sstevel@tonic-gate ** The temporary cursor is not on the cursor list for the Btree.
11317c478bd9Sstevel@tonic-gate */
getTempCursor(BtCursor * pCur,BtCursor * pTempCur)11327c478bd9Sstevel@tonic-gate static void getTempCursor(BtCursor *pCur, BtCursor *pTempCur){
11337c478bd9Sstevel@tonic-gate   memcpy(pTempCur, pCur, sizeof(*pCur));
11347c478bd9Sstevel@tonic-gate   pTempCur->pNext = 0;
11357c478bd9Sstevel@tonic-gate   pTempCur->pPrev = 0;
11367c478bd9Sstevel@tonic-gate   if( pTempCur->pPage ){
11377c478bd9Sstevel@tonic-gate     sqlitepager_ref(pTempCur->pPage);
11387c478bd9Sstevel@tonic-gate   }
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate /*
11427c478bd9Sstevel@tonic-gate ** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
11437c478bd9Sstevel@tonic-gate ** function above.
11447c478bd9Sstevel@tonic-gate */
releaseTempCursor(BtCursor * pCur)11457c478bd9Sstevel@tonic-gate static void releaseTempCursor(BtCursor *pCur){
11467c478bd9Sstevel@tonic-gate   if( pCur->pPage ){
11477c478bd9Sstevel@tonic-gate     sqlitepager_unref(pCur->pPage);
11487c478bd9Sstevel@tonic-gate   }
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate ** Set *pSize to the number of bytes of key in the entry the
11537c478bd9Sstevel@tonic-gate ** cursor currently points to.  Always return SQLITE_OK.
11547c478bd9Sstevel@tonic-gate ** Failure is not possible.  If the cursor is not currently
11557c478bd9Sstevel@tonic-gate ** pointing to an entry (which can happen, for example, if
11567c478bd9Sstevel@tonic-gate ** the database is empty) then *pSize is set to 0.
11577c478bd9Sstevel@tonic-gate */
fileBtreeKeySize(BtCursor * pCur,int * pSize)11587c478bd9Sstevel@tonic-gate static int fileBtreeKeySize(BtCursor *pCur, int *pSize){
11597c478bd9Sstevel@tonic-gate   Cell *pCell;
11607c478bd9Sstevel@tonic-gate   MemPage *pPage;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
11637c478bd9Sstevel@tonic-gate   assert( pPage!=0 );
11647c478bd9Sstevel@tonic-gate   if( pCur->idx >= pPage->nCell ){
11657c478bd9Sstevel@tonic-gate     *pSize = 0;
11667c478bd9Sstevel@tonic-gate   }else{
11677c478bd9Sstevel@tonic-gate     pCell = pPage->apCell[pCur->idx];
11687c478bd9Sstevel@tonic-gate     *pSize = NKEY(pCur->pBt, pCell->h);
11697c478bd9Sstevel@tonic-gate   }
11707c478bd9Sstevel@tonic-gate   return SQLITE_OK;
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate /*
11747c478bd9Sstevel@tonic-gate ** Read payload information from the entry that the pCur cursor is
11757c478bd9Sstevel@tonic-gate ** pointing to.  Begin reading the payload at "offset" and read
11767c478bd9Sstevel@tonic-gate ** a total of "amt" bytes.  Put the result in zBuf.
11777c478bd9Sstevel@tonic-gate **
11787c478bd9Sstevel@tonic-gate ** This routine does not make a distinction between key and data.
11797c478bd9Sstevel@tonic-gate ** It just reads bytes from the payload area.
11807c478bd9Sstevel@tonic-gate */
getPayload(BtCursor * pCur,int offset,int amt,char * zBuf)11817c478bd9Sstevel@tonic-gate static int getPayload(BtCursor *pCur, int offset, int amt, char *zBuf){
11827c478bd9Sstevel@tonic-gate   char *aPayload;
11837c478bd9Sstevel@tonic-gate   Pgno nextPage;
11847c478bd9Sstevel@tonic-gate   int rc;
11857c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
11867c478bd9Sstevel@tonic-gate   assert( pCur!=0 && pCur->pPage!=0 );
11877c478bd9Sstevel@tonic-gate   assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
11887c478bd9Sstevel@tonic-gate   aPayload = pCur->pPage->apCell[pCur->idx]->aPayload;
11897c478bd9Sstevel@tonic-gate   if( offset<MX_LOCAL_PAYLOAD ){
11907c478bd9Sstevel@tonic-gate     int a = amt;
11917c478bd9Sstevel@tonic-gate     if( a+offset>MX_LOCAL_PAYLOAD ){
11927c478bd9Sstevel@tonic-gate       a = MX_LOCAL_PAYLOAD - offset;
11937c478bd9Sstevel@tonic-gate     }
11947c478bd9Sstevel@tonic-gate     memcpy(zBuf, &aPayload[offset], a);
11957c478bd9Sstevel@tonic-gate     if( a==amt ){
11967c478bd9Sstevel@tonic-gate       return SQLITE_OK;
11977c478bd9Sstevel@tonic-gate     }
11987c478bd9Sstevel@tonic-gate     offset = 0;
11997c478bd9Sstevel@tonic-gate     zBuf += a;
12007c478bd9Sstevel@tonic-gate     amt -= a;
12017c478bd9Sstevel@tonic-gate   }else{
12027c478bd9Sstevel@tonic-gate     offset -= MX_LOCAL_PAYLOAD;
12037c478bd9Sstevel@tonic-gate   }
12047c478bd9Sstevel@tonic-gate   if( amt>0 ){
12057c478bd9Sstevel@tonic-gate     nextPage = SWAB32(pBt, pCur->pPage->apCell[pCur->idx]->ovfl);
12067c478bd9Sstevel@tonic-gate   }
12077c478bd9Sstevel@tonic-gate   while( amt>0 && nextPage ){
12087c478bd9Sstevel@tonic-gate     OverflowPage *pOvfl;
12097c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
12107c478bd9Sstevel@tonic-gate     if( rc!=0 ){
12117c478bd9Sstevel@tonic-gate       return rc;
12127c478bd9Sstevel@tonic-gate     }
12137c478bd9Sstevel@tonic-gate     nextPage = SWAB32(pBt, pOvfl->iNext);
12147c478bd9Sstevel@tonic-gate     if( offset<OVERFLOW_SIZE ){
12157c478bd9Sstevel@tonic-gate       int a = amt;
12167c478bd9Sstevel@tonic-gate       if( a + offset > OVERFLOW_SIZE ){
12177c478bd9Sstevel@tonic-gate         a = OVERFLOW_SIZE - offset;
12187c478bd9Sstevel@tonic-gate       }
12197c478bd9Sstevel@tonic-gate       memcpy(zBuf, &pOvfl->aPayload[offset], a);
12207c478bd9Sstevel@tonic-gate       offset = 0;
12217c478bd9Sstevel@tonic-gate       amt -= a;
12227c478bd9Sstevel@tonic-gate       zBuf += a;
12237c478bd9Sstevel@tonic-gate     }else{
12247c478bd9Sstevel@tonic-gate       offset -= OVERFLOW_SIZE;
12257c478bd9Sstevel@tonic-gate     }
12267c478bd9Sstevel@tonic-gate     sqlitepager_unref(pOvfl);
12277c478bd9Sstevel@tonic-gate   }
12287c478bd9Sstevel@tonic-gate   if( amt>0 ){
12297c478bd9Sstevel@tonic-gate     return SQLITE_CORRUPT;
12307c478bd9Sstevel@tonic-gate   }
12317c478bd9Sstevel@tonic-gate   return SQLITE_OK;
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate ** Read part of the key associated with cursor pCur.  A maximum
12367c478bd9Sstevel@tonic-gate ** of "amt" bytes will be transfered into zBuf[].  The transfer
12377c478bd9Sstevel@tonic-gate ** begins at "offset".  The number of bytes actually read is
1238*1da57d55SToomas Soome ** returned.
12397c478bd9Sstevel@tonic-gate **
12407c478bd9Sstevel@tonic-gate ** Change:  It used to be that the amount returned will be smaller
12417c478bd9Sstevel@tonic-gate ** than the amount requested if there are not enough bytes in the key
12427c478bd9Sstevel@tonic-gate ** to satisfy the request.  But now, it must be the case that there
12437c478bd9Sstevel@tonic-gate ** is enough data available to satisfy the request.  If not, an exception
12447c478bd9Sstevel@tonic-gate ** is raised.  The change was made in an effort to boost performance
12457c478bd9Sstevel@tonic-gate ** by eliminating unneeded tests.
12467c478bd9Sstevel@tonic-gate */
fileBtreeKey(BtCursor * pCur,int offset,int amt,char * zBuf)12477c478bd9Sstevel@tonic-gate static int fileBtreeKey(BtCursor *pCur, int offset, int amt, char *zBuf){
12487c478bd9Sstevel@tonic-gate   MemPage *pPage;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate   assert( amt>=0 );
12517c478bd9Sstevel@tonic-gate   assert( offset>=0 );
12527c478bd9Sstevel@tonic-gate   assert( pCur->pPage!=0 );
12537c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
12547c478bd9Sstevel@tonic-gate   if( pCur->idx >= pPage->nCell ){
12557c478bd9Sstevel@tonic-gate     return 0;
12567c478bd9Sstevel@tonic-gate   }
12577c478bd9Sstevel@tonic-gate   assert( amt+offset <= NKEY(pCur->pBt, pPage->apCell[pCur->idx]->h) );
12587c478bd9Sstevel@tonic-gate   getPayload(pCur, offset, amt, zBuf);
12597c478bd9Sstevel@tonic-gate   return amt;
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate /*
12637c478bd9Sstevel@tonic-gate ** Set *pSize to the number of bytes of data in the entry the
12647c478bd9Sstevel@tonic-gate ** cursor currently points to.  Always return SQLITE_OK.
12657c478bd9Sstevel@tonic-gate ** Failure is not possible.  If the cursor is not currently
12667c478bd9Sstevel@tonic-gate ** pointing to an entry (which can happen, for example, if
12677c478bd9Sstevel@tonic-gate ** the database is empty) then *pSize is set to 0.
12687c478bd9Sstevel@tonic-gate */
fileBtreeDataSize(BtCursor * pCur,int * pSize)12697c478bd9Sstevel@tonic-gate static int fileBtreeDataSize(BtCursor *pCur, int *pSize){
12707c478bd9Sstevel@tonic-gate   Cell *pCell;
12717c478bd9Sstevel@tonic-gate   MemPage *pPage;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
12747c478bd9Sstevel@tonic-gate   assert( pPage!=0 );
12757c478bd9Sstevel@tonic-gate   if( pCur->idx >= pPage->nCell ){
12767c478bd9Sstevel@tonic-gate     *pSize = 0;
12777c478bd9Sstevel@tonic-gate   }else{
12787c478bd9Sstevel@tonic-gate     pCell = pPage->apCell[pCur->idx];
12797c478bd9Sstevel@tonic-gate     *pSize = NDATA(pCur->pBt, pCell->h);
12807c478bd9Sstevel@tonic-gate   }
12817c478bd9Sstevel@tonic-gate   return SQLITE_OK;
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate ** Read part of the data associated with cursor pCur.  A maximum
12867c478bd9Sstevel@tonic-gate ** of "amt" bytes will be transfered into zBuf[].  The transfer
12877c478bd9Sstevel@tonic-gate ** begins at "offset".  The number of bytes actually read is
12887c478bd9Sstevel@tonic-gate ** returned.  The amount returned will be smaller than the
12897c478bd9Sstevel@tonic-gate ** amount requested if there are not enough bytes in the data
12907c478bd9Sstevel@tonic-gate ** to satisfy the request.
12917c478bd9Sstevel@tonic-gate */
fileBtreeData(BtCursor * pCur,int offset,int amt,char * zBuf)12927c478bd9Sstevel@tonic-gate static int fileBtreeData(BtCursor *pCur, int offset, int amt, char *zBuf){
12937c478bd9Sstevel@tonic-gate   Cell *pCell;
12947c478bd9Sstevel@tonic-gate   MemPage *pPage;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate   assert( amt>=0 );
12977c478bd9Sstevel@tonic-gate   assert( offset>=0 );
12987c478bd9Sstevel@tonic-gate   assert( pCur->pPage!=0 );
12997c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
13007c478bd9Sstevel@tonic-gate   if( pCur->idx >= pPage->nCell ){
13017c478bd9Sstevel@tonic-gate     return 0;
13027c478bd9Sstevel@tonic-gate   }
13037c478bd9Sstevel@tonic-gate   pCell = pPage->apCell[pCur->idx];
13047c478bd9Sstevel@tonic-gate   assert( amt+offset <= NDATA(pCur->pBt, pCell->h) );
13057c478bd9Sstevel@tonic-gate   getPayload(pCur, offset + NKEY(pCur->pBt, pCell->h), amt, zBuf);
13067c478bd9Sstevel@tonic-gate   return amt;
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate /*
13107c478bd9Sstevel@tonic-gate ** Compare an external key against the key on the entry that pCur points to.
13117c478bd9Sstevel@tonic-gate **
13127c478bd9Sstevel@tonic-gate ** The external key is pKey and is nKey bytes long.  The last nIgnore bytes
13137c478bd9Sstevel@tonic-gate ** of the key associated with pCur are ignored, as if they do not exist.
13147c478bd9Sstevel@tonic-gate ** (The normal case is for nIgnore to be zero in which case the entire
13157c478bd9Sstevel@tonic-gate ** internal key is used in the comparison.)
13167c478bd9Sstevel@tonic-gate **
13177c478bd9Sstevel@tonic-gate ** The comparison result is written to *pRes as follows:
13187c478bd9Sstevel@tonic-gate **
13197c478bd9Sstevel@tonic-gate **    *pRes<0    This means pCur<pKey
13207c478bd9Sstevel@tonic-gate **
13217c478bd9Sstevel@tonic-gate **    *pRes==0   This means pCur==pKey for all nKey bytes
13227c478bd9Sstevel@tonic-gate **
13237c478bd9Sstevel@tonic-gate **    *pRes>0    This means pCur>pKey
13247c478bd9Sstevel@tonic-gate **
13257c478bd9Sstevel@tonic-gate ** When one key is an exact prefix of the other, the shorter key is
13267c478bd9Sstevel@tonic-gate ** considered less than the longer one.  In order to be equal the
13277c478bd9Sstevel@tonic-gate ** keys must be exactly the same length. (The length of the pCur key
13287c478bd9Sstevel@tonic-gate ** is the actual key length minus nIgnore bytes.)
13297c478bd9Sstevel@tonic-gate */
fileBtreeKeyCompare(BtCursor * pCur,const void * pKey,int nKey,int nIgnore,int * pResult)13307c478bd9Sstevel@tonic-gate static int fileBtreeKeyCompare(
13317c478bd9Sstevel@tonic-gate   BtCursor *pCur,       /* Pointer to entry to compare against */
13327c478bd9Sstevel@tonic-gate   const void *pKey,     /* Key to compare against entry that pCur points to */
13337c478bd9Sstevel@tonic-gate   int nKey,             /* Number of bytes in pKey */
13347c478bd9Sstevel@tonic-gate   int nIgnore,          /* Ignore this many bytes at the end of pCur */
13357c478bd9Sstevel@tonic-gate   int *pResult          /* Write the result here */
13367c478bd9Sstevel@tonic-gate ){
13377c478bd9Sstevel@tonic-gate   Pgno nextPage;
13387c478bd9Sstevel@tonic-gate   int n, c, rc, nLocal;
13397c478bd9Sstevel@tonic-gate   Cell *pCell;
13407c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
13417c478bd9Sstevel@tonic-gate   const char *zKey  = (const char*)pKey;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate   assert( pCur->pPage );
13447c478bd9Sstevel@tonic-gate   assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
13457c478bd9Sstevel@tonic-gate   pCell = pCur->pPage->apCell[pCur->idx];
13467c478bd9Sstevel@tonic-gate   nLocal = NKEY(pBt, pCell->h) - nIgnore;
13477c478bd9Sstevel@tonic-gate   if( nLocal<0 ) nLocal = 0;
13487c478bd9Sstevel@tonic-gate   n = nKey<nLocal ? nKey : nLocal;
13497c478bd9Sstevel@tonic-gate   if( n>MX_LOCAL_PAYLOAD ){
13507c478bd9Sstevel@tonic-gate     n = MX_LOCAL_PAYLOAD;
13517c478bd9Sstevel@tonic-gate   }
13527c478bd9Sstevel@tonic-gate   c = memcmp(pCell->aPayload, zKey, n);
13537c478bd9Sstevel@tonic-gate   if( c!=0 ){
13547c478bd9Sstevel@tonic-gate     *pResult = c;
13557c478bd9Sstevel@tonic-gate     return SQLITE_OK;
13567c478bd9Sstevel@tonic-gate   }
13577c478bd9Sstevel@tonic-gate   zKey += n;
13587c478bd9Sstevel@tonic-gate   nKey -= n;
13597c478bd9Sstevel@tonic-gate   nLocal -= n;
13607c478bd9Sstevel@tonic-gate   nextPage = SWAB32(pBt, pCell->ovfl);
13617c478bd9Sstevel@tonic-gate   while( nKey>0 && nLocal>0 ){
13627c478bd9Sstevel@tonic-gate     OverflowPage *pOvfl;
13637c478bd9Sstevel@tonic-gate     if( nextPage==0 ){
13647c478bd9Sstevel@tonic-gate       return SQLITE_CORRUPT;
13657c478bd9Sstevel@tonic-gate     }
13667c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
13677c478bd9Sstevel@tonic-gate     if( rc ){
13687c478bd9Sstevel@tonic-gate       return rc;
13697c478bd9Sstevel@tonic-gate     }
13707c478bd9Sstevel@tonic-gate     nextPage = SWAB32(pBt, pOvfl->iNext);
13717c478bd9Sstevel@tonic-gate     n = nKey<nLocal ? nKey : nLocal;
13727c478bd9Sstevel@tonic-gate     if( n>OVERFLOW_SIZE ){
13737c478bd9Sstevel@tonic-gate       n = OVERFLOW_SIZE;
13747c478bd9Sstevel@tonic-gate     }
13757c478bd9Sstevel@tonic-gate     c = memcmp(pOvfl->aPayload, zKey, n);
13767c478bd9Sstevel@tonic-gate     sqlitepager_unref(pOvfl);
13777c478bd9Sstevel@tonic-gate     if( c!=0 ){
13787c478bd9Sstevel@tonic-gate       *pResult = c;
13797c478bd9Sstevel@tonic-gate       return SQLITE_OK;
13807c478bd9Sstevel@tonic-gate     }
13817c478bd9Sstevel@tonic-gate     nKey -= n;
13827c478bd9Sstevel@tonic-gate     nLocal -= n;
13837c478bd9Sstevel@tonic-gate     zKey += n;
13847c478bd9Sstevel@tonic-gate   }
13857c478bd9Sstevel@tonic-gate   if( c==0 ){
13867c478bd9Sstevel@tonic-gate     c = nLocal - nKey;
13877c478bd9Sstevel@tonic-gate   }
13887c478bd9Sstevel@tonic-gate   *pResult = c;
13897c478bd9Sstevel@tonic-gate   return SQLITE_OK;
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate /*
13937c478bd9Sstevel@tonic-gate ** Move the cursor down to a new child page.  The newPgno argument is the
13947c478bd9Sstevel@tonic-gate ** page number of the child page in the byte order of the disk image.
13957c478bd9Sstevel@tonic-gate */
moveToChild(BtCursor * pCur,int newPgno)13967c478bd9Sstevel@tonic-gate static int moveToChild(BtCursor *pCur, int newPgno){
13977c478bd9Sstevel@tonic-gate   int rc;
13987c478bd9Sstevel@tonic-gate   MemPage *pNewPage;
13997c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate   newPgno = SWAB32(pBt, newPgno);
14027c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, newPgno, (void**)&pNewPage);
14037c478bd9Sstevel@tonic-gate   if( rc ) return rc;
14047c478bd9Sstevel@tonic-gate   rc = initPage(pBt, pNewPage, newPgno, pCur->pPage);
14057c478bd9Sstevel@tonic-gate   if( rc ) return rc;
14067c478bd9Sstevel@tonic-gate   assert( pCur->idx>=pCur->pPage->nCell
14077c478bd9Sstevel@tonic-gate           || pCur->pPage->apCell[pCur->idx]->h.leftChild==SWAB32(pBt,newPgno) );
14087c478bd9Sstevel@tonic-gate   assert( pCur->idx<pCur->pPage->nCell
14097c478bd9Sstevel@tonic-gate           || pCur->pPage->u.hdr.rightChild==SWAB32(pBt,newPgno) );
14107c478bd9Sstevel@tonic-gate   pNewPage->idxParent = pCur->idx;
14117c478bd9Sstevel@tonic-gate   pCur->pPage->idxShift = 0;
14127c478bd9Sstevel@tonic-gate   sqlitepager_unref(pCur->pPage);
14137c478bd9Sstevel@tonic-gate   pCur->pPage = pNewPage;
14147c478bd9Sstevel@tonic-gate   pCur->idx = 0;
14157c478bd9Sstevel@tonic-gate   if( pNewPage->nCell<1 ){
14167c478bd9Sstevel@tonic-gate     return SQLITE_CORRUPT;
14177c478bd9Sstevel@tonic-gate   }
14187c478bd9Sstevel@tonic-gate   return SQLITE_OK;
14197c478bd9Sstevel@tonic-gate }
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate /*
14227c478bd9Sstevel@tonic-gate ** Move the cursor up to the parent page.
14237c478bd9Sstevel@tonic-gate **
14247c478bd9Sstevel@tonic-gate ** pCur->idx is set to the cell index that contains the pointer
14257c478bd9Sstevel@tonic-gate ** to the page we are coming from.  If we are coming from the
14267c478bd9Sstevel@tonic-gate ** right-most child page then pCur->idx is set to one more than
14277c478bd9Sstevel@tonic-gate ** the largest cell index.
14287c478bd9Sstevel@tonic-gate */
moveToParent(BtCursor * pCur)14297c478bd9Sstevel@tonic-gate static void moveToParent(BtCursor *pCur){
14307c478bd9Sstevel@tonic-gate   Pgno oldPgno;
14317c478bd9Sstevel@tonic-gate   MemPage *pParent;
14327c478bd9Sstevel@tonic-gate   MemPage *pPage;
14337c478bd9Sstevel@tonic-gate   int idxParent;
14347c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
14357c478bd9Sstevel@tonic-gate   assert( pPage!=0 );
14367c478bd9Sstevel@tonic-gate   pParent = pPage->pParent;
14377c478bd9Sstevel@tonic-gate   assert( pParent!=0 );
14387c478bd9Sstevel@tonic-gate   idxParent = pPage->idxParent;
14397c478bd9Sstevel@tonic-gate   sqlitepager_ref(pParent);
14407c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
14417c478bd9Sstevel@tonic-gate   pCur->pPage = pParent;
14427c478bd9Sstevel@tonic-gate   assert( pParent->idxShift==0 );
14437c478bd9Sstevel@tonic-gate   if( pParent->idxShift==0 ){
14447c478bd9Sstevel@tonic-gate     pCur->idx = idxParent;
1445*1da57d55SToomas Soome #ifndef NDEBUG
14467c478bd9Sstevel@tonic-gate     /* Verify that pCur->idx is the correct index to point back to the child
1447*1da57d55SToomas Soome     ** page we just came from
14487c478bd9Sstevel@tonic-gate     */
14497c478bd9Sstevel@tonic-gate     oldPgno = SWAB32(pCur->pBt, sqlitepager_pagenumber(pPage));
14507c478bd9Sstevel@tonic-gate     if( pCur->idx<pParent->nCell ){
14517c478bd9Sstevel@tonic-gate       assert( pParent->apCell[idxParent]->h.leftChild==oldPgno );
14527c478bd9Sstevel@tonic-gate     }else{
14537c478bd9Sstevel@tonic-gate       assert( pParent->u.hdr.rightChild==oldPgno );
14547c478bd9Sstevel@tonic-gate     }
14557c478bd9Sstevel@tonic-gate #endif
14567c478bd9Sstevel@tonic-gate   }else{
1457*1da57d55SToomas Soome     /* The MemPage.idxShift flag indicates that cell indices might have
14587c478bd9Sstevel@tonic-gate     ** changed since idxParent was set and hence idxParent might be out
14597c478bd9Sstevel@tonic-gate     ** of date.  So recompute the parent cell index by scanning all cells
14607c478bd9Sstevel@tonic-gate     ** and locating the one that points to the child we just came from.
14617c478bd9Sstevel@tonic-gate     */
14627c478bd9Sstevel@tonic-gate     int i;
14637c478bd9Sstevel@tonic-gate     pCur->idx = pParent->nCell;
14647c478bd9Sstevel@tonic-gate     oldPgno = SWAB32(pCur->pBt, sqlitepager_pagenumber(pPage));
14657c478bd9Sstevel@tonic-gate     for(i=0; i<pParent->nCell; i++){
14667c478bd9Sstevel@tonic-gate       if( pParent->apCell[i]->h.leftChild==oldPgno ){
14677c478bd9Sstevel@tonic-gate         pCur->idx = i;
14687c478bd9Sstevel@tonic-gate         break;
14697c478bd9Sstevel@tonic-gate       }
14707c478bd9Sstevel@tonic-gate     }
14717c478bd9Sstevel@tonic-gate   }
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate ** Move the cursor to the root page
14767c478bd9Sstevel@tonic-gate */
moveToRoot(BtCursor * pCur)14777c478bd9Sstevel@tonic-gate static int moveToRoot(BtCursor *pCur){
14787c478bd9Sstevel@tonic-gate   MemPage *pNew;
14797c478bd9Sstevel@tonic-gate   int rc;
14807c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pNew);
14837c478bd9Sstevel@tonic-gate   if( rc ) return rc;
14847c478bd9Sstevel@tonic-gate   rc = initPage(pBt, pNew, pCur->pgnoRoot, 0);
14857c478bd9Sstevel@tonic-gate   if( rc ) return rc;
14867c478bd9Sstevel@tonic-gate   sqlitepager_unref(pCur->pPage);
14877c478bd9Sstevel@tonic-gate   pCur->pPage = pNew;
14887c478bd9Sstevel@tonic-gate   pCur->idx = 0;
14897c478bd9Sstevel@tonic-gate   return SQLITE_OK;
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate ** Move the cursor down to the left-most leaf entry beneath the
14947c478bd9Sstevel@tonic-gate ** entry to which it is currently pointing.
14957c478bd9Sstevel@tonic-gate */
moveToLeftmost(BtCursor * pCur)14967c478bd9Sstevel@tonic-gate static int moveToLeftmost(BtCursor *pCur){
14977c478bd9Sstevel@tonic-gate   Pgno pgno;
14987c478bd9Sstevel@tonic-gate   int rc;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate   while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
15017c478bd9Sstevel@tonic-gate     rc = moveToChild(pCur, pgno);
15027c478bd9Sstevel@tonic-gate     if( rc ) return rc;
15037c478bd9Sstevel@tonic-gate   }
15047c478bd9Sstevel@tonic-gate   return SQLITE_OK;
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate ** Move the cursor down to the right-most leaf entry beneath the
15097c478bd9Sstevel@tonic-gate ** page to which it is currently pointing.  Notice the difference
15107c478bd9Sstevel@tonic-gate ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
15117c478bd9Sstevel@tonic-gate ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
15127c478bd9Sstevel@tonic-gate ** finds the right-most entry beneath the *page*.
15137c478bd9Sstevel@tonic-gate */
moveToRightmost(BtCursor * pCur)15147c478bd9Sstevel@tonic-gate static int moveToRightmost(BtCursor *pCur){
15157c478bd9Sstevel@tonic-gate   Pgno pgno;
15167c478bd9Sstevel@tonic-gate   int rc;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate   while( (pgno = pCur->pPage->u.hdr.rightChild)!=0 ){
15197c478bd9Sstevel@tonic-gate     pCur->idx = pCur->pPage->nCell;
15207c478bd9Sstevel@tonic-gate     rc = moveToChild(pCur, pgno);
15217c478bd9Sstevel@tonic-gate     if( rc ) return rc;
15227c478bd9Sstevel@tonic-gate   }
15237c478bd9Sstevel@tonic-gate   pCur->idx = pCur->pPage->nCell - 1;
15247c478bd9Sstevel@tonic-gate   return SQLITE_OK;
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /* Move the cursor to the first entry in the table.  Return SQLITE_OK
15287c478bd9Sstevel@tonic-gate ** on success.  Set *pRes to 0 if the cursor actually points to something
15297c478bd9Sstevel@tonic-gate ** or set *pRes to 1 if the table is empty.
15307c478bd9Sstevel@tonic-gate */
fileBtreeFirst(BtCursor * pCur,int * pRes)15317c478bd9Sstevel@tonic-gate static int fileBtreeFirst(BtCursor *pCur, int *pRes){
15327c478bd9Sstevel@tonic-gate   int rc;
15337c478bd9Sstevel@tonic-gate   if( pCur->pPage==0 ) return SQLITE_ABORT;
15347c478bd9Sstevel@tonic-gate   rc = moveToRoot(pCur);
15357c478bd9Sstevel@tonic-gate   if( rc ) return rc;
15367c478bd9Sstevel@tonic-gate   if( pCur->pPage->nCell==0 ){
15377c478bd9Sstevel@tonic-gate     *pRes = 1;
15387c478bd9Sstevel@tonic-gate     return SQLITE_OK;
15397c478bd9Sstevel@tonic-gate   }
15407c478bd9Sstevel@tonic-gate   *pRes = 0;
15417c478bd9Sstevel@tonic-gate   rc = moveToLeftmost(pCur);
15427c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_NONE;
15437c478bd9Sstevel@tonic-gate   return rc;
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /* Move the cursor to the last entry in the table.  Return SQLITE_OK
15477c478bd9Sstevel@tonic-gate ** on success.  Set *pRes to 0 if the cursor actually points to something
15487c478bd9Sstevel@tonic-gate ** or set *pRes to 1 if the table is empty.
15497c478bd9Sstevel@tonic-gate */
fileBtreeLast(BtCursor * pCur,int * pRes)15507c478bd9Sstevel@tonic-gate static int fileBtreeLast(BtCursor *pCur, int *pRes){
15517c478bd9Sstevel@tonic-gate   int rc;
15527c478bd9Sstevel@tonic-gate   if( pCur->pPage==0 ) return SQLITE_ABORT;
15537c478bd9Sstevel@tonic-gate   rc = moveToRoot(pCur);
15547c478bd9Sstevel@tonic-gate   if( rc ) return rc;
15557c478bd9Sstevel@tonic-gate   assert( pCur->pPage->isInit );
15567c478bd9Sstevel@tonic-gate   if( pCur->pPage->nCell==0 ){
15577c478bd9Sstevel@tonic-gate     *pRes = 1;
15587c478bd9Sstevel@tonic-gate     return SQLITE_OK;
15597c478bd9Sstevel@tonic-gate   }
15607c478bd9Sstevel@tonic-gate   *pRes = 0;
15617c478bd9Sstevel@tonic-gate   rc = moveToRightmost(pCur);
15627c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_NONE;
15637c478bd9Sstevel@tonic-gate   return rc;
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate /* Move the cursor so that it points to an entry near pKey.
15677c478bd9Sstevel@tonic-gate ** Return a success code.
15687c478bd9Sstevel@tonic-gate **
15697c478bd9Sstevel@tonic-gate ** If an exact match is not found, then the cursor is always
15707c478bd9Sstevel@tonic-gate ** left pointing at a leaf page which would hold the entry if it
15717c478bd9Sstevel@tonic-gate ** were present.  The cursor might point to an entry that comes
15727c478bd9Sstevel@tonic-gate ** before or after the key.
15737c478bd9Sstevel@tonic-gate **
15747c478bd9Sstevel@tonic-gate ** The result of comparing the key with the entry to which the
15757c478bd9Sstevel@tonic-gate ** cursor is left pointing is stored in pCur->iMatch.  The same
15767c478bd9Sstevel@tonic-gate ** value is also written to *pRes if pRes!=NULL.  The meaning of
15777c478bd9Sstevel@tonic-gate ** this value is as follows:
15787c478bd9Sstevel@tonic-gate **
15797c478bd9Sstevel@tonic-gate **     *pRes<0      The cursor is left pointing at an entry that
15807c478bd9Sstevel@tonic-gate **                  is smaller than pKey or if the table is empty
15817c478bd9Sstevel@tonic-gate **                  and the cursor is therefore left point to nothing.
15827c478bd9Sstevel@tonic-gate **
15837c478bd9Sstevel@tonic-gate **     *pRes==0     The cursor is left pointing at an entry that
15847c478bd9Sstevel@tonic-gate **                  exactly matches pKey.
15857c478bd9Sstevel@tonic-gate **
15867c478bd9Sstevel@tonic-gate **     *pRes>0      The cursor is left pointing at an entry that
15877c478bd9Sstevel@tonic-gate **                  is larger than pKey.
15887c478bd9Sstevel@tonic-gate */
15897c478bd9Sstevel@tonic-gate static
fileBtreeMoveto(BtCursor * pCur,const void * pKey,int nKey,int * pRes)15907c478bd9Sstevel@tonic-gate int fileBtreeMoveto(BtCursor *pCur, const void *pKey, int nKey, int *pRes){
15917c478bd9Sstevel@tonic-gate   int rc;
15927c478bd9Sstevel@tonic-gate   if( pCur->pPage==0 ) return SQLITE_ABORT;
15937c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_NONE;
15947c478bd9Sstevel@tonic-gate   rc = moveToRoot(pCur);
15957c478bd9Sstevel@tonic-gate   if( rc ) return rc;
15967c478bd9Sstevel@tonic-gate   for(;;){
15977c478bd9Sstevel@tonic-gate     int lwr, upr;
15987c478bd9Sstevel@tonic-gate     Pgno chldPg;
15997c478bd9Sstevel@tonic-gate     MemPage *pPage = pCur->pPage;
16007c478bd9Sstevel@tonic-gate     int c = -1;  /* pRes return if table is empty must be -1 */
16017c478bd9Sstevel@tonic-gate     lwr = 0;
16027c478bd9Sstevel@tonic-gate     upr = pPage->nCell-1;
16037c478bd9Sstevel@tonic-gate     while( lwr<=upr ){
16047c478bd9Sstevel@tonic-gate       pCur->idx = (lwr+upr)/2;
16057c478bd9Sstevel@tonic-gate       rc = fileBtreeKeyCompare(pCur, pKey, nKey, 0, &c);
16067c478bd9Sstevel@tonic-gate       if( rc ) return rc;
16077c478bd9Sstevel@tonic-gate       if( c==0 ){
16087c478bd9Sstevel@tonic-gate         pCur->iMatch = c;
16097c478bd9Sstevel@tonic-gate         if( pRes ) *pRes = 0;
16107c478bd9Sstevel@tonic-gate         return SQLITE_OK;
16117c478bd9Sstevel@tonic-gate       }
16127c478bd9Sstevel@tonic-gate       if( c<0 ){
16137c478bd9Sstevel@tonic-gate         lwr = pCur->idx+1;
16147c478bd9Sstevel@tonic-gate       }else{
16157c478bd9Sstevel@tonic-gate         upr = pCur->idx-1;
16167c478bd9Sstevel@tonic-gate       }
16177c478bd9Sstevel@tonic-gate     }
16187c478bd9Sstevel@tonic-gate     assert( lwr==upr+1 );
16197c478bd9Sstevel@tonic-gate     assert( pPage->isInit );
16207c478bd9Sstevel@tonic-gate     if( lwr>=pPage->nCell ){
16217c478bd9Sstevel@tonic-gate       chldPg = pPage->u.hdr.rightChild;
16227c478bd9Sstevel@tonic-gate     }else{
16237c478bd9Sstevel@tonic-gate       chldPg = pPage->apCell[lwr]->h.leftChild;
16247c478bd9Sstevel@tonic-gate     }
16257c478bd9Sstevel@tonic-gate     if( chldPg==0 ){
16267c478bd9Sstevel@tonic-gate       pCur->iMatch = c;
16277c478bd9Sstevel@tonic-gate       if( pRes ) *pRes = c;
16287c478bd9Sstevel@tonic-gate       return SQLITE_OK;
16297c478bd9Sstevel@tonic-gate     }
16307c478bd9Sstevel@tonic-gate     pCur->idx = lwr;
16317c478bd9Sstevel@tonic-gate     rc = moveToChild(pCur, chldPg);
16327c478bd9Sstevel@tonic-gate     if( rc ) return rc;
16337c478bd9Sstevel@tonic-gate   }
16347c478bd9Sstevel@tonic-gate   /* NOT REACHED */
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate /*
16387c478bd9Sstevel@tonic-gate ** Advance the cursor to the next entry in the database.  If
16397c478bd9Sstevel@tonic-gate ** successful then set *pRes=0.  If the cursor
16407c478bd9Sstevel@tonic-gate ** was already pointing to the last entry in the database before
16417c478bd9Sstevel@tonic-gate ** this routine was called, then set *pRes=1.
16427c478bd9Sstevel@tonic-gate */
fileBtreeNext(BtCursor * pCur,int * pRes)16437c478bd9Sstevel@tonic-gate static int fileBtreeNext(BtCursor *pCur, int *pRes){
16447c478bd9Sstevel@tonic-gate   int rc;
16457c478bd9Sstevel@tonic-gate   MemPage *pPage = pCur->pPage;
16467c478bd9Sstevel@tonic-gate   assert( pRes!=0 );
16477c478bd9Sstevel@tonic-gate   if( pPage==0 ){
16487c478bd9Sstevel@tonic-gate     *pRes = 1;
16497c478bd9Sstevel@tonic-gate     return SQLITE_ABORT;
16507c478bd9Sstevel@tonic-gate   }
16517c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
16527c478bd9Sstevel@tonic-gate   assert( pCur->eSkip!=SKIP_INVALID );
16537c478bd9Sstevel@tonic-gate   if( pPage->nCell==0 ){
16547c478bd9Sstevel@tonic-gate     *pRes = 1;
16557c478bd9Sstevel@tonic-gate     return SQLITE_OK;
16567c478bd9Sstevel@tonic-gate   }
16577c478bd9Sstevel@tonic-gate   assert( pCur->idx<pPage->nCell );
16587c478bd9Sstevel@tonic-gate   if( pCur->eSkip==SKIP_NEXT ){
16597c478bd9Sstevel@tonic-gate     pCur->eSkip = SKIP_NONE;
16607c478bd9Sstevel@tonic-gate     *pRes = 0;
16617c478bd9Sstevel@tonic-gate     return SQLITE_OK;
16627c478bd9Sstevel@tonic-gate   }
16637c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_NONE;
16647c478bd9Sstevel@tonic-gate   pCur->idx++;
16657c478bd9Sstevel@tonic-gate   if( pCur->idx>=pPage->nCell ){
16667c478bd9Sstevel@tonic-gate     if( pPage->u.hdr.rightChild ){
16677c478bd9Sstevel@tonic-gate       rc = moveToChild(pCur, pPage->u.hdr.rightChild);
16687c478bd9Sstevel@tonic-gate       if( rc ) return rc;
16697c478bd9Sstevel@tonic-gate       rc = moveToLeftmost(pCur);
16707c478bd9Sstevel@tonic-gate       *pRes = 0;
16717c478bd9Sstevel@tonic-gate       return rc;
16727c478bd9Sstevel@tonic-gate     }
16737c478bd9Sstevel@tonic-gate     do{
16747c478bd9Sstevel@tonic-gate       if( pPage->pParent==0 ){
16757c478bd9Sstevel@tonic-gate         *pRes = 1;
16767c478bd9Sstevel@tonic-gate         return SQLITE_OK;
16777c478bd9Sstevel@tonic-gate       }
16787c478bd9Sstevel@tonic-gate       moveToParent(pCur);
16797c478bd9Sstevel@tonic-gate       pPage = pCur->pPage;
16807c478bd9Sstevel@tonic-gate     }while( pCur->idx>=pPage->nCell );
16817c478bd9Sstevel@tonic-gate     *pRes = 0;
16827c478bd9Sstevel@tonic-gate     return SQLITE_OK;
16837c478bd9Sstevel@tonic-gate   }
16847c478bd9Sstevel@tonic-gate   *pRes = 0;
16857c478bd9Sstevel@tonic-gate   if( pPage->u.hdr.rightChild==0 ){
16867c478bd9Sstevel@tonic-gate     return SQLITE_OK;
16877c478bd9Sstevel@tonic-gate   }
16887c478bd9Sstevel@tonic-gate   rc = moveToLeftmost(pCur);
16897c478bd9Sstevel@tonic-gate   return rc;
16907c478bd9Sstevel@tonic-gate }
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate /*
16937c478bd9Sstevel@tonic-gate ** Step the cursor to the back to the previous entry in the database.  If
16947c478bd9Sstevel@tonic-gate ** successful then set *pRes=0.  If the cursor
16957c478bd9Sstevel@tonic-gate ** was already pointing to the first entry in the database before
16967c478bd9Sstevel@tonic-gate ** this routine was called, then set *pRes=1.
16977c478bd9Sstevel@tonic-gate */
fileBtreePrevious(BtCursor * pCur,int * pRes)16987c478bd9Sstevel@tonic-gate static int fileBtreePrevious(BtCursor *pCur, int *pRes){
16997c478bd9Sstevel@tonic-gate   int rc;
17007c478bd9Sstevel@tonic-gate   Pgno pgno;
17017c478bd9Sstevel@tonic-gate   MemPage *pPage;
17027c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
17037c478bd9Sstevel@tonic-gate   if( pPage==0 ){
17047c478bd9Sstevel@tonic-gate     *pRes = 1;
17057c478bd9Sstevel@tonic-gate     return SQLITE_ABORT;
17067c478bd9Sstevel@tonic-gate   }
17077c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
17087c478bd9Sstevel@tonic-gate   assert( pCur->eSkip!=SKIP_INVALID );
17097c478bd9Sstevel@tonic-gate   if( pPage->nCell==0 ){
17107c478bd9Sstevel@tonic-gate     *pRes = 1;
17117c478bd9Sstevel@tonic-gate     return SQLITE_OK;
17127c478bd9Sstevel@tonic-gate   }
17137c478bd9Sstevel@tonic-gate   if( pCur->eSkip==SKIP_PREV ){
17147c478bd9Sstevel@tonic-gate     pCur->eSkip = SKIP_NONE;
17157c478bd9Sstevel@tonic-gate     *pRes = 0;
17167c478bd9Sstevel@tonic-gate     return SQLITE_OK;
17177c478bd9Sstevel@tonic-gate   }
17187c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_NONE;
17197c478bd9Sstevel@tonic-gate   assert( pCur->idx>=0 );
17207c478bd9Sstevel@tonic-gate   if( (pgno = pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
17217c478bd9Sstevel@tonic-gate     rc = moveToChild(pCur, pgno);
17227c478bd9Sstevel@tonic-gate     if( rc ) return rc;
17237c478bd9Sstevel@tonic-gate     rc = moveToRightmost(pCur);
17247c478bd9Sstevel@tonic-gate   }else{
17257c478bd9Sstevel@tonic-gate     while( pCur->idx==0 ){
17267c478bd9Sstevel@tonic-gate       if( pPage->pParent==0 ){
17277c478bd9Sstevel@tonic-gate         if( pRes ) *pRes = 1;
17287c478bd9Sstevel@tonic-gate         return SQLITE_OK;
17297c478bd9Sstevel@tonic-gate       }
17307c478bd9Sstevel@tonic-gate       moveToParent(pCur);
17317c478bd9Sstevel@tonic-gate       pPage = pCur->pPage;
17327c478bd9Sstevel@tonic-gate     }
17337c478bd9Sstevel@tonic-gate     pCur->idx--;
17347c478bd9Sstevel@tonic-gate     rc = SQLITE_OK;
17357c478bd9Sstevel@tonic-gate   }
17367c478bd9Sstevel@tonic-gate   *pRes = 0;
17377c478bd9Sstevel@tonic-gate   return rc;
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate ** Allocate a new page from the database file.
17427c478bd9Sstevel@tonic-gate **
17437c478bd9Sstevel@tonic-gate ** The new page is marked as dirty.  (In other words, sqlitepager_write()
17447c478bd9Sstevel@tonic-gate ** has already been called on the new page.)  The new page has also
17457c478bd9Sstevel@tonic-gate ** been referenced and the calling routine is responsible for calling
17467c478bd9Sstevel@tonic-gate ** sqlitepager_unref() on the new page when it is done.
17477c478bd9Sstevel@tonic-gate **
17487c478bd9Sstevel@tonic-gate ** SQLITE_OK is returned on success.  Any other return value indicates
17497c478bd9Sstevel@tonic-gate ** an error.  *ppPage and *pPgno are undefined in the event of an error.
17507c478bd9Sstevel@tonic-gate ** Do not invoke sqlitepager_unref() on *ppPage if an error is returned.
17517c478bd9Sstevel@tonic-gate **
1752*1da57d55SToomas Soome ** If the "nearby" parameter is not 0, then a (feeble) effort is made to
17537c478bd9Sstevel@tonic-gate ** locate a page close to the page number "nearby".  This can be used in an
17547c478bd9Sstevel@tonic-gate ** attempt to keep related pages close to each other in the database file,
17557c478bd9Sstevel@tonic-gate ** which in turn can make database access faster.
17567c478bd9Sstevel@tonic-gate */
allocatePage(Btree * pBt,MemPage ** ppPage,Pgno * pPgno,Pgno nearby)17577c478bd9Sstevel@tonic-gate static int allocatePage(Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby){
17587c478bd9Sstevel@tonic-gate   PageOne *pPage1 = pBt->page1;
17597c478bd9Sstevel@tonic-gate   int rc;
17607c478bd9Sstevel@tonic-gate   if( pPage1->freeList ){
17617c478bd9Sstevel@tonic-gate     OverflowPage *pOvfl;
17627c478bd9Sstevel@tonic-gate     FreelistInfo *pInfo;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(pPage1);
17657c478bd9Sstevel@tonic-gate     if( rc ) return rc;
17667c478bd9Sstevel@tonic-gate     SWAB_ADD(pBt, pPage1->nFree, -1);
17677c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
17687c478bd9Sstevel@tonic-gate                         (void**)&pOvfl);
17697c478bd9Sstevel@tonic-gate     if( rc ) return rc;
17707c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(pOvfl);
17717c478bd9Sstevel@tonic-gate     if( rc ){
17727c478bd9Sstevel@tonic-gate       sqlitepager_unref(pOvfl);
17737c478bd9Sstevel@tonic-gate       return rc;
17747c478bd9Sstevel@tonic-gate     }
17757c478bd9Sstevel@tonic-gate     pInfo = (FreelistInfo*)pOvfl->aPayload;
17767c478bd9Sstevel@tonic-gate     if( pInfo->nFree==0 ){
17777c478bd9Sstevel@tonic-gate       *pPgno = SWAB32(pBt, pPage1->freeList);
17787c478bd9Sstevel@tonic-gate       pPage1->freeList = pOvfl->iNext;
17797c478bd9Sstevel@tonic-gate       *ppPage = (MemPage*)pOvfl;
17807c478bd9Sstevel@tonic-gate     }else{
17817c478bd9Sstevel@tonic-gate       int closest, n;
17827c478bd9Sstevel@tonic-gate       n = SWAB32(pBt, pInfo->nFree);
17837c478bd9Sstevel@tonic-gate       if( n>1 && nearby>0 ){
17847c478bd9Sstevel@tonic-gate         int i, dist;
17857c478bd9Sstevel@tonic-gate         closest = 0;
17867c478bd9Sstevel@tonic-gate         dist = SWAB32(pBt, pInfo->aFree[0]) - nearby;
17877c478bd9Sstevel@tonic-gate         if( dist<0 ) dist = -dist;
17887c478bd9Sstevel@tonic-gate         for(i=1; i<n; i++){
17897c478bd9Sstevel@tonic-gate           int d2 = SWAB32(pBt, pInfo->aFree[i]) - nearby;
17907c478bd9Sstevel@tonic-gate           if( d2<0 ) d2 = -d2;
17917c478bd9Sstevel@tonic-gate           if( d2<dist ) closest = i;
17927c478bd9Sstevel@tonic-gate         }
17937c478bd9Sstevel@tonic-gate       }else{
17947c478bd9Sstevel@tonic-gate         closest = 0;
17957c478bd9Sstevel@tonic-gate       }
17967c478bd9Sstevel@tonic-gate       SWAB_ADD(pBt, pInfo->nFree, -1);
17977c478bd9Sstevel@tonic-gate       *pPgno = SWAB32(pBt, pInfo->aFree[closest]);
17987c478bd9Sstevel@tonic-gate       pInfo->aFree[closest] = pInfo->aFree[n-1];
17997c478bd9Sstevel@tonic-gate       rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
18007c478bd9Sstevel@tonic-gate       sqlitepager_unref(pOvfl);
18017c478bd9Sstevel@tonic-gate       if( rc==SQLITE_OK ){
18027c478bd9Sstevel@tonic-gate         sqlitepager_dont_rollback(*ppPage);
18037c478bd9Sstevel@tonic-gate         rc = sqlitepager_write(*ppPage);
18047c478bd9Sstevel@tonic-gate       }
18057c478bd9Sstevel@tonic-gate     }
18067c478bd9Sstevel@tonic-gate   }else{
18077c478bd9Sstevel@tonic-gate     *pPgno = sqlitepager_pagecount(pBt->pPager) + 1;
18087c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
18097c478bd9Sstevel@tonic-gate     if( rc ) return rc;
18107c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(*ppPage);
18117c478bd9Sstevel@tonic-gate   }
18127c478bd9Sstevel@tonic-gate   return rc;
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate /*
18167c478bd9Sstevel@tonic-gate ** Add a page of the database file to the freelist.  Either pgno or
1817*1da57d55SToomas Soome ** pPage but not both may be 0.
18187c478bd9Sstevel@tonic-gate **
18197c478bd9Sstevel@tonic-gate ** sqlitepager_unref() is NOT called for pPage.
18207c478bd9Sstevel@tonic-gate */
freePage(Btree * pBt,void * pPage,Pgno pgno)18217c478bd9Sstevel@tonic-gate static int freePage(Btree *pBt, void *pPage, Pgno pgno){
18227c478bd9Sstevel@tonic-gate   PageOne *pPage1 = pBt->page1;
18237c478bd9Sstevel@tonic-gate   OverflowPage *pOvfl = (OverflowPage*)pPage;
18247c478bd9Sstevel@tonic-gate   int rc;
18257c478bd9Sstevel@tonic-gate   int needUnref = 0;
18267c478bd9Sstevel@tonic-gate   MemPage *pMemPage;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate   if( pgno==0 ){
18297c478bd9Sstevel@tonic-gate     assert( pOvfl!=0 );
18307c478bd9Sstevel@tonic-gate     pgno = sqlitepager_pagenumber(pOvfl);
18317c478bd9Sstevel@tonic-gate   }
18327c478bd9Sstevel@tonic-gate   assert( pgno>2 );
18337c478bd9Sstevel@tonic-gate   assert( sqlitepager_pagenumber(pOvfl)==pgno );
18347c478bd9Sstevel@tonic-gate   pMemPage = (MemPage*)pPage;
18357c478bd9Sstevel@tonic-gate   pMemPage->isInit = 0;
18367c478bd9Sstevel@tonic-gate   if( pMemPage->pParent ){
18377c478bd9Sstevel@tonic-gate     sqlitepager_unref(pMemPage->pParent);
18387c478bd9Sstevel@tonic-gate     pMemPage->pParent = 0;
18397c478bd9Sstevel@tonic-gate   }
18407c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pPage1);
18417c478bd9Sstevel@tonic-gate   if( rc ){
18427c478bd9Sstevel@tonic-gate     return rc;
18437c478bd9Sstevel@tonic-gate   }
18447c478bd9Sstevel@tonic-gate   SWAB_ADD(pBt, pPage1->nFree, 1);
18457c478bd9Sstevel@tonic-gate   if( pPage1->nFree!=0 && pPage1->freeList!=0 ){
18467c478bd9Sstevel@tonic-gate     OverflowPage *pFreeIdx;
18477c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
18487c478bd9Sstevel@tonic-gate                         (void**)&pFreeIdx);
18497c478bd9Sstevel@tonic-gate     if( rc==SQLITE_OK ){
18507c478bd9Sstevel@tonic-gate       FreelistInfo *pInfo = (FreelistInfo*)pFreeIdx->aPayload;
18517c478bd9Sstevel@tonic-gate       int n = SWAB32(pBt, pInfo->nFree);
18527c478bd9Sstevel@tonic-gate       if( n<(sizeof(pInfo->aFree)/sizeof(pInfo->aFree[0])) ){
18537c478bd9Sstevel@tonic-gate         rc = sqlitepager_write(pFreeIdx);
18547c478bd9Sstevel@tonic-gate         if( rc==SQLITE_OK ){
18557c478bd9Sstevel@tonic-gate           pInfo->aFree[n] = SWAB32(pBt, pgno);
18567c478bd9Sstevel@tonic-gate           SWAB_ADD(pBt, pInfo->nFree, 1);
18577c478bd9Sstevel@tonic-gate           sqlitepager_unref(pFreeIdx);
18587c478bd9Sstevel@tonic-gate           sqlitepager_dont_write(pBt->pPager, pgno);
18597c478bd9Sstevel@tonic-gate           return rc;
18607c478bd9Sstevel@tonic-gate         }
18617c478bd9Sstevel@tonic-gate       }
18627c478bd9Sstevel@tonic-gate       sqlitepager_unref(pFreeIdx);
18637c478bd9Sstevel@tonic-gate     }
18647c478bd9Sstevel@tonic-gate   }
18657c478bd9Sstevel@tonic-gate   if( pOvfl==0 ){
18667c478bd9Sstevel@tonic-gate     assert( pgno>0 );
18677c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl);
18687c478bd9Sstevel@tonic-gate     if( rc ) return rc;
18697c478bd9Sstevel@tonic-gate     needUnref = 1;
18707c478bd9Sstevel@tonic-gate   }
18717c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pOvfl);
18727c478bd9Sstevel@tonic-gate   if( rc ){
18737c478bd9Sstevel@tonic-gate     if( needUnref ) sqlitepager_unref(pOvfl);
18747c478bd9Sstevel@tonic-gate     return rc;
18757c478bd9Sstevel@tonic-gate   }
18767c478bd9Sstevel@tonic-gate   pOvfl->iNext = pPage1->freeList;
18777c478bd9Sstevel@tonic-gate   pPage1->freeList = SWAB32(pBt, pgno);
18787c478bd9Sstevel@tonic-gate   memset(pOvfl->aPayload, 0, OVERFLOW_SIZE);
18797c478bd9Sstevel@tonic-gate   if( needUnref ) rc = sqlitepager_unref(pOvfl);
18807c478bd9Sstevel@tonic-gate   return rc;
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate /*
18847c478bd9Sstevel@tonic-gate ** Erase all the data out of a cell.  This involves returning overflow
18857c478bd9Sstevel@tonic-gate ** pages back the freelist.
18867c478bd9Sstevel@tonic-gate */
clearCell(Btree * pBt,Cell * pCell)18877c478bd9Sstevel@tonic-gate static int clearCell(Btree *pBt, Cell *pCell){
18887c478bd9Sstevel@tonic-gate   Pager *pPager = pBt->pPager;
18897c478bd9Sstevel@tonic-gate   OverflowPage *pOvfl;
18907c478bd9Sstevel@tonic-gate   Pgno ovfl, nextOvfl;
18917c478bd9Sstevel@tonic-gate   int rc;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate   if( NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h) <= MX_LOCAL_PAYLOAD ){
18947c478bd9Sstevel@tonic-gate     return SQLITE_OK;
18957c478bd9Sstevel@tonic-gate   }
18967c478bd9Sstevel@tonic-gate   ovfl = SWAB32(pBt, pCell->ovfl);
18977c478bd9Sstevel@tonic-gate   pCell->ovfl = 0;
18987c478bd9Sstevel@tonic-gate   while( ovfl ){
18997c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl);
19007c478bd9Sstevel@tonic-gate     if( rc ) return rc;
19017c478bd9Sstevel@tonic-gate     nextOvfl = SWAB32(pBt, pOvfl->iNext);
19027c478bd9Sstevel@tonic-gate     rc = freePage(pBt, pOvfl, ovfl);
19037c478bd9Sstevel@tonic-gate     if( rc ) return rc;
19047c478bd9Sstevel@tonic-gate     sqlitepager_unref(pOvfl);
19057c478bd9Sstevel@tonic-gate     ovfl = nextOvfl;
19067c478bd9Sstevel@tonic-gate   }
19077c478bd9Sstevel@tonic-gate   return SQLITE_OK;
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate /*
19117c478bd9Sstevel@tonic-gate ** Create a new cell from key and data.  Overflow pages are allocated as
1912*1da57d55SToomas Soome ** necessary and linked to this cell.
19137c478bd9Sstevel@tonic-gate */
fillInCell(Btree * pBt,Cell * pCell,const void * pKey,int nKey,const void * pData,int nData)19147c478bd9Sstevel@tonic-gate static int fillInCell(
19157c478bd9Sstevel@tonic-gate   Btree *pBt,              /* The whole Btree.  Needed to allocate pages */
19167c478bd9Sstevel@tonic-gate   Cell *pCell,             /* Populate this Cell structure */
19177c478bd9Sstevel@tonic-gate   const void *pKey, int nKey,    /* The key */
19187c478bd9Sstevel@tonic-gate   const void *pData,int nData    /* The data */
19197c478bd9Sstevel@tonic-gate ){
19207c478bd9Sstevel@tonic-gate   OverflowPage *pOvfl, *pPrior;
19217c478bd9Sstevel@tonic-gate   Pgno *pNext;
19227c478bd9Sstevel@tonic-gate   int spaceLeft;
19237c478bd9Sstevel@tonic-gate   int n, rc;
19247c478bd9Sstevel@tonic-gate   int nPayload;
19257c478bd9Sstevel@tonic-gate   const char *pPayload;
19267c478bd9Sstevel@tonic-gate   char *pSpace;
19277c478bd9Sstevel@tonic-gate   Pgno nearby = 0;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate   pCell->h.leftChild = 0;
19307c478bd9Sstevel@tonic-gate   pCell->h.nKey = SWAB16(pBt, nKey & 0xffff);
19317c478bd9Sstevel@tonic-gate   pCell->h.nKeyHi = nKey >> 16;
19327c478bd9Sstevel@tonic-gate   pCell->h.nData = SWAB16(pBt, nData & 0xffff);
19337c478bd9Sstevel@tonic-gate   pCell->h.nDataHi = nData >> 16;
19347c478bd9Sstevel@tonic-gate   pCell->h.iNext = 0;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate   pNext = &pCell->ovfl;
19377c478bd9Sstevel@tonic-gate   pSpace = pCell->aPayload;
19387c478bd9Sstevel@tonic-gate   spaceLeft = MX_LOCAL_PAYLOAD;
19397c478bd9Sstevel@tonic-gate   pPayload = pKey;
19407c478bd9Sstevel@tonic-gate   pKey = 0;
19417c478bd9Sstevel@tonic-gate   nPayload = nKey;
19427c478bd9Sstevel@tonic-gate   pPrior = 0;
19437c478bd9Sstevel@tonic-gate   while( nPayload>0 ){
19447c478bd9Sstevel@tonic-gate     if( spaceLeft==0 ){
19457c478bd9Sstevel@tonic-gate       rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext, nearby);
19467c478bd9Sstevel@tonic-gate       if( rc ){
19477c478bd9Sstevel@tonic-gate         *pNext = 0;
19487c478bd9Sstevel@tonic-gate       }else{
19497c478bd9Sstevel@tonic-gate         nearby = *pNext;
19507c478bd9Sstevel@tonic-gate       }
19517c478bd9Sstevel@tonic-gate       if( pPrior ) sqlitepager_unref(pPrior);
19527c478bd9Sstevel@tonic-gate       if( rc ){
19537c478bd9Sstevel@tonic-gate         clearCell(pBt, pCell);
19547c478bd9Sstevel@tonic-gate         return rc;
19557c478bd9Sstevel@tonic-gate       }
19567c478bd9Sstevel@tonic-gate       if( pBt->needSwab ) *pNext = swab32(*pNext);
19577c478bd9Sstevel@tonic-gate       pPrior = pOvfl;
19587c478bd9Sstevel@tonic-gate       spaceLeft = OVERFLOW_SIZE;
19597c478bd9Sstevel@tonic-gate       pSpace = pOvfl->aPayload;
19607c478bd9Sstevel@tonic-gate       pNext = &pOvfl->iNext;
19617c478bd9Sstevel@tonic-gate     }
19627c478bd9Sstevel@tonic-gate     n = nPayload;
19637c478bd9Sstevel@tonic-gate     if( n>spaceLeft ) n = spaceLeft;
19647c478bd9Sstevel@tonic-gate     memcpy(pSpace, pPayload, n);
19657c478bd9Sstevel@tonic-gate     nPayload -= n;
19667c478bd9Sstevel@tonic-gate     if( nPayload==0 && pData ){
19677c478bd9Sstevel@tonic-gate       pPayload = pData;
19687c478bd9Sstevel@tonic-gate       nPayload = nData;
19697c478bd9Sstevel@tonic-gate       pData = 0;
19707c478bd9Sstevel@tonic-gate     }else{
19717c478bd9Sstevel@tonic-gate       pPayload += n;
19727c478bd9Sstevel@tonic-gate     }
19737c478bd9Sstevel@tonic-gate     spaceLeft -= n;
19747c478bd9Sstevel@tonic-gate     pSpace += n;
19757c478bd9Sstevel@tonic-gate   }
19767c478bd9Sstevel@tonic-gate   *pNext = 0;
19777c478bd9Sstevel@tonic-gate   if( pPrior ){
19787c478bd9Sstevel@tonic-gate     sqlitepager_unref(pPrior);
19797c478bd9Sstevel@tonic-gate   }
19807c478bd9Sstevel@tonic-gate   return SQLITE_OK;
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate /*
19847c478bd9Sstevel@tonic-gate ** Change the MemPage.pParent pointer on the page whose number is
19857c478bd9Sstevel@tonic-gate ** given in the second argument so that MemPage.pParent holds the
19867c478bd9Sstevel@tonic-gate ** pointer in the third argument.
19877c478bd9Sstevel@tonic-gate */
reparentPage(Pager * pPager,Pgno pgno,MemPage * pNewParent,int idx)19887c478bd9Sstevel@tonic-gate static void reparentPage(Pager *pPager, Pgno pgno, MemPage *pNewParent,int idx){
19897c478bd9Sstevel@tonic-gate   MemPage *pThis;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate   if( pgno==0 ) return;
19927c478bd9Sstevel@tonic-gate   assert( pPager!=0 );
19937c478bd9Sstevel@tonic-gate   pThis = sqlitepager_lookup(pPager, pgno);
19947c478bd9Sstevel@tonic-gate   if( pThis && pThis->isInit ){
19957c478bd9Sstevel@tonic-gate     if( pThis->pParent!=pNewParent ){
19967c478bd9Sstevel@tonic-gate       if( pThis->pParent ) sqlitepager_unref(pThis->pParent);
19977c478bd9Sstevel@tonic-gate       pThis->pParent = pNewParent;
19987c478bd9Sstevel@tonic-gate       if( pNewParent ) sqlitepager_ref(pNewParent);
19997c478bd9Sstevel@tonic-gate     }
20007c478bd9Sstevel@tonic-gate     pThis->idxParent = idx;
20017c478bd9Sstevel@tonic-gate     sqlitepager_unref(pThis);
20027c478bd9Sstevel@tonic-gate   }
20037c478bd9Sstevel@tonic-gate }
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate /*
20067c478bd9Sstevel@tonic-gate ** Reparent all children of the given page to be the given page.
20077c478bd9Sstevel@tonic-gate ** In other words, for every child of pPage, invoke reparentPage()
20087c478bd9Sstevel@tonic-gate ** to make sure that each child knows that pPage is its parent.
20097c478bd9Sstevel@tonic-gate **
20107c478bd9Sstevel@tonic-gate ** This routine gets called after you memcpy() one page into
20117c478bd9Sstevel@tonic-gate ** another.
20127c478bd9Sstevel@tonic-gate */
reparentChildPages(Btree * pBt,MemPage * pPage)20137c478bd9Sstevel@tonic-gate static void reparentChildPages(Btree *pBt, MemPage *pPage){
20147c478bd9Sstevel@tonic-gate   int i;
20157c478bd9Sstevel@tonic-gate   Pager *pPager = pBt->pPager;
20167c478bd9Sstevel@tonic-gate   for(i=0; i<pPage->nCell; i++){
20177c478bd9Sstevel@tonic-gate     reparentPage(pPager, SWAB32(pBt, pPage->apCell[i]->h.leftChild), pPage, i);
20187c478bd9Sstevel@tonic-gate   }
20197c478bd9Sstevel@tonic-gate   reparentPage(pPager, SWAB32(pBt, pPage->u.hdr.rightChild), pPage, i);
20207c478bd9Sstevel@tonic-gate   pPage->idxShift = 0;
20217c478bd9Sstevel@tonic-gate }
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate /*
20247c478bd9Sstevel@tonic-gate ** Remove the i-th cell from pPage.  This routine effects pPage only.
20257c478bd9Sstevel@tonic-gate ** The cell content is not freed or deallocated.  It is assumed that
20267c478bd9Sstevel@tonic-gate ** the cell content has been copied someplace else.  This routine just
20277c478bd9Sstevel@tonic-gate ** removes the reference to the cell from pPage.
20287c478bd9Sstevel@tonic-gate **
20297c478bd9Sstevel@tonic-gate ** "sz" must be the number of bytes in the cell.
20307c478bd9Sstevel@tonic-gate **
20317c478bd9Sstevel@tonic-gate ** Do not bother maintaining the integrity of the linked list of Cells.
2032*1da57d55SToomas Soome ** Only the pPage->apCell[] array is important.  The relinkCellList()
2033*1da57d55SToomas Soome ** routine will be called soon after this routine in order to rebuild
20347c478bd9Sstevel@tonic-gate ** the linked list.
20357c478bd9Sstevel@tonic-gate */
dropCell(Btree * pBt,MemPage * pPage,int idx,int sz)20367c478bd9Sstevel@tonic-gate static void dropCell(Btree *pBt, MemPage *pPage, int idx, int sz){
20377c478bd9Sstevel@tonic-gate   int j;
20387c478bd9Sstevel@tonic-gate   assert( idx>=0 && idx<pPage->nCell );
20397c478bd9Sstevel@tonic-gate   assert( sz==cellSize(pBt, pPage->apCell[idx]) );
20407c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
20417c478bd9Sstevel@tonic-gate   freeSpace(pBt, pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz);
20427c478bd9Sstevel@tonic-gate   for(j=idx; j<pPage->nCell-1; j++){
20437c478bd9Sstevel@tonic-gate     pPage->apCell[j] = pPage->apCell[j+1];
20447c478bd9Sstevel@tonic-gate   }
20457c478bd9Sstevel@tonic-gate   pPage->nCell--;
20467c478bd9Sstevel@tonic-gate   pPage->idxShift = 1;
20477c478bd9Sstevel@tonic-gate }
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate /*
20507c478bd9Sstevel@tonic-gate ** Insert a new cell on pPage at cell index "i".  pCell points to the
20517c478bd9Sstevel@tonic-gate ** content of the cell.
20527c478bd9Sstevel@tonic-gate **
20537c478bd9Sstevel@tonic-gate ** If the cell content will fit on the page, then put it there.  If it
20547c478bd9Sstevel@tonic-gate ** will not fit, then just make pPage->apCell[i] point to the content
2055*1da57d55SToomas Soome ** and set pPage->isOverfull.
20567c478bd9Sstevel@tonic-gate **
20577c478bd9Sstevel@tonic-gate ** Do not bother maintaining the integrity of the linked list of Cells.
2058*1da57d55SToomas Soome ** Only the pPage->apCell[] array is important.  The relinkCellList()
2059*1da57d55SToomas Soome ** routine will be called soon after this routine in order to rebuild
20607c478bd9Sstevel@tonic-gate ** the linked list.
20617c478bd9Sstevel@tonic-gate */
insertCell(Btree * pBt,MemPage * pPage,int i,Cell * pCell,int sz)20627c478bd9Sstevel@tonic-gate static void insertCell(Btree *pBt, MemPage *pPage, int i, Cell *pCell, int sz){
20637c478bd9Sstevel@tonic-gate   int idx, j;
20647c478bd9Sstevel@tonic-gate   assert( i>=0 && i<=pPage->nCell );
20657c478bd9Sstevel@tonic-gate   assert( sz==cellSize(pBt, pCell) );
20667c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
20677c478bd9Sstevel@tonic-gate   idx = allocateSpace(pBt, pPage, sz);
20687c478bd9Sstevel@tonic-gate   for(j=pPage->nCell; j>i; j--){
20697c478bd9Sstevel@tonic-gate     pPage->apCell[j] = pPage->apCell[j-1];
20707c478bd9Sstevel@tonic-gate   }
20717c478bd9Sstevel@tonic-gate   pPage->nCell++;
20727c478bd9Sstevel@tonic-gate   if( idx<=0 ){
20737c478bd9Sstevel@tonic-gate     pPage->isOverfull = 1;
20747c478bd9Sstevel@tonic-gate     pPage->apCell[i] = pCell;
20757c478bd9Sstevel@tonic-gate   }else{
20767c478bd9Sstevel@tonic-gate     memcpy(&pPage->u.aDisk[idx], pCell, sz);
20777c478bd9Sstevel@tonic-gate     pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx];
20787c478bd9Sstevel@tonic-gate   }
20797c478bd9Sstevel@tonic-gate   pPage->idxShift = 1;
20807c478bd9Sstevel@tonic-gate }
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate /*
20837c478bd9Sstevel@tonic-gate ** Rebuild the linked list of cells on a page so that the cells
2084*1da57d55SToomas Soome ** occur in the order specified by the pPage->apCell[] array.
20857c478bd9Sstevel@tonic-gate ** Invoke this routine once to repair damage after one or more
20867c478bd9Sstevel@tonic-gate ** invocations of either insertCell() or dropCell().
20877c478bd9Sstevel@tonic-gate */
relinkCellList(Btree * pBt,MemPage * pPage)20887c478bd9Sstevel@tonic-gate static void relinkCellList(Btree *pBt, MemPage *pPage){
20897c478bd9Sstevel@tonic-gate   int i;
20907c478bd9Sstevel@tonic-gate   u16 *pIdx;
20917c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
20927c478bd9Sstevel@tonic-gate   pIdx = &pPage->u.hdr.firstCell;
20937c478bd9Sstevel@tonic-gate   for(i=0; i<pPage->nCell; i++){
20947c478bd9Sstevel@tonic-gate     int idx = Addr(pPage->apCell[i]) - Addr(pPage);
20957c478bd9Sstevel@tonic-gate     assert( idx>0 && idx<SQLITE_USABLE_SIZE );
20967c478bd9Sstevel@tonic-gate     *pIdx = SWAB16(pBt, idx);
20977c478bd9Sstevel@tonic-gate     pIdx = &pPage->apCell[i]->h.iNext;
20987c478bd9Sstevel@tonic-gate   }
20997c478bd9Sstevel@tonic-gate   *pIdx = 0;
21007c478bd9Sstevel@tonic-gate }
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate /*
21037c478bd9Sstevel@tonic-gate ** Make a copy of the contents of pFrom into pTo.  The pFrom->apCell[]
21047c478bd9Sstevel@tonic-gate ** pointers that point into pFrom->u.aDisk[] must be adjusted to point
21057c478bd9Sstevel@tonic-gate ** into pTo->u.aDisk[] instead.  But some pFrom->apCell[] entries might
21067c478bd9Sstevel@tonic-gate ** not point to pFrom->u.aDisk[].  Those are unchanged.
21077c478bd9Sstevel@tonic-gate */
copyPage(MemPage * pTo,MemPage * pFrom)21087c478bd9Sstevel@tonic-gate static void copyPage(MemPage *pTo, MemPage *pFrom){
21097c478bd9Sstevel@tonic-gate   uptr from, to;
21107c478bd9Sstevel@tonic-gate   int i;
21117c478bd9Sstevel@tonic-gate   memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_USABLE_SIZE);
21127c478bd9Sstevel@tonic-gate   pTo->pParent = 0;
21137c478bd9Sstevel@tonic-gate   pTo->isInit = 1;
21147c478bd9Sstevel@tonic-gate   pTo->nCell = pFrom->nCell;
21157c478bd9Sstevel@tonic-gate   pTo->nFree = pFrom->nFree;
21167c478bd9Sstevel@tonic-gate   pTo->isOverfull = pFrom->isOverfull;
21177c478bd9Sstevel@tonic-gate   to = Addr(pTo);
21187c478bd9Sstevel@tonic-gate   from = Addr(pFrom);
21197c478bd9Sstevel@tonic-gate   for(i=0; i<pTo->nCell; i++){
21207c478bd9Sstevel@tonic-gate     uptr x = Addr(pFrom->apCell[i]);
21217c478bd9Sstevel@tonic-gate     if( x>from && x<from+SQLITE_USABLE_SIZE ){
21227c478bd9Sstevel@tonic-gate       *((uptr*)&pTo->apCell[i]) = x + to - from;
21237c478bd9Sstevel@tonic-gate     }else{
21247c478bd9Sstevel@tonic-gate       pTo->apCell[i] = pFrom->apCell[i];
21257c478bd9Sstevel@tonic-gate     }
21267c478bd9Sstevel@tonic-gate   }
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate /*
21307c478bd9Sstevel@tonic-gate ** The following parameters determine how many adjacent pages get involved
21317c478bd9Sstevel@tonic-gate ** in a balancing operation.  NN is the number of neighbors on either side
21327c478bd9Sstevel@tonic-gate ** of the page that participate in the balancing operation.  NB is the
21337c478bd9Sstevel@tonic-gate ** total number of pages that participate, including the target page and
21347c478bd9Sstevel@tonic-gate ** NN neighbors on either side.
21357c478bd9Sstevel@tonic-gate **
21367c478bd9Sstevel@tonic-gate ** The minimum value of NN is 1 (of course).  Increasing NN above 1
21377c478bd9Sstevel@tonic-gate ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
21387c478bd9Sstevel@tonic-gate ** in exchange for a larger degradation in INSERT and UPDATE performance.
21397c478bd9Sstevel@tonic-gate ** The value of NN appears to give the best results overall.
21407c478bd9Sstevel@tonic-gate */
21417c478bd9Sstevel@tonic-gate #define NN 1             /* Number of neighbors on either side of pPage */
21427c478bd9Sstevel@tonic-gate #define NB (NN*2+1)      /* Total pages involved in the balance */
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate /*
21457c478bd9Sstevel@tonic-gate ** This routine redistributes Cells on pPage and up to two siblings
21467c478bd9Sstevel@tonic-gate ** of pPage so that all pages have about the same amount of free space.
21477c478bd9Sstevel@tonic-gate ** Usually one sibling on either side of pPage is used in the balancing,
21487c478bd9Sstevel@tonic-gate ** though both siblings might come from one side if pPage is the first
21497c478bd9Sstevel@tonic-gate ** or last child of its parent.  If pPage has fewer than two siblings
2150*1da57d55SToomas Soome ** (something which can only happen if pPage is the root page or a
21517c478bd9Sstevel@tonic-gate ** child of root) then all available siblings participate in the balancing.
21527c478bd9Sstevel@tonic-gate **
21537c478bd9Sstevel@tonic-gate ** The number of siblings of pPage might be increased or decreased by
21547c478bd9Sstevel@tonic-gate ** one in an effort to keep pages between 66% and 100% full. The root page
2155*1da57d55SToomas Soome ** is special and is allowed to be less than 66% full. If pPage is
21567c478bd9Sstevel@tonic-gate ** the root page, then the depth of the tree might be increased
21577c478bd9Sstevel@tonic-gate ** or decreased by one, as necessary, to keep the root page from being
21587c478bd9Sstevel@tonic-gate ** overfull or empty.
21597c478bd9Sstevel@tonic-gate **
21607c478bd9Sstevel@tonic-gate ** This routine calls relinkCellList() on its input page regardless of
21617c478bd9Sstevel@tonic-gate ** whether or not it does any real balancing.  Client routines will typically
21627c478bd9Sstevel@tonic-gate ** invoke insertCell() or dropCell() before calling this routine, so we
21637c478bd9Sstevel@tonic-gate ** need to call relinkCellList() to clean up the mess that those other
21647c478bd9Sstevel@tonic-gate ** routines left behind.
21657c478bd9Sstevel@tonic-gate **
21667c478bd9Sstevel@tonic-gate ** pCur is left pointing to the same cell as when this routine was called
21677c478bd9Sstevel@tonic-gate ** even if that cell gets moved to a different page.  pCur may be NULL.
21687c478bd9Sstevel@tonic-gate ** Set the pCur parameter to NULL if you do not care about keeping track
21697c478bd9Sstevel@tonic-gate ** of a cell as that will save this routine the work of keeping track of it.
21707c478bd9Sstevel@tonic-gate **
21717c478bd9Sstevel@tonic-gate ** Note that when this routine is called, some of the Cells on pPage
21727c478bd9Sstevel@tonic-gate ** might not actually be stored in pPage->u.aDisk[].  This can happen
21737c478bd9Sstevel@tonic-gate ** if the page is overfull.  Part of the job of this routine is to
21747c478bd9Sstevel@tonic-gate ** make sure all Cells for pPage once again fit in pPage->u.aDisk[].
21757c478bd9Sstevel@tonic-gate **
21767c478bd9Sstevel@tonic-gate ** In the course of balancing the siblings of pPage, the parent of pPage
21777c478bd9Sstevel@tonic-gate ** might become overfull or underfull.  If that happens, then this routine
21787c478bd9Sstevel@tonic-gate ** is called recursively on the parent.
21797c478bd9Sstevel@tonic-gate **
21807c478bd9Sstevel@tonic-gate ** If this routine fails for any reason, it might leave the database
21817c478bd9Sstevel@tonic-gate ** in a corrupted state.  So if this routine fails, the database should
21827c478bd9Sstevel@tonic-gate ** be rolled back.
21837c478bd9Sstevel@tonic-gate */
balance(Btree * pBt,MemPage * pPage,BtCursor * pCur)21847c478bd9Sstevel@tonic-gate static int balance(Btree *pBt, MemPage *pPage, BtCursor *pCur){
21857c478bd9Sstevel@tonic-gate   MemPage *pParent;            /* The parent of pPage */
21867c478bd9Sstevel@tonic-gate   int nCell;                   /* Number of cells in apCell[] */
21877c478bd9Sstevel@tonic-gate   int nOld;                    /* Number of pages in apOld[] */
21887c478bd9Sstevel@tonic-gate   int nNew;                    /* Number of pages in apNew[] */
21897c478bd9Sstevel@tonic-gate   int nDiv;                    /* Number of cells in apDiv[] */
21907c478bd9Sstevel@tonic-gate   int i, j, k;                 /* Loop counters */
21917c478bd9Sstevel@tonic-gate   int idx;                     /* Index of pPage in pParent->apCell[] */
21927c478bd9Sstevel@tonic-gate   int nxDiv;                   /* Next divider slot in pParent->apCell[] */
21937c478bd9Sstevel@tonic-gate   int rc;                      /* The return code */
21947c478bd9Sstevel@tonic-gate   int iCur;                    /* apCell[iCur] is the cell of the cursor */
21957c478bd9Sstevel@tonic-gate   MemPage *pOldCurPage;        /* The cursor originally points to this page */
21967c478bd9Sstevel@tonic-gate   int subtotal;                /* Subtotal of bytes in cells on one page */
21977c478bd9Sstevel@tonic-gate   MemPage *extraUnref = 0;     /* A page that needs to be unref-ed */
21987c478bd9Sstevel@tonic-gate   MemPage *apOld[NB];          /* pPage and up to two siblings */
21997c478bd9Sstevel@tonic-gate   Pgno pgnoOld[NB];            /* Page numbers for each page in apOld[] */
22007c478bd9Sstevel@tonic-gate   MemPage *apNew[NB+1];        /* pPage and up to NB siblings after balancing */
22017c478bd9Sstevel@tonic-gate   Pgno pgnoNew[NB+1];          /* Page numbers for each page in apNew[] */
22027c478bd9Sstevel@tonic-gate   int idxDiv[NB];              /* Indices of divider cells in pParent */
22037c478bd9Sstevel@tonic-gate   Cell *apDiv[NB];             /* Divider cells in pParent */
22047c478bd9Sstevel@tonic-gate   Cell aTemp[NB];              /* Temporary holding area for apDiv[] */
22057c478bd9Sstevel@tonic-gate   int cntNew[NB+1];            /* Index in apCell[] of cell after i-th page */
22067c478bd9Sstevel@tonic-gate   int szNew[NB+1];             /* Combined size of cells place on i-th page */
22077c478bd9Sstevel@tonic-gate   MemPage aOld[NB];            /* Temporary copies of pPage and its siblings */
22087c478bd9Sstevel@tonic-gate   Cell *apCell[(MX_CELL+2)*NB]; /* All cells from pages being balanced */
22097c478bd9Sstevel@tonic-gate   int szCell[(MX_CELL+2)*NB];  /* Local size of all cells */
22107c478bd9Sstevel@tonic-gate 
2211*1da57d55SToomas Soome   /*
22127c478bd9Sstevel@tonic-gate   ** Return without doing any work if pPage is neither overfull nor
22137c478bd9Sstevel@tonic-gate   ** underfull.
22147c478bd9Sstevel@tonic-gate   */
22157c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pPage) );
2216*1da57d55SToomas Soome   if( !pPage->isOverfull && pPage->nFree<SQLITE_USABLE_SIZE/2
22177c478bd9Sstevel@tonic-gate         && pPage->nCell>=2){
22187c478bd9Sstevel@tonic-gate     relinkCellList(pBt, pPage);
22197c478bd9Sstevel@tonic-gate     return SQLITE_OK;
22207c478bd9Sstevel@tonic-gate   }
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate   /*
22237c478bd9Sstevel@tonic-gate   ** Find the parent of the page to be balanceed.
22247c478bd9Sstevel@tonic-gate   ** If there is no parent, it means this page is the root page and
22257c478bd9Sstevel@tonic-gate   ** special rules apply.
22267c478bd9Sstevel@tonic-gate   */
22277c478bd9Sstevel@tonic-gate   pParent = pPage->pParent;
22287c478bd9Sstevel@tonic-gate   if( pParent==0 ){
22297c478bd9Sstevel@tonic-gate     Pgno pgnoChild;
22307c478bd9Sstevel@tonic-gate     MemPage *pChild;
22317c478bd9Sstevel@tonic-gate     assert( pPage->isInit );
22327c478bd9Sstevel@tonic-gate     if( pPage->nCell==0 ){
22337c478bd9Sstevel@tonic-gate       if( pPage->u.hdr.rightChild ){
22347c478bd9Sstevel@tonic-gate         /*
22357c478bd9Sstevel@tonic-gate         ** The root page is empty.  Copy the one child page
22367c478bd9Sstevel@tonic-gate         ** into the root page and return.  This reduces the depth
22377c478bd9Sstevel@tonic-gate         ** of the BTree by one.
22387c478bd9Sstevel@tonic-gate         */
22397c478bd9Sstevel@tonic-gate         pgnoChild = SWAB32(pBt, pPage->u.hdr.rightChild);
22407c478bd9Sstevel@tonic-gate         rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild);
22417c478bd9Sstevel@tonic-gate         if( rc ) return rc;
22427c478bd9Sstevel@tonic-gate         memcpy(pPage, pChild, SQLITE_USABLE_SIZE);
22437c478bd9Sstevel@tonic-gate         pPage->isInit = 0;
22447c478bd9Sstevel@tonic-gate         rc = initPage(pBt, pPage, sqlitepager_pagenumber(pPage), 0);
22457c478bd9Sstevel@tonic-gate         assert( rc==SQLITE_OK );
22467c478bd9Sstevel@tonic-gate         reparentChildPages(pBt, pPage);
22477c478bd9Sstevel@tonic-gate         if( pCur && pCur->pPage==pChild ){
22487c478bd9Sstevel@tonic-gate           sqlitepager_unref(pChild);
22497c478bd9Sstevel@tonic-gate           pCur->pPage = pPage;
22507c478bd9Sstevel@tonic-gate           sqlitepager_ref(pPage);
22517c478bd9Sstevel@tonic-gate         }
22527c478bd9Sstevel@tonic-gate         freePage(pBt, pChild, pgnoChild);
22537c478bd9Sstevel@tonic-gate         sqlitepager_unref(pChild);
22547c478bd9Sstevel@tonic-gate       }else{
22557c478bd9Sstevel@tonic-gate         relinkCellList(pBt, pPage);
22567c478bd9Sstevel@tonic-gate       }
22577c478bd9Sstevel@tonic-gate       return SQLITE_OK;
22587c478bd9Sstevel@tonic-gate     }
22597c478bd9Sstevel@tonic-gate     if( !pPage->isOverfull ){
22607c478bd9Sstevel@tonic-gate       /* It is OK for the root page to be less than half full.
22617c478bd9Sstevel@tonic-gate       */
22627c478bd9Sstevel@tonic-gate       relinkCellList(pBt, pPage);
22637c478bd9Sstevel@tonic-gate       return SQLITE_OK;
22647c478bd9Sstevel@tonic-gate     }
22657c478bd9Sstevel@tonic-gate     /*
22667c478bd9Sstevel@tonic-gate     ** If we get to here, it means the root page is overfull.
22677c478bd9Sstevel@tonic-gate     ** When this happens, Create a new child page and copy the
22687c478bd9Sstevel@tonic-gate     ** contents of the root into the child.  Then make the root
22697c478bd9Sstevel@tonic-gate     ** page an empty page with rightChild pointing to the new
22707c478bd9Sstevel@tonic-gate     ** child.  Then fall thru to the code below which will cause
22717c478bd9Sstevel@tonic-gate     ** the overfull child page to be split.
22727c478bd9Sstevel@tonic-gate     */
22737c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(pPage);
22747c478bd9Sstevel@tonic-gate     if( rc ) return rc;
22757c478bd9Sstevel@tonic-gate     rc = allocatePage(pBt, &pChild, &pgnoChild, sqlitepager_pagenumber(pPage));
22767c478bd9Sstevel@tonic-gate     if( rc ) return rc;
22777c478bd9Sstevel@tonic-gate     assert( sqlitepager_iswriteable(pChild) );
22787c478bd9Sstevel@tonic-gate     copyPage(pChild, pPage);
22797c478bd9Sstevel@tonic-gate     pChild->pParent = pPage;
22807c478bd9Sstevel@tonic-gate     pChild->idxParent = 0;
22817c478bd9Sstevel@tonic-gate     sqlitepager_ref(pPage);
22827c478bd9Sstevel@tonic-gate     pChild->isOverfull = 1;
22837c478bd9Sstevel@tonic-gate     if( pCur && pCur->pPage==pPage ){
22847c478bd9Sstevel@tonic-gate       sqlitepager_unref(pPage);
22857c478bd9Sstevel@tonic-gate       pCur->pPage = pChild;
22867c478bd9Sstevel@tonic-gate     }else{
22877c478bd9Sstevel@tonic-gate       extraUnref = pChild;
22887c478bd9Sstevel@tonic-gate     }
22897c478bd9Sstevel@tonic-gate     zeroPage(pBt, pPage);
22907c478bd9Sstevel@tonic-gate     pPage->u.hdr.rightChild = SWAB32(pBt, pgnoChild);
22917c478bd9Sstevel@tonic-gate     pParent = pPage;
22927c478bd9Sstevel@tonic-gate     pPage = pChild;
22937c478bd9Sstevel@tonic-gate   }
22947c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pParent);
22957c478bd9Sstevel@tonic-gate   if( rc ) return rc;
22967c478bd9Sstevel@tonic-gate   assert( pParent->isInit );
2297*1da57d55SToomas Soome 
22987c478bd9Sstevel@tonic-gate   /*
22997c478bd9Sstevel@tonic-gate   ** Find the Cell in the parent page whose h.leftChild points back
23007c478bd9Sstevel@tonic-gate   ** to pPage.  The "idx" variable is the index of that cell.  If pPage
2301*1da57d55SToomas Soome   ** is the rightmost child of pParent then set idx to pParent->nCell
23027c478bd9Sstevel@tonic-gate   */
23037c478bd9Sstevel@tonic-gate   if( pParent->idxShift ){
23047c478bd9Sstevel@tonic-gate     Pgno pgno, swabPgno;
23057c478bd9Sstevel@tonic-gate     pgno = sqlitepager_pagenumber(pPage);
23067c478bd9Sstevel@tonic-gate     swabPgno = SWAB32(pBt, pgno);
23077c478bd9Sstevel@tonic-gate     for(idx=0; idx<pParent->nCell; idx++){
23087c478bd9Sstevel@tonic-gate       if( pParent->apCell[idx]->h.leftChild==swabPgno ){
23097c478bd9Sstevel@tonic-gate         break;
23107c478bd9Sstevel@tonic-gate       }
23117c478bd9Sstevel@tonic-gate     }
23127c478bd9Sstevel@tonic-gate     assert( idx<pParent->nCell || pParent->u.hdr.rightChild==swabPgno );
23137c478bd9Sstevel@tonic-gate   }else{
23147c478bd9Sstevel@tonic-gate     idx = pPage->idxParent;
23157c478bd9Sstevel@tonic-gate   }
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate   /*
23187c478bd9Sstevel@tonic-gate   ** Initialize variables so that it will be safe to jump
23197c478bd9Sstevel@tonic-gate   ** directly to balance_cleanup at any moment.
23207c478bd9Sstevel@tonic-gate   */
23217c478bd9Sstevel@tonic-gate   nOld = nNew = 0;
23227c478bd9Sstevel@tonic-gate   sqlitepager_ref(pParent);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate   /*
23257c478bd9Sstevel@tonic-gate   ** Find sibling pages to pPage and the Cells in pParent that divide
23267c478bd9Sstevel@tonic-gate   ** the siblings.  An attempt is made to find NN siblings on either
23277c478bd9Sstevel@tonic-gate   ** side of pPage.  More siblings are taken from one side, however, if
23287c478bd9Sstevel@tonic-gate   ** pPage there are fewer than NN siblings on the other side.  If pParent
23297c478bd9Sstevel@tonic-gate   ** has NB or fewer children then all children of pParent are taken.
23307c478bd9Sstevel@tonic-gate   */
23317c478bd9Sstevel@tonic-gate   nxDiv = idx - NN;
23327c478bd9Sstevel@tonic-gate   if( nxDiv + NB > pParent->nCell ){
23337c478bd9Sstevel@tonic-gate     nxDiv = pParent->nCell - NB + 1;
23347c478bd9Sstevel@tonic-gate   }
23357c478bd9Sstevel@tonic-gate   if( nxDiv<0 ){
23367c478bd9Sstevel@tonic-gate     nxDiv = 0;
23377c478bd9Sstevel@tonic-gate   }
23387c478bd9Sstevel@tonic-gate   nDiv = 0;
23397c478bd9Sstevel@tonic-gate   for(i=0, k=nxDiv; i<NB; i++, k++){
23407c478bd9Sstevel@tonic-gate     if( k<pParent->nCell ){
23417c478bd9Sstevel@tonic-gate       idxDiv[i] = k;
23427c478bd9Sstevel@tonic-gate       apDiv[i] = pParent->apCell[k];
23437c478bd9Sstevel@tonic-gate       nDiv++;
23447c478bd9Sstevel@tonic-gate       pgnoOld[i] = SWAB32(pBt, apDiv[i]->h.leftChild);
23457c478bd9Sstevel@tonic-gate     }else if( k==pParent->nCell ){
23467c478bd9Sstevel@tonic-gate       pgnoOld[i] = SWAB32(pBt, pParent->u.hdr.rightChild);
23477c478bd9Sstevel@tonic-gate     }else{
23487c478bd9Sstevel@tonic-gate       break;
23497c478bd9Sstevel@tonic-gate     }
23507c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]);
23517c478bd9Sstevel@tonic-gate     if( rc ) goto balance_cleanup;
23527c478bd9Sstevel@tonic-gate     rc = initPage(pBt, apOld[i], pgnoOld[i], pParent);
23537c478bd9Sstevel@tonic-gate     if( rc ) goto balance_cleanup;
23547c478bd9Sstevel@tonic-gate     apOld[i]->idxParent = k;
23557c478bd9Sstevel@tonic-gate     nOld++;
23567c478bd9Sstevel@tonic-gate   }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate   /*
23597c478bd9Sstevel@tonic-gate   ** Set iCur to be the index in apCell[] of the cell that the cursor
23607c478bd9Sstevel@tonic-gate   ** is pointing to.  We will need this later on in order to keep the
23617c478bd9Sstevel@tonic-gate   ** cursor pointing at the same cell.  If pCur points to a page that
23627c478bd9Sstevel@tonic-gate   ** has no involvement with this rebalancing, then set iCur to a large
23637c478bd9Sstevel@tonic-gate   ** number so that the iCur==j tests always fail in the main cell
23647c478bd9Sstevel@tonic-gate   ** distribution loop below.
23657c478bd9Sstevel@tonic-gate   */
23667c478bd9Sstevel@tonic-gate   if( pCur ){
23677c478bd9Sstevel@tonic-gate     iCur = 0;
23687c478bd9Sstevel@tonic-gate     for(i=0; i<nOld; i++){
23697c478bd9Sstevel@tonic-gate       if( pCur->pPage==apOld[i] ){
23707c478bd9Sstevel@tonic-gate         iCur += pCur->idx;
23717c478bd9Sstevel@tonic-gate         break;
23727c478bd9Sstevel@tonic-gate       }
23737c478bd9Sstevel@tonic-gate       iCur += apOld[i]->nCell;
23747c478bd9Sstevel@tonic-gate       if( i<nOld-1 && pCur->pPage==pParent && pCur->idx==idxDiv[i] ){
23757c478bd9Sstevel@tonic-gate         break;
23767c478bd9Sstevel@tonic-gate       }
23777c478bd9Sstevel@tonic-gate       iCur++;
23787c478bd9Sstevel@tonic-gate     }
23797c478bd9Sstevel@tonic-gate     pOldCurPage = pCur->pPage;
23807c478bd9Sstevel@tonic-gate   }
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate   /*
23837c478bd9Sstevel@tonic-gate   ** Make copies of the content of pPage and its siblings into aOld[].
23847c478bd9Sstevel@tonic-gate   ** The rest of this function will use data from the copies rather
23857c478bd9Sstevel@tonic-gate   ** that the original pages since the original pages will be in the
23867c478bd9Sstevel@tonic-gate   ** process of being overwritten.
23877c478bd9Sstevel@tonic-gate   */
23887c478bd9Sstevel@tonic-gate   for(i=0; i<nOld; i++){
23897c478bd9Sstevel@tonic-gate     copyPage(&aOld[i], apOld[i]);
23907c478bd9Sstevel@tonic-gate   }
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate   /*
23937c478bd9Sstevel@tonic-gate   ** Load pointers to all cells on sibling pages and the divider cells
23947c478bd9Sstevel@tonic-gate   ** into the local apCell[] array.  Make copies of the divider cells
23957c478bd9Sstevel@tonic-gate   ** into aTemp[] and remove the the divider Cells from pParent.
23967c478bd9Sstevel@tonic-gate   */
23977c478bd9Sstevel@tonic-gate   nCell = 0;
23987c478bd9Sstevel@tonic-gate   for(i=0; i<nOld; i++){
23997c478bd9Sstevel@tonic-gate     MemPage *pOld = &aOld[i];
24007c478bd9Sstevel@tonic-gate     for(j=0; j<pOld->nCell; j++){
24017c478bd9Sstevel@tonic-gate       apCell[nCell] = pOld->apCell[j];
24027c478bd9Sstevel@tonic-gate       szCell[nCell] = cellSize(pBt, apCell[nCell]);
24037c478bd9Sstevel@tonic-gate       nCell++;
24047c478bd9Sstevel@tonic-gate     }
24057c478bd9Sstevel@tonic-gate     if( i<nOld-1 ){
24067c478bd9Sstevel@tonic-gate       szCell[nCell] = cellSize(pBt, apDiv[i]);
24077c478bd9Sstevel@tonic-gate       memcpy(&aTemp[i], apDiv[i], szCell[nCell]);
24087c478bd9Sstevel@tonic-gate       apCell[nCell] = &aTemp[i];
24097c478bd9Sstevel@tonic-gate       dropCell(pBt, pParent, nxDiv, szCell[nCell]);
24107c478bd9Sstevel@tonic-gate       assert( SWAB32(pBt, apCell[nCell]->h.leftChild)==pgnoOld[i] );
24117c478bd9Sstevel@tonic-gate       apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild;
24127c478bd9Sstevel@tonic-gate       nCell++;
24137c478bd9Sstevel@tonic-gate     }
24147c478bd9Sstevel@tonic-gate   }
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate   /*
24177c478bd9Sstevel@tonic-gate   ** Figure out the number of pages needed to hold all nCell cells.
24187c478bd9Sstevel@tonic-gate   ** Store this number in "k".  Also compute szNew[] which is the total
24197c478bd9Sstevel@tonic-gate   ** size of all cells on the i-th page and cntNew[] which is the index
2420*1da57d55SToomas Soome   ** in apCell[] of the cell that divides path i from path i+1.
24217c478bd9Sstevel@tonic-gate   ** cntNew[k] should equal nCell.
24227c478bd9Sstevel@tonic-gate   **
24237c478bd9Sstevel@tonic-gate   ** This little patch of code is critical for keeping the tree
2424*1da57d55SToomas Soome   ** balanced.
24257c478bd9Sstevel@tonic-gate   */
24267c478bd9Sstevel@tonic-gate   for(subtotal=k=i=0; i<nCell; i++){
24277c478bd9Sstevel@tonic-gate     subtotal += szCell[i];
24287c478bd9Sstevel@tonic-gate     if( subtotal > USABLE_SPACE ){
24297c478bd9Sstevel@tonic-gate       szNew[k] = subtotal - szCell[i];
24307c478bd9Sstevel@tonic-gate       cntNew[k] = i;
24317c478bd9Sstevel@tonic-gate       subtotal = 0;
24327c478bd9Sstevel@tonic-gate       k++;
24337c478bd9Sstevel@tonic-gate     }
24347c478bd9Sstevel@tonic-gate   }
24357c478bd9Sstevel@tonic-gate   szNew[k] = subtotal;
24367c478bd9Sstevel@tonic-gate   cntNew[k] = nCell;
24377c478bd9Sstevel@tonic-gate   k++;
24387c478bd9Sstevel@tonic-gate   for(i=k-1; i>0; i--){
24397c478bd9Sstevel@tonic-gate     while( szNew[i]<USABLE_SPACE/2 ){
24407c478bd9Sstevel@tonic-gate       cntNew[i-1]--;
24417c478bd9Sstevel@tonic-gate       assert( cntNew[i-1]>0 );
24427c478bd9Sstevel@tonic-gate       szNew[i] += szCell[cntNew[i-1]];
24437c478bd9Sstevel@tonic-gate       szNew[i-1] -= szCell[cntNew[i-1]-1];
24447c478bd9Sstevel@tonic-gate     }
24457c478bd9Sstevel@tonic-gate   }
24467c478bd9Sstevel@tonic-gate   assert( cntNew[0]>0 );
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate   /*
24497c478bd9Sstevel@tonic-gate   ** Allocate k new pages.  Reuse old pages where possible.
24507c478bd9Sstevel@tonic-gate   */
24517c478bd9Sstevel@tonic-gate   for(i=0; i<k; i++){
24527c478bd9Sstevel@tonic-gate     if( i<nOld ){
24537c478bd9Sstevel@tonic-gate       apNew[i] = apOld[i];
24547c478bd9Sstevel@tonic-gate       pgnoNew[i] = pgnoOld[i];
24557c478bd9Sstevel@tonic-gate       apOld[i] = 0;
24567c478bd9Sstevel@tonic-gate       rc = sqlitepager_write(apNew[i]);
24577c478bd9Sstevel@tonic-gate       if( rc ) goto balance_cleanup;
24587c478bd9Sstevel@tonic-gate     }else{
24597c478bd9Sstevel@tonic-gate       rc = allocatePage(pBt, &apNew[i], &pgnoNew[i], pgnoNew[i-1]);
24607c478bd9Sstevel@tonic-gate       if( rc ) goto balance_cleanup;
24617c478bd9Sstevel@tonic-gate     }
24627c478bd9Sstevel@tonic-gate     nNew++;
24637c478bd9Sstevel@tonic-gate     zeroPage(pBt, apNew[i]);
24647c478bd9Sstevel@tonic-gate     apNew[i]->isInit = 1;
24657c478bd9Sstevel@tonic-gate   }
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate   /* Free any old pages that were not reused as new pages.
24687c478bd9Sstevel@tonic-gate   */
24697c478bd9Sstevel@tonic-gate   while( i<nOld ){
24707c478bd9Sstevel@tonic-gate     rc = freePage(pBt, apOld[i], pgnoOld[i]);
24717c478bd9Sstevel@tonic-gate     if( rc ) goto balance_cleanup;
24727c478bd9Sstevel@tonic-gate     sqlitepager_unref(apOld[i]);
24737c478bd9Sstevel@tonic-gate     apOld[i] = 0;
24747c478bd9Sstevel@tonic-gate     i++;
24757c478bd9Sstevel@tonic-gate   }
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate   /*
24787c478bd9Sstevel@tonic-gate   ** Put the new pages in accending order.  This helps to
24797c478bd9Sstevel@tonic-gate   ** keep entries in the disk file in order so that a scan
24807c478bd9Sstevel@tonic-gate   ** of the table is a linear scan through the file.  That
24817c478bd9Sstevel@tonic-gate   ** in turn helps the operating system to deliver pages
24827c478bd9Sstevel@tonic-gate   ** from the disk more rapidly.
24837c478bd9Sstevel@tonic-gate   **
24847c478bd9Sstevel@tonic-gate   ** An O(n^2) insertion sort algorithm is used, but since
24857c478bd9Sstevel@tonic-gate   ** n is never more than NB (a small constant), that should
24867c478bd9Sstevel@tonic-gate   ** not be a problem.
24877c478bd9Sstevel@tonic-gate   **
24887c478bd9Sstevel@tonic-gate   ** When NB==3, this one optimization makes the database
24897c478bd9Sstevel@tonic-gate   ** about 25% faster for large insertions and deletions.
24907c478bd9Sstevel@tonic-gate   */
24917c478bd9Sstevel@tonic-gate   for(i=0; i<k-1; i++){
24927c478bd9Sstevel@tonic-gate     int minV = pgnoNew[i];
24937c478bd9Sstevel@tonic-gate     int minI = i;
24947c478bd9Sstevel@tonic-gate     for(j=i+1; j<k; j++){
24957c478bd9Sstevel@tonic-gate       if( pgnoNew[j]<(unsigned)minV ){
24967c478bd9Sstevel@tonic-gate         minI = j;
24977c478bd9Sstevel@tonic-gate         minV = pgnoNew[j];
24987c478bd9Sstevel@tonic-gate       }
24997c478bd9Sstevel@tonic-gate     }
25007c478bd9Sstevel@tonic-gate     if( minI>i ){
25017c478bd9Sstevel@tonic-gate       int t;
25027c478bd9Sstevel@tonic-gate       MemPage *pT;
25037c478bd9Sstevel@tonic-gate       t = pgnoNew[i];
25047c478bd9Sstevel@tonic-gate       pT = apNew[i];
25057c478bd9Sstevel@tonic-gate       pgnoNew[i] = pgnoNew[minI];
25067c478bd9Sstevel@tonic-gate       apNew[i] = apNew[minI];
25077c478bd9Sstevel@tonic-gate       pgnoNew[minI] = t;
25087c478bd9Sstevel@tonic-gate       apNew[minI] = pT;
25097c478bd9Sstevel@tonic-gate     }
25107c478bd9Sstevel@tonic-gate   }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate   /*
25137c478bd9Sstevel@tonic-gate   ** Evenly distribute the data in apCell[] across the new pages.
25147c478bd9Sstevel@tonic-gate   ** Insert divider cells into pParent as necessary.
25157c478bd9Sstevel@tonic-gate   */
25167c478bd9Sstevel@tonic-gate   j = 0;
25177c478bd9Sstevel@tonic-gate   for(i=0; i<nNew; i++){
25187c478bd9Sstevel@tonic-gate     MemPage *pNew = apNew[i];
25197c478bd9Sstevel@tonic-gate     while( j<cntNew[i] ){
25207c478bd9Sstevel@tonic-gate       assert( pNew->nFree>=szCell[j] );
25217c478bd9Sstevel@tonic-gate       if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; }
25227c478bd9Sstevel@tonic-gate       insertCell(pBt, pNew, pNew->nCell, apCell[j], szCell[j]);
25237c478bd9Sstevel@tonic-gate       j++;
25247c478bd9Sstevel@tonic-gate     }
25257c478bd9Sstevel@tonic-gate     assert( pNew->nCell>0 );
25267c478bd9Sstevel@tonic-gate     assert( !pNew->isOverfull );
25277c478bd9Sstevel@tonic-gate     relinkCellList(pBt, pNew);
25287c478bd9Sstevel@tonic-gate     if( i<nNew-1 && j<nCell ){
25297c478bd9Sstevel@tonic-gate       pNew->u.hdr.rightChild = apCell[j]->h.leftChild;
25307c478bd9Sstevel@tonic-gate       apCell[j]->h.leftChild = SWAB32(pBt, pgnoNew[i]);
25317c478bd9Sstevel@tonic-gate       if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; }
25327c478bd9Sstevel@tonic-gate       insertCell(pBt, pParent, nxDiv, apCell[j], szCell[j]);
25337c478bd9Sstevel@tonic-gate       j++;
25347c478bd9Sstevel@tonic-gate       nxDiv++;
25357c478bd9Sstevel@tonic-gate     }
25367c478bd9Sstevel@tonic-gate   }
25377c478bd9Sstevel@tonic-gate   assert( j==nCell );
25387c478bd9Sstevel@tonic-gate   apNew[nNew-1]->u.hdr.rightChild = aOld[nOld-1].u.hdr.rightChild;
25397c478bd9Sstevel@tonic-gate   if( nxDiv==pParent->nCell ){
25407c478bd9Sstevel@tonic-gate     pParent->u.hdr.rightChild = SWAB32(pBt, pgnoNew[nNew-1]);
25417c478bd9Sstevel@tonic-gate   }else{
25427c478bd9Sstevel@tonic-gate     pParent->apCell[nxDiv]->h.leftChild = SWAB32(pBt, pgnoNew[nNew-1]);
25437c478bd9Sstevel@tonic-gate   }
25447c478bd9Sstevel@tonic-gate   if( pCur ){
25457c478bd9Sstevel@tonic-gate     if( j<=iCur && pCur->pPage==pParent && pCur->idx>idxDiv[nOld-1] ){
25467c478bd9Sstevel@tonic-gate       assert( pCur->pPage==pOldCurPage );
25477c478bd9Sstevel@tonic-gate       pCur->idx += nNew - nOld;
25487c478bd9Sstevel@tonic-gate     }else{
25497c478bd9Sstevel@tonic-gate       assert( pOldCurPage!=0 );
25507c478bd9Sstevel@tonic-gate       sqlitepager_ref(pCur->pPage);
25517c478bd9Sstevel@tonic-gate       sqlitepager_unref(pOldCurPage);
25527c478bd9Sstevel@tonic-gate     }
25537c478bd9Sstevel@tonic-gate   }
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate   /*
25567c478bd9Sstevel@tonic-gate   ** Reparent children of all cells.
25577c478bd9Sstevel@tonic-gate   */
25587c478bd9Sstevel@tonic-gate   for(i=0; i<nNew; i++){
25597c478bd9Sstevel@tonic-gate     reparentChildPages(pBt, apNew[i]);
25607c478bd9Sstevel@tonic-gate   }
25617c478bd9Sstevel@tonic-gate   reparentChildPages(pBt, pParent);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate   /*
25647c478bd9Sstevel@tonic-gate   ** balance the parent page.
25657c478bd9Sstevel@tonic-gate   */
25667c478bd9Sstevel@tonic-gate   rc = balance(pBt, pParent, pCur);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate   /*
25697c478bd9Sstevel@tonic-gate   ** Cleanup before returning.
25707c478bd9Sstevel@tonic-gate   */
25717c478bd9Sstevel@tonic-gate balance_cleanup:
25727c478bd9Sstevel@tonic-gate   if( extraUnref ){
25737c478bd9Sstevel@tonic-gate     sqlitepager_unref(extraUnref);
25747c478bd9Sstevel@tonic-gate   }
25757c478bd9Sstevel@tonic-gate   for(i=0; i<nOld; i++){
25767c478bd9Sstevel@tonic-gate     if( apOld[i]!=0 && apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
25777c478bd9Sstevel@tonic-gate   }
25787c478bd9Sstevel@tonic-gate   for(i=0; i<nNew; i++){
25797c478bd9Sstevel@tonic-gate     sqlitepager_unref(apNew[i]);
25807c478bd9Sstevel@tonic-gate   }
25817c478bd9Sstevel@tonic-gate   if( pCur && pCur->pPage==0 ){
25827c478bd9Sstevel@tonic-gate     pCur->pPage = pParent;
25837c478bd9Sstevel@tonic-gate     pCur->idx = 0;
25847c478bd9Sstevel@tonic-gate   }else{
25857c478bd9Sstevel@tonic-gate     sqlitepager_unref(pParent);
25867c478bd9Sstevel@tonic-gate   }
25877c478bd9Sstevel@tonic-gate   return rc;
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate /*
25917c478bd9Sstevel@tonic-gate ** This routine checks all cursors that point to the same table
25927c478bd9Sstevel@tonic-gate ** as pCur points to.  If any of those cursors were opened with
25937c478bd9Sstevel@tonic-gate ** wrFlag==0 then this routine returns SQLITE_LOCKED.  If all
25947c478bd9Sstevel@tonic-gate ** cursors point to the same table were opened with wrFlag==1
25957c478bd9Sstevel@tonic-gate ** then this routine returns SQLITE_OK.
25967c478bd9Sstevel@tonic-gate **
2597*1da57d55SToomas Soome ** In addition to checking for read-locks (where a read-lock
25987c478bd9Sstevel@tonic-gate ** means a cursor opened with wrFlag==0) this routine also moves
2599*1da57d55SToomas Soome ** all cursors other than pCur so that they are pointing to the
2600*1da57d55SToomas Soome ** first Cell on root page.  This is necessary because an insert
26017c478bd9Sstevel@tonic-gate ** or delete might change the number of cells on a page or delete
2602*1da57d55SToomas Soome ** a page entirely and we do not want to leave any cursors
26037c478bd9Sstevel@tonic-gate ** pointing to non-existant pages or cells.
26047c478bd9Sstevel@tonic-gate */
checkReadLocks(BtCursor * pCur)26057c478bd9Sstevel@tonic-gate static int checkReadLocks(BtCursor *pCur){
26067c478bd9Sstevel@tonic-gate   BtCursor *p;
26077c478bd9Sstevel@tonic-gate   assert( pCur->wrFlag );
26087c478bd9Sstevel@tonic-gate   for(p=pCur->pShared; p!=pCur; p=p->pShared){
26097c478bd9Sstevel@tonic-gate     assert( p );
26107c478bd9Sstevel@tonic-gate     assert( p->pgnoRoot==pCur->pgnoRoot );
26117c478bd9Sstevel@tonic-gate     if( p->wrFlag==0 ) return SQLITE_LOCKED;
26127c478bd9Sstevel@tonic-gate     if( sqlitepager_pagenumber(p->pPage)!=p->pgnoRoot ){
26137c478bd9Sstevel@tonic-gate       moveToRoot(p);
26147c478bd9Sstevel@tonic-gate     }
26157c478bd9Sstevel@tonic-gate   }
26167c478bd9Sstevel@tonic-gate   return SQLITE_OK;
26177c478bd9Sstevel@tonic-gate }
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate /*
26207c478bd9Sstevel@tonic-gate ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
26217c478bd9Sstevel@tonic-gate ** and the data is given by (pData,nData).  The cursor is used only to
26227c478bd9Sstevel@tonic-gate ** define what database the record should be inserted into.  The cursor
26237c478bd9Sstevel@tonic-gate ** is left pointing at the new record.
26247c478bd9Sstevel@tonic-gate */
fileBtreeInsert(BtCursor * pCur,const void * pKey,int nKey,const void * pData,int nData)26257c478bd9Sstevel@tonic-gate static int fileBtreeInsert(
26267c478bd9Sstevel@tonic-gate   BtCursor *pCur,                /* Insert data into the table of this cursor */
26277c478bd9Sstevel@tonic-gate   const void *pKey, int nKey,    /* The key of the new record */
26287c478bd9Sstevel@tonic-gate   const void *pData, int nData   /* The data of the new record */
26297c478bd9Sstevel@tonic-gate ){
26307c478bd9Sstevel@tonic-gate   Cell newCell;
26317c478bd9Sstevel@tonic-gate   int rc;
26327c478bd9Sstevel@tonic-gate   int loc;
26337c478bd9Sstevel@tonic-gate   int szNew;
26347c478bd9Sstevel@tonic-gate   MemPage *pPage;
26357c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate   if( pCur->pPage==0 ){
26387c478bd9Sstevel@tonic-gate     return SQLITE_ABORT;  /* A rollback destroyed this cursor */
26397c478bd9Sstevel@tonic-gate   }
26407c478bd9Sstevel@tonic-gate   if( !pBt->inTrans || nKey+nData==0 ){
26417c478bd9Sstevel@tonic-gate     /* Must start a transaction before doing an insert */
26427c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
26437c478bd9Sstevel@tonic-gate   }
26447c478bd9Sstevel@tonic-gate   assert( !pBt->readOnly );
26457c478bd9Sstevel@tonic-gate   if( !pCur->wrFlag ){
26467c478bd9Sstevel@tonic-gate     return SQLITE_PERM;   /* Cursor not open for writing */
26477c478bd9Sstevel@tonic-gate   }
26487c478bd9Sstevel@tonic-gate   if( checkReadLocks(pCur) ){
26497c478bd9Sstevel@tonic-gate     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
26507c478bd9Sstevel@tonic-gate   }
26517c478bd9Sstevel@tonic-gate   rc = fileBtreeMoveto(pCur, pKey, nKey, &loc);
26527c478bd9Sstevel@tonic-gate   if( rc ) return rc;
26537c478bd9Sstevel@tonic-gate   pPage = pCur->pPage;
26547c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
26557c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pPage);
26567c478bd9Sstevel@tonic-gate   if( rc ) return rc;
26577c478bd9Sstevel@tonic-gate   rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData);
26587c478bd9Sstevel@tonic-gate   if( rc ) return rc;
26597c478bd9Sstevel@tonic-gate   szNew = cellSize(pBt, &newCell);
26607c478bd9Sstevel@tonic-gate   if( loc==0 ){
26617c478bd9Sstevel@tonic-gate     newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild;
26627c478bd9Sstevel@tonic-gate     rc = clearCell(pBt, pPage->apCell[pCur->idx]);
26637c478bd9Sstevel@tonic-gate     if( rc ) return rc;
26647c478bd9Sstevel@tonic-gate     dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pPage->apCell[pCur->idx]));
26657c478bd9Sstevel@tonic-gate   }else if( loc<0 && pPage->nCell>0 ){
26667c478bd9Sstevel@tonic-gate     assert( pPage->u.hdr.rightChild==0 );  /* Must be a leaf page */
26677c478bd9Sstevel@tonic-gate     pCur->idx++;
26687c478bd9Sstevel@tonic-gate   }else{
26697c478bd9Sstevel@tonic-gate     assert( pPage->u.hdr.rightChild==0 );  /* Must be a leaf page */
26707c478bd9Sstevel@tonic-gate   }
26717c478bd9Sstevel@tonic-gate   insertCell(pBt, pPage, pCur->idx, &newCell, szNew);
26727c478bd9Sstevel@tonic-gate   rc = balance(pCur->pBt, pPage, pCur);
26737c478bd9Sstevel@tonic-gate   /* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
26747c478bd9Sstevel@tonic-gate   /* fflush(stdout); */
26757c478bd9Sstevel@tonic-gate   pCur->eSkip = SKIP_INVALID;
26767c478bd9Sstevel@tonic-gate   return rc;
26777c478bd9Sstevel@tonic-gate }
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate /*
26807c478bd9Sstevel@tonic-gate ** Delete the entry that the cursor is pointing to.
26817c478bd9Sstevel@tonic-gate **
26827c478bd9Sstevel@tonic-gate ** The cursor is left pointing at either the next or the previous
2683*1da57d55SToomas Soome ** entry.  If the cursor is left pointing to the next entry, then
2684*1da57d55SToomas Soome ** the pCur->eSkip flag is set to SKIP_NEXT which forces the next call to
26857c478bd9Sstevel@tonic-gate ** sqliteBtreeNext() to be a no-op.  That way, you can always call
26867c478bd9Sstevel@tonic-gate ** sqliteBtreeNext() after a delete and the cursor will be left
26877c478bd9Sstevel@tonic-gate ** pointing to the first entry after the deleted entry.  Similarly,
26887c478bd9Sstevel@tonic-gate ** pCur->eSkip is set to SKIP_PREV is the cursor is left pointing to
26897c478bd9Sstevel@tonic-gate ** the entry prior to the deleted entry so that a subsequent call to
26907c478bd9Sstevel@tonic-gate ** sqliteBtreePrevious() will always leave the cursor pointing at the
26917c478bd9Sstevel@tonic-gate ** entry immediately before the one that was deleted.
26927c478bd9Sstevel@tonic-gate */
fileBtreeDelete(BtCursor * pCur)26937c478bd9Sstevel@tonic-gate static int fileBtreeDelete(BtCursor *pCur){
26947c478bd9Sstevel@tonic-gate   MemPage *pPage = pCur->pPage;
26957c478bd9Sstevel@tonic-gate   Cell *pCell;
26967c478bd9Sstevel@tonic-gate   int rc;
26977c478bd9Sstevel@tonic-gate   Pgno pgnoChild;
26987c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate   assert( pPage->isInit );
27017c478bd9Sstevel@tonic-gate   if( pCur->pPage==0 ){
27027c478bd9Sstevel@tonic-gate     return SQLITE_ABORT;  /* A rollback destroyed this cursor */
27037c478bd9Sstevel@tonic-gate   }
27047c478bd9Sstevel@tonic-gate   if( !pBt->inTrans ){
27057c478bd9Sstevel@tonic-gate     /* Must start a transaction before doing a delete */
27067c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
27077c478bd9Sstevel@tonic-gate   }
27087c478bd9Sstevel@tonic-gate   assert( !pBt->readOnly );
27097c478bd9Sstevel@tonic-gate   if( pCur->idx >= pPage->nCell ){
27107c478bd9Sstevel@tonic-gate     return SQLITE_ERROR;  /* The cursor is not pointing to anything */
27117c478bd9Sstevel@tonic-gate   }
27127c478bd9Sstevel@tonic-gate   if( !pCur->wrFlag ){
27137c478bd9Sstevel@tonic-gate     return SQLITE_PERM;   /* Did not open this cursor for writing */
27147c478bd9Sstevel@tonic-gate   }
27157c478bd9Sstevel@tonic-gate   if( checkReadLocks(pCur) ){
27167c478bd9Sstevel@tonic-gate     return SQLITE_LOCKED; /* The table pCur points to has a read lock */
27177c478bd9Sstevel@tonic-gate   }
27187c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pPage);
27197c478bd9Sstevel@tonic-gate   if( rc ) return rc;
27207c478bd9Sstevel@tonic-gate   pCell = pPage->apCell[pCur->idx];
27217c478bd9Sstevel@tonic-gate   pgnoChild = SWAB32(pBt, pCell->h.leftChild);
27227c478bd9Sstevel@tonic-gate   rc = clearCell(pBt, pCell);
27237c478bd9Sstevel@tonic-gate   if( rc ) return rc;
27247c478bd9Sstevel@tonic-gate   if( pgnoChild ){
27257c478bd9Sstevel@tonic-gate     /*
27267c478bd9Sstevel@tonic-gate     ** The entry we are about to delete is not a leaf so if we do not
27277c478bd9Sstevel@tonic-gate     ** do something we will leave a hole on an internal page.
27287c478bd9Sstevel@tonic-gate     ** We have to fill the hole by moving in a cell from a leaf.  The
27297c478bd9Sstevel@tonic-gate     ** next Cell after the one to be deleted is guaranteed to exist and
27307c478bd9Sstevel@tonic-gate     ** to be a leaf so we can use it.
27317c478bd9Sstevel@tonic-gate     */
27327c478bd9Sstevel@tonic-gate     BtCursor leafCur;
27337c478bd9Sstevel@tonic-gate     Cell *pNext;
27347c478bd9Sstevel@tonic-gate     int szNext;
27357c478bd9Sstevel@tonic-gate     int notUsed;
27367c478bd9Sstevel@tonic-gate     getTempCursor(pCur, &leafCur);
27377c478bd9Sstevel@tonic-gate     rc = fileBtreeNext(&leafCur, &notUsed);
27387c478bd9Sstevel@tonic-gate     if( rc!=SQLITE_OK ){
27397c478bd9Sstevel@tonic-gate       if( rc!=SQLITE_NOMEM ) rc = SQLITE_CORRUPT;
27407c478bd9Sstevel@tonic-gate       return rc;
27417c478bd9Sstevel@tonic-gate     }
27427c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(leafCur.pPage);
27437c478bd9Sstevel@tonic-gate     if( rc ) return rc;
27447c478bd9Sstevel@tonic-gate     dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
27457c478bd9Sstevel@tonic-gate     pNext = leafCur.pPage->apCell[leafCur.idx];
27467c478bd9Sstevel@tonic-gate     szNext = cellSize(pBt, pNext);
27477c478bd9Sstevel@tonic-gate     pNext->h.leftChild = SWAB32(pBt, pgnoChild);
27487c478bd9Sstevel@tonic-gate     insertCell(pBt, pPage, pCur->idx, pNext, szNext);
27497c478bd9Sstevel@tonic-gate     rc = balance(pBt, pPage, pCur);
27507c478bd9Sstevel@tonic-gate     if( rc ) return rc;
27517c478bd9Sstevel@tonic-gate     pCur->eSkip = SKIP_NEXT;
27527c478bd9Sstevel@tonic-gate     dropCell(pBt, leafCur.pPage, leafCur.idx, szNext);
27537c478bd9Sstevel@tonic-gate     rc = balance(pBt, leafCur.pPage, pCur);
27547c478bd9Sstevel@tonic-gate     releaseTempCursor(&leafCur);
27557c478bd9Sstevel@tonic-gate   }else{
27567c478bd9Sstevel@tonic-gate     dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
27577c478bd9Sstevel@tonic-gate     if( pCur->idx>=pPage->nCell ){
27587c478bd9Sstevel@tonic-gate       pCur->idx = pPage->nCell-1;
2759*1da57d55SToomas Soome       if( pCur->idx<0 ){
27607c478bd9Sstevel@tonic-gate         pCur->idx = 0;
27617c478bd9Sstevel@tonic-gate         pCur->eSkip = SKIP_NEXT;
27627c478bd9Sstevel@tonic-gate       }else{
27637c478bd9Sstevel@tonic-gate         pCur->eSkip = SKIP_PREV;
27647c478bd9Sstevel@tonic-gate       }
27657c478bd9Sstevel@tonic-gate     }else{
27667c478bd9Sstevel@tonic-gate       pCur->eSkip = SKIP_NEXT;
27677c478bd9Sstevel@tonic-gate     }
27687c478bd9Sstevel@tonic-gate     rc = balance(pBt, pPage, pCur);
27697c478bd9Sstevel@tonic-gate   }
27707c478bd9Sstevel@tonic-gate   return rc;
27717c478bd9Sstevel@tonic-gate }
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate /*
27747c478bd9Sstevel@tonic-gate ** Create a new BTree table.  Write into *piTable the page
27757c478bd9Sstevel@tonic-gate ** number for the root page of the new table.
27767c478bd9Sstevel@tonic-gate **
2777*1da57d55SToomas Soome ** In the current implementation, BTree tables and BTree indices are the
27787c478bd9Sstevel@tonic-gate ** the same.  In the future, we may change this so that BTree tables
27797c478bd9Sstevel@tonic-gate ** are restricted to having a 4-byte integer key and arbitrary data and
27807c478bd9Sstevel@tonic-gate ** BTree indices are restricted to having an arbitrary key and no data.
27817c478bd9Sstevel@tonic-gate ** But for now, this routine also serves to create indices.
27827c478bd9Sstevel@tonic-gate */
fileBtreeCreateTable(Btree * pBt,int * piTable)27837c478bd9Sstevel@tonic-gate static int fileBtreeCreateTable(Btree *pBt, int *piTable){
27847c478bd9Sstevel@tonic-gate   MemPage *pRoot;
27857c478bd9Sstevel@tonic-gate   Pgno pgnoRoot;
27867c478bd9Sstevel@tonic-gate   int rc;
27877c478bd9Sstevel@tonic-gate   if( !pBt->inTrans ){
27887c478bd9Sstevel@tonic-gate     /* Must start a transaction first */
27897c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
27907c478bd9Sstevel@tonic-gate   }
27917c478bd9Sstevel@tonic-gate   if( pBt->readOnly ){
27927c478bd9Sstevel@tonic-gate     return SQLITE_READONLY;
27937c478bd9Sstevel@tonic-gate   }
27947c478bd9Sstevel@tonic-gate   rc = allocatePage(pBt, &pRoot, &pgnoRoot, 0);
27957c478bd9Sstevel@tonic-gate   if( rc ) return rc;
27967c478bd9Sstevel@tonic-gate   assert( sqlitepager_iswriteable(pRoot) );
27977c478bd9Sstevel@tonic-gate   zeroPage(pBt, pRoot);
27987c478bd9Sstevel@tonic-gate   sqlitepager_unref(pRoot);
27997c478bd9Sstevel@tonic-gate   *piTable = (int)pgnoRoot;
28007c478bd9Sstevel@tonic-gate   return SQLITE_OK;
28017c478bd9Sstevel@tonic-gate }
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate /*
28047c478bd9Sstevel@tonic-gate ** Erase the given database page and all its children.  Return
28057c478bd9Sstevel@tonic-gate ** the page to the freelist.
28067c478bd9Sstevel@tonic-gate */
clearDatabasePage(Btree * pBt,Pgno pgno,int freePageFlag)28077c478bd9Sstevel@tonic-gate static int clearDatabasePage(Btree *pBt, Pgno pgno, int freePageFlag){
28087c478bd9Sstevel@tonic-gate   MemPage *pPage;
28097c478bd9Sstevel@tonic-gate   int rc;
28107c478bd9Sstevel@tonic-gate   Cell *pCell;
28117c478bd9Sstevel@tonic-gate   int idx;
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage);
28147c478bd9Sstevel@tonic-gate   if( rc ) return rc;
28157c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pPage);
28167c478bd9Sstevel@tonic-gate   if( rc ) return rc;
28177c478bd9Sstevel@tonic-gate   rc = initPage(pBt, pPage, pgno, 0);
28187c478bd9Sstevel@tonic-gate   if( rc ) return rc;
28197c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstCell);
28207c478bd9Sstevel@tonic-gate   while( idx>0 ){
28217c478bd9Sstevel@tonic-gate     pCell = (Cell*)&pPage->u.aDisk[idx];
28227c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, pCell->h.iNext);
28237c478bd9Sstevel@tonic-gate     if( pCell->h.leftChild ){
28247c478bd9Sstevel@tonic-gate       rc = clearDatabasePage(pBt, SWAB32(pBt, pCell->h.leftChild), 1);
28257c478bd9Sstevel@tonic-gate       if( rc ) return rc;
28267c478bd9Sstevel@tonic-gate     }
28277c478bd9Sstevel@tonic-gate     rc = clearCell(pBt, pCell);
28287c478bd9Sstevel@tonic-gate     if( rc ) return rc;
28297c478bd9Sstevel@tonic-gate   }
28307c478bd9Sstevel@tonic-gate   if( pPage->u.hdr.rightChild ){
28317c478bd9Sstevel@tonic-gate     rc = clearDatabasePage(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1);
28327c478bd9Sstevel@tonic-gate     if( rc ) return rc;
28337c478bd9Sstevel@tonic-gate   }
28347c478bd9Sstevel@tonic-gate   if( freePageFlag ){
28357c478bd9Sstevel@tonic-gate     rc = freePage(pBt, pPage, pgno);
28367c478bd9Sstevel@tonic-gate   }else{
28377c478bd9Sstevel@tonic-gate     zeroPage(pBt, pPage);
28387c478bd9Sstevel@tonic-gate   }
28397c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
28407c478bd9Sstevel@tonic-gate   return rc;
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate /*
28447c478bd9Sstevel@tonic-gate ** Delete all information from a single table in the database.
28457c478bd9Sstevel@tonic-gate */
fileBtreeClearTable(Btree * pBt,int iTable)28467c478bd9Sstevel@tonic-gate static int fileBtreeClearTable(Btree *pBt, int iTable){
28477c478bd9Sstevel@tonic-gate   int rc;
28487c478bd9Sstevel@tonic-gate   BtCursor *pCur;
28497c478bd9Sstevel@tonic-gate   if( !pBt->inTrans ){
28507c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
28517c478bd9Sstevel@tonic-gate   }
28527c478bd9Sstevel@tonic-gate   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
28537c478bd9Sstevel@tonic-gate     if( pCur->pgnoRoot==(Pgno)iTable ){
28547c478bd9Sstevel@tonic-gate       if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
28557c478bd9Sstevel@tonic-gate       moveToRoot(pCur);
28567c478bd9Sstevel@tonic-gate     }
28577c478bd9Sstevel@tonic-gate   }
28587c478bd9Sstevel@tonic-gate   rc = clearDatabasePage(pBt, (Pgno)iTable, 0);
28597c478bd9Sstevel@tonic-gate   if( rc ){
28607c478bd9Sstevel@tonic-gate     fileBtreeRollback(pBt);
28617c478bd9Sstevel@tonic-gate   }
28627c478bd9Sstevel@tonic-gate   return rc;
28637c478bd9Sstevel@tonic-gate }
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate /*
28667c478bd9Sstevel@tonic-gate ** Erase all information in a table and add the root of the table to
28677c478bd9Sstevel@tonic-gate ** the freelist.  Except, the root of the principle table (the one on
28687c478bd9Sstevel@tonic-gate ** page 2) is never added to the freelist.
28697c478bd9Sstevel@tonic-gate */
fileBtreeDropTable(Btree * pBt,int iTable)28707c478bd9Sstevel@tonic-gate static int fileBtreeDropTable(Btree *pBt, int iTable){
28717c478bd9Sstevel@tonic-gate   int rc;
28727c478bd9Sstevel@tonic-gate   MemPage *pPage;
28737c478bd9Sstevel@tonic-gate   BtCursor *pCur;
28747c478bd9Sstevel@tonic-gate   if( !pBt->inTrans ){
28757c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
28767c478bd9Sstevel@tonic-gate   }
28777c478bd9Sstevel@tonic-gate   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
28787c478bd9Sstevel@tonic-gate     if( pCur->pgnoRoot==(Pgno)iTable ){
28797c478bd9Sstevel@tonic-gate       return SQLITE_LOCKED;  /* Cannot drop a table that has a cursor */
28807c478bd9Sstevel@tonic-gate     }
28817c478bd9Sstevel@tonic-gate   }
28827c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
28837c478bd9Sstevel@tonic-gate   if( rc ) return rc;
28847c478bd9Sstevel@tonic-gate   rc = fileBtreeClearTable(pBt, iTable);
28857c478bd9Sstevel@tonic-gate   if( rc ) return rc;
28867c478bd9Sstevel@tonic-gate   if( iTable>2 ){
28877c478bd9Sstevel@tonic-gate     rc = freePage(pBt, pPage, iTable);
28887c478bd9Sstevel@tonic-gate   }else{
28897c478bd9Sstevel@tonic-gate     zeroPage(pBt, pPage);
28907c478bd9Sstevel@tonic-gate   }
28917c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
2892*1da57d55SToomas Soome   return rc;
28937c478bd9Sstevel@tonic-gate }
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate #if 0 /* UNTESTED */
28967c478bd9Sstevel@tonic-gate /*
28977c478bd9Sstevel@tonic-gate ** Copy all cell data from one database file into another.
28987c478bd9Sstevel@tonic-gate ** pages back the freelist.
28997c478bd9Sstevel@tonic-gate */
29007c478bd9Sstevel@tonic-gate static int copyCell(Btree *pBtFrom, BTree *pBtTo, Cell *pCell){
29017c478bd9Sstevel@tonic-gate   Pager *pFromPager = pBtFrom->pPager;
29027c478bd9Sstevel@tonic-gate   OverflowPage *pOvfl;
29037c478bd9Sstevel@tonic-gate   Pgno ovfl, nextOvfl;
29047c478bd9Sstevel@tonic-gate   Pgno *pPrev;
29057c478bd9Sstevel@tonic-gate   int rc = SQLITE_OK;
29067c478bd9Sstevel@tonic-gate   MemPage *pNew, *pPrevPg;
29077c478bd9Sstevel@tonic-gate   Pgno new;
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate   if( NKEY(pBtTo, pCell->h) + NDATA(pBtTo, pCell->h) <= MX_LOCAL_PAYLOAD ){
29107c478bd9Sstevel@tonic-gate     return SQLITE_OK;
29117c478bd9Sstevel@tonic-gate   }
29127c478bd9Sstevel@tonic-gate   pPrev = &pCell->ovfl;
29137c478bd9Sstevel@tonic-gate   pPrevPg = 0;
29147c478bd9Sstevel@tonic-gate   ovfl = SWAB32(pBtTo, pCell->ovfl);
29157c478bd9Sstevel@tonic-gate   while( ovfl && rc==SQLITE_OK ){
29167c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pFromPager, ovfl, (void**)&pOvfl);
29177c478bd9Sstevel@tonic-gate     if( rc ) return rc;
29187c478bd9Sstevel@tonic-gate     nextOvfl = SWAB32(pBtFrom, pOvfl->iNext);
29197c478bd9Sstevel@tonic-gate     rc = allocatePage(pBtTo, &pNew, &new, 0);
29207c478bd9Sstevel@tonic-gate     if( rc==SQLITE_OK ){
29217c478bd9Sstevel@tonic-gate       rc = sqlitepager_write(pNew);
29227c478bd9Sstevel@tonic-gate       if( rc==SQLITE_OK ){
29237c478bd9Sstevel@tonic-gate         memcpy(pNew, pOvfl, SQLITE_USABLE_SIZE);
29247c478bd9Sstevel@tonic-gate         *pPrev = SWAB32(pBtTo, new);
29257c478bd9Sstevel@tonic-gate         if( pPrevPg ){
29267c478bd9Sstevel@tonic-gate           sqlitepager_unref(pPrevPg);
29277c478bd9Sstevel@tonic-gate         }
29287c478bd9Sstevel@tonic-gate         pPrev = &pOvfl->iNext;
29297c478bd9Sstevel@tonic-gate         pPrevPg = pNew;
29307c478bd9Sstevel@tonic-gate       }
29317c478bd9Sstevel@tonic-gate     }
29327c478bd9Sstevel@tonic-gate     sqlitepager_unref(pOvfl);
29337c478bd9Sstevel@tonic-gate     ovfl = nextOvfl;
29347c478bd9Sstevel@tonic-gate   }
29357c478bd9Sstevel@tonic-gate   if( pPrevPg ){
29367c478bd9Sstevel@tonic-gate     sqlitepager_unref(pPrevPg);
29377c478bd9Sstevel@tonic-gate   }
29387c478bd9Sstevel@tonic-gate   return rc;
29397c478bd9Sstevel@tonic-gate }
29407c478bd9Sstevel@tonic-gate #endif
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate #if 0 /* UNTESTED */
29447c478bd9Sstevel@tonic-gate /*
29457c478bd9Sstevel@tonic-gate ** Copy a page of data from one database over to another.
29467c478bd9Sstevel@tonic-gate */
29477c478bd9Sstevel@tonic-gate static int copyDatabasePage(
29487c478bd9Sstevel@tonic-gate   Btree *pBtFrom,
29497c478bd9Sstevel@tonic-gate   Pgno pgnoFrom,
29507c478bd9Sstevel@tonic-gate   Btree *pBtTo,
29517c478bd9Sstevel@tonic-gate   Pgno *pTo
29527c478bd9Sstevel@tonic-gate ){
29537c478bd9Sstevel@tonic-gate   MemPage *pPageFrom, *pPage;
29547c478bd9Sstevel@tonic-gate   Pgno to;
29557c478bd9Sstevel@tonic-gate   int rc;
29567c478bd9Sstevel@tonic-gate   Cell *pCell;
29577c478bd9Sstevel@tonic-gate   int idx;
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBtFrom->pPager, pgno, (void**)&pPageFrom);
29607c478bd9Sstevel@tonic-gate   if( rc ) return rc;
29617c478bd9Sstevel@tonic-gate   rc = allocatePage(pBt, &pPage, pTo, 0);
29627c478bd9Sstevel@tonic-gate   if( rc==SQLITE_OK ){
29637c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(pPage);
29647c478bd9Sstevel@tonic-gate   }
29657c478bd9Sstevel@tonic-gate   if( rc==SQLITE_OK ){
29667c478bd9Sstevel@tonic-gate     memcpy(pPage, pPageFrom, SQLITE_USABLE_SIZE);
29677c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, pPage->u.hdr.firstCell);
29687c478bd9Sstevel@tonic-gate     while( idx>0 ){
29697c478bd9Sstevel@tonic-gate       pCell = (Cell*)&pPage->u.aDisk[idx];
29707c478bd9Sstevel@tonic-gate       idx = SWAB16(pBt, pCell->h.iNext);
29717c478bd9Sstevel@tonic-gate       if( pCell->h.leftChild ){
29727c478bd9Sstevel@tonic-gate         Pgno newChld;
29737c478bd9Sstevel@tonic-gate         rc = copyDatabasePage(pBtFrom, SWAB32(pBtFrom, pCell->h.leftChild),
29747c478bd9Sstevel@tonic-gate                               pBtTo, &newChld);
29757c478bd9Sstevel@tonic-gate         if( rc ) return rc;
29767c478bd9Sstevel@tonic-gate         pCell->h.leftChild = SWAB32(pBtFrom, newChld);
29777c478bd9Sstevel@tonic-gate       }
29787c478bd9Sstevel@tonic-gate       rc = copyCell(pBtFrom, pBtTo, pCell);
29797c478bd9Sstevel@tonic-gate       if( rc ) return rc;
29807c478bd9Sstevel@tonic-gate     }
29817c478bd9Sstevel@tonic-gate     if( pPage->u.hdr.rightChild ){
29827c478bd9Sstevel@tonic-gate       Pgno newChld;
2983*1da57d55SToomas Soome       rc = copyDatabasePage(pBtFrom, SWAB32(pBtFrom, pPage->u.hdr.rightChild),
29847c478bd9Sstevel@tonic-gate                             pBtTo, &newChld);
29857c478bd9Sstevel@tonic-gate       if( rc ) return rc;
29867c478bd9Sstevel@tonic-gate       pPage->u.hdr.rightChild = SWAB32(pBtTo, newChild);
29877c478bd9Sstevel@tonic-gate     }
29887c478bd9Sstevel@tonic-gate   }
29897c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
29907c478bd9Sstevel@tonic-gate   return rc;
29917c478bd9Sstevel@tonic-gate }
29927c478bd9Sstevel@tonic-gate #endif
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate /*
29957c478bd9Sstevel@tonic-gate ** Read the meta-information out of a database file.
29967c478bd9Sstevel@tonic-gate */
fileBtreeGetMeta(Btree * pBt,int * aMeta)29977c478bd9Sstevel@tonic-gate static int fileBtreeGetMeta(Btree *pBt, int *aMeta){
29987c478bd9Sstevel@tonic-gate   PageOne *pP1;
29997c478bd9Sstevel@tonic-gate   int rc;
30007c478bd9Sstevel@tonic-gate   int i;
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1);
30037c478bd9Sstevel@tonic-gate   if( rc ) return rc;
30047c478bd9Sstevel@tonic-gate   aMeta[0] = SWAB32(pBt, pP1->nFree);
30057c478bd9Sstevel@tonic-gate   for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
30067c478bd9Sstevel@tonic-gate     aMeta[i+1] = SWAB32(pBt, pP1->aMeta[i]);
30077c478bd9Sstevel@tonic-gate   }
30087c478bd9Sstevel@tonic-gate   sqlitepager_unref(pP1);
30097c478bd9Sstevel@tonic-gate   return SQLITE_OK;
30107c478bd9Sstevel@tonic-gate }
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate /*
30137c478bd9Sstevel@tonic-gate ** Write meta-information back into the database.
30147c478bd9Sstevel@tonic-gate */
fileBtreeUpdateMeta(Btree * pBt,int * aMeta)30157c478bd9Sstevel@tonic-gate static int fileBtreeUpdateMeta(Btree *pBt, int *aMeta){
30167c478bd9Sstevel@tonic-gate   PageOne *pP1;
30177c478bd9Sstevel@tonic-gate   int rc, i;
30187c478bd9Sstevel@tonic-gate   if( !pBt->inTrans ){
30197c478bd9Sstevel@tonic-gate     return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
30207c478bd9Sstevel@tonic-gate   }
30217c478bd9Sstevel@tonic-gate   pP1 = pBt->page1;
30227c478bd9Sstevel@tonic-gate   rc = sqlitepager_write(pP1);
3023*1da57d55SToomas Soome   if( rc ) return rc;
30247c478bd9Sstevel@tonic-gate   for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
30257c478bd9Sstevel@tonic-gate     pP1->aMeta[i] = SWAB32(pBt, aMeta[i+1]);
30267c478bd9Sstevel@tonic-gate   }
30277c478bd9Sstevel@tonic-gate   return SQLITE_OK;
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate /******************************************************************************
30317c478bd9Sstevel@tonic-gate ** The complete implementation of the BTree subsystem is above this line.
30327c478bd9Sstevel@tonic-gate ** All the code the follows is for testing and troubleshooting the BTree
30337c478bd9Sstevel@tonic-gate ** subsystem.  None of the code that follows is used during normal operation.
30347c478bd9Sstevel@tonic-gate ******************************************************************************/
30357c478bd9Sstevel@tonic-gate 
30367c478bd9Sstevel@tonic-gate /*
30377c478bd9Sstevel@tonic-gate ** Print a disassembly of the given page on standard output.  This routine
30387c478bd9Sstevel@tonic-gate ** is used for debugging and testing only.
30397c478bd9Sstevel@tonic-gate */
30407c478bd9Sstevel@tonic-gate #ifdef SQLITE_TEST
fileBtreePageDump(Btree * pBt,int pgno,int recursive)30417c478bd9Sstevel@tonic-gate static int fileBtreePageDump(Btree *pBt, int pgno, int recursive){
30427c478bd9Sstevel@tonic-gate   int rc;
30437c478bd9Sstevel@tonic-gate   MemPage *pPage;
30447c478bd9Sstevel@tonic-gate   int i, j;
30457c478bd9Sstevel@tonic-gate   int nFree;
30467c478bd9Sstevel@tonic-gate   u16 idx;
30477c478bd9Sstevel@tonic-gate   char range[20];
30487c478bd9Sstevel@tonic-gate   unsigned char payload[20];
30497c478bd9Sstevel@tonic-gate   rc = sqlitepager_get(pBt->pPager, (Pgno)pgno, (void**)&pPage);
30507c478bd9Sstevel@tonic-gate   if( rc ){
30517c478bd9Sstevel@tonic-gate     return rc;
30527c478bd9Sstevel@tonic-gate   }
30537c478bd9Sstevel@tonic-gate   if( recursive ) printf("PAGE %d:\n", pgno);
30547c478bd9Sstevel@tonic-gate   i = 0;
30557c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstCell);
30567c478bd9Sstevel@tonic-gate   while( idx>0 && idx<=SQLITE_USABLE_SIZE-MIN_CELL_SIZE ){
30577c478bd9Sstevel@tonic-gate     Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
30587c478bd9Sstevel@tonic-gate     int sz = cellSize(pBt, pCell);
30597c478bd9Sstevel@tonic-gate     sprintf(range,"%d..%d", idx, idx+sz-1);
30607c478bd9Sstevel@tonic-gate     sz = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h);
30617c478bd9Sstevel@tonic-gate     if( sz>sizeof(payload)-1 ) sz = sizeof(payload)-1;
30627c478bd9Sstevel@tonic-gate     memcpy(payload, pCell->aPayload, sz);
30637c478bd9Sstevel@tonic-gate     for(j=0; j<sz; j++){
30647c478bd9Sstevel@tonic-gate       if( payload[j]<0x20 || payload[j]>0x7f ) payload[j] = '.';
30657c478bd9Sstevel@tonic-gate     }
30667c478bd9Sstevel@tonic-gate     payload[sz] = 0;
30677c478bd9Sstevel@tonic-gate     printf(
30687c478bd9Sstevel@tonic-gate       "cell %2d: i=%-10s chld=%-4d nk=%-4d nd=%-4d payload=%s\n",
3069*1da57d55SToomas Soome       i, range, (int)pCell->h.leftChild,
30707c478bd9Sstevel@tonic-gate       NKEY(pBt, pCell->h), NDATA(pBt, pCell->h),
30717c478bd9Sstevel@tonic-gate       payload
30727c478bd9Sstevel@tonic-gate     );
30737c478bd9Sstevel@tonic-gate     if( pPage->isInit && pPage->apCell[i]!=pCell ){
30747c478bd9Sstevel@tonic-gate       printf("**** apCell[%d] does not match on prior entry ****\n", i);
30757c478bd9Sstevel@tonic-gate     }
30767c478bd9Sstevel@tonic-gate     i++;
30777c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, pCell->h.iNext);
30787c478bd9Sstevel@tonic-gate   }
30797c478bd9Sstevel@tonic-gate   if( idx!=0 ){
30807c478bd9Sstevel@tonic-gate     printf("ERROR: next cell index out of range: %d\n", idx);
30817c478bd9Sstevel@tonic-gate   }
30827c478bd9Sstevel@tonic-gate   printf("right_child: %d\n", SWAB32(pBt, pPage->u.hdr.rightChild));
30837c478bd9Sstevel@tonic-gate   nFree = 0;
30847c478bd9Sstevel@tonic-gate   i = 0;
30857c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstFree);
30867c478bd9Sstevel@tonic-gate   while( idx>0 && idx<SQLITE_USABLE_SIZE ){
30877c478bd9Sstevel@tonic-gate     FreeBlk *p = (FreeBlk*)&pPage->u.aDisk[idx];
30887c478bd9Sstevel@tonic-gate     sprintf(range,"%d..%d", idx, idx+p->iSize-1);
30897c478bd9Sstevel@tonic-gate     nFree += SWAB16(pBt, p->iSize);
30907c478bd9Sstevel@tonic-gate     printf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
30917c478bd9Sstevel@tonic-gate        i, range, SWAB16(pBt, p->iSize), nFree);
30927c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, p->iNext);
30937c478bd9Sstevel@tonic-gate     i++;
30947c478bd9Sstevel@tonic-gate   }
30957c478bd9Sstevel@tonic-gate   if( idx!=0 ){
30967c478bd9Sstevel@tonic-gate     printf("ERROR: next freeblock index out of range: %d\n", idx);
30977c478bd9Sstevel@tonic-gate   }
30987c478bd9Sstevel@tonic-gate   if( recursive && pPage->u.hdr.rightChild!=0 ){
30997c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, pPage->u.hdr.firstCell);
31007c478bd9Sstevel@tonic-gate     while( idx>0 && idx<SQLITE_USABLE_SIZE-MIN_CELL_SIZE ){
31017c478bd9Sstevel@tonic-gate       Cell *pCell = (Cell*)&pPage->u.aDisk[idx];
31027c478bd9Sstevel@tonic-gate       fileBtreePageDump(pBt, SWAB32(pBt, pCell->h.leftChild), 1);
31037c478bd9Sstevel@tonic-gate       idx = SWAB16(pBt, pCell->h.iNext);
31047c478bd9Sstevel@tonic-gate     }
31057c478bd9Sstevel@tonic-gate     fileBtreePageDump(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1);
31067c478bd9Sstevel@tonic-gate   }
31077c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
31087c478bd9Sstevel@tonic-gate   return SQLITE_OK;
31097c478bd9Sstevel@tonic-gate }
31107c478bd9Sstevel@tonic-gate #endif
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate #ifdef SQLITE_TEST
31137c478bd9Sstevel@tonic-gate /*
31147c478bd9Sstevel@tonic-gate ** Fill aResult[] with information about the entry and page that the
31157c478bd9Sstevel@tonic-gate ** cursor is pointing to.
3116*1da57d55SToomas Soome **
31177c478bd9Sstevel@tonic-gate **   aResult[0] =  The page number
31187c478bd9Sstevel@tonic-gate **   aResult[1] =  The entry number
31197c478bd9Sstevel@tonic-gate **   aResult[2] =  Total number of entries on this page
31207c478bd9Sstevel@tonic-gate **   aResult[3] =  Size of this entry
31217c478bd9Sstevel@tonic-gate **   aResult[4] =  Number of free bytes on this page
31227c478bd9Sstevel@tonic-gate **   aResult[5] =  Number of free blocks on the page
31237c478bd9Sstevel@tonic-gate **   aResult[6] =  Page number of the left child of this entry
31247c478bd9Sstevel@tonic-gate **   aResult[7] =  Page number of the right child for the whole page
31257c478bd9Sstevel@tonic-gate **
31267c478bd9Sstevel@tonic-gate ** This routine is used for testing and debugging only.
31277c478bd9Sstevel@tonic-gate */
fileBtreeCursorDump(BtCursor * pCur,int * aResult)31287c478bd9Sstevel@tonic-gate static int fileBtreeCursorDump(BtCursor *pCur, int *aResult){
31297c478bd9Sstevel@tonic-gate   int cnt, idx;
31307c478bd9Sstevel@tonic-gate   MemPage *pPage = pCur->pPage;
31317c478bd9Sstevel@tonic-gate   Btree *pBt = pCur->pBt;
31327c478bd9Sstevel@tonic-gate   aResult[0] = sqlitepager_pagenumber(pPage);
31337c478bd9Sstevel@tonic-gate   aResult[1] = pCur->idx;
31347c478bd9Sstevel@tonic-gate   aResult[2] = pPage->nCell;
31357c478bd9Sstevel@tonic-gate   if( pCur->idx>=0 && pCur->idx<pPage->nCell ){
31367c478bd9Sstevel@tonic-gate     aResult[3] = cellSize(pBt, pPage->apCell[pCur->idx]);
31377c478bd9Sstevel@tonic-gate     aResult[6] = SWAB32(pBt, pPage->apCell[pCur->idx]->h.leftChild);
31387c478bd9Sstevel@tonic-gate   }else{
31397c478bd9Sstevel@tonic-gate     aResult[3] = 0;
31407c478bd9Sstevel@tonic-gate     aResult[6] = 0;
31417c478bd9Sstevel@tonic-gate   }
31427c478bd9Sstevel@tonic-gate   aResult[4] = pPage->nFree;
31437c478bd9Sstevel@tonic-gate   cnt = 0;
31447c478bd9Sstevel@tonic-gate   idx = SWAB16(pBt, pPage->u.hdr.firstFree);
31457c478bd9Sstevel@tonic-gate   while( idx>0 && idx<SQLITE_USABLE_SIZE ){
31467c478bd9Sstevel@tonic-gate     cnt++;
31477c478bd9Sstevel@tonic-gate     idx = SWAB16(pBt, ((FreeBlk*)&pPage->u.aDisk[idx])->iNext);
31487c478bd9Sstevel@tonic-gate   }
31497c478bd9Sstevel@tonic-gate   aResult[5] = cnt;
31507c478bd9Sstevel@tonic-gate   aResult[7] = SWAB32(pBt, pPage->u.hdr.rightChild);
31517c478bd9Sstevel@tonic-gate   return SQLITE_OK;
31527c478bd9Sstevel@tonic-gate }
31537c478bd9Sstevel@tonic-gate #endif
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate /*
31567c478bd9Sstevel@tonic-gate ** Return the pager associated with a BTree.  This routine is used for
31577c478bd9Sstevel@tonic-gate ** testing and debugging only.
31587c478bd9Sstevel@tonic-gate */
fileBtreePager(Btree * pBt)31597c478bd9Sstevel@tonic-gate static Pager *fileBtreePager(Btree *pBt){
31607c478bd9Sstevel@tonic-gate   return pBt->pPager;
31617c478bd9Sstevel@tonic-gate }
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate /*
31647c478bd9Sstevel@tonic-gate ** This structure is passed around through all the sanity checking routines
31657c478bd9Sstevel@tonic-gate ** in order to keep track of some global state information.
31667c478bd9Sstevel@tonic-gate */
31677c478bd9Sstevel@tonic-gate typedef struct IntegrityCk IntegrityCk;
31687c478bd9Sstevel@tonic-gate struct IntegrityCk {
31697c478bd9Sstevel@tonic-gate   Btree *pBt;    /* The tree being checked out */
31707c478bd9Sstevel@tonic-gate   Pager *pPager; /* The associated pager.  Also accessible by pBt->pPager */
31717c478bd9Sstevel@tonic-gate   int nPage;     /* Number of pages in the database */
31727c478bd9Sstevel@tonic-gate   int *anRef;    /* Number of times each page is referenced */
31737c478bd9Sstevel@tonic-gate   char *zErrMsg; /* An error message.  NULL of no errors seen. */
31747c478bd9Sstevel@tonic-gate };
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate /*
31777c478bd9Sstevel@tonic-gate ** Append a message to the error message string.
31787c478bd9Sstevel@tonic-gate */
checkAppendMsg(IntegrityCk * pCheck,char * zMsg1,char * zMsg2)31797c478bd9Sstevel@tonic-gate static void checkAppendMsg(IntegrityCk *pCheck, char *zMsg1, char *zMsg2){
31807c478bd9Sstevel@tonic-gate   if( pCheck->zErrMsg ){
31817c478bd9Sstevel@tonic-gate     char *zOld = pCheck->zErrMsg;
31827c478bd9Sstevel@tonic-gate     pCheck->zErrMsg = 0;
31837c478bd9Sstevel@tonic-gate     sqliteSetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
31847c478bd9Sstevel@tonic-gate     sqliteFree(zOld);
31857c478bd9Sstevel@tonic-gate   }else{
31867c478bd9Sstevel@tonic-gate     sqliteSetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
31877c478bd9Sstevel@tonic-gate   }
31887c478bd9Sstevel@tonic-gate }
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate /*
31917c478bd9Sstevel@tonic-gate ** Add 1 to the reference count for page iPage.  If this is the second
31927c478bd9Sstevel@tonic-gate ** reference to the page, add an error message to pCheck->zErrMsg.
31937c478bd9Sstevel@tonic-gate ** Return 1 if there are 2 ore more references to the page and 0 if
31947c478bd9Sstevel@tonic-gate ** if this is the first reference to the page.
31957c478bd9Sstevel@tonic-gate **
31967c478bd9Sstevel@tonic-gate ** Also check that the page number is in bounds.
31977c478bd9Sstevel@tonic-gate */
checkRef(IntegrityCk * pCheck,int iPage,char * zContext)31987c478bd9Sstevel@tonic-gate static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
31997c478bd9Sstevel@tonic-gate   if( iPage==0 ) return 1;
32007c478bd9Sstevel@tonic-gate   if( iPage>pCheck->nPage || iPage<0 ){
32017c478bd9Sstevel@tonic-gate     char zBuf[100];
32027c478bd9Sstevel@tonic-gate     sprintf(zBuf, "invalid page number %d", iPage);
32037c478bd9Sstevel@tonic-gate     checkAppendMsg(pCheck, zContext, zBuf);
32047c478bd9Sstevel@tonic-gate     return 1;
32057c478bd9Sstevel@tonic-gate   }
32067c478bd9Sstevel@tonic-gate   if( pCheck->anRef[iPage]==1 ){
32077c478bd9Sstevel@tonic-gate     char zBuf[100];
32087c478bd9Sstevel@tonic-gate     sprintf(zBuf, "2nd reference to page %d", iPage);
32097c478bd9Sstevel@tonic-gate     checkAppendMsg(pCheck, zContext, zBuf);
32107c478bd9Sstevel@tonic-gate     return 1;
32117c478bd9Sstevel@tonic-gate   }
32127c478bd9Sstevel@tonic-gate   return  (pCheck->anRef[iPage]++)>1;
32137c478bd9Sstevel@tonic-gate }
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate /*
32167c478bd9Sstevel@tonic-gate ** Check the integrity of the freelist or of an overflow page list.
32177c478bd9Sstevel@tonic-gate ** Verify that the number of pages on the list is N.
32187c478bd9Sstevel@tonic-gate */
checkList(IntegrityCk * pCheck,int isFreeList,int iPage,int N,char * zContext)32197c478bd9Sstevel@tonic-gate static void checkList(
32207c478bd9Sstevel@tonic-gate   IntegrityCk *pCheck,  /* Integrity checking context */
32217c478bd9Sstevel@tonic-gate   int isFreeList,       /* True for a freelist.  False for overflow page list */
32227c478bd9Sstevel@tonic-gate   int iPage,            /* Page number for first page in the list */
32237c478bd9Sstevel@tonic-gate   int N,                /* Expected number of pages in the list */
32247c478bd9Sstevel@tonic-gate   char *zContext        /* Context for error messages */
32257c478bd9Sstevel@tonic-gate ){
32267c478bd9Sstevel@tonic-gate   int i;
32277c478bd9Sstevel@tonic-gate   char zMsg[100];
32287c478bd9Sstevel@tonic-gate   while( N-- > 0 ){
32297c478bd9Sstevel@tonic-gate     OverflowPage *pOvfl;
32307c478bd9Sstevel@tonic-gate     if( iPage<1 ){
32317c478bd9Sstevel@tonic-gate       sprintf(zMsg, "%d pages missing from overflow list", N+1);
32327c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zContext, zMsg);
32337c478bd9Sstevel@tonic-gate       break;
32347c478bd9Sstevel@tonic-gate     }
32357c478bd9Sstevel@tonic-gate     if( checkRef(pCheck, iPage, zContext) ) break;
32367c478bd9Sstevel@tonic-gate     if( sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
32377c478bd9Sstevel@tonic-gate       sprintf(zMsg, "failed to get page %d", iPage);
32387c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zContext, zMsg);
32397c478bd9Sstevel@tonic-gate       break;
32407c478bd9Sstevel@tonic-gate     }
32417c478bd9Sstevel@tonic-gate     if( isFreeList ){
32427c478bd9Sstevel@tonic-gate       FreelistInfo *pInfo = (FreelistInfo*)pOvfl->aPayload;
32437c478bd9Sstevel@tonic-gate       int n = SWAB32(pCheck->pBt, pInfo->nFree);
32447c478bd9Sstevel@tonic-gate       for(i=0; i<n; i++){
32457c478bd9Sstevel@tonic-gate         checkRef(pCheck, SWAB32(pCheck->pBt, pInfo->aFree[i]), zContext);
32467c478bd9Sstevel@tonic-gate       }
32477c478bd9Sstevel@tonic-gate       N -= n;
32487c478bd9Sstevel@tonic-gate     }
32497c478bd9Sstevel@tonic-gate     iPage = SWAB32(pCheck->pBt, pOvfl->iNext);
32507c478bd9Sstevel@tonic-gate     sqlitepager_unref(pOvfl);
32517c478bd9Sstevel@tonic-gate   }
32527c478bd9Sstevel@tonic-gate }
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate /*
32557c478bd9Sstevel@tonic-gate ** Return negative if zKey1<zKey2.
32567c478bd9Sstevel@tonic-gate ** Return zero if zKey1==zKey2.
32577c478bd9Sstevel@tonic-gate ** Return positive if zKey1>zKey2.
32587c478bd9Sstevel@tonic-gate */
keyCompare(const char * zKey1,int nKey1,const char * zKey2,int nKey2)32597c478bd9Sstevel@tonic-gate static int keyCompare(
32607c478bd9Sstevel@tonic-gate   const char *zKey1, int nKey1,
32617c478bd9Sstevel@tonic-gate   const char *zKey2, int nKey2
32627c478bd9Sstevel@tonic-gate ){
32637c478bd9Sstevel@tonic-gate   int min = nKey1>nKey2 ? nKey2 : nKey1;
32647c478bd9Sstevel@tonic-gate   int c = memcmp(zKey1, zKey2, min);
32657c478bd9Sstevel@tonic-gate   if( c==0 ){
32667c478bd9Sstevel@tonic-gate     c = nKey1 - nKey2;
32677c478bd9Sstevel@tonic-gate   }
32687c478bd9Sstevel@tonic-gate   return c;
32697c478bd9Sstevel@tonic-gate }
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate /*
32727c478bd9Sstevel@tonic-gate ** Do various sanity checks on a single page of a tree.  Return
32737c478bd9Sstevel@tonic-gate ** the tree depth.  Root pages return 0.  Parents of root pages
32747c478bd9Sstevel@tonic-gate ** return 1, and so forth.
3275*1da57d55SToomas Soome **
32767c478bd9Sstevel@tonic-gate ** These checks are done:
32777c478bd9Sstevel@tonic-gate **
32787c478bd9Sstevel@tonic-gate **      1.  Make sure that cells and freeblocks do not overlap
32797c478bd9Sstevel@tonic-gate **          but combine to completely cover the page.
32807c478bd9Sstevel@tonic-gate **      2.  Make sure cell keys are in order.
32817c478bd9Sstevel@tonic-gate **      3.  Make sure no key is less than or equal to zLowerBound.
32827c478bd9Sstevel@tonic-gate **      4.  Make sure no key is greater than or equal to zUpperBound.
32837c478bd9Sstevel@tonic-gate **      5.  Check the integrity of overflow pages.
32847c478bd9Sstevel@tonic-gate **      6.  Recursively call checkTreePage on all children.
32857c478bd9Sstevel@tonic-gate **      7.  Verify that the depth of all children is the same.
32867c478bd9Sstevel@tonic-gate **      8.  Make sure this page is at least 33% full or else it is
32877c478bd9Sstevel@tonic-gate **          the root of the tree.
32887c478bd9Sstevel@tonic-gate */
checkTreePage(IntegrityCk * pCheck,int iPage,MemPage * pParent,char * zParentContext,char * zLowerBound,int nLower,char * zUpperBound,int nUpper)32897c478bd9Sstevel@tonic-gate static int checkTreePage(
32907c478bd9Sstevel@tonic-gate   IntegrityCk *pCheck,  /* Context for the sanity check */
32917c478bd9Sstevel@tonic-gate   int iPage,            /* Page number of the page to check */
32927c478bd9Sstevel@tonic-gate   MemPage *pParent,     /* Parent page */
32937c478bd9Sstevel@tonic-gate   char *zParentContext, /* Parent context */
32947c478bd9Sstevel@tonic-gate   char *zLowerBound,    /* All keys should be greater than this, if not NULL */
32957c478bd9Sstevel@tonic-gate   int nLower,           /* Number of characters in zLowerBound */
32967c478bd9Sstevel@tonic-gate   char *zUpperBound,    /* All keys should be less than this, if not NULL */
32977c478bd9Sstevel@tonic-gate   int nUpper            /* Number of characters in zUpperBound */
32987c478bd9Sstevel@tonic-gate ){
32997c478bd9Sstevel@tonic-gate   MemPage *pPage;
33007c478bd9Sstevel@tonic-gate   int i, rc, depth, d2, pgno;
33017c478bd9Sstevel@tonic-gate   char *zKey1, *zKey2;
33027c478bd9Sstevel@tonic-gate   int nKey1, nKey2;
33037c478bd9Sstevel@tonic-gate   BtCursor cur;
33047c478bd9Sstevel@tonic-gate   Btree *pBt;
33057c478bd9Sstevel@tonic-gate   char zMsg[100];
33067c478bd9Sstevel@tonic-gate   char zContext[100];
33077c478bd9Sstevel@tonic-gate   char hit[SQLITE_USABLE_SIZE];
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate   /* Check that the page exists
33107c478bd9Sstevel@tonic-gate   */
33117c478bd9Sstevel@tonic-gate   cur.pBt = pBt = pCheck->pBt;
33127c478bd9Sstevel@tonic-gate   if( iPage==0 ) return 0;
33137c478bd9Sstevel@tonic-gate   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
33147c478bd9Sstevel@tonic-gate   sprintf(zContext, "On tree page %d: ", iPage);
33157c478bd9Sstevel@tonic-gate   if( (rc = sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pPage))!=0 ){
33167c478bd9Sstevel@tonic-gate     sprintf(zMsg, "unable to get the page. error code=%d", rc);
33177c478bd9Sstevel@tonic-gate     checkAppendMsg(pCheck, zContext, zMsg);
33187c478bd9Sstevel@tonic-gate     return 0;
33197c478bd9Sstevel@tonic-gate   }
33207c478bd9Sstevel@tonic-gate   if( (rc = initPage(pBt, pPage, (Pgno)iPage, pParent))!=0 ){
33217c478bd9Sstevel@tonic-gate     sprintf(zMsg, "initPage() returns error code %d", rc);
33227c478bd9Sstevel@tonic-gate     checkAppendMsg(pCheck, zContext, zMsg);
33237c478bd9Sstevel@tonic-gate     sqlitepager_unref(pPage);
33247c478bd9Sstevel@tonic-gate     return 0;
33257c478bd9Sstevel@tonic-gate   }
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate   /* Check out all the cells.
33287c478bd9Sstevel@tonic-gate   */
33297c478bd9Sstevel@tonic-gate   depth = 0;
33307c478bd9Sstevel@tonic-gate   if( zLowerBound ){
33317c478bd9Sstevel@tonic-gate     zKey1 = sqliteMalloc( nLower+1 );
33327c478bd9Sstevel@tonic-gate     memcpy(zKey1, zLowerBound, nLower);
33337c478bd9Sstevel@tonic-gate     zKey1[nLower] = 0;
33347c478bd9Sstevel@tonic-gate   }else{
33357c478bd9Sstevel@tonic-gate     zKey1 = 0;
33367c478bd9Sstevel@tonic-gate   }
33377c478bd9Sstevel@tonic-gate   nKey1 = nLower;
33387c478bd9Sstevel@tonic-gate   cur.pPage = pPage;
33397c478bd9Sstevel@tonic-gate   for(i=0; i<pPage->nCell; i++){
33407c478bd9Sstevel@tonic-gate     Cell *pCell = pPage->apCell[i];
33417c478bd9Sstevel@tonic-gate     int sz;
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate     /* Check payload overflow pages
33447c478bd9Sstevel@tonic-gate     */
33457c478bd9Sstevel@tonic-gate     nKey2 = NKEY(pBt, pCell->h);
33467c478bd9Sstevel@tonic-gate     sz = nKey2 + NDATA(pBt, pCell->h);
33477c478bd9Sstevel@tonic-gate     sprintf(zContext, "On page %d cell %d: ", iPage, i);
33487c478bd9Sstevel@tonic-gate     if( sz>MX_LOCAL_PAYLOAD ){
33497c478bd9Sstevel@tonic-gate       int nPage = (sz - MX_LOCAL_PAYLOAD + OVERFLOW_SIZE - 1)/OVERFLOW_SIZE;
33507c478bd9Sstevel@tonic-gate       checkList(pCheck, 0, SWAB32(pBt, pCell->ovfl), nPage, zContext);
33517c478bd9Sstevel@tonic-gate     }
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate     /* Check that keys are in the right order
33547c478bd9Sstevel@tonic-gate     */
33557c478bd9Sstevel@tonic-gate     cur.idx = i;
33567c478bd9Sstevel@tonic-gate     zKey2 = sqliteMallocRaw( nKey2+1 );
33577c478bd9Sstevel@tonic-gate     getPayload(&cur, 0, nKey2, zKey2);
33587c478bd9Sstevel@tonic-gate     if( zKey1 && keyCompare(zKey1, nKey1, zKey2, nKey2)>=0 ){
33597c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zContext, "Key is out of order");
33607c478bd9Sstevel@tonic-gate     }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate     /* Check sanity of left child page.
33637c478bd9Sstevel@tonic-gate     */
33647c478bd9Sstevel@tonic-gate     pgno = SWAB32(pBt, pCell->h.leftChild);
33657c478bd9Sstevel@tonic-gate     d2 = checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zKey2,nKey2);
33667c478bd9Sstevel@tonic-gate     if( i>0 && d2!=depth ){
33677c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zContext, "Child page depth differs");
33687c478bd9Sstevel@tonic-gate     }
33697c478bd9Sstevel@tonic-gate     depth = d2;
33707c478bd9Sstevel@tonic-gate     sqliteFree(zKey1);
33717c478bd9Sstevel@tonic-gate     zKey1 = zKey2;
33727c478bd9Sstevel@tonic-gate     nKey1 = nKey2;
33737c478bd9Sstevel@tonic-gate   }
33747c478bd9Sstevel@tonic-gate   pgno = SWAB32(pBt, pPage->u.hdr.rightChild);
33757c478bd9Sstevel@tonic-gate   sprintf(zContext, "On page %d at right child: ", iPage);
33767c478bd9Sstevel@tonic-gate   checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zUpperBound,nUpper);
33777c478bd9Sstevel@tonic-gate   sqliteFree(zKey1);
3378*1da57d55SToomas Soome 
33797c478bd9Sstevel@tonic-gate   /* Check for complete coverage of the page
33807c478bd9Sstevel@tonic-gate   */
33817c478bd9Sstevel@tonic-gate   memset(hit, 0, sizeof(hit));
33827c478bd9Sstevel@tonic-gate   memset(hit, 1, sizeof(PageHdr));
33837c478bd9Sstevel@tonic-gate   for(i=SWAB16(pBt, pPage->u.hdr.firstCell); i>0 && i<SQLITE_USABLE_SIZE; ){
33847c478bd9Sstevel@tonic-gate     Cell *pCell = (Cell*)&pPage->u.aDisk[i];
33857c478bd9Sstevel@tonic-gate     int j;
33867c478bd9Sstevel@tonic-gate     for(j=i+cellSize(pBt, pCell)-1; j>=i; j--) hit[j]++;
33877c478bd9Sstevel@tonic-gate     i = SWAB16(pBt, pCell->h.iNext);
33887c478bd9Sstevel@tonic-gate   }
33897c478bd9Sstevel@tonic-gate   for(i=SWAB16(pBt,pPage->u.hdr.firstFree); i>0 && i<SQLITE_USABLE_SIZE; ){
33907c478bd9Sstevel@tonic-gate     FreeBlk *pFBlk = (FreeBlk*)&pPage->u.aDisk[i];
33917c478bd9Sstevel@tonic-gate     int j;
33927c478bd9Sstevel@tonic-gate     for(j=i+SWAB16(pBt,pFBlk->iSize)-1; j>=i; j--) hit[j]++;
33937c478bd9Sstevel@tonic-gate     i = SWAB16(pBt,pFBlk->iNext);
33947c478bd9Sstevel@tonic-gate   }
33957c478bd9Sstevel@tonic-gate   for(i=0; i<SQLITE_USABLE_SIZE; i++){
33967c478bd9Sstevel@tonic-gate     if( hit[i]==0 ){
33977c478bd9Sstevel@tonic-gate       sprintf(zMsg, "Unused space at byte %d of page %d", i, iPage);
33987c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zMsg, 0);
33997c478bd9Sstevel@tonic-gate       break;
34007c478bd9Sstevel@tonic-gate     }else if( hit[i]>1 ){
34017c478bd9Sstevel@tonic-gate       sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage);
34027c478bd9Sstevel@tonic-gate       checkAppendMsg(pCheck, zMsg, 0);
34037c478bd9Sstevel@tonic-gate       break;
34047c478bd9Sstevel@tonic-gate     }
34057c478bd9Sstevel@tonic-gate   }
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate   /* Check that free space is kept to a minimum
34087c478bd9Sstevel@tonic-gate   */
34097c478bd9Sstevel@tonic-gate #if 0
34107c478bd9Sstevel@tonic-gate   if( pParent && pParent->nCell>2 && pPage->nFree>3*SQLITE_USABLE_SIZE/4 ){
34117c478bd9Sstevel@tonic-gate     sprintf(zMsg, "free space (%d) greater than max (%d)", pPage->nFree,
34127c478bd9Sstevel@tonic-gate        SQLITE_USABLE_SIZE/3);
34137c478bd9Sstevel@tonic-gate     checkAppendMsg(pCheck, zContext, zMsg);
34147c478bd9Sstevel@tonic-gate   }
34157c478bd9Sstevel@tonic-gate #endif
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate   sqlitepager_unref(pPage);
34187c478bd9Sstevel@tonic-gate   return depth;
34197c478bd9Sstevel@tonic-gate }
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate /*
34227c478bd9Sstevel@tonic-gate ** This routine does a complete check of the given BTree file.  aRoot[] is
34237c478bd9Sstevel@tonic-gate ** an array of pages numbers were each page number is the root page of
34247c478bd9Sstevel@tonic-gate ** a table.  nRoot is the number of entries in aRoot.
34257c478bd9Sstevel@tonic-gate **
34267c478bd9Sstevel@tonic-gate ** If everything checks out, this routine returns NULL.  If something is
34277c478bd9Sstevel@tonic-gate ** amiss, an error message is written into memory obtained from malloc()
34287c478bd9Sstevel@tonic-gate ** and a pointer to that error message is returned.  The calling function
34297c478bd9Sstevel@tonic-gate ** is responsible for freeing the error message when it is done.
34307c478bd9Sstevel@tonic-gate */
fileBtreeIntegrityCheck(Btree * pBt,int * aRoot,int nRoot)34317c478bd9Sstevel@tonic-gate char *fileBtreeIntegrityCheck(Btree *pBt, int *aRoot, int nRoot){
34327c478bd9Sstevel@tonic-gate   int i;
34337c478bd9Sstevel@tonic-gate   int nRef;
34347c478bd9Sstevel@tonic-gate   IntegrityCk sCheck;
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate   nRef = *sqlitepager_stats(pBt->pPager);
34377c478bd9Sstevel@tonic-gate   if( lockBtree(pBt)!=SQLITE_OK ){
34387c478bd9Sstevel@tonic-gate     return sqliteStrDup("Unable to acquire a read lock on the database");
34397c478bd9Sstevel@tonic-gate   }
34407c478bd9Sstevel@tonic-gate   sCheck.pBt = pBt;
34417c478bd9Sstevel@tonic-gate   sCheck.pPager = pBt->pPager;
34427c478bd9Sstevel@tonic-gate   sCheck.nPage = sqlitepager_pagecount(sCheck.pPager);
34437c478bd9Sstevel@tonic-gate   if( sCheck.nPage==0 ){
34447c478bd9Sstevel@tonic-gate     unlockBtreeIfUnused(pBt);
34457c478bd9Sstevel@tonic-gate     return 0;
34467c478bd9Sstevel@tonic-gate   }
34477c478bd9Sstevel@tonic-gate   sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
34487c478bd9Sstevel@tonic-gate   sCheck.anRef[1] = 1;
34497c478bd9Sstevel@tonic-gate   for(i=2; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
34507c478bd9Sstevel@tonic-gate   sCheck.zErrMsg = 0;
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate   /* Check the integrity of the freelist
34537c478bd9Sstevel@tonic-gate   */
34547c478bd9Sstevel@tonic-gate   checkList(&sCheck, 1, SWAB32(pBt, pBt->page1->freeList),
34557c478bd9Sstevel@tonic-gate             SWAB32(pBt, pBt->page1->nFree), "Main freelist: ");
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate   /* Check all the tables.
34587c478bd9Sstevel@tonic-gate   */
34597c478bd9Sstevel@tonic-gate   for(i=0; i<nRoot; i++){
34607c478bd9Sstevel@tonic-gate     if( aRoot[i]==0 ) continue;
34617c478bd9Sstevel@tonic-gate     checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0);
34627c478bd9Sstevel@tonic-gate   }
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate   /* Make sure every page in the file is referenced
34657c478bd9Sstevel@tonic-gate   */
34667c478bd9Sstevel@tonic-gate   for(i=1; i<=sCheck.nPage; i++){
34677c478bd9Sstevel@tonic-gate     if( sCheck.anRef[i]==0 ){
34687c478bd9Sstevel@tonic-gate       char zBuf[100];
34697c478bd9Sstevel@tonic-gate       sprintf(zBuf, "Page %d is never used", i);
34707c478bd9Sstevel@tonic-gate       checkAppendMsg(&sCheck, zBuf, 0);
34717c478bd9Sstevel@tonic-gate     }
34727c478bd9Sstevel@tonic-gate   }
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate   /* Make sure this analysis did not leave any unref() pages
34757c478bd9Sstevel@tonic-gate   */
34767c478bd9Sstevel@tonic-gate   unlockBtreeIfUnused(pBt);
34777c478bd9Sstevel@tonic-gate   if( nRef != *sqlitepager_stats(pBt->pPager) ){
34787c478bd9Sstevel@tonic-gate     char zBuf[100];
3479*1da57d55SToomas Soome     sprintf(zBuf,
34807c478bd9Sstevel@tonic-gate       "Outstanding page count goes from %d to %d during this analysis",
34817c478bd9Sstevel@tonic-gate       nRef, *sqlitepager_stats(pBt->pPager)
34827c478bd9Sstevel@tonic-gate     );
34837c478bd9Sstevel@tonic-gate     checkAppendMsg(&sCheck, zBuf, 0);
34847c478bd9Sstevel@tonic-gate   }
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate   /* Clean  up and report errors.
34877c478bd9Sstevel@tonic-gate   */
34887c478bd9Sstevel@tonic-gate   sqliteFree(sCheck.anRef);
34897c478bd9Sstevel@tonic-gate   return sCheck.zErrMsg;
34907c478bd9Sstevel@tonic-gate }
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate /*
34937c478bd9Sstevel@tonic-gate ** Return the full pathname of the underlying database file.
34947c478bd9Sstevel@tonic-gate */
fileBtreeGetFilename(Btree * pBt)34957c478bd9Sstevel@tonic-gate static const char *fileBtreeGetFilename(Btree *pBt){
34967c478bd9Sstevel@tonic-gate   assert( pBt->pPager!=0 );
34977c478bd9Sstevel@tonic-gate   return sqlitepager_filename(pBt->pPager);
34987c478bd9Sstevel@tonic-gate }
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate /*
35017c478bd9Sstevel@tonic-gate ** Copy the complete content of pBtFrom into pBtTo.  A transaction
35027c478bd9Sstevel@tonic-gate ** must be active for both files.
35037c478bd9Sstevel@tonic-gate **
35047c478bd9Sstevel@tonic-gate ** The size of file pBtFrom may be reduced by this operation.
35057c478bd9Sstevel@tonic-gate ** If anything goes wrong, the transaction on pBtFrom is rolled back.
35067c478bd9Sstevel@tonic-gate */
fileBtreeCopyFile(Btree * pBtTo,Btree * pBtFrom)35077c478bd9Sstevel@tonic-gate static int fileBtreeCopyFile(Btree *pBtTo, Btree *pBtFrom){
35087c478bd9Sstevel@tonic-gate   int rc = SQLITE_OK;
35097c478bd9Sstevel@tonic-gate   Pgno i, nPage, nToPage;
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate   if( !pBtTo->inTrans || !pBtFrom->inTrans ) return SQLITE_ERROR;
35127c478bd9Sstevel@tonic-gate   if( pBtTo->needSwab!=pBtFrom->needSwab ) return SQLITE_ERROR;
35137c478bd9Sstevel@tonic-gate   if( pBtTo->pCursor ) return SQLITE_BUSY;
35147c478bd9Sstevel@tonic-gate   memcpy(pBtTo->page1, pBtFrom->page1, SQLITE_USABLE_SIZE);
35157c478bd9Sstevel@tonic-gate   rc = sqlitepager_overwrite(pBtTo->pPager, 1, pBtFrom->page1);
35167c478bd9Sstevel@tonic-gate   nToPage = sqlitepager_pagecount(pBtTo->pPager);
35177c478bd9Sstevel@tonic-gate   nPage = sqlitepager_pagecount(pBtFrom->pPager);
35187c478bd9Sstevel@tonic-gate   for(i=2; rc==SQLITE_OK && i<=nPage; i++){
35197c478bd9Sstevel@tonic-gate     void *pPage;
35207c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBtFrom->pPager, i, &pPage);
35217c478bd9Sstevel@tonic-gate     if( rc ) break;
35227c478bd9Sstevel@tonic-gate     rc = sqlitepager_overwrite(pBtTo->pPager, i, pPage);
35237c478bd9Sstevel@tonic-gate     if( rc ) break;
35247c478bd9Sstevel@tonic-gate     sqlitepager_unref(pPage);
35257c478bd9Sstevel@tonic-gate   }
35267c478bd9Sstevel@tonic-gate   for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
35277c478bd9Sstevel@tonic-gate     void *pPage;
35287c478bd9Sstevel@tonic-gate     rc = sqlitepager_get(pBtTo->pPager, i, &pPage);
35297c478bd9Sstevel@tonic-gate     if( rc ) break;
35307c478bd9Sstevel@tonic-gate     rc = sqlitepager_write(pPage);
35317c478bd9Sstevel@tonic-gate     sqlitepager_unref(pPage);
35327c478bd9Sstevel@tonic-gate     sqlitepager_dont_write(pBtTo->pPager, i);
35337c478bd9Sstevel@tonic-gate   }
35347c478bd9Sstevel@tonic-gate   if( !rc && nPage<nToPage ){
35357c478bd9Sstevel@tonic-gate     rc = sqlitepager_truncate(pBtTo->pPager, nPage);
35367c478bd9Sstevel@tonic-gate   }
35377c478bd9Sstevel@tonic-gate   if( rc ){
35387c478bd9Sstevel@tonic-gate     fileBtreeRollback(pBtTo);
35397c478bd9Sstevel@tonic-gate   }
3540*1da57d55SToomas Soome   return rc;
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate /*
35447c478bd9Sstevel@tonic-gate ** The following tables contain pointers to all of the interface
35457c478bd9Sstevel@tonic-gate ** routines for this implementation of the B*Tree backend.  To
35467c478bd9Sstevel@tonic-gate ** substitute a different implemention of the backend, one has merely
35477c478bd9Sstevel@tonic-gate ** to provide pointers to alternative functions in similar tables.
35487c478bd9Sstevel@tonic-gate */
35497c478bd9Sstevel@tonic-gate static BtOps sqliteBtreeOps = {
35507c478bd9Sstevel@tonic-gate     fileBtreeClose,
35517c478bd9Sstevel@tonic-gate     fileBtreeSetCacheSize,
35527c478bd9Sstevel@tonic-gate     fileBtreeSetSafetyLevel,
35537c478bd9Sstevel@tonic-gate     fileBtreeBeginTrans,
35547c478bd9Sstevel@tonic-gate     fileBtreeCommit,
35557c478bd9Sstevel@tonic-gate     fileBtreeRollback,
35567c478bd9Sstevel@tonic-gate     fileBtreeBeginCkpt,
35577c478bd9Sstevel@tonic-gate     fileBtreeCommitCkpt,
35587c478bd9Sstevel@tonic-gate     fileBtreeRollbackCkpt,
35597c478bd9Sstevel@tonic-gate     fileBtreeCreateTable,
35607c478bd9Sstevel@tonic-gate     fileBtreeCreateTable,  /* Really sqliteBtreeCreateIndex() */
35617c478bd9Sstevel@tonic-gate     fileBtreeDropTable,
35627c478bd9Sstevel@tonic-gate     fileBtreeClearTable,
35637c478bd9Sstevel@tonic-gate     fileBtreeCursor,
35647c478bd9Sstevel@tonic-gate     fileBtreeGetMeta,
35657c478bd9Sstevel@tonic-gate     fileBtreeUpdateMeta,
35667c478bd9Sstevel@tonic-gate     fileBtreeIntegrityCheck,
35677c478bd9Sstevel@tonic-gate     fileBtreeGetFilename,
35687c478bd9Sstevel@tonic-gate     fileBtreeCopyFile,
35697c478bd9Sstevel@tonic-gate     fileBtreePager,
35707c478bd9Sstevel@tonic-gate #ifdef SQLITE_TEST
35717c478bd9Sstevel@tonic-gate     fileBtreePageDump,
35727c478bd9Sstevel@tonic-gate #endif
35737c478bd9Sstevel@tonic-gate };
35747c478bd9Sstevel@tonic-gate static BtCursorOps sqliteBtreeCursorOps = {
35757c478bd9Sstevel@tonic-gate     fileBtreeMoveto,
35767c478bd9Sstevel@tonic-gate     fileBtreeDelete,
35777c478bd9Sstevel@tonic-gate     fileBtreeInsert,
35787c478bd9Sstevel@tonic-gate     fileBtreeFirst,
35797c478bd9Sstevel@tonic-gate     fileBtreeLast,
35807c478bd9Sstevel@tonic-gate     fileBtreeNext,
35817c478bd9Sstevel@tonic-gate     fileBtreePrevious,
35827c478bd9Sstevel@tonic-gate     fileBtreeKeySize,
35837c478bd9Sstevel@tonic-gate     fileBtreeKey,
35847c478bd9Sstevel@tonic-gate     fileBtreeKeyCompare,
35857c478bd9Sstevel@tonic-gate     fileBtreeDataSize,
35867c478bd9Sstevel@tonic-gate     fileBtreeData,
35877c478bd9Sstevel@tonic-gate     fileBtreeCloseCursor,
35887c478bd9Sstevel@tonic-gate #ifdef SQLITE_TEST
35897c478bd9Sstevel@tonic-gate     fileBtreeCursorDump,
35907c478bd9Sstevel@tonic-gate #endif
35917c478bd9Sstevel@tonic-gate };
3592