]> git.sur5r.net Git - openldap/blob - libraries/libmdb/mdb.h
7662c08d29517e8ff6cc115078493ea72277f493
[openldap] / libraries / libmdb / mdb.h
1 /** @file mdb.h
2  *      @brief memory-mapped database library
3  *
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
14  *      application code.
15  *
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.
25  *
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.
33  *
34  *      @author Howard Chu, Symas Corporation.
35  *
36  *      @copyright Copyright 2011 Howard Chu, Symas Corp. All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted only as authorized by the OpenLDAP
40  * Public License.
41  *
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>.
45  *
46  *      @par Derived From:
47  * This code is derived from btree.c written by Martin Hedenfalk.
48  *
49  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
50  *
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.
54  *
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.
62  */
63 #ifndef _MDB_H_
64 #define _MDB_H_
65
66 #include <sys/types.h>
67
68 /** @defgroup public Public API
69  *      @{
70  */
71 /** @defgroup Version Version Macros
72  *      @{
73  */
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
80
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))
83
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)
87
88 /** The release date of this library version */
89 #define MDB_VERSION_DATE        "September 1, 2011"
90
91 /** A stringifier for the version info */
92 #define MDB_VERSTR(a,b,c,d)     "MDB " #a "." #b "." #c ": (" #d ")"
93
94 /** A helper for the stringifier macro */
95 #define MDB_VERFOO(a,b,c,d)     MDB_VERSTR(a,b,c,d)
96
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)
100 /**     @} */
101
102 /** Opaque structure for a database environment. A DB environment supports multiple
103  * databases, all residing in the same shared-memory map.
104  */
105 typedef struct MDB_env MDB_env;
106
107 /** Opaque structure for a transaction handle. All database operations require
108  * a transaction handle. Transactions may be read-only or read-write.
109  */
110 typedef struct MDB_txn MDB_txn;
111
112 /** A handle for an individual database in the DB environment. */
113 typedef unsigned int    MDB_dbi;
114
115 /** Opaque structure for navigating through a database */
116 typedef struct MDB_cursor MDB_cursor;
117
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 */
122 } MDB_val;
123
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);
126
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.
133  */
134 typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size);
135
136 /** @defgroup   mdb_env Environment Flags
137  *      @{
138  */
139         /** mmap at a fixed address */
140 #define MDB_FIXEDMAP    0x01
141         /** don't fsync after commit */
142 #define MDB_NOSYNC              0x10000
143         /** read only */
144 #define MDB_RDONLY              0x20000
145 /** @} */
146
147 /**     @defgroup       mdb_open        Database Flags
148  *      @{
149  */
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          *      @note The keys size must actually be equal to
156          *      sizeof(int) or sizeof(long) otherwise there will be
157          *      alignment issues. On some processors, accessing misaligned
158          *      data will cause a SIGBUS.
159          */
160 #define MDB_INTEGERKEY  0x08
161         /** with #MDB_DUPSORT, sorted dup items have fixed size */
162 #define MDB_DUPFIXED    0x10
163         /** with #MDB_DUPSORT, dups are numeric in native byte order */
164 #define MDB_INTEGERDUP  0x20
165         /** with #MDB_DUPSORT, use reverse string dups */
166 #define MDB_REVERSEDUP  0x40
167         /** create DB if not already existing */
168 #define MDB_CREATE              0x40000
169 /** @} */
170
171 /**     @defgroup mdb_put       Write Flags
172  *      @{
173  */
174 /** For put: Don't write if the key already exists. */
175 #define MDB_NOOVERWRITE 0x10
176 /** Only for #MDB_DUPSORT<br>
177  * For put: don't write if the key and data pair already exist.<br>
178  * For mdb_cursor_del: remove all duplicate data items.
179  */
180 #define MDB_NODUPDATA   0x20
181 /** For mdb_cursor_put: overwrite the current key/data pair */
182 #define MDB_CURRENT     0x40
183 /*      @} */
184
185 /** Cursor operations */
186 typedef enum MDB_cursor_op {
187         MDB_FIRST,                              /**< Position at first key/data item */
188         MDB_FIRST_DUP,                  /**< Position at first data item of current key.
189                                                                 Only for #MDB_DUPSORT */
190         MDB_GET_BOTH,                   /**< Position at key/data pair. Only for #MDB_DUPSORT */
191         MDB_GET_BOTH_RANGE,             /**< position at key, nearest data. Only for #MDB_DUPSORT */
192         MDB_GET_MULTIPLE,               /**< Return all the duplicate data items at the current
193                                                                  cursor position. Only for #MDB_DUPFIXED */
194         MDB_LAST,                               /**< Position at last key/data item */
195         MDB_LAST_DUP,                   /**< Position at last data item of current key.
196                                                                 Only for #MDB_DUPSORT */
197         MDB_NEXT,                               /**< Position at next data item */
198         MDB_NEXT_DUP,                   /**< Position at next data item of current key.
199                                                                 Only for #MDB_DUPSORT */
200         MDB_NEXT_MULTIPLE,              /**< Return all duplicate data items at the next
201                                                                 cursor position. Only for #MDB_DUPFIXED */
202         MDB_NEXT_NODUP,                 /**< Position at first data item of next key.
203                                                                 Only for #MDB_DUPSORT */
204         MDB_PREV,                               /**< Position at previous data item */
205         MDB_PREV_DUP,                   /**< Position at previous data item of current key.
206                                                                 Only for #MDB_DUPSORT */
207         MDB_PREV_NODUP,                 /**< Position at last data item of previous key.
208                                                                 only for #MDB_DUPSORT */
209         MDB_SET,                                /**< Position at specified key */
210         MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
211 } MDB_cursor_op;
212
213 /** @defgroup  errors   Return Codes
214  *
215  *      BerkeleyDB uses -30800 to -30999, we'll go under them
216  *      @{
217  */
218         /**     Successful result */
219 #define MDB_SUCCESS      0
220         /** key/data pair already exists */
221 #define MDB_KEYEXIST    (-30799)
222         /** key/data pair not found (EOF) */
223 #define MDB_NOTFOUND    (-30798)
224         /** Requested page not found - this usually indicates corruption */
225 #define MDB_PAGE_NOTFOUND       (-30797)
226         /** Located page was wrong type */
227 #define MDB_CORRUPTED   (-30796)
228         /** Update of meta page failed, probably I/O error */
229 #define MDB_PANIC               (-30795)
230         /** Environment version mismatch */
231 #define MDB_VERSION_MISMATCH    (-30794)
232 /** @} */
233
234 /** Statistics for a database in the environment */
235 typedef struct MDB_stat {
236         unsigned int    ms_psize;                       /**< Size of a database page.
237                                                                                         This is currently the same for all databases. */
238         unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
239         unsigned long   ms_branch_pages;        /**< Number of internal (non-leaf) pages */
240         unsigned long   ms_leaf_pages;          /**< Number of leaf pages */
241         unsigned long   ms_overflow_pages;      /**< Number of overflow pages */
242         unsigned long   ms_entries;                     /**< Number of data items */
243 } MDB_stat;
244
245         /** Return the mdb library version information.
246          * @param[out] major if non-NULL, the library major version number is copied here
247          * @param[out] minor if non-NULL, the library minor version number is copied here
248          * @param[out] patch if non-NULL, the library patch version number is copied here
249          * @retval "version string" The library version as a string
250          */
251 char *mdb_version(int *major, int *minor, int *patch);
252
253         /** Return a string describing a given error code.
254          * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
255          * function. If the error code is greater than or equal to 0, then the string
256          * returned by the system function strerror(3) is returned. If the error code
257          * is less than 0, an error string corresponding to the MDB library error is
258          * returned. See @ref errors for a list of MDB-specific error codes.
259          * @param[in] err The error code
260          * @retval "error message" The description of the error
261          */
262 char *mdb_strerror(int err);
263
264         /** Create an MDB environment handle.
265          * This function allocates memory for a #MDB_env structure. To release
266          * the allocated memory and discard the handle, call #mdb_env_close().
267          * Before the handle may be used, it must be opened using #mdb_env_open().
268          * Various other options may also need to be set before opening the handle,
269          * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
270          * depending on usage requirements.
271          * @param[out] env The address where the new handle will be stored
272          * @return A non-zero error value on failure and 0 on success.
273          */
274 int  mdb_env_create(MDB_env **env);
275
276         /** Open an environment handle.
277          * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
278          * @param[in] env An environment handle returned by #mdb_env_create()
279          * @param[in] path The directory in which the database files reside. This
280          * directory must already exist and be writable.
281          * @param[in] flags Special options for this environment. This parameter
282          * must be set to 0 or by bitwise OR'ing together one or more of the
283          * values described here.
284          * <ul>
285          *      <li>#MDB_FIXEDMAP
286          *      use a fixed address for the mmap region. This flag must be specified
287          *      when creating the environment, and is stored persistently in the environment.
288          *              If successful, the memory map will always reside at the same virtual address
289          *              and pointers used to reference data items in the database will be constant
290          *              across multiple invocations. This option may not always work, depending on
291          *              how the operating system has allocated memory to shared libraries and other uses.
292          *              The feature is highly experimental.
293          *      <li>#MDB_NOSYNC
294          *              Don't perform a synchronous flush after committing a transaction. This means
295          *              transactions will exhibit the ACI (atomicity, consistency, and isolation)
296          *              properties, but not D (durability); that is database integrity will be
297          *              maintained but it is possible some number of the most recently committed
298          *              transactions may be undone after a system crash. The number of transactions
299          *              at risk is governed by how often the system flushes dirty buffers to disk
300          *              and how often #mdb_env_sync() is called. This flag may be changed
301          *              at any time using #mdb_env_set_flags().
302          *      <li>#MDB_RDONLY
303          *              Open the environment in read-only mode. No write operations will be allowed.
304          * </ul>
305          * @param[in] mode The UNIX permissions to set on created files. This parameter
306          * is ignored on Windows.
307          * @return A non-zero error value on failure and 0 on success. Some possible
308          * errors are:
309          * <ul>
310          *      <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
311          *      version that created the database environment.
312          *      <li>EINVAL - the environment file headers are corrupted.
313          *      <li>ENOENT - the directory specified by the path parameter doesn't exist.
314          *      <li>EACCES - the user didn't have permission to access the environment files.
315          *      <li>EAGAIN - the environment was locked by another process.
316          * </ul>
317          */
318 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
319
320         /** Return statistics about the MDB environment.
321          * @param[in] env An environment handle returned by #mdb_env_create()
322          * @param[out] stat The address of an #MDB_stat structure
323          *      where the statistics will be copied
324          */
325 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
326
327         /** Flush the data buffers to disk.
328          * Data is always written to disk when #mdb_txn_commit() is called,
329          * but the operating system may keep it buffered. MDB always flushes
330          * the OS buffers upon commit as well, unless the environment was
331          * opened with #MDB_NOSYNC.
332          * @param[in] env An environment handle returned by #mdb_env_create()
333          * @param[in] force If non-zero, force the flush to occur. Otherwise
334          *  if the environment has the #MDB_NOSYNC flag set the flushes
335          *      will be omitted.
336          * @return A non-zero error value on failure and 0 on success. Some possible
337          * errors are:
338          * <ul>
339          *      <li>EINVAL - an invalid parameter was specified.
340          *      <li>EIO - an error occurred during synchronization.
341          * </ul>
342          */
343 int  mdb_env_sync(MDB_env *env, int force);
344
345         /** Close the environment and release the memory map.
346          * Only a single thread may call this function. All transactions, databases,
347          * and cursors must already be closed before calling this function. Attempts to
348          * use any such handles after calling this function will cause a SIGSEGV.
349          * The environment handle will be freed and must not be used again after this call.
350          * @param[in] env An environment handle returned by #mdb_env_create()
351          */
352 void mdb_env_close(MDB_env *env);
353
354         /** Set environment flags.
355          * This may be used to set some flags that weren't already set during
356          * #mdb_env_open(), or to unset these flags. Currently only the
357          * #MDB_NOSYNC flag setting may be changed with this function.
358          * @param[in] env An environment handle returned by #mdb_env_create()
359          * @param[in] flags The flags to change, bitwise OR'ed together
360          * @param[in] onoff A non-zero value sets the flags, zero clears them.
361          * @return A non-zero error value on failure and 0 on success. Some possible
362          * errors are:
363          * <ul>
364          *      <li>EINVAL - an invalid parameter was specified.
365          * </ul>
366          */
367 int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
368
369         /** Get environment flags.
370          * @param[in] env An environment handle returned by #mdb_env_create()
371          * @param[out] flags The address of an integer to store the flags
372          * @return A non-zero error value on failure and 0 on success. Some possible
373          * errors are:
374          * <ul>
375          *      <li>EINVAL - an invalid parameter was specified.
376          * </ul>
377          */
378 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
379
380         /** Return the path that was used in #mdb_env_open().
381          * @param[in] env An environment handle returned by #mdb_env_create()
382          * @param[out] path Address of a string pointer to contain the path. This
383          * is the actual string in the environment, not a copy. It should not be
384          * altered in any way.
385          * @return A non-zero error value on failure and 0 on success. Some possible
386          * errors are:
387          * <ul>
388          *      <li>EINVAL - an invalid parameter was specified.
389          * </ul>
390          */
391 int  mdb_env_get_path(MDB_env *env, const char **path);
392
393         /** Set the size of the memory map to use for this environment.
394          * The size should be a multiple of the OS page size. The default is
395          * 10485760 bytes. The size of the memory map is also the maximum size
396          * of the database. The value should be chosen as large as possible,
397          * to accomodate future growth of the database.
398          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
399          * @param[in] env An environment handle returned by #mdb_env_create()
400          * @param[in] size The size in bytes
401          * @return A non-zero error value on failure and 0 on success. Some possible
402          * errors are:
403          * <ul>
404          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
405          * </ul>
406          */
407 int  mdb_env_set_mapsize(MDB_env *env, size_t size);
408
409         /** Set the maximum number of threads for the environment.
410          * This defines the number of slots in the lock table that is used to track readers in the
411          * the environment. The default is 126.
412          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
413          * @param[in] env An environment handle returned by #mdb_env_create()
414          * @param[in] readers The maximum number of threads
415          * @return A non-zero error value on failure and 0 on success. Some possible
416          * errors are:
417          * <ul>
418          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
419          * </ul>
420          */
421 int  mdb_env_set_maxreaders(MDB_env *env, int readers);
422
423         /** Get the maximum number of threads for the environment.
424          * @param[in] env An environment handle returned by #mdb_env_create()
425          * @param[out] readers Address of an integer to store the number of readers
426          * @return A non-zero error value on failure and 0 on success. Some possible
427          * errors are:
428          * <ul>
429          *      <li>EINVAL - an invalid parameter was specified.
430          * </ul>
431          */
432 int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
433
434         /** Set the maximum number of databases for the environment.
435          * This function is only needed if multiple databases will be used in the
436          * environment. Simpler applications that only use a single database can ignore
437          * this option.
438          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
439          * @param[in] env An environment handle returned by #mdb_env_create()
440          * @param[in] dbs The maximum number of databases
441          * @return A non-zero error value on failure and 0 on success. Some possible
442          * errors are:
443          * <ul>
444          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
445          * </ul>
446          */
447 int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
448
449         /** Create a transaction for use with the environment.
450          * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
451          * @note Transactions may not span threads, a transaction must only be used by a
452          * single thread.
453          * @note Cursors may not span transactions; each cursor must be opened and closed
454          * within a single transaction.
455          * @param[in] env An environment handle returned by #mdb_env_create()
456          * @param[in] flags Special options for this transaction. This parameter
457          * must be set to 0 or by bitwise OR'ing together one or more of the
458          * values described here.
459          * <ul>
460          *      <li>#MDB_RDONLY
461          *              This transaction will not perform any write operations.
462          * </ul>
463          * @param[out] txn Address where the new #MDB_txn handle will be stored
464          * @return A non-zero error value on failure and 0 on success. Some possible
465          * errors are:
466          * <ul>
467          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
468          *              must be shut down.
469          *      <li>ENOMEM - out of memory, or a read-only transaction was requested and
470          *              the reader lock table is full. See #mdb_env_set_maxreaders().
471          * </ul>
472          */
473 int  mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn);
474
475         /** Commit all the operations of a transaction into the database.
476          * All cursors opened within the transaction must be closed before the transaction
477          * is committed.
478          * The transaction handle will be freed and must not be used again after this call.
479          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
480          * @return A non-zero error value on failure and 0 on success. Some possible
481          * errors are:
482          * <ul>
483          *      <li>EINVAL - an invalid parameter was specified.
484          *      <li>ENOSPC - no more disk space.
485          *      <li>EIO - a low-level I/O error occurred while writing.
486          * </ul>
487          */
488 int  mdb_txn_commit(MDB_txn *txn);
489
490         /** Abandon all the operations of the transaction instead of saving them.
491          * All cursors opened within the transaction must be closed before the transaction
492          * is aborted.
493          * The transaction handle will be freed and must not be used again after this call.
494          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
495          */
496 void mdb_txn_abort(MDB_txn *txn);
497
498         /** Reset a read-only transaction.
499          * This releases the current reader lock but doesn't free the
500          * transaction handle, allowing it to be used again later by #mdb_txn_renew().
501          * It otherwise has the same affect as #mdb_txn_abort() but saves some memory
502          * allocation/deallocation overhead if a thread is going to start a new
503          * read-only transaction again soon.
504          * All cursors opened within the transaction must be closed before the transaction
505          * is reset.
506          * Reader locks generally don't interfere with writers, but they keep old
507          * versions of database pages allocated. Thus they prevent the old pages
508          * from being reused when writers commit new data, and so under heavy load
509          * the database size may grow much more rapidly than otherwise.
510          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
511          */
512 void mdb_txn_reset(MDB_txn *txn);
513
514         /** Renew a read-only transaction.
515          * This acquires a new reader lock for a transaction handle that had been
516          * released by #mdb_txn_reset(). It must be called before a reset transaction
517          * may be used again.
518          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
519          * @return A non-zero error value on failure and 0 on success. Some possible
520          * errors are:
521          * <ul>
522          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
523          *              must be shut down.
524          *      <li>EINVAL - an invalid parameter was specified.
525          * </ul>
526          */
527 int  mdb_txn_renew(MDB_txn *txn);
528
529         /** Open a database in the environment.
530          * The database handle may be discarded by calling #mdb_close().
531          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
532          * @param[in] name The name of the database to open. If only a single
533          *      database is needed in the enviroment, this value may be NULL.
534          * @param[in] flags Special options for this database. This parameter
535          * must be set to 0 or by bitwise OR'ing together one or more of the
536          * values described here.
537          * <ul>
538          *      <li>#MDB_REVERSEKEY
539          *              Keys are strings to be compared in reverse order, from the end
540          *              of the strings to the beginning. By default, Keys are treated as strings and
541          *              compared from beginning to end.
542          *      <li>#MDB_DUPSORT
543          *              Duplicate keys may be used in the database. (Or, from another perspective,
544          *              keys may have multiple data items, stored in sorted order.) By default
545          *              keys must be unique and may have only a single data item.
546          *      <li>#MDB_INTEGERKEY
547          *              Keys are binary integers in native byte order. On Big-Endian systems
548          *              this flag has no effect. On Little-Endian systems this flag behaves
549          *              the same as #MDB_REVERSEKEY. This flag is simply provided as a
550          *              convenience so that applications don't need to detect Endianness themselves
551          *              when using integers as keys.
552          *      <li>#MDB_DUPFIXED
553          *              This flag may only be used in combination with #MDB_DUPSORT. This option
554          *              tells the library that the data items for this database are all the same
555          *              size, which allows further optimizations in storage and retrieval. When
556          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
557          *              cursor operations may be used to retrieve multiple items at once.
558          *      <li>#MDB_INTEGERDUP
559          *              This option specifies that duplicate data items are also integers, and
560          *              should be sorted as such.
561          *      <li>#MDB_CREATE
562          *              Create the named database if it doesn't exist. This option is not
563          *              allowed in a read-only transaction or a read-only environment.
564          * </ul>
565          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
566          * @return A non-zero error value on failure and 0 on success. Some possible
567          * errors are:
568          * <ul>
569          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
570          *              and #MDB_CREATE was not specified.
571          *      <li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
572          * </ul>
573          */
574 int  mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
575
576         /** Retrieve statistics for a database.
577          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
578          * @param[in] dbi A database handle returned by #mdb_open()
579          * @param[out] stat The address of an #MDB_stat structure
580          *      where the statistics will be copied
581          * @return A non-zero error value on failure and 0 on success. Some possible
582          * errors are:
583          * <ul>
584          *      <li>EINVAL - an invalid parameter was specified.
585          * </ul>
586          */
587 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
588
589         /** Close a database handle.
590          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
591          * @param[in] dbi A database handle returned by #mdb_open()
592          */
593 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
594
595         /** Set a custom key comparison function for a database.
596          * The comparison function is called whenever it is necessary to compare a
597          * key specified by the application with a key currently stored in the database.
598          * If no comparison function is specified, and no special key flags were specified
599          * with #mdb_open(), the keys are compared lexically, with shorter keys collating
600          * before longer keys.
601          * @warning This function must be called before any data access functions are used,
602          * otherwise data corruption may occur. The same function must be used by every
603          * process accessing the database, every time the database is used.
604          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
605          * @param[in] dbi A database handle returned by #mdb_open()
606          * @param[in] cmp A #MDB_cmp_func function
607          * @return A non-zero error value on failure and 0 on success. Some possible
608          * errors are:
609          * <ul>
610          *      <li>EINVAL - an invalid parameter was specified.
611          * </ul>
612          */
613 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
614
615         /** Set a custom data comparison function for a #MDB_DUPSORT database.
616          * This comparison function is called whenever it is necessary to compare a data
617          * item specified by the application with a data item currently stored in the database.
618          * This function only takes effect if the database was opened with the #MDB_DUPSORT
619          * flag.
620          * If no comparison function is specified, and no special key flags were specified
621          * with #mdb_open(), the data items are compared lexically, with shorter items collating
622          * before longer items.
623          * @warning This function must be called before any data access functions are used,
624          * otherwise data corruption may occur. The same function must be used by every
625          * process accessing the database, every time the database is used.
626          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
627          * @param[in] dbi A database handle returned by #mdb_open()
628          * @param[in] cmp A #MDB_cmp_func function
629          * @return A non-zero error value on failure and 0 on success. Some possible
630          * errors are:
631          * <ul>
632          *      <li>EINVAL - an invalid parameter was specified.
633          * </ul>
634          */
635 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
636
637         /** Set a relocation function for a #MDB_FIXEDMAP database.
638          * @todo The relocation function is called whenever it is necessary to move the data
639          * of an item to a different position in the database (e.g. through tree
640          * balancing operations, shifts as a result of adds or deletes, etc.). It is
641          * intended to allow address/position-dependent data items to be stored in
642          * a database in an environment opened with the #MDB_FIXEDMAP option.
643          * Currently the relocation feature is unimplemented and setting
644          * this function has no effect.
645          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
646          * @param[in] dbi A database handle returned by #mdb_open()
647          * @param[in] rel A #MDB_rel_func function
648          * @return A non-zero error value on failure and 0 on success. Some possible
649          * errors are:
650          * <ul>
651          *      <li>EINVAL - an invalid parameter was specified.
652          * </ul>
653          */
654 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
655
656         /** Get items from a database.
657          * This function retrieves key/data pairs from the database. The address
658          * and length of the data associated with the specified \b key are returned
659          * in the structure to which \b data refers.
660          * If the database supports duplicate keys (#MDB_DUPSORT) then the
661          * first data item for the key will be returned. Retrieval of other
662          * items requires the use of #mdb_cursor_get().
663          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
664          * @param[in] dbi A database handle returned by #mdb_open()
665          * @param[in] key The key to search for in the database
666          * @param[out] data The data corresponding to the key
667          * @return A non-zero error value on failure and 0 on success. Some possible
668          * errors are:
669          * <ul>
670          *      <li>#MDB_NOTFOUND - the key was not in the database.
671          *      <li>EINVAL - an invalid parameter was specified.
672          * </ul>
673          */
674 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
675
676         /** Store items into a database.
677          * This function stores key/data pairs in the database. The default behavior
678          * is to enter the new key/data pair, replacing any previously existing key
679          * if duplicates are disallowed, or adding a duplicate data item if
680          * duplicates are allowed (#MDB_DUPSORT).
681          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
682          * @param[in] dbi A database handle returned by #mdb_open()
683          * @param[in] key The key to store in the database
684          * @param[in] data The data to store
685          * @param[in] flags Special options for this operation. This parameter
686          * must be set to 0 or by bitwise OR'ing together one or more of the
687          * values described here.
688          * <ul>
689          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
690          *              already appear in the database. This flag may only be specified
691          *              if the database was opened with #MDB_DUPSORT. The function will
692          *              return #MDB_KEYEXIST if the key/data pair already appears in the
693          *              database.
694          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
695          *              does not already appear in the database. The function will return
696          *              #MDB_KEYEXIST if the key already appears in the database, even if
697          *              the database supports duplicates (#MDB_DUPSORT).
698          * </ul>
699          * @return A non-zero error value on failure and 0 on success. Some possible
700          * errors are:
701          * <ul>
702          *      <li>EACCESS - an attempt was made to write in a read-only transaction.
703          *      <li>EINVAL - an invalid parameter was specified.
704          *      <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
705          * </ul>
706          */
707 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
708                             unsigned int flags);
709
710         /** Delete items from a database.
711          * This function removes key/data pairs from the database.
712          * If the database does not support sorted duplicate data items
713          * (#MDB_DUPSORT) the data parameter is ignored.
714          * If the database supports sorted duplicates and the data parameter
715          * is NULL, all of the duplicate data items for the key will be
716          * deleted. Otherwise, if the data parameter is non-NULL
717          * only the matching data item will be deleted.
718          * This function will return #MDB_NOTFOUND if the specified key/data
719          * pair is not in the database.
720          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
721          * @param[in] dbi A database handle returned by #mdb_open()
722          * @param[in] key The key to delete from the database
723          * @param[in] data The data to delete
724          * @return A non-zero error value on failure and 0 on success. Some possible
725          * errors are:
726          * <ul>
727          *      <li>EACCESS - an attempt was made to write in a read-only transaction.
728          *      <li>EINVAL - an invalid parameter was specified.
729          * </ul>
730          */
731 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
732
733         /** Create a cursor handle.
734          * Cursors are associated with a specific transaction and database and
735          * may not span threads.
736          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
737          * @param[in] dbi A database handle returned by #mdb_open()
738          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
739          * @return A non-zero error value on failure and 0 on success. Some possible
740          * errors are:
741          * <ul>
742          *      <li>EINVAL - an invalid parameter was specified.
743          * </ul>
744          */
745 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
746
747         /** Close a cursor handle.
748          * The cursor handle will be freed and must not be used again after this call.
749          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
750          */
751 void mdb_cursor_close(MDB_cursor *cursor);
752
753         /** Retrieve by cursor.
754          * This function retrieves key/data pairs from the database. The address and length
755          * of the key are returned in the object to which \b key refers (except for the
756          * case of the #MDB_SET option, in which the \b key object is unchanged), and
757          * the address and length of the data are returned in the object to which \b data
758          * refers.
759          * @bug Cursors are not coordinated with write operations. If a cursor in a
760          * write transaction is performing a sequential scan while records are being
761          * inserted or deleted in the same transaction, the cursor will be corrupted.
762          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
763          * @param[in,out] key The key for a retrieved item
764          * @param[in,out] data The data of a retrieved item
765          * @param[in] op A cursor operation #MDB_cursor_op
766          * @return A non-zero error value on failure and 0 on success. Some possible
767          * errors are:
768          * <ul>
769          *      <li>#MDB_NOTFOUND - no matching key found.
770          *      <li>EINVAL - an invalid parameter was specified.
771          * </ul>
772          */
773 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
774                             MDB_cursor_op op);
775
776         /** Store by cursor.
777          * This function stores key/data pairs into the database.
778          * If the function fails for any reason, the state of the cursor will be
779          * unchanged. If the function succeeds and an item is inserted into the
780          * database, the cursor is always positioned to refer to the newly inserted item.
781          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
782          * @param[in] key The key operated on.
783          * @param[in] data The data operated on.
784          * @param[in] flags Options for this operation. This parameter
785          * must be set to 0 or one of the values described here.
786          * <ul>
787          *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
788          *              the cursor refers with the specified data item. The \b key
789          *              parameter is ignored.
790          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
791          *              already appear in the database. This flag may only be specified
792          *              if the database was opened with #MDB_DUPSORT. The function will
793          *              return #MDB_KEYEXIST if the key/data pair already appears in the
794          *              database.
795          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
796          *              does not already appear in the database. The function will return
797          *              #MDB_KEYEXIST if the key already appears in the database, even if
798          *              the database supports duplicates (#MDB_DUPSORT).
799          * </ul>
800          * @return A non-zero error value on failure and 0 on success. Some possible
801          * errors are:
802          * <ul>
803          *      <li>EACCES - an attempt was made to modify a read-only database.
804          *      <li>EINVAL - an invalid parameter was specified.
805          * </ul>
806          */
807 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
808                                 unsigned int flags);
809
810         /**  Delete current key/data pair
811          * This function deletes the key/data pair to which the cursor refers.
812          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
813          * @param[in] flags Options for this operation. This parameter
814          * must be set to 0 or one of the values described here.
815          * <ul>
816          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
817          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
818          * </ul>
819          * @return A non-zero error value on failure and 0 on success. Some possible
820          * errors are:
821          * <ul>
822          *      <li>EACCES - an attempt was made to modify a read-only database.
823          *      <li>EINVAL - an invalid parameter was specified.
824          * </ul>
825          */
826 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
827
828         /** Return count of duplicates for current key.
829          * This call is only valid on databases that support sorted duplicate
830          * data items #MDB_DUPSORT.
831          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
832          * @param[out] countp Address where the count will be stored
833          * @return A non-zero error value on failure and 0 on success. Some possible
834          * errors are:
835          * <ul>
836          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
837          * </ul>
838          */
839 int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
840
841         /** Compare two data items according to a particular database.
842          * This returns a comparison as if the two data items were keys in the
843          * specified database.
844          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
845          * @param[in] dbi A database handle returned by #mdb_open()
846          * @param[in] a The first item to compare
847          * @param[in] b The second item to compare
848          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
849          */
850 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
851
852 /**     @} */
853 #endif /* _MDB_H_ */