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