diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index e91233f..143b5a1 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -704,6 +704,8 @@ typedef struct MDB_dbx { * Every operation requires a transaction handle. */ struct MDB_txn { + MDB_txn *mt_parent; /**< parent of a nested txn */ + MDB_txn *mt_child; /**< nested txn under this txn */ pgno_t mt_next_pgno; /**< next unallocated page */ /** The ID of this transaction. IDs are integers incrementing from 1. * Only committed write transactions increment the ID. If a transaction @@ -994,6 +996,21 @@ mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b) return EINVAL; /* too bad you can't distinguish this from a valid result */ } +/** Allocate a single page. + * Re-use old malloc'd pages first, otherwise just malloc. + */ +static MDB_page * +mdb_page_malloc(MDB_cursor *mc) { + MDB_page *ret; + if (mc->mc_txn->mt_env->me_dpages) { + ret = mc->mc_txn->mt_env->me_dpages; + mc->mc_txn->mt_env->me_dpages = ret->mp_next; + } else { + ret = malloc(mc->mc_txn->mt_env->me_psize); + } + return ret; +} + /** Allocate pages for writing. * If there are free pages available from older transactions, they * will be re-used first. Otherwise a new page will be allocated. @@ -1142,12 +1159,40 @@ mdb_page_touch(MDB_cursor *mc) mp->mp_pgno = pgno; mp->mp_flags |= P_DIRTY; +finish: mc->mc_pg[mc->mc_top] = mp; /** If this page has a parent, update the parent to point to * this new page. */ if (mc->mc_top) SETPGNO(NODEPTR(mc->mc_pg[mc->mc_top-1], mc->mc_ki[mc->mc_top-1]), mp->mp_pgno); + else + mc->mc_db->md_root = mp->mp_pgno; + } else if (mc->mc_txn->mt_parent) { + MDB_page *np; + ID2 mid; + /* If txn has a parent, make sure the page is in our + * dirty list. + */ + if (mc->mc_txn->mt_u.dirty_list[0].mid) { + unsigned x = mdb_mid2l_search(mc->mc_txn->mt_u.dirty_list, mp->mp_pgno); + if (x <= mc->mc_txn->mt_u.dirty_list[0].mid && + mc->mc_txn->mt_u.dirty_list[x].mid == mp->mp_pgno) { + if (mc->mc_txn->mt_u.dirty_list[x].mptr != mp) { + mp = mc->mc_txn->mt_u.dirty_list[x].mptr; + mc->mc_pg[mc->mc_top] = mp; + } + return 0; + } + } + /* No - copy it */ + np = mdb_page_malloc(mc); + memcpy(np, mp, mc->mc_txn->mt_env->me_psize); + mid.mid = np->mp_pgno; + mid.mptr = np; + mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &mid); + mp = np; + goto finish; } return 0; } @@ -1264,7 +1309,7 @@ mdb_txn_renew(MDB_txn *txn) } int -mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **ret) +mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) { MDB_txn *txn; int rc; @@ -1273,6 +1318,12 @@ mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **ret) DPUTS("environment had fatal error, must shutdown!"); return MDB_PANIC; } + if (parent) { + /* parent already has an active child txn */ + if (parent->mt_child) { + return EINVAL; + } + } if ((txn = calloc(1, sizeof(MDB_txn) + env->me_maxdbs * (sizeof(MDB_db)+1))) == NULL) { DPRINTF("calloc: %s", strerror(ErrCode())); @@ -1285,7 +1336,33 @@ mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **ret) txn->mt_dbflags = (unsigned char *)(txn->mt_dbs + env->me_maxdbs); txn->mt_env = env; - rc = mdb_txn_renew0(txn); + if (parent) { + txn->mt_free_pgs = mdb_midl_alloc(); + if (!txn->mt_free_pgs) { + free(txn); + return ENOMEM; + } + txn->mt_u.dirty_list = malloc(sizeof(ID2)*MDB_IDL_UM_SIZE); + if (!txn->mt_u.dirty_list) { + free(txn->mt_free_pgs); + free(txn); + return ENOMEM; + } + txn->mt_txnid = parent->mt_txnid; + txn->mt_toggle = parent->mt_toggle; + txn->mt_u.dirty_list[0].mid = 0; + txn->mt_free_pgs[0] = 0; + txn->mt_next_pgno = parent->mt_next_pgno; + parent->mt_child = txn; + txn->mt_parent = parent; + txn->mt_numdbs = parent->mt_numdbs; + txn->mt_dbxs = parent->mt_dbxs; + memcpy(txn->mt_dbs, parent->mt_dbs, txn->mt_numdbs * sizeof(MDB_db)); + memcpy(txn->mt_dbflags, parent->mt_dbflags, txn->mt_numdbs); + rc = 0; + } else { + rc = mdb_txn_renew0(txn); + } if (rc) free(txn); else { @@ -1313,9 +1390,6 @@ mdb_txn_reset0(MDB_txn *txn) MDB_page *dp; unsigned int i; - if (mdb_midl_shrink(&txn->mt_free_pgs)) - env->me_free_pgs = txn->mt_free_pgs; - /* return all dirty pages to dpage list */ for (i=1; i<=txn->mt_u.dirty_list[0].mid; i++) { dp = txn->mt_u.dirty_list[i].mptr; @@ -1328,6 +1402,16 @@ mdb_txn_reset0(MDB_txn *txn) } } + if (txn->mt_parent) { + txn->mt_parent->mt_child = NULL; + free(txn->mt_free_pgs); + free(txn->mt_u.dirty_list); + return; + } else { + if (mdb_midl_shrink(&txn->mt_free_pgs)) + env->me_free_pgs = txn->mt_free_pgs; + } + while ((mop = txn->mt_env->me_pghead)) { txn->mt_env->me_pghead = mop->mo_next; free(mop); @@ -1362,6 +1446,9 @@ mdb_txn_abort(MDB_txn *txn) txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w', (void *)txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root); + if (txn->mt_child) + mdb_txn_abort(txn->mt_child); + mdb_txn_reset0(txn); free(txn); } @@ -1381,6 +1468,11 @@ mdb_txn_commit(MDB_txn *txn) assert(txn != NULL); assert(txn->mt_env != NULL); + if (txn->mt_child) { + mdb_txn_commit(txn->mt_child); + txn->mt_child = NULL; + } + env = txn->mt_env; if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) { @@ -1405,14 +1497,72 @@ mdb_txn_commit(MDB_txn *txn) return MDB_SUCCESS; } - if (txn != env->me_txn) { - DPUTS("attempt to commit unknown transaction"); + + if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) { + DPUTS("error flag is set, can't commit"); + if (txn->mt_parent) + txn->mt_parent->mt_flags |= MDB_TXN_ERROR; mdb_txn_abort(txn); return EINVAL; } - if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) { - DPUTS("error flag is set, can't commit"); + if (txn->mt_parent) { + MDB_db *ip, *jp; + MDB_dbi i; + unsigned x, y; + ID2L dst, src; + + /* Update parent's DB table */ + ip = &txn->mt_parent->mt_dbs[2]; + jp = &txn->mt_dbs[2]; + for (i = 2; i < txn->mt_numdbs; i++) { + if (ip->md_root != jp->md_root) + *ip = *jp; + ip++; jp++; + } + txn->mt_parent->mt_numdbs = txn->mt_numdbs; + + /* Append our free list to parent's */ + mdb_midl_append_list(&txn->mt_parent->mt_free_pgs, + txn->mt_free_pgs); + mdb_midl_free(txn->mt_free_pgs); + + /* Merge our dirty list with parent's */ + dst = txn->mt_parent->mt_u.dirty_list; + src = txn->mt_u.dirty_list; + x = mdb_mid2l_search(dst, src[1].mid); + for (y=1; y<=src[0].mid; y++) { + MDB_page *mp; + while (x <= dst[0].mid && dst[x].mid != src[y].mid) x++; + if (x > dst[0].mid) + break; + mp = dst[x].mptr; + dp = src[y].mptr; + mp->mp_lower = dp->mp_lower; + mp->mp_upper = dp->mp_upper; + if (IS_LEAF2(mp)) { + memcpy(METADATA(mp), METADATA(dp), NUMKEYS(mp) * mp->mp_pad); + } else { + memcpy((char *)mp+mp->mp_upper, (char *)dp+dp->mp_upper, + txn->mt_env->me_psize - mp->mp_upper); + } + free(src[y].mptr); + } + x = dst[0].mid; + for (; y<=src[0].mid; y++) { + if (++x >= MDB_IDL_UM_MAX) + return ENOMEM; + dst[x] = src[y]; + } + dst[0].mid = x; + free(txn->mt_u.dirty_list); + txn->mt_parent->mt_child = NULL; + free(txn); + return MDB_SUCCESS; + } + + if (txn != env->me_txn) { + DPUTS("attempt to commit unknown transaction"); mdb_txn_abort(txn); return EINVAL; } @@ -1892,6 +2042,8 @@ mdb_env_set_mapsize(MDB_env *env, size_t size) if (env->me_map) return EINVAL; env->me_mapsize = size; + if (env->me_psize) + env->me_maxpg = env->me_mapsize / env->me_psize; return MDB_SUCCESS; } @@ -2896,11 +3048,8 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int modify) mc->mc_dbi, root, mc->mc_pg[0]->mp_flags); if (modify) { - if (!F_ISSET(mc->mc_pg[0]->mp_flags, P_DIRTY)) { - if ((rc = mdb_page_touch(mc))) - return rc; - mc->mc_db->md_root = mc->mc_pg[0]->mp_pgno; - } + if ((rc = mdb_page_touch(mc))) + return rc; } return mdb_page_search_root(mc, key, modify); @@ -3545,15 +3694,9 @@ mdb_cursor_touch(MDB_cursor *mc) *mc->mc_dbflag = DB_DIRTY; } for (mc->mc_top = 0; mc->mc_top < mc->mc_snum; mc->mc_top++) { - if (!F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY)) { - rc = mdb_page_touch(mc); - if (rc) - return rc; - if (!mc->mc_top) { - mc->mc_db->md_root = - mc->mc_pg[mc->mc_top]->mp_pgno; - } - } + rc = mdb_page_touch(mc); + if (rc) + return rc; } mc->mc_top = mc->mc_snum-1; return MDB_SUCCESS; @@ -3578,7 +3721,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data, return EACCES; DPRINTF("==> put db %u key [%s], size %zu, data size %zu", - mc->mc_dbi, DKEY(key), key->mv_size, data->mv_size); + mc->mc_dbi, DKEY(key), key ? key->mv_size:0, data->mv_size); dkey.mv_size = 0; @@ -4969,13 +5112,9 @@ newsep: /* Move half of the keys to the right sibling. */ /* grab a page to hold a temporary copy */ - if (mc->mc_txn->mt_env->me_dpages) { - copy = mc->mc_txn->mt_env->me_dpages; - mc->mc_txn->mt_env->me_dpages = copy->mp_next; - } else { - if ((copy = malloc(mc->mc_txn->mt_env->me_psize)) == NULL) - return ENOMEM; - } + copy = mdb_page_malloc(mc); + if (copy == NULL) + return ENOMEM; copy->mp_pgno = mp->mp_pgno; copy->mp_flags = mp->mp_flags; @@ -5075,14 +5214,14 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi, return mdb_cursor_put(&mc, key, data, flags); } +/** Only a subset of the @ref mdb_env flags can be changed + * at runtime. Changing other flags requires closing the environment + * and re-opening it with the new flags. + */ +#define CHANGEABLE (MDB_NOSYNC) int mdb_env_set_flags(MDB_env *env, unsigned int flag, int onoff) { - /** Only a subset of the @ref mdb_env flags can be changed - * at runtime. Changing other flags requires closing the environment - * and re-opening it with the new flags. - */ -#define CHANGEABLE (MDB_NOSYNC) if ((flag & CHANGEABLE) != flag) return EINVAL; if (onoff) @@ -5340,7 +5479,7 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del) if (rc) return rc; - rc = mdb_drop0(mc, 1); + rc = mdb_drop0(mc, mc->mc_db->md_flags & MDB_DUPSORT); if (rc) mdb_cursor_close(mc); return rc; diff --git a/libraries/libmdb/mdb.h b/libraries/libmdb/mdb.h index 18d7368..1672d81 100644 --- a/libraries/libmdb/mdb.h +++ b/libraries/libmdb/mdb.h @@ -99,33 +99,39 @@ MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE) /** @} */ -/** Opaque structure for a database environment. A DB environment supports multiple - * databases, all residing in the same shared-memory map. +/** @brief 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. +/** @brief 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; -/** A handle for an individual database in the DB environment. */ +/** @brief A handle for an individual database in the DB environment. */ typedef unsigned int MDB_dbi; -/** Opaque structure for navigating through a database */ +/** @brief 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. */ +/** @brief 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 */ void *mv_data; /**< address of the data item */ } MDB_val; -/** A callback function used to compare two keys in a database */ +/** @brief A callback function used to compare two keys in a database */ 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. The \b newptr gives the item's desired address in +/** @brief A callback function used to relocate a position-dependent data item + * 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 @@ -183,7 +189,8 @@ typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *rel #define MDB_CURRENT 0x40 /* @} */ -/** Cursor Get operations +/** @brief Cursor Get operations. + * * This is the set of all operations for retrieving data * using a cursor. */ @@ -235,7 +242,7 @@ typedef enum MDB_cursor_op { #define MDB_VERSION_MISMATCH (-30794) /** @} */ -/** Statistics for a database in the environment */ +/** @brief 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. */ @@ -246,7 +253,8 @@ typedef struct MDB_stat { size_t ms_entries; /**< Number of data items */ } MDB_stat; - /** Return the mdb library version information. + /** @brief Return the mdb library version information. + * * @param[out] major if non-NULL, the library major version number is copied here * @param[out] minor if non-NULL, the library minor version number is copied here * @param[out] patch if non-NULL, the library patch version number is copied here @@ -254,7 +262,8 @@ typedef struct MDB_stat { */ char *mdb_version(int *major, int *minor, int *patch); - /** Return a string describing a given error code. + /** @brief Return a string describing a given error code. + * * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3) * function. If the error code is greater than or equal to 0, then the string * returned by the system function strerror(3) is returned. If the error code @@ -265,7 +274,8 @@ char *mdb_version(int *major, int *minor, int *patch); */ char *mdb_strerror(int err); - /** Create an MDB environment handle. + /** @brief Create an MDB environment handle. + * * This function allocates memory for a #MDB_env structure. To release * the allocated memory and discard the handle, call #mdb_env_close(). * Before the handle may be used, it must be opened using #mdb_env_open(). @@ -277,7 +287,8 @@ char *mdb_strerror(int err); */ int mdb_env_create(MDB_env **env); - /** Open an environment handle. + /** @brief 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. This @@ -321,14 +332,16 @@ int mdb_env_create(MDB_env **env); */ int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode); - /** Return statistics about the MDB environment. + /** @brief Return statistics about the MDB environment. + * * @param[in] env An environment handle returned by #mdb_env_create() * @param[out] stat The address of an #MDB_stat structure * where the statistics will be copied */ int mdb_env_stat(MDB_env *env, MDB_stat *stat); - /** Flush the data buffers to disk. + /** @brief Flush the data buffers to disk. + * * Data is always written to disk when #mdb_txn_commit() is called, * but the operating system may keep it buffered. MDB always flushes * the OS buffers upon commit as well, unless the environment was @@ -346,7 +359,8 @@ int mdb_env_stat(MDB_env *env, MDB_stat *stat); */ int mdb_env_sync(MDB_env *env, int force); - /** Close the environment and release the memory map. + /** @brief Close the environment and release the memory map. + * * Only a single thread may call this function. All transactions, databases, * and cursors must already be closed before calling this function. Attempts to * use any such handles after calling this function will cause a SIGSEGV. @@ -355,7 +369,8 @@ int mdb_env_sync(MDB_env *env, int force); */ void mdb_env_close(MDB_env *env); - /** Set environment flags. + /** @brief Set environment flags. + * * This may be used to set some flags that weren't already set during * #mdb_env_open(), or to unset these flags. Currently only the * #MDB_NOSYNC flag setting may be changed with this function. @@ -370,7 +385,8 @@ void mdb_env_close(MDB_env *env); */ int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff); - /** Get environment flags. + /** @brief Get environment flags. + * * @param[in] env An environment handle returned by #mdb_env_create() * @param[out] flags The address of an integer to store the flags * @return A non-zero error value on failure and 0 on success. Some possible @@ -381,7 +397,8 @@ int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff); */ int mdb_env_get_flags(MDB_env *env, unsigned int *flags); - /** Return the path that was used in #mdb_env_open(). + /** @brief Return the path that was used in #mdb_env_open(). + * * @param[in] env An environment handle returned by #mdb_env_create() * @param[out] path Address of a string pointer to contain the path. This * is the actual string in the environment, not a copy. It should not be @@ -394,7 +411,8 @@ int mdb_env_get_flags(MDB_env *env, unsigned int *flags); */ int mdb_env_get_path(MDB_env *env, const char **path); - /** Set the size of the memory map to use for this environment. + /** @brief 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 size of the memory map is also the maximum size * of the database. The value should be chosen as large as possible, @@ -410,7 +428,8 @@ int mdb_env_get_path(MDB_env *env, const char **path); */ int mdb_env_set_mapsize(MDB_env *env, size_t size); - /** Set the maximum number of threads for the environment. + /** @brief Set the maximum number of threads for the environment. + * * This defines the number of slots in the lock table that is used to track readers in the * the environment. The default is 126. * This function may only be called after #mdb_env_create() and before #mdb_env_open(). @@ -424,7 +443,8 @@ int mdb_env_set_mapsize(MDB_env *env, size_t size); */ int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers); - /** Get the maximum number of threads for the environment. + /** @brief Get the maximum number of threads for the environment. + * * @param[in] env An environment handle returned by #mdb_env_create() * @param[out] readers Address of an integer to store the number of readers * @return A non-zero error value on failure and 0 on success. Some possible @@ -435,7 +455,8 @@ int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers); */ int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers); - /** Set the maximum number of databases for the environment. + /** @brief Set the maximum number of databases for the environment. + * * This function is only needed if multiple databases will be used in the * environment. Simpler applications that only use a single database can ignore * this option. @@ -450,13 +471,19 @@ int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers); */ int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs); - /** Create a transaction for use with the environment. + /** @brief Create a transaction for use with the environment. + * * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit(). * @note Transactions may not span threads, a transaction must only be used by a * single thread. * @note Cursors may not span transactions; each cursor must be opened and closed * within a single transaction. * @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. * @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. @@ -474,9 +501,10 @@ int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs); * the reader lock table is full. See #mdb_env_set_maxreaders(). * */ -int mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn); +int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn); - /** Commit all the operations of a transaction into the database. + /** @brief Commit all the operations of a transaction into the database. + * * All cursors opened within the transaction must be closed before the transaction * is committed. * The transaction handle will be freed and must not be used again after this call. @@ -491,7 +519,8 @@ int mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn); */ int mdb_txn_commit(MDB_txn *txn); - /** Abandon all the operations of the transaction instead of saving them. + /** @brief Abandon all the operations of the transaction instead of saving them. + * * All cursors opened within the transaction must be closed before the transaction * is aborted. * The transaction handle will be freed and must not be used again after this call. @@ -499,7 +528,8 @@ int mdb_txn_commit(MDB_txn *txn); */ void mdb_txn_abort(MDB_txn *txn); - /** Reset a read-only transaction. + /** @brief 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 effect as #mdb_txn_abort() but saves some memory @@ -515,7 +545,8 @@ void mdb_txn_abort(MDB_txn *txn); */ void mdb_txn_reset(MDB_txn *txn); - /** Renew a read-only transaction. + /** @brief Renew a read-only transaction. + * * This acquires a new reader lock for a transaction handle that had been * released by #mdb_txn_reset(). It must be called before a reset transaction * may be used again. @@ -530,7 +561,8 @@ void mdb_txn_reset(MDB_txn *txn); */ int mdb_txn_renew(MDB_txn *txn); - /** Open a database in the environment. + /** @brief Open a database in the environment. + * * The database handle may be discarded by calling #mdb_close(). Only * one thread should call this function; it is not mutex-protected in * a read-only transaction. @@ -580,7 +612,8 @@ int mdb_txn_renew(MDB_txn *txn); */ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi); - /** Retrieve statistics for a database. + /** @brief Retrieve statistics for a database. + * * @param[in] txn A transaction handle returned by #mdb_txn_begin() * @param[in] dbi A database handle returned by #mdb_open() * @param[out] stat The address of an #MDB_stat structure @@ -593,7 +626,8 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi); */ int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat); - /** Close a database handle. + /** @brief Close a database handle. + * * This call is not mutex protected. Handles should only be closed by * a single thread, and only if no other threads are going to reference * the database handle any further. @@ -602,7 +636,8 @@ int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat); */ void mdb_close(MDB_env *env, MDB_dbi dbi); - /** Delete a database and/or free all its pages. + /** @brief Delete a database and/or free all its pages. + * * If the \b del parameter is non-zero the DB handle will be closed * and the DB will be deleted. * @param[in] txn A transaction handle returned by #mdb_txn_begin() @@ -613,7 +648,8 @@ void mdb_close(MDB_env *env, MDB_dbi dbi); */ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del); - /** Set a custom key comparison function for a database. + /** @brief Set a custom key comparison function for a database. + * * The comparison function is called whenever it is necessary to compare a * key specified by the application with a key currently stored in the database. * If no comparison function is specified, and no speAGAINcial key flags were specified @@ -633,7 +669,8 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del); */ int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp); - /** Set a custom data comparison function for a #MDB_DUPSORT database. + /** @brief Set a custom data comparison function for a #MDB_DUPSORT database. + * * This comparison function is called whenever it is necessary to compare a data * item specified by the application with a data item currently stored in the database. * This function only takes effect if the database was opened with the #MDB_DUPSORT @@ -655,7 +692,8 @@ int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp); */ int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp); - /** Set a relocation function for a #MDB_FIXEDMAP database. + /** @brief Set a relocation function for a #MDB_FIXEDMAP database. + * * @todo The relocation function is called whenever it is necessary to move the data * of an item to a different position in the database (e.g. through tree * balancing operations, shifts as a result of adds or deletes, etc.). It is @@ -674,7 +712,8 @@ 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. + /** @brief 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() @@ -689,7 +728,8 @@ int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel); */ int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx); - /** Get items from a database. + /** @brief 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 * in the structure to which \b data refers. @@ -714,7 +754,8 @@ int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx); */ int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); - /** Store items into a database. + /** @brief Store items into a database. + * * This function stores key/data pairs in the database. The default behavior * is to enter the new key/data pair, replacing any previously existing key * if duplicates are disallowed, or adding a duplicate data item if @@ -749,7 +790,8 @@ int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned int flags); - /** Delete items from a database. + /** @brief Delete items from a database. + * * This function removes key/data pairs from the database. * If the database does not support sorted duplicate data items * (#MDB_DUPSORT) the data parameter is ignored. @@ -772,7 +814,8 @@ int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, */ int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); - /** Create a cursor handle. + /** @brief Create a cursor handle. + * * Cursors are associated with a specific transaction and database and * may not span threads. * @param[in] txn A transaction handle returned by #mdb_txn_begin() @@ -786,13 +829,15 @@ int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); */ int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor); - /** Close a cursor handle. + /** @brief Close a cursor handle. + * * The cursor handle will be freed and must not be used again after this call. * @param[in] cursor A cursor handle returned by #mdb_cursor_open() */ void mdb_cursor_close(MDB_cursor *cursor); - /** Retrieve by cursor. + /** @brief Retrieve by cursor. + * * This function retrieves key/data pairs from the database. The address and length * of the key are returned in the object to which \b key refers (except for the * case of the #MDB_SET option, in which the \b key object is unchanged), and @@ -815,7 +860,8 @@ 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. + /** @brief 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 @@ -849,7 +895,8 @@ int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, unsigned int flags); - /** Delete current key/data pair + /** @brief 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 @@ -867,7 +914,8 @@ int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, */ int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags); - /** Return count of duplicates for current key. + /** @brief Return count of duplicates for current key. + * * This call is only valid on databases that support sorted duplicate * data items #MDB_DUPSORT. * @param[in] cursor A cursor handle returned by #mdb_cursor_open() @@ -880,7 +928,8 @@ int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags); */ int mdb_cursor_count(MDB_cursor *cursor, size_t *countp); - /** Compare two data items according to a particular database. + /** @brief Compare two data items according to a particular database. + * * This returns a comparison as if the two data items were keys in the * specified database. * @param[in] txn A transaction handle returned by #mdb_txn_begin() @@ -891,7 +940,8 @@ int mdb_cursor_count(MDB_cursor *cursor, size_t *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. + /** @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. * @param[in] txn A transaction handle returned by #mdb_txn_begin() diff --git a/libraries/libmdb/mdb_stat.c b/libraries/libmdb/mdb_stat.c index 931ec61..a5f484d 100644 --- a/libraries/libmdb/mdb_stat.c +++ b/libraries/libmdb/mdb_stat.c @@ -38,7 +38,7 @@ int main(int argc,char * argv[]) printf("mdb_env_open failed, error %d\n", rc); exit(1); } - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); if (rc) { printf("mdb_txn_begin failed, error %d\n", rc); exit(1); diff --git a/libraries/libmdb/midl.c b/libraries/libmdb/midl.c index 30f7410..500811f 100644 --- a/libraries/libmdb/midl.c +++ b/libraries/libmdb/midl.c @@ -160,6 +160,25 @@ int mdb_midl_append( IDL *idp, ID id ) return 0; } +int mdb_midl_append_list( IDL *idp, IDL app ) +{ + IDL ids = *idp; + /* Too big? */ + if (ids[0] + app[0] >= ids[-1]) { + IDL idn = ids-1; + /* grow it */ + idn = realloc(idn, (*idn + app[-1]) * sizeof(ID)); + if (!idn) + return -1; + *idn++ += app[-1]; + ids = idn; + *idp = ids; + } + memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(ID)); + ids[0] += app[0]; + return 0; +} + /* Quicksort + Insertion sort for small arrays */ #define SMALL 8 diff --git a/libraries/libmdb/midl.h b/libraries/libmdb/midl.h index f3fb3a0..4c0e615 100644 --- a/libraries/libmdb/midl.h +++ b/libraries/libmdb/midl.h @@ -136,6 +136,13 @@ int mdb_midl_shrink(IDL *idp); */ int mdb_midl_append( IDL *idp, ID id ); + /** Append an IDL onto an IDL. + * @param[in,out] idp Address of the IDL to append to. + * @param[in] app The IDL to append. + * @return 0 on success, -1 if the IDL is too large. + */ +int mdb_midl_append_list( IDL *idp, IDL app ); + /** Sort an IDL. * @param[in,out] ids The IDL to sort. */ diff --git a/libraries/libmdb/mtest.c b/libraries/libmdb/mtest.c index 89f0ac4..bb5ec81 100644 --- a/libraries/libmdb/mtest.c +++ b/libraries/libmdb/mtest.c @@ -42,7 +42,7 @@ int main(int argc,char * argv[]) rc = mdb_env_create(&env); rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, NULL, 0, &dbi); key.mv_size = sizeof(int); @@ -60,7 +60,7 @@ int main(int argc,char * argv[]) rc = mdb_txn_commit(txn); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", @@ -75,7 +75,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(sval, "%03x ", values[i]); rc = mdb_del(txn, dbi, &key, NULL); if (rc) { @@ -89,7 +89,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { @@ -108,7 +108,7 @@ int main(int argc,char * argv[]) * this stuff all breaks */ printf("Deleting with cursor\n"); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_cursor_open(db, txn, &cur2); for (i=0; i<50; i++) { rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT); diff --git a/libraries/libmdb/mtest2.c b/libraries/libmdb/mtest2.c index 17c1ecf..a0e9914 100644 --- a/libraries/libmdb/mtest2.c +++ b/libraries/libmdb/mtest2.c @@ -46,7 +46,7 @@ int main(int argc,char * argv[]) rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_set_maxdbs(env, 4); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, "id1", MDB_CREATE, &dbi); key.mv_size = sizeof(int); @@ -64,7 +64,7 @@ int main(int argc,char * argv[]) rc = mdb_txn_commit(txn); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", @@ -79,7 +79,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(sval, "%03x ", values[i]); rc = mdb_del(txn, dbi, &key, NULL); if (rc) { @@ -93,7 +93,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { diff --git a/libraries/libmdb/mtest3.c b/libraries/libmdb/mtest3.c index 43e3481..eb9723b 100644 --- a/libraries/libmdb/mtest3.c +++ b/libraries/libmdb/mtest3.c @@ -49,7 +49,7 @@ int main(int argc,char * argv[]) rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_set_maxdbs(env, 4); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi); key.mv_size = sizeof(int); @@ -69,7 +69,7 @@ int main(int argc,char * argv[]) rc = mdb_txn_commit(txn); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", @@ -84,7 +84,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); @@ -103,7 +103,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { diff --git a/libraries/libmdb/mtest4.c b/libraries/libmdb/mtest4.c index 821055a..b2f5d93 100644 --- a/libraries/libmdb/mtest4.c +++ b/libraries/libmdb/mtest4.c @@ -47,7 +47,7 @@ int main(int argc,char * argv[]) rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_set_maxdbs(env, 4); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi); key.mv_size = sizeof(int); @@ -68,7 +68,7 @@ int main(int argc,char * argv[]) /* there should be one full page of dups now. */ - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", @@ -90,22 +90,22 @@ int main(int argc,char * argv[]) data.mv_data = sval; sprintf(sval, "%07x", values[3]+1); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA); mdb_txn_abort(txn); sprintf(sval, "%07x", values[255]+1); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA); mdb_txn_abort(txn); sprintf(sval, "%07x", values[500]+1); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA); rc = mdb_txn_commit(txn); /* Try MDB_NEXT_MULTIPLE */ - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 0) { printf("key: %.*s, data: %.*s\n", @@ -119,7 +119,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%3)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(sval, "%07x", values[i]); key.mv_size = sizeof(int); key.mv_data = kval; @@ -137,7 +137,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { diff --git a/libraries/libmdb/mtest5.c b/libraries/libmdb/mtest5.c index 5fb6bb0..c63402e 100644 --- a/libraries/libmdb/mtest5.c +++ b/libraries/libmdb/mtest5.c @@ -49,7 +49,7 @@ int main(int argc,char * argv[]) rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_set_maxdbs(env, 4); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi); rc = mdb_cursor_open(txn, dbi, &cursor); @@ -71,7 +71,7 @@ int main(int argc,char * argv[]) rc = mdb_txn_commit(txn); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", @@ -86,7 +86,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); @@ -105,7 +105,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { diff --git a/libraries/libmdb/mtest6.c b/libraries/libmdb/mtest6.c index 36ad267..a5bf3a2 100644 --- a/libraries/libmdb/mtest6.c +++ b/libraries/libmdb/mtest6.c @@ -42,7 +42,7 @@ int main(int argc,char * argv[]) rc = mdb_env_set_mapsize(env, 10485760); rc = mdb_env_set_maxdbs(env, 4); rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664); - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); rc = mdb_open(txn, "id2", MDB_CREATE|MDB_INTEGERKEY, &dbi); rc = mdb_cursor_open(txn, dbi, &cursor); rc = mdb_stat(txn, dbi, &mst); @@ -87,7 +87,7 @@ int main(int argc,char * argv[]) for (i= count - 1; i > -1; i-= (random()%5)) { j++; txn=NULL; - rc = mdb_txn_begin(env, 0, &txn); + rc = mdb_txn_begin(env, NULL, 0, &txn); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); @@ -106,7 +106,7 @@ int main(int argc,char * argv[]) printf("Deleted %d values\n", j); rc = mdb_env_stat(env, &mst); - rc = mdb_txn_begin(env, 1, &txn); + rc = mdb_txn_begin(env, NULL, 1, &txn); rc = mdb_cursor_open(txn, dbi, &cursor); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {