btree.c revision c5c4113dfcabb1eed3d4bdf7609de5170027a794
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
** 2001 September 15
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.103 2004/03/10 13:42:38 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
**
** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
** "Sorting And Searching", pages 473-480. Addison-Wesley
** Publishing Company, Reading, Massachusetts.
**
** The basic idea is that each page of the file contains N database
** entries and N+1 pointers to subpages.
**
** ----------------------------------------------------------------
** | Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N) | Ptr(N+1) |
** ----------------------------------------------------------------
**
** All of the keys on the page that Ptr(0) points to have values less
** than Key(0). All of the keys on page Ptr(1) and its subpages have
** values greater than Key(0) and less than Key(1). All of the keys
** on Ptr(N+1) and its subpages have values greater than Key(N). And
** so forth.
**
** Finding a particular key requires reading O(log(M)) pages from the
** disk where M is the number of entries in the tree.
**
** In this implementation, a single file can hold one or more separate
** BTrees. Each BTree is identified by the index of its root page. The
** key and data for any entry are combined to form the "payload". Up to
** MX_LOCAL_PAYLOAD bytes of payload can be carried directly on the
** database page. If the payload is larger than MX_LOCAL_PAYLOAD bytes
** then surplus bytes are stored on overflow pages. The payload for an
** entry and the preceding pointer are combined to form a "Cell". Each
** page has a small header which contains the Ptr(N+1) pointer.
**
** The first page of the file contains a magic string used to verify that
** the file really is a valid BTree database, a pointer to a list of unused
** pages in the file, and some meta information. The root of the first
** BTree begins on page 2 of the file. (Pages are numbered beginning with
** 1, not 0.) Thus a minimum database contains 2 pages.
*/
#include "sqliteInt.h"
#include "pager.h"
#include "btree.h"
#include <assert.h>
/* Forward declarations */
static BtOps sqliteBtreeOps;
static BtCursorOps sqliteBtreeCursorOps;
/*
** Macros used for byteswapping. B is a pointer to the Btree
** structure. This is needed to access the Btree.needSwab boolean
** in order to tell if byte swapping is needed or not.
** X is an unsigned integer. SWAB16 byte swaps a 16-bit integer.
** SWAB32 byteswaps a 32-bit integer.
*/
#define SWAB_ADD(B,X,A) \
/*
** The following global variable - available only if SQLITE_TEST is
** defined - is used to determine whether new databases are created in
** native byte order or in non-native byte order. Non-native byte order
** databases are created for testing purposes only. Under normal operation,
** only native byte-order databases should be created, but we should be
** able to read or write existing databases regardless of the byteorder.
*/
#ifdef SQLITE_TEST
int btree_native_byte_order = 1;
#else
# define btree_native_byte_order 1
#endif
/*
** Forward declarations of structures used only in this file.
*/
typedef struct OverflowPage OverflowPage;
typedef struct FreelistInfo FreelistInfo;
/*
** All structures on a database page are aligned to 4-byte boundries.
** This routine rounds up a number of bytes to the next multiple of 4.
**
** This might need to change for computer architectures that require
** and 8-byte alignment boundry for structures.
*/
/*
** This is a magic string that appears at the beginning of every
** SQLite database in order to identify the file as a real database.
*/
static const char zMagicHeader[] =
"** This file contains an SQLite 2.1 database **";
#define MAGIC_SIZE (sizeof(zMagicHeader))
/*
** This is a magic integer also used to test the integrity of the database
** file. This integer is used in addition to the string above so that
** if the file is written on a little-endian architecture and read
** on a big-endian architectures (or vice versa) we can detect the
** problem.
**
** The number used was obtained at random and has no special
** significance other than the fact that it represents a different
** integer on little-endian and big-endian machines.
*/
#define MAGIC 0xdae37528
/*
** The first page of the database file contains a magic header string
** to identify the file as an SQLite database file. It also contains
** a pointer to the first free page of the file. Page 2 contains the
** root of the principle BTree. The file might contain other BTrees
** rooted on pages above 2.
**
** The first page also contains SQLITE_N_BTREE_META integers that
** can be used by higher-level routines.
**
** Remember that pages are numbered beginning with 1. (See pager.c
** for additional information.) Page 0 does not exist and a page
** number of 0 is used to mean "no such page".
*/
struct PageOne {
int iMagic; /* Integer to verify correct byte order */
int nFree; /* Number of pages on the free list */
};
/*
** Each database page has a header that is an instance of this
** structure.
**
** PageHdr.firstFree is 0 if there is no free space on this page.
** Otherwise, PageHdr.firstFree is the index in MemPage.u.aDisk[] of a
** FreeBlk structure that describes the first block of free space.
** All free space is defined by a linked list of FreeBlk structures.
**
** Data is stored in a linked list of Cell structures. PageHdr.firstCell
** is the index into MemPage.u.aDisk[] of the first cell on the page. The
** Cells are kept in sorted order.
**
** A Cell contains all information about a database entry and a pointer
** to a child page that contains other entries less than itself. In
** other words, the i-th Cell contains both Ptr(i) and Key(i). The
** right-most pointer of the page is contained in PageHdr.rightChild.
*/
struct PageHdr {
};
/*
** Entries on a page of the database are called "Cells". Each Cell
** has a header and data. This structure defines the header. The
** key and data (collectively the "payload") follow this header on
** the database page.
**
** A definition of the complete Cell structure is given below. The
** header for the cell must be defined first in order to do some
** of the sizing #defines that follow.
*/
struct CellHdr {
};
/*
** The key and data size are split into a lower 16-bit segment and an
** upper 8-bit segment in order to pack them together into a smaller
** space. The following macros reassembly a key or data size back
** into an integer.
*/
/*
** The minimum size of a complete Cell. The Cell must contain a header
** and at least 4 bytes of payload.
*/
/*
** The maximum number of database entries that can be held in a single
** page of the database.
*/
/*
** The amount of usable space on a single page of the BTree. This is the
** page size minus the overhead of the page header.
*/
/*
** The maximum amount of payload (in bytes) that can be stored locally for
** a database entry. If the entry contains more data than this, the
** extra goes onto overflow pages.
**
** This number is chosen so that at least 4 cells will fit on every page.
*/
/*
** Data on a database page is stored as a linked list of Cell structures.
** Both the key and the data are stored in aPayload[]. The key always comes
** first. The aPayload[] field grows as necessary to hold the key and data,
** up to a maximum of MX_LOCAL_PAYLOAD bytes. If the size of the key and
** data combined exceeds MX_LOCAL_PAYLOAD bytes, then Cell.ovfl is the
** page number of the first overflow page.
**
** Though this structure is fixed in size, the Cell on the database
** page varies in size. Every cell has a CellHdr and at least 4 bytes
** of payload space. Additional payload bytes (up to the maximum of
** MX_LOCAL_PAYLOAD) and the Cell.ovfl value are allocated only as
** needed.
*/
struct Cell {
CellHdr h; /* The cell header */
};
/*
** Free space on a page is remembered using a linked list of the FreeBlk
** structures. Space on a database page is allocated in increments of
** at least 4 bytes and is always aligned to a 4-byte boundry. The
** linked list of FreeBlks is always kept in order by address.
*/
struct FreeBlk {
};
/*
** The number of bytes of payload that will fit on a single overflow page.
*/
/*
** When the key and data for a single entry in the BTree will not fit in
** the MX_LOCAL_PAYLOAD bytes of space available on the database page,
** then all extra bytes are written to a linked list of overflow pages.
** Each overflow page is an instance of the following structure.
**
** Unused pages in the database are also represented by instances of
** the OverflowPage structure. The PageOne.freeList field is the
** page number of the first page in a linked list of unused database
** pages.
*/
struct OverflowPage {
char aPayload[OVERFLOW_SIZE];
};
/*
** The PageOne.freeList field points to a linked list of overflow pages
** hold information about free pages. The aPayload section of each
** overflow page contains an instance of the following structure. The
** aFree[] array holds the page number of nFree unused pages in the disk
** file.
*/
struct FreelistInfo {
int nFree;
};
/*
** For every page in the database file, an instance of the following structure
** is stored in memory. The u.aDisk[] array contains the raw bits read from
** the disk. The rest is auxiliary information held in memory only. The
** auxiliary info is only valid for regular database pages - it is not
** used for overflow pages and pages on the freelist.
**
** Of particular interest in the auxiliary info is the apCell[] entry. Each
** apCell[] entry is a pointer to a Cell structure in u.aDisk[]. The cells are
** put in this array so that they can be accessed in constant time, rather
** than in linear time which would be needed if we had to walk the linked
** list on every access.
**
** Note that apCell[] contains enough space to hold up to two more Cells
** than can possibly fit on one page. In the steady state, every apCell[]
** points to memory inside u.aDisk[]. But in the middle of an insert
** operation, some apCell[] entries may temporarily point to data space
** outside of u.aDisk[]. This is a transient situation that is quickly
** resolved. But while it is happening, it is possible for a database
** page to hold as many as two more cells than it might otherwise hold.
** The extra two entries in apCell[] are an allowance for this situation.
**
** The pParent field points back to the parent page. This allows us to
** walk up the BTree from any leaf to the root. Care must be taken to
** unref() the parent page pointer when this page is no longer referenced.
** The pageDestructor() routine handles that chore.
*/
struct MemPage {
union u_page_data {
} u;
int idxParent; /* Index in pParent->apCell[] of this node */
int nFree; /* Number of free bytes in u.aDisk[] */
int nCell; /* Number of entries on this page */
};
/*
** The in-memory image of a disk page has the auxiliary information appended
** to the end. EXTRA_SIZE is the number of bytes of space needed to hold
** that extra information.
*/
/*
** Everything we need to know about an open database
*/
struct Btree {
};
/*
** A cursor is a pointer to a particular entry in the BTree.
** The entry is identified by its MemPage and the index in
** MemPage.apCell[] of the entry.
*/
struct BtCursor {
int idx; /* Index of the entry in pPage->apCell[] */
};
/*
** Legal values for BtCursor.eSkip.
*/
#define SKIP_NONE 0 /* Always step the cursor */
/* Forward declarations */
/*
** Routines for byte swapping.
*/
return ((x & 0xff)<<8) | ((x>>8)&0xff);
}
return ((x & 0xff)<<24) | ((x & 0xff00)<<8) |
((x>>8) & 0xff00) | ((x>>24)&0xff);
}
/*
** Compute the total number of bytes that a Cell needs on the main
** database page. The number returned includes the Cell header,
** local payload storage, and the pointer to overflow pages (if
** applicable). Additional space allocated on overflow pages
** is NOT included in the value returned from this routine.
*/
if( n>MX_LOCAL_PAYLOAD ){
n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
}else{
n = ROUNDUP(n);
}
n += sizeof(CellHdr);
return n;
}
/*
** Defragment the page given. All Cells are moved to the
** beginning of the page and all free space is collected
** into one big FreeBlk at the end of the page.
*/
int pc, i, n;
char newPage[SQLITE_USABLE_SIZE];
/* This routine should never be called on an overfull page. The
** following asserts verify that constraint. */
pc += n;
}
}
}
/*
** Allocate nByte bytes of space on a page. nByte must be a
** multiple of 4.
**
** Return the index into pPage->u.aDisk[] of the first byte of
** the new allocation. Or return 0 if there is not enough free
** space on the page to satisfy the allocation request.
**
** If the page contains nBytes of free space but does not contain
** nBytes of contiguous free space, then this routine automatically
** calls defragementPage() to consolidate all free space before
** allocating the new chunk.
*/
FreeBlk *p;
int start;
int iSize;
#ifndef NDEBUG
int cnt = 0;
#endif
if( p->iNext==0 ){
}else{
}
}
}else{
}
return start;
}
/*
** Return a section of the MemPage.u.aDisk[] to the freelist.
** The first byte of the new free block is pPage->u.aDisk[start]
** and the size of the block is "size" bytes. Size must be
** a multiple of 4.
**
** Most of the effort here is involved in coalesing adjacent
** free blocks into a single big free block.
*/
int iSize;
}else{
}
}
return;
}
}
}else{
}
}
/*
** Initialize the auxiliary information for a disk block.
**
** The pParent parameter must be a pointer to the MemPage which
** is the parent of the page being initialized. The root of the
** BTree (usually page 2) has no parent and so for that page,
** pParent==NULL.
**
** Return SQLITE_OK on success. If we see that the page does
** not contain a well-formed database page, then return
** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
** guarantee that the page is well-formed. It only shows that
** we failed to detect any corruption.
*/
int idx; /* An index into pPage->u.aDisk[] */
int sz; /* The size of a Cell in bytes */
int freeSpace; /* Amount of free space on the page */
return SQLITE_OK;
}
if( pParent ){
}
while( idx!=0 ){
}
while( idx!=0 ){
int iNext;
}
/* As a special case, an uninitialized root page appears to be
** an empty database */
return SQLITE_OK;
}
return SQLITE_OK;
return SQLITE_CORRUPT;
}
/*
** Set up a raw page so that it looks like a database page holding
** no entries.
*/
pPage->isOverfull = 0;
}
/*
** This routine is called when the reference count for a page
** reaches zero. We need to unref the pParent pointer when that
** happens.
*/
static void pageDestructor(void *pData){
}
}
/*
** Open a new database.
**
** Actually, this routine just sets up the internal data structures
** for accessing the database. We do not open the database file
** until the first page is loaded.
**
** zFilename is the name of the database file. If zFilename is NULL
** a new database with a random name is created. This randomly named
** database file will be deleted when sqliteBtreeClose() is called.
*/
int sqliteBtreeOpen(
const char *zFilename, /* Name of the file containing the BTree database */
int omitJournal, /* if TRUE then do not journal this file */
int nCache, /* How many pages in the page cache */
){
int rc;
/*
** The following asserts make sure that structures used by the btree are
** the right size. This is to guard against size changes that result
** when compiling on a different architecture.
*/
if( pBt==0 ){
*ppBtree = 0;
return SQLITE_NOMEM;
}
!omitJournal);
*ppBtree = 0;
return rc;
}
return SQLITE_OK;
}
/*
** Close an open database and invalidate all cursors.
*/
}
return SQLITE_OK;
}
/*
** Change the limit on the number of pages allowed in the cache.
**
** The maximum number of cache pages is set to the absolute
** value of mxPage. If mxPage is negative, the pager will
** operate asynchronously - it will not stop to do fsync()s
** to insure data is written to the disk surface before
** continuing. Transactions still work if synchronous is off,
** and the database cannot be corrupted if this program
** crashes. But if the operating system crashes or there is
** an abrupt power failure when synchronous is off, the database
** could be left in an inconsistent and unrecoverable state.
** Synchronous is on by default so database corruption is not
** normally a worry.
*/
return SQLITE_OK;
}
/*
** Change the way data is synced to disk in order to increase or decrease
** how well the database resists damage due to OS crashes and power
** failures. Level 1 is the same as asynchronous (no syncs() occur and
** there is a high probability of damage) Level 2 is the default. There
** is a very low but non-zero probability of damage. Level 3 reduces the
** probability of damage to near zero but with a write performance reduction.
*/
return SQLITE_OK;
}
/*
** Get a reference to page1 of the database file. This will
** also acquire a readlock on that file.
**
** SQLITE_OK is returned on success. If the file is not a
** well-formed database file, then SQLITE_CORRUPT is returned.
** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
** is returned if we run out of memory. SQLITE_PROTOCOL is returned
** if there is a locking protocol violation.
*/
int rc;
/* Do some checking to help insure the file we opened really is
** a valid database file.
*/
rc = SQLITE_NOTADB;
goto page1_init_failed;
}
}
return rc;
return rc;
}
/*
** If there are no outstanding cursors and we are not in the middle
** of a transaction but there is a read lock on the database, then
** this routine unrefs the first page of the database file which
** has the effect of releasing the read lock.
**
** If there are any outstanding cursors, this routine is a no-op.
**
** If there is a transaction in progress, this routine is a no-op.
*/
}
}
/*
** Create a new database by initializing the first two pages of the
** file.
*/
int rc;
if( rc ){
return rc;
}
if( btree_native_byte_order ){
}else{
}
return SQLITE_OK;
}
/*
** Attempt to start a new transaction.
**
** A transaction must be started before attempting any changes
** to the database. None of the following routines will work
** unless a transaction is started first:
**
** sqliteBtreeCreateTable()
** sqliteBtreeCreateIndex()
** sqliteBtreeClearTable()
** sqliteBtreeDropTable()
** sqliteBtreeInsert()
** sqliteBtreeDelete()
** sqliteBtreeUpdateMeta()
*/
int rc;
return rc;
}
}
}
}else{
}
return rc;
}
/*
** Commit the transaction currently in progress.
**
** This will release the write lock on the database file. If there
** are no active cursors, it also releases the read lock.
*/
int rc;
return rc;
}
/*
** Rollback the transaction in progress. All cursors will be
** invalided by this operation. Any attempt to use a cursor
** that was open at the beginning of this operation will result
** in an error.
**
** This will release the write lock on the database file. If there
** are no active cursors, it also releases the read lock.
*/
int rc;
}
}
return rc;
}
/*
** Set the checkpoint for the current transaction. The checkpoint serves
** as a sub-transaction that can be rolled back independently of the
** main transaction. You must start a transaction before starting a
** checkpoint. The checkpoint is ended automatically if the transaction
** commits or rolls back.
**
** Only one checkpoint may be active at a time. It is an error to try
** to start a new checkpoint if another checkpoint is already active.
*/
int rc;
}
return rc;
}
/*
** Commit a checkpoint to transaction currently in progress. If no
** checkpoint is active, this is a no-op.
*/
int rc;
}else{
}
return rc;
}
/*
** Rollback the checkpoint to the current transaction. If there
** is no active checkpoint or transaction, this routine is a no-op.
**
** All cursors will be invalided by this operation. Any attempt
** to use a cursor that was open at the beginning of this operation
** will result in an error.
*/
int rc;
}
}
return rc;
}
/*
** Create a new cursor for the BTree whose root is on the page
** iTable. The act of acquiring a cursor gets a read lock on
** the database file.
**
** If wrFlag==0, then the cursor can only be used for reading.
** If wrFlag==1, then the cursor can be used for reading or for
** writing if other conditions for writing are also met. These
** are the conditions that must be met in order for writing to
** be allowed:
**
** 1: The cursor must have been opened with wrFlag==1
**
** 2: No other cursors may be open with wrFlag==0 on the same table
**
** 3: The database must be writable (not on read-only media)
**
** 4: There must be an active transaction.
**
** Condition 2 warrants further discussion. If any cursor is opened
** on a table with wrFlag==0, that prevents all other cursors from
** writing to that table. This is a kind of "read-lock". When a cursor
** is opened with wrFlag==0 it is guaranteed that the table will not
** change as long as the cursor is open. This allows the cursor to
** do a sequential scan of the table without having to worry about
** entries being inserted or deleted during the scan. Cursors should
** be opened with wrFlag==0 only if this read-lock property is needed.
** That is to say, cursors should be opened with wrFlag==0 only if they
** intend to use the sqliteBtreeNext() system call. All other cursors
** should be opened with wrFlag==1 even if they never really intend
** to write.
**
** No checking is done to make sure that page iTable really is the
** root page of a b-tree. If it is not, then the cursor acquired
** will not work correctly.
*/
static
int rc;
*ppCur = 0;
return SQLITE_READONLY;
}
*ppCur = 0;
return rc;
}
}
if( pCur==0 ){
rc = SQLITE_NOMEM;
goto create_cursor_exception;
}
goto create_cursor_exception;
}
goto create_cursor_exception;
}
}
if( pRing ){
}else{
}
return SQLITE_OK;
*ppCur = 0;
if( pCur ){
}
return rc;
}
/*
** Close a cursor. The read lock on the database file is released
** when the last cursor is closed.
*/
}else{
}
}
}
}
return SQLITE_OK;
}
/*
** Make a temporary cursor by filling in the fields of pTempCur.
** The temporary cursor is not on the cursor list for the Btree.
*/
}
}
/*
** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
** function above.
*/
}
}
/*
** Set *pSize to the number of bytes of key in the entry the
** cursor currently points to. Always return SQLITE_OK.
** Failure is not possible. If the cursor is not currently
** pointing to an entry (which can happen, for example, if
** the database is empty) then *pSize is set to 0.
*/
*pSize = 0;
}else{
}
return SQLITE_OK;
}
/*
** Read payload information from the entry that the pCur cursor is
** pointing to. Begin reading the payload at "offset" and read
** a total of "amt" bytes. Put the result in zBuf.
**
** This routine does not make a distinction between key and data.
** It just reads bytes from the payload area.
*/
char *aPayload;
int rc;
if( offset<MX_LOCAL_PAYLOAD ){
int a = amt;
if( a+offset>MX_LOCAL_PAYLOAD ){
a = MX_LOCAL_PAYLOAD - offset;
}
if( a==amt ){
return SQLITE_OK;
}
offset = 0;
zBuf += a;
amt -= a;
}else{
}
if( amt>0 ){
}
if( rc!=0 ){
return rc;
}
if( offset<OVERFLOW_SIZE ){
int a = amt;
if( a + offset > OVERFLOW_SIZE ){
a = OVERFLOW_SIZE - offset;
}
offset = 0;
amt -= a;
zBuf += a;
}else{
offset -= OVERFLOW_SIZE;
}
}
if( amt>0 ){
return SQLITE_CORRUPT;
}
return SQLITE_OK;
}
/*
** Read part of the key associated with cursor pCur. A maximum
** of "amt" bytes will be transfered into zBuf[]. The transfer
** begins at "offset". The number of bytes actually read is
** returned.
**
** Change: It used to be that the amount returned will be smaller
** than the amount requested if there are not enough bytes in the key
** to satisfy the request. But now, it must be the case that there
** is enough data available to satisfy the request. If not, an exception
** is raised. The change was made in an effort to boost performance
** by eliminating unneeded tests.
*/
return 0;
}
return amt;
}
/*
** Set *pSize to the number of bytes of data in the entry the
** cursor currently points to. Always return SQLITE_OK.
** Failure is not possible. If the cursor is not currently
** pointing to an entry (which can happen, for example, if
** the database is empty) then *pSize is set to 0.
*/
*pSize = 0;
}else{
}
return SQLITE_OK;
}
/*
** Read part of the data associated with cursor pCur. A maximum
** of "amt" bytes will be transfered into zBuf[]. The transfer
** begins at "offset". The number of bytes actually read is
** returned. The amount returned will be smaller than the
** amount requested if there are not enough bytes in the data
** to satisfy the request.
*/
return 0;
}
return amt;
}
/*
** Compare an external key against the key on the entry that pCur points to.
**
** The external key is pKey and is nKey bytes long. The last nIgnore bytes
** of the key associated with pCur are ignored, as if they do not exist.
** (The normal case is for nIgnore to be zero in which case the entire
** internal key is used in the comparison.)
**
** The comparison result is written to *pRes as follows:
**
** *pRes<0 This means pCur<pKey
**
** *pRes==0 This means pCur==pKey for all nKey bytes
**
** *pRes>0 This means pCur>pKey
**
** When one key is an exact prefix of the other, the shorter key is
** considered less than the longer one. In order to be equal the
** keys must be exactly the same length. (The length of the pCur key
** is the actual key length minus nIgnore bytes.)
*/
static int fileBtreeKeyCompare(
const void *pKey, /* Key to compare against entry that pCur points to */
int nKey, /* Number of bytes in pKey */
int nIgnore, /* Ignore this many bytes at the end of pCur */
int *pResult /* Write the result here */
){
if( n>MX_LOCAL_PAYLOAD ){
n = MX_LOCAL_PAYLOAD;
}
if( c!=0 ){
*pResult = c;
return SQLITE_OK;
}
zKey += n;
nKey -= n;
nLocal -= n;
if( nextPage==0 ){
return SQLITE_CORRUPT;
}
if( rc ){
return rc;
}
if( n>OVERFLOW_SIZE ){
n = OVERFLOW_SIZE;
}
if( c!=0 ){
*pResult = c;
return SQLITE_OK;
}
nKey -= n;
nLocal -= n;
zKey += n;
}
if( c==0 ){
}
*pResult = c;
return SQLITE_OK;
}
/*
** Move the cursor down to a new child page. The newPgno argument is the
** page number of the child page in the byte order of the disk image.
*/
int rc;
return SQLITE_CORRUPT;
}
return SQLITE_OK;
}
/*
** Move the cursor up to the parent page.
**
** pCur->idx is set to the cell index that contains the pointer
** to the page we are coming from. If we are coming from the
** right-most child page then pCur->idx is set to one more than
** the largest cell index.
*/
int idxParent;
#ifndef NDEBUG
/* Verify that pCur->idx is the correct index to point back to the child
** page we just came from
*/
}else{
}
#endif
}else{
/* The MemPage.idxShift flag indicates that cell indices might have
** changed since idxParent was set and hence idxParent might be out
** of date. So recompute the parent cell index by scanning all cells
** and locating the one that points to the child we just came from.
*/
int i;
break;
}
}
}
}
/*
** Move the cursor to the root page
*/
int rc;
return SQLITE_OK;
}
/*
** Move the cursor down to the left-most leaf entry beneath the
** entry to which it is currently pointing.
*/
int rc;
}
return SQLITE_OK;
}
/*
** Move the cursor down to the right-most leaf entry beneath the
** page to which it is currently pointing. Notice the difference
** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
** finds the left-most entry beneath the *entry* whereas moveToRightmost()
** finds the right-most entry beneath the *page*.
*/
int rc;
}
return SQLITE_OK;
}
/* Move the cursor to the first entry in the table. Return SQLITE_OK
** on success. Set *pRes to 0 if the cursor actually points to something
** or set *pRes to 1 if the table is empty.
*/
int rc;
*pRes = 1;
return SQLITE_OK;
}
*pRes = 0;
return rc;
}
/* Move the cursor to the last entry in the table. Return SQLITE_OK
** on success. Set *pRes to 0 if the cursor actually points to something
** or set *pRes to 1 if the table is empty.
*/
int rc;
*pRes = 1;
return SQLITE_OK;
}
*pRes = 0;
return rc;
}
/* Move the cursor so that it points to an entry near pKey.
** Return a success code.
**
** If an exact match is not found, then the cursor is always
** left pointing at a leaf page which would hold the entry if it
** were present. The cursor might point to an entry that comes
** before or after the key.
**
** The result of comparing the key with the entry to which the
** cursor is left pointing is stored in pCur->iMatch. The same
** value is also written to *pRes if pRes!=NULL. The meaning of
** this value is as follows:
**
** *pRes<0 The cursor is left pointing at an entry that
** is smaller than pKey or if the table is empty
** and the cursor is therefore left point to nothing.
**
** *pRes==0 The cursor is left pointing at an entry that
** exactly matches pKey.
**
** *pRes>0 The cursor is left pointing at an entry that
** is larger than pKey.
*/
static
int rc;
for(;;){
int c = -1; /* pRes return if table is empty must be -1 */
lwr = 0;
if( c==0 ){
return SQLITE_OK;
}
if( c<0 ){
}else{
}
}
}else{
}
if( chldPg==0 ){
return SQLITE_OK;
}
}
/* NOT REACHED */
}
/*
** Advance the cursor to the next entry in the database. If
** successful then set *pRes=0. If the cursor
** was already pointing to the last entry in the database before
** this routine was called, then set *pRes=1.
*/
int rc;
if( pPage==0 ){
*pRes = 1;
return SQLITE_ABORT;
}
*pRes = 1;
return SQLITE_OK;
}
*pRes = 0;
return SQLITE_OK;
}
*pRes = 0;
return rc;
}
do{
*pRes = 1;
return SQLITE_OK;
}
*pRes = 0;
return SQLITE_OK;
}
*pRes = 0;
return SQLITE_OK;
}
return rc;
}
/*
** Step the cursor to the back to the previous entry in the database. If
** successful then set *pRes=0. If the cursor
** was already pointing to the first entry in the database before
** this routine was called, then set *pRes=1.
*/
int rc;
if( pPage==0 ){
*pRes = 1;
return SQLITE_ABORT;
}
*pRes = 1;
return SQLITE_OK;
}
*pRes = 0;
return SQLITE_OK;
}
}else{
return SQLITE_OK;
}
}
}
*pRes = 0;
return rc;
}
/*
** Allocate a new page from the database file.
**
** The new page is marked as dirty. (In other words, sqlitepager_write()
** has already been called on the new page.) The new page has also
** been referenced and the calling routine is responsible for calling
** sqlitepager_unref() on the new page when it is done.
**
** SQLITE_OK is returned on success. Any other return value indicates
** an error. *ppPage and *pPgno are undefined in the event of an error.
** Do not invoke sqlitepager_unref() on *ppPage if an error is returned.
**
** If the "nearby" parameter is not 0, then a (feeble) effort is made to
** locate a page close to the page number "nearby". This can be used in an
** attempt to keep related pages close to each other in the database file,
** which in turn can make database access faster.
*/
int rc;
(void**)&pOvfl);
if( rc ){
return rc;
}
}else{
int closest, n;
if( n>1 && nearby>0 ){
int i, dist;
closest = 0;
for(i=1; i<n; i++){
}
}else{
closest = 0;
}
}
}
}else{
}
return rc;
}
/*
** Add a page of the database file to the freelist. Either pgno or
** pPage but not both may be 0.
**
** sqlitepager_unref() is NOT called for pPage.
*/
int rc;
int needUnref = 0;
if( pgno==0 ){
}
}
if( rc ){
return rc;
}
(void**)&pFreeIdx);
return rc;
}
}
}
}
if( pOvfl==0 ){
needUnref = 1;
}
if( rc ){
return rc;
}
return rc;
}
/*
** Erase all the data out of a cell. This involves returning overflow
** pages back the freelist.
*/
int rc;
return SQLITE_OK;
}
while( ovfl ){
}
return SQLITE_OK;
}
/*
** Create a new cell from key and data. Overflow pages are allocated as
** necessary and linked to this cell.
*/
static int fillInCell(
){
int spaceLeft;
int n, rc;
int nPayload;
const char *pPayload;
char *pSpace;
pKey = 0;
pPrior = 0;
while( nPayload>0 ){
if( spaceLeft==0 ){
if( rc ){
*pNext = 0;
}else{
}
if( rc ){
return rc;
}
}
n = nPayload;
nPayload -= n;
pData = 0;
}else{
pPayload += n;
}
spaceLeft -= n;
pSpace += n;
}
*pNext = 0;
if( pPrior ){
}
return SQLITE_OK;
}
/*
** Change the MemPage.pParent pointer on the page whose number is
** given in the second argument so that MemPage.pParent holds the
** pointer in the third argument.
*/
if( pgno==0 ) return;
}
}
}
/*
** Reparent all children of the given page to be the given page.
** In other words, for every child of pPage, invoke reparentPage()
** to make sure that each child knows that pPage is its parent.
**
** This routine gets called after you memcpy() one page into
** another.
*/
int i;
}
}
/*
** Remove the i-th cell from pPage. This routine effects pPage only.
** The cell content is not freed or deallocated. It is assumed that
** the cell content has been copied someplace else. This routine just
** removes the reference to the cell from pPage.
**
** "sz" must be the number of bytes in the cell.
**
** Do not bother maintaining the integrity of the linked list of Cells.
** Only the pPage->apCell[] array is important. The relinkCellList()
** routine will be called soon after this routine in order to rebuild
** the linked list.
*/
int j;
}
}
/*
** Insert a new cell on pPage at cell index "i". pCell points to the
** content of the cell.
**
** If the cell content will fit on the page, then put it there. If it
** will not fit, then just make pPage->apCell[i] point to the content
** and set pPage->isOverfull.
**
** Do not bother maintaining the integrity of the linked list of Cells.
** Only the pPage->apCell[] array is important. The relinkCellList()
** routine will be called soon after this routine in order to rebuild
** the linked list.
*/
int idx, j;
}
if( idx<=0 ){
}else{
}
}
/*
** Rebuild the linked list of cells on a page so that the cells
** occur in the order specified by the pPage->apCell[] array.
** Invoke this routine once to repair damage after one or more
** invocations of either insertCell() or dropCell().
*/
int i;
}
*pIdx = 0;
}
/*
** Make a copy of the contents of pFrom into pTo. The pFrom->apCell[]
** pointers that point into pFrom->u.aDisk[] must be adjusted to point
** into pTo->u.aDisk[] instead. But some pFrom->apCell[] entries might
** not point to pFrom->u.aDisk[]. Those are unchanged.
*/
int i;
}else{
}
}
}
/*
** The following parameters determine how many adjacent pages get involved
** in a balancing operation. NN is the number of neighbors on either side
** of the page that participate in the balancing operation. NB is the
** total number of pages that participate, including the target page and
** NN neighbors on either side.
**
** The minimum value of NN is 1 (of course). Increasing NN above 1
** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
** in exchange for a larger degradation in INSERT and UPDATE performance.
** The value of NN appears to give the best results overall.
*/
/*
** This routine redistributes Cells on pPage and up to two siblings
** of pPage so that all pages have about the same amount of free space.
** Usually one sibling on either side of pPage is used in the balancing,
** though both siblings might come from one side if pPage is the first
** or last child of its parent. If pPage has fewer than two siblings
** (something which can only happen if pPage is the root page or a
** child of root) then all available siblings participate in the balancing.
**
** The number of siblings of pPage might be increased or decreased by
** one in an effort to keep pages between 66% and 100% full. The root page
** is special and is allowed to be less than 66% full. If pPage is
** the root page, then the depth of the tree might be increased
** or decreased by one, as necessary, to keep the root page from being
** overfull or empty.
**
** This routine calls relinkCellList() on its input page regardless of
** whether or not it does any real balancing. Client routines will typically
** invoke insertCell() or dropCell() before calling this routine, so we
** need to call relinkCellList() to clean up the mess that those other
** routines left behind.
**
** pCur is left pointing to the same cell as when this routine was called
** even if that cell gets moved to a different page. pCur may be NULL.
** Set the pCur parameter to NULL if you do not care about keeping track
** of a cell as that will save this routine the work of keeping track of it.
**
** Note that when this routine is called, some of the Cells on pPage
** might not actually be stored in pPage->u.aDisk[]. This can happen
** if the page is overfull. Part of the job of this routine is to
** make sure all Cells for pPage once again fit in pPage->u.aDisk[].
**
** In the course of balancing the siblings of pPage, the parent of pPage
** might become overfull or underfull. If that happens, then this routine
** is called recursively on the parent.
**
** If this routine fails for any reason, it might leave the database
** in a corrupted state. So if this routine fails, the database should
** be rolled back.
*/
int nCell; /* Number of cells in apCell[] */
int nOld; /* Number of pages in apOld[] */
int nNew; /* Number of pages in apNew[] */
int nDiv; /* Number of cells in apDiv[] */
int i, j, k; /* Loop counters */
int idx; /* Index of pPage in pParent->apCell[] */
int nxDiv; /* Next divider slot in pParent->apCell[] */
int rc; /* The return code */
int iCur; /* apCell[iCur] is the cell of the cursor */
int subtotal; /* Subtotal of bytes in cells on one page */
/*
** Return without doing any work if pPage is neither overfull nor
** underfull.
*/
return SQLITE_OK;
}
/*
** Find the parent of the page to be balanceed.
** If there is no parent, it means this page is the root page and
** special rules apply.
*/
if( pParent==0 ){
/*
** The root page is empty. Copy the one child page
** into the root page and return. This reduces the depth
** of the BTree by one.
*/
}
}else{
}
return SQLITE_OK;
}
if( !pPage->isOverfull ){
/* It is OK for the root page to be less than half full.
*/
return SQLITE_OK;
}
/*
** If we get to here, it means the root page is overfull.
** When this happens, Create a new child page and copy the
** contents of the root into the child. Then make the root
** page an empty page with rightChild pointing to the new
** child. Then fall thru to the code below which will cause
** the overfull child page to be split.
*/
}else{
extraUnref = pChild;
}
}
/*
** Find the Cell in the parent page whose h.leftChild points back
** to pPage. The "idx" variable is the index of that cell. If pPage
** is the rightmost child of pParent then set idx to pParent->nCell
*/
break;
}
}
}else{
}
/*
** Initialize variables so that it will be safe to jump
** directly to balance_cleanup at any moment.
*/
/*
** Find sibling pages to pPage and the Cells in pParent that divide
** the siblings. An attempt is made to find NN siblings on either
** side of pPage. More siblings are taken from one side, however, if
** pPage there are fewer than NN siblings on the other side. If pParent
** has NB or fewer children then all children of pParent are taken.
*/
}
if( nxDiv<0 ){
nxDiv = 0;
}
nDiv = 0;
idxDiv[i] = k;
nDiv++;
}else{
break;
}
if( rc ) goto balance_cleanup;
if( rc ) goto balance_cleanup;
nOld++;
}
/*
** Set iCur to be the index in apCell[] of the cell that the cursor
** is pointing to. We will need this later on in order to keep the
** cursor pointing at the same cell. If pCur points to a page that
** has no involvement with this rebalancing, then set iCur to a large
** number so that the iCur==j tests always fail in the main cell
** distribution loop below.
*/
if( pCur ){
iCur = 0;
for(i=0; i<nOld; i++){
break;
}
break;
}
iCur++;
}
}
/*
** Make copies of the content of pPage and its siblings into aOld[].
** The rest of this function will use data from the copies rather
** that the original pages since the original pages will be in the
** process of being overwritten.
*/
for(i=0; i<nOld; i++){
}
/*
** Load pointers to all cells on sibling pages and the divider cells
** into the local apCell[] array. Make copies of the divider cells
** into aTemp[] and remove the the divider Cells from pParent.
*/
nCell = 0;
for(i=0; i<nOld; i++){
nCell++;
}
if( i<nOld-1 ){
nCell++;
}
}
/*
** Figure out the number of pages needed to hold all nCell cells.
** Store this number in "k". Also compute szNew[] which is the total
** size of all cells on the i-th page and cntNew[] which is the index
** in apCell[] of the cell that divides path i from path i+1.
** cntNew[k] should equal nCell.
**
** This little patch of code is critical for keeping the tree
** balanced.
*/
if( subtotal > USABLE_SPACE ){
cntNew[k] = i;
subtotal = 0;
k++;
}
}
k++;
for(i=k-1; i>0; i--){
cntNew[i-1]--;
}
}
/*
** Allocate k new pages. Reuse old pages where possible.
*/
for(i=0; i<k; i++){
if( i<nOld ){
apOld[i] = 0;
if( rc ) goto balance_cleanup;
}else{
if( rc ) goto balance_cleanup;
}
nNew++;
}
/* Free any old pages that were not reused as new pages.
*/
while( i<nOld ){
if( rc ) goto balance_cleanup;
sqlitepager_unref(apOld[i]);
apOld[i] = 0;
i++;
}
/*
** Put the new pages in accending order. This helps to
** keep entries in the disk file in order so that a scan
** of the table is a linear scan through the file. That
** in turn helps the operating system to deliver pages
** from the disk more rapidly.
**
** An O(n^2) insertion sort algorithm is used, but since
** n is never more than NB (a small constant), that should
** not be a problem.
**
** When NB==3, this one optimization makes the database
** about 25% faster for large insertions and deletions.
*/
for(i=0; i<k-1; i++){
int minI = i;
for(j=i+1; j<k; j++){
minI = j;
}
}
if( minI>i ){
int t;
t = pgnoNew[i];
}
}
/*
** Evenly distribute the data in apCell[] across the new pages.
** Insert divider cells into pParent as necessary.
*/
j = 0;
for(i=0; i<nNew; i++){
while( j<cntNew[i] ){
j++;
}
j++;
nxDiv++;
}
}
}else{
}
if( pCur ){
}else{
assert( pOldCurPage!=0 );
}
}
/*
** Reparent children of all cells.
*/
for(i=0; i<nNew; i++){
}
/*
** balance the parent page.
*/
/*
** Cleanup before returning.
*/
if( extraUnref ){
}
for(i=0; i<nOld; i++){
}
for(i=0; i<nNew; i++){
sqlitepager_unref(apNew[i]);
}
}else{
}
return rc;
}
/*
** This routine checks all cursors that point to the same table
** as pCur points to. If any of those cursors were opened with
** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
** cursors point to the same table were opened with wrFlag==1
** then this routine returns SQLITE_OK.
**
** In addition to checking for read-locks (where a read-lock
** means a cursor opened with wrFlag==0) this routine also moves
** all cursors other than pCur so that they are pointing to the
** first Cell on root page. This is necessary because an insert
** or delete might change the number of cells on a page or delete
** a page entirely and we do not want to leave any cursors
** pointing to non-existant pages or cells.
*/
BtCursor *p;
assert( p );
if( p->wrFlag==0 ) return SQLITE_LOCKED;
moveToRoot(p);
}
}
return SQLITE_OK;
}
/*
** Insert a new record into the BTree. The key is given by (pKey,nKey)
** and the data is given by (pData,nData). The cursor is used only to
** define what database the record should be inserted into. The cursor
** is left pointing at the new record.
*/
static int fileBtreeInsert(
){
int rc;
int loc;
int szNew;
return SQLITE_ABORT; /* A rollback destroyed this cursor */
}
/* Must start a transaction before doing an insert */
}
return SQLITE_PERM; /* Cursor not open for writing */
}
if( checkReadLocks(pCur) ){
return SQLITE_LOCKED; /* The table pCur points to has a read lock */
}
if( loc==0 ){
}else{
}
/* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
/* fflush(stdout); */
return rc;
}
/*
** Delete the entry that the cursor is pointing to.
**
** The cursor is left pointing at either the next or the previous
** entry. If the cursor is left pointing to the next entry, then
** the pCur->eSkip flag is set to SKIP_NEXT which forces the next call to
** sqliteBtreeNext() to be a no-op. That way, you can always call
** sqliteBtreeNext() after a delete and the cursor will be left
** pointing to the first entry after the deleted entry. Similarly,
** pCur->eSkip is set to SKIP_PREV is the cursor is left pointing to
** the entry prior to the deleted entry so that a subsequent call to
** sqliteBtreePrevious() will always leave the cursor pointing at the
** entry immediately before the one that was deleted.
*/
int rc;
return SQLITE_ABORT; /* A rollback destroyed this cursor */
}
/* Must start a transaction before doing a delete */
}
return SQLITE_ERROR; /* The cursor is not pointing to anything */
}
return SQLITE_PERM; /* Did not open this cursor for writing */
}
if( checkReadLocks(pCur) ){
return SQLITE_LOCKED; /* The table pCur points to has a read lock */
}
if( pgnoChild ){
/*
** The entry we are about to delete is not a leaf so if we do not
** do something we will leave a hole on an internal page.
** We have to fill the hole by moving in a cell from a leaf. The
** next Cell after the one to be deleted is guaranteed to exist and
** to be a leaf so we can use it.
*/
int szNext;
int notUsed;
return rc;
}
}else{
}else{
}
}else{
}
}
return rc;
}
/*
** Create a new BTree table. Write into *piTable the page
** number for the root page of the new table.
**
** In the current implementation, BTree tables and BTree indices are the
** the same. In the future, we may change this so that BTree tables
** are restricted to having a 4-byte integer key and arbitrary data and
** BTree indices are restricted to having an arbitrary key and no data.
** But for now, this routine also serves to create indices.
*/
int rc;
/* Must start a transaction first */
}
return SQLITE_READONLY;
}
return SQLITE_OK;
}
/*
** Erase the given database page and all its children. Return
** the page to the freelist.
*/
int rc;
int idx;
while( idx>0 ){
}
}
}
if( freePageFlag ){
}else{
}
return rc;
}
/*
** Delete all information from a single table in the database.
*/
int rc;
}
}
}
if( rc ){
}
return rc;
}
/*
** Erase all information in a table and add the root of the table to
** the freelist. Except, the root of the principle table (the one on
** page 2) is never added to the freelist.
*/
int rc;
}
return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */
}
}
if( iTable>2 ){
}else{
}
return rc;
}
#if 0 /* UNTESTED */
/*
** Copy all cell data from one database file into another.
** pages back the freelist.
*/
return SQLITE_OK;
}
pPrevPg = 0;
if( pPrevPg ){
}
}
}
}
if( pPrevPg ){
}
return rc;
}
#endif
#if 0 /* UNTESTED */
/*
** Copy a page of data from one database over to another.
*/
static int copyDatabasePage(
){
int rc;
int idx;
}
while( idx>0 ){
}
}
}
}
return rc;
}
#endif
/*
** Read the meta-information out of a database file.
*/
int rc;
int i;
}
return SQLITE_OK;
}
/*
** Write meta-information back into the database.
*/
int rc, i;
}
}
return SQLITE_OK;
}
/******************************************************************************
** The complete implementation of the BTree subsystem is above this line.
** All the code the follows is for testing and troubleshooting the BTree
** subsystem. None of the code that follows is used during normal operation.
******************************************************************************/
/*
** Print a disassembly of the given page on standard output. This routine
** is used for debugging and testing only.
*/
#ifdef SQLITE_TEST
int rc;
int i, j;
int nFree;
char range[20];
unsigned char payload[20];
if( rc ){
return rc;
}
i = 0;
for(j=0; j<sz; j++){
}
"cell %2d: i=%-10s chld=%-4d nk=%-4d nd=%-4d payload=%s\n",
);
printf("**** apCell[%d] does not match on prior entry ****\n", i);
}
i++;
}
if( idx!=0 ){
}
nFree = 0;
i = 0;
printf("freeblock %2d: i=%-10s size=%-4d total=%d\n",
i++;
}
if( idx!=0 ){
}
}
}
return SQLITE_OK;
}
#endif
#ifdef SQLITE_TEST
/*
** Fill aResult[] with information about the entry and page that the
** cursor is pointing to.
**
** aResult[0] = The page number
** aResult[1] = The entry number
** aResult[2] = Total number of entries on this page
** aResult[3] = Size of this entry
** aResult[4] = Number of free bytes on this page
** aResult[5] = Number of free blocks on the page
** aResult[6] = Page number of the left child of this entry
** aResult[7] = Page number of the right child for the whole page
**
** This routine is used for testing and debugging only.
*/
}else{
aResult[3] = 0;
aResult[6] = 0;
}
cnt = 0;
cnt++;
}
return SQLITE_OK;
}
#endif
/*
** Return the pager associated with a BTree. This routine is used for
** testing and debugging only.
*/
}
/*
** This structure is passed around through all the sanity checking routines
** in order to keep track of some global state information.
*/
typedef struct IntegrityCk IntegrityCk;
struct IntegrityCk {
int nPage; /* Number of pages in the database */
int *anRef; /* Number of times each page is referenced */
char *zErrMsg; /* An error message. NULL of no errors seen. */
};
/*
** Append a message to the error message string.
*/
}else{
}
}
/*
** Add 1 to the reference count for page iPage. If this is the second
** reference to the page, add an error message to pCheck->zErrMsg.
** Return 1 if there are 2 ore more references to the page and 0 if
** if this is the first reference to the page.
**
** Also check that the page number is in bounds.
*/
if( iPage==0 ) return 1;
char zBuf[100];
return 1;
}
char zBuf[100];
return 1;
}
}
/*
** Check the integrity of the freelist or of an overflow page list.
** Verify that the number of pages on the list is N.
*/
static void checkList(
int isFreeList, /* True for a freelist. False for overflow page list */
int iPage, /* Page number for first page in the list */
int N, /* Expected number of pages in the list */
char *zContext /* Context for error messages */
){
int i;
char zMsg[100];
while( N-- > 0 ){
if( iPage<1 ){
break;
}
break;
}
if( isFreeList ){
for(i=0; i<n; i++){
}
N -= n;
}
}
}
/*
** Return negative if zKey1<zKey2.
** Return zero if zKey1==zKey2.
** Return positive if zKey1>zKey2.
*/
static int keyCompare(
){
if( c==0 ){
}
return c;
}
/*
** Do various sanity checks on a single page of a tree. Return
** the tree depth. Root pages return 0. Parents of root pages
** return 1, and so forth.
**
** These checks are done:
**
** 1. Make sure that cells and freeblocks do not overlap
** but combine to completely cover the page.
** 2. Make sure cell keys are in order.
** 3. Make sure no key is less than or equal to zLowerBound.
** 4. Make sure no key is greater than or equal to zUpperBound.
** 5. Check the integrity of overflow pages.
** 6. Recursively call checkTreePage on all children.
** 7. Verify that the depth of all children is the same.
** 8. Make sure this page is at least 33% full or else it is
** the root of the tree.
*/
static int checkTreePage(
int iPage, /* Page number of the page to check */
char *zParentContext, /* Parent context */
char *zLowerBound, /* All keys should be greater than this, if not NULL */
int nLower, /* Number of characters in zLowerBound */
char *zUpperBound, /* All keys should be less than this, if not NULL */
int nUpper /* Number of characters in zUpperBound */
){
char zMsg[100];
char zContext[100];
char hit[SQLITE_USABLE_SIZE];
/* Check that the page exists
*/
if( iPage==0 ) return 0;
return 0;
}
return 0;
}
/* Check out all the cells.
*/
depth = 0;
if( zLowerBound ){
}else{
zKey1 = 0;
}
int sz;
/* Check payload overflow pages
*/
if( sz>MX_LOCAL_PAYLOAD ){
}
/* Check that keys are in the right order
*/
}
/* Check sanity of left child page.
*/
}
}
/* Check for complete coverage of the page
*/
int j;
}
int j;
}
for(i=0; i<SQLITE_USABLE_SIZE; i++){
if( hit[i]==0 ){
break;
}else if( hit[i]>1 ){
break;
}
}
/* Check that free space is kept to a minimum
*/
#if 0
}
#endif
return depth;
}
/*
** This routine does a complete check of the given BTree file. aRoot[] is
** an array of pages numbers were each page number is the root page of
** a table. nRoot is the number of entries in aRoot.
**
** If everything checks out, this routine returns NULL. If something is
** amiss, an error message is written into memory obtained from malloc()
** and a pointer to that error message is returned. The calling function
** is responsible for freeing the error message when it is done.
*/
int i;
int nRef;
return sqliteStrDup("Unable to acquire a read lock on the database");
}
return 0;
}
/* Check the integrity of the freelist
*/
/* Check all the tables.
*/
for(i=0; i<nRoot; i++){
if( aRoot[i]==0 ) continue;
}
/* Make sure every page in the file is referenced
*/
char zBuf[100];
}
}
/* Make sure this analysis did not leave any unref() pages
*/
char zBuf[100];
"Outstanding page count goes from %d to %d during this analysis",
);
}
/* Clean up and report errors.
*/
}
/*
** Return the full pathname of the underlying database file.
*/
}
/*
** Copy the complete content of pBtFrom into pBtTo. A transaction
** must be active for both files.
**
** The size of file pBtFrom may be reduced by this operation.
** If anything goes wrong, the transaction on pBtFrom is rolled back.
*/
void *pPage;
if( rc ) break;
if( rc ) break;
}
void *pPage;
if( rc ) break;
}
}
if( rc ){
}
return rc;
}
/*
** The following tables contain pointers to all of the interface
** routines for this implementation of the B*Tree backend. To
** substitute a different implemention of the backend, one has merely
** to provide pointers to alternative functions in similar tables.
*/
static BtOps sqliteBtreeOps = {
fileBtreeCreateTable, /* Really sqliteBtreeCreateIndex() */
#ifdef SQLITE_TEST
#endif
};
static BtCursorOps sqliteBtreeCursorOps = {
#ifdef SQLITE_TEST
#endif
};