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