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 | ************************************************************************* |
| 12 | ** This header file defines the interface that the sqlite B-Tree file |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 13 | ** subsystem. See comments in the source code for a detailed description |
| 14 | ** of what each interface routine does. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 15 | */ |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 16 | #ifndef SQLITE_BTREE_H |
| 17 | #define SQLITE_BTREE_H |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 18 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 19 | /* TODO: This definition is just included so other modules compile. It |
| 20 | ** needs to be revisited. |
| 21 | */ |
drh | 9161856 | 2014-12-19 19:28:02 +0000 | [diff] [blame] | 22 | #define SQLITE_N_BTREE_META 16 |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 23 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 24 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 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 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 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 | |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 36 | /* |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 37 | ** Forward declarations of structure |
| 38 | */ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 39 | typedef struct Btree Btree; |
| 40 | typedef struct BtCursor BtCursor; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 41 | typedef struct BtShared BtShared; |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 42 | typedef struct BtreePayload BtreePayload; |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 43 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 44 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 45 | int sqlite3BtreeOpen( |
dan | 3a6d8ae | 2011-04-23 15:54:54 +0000 | [diff] [blame] | 46 | sqlite3_vfs *pVfs, /* VFS to use with this b-tree */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 47 | const char *zFilename, /* Name of database file to open */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 48 | sqlite3 *db, /* Associated database connection */ |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 49 | Btree **ppBtree, /* Return open Btree* here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 50 | int flags, /* Flags */ |
| 51 | int vfsFlags /* Flags passed through to VFS open */ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 52 | ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 53 | |
| 54 | /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the |
| 55 | ** following values. |
drh | 7bec505 | 2005-02-06 02:45:41 +0000 | [diff] [blame] | 56 | ** |
| 57 | ** NOTE: These values must match the corresponding PAGER_ values in |
| 58 | ** pager.h. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 59 | */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 60 | #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ |
drh | 33f111d | 2012-01-17 15:29:14 +0000 | [diff] [blame] | 61 | #define BTREE_MEMORY 2 /* This is an in-memory DB */ |
| 62 | #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */ |
| 63 | #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 64 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 65 | int sqlite3BtreeClose(Btree*); |
| 66 | int sqlite3BtreeSetCacheSize(Btree*,int); |
drh | 9b0cf34 | 2015-11-12 14:57:19 +0000 | [diff] [blame] | 67 | int sqlite3BtreeSetSpillSize(Btree*,int); |
drh | 18c7e40 | 2014-03-14 11:46:10 +0000 | [diff] [blame] | 68 | #if SQLITE_MAX_MMAP_SIZE>0 |
| 69 | int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64); |
| 70 | #endif |
drh | 40c3941 | 2013-08-16 20:42:20 +0000 | [diff] [blame] | 71 | int sqlite3BtreeSetPagerFlags(Btree*,unsigned); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 72 | int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 73 | int sqlite3BtreeGetPageSize(Btree*); |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 74 | int sqlite3BtreeMaxPageCount(Btree*,int); |
drh | b129915 | 2010-03-30 22:58:33 +0000 | [diff] [blame] | 75 | u32 sqlite3BtreeLastPage(Btree*); |
drh | 5b47efa | 2010-02-12 18:18:39 +0000 | [diff] [blame] | 76 | int sqlite3BtreeSecureDelete(Btree*,int); |
drh | ad0961b | 2015-02-21 00:19:25 +0000 | [diff] [blame] | 77 | int sqlite3BtreeGetOptimalReserve(Btree*); |
dan | 0094f37 | 2012-09-28 20:23:42 +0000 | [diff] [blame] | 78 | int sqlite3BtreeGetReserveNoMutex(Btree *p); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 79 | int sqlite3BtreeSetAutoVacuum(Btree *, int); |
| 80 | int sqlite3BtreeGetAutoVacuum(Btree *); |
danielk1977 | 40b38dc | 2004-06-26 08:38:24 +0000 | [diff] [blame] | 81 | int sqlite3BtreeBeginTrans(Btree*,int); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 82 | int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); |
dan | 60939d0 | 2011-03-29 15:40:55 +0000 | [diff] [blame] | 83 | int sqlite3BtreeCommitPhaseTwo(Btree*, int); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 84 | int sqlite3BtreeCommit(Btree*); |
drh | 47b7fc7 | 2014-11-11 01:33:57 +0000 | [diff] [blame] | 85 | int sqlite3BtreeRollback(Btree*,int,int); |
danielk1977 | bd43455 | 2009-03-18 10:33:00 +0000 | [diff] [blame] | 86 | int sqlite3BtreeBeginStmt(Btree*,int); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 87 | int sqlite3BtreeCreateTable(Btree*, int*, int flags); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 88 | int sqlite3BtreeIsInTrans(Btree*); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 89 | int sqlite3BtreeIsInReadTrans(Btree*); |
danielk1977 | 0410302 | 2009-02-03 16:51:24 +0000 | [diff] [blame] | 90 | int sqlite3BtreeIsInBackup(Btree*); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 91 | void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 92 | int sqlite3BtreeSchemaLocked(Btree *pBtree); |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 93 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 94 | int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 95 | #endif |
danielk1977 | fd7f045 | 2008-12-17 17:30:26 +0000 | [diff] [blame] | 96 | int sqlite3BtreeSavepoint(Btree *, int, int); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 97 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 98 | const char *sqlite3BtreeGetFilename(Btree *); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 99 | const char *sqlite3BtreeGetJournalname(Btree *); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 100 | int sqlite3BtreeCopyFile(Btree *, Btree *); |
| 101 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 102 | int sqlite3BtreeIncrVacuum(Btree *); |
| 103 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 104 | /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 105 | ** of the flags shown below. |
| 106 | ** |
| 107 | ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. |
| 108 | ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data |
| 109 | ** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With |
| 110 | ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored |
| 111 | ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL |
| 112 | ** indices.) |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 113 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 114 | #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ |
drh | d4187c7 | 2010-08-30 22:15:45 +0000 | [diff] [blame] | 115 | #define BTREE_BLOBKEY 2 /* Table has keys only - no data */ |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 116 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 117 | int sqlite3BtreeDropTable(Btree*, int, int*); |
danielk1977 | c7af484 | 2008-10-27 13:59:33 +0000 | [diff] [blame] | 118 | int sqlite3BtreeClearTable(Btree*, int, int*); |
drh | 079a307 | 2014-03-19 14:10:55 +0000 | [diff] [blame] | 119 | int sqlite3BtreeClearTableOfCursor(BtCursor*); |
dan | 8023104 | 2014-11-12 14:56:02 +0000 | [diff] [blame] | 120 | int sqlite3BtreeTripAllCursors(Btree*, int, int); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 121 | |
danielk1977 | 602b466 | 2009-07-02 07:47:33 +0000 | [diff] [blame] | 122 | void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 123 | int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 124 | |
dan | b483eba | 2012-10-13 19:58:11 +0000 | [diff] [blame] | 125 | int sqlite3BtreeNewDb(Btree *p); |
| 126 | |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 127 | /* |
| 128 | ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta |
| 129 | ** should be one of the following values. The integer values are assigned |
| 130 | ** to constants so that the offset of the corresponding field in an |
| 131 | ** SQLite database header may be found using the following formula: |
| 132 | ** |
| 133 | ** offset = 36 + (idx * 4) |
| 134 | ** |
| 135 | ** For example, the free-page-count field is located at byte offset 36 of |
| 136 | ** the database file header. The incr-vacuum-flag field is located at |
| 137 | ** byte offset 64 (== 36+4*7). |
drh | 9161856 | 2014-12-19 19:28:02 +0000 | [diff] [blame] | 138 | ** |
| 139 | ** The BTREE_DATA_VERSION value is not really a value stored in the header. |
| 140 | ** It is a read-only number computed by the pager. But we merge it with |
| 141 | ** the header value access routines since its access pattern is the same. |
| 142 | ** Call it a "virtual meta value". |
danielk1977 | 0d19f7a | 2009-06-03 11:25:07 +0000 | [diff] [blame] | 143 | */ |
| 144 | #define BTREE_FREE_PAGE_COUNT 0 |
| 145 | #define BTREE_SCHEMA_VERSION 1 |
| 146 | #define BTREE_FILE_FORMAT 2 |
| 147 | #define BTREE_DEFAULT_CACHE_SIZE 3 |
| 148 | #define BTREE_LARGEST_ROOT_PAGE 4 |
| 149 | #define BTREE_TEXT_ENCODING 5 |
| 150 | #define BTREE_USER_VERSION 6 |
| 151 | #define BTREE_INCR_VACUUM 7 |
drh | 4ee09b4 | 2013-05-01 19:49:27 +0000 | [diff] [blame] | 152 | #define BTREE_APPLICATION_ID 8 |
drh | 9161856 | 2014-12-19 19:28:02 +0000 | [diff] [blame] | 153 | #define BTREE_DATA_VERSION 15 /* A virtual meta-value */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 154 | |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 155 | /* |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 156 | ** Kinds of hints that can be passed into the sqlite3BtreeCursorHint() |
| 157 | ** interface. |
| 158 | ** |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 159 | ** BTREE_HINT_RANGE (arguments: Expr*, Mem*) |
| 160 | ** |
| 161 | ** The first argument is an Expr* (which is guaranteed to be constant for |
| 162 | ** the lifetime of the cursor) that defines constraints on which rows |
| 163 | ** might be fetched with this cursor. The Expr* tree may contain |
| 164 | ** TK_REGISTER nodes that refer to values stored in the array of registers |
| 165 | ** passed as the second parameter. In other words, if Expr.op==TK_REGISTER |
| 166 | ** then the value of the node is the value in Mem[pExpr.iTable]. Any |
| 167 | ** TK_COLUMN node in the expression tree refers to the Expr.iColumn-th |
| 168 | ** column of the b-tree of the cursor. The Expr tree will not contain |
| 169 | ** any function calls nor subqueries nor references to b-trees other than |
| 170 | ** the cursor being hinted. |
| 171 | ** |
| 172 | ** The design of the _RANGE hint is aid b-tree implementations that try |
| 173 | ** to prefetch content from remote machines - to provide those |
| 174 | ** implementations with limits on what needs to be prefetched and thereby |
| 175 | ** reduce network bandwidth. |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 176 | ** |
| 177 | ** Note that BTREE_HINT_FLAGS with BTREE_BULKLOAD is the only hint used by |
| 178 | ** standard SQLite. The other hints are provided for extentions that use |
| 179 | ** the SQLite parser and code generator but substitute their own storage |
| 180 | ** engine. |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 181 | */ |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 182 | #define BTREE_HINT_RANGE 0 /* Range constraints on queries */ |
drh | 0df5701 | 2015-08-14 15:05:55 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | ** Values that may be OR'd together to form the argument to the |
| 186 | ** BTREE_HINT_FLAGS hint for sqlite3BtreeCursorHint(): |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 187 | ** |
| 188 | ** The BTREE_BULKLOAD flag is set on index cursors when the index is going |
| 189 | ** to be filled with content that is already in sorted order. |
| 190 | ** |
| 191 | ** The BTREE_SEEK_EQ flag is set on cursors that will get OP_SeekGE or |
| 192 | ** OP_SeekLE opcodes for a range search, but where the range of entries |
| 193 | ** selected will all have the same key. In other words, the cursor will |
| 194 | ** be used only for equality key searches. |
| 195 | ** |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 196 | */ |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 197 | #define BTREE_BULKLOAD 0x00000001 /* Used to full index in sorted order */ |
| 198 | #define BTREE_SEEK_EQ 0x00000002 /* EQ seeks only - no range seeks */ |
dan | 428c218 | 2012-08-06 18:50:11 +0000 | [diff] [blame] | 199 | |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 200 | /* |
| 201 | ** Flags passed as the third argument to sqlite3BtreeCursor(). |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 202 | ** |
| 203 | ** For read-only cursors the wrFlag argument is always zero. For read-write |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 204 | ** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just |
| 205 | ** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 206 | ** only be used by SQLite for the following: |
| 207 | ** |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 208 | ** * to seek to and then delete specific entries, and/or |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 209 | ** |
| 210 | ** * to read values that will be used to create keys that other |
| 211 | ** BTREE_FORDELETE cursors will seek to and delete. |
drh | 9c0c57a | 2016-01-21 15:55:37 +0000 | [diff] [blame] | 212 | ** |
| 213 | ** The BTREE_FORDELETE flag is an optimization hint. It is not used by |
| 214 | ** by this, the native b-tree engine of SQLite, but it is available to |
| 215 | ** alternative storage engines that might be substituted in place of this |
| 216 | ** b-tree system. For alternative storage engines in which a delete of |
| 217 | ** the main table row automatically deletes corresponding index rows, |
| 218 | ** the FORDELETE flag hint allows those alternative storage engines to |
| 219 | ** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK |
| 220 | ** and DELETE operations as no-ops, and any READ operation against a |
| 221 | ** FORDELETE cursor may return a null row: 0x01 0x00. |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 222 | */ |
dan | 2b4e952 | 2015-10-23 11:50:23 +0000 | [diff] [blame] | 223 | #define BTREE_WRCSR 0x00000004 /* read-write cursor */ |
| 224 | #define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */ |
dan | fd261ec | 2015-10-22 20:54:33 +0000 | [diff] [blame] | 225 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 226 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 227 | Btree*, /* BTree containing table to open */ |
| 228 | int iTable, /* Index of root page */ |
| 229 | int wrFlag, /* 1 for writing. 0 for read-only */ |
| 230 | struct KeyInfo*, /* First argument to compare function */ |
| 231 | BtCursor *pCursor /* Space to write cursor structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 232 | ); |
drh | 59020f3 | 2008-04-26 13:39:46 +0000 | [diff] [blame] | 233 | int sqlite3BtreeCursorSize(void); |
drh | f25a507 | 2009-11-18 23:01:25 +0000 | [diff] [blame] | 234 | void sqlite3BtreeCursorZero(BtCursor*); |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 235 | void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned); |
| 236 | #ifdef SQLITE_ENABLE_CURSOR_HINTS |
drh | 0403cb3 | 2015-08-14 23:57:04 +0000 | [diff] [blame] | 237 | void sqlite3BtreeCursorHint(BtCursor*, int, ...); |
drh | f7854c7 | 2015-10-27 13:24:37 +0000 | [diff] [blame] | 238 | #endif |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 239 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 240 | int sqlite3BtreeCloseCursor(BtCursor*); |
drh | e63d999 | 2008-08-13 19:11:48 +0000 | [diff] [blame] | 241 | int sqlite3BtreeMovetoUnpacked( |
| 242 | BtCursor*, |
| 243 | UnpackedRecord *pUnKey, |
| 244 | i64 intKey, |
| 245 | int bias, |
| 246 | int *pRes |
| 247 | ); |
drh | 6848dad | 2014-08-22 23:33:03 +0000 | [diff] [blame] | 248 | int sqlite3BtreeCursorHasMoved(BtCursor*); |
| 249 | int sqlite3BtreeCursorRestore(BtCursor*, int*); |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 250 | int sqlite3BtreeDelete(BtCursor*, u8 flags); |
| 251 | |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 252 | /* Allowed flags for sqlite3BtreeDelete() and sqlite3BtreeInsert() */ |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 253 | #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */ |
drh | def19e3 | 2016-01-27 16:26:25 +0000 | [diff] [blame] | 254 | #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */ |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 255 | #define BTREE_APPEND 0x08 /* Insert is likely an append */ |
drh | e807bdb | 2016-01-21 17:06:33 +0000 | [diff] [blame] | 256 | |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 257 | /* An instance of the BtreePayload object describes the content of a single |
| 258 | ** entry in either an index or table btree. |
| 259 | ** |
| 260 | ** Index btrees (used for indexes and also WITHOUT ROWID tables) contain |
| 261 | ** an arbitrary key and no data. These btrees have pKey,nKey set to their |
| 262 | ** key and pData,nData,nZero set to zero. |
| 263 | ** |
| 264 | ** Table btrees (used for rowid tables) contain an integer rowid used as |
| 265 | ** the key and passed in the nKey field. The pKey field is zero. |
| 266 | ** pData,nData hold the content of the new entry. nZero extra zero bytes |
| 267 | ** are appended to the end of the content when constructing the entry. |
| 268 | ** |
| 269 | ** This object is used to pass information into sqlite3BtreeInsert(). The |
| 270 | ** same information used to be passed as five separate parameters. But placing |
| 271 | ** the information into this object helps to keep the interface more |
| 272 | ** organized and understandable, and it also helps the resulting code to |
| 273 | ** run a little faster by using fewer registers for parameter passing. |
| 274 | */ |
| 275 | struct BtreePayload { |
| 276 | const void *pKey; /* Key content for indexes. NULL for tables */ |
| 277 | sqlite3_int64 nKey; /* Size of pKey for indexes. PRIMARY KEY for tabs */ |
| 278 | const void *pData; /* Data for tables. NULL for indexes */ |
drh | 9b4eaeb | 2016-11-09 00:10:33 +0000 | [diff] [blame] | 279 | struct Mem *aMem; /* First of nMem value in the unpacked pKey */ |
| 280 | u16 nMem; /* Number of aMem[] value. Might be zero */ |
drh | 8eeb446 | 2016-05-21 20:03:42 +0000 | [diff] [blame] | 281 | int nData; /* Size of pData. 0 if none. */ |
| 282 | int nZero; /* Extra zero data appended after pData,nData */ |
| 283 | }; |
| 284 | |
| 285 | int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload, |
dan | f91c131 | 2017-01-10 20:04:38 +0000 | [diff] [blame] | 286 | int flags, int seekResult); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 287 | int sqlite3BtreeFirst(BtCursor*, int *pRes); |
| 288 | int sqlite3BtreeLast(BtCursor*, int *pRes); |
| 289 | int sqlite3BtreeNext(BtCursor*, int *pRes); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 290 | int sqlite3BtreeEof(BtCursor*); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 291 | int sqlite3BtreePrevious(BtCursor*, int *pRes); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 292 | i64 sqlite3BtreeIntegerKey(BtCursor*); |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 293 | int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*); |
drh | a7c90c4 | 2016-06-04 20:37:10 +0000 | [diff] [blame] | 294 | const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt); |
| 295 | u32 sqlite3BtreePayloadSize(BtCursor*); |
drh | 144f9ea | 2003-04-16 01:28:16 +0000 | [diff] [blame] | 296 | |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 297 | char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 298 | struct Pager *sqlite3BtreePager(Btree*); |
| 299 | |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 300 | #ifndef SQLITE_OMIT_INCRBLOB |
drh | cb3cabd | 2016-11-25 19:18:28 +0000 | [diff] [blame] | 301 | int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 302 | int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); |
dan | 5a500af | 2014-03-11 20:33:04 +0000 | [diff] [blame] | 303 | void sqlite3BtreeIncrblobCursor(BtCursor *); |
drh | 2eb22af | 2016-09-10 19:51:40 +0000 | [diff] [blame] | 304 | #endif |
danielk1977 | be51a65 | 2008-10-08 17:58:48 +0000 | [diff] [blame] | 305 | void sqlite3BtreeClearCursor(BtCursor *); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 306 | int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); |
drh | e0997b3 | 2015-03-20 14:57:50 +0000 | [diff] [blame] | 307 | int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask); |
drh | 781597f | 2014-05-21 08:21:07 +0000 | [diff] [blame] | 308 | int sqlite3BtreeIsReadonly(Btree *pBt); |
drh | def6889 | 2014-11-04 12:11:23 +0000 | [diff] [blame] | 309 | int sqlite3HeaderSizeBtree(void); |
dan | e04dc88 | 2010-04-20 18:53:15 +0000 | [diff] [blame] | 310 | |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 311 | #ifndef NDEBUG |
| 312 | int sqlite3BtreeCursorIsValid(BtCursor*); |
| 313 | #endif |
drh | d6ef5af | 2016-11-15 04:00:24 +0000 | [diff] [blame] | 314 | int sqlite3BtreeCursorIsValidNN(BtCursor*); |
drh | ea8ffdf | 2009-07-22 00:35:23 +0000 | [diff] [blame] | 315 | |
danielk1977 | a553316 | 2009-02-24 10:01:51 +0000 | [diff] [blame] | 316 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 317 | int sqlite3BtreeCount(BtCursor *, i64 *); |
| 318 | #endif |
| 319 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 320 | #ifdef SQLITE_TEST |
drh | 3e27c02 | 2004-07-23 00:01:38 +0000 | [diff] [blame] | 321 | int sqlite3BtreeCursorInfo(BtCursor*, int*, int); |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 322 | void sqlite3BtreeCursorList(Btree*); |
danielk1977 | b5402fb | 2005-01-12 07:15:04 +0000 | [diff] [blame] | 323 | #endif |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 324 | |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 325 | #ifndef SQLITE_OMIT_WAL |
dan | cdc1f04 | 2010-11-18 12:11:05 +0000 | [diff] [blame] | 326 | int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); |
dan | a550f2d | 2010-08-02 10:47:05 +0000 | [diff] [blame] | 327 | #endif |
| 328 | |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 329 | /* |
| 330 | ** If we are not using shared cache, then there is no need to |
| 331 | ** use mutexes to access the BtShared structures. So make the |
| 332 | ** Enter and Leave procedures no-ops. |
| 333 | */ |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 334 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 335 | void sqlite3BtreeEnter(Btree*); |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 336 | void sqlite3BtreeEnterAll(sqlite3*); |
dan | 20d876f | 2016-01-07 16:06:22 +0000 | [diff] [blame] | 337 | int sqlite3BtreeSharable(Btree*); |
| 338 | void sqlite3BtreeEnterCursor(BtCursor*); |
dan | 272989b | 2016-07-06 10:12:02 +0000 | [diff] [blame] | 339 | int sqlite3BtreeConnectionCount(Btree*); |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 340 | #else |
| 341 | # define sqlite3BtreeEnter(X) |
| 342 | # define sqlite3BtreeEnterAll(X) |
dan | 20d876f | 2016-01-07 16:06:22 +0000 | [diff] [blame] | 343 | # define sqlite3BtreeSharable(X) 0 |
| 344 | # define sqlite3BtreeEnterCursor(X) |
dan | 272989b | 2016-07-06 10:12:02 +0000 | [diff] [blame] | 345 | # define sqlite3BtreeConnectionCount(X) 1 |
shane | a6f6d20 | 2008-05-29 03:01:23 +0000 | [diff] [blame] | 346 | #endif |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 347 | |
| 348 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE |
| 349 | void sqlite3BtreeLeave(Btree*); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 350 | void sqlite3BtreeLeaveCursor(BtCursor*); |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 351 | void sqlite3BtreeLeaveAll(sqlite3*); |
shane | a6f6d20 | 2008-05-29 03:01:23 +0000 | [diff] [blame] | 352 | #ifndef NDEBUG |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 353 | /* These routines are used inside assert() statements only. */ |
| 354 | int sqlite3BtreeHoldsMutex(Btree*); |
| 355 | int sqlite3BtreeHoldsAllMutexes(sqlite3*); |
drh | 2120608 | 2011-04-04 18:22:02 +0000 | [diff] [blame] | 356 | int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); |
shane | a6f6d20 | 2008-05-29 03:01:23 +0000 | [diff] [blame] | 357 | #endif |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 358 | #else |
| 359 | |
| 360 | # define sqlite3BtreeLeave(X) |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 361 | # define sqlite3BtreeLeaveCursor(X) |
drh | b1ab8ea | 2007-08-29 00:33:07 +0000 | [diff] [blame] | 362 | # define sqlite3BtreeLeaveAll(X) |
danielk1977 | f7590db | 2009-04-10 12:55:16 +0000 | [diff] [blame] | 363 | |
| 364 | # define sqlite3BtreeHoldsMutex(X) 1 |
| 365 | # define sqlite3BtreeHoldsAllMutexes(X) 1 |
drh | e54e051 | 2011-04-05 17:31:56 +0000 | [diff] [blame] | 366 | # define sqlite3SchemaMutexHeld(X,Y,Z) 1 |
drh | 900b31e | 2007-08-28 02:27:51 +0000 | [diff] [blame] | 367 | #endif |
| 368 | |
| 369 | |
drh | 43f58d6 | 2016-07-09 16:14:45 +0000 | [diff] [blame] | 370 | #endif /* SQLITE_BTREE_H */ |