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