]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/lmdb.h
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / libraries / liblmdb / lmdb.h
1 /** @file lmdb.h
2  *      @brief Lightning memory-mapped database library
3  *
4  *      @mainpage       Lightning Memory-Mapped Database Manager (MDB)
5  *
6  *      @section intro_sec Introduction
7  *      MDB is a Btree-based database management library modeled loosely on the
8  *      BerkeleyDB API, but much simplified. The entire database is exposed
9  *      in a memory map, and all data fetches return data directly
10  *      from the mapped memory, so no malloc's or memcpy's occur during
11  *      data fetches. As such, the library is extremely simple because it
12  *      requires no page caching layer of its own, and it is extremely high
13  *      performance and memory-efficient. It is also fully transactional with
14  *      full ACID semantics, and when the memory map is read-only, the
15  *      database integrity cannot be corrupted by stray pointer writes from
16  *      application code.
17  *
18  *      The library is fully thread-aware and supports concurrent read/write
19  *      access from multiple processes and threads. Data pages use a copy-on-
20  *      write strategy so no active data pages are ever overwritten, which
21  *      also provides resistance to corruption and eliminates the need of any
22  *      special recovery procedures after a system crash. Writes are fully
23  *      serialized; only one write transaction may be active at a time, which
24  *      guarantees that writers can never deadlock. The database structure is
25  *      multi-versioned so readers run with no locks; writers cannot block
26  *      readers, and readers don't block writers.
27  *
28  *      Unlike other well-known database mechanisms which use either write-ahead
29  *      transaction logs or append-only data writes, MDB requires no maintenance
30  *      during operation. Both write-ahead loggers and append-only databases
31  *      require periodic checkpointing and/or compaction of their log or database
32  *      files otherwise they grow without bound. MDB tracks free pages within
33  *      the database and re-uses them for new write operations, so the database
34  *      size does not grow without bound in normal use.
35  *
36  *      The memory map can be used as a read-only or read-write map. It is
37  *      read-only by default as this provides total immunity to corruption.
38  *      Using read-write mode offers much higher write performance, but adds
39  *      the possibility for stray application writes thru pointers to silently
40  *      corrupt the database. Of course if your application code is known to
41  *      be bug-free (...) then this is not an issue.
42  *
43  *      @section caveats_sec Caveats
44  *      Troubleshooting the lock file, plus semaphores on BSD systems:
45  *
46  *      - A broken lockfile can cause sync issues.
47  *        Stale reader transactions left behind by an aborted program
48  *        cause further writes to grow the database quickly, and
49  *        stale locks can block further operation.
50  *
51  *        Fix: Terminate all programs using the database, or make
52  *        them close it.  Next database user will reset the lockfile.
53  *
54  *      - On BSD systems or others configured with MDB_USE_POSIX_SEM,
55  *        startup can fail due to semaphores owned by another userid.
56  *
57  *        Fix: Open and close the database as the user which owns the
58  *        semaphores (likely last user) or as root, while no other
59  *        process is using the database.
60  *
61  *      Restrictions/caveats (in addition to those listed for some functions):
62  *
63  *      - Only the database owner should normally use the database on
64  *        BSD systems or when otherwise configured with MDB_USE_POSIX_SEM.
65  *        Multiple users can cause startup to fail later, as noted above.
66  *
67  *      - A thread can only use one transaction at a time, plus any child
68  *        transactions.  Each transaction belongs to one thread.  See below.
69  *
70  *      - Use an MDB_env* in the process which opened it, without fork()ing.
71  *
72  *      - Do not have open an MDB database twice in the same process at
73  *        the same time.  Not even from a plain open() call - close()ing it
74  *        breaks flock() advisory locking.
75  *
76  *      - Avoid long-lived transactions.  Read transactions prevent
77  *        reuse of pages freed by newer write transactions, thus the
78  *        database can grow quickly.  Write transactions prevent
79  *        other write transactions, since writes are serialized.
80  *
81  *      - Avoid suspending a process with active transactions.  These
82  *        would then be "long-lived" as above.  Also read transactions
83  *        suspended when writers commit could sometimes see wrong data.
84  *
85  *      ...when several processes can use a database concurrently:
86  *
87  *      - Avoid aborting a process with an active transaction.
88  *        The transaction becomes "long-lived" as above until the lockfile
89  *        is reset, since the process may not remove it from the lockfile.
90  *
91  *      - If you do that anyway, close the environment once in a while,
92  *        so the lockfile can get reset.
93  *
94  *      - Do not use MDB databases on remote filesystems, even between
95  *        processes on the same host.  This breaks flock() on some OSes,
96  *        possibly memory map sync, and certainly sync between programs
97  *        on different hosts.
98  *
99  *      - Opening a database can fail if another process is opening or
100  *        closing it at exactly the same time.
101  *
102  *      @author Howard Chu, Symas Corporation.
103  *
104  *      @copyright Copyright 2011-2013 Howard Chu, Symas Corp. All rights reserved.
105  *
106  * Redistribution and use in source and binary forms, with or without
107  * modification, are permitted only as authorized by the OpenLDAP
108  * Public License.
109  *
110  * A copy of this license is available in the file LICENSE in the
111  * top-level directory of the distribution or, alternatively, at
112  * <http://www.OpenLDAP.org/license.html>.
113  *
114  *      @par Derived From:
115  * This code is derived from btree.c written by Martin Hedenfalk.
116  *
117  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
118  *
119  * Permission to use, copy, modify, and distribute this software for any
120  * purpose with or without fee is hereby granted, provided that the above
121  * copyright notice and this permission notice appear in all copies.
122  *
123  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
124  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
125  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
126  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
127  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
128  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
129  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
130  */
131 #ifndef _LMDB_H_
132 #define _LMDB_H_
133
134 #include <sys/types.h>
135
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139
140 #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          * The old database handle is returned if the database was already open.
771          * The handle must only be closed once.
772          * The database handle will be private to the current transaction until
773          * the transaction is successfully committed. If the transaction is
774          * aborted the handle will be closed automatically.
775          * After a successful commit the
776          * handle will reside in the shared environment, and may be used
777          * by other transactions. This function must not be called from
778          * multiple concurrent transactions. A transaction that uses this function
779          * must finish (either commit or abort) before any other transaction may
780          * use this function.
781          *
782          * To use named databases (with name != NULL), #mdb_env_set_maxdbs()
783          * must be called before opening the environment.
784          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
785          * @param[in] name The name of the database to open. If only a single
786          *      database is needed in the environment, this value may be NULL.
787          * @param[in] flags Special options for this database. This parameter
788          * must be set to 0 or by bitwise OR'ing together one or more of the
789          * values described here.
790          * <ul>
791          *      <li>#MDB_REVERSEKEY
792          *              Keys are strings to be compared in reverse order, from the end
793          *              of the strings to the beginning. By default, Keys are treated as strings and
794          *              compared from beginning to end.
795          *      <li>#MDB_DUPSORT
796          *              Duplicate keys may be used in the database. (Or, from another perspective,
797          *              keys may have multiple data items, stored in sorted order.) By default
798          *              keys must be unique and may have only a single data item.
799          *      <li>#MDB_INTEGERKEY
800          *              Keys are binary integers in native byte order. Setting this option
801          *              requires all keys to be the same size, typically sizeof(int)
802          *              or sizeof(size_t).
803          *      <li>#MDB_DUPFIXED
804          *              This flag may only be used in combination with #MDB_DUPSORT. This option
805          *              tells the library that the data items for this database are all the same
806          *              size, which allows further optimizations in storage and retrieval. When
807          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
808          *              cursor operations may be used to retrieve multiple items at once.
809          *      <li>#MDB_INTEGERDUP
810          *              This option specifies that duplicate data items are also integers, and
811          *              should be sorted as such.
812          *      <li>#MDB_REVERSEDUP
813          *              This option specifies that duplicate data items should be compared as
814          *              strings in reverse order.
815          *      <li>#MDB_CREATE
816          *              Create the named database if it doesn't exist. This option is not
817          *              allowed in a read-only transaction or a read-only environment.
818          * </ul>
819          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
820          * @return A non-zero error value on failure and 0 on success. Some possible
821          * errors are:
822          * <ul>
823          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
824          *              and #MDB_CREATE was not specified.
825          *      <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs().
826          * </ul>
827          */
828 int  mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
829
830         /** @brief Retrieve statistics for a database.
831          *
832          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
833          * @param[in] dbi A database handle returned by #mdb_dbi_open()
834          * @param[out] stat The address of an #MDB_stat structure
835          *      where the statistics will be copied
836          * @return A non-zero error value on failure and 0 on success. Some possible
837          * errors are:
838          * <ul>
839          *      <li>EINVAL - an invalid parameter was specified.
840          * </ul>
841          */
842 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
843
844         /** @brief Close a database handle.
845          *
846          * This call is not mutex protected. Handles should only be closed by
847          * a single thread, and only if no other threads are going to reference
848          * the database handle or one of its cursors any further. Do not close
849          * a handle if an existing transaction has modified its database.
850          * @param[in] env An environment handle returned by #mdb_env_create()
851          * @param[in] dbi A database handle returned by #mdb_dbi_open()
852          */
853 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
854
855         /** @brief Delete a database and/or free all its pages.
856          *
857          * If the \b del parameter is 1, the DB handle will be closed
858          * and the DB will be deleted.
859          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
860          * @param[in] dbi A database handle returned by #mdb_dbi_open()
861          * @param[in] del 1 to delete the DB from the environment,
862          * 0 to just free its pages.
863          * @return A non-zero error value on failure and 0 on success.
864          */
865 int  mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
866
867         /** @brief Set a custom key comparison function for a database.
868          *
869          * The comparison function is called whenever it is necessary to compare a
870          * key specified by the application with a key currently stored in the database.
871          * If no comparison function is specified, and no special key flags were specified
872          * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating
873          * before longer keys.
874          * @warning This function must be called before any data access functions are used,
875          * otherwise data corruption may occur. The same comparison function must be used by every
876          * program accessing the database, every time the database is used.
877          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
878          * @param[in] dbi A database handle returned by #mdb_dbi_open()
879          * @param[in] cmp A #MDB_cmp_func function
880          * @return A non-zero error value on failure and 0 on success. Some possible
881          * errors are:
882          * <ul>
883          *      <li>EINVAL - an invalid parameter was specified.
884          * </ul>
885          */
886 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
887
888         /** @brief Set a custom data comparison function for a #MDB_DUPSORT database.
889          *
890          * This comparison function is called whenever it is necessary to compare a data
891          * item specified by the application with a data item currently stored in the database.
892          * This function only takes effect if the database was opened with the #MDB_DUPSORT
893          * flag.
894          * If no comparison function is specified, and no special key flags were specified
895          * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating
896          * before longer items.
897          * @warning This function must be called before any data access functions are used,
898          * otherwise data corruption may occur. The same comparison function must be used by every
899          * program accessing the database, every time the database is used.
900          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
901          * @param[in] dbi A database handle returned by #mdb_dbi_open()
902          * @param[in] cmp A #MDB_cmp_func function
903          * @return A non-zero error value on failure and 0 on success. Some possible
904          * errors are:
905          * <ul>
906          *      <li>EINVAL - an invalid parameter was specified.
907          * </ul>
908          */
909 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
910
911         /** @brief Set a relocation function for a #MDB_FIXEDMAP database.
912          *
913          * @todo The relocation function is called whenever it is necessary to move the data
914          * of an item to a different position in the database (e.g. through tree
915          * balancing operations, shifts as a result of adds or deletes, etc.). It is
916          * intended to allow address/position-dependent data items to be stored in
917          * a database in an environment opened with the #MDB_FIXEDMAP option.
918          * Currently the relocation feature is unimplemented and setting
919          * this function has no effect.
920          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
921          * @param[in] dbi A database handle returned by #mdb_dbi_open()
922          * @param[in] rel A #MDB_rel_func function
923          * @return A non-zero error value on failure and 0 on success. Some possible
924          * errors are:
925          * <ul>
926          *      <li>EINVAL - an invalid parameter was specified.
927          * </ul>
928          */
929 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
930
931         /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
932          *
933          * See #mdb_set_relfunc and #MDB_rel_func for more details.
934          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
935          * @param[in] dbi A database handle returned by #mdb_dbi_open()
936          * @param[in] ctx An arbitrary pointer for whatever the application needs.
937          * It will be passed to the callback function set by #mdb_set_relfunc
938          * as its \b relctx parameter whenever the callback is invoked.
939          * @return A non-zero error value on failure and 0 on success. Some possible
940          * errors are:
941          * <ul>
942          *      <li>EINVAL - an invalid parameter was specified.
943          * </ul>
944          */
945 int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
946
947         /** @brief Get items from a database.
948          *
949          * This function retrieves key/data pairs from the database. The address
950          * and length of the data associated with the specified \b key are returned
951          * in the structure to which \b data refers.
952          * If the database supports duplicate keys (#MDB_DUPSORT) then the
953          * first data item for the key will be returned. Retrieval of other
954          * items requires the use of #mdb_cursor_get().
955          *
956          * @note The memory pointed to by the returned values is owned by the
957          * database. The caller need not dispose of the memory, and may not
958          * modify it in any way. For values returned in a read-only transaction
959          * any modification attempts will cause a SIGSEGV.
960          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
961          * @param[in] dbi A database handle returned by #mdb_dbi_open()
962          * @param[in] key The key to search for in the database
963          * @param[out] data The data corresponding to the key
964          * @return A non-zero error value on failure and 0 on success. Some possible
965          * errors are:
966          * <ul>
967          *      <li>#MDB_NOTFOUND - the key was not in the database.
968          *      <li>EINVAL - an invalid parameter was specified.
969          * </ul>
970          */
971 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
972
973         /** @brief Store items into a database.
974          *
975          * This function stores key/data pairs in the database. The default behavior
976          * is to enter the new key/data pair, replacing any previously existing key
977          * if duplicates are disallowed, or adding a duplicate data item if
978          * duplicates are allowed (#MDB_DUPSORT).
979          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
980          * @param[in] dbi A database handle returned by #mdb_dbi_open()
981          * @param[in] key The key to store in the database
982          * @param[in,out] data The data to store
983          * @param[in] flags Special options for this operation. This parameter
984          * must be set to 0 or by bitwise OR'ing together one or more of the
985          * values described here.
986          * <ul>
987          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
988          *              already appear in the database. This flag may only be specified
989          *              if the database was opened with #MDB_DUPSORT. The function will
990          *              return #MDB_KEYEXIST if the key/data pair already appears in the
991          *              database.
992          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
993          *              does not already appear in the database. The function will return
994          *              #MDB_KEYEXIST if the key already appears in the database, even if
995          *              the database supports duplicates (#MDB_DUPSORT). The \b data
996          *              parameter will be set to point to the existing item.
997          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
998          *              don't copy the given data. Instead, return a pointer to the
999          *              reserved space, which the caller can fill in later. This saves
1000          *              an extra memcpy if the data is being generated later.
1001          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1002          *              database. No key comparisons are performed. This option allows
1003          *              fast bulk loading when keys are already known to be in the
1004          *              correct order. Loading unsorted keys with this flag will cause
1005          *              data corruption.
1006          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1007          * </ul>
1008          * @return A non-zero error value on failure and 0 on success. Some possible
1009          * errors are:
1010          * <ul>
1011          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1012          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1013          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1014          *      <li>EINVAL - an invalid parameter was specified.
1015          * </ul>
1016          */
1017 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1018                             unsigned int flags);
1019
1020         /** @brief Delete items from a database.
1021          *
1022          * This function removes key/data pairs from the database.
1023          * If the database does not support sorted duplicate data items
1024          * (#MDB_DUPSORT) the data parameter is ignored.
1025          * If the database supports sorted duplicates and the data parameter
1026          * is NULL, all of the duplicate data items for the key will be
1027          * deleted. Otherwise, if the data parameter is non-NULL
1028          * only the matching data item will be deleted.
1029          * This function will return #MDB_NOTFOUND if the specified key/data
1030          * pair is not in the database.
1031          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1032          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1033          * @param[in] key The key to delete from the database
1034          * @param[in] data The data to delete
1035          * @return A non-zero error value on failure and 0 on success. Some possible
1036          * errors are:
1037          * <ul>
1038          *      <li>EACCES - an attempt was made to write in a read-only transaction.
1039          *      <li>EINVAL - an invalid parameter was specified.
1040          * </ul>
1041          */
1042 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1043
1044         /** @brief Create a cursor handle.
1045          *
1046          * Cursors are associated with a specific transaction and database and
1047          * may not span threads.
1048          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1049          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1050          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
1051          * @return A non-zero error value on failure and 0 on success. Some possible
1052          * errors are:
1053          * <ul>
1054          *      <li>EINVAL - an invalid parameter was specified.
1055          * </ul>
1056          */
1057 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1058
1059         /** @brief Close a cursor handle.
1060          *
1061          * The cursor handle will be freed and must not be used again after this call.
1062          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1063          */
1064 void mdb_cursor_close(MDB_cursor *cursor);
1065
1066         /** @brief Renew a cursor handle.
1067          *
1068          * Cursors are associated with a specific transaction and database and
1069          * may not span threads. Cursors that are only used in read-only
1070          * transactions may be re-used, to avoid unnecessary malloc/free overhead.
1071          * The cursor may be associated with a new read-only transaction, and
1072          * referencing the same database handle as it was created with.
1073          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1074          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1075          * @return A non-zero error value on failure and 0 on success. Some possible
1076          * errors are:
1077          * <ul>
1078          *      <li>EINVAL - an invalid parameter was specified.
1079          * </ul>
1080          */
1081 int  mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1082
1083         /** @brief Return the cursor's transaction handle.
1084          *
1085          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1086          */
1087 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor);
1088
1089         /** @brief Return the cursor's database handle.
1090          *
1091          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1092          */
1093 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1094
1095         /** @brief Retrieve by cursor.
1096          *
1097          * This function retrieves key/data pairs from the database. The address and length
1098          * of the key are returned in the object to which \b key refers (except for the
1099          * case of the #MDB_SET option, in which the \b key object is unchanged), and
1100          * the address and length of the data are returned in the object to which \b data
1101          * refers.
1102          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1103          * @param[in,out] key The key for a retrieved item
1104          * @param[in,out] data The data of a retrieved item
1105          * @param[in] op A cursor operation #MDB_cursor_op
1106          * @return A non-zero error value on failure and 0 on success. Some possible
1107          * errors are:
1108          * <ul>
1109          *      <li>#MDB_NOTFOUND - no matching key found.
1110          *      <li>EINVAL - an invalid parameter was specified.
1111          * </ul>
1112          */
1113 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1114                             MDB_cursor_op op);
1115
1116         /** @brief Store by cursor.
1117          *
1118          * This function stores key/data pairs into the database.
1119          * If the function fails for any reason, the state of the cursor will be
1120          * unchanged. If the function succeeds and an item is inserted into the
1121          * database, the cursor is always positioned to refer to the newly inserted item.
1122          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1123          * @param[in] key The key operated on.
1124          * @param[in] data The data operated on.
1125          * @param[in] flags Options for this operation. This parameter
1126          * must be set to 0 or one of the values described here.
1127          * <ul>
1128          *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
1129          *              the cursor refers with the specified data item. The \b key
1130          *              parameter is ignored.
1131          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
1132          *              already appear in the database. This flag may only be specified
1133          *              if the database was opened with #MDB_DUPSORT. The function will
1134          *              return #MDB_KEYEXIST if the key/data pair already appears in the
1135          *              database.
1136          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
1137          *              does not already appear in the database. The function will return
1138          *              #MDB_KEYEXIST if the key already appears in the database, even if
1139          *              the database supports duplicates (#MDB_DUPSORT).
1140          *      <li>#MDB_RESERVE - reserve space for data of the given size, but
1141          *              don't copy the given data. Instead, return a pointer to the
1142          *              reserved space, which the caller can fill in later. This saves
1143          *              an extra memcpy if the data is being generated later.
1144          *      <li>#MDB_APPEND - append the given key/data pair to the end of the
1145          *              database. No key comparisons are performed. This option allows
1146          *              fast bulk loading when keys are already known to be in the
1147          *              correct order. Loading unsorted keys with this flag will cause
1148          *              data corruption.
1149          *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
1150          * </ul>
1151          * @return A non-zero error value on failure and 0 on success. Some possible
1152          * errors are:
1153          * <ul>
1154          *      <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize().
1155          *      <li>#MDB_TXN_FULL - the transaction has too many dirty pages.
1156          *      <li>EACCES - an attempt was made to modify a read-only database.
1157          *      <li>EINVAL - an invalid parameter was specified.
1158          * </ul>
1159          */
1160 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1161                                 unsigned int flags);
1162
1163         /** @brief Delete current key/data pair
1164          *
1165          * This function deletes the key/data pair to which the cursor refers.
1166          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1167          * @param[in] flags Options for this operation. This parameter
1168          * must be set to 0 or one of the values described here.
1169          * <ul>
1170          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
1171          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
1172          * </ul>
1173          * @return A non-zero error value on failure and 0 on success. Some possible
1174          * errors are:
1175          * <ul>
1176          *      <li>EACCES - an attempt was made to modify a read-only database.
1177          *      <li>EINVAL - an invalid parameter was specified.
1178          * </ul>
1179          */
1180 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1181
1182         /** @brief Return count of duplicates for current key.
1183          *
1184          * This call is only valid on databases that support sorted duplicate
1185          * data items #MDB_DUPSORT.
1186          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
1187          * @param[out] countp Address where the count will be stored
1188          * @return A non-zero error value on failure and 0 on success. Some possible
1189          * errors are:
1190          * <ul>
1191          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
1192          * </ul>
1193          */
1194 int  mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1195
1196         /** @brief Compare two data items according to a particular database.
1197          *
1198          * This returns a comparison as if the two data items were keys in the
1199          * specified database.
1200          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1201          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1202          * @param[in] a The first item to compare
1203          * @param[in] b The second item to compare
1204          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1205          */
1206 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1207
1208         /** @brief Compare two data items according to a particular database.
1209          *
1210          * This returns a comparison as if the two items were data items of
1211          * a sorted duplicates #MDB_DUPSORT database.
1212          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
1213          * @param[in] dbi A database handle returned by #mdb_dbi_open()
1214          * @param[in] a The first item to compare
1215          * @param[in] b The second item to compare
1216          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
1217          */
1218 int  mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1219 /**     @} */
1220
1221 #ifdef __cplusplus
1222 }
1223 #endif
1224 #endif /* _LMDB_H_ */