btree.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This header file defines the interface that the sqlite B-Tree file
  13. ** subsystem. See comments in the source code for a detailed description
  14. ** of what each interface routine does.
  15. */
  16. #ifndef _BTREE_H_
  17. #define _BTREE_H_
  18. /* TODO: This definition is just included so other modules compile. It
  19. ** needs to be revisited.
  20. */
  21. #define SQLITE_N_BTREE_META 10
  22. /*
  23. ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  24. ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  25. */
  26. #ifndef SQLITE_DEFAULT_AUTOVACUUM
  27. #define SQLITE_DEFAULT_AUTOVACUUM 0
  28. #endif
  29. #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
  30. #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
  31. #define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
  32. /*
  33. ** Forward declarations of structure
  34. */
  35. typedef struct Btree Btree;
  36. typedef struct BtCursor BtCursor;
  37. typedef struct BtShared BtShared;
  38. int sqlite3BtreeOpen(
  39. sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
  40. const char *zFilename, /* Name of database file to open */
  41. sqlite3 *db, /* Associated database connection */
  42. Btree **ppBtree, /* Return open Btree* here */
  43. int flags, /* Flags */
  44. int vfsFlags /* Flags passed through to VFS open */
  45. );
  46. /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  47. ** following values.
  48. **
  49. ** NOTE: These values must match the corresponding PAGER_ values in
  50. ** pager.h.
  51. */
  52. #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
  53. #define BTREE_MEMORY 2 /* This is an in-memory DB */
  54. #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
  55. #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
  56. int sqlite3BtreeClose(Btree*);
  57. int sqlite3BtreeSetCacheSize(Btree*,int);
  58. int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
  59. int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
  60. int sqlite3BtreeSyncDisabled(Btree*);
  61. int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  62. int sqlite3BtreeGetPageSize(Btree*);
  63. int sqlite3BtreeMaxPageCount(Btree*,int);
  64. u32 sqlite3BtreeLastPage(Btree*);
  65. int sqlite3BtreeSecureDelete(Btree*,int);
  66. int sqlite3BtreeGetReserve(Btree*);
  67. #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
  68. int sqlite3BtreeGetReserveNoMutex(Btree *p);
  69. #endif
  70. int sqlite3BtreeSetAutoVacuum(Btree *, int);
  71. int sqlite3BtreeGetAutoVacuum(Btree *);
  72. int sqlite3BtreeBeginTrans(Btree*,int);
  73. int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  74. int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  75. int sqlite3BtreeCommit(Btree*);
  76. int sqlite3BtreeRollback(Btree*,int);
  77. int sqlite3BtreeBeginStmt(Btree*,int);
  78. int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  79. int sqlite3BtreeIsInTrans(Btree*);
  80. int sqlite3BtreeIsInReadTrans(Btree*);
  81. int sqlite3BtreeIsInBackup(Btree*);
  82. void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  83. int sqlite3BtreeSchemaLocked(Btree *pBtree);
  84. int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  85. int sqlite3BtreeSavepoint(Btree *, int, int);
  86. const char *sqlite3BtreeGetFilename(Btree *);
  87. const char *sqlite3BtreeGetJournalname(Btree *);
  88. int sqlite3BtreeCopyFile(Btree *, Btree *);
  89. int sqlite3BtreeIncrVacuum(Btree *);
  90. /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  91. ** of the flags shown below.
  92. **
  93. ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
  94. ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
  95. ** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
  96. ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
  97. ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
  98. ** indices.)
  99. */
  100. #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
  101. #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
  102. int sqlite3BtreeDropTable(Btree*, int, int*);
  103. int sqlite3BtreeClearTable(Btree*, int, int*);
  104. void sqlite3BtreeTripAllCursors(Btree*, int);
  105. void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  106. int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  107. int sqlite3BtreeNewDb(Btree *p);
  108. /*
  109. ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  110. ** should be one of the following values. The integer values are assigned
  111. ** to constants so that the offset of the corresponding field in an
  112. ** SQLite database header may be found using the following formula:
  113. **
  114. ** offset = 36 + (idx * 4)
  115. **
  116. ** For example, the free-page-count field is located at byte offset 36 of
  117. ** the database file header. The incr-vacuum-flag field is located at
  118. ** byte offset 64 (== 36+4*7).
  119. */
  120. #define BTREE_FREE_PAGE_COUNT 0
  121. #define BTREE_SCHEMA_VERSION 1
  122. #define BTREE_FILE_FORMAT 2
  123. #define BTREE_DEFAULT_CACHE_SIZE 3
  124. #define BTREE_LARGEST_ROOT_PAGE 4
  125. #define BTREE_TEXT_ENCODING 5
  126. #define BTREE_USER_VERSION 6
  127. #define BTREE_INCR_VACUUM 7
  128. #define BTREE_APPLICATION_ID 8
  129. /*
  130. ** Values that may be OR'd together to form the second argument of an
  131. ** sqlite3BtreeCursorHints() call.
  132. */
  133. #define BTREE_BULKLOAD 0x00000001
  134. int sqlite3BtreeCursor(
  135. Btree*, /* BTree containing table to open */
  136. int iTable, /* Index of root page */
  137. int wrFlag, /* 1 for writing. 0 for read-only */
  138. struct KeyInfo*, /* First argument to compare function */
  139. BtCursor *pCursor /* Space to write cursor structure */
  140. );
  141. int sqlite3BtreeCursorSize(void);
  142. void sqlite3BtreeCursorZero(BtCursor*);
  143. int sqlite3BtreeCloseCursor(BtCursor*);
  144. int sqlite3BtreeMovetoUnpacked(
  145. BtCursor*,
  146. UnpackedRecord *pUnKey,
  147. i64 intKey,
  148. int bias,
  149. int *pRes
  150. );
  151. int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  152. int sqlite3BtreeDelete(BtCursor*);
  153. int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  154. const void *pData, int nData,
  155. int nZero, int bias, int seekResult);
  156. int sqlite3BtreeFirst(BtCursor*, int *pRes);
  157. int sqlite3BtreeLast(BtCursor*, int *pRes);
  158. int sqlite3BtreeNext(BtCursor*, int *pRes);
  159. int sqlite3BtreeEof(BtCursor*);
  160. int sqlite3BtreePrevious(BtCursor*, int *pRes);
  161. int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  162. int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  163. const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
  164. const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
  165. int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  166. int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  167. void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  168. sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  169. char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  170. struct Pager *sqlite3BtreePager(Btree*);
  171. int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  172. void sqlite3BtreeCacheOverflow(BtCursor *);
  173. void sqlite3BtreeClearCursor(BtCursor *);
  174. int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  175. void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
  176. #ifndef NDEBUG
  177. int sqlite3BtreeCursorIsValid(BtCursor*);
  178. #endif
  179. #ifndef SQLITE_OMIT_BTREECOUNT
  180. int sqlite3BtreeCount(BtCursor *, i64 *);
  181. #endif
  182. #ifdef SQLITE_TEST
  183. int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  184. void sqlite3BtreeCursorList(Btree*);
  185. #endif
  186. #ifndef SQLITE_OMIT_WAL
  187. int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  188. #endif
  189. /*
  190. ** If we are not using shared cache, then there is no need to
  191. ** use mutexes to access the BtShared structures. So make the
  192. ** Enter and Leave procedures no-ops.
  193. */
  194. #ifndef SQLITE_OMIT_SHARED_CACHE
  195. void sqlite3BtreeEnter(Btree*);
  196. void sqlite3BtreeEnterAll(sqlite3*);
  197. #else
  198. # define sqlite3BtreeEnter(X)
  199. # define sqlite3BtreeEnterAll(X)
  200. #endif
  201. #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  202. int sqlite3BtreeSharable(Btree*);
  203. void sqlite3BtreeLeave(Btree*);
  204. void sqlite3BtreeEnterCursor(BtCursor*);
  205. void sqlite3BtreeLeaveCursor(BtCursor*);
  206. void sqlite3BtreeLeaveAll(sqlite3*);
  207. #ifndef NDEBUG
  208. /* These routines are used inside assert() statements only. */
  209. int sqlite3BtreeHoldsMutex(Btree*);
  210. int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  211. int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  212. #endif
  213. #else
  214. # define sqlite3BtreeSharable(X) 0
  215. # define sqlite3BtreeLeave(X)
  216. # define sqlite3BtreeEnterCursor(X)
  217. # define sqlite3BtreeLeaveCursor(X)
  218. # define sqlite3BtreeLeaveAll(X)
  219. # define sqlite3BtreeHoldsMutex(X) 1
  220. # define sqlite3BtreeHoldsAllMutexes(X) 1
  221. # define sqlite3SchemaMutexHeld(X,Y,Z) 1
  222. #endif
  223. #endif /* _BTREE_H_ */