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.
69 * The #MDB_NOTLS flag changes this for read-only transactions.
71 * - Use an MDB_env* in the process which opened it, without fork()ing.
73 * - Do not have open an MDB database twice in the same process at
74 * the same time. Not even from a plain open() call - close()ing it
75 * breaks flock() advisory locking.
77 * - Avoid long-lived transactions. Read transactions prevent
78 * reuse of pages freed by newer write transactions, thus the
79 * database can grow quickly. Write transactions prevent
80 * other write transactions, since writes are serialized.
82 * - Avoid suspending a process with active transactions. These
83 * would then be "long-lived" as above. Also read transactions
84 * suspended when writers commit could sometimes see wrong data.
86 * ...when several processes can use a database concurrently:
88 * - Avoid aborting a process with an active transaction.
89 * The transaction becomes "long-lived" as above until the lockfile
90 * is reset, since the process may not remove it from the lockfile.
92 * - If you do that anyway, close the environment once in a while,
93 * so the lockfile can get reset.
95 * - Do not use MDB databases on remote filesystems, even between
96 * processes on the same host. This breaks flock() on some OSes,
97 * possibly memory map sync, and certainly sync between programs
100 * - Opening a database can fail if another process is opening or
101 * closing it at exactly the same time.
103 * @author Howard Chu, Symas Corporation.
105 * @copyright Copyright 2011-2013 Howard Chu, Symas Corp. All rights reserved.
107 * Redistribution and use in source and binary forms, with or without
108 * modification, are permitted only as authorized by the OpenLDAP
111 * A copy of this license is available in the file LICENSE in the
112 * top-level directory of the distribution or, alternatively, at
113 * <http://www.OpenLDAP.org/license.html>.
116 * This code is derived from btree.c written by Martin Hedenfalk.
118 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
120 * Permission to use, copy, modify, and distribute this software for any
121 * purpose with or without fee is hereby granted, provided that the above
122 * copyright notice and this permission notice appear in all copies.
124 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
125 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
126 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
127 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
129 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
130 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
135 #include <sys/types.h>
142 typedef int mdb_mode_t;
144 typedef mode_t mdb_mode_t;
147 /** @defgroup mdb MDB API
149 * @brief OpenLDAP Lightning Memory-Mapped Database Manager
151 /** @defgroup Version Version Macros
154 /** Library major version */
155 #define MDB_VERSION_MAJOR 0
156 /** Library minor version */
157 #define MDB_VERSION_MINOR 9
158 /** Library patch version */
159 #define MDB_VERSION_PATCH 6
161 /** Combine args a,b,c into a single integer for easy version comparisons */
162 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
164 /** The full library version as a single integer */
165 #define MDB_VERSION_FULL \
166 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
168 /** The release date of this library version */
169 #define MDB_VERSION_DATE "January 10, 2013"
171 /** A stringifier for the version info */
172 #define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" d ")"
174 /** A helper for the stringifier macro */
175 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
177 /** The full library version as a C string */
178 #define MDB_VERSION_STRING \
179 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
182 /** @brief Opaque structure for a database environment.
184 * A DB environment supports multiple databases, all residing in the same
187 typedef struct MDB_env MDB_env;
189 /** @brief Opaque structure for a transaction handle.
191 * All database operations require a transaction handle. Transactions may be
192 * read-only or read-write.
194 typedef struct MDB_txn MDB_txn;
196 /** @brief A handle for an individual database in the DB environment. */
197 typedef unsigned int MDB_dbi;
199 /** @brief Opaque structure for navigating through a database */
200 typedef struct MDB_cursor MDB_cursor;
202 /** @brief Generic structure used for passing keys and data in and out
205 * Key sizes must be between 1 and the liblmdb build-time constant
206 * #MDB_MAXKEYSIZE inclusive. This currently defaults to 511. The
207 * same applies to data sizes in databases with the #MDB_DUPSORT flag.
208 * Other data items can in theory be from 0 to 0xffffffff bytes long.
210 typedef struct MDB_val {
211 size_t mv_size; /**< size of the data item */
212 void *mv_data; /**< address of the data item */
215 /** @brief A callback function used to compare two keys in a database */
216 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
218 /** @brief A callback function used to relocate a position-dependent data item
219 * in a fixed-address database.
221 * The \b newptr gives the item's desired address in
222 * the memory map, and \b oldptr gives its previous address. The item's actual
223 * data resides at the address in \b item. This callback is expected to walk
224 * through the fields of the record in \b item and modify any
225 * values based at the \b oldptr address to be relative to the \b newptr address.
226 * @param[in,out] item The item that is to be relocated.
227 * @param[in] oldptr The previous address.
228 * @param[in] newptr The new address to relocate to.
229 * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
230 * @todo This feature is currently unimplemented.
232 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
234 /** @defgroup mdb_env Environment Flags
236 * Values do not overlap Database Flags.
239 /** mmap at a fixed address (experimental) */
240 #define MDB_FIXEDMAP 0x01
241 /** no environment directory */
242 #define MDB_NOSUBDIR 0x4000
243 /** don't fsync after commit */
244 #define MDB_NOSYNC 0x10000
246 #define MDB_RDONLY 0x20000
247 /** don't fsync metapage after commit */
248 #define MDB_NOMETASYNC 0x40000
249 /** use writable mmap */
250 #define MDB_WRITEMAP 0x80000
251 /** use asynchronous msync when MDB_WRITEMAP is used */
252 #define MDB_MAPASYNC 0x100000
253 /** tie reader locktable slots to #MDB_txn objects instead of to threads */
254 #define MDB_NOTLS 0x200000
257 /** @defgroup mdb_dbi_open Database Flags
259 * Values do not overlap Environment Flags.
262 /** use reverse string keys */
263 #define MDB_REVERSEKEY 0x02
264 /** use sorted duplicates */
265 #define MDB_DUPSORT 0x04
266 /** numeric keys in native byte order.
267 * The keys must all be of the same size. */
268 #define MDB_INTEGERKEY 0x08
269 /** with #MDB_DUPSORT, sorted dup items have fixed size */
270 #define MDB_DUPFIXED 0x10
271 /** with #MDB_DUPSORT, dups are numeric in native byte order */
272 #define MDB_INTEGERDUP 0x20
273 /** with #MDB_DUPSORT, use reverse string dups */
274 #define MDB_REVERSEDUP 0x40
275 /** create DB if not already existing */
276 #define MDB_CREATE 0x40000
279 /** @defgroup mdb_put Write Flags
282 /** For put: Don't write if the key already exists. */
283 #define MDB_NOOVERWRITE 0x10
284 /** Only for #MDB_DUPSORT<br>
285 * For put: don't write if the key and data pair already exist.<br>
286 * For mdb_cursor_del: remove all duplicate data items.
288 #define MDB_NODUPDATA 0x20
289 /** For mdb_cursor_put: overwrite the current key/data pair */
290 #define MDB_CURRENT 0x40
291 /** For put: Just reserve space for data, don't copy it. Return a
292 * pointer to the reserved space.
294 #define MDB_RESERVE 0x10000
295 /** Data is being appended, don't split full pages. */
296 #define MDB_APPEND 0x20000
297 /** Duplicate data is being appended, don't split full pages. */
298 #define MDB_APPENDDUP 0x40000
299 /** Store multiple data items in one call. */
300 #define MDB_MULTIPLE 0x80000
303 /** @brief Cursor Get operations.
305 * This is the set of all operations for retrieving data
308 typedef enum MDB_cursor_op {
309 MDB_FIRST, /**< Position at first key/data item */
310 MDB_FIRST_DUP, /**< Position at first data item of current key.
311 Only for #MDB_DUPSORT */
312 MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
313 MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
314 MDB_GET_CURRENT, /**< Return key/data at current cursor position */
315 MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current
316 cursor position. Only for #MDB_DUPFIXED */
317 MDB_LAST, /**< Position at last key/data item */
318 MDB_LAST_DUP, /**< Position at last data item of current key.
319 Only for #MDB_DUPSORT */
320 MDB_NEXT, /**< Position at next data item */
321 MDB_NEXT_DUP, /**< Position at next data item of current key.
322 Only for #MDB_DUPSORT */
323 MDB_NEXT_MULTIPLE, /**< Return all duplicate data items at the next
324 cursor position. Only for #MDB_DUPFIXED */
325 MDB_NEXT_NODUP, /**< Position at first data item of next key.
326 Only for #MDB_DUPSORT */
327 MDB_PREV, /**< Position at previous data item */
328 MDB_PREV_DUP, /**< Position at previous data item of current key.
329 Only for #MDB_DUPSORT */
330 MDB_PREV_NODUP, /**< Position at last data item of previous key.
331 Only for #MDB_DUPSORT */
332 MDB_SET, /**< Position at specified key */
333 MDB_SET_KEY, /**< Position at specified key, return key + data */
334 MDB_SET_RANGE /**< Position at first key greater than or equal to specified key. */
337 /** @defgroup errors Return Codes
339 * BerkeleyDB uses -30800 to -30999, we'll go under them
342 /** Successful result */
343 #define MDB_SUCCESS 0
344 /** key/data pair already exists */
345 #define MDB_KEYEXIST (-30799)
346 /** key/data pair not found (EOF) */
347 #define MDB_NOTFOUND (-30798)
348 /** Requested page not found - this usually indicates corruption */
349 #define MDB_PAGE_NOTFOUND (-30797)
350 /** Located page was wrong type */
351 #define MDB_CORRUPTED (-30796)
352 /** Update of meta page failed, probably I/O error */
353 #define MDB_PANIC (-30795)
354 /** Environment version mismatch */
355 #define MDB_VERSION_MISMATCH (-30794)
356 /** File is not a valid MDB file */
357 #define MDB_INVALID (-30793)
358 /** Environment mapsize reached */
359 #define MDB_MAP_FULL (-30792)
360 /** Environment maxdbs reached */
361 #define MDB_DBS_FULL (-30791)
362 /** Environment maxreaders reached */
363 #define MDB_READERS_FULL (-30790)
364 /** Too many TLS keys in use - Windows only */
365 #define MDB_TLS_FULL (-30789)
366 /** Txn has too many dirty pages */
367 #define MDB_TXN_FULL (-30788)
368 /** Cursor stack too deep - internal error */
369 #define MDB_CURSOR_FULL (-30787)
370 /** Page has not enough space - internal error */
371 #define MDB_PAGE_FULL (-30786)
372 /** Database contents grew beyond environment mapsize */
373 #define MDB_MAP_RESIZED (-30785)
374 /** Database flags changed or would change */
375 #define MDB_INCOMPATIBLE (-30784)
376 /** Invalid reuse of reader locktable slot */
377 #define MDB_BAD_RSLOT (-30783)
378 #define MDB_LAST_ERRCODE MDB_BAD_RSLOT
381 /** @brief Statistics for a database in the environment */
382 typedef struct MDB_stat {
383 unsigned int ms_psize; /**< Size of a database page.
384 This is currently the same for all databases. */
385 unsigned int ms_depth; /**< Depth (height) of the B-tree */
386 size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */
387 size_t ms_leaf_pages; /**< Number of leaf pages */
388 size_t ms_overflow_pages; /**< Number of overflow pages */
389 size_t ms_entries; /**< Number of data items */
392 /** @brief Information about the environment */
393 typedef struct MDB_envinfo {
394 void *me_mapaddr; /**< Address of map, if fixed */
395 size_t me_mapsize; /**< Size of the data memory map */
396 size_t me_last_pgno; /**< ID of the last used page */
397 size_t me_last_txnid; /**< ID of the last committed transaction */
398 unsigned int me_maxreaders; /**< max reader slots in the environment */
399 unsigned int me_numreaders; /**< max reader slots used in the environment */
402 /** @brief Return the mdb library version information.
404 * @param[out] major if non-NULL, the library major version number is copied here
405 * @param[out] minor if non-NULL, the library minor version number is copied here
406 * @param[out] patch if non-NULL, the library patch version number is copied here
407 * @retval "version string" The library version as a string
409 char *mdb_version(int *major, int *minor, int *patch);
411 /** @brief Return a string describing a given error code.
413 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
414 * function. If the error code is greater than or equal to 0, then the string
415 * returned by the system function strerror(3) is returned. If the error code
416 * is less than 0, an error string corresponding to the MDB library error is
417 * returned. See @ref errors for a list of MDB-specific error codes.
418 * @param[in] err The error code
419 * @retval "error message" The description of the error
421 char *mdb_strerror(int err);
423 /** @brief Create an MDB environment handle.
425 * This function allocates memory for a #MDB_env structure. To release
426 * the allocated memory and discard the handle, call #mdb_env_close().
427 * Before the handle may be used, it must be opened using #mdb_env_open().
428 * Various other options may also need to be set before opening the handle,
429 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
430 * depending on usage requirements.
431 * @param[out] env The address where the new handle will be stored
432 * @return A non-zero error value on failure and 0 on success.
434 int mdb_env_create(MDB_env **env);
436 /** @brief Open an environment handle.
438 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
439 * @param[in] env An environment handle returned by #mdb_env_create()
440 * @param[in] path The directory in which the database files reside. This
441 * directory must already exist and be writable.
442 * @param[in] flags Special options for this environment. This parameter
443 * must be set to 0 or by bitwise OR'ing together one or more of the
444 * values described here.
445 * Flags set by mdb_env_set_flags() are also used.
448 * use a fixed address for the mmap region. This flag must be specified
449 * when creating the environment, and is stored persistently in the environment.
450 * If successful, the memory map will always reside at the same virtual address
451 * and pointers used to reference data items in the database will be constant
452 * across multiple invocations. This option may not always work, depending on
453 * how the operating system has allocated memory to shared libraries and other uses.
454 * The feature is highly experimental.
456 * By default, MDB creates its environment in a directory whose
457 * pathname is given in \b path, and creates its data and lock files
458 * under that directory. With this option, \b path is used as-is for
459 * the database main data file. The database lock file is the \b path
460 * with "-lock" appended.
462 * Open the environment in read-only mode. No write operations will be
463 * allowed. MDB will still modify the lock file - except on read-only
464 * filesystems, where MDB does not use locks.
466 * Use a writeable memory map unless MDB_RDONLY is set. This is faster
467 * and uses fewer mallocs, but loses protection from application bugs
468 * like wild pointer writes and other bad updates into the database.
469 * Incompatible with nested transactions.
470 * <li>#MDB_NOMETASYNC
471 * Flush system buffers to disk only once per transaction, omit the
472 * metadata flush. Defer that until the system flushes files to disk,
473 * or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
474 * maintains database integrity, but a system crash may undo the last
475 * committed transaction. I.e. it preserves the ACI (atomicity,
476 * consistency, isolation) but not D (durability) database property.
477 * This flag may be changed at any time using #mdb_env_set_flags().
479 * Don't flush system buffers to disk when committing a transaction.
480 * This optimization means a system crash can corrupt the database or
481 * lose the last transactions if buffers are not yet flushed to disk.
482 * The risk is governed by how often the system flushes dirty buffers
483 * to disk and how often #mdb_env_sync() is called. However, if the
484 * filesystem preserves write order and the #MDB_WRITEMAP flag is not
485 * used, transactions exhibit ACI (atomicity, consistency, isolation)
486 * properties and only lose D (durability). I.e. database integrity
487 * is maintained, but a system crash may undo the final transactions.
488 * Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
489 * hint for when to write transactions to disk, unless #mdb_env_sync()
490 * is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
491 * This flag may be changed at any time using #mdb_env_set_flags().
493 * When using #MDB_WRITEMAP, use asynchronous flushes to disk.
494 * As with #MDB_NOSYNC, a system crash can then corrupt the
495 * database or lose the last transactions. Calling #mdb_env_sync()
496 * ensures on-disk database integrity until next commit.
497 * This flag may be changed at any time using #mdb_env_set_flags().
499 * Don't use Thread-Local Storage. Tie reader locktable slots to
500 * #MDB_txn objects instead of to threads. I.e. #mdb_txn_reset() keeps
501 * the slot reseved for the #MDB_txn object. A thread may use parallel
502 * read-only transactions. A read-only transaction may span threads if
503 * the user synchronizes its use. Applications that multiplex many
504 * user threads over individual OS threads need this option. Such an
505 * application must also serialize the write transactions in an OS
506 * thread, since MDB's write locking is unaware of the user threads.
508 * @param[in] mode The UNIX permissions to set on created files. This parameter
509 * is ignored on Windows.
510 * @return A non-zero error value on failure and 0 on success. Some possible
513 * <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
514 * version that created the database environment.
515 * <li>#MDB_INVALID - the environment file headers are corrupted.
516 * <li>ENOENT - the directory specified by the path parameter doesn't exist.
517 * <li>EACCES - the user didn't have permission to access the environment files.
518 * <li>EAGAIN - the environment was locked by another process.
521 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
523 /** @brief Copy an MDB environment to the specified path.
525 * This function may be used to make a backup of an existing environment.
526 * @param[in] env An environment handle returned by #mdb_env_create(). It
527 * must have already been opened successfully.
528 * @param[in] path The directory in which the copy will reside. This
529 * directory must already exist and be writable but must otherwise be
531 * @return A non-zero error value on failure and 0 on success.
533 int mdb_env_copy(MDB_env *env, const char *path);
535 /** @brief Return statistics about the MDB environment.
537 * @param[in] env An environment handle returned by #mdb_env_create()
538 * @param[out] stat The address of an #MDB_stat structure
539 * where the statistics will be copied
541 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
543 /** @brief Return information about the MDB environment.
545 * @param[in] env An environment handle returned by #mdb_env_create()
546 * @param[out] stat The address of an #MDB_envinfo structure
547 * where the information will be copied
549 int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
551 /** @brief Flush the data buffers to disk.
553 * Data is always written to disk when #mdb_txn_commit() is called,
554 * but the operating system may keep it buffered. MDB always flushes
555 * the OS buffers upon commit as well, unless the environment was
556 * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC.
557 * @param[in] env An environment handle returned by #mdb_env_create()
558 * @param[in] force If non-zero, force a synchronous flush. Otherwise
559 * if the environment has the #MDB_NOSYNC flag set the flushes
560 * will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
561 * @return A non-zero error value on failure and 0 on success. Some possible
564 * <li>EINVAL - an invalid parameter was specified.
565 * <li>EIO - an error occurred during synchronization.
568 int mdb_env_sync(MDB_env *env, int force);
570 /** @brief Close the environment and release the memory map.
572 * Only a single thread may call this function. All transactions, databases,
573 * and cursors must already be closed before calling this function. Attempts to
574 * use any such handles after calling this function will cause a SIGSEGV.
575 * The environment handle will be freed and must not be used again after this call.
576 * @param[in] env An environment handle returned by #mdb_env_create()
578 void mdb_env_close(MDB_env *env);
580 /** @brief Set environment flags.
582 * This may be used to set some flags in addition to those from
583 * #mdb_env_open(), or to unset these flags.
584 * @param[in] env An environment handle returned by #mdb_env_create()
585 * @param[in] flags The flags to change, bitwise OR'ed together
586 * @param[in] onoff A non-zero value sets the flags, zero clears them.
587 * @return A non-zero error value on failure and 0 on success. Some possible
590 * <li>EINVAL - an invalid parameter was specified.
593 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
595 /** @brief Get environment flags.
597 * @param[in] env An environment handle returned by #mdb_env_create()
598 * @param[out] flags The address of an integer to store the flags
599 * @return A non-zero error value on failure and 0 on success. Some possible
602 * <li>EINVAL - an invalid parameter was specified.
605 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
607 /** @brief Return the path that was used in #mdb_env_open().
609 * @param[in] env An environment handle returned by #mdb_env_create()
610 * @param[out] path Address of a string pointer to contain the path. This
611 * is the actual string in the environment, not a copy. It should not be
612 * altered in any way.
613 * @return A non-zero error value on failure and 0 on success. Some possible
616 * <li>EINVAL - an invalid parameter was specified.
619 int mdb_env_get_path(MDB_env *env, const char **path);
621 /** @brief Set the size of the memory map to use for this environment.
623 * The size should be a multiple of the OS page size. The default is
624 * 10485760 bytes. The size of the memory map is also the maximum size
625 * of the database. The value should be chosen as large as possible,
626 * to accommodate future growth of the database.
627 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
628 * The size may be changed by closing and reopening the environment.
629 * Any attempt to set a size smaller than the space already consumed
630 * by the environment will be silently changed to the current size of the used space.
631 * @param[in] env An environment handle returned by #mdb_env_create()
632 * @param[in] size The size in bytes
633 * @return A non-zero error value on failure and 0 on success. Some possible
636 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
639 int mdb_env_set_mapsize(MDB_env *env, size_t size);
641 /** @brief Set the maximum number of threads/reader slots for the environment.
643 * This defines the number of slots in the lock table that is used to track readers in the
644 * the environment. The default is 126.
645 * Starting a read-only transaction normally ties a lock table slot to the
646 * current thread until the environment closes or the thread exits. If
647 * MDB_NOTLS is in use, #mdb_txn_begin() instead ties the slot to the
648 * MDB_txn object until it or the #MDB_env object is destroyed.
649 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
650 * @param[in] env An environment handle returned by #mdb_env_create()
651 * @param[in] readers The maximum number of reader lock table slots
652 * @return A non-zero error value on failure and 0 on success. Some possible
655 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
658 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
660 /** @brief Get the maximum number of threads/reader slots for the environment.
662 * @param[in] env An environment handle returned by #mdb_env_create()
663 * @param[out] readers Address of an integer to store the number of readers
664 * @return A non-zero error value on failure and 0 on success. Some possible
667 * <li>EINVAL - an invalid parameter was specified.
670 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
672 /** @brief Set the maximum number of named databases for the environment.
674 * This function is only needed if multiple databases will be used in the
675 * environment. Simpler applications that use the environment as a single
676 * unnamed database can ignore this option.
677 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
678 * @param[in] env An environment handle returned by #mdb_env_create()
679 * @param[in] dbs The maximum number of databases
680 * @return A non-zero error value on failure and 0 on success. Some possible
683 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
686 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
688 /** @brief Create a transaction for use with the environment.
690 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
691 * @note A transaction and its cursors must only be used by a single
692 * thread, and a thread may only have a single transaction at a time.
693 * If #MDB_NOTLS is in use, this does not apply to read-only transactions.
694 * @note Cursors may not span transactions; each cursor must be opened and closed
695 * within a single transaction.
696 * @param[in] env An environment handle returned by #mdb_env_create()
697 * @param[in] parent If this parameter is non-NULL, the new transaction
698 * will be a nested transaction, with the transaction indicated by \b parent
699 * as its parent. Transactions may be nested to any level. A parent
700 * transaction may not issue any other operations besides mdb_txn_begin,
701 * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
702 * @param[in] flags Special options for this transaction. This parameter
703 * must be set to 0 or by bitwise OR'ing together one or more of the
704 * values described here.
707 * This transaction will not perform any write operations.
709 * @param[out] txn Address where the new #MDB_txn handle will be stored
710 * @return A non-zero error value on failure and 0 on success. Some possible
713 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
715 * <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
716 * mapsize and the environment must be shut down.
717 * <li>#MDB_READERS_FULL - a read-only transaction was requested and
718 * the reader lock table is full. See #mdb_env_set_maxreaders().
719 * <li>ENOMEM - out of memory.
722 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
724 /** @brief Commit all the operations of a transaction into the database.
726 * All cursors opened within the transaction will be closed by this call. The cursors
727 * and transaction handle will be freed and must not be used again after this call.
728 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
729 * @return A non-zero error value on failure and 0 on success. Some possible
732 * <li>EINVAL - an invalid parameter was specified.
733 * <li>ENOSPC - no more disk space.
734 * <li>EIO - a low-level I/O error occurred while writing.
735 * <li>ENOMEM - out of memory.
738 int mdb_txn_commit(MDB_txn *txn);
740 /** @brief Abandon all the operations of the transaction instead of saving them.
742 * All cursors opened within the transaction will be closed by this call. The cursors
743 * and transaction handle will be freed and must not be used again after this call.
744 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
746 void mdb_txn_abort(MDB_txn *txn);
748 /** @brief Reset a read-only transaction.
750 * Abort the transaction like #mdb_txn_abort(), but keep the transaction
751 * handle. #mdb_txn_renew() may reuse the handle. This saves allocation
752 * overhead if the process will start a new read-only transaction soon,
753 * and also locking overhead if #MDB_NOTLS is in use. The reader table
754 * lock is released, but the table slot stays tied to its thread or
755 * #MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free
756 * its lock table slot if MDB_NOTLS is in use.
757 * All cursors opened within the transaction must be closed before the transaction
759 * Reader locks generally don't interfere with writers, but they keep old
760 * versions of database pages allocated. Thus they prevent the old pages
761 * from being reused when writers commit new data, and so under heavy load
762 * the database size may grow much more rapidly than otherwise.
763 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
765 void mdb_txn_reset(MDB_txn *txn);
767 /** @brief Renew a read-only transaction.
769 * This acquires a new reader lock for a transaction handle that had been
770 * released by #mdb_txn_reset(). It must be called before a reset transaction
772 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
773 * @return A non-zero error value on failure and 0 on success. Some possible
776 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
778 * <li>EINVAL - an invalid parameter was specified.
781 int mdb_txn_renew(MDB_txn *txn);
783 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
784 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
785 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
786 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
788 /** @brief Open a database in the environment.
790 * The database handle may be discarded by calling #mdb_dbi_close().
791 * The old database handle is returned if the database was already open.
792 * The handle must only be closed once.
793 * The database handle will be private to the current transaction until
794 * the transaction is successfully committed. If the transaction is
795 * aborted the handle will be closed automatically.
796 * After a successful commit the
797 * handle will reside in the shared environment, and may be used
798 * by other transactions. This function must not be called from
799 * multiple concurrent transactions. A transaction that uses this function
800 * must finish (either commit or abort) before any other transaction may
803 * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
804 * must be called before opening the environment.
805 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
806 * @param[in] name The name of the database to open. If only a single
807 * database is needed in the environment, this value may be NULL.
808 * @param[in] flags Special options for this database. This parameter
809 * must be set to 0 or by bitwise OR'ing together one or more of the
810 * values described here.
812 * <li>#MDB_REVERSEKEY
813 * Keys are strings to be compared in reverse order, from the end
814 * of the strings to the beginning. By default, Keys are treated as strings and
815 * compared from beginning to end.
817 * Duplicate keys may be used in the database. (Or, from another perspective,
818 * keys may have multiple data items, stored in sorted order.) By default
819 * keys must be unique and may have only a single data item.
820 * <li>#MDB_INTEGERKEY
821 * Keys are binary integers in native byte order. Setting this option
822 * requires all keys to be the same size, typically sizeof(int)
825 * This flag may only be used in combination with #MDB_DUPSORT. This option
826 * tells the library that the data items for this database are all the same
827 * size, which allows further optimizations in storage and retrieval. When
828 * all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
829 * cursor operations may be used to retrieve multiple items at once.
830 * <li>#MDB_INTEGERDUP
831 * This option specifies that duplicate data items are also integers, and
832 * should be sorted as such.
833 * <li>#MDB_REVERSEDUP
834 * This option specifies that duplicate data items should be compared as
835 * strings in reverse order.
837 * Create the named database if it doesn't exist. This option is not
838 * allowed in a read-only transaction or a read-only environment.
840 * @param[out] dbi Address where the new #MDB_dbi handle will be stored
841 * @return A non-zero error value on failure and 0 on success. Some possible
844 * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
845 * and #MDB_CREATE was not specified.
846 * <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
849 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
851 /** @brief Retrieve statistics for a database.
853 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
854 * @param[in] dbi A database handle returned by #mdb_dbi_open()
855 * @param[out] stat The address of an #MDB_stat structure
856 * where the statistics will be copied
857 * @return A non-zero error value on failure and 0 on success. Some possible
860 * <li>EINVAL - an invalid parameter was specified.
863 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
865 /** @brief Close a database handle.
867 * This call is not mutex protected. Handles should only be closed by
868 * a single thread, and only if no other threads are going to reference
869 * the database handle or one of its cursors any further. Do not close
870 * a handle if an existing transaction has modified its database.
871 * @param[in] env An environment handle returned by #mdb_env_create()
872 * @param[in] dbi A database handle returned by #mdb_dbi_open()
874 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
876 /** @brief Delete a database and/or free all its pages.
878 * If the \b del parameter is 1, the DB handle will be closed
879 * and the DB will be deleted.
880 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
881 * @param[in] dbi A database handle returned by #mdb_dbi_open()
882 * @param[in] del 1 to delete the DB from the environment,
883 * 0 to just free its pages.
884 * @return A non-zero error value on failure and 0 on success.
886 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
888 /** @brief Set a custom key comparison function for a database.
890 * The comparison function is called whenever it is necessary to compare a
891 * key specified by the application with a key currently stored in the database.
892 * If no comparison function is specified, and no special key flags were specified
893 * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
894 * before longer keys.
895 * @warning This function must be called before any data access functions are used,
896 * otherwise data corruption may occur. The same comparison function must be used by every
897 * program accessing the database, every time the database is used.
898 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
899 * @param[in] dbi A database handle returned by #mdb_dbi_open()
900 * @param[in] cmp A #MDB_cmp_func function
901 * @return A non-zero error value on failure and 0 on success. Some possible
904 * <li>EINVAL - an invalid parameter was specified.
907 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
909 /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
911 * This comparison function is called whenever it is necessary to compare a data
912 * item specified by the application with a data item currently stored in the database.
913 * This function only takes effect if the database was opened with the #MDB_DUPSORT
915 * If no comparison function is specified, and no special key flags were specified
916 * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
917 * before longer items.
918 * @warning This function must be called before any data access functions are used,
919 * otherwise data corruption may occur. The same comparison function must be used by every
920 * program accessing the database, every time the database is used.
921 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
922 * @param[in] dbi A database handle returned by #mdb_dbi_open()
923 * @param[in] cmp A #MDB_cmp_func function
924 * @return A non-zero error value on failure and 0 on success. Some possible
927 * <li>EINVAL - an invalid parameter was specified.
930 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
932 /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
934 * @todo The relocation function is called whenever it is necessary to move the data
935 * of an item to a different position in the database (e.g. through tree
936 * balancing operations, shifts as a result of adds or deletes, etc.). It is
937 * intended to allow address/position-dependent data items to be stored in
938 * a database in an environment opened with the #MDB_FIXEDMAP option.
939 * Currently the relocation feature is unimplemented and setting
940 * this function has no effect.
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] rel A #MDB_rel_func function
944 * @return A non-zero error value on failure and 0 on success. Some possible
947 * <li>EINVAL - an invalid parameter was specified.
950 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
952 /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
954 * See #mdb_set_relfunc and #MDB_rel_func for more details.
955 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
956 * @param[in] dbi A database handle returned by #mdb_dbi_open()
957 * @param[in] ctx An arbitrary pointer for whatever the application needs.
958 * It will be passed to the callback function set by #mdb_set_relfunc
959 * as its \b relctx parameter whenever the callback is invoked.
960 * @return A non-zero error value on failure and 0 on success. Some possible
963 * <li>EINVAL - an invalid parameter was specified.
966 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
968 /** @brief Get items from a database.
970 * This function retrieves key/data pairs from the database. The address
971 * and length of the data associated with the specified \b key are returned
972 * in the structure to which \b data refers.
973 * If the database supports duplicate keys (#MDB_DUPSORT) then the
974 * first data item for the key will be returned. Retrieval of other
975 * items requires the use of #mdb_cursor_get().
977 * @note The memory pointed to by the returned values is owned by the
978 * database. The caller need not dispose of the memory, and may not
979 * modify it in any way. For values returned in a read-only transaction
980 * any modification attempts will cause a SIGSEGV.
981 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
982 * @param[in] dbi A database handle returned by #mdb_dbi_open()
983 * @param[in] key The key to search for in the database
984 * @param[out] data The data corresponding to the key
985 * @return A non-zero error value on failure and 0 on success. Some possible
988 * <li>#MDB_NOTFOUND - the key was not in the database.
989 * <li>EINVAL - an invalid parameter was specified.
992 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
994 /** @brief Store items into a database.
996 * This function stores key/data pairs in the database. The default behavior
997 * is to enter the new key/data pair, replacing any previously existing key
998 * if duplicates are disallowed, or adding a duplicate data item if
999 * duplicates are allowed (#MDB_DUPSORT).
1000 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1001 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1002 * @param[in] key The key to store in the database
1003 * @param[in,out] data The data to store
1004 * @param[in] flags Special options for this operation. This parameter
1005 * must be set to 0 or by bitwise OR'ing together one or more of the
1006 * values described here.
1008 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1009 * already appear in the database. This flag may only be specified
1010 * if the database was opened with #MDB_DUPSORT. The function will
1011 * return #MDB_KEYEXIST if the key/data pair already appears in the
1013 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1014 * does not already appear in the database. The function will return
1015 * #MDB_KEYEXIST if the key already appears in the database, even if
1016 * the database supports duplicates (#MDB_DUPSORT). The \b data
1017 * parameter will be set to point to the existing item.
1018 * <li>#MDB_RESERVE - reserve space for data of the given size, but
1019 * don't copy the given data. Instead, return a pointer to the
1020 * reserved space, which the caller can fill in later. This saves
1021 * an extra memcpy if the data is being generated later.
1022 * <li>#MDB_APPEND - append the given key/data pair to the end of the
1023 * database. No key comparisons are performed. This option allows
1024 * fast bulk loading when keys are already known to be in the
1025 * correct order. Loading unsorted keys with this flag will cause
1027 * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1029 * @return A non-zero error value on failure and 0 on success. Some possible
1032 * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1033 * <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1034 * <li>EACCES - an attempt was made to write in a read-only transaction.
1035 * <li>EINVAL - an invalid parameter was specified.
1038 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1039 unsigned int flags);
1041 /** @brief Delete items from a database.
1043 * This function removes key/data pairs from the database.
1044 * If the database does not support sorted duplicate data items
1045 * (#MDB_DUPSORT) the data parameter is ignored.
1046 * If the database supports sorted duplicates and the data parameter
1047 * is NULL, all of the duplicate data items for the key will be
1048 * deleted. Otherwise, if the data parameter is non-NULL
1049 * only the matching data item will be deleted.
1050 * This function will return #MDB_NOTFOUND if the specified key/data
1051 * pair is not in the database.
1052 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1053 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1054 * @param[in] key The key to delete from the database
1055 * @param[in] data The data to delete
1056 * @return A non-zero error value on failure and 0 on success. Some possible
1059 * <li>EACCES - an attempt was made to write in a read-only transaction.
1060 * <li>EINVAL - an invalid parameter was specified.
1063 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1065 /** @brief Create a cursor handle.
1067 * A cursor is associated with a specific transaction and database.
1068 * It must be closed before its transaction ends.
1069 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1070 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1071 * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1072 * @return A non-zero error value on failure and 0 on success. Some possible
1075 * <li>EINVAL - an invalid parameter was specified.
1078 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1080 /** @brief Close a cursor handle.
1082 * The cursor handle will be freed and must not be used again after this call.
1083 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1085 void mdb_cursor_close(MDB_cursor *cursor);
1087 /** @brief Renew a cursor handle.
1089 * A cursor is associated with a specific transaction and database.
1090 * It must be closed before its transaction ends.
1091 * Cursors that are only used in read-only
1092 * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1093 * The cursor may be associated with a new read-only transaction, and
1094 * referencing the same database handle as it was created with.
1095 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1096 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1097 * @return A non-zero error value on failure and 0 on success. Some possible
1100 * <li>EINVAL - an invalid parameter was specified.
1103 int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1105 /** @brief Return the cursor's transaction handle.
1107 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1109 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1111 /** @brief Return the cursor's database handle.
1113 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1115 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1117 /** @brief Retrieve by cursor.
1119 * This function retrieves key/data pairs from the database. The address and length
1120 * of the key are returned in the object to which \b key refers (except for the
1121 * case of the #MDB_SET option, in which the \b key object is unchanged), and
1122 * the address and length of the data are returned in the object to which \b data
1124 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1125 * @param[in,out] key The key for a retrieved item
1126 * @param[in,out] data The data of a retrieved item
1127 * @param[in] op A cursor operation #MDB_cursor_op
1128 * @return A non-zero error value on failure and 0 on success. Some possible
1131 * <li>#MDB_NOTFOUND - no matching key found.
1132 * <li>EINVAL - an invalid parameter was specified.
1135 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1138 /** @brief Store by cursor.
1140 * This function stores key/data pairs into the database.
1141 * If the function fails for any reason, the state of the cursor will be
1142 * unchanged. If the function succeeds and an item is inserted into the
1143 * database, the cursor is always positioned to refer to the newly inserted item.
1144 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1145 * @param[in] key The key operated on.
1146 * @param[in] data The data operated on.
1147 * @param[in] flags Options for this operation. This parameter
1148 * must be set to 0 or one of the values described here.
1150 * <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
1151 * the cursor refers with the specified data item. The \b key
1152 * parameter is ignored.
1153 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1154 * already appear in the database. This flag may only be specified
1155 * if the database was opened with #MDB_DUPSORT. The function will
1156 * return #MDB_KEYEXIST if the key/data pair already appears in the
1158 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1159 * does not already appear in the database. The function will return
1160 * #MDB_KEYEXIST if the key already appears in the database, even if
1161 * the database supports duplicates (#MDB_DUPSORT).
1162 * <li>#MDB_RESERVE - reserve space for data of the given size, but
1163 * don't copy the given data. Instead, return a pointer to the
1164 * reserved space, which the caller can fill in later. This saves
1165 * an extra memcpy if the data is being generated later.
1166 * <li>#MDB_APPEND - append the given key/data pair to the end of the
1167 * database. No key comparisons are performed. This option allows
1168 * fast bulk loading when keys are already known to be in the
1169 * correct order. Loading unsorted keys with this flag will cause
1171 * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1173 * @return A non-zero error value on failure and 0 on success. Some possible
1176 * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1177 * <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1178 * <li>EACCES - an attempt was made to modify a read-only database.
1179 * <li>EINVAL - an invalid parameter was specified.
1182 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1183 unsigned int flags);
1185 /** @brief Delete current key/data pair
1187 * This function deletes the key/data pair to which the cursor refers.
1188 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1189 * @param[in] flags Options for this operation. This parameter
1190 * must be set to 0 or one of the values described here.
1192 * <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1193 * This flag may only be specified if the database was opened with #MDB_DUPSORT.
1195 * @return A non-zero error value on failure and 0 on success. Some possible
1198 * <li>EACCES - an attempt was made to modify a read-only database.
1199 * <li>EINVAL - an invalid parameter was specified.
1202 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1204 /** @brief Return count of duplicates for current key.
1206 * This call is only valid on databases that support sorted duplicate
1207 * data items #MDB_DUPSORT.
1208 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1209 * @param[out] countp Address where the count will be stored
1210 * @return A non-zero error value on failure and 0 on success. Some possible
1213 * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1216 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1218 /** @brief Compare two data items according to a particular database.
1220 * This returns a comparison as if the two data items were keys in the
1221 * specified database.
1222 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1223 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1224 * @param[in] a The first item to compare
1225 * @param[in] b The second item to compare
1226 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1228 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1230 /** @brief Compare two data items according to a particular database.
1232 * This returns a comparison as if the two items were data items of
1233 * a sorted duplicates #MDB_DUPSORT database.
1234 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1235 * @param[in] dbi A database handle returned by #mdb_dbi_open()
1236 * @param[in] a The first item to compare
1237 * @param[in] b The second item to compare
1238 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1240 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1246 #endif /* _LMDB_H_ */