pgno_t md_root; /**< the root page of this tree */
} MDB_db;
+ /** mdb_dbi_open flags */
+#define PERSISTENT_FLAGS 0x7fff
+#define VALID_FLAGS (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
+ MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
+
/** Handle for the DB used to track free pages. */
#define FREE_DBI 0
/** Handle for the default DB. */
*/
#define DB_DIRTY 0x01 /**< DB was written in this txn */
#define DB_STALE 0x02 /**< DB record is older than txnID */
+#define DB_NEW 0x04 /**< DB handle opened in this txn */
+#define DB_VALID 0x08 /**< DB handle is valid */
+#define MDB_VALID 0x8000 /**< DB handle is valid, for me_dbflags */
/** @} */
/** In write txns, array of cursors for each DB */
MDB_cursor **mt_cursors;
{
MDB_env *env = txn->mt_env;
unsigned int i;
+ uint16_t x;
int rc;
/* Setup db info */
/* Copy the DB info and flags */
memcpy(txn->mt_dbs, env->me_metas[txn->mt_toggle]->mm_dbs, 2 * sizeof(MDB_db));
- for (i=2; i<txn->mt_numdbs; i++)
- txn->mt_dbs[i].md_flags = env->me_dbflags[i];
- txn->mt_dbflags[0] = txn->mt_dbflags[1] = 0;
- if (txn->mt_numdbs > 2)
- memset(txn->mt_dbflags+2, DB_STALE, txn->mt_numdbs-2);
+ for (i=2; i<txn->mt_numdbs; i++) {
+ txn->mt_dbs[i].md_flags = x = env->me_dbflags[i];
+ txn->mt_dbflags[i] = (x & MDB_VALID) ? DB_VALID|DB_STALE : 0;
+ }
+ txn->mt_dbflags[0] = txn->mt_dbflags[1] = DB_VALID;
if (env->me_maxpg < txn->mt_next_pgno) {
mdb_txn_reset0(txn);
txn->mt_env = env;
if (parent) {
+ unsigned int i;
txn->mt_free_pgs = mdb_midl_alloc();
if (!txn->mt_free_pgs) {
free(txn);
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);
+ /* Copy parent's mt_dbflags, but clear DB_NEW */
+ for (i=0; i<txn->mt_numdbs; i++)
+ txn->mt_dbflags[i] = parent->mt_dbflags[i] & ~DB_NEW;
rc = 0;
ntxn = (MDB_ntxn *)txn;
ntxn->mnt_pgstate = env->me_pgstate; /* save parent me_pghead & co */
mdb_txn_reset0(MDB_txn *txn)
{
MDB_env *env = txn->mt_env;
+ unsigned int i;
+
+ /* Close any DBI handles opened in this txn */
+ for (i=2; i<txn->mt_numdbs; i++) {
+ if (txn->mt_dbflags[i] & DB_NEW) {
+ char *ptr = env->me_dbxs[i].md_name.mv_data;
+ env->me_dbxs[i].md_name.mv_data = NULL;
+ env->me_dbxs[i].md_name.mv_size = 0;
+ free(ptr);
+ }
+ }
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
if (!(env->me_flags & MDB_ROFS))
txn->mt_u.reader->mr_txnid = (txnid_t)-1;
} else {
MDB_page *dp;
- unsigned int i;
/* close(free) all cursors */
for (i=0; i<txn->mt_numdbs; i++) {
env = txn->mt_env;
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
- if (txn->mt_numdbs > env->me_numdbs) {
- /* update the DB flags */
- MDB_dbi i;
- for (i = env->me_numdbs; i<txn->mt_numdbs; i++)
- env->me_dbflags[i] = txn->mt_dbs[i].md_flags;
- env->me_numdbs = i;
+ /* update the DB flags */
+ for (i = 2; i<txn->mt_numdbs; i++) {
+ if (txn->mt_dbflags[i] & DB_NEW)
+ env->me_dbflags[i] = txn->mt_dbs[i].md_flags | MDB_VALID;
}
+ if (txn->mt_numdbs > env->me_numdbs)
+ env->me_numdbs = txn->mt_numdbs;
+ txn->mt_numdbs = 2; /* so txn_abort() doesn't close any new handles */
mdb_txn_abort(txn);
return MDB_SUCCESS;
}
/* Update parent's DB table. */
memcpy(parent->mt_dbs, txn->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
- memcpy(parent->mt_dbflags, txn->mt_dbflags, txn->mt_numdbs);
txn->mt_parent->mt_numdbs = txn->mt_numdbs;
+ txn->mt_parent->mt_dbflags[0] = txn->mt_dbflags[0];
+ txn->mt_parent->mt_dbflags[1] = txn->mt_dbflags[1];
+ for (i=2; i<txn->mt_numdbs; i++) {
+ /* preserve parent's DB_NEW status */
+ x = txn->mt_parent->mt_dbflags[i] & DB_NEW;
+ txn->mt_parent->mt_dbflags[i] = txn->mt_dbflags[i] | x;
+ }
dst = txn->mt_parent->mt_u.dirty_list;
src = txn->mt_u.dirty_list;
done:
env->me_pglast = 0;
env->me_txn = NULL;
- if (txn->mt_numdbs > env->me_numdbs) {
- /* update the DB flags */
- MDB_dbi i;
- for (i = env->me_numdbs; i<txn->mt_numdbs; i++)
- env->me_dbflags[i] = txn->mt_dbs[i].md_flags;
- env->me_numdbs = i;
+ /* update the DB flags */
+ for (i = 2; i<txn->mt_numdbs; i++) {
+ if (txn->mt_dbflags[i] & DB_NEW)
+ env->me_dbflags[i] = txn->mt_dbs[i].md_flags | MDB_VALID;
}
+ if (txn->mt_numdbs > env->me_numdbs)
+ env->me_numdbs = txn->mt_numdbs;
UNLOCK_MUTEX_W(env);
free(txn);
/* The txn may not know this DBI, or another process may
* have dropped and recreated the DB with other flags.
*/
- if (mc->mc_db->md_flags != flags)
+ if ((mc->mc_db->md_flags & PERSISTENT_FLAGS) != flags)
return MDB_INCOMPATIBLE;
memcpy(mc->mc_db, data.mv_data, sizeof(MDB_db));
}
if (flags & MDB_PS_MODIFY)
dbflag = DB_DIRTY;
- *mc->mc_dbflag = dbflag;
+ *mc->mc_dbflag &= ~DB_STALE;
+ *mc->mc_dbflag |= dbflag;
}
}
root = mc->mc_db->md_root;
assert(data);
DPRINTF("===> get db %u key [%s]", dbi, DKEY(key));
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
rc = mdb_page_search(&mc2, &mc->mc_dbx->md_name, MDB_PS_MODIFY);
if (rc)
return rc;
- *mc->mc_dbflag = DB_DIRTY;
+ *mc->mc_dbflag |= DB_DIRTY;
}
for (mc->mc_top = 0; mc->mc_top < mc->mc_snum; mc->mc_top++) {
rc = mdb_page_touch(mc);
mdb_cursor_push(mc, np);
mc->mc_db->md_root = np->mp_pgno;
mc->mc_db->md_depth++;
- *mc->mc_dbflag = DB_DIRTY;
+ *mc->mc_dbflag |= DB_DIRTY;
if ((mc->mc_db->md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
== MDB_DUPFIXED)
np->mp_flags |= P_LEAF2;
}
DPRINTF("Sub-db %u for db %u root page %zu", mx->mx_cursor.mc_dbi, mc->mc_dbi,
mx->mx_db.md_root);
- mx->mx_dbflag = (F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY)) ?
- DB_DIRTY : 0;
+ mx->mx_dbflag = DB_VALID | (F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY) ?
+ DB_DIRTY : 0);
mx->mx_dbx.md_name.mv_data = NODEKEY(node);
mx->mx_dbx.md_name.mv_size = node->mn_ksize;
#if UINT_MAX < SIZE_MAX
MDB_xcursor *mx = NULL;
size_t size = sizeof(MDB_cursor);
- if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs)
+ if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
/* Allow read access to the freelist */
DPRINTF("====> delete db %u key [%s]", dbi, DKEY(key));
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
assert(key != NULL);
assert(data != NULL);
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
: ((f & MDB_REVERSEDUP) ? mdb_cmp_memnr : mdb_cmp_memn));
}
-#define PERSISTENT_FLAGS 0xffff
-#define VALID_FLAGS (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
- MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
{
MDB_val key, data;
MDB_dbi i;
MDB_cursor mc;
- uint16_t mdflags;
int rc, dbflag, exact;
unsigned int unused = 0;
size_t len;
return MDB_DBS_FULL;
/* Find the DB info */
- dbflag = 0;
+ dbflag = DB_NEW|DB_VALID;
exact = 0;
key.mv_size = len;
key.mv_data = (void *)name;
dummy.md_root = P_INVALID;
dummy.md_flags = flags & PERSISTENT_FLAGS;
rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
- dbflag = DB_DIRTY;
+ dbflag |= DB_DIRTY;
}
/* OK, got info, add to table */
txn->mt_dbflags[slot] = dbflag;
memcpy(&txn->mt_dbs[slot], data.mv_data, sizeof(MDB_db));
*dbi = slot;
- txn->mt_env->me_dbflags[slot] = mdflags = txn->mt_dbs[slot].md_flags;
+ txn->mt_env->me_dbflags[slot] = txn->mt_dbs[slot].md_flags;
mdb_default_cmp(txn, slot);
if (!unused) {
txn->mt_numdbs++;
- txn->mt_env->me_numdbs++;
- }
- /* Open the DB in parent txns as well */
- while ((txn = txn->mt_parent) != NULL) {
- txn->mt_dbflags[slot] = DB_STALE;
- txn->mt_dbs[slot].md_flags = mdflags;
- if (!unused)
- txn->mt_numdbs++;
}
}
MDB_cursor *mc;
int rc;
- if (!txn || !dbi || dbi >= txn->mt_numdbs || (unsigned)del > 1)
+ if (!txn || !dbi || dbi >= txn->mt_numdbs || (unsigned)del > 1 || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
{
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
txn->mt_dbxs[dbi].md_cmp = cmp;
int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
{
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
txn->mt_dbxs[dbi].md_dcmp = cmp;
int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
{
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
txn->mt_dbxs[dbi].md_rel = rel;
int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
{
- if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
return EINVAL;
txn->mt_dbxs[dbi].md_relctx = ctx;