* 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
#define DB_DIRTY 0x01 /**< DB was written in this txn */
#define DB_STALE 0x02 /**< DB record is older than txnID */
/** @} */
+ /** Array of cursors for each DB */
+ MDB_cursor **mt_cursors;
/** Array of flags for each DB */
unsigned char *mt_dbflags;
/** Number of DB records in use. This number only ever increments;
/** Cursors are used for all DB operations */
struct MDB_cursor {
+ /** Next cursor on this DB in this txn */
+ MDB_cursor *mc_next;
+ /** Original cursor if this is a shadow */
+ MDB_cursor *mc_orig;
/** Context used for databases with #MDB_DUPSORT, otherwise NULL */
struct MDB_xcursor *mc_xcursor;
/** The transaction that owns this cursor */
*/
#define C_INITIALIZED 0x01 /**< cursor has been initialized and is valid */
#define C_EOF 0x02 /**< No more data */
+#define C_SUB 0x04 /**< Cursor is a sub-cursor */
+#define C_SHADOW 0x08 /**< Cursor is a dup from a parent txn */
/** @} */
unsigned int mc_flags; /**< @ref mdb_cursor */
MDB_page *mc_pg[CURSOR_STACK]; /**< stack of pushed pages */
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.
mp->mp_pgno = pgno;
mp->mp_flags |= P_DIRTY;
+finish:
+ /* Adjust other cursors pointing to mp */
+ if (mc->mc_flags & C_SUB) {
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = mc->mc_dbi-1;
+
+ for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ m3 = &m2->mc_xcursor->mx_cursor;
+ if (m3->mc_pg[mc->mc_top] == mc->mc_pg[mc->mc_top]) {
+ m3->mc_pg[mc->mc_top] = mp;
+ }
+ }
+ } else {
+ MDB_cursor *m2;
+
+ for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
+ if (m2->mc_pg[mc->mc_top] == mc->mc_pg[mc->mc_top]) {
+ m2->mc_pg[mc->mc_top] = mp;
+ }
+ }
+ }
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;
}
return rc;
}
+/** Make shadow copies of all of parent txn's cursors */
+static int
+mdb_cursor_shadow(MDB_txn *src, MDB_txn *dst)
+{
+ MDB_cursor *mc, *m2;
+ unsigned int i, j, size;
+
+ for (i=0;i<src->mt_numdbs; i++) {
+ if (src->mt_cursors[i]) {
+ size = sizeof(MDB_cursor);
+ if (src->mt_cursors[i]->mc_xcursor)
+ size += sizeof(MDB_xcursor);
+ for (m2 = src->mt_cursors[i]; m2; m2=m2->mc_next) {
+ mc = malloc(size);
+ if (!mc)
+ return ENOMEM;
+ mc->mc_orig = m2;
+ mc->mc_txn = dst;
+ mc->mc_dbi = i;
+ mc->mc_db = &dst->mt_dbs[i];
+ mc->mc_dbx = m2->mc_dbx;
+ mc->mc_dbflag = &dst->mt_dbflags[i];
+ mc->mc_snum = m2->mc_snum;
+ mc->mc_top = m2->mc_top;
+ mc->mc_flags = m2->mc_flags | C_SHADOW;
+ for (j=0; j<mc->mc_snum; j++) {
+ mc->mc_pg[j] = m2->mc_pg[j];
+ mc->mc_ki[j] = m2->mc_ki[j];
+ }
+ if (m2->mc_xcursor) {
+ MDB_xcursor *mx, *mx2;
+ mx = (MDB_xcursor *)(mc+1);
+ mc->mc_xcursor = mx;
+ mx2 = m2->mc_xcursor;
+ mx->mx_db = mx2->mx_db;
+ mx->mx_dbx = mx2->mx_dbx;
+ mx->mx_dbflag = mx2->mx_dbflag;
+ mx->mx_cursor.mc_txn = dst;
+ mx->mx_cursor.mc_dbi = mx2->mx_cursor.mc_dbi;
+ mx->mx_cursor.mc_db = &mx->mx_db;
+ mx->mx_cursor.mc_dbx = &mx->mx_dbx;
+ mx->mx_cursor.mc_dbflag = &mx->mx_dbflag;
+ mx->mx_cursor.mc_snum = mx2->mx_cursor.mc_snum;
+ mx->mx_cursor.mc_top = mx2->mx_cursor.mc_top;
+ mx->mx_cursor.mc_flags = mx2->mx_cursor.mc_flags | C_SHADOW;
+ for (j=0; j<mx2->mx_cursor.mc_snum; j++) {
+ mx->mx_cursor.mc_pg[j] = mx2->mx_cursor.mc_pg[j];
+ mx->mx_cursor.mc_ki[j] = mx2->mx_cursor.mc_ki[j];
+ }
+ } else {
+ mc->mc_xcursor = NULL;
+ }
+ mc->mc_next = dst->mt_cursors[i];
+ dst->mt_cursors[i] = mc;
+ }
+ }
+ }
+ return MDB_SUCCESS;
+}
+
+/** Merge shadow cursors back into parent's */
+static void
+mdb_cursor_merge(MDB_txn *txn)
+{
+ MDB_dbi i;
+ for (i=0; i<txn->mt_numdbs; i++) {
+ if (txn->mt_cursors[i]) {
+ MDB_cursor *mc;
+ while ((mc = txn->mt_cursors[i])) {
+ txn->mt_cursors[i] = mc->mc_next;
+ if (mc->mc_flags & C_SHADOW) {
+ MDB_cursor *m2 = mc->mc_orig;
+ unsigned int j;
+ m2->mc_snum = mc->mc_snum;
+ m2->mc_top = mc->mc_top;
+ for (j=0; j<mc->mc_snum; j++) {
+ m2->mc_pg[j] = mc->mc_pg[j];
+ m2->mc_ki[j] = mc->mc_ki[j];
+ }
+ }
+ free(mc);
+ }
+ }
+ }
+}
+
static void
mdb_txn_reset0(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;
+ int rc, size;
if (env->me_flags & MDB_FATAL_ERROR) {
DPUTS("environment had fatal error, must shutdown!");
return MDB_PANIC;
}
- if ((txn = calloc(1, sizeof(MDB_txn) +
- env->me_maxdbs * (sizeof(MDB_db)+1))) == NULL) {
+ if (parent) {
+ /* parent already has an active child txn */
+ if (parent->mt_child) {
+ return EINVAL;
+ }
+ }
+ size = sizeof(MDB_txn) + env->me_maxdbs * (sizeof(MDB_db)+1);
+ if (!(flags & MDB_RDONLY))
+ size += env->me_maxdbs * sizeof(MDB_cursor *);
+
+ if ((txn = calloc(1, size)) == NULL) {
DPRINTF("calloc: %s", strerror(ErrCode()));
return ENOMEM;
}
txn->mt_dbs = (MDB_db *)(txn+1);
if (flags & MDB_RDONLY) {
txn->mt_flags |= MDB_TXN_RDONLY;
+ txn->mt_dbflags = (unsigned char *)(txn->mt_dbs + env->me_maxdbs);
+ } else {
+ txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
+ txn->mt_dbflags = (unsigned char *)(txn->mt_cursors + env->me_maxdbs);
}
- 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);
+ mdb_cursor_shadow(parent, txn);
+ rc = 0;
+ } else {
+ rc = mdb_txn_renew0(txn);
+ }
if (rc)
free(txn);
else {
MDB_page *dp;
unsigned int i;
- if (mdb_midl_shrink(&txn->mt_free_pgs))
- env->me_free_pgs = txn->mt_free_pgs;
+ /* close(free) all cursors */
+ for (i=0; i<txn->mt_numdbs; i++) {
+ if (txn->mt_cursors[i]) {
+ MDB_cursor *mc;
+ while ((mc = txn->mt_cursors[i])) {
+ txn->mt_cursors[i] = mc->mc_next;
+ free(mc);
+ }
+ }
+ }
/* return all dirty pages to dpage list */
for (i=1; i<=txn->mt_u.dirty_list[0].mid; i++) {
}
}
+ 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);
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);
}
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)) {
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");
+ /* Merge (and close) our cursors with parent's */
+ mdb_cursor_merge(txn);
+
+ 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++) {
+ while (x <= dst[0].mid && dst[x].mid != src[y].mid) x++;
+ if (x > dst[0].mid)
+ break;
+ free(dst[x].mptr);
+ dst[x].mptr = 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;
}
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;
}
return node;
}
+#if 0
+static void
+mdb_cursor_adjust(MDB_cursor *mc, func)
+{
+ MDB_cursor *m2;
+
+ for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
+ if (m2->mc_pg[m2->mc_top] == mc->mc_pg[mc->mc_top]) {
+ func(mc, m2);
+ }
+ }
+}
+#endif
+
/** Pop a page off the top of the cursor's stack. */
static void
mdb_cursor_pop(MDB_cursor *mc)
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);
*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;
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;
if (flags == MDB_CURRENT)
goto current;
- /* create a fake page for the dup items */
dkey.mv_size = NODEDSZ(leaf);
dkey.mv_data = NODEDATA(leaf);
/* data matches, ignore it */
if (!mc->mc_dbx->md_dcmp(data, &dkey))
return (flags == MDB_NODUPDATA) ? MDB_KEYEXIST : MDB_SUCCESS;
+
+ /* create a fake page for the dup items */
memcpy(dbuf, dkey.mv_data, dkey.mv_size);
dkey.mv_data = dbuf;
fp = (MDB_page *)pbuf;
} else {
/* There is room already in this leaf page. */
rc = mdb_node_add(mc, mc->mc_ki[mc->mc_top], key, rdata, 0, 0);
+ if (rc == 0 && !do_sub) {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = mc->mc_dbi;
+ unsigned i = mc->mc_top;
+ MDB_page *mp = mc->mc_pg[i];
+
+ if (mc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (mc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (m3->mc_pg[i] == mp && m3->mc_ki[i] >= mc->mc_ki[i]) {
+ m3->mc_ki[i]++;
+ }
+ }
+ }
}
if (rc != MDB_SUCCESS)
rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, xflags);
if (rc)
return rc;
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2;
+ unsigned i = mc->mc_top;
+ MDB_page *mp = mc->mc_pg[i];
+
+ for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
+ if (m2->mc_pg[i] == mp && m2->mc_ki[i] == mc->mc_ki[i]) {
+ mdb_xcursor_init1(m2, leaf);
+ }
+ }
+ }
}
rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, xflags);
if (flags & F_SUBDATA) {
MDB_db *db = NODEDATA(node);
mx->mx_db = *db;
mx->mx_cursor.mc_snum = 0;
- mx->mx_cursor.mc_flags = 0;
+ mx->mx_cursor.mc_flags = C_SUB;
} else {
MDB_page *fp = NODEDATA(node);
mx->mx_db.md_pad = mc->mc_pg[mc->mc_top]->mp_pad;
mx->mx_db.md_entries = NUMKEYS(fp);
mx->mx_db.md_root = fp->mp_pgno;
mx->mx_cursor.mc_snum = 1;
- mx->mx_cursor.mc_flags = C_INITIALIZED;
+ mx->mx_cursor.mc_flags = C_INITIALIZED|C_SUB;
mx->mx_cursor.mc_top = 0;
mx->mx_cursor.mc_pg[0] = fp;
mx->mx_cursor.mc_ki[0] = 0;
static void
mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
{
+ mc->mc_orig = NULL;
mc->mc_dbi = dbi;
mc->mc_txn = txn;
mc->mc_db = &txn->mt_dbs[dbi];
mx = (MDB_xcursor *)(mc + 1);
}
mdb_cursor_init(mc, txn, dbi, mx);
+ if (txn->mt_cursors) {
+ mc->mc_next = txn->mt_cursors[dbi];
+ txn->mt_cursors[dbi] = mc;
+ }
} else {
return ENOMEM;
}
mdb_cursor_close(MDB_cursor *mc)
{
if (mc != NULL) {
+ /* remove from txn, if tracked */
+ if (mc->mc_txn->mt_cursors) {
+ MDB_cursor **prev = &mc->mc_txn->mt_cursors[mc->mc_dbi];
+ while (*prev && *prev != mc) prev = &(*prev)->mc_next;
+ if (*prev == mc)
+ *prev = mc->mc_next;
+ }
free(mc);
}
}
*/
mdb_node_del(csrc->mc_pg[csrc->mc_top], csrc->mc_ki[csrc->mc_top], key.mv_size);
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = csrc->mc_dbi;
+ MDB_page *mp = csrc->mc_pg[csrc->mc_top];
+
+ if (csrc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (csrc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (m3->mc_pg[csrc->mc_top] == mp && m3->mc_ki[csrc->mc_top] ==
+ csrc->mc_ki[csrc->mc_top]) {
+ m3->mc_pg[csrc->mc_top] = cdst->mc_pg[cdst->mc_top];
+ m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top];
+ }
+ }
+ }
+
/* Update the parent separators.
*/
if (csrc->mc_ki[csrc->mc_top] == 0) {
indx_t i, j;
MDB_node *srcnode;
MDB_val key, data;
+ unsigned nkeys;
DPRINTF("merging page %zu into %zu", csrc->mc_pg[csrc->mc_top]->mp_pgno,
cdst->mc_pg[cdst->mc_top]->mp_pgno);
/* Move all nodes from src to dst.
*/
- j = NUMKEYS(cdst->mc_pg[cdst->mc_top]);
+ j = nkeys = NUMKEYS(cdst->mc_pg[cdst->mc_top]);
if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
key.mv_size = csrc->mc_db->md_pad;
key.mv_data = METADATA(csrc->mc_pg[csrc->mc_top]);
csrc->mc_db->md_leaf_pages--;
else
csrc->mc_db->md_branch_pages--;
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = csrc->mc_dbi;
+ MDB_page *mp = cdst->mc_pg[cdst->mc_top];
+
+ if (csrc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (csrc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (m3->mc_pg[csrc->mc_top] == csrc->mc_pg[csrc->mc_top]) {
+ m3->mc_pg[csrc->mc_top] = mp;
+ m3->mc_ki[csrc->mc_top] += nkeys;
+ }
+ }
+ }
mdb_cursor_pop(csrc);
return mdb_rebalance(csrc);
}
if (mc->mc_snum < 2) {
- if (NUMKEYS(mc->mc_pg[mc->mc_top]) == 0) {
+ MDB_page *mp = mc->mc_pg[0];
+ if (NUMKEYS(mp) == 0) {
DPUTS("tree is completely empty");
mc->mc_db->md_root = P_INVALID;
mc->mc_db->md_depth = 0;
mc->mc_db->md_leaf_pages = 0;
- mdb_midl_append(&mc->mc_txn->mt_free_pgs, mc->mc_pg[mc->mc_top]->mp_pgno);
+ mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
mc->mc_snum = 0;
- } else if (IS_BRANCH(mc->mc_pg[mc->mc_top]) && NUMKEYS(mc->mc_pg[mc->mc_top]) == 1) {
+ mc->mc_top = 0;
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = mc->mc_dbi;
+
+ if (mc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (mc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (m3->mc_pg[0] == mp) {
+ m3->mc_snum = 0;
+ m3->mc_top = 0;
+ }
+ }
+ }
+ } else if (IS_BRANCH(mp) && NUMKEYS(mp) == 1) {
DPUTS("collapsing root page!");
- mdb_midl_append(&mc->mc_txn->mt_free_pgs, mc->mc_pg[mc->mc_top]->mp_pgno);
- mc->mc_db->md_root = NODEPGNO(NODEPTR(mc->mc_pg[mc->mc_top], 0));
+ mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
+ mc->mc_db->md_root = NODEPGNO(NODEPTR(mp, 0));
if ((rc = mdb_page_get(mc->mc_txn, mc->mc_db->md_root,
- &mc->mc_pg[mc->mc_top])))
+ &mc->mc_pg[0])))
return rc;
mc->mc_db->md_depth--;
mc->mc_db->md_branch_pages--;
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = mc->mc_dbi;
+
+ if (mc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (mc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (m3->mc_pg[0] == mp) {
+ m3->mc_pg[0] = mc->mc_pg[0];
+ }
+ }
+ }
} else
DPUTS("root page doesn't need rebalancing");
return MDB_SUCCESS;
mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno)
{
uint8_t flags;
- int rc = MDB_SUCCESS, ins_new = 0;
+ int rc = MDB_SUCCESS, ins_new = 0, new_root = 0;
indx_t newindx;
pgno_t pgno = 0;
unsigned int i, j, split_indx, nkeys, pmax;
mc->mc_db->md_root = pp->mp_pgno;
DPRINTF("root split! new root = %zu", pp->mp_pgno);
mc->mc_db->md_depth++;
+ new_root = 1;
/* Add left (implicit) pointer. */
if ((rc = mdb_node_add(mc, 0, NULL, NULL, mp->mp_pgno, 0)) != MDB_SUCCESS) {
rc = mdb_node_add(&mn, mn.mc_ki[ptop], &sepkey, NULL, rp->mp_pgno, 0);
mn.mc_top++;
}
- if (IS_LEAF2(rp)) {
- return rc;
- }
if (rc != MDB_SUCCESS) {
return rc;
}
+ if (IS_LEAF2(rp)) {
+ goto done;
+ }
/* 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;
/* return tmp page to freelist */
copy->mp_next = mc->mc_txn->mt_env->me_dpages;
mc->mc_txn->mt_env->me_dpages = copy;
+done:
+ {
+ /* Adjust other cursors pointing to mp */
+ MDB_cursor *m2, *m3;
+ MDB_dbi dbi = mc->mc_dbi;
+
+ if (mc->mc_flags & C_SUB)
+ dbi--;
+
+ for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+ if (mc->mc_flags & C_SUB)
+ m3 = &m2->mc_xcursor->mx_cursor;
+ else
+ m3 = m2;
+ if (new_root) {
+ /* root split */
+ for (i=m3->mc_top; i>0; i--) {
+ m3->mc_ki[i+1] = m3->mc_ki[i];
+ m3->mc_pg[i+1] = m3->mc_pg[i];
+ }
+ m3->mc_ki[0] = mc->mc_ki[0];
+ m3->mc_pg[0] = mc->mc_pg[0];
+ m3->mc_snum++;
+ m3->mc_top++;
+ }
+ if (m3->mc_pg[mc->mc_top] == mp) {
+ if (m3->mc_ki[m3->mc_top] >= split_indx) {
+ m3->mc_pg[m3->mc_top] = rp;
+ m3->mc_ki[m3->mc_top] -= split_indx;
+ }
+ }
+ }
+ }
return rc;
}
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)
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;
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
#define MDB_CURRENT 0x40
/* @} */
-/** Cursor Get operations
+/** @brief Cursor Get operations.
+ *
* This is the set of all operations for retrieving data
* using a cursor.
*/
#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. */
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
*/
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
*/
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().
*/
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
*/
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
*/
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.
*/
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.
*/
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
*/
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
*/
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,
*/
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().
*/
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
*/
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.
*/
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.
* the reader lock table is full. See #mdb_env_set_maxreaders().
* </ul>
*/
-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.
*/
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.
*/
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
*/
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.
*/
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.
*/
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
*/
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.
*/
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()
*/
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
*/
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
*/
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
*/
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()
*/
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.
*/
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
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.
*/
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()
*/
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
* the address and length of the data are returned in the object to which \b data
* refers.
- * @bug Cursors are not coordinated with write operations. If a cursor in a
- * write transaction is performing a sequential scan while records are being
- * inserted or deleted in the same transaction, the cursor will be corrupted.
* @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
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
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
*/
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()
*/
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()
*/
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()
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);
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
*/
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.
*/
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);
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",
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) {
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) {
* 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);
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);
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",
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) {
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) {
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);
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",
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);
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) {
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);
/* 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",
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",
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;
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) {
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);
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",
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);
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) {
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);
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);
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) {