1 /* mdb.c - memory-mapped database library */
3 * Copyright 2011 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
14 * This code is derived from btree.c written by Martin Hedenfalk.
16 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
18 * Permission to use, copy, modify, and distribute this software for any
19 * purpose with or without fee is hereby granted, provided that the above
20 * copyright notice and this permission notice appear in all copies.
22 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
23 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
25 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
27 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/param.h>
36 #ifdef HAVE_SYS_FILE_H
57 #define ULONG unsigned long
66 #if DEBUG && defined(__GNUC__)
67 # define DPRINTF(fmt, ...) \
68 fprintf(stderr, "%s:%d: " fmt "\n", __func__, __LINE__, ##__VA_ARGS__)
70 # define DPRINTF(...) ((void) 0)
75 #define MDB_MAGIC 0xBEEFC0DE
77 #define MAXKEYSIZE 511
79 #define P_INVALID (~0UL)
81 #define F_ISSET(w, f) (((w) & (f)) == (f))
83 typedef uint16_t indx_t;
85 #define DEFAULT_READERS 126
86 #define DEFAULT_MAPSIZE 1048576
88 /* Lock descriptor stuff */
93 typedef struct MDB_rxbody {
98 #define CACHELINE 64 /* most CPUs. Itanium uses 128 */
101 typedef struct MDB_reader {
103 /* cache line alignment */
104 char pad[CACHELINE-sizeof(MDB_rxbody)];
109 uint32_t mt_version; \
110 pthread_mutex_t mt_mutex; \
112 uint32_t mt_numreaders
113 typedef struct MDB_txbody {
117 typedef struct MDB_txninfo {
119 char pad[CACHELINE-sizeof(MDB_txbody)];
120 pthread_mutex_t mt_wmutex;
121 char pad2[CACHELINE-sizeof(pthread_mutex_t)];
122 MDB_reader mt_readers[1];
125 /* Common header for all page types. Overflow pages
126 * occupy a number of contiguous pages with no
127 * headers on any page after the first.
129 typedef struct MDB_page { /* represents a page of storage */
130 #define mp_pgno mp_p.p_pgno
132 pgno_t p_pgno; /* page number */
135 #define P_BRANCH 0x01 /* branch page */
136 #define P_LEAF 0x02 /* leaf page */
137 #define P_OVERFLOW 0x04 /* overflow page */
138 #define P_META 0x08 /* meta page */
139 #define P_DIRTY 0x10 /* dirty page */
141 #define mp_lower mp_pb.pb.pb_lower
142 #define mp_upper mp_pb.pb.pb_upper
143 #define mp_pages mp_pb.pb_pages
146 indx_t pb_lower; /* lower bound of free space */
147 indx_t pb_upper; /* upper bound of free space */
149 uint32_t pb_pages; /* number of overflow pages */
151 indx_t mp_ptrs[1]; /* dynamic size */
154 #define PAGEHDRSZ ((unsigned) offsetof(MDB_page, mp_ptrs))
156 #define NUMKEYS(p) (((p)->mp_lower - PAGEHDRSZ) >> 1)
157 #define SIZELEFT(p) (indx_t)((p)->mp_upper - (p)->mp_lower)
158 #define PAGEFILL(env, p) (1000L * ((env)->me_psize - PAGEHDRSZ - SIZELEFT(p)) / \
159 ((env)->me_psize - PAGEHDRSZ))
160 #define IS_LEAF(p) F_ISSET((p)->mp_flags, P_LEAF)
161 #define IS_BRANCH(p) F_ISSET((p)->mp_flags, P_BRANCH)
162 #define IS_OVERFLOW(p) F_ISSET((p)->mp_flags, P_OVERFLOW)
164 #define OVPAGES(size, psize) (PAGEHDRSZ + size + psize - 1) / psize;
166 typedef struct MDB_db {
170 ULONG md_branch_pages;
172 ULONG md_overflow_pages;
180 typedef struct MDB_meta { /* meta (footer) page content */
183 void *mm_address; /* address for fixed mapping */
184 size_t mm_mapsize; /* size of mmap region */
185 MDB_db mm_dbs[2]; /* first is free space, 2nd is main db */
186 #define mm_psize mm_dbs[0].md_pad
187 #define mm_flags mm_dbs[0].md_flags
188 pgno_t mm_last_pg; /* last used page in file */
189 ULONG mm_txnid; /* txnid that committed this page */
192 typedef struct MDB_dhead { /* a dirty page */
193 STAILQ_ENTRY(MDB_dpage) md_next; /* queue of dirty pages */
195 unsigned md_pi; /* parent index */
199 typedef struct MDB_dpage {
204 STAILQ_HEAD(dirty_queue, MDB_dpage); /* FIXME: use a sorted data structure */
206 typedef struct MDB_oldpages {
207 struct MDB_oldpages *mo_next;
209 pgno_t mo_pages[1]; /* dynamic */
212 typedef struct MDB_pageparent {
218 static MDB_dpage *mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num);
219 static int mdb_touch(MDB_txn *txn, MDB_pageparent *mp);
221 typedef struct MDB_ppage { /* ordered list of pages */
222 SLIST_ENTRY(MDB_ppage) mp_entry;
224 unsigned int mp_ki; /* cursor index on page */
226 SLIST_HEAD(page_stack, MDB_ppage);
228 #define CURSOR_EMPTY(c) SLIST_EMPTY(&(c)->mc_stack)
229 #define CURSOR_TOP(c) SLIST_FIRST(&(c)->mc_stack)
230 #define CURSOR_POP(c) SLIST_REMOVE_HEAD(&(c)->mc_stack, mp_entry)
231 #define CURSOR_PUSH(c,p) SLIST_INSERT_HEAD(&(c)->mc_stack, p, mp_entry)
237 struct page_stack mc_stack; /* stack of parent pages */
239 short mc_initialized; /* 1 if initialized */
240 short mc_eof; /* 1 if end is reached */
241 struct MDB_xcursor *mc_xcursor;
244 #define METAHASHLEN offsetof(MDB_meta, mm_hash)
245 #define METADATA(p) ((void *)((char *)p + PAGEHDRSZ))
247 typedef struct MDB_node {
248 #define mn_pgno mn_p.np_pgno
249 #define mn_dsize mn_p.np_dsize
251 pgno_t np_pgno; /* child page number */
252 uint32_t np_dsize; /* leaf data size */
254 unsigned int mn_flags:4;
255 unsigned int mn_ksize:12; /* key size */
256 #define F_BIGDATA 0x01 /* data put on overflow page */
257 #define F_SUBDATA 0x02 /* data is a sub-database */
261 typedef struct MDB_dbx {
263 MDB_cmp_func *md_cmp; /* user compare function */
264 MDB_cmp_func *md_dcmp; /* user dupsort function */
265 MDB_rel_func *md_rel; /* user relocate function */
267 unsigned int md_dirty;
271 pgno_t mt_next_pgno; /* next unallocated page */
275 pgno_t *mt_free_pgs; /* this is an IDL */
277 struct dirty_queue *dirty_queue; /* modified pages */
280 MDB_dbx *mt_dbxs; /* array */
282 unsigned int mt_numdbs;
284 #define MDB_TXN_RDONLY 0x01 /* read-only transaction */
285 #define MDB_TXN_ERROR 0x02 /* an error has occurred */
286 #define MDB_TXN_METOGGLE 0x04 /* used meta page 1 */
287 unsigned int mt_flags;
290 /* Context for sorted-dup records */
291 typedef struct MDB_xcursor {
292 MDB_cursor mx_cursor;
302 unsigned int me_maxreaders;
303 unsigned int me_numdbs;
304 unsigned int me_maxdbs;
307 MDB_txninfo *me_txns;
308 MDB_meta *me_metas[2];
310 MDB_txn *me_txn; /* current write transaction */
312 off_t me_size; /* current file size */
313 unsigned int me_psize;
315 MDB_dbx *me_dbxs; /* array */
317 MDB_oldpages *me_pghead;
318 pthread_key_t me_txkey; /* thread-key for readers */
319 pgno_t me_free_pgs[MDB_IDL_UM_SIZE];
322 #define NODESIZE offsetof(MDB_node, mn_data)
324 #define INDXSIZE(k) (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size))
325 #define LEAFSIZE(k, d) (NODESIZE + (k)->mv_size + (d)->mv_size)
326 #define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i]))
327 #define NODEKEY(node) (void *)((node)->mn_data)
328 #define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize)
329 #define NODEPGNO(node) ((node)->mn_pgno)
330 #define NODEDSZ(node) ((node)->mn_dsize)
332 #define MDB_COMMIT_PAGES 64 /* max number of pages to write in one commit */
333 #define MDB_MAXCACHE_DEF 1024 /* max number of pages to keep in cache */
335 static int mdb_search_page_root(MDB_txn *txn,
336 MDB_dbi dbi, MDB_val *key,
337 MDB_cursor *cursor, int modify,
338 MDB_pageparent *mpp);
339 static int mdb_search_page(MDB_txn *txn,
340 MDB_dbi dbi, MDB_val *key,
341 MDB_cursor *cursor, int modify,
342 MDB_pageparent *mpp);
344 static int mdbenv_read_header(MDB_env *env, MDB_meta *meta);
345 static int mdbenv_read_meta(MDB_env *env, int *which);
346 static int mdbenv_write_meta(MDB_txn *txn);
347 static MDB_page *mdb_get_page(MDB_txn *txn, pgno_t pgno);
349 static MDB_node *mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
350 MDB_val *key, int *exactp, unsigned int *kip);
351 static int mdb_add_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
352 indx_t indx, MDB_val *key, MDB_val *data,
353 pgno_t pgno, uint8_t flags);
354 static void mdb_del_node(MDB_page *mp, indx_t indx);
355 static int mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki,
356 MDB_pageparent *mpp, MDB_node *leaf);
357 static int mdb_put0(MDB_txn *txn, MDB_dbi dbi,
358 MDB_val *key, MDB_val *data, unsigned int flags);
359 static int mdb_read_data(MDB_txn *txn, MDB_node *leaf, MDB_val *data);
361 static int mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mp);
362 static int mdb_update_key(MDB_page *mp, indx_t indx, MDB_val *key);
363 static int mdb_move_node(MDB_txn *txn, MDB_dbi dbi,
364 MDB_pageparent *src, indx_t srcindx,
365 MDB_pageparent *dst, indx_t dstindx);
366 static int mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src,
367 MDB_pageparent *dst);
368 static int mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp,
369 unsigned int *newindxp, MDB_val *newkey,
370 MDB_val *newdata, pgno_t newpgno);
371 static MDB_dpage *mdb_new_page(MDB_txn *txn, MDB_dbi dbi, uint32_t flags, int num);
373 static void cursor_pop_page(MDB_cursor *cursor);
374 static MDB_ppage *cursor_push_page(MDB_cursor *cursor,
377 static int mdb_set_key(MDB_node *node, MDB_val *key);
378 static int mdb_sibling(MDB_cursor *cursor, int move_right);
379 static int mdb_cursor_next(MDB_cursor *cursor,
380 MDB_val *key, MDB_val *data, MDB_cursor_op op);
381 static int mdb_cursor_prev(MDB_cursor *cursor,
382 MDB_val *key, MDB_val *data, MDB_cursor_op op);
383 static int mdb_cursor_set(MDB_cursor *cursor,
384 MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp);
385 static int mdb_cursor_first(MDB_cursor *cursor,
386 MDB_val *key, MDB_val *data);
387 static int mdb_cursor_last(MDB_cursor *cursor,
388 MDB_val *key, MDB_val *data);
390 static void mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
391 static void mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_db *db);
392 static void mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
394 static size_t mdb_leaf_size(MDB_env *env, MDB_val *key,
396 static size_t mdb_branch_size(MDB_env *env, MDB_val *key);
398 static int memncmp(const void *s1, size_t n1,
399 const void *s2, size_t n2);
400 static int memnrcmp(const void *s1, size_t n1,
401 const void *s2, size_t n2);
404 memncmp(const void *s1, size_t n1, const void *s2, size_t n2)
406 int diff, len_diff = -1;
409 len_diff = (n1 > n2);
412 diff = memcmp(s1, s2, n1);
413 return diff ? diff : len_diff;
417 memnrcmp(const void *s1, size_t n1, const void *s2, size_t n2)
419 const unsigned char *p1, *p2, *p1_lim;
426 p1 = (const unsigned char *)s1 + n1 - 1;
427 p2 = (const unsigned char *)s2 + n2 - 1;
429 for (p1_lim = (n1 <= n2 ? s1 : s2); *p1 == *p2; p1--, p2--) {
431 return (p1 != s1) ? (p1 != p2) : (p2 != s2) ? -1 : 0;
437 mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
439 return txn->mt_dbxs[dbi].md_cmp(a, b);
443 _mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key1, const MDB_val *key2)
445 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_REVERSEKEY))
446 return memnrcmp(key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
448 return memncmp((char *)key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
451 /* Allocate new page(s) for writing */
453 mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num)
456 pgno_t pgno = P_INVALID;
459 if (txn->mt_txnid > 2) {
461 oldest = txn->mt_txnid - 2;
462 if (!txn->mt_env->me_pghead && txn->mt_dbs[FREE_DBI].md_root != P_INVALID) {
463 /* See if there's anything in the free DB */
468 mpp.mp_parent = NULL;
470 mdb_search_page(txn, FREE_DBI, NULL, NULL, 0, &mpp);
471 leaf = NODEPTR(mpp.mp_page, 0);
472 kptr = (ULONG *)NODEKEY(leaf);
474 /* It's potentially usable, unless there are still
475 * older readers outstanding. Grab it.
477 if (oldest > *kptr) {
482 mdb_read_data(txn, leaf, &data);
483 idl = (ULONG *)data.mv_data;
484 mop = malloc(sizeof(MDB_oldpages) + MDB_IDL_SIZEOF(idl) - sizeof(pgno_t));
485 mop->mo_next = txn->mt_env->me_pghead;
486 mop->mo_txnid = *kptr;
487 txn->mt_env->me_pghead = mop;
488 memcpy(mop->mo_pages, idl, MDB_IDL_SIZEOF(idl));
493 DPRINTF("IDL read txn %lu root %lu num %lu",
494 mop->mo_txnid, txn->mt_dbs[FREE_DBI].md_root, idl[0]);
495 for (i=0; i<idl[0]; i++) {
496 DPRINTF("IDL %lu", idl[i+1]);
500 /* drop this IDL from the DB */
501 mpp.mp_parent = NULL;
503 mdb_search_page(txn, FREE_DBI, NULL, NULL, 1, &mpp);
504 leaf = NODEPTR(mpp.mp_page, 0);
505 mdb_del0(txn, FREE_DBI, 0, &mpp, leaf);
508 if (txn->mt_env->me_pghead) {
510 for (i=0; i<txn->mt_env->me_txns->mt_numreaders; i++) {
511 ULONG mr = txn->mt_env->me_txns->mt_readers[i].mr_txnid;
514 oldest = txn->mt_env->me_txns->mt_readers[i].mr_txnid;
516 if (oldest > txn->mt_env->me_pghead->mo_txnid) {
517 MDB_oldpages *mop = txn->mt_env->me_pghead;
518 txn->mt_oldest = oldest;
520 /* FIXME: For now, always use fresh pages. We
521 * really ought to search the free list for a
526 /* peel pages off tail, so we only have to truncate the list */
527 pgno = MDB_IDL_LAST(mop->mo_pages);
528 if (MDB_IDL_IS_RANGE(mop->mo_pages)) {
530 if (mop->mo_pages[2] > mop->mo_pages[1])
531 mop->mo_pages[0] = 0;
535 if (MDB_IDL_IS_ZERO(mop->mo_pages)) {
536 txn->mt_env->me_pghead = mop->mo_next;
544 if ((dp = malloc(txn->mt_env->me_psize * num + sizeof(MDB_dhead))) == NULL)
547 dp->h.md_parent = parent;
548 dp->h.md_pi = parent_idx;
549 STAILQ_INSERT_TAIL(txn->mt_u.dirty_queue, dp, h.md_next);
550 if (pgno == P_INVALID) {
551 dp->p.mp_pgno = txn->mt_next_pgno;
552 txn->mt_next_pgno += num;
554 dp->p.mp_pgno = pgno;
560 /* Touch a page: make it dirty and re-insert into tree with updated pgno.
563 mdb_touch(MDB_txn *txn, MDB_pageparent *pp)
565 MDB_page *mp = pp->mp_page;
570 if (!F_ISSET(mp->mp_flags, P_DIRTY)) {
572 if ((dp = mdb_alloc_page(txn, pp->mp_parent, pp->mp_pi, 1)) == NULL)
574 DPRINTF("touched page %lu -> %lu", mp->mp_pgno, dp->p.mp_pgno);
575 mdb_idl_insert(txn->mt_free_pgs, mp->mp_pgno);
576 pgno = dp->p.mp_pgno;
577 memcpy(&dp->p, mp, txn->mt_env->me_psize);
580 mp->mp_flags |= P_DIRTY;
582 /* Update the page number to new touched page. */
583 if (pp->mp_parent != NULL)
584 NODEPGNO(NODEPTR(pp->mp_parent, pp->mp_pi)) = mp->mp_pgno;
591 mdbenv_sync(MDB_env *env)
594 if (!F_ISSET(env->me_flags, MDB_NOSYNC)) {
595 if (fsync(env->me_fd))
602 mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **ret)
607 if ((txn = calloc(1, sizeof(MDB_txn))) == NULL) {
608 DPRINTF("calloc: %s", strerror(errno));
613 txn->mt_flags |= MDB_TXN_RDONLY;
615 txn->mt_u.dirty_queue = calloc(1, sizeof(*txn->mt_u.dirty_queue));
616 if (txn->mt_u.dirty_queue == NULL) {
620 STAILQ_INIT(txn->mt_u.dirty_queue);
622 pthread_mutex_lock(&env->me_txns->mt_wmutex);
623 env->me_txns->mt_txnid++;
624 txn->mt_free_pgs = env->me_free_pgs;
625 txn->mt_free_pgs[0] = 0;
628 txn->mt_txnid = env->me_txns->mt_txnid;
630 MDB_reader *r = pthread_getspecific(env->me_txkey);
633 pthread_mutex_lock(&env->me_txns->mt_mutex);
634 for (i=0; i<env->me_txns->mt_numreaders; i++)
635 if (env->me_txns->mt_readers[i].mr_pid == 0)
637 if (i == env->me_maxreaders) {
638 pthread_mutex_unlock(&env->me_txns->mti_mutex);
641 env->me_txns->mt_readers[i].mr_pid = getpid();
642 env->me_txns->mt_readers[i].mr_tid = pthread_self();
643 r = &env->me_txns->mt_readers[i];
644 pthread_setspecific(env->me_txkey, r);
645 if (i >= env->me_txns->mt_numreaders)
646 env->me_txns->mt_numreaders = i+1;
647 pthread_mutex_unlock(&env->me_txns->mt_mutex);
649 r->mr_txnid = txn->mt_txnid;
650 txn->mt_u.reader = r;
657 if ((rc = mdbenv_read_meta(env, &toggle)) != MDB_SUCCESS) {
662 /* Copy the DB arrays */
663 txn->mt_numdbs = env->me_numdbs;
664 txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */
665 txn->mt_dbs = malloc(env->me_maxdbs * sizeof(MDB_db));
666 memcpy(txn->mt_dbs, env->me_meta->mm_dbs, 2 * sizeof(MDB_db));
667 if (txn->mt_numdbs > 2)
668 memcpy(txn->mt_dbs+2, env->me_dbs[env->me_db_toggle]+2,
669 (txn->mt_numdbs - 2) * sizeof(MDB_db));
673 txn->mt_flags |= MDB_TXN_METOGGLE;
674 txn->mt_next_pgno = env->me_meta->mm_last_pg+1;
677 DPRINTF("begin transaction %lu on mdbenv %p, root page %lu",
678 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
685 mdb_txn_abort(MDB_txn *txn)
694 DPRINTF("abort transaction %lu on mdbenv %p, root page %lu",
695 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
699 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
700 txn->mt_u.reader->mr_txnid = 0;
705 /* Discard all dirty pages. */
706 while (!STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
707 dp = STAILQ_FIRST(txn->mt_u.dirty_queue);
708 STAILQ_REMOVE_HEAD(txn->mt_u.dirty_queue, h.md_next);
711 free(txn->mt_u.dirty_queue);
713 while ((mop = txn->mt_env->me_pghead)) {
714 txn->mt_env->me_pghead = mop->mo_next;
719 env->me_txns->mt_txnid--;
720 for (i=2; i<env->me_numdbs; i++)
721 env->me_dbxs[i].md_dirty = 0;
722 pthread_mutex_unlock(&env->me_txns->mt_wmutex);
729 mdb_txn_commit(MDB_txn *txn)
738 struct iovec iov[MDB_COMMIT_PAGES];
741 assert(txn->mt_env != NULL);
745 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
746 DPRINTF("attempt to commit read-only transaction");
751 if (txn != env->me_txn) {
752 DPRINTF("attempt to commit unknown transaction");
757 if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) {
758 DPRINTF("error flag is set, can't commit");
763 if (STAILQ_EMPTY(txn->mt_u.dirty_queue))
766 DPRINTF("committing transaction %lu on mdbenv %p, root page %lu",
767 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
769 /* should only be one record now */
770 if (env->me_pghead) {
774 mop = env->me_pghead;
775 key.mv_size = sizeof(pgno_t);
776 key.mv_data = (char *)&mop->mo_txnid;
777 data.mv_size = MDB_IDL_SIZEOF(mop->mo_pages);
778 data.mv_data = mop->mo_pages;
779 mdb_put0(txn, FREE_DBI, &key, &data, 0);
780 free(env->me_pghead);
781 env->me_pghead = NULL;
783 /* save to free list */
784 if (!MDB_IDL_IS_ZERO(txn->mt_free_pgs)) {
788 /* make sure last page of freeDB is touched and on freelist */
789 key.mv_size = MAXKEYSIZE+1;
791 mpp.mp_parent = NULL;
793 mdb_search_page(txn, FREE_DBI, &key, NULL, 1, &mpp);
798 ULONG *idl = txn->mt_free_pgs;
799 DPRINTF("IDL write txn %lu root %lu num %lu",
800 txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, idl[0]);
801 for (i=0; i<idl[0]; i++) {
802 DPRINTF("IDL %lu", idl[i+1]);
806 /* write to last page of freeDB */
807 key.mv_size = sizeof(pgno_t);
808 key.mv_data = (char *)&txn->mt_txnid;
809 data.mv_size = MDB_IDL_SIZEOF(txn->mt_free_pgs);
810 data.mv_data = txn->mt_free_pgs;
811 mdb_put0(txn, FREE_DBI, &key, &data, 0);
814 /* Update DB root pointers. Their pages have already been
815 * touched so this is all in-place and cannot fail.
819 data.mv_size = sizeof(MDB_db);
821 for (i = 2; i < txn->mt_numdbs; i++) {
822 if (txn->mt_dbxs[i].md_dirty) {
823 data.mv_data = &txn->mt_dbs[i];
824 mdb_put0(txn, MAIN_DBI, &txn->mt_dbxs[i].md_name, &data, 0);
829 /* Commit up to MDB_COMMIT_PAGES dirty pages to disk until done.
836 STAILQ_FOREACH(dp, txn->mt_u.dirty_queue, h.md_next) {
837 if (dp->p.mp_pgno != next) {
839 DPRINTF("committing %u dirty pages", n);
840 rc = writev(env->me_fd, iov, n);
844 DPRINTF("short write, filesystem full?");
846 DPRINTF("writev: %s", strerror(errno));
853 lseek(env->me_fd, dp->p.mp_pgno * env->me_psize, SEEK_SET);
854 next = dp->p.mp_pgno;
856 DPRINTF("committing page %lu", dp->p.mp_pgno);
857 iov[n].iov_len = env->me_psize * dp->h.md_num;
858 iov[n].iov_base = &dp->p;
859 size += iov[n].iov_len;
860 next = dp->p.mp_pgno + dp->h.md_num;
861 /* clear dirty flag */
862 dp->p.mp_flags &= ~P_DIRTY;
863 if (++n >= MDB_COMMIT_PAGES) {
872 DPRINTF("committing %u dirty pages", n);
873 rc = writev(env->me_fd, iov, n);
877 DPRINTF("short write, filesystem full?");
879 DPRINTF("writev: %s", strerror(errno));
886 /* Drop the dirty pages.
888 while (!STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
889 dp = STAILQ_FIRST(txn->mt_u.dirty_queue);
890 STAILQ_REMOVE_HEAD(txn->mt_u.dirty_queue, h.md_next);
894 if ((n = mdbenv_sync(env)) != 0 ||
895 (n = mdbenv_write_meta(txn)) != MDB_SUCCESS ||
896 (n = mdbenv_sync(env)) != 0) {
902 /* update the DB tables */
904 int toggle = !env->me_db_toggle;
906 for (i = 2; i < env->me_numdbs; i++) {
907 if (txn->mt_dbxs[i].md_dirty) {
908 env->me_dbs[toggle][i] = txn->mt_dbs[i];
909 txn->mt_dbxs[i].md_dirty = 0;
912 for (i = env->me_numdbs; i < txn->mt_numdbs; i++) {
913 txn->mt_dbxs[i].md_dirty = 0;
914 env->me_dbxs[i] = txn->mt_dbxs[i];
915 env->me_dbs[toggle][i] = txn->mt_dbs[i];
917 env->me_db_toggle = toggle;
918 env->me_numdbs = txn->mt_numdbs;
923 pthread_mutex_unlock(&env->me_txns->mt_wmutex);
924 free(txn->mt_u.dirty_queue);
935 mdbenv_read_header(MDB_env *env, MDB_meta *meta)
944 /* We don't know the page size yet, so use a minimum value.
947 if ((rc = pread(env->me_fd, page, PAGESIZE, 0)) == 0) {
949 } else if (rc != PAGESIZE) {
952 DPRINTF("read: %s", strerror(errno));
956 p = (MDB_page *)page;
958 if (!F_ISSET(p->mp_flags, P_META)) {
959 DPRINTF("page %lu not a meta page", p->mp_pgno);
964 if (m->mm_magic != MDB_MAGIC) {
965 DPRINTF("meta has invalid magic");
969 if (m->mm_version != MDB_VERSION) {
970 DPRINTF("database is version %u, expected version %u",
971 m->mm_version, MDB_VERSION);
972 return MDB_VERSION_MISMATCH;
975 memcpy(meta, m, sizeof(*m));
980 mdbenv_init_meta(MDB_env *env, MDB_meta *meta)
987 DPRINTF("writing new meta page");
988 psize = sysconf(_SC_PAGE_SIZE);
990 meta->mm_magic = MDB_MAGIC;
991 meta->mm_version = MDB_VERSION;
992 meta->mm_psize = psize;
993 meta->mm_last_pg = 1;
994 meta->mm_flags = env->me_flags & 0xffff;
995 #if __BYTE_ORDER == __LITTLE_ENDIAN
996 /* freeDB keys are pgno_t's, must compare in int order */
997 meta->mm_flags |= MDB_REVERSEKEY;
999 meta->mm_dbs[0].md_root = P_INVALID;
1000 meta->mm_dbs[1].md_root = P_INVALID;
1002 p = calloc(2, psize);
1004 p->mp_flags = P_META;
1007 memcpy(m, meta, sizeof(*meta));
1009 q = (MDB_page *)((char *)p + psize);
1012 q->mp_flags = P_META;
1015 memcpy(m, meta, sizeof(*meta));
1017 rc = write(env->me_fd, p, psize * 2);
1019 return (rc == (int)psize * 2) ? MDB_SUCCESS : errno;
1023 mdbenv_write_meta(MDB_txn *txn)
1031 assert(txn != NULL);
1032 assert(txn->mt_env != NULL);
1034 DPRINTF("writing meta page for root page %lu", txn->mt_dbs[MAIN_DBI].md_root);
1038 ptr = (char *)&meta;
1039 off = offsetof(MDB_meta, mm_dbs[0].md_depth);
1040 len = sizeof(MDB_meta) - off;
1043 meta.mm_dbs[0] = txn->mt_dbs[0];
1044 meta.mm_dbs[1] = txn->mt_dbs[1];
1045 meta.mm_last_pg = txn->mt_next_pgno - 1;
1046 meta.mm_txnid = txn->mt_txnid;
1048 if (!F_ISSET(txn->mt_flags, MDB_TXN_METOGGLE))
1049 off += env->me_psize;
1052 lseek(env->me_fd, off, SEEK_SET);
1053 rc = write(env->me_fd, ptr, len);
1055 DPRINTF("write failed, disk error?");
1063 mdbenv_read_meta(MDB_env *env, int *which)
1067 assert(env != NULL);
1069 if (env->me_metas[0]->mm_txnid < env->me_metas[1]->mm_txnid)
1072 if (env->me_meta != env->me_metas[toggle])
1073 env->me_meta = env->me_metas[toggle];
1077 DPRINTF("Using meta page %d", toggle);
1083 mdbenv_create(MDB_env **env)
1087 e = calloc(1, sizeof(MDB_env));
1088 if (!e) return ENOMEM;
1090 e->me_maxreaders = DEFAULT_READERS;
1099 mdbenv_set_mapsize(MDB_env *env, size_t size)
1103 env->me_mapsize = size;
1108 mdbenv_set_maxdbs(MDB_env *env, int dbs)
1110 env->me_maxdbs = dbs;
1115 mdbenv_set_maxreaders(MDB_env *env, int readers)
1117 env->me_maxreaders = readers;
1122 mdbenv_get_maxreaders(MDB_env *env, int *readers)
1124 if (!env || !readers)
1126 *readers = env->me_maxreaders;
1131 mdbenv_open2(MDB_env *env, unsigned int flags)
1137 env->me_flags = flags;
1139 memset(&meta, 0, sizeof(meta));
1141 if ((i = mdbenv_read_header(env, &meta)) != 0) {
1144 DPRINTF("new mdbenv");
1148 if (!env->me_mapsize) {
1149 env->me_mapsize = newenv ? DEFAULT_MAPSIZE : meta.mm_mapsize;
1153 if (meta.mm_address && (flags & MDB_FIXEDMAP))
1155 env->me_map = mmap(meta.mm_address, env->me_mapsize, PROT_READ, i,
1157 if (env->me_map == MAP_FAILED)
1161 meta.mm_mapsize = env->me_mapsize;
1162 if (flags & MDB_FIXEDMAP)
1163 meta.mm_address = env->me_map;
1164 i = mdbenv_init_meta(env, &meta);
1165 if (i != MDB_SUCCESS) {
1166 munmap(env->me_map, env->me_mapsize);
1170 env->me_psize = meta.mm_psize;
1172 p = (MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)env->me_map;
1173 env->me_metas[0] = METADATA(p);
1174 env->me_metas[1] = (MDB_meta *)((char *)env->me_metas[0] + meta.mm_psize);
1176 if ((i = mdbenv_read_meta(env, NULL)) != 0)
1179 DPRINTF("opened database version %u, pagesize %u",
1180 env->me_meta->mm_version, env->me_psize);
1181 DPRINTF("depth: %u", env->me_meta->mm_dbs[MAIN_DBI].md_depth);
1182 DPRINTF("entries: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_entries);
1183 DPRINTF("branch pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_branch_pages);
1184 DPRINTF("leaf pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_leaf_pages);
1185 DPRINTF("overflow pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_overflow_pages);
1186 DPRINTF("root: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_root);
1192 mdbenv_reader_dest(void *ptr)
1194 MDB_reader *reader = ptr;
1196 reader->mr_txnid = 0;
1201 /* downgrade the exclusive lock on the region back to shared */
1203 mdbenv_share_locks(MDB_env *env)
1205 struct flock lock_info;
1207 env->me_txns->mt_txnid = env->me_meta->mm_txnid;
1209 memset((void *)&lock_info, 0, sizeof(lock_info));
1210 lock_info.l_type = F_RDLCK;
1211 lock_info.l_whence = SEEK_SET;
1212 lock_info.l_start = 0;
1213 lock_info.l_len = 1;
1214 fcntl(env->me_lfd, F_SETLK, &lock_info);
1218 mdbenv_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
1222 struct flock lock_info;
1226 if ((env->me_lfd = open(lpath, O_RDWR|O_CREAT, mode)) == -1) {
1230 /* Try to get exclusive lock. If we succeed, then
1231 * nobody is using the lock region and we should initialize it.
1233 memset((void *)&lock_info, 0, sizeof(lock_info));
1234 lock_info.l_type = F_WRLCK;
1235 lock_info.l_whence = SEEK_SET;
1236 lock_info.l_start = 0;
1237 lock_info.l_len = 1;
1238 rc = fcntl(env->me_lfd, F_SETLK, &lock_info);
1242 lock_info.l_type = F_RDLCK;
1243 rc = fcntl(env->me_lfd, F_SETLK, &lock_info);
1249 size = lseek(env->me_lfd, 0, SEEK_END);
1250 rsize = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
1251 if (size < rsize && *excl) {
1252 if (ftruncate(env->me_lfd, rsize) != 0) {
1258 size = rsize - sizeof(MDB_txninfo);
1259 env->me_maxreaders = size/sizeof(MDB_reader) + 1;
1261 env->me_txns = mmap(0, rsize, PROT_READ|PROT_WRITE, MAP_SHARED,
1263 if (env->me_txns == MAP_FAILED) {
1268 pthread_mutexattr_t mattr;
1270 pthread_mutexattr_init(&mattr);
1271 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
1272 pthread_mutex_init(&env->me_txns->mt_mutex, &mattr);
1273 pthread_mutex_init(&env->me_txns->mt_wmutex, &mattr);
1274 env->me_txns->mt_version = MDB_VERSION;
1275 env->me_txns->mt_magic = MDB_MAGIC;
1276 env->me_txns->mt_txnid = 0;
1277 env->me_txns->mt_numreaders = 0;
1280 if (env->me_txns->mt_magic != MDB_MAGIC) {
1281 DPRINTF("lock region has invalid magic");
1285 if (env->me_txns->mt_version != MDB_VERSION) {
1286 DPRINTF("lock region is version %u, expected version %u",
1287 env->me_txns->mt_version, MDB_VERSION);
1288 rc = MDB_VERSION_MISMATCH;
1291 if (errno != EACCES && errno != EAGAIN) {
1304 #define LOCKNAME "/lock.mdb"
1305 #define DATANAME "/data.mdb"
1307 mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
1309 int oflags, rc, len, excl;
1310 char *lpath, *dpath;
1313 lpath = malloc(len + sizeof(LOCKNAME) + len + sizeof(DATANAME));
1316 dpath = lpath + len + sizeof(LOCKNAME);
1317 sprintf(lpath, "%s" LOCKNAME, path);
1318 sprintf(dpath, "%s" DATANAME, path);
1320 rc = mdbenv_setup_locks(env, lpath, mode, &excl);
1324 if (F_ISSET(flags, MDB_RDONLY))
1327 oflags = O_RDWR | O_CREAT;
1329 if ((env->me_fd = open(dpath, oflags, mode)) == -1)
1332 if ((rc = mdbenv_open2(env, flags)) != MDB_SUCCESS) {
1336 env->me_path = strdup(path);
1337 DPRINTF("opened dbenv %p", (void *) env);
1338 pthread_key_create(&env->me_txkey, mdbenv_reader_dest);
1340 mdbenv_share_locks(env);
1341 env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
1342 env->me_dbs[0] = calloc(env->me_maxdbs, sizeof(MDB_db));
1343 env->me_dbs[1] = calloc(env->me_maxdbs, sizeof(MDB_db));
1353 mdbenv_close(MDB_env *env)
1358 free(env->me_dbs[1]);
1359 free(env->me_dbs[0]);
1364 munmap(env->me_map, env->me_mapsize);
1368 size_t size = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
1369 munmap(env->me_txns, size);
1375 /* Search for key within a leaf page, using binary search.
1376 * Returns the smallest entry larger or equal to the key.
1377 * If exactp is non-null, stores whether the found entry was an exact match
1378 * in *exactp (1 or 0).
1379 * If kip is non-null, stores the index of the found entry in *kip.
1380 * If no entry larger or equal to the key is found, returns NULL.
1383 mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp, MDB_val *key,
1384 int *exactp, unsigned int *kip)
1392 DPRINTF("searching %u keys in %s page %lu",
1394 IS_LEAF(mp) ? "leaf" : "branch",
1397 assert(NUMKEYS(mp) > 0);
1399 memset(&nodekey, 0, sizeof(nodekey));
1401 low = IS_LEAF(mp) ? 0 : 1;
1402 high = NUMKEYS(mp) - 1;
1403 while (low <= high) {
1404 i = (low + high) >> 1;
1405 node = NODEPTR(mp, i);
1407 nodekey.mv_size = node->mn_ksize;
1408 nodekey.mv_data = NODEKEY(node);
1410 if (txn->mt_dbxs[dbi].md_cmp)
1411 rc = txn->mt_dbxs[dbi].md_cmp(key, &nodekey);
1413 rc = _mdb_cmp(txn, dbi, key, &nodekey);
1416 DPRINTF("found leaf index %u [%.*s], rc = %i",
1417 i, (int)nodekey.mv_size, (char *)nodekey.mv_data, rc);
1419 DPRINTF("found branch index %u [%.*s -> %lu], rc = %i",
1420 i, (int)node->mn_ksize, (char *)NODEKEY(node),
1431 if (rc > 0) { /* Found entry is less than the key. */
1432 i++; /* Skip to get the smallest entry larger than key. */
1433 if (i >= NUMKEYS(mp))
1434 /* There is no entry larger or equal to the key. */
1438 *exactp = (rc == 0);
1439 if (kip) /* Store the key index if requested. */
1442 return NODEPTR(mp, i);
1446 cursor_pop_page(MDB_cursor *cursor)
1450 top = CURSOR_TOP(cursor);
1453 DPRINTF("popped page %lu off cursor %p", top->mp_page->mp_pgno, (void *) cursor);
1459 cursor_push_page(MDB_cursor *cursor, MDB_page *mp)
1463 DPRINTF("pushing page %lu on cursor %p", mp->mp_pgno, (void *) cursor);
1465 if ((ppage = calloc(1, sizeof(MDB_ppage))) == NULL)
1467 ppage->mp_page = mp;
1468 CURSOR_PUSH(cursor, ppage);
1473 mdb_get_page(MDB_txn *txn, pgno_t pgno)
1478 if (!F_ISSET(txn->mt_flags, MDB_TXN_RDONLY) && !STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
1480 STAILQ_FOREACH(dp, txn->mt_u.dirty_queue, h.md_next) {
1481 if (dp->p.mp_pgno == pgno) {
1489 if (pgno > txn->mt_env->me_meta->mm_last_pg)
1491 p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
1497 mdb_search_page_root(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
1498 MDB_cursor *cursor, int modify, MDB_pageparent *mpp)
1500 MDB_page *mp = mpp->mp_page;
1503 if (cursor && cursor_push_page(cursor, mp) == NULL)
1506 while (IS_BRANCH(mp)) {
1510 DPRINTF("branch page %lu has %u keys", mp->mp_pgno, NUMKEYS(mp));
1511 assert(NUMKEYS(mp) > 1);
1512 DPRINTF("found index 0 to page %lu", NODEPGNO(NODEPTR(mp, 0)));
1514 if (key == NULL) /* Initialize cursor to first page. */
1516 else if (key->mv_size > MAXKEYSIZE && key->mv_data == NULL) {
1517 /* cursor to last page */
1521 node = mdb_search_node(txn, dbi, mp, key, &exact, &i);
1523 i = NUMKEYS(mp) - 1;
1531 DPRINTF("following index %u for key %.*s",
1532 i, (int)key->mv_size, (char *)key->mv_data);
1533 assert(i < NUMKEYS(mp));
1534 node = NODEPTR(mp, i);
1537 CURSOR_TOP(cursor)->mp_ki = i;
1539 mpp->mp_parent = mp;
1540 if ((mp = mdb_get_page(txn, NODEPGNO(node))) == NULL)
1545 if (cursor && cursor_push_page(cursor, mp) == NULL)
1549 MDB_dhead *dh = ((MDB_dhead *)mp)-1;
1550 if ((rc = mdb_touch(txn, mpp)) != 0)
1552 dh = ((MDB_dhead *)mpp->mp_page)-1;
1553 dh->md_parent = mpp->mp_parent;
1554 dh->md_pi = mpp->mp_pi;
1561 DPRINTF("internal error, index points to a %02X page!?",
1566 DPRINTF("found leaf page %lu for key %.*s", mp->mp_pgno,
1567 key ? (int)key->mv_size : 0, key ? (char *)key->mv_data : NULL);
1572 /* Search for the page a given key should be in.
1573 * Stores a pointer to the found page in *mpp.
1574 * If key is NULL, search for the lowest page (used by mdb_cursor_first).
1575 * If cursor is non-null, pushes parent pages on the cursor stack.
1576 * If modify is true, visited pages are updated with new page numbers.
1579 mdb_search_page(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
1580 MDB_cursor *cursor, int modify, MDB_pageparent *mpp)
1585 /* Choose which root page to start with. If a transaction is given
1586 * use the root page from the transaction, otherwise read the last
1587 * committed root page.
1589 if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) {
1590 DPRINTF("transaction has failed, must abort");
1593 root = txn->mt_dbs[dbi].md_root;
1595 if (root == P_INVALID) { /* Tree is empty. */
1596 DPRINTF("tree is empty");
1597 return MDB_NOTFOUND;
1600 if ((mpp->mp_page = mdb_get_page(txn, root)) == NULL)
1603 DPRINTF("root page has flags 0x%X", mpp->mp_page->mp_flags);
1606 /* For sub-databases, update main root first */
1607 if (dbi > MAIN_DBI && !txn->mt_dbxs[dbi].md_dirty) {
1609 rc = mdb_search_page(txn, MAIN_DBI, &txn->mt_dbxs[dbi].md_name,
1613 txn->mt_dbxs[dbi].md_dirty = 1;
1615 if (!F_ISSET(mpp->mp_page->mp_flags, P_DIRTY)) {
1616 mpp->mp_parent = NULL;
1618 if ((rc = mdb_touch(txn, mpp)))
1620 txn->mt_dbs[dbi].md_root = mpp->mp_page->mp_pgno;
1624 return mdb_search_page_root(txn, dbi, key, cursor, modify, mpp);
1628 mdb_read_data(MDB_txn *txn, MDB_node *leaf, MDB_val *data)
1630 MDB_page *omp; /* overflow mpage */
1633 if (!F_ISSET(leaf->mn_flags, F_BIGDATA)) {
1634 data->mv_size = leaf->mn_dsize;
1635 data->mv_data = NODEDATA(leaf);
1639 /* Read overflow data.
1641 data->mv_size = leaf->mn_dsize;
1642 memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
1643 if ((omp = mdb_get_page(txn, pgno)) == NULL) {
1644 DPRINTF("read overflow page %lu failed", pgno);
1647 data->mv_data = omp;
1653 mdb_get(MDB_txn *txn, MDB_dbi dbi,
1654 MDB_val *key, MDB_val *data)
1662 DPRINTF("===> get key [%.*s]", (int)key->mv_size, (char *)key->mv_data);
1664 if (txn == NULL || dbi >= txn->mt_numdbs)
1667 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
1671 if ((rc = mdb_search_page(txn, dbi, key, NULL, 0, &mpp)) != MDB_SUCCESS)
1674 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, NULL);
1675 if (leaf && exact) {
1676 /* Return first duplicate data item */
1677 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
1680 mdb_xcursor_init0(txn, dbi, &mx);
1681 mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
1682 rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi, NULL, NULL, 0, &mpp);
1683 if (rc != MDB_SUCCESS)
1685 leaf = NODEPTR(mpp.mp_page, 0);
1687 rc = mdb_read_data(txn, leaf, data);
1696 mdb_sibling(MDB_cursor *cursor, int move_right)
1700 MDB_ppage *parent, *top;
1703 top = CURSOR_TOP(cursor);
1704 if ((parent = SLIST_NEXT(top, mp_entry)) == NULL) {
1705 return MDB_NOTFOUND; /* root has no siblings */
1708 DPRINTF("parent page is page %lu, index %u",
1709 parent->mp_page->mp_pgno, parent->mp_ki);
1711 cursor_pop_page(cursor);
1712 if (move_right ? (parent->mp_ki + 1 >= NUMKEYS(parent->mp_page))
1713 : (parent->mp_ki == 0)) {
1714 DPRINTF("no more keys left, moving to %s sibling",
1715 move_right ? "right" : "left");
1716 if ((rc = mdb_sibling(cursor, move_right)) != MDB_SUCCESS)
1718 parent = CURSOR_TOP(cursor);
1724 DPRINTF("just moving to %s index key %u",
1725 move_right ? "right" : "left", parent->mp_ki);
1727 assert(IS_BRANCH(parent->mp_page));
1729 indx = NODEPTR(parent->mp_page, parent->mp_ki);
1730 if ((mp = mdb_get_page(cursor->mc_txn, indx->mn_pgno)) == NULL)
1733 mp->parent = parent->mp_page;
1734 mp->parent_index = parent->mp_ki;
1737 cursor_push_page(cursor, mp);
1743 mdb_set_key(MDB_node *node, MDB_val *key)
1748 key->mv_size = node->mn_ksize;
1749 key->mv_data = NODEKEY(node);
1755 mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
1762 if (cursor->mc_eof) {
1763 return MDB_NOTFOUND;
1766 assert(cursor->mc_initialized);
1768 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1769 if (op == MDB_NEXT || op == MDB_NEXT_DUP) {
1770 rc = mdb_cursor_next(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_NEXT);
1771 if (op != MDB_NEXT || rc == MDB_SUCCESS)
1776 top = CURSOR_TOP(cursor);
1779 DPRINTF("cursor_next: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
1781 if (top->mp_ki + 1 >= NUMKEYS(mp)) {
1782 DPRINTF("=====> move to next sibling page");
1783 if (mdb_sibling(cursor, 1) != MDB_SUCCESS) {
1785 return MDB_NOTFOUND;
1787 top = CURSOR_TOP(cursor);
1789 DPRINTF("next page is %lu, key index %u", mp->mp_pgno, top->mp_ki);
1793 DPRINTF("==> cursor points to page %lu with %u keys, key index %u",
1794 mp->mp_pgno, NUMKEYS(mp), top->mp_ki);
1796 assert(IS_LEAF(mp));
1797 leaf = NODEPTR(mp, top->mp_ki);
1800 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
1803 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1804 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
1805 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1806 if (rc != MDB_SUCCESS)
1811 return mdb_set_key(leaf, key);
1815 mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
1822 assert(cursor->mc_initialized);
1824 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1825 if (op == MDB_PREV || op == MDB_PREV_DUP) {
1826 rc = mdb_cursor_next(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
1827 if (op != MDB_PREV || rc == MDB_SUCCESS)
1832 top = CURSOR_TOP(cursor);
1835 DPRINTF("cursor_prev: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
1837 if (top->mp_ki == 0) {
1838 DPRINTF("=====> move to prev sibling page");
1839 if (mdb_sibling(cursor, 0) != MDB_SUCCESS) {
1840 return MDB_NOTFOUND;
1842 top = CURSOR_TOP(cursor);
1844 top->mp_ki = NUMKEYS(mp) - 1;
1845 DPRINTF("prev page is %lu, key index %u", mp->mp_pgno, top->mp_ki);
1851 DPRINTF("==> cursor points to page %lu with %u keys, key index %u",
1852 mp->mp_pgno, NUMKEYS(mp), top->mp_ki);
1854 assert(IS_LEAF(mp));
1855 leaf = NODEPTR(mp, top->mp_ki);
1858 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
1861 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1862 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
1863 rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
1864 if (rc != MDB_SUCCESS)
1869 return mdb_set_key(leaf, key);
1873 mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1874 MDB_cursor_op op, int *exactp)
1883 assert(key->mv_size > 0);
1885 while (CURSOR_TOP(cursor) != NULL)
1886 cursor_pop_page(cursor);
1888 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, key, cursor, 0, &mpp);
1889 if (rc != MDB_SUCCESS)
1891 assert(IS_LEAF(mpp.mp_page));
1893 top = CURSOR_TOP(cursor);
1894 leaf = mdb_search_node(cursor->mc_txn, cursor->mc_dbi, mpp.mp_page, key, exactp, &top->mp_ki);
1895 if (exactp != NULL && !*exactp) {
1896 /* MDB_SET specified and not an exact match. */
1897 return MDB_NOTFOUND;
1901 DPRINTF("===> inexact leaf not found, goto sibling");
1902 if ((rc = mdb_sibling(cursor, 1)) != MDB_SUCCESS)
1903 return rc; /* no entries matched */
1904 top = CURSOR_TOP(cursor);
1906 mpp.mp_page = top->mp_page;
1907 assert(IS_LEAF(mpp.mp_page));
1908 leaf = NODEPTR(mpp.mp_page, 0);
1911 cursor->mc_initialized = 1;
1915 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
1918 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1919 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
1920 if (op == MDB_SET || op == MDB_SET_RANGE) {
1921 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1925 if (op == MDB_GET_BOTH) {
1930 op2 = MDB_SET_RANGE;
1932 rc = mdb_cursor_set(&cursor->mc_xcursor->mx_cursor, data, NULL, op2, ex2p);
1933 if (rc != MDB_SUCCESS)
1939 rc = mdb_set_key(leaf, key);
1940 if (rc == MDB_SUCCESS) {
1941 DPRINTF("==> cursor placed on key %.*s",
1942 (int)key->mv_size, (char *)key->mv_data);
1950 mdb_cursor_first(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
1956 while (CURSOR_TOP(cursor) != NULL)
1957 cursor_pop_page(cursor);
1959 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, NULL, cursor, 0, &mpp);
1960 if (rc != MDB_SUCCESS)
1962 assert(IS_LEAF(mpp.mp_page));
1964 leaf = NODEPTR(mpp.mp_page, 0);
1965 cursor->mc_initialized = 1;
1969 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
1972 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1973 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
1974 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1979 return mdb_set_key(leaf, key);
1983 mdb_cursor_last(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
1991 while (CURSOR_TOP(cursor) != NULL)
1992 cursor_pop_page(cursor);
1994 lkey.mv_size = MAXKEYSIZE+1;
1995 lkey.mv_data = NULL;
1997 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, &lkey, cursor, 0, &mpp);
1998 if (rc != MDB_SUCCESS)
2000 assert(IS_LEAF(mpp.mp_page));
2002 leaf = NODEPTR(mpp.mp_page, NUMKEYS(mpp.mp_page)-1);
2003 cursor->mc_initialized = 1;
2006 top = CURSOR_TOP(cursor);
2007 top->mp_ki = NUMKEYS(top->mp_page) - 1;
2010 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
2013 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
2014 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, NODEDATA(leaf));
2015 rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
2021 return mdb_set_key(leaf, key);
2025 mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
2035 case MDB_GET_BOTH_RANGE:
2043 if (key == NULL || key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
2045 } else if (op != MDB_SET_RANGE)
2046 rc = mdb_cursor_set(cursor, key, data, op, NULL);
2048 rc = mdb_cursor_set(cursor, key, data, op, &exact);
2052 case MDB_NEXT_NODUP:
2053 if (!cursor->mc_initialized)
2054 rc = mdb_cursor_first(cursor, key, data);
2056 rc = mdb_cursor_next(cursor, key, data, op);
2060 case MDB_PREV_NODUP:
2061 if (!cursor->mc_initialized || cursor->mc_eof)
2062 rc = mdb_cursor_last(cursor, key, data);
2064 rc = mdb_cursor_prev(cursor, key, data, op);
2067 rc = mdb_cursor_first(cursor, key, data);
2070 rc = mdb_cursor_last(cursor, key, data);
2073 DPRINTF("unhandled/unimplemented cursor operation %u", op);
2081 /* Allocate a page and initialize it
2084 mdb_new_page(MDB_txn *txn, MDB_dbi dbi, uint32_t flags, int num)
2088 if ((dp = mdb_alloc_page(txn, NULL, 0, num)) == NULL)
2090 DPRINTF("allocated new mpage %lu, page size %u",
2091 dp->p.mp_pgno, txn->mt_env->me_psize);
2092 dp->p.mp_flags = flags | P_DIRTY;
2093 dp->p.mp_lower = PAGEHDRSZ;
2094 dp->p.mp_upper = txn->mt_env->me_psize;
2096 if (IS_BRANCH(&dp->p))
2097 txn->mt_dbs[dbi].md_branch_pages++;
2098 else if (IS_LEAF(&dp->p))
2099 txn->mt_dbs[dbi].md_leaf_pages++;
2100 else if (IS_OVERFLOW(&dp->p)) {
2101 txn->mt_dbs[dbi].md_overflow_pages += num;
2102 dp->p.mp_pages = num;
2109 mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data)
2113 sz = LEAFSIZE(key, data);
2114 if (data->mv_size >= env->me_psize / MDB_MINKEYS) {
2115 /* put on overflow page */
2116 sz -= data->mv_size - sizeof(pgno_t);
2119 return sz + sizeof(indx_t);
2123 mdb_branch_size(MDB_env *env, MDB_val *key)
2128 if (sz >= env->me_psize / MDB_MINKEYS) {
2129 /* put on overflow page */
2130 /* not implemented */
2131 /* sz -= key->size - sizeof(pgno_t); */
2134 return sz + sizeof(indx_t);
2138 mdb_add_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp, indx_t indx,
2139 MDB_val *key, MDB_val *data, pgno_t pgno, uint8_t flags)
2142 size_t node_size = NODESIZE;
2145 MDB_dpage *ofp = NULL; /* overflow page */
2147 assert(mp->mp_upper >= mp->mp_lower);
2149 DPRINTF("add node [%.*s] to %s page %lu at index %i, key size %zu",
2150 key ? (int)key->mv_size : 0, key ? (char *)key->mv_data : NULL,
2151 IS_LEAF(mp) ? "leaf" : "branch",
2152 mp->mp_pgno, indx, key ? key->mv_size : 0);
2155 node_size += key->mv_size;
2159 if (F_ISSET(flags, F_BIGDATA)) {
2160 /* Data already on overflow page. */
2161 node_size += sizeof(pgno_t);
2162 } else if (data->mv_size >= txn->mt_env->me_psize / MDB_MINKEYS) {
2163 int ovpages = OVPAGES(data->mv_size, txn->mt_env->me_psize);
2164 /* Put data on overflow page. */
2165 DPRINTF("data size is %zu, put on overflow page",
2167 node_size += sizeof(pgno_t);
2168 if ((ofp = mdb_new_page(txn, dbi, P_OVERFLOW, ovpages)) == NULL)
2170 DPRINTF("allocated overflow page %lu", ofp->p.mp_pgno);
2173 node_size += data->mv_size;
2177 if (node_size + sizeof(indx_t) > SIZELEFT(mp)) {
2178 DPRINTF("not enough room in page %lu, got %u ptrs",
2179 mp->mp_pgno, NUMKEYS(mp));
2180 DPRINTF("upper - lower = %u - %u = %u", mp->mp_upper, mp->mp_lower,
2181 mp->mp_upper - mp->mp_lower);
2182 DPRINTF("node size = %zu", node_size);
2186 /* Move higher pointers up one slot. */
2187 for (i = NUMKEYS(mp); i > indx; i--)
2188 mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];
2190 /* Adjust free space offsets. */
2191 ofs = mp->mp_upper - node_size;
2192 assert(ofs >= mp->mp_lower + sizeof(indx_t));
2193 mp->mp_ptrs[indx] = ofs;
2195 mp->mp_lower += sizeof(indx_t);
2197 /* Write the node data. */
2198 node = NODEPTR(mp, indx);
2199 node->mn_ksize = (key == NULL) ? 0 : key->mv_size;
2200 node->mn_flags = flags;
2202 node->mn_dsize = data->mv_size;
2204 node->mn_pgno = pgno;
2207 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
2212 if (F_ISSET(flags, F_BIGDATA))
2213 memcpy(node->mn_data + key->mv_size, data->mv_data,
2216 memcpy(node->mn_data + key->mv_size, data->mv_data,
2219 memcpy(node->mn_data + key->mv_size, &ofp->p.mp_pgno,
2221 memcpy(METADATA(&ofp->p), data->mv_data, data->mv_size);
2229 mdb_del_node(MDB_page *mp, indx_t indx)
2232 indx_t i, j, numkeys, ptr;
2236 DPRINTF("delete node %u on %s page %lu", indx,
2237 IS_LEAF(mp) ? "leaf" : "branch", mp->mp_pgno);
2238 assert(indx < NUMKEYS(mp));
2240 node = NODEPTR(mp, indx);
2241 sz = NODESIZE + node->mn_ksize;
2243 if (F_ISSET(node->mn_flags, F_BIGDATA))
2244 sz += sizeof(pgno_t);
2246 sz += NODEDSZ(node);
2249 ptr = mp->mp_ptrs[indx];
2250 numkeys = NUMKEYS(mp);
2251 for (i = j = 0; i < numkeys; i++) {
2253 mp->mp_ptrs[j] = mp->mp_ptrs[i];
2254 if (mp->mp_ptrs[i] < ptr)
2255 mp->mp_ptrs[j] += sz;
2260 base = (char *)mp + mp->mp_upper;
2261 memmove(base + sz, base, ptr - mp->mp_upper);
2263 mp->mp_lower -= sizeof(indx_t);
2268 mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
2273 mx->mx_txn.mt_dbxs = mx->mx_dbxs;
2274 mx->mx_txn.mt_dbs = mx->mx_dbs;
2275 mx->mx_dbxs[0] = txn->mt_dbxs[0];
2276 mx->mx_dbxs[1] = txn->mt_dbxs[1];
2278 mx->mx_dbxs[2] = txn->mt_dbxs[dbi];
2283 mx->mx_dbxs[dbn+1].md_parent = dbn;
2284 mx->mx_dbxs[dbn+1].md_cmp = mx->mx_dbxs[dbn].md_dcmp;
2285 mx->mx_dbxs[dbn+1].md_rel = mx->mx_dbxs[dbn].md_rel;
2286 mx->mx_dbxs[dbn+1].md_dirty = 0;
2287 mx->mx_txn.mt_numdbs = dbn+2;
2289 SLIST_INIT(&mx->mx_cursor.mc_stack);
2290 mx->mx_cursor.mc_txn = &mx->mx_txn;
2291 mx->mx_cursor.mc_dbi = dbn+1;
2295 mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_db *db)
2297 mx->mx_dbs[0] = txn->mt_dbs[0];
2298 mx->mx_dbs[1] = txn->mt_dbs[1];
2300 mx->mx_dbs[2] = txn->mt_dbs[dbi];
2301 mx->mx_dbs[3] = *db;
2303 mx->mx_dbs[2] = *db;
2308 mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
2310 txn->mt_dbs[0] = mx->mx_dbs[0];
2311 txn->mt_dbs[1] = mx->mx_dbs[1];
2312 txn->mt_dbxs[0].md_dirty = mx->mx_dbxs[0].md_dirty;
2313 txn->mt_dbxs[1].md_dirty = mx->mx_dbxs[1].md_dirty;
2315 txn->mt_dbs[dbi] = mx->mx_dbs[2];
2316 txn->mt_dbxs[dbi].md_dirty = mx->mx_dbxs[2].md_dirty;
2321 mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
2324 size_t size = sizeof(MDB_cursor);
2326 if (txn == NULL || ret == NULL || !dbi || dbi >= txn->mt_numdbs)
2329 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT)
2330 size += sizeof(MDB_xcursor);
2332 if ((cursor = calloc(1, size)) != NULL) {
2333 SLIST_INIT(&cursor->mc_stack);
2334 cursor->mc_dbi = dbi;
2335 cursor->mc_txn = txn;
2336 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
2337 MDB_xcursor *mx = (MDB_xcursor *)(cursor + 1);
2338 cursor->mc_xcursor = mx;
2339 mdb_xcursor_init0(txn, dbi, mx);
2351 mdb_cursor_close(MDB_cursor *cursor)
2353 if (cursor != NULL) {
2354 while(!CURSOR_EMPTY(cursor))
2355 cursor_pop_page(cursor);
2356 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
2357 mdb_xcursor_fini(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor);
2358 while(!CURSOR_EMPTY(&cursor->mc_xcursor->mx_cursor))
2359 cursor_pop_page(&cursor->mc_xcursor->mx_cursor);
2367 mdb_update_key(MDB_page *mp, indx_t indx, MDB_val *key)
2369 indx_t ptr, i, numkeys;
2375 node = NODEPTR(mp, indx);
2376 ptr = mp->mp_ptrs[indx];
2377 DPRINTF("update key %u (ofs %u) [%.*s] to [%.*s] on page %lu",
2379 (int)node->mn_ksize, (char *)NODEKEY(node),
2380 (int)key->mv_size, (char *)key->mv_data,
2383 delta = key->mv_size - node->mn_ksize;
2385 if (delta > 0 && SIZELEFT(mp) < delta) {
2386 DPRINTF("OUCH! Not enough room, delta = %d", delta);
2390 numkeys = NUMKEYS(mp);
2391 for (i = 0; i < numkeys; i++) {
2392 if (mp->mp_ptrs[i] <= ptr)
2393 mp->mp_ptrs[i] -= delta;
2396 base = (char *)mp + mp->mp_upper;
2397 len = ptr - mp->mp_upper + NODESIZE;
2398 memmove(base - delta, base, len);
2399 mp->mp_upper -= delta;
2401 node = NODEPTR(mp, indx);
2402 node->mn_ksize = key->mv_size;
2405 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
2410 /* Move a node from src to dst.
2413 mdb_move_node(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src, indx_t srcindx,
2414 MDB_pageparent *dst, indx_t dstindx)
2420 srcnode = NODEPTR(src->mp_page, srcindx);
2421 DPRINTF("moving %s node %u [%.*s] on page %lu to node %u on page %lu",
2422 IS_LEAF(src->mp_page) ? "leaf" : "branch",
2424 (int)srcnode->mn_ksize, (char *)NODEKEY(srcnode),
2425 src->mp_page->mp_pgno,
2426 dstindx, dst->mp_page->mp_pgno);
2428 /* Mark src and dst as dirty. */
2429 if ((rc = mdb_touch(txn, src)) ||
2430 (rc = mdb_touch(txn, dst)))
2433 /* Add the node to the destination page.
2435 key.mv_size = srcnode->mn_ksize;
2436 key.mv_data = NODEKEY(srcnode);
2437 data.mv_size = NODEDSZ(srcnode);
2438 data.mv_data = NODEDATA(srcnode);
2439 rc = mdb_add_node(txn, dbi, dst->mp_page, dstindx, &key, &data, NODEPGNO(srcnode),
2441 if (rc != MDB_SUCCESS)
2444 /* Delete the node from the source page.
2446 mdb_del_node(src->mp_page, srcindx);
2448 /* Update the parent separators.
2450 if (srcindx == 0 && src->mp_pi != 0) {
2451 DPRINTF("update separator for source page %lu to [%.*s]",
2452 src->mp_page->mp_pgno, (int)key.mv_size, (char *)key.mv_data);
2453 if ((rc = mdb_update_key(src->mp_parent, src->mp_pi,
2454 &key)) != MDB_SUCCESS)
2458 if (srcindx == 0 && IS_BRANCH(src->mp_page)) {
2460 nullkey.mv_size = 0;
2461 assert(mdb_update_key(src->mp_page, 0, &nullkey) == MDB_SUCCESS);
2464 if (dstindx == 0 && dst->mp_pi != 0) {
2465 DPRINTF("update separator for destination page %lu to [%.*s]",
2466 dst->mp_page->mp_pgno, (int)key.mv_size, (char *)key.mv_data);
2467 if ((rc = mdb_update_key(dst->mp_parent, dst->mp_pi,
2468 &key)) != MDB_SUCCESS)
2472 if (dstindx == 0 && IS_BRANCH(dst->mp_page)) {
2474 nullkey.mv_size = 0;
2475 assert(mdb_update_key(dst->mp_page, 0, &nullkey) == MDB_SUCCESS);
2482 mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src, MDB_pageparent *dst)
2491 DPRINTF("merging page %lu and %lu", src->mp_page->mp_pgno, dst->mp_page->mp_pgno);
2493 assert(txn != NULL);
2494 assert(src->mp_parent); /* can't merge root page */
2495 assert(dst->mp_parent);
2497 /* Mark src and dst as dirty. */
2498 if ((rc = mdb_touch(txn, src)) ||
2499 (rc = mdb_touch(txn, dst)))
2502 /* Move all nodes from src to dst.
2504 for (i = 0; i < NUMKEYS(src->mp_page); i++) {
2505 srcnode = NODEPTR(src->mp_page, i);
2507 key.mv_size = srcnode->mn_ksize;
2508 key.mv_data = NODEKEY(srcnode);
2509 data.mv_size = NODEDSZ(srcnode);
2510 data.mv_data = NODEDATA(srcnode);
2511 rc = mdb_add_node(txn, dbi, dst->mp_page, NUMKEYS(dst->mp_page), &key,
2512 &data, NODEPGNO(srcnode), srcnode->mn_flags);
2513 if (rc != MDB_SUCCESS)
2517 DPRINTF("dst page %lu now has %u keys (%.1f%% filled)",
2518 dst->mp_page->mp_pgno, NUMKEYS(dst->mp_page), (float)PAGEFILL(txn->mt_env, dst->mp_page) / 10);
2520 /* Unlink the src page from parent.
2522 mdb_del_node(src->mp_parent, src->mp_pi);
2523 if (src->mp_pi == 0) {
2525 if ((rc = mdb_update_key(src->mp_parent, 0, &key)) != MDB_SUCCESS)
2529 if (IS_LEAF(src->mp_page))
2530 txn->mt_dbs[dbi].md_leaf_pages--;
2532 txn->mt_dbs[dbi].md_branch_pages--;
2534 mpp.mp_page = src->mp_parent;
2535 dh = (MDB_dhead *)src->mp_parent;
2537 mpp.mp_parent = dh->md_parent;
2538 mpp.mp_pi = dh->md_pi;
2540 return mdb_rebalance(txn, dbi, &mpp);
2543 #define FILL_THRESHOLD 250
2546 mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mpp)
2551 indx_t si = 0, di = 0;
2553 assert(txn != NULL);
2554 assert(mpp != NULL);
2556 DPRINTF("rebalancing %s page %lu (has %u keys, %.1f%% full)",
2557 IS_LEAF(mpp->mp_page) ? "leaf" : "branch",
2558 mpp->mp_page->mp_pgno, NUMKEYS(mpp->mp_page), (float)PAGEFILL(txn->mt_env, mpp->mp_page) / 10);
2560 if (PAGEFILL(txn->mt_env, mpp->mp_page) >= FILL_THRESHOLD) {
2561 DPRINTF("no need to rebalance page %lu, above fill threshold",
2562 mpp->mp_page->mp_pgno);
2566 if (mpp->mp_parent == NULL) {
2567 if (NUMKEYS(mpp->mp_page) == 0) {
2568 DPRINTF("tree is completely empty");
2569 txn->mt_dbs[dbi].md_root = P_INVALID;
2570 txn->mt_dbs[dbi].md_depth--;
2571 txn->mt_dbs[dbi].md_leaf_pages--;
2572 } else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) {
2573 DPRINTF("collapsing root page!");
2574 txn->mt_dbs[dbi].md_root = NODEPGNO(NODEPTR(mpp->mp_page, 0));
2575 if ((root = mdb_get_page(txn, txn->mt_dbs[dbi].md_root)) == NULL)
2577 txn->mt_dbs[dbi].md_depth--;
2578 txn->mt_dbs[dbi].md_branch_pages--;
2580 DPRINTF("root page doesn't need rebalancing");
2584 /* The parent (branch page) must have at least 2 pointers,
2585 * otherwise the tree is invalid.
2587 assert(NUMKEYS(mpp->mp_parent) > 1);
2589 /* Leaf page fill factor is below the threshold.
2590 * Try to move keys from left or right neighbor, or
2591 * merge with a neighbor page.
2596 if (mpp->mp_pi == 0) {
2597 /* We're the leftmost leaf in our parent.
2599 DPRINTF("reading right neighbor");
2600 node = NODEPTR(mpp->mp_parent, mpp->mp_pi + 1);
2601 if ((npp.mp_page = mdb_get_page(txn, NODEPGNO(node))) == NULL)
2603 npp.mp_pi = mpp->mp_pi + 1;
2605 di = NUMKEYS(mpp->mp_page);
2607 /* There is at least one neighbor to the left.
2609 DPRINTF("reading left neighbor");
2610 node = NODEPTR(mpp->mp_parent, mpp->mp_pi - 1);
2611 if ((npp.mp_page = mdb_get_page(txn, NODEPGNO(node))) == NULL)
2613 npp.mp_pi = mpp->mp_pi - 1;
2614 si = NUMKEYS(npp.mp_page) - 1;
2617 npp.mp_parent = mpp->mp_parent;
2619 DPRINTF("found neighbor page %lu (%u keys, %.1f%% full)",
2620 npp.mp_page->mp_pgno, NUMKEYS(npp.mp_page), (float)PAGEFILL(txn->mt_env, npp.mp_page) / 10);
2622 /* If the neighbor page is above threshold and has at least two
2623 * keys, move one key from it.
2625 * Otherwise we should try to merge them.
2627 if (PAGEFILL(txn->mt_env, npp.mp_page) >= FILL_THRESHOLD && NUMKEYS(npp.mp_page) >= 2)
2628 return mdb_move_node(txn, dbi, &npp, si, mpp, di);
2629 else { /* FIXME: if (has_enough_room()) */
2630 if (mpp->mp_pi == 0)
2631 return mdb_merge(txn, dbi, &npp, mpp);
2633 return mdb_merge(txn, dbi, mpp, &npp);
2638 mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki, MDB_pageparent *mpp, MDB_node *leaf)
2642 /* add overflow pages to free list */
2643 if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
2647 memcpy(&pg, NODEDATA(leaf), sizeof(pg));
2648 ovpages = OVPAGES(NODEDSZ(leaf), txn->mt_env->me_psize);
2649 for (i=0; i<ovpages; i++) {
2650 DPRINTF("freed ov page %lu", pg);
2651 mdb_idl_insert(txn->mt_free_pgs, pg);
2655 mdb_del_node(mpp->mp_page, ki);
2656 txn->mt_dbs[dbi].md_entries--;
2657 rc = mdb_rebalance(txn, dbi, mpp);
2658 if (rc != MDB_SUCCESS)
2659 txn->mt_flags |= MDB_TXN_ERROR;
2665 mdb_del(MDB_txn *txn, MDB_dbi dbi,
2666 MDB_val *key, MDB_val *data,
2674 DPRINTF("========> delete key %.*s", (int)key->mv_size, (char *)key->mv_data);
2676 assert(key != NULL);
2678 if (txn == NULL || dbi >= txn->mt_numdbs)
2681 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
2685 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
2689 mpp.mp_parent = NULL;
2691 if ((rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp)) != MDB_SUCCESS)
2694 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
2695 if (leaf == NULL || !exact) {
2696 return MDB_NOTFOUND;
2699 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
2703 mdb_xcursor_init0(txn, dbi, &mx);
2704 mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
2705 if (flags == MDB_DEL_DUP) {
2706 rc = mdb_del(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, NULL, 0);
2707 mdb_xcursor_fini(txn, dbi, &mx);
2708 if (rc != MDB_SUCCESS)
2710 /* If sub-DB still has entries, we're done */
2711 if (mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root != P_INVALID) {
2712 memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
2716 /* otherwise fall thru and delete the sub-DB */
2718 /* add all the child DB's pages to the free list */
2719 rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi,
2720 NULL, &mx.mx_cursor, 0, &mp2);
2721 if (rc == MDB_SUCCESS) {
2722 MDB_ppage *top, *parent;
2726 cursor_pop_page(&mx.mx_cursor);
2727 top = CURSOR_TOP(&mx.mx_cursor);
2729 parent = SLIST_NEXT(top, mp_entry);
2730 while (parent != NULL) {
2731 for (i=0; i<NUMKEYS(top->mp_page); i++) {
2732 ni = NODEPTR(top->mp_page, i);
2733 mdb_idl_insert(txn->mt_free_pgs, ni->mn_pgno);
2737 if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
2738 cursor_pop_page(&mx.mx_cursor);
2739 top = CURSOR_TOP(&mx.mx_cursor);
2740 parent = SLIST_NEXT(top, mp_entry);
2742 ni = NODEPTR(parent->mp_page, parent->mp_ki);
2743 top->mp_page = mdb_get_page(&mx.mx_txn, ni->mn_pgno);
2748 mdb_idl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root);
2753 if (data && (rc = mdb_read_data(txn, leaf, data)) != MDB_SUCCESS)
2756 return mdb_del0(txn, dbi, ki, &mpp, leaf);
2759 /* Split page <*mpp>, and insert <key,(data|newpgno)> in either left or
2760 * right sibling, at index <*newindxp> (as if unsplit). Updates *mpp and
2761 * *newindxp with the actual values after split, ie if *mpp and *newindxp
2762 * refer to a node in the new right sibling page.
2765 mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp, unsigned int *newindxp,
2766 MDB_val *newkey, MDB_val *newdata, pgno_t newpgno)
2769 int rc = MDB_SUCCESS, ins_new = 0;
2772 unsigned int i, j, split_indx;
2774 MDB_val sepkey, rkey, rdata;
2776 MDB_dpage *mdp, *rdp, *pdp;
2779 assert(txn != NULL);
2781 dh = ((MDB_dhead *)*mpp) - 1;
2782 mdp = (MDB_dpage *)dh;
2783 newindx = *newindxp;
2785 DPRINTF("-----> splitting %s page %lu and adding [%.*s] at index %i",
2786 IS_LEAF(&mdp->p) ? "leaf" : "branch", mdp->p.mp_pgno,
2787 (int)newkey->mv_size, (char *)newkey->mv_data, *newindxp);
2789 if (mdp->h.md_parent == NULL) {
2790 if ((pdp = mdb_new_page(txn, dbi, P_BRANCH, 1)) == NULL)
2793 mdp->h.md_parent = &pdp->p;
2794 txn->mt_dbs[dbi].md_root = pdp->p.mp_pgno;
2795 DPRINTF("root split! new root = %lu", pdp->p.mp_pgno);
2796 txn->mt_dbs[dbi].md_depth++;
2798 /* Add left (implicit) pointer. */
2799 if (mdb_add_node(txn, dbi, &pdp->p, 0, NULL, NULL,
2800 mdp->p.mp_pgno, 0) != MDB_SUCCESS)
2803 DPRINTF("parent branch page is %lu", mdp->h.md_parent->mp_pgno);
2806 /* Create a right sibling. */
2807 if ((rdp = mdb_new_page(txn, dbi, mdp->p.mp_flags, 1)) == NULL)
2809 rdp->h.md_parent = mdp->h.md_parent;
2810 rdp->h.md_pi = mdp->h.md_pi + 1;
2811 DPRINTF("new right sibling: page %lu", rdp->p.mp_pgno);
2813 /* Move half of the keys to the right sibling. */
2814 if ((copy = malloc(txn->mt_env->me_psize)) == NULL)
2816 memcpy(copy, &mdp->p, txn->mt_env->me_psize);
2817 memset(&mdp->p.mp_ptrs, 0, txn->mt_env->me_psize - PAGEHDRSZ);
2818 mdp->p.mp_lower = PAGEHDRSZ;
2819 mdp->p.mp_upper = txn->mt_env->me_psize;
2821 split_indx = NUMKEYS(copy) / 2 + 1;
2823 /* First find the separating key between the split pages.
2825 memset(&sepkey, 0, sizeof(sepkey));
2826 if (newindx == split_indx) {
2827 sepkey.mv_size = newkey->mv_size;
2828 sepkey.mv_data = newkey->mv_data;
2830 node = NODEPTR(copy, split_indx);
2831 sepkey.mv_size = node->mn_ksize;
2832 sepkey.mv_data = NODEKEY(node);
2835 DPRINTF("separator is [%.*s]", (int)sepkey.mv_size, (char *)sepkey.mv_data);
2837 /* Copy separator key to the parent.
2839 if (SIZELEFT(rdp->h.md_parent) < mdb_branch_size(txn->mt_env, &sepkey)) {
2840 rc = mdb_split(txn, dbi, &rdp->h.md_parent, &rdp->h.md_pi,
2841 &sepkey, NULL, rdp->p.mp_pgno);
2843 /* Right page might now have changed parent.
2844 * Check if left page also changed parent.
2846 if (rdp->h.md_parent != mdp->h.md_parent &&
2847 mdp->h.md_pi >= NUMKEYS(mdp->h.md_parent)) {
2848 mdp->h.md_parent = rdp->h.md_parent;
2849 mdp->h.md_pi = rdp->h.md_pi - 1;
2852 rc = mdb_add_node(txn, dbi, rdp->h.md_parent, rdp->h.md_pi,
2853 &sepkey, NULL, rdp->p.mp_pgno, 0);
2855 if (rc != MDB_SUCCESS) {
2860 for (i = j = 0; i <= NUMKEYS(copy); j++) {
2861 if (i < split_indx) {
2862 /* Re-insert in left sibling. */
2865 /* Insert in right sibling. */
2866 if (i == split_indx)
2867 /* Reset insert index for right sibling. */
2868 j = (i == newindx && ins_new);
2872 if (i == newindx && !ins_new) {
2873 /* Insert the original entry that caused the split. */
2874 rkey.mv_data = newkey->mv_data;
2875 rkey.mv_size = newkey->mv_size;
2876 if (IS_LEAF(&mdp->p)) {
2877 rdata.mv_data = newdata->mv_data;
2878 rdata.mv_size = newdata->mv_size;
2885 /* Update page and index for the new key. */
2888 } else if (i == NUMKEYS(copy)) {
2891 node = NODEPTR(copy, i);
2892 rkey.mv_data = NODEKEY(node);
2893 rkey.mv_size = node->mn_ksize;
2894 if (IS_LEAF(&mdp->p)) {
2895 rdata.mv_data = NODEDATA(node);
2896 rdata.mv_size = node->mn_dsize;
2898 pgno = node->mn_pgno;
2899 flags = node->mn_flags;
2904 if (!IS_LEAF(&mdp->p) && j == 0) {
2905 /* First branch index doesn't need key data. */
2909 rc = mdb_add_node(txn, dbi, &pdp->p, j, &rkey, &rdata, pgno,flags);
2917 mdb_put0(MDB_txn *txn, MDB_dbi dbi,
2918 MDB_val *key, MDB_val *data, unsigned int flags)
2920 int rc = MDB_SUCCESS, exact;
2924 MDB_val xdata, *rdata;
2927 DPRINTF("==> put key %.*s, size %zu, data size %zu",
2928 (int)key->mv_size, (char *)key->mv_data, key->mv_size, data->mv_size);
2930 mpp.mp_parent = NULL;
2932 rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp);
2933 if (rc == MDB_SUCCESS) {
2934 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
2935 if (leaf && exact) {
2936 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
2939 if (flags == MDB_NOOVERWRITE) {
2940 DPRINTF("duplicate key %.*s",
2941 (int)key->mv_size, (char *)key->mv_data);
2942 return MDB_KEYEXIST;
2944 /* same size, just replace it */
2945 if (NODEDSZ(leaf) == data->mv_size) {
2946 memcpy(NODEDATA(leaf), data->mv_data, data->mv_size);
2949 mdb_del_node(mpp.mp_page, ki);
2951 if (leaf == NULL) { /* append if not found */
2952 ki = NUMKEYS(mpp.mp_page);
2953 DPRINTF("appending key at index %i", ki);
2955 } else if (rc == MDB_NOTFOUND) {
2957 /* new file, just write a root leaf page */
2958 DPRINTF("allocating new root leaf page");
2959 if ((dp = mdb_new_page(txn, dbi, P_LEAF, 1)) == NULL) {
2962 mpp.mp_page = &dp->p;
2963 txn->mt_dbs[dbi].md_root = mpp.mp_page->mp_pgno;
2964 txn->mt_dbs[dbi].md_depth++;
2970 assert(IS_LEAF(mpp.mp_page));
2971 DPRINTF("there are %u keys, should insert new key at index %i",
2972 NUMKEYS(mpp.mp_page), ki);
2974 /* For sorted dups, the data item at this level is a DB record
2975 * for a child DB; the actual data elements are stored as keys
2978 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
2980 xdata.mv_size = sizeof(MDB_db);
2981 xdata.mv_data = &dummy;
2982 memset(&dummy, 0, sizeof(dummy));
2983 dummy.md_root = P_INVALID;
2988 if (SIZELEFT(mpp.mp_page) < mdb_leaf_size(txn->mt_env, key, data)) {
2989 rc = mdb_split(txn, dbi, &mpp.mp_page, &ki, key, data, P_INVALID);
2991 /* There is room already in this leaf page. */
2992 rc = mdb_add_node(txn, dbi, mpp.mp_page, ki, key, data, 0, 0);
2995 if (rc != MDB_SUCCESS)
2996 txn->mt_flags |= MDB_TXN_ERROR;
2998 txn->mt_dbs[dbi].md_entries++;
3000 /* Remember if we just added a subdatabase */
3001 if (flags & F_SUBDATA) {
3002 leaf = NODEPTR(mpp.mp_page, ki);
3003 leaf->mn_flags |= F_SUBDATA;
3006 /* Now store the actual data in the child DB. Note that we're
3007 * storing the user data in the keys field, so there are strict
3008 * size limits on dupdata. The actual data fields of the child
3009 * DB are all zero size.
3011 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
3014 leaf = NODEPTR(mpp.mp_page, ki);
3016 mdb_xcursor_init0(txn, dbi, &mx);
3017 mdb_xcursor_init1(txn, dbi, &mx, NODEDATA(leaf));
3020 if (flags == MDB_NODUPDATA)
3021 flags = MDB_NOOVERWRITE;
3022 rc = mdb_put0(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, &xdata, flags);
3023 mdb_xcursor_fini(txn, dbi, &mx);
3024 memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
3034 mdb_put(MDB_txn *txn, MDB_dbi dbi,
3035 MDB_val *key, MDB_val *data, unsigned int flags)
3037 assert(key != NULL);
3038 assert(data != NULL);
3040 if (txn == NULL || dbi >= txn->mt_numdbs)
3043 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
3047 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
3051 if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA)) != flags)
3054 return mdb_put0(txn, dbi, key, data, flags);
3058 mdbenv_get_flags(MDB_env *env, unsigned int *arg)
3063 *arg = env->me_flags;
3068 mdbenv_get_path(MDB_env *env, const char **arg)
3073 *arg = env->me_path;
3078 mdbenv_stat(MDB_env *env, MDB_stat *arg)
3080 if (env == NULL || arg == NULL)
3083 arg->ms_psize = env->me_psize;
3084 arg->ms_depth = env->me_meta->mm_dbs[MAIN_DBI].md_depth;
3085 arg->ms_branch_pages = env->me_meta->mm_dbs[MAIN_DBI].md_branch_pages;
3086 arg->ms_leaf_pages = env->me_meta->mm_dbs[MAIN_DBI].md_leaf_pages;
3087 arg->ms_overflow_pages = env->me_meta->mm_dbs[MAIN_DBI].md_overflow_pages;
3088 arg->ms_entries = env->me_meta->mm_dbs[MAIN_DBI].md_entries;
3093 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
3106 /* Is the DB already open? */
3108 for (i=2; i<txn->mt_numdbs; i++) {
3109 if (len == txn->mt_dbxs[i].md_name.mv_size &&
3110 !strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) {
3116 if (txn->mt_numdbs >= txn->mt_env->me_maxdbs - 1)
3119 /* Find the DB info */
3121 key.mv_data = (void *)name;
3122 rc = mdb_get(txn, MAIN_DBI, &key, &data);
3124 /* Create if requested */
3125 if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
3127 data.mv_size = sizeof(MDB_db);
3128 data.mv_data = &dummy;
3129 memset(&dummy, 0, sizeof(dummy));
3130 dummy.md_root = P_INVALID;
3131 dummy.md_flags = flags & 0xffff;
3132 rc = mdb_put0(txn, MAIN_DBI, &key, &data, F_SUBDATA);
3136 /* OK, got info, add to table */
3137 if (rc == MDB_SUCCESS) {
3138 txn->mt_dbxs[txn->mt_numdbs].md_name.mv_data = strdup(name);
3139 txn->mt_dbxs[txn->mt_numdbs].md_name.mv_size = len;
3140 txn->mt_dbxs[txn->mt_numdbs].md_cmp = NULL;
3141 txn->mt_dbxs[txn->mt_numdbs].md_dcmp = NULL;
3142 txn->mt_dbxs[txn->mt_numdbs].md_rel = NULL;
3143 txn->mt_dbxs[txn->mt_numdbs].md_parent = MAIN_DBI;
3144 txn->mt_dbxs[txn->mt_numdbs].md_dirty = dirty;
3145 memcpy(&txn->mt_dbs[txn->mt_numdbs], data.mv_data, sizeof(MDB_db));
3146 *dbi = txn->mt_numdbs;
3153 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
3155 if (txn == NULL || arg == NULL)
3158 arg->ms_psize = txn->mt_env->me_psize;
3159 arg->ms_depth = txn->mt_dbs[dbi].md_depth;
3160 arg->ms_branch_pages = txn->mt_dbs[dbi].md_branch_pages;
3161 arg->ms_leaf_pages = txn->mt_dbs[dbi].md_leaf_pages;
3162 arg->ms_overflow_pages = txn->mt_dbs[dbi].md_overflow_pages;
3163 arg->ms_entries = txn->mt_dbs[dbi].md_entries;
3168 void mdb_close(MDB_txn *txn, MDB_dbi dbi)
3171 if (dbi <= MAIN_DBI || dbi >= txn->mt_numdbs)
3173 ptr = txn->mt_dbxs[dbi].md_name.mv_data;
3174 txn->mt_dbxs[dbi].md_name.mv_data = NULL;
3175 txn->mt_dbxs[dbi].md_name.mv_size = 0;