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 /** Opaque structure for a database environment. A DB environment supports multiple
103 * databases, all residing in the same shared-memory map.
105 typedef struct MDB_env MDB_env;
107 /** Opaque structure for a transaction handle. All database operations require
108 * a transaction handle. Transactions may be read-only or read-write.
110 typedef struct MDB_txn MDB_txn;
112 /** A handle for an individual database in the DB environment. */
113 typedef unsigned int MDB_dbi;
115 /** Opaque structure for navigating through a database */
116 typedef struct MDB_cursor MDB_cursor;
118 /** Generic structure used for passing keys and data in and out of the database. */
119 typedef struct MDB_val {
120 size_t mv_size; /**< size of the data item */
121 void *mv_data; /**< address of the data item */
124 /** A callback function used to compare two keys in a database */
125 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
127 /** A callback function used to relocate a position-dependent data item
128 * in a fixed-address database. The \b newptr gives the item's current address in
129 * the memory map, and \b oldptr gives its previous address. This callback is
130 * expected to walk through the fields of the record in newptr and modify any
131 * values based at the \b oldptr address to be relative to the \b newptr address.
132 * @todo This feature is currently unimplemented.
134 typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size);
136 /** @defgroup mdb_env Environment Flags
139 /** mmap at a fixed address */
140 #define MDB_FIXEDMAP 0x01
141 /** don't fsync after commit */
142 #define MDB_NOSYNC 0x10000
144 #define MDB_RDONLY 0x20000
147 /** @defgroup mdb_open Database Flags
150 /** use reverse string keys */
151 #define MDB_REVERSEKEY 0x02
152 /** use sorted duplicates */
153 #define MDB_DUPSORT 0x04
154 /** numeric keys in native byte order */
155 #define MDB_INTEGERKEY 0x08
156 /** with #MDB_DUPSORT, sorted dup items have fixed size */
157 #define MDB_DUPFIXED 0x10
158 /** with #MDB_DUPSORT, dups are numeric in native byte order */
159 #define MDB_INTEGERDUP 0x20
160 /** create DB if not already existing */
161 #define MDB_CREATE 0x40000
164 /** @defgroup mdb_put Write Flags
167 /** For put: Don't write if the key already exists. */
168 #define MDB_NOOVERWRITE 0x10
169 /** Only for #MDB_DUPSORT<br>
170 * For put: don't write if the key and data pair already exist.<br>
171 * For mdb_cursor_del: remove all duplicate data items.
173 #define MDB_NODUPDATA 0x20
174 /** For mdb_cursor_put: overwrite the current key/data pair */
175 #define MDB_CURRENT 0x40
178 /** Cursor operations */
179 typedef enum MDB_cursor_op {
180 MDB_FIRST, /**< Position at first key/data item */
181 MDB_FIRST_DUP, /**< Position at first data item of current key.
182 Only for #MDB_DUPSORT */
183 MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */
184 MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */
185 MDB_GET_MULTIPLE, /**< Return all the duplicate data items at the current
186 cursor position. Only for #MDB_DUPFIXED */
187 MDB_LAST, /**< Position at last key/data item */
188 MDB_LAST_DUP, /**< Position at last data item of current key.
189 Only for #MDB_DUPSORT */
190 MDB_NEXT, /**< Position at next data item */
191 MDB_NEXT_DUP, /**< Position at next data item of current key.
192 Only for #MDB_DUPSORT */
193 MDB_NEXT_MULTIPLE, /**< Return all duplicate data items at the next
194 cursor position. Only for #MDB_DUPFIXED */
195 MDB_NEXT_NODUP, /**< Position at first data item of next key.
196 Only for #MDB_DUPSORT */
197 MDB_PREV, /**< Position at previous data item */
198 MDB_PREV_DUP, /**< Position at previous data item of current key.
199 Only for #MDB_DUPSORT */
200 MDB_PREV_NODUP, /**< Position at last data item of previous key.
201 only for #MDB_DUPSORT */
202 MDB_SET, /**< Position at specified key */
203 MDB_SET_RANGE /**< Position at first key greater than or equal to specified key. */
206 /** @defgroup errors Return Codes
208 * BerkeleyDB uses -30800 to -30999, we'll go under them
211 /** Successful result */
212 #define MDB_SUCCESS 0
213 /** key/data pair already exists */
214 #define MDB_KEYEXIST (-30799)
215 /** key/data pair not found (EOF) */
216 #define MDB_NOTFOUND (-30798)
217 /** Requested page not found - this usually indicates corruption */
218 #define MDB_PAGE_NOTFOUND (-30797)
219 /** Located page was wrong type */
220 #define MDB_CORRUPTED (-30796)
221 /** Update of meta page failed, probably I/O error */
222 #define MDB_PANIC (-30795)
223 /** Environment version mismatch */
224 #define MDB_VERSION_MISMATCH (-30794)
227 /** Statistics for a database in the environment */
228 typedef struct MDB_stat {
229 unsigned int ms_psize; /**< Size of a database page.
230 This is currently the same for all databases. */
231 unsigned int ms_depth; /**< Depth (height) of the B-tree */
232 unsigned long ms_branch_pages; /**< Number of internal (non-leaf) pages */
233 unsigned long ms_leaf_pages; /**< Number of leaf pages */
234 unsigned long ms_overflow_pages; /**< Number of overflow pages */
235 unsigned long ms_entries; /**< Number of data items */
238 /** Return the mdb library version information.
239 * @param[out] major if non-NULL, the library major version number is copied here
240 * @param[out] minor if non-NULL, the library minor version number is copied here
241 * @param[out] patch if non-NULL, the library patch version number is copied here
242 * @retval "version string" The library version as a string
244 char *mdb_version(int *major, int *minor, int *patch);
246 /** Return a string describing a given error code.
247 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
248 * function. If the error code is greater than or equal to 0, then the string
249 * returned by the system function strerror(3) is returned. If the error code
250 * is less than 0, an error string corresponding to the MDB library error is
251 * returned. See @ref errors for a list of MDB-specific error codes.
252 * @param[in] err The error code
253 * @retval "error message" The description of the error
255 char *mdb_strerror(int err);
257 /** Create an MDB environment handle.
258 * This function allocates memory for a #MDB_env structure. To release
259 * the allocated memory and discard the handle, call #mdb_env_close().
260 * Before the handle may be used, it must be opened using #mdb_env_open().
261 * Various other options may also need to be set before opening the handle,
262 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
263 * depending on usage requirements.
264 * @param[out] env The address where the new handle will be stored
265 * @return A non-zero error value on failure and 0 on success.
267 int mdb_env_create(MDB_env **env);
269 /** Open an environment handle.
270 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
271 * @param[in] env An environment handle returned by #mdb_env_create()
272 * @param[in] path The directory in which the database files reside. This
273 * directory must already exist and be writable.
274 * @param[in] flags Special options for this environment. This parameter
275 * must be set to 0 or by bitwise OR'ing together one or more of the
276 * values described here.
279 * use a fixed address for the mmap region. This flag must be specified
280 * when creating the environment, and is stored persistently in the environment.
281 * If successful, the memory map will always reside at the same virtual address
282 * and pointers used to reference data items in the database will be constant
283 * across multiple invocations. This option may not always work, depending on
284 * how the operating system has allocated memory to shared libraries and other uses.
285 * The feature is highly experimental.
287 * Don't perform a synchronous flush after committing a transaction. This means
288 * transactions will exhibit the ACI (atomicity, consistency, and isolation)
289 * properties, but not D (durability); that is database integrity will be
290 * maintained but it is possible some number of the most recently committed
291 * transactions may be undone after a system crash. The number of transactions
292 * at risk is governed by how often the system flushes dirty buffers to disk
293 * and how often #mdb_env_sync() is called. This flag may be changed
294 * at any time using #mdb_env_set_flags().
296 * Open the environment in read-only mode. No write operations will be allowed.
298 * @param[in] mode The UNIX permissions to set on created files. This parameter
299 * is ignored on Windows.
300 * @return A non-zero error value on failure and 0 on success. Some possible
303 * <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
304 * version that created the database environment.
305 * <li>EINVAL - the environment file headers are corrupted.
306 * <li>ENOENT - the directory specified by the path parameter doesn't exist.
307 * <li>EACCES - the user didn't have permission to access the environment files.
308 * <li>EAGAIN - the environment was locked by another process.
311 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
313 /** Return statistics about the MDB environment.
314 * @param[in] env An environment handle returned by #mdb_env_create()
315 * @param[out] stat The address of an #MDB_stat structure
316 * where the statistics will be copied
318 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
320 /** Flush the data buffers to disk.
321 * Data is always written to disk when #mdb_txn_commit() is called,
322 * but the operating system may keep it buffered. MDB always flushes
323 * the OS buffers upon commit as well, unless the environment was
324 * opened with #MDB_NOSYNC.
325 * @param[in] env An environment handle returned by #mdb_env_create()
326 * @param[in] force If non-zero, force the flush to occur. Otherwise
327 * if the environment has the #MDB_NOSYNC flag set the flushes
329 * @return A non-zero error value on failure and 0 on success. Some possible
332 * <li>EINVAL - an invalid parameter was specified.
333 * <li>EIO - an error occurred during synchronization.
336 int mdb_env_sync(MDB_env *env, int force);
338 /** Close the environment and release the memory map.
339 * Only a single thread may call this function. All transactions, databases,
340 * and cursors must already be closed before calling this function. Attempts to
341 * use any such handles after calling this function will cause a SIGSEGV.
342 * The environment handle will be freed and must not be used again after this call.
343 * @param[in] env An environment handle returned by #mdb_env_create()
345 void mdb_env_close(MDB_env *env);
347 /** Set environment flags.
348 * This may be used to set some flags that weren't already set during
349 * #mdb_env_open(), or to unset these flags. Currently only the
350 * #MDB_NOSYNC flag setting may be changed with this function.
351 * @param[in] env An environment handle returned by #mdb_env_create()
352 * @param[in] flags The flags to change, bitwise OR'ed together
353 * @param[in] onoff A non-zero value sets the flags, zero clears them.
354 * @return A non-zero error value on failure and 0 on success. Some possible
357 * <li>EINVAL - an invalid parameter was specified.
360 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
362 /** Get environment flags.
363 * @param[in] env An environment handle returned by #mdb_env_create()
364 * @param[out] flags The address of an integer to store the flags
365 * @return A non-zero error value on failure and 0 on success. Some possible
368 * <li>EINVAL - an invalid parameter was specified.
371 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
373 /** Return the path that was used in #mdb_env_open().
374 * @param[in] env An environment handle returned by #mdb_env_create()
375 * @param[out] path Address of a string pointer to contain the path. This
376 * is the actual string in the environment, not a copy. It should not be
377 * altered in any way.
378 * @return A non-zero error value on failure and 0 on success. Some possible
381 * <li>EINVAL - an invalid parameter was specified.
384 int mdb_env_get_path(MDB_env *env, const char **path);
386 /** Set the size of the memory map to use for this environment.
387 * The size should be a multiple of the OS page size. The default is
388 * 10485760 bytes. The size of the memory map is also the maximum size
389 * of the database. The value should be chosen as large as possible,
390 * to accomodate future growth of the database.
391 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
392 * @param[in] env An environment handle returned by #mdb_env_create()
393 * @param[in] size The size in bytes
394 * @return A non-zero error value on failure and 0 on success. Some possible
397 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
400 int mdb_env_set_mapsize(MDB_env *env, size_t size);
402 /** Set the maximum number of threads for the environment.
403 * This defines the number of slots in the lock table that is used to track readers in the
404 * the environment. The default is 126.
405 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
406 * @param[in] env An environment handle returned by #mdb_env_create()
407 * @param[in] readers The maximum number of threads
408 * @return A non-zero error value on failure and 0 on success. Some possible
411 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
414 int mdb_env_set_maxreaders(MDB_env *env, int readers);
416 /** Get the maximum number of threads for the environment.
417 * @param[in] env An environment handle returned by #mdb_env_create()
418 * @param[out] readers Address of an integer to store the number of readers
419 * @return A non-zero error value on failure and 0 on success. Some possible
422 * <li>EINVAL - an invalid parameter was specified.
425 int mdb_env_get_maxreaders(MDB_env *env, int *readers);
427 /** Set the maximum number of databases for the environment.
428 * This function is only needed if multiple databases will be used in the
429 * environment. Simpler applications that only use a single database can ignore
431 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
432 * @param[in] env An environment handle returned by #mdb_env_create()
433 * @param[in] dbs The maximum number of databases
434 * @return A non-zero error value on failure and 0 on success. Some possible
437 * <li>EINVAL - an invalid parameter was specified, or the environment is already open.
440 int mdb_env_set_maxdbs(MDB_env *env, int dbs);
442 /** Create a transaction for use with the environment.
443 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
444 * @note Transactions may not span threads, a transaction must only be used by a
446 * @note Cursors may not span transactions; each cursor must be opened and closed
447 * within a single transaction.
448 * @param[in] env An environment handle returned by #mdb_env_create()
449 * @param[in] flags Special options for this transaction. This parameter
450 * must be set to 0 or by bitwise OR'ing together one or more of the
451 * values described here.
454 * This transaction will not perform any write operations.
456 * @param[out] txn Address where the new #MDB_txn handle will be stored
457 * @return A non-zero error value on failure and 0 on success. Some possible
460 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
462 * <li>ENOMEM - out of memory, or a read-only transaction was requested and
463 * the reader lock table is full. See #mdb_env_set_maxreaders().
466 int mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn);
468 /** Commit all the operations of a transaction into the database.
469 * All cursors opened within the transaction must be closed before the transaction
471 * The transaction handle will be freed and must not be used again after this call.
472 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
473 * @return A non-zero error value on failure and 0 on success. Some possible
476 * <li>EINVAL - an invalid parameter was specified.
477 * <li>ENOSPC - no more disk space.
478 * <li>EIO - a low-level I/O error occurred while writing.
481 int mdb_txn_commit(MDB_txn *txn);
483 /** Abandon all the operations of the transaction instead of saving them.
484 * All cursors opened within the transaction must be closed before the transaction
486 * The transaction handle will be freed and must not be used again after this call.
487 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
489 void mdb_txn_abort(MDB_txn *txn);
491 /** Reset a read-only transaction.
492 * This releases the current reader lock but doesn't free the
493 * transaction handle, allowing it to be used again later by #mdb_txn_renew().
494 * It otherwise has the same affect as #mdb_txn_abort() but saves some memory
495 * allocation/deallocation overhead if a thread is going to start a new
496 * read-only transaction again soon.
497 * All cursors opened within the transaction must be closed before the transaction
499 * Reader locks generally don't interfere with writers, but they keep old
500 * versions of database pages allocated. Thus they prevent the old pages
501 * from being reused when writers commit new data, and so under heavy load
502 * the database size may grow much more rapidly than otherwise.
503 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
505 void mdb_txn_reset(MDB_txn *txn);
507 /** Renew a read-only transaction.
508 * This acquires a new reader lock for a transaction handle that had been
509 * released by #mdb_txn_reset(). It must be called before a reset transaction
511 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
512 * @return A non-zero error value on failure and 0 on success. Some possible
515 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment
517 * <li>EINVAL - an invalid parameter was specified.
520 int mdb_txn_renew(MDB_txn *txn);
522 /** Open a database in the environment.
523 * The database handle may be discarded by calling #mdb_close().
524 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
525 * @param[in] name The name of the database to open. If only a single
526 * database is needed in the enviroment, this value may be NULL.
527 * @param[in] flags Special options for this database. This parameter
528 * must be set to 0 or by bitwise OR'ing together one or more of the
529 * values described here.
531 * <li>#MDB_REVERSEKEY
532 * Keys are strings to be compared in reverse order, from the end
533 * of the strings to the beginning. By default, Keys are treated as strings and
534 * compared from beginning to end.
536 * Duplicate keys may be used in the database. (Or, from another perspective,
537 * keys may have multiple data items, stored in sorted order.) By default
538 * keys must be unique and may have only a single data item.
539 * <li>#MDB_INTEGERKEY
540 * Keys are binary integers in native byte order. On Big-Endian systems
541 * this flag has no effect. On Little-Endian systems this flag behaves
542 * the same as #MDB_REVERSEKEY. This flag is simply provided as a
543 * convenience so that applications don't need to detect Endianness themselves
544 * when using integers as keys.
546 * This flag may only be used in combination with #MDB_DUPSORT. This option
547 * tells the library that the data items for this database are all the same
548 * size, which allows further optimizations in storage and retrieval. When
549 * all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
550 * cursor operations may be used to retrieve multiple items at once.
551 * <li>#MDB_INTEGERDUP
552 * This option specifies that duplicate data items are also integers, and
553 * should be sorted as such.
555 * Create the named database if it doesn't exist. This option is not
556 * allowed in a read-only transaction or a read-only environment.
558 * @param[out] dbi Address where the new #MDB_dbi handle will be stored
559 * @return A non-zero error value on failure and 0 on success. Some possible
562 * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
563 * and #MDB_CREATE was not specified.
564 * <li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
567 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
569 /** Retrieve statistics for a database.
570 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
571 * @param[in] dbi A database handle returned by #mdb_open()
572 * @param[out] stat The address of an #MDB_stat structure
573 * where the statistics will be copied
574 * @return A non-zero error value on failure and 0 on success. Some possible
577 * <li>EINVAL - an invalid parameter was specified.
580 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
582 /** Close a database handle.
583 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
584 * @param[in] dbi A database handle returned by #mdb_open()
586 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
588 /** Set a custom key comparison function for a database.
589 * The comparison function is called whenever it is necessary to compare a
590 * key specified by the application with a key currently stored in the database.
591 * If no comparison function is specified, and no special key flags were specified
592 * with #mdb_open(), the keys are compared lexically, with shorter keys collating
593 * before longer keys.
594 * @warning This function must be called before any data access functions are used,
595 * otherwise data corruption may occur. The same function must be used by every
596 * process accessing the database, every time the database is used.
597 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
598 * @param[in] dbi A database handle returned by #mdb_open()
599 * @param[in] cmp A #MDB_cmp_func function
600 * @return A non-zero error value on failure and 0 on success. Some possible
603 * <li>EINVAL - an invalid parameter was specified.
606 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
608 /** Set a custom data comparison function for a #MDB_DUPSORT database.
609 * This comparison function is called whenever it is necessary to compare a data
610 * item specified by the application with a data item currently stored in the database.
611 * This function only takes effect if the database was opened with the #MDB_DUPSORT
613 * If no comparison function is specified, and no special key flags were specified
614 * with #mdb_open(), the data items are compared lexically, with shorter items collating
615 * before longer items.
616 * @warning This function must be called before any data access functions are used,
617 * otherwise data corruption may occur. The same function must be used by every
618 * process accessing the database, every time the database is used.
619 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
620 * @param[in] dbi A database handle returned by #mdb_open()
621 * @param[in] cmp A #MDB_cmp_func function
622 * @return A non-zero error value on failure and 0 on success. Some possible
625 * <li>EINVAL - an invalid parameter was specified.
628 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
630 /** Set a relocation function for a #MDB_FIXEDMAP database.
631 * @todo The relocation function is called whenever it is necessary to move the data
632 * of an item to a different position in the database (e.g. through tree
633 * balancing operations, shifts as a result of adds or deletes, etc.). It is
634 * intended to allow address/position-dependent data items to be stored in
635 * a database in an environment opened with the #MDB_FIXEDMAP option.
636 * Currently the relocation feature is unimplemented and setting
637 * this function has no effect.
638 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
639 * @param[in] dbi A database handle returned by #mdb_open()
640 * @param[in] rel A #MDB_rel_func function
641 * @return A non-zero error value on failure and 0 on success. Some possible
644 * <li>EINVAL - an invalid parameter was specified.
647 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
649 /** Get items from a database.
650 * This function retrieves key/data pairs from the database. The address
651 * and length of the data associated with the specified \b key are returned
652 * in the structure to which \b data refers.
653 * If the database supports duplicate keys (#MDB_DUPSORT) then the
654 * first data item for the key will be returned. Retrieval of other
655 * items requires the use of #mdb_cursor_get().
656 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
657 * @param[in] dbi A database handle returned by #mdb_open()
658 * @param[in] key The key to search for in the database
659 * @param[out] data The data corresponding to the key
660 * @return A non-zero error value on failure and 0 on success. Some possible
663 * <li>#MDB_NOTFOUND - the key was not in the database.
664 * <li>EINVAL - an invalid parameter was specified.
667 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
669 /** Store items into a database.
670 * This function stores key/data pairs in the database. The default behavior
671 * is to enter the new key/data pair, replacing any previously existing key
672 * if duplicates are disallowed, or adding a duplicate data item if
673 * duplicates are allowed (#MDB_DUPSORT).
674 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
675 * @param[in] dbi A database handle returned by #mdb_open()
676 * @param[in] key The key to store in the database
677 * @param[in] data The data to store
678 * @param[in] flags Special options for this operation. This parameter
679 * must be set to 0 or by bitwise OR'ing together one or more of the
680 * values described here.
682 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
683 * already appear in the database. This flag may only be specified
684 * if the database was opened with #MDB_DUPSORT. The function will
685 * return #MDB_KEYEXIST if the key/data pair already appears in the
687 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
688 * does not already appear in the database. The function will return
689 * #MDB_KEYEXIST if the key already appears in the database, even if
690 * the database supports duplicates (#MDB_DUPSORT).
692 * @return A non-zero error value on failure and 0 on success. Some possible
695 * <li>EACCESS - an attempt was made to write in a read-only transaction.
696 * <li>EINVAL - an invalid parameter was specified.
697 * <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
700 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
703 /** Delete items from a database.
704 * This function removes key/data pairs from the database.
705 * If the database does not support sorted duplicate data items
706 * (#MDB_DUPSORT) the data parameter is ignored.
707 * If the database supports sorted duplicates and the data parameter
708 * is NULL, all of the duplicate data items for the key will be
709 * deleted. Otherwise, if the data parameter is non-NULL
710 * only the matching data item will be deleted.
711 * This function will return #MDB_NOTFOUND if the specified key/data
712 * pair is not in the database.
713 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
714 * @param[in] dbi A database handle returned by #mdb_open()
715 * @param[in] key The key to delete from the database
716 * @param[in] data The data to delete
717 * @return A non-zero error value on failure and 0 on success. Some possible
720 * <li>EACCESS - an attempt was made to write in a read-only transaction.
721 * <li>EINVAL - an invalid parameter was specified.
724 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
726 /** Create a cursor handle.
727 * Cursors are associated with a specific transaction and database and
728 * may not span threads.
729 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
730 * @param[in] dbi A database handle returned by #mdb_open()
731 * @param[out] cursor Address where the new #MDB_cursor handle will be stored
732 * @return A non-zero error value on failure and 0 on success. Some possible
735 * <li>EINVAL - an invalid parameter was specified.
738 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
740 /** Close a cursor handle.
741 * The cursor handle will be freed and must not be used again after this call.
742 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
744 void mdb_cursor_close(MDB_cursor *cursor);
746 /** Retrieve by cursor.
747 * This function retrieves key/data pairs from the database. The address and length
748 * of the key are returned in the object to which \b key refers (except for the
749 * case of the #MDB_SET option, in which the \b key object is unchanged), and
750 * the address and length of the data are returned in the object to which \b data
752 * @bug Cursors are not coordinated with write operations. If a cursor in a
753 * write transaction is performing a sequential scan while records are being
754 * inserted or deleted in the same transaction, the cursor will be corrupted.
755 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
756 * @param[in,out] key The key for a retrieved item
757 * @param[in,out] data The data of a retrieved item
758 * @param[in] op A cursor operation #MDB_cursor_op
759 * @return A non-zero error value on failure and 0 on success. Some possible
762 * <li>#MDB_NOTFOUND - no matching key found.
763 * <li>EINVAL - an invalid parameter was specified.
766 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
770 * This function stores key/data pairs into the database.
771 * If the function fails for any reason, the state of the cursor will be
772 * unchanged. If the function succeeds and an item is inserted into the
773 * database, the cursor is always positioned to refer to the newly inserted item.
774 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
775 * @param[in] key The key operated on.
776 * @param[in] data The data operated on.
777 * @param[in] flags Options for this operation. This parameter
778 * must be set to 0 or one of the values described here.
780 * <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
781 * the cursor refers with the specified data item. The \b key
782 * parameter is ignored.
783 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
784 * already appear in the database. This flag may only be specified
785 * if the database was opened with #MDB_DUPSORT. The function will
786 * return #MDB_KEYEXIST if the key/data pair already appears in the
788 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
789 * does not already appear in the database. The function will return
790 * #MDB_KEYEXIST if the key already appears in the database, even if
791 * the database supports duplicates (#MDB_DUPSORT).
793 * @return A non-zero error value on failure and 0 on success. Some possible
796 * <li>EACCES - an attempt was made to modify a read-only database.
797 * <li>EINVAL - an invalid parameter was specified.
800 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
803 /** Delete current key/data pair
804 * This function deletes the key/data pair to which the cursor refers.
805 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
806 * @param[in] flags Options for this operation. This parameter
807 * must be set to 0 or one of the values described here.
809 * <li>#MDB_NODUPDATA - delete all of the data items for the current key.
810 * This flag may only be specified if the database was opened with #MDB_DUPSORT.
812 * @return A non-zero error value on failure and 0 on success. Some possible
815 * <li>EACCES - an attempt was made to modify a read-only database.
816 * <li>EINVAL - an invalid parameter was specified.
819 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
821 /** Return count of duplicates for current key.
822 * This call is only valid on databases that support sorted duplicate
823 * data items #MDB_DUPSORT.
824 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
825 * @param[out] countp Address where the count will be stored
826 * @return A non-zero error value on failure and 0 on success. Some possible
829 * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
832 int mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
834 /** Compare two data items according to a particular database.
835 * This returns a comparison as if the two data items were keys in the
836 * specified database.
837 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
838 * @param[in] dbi A database handle returned by #mdb_open()
839 * @param[in] a The first item to compare
840 * @param[in] b The second item to compare
841 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
843 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);