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