Home | History | Annotate | Download | only in src
      1 /*
      2 ** 2001 September 15
      3 **
      4 ** The author disclaims copyright to this source code.  In place of
      5 ** a legal notice, here is a blessing:
      6 **
      7 **    May you do good and not evil.
      8 **    May you find forgiveness for yourself and forgive others.
      9 **    May you share freely, never taking more than you give.
     10 **
     11 *************************************************************************
     12 ** This header file defines the interface that the sqlite B-Tree file
     13 ** subsystem.  See comments in the source code for a detailed description
     14 ** of what each interface routine does.
     15 */
     16 #ifndef _BTREE_H_
     17 #define _BTREE_H_
     18 
     19 /* TODO: This definition is just included so other modules compile. It
     20 ** needs to be revisited.
     21 */
     22 #define SQLITE_N_BTREE_META 10
     23 
     24 /*
     25 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
     26 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
     27 */
     28 #ifndef SQLITE_DEFAULT_AUTOVACUUM
     29   #define SQLITE_DEFAULT_AUTOVACUUM 0
     30 #endif
     31 
     32 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
     33 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
     34 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
     35 
     36 /*
     37 ** Forward declarations of structure
     38 */
     39 typedef struct Btree Btree;
     40 typedef struct BtCursor BtCursor;
     41 typedef struct BtShared BtShared;
     42 
     43 
     44 int sqlite3BtreeOpen(
     45   const char *zFilename,   /* Name of database file to open */
     46   sqlite3 *db,             /* Associated database connection */
     47   Btree **ppBtree,         /* Return open Btree* here */
     48   int flags,               /* Flags */
     49   int vfsFlags             /* Flags passed through to VFS open */
     50 );
     51 
     52 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
     53 ** following values.
     54 **
     55 ** NOTE:  These values must match the corresponding PAGER_ values in
     56 ** pager.h.
     57 */
     58 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
     59 #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
     60 #define BTREE_MEMORY        4  /* This is an in-memory DB */
     61 #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
     62 #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
     63 
     64 int sqlite3BtreeClose(Btree*);
     65 int sqlite3BtreeSetCacheSize(Btree*,int);
     66 int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
     67 int sqlite3BtreeSyncDisabled(Btree*);
     68 int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
     69 int sqlite3BtreeGetPageSize(Btree*);
     70 int sqlite3BtreeMaxPageCount(Btree*,int);
     71 u32 sqlite3BtreeLastPage(Btree*);
     72 int sqlite3BtreeSecureDelete(Btree*,int);
     73 int sqlite3BtreeGetReserve(Btree*);
     74 int sqlite3BtreeSetAutoVacuum(Btree *, int);
     75 int sqlite3BtreeGetAutoVacuum(Btree *);
     76 int sqlite3BtreeBeginTrans(Btree*,int);
     77 int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
     78 int sqlite3BtreeCommitPhaseTwo(Btree*, int);
     79 int sqlite3BtreeCommit(Btree*);
     80 int sqlite3BtreeRollback(Btree*);
     81 int sqlite3BtreeBeginStmt(Btree*,int);
     82 int sqlite3BtreeCreateTable(Btree*, int*, int flags);
     83 int sqlite3BtreeIsInTrans(Btree*);
     84 int sqlite3BtreeIsInReadTrans(Btree*);
     85 int sqlite3BtreeIsInBackup(Btree*);
     86 void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
     87 int sqlite3BtreeSchemaLocked(Btree *pBtree);
     88 int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
     89 int sqlite3BtreeSavepoint(Btree *, int, int);
     90 
     91 const char *sqlite3BtreeGetFilename(Btree *);
     92 const char *sqlite3BtreeGetJournalname(Btree *);
     93 int sqlite3BtreeCopyFile(Btree *, Btree *);
     94 
     95 int sqlite3BtreeIncrVacuum(Btree *);
     96 
     97 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
     98 ** of the flags shown below.
     99 **
    100 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
    101 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
    102 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
    103 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
    104 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
    105 ** indices.)
    106 */
    107 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
    108 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
    109 
    110 int sqlite3BtreeDropTable(Btree*, int, int*);
    111 int sqlite3BtreeClearTable(Btree*, int, int*);
    112 void sqlite3BtreeTripAllCursors(Btree*, int);
    113 
    114 void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
    115 int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
    116 
    117 /*
    118 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
    119 ** should be one of the following values. The integer values are assigned
    120 ** to constants so that the offset of the corresponding field in an
    121 ** SQLite database header may be found using the following formula:
    122 **
    123 **   offset = 36 + (idx * 4)
    124 **
    125 ** For example, the free-page-count field is located at byte offset 36 of
    126 ** the database file header. The incr-vacuum-flag field is located at
    127 ** byte offset 64 (== 36+4*7).
    128 */
    129 #define BTREE_FREE_PAGE_COUNT     0
    130 #define BTREE_SCHEMA_VERSION      1
    131 #define BTREE_FILE_FORMAT         2
    132 #define BTREE_DEFAULT_CACHE_SIZE  3
    133 #define BTREE_LARGEST_ROOT_PAGE   4
    134 #define BTREE_TEXT_ENCODING       5
    135 #define BTREE_USER_VERSION        6
    136 #define BTREE_INCR_VACUUM         7
    137 
    138 int sqlite3BtreeCursor(
    139   Btree*,                              /* BTree containing table to open */
    140   int iTable,                          /* Index of root page */
    141   int wrFlag,                          /* 1 for writing.  0 for read-only */
    142   struct KeyInfo*,                     /* First argument to compare function */
    143   BtCursor *pCursor                    /* Space to write cursor structure */
    144 );
    145 int sqlite3BtreeCursorSize(void);
    146 void sqlite3BtreeCursorZero(BtCursor*);
    147 
    148 int sqlite3BtreeCloseCursor(BtCursor*);
    149 int sqlite3BtreeMovetoUnpacked(
    150   BtCursor*,
    151   UnpackedRecord *pUnKey,
    152   i64 intKey,
    153   int bias,
    154   int *pRes
    155 );
    156 int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
    157 int sqlite3BtreeDelete(BtCursor*);
    158 int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
    159                                   const void *pData, int nData,
    160                                   int nZero, int bias, int seekResult);
    161 int sqlite3BtreeFirst(BtCursor*, int *pRes);
    162 int sqlite3BtreeLast(BtCursor*, int *pRes);
    163 int sqlite3BtreeNext(BtCursor*, int *pRes);
    164 int sqlite3BtreeEof(BtCursor*);
    165 int sqlite3BtreePrevious(BtCursor*, int *pRes);
    166 int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
    167 int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
    168 const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
    169 const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
    170 int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
    171 int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
    172 void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
    173 sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
    174 
    175 char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
    176 struct Pager *sqlite3BtreePager(Btree*);
    177 
    178 int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
    179 void sqlite3BtreeCacheOverflow(BtCursor *);
    180 void sqlite3BtreeClearCursor(BtCursor *);
    181 
    182 int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
    183 
    184 #ifndef NDEBUG
    185 int sqlite3BtreeCursorIsValid(BtCursor*);
    186 #endif
    187 
    188 #ifndef SQLITE_OMIT_BTREECOUNT
    189 int sqlite3BtreeCount(BtCursor *, i64 *);
    190 #endif
    191 
    192 #ifdef SQLITE_TEST
    193 int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
    194 void sqlite3BtreeCursorList(Btree*);
    195 #endif
    196 
    197 #ifndef SQLITE_OMIT_WAL
    198   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
    199 #endif
    200 
    201 /*
    202 ** If we are not using shared cache, then there is no need to
    203 ** use mutexes to access the BtShared structures.  So make the
    204 ** Enter and Leave procedures no-ops.
    205 */
    206 #ifndef SQLITE_OMIT_SHARED_CACHE
    207   void sqlite3BtreeEnter(Btree*);
    208   void sqlite3BtreeEnterAll(sqlite3*);
    209 #else
    210 # define sqlite3BtreeEnter(X)
    211 # define sqlite3BtreeEnterAll(X)
    212 #endif
    213 
    214 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
    215   int sqlite3BtreeSharable(Btree*);
    216   void sqlite3BtreeLeave(Btree*);
    217   void sqlite3BtreeEnterCursor(BtCursor*);
    218   void sqlite3BtreeLeaveCursor(BtCursor*);
    219   void sqlite3BtreeLeaveAll(sqlite3*);
    220 #ifndef NDEBUG
    221   /* These routines are used inside assert() statements only. */
    222   int sqlite3BtreeHoldsMutex(Btree*);
    223   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
    224   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
    225 #endif
    226 #else
    227 
    228 # define sqlite3BtreeSharable(X) 0
    229 # define sqlite3BtreeLeave(X)
    230 # define sqlite3BtreeEnterCursor(X)
    231 # define sqlite3BtreeLeaveCursor(X)
    232 # define sqlite3BtreeLeaveAll(X)
    233 
    234 # define sqlite3BtreeHoldsMutex(X) 1
    235 # define sqlite3BtreeHoldsAllMutexes(X) 1
    236 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
    237 #endif
    238 
    239 
    240 #endif /* _BTREE_H_ */
    241