xref: /sqlite-3.40.0/src/btree.h (revision f2fcd075)
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 typedef struct BtreeMutexArray BtreeMutexArray;
43 
44 /*
45 ** This structure records all of the Btrees that need to hold
46 ** a mutex before we enter sqlite3VdbeExec().  The Btrees are
47 ** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
48 ** we can always lock and unlock them all quickly.
49 */
50 struct BtreeMutexArray {
51   int nMutex;
52   Btree *aBtree[SQLITE_MAX_ATTACHED+1];
53 };
54 
55 
56 int sqlite3BtreeOpen(
57   const char *zFilename,   /* Name of database file to open */
58   sqlite3 *db,             /* Associated database connection */
59   Btree **ppBtree,         /* Return open Btree* here */
60   int flags,               /* Flags */
61   int vfsFlags             /* Flags passed through to VFS open */
62 );
63 
64 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
65 ** following values.
66 **
67 ** NOTE:  These values must match the corresponding PAGER_ values in
68 ** pager.h.
69 */
70 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
71 #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
72 #define BTREE_MEMORY        4  /* This is an in-memory DB */
73 #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
74 #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
75 
76 int sqlite3BtreeClose(Btree*);
77 int sqlite3BtreeSetCacheSize(Btree*,int);
78 int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
79 int sqlite3BtreeSyncDisabled(Btree*);
80 int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
81 int sqlite3BtreeGetPageSize(Btree*);
82 int sqlite3BtreeMaxPageCount(Btree*,int);
83 u32 sqlite3BtreeLastPage(Btree*);
84 int sqlite3BtreeSecureDelete(Btree*,int);
85 int sqlite3BtreeGetReserve(Btree*);
86 int sqlite3BtreeSetAutoVacuum(Btree *, int);
87 int sqlite3BtreeGetAutoVacuum(Btree *);
88 int sqlite3BtreeBeginTrans(Btree*,int);
89 int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
90 int sqlite3BtreeCommitPhaseTwo(Btree*);
91 int sqlite3BtreeCommit(Btree*);
92 int sqlite3BtreeRollback(Btree*);
93 int sqlite3BtreeBeginStmt(Btree*,int);
94 int sqlite3BtreeCreateTable(Btree*, int*, int flags);
95 int sqlite3BtreeIsInTrans(Btree*);
96 int sqlite3BtreeIsInReadTrans(Btree*);
97 int sqlite3BtreeIsInBackup(Btree*);
98 void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
99 int sqlite3BtreeSchemaLocked(Btree *pBtree);
100 int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
101 int sqlite3BtreeSavepoint(Btree *, int, int);
102 
103 const char *sqlite3BtreeGetFilename(Btree *);
104 const char *sqlite3BtreeGetJournalname(Btree *);
105 int sqlite3BtreeCopyFile(Btree *, Btree *);
106 
107 int sqlite3BtreeIncrVacuum(Btree *);
108 
109 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
110 ** of the flags shown below.
111 **
112 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
113 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
114 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
115 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
116 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
117 ** indices.)
118 */
119 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
120 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
121 
122 int sqlite3BtreeDropTable(Btree*, int, int*);
123 int sqlite3BtreeClearTable(Btree*, int, int*);
124 void sqlite3BtreeTripAllCursors(Btree*, int);
125 
126 void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
127 int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
128 
129 /*
130 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
131 ** should be one of the following values. The integer values are assigned
132 ** to constants so that the offset of the corresponding field in an
133 ** SQLite database header may be found using the following formula:
134 **
135 **   offset = 36 + (idx * 4)
136 **
137 ** For example, the free-page-count field is located at byte offset 36 of
138 ** the database file header. The incr-vacuum-flag field is located at
139 ** byte offset 64 (== 36+4*7).
140 */
141 #define BTREE_FREE_PAGE_COUNT     0
142 #define BTREE_SCHEMA_VERSION      1
143 #define BTREE_FILE_FORMAT         2
144 #define BTREE_DEFAULT_CACHE_SIZE  3
145 #define BTREE_LARGEST_ROOT_PAGE   4
146 #define BTREE_TEXT_ENCODING       5
147 #define BTREE_USER_VERSION        6
148 #define BTREE_INCR_VACUUM         7
149 
150 int sqlite3BtreeCursor(
151   Btree*,                              /* BTree containing table to open */
152   int iTable,                          /* Index of root page */
153   int wrFlag,                          /* 1 for writing.  0 for read-only */
154   struct KeyInfo*,                     /* First argument to compare function */
155   BtCursor *pCursor                    /* Space to write cursor structure */
156 );
157 int sqlite3BtreeCursorSize(void);
158 void sqlite3BtreeCursorZero(BtCursor*);
159 
160 int sqlite3BtreeCloseCursor(BtCursor*);
161 int sqlite3BtreeMovetoUnpacked(
162   BtCursor*,
163   UnpackedRecord *pUnKey,
164   i64 intKey,
165   int bias,
166   int *pRes
167 );
168 int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
169 int sqlite3BtreeDelete(BtCursor*);
170 int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
171                                   const void *pData, int nData,
172                                   int nZero, int bias, int seekResult);
173 int sqlite3BtreeFirst(BtCursor*, int *pRes);
174 int sqlite3BtreeLast(BtCursor*, int *pRes);
175 int sqlite3BtreeNext(BtCursor*, int *pRes);
176 int sqlite3BtreeEof(BtCursor*);
177 int sqlite3BtreePrevious(BtCursor*, int *pRes);
178 int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
179 int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
180 const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
181 const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
182 int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
183 int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
184 void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
185 sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
186 
187 char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
188 struct Pager *sqlite3BtreePager(Btree*);
189 
190 int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
191 void sqlite3BtreeCacheOverflow(BtCursor *);
192 void sqlite3BtreeClearCursor(BtCursor *);
193 
194 int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
195 
196 #ifndef NDEBUG
197 int sqlite3BtreeCursorIsValid(BtCursor*);
198 #endif
199 
200 #ifndef SQLITE_OMIT_BTREECOUNT
201 int sqlite3BtreeCount(BtCursor *, i64 *);
202 #endif
203 
204 #ifdef SQLITE_TEST
205 int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
206 void sqlite3BtreeCursorList(Btree*);
207 #endif
208 
209 #ifndef SQLITE_OMIT_WAL
210   int sqlite3BtreeCheckpoint(Btree*);
211 #endif
212 
213 /*
214 ** If we are not using shared cache, then there is no need to
215 ** use mutexes to access the BtShared structures.  So make the
216 ** Enter and Leave procedures no-ops.
217 */
218 #ifndef SQLITE_OMIT_SHARED_CACHE
219   void sqlite3BtreeEnter(Btree*);
220   void sqlite3BtreeEnterAll(sqlite3*);
221 #else
222 # define sqlite3BtreeEnter(X)
223 # define sqlite3BtreeEnterAll(X)
224 #endif
225 
226 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
227   void sqlite3BtreeLeave(Btree*);
228   void sqlite3BtreeEnterCursor(BtCursor*);
229   void sqlite3BtreeLeaveCursor(BtCursor*);
230   void sqlite3BtreeLeaveAll(sqlite3*);
231   void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
232   void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
233   void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
234 #ifndef NDEBUG
235   /* These routines are used inside assert() statements only. */
236   int sqlite3BtreeHoldsMutex(Btree*);
237   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
238 #endif
239 #else
240 
241 # define sqlite3BtreeLeave(X)
242 # define sqlite3BtreeEnterCursor(X)
243 # define sqlite3BtreeLeaveCursor(X)
244 # define sqlite3BtreeLeaveAll(X)
245 # define sqlite3BtreeMutexArrayEnter(X)
246 # define sqlite3BtreeMutexArrayLeave(X)
247 # define sqlite3BtreeMutexArrayInsert(X,Y)
248 
249 # define sqlite3BtreeHoldsMutex(X) 1
250 # define sqlite3BtreeHoldsAllMutexes(X) 1
251 #endif
252 
253 
254 #endif /* _BTREE_H_ */
255