static int mdb_cursor_first(MDB_cursor *mc, MDB_val *key, MDB_val *data);
static int mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data);
-static void mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi);
+static void mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
static void mdb_xcursor_init0(MDB_cursor *mc);
static void mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node);
MDB_node *leaf;
txnid_t *kptr, oldest;
- mdb_cursor_init(&m2, txn, FREE_DBI);
+ mdb_cursor_init(&m2, txn, FREE_DBI, NULL);
mdb_search_page(&m2, NULL, 0);
leaf = NODEPTR(m2.mc_pg[m2.mc_top], 0);
kptr = (txnid_t *)NODEKEY(leaf);
DPRINTF("committing txn %zu %p on mdbenv %p, root page %zu",
txn->mt_txnid, (void *)txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root);
- mdb_cursor_init(&mc, txn, FREE_DBI);
+ mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
/* should only be one record now */
if (env->me_pghead) {
MDB_val data;
data.mv_size = sizeof(MDB_db);
- mdb_cursor_init(&mc, txn, MAIN_DBI);
+ mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
for (i = 2; i < txn->mt_numdbs; i++) {
if (txn->mt_dbxs[i].md_dirty) {
data.mv_data = &txn->mt_dbs[i];
/* For sub-databases, update main root first */
if (mc->mc_dbi > MAIN_DBI && !mc->mc_dbx->md_dirty) {
MDB_cursor mc2;
- mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI);
+ mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, NULL);
rc = mdb_search_page(&mc2, &mc->mc_dbx->md_name, 1);
if (rc)
return rc;
return EINVAL;
}
- mdb_cursor_init(&mc, txn, dbi);
- if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
- mc.mc_xcursor = &mx;
- mdb_xcursor_init0(&mc);
- } else {
- mc.mc_xcursor = NULL;
- }
+ mdb_cursor_init(&mc, txn, dbi, &mx);
return mdb_cursor_set(&mc, key, data, MDB_SET, &exact);
}
}
} else {
- if (mc->mc_db->md_flags & MDB_DUPSORT) {
- mc->mc_xcursor->mx_cursor.mc_flags &= ~C_INITIALIZED;
- }
+ if (mc->mc_xcursor)
+ mc->mc_xcursor->mx_cursor.mc_flags = 0;
if ((rc = mdb_read_data(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
return rc;
}
if (rc)
return rc;
} else {
+ if (mc->mc_xcursor)
+ mc->mc_xcursor->mx_cursor.mc_flags = 0;
if ((rc = mdb_read_data(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
return rc;
}
if (mc->mc_dbi > MAIN_DBI && !mc->mc_dbx->md_dirty) {
MDB_cursor mc2;
- mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI);
+ mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, NULL);
rc = mdb_search_page(&mc2, &mc->mc_dbx->md_name, 1);
if (rc)
return rc;
}
static void
-mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi)
+mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
{
mc->mc_dbi = dbi;
mc->mc_txn = txn;
mc->mc_dbx = &txn->mt_dbxs[dbi];
mc->mc_snum = 0;
mc->mc_flags = 0;
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
+ assert(mx != NULL);
+ mc->mc_xcursor = mx;
+ mdb_xcursor_init0(mc);
+ } else {
+ mc->mc_xcursor = NULL;
+ }
}
int
mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
{
MDB_cursor *mc;
+ MDB_xcursor *mx = NULL;
size_t size = sizeof(MDB_cursor);
if (txn == NULL || ret == NULL || !dbi || dbi >= txn->mt_numdbs)
size += sizeof(MDB_xcursor);
if ((mc = malloc(size)) != NULL) {
- mdb_cursor_init(mc, txn, dbi);
if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
- MDB_xcursor *mx = (MDB_xcursor *)(mc + 1);
- mc->mc_xcursor = mx;
- mdb_xcursor_init0(mc);
+ mx = (MDB_xcursor *)(mc + 1);
}
+ mdb_cursor_init(mc, txn, dbi, mx);
} else {
return ENOMEM;
}
return EINVAL;
}
- mdb_cursor_init(&mc, txn, dbi);
- if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
- mc.mc_xcursor = &mx;
- mdb_xcursor_init0(&mc);
- } else {
- mc.mc_xcursor = NULL;
- }
+ mdb_cursor_init(&mc, txn, dbi, &mx);
exact = 0;
if (data) {
if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA)) != flags)
return EINVAL;
- mdb_cursor_init(&mc, txn, dbi);
- if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
- mc.mc_xcursor = &mx;
- mdb_xcursor_init0(&mc);
- } else {
- mc.mc_xcursor = NULL;
- }
+ mdb_cursor_init(&mc, txn, dbi, &mx);
return mdb_cursor_put(&mc, key, data, flags);
}
memset(&dummy, 0, sizeof(dummy));
dummy.md_root = P_INVALID;
dummy.md_flags = flags & 0xffff;
- mdb_cursor_init(&mc, txn, MAIN_DBI);
+ mdb_cursor_init(&mc, txn, MAIN_DBI, NULL);
rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
dirty = 1;
}