drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 12 | ** 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 | ** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 19 | ** 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 . |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 28 | ** |
| 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 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 57 | */ |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 58 | |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 59 | #ifndef SQLITE_BTREE_H |
| 60 | #define SQLITE_BTREE_H |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 61 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 62 | /* |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 63 | ** Forward declarations of structure |
| 64 | */ |
| 65 | typedef struct Btree Btree; |
| 66 | typedef struct BtCursor BtCursor; |
| 67 | typedef struct BtShared BtShared; |
| 68 | typedef struct BtreePayload BtreePayload; |
| 69 | |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 70 | /* |
| 71 | ******************************************** |
| 72 | * Opening and Closing Database Connections |
| 73 | ******************************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | int 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 | |
shearer | f311315 | 2020-10-27 11:07:16 +0000 | [diff] [blame] | 85 | /* 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 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 92 | #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 | |
| 98 | int sqlite3BtreeClose(Btree*); |
| 99 | |
| 100 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 101 | ********************************************** |
| 102 | ** Database Image Configuration and Querying |
| 103 | ********************************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 107 | ** 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 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 114 | #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 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 118 | int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); |
| 119 | int sqlite3BtreeGetPageSize(Btree*); |
| 120 | |
| 121 | int sqlite3BtreeSetAutoVacuum(Btree *, int); |
| 122 | int sqlite3BtreeGetAutoVacuum(Btree *); |
| 123 | |
| 124 | int sqlite3BtreeSetSpillSize(Btree*,int); |
| 125 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 126 | int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64); |
| 127 | #endif |
| 128 | int sqlite3BtreeSetPagerFlags(Btree*,unsigned); |
| 129 | int sqlite3BtreeSecureDelete(Btree*,int); |
| 130 | int sqlite3BtreeGetRequestedReserve(Btree*); |
| 131 | int sqlite3BtreeGetReserveNoMutex(Btree *p); |
| 132 | |
shearer | f311315 | 2020-10-27 11:07:16 +0000 | [diff] [blame] | 133 | /* Implements PRAGMA integrity_check on a Btree and its associated file. |
| 134 | * Only called from vdbe.c/OP_IntegrityCk */ |
shearer | 608e2f0 | 2020-10-27 11:13:37 +0000 | [diff] [blame] | 135 | char *sqlite3BtreeIntegrityCheck(sqlite3*,Btree*,Pgno*aRoot,int nRoot,int,int*); |
shearer | f311315 | 2020-10-27 11:07:16 +0000 | [diff] [blame] | 136 | |
| 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. */ |
| 139 | int 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 */ |
| 143 | int sqlite3BtreeCopyFile(Btree *, Btree *); |
| 144 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 145 | |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 146 | /* |
| 147 | ************************************************ |
| 148 | ** Btree Connection Configuration and Querying |
| 149 | ************************************************ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 150 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 151 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 152 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 153 | int sqlite3BtreeSetCacheSize(Btree*,int); |
drh | e9261db | 2020-07-20 12:47:32 +0000 | [diff] [blame] | 154 | Pgno sqlite3BtreeMaxPageCount(Btree*,Pgno); |
drh | 584e8b7 | 2020-07-22 17:12:59 +0000 | [diff] [blame] | 155 | Pgno sqlite3BtreeLastPage(Btree*); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 156 | const char *sqlite3BtreeGetFilename(Btree *); |
| 157 | const char *sqlite3BtreeGetJournalname(Btree *); |
| 158 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 159 | int sqlite3BtreeIsReadonly(Btree *pBt); |
| 160 | |
| 161 | /* Estimate number of rows in table |
| 162 | * called only by OP IsSmaller, from PRAGMA optimize |
| 163 | */ |
| 164 | i64 sqlite3BtreeRowCountEst(BtCursor*); |
| 165 | |
shearer | f311315 | 2020-10-27 11:07:16 +0000 | [diff] [blame] | 166 | /* Return the pager associated with a BTree */ |
| 167 | struct Pager *sqlite3BtreePager(Btree*); |
| 168 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 169 | |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 170 | /* |
| 171 | **************************** |
| 172 | ** Mutex Function Wrappers |
| 173 | **************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 174 | ** |
| 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 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 222 | **************************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 223 | ** Transaction and SavePoint Functions |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 224 | **************************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 225 | */ |
| 226 | |
drh | bb2d9b1 | 2018-06-06 16:28:40 +0000 | [diff] [blame] | 227 | int sqlite3BtreeBeginTrans(Btree*,int,int*); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 228 | int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 229 | int sqlite3BtreeCommitPhaseTwo(Btree*, int); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 230 | int sqlite3BtreeCommit(Btree*); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 231 | int sqlite3BtreeRollback(Btree*,int,int); |
drh | abc3815 | 2020-07-22 13:38:04 +0000 | [diff] [blame] | 232 | int sqlite3BtreeCreateTable(Btree*, Pgno*, int flags); |
drh | 99744fa | 2020-08-25 19:09:07 +0000 | [diff] [blame] | 233 | int sqlite3BtreeTxnState(Btree*); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 234 | int sqlite3BtreeIsInBackup(Btree*); |
shearer | 399b740 | 2020-09-25 16:54:55 +0000 | [diff] [blame] | 235 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 236 | /* Savepoints are named, nestable SQL transactions mostly implemented */ |
shearer | 1e6c58d | 2020-09-30 09:17:53 +0000 | [diff] [blame] | 237 | /* in vdbe.c and pager.c See https://sqlite.org/lang_savepoint.html */ |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 238 | int sqlite3BtreeSavepoint(Btree *, int, int); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 239 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 240 | /* 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. */ |
| 243 | int sqlite3BtreeBeginStmt(Btree*,int); |
| 244 | |
shearer | 399b740 | 2020-09-25 16:54:55 +0000 | [diff] [blame] | 245 | /* A checkpoint refers to only to WAL. See https://sqlite.org/wal.html#ckpt */ |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 246 | #ifndef SQLITE_OMIT_WAL |
| 247 | int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); |
| 248 | #endif |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 249 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 250 | /* Set all relevant cursors to error state on transaction rollback */ |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 251 | int sqlite3BtreeTripAllCursors(Btree*, int, int); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 252 | |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 253 | |
| 254 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 255 | ********************************* |
| 256 | ** Cursors and Cursor functions |
| 257 | ********************************* |
shearer | fc1070b | 2020-09-25 13:56:31 +0000 | [diff] [blame] | 258 | ** |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 259 | */ |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 260 | |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 261 | /* |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 262 | ** Kinds of hints that can be passed into the sqlite3BtreeCursorHint() |
| 263 | ** interface. |
| 264 | ** |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 265 | ** 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. |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 282 | ** |
| 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. |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 287 | */ |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 288 | #define BTREE_HINT_RANGE 0 /* Range constraints on queries */ |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | ** Values that may be OR'd together to form the argument to the |
| 292 | ** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint(): |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 293 | ** |
| 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 | ** |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 302 | */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 303 | #define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */ |
| 304 | #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */ |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 305 | |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 306 | /* |
| 307 | ** Flags passed as the third argument to sqlite3BtreeCursor(). |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 308 | ** |
| 309 | ** For read-only cursors the wrFlag argument is always zero. For read-write |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 310 | ** 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 |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 312 | ** only be used by SQLite for the following: |
| 313 | ** |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 314 | ** * to seek to and then delete specific entries, and/or |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 315 | ** |
| 316 | ** * to read values that will be used to create keys that other |
| 317 | ** BTREE_FORDELETE cursors will seek to and delete. |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 318 | ** |
| 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. |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 328 | */ |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 329 | #define BTREE_WRCSR 0x00000004 /* read-write cursor */ |
| 330 | #define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 331 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 332 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 333 | Btree*, /* BTree containing table to open */ |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 334 | Pgno iTable, /* Index of root page */ |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 335 | 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 */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 338 | ); |
shearer | fc1070b | 2020-09-25 13:56:31 +0000 | [diff] [blame] | 339 | |
| 340 | /* True if hint specified, used in or around assert statements only */ |
| 341 | int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); |
| 342 | |
drh | fe0cf7a | 2017-08-16 19:20:20 +0000 | [diff] [blame] | 343 | BtCursor *sqlite3BtreeFakeValidCursor(void); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 344 | |
| 345 | #ifdef SQLITE_DEBUG |
| 346 | sqlite3_uint64 sqlite3BtreeSeekCount(Btree*); |
| 347 | #else |
| 348 | # define sqlite3BtreeSeekCount(X) 0 |
| 349 | #endif |
| 350 | |
| 351 | #ifndef NDEBUG |
| 352 | int sqlite3BtreeCursorIsValid(BtCursor*); |
| 353 | #endif |
| 354 | int sqlite3BtreeCursorIsValidNN(BtCursor*); |
| 355 | |
drh | 59020f3 | 2008-04-26 13:39:46 +0000 | [diff] [blame] | 356 | int sqlite3BtreeCursorSize(void); |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 357 | void sqlite3BtreeCursorZero(BtCursor*); |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 358 | void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned); |
| 359 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 360 | void sqlite3BtreeCursorHint(BtCursor*, int, ...); |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 361 | #endif |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 362 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 363 | void sqlite3BtreeClearCursor(BtCursor *); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 364 | int sqlite3BtreeCloseCursor(BtCursor*); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 365 | |
| 366 | /* Cursor seek to a specified rowid in the Btree */ |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 367 | int sqlite3BtreeMovetoUnpacked( |
| 368 | BtCursor*, |
| 369 | UnpackedRecord *pUnKey, |
| 370 | i64 intKey, |
| 371 | int bias, |
| 372 | int *pRes |
| 373 | ); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 374 | |
| 375 | int sqlite3BtreeFirst(BtCursor*, int *pRes); |
| 376 | int sqlite3BtreeLast(BtCursor*, int *pRes); |
| 377 | int sqlite3BtreeNext(BtCursor*, int flags); |
| 378 | int sqlite3BtreeEof(BtCursor*); |
| 379 | int sqlite3BtreePrevious(BtCursor*, int flags); |
| 380 | i64 sqlite3BtreeIntegerKey(BtCursor*); |
| 381 | |
| 382 | int sqlite3BtreeCount(sqlite3*, BtCursor*, i64*); |
| 383 | |
| 384 | |
| 385 | void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); |
| 386 | int sqlite3BtreeSchemaLocked(Btree *pBtree); |
| 387 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 388 | int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); |
| 389 | #endif |
| 390 | void sqlite3BtreeCursorPin(BtCursor*); |
| 391 | void sqlite3BtreeCursorUnpin(BtCursor*); |
| 392 | |
| 393 | int sqlite3BtreeCursorHasMoved(BtCursor*); |
| 394 | int sqlite3BtreeCursorRestore(BtCursor*, int*); |
| 395 | |
| 396 | #ifdef SQLITE_TEST |
| 397 | int sqlite3BtreeCursorInfo(BtCursor*, int*, int); |
| 398 | void sqlite3BtreeCursorList(Btree*); |
| 399 | #endif |
| 400 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 401 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 402 | ****************************************** |
| 403 | ** Record and Payload handling functions |
| 404 | ****************************************** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 405 | ** |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 406 | ** 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 | ** |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 409 | */ |
| 410 | |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 411 | /* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 412 | #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 413 | #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 414 | #define BTREE_APPEND 0x08 /* Insert is likely an append */ |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 415 | |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 416 | /* 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 |
drh | 89ee229 | 2018-05-07 18:41:19 +0000 | [diff] [blame] | 420 | ** 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. |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 424 | ** |
| 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. |
drh | 89ee229 | 2018-05-07 18:41:19 +0000 | [diff] [blame] | 429 | ** 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 |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 442 | ** |
| 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 | */ |
| 449 | struct 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 */ |
drh | 89ee229 | 2018-05-07 18:41:19 +0000 | [diff] [blame] | 452 | const void *pData; /* Data for tables. */ |
drh | 7a6ea93 | 2017-04-09 19:23:55 +0000 | [diff] [blame] | 453 | sqlite3_value *aMem; /* First of nMem value in the unpacked pKey */ |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 454 | u16 nMem; /* Number of aMem[] value. Might be zero */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 455 | int nData; /* Size of pData. 0 if none. */ |
| 456 | int nZero; /* Extra zero data appended after pData,nData */ |
| 457 | }; |
| 458 | |
| 459 | int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 460 | int flags, int seekResult); |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 461 | |
| 462 | int sqlite3BtreeDelete(BtCursor*, u8 flags); |
| 463 | |
drh | 092457b | 2017-12-29 15:04:49 +0000 | [diff] [blame] | 464 | #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC |
| 465 | i64 sqlite3BtreeOffset(BtCursor*); |
| 466 | #endif |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 467 | int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 468 | const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt); |
| 469 | u32 sqlite3BtreePayloadSize(BtCursor*); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 470 | |
shearer | f311315 | 2020-10-27 11:07:16 +0000 | [diff] [blame] | 471 | /* These functions deal with arbitary-sized binary blobs in a Btree */ |
| 472 | #ifndef SQLITE_OMIT_INCRBLOB |
| 473 | int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*); |
| 474 | int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); |
| 475 | void sqlite3BtreeIncrblobCursor(BtCursor *); |
| 476 | #endif |
| 477 | |
| 478 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 479 | |
| 480 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 481 | ******************** |
| 482 | ** Table functions |
| 483 | ******************** |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 484 | ** |
| 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 | |
| 501 | int sqlite3BtreeDropTable(Btree*, int, int*); |
| 502 | int sqlite3BtreeClearTable(Btree*, int, int*); |
| 503 | int sqlite3BtreeClearTableOfCursor(BtCursor*); |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 504 | |
| 505 | |
| 506 | /* |
shearer | 4598bf8 | 2020-10-27 11:58:37 +0000 | [diff] [blame] | 507 | ********************************* |
| 508 | ** Reading and Writing Metadata |
| 509 | ********************************* |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 510 | ** |
| 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 | |
shearer | fc1070b | 2020-09-25 13:56:31 +0000 | [diff] [blame] | 545 | /* 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 */ |
| 547 | int sqlite3HeaderSizeBtree(void); |
| 548 | |
shearer | 153fa60 | 2020-09-24 06:37:18 +0000 | [diff] [blame] | 549 | /* 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 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 556 | void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); |
| 557 | int 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 . */ |
drh | 53d30dd | 2019-02-04 21:10:24 +0000 | [diff] [blame] | 561 | sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*); |
drh | 144f9ea | 2003-04-16 01:28:16 +0000 | [diff] [blame] | 562 | |
shearer | fc1070b | 2020-09-25 13:56:31 +0000 | [diff] [blame] | 563 | /* 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 */ |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 565 | int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
| 566 | |
shearer | a41cc5c | 2020-10-06 15:48:59 +0000 | [diff] [blame] | 567 | /* Initialize the first page of the database file and return */ |
| 568 | int sqlite3BtreeNewDb(Btree *p); |
| 569 | |
shearer | acc225a | 2020-09-17 15:04:16 +0000 | [diff] [blame] | 570 | |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 571 | #endif /* SQLITE_BTREE_H */ |