]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/lmdb.h
57c3f886f4396a725d3ae3b4ba4f075d69899337
[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 #define MDB_LAST_ERRCODE        MDB_INCOMPATIBLE
374 /** @} */
375
376 /** @brief Statistics for a database in the environment */
377 typedef struct MDB_stat {
378         unsigned int    ms_psize;                       /**< Size of a database page.
379                                                                                         This is currently the same for all databases. */
380         unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
381         size_t          ms_branch_pages;        /**< Number of internal (non-leaf) pages */
382         size_t          ms_leaf_pages;          /**< Number of leaf pages */
383         size_t          ms_overflow_pages;      /**< Number of overflow pages */
384         size_t          ms_entries;                     /**< Number of data items */
385 } MDB_stat;
386
387 /** @brief Information about the environment */
388 typedef struct MDB_envinfo {
389         void    *me_mapaddr;                    /**< Address of map, if fixed */
390         size_t  me_mapsize;                             /**< Size of the data memory map */
391         size_t  me_last_pgno;                   /**< ID of the last used page */
392         size_t  me_last_txnid;                  /**< ID of the last committed transaction */
393         unsigned int me_maxreaders;             /**< maximum number of threads for the environment */
394         unsigned int me_numreaders;             /**< maximum number of threads used in the environment */
395 } MDB_envinfo;
396
397         /** @brief Return the mdb library version information.
398          *
399          * @param[out] major if non-NULL, the library major version number is copied here
400          * @param[out] minor if non-NULL, the library minor version number is copied here
401          * @param[out] patch if non-NULL, the library patch version number is copied here
402          * @retval "version string" The library version as a string
403          */
404 char *mdb_version(int *major, int *minor, int *patch);
405
406         /** @brief Return a string describing a given error code.
407          *
408          * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
409          * function. If the error code is greater than or equal to 0, then the string
410          * returned by the system function strerror(3) is returned. If the error code
411          * is less than 0, an error string corresponding to the MDB library error is
412          * returned. See @ref errors for a list of MDB-specific error codes.
413          * @param[in] err The error code
414          * @retval "error message" The description of the error
415          */
416 char *mdb_strerror(int err);
417
418         /** @brief Create an MDB environment handle.
419          *
420          * This function allocates memory for a #MDB_env structure. To release
421          * the allocated memory and discard the handle, call #mdb_env_close().
422          * Before the handle may be used, it must be opened using #mdb_env_open().
423          * Various other options may also need to be set before opening the handle,
424          * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
425          * depending on usage requirements.
426          * @param[out] env The address where the new handle will be stored
427          * @return A non-zero error value on failure and 0 on success.
428          */
429 int  mdb_env_create(MDB_env **env);
430
431         /** @brief Open an environment handle.
432          *
433          * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
434          * @param[in] env An environment handle returned by #mdb_env_create()
435          * @param[in] path The directory in which the database files reside. This
436          * directory must already exist and be writable.
437          * @param[in] flags Special options for this environment. This parameter
438          * must be set to 0 or by bitwise OR'ing together one or more of the
439          * values described here.
440          * Flags set by mdb_env_set_flags() are also used.
441          * <ul>
442          *      <li>#MDB_FIXEDMAP
443          *      use a fixed address for the mmap region. This flag must be specified
444          *      when creating the environment, and is stored persistently in the environment.
445          *              If successful, the memory map will always reside at the same virtual address
446          *              and pointers used to reference data items in the database will be constant
447          *              across multiple invocations. This option may not always work, depending on
448          *              how the operating system has allocated memory to shared libraries and other uses.
449          *              The feature is highly experimental.
450          *      <li>#MDB_NOSUBDIR
451          *              By default, MDB creates its environment in a directory whose
452          *              pathname is given in \b path, and creates its data and lock files
453          *              under that directory. With this option, \b path is used as-is for
454          *              the database main data file. The database lock file is the \b path
455          *              with "-lock" appended.
456          *      <li>#MDB_RDONLY
457          *              Open the environment in read-only mode. No write operations will be
458          *              allowed. MDB will still modify the lock file - except on read-only
459          *              filesystems, where MDB does not use locks.
460          *      <li>#MDB_WRITEMAP
461          *              Use a writeable memory map unless MDB_RDONLY is set. This is faster
462          *              and uses fewer mallocs, but loses protection from application bugs
463          *              like wild pointer writes and other bad updates into the database.
464          *              Incompatible with nested transactions.
465          *      <li>#MDB_NOMETASYNC
466          *              Flush system buffers to disk only once per transaction, omit the
467          *              metadata flush. Defer that until the system flushes files to disk,
468          *              or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization
469          *              maintains database integrity, but a system crash may undo the last
470          *              committed transaction. I.e. it preserves the ACI (atomicity,
471          *              consistency, isolation) but not D (durability) database property.
472          *              This flag may be changed at any time using #mdb_env_set_flags().
473          *      <li>#MDB_NOSYNC
474          *              Don't flush system buffers to disk when committing a transaction.
475          *              This optimization means a system crash can corrupt the database or
476          *              lose the last transactions if buffers are not yet flushed to disk.
477          *              The risk is governed by how often the system flushes dirty buffers
478          *              to disk and how often #mdb_env_sync() is called.  However, if the
479          *              filesystem preserves write order and the #MDB_WRITEMAP flag is not
480          *              used, transactions exhibit ACI (atomicity, consistency, isolation)
481          *              properties and only lose D (durability).  I.e. database integrity
482          *              is maintained, but a system crash may undo the final transactions.
483          *              Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no
484          *              hint for when to write transactions to disk, unless #mdb_env_sync()
485          *              is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable.
486          *              This flag may be changed at any time using #mdb_env_set_flags().
487          *      <li>#MDB_MAPASYNC
488          *              When using #MDB_WRITEMAP, use asynchronous flushes to disk.
489          *              As with #MDB_NOSYNC, a system crash can then corrupt the
490          *              database or lose the last transactions. Calling #mdb_env_sync()
491          *              ensures on-disk database integrity until next commit.
492          *              This flag may be changed at any time using #mdb_env_set_flags().
493          * </ul>
494          * @param[in] mode The UNIX permissions to set on created files. This parameter
495          * is ignored on Windows.
496          * @return A non-zero error value on failure and 0 on success. Some possible
497          * errors are:
498          * <ul>
499          *      <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
500          *      version that created the database environment.
501          *      <li>#MDB_INVALID - the environment file headers are corrupted.
502          *      <li>ENOENT - the directory specified by the path parameter doesn't exist.
503          *      <li>EACCES - the user didn't have permission to access the environment files.
504          *      <li>EAGAIN - the environment was locked by another process.
505          * </ul>
506          */
507 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
508
509         /** @brief Copy an MDB environment to the specified path.
510          *
511          * This function may be used to make a backup of an existing environment.
512          * @param[in] env An environment handle returned by #mdb_env_create(). It
513          * must have already been opened successfully.
514          * @param[in] path The directory in which the copy will reside. This
515          * directory must already exist and be writable but must otherwise be
516          * empty.
517          * @return A non-zero error value on failure and 0 on success.
518          */
519 int  mdb_env_copy(MDB_env *env, const char *path);
520
521         /** @brief Return statistics about the MDB environment.
522          *
523          * @param[in] env An environment handle returned by #mdb_env_create()
524          * @param[out] stat The address of an #MDB_stat structure
525          *      where the statistics will be copied
526          */
527 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
528
529         /** @brief Return information about the MDB environment.
530          *
531          * @param[in] env An environment handle returned by #mdb_env_create()
532          * @param[out] stat The address of an #MDB_envinfo structure
533          *      where the information will be copied
534          */
535 int  mdb_env_info(MDB_env *env, MDB_envinfo *stat);
536
537         /** @brief Flush the data buffers to disk.
538          *
539          * Data is always written to disk when #mdb_txn_commit() is called,
540          * but the operating system may keep it buffered. MDB always flushes
541          * the OS buffers upon commit as well, unless the environment was
542          * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC.
543          * @param[in] env An environment handle returned by #mdb_env_create()
544          * @param[in] force If non-zero, force a synchronous flush.  Otherwise
545          *  if the environment has the #MDB_NOSYNC flag set the flushes
546          *      will be omitted, and with #MDB_MAPASYNC they will be asynchronous.
547          * @return A non-zero error value on failure and 0 on success. Some possible
548          * errors are:
549          * <ul>
550          *      <li>EINVAL - an invalid parameter was specified.
551          *      <li>EIO - an error occurred during synchronization.
552          * </ul>
553          */
554 int  mdb_env_sync(MDB_env *env, int force);
555
556         /** @brief Close the environment and release the memory map.
557          *
558          * Only a single thread may call this function. All transactions, databases,
559          * and cursors must already be closed before calling this function. Attempts to
560          * use any such handles after calling this function will cause a SIGSEGV.
561          * The environment handle will be freed and must not be used again after this call.
562          * @param[in] env An environment handle returned by #mdb_env_create()
563          */
564 void mdb_env_close(MDB_env *env);
565
566         /** @brief Set environment flags.
567          *
568          * This may be used to set some flags in addition to those from
569          * #mdb_env_open(), or to unset these flags.
570          * @param[in] env An environment handle returned by #mdb_env_create()
571          * @param[in] flags The flags to change, bitwise OR'ed together
572          * @param[in] onoff A non-zero value sets the flags, zero clears them.
573          * @return A non-zero error value on failure and 0 on success. Some possible
574          * errors are:
575          * <ul>
576          *      <li>EINVAL - an invalid parameter was specified.
577          * </ul>
578          */
579 int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
580
581         /** @brief Get environment flags.
582          *
583          * @param[in] env An environment handle returned by #mdb_env_create()
584          * @param[out] flags The address of an integer to store the flags
585          * @return A non-zero error value on failure and 0 on success. Some possible
586          * errors are:
587          * <ul>
588          *      <li>EINVAL - an invalid parameter was specified.
589          * </ul>
590          */
591 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
592
593         /** @brief Return the path that was used in #mdb_env_open().
594          *
595          * @param[in] env An environment handle returned by #mdb_env_create()
596          * @param[out] path Address of a string pointer to contain the path. This
597          * is the actual string in the environment, not a copy. It should not be
598          * altered in any way.
599          * @return A non-zero error value on failure and 0 on success. Some possible
600          * errors are:
601          * <ul>
602          *      <li>EINVAL - an invalid parameter was specified.
603          * </ul>
604          */
605 int  mdb_env_get_path(MDB_env *env, const char **path);
606
607         /** @brief Set the size of the memory map to use for this environment.
608          *
609          * The size should be a multiple of the OS page size. The default is
610          * 10485760 bytes. The size of the memory map is also the maximum size
611          * of the database. The value should be chosen as large as possible,
612          * to accommodate future growth of the database.
613          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
614          * The size may be changed by closing and reopening the environment.
615          * Any attempt to set a size smaller than the space already consumed
616          * by the environment will be silently changed to the current size of the used space.
617          * @param[in] env An environment handle returned by #mdb_env_create()
618          * @param[in] size The size in bytes
619          * @return A non-zero error value on failure and 0 on success. Some possible
620          * errors are:
621          * <ul>
622          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
623          * </ul>
624          */
625 int  mdb_env_set_mapsize(MDB_env *env, size_t size);
626
627         /** @brief Set the maximum number of threads for the environment.
628          *
629          * This defines the number of slots in the lock table that is used to track readers in the
630          * the environment. The default is 126.
631          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
632          * @param[in] env An environment handle returned by #mdb_env_create()
633          * @param[in] readers The maximum number of threads
634          * @return A non-zero error value on failure and 0 on success. Some possible
635          * errors are:
636          * <ul>
637          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
638          * </ul>
639          */
640 int  mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
641
642         /** @brief Get the maximum number of threads for the environment.
643          *
644          * @param[in] env An environment handle returned by #mdb_env_create()
645          * @param[out] readers Address of an integer to store the number of readers
646          * @return A non-zero error value on failure and 0 on success. Some possible
647          * errors are:
648          * <ul>
649          *      <li>EINVAL - an invalid parameter was specified.
650          * </ul>
651          */
652 int  mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
653
654         /** @brief Set the maximum number of named databases for the environment.
655          *
656          * This function is only needed if multiple databases will be used in the
657          * environment. Simpler applications that use the environment as a single
658          * unnamed database can ignore this option.
659          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
660          * @param[in] env An environment handle returned by #mdb_env_create()
661          * @param[in] dbs The maximum number of databases
662          * @return A non-zero error value on failure and 0 on success. Some possible
663          * errors are:
664          * <ul>
665          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
666          * </ul>
667          */
668 int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
669
670         /** @brief Create a transaction for use with the environment.
671          *
672          * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
673          * @note Transactions may not span threads; a transaction must only be used by a
674          * single thread. Also, a thread may only have a single transaction.
675          * @note Cursors may not span transactions; each cursor must be opened and closed
676          * within a single transaction.
677          * @param[in] env An environment handle returned by #mdb_env_create()
678          * @param[in] parent If this parameter is non-NULL, the new transaction
679          * will be a nested transaction, with the transaction indicated by \b parent
680          * as its parent. Transactions may be nested to any level. A parent
681          * transaction may not issue any other operations besides mdb_txn_begin,
682          * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
683          * @param[in] flags Special options for this transaction. This parameter
684          * must be set to 0 or by bitwise OR'ing together one or more of the
685          * values described here.
686          * <ul>
687          *      <li>#MDB_RDONLY
688          *              This transaction will not perform any write operations.
689          * </ul>
690          * @param[out] txn Address where the new #MDB_txn handle will be stored
691          * @return A non-zero error value on failure and 0 on success. Some possible
692          * errors are:
693          * <ul>
694          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
695 -        *              must be shut down.
696          *      <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
697          *              mapsize and the environment must be shut down.
698          *      <li>#MDB_READERS_FULL - a read-only transaction was requested and
699          *              the reader lock table is full. See #mdb_env_set_maxreaders().
700          *      <li>ENOMEM - out of memory.
701          * </ul>
702          */
703 int  mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
704
705         /** @brief Commit all the operations of a transaction into the database.
706          *
707          * All cursors opened within the transaction will be closed by this call. The cursors
708          * and transaction handle will be freed and must not be used again after this call.
709          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
710          * @return A non-zero error value on failure and 0 on success. Some possible
711          * errors are:
712          * <ul>
713          *      <li>EINVAL - an invalid parameter was specified.
714          *      <li>ENOSPC - no more disk space.
715          *      <li>EIO - a low-level I/O error occurred while writing.
716          *      <li>ENOMEM - out of memory.
717          * </ul>
718          */
719 int  mdb_txn_commit(MDB_txn *txn);
720
721         /** @brief Abandon all the operations of the transaction instead of saving them.
722          *
723          * All cursors opened within the transaction will be closed by this call. The cursors
724          * and transaction handle will be freed and must not be used again after this call.
725          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
726          */
727 void mdb_txn_abort(MDB_txn *txn);
728
729         /** @brief Reset a read-only transaction.
730          *
731          * This releases the current reader lock but doesn't free the
732          * transaction handle, allowing it to be used again later by #mdb_txn_renew().
733          * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
734          * allocation/deallocation overhead if a thread is going to start a new
735          * read-only transaction again soon.
736          * All cursors opened within the transaction must be closed before the transaction
737          * is reset.
738          * Reader locks generally don't interfere with writers, but they keep old
739          * versions of database pages allocated. Thus they prevent the old pages
740          * from being reused when writers commit new data, and so under heavy load
741          * the database size may grow much more rapidly than otherwise.
742          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
743          */
744 void mdb_txn_reset(MDB_txn *txn);
745
746         /** @brief Renew a read-only transaction.
747          *
748          * This acquires a new reader lock for a transaction handle that had been
749          * released by #mdb_txn_reset(). It must be called before a reset transaction
750          * may be used again.
751          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
752          * @return A non-zero error value on failure and 0 on success. Some possible
753          * errors are:
754          * <ul>
755          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
756          *              must be shut down.
757          *      <li>EINVAL - an invalid parameter was specified.
758          * </ul>
759          */
760 int  mdb_txn_renew(MDB_txn *txn);
761
762 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
763 #define mdb_open(txn,name,flags,dbi)    mdb_dbi_open(txn,name,flags,dbi)
764 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */
765 #define mdb_close(env,dbi)                              mdb_dbi_close(env,dbi)
766
767         /** @brief Open a database in the environment.
768          *
769          * The database handle may be discarded by calling #mdb_dbi_close().
770          * It denotes the name and parameters of a database, independently of
771          * whether such a database exists. It will not exist if the transaction
772          * which created it aborted, nor if another process deleted it. The
773          * database handle resides in the shared environment, it is not owned
774          * by the given transaction. Only one thread should call this function;
775          * it is not mutex-protected in a read-only transaction.
776          * Preexisting transactions, other than the current transaction and
777          * any parents, must not use the new handle. Nor must their children.
778          * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
779          * must be called before opening the environment.
780          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
781          * @param[in] name The name of the database to open. If only a single
782          *      database is needed in the environment, this value may be NULL.
783          * @param[in] flags Special options for this database. This parameter
784          * must be set to 0 or by bitwise OR'ing together one or more of the
785          * values described here.
786          * <ul>
787          *      <li>#MDB_REVERSEKEY
788          *              Keys are strings to be compared in reverse order, from the end
789          *              of the strings to the beginning. By default, Keys are treated as strings and
790          *              compared from beginning to end.
791          *      <li>#MDB_DUPSORT
792          *              Duplicate keys may be used in the database. (Or, from another perspective,
793          *              keys may have multiple data items, stored in sorted order.) By default
794          *              keys must be unique and may have only a single data item.
795          *      <li>#MDB_INTEGERKEY
796          *              Keys are binary integers in native byte order. Setting this option
797          *              requires all keys to be the same size, typically sizeof(int)
798          *              or sizeof(size_t).
799          *      <li>#MDB_DUPFIXED
800          *              This flag may only be used in combination with #MDB_DUPSORT. This option
801          *              tells the library that the data items for this database are all the same
802          *              size, which allows further optimizations in storage and retrieval. When
803          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
804          *              cursor operations may be used to retrieve multiple items at once.
805          *      <li>#MDB_INTEGERDUP
806          *              This option specifies that duplicate data items are also integers, and
807          *              should be sorted as such.
808          *      <li>#MDB_REVERSEDUP
809          *              This option specifies that duplicate data items should be compared as
810          *              strings in reverse order.
811          *      <li>#MDB_CREATE
812          *              Create the named database if it doesn't exist. This option is not
813          *              allowed in a read-only transaction or a read-only environment.
814          * </ul>
815          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
816          * @return A non-zero error value on failure and 0 on success. Some possible
817          * errors are:
818          * <ul>
819          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
820          *              and #MDB_CREATE was not specified.
821          *      <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
822          * </ul>
823          */
824 int  mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
825
826         /** @brief Retrieve statistics for a database.
827          *
828          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
829          * @param[in] dbi A database handle returned by #mdb_dbi_open()
830          * @param[out] stat The address of an #MDB_stat structure
831          *      where the statistics will be copied
832          * @return A non-zero error value on failure and 0 on success. Some possible
833          * errors are:
834          * <ul>
835          *      <li>EINVAL - an invalid parameter was specified.
836          * </ul>
837          */
838 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
839
840         /** @brief Close a database handle.
841          *
842          * This call is not mutex protected. Handles should only be closed by
843          * a single thread, and only if no other threads are going to reference
844          * the database handle or one of its cursors any further. Do not close
845          * a handle if an existing transaction has modified its database.
846          * @param[in] env An environment handle returned by #mdb_env_create()
847          * @param[in] dbi A database handle returned by #mdb_dbi_open()
848          */
849 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
850
851         /** @brief Delete a database and/or free all its pages.
852          *
853          * If the \b del parameter is 1, the DB handle will be closed
854          * and the DB will be deleted.
855          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
856          * @param[in] dbi A database handle returned by #mdb_dbi_open()
857          * @param[in] del 1 to delete the DB from the environment,
858          * 0 to just free its pages.
859          * @return A non-zero error value on failure and 0 on success.
860          */
861 int  mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
862
863         /** @brief Set a custom key comparison function for a database.
864          *
865          * The comparison function is called whenever it is necessary to compare a
866          * key specified by the application with a key currently stored in the database.
867          * If no comparison function is specified, and no special key flags were specified
868          * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
869          * before longer keys.
870          * @warning This function must be called before any data access functions are used,
871          * otherwise data corruption may occur. The same comparison function must be used by every
872          * program accessing the database, every time the database is used.
873          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
874          * @param[in] dbi A database handle returned by #mdb_dbi_open()
875          * @param[in] cmp A #MDB_cmp_func function
876          * @return A non-zero error value on failure and 0 on success. Some possible
877          * errors are:
878          * <ul>
879          *      <li>EINVAL - an invalid parameter was specified.
880          * </ul>
881          */
882 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
883
884         /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
885          *
886          * This comparison function is called whenever it is necessary to compare a data
887          * item specified by the application with a data item currently stored in the database.
888          * This function only takes effect if the database was opened with the #MDB_DUPSORT
889          * flag.
890          * If no comparison function is specified, and no special key flags were specified
891          * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
892          * before longer items.
893          * @warning This function must be called before any data access functions are used,
894          * otherwise data corruption may occur. The same comparison function must be used by every
895          * program accessing the database, every time the database is used.
896          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
897          * @param[in] dbi A database handle returned by #mdb_dbi_open()
898          * @param[in] cmp A #MDB_cmp_func function
899          * @return A non-zero error value on failure and 0 on success. Some possible
900          * errors are:
901          * <ul>
902          *      <li>EINVAL - an invalid parameter was specified.
903          * </ul>
904          */
905 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
906
907         /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
908          *
909          * @todo The relocation function is called whenever it is necessary to move the data
910          * of an item to a different position in the database (e.g. through tree
911          * balancing operations, shifts as a result of adds or deletes, etc.). It is
912          * intended to allow address/position-dependent data items to be stored in
913          * a database in an environment opened with the #MDB_FIXEDMAP option.
914          * Currently the relocation feature is unimplemented and setting
915          * this function has no effect.
916          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
917          * @param[in] dbi A database handle returned by #mdb_dbi_open()
918          * @param[in] rel A #MDB_rel_func function
919          * @return A non-zero error value on failure and 0 on success. Some possible
920          * errors are:
921          * <ul>
922          *      <li>EINVAL - an invalid parameter was specified.
923          * </ul>
924          */
925 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
926
927         /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
928          *
929          * See #mdb_set_relfunc and #MDB_rel_func for more details.
930          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
931          * @param[in] dbi A database handle returned by #mdb_dbi_open()
932          * @param[in] ctx An arbitrary pointer for whatever the application needs.
933          * It will be passed to the callback function set by #mdb_set_relfunc
934          * as its \b relctx parameter whenever the callback is invoked.
935          * @return A non-zero error value on failure and 0 on success. Some possible
936          * errors are:
937          * <ul>
938          *      <li>EINVAL - an invalid parameter was specified.
939          * </ul>
940          */
941 int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
942
943         /** @brief Get items from a database.
944          *
945          * This function retrieves key/data pairs from the database. The address
946          * and length of the data associated with the specified \b key are returned
947          * in the structure to which \b data refers.
948          * If the database supports duplicate keys (#MDB_DUPSORT) then the
949          * first data item for the key will be returned. Retrieval of other
950          * items requires the use of #mdb_cursor_get().
951          *
952          * @note The memory pointed to by the returned values is owned by the
953          * database. The caller need not dispose of the memory, and may not
954          * modify it in any way. For values returned in a read-only transaction
955          * any modification attempts will cause a SIGSEGV.
956          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
957          * @param[in] dbi A database handle returned by #mdb_dbi_open()
958          * @param[in] key The key to search for in the database
959          * @param[out] data The data corresponding to the key
960          * @return A non-zero error value on failure and 0 on success. Some possible
961          * errors are:
962          * <ul>
963          *      <li>#MDB_NOTFOUND - the key was not in the database.
964          *      <li>EINVAL - an invalid parameter was specified.
965          * </ul>
966          */
967 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
968
969         /** @brief Store items into a database.
970          *
971          * This function stores key/data pairs in the database. The default behavior
972          * is to enter the new key/data pair, replacing any previously existing key
973          * if duplicates are disallowed, or adding a duplicate data item if
974          * duplicates are allowed (#MDB_DUPSORT).
975          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
976          * @param[in] dbi A database handle returned by #mdb_dbi_open()
977          * @param[in] key The key to store in the database
978          * @param[in,out] data The data to store
979          * @param[in] flags Special options for this operation. This parameter
980          * must be set to 0 or by bitwise OR'ing together one or more of the
981          * values described here.
982          * <ul>
983          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
984          *              already appear in the database. This flag may only be specified
985          *              if the database was opened with #MDB_DUPSORT. The function will
986          *              return #MDB_KEYEXIST if the key/data pair already appears in the
987          *              database.
988          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
989          *              does not already appear in the database. The function will return
990          *              #MDB_KEYEXIST if the key already appears in the database, even if
991          *              the database supports duplicates (#MDB_DUPSORT). The \b data
992          *              parameter will be set to point to the existing item.
993          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
994          *              don't copy the given data. Instead, return a pointer to the
995          *              reserved space, which the caller can fill in later. This saves
996          *              an extra memcpy if the data is being generated later.
997          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
998          *              database. No key comparisons are performed. This option allows
999          *              fast bulk loading when keys are already known to be in the
1000          *              correct order. Loading unsorted keys with this flag will cause
1001          *              data corruption.
1002          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1003          * </ul>
1004          * @return A non-zero error value on failure and 0 on success. Some possible
1005          * errors are:
1006          * <ul>
1007          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1008          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1009          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1010          *      <li>EINVAL - an invalid parameter was specified.
1011          * </ul>
1012          */
1013 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1014                             unsigned int flags);
1015
1016         /** @brief Delete items from a database.
1017          *
1018          * This function removes key/data pairs from the database.
1019          * If the database does not support sorted duplicate data items
1020          * (#MDB_DUPSORT) the data parameter is ignored.
1021          * If the database supports sorted duplicates and the data parameter
1022          * is NULL, all of the duplicate data items for the key will be
1023          * deleted. Otherwise, if the data parameter is non-NULL
1024          * only the matching data item will be deleted.
1025          * This function will return #MDB_NOTFOUND if the specified key/data
1026          * pair is not in the database.
1027          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1028          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1029          * @param[in] key The key to delete from the database
1030          * @param[in] data The data to delete
1031          * @return A non-zero error value on failure and 0 on success. Some possible
1032          * errors are:
1033          * <ul>
1034          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1035          *      <li>EINVAL - an invalid parameter was specified.
1036          * </ul>
1037          */
1038 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1039
1040         /** @brief Create a cursor handle.
1041          *
1042          * Cursors are associated with a specific transaction and database and
1043          * may not span threads.
1044          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1045          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1046          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1047          * @return A non-zero error value on failure and 0 on success. Some possible
1048          * errors are:
1049          * <ul>
1050          *      <li>EINVAL - an invalid parameter was specified.
1051          * </ul>
1052          */
1053 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1054
1055         /** @brief Close a cursor handle.
1056          *
1057          * The cursor handle will be freed and must not be used again after this call.
1058          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1059          */
1060 void mdb_cursor_close(MDB_cursor *cursor);
1061
1062         /** @brief Renew a cursor handle.
1063          *
1064          * Cursors are associated with a specific transaction and database and
1065          * may not span threads. Cursors that are only used in read-only
1066          * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1067          * The cursor may be associated with a new read-only transaction, and
1068          * referencing the same database handle as it was created with.
1069          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1070          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1071          * @return A non-zero error value on failure and 0 on success. Some possible
1072          * errors are:
1073          * <ul>
1074          *      <li>EINVAL - an invalid parameter was specified.
1075          * </ul>
1076          */
1077 int  mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1078
1079         /** @brief Return the cursor's transaction handle.
1080          *
1081          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1082          */
1083 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1084
1085         /** @brief Return the cursor's database handle.
1086          *
1087          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1088          */
1089 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1090
1091         /** @brief Retrieve by cursor.
1092          *
1093          * This function retrieves key/data pairs from the database. The address and length
1094          * of the key are returned in the object to which \b key refers (except for the
1095          * case of the #MDB_SET option, in which the \b key object is unchanged), and
1096          * the address and length of the data are returned in the object to which \b data
1097          * refers.
1098          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1099          * @param[in,out] key The key for a retrieved item
1100          * @param[in,out] data The data of a retrieved item
1101          * @param[in] op A cursor operation #MDB_cursor_op
1102          * @return A non-zero error value on failure and 0 on success. Some possible
1103          * errors are:
1104          * <ul>
1105          *      <li>#MDB_NOTFOUND - no matching key found.
1106          *      <li>EINVAL - an invalid parameter was specified.
1107          * </ul>
1108          */
1109 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1110                             MDB_cursor_op op);
1111
1112         /** @brief Store by cursor.
1113          *
1114          * This function stores key/data pairs into the database.
1115          * If the function fails for any reason, the state of the cursor will be
1116          * unchanged. If the function succeeds and an item is inserted into the
1117          * database, the cursor is always positioned to refer to the newly inserted item.
1118          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1119          * @param[in] key The key operated on.
1120          * @param[in] data The data operated on.
1121          * @param[in] flags Options for this operation. This parameter
1122          * must be set to 0 or one of the values described here.
1123          * <ul>
1124          *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
1125          *              the cursor refers with the specified data item. The \b key
1126          *              parameter is ignored.
1127          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1128          *              already appear in the database. This flag may only be specified
1129          *              if the database was opened with #MDB_DUPSORT. The function will
1130          *              return #MDB_KEYEXIST if the key/data pair already appears in the
1131          *              database.
1132          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1133          *              does not already appear in the database. The function will return
1134          *              #MDB_KEYEXIST if the key already appears in the database, even if
1135          *              the database supports duplicates (#MDB_DUPSORT).
1136          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
1137          *              don't copy the given data. Instead, return a pointer to the
1138          *              reserved space, which the caller can fill in later. This saves
1139          *              an extra memcpy if the data is being generated later.
1140          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1141          *              database. No key comparisons are performed. This option allows
1142          *              fast bulk loading when keys are already known to be in the
1143          *              correct order. Loading unsorted keys with this flag will cause
1144          *              data corruption.
1145          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1146          * </ul>
1147          * @return A non-zero error value on failure and 0 on success. Some possible
1148          * errors are:
1149          * <ul>
1150          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1151          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1152          *      <li>EACCES - an attempt was made to modify a read-only database.
1153          *      <li>EINVAL - an invalid parameter was specified.
1154          * </ul>
1155          */
1156 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1157                                 unsigned int flags);
1158
1159         /** @brief Delete current key/data pair
1160          *
1161          * This function deletes the key/data pair to which the cursor refers.
1162          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1163          * @param[in] flags Options for this operation. This parameter
1164          * must be set to 0 or one of the values described here.
1165          * <ul>
1166          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1167          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
1168          * </ul>
1169          * @return A non-zero error value on failure and 0 on success. Some possible
1170          * errors are:
1171          * <ul>
1172          *      <li>EACCES - an attempt was made to modify a read-only database.
1173          *      <li>EINVAL - an invalid parameter was specified.
1174          * </ul>
1175          */
1176 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1177
1178         /** @brief Return count of duplicates for current key.
1179          *
1180          * This call is only valid on databases that support sorted duplicate
1181          * data items #MDB_DUPSORT.
1182          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1183          * @param[out] countp Address where the count will be stored
1184          * @return A non-zero error value on failure and 0 on success. Some possible
1185          * errors are:
1186          * <ul>
1187          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1188          * </ul>
1189          */
1190 int  mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1191
1192         /** @brief Compare two data items according to a particular database.
1193          *
1194          * This returns a comparison as if the two data items were keys in the
1195          * specified database.
1196          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1197          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1198          * @param[in] a The first item to compare
1199          * @param[in] b The second item to compare
1200          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1201          */
1202 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1203
1204         /** @brief Compare two data items according to a particular database.
1205          *
1206          * This returns a comparison as if the two items were data items of
1207          * a sorted duplicates #MDB_DUPSORT database.
1208          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1209          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1210          * @param[in] a The first item to compare
1211          * @param[in] b The second item to compare
1212          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1213          */
1214 int  mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1215 /**     @} */
1216
1217 #ifdef __cplusplus
1218 }
1219 #endif
1220 #endif /* _LMDB_H_ */