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