]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/lmdb.h
d616cabf95112feb5ef4e905adfdfea4c5de9df2
[openldap] / libraries / liblmdb / lmdb.h
1 /** @file lmdb.h
2  *      @brief Lightning memory-mapped database library
3  *
4  *      @mainpage       Lightning Memory-Mapped Database Manager (LMDB)
5  *
6  *      @section intro_sec Introduction
7  *      LMDB 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, LMDB 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. LMDB 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  *      If this is your first time using a transactional embedded key/value
44  *      store, you may find the \ref starting page to be helpful.
45  *
46  *      @section caveats_sec Caveats
47  *      Troubleshooting the lock file, plus semaphores on BSD systems:
48  *
49  *      - A broken lockfile can cause sync issues.
50  *        Stale reader transactions left behind by an aborted program
51  *        cause further writes to grow the database quickly, and
52  *        stale locks can block further operation.
53  *
54  *        Fix: Check for stale readers periodically, using the
55  *        #mdb_reader_check function or the \ref mdb_stat_1 "mdb_stat" tool.
56  *        Stale writers will be cleared automatically on most systems:
57  *        - Windows - automatic
58  *        - BSD, systems using SysV semaphores - automatic
59  *        - Linux, systems using POSIX mutexes with Robust option - automatic
60  *        Otherwise just make all programs using the database close it;
61  *        the lockfile is always reset on first open of the environment.
62  *
63  *      - On BSD systems or others configured with MDB_USE_SYSV_SEM or
64  *        MDB_USE_POSIX_SEM,
65  *        startup can fail due to semaphores owned by another userid.
66  *
67  *        Fix: Open and close the database as the user which owns the
68  *        semaphores (likely last user) or as root, while no other
69  *        process is using the database.
70  *
71  *      Restrictions/caveats (in addition to those listed for some functions):
72  *
73  *      - Only the database owner should normally use the database on
74  *        BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
75  *        Multiple users can cause startup to fail later, as noted above.
76  *
77  *      - There is normally no pure read-only mode, since readers need write
78  *        access to locks and lock file. Exceptions: On read-only filesystems
79  *        or with the #MDB_NOLOCK flag described under #mdb_env_open().
80  *
81  *      - By default, in versions before 0.9.10, unused portions of the data
82  *        file might receive garbage data from memory freed by other code.
83  *        (This does not happen when using the #MDB_WRITEMAP flag.) As of
84  *        0.9.10 the default behavior is to initialize such memory before
85  *        writing to the data file. Since there may be a slight performance
86  *        cost due to this initialization, applications may disable it using
87  *        the #MDB_NOMEMINIT flag. Applications handling sensitive data
88  *        which must not be written should not use this flag. This flag is
89  *        irrelevant when using #MDB_WRITEMAP.
90  *
91  *      - A thread can only use one transaction at a time, plus any child
92  *        transactions.  Each transaction belongs to one thread.  See below.
93  *        The #MDB_NOTLS flag changes this for read-only transactions.
94  *
95  *      - Use an MDB_env* in the process which opened it, without fork()ing.
96  *
97  *      - Do not have open an LMDB database twice in the same process at
98  *        the same time.  Not even from a plain open() call - close()ing it
99  *        breaks flock() advisory locking.
100  *
101  *      - Avoid long-lived transactions.  Read transactions prevent
102  *        reuse of pages freed by newer write transactions, thus the
103  *        database can grow quickly.  Write transactions prevent
104  *        other write transactions, since writes are serialized.
105  *
106  *      - Avoid suspending a process with active transactions.  These
107  *        would then be "long-lived" as above.  Also read transactions
108  *        suspended when writers commit could sometimes see wrong data.
109  *
110  *      ...when several processes can use a database concurrently:
111  *
112  *      - Avoid aborting a process with an active transaction.
113  *        The transaction becomes "long-lived" as above until a check
114  *        for stale readers is performed or the lockfile is reset,
115  *        since the process may not remove it from the lockfile.
116  *
117  *        This does not apply to write transactions if the system clears
118  *        stale writers, see above.
119  *
120  *      - If you do that anyway, do a periodic check for stale readers. Or
121  *        close the environment once in a while, so the lockfile can get reset.
122  *
123  *      - Do not use LMDB databases on remote filesystems, even between
124  *        processes on the same host.  This breaks flock() on some OSes,
125  *        possibly memory map sync, and certainly sync between programs
126  *        on different hosts.
127  *
128  *      - Opening a database can fail if another process is opening or
129  *        closing it at exactly the same time.
130  *
131  *      @author Howard Chu, Symas Corporation.
132  *
133  *      @copyright Copyright 2011-2015 Howard Chu, Symas Corp. All rights reserved.
134  *
135  * Redistribution and use in source and binary forms, with or without
136  * modification, are permitted only as authorized by the OpenLDAP
137  * Public License.
138  *
139  * A copy of this license is available in the file LICENSE in the
140  * top-level directory of the distribution or, alternatively, at
141  * <http://www.OpenLDAP.org/license.html>.
142  *
143  *      @par Derived From:
144  * This code is derived from btree.c written by Martin Hedenfalk.
145  *
146  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
147  *
148  * Permission to use, copy, modify, and distribute this software for any
149  * purpose with or without fee is hereby granted, provided that the above
150  * copyright notice and this permission notice appear in all copies.
151  *
152  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
153  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
154  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
155  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
156  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
157  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
159  */
160 #ifndef _LMDB_H_
161 #define _LMDB_H_
162
163 #include <sys/types.h>
164 #include <inttypes.h>
165
166 #ifdef __cplusplus
167 extern "C" {
168 #endif
169
170 /** Unix permissions for creating files, or dummy definition for Windows */
171 #ifdef _MSC_VER
172 typedef int     mdb_mode_t;
173 #else
174 typedef mode_t  mdb_mode_t;
175 #endif
176
177 #ifdef MDB_VL32
178 typedef uint64_t        mdb_size_t;
179 #define mdb_env_create  mdb_env_create_vl32     /**< Prevent mixing with non-VL32 builds */
180 #else
181 typedef size_t  mdb_size_t;
182 #endif
183
184 /** An abstraction for a file handle.
185  *      On POSIX systems file handles are small integers. On Windows
186  *      they're opaque pointers.
187  */
188 #ifdef _WIN32
189 typedef void *mdb_filehandle_t;
190 #else
191 typedef int mdb_filehandle_t;
192 #endif
193
194 /** @defgroup mdb LMDB API
195  *      @{
196  *      @brief OpenLDAP Lightning Memory-Mapped Database Manager
197  */
198 /** @defgroup Version Version Macros
199  *      @{
200  */
201 /** Library major version */
202 #define MDB_VERSION_MAJOR       0
203 /** Library minor version */
204 #define MDB_VERSION_MINOR       9
205 /** Library patch version */
206 #define MDB_VERSION_PATCH       70
207
208 /** Combine args a,b,c into a single integer for easy version comparisons */
209 #define MDB_VERINT(a,b,c)       (((a) << 24) | ((b) << 16) | (c))
210
211 /** The full library version as a single integer */
212 #define MDB_VERSION_FULL        \
213         MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
214
215 /** The release date of this library version */
216 #define MDB_VERSION_DATE        "December 19, 2015"
217
218 /** A stringifier for the version info */
219 #define MDB_VERSTR(a,b,c,d)     "LMDB " #a "." #b "." #c ": (" d ")"
220
221 /** A helper for the stringifier macro */
222 #define MDB_VERFOO(a,b,c,d)     MDB_VERSTR(a,b,c,d)
223
224 /** The full library version as a C string */
225 #define MDB_VERSION_STRING      \
226         MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
227 /**     @} */
228
229 /** @brief Opaque structure for a database environment.
230  *
231  * A DB environment supports multiple databases, all residing in the same
232  * shared-memory map.
233  */
234 typedef struct MDB_env MDB_env;
235
236 /** @brief Opaque structure for a transaction handle.
237  *
238  * All database operations require a transaction handle. Transactions may be
239  * read-only or read-write.
240  */
241 typedef struct MDB_txn MDB_txn;
242
243 /** @brief A handle for an individual database in the DB environment. */
244 typedef unsigned int    MDB_dbi;
245
246 /** @brief Opaque structure for navigating through a database */
247 typedef struct MDB_cursor MDB_cursor;
248
249 /** @brief Generic structure used for passing keys and data in and out
250  * of the database.
251  *
252  * Values returned from the database are valid only until a subsequent
253  * update operation, or the end of the transaction. Do not modify or
254  * free them, they commonly point into the database itself.
255  *
256  * Key sizes must be between 1 and #mdb_env_get_maxkeysize() inclusive.
257  * The same applies to data sizes in databases with the #MDB_DUPSORT flag.
258  * Other data items can in theory be from 0 to 0xffffffff bytes long.
259  */
260 typedef struct MDB_val {
261         size_t           mv_size;       /**< size of the data item */
262         void            *mv_data;       /**< address of the data item */
263 } MDB_val;
264
265 /** @brief A callback function used to compare two keys in a database */
266 typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
267
268 /** @brief A callback function used to relocate a position-dependent data item
269  * in a fixed-address database.
270  *
271  * The \b newptr gives the item's desired address in
272  * the memory map, and \b oldptr gives its previous address. The item's actual
273  * data resides at the address in \b item.  This callback is expected to walk
274  * through the fields of the record in \b item and modify any
275  * values based at the \b oldptr address to be relative to the \b newptr address.
276  * @param[in,out] item The item that is to be relocated.
277  * @param[in] oldptr The previous address.
278  * @param[in] newptr The new address to relocate to.
279  * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
280  * @todo This feature is currently unimplemented.
281  */
282 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
283
284 /** @defgroup   mdb_env Environment Flags
285  *      @{
286  */
287         /** mmap at a fixed address (experimental) */
288 #define MDB_FIXEDMAP    0x01
289         /** no environment directory */
290 #define MDB_NOSUBDIR    0x4000
291         /** don't fsync after commit */
292 #define MDB_NOSYNC              0x10000
293         /** read only */
294 #define MDB_RDONLY              0x20000
295         /** don't fsync metapage after commit */
296 #define MDB_NOMETASYNC          0x40000
297         /** use writable mmap */
298 #define MDB_WRITEMAP            0x80000
299         /** use asynchronous msync when #MDB_WRITEMAP is used */
300 #define MDB_MAPASYNC            0x100000
301         /** tie reader locktable slots to #MDB_txn objects instead of to threads */
302 #define MDB_NOTLS               0x200000
303         /** don't do any locking, caller must manage their own locks */
304 #define MDB_NOLOCK              0x400000
305         /** don't do readahead (no effect on Windows) */
306 #define MDB_NORDAHEAD   0x800000
307         /** don't initialize malloc'd memory before writing to datafile */
308 #define MDB_NOMEMINIT   0x1000000
309 /** @} */
310
311 /**     @defgroup       mdb_dbi_open    Database Flags
312  *      @{
313  */
314         /** use reverse string keys */
315 #define MDB_REVERSEKEY  0x02
316         /** use sorted duplicates */
317 #define MDB_DUPSORT             0x04
318         /** numeric keys in native byte order: either unsigned int or size_t.
319          *  The keys must all be of the same size. */
320 #define MDB_INTEGERKEY  0x08
321         /** with #MDB_DUPSORT, sorted dup items have fixed size */
322 #define MDB_DUPFIXED    0x10
323         /** with #MDB_DUPSORT, dups are #MDB_INTEGERKEY-style integers */
324 #define MDB_INTEGERDUP  0x20
325         /** with #MDB_DUPSORT, use reverse string dups */
326 #define MDB_REVERSEDUP  0x40
327         /** create DB if not already existing */
328 #define MDB_CREATE              0x40000
329 /** @} */
330
331 /**     @defgroup mdb_put       Write Flags
332  *      @{
333  */
334 /** For put: Don't write if the key already exists. */
335 #define MDB_NOOVERWRITE 0x10
336 /** Only for #MDB_DUPSORT<br>
337  * For put: don't write if the key and data pair already exist.<br>
338  * For mdb_cursor_del: remove all duplicate data items.
339  */
340 #define MDB_NODUPDATA   0x20
341 /** For mdb_cursor_put: overwrite the current key/data pair */
342 #define MDB_CURRENT     0x40
343 /** For put: Just reserve space for data, don't copy it. Return a
344  * pointer to the reserved space.
345  */
346 #define MDB_RESERVE     0x10000
347 /** Data is being appended, don't split full pages. */
348 #define MDB_APPEND      0x20000
349 /** Duplicate data is being appended, don't split full pages. */
350 #define MDB_APPENDDUP   0x40000
351 /** Store multiple data items in one call. Only for #MDB_DUPFIXED. */
352 #define MDB_MULTIPLE    0x80000
353 /*      @} */
354
355 /**     @defgroup mdb_copy      Copy Flags
356  *      @{
357  */
358 /** Compacting copy: Omit free space from copy, and renumber all
359  * pages sequentially.
360  */
361 #define MDB_CP_COMPACT  0x01
362 /*      @} */
363
364 /** @brief Cursor Get operations.
365  *
366  *      This is the set of all operations for retrieving data
367  *      using a cursor.
368  */
369 typedef enum MDB_cursor_op {
370         MDB_FIRST,                              /**< Position at first key/data item */
371         MDB_FIRST_DUP,                  /**< Position at first data item of current key.
372                                                                 Only for #MDB_DUPSORT */
373         MDB_GET_BOTH,                   /**< Position at key/data pair. Only for #MDB_DUPSORT */
374         MDB_GET_BOTH_RANGE,             /**< position at key, nearest data. Only for #MDB_DUPSORT */
375         MDB_GET_CURRENT,                /**< Return key/data at current cursor position */
376         MDB_GET_MULTIPLE,               /**< Return key and up to a page of duplicate data items
377                                                                 from current cursor position. Move cursor to prepare
378                                                                 for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
379         MDB_LAST,                               /**< Position at last key/data item */
380         MDB_LAST_DUP,                   /**< Position at last data item of current key.
381                                                                 Only for #MDB_DUPSORT */
382         MDB_NEXT,                               /**< Position at next data item */
383         MDB_NEXT_DUP,                   /**< Position at next data item of current key.
384                                                                 Only for #MDB_DUPSORT */
385         MDB_NEXT_MULTIPLE,              /**< Return key and up to a page of duplicate data items
386                                                                 from next cursor position. Move cursor to prepare
387                                                                 for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */
388         MDB_NEXT_NODUP,                 /**< Position at first data item of next key */
389         MDB_PREV,                               /**< Position at previous data item */
390         MDB_PREV_DUP,                   /**< Position at previous data item of current key.
391                                                                 Only for #MDB_DUPSORT */
392         MDB_PREV_NODUP,                 /**< Position at last data item of previous key */
393         MDB_SET,                                /**< Position at specified key */
394         MDB_SET_KEY,                    /**< Position at specified key, return key + data */
395         MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
396 } MDB_cursor_op;
397
398 /** @defgroup  errors   Return Codes
399  *
400  *      BerkeleyDB uses -30800 to -30999, we'll go under them
401  *      @{
402  */
403         /**     Successful result */
404 #define MDB_SUCCESS      0
405         /** key/data pair already exists */
406 #define MDB_KEYEXIST    (-30799)
407         /** key/data pair not found (EOF) */
408 #define MDB_NOTFOUND    (-30798)
409         /** Requested page not found - this usually indicates corruption */
410 #define MDB_PAGE_NOTFOUND       (-30797)
411         /** Located page was wrong type */
412 #define MDB_CORRUPTED   (-30796)
413         /** Update of meta page failed or environment had fatal error */
414 #define MDB_PANIC               (-30795)
415         /** Environment version mismatch */
416 #define MDB_VERSION_MISMATCH    (-30794)
417         /** File is not a valid LMDB file */
418 #define MDB_INVALID     (-30793)
419         /** Environment mapsize reached */
420 #define MDB_MAP_FULL    (-30792)
421         /** Environment maxdbs reached */
422 #define MDB_DBS_FULL    (-30791)
423         /** Environment maxreaders reached */
424 #define MDB_READERS_FULL        (-30790)
425         /** Too many TLS keys in use - Windows only */
426 #define MDB_TLS_FULL    (-30789)
427         /** Txn has too many dirty pages */
428 #define MDB_TXN_FULL    (-30788)
429         /** Cursor stack too deep - internal error */
430 #define MDB_CURSOR_FULL (-30787)
431         /** Page has not enough space - internal error */
432 #define MDB_PAGE_FULL   (-30786)
433         /** Database contents grew beyond environment mapsize */
434 #define MDB_MAP_RESIZED (-30785)
435         /** Operation and DB incompatible, or DB type changed. This can mean:
436          *      <ul>
437          *      <li>The operation expects an #MDB_DUPSORT / #MDB_DUPFIXED database.
438          *      <li>Opening a named DB when the unnamed DB has #MDB_DUPSORT / #MDB_INTEGERKEY.
439          *      <li>Accessing a data record as a database, or vice versa.
440          *      <li>The database was dropped and recreated with different flags.
441          *      </ul>
442          */
443 #define MDB_INCOMPATIBLE        (-30784)
444         /** Invalid reuse of reader locktable slot */
445 #define MDB_BAD_RSLOT           (-30783)
446         /** Transaction must abort, has a child, or is invalid */
447 #define MDB_BAD_TXN                     (-30782)
448         /** Unsupported size of key/DB name/data, or wrong DUPFIXED size */
449 #define MDB_BAD_VALSIZE         (-30781)
450         /** The specified DBI was changed unexpectedly */
451 #define MDB_BAD_DBI             (-30780)
452         /** The last defined error code */
453 #define MDB_LAST_ERRCODE        MDB_BAD_DBI
454 /** @} */
455
456 /** @brief Statistics for a database in the environment */
457 typedef struct MDB_stat {
458         unsigned int    ms_psize;                       /**< Size of a database page.
459                                                                                         This is currently the same for all databases. */
460         unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
461         mdb_size_t              ms_branch_pages;        /**< Number of internal (non-leaf) pages */
462         mdb_size_t              ms_leaf_pages;          /**< Number of leaf pages */
463         mdb_size_t              ms_overflow_pages;      /**< Number of overflow pages */
464         mdb_size_t              ms_entries;                     /**< Number of data items */
465 } MDB_stat;
466
467 /** @brief Information about the environment */
468 typedef struct MDB_envinfo {
469         void    *me_mapaddr;                    /**< Address of map, if fixed */
470         mdb_size_t      me_mapsize;                             /**< Size of the data memory map */
471         mdb_size_t      me_last_pgno;                   /**< ID of the last used page */
472         mdb_size_t      me_last_txnid;                  /**< ID of the last committed transaction */
473         unsigned int me_maxreaders;             /**< max reader slots in the environment */
474         unsigned int me_numreaders;             /**< max reader slots used in the environment */
475 } MDB_envinfo;
476
477         /** @brief Return the LMDB library version information.
478          *
479          * @param[out] major if non-NULL, the library major version number is copied here
480          * @param[out] minor if non-NULL, the library minor version number is copied here
481          * @param[out] patch if non-NULL, the library patch version number is copied here
482          * @retval "version string" The library version as a string
483          */
484 char *mdb_version(int *major, int *minor, int *patch);
485
486         /** @brief Return a string describing a given error code.
487          *
488          * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
489          * function. If the error code is greater than or equal to 0, then the string
490          * returned by the system function strerror(3) is returned. If the error code
491          * is less than 0, an error string corresponding to the LMDB library error is
492          * returned. See @ref errors for a list of LMDB-specific error codes.
493          * @param[in] err The error code
494          * @retval "error message" The description of the error
495          */
496 char *mdb_strerror(int err);
497
498         /** @brief Create an LMDB environment handle.
499          *
500          * This function allocates memory for a #MDB_env structure. To release
501          * the allocated memory and discard the handle, call #mdb_env_close().
502          * Before the handle may be used, it must be opened using #mdb_env_open().
503          * Various other options may also need to be set before opening the handle,
504          * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
505          * depending on usage requirements.
506          * @param[out] env The address where the new handle will be stored
507          * @return A non-zero error value on failure and 0 on success.
508          */
509 int  mdb_env_create(MDB_env **env);
510
511         /** @brief Open an environment handle.
512          *
513          * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
514          * @param[in] env An environment handle returned by #mdb_env_create()
515          * @param[in] path The directory in which the database files reside. This
516          * directory must already exist and be writable.
517          * @param[in] flags Special options for this environment. This parameter
518          * must be set to 0 or by bitwise OR'ing together one or more of the
519          * values described here.
520          * Flags set by mdb_env_set_flags() are also used.
521          * <ul>
522          *      <li>#MDB_FIXEDMAP
523          *      use a fixed address for the mmap region. This flag must be specified
524          *      when creating the environment, and is stored persistently in the environment.
525          *              If successful, the memory map will always reside at the same virtual address
526          *              and pointers used to reference data items in the database will be constant
527          *              across multiple invocations. This option may not always work, depending on
528          *              how the operating system has allocated memory to shared libraries and other uses.
529          *              The feature is highly experimental.
530          *      <li>#MDB_NOSUBDIR
531          *              By default, LMDB creates its environment in a directory whose
532          *              pathname is given in \b path, and creates its data and lock files
533          *              under that directory. With this option, \b path is used as-is for
534          *              the database main data file. The database lock file is the \b path
535          *              with "-lock" appended.
536          *      <li>#MDB_RDONLY
537          *              Open the environment in read-only mode. No write operations will be
538          *              allowed. LMDB will still modify the lock file - except on read-only
539          *              filesystems, where LMDB does not use locks.
540          *      <li>#MDB_WRITEMAP
541          *              Use a writeable memory map unless MDB_RDONLY is set. This is faster
542          *              and uses fewer mallocs, but loses protection from application bugs
543          *              like wild pointer writes and other bad updates into the database.
544          *              Incompatible with nested transactions.
545          *              Do not mix processes with and without MDB_WRITEMAP on the same
546          *              environment.  This can defeat durability (#mdb_env_sync etc).
547          *      <li>#MDB_NOMETASYNC
548          *              Flush system buffers to disk only once per transaction, omit the
549          *              metadata flush. Defer that until the system flushes files to disk,
550          *              or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
551          *              maintains database integrity, but a system crash may undo the last
552          *              committed transaction. I.e. it preserves the ACI (atomicity,
553          *              consistency, isolation) but not D (durability) database property.
554          *              This flag may be changed at any time using #mdb_env_set_flags().
555          *      <li>#MDB_NOSYNC
556          *              Don't flush system buffers to disk when committing a transaction.
557          *              This optimization means a system crash can corrupt the database or
558          *              lose the last transactions if buffers are not yet flushed to disk.
559          *              The risk is governed by how often the system flushes dirty buffers
560          *              to disk and how often #mdb_env_sync() is called.  However, if the
561          *              filesystem preserves write order and the #MDB_WRITEMAP flag is not
562          *              used, transactions exhibit ACI (atomicity, consistency, isolation)
563          *              properties and only lose D (durability).  I.e. database integrity
564          *              is maintained, but a system crash may undo the final transactions.
565          *              Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
566          *              hint for when to write transactions to disk, unless #mdb_env_sync()
567          *              is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
568          *              This flag may be changed at any time using #mdb_env_set_flags().
569          *      <li>#MDB_MAPASYNC
570          *              When using #MDB_WRITEMAP, use asynchronous flushes to disk.
571          *              As with #MDB_NOSYNC, a system crash can then corrupt the
572          *              database or lose the last transactions. Calling #mdb_env_sync()
573          *              ensures on-disk database integrity until next commit.
574          *              This flag may be changed at any time using #mdb_env_set_flags().
575          *      <li>#MDB_NOTLS
576          *              Don't use Thread-Local Storage. Tie reader locktable slots to
577          *              #MDB_txn objects instead of to threads. I.e. #mdb_txn_reset() keeps
578          *              the slot reseved for the #MDB_txn object. A thread may use parallel
579          *              read-only transactions. A read-only transaction may span threads if
580          *              the user synchronizes its use. Applications that multiplex many
581          *              user threads over individual OS threads need this option. Such an
582          *              application must also serialize the write transactions in an OS
583          *              thread, since LMDB's write locking is unaware of the user threads.
584          *      <li>#MDB_NOLOCK
585          *              Don't do any locking. If concurrent access is anticipated, the
586          *              caller must manage all concurrency itself. For proper operation
587          *              the caller must enforce single-writer semantics, and must ensure
588          *              that no readers are using old transactions while a writer is
589          *              active. The simplest approach is to use an exclusive lock so that
590          *              no readers may be active at all when a writer begins.
591          *      <li>#MDB_NORDAHEAD
592          *              Turn off readahead. Most operating systems perform readahead on
593          *              read requests by default. This option turns it off if the OS
594          *              supports it. Turning it off may help random read performance
595          *              when the DB is larger than RAM and system RAM is full.
596          *              The option is not implemented on Windows.
597          *      <li>#MDB_NOMEMINIT
598          *              Don't initialize malloc'd memory before writing to unused spaces
599          *              in the data file. By default, memory for pages written to the data
600          *              file is obtained using malloc. While these pages may be reused in
601          *              subsequent transactions, freshly malloc'd pages will be initialized
602          *              to zeroes before use. This avoids persisting leftover data from other
603          *              code (that used the heap and subsequently freed the memory) into the
604          *              data file. Note that many other system libraries may allocate
605          *              and free memory from the heap for arbitrary uses. E.g., stdio may
606          *              use the heap for file I/O buffers. This initialization step has a
607          *              modest performance cost so some applications may want to disable
608          *              it using this flag. This option can be a problem for applications
609          *              which handle sensitive data like passwords, and it makes memory
610          *              checkers like Valgrind noisy. This flag is not needed with #MDB_WRITEMAP,
611          *              which writes directly to the mmap instead of using malloc for pages. The
612          *              initialization is also skipped if #MDB_RESERVE is used; the
613          *              caller is expected to overwrite all of the memory that was
614          *              reserved in that case.
615          *              This flag may be changed at any time using #mdb_env_set_flags().
616          * </ul>
617          * @param[in] mode The UNIX permissions to set on created files and semaphores.
618          * This parameter is ignored on Windows.
619          * @return A non-zero error value on failure and 0 on success. Some possible
620          * errors are:
621          * <ul>
622          *      <li>#MDB_VERSION_MISMATCH - the version of the LMDB library doesn't match the
623          *      version that created the database environment.
624          *      <li>#MDB_INVALID - the environment file headers are corrupted.
625          *      <li>ENOENT - the directory specified by the path parameter doesn't exist.
626          *      <li>EACCES - the user didn't have permission to access the environment files.
627          *      <li>EAGAIN - the environment was locked by another process.
628          * </ul>
629          */
630 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
631
632         /** @brief Copy an LMDB environment to the specified path.
633          *
634          * This function may be used to make a backup of an existing environment.
635          * No lockfile is created, since it gets recreated at need.
636          * @note This call can trigger significant file size growth if run in
637          * parallel with write transactions, because it employs a read-only
638          * transaction. See long-lived transactions under @ref caveats_sec.
639          * @param[in] env An environment handle returned by #mdb_env_create(). It
640          * must have already been opened successfully.
641          * @param[in] path The directory in which the copy will reside. This
642          * directory must already exist and be writable but must otherwise be
643          * empty.
644          * @return A non-zero error value on failure and 0 on success.
645          */
646 int  mdb_env_copy(MDB_env *env, const char *path);
647
648         /** @brief Copy an LMDB environment to the specified file descriptor.
649          *
650          * This function may be used to make a backup of an existing environment.
651          * No lockfile is created, since it gets recreated at need.
652          * @note This call can trigger significant file size growth if run in
653          * parallel with write transactions, because it employs a read-only
654          * transaction. See long-lived transactions under @ref caveats_sec.
655          * @param[in] env An environment handle returned by #mdb_env_create(). It
656          * must have already been opened successfully.
657          * @param[in] fd The filedescriptor to write the copy to. It must
658          * have already been opened for Write access.
659          * @return A non-zero error value on failure and 0 on success.
660          */
661 int  mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd);
662
663         /** @brief Copy an LMDB environment to the specified path, with options.
664          *
665          * This function may be used to make a backup of an existing environment.
666          * No lockfile is created, since it gets recreated at need.
667          * @note This call can trigger significant file size growth if run in
668          * parallel with write transactions, because it employs a read-only
669          * transaction. See long-lived transactions under @ref caveats_sec.
670          * @param[in] env An environment handle returned by #mdb_env_create(). It
671          * must have already been opened successfully.
672          * @param[in] path The directory in which the copy will reside. This
673          * directory must already exist and be writable but must otherwise be
674          * empty.
675          * @param[in] flags Special options for this operation. This parameter
676          * must be set to 0 or by bitwise OR'ing together one or more of the
677          * values described here.
678          * <ul>
679          *      <li>#MDB_CP_COMPACT - Perform compaction while copying: omit free
680          *              pages and sequentially renumber all pages in output. This option
681          *              consumes more CPU and runs more slowly than the default.
682          * </ul>
683          * @return A non-zero error value on failure and 0 on success.
684          */
685 int  mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags);
686
687         /** @brief Copy an LMDB environment to the specified file descriptor,
688          *      with options.
689          *
690          * This function may be used to make a backup of an existing environment.
691          * No lockfile is created, since it gets recreated at need. See
692          * #mdb_env_copy2() for further details.
693          * @note This call can trigger significant file size growth if run in
694          * parallel with write transactions, because it employs a read-only
695          * transaction. See long-lived transactions under @ref caveats_sec.
696          * @param[in] env An environment handle returned by #mdb_env_create(). It
697          * must have already been opened successfully.
698          * @param[in] fd The filedescriptor to write the copy to. It must
699          * have already been opened for Write access.
700          * @param[in] flags Special options for this operation.
701          * See #mdb_env_copy2() for options.
702          * @return A non-zero error value on failure and 0 on success.
703          */
704 int  mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags);
705
706         /** @brief Return statistics about the LMDB environment.
707          *
708          * @param[in] env An environment handle returned by #mdb_env_create()
709          * @param[out] stat The address of an #MDB_stat structure
710          *      where the statistics will be copied
711          */
712 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
713
714         /** @brief Return information about the LMDB environment.
715          *
716          * @param[in] env An environment handle returned by #mdb_env_create()
717          * @param[out] stat The address of an #MDB_envinfo structure
718          *      where the information will be copied
719          */
720 int  mdb_env_info(MDB_env *env, MDB_envinfo *stat);
721
722         /** @brief Flush the data buffers to disk.
723          *
724          * Data is always written to disk when #mdb_txn_commit() is called,
725          * but the operating system may keep it buffered. LMDB always flushes
726          * the OS buffers upon commit as well, unless the environment was
727          * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC. This call is
728          * not valid if the environment was opened with #MDB_RDONLY.
729          * @param[in] env An environment handle returned by #mdb_env_create()
730          * @param[in] force If non-zero, force a synchronous flush.  Otherwise
731          *  if the environment has the #MDB_NOSYNC flag set the flushes
732          *      will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
733          * @return A non-zero error value on failure and 0 on success. Some possible
734          * errors are:
735          * <ul>
736          *      <li>EACCES - the environment is read-only.
737          *      <li>EINVAL - an invalid parameter was specified.
738          *      <li>EIO - an error occurred during synchronization.
739          * </ul>
740          */
741 int  mdb_env_sync(MDB_env *env, int force);
742
743         /** @brief Close the environment and release the memory map.
744          *
745          * Only a single thread may call this function. All transactions, databases,
746          * and cursors must already be closed before calling this function. Attempts to
747          * use any such handles after calling this function will cause a SIGSEGV.
748          * The environment handle will be freed and must not be used again after this call.
749          * @param[in] env An environment handle returned by #mdb_env_create()
750          */
751 void mdb_env_close(MDB_env *env);
752
753         /** @brief Set environment flags.
754          *
755          * This may be used to set some flags in addition to those from
756          * #mdb_env_open(), or to unset these flags.  If several threads
757          * change the flags at the same time, the result is undefined.
758          * @param[in] env An environment handle returned by #mdb_env_create()
759          * @param[in] flags The flags to change, bitwise OR'ed together
760          * @param[in] onoff A non-zero value sets the flags, zero clears them.
761          * @return A non-zero error value on failure and 0 on success. Some possible
762          * errors are:
763          * <ul>
764          *      <li>EINVAL - an invalid parameter was specified.
765          * </ul>
766          */
767 int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
768
769         /** @brief Get environment flags.
770          *
771          * @param[in] env An environment handle returned by #mdb_env_create()
772          * @param[out] flags The address of an integer to store the flags
773          * @return A non-zero error value on failure and 0 on success. Some possible
774          * errors are:
775          * <ul>
776          *      <li>EINVAL - an invalid parameter was specified.
777          * </ul>
778          */
779 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
780
781         /** @brief Return the path that was used in #mdb_env_open().
782          *
783          * @param[in] env An environment handle returned by #mdb_env_create()
784          * @param[out] path Address of a string pointer to contain the path. This
785          * is the actual string in the environment, not a copy. It should not be
786          * altered in any way.
787          * @return A non-zero error value on failure and 0 on success. Some possible
788          * errors are:
789          * <ul>
790          *      <li>EINVAL - an invalid parameter was specified.
791          * </ul>
792          */
793 int  mdb_env_get_path(MDB_env *env, const char **path);
794
795         /** @brief Return the filedescriptor for the given environment.
796          *
797          * @param[in] env An environment handle returned by #mdb_env_create()
798          * @param[out] fd Address of a mdb_filehandle_t to contain the descriptor.
799          * @return A non-zero error value on failure and 0 on success. Some possible
800          * errors are:
801          * <ul>
802          *      <li>EINVAL - an invalid parameter was specified.
803          * </ul>
804          */
805 int  mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd);
806
807         /** @brief Set the size of the memory map to use for this environment.
808          *
809          * The size should be a multiple of the OS page size. The default is
810          * 10485760 bytes. The size of the memory map is also the maximum size
811          * of the database. The value should be chosen as large as possible,
812          * to accommodate future growth of the database.
813          * This function should be called after #mdb_env_create() and before #mdb_env_open().
814          * It may be called at later times if no transactions are active in
815          * this process. Note that the library does not check for this condition,
816          * the caller must ensure it explicitly.
817          *
818          * The new size takes effect immediately for the current process but
819          * will not be persisted to any others until a write transaction has been
820          * committed by the current process. Also, only mapsize increases are
821          * persisted into the environment.
822          *
823          * If the mapsize is increased by another process, and data has grown
824          * beyond the range of the current mapsize, #mdb_txn_begin() will
825          * return #MDB_MAP_RESIZED. This function may be called with a size
826          * of zero to adopt the new size.
827          *
828          * Any attempt to set a size smaller than the space already consumed
829          * by the environment will be silently changed to the current size of the used space.
830          * @param[in] env An environment handle returned by #mdb_env_create()
831          * @param[in] size The size in bytes
832          * @return A non-zero error value on failure and 0 on success. Some possible
833          * errors are:
834          * <ul>
835          *      <li>EINVAL - an invalid parameter was specified, or the environment has
836          *      an active write transaction.
837          * </ul>
838          */
839 int  mdb_env_set_mapsize(MDB_env *env, mdb_size_t size);
840
841         /** @brief Set the maximum number of threads/reader slots for the environment.
842          *
843          * This defines the number of slots in the lock table that is used to track readers in the
844          * the environment. The default is 126.
845          * Starting a read-only transaction normally ties a lock table slot to the
846          * current thread until the environment closes or the thread exits. If
847          * MDB_NOTLS is in use, #mdb_txn_begin() instead ties the slot to the
848          * MDB_txn object until it or the #MDB_env object is destroyed.
849          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
850          * @param[in] env An environment handle returned by #mdb_env_create()
851          * @param[in] readers The maximum number of reader lock table slots
852          * @return A non-zero error value on failure and 0 on success. Some possible
853          * errors are:
854          * <ul>
855          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
856          * </ul>
857          */
858 int  mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
859
860         /** @brief Get the maximum number of threads/reader slots for the environment.
861          *
862          * @param[in] env An environment handle returned by #mdb_env_create()
863          * @param[out] readers Address of an integer to store the number of readers
864          * @return A non-zero error value on failure and 0 on success. Some possible
865          * errors are:
866          * <ul>
867          *      <li>EINVAL - an invalid parameter was specified.
868          * </ul>
869          */
870 int  mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
871
872         /** @brief Set the maximum number of named databases for the environment.
873          *
874          * This function is only needed if multiple databases will be used in the
875          * environment. Simpler applications that use the environment as a single
876          * unnamed database can ignore this option.
877          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
878          *
879          * Currently a moderate number of slots are cheap but a huge number gets
880          * expensive: 7-120 words per transaction, and every #mdb_dbi_open()
881          * does a linear search of the opened slots.
882          * @param[in] env An environment handle returned by #mdb_env_create()
883          * @param[in] dbs The maximum number of databases
884          * @return A non-zero error value on failure and 0 on success. Some possible
885          * errors are:
886          * <ul>
887          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
888          * </ul>
889          */
890 int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
891
892         /** @brief Get the maximum size of keys and #MDB_DUPSORT data we can write.
893          *
894          * Depends on the compile-time constant #MDB_MAXKEYSIZE. Default 511.
895          * See @ref MDB_val.
896          * @param[in] env An environment handle returned by #mdb_env_create()
897          * @return The maximum size of a key we can write
898          */
899 int  mdb_env_get_maxkeysize(MDB_env *env);
900
901         /** @brief Set application information associated with the #MDB_env.
902          *
903          * @param[in] env An environment handle returned by #mdb_env_create()
904          * @param[in] ctx An arbitrary pointer for whatever the application needs.
905          * @return A non-zero error value on failure and 0 on success.
906          */
907 int  mdb_env_set_userctx(MDB_env *env, void *ctx);
908
909         /** @brief Get the application information associated with the #MDB_env.
910          *
911          * @param[in] env An environment handle returned by #mdb_env_create()
912          * @return The pointer set by #mdb_env_set_userctx().
913          */
914 void *mdb_env_get_userctx(MDB_env *env);
915
916         /** @brief A callback function for most LMDB assert() failures,
917          * called before printing the message and aborting.
918          *
919          * @param[in] env An environment handle returned by #mdb_env_create().
920          * @param[in] msg The assertion message, not including newline.
921          */
922 typedef void MDB_assert_func(MDB_env *env, const char *msg);
923
924         /** Set or reset the assert() callback of the environment.
925          * Disabled if liblmdb is buillt with NDEBUG.
926          * @note This hack should become obsolete as lmdb's error handling matures.
927          * @param[in] env An environment handle returned by #mdb_env_create().
928          * @param[in] func An #MDB_assert_func function, or 0.
929          * @return A non-zero error value on failure and 0 on success.
930          */
931 int  mdb_env_set_assert(MDB_env *env, MDB_assert_func *func);
932
933         /** @brief Create a transaction for use with the environment.
934          *
935          * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
936          * @note A transaction and its cursors must only be used by a single
937          * thread, and a thread may only have a single transaction at a time.
938          * If #MDB_NOTLS is in use, this does not apply to read-only transactions.
939          * @note Cursors may not span transactions.
940          * @param[in] env An environment handle returned by #mdb_env_create()
941          * @param[in] parent If this parameter is non-NULL, the new transaction
942          * will be a nested transaction, with the transaction indicated by \b parent
943          * as its parent. Transactions may be nested to any level. A parent
944          * transaction and its cursors may not issue any other operations than
945          * mdb_txn_commit and mdb_txn_abort while it has active child transactions.
946          * @param[in] flags Special options for this transaction. This parameter
947          * must be set to 0 or by bitwise OR'ing together one or more of the
948          * values described here.
949          * <ul>
950          *      <li>#MDB_RDONLY
951          *              This transaction will not perform any write operations.
952          *      <li>#MDB_NOSYNC
953          *              Don't flush system buffers to disk when committing this transaction.
954          *      <li>#MDB_NOMETASYNC
955          *              Flush system buffers but omit metadata flush when committing this transaction.
956          * </ul>
957          * @param[out] txn Address where the new #MDB_txn handle will be stored
958          * @return A non-zero error value on failure and 0 on success. Some possible
959          * errors are:
960          * <ul>
961          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
962          *              must be shut down.
963          *      <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
964          *              mapsize and this environment's map must be resized as well.
965          *              See #mdb_env_set_mapsize().
966          *      <li>#MDB_READERS_FULL - a read-only transaction was requested and
967          *              the reader lock table is full. See #mdb_env_set_maxreaders().
968          *      <li>ENOMEM - out of memory.
969          * </ul>
970          */
971 int  mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
972
973         /** @brief Returns the transaction's #MDB_env
974          *
975          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
976          */
977 MDB_env *mdb_txn_env(MDB_txn *txn);
978
979         /** @brief Return the transaction's ID.
980          *
981          * This returns the identifier associated with this transaction. For a
982          * read-only transaction, this corresponds to the snapshot being read;
983          * concurrent readers will frequently have the same transaction ID.
984          *
985          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
986          * @return A transaction ID, valid if input is an active transaction.
987          */
988 mdb_size_t mdb_txn_id(MDB_txn *txn);
989
990         /** @brief Commit all the operations of a transaction into the database.
991          *
992          * The transaction handle is freed. It and its cursors must not be used
993          * again after this call, except with #mdb_cursor_renew().
994          * @note Earlier documentation incorrectly said all cursors would be freed.
995          * Only write-transactions free cursors.
996          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
997          * @return A non-zero error value on failure and 0 on success. Some possible
998          * errors are:
999          * <ul>
1000          *      <li>EINVAL - an invalid parameter was specified.
1001          *      <li>ENOSPC - no more disk space.
1002          *      <li>EIO - a low-level I/O error occurred while writing.
1003          *      <li>ENOMEM - out of memory.
1004          * </ul>
1005          */
1006 int  mdb_txn_commit(MDB_txn *txn);
1007
1008         /** @brief Abandon all the operations of the transaction instead of saving them.
1009          *
1010          * The transaction handle is freed. It and its cursors must not be used
1011          * again after this call, except with #mdb_cursor_renew().
1012          * @note Earlier documentation incorrectly said all cursors would be freed.
1013          * Only write-transactions free cursors.
1014          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1015          */
1016 void mdb_txn_abort(MDB_txn *txn);
1017
1018         /** @brief Reset a read-only transaction.
1019          *
1020          * Abort the transaction like #mdb_txn_abort(), but keep the transaction
1021          * handle. #mdb_txn_renew() may reuse the handle. This saves allocation
1022          * overhead if the process will start a new read-only transaction soon,
1023          * and also locking overhead if #MDB_NOTLS is in use. The reader table
1024          * lock is released, but the table slot stays tied to its thread or
1025          * #MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free
1026          * its lock table slot if MDB_NOTLS is in use.
1027          * Cursors opened within the transaction must not be used
1028          * again after this call, except with #mdb_cursor_renew().
1029          * Reader locks generally don't interfere with writers, but they keep old
1030          * versions of database pages allocated. Thus they prevent the old pages
1031          * from being reused when writers commit new data, and so under heavy load
1032          * the database size may grow much more rapidly than otherwise.
1033          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1034          */
1035 void mdb_txn_reset(MDB_txn *txn);
1036
1037         /** @brief Renew a read-only transaction.
1038          *
1039          * This acquires a new reader lock for a transaction handle that had been
1040          * released by #mdb_txn_reset(). It must be called before a reset transaction
1041          * may be used again.
1042          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1043          * @return A non-zero error value on failure and 0 on success. Some possible
1044          * errors are:
1045          * <ul>
1046          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
1047          *              must be shut down.
1048          *      <li>EINVAL - an invalid parameter was specified.
1049          * </ul>
1050          */
1051 int  mdb_txn_renew(MDB_txn *txn);
1052
1053 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
1054 #define mdb_open(txn,name,flags,dbi)    mdb_dbi_open(txn,name,flags,dbi)
1055 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
1056 #define mdb_close(env,dbi)                              mdb_dbi_close(env,dbi)
1057
1058         /** @brief Open a database in the environment.
1059          *
1060          * A database handle denotes the name and parameters of a database,
1061          * independently of whether such a database exists.
1062          * The database handle may be discarded by calling #mdb_dbi_close().
1063          * The old database handle is returned if the database was already open.
1064          * The handle may only be closed once.
1065          *
1066          * The database handle will be private to the current transaction until
1067          * the transaction is successfully committed. If the transaction is
1068          * aborted the handle will be closed automatically.
1069          * After a successful commit the handle will reside in the shared
1070          * environment, and may be used by other transactions.
1071          *
1072          * This function must not be called from multiple concurrent
1073          * transactions in the same process. A transaction that uses
1074          * this function must finish (either commit or abort) before
1075          * any other transaction in the process may use this function.
1076          *
1077          * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
1078          * must be called before opening the environment.  Database names are
1079          * keys in the unnamed database, and may be read but not written.
1080          *
1081          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1082          * @param[in] name The name of the database to open. If only a single
1083          *      database is needed in the environment, this value may be NULL.
1084          * @param[in] flags Special options for this database. This parameter
1085          * must be set to 0 or by bitwise OR'ing together one or more of the
1086          * values described here.
1087          * <ul>
1088          *      <li>#MDB_REVERSEKEY
1089          *              Keys are strings to be compared in reverse order, from the end
1090          *              of the strings to the beginning. By default, Keys are treated as strings and
1091          *              compared from beginning to end.
1092          *      <li>#MDB_DUPSORT
1093          *              Duplicate keys may be used in the database. (Or, from another perspective,
1094          *              keys may have multiple data items, stored in sorted order.) By default
1095          *              keys must be unique and may have only a single data item.
1096          *      <li>#MDB_INTEGERKEY
1097          *              Keys are binary integers in native byte order, either unsigned int
1098          *              or size_t, and will be sorted as such.
1099          *              The keys must all be of the same size.
1100          *      <li>#MDB_DUPFIXED
1101          *              This flag may only be used in combination with #MDB_DUPSORT. This option
1102          *              tells the library that the data items for this database are all the same
1103          *              size, which allows further optimizations in storage and retrieval. When
1104          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
1105          *              cursor operations may be used to retrieve multiple items at once.
1106          *      <li>#MDB_INTEGERDUP
1107          *              This option specifies that duplicate data items are binary integers,
1108          *              similar to #MDB_INTEGERKEY keys.
1109          *      <li>#MDB_REVERSEDUP
1110          *              This option specifies that duplicate data items should be compared as
1111          *              strings in reverse order.
1112          *      <li>#MDB_CREATE
1113          *              Create the named database if it doesn't exist. This option is not
1114          *              allowed in a read-only transaction or a read-only environment.
1115          * </ul>
1116          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
1117          * @return A non-zero error value on failure and 0 on success. Some possible
1118          * errors are:
1119          * <ul>
1120          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
1121          *              and #MDB_CREATE was not specified.
1122          *      <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
1123          * </ul>
1124          */
1125 int  mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
1126
1127         /** @brief Retrieve statistics for a database.
1128          *
1129          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1130          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1131          * @param[out] stat The address of an #MDB_stat structure
1132          *      where the statistics will be copied
1133          * @return A non-zero error value on failure and 0 on success. Some possible
1134          * errors are:
1135          * <ul>
1136          *      <li>EINVAL - an invalid parameter was specified.
1137          * </ul>
1138          */
1139 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
1140
1141         /** @brief Retrieve the DB flags for a database handle.
1142          *
1143          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1144          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1145          * @param[out] flags Address where the flags will be returned.
1146          * @return A non-zero error value on failure and 0 on success.
1147          */
1148 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags);
1149
1150         /** @brief Close a database handle. Normally unnecessary. Use with care:
1151          *
1152          * This call is not mutex protected. Handles should only be closed by
1153          * a single thread, and only if no other threads are going to reference
1154          * the database handle or one of its cursors any further. Do not close
1155          * a handle if an existing transaction has modified its database.
1156          * Doing so can cause misbehavior from database corruption to errors
1157          * like MDB_BAD_VALSIZE (since the DB name is gone).
1158          *
1159          * Closing a database handle is not necessary, but lets #mdb_dbi_open()
1160          * reuse the handle value.  Usually it's better to set a bigger
1161          * #mdb_env_set_maxdbs(), unless that value would be large.
1162          *
1163          * @param[in] env An environment handle returned by #mdb_env_create()
1164          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1165          */
1166 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
1167
1168         /** @brief Empty or delete+close a database.
1169          *
1170          * See #mdb_dbi_close() for restrictions about closing the DB handle.
1171          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1172          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1173          * @param[in] del 0 to empty the DB, 1 to delete it from the
1174          * environment and close the DB handle.
1175          * @return A non-zero error value on failure and 0 on success.
1176          */
1177 int  mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
1178
1179         /** @brief Set a custom key comparison function for a database.
1180          *
1181          * The comparison function is called whenever it is necessary to compare a
1182          * key specified by the application with a key currently stored in the database.
1183          * If no comparison function is specified, and no special key flags were specified
1184          * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
1185          * before longer keys.
1186          * @warning This function must be called before any data access functions are used,
1187          * otherwise data corruption may occur. The same comparison function must be used by every
1188          * program accessing the database, every time the database is used.
1189          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1190          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1191          * @param[in] cmp A #MDB_cmp_func function
1192          * @return A non-zero error value on failure and 0 on success. Some possible
1193          * errors are:
1194          * <ul>
1195          *      <li>EINVAL - an invalid parameter was specified.
1196          * </ul>
1197          */
1198 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1199
1200         /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
1201          *
1202          * This comparison function is called whenever it is necessary to compare a data
1203          * item specified by the application with a data item currently stored in the database.
1204          * This function only takes effect if the database was opened with the #MDB_DUPSORT
1205          * flag.
1206          * If no comparison function is specified, and no special key flags were specified
1207          * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
1208          * before longer items.
1209          * @warning This function must be called before any data access functions are used,
1210          * otherwise data corruption may occur. The same comparison function must be used by every
1211          * program accessing the database, every time the database is used.
1212          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1213          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1214          * @param[in] cmp A #MDB_cmp_func function
1215          * @return A non-zero error value on failure and 0 on success. Some possible
1216          * errors are:
1217          * <ul>
1218          *      <li>EINVAL - an invalid parameter was specified.
1219          * </ul>
1220          */
1221 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1222
1223         /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
1224          *
1225          * @todo The relocation function is called whenever it is necessary to move the data
1226          * of an item to a different position in the database (e.g. through tree
1227          * balancing operations, shifts as a result of adds or deletes, etc.). It is
1228          * intended to allow address/position-dependent data items to be stored in
1229          * a database in an environment opened with the #MDB_FIXEDMAP option.
1230          * Currently the relocation feature is unimplemented and setting
1231          * this function has no effect.
1232          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1233          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1234          * @param[in] rel A #MDB_rel_func function
1235          * @return A non-zero error value on failure and 0 on success. Some possible
1236          * errors are:
1237          * <ul>
1238          *      <li>EINVAL - an invalid parameter was specified.
1239          * </ul>
1240          */
1241 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
1242
1243         /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
1244          *
1245          * See #mdb_set_relfunc and #MDB_rel_func for more details.
1246          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1247          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1248          * @param[in] ctx An arbitrary pointer for whatever the application needs.
1249          * It will be passed to the callback function set by #mdb_set_relfunc
1250          * as its \b relctx parameter whenever the callback is invoked.
1251          * @return A non-zero error value on failure and 0 on success. Some possible
1252          * errors are:
1253          * <ul>
1254          *      <li>EINVAL - an invalid parameter was specified.
1255          * </ul>
1256          */
1257 int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
1258
1259         /** @brief Get items from a database.
1260          *
1261          * This function retrieves key/data pairs from the database. The address
1262          * and length of the data associated with the specified \b key are returned
1263          * in the structure to which \b data refers.
1264          * If the database supports duplicate keys (#MDB_DUPSORT) then the
1265          * first data item for the key will be returned. Retrieval of other
1266          * items requires the use of #mdb_cursor_get().
1267          *
1268          * @note The memory pointed to by the returned values is owned by the
1269          * database. The caller need not dispose of the memory, and may not
1270          * modify it in any way. For values returned in a read-only transaction
1271          * any modification attempts will cause a SIGSEGV.
1272          * @note Values returned from the database are valid only until a
1273          * subsequent update operation, or the end of the transaction.
1274          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1275          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1276          * @param[in] key The key to search for in the database
1277          * @param[out] data The data corresponding to the key
1278          * @return A non-zero error value on failure and 0 on success. Some possible
1279          * errors are:
1280          * <ul>
1281          *      <li>#MDB_NOTFOUND - the key was not in the database.
1282          *      <li>EINVAL - an invalid parameter was specified.
1283          * </ul>
1284          */
1285 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1286
1287         /** @brief Store items into a database.
1288          *
1289          * This function stores key/data pairs in the database. The default behavior
1290          * is to enter the new key/data pair, replacing any previously existing key
1291          * if duplicates are disallowed, or adding a duplicate data item if
1292          * duplicates are allowed (#MDB_DUPSORT).
1293          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1294          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1295          * @param[in] key The key to store in the database
1296          * @param[in,out] data The data to store
1297          * @param[in] flags Special options for this operation. This parameter
1298          * must be set to 0 or by bitwise OR'ing together one or more of the
1299          * values described here.
1300          * <ul>
1301          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1302          *              already appear in the database. This flag may only be specified
1303          *              if the database was opened with #MDB_DUPSORT. The function will
1304          *              return #MDB_KEYEXIST if the key/data pair already appears in the
1305          *              database.
1306          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1307          *              does not already appear in the database. The function will return
1308          *              #MDB_KEYEXIST if the key already appears in the database, even if
1309          *              the database supports duplicates (#MDB_DUPSORT). The \b data
1310          *              parameter will be set to point to the existing item.
1311          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
1312          *              don't copy the given data. Instead, return a pointer to the
1313          *              reserved space, which the caller can fill in later - before
1314          *              the next update operation or the transaction ends. This saves
1315          *              an extra memcpy if the data is being generated later.
1316          *              LMDB does nothing else with this memory, the caller is expected
1317          *              to modify all of the space requested. This flag must not be
1318          *              specified if the database was opened with #MDB_DUPSORT.
1319          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1320          *              database. This option allows fast bulk loading when keys are
1321          *              already known to be in the correct order. Loading unsorted keys
1322          *              with this flag will cause a #MDB_KEYEXIST error.
1323          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1324          * </ul>
1325          * @return A non-zero error value on failure and 0 on success. Some possible
1326          * errors are:
1327          * <ul>
1328          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1329          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1330          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1331          *      <li>EINVAL - an invalid parameter was specified.
1332          * </ul>
1333          */
1334 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1335                             unsigned int flags);
1336
1337         /** @brief Delete items from a database.
1338          *
1339          * This function removes key/data pairs from the database.
1340          * If the database does not support sorted duplicate data items
1341          * (#MDB_DUPSORT) the data parameter is ignored.
1342          * If the database supports sorted duplicates and the data parameter
1343          * is NULL, all of the duplicate data items for the key will be
1344          * deleted. Otherwise, if the data parameter is non-NULL
1345          * only the matching data item will be deleted.
1346          * This function will return #MDB_NOTFOUND if the specified key/data
1347          * pair is not in the database.
1348          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1349          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1350          * @param[in] key The key to delete from the database
1351          * @param[in] data The data to delete
1352          * @return A non-zero error value on failure and 0 on success. Some possible
1353          * errors are:
1354          * <ul>
1355          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1356          *      <li>EINVAL - an invalid parameter was specified.
1357          * </ul>
1358          */
1359 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1360
1361         /** @brief Create a cursor handle.
1362          *
1363          * A cursor is associated with a specific transaction and database.
1364          * A cursor cannot be used when its database handle is closed.  Nor
1365          * when its transaction has ended, except with #mdb_cursor_renew().
1366          * It can be discarded with #mdb_cursor_close().
1367          * A cursor in a write-transaction can be closed before its transaction
1368          * ends, and will otherwise be closed when its transaction ends.
1369          * A cursor in a read-only transaction must be closed explicitly, before
1370          * or after its transaction ends. It can be reused with
1371          * #mdb_cursor_renew() before finally closing it.
1372          * @note Earlier documentation said that cursors in every transaction
1373          * were closed when the transaction committed or aborted.
1374          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1375          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1376          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1377          * @return A non-zero error value on failure and 0 on success. Some possible
1378          * errors are:
1379          * <ul>
1380          *      <li>EINVAL - an invalid parameter was specified.
1381          * </ul>
1382          */
1383 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1384
1385         /** @brief Close a cursor handle.
1386          *
1387          * The cursor handle will be freed and must not be used again after this call.
1388          * Its transaction must still be live if it is a write-transaction.
1389          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1390          */
1391 void mdb_cursor_close(MDB_cursor *cursor);
1392
1393         /** @brief Renew a cursor handle.
1394          *
1395          * A cursor is associated with a specific transaction and database.
1396          * Cursors that are only used in read-only
1397          * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1398          * The cursor may be associated with a new read-only transaction, and
1399          * referencing the same database handle as it was created with.
1400          * This may be done whether the previous transaction is live or dead.
1401          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1402          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1403          * @return A non-zero error value on failure and 0 on success. Some possible
1404          * errors are:
1405          * <ul>
1406          *      <li>EINVAL - an invalid parameter was specified.
1407          * </ul>
1408          */
1409 int  mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1410
1411         /** @brief Return the cursor's transaction handle.
1412          *
1413          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1414          */
1415 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1416
1417         /** @brief Return the cursor's database handle.
1418          *
1419          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1420          */
1421 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1422
1423         /** @brief Retrieve by cursor.
1424          *
1425          * This function retrieves key/data pairs from the database. The address and length
1426          * of the key are returned in the object to which \b key refers (except for the
1427          * case of the #MDB_SET option, in which the \b key object is unchanged), and
1428          * the address and length of the data are returned in the object to which \b data
1429          * refers.
1430          * See #mdb_get() for restrictions on using the output values.
1431          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1432          * @param[in,out] key The key for a retrieved item
1433          * @param[in,out] data The data of a retrieved item
1434          * @param[in] op A cursor operation #MDB_cursor_op
1435          * @return A non-zero error value on failure and 0 on success. Some possible
1436          * errors are:
1437          * <ul>
1438          *      <li>#MDB_NOTFOUND - no matching key found.
1439          *      <li>EINVAL - an invalid parameter was specified.
1440          * </ul>
1441          */
1442 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1443                             MDB_cursor_op op);
1444
1445         /** @brief Store by cursor.
1446          *
1447          * This function stores key/data pairs into the database.
1448          * The cursor is positioned at the new item, or on failure usually near it.
1449          * @note Earlier documentation incorrectly said errors would leave the
1450          * state of the cursor unchanged.
1451          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1452          * @param[in] key The key operated on.
1453          * @param[in] data The data operated on.
1454          * @param[in] flags Options for this operation. This parameter
1455          * must be set to 0 or one of the values described here.
1456          * <ul>
1457          *      <li>#MDB_CURRENT - replace the item at the current cursor position.
1458          *              The \b key parameter must still be provided, and must match it.
1459          *              If using sorted duplicates (#MDB_DUPSORT) the data item must still
1460          *              sort into the same place. This is intended to be used when the
1461          *              new data is the same size as the old. Otherwise it will simply
1462          *              perform a delete of the old record followed by an insert.
1463          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1464          *              already appear in the database. This flag may only be specified
1465          *              if the database was opened with #MDB_DUPSORT. The function will
1466          *              return #MDB_KEYEXIST if the key/data pair already appears in the
1467          *              database.
1468          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1469          *              does not already appear in the database. The function will return
1470          *              #MDB_KEYEXIST if the key already appears in the database, even if
1471          *              the database supports duplicates (#MDB_DUPSORT).
1472          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
1473          *              don't copy the given data. Instead, return a pointer to the
1474          *              reserved space, which the caller can fill in later - before
1475          *              the next update operation or the transaction ends. This saves
1476          *              an extra memcpy if the data is being generated later. This flag
1477          *              must not be specified if the database was opened with #MDB_DUPSORT.
1478          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1479          *              database. No key comparisons are performed. This option allows
1480          *              fast bulk loading when keys are already known to be in the
1481          *              correct order. Loading unsorted keys with this flag will cause
1482          *              a #MDB_KEYEXIST error.
1483          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1484          *      <li>#MDB_MULTIPLE - store multiple contiguous data elements in a
1485          *              single request. This flag may only be specified if the database
1486          *              was opened with #MDB_DUPFIXED. The \b data argument must be an
1487          *              array of two MDB_vals. The mv_size of the first MDB_val must be
1488          *              the size of a single data element. The mv_data of the first MDB_val
1489          *              must point to the beginning of the array of contiguous data elements.
1490          *              The mv_size of the second MDB_val must be the count of the number
1491          *              of data elements to store. On return this field will be set to
1492          *              the count of the number of elements actually written. The mv_data
1493          *              of the second MDB_val is unused.
1494          * </ul>
1495          * @return A non-zero error value on failure and 0 on success. Some possible
1496          * errors are:
1497          * <ul>
1498          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1499          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1500          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1501          *      <li>EINVAL - an invalid parameter was specified.
1502          * </ul>
1503          */
1504 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1505                                 unsigned int flags);
1506
1507         /** @brief Delete current key/data pair
1508          *
1509          * This function deletes the key/data pair to which the cursor refers.
1510          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1511          * @param[in] flags Options for this operation. This parameter
1512          * must be set to 0 or one of the values described here.
1513          * <ul>
1514          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1515          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
1516          * </ul>
1517          * @return A non-zero error value on failure and 0 on success. Some possible
1518          * errors are:
1519          * <ul>
1520          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1521          *      <li>EINVAL - an invalid parameter was specified.
1522          * </ul>
1523          */
1524 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1525
1526         /** @brief Return count of duplicates for current key.
1527          *
1528          * This call is only valid on databases that support sorted duplicate
1529          * data items #MDB_DUPSORT.
1530          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1531          * @param[out] countp Address where the count will be stored
1532          * @return A non-zero error value on failure and 0 on success. Some possible
1533          * errors are:
1534          * <ul>
1535          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1536          * </ul>
1537          */
1538 int  mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1539
1540         /** @brief Compare two data items according to a particular database.
1541          *
1542          * This returns a comparison as if the two data items were keys in the
1543          * specified database.
1544          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1545          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1546          * @param[in] a The first item to compare
1547          * @param[in] b The second item to compare
1548          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1549          */
1550 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1551
1552         /** @brief Compare two data items according to a particular database.
1553          *
1554          * This returns a comparison as if the two items were data items of
1555          * the specified database. The database must have the #MDB_DUPSORT flag.
1556          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1557          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1558          * @param[in] a The first item to compare
1559          * @param[in] b The second item to compare
1560          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1561          */
1562 int  mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1563
1564         /** @brief A callback function used to print a message from the library.
1565          *
1566          * @param[in] msg The string to be printed.
1567          * @param[in] ctx An arbitrary context pointer for the callback.
1568          * @return < 0 on failure, >= 0 on success.
1569          */
1570 typedef int (MDB_msg_func)(const char *msg, void *ctx);
1571
1572         /** @brief Dump the entries in the reader lock table.
1573          *
1574          * @param[in] env An environment handle returned by #mdb_env_create()
1575          * @param[in] func A #MDB_msg_func function
1576          * @param[in] ctx Anything the message function needs
1577          * @return < 0 on failure, >= 0 on success.
1578          */
1579 int     mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
1580
1581         /** @brief Check for stale entries in the reader lock table.
1582          *
1583          * @param[in] env An environment handle returned by #mdb_env_create()
1584          * @param[out] dead Number of stale slots that were cleared
1585          * @return 0 on success, non-zero on failure.
1586          */
1587 int     mdb_reader_check(MDB_env *env, int *dead);
1588 /**     @} */
1589
1590 #ifdef __cplusplus
1591 }
1592 #endif
1593 /** @page tools LMDB Command Line Tools
1594         The following describes the command line tools that are available for LMDB.
1595         \li \ref mdb_copy_1
1596         \li \ref mdb_dump_1
1597         \li \ref mdb_load_1
1598         \li \ref mdb_stat_1
1599 */
1600
1601 #endif /* _LMDB_H_ */