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