]> git.sur5r.net Git - openldap/blob - libraries/libmdb/mdb.h
Check some mdb integer type assumptions
[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 read-only 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 because 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  *      @author Howard Chu, Symas Corporation.
35  *
36  *      @copyright Copyright 2011 Howard Chu, Symas Corp. All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted only as authorized by the OpenLDAP
40  * Public License.
41  *
42  * A copy of this license is available in the file LICENSE in the
43  * top-level directory of the distribution or, alternatively, at
44  * <http://www.OpenLDAP.org/license.html>.
45  *
46  *      @par Derived From:
47  * This code is derived from btree.c written by Martin Hedenfalk.
48  *
49  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
50  *
51  * Permission to use, copy, modify, and distribute this software for any
52  * purpose with or without fee is hereby granted, provided that the above
53  * copyright notice and this permission notice appear in all copies.
54  *
55  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
56  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
57  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
58  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
59  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
61  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
62  */
63 #ifndef _MDB_H_
64 #define _MDB_H_
65
66 #include <sys/types.h>
67
68 /** @defgroup public Public API
69  *      @{
70  */
71 /** @defgroup Version Version Macros
72  *      @{
73  */
74 /** Library major version */
75 #define MDB_VERSION_MAJOR       0
76 /** Library minor version */
77 #define MDB_VERSION_MINOR       9
78 /** Library patch version */
79 #define MDB_VERSION_PATCH       0
80
81 /** Combine args a,b,c into a single integer for easy version comparisons */
82 #define MDB_VERINT(a,b,c)       (((a) << 24) | ((b) << 16) | (c))
83
84 /** The full library version as a single integer */
85 #define MDB_VERSION_FULL        \
86         MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
87
88 /** The release date of this library version */
89 #define MDB_VERSION_DATE        "September 1, 2011"
90
91 /** A stringifier for the version info */
92 #define MDB_VERSTR(a,b,c,d)     "MDB " #a "." #b "." #c ": (" #d ")"
93
94 /** A helper for the stringifier macro */
95 #define MDB_VERFOO(a,b,c,d)     MDB_VERSTR(a,b,c,d)
96
97 /** The full library version as a C string */
98 #define MDB_VERSION_STRING      \
99         MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
100 /**     @} */
101
102 /** Opaque structure for a database environment. A DB environment supports multiple
103  * databases, all residing in the same shared-memory map.
104  */
105 typedef struct MDB_env MDB_env;
106
107 /** Opaque structure for a transaction handle. All database operations require
108  * a transaction handle. Transactions may be read-only or read-write.
109  */
110 typedef struct MDB_txn MDB_txn;
111
112 /** A handle for an individual database in the DB environment. */
113 typedef unsigned int    MDB_dbi;
114
115 /** Opaque structure for navigating through a database */
116 typedef struct MDB_cursor MDB_cursor;
117
118 /** Generic structure used for passing keys and data in and out of the database. */
119 typedef struct MDB_val {
120         size_t           mv_size;       /**< size of the data item */
121         void            *mv_data;       /**< address of the data item */
122 } MDB_val;
123
124 /** A callback function used to compare two keys in a database */
125 typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
126
127 /** A callback function used to relocate a position-dependent data item
128  * in a fixed-address database. The \b newptr gives the item's current address in
129  * the memory map, and \b oldptr gives its previous address. This callback is
130  * expected to walk through the fields of the record in newptr and modify any
131  * values based at the \b oldptr address to be relative to the \b newptr address.
132  * @todo This feature is currently unimplemented.
133  */
134 typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size);
135
136 /** @defgroup   mdb_env Environment Flags
137  *      @{
138  */
139         /** mmap at a fixed address */
140 #define MDB_FIXEDMAP    0x01
141         /** don't fsync after commit */
142 #define MDB_NOSYNC              0x10000
143         /** read only */
144 #define MDB_RDONLY              0x20000
145 /** @} */
146
147 /**     @defgroup       mdb_open        Database Flags
148  *      @{
149  */
150         /** use reverse string keys */
151 #define MDB_REVERSEKEY  0x02
152         /** use sorted duplicates */
153 #define MDB_DUPSORT             0x04
154         /** numeric keys in native byte order.
155          *  The keys must all be of the same size. */
156 #define MDB_INTEGERKEY  0x08
157         /** with #MDB_DUPSORT, sorted dup items have fixed size */
158 #define MDB_DUPFIXED    0x10
159         /** with #MDB_DUPSORT, dups are numeric in native byte order */
160 #define MDB_INTEGERDUP  0x20
161         /** with #MDB_DUPSORT, use reverse string dups */
162 #define MDB_REVERSEDUP  0x40
163         /** create DB if not already existing */
164 #define MDB_CREATE              0x40000
165 /** @} */
166
167 /**     @defgroup mdb_put       Write Flags
168  *      @{
169  */
170 /** For put: Don't write if the key already exists. */
171 #define MDB_NOOVERWRITE 0x10
172 /** Only for #MDB_DUPSORT<br>
173  * For put: don't write if the key and data pair already exist.<br>
174  * For mdb_cursor_del: remove all duplicate data items.
175  */
176 #define MDB_NODUPDATA   0x20
177 /** For mdb_cursor_put: overwrite the current key/data pair */
178 #define MDB_CURRENT     0x40
179 /*      @} */
180
181 /** Cursor operations */
182 typedef enum MDB_cursor_op {
183         MDB_FIRST,                              /**< Position at first key/data item */
184         MDB_FIRST_DUP,                  /**< Position at first data item of current key.
185                                                                 Only for #MDB_DUPSORT */
186         MDB_GET_BOTH,                   /**< Position at key/data pair. Only for #MDB_DUPSORT */
187         MDB_GET_BOTH_RANGE,             /**< position at key, nearest data. Only for #MDB_DUPSORT */
188         MDB_GET_MULTIPLE,               /**< Return all the duplicate data items at the current
189                                                                  cursor position. Only for #MDB_DUPFIXED */
190         MDB_LAST,                               /**< Position at last key/data item */
191         MDB_LAST_DUP,                   /**< Position at last data item of current key.
192                                                                 Only for #MDB_DUPSORT */
193         MDB_NEXT,                               /**< Position at next data item */
194         MDB_NEXT_DUP,                   /**< Position at next data item of current key.
195                                                                 Only for #MDB_DUPSORT */
196         MDB_NEXT_MULTIPLE,              /**< Return all duplicate data items at the next
197                                                                 cursor position. Only for #MDB_DUPFIXED */
198         MDB_NEXT_NODUP,                 /**< Position at first data item of next key.
199                                                                 Only for #MDB_DUPSORT */
200         MDB_PREV,                               /**< Position at previous data item */
201         MDB_PREV_DUP,                   /**< Position at previous data item of current key.
202                                                                 Only for #MDB_DUPSORT */
203         MDB_PREV_NODUP,                 /**< Position at last data item of previous key.
204                                                                 Only for #MDB_DUPSORT */
205         MDB_SET,                                /**< Position at specified key */
206         MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
207 } MDB_cursor_op;
208
209 /** @defgroup  errors   Return Codes
210  *
211  *      BerkeleyDB uses -30800 to -30999, we'll go under them
212  *      @{
213  */
214         /**     Successful result */
215 #define MDB_SUCCESS      0
216         /** key/data pair already exists */
217 #define MDB_KEYEXIST    (-30799)
218         /** key/data pair not found (EOF) */
219 #define MDB_NOTFOUND    (-30798)
220         /** Requested page not found - this usually indicates corruption */
221 #define MDB_PAGE_NOTFOUND       (-30797)
222         /** Located page was wrong type */
223 #define MDB_CORRUPTED   (-30796)
224         /** Update of meta page failed, probably I/O error */
225 #define MDB_PANIC               (-30795)
226         /** Environment version mismatch */
227 #define MDB_VERSION_MISMATCH    (-30794)
228 /** @} */
229
230 /** Statistics for a database in the environment */
231 typedef struct MDB_stat {
232         unsigned int    ms_psize;                       /**< Size of a database page.
233                                                                                         This is currently the same for all databases. */
234         unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
235         unsigned long   ms_branch_pages;        /**< Number of internal (non-leaf) pages */
236         unsigned long   ms_leaf_pages;          /**< Number of leaf pages */
237         unsigned long   ms_overflow_pages;      /**< Number of overflow pages */
238         unsigned long   ms_entries;                     /**< Number of data items */
239 } MDB_stat;
240
241         /** Return the mdb library version information.
242          * @param[out] major if non-NULL, the library major version number is copied here
243          * @param[out] minor if non-NULL, the library minor version number is copied here
244          * @param[out] patch if non-NULL, the library patch version number is copied here
245          * @retval "version string" The library version as a string
246          */
247 char *mdb_version(int *major, int *minor, int *patch);
248
249         /** Return a string describing a given error code.
250          * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
251          * function. If the error code is greater than or equal to 0, then the string
252          * returned by the system function strerror(3) is returned. If the error code
253          * is less than 0, an error string corresponding to the MDB library error is
254          * returned. See @ref errors for a list of MDB-specific error codes.
255          * @param[in] err The error code
256          * @retval "error message" The description of the error
257          */
258 char *mdb_strerror(int err);
259
260         /** Create an MDB environment handle.
261          * This function allocates memory for a #MDB_env structure. To release
262          * the allocated memory and discard the handle, call #mdb_env_close().
263          * Before the handle may be used, it must be opened using #mdb_env_open().
264          * Various other options may also need to be set before opening the handle,
265          * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
266          * depending on usage requirements.
267          * @param[out] env The address where the new handle will be stored
268          * @return A non-zero error value on failure and 0 on success.
269          */
270 int  mdb_env_create(MDB_env **env);
271
272         /** Open an environment handle.
273          * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
274          * @param[in] env An environment handle returned by #mdb_env_create()
275          * @param[in] path The directory in which the database files reside. This
276          * directory must already exist and be writable.
277          * @param[in] flags Special options for this environment. This parameter
278          * must be set to 0 or by bitwise OR'ing together one or more of the
279          * values described here.
280          * <ul>
281          *      <li>#MDB_FIXEDMAP
282          *      use a fixed address for the mmap region. This flag must be specified
283          *      when creating the environment, and is stored persistently in the environment.
284          *              If successful, the memory map will always reside at the same virtual address
285          *              and pointers used to reference data items in the database will be constant
286          *              across multiple invocations. This option may not always work, depending on
287          *              how the operating system has allocated memory to shared libraries and other uses.
288          *              The feature is highly experimental.
289          *      <li>#MDB_NOSYNC
290          *              Don't perform a synchronous flush after committing a transaction. This means
291          *              transactions will exhibit the ACI (atomicity, consistency, and isolation)
292          *              properties, but not D (durability); that is database integrity will be
293          *              maintained but it is possible some number of the most recently committed
294          *              transactions may be undone after a system crash. The number of transactions
295          *              at risk is governed by how often the system flushes dirty buffers to disk
296          *              and how often #mdb_env_sync() is called. This flag may be changed
297          *              at any time using #mdb_env_set_flags().
298          *      <li>#MDB_RDONLY
299          *              Open the environment in read-only mode. No write operations will be allowed.
300          * </ul>
301          * @param[in] mode The UNIX permissions to set on created files. This parameter
302          * is ignored on Windows.
303          * @return A non-zero error value on failure and 0 on success. Some possible
304          * errors are:
305          * <ul>
306          *      <li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
307          *      version that created the database environment.
308          *      <li>EINVAL - the environment file headers are corrupted.
309          *      <li>ENOENT - the directory specified by the path parameter doesn't exist.
310          *      <li>EACCES - the user didn't have permission to access the environment files.
311          *      <li>EAGAIN - the environment was locked by another process.
312          * </ul>
313          */
314 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
315
316         /** Return statistics about the MDB environment.
317          * @param[in] env An environment handle returned by #mdb_env_create()
318          * @param[out] stat The address of an #MDB_stat structure
319          *      where the statistics will be copied
320          */
321 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
322
323         /** Flush the data buffers to disk.
324          * Data is always written to disk when #mdb_txn_commit() is called,
325          * but the operating system may keep it buffered. MDB always flushes
326          * the OS buffers upon commit as well, unless the environment was
327          * opened with #MDB_NOSYNC.
328          * @param[in] env An environment handle returned by #mdb_env_create()
329          * @param[in] force If non-zero, force the flush to occur. Otherwise
330          *  if the environment has the #MDB_NOSYNC flag set the flushes
331          *      will be omitted.
332          * @return A non-zero error value on failure and 0 on success. Some possible
333          * errors are:
334          * <ul>
335          *      <li>EINVAL - an invalid parameter was specified.
336          *      <li>EIO - an error occurred during synchronization.
337          * </ul>
338          */
339 int  mdb_env_sync(MDB_env *env, int force);
340
341         /** Close the environment and release the memory map.
342          * Only a single thread may call this function. All transactions, databases,
343          * and cursors must already be closed before calling this function. Attempts to
344          * use any such handles after calling this function will cause a SIGSEGV.
345          * The environment handle will be freed and must not be used again after this call.
346          * @param[in] env An environment handle returned by #mdb_env_create()
347          */
348 void mdb_env_close(MDB_env *env);
349
350         /** Set environment flags.
351          * This may be used to set some flags that weren't already set during
352          * #mdb_env_open(), or to unset these flags. Currently only the
353          * #MDB_NOSYNC flag setting may be changed with this function.
354          * @param[in] env An environment handle returned by #mdb_env_create()
355          * @param[in] flags The flags to change, bitwise OR'ed together
356          * @param[in] onoff A non-zero value sets the flags, zero clears them.
357          * @return A non-zero error value on failure and 0 on success. Some possible
358          * errors are:
359          * <ul>
360          *      <li>EINVAL - an invalid parameter was specified.
361          * </ul>
362          */
363 int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
364
365         /** Get environment flags.
366          * @param[in] env An environment handle returned by #mdb_env_create()
367          * @param[out] flags The address of an integer to store the flags
368          * @return A non-zero error value on failure and 0 on success. Some possible
369          * errors are:
370          * <ul>
371          *      <li>EINVAL - an invalid parameter was specified.
372          * </ul>
373          */
374 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
375
376         /** Return the path that was used in #mdb_env_open().
377          * @param[in] env An environment handle returned by #mdb_env_create()
378          * @param[out] path Address of a string pointer to contain the path. This
379          * is the actual string in the environment, not a copy. It should not be
380          * altered in any way.
381          * @return A non-zero error value on failure and 0 on success. Some possible
382          * errors are:
383          * <ul>
384          *      <li>EINVAL - an invalid parameter was specified.
385          * </ul>
386          */
387 int  mdb_env_get_path(MDB_env *env, const char **path);
388
389         /** Set the size of the memory map to use for this environment.
390          * The size should be a multiple of the OS page size. The default is
391          * 10485760 bytes. The size of the memory map is also the maximum size
392          * of the database. The value should be chosen as large as possible,
393          * to accomodate future growth of the database.
394          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
395          * @param[in] env An environment handle returned by #mdb_env_create()
396          * @param[in] size The size in bytes
397          * @return A non-zero error value on failure and 0 on success. Some possible
398          * errors are:
399          * <ul>
400          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
401          * </ul>
402          */
403 int  mdb_env_set_mapsize(MDB_env *env, size_t size);
404
405         /** Set the maximum number of threads for the environment.
406          * This defines the number of slots in the lock table that is used to track readers in the
407          * the environment. The default is 126.
408          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
409          * @param[in] env An environment handle returned by #mdb_env_create()
410          * @param[in] readers The maximum number of threads
411          * @return A non-zero error value on failure and 0 on success. Some possible
412          * errors are:
413          * <ul>
414          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
415          * </ul>
416          */
417 int  mdb_env_set_maxreaders(MDB_env *env, int readers);
418
419         /** Get the maximum number of threads for the environment.
420          * @param[in] env An environment handle returned by #mdb_env_create()
421          * @param[out] readers Address of an integer to store the number of readers
422          * @return A non-zero error value on failure and 0 on success. Some possible
423          * errors are:
424          * <ul>
425          *      <li>EINVAL - an invalid parameter was specified.
426          * </ul>
427          */
428 int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
429
430         /** Set the maximum number of databases for the environment.
431          * This function is only needed if multiple databases will be used in the
432          * environment. Simpler applications that only use a single database can ignore
433          * this option.
434          * This function may only be called after #mdb_env_create() and before #mdb_env_open().
435          * @param[in] env An environment handle returned by #mdb_env_create()
436          * @param[in] dbs The maximum number of databases
437          * @return A non-zero error value on failure and 0 on success. Some possible
438          * errors are:
439          * <ul>
440          *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
441          * </ul>
442          */
443 int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
444
445         /** Create a transaction for use with the environment.
446          * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
447          * @note Transactions may not span threads, a transaction must only be used by a
448          * single thread.
449          * @note Cursors may not span transactions; each cursor must be opened and closed
450          * within a single transaction.
451          * @param[in] env An environment handle returned by #mdb_env_create()
452          * @param[in] flags Special options for this transaction. This parameter
453          * must be set to 0 or by bitwise OR'ing together one or more of the
454          * values described here.
455          * <ul>
456          *      <li>#MDB_RDONLY
457          *              This transaction will not perform any write operations.
458          * </ul>
459          * @param[out] txn Address where the new #MDB_txn handle will be stored
460          * @return A non-zero error value on failure and 0 on success. Some possible
461          * errors are:
462          * <ul>
463          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
464          *              must be shut down.
465          *      <li>ENOMEM - out of memory, or a read-only transaction was requested and
466          *              the reader lock table is full. See #mdb_env_set_maxreaders().
467          * </ul>
468          */
469 int  mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn);
470
471         /** Commit all the operations of a transaction into the database.
472          * All cursors opened within the transaction must be closed before the transaction
473          * is committed.
474          * The transaction handle will be freed and must not be used again after this call.
475          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
476          * @return A non-zero error value on failure and 0 on success. Some possible
477          * errors are:
478          * <ul>
479          *      <li>EINVAL - an invalid parameter was specified.
480          *      <li>ENOSPC - no more disk space.
481          *      <li>EIO - a low-level I/O error occurred while writing.
482          * </ul>
483          */
484 int  mdb_txn_commit(MDB_txn *txn);
485
486         /** Abandon all the operations of the transaction instead of saving them.
487          * All cursors opened within the transaction must be closed before the transaction
488          * is aborted.
489          * The transaction handle will be freed and must not be used again after this call.
490          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
491          */
492 void mdb_txn_abort(MDB_txn *txn);
493
494         /** Reset a read-only transaction.
495          * This releases the current reader lock but doesn't free the
496          * transaction handle, allowing it to be used again later by #mdb_txn_renew().
497          * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
498          * allocation/deallocation overhead if a thread is going to start a new
499          * read-only transaction again soon.
500          * All cursors opened within the transaction must be closed before the transaction
501          * is reset.
502          * Reader locks generally don't interfere with writers, but they keep old
503          * versions of database pages allocated. Thus they prevent the old pages
504          * from being reused when writers commit new data, and so under heavy load
505          * the database size may grow much more rapidly than otherwise.
506          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
507          */
508 void mdb_txn_reset(MDB_txn *txn);
509
510         /** Renew a read-only transaction.
511          * This acquires a new reader lock for a transaction handle that had been
512          * released by #mdb_txn_reset(). It must be called before a reset transaction
513          * may be used again.
514          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
515          * @return A non-zero error value on failure and 0 on success. Some possible
516          * errors are:
517          * <ul>
518          *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
519          *              must be shut down.
520          *      <li>EINVAL - an invalid parameter was specified.
521          * </ul>
522          */
523 int  mdb_txn_renew(MDB_txn *txn);
524
525         /** Open a database in the environment.
526          * The database handle may be discarded by calling #mdb_close().
527          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
528          * @param[in] name The name of the database to open. If only a single
529          *      database is needed in the enviroment, this value may be NULL.
530          * @param[in] flags Special options for this database. This parameter
531          * must be set to 0 or by bitwise OR'ing together one or more of the
532          * values described here.
533          * <ul>
534          *      <li>#MDB_REVERSEKEY
535          *              Keys are strings to be compared in reverse order, from the end
536          *              of the strings to the beginning. By default, Keys are treated as strings and
537          *              compared from beginning to end.
538          *      <li>#MDB_DUPSORT
539          *              Duplicate keys may be used in the database. (Or, from another perspective,
540          *              keys may have multiple data items, stored in sorted order.) By default
541          *              keys must be unique and may have only a single data item.
542          *      <li>#MDB_INTEGERKEY
543          *              Keys are binary integers in native byte order. Setting this option
544          *              requires all keys to be the same size, typically sizeof(int)
545          *              or sizeof(long).
546          *      <li>#MDB_DUPFIXED
547          *              This flag may only be used in combination with #MDB_DUPSORT. This option
548          *              tells the library that the data items for this database are all the same
549          *              size, which allows further optimizations in storage and retrieval. When
550          *              all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
551          *              cursor operations may be used to retrieve multiple items at once.
552          *      <li>#MDB_INTEGERDUP
553          *              This option specifies that duplicate data items are also integers, and
554          *              should be sorted as such.
555          *      <li>#MDB_REVERSEDUP
556          *              This option specifies that duplicate data items should be compared as
557          *              strings in reverse order.
558          *      <li>#MDB_CREATE
559          *              Create the named database if it doesn't exist. This option is not
560          *              allowed in a read-only transaction or a read-only environment.
561          * </ul>
562          * @param[out] dbi Address where the new #MDB_dbi handle will be stored
563          * @return A non-zero error value on failure and 0 on success. Some possible
564          * errors are:
565          * <ul>
566          *      <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
567          *              and #MDB_CREATE was not specified.
568          *      <li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
569          * </ul>
570          */
571 int  mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
572
573         /** Retrieve statistics for a database.
574          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
575          * @param[in] dbi A database handle returned by #mdb_open()
576          * @param[out] stat The address of an #MDB_stat structure
577          *      where the statistics will be copied
578          * @return A non-zero error value on failure and 0 on success. Some possible
579          * errors are:
580          * <ul>
581          *      <li>EINVAL - an invalid parameter was specified.
582          * </ul>
583          */
584 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
585
586         /** Close a database handle.
587          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
588          * @param[in] dbi A database handle returned by #mdb_open()
589          */
590 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
591
592         /** Set a custom key comparison function for a database.
593          * The comparison function is called whenever it is necessary to compare a
594          * key specified by the application with a key currently stored in the database.
595          * If no comparison function is specified, and no special key flags were specified
596          * with #mdb_open(), the keys are compared lexically, with shorter keys collating
597          * before longer keys.
598          * @warning This function must be called before any data access functions are used,
599          * otherwise data corruption may occur. The same function must be used by every
600          * process accessing the database, every time the database is used.
601          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
602          * @param[in] dbi A database handle returned by #mdb_open()
603          * @param[in] cmp A #MDB_cmp_func function
604          * @return A non-zero error value on failure and 0 on success. Some possible
605          * errors are:
606          * <ul>
607          *      <li>EINVAL - an invalid parameter was specified.
608          * </ul>
609          */
610 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
611
612         /** Set a custom data comparison function for a #MDB_DUPSORT database.
613          * This comparison function is called whenever it is necessary to compare a data
614          * item specified by the application with a data item currently stored in the database.
615          * This function only takes effect if the database was opened with the #MDB_DUPSORT
616          * flag.
617          * If no comparison function is specified, and no special key flags were specified
618          * with #mdb_open(), the data items are compared lexically, with shorter items collating
619          * before longer items.
620          * @warning This function must be called before any data access functions are used,
621          * otherwise data corruption may occur. The same function must be used by every
622          * process accessing the database, every time the database is used.
623          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
624          * @param[in] dbi A database handle returned by #mdb_open()
625          * @param[in] cmp A #MDB_cmp_func function
626          * @return A non-zero error value on failure and 0 on success. Some possible
627          * errors are:
628          * <ul>
629          *      <li>EINVAL - an invalid parameter was specified.
630          * </ul>
631          */
632 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
633
634         /** Set a relocation function for a #MDB_FIXEDMAP database.
635          * @todo The relocation function is called whenever it is necessary to move the data
636          * of an item to a different position in the database (e.g. through tree
637          * balancing operations, shifts as a result of adds or deletes, etc.). It is
638          * intended to allow address/position-dependent data items to be stored in
639          * a database in an environment opened with the #MDB_FIXEDMAP option.
640          * Currently the relocation feature is unimplemented and setting
641          * this function has no effect.
642          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
643          * @param[in] dbi A database handle returned by #mdb_open()
644          * @param[in] rel A #MDB_rel_func function
645          * @return A non-zero error value on failure and 0 on success. Some possible
646          * errors are:
647          * <ul>
648          *      <li>EINVAL - an invalid parameter was specified.
649          * </ul>
650          */
651 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
652
653         /** Get items from a database.
654          * This function retrieves key/data pairs from the database. The address
655          * and length of the data associated with the specified \b key are returned
656          * in the structure to which \b data refers.
657          * If the database supports duplicate keys (#MDB_DUPSORT) then the
658          * first data item for the key will be returned. Retrieval of other
659          * items requires the use of #mdb_cursor_get().
660          *
661          * @note The memory pointed to by the returned values is owned by the
662          * database. The caller need not dispose of the memory, and may not
663          * modify it in any way. For values returned in a read-only transaction
664          * any modification attempts will cause a SIGSEGV.
665          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
666          * @param[in] dbi A database handle returned by #mdb_open()
667          * @param[in] key The key to search for in the database
668          * @param[out] data The data corresponding to the key
669          * @return A non-zero error value on failure and 0 on success. Some possible
670          * errors are:
671          * <ul>
672          *      <li>#MDB_NOTFOUND - the key was not in the database.
673          *      <li>EINVAL - an invalid parameter was specified.
674          * </ul>
675          */
676 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
677
678         /** Store items into a database.
679          * This function stores key/data pairs in the database. The default behavior
680          * is to enter the new key/data pair, replacing any previously existing key
681          * if duplicates are disallowed, or adding a duplicate data item if
682          * duplicates are allowed (#MDB_DUPSORT).
683          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
684          * @param[in] dbi A database handle returned by #mdb_open()
685          * @param[in] key The key to store in the database
686          * @param[in,out] data The data to store
687          * @param[in] flags Special options for this operation. This parameter
688          * must be set to 0 or by bitwise OR'ing together one or more of the
689          * values described here.
690          * <ul>
691          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
692          *              already appear in the database. This flag may only be specified
693          *              if the database was opened with #MDB_DUPSORT. The function will
694          *              return #MDB_KEYEXIST if the key/data pair already appears in the
695          *              database.
696          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
697          *              does not already appear in the database. The function will return
698          *              #MDB_KEYEXIST if the key already appears in the database, even if
699          *              the database supports duplicates (#MDB_DUPSORT). The \b data
700          *              parameter will be set to point to the existing item.
701          * </ul>
702          * @return A non-zero error value on failure and 0 on success. Some possible
703          * errors are:
704          * <ul>
705          *      <li>EACCESS - an attempt was made to write in a read-only transaction.
706          *      <li>EINVAL - an invalid parameter was specified.
707          *      <li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
708          * </ul>
709          */
710 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
711                             unsigned int flags);
712
713         /** Delete items from a database.
714          * This function removes key/data pairs from the database.
715          * If the database does not support sorted duplicate data items
716          * (#MDB_DUPSORT) the data parameter is ignored.
717          * If the database supports sorted duplicates and the data parameter
718          * is NULL, all of the duplicate data items for the key will be
719          * deleted. Otherwise, if the data parameter is non-NULL
720          * only the matching data item will be deleted.
721          * This function will return #MDB_NOTFOUND if the specified key/data
722          * pair is not in the database.
723          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
724          * @param[in] dbi A database handle returned by #mdb_open()
725          * @param[in] key The key to delete from the database
726          * @param[in] data The data to delete
727          * @return A non-zero error value on failure and 0 on success. Some possible
728          * errors are:
729          * <ul>
730          *      <li>EACCESS - an attempt was made to write in a read-only transaction.
731          *      <li>EINVAL - an invalid parameter was specified.
732          * </ul>
733          */
734 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
735
736         /** Create a cursor handle.
737          * Cursors are associated with a specific transaction and database and
738          * may not span threads.
739          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
740          * @param[in] dbi A database handle returned by #mdb_open()
741          * @param[out] cursor Address where the new #MDB_cursor handle will be stored
742          * @return A non-zero error value on failure and 0 on success. Some possible
743          * errors are:
744          * <ul>
745          *      <li>EINVAL - an invalid parameter was specified.
746          * </ul>
747          */
748 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
749
750         /** Close a cursor handle.
751          * The cursor handle will be freed and must not be used again after this call.
752          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
753          */
754 void mdb_cursor_close(MDB_cursor *cursor);
755
756         /** Retrieve by cursor.
757          * This function retrieves key/data pairs from the database. The address and length
758          * of the key are returned in the object to which \b key refers (except for the
759          * case of the #MDB_SET option, in which the \b key object is unchanged), and
760          * the address and length of the data are returned in the object to which \b data
761          * refers.
762          * @bug Cursors are not coordinated with write operations. If a cursor in a
763          * write transaction is performing a sequential scan while records are being
764          * inserted or deleted in the same transaction, the cursor will be corrupted.
765          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
766          * @param[in,out] key The key for a retrieved item
767          * @param[in,out] data The data of a retrieved item
768          * @param[in] op A cursor operation #MDB_cursor_op
769          * @return A non-zero error value on failure and 0 on success. Some possible
770          * errors are:
771          * <ul>
772          *      <li>#MDB_NOTFOUND - no matching key found.
773          *      <li>EINVAL - an invalid parameter was specified.
774          * </ul>
775          */
776 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
777                             MDB_cursor_op op);
778
779         /** Store by cursor.
780          * This function stores key/data pairs into the database.
781          * If the function fails for any reason, the state of the cursor will be
782          * unchanged. If the function succeeds and an item is inserted into the
783          * database, the cursor is always positioned to refer to the newly inserted item.
784          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
785          * @param[in] key The key operated on.
786          * @param[in] data The data operated on.
787          * @param[in] flags Options for this operation. This parameter
788          * must be set to 0 or one of the values described here.
789          * <ul>
790          *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
791          *              the cursor refers with the specified data item. The \b key
792          *              parameter is ignored.
793          *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
794          *              already appear in the database. This flag may only be specified
795          *              if the database was opened with #MDB_DUPSORT. The function will
796          *              return #MDB_KEYEXIST if the key/data pair already appears in the
797          *              database.
798          *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
799          *              does not already appear in the database. The function will return
800          *              #MDB_KEYEXIST if the key already appears in the database, even if
801          *              the database supports duplicates (#MDB_DUPSORT).
802          * </ul>
803          * @return A non-zero error value on failure and 0 on success. Some possible
804          * errors are:
805          * <ul>
806          *      <li>EACCES - an attempt was made to modify a read-only database.
807          *      <li>EINVAL - an invalid parameter was specified.
808          * </ul>
809          */
810 int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
811                                 unsigned int flags);
812
813         /**  Delete current key/data pair
814          * This function deletes the key/data pair to which the cursor refers.
815          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
816          * @param[in] flags Options for this operation. This parameter
817          * must be set to 0 or one of the values described here.
818          * <ul>
819          *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
820          *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
821          * </ul>
822          * @return A non-zero error value on failure and 0 on success. Some possible
823          * errors are:
824          * <ul>
825          *      <li>EACCES - an attempt was made to modify a read-only database.
826          *      <li>EINVAL - an invalid parameter was specified.
827          * </ul>
828          */
829 int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
830
831         /** Return count of duplicates for current key.
832          * This call is only valid on databases that support sorted duplicate
833          * data items #MDB_DUPSORT.
834          * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
835          * @param[out] countp Address where the count will be stored
836          * @return A non-zero error value on failure and 0 on success. Some possible
837          * errors are:
838          * <ul>
839          *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
840          * </ul>
841          */
842 int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
843
844         /** Compare two data items according to a particular database.
845          * This returns a comparison as if the two data items were keys in the
846          * specified database.
847          * @param[in] txn A transaction handle returned by #mdb_txn_begin()
848          * @param[in] dbi A database handle returned by #mdb_open()
849          * @param[in] a The first item to compare
850          * @param[in] b The second item to compare
851          * @return < 0 if a < b, 0 if a == b, > 0 if a > b
852          */
853 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
854
855 /**     @} */
856 #endif /* _MDB_H_ */