blob: 3edc2b3b57739dca09b7c0ee9ed9e1847ba2d2ad [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drha059ad02001-04-17 20:09:11 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drha059ad02001-04-17 20:09:11 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drha059ad02001-04-17 20:09:11 +000010**
11*************************************************************************
12** This header file defines the interface that the sqlite B-Tree file
drh6446c4d2001-12-15 14:22:18 +000013** subsystem. See comments in the source code for a detailed description
14** of what each interface routine does.
drha059ad02001-04-17 20:09:11 +000015*/
drhbe0072d2001-09-13 14:46:09 +000016#ifndef _BTREE_H_
17#define _BTREE_H_
drha059ad02001-04-17 20:09:11 +000018
danielk19774adee202004-05-08 08:23:19 +000019/* TODO: This definition is just included so other modules compile. It
20** needs to be revisited.
21*/
drh91618562014-12-19 19:28:02 +000022#define SQLITE_N_BTREE_META 16
danielk19774adee202004-05-08 08:23:19 +000023
drh73509ee2003-04-06 20:44:45 +000024/*
danielk1977951af802004-11-05 15:45:09 +000025** 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
danielk1977dddbcdc2007-04-26 14:42:34 +000032#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
danielk1977951af802004-11-05 15:45:09 +000036/*
drh73509ee2003-04-06 20:44:45 +000037** Forward declarations of structure
38*/
drha059ad02001-04-17 20:09:11 +000039typedef struct Btree Btree;
40typedef struct BtCursor BtCursor;
danielk1977aef0bf62005-12-30 16:28:01 +000041typedef struct BtShared BtShared;
paulb95a8862003-04-01 21:16:41 +000042
drh73509ee2003-04-06 20:44:45 +000043
danielk197724162fe2004-06-04 06:22:00 +000044int sqlite3BtreeOpen(
dan3a6d8ae2011-04-23 15:54:54 +000045 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
drh90f5ecb2004-07-22 01:19:35 +000046 const char *zFilename, /* Name of database file to open */
danielk1977aef0bf62005-12-30 16:28:01 +000047 sqlite3 *db, /* Associated database connection */
danielk19770d19f7a2009-06-03 11:25:07 +000048 Btree **ppBtree, /* Return open Btree* here */
drh33f4e022007-09-03 15:19:34 +000049 int flags, /* Flags */
50 int vfsFlags /* Flags passed through to VFS open */
danielk197724162fe2004-06-04 06:22:00 +000051);
drh3aac2dd2004-04-26 14:10:20 +000052
53/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
54** following values.
drh7bec5052005-02-06 02:45:41 +000055**
56** NOTE: These values must match the corresponding PAGER_ values in
57** pager.h.
drh73509ee2003-04-06 20:44:45 +000058*/
drhd4187c72010-08-30 22:15:45 +000059#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
drh33f111d2012-01-17 15:29:14 +000060#define BTREE_MEMORY 2 /* This is an in-memory DB */
61#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
62#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
drhe53831d2007-08-17 01:14:38 +000063
drh3aac2dd2004-04-26 14:10:20 +000064int sqlite3BtreeClose(Btree*);
65int sqlite3BtreeSetCacheSize(Btree*,int);
drh18c7e402014-03-14 11:46:10 +000066#if SQLITE_MAX_MMAP_SIZE>0
67 int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
68#endif
drh40c39412013-08-16 20:42:20 +000069int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
drh2c8997b2005-08-27 16:36:48 +000070int sqlite3BtreeSyncDisabled(Btree*);
danielk19770d19f7a2009-06-03 11:25:07 +000071int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
drh90f5ecb2004-07-22 01:19:35 +000072int sqlite3BtreeGetPageSize(Btree*);
drhf8e632b2007-05-08 14:51:36 +000073int sqlite3BtreeMaxPageCount(Btree*,int);
drhb1299152010-03-30 22:58:33 +000074u32 sqlite3BtreeLastPage(Btree*);
drh5b47efa2010-02-12 18:18:39 +000075int sqlite3BtreeSecureDelete(Btree*,int);
drhad0961b2015-02-21 00:19:25 +000076int sqlite3BtreeGetOptimalReserve(Btree*);
dan0094f372012-09-28 20:23:42 +000077int sqlite3BtreeGetReserveNoMutex(Btree *p);
danielk1977951af802004-11-05 15:45:09 +000078int sqlite3BtreeSetAutoVacuum(Btree *, int);
79int sqlite3BtreeGetAutoVacuum(Btree *);
danielk197740b38dc2004-06-26 08:38:24 +000080int sqlite3BtreeBeginTrans(Btree*,int);
drh80e35f42007-03-30 14:06:34 +000081int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
dan60939d02011-03-29 15:40:55 +000082int sqlite3BtreeCommitPhaseTwo(Btree*, int);
drh3aac2dd2004-04-26 14:10:20 +000083int sqlite3BtreeCommit(Btree*);
drh47b7fc72014-11-11 01:33:57 +000084int sqlite3BtreeRollback(Btree*,int,int);
danielk1977bd434552009-03-18 10:33:00 +000085int sqlite3BtreeBeginStmt(Btree*,int);
drh3aac2dd2004-04-26 14:10:20 +000086int sqlite3BtreeCreateTable(Btree*, int*, int flags);
danielk19771d850a72004-05-31 08:26:49 +000087int sqlite3BtreeIsInTrans(Btree*);
danielk19772372c2b2006-06-27 16:34:56 +000088int sqlite3BtreeIsInReadTrans(Btree*);
danielk197704103022009-02-03 16:51:24 +000089int sqlite3BtreeIsInBackup(Btree*);
danielk1977da184232006-01-05 11:34:32 +000090void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
danielk1977602b4662009-07-02 07:47:33 +000091int sqlite3BtreeSchemaLocked(Btree *pBtree);
92int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
danielk1977fd7f0452008-12-17 17:30:26 +000093int sqlite3BtreeSavepoint(Btree *, int, int);
drh3aac2dd2004-04-26 14:10:20 +000094
danielk19774adee202004-05-08 08:23:19 +000095const char *sqlite3BtreeGetFilename(Btree *);
danielk19775865e3d2004-06-14 06:03:57 +000096const char *sqlite3BtreeGetJournalname(Btree *);
danielk19774adee202004-05-08 08:23:19 +000097int sqlite3BtreeCopyFile(Btree *, Btree *);
98
danielk1977dddbcdc2007-04-26 14:42:34 +000099int sqlite3BtreeIncrVacuum(Btree *);
100
drh3aac2dd2004-04-26 14:10:20 +0000101/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
drhd4187c72010-08-30 22:15:45 +0000102** of the flags shown below.
103**
104** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
105** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
106** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
107** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
108** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
109** indices.)
drh73509ee2003-04-06 20:44:45 +0000110*/
drh8b18dd42004-05-12 19:18:15 +0000111#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
drhd4187c72010-08-30 22:15:45 +0000112#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
drh73509ee2003-04-06 20:44:45 +0000113
danielk1977a0bf2652004-11-04 14:30:04 +0000114int sqlite3BtreeDropTable(Btree*, int, int*);
danielk1977c7af4842008-10-27 13:59:33 +0000115int sqlite3BtreeClearTable(Btree*, int, int*);
drh079a3072014-03-19 14:10:55 +0000116int sqlite3BtreeClearTableOfCursor(BtCursor*);
dan80231042014-11-12 14:56:02 +0000117int sqlite3BtreeTripAllCursors(Btree*, int, int);
danielk19770d19f7a2009-06-03 11:25:07 +0000118
danielk1977602b4662009-07-02 07:47:33 +0000119void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
drh3aac2dd2004-04-26 14:10:20 +0000120int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
danielk19770d19f7a2009-06-03 11:25:07 +0000121
danb483eba2012-10-13 19:58:11 +0000122int sqlite3BtreeNewDb(Btree *p);
123
danielk19770d19f7a2009-06-03 11:25:07 +0000124/*
125** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
126** should be one of the following values. The integer values are assigned
127** to constants so that the offset of the corresponding field in an
128** SQLite database header may be found using the following formula:
129**
130** offset = 36 + (idx * 4)
131**
132** For example, the free-page-count field is located at byte offset 36 of
133** the database file header. The incr-vacuum-flag field is located at
134** byte offset 64 (== 36+4*7).
drh91618562014-12-19 19:28:02 +0000135**
136** The BTREE_DATA_VERSION value is not really a value stored in the header.
137** It is a read-only number computed by the pager. But we merge it with
138** the header value access routines since its access pattern is the same.
139** Call it a "virtual meta value".
danielk19770d19f7a2009-06-03 11:25:07 +0000140*/
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
drh4ee09b42013-05-01 19:49:27 +0000149#define BTREE_APPLICATION_ID 8
drh91618562014-12-19 19:28:02 +0000150#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
paulb95a8862003-04-01 21:16:41 +0000151
dan428c2182012-08-06 18:50:11 +0000152/*
153** Values that may be OR'd together to form the second argument of an
154** sqlite3BtreeCursorHints() call.
drhe0997b32015-03-20 14:57:50 +0000155**
156** The BTREE_BULKLOAD flag is set on index cursors when the index is going
157** to be filled with content that is already in sorted order.
158**
159** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
160** OP_SeekLE opcodes for a range search, but where the range of entries
161** selected will all have the same key. In other words, the cursor will
162** be used only for equality key searches.
163**
dan428c2182012-08-06 18:50:11 +0000164*/
drhe0997b32015-03-20 14:57:50 +0000165#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
166#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
dan428c2182012-08-06 18:50:11 +0000167
drh3aac2dd2004-04-26 14:10:20 +0000168int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +0000169 Btree*, /* BTree containing table to open */
170 int iTable, /* Index of root page */
171 int wrFlag, /* 1 for writing. 0 for read-only */
172 struct KeyInfo*, /* First argument to compare function */
173 BtCursor *pCursor /* Space to write cursor structure */
drh3aac2dd2004-04-26 14:10:20 +0000174);
drh59020f32008-04-26 13:39:46 +0000175int sqlite3BtreeCursorSize(void);
drhf25a5072009-11-18 23:01:25 +0000176void sqlite3BtreeCursorZero(BtCursor*);
paulb95a8862003-04-01 21:16:41 +0000177
drha34b6762004-05-07 13:30:42 +0000178int sqlite3BtreeCloseCursor(BtCursor*);
drhe63d9992008-08-13 19:11:48 +0000179int sqlite3BtreeMovetoUnpacked(
180 BtCursor*,
181 UnpackedRecord *pUnKey,
182 i64 intKey,
183 int bias,
184 int *pRes
185);
drh6848dad2014-08-22 23:33:03 +0000186int sqlite3BtreeCursorHasMoved(BtCursor*);
187int sqlite3BtreeCursorRestore(BtCursor*, int*);
drh3aac2dd2004-04-26 14:10:20 +0000188int sqlite3BtreeDelete(BtCursor*);
drh4a1c3802004-05-12 15:15:47 +0000189int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
drhb026e052007-05-02 01:34:31 +0000190 const void *pData, int nData,
danielk1977de630352009-05-04 11:42:29 +0000191 int nZero, int bias, int seekResult);
drh3aac2dd2004-04-26 14:10:20 +0000192int sqlite3BtreeFirst(BtCursor*, int *pRes);
193int sqlite3BtreeLast(BtCursor*, int *pRes);
194int sqlite3BtreeNext(BtCursor*, int *pRes);
drhc39e0002004-05-07 23:50:57 +0000195int sqlite3BtreeEof(BtCursor*);
drh3aac2dd2004-04-26 14:10:20 +0000196int sqlite3BtreePrevious(BtCursor*, int *pRes);
drh4a1c3802004-05-12 15:15:47 +0000197int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
drh3aac2dd2004-04-26 14:10:20 +0000198int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
drh501932c2013-11-21 21:59:53 +0000199const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
200const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
drh3aac2dd2004-04-26 14:10:20 +0000201int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
202int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
drh144f9ea2003-04-16 01:28:16 +0000203
drh1dcdbc02007-01-27 02:24:54 +0000204char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
drha34b6762004-05-07 13:30:42 +0000205struct Pager *sqlite3BtreePager(Btree*);
206
danielk1977dcbb5d32007-05-04 18:36:44 +0000207int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
dan5a500af2014-03-11 20:33:04 +0000208void sqlite3BtreeIncrblobCursor(BtCursor *);
danielk1977be51a652008-10-08 17:58:48 +0000209void sqlite3BtreeClearCursor(BtCursor *);
dane04dc882010-04-20 18:53:15 +0000210int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
dan428c2182012-08-06 18:50:11 +0000211void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
drhe0997b32015-03-20 14:57:50 +0000212#ifdef SQLITE_DEBUG
213int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
214#endif
drh781597f2014-05-21 08:21:07 +0000215int sqlite3BtreeIsReadonly(Btree *pBt);
drhdef68892014-11-04 12:11:23 +0000216int sqlite3HeaderSizeBtree(void);
dane04dc882010-04-20 18:53:15 +0000217
drhea8ffdf2009-07-22 00:35:23 +0000218#ifndef NDEBUG
219int sqlite3BtreeCursorIsValid(BtCursor*);
220#endif
221
danielk1977a5533162009-02-24 10:01:51 +0000222#ifndef SQLITE_OMIT_BTREECOUNT
223int sqlite3BtreeCount(BtCursor *, i64 *);
224#endif
225
drha34b6762004-05-07 13:30:42 +0000226#ifdef SQLITE_TEST
drh3e27c022004-07-23 00:01:38 +0000227int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
drhc8629a12004-05-08 20:07:40 +0000228void sqlite3BtreeCursorList(Btree*);
danielk1977b5402fb2005-01-12 07:15:04 +0000229#endif
drhbe0072d2001-09-13 14:46:09 +0000230
dana550f2d2010-08-02 10:47:05 +0000231#ifndef SQLITE_OMIT_WAL
dancdc1f042010-11-18 12:11:05 +0000232 int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
dana550f2d2010-08-02 10:47:05 +0000233#endif
234
drhb1ab8ea2007-08-29 00:33:07 +0000235/*
236** If we are not using shared cache, then there is no need to
237** use mutexes to access the BtShared structures. So make the
238** Enter and Leave procedures no-ops.
239*/
danielk1977f7590db2009-04-10 12:55:16 +0000240#ifndef SQLITE_OMIT_SHARED_CACHE
drhb1ab8ea2007-08-29 00:33:07 +0000241 void sqlite3BtreeEnter(Btree*);
danielk1977f7590db2009-04-10 12:55:16 +0000242 void sqlite3BtreeEnterAll(sqlite3*);
243#else
244# define sqlite3BtreeEnter(X)
245# define sqlite3BtreeEnterAll(X)
shanea6f6d202008-05-29 03:01:23 +0000246#endif
danielk1977f7590db2009-04-10 12:55:16 +0000247
248#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
drhdc5b0472011-04-06 22:05:53 +0000249 int sqlite3BtreeSharable(Btree*);
danielk1977f7590db2009-04-10 12:55:16 +0000250 void sqlite3BtreeLeave(Btree*);
drhff0587c2007-08-29 17:43:19 +0000251 void sqlite3BtreeEnterCursor(BtCursor*);
252 void sqlite3BtreeLeaveCursor(BtCursor*);
drhb1ab8ea2007-08-29 00:33:07 +0000253 void sqlite3BtreeLeaveAll(sqlite3*);
shanea6f6d202008-05-29 03:01:23 +0000254#ifndef NDEBUG
danielk1977f7590db2009-04-10 12:55:16 +0000255 /* These routines are used inside assert() statements only. */
256 int sqlite3BtreeHoldsMutex(Btree*);
257 int sqlite3BtreeHoldsAllMutexes(sqlite3*);
drh21206082011-04-04 18:22:02 +0000258 int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
shanea6f6d202008-05-29 03:01:23 +0000259#endif
danielk1977f7590db2009-04-10 12:55:16 +0000260#else
261
drhdc5b0472011-04-06 22:05:53 +0000262# define sqlite3BtreeSharable(X) 0
danielk1977f7590db2009-04-10 12:55:16 +0000263# define sqlite3BtreeLeave(X)
drhff0587c2007-08-29 17:43:19 +0000264# define sqlite3BtreeEnterCursor(X)
265# define sqlite3BtreeLeaveCursor(X)
drhb1ab8ea2007-08-29 00:33:07 +0000266# define sqlite3BtreeLeaveAll(X)
danielk1977f7590db2009-04-10 12:55:16 +0000267
268# define sqlite3BtreeHoldsMutex(X) 1
269# define sqlite3BtreeHoldsAllMutexes(X) 1
drhe54e0512011-04-05 17:31:56 +0000270# define sqlite3SchemaMutexHeld(X,Y,Z) 1
drh900b31e2007-08-28 02:27:51 +0000271#endif
272
273
drhbe0072d2001-09-13 14:46:09 +0000274#endif /* _BTREE_H_ */