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