]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/lmdb.h
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / libraries / liblmdb / lmdb.h
1 /** @file lmdb.h
2  *      @brief Lightning memory-mapped database library
3  *
4  *      @mainpage       Lightning Memory-Mapped Database Manager (MDB)
5  *
6  *      @section intro_sec Introduction
7  *      MDB is a Btree-based database management library modeled loosely on the
8  *      BerkeleyDB API, but much simplified. The entire database is exposed
9  *      in a memory map, and all data fetches return data directly
10  *      from the mapped memory, so no malloc's or memcpy's occur during
11  *      data fetches. As such, the library is extremely simple because it
12  *      requires no page caching layer of its own, and it is extremely high
13  *      performance and memory-efficient. It is also fully transactional with
14  *      full ACID semantics, and when the memory map is read-only, the
15  *      database integrity cannot be corrupted by stray pointer writes from
16  *      application code.
17  *
18  *      The library is fully thread-aware and supports concurrent read/write
19  *      access from multiple processes and threads. Data pages use a copy-on-
20  *      write strategy so no active data pages are ever overwritten, which
21  *      also provides resistance to corruption and eliminates the need of any
22  *      special recovery procedures after a system crash. Writes are fully
23  *      serialized; only one write transaction may be active at a time, which
24  *      guarantees that writers can never deadlock. The database structure is
25  *      multi-versioned so readers run with no locks; writers cannot block
26  *      readers, and readers don't block writers.
27  *
28  *      Unlike other well-known database mechanisms which use either write-ahead
29  *      transaction logs or append-only data writes, MDB requires no maintenance
30  *      during operation. Both write-ahead loggers and append-only databases
31  *      require periodic checkpointing and/or compaction of their log or database
32  *      files otherwise they grow without bound. MDB tracks free pages within
33  *      the database and re-uses them for new write operations, so the database
34  *      size does not grow without bound in normal use.
35  *
36  *      The memory map can be used as a read-only or read-write map. It is
37  *      read-only by default as this provides total immunity to corruption.
38  *      Using read-write mode offers much higher write performance, but adds
39  *      the possibility for stray application writes thru pointers to silently
40  *      corrupt the database. Of course if your application code is known to
41  *      be bug-free (...) then this is not an issue.
42  *
43  *      @section caveats_sec Caveats
44  *      Troubleshooting the lock file, plus semaphores on BSD systems:
45  *
46  *      - A broken lockfile can cause sync issues.
47  *        Stale reader transactions left behind by an aborted program
48  *        cause further writes to grow the database quickly, and
49  *        stale locks can block further operation.
50  *
51  *        Fix: Terminate all programs using the database, or make
52  *        them close it.  Next database user will reset the lockfile.
53  *
54  *      - On BSD systems or others configured with MDB_USE_POSIX_SEM,
55  *        startup can fail due to semaphores owned by another userid.
56  *
57  *        Fix: Open and close the database as the user which owns the
58  *        semaphores (likely last user) or as root, while no other
59  *        process is using the database.
60  *
61  *      Restrictions/caveats (in addition to those listed for some functions):
62  *
63  *      - Only the database owner should normally use the database on
64  *        BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
65  *        Multiple users can cause startup to fail later, as noted above.
66  *
67  *      - A thread can only use one transaction at a time, plus any child
68  *        transactions.  Each transaction belongs to one thread.  See below.
69  *
70  *      - Use an MDB_env* in the process which opened it, without fork()ing.
71  *
72  *      - Do not have open an MDB database twice in the same process at
73  *        the same time.  Not even from a plain open() call - close()ing it
74  *        breaks flock() advisory locking.
75  *
76  *      - Avoid long-lived transactions.  Read transactions prevent
77  *        reuse of pages freed by newer write transactions, thus the
78  *        database can grow quickly.  Write transactions prevent
79  *        other write transactions, since writes are serialized.
80  *
81  *      - Avoid suspending a process with active transactions.  These
82  *        would then be "long-lived" as above.  Also read transactions
83  *        suspended when writers commit could sometimes see wrong data.
84  *
85  *      ...when several processes can use a database concurrently:
86  *
87  *      - Avoid aborting a process with an active transaction.
88  *        The transaction becomes "long-lived" as above until the lockfile
89  *        is reset, since the process may not remove it from the lockfile.
90  *
91  *      - If you do that anyway, close the environment once in a while,
92  *        so the lockfile can get reset.
93  *
94  *      - Do not use MDB databases on remote filesystems, even between
95  *        processes on the same host.  This breaks flock() on some OSes,
96  *        possibly memory map sync, and certainly sync between programs
97  *        on different hosts.
98  *
99  *      - Opening a database can fail if another process is opening or
100  *        closing it at exactly the same time.
101  *
102  *      @author Howard Chu, Symas Corporation.
103  *
104  *      @copyright Copyright 2011-2013 Howard Chu, Symas Corp. All rights reserved.
105  *
106  * Redistribution and use in source and binary forms, with or without
107  * modification, are permitted only as authorized by the OpenLDAP
108  * Public License.
109  *
110  * A copy of this license is available in the file LICENSE in the
111  * top-level directory of the distribution or, alternatively, at
112  * <http://www.OpenLDAP.org/license.html>.
113  *
114  *      @par Derived From:
115  * This code is derived from btree.c written by Martin Hedenfalk.
116  *
117  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
118  *
119  * Permission to use, copy, modify, and distribute this software for any
120  * purpose with or without fee is hereby granted, provided that the above
121  * copyright notice and this permission notice appear in all copies.
122  *
123  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
124  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
125  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
126  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
127  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
128  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
129  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
130  */
131 #ifndef _LMDB_H_
132 #define _LMDB_H_
133
134 #include <sys/types.h>
135
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139
140 /** @defgroup mdb MDB API
141  *      @{
142  *      @brief OpenLDAP Lightning Memory-Mapped Database Manager
143  */
144 /** @defgroup Version Version Macros
145  *      @{
146  */
147 /** Library major version */
148 #define MDB_VERSION_MAJOR       0
149 /** Library minor version */
150 #define MDB_VERSION_MINOR       9
151 /** Library patch version */
152 #define MDB_VERSION_PATCH       6
153
154 /** Combine args a,b,c into a single integer for easy version comparisons */
155 #define MDB_VERINT(a,b,c)       (((a) << 24) | ((b) << 16) | (c))
156
157 /** The full library version as a single integer */
158 #define MDB_VERSION_FULL        \
159         MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
160
161 /** The release date of this library version */
162 #define MDB_VERSION_DATE        "January 10, 2013"
163
164 /** A stringifier for the version info */
165 #define MDB_VERSTR(a,b,c,d)     "MDB " #a "." #b "." #c ": (" d ")"
166
167 /** A helper for the stringifier macro */
168 #define MDB_VERFOO(a,b,c,d)     MDB_VERSTR(a,b,c,d)
169
170 /** The full library version as a C string */
171 #define MDB_VERSION_STRING      \
172         MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
173 /**     @} */
174
175 /** @brief Opaque structure for a database environment.
176  *
177  * A DB environment supports multiple databases, all residing in the same
178  * shared-memory map.
179  */
180 typedef struct MDB_env MDB_env;
181
182 /** @brief Opaque structure for a transaction handle.
183  *
184  * All database operations require a transaction handle. Transactions may be
185  * read-only or read-write.
186  */
187 typedef struct MDB_txn MDB_txn;
188
189 /** @brief A handle for an individual database in the DB environment. */
190 typedef unsigned int    MDB_dbi;
191
192 /** @brief Opaque structure for navigating through a database */
193 typedef struct MDB_cursor MDB_cursor;
194
195 /** @brief Generic structure used for passing keys and data in and out
196  * of the database.
197  *
198  * Key sizes must be between 1 and the liblmdb build-time constant
199  * #MDB_MAXKEYSIZE inclusive. This currently defaults to 511. The
200  * same applies to data sizes in databases with the #MDB_DUPSORT flag.
201  * Other data items can in theory be from 0 to 0xffffffff bytes long.
202  */
203 typedef struct MDB_val {
204         size_t           mv_size;       /**< size of the data item */
205         void            *mv_data;       /**< address of the data item */
206 } MDB_val;
207
208 /** @brief A callback function used to compare two keys in a database */
209 typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
210
211 /** @brief A callback function used to relocate a position-dependent data item
212  * in a fixed-address database.
213  *
214  * The \b newptr gives the item's desired address in
215  * the memory map, and \b oldptr gives its previous address. The item's actual
216  * data resides at the address in \b item.  This callback is expected to walk
217  * through the fields of the record in \b item and modify any
218  * values based at the \b oldptr address to be relative to the \b newptr address.
219  * @param[in,out] item The item that is to be relocated.
220  * @param[in] oldptr The previous address.
221  * @param[in] newptr The new address to relocate to.
222  * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
223  * @todo This feature is currently unimplemented.
224  */
225 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
226
227 /** @defgroup   mdb_env Environment Flags
228  *
229  *      Values do not overlap Database Flags.
230  *      @{
231  */
232         /** mmap at a fixed address (experimental) */
233 #define MDB_FIXEDMAP    0x01
234         /** no environment directory */
235 #define MDB_NOSUBDIR    0x4000
236         /** don't fsync after commit */
237 #define MDB_NOSYNC              0x10000
238         /** read only */
239 #define MDB_RDONLY              0x20000
240         /** don't fsync metapage after commit */
241 #define MDB_NOMETASYNC          0x40000
242         /** use writable mmap */
243 #define MDB_WRITEMAP            0x80000
244         /** use asynchronous msync when MDB_WRITEMAP is used */
245 #define MDB_MAPASYNC            0x100000
246 /** @} */
247
248 /**     @defgroup       mdb_dbi_open    Database Flags
249  *
250  *      Values do not overlap Environment Flags.
251  *      @{
252  */
253         /** use reverse string keys */
254 #define MDB_REVERSEKEY  0x02
255         /** use sorted duplicates */
256 #define MDB_DUPSORT             0x04
257         /** numeric keys in native byte order.
258          *  The keys must all be of the same size. */
259 #define MDB_INTEGERKEY  0x08
260         /** with #MDB_DUPSORT, sorted dup items have fixed size */
261 #define MDB_DUPFIXED    0x10
262         /** with #MDB_DUPSORT, dups are numeric in native byte order */
263 #define MDB_INTEGERDUP  0x20
264         /** with #MDB_DUPSORT, use reverse string dups */
265 #define MDB_REVERSEDUP  0x40
266         /** create DB if not already existing */
267 #define MDB_CREATE              0x40000
268 /** @} */
269
270 /**     @defgroup mdb_put       Write Flags
271  *      @{
272  */
273 /** For put: Don't write if the key already exists. */
274 #define MDB_NOOVERWRITE 0x10
275 /** Only for #MDB_DUPSORT<br>
276  * For put: don't write if the key and data pair already exist.<br>
277  * For mdb_cursor_del: remove all duplicate data items.
278  */
279 #define MDB_NODUPDATA   0x20
280 /** For mdb_cursor_put: overwrite the current key/data pair */
281 #define MDB_CURRENT     0x40
282 /** For put: Just reserve space for data, don't copy it. Return a
283  * pointer to the reserved space.
284  */
285 #define MDB_RESERVE     0x10000
286 /** Data is being appended, don't split full pages. */
287 #define MDB_APPEND      0x20000
288 /** Duplicate data is being appended, don't split full pages. */
289 #define MDB_APPENDDUP   0x40000
290 /** Store multiple data items in one call. */
291 #define MDB_MULTIPLE    0x80000
292 /*      @} */
293
294 /** @brief Cursor Get operations.
295  *
296  *      This is the set of all operations for retrieving data
297  *      using a cursor.
298  */
299 typedef enum MDB_cursor_op {
300         MDB_FIRST,                              /**< Position at first key/data item */
301         MDB_FIRST_DUP,                  /**< Position at first data item of current key.
302                                                                 Only for #MDB_DUPSORT */
303         MDB_GET_BOTH,                   /**< Position at key/data pair. Only for #MDB_DUPSORT */
304         MDB_GET_BOTH_RANGE,             /**< position at key, nearest data. Only for #MDB_DUPSORT */
305         MDB_GET_CURRENT,                /**< Return key/data at current cursor position */
306         MDB_GET_MULTIPLE,               /**< Return all the duplicate data items at the current
307                                                                  cursor position. Only for #MDB_DUPFIXED */
308         MDB_LAST,                               /**< Position at last key/data item */
309         MDB_LAST_DUP,                   /**< Position at last data item of current key.
310                                                                 Only for #MDB_DUPSORT */
311         MDB_NEXT,                               /**< Position at next data item */
312         MDB_NEXT_DUP,                   /**< Position at next data item of current key.
313                                                                 Only for #MDB_DUPSORT */
314         MDB_NEXT_MULTIPLE,              /**< Return all duplicate data items at the next
315                                                                 cursor position. Only for #MDB_DUPFIXED */
316         MDB_NEXT_NODUP,                 /**< Position at first data item of next key.
317                                                                 Only for #MDB_DUPSORT */
318         MDB_PREV,                               /**< Position at previous data item */
319         MDB_PREV_DUP,                   /**< Position at previous data item of current key.
320                                                                 Only for #MDB_DUPSORT */
321         MDB_PREV_NODUP,                 /**< Position at last data item of previous key.
322                                                                 Only for #MDB_DUPSORT */
323         MDB_SET,                                /**< Position at specified key */
324         MDB_SET_KEY,                    /**< Position at specified key, return key + data */
325         MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
326 } MDB_cursor_op;
327
328 /** @defgroup  errors   Return Codes
329  *
330  *      BerkeleyDB uses -30800 to -30999, we'll go under them
331  *      @{
332  */
333         /**     Successful result */
334 #define MDB_SUCCESS      0
335         /** key/data pair already exists */
336 #define MDB_KEYEXIST    (-30799)
337         /** key/data pair not found (EOF) */
338 #define MDB_NOTFOUND    (-30798)
339         /** Requested page not found - this usually indicates corruption */
340 #define MDB_PAGE_NOTFOUND       (-30797)
341         /** Located page was wrong type */
342 #define MDB_CORRUPTED   (-30796)
343         /** Update of meta page failed, probably I/O error */
344 #define MDB_PANIC               (-30795)
345         /** Environment version mismatch */
346 #define MDB_VERSION_MISMATCH    (-30794)
347         /** File is not a valid MDB file */
348 #define MDB_INVALID     (-30793)
349         /** Environment mapsize reached */
350 #define MDB_MAP_FULL    (-30792)
351         /** Environment maxdbs reached */
352 #define MDB_DBS_FULL    (-30791)
353         /** Environment maxreaders reached */
354 #define MDB_READERS_FULL        (-30790)
355         /** Too many TLS keys in use - Windows only */
356 #define MDB_TLS_FULL    (-30789)
357         /** Txn has too many dirty pages */
358 #define MDB_TXN_FULL    (-30788)
359         /** Cursor stack too deep - internal error */
360 #define MDB_CURSOR_FULL (-30787)
361         /** Page has not enough space - internal error */
362 #define MDB_PAGE_FULL   (-30786)
363         /** Database contents grew beyond environment mapsize */
364 #define MDB_MAP_RESIZED (-30785)
365         /** Database flags changed or would change */
366 #define MDB_INCOMPATIBLE        (-30784)
367 #define MDB_LAST_ERRCODE        MDB_INCOMPATIBLE
368 /** @} */
369
370 /** @brief Statistics for a database in the environment */
371 typedef struct MDB_stat {
372         unsigned int    ms_psize;                       /**< Size of a database page.
373                                                                                         This is currently the same for all databases. */
374         unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
375         size_t          ms_branch_pages;        /**< Number of internal (non-leaf) pages */
376         size_t          ms_leaf_pages;          /**< Number of leaf pages */
377         size_t          ms_overflow_pages;      /**< Number of overflow pages */
378         size_t          ms_entries;                     /**< Number of data items */
379 } MDB_stat;
380
381 /** @brief Information about the environment */
382 typedef struct MDB_envinfo {
383         void    *me_mapaddr;                    /**< Address of map, if fixed */
384         size_t  me_mapsize;                             /**< Size of the data memory map */
385         size_t  me_last_pgno;                   /**< ID of the last used page */
386         size_t  me_last_txnid;                  /**< ID of the last committed transaction */
387         unsigned int me_maxreaders;             /**< maximum number of threads for the environment */
388         unsigned int me_numreaders;             /**< maximum number of threads used in the environment */
389 } MDB_envinfo;
390
391         /** @brief Return the mdb library version information.
392          *
393          * @param[out] major if non-NULL, the library major version number is copied here
394          * @param[out] minor if non-NULL, the library minor version number is copied here
395          * @param[out] patch if non-NULL, the library patch version number is copied here
396          * @retval "version string" The library version as a string
397          */
398 char *mdb_version(int *major, int *minor, int *patch);
399
400         /** @brief Return a string describing a given error code.
401          *
402          * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
403          * function. If the error code is greater than or equal to 0, then the string
404          * returned by the system function strerror(3) is returned. If the error code
405          * is less than 0, an error string corresponding to the MDB library error is
406          * returned. See @ref errors for a list of MDB-specific error codes.
407          * @param[in] err The error code
408          * @retval "error message" The description of the error
409          */
410 char *mdb_strerror(int err);
411
412         /** @brief Create an MDB environment handle.
413          *
414          * This function allocates memory for a #MDB_env structure. To release
415          * the allocated memory and discard the handle, call #mdb_env_close().
416          * Before the handle may be used, it must be opened using #mdb_env_open().
417          * Various other options may also need to be set before opening the handle,
418          * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
419          * depending on usage requirements.
420          * @param[out] env The address where the new handle will be stored
421          * @return A non-zero error value on failure and 0 on success.
422          */
423 int  mdb_env_create(MDB_env **env);
424
425         /** @brief Open an environment handle.
426          *
427          * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
428          * @param[in] env An environment handle returned by #mdb_env_create()
429          * @param[in] path The directory in which the database files reside. This
430          * directory must already exist and be writable.
431          * @param[in] flags Special options for this environment. This parameter
432          * must be set to 0 or by bitwise OR'ing together one or more of the
433          * values described here.
434          * Flags set by mdb_env_set_flags() are also used.
435          * <ul>
436          *      <li>#MDB_FIXEDMAP
437          *      use a fixed address for the mmap region. This flag must be specified
438          *      when creating the environment, and is stored persistently in the environment.
439          *              If successful, the memory map will always reside at the same virtual address
440          *              and pointers used to reference data items in the database will be constant
441          *              across multiple invocations. This option may not always work, depending on
442          *              how the operating system has allocated memory to shared libraries and other uses.
443          *              The feature is highly experimental.
444          *      <li>#MDB_NOSUBDIR
445          *              By default, MDB creates its environment in a directory whose
446          *              pathname is given in \b path, and creates its data and lock files
447          *              under that directory. With this option, \b path is used as-is for
448          *              the database main data file. The database lock file is the \b path
449          *              with "-lock" appended.
450          *      <li>#MDB_RDONLY
451          *              Open the environment in read-only mode. No write operations will be
452          *              allowed. MDB will still modify the lock file - except on read-only
453          *              filesystems, where MDB does not use locks.
454          *      <li>#MDB_WRITEMAP
455          *              Use a writeable memory map unless MDB_RDONLY is set. This is faster
456          *              and uses fewer mallocs, but loses protection from application bugs
457          *              like wild pointer writes and other bad updates into the database.
458          *              Incompatible with nested transactions.
459          *      <li>#MDB_NOMETASYNC
460          *              Flush system buffers to disk only once per transaction, omit the
461          *              metadata flush. Defer that until the system flushes files to disk,
462          *              or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
463          *              maintains database integrity, but a system crash may undo the last
464          *              committed transaction. I.e. it preserves the ACI (atomicity,
465          *              consistency, isolation) but not D (durability) database property.
466          *              This flag may be changed at any time using #mdb_env_set_flags().
467          *      <li>#MDB_NOSYNC
468          *              Don't flush system buffers to disk when committing a transaction.
469          *              This optimization means a system crash can corrupt the database or
470          *              lose the last transactions if buffers are not yet flushed to disk.
471          *              The risk is governed by how often the system flushes dirty buffers
472          *              to disk and how often #mdb_env_sync() is called.  However, if the
473          *              filesystem preserves write order and the #MDB_WRITEMAP flag is not
474          *              used, transactions exhibit ACI (atomicity, consistency, isolation)
475          *              properties and only lose D (durability).  I.e. database integrity
476          *              is maintained, but a system crash may undo the final transactions.
477          *              Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
478          *              hint for when to write transactions to disk, unless #mdb_env_sync()
479          *              is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
480          *              This flag may be changed at any time using #mdb_env_set_flags().
481          *      <li>#MDB_MAPASYNC
482          *              When using #MDB_WRITEMAP, use asynchronous flushes to disk.
483          *              As with #MDB_NOSYNC, a system crash can then corrupt the
484          *              database or lose the last transactions. Calling #mdb_env_sync()
485          *              ensures on-disk database integrity until next commit.
486          *              This flag may be changed at any time using #mdb_env_set_flags().
487          * </ul>
488          * @param[in] mode The UNIX permissions to set on created files. This parameter
489          * is ignored on Windows.
490          * @return A non-zero error value on failure and 0 on success. Some possible
491          * errors are:
492          * <ul>
493          *      <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
494          *      version that created the database environment.
495          *      <li>#MDB_INVALID - the environment file headers are corrupted.
496          *      <li>ENOENT - the directory specified by the path parameter doesn't exist.
497          *      <li>EACCES - the user didn't have permission to access the environment files.
498          *      <li>EAGAIN - the environment was locked by another process.
499          * </ul>
500          */
501 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
502
503         /** @brief Copy an MDB environment to the specified path.
504          *
505          * This function may be used to make a backup of an existing environment.
506          * @param[in] env An environment handle returned by #mdb_env_create(). It
507          * must have already been opened successfully.
508          * @param[in] path The directory in which the copy will reside. This
509          * directory must already exist and be writable but must otherwise be
510          * empty.
511          * @return A non-zero error value on failure and 0 on success.
512          */
513 int  mdb_env_copy(MDB_env *env, const char *path);
514
515         /** @brief Return statistics about the MDB environment.
516          *
517          * @param[in] env An environment handle returned by #mdb_env_create()
518          * @param[out] stat The address of an #MDB_stat structure
519          *      where the statistics will be copied
520          */
521 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
522
523         /** @brief Return information about the MDB environment.
524          *
525          * @param[in] env An environment handle returned by #mdb_env_create()
526          * @param[out] stat The address of an #MDB_envinfo structure
527          *      where the information will be copied
528          */
529 int  mdb_env_info(MDB_env *env, MDB_envinfo *stat);
530
531         /** @brief Flush the data buffers to disk.
532          *
533          * Data is always written to disk when #mdb_txn_commit() is called,
534          * but the operating system may keep it buffered. MDB always flushes
535          * the OS buffers upon commit as well, unless the environment was
536          * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC.
537          * @param[in] env An environment handle returned by #mdb_env_create()
538          * @param[in] force If non-zero, force a synchronous flush.  Otherwise
539          *  if the environment has the #MDB_NOSYNC flag set the flushes
540          *      will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
541          * @return A non-zero error value on failure and 0 on success. Some possible
542          * errors are:
543          * <ul>
544          *      <li>EINVAL - an invalid parameter was specified.
545          *      <li>EIO - an error occurred during synchronization.
546          * </ul>
547          */
548 int  mdb_env_sync(MDB_env *env, int force);
549
550         /** @brief Close the environment and release the memory map.
551          *
552          * Only a single thread may call this function. All transactions, databases,
553          * and cursors must already be closed before calling this function. Attempts to
554          * use any such handles after calling this function will cause a SIGSEGV.
555          * The environment handle will be freed and must not be used again after this call.
556          * @param[in] env An environment handle returned by #mdb_env_create()
557          */
558 void mdb_env_close(MDB_env *env);
559
560         /** @brief Set environment flags.
561          *
562          * This may be used to set some flags in addition to those from
563          * #mdb_env_open(), or to unset these flags.
564          * @param[in] env An environment handle returned by #mdb_env_create()
565          * @param[in] flags The flags to change, bitwise OR'ed together
566          * @param[in] onoff A non-zero value sets the flags, zero clears them.
567          * @return A non-zero error value on failure and 0 on success. Some possible
568          * errors are:
569          * <ul>
570          *      <li>EINVAL - an invalid parameter was specified.
571          * </ul>
572          */
573 int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
574
575         /** @brief Get environment flags.
576          *
577          * @param[in] env An environment handle returned by #mdb_env_create()
578          * @param[out] flags The address of an integer to store the flags
579          * @return A non-zero error value on failure and 0 on success. Some possible
580          * errors are:
581          * <ul>
582          *      <li>EINVAL - an invalid parameter was specified.
583          * </ul>
584          */
585 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
586
587         /** @brief Return the path that was used in #mdb_env_open().
588          *
589          * @param[in] env An environment handle returned by #mdb_env_create()
590          * @param[out] path Address of a string pointer to contain the path. This
591          * is the actual string in the environment, not a copy. It should not be
592          * altered in any way.
593          * @return A non-zero error value on failure and 0 on success. Some possible
594          * errors are:
595          * <ul>
596          *      <li>EINVAL - an invalid parameter was specified.
597          * </ul>
598          */
599 int  mdb_env_get_path(MDB_env *env, const char **path);
600
601         /** @brief Set the size of the memory map to use for this environment.
602          *
603          * The size should be a multiple of the OS page size. The default is
604          * 10485760 bytes. The size of the memory map is also the maximum size
605          * of the database. The value should be chosen as large as possible,
606          * to accommodate future growth of the database.
607          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
608          * The size may be changed by closing and reopening the environment.
609          * Any attempt to set a size smaller than the space already consumed
610          * by the environment will be silently changed to the current size of the used space.
611          * @param[in] env An environment handle returned by #mdb_env_create()
612          * @param[in] size The size in bytes
613          * @return A non-zero error value on failure and 0 on success. Some possible
614          * errors are:
615          * <ul>
616          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
617          * </ul>
618          */
619 int  mdb_env_set_mapsize(MDB_env *env, size_t size);
620
621         /** @brief Set the maximum number of threads for the environment.
622          *
623          * This defines the number of slots in the lock table that is used to track readers in the
624          * the environment. The default is 126.
625          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
626          * @param[in] env An environment handle returned by #mdb_env_create()
627          * @param[in] readers The maximum number of threads
628          * @return A non-zero error value on failure and 0 on success. Some possible
629          * errors are:
630          * <ul>
631          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
632          * </ul>
633          */
634 int  mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
635
636         /** @brief Get the maximum number of threads for the environment.
637          *
638          * @param[in] env An environment handle returned by #mdb_env_create()
639          * @param[out] readers Address of an integer to store the number of readers
640          * @return A non-zero error value on failure and 0 on success. Some possible
641          * errors are:
642          * <ul>
643          *      <li>EINVAL - an invalid parameter was specified.
644          * </ul>
645          */
646 int  mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
647
648         /** @brief Set the maximum number of named databases for the environment.
649          *
650          * This function is only needed if multiple databases will be used in the
651          * environment. Simpler applications that use the environment as a single
652          * unnamed database can ignore this option.
653          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
654          * @param[in] env An environment handle returned by #mdb_env_create()
655          * @param[in] dbs The maximum number of databases
656          * @return A non-zero error value on failure and 0 on success. Some possible
657          * errors are:
658          * <ul>
659          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
660          * </ul>
661          */
662 int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
663
664         /** @brief Create a transaction for use with the environment.
665          *
666          * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
667          * @note Transactions may not span threads; a transaction must only be used by a
668          * single thread. Also, a thread may only have a single transaction.
669          * @note Cursors may not span transactions; each cursor must be opened and closed
670          * within a single transaction.
671          * @param[in] env An environment handle returned by #mdb_env_create()
672          * @param[in] parent If this parameter is non-NULL, the new transaction
673          * will be a nested transaction, with the transaction indicated by \b parent
674          * as its parent. Transactions may be nested to any level. A parent
675          * transaction may not issue any other operations besides mdb_txn_begin,
676          * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
677          * @param[in] flags Special options for this transaction. This parameter
678          * must be set to 0 or by bitwise OR'ing together one or more of the
679          * values described here.
680          * <ul>
681          *      <li>#MDB_RDONLY
682          *              This transaction will not perform any write operations.
683          * </ul>
684          * @param[out] txn Address where the new #MDB_txn handle will be stored
685          * @return A non-zero error value on failure and 0 on success. Some possible
686          * errors are:
687          * <ul>
688          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
689 -        *              must be shut down.
690          *      <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
691          *              mapsize and the environment must be shut down.
692          *      <li>#MDB_READERS_FULL - a read-only transaction was requested and
693          *              the reader lock table is full. See #mdb_env_set_maxreaders().
694          *      <li>ENOMEM - out of memory.
695          * </ul>
696          */
697 int  mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
698
699         /** @brief Commit all the operations of a transaction into the database.
700          *
701          * All cursors opened within the transaction will be closed by this call. The cursors
702          * and transaction handle will be freed and must not be used again after this call.
703          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
704          * @return A non-zero error value on failure and 0 on success. Some possible
705          * errors are:
706          * <ul>
707          *      <li>EINVAL - an invalid parameter was specified.
708          *      <li>ENOSPC - no more disk space.
709          *      <li>EIO - a low-level I/O error occurred while writing.
710          *      <li>ENOMEM - out of memory.
711          * </ul>
712          */
713 int  mdb_txn_commit(MDB_txn *txn);
714
715         /** @brief Abandon all the operations of the transaction instead of saving them.
716          *
717          * All cursors opened within the transaction will be closed by this call. The cursors
718          * and transaction handle will be freed and must not be used again after this call.
719          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
720          */
721 void mdb_txn_abort(MDB_txn *txn);
722
723         /** @brief Reset a read-only transaction.
724          *
725          * This releases the current reader lock but doesn't free the
726          * transaction handle, allowing it to be used again later by #mdb_txn_renew().
727          * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
728          * allocation/deallocation overhead if a thread is going to start a new
729          * read-only transaction again soon.
730          * All cursors opened within the transaction must be closed before the transaction
731          * is reset.
732          * Reader locks generally don't interfere with writers, but they keep old
733          * versions of database pages allocated. Thus they prevent the old pages
734          * from being reused when writers commit new data, and so under heavy load
735          * the database size may grow much more rapidly than otherwise.
736          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
737          */
738 void mdb_txn_reset(MDB_txn *txn);
739
740         /** @brief Renew a read-only transaction.
741          *
742          * This acquires a new reader lock for a transaction handle that had been
743          * released by #mdb_txn_reset(). It must be called before a reset transaction
744          * may be used again.
745          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
746          * @return A non-zero error value on failure and 0 on success. Some possible
747          * errors are:
748          * <ul>
749          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
750          *              must be shut down.
751          *      <li>EINVAL - an invalid parameter was specified.
752          * </ul>
753          */
754 int  mdb_txn_renew(MDB_txn *txn);
755
756 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
757 #define mdb_open(txn,name,flags,dbi)    mdb_dbi_open(txn,name,flags,dbi)
758 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
759 #define mdb_close(env,dbi)                              mdb_dbi_close(env,dbi)
760
761         /** @brief Open a database in the environment.
762          *
763          * The database handle may be discarded by calling #mdb_dbi_close().
764          * It denotes the name and parameters of a database, independently of
765          * whether such a database exists. It will not exist if the transaction
766          * which created it aborted, nor if another process deleted it. The
767          * database handle resides in the shared environment, it is not owned
768          * by the given transaction. Only one thread should call this function;
769          * it is not mutex-protected in a read-only transaction.
770          * Preexisting transactions, other than the current transaction and
771          * any parents, must not use the new handle. Nor must their children.
772          * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
773          * must be called before opening the environment.
774          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
775          * @param[in] name The name of the database to open. If only a single
776          *      database is needed in the environment, this value may be NULL.
777          * @param[in] flags Special options for this database. This parameter
778          * must be set to 0 or by bitwise OR'ing together one or more of the
779          * values described here.
780          * <ul>
781          *      <li>#MDB_REVERSEKEY
782          *              Keys are strings to be compared in reverse order, from the end
783          *              of the strings to the beginning. By default, Keys are treated as strings and
784          *              compared from beginning to end.
785          *      <li>#MDB_DUPSORT
786          *              Duplicate keys may be used in the database. (Or, from another perspective,
787          *              keys may have multiple data items, stored in sorted order.) By default
788          *              keys must be unique and may have only a single data item.
789          *      <li>#MDB_INTEGERKEY
790          *              Keys are binary integers in native byte order. Setting this option
791          *              requires all keys to be the same size, typically sizeof(int)
792          *              or sizeof(size_t).
793          *      <li>#MDB_DUPFIXED
794          *              This flag may only be used in combination with #MDB_DUPSORT. This option
795          *              tells the library that the data items for this database are all the same
796          *              size, which allows further optimizations in storage and retrieval. When
797          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
798          *              cursor operations may be used to retrieve multiple items at once.
799          *      <li>#MDB_INTEGERDUP
800          *              This option specifies that duplicate data items are also integers, and
801          *              should be sorted as such.
802          *      <li>#MDB_REVERSEDUP
803          *              This option specifies that duplicate data items should be compared as
804          *              strings in reverse order.
805          *      <li>#MDB_CREATE
806          *              Create the named database if it doesn't exist. This option is not
807          *              allowed in a read-only transaction or a read-only environment.
808          * </ul>
809          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
810          * @return A non-zero error value on failure and 0 on success. Some possible
811          * errors are:
812          * <ul>
813          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
814          *              and #MDB_CREATE was not specified.
815          *      <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
816          * </ul>
817          */
818 int  mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
819
820         /** @brief Retrieve statistics for a database.
821          *
822          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
823          * @param[in] dbi A database handle returned by #mdb_dbi_open()
824          * @param[out] stat The address of an #MDB_stat structure
825          *      where the statistics will be copied
826          * @return A non-zero error value on failure and 0 on success. Some possible
827          * errors are:
828          * <ul>
829          *      <li>EINVAL - an invalid parameter was specified.
830          * </ul>
831          */
832 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
833
834         /** @brief Close a database handle.
835          *
836          * This call is not mutex protected. Handles should only be closed by
837          * a single thread, and only if no other threads are going to reference
838          * the database handle or one of its cursors any further. Do not close
839          * a handle if an existing transaction has modified its database.
840          * @param[in] env An environment handle returned by #mdb_env_create()
841          * @param[in] dbi A database handle returned by #mdb_dbi_open()
842          */
843 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
844
845         /** @brief Delete a database and/or free all its pages.
846          *
847          * If the \b del parameter is 1, the DB handle will be closed
848          * and the DB will be deleted.
849          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
850          * @param[in] dbi A database handle returned by #mdb_dbi_open()
851          * @param[in] del 1 to delete the DB from the environment,
852          * 0 to just free its pages.
853          * @return A non-zero error value on failure and 0 on success.
854          */
855 int  mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
856
857         /** @brief Set a custom key comparison function for a database.
858          *
859          * The comparison function is called whenever it is necessary to compare a
860          * key specified by the application with a key currently stored in the database.
861          * If no comparison function is specified, and no special key flags were specified
862          * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
863          * before longer keys.
864          * @warning This function must be called before any data access functions are used,
865          * otherwise data corruption may occur. The same comparison function must be used by every
866          * program accessing the database, every time the database is used.
867          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
868          * @param[in] dbi A database handle returned by #mdb_dbi_open()
869          * @param[in] cmp A #MDB_cmp_func function
870          * @return A non-zero error value on failure and 0 on success. Some possible
871          * errors are:
872          * <ul>
873          *      <li>EINVAL - an invalid parameter was specified.
874          * </ul>
875          */
876 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
877
878         /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
879          *
880          * This comparison function is called whenever it is necessary to compare a data
881          * item specified by the application with a data item currently stored in the database.
882          * This function only takes effect if the database was opened with the #MDB_DUPSORT
883          * flag.
884          * If no comparison function is specified, and no special key flags were specified
885          * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
886          * before longer items.
887          * @warning This function must be called before any data access functions are used,
888          * otherwise data corruption may occur. The same comparison function must be used by every
889          * program accessing the database, every time the database is used.
890          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
891          * @param[in] dbi A database handle returned by #mdb_dbi_open()
892          * @param[in] cmp A #MDB_cmp_func function
893          * @return A non-zero error value on failure and 0 on success. Some possible
894          * errors are:
895          * <ul>
896          *      <li>EINVAL - an invalid parameter was specified.
897          * </ul>
898          */
899 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
900
901         /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
902          *
903          * @todo The relocation function is called whenever it is necessary to move the data
904          * of an item to a different position in the database (e.g. through tree
905          * balancing operations, shifts as a result of adds or deletes, etc.). It is
906          * intended to allow address/position-dependent data items to be stored in
907          * a database in an environment opened with the #MDB_FIXEDMAP option.
908          * Currently the relocation feature is unimplemented and setting
909          * this function has no effect.
910          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
911          * @param[in] dbi A database handle returned by #mdb_dbi_open()
912          * @param[in] rel A #MDB_rel_func function
913          * @return A non-zero error value on failure and 0 on success. Some possible
914          * errors are:
915          * <ul>
916          *      <li>EINVAL - an invalid parameter was specified.
917          * </ul>
918          */
919 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
920
921         /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
922          *
923          * See #mdb_set_relfunc and #MDB_rel_func for more details.
924          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
925          * @param[in] dbi A database handle returned by #mdb_dbi_open()
926          * @param[in] ctx An arbitrary pointer for whatever the application needs.
927          * It will be passed to the callback function set by #mdb_set_relfunc
928          * as its \b relctx parameter whenever the callback is invoked.
929          * @return A non-zero error value on failure and 0 on success. Some possible
930          * errors are:
931          * <ul>
932          *      <li>EINVAL - an invalid parameter was specified.
933          * </ul>
934          */
935 int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
936
937         /** @brief Get items from a database.
938          *
939          * This function retrieves key/data pairs from the database. The address
940          * and length of the data associated with the specified \b key are returned
941          * in the structure to which \b data refers.
942          * If the database supports duplicate keys (#MDB_DUPSORT) then the
943          * first data item for the key will be returned. Retrieval of other
944          * items requires the use of #mdb_cursor_get().
945          *
946          * @note The memory pointed to by the returned values is owned by the
947          * database. The caller need not dispose of the memory, and may not
948          * modify it in any way. For values returned in a read-only transaction
949          * any modification attempts will cause a SIGSEGV.
950          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
951          * @param[in] dbi A database handle returned by #mdb_dbi_open()
952          * @param[in] key The key to search for in the database
953          * @param[out] data The data corresponding to the key
954          * @return A non-zero error value on failure and 0 on success. Some possible
955          * errors are:
956          * <ul>
957          *      <li>#MDB_NOTFOUND - the key was not in the database.
958          *      <li>EINVAL - an invalid parameter was specified.
959          * </ul>
960          */
961 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
962
963         /** @brief Store items into a database.
964          *
965          * This function stores key/data pairs in the database. The default behavior
966          * is to enter the new key/data pair, replacing any previously existing key
967          * if duplicates are disallowed, or adding a duplicate data item if
968          * duplicates are allowed (#MDB_DUPSORT).
969          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
970          * @param[in] dbi A database handle returned by #mdb_dbi_open()
971          * @param[in] key The key to store in the database
972          * @param[in,out] data The data to store
973          * @param[in] flags Special options for this operation. This parameter
974          * must be set to 0 or by bitwise OR'ing together one or more of the
975          * values described here.
976          * <ul>
977          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
978          *              already appear in the database. This flag may only be specified
979          *              if the database was opened with #MDB_DUPSORT. The function will
980          *              return #MDB_KEYEXIST if the key/data pair already appears in the
981          *              database.
982          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
983          *              does not already appear in the database. The function will return
984          *              #MDB_KEYEXIST if the key already appears in the database, even if
985          *              the database supports duplicates (#MDB_DUPSORT). The \b data
986          *              parameter will be set to point to the existing item.
987          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
988          *              don't copy the given data. Instead, return a pointer to the
989          *              reserved space, which the caller can fill in later. This saves
990          *              an extra memcpy if the data is being generated later.
991          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
992          *              database. No key comparisons are performed. This option allows
993          *              fast bulk loading when keys are already known to be in the
994          *              correct order. Loading unsorted keys with this flag will cause
995          *              data corruption.
996          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
997          * </ul>
998          * @return A non-zero error value on failure and 0 on success. Some possible
999          * errors are:
1000          * <ul>
1001          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1002          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1003          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1004          *      <li>EINVAL - an invalid parameter was specified.
1005          * </ul>
1006          */
1007 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1008                             unsigned int flags);
1009
1010         /** @brief Delete items from a database.
1011          *
1012          * This function removes key/data pairs from the database.
1013          * If the database does not support sorted duplicate data items
1014          * (#MDB_DUPSORT) the data parameter is ignored.
1015          * If the database supports sorted duplicates and the data parameter
1016          * is NULL, all of the duplicate data items for the key will be
1017          * deleted. Otherwise, if the data parameter is non-NULL
1018          * only the matching data item will be deleted.
1019          * This function will return #MDB_NOTFOUND if the specified key/data
1020          * pair is not in the database.
1021          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1022          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1023          * @param[in] key The key to delete from the database
1024          * @param[in] data The data to delete
1025          * @return A non-zero error value on failure and 0 on success. Some possible
1026          * errors are:
1027          * <ul>
1028          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1029          *      <li>EINVAL - an invalid parameter was specified.
1030          * </ul>
1031          */
1032 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1033
1034         /** @brief Create a cursor handle.
1035          *
1036          * Cursors are associated with a specific transaction and database and
1037          * may not span threads.
1038          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1039          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1040          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1041          * @return A non-zero error value on failure and 0 on success. Some possible
1042          * errors are:
1043          * <ul>
1044          *      <li>EINVAL - an invalid parameter was specified.
1045          * </ul>
1046          */
1047 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1048
1049         /** @brief Close a cursor handle.
1050          *
1051          * The cursor handle will be freed and must not be used again after this call.
1052          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1053          */
1054 void mdb_cursor_close(MDB_cursor *cursor);
1055
1056         /** @brief Renew a cursor handle.
1057          *
1058          * Cursors are associated with a specific transaction and database and
1059          * may not span threads. Cursors that are only used in read-only
1060          * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1061          * The cursor may be associated with a new read-only transaction, and
1062          * referencing the same database handle as it was created with.
1063          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1064          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1065          * @return A non-zero error value on failure and 0 on success. Some possible
1066          * errors are:
1067          * <ul>
1068          *      <li>EINVAL - an invalid parameter was specified.
1069          * </ul>
1070          */
1071 int  mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1072
1073         /** @brief Return the cursor's transaction handle.
1074          *
1075          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1076          */
1077 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1078
1079         /** @brief Return the cursor's database handle.
1080          *
1081          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1082          */
1083 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1084
1085         /** @brief Retrieve by cursor.
1086          *
1087          * This function retrieves key/data pairs from the database. The address and length
1088          * of the key are returned in the object to which \b key refers (except for the
1089          * case of the #MDB_SET option, in which the \b key object is unchanged), and
1090          * the address and length of the data are returned in the object to which \b data
1091          * refers.
1092          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1093          * @param[in,out] key The key for a retrieved item
1094          * @param[in,out] data The data of a retrieved item
1095          * @param[in] op A cursor operation #MDB_cursor_op
1096          * @return A non-zero error value on failure and 0 on success. Some possible
1097          * errors are:
1098          * <ul>
1099          *      <li>#MDB_NOTFOUND - no matching key found.
1100          *      <li>EINVAL - an invalid parameter was specified.
1101          * </ul>
1102          */
1103 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1104                             MDB_cursor_op op);
1105
1106         /** @brief Store by cursor.
1107          *
1108          * This function stores key/data pairs into the database.
1109          * If the function fails for any reason, the state of the cursor will be
1110          * unchanged. If the function succeeds and an item is inserted into the
1111          * database, the cursor is always positioned to refer to the newly inserted item.
1112          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1113          * @param[in] key The key operated on.
1114          * @param[in] data The data operated on.
1115          * @param[in] flags Options for this operation. This parameter
1116          * must be set to 0 or one of the values described here.
1117          * <ul>
1118          *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
1119          *              the cursor refers with the specified data item. The \b key
1120          *              parameter is ignored.
1121          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1122          *              already appear in the database. This flag may only be specified
1123          *              if the database was opened with #MDB_DUPSORT. The function will
1124          *              return #MDB_KEYEXIST if the key/data pair already appears in the
1125          *              database.
1126          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1127          *              does not already appear in the database. The function will return
1128          *              #MDB_KEYEXIST if the key already appears in the database, even if
1129          *              the database supports duplicates (#MDB_DUPSORT).
1130          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
1131          *              don't copy the given data. Instead, return a pointer to the
1132          *              reserved space, which the caller can fill in later. This saves
1133          *              an extra memcpy if the data is being generated later.
1134          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1135          *              database. No key comparisons are performed. This option allows
1136          *              fast bulk loading when keys are already known to be in the
1137          *              correct order. Loading unsorted keys with this flag will cause
1138          *              data corruption.
1139          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1140          * </ul>
1141          * @return A non-zero error value on failure and 0 on success. Some possible
1142          * errors are:
1143          * <ul>
1144          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1145          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1146          *      <li>EACCES - an attempt was made to modify a read-only database.
1147          *      <li>EINVAL - an invalid parameter was specified.
1148          * </ul>
1149          */
1150 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1151                                 unsigned int flags);
1152
1153         /** @brief Delete current key/data pair
1154          *
1155          * This function deletes the key/data pair to which the cursor refers.
1156          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1157          * @param[in] flags Options for this operation. This parameter
1158          * must be set to 0 or one of the values described here.
1159          * <ul>
1160          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1161          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
1162          * </ul>
1163          * @return A non-zero error value on failure and 0 on success. Some possible
1164          * errors are:
1165          * <ul>
1166          *      <li>EACCES - an attempt was made to modify a read-only database.
1167          *      <li>EINVAL - an invalid parameter was specified.
1168          * </ul>
1169          */
1170 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1171
1172         /** @brief Return count of duplicates for current key.
1173          *
1174          * This call is only valid on databases that support sorted duplicate
1175          * data items #MDB_DUPSORT.
1176          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1177          * @param[out] countp Address where the count will be stored
1178          * @return A non-zero error value on failure and 0 on success. Some possible
1179          * errors are:
1180          * <ul>
1181          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1182          * </ul>
1183          */
1184 int  mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1185
1186         /** @brief Compare two data items according to a particular database.
1187          *
1188          * This returns a comparison as if the two data items were keys in the
1189          * specified database.
1190          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1191          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1192          * @param[in] a The first item to compare
1193          * @param[in] b The second item to compare
1194          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1195          */
1196 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1197
1198         /** @brief Compare two data items according to a particular database.
1199          *
1200          * This returns a comparison as if the two items were data items of
1201          * a sorted duplicates #MDB_DUPSORT database.
1202          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1203          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1204          * @param[in] a The first item to compare
1205          * @param[in] b The second item to compare
1206          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1207          */
1208 int  mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1209 /**     @} */
1210
1211 #ifdef __cplusplus
1212 }
1213 #endif
1214 #endif /* _LMDB_H_ */