]> git.sur5r.net Git - openldap/blobdiff - libraries/libmdb/mdb.h
Use FNV-1A hash to generate unique mutex names
[openldap] / libraries / libmdb / mdb.h
index 206ddabffa9cbe6378a531b211bd3d23fd77c2f4..b99ece7c95b0cda81ded04b42bb8e465c4c52588 100644 (file)
 
 #include <sys/types.h>
 
+/** @defgroup public Public API
+ *     @{
+ */
 /** @defgroup Version Version Macros
  *     @{
  */
 /** Library major version */
 #define MDB_VERSION_MAJOR      0
 /** Library minor version */
-#define MDB_VERSION_MINOR      8
+#define MDB_VERSION_MINOR      9
 /** Library patch version */
 #define MDB_VERSION_PATCH      0
 
@@ -83,7 +86,7 @@
        MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
 
 /** The release date of this library version */
-#define MDB_VERSION_DATE       "August 11, 2011"
+#define MDB_VERSION_DATE       "September 1, 2011"
 
 /** A stringifier for the version info */
 #define MDB_VERSTR(a,b,c,d)    "MDB " #a "." #b "." #c ": (" #d ")"
        MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
 /**    @} */
 
-/** Opaque structure for navigating through a database */
-typedef struct MDB_cursor MDB_cursor;
+/** Opaque structure for a database environment. A DB environment supports multiple
+ * databases, all residing in the same shared-memory map.
+ */
+typedef struct MDB_env MDB_env;
 
 /** Opaque structure for a transaction handle. All database operations require
  * a transaction handle. Transactions may be read-only or read-write.
  */
 typedef struct MDB_txn MDB_txn;
 
-/** Opaque structure for a database environment. A DB environment supports multiple
- * databases, all residing in the same shared-memory map.
- */
-typedef struct MDB_env MDB_env;
-
 /** A handle for an individual database in the DB environment. */
 typedef unsigned int   MDB_dbi;
 
+/** Opaque structure for navigating through a database */
+typedef struct MDB_cursor MDB_cursor;
+
 /** Generic structure used for passing keys and data in and out of the database. */
 typedef struct MDB_val {
        size_t           mv_size;       /**< size of the data item */
@@ -122,29 +125,79 @@ typedef struct MDB_val {
 typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
 
 /** A callback function used to relocate a position-dependent data item
- * in a fixed-address database. This feature is currently unimplemented.
+ * in a fixed-address database. The \b newptr gives the item's desired address in
+ * the memory map, and \b oldptr gives its previous address. The item's actual
+ * data resides at the address in \b item.  This callback is expected to walk
+ * through the fields of the record in \b item and modify any
+ * values based at the \b oldptr address to be relative to the \b newptr address.
+ * @param[in,out] item The item that is to be relocated.
+ * @param[in] oldptr The previous address.
+ * @param[in] newptr The new address to relocate to.
+ * @param[in] relctx An application-provided context, set by #mdb_set_relctx().
+ * @todo This feature is currently unimplemented.
  */
-typedef void (MDB_rel_func)(void *ptr, void *oldptr);
+typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
 
-/**    @defgroup mdb_put       mdb_put flags
+/** @defgroup  mdb_env Environment Flags
  *     @{
  */
-/** For mdb_put: don't write if the key already exists. */
+       /** mmap at a fixed address */
+#define MDB_FIXEDMAP   0x01
+       /** don't fsync after commit */
+#define MDB_NOSYNC             0x10000
+       /** read only */
+#define MDB_RDONLY             0x20000
+/** @} */
+
+/**    @defgroup       mdb_open        Database Flags
+ *     @{
+ */
+       /** use reverse string keys */
+#define MDB_REVERSEKEY 0x02
+       /** use sorted duplicates */
+#define MDB_DUPSORT            0x04
+       /** numeric keys in native byte order.
+        *  The keys must all be of the same size. */
+#define MDB_INTEGERKEY 0x08
+       /** with #MDB_DUPSORT, sorted dup items have fixed size */
+#define MDB_DUPFIXED   0x10
+       /** with #MDB_DUPSORT, dups are numeric in native byte order */
+#define MDB_INTEGERDUP 0x20
+       /** with #MDB_DUPSORT, use reverse string dups */
+#define MDB_REVERSEDUP 0x40
+       /** create DB if not already existing */
+#define MDB_CREATE             0x40000
+/** @} */
+
+/**    @defgroup mdb_put       Write Flags
+ *     @{
+ */
+/** For put: Don't write if the key already exists. */
 #define MDB_NOOVERWRITE        0x10
-/** For mdb_put: don't write if the key and data pair already exist.
- * Only for #MDB_DUPSORT
+/** Only for #MDB_DUPSORT<br>
+ * For put: don't write if the key and data pair already exist.<br>
+ * For mdb_cursor_del: remove all duplicate data items.
  */
 #define MDB_NODUPDATA  0x20
+/** For mdb_cursor_put: overwrite the current key/data pair */
+#define MDB_CURRENT    0x40
 /*     @} */
 
-/** Cursor operations */
+/** Cursor Get operations
+ *     This is the set of all operations for retrieving data
+ *     using a cursor.
+ */
 typedef enum MDB_cursor_op {
        MDB_FIRST,                              /**< Position at first key/data item */
+       MDB_FIRST_DUP,                  /**< Position at first data item of current key.
+                                                               Only for #MDB_DUPSORT */
        MDB_GET_BOTH,                   /**< Position at key/data pair. Only for #MDB_DUPSORT */
        MDB_GET_BOTH_RANGE,             /**< position at key, nearest data. Only for #MDB_DUPSORT */
        MDB_GET_MULTIPLE,               /**< Return all the duplicate data items at the current
                                                                 cursor position. Only for #MDB_DUPFIXED */
        MDB_LAST,                               /**< Position at last key/data item */
+       MDB_LAST_DUP,                   /**< Position at last data item of current key.
+                                                               Only for #MDB_DUPSORT */
        MDB_NEXT,                               /**< Position at next data item */
        MDB_NEXT_DUP,                   /**< Position at next data item of current key.
                                                                Only for #MDB_DUPSORT */
@@ -156,7 +209,7 @@ typedef enum MDB_cursor_op {
        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 */
+                                                               Only for #MDB_DUPSORT */
        MDB_SET,                                /**< Position at specified key */
        MDB_SET_RANGE                   /**< Position at first key greater than or equal to specified key. */
 } MDB_cursor_op;
@@ -182,44 +235,15 @@ typedef enum MDB_cursor_op {
 #define MDB_VERSION_MISMATCH   (-30794)
 /** @} */
 
-/**    @defgroup       mdb_open         mdb_open flags
- *     @{
- */
-       /** use reverse string keys */
-#define MDB_REVERSEKEY 0x02
-       /** use sorted duplicates */
-#define MDB_DUPSORT            0x04
-       /** numeric keys in native byte order */
-#define MDB_INTEGERKEY 0x08
-       /** with #MDB_DUPSORT, sorted dup items have fixed size */
-#define MDB_DUPFIXED   0x10
-       /** with #MDB_DUPSORT, dups are numeric in native byte order */
-#define MDB_INTEGERDUP 0x20
-/** @} */
-
-/** @defgroup  mdb_env  environment flags
- *     @{
- */
-       /** mmap at a fixed address */
-#define MDB_FIXEDMAP   0x01
-       /** don't fsync after commit */
-#define MDB_NOSYNC             0x10000
-       /** read only */
-#define MDB_RDONLY             0x20000
-/** @} */
-
-       /** create env or DB if not already existing */
-#define MDB_CREATE             0x40000
-
 /** Statistics for a database in the environment */
 typedef struct MDB_stat {
        unsigned int    ms_psize;                       /**< Size of a database page.
                                                                                        This is currently the same for all databases. */
        unsigned int    ms_depth;                       /**< Depth (height) of the B-tree */
-       unsigned long   ms_branch_pages;        /**< Number of internal (non-leaf) pages */
-       unsigned long   ms_leaf_pages;          /**< Number of leaf pages */
-       unsigned long   ms_overflow_pages;      /**< Number of overflow pages */
-       unsigned long   ms_entries;                     /**< Number of data items */
+       size_t          ms_branch_pages;        /**< Number of internal (non-leaf) pages */
+       size_t          ms_leaf_pages;          /**< Number of leaf pages */
+       size_t          ms_overflow_pages;      /**< Number of overflow pages */
+       size_t          ms_entries;                     /**< Number of data items */
 } MDB_stat;
 
        /** Return the mdb library version information.
@@ -256,7 +280,8 @@ int  mdb_env_create(MDB_env **env);
        /** Open an environment handle.
         * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
         * @param[in] env An environment handle returned by #mdb_env_create()
-        * @param[in] path The directory in which the database files reside
+        * @param[in] path The directory in which the database files reside. This
+        * directory must already exist and be writable.
         * @param[in] flags Special options for this environment. This parameter
         * must be set to 0 or by bitwise OR'ing together one or more of the
         * values described here.
@@ -280,10 +305,9 @@ int  mdb_env_create(MDB_env **env);
         *              at any time using #mdb_env_set_flags().
         *      <li>#MDB_RDONLY
         *              Open the environment in read-only mode. No write operations will be allowed.
-        *      <li>#MDB_CREATE
-        *              Create the environment if it doesn't already exist.
         * </ul>
-        * @param[in] mode The UNIX permissions to set on created files.
+        * @param[in] mode The UNIX permissions to set on created files. This parameter
+        * is ignored on Windows.
         * @return A non-zero error value on failure and 0 on success. Some possible
         * errors are:
         * <ul>
@@ -372,7 +396,8 @@ int  mdb_env_get_path(MDB_env *env, const char **path);
 
        /** Set the size of the memory map to use for this environment.
         * The size should be a multiple of the OS page size. The default is
-        * 10485760 bytes. The value should be chosen as large as possible,
+        * 10485760 bytes. The size of the memory map is also the maximum size
+        * of the database. The value should be chosen as large as possible,
         * to accomodate future growth of the database.
         * This function may only be called after #mdb_env_create() and before #mdb_env_open().
         * @param[in] env An environment handle returned by #mdb_env_create()
@@ -397,7 +422,7 @@ int  mdb_env_set_mapsize(MDB_env *env, size_t size);
         *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
         * </ul>
         */
-int  mdb_env_set_maxreaders(MDB_env *env, int readers);
+int  mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
 
        /** Get the maximum number of threads for the environment.
         * @param[in] env An environment handle returned by #mdb_env_create()
@@ -408,7 +433,7 @@ int  mdb_env_set_maxreaders(MDB_env *env, int readers);
         *      <li>EINVAL - an invalid parameter was specified.
         * </ul>
         */
-int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
+int  mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
 
        /** Set the maximum number of databases for the environment.
         * This function is only needed if multiple databases will be used in the
@@ -423,7 +448,7 @@ int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
         *      <li>EINVAL - an invalid parameter was specified, or the environment is already open.
         * </ul>
         */
-int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
+int  mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
 
        /** Create a transaction for use with the environment.
         * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
@@ -477,7 +502,7 @@ void mdb_txn_abort(MDB_txn *txn);
        /** Reset a read-only transaction.
         * This releases the current reader lock but doesn't free the
         * transaction handle, allowing it to be used again later by #mdb_txn_renew().
-        * It otherwise has the same affect as #mdb_txn_abort() but saves some memory
+        * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
         * allocation/deallocation overhead if a thread is going to start a new
         * read-only transaction again soon.
         * All cursors opened within the transaction must be closed before the transaction
@@ -523,11 +548,9 @@ int  mdb_txn_renew(MDB_txn *txn);
         *              keys may have multiple data items, stored in sorted order.) By default
         *              keys must be unique and may have only a single data item.
         *      <li>#MDB_INTEGERKEY
-        *              Keys are binary integers in native byte order. On Big-Endian systems
-        *              this flag has no effect. On Little-Endian systems this flag behaves
-        *              the same as #MDB_REVERSEKEY. This flag is simply provided as a
-        *              convenience so that applications don't need to detect Endianness themselves
-        *              when using integers as keys.
+        *              Keys are binary integers in native byte order. Setting this option
+        *              requires all keys to be the same size, typically sizeof(int)
+        *              or sizeof(size_t).
         *      <li>#MDB_DUPFIXED
         *              This flag may only be used in combination with #MDB_DUPSORT. This option
         *              tells the library that the data items for this database are all the same
@@ -537,6 +560,9 @@ int  mdb_txn_renew(MDB_txn *txn);
         *      <li>#MDB_INTEGERDUP
         *              This option specifies that duplicate data items are also integers, and
         *              should be sorted as such.
+        *      <li>#MDB_REVERSEDUP
+        *              This option specifies that duplicate data items should be compared as
+        *              strings in reverse order.
         *      <li>#MDB_CREATE
         *              Create the named database if it doesn't exist. This option is not
         *              allowed in a read-only transaction or a read-only environment.
@@ -578,8 +604,8 @@ void mdb_close(MDB_txn *txn, MDB_dbi dbi);
         * with #mdb_open(), the keys are compared lexically, with shorter keys collating
         * before longer keys.
         * @warning This function must be called before any data access functions are used,
-        * otherwise data corruption may occur. The same function must be used by every
-        * process accessing the database, every time the database is used.
+        * otherwise data corruption may occur. The same comparison function must be used by every
+        * program accessing the database, every time the database is used.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_open()
         * @param[in] cmp A #MDB_cmp_func function
@@ -600,8 +626,8 @@ int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
         * with #mdb_open(), the data items are compared lexically, with shorter items collating
         * before longer items.
         * @warning This function must be called before any data access functions are used,
-        * otherwise data corruption may occur. The same function must be used by every
-        * process accessing the database, every time the database is used.
+        * otherwise data corruption may occur. The same comparison function must be used by every
+        * program accessing the database, every time the database is used.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_open()
         * @param[in] cmp A #MDB_cmp_func function
@@ -632,6 +658,21 @@ int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
         */
 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
 
+       /** Set a context pointer for a #MDB_FIXEDMAP database's relocation function.
+        * See #mdb_set_relfunc and #MDB_rel_func for more details.
+        * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+        * @param[in] dbi A database handle returned by #mdb_open()
+        * @param[in] ctx An arbitrary pointer for whatever the application needs.
+        * It will be passed to the callback function set by #mdb_set_relfunc
+        * as its \b relctx parameter whenever the callback is invoked.
+        * @return A non-zero error value on failure and 0 on success. Some possible
+        * errors are:
+        * <ul>
+        *      <li>EINVAL - an invalid parameter was specified.
+        * </ul>
+        */
+int  mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
+
        /** Get items from a database.
         * This function retrieves key/data pairs from the database. The address
         * and length of the data associated with the specified \b key are returned
@@ -639,6 +680,11 @@ int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
         * If the database supports duplicate keys (#MDB_DUPSORT) then the
         * first data item for the key will be returned. Retrieval of other
         * items requires the use of #mdb_cursor_get().
+        *
+        * @note The memory pointed to by the returned values is owned by the
+        * 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.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_open()
         * @param[in] key The key to search for in the database
@@ -660,7 +706,7 @@ int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_open()
         * @param[in] key The key to store in the database
-        * @param[in] data The data to store
+        * @param[in,out] data The data to store
         * @param[in] flags Special options for this operation. This parameter
         * must be set to 0 or by bitwise OR'ing together one or more of the
         * values described here.
@@ -673,7 +719,8 @@ int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
         *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
         *              does not already appear in the database. The function will return
         *              #MDB_KEYEXIST if the key already appears in the database, even if
-        *              the database supports duplicates (#MDB_DUPSORT).
+        *              the database supports duplicates (#MDB_DUPSORT). The \b data
+        *              parameter will be set to point to the existing item.
         * </ul>
         * @return A non-zero error value on failure and 0 on success. Some possible
         * errors are:
@@ -712,8 +759,6 @@ int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
        /** Create a cursor handle.
         * Cursors are associated with a specific transaction and database and
         * may not span threads.
-        * @todo Cursors only support read operations. Support for cursor_put() and
-        * cursor_del() needs to be added.
         * @param[in] txn A transaction handle returned by #mdb_txn_begin()
         * @param[in] dbi A database handle returned by #mdb_open()
         * @param[out] cursor Address where the new #MDB_cursor handle will be stored
@@ -754,6 +799,58 @@ void mdb_cursor_close(MDB_cursor *cursor);
 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
                            MDB_cursor_op op);
 
+       /** Store by cursor.
+        * This function stores key/data pairs into the database.
+        * If the function fails for any reason, the state of the cursor will be
+        * unchanged. If the function succeeds and an item is inserted into the
+        * database, the cursor is always positioned to refer to the newly inserted item.
+        * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
+        * @param[in] key The key operated on.
+        * @param[in] data The data operated on.
+        * @param[in] flags Options for this operation. This parameter
+        * must be set to 0 or one of the values described here.
+        * <ul>
+        *      <li>#MDB_CURRENT - overwrite the data of the key/data pair to which
+        *              the cursor refers with the specified data item. The \b key
+        *              parameter is ignored.
+        *      <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
+        *              already appear in the database. This flag may only be specified
+        *              if the database was opened with #MDB_DUPSORT. The function will
+        *              return #MDB_KEYEXIST if the key/data pair already appears in the
+        *              database.
+        *      <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
+        *              does not already appear in the database. The function will return
+        *              #MDB_KEYEXIST if the key already appears in the database, even if
+        *              the database supports duplicates (#MDB_DUPSORT).
+        * </ul>
+        * @return A non-zero error value on failure and 0 on success. Some possible
+        * errors are:
+        * <ul>
+        *      <li>EACCES - an attempt was made to modify a read-only database.
+        *      <li>EINVAL - an invalid parameter was specified.
+        * </ul>
+        */
+int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
+                               unsigned int flags);
+
+       /**  Delete current key/data pair
+        * This function deletes the key/data pair to which the cursor refers.
+        * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
+        * @param[in] flags Options for this operation. This parameter
+        * must be set to 0 or one of the values described here.
+        * <ul>
+        *      <li>#MDB_NODUPDATA - delete all of the data items for the current key.
+        *              This flag may only be specified if the database was opened with #MDB_DUPSORT.
+        * </ul>
+        * @return A non-zero error value on failure and 0 on success. Some possible
+        * errors are:
+        * <ul>
+        *      <li>EACCES - an attempt was made to modify a read-only database.
+        *      <li>EINVAL - an invalid parameter was specified.
+        * </ul>
+        */
+int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
+
        /** Return count of duplicates for current key.
         * This call is only valid on databases that support sorted duplicate
         * data items #MDB_DUPSORT.
@@ -765,7 +862,7 @@ int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
         *      <li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
         * </ul>
         */
-int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
+int  mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
 
        /** Compare two data items according to a particular database.
         * This returns a comparison as if the two data items were keys in the
@@ -778,4 +875,15 @@ int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
         */
 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
 
+       /** 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.
+        * @param[in] txn A transaction handle returned by #mdb_txn_begin()
+        * @param[in] dbi A database handle returned by #mdb_open()
+        * @param[in] a The first item to compare
+        * @param[in] b The second item to compare
+        * @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);
+/**    @} */
 #endif /* _MDB_H_ */