Cursor stuff working, no major perf gain

vmware
Howard Chu 13 years ago
parent 1843fe0b53
commit 7bb14a2e79
  1. 3
      libraries/libmdb/Makefile
  2. 176
      libraries/libmdb/mdb.c

@ -4,7 +4,7 @@ OPT = -O2 -g
CFLAGS = -pthread $(OPT) $(W) $(XCFLAGS) CFLAGS = -pthread $(OPT) $(W) $(XCFLAGS)
LDLIBS = LDLIBS =
PROGS = mdb_stat mtest mtest2 mtest3 PROGS = mdb_stat mtest mtest2 mtest3 mtest4 mtest5
all: libmdb.a libmdb.so $(PROGS) all: libmdb.a libmdb.so $(PROGS)
clean: clean:
@ -25,6 +25,7 @@ mtest: mtest.o libmdb.a
mtest2: mtest2.o libmdb.a mtest2: mtest2.o libmdb.a
mtest3: mtest3.o libmdb.a mtest3: mtest3.o libmdb.a
mtest4: mtest4.o libmdb.a mtest4: mtest4.o libmdb.a
mtest5: mtest5.o libmdb.a
mdb.o: mdb.c mdb.h midl.h mdb.o: mdb.c mdb.h midl.h
$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c mdb.c $(CC) $(CFLAGS) -fPIC $(CPPFLAGS) -c mdb.c

@ -509,7 +509,6 @@ static MDB_ppage *cursor_push_page(MDB_cursor *cursor,
MDB_page *mp); MDB_page *mp);
static int mdb_sibling(MDB_cursor *cursor, int move_right); static int mdb_sibling(MDB_cursor *cursor, int move_right);
static void mdb_cursor_reset(MDB_cursor *mc);
static int mdb_cursor_next(MDB_cursor *cursor, static int mdb_cursor_next(MDB_cursor *cursor,
MDB_val *key, MDB_val *data, MDB_cursor_op op); MDB_val *key, MDB_val *data, MDB_cursor_op op);
static int mdb_cursor_prev(MDB_cursor *cursor, static int mdb_cursor_prev(MDB_cursor *cursor,
@ -2380,9 +2379,6 @@ mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op o
} }
} }
if (cursor->mc_flags & C_XDIRTY)
mdb_cursor_reset(cursor);
DPRINTF("cursor_next: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor); DPRINTF("cursor_next: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
if (top->mp_ki + 1 >= NUMKEYS(mp)) { if (top->mp_ki + 1 >= NUMKEYS(mp)) {
@ -2455,9 +2451,6 @@ mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op o
} }
} }
if (cursor->mc_flags & C_XDIRTY)
mdb_cursor_reset(cursor);
DPRINTF("cursor_prev: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor); DPRINTF("cursor_prev: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
if (top->mp_ki == 0) { if (top->mp_ki == 0) {
@ -2522,27 +2515,49 @@ mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
/* See if we're already on the right page */ /* See if we're already on the right page */
if (cursor->mc_flags & C_INITIALIZED) { if (cursor->mc_flags & C_INITIALIZED) {
MDB_val nodekey; MDB_val nodekey;
top = CURSOR_TOP(cursor); top = CURSOR_TOP(cursor);
/* Don't try this for LEAF2 pages. Maybe support that later. */ if (top->mp_page->mp_flags & P_LEAF2) {
if ((top->mp_page->mp_flags & (P_LEAF|P_LEAF2)) == P_LEAF) { nodekey.mv_size = cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_pad;
nodekey.mv_data = LEAF2KEY(top->mp_page, 0, nodekey.mv_size);
} else {
leaf = NODEPTR(top->mp_page, 0); leaf = NODEPTR(top->mp_page, 0);
MDB_SET_KEY(leaf, &nodekey); MDB_SET_KEY(leaf, &nodekey);
rc = mdb_cmp(cursor->mc_txn, cursor->mc_dbi, key, &nodekey); }
if (rc >= 0) { rc = mdb_cmp(cursor->mc_txn, cursor->mc_dbi, key, &nodekey);
leaf = NODEPTR(top->mp_page, NUMKEYS(top->mp_page)-1); if (rc == 0) {
MDB_SET_KEY(leaf, &nodekey); set1:
rc = mdb_cmp(cursor->mc_txn, cursor->mc_dbi, key, &nodekey); /* we're already on the right page */
if (rc <= 0) { mpp.mp_page = top->mp_page;
/* we're already on the right page */ rc = 0;
mpp.mp_page = top->mp_page; goto set2;
rc = 0; }
goto set2; if (rc > 0) {
unsigned int i;
if (NUMKEYS(top->mp_page) > 1) {
if (top->mp_page->mp_flags & P_LEAF2) {
nodekey.mv_data = LEAF2KEY(top->mp_page,
NUMKEYS(top->mp_page)-1, nodekey.mv_size);
} else {
leaf = NODEPTR(top->mp_page, NUMKEYS(top->mp_page)-1);
MDB_SET_KEY(leaf, &nodekey);
} }
rc = mdb_cmp(cursor->mc_txn, cursor->mc_dbi, key, &nodekey);
if (rc <= 0) goto set1;
}
/* If any parents have right-sibs, search.
* Otherwise, there's nothing further.
*/
for (i=0; i<cursor->mc_snum-1; i++)
if (cursor->mc_stack[i].mp_ki <
NUMKEYS(cursor->mc_stack[i].mp_page)-1)
break;
if (i == cursor->mc_snum - 1) {
/* There are no other pages */
goto set1;
} }
} }
} }
if (cursor->mc_flags & C_XDIRTY)
mdb_cursor_reset(cursor);
cursor->mc_snum = 0; cursor->mc_snum = 0;
@ -2630,9 +2645,6 @@ mdb_cursor_first(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
MDB_pageparent mpp; MDB_pageparent mpp;
MDB_node *leaf; MDB_node *leaf;
if (cursor->mc_flags & C_XDIRTY)
mdb_cursor_reset(cursor);
cursor->mc_snum = 0; cursor->mc_snum = 0;
rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, NULL, cursor, 0, &mpp); rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, NULL, cursor, 0, &mpp);
@ -2676,9 +2688,6 @@ mdb_cursor_last(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
MDB_node *leaf; MDB_node *leaf;
MDB_val lkey; MDB_val lkey;
if (cursor->mc_flags & C_XDIRTY)
mdb_cursor_reset(cursor);
cursor->mc_snum = 0; cursor->mc_snum = 0;
lkey.mv_size = MAXKEYSIZE+1; lkey.mv_size = MAXKEYSIZE+1;
@ -2907,7 +2916,9 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
if ((mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & (MDB_DUPSORT|MDB_DUPFIXED)) if ((mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
== MDB_DUPFIXED) == MDB_DUPFIXED)
dp->p.mp_flags |= P_LEAF2; dp->p.mp_flags |= P_LEAF2;
mc->mc_flags |= C_INITIALIZED;
rc = MDB_NOTFOUND; rc = MDB_NOTFOUND;
goto top;
} else { } else {
int exact = 0; int exact = 0;
rc = mdb_cursor_set(mc, key, NULL, MDB_SET, &exact); rc = mdb_cursor_set(mc, key, NULL, MDB_SET, &exact);
@ -2923,13 +2934,22 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
rc2 = mdb_cursor_touch(mc); rc2 = mdb_cursor_touch(mc);
if (rc2) return rc2; if (rc2) return rc2;
top:
top = CURSOR_TOP(mc); top = CURSOR_TOP(mc);
/* The key already exists */ /* The key already exists */
if (rc == MDB_SUCCESS) { if (rc == MDB_SUCCESS) {
/* there's only a key anyway, so this is a no-op */ /* there's only a key anyway, so this is a no-op */
if (IS_LEAF2(top->mp_page)) if (IS_LEAF2(top->mp_page)) {
int ksize = mc->mc_txn->mt_dbs[mc->mc_dbi].md_pad;
if (key->mv_size != ksize)
return EINVAL;
if (flags == MDB_CURRENT) {
char *ptr = LEAF2KEY(top->mp_page, top->mp_ki, ksize);
memcpy(ptr, key->mv_data, ksize);
}
return MDB_SUCCESS; return MDB_SUCCESS;
}
leaf = NODEPTR(top->mp_page, top->mp_ki); leaf = NODEPTR(top->mp_page, top->mp_ki);
@ -2972,7 +2992,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
} }
mdb_del_node(top->mp_page, top->mp_ki, 0); mdb_del_node(top->mp_page, top->mp_ki, 0);
} else { } else {
DPRINTF("appending key at index %i", top->mp_ki); DPRINTF("inserting key at index %i", top->mp_ki);
} }
rdata = data; rdata = data;
@ -3003,7 +3023,10 @@ new_sub:
if (do_sub) { if (do_sub) {
leaf = NODEPTR(top->mp_page, top->mp_ki); leaf = NODEPTR(top->mp_page, top->mp_ki);
put_sub: put_sub:
mdb_xcursor_init1(mc, top->mp_page, leaf); if (flags == MDB_CURRENT)
mdb_xcursor_init2(mc);
else
mdb_xcursor_init1(mc, top->mp_page, leaf);
xdata.mv_size = 0; xdata.mv_size = 0;
xdata.mv_data = ""; xdata.mv_data = "";
if (flags == MDB_NODUPDATA) if (flags == MDB_NODUPDATA)
@ -3015,7 +3038,10 @@ put_sub:
leaf->mn_flags |= F_DUPDATA; leaf->mn_flags |= F_DUPDATA;
} }
rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, flags); rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, flags);
mc->mc_flags |= C_XDIRTY; mdb_xcursor_fini(mc);
memcpy(NODEDATA(leaf),
&mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
sizeof(MDB_db));
} }
mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries++; mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries++;
} }
@ -3056,12 +3082,15 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
MDB_pageparent mp2; MDB_pageparent mp2;
if (flags != MDB_NODUPDATA) { if (flags != MDB_NODUPDATA) {
mdb_xcursor_init2(mc); /* mdb_xcursor_init2(mc); */
rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, 0); rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, 0);
mc->mc_flags |= C_XDIRTY; mdb_xcursor_fini(mc);
/* If sub-DB still has entries, we're done */ /* If sub-DB still has entries, we're done */
if (mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root if (mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root
!= P_INVALID) { != P_INVALID) {
memcpy(NODEDATA(leaf),
&mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
sizeof(MDB_db));
mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries--; mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries--;
return rc; return rc;
} }
@ -3069,37 +3098,71 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
} }
/* add all the child DB's pages to the free list */ /* add all the child DB's pages to the free list */
mc->mc_xcursor->mx_cursor.mc_snum = 0;
rc = mdb_search_page(&mc->mc_xcursor->mx_txn, mc->mc_xcursor->mx_cursor.mc_dbi, rc = mdb_search_page(&mc->mc_xcursor->mx_txn, mc->mc_xcursor->mx_cursor.mc_dbi,
NULL, &mc->mc_xcursor->mx_cursor, 0, &mp2); NULL, &mc->mc_xcursor->mx_cursor, 0, &mp2);
if (rc == MDB_SUCCESS) { if (rc == MDB_SUCCESS) {
MDB_ppage *top, *parent; MDB_ppage *top, *parent;
MDB_dpage *dp;
ID2 mid;
MDB_node *ni; MDB_node *ni;
unsigned int i; unsigned int i;
int dirty_root = 0;
mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries -= mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries -=
mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries; mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries;
cursor_pop_page(&mc->mc_xcursor->mx_cursor); cursor_pop_page(&mc->mc_xcursor->mx_cursor);
if (mc->mc_xcursor->mx_cursor.mc_snum) { if (mc->mc_xcursor->mx_cursor.mc_snum) {
top = CURSOR_TOP(&mc->mc_xcursor->mx_cursor); #if 0
if (mc->mc_xcursor->mx_cursor.mc_stack[0].mp_page->mp_flags & P_DIRTY) {
dirty_root = 1;
}
#endif
while (mc->mc_xcursor->mx_cursor.mc_snum > 1) { while (mc->mc_xcursor->mx_cursor.mc_snum > 1) {
top = CURSOR_TOP(&mc->mc_xcursor->mx_cursor);
parent = CURSOR_PARENT(&mc->mc_xcursor->mx_cursor); parent = CURSOR_PARENT(&mc->mc_xcursor->mx_cursor);
for (i=0; i<NUMKEYS(top->mp_page); i++) { for (i=0; i<NUMKEYS(top->mp_page); i++) {
MDB_page *mp;
pgno_t pg;
ni = NODEPTR(top->mp_page, i); ni = NODEPTR(top->mp_page, i);
mdb_midl_insert(mc->mc_txn->mt_free_pgs, NODEPGNO(ni)); pg = NODEPGNO(ni);
} if ((rc = mdb_get_page(mc->mc_txn, pg, &mp)))
parent->mp_ki++; return rc;
if (parent->mp_ki >= NUMKEYS(parent->mp_page)) { #if 0
cursor_pop_page(&mc->mc_xcursor->mx_cursor); if (mp->mp_flags & P_DIRTY) {
top = parent; /* drop it */
} else { mid.mid = pg;
ni = NODEPTR(parent->mp_page, parent->mp_ki); mdb_mid2l_delete(mc->mc_txn->mt_u.dirty_list, &mid);
rc = mdb_get_page(&mc->mc_xcursor->mx_txn, NODEPGNO(ni), &top->mp_page); dp = mid.mptr;
dp->h.md_parent = (MDB_page *)mc->mc_txn->mt_env->me_dpages;
mc->mc_txn->mt_env->me_dpages = dp;
} else
#endif
{
/* free it */
mdb_midl_insert(mc->mc_txn->mt_free_pgs, pg);
}
} }
rc = mdb_sibling(&mc->mc_xcursor->mx_cursor, 1);
if (rc) break;
} }
} }
mdb_midl_insert(mc->mc_txn->mt_free_pgs, #if 0
mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root); if (dirty_root) {
/* drop it */
mid.mid = mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root;
mdb_mid2l_delete(mc->mc_txn->mt_u.dirty_list, &mid);
dp = mid.mptr;
dp->h.md_parent = (MDB_page *)mc->mc_txn->mt_env->me_dpages;
mc->mc_txn->mt_env->me_dpages = dp;
} else
#endif
{
/* free it */
mdb_midl_insert(mc->mc_txn->mt_free_pgs,
mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root);
}
} }
} }
@ -3344,7 +3407,6 @@ mdb_xcursor_init0(MDB_cursor *mc)
mx->mx_cursor.mc_xcursor = NULL; mx->mx_cursor.mc_xcursor = NULL;
mx->mx_cursor.mc_txn = &mx->mx_txn; mx->mx_cursor.mc_txn = &mx->mx_txn;
mx->mx_cursor.mc_snum = 0;
mx->mx_cursor.mc_dbi = dbn+1; mx->mx_cursor.mc_dbi = dbn+1;
} }
@ -3370,6 +3432,7 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_page *mp, MDB_node *node)
mx->mx_dbxs[dbn].md_name.mv_size = node->mn_ksize; mx->mx_dbxs[dbn].md_name.mv_size = node->mn_ksize;
mx->mx_txn.mt_next_pgno = mc->mc_txn->mt_next_pgno; mx->mx_txn.mt_next_pgno = mc->mc_txn->mt_next_pgno;
mx->mx_txn.mt_u = mc->mc_txn->mt_u; mx->mx_txn.mt_u = mc->mc_txn->mt_u;
mx->mx_cursor.mc_snum = 0;
mx->mx_cursor.mc_flags = 0; mx->mx_cursor.mc_flags = 0;
} }
@ -3377,11 +3440,17 @@ static void
mdb_xcursor_init2(MDB_cursor *mc) mdb_xcursor_init2(MDB_cursor *mc)
{ {
MDB_xcursor *mx = mc->mc_xcursor; MDB_xcursor *mx = mc->mc_xcursor;
MDB_dbi dbn;
mx->mx_dbs[0] = mc->mc_txn->mt_dbs[0]; mx->mx_dbs[0] = mc->mc_txn->mt_dbs[0];
mx->mx_dbs[1] = mc->mc_txn->mt_dbs[1]; mx->mx_dbs[1] = mc->mc_txn->mt_dbs[1];
if (mc->mc_dbi > 1) { if (mc->mc_dbi > 1) {
mx->mx_dbs[2] = mc->mc_txn->mt_dbs[mc->mc_dbi]; mx->mx_dbs[2] = mc->mc_txn->mt_dbs[mc->mc_dbi];
dbn = 3;
} else {
dbn = 2;
} }
DPRINTF("Sub-db %u for db %u root page %lu", dbn, mc->mc_dbi,
mx->mx_dbs[dbn].md_root);
mx->mx_txn.mt_next_pgno = mc->mc_txn->mt_next_pgno; mx->mx_txn.mt_next_pgno = mc->mc_txn->mt_next_pgno;
} }
@ -3455,6 +3524,7 @@ mdb_cursor_count(MDB_cursor *mc, unsigned long *countp)
return MDB_SUCCESS; return MDB_SUCCESS;
} }
#if 0
static void static void
mdb_cursor_reset(MDB_cursor *mc) mdb_cursor_reset(MDB_cursor *mc)
{ {
@ -3474,15 +3544,16 @@ mdb_cursor_reset(MDB_cursor *mc)
&mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi], &mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
sizeof(MDB_db)); sizeof(MDB_db));
} }
mc->mc_flags ^= C_XDIRTY;
} }
} }
} }
#endif
void void
mdb_cursor_close(MDB_cursor *cursor) mdb_cursor_close(MDB_cursor *cursor)
{ {
if (cursor != NULL) { if (cursor != NULL) {
mdb_cursor_reset(cursor);
free(cursor); free(cursor);
} }
} }
@ -3725,8 +3796,10 @@ mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mpp)
txn->mt_dbs[dbi].md_root = P_INVALID; txn->mt_dbs[dbi].md_root = P_INVALID;
txn->mt_dbs[dbi].md_depth = 0; txn->mt_dbs[dbi].md_depth = 0;
txn->mt_dbs[dbi].md_leaf_pages = 0; txn->mt_dbs[dbi].md_leaf_pages = 0;
mdb_midl_insert(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
} else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) { } else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) {
DPUTS("collapsing root page!"); DPUTS("collapsing root page!");
mdb_midl_insert(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
txn->mt_dbs[dbi].md_root = NODEPGNO(NODEPTR(mpp->mp_page, 0)); txn->mt_dbs[dbi].md_root = NODEPGNO(NODEPTR(mpp->mp_page, 0));
if ((rc = mdb_get_page(txn, txn->mt_dbs[dbi].md_root, &root))) if ((rc = mdb_get_page(txn, txn->mt_dbs[dbi].md_root, &root)))
return rc; return rc;
@ -3865,9 +3938,6 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
rc = mdb_cursor_set(&mc, key, xdata, op, &exact); rc = mdb_cursor_set(&mc, key, xdata, op, &exact);
if (rc == 0) if (rc == 0)
rc = mdb_cursor_del(&mc, data ? 0 : MDB_NODUPDATA); rc = mdb_cursor_del(&mc, data ? 0 : MDB_NODUPDATA);
if (mc.mc_flags & C_XDIRTY) {
mdb_cursor_reset(&mc);
}
return rc; return rc;
} }
@ -4312,11 +4382,7 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
} else { } else {
mc.mc_xcursor = NULL; mc.mc_xcursor = NULL;
} }
rc = mdb_cursor_put(&mc, key, data, flags); return mdb_cursor_put(&mc, key, data, flags);
if (mc.mc_flags & C_XDIRTY) {
mdb_cursor_reset(&mc);
}
return rc;
} }
int int

Loading…
Cancel
Save