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