#include <fcntl.h>
#include <assert.h>
-#include <err.h>
#include <errno.h>
-#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
unsigned int mn_flags:4;
unsigned int mn_ksize:12; /* key size */
#define F_BIGDATA 0x01 /* data put on overflow page */
+#define F_SUBDATA 0x02 /* data is a sub-database */
char mn_data[1];
} MDB_node;
-
typedef struct MDB_dbx {
MDB_val md_name;
MDB_cmp_func *md_cmp; /* user compare function */
static void mdb_del_node(MDB_page *mp, indx_t indx);
static int mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki,
MDB_pageparent *mpp, MDB_node *leaf);
+static int mdb_put0(MDB_txn *txn, MDB_dbi dbi,
+ MDB_val *key, MDB_val *data, unsigned int flags);
static int mdb_read_data(MDB_txn *txn, MDB_node *leaf, MDB_val *data);
static int mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mp);
static int mdb_set_key(MDB_node *node, MDB_val *key);
static int mdb_sibling(MDB_cursor *cursor, int move_right);
static int mdb_cursor_next(MDB_cursor *cursor,
- MDB_val *key, MDB_val *data);
+ MDB_val *key, MDB_val *data, MDB_cursor_op op);
static int mdb_cursor_prev(MDB_cursor *cursor,
- MDB_val *key, MDB_val *data);
+ MDB_val *key, MDB_val *data, MDB_cursor_op op);
static int mdb_cursor_set(MDB_cursor *cursor,
- MDB_val *key, MDB_val *data, int *exactp);
+ MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp);
static int mdb_cursor_first(MDB_cursor *cursor,
MDB_val *key, MDB_val *data);
static int mdb_cursor_last(MDB_cursor *cursor,
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_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_val *data);
static size_t mdb_branch_size(MDB_env *env, MDB_val *key);
static int
_mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key1, const MDB_val *key2)
{
- if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_REVERSEKEY))
+ if (txn->mt_dbs[dbi].md_flags & (MDB_REVERSEKEY
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+ |MDB_INTEGERKEY
+#endif
+ ))
return memnrcmp(key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
else
return memncmp((char *)key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
{
MDB_dpage *dp;
pgno_t pgno = P_INVALID;
- ULONG oldest = txn->mt_txnid - 2;
+ ULONG oldest;
+ if (txn->mt_txnid > 2) {
+
+ oldest = txn->mt_txnid - 2;
if (!txn->mt_env->me_pghead && txn->mt_dbs[FREE_DBI].md_root != P_INVALID) {
/* See if there's anything in the free DB */
MDB_pageparent mpp;
}
}
}
+ }
if ((dp = malloc(txn->mt_env->me_psize * num + sizeof(MDB_dhead))) == NULL)
return NULL;
MDB_txn *txn;
int rc, toggle;
- if ((txn = calloc(1, sizeof(*txn))) == NULL) {
+ if ((txn = calloc(1, sizeof(MDB_txn))) == NULL) {
DPRINTF("calloc: %s", strerror(errno));
return ENOMEM;
}
key.mv_data = (char *)&mop->mo_txnid;
data.mv_size = MDB_IDL_SIZEOF(mop->mo_pages);
data.mv_data = mop->mo_pages;
- mdb_put(txn, FREE_DBI, &key, &data, 0);
+ mdb_put0(txn, FREE_DBI, &key, &data, 0);
free(env->me_pghead);
env->me_pghead = NULL;
}
key.mv_data = (char *)&txn->mt_txnid;
data.mv_size = MDB_IDL_SIZEOF(txn->mt_free_pgs);
data.mv_data = txn->mt_free_pgs;
- mdb_put(txn, FREE_DBI, &key, &data, 0);
+ mdb_put0(txn, FREE_DBI, &key, &data, 0);
}
/* Update DB root pointers. Their pages have already been
for (i = 2; i < txn->mt_numdbs; i++) {
if (txn->mt_dbxs[i].md_dirty) {
data.mv_data = &txn->mt_dbs[i];
- mdb_put(txn, i, &txn->mt_dbxs[i].md_name, &data, 0);
+ mdb_put0(txn, MAIN_DBI, &txn->mt_dbxs[i].md_name, &data, 0);
}
}
}
if (m->mm_version != MDB_VERSION) {
DPRINTF("database is version %u, expected version %u",
m->mm_version, MDB_VERSION);
- return EINVAL;
+ return MDB_VERSION_MISMATCH;
}
memcpy(meta, m, sizeof(*m));
meta->mm_psize = psize;
meta->mm_last_pg = 1;
meta->mm_flags = env->me_flags & 0xffff;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
- /* freeDB keys are pgno_t's, must compare in int order */
- meta->mm_flags |= MDB_REVERSEKEY;
-#endif
+ meta->mm_flags |= MDB_INTEGERKEY;
meta->mm_dbs[0].md_root = P_INVALID;
meta->mm_dbs[1].md_root = P_INVALID;
assert(txn != NULL);
assert(txn->mt_env != NULL);
- DPRINTF("writing meta page for root page %lu", txn->mt_dbs[MAIN_DBI].md_root);
+ DPRINTF("writing meta page %d for root page %lu",
+ !F_ISSET(txn->mt_flags, MDB_TXN_METOGGLE), txn->mt_dbs[MAIN_DBI].md_root);
env = txn->mt_env;
{
MDB_env *e;
- e = calloc(1, sizeof(*e));
+ e = calloc(1, sizeof(MDB_env));
if (!e) return ENOMEM;
e->me_maxreaders = DEFAULT_READERS;
int
mdbenv_set_maxdbs(MDB_env *env, int dbs)
{
- if (env->me_map)
- return EINVAL;
env->me_maxdbs = dbs;
return MDB_SUCCESS;
}
return MDB_SUCCESS;
}
-int
+static int
mdbenv_open2(MDB_env *env, unsigned int flags)
{
int i, newenv = 0;
} else {
if (env->me_txns->mt_magic != MDB_MAGIC) {
DPRINTF("lock region has invalid magic");
- errno = EINVAL;
+ rc = EINVAL;
+ goto fail;
}
if (env->me_txns->mt_version != MDB_VERSION) {
DPRINTF("lock region is version %u, expected version %u",
env->me_txns->mt_version, MDB_VERSION);
- errno = EINVAL;
+ rc = MDB_VERSION_MISMATCH;
+ goto fail;
}
if (errno != EACCES && errno != EAGAIN) {
rc = errno;
}
+#define LOCKNAME "/lock.mdb"
+#define DATANAME "/data.mdb"
int
mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
{
char *lpath, *dpath;
len = strlen(path);
- lpath = malloc(len + sizeof("/lock.mdb") + len + sizeof("/data.db"));
+ lpath = malloc(len + sizeof(LOCKNAME) + len + sizeof(DATANAME));
if (!lpath)
return ENOMEM;
- dpath = lpath + len + sizeof("/lock.mdb");
- sprintf(lpath, "%s/lock.mdb", path);
- sprintf(dpath, "%s/data.mdb", path);
+ dpath = lpath + len + sizeof(LOCKNAME);
+ sprintf(lpath, "%s" LOCKNAME, path);
+ sprintf(dpath, "%s" DATANAME, path);
rc = mdbenv_setup_locks(env, lpath, mode, &excl);
if (rc)
if (env == NULL)
return;
+ free(env->me_dbs[1]);
+ free(env->me_dbs[0]);
free(env->me_dbxs);
free(env->me_path);
DPRINTF("pushing page %lu on cursor %p", mp->mp_pgno, (void *) cursor);
- if ((ppage = calloc(1, sizeof(*ppage))) == NULL)
+ if ((ppage = calloc(1, sizeof(MDB_ppage))) == NULL)
return NULL;
ppage->mp_page = mp;
CURSOR_PUSH(cursor, ppage);
}
}
if (!found) {
+ if (pgno > txn->mt_env->me_meta->mm_last_pg)
+ return NULL;
p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
}
return p;
if (root == P_INVALID) { /* Tree is empty. */
DPRINTF("tree is empty");
- return ENOENT;
+ return MDB_NOTFOUND;
}
if ((mpp->mp_page = mdb_get_page(txn, root)) == NULL)
/* For sub-databases, update main root first */
if (dbi > MAIN_DBI && !txn->mt_dbxs[dbi].md_dirty) {
MDB_pageparent mp2;
- rc = mdb_search_page(txn, 0, &txn->mt_dbxs[dbi].md_name,
+ rc = mdb_search_page(txn, MAIN_DBI, &txn->mt_dbxs[dbi].md_name,
NULL, 1, &mp2);
if (rc)
return rc;
assert(data);
DPRINTF("===> get key [%.*s]", (int)key->mv_size, (char *)key->mv_data);
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ return EINVAL;
+
if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
return EINVAL;
}
return rc;
leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, NULL);
- if (leaf && exact)
+ if (leaf && exact) {
+ /* Return first duplicate data item */
+ if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
+ MDB_xcursor mx;
+
+ mdb_xcursor_init0(txn, dbi, &mx);
+ 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;
+ leaf = NODEPTR(mpp.mp_page, 0);
+ }
rc = mdb_read_data(txn, leaf, data);
- else {
- rc = ENOENT;
+ } else {
+ rc = MDB_NOTFOUND;
}
return rc;
top = CURSOR_TOP(cursor);
if ((parent = SLIST_NEXT(top, mp_entry)) == NULL) {
- return ENOENT; /* root has no siblings */
+ return MDB_NOTFOUND; /* root has no siblings */
}
DPRINTF("parent page is page %lu, index %u",
}
static int
-mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
+mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
{
MDB_ppage *top;
MDB_page *mp;
MDB_node *leaf;
+ int rc;
if (cursor->mc_eof) {
- return ENOENT;
+ return MDB_NOTFOUND;
}
assert(cursor->mc_initialized);
+ if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
+ if (op == MDB_NEXT || op == MDB_NEXT_DUP) {
+ rc = mdb_cursor_next(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_NEXT);
+ if (op != MDB_NEXT || rc == MDB_SUCCESS)
+ return rc;
+ }
+ }
+
top = CURSOR_TOP(cursor);
mp = top->mp_page;
DPRINTF("=====> move to next sibling page");
if (mdb_sibling(cursor, 1) != MDB_SUCCESS) {
cursor->mc_eof = 1;
- return ENOENT;
+ return MDB_NOTFOUND;
}
top = CURSOR_TOP(cursor);
mp = top->mp_page;
assert(IS_LEAF(mp));
leaf = NODEPTR(mp, top->mp_ki);
- if (data && mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS)
- return MDB_FAIL;
+ if (data) {
+ if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
+ 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, leaf);
+ rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ if (rc != MDB_SUCCESS)
+ return rc;
+ }
+ }
return mdb_set_key(leaf, key);
}
static int
-mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
+mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
{
MDB_ppage *top;
MDB_page *mp;
MDB_node *leaf;
+ int rc;
assert(cursor->mc_initialized);
+ if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
+ if (op == MDB_PREV || op == MDB_PREV_DUP) {
+ rc = mdb_cursor_prev(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
+ if (op != MDB_PREV || rc == MDB_SUCCESS)
+ return rc;
+ }
+ }
+
top = CURSOR_TOP(cursor);
mp = top->mp_page;
if (top->mp_ki == 0) {
DPRINTF("=====> move to prev sibling page");
if (mdb_sibling(cursor, 0) != MDB_SUCCESS) {
- return ENOENT;
+ return MDB_NOTFOUND;
}
top = CURSOR_TOP(cursor);
mp = top->mp_page;
assert(IS_LEAF(mp));
leaf = NODEPTR(mp, top->mp_ki);
- if (data && mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS)
- return MDB_FAIL;
+ if (data) {
+ if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
+ 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, leaf);
+ rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ if (rc != MDB_SUCCESS)
+ return rc;
+ }
+ }
return mdb_set_key(leaf, key);
}
static int
mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
- int *exactp)
+ MDB_cursor_op op, int *exactp)
{
int rc;
MDB_node *leaf;
assert(key);
assert(key->mv_size > 0);
+ while (CURSOR_TOP(cursor) != NULL)
+ cursor_pop_page(cursor);
+
rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, key, cursor, 0, &mpp);
if (rc != MDB_SUCCESS)
return rc;
leaf = mdb_search_node(cursor->mc_txn, cursor->mc_dbi, mpp.mp_page, key, exactp, &top->mp_ki);
if (exactp != NULL && !*exactp) {
/* MDB_SET specified and not an exact match. */
- return ENOENT;
+ return MDB_NOTFOUND;
}
if (leaf == NULL) {
cursor->mc_initialized = 1;
cursor->mc_eof = 0;
- if (data && (rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
- return rc;
+ if (data) {
+ if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
+ 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, leaf);
+ if (op == MDB_SET || op == MDB_SET_RANGE) {
+ rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ } else {
+ int ex2, *ex2p;
+ MDB_cursor_op op2;
+ if (op == MDB_GET_BOTH) {
+ ex2p = &ex2;
+ op2 = MDB_SET;
+ } else {
+ ex2p = NULL;
+ op2 = MDB_SET_RANGE;
+ }
+ rc = mdb_cursor_set(&cursor->mc_xcursor->mx_cursor, data, NULL, op2, ex2p);
+ if (rc != MDB_SUCCESS)
+ return rc;
+ }
+ }
+ }
rc = mdb_set_key(leaf, key);
if (rc == MDB_SUCCESS) {
MDB_pageparent mpp;
MDB_node *leaf;
+ while (CURSOR_TOP(cursor) != NULL)
+ cursor_pop_page(cursor);
+
rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, NULL, cursor, 0, &mpp);
if (rc != MDB_SUCCESS)
return rc;
cursor->mc_initialized = 1;
cursor->mc_eof = 0;
- if (data && (rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
- return rc;
+ if (data) {
+ if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
+ 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, leaf);
+ rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ if (rc)
+ return rc;
+ }
+ }
return mdb_set_key(leaf, key);
}
MDB_node *leaf;
MDB_val lkey;
+ while (CURSOR_TOP(cursor) != NULL)
+ cursor_pop_page(cursor);
+
lkey.mv_size = MAXKEYSIZE+1;
lkey.mv_data = NULL;
top = CURSOR_TOP(cursor);
top->mp_ki = NUMKEYS(top->mp_page) - 1;
- if (data && (rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
- return rc;
+ if (data) {
+ if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
+ 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, leaf);
+ rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ if (rc)
+ return rc;
+ }
+ }
return mdb_set_key(leaf, key);
}
assert(cursor);
switch (op) {
+ case MDB_GET_BOTH:
+ case MDB_GET_BOTH_RANGE:
+ if (data == NULL) {
+ rc = EINVAL;
+ break;
+ }
+ /* FALLTHRU */
case MDB_SET:
case MDB_SET_RANGE:
- while (CURSOR_TOP(cursor) != NULL)
- cursor_pop_page(cursor);
if (key == NULL || key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
rc = EINVAL;
- } else if (op == MDB_SET)
- rc = mdb_cursor_set(cursor, key, data, &exact);
+ } else if (op != MDB_SET_RANGE)
+ rc = mdb_cursor_set(cursor, key, data, op, NULL);
else
- rc = mdb_cursor_set(cursor, key, data, NULL);
+ rc = mdb_cursor_set(cursor, key, data, op, &exact);
break;
case MDB_NEXT:
+ case MDB_NEXT_DUP:
+ case MDB_NEXT_NODUP:
if (!cursor->mc_initialized)
rc = mdb_cursor_first(cursor, key, data);
else
- rc = mdb_cursor_next(cursor, key, data);
+ rc = mdb_cursor_next(cursor, key, data, op);
break;
case MDB_PREV:
- if (!cursor->mc_initialized || cursor->mc_eof) {
- while (CURSOR_TOP(cursor) != NULL)
- cursor_pop_page(cursor);
+ case MDB_PREV_DUP:
+ case MDB_PREV_NODUP:
+ if (!cursor->mc_initialized || cursor->mc_eof)
rc = mdb_cursor_last(cursor, key, data);
- } else
- rc = mdb_cursor_prev(cursor, key, data);
+ else
+ rc = mdb_cursor_prev(cursor, key, data, op);
break;
case MDB_FIRST:
- while (CURSOR_TOP(cursor) != NULL)
- cursor_pop_page(cursor);
rc = mdb_cursor_first(cursor, key, data);
break;
case MDB_LAST:
- while (CURSOR_TOP(cursor) != NULL)
- cursor_pop_page(cursor);
rc = mdb_cursor_last(cursor, key, data);
break;
default:
{
MDB_dbi dbn;
- mx->mx_cursor.mc_txn = &mx->mx_txn;
mx->mx_txn = *txn;
mx->mx_txn.mt_dbxs = mx->mx_dbxs;
mx->mx_txn.mt_dbs = mx->mx_dbs;
mx->mx_dbxs[dbn+1].md_rel = mx->mx_dbxs[dbn].md_rel;
mx->mx_dbxs[dbn+1].md_dirty = 0;
mx->mx_txn.mt_numdbs = dbn+2;
+
+ SLIST_INIT(&mx->mx_cursor.mc_stack);
+ mx->mx_cursor.mc_txn = &mx->mx_txn;
+ mx->mx_cursor.mc_dbi = dbn+1;
}
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;
txn->mt_dbxs[1].md_dirty = mx->mx_dbxs[1].md_dirty;
if (dbi > 1) {
txn->mt_dbs[dbi] = mx->mx_dbs[2];
- txn->mt_dbxs[2].md_dirty = mx->mx_dbxs[2].md_dirty;
+ txn->mt_dbxs[dbi].md_dirty = mx->mx_dbxs[2].md_dirty;
}
}
return MDB_SUCCESS;
}
+/* Return the count of duplicate data items for the current key */
+int
+mdb_cursor_count(MDB_cursor *mc, unsigned long *countp)
+{
+ if (mc == NULL || countp == NULL)
+ return EINVAL;
+
+ if (!(mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_DUPSORT))
+ return EINVAL;
+
+ if (!mc->mc_xcursor->mx_cursor.mc_initialized)
+ return EINVAL;
+
+ *countp = mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries;
+ return MDB_SUCCESS;
+}
+
void
mdb_cursor_close(MDB_cursor *cursor)
{
if (cursor != NULL) {
- while (!CURSOR_EMPTY(cursor))
+ while(!CURSOR_EMPTY(cursor))
cursor_pop_page(cursor);
+ if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
+ mdb_xcursor_fini(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor);
+ while(!CURSOR_EMPTY(&cursor->mc_xcursor->mx_cursor))
+ cursor_pop_page(&cursor->mc_xcursor->mx_cursor);
+ }
-/* btree_close(cursor->bt); */
free(cursor);
}
}
int
mdb_del(MDB_txn *txn, MDB_dbi dbi,
- MDB_val *key, MDB_val *data)
+ MDB_val *key, MDB_val *data,
+ unsigned int flags)
{
int rc, exact;
unsigned int ki;
assert(key != NULL);
- if (txn == NULL || dbi >= txn->mt_numdbs)
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
return EINVAL;
if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
if (leaf == NULL || !exact) {
- return ENOENT;
+ return MDB_NOTFOUND;
}
- if (data && (rc = mdb_read_data(txn, leaf, data)) != MDB_SUCCESS)
- return rc;
-
if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
- /* add all the child DB's pages to the free list */
- MDB_cursor mc;
MDB_xcursor mx;
MDB_pageparent mp2;
mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
- SLIST_INIT(&mc.mc_stack);
- mc.mc_dbi = mx.mx_txn.mt_numdbs-1;
- mc.mc_txn = &mx.mx_txn;
- rc = mdb_search_page(&mx.mx_txn, mx.mx_txn.mt_numdbs - 1, NULL, &mc, 0, &mp2);
- if (rc == MDB_SUCCESS) {
- MDB_ppage *top, *parent;
- MDB_node *ni;
- unsigned int i;
+ 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);
+ if (rc != MDB_SUCCESS)
+ return rc;
+ /* If sub-DB still has entries, we're done */
+ if (mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root != P_INVALID) {
+ memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
+ sizeof(MDB_db));
+ return rc;
+ }
+ /* otherwise fall thru and delete the sub-DB */
+ } else {
+ /* add all the child DB's pages to the free list */
+ rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi,
+ NULL, &mx.mx_cursor, 0, &mp2);
+ if (rc == MDB_SUCCESS) {
+ MDB_ppage *top, *parent;
+ MDB_node *ni;
+ unsigned int i;
- cursor_pop_page(&mc);
- top = CURSOR_TOP(&mc);
- parent = SLIST_NEXT(top, mp_entry);
- do {
- for (i=0; i<NUMKEYS(top->mp_page); i++) {
- ni = NODEPTR(top->mp_page, i);
- mdb_idl_insert(txn->mt_free_pgs, ni->mn_pgno);
- }
- if (parent) {
- parent->mp_ki++;
- if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
- cursor_pop_page(&mc);
- top = CURSOR_TOP(&mc);
- parent = SLIST_NEXT(top, mp_entry);
- } else {
- ni = NODEPTR(parent->mp_page, parent->mp_ki);
- top = mdb_get_page(mc.mc_txn, ni->mn_pgno);
+ cursor_pop_page(&mx.mx_cursor);
+ top = CURSOR_TOP(&mx.mx_cursor);
+ if (top != NULL) {
+ parent = SLIST_NEXT(top, mp_entry);
+ while (parent != NULL) {
+ for (i=0; i<NUMKEYS(top->mp_page); i++) {
+ ni = NODEPTR(top->mp_page, i);
+ mdb_idl_insert(txn->mt_free_pgs, ni->mn_pgno);
+ }
+ if (parent) {
+ parent->mp_ki++;
+ if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
+ cursor_pop_page(&mx.mx_cursor);
+ top = CURSOR_TOP(&mx.mx_cursor);
+ parent = SLIST_NEXT(top, mp_entry);
+ } else {
+ ni = NODEPTR(parent->mp_page, parent->mp_ki);
+ top->mp_page = mdb_get_page(&mx.mx_txn, ni->mn_pgno);
+ }
+ }
}
}
+ mdb_idl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root);
}
- mdb_idl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mc.mc_dbi].md_root);
}
}
+ if (data && (rc = mdb_read_data(txn, leaf, data)) != MDB_SUCCESS)
+ return rc;
+
return mdb_del0(txn, dbi, ki, &mpp, leaf);
}
return rc;
}
-int
-mdb_put(MDB_txn *txn, MDB_dbi dbi,
+static int
+mdb_put0(MDB_txn *txn, MDB_dbi dbi,
MDB_val *key, MDB_val *data, unsigned int flags)
{
int rc = MDB_SUCCESS, exact;
MDB_val xdata, *rdata;
MDB_db dummy;
- assert(key != NULL);
- assert(data != NULL);
-
- if (txn == NULL)
- return EINVAL;
-
- if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
- return EINVAL;
- }
-
- if (txn->mt_env->me_txn != txn) {
- return EINVAL;
- }
-
- if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
- return EINVAL;
- }
-
DPRINTF("==> put key %.*s, size %zu, data size %zu",
(int)key->mv_size, (char *)key->mv_data, key->mv_size, data->mv_size);
if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
goto put_sub;
}
- if (F_ISSET(flags, MDB_NOOVERWRITE)) {
+ if (flags == MDB_NOOVERWRITE) {
DPRINTF("duplicate key %.*s",
(int)key->mv_size, (char *)key->mv_data);
- return EEXIST;
+ return MDB_KEYEXIST;
}
/* same size, just replace it */
if (NODEDSZ(leaf) == data->mv_size) {
ki = NUMKEYS(mpp.mp_page);
DPRINTF("appending key at index %i", ki);
}
- } else if (rc == ENOENT) {
+ } else if (rc == MDB_NOTFOUND) {
MDB_dpage *dp;
/* new file, just write a root leaf page */
DPRINTF("allocating new root leaf page");
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)
txn->mt_flags |= MDB_TXN_ERROR;
else {
txn->mt_dbs[dbi].md_entries++;
+
+ /* Remember if we just added a subdatabase */
+ if (flags & F_SUBDATA) {
+ leaf = NODEPTR(mpp.mp_page, ki);
+ leaf->mn_flags |= F_SUBDATA;
+ }
+
/* Now store the actual data in the child DB. Note that we're
* storing the user data in the keys field, so there are strict
* size limits on dupdata. The actual data fields of the child
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 = "";
- rc = mdb_put(&mx.mx_txn, mx.mx_txn.mt_numdbs-1, data, &xdata, flags);
+ if (flags == MDB_NODUPDATA)
+ flags = MDB_NOOVERWRITE;
+ rc = mdb_put0(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, &xdata, flags);
mdb_xcursor_fini(txn, dbi, &mx);
+ memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
+ sizeof(MDB_db));
}
}
return rc;
}
+int
+mdb_put(MDB_txn *txn, MDB_dbi dbi,
+ MDB_val *key, MDB_val *data, unsigned int flags)
+{
+ assert(key != NULL);
+ assert(data != NULL);
+
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ return EINVAL;
+
+ if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
+ return EINVAL;
+ }
+
+ if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
+ return EINVAL;
+ }
+
+ if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA)) != flags)
+ return EINVAL;
+
+ return mdb_put0(txn, dbi, key, data, flags);
+}
+
int
mdbenv_get_flags(MDB_env *env, unsigned int *arg)
{
return MDB_SUCCESS;
}
+static int
+mdb_stat0(MDB_env *env, MDB_db *db, MDB_stat *arg)
+{
+ arg->ms_psize = env->me_psize;
+ arg->ms_depth = db->md_depth;
+ arg->ms_branch_pages = db->md_branch_pages;
+ arg->ms_leaf_pages = db->md_leaf_pages;
+ arg->ms_overflow_pages = db->md_overflow_pages;
+ arg->ms_entries = db->md_entries;
+
+ return MDB_SUCCESS;
+}
int
mdbenv_stat(MDB_env *env, MDB_stat *arg)
{
if (env == NULL || arg == NULL)
return EINVAL;
- arg->ms_psize = env->me_psize;
- arg->ms_depth = env->me_meta->mm_dbs[MAIN_DBI].md_depth;
- arg->ms_branch_pages = env->me_meta->mm_dbs[MAIN_DBI].md_branch_pages;
- arg->ms_leaf_pages = env->me_meta->mm_dbs[MAIN_DBI].md_leaf_pages;
- arg->ms_overflow_pages = env->me_meta->mm_dbs[MAIN_DBI].md_overflow_pages;
- arg->ms_entries = env->me_meta->mm_dbs[MAIN_DBI].md_entries;
-
- return MDB_SUCCESS;
+ return mdb_stat0(env, &env->me_meta->mm_dbs[MAIN_DBI], arg);
}
int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
{
MDB_val key, data;
MDB_dbi i;
- int rc;
+ int rc, dirty = 0;
size_t len;
/* main DB? */
if (!name) {
*dbi = MAIN_DBI;
+ if (flags & (MDB_DUPSORT|MDB_REVERSEKEY|MDB_INTEGERKEY))
+ txn->mt_dbs[MAIN_DBI].md_flags |= (flags & (MDB_DUPSORT|MDB_REVERSEKEY|MDB_INTEGERKEY));
return MDB_SUCCESS;
}
rc = mdb_get(txn, MAIN_DBI, &key, &data);
/* Create if requested */
- if (rc == ENOENT && (flags & MDB_CREATE)) {
+ if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
MDB_db dummy;
data.mv_size = sizeof(MDB_db);
data.mv_data = &dummy;
memset(&dummy, 0, sizeof(dummy));
dummy.md_root = P_INVALID;
dummy.md_flags = flags & 0xffff;
- rc = mdb_put(txn, 0, &key, &data, 0);
+ rc = mdb_put0(txn, MAIN_DBI, &key, &data, F_SUBDATA);
+ dirty = 1;
}
/* OK, got info, add to table */
txn->mt_dbxs[txn->mt_numdbs].md_dcmp = NULL;
txn->mt_dbxs[txn->mt_numdbs].md_rel = NULL;
txn->mt_dbxs[txn->mt_numdbs].md_parent = MAIN_DBI;
- txn->mt_dbxs[txn->mt_numdbs].md_dirty = 0;
+ txn->mt_dbxs[txn->mt_numdbs].md_dirty = dirty;
memcpy(&txn->mt_dbs[txn->mt_numdbs], data.mv_data, sizeof(MDB_db));
*dbi = txn->mt_numdbs;
txn->mt_numdbs++;
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
{
- if (txn == NULL || arg == NULL)
+ if (txn == NULL || arg == NULL || dbi >= txn->mt_numdbs)
return EINVAL;
- arg->ms_psize = txn->mt_env->me_psize;
- arg->ms_depth = txn->mt_dbs[dbi].md_depth;
- arg->ms_branch_pages = txn->mt_dbs[dbi].md_branch_pages;
- arg->ms_leaf_pages = txn->mt_dbs[dbi].md_leaf_pages;
- arg->ms_overflow_pages = txn->mt_dbs[dbi].md_overflow_pages;
- arg->ms_entries = txn->mt_dbs[dbi].md_entries;
-
- return MDB_SUCCESS;
+ return mdb_stat0(txn->mt_env, &txn->mt_dbs[dbi], arg);
}
void mdb_close(MDB_txn *txn, MDB_dbi dbi)
txn->mt_dbxs[dbi].md_name.mv_size = 0;
free(ptr);
}
+
+int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
+{
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ return EINVAL;
+
+ txn->mt_dbxs[dbi].md_cmp = cmp;
+ return MDB_SUCCESS;
+}
+
+int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
+{
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ return EINVAL;
+
+ txn->mt_dbxs[dbi].md_dcmp = cmp;
+ return MDB_SUCCESS;
+}
+
+int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
+{
+ if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+ return EINVAL;
+
+ txn->mt_dbxs[dbi].md_rel = rel;
+ return MDB_SUCCESS;
+}