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