2 * @brief Lightning memory-mapped database library
4 * @mainpage Lightning Memory-Mapped Database Manager (MDB)
6 * @section intro_sec Introduction
7 * MDB 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
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.
28 * Unlike other well-known database mechanisms which use either write-ahead
29 * transaction logs or append-only data writes, MDB 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. MDB 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.
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.
43 * @section caveats_sec Caveats
44 * Troubleshooting the lock file, plus semaphores on BSD systems:
46 * - A broken lockfile can cause sync issues.
47 * Stale reader transactions left behind by an aborted program
48 * cause further writes to grow the database quickly, and
49 * stale locks can block further operation.
51 * Fix: Terminate all programs using the database, or make
52 * them close it. Next database user will reset the lockfile.
54 * - On BSD systems or others configured with MDB_USE_POSIX_SEM,
55 * startup can fail due to semaphores owned by another userid.
57 * Fix: Open and close the database as the user which owns the
58 * semaphores (likely last user) or as root, while no other
59 * process is using the database.
61 * Restrictions/caveats (in addition to those listed for some functions):
63 * - Only the database owner should normally use the database on
64 * BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
65 * Multiple users can cause startup to fail later, as noted above.
67 * - A thread can only use one transaction at a time, plus any child
68 * transactions. Each transaction belongs to one thread. See below.
70 * - Use an MDB_env* in the process which opened it, without fork()ing.
72 * - Do not have open an MDB database twice in the same process at
73 * the same time. Not even from a plain open() call - close()ing it
74 * breaks flock() advisory locking.
76 * - Avoid long-lived transactions. Read transactions prevent
77 * reuse of pages freed by newer write transactions, thus the
78 * database can grow quickly. Write transactions prevent
79 * other write transactions, since writes are serialized.
81 * - Avoid suspending a process with active transactions. These
82 * would then be "long-lived" as above. Also read transactions
83 * suspended when writers commit could sometimes see wrong data.
85 * ...when several processes can use a database concurrently:
87 * - Avoid aborting a process with an active transaction.
88 * The transaction becomes "long-lived" as above until the lockfile
89 * is reset, since the process may not remove it from the lockfile.
91 * - If you do that anyway, close the environment once in a while,
92 * so the lockfile can get reset.
94 * - Do not use MDB databases on remote filesystems, even between
95 * processes on the same host. This breaks flock() on some OSes,
96 * possibly memory map sync, and certainly sync between programs
99 * - Opening a database can fail if another process is opening or
100 * closing it at exactly the same time.
102 * @author Howard Chu, Symas Corporation.
104 * @copyright Copyright 2011-2013 Howard Chu, Symas Corp. All rights reserved.
106 * Redistribution and use in source and binary forms, with or without
107 * modification, are permitted only as authorized by the OpenLDAP
110 * A copy of this license is available in the file LICENSE in the
111 * top-level directory of the distribution or, alternatively, at
112 * <http://www.OpenLDAP.org/license.html>.
115 * This code is derived from btree.c written by Martin Hedenfalk.
117 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
119 * Permission to use, copy, modify, and distribute this software for any
120 * purpose with or without fee is hereby granted, provided that the above
121 * copyright notice and this permission notice appear in all copies.
123 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
124 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
125 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
126 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
127 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
128 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
129 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
134 #include <sys/types.h>
140 /** @defgroup mdb MDB API
142 * @brief OpenLDAP Lightning Memory-Mapped Database Manager
144 /** @defgroup Version Version Macros
147 /** Library major version */
148 #define MDB_VERSION_MAJOR 0
149 /** Library minor version */
150 #define MDB_VERSION_MINOR 9
151 /** Library patch version */
152 #define MDB_VERSION_PATCH 6
154 /** Combine args a,b,c into a single integer for easy version comparisons */
155 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
157 /** The full library version as a single integer */
158 #define MDB_VERSION_FULL \
159 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
161 /** The release date of this library version */
162 #define MDB_VERSION_DATE "January 10, 2013"
164 /** A stringifier for the version info */
165 #define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" d ")"
167 /** A helper for the stringifier macro */
168 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
170 /** The full library version as a C string */
171 #define MDB_VERSION_STRING \
172 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
175 /** @brief Opaque structure for a database environment.
177 * A DB environment supports multiple databases, all residing in the same
180 typedef struct MDB_env MDB_env;
182 /** @brief Opaque structure for a transaction handle.
184 * All database operations require a transaction handle. Transactions may be
185 * read-only or read-write.
187 typedef struct MDB_txn MDB_txn;
189 /** @brief A handle for an individual database in the DB environment. */
190 typedef unsigned int MDB_dbi;
192 /** @brief Opaque structure for navigating through a database */
193 typedef struct MDB_cursor MDB_cursor;
195 /** @brief Generic structure used for passing keys and data in and out
198 * Key sizes must be between 1 and the liblmdb build-time constant
199 * #MDB_MAXKEYSIZE inclusive. This currently defaults to 511. The
200 * same applies to data sizes in databases with the #MDB_DUPSORT flag.
201 * Other data items can in theory be from 0 to 0xffffffff bytes long.
203 typedef struct MDB_val {
204 size_t mv_size; /**< size of the data item */
205 void *mv_data; /**< address of the data item */
208 /** @brief A callback function used to compare two keys in a database */
209 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
211 /** @brief A callback function used to relocate a position-dependent data item
212 * in a fixed-address database.
214 * The \b newptr gives the item's desired address in
215 * the memory map, and \b oldptr gives its previous address. The item's actual
216 * data resides at the address in \b item. This callback is expected to walk
217 * through the fields of the record in \b item and modify any
218 * values based at the \b oldptr address to be relative to the \b newptr address.
219 * @param[in,out] item The item that is to be relocated.
220 * @param[in] oldptr The previous address.
221 * @param[in] newptr The new address to relocate to.
222 * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
223 * @todo This feature is currently unimplemented.
225 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
227 /** @defgroup mdb_env Environment Flags
229 * Values do not overlap Database Flags.
232 /** mmap at a fixed address (experimental) */
233 #define MDB_FIXEDMAP 0x01
234 /** no environment directory */
235 #define MDB_NOSUBDIR 0x4000
236 /** don't fsync after commit */
237 #define MDB_NOSYNC 0x10000
239 #define MDB_RDONLY 0x20000
240 /** don't fsync metapage after commit */
241 #define MDB_NOMETASYNC 0x40000
242 /** use writable mmap */
243 #define MDB_WRITEMAP 0x80000
244 /** use asynchronous msync when MDB_WRITEMAP is used */
245 #define MDB_MAPASYNC 0x100000
248 /** @defgroup mdb_dbi_open Database Flags
250 * Values do not overlap Environment Flags.
253 /** use reverse string keys */
254 #define MDB_REVERSEKEY 0x02
255 /** use sorted duplicates */
256 #define MDB_DUPSORT 0x04
257 /** numeric keys in native byte order.
258 * The keys must all be of the same size. */
259 #define MDB_INTEGERKEY 0x08
260 /** with #MDB_DUPSORT, sorted dup items have fixed size */
261 #define MDB_DUPFIXED 0x10
262 /** with #MDB_DUPSORT, dups are numeric in native byte order */
263 #define MDB_INTEGERDUP 0x20
264 /** with #MDB_DUPSORT, use reverse string dups */
265 #define MDB_REVERSEDUP 0x40
266 /** create DB if not already existing */
267 #define MDB_CREATE 0x40000
270 /** @defgroup mdb_put Write Flags
273 /** For put: Don't write if the key already exists. */
274 #define MDB_NOOVERWRITE 0x10
275 /** Only for #MDB_DUPSORT<br>
276 * For put: don't write if the key and data pair already exist.<br>
277 * For mdb_cursor_del: remove all duplicate data items.
279 #define MDB_NODUPDATA 0x20
280 /** For mdb_cursor_put: overwrite the current key/data pair */
281 #define MDB_CURRENT 0x40
282 /** For put: Just reserve space for data, don't copy it. Return a
283 * pointer to the reserved space.
285 #define MDB_RESERVE 0x10000
286 /** Data is being appended, don't split full pages. */
287 #define MDB_APPEND 0x20000
288 /** Duplicate data is being appended, don't split full pages. */
289 #define MDB_APPENDDUP 0x40000
290 /** Store multiple data items in one call. */
291 #define MDB_MULTIPLE 0x80000
294 /** @brief Cursor Get operations.
296 * This is the set of all operations for retrieving data
299 typedef enum MDB_cursor_op {
300 MDB_FIRST, /**< Position at first key/data item */
301 MDB_FIRST_DUP, /**< Position at first data item of current key.
302 Only for #MDB_DUPSORT */
303 MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
304 MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
305 MDB_GET_CURRENT, /**< Return key/data at current cursor position */
306 MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current
307 cursor position. Only for #MDB_DUPFIXED */
308 MDB_LAST, /**< Position at last key/data item */
309 MDB_LAST_DUP, /**< Position at last data item of current key.
310 Only for #MDB_DUPSORT */
311 MDB_NEXT, /**< Position at next data item */
312 MDB_NEXT_DUP, /**< Position at next data item of current key.
313 Only for #MDB_DUPSORT */
314 MDB_NEXT_MULTIPLE, /**< Return all duplicate data items at the next
315 cursor position. Only for #MDB_DUPFIXED */
316 MDB_NEXT_NODUP, /**< Position at first data item of next key.
317 Only for #MDB_DUPSORT */
318 MDB_PREV, /**< Position at previous data item */
319 MDB_PREV_DUP, /**< Position at previous data item of current key.
320 Only for #MDB_DUPSORT */
321 MDB_PREV_NODUP, /**< Position at last data item of previous key.
322 Only for #MDB_DUPSORT */
323 MDB_SET, /**< Position at specified key */
324 MDB_SET_KEY, /**< Position at specified key, return key + data */
325 MDB_SET_RANGE /**< Position at first key greater than or equal to specified key. */
328 /** @defgroup errors Return Codes
330 * BerkeleyDB uses -30800 to -30999, we'll go under them
333 /** Successful result */
334 #define MDB_SUCCESS 0
335 /** key/data pair already exists */
336 #define MDB_KEYEXIST (-30799)
337 /** key/data pair not found (EOF) */
338 #define MDB_NOTFOUND (-30798)
339 /** Requested page not found - this usually indicates corruption */
340 #define MDB_PAGE_NOTFOUND (-30797)
341 /** Located page was wrong type */
342 #define MDB_CORRUPTED (-30796)
343 /** Update of meta page failed, probably I/O error */
344 #define MDB_PANIC (-30795)
345 /** Environment version mismatch */
346 #define MDB_VERSION_MISMATCH (-30794)
347 /** File is not a valid MDB file */
348 #define MDB_INVALID (-30793)
349 /** Environment mapsize reached */
350 #define MDB_MAP_FULL (-30792)
351 /** Environment maxdbs reached */
352 #define MDB_DBS_FULL (-30791)
353 /** Environment maxreaders reached */
354 #define MDB_READERS_FULL (-30790)
355 /** Too many TLS keys in use - Windows only */
356 #define MDB_TLS_FULL (-30789)
357 /** Txn has too many dirty pages */
358 #define MDB_TXN_FULL (-30788)
359 /** Cursor stack too deep - internal error */
360 #define MDB_CURSOR_FULL (-30787)
361 /** Page has not enough space - internal error */
362 #define MDB_PAGE_FULL (-30786)
363 /** Database contents grew beyond environment mapsize */
364 #define MDB_MAP_RESIZED (-30785)
365 #define MDB_LAST_ERRCODE MDB_MAP_RESIZED
368 /** @brief Statistics for a database in the environment */
369 typedef struct MDB_stat {
370 unsigned int ms_psize; /**< Size of a database page.
371 This is currently the same for all databases. */
372 unsigned int ms_depth; /**< Depth (height) of the B-tree */
373 size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */
374 size_t ms_leaf_pages; /**< Number of leaf pages */
375 size_t ms_overflow_pages; /**< Number of overflow pages */
376 size_t ms_entries; /**< Number of data items */
379 /** @brief Information about the environment */
380 typedef struct MDB_envinfo {
381 void *me_mapaddr; /**< Address of map, if fixed */
382 size_t me_mapsize; /**< Size of the data memory map */
383 size_t me_last_pgno; /**< ID of the last used page */
384 size_t me_last_txnid; /**< ID of the last committed transaction */
385 unsigned int me_maxreaders; /**< maximum number of threads for the environment */
386 unsigned int me_numreaders; /**< maximum number of threads used in the environment */
389 /** @brief Return the mdb library version information.
391 * @param[out] major if non-NULL, the library major version number is copied here
392 * @param[out] minor if non-NULL, the library minor version number is copied here
393 * @param[out] patch if non-NULL, the library patch version number is copied here
394 * @retval "version string" The library version as a string
396 char *mdb_version(int *major, int *minor, int *patch);
398 /** @brief Return a string describing a given error code.
400 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
401 * function. If the error code is greater than or equal to 0, then the string
402 * returned by the system function strerror(3) is returned. If the error code
403 * is less than 0, an error string corresponding to the MDB library error is
404 * returned. See @ref errors for a list of MDB-specific error codes.
405 * @param[in] err The error code
406 * @retval "error message" The description of the error
408 char *mdb_strerror(int err);
410 /** @brief Create an MDB environment handle.
412 * This function allocates memory for a #MDB_env structure. To release
413 * the allocated memory and discard the handle, call #mdb_env_close().
414 * Before the handle may be used, it must be opened using #mdb_env_open().
415 * Various other options may also need to be set before opening the handle,
416 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
417 * depending on usage requirements.
418 * @param[out] env The address where the new handle will be stored
419 * @return A non-zero error value on failure and 0 on success.
421 int mdb_env_create(MDB_env **env);
423 /** @brief Open an environment handle.
425 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
426 * @param[in] env An environment handle returned by #mdb_env_create()
427 * @param[in] path The directory in which the database files reside. This
428 * directory must already exist and be writable.
429 * @param[in] flags Special options for this environment. This parameter
430 * must be set to 0 or by bitwise OR'ing together one or more of the
431 * values described here.
432 * Flags set by mdb_env_set_flags() are also used.
435 * use a fixed address for the mmap region. This flag must be specified
436 * when creating the environment, and is stored persistently in the environment.
437 * If successful, the memory map will always reside at the same virtual address
438 * and pointers used to reference data items in the database will be constant
439 * across multiple invocations. This option may not always work, depending on
440 * how the operating system has allocated memory to shared libraries and other uses.
441 * The feature is highly experimental.
443 * By default, MDB creates its environment in a directory whose
444 * pathname is given in \b path, and creates its data and lock files
445 * under that directory. With this option, \b path is used as-is for
446 * the database main data file. The database lock file is the \b path
447 * with "-lock" appended.
449 * Open the environment in read-only mode. No write operations will be
450 * allowed. MDB will still modify the lock file - except on read-only
451 * filesystems, where MDB does not use locks.
453 * Use a writeable memory map unless MDB_RDONLY is set. This is faster
454 * and uses fewer mallocs, but loses protection from application bugs
455 * like wild pointer writes and other bad updates into the database.
456 * Incompatible with nested transactions.
457 * <li>#MDB_NOMETASYNC
458 * Flush system buffers to disk only once per transaction, omit the
459 * metadata flush. Defer that until the system flushes files to disk,
460 * or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
461 * maintains database integrity, but a system crash may undo the last
462 * committed transaction. I.e. it preserves the ACI (atomicity,
463 * consistency, isolation) but not D (durability) database property.
464 * This flag may be changed at any time using #mdb_env_set_flags().
466 * Don't flush system buffers to disk when committing a transaction.
467 * This optimization means a system crash can corrupt the database or
468 * lose the last transactions if buffers are not yet flushed to disk.
469 * The risk is governed by how often the system flushes dirty buffers
470 * to disk and how often #mdb_env_sync() is called. However, if the
471 * filesystem preserves write order and the #MDB_WRITEMAP flag is not
472 * used, transactions exhibit ACI (atomicity, consistency, isolation)
473 * properties and only lose D (durability). I.e. database integrity
474 * is maintained, but a system crash may undo the final transactions.
475 * Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
476 * hint for when to write transactions to disk, unless #mdb_env_sync()
477 * is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
478 * This flag may be changed at any time using #mdb_env_set_flags().
480 * When using #MDB_WRITEMAP, use asynchronous flushes to disk.
481 * As with #MDB_NOSYNC, a system crash can then corrupt the
482 * database or lose the last transactions. Calling #mdb_env_sync()
483 * ensures on-disk database integrity until next commit.
484 * This flag may be changed at any time using #mdb_env_set_flags().
486 * @param[in] mode The UNIX permissions to set on created files. This parameter
487 * is ignored on Windows.
488 * @return A non-zero error value on failure and 0 on success. Some possible
491 * <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
492 * version that created the database environment.
493 * <li>EINVAL - the environment file headers are corrupted.
494 * <li>ENOENT - the directory specified by the path parameter doesn't exist.
495 * <li>EACCES - the user didn't have permission to access the environment files.
496 * <li>EAGAIN - the environment was locked by another process.
499 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
501 /** @brief Copy an MDB environment to the specified path.
503 * This function may be used to make a backup of an existing environment.
504 * @param[in] env An environment handle returned by #mdb_env_create(). It
505 * must have already been opened successfully.
506 * @param[in] path The directory in which the copy will reside. This
507 * directory must already exist and be writable but must otherwise be
509 * @return A non-zero error value on failure and 0 on success.
511 int mdb_env_copy(MDB_env *env, const char *path);
513 /** @brief Return statistics about the MDB environment.
515 * @param[in] env An environment handle returned by #mdb_env_create()
516 * @param[out] stat The address of an #MDB_stat structure
517 * where the statistics will be copied
519 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
521 /** @brief Return information about the MDB environment.
523 * @param[in] env An environment handle returned by #mdb_env_create()
524 * @param[out] stat The address of an #MDB_envinfo structure
525 * where the information will be copied
527 int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
529 /** @brief Flush the data buffers to disk.
531 * Data is always written to disk when #mdb_txn_commit() is called,
532 * but the operating system may keep it buffered. MDB always flushes
533 * the OS buffers upon commit as well, unless the environment was
534 * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC.
535 * @param[in] env An environment handle returned by #mdb_env_create()
536 * @param[in] force If non-zero, force a synchronous flush. Otherwise
537 * if the environment has the #MDB_NOSYNC flag set the flushes
538 * will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
539 * @return A non-zero error value on failure and 0 on success. Some possible
542 * <li>EINVAL - an invalid parameter was specified.
543 * <li>EIO - an error occurred during synchronization.
546 int mdb_env_sync(MDB_env *env, int force);
548 /** @brief Close the environment and release the memory map.
550 * Only a single thread may call this function. All transactions, databases,
551 * and cursors must already be closed before calling this function. Attempts to
552 * use any such handles after calling this function will cause a SIGSEGV.
553 * The environment handle will be freed and must not be used again after this call.
554 * @param[in] env An environment handle returned by #mdb_env_create()
556 void mdb_env_close(MDB_env *env);
558 /** @brief Set environment flags.
560 * This may be used to set some flags in addition to those from
561 * #mdb_env_open(), or to unset these flags.
562 * @param[in] env An environment handle returned by #mdb_env_create()
563 * @param[in] flags The flags to change, bitwise OR'ed together
564 * @param[in] onoff A non-zero value sets the flags, zero clears them.
565 * @return A non-zero error value on failure and 0 on success. Some possible
568 * <li>EINVAL - an invalid parameter was specified.
571 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
573 /** @brief Get environment flags.
575 * @param[in] env An environment handle returned by #mdb_env_create()
576 * @param[out] flags The address of an integer to store the flags
577 * @return A non-zero error value on failure and 0 on success. Some possible
580 * <li>EINVAL - an invalid parameter was specified.
583 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
585 /** @brief Return the path that was used in #mdb_env_open().
587 * @param[in] env An environment handle returned by #mdb_env_create()
588 * @param[out] path Address of a string pointer to contain the path. This
589 * is the actual string in the environment, not a copy. It should not be
590 * altered in any way.
591 * @return A non-zero error value on failure and 0 on success. Some possible
594 * <li>EINVAL - an invalid parameter was specified.
597 int mdb_env_get_path(MDB_env *env, const char **path);
599 /** @brief Set the size of the memory map to use for this environment.
601 * The size should be a multiple of the OS page size. The default is
602 * 10485760 bytes. The size of the memory map is also the maximum size
603 * of the database. The value should be chosen as large as possible,
604 * to accommodate future growth of the database.
605 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
606 * The size may be changed by closing and reopening the environment.
607 * Any attempt to set a size smaller than the space already consumed
608 * by the environment will be silently changed to the current size of the used space.
609 * @param[in] env An environment handle returned by #mdb_env_create()
610 * @param[in] size The size in bytes
611 * @return A non-zero error value on failure and 0 on success. Some possible
614 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
617 int mdb_env_set_mapsize(MDB_env *env, size_t size);
619 /** @brief Set the maximum number of threads for the environment.
621 * This defines the number of slots in the lock table that is used to track readers in the
622 * the environment. The default is 126.
623 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
624 * @param[in] env An environment handle returned by #mdb_env_create()
625 * @param[in] readers The maximum number of threads
626 * @return A non-zero error value on failure and 0 on success. Some possible
629 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
632 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
634 /** @brief Get the maximum number of threads for the environment.
636 * @param[in] env An environment handle returned by #mdb_env_create()
637 * @param[out] readers Address of an integer to store the number of readers
638 * @return A non-zero error value on failure and 0 on success. Some possible
641 * <li>EINVAL - an invalid parameter was specified.
644 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
646 /** @brief Set the maximum number of named databases for the environment.
648 * This function is only needed if multiple databases will be used in the
649 * environment. Simpler applications that use the environment as a single
650 * unnamed database can ignore this option.
651 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
652 * @param[in] env An environment handle returned by #mdb_env_create()
653 * @param[in] dbs The maximum number of databases
654 * @return A non-zero error value on failure and 0 on success. Some possible
657 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
660 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
662 /** @brief Create a transaction for use with the environment.
664 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
665 * @note Transactions may not span threads; a transaction must only be used by a
666 * single thread. Also, a thread may only have a single transaction.
667 * @note Cursors may not span transactions; each cursor must be opened and closed
668 * within a single transaction.
669 * @param[in] env An environment handle returned by #mdb_env_create()
670 * @param[in] parent If this parameter is non-NULL, the new transaction
671 * will be a nested transaction, with the transaction indicated by \b parent
672 * as its parent. Transactions may be nested to any level. A parent
673 * transaction may not issue any other operations besides mdb_txn_begin,
674 * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
675 * @param[in] flags Special options for this transaction. This parameter
676 * must be set to 0 or by bitwise OR'ing together one or more of the
677 * values described here.
680 * This transaction will not perform any write operations.
682 * @param[out] txn Address where the new #MDB_txn handle will be stored
683 * @return A non-zero error value on failure and 0 on success. Some possible
686 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
687 - * must be shut down.
688 * <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
689 * mapsize and the environment must be shut down.
690 * <li>ENOMEM - out of memory, or a read-only transaction was requested and
691 * the reader lock table is full. See #mdb_env_set_maxreaders().
694 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
696 /** @brief Commit all the operations of a transaction into the database.
698 * All cursors opened within the transaction will be closed by this call. The cursors
699 * and transaction handle will be freed and must not be used again after this call.
700 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
701 * @return A non-zero error value on failure and 0 on success. Some possible
704 * <li>EINVAL - an invalid parameter was specified.
705 * <li>ENOSPC - no more disk space.
706 * <li>EIO - a low-level I/O error occurred while writing.
707 * <li>ENOMEM - the transaction is nested and could not be merged into its parent.
710 int mdb_txn_commit(MDB_txn *txn);
712 /** @brief Abandon all the operations of the transaction instead of saving them.
714 * All cursors opened within the transaction will be closed by this call. The cursors
715 * and transaction handle will be freed and must not be used again after this call.
716 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
718 void mdb_txn_abort(MDB_txn *txn);
720 /** @brief Reset a read-only transaction.
722 * This releases the current reader lock but doesn't free the
723 * transaction handle, allowing it to be used again later by #mdb_txn_renew().
724 * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
725 * allocation/deallocation overhead if a thread is going to start a new
726 * read-only transaction again soon.
727 * All cursors opened within the transaction must be closed before the transaction
729 * Reader locks generally don't interfere with writers, but they keep old
730 * versions of database pages allocated. Thus they prevent the old pages
731 * from being reused when writers commit new data, and so under heavy load
732 * the database size may grow much more rapidly than otherwise.
733 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
735 void mdb_txn_reset(MDB_txn *txn);
737 /** @brief Renew a read-only transaction.
739 * This acquires a new reader lock for a transaction handle that had been
740 * released by #mdb_txn_reset(). It must be called before a reset transaction
742 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
743 * @return A non-zero error value on failure and 0 on success. Some possible
746 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
748 * <li>EINVAL - an invalid parameter was specified.
751 int mdb_txn_renew(MDB_txn *txn);
753 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
754 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
755 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
756 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
758 /** @brief Open a database in the environment.
760 * The database handle may be discarded by calling #mdb_dbi_close(). The
761 * database handle resides in the shared environment, it is not owned
762 * by the given transaction. Only one thread should call this function;
763 * it is not mutex-protected in a read-only transaction.
764 * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
765 * must be called before opening the environment.
766 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
767 * @param[in] name The name of the database to open. If only a single
768 * database is needed in the environment, this value may be NULL.
769 * @param[in] flags Special options for this database. This parameter
770 * must be set to 0 or by bitwise OR'ing together one or more of the
771 * values described here.
773 * <li>#MDB_REVERSEKEY
774 * Keys are strings to be compared in reverse order, from the end
775 * of the strings to the beginning. By default, Keys are treated as strings and
776 * compared from beginning to end.
778 * Duplicate keys may be used in the database. (Or, from another perspective,
779 * keys may have multiple data items, stored in sorted order.) By default
780 * keys must be unique and may have only a single data item.
781 * <li>#MDB_INTEGERKEY
782 * Keys are binary integers in native byte order. Setting this option
783 * requires all keys to be the same size, typically sizeof(int)
786 * This flag may only be used in combination with #MDB_DUPSORT. This option
787 * tells the library that the data items for this database are all the same
788 * size, which allows further optimizations in storage and retrieval. When
789 * all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
790 * cursor operations may be used to retrieve multiple items at once.
791 * <li>#MDB_INTEGERDUP
792 * This option specifies that duplicate data items are also integers, and
793 * should be sorted as such.
794 * <li>#MDB_REVERSEDUP
795 * This option specifies that duplicate data items should be compared as
796 * strings in reverse order.
798 * Create the named database if it doesn't exist. This option is not
799 * allowed in a read-only transaction or a read-only environment.
801 * @param[out] dbi Address where the new #MDB_dbi handle will be stored
802 * @return A non-zero error value on failure and 0 on success. Some possible
805 * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
806 * and #MDB_CREATE was not specified.
807 * <li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
810 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
812 /** @brief Retrieve statistics for a database.
814 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
815 * @param[in] dbi A database handle returned by #mdb_dbi_open()
816 * @param[out] stat The address of an #MDB_stat structure
817 * where the statistics will be copied
818 * @return A non-zero error value on failure and 0 on success. Some possible
821 * <li>EINVAL - an invalid parameter was specified.
824 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
826 /** @brief Close a database handle.
828 * This call is not mutex protected. Handles should only be closed by
829 * a single thread, and only if no other threads are going to reference
830 * the database handle or one of its cursors any further.
831 * @param[in] env An environment handle returned by #mdb_env_create()
832 * @param[in] dbi A database handle returned by #mdb_dbi_open()
834 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
836 /** @brief Delete a database and/or free all its pages.
838 * If the \b del parameter is 1, the DB handle will be closed
839 * and the DB will be deleted.
840 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
841 * @param[in] dbi A database handle returned by #mdb_dbi_open()
842 * @param[in] del 1 to delete the DB from the environment,
843 * 0 to just free its pages.
844 * @return A non-zero error value on failure and 0 on success.
846 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
848 /** @brief Set a custom key comparison function for a database.
850 * The comparison function is called whenever it is necessary to compare a
851 * key specified by the application with a key currently stored in the database.
852 * If no comparison function is specified, and no special key flags were specified
853 * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
854 * before longer keys.
855 * @warning This function must be called before any data access functions are used,
856 * otherwise data corruption may occur. The same comparison function must be used by every
857 * program accessing the database, every time the database is used.
858 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
859 * @param[in] dbi A database handle returned by #mdb_dbi_open()
860 * @param[in] cmp A #MDB_cmp_func function
861 * @return A non-zero error value on failure and 0 on success. Some possible
864 * <li>EINVAL - an invalid parameter was specified.
867 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
869 /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
871 * This comparison function is called whenever it is necessary to compare a data
872 * item specified by the application with a data item currently stored in the database.
873 * This function only takes effect if the database was opened with the #MDB_DUPSORT
875 * If no comparison function is specified, and no special key flags were specified
876 * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
877 * before longer items.
878 * @warning This function must be called before any data access functions are used,
879 * otherwise data corruption may occur. The same comparison function must be used by every
880 * program accessing the database, every time the database is used.
881 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
882 * @param[in] dbi A database handle returned by #mdb_dbi_open()
883 * @param[in] cmp A #MDB_cmp_func function
884 * @return A non-zero error value on failure and 0 on success. Some possible
887 * <li>EINVAL - an invalid parameter was specified.
890 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
892 /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
894 * @todo The relocation function is called whenever it is necessary to move the data
895 * of an item to a different position in the database (e.g. through tree
896 * balancing operations, shifts as a result of adds or deletes, etc.). It is
897 * intended to allow address/position-dependent data items to be stored in
898 * a database in an environment opened with the #MDB_FIXEDMAP option.
899 * Currently the relocation feature is unimplemented and setting
900 * this function has no effect.
901 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
902 * @param[in] dbi A database handle returned by #mdb_dbi_open()
903 * @param[in] rel A #MDB_rel_func function
904 * @return A non-zero error value on failure and 0 on success. Some possible
907 * <li>EINVAL - an invalid parameter was specified.
910 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
912 /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
914 * See #mdb_set_relfunc and #MDB_rel_func for more details.
915 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
916 * @param[in] dbi A database handle returned by #mdb_dbi_open()
917 * @param[in] ctx An arbitrary pointer for whatever the application needs.
918 * It will be passed to the callback function set by #mdb_set_relfunc
919 * as its \b relctx parameter whenever the callback is invoked.
920 * @return A non-zero error value on failure and 0 on success. Some possible
923 * <li>EINVAL - an invalid parameter was specified.
926 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
928 /** @brief Get items from a database.
930 * This function retrieves key/data pairs from the database. The address
931 * and length of the data associated with the specified \b key are returned
932 * in the structure to which \b data refers.
933 * If the database supports duplicate keys (#MDB_DUPSORT) then the
934 * first data item for the key will be returned. Retrieval of other
935 * items requires the use of #mdb_cursor_get().
937 * @note The memory pointed to by the returned values is owned by the
938 * database. The caller need not dispose of the memory, and may not
939 * modify it in any way. For values returned in a read-only transaction
940 * any modification attempts will cause a SIGSEGV.
941 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
942 * @param[in] dbi A database handle returned by #mdb_dbi_open()
943 * @param[in] key The key to search for in the database
944 * @param[out] data The data corresponding to the key
945 * @return A non-zero error value on failure and 0 on success. Some possible
948 * <li>#MDB_NOTFOUND - the key was not in the database.
949 * <li>EINVAL - an invalid parameter was specified.
952 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
954 /** @brief Store items into a database.
956 * This function stores key/data pairs in the database. The default behavior
957 * is to enter the new key/data pair, replacing any previously existing key
958 * if duplicates are disallowed, or adding a duplicate data item if
959 * duplicates are allowed (#MDB_DUPSORT).
960 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
961 * @param[in] dbi A database handle returned by #mdb_dbi_open()
962 * @param[in] key The key to store in the database
963 * @param[in,out] data The data to store
964 * @param[in] flags Special options for this operation. This parameter
965 * must be set to 0 or by bitwise OR'ing together one or more of the
966 * values described here.
968 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
969 * already appear in the database. This flag may only be specified
970 * if the database was opened with #MDB_DUPSORT. The function will
971 * return #MDB_KEYEXIST if the key/data pair already appears in the
973 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
974 * does not already appear in the database. The function will return
975 * #MDB_KEYEXIST if the key already appears in the database, even if
976 * the database supports duplicates (#MDB_DUPSORT). The \b data
977 * parameter will be set to point to the existing item.
978 * <li>#MDB_RESERVE - reserve space for data of the given size, but
979 * don't copy the given data. Instead, return a pointer to the
980 * reserved space, which the caller can fill in later. This saves
981 * an extra memcpy if the data is being generated later.
982 * <li>#MDB_APPEND - append the given key/data pair to the end of the
983 * database. No key comparisons are performed. This option allows
984 * fast bulk loading when keys are already known to be in the
985 * correct order. Loading unsorted keys with this flag will cause
987 * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
989 * @return A non-zero error value on failure and 0 on success. Some possible
992 * <li>EACCES - an attempt was made to write in a read-only transaction.
993 * <li>EINVAL - an invalid parameter was specified.
994 * <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
997 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1000 /** @brief Delete items from a database.
1002 * This function removes key/data pairs from the database.
1003 * If the database does not support sorted duplicate data items
1004 * (#MDB_DUPSORT) the data parameter is ignored.
1005 * If the database supports sorted duplicates and the data parameter
1006 * is NULL, all of the duplicate data items for the key will be
1007 * deleted. Otherwise, if the data parameter is non-NULL
1008 * only the matching data item will be deleted.
1009 * This function will return #MDB_NOTFOUND if the specified key/data
1010 * pair is not in the database.
1011 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1012 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1013 * @param[in] key The key to delete from the database
1014 * @param[in] data The data to delete
1015 * @return A non-zero error value on failure and 0 on success. Some possible
1018 * <li>EACCES - an attempt was made to write in a read-only transaction.
1019 * <li>EINVAL - an invalid parameter was specified.
1022 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1024 /** @brief Create a cursor handle.
1026 * Cursors are associated with a specific transaction and database and
1027 * may not span threads.
1028 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1029 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1030 * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1031 * @return A non-zero error value on failure and 0 on success. Some possible
1034 * <li>EINVAL - an invalid parameter was specified.
1037 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1039 /** @brief Close a cursor handle.
1041 * The cursor handle will be freed and must not be used again after this call.
1042 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1044 void mdb_cursor_close(MDB_cursor *cursor);
1046 /** @brief Renew a cursor handle.
1048 * Cursors are associated with a specific transaction and database and
1049 * may not span threads. Cursors that are only used in read-only
1050 * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1051 * The cursor may be associated with a new read-only transaction, and
1052 * referencing the same database handle as it was created with.
1053 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1054 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1055 * @return A non-zero error value on failure and 0 on success. Some possible
1058 * <li>EINVAL - an invalid parameter was specified.
1061 int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1063 /** @brief Return the cursor's transaction handle.
1065 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1067 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1069 /** @brief Return the cursor's database handle.
1071 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1073 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1075 /** @brief Retrieve by cursor.
1077 * This function retrieves key/data pairs from the database. The address and length
1078 * of the key are returned in the object to which \b key refers (except for the
1079 * case of the #MDB_SET option, in which the \b key object is unchanged), and
1080 * the address and length of the data are returned in the object to which \b data
1082 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1083 * @param[in,out] key The key for a retrieved item
1084 * @param[in,out] data The data of a retrieved item
1085 * @param[in] op A cursor operation #MDB_cursor_op
1086 * @return A non-zero error value on failure and 0 on success. Some possible
1089 * <li>#MDB_NOTFOUND - no matching key found.
1090 * <li>EINVAL - an invalid parameter was specified.
1093 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1096 /** @brief Store by cursor.
1098 * This function stores key/data pairs into the database.
1099 * If the function fails for any reason, the state of the cursor will be
1100 * unchanged. If the function succeeds and an item is inserted into the
1101 * database, the cursor is always positioned to refer to the newly inserted item.
1102 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1103 * @param[in] key The key operated on.
1104 * @param[in] data The data operated on.
1105 * @param[in] flags Options for this operation. This parameter
1106 * must be set to 0 or one of the values described here.
1108 * <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
1109 * the cursor refers with the specified data item. The \b key
1110 * parameter is ignored.
1111 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1112 * already appear in the database. This flag may only be specified
1113 * if the database was opened with #MDB_DUPSORT. The function will
1114 * return #MDB_KEYEXIST if the key/data pair already appears in the
1116 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1117 * does not already appear in the database. The function will return
1118 * #MDB_KEYEXIST if the key already appears in the database, even if
1119 * the database supports duplicates (#MDB_DUPSORT).
1120 * <li>#MDB_RESERVE - reserve space for data of the given size, but
1121 * don't copy the given data. Instead, return a pointer to the
1122 * reserved space, which the caller can fill in later. This saves
1123 * an extra memcpy if the data is being generated later.
1124 * <li>#MDB_APPEND - append the given key/data pair to the end of the
1125 * database. No key comparisons are performed. This option allows
1126 * fast bulk loading when keys are already known to be in the
1127 * correct order. Loading unsorted keys with this flag will cause
1129 * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1131 * @return A non-zero error value on failure and 0 on success. Some possible
1134 * <li>EACCES - an attempt was made to modify a read-only database.
1135 * <li>EINVAL - an invalid parameter was specified.
1138 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1139 unsigned int flags);
1141 /** @brief Delete current key/data pair
1143 * This function deletes the key/data pair to which the cursor refers.
1144 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1145 * @param[in] flags Options for this operation. This parameter
1146 * must be set to 0 or one of the values described here.
1148 * <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1149 * This flag may only be specified if the database was opened with #MDB_DUPSORT.
1151 * @return A non-zero error value on failure and 0 on success. Some possible
1154 * <li>EACCES - an attempt was made to modify a read-only database.
1155 * <li>EINVAL - an invalid parameter was specified.
1158 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1160 /** @brief Return count of duplicates for current key.
1162 * This call is only valid on databases that support sorted duplicate
1163 * data items #MDB_DUPSORT.
1164 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1165 * @param[out] countp Address where the count will be stored
1166 * @return A non-zero error value on failure and 0 on success. Some possible
1169 * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1172 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1174 /** @brief Compare two data items according to a particular database.
1176 * This returns a comparison as if the two data items were keys in the
1177 * specified database.
1178 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1179 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1180 * @param[in] a The first item to compare
1181 * @param[in] b The second item to compare
1182 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1184 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1186 /** @brief Compare two data items according to a particular database.
1188 * This returns a comparison as if the two items were data items of
1189 * a sorted duplicates #MDB_DUPSORT database.
1190 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1191 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1192 * @param[in] a The first item to compare
1193 * @param[in] b The second item to compare
1194 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1196 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1202 #endif /* _LMDB_H_ */