blob: 49fa6dc37d93b3fbcf9bfa028b0a445a5e063de8 [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*************************************************************************
sheareracc225a2020-09-17 15:04:16 +000012** This header file defines the interface to the sqlite B-Tree storage
13** subsystem. All SQLite database operations become calls to the B-Tree
14** storage layer. The B-Tree layer requires a key-value store underneath
15** that is capable of MVCC - that is, transaction commit and rollback,
16** locking and concurrent access. See comments in btree.c for a detailed
17** description of each interface routine.
18**
sheareracc225a2020-09-17 15:04:16 +000019** The btree module organises an SQL database of tables and rows into
20** multiple key-value pair stores, which in turn are represented as
21** fixed-size pages in the memory-based page cache. The BTree functions
22** operate on pages in the page cache. A separate B-tree is used for each
23** table and each index in the database. Databases are stored in an on-disk
24** Database Image, which the B-Tree module knows nothing about except for
25** opening and closing.
26**
27** The architecture is described at https://www.sqlite.org/arch.html .
shearera41cc5c2020-10-06 15:48:59 +000028**
29** A Btree record is also called a payload in SQLite source. A Btree
30** record for table data contains only two fields: the unique key value
31** ROWID, and the table row data. A Btree record for an index btree or
32** a WITHOUT ROWID table contain an arbitary key and no (uninitialised) data.
33** Btree pages are a fixed size. There will usually be multiple payloads
34** per page, but large payloads (eg BLOB data) may spill over to multiple
35** pages.
36**
37** Functions in this header file are grouped according to their logical task:
38**
39** Opening and Closing Database Connections
40**
41** Database Image Configuration and Querying
42**
43** Btree Connection Configuration and Querying
44**
45** Mutex Function Wrappers
46**
47** Transaction and Savepoint Functions
48**
49** Cursors and Cursor Functions
50**
51** Record and Payload Handling Functions
52**
53** Table Functions
54**
55** Reading and Writing Metadata
56**
drha059ad02001-04-17 20:09:11 +000057*/
sheareracc225a2020-09-17 15:04:16 +000058
drh43f58d62016-07-09 16:14:45 +000059#ifndef SQLITE_BTREE_H
60#define SQLITE_BTREE_H
drha059ad02001-04-17 20:09:11 +000061
drh73509ee2003-04-06 20:44:45 +000062/*
sheareracc225a2020-09-17 15:04:16 +000063** Forward declarations of structure
64*/
65typedef struct Btree Btree;
66typedef struct BtCursor BtCursor;
67typedef struct BtShared BtShared;
68typedef struct BtreePayload BtreePayload;
69
shearer4598bf82020-10-27 11:58:37 +000070/*
71********************************************
72* Opening and Closing Database Connections
73********************************************
sheareracc225a2020-09-17 15:04:16 +000074*/
75
76int sqlite3BtreeOpen(
77 sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
78 const char *zFilename, /* Name of database file to open */
79 sqlite3 *db, /* Associated database connection */
80 Btree **ppBtree, /* Return open Btree* here */
81 int flags, /* Flags */
82 int vfsFlags /* Flags passed through to VFS open */
83);
84
shearerf3113152020-10-27 11:07:16 +000085/* The flags parameter to sqlite3BtreeOpen can be the bitwise OR of the
86** following values.
87**
88** NOTE: These values must match the corresponding PAGER_ values in
89** pager.h.
90*/
91
sheareracc225a2020-09-17 15:04:16 +000092#define BTREE_OMIT_JOURNAL 1 /* No create/use journal in temp databases */
93#define BTREE_MEMORY 2 /* This is an in-memory DB */
94#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
95#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
96 /* Only a BTREE_SINGLE can be BTREE_UNORDERED */
97
98int sqlite3BtreeClose(Btree*);
99
100/*
shearer4598bf82020-10-27 11:58:37 +0000101**********************************************
102** Database Image Configuration and Querying
103**********************************************
sheareracc225a2020-09-17 15:04:16 +0000104*/
105
106/*
danielk1977951af802004-11-05 15:45:09 +0000107** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
108** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
109*/
110#ifndef SQLITE_DEFAULT_AUTOVACUUM
111 #define SQLITE_DEFAULT_AUTOVACUUM 0
112#endif
113
danielk1977dddbcdc2007-04-26 14:42:34 +0000114#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
115#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
116#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
117
sheareracc225a2020-09-17 15:04:16 +0000118int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
119int sqlite3BtreeGetPageSize(Btree*);
120
121int sqlite3BtreeSetAutoVacuum(Btree *, int);
122int sqlite3BtreeGetAutoVacuum(Btree *);
123
124int sqlite3BtreeSetSpillSize(Btree*,int);
125#if SQLITE_MAX_MMAP_SIZE>0
126 int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
127#endif
128int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
129int sqlite3BtreeSecureDelete(Btree*,int);
130int sqlite3BtreeGetRequestedReserve(Btree*);
131int sqlite3BtreeGetReserveNoMutex(Btree *p);
132
shearerf3113152020-10-27 11:07:16 +0000133/* Implements PRAGMA integrity_check on a Btree and its associated file.
134 * Only called from vdbe.c/OP_IntegrityCk */
shearer608e2f02020-10-27 11:13:37 +0000135char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,Pgno*aRoot,int nRoot,int,int*);
shearerf3113152020-10-27 11:07:16 +0000136
137/* A single step of an incremental vacuum. For PRAGMA incremental_vacuum(N) */
138/* Neither autovacuum mode nor the VACUUM SQLite command use this function. */
139int sqlite3BtreeIncrVacuum(Btree *);
140
141/* Copy a complete Btree into another Btree, ie from one file into another */
142/* Used only in the case of a backup and vacuum operations */
143int sqlite3BtreeCopyFile(Btree *, Btree *);
144
sheareracc225a2020-09-17 15:04:16 +0000145
shearer4598bf82020-10-27 11:58:37 +0000146/*
147************************************************
148** Btree Connection Configuration and Querying
149************************************************
drh73509ee2003-04-06 20:44:45 +0000150*/
drh3aac2dd2004-04-26 14:10:20 +0000151
drhe53831d2007-08-17 01:14:38 +0000152
drh3aac2dd2004-04-26 14:10:20 +0000153int sqlite3BtreeSetCacheSize(Btree*,int);
drhe9261db2020-07-20 12:47:32 +0000154Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno);
drh584e8b72020-07-22 17:12:59 +0000155Pgno sqlite3BtreeLastPage(Btree*);
sheareracc225a2020-09-17 15:04:16 +0000156const char *sqlite3BtreeGetFilename(Btree *);
157const char *sqlite3BtreeGetJournalname(Btree *);
158
shearera41cc5c2020-10-06 15:48:59 +0000159int sqlite3BtreeIsReadonly(Btree *pBt);
160
161/* Estimate number of rows in table
162 * called only by OP IsSmaller, from PRAGMA optimize
163*/
164i64 sqlite3BtreeRowCountEst(BtCursor*);
165
shearerf3113152020-10-27 11:07:16 +0000166/* Return the pager associated with a BTree */
167struct Pager *sqlite3BtreePager(Btree*);
168
shearera41cc5c2020-10-06 15:48:59 +0000169
shearer4598bf82020-10-27 11:58:37 +0000170/*
171****************************
172** Mutex Function Wrappers
173****************************
sheareracc225a2020-09-17 15:04:16 +0000174**
175** Every lock applies to an entire BTree. These functions are
176** wrappers for the sqlite3_mutex* functions, which are called
177** outside btree.c
178*/
179
180/*
181** If we are not using shared cache, then there is no need to
182** use mutexes to access the BtShared structures. So make the
183** Enter and Leave procedures no-ops.
184*/
185#ifndef SQLITE_OMIT_SHARED_CACHE
186 void sqlite3BtreeEnter(Btree*);
187 void sqlite3BtreeEnterAll(sqlite3*);
188 int sqlite3BtreeSharable(Btree*);
189 void sqlite3BtreeEnterCursor(BtCursor*);
190 int sqlite3BtreeConnectionCount(Btree*);
191#else
192# define sqlite3BtreeEnter(X)
193# define sqlite3BtreeEnterAll(X)
194# define sqlite3BtreeSharable(X) 0
195# define sqlite3BtreeEnterCursor(X)
196# define sqlite3BtreeConnectionCount(X) 1
197#endif
198
199#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
200 void sqlite3BtreeLeave(Btree*);
201 void sqlite3BtreeLeaveCursor(BtCursor*);
202 void sqlite3BtreeLeaveAll(sqlite3*);
203#ifndef NDEBUG
204 /* These routines are used inside assert() statements only. */
205 int sqlite3BtreeHoldsMutex(Btree*);
206 int sqlite3BtreeHoldsAllMutexes(sqlite3*);
207 int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
208#endif
209#else
210
211# define sqlite3BtreeLeave(X)
212# define sqlite3BtreeLeaveCursor(X)
213# define sqlite3BtreeLeaveAll(X)
214
215# define sqlite3BtreeHoldsMutex(X) 1
216# define sqlite3BtreeHoldsAllMutexes(X) 1
217# define sqlite3SchemaMutexHeld(X,Y,Z) 1
218#endif
219
220
221/*
shearer4598bf82020-10-27 11:58:37 +0000222****************************************
sheareracc225a2020-09-17 15:04:16 +0000223** Transaction and SavePoint Functions
shearer4598bf82020-10-27 11:58:37 +0000224****************************************
sheareracc225a2020-09-17 15:04:16 +0000225*/
226
drhbb2d9b12018-06-06 16:28:40 +0000227int sqlite3BtreeBeginTrans(Btree*,int,int*);
sheareracc225a2020-09-17 15:04:16 +0000228int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
dan60939d02011-03-29 15:40:55 +0000229int sqlite3BtreeCommitPhaseTwo(Btree*, int);
drh3aac2dd2004-04-26 14:10:20 +0000230int sqlite3BtreeCommit(Btree*);
drh47b7fc72014-11-11 01:33:57 +0000231int sqlite3BtreeRollback(Btree*,int,int);
drhabc38152020-07-22 13:38:04 +0000232int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags);
drh99744fa2020-08-25 19:09:07 +0000233int sqlite3BtreeTxnState(Btree*);
danielk197704103022009-02-03 16:51:24 +0000234int sqlite3BtreeIsInBackup(Btree*);
shearer399b7402020-09-25 16:54:55 +0000235
shearera41cc5c2020-10-06 15:48:59 +0000236/* Savepoints are named, nestable SQL transactions mostly implemented */
shearer1e6c58d2020-09-30 09:17:53 +0000237/* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */
danielk1977fd7f0452008-12-17 17:30:26 +0000238int sqlite3BtreeSavepoint(Btree *, int, int);
drh3aac2dd2004-04-26 14:10:20 +0000239
shearera41cc5c2020-10-06 15:48:59 +0000240/* A statement sub-transaction is an internal-only transaction used */
241/* only by OP_Transaction, see comments in vdbe.c. A statement */
242/* sub-transaction is implemented as an anonymous savepoint. */
243int sqlite3BtreeBeginStmt(Btree*,int);
244
shearer399b7402020-09-25 16:54:55 +0000245/* A checkpoint refers to only to WAL. See https://sqlite.org/wal.html#ckpt */
sheareracc225a2020-09-17 15:04:16 +0000246#ifndef SQLITE_OMIT_WAL
247 int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
248#endif
danielk19774adee202004-05-08 08:23:19 +0000249
shearera41cc5c2020-10-06 15:48:59 +0000250/* Set all relevant cursors to error state on transaction rollback */
dan80231042014-11-12 14:56:02 +0000251int sqlite3BtreeTripAllCursors(Btree*, int, int);
danielk19770d19f7a2009-06-03 11:25:07 +0000252
danielk19770d19f7a2009-06-03 11:25:07 +0000253
254/*
shearer4598bf82020-10-27 11:58:37 +0000255*********************************
256** Cursors and Cursor functions
257*********************************
shearerfc1070b2020-09-25 13:56:31 +0000258**
danielk19770d19f7a2009-06-03 11:25:07 +0000259*/
sheareracc225a2020-09-17 15:04:16 +0000260
dan428c2182012-08-06 18:50:11 +0000261/*
drh0df57012015-08-14 15:05:55 +0000262** Kinds of hints that can be passed into the sqlite3BtreeCursorHint()
263** interface.
264**
drh0df57012015-08-14 15:05:55 +0000265** BTREE_HINT_RANGE (arguments: Expr*, Mem*)
266**
267** The first argument is an Expr* (which is guaranteed to be constant for
268** the lifetime of the cursor) that defines constraints on which rows
269** might be fetched with this cursor. The Expr* tree may contain
270** TK_REGISTER nodes that refer to values stored in the array of registers
271** passed as the second parameter. In other words, if Expr.op==TK_REGISTER
272** then the value of the node is the value in Mem[pExpr.iTable]. Any
273** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th
274** column of the b-tree of the cursor. The Expr tree will not contain
275** any function calls nor subqueries nor references to b-trees other than
276** the cursor being hinted.
277**
278** The design of the _RANGE hint is aid b-tree implementations that try
279** to prefetch content from remote machines - to provide those
280** implementations with limits on what needs to be prefetched and thereby
281** reduce network bandwidth.
drh0403cb32015-08-14 23:57:04 +0000282**
283** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by
284** standard SQLite. The other hints are provided for extentions that use
285** the SQLite parser and code generator but substitute their own storage
286** engine.
drh0df57012015-08-14 15:05:55 +0000287*/
drhf7854c72015-10-27 13:24:37 +0000288#define BTREE_HINT_RANGE 0 /* Range constraints on queries */
drh0df57012015-08-14 15:05:55 +0000289
290/*
291** Values that may be OR'd together to form the argument to the
292** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint():
drhe0997b32015-03-20 14:57:50 +0000293**
294** The BTREE_BULKLOAD flag is set on index cursors when the index is going
295** to be filled with content that is already in sorted order.
296**
297** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or
298** OP_SeekLE opcodes for a range search, but where the range of entries
299** selected will all have the same key. In other words, the cursor will
300** be used only for equality key searches.
301**
dan428c2182012-08-06 18:50:11 +0000302*/
drhe0997b32015-03-20 14:57:50 +0000303#define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */
304#define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */
dan428c2182012-08-06 18:50:11 +0000305
danfd261ec2015-10-22 20:54:33 +0000306/*
307** Flags passed as the third argument to sqlite3BtreeCursor().
dan2b4e9522015-10-23 11:50:23 +0000308**
309** For read-only cursors the wrFlag argument is always zero. For read-write
drh9c0c57a2016-01-21 15:55:37 +0000310** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
311** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
dan2b4e9522015-10-23 11:50:23 +0000312** only be used by SQLite for the following:
313**
drh9c0c57a2016-01-21 15:55:37 +0000314** * to seek to and then delete specific entries, and/or
dan2b4e9522015-10-23 11:50:23 +0000315**
316** * to read values that will be used to create keys that other
317** BTREE_FORDELETE cursors will seek to and delete.
drh9c0c57a2016-01-21 15:55:37 +0000318**
319** The BTREE_FORDELETE flag is an optimization hint. It is not used by
320** by this, the native b-tree engine of SQLite, but it is available to
321** alternative storage engines that might be substituted in place of this
322** b-tree system. For alternative storage engines in which a delete of
323** the main table row automatically deletes corresponding index rows,
324** the FORDELETE flag hint allows those alternative storage engines to
325** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
326** and DELETE operations as no-ops, and any READ operation against a
327** FORDELETE cursor may return a null row: 0x01 0x00.
danfd261ec2015-10-22 20:54:33 +0000328*/
dan2b4e9522015-10-23 11:50:23 +0000329#define BTREE_WRCSR 0x00000004 /* read-write cursor */
330#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
danfd261ec2015-10-22 20:54:33 +0000331
drh3aac2dd2004-04-26 14:10:20 +0000332int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +0000333 Btree*, /* BTree containing table to open */
sheareracc225a2020-09-17 15:04:16 +0000334 Pgno iTable, /* Index of root page */
danielk1977cd3e8f72008-03-25 09:47:35 +0000335 int wrFlag, /* 1 for writing. 0 for read-only */
336 struct KeyInfo*, /* First argument to compare function */
337 BtCursor *pCursor /* Space to write cursor structure */
drh3aac2dd2004-04-26 14:10:20 +0000338);
shearerfc1070b2020-09-25 13:56:31 +0000339
340/* True if hint specified, used in or around assert statements only */
341int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
342
drhfe0cf7a2017-08-16 19:20:20 +0000343BtCursor *sqlite3BtreeFakeValidCursor(void);
sheareracc225a2020-09-17 15:04:16 +0000344
345#ifdef SQLITE_DEBUG
346sqlite3_uint64 sqlite3BtreeSeekCount(Btree*);
347#else
348# define sqlite3BtreeSeekCount(X) 0
349#endif
350
351#ifndef NDEBUG
352int sqlite3BtreeCursorIsValid(BtCursor*);
353#endif
354int sqlite3BtreeCursorIsValidNN(BtCursor*);
355
drh59020f32008-04-26 13:39:46 +0000356int sqlite3BtreeCursorSize(void);
drhf25a5072009-11-18 23:01:25 +0000357void sqlite3BtreeCursorZero(BtCursor*);
drhf7854c72015-10-27 13:24:37 +0000358void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
359#ifdef SQLITE_ENABLE_CURSOR_HINTS
drh0403cb32015-08-14 23:57:04 +0000360void sqlite3BtreeCursorHint(BtCursor*, int, ...);
drhf7854c72015-10-27 13:24:37 +0000361#endif
paulb95a8862003-04-01 21:16:41 +0000362
sheareracc225a2020-09-17 15:04:16 +0000363void sqlite3BtreeClearCursor(BtCursor *);
drha34b6762004-05-07 13:30:42 +0000364int sqlite3BtreeCloseCursor(BtCursor*);
sheareracc225a2020-09-17 15:04:16 +0000365
366/* Cursor seek to a specified rowid in the Btree */
drhe63d9992008-08-13 19:11:48 +0000367int sqlite3BtreeMovetoUnpacked(
368 BtCursor*,
369 UnpackedRecord *pUnKey,
370 i64 intKey,
371 int bias,
372 int *pRes
373);
sheareracc225a2020-09-17 15:04:16 +0000374
375int sqlite3BtreeFirst(BtCursor*, int *pRes);
376int sqlite3BtreeLast(BtCursor*, int *pRes);
377int sqlite3BtreeNext(BtCursor*, int flags);
378int sqlite3BtreeEof(BtCursor*);
379int sqlite3BtreePrevious(BtCursor*, int flags);
380i64 sqlite3BtreeIntegerKey(BtCursor*);
381
382int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*);
383
384
385void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
386int sqlite3BtreeSchemaLocked(Btree *pBtree);
387#ifndef SQLITE_OMIT_SHARED_CACHE
388int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
389#endif
390void sqlite3BtreeCursorPin(BtCursor*);
391void sqlite3BtreeCursorUnpin(BtCursor*);
392
393int sqlite3BtreeCursorHasMoved(BtCursor*);
394int sqlite3BtreeCursorRestore(BtCursor*, int*);
395
396#ifdef SQLITE_TEST
397int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
398void sqlite3BtreeCursorList(Btree*);
399#endif
400
sheareracc225a2020-09-17 15:04:16 +0000401/*
shearer4598bf82020-10-27 11:58:37 +0000402******************************************
403** Record and Payload handling functions
404******************************************
sheareracc225a2020-09-17 15:04:16 +0000405**
shearera41cc5c2020-10-06 15:48:59 +0000406** A Btree record is a key-value pair consisting of a rowid key, and arbitary
407** data value. The record is also called a payload.
408**
sheareracc225a2020-09-17 15:04:16 +0000409*/
410
danf91c1312017-01-10 20:04:38 +0000411/* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */
drhe807bdb2016-01-21 17:06:33 +0000412#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
drhdef19e32016-01-27 16:26:25 +0000413#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
danf91c1312017-01-10 20:04:38 +0000414#define BTREE_APPEND 0x08 /* Insert is likely an append */
drhe807bdb2016-01-21 17:06:33 +0000415
drh8eeb4462016-05-21 20:03:42 +0000416/* An instance of the BtreePayload object describes the content of a single
417** entry in either an index or table btree.
418**
419** Index btrees (used for indexes and also WITHOUT ROWID tables) contain
drh89ee2292018-05-07 18:41:19 +0000420** an arbitrary key and no data. These btrees have pKey,nKey set to the
421** key and the pData,nData,nZero fields are uninitialized. The aMem,nMem
422** fields give an array of Mem objects that are a decomposition of the key.
423** The nMem field might be zero, indicating that no decomposition is available.
drh8eeb4462016-05-21 20:03:42 +0000424**
425** Table btrees (used for rowid tables) contain an integer rowid used as
426** the key and passed in the nKey field. The pKey field is zero.
427** pData,nData hold the content of the new entry. nZero extra zero bytes
428** are appended to the end of the content when constructing the entry.
drh89ee2292018-05-07 18:41:19 +0000429** The aMem,nMem fields are uninitialized for table btrees.
430**
431** Field usage summary:
432**
433** Table BTrees Index Btrees
434**
435** pKey always NULL encoded key
436** nKey the ROWID length of pKey
437** pData data not used
438** aMem not used decomposed key value
439** nMem not used entries in aMem
440** nData length of pData not used
441** nZero extra zeros after pData not used
drh8eeb4462016-05-21 20:03:42 +0000442**
443** This object is used to pass information into sqlite3BtreeInsert(). The
444** same information used to be passed as five separate parameters. But placing
445** the information into this object helps to keep the interface more
446** organized and understandable, and it also helps the resulting code to
447** run a little faster by using fewer registers for parameter passing.
448*/
449struct BtreePayload {
450 const void *pKey; /* Key content for indexes. NULL for tables */
451 sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */
drh89ee2292018-05-07 18:41:19 +0000452 const void *pData; /* Data for tables. */
drh7a6ea932017-04-09 19:23:55 +0000453 sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */
drh9b4eaeb2016-11-09 00:10:33 +0000454 u16 nMem; /* Number of aMem[] value. Might be zero */
drh8eeb4462016-05-21 20:03:42 +0000455 int nData; /* Size of pData. 0 if none. */
456 int nZero; /* Extra zero data appended after pData,nData */
457};
458
459int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
danf91c1312017-01-10 20:04:38 +0000460 int flags, int seekResult);
shearera41cc5c2020-10-06 15:48:59 +0000461
462int sqlite3BtreeDelete(BtCursor*, u8 flags);
463
drh092457b2017-12-29 15:04:49 +0000464#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
465i64 sqlite3BtreeOffset(BtCursor*);
466#endif
drhcb3cabd2016-11-25 19:18:28 +0000467int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
drha7c90c42016-06-04 20:37:10 +0000468const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
469u32 sqlite3BtreePayloadSize(BtCursor*);
sheareracc225a2020-09-17 15:04:16 +0000470
shearerf3113152020-10-27 11:07:16 +0000471/* These functions deal with arbitary-sized binary blobs in a Btree */
472#ifndef SQLITE_OMIT_INCRBLOB
473int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
474int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
475void sqlite3BtreeIncrblobCursor(BtCursor *);
476#endif
477
478
shearera41cc5c2020-10-06 15:48:59 +0000479
480/*
shearer4598bf82020-10-27 11:58:37 +0000481********************
482** Table functions
483********************
shearera41cc5c2020-10-06 15:48:59 +0000484**
485*/
486
487/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
488** of the flags shown below.
489**
490** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
491** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
492** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
493** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
494** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
495** indices.)
496*/
497
498#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
499#define BTREE_BLOBKEY 2 /* Table has keys only - no data */
500
501int sqlite3BtreeDropTable(Btree*, int, int*);
502int sqlite3BtreeClearTable(Btree*, int, int*);
503int sqlite3BtreeClearTableOfCursor(BtCursor*);
sheareracc225a2020-09-17 15:04:16 +0000504
505
506/*
shearer4598bf82020-10-27 11:58:37 +0000507*********************************
508** Reading and Writing Metadata
509*********************************
sheareracc225a2020-09-17 15:04:16 +0000510**
511** Note there is metadata which is not in the first database page
512** including (as below) some visible via the os.h API
513*/
514
515
516/*
517** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
518** should be one of the following values. The integer values are assigned
519** to constants so that the offset of the corresponding field in an
520** SQLite database header may be found using the following formula:
521**
522** offset = 36 + (idx * 4)
523**
524** For example, the free-page-count field is located at byte offset 36 of
525** the database file header. The incr-vacuum-flag field is located at
526** byte offset 64 (== 36+4*7).
527**
528** The BTREE_DATA_VERSION value is not really a value stored in the header.
529** It is a read-only number computed by the pager. But we merge it with
530** the header value access routines since its access pattern is the same.
531** Call it a "virtual meta value".
532*/
533#define BTREE_FREE_PAGE_COUNT 0
534#define BTREE_SCHEMA_VERSION 1
535#define BTREE_FILE_FORMAT 2
536#define BTREE_DEFAULT_CACHE_SIZE 3
537#define BTREE_LARGEST_ROOT_PAGE 4
538#define BTREE_TEXT_ENCODING 5
539#define BTREE_USER_VERSION 6
540#define BTREE_INCR_VACUUM 7
541#define BTREE_APPLICATION_ID 8
542#define BTREE_DATA_VERSION 15 /* A virtual meta-value */
543
544
shearerfc1070b2020-09-25 13:56:31 +0000545/* Refers to the size of the per-page header, not per-database header */
546/* Used for configuring the size of the pages in the page cache */
547int sqlite3HeaderSizeBtree(void);
548
shearer153fa602020-09-24 06:37:18 +0000549/* TODO: This definition is only used in asserts to determine whether
550 * the metadata index (second parameter of Get/UpdateMeta functions)
551 * is out of range. It is only included here so other modules compile. It
552 * ** needs to be revisited.
553 * */
554#define SQLITE_N_BTREE_META 16
555
sheareracc225a2020-09-17 15:04:16 +0000556void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
557int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
558
559/* This returns the size of the current database file, ie the same */
560/* result as sqlite3OsFileSize in os.h . */
drh53d30dd2019-02-04 21:10:24 +0000561sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
drh144f9ea2003-04-16 01:28:16 +0000562
shearerfc1070b2020-09-25 13:56:31 +0000563/* Version number of the file format must be either 1 or 2 */
564/* 1=legacy, 2=WAL . Read and write versions set to same value */
sheareracc225a2020-09-17 15:04:16 +0000565int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
566
shearera41cc5c2020-10-06 15:48:59 +0000567/* Initialize the first page of the database file and return */
568int sqlite3BtreeNewDb(Btree *p);
569
sheareracc225a2020-09-17 15:04:16 +0000570
drh43f58d62016-07-09 16:14:45 +0000571#endif /* SQLITE_BTREE_H */