Back to index

php5  5.3.10
Classes | Defines | Typedefs | Functions | Variables
btree.c File Reference
#include "sqliteInt.h"
#include "pager.h"
#include "btree.h"
#include <assert.h>

Go to the source code of this file.

Classes

struct  PageOne
struct  PageHdr
struct  CellHdr
struct  Cell
struct  FreeBlk
struct  OverflowPage
struct  FreelistInfo
struct  MemPage
union  MemPage::u_page_data
struct  Btree
struct  BtCursor
struct  IntegrityCk

Defines

#define SWAB16(B, X)   ((B)->needSwab? swab16((u16)X) : ((u16)X))
#define SWAB32(B, X)   ((B)->needSwab? swab32(X) : (X))
#define SWAB_ADD(B, X, A)   if((B)->needSwab){ X=swab32(swab32(X)+A); }else{ X += (A); }
#define btree_native_byte_order   1
#define ROUNDUP(X)   ((X+3) & ~3)
#define MAGIC_SIZE   (sizeof(zMagicHeader))
#define MAGIC   0xdae37528
#define NKEY(b, h)   (SWAB16(b,h.nKey) + h.nKeyHi*65536)
#define NDATA(b, h)   (SWAB16(b,h.nData) + h.nDataHi*65536)
#define MIN_CELL_SIZE   (sizeof(CellHdr)+4)
#define MX_CELL   ((SQLITE_USABLE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)
#define USABLE_SPACE   (SQLITE_USABLE_SIZE - sizeof(PageHdr))
#define MX_LOCAL_PAYLOAD   ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3)
#define OVERFLOW_SIZE   (SQLITE_USABLE_SIZE-sizeof(Pgno))
#define EXTRA_SIZE   (sizeof(MemPage)-sizeof(union u_page_data))
#define SKIP_NONE   0 /* Always step the cursor */
#define SKIP_NEXT   1 /* The next sqliteBtreeNext() is a no-op */
#define SKIP_PREV   2 /* The next sqliteBtreePrevious() is a no-op */
#define SKIP_INVALID   3 /* Calls to Next() and Previous() are invalid */
#define NN   1 /* Number of neighbors on either side of pPage */
#define NB   (NN*2+1) /* Total pages involved in the balance */

Typedefs

typedef struct PageOne
typedef struct MemPage
typedef struct PageHdr
typedef struct Cell
typedef struct CellHdr
typedef struct FreeBlk
typedef struct OverflowPage
typedef struct FreelistInfo
typedef Btree Bt
typedef struct IntegrityCk

Functions

static int fileBtreeCloseCursor (BtCursor *pCur)
u16 swab16 (u16 x)
u32 swab32 (u32 x)
static int cellSize (Btree *pBt, Cell *pCell)
static void defragmentPage (Btree *pBt, MemPage *pPage)
static int allocateSpace (Btree *pBt, MemPage *pPage, int nByte)
static void freeSpace (Btree *pBt, MemPage *pPage, int start, int size)
static int initPage (Bt *pBt, MemPage *pPage, Pgno pgnoThis, MemPage *pParent)
static void zeroPage (Btree *pBt, MemPage *pPage)
static void pageDestructor (void *pData)
int sqliteBtreeOpen (const char *zFilename, int omitJournal, int nCache, Btree **ppBtree)
static int fileBtreeClose (Btree *pBt)
static int fileBtreeSetCacheSize (Btree *pBt, int mxPage)
static int fileBtreeSetSafetyLevel (Btree *pBt, int level)
static int lockBtree (Btree *pBt)
static void unlockBtreeIfUnused (Btree *pBt)
static int newDatabase (Btree *pBt)
static int fileBtreeBeginTrans (Btree *pBt)
static int fileBtreeCommit (Btree *pBt)
static int fileBtreeRollback (Btree *pBt)
static int fileBtreeBeginCkpt (Btree *pBt)
static int fileBtreeCommitCkpt (Btree *pBt)
static int fileBtreeRollbackCkpt (Btree *pBt)
static int fileBtreeCursor (Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur)
static void getTempCursor (BtCursor *pCur, BtCursor *pTempCur)
static void releaseTempCursor (BtCursor *pCur)
static int fileBtreeKeySize (BtCursor *pCur, int *pSize)
static int getPayload (BtCursor *pCur, int offset, int amt, char *zBuf)
static int fileBtreeKey (BtCursor *pCur, int offset, int amt, char *zBuf)
static int fileBtreeDataSize (BtCursor *pCur, int *pSize)
static int fileBtreeData (BtCursor *pCur, int offset, int amt, char *zBuf)
static int fileBtreeKeyCompare (BtCursor *pCur, const void *pKey, int nKey, int nIgnore, int *pResult)
static int moveToChild (BtCursor *pCur, int newPgno)
static void moveToParent (BtCursor *pCur)
static int moveToRoot (BtCursor *pCur)
static int moveToLeftmost (BtCursor *pCur)
static int moveToRightmost (BtCursor *pCur)
static int fileBtreeFirst (BtCursor *pCur, int *pRes)
static int fileBtreeLast (BtCursor *pCur, int *pRes)
static int fileBtreeMoveto (BtCursor *pCur, const void *pKey, int nKey, int *pRes)
static int fileBtreeNext (BtCursor *pCur, int *pRes)
static int fileBtreePrevious (BtCursor *pCur, int *pRes)
static int allocatePage (Btree *pBt, MemPage **ppPage, Pgno *pPgno, Pgno nearby)
static int freePage (Btree *pBt, void *pPage, Pgno pgno)
static int clearCell (Btree *pBt, Cell *pCell)
static int fillInCell (Btree *pBt, Cell *pCell, const void *pKey, int nKey, const void *pData, int nData)
static void reparentPage (Pager *pPager, Pgno pgno, MemPage *pNewParent, int idx)
static void reparentChildPages (Btree *pBt, MemPage *pPage)
static void dropCell (Btree *pBt, MemPage *pPage, int idx, int sz)
static void insertCell (Btree *pBt, MemPage *pPage, int i, Cell *pCell, int sz)
static void relinkCellList (Btree *pBt, MemPage *pPage)
static void copyPage (MemPage *pTo, MemPage *pFrom)
static int balance (Btree *pBt, MemPage *pPage, BtCursor *pCur)
static int checkReadLocks (BtCursor *pCur)
static int fileBtreeInsert (BtCursor *pCur, const void *pKey, int nKey, const void *pData, int nData)
static int fileBtreeDelete (BtCursor *pCur)
static int fileBtreeCreateTable (Btree *pBt, int *piTable)
static int clearDatabasePage (Btree *pBt, Pgno pgno, int freePageFlag)
static int fileBtreeClearTable (Btree *pBt, int iTable)
static int fileBtreeDropTable (Btree *pBt, int iTable)
static int fileBtreeGetMeta (Btree *pBt, int *aMeta)
static int fileBtreeUpdateMeta (Btree *pBt, int *aMeta)
static PagerfileBtreePager (Btree *pBt)
static void checkAppendMsg (IntegrityCk *pCheck, char *zMsg1, char *zMsg2)
static int checkRef (IntegrityCk *pCheck, int iPage, char *zContext)
static void checkList (IntegrityCk *pCheck, int isFreeList, int iPage, int N, char *zContext)
static int keyCompare (const char *zKey1, int nKey1, const char *zKey2, int nKey2)
static int checkTreePage (IntegrityCk *pCheck, int iPage, MemPage *pParent, char *zParentContext, char *zLowerBound, int nLower, char *zUpperBound, int nUpper)
char * fileBtreeIntegrityCheck (Btree *pBt, int *aRoot, int nRoot)
static const char * fileBtreeGetFilename (Btree *pBt)
static int fileBtreeCopyFile (Btree *pBtTo, Btree *pBtFrom)

Variables

static BtOps sqliteBtreeOps
static BtCursorOps sqliteBtreeCursorOps
static const char zMagicHeader [] = "** This file contains an SQLite 2.1 database **"

Class Documentation

struct PageOne

Definition at line 143 of file btree.c.

Class Members
int aMeta
Pgno freeList
int iMagic
int nFree
char zMagic
struct PageHdr

Definition at line 169 of file btree.c.

Class Members
u16 firstCell
u16 firstFree
Pgno rightChild
struct CellHdr

Definition at line 185 of file btree.c.

Class Members
u16 iNext
Pgno leftChild
u16 nData
u8 nDataHi
u16 nKey
u8 nKeyHi
struct Cell

Definition at line 244 of file btree.c.

Class Members
char aPayload
CellHdr h
Pgno ovfl
struct FreeBlk

Definition at line 256 of file btree.c.

Class Members
u16 iNext
u16 iSize
struct OverflowPage

Definition at line 277 of file btree.c.

Class Members
char aPayload
Pgno iNext
struct FreelistInfo

Definition at line 289 of file btree.c.

Class Members
Pgno aFree
int nFree
struct MemPage

Definition at line 321 of file btree.c.

Collaboration diagram for MemPage:
Class Members
u8 * aData
struct _OvflCell aOvfl
Cell * apCell
u16 cellOffset
u8 childPtrSize
u8 hasData
u8 hdrOffset
int idxParent
u8 idxShift
u8 intKey
u8 isInit
u8 isOverfull
u8 leaf
u16 maskPage
u16 maxLocal
u16 minLocal
int nCell
u16 nCell
int nFree
u16 nFree
u8 nOverflow
BtShared * pBt
DbPage * pDbPage
Pgno pgno
MemPage * pParent
union u_page_data u
union MemPage::u_page_data

Definition at line 322 of file btree.c.

Class Members
char aDisk
PageHdr hdr
struct Btree

Definition at line 346 of file btree.c.

Collaboration diagram for Btree:
Class Members
sqlite3 * db
u8 inCkpt
u8 inTrans
BtLock lock
u8 locked
int nBackup
u8 needSwab
PageOne * page1
BtShared * pBt
BtCursor * pCursor
Btree * pNext
BtOps * pOps
Pager * pPager
Btree * pPrev
u8 readOnly
u8 sharable
int wantToLock
struct BtCursor

Definition at line 363 of file btree.c.

Collaboration diagram for BtCursor:
Class Members
u16 aiIdx
Pgno * aOverflow
MemPage * apPage
u8 atLast
sqlite3_int64 cachedRowid
u8 eSkip
u8 eState
int idx
u8 iMatch
CellInfo info
i16 iPage
u8 isIncrblobHandle
i64 nKey
Btree * pBt
BtShared * pBt
Btree * pBtree
Pgno pgnoRoot
void * pKey
struct KeyInfo * pKeyInfo
BtCursor * pNext
BtCursorOps * pOps
MemPage * pPage
BtCursor * pPrev
BtCursor * pShared
int skipNext
u8 validNKey
u8 wrFlag
struct IntegrityCk

Definition at line 3161 of file btree.c.

Collaboration diagram for IntegrityCk:
Class Members
int * anRef
StrAccum errMsg
int mallocFailed
int mxErr
int nErr
int nPage
Pgno nPage
Btree * pBt
BtShared * pBt
Pager * pPager
char * zErrMsg

Define Documentation

#define btree_native_byte_order   1

Definition at line 84 of file btree.c.

#define EXTRA_SIZE   (sizeof(MemPage)-sizeof(union u_page_data))

Definition at line 341 of file btree.c.

#define MAGIC   0xdae37528

Definition at line 127 of file btree.c.

#define MAGIC_SIZE   (sizeof(zMagicHeader))

Definition at line 114 of file btree.c.

#define MIN_CELL_SIZE   (sizeof(CellHdr)+4)

Definition at line 207 of file btree.c.

#define MX_CELL   ((SQLITE_USABLE_SIZE-sizeof(PageHdr))/MIN_CELL_SIZE)

Definition at line 213 of file btree.c.

#define MX_LOCAL_PAYLOAD   ((USABLE_SPACE/4-(sizeof(CellHdr)+sizeof(Pgno)))&~3)

Definition at line 228 of file btree.c.

#define NB   (NN*2+1) /* Total pages involved in the balance */

Definition at line 2137 of file btree.c.

#define NDATA (   b,
 
)    (SWAB16(b,h.nData) + h.nDataHi*65536)

Definition at line 201 of file btree.c.

#define NKEY (   b,
 
)    (SWAB16(b,h.nKey) + h.nKeyHi*65536)

Definition at line 200 of file btree.c.

#define NN   1 /* Number of neighbors on either side of pPage */

Definition at line 2136 of file btree.c.

#define OVERFLOW_SIZE   (SQLITE_USABLE_SIZE-sizeof(Pgno))

Definition at line 264 of file btree.c.

#define ROUNDUP (   X)    ((X+3) & ~3)

Definition at line 106 of file btree.c.

#define SKIP_INVALID   3 /* Calls to Next() and Previous() are invalid */

Definition at line 382 of file btree.c.

#define SKIP_NEXT   1 /* The next sqliteBtreeNext() is a no-op */

Definition at line 380 of file btree.c.

#define SKIP_NONE   0 /* Always step the cursor */

Definition at line 379 of file btree.c.

#define SKIP_PREV   2 /* The next sqliteBtreePrevious() is a no-op */

Definition at line 381 of file btree.c.

#define SWAB16 (   B,
  X 
)    ((B)->needSwab? swab16((u16)X) : ((u16)X))

Definition at line 68 of file btree.c.

#define SWAB32 (   B,
  X 
)    ((B)->needSwab? swab32(X) : (X))

Definition at line 69 of file btree.c.

#define SWAB_ADD (   B,
  X,
  A 
)    if((B)->needSwab){ X=swab32(swab32(X)+A); }else{ X += (A); }

Definition at line 70 of file btree.c.

#define USABLE_SPACE   (SQLITE_USABLE_SIZE - sizeof(PageHdr))

Definition at line 219 of file btree.c.


Typedef Documentation

typedef Btree Bt

Definition at line 356 of file btree.c.

typedef struct Cell

Definition at line 93 of file btree.c.

typedef struct CellHdr

Definition at line 94 of file btree.c.

typedef struct FreeBlk

Definition at line 95 of file btree.c.

typedef struct FreelistInfo

Definition at line 97 of file btree.c.

typedef struct IntegrityCk

Definition at line 3160 of file btree.c.

typedef struct MemPage

Definition at line 91 of file btree.c.

typedef struct OverflowPage

Definition at line 96 of file btree.c.

typedef struct PageHdr

Definition at line 92 of file btree.c.

typedef struct PageOne

Definition at line 90 of file btree.c.


Function Documentation

static int allocatePage ( Btree pBt,
MemPage **  ppPage,
Pgno pPgno,
Pgno  nearby 
) [static]

Definition at line 1752 of file btree.c.

                                                                               {
  PageOne *pPage1 = pBt->page1;
  int rc;
  if( pPage1->freeList ){
    OverflowPage *pOvfl;
    FreelistInfo *pInfo;

    rc = sqlitepager_write(pPage1);
    if( rc ) return rc;
    SWAB_ADD(pBt, pPage1->nFree, -1);
    rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
                        (void**)&pOvfl);
    if( rc ) return rc;
    rc = sqlitepager_write(pOvfl);
    if( rc ){
      sqlitepager_unref(pOvfl);
      return rc;
    }
    pInfo = (FreelistInfo*)pOvfl->aPayload;
    if( pInfo->nFree==0 ){
      *pPgno = SWAB32(pBt, pPage1->freeList);
      pPage1->freeList = pOvfl->iNext;
      *ppPage = (MemPage*)pOvfl;
    }else{
      int closest, n;
      n = SWAB32(pBt, pInfo->nFree);
      if( n>1 && nearby>0 ){
        int i, dist;
        closest = 0;
        dist = SWAB32(pBt, pInfo->aFree[0]) - nearby;
        if( dist<0 ) dist = -dist;
        for(i=1; i<n; i++){
          int d2 = SWAB32(pBt, pInfo->aFree[i]) - nearby;
          if( d2<0 ) d2 = -d2;
          if( d2<dist ) closest = i;
        }
      }else{
        closest = 0;
      }
      SWAB_ADD(pBt, pInfo->nFree, -1);
      *pPgno = SWAB32(pBt, pInfo->aFree[closest]);
      pInfo->aFree[closest] = pInfo->aFree[n-1];
      rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
      sqlitepager_unref(pOvfl);
      if( rc==SQLITE_OK ){
        sqlitepager_dont_rollback(*ppPage);
        rc = sqlitepager_write(*ppPage);
      }
    }
  }else{
    *pPgno = sqlitepager_pagecount(pBt->pPager) + 1;
    rc = sqlitepager_get(pBt->pPager, *pPgno, (void**)ppPage);
    if( rc ) return rc;
    rc = sqlitepager_write(*ppPage);
  }
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int allocateSpace ( Btree pBt,
MemPage pPage,
int  nByte 
) [static]

Definition at line 470 of file btree.c.

                                                               {
  FreeBlk *p;
  u16 *pIdx;
  int start;
  int iSize;
#ifndef NDEBUG
  int cnt = 0;
#endif

  assert( sqlitepager_iswriteable(pPage) );
  assert( nByte==ROUNDUP(nByte) );
  assert( pPage->isInit );
  if( pPage->nFree<nByte || pPage->isOverfull ) return 0;
  pIdx = &pPage->u.hdr.firstFree;
  p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
  while( (iSize = SWAB16(pBt, p->iSize))<nByte ){
    assert( cnt++ < SQLITE_USABLE_SIZE/4 );
    if( p->iNext==0 ){
      defragmentPage(pBt, pPage);
      pIdx = &pPage->u.hdr.firstFree;
    }else{
      pIdx = &p->iNext;
    }
    p = (FreeBlk*)&pPage->u.aDisk[SWAB16(pBt, *pIdx)];
  }
  if( iSize==nByte ){
    start = SWAB16(pBt, *pIdx);
    *pIdx = p->iNext;
  }else{
    FreeBlk *pNew;
    start = SWAB16(pBt, *pIdx);
    pNew = (FreeBlk*)&pPage->u.aDisk[start + nByte];
    pNew->iNext = p->iNext;
    pNew->iSize = SWAB16(pBt, iSize - nByte);
    *pIdx = SWAB16(pBt, start + nByte);
  }
  pPage->nFree -= nByte;
  return start;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int balance ( Btree pBt,
MemPage pPage,
BtCursor pCur 
) [static]

Definition at line 2179 of file btree.c.

                                                              {
  MemPage *pParent;            /* The parent of pPage */
  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 */
  MemPage *pOldCurPage;        /* The cursor originally points to this page */
  int subtotal;                /* Subtotal of bytes in cells on one page */
  MemPage *extraUnref = 0;     /* A page that needs to be unref-ed */
  MemPage *apOld[NB];          /* pPage and up to two siblings */
  Pgno pgnoOld[NB];            /* Page numbers for each page in apOld[] */
  MemPage *apNew[NB+1];        /* pPage and up to NB siblings after balancing */
  Pgno pgnoNew[NB+1];          /* Page numbers for each page in apNew[] */
  int idxDiv[NB];              /* Indices of divider cells in pParent */
  Cell *apDiv[NB];             /* Divider cells in pParent */
  Cell aTemp[NB];              /* Temporary holding area for apDiv[] */
  int cntNew[NB+1];            /* Index in apCell[] of cell after i-th page */
  int szNew[NB+1];             /* Combined size of cells place on i-th page */
  MemPage aOld[NB];            /* Temporary copies of pPage and its siblings */
  Cell *apCell[(MX_CELL+2)*NB]; /* All cells from pages being balanced */
  int szCell[(MX_CELL+2)*NB];  /* Local size of all cells */

  /* 
  ** Return without doing any work if pPage is neither overfull nor
  ** underfull.
  */
  assert( sqlitepager_iswriteable(pPage) );
  if( !pPage->isOverfull && pPage->nFree<SQLITE_USABLE_SIZE/2 
        && pPage->nCell>=2){
    relinkCellList(pBt, pPage);
    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.
  */
  pParent = pPage->pParent;
  if( pParent==0 ){
    Pgno pgnoChild;
    MemPage *pChild;
    assert( pPage->isInit );
    if( pPage->nCell==0 ){
      if( pPage->u.hdr.rightChild ){
        /*
        ** 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.
        */
        pgnoChild = SWAB32(pBt, pPage->u.hdr.rightChild);
        rc = sqlitepager_get(pBt->pPager, pgnoChild, (void**)&pChild);
        if( rc ) return rc;
        memcpy(pPage, pChild, SQLITE_USABLE_SIZE);
        pPage->isInit = 0;
        rc = initPage(pBt, pPage, sqlitepager_pagenumber(pPage), 0);
        assert( rc==SQLITE_OK );
        reparentChildPages(pBt, pPage);
        if( pCur && pCur->pPage==pChild ){
          sqlitepager_unref(pChild);
          pCur->pPage = pPage;
          sqlitepager_ref(pPage);
        }
        freePage(pBt, pChild, pgnoChild);
        sqlitepager_unref(pChild);
      }else{
        relinkCellList(pBt, pPage);
      }
      return SQLITE_OK;
    }
    if( !pPage->isOverfull ){
      /* It is OK for the root page to be less than half full.
      */
      relinkCellList(pBt, pPage);
      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.
    */
    rc = sqlitepager_write(pPage);
    if( rc ) return rc;
    rc = allocatePage(pBt, &pChild, &pgnoChild, sqlitepager_pagenumber(pPage));
    if( rc ) return rc;
    assert( sqlitepager_iswriteable(pChild) );
    copyPage(pChild, pPage);
    pChild->pParent = pPage;
    pChild->idxParent = 0;
    sqlitepager_ref(pPage);
    pChild->isOverfull = 1;
    if( pCur && pCur->pPage==pPage ){
      sqlitepager_unref(pPage);
      pCur->pPage = pChild;
    }else{
      extraUnref = pChild;
    }
    zeroPage(pBt, pPage);
    pPage->u.hdr.rightChild = SWAB32(pBt, pgnoChild);
    pParent = pPage;
    pPage = pChild;
  }
  rc = sqlitepager_write(pParent);
  if( rc ) return rc;
  assert( pParent->isInit );
  
  /*
  ** 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 
  */
  if( pParent->idxShift ){
    Pgno pgno, swabPgno;
    pgno = sqlitepager_pagenumber(pPage);
    swabPgno = SWAB32(pBt, pgno);
    for(idx=0; idx<pParent->nCell; idx++){
      if( pParent->apCell[idx]->h.leftChild==swabPgno ){
        break;
      }
    }
    assert( idx<pParent->nCell || pParent->u.hdr.rightChild==swabPgno );
  }else{
    idx = pPage->idxParent;
  }

  /*
  ** Initialize variables so that it will be safe to jump
  ** directly to balance_cleanup at any moment.
  */
  nOld = nNew = 0;
  sqlitepager_ref(pParent);

  /*
  ** 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.
  */
  nxDiv = idx - NN;
  if( nxDiv + NB > pParent->nCell ){
    nxDiv = pParent->nCell - NB + 1;
  }
  if( nxDiv<0 ){
    nxDiv = 0;
  }
  nDiv = 0;
  for(i=0, k=nxDiv; i<NB; i++, k++){
    if( k<pParent->nCell ){
      idxDiv[i] = k;
      apDiv[i] = pParent->apCell[k];
      nDiv++;
      pgnoOld[i] = SWAB32(pBt, apDiv[i]->h.leftChild);
    }else if( k==pParent->nCell ){
      pgnoOld[i] = SWAB32(pBt, pParent->u.hdr.rightChild);
    }else{
      break;
    }
    rc = sqlitepager_get(pBt->pPager, pgnoOld[i], (void**)&apOld[i]);
    if( rc ) goto balance_cleanup;
    rc = initPage(pBt, apOld[i], pgnoOld[i], pParent);
    if( rc ) goto balance_cleanup;
    apOld[i]->idxParent = k;
    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++){
      if( pCur->pPage==apOld[i] ){
        iCur += pCur->idx;
        break;
      }
      iCur += apOld[i]->nCell;
      if( i<nOld-1 && pCur->pPage==pParent && pCur->idx==idxDiv[i] ){
        break;
      }
      iCur++;
    }
    pOldCurPage = pCur->pPage;
  }

  /*
  ** 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++){
    copyPage(&aOld[i], apOld[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++){
    MemPage *pOld = &aOld[i];
    for(j=0; j<pOld->nCell; j++){
      apCell[nCell] = pOld->apCell[j];
      szCell[nCell] = cellSize(pBt, apCell[nCell]);
      nCell++;
    }
    if( i<nOld-1 ){
      szCell[nCell] = cellSize(pBt, apDiv[i]);
      memcpy(&aTemp[i], apDiv[i], szCell[nCell]);
      apCell[nCell] = &aTemp[i];
      dropCell(pBt, pParent, nxDiv, szCell[nCell]);
      assert( SWAB32(pBt, apCell[nCell]->h.leftChild)==pgnoOld[i] );
      apCell[nCell]->h.leftChild = pOld->u.hdr.rightChild;
      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. 
  */
  for(subtotal=k=i=0; i<nCell; i++){
    subtotal += szCell[i];
    if( subtotal > USABLE_SPACE ){
      szNew[k] = subtotal - szCell[i];
      cntNew[k] = i;
      subtotal = 0;
      k++;
    }
  }
  szNew[k] = subtotal;
  cntNew[k] = nCell;
  k++;
  for(i=k-1; i>0; i--){
    while( szNew[i]<USABLE_SPACE/2 ){
      cntNew[i-1]--;
      assert( cntNew[i-1]>0 );
      szNew[i] += szCell[cntNew[i-1]];
      szNew[i-1] -= szCell[cntNew[i-1]-1];
    }
  }
  assert( cntNew[0]>0 );

  /*
  ** Allocate k new pages.  Reuse old pages where possible.
  */
  for(i=0; i<k; i++){
    if( i<nOld ){
      apNew[i] = apOld[i];
      pgnoNew[i] = pgnoOld[i];
      apOld[i] = 0;
      sqlitepager_write(apNew[i]);
    }else{
      rc = allocatePage(pBt, &apNew[i], &pgnoNew[i], pgnoNew[i-1]);
      if( rc ) goto balance_cleanup;
    }
    nNew++;
    zeroPage(pBt, apNew[i]);
    apNew[i]->isInit = 1;
  }

  /* Free any old pages that were not reused as new pages.
  */
  while( i<nOld ){
    rc = freePage(pBt, apOld[i], pgnoOld[i]);
    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 minV = pgnoNew[i];
    int minI = i;
    for(j=i+1; j<k; j++){
      if( pgnoNew[j]<(unsigned)minV ){
        minI = j;
        minV = pgnoNew[j];
      }
    }
    if( minI>i ){
      int t;
      MemPage *pT;
      t = pgnoNew[i];
      pT = apNew[i];
      pgnoNew[i] = pgnoNew[minI];
      apNew[i] = apNew[minI];
      pgnoNew[minI] = t;
      apNew[minI] = pT;
    }
  }

  /*
  ** 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++){
    MemPage *pNew = apNew[i];
    while( j<cntNew[i] ){
      assert( pNew->nFree>=szCell[j] );
      if( pCur && iCur==j ){ pCur->pPage = pNew; pCur->idx = pNew->nCell; }
      insertCell(pBt, pNew, pNew->nCell, apCell[j], szCell[j]);
      j++;
    }
    assert( pNew->nCell>0 );
    assert( !pNew->isOverfull );
    relinkCellList(pBt, pNew);
    if( i<nNew-1 && j<nCell ){
      pNew->u.hdr.rightChild = apCell[j]->h.leftChild;
      apCell[j]->h.leftChild = SWAB32(pBt, pgnoNew[i]);
      if( pCur && iCur==j ){ pCur->pPage = pParent; pCur->idx = nxDiv; }
      insertCell(pBt, pParent, nxDiv, apCell[j], szCell[j]);
      j++;
      nxDiv++;
    }
  }
  assert( j==nCell );
  apNew[nNew-1]->u.hdr.rightChild = aOld[nOld-1].u.hdr.rightChild;
  if( nxDiv==pParent->nCell ){
    pParent->u.hdr.rightChild = SWAB32(pBt, pgnoNew[nNew-1]);
  }else{
    pParent->apCell[nxDiv]->h.leftChild = SWAB32(pBt, pgnoNew[nNew-1]);
  }
  if( pCur ){
    if( j<=iCur && pCur->pPage==pParent && pCur->idx>idxDiv[nOld-1] ){
      assert( pCur->pPage==pOldCurPage );
      pCur->idx += nNew - nOld;
    }else{
      assert( pOldCurPage!=0 );
      sqlitepager_ref(pCur->pPage);
      sqlitepager_unref(pOldCurPage);
    }
  }

  /*
  ** Reparent children of all cells.
  */
  for(i=0; i<nNew; i++){
    reparentChildPages(pBt, apNew[i]);
  }
  reparentChildPages(pBt, pParent);

  /*
  ** balance the parent page.
  */
  rc = balance(pBt, pParent, pCur);

  /*
  ** Cleanup before returning.
  */
balance_cleanup:
  if( extraUnref ){
    sqlitepager_unref(extraUnref);
  }
  for(i=0; i<nOld; i++){
    if( apOld[i]!=0 && apOld[i]!=&aOld[i] ) sqlitepager_unref(apOld[i]);
  }
  for(i=0; i<nNew; i++){
    sqlitepager_unref(apNew[i]);
  }
  if( pCur && pCur->pPage==0 ){
    pCur->pPage = pParent;
    pCur->idx = 0;
  }else{
    sqlitepager_unref(pParent);
  }
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int cellSize ( Btree pBt,
Cell pCell 
) [static]

Definition at line 405 of file btree.c.

                                            {
  int n = NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h);
  if( n>MX_LOCAL_PAYLOAD ){
    n = MX_LOCAL_PAYLOAD + sizeof(Pgno);
  }else{
    n = ROUNDUP(n);
  }
  n += sizeof(CellHdr);
  return n;
}

Here is the caller graph for this function:

static void checkAppendMsg ( IntegrityCk pCheck,
char *  zMsg1,
char *  zMsg2 
) [static]

Definition at line 3172 of file btree.c.

                                                                         {
  if( pCheck->zErrMsg ){
    char *zOld = pCheck->zErrMsg;
    pCheck->zErrMsg = 0;
    sqliteSetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
    sqliteFree(zOld);
  }else{
    sqliteSetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void checkList ( IntegrityCk pCheck,
int  isFreeList,
int  iPage,
int  N,
char *  zContext 
) [static]

Definition at line 3212 of file btree.c.

 {
  int i;
  char zMsg[100];
  while( N-- > 0 ){
    OverflowPage *pOvfl;
    if( iPage<1 ){
      sprintf(zMsg, "%d pages missing from overflow list", N+1);
      checkAppendMsg(pCheck, zContext, zMsg);
      break;
    }
    if( checkRef(pCheck, iPage, zContext) ) break;
    if( sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pOvfl) ){
      sprintf(zMsg, "failed to get page %d", iPage);
      checkAppendMsg(pCheck, zContext, zMsg);
      break;
    }
    if( isFreeList ){
      FreelistInfo *pInfo = (FreelistInfo*)pOvfl->aPayload;
      int n = SWAB32(pCheck->pBt, pInfo->nFree);
      for(i=0; i<n; i++){
        checkRef(pCheck, SWAB32(pCheck->pBt, pInfo->aFree[i]), zContext);
      }
      N -= n;
    }
    iPage = SWAB32(pCheck->pBt, pOvfl->iNext);
    sqlitepager_unref(pOvfl);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int checkReadLocks ( BtCursor pCur) [static]

Definition at line 2599 of file btree.c.

                                         {
  BtCursor *p;
  assert( pCur->wrFlag );
  for(p=pCur->pShared; p!=pCur; p=p->pShared){
    assert( p );
    assert( p->pgnoRoot==pCur->pgnoRoot );
    if( p->wrFlag==0 ) return SQLITE_LOCKED;
    if( sqlitepager_pagenumber(p->pPage)!=p->pgnoRoot ){
      moveToRoot(p);
    }
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int checkRef ( IntegrityCk pCheck,
int  iPage,
char *  zContext 
) [static]

Definition at line 3191 of file btree.c.

                                                                   {
  if( iPage==0 ) return 1;
  if( iPage>pCheck->nPage || iPage<0 ){
    char zBuf[100];
    sprintf(zBuf, "invalid page number %d", iPage);
    checkAppendMsg(pCheck, zContext, zBuf);
    return 1;
  }
  if( pCheck->anRef[iPage]==1 ){
    char zBuf[100];
    sprintf(zBuf, "2nd reference to page %d", iPage);
    checkAppendMsg(pCheck, zContext, zBuf);
    return 1;
  }
  return  (pCheck->anRef[iPage]++)>1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int checkTreePage ( IntegrityCk pCheck,
int  iPage,
MemPage pParent,
char *  zParentContext,
char *  zLowerBound,
int  nLower,
char *  zUpperBound,
int  nUpper 
) [static]

Definition at line 3282 of file btree.c.

 {
  MemPage *pPage;
  int i, rc, depth, d2, pgno;
  char *zKey1, *zKey2;
  int nKey1, nKey2;
  BtCursor cur;
  Btree *pBt;
  char zMsg[100];
  char zContext[100];
  char hit[SQLITE_USABLE_SIZE];

  /* Check that the page exists
  */
  cur.pBt = pBt = pCheck->pBt;
  if( iPage==0 ) return 0;
  if( checkRef(pCheck, iPage, zParentContext) ) return 0;
  sprintf(zContext, "On tree page %d: ", iPage);
  if( (rc = sqlitepager_get(pCheck->pPager, (Pgno)iPage, (void**)&pPage))!=0 ){
    sprintf(zMsg, "unable to get the page. error code=%d", rc);
    checkAppendMsg(pCheck, zContext, zMsg);
    return 0;
  }
  if( (rc = initPage(pBt, pPage, (Pgno)iPage, pParent))!=0 ){
    sprintf(zMsg, "initPage() returns error code %d", rc);
    checkAppendMsg(pCheck, zContext, zMsg);
    sqlitepager_unref(pPage);
    return 0;
  }

  /* Check out all the cells.
  */
  depth = 0;
  if( zLowerBound ){
    zKey1 = sqliteMalloc( nLower+1 );
    memcpy(zKey1, zLowerBound, nLower);
    zKey1[nLower] = 0;
  }else{
    zKey1 = 0;
  }
  nKey1 = nLower;
  cur.pPage = pPage;
  for(i=0; i<pPage->nCell; i++){
    Cell *pCell = pPage->apCell[i];
    int sz;

    /* Check payload overflow pages
    */
    nKey2 = NKEY(pBt, pCell->h);
    sz = nKey2 + NDATA(pBt, pCell->h);
    sprintf(zContext, "On page %d cell %d: ", iPage, i);
    if( sz>MX_LOCAL_PAYLOAD ){
      int nPage = (sz - MX_LOCAL_PAYLOAD + OVERFLOW_SIZE - 1)/OVERFLOW_SIZE;
      checkList(pCheck, 0, SWAB32(pBt, pCell->ovfl), nPage, zContext);
    }

    /* Check that keys are in the right order
    */
    cur.idx = i;
    zKey2 = sqliteMallocRaw( nKey2+1 );
    getPayload(&cur, 0, nKey2, zKey2);
    if( zKey1 && keyCompare(zKey1, nKey1, zKey2, nKey2)>=0 ){
      checkAppendMsg(pCheck, zContext, "Key is out of order");
    }

    /* Check sanity of left child page.
    */
    pgno = SWAB32(pBt, pCell->h.leftChild);
    d2 = checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zKey2,nKey2);
    if( i>0 && d2!=depth ){
      checkAppendMsg(pCheck, zContext, "Child page depth differs");
    }
    depth = d2;
    sqliteFree(zKey1);
    zKey1 = zKey2;
    nKey1 = nKey2;
  }
  pgno = SWAB32(pBt, pPage->u.hdr.rightChild);
  sprintf(zContext, "On page %d at right child: ", iPage);
  checkTreePage(pCheck, pgno, pPage, zContext, zKey1,nKey1,zUpperBound,nUpper);
  sqliteFree(zKey1);
 
  /* Check for complete coverage of the page
  */
  memset(hit, 0, sizeof(hit));
  memset(hit, 1, sizeof(PageHdr));
  for(i=SWAB16(pBt, pPage->u.hdr.firstCell); i>0 && i<SQLITE_USABLE_SIZE; ){
    Cell *pCell = (Cell*)&pPage->u.aDisk[i];
    int j;
    for(j=i+cellSize(pBt, pCell)-1; j>=i; j--) hit[j]++;
    i = SWAB16(pBt, pCell->h.iNext);
  }
  for(i=SWAB16(pBt,pPage->u.hdr.firstFree); i>0 && i<SQLITE_USABLE_SIZE; ){
    FreeBlk *pFBlk = (FreeBlk*)&pPage->u.aDisk[i];
    int j;
    for(j=i+SWAB16(pBt,pFBlk->iSize)-1; j>=i; j--) hit[j]++;
    i = SWAB16(pBt,pFBlk->iNext);
  }
  for(i=0; i<SQLITE_USABLE_SIZE; i++){
    if( hit[i]==0 ){
      sprintf(zMsg, "Unused space at byte %d of page %d", i, iPage);
      checkAppendMsg(pCheck, zMsg, 0);
      break;
    }else if( hit[i]>1 ){
      sprintf(zMsg, "Multiple uses for byte %d of page %d", i, iPage);
      checkAppendMsg(pCheck, zMsg, 0);
      break;
    }
  }

  /* Check that free space is kept to a minimum
  */
#if 0
  if( pParent && pParent->nCell>2 && pPage->nFree>3*SQLITE_USABLE_SIZE/4 ){
    sprintf(zMsg, "free space (%d) greater than max (%d)", pPage->nFree,
       SQLITE_USABLE_SIZE/3);
    checkAppendMsg(pCheck, zContext, zMsg);
  }
#endif

  sqlitepager_unref(pPage);
  return depth;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int clearCell ( Btree pBt,
Cell pCell 
) [static]

Definition at line 1882 of file btree.c.

                                             {
  Pager *pPager = pBt->pPager;
  OverflowPage *pOvfl;
  Pgno ovfl, nextOvfl;
  int rc;

  if( NKEY(pBt, pCell->h) + NDATA(pBt, pCell->h) <= MX_LOCAL_PAYLOAD ){
    return SQLITE_OK;
  }
  ovfl = SWAB32(pBt, pCell->ovfl);
  pCell->ovfl = 0;
  while( ovfl ){
    rc = sqlitepager_get(pPager, ovfl, (void**)&pOvfl);
    if( rc ) return rc;
    nextOvfl = SWAB32(pBt, pOvfl->iNext);
    rc = freePage(pBt, pOvfl, ovfl);
    if( rc ) return rc;
    sqlitepager_unref(pOvfl);
    ovfl = nextOvfl;
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int clearDatabasePage ( Btree pBt,
Pgno  pgno,
int  freePageFlag 
) [static]

Definition at line 2800 of file btree.c.

                                                                     {
  MemPage *pPage;
  int rc;
  Cell *pCell;
  int idx;

  rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pPage);
  if( rc ) return rc;
  rc = sqlitepager_write(pPage);
  if( rc ) return rc;
  rc = initPage(pBt, pPage, pgno, 0);
  if( rc ) return rc;
  idx = SWAB16(pBt, pPage->u.hdr.firstCell);
  while( idx>0 ){
    pCell = (Cell*)&pPage->u.aDisk[idx];
    idx = SWAB16(pBt, pCell->h.iNext);
    if( pCell->h.leftChild ){
      rc = clearDatabasePage(pBt, SWAB32(pBt, pCell->h.leftChild), 1);
      if( rc ) return rc;
    }
    rc = clearCell(pBt, pCell);
    if( rc ) return rc;
  }
  if( pPage->u.hdr.rightChild ){
    rc = clearDatabasePage(pBt, SWAB32(pBt, pPage->u.hdr.rightChild), 1);
    if( rc ) return rc;
  }
  if( freePageFlag ){
    rc = freePage(pBt, pPage, pgno);
  }else{
    zeroPage(pBt, pPage);
  }
  sqlitepager_unref(pPage);
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void copyPage ( MemPage pTo,
MemPage pFrom 
) [static]

Definition at line 2103 of file btree.c.

                                                  {
  uptr from, to;
  int i;
  memcpy(pTo->u.aDisk, pFrom->u.aDisk, SQLITE_USABLE_SIZE);
  pTo->pParent = 0;
  pTo->isInit = 1;
  pTo->nCell = pFrom->nCell;
  pTo->nFree = pFrom->nFree;
  pTo->isOverfull = pFrom->isOverfull;
  to = Addr(pTo);
  from = Addr(pFrom);
  for(i=0; i<pTo->nCell; i++){
    uptr x = Addr(pFrom->apCell[i]);
    if( x>from && x<from+SQLITE_USABLE_SIZE ){
      *((uptr*)&pTo->apCell[i]) = x + to - from;
    }else{
      pTo->apCell[i] = pFrom->apCell[i];
    }
  }
}

Here is the caller graph for this function:

static void defragmentPage ( Btree pBt,
MemPage pPage 
) [static]

Definition at line 421 of file btree.c.

                                                      {
  int pc, i, n;
  FreeBlk *pFBlk;
  char newPage[SQLITE_USABLE_SIZE];

  assert( sqlitepager_iswriteable(pPage) );
  assert( pPage->isInit );
  pc = sizeof(PageHdr);
  pPage->u.hdr.firstCell = SWAB16(pBt, pc);
  memcpy(newPage, pPage->u.aDisk, pc);
  for(i=0; i<pPage->nCell; i++){
    Cell *pCell = pPage->apCell[i];

    /* This routine should never be called on an overfull page.  The
    ** following asserts verify that constraint. */
    assert( Addr(pCell) > Addr(pPage) );
    assert( Addr(pCell) < Addr(pPage) + SQLITE_USABLE_SIZE );

    n = cellSize(pBt, pCell);
    pCell->h.iNext = SWAB16(pBt, pc + n);
    memcpy(&newPage[pc], pCell, n);
    pPage->apCell[i] = (Cell*)&pPage->u.aDisk[pc];
    pc += n;
  }
  assert( pPage->nFree==SQLITE_USABLE_SIZE-pc );
  memcpy(pPage->u.aDisk, newPage, pc);
  if( pPage->nCell>0 ){
    pPage->apCell[pPage->nCell-1]->h.iNext = 0;
  }
  pFBlk = (FreeBlk*)&pPage->u.aDisk[pc];
  pFBlk->iSize = SWAB16(pBt, SQLITE_USABLE_SIZE - pc);
  pFBlk->iNext = 0;
  pPage->u.hdr.firstFree = SWAB16(pBt, pc);
  memset(&pFBlk[1], 0, SQLITE_USABLE_SIZE - pc - sizeof(FreeBlk));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void dropCell ( Btree pBt,
MemPage pPage,
int  idx,
int  sz 
) [static]

Definition at line 2031 of file btree.c.

                                                                 {
  int j;
  assert( idx>=0 && idx<pPage->nCell );
  assert( sz==cellSize(pBt, pPage->apCell[idx]) );
  assert( sqlitepager_iswriteable(pPage) );
  freeSpace(pBt, pPage, Addr(pPage->apCell[idx]) - Addr(pPage), sz);
  for(j=idx; j<pPage->nCell-1; j++){
    pPage->apCell[j] = pPage->apCell[j+1];
  }
  pPage->nCell--;
  pPage->idxShift = 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeBeginCkpt ( Btree pBt) [static]

Definition at line 949 of file btree.c.

                                         {
  int rc;
  if( !pBt->inTrans || pBt->inCkpt ){
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  rc = pBt->readOnly ? SQLITE_OK : sqlitepager_ckpt_begin(pBt->pPager);
  pBt->inCkpt = 1;
  return rc;
}

Here is the call graph for this function:

static int fileBtreeBeginTrans ( Btree pBt) [static]

Definition at line 875 of file btree.c.

                                          {
  int rc;
  if( pBt->inTrans ) return SQLITE_ERROR;
  if( pBt->readOnly ) return SQLITE_READONLY;
  if( pBt->page1==0 ){
    rc = lockBtree(pBt);
    if( rc!=SQLITE_OK ){
      return rc;
    }
  }
  rc = sqlitepager_begin(pBt->page1);
  if( rc==SQLITE_OK ){
    rc = newDatabase(pBt);
  }
  if( rc==SQLITE_OK ){
    pBt->inTrans = 1;
    pBt->inCkpt = 0;
  }else{
    unlockBtreeIfUnused(pBt);
  }
  return rc;
}

Here is the call graph for this function:

static int fileBtreeClearTable ( Btree pBt,
int  iTable 
) [static]

Definition at line 2839 of file btree.c.

                                                      {
  int rc;
  BtCursor *pCur;
  if( !pBt->inTrans ){
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
    if( pCur->pgnoRoot==(Pgno)iTable ){
      if( pCur->wrFlag==0 ) return SQLITE_LOCKED;
      moveToRoot(pCur);
    }
  }
  rc = clearDatabasePage(pBt, (Pgno)iTable, 0);
  if( rc ){
    fileBtreeRollback(pBt);
  }
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeClose ( Btree pBt) [static]

Definition at line 731 of file btree.c.

                                     {
  while( pBt->pCursor ){
    fileBtreeCloseCursor(pBt->pCursor);
  }
  sqlitepager_close(pBt->pPager);
  sqliteFree(pBt);
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fileBtreeCloseCursor ( BtCursor pCur) [static]

Definition at line 1100 of file btree.c.

                                               {
  Btree *pBt = pCur->pBt;
  if( pCur->pPrev ){
    pCur->pPrev->pNext = pCur->pNext;
  }else{
    pBt->pCursor = pCur->pNext;
  }
  if( pCur->pNext ){
    pCur->pNext->pPrev = pCur->pPrev;
  }
  if( pCur->pPage ){
    sqlitepager_unref(pCur->pPage);
  }
  if( pCur->pShared!=pCur ){
    BtCursor *pRing = pCur->pShared;
    while( pRing->pShared!=pCur ){ pRing = pRing->pShared; }
    pRing->pShared = pCur->pShared;
  }
  unlockBtreeIfUnused(pBt);
  sqliteFree(pCur);
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeCommit ( Btree pBt) [static]

Definition at line 904 of file btree.c.

                                      {
  int rc;
  rc = pBt->readOnly ? SQLITE_OK : sqlitepager_commit(pBt->pPager);
  pBt->inTrans = 0;
  pBt->inCkpt = 0;
  unlockBtreeIfUnused(pBt);
  return rc;
}

Here is the call graph for this function:

static int fileBtreeCommitCkpt ( Btree pBt) [static]

Definition at line 964 of file btree.c.

                                          {
  int rc;
  if( pBt->inCkpt && !pBt->readOnly ){
    rc = sqlitepager_ckpt_commit(pBt->pPager);
  }else{
    rc = SQLITE_OK;
  }
  pBt->inCkpt = 0;
  return rc;
}

Here is the call graph for this function:

static int fileBtreeCopyFile ( Btree pBtTo,
Btree pBtFrom 
) [static]

Definition at line 3500 of file btree.c.

                                                          {
  int rc = SQLITE_OK;
  Pgno i, nPage, nToPage;

  if( !pBtTo->inTrans || !pBtFrom->inTrans ) return SQLITE_ERROR;
  if( pBtTo->needSwab!=pBtFrom->needSwab ) return SQLITE_ERROR;
  if( pBtTo->pCursor ) return SQLITE_BUSY;
  memcpy(pBtTo->page1, pBtFrom->page1, SQLITE_USABLE_SIZE);
  rc = sqlitepager_overwrite(pBtTo->pPager, 1, pBtFrom->page1);
  nToPage = sqlitepager_pagecount(pBtTo->pPager);
  nPage = sqlitepager_pagecount(pBtFrom->pPager);
  for(i=2; rc==SQLITE_OK && i<=nPage; i++){
    void *pPage;
    rc = sqlitepager_get(pBtFrom->pPager, i, &pPage);
    if( rc ) break;
    rc = sqlitepager_overwrite(pBtTo->pPager, i, pPage);
    if( rc ) break;
    sqlitepager_unref(pPage);
  }
  for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){
    void *pPage;
    rc = sqlitepager_get(pBtTo->pPager, i, &pPage);
    if( rc ) break;
    rc = sqlitepager_write(pPage);
    sqlitepager_unref(pPage);
    sqlitepager_dont_write(pBtTo->pPager, i);
  }
  if( !rc && nPage<nToPage ){
    rc = sqlitepager_truncate(pBtTo->pPager, nPage);
  }
  if( rc ){
    fileBtreeRollback(pBtTo);
  }
  return rc;  
}

Here is the call graph for this function:

static int fileBtreeCreateTable ( Btree pBt,
int piTable 
) [static]

Definition at line 2776 of file btree.c.

                                                         {
  MemPage *pRoot;
  Pgno pgnoRoot;
  int rc;
  if( !pBt->inTrans ){
    /* Must start a transaction first */
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  if( pBt->readOnly ){
    return SQLITE_READONLY;
  }
  rc = allocatePage(pBt, &pRoot, &pgnoRoot, 0);
  if( rc ) return rc;
  assert( sqlitepager_iswriteable(pRoot) );
  zeroPage(pBt, pRoot);
  sqlitepager_unref(pRoot);
  *piTable = (int)pgnoRoot;
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fileBtreeCursor ( Btree pBt,
int  iTable,
int  wrFlag,
BtCursor **  ppCur 
) [static]

Definition at line 1035 of file btree.c.

                                                                         {
  int rc;
  BtCursor *pCur, *pRing;

  if( pBt->readOnly && wrFlag ){
    *ppCur = 0;
    return SQLITE_READONLY;
  }
  if( pBt->page1==0 ){
    rc = lockBtree(pBt);
    if( rc!=SQLITE_OK ){
      *ppCur = 0;
      return rc;
    }
  }
  pCur = sqliteMalloc( sizeof(*pCur) );
  if( pCur==0 ){
    rc = SQLITE_NOMEM;
    goto create_cursor_exception;
  }
  pCur->pgnoRoot = (Pgno)iTable;
  rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pCur->pPage);
  if( rc!=SQLITE_OK ){
    goto create_cursor_exception;
  }
  rc = initPage(pBt, pCur->pPage, pCur->pgnoRoot, 0);
  if( rc!=SQLITE_OK ){
    goto create_cursor_exception;
  }
  pCur->pOps = &sqliteBtreeCursorOps;
  pCur->pBt = pBt;
  pCur->wrFlag = wrFlag;
  pCur->idx = 0;
  pCur->eSkip = SKIP_INVALID;
  pCur->pNext = pBt->pCursor;
  if( pCur->pNext ){
    pCur->pNext->pPrev = pCur;
  }
  pCur->pPrev = 0;
  pRing = pBt->pCursor;
  while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; }
  if( pRing ){
    pCur->pShared = pRing->pShared;
    pRing->pShared = pCur;
  }else{
    pCur->pShared = pCur;
  }
  pBt->pCursor = pCur;
  *ppCur = pCur;
  return SQLITE_OK;

create_cursor_exception:
  *ppCur = 0;
  if( pCur ){
    if( pCur->pPage ) sqlitepager_unref(pCur->pPage);
    sqliteFree(pCur);
  }
  unlockBtreeIfUnused(pBt);
  return rc;
}

Here is the call graph for this function:

static int fileBtreeData ( BtCursor pCur,
int  offset,
int  amt,
char *  zBuf 
) [static]

Definition at line 1287 of file btree.c.

                                                                         {
  Cell *pCell;
  MemPage *pPage;

  assert( amt>=0 );
  assert( offset>=0 );
  assert( pCur->pPage!=0 );
  pPage = pCur->pPage;
  if( pCur->idx >= pPage->nCell ){
    return 0;
  }
  pCell = pPage->apCell[pCur->idx];
  assert( amt+offset <= NDATA(pCur->pBt, pCell->h) );
  getPayload(pCur, offset + NKEY(pCur->pBt, pCell->h), amt, zBuf);
  return amt;
}

Here is the call graph for this function:

static int fileBtreeDataSize ( BtCursor pCur,
int pSize 
) [static]

Definition at line 1264 of file btree.c.

                                                        {
  Cell *pCell;
  MemPage *pPage;

  pPage = pCur->pPage;
  assert( pPage!=0 );
  if( pCur->idx >= pPage->nCell ){
    *pSize = 0;
  }else{
    pCell = pPage->apCell[pCur->idx];
    *pSize = NDATA(pCur->pBt, pCell->h);
  }
  return SQLITE_OK;
}
static int fileBtreeDelete ( BtCursor pCur) [static]

Definition at line 2687 of file btree.c.

                                          {
  MemPage *pPage = pCur->pPage;
  Cell *pCell;
  int rc;
  Pgno pgnoChild;
  Btree *pBt = pCur->pBt;

  assert( pPage->isInit );
  if( pCur->pPage==0 ){
    return SQLITE_ABORT;  /* A rollback destroyed this cursor */
  }
  if( !pBt->inTrans ){
    /* Must start a transaction before doing a delete */
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  assert( !pBt->readOnly );
  if( pCur->idx >= pPage->nCell ){
    return SQLITE_ERROR;  /* The cursor is not pointing to anything */
  }
  if( !pCur->wrFlag ){
    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 */
  }
  rc = sqlitepager_write(pPage);
  if( rc ) return rc;
  pCell = pPage->apCell[pCur->idx];
  pgnoChild = SWAB32(pBt, pCell->h.leftChild);
  clearCell(pBt, pCell);
  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.
    */
    BtCursor leafCur;
    Cell *pNext;
    int szNext;
    int notUsed;
    getTempCursor(pCur, &leafCur);
    rc = fileBtreeNext(&leafCur, &notUsed);
    if( rc!=SQLITE_OK ){
      if( rc!=SQLITE_NOMEM ) rc = SQLITE_CORRUPT;
      return rc;
    }
    rc = sqlitepager_write(leafCur.pPage);
    if( rc ) return rc;
    dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
    pNext = leafCur.pPage->apCell[leafCur.idx];
    szNext = cellSize(pBt, pNext);
    pNext->h.leftChild = SWAB32(pBt, pgnoChild);
    insertCell(pBt, pPage, pCur->idx, pNext, szNext);
    rc = balance(pBt, pPage, pCur);
    if( rc ) return rc;
    pCur->eSkip = SKIP_NEXT;
    dropCell(pBt, leafCur.pPage, leafCur.idx, szNext);
    rc = balance(pBt, leafCur.pPage, pCur);
    releaseTempCursor(&leafCur);
  }else{
    dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pCell));
    if( pCur->idx>=pPage->nCell ){
      pCur->idx = pPage->nCell-1;
      if( pCur->idx<0 ){ 
        pCur->idx = 0;
        pCur->eSkip = SKIP_NEXT;
      }else{
        pCur->eSkip = SKIP_PREV;
      }
    }else{
      pCur->eSkip = SKIP_NEXT;
    }
    rc = balance(pBt, pPage, pCur);
  }
  return rc;
}

Here is the call graph for this function:

static int fileBtreeDropTable ( Btree pBt,
int  iTable 
) [static]

Definition at line 2863 of file btree.c.

                                                     {
  int rc;
  MemPage *pPage;
  BtCursor *pCur;
  if( !pBt->inTrans ){
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
    if( pCur->pgnoRoot==(Pgno)iTable ){
      return SQLITE_LOCKED;  /* Cannot drop a table that has a cursor */
    }
  }
  rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
  if( rc ) return rc;
  rc = fileBtreeClearTable(pBt, iTable);
  if( rc ) return rc;
  if( iTable>2 ){
    rc = freePage(pBt, pPage, iTable);
  }else{
    zeroPage(pBt, pPage);
  }
  sqlitepager_unref(pPage);
  return rc;  
}

Here is the call graph for this function:

static int fileBtreeFirst ( BtCursor pCur,
int pRes 
) [static]

Definition at line 1526 of file btree.c.

                                                    {
  int rc;
  if( pCur->pPage==0 ) return SQLITE_ABORT;
  rc = moveToRoot(pCur);
  if( rc ) return rc;
  if( pCur->pPage->nCell==0 ){
    *pRes = 1;
    return SQLITE_OK;
  }
  *pRes = 0;
  rc = moveToLeftmost(pCur);
  pCur->eSkip = SKIP_NONE;
  return rc;
}

Here is the call graph for this function:

static const char* fileBtreeGetFilename ( Btree pBt) [static]

Definition at line 3488 of file btree.c.

                                                   {
  assert( pBt->pPager!=0 );
  return sqlitepager_filename(pBt->pPager);
}

Here is the call graph for this function:

static int fileBtreeGetMeta ( Btree pBt,
int aMeta 
) [static]

Definition at line 2990 of file btree.c.

                                                   {
  PageOne *pP1;
  int rc;
  int i;

  rc = sqlitepager_get(pBt->pPager, 1, (void**)&pP1);
  if( rc ) return rc;
  aMeta[0] = SWAB32(pBt, pP1->nFree);
  for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
    aMeta[i+1] = SWAB32(pBt, pP1->aMeta[i]);
  }
  sqlitepager_unref(pP1);
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fileBtreeInsert ( BtCursor pCur,
const void *  pKey,
int  nKey,
const void *  pData,
int  nData 
) [static]

Definition at line 2619 of file btree.c.

 {
  Cell newCell;
  int rc;
  int loc;
  int szNew;
  MemPage *pPage;
  Btree *pBt = pCur->pBt;

  if( pCur->pPage==0 ){
    return SQLITE_ABORT;  /* A rollback destroyed this cursor */
  }
  if( !pBt->inTrans || nKey+nData==0 ){
    /* Must start a transaction before doing an insert */
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  assert( !pBt->readOnly );
  if( !pCur->wrFlag ){
    return SQLITE_PERM;   /* Cursor not open for writing */
  }
  if( checkReadLocks(pCur) ){
    return SQLITE_LOCKED; /* The table pCur points to has a read lock */
  }
  rc = fileBtreeMoveto(pCur, pKey, nKey, &loc);
  if( rc ) return rc;
  pPage = pCur->pPage;
  assert( pPage->isInit );
  rc = sqlitepager_write(pPage);
  if( rc ) return rc;
  rc = fillInCell(pBt, &newCell, pKey, nKey, pData, nData);
  if( rc ) return rc;
  szNew = cellSize(pBt, &newCell);
  if( loc==0 ){
    newCell.h.leftChild = pPage->apCell[pCur->idx]->h.leftChild;
    rc = clearCell(pBt, pPage->apCell[pCur->idx]);
    if( rc ) return rc;
    dropCell(pBt, pPage, pCur->idx, cellSize(pBt, pPage->apCell[pCur->idx]));
  }else if( loc<0 && pPage->nCell>0 ){
    assert( pPage->u.hdr.rightChild==0 );  /* Must be a leaf page */
    pCur->idx++;
  }else{
    assert( pPage->u.hdr.rightChild==0 );  /* Must be a leaf page */
  }
  insertCell(pBt, pPage, pCur->idx, &newCell, szNew);
  rc = balance(pCur->pBt, pPage, pCur);
  /* sqliteBtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
  /* fflush(stdout); */
  pCur->eSkip = SKIP_INVALID;
  return rc;
}

Here is the call graph for this function:

char* fileBtreeIntegrityCheck ( Btree pBt,
int aRoot,
int  nRoot 
)

Definition at line 3424 of file btree.c.

                                                                {
  int i;
  int nRef;
  IntegrityCk sCheck;

  nRef = *sqlitepager_stats(pBt->pPager);
  if( lockBtree(pBt)!=SQLITE_OK ){
    return sqliteStrDup("Unable to acquire a read lock on the database");
  }
  sCheck.pBt = pBt;
  sCheck.pPager = pBt->pPager;
  sCheck.nPage = sqlitepager_pagecount(sCheck.pPager);
  if( sCheck.nPage==0 ){
    unlockBtreeIfUnused(pBt);
    return 0;
  }
  sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
  sCheck.anRef[1] = 1;
  for(i=2; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
  sCheck.zErrMsg = 0;

  /* Check the integrity of the freelist
  */
  checkList(&sCheck, 1, SWAB32(pBt, pBt->page1->freeList),
            SWAB32(pBt, pBt->page1->nFree), "Main freelist: ");

  /* Check all the tables.
  */
  for(i=0; i<nRoot; i++){
    if( aRoot[i]==0 ) continue;
    checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ", 0,0,0,0);
  }

  /* Make sure every page in the file is referenced
  */
  for(i=1; i<=sCheck.nPage; i++){
    if( sCheck.anRef[i]==0 ){
      char zBuf[100];
      sprintf(zBuf, "Page %d is never used", i);
      checkAppendMsg(&sCheck, zBuf, 0);
    }
  }

  /* Make sure this analysis did not leave any unref() pages
  */
  unlockBtreeIfUnused(pBt);
  if( nRef != *sqlitepager_stats(pBt->pPager) ){
    char zBuf[100];
    sprintf(zBuf, 
      "Outstanding page count goes from %d to %d during this analysis",
      nRef, *sqlitepager_stats(pBt->pPager)
    );
    checkAppendMsg(&sCheck, zBuf, 0);
  }

  /* Clean  up and report errors.
  */
  sqliteFree(sCheck.anRef);
  return sCheck.zErrMsg;
}

Here is the call graph for this function:

static int fileBtreeKey ( BtCursor pCur,
int  offset,
int  amt,
char *  zBuf 
) [static]

Definition at line 1242 of file btree.c.

                                                                        {
  MemPage *pPage;

  assert( amt>=0 );
  assert( offset>=0 );
  assert( pCur->pPage!=0 );
  pPage = pCur->pPage;
  if( pCur->idx >= pPage->nCell ){
    return 0;
  }
  assert( amt+offset <= NKEY(pCur->pBt, pPage->apCell[pCur->idx]->h) );
  getPayload(pCur, offset, amt, zBuf);
  return amt;
}

Here is the call graph for this function:

static int fileBtreeKeyCompare ( BtCursor pCur,
const void *  pKey,
int  nKey,
int  nIgnore,
int pResult 
) [static]

Definition at line 1325 of file btree.c.

 {
  Pgno nextPage;
  int n, c, rc, nLocal;
  Cell *pCell;
  Btree *pBt = pCur->pBt;
  const char *zKey  = (const char*)pKey;

  assert( pCur->pPage );
  assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
  pCell = pCur->pPage->apCell[pCur->idx];
  nLocal = NKEY(pBt, pCell->h) - nIgnore;
  if( nLocal<0 ) nLocal = 0;
  n = nKey<nLocal ? nKey : nLocal;
  if( n>MX_LOCAL_PAYLOAD ){
    n = MX_LOCAL_PAYLOAD;
  }
  c = memcmp(pCell->aPayload, zKey, n);
  if( c!=0 ){
    *pResult = c;
    return SQLITE_OK;
  }
  zKey += n;
  nKey -= n;
  nLocal -= n;
  nextPage = SWAB32(pBt, pCell->ovfl);
  while( nKey>0 && nLocal>0 ){
    OverflowPage *pOvfl;
    if( nextPage==0 ){
      return SQLITE_CORRUPT;
    }
    rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
    if( rc ){
      return rc;
    }
    nextPage = SWAB32(pBt, pOvfl->iNext);
    n = nKey<nLocal ? nKey : nLocal;
    if( n>OVERFLOW_SIZE ){
      n = OVERFLOW_SIZE;
    }
    c = memcmp(pOvfl->aPayload, zKey, n);
    sqlitepager_unref(pOvfl);
    if( c!=0 ){
      *pResult = c;
      return SQLITE_OK;
    }
    nKey -= n;
    nLocal -= n;
    zKey += n;
  }
  if( c==0 ){
    c = nLocal - nKey;
  }
  *pResult = c;
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeKeySize ( BtCursor pCur,
int pSize 
) [static]

Definition at line 1153 of file btree.c.

                                                       {
  Cell *pCell;
  MemPage *pPage;

  pPage = pCur->pPage;
  assert( pPage!=0 );
  if( pCur->idx >= pPage->nCell ){
    *pSize = 0;
  }else{
    pCell = pPage->apCell[pCur->idx];
    *pSize = NKEY(pCur->pBt, pCell->h);
  }
  return SQLITE_OK;
}
static int fileBtreeLast ( BtCursor pCur,
int pRes 
) [static]

Definition at line 1545 of file btree.c.

                                                   {
  int rc;
  if( pCur->pPage==0 ) return SQLITE_ABORT;
  rc = moveToRoot(pCur);
  if( rc ) return rc;
  assert( pCur->pPage->isInit );
  if( pCur->pPage->nCell==0 ){
    *pRes = 1;
    return SQLITE_OK;
  }
  *pRes = 0;
  rc = moveToRightmost(pCur);
  pCur->eSkip = SKIP_NONE;
  return rc;
}

Here is the call graph for this function:

static int fileBtreeMoveto ( BtCursor pCur,
const void *  pKey,
int  nKey,
int pRes 
) [static]

Definition at line 1585 of file btree.c.

                                                                          {
  int rc;
  if( pCur->pPage==0 ) return SQLITE_ABORT;
  pCur->eSkip = SKIP_NONE;
  rc = moveToRoot(pCur);
  if( rc ) return rc;
  for(;;){
    int lwr, upr;
    Pgno chldPg;
    MemPage *pPage = pCur->pPage;
    int c = -1;  /* pRes return if table is empty must be -1 */
    lwr = 0;
    upr = pPage->nCell-1;
    while( lwr<=upr ){
      pCur->idx = (lwr+upr)/2;
      rc = fileBtreeKeyCompare(pCur, pKey, nKey, 0, &c);
      if( rc ) return rc;
      if( c==0 ){
        pCur->iMatch = c;
        if( pRes ) *pRes = 0;
        return SQLITE_OK;
      }
      if( c<0 ){
        lwr = pCur->idx+1;
      }else{
        upr = pCur->idx-1;
      }
    }
    assert( lwr==upr+1 );
    assert( pPage->isInit );
    if( lwr>=pPage->nCell ){
      chldPg = pPage->u.hdr.rightChild;
    }else{
      chldPg = pPage->apCell[lwr]->h.leftChild;
    }
    if( chldPg==0 ){
      pCur->iMatch = c;
      if( pRes ) *pRes = c;
      return SQLITE_OK;
    }
    pCur->idx = lwr;
    rc = moveToChild(pCur, chldPg);
    if( rc ) return rc;
  }
  /* NOT REACHED */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeNext ( BtCursor pCur,
int pRes 
) [static]

Definition at line 1638 of file btree.c.

                                                   {
  int rc;
  MemPage *pPage = pCur->pPage;
  assert( pRes!=0 );
  if( pPage==0 ){
    *pRes = 1;
    return SQLITE_ABORT;
  }
  assert( pPage->isInit );
  assert( pCur->eSkip!=SKIP_INVALID );
  if( pPage->nCell==0 ){
    *pRes = 1;
    return SQLITE_OK;
  }
  assert( pCur->idx<pPage->nCell );
  if( pCur->eSkip==SKIP_NEXT ){
    pCur->eSkip = SKIP_NONE;
    *pRes = 0;
    return SQLITE_OK;
  }
  pCur->eSkip = SKIP_NONE;
  pCur->idx++;
  if( pCur->idx>=pPage->nCell ){
    if( pPage->u.hdr.rightChild ){
      rc = moveToChild(pCur, pPage->u.hdr.rightChild);
      if( rc ) return rc;
      rc = moveToLeftmost(pCur);
      *pRes = 0;
      return rc;
    }
    do{
      if( pPage->pParent==0 ){
        *pRes = 1;
        return SQLITE_OK;
      }
      moveToParent(pCur);
      pPage = pCur->pPage;
    }while( pCur->idx>=pPage->nCell );
    *pRes = 0;
    return SQLITE_OK;
  }
  *pRes = 0;
  if( pPage->u.hdr.rightChild==0 ){
    return SQLITE_OK;
  }
  rc = moveToLeftmost(pCur);
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static Pager* fileBtreePager ( Btree pBt) [static]

Definition at line 3152 of file btree.c.

                                        {
  return pBt->pPager;
}
static int fileBtreePrevious ( BtCursor pCur,
int pRes 
) [static]

Definition at line 1693 of file btree.c.

                                                       {
  int rc;
  Pgno pgno;
  MemPage *pPage;
  pPage = pCur->pPage;
  if( pPage==0 ){
    *pRes = 1;
    return SQLITE_ABORT;
  }
  assert( pPage->isInit );
  assert( pCur->eSkip!=SKIP_INVALID );
  if( pPage->nCell==0 ){
    *pRes = 1;
    return SQLITE_OK;
  }
  if( pCur->eSkip==SKIP_PREV ){
    pCur->eSkip = SKIP_NONE;
    *pRes = 0;
    return SQLITE_OK;
  }
  pCur->eSkip = SKIP_NONE;
  assert( pCur->idx>=0 );
  if( (pgno = pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
    rc = moveToChild(pCur, pgno);
    if( rc ) return rc;
    rc = moveToRightmost(pCur);
  }else{
    while( pCur->idx==0 ){
      if( pPage->pParent==0 ){
        if( pRes ) *pRes = 1;
        return SQLITE_OK;
      }
      moveToParent(pCur);
      pPage = pCur->pPage;
    }
    pCur->idx--;
    rc = SQLITE_OK;
  }
  *pRes = 0;
  return rc;
}

Here is the call graph for this function:

static int fileBtreeRollback ( Btree pBt) [static]

Definition at line 922 of file btree.c.

                                        {
  int rc;
  BtCursor *pCur;
  if( pBt->inTrans==0 ) return SQLITE_OK;
  pBt->inTrans = 0;
  pBt->inCkpt = 0;
  rc = pBt->readOnly ? SQLITE_OK : sqlitepager_rollback(pBt->pPager);
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
    if( pCur->pPage && pCur->pPage->isInit==0 ){
      sqlitepager_unref(pCur->pPage);
      pCur->pPage = 0;
    }
  }
  unlockBtreeIfUnused(pBt);
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int fileBtreeRollbackCkpt ( Btree pBt) [static]

Definition at line 983 of file btree.c.

                                            {
  int rc;
  BtCursor *pCur;
  if( pBt->inCkpt==0 || pBt->readOnly ) return SQLITE_OK;
  rc = sqlitepager_ckpt_rollback(pBt->pPager);
  for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
    if( pCur->pPage && pCur->pPage->isInit==0 ){
      sqlitepager_unref(pCur->pPage);
      pCur->pPage = 0;
    }
  }
  pBt->inCkpt = 0;
  return rc;
}

Here is the call graph for this function:

static int fileBtreeSetCacheSize ( Btree pBt,
int  mxPage 
) [static]

Definition at line 755 of file btree.c.

                                                        {
  sqlitepager_set_cachesize(pBt->pPager, mxPage);
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fileBtreeSetSafetyLevel ( Btree pBt,
int  level 
) [static]

Definition at line 768 of file btree.c.

                                                         {
  sqlitepager_set_safety_level(pBt->pPager, level);
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fileBtreeUpdateMeta ( Btree pBt,
int aMeta 
) [static]

Definition at line 3008 of file btree.c.

                                                      {
  PageOne *pP1;
  int rc, i;
  if( !pBt->inTrans ){
    return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
  }
  pP1 = pBt->page1;
  rc = sqlitepager_write(pP1);
  if( rc ) return rc;   
  for(i=0; i<sizeof(pP1->aMeta)/sizeof(pP1->aMeta[0]); i++){
    pP1->aMeta[i] = SWAB32(pBt, aMeta[i+1]);
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

static int fillInCell ( Btree pBt,
Cell pCell,
const void *  pKey,
int  nKey,
const void *  pData,
int  nData 
) [static]

Definition at line 1909 of file btree.c.

 {
  OverflowPage *pOvfl, *pPrior;
  Pgno *pNext;
  int spaceLeft;
  int n, rc;
  int nPayload;
  const char *pPayload;
  char *pSpace;
  Pgno nearby = 0;

  pCell->h.leftChild = 0;
  pCell->h.nKey = SWAB16(pBt, nKey & 0xffff);
  pCell->h.nKeyHi = nKey >> 16;
  pCell->h.nData = SWAB16(pBt, nData & 0xffff);
  pCell->h.nDataHi = nData >> 16;
  pCell->h.iNext = 0;

  pNext = &pCell->ovfl;
  pSpace = pCell->aPayload;
  spaceLeft = MX_LOCAL_PAYLOAD;
  pPayload = pKey;
  pKey = 0;
  nPayload = nKey;
  pPrior = 0;
  while( nPayload>0 ){
    if( spaceLeft==0 ){
      rc = allocatePage(pBt, (MemPage**)&pOvfl, pNext, nearby);
      if( rc ){
        *pNext = 0;
      }else{
        nearby = *pNext;
      }
      if( pPrior ) sqlitepager_unref(pPrior);
      if( rc ){
        clearCell(pBt, pCell);
        return rc;
      }
      if( pBt->needSwab ) *pNext = swab32(*pNext);
      pPrior = pOvfl;
      spaceLeft = OVERFLOW_SIZE;
      pSpace = pOvfl->aPayload;
      pNext = &pOvfl->iNext;
    }
    n = nPayload;
    if( n>spaceLeft ) n = spaceLeft;
    memcpy(pSpace, pPayload, n);
    nPayload -= n;
    if( nPayload==0 && pData ){
      pPayload = pData;
      nPayload = nData;
      pData = 0;
    }else{
      pPayload += n;
    }
    spaceLeft -= n;
    pSpace += n;
  }
  *pNext = 0;
  if( pPrior ){
    sqlitepager_unref(pPrior);
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int freePage ( Btree pBt,
void *  pPage,
Pgno  pgno 
) [static]

Definition at line 1816 of file btree.c.

                                                       {
  PageOne *pPage1 = pBt->page1;
  OverflowPage *pOvfl = (OverflowPage*)pPage;
  int rc;
  int needUnref = 0;
  MemPage *pMemPage;

  if( pgno==0 ){
    assert( pOvfl!=0 );
    pgno = sqlitepager_pagenumber(pOvfl);
  }
  assert( pgno>2 );
  assert( sqlitepager_pagenumber(pOvfl)==pgno );
  pMemPage = (MemPage*)pPage;
  pMemPage->isInit = 0;
  if( pMemPage->pParent ){
    sqlitepager_unref(pMemPage->pParent);
    pMemPage->pParent = 0;
  }
  rc = sqlitepager_write(pPage1);
  if( rc ){
    return rc;
  }
  SWAB_ADD(pBt, pPage1->nFree, 1);
  if( pPage1->nFree!=0 && pPage1->freeList!=0 ){
    OverflowPage *pFreeIdx;
    rc = sqlitepager_get(pBt->pPager, SWAB32(pBt, pPage1->freeList),
                        (void**)&pFreeIdx);
    if( rc==SQLITE_OK ){
      FreelistInfo *pInfo = (FreelistInfo*)pFreeIdx->aPayload;
      int n = SWAB32(pBt, pInfo->nFree);
      if( n<(sizeof(pInfo->aFree)/sizeof(pInfo->aFree[0])) ){
        rc = sqlitepager_write(pFreeIdx);
        if( rc==SQLITE_OK ){
          pInfo->aFree[n] = SWAB32(pBt, pgno);
          SWAB_ADD(pBt, pInfo->nFree, 1);
          sqlitepager_unref(pFreeIdx);
          sqlitepager_dont_write(pBt->pPager, pgno);
          return rc;
        }
      }
      sqlitepager_unref(pFreeIdx);
    }
  }
  if( pOvfl==0 ){
    assert( pgno>0 );
    rc = sqlitepager_get(pBt->pPager, pgno, (void**)&pOvfl);
    if( rc ) return rc;
    needUnref = 1;
  }
  rc = sqlitepager_write(pOvfl);
  if( rc ){
    if( needUnref ) sqlitepager_unref(pOvfl);
    return rc;
  }
  pOvfl->iNext = pPage1->freeList;
  pPage1->freeList = SWAB32(pBt, pgno);
  memset(pOvfl->aPayload, 0, OVERFLOW_SIZE);
  if( needUnref ) rc = sqlitepager_unref(pOvfl);
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void freeSpace ( Btree pBt,
MemPage pPage,
int  start,
int  size 
) [static]

Definition at line 519 of file btree.c.

                                                                      {
  int end = start + size;
  u16 *pIdx, idx;
  FreeBlk *pFBlk;
  FreeBlk *pNew;
  FreeBlk *pNext;
  int iSize;

  assert( sqlitepager_iswriteable(pPage) );
  assert( size == ROUNDUP(size) );
  assert( start == ROUNDUP(start) );
  assert( pPage->isInit );
  pIdx = &pPage->u.hdr.firstFree;
  idx = SWAB16(pBt, *pIdx);
  while( idx!=0 && idx<start ){
    pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
    iSize = SWAB16(pBt, pFBlk->iSize);
    if( idx + iSize == start ){
      pFBlk->iSize = SWAB16(pBt, iSize + size);
      if( idx + iSize + size == SWAB16(pBt, pFBlk->iNext) ){
        pNext = (FreeBlk*)&pPage->u.aDisk[idx + iSize + size];
        if( pBt->needSwab ){
          pFBlk->iSize = swab16((u16)swab16(pNext->iSize)+iSize+size);
        }else{
          pFBlk->iSize += pNext->iSize;
        }
        pFBlk->iNext = pNext->iNext;
      }
      pPage->nFree += size;
      return;
    }
    pIdx = &pFBlk->iNext;
    idx = SWAB16(pBt, *pIdx);
  }
  pNew = (FreeBlk*)&pPage->u.aDisk[start];
  if( idx != end ){
    pNew->iSize = SWAB16(pBt, size);
    pNew->iNext = SWAB16(pBt, idx);
  }else{
    pNext = (FreeBlk*)&pPage->u.aDisk[idx];
    pNew->iSize = SWAB16(pBt, size + SWAB16(pBt, pNext->iSize));
    pNew->iNext = pNext->iNext;
  }
  *pIdx = SWAB16(pBt, start);
  pPage->nFree += size;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int getPayload ( BtCursor pCur,
int  offset,
int  amt,
char *  zBuf 
) [static]

Definition at line 1176 of file btree.c.

                                                                      {
  char *aPayload;
  Pgno nextPage;
  int rc;
  Btree *pBt = pCur->pBt;
  assert( pCur!=0 && pCur->pPage!=0 );
  assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
  aPayload = pCur->pPage->apCell[pCur->idx]->aPayload;
  if( offset<MX_LOCAL_PAYLOAD ){
    int a = amt;
    if( a+offset>MX_LOCAL_PAYLOAD ){
      a = MX_LOCAL_PAYLOAD - offset;
    }
    memcpy(zBuf, &aPayload[offset], a);
    if( a==amt ){
      return SQLITE_OK;
    }
    offset = 0;
    zBuf += a;
    amt -= a;
  }else{
    offset -= MX_LOCAL_PAYLOAD;
  }
  if( amt>0 ){
    nextPage = SWAB32(pBt, pCur->pPage->apCell[pCur->idx]->ovfl);
  }
  while( amt>0 && nextPage ){
    OverflowPage *pOvfl;
    rc = sqlitepager_get(pBt->pPager, nextPage, (void**)&pOvfl);
    if( rc!=0 ){
      return rc;
    }
    nextPage = SWAB32(pBt, pOvfl->iNext);
    if( offset<OVERFLOW_SIZE ){
      int a = amt;
      if( a + offset > OVERFLOW_SIZE ){
        a = OVERFLOW_SIZE - offset;
      }
      memcpy(zBuf, &pOvfl->aPayload[offset], a);
      offset = 0;
      amt -= a;
      zBuf += a;
    }else{
      offset -= OVERFLOW_SIZE;
    }
    sqlitepager_unref(pOvfl);
  }
  if( amt>0 ){
    return SQLITE_CORRUPT;
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void getTempCursor ( BtCursor pCur,
BtCursor pTempCur 
) [static]

Definition at line 1127 of file btree.c.

                                                             {
  memcpy(pTempCur, pCur, sizeof(*pCur));
  pTempCur->pNext = 0;
  pTempCur->pPrev = 0;
  if( pTempCur->pPage ){
    sqlitepager_ref(pTempCur->pPage);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int initPage ( Bt pBt,
MemPage pPage,
Pgno  pgnoThis,
MemPage pParent 
) [static]

Definition at line 580 of file btree.c.

                                                                             {
  int idx;           /* An index into pPage->u.aDisk[] */
  Cell *pCell;       /* A pointer to a Cell in pPage->u.aDisk[] */
  FreeBlk *pFBlk;    /* A pointer to a free block in pPage->u.aDisk[] */
  int sz;            /* The size of a Cell in bytes */
  int freeSpace;     /* Amount of free space on the page */

  if( pPage->pParent ){
    assert( pPage->pParent==pParent );
    return SQLITE_OK;
  }
  if( pParent ){
    pPage->pParent = pParent;
    sqlitepager_ref(pParent);
  }
  if( pPage->isInit ) return SQLITE_OK;
  pPage->isInit = 1;
  pPage->nCell = 0;
  freeSpace = USABLE_SPACE;
  idx = SWAB16(pBt, pPage->u.hdr.firstCell);
  while( idx!=0 ){
    if( idx>SQLITE_USABLE_SIZE-MIN_CELL_SIZE ) goto page_format_error;
    if( idx<sizeof(PageHdr) ) goto page_format_error;
    if( idx!=ROUNDUP(idx) ) goto page_format_error;
    pCell = (Cell*)&pPage->u.aDisk[idx];
    sz = cellSize(pBt, pCell);
    if( idx+sz > SQLITE_USABLE_SIZE ) goto page_format_error;
    freeSpace -= sz;
    pPage->apCell[pPage->nCell++] = pCell;
    idx = SWAB16(pBt, pCell->h.iNext);
  }
  pPage->nFree = 0;
  idx = SWAB16(pBt, pPage->u.hdr.firstFree);
  while( idx!=0 ){
    int iNext;
    if( idx>SQLITE_USABLE_SIZE-sizeof(FreeBlk) ) goto page_format_error;
    if( idx<sizeof(PageHdr) ) goto page_format_error;
    pFBlk = (FreeBlk*)&pPage->u.aDisk[idx];
    pPage->nFree += SWAB16(pBt, pFBlk->iSize);
    iNext = SWAB16(pBt, pFBlk->iNext);
    if( iNext>0 && iNext <= idx ) goto page_format_error;
    idx = iNext;
  }
  if( pPage->nCell==0 && pPage->nFree==0 ){
    /* As a special case, an uninitialized root page appears to be
    ** an empty database */
    return SQLITE_OK;
  }
  if( pPage->nFree!=freeSpace ) goto page_format_error;
  return SQLITE_OK;

page_format_error:
  return SQLITE_CORRUPT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void insertCell ( Btree pBt,
MemPage pPage,
int  i,
Cell pCell,
int  sz 
) [static]

Definition at line 2057 of file btree.c.

                                                                              {
  int idx, j;
  assert( i>=0 && i<=pPage->nCell );
  assert( sz==cellSize(pBt, pCell) );
  assert( sqlitepager_iswriteable(pPage) );
  idx = allocateSpace(pBt, pPage, sz);
  for(j=pPage->nCell; j>i; j--){
    pPage->apCell[j] = pPage->apCell[j-1];
  }
  pPage->nCell++;
  if( idx<=0 ){
    pPage->isOverfull = 1;
    pPage->apCell[i] = pCell;
  }else{
    memcpy(&pPage->u.aDisk[idx], pCell, sz);
    pPage->apCell[i] = (Cell*)&pPage->u.aDisk[idx];
  }
  pPage->idxShift = 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int keyCompare ( const char *  zKey1,
int  nKey1,
const char *  zKey2,
int  nKey2 
) [static]

Definition at line 3252 of file btree.c.

 {
  int min = nKey1>nKey2 ? nKey2 : nKey1;
  int c = memcmp(zKey1, zKey2, min);
  if( c==0 ){
    c = nKey1 - nKey2;
  }
  return c;
}

Here is the caller graph for this function:

static int lockBtree ( Btree pBt) [static]

Definition at line 783 of file btree.c.

                                {
  int rc;
  if( pBt->page1 ) return SQLITE_OK;
  rc = sqlitepager_get(pBt->pPager, 1, (void**)&pBt->page1);
  if( rc!=SQLITE_OK ) return rc;

  /* Do some checking to help insure the file we opened really is
  ** a valid database file. 
  */
  if( sqlitepager_pagecount(pBt->pPager)>0 ){
    PageOne *pP1 = pBt->page1;
    if( strcmp(pP1->zMagic,zMagicHeader)!=0 ||
          (pP1->iMagic!=MAGIC && swab32(pP1->iMagic)!=MAGIC) ){
      rc = SQLITE_NOTADB;
      goto page1_init_failed;
    }
    pBt->needSwab = pP1->iMagic!=MAGIC;
  }
  return rc;

page1_init_failed:
  sqlitepager_unref(pBt->page1);
  pBt->page1 = 0;
  return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int moveToChild ( BtCursor pCur,
int  newPgno 
) [static]

Definition at line 1391 of file btree.c.

                                                   {
  int rc;
  MemPage *pNewPage;
  Btree *pBt = pCur->pBt;

  newPgno = SWAB32(pBt, newPgno);
  rc = sqlitepager_get(pBt->pPager, newPgno, (void**)&pNewPage);
  if( rc ) return rc;
  rc = initPage(pBt, pNewPage, newPgno, pCur->pPage);
  if( rc ) return rc;
  assert( pCur->idx>=pCur->pPage->nCell
          || pCur->pPage->apCell[pCur->idx]->h.leftChild==SWAB32(pBt,newPgno) );
  assert( pCur->idx<pCur->pPage->nCell
          || pCur->pPage->u.hdr.rightChild==SWAB32(pBt,newPgno) );
  pNewPage->idxParent = pCur->idx;
  pCur->pPage->idxShift = 0;
  sqlitepager_unref(pCur->pPage);
  pCur->pPage = pNewPage;
  pCur->idx = 0;
  if( pNewPage->nCell<1 ){
    return SQLITE_CORRUPT;
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int moveToLeftmost ( BtCursor pCur) [static]

Definition at line 1491 of file btree.c.

                                         {
  Pgno pgno;
  int rc;

  while( (pgno = pCur->pPage->apCell[pCur->idx]->h.leftChild)!=0 ){
    rc = moveToChild(pCur, pgno);
    if( rc ) return rc;
  }
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void moveToParent ( BtCursor pCur) [static]

Definition at line 1424 of file btree.c.

                                        {
  Pgno oldPgno;
  MemPage *pParent;
  MemPage *pPage;
  int idxParent;
  pPage = pCur->pPage;
  assert( pPage!=0 );
  pParent = pPage->pParent;
  assert( pParent!=0 );
  idxParent = pPage->idxParent;
  sqlitepager_ref(pParent);
  sqlitepager_unref(pPage);
  pCur->pPage = pParent;
  assert( pParent->idxShift==0 );
  if( pParent->idxShift==0 ){
    pCur->idx = idxParent;
#ifndef NDEBUG  
    /* Verify that pCur->idx is the correct index to point back to the child
    ** page we just came from 
    */
    oldPgno = SWAB32(pCur->pBt, sqlitepager_pagenumber(pPage));
    if( pCur->idx<pParent->nCell ){
      assert( pParent->apCell[idxParent]->h.leftChild==oldPgno );
    }else{
      assert( pParent->u.hdr.rightChild==oldPgno );
    }
#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;
    pCur->idx = pParent->nCell;
    oldPgno = SWAB32(pCur->pBt, sqlitepager_pagenumber(pPage));
    for(i=0; i<pParent->nCell; i++){
      if( pParent->apCell[i]->h.leftChild==oldPgno ){
        pCur->idx = i;
        break;
      }
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int moveToRightmost ( BtCursor pCur) [static]

Definition at line 1509 of file btree.c.

                                          {
  Pgno pgno;
  int rc;

  while( (pgno = pCur->pPage->u.hdr.rightChild)!=0 ){
    pCur->idx = pCur->pPage->nCell;
    rc = moveToChild(pCur, pgno);
    if( rc ) return rc;
  }
  pCur->idx = pCur->pPage->nCell - 1;
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int moveToRoot ( BtCursor pCur) [static]

Definition at line 1472 of file btree.c.

                                     {
  MemPage *pNew;
  int rc;
  Btree *pBt = pCur->pBt;

  rc = sqlitepager_get(pBt->pPager, pCur->pgnoRoot, (void**)&pNew);
  if( rc ) return rc;
  rc = initPage(pBt, pNew, pCur->pgnoRoot, 0);
  if( rc ) return rc;
  sqlitepager_unref(pCur->pPage);
  pCur->pPage = pNew;
  pCur->idx = 0;
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int newDatabase ( Btree pBt) [static]

Definition at line 832 of file btree.c.

                                  {
  MemPage *pRoot;
  PageOne *pP1;
  int rc;
  if( sqlitepager_pagecount(pBt->pPager)>1 ) return SQLITE_OK;
  pP1 = pBt->page1;
  rc = sqlitepager_write(pBt->page1);
  if( rc ) return rc;
  rc = sqlitepager_get(pBt->pPager, 2, (void**)&pRoot);
  if( rc ) return rc;
  rc = sqlitepager_write(pRoot);
  if( rc ){
    sqlitepager_unref(pRoot);
    return rc;
  }
  strcpy(pP1->zMagic, zMagicHeader);
  if( btree_native_byte_order ){
    pP1->iMagic = MAGIC;
    pBt->needSwab = 0;
  }else{
    pP1->iMagic = swab32(MAGIC);
    pBt->needSwab = 1;
  }
  zeroPage(pBt, pRoot);
  sqlitepager_unref(pRoot);
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void pageDestructor ( void *  pData) [static]

Definition at line 660 of file btree.c.

                                       {
  MemPage *pPage = (MemPage*)pData;
  if( pPage->pParent ){
    MemPage *pParent = pPage->pParent;
    pPage->pParent = 0;
    sqlitepager_unref(pParent);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void releaseTempCursor ( BtCursor pCur) [static]

Definition at line 1140 of file btree.c.

                                             {
  if( pCur->pPage ){
    sqlitepager_unref(pCur->pPage);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void relinkCellList ( Btree pBt,
MemPage pPage 
) [static]

Definition at line 2083 of file btree.c.

                                                      {
  int i;
  u16 *pIdx;
  assert( sqlitepager_iswriteable(pPage) );
  pIdx = &pPage->u.hdr.firstCell;
  for(i=0; i<pPage->nCell; i++){
    int idx = Addr(pPage->apCell[i]) - Addr(pPage);
    assert( idx>0 && idx<SQLITE_USABLE_SIZE );
    *pIdx = SWAB16(pBt, idx);
    pIdx = &pPage->apCell[i]->h.iNext;
  }
  *pIdx = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void reparentChildPages ( Btree pBt,
MemPage pPage 
) [static]

Definition at line 2008 of file btree.c.

                                                          {
  int i;
  Pager *pPager = pBt->pPager;
  for(i=0; i<pPage->nCell; i++){
    reparentPage(pPager, SWAB32(pBt, pPage->apCell[i]->h.leftChild), pPage, i);
  }
  reparentPage(pPager, SWAB32(pBt, pPage->u.hdr.rightChild), pPage, i);
  pPage->idxShift = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void reparentPage ( Pager pPager,
Pgno  pgno,
MemPage pNewParent,
int  idx 
) [static]

Definition at line 1983 of file btree.c.

                                                                               {
  MemPage *pThis;

  if( pgno==0 ) return;
  assert( pPager!=0 );
  pThis = sqlitepager_lookup(pPager, pgno);
  if( pThis && pThis->isInit ){
    if( pThis->pParent!=pNewParent ){
      if( pThis->pParent ) sqlitepager_unref(pThis->pParent);
      pThis->pParent = pNewParent;
      if( pNewParent ) sqlitepager_ref(pNewParent);
    }
    pThis->idxParent = idx;
    sqlitepager_unref(pThis);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqliteBtreeOpen ( const char *  zFilename,
int  omitJournal,
int  nCache,
Btree **  ppBtree 
)

Definition at line 680 of file btree.c.

 {
  Btree *pBt;
  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.
  */
  assert( sizeof(u32)==4 );
  assert( sizeof(u16)==2 );
  assert( sizeof(Pgno)==4 );
  assert( sizeof(PageHdr)==8 );
  assert( sizeof(CellHdr)==12 );
  assert( sizeof(FreeBlk)==4 );
  assert( sizeof(OverflowPage)==SQLITE_USABLE_SIZE );
  assert( sizeof(FreelistInfo)==OVERFLOW_SIZE );
  assert( sizeof(ptr)==sizeof(char*) );
  assert( sizeof(uptr)==sizeof(ptr) );

  pBt = sqliteMalloc( sizeof(*pBt) );
  if( pBt==0 ){
    *ppBtree = 0;
    return SQLITE_NOMEM;
  }
  if( nCache<10 ) nCache = 10;
  rc = sqlitepager_open(&pBt->pPager, zFilename, nCache, EXTRA_SIZE,
                        !omitJournal);
  if( rc!=SQLITE_OK ){
    if( pBt->pPager ) sqlitepager_close(pBt->pPager);
    sqliteFree(pBt);
    *ppBtree = 0;
    return rc;
  }
  sqlitepager_set_destructor(pBt->pPager, pageDestructor);
  pBt->pCursor = 0;
  pBt->page1 = 0;
  pBt->readOnly = sqlitepager_isreadonly(pBt->pPager);
  pBt->pOps = &sqliteBtreeOps;
  *ppBtree = pBt;
  return SQLITE_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

u16 swab16 ( u16  x)

Definition at line 390 of file btree.c.

                 {
  return ((x & 0xff)<<8) | ((x>>8)&0xff);
}

Here is the caller graph for this function:

u32 swab32 ( u32  x)

Definition at line 393 of file btree.c.

                 {
  return ((x & 0xff)<<24) | ((x & 0xff00)<<8) |
         ((x>>8) & 0xff00) | ((x>>24)&0xff);
}

Here is the caller graph for this function:

static void unlockBtreeIfUnused ( Btree pBt) [static]

Definition at line 819 of file btree.c.

                                           {
  if( pBt->inTrans==0 && pBt->pCursor==0 && pBt->page1!=0 ){
    sqlitepager_unref(pBt->page1);
    pBt->page1 = 0;
    pBt->inTrans = 0;
    pBt->inCkpt = 0;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void zeroPage ( Btree pBt,
MemPage pPage 
) [static]

Definition at line 639 of file btree.c.

                                                {
  PageHdr *pHdr;
  FreeBlk *pFBlk;
  assert( sqlitepager_iswriteable(pPage) );
  memset(pPage, 0, SQLITE_USABLE_SIZE);
  pHdr = &pPage->u.hdr;
  pHdr->firstCell = 0;
  pHdr->firstFree = SWAB16(pBt, sizeof(*pHdr));
  pFBlk = (FreeBlk*)&pHdr[1];
  pFBlk->iNext = 0;
  pPage->nFree = SQLITE_USABLE_SIZE - sizeof(*pHdr);
  pFBlk->iSize = SWAB16(pBt, pPage->nFree);
  pPage->nCell = 0;
  pPage->isOverfull = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

static BtOps sqliteBtreeOps [static]
const char zMagicHeader[] = "** This file contains an SQLite 2.1 database **" [static]

Definition at line 112 of file btree.c.