lmdb.h 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. /** @file lmdb.h
  2. * @brief Lightning memory-mapped database library
  3. *
  4. * @mainpage Lightning Memory-Mapped Database Manager (LMDB)
  5. *
  6. * @section intro_sec Introduction
  7. * LMDB is a Btree-based database management library modeled loosely on the
  8. * BerkeleyDB API, but much simplified. The entire database is exposed
  9. * in a memory map, and all data fetches return data directly
  10. * from the mapped memory, so no malloc's or memcpy's occur during
  11. * data fetches. As such, the library is extremely simple because it
  12. * requires no page caching layer of its own, and it is extremely high
  13. * performance and memory-efficient. It is also fully transactional with
  14. * full ACID semantics, and when the memory map is read-only, the
  15. * database integrity cannot be corrupted by stray pointer writes from
  16. * application code.
  17. *
  18. * The library is fully thread-aware and supports concurrent read/write
  19. * access from multiple processes and threads. Data pages use a copy-on-
  20. * write strategy so no active data pages are ever overwritten, which
  21. * also provides resistance to corruption and eliminates the need of any
  22. * special recovery procedures after a system crash. Writes are fully
  23. * serialized; only one write transaction may be active at a time, which
  24. * guarantees that writers can never deadlock. The database structure is
  25. * multi-versioned so readers run with no locks; writers cannot block
  26. * readers, and readers don't block writers.
  27. *
  28. * Unlike other well-known database mechanisms which use either write-ahead
  29. * transaction logs or append-only data writes, LMDB requires no maintenance
  30. * during operation. Both write-ahead loggers and append-only databases
  31. * require periodic checkpointing and/or compaction of their log or database
  32. * files otherwise they grow without bound. LMDB tracks free pages within
  33. * the database and re-uses them for new write operations, so the database
  34. * size does not grow without bound in normal use.
  35. *
  36. * The memory map can be used as a read-only or read-write map. It is
  37. * read-only by default as this provides total immunity to corruption.
  38. * Using read-write mode offers much higher write performance, but adds
  39. * the possibility for stray application writes thru pointers to silently
  40. * corrupt the database. Of course if your application code is known to
  41. * be bug-free (...) then this is not an issue.
  42. *
  43. * If this is your first time using a transactional embedded key/value
  44. * store, you may find the \ref starting page to be helpful.
  45. *
  46. * @section caveats_sec Caveats
  47. * Troubleshooting the lock file, plus semaphores on BSD systems:
  48. *
  49. * - A broken lockfile can cause sync issues.
  50. * Stale reader transactions left behind by an aborted program
  51. * cause further writes to grow the database quickly, and
  52. * stale locks can block further operation.
  53. *
  54. * Fix: Check for stale readers periodically, using the
  55. * #mdb_reader_check function or the \ref mdb_stat_1 "mdb_stat" tool.
  56. * Stale writers will be cleared automatically on some systems:
  57. * - Windows - automatic
  58. * - Linux, systems using POSIX mutexes with Robust option - automatic
  59. * - not on BSD, systems using POSIX semaphores.
  60. * Otherwise just make all programs using the database close it;
  61. * the lockfile is always reset on first open of the environment.
  62. *
  63. * - On BSD systems or others configured with MDB_USE_POSIX_SEM,
  64. * startup can fail due to semaphores owned by another userid.
  65. *
  66. * Fix: Open and close the database as the user which owns the
  67. * semaphores (likely last user) or as root, while no other
  68. * process is using the database.
  69. *
  70. * Restrictions/caveats (in addition to those listed for some functions):
  71. *
  72. * - Only the database owner should normally use the database on
  73. * BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
  74. * Multiple users can cause startup to fail later, as noted above.
  75. *
  76. * - There is normally no pure read-only mode, since readers need write
  77. * access to locks and lock file. Exceptions: On read-only filesystems
  78. * or with the #MDB_NOLOCK flag described under #mdb_env_open().
  79. *
  80. * - An LMDB configuration will often reserve considerable \b unused
  81. * memory address space and maybe file size for future growth.
  82. * This does not use actual memory or disk space, but users may need
  83. * to understand the difference so they won't be scared off.
  84. *
  85. * - By default, in versions before 0.9.10, unused portions of the data
  86. * file might receive garbage data from memory freed by other code.
  87. * (This does not happen when using the #MDB_WRITEMAP flag.) As of
  88. * 0.9.10 the default behavior is to initialize such memory before
  89. * writing to the data file. Since there may be a slight performance
  90. * cost due to this initialization, applications may disable it using
  91. * the #MDB_NOMEMINIT flag. Applications handling sensitive data
  92. * which must not be written should not use this flag. This flag is
  93. * irrelevant when using #MDB_WRITEMAP.
  94. *
  95. * - A thread can only use one transaction at a time, plus any child
  96. * transactions. Each transaction belongs to one thread. See below.
  97. * The #MDB_NOTLS flag changes this for read-only transactions.
  98. *
  99. * - Use an MDB_env* in the process which opened it, not after fork().
  100. *
  101. * - Do not have open an LMDB database twice in the same process at
  102. * the same time. Not even from a plain open() call - close()ing it
  103. * breaks fcntl() advisory locking. (It is OK to reopen it after
  104. * fork() - exec*(), since the lockfile has FD_CLOEXEC set.)
  105. *
  106. * - Avoid long-lived transactions. Read transactions prevent
  107. * reuse of pages freed by newer write transactions, thus the
  108. * database can grow quickly. Write transactions prevent
  109. * other write transactions, since writes are serialized.
  110. *
  111. * - Avoid suspending a process with active transactions. These
  112. * would then be "long-lived" as above. Also read transactions
  113. * suspended when writers commit could sometimes see wrong data.
  114. *
  115. * ...when several processes can use a database concurrently:
  116. *
  117. * - Avoid aborting a process with an active transaction.
  118. * The transaction becomes "long-lived" as above until a check
  119. * for stale readers is performed or the lockfile is reset,
  120. * since the process may not remove it from the lockfile.
  121. *
  122. * This does not apply to write transactions if the system clears
  123. * stale writers, see above.
  124. *
  125. * - If you do that anyway, do a periodic check for stale readers. Or
  126. * close the environment once in a while, so the lockfile can get reset.
  127. *
  128. * - Do not use LMDB databases on remote filesystems, even between
  129. * processes on the same host. This breaks flock() on some OSes,
  130. * possibly memory map sync, and certainly sync between programs
  131. * on different hosts.
  132. *
  133. * - Opening a database can fail if another process is opening or
  134. * closing it at exactly the same time.
  135. *
  136. * @author Howard Chu, Symas Corporation.
  137. *
  138. * @copyright Copyright 2011-2021 Howard Chu, Symas Corp. All rights reserved.
  139. *
  140. * Redistribution and use in source and binary forms, with or without
  141. * modification, are permitted only as authorized by the OpenLDAP
  142. * Public License.
  143. *
  144. * A copy of this license is available in the file LICENSE in the
  145. * top-level directory of the distribution or, alternatively, at
  146. * <http://www.OpenLDAP.org/license.html>.
  147. *
  148. * @par Derived From:
  149. * This code is derived from btree.c written by Martin Hedenfalk.
  150. *
  151. * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
  152. *
  153. * Permission to use, copy, modify, and distribute this software for any
  154. * purpose with or without fee is hereby granted, provided that the above
  155. * copyright notice and this permission notice appear in all copies.
  156. *
  157. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  158. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  159. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  160. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  161. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  162. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  163. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  164. */
  165. #ifndef _LMDB_H_
  166. #define _LMDB_H_
  167. #include <sys/types.h>
  168. #ifdef __cplusplus
  169. extern "C" {
  170. #endif
  171. /** Unix permissions for creating files, or dummy definition for Windows */
  172. #ifdef _MSC_VER
  173. typedef int mdb_mode_t;
  174. #else
  175. typedef mode_t mdb_mode_t;
  176. #endif
  177. /** An abstraction for a file handle.
  178. * On POSIX systems file handles are small integers. On Windows
  179. * they're opaque pointers.
  180. */
  181. #ifdef _WIN32
  182. typedef void *mdb_filehandle_t;
  183. #else
  184. typedef int mdb_filehandle_t;
  185. #endif
  186. /** @defgroup mdb LMDB API
  187. * @{
  188. * @brief OpenLDAP Lightning Memory-Mapped Database Manager
  189. */
  190. /** @defgroup Version Version Macros
  191. * @{
  192. */
  193. /** Library major version */
  194. #define MDB_VERSION_MAJOR 0
  195. /** Library minor version */
  196. #define MDB_VERSION_MINOR 9
  197. /** Library patch version */
  198. #define MDB_VERSION_PATCH 31
  199. /** Combine args a,b,c into a single integer for easy version comparisons */
  200. #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
  201. /** The full library version as a single integer */
  202. #define MDB_VERSION_FULL \
  203. MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
  204. /** The release date of this library version */
  205. #define MDB_VERSION_DATE "July 10, 2023"
  206. /** A stringifier for the version info */
  207. #define MDB_VERSTR(a,b,c,d) "LMDB " #a "." #b "." #c ": (" d ")"
  208. /** A helper for the stringifier macro */
  209. #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
  210. /** The full library version as a C string */
  211. #define MDB_VERSION_STRING \
  212. MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
  213. /** @} */
  214. /** @brief Opaque structure for a database environment.
  215. *
  216. * A DB environment supports multiple databases, all residing in the same
  217. * shared-memory map.
  218. */
  219. typedef struct MDB_env MDB_env;
  220. /** @brief Opaque structure for a transaction handle.
  221. *
  222. * All database operations require a transaction handle. Transactions may be
  223. * read-only or read-write.
  224. */
  225. typedef struct MDB_txn MDB_txn;
  226. /** @brief A handle for an individual database in the DB environment. */
  227. typedef unsigned int MDB_dbi;
  228. /** @brief Opaque structure for navigating through a database */
  229. typedef struct MDB_cursor MDB_cursor;
  230. /** @brief Generic structure used for passing keys and data in and out
  231. * of the database.
  232. *
  233. * Values returned from the database are valid only until a subsequent
  234. * update operation, or the end of the transaction. Do not modify or
  235. * free them, they commonly point into the database itself.
  236. *
  237. * Key sizes must be between 1 and #mdb_env_get_maxkeysize() inclusive.
  238. * The same applies to data sizes in databases with the #MDB_DUPSORT flag.
  239. * Other data items can in theory be from 0 to 0xffffffff bytes long.
  240. */
  241. typedef struct MDB_val {
  242. size_t mv_size; /**< size of the data item */
  243. void *mv_data; /**< address of the data item */
  244. } MDB_val;
  245. /** @brief A callback function used to compare two keys in a database */
  246. typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
  247. /** @brief A callback function used to relocate a position-dependent data item
  248. * in a fixed-address database.
  249. *
  250. * The \b newptr gives the item's desired address in
  251. * the memory map, and \b oldptr gives its previous address. The item's actual
  252. * data resides at the address in \b item. This callback is expected to walk
  253. * through the fields of the record in \b item and modify any
  254. * values based at the \b oldptr address to be relative to the \b newptr address.
  255. * @param[in,out] item The item that is to be relocated.
  256. * @param[in] oldptr The previous address.
  257. * @param[in] newptr The new address to relocate to.
  258. * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
  259. * @todo This feature is currently unimplemented.
  260. */
  261. typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
  262. /** @defgroup mdb_env Environment Flags
  263. * @{
  264. */
  265. /** mmap at a fixed address (experimental) */
  266. #define MDB_FIXEDMAP 0x01
  267. /** no environment directory */
  268. #define MDB_NOSUBDIR 0x4000
  269. /** don't fsync after commit */
  270. #define MDB_NOSYNC 0x10000
  271. /** read only */
  272. #define MDB_RDONLY 0x20000
  273. /** don't fsync metapage after commit */
  274. #define MDB_NOMETASYNC 0x40000
  275. /** use writable mmap */
  276. #define MDB_WRITEMAP 0x80000
  277. /** use asynchronous msync when #MDB_WRITEMAP is used */
  278. #define MDB_MAPASYNC 0x100000
  279. /** tie reader locktable slots to #MDB_txn objects instead of to threads */
  280. #define MDB_NOTLS 0x200000
  281. /** don't do any locking, caller must manage their own locks */
  282. #define MDB_NOLOCK 0x400000
  283. /** don't do readahead (no effect on Windows) */
  284. #define MDB_NORDAHEAD 0x800000
  285. /** don't initialize malloc'd memory before writing to datafile */
  286. #define MDB_NOMEMINIT 0x1000000
  287. /** @} */
  288. /** @defgroup mdb_dbi_open Database Flags
  289. * @{
  290. */
  291. /** use reverse string keys */
  292. #define MDB_REVERSEKEY 0x02
  293. /** use sorted duplicates */
  294. #define MDB_DUPSORT 0x04
  295. /** numeric keys in native byte order: either unsigned int or size_t.
  296. * The keys must all be of the same size. */
  297. #define MDB_INTEGERKEY 0x08
  298. /** with #MDB_DUPSORT, sorted dup items have fixed size */
  299. #define MDB_DUPFIXED 0x10
  300. /** with #MDB_DUPSORT, dups are #MDB_INTEGERKEY-style integers */
  301. #define MDB_INTEGERDUP 0x20
  302. /** with #MDB_DUPSORT, use reverse string dups */
  303. #define MDB_REVERSEDUP 0x40
  304. /** create DB if not already existing */
  305. #define MDB_CREATE 0x40000
  306. /** @} */
  307. /** @defgroup mdb_put Write Flags
  308. * @{
  309. */
  310. /** For put: Don't write if the key already exists. */
  311. #define MDB_NOOVERWRITE 0x10
  312. /** Only for #MDB_DUPSORT<br>
  313. * For put: don't write if the key and data pair already exist.<br>
  314. * For mdb_cursor_del: remove all duplicate data items.
  315. */
  316. #define MDB_NODUPDATA 0x20
  317. /** For mdb_cursor_put: overwrite the current key/data pair */
  318. #define MDB_CURRENT 0x40
  319. /** For put: Just reserve space for data, don't copy it. Return a
  320. * pointer to the reserved space.
  321. */
  322. #define MDB_RESERVE 0x10000
  323. /** Data is being appended, don't split full pages. */
  324. #define MDB_APPEND 0x20000
  325. /** Duplicate data is being appended, don't split full pages. */
  326. #define MDB_APPENDDUP 0x40000
  327. /** Store multiple data items in one call. Only for #MDB_DUPFIXED. */
  328. #define MDB_MULTIPLE 0x80000
  329. /* @} */
  330. /** @defgroup mdb_copy Copy Flags
  331. * @{
  332. */
  333. /** Compacting copy: Omit free space from copy, and renumber all
  334. * pages sequentially.
  335. */
  336. #define MDB_CP_COMPACT 0x01
  337. /* @} */
  338. /** @brief Cursor Get operations.
  339. *
  340. * This is the set of all operations for retrieving data
  341. * using a cursor.
  342. */
  343. typedef enum MDB_cursor_op {
  344. MDB_FIRST, /**< Position at first key/data item */
  345. MDB_FIRST_DUP, /**< Position at first data item of current key.
  346. Only for #MDB_DUPSORT */
  347. MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
  348. MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
  349. MDB_GET_CURRENT, /**< Return key/data at current cursor position */
  350. MDB_GET_MULTIPLE, /**< Return up to a page of duplicate data items
  351. from current cursor position. Move cursor to prepare
  352. for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
  353. MDB_LAST, /**< Position at last key/data item */
  354. MDB_LAST_DUP, /**< Position at last data item of current key.
  355. Only for #MDB_DUPSORT */
  356. MDB_NEXT, /**< Position at next data item */
  357. MDB_NEXT_DUP, /**< Position at next data item of current key.
  358. Only for #MDB_DUPSORT */
  359. MDB_NEXT_MULTIPLE, /**< Return up to a page of duplicate data items
  360. from next cursor position. Move cursor to prepare
  361. for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
  362. MDB_NEXT_NODUP, /**< Position at first data item of next key */
  363. MDB_PREV, /**< Position at previous data item */
  364. MDB_PREV_DUP, /**< Position at previous data item of current key.
  365. Only for #MDB_DUPSORT */
  366. MDB_PREV_NODUP, /**< Position at last data item of previous key */
  367. MDB_SET, /**< Position at specified key */
  368. MDB_SET_KEY, /**< Position at specified key, return key + data */
  369. MDB_SET_RANGE, /**< Position at first key greater than or equal to specified key. */
  370. MDB_PREV_MULTIPLE /**< Position at previous page and return up to
  371. a page of duplicate data items. Only for #MDB_DUPFIXED */
  372. } MDB_cursor_op;
  373. /** @defgroup errors Return Codes
  374. *
  375. * BerkeleyDB uses -30800 to -30999, we'll go under them
  376. * @{
  377. */
  378. /** Successful result */
  379. #define MDB_SUCCESS 0
  380. /** key/data pair already exists */
  381. #define MDB_KEYEXIST (-30799)
  382. /** key/data pair not found (EOF) */
  383. #define MDB_NOTFOUND (-30798)
  384. /** Requested page not found - this usually indicates corruption */
  385. #define MDB_PAGE_NOTFOUND (-30797)
  386. /** Located page was wrong type */
  387. #define MDB_CORRUPTED (-30796)
  388. /** Update of meta page failed or environment had fatal error */
  389. #define MDB_PANIC (-30795)
  390. /** Environment version mismatch */
  391. #define MDB_VERSION_MISMATCH (-30794)
  392. /** File is not a valid LMDB file */
  393. #define MDB_INVALID (-30793)
  394. /** Environment mapsize reached */
  395. #define MDB_MAP_FULL (-30792)
  396. /** Environment maxdbs reached */
  397. #define MDB_DBS_FULL (-30791)
  398. /** Environment maxreaders reached */
  399. #define MDB_READERS_FULL (-30790)
  400. /** Too many TLS keys in use - Windows only */
  401. #define MDB_TLS_FULL (-30789)
  402. /** Txn has too many dirty pages */
  403. #define MDB_TXN_FULL (-30788)
  404. /** Cursor stack too deep - internal error */
  405. #define MDB_CURSOR_FULL (-30787)
  406. /** Page has not enough space - internal error */
  407. #define MDB_PAGE_FULL (-30786)
  408. /** Database contents grew beyond environment mapsize */
  409. #define MDB_MAP_RESIZED (-30785)
  410. /** Operation and DB incompatible, or DB type changed. This can mean:
  411. * <ul>
  412. * <li>The operation expects an #MDB_DUPSORT / #MDB_DUPFIXED database.
  413. * <li>Opening a named DB when the unnamed DB has #MDB_DUPSORT / #MDB_INTEGERKEY.
  414. * <li>Accessing a data record as a database, or vice versa.
  415. * <li>The database was dropped and recreated with different flags.
  416. * </ul>
  417. */
  418. #define MDB_INCOMPATIBLE (-30784)
  419. /** Invalid reuse of reader locktable slot */
  420. #define MDB_BAD_RSLOT (-30783)
  421. /** Transaction must abort, has a child, or is invalid */
  422. #define MDB_BAD_TXN (-30782)
  423. /** Unsupported size of key/DB name/data, or wrong DUPFIXED size */
  424. #define MDB_BAD_VALSIZE (-30781)
  425. /** The specified DBI was changed unexpectedly */
  426. #define MDB_BAD_DBI (-30780)
  427. /** The last defined error code */
  428. #define MDB_LAST_ERRCODE MDB_BAD_DBI
  429. /** @} */
  430. /** @brief Statistics for a database in the environment */
  431. typedef struct MDB_stat {
  432. unsigned int ms_psize; /**< Size of a database page.
  433. This is currently the same for all databases. */
  434. unsigned int ms_depth; /**< Depth (height) of the B-tree */
  435. size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */
  436. size_t ms_leaf_pages; /**< Number of leaf pages */
  437. size_t ms_overflow_pages; /**< Number of overflow pages */
  438. size_t ms_entries; /**< Number of data items */
  439. } MDB_stat;
  440. /** @brief Information about the environment */
  441. typedef struct MDB_envinfo {
  442. void *me_mapaddr; /**< Address of map, if fixed */
  443. size_t me_mapsize; /**< Size of the data memory map */
  444. size_t me_last_pgno; /**< ID of the last used page */
  445. size_t me_last_txnid; /**< ID of the last committed transaction */
  446. unsigned int me_maxreaders; /**< max reader slots in the environment */
  447. unsigned int me_numreaders; /**< max reader slots used in the environment */
  448. } MDB_envinfo;
  449. /** @brief Return the LMDB library version information.
  450. *
  451. * @param[out] major if non-NULL, the library major version number is copied here
  452. * @param[out] minor if non-NULL, the library minor version number is copied here
  453. * @param[out] patch if non-NULL, the library patch version number is copied here
  454. * @retval "version string" The library version as a string
  455. */
  456. char *mdb_version(int *major, int *minor, int *patch);
  457. /** @brief Return a string describing a given error code.
  458. *
  459. * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
  460. * function. If the error code is greater than or equal to 0, then the string
  461. * returned by the system function strerror(3) is returned. If the error code
  462. * is less than 0, an error string corresponding to the LMDB library error is
  463. * returned. See @ref errors for a list of LMDB-specific error codes.
  464. * @param[in] err The error code
  465. * @retval "error message" The description of the error
  466. */
  467. char *mdb_strerror(int err);
  468. /** @brief Create an LMDB environment handle.
  469. *
  470. * This function allocates memory for a #MDB_env structure. To release
  471. * the allocated memory and discard the handle, call #mdb_env_close().
  472. * Before the handle may be used, it must be opened using #mdb_env_open().
  473. * Various other options may also need to be set before opening the handle,
  474. * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
  475. * depending on usage requirements.
  476. * @param[out] env The address where the new handle will be stored
  477. * @return A non-zero error value on failure and 0 on success.
  478. */
  479. int mdb_env_create(MDB_env **env);
  480. /** @brief Open an environment handle.
  481. *
  482. * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
  483. * @param[in] env An environment handle returned by #mdb_env_create()
  484. * @param[in] path The directory in which the database files reside. This
  485. * directory must already exist and be writable.
  486. * @param[in] flags Special options for this environment. This parameter
  487. * must be set to 0 or by bitwise OR'ing together one or more of the
  488. * values described here.
  489. * Flags set by mdb_env_set_flags() are also used.
  490. * <ul>
  491. * <li>#MDB_FIXEDMAP
  492. * use a fixed address for the mmap region. This flag must be specified
  493. * when creating the environment, and is stored persistently in the environment.
  494. * If successful, the memory map will always reside at the same virtual address
  495. * and pointers used to reference data items in the database will be constant
  496. * across multiple invocations. This option may not always work, depending on
  497. * how the operating system has allocated memory to shared libraries and other uses.
  498. * The feature is highly experimental.
  499. * <li>#MDB_NOSUBDIR
  500. * By default, LMDB creates its environment in a directory whose
  501. * pathname is given in \b path, and creates its data and lock files
  502. * under that directory. With this option, \b path is used as-is for
  503. * the database main data file. The database lock file is the \b path
  504. * with "-lock" appended.
  505. * <li>#MDB_RDONLY
  506. * Open the environment in read-only mode. No write operations will be
  507. * allowed. LMDB will still modify the lock file - except on read-only
  508. * filesystems, where LMDB does not use locks.
  509. * <li>#MDB_WRITEMAP
  510. * Use a writeable memory map unless MDB_RDONLY is set. This uses
  511. * fewer mallocs but loses protection from application bugs
  512. * like wild pointer writes and other bad updates into the database.
  513. * This may be slightly faster for DBs that fit entirely in RAM, but
  514. * is slower for DBs larger than RAM.
  515. * Incompatible with nested transactions.
  516. * Do not mix processes with and without MDB_WRITEMAP on the same
  517. * environment. This can defeat durability (#mdb_env_sync etc).
  518. * <li>#MDB_NOMETASYNC
  519. * Flush system buffers to disk only once per transaction, omit the
  520. * metadata flush. Defer that until the system flushes files to disk,
  521. * or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
  522. * maintains database integrity, but a system crash may undo the last
  523. * committed transaction. I.e. it preserves the ACI (atomicity,
  524. * consistency, isolation) but not D (durability) database property.
  525. * This flag may be changed at any time using #mdb_env_set_flags().
  526. * <li>#MDB_NOSYNC
  527. * Don't flush system buffers to disk when committing a transaction.
  528. * This optimization means a system crash can corrupt the database or
  529. * lose the last transactions if buffers are not yet flushed to disk.
  530. * The risk is governed by how often the system flushes dirty buffers
  531. * to disk and how often #mdb_env_sync() is called. However, if the
  532. * filesystem preserves write order and the #MDB_WRITEMAP flag is not
  533. * used, transactions exhibit ACI (atomicity, consistency, isolation)
  534. * properties and only lose D (durability). I.e. database integrity
  535. * is maintained, but a system crash may undo the final transactions.
  536. * Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
  537. * hint for when to write transactions to disk, unless #mdb_env_sync()
  538. * is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
  539. * This flag may be changed at any time using #mdb_env_set_flags().
  540. * <li>#MDB_MAPASYNC
  541. * When using #MDB_WRITEMAP, use asynchronous flushes to disk.
  542. * As with #MDB_NOSYNC, a system crash can then corrupt the
  543. * database or lose the last transactions. Calling #mdb_env_sync()
  544. * ensures on-disk database integrity until next commit.
  545. * This flag may be changed at any time using #mdb_env_set_flags().
  546. * <li>#MDB_NOTLS
  547. * Don't use Thread-Local Storage. Tie reader locktable slots to
  548. * #MDB_txn objects instead of to threads. I.e. #mdb_txn_reset() keeps
  549. * the slot reserved for the #MDB_txn object. A thread may use parallel
  550. * read-only transactions. A read-only transaction may span threads if
  551. * the user synchronizes its use. Applications that multiplex many
  552. * user threads over individual OS threads need this option. Such an
  553. * application must also serialize the write transactions in an OS
  554. * thread, since LMDB's write locking is unaware of the user threads.
  555. * <li>#MDB_NOLOCK
  556. * Don't do any locking. If concurrent access is anticipated, the
  557. * caller must manage all concurrency itself. For proper operation
  558. * the caller must enforce single-writer semantics, and must ensure
  559. * that no readers are using old transactions while a writer is
  560. * active. The simplest approach is to use an exclusive lock so that
  561. * no readers may be active at all when a writer begins.
  562. * <li>#MDB_NORDAHEAD
  563. * Turn off readahead. Most operating systems perform readahead on
  564. * read requests by default. This option turns it off if the OS
  565. * supports it. Turning it off may help random read performance
  566. * when the DB is larger than RAM and system RAM is full.
  567. * The option is not implemented on Windows.
  568. * <li>#MDB_NOMEMINIT
  569. * Don't initialize malloc'd memory before writing to unused spaces
  570. * in the data file. By default, memory for pages written to the data
  571. * file is obtained using malloc. While these pages may be reused in
  572. * subsequent transactions, freshly malloc'd pages will be initialized
  573. * to zeroes before use. This avoids persisting leftover data from other
  574. * code (that used the heap and subsequently freed the memory) into the
  575. * data file. Note that many other system libraries may allocate
  576. * and free memory from the heap for arbitrary uses. E.g., stdio may
  577. * use the heap for file I/O buffers. This initialization step has a
  578. * modest performance cost so some applications may want to disable
  579. * it using this flag. This option can be a problem for applications
  580. * which handle sensitive data like passwords, and it makes memory
  581. * checkers like Valgrind noisy. This flag is not needed with #MDB_WRITEMAP,
  582. * which writes directly to the mmap instead of using malloc for pages. The
  583. * initialization is also skipped if #MDB_RESERVE is used; the
  584. * caller is expected to overwrite all of the memory that was
  585. * reserved in that case.
  586. * This flag may be changed at any time using #mdb_env_set_flags().
  587. * </ul>
  588. * @param[in] mode The UNIX permissions to set on created files and semaphores.
  589. * This parameter is ignored on Windows.
  590. * @return A non-zero error value on failure and 0 on success. Some possible
  591. * errors are:
  592. * <ul>
  593. * <li>#MDB_VERSION_MISMATCH - the version of the LMDB library doesn't match the
  594. * version that created the database environment.
  595. * <li>#MDB_INVALID - the environment file headers are corrupted.
  596. * <li>ENOENT - the directory specified by the path parameter doesn't exist.
  597. * <li>EACCES - the user didn't have permission to access the environment files.
  598. * <li>EAGAIN - the environment was locked by another process.
  599. * </ul>
  600. */
  601. int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
  602. /** @brief Copy an LMDB environment to the specified path.
  603. *
  604. * This function may be used to make a backup of an existing environment.
  605. * No lockfile is created, since it gets recreated at need.
  606. * @note This call can trigger significant file size growth if run in
  607. * parallel with write transactions, because it employs a read-only
  608. * transaction. See long-lived transactions under @ref caveats_sec.
  609. * @param[in] env An environment handle returned by #mdb_env_create(). It
  610. * must have already been opened successfully.
  611. * @param[in] path The directory in which the copy will reside. This
  612. * directory must already exist and be writable but must otherwise be
  613. * empty.
  614. * @return A non-zero error value on failure and 0 on success.
  615. */
  616. int mdb_env_copy(MDB_env *env, const char *path);
  617. /** @brief Copy an LMDB environment to the specified file descriptor.
  618. *
  619. * This function may be used to make a backup of an existing environment.
  620. * No lockfile is created, since it gets recreated at need.
  621. * @note This call can trigger significant file size growth if run in
  622. * parallel with write transactions, because it employs a read-only
  623. * transaction. See long-lived transactions under @ref caveats_sec.
  624. * @param[in] env An environment handle returned by #mdb_env_create(). It
  625. * must have already been opened successfully.
  626. * @param[in] fd The filedescriptor to write the copy to. It must
  627. * have already been opened for Write access.
  628. * @return A non-zero error value on failure and 0 on success.
  629. */
  630. int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd);
  631. /** @brief Copy an LMDB environment to the specified path, with options.
  632. *
  633. * This function may be used to make a backup of an existing environment.
  634. * No lockfile is created, since it gets recreated at need.
  635. * @note This call can trigger significant file size growth if run in
  636. * parallel with write transactions, because it employs a read-only
  637. * transaction. See long-lived transactions under @ref caveats_sec.
  638. * @param[in] env An environment handle returned by #mdb_env_create(). It
  639. * must have already been opened successfully.
  640. * @param[in] path The directory in which the copy will reside. This
  641. * directory must already exist and be writable but must otherwise be
  642. * empty.
  643. * @param[in] flags Special options for this operation. This parameter
  644. * must be set to 0 or by bitwise OR'ing together one or more of the
  645. * values described here.
  646. * <ul>
  647. * <li>#MDB_CP_COMPACT - Perform compaction while copying: omit free
  648. * pages and sequentially renumber all pages in output. This option
  649. * consumes more CPU and runs more slowly than the default.
  650. * Currently it fails if the environment has suffered a page leak.
  651. * </ul>
  652. * @return A non-zero error value on failure and 0 on success.
  653. */
  654. int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags);
  655. /** @brief Copy an LMDB environment to the specified file descriptor,
  656. * with options.
  657. *
  658. * This function may be used to make a backup of an existing environment.
  659. * No lockfile is created, since it gets recreated at need. See
  660. * #mdb_env_copy2() for further details.
  661. * @note This call can trigger significant file size growth if run in
  662. * parallel with write transactions, because it employs a read-only
  663. * transaction. See long-lived transactions under @ref caveats_sec.
  664. * @param[in] env An environment handle returned by #mdb_env_create(). It
  665. * must have already been opened successfully.
  666. * @param[in] fd The filedescriptor to write the copy to. It must
  667. * have already been opened for Write access.
  668. * @param[in] flags Special options for this operation.
  669. * See #mdb_env_copy2() for options.
  670. * @return A non-zero error value on failure and 0 on success.
  671. */
  672. int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags);
  673. /** @brief Return statistics about the LMDB environment.
  674. *
  675. * @param[in] env An environment handle returned by #mdb_env_create()
  676. * @param[out] stat The address of an #MDB_stat structure
  677. * where the statistics will be copied
  678. */
  679. int mdb_env_stat(MDB_env *env, MDB_stat *stat);
  680. /** @brief Return information about the LMDB environment.
  681. *
  682. * @param[in] env An environment handle returned by #mdb_env_create()
  683. * @param[out] stat The address of an #MDB_envinfo structure
  684. * where the information will be copied
  685. */
  686. int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
  687. /** @brief Flush the data buffers to disk.
  688. *
  689. * Data is always written to disk when #mdb_txn_commit() is called,
  690. * but the operating system may keep it buffered. LMDB always flushes
  691. * the OS buffers upon commit as well, unless the environment was
  692. * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC. This call is
  693. * not valid if the environment was opened with #MDB_RDONLY.
  694. * @param[in] env An environment handle returned by #mdb_env_create()
  695. * @param[in] force If non-zero, force a synchronous flush. Otherwise
  696. * if the environment has the #MDB_NOSYNC flag set the flushes
  697. * will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
  698. * @return A non-zero error value on failure and 0 on success. Some possible
  699. * errors are:
  700. * <ul>
  701. * <li>EACCES - the environment is read-only.
  702. * <li>EINVAL - an invalid parameter was specified.
  703. * <li>EIO - an error occurred during synchronization.
  704. * </ul>
  705. */
  706. int mdb_env_sync(MDB_env *env, int force);
  707. /** @brief Close the environment and release the memory map.
  708. *
  709. * Only a single thread may call this function. All transactions, databases,
  710. * and cursors must already be closed before calling this function. Attempts to
  711. * use any such handles after calling this function will cause a SIGSEGV.
  712. * The environment handle will be freed and must not be used again after this call.
  713. * @param[in] env An environment handle returned by #mdb_env_create()
  714. */
  715. void mdb_env_close(MDB_env *env);
  716. /** @brief Set environment flags.
  717. *
  718. * This may be used to set some flags in addition to those from
  719. * #mdb_env_open(), or to unset these flags. If several threads
  720. * change the flags at the same time, the result is undefined.
  721. * @param[in] env An environment handle returned by #mdb_env_create()
  722. * @param[in] flags The flags to change, bitwise OR'ed together
  723. * @param[in] onoff A non-zero value sets the flags, zero clears them.
  724. * @return A non-zero error value on failure and 0 on success. Some possible
  725. * errors are:
  726. * <ul>
  727. * <li>EINVAL - an invalid parameter was specified.
  728. * </ul>
  729. */
  730. int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
  731. /** @brief Get environment flags.
  732. *
  733. * @param[in] env An environment handle returned by #mdb_env_create()
  734. * @param[out] flags The address of an integer to store the flags
  735. * @return A non-zero error value on failure and 0 on success. Some possible
  736. * errors are:
  737. * <ul>
  738. * <li>EINVAL - an invalid parameter was specified.
  739. * </ul>
  740. */
  741. int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
  742. /** @brief Return the path that was used in #mdb_env_open().
  743. *
  744. * @param[in] env An environment handle returned by #mdb_env_create()
  745. * @param[out] path Address of a string pointer to contain the path. This
  746. * is the actual string in the environment, not a copy. It should not be
  747. * altered in any way.
  748. * @return A non-zero error value on failure and 0 on success. Some possible
  749. * errors are:
  750. * <ul>
  751. * <li>EINVAL - an invalid parameter was specified.
  752. * </ul>
  753. */
  754. int mdb_env_get_path(MDB_env *env, const char **path);
  755. /** @brief Return the filedescriptor for the given environment.
  756. *
  757. * This function may be called after fork(), so the descriptor can be
  758. * closed before exec*(). Other LMDB file descriptors have FD_CLOEXEC.
  759. * (Until LMDB 0.9.18, only the lockfile had that.)
  760. *
  761. * @param[in] env An environment handle returned by #mdb_env_create()
  762. * @param[out] fd Address of a mdb_filehandle_t to contain the descriptor.
  763. * @return A non-zero error value on failure and 0 on success. Some possible
  764. * errors are:
  765. * <ul>
  766. * <li>EINVAL - an invalid parameter was specified.
  767. * </ul>
  768. */
  769. int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd);
  770. /** @brief Set the size of the memory map to use for this environment.
  771. *
  772. * The size should be a multiple of the OS page size. The default is
  773. * 10485760 bytes. The size of the memory map is also the maximum size
  774. * of the database. The value should be chosen as large as possible,
  775. * to accommodate future growth of the database.
  776. * This function should be called after #mdb_env_create() and before #mdb_env_open().
  777. * It may be called at later times if no transactions are active in
  778. * this process. Note that the library does not check for this condition,
  779. * the caller must ensure it explicitly.
  780. *
  781. * The new size takes effect immediately for the current process but
  782. * will not be persisted to any others until a write transaction has been
  783. * committed by the current process. Also, only mapsize increases are
  784. * persisted into the environment.
  785. *
  786. * If the mapsize is increased by another process, and data has grown
  787. * beyond the range of the current mapsize, #mdb_txn_begin() will
  788. * return #MDB_MAP_RESIZED. This function may be called with a size
  789. * of zero to adopt the new size.
  790. *
  791. * Any attempt to set a size smaller than the space already consumed
  792. * by the environment will be silently changed to the current size of the used space.
  793. * @param[in] env An environment handle returned by #mdb_env_create()
  794. * @param[in] size The size in bytes
  795. * @return A non-zero error value on failure and 0 on success. Some possible
  796. * errors are:
  797. * <ul>
  798. * <li>EINVAL - an invalid parameter was specified, or the environment has
  799. * an active write transaction.
  800. * </ul>
  801. */
  802. int mdb_env_set_mapsize(MDB_env *env, size_t size);
  803. /** @brief Set the maximum number of threads/reader slots for the environment.
  804. *
  805. * This defines the number of slots in the lock table that is used to track readers in the
  806. * the environment. The default is 126.
  807. * Starting a read-only transaction normally ties a lock table slot to the
  808. * current thread until the environment closes or the thread exits. If
  809. * MDB_NOTLS is in use, #mdb_txn_begin() instead ties the slot to the
  810. * MDB_txn object until it or the #MDB_env object is destroyed.
  811. * This function may only be called after #mdb_env_create() and before #mdb_env_open().
  812. * @param[in] env An environment handle returned by #mdb_env_create()
  813. * @param[in] readers The maximum number of reader lock table slots
  814. * @return A non-zero error value on failure and 0 on success. Some possible
  815. * errors are:
  816. * <ul>
  817. * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
  818. * </ul>
  819. */
  820. int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
  821. /** @brief Get the maximum number of threads/reader slots for the environment.
  822. *
  823. * @param[in] env An environment handle returned by #mdb_env_create()
  824. * @param[out] readers Address of an integer to store the number of readers
  825. * @return A non-zero error value on failure and 0 on success. Some possible
  826. * errors are:
  827. * <ul>
  828. * <li>EINVAL - an invalid parameter was specified.
  829. * </ul>
  830. */
  831. int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
  832. /** @brief Set the maximum number of named databases for the environment.
  833. *
  834. * This function is only needed if multiple databases will be used in the
  835. * environment. Simpler applications that use the environment as a single
  836. * unnamed database can ignore this option.
  837. * This function may only be called after #mdb_env_create() and before #mdb_env_open().
  838. *
  839. * Currently a moderate number of slots are cheap but a huge number gets
  840. * expensive: 7-120 words per transaction, and every #mdb_dbi_open()
  841. * does a linear search of the opened slots.
  842. * @param[in] env An environment handle returned by #mdb_env_create()
  843. * @param[in] dbs The maximum number of databases
  844. * @return A non-zero error value on failure and 0 on success. Some possible
  845. * errors are:
  846. * <ul>
  847. * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
  848. * </ul>
  849. */
  850. int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
  851. /** @brief Get the maximum size of keys and #MDB_DUPSORT data we can write.
  852. *
  853. * Depends on the compile-time constant #MDB_MAXKEYSIZE. Default 511.
  854. * See @ref MDB_val.
  855. * @param[in] env An environment handle returned by #mdb_env_create()
  856. * @return The maximum size of a key we can write
  857. */
  858. int mdb_env_get_maxkeysize(MDB_env *env);
  859. /** @brief Set application information associated with the #MDB_env.
  860. *
  861. * @param[in] env An environment handle returned by #mdb_env_create()
  862. * @param[in] ctx An arbitrary pointer for whatever the application needs.
  863. * @return A non-zero error value on failure and 0 on success.
  864. */
  865. int mdb_env_set_userctx(MDB_env *env, void *ctx);
  866. /** @brief Get the application information associated with the #MDB_env.
  867. *
  868. * @param[in] env An environment handle returned by #mdb_env_create()
  869. * @return The pointer set by #mdb_env_set_userctx().
  870. */
  871. void *mdb_env_get_userctx(MDB_env *env);
  872. /** @brief A callback function for most LMDB assert() failures,
  873. * called before printing the message and aborting.
  874. *
  875. * @param[in] env An environment handle returned by #mdb_env_create().
  876. * @param[in] msg The assertion message, not including newline.
  877. */
  878. typedef void MDB_assert_func(MDB_env *env, const char *msg);
  879. /** Set or reset the assert() callback of the environment.
  880. * Disabled if liblmdb is built with NDEBUG.
  881. * @note This hack should become obsolete as lmdb's error handling matures.
  882. * @param[in] env An environment handle returned by #mdb_env_create().
  883. * @param[in] func An #MDB_assert_func function, or 0.
  884. * @return A non-zero error value on failure and 0 on success.
  885. */
  886. int mdb_env_set_assert(MDB_env *env, MDB_assert_func *func);
  887. /** @brief Create a transaction for use with the environment.
  888. *
  889. * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
  890. * @note A transaction and its cursors must only be used by a single
  891. * thread, and a thread may only have a single transaction at a time.
  892. * If #MDB_NOTLS is in use, this does not apply to read-only transactions.
  893. * @note Cursors may not span transactions.
  894. * @param[in] env An environment handle returned by #mdb_env_create()
  895. * @param[in] parent If this parameter is non-NULL, the new transaction
  896. * will be a nested transaction, with the transaction indicated by \b parent
  897. * as its parent. Transactions may be nested to any level. A parent
  898. * transaction and its cursors may not issue any other operations than
  899. * mdb_txn_commit and mdb_txn_abort while it has active child transactions.
  900. * @param[in] flags Special options for this transaction. This parameter
  901. * must be set to 0 or by bitwise OR'ing together one or more of the
  902. * values described here.
  903. * <ul>
  904. * <li>#MDB_RDONLY
  905. * This transaction will not perform any write operations.
  906. * </ul>
  907. * @param[out] txn Address where the new #MDB_txn handle will be stored
  908. * @return A non-zero error value on failure and 0 on success. Some possible
  909. * errors are:
  910. * <ul>
  911. * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
  912. * must be shut down.
  913. * <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
  914. * mapsize and this environment's map must be resized as well.
  915. * See #mdb_env_set_mapsize().
  916. * <li>#MDB_READERS_FULL - a read-only transaction was requested and
  917. * the reader lock table is full. See #mdb_env_set_maxreaders().
  918. * <li>ENOMEM - out of memory.
  919. * </ul>
  920. */
  921. int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
  922. /** @brief Returns the transaction's #MDB_env
  923. *
  924. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  925. */
  926. MDB_env *mdb_txn_env(MDB_txn *txn);
  927. /** @brief Return the transaction's ID.
  928. *
  929. * This returns the identifier associated with this transaction. For a
  930. * read-only transaction, this corresponds to the snapshot being read;
  931. * concurrent readers will frequently have the same transaction ID.
  932. *
  933. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  934. * @return A transaction ID, valid if input is an active transaction.
  935. */
  936. size_t mdb_txn_id(MDB_txn *txn);
  937. /** @brief Commit all the operations of a transaction into the database.
  938. *
  939. * The transaction handle is freed. It and its cursors must not be used
  940. * again after this call, except with #mdb_cursor_renew().
  941. * @note Earlier documentation incorrectly said all cursors would be freed.
  942. * Only write-transactions free cursors.
  943. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  944. * @return A non-zero error value on failure and 0 on success. Some possible
  945. * errors are:
  946. * <ul>
  947. * <li>EINVAL - an invalid parameter was specified.
  948. * <li>ENOSPC - no more disk space.
  949. * <li>EIO - a low-level I/O error occurred while writing.
  950. * <li>ENOMEM - out of memory.
  951. * </ul>
  952. */
  953. int mdb_txn_commit(MDB_txn *txn);
  954. /** @brief Abandon all the operations of the transaction instead of saving them.
  955. *
  956. * The transaction handle is freed. It and its cursors must not be used
  957. * again after this call, except with #mdb_cursor_renew().
  958. * @note Earlier documentation incorrectly said all cursors would be freed.
  959. * Only write-transactions free cursors.
  960. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  961. */
  962. void mdb_txn_abort(MDB_txn *txn);
  963. /** @brief Reset a read-only transaction.
  964. *
  965. * Abort the transaction like #mdb_txn_abort(), but keep the transaction
  966. * handle. #mdb_txn_renew() may reuse the handle. This saves allocation
  967. * overhead if the process will start a new read-only transaction soon,
  968. * and also locking overhead if #MDB_NOTLS is in use. The reader table
  969. * lock is released, but the table slot stays tied to its thread or
  970. * #MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free
  971. * its lock table slot if MDB_NOTLS is in use.
  972. * Cursors opened within the transaction must not be used
  973. * again after this call, except with #mdb_cursor_renew().
  974. * Reader locks generally don't interfere with writers, but they keep old
  975. * versions of database pages allocated. Thus they prevent the old pages
  976. * from being reused when writers commit new data, and so under heavy load
  977. * the database size may grow much more rapidly than otherwise.
  978. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  979. */
  980. void mdb_txn_reset(MDB_txn *txn);
  981. /** @brief Renew a read-only transaction.
  982. *
  983. * This acquires a new reader lock for a transaction handle that had been
  984. * released by #mdb_txn_reset(). It must be called before a reset transaction
  985. * may be used again.
  986. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  987. * @return A non-zero error value on failure and 0 on success. Some possible
  988. * errors are:
  989. * <ul>
  990. * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
  991. * must be shut down.
  992. * <li>EINVAL - an invalid parameter was specified.
  993. * </ul>
  994. */
  995. int mdb_txn_renew(MDB_txn *txn);
  996. /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
  997. #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
  998. /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
  999. #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
  1000. /** @brief Open a database in the environment.
  1001. *
  1002. * A database handle denotes the name and parameters of a database,
  1003. * independently of whether such a database exists.
  1004. * The database handle may be discarded by calling #mdb_dbi_close().
  1005. * The old database handle is returned if the database was already open.
  1006. * The handle may only be closed once.
  1007. *
  1008. * The database handle will be private to the current transaction until
  1009. * the transaction is successfully committed. If the transaction is
  1010. * aborted the handle will be closed automatically.
  1011. * After a successful commit the handle will reside in the shared
  1012. * environment, and may be used by other transactions.
  1013. *
  1014. * This function must not be called from multiple concurrent
  1015. * transactions in the same process. A transaction that uses
  1016. * this function must finish (either commit or abort) before
  1017. * any other transaction in the process may use this function.
  1018. *
  1019. * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
  1020. * must be called before opening the environment. Database names are
  1021. * keys in the unnamed database, and may be read but not written.
  1022. *
  1023. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1024. * @param[in] name The name of the database to open. If only a single
  1025. * database is needed in the environment, this value may be NULL.
  1026. * @param[in] flags Special options for this database. This parameter
  1027. * must be set to 0 or by bitwise OR'ing together one or more of the
  1028. * values described here.
  1029. * <ul>
  1030. * <li>#MDB_REVERSEKEY
  1031. * Keys are strings to be compared in reverse order, from the end
  1032. * of the strings to the beginning. By default, Keys are treated as strings and
  1033. * compared from beginning to end.
  1034. * <li>#MDB_DUPSORT
  1035. * Duplicate keys may be used in the database. (Or, from another perspective,
  1036. * keys may have multiple data items, stored in sorted order.) By default
  1037. * keys must be unique and may have only a single data item.
  1038. * <li>#MDB_INTEGERKEY
  1039. * Keys are binary integers in native byte order, either unsigned int
  1040. * or size_t, and will be sorted as such.
  1041. * The keys must all be of the same size.
  1042. * <li>#MDB_DUPFIXED
  1043. * This flag may only be used in combination with #MDB_DUPSORT. This option
  1044. * tells the library that the data items for this database are all the same
  1045. * size, which allows further optimizations in storage and retrieval. When
  1046. * all data items are the same size, the #MDB_GET_MULTIPLE, #MDB_NEXT_MULTIPLE
  1047. * and #MDB_PREV_MULTIPLE cursor operations may be used to retrieve multiple
  1048. * items at once.
  1049. * <li>#MDB_INTEGERDUP
  1050. * This option specifies that duplicate data items are binary integers,
  1051. * similar to #MDB_INTEGERKEY keys.
  1052. * <li>#MDB_REVERSEDUP
  1053. * This option specifies that duplicate data items should be compared as
  1054. * strings in reverse order.
  1055. * <li>#MDB_CREATE
  1056. * Create the named database if it doesn't exist. This option is not
  1057. * allowed in a read-only transaction or a read-only environment.
  1058. * </ul>
  1059. * @param[out] dbi Address where the new #MDB_dbi handle will be stored
  1060. * @return A non-zero error value on failure and 0 on success. Some possible
  1061. * errors are:
  1062. * <ul>
  1063. * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
  1064. * and #MDB_CREATE was not specified.
  1065. * <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
  1066. * </ul>
  1067. */
  1068. int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
  1069. /** @brief Retrieve statistics for a database.
  1070. *
  1071. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1072. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1073. * @param[out] stat The address of an #MDB_stat structure
  1074. * where the statistics will be copied
  1075. * @return A non-zero error value on failure and 0 on success. Some possible
  1076. * errors are:
  1077. * <ul>
  1078. * <li>EINVAL - an invalid parameter was specified.
  1079. * </ul>
  1080. */
  1081. int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
  1082. /** @brief Retrieve the DB flags for a database handle.
  1083. *
  1084. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1085. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1086. * @param[out] flags Address where the flags will be returned.
  1087. * @return A non-zero error value on failure and 0 on success.
  1088. */
  1089. int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags);
  1090. /** @brief Close a database handle. Normally unnecessary. Use with care:
  1091. *
  1092. * This call is not mutex protected. Handles should only be closed by
  1093. * a single thread, and only if no other threads are going to reference
  1094. * the database handle or one of its cursors any further. Do not close
  1095. * a handle if an existing transaction has modified its database.
  1096. * Doing so can cause misbehavior from database corruption to errors
  1097. * like MDB_BAD_VALSIZE (since the DB name is gone).
  1098. *
  1099. * Closing a database handle is not necessary, but lets #mdb_dbi_open()
  1100. * reuse the handle value. Usually it's better to set a bigger
  1101. * #mdb_env_set_maxdbs(), unless that value would be large.
  1102. *
  1103. * @param[in] env An environment handle returned by #mdb_env_create()
  1104. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1105. */
  1106. void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
  1107. /** @brief Empty or delete+close a database.
  1108. *
  1109. * See #mdb_dbi_close() for restrictions about closing the DB handle.
  1110. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1111. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1112. * @param[in] del 0 to empty the DB, 1 to delete it from the
  1113. * environment and close the DB handle.
  1114. * @return A non-zero error value on failure and 0 on success.
  1115. */
  1116. int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
  1117. /** @brief Set a custom key comparison function for a database.
  1118. *
  1119. * The comparison function is called whenever it is necessary to compare a
  1120. * key specified by the application with a key currently stored in the database.
  1121. * If no comparison function is specified, and no special key flags were specified
  1122. * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
  1123. * before longer keys.
  1124. * @warning This function must be called before any data access functions are used,
  1125. * otherwise data corruption may occur. The same comparison function must be used by every
  1126. * program accessing the database, every time the database is used.
  1127. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1128. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1129. * @param[in] cmp A #MDB_cmp_func function
  1130. * @return A non-zero error value on failure and 0 on success. Some possible
  1131. * errors are:
  1132. * <ul>
  1133. * <li>EINVAL - an invalid parameter was specified.
  1134. * </ul>
  1135. */
  1136. int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
  1137. /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
  1138. *
  1139. * This comparison function is called whenever it is necessary to compare a data
  1140. * item specified by the application with a data item currently stored in the database.
  1141. * This function only takes effect if the database was opened with the #MDB_DUPSORT
  1142. * flag.
  1143. * If no comparison function is specified, and no special key flags were specified
  1144. * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
  1145. * before longer items.
  1146. * @warning This function must be called before any data access functions are used,
  1147. * otherwise data corruption may occur. The same comparison function must be used by every
  1148. * program accessing the database, every time the database is used.
  1149. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1150. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1151. * @param[in] cmp A #MDB_cmp_func function
  1152. * @return A non-zero error value on failure and 0 on success. Some possible
  1153. * errors are:
  1154. * <ul>
  1155. * <li>EINVAL - an invalid parameter was specified.
  1156. * </ul>
  1157. */
  1158. int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
  1159. /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
  1160. *
  1161. * @todo The relocation function is called whenever it is necessary to move the data
  1162. * of an item to a different position in the database (e.g. through tree
  1163. * balancing operations, shifts as a result of adds or deletes, etc.). It is
  1164. * intended to allow address/position-dependent data items to be stored in
  1165. * a database in an environment opened with the #MDB_FIXEDMAP option.
  1166. * Currently the relocation feature is unimplemented and setting
  1167. * this function has no effect.
  1168. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1169. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1170. * @param[in] rel A #MDB_rel_func function
  1171. * @return A non-zero error value on failure and 0 on success. Some possible
  1172. * errors are:
  1173. * <ul>
  1174. * <li>EINVAL - an invalid parameter was specified.
  1175. * </ul>
  1176. */
  1177. int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
  1178. /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
  1179. *
  1180. * See #mdb_set_relfunc and #MDB_rel_func for more details.
  1181. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1182. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1183. * @param[in] ctx An arbitrary pointer for whatever the application needs.
  1184. * It will be passed to the callback function set by #mdb_set_relfunc
  1185. * as its \b relctx parameter whenever the callback is invoked.
  1186. * @return A non-zero error value on failure and 0 on success. Some possible
  1187. * errors are:
  1188. * <ul>
  1189. * <li>EINVAL - an invalid parameter was specified.
  1190. * </ul>
  1191. */
  1192. int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
  1193. /** @brief Get items from a database.
  1194. *
  1195. * This function retrieves key/data pairs from the database. The address
  1196. * and length of the data associated with the specified \b key are returned
  1197. * in the structure to which \b data refers.
  1198. * If the database supports duplicate keys (#MDB_DUPSORT) then the
  1199. * first data item for the key will be returned. Retrieval of other
  1200. * items requires the use of #mdb_cursor_get().
  1201. *
  1202. * @note The memory pointed to by the returned values is owned by the
  1203. * database. The caller need not dispose of the memory, and may not
  1204. * modify it in any way. For values returned in a read-only transaction
  1205. * any modification attempts will cause a SIGSEGV.
  1206. * @note Values returned from the database are valid only until a
  1207. * subsequent update operation, or the end of the transaction.
  1208. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1209. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1210. * @param[in] key The key to search for in the database
  1211. * @param[out] data The data corresponding to the key
  1212. * @return A non-zero error value on failure and 0 on success. Some possible
  1213. * errors are:
  1214. * <ul>
  1215. * <li>#MDB_NOTFOUND - the key was not in the database.
  1216. * <li>EINVAL - an invalid parameter was specified.
  1217. * </ul>
  1218. */
  1219. int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
  1220. /** @brief Store items into a database.
  1221. *
  1222. * This function stores key/data pairs in the database. The default behavior
  1223. * is to enter the new key/data pair, replacing any previously existing key
  1224. * if duplicates are disallowed, or adding a duplicate data item if
  1225. * duplicates are allowed (#MDB_DUPSORT).
  1226. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1227. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1228. * @param[in] key The key to store in the database
  1229. * @param[in,out] data The data to store
  1230. * @param[in] flags Special options for this operation. This parameter
  1231. * must be set to 0 or by bitwise OR'ing together one or more of the
  1232. * values described here.
  1233. * <ul>
  1234. * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
  1235. * already appear in the database. This flag may only be specified
  1236. * if the database was opened with #MDB_DUPSORT. The function will
  1237. * return #MDB_KEYEXIST if the key/data pair already appears in the
  1238. * database.
  1239. * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
  1240. * does not already appear in the database. The function will return
  1241. * #MDB_KEYEXIST if the key already appears in the database, even if
  1242. * the database supports duplicates (#MDB_DUPSORT). The \b data
  1243. * parameter will be set to point to the existing item.
  1244. * <li>#MDB_RESERVE - reserve space for data of the given size, but
  1245. * don't copy the given data. Instead, return a pointer to the
  1246. * reserved space, which the caller can fill in later - before
  1247. * the next update operation or the transaction ends. This saves
  1248. * an extra memcpy if the data is being generated later.
  1249. * LMDB does nothing else with this memory, the caller is expected
  1250. * to modify all of the space requested. This flag must not be
  1251. * specified if the database was opened with #MDB_DUPSORT.
  1252. * <li>#MDB_APPEND - append the given key/data pair to the end of the
  1253. * database. This option allows fast bulk loading when keys are
  1254. * already known to be in the correct order. Loading unsorted keys
  1255. * with this flag will cause a #MDB_KEYEXIST error.
  1256. * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
  1257. * </ul>
  1258. * @return A non-zero error value on failure and 0 on success. Some possible
  1259. * errors are:
  1260. * <ul>
  1261. * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
  1262. * <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
  1263. * <li>EACCES - an attempt was made to write in a read-only transaction.
  1264. * <li>EINVAL - an invalid parameter was specified.
  1265. * </ul>
  1266. */
  1267. int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
  1268. unsigned int flags);
  1269. /** @brief Delete items from a database.
  1270. *
  1271. * This function removes key/data pairs from the database.
  1272. * If the database does not support sorted duplicate data items
  1273. * (#MDB_DUPSORT) the data parameter is ignored.
  1274. * If the database supports sorted duplicates and the data parameter
  1275. * is NULL, all of the duplicate data items for the key will be
  1276. * deleted. Otherwise, if the data parameter is non-NULL
  1277. * only the matching data item will be deleted.
  1278. * This function will return #MDB_NOTFOUND if the specified key/data
  1279. * pair is not in the database.
  1280. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1281. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1282. * @param[in] key The key to delete from the database
  1283. * @param[in] data The data to delete
  1284. * @return A non-zero error value on failure and 0 on success. Some possible
  1285. * errors are:
  1286. * <ul>
  1287. * <li>EACCES - an attempt was made to write in a read-only transaction.
  1288. * <li>EINVAL - an invalid parameter was specified.
  1289. * </ul>
  1290. */
  1291. int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
  1292. /** @brief Create a cursor handle.
  1293. *
  1294. * A cursor is associated with a specific transaction and database.
  1295. * A cursor cannot be used when its database handle is closed. Nor
  1296. * when its transaction has ended, except with #mdb_cursor_renew().
  1297. * It can be discarded with #mdb_cursor_close().
  1298. * A cursor in a write-transaction can be closed before its transaction
  1299. * ends, and will otherwise be closed when its transaction ends.
  1300. * A cursor in a read-only transaction must be closed explicitly, before
  1301. * or after its transaction ends. It can be reused with
  1302. * #mdb_cursor_renew() before finally closing it.
  1303. * @note Earlier documentation said that cursors in every transaction
  1304. * were closed when the transaction committed or aborted.
  1305. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1306. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1307. * @param[out] cursor Address where the new #MDB_cursor handle will be stored
  1308. * @return A non-zero error value on failure and 0 on success. Some possible
  1309. * errors are:
  1310. * <ul>
  1311. * <li>EINVAL - an invalid parameter was specified.
  1312. * </ul>
  1313. */
  1314. int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
  1315. /** @brief Close a cursor handle.
  1316. *
  1317. * The cursor handle will be freed and must not be used again after this call.
  1318. * Its transaction must still be live if it is a write-transaction.
  1319. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1320. */
  1321. void mdb_cursor_close(MDB_cursor *cursor);
  1322. /** @brief Renew a cursor handle.
  1323. *
  1324. * A cursor is associated with a specific transaction and database.
  1325. * Cursors that are only used in read-only
  1326. * transactions may be re-used, to avoid unnecessary malloc/free overhead.
  1327. * The cursor may be associated with a new read-only transaction, and
  1328. * referencing the same database handle as it was created with.
  1329. * This may be done whether the previous transaction is live or dead.
  1330. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1331. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1332. * @return A non-zero error value on failure and 0 on success. Some possible
  1333. * errors are:
  1334. * <ul>
  1335. * <li>EINVAL - an invalid parameter was specified.
  1336. * </ul>
  1337. */
  1338. int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
  1339. /** @brief Return the cursor's transaction handle.
  1340. *
  1341. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1342. */
  1343. MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
  1344. /** @brief Return the cursor's database handle.
  1345. *
  1346. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1347. */
  1348. MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
  1349. /** @brief Retrieve by cursor.
  1350. *
  1351. * This function retrieves key/data pairs from the database. The address and length
  1352. * of the key are returned in the object to which \b key refers (except for the
  1353. * case of the #MDB_SET option, in which the \b key object is unchanged), and
  1354. * the address and length of the data are returned in the object to which \b data
  1355. * refers.
  1356. * See #mdb_get() for restrictions on using the output values.
  1357. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1358. * @param[in,out] key The key for a retrieved item
  1359. * @param[in,out] data The data of a retrieved item
  1360. * @param[in] op A cursor operation #MDB_cursor_op
  1361. * @return A non-zero error value on failure and 0 on success. Some possible
  1362. * errors are:
  1363. * <ul>
  1364. * <li>#MDB_NOTFOUND - no matching key found.
  1365. * <li>EINVAL - an invalid parameter was specified.
  1366. * </ul>
  1367. */
  1368. int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
  1369. MDB_cursor_op op);
  1370. /** @brief Store by cursor.
  1371. *
  1372. * This function stores key/data pairs into the database.
  1373. * The cursor is positioned at the new item, or on failure usually near it.
  1374. * @note Earlier documentation incorrectly said errors would leave the
  1375. * state of the cursor unchanged.
  1376. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1377. * @param[in] key The key operated on.
  1378. * @param[in] data The data operated on.
  1379. * @param[in] flags Options for this operation. This parameter
  1380. * must be set to 0 or one of the values described here.
  1381. * <ul>
  1382. * <li>#MDB_CURRENT - replace the item at the current cursor position.
  1383. * The \b key parameter must still be provided, and must match it.
  1384. * If using sorted duplicates (#MDB_DUPSORT) the data item must still
  1385. * sort into the same place. This is intended to be used when the
  1386. * new data is the same size as the old. Otherwise it will simply
  1387. * perform a delete of the old record followed by an insert.
  1388. * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
  1389. * already appear in the database. This flag may only be specified
  1390. * if the database was opened with #MDB_DUPSORT. The function will
  1391. * return #MDB_KEYEXIST if the key/data pair already appears in the
  1392. * database.
  1393. * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
  1394. * does not already appear in the database. The function will return
  1395. * #MDB_KEYEXIST if the key already appears in the database, even if
  1396. * the database supports duplicates (#MDB_DUPSORT).
  1397. * <li>#MDB_RESERVE - reserve space for data of the given size, but
  1398. * don't copy the given data. Instead, return a pointer to the
  1399. * reserved space, which the caller can fill in later - before
  1400. * the next update operation or the transaction ends. This saves
  1401. * an extra memcpy if the data is being generated later. This flag
  1402. * must not be specified if the database was opened with #MDB_DUPSORT.
  1403. * <li>#MDB_APPEND - append the given key/data pair to the end of the
  1404. * database. No key comparisons are performed. This option allows
  1405. * fast bulk loading when keys are already known to be in the
  1406. * correct order. Loading unsorted keys with this flag will cause
  1407. * a #MDB_KEYEXIST error.
  1408. * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
  1409. * <li>#MDB_MULTIPLE - store multiple contiguous data elements in a
  1410. * single request. This flag may only be specified if the database
  1411. * was opened with #MDB_DUPFIXED. The \b data argument must be an
  1412. * array of two MDB_vals. The mv_size of the first MDB_val must be
  1413. * the size of a single data element. The mv_data of the first MDB_val
  1414. * must point to the beginning of the array of contiguous data elements.
  1415. * The mv_size of the second MDB_val must be the count of the number
  1416. * of data elements to store. On return this field will be set to
  1417. * the count of the number of elements actually written. The mv_data
  1418. * of the second MDB_val is unused.
  1419. * </ul>
  1420. * @return A non-zero error value on failure and 0 on success. Some possible
  1421. * errors are:
  1422. * <ul>
  1423. * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
  1424. * <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
  1425. * <li>EACCES - an attempt was made to write in a read-only transaction.
  1426. * <li>EINVAL - an invalid parameter was specified.
  1427. * </ul>
  1428. */
  1429. int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
  1430. unsigned int flags);
  1431. /** @brief Delete current key/data pair
  1432. *
  1433. * This function deletes the key/data pair to which the cursor refers.
  1434. * This does not invalidate the cursor, so operations such as MDB_NEXT
  1435. * can still be used on it.
  1436. * Both MDB_NEXT and MDB_GET_CURRENT will return the same record after
  1437. * this operation.
  1438. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1439. * @param[in] flags Options for this operation. This parameter
  1440. * must be set to 0 or one of the values described here.
  1441. * <ul>
  1442. * <li>#MDB_NODUPDATA - delete all of the data items for the current key.
  1443. * This flag may only be specified if the database was opened with #MDB_DUPSORT.
  1444. * </ul>
  1445. * @return A non-zero error value on failure and 0 on success. Some possible
  1446. * errors are:
  1447. * <ul>
  1448. * <li>EACCES - an attempt was made to write in a read-only transaction.
  1449. * <li>EINVAL - an invalid parameter was specified.
  1450. * </ul>
  1451. */
  1452. int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
  1453. /** @brief Return count of duplicates for current key.
  1454. *
  1455. * This call is only valid on databases that support sorted duplicate
  1456. * data items #MDB_DUPSORT.
  1457. * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
  1458. * @param[out] countp Address where the count will be stored
  1459. * @return A non-zero error value on failure and 0 on success. Some possible
  1460. * errors are:
  1461. * <ul>
  1462. * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
  1463. * </ul>
  1464. */
  1465. int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
  1466. /** @brief Compare two data items according to a particular database.
  1467. *
  1468. * This returns a comparison as if the two data items were keys in the
  1469. * specified database.
  1470. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1471. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1472. * @param[in] a The first item to compare
  1473. * @param[in] b The second item to compare
  1474. * @return < 0 if a < b, 0 if a == b, > 0 if a > b
  1475. */
  1476. int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
  1477. /** @brief Compare two data items according to a particular database.
  1478. *
  1479. * This returns a comparison as if the two items were data items of
  1480. * the specified database. The database must have the #MDB_DUPSORT flag.
  1481. * @param[in] txn A transaction handle returned by #mdb_txn_begin()
  1482. * @param[in] dbi A database handle returned by #mdb_dbi_open()
  1483. * @param[in] a The first item to compare
  1484. * @param[in] b The second item to compare
  1485. * @return < 0 if a < b, 0 if a == b, > 0 if a > b
  1486. */
  1487. int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
  1488. /** @brief A callback function used to print a message from the library.
  1489. *
  1490. * @param[in] msg The string to be printed.
  1491. * @param[in] ctx An arbitrary context pointer for the callback.
  1492. * @return < 0 on failure, >= 0 on success.
  1493. */
  1494. typedef int (MDB_msg_func)(const char *msg, void *ctx);
  1495. /** @brief Dump the entries in the reader lock table.
  1496. *
  1497. * @param[in] env An environment handle returned by #mdb_env_create()
  1498. * @param[in] func A #MDB_msg_func function
  1499. * @param[in] ctx Anything the message function needs
  1500. * @return < 0 on failure, >= 0 on success.
  1501. */
  1502. int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
  1503. /** @brief Check for stale entries in the reader lock table.
  1504. *
  1505. * @param[in] env An environment handle returned by #mdb_env_create()
  1506. * @param[out] dead Number of stale slots that were cleared
  1507. * @return 0 on success, non-zero on failure.
  1508. */
  1509. int mdb_reader_check(MDB_env *env, int *dead);
  1510. /** @} */
  1511. #ifdef __cplusplus
  1512. }
  1513. #endif
  1514. /** @page tools LMDB Command Line Tools
  1515. The following describes the command line tools that are available for LMDB.
  1516. \li \ref mdb_copy_1
  1517. \li \ref mdb_dump_1
  1518. \li \ref mdb_load_1
  1519. \li \ref mdb_stat_1
  1520. */
  1521. #endif /* _LMDB_H_ */