#include <string.h>
#include <time.h>
#include <unistd.h>
+
+#ifndef _WIN32
+#include <pthread.h>
+#endif
+
+#include "mdb.h"
+#include "midl.h"
+
+/** @defgroup internal MDB Internals
+ * @{
+ */
+/** @defgroup compat Windows Compatibility Macros
+ * @{
+ */
#ifdef _WIN32
#define pthread_t DWORD
#define pthread_mutex_t HANDLE
#define LOCK_MUTEX_W(env) pthread_mutex_lock(env->me_wmutex)
#define UNLOCK_MUTEX_W(env) pthread_mutex_unlock(env->me_wmutex)
#define getpid() GetCurrentProcessId()
-#else
-#include <pthread.h>
-#define LOCK_MUTEX_R(env) pthread_mutex_lock(&env->me_txns->mti_mutex)
-#define UNLOCK_MUTEX_R(env) pthread_mutex_unlock(&env->me_txns->mti_mutex)
-#define LOCK_MUTEX_W(env) pthread_mutex_lock(&env->me_txns->mti_wmutex)
-#define UNLOCK_MUTEX_W(env) pthread_mutex_unlock(&env->me_txns->mti_wmutex)
-#endif
-
-#ifdef _WIN32
#define fdatasync(fd) !FlushFileBuffers(fd)
#define ErrCode() GetLastError()
#define GetPageSize(x) {SYSTEM_INFO si; GetSystemInfo(&si); (x) = si.dwPageSize;}
#define close(fd) CloseHandle(fd)
#define munmap(ptr,len) UnmapViewOfFile(ptr)
#else
+#define LOCK_MUTEX_R(env) pthread_mutex_lock(&env->me_txns->mti_mutex)
+#define UNLOCK_MUTEX_R(env) pthread_mutex_unlock(&env->me_txns->mti_mutex)
+#define LOCK_MUTEX_W(env) pthread_mutex_lock(&env->me_txns->mti_wmutex)
+#define UNLOCK_MUTEX_W(env) pthread_mutex_unlock(&env->me_txns->mti_wmutex)
#define ErrCode() errno
#define HANDLE int
#define INVALID_HANDLE_VALUE -1
#define GetPageSize(x) (x) = sysconf(_SC_PAGE_SIZE)
#endif
-#include "mdb.h"
-#define ULONG unsigned long
-typedef ULONG pgno_t;
-
-#include "midl.h"
+/** @} */
+#ifndef _WIN32
/* Note: If O_DSYNC is undefined but exists in /usr/include,
* preferably set some compiler flag to get the definition.
* Otherwise compile with the less efficient -DMDB_DSYNC=O_SYNC.
#ifndef MDB_DSYNC
# define MDB_DSYNC O_DSYNC
#endif
+#endif
+
+typedef ULONG pgno_t;
#ifndef DEBUG
#define DEBUG 0
struct MDB_xcursor;
struct MDB_cursor {
+ struct MDB_xcursor *mc_xcursor;
MDB_txn *mc_txn;
- MDB_ppage mc_stack[CURSOR_STACK]; /* stack of parent pages */
- unsigned int mc_snum; /* number of pushed pages */
MDB_dbi mc_dbi;
- short mc_initialized; /* 1 if initialized */
- short mc_eof; /* 1 if end is reached */
- struct MDB_xcursor *mc_xcursor;
+ unsigned int mc_snum; /* number of pushed pages */
+ unsigned int mc_flags;
+#define C_INITIALIZED 0x01
+#define C_EOF 0x02
+#define C_XDIRTY 0x04
+ MDB_ppage mc_stack[CURSOR_STACK]; /* stack of parent pages */
};
#define METADATA(p) ((void *)((char *)(p) + PAGEHDRSZ))
+/* We guarantee 2-byte alignment for nodes */
typedef struct MDB_node {
-#define mn_pgno mn_p.np_pgno
-#define mn_dsize mn_p.np_dsize
- union {
- pgno_t np_pgno; /* child page number */
- uint32_t np_dsize; /* leaf data size */
- } mn_p;
- unsigned int mn_flags:4;
- unsigned int mn_ksize:12; /* key size */
+ /* lo and hi are used for data size on leaf nodes and for
+ * child pgno on branch nodes. On 64 bit platforms, flags
+ * is also used for pgno. (branch nodes ignore flags)
+ */
+ unsigned short mn_lo;
+ unsigned short mn_hi;
+ unsigned short mn_flags;
#define F_BIGDATA 0x01 /* data put on overflow page */
#define F_SUBDATA 0x02 /* data is a sub-database */
#define F_DUPDATA 0x04 /* data has duplicates */
+ unsigned short mn_ksize; /* key size */
char mn_data[1];
} MDB_node;
MDB_env *mt_env;
pgno_t *mt_free_pgs; /* this is an IDL */
union {
- MIDL2 *dirty_list; /* modified pages */
+ ID2L dirty_list; /* modified pages */
MDB_reader *reader;
} mt_u;
MDB_dbx *mt_dbxs; /* array */
pthread_key_t me_txkey; /* thread-key for readers */
MDB_dpage *me_dpages;
pgno_t me_free_pgs[MDB_IDL_UM_SIZE];
- MIDL2 me_dirty_list[MDB_IDL_DB_SIZE];
+ ID2 me_dirty_list[MDB_IDL_UM_SIZE];
LAZY_RWLOCK_DEF(me_dblock);
#ifdef _WIN32
HANDLE me_rmutex; /* Windows mutexes don't reside in shared mem */
#define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i]))
#define NODEKEY(node) (void *)((node)->mn_data)
#define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize)
-#define NODEPGNO(node) ((node)->mn_pgno)
-#define NODEDSZ(node) ((node)->mn_dsize)
+#if LONG_MAX == 0x7fffffff
+#define NODEPGNO(node) ((node)->mn_lo | ((node)->mn_hi << 16))
+#define SETPGNO(node,pgno) do { \
+ (node)->mn_lo = (pgno) & 0xffff; (node)->mn_hi = (pgno) >> 16;} while(0)
+#else
+#define NODEPGNO(node) ((node)->mn_lo | ((node)->mn_hi << 16) | ((unsigned long)(node)->mn_flags << 32))
+#define SETPGNO(node,pgno) do { \
+ (node)->mn_lo = (pgno) & 0xffff; (node)->mn_hi = (pgno) >> 16; \
+ (node)->mn_flags = (pgno) >> 32; } while(0)
+#endif
+#define NODEDSZ(node) ((node)->mn_lo | ((unsigned)(node)->mn_hi << 16))
+#define SETDSZ(node,size) do { \
+ (node)->mn_lo = (size) & 0xffff; (node)->mn_hi = (size) >> 16;} while(0)
#define NODEKSZ(node) ((node)->mn_ksize)
#define LEAF2KEY(p, i, ks) ((char *)(p) + PAGEHDRSZ + ((i)*(ks)))
indx_t indx, MDB_val *key, MDB_val *data,
pgno_t pgno, uint8_t flags);
static void mdb_del_node(MDB_page *mp, indx_t indx, int ksize);
-static int mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki,
+static int mdb_del0(MDB_cursor *mc, 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_update_key(MDB_page *mp, indx_t indx, MDB_val *key);
-static int mdb_move_node(MDB_txn *txn, MDB_dbi dbi,
+static int mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mp);
+static int mdb_update_key(MDB_page *mp, indx_t indx, MDB_val *key);
+static int mdb_move_node(MDB_txn *txn, MDB_dbi dbi,
MDB_pageparent *src, indx_t srcindx,
MDB_pageparent *dst, indx_t dstindx);
-static int mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src,
+static int mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src,
MDB_pageparent *dst);
-static int mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp,
+static int mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp,
unsigned int *newindxp, MDB_val *newkey,
MDB_val *newdata, pgno_t newpgno);
static MDB_dpage *mdb_new_page(MDB_txn *txn, MDB_dbi dbi, uint32_t flags, int num);
-static void cursor_pop_page(MDB_cursor *cursor);
+static void cursor_pop_page(MDB_cursor *cursor);
static MDB_ppage *cursor_push_page(MDB_cursor *cursor,
MDB_page *mp);
-static int mdb_sibling(MDB_cursor *cursor, int move_right);
-static int mdb_cursor_next(MDB_cursor *cursor,
+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_cursor_op op);
-static int mdb_cursor_prev(MDB_cursor *cursor,
+static int mdb_cursor_prev(MDB_cursor *cursor,
MDB_val *key, MDB_val *data, MDB_cursor_op op);
-static int mdb_cursor_set(MDB_cursor *cursor,
+static int mdb_cursor_set(MDB_cursor *cursor,
MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp);
-static int mdb_cursor_first(MDB_cursor *cursor,
+static int mdb_cursor_first(MDB_cursor *cursor,
MDB_val *key, MDB_val *data);
-static int mdb_cursor_last(MDB_cursor *cursor,
+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_page *mp, MDB_node *node);
-static void mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
+static void mdb_xcursor_init0(MDB_cursor *mc);
+static void mdb_xcursor_init1(MDB_cursor *mc, MDB_page *mp, MDB_node *node);
+static void mdb_xcursor_init2(MDB_cursor *mc);
+static void mdb_xcursor_fini(MDB_cursor *mc);
-static size_t mdb_leaf_size(MDB_env *env, MDB_val *key,
+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 size_t mdb_branch_size(MDB_env *env, MDB_val *key);
+
+static void mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi);
-static int memncmp(const void *s1, size_t n1,
- const void *s2, size_t n2);
-static int memnrcmp(const void *s1, size_t n1,
- const void *s2, size_t n2);
+static MDB_cmp_func memncmp, memnrcmp, intcmp, cintcmp;
#ifdef _WIN32
static SECURITY_DESCRIPTOR mdb_null_sd;
static int mdb_sec_inited;
#endif
-static int
-memncmp(const void *s1, size_t n1, const void *s2, size_t n2)
-{
- int diff, len_diff = -1;
-
- if (n1 >= n2) {
- len_diff = (n1 > n2);
- n1 = n2;
- }
- diff = memcmp(s1, s2, n1);
- return diff ? diff : len_diff;
-}
-
-static int
-memnrcmp(const void *s1, size_t n1, const void *s2, size_t n2)
-{
- const unsigned char *p1, *p2, *p1_lim;
-
- if (n2 == 0)
- return n1 != 0;
- if (n1 == 0)
- return -1;
-
- p1 = (const unsigned char *)s1 + n1 - 1;
- p2 = (const unsigned char *)s2 + n2 - 1;
-
- for (p1_lim = (n1 <= n2 ? s1 : s2); *p1 == *p2; p1--, p2--) {
- if (p1 == p1_lim)
- return (p1 != s1) ? (p1 != p2) : (p2 != s2) ? -1 : 0;
- }
- return *p1 - *p2;
-}
-
char *
mdb_version(int *maj, int *min, int *pat)
{
unsigned int i;
if (key->mv_size > MAXKEYSIZE)
return "MAXKEYSIZE";
+#if 1
for (i=0; i<key->mv_size; i++)
ptr += sprintf(ptr, "%02x", *c++);
+#else
+ sprintf(buf, "%.*s", key->mv_size, key->mv_data);
+#endif
return buf;
}
#endif
int
mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
{
- if (txn->mt_dbxs[dbi].md_cmp)
- return txn->mt_dbxs[dbi].md_cmp(a, b);
-
- if (txn->mt_dbs[dbi].md_flags & (MDB_REVERSEKEY
-#if __BYTE_ORDER == __LITTLE_ENDIAN
- |MDB_INTEGERKEY
-#endif
- ))
- return memnrcmp(a->mv_data, a->mv_size, b->mv_data, b->mv_size);
- else
- return memncmp((char *)a->mv_data, a->mv_size, b->mv_data, b->mv_size);
+ return txn->mt_dbxs[dbi].md_cmp(a, b);
}
int
{
if (txn->mt_dbxs[dbi].md_dcmp)
return txn->mt_dbxs[dbi].md_dcmp(a, b);
-
- if (txn->mt_dbs[dbi].md_flags & (0
-#if __BYTE_ORDER == __LITTLE_ENDIAN
- |MDB_INTEGERDUP
-#endif
- ))
- return memnrcmp(a->mv_data, a->mv_size, b->mv_data, b->mv_size);
else
- return memncmp((char *)a->mv_data, a->mv_size, b->mv_data, b->mv_size);
+ return EINVAL; /* too bad you can't distinguish this from a valid result */
}
/* Allocate new page(s) for writing */
{
MDB_dpage *dp;
pgno_t pgno = P_INVALID;
- MIDL2 mid;
+ ID2 mid;
if (txn->mt_txnid > 2) {
if (!txn->mt_env->me_pghead && dbi != FREE_DBI &&
txn->mt_dbs[FREE_DBI].md_root != P_INVALID) {
/* See if there's anything in the free DB */
+ MDB_cursor mc;
MDB_pageparent mpp;
MDB_node *leaf;
ULONG *kptr, oldest;
mpp.mp_parent = NULL;
mpp.mp_pi = 0;
- mdb_search_page(txn, FREE_DBI, NULL, NULL, 0, &mpp);
+ mc.mc_txn = txn;
+ mc.mc_dbi = FREE_DBI;
+ mc.mc_snum = 0;
+ mc.mc_flags = 0;
+ mdb_search_page(txn, FREE_DBI, NULL, &mc, 0, &mpp);
leaf = NODEPTR(mpp.mp_page, 0);
kptr = (ULONG *)NODEKEY(leaf);
/* It's usable, grab it.
*/
MDB_oldpages *mop;
+ MDB_ppage *top;
MDB_val data;
pgno_t *idl;
}
#endif
/* drop this IDL from the DB */
- mpp.mp_parent = NULL;
- mpp.mp_pi = 0;
- mdb_search_page(txn, FREE_DBI, NULL, NULL, 1, &mpp);
- leaf = NODEPTR(mpp.mp_page, 0);
- mdb_del0(txn, FREE_DBI, 0, &mpp, leaf);
+ top = CURSOR_TOP(&mc);
+ top->mp_ki = 0;
+ mc.mc_flags = C_INITIALIZED;
+ mdb_cursor_del(&mc, 0);
}
}
if (txn->mt_env->me_pghead) {
}
mid.mid = dp->p.mp_pgno;
mid.mptr = dp;
- mdb_midl2_insert(txn->mt_u.dirty_list, &mid);
+ mdb_mid2l_insert(txn->mt_u.dirty_list, &mid);
return dp;
}
return ENOMEM;
DPRINTF("touched db %u page %lu -> %lu", dbi, mp->mp_pgno, dp->p.mp_pgno);
assert(mp->mp_pgno != dp->p.mp_pgno);
- mdb_midl_insert(txn->mt_free_pgs, mp->mp_pgno);
+ mdb_midl_append(txn->mt_free_pgs, mp->mp_pgno);
pgno = dp->p.mp_pgno;
memcpy(&dp->p, mp, txn->mt_env->me_psize);
mp = &dp->p;
/* Update the page number to new touched page. */
if (pp->mp_parent != NULL)
- NODEPGNO(NODEPTR(pp->mp_parent, pp->mp_pi)) = mp->mp_pgno;
+ SETPGNO(NODEPTR(pp->mp_parent, pp->mp_pi), mp->mp_pgno);
pp->mp_page = mp;
}
return 0;
MDB_dpage *dp;
MDB_env *env;
pgno_t next;
+ MDB_cursor mc;
assert(txn != NULL);
assert(txn->mt_env != NULL);
DPRINTF("committing txn %lu %p on mdbenv %p, root page %lu",
txn->mt_txnid, txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root);
+ mc.mc_txn = txn;
+ mc.mc_dbi = FREE_DBI;
+ mc.mc_flags = 0;
+
/* should only be one record now */
if (env->me_pghead) {
MDB_pageparent mpp;
/* make sure first page of freeDB is touched and on freelist */
mpp.mp_parent = NULL;
mpp.mp_pi = 0;
- mdb_search_page(txn, FREE_DBI, NULL, NULL, 1, &mpp);
+ mc.mc_snum = 0;
+ mdb_search_page(txn, FREE_DBI, NULL, &mc, 1, &mpp);
}
/* save to free list */
if (!MDB_IDL_IS_ZERO(txn->mt_free_pgs)) {
key.mv_data = NULL;
mpp.mp_parent = NULL;
mpp.mp_pi = 0;
- mdb_search_page(txn, FREE_DBI, &key, NULL, 1, &mpp);
+ mc.mc_snum = 0;
+ mdb_search_page(txn, FREE_DBI, &key, &mc, 1, &mpp);
+ mdb_midl_sort(txn->mt_free_pgs);
#if DEBUG > 1
{
unsigned int i;
do {
i = txn->mt_free_pgs[0];
data.mv_size = MDB_IDL_SIZEOF(txn->mt_free_pgs);
- rc = mdb_put0(txn, FREE_DBI, &key, &data, 0);
+ rc = mdb_cursor_put(&mc, &key, &data, 0);
if (rc) {
mdb_txn_abort(txn);
return rc;
key.mv_data = (char *)&mop->mo_txnid;
data.mv_size = MDB_IDL_SIZEOF(mop->mo_pages);
data.mv_data = mop->mo_pages;
- mdb_put0(txn, FREE_DBI, &key, &data, 0);
+ mdb_cursor_put(&mc, &key, &data, 0);
free(env->me_pghead);
env->me_pghead = NULL;
}
MDB_val data;
data.mv_size = sizeof(MDB_db);
+ mc.mc_dbi = MAIN_DBI;
+ mc.mc_flags = 0;
for (i = 2; i < txn->mt_numdbs; i++) {
if (txn->mt_dbxs[i].md_dirty) {
data.mv_data = &txn->mt_dbs[i];
- mdb_put0(txn, MAIN_DBI, &txn->mt_dbxs[i].md_name, &data, 0);
+ mdb_cursor_put(&mc, &txn->mt_dbxs[i].md_name, &data, 0);
}
}
}
free(env);
}
+/* only for aligned ints */
+static int
+intcmp(const MDB_val *a, const MDB_val *b)
+{
+ if (a->mv_size == sizeof(long))
+ {
+ unsigned long *la, *lb;
+ la = a->mv_data;
+ lb = b->mv_data;
+ return *la - *lb;
+ } else {
+ unsigned int *ia, *ib;
+ ia = a->mv_data;
+ ib = b->mv_data;
+ return *ia - *ib;
+ }
+}
+
+/* ints must always be the same size */
+static int
+cintcmp(const MDB_val *a, const MDB_val *b)
+{
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+ unsigned char *u, *c;
+ int x;
+
+ u = a->mv_data + a->mv_size;
+ c = b->mv_data + a->mv_size;
+ while(u > (unsigned char *)a->mv_data) {
+ x = *--u - *--c;
+ if (x) break;
+ }
+ return x;
+#else
+ return memcmp(a->mv_data, b->mv_data, a->mv_size);
+#endif
+}
+
+static int
+memncmp(const MDB_val *a, const MDB_val *b)
+{
+ int diff, len_diff;
+ unsigned int len;
+
+ len = a->mv_size;
+ len_diff = a->mv_size - b->mv_size;
+ if (len_diff > 0)
+ len = b->mv_size;
+ diff = memcmp(a->mv_data, b->mv_data, len);
+ return diff ? diff : len_diff;
+}
+
+static int
+memnrcmp(const MDB_val *a, const MDB_val *b)
+{
+ const unsigned char *p1, *p2, *p1_lim;
+ int diff, len_diff;
+
+ if (b->mv_size == 0)
+ return a->mv_size != 0;
+ if (a->mv_size == 0)
+ return -1;
+
+ p1 = (const unsigned char *)a->mv_data + a->mv_size - 1;
+ p2 = (const unsigned char *)b->mv_data + b->mv_size - 1;
+
+ len_diff = a->mv_size - b->mv_size;
+ if (len_diff < 0)
+ p1_lim = p1 - a->mv_size;
+ else
+ p1_lim = p1 - b->mv_size;
+
+ while (p1 > p1_lim) {
+ diff = *p1 - *p2;
+ if (diff)
+ return diff;
+ p1--;
+ p2--;
+ }
+ return len_diff;
+}
+
/* Search for key within a leaf page, using binary search.
* Returns the smallest entry larger or equal to the key.
* If exactp is non-null, stores whether the found entry was an exact match
mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp, MDB_val *key,
int *exactp, unsigned int *kip)
{
- unsigned int i = 0;
+ unsigned int i = 0, nkeys;
int low, high;
int rc = 0;
MDB_node *node = NULL;
MDB_val nodekey;
+ MDB_cmp_func *cmp;
DKBUF;
+ nkeys = NUMKEYS(mp);
+
DPRINTF("searching %u keys in %s page %lu",
- NUMKEYS(mp),
- IS_LEAF(mp) ? "leaf" : "branch",
+ nkeys, IS_LEAF(mp) ? "leaf" : "branch",
mp->mp_pgno);
- assert(NUMKEYS(mp) > 0);
-
- memset(&nodekey, 0, sizeof(nodekey));
+ assert(nkeys > 0);
low = IS_LEAF(mp) ? 0 : 1;
- high = NUMKEYS(mp) - 1;
+ high = nkeys - 1;
+ cmp = txn->mt_dbxs[dbi].md_cmp;
+ if (IS_LEAF2(mp)) {
+ nodekey.mv_size = txn->mt_dbs[dbi].md_pad;
+ node = NODEPTR(mp, 0); /* fake */
+ }
while (low <= high) {
i = (low + high) >> 1;
if (IS_LEAF2(mp)) {
- nodekey.mv_size = txn->mt_dbs[dbi].md_pad;
nodekey.mv_data = LEAF2KEY(mp, i, nodekey.mv_size);
} else {
node = NODEPTR(mp, i);
nodekey.mv_data = NODEKEY(node);
}
- rc = mdb_cmp(txn, dbi, key, &nodekey);
+ rc = cmp(key, &nodekey);
+#if DEBUG
if (IS_LEAF(mp))
DPRINTF("found leaf index %u [%s], rc = %i",
i, DKEY(&nodekey), rc);
else
DPRINTF("found branch index %u [%s -> %lu], rc = %i",
i, DKEY(&nodekey), NODEPGNO(node), rc);
+#endif
if (rc == 0)
break;
if (rc > 0) { /* Found entry is less than the key. */
i++; /* Skip to get the smallest entry larger than key. */
- if (i >= NUMKEYS(mp))
- /* There is no entry larger or equal to the key. */
- return NULL;
+ if (!IS_LEAF2(mp))
+ node = NODEPTR(mp, i);
}
if (exactp)
*exactp = (rc == 0);
if (kip) /* Store the key index if requested. */
*kip = i;
+ if (i >= nkeys)
+ /* There is no entry larger or equal to the key. */
+ return NULL;
/* nodeptr is fake for LEAF2 */
- return IS_LEAF2(mp) ? NODEPTR(mp, 0) : NODEPTR(mp, i);
+ return node;
}
static void
if (!F_ISSET(txn->mt_flags, MDB_TXN_RDONLY) && txn->mt_u.dirty_list[0].mid) {
MDB_dpage *dp;
- MIDL2 id;
unsigned x;
- id.mid = pgno;
- x = mdb_midl2_search(txn->mt_u.dirty_list, &id);
+ x = mdb_mid2l_search(txn->mt_u.dirty_list, pgno);
if (x <= txn->mt_u.dirty_list[0].mid && txn->mt_u.dirty_list[x].mid == pgno) {
dp = txn->mt_u.dirty_list[x].mptr;
p = &dp->p;
return ENOMEM;
if (modify) {
- MDB_dhead *dh = ((MDB_dhead *)mp)-1;
+ MDB_dhead *dh;
if ((rc = mdb_touch(txn, dbi, mpp)) != 0)
return rc;
dh = ((MDB_dhead *)mpp->mp_page)-1;
int rc;
if (!F_ISSET(leaf->mn_flags, F_BIGDATA)) {
- data->mv_size = leaf->mn_dsize;
+ data->mv_size = NODEDSZ(leaf);
data->mv_data = NODEDATA(leaf);
return MDB_SUCCESS;
}
/* Read overflow data.
*/
- data->mv_size = leaf->mn_dsize;
+ data->mv_size = NODEDSZ(leaf);
memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
if ((rc = mdb_get_page(txn, pgno, &omp))) {
DPRINTF("read overflow page %lu failed", pgno);
mdb_get(MDB_txn *txn, MDB_dbi dbi,
MDB_val *key, MDB_val *data)
{
- int rc, exact;
- MDB_node *leaf;
- MDB_pageparent mpp;
+ MDB_cursor mc;
+ MDB_xcursor mx;
+ int exact = 0;
DKBUF;
assert(key);
return EINVAL;
}
- if ((rc = mdb_search_page(txn, dbi, key, NULL, 0, &mpp)) != MDB_SUCCESS)
- return rc;
-
- leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, NULL);
- if (leaf && exact) {
- /* Return first duplicate data item */
- if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- MDB_xcursor mx;
-
- mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, mpp.mp_page, leaf);
- rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi, NULL, NULL, 0, &mpp);
- if (rc != MDB_SUCCESS)
- return rc;
- if (IS_LEAF2(mpp.mp_page)) {
- data->mv_size = txn->mt_dbs[dbi].md_pad;
- data->mv_data = LEAF2KEY(mpp.mp_page, 0, data->mv_size);
- } else {
- leaf = NODEPTR(mpp.mp_page, 0);
- data->mv_size = NODEKSZ(leaf);
- data->mv_data = NODEKEY(leaf);
- }
- } else {
- rc = mdb_read_data(txn, leaf, data);
- }
+ mc.mc_txn = txn;
+ mc.mc_dbi = dbi;
+ mc.mc_flags = 0;
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
+ mc.mc_xcursor = &mx;
+ mdb_xcursor_init0(&mc);
} else {
- rc = MDB_NOTFOUND;
+ mc.mc_xcursor = NULL;
}
-
- return rc;
+ return mdb_cursor_set(&mc, key, data, MDB_SET, &exact);
}
static int
MDB_node *leaf;
int rc;
- if (cursor->mc_eof) {
+ if (cursor->mc_flags & C_EOF) {
return MDB_NOTFOUND;
}
- assert(cursor->mc_initialized);
+ assert(cursor->mc_flags & C_INITIALIZED);
top = CURSOR_TOP(cursor);
mp = top->mp_page;
return rc;
}
} else {
- cursor->mc_xcursor->mx_cursor.mc_initialized = 0;
+ cursor->mc_xcursor->mx_cursor.mc_flags = 0;
if (op == MDB_NEXT_DUP)
return MDB_NOTFOUND;
}
if (top->mp_ki + 1 >= NUMKEYS(mp)) {
DPUTS("=====> move to next sibling page");
if (mdb_sibling(cursor, 1) != MDB_SUCCESS) {
- cursor->mc_eof = 1;
+ cursor->mc_flags |= C_EOF;
return MDB_NOTFOUND;
}
top = CURSOR_TOP(cursor);
leaf = NODEPTR(mp, top->mp_ki);
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, mp, leaf);
+ mdb_xcursor_init1(cursor, mp, leaf);
}
if (data) {
if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
MDB_node *leaf;
int rc;
- assert(cursor->mc_initialized);
+ assert(cursor->mc_flags & C_INITIALIZED);
top = CURSOR_TOP(cursor);
mp = top->mp_page;
if (op != MDB_PREV || rc == MDB_SUCCESS)
return rc;
} else {
- cursor->mc_xcursor->mx_cursor.mc_initialized = 0;
+ cursor->mc_xcursor->mx_cursor.mc_flags = 0;
if (op == MDB_PREV_DUP)
return MDB_NOTFOUND;
}
if (top->mp_ki == 0) {
DPUTS("=====> move to prev sibling page");
if (mdb_sibling(cursor, 0) != MDB_SUCCESS) {
- cursor->mc_initialized = 0;
+ cursor->mc_flags &= ~C_INITIALIZED;
return MDB_NOTFOUND;
}
top = CURSOR_TOP(cursor);
} else
top->mp_ki--;
- cursor->mc_eof = 0;
+ cursor->mc_flags &= ~C_EOF;
DPRINTF("==> cursor points to page %lu with %u keys, key index %u",
mp->mp_pgno, NUMKEYS(mp), top->mp_ki);
leaf = NODEPTR(mp, top->mp_ki);
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, mp, leaf);
+ mdb_xcursor_init1(cursor, mp, leaf);
}
if (data) {
if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
assert(key->mv_size > 0);
/* See if we're already on the right page */
- if (cursor->mc_initialized) {
+ if (cursor->mc_flags & C_INITIALIZED) {
MDB_val nodekey;
+
top = CURSOR_TOP(cursor);
- /* Don't try this for LEAF2 pages. Maybe support that later. */
- if ((top->mp_page->mp_flags & (P_LEAF|P_LEAF2)) == P_LEAF) {
+ if (top->mp_page->mp_flags & P_LEAF2) {
+ 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);
MDB_SET_KEY(leaf, &nodekey);
- rc = mdb_cmp(cursor->mc_txn, cursor->mc_dbi, key, &nodekey);
- if (rc >= 0) {
- 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) {
- /* we're already on the right page */
+ }
+ rc = cursor->mc_txn->mt_dbxs[cursor->mc_dbi].md_cmp(key, &nodekey);
+ if (rc == 0) {
+ /* Probably happens rarely, but first node on the page
+ * was the one we wanted.
+ */
+ top->mp_ki = 0;
+set1:
+ mpp.mp_page = top->mp_page;
+ if (exactp)
+ *exactp = 1;
+ rc = 0;
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+ goto set3;
+ }
+ 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 = cursor->mc_txn->mt_dbxs[cursor->mc_dbi].md_cmp(key, &nodekey);
+ if (rc == 0) {
+ /* last node was the one we wanted */
+ top->mp_ki = NUMKEYS(top->mp_page)-1;
+ goto set1;
+ }
+ if (rc < 0) {
+ /* This is definitely the right page, skip search_page */
mpp.mp_page = top->mp_page;
rc = 0;
goto set2;
}
}
+ /* 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 */
+ top->mp_ki = NUMKEYS(top->mp_page);
+ return MDB_NOTFOUND;
+ }
}
}
+
cursor->mc_snum = 0;
rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, key, cursor, 0, &mpp);
leaf = NODEPTR(mpp.mp_page, 0);
}
- cursor->mc_initialized = 1;
- cursor->mc_eof = 0;
+set3:
+ cursor->mc_flags |= C_INITIALIZED;
+ cursor->mc_flags &= ~C_EOF;
if (IS_LEAF2(mpp.mp_page)) {
key->mv_size = cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_pad;
}
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, mpp.mp_page, leaf);
+ mdb_xcursor_init1(cursor, mpp.mp_page, leaf);
}
if (data) {
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
int ex2, *ex2p;
if (op == MDB_GET_BOTH) {
ex2p = &ex2;
+ ex2 = 0;
} else {
ex2p = NULL;
}
MDB_val d2;
if ((rc = mdb_read_data(cursor->mc_txn, leaf, &d2)) != MDB_SUCCESS)
return rc;
- rc = mdb_dcmp(cursor->mc_txn, cursor->mc_dbi, data, &d2);
+ rc = cursor->mc_txn->mt_dbxs[cursor->mc_dbi].md_dcmp(data, &d2);
if (rc) {
if (op == MDB_GET_BOTH || rc > 0)
return MDB_NOTFOUND;
assert(IS_LEAF(mpp.mp_page));
leaf = NODEPTR(mpp.mp_page, 0);
- cursor->mc_initialized = 1;
- cursor->mc_eof = 0;
+ cursor->mc_flags |= C_INITIALIZED;
+ cursor->mc_flags &= ~C_EOF;
if (IS_LEAF2(mpp.mp_page)) {
key->mv_size = cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_pad;
if (data) {
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, mpp.mp_page, leaf);
+ mdb_xcursor_init1(cursor, mpp.mp_page, leaf);
rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc)
return rc;
} else {
if (cursor->mc_xcursor)
- cursor->mc_xcursor->mx_cursor.mc_initialized = 0;
+ cursor->mc_xcursor->mx_cursor.mc_flags = 0;
if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
return rc;
}
assert(IS_LEAF(mpp.mp_page));
leaf = NODEPTR(mpp.mp_page, NUMKEYS(mpp.mp_page)-1);
- cursor->mc_initialized = 1;
- cursor->mc_eof = 0;
+ cursor->mc_flags |= C_INITIALIZED;
+ cursor->mc_flags &= ~C_EOF;
top = CURSOR_TOP(cursor);
top->mp_ki = NUMKEYS(top->mp_page) - 1;
if (data) {
if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, mpp.mp_page, leaf);
+ mdb_xcursor_init1(cursor, mpp.mp_page, leaf);
rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
if (rc)
return rc;
case MDB_GET_MULTIPLE:
if (data == NULL ||
!(cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPFIXED) ||
- !cursor->mc_initialized) {
+ !(cursor->mc_flags & C_INITIALIZED)) {
rc = EINVAL;
break;
}
rc = MDB_SUCCESS;
- if (!cursor->mc_xcursor->mx_cursor.mc_initialized || cursor->mc_xcursor->mx_cursor.mc_eof)
+ if (!(cursor->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) ||
+ (cursor->mc_xcursor->mx_cursor.mc_flags & C_EOF))
break;
goto fetchm;
case MDB_NEXT_MULTIPLE:
rc = EINVAL;
break;
}
- if (!cursor->mc_initialized)
+ if (!(cursor->mc_flags & C_INITIALIZED))
rc = mdb_cursor_first(cursor, key, data);
else
rc = mdb_cursor_next(cursor, key, data, MDB_NEXT_DUP);
if (rc == MDB_SUCCESS) {
- if (cursor->mc_xcursor->mx_cursor.mc_initialized) {
+ if (cursor->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
MDB_ppage *top;
fetchm:
top = CURSOR_TOP(&cursor->mc_xcursor->mx_cursor);
case MDB_NEXT:
case MDB_NEXT_DUP:
case MDB_NEXT_NODUP:
- if (!cursor->mc_initialized)
+ if (!(cursor->mc_flags & C_INITIALIZED))
rc = mdb_cursor_first(cursor, key, data);
else
rc = mdb_cursor_next(cursor, key, data, op);
case MDB_PREV:
case MDB_PREV_DUP:
case MDB_PREV_NODUP:
- if (!cursor->mc_initialized || cursor->mc_eof)
+ if (!(cursor->mc_flags & C_INITIALIZED) || (cursor->mc_flags & C_EOF))
rc = mdb_cursor_last(cursor, key, data);
else
rc = mdb_cursor_prev(cursor, key, data, op);
case MDB_FIRST:
rc = mdb_cursor_first(cursor, key, data);
break;
+ case MDB_FIRST_DUP:
+ if (data == NULL ||
+ !(cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) ||
+ !(cursor->mc_flags & C_INITIALIZED) ||
+ !(cursor->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
+ rc = EINVAL;
+ break;
+ }
+ rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ break;
case MDB_LAST:
rc = mdb_cursor_last(cursor, key, data);
break;
+ case MDB_LAST_DUP:
+ if (data == NULL ||
+ !(cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) ||
+ !(cursor->mc_flags & C_INITIALIZED) ||
+ !(cursor->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
+ rc = EINVAL;
+ break;
+ }
+ rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
+ break;
default:
DPRINTF("unhandled/unimplemented cursor operation %u", op);
rc = EINVAL;
return rc;
}
+static int
+mdb_cursor_touch(MDB_cursor *mc)
+{
+ MDB_pageparent mpp;
+ MDB_dhead *dh;
+ unsigned int i;
+ int rc;
+
+ mpp.mp_parent = NULL;
+ mpp.mp_pi = 0;
+ if (mc->mc_dbi > MAIN_DBI && !mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty) {
+ rc = mdb_search_page(mc->mc_txn, MAIN_DBI, &mc->mc_txn->mt_dbxs[mc->mc_dbi].md_name,
+ NULL, 1, &mpp);
+ if (rc) return rc;
+ mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty = 1;
+ }
+ for(i=0; i<mc->mc_snum; i++) {
+ mpp.mp_page = mc->mc_stack[i].mp_page;
+ if (!F_ISSET(mpp.mp_page->mp_flags, P_DIRTY)) {
+ rc = mdb_touch(mc->mc_txn, mc->mc_dbi, &mpp);
+ if (rc) return rc;
+ mc->mc_stack[i].mp_page = mpp.mp_page;
+ if (!i) {
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_root = mpp.mp_page->mp_pgno;
+ }
+ }
+ dh = ((MDB_dhead *)mpp.mp_page)-1;
+ dh->md_parent = mpp.mp_parent;
+ dh->md_pi = mpp.mp_pi;
+ mpp.mp_parent = mpp.mp_page;
+ mpp.mp_pi = mc->mc_stack[i].mp_ki;
+ }
+ return MDB_SUCCESS;
+}
+
+int
+mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
+ unsigned int flags)
+{
+ MDB_ppage *top;
+ MDB_node *leaf;
+ MDB_val xdata, *rdata, dkey;
+ MDB_db dummy;
+ char dbuf[PAGESIZE];
+ int do_sub = 0;
+ size_t nsize;
+ DKBUF;
+ int rc, rc2;
+
+ if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
+ return EACCES;
+
+ DPRINTF("==> put db %u key [%s], size %zu, data size %zu",
+ mc->mc_dbi, DKEY(key), key->mv_size, data->mv_size);
+
+ dkey.mv_size = 0;
+
+ if (flags == MDB_CURRENT) {
+ if (!(mc->mc_flags & C_INITIALIZED))
+ return EINVAL;
+ rc = MDB_SUCCESS;
+ } else if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_root == P_INVALID) {
+ MDB_dpage *dp;
+ /* new database, write a root leaf page */
+ DPUTS("allocating new root leaf page");
+ if ((dp = mdb_new_page(mc->mc_txn, mc->mc_dbi, P_LEAF, 1)) == NULL) {
+ return ENOMEM;
+ }
+ mc->mc_snum = 0;
+ cursor_push_page(mc, &dp->p);
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_root = dp->p.mp_pgno;
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_depth++;
+ mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty = 1;
+ if ((mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
+ == MDB_DUPFIXED)
+ dp->p.mp_flags |= P_LEAF2;
+ mc->mc_flags |= C_INITIALIZED;
+ rc = MDB_NOTFOUND;
+ goto top;
+ } else {
+ int exact = 0;
+ rc = mdb_cursor_set(mc, key, NULL, MDB_SET, &exact);
+ if (flags == MDB_NOOVERWRITE && rc == 0) {
+ DPRINTF("duplicate key [%s]", DKEY(key));
+ return MDB_KEYEXIST;
+ }
+ if (rc && rc != MDB_NOTFOUND)
+ return rc;
+ }
+
+ /* Cursor is positioned, now make sure all pages are writable */
+ rc2 = mdb_cursor_touch(mc);
+ if (rc2) return rc2;
+
+top:
+ top = CURSOR_TOP(mc);
+
+ /* The key already exists */
+ if (rc == MDB_SUCCESS) {
+ /* there's only a key anyway, so this is a no-op */
+ if (IS_LEAF2(top->mp_page)) {
+ unsigned 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;
+ }
+
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+
+ /* DB has dups? */
+ if (F_ISSET(mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags, MDB_DUPSORT)) {
+ /* Was a single item before, must convert now */
+ if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
+ dkey.mv_size = NODEDSZ(leaf);
+ dkey.mv_data = dbuf;
+ memcpy(dbuf, NODEDATA(leaf), dkey.mv_size);
+ /* data matches, ignore it */
+ if (!mdb_dcmp(mc->mc_txn, mc->mc_dbi, data, &dkey))
+ return (flags == MDB_NODUPDATA) ? MDB_KEYEXIST : MDB_SUCCESS;
+ memset(&dummy, 0, sizeof(dummy));
+ if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_DUPFIXED) {
+ dummy.md_pad = data->mv_size;
+ dummy.md_flags = MDB_DUPFIXED;
+ if (mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_INTEGERDUP)
+ dummy.md_flags |= MDB_INTEGERKEY;
+ }
+ dummy.md_root = P_INVALID;
+ if (dkey.mv_size == sizeof(MDB_db)) {
+ memcpy(NODEDATA(leaf), &dummy, sizeof(dummy));
+ goto put_sub;
+ }
+ mdb_del_node(top->mp_page, top->mp_ki, 0);
+ do_sub = 1;
+ rdata = &xdata;
+ xdata.mv_size = sizeof(MDB_db);
+ xdata.mv_data = &dummy;
+ goto new_sub;
+ }
+ goto put_sub;
+ }
+ /* same size, just replace it */
+ if (!F_ISSET(leaf->mn_flags, F_BIGDATA) &&
+ NODEDSZ(leaf) == data->mv_size) {
+ memcpy(NODEDATA(leaf), data->mv_data, data->mv_size);
+ goto done;
+ }
+ mdb_del_node(top->mp_page, top->mp_ki, 0);
+ } else {
+ DPRINTF("inserting key at index %i", top->mp_ki);
+ }
+
+ rdata = data;
+
+new_sub:
+ nsize = IS_LEAF2(top->mp_page) ? key->mv_size : mdb_leaf_size(mc->mc_txn->mt_env, key, rdata);
+ if (SIZELEFT(top->mp_page) < nsize) {
+ rc = mdb_split(mc->mc_txn, mc->mc_dbi, &top->mp_page, &top->mp_ki, key, rdata, P_INVALID);
+ } else {
+ /* There is room already in this leaf page. */
+ rc = mdb_add_node(mc->mc_txn, mc->mc_dbi, top->mp_page, top->mp_ki, key, rdata, 0, 0);
+ }
+
+ if (rc != MDB_SUCCESS)
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
+ else {
+ /* Remember if we just added a subdatabase */
+ if (flags & F_SUBDATA) {
+ leaf = NODEPTR(top->mp_page, top->mp_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
+ * DB are all zero size.
+ */
+ if (do_sub) {
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+put_sub:
+ if (flags == MDB_CURRENT)
+ mdb_xcursor_init2(mc);
+ else
+ mdb_xcursor_init1(mc, top->mp_page, leaf);
+ xdata.mv_size = 0;
+ xdata.mv_data = "";
+ if (flags == MDB_NODUPDATA)
+ flags = MDB_NOOVERWRITE;
+ /* converted, write the original data first */
+ if (dkey.mv_size) {
+ rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, flags);
+ if (rc) return rc;
+ leaf->mn_flags |= F_DUPDATA;
+ }
+ rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, flags);
+ 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++;
+ }
+done:
+ return rc;
+}
+
+int
+mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
+{
+ MDB_pageparent mpp;
+ MDB_ppage *top;
+ MDB_node *leaf;
+ int rc;
+
+ if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
+ return EACCES;
+
+ if (!mc->mc_flags & C_INITIALIZED)
+ return EINVAL;
+
+ rc = mdb_cursor_touch(mc);
+ if (rc) return rc;
+
+ top = CURSOR_TOP(mc);
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+ mpp.mp_page = top->mp_page;
+ if (mc->mc_snum > 1) {
+ MDB_ppage *parent = CURSOR_PARENT(mc);
+ mpp.mp_parent = parent->mp_page;
+ mpp.mp_pi = parent->mp_ki;
+ } else {
+ mpp.mp_parent = NULL;
+ mpp.mp_pi = 0;
+ }
+
+ if (!IS_LEAF2(top->mp_page) && F_ISSET(leaf->mn_flags, F_DUPDATA)) {
+ MDB_pageparent mp2;
+
+ if (flags != MDB_NODUPDATA) {
+ mdb_xcursor_init2(mc);
+ rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, 0);
+ mdb_xcursor_fini(mc);
+ /* 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
+ != 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--;
+ return rc;
+ }
+ /* otherwise fall thru and delete the sub-DB */
+ }
+
+ /* 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,
+ NULL, &mc->mc_xcursor->mx_cursor, 0, &mp2);
+ if (rc == MDB_SUCCESS) {
+ MDB_ppage *top, *parent;
+ MDB_node *ni;
+ unsigned int i;
+
+ 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;
+
+ cursor_pop_page(&mc->mc_xcursor->mx_cursor);
+ if (mc->mc_xcursor->mx_cursor.mc_snum) {
+ 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);
+ for (i=0; i<NUMKEYS(top->mp_page); i++) {
+ MDB_page *mp;
+ pgno_t pg;
+ ni = NODEPTR(top->mp_page, i);
+ pg = NODEPGNO(ni);
+ if ((rc = mdb_get_page(mc->mc_txn, pg, &mp)))
+ return rc;
+ /* free it */
+ mdb_midl_append(mc->mc_txn->mt_free_pgs, pg);
+ }
+ rc = mdb_sibling(&mc->mc_xcursor->mx_cursor, 1);
+ if (rc) break;
+ }
+ }
+ /* free it */
+ mdb_midl_append(mc->mc_txn->mt_free_pgs,
+ mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root);
+ }
+ }
+
+ return mdb_del0(mc, top->mp_ki, &mpp, leaf);
+}
+
/* Allocate a page and initialize it
*/
static MDB_dpage *
/* put on overflow page */
sz -= data->mv_size - sizeof(pgno_t);
}
+ sz += sz & 1;
return sz + sizeof(indx_t);
}
node_size += data->mv_size;
}
}
+ node_size += node_size & 1;
if (node_size + sizeof(indx_t) > SIZELEFT(mp)) {
DPRINTF("not enough room in page %lu, got %u ptrs",
node->mn_ksize = (key == NULL) ? 0 : key->mv_size;
node->mn_flags = flags;
if (IS_LEAF(mp))
- node->mn_dsize = data->mv_size;
+ SETDSZ(node,data->mv_size);
else
- NODEPGNO(node) = pgno;
+ SETPGNO(node,pgno);
if (key)
memcpy(NODEKEY(node), key->mv_data, key->mv_size);
else
sz += NODEDSZ(node);
}
+ sz += sz & 1;
ptr = mp->mp_ptrs[indx];
numkeys = NUMKEYS(mp);
}
static void
-mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
+mdb_xcursor_init0(MDB_cursor *mc)
{
+ MDB_xcursor *mx = mc->mc_xcursor;
MDB_dbi dbn;
- mx->mx_txn = *txn;
+ mx->mx_txn = *mc->mc_txn;
mx->mx_txn.mt_dbxs = mx->mx_dbxs;
mx->mx_txn.mt_dbs = mx->mx_dbs;
- mx->mx_dbxs[0] = txn->mt_dbxs[0];
- mx->mx_dbxs[1] = txn->mt_dbxs[1];
- if (dbi > 1) {
- mx->mx_dbxs[2] = txn->mt_dbxs[dbi];
+ mx->mx_dbxs[0] = mc->mc_txn->mt_dbxs[0];
+ mx->mx_dbxs[1] = mc->mc_txn->mt_dbxs[1];
+ if (mc->mc_dbi > 1) {
+ mx->mx_dbxs[2] = mc->mc_txn->mt_dbxs[mc->mc_dbi];
dbn = 2;
} else {
dbn = 1;
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;
+ mx->mx_txn.mt_u = mc->mc_txn->mt_u;
- mx->mx_cursor.mc_snum = 0;
+ mx->mx_cursor.mc_xcursor = NULL;
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_page *mp, MDB_node *node)
+mdb_xcursor_init1(MDB_cursor *mc, MDB_page *mp, MDB_node *node)
{
MDB_db *db = NODEDATA(node);
+ MDB_xcursor *mx = mc->mc_xcursor;
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[0] = mc->mc_txn->mt_dbs[0];
+ mx->mx_dbs[1] = mc->mc_txn->mt_dbs[1];
+ if (mc->mc_dbi > 1) {
+ mx->mx_dbs[2] = mc->mc_txn->mt_dbs[mc->mc_dbi];
+ mx->mx_dbxs[2].md_dirty = mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty;
dbn = 3;
} else {
dbn = 2;
}
- DPRINTF("Sub-db %u for db %u root page %lu", dbn, dbi, db->md_root);
+ DPRINTF("Sub-db %u for db %u root page %lu", dbn, mc->mc_dbi, db->md_root);
mx->mx_dbs[dbn] = *db;
if (F_ISSET(mp->mp_flags, P_DIRTY))
mx->mx_dbxs[dbn].md_dirty = 1;
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_u = txn->mt_u;
- mx->mx_cursor.mc_initialized = 0;
- mx->mx_cursor.mc_eof = 0;
+ mx->mx_txn.mt_next_pgno = mc->mc_txn->mt_next_pgno;
+ mx->mx_cursor.mc_snum = 0;
+ mx->mx_cursor.mc_flags = 0;
+}
+
+static void
+mdb_xcursor_init2(MDB_cursor *mc)
+{
+ MDB_xcursor *mx = mc->mc_xcursor;
+ MDB_dbi dbn;
+ mx->mx_dbs[0] = mc->mc_txn->mt_dbs[0];
+ mx->mx_dbs[1] = mc->mc_txn->mt_dbs[1];
+ if (mc->mc_dbi > 1) {
+ mx->mx_dbs[2] = mc->mc_txn->mt_dbs[mc->mc_dbi];
+ mx->mx_dbxs[2].md_dirty = mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty;
+ 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;
}
static void
-mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
+mdb_xcursor_fini(MDB_cursor *mc)
{
- txn->mt_next_pgno = mx->mx_txn.mt_next_pgno;
- 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[dbi].md_dirty = mx->mx_dbxs[2].md_dirty;
+ MDB_xcursor *mx = mc->mc_xcursor;
+ mc->mc_txn->mt_next_pgno = mx->mx_txn.mt_next_pgno;
+ mc->mc_txn->mt_dbs[0] = mx->mx_dbs[0];
+ mc->mc_txn->mt_dbs[1] = mx->mx_dbs[1];
+ if (mc->mc_dbi > 1) {
+ mc->mc_txn->mt_dbs[mc->mc_dbi] = mx->mx_dbs[2];
+ mc->mc_txn->mt_dbxs[mc->mc_dbi].md_dirty = mx->mx_dbxs[2].md_dirty;
}
}
if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
MDB_xcursor *mx = (MDB_xcursor *)(cursor + 1);
cursor->mc_xcursor = mx;
- mdb_xcursor_init0(txn, dbi, mx);
+ mdb_xcursor_init0(cursor);
}
} else {
return ENOMEM;
if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
*countp = 1;
} else {
- if (!mc->mc_xcursor->mx_cursor.mc_initialized)
+ if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
return EINVAL;
*countp = mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries;
return MDB_SUCCESS;
}
+#if 0
+static void
+mdb_cursor_reset(MDB_cursor *mc)
+{
+ if (mc != NULL) {
+ if (mc->mc_flags & C_XDIRTY) {
+
+ mdb_xcursor_fini(mc);
+
+ /* If sub-DB still has entries, update root record */
+ if (mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_root
+ != P_INVALID) {
+ MDB_ppage *top;
+ MDB_node *leaf;
+ top = CURSOR_TOP(mc);
+ leaf = NODEPTR(top->mp_page, top->mp_ki);
+ memcpy(NODEDATA(leaf),
+ &mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi],
+ sizeof(MDB_db));
+ }
+ mc->mc_flags ^= C_XDIRTY;
+ }
+ }
+}
+#endif
+
void
mdb_cursor_close(MDB_cursor *cursor)
{
data.mv_size = 0;
data.mv_data = NULL;
} else {
- srcnode = NODEPTR(src->mp_page, srcindx);
+ if (srcindx == 0 && IS_BRANCH(src->mp_page)) {
+ /* must find the lowest key below src */
+ MDB_pageparent mpp;
+ mpp.mp_page = src->mp_page;
+ mpp.mp_pi = 0;
+ mdb_search_page_root(txn, dbi, NULL, NULL, 0, &mpp);
+ srcnode = NODEPTR(mpp.mp_page, 0);
+ } else {
+ srcnode = NODEPTR(src->mp_page, srcindx);
+ }
key.mv_size = NODEKSZ(srcnode);
key.mv_data = NODEKEY(srcnode);
data.mv_size = NODEDSZ(srcnode);
*/
mdb_del_node(src->mp_page, srcindx, key.mv_size);
- /* The key value just changed due to del_node, find it again.
- */
- if (!IS_LEAF2(src->mp_page)) {
- srcnode = NODEPTR(src->mp_page, srcindx);
- key.mv_data = NODEKEY(srcnode);
- }
-
/* Update the parent separators.
*/
if (srcindx == 0) {
if (src->mp_pi != 0) {
+ if (IS_LEAF2(src->mp_page)) {
+ key.mv_data = LEAF2KEY(src->mp_page, srcindx, key.mv_size);
+ } else {
+ srcnode = NODEPTR(src->mp_page, srcindx);
+ key.mv_size = NODEKSZ(srcnode);
+ key.mv_data = NODEKEY(srcnode);
+ }
DPRINTF("update separator for source page %lu to [%s]",
src->mp_page->mp_pgno, DKEY(&key));
if ((rc = mdb_update_key(src->mp_parent, src->mp_pi,
if (dstindx == 0) {
if (dst->mp_pi != 0) {
+ if (IS_LEAF2(src->mp_page)) {
+ key.mv_data = LEAF2KEY(dst->mp_page, 0, key.mv_size);
+ } else {
+ srcnode = NODEPTR(dst->mp_page, 0);
+ key.mv_size = NODEKSZ(srcnode);
+ key.mv_data = NODEKEY(srcnode);
+ }
DPRINTF("update separator for destination page %lu to [%s]",
dst->mp_page->mp_pgno, DKEY(&key));
if ((rc = mdb_update_key(dst->mp_parent, dst->mp_pi,
txn->mt_dbs[dbi].md_root = P_INVALID;
txn->mt_dbs[dbi].md_depth = 0;
txn->mt_dbs[dbi].md_leaf_pages = 0;
+ mdb_midl_append(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
} else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) {
DPUTS("collapsing root page!");
+ mdb_midl_append(txn->mt_free_pgs, mpp->mp_page->mp_pgno);
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)))
return rc;
}
static int
-mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki, MDB_pageparent *mpp, MDB_node *leaf)
+mdb_del0(MDB_cursor *mc, unsigned int ki, MDB_pageparent *mpp, MDB_node *leaf)
{
int rc;
pgno_t pg;
memcpy(&pg, NODEDATA(leaf), sizeof(pg));
- ovpages = OVPAGES(NODEDSZ(leaf), txn->mt_env->me_psize);
+ ovpages = OVPAGES(NODEDSZ(leaf), mc->mc_txn->mt_env->me_psize);
for (i=0; i<ovpages; i++) {
DPRINTF("freed ov page %lu", pg);
- mdb_midl_insert(txn->mt_free_pgs, pg);
+ mdb_midl_append(mc->mc_txn->mt_free_pgs, pg);
pg++;
}
}
- mdb_del_node(mpp->mp_page, ki, txn->mt_dbs[dbi].md_pad);
- txn->mt_dbs[dbi].md_entries--;
- rc = mdb_rebalance(txn, dbi, mpp);
+ mdb_del_node(mpp->mp_page, ki, mc->mc_txn->mt_dbs[mc->mc_dbi].md_pad);
+ mc->mc_txn->mt_dbs[mc->mc_dbi].md_entries--;
+ rc = mdb_rebalance(mc->mc_txn, mc->mc_dbi, mpp);
if (rc != MDB_SUCCESS)
- txn->mt_flags |= MDB_TXN_ERROR;
+ mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
return rc;
}
mdb_del(MDB_txn *txn, MDB_dbi dbi,
MDB_val *key, MDB_val *data)
{
+ MDB_cursor mc;
+ MDB_xcursor mx;
+ MDB_cursor_op op;
+ MDB_val rdata, *xdata;
int rc, exact;
- unsigned int ki;
- MDB_node *leaf;
- MDB_pageparent mpp;
DKBUF;
assert(key != NULL);
return EINVAL;
}
- mpp.mp_parent = NULL;
- mpp.mp_pi = 0;
- if ((rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp)) != MDB_SUCCESS)
- return rc;
-
- leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
- if (leaf == NULL || !exact) {
- return MDB_NOTFOUND;
+ mc.mc_txn = txn;
+ mc.mc_dbi = dbi;
+ mc.mc_flags = 0;
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
+ mc.mc_xcursor = &mx;
+ mdb_xcursor_init0(&mc);
+ } else {
+ mc.mc_xcursor = NULL;
}
- if (!IS_LEAF2(mpp.mp_page) && F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- MDB_xcursor mx;
- MDB_pageparent mp2;
-
- mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, mpp.mp_page, leaf);
- if (data) {
- rc = mdb_del(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, NULL);
- mdb_xcursor_fini(txn, dbi, &mx);
- /* 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));
- txn->mt_dbs[dbi].md_entries--;
- 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(&mx.mx_cursor);
- if (mx.mx_cursor.mc_snum) {
- top = CURSOR_TOP(&mx.mx_cursor);
- while (mx.mx_cursor.mc_snum > 1) {
- parent = CURSOR_PARENT(&mx.mx_cursor);
- for (i=0; i<NUMKEYS(top->mp_page); i++) {
- ni = NODEPTR(top->mp_page, i);
- mdb_midl_insert(txn->mt_free_pgs, NODEPGNO(ni));
- }
- parent->mp_ki++;
- if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
- cursor_pop_page(&mx.mx_cursor);
- top = parent;
- } else {
- ni = NODEPTR(parent->mp_page, parent->mp_ki);
- rc = mdb_get_page(&mx.mx_txn, NODEPGNO(ni), &top->mp_page);
- }
- }
- }
- mdb_midl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root);
- }
- }
+ exact = 0;
+ if (data) {
+ op = MDB_GET_BOTH;
+ rdata = *data;
+ xdata = &rdata;
+ } else {
+ op = MDB_SET;
+ xdata = NULL;
}
-
- return mdb_del0(txn, dbi, ki, &mpp, leaf);
+ rc = mdb_cursor_set(&mc, key, xdata, op, &exact);
+ if (rc == 0)
+ rc = mdb_cursor_del(&mc, data ? 0 : MDB_NODUPDATA);
+ return rc;
}
/* Split page <*mpp>, and insert <key,(data|newpgno)> in either left or
/* Maximum free space in an empty page */
pmax = txn->mt_env->me_psize - PAGEHDRSZ;
nsize = mdb_leaf_size(txn->mt_env, newkey, newdata);
- if (newindx <= split_indx) {
-split1:
+ if (newindx < split_indx) {
psize = nsize;
for (i=0; i<split_indx; i++) {
node = NODEPTR(&mdp->p, i);
- psize += NODESIZE + NODEKSZ(node);
+ psize += NODESIZE + NODEKSZ(node) + sizeof(indx_t);
if (F_ISSET(node->mn_flags, F_BIGDATA))
psize += sizeof(pgno_t);
else
psize += NODEDSZ(node);
+ psize += psize & 1;
if (psize > pmax) {
- split_indx--;
- goto split1;
+ split_indx = i;
+ break;
}
}
} else {
-split2:
psize = nsize;
- for (i=split_indx; i<nkeys; i++) {
+ for (i=nkeys-1; i>=split_indx; i--) {
node = NODEPTR(&mdp->p, i);
- psize += NODESIZE + NODEKSZ(node);
+ psize += NODESIZE + NODEKSZ(node) + sizeof(indx_t);
if (F_ISSET(node->mn_flags, F_BIGDATA))
psize += sizeof(pgno_t);
else
psize += NODEDSZ(node);
+ psize += psize & 1;
if (psize > pmax) {
- split_indx++;
- goto split2;
+ split_indx = i+1;
+ break;
}
}
}
/* First find the separating key between the split pages.
*/
- memset(&sepkey, 0, sizeof(sepkey));
if (newindx == split_indx) {
sepkey.mv_size = newkey->mv_size;
sepkey.mv_data = newkey->mv_data;
rkey.mv_size = node->mn_ksize;
if (IS_LEAF(&mdp->p)) {
rdata.mv_data = NODEDATA(node);
- rdata.mv_size = node->mn_dsize;
+ rdata.mv_size = NODEDSZ(node);
} else
pgno = NODEPGNO(node);
flags = node->mn_flags;
return rc;
}
-static int
-mdb_put0(MDB_txn *txn, MDB_dbi dbi,
- MDB_val *key, MDB_val *data, unsigned int flags)
-{
- int rc = MDB_SUCCESS, exact;
- unsigned int ki;
- MDB_node *leaf;
- MDB_pageparent mpp;
- MDB_val xdata, *rdata, dkey;
- MDB_db dummy;
- char dbuf[PAGESIZE];
- int do_sub = 0;
- size_t nsize;
- DKBUF;
-
- DPRINTF("==> put db %u key [%s], size %zu, data size %zu",
- dbi, DKEY(key), key->mv_size, data->mv_size);
-
- dkey.mv_size = 0;
- mpp.mp_parent = NULL;
- mpp.mp_pi = 0;
- rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp);
- if (rc == MDB_SUCCESS) {
- leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
- if (leaf && exact) {
- if (flags == MDB_NOOVERWRITE) {
- DPRINTF("duplicate key [%s]", DKEY(key));
- return MDB_KEYEXIST;
- }
- /* there's only a key anyway, so this is a no-op */
- if (IS_LEAF2(mpp.mp_page))
- return MDB_SUCCESS;
-
- if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
- /* Was a single item before, must convert now */
- if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
- dkey.mv_size = NODEDSZ(leaf);
- dkey.mv_data = dbuf;
- memcpy(dbuf, NODEDATA(leaf), dkey.mv_size);
- /* data matches, ignore it */
- if (!mdb_dcmp(txn, dbi, data, &dkey))
- return (flags == MDB_NODUPDATA) ? MDB_KEYEXIST : MDB_SUCCESS;
- memset(&dummy, 0, sizeof(dummy));
- if (txn->mt_dbs[dbi].md_flags & MDB_DUPFIXED) {
- dummy.md_pad = data->mv_size;
- dummy.md_flags = MDB_DUPFIXED;
- if (txn->mt_dbs[dbi].md_flags & MDB_INTEGERDUP)
- dummy.md_flags |= MDB_INTEGERKEY;
- }
- dummy.md_root = P_INVALID;
- if (dkey.mv_size == sizeof(MDB_db)) {
- memcpy(NODEDATA(leaf), &dummy, sizeof(dummy));
- goto put_sub;
- }
- mdb_del_node(mpp.mp_page, ki, 0);
- do_sub = 1;
- rdata = &xdata;
- xdata.mv_size = sizeof(MDB_db);
- xdata.mv_data = &dummy;
- goto new_sub;
- }
- goto put_sub;
- }
- /* same size, just replace it */
- if (!F_ISSET(leaf->mn_flags, F_BIGDATA) &&
- NODEDSZ(leaf) == data->mv_size) {
- memcpy(NODEDATA(leaf), data->mv_data, data->mv_size);
- goto done;
- }
- mdb_del_node(mpp.mp_page, ki, 0);
- }
- if (leaf == NULL) { /* append if not found */
- ki = NUMKEYS(mpp.mp_page);
- DPRINTF("appending key at index %i", ki);
- }
- } else if (rc == MDB_NOTFOUND) {
- MDB_dpage *dp;
- /* new file, just write a root leaf page */
- DPUTS("allocating new root leaf page");
- if ((dp = mdb_new_page(txn, dbi, P_LEAF, 1)) == NULL) {
- return ENOMEM;
- }
- mpp.mp_page = &dp->p;
- txn->mt_dbs[dbi].md_root = mpp.mp_page->mp_pgno;
- txn->mt_dbs[dbi].md_depth++;
- txn->mt_dbxs[dbi].md_dirty = 1;
- if ((txn->mt_dbs[dbi].md_flags & (MDB_DUPSORT|MDB_DUPFIXED)) == MDB_DUPFIXED)
- mpp.mp_page->mp_flags |= P_LEAF2;
- ki = 0;
- }
- else
- goto done;
-
- assert(IS_LEAF(mpp.mp_page));
- DPRINTF("there are %u keys, should insert new key at index %i",
- NUMKEYS(mpp.mp_page), ki);
-
- rdata = data;
-
-new_sub:
- nsize = IS_LEAF2(mpp.mp_page) ? key->mv_size : mdb_leaf_size(txn->mt_env, key, rdata);
- if (SIZELEFT(mpp.mp_page) < nsize) {
- 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, rdata, 0, 0);
- }
-
- if (rc != MDB_SUCCESS)
- txn->mt_flags |= MDB_TXN_ERROR;
- else {
- /* 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
- * DB are all zero size.
- */
- if (do_sub) {
- MDB_xcursor mx;
-
- leaf = NODEPTR(mpp.mp_page, ki);
-put_sub:
- mdb_xcursor_init0(txn, dbi, &mx);
- mdb_xcursor_init1(txn, dbi, &mx, mpp.mp_page, leaf);
- xdata.mv_size = 0;
- xdata.mv_data = "";
- if (flags == MDB_NODUPDATA)
- flags = MDB_NOOVERWRITE;
- /* converted, write the original data first */
- if (dkey.mv_size) {
- rc = mdb_put0(&mx.mx_txn, mx.mx_cursor.mc_dbi, &dkey, &xdata, flags);
- if (rc) return rc;
- leaf->mn_flags |= F_DUPDATA;
- }
- 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));
- }
- txn->mt_dbs[dbi].md_entries++;
- }
-
-done:
- return rc;
-}
-
int
mdb_put(MDB_txn *txn, MDB_dbi dbi,
MDB_val *key, MDB_val *data, unsigned int flags)
{
+ MDB_cursor mc;
+ MDB_xcursor mx;
+
assert(key != NULL);
assert(data != NULL);
if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA)) != flags)
return EINVAL;
- return mdb_put0(txn, dbi, key, data, flags);
+ mc.mc_txn = txn;
+ mc.mc_dbi = dbi;
+ mc.mc_snum = 0;
+ mc.mc_flags = 0;
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
+ mc.mc_xcursor = &mx;
+ mdb_xcursor_init0(&mc);
+ } else {
+ mc.mc_xcursor = NULL;
+ }
+ return mdb_cursor_put(&mc, key, data, flags);
}
int
return mdb_stat0(env, &env->me_metas[toggle]->mm_dbs[MAIN_DBI], arg);
}
+static void
+mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi)
+{
+ if (txn->mt_dbs[dbi].md_flags & MDB_REVERSEKEY)
+ txn->mt_dbxs[dbi].md_cmp = memnrcmp;
+ else if (txn->mt_dbs[dbi].md_flags & MDB_INTEGERKEY)
+ txn->mt_dbxs[dbi].md_cmp = cintcmp;
+ else
+ txn->mt_dbxs[dbi].md_cmp = memncmp;
+
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
+ if (txn->mt_dbs[dbi].md_flags & MDB_INTEGERDUP) {
+ if (txn->mt_dbs[dbi].md_flags & MDB_DUPFIXED)
+ txn->mt_dbxs[dbi].md_dcmp = intcmp;
+ else
+ txn->mt_dbxs[dbi].md_dcmp = cintcmp;
+ } else if (txn->mt_dbs[dbi].md_flags & MDB_REVERSEDUP) {
+ txn->mt_dbxs[dbi].md_dcmp = memnrcmp;
+ } else {
+ txn->mt_dbxs[dbi].md_dcmp = memncmp;
+ }
+ } else {
+ txn->mt_dbxs[dbi].md_dcmp = NULL;
+ }
+}
+
int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
{
MDB_val key, data;
int rc, dirty = 0;
size_t len;
+ if (txn->mt_dbxs[FREE_DBI].md_cmp == NULL) {
+ mdb_default_cmp(txn, FREE_DBI);
+ }
+
/* 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));
+ mdb_default_cmp(txn, MAIN_DBI);
return MDB_SUCCESS;
}
+ if (txn->mt_dbxs[MAIN_DBI].md_cmp == NULL) {
+ mdb_default_cmp(txn, MAIN_DBI);
+ }
+
/* Is the DB already open? */
len = strlen(name);
for (i=2; i<txn->mt_numdbs; i++) {
/* Create if requested */
if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
+ MDB_cursor mc;
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_put0(txn, MAIN_DBI, &key, &data, F_SUBDATA);
+ mc.mc_txn = txn;
+ mc.mc_dbi = MAIN_DBI;
+ mc.mc_flags = 0;
+ rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
dirty = 1;
}
if (rc == MDB_SUCCESS) {
txn->mt_dbxs[txn->mt_numdbs].md_name.mv_data = strdup(name);
txn->mt_dbxs[txn->mt_numdbs].md_name.mv_size = len;
- txn->mt_dbxs[txn->mt_numdbs].md_cmp = NULL;
- 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 = dirty;
*dbi = txn->mt_numdbs;
txn->mt_env->me_dbs[0][txn->mt_numdbs] = txn->mt_dbs[txn->mt_numdbs];
txn->mt_env->me_dbs[1][txn->mt_numdbs] = txn->mt_dbs[txn->mt_numdbs];
+ mdb_default_cmp(txn, txn->mt_numdbs);
txn->mt_numdbs++;
}
txn->mt_dbxs[dbi].md_rel = rel;
return MDB_SUCCESS;
}
+
+/** @} */