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