]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/lmdb.h
ITS#7672 fix mdb_dbi_flags
[openldap] / libraries / liblmdb / lmdb.h
index 4906472f24d7a120636c1b07650e366eb8a8ce55..b2c3861ac99bf4f1632e3c27c5e1ab52a2f89500 100644 (file)
  *       cause further writes to grow the database quickly, and
  *       stale locks can block further operation.
  *
- *       Fix: Terminate all programs using the database, or make
- *       them close it.  Next database user will reset the lockfile.
+ *       Fix: Check for stale readers periodically, using the
+ *       #mdb_reader_check function or the mdb_stat tool. Or just
+ *       make all programs using the database close it; the lockfile
+ *       is always reset on first open of the environment.
  *
  *     - On BSD systems or others configured with MDB_USE_POSIX_SEM,
  *       startup can fail due to semaphores owned by another userid.
  *     ...when several processes can use a database concurrently:
  *
  *     - Avoid aborting a process with an active transaction.
- *       The transaction becomes "long-lived" as above until the lockfile
- *       is reset, since the process may not remove it from the lockfile.
+ *       The transaction becomes "long-lived" as above until a check
+ *       for stale readers is performed or the lockfile is reset,
+ *       since the process may not remove it from the lockfile.
  *
- *     - If you do that anyway, close the environment once in a while,
- *       so the lockfile can get reset.
+ *     - If you do that anyway, do a periodic check for stale readers. Or
+ *       close the environment once in a while, so the lockfile can get reset.
  *
  *     - Do not use MDB databases on remote filesystems, even between
  *       processes on the same host.  This breaks flock() on some OSes,
 extern "C" {
 #endif
 
+/** Unix permissions for creating files, or dummy definition for Windows */
 #ifdef _MSC_VER
 typedef        int     mdb_mode_t;
 #else
 typedef        mode_t  mdb_mode_t;
 #endif
 
+/** An abstraction for a file handle.
+ *     On POSIX systems file handles are small integers. On Windows
+ *     they're opaque pointers.
+ */
+#ifdef _WIN32
+typedef        void *mdb_filehandle_t;
+#else
+typedef int mdb_filehandle_t;
+#endif
+
 /** @defgroup mdb MDB API
  *     @{
  *     @brief OpenLDAP Lightning Memory-Mapped Database Manager
@@ -156,7 +170,7 @@ typedef     mode_t  mdb_mode_t;
 /** Library minor version */
 #define MDB_VERSION_MINOR      9
 /** Library patch version */
-#define MDB_VERSION_PATCH      6
+#define MDB_VERSION_PATCH      7
 
 /** Combine args a,b,c into a single integer for easy version comparisons */
 #define MDB_VERINT(a,b,c)      (((a) << 24) | ((b) << 16) | (c))
@@ -206,6 +220,9 @@ typedef struct MDB_cursor MDB_cursor;
  * #MDB_MAXKEYSIZE inclusive. This currently defaults to 511. The
  * same applies to data sizes in databases with the #MDB_DUPSORT flag.
  * Other data items can in theory be from 0 to 0xffffffff bytes long.
+ *
+ * Values returned from the database are valid only until a subsequent
+ * update operation, or the end of the transaction.
  */
 typedef struct MDB_val {
        size_t           mv_size;       /**< size of the data item */
@@ -296,7 +313,7 @@ typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *rel
 #define MDB_APPEND     0x20000
 /** Duplicate data is being appended, don't split full pages. */
 #define MDB_APPENDDUP  0x40000
-/** Store multiple data items in one call. */
+/** Store multiple data items in one call. Only for #MDB_DUPFIXED. */
 #define MDB_MULTIPLE   0x80000
 /*     @} */
 
@@ -322,13 +339,11 @@ typedef enum MDB_cursor_op {
                                                                Only for #MDB_DUPSORT */
        MDB_NEXT_MULTIPLE,              /**< Return all duplicate data items at the next
                                                                cursor position. Only for #MDB_DUPFIXED */
-       MDB_NEXT_NODUP,                 /**< Position at first data item of next key.
-                                                               Only for #MDB_DUPSORT */
+       MDB_NEXT_NODUP,                 /**< Position at first data item of next key */
        MDB_PREV,                               /**< Position at previous data item */
        MDB_PREV_DUP,                   /**< Position at previous data item of current key.
                                                                Only for #MDB_DUPSORT */
-       MDB_PREV_NODUP,                 /**< Position at last data item of previous key.
-                                                               Only for #MDB_DUPSORT */
+       MDB_PREV_NODUP,                 /**< Position at last data item of previous key */
        MDB_SET,                                /**< Position at specified key */
        MDB_SET_KEY,                    /**< Position at specified key, return key + data */
        MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
@@ -371,11 +386,15 @@ typedef enum MDB_cursor_op {
 #define MDB_PAGE_FULL  (-30786)
        /** Database contents grew beyond environment mapsize */
 #define MDB_MAP_RESIZED        (-30785)
-       /** Database flags changed or would change */
+       /** MDB_INCOMPATIBLE: Operation and DB incompatible, or DB flags changed */
 #define MDB_INCOMPATIBLE       (-30784)
        /** Invalid reuse of reader locktable slot */
 #define MDB_BAD_RSLOT          (-30783)
-#define MDB_LAST_ERRCODE       MDB_BAD_RSLOT
+       /** Transaction cannot recover - it must be aborted */
+#define MDB_BAD_TXN                    (-30782)
+       /** Too big key/data, key is empty, or wrong DUPFIXED size */
+#define MDB_BAD_VALSIZE                (-30781)
+#define MDB_LAST_ERRCODE       MDB_BAD_VALSIZE
 /** @} */
 
 /** @brief Statistics for a database in the environment */
@@ -523,6 +542,10 @@ int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t
        /** @brief Copy an MDB environment to the specified path.
         *
         * This function may be used to make a backup of an existing environment.
+        * No lockfile is created, since it gets recreated at need.
+        * @note This call can trigger significant file size growth if run in
+        * parallel with write transactions, because it employs a read-only
+        * transaction. See long-lived transactions under @ref caveats_sec.
         * @param[in] env An environment handle returned by #mdb_env_create(). It
         * must have already been opened successfully.
         * @param[in] path The directory in which the copy will reside. This
@@ -532,6 +555,21 @@ int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t
         */
 int  mdb_env_copy(MDB_env *env, const char *path);
 
+       /** @brief Copy an MDB environment to the specified file descriptor.
+        *
+        * This function may be used to make a backup of an existing environment.
+        * No lockfile is created, since it gets recreated at need.
+        * @note This call can trigger significant file size growth if run in
+        * parallel with write transactions, because it employs a read-only
+        * transaction. See long-lived transactions under @ref caveats_sec.
+        * @param[in] env An environment handle returned by #mdb_env_create(). It
+        * must have already been opened successfully.
+        * @param[in] fd The filedescriptor to write the copy to. It must
+        * have already been opened for Write access.
+        * @return A non-zero error value on failure and 0 on success.
+        */
+int  mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd);
+
        /** @brief Return statistics about the MDB environment.
         *
         * @param[in] env An environment handle returned by #mdb_env_create()
@@ -685,20 +723,26 @@ int  mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
         */
 int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
 
+       /** @brief Get the maximum size of a key for the environment.
+        *
+        * @param[in] env An environment handle returned by #mdb_env_create()
+        * @return The maximum size of a key. (#MDB_MAXKEYSIZE)
+        */
+int  mdb_env_get_maxkeysize(MDB_env *env);
+
        /** @brief Create a transaction for use with the environment.
         *
         * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
         * @note A transaction and its cursors must only be used by a single
         * thread, and a thread may only have a single transaction at a time.
         * If #MDB_NOTLS is in use, this does not apply to read-only transactions.
-        * @note Cursors may not span transactions; each cursor must be opened and closed
-        * within a single transaction.
+        * @note Cursors may not span transactions.
         * @param[in] env An environment handle returned by #mdb_env_create()
         * @param[in] parent If this parameter is non-NULL, the new transaction
         * will be a nested transaction, with the transaction indicated by \b parent
         * as its parent. Transactions may be nested to any level. A parent
-        * transaction may not issue any other operations besides mdb_txn_begin,
-        * mdb_txn_abort, or mdb_txn_commit while it has active child transactions.
+        * transaction and its cursors may not issue any other operations than
+        * mdb_txn_commit and mdb_txn_abort while it has active child transactions.
         * @param[in] flags Special options for this transaction. This parameter
         * must be set to 0 or by bitwise OR'ing together one or more of the
         * values described here.
@@ -711,7 +755,7 @@ int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
         * errors are:
         * <ul>
         *      <li>#MDB_PANIC - a fatal error occurred earlier and the environment
--       *              must be shut down.
+        *              must be shut down.
         *      <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's
         *              mapsize and the environment must be shut down.
         *      <li>#MDB_READERS_FULL - a read-only transaction was requested and
@@ -721,10 +765,18 @@ int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
         */
 int  mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
 
+       /** @brief Returns the transaction's #MDB_env
+        *
+        * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+        */
+MDB_env *mdb_txn_env(MDB_txn *txn);
+
        /** @brief Commit all the operations of a transaction into the database.
         *
-        * All cursors opened within the transaction will be closed by this call. The cursors
-        * and transaction handle will be freed and must not be used again after this call.
+        * The transaction handle is freed. It and its cursors must not be used
+        * again after this call, except with #mdb_cursor_renew().
+        * @note Earlier documentation incorrectly said all cursors would be freed.
+        * Only write-transactions free cursors.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @return A non-zero error value on failure and 0 on success. Some possible
         * errors are:
@@ -739,8 +791,10 @@ int  mdb_txn_commit(MDB_txn *txn);
 
        /** @brief Abandon all the operations of the transaction instead of saving them.
         *
-        * All cursors opened within the transaction will be closed by this call. The cursors
-        * and transaction handle will be freed and must not be used again after this call.
+        * The transaction handle is freed. It and its cursors must not be used
+        * again after this call, except with #mdb_cursor_renew().
+        * @note Earlier documentation incorrectly said all cursors would be freed.
+        * Only write-transactions free cursors.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         */
 void mdb_txn_abort(MDB_txn *txn);
@@ -754,8 +808,8 @@ void mdb_txn_abort(MDB_txn *txn);
         * lock is released, but the table slot stays tied to its thread or
         * #MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free
         * its lock table slot if MDB_NOTLS is in use.
-        * All cursors opened within the transaction must be closed before the transaction
-        * is reset.
+        * Cursors opened within the transaction must not be used
+        * again after this call, except with #mdb_cursor_renew().
         * Reader locks generally don't interfere with writers, but they keep old
         * versions of database pages allocated. Thus they prevent the old pages
         * from being reused when writers commit new data, and so under heavy load
@@ -787,6 +841,8 @@ int  mdb_txn_renew(MDB_txn *txn);
 
        /** @brief Open a database in the environment.
         *
+        * A database handle denotes the name and parameters of a database,
+        * independently of whether such a database exists.
         * The database handle may be discarded by calling #mdb_dbi_close().
         * The old database handle is returned if the database was already open.
         * The handle must only be closed once.
@@ -862,6 +918,15 @@ int  mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *d
         */
 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
 
+       /** @brief Retrieve the DB flags for a database handle.
+        *
+        * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+        * @param[in] dbi A database handle returned by #mdb_dbi_open()
+        * @param[out] flags Address where the flags will be returned.
+        * @return A non-zero error value on failure and 0 on success.
+        */
+int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags);
+
        /** @brief Close a database handle.
         *
         * This call is not mutex protected. Handles should only be closed by
@@ -873,14 +938,12 @@ int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
         */
 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
 
-       /** @brief Delete a database and/or free all its pages.
+       /** @brief Empty or delete+close a database.
         *
-        * If the \b del parameter is 1, the DB handle will be closed
-        * and the DB will be deleted.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_dbi_open()
-        * @param[in] del 1 to delete the DB from the environment,
-        * 0 to just free its pages.
+        * @param[in] del 0 to empty the DB, 1 to delete it from the
+        * environment and close the DB handle.
         * @return A non-zero error value on failure and 0 on success.
         */
 int  mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
@@ -978,6 +1041,8 @@ int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
         * database. The caller need not dispose of the memory, and may not
         * modify it in any way. For values returned in a read-only transaction
         * any modification attempts will cause a SIGSEGV.
+        * @note Values returned from the database are valid only until a
+        * subsequent update operation, or the end of the transaction.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_dbi_open()
         * @param[in] key The key to search for in the database
@@ -1017,7 +1082,8 @@ int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
         *              parameter will be set to point to the existing item.
         *      <li>#MDB_RESERVE - reserve space for data of the given size, but
         *              don't copy the given data. Instead, return a pointer to the
-        *              reserved space, which the caller can fill in later. This saves
+        *              reserved space, which the caller can fill in later - before
+        *              the next update operation or the transaction ends. This saves
         *              an extra memcpy if the data is being generated later.
         *      <li>#MDB_APPEND - append the given key/data pair to the end of the
         *              database. No key comparisons are performed. This option allows
@@ -1065,7 +1131,16 @@ int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
        /** @brief Create a cursor handle.
         *
         * A cursor is associated with a specific transaction and database.
-        * It must be closed before its transaction ends.
+        * A cursor cannot be used when its database handle is closed.  Nor
+        * when its transaction has ended, except with #mdb_cursor_renew().
+        * It can be discarded with #mdb_cursor_close().
+        * A cursor in a write-transaction can be closed before its transaction
+        * ends, and will otherwise be closed when its transaction ends.
+        * A cursor in a read-only transaction must be closed explicitly, before
+        * or after its transaction ends. It can be reused with
+        * #mdb_cursor_renew() before finally closing it.
+        * @note Earlier documentation said that cursors in every transaction
+        * were closed when the transaction committed or aborted.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_dbi_open()
         * @param[out] cursor Address where the new #MDB_cursor handle will be stored
@@ -1080,6 +1155,7 @@ int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
        /** @brief Close a cursor handle.
         *
         * The cursor handle will be freed and must not be used again after this call.
+        * Its transaction must still be live if it is a write-transaction.
         * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
         */
 void mdb_cursor_close(MDB_cursor *cursor);
@@ -1087,11 +1163,11 @@ void mdb_cursor_close(MDB_cursor *cursor);
        /** @brief Renew a cursor handle.
         *
         * A cursor is associated with a specific transaction and database.
-        * It must be closed before its transaction ends.
         * Cursors that are only used in read-only
         * transactions may be re-used, to avoid unnecessary malloc/free overhead.
         * The cursor may be associated with a new read-only transaction, and
         * referencing the same database handle as it was created with.
+        * This may be done whether the previous transaction is live or dead.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
         * @return A non-zero error value on failure and 0 on success. Some possible
@@ -1121,6 +1197,7 @@ MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
         * case of the #MDB_SET option, in which the \b key object is unchanged), and
         * the address and length of the data are returned in the object to which \b data
         * refers.
+        * See #mdb_get() for restrictions on using the output values.
         * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
         * @param[in,out] key The key for a retrieved item
         * @param[in,out] data The data of a retrieved item
@@ -1169,6 +1246,16 @@ int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
         *              correct order. Loading unsorted keys with this flag will cause
         *              data corruption.
         *      <li>#MDB_APPENDDUP - as above, but for sorted dup data.
+        *      <li>#MDB_MULTIPLE - store multiple contiguous data elements in a
+        *              single request. This flag may only be specified if the database
+        *              was opened with #MDB_DUPFIXED. The \b data argument must be an
+        *              array of two MDB_vals. The mv_size of the first MDB_val must be
+        *              the size of a single data element. The mv_data of the first MDB_val
+        *              must point to the beginning of the array of contiguous data elements.
+        *              The mv_size of the second MDB_val must be the count of the number
+        *              of data elements to store. On return this field will be set to
+        *              the count of the number of elements actually written. The mv_data
+        *              of the second MDB_val is unused.
         * </ul>
         * @return A non-zero error value on failure and 0 on success. Some possible
         * errors are:
@@ -1230,7 +1317,7 @@ int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
        /** @brief Compare two data items according to a particular database.
         *
         * This returns a comparison as if the two items were data items of
-        * a sorted duplicates #MDB_DUPSORT database.
+        * the specified database. The database must have the #MDB_DUPSORT flag.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_dbi_open()
         * @param[in] a The first item to compare
@@ -1238,6 +1325,31 @@ int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
         * @return < 0 if a < b, 0 if a == b, > 0 if a > b
         */
 int  mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
+
+       /** @brief A callback function used to print a message from the library.
+        *
+        * @param[in] msg The string to be printed.
+        * @param[in] ctx An arbitrary context pointer for the callback.
+        * @return < 0 on failure, 0 on success.
+        */
+typedef int (MDB_msg_func)(const char *msg, void *ctx);
+
+       /** @brief Dump the entries in the reader lock table.
+        *
+        * @param[in] env An environment handle returned by #mdb_env_create()
+        * @param[in] func A #MDB_msg_func function
+        * @param[in] ctx Anything the message function needs
+        * @return < 0 on failure, 0 on success.
+        */
+int    mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
+
+       /** @brief Check for stale entries in the reader lock table.
+        *
+        * @param[in] env An environment handle returned by #mdb_env_create()
+        * @param[out] dead Number of stale slots that were cleared
+        * @return 0 on success, non-zero on failure.
+        */
+int    mdb_reader_check(MDB_env *env, int *dead);
 /**    @} */
 
 #ifdef __cplusplus