* @mainpage MDB Memory-Mapped Database Manager
* MDB is a Btree-based database management library modeled loosely on the
* BerkeleyDB API, but much simplified. The entire database is exposed
- * in a read-only memory map, and all data fetches return data directly
+ * in a memory map, and all data fetches return data directly
* from the mapped memory, so no malloc's or memcpy's occur during
* data fetches. As such, the library is extremely simple because it
* requires no page caching layer of its own, and it is extremely high
* performance and memory-efficient. It is also fully transactional with
- * full ACID semantics, and because the memory map is read-only, the
+ * full ACID semantics, and when the memory map is read-only, the
* database integrity cannot be corrupted by stray pointer writes from
* application code.
*
* the database and re-uses them for new write operations, so the database
* size does not grow without bound in normal use.
*
+ * The memory map can be used as a read-only or read-write map. It is
+ * read-only by default as this provides total immunity to corruption.
+ * Using read-write mode offers much higher write performance, but adds
+ * the possibility for stray application writes thru pointers to silently
+ * corrupt the database. Of course if your application code is known to
+ * be bug-free (...) then this is not an issue.
+ *
+ * Troubleshooting the lock file, plus semaphores on BSD systems:
+ *
+ * - A broken lockfile can cause sync issues.
+ * Stale reader transactions left behind by an aborted program
+ * cause further writes to grow the database quickly, and
+ * stale locks can block further operation.
+ *
+ * Fix: Terminate all programs using the database, or make
+ * them close it. Next database user will reset the lockfile.
+ *
+ * - On BSD systems or others configured with MDB_USE_POSIX_SEM,
+ * startup can fail due to semaphores owned by another userid.
+ *
+ * Fix: Open and close the database as the user which owns the
+ * semaphores (likely last user) or as root, while no other
+ * process is using the database.
+ *
+ * Restrictions/caveats (in addition to those listed for some functions):
+ *
+ * - Only the database owner should normally use the database on
+ * BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
+ * Multiple users can cause startup to fail later, as noted above.
+ *
+ * - A thread can only use one transaction at a time, plus any child
+ * transactions. Each transaction belongs to one thread. See below.
+ *
+ * - Use an MDB_env* in the process which opened it, without fork()ing.
+ *
+ * - Do not have open an MDB database twice in the same process at
+ * the same time. Not even from a plain open() call - close()ing it
+ * breaks flock() advisory locking.
+ *
+ * - Avoid long-lived transactions. Read transactions prevent
+ * reuse of pages freed by newer write transactions, thus the
+ * database can grow quickly. Write transactions prevent
+ * other write transactions, since writes are serialized.
+ *
+ * ...when several processes can use a database concurrently:
+ *
+ * - Avoid suspending a process with active transactions. These
+ * would then be "long-lived" as above.
+ *
+ * - Avoid aborting a process with an active transaction.
+ * The transaction becomes "long-lived" as above until the lockfile
+ * is reset, since the process may not remove it from the lockfile.
+ *
+ * - If you do that anyway, close the environment once in a while,
+ * so the lockfile can get reset.
+ *
+ * - Do not use MDB databases on remote filesystems, even between
+ * processes on the same host. This breaks flock() on some OSes,
+ * possibly memory map sync, and certainly sync between programs
+ * on different hosts.
+ *
+ * - Opening a database can fail if another process is opening or
+ * closing it at exactly the same time.
+ *
* @author Howard Chu, Symas Corporation.
*
- * @copyright Copyright 2011 Howard Chu, Symas Corp. All rights reserved.
+ * @copyright Copyright 2011-2012 Howard Chu, Symas Corp. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
#include <sys/types.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/** @defgroup public Public API
* @{
*/
/** Library minor version */
#define MDB_VERSION_MINOR 9
/** Library patch version */
-#define MDB_VERSION_PATCH 0
+#define MDB_VERSION_PATCH 4
/** Combine args a,b,c into a single integer for easy version comparisons */
#define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
/** The release date of this library version */
-#define MDB_VERSION_DATE "September 1, 2011"
+#define MDB_VERSION_DATE "September 14, 2012"
/** A stringifier for the version info */
-#define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" #d ")"
+#define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" d ")"
/** A helper for the stringifier macro */
#define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
/** @} */
-/** Opaque structure for a database environment. A DB environment supports multiple
- * databases, all residing in the same shared-memory map.
+/** @brief Opaque structure for a database environment.
+ *
+ * A DB environment supports multiple databases, all residing in the same
+ * shared-memory map.
*/
typedef struct MDB_env MDB_env;
-/** Opaque structure for a transaction handle. All database operations require
- * a transaction handle. Transactions may be read-only or read-write.
+/** @brief Opaque structure for a transaction handle.
+ *
+ * All database operations require a transaction handle. Transactions may be
+ * read-only or read-write.
*/
typedef struct MDB_txn MDB_txn;
-/** A handle for an individual database in the DB environment. */
+/** @brief A handle for an individual database in the DB environment. */
typedef unsigned int MDB_dbi;
-/** Opaque structure for navigating through a database */
+/** @brief Opaque structure for navigating through a database */
typedef struct MDB_cursor MDB_cursor;
-/** Generic structure used for passing keys and data in and out of the database. */
+/** @brief Generic structure used for passing keys and data in and out of the database. */
typedef struct MDB_val {
size_t mv_size; /**< size of the data item */
void *mv_data; /**< address of the data item */
} MDB_val;
-/** A callback function used to compare two keys in a database */
+/** @brief A callback function used to compare two keys in a database */
typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
-/** A callback function used to relocate a position-dependent data item
- * in a fixed-address database. The \b newptr gives the item's current address in
- * the memory map, and \b oldptr gives its previous address. This callback is
- * expected to walk through the fields of the record in newptr and modify any
+/** @brief A callback function used to relocate a position-dependent data item
+ * in a fixed-address database.
+ *
+ * The \b newptr gives the item's desired address in
+ * the memory map, and \b oldptr gives its previous address. The item's actual
+ * data resides at the address in \b item. This callback is expected to walk
+ * through the fields of the record in \b item and modify any
* values based at the \b oldptr address to be relative to the \b newptr address.
+ * @param[in,out] item The item that is to be relocated.
+ * @param[in] oldptr The previous address.
+ * @param[in] newptr The new address to relocate to.
+ * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
* @todo This feature is currently unimplemented.
*/
-typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size);
+typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
/** @defgroup mdb_env Environment Flags
* @{
*/
/** mmap at a fixed address */
#define MDB_FIXEDMAP 0x01
+ /** no environment directory */
+#define MDB_NOSUBDIR 0x02
/** don't fsync after commit */
#define MDB_NOSYNC 0x10000
/** read only */
#define MDB_RDONLY 0x20000
+ /** don't fsync metapage after commit */
+#define MDB_NOMETASYNC 0x40000
+ /** use writable mmap */
+#define MDB_WRITEMAP 0x80000
+ /** use asynchronous msync */
+#define MDB_MAPASYNC 0x100000
/** @} */
/** @defgroup mdb_open Database Flags
#define MDB_NODUPDATA 0x20
/** For mdb_cursor_put: overwrite the current key/data pair */
#define MDB_CURRENT 0x40
+/** For put: Just reserve space for data, don't copy it. Return a
+ * pointer to the reserved space.
+ */
+#define MDB_RESERVE 0x10000
+/** Data is being appended, don't split full pages. */
+#define MDB_APPEND 0x20000
+/** Duplicate data is being appended, don't split full pages. */
+#define MDB_APPENDDUP 0x40000
+/** Store multiple data items in one call. */
+#define MDB_MULTIPLE 0x80000
/* @} */
-/** Cursor operations */
+/** @brief Cursor Get operations.
+ *
+ * This is the set of all operations for retrieving data
+ * using a cursor.
+ */
typedef enum MDB_cursor_op {
MDB_FIRST, /**< Position at first key/data item */
MDB_FIRST_DUP, /**< Position at first data item of current key.
Only for #MDB_DUPSORT */
MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
+ MDB_GET_CURRENT, /**< Return key/data at current cursor position */
MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current
cursor position. Only for #MDB_DUPFIXED */
MDB_LAST, /**< Position at last key/data item */
MDB_PREV_NODUP, /**< Position at last data item of previous key.
Only for #MDB_DUPSORT */
MDB_SET, /**< Position at specified key */
+ MDB_SET_KEY, /**< Position at specified key, return key + data */
MDB_SET_RANGE /**< Position at first key greater than or equal to specified key. */
} MDB_cursor_op;
#define MDB_PANIC (-30795)
/** Environment version mismatch */
#define MDB_VERSION_MISMATCH (-30794)
+ /** File is not a valid MDB file */
+#define MDB_INVALID (-30793)
+ /** Environment mapsize reached */
+#define MDB_MAP_FULL (-30792)
+ /** Environment maxdbs reached */
+#define MDB_DBS_FULL (-30791)
+ /** Environment maxreaders reached */
+#define MDB_READERS_FULL (-30790)
+ /** Too many TLS keys in use - Windows only */
+#define MDB_TLS_FULL (-30789)
+ /** Nested txn has too many dirty pages */
+#define MDB_TXN_FULL (-30788)
+ /** Cursor stack too deep - internal error */
+#define MDB_CURSOR_FULL (-30787)
+ /** Page has not enough space - internal error */
+#define MDB_PAGE_FULL (-30786)
+#define MDB_LAST_ERRCODE MDB_PAGE_FULL
/** @} */
-/** Statistics for a database in the environment */
+/** @brief Statistics for a database in the environment */
typedef struct MDB_stat {
unsigned int ms_psize; /**< Size of a database page.
This is currently the same for all databases. */
unsigned int ms_depth; /**< Depth (height) of the B-tree */
- unsigned long ms_branch_pages; /**< Number of internal (non-leaf) pages */
- unsigned long ms_leaf_pages; /**< Number of leaf pages */
- unsigned long ms_overflow_pages; /**< Number of overflow pages */
- unsigned long ms_entries; /**< Number of data items */
+ size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */
+ size_t ms_leaf_pages; /**< Number of leaf pages */
+ size_t ms_overflow_pages; /**< Number of overflow pages */
+ size_t ms_entries; /**< Number of data items */
} MDB_stat;
- /** Return the mdb library version information.
+/** @brief Information about the environment */
+typedef struct MDB_envinfo {
+ void *me_mapaddr; /**< Address of map, if fixed */
+ size_t me_mapsize; /**< Size of the data memory map */
+ size_t me_last_pgno; /**< ID of the last used page */
+ size_t me_last_txnid; /**< ID of the last committed transaction */
+ unsigned int me_maxreaders; /**< maximum number of threads for the environment */
+ unsigned int me_numreaders; /**< maximum number of threads used in the environment */
+} MDB_envinfo;
+
+ /** @brief Return the mdb library version information.
+ *
* @param[out] major if non-NULL, the library major version number is copied here
* @param[out] minor if non-NULL, the library minor version number is copied here
* @param[out] patch if non-NULL, the library patch version number is copied here
*/
char *mdb_version(int *major, int *minor, int *patch);
- /** Return a string describing a given error code.
+ /** @brief Return a string describing a given error code.
+ *
* This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
* function. If the error code is greater than or equal to 0, then the string
* returned by the system function strerror(3) is returned. If the error code
*/
char *mdb_strerror(int err);
- /** Create an MDB environment handle.
+ /** @brief Create an MDB environment handle.
+ *
* This function allocates memory for a #MDB_env structure. To release
* the allocated memory and discard the handle, call #mdb_env_close().
* Before the handle may be used, it must be opened using #mdb_env_open().
*/
int mdb_env_create(MDB_env **env);
- /** Open an environment handle.
+ /** @brief Open an environment handle.
+ *
* If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[in] path The directory in which the database files reside. This
* @param[in] flags Special options for this environment. This parameter
* must be set to 0 or by bitwise OR'ing together one or more of the
* values described here.
+ * Flags set by mdb_env_set_flags() are also used.
* <ul>
* <li>#MDB_FIXEDMAP
* use a fixed address for the mmap region. This flag must be specified
* across multiple invocations. This option may not always work, depending on
* how the operating system has allocated memory to shared libraries and other uses.
* The feature is highly experimental.
+ * <li>#MDB_NOSUBDIR
+ * By default, MDB creates its environment in a directory whose
+ * pathname is given in \b path, and creates its data and lock files
+ * under that directory. With this option, \b path is used as-is for
+ * the database main data file. The database lock file is the \b path
+ * with "-lock" appended.
* <li>#MDB_NOSYNC
* Don't perform a synchronous flush after committing a transaction. This means
* transactions will exhibit the ACI (atomicity, consistency, and isolation)
* at risk is governed by how often the system flushes dirty buffers to disk
* and how often #mdb_env_sync() is called. This flag may be changed
* at any time using #mdb_env_set_flags().
+ * <li>#MDB_NOMETASYNC
+ * Don't perform a synchronous flush of the meta page after committing
+ * a transaction. This is similar to the #MDB_NOSYNC case, but safer
+ * because the transaction data is still flushed. The meta page for any
+ * transaction N will be flushed by the data flush of transaction N+1.
+ * In case of a system crash, the last committed transaction may be
+ * lost. This flag may be changed at any time using #mdb_env_set_flags().
* <li>#MDB_RDONLY
* Open the environment in read-only mode. No write operations will be allowed.
* </ul>
*/
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
- /** Return statistics about the MDB environment.
+ /** @brief Copy an MDB environment to the specified path.
+ *
+ * This function may be used to make a backup of an existing environment.
+ * @param[in] env An environment handle returned by #mdb_env_create(). It
+ * must have already been opened successfully.
+ * @param[in] path The directory in which the copy will reside. This
+ * directory must already exist and be writable but must otherwise be
+ * empty.
+ * @return A non-zero error value on failure and 0 on success.
+ */
+int mdb_env_copy(MDB_env *env, const char *path);
+
+ /** @brief Return statistics about the MDB environment.
+ *
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[out] stat The address of an #MDB_stat structure
* where the statistics will be copied
*/
int mdb_env_stat(MDB_env *env, MDB_stat *stat);
- /** Flush the data buffers to disk.
+ /** @brief Return information about the MDB environment.
+ *
+ * @param[in] env An environment handle returned by #mdb_env_create()
+ * @param[out] stat The address of an #MDB_envinfo structure
+ * where the information will be copied
+ */
+int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
+
+ /** @brief Flush the data buffers to disk.
+ *
* Data is always written to disk when #mdb_txn_commit() is called,
* but the operating system may keep it buffered. MDB always flushes
* the OS buffers upon commit as well, unless the environment was
*/
int mdb_env_sync(MDB_env *env, int force);
- /** Close the environment and release the memory map.
+ /** @brief Close the environment and release the memory map.
+ *
* Only a single thread may call this function. All transactions, databases,
* and cursors must already be closed before calling this function. Attempts to
* use any such handles after calling this function will cause a SIGSEGV.
*/
void mdb_env_close(MDB_env *env);
- /** Set environment flags.
- * This may be used to set some flags that weren't already set during
- * #mdb_env_open(), or to unset these flags. Currently only the
- * #MDB_NOSYNC flag setting may be changed with this function.
+ /** @brief Set environment flags.
+ *
+ * This may be used to set some flags in addition to those from
+ * #mdb_env_open(), or to unset these flags.
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[in] flags The flags to change, bitwise OR'ed together
* @param[in] onoff A non-zero value sets the flags, zero clears them.
*/
int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
- /** Get environment flags.
+ /** @brief Get environment flags.
+ *
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[out] flags The address of an integer to store the flags
* @return A non-zero error value on failure and 0 on success. Some possible
*/
int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
- /** Return the path that was used in #mdb_env_open().
+ /** @brief Return the path that was used in #mdb_env_open().
+ *
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[out] path Address of a string pointer to contain the path. This
* is the actual string in the environment, not a copy. It should not be
*/
int mdb_env_get_path(MDB_env *env, const char **path);
- /** Set the size of the memory map to use for this environment.
+ /** @brief Set the size of the memory map to use for this environment.
+ *
* The size should be a multiple of the OS page size. The default is
* 10485760 bytes. The size of the memory map is also the maximum size
* of the database. The value should be chosen as large as possible,
- * to accomodate future growth of the database.
+ * to accommodate future growth of the database.
* This function may only be called after #mdb_env_create() and before #mdb_env_open().
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[in] size The size in bytes
*/
int mdb_env_set_mapsize(MDB_env *env, size_t size);
- /** Set the maximum number of threads for the environment.
+ /** @brief Set the maximum number of threads for the environment.
+ *
* This defines the number of slots in the lock table that is used to track readers in the
* the environment. The default is 126.
* This function may only be called after #mdb_env_create() and before #mdb_env_open().
*/
int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
- /** Get the maximum number of threads for the environment.
+ /** @brief Get the maximum number of threads for the environment.
+ *
* @param[in] env An environment handle returned by #mdb_env_create()
* @param[out] readers Address of an integer to store the number of readers
* @return A non-zero error value on failure and 0 on success. Some possible
*/
int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
- /** Set the maximum number of databases for the environment.
+ /** @brief Set the maximum number of databases for the environment.
+ *
* This function is only needed if multiple databases will be used in the
* environment. Simpler applications that only use a single database can ignore
* this option.
* <li>EINVAL - an invalid parameter was specified, or the environment is already open.
* </ul>
*/
-int mdb_env_set_maxdbs(MDB_env *env, int dbs);
+int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
- /** Create a transaction for use with the environment.
+ /** @brief Create a transaction for use with the environment.
+ *
* The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
- * @note Transactions may not span threads, a transaction must only be used by a
- * single thread.
+ * @note Transactions may not span threads; a transaction must only be used by a
+ * single thread. Also, a thread may only have a single transaction.
* @note Cursors may not span transactions; each cursor must be opened and closed
* within a single transaction.
* @param[in] env An environment handle returned by #mdb_env_create()
+ * @param[in] parent If this parameter is non-NULL, the new transaction
+ * will be a nested transaction, with the transaction indicated by \b parent
+ * as its parent. Transactions may be nested to any level. A parent
+ * transaction may not issue any other operations besides mdb_txn_begin,
+ * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
* @param[in] flags Special options for this transaction. This parameter
* must be set to 0 or by bitwise OR'ing together one or more of the
* values described here.
* the reader lock table is full. See #mdb_env_set_maxreaders().
* </ul>
*/
-int mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn);
+int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
- /** Commit all the operations of a transaction into the database.
- * All cursors opened within the transaction must be closed before the transaction
- * is committed.
- * The transaction handle will be freed and must not be used again after this call.
+ /** @brief Commit all the operations of a transaction into the database.
+ *
+ * All cursors opened within the transaction will be closed by this call. The cursors
+ * and transaction handle will be freed and must not be used again after this call.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @return A non-zero error value on failure and 0 on success. Some possible
* errors are:
* <li>EINVAL - an invalid parameter was specified.
* <li>ENOSPC - no more disk space.
* <li>EIO - a low-level I/O error occurred while writing.
+ * <li>ENOMEM - the transaction is nested and could not be merged into its parent.
* </ul>
*/
int mdb_txn_commit(MDB_txn *txn);
- /** Abandon all the operations of the transaction instead of saving them.
- * All cursors opened within the transaction must be closed before the transaction
- * is aborted.
- * The transaction handle will be freed and must not be used again after this call.
+ /** @brief Abandon all the operations of the transaction instead of saving them.
+ *
+ * All cursors opened within the transaction will be closed by this call. The cursors
+ * and transaction handle will be freed and must not be used again after this call.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
*/
void mdb_txn_abort(MDB_txn *txn);
- /** Reset a read-only transaction.
+ /** @brief Reset a read-only transaction.
+ *
* This releases the current reader lock but doesn't free the
* transaction handle, allowing it to be used again later by #mdb_txn_renew().
* It otherwise has the same effect as #mdb_txn_abort() but saves some memory
*/
void mdb_txn_reset(MDB_txn *txn);
- /** Renew a read-only transaction.
+ /** @brief Renew a read-only transaction.
+ *
* This acquires a new reader lock for a transaction handle that had been
* released by #mdb_txn_reset(). It must be called before a reset transaction
* may be used again.
*/
int mdb_txn_renew(MDB_txn *txn);
- /** Open a database in the environment.
- * The database handle may be discarded by calling #mdb_close().
+ /** @brief Open a database in the environment.
+ *
+ * The database handle may be discarded by calling #mdb_close(). The
+ * database handle resides in the shared environment, it is not owned
+ * by the given transaction. Only one thread should call this function;
+ * it is not mutex-protected in a read-only transaction.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @param[in] name The name of the database to open. If only a single
- * database is needed in the enviroment, this value may be NULL.
+ * database is needed in the environment, this value may be NULL.
* @param[in] flags Special options for this database. This parameter
* must be set to 0 or by bitwise OR'ing together one or more of the
* values described here.
* <li>#MDB_INTEGERKEY
* Keys are binary integers in native byte order. Setting this option
* requires all keys to be the same size, typically sizeof(int)
- * or sizeof(long).
+ * or sizeof(size_t).
* <li>#MDB_DUPFIXED
* This flag may only be used in combination with #MDB_DUPSORT. This option
* tells the library that the data items for this database are all the same
*/
int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
- /** Retrieve statistics for a database.
+ /** @brief Retrieve statistics for a database.
+ *
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @param[in] dbi A database handle returned by #mdb_open()
* @param[out] stat The address of an #MDB_stat structure
*/
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
- /** Close a database handle.
+ /** @brief Close a database handle.
+ *
+ * This call is not mutex protected. Handles should only be closed by
+ * a single thread, and only if no other threads are going to reference
+ * the database handle any further.
+ * @param[in] env An environment handle returned by #mdb_env_create()
+ * @param[in] dbi A database handle returned by #mdb_open()
+ */
+void mdb_close(MDB_env *env, MDB_dbi dbi);
+
+ /** @brief Delete a database and/or free all its pages.
+ *
+ * If the \b del parameter is non-zero the DB handle will be closed
+ * and the DB will be deleted.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @param[in] dbi A database handle returned by #mdb_open()
+ * @param[in] del non-zero to delete the DB from the environment,
+ * otherwise just free its pages.
+ * @return A non-zero error value on failure and 0 on success.
*/
-void mdb_close(MDB_txn *txn, MDB_dbi dbi);
+int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
- /** Set a custom key comparison function for a database.
+ /** @brief Set a custom key comparison function for a database.
+ *
* The comparison function is called whenever it is necessary to compare a
* key specified by the application with a key currently stored in the database.
* If no comparison function is specified, and no special key flags were specified
* with #mdb_open(), the keys are compared lexically, with shorter keys collating
* before longer keys.
* @warning This function must be called before any data access functions are used,
- * otherwise data corruption may occur. The same function must be used by every
- * process accessing the database, every time the database is used.
+ * otherwise data corruption may occur. The same comparison function must be used by every
+ * program accessing the database, every time the database is used.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @param[in] dbi A database handle returned by #mdb_open()
* @param[in] cmp A #MDB_cmp_func function
*/
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
- /** Set a custom data comparison function for a #MDB_DUPSORT database.
+ /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
+ *
* This comparison function is called whenever it is necessary to compare a data
* item specified by the application with a data item currently stored in the database.
* This function only takes effect if the database was opened with the #MDB_DUPSORT
* with #mdb_open(), the data items are compared lexically, with shorter items collating
* before longer items.
* @warning This function must be called before any data access functions are used,
- * otherwise data corruption may occur. The same function must be used by every
- * process accessing the database, every time the database is used.
+ * otherwise data corruption may occur. The same comparison function must be used by every
+ * program accessing the database, every time the database is used.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
* @param[in] dbi A database handle returned by #mdb_open()
* @param[in] cmp A #MDB_cmp_func function
*/
int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
- /** Set a relocation function for a #MDB_FIXEDMAP database.
+ /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
+ *
* @todo The relocation function is called whenever it is necessary to move the data
* of an item to a different position in the database (e.g. through tree
* balancing operations, shifts as a result of adds or deletes, etc.). It is
*/
int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
- /** Get items from a database.
+ /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
+ *
+ * See #mdb_set_relfunc and #MDB_rel_func for more details.
+ * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+ * @param[in] dbi A database handle returned by #mdb_open()
+ * @param[in] ctx An arbitrary pointer for whatever the application needs.
+ * It will be passed to the callback function set by #mdb_set_relfunc
+ * as its \b relctx parameter whenever the callback is invoked.
+ * @return A non-zero error value on failure and 0 on success. Some possible
+ * errors are:
+ * <ul>
+ * <li>EINVAL - an invalid parameter was specified.
+ * </ul>
+ */
+int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
+
+ /** @brief Get items from a database.
+ *
* This function retrieves key/data pairs from the database. The address
* and length of the data associated with the specified \b key are returned
* in the structure to which \b data refers.
*/
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
- /** Store items into a database.
+ /** @brief Store items into a database.
+ *
* This function stores key/data pairs in the database. The default behavior
* is to enter the new key/data pair, replacing any previously existing key
* if duplicates are disallowed, or adding a duplicate data item if
* #MDB_KEYEXIST if the key already appears in the database, even if
* the database supports duplicates (#MDB_DUPSORT). The \b data
* parameter will be set to point to the existing item.
+ * <li>#MDB_RESERVE - reserve space for data of the given size, but
+ * don't copy the given data. Instead, return a pointer to the
+ * reserved space, which the caller can fill in later. This saves
+ * an extra memcpy if the data is being generated later.
+ * <li>#MDB_APPEND - append the given key/data pair to the end of the
+ * database. No key comparisons are performed. This option allows
+ * fast bulk loading when keys are already known to be in the
+ * correct order. Loading unsorted keys with this flag will cause
+ * data corruption.
+ * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
* </ul>
* @return A non-zero error value on failure and 0 on success. Some possible
* errors are:
* <ul>
- * <li>EACCESS - an attempt was made to write in a read-only transaction.
+ * <li>EACCES - an attempt was made to write in a read-only transaction.
* <li>EINVAL - an invalid parameter was specified.
* <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
* </ul>
int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
unsigned int flags);
- /** Delete items from a database.
+ /** @brief Delete items from a database.
+ *
* This function removes key/data pairs from the database.
* If the database does not support sorted duplicate data items
* (#MDB_DUPSORT) the data parameter is ignored.
* @return A non-zero error value on failure and 0 on success. Some possible
* errors are:
* <ul>
- * <li>EACCESS - an attempt was made to write in a read-only transaction.
+ * <li>EACCES - an attempt was made to write in a read-only transaction.
* <li>EINVAL - an invalid parameter was specified.
* </ul>
*/
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
- /** Create a cursor handle.
+ /** @brief Create a cursor handle.
+ *
* Cursors are associated with a specific transaction and database and
* may not span threads.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
*/
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
- /** Close a cursor handle.
+ /** @brief Close a cursor handle.
+ *
* The cursor handle will be freed and must not be used again after this call.
* @param[in] cursor A cursor handle returned by #mdb_cursor_open()
*/
void mdb_cursor_close(MDB_cursor *cursor);
- /** Retrieve by cursor.
+ /** @brief Renew a cursor handle.
+ *
+ * Cursors are associated with a specific transaction and database and
+ * may not span threads. Cursors that are only used in read-only
+ * transactions may be re-used, to avoid unnecessary malloc/free overhead.
+ * The cursor may be associated with a new read-only transaction, and
+ * referencing the same database handle as it was created with.
+ * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+ * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
+ * @return A non-zero error value on failure and 0 on success. Some possible
+ * errors are:
+ * <ul>
+ * <li>EINVAL - an invalid parameter was specified.
+ * </ul>
+ */
+int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
+
+ /** @brief Return the cursor's transaction handle.
+ *
+ * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
+ */
+MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
+
+ /** @brief Return the cursor's database handle.
+ *
+ * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
+ */
+MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
+
+ /** @brief Retrieve by cursor.
+ *
* This function retrieves key/data pairs from the database. The address and length
* of the key are returned in the object to which \b key refers (except for the
* case of the #MDB_SET option, in which the \b key object is unchanged), and
* the address and length of the data are returned in the object to which \b data
* refers.
- * @bug Cursors are not coordinated with write operations. If a cursor in a
- * write transaction is performing a sequential scan while records are being
- * inserted or deleted in the same transaction, the cursor will be corrupted.
* @param[in] cursor A cursor handle returned by #mdb_cursor_open()
* @param[in,out] key The key for a retrieved item
* @param[in,out] data The data of a retrieved item
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
MDB_cursor_op op);
- /** Store by cursor.
+ /** @brief Store by cursor.
+ *
* This function stores key/data pairs into the database.
* If the function fails for any reason, the state of the cursor will be
* unchanged. If the function succeeds and an item is inserted into the
* does not already appear in the database. The function will return
* #MDB_KEYEXIST if the key already appears in the database, even if
* the database supports duplicates (#MDB_DUPSORT).
+ * <li>#MDB_RESERVE - reserve space for data of the given size, but
+ * don't copy the given data. Instead, return a pointer to the
+ * reserved space, which the caller can fill in later. This saves
+ * an extra memcpy if the data is being generated later.
+ * <li>#MDB_APPEND - append the given key/data pair to the end of the
+ * database. No key comparisons are performed. This option allows
+ * fast bulk loading when keys are already known to be in the
+ * correct order. Loading unsorted keys with this flag will cause
+ * data corruption.
+ * <li>#MDB_APPENDDUP - as above, but for sorted dup data.
* </ul>
* @return A non-zero error value on failure and 0 on success. Some possible
* errors are:
int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
unsigned int flags);
- /** Delete current key/data pair
+ /** @brief Delete current key/data pair
+ *
* This function deletes the key/data pair to which the cursor refers.
* @param[in] cursor A cursor handle returned by #mdb_cursor_open()
* @param[in] flags Options for this operation. This parameter
*/
int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
- /** Return count of duplicates for current key.
+ /** @brief Return count of duplicates for current key.
+ *
* This call is only valid on databases that support sorted duplicate
* data items #MDB_DUPSORT.
* @param[in] cursor A cursor handle returned by #mdb_cursor_open()
* <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
* </ul>
*/
-int mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
+int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
- /** Compare two data items according to a particular database.
+ /** @brief Compare two data items according to a particular database.
+ *
* This returns a comparison as if the two data items were keys in the
* specified database.
* @param[in] txn A transaction handle returned by #mdb_txn_begin()
*/
int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
+ /** @brief Compare two data items according to a particular database.
+ *
+ * This returns a comparison as if the two items were data items of
+ * a sorted duplicates #MDB_DUPSORT database.
+ * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+ * @param[in] dbi A database handle returned by #mdb_open()
+ * @param[in] a The first item to compare
+ * @param[in] b The second item to compare
+ * @return < 0 if a < b, 0 if a == b, > 0 if a > b
+ */
+int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
/** @} */
+
+#ifdef __cplusplus
+}
+#endif
#endif /* _MDB_H_ */