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