2 * @brief memory-mapped database library
4 * @mainpage MDB Memory-Mapped Database Manager
5 * MDB is a Btree-based database management library modeled loosely on the
6 * BerkeleyDB API, but much simplified. The entire database is exposed
7 * in a read-only memory map, and all data fetches return data directly
8 * from the mapped memory, so no malloc's or memcpy's occur during
9 * data fetches. As such, the library is extremely simple because it
10 * requires no page caching layer of its own, and it is extremely high
11 * performance and memory-efficient. It is also fully transactional with
12 * full ACID semantics, and because the memory map is read-only, the
13 * database integrity cannot be corrupted by stray pointer writes from
16 * The library is fully thread-aware and supports concurrent read/write
17 * access from multiple processes and threads. Data pages use a copy-on-
18 * write strategy so no active data pages are ever overwritten, which
19 * also provides resistance to corruption and eliminates the need of any
20 * special recovery procedures after a system crash. Writes are fully
21 * serialized; only one write transaction may be active at a time, which
22 * guarantees that writers can never deadlock. The database structure is
23 * multi-versioned so readers run with no locks; writers cannot block
24 * readers, and readers don't block writers.
26 * Unlike other well-known database mechanisms which use either write-ahead
27 * transaction logs or append-only data writes, MDB requires no maintenance
28 * during operation. Both write-ahead loggers and append-only databases
29 * require periodic checkpointing and/or compaction of their log or database
30 * files otherwise they grow without bound. MDB tracks free pages within
31 * the database and re-uses them for new write operations, so the database
32 * size does not grow without bound in normal use.
34 * @author Howard Chu, Symas Corporation.
36 * @copyright Copyright 2011 Howard Chu, Symas Corp. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted only as authorized by the OpenLDAP
42 * A copy of this license is available in the file LICENSE in the
43 * top-level directory of the distribution or, alternatively, at
44 * <http://www.OpenLDAP.org/license.html>.
47 * This code is derived from btree.c written by Martin Hedenfalk.
49 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
51 * Permission to use, copy, modify, and distribute this software for any
52 * purpose with or without fee is hereby granted, provided that the above
53 * copyright notice and this permission notice appear in all copies.
55 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
56 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
57 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
58 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
59 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
61 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
66 #include <sys/types.h>
68 /** @defgroup public Public API
71 /** @defgroup Version Version Macros
74 /** Library major version */
75 #define MDB_VERSION_MAJOR 0
76 /** Library minor version */
77 #define MDB_VERSION_MINOR 9
78 /** Library patch version */
79 #define MDB_VERSION_PATCH 0
81 /** Combine args a,b,c into a single integer for easy version comparisons */
82 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
84 /** The full library version as a single integer */
85 #define MDB_VERSION_FULL \
86 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
88 /** The release date of this library version */
89 #define MDB_VERSION_DATE "September 1, 2011"
91 /** A stringifier for the version info */
92 #define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" #d ")"
94 /** A helper for the stringifier macro */
95 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
97 /** The full library version as a C string */
98 #define MDB_VERSION_STRING \
99 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
102 /** @brief Opaque structure for a database environment.
104 * A DB environment supports multiple databases, all residing in the same
107 typedef struct MDB_env MDB_env;
109 /** @brief Opaque structure for a transaction handle.
111 * All database operations require a transaction handle. Transactions may be
112 * read-only or read-write.
114 typedef struct MDB_txn MDB_txn;
116 /** @brief A handle for an individual database in the DB environment. */
117 typedef unsigned int MDB_dbi;
119 /** @brief Opaque structure for navigating through a database */
120 typedef struct MDB_cursor MDB_cursor;
122 /** @brief Generic structure used for passing keys and data in and out of the database. */
123 typedef struct MDB_val {
124 size_t mv_size; /**< size of the data item */
125 void *mv_data; /**< address of the data item */
128 /** @brief A callback function used to compare two keys in a database */
129 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
131 /** @brief A callback function used to relocate a position-dependent data item
132 * in a fixed-address database.
134 * The \b newptr gives the item's desired address in
135 * the memory map, and \b oldptr gives its previous address. The item's actual
136 * data resides at the address in \b item. This callback is expected to walk
137 * through the fields of the record in \b item and modify any
138 * values based at the \b oldptr address to be relative to the \b newptr address.
139 * @param[in,out] item The item that is to be relocated.
140 * @param[in] oldptr The previous address.
141 * @param[in] newptr The new address to relocate to.
142 * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
143 * @todo This feature is currently unimplemented.
145 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
147 /** @defgroup mdb_env Environment Flags
150 /** mmap at a fixed address */
151 #define MDB_FIXEDMAP 0x01
152 /** no environment directory */
153 #define MDB_NOSUBDIR 0x02
154 /** don't fsync after commit */
155 #define MDB_NOSYNC 0x10000
157 #define MDB_RDONLY 0x20000
160 /** @defgroup mdb_open Database Flags
163 /** use reverse string keys */
164 #define MDB_REVERSEKEY 0x02
165 /** use sorted duplicates */
166 #define MDB_DUPSORT 0x04
167 /** numeric keys in native byte order.
168 * The keys must all be of the same size. */
169 #define MDB_INTEGERKEY 0x08
170 /** with #MDB_DUPSORT, sorted dup items have fixed size */
171 #define MDB_DUPFIXED 0x10
172 /** with #MDB_DUPSORT, dups are numeric in native byte order */
173 #define MDB_INTEGERDUP 0x20
174 /** with #MDB_DUPSORT, use reverse string dups */
175 #define MDB_REVERSEDUP 0x40
176 /** create DB if not already existing */
177 #define MDB_CREATE 0x40000
180 /** @defgroup mdb_put Write Flags
183 /** For put: Don't write if the key already exists. */
184 #define MDB_NOOVERWRITE 0x10
185 /** Only for #MDB_DUPSORT<br>
186 * For put: don't write if the key and data pair already exist.<br>
187 * For mdb_cursor_del: remove all duplicate data items.
189 #define MDB_NODUPDATA 0x20
190 /** For mdb_cursor_put: overwrite the current key/data pair */
191 #define MDB_CURRENT 0x40
192 /** For put: Just reserve space for data, don't copy it. Return a
193 * pointer to the reserved space.
195 #define MDB_RESERVE 0x10000
196 /** Data is being appended, don't split full pages. */
197 #define MDB_APPEND 0x20000
198 /** Store multiple data items in one call. */
199 #define MDB_MULTIPLE 0x40000
202 /** @brief Cursor Get operations.
204 * This is the set of all operations for retrieving data
207 typedef enum MDB_cursor_op {
208 MDB_FIRST, /**< Position at first key/data item */
209 MDB_FIRST_DUP, /**< Position at first data item of current key.
210 Only for #MDB_DUPSORT */
211 MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
212 MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
213 MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current
214 cursor position. Only for #MDB_DUPFIXED */
215 MDB_LAST, /**< Position at last key/data item */
216 MDB_LAST_DUP, /**< Position at last data item of current key.
217 Only for #MDB_DUPSORT */
218 MDB_NEXT, /**< Position at next data item */
219 MDB_NEXT_DUP, /**< Position at next data item of current key.
220 Only for #MDB_DUPSORT */
221 MDB_NEXT_MULTIPLE, /**< Return all duplicate data items at the next
222 cursor position. Only for #MDB_DUPFIXED */
223 MDB_NEXT_NODUP, /**< Position at first data item of next key.
224 Only for #MDB_DUPSORT */
225 MDB_PREV, /**< Position at previous data item */
226 MDB_PREV_DUP, /**< Position at previous data item of current key.
227 Only for #MDB_DUPSORT */
228 MDB_PREV_NODUP, /**< Position at last data item of previous key.
229 Only for #MDB_DUPSORT */
230 MDB_SET, /**< Position at specified key */
231 MDB_SET_RANGE /**< Position at first key greater than or equal to specified key. */
234 /** @defgroup errors Return Codes
236 * BerkeleyDB uses -30800 to -30999, we'll go under them
239 /** Successful result */
240 #define MDB_SUCCESS 0
241 /** key/data pair already exists */
242 #define MDB_KEYEXIST (-30799)
243 /** key/data pair not found (EOF) */
244 #define MDB_NOTFOUND (-30798)
245 /** Requested page not found - this usually indicates corruption */
246 #define MDB_PAGE_NOTFOUND (-30797)
247 /** Located page was wrong type */
248 #define MDB_CORRUPTED (-30796)
249 /** Update of meta page failed, probably I/O error */
250 #define MDB_PANIC (-30795)
251 /** Environment version mismatch */
252 #define MDB_VERSION_MISMATCH (-30794)
255 /** @brief Statistics for a database in the environment */
256 typedef struct MDB_stat {
257 unsigned int ms_psize; /**< Size of a database page.
258 This is currently the same for all databases. */
259 unsigned int ms_depth; /**< Depth (height) of the B-tree */
260 size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */
261 size_t ms_leaf_pages; /**< Number of leaf pages */
262 size_t ms_overflow_pages; /**< Number of overflow pages */
263 size_t ms_entries; /**< Number of data items */
266 /** @brief Return the mdb library version information.
268 * @param[out] major if non-NULL, the library major version number is copied here
269 * @param[out] minor if non-NULL, the library minor version number is copied here
270 * @param[out] patch if non-NULL, the library patch version number is copied here
271 * @retval "version string" The library version as a string
273 char *mdb_version(int *major, int *minor, int *patch);
275 /** @brief Return a string describing a given error code.
277 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
278 * function. If the error code is greater than or equal to 0, then the string
279 * returned by the system function strerror(3) is returned. If the error code
280 * is less than 0, an error string corresponding to the MDB library error is
281 * returned. See @ref errors for a list of MDB-specific error codes.
282 * @param[in] err The error code
283 * @retval "error message" The description of the error
285 char *mdb_strerror(int err);
287 /** @brief Create an MDB environment handle.
289 * This function allocates memory for a #MDB_env structure. To release
290 * the allocated memory and discard the handle, call #mdb_env_close().
291 * Before the handle may be used, it must be opened using #mdb_env_open().
292 * Various other options may also need to be set before opening the handle,
293 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
294 * depending on usage requirements.
295 * @param[out] env The address where the new handle will be stored
296 * @return A non-zero error value on failure and 0 on success.
298 int mdb_env_create(MDB_env **env);
300 /** @brief Open an environment handle.
302 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
303 * @param[in] env An environment handle returned by #mdb_env_create()
304 * @param[in] path The directory in which the database files reside. This
305 * directory must already exist and be writable.
306 * @param[in] flags Special options for this environment. This parameter
307 * must be set to 0 or by bitwise OR'ing together one or more of the
308 * values described here.
311 * use a fixed address for the mmap region. This flag must be specified
312 * when creating the environment, and is stored persistently in the environment.
313 * If successful, the memory map will always reside at the same virtual address
314 * and pointers used to reference data items in the database will be constant
315 * across multiple invocations. This option may not always work, depending on
316 * how the operating system has allocated memory to shared libraries and other uses.
317 * The feature is highly experimental.
319 * By default, MDB creates its environment in a directory whose
320 * pathname is given in \b path, and creates its data and lock files
321 * under that directory. With this option, \b path is used as-is for
322 * the database main data file. The database lock file is the \b path
323 * with "-lock" appended.
325 * Don't perform a synchronous flush after committing a transaction. This means
326 * transactions will exhibit the ACI (atomicity, consistency, and isolation)
327 * properties, but not D (durability); that is database integrity will be
328 * maintained but it is possible some number of the most recently committed
329 * transactions may be undone after a system crash. The number of transactions
330 * at risk is governed by how often the system flushes dirty buffers to disk
331 * and how often #mdb_env_sync() is called. This flag may be changed
332 * at any time using #mdb_env_set_flags().
334 * Open the environment in read-only mode. No write operations will be allowed.
336 * @param[in] mode The UNIX permissions to set on created files. This parameter
337 * is ignored on Windows.
338 * @return A non-zero error value on failure and 0 on success. Some possible
341 * <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
342 * version that created the database environment.
343 * <li>EINVAL - the environment file headers are corrupted.
344 * <li>ENOENT - the directory specified by the path parameter doesn't exist.
345 * <li>EACCES - the user didn't have permission to access the environment files.
346 * <li>EAGAIN - the environment was locked by another process.
349 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
351 /** @brief Return statistics about the MDB environment.
353 * @param[in] env An environment handle returned by #mdb_env_create()
354 * @param[out] stat The address of an #MDB_stat structure
355 * where the statistics will be copied
357 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
359 /** @brief Flush the data buffers to disk.
361 * Data is always written to disk when #mdb_txn_commit() is called,
362 * but the operating system may keep it buffered. MDB always flushes
363 * the OS buffers upon commit as well, unless the environment was
364 * opened with #MDB_NOSYNC.
365 * @param[in] env An environment handle returned by #mdb_env_create()
366 * @param[in] force If non-zero, force the flush to occur. Otherwise
367 * if the environment has the #MDB_NOSYNC flag set the flushes
369 * @return A non-zero error value on failure and 0 on success. Some possible
372 * <li>EINVAL - an invalid parameter was specified.
373 * <li>EIO - an error occurred during synchronization.
376 int mdb_env_sync(MDB_env *env, int force);
378 /** @brief Close the environment and release the memory map.
380 * Only a single thread may call this function. All transactions, databases,
381 * and cursors must already be closed before calling this function. Attempts to
382 * use any such handles after calling this function will cause a SIGSEGV.
383 * The environment handle will be freed and must not be used again after this call.
384 * @param[in] env An environment handle returned by #mdb_env_create()
386 void mdb_env_close(MDB_env *env);
388 /** @brief Set environment flags.
390 * This may be used to set some flags that weren't already set during
391 * #mdb_env_open(), or to unset these flags. Currently only the
392 * #MDB_NOSYNC flag setting may be changed with this function.
393 * @param[in] env An environment handle returned by #mdb_env_create()
394 * @param[in] flags The flags to change, bitwise OR'ed together
395 * @param[in] onoff A non-zero value sets the flags, zero clears them.
396 * @return A non-zero error value on failure and 0 on success. Some possible
399 * <li>EINVAL - an invalid parameter was specified.
402 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
404 /** @brief Get environment flags.
406 * @param[in] env An environment handle returned by #mdb_env_create()
407 * @param[out] flags The address of an integer to store the flags
408 * @return A non-zero error value on failure and 0 on success. Some possible
411 * <li>EINVAL - an invalid parameter was specified.
414 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
416 /** @brief Return the path that was used in #mdb_env_open().
418 * @param[in] env An environment handle returned by #mdb_env_create()
419 * @param[out] path Address of a string pointer to contain the path. This
420 * is the actual string in the environment, not a copy. It should not be
421 * altered in any way.
422 * @return A non-zero error value on failure and 0 on success. Some possible
425 * <li>EINVAL - an invalid parameter was specified.
428 int mdb_env_get_path(MDB_env *env, const char **path);
430 /** @brief Set the size of the memory map to use for this environment.
432 * The size should be a multiple of the OS page size. The default is
433 * 10485760 bytes. The size of the memory map is also the maximum size
434 * of the database. The value should be chosen as large as possible,
435 * to accomodate future growth of the database.
436 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
437 * @param[in] env An environment handle returned by #mdb_env_create()
438 * @param[in] size The size in bytes
439 * @return A non-zero error value on failure and 0 on success. Some possible
442 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
445 int mdb_env_set_mapsize(MDB_env *env, size_t size);
447 /** @brief Set the maximum number of threads for the environment.
449 * This defines the number of slots in the lock table that is used to track readers in the
450 * the environment. The default is 126.
451 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
452 * @param[in] env An environment handle returned by #mdb_env_create()
453 * @param[in] readers The maximum number of threads
454 * @return A non-zero error value on failure and 0 on success. Some possible
457 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
460 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
462 /** @brief Get the maximum number of threads for the environment.
464 * @param[in] env An environment handle returned by #mdb_env_create()
465 * @param[out] readers Address of an integer to store the number of readers
466 * @return A non-zero error value on failure and 0 on success. Some possible
469 * <li>EINVAL - an invalid parameter was specified.
472 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
474 /** @brief Set the maximum number of databases for the environment.
476 * This function is only needed if multiple databases will be used in the
477 * environment. Simpler applications that only use a single database can ignore
479 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
480 * @param[in] env An environment handle returned by #mdb_env_create()
481 * @param[in] dbs The maximum number of databases
482 * @return A non-zero error value on failure and 0 on success. Some possible
485 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
488 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
490 /** @brief Create a transaction for use with the environment.
492 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
493 * @note Transactions may not span threads, a transaction must only be used by a
495 * @note Cursors may not span transactions; each cursor must be opened and closed
496 * within a single transaction.
497 * @param[in] env An environment handle returned by #mdb_env_create()
498 * @param[in] parent If this parameter is non-NULL, the new transaction
499 * will be a nested transaction, with the transaction indicated by \b parent
500 * as its parent. Transactions may be nested to any level. A parent
501 * transaction may not issue any other operations besides mdb_txn_begin,
502 * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
503 * @param[in] flags Special options for this transaction. This parameter
504 * must be set to 0 or by bitwise OR'ing together one or more of the
505 * values described here.
508 * This transaction will not perform any write operations.
510 * @param[out] txn Address where the new #MDB_txn handle will be stored
511 * @return A non-zero error value on failure and 0 on success. Some possible
514 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
516 * <li>ENOMEM - out of memory, or a read-only transaction was requested and
517 * the reader lock table is full. See #mdb_env_set_maxreaders().
520 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
522 /** @brief Commit all the operations of a transaction into the database.
524 * All cursors opened within the transaction will be closed by this call. The cursors
525 * and transaction handle will be freed and must not be used again after this call.
526 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
527 * @return A non-zero error value on failure and 0 on success. Some possible
530 * <li>EINVAL - an invalid parameter was specified.
531 * <li>ENOSPC - no more disk space.
532 * <li>EIO - a low-level I/O error occurred while writing.
535 int mdb_txn_commit(MDB_txn *txn);
537 /** @brief Abandon all the operations of the transaction instead of saving them.
539 * All cursors opened within the transaction will be closed by this call. The cursors
540 * and transaction handle will be freed and must not be used again after this call.
541 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
543 void mdb_txn_abort(MDB_txn *txn);
545 /** @brief Reset a read-only transaction.
547 * This releases the current reader lock but doesn't free the
548 * transaction handle, allowing it to be used again later by #mdb_txn_renew().
549 * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
550 * allocation/deallocation overhead if a thread is going to start a new
551 * read-only transaction again soon.
552 * All cursors opened within the transaction must be closed before the transaction
554 * Reader locks generally don't interfere with writers, but they keep old
555 * versions of database pages allocated. Thus they prevent the old pages
556 * from being reused when writers commit new data, and so under heavy load
557 * the database size may grow much more rapidly than otherwise.
558 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
560 void mdb_txn_reset(MDB_txn *txn);
562 /** @brief Renew a read-only transaction.
564 * This acquires a new reader lock for a transaction handle that had been
565 * released by #mdb_txn_reset(). It must be called before a reset transaction
567 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
568 * @return A non-zero error value on failure and 0 on success. Some possible
571 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
573 * <li>EINVAL - an invalid parameter was specified.
576 int mdb_txn_renew(MDB_txn *txn);
578 /** @brief Open a database in the environment.
580 * The database handle may be discarded by calling #mdb_close(). Only
581 * one thread should call this function; it is not mutex-protected in
582 * a read-only transaction.
583 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
584 * @param[in] name The name of the database to open. If only a single
585 * database is needed in the enviroment, this value may be NULL.
586 * @param[in] flags Special options for this database. This parameter
587 * must be set to 0 or by bitwise OR'ing together one or more of the
588 * values described here.
590 * <li>#MDB_REVERSEKEY
591 * Keys are strings to be compared in reverse order, from the end
592 * of the strings to the beginning. By default, Keys are treated as strings and
593 * compared from beginning to end.
595 * Duplicate keys may be used in the database. (Or, from another perspective,
596 * keys may have multiple data items, stored in sorted order.) By default
597 * keys must be unique and may have only a single data item.
598 * <li>#MDB_INTEGERKEY
599 * Keys are binary integers in native byte order. Setting this option
600 * requires all keys to be the same size, typically sizeof(int)
603 * This flag may only be used in combination with #MDB_DUPSORT. This option
604 * tells the library that the data items for this database are all the same
605 * size, which allows further optimizations in storage and retrieval. When
606 * all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
607 * cursor operations may be used to retrieve multiple items at once.
608 * <li>#MDB_INTEGERDUP
609 * This option specifies that duplicate data items are also integers, and
610 * should be sorted as such.
611 * <li>#MDB_REVERSEDUP
612 * This option specifies that duplicate data items should be compared as
613 * strings in reverse order.
615 * Create the named database if it doesn't exist. This option is not
616 * allowed in a read-only transaction or a read-only environment.
618 * @param[out] dbi Address where the new #MDB_dbi handle will be stored
619 * @return A non-zero error value on failure and 0 on success. Some possible
622 * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
623 * and #MDB_CREATE was not specified.
624 * <li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
627 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
629 /** @brief Retrieve statistics for a database.
631 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
632 * @param[in] dbi A database handle returned by #mdb_open()
633 * @param[out] stat The address of an #MDB_stat structure
634 * where the statistics will be copied
635 * @return A non-zero error value on failure and 0 on success. Some possible
638 * <li>EINVAL - an invalid parameter was specified.
641 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
643 /** @brief Close a database handle.
645 * This call is not mutex protected. Handles should only be closed by
646 * a single thread, and only if no other threads are going to reference
647 * the database handle any further.
648 * @param[in] env An environment handle returned by #mdb_env_create()
649 * @param[in] dbi A database handle returned by #mdb_open()
651 void mdb_close(MDB_env *env, MDB_dbi dbi);
653 /** @brief Delete a database and/or free all its pages.
655 * If the \b del parameter is non-zero the DB handle will be closed
656 * and the DB will be deleted.
657 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
658 * @param[in] dbi A database handle returned by #mdb_open()
659 * @param[in] del non-zero to delete the DB from the environment,
660 * otherwise just free its pages.
661 * @return A non-zero error value on failure and 0 on success.
663 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
665 /** @brief Set a custom key comparison function for a database.
667 * The comparison function is called whenever it is necessary to compare a
668 * key specified by the application with a key currently stored in the database.
669 * If no comparison function is specified, and no speAGAINcial key flags were specified
670 * with #mdb_open(), the keys are compared lexically, with shorter keys collating
671 * before longer keys.
672 * @warning This function must be called before any data access functions are used,
673 * otherwise data corruption may occur. The same comparison function must be used by every
674 * program accessing the database, every time the database is used.
675 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
676 * @param[in] dbi A database handle returned by #mdb_open()
677 * @param[in] cmp A #MDB_cmp_func function
678 * @return A non-zero error value on failure and 0 on success. Some possible
681 * <li>EINVAL - an invalid parameter was specified.
684 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
686 /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
688 * This comparison function is called whenever it is necessary to compare a data
689 * item specified by the application with a data item currently stored in the database.
690 * This function only takes effect if the database was opened with the #MDB_DUPSORT
692 * If no comparison function is specified, and no special key flags were specified
693 * with #mdb_open(), the data items are compared lexically, with shorter items collating
694 * before longer items.
695 * @warning This function must be called before any data access functions are used,
696 * otherwise data corruption may occur. The same comparison function must be used by every
697 * program accessing the database, every time the database is used.
698 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
699 * @param[in] dbi A database handle returned by #mdb_open()
700 * @param[in] cmp A #MDB_cmp_func function
701 * @return A non-zero error value on failure and 0 on success. Some possible
704 * <li>EINVAL - an invalid parameter was specified.
707 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
709 /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
711 * @todo The relocation function is called whenever it is necessary to move the data
712 * of an item to a different position in the database (e.g. through tree
713 * balancing operations, shifts as a result of adds or deletes, etc.). It is
714 * intended to allow address/position-dependent data items to be stored in
715 * a database in an environment opened with the #MDB_FIXEDMAP option.
716 * Currently the relocation feature is unimplemented and setting
717 * this function has no effect.
718 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
719 * @param[in] dbi A database handle returned by #mdb_open()
720 * @param[in] rel A #MDB_rel_func function
721 * @return A non-zero error value on failure and 0 on success. Some possible
724 * <li>EINVAL - an invalid parameter was specified.
727 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
729 /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
731 * See #mdb_set_relfunc and #MDB_rel_func for more details.
732 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
733 * @param[in] dbi A database handle returned by #mdb_open()
734 * @param[in] ctx An arbitrary pointer for whatever the application needs.
735 * It will be passed to the callback function set by #mdb_set_relfunc
736 * as its \b relctx parameter whenever the callback is invoked.
737 * @return A non-zero error value on failure and 0 on success. Some possible
740 * <li>EINVAL - an invalid parameter was specified.
743 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
745 /** @brief Get items from a database.
747 * This function retrieves key/data pairs from the database. The address
748 * and length of the data associated with the specified \b key are returned
749 * in the structure to which \b data refers.
750 * If the database supports duplicate keys (#MDB_DUPSORT) then the
751 * first data item for the key will be returned. Retrieval of other
752 * items requires the use of #mdb_cursor_get().
754 * @note The memory pointed to by the returned values is owned by the
755 * database. The caller need not dispose of the memory, and may not
756 * modify it in any way. For values returned in a read-only transaction
757 * any modification attempts will cause a SIGSEGV.
758 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
759 * @param[in] dbi A database handle returned by #mdb_open()
760 * @param[in] key The key to search for in the database
761 * @param[out] data The data corresponding to the key
762 * @return A non-zero error value on failure and 0 on success. Some possible
765 * <li>#MDB_NOTFOUND - the key was not in the database.
766 * <li>EINVAL - an invalid parameter was specified.
769 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
771 /** @brief Store items into a database.
773 * This function stores key/data pairs in the database. The default behavior
774 * is to enter the new key/data pair, replacing any previously existing key
775 * if duplicates are disallowed, or adding a duplicate data item if
776 * duplicates are allowed (#MDB_DUPSORT).
777 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
778 * @param[in] dbi A database handle returned by #mdb_open()
779 * @param[in] key The key to store in the database
780 * @param[in,out] data The data to store
781 * @param[in] flags Special options for this operation. This parameter
782 * must be set to 0 or by bitwise OR'ing together one or more of the
783 * values described here.
785 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
786 * already appear in the database. This flag may only be specified
787 * if the database was opened with #MDB_DUPSORT. The function will
788 * return #MDB_KEYEXIST if the key/data pair already appears in the
790 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
791 * does not already appear in the database. The function will return
792 * #MDB_KEYEXIST if the key already appears in the database, even if
793 * the database supports duplicates (#MDB_DUPSORT). The \b data
794 * parameter will be set to point to the existing item.
796 * @return A non-zero error value on failure and 0 on success. Some possible
799 * <li>EACCESS - an attempt was made to write in a read-only transaction.
800 * <li>EINVAL - an invalid parameter was specified.
801 * <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
804 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
807 /** @brief Delete items from a database.
809 * This function removes key/data pairs from the database.
810 * If the database does not support sorted duplicate data items
811 * (#MDB_DUPSORT) the data parameter is ignored.
812 * If the database supports sorted duplicates and the data parameter
813 * is NULL, all of the duplicate data items for the key will be
814 * deleted. Otherwise, if the data parameter is non-NULL
815 * only the matching data item will be deleted.
816 * This function will return #MDB_NOTFOUND if the specified key/data
817 * pair is not in the database.
818 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
819 * @param[in] dbi A database handle returned by #mdb_open()
820 * @param[in] key The key to delete from the database
821 * @param[in] data The data to delete
822 * @return A non-zero error value on failure and 0 on success. Some possible
825 * <li>EACCESS - an attempt was made to write in a read-only transaction.
826 * <li>EINVAL - an invalid parameter was specified.
829 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
831 /** @brief Create a cursor handle.
833 * Cursors are associated with a specific transaction and database and
834 * may not span threads.
835 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
836 * @param[in] dbi A database handle returned by #mdb_open()
837 * @param[out] cursor Address where the new #MDB_cursor handle will be stored
838 * @return A non-zero error value on failure and 0 on success. Some possible
841 * <li>EINVAL - an invalid parameter was specified.
844 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
846 /** @brief Close a cursor handle.
848 * The cursor handle will be freed and must not be used again after this call.
849 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
851 void mdb_cursor_close(MDB_cursor *cursor);
853 /** @brief Return the cursor's transaction handle.
855 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
857 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
859 /** @brief Return the cursor's database handle.
861 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
863 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
865 /** @brief Retrieve by cursor.
867 * This function retrieves key/data pairs from the database. The address and length
868 * of the key are returned in the object to which \b key refers (except for the
869 * case of the #MDB_SET option, in which the \b key object is unchanged), and
870 * the address and length of the data are returned in the object to which \b data
872 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
873 * @param[in,out] key The key for a retrieved item
874 * @param[in,out] data The data of a retrieved item
875 * @param[in] op A cursor operation #MDB_cursor_op
876 * @return A non-zero error value on failure and 0 on success. Some possible
879 * <li>#MDB_NOTFOUND - no matching key found.
880 * <li>EINVAL - an invalid parameter was specified.
883 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
886 /** @brief Store by cursor.
888 * This function stores key/data pairs into the database.
889 * If the function fails for any reason, the state of the cursor will be
890 * unchanged. If the function succeeds and an item is inserted into the
891 * database, the cursor is always positioned to refer to the newly inserted item.
892 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
893 * @param[in] key The key operated on.
894 * @param[in] data The data operated on.
895 * @param[in] flags Options for this operation. This parameter
896 * must be set to 0 or one of the values described here.
898 * <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
899 * the cursor refers with the specified data item. The \b key
900 * parameter is ignored.
901 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
902 * already appear in the database. This flag may only be specified
903 * if the database was opened with #MDB_DUPSORT. The function will
904 * return #MDB_KEYEXIST if the key/data pair already appears in the
906 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
907 * does not already appear in the database. The function will return
908 * #MDB_KEYEXIST if the key already appears in the database, even if
909 * the database supports duplicates (#MDB_DUPSORT).
911 * @return A non-zero error value on failure and 0 on success. Some possible
914 * <li>EACCES - an attempt was made to modify a read-only database.
915 * <li>EINVAL - an invalid parameter was specified.
918 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
921 /** @brief Delete current key/data pair
923 * This function deletes the key/data pair to which the cursor refers.
924 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
925 * @param[in] flags Options for this operation. This parameter
926 * must be set to 0 or one of the values described here.
928 * <li>#MDB_NODUPDATA - delete all of the data items for the current key.
929 * This flag may only be specified if the database was opened with #MDB_DUPSORT.
931 * @return A non-zero error value on failure and 0 on success. Some possible
934 * <li>EACCES - an attempt was made to modify a read-only database.
935 * <li>EINVAL - an invalid parameter was specified.
938 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
940 /** @brief Return count of duplicates for current key.
942 * This call is only valid on databases that support sorted duplicate
943 * data items #MDB_DUPSORT.
944 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
945 * @param[out] countp Address where the count will be stored
946 * @return A non-zero error value on failure and 0 on success. Some possible
949 * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
952 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
954 /** @brief Compare two data items according to a particular database.
956 * This returns a comparison as if the two data items were keys in the
957 * specified database.
958 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
959 * @param[in] dbi A database handle returned by #mdb_open()
960 * @param[in] a The first item to compare
961 * @param[in] b The second item to compare
962 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
964 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
966 /** @brief Compare two data items according to a particular database.
968 * This returns a comparison as if the two items were data items of
969 * a sorted duplicates #MDB_DUPSORT database.
970 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
971 * @param[in] dbi A database handle returned by #mdb_open()
972 * @param[in] a The first item to compare
973 * @param[in] b The second item to compare
974 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
976 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);