lsm.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. ** 2011-08-10
  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. **
  13. ** This file defines the LSM API.
  14. */
  15. #ifndef _LSM_H
  16. #define _LSM_H
  17. #include <stddef.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /*
  22. ** Opaque handle types.
  23. */
  24. typedef struct lsm_compress lsm_compress; /* Compression library functions */
  25. typedef struct lsm_compress_factory lsm_compress_factory;
  26. typedef struct lsm_cursor lsm_cursor; /* Database cursor handle */
  27. typedef struct lsm_db lsm_db; /* Database connection handle */
  28. typedef struct lsm_env lsm_env; /* Runtime environment */
  29. typedef struct lsm_file lsm_file; /* OS file handle */
  30. typedef struct lsm_mutex lsm_mutex; /* Mutex handle */
  31. /* 64-bit integer type used for file offsets. */
  32. typedef long long int lsm_i64; /* 64-bit signed integer type */
  33. /* Candidate values for the 3rd argument to lsm_env.xLock() */
  34. #define LSM_LOCK_UNLOCK 0
  35. #define LSM_LOCK_SHARED 1
  36. #define LSM_LOCK_EXCL 2
  37. /* Flags for lsm_env.xOpen() */
  38. #define LSM_OPEN_READONLY 0x0001
  39. /*
  40. ** CAPI: Database Runtime Environment
  41. **
  42. ** Run-time environment used by LSM
  43. */
  44. struct lsm_env {
  45. int nByte; /* Size of this structure in bytes */
  46. int iVersion; /* Version number of this structure (1) */
  47. /****** file i/o ***********************************************/
  48. void *pVfsCtx;
  49. int (*xFullpath)(lsm_env*, const char *, char *, int *);
  50. int (*xOpen)(lsm_env*, const char *, int flags, lsm_file **);
  51. int (*xRead)(lsm_file *, lsm_i64, void *, int);
  52. int (*xWrite)(lsm_file *, lsm_i64, void *, int);
  53. int (*xTruncate)(lsm_file *, lsm_i64);
  54. int (*xSync)(lsm_file *);
  55. int (*xSectorSize)(lsm_file *);
  56. int (*xRemap)(lsm_file *, lsm_i64, void **, lsm_i64*);
  57. int (*xFileid)(lsm_file *, void *pBuf, int *pnBuf);
  58. int (*xClose)(lsm_file *);
  59. int (*xUnlink)(lsm_env*, const char *);
  60. int (*xLock)(lsm_file*, int, int);
  61. int (*xTestLock)(lsm_file*, int, int, int);
  62. int (*xShmMap)(lsm_file*, int, int, void **);
  63. void (*xShmBarrier)(void);
  64. int (*xShmUnmap)(lsm_file*, int);
  65. /****** memory allocation ****************************************/
  66. void *pMemCtx;
  67. void *(*xMalloc)(lsm_env*, size_t); /* malloc(3) function */
  68. void *(*xRealloc)(lsm_env*, void *, size_t); /* realloc(3) function */
  69. void (*xFree)(lsm_env*, void *); /* free(3) function */
  70. size_t (*xSize)(lsm_env*, void *); /* xSize function */
  71. /****** mutexes ****************************************************/
  72. void *pMutexCtx;
  73. int (*xMutexStatic)(lsm_env*,int,lsm_mutex**); /* Obtain a static mutex */
  74. int (*xMutexNew)(lsm_env*, lsm_mutex**); /* Get a new dynamic mutex */
  75. void (*xMutexDel)(lsm_mutex *); /* Delete an allocated mutex */
  76. void (*xMutexEnter)(lsm_mutex *); /* Grab a mutex */
  77. int (*xMutexTry)(lsm_mutex *); /* Attempt to obtain a mutex */
  78. void (*xMutexLeave)(lsm_mutex *); /* Leave a mutex */
  79. int (*xMutexHeld)(lsm_mutex *); /* Return true if mutex is held */
  80. int (*xMutexNotHeld)(lsm_mutex *); /* Return true if mutex not held */
  81. /****** other ****************************************************/
  82. int (*xSleep)(lsm_env*, int microseconds);
  83. /* New fields may be added in future releases, in which case the
  84. ** iVersion value will increase. */
  85. };
  86. /*
  87. ** Values that may be passed as the second argument to xMutexStatic.
  88. */
  89. #define LSM_MUTEX_GLOBAL 1
  90. #define LSM_MUTEX_HEAP 2
  91. /*
  92. ** CAPI: LSM Error Codes
  93. */
  94. #define LSM_OK 0
  95. #define LSM_ERROR 1
  96. #define LSM_BUSY 5
  97. #define LSM_NOMEM 7
  98. #define LSM_READONLY 8
  99. #define LSM_IOERR 10
  100. #define LSM_CORRUPT 11
  101. #define LSM_FULL 13
  102. #define LSM_CANTOPEN 14
  103. #define LSM_PROTOCOL 15
  104. #define LSM_MISUSE 21
  105. #define LSM_MISMATCH 50
  106. #define LSM_IOERR_NOENT (LSM_IOERR | (1<<8))
  107. /*
  108. ** CAPI: Creating and Destroying Database Connection Handles
  109. **
  110. ** Open and close a database connection handle.
  111. */
  112. int lsm_new(lsm_env*, lsm_db **ppDb);
  113. int lsm_close(lsm_db *pDb);
  114. /*
  115. ** CAPI: Connecting to a Database
  116. */
  117. int lsm_open(lsm_db *pDb, const char *zFilename);
  118. /*
  119. ** CAPI: Obtaining pointers to database environments
  120. **
  121. ** Return a pointer to the environment used by the database connection
  122. ** passed as the first argument. Assuming the argument is valid, this
  123. ** function always returns a valid environment pointer - it cannot fail.
  124. */
  125. lsm_env *lsm_get_env(lsm_db *pDb);
  126. /*
  127. ** The lsm_default_env() function returns a pointer to the default LSM
  128. ** environment for the current platform.
  129. */
  130. lsm_env *lsm_default_env(void);
  131. /*
  132. ** CAPI: Configuring a database connection.
  133. **
  134. ** The lsm_config() function is used to configure a database connection.
  135. */
  136. int lsm_config(lsm_db *, int, ...);
  137. /*
  138. ** The following values may be passed as the second argument to lsm_config().
  139. **
  140. ** LSM_CONFIG_AUTOFLUSH:
  141. ** A read/write integer parameter.
  142. **
  143. ** This value determines the amount of data allowed to accumulate in a
  144. ** live in-memory tree before it is marked as old. After committing a
  145. ** transaction, a connection checks if the size of the live in-memory tree,
  146. ** including data structure overhead, is greater than the value of this
  147. ** option in KB. If it is, and there is not already an old in-memory tree,
  148. ** the live in-memory tree is marked as old.
  149. **
  150. ** The maximum allowable value is 1048576 (1GB). There is no minimum
  151. ** value. If this parameter is set to zero, then an attempt is made to
  152. ** mark the live in-memory tree as old after each transaction is committed.
  153. **
  154. ** The default value is 1024 (1MB).
  155. **
  156. ** LSM_CONFIG_PAGE_SIZE:
  157. ** A read/write integer parameter. This parameter may only be set before
  158. ** lsm_open() has been called.
  159. **
  160. ** LSM_CONFIG_BLOCK_SIZE:
  161. ** A read/write integer parameter.
  162. **
  163. ** This parameter may only be set before lsm_open() has been called. It
  164. ** must be set to a power of two between 64 and 65536, inclusive (block
  165. ** sizes between 64KB and 64MB).
  166. **
  167. ** If the connection creates a new database, the block size of the new
  168. ** database is set to the value of this option in KB. After lsm_open()
  169. ** has been called, querying this parameter returns the actual block
  170. ** size of the opened database.
  171. **
  172. ** The default value is 1024 (1MB blocks).
  173. **
  174. ** LSM_CONFIG_SAFETY:
  175. ** A read/write integer parameter. Valid values are 0, 1 (the default)
  176. ** and 2. This parameter determines how robust the database is in the
  177. ** face of a system crash (e.g. a power failure or operating system
  178. ** crash). As follows:
  179. **
  180. ** 0 (off): No robustness. A system crash may corrupt the database.
  181. **
  182. ** 1 (normal): Some robustness. A system crash may not corrupt the
  183. ** database file, but recently committed transactions may
  184. ** be lost following recovery.
  185. **
  186. ** 2 (full): Full robustness. A system crash may not corrupt the
  187. ** database file. Following recovery the database file
  188. ** contains all successfully committed transactions.
  189. **
  190. ** LSM_CONFIG_AUTOWORK:
  191. ** A read/write integer parameter.
  192. **
  193. ** LSM_CONFIG_AUTOCHECKPOINT:
  194. ** A read/write integer parameter.
  195. **
  196. ** If this option is set to non-zero value N, then a checkpoint is
  197. ** automatically attempted after each N KB of data have been written to
  198. ** the database file.
  199. **
  200. ** The amount of uncheckpointed data already written to the database file
  201. ** is a global parameter. After performing database work (writing to the
  202. ** database file), the process checks if the total amount of uncheckpointed
  203. ** data exceeds the value of this paramter. If so, a checkpoint is performed.
  204. ** This means that this option may cause the connection to perform a
  205. ** checkpoint even if the current connection has itself written very little
  206. ** data into the database file.
  207. **
  208. ** The default value is 2048 (checkpoint every 2MB).
  209. **
  210. ** LSM_CONFIG_MMAP:
  211. ** A read/write integer parameter. If this value is set to 0, then the
  212. ** database file is accessed using ordinary read/write IO functions. Or,
  213. ** if it is set to 1, then the database file is memory mapped and accessed
  214. ** that way. If this parameter is set to any value N greater than 1, then
  215. ** up to the first N KB of the file are memory mapped, and any remainder
  216. ** accessed using read/write IO.
  217. **
  218. ** The default value is 1 on 64-bit platforms and 32768 on 32-bit platforms.
  219. **
  220. **
  221. ** LSM_CONFIG_USE_LOG:
  222. ** A read/write boolean parameter. True (the default) to use the log
  223. ** file normally. False otherwise.
  224. **
  225. ** LSM_CONFIG_AUTOMERGE:
  226. ** A read/write integer parameter. The minimum number of segments to
  227. ** merge together at a time. Default value 4.
  228. **
  229. ** LSM_CONFIG_MAX_FREELIST:
  230. ** A read/write integer parameter. The maximum number of free-list
  231. ** entries that are stored in a database checkpoint (the others are
  232. ** stored elsewhere in the database).
  233. **
  234. ** There is no reason for an application to configure or query this
  235. ** parameter. It is only present because configuring a small value
  236. ** makes certain parts of the lsm code easier to test.
  237. **
  238. ** LSM_CONFIG_MULTIPLE_PROCESSES:
  239. ** A read/write boolean parameter. This parameter may only be set before
  240. ** lsm_open() has been called. If true, the library uses shared-memory
  241. ** and posix advisory locks to co-ordinate access by clients from within
  242. ** multiple processes. Otherwise, if false, all database clients must be
  243. ** located in the same process. The default value is true.
  244. **
  245. ** LSM_CONFIG_SET_COMPRESSION:
  246. ** Set the compression methods used to compress and decompress database
  247. ** content. The argument to this option should be a pointer to a structure
  248. ** of type lsm_compress. The lsm_config() method takes a copy of the
  249. ** structures contents.
  250. **
  251. ** This option may only be used before lsm_open() is called. Invoking it
  252. ** after lsm_open() has been called results in an LSM_MISUSE error.
  253. **
  254. ** LSM_CONFIG_GET_COMPRESSION:
  255. ** Query the compression methods used to compress and decompress database
  256. ** content.
  257. **
  258. ** LSM_CONFIG_SET_COMPRESSION_FACTORY:
  259. ** Configure a factory method to be invoked in case of an LSM_MISMATCH
  260. ** error.
  261. **
  262. ** LSM_CONFIG_READONLY:
  263. ** A read/write boolean parameter. This parameter may only be set before
  264. ** lsm_open() is called.
  265. */
  266. #define LSM_CONFIG_AUTOFLUSH 1
  267. #define LSM_CONFIG_PAGE_SIZE 2
  268. #define LSM_CONFIG_SAFETY 3
  269. #define LSM_CONFIG_BLOCK_SIZE 4
  270. #define LSM_CONFIG_AUTOWORK 5
  271. #define LSM_CONFIG_MMAP 7
  272. #define LSM_CONFIG_USE_LOG 8
  273. #define LSM_CONFIG_AUTOMERGE 9
  274. #define LSM_CONFIG_MAX_FREELIST 10
  275. #define LSM_CONFIG_MULTIPLE_PROCESSES 11
  276. #define LSM_CONFIG_AUTOCHECKPOINT 12
  277. #define LSM_CONFIG_SET_COMPRESSION 13
  278. #define LSM_CONFIG_GET_COMPRESSION 14
  279. #define LSM_CONFIG_SET_COMPRESSION_FACTORY 15
  280. #define LSM_CONFIG_READONLY 16
  281. #define LSM_SAFETY_OFF 0
  282. #define LSM_SAFETY_NORMAL 1
  283. #define LSM_SAFETY_FULL 2
  284. /*
  285. ** CAPI: Compression and/or Encryption Hooks
  286. */
  287. struct lsm_compress {
  288. void *pCtx;
  289. unsigned int iId;
  290. int (*xBound)(void *, int nSrc);
  291. int (*xCompress)(void *, char *, int *, const char *, int);
  292. int (*xUncompress)(void *, char *, int *, const char *, int);
  293. void (*xFree)(void *pCtx);
  294. };
  295. struct lsm_compress_factory {
  296. void *pCtx;
  297. int (*xFactory)(void *, lsm_db *, unsigned int);
  298. void (*xFree)(void *pCtx);
  299. };
  300. #define LSM_COMPRESSION_EMPTY 0
  301. #define LSM_COMPRESSION_NONE 1
  302. /*
  303. ** CAPI: Allocating and Freeing Memory
  304. **
  305. ** Invoke the memory allocation functions that belong to environment
  306. ** pEnv. Or the system defaults if no memory allocation functions have
  307. ** been registered.
  308. */
  309. void *lsm_malloc(lsm_env*, size_t);
  310. void *lsm_realloc(lsm_env*, void *, size_t);
  311. void lsm_free(lsm_env*, void *);
  312. /*
  313. ** CAPI: Querying a Connection For Operational Data
  314. **
  315. ** Query a database connection for operational statistics or data.
  316. */
  317. int lsm_info(lsm_db *, int, ...);
  318. int lsm_get_user_version(lsm_db *, unsigned int *);
  319. int lsm_set_user_version(lsm_db *, unsigned int);
  320. /*
  321. ** The following values may be passed as the second argument to lsm_info().
  322. **
  323. ** LSM_INFO_NWRITE:
  324. ** The third parameter should be of type (int *). The location pointed
  325. ** to by the third parameter is set to the number of 4KB pages written to
  326. ** the database file during the lifetime of this connection.
  327. **
  328. ** LSM_INFO_NREAD:
  329. ** The third parameter should be of type (int *). The location pointed
  330. ** to by the third parameter is set to the number of 4KB pages read from
  331. ** the database file during the lifetime of this connection.
  332. **
  333. ** LSM_INFO_DB_STRUCTURE:
  334. ** The third argument should be of type (char **). The location pointed
  335. ** to is populated with a pointer to a nul-terminated string containing
  336. ** the string representation of a Tcl data-structure reflecting the
  337. ** current structure of the database file. Specifically, the current state
  338. ** of the worker snapshot. The returned string should be eventually freed
  339. ** by the caller using lsm_free().
  340. **
  341. ** The returned list contains one element for each level in the database,
  342. ** in order from most to least recent. Each element contains a
  343. ** single element for each segment comprising the corresponding level,
  344. ** starting with the lhs segment, then each of the rhs segments (if any)
  345. ** in order from most to least recent.
  346. **
  347. ** Each segment element is itself a list of 4 integer values, as follows:
  348. **
  349. ** <ol><li> First page of segment
  350. ** <li> Last page of segment
  351. ** <li> Root page of segment (if applicable)
  352. ** <li> Total number of pages in segment
  353. ** </ol>
  354. **
  355. ** LSM_INFO_ARRAY_STRUCTURE:
  356. ** There should be two arguments passed following this option (i.e. a
  357. ** total of four arguments passed to lsm_info()). The first argument
  358. ** should be the page number of the first page in a database array
  359. ** (perhaps obtained from an earlier INFO_DB_STRUCTURE call). The second
  360. ** trailing argument should be of type (char **). The location pointed
  361. ** to is populated with a pointer to a nul-terminated string that must
  362. ** be eventually freed using lsm_free() by the caller.
  363. **
  364. ** The output string contains the text representation of a Tcl list of
  365. ** integers. Each pair of integers represent a range of pages used by
  366. ** the identified array. For example, if the array occupies database
  367. ** pages 993 to 1024, then pages 2048 to 2777, then the returned string
  368. ** will be "993 1024 2048 2777".
  369. **
  370. ** If the specified integer argument does not correspond to the first
  371. ** page of any database array, LSM_ERROR is returned and the output
  372. ** pointer is set to a NULL value.
  373. **
  374. ** LSM_INFO_LOG_STRUCTURE:
  375. ** The third argument should be of type (char **). The location pointed
  376. ** to is populated with a pointer to a nul-terminated string containing
  377. ** the string representation of a Tcl data-structure. The returned
  378. ** string should be eventually freed by the caller using lsm_free().
  379. **
  380. ** The Tcl structure returned is a list of six integers that describe
  381. ** the current structure of the log file.
  382. **
  383. ** LSM_INFO_ARRAY_PAGES:
  384. **
  385. ** LSM_INFO_PAGE_ASCII_DUMP:
  386. ** As with LSM_INFO_ARRAY_STRUCTURE, there should be two arguments passed
  387. ** with calls that specify this option - an integer page number and a
  388. ** (char **) used to return a nul-terminated string that must be later
  389. ** freed using lsm_free(). In this case the output string is populated
  390. ** with a human-readable description of the page content.
  391. **
  392. ** If the page cannot be decoded, it is not an error. In this case the
  393. ** human-readable output message will report the systems failure to
  394. ** interpret the page data.
  395. **
  396. ** LSM_INFO_PAGE_HEX_DUMP:
  397. ** This argument is similar to PAGE_ASCII_DUMP, except that keys and
  398. ** values are represented using hexadecimal notation instead of ascii.
  399. **
  400. ** LSM_INFO_FREELIST:
  401. ** The third argument should be of type (char **). The location pointed
  402. ** to is populated with a pointer to a nul-terminated string containing
  403. ** the string representation of a Tcl data-structure. The returned
  404. ** string should be eventually freed by the caller using lsm_free().
  405. **
  406. ** The Tcl structure returned is a list containing one element for each
  407. ** free block in the database. The element itself consists of two
  408. ** integers - the block number and the id of the snapshot that freed it.
  409. **
  410. ** LSM_INFO_CHECKPOINT_SIZE:
  411. ** The third argument should be of type (int *). The location pointed to
  412. ** by this argument is populated with the number of KB written to the
  413. ** database file since the most recent checkpoint.
  414. **
  415. ** LSM_INFO_TREE_SIZE:
  416. ** If this value is passed as the second argument to an lsm_info() call, it
  417. ** should be followed by two arguments of type (int *) (for a total of four
  418. ** arguments).
  419. **
  420. ** At any time, there are either one or two tree structures held in shared
  421. ** memory that new database clients will access (there may also be additional
  422. ** tree structures being used by older clients - this API does not provide
  423. ** information on them). One tree structure - the current tree - is used to
  424. ** accumulate new data written to the database. The other tree structure -
  425. ** the old tree - is a read-only tree holding older data and may be flushed
  426. ** to disk at any time.
  427. **
  428. ** Assuming no error occurs, the location pointed to by the first of the two
  429. ** (int *) arguments is set to the size of the old in-memory tree in KB.
  430. ** The second is set to the size of the current, or live in-memory tree.
  431. **
  432. ** LSM_INFO_COMPRESSION_ID:
  433. ** This value should be followed by a single argument of type
  434. ** (unsigned int *). If successful, the location pointed to is populated
  435. ** with the database compression id before returning.
  436. */
  437. #define LSM_INFO_NWRITE 1
  438. #define LSM_INFO_NREAD 2
  439. #define LSM_INFO_DB_STRUCTURE 3
  440. #define LSM_INFO_LOG_STRUCTURE 4
  441. #define LSM_INFO_ARRAY_STRUCTURE 5
  442. #define LSM_INFO_PAGE_ASCII_DUMP 6
  443. #define LSM_INFO_PAGE_HEX_DUMP 7
  444. #define LSM_INFO_FREELIST 8
  445. #define LSM_INFO_ARRAY_PAGES 9
  446. #define LSM_INFO_CHECKPOINT_SIZE 10
  447. #define LSM_INFO_TREE_SIZE 11
  448. #define LSM_INFO_FREELIST_SIZE 12
  449. #define LSM_INFO_COMPRESSION_ID 13
  450. /*
  451. ** CAPI: Opening and Closing Write Transactions
  452. **
  453. ** These functions are used to open and close transactions and nested
  454. ** sub-transactions.
  455. **
  456. ** The lsm_begin() function is used to open transactions and sub-transactions.
  457. ** A successful call to lsm_begin() ensures that there are at least iLevel
  458. ** nested transactions open. To open a top-level transaction, pass iLevel=1.
  459. ** To open a sub-transaction within the top-level transaction, iLevel=2.
  460. ** Passing iLevel=0 is a no-op.
  461. **
  462. ** lsm_commit() is used to commit transactions and sub-transactions. A
  463. ** successful call to lsm_commit() ensures that there are at most iLevel
  464. ** nested transactions open. To commit a top-level transaction, pass iLevel=0.
  465. ** To commit all sub-transactions inside the main transaction, pass iLevel=1.
  466. **
  467. ** Function lsm_rollback() is used to roll back transactions and
  468. ** sub-transactions. A successful call to lsm_rollback() restores the database
  469. ** to the state it was in when the iLevel'th nested sub-transaction (if any)
  470. ** was first opened. And then closes transactions to ensure that there are
  471. ** at most iLevel nested transactions open. Passing iLevel=0 rolls back and
  472. ** closes the top-level transaction. iLevel=1 also rolls back the top-level
  473. ** transaction, but leaves it open. iLevel=2 rolls back the sub-transaction
  474. ** nested directly inside the top-level transaction (and leaves it open).
  475. */
  476. int lsm_begin(lsm_db *pDb, int iLevel);
  477. int lsm_commit(lsm_db *pDb, int iLevel);
  478. int lsm_rollback(lsm_db *pDb, int iLevel);
  479. /*
  480. ** CAPI: Writing to a Database
  481. **
  482. ** Write a new value into the database. If a value with a duplicate key
  483. ** already exists it is replaced.
  484. */
  485. int lsm_insert(lsm_db*, const void *pKey, int nKey, const void *pVal, int nVal);
  486. /*
  487. ** Delete a value from the database. No error is returned if the specified
  488. ** key value does not exist in the database.
  489. */
  490. int lsm_delete(lsm_db *, const void *pKey, int nKey);
  491. /*
  492. ** Delete all database entries with keys that are greater than (pKey1/nKey1)
  493. ** and smaller than (pKey2/nKey2). Note that keys (pKey1/nKey1) and
  494. ** (pKey2/nKey2) themselves, if they exist in the database, are not deleted.
  495. **
  496. ** Return LSM_OK if successful, or an LSM error code otherwise.
  497. */
  498. int lsm_delete_range(lsm_db *,
  499. const void *pKey1, int nKey1, const void *pKey2, int nKey2
  500. );
  501. /*
  502. ** CAPI: Explicit Database Work and Checkpointing
  503. **
  504. ** This function is called by a thread to work on the database structure.
  505. */
  506. int lsm_work(lsm_db *pDb, int nMerge, int nKB, int *pnWrite);
  507. int lsm_flush(lsm_db *pDb);
  508. /*
  509. ** Attempt to checkpoint the current database snapshot. Return an LSM
  510. ** error code if an error occurs or LSM_OK otherwise.
  511. **
  512. ** If the current snapshot has already been checkpointed, calling this
  513. ** function is a no-op. In this case if pnKB is not NULL, *pnKB is
  514. ** set to 0. Or, if the current snapshot is successfully checkpointed
  515. ** by this function and pbKB is not NULL, *pnKB is set to the number
  516. ** of bytes written to the database file since the previous checkpoint
  517. ** (the same measure as returned by the LSM_INFO_CHECKPOINT_SIZE query).
  518. */
  519. int lsm_checkpoint(lsm_db *pDb, int *pnKB);
  520. /*
  521. ** CAPI: Opening and Closing Database Cursors
  522. **
  523. ** Open and close a database cursor.
  524. */
  525. int lsm_csr_open(lsm_db *pDb, lsm_cursor **ppCsr);
  526. int lsm_csr_close(lsm_cursor *pCsr);
  527. /*
  528. ** CAPI: Positioning Database Cursors
  529. **
  530. ** If the fourth parameter is LSM_SEEK_EQ, LSM_SEEK_GE or LSM_SEEK_LE,
  531. ** this function searches the database for an entry with key (pKey/nKey).
  532. ** If an error occurs, an LSM error code is returned. Otherwise, LSM_OK.
  533. **
  534. ** If no error occurs and the requested key is present in the database, the
  535. ** cursor is left pointing to the entry with the specified key. Or, if the
  536. ** specified key is not present in the database the state of the cursor
  537. ** depends on the value passed as the final parameter, as follows:
  538. **
  539. ** LSM_SEEK_EQ:
  540. ** The cursor is left at EOF (invalidated). A call to lsm_csr_valid()
  541. ** returns non-zero.
  542. **
  543. ** LSM_SEEK_LE:
  544. ** The cursor is left pointing to the largest key in the database that
  545. ** is smaller than (pKey/nKey). If the database contains no keys smaller
  546. ** than (pKey/nKey), the cursor is left at EOF.
  547. **
  548. ** LSM_SEEK_GE:
  549. ** The cursor is left pointing to the smallest key in the database that
  550. ** is larger than (pKey/nKey). If the database contains no keys larger
  551. ** than (pKey/nKey), the cursor is left at EOF.
  552. **
  553. ** If the fourth parameter is LSM_SEEK_LEFAST, this function searches the
  554. ** database in a similar manner to LSM_SEEK_LE, with two differences:
  555. **
  556. ** <ol><li>Even if a key can be found (the cursor is not left at EOF), the
  557. ** lsm_csr_value() function may not be used (attempts to do so return
  558. ** LSM_MISUSE).
  559. **
  560. ** <li>The key that the cursor is left pointing to may be one that has
  561. ** been recently deleted from the database. In this case it is
  562. ** guaranteed that the returned key is larger than any key currently
  563. ** in the database that is less than or equal to (pKey/nKey).
  564. ** </ol>
  565. **
  566. ** LSM_SEEK_LEFAST requests are intended to be used to allocate database
  567. ** keys.
  568. */
  569. int lsm_csr_seek(lsm_cursor *pCsr, const void *pKey, int nKey, int eSeek);
  570. int lsm_csr_first(lsm_cursor *pCsr);
  571. int lsm_csr_last(lsm_cursor *pCsr);
  572. /*
  573. ** Advance the specified cursor to the next or previous key in the database.
  574. ** Return LSM_OK if successful, or an LSM error code otherwise.
  575. **
  576. ** Functions lsm_csr_seek(), lsm_csr_first() and lsm_csr_last() are "seek"
  577. ** functions. Whether or not lsm_csr_next and lsm_csr_prev may be called
  578. ** successfully also depends on the most recent seek function called on
  579. ** the cursor. Specifically:
  580. **
  581. ** <ul>
  582. ** <li> At least one seek function must have been called on the cursor.
  583. ** <li> To call lsm_csr_next(), the most recent call to a seek function must
  584. ** have been either lsm_csr_first() or a call to lsm_csr_seek() specifying
  585. ** LSM_SEEK_GE.
  586. ** <li> To call lsm_csr_prev(), the most recent call to a seek function must
  587. ** have been either lsm_csr_last() or a call to lsm_csr_seek() specifying
  588. ** LSM_SEEK_LE.
  589. ** </ul>
  590. **
  591. ** Otherwise, if the above conditions are not met when lsm_csr_next or
  592. ** lsm_csr_prev is called, LSM_MISUSE is returned and the cursor position
  593. ** remains unchanged.
  594. */
  595. int lsm_csr_next(lsm_cursor *pCsr);
  596. int lsm_csr_prev(lsm_cursor *pCsr);
  597. /*
  598. ** Values that may be passed as the fourth argument to lsm_csr_seek().
  599. */
  600. #define LSM_SEEK_LEFAST -2
  601. #define LSM_SEEK_LE -1
  602. #define LSM_SEEK_EQ 0
  603. #define LSM_SEEK_GE 1
  604. /*
  605. ** CAPI: Extracting Data From Database Cursors
  606. **
  607. ** Retrieve data from a database cursor.
  608. */
  609. int lsm_csr_valid(lsm_cursor *pCsr);
  610. int lsm_csr_key(lsm_cursor *pCsr, const void **ppKey, int *pnKey);
  611. int lsm_csr_value(lsm_cursor *pCsr, const void **ppVal, int *pnVal);
  612. /*
  613. ** If no error occurs, this function compares the database key passed via
  614. ** the pKey/nKey arguments with the key that the cursor passed as the first
  615. ** argument currently points to. If the cursors key is less than, equal to
  616. ** or greater than pKey/nKey, *piRes is set to less than, equal to or greater
  617. ** than zero before returning. LSM_OK is returned in this case.
  618. **
  619. ** Or, if an error occurs, an LSM error code is returned and the final
  620. ** value of *piRes is undefined. If the cursor does not point to a valid
  621. ** key when this function is called, LSM_MISUSE is returned.
  622. */
  623. int lsm_csr_cmp(lsm_cursor *pCsr, const void *pKey, int nKey, int *piRes);
  624. /*
  625. ** CAPI: Change these!!
  626. **
  627. ** Configure a callback to which debugging and other messages should
  628. ** be directed. Only useful for debugging lsm.
  629. */
  630. void lsm_config_log(lsm_db *, void (*)(void *, int, const char *), void *);
  631. /*
  632. ** Configure a callback that is invoked if the database connection ever
  633. ** writes to the database file.
  634. */
  635. void lsm_config_work_hook(lsm_db *, void (*)(lsm_db *, void *), void *);
  636. /* ENDOFAPI */
  637. #ifdef __cplusplus
  638. } /* End of the 'extern "C"' block */
  639. #endif
  640. #endif /* ifndef _LSM_H */