MDB_val *key, MDB_val *data);
static void mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
-static void mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_db *db);
+static void mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_node *node);
static void mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
static size_t mdb_leaf_size(MDB_env *env, MDB_val *key,
MDB_xcursor mx;
mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
+ mdb_xcursor_init1(txn, dbi, &mx, leaf);
rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi, NULL, NULL, 0, &mpp);
if (rc != MDB_SUCCESS)
return rc;
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
+ mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc != MDB_SUCCESS)
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
if (op == MDB_PREV || op == MDB_PREV_DUP) {
- rc = mdb_cursor_next(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
+ rc = mdb_cursor_prev(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
if (op != MDB_PREV || rc == MDB_SUCCESS)
return rc;
}
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
+ mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc != MDB_SUCCESS)
return rc;
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
+ mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
if (op == MDB_SET || op == MDB_SET_RANGE) {
rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
} else {
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
+ mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc)
return rc;
return rc;
if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
+ mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc)
return rc;
}
static void
-mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_db *db)
+mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_node *node)
{
+ MDB_db *db = NODEDATA(node);
+ MDB_dbi dbn;
mx->mx_dbs[0] = txn->mt_dbs[0];
mx->mx_dbs[1] = txn->mt_dbs[1];
if (dbi > 1) {
mx->mx_dbs[2] = txn->mt_dbs[dbi];
- mx->mx_dbs[3] = *db;
+ dbn = 3;
} else {
- mx->mx_dbs[2] = *db;
+ dbn = 2;
}
+ mx->mx_dbs[dbn] = *db;
+ mx->mx_dbxs[dbn].md_name.mv_data = NODEKEY(node);
+ mx->mx_dbxs[dbn].md_name.mv_size = node->mn_ksize;
+ mx->mx_txn.mt_next_pgno = txn->mt_next_pgno;
+ mx->mx_txn.mt_oldest = txn->mt_oldest;
+ mx->mx_txn.mt_u = txn->mt_u;
}
static void
mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
{
+ txn->mt_next_pgno = mx->mx_txn.mt_next_pgno;
+ txn->mt_oldest = mx->mx_txn.mt_oldest;
+ txn->mt_u = mx->mx_txn.mt_u;
txn->mt_dbs[0] = mx->mx_dbs[0];
txn->mt_dbs[1] = mx->mx_dbs[1];
txn->mt_dbxs[0].md_dirty = mx->mx_dbxs[0].md_dirty;
MDB_pageparent mp2;
mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
+ mdb_xcursor_init1(txn, dbi, &mx, leaf);
if (flags == MDB_DEL_DUP) {
rc = mdb_del(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, NULL, 0);
mdb_xcursor_fini(txn, dbi, &mx);
rdata = data;
}
- if (SIZELEFT(mpp.mp_page) < mdb_leaf_size(txn->mt_env, key, data)) {
- rc = mdb_split(txn, dbi, &mpp.mp_page, &ki, key, data, P_INVALID);
+ if (SIZELEFT(mpp.mp_page) < mdb_leaf_size(txn->mt_env, key, rdata)) {
+ rc = mdb_split(txn, dbi, &mpp.mp_page, &ki, key, rdata, P_INVALID);
} else {
/* There is room already in this leaf page. */
- rc = mdb_add_node(txn, dbi, mpp.mp_page, ki, key, data, 0, 0);
+ rc = mdb_add_node(txn, dbi, mpp.mp_page, ki, key, rdata, 0, 0);
}
if (rc != MDB_SUCCESS)
leaf = NODEPTR(mpp.mp_page, ki);
put_sub:
mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
+ mdb_xcursor_init1(txn, dbi, &mx, leaf);
xdata.mv_size = 0;
xdata.mv_data = "";
if (flags == MDB_NODUPDATA)
--- /dev/null
+/* mtest3.c - memory-mapped database tester/toy */
+/*
+ * Copyright 2011 Howard Chu, Symas Corp.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+
+/* Tests for sorted duplicate DBs */
+#define _XOPEN_SOURCE 500 /* srandom(), random() */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include "mdb.h"
+
+int main(int argc,char * argv[])
+{
+ int i = 0, j = 0, rc;
+ MDB_env *env;
+ MDB_dbi dbi;
+ MDB_val key, data;
+ MDB_txn *txn;
+ MDB_stat mst;
+ MDB_cursor *cursor;
+ int count;
+ int *values;
+ char sval[32];
+ char kval[sizeof(int)];
+
+ srandom(time(NULL));
+
+ memset(sval, 0, sizeof(sval));
+
+ count = (random()%384) + 64;
+ values = (int *)malloc(count*sizeof(int));
+
+ for(i = 0;i<count;i++) {
+ values[i] = random()%1024;
+ }
+
+ rc = mdbenv_create(&env);
+ rc = mdbenv_set_mapsize(env, 10485760);
+ rc = mdbenv_set_maxdbs(env, 4);
+ rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
+ rc = mdb_txn_begin(env, 0, &txn);
+ rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi);
+
+ key.mv_size = sizeof(int);
+ key.mv_data = kval;
+ data.mv_size = sizeof(sval);
+ data.mv_data = sval;
+
+ printf("Adding %d values\n", count);
+ for (i=0;i<count;i++) {
+ if (!(i & 0x0f))
+ sprintf(kval, "%03x", values[i]);
+ sprintf(sval, "%03x %d foo bar", values[i], values[i]);
+ rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
+ if (rc) j++;
+ }
+ if (j) printf("%d duplicates skipped\n", j);
+ rc = mdb_txn_commit(txn);
+ rc = mdbenv_stat(env, &mst);
+
+ rc = mdb_txn_begin(env, 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",
+ key.mv_data, (int) key.mv_size, (char *) key.mv_data,
+ data.mv_data, (int) data.mv_size, (char *) data.mv_data);
+ }
+ mdb_cursor_close(cursor);
+ mdb_txn_abort(txn);
+
+ j=0;
+
+ for (i= count - 1; i > -1; i-= (random()%5)) {
+ j++;
+ txn=NULL;
+ rc = mdb_txn_begin(env, 0, &txn);
+ sprintf(kval, "%03x", values[i & ~0x0f]);
+ sprintf(sval, "%03x %d foo bar", values[i], values[i]);
+ key.mv_size = sizeof(int);
+ key.mv_data = kval;
+ data.mv_size = sizeof(sval);
+ data.mv_data = sval;
+ rc = mdb_del(txn, dbi, &key, &data, MDB_DEL_DUP);
+ if (rc) {
+ j--;
+ mdb_txn_abort(txn);
+ } else {
+ rc = mdb_txn_commit(txn);
+ }
+ }
+ free(values);
+ printf("Deleted %d values\n", j);
+
+ rc = mdbenv_stat(env, &mst);
+ rc = mdb_txn_begin(env, 1, &txn);
+ rc = mdb_cursor_open(txn, dbi, &cursor);
+ printf("Cursor next\n");
+ while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
+ printf("key: %.*s, data: %.*s\n",
+ (int) key.mv_size, (char *) key.mv_data,
+ (int) data.mv_size, (char *) data.mv_data);
+ }
+ printf("Cursor prev\n");
+ while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
+ printf("key: %.*s, data: %.*s\n",
+ (int) key.mv_size, (char *) key.mv_data,
+ (int) data.mv_size, (char *) data.mv_data);
+ }
+ mdb_cursor_close(cursor);
+ mdb_close(txn, dbi);
+
+ mdb_txn_abort(txn);
+ mdbenv_close(env);
+
+ return 0;
+}