]> git.sur5r.net Git - openldap/blobdiff - libraries/libmdb/mdb.c
Add another FIXME comment
[openldap] / libraries / libmdb / mdb.c
index 95db54a2c76c28a36df5c60efbc6efa6f90ab865..489bbde65618e372a2e9df5b28051fa3f4ef6531 100644 (file)
@@ -39,9 +39,7 @@
 #include <fcntl.h>
 
 #include <assert.h>
-#include <err.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -50,7 +48,6 @@
 #include <time.h>
 #include <unistd.h>
 #include <pthread.h>
-#include <endian.h>
 
 #include "mdb.h"
 
@@ -95,7 +92,11 @@ typedef struct MDB_rxbody {
 } MDB_rxbody;
 
 #ifndef CACHELINE
-#define CACHELINE      64      /* most CPUs. Itanium uses 128 */
+# ifdef __APPLE__
+#  define CACHELINE    128     /* 64 is too small to contain a mutex */
+# else
+#  define CACHELINE    64      /* most CPUs. Itanium uses 128 */
+# endif
 #endif
 
 typedef struct MDB_reader {
@@ -225,6 +226,9 @@ typedef struct MDB_ppage {                                  /* ordered list of pages */
 } MDB_ppage;
 SLIST_HEAD(page_stack, MDB_ppage);
 
+/* FIXME: tree depth is mostly bounded, we should just
+ * use a fixed array and avoid malloc/pointer chasing
+ */
 #define CURSOR_EMPTY(c)                 SLIST_EMPTY(&(c)->mc_stack)
 #define CURSOR_TOP(c)           SLIST_FIRST(&(c)->mc_stack)
 #define CURSOR_POP(c)           SLIST_REMOVE_HEAD(&(c)->mc_stack, mp_entry)
@@ -254,10 +258,10 @@ typedef struct MDB_node {
        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 */
@@ -354,6 +358,8 @@ static int  mdb_add_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
 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);
@@ -375,18 +381,18 @@ static MDB_ppage *cursor_push_page(MDB_cursor *cursor,
 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_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,
@@ -440,7 +446,11 @@ mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
 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);
@@ -452,8 +462,11 @@ mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num)
 {
        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;
@@ -534,6 +547,7 @@ mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num)
                        }
                }
        }
+       }
 
        if ((dp = malloc(txn->mt_env->me_psize * num + sizeof(MDB_dhead))) == NULL)
                return NULL;
@@ -598,7 +612,7 @@ mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **ret)
        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;
        }
@@ -770,7 +784,7 @@ mdb_txn_commit(MDB_txn *txn)
                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;
        }
@@ -802,7 +816,7 @@ mdb_txn_commit(MDB_txn *txn)
                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
@@ -815,7 +829,7 @@ mdb_txn_commit(MDB_txn *txn)
                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);
                        }
                }
        }
@@ -963,7 +977,7 @@ mdbenv_read_header(MDB_env *env, MDB_meta *meta)
        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));
@@ -986,10 +1000,7 @@ mdbenv_init_meta(MDB_env *env, MDB_meta *meta)
        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;
 
@@ -1025,7 +1036,8 @@ mdbenv_write_meta(MDB_txn *txn)
        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;
 
@@ -1078,7 +1090,7 @@ mdbenv_create(MDB_env **env)
 {
        MDB_env *e;
 
-       e = calloc(1, sizeof(*e));
+       e = calloc(1, sizeof(MDB_env));
        if (!e) return ENOMEM;
 
        e->me_maxreaders = DEFAULT_READERS;
@@ -1101,8 +1113,6 @@ mdbenv_set_mapsize(MDB_env *env, size_t size)
 int
 mdbenv_set_maxdbs(MDB_env *env, int dbs)
 {
-       if (env->me_map)
-               return EINVAL;
        env->me_maxdbs = dbs;
        return MDB_SUCCESS;
 }
@@ -1123,7 +1133,7 @@ mdbenv_get_maxreaders(MDB_env *env, int *readers)
        return MDB_SUCCESS;
 }
 
-int
+static int
 mdbenv_open2(MDB_env *env, unsigned int flags)
 {
        int i, newenv = 0;
@@ -1275,12 +1285,14 @@ mdbenv_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
        } 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;
@@ -1295,6 +1307,8 @@ fail:
 
 }
 
+#define LOCKNAME       "/lock.mdb"
+#define DATANAME       "/data.mdb"
 int
 mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
 {
@@ -1302,12 +1316,12 @@ 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)
@@ -1347,6 +1361,8 @@ mdbenv_close(MDB_env *env)
        if (env == NULL)
                return;
 
+       free(env->me_dbs[1]);
+       free(env->me_dbs[0]);
        free(env->me_dbxs);
        free(env->me_path);
 
@@ -1452,7 +1468,7 @@ cursor_push_page(MDB_cursor *cursor, MDB_page *mp)
 
        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);
@@ -1584,7 +1600,7 @@ mdb_search_page(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
 
        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)
@@ -1596,7 +1612,7 @@ mdb_search_page(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
                /* 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;
@@ -1651,6 +1667,9 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
        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;
        }
@@ -1665,15 +1684,15 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
                        MDB_xcursor mx;
 
                        mdb_xcursor_init0(txn, dbi, &mx);
-                       mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
-                       rc = mdb_search_page(&mx.mx_txn, mx.mx_txn.mt_numdbs-1, NULL, NULL, 0, &mpp);
+                       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;
+               rc = MDB_NOTFOUND;
        }
 
        return rc;
@@ -1689,7 +1708,7 @@ mdb_sibling(MDB_cursor *cursor, int move_right)
 
        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",
@@ -1739,18 +1758,27 @@ mdb_set_key(MDB_node *node, MDB_val *key)
 }
 
 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;
 
@@ -1760,7 +1788,7 @@ mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
                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;
@@ -1774,21 +1802,39 @@ mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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;
 
@@ -1797,7 +1843,7 @@ mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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;
@@ -1814,15 +1860,24 @@ mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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;
@@ -1833,6 +1888,9 @@ mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
        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;
@@ -1842,7 +1900,7 @@ mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
        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) {
@@ -1859,8 +1917,30 @@ mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
        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) {
@@ -1879,6 +1959,9 @@ mdb_cursor_first(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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;
@@ -1888,9 +1971,17 @@ mdb_cursor_first(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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);
 }
 
@@ -1903,6 +1994,9 @@ mdb_cursor_last(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        MDB_node        *leaf;
        MDB_val lkey;
 
+       while (CURSOR_TOP(cursor) != NULL)
+               cursor_pop_page(cursor);
+
        lkey.mv_size = MAXKEYSIZE+1;
        lkey.mv_data = NULL;
 
@@ -1918,8 +2012,17 @@ mdb_cursor_last(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
        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);
 }
@@ -1934,39 +2037,42 @@ mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
        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:
@@ -2169,7 +2275,6 @@ mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
 {
        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;
@@ -2186,31 +2291,46 @@ mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
        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;
        }
 }
 
@@ -2244,14 +2364,35 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
        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);
        }
 }
@@ -2556,7 +2697,8 @@ mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki, MDB_pageparent *mpp, MDB_no
 
 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;
@@ -2567,7 +2709,7 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
 
        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)) {
@@ -2585,53 +2727,66 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
 
        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->mp_page = 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);
 }
 
@@ -2792,8 +2947,8 @@ mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp, unsigned int *newindxp,
        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;
@@ -2803,24 +2958,6 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
        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);
 
@@ -2833,10 +2970,10 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
                        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) {
@@ -2849,7 +2986,7 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
                        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");
@@ -2882,17 +3019,24 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
                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
@@ -2904,11 +3048,15 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
                        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));
                }
        }
 
@@ -2916,6 +3064,30 @@ done:
        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)
 {
@@ -2936,32 +3108,39 @@ mdbenv_get_path(MDB_env *env, const char **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;
        }
 
@@ -2984,14 +3163,15 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
        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 */
@@ -3002,7 +3182,7 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
                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++;
@@ -3013,17 +3193,10 @@ int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
 
 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)
@@ -3036,3 +3209,30 @@ 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;
+}