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
54 #define ULONG unsigned long
63 #if DEBUG && defined(__GNUC__)
64 # define DPRINTF(fmt, ...) \
65 fprintf(stderr, "%s:%d: " fmt "\n", __func__, __LINE__, ##__VA_ARGS__)
67 # define DPRINTF(...) ((void) 0)
72 #define MDB_MAGIC 0xBEEFC0DE
74 #define MAXKEYSIZE 511
76 #define P_INVALID (~0UL)
78 #define F_ISSET(w, f) (((w) & (f)) == (f))
80 typedef uint16_t indx_t;
82 #define DEFAULT_READERS 126
83 #define DEFAULT_MAPSIZE 1048576
85 /* Lock descriptor stuff */
90 typedef struct MDB_rxbody {
96 # define CACHELINE 128 /* 64 is too small to contain a mutex */
98 # define CACHELINE 64 /* most CPUs. Itanium uses 128 */
102 typedef struct MDB_reader {
104 /* cache line alignment */
105 char pad[CACHELINE-sizeof(MDB_rxbody)];
110 uint32_t mt_version; \
111 pthread_mutex_t mt_mutex; \
113 uint32_t mt_numreaders
114 typedef struct MDB_txbody {
118 typedef struct MDB_txninfo {
120 char pad[CACHELINE-sizeof(MDB_txbody)];
121 pthread_mutex_t mt_wmutex;
122 char pad2[CACHELINE-sizeof(pthread_mutex_t)];
123 MDB_reader mt_readers[1];
126 /* Common header for all page types. Overflow pages
127 * occupy a number of contiguous pages with no
128 * headers on any page after the first.
130 typedef struct MDB_page { /* represents a page of storage */
131 #define mp_pgno mp_p.p_pgno
133 pgno_t p_pgno; /* page number */
136 #define P_BRANCH 0x01 /* branch page */
137 #define P_LEAF 0x02 /* leaf page */
138 #define P_OVERFLOW 0x04 /* overflow page */
139 #define P_META 0x08 /* meta page */
140 #define P_DIRTY 0x10 /* dirty page */
142 #define mp_lower mp_pb.pb.pb_lower
143 #define mp_upper mp_pb.pb.pb_upper
144 #define mp_pages mp_pb.pb_pages
147 indx_t pb_lower; /* lower bound of free space */
148 indx_t pb_upper; /* upper bound of free space */
150 uint32_t pb_pages; /* number of overflow pages */
152 indx_t mp_ptrs[1]; /* dynamic size */
155 #define PAGEHDRSZ ((unsigned) offsetof(MDB_page, mp_ptrs))
157 #define NUMKEYS(p) (((p)->mp_lower - PAGEHDRSZ) >> 1)
158 #define SIZELEFT(p) (indx_t)((p)->mp_upper - (p)->mp_lower)
159 #define PAGEFILL(env, p) (1000L * ((env)->me_psize - PAGEHDRSZ - SIZELEFT(p)) / \
160 ((env)->me_psize - PAGEHDRSZ))
161 #define IS_LEAF(p) F_ISSET((p)->mp_flags, P_LEAF)
162 #define IS_BRANCH(p) F_ISSET((p)->mp_flags, P_BRANCH)
163 #define IS_OVERFLOW(p) F_ISSET((p)->mp_flags, P_OVERFLOW)
165 #define OVPAGES(size, psize) (PAGEHDRSZ + size + psize - 1) / psize;
167 typedef struct MDB_db {
171 ULONG md_branch_pages;
173 ULONG md_overflow_pages;
181 typedef struct MDB_meta { /* meta (footer) page content */
184 void *mm_address; /* address for fixed mapping */
185 size_t mm_mapsize; /* size of mmap region */
186 MDB_db mm_dbs[2]; /* first is free space, 2nd is main db */
187 #define mm_psize mm_dbs[0].md_pad
188 #define mm_flags mm_dbs[0].md_flags
189 pgno_t mm_last_pg; /* last used page in file */
190 ULONG mm_txnid; /* txnid that committed this page */
193 typedef struct MDB_dhead { /* a dirty page */
194 STAILQ_ENTRY(MDB_dpage) md_next; /* queue of dirty pages */
196 unsigned md_pi; /* parent index */
200 typedef struct MDB_dpage {
205 STAILQ_HEAD(dirty_queue, MDB_dpage); /* FIXME: use a sorted data structure */
207 typedef struct MDB_oldpages {
208 struct MDB_oldpages *mo_next;
210 pgno_t mo_pages[1]; /* dynamic */
213 typedef struct MDB_pageparent {
219 static MDB_dpage *mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num);
220 static int mdb_touch(MDB_txn *txn, MDB_pageparent *mp);
222 typedef struct MDB_ppage { /* ordered list of pages */
223 SLIST_ENTRY(MDB_ppage) mp_entry;
225 unsigned int mp_ki; /* cursor index on page */
227 SLIST_HEAD(page_stack, MDB_ppage);
229 /* FIXME: tree depth is mostly bounded, we should just
230 * use a fixed array and avoid malloc/pointer chasing
232 #define CURSOR_EMPTY(c) SLIST_EMPTY(&(c)->mc_stack)
233 #define CURSOR_TOP(c) SLIST_FIRST(&(c)->mc_stack)
234 #define CURSOR_POP(c) SLIST_REMOVE_HEAD(&(c)->mc_stack, mp_entry)
235 #define CURSOR_PUSH(c,p) SLIST_INSERT_HEAD(&(c)->mc_stack, p, mp_entry)
241 struct page_stack mc_stack; /* stack of parent pages */
243 short mc_initialized; /* 1 if initialized */
244 short mc_eof; /* 1 if end is reached */
245 struct MDB_xcursor *mc_xcursor;
248 #define METAHASHLEN offsetof(MDB_meta, mm_hash)
249 #define METADATA(p) ((void *)((char *)p + PAGEHDRSZ))
251 typedef struct MDB_node {
252 #define mn_pgno mn_p.np_pgno
253 #define mn_dsize mn_p.np_dsize
255 pgno_t np_pgno; /* child page number */
256 uint32_t np_dsize; /* leaf data size */
258 unsigned int mn_flags:4;
259 unsigned int mn_ksize:12; /* key size */
260 #define F_BIGDATA 0x01 /* data put on overflow page */
261 #define F_SUBDATA 0x02 /* data is a sub-database */
265 typedef struct MDB_dbx {
267 MDB_cmp_func *md_cmp; /* user compare function */
268 MDB_cmp_func *md_dcmp; /* user dupsort function */
269 MDB_rel_func *md_rel; /* user relocate function */
271 unsigned int md_dirty;
275 pgno_t mt_next_pgno; /* next unallocated page */
279 pgno_t *mt_free_pgs; /* this is an IDL */
281 struct dirty_queue *dirty_queue; /* modified pages */
284 MDB_dbx *mt_dbxs; /* array */
286 unsigned int mt_numdbs;
288 #define MDB_TXN_RDONLY 0x01 /* read-only transaction */
289 #define MDB_TXN_ERROR 0x02 /* an error has occurred */
290 #define MDB_TXN_METOGGLE 0x04 /* used meta page 1 */
291 unsigned int mt_flags;
294 /* Context for sorted-dup records */
295 typedef struct MDB_xcursor {
296 MDB_cursor mx_cursor;
306 unsigned int me_maxreaders;
307 unsigned int me_numdbs;
308 unsigned int me_maxdbs;
311 MDB_txninfo *me_txns;
312 MDB_meta *me_metas[2];
314 MDB_txn *me_txn; /* current write transaction */
316 off_t me_size; /* current file size */
317 unsigned int me_psize;
319 MDB_dbx *me_dbxs; /* array */
321 MDB_oldpages *me_pghead;
322 pthread_key_t me_txkey; /* thread-key for readers */
323 pgno_t me_free_pgs[MDB_IDL_UM_SIZE];
326 #define NODESIZE offsetof(MDB_node, mn_data)
328 #define INDXSIZE(k) (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size))
329 #define LEAFSIZE(k, d) (NODESIZE + (k)->mv_size + (d)->mv_size)
330 #define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i]))
331 #define NODEKEY(node) (void *)((node)->mn_data)
332 #define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize)
333 #define NODEPGNO(node) ((node)->mn_pgno)
334 #define NODEDSZ(node) ((node)->mn_dsize)
336 #define MDB_COMMIT_PAGES 64 /* max number of pages to write in one commit */
337 #define MDB_MAXCACHE_DEF 1024 /* max number of pages to keep in cache */
339 static int mdb_search_page_root(MDB_txn *txn,
340 MDB_dbi dbi, MDB_val *key,
341 MDB_cursor *cursor, int modify,
342 MDB_pageparent *mpp);
343 static int mdb_search_page(MDB_txn *txn,
344 MDB_dbi dbi, MDB_val *key,
345 MDB_cursor *cursor, int modify,
346 MDB_pageparent *mpp);
348 static int mdbenv_read_header(MDB_env *env, MDB_meta *meta);
349 static int mdbenv_read_meta(MDB_env *env, int *which);
350 static int mdbenv_write_meta(MDB_txn *txn);
351 static MDB_page *mdb_get_page(MDB_txn *txn, pgno_t pgno);
353 static MDB_node *mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
354 MDB_val *key, int *exactp, unsigned int *kip);
355 static int mdb_add_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp,
356 indx_t indx, MDB_val *key, MDB_val *data,
357 pgno_t pgno, uint8_t flags);
358 static void mdb_del_node(MDB_page *mp, indx_t indx);
359 static int mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki,
360 MDB_pageparent *mpp, MDB_node *leaf);
361 static int mdb_put0(MDB_txn *txn, MDB_dbi dbi,
362 MDB_val *key, MDB_val *data, unsigned int flags);
363 static int mdb_read_data(MDB_txn *txn, MDB_node *leaf, MDB_val *data);
365 static int mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mp);
366 static int mdb_update_key(MDB_page *mp, indx_t indx, MDB_val *key);
367 static int mdb_move_node(MDB_txn *txn, MDB_dbi dbi,
368 MDB_pageparent *src, indx_t srcindx,
369 MDB_pageparent *dst, indx_t dstindx);
370 static int mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src,
371 MDB_pageparent *dst);
372 static int mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp,
373 unsigned int *newindxp, MDB_val *newkey,
374 MDB_val *newdata, pgno_t newpgno);
375 static MDB_dpage *mdb_new_page(MDB_txn *txn, MDB_dbi dbi, uint32_t flags, int num);
377 static void cursor_pop_page(MDB_cursor *cursor);
378 static MDB_ppage *cursor_push_page(MDB_cursor *cursor,
381 static int mdb_set_key(MDB_node *node, MDB_val *key);
382 static int mdb_sibling(MDB_cursor *cursor, int move_right);
383 static int mdb_cursor_next(MDB_cursor *cursor,
384 MDB_val *key, MDB_val *data, MDB_cursor_op op);
385 static int mdb_cursor_prev(MDB_cursor *cursor,
386 MDB_val *key, MDB_val *data, MDB_cursor_op op);
387 static int mdb_cursor_set(MDB_cursor *cursor,
388 MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp);
389 static int mdb_cursor_first(MDB_cursor *cursor,
390 MDB_val *key, MDB_val *data);
391 static int mdb_cursor_last(MDB_cursor *cursor,
392 MDB_val *key, MDB_val *data);
394 static void mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
395 static void mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_node *node);
396 static void mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
398 static size_t mdb_leaf_size(MDB_env *env, MDB_val *key,
400 static size_t mdb_branch_size(MDB_env *env, MDB_val *key);
402 static int memncmp(const void *s1, size_t n1,
403 const void *s2, size_t n2);
404 static int memnrcmp(const void *s1, size_t n1,
405 const void *s2, size_t n2);
408 memncmp(const void *s1, size_t n1, const void *s2, size_t n2)
410 int diff, len_diff = -1;
413 len_diff = (n1 > n2);
416 diff = memcmp(s1, s2, n1);
417 return diff ? diff : len_diff;
421 memnrcmp(const void *s1, size_t n1, const void *s2, size_t n2)
423 const unsigned char *p1, *p2, *p1_lim;
430 p1 = (const unsigned char *)s1 + n1 - 1;
431 p2 = (const unsigned char *)s2 + n2 - 1;
433 for (p1_lim = (n1 <= n2 ? s1 : s2); *p1 == *p2; p1--, p2--) {
435 return (p1 != s1) ? (p1 != p2) : (p2 != s2) ? -1 : 0;
441 mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
443 return txn->mt_dbxs[dbi].md_cmp(a, b);
447 _mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *key1, const MDB_val *key2)
449 if (txn->mt_dbs[dbi].md_flags & (MDB_REVERSEKEY
450 #if __BYTE_ORDER == __LITTLE_ENDIAN
454 return memnrcmp(key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
456 return memncmp((char *)key1->mv_data, key1->mv_size, key2->mv_data, key2->mv_size);
459 /* Allocate new page(s) for writing */
461 mdb_alloc_page(MDB_txn *txn, MDB_page *parent, unsigned int parent_idx, int num)
464 pgno_t pgno = P_INVALID;
467 if (txn->mt_txnid > 2) {
469 oldest = txn->mt_txnid - 2;
470 if (!txn->mt_env->me_pghead && txn->mt_dbs[FREE_DBI].md_root != P_INVALID) {
471 /* See if there's anything in the free DB */
476 mpp.mp_parent = NULL;
478 mdb_search_page(txn, FREE_DBI, NULL, NULL, 0, &mpp);
479 leaf = NODEPTR(mpp.mp_page, 0);
480 kptr = (ULONG *)NODEKEY(leaf);
482 /* It's potentially usable, unless there are still
483 * older readers outstanding. Grab it.
485 if (oldest > *kptr) {
490 mdb_read_data(txn, leaf, &data);
491 idl = (ULONG *)data.mv_data;
492 mop = malloc(sizeof(MDB_oldpages) + MDB_IDL_SIZEOF(idl) - sizeof(pgno_t));
493 mop->mo_next = txn->mt_env->me_pghead;
494 mop->mo_txnid = *kptr;
495 txn->mt_env->me_pghead = mop;
496 memcpy(mop->mo_pages, idl, MDB_IDL_SIZEOF(idl));
501 DPRINTF("IDL read txn %lu root %lu num %lu",
502 mop->mo_txnid, txn->mt_dbs[FREE_DBI].md_root, idl[0]);
503 for (i=0; i<idl[0]; i++) {
504 DPRINTF("IDL %lu", idl[i+1]);
508 /* drop this IDL from the DB */
509 mpp.mp_parent = NULL;
511 mdb_search_page(txn, FREE_DBI, NULL, NULL, 1, &mpp);
512 leaf = NODEPTR(mpp.mp_page, 0);
513 mdb_del0(txn, FREE_DBI, 0, &mpp, leaf);
516 if (txn->mt_env->me_pghead) {
518 for (i=0; i<txn->mt_env->me_txns->mt_numreaders; i++) {
519 ULONG mr = txn->mt_env->me_txns->mt_readers[i].mr_txnid;
522 oldest = txn->mt_env->me_txns->mt_readers[i].mr_txnid;
524 if (oldest > txn->mt_env->me_pghead->mo_txnid) {
525 MDB_oldpages *mop = txn->mt_env->me_pghead;
526 txn->mt_oldest = oldest;
528 /* FIXME: For now, always use fresh pages. We
529 * really ought to search the free list for a
534 /* peel pages off tail, so we only have to truncate the list */
535 pgno = MDB_IDL_LAST(mop->mo_pages);
536 if (MDB_IDL_IS_RANGE(mop->mo_pages)) {
538 if (mop->mo_pages[2] > mop->mo_pages[1])
539 mop->mo_pages[0] = 0;
543 if (MDB_IDL_IS_ZERO(mop->mo_pages)) {
544 txn->mt_env->me_pghead = mop->mo_next;
552 if ((dp = malloc(txn->mt_env->me_psize * num + sizeof(MDB_dhead))) == NULL)
555 dp->h.md_parent = parent;
556 dp->h.md_pi = parent_idx;
557 STAILQ_INSERT_TAIL(txn->mt_u.dirty_queue, dp, h.md_next);
558 if (pgno == P_INVALID) {
559 dp->p.mp_pgno = txn->mt_next_pgno;
560 txn->mt_next_pgno += num;
562 dp->p.mp_pgno = pgno;
568 /* Touch a page: make it dirty and re-insert into tree with updated pgno.
571 mdb_touch(MDB_txn *txn, MDB_pageparent *pp)
573 MDB_page *mp = pp->mp_page;
578 if (!F_ISSET(mp->mp_flags, P_DIRTY)) {
580 if ((dp = mdb_alloc_page(txn, pp->mp_parent, pp->mp_pi, 1)) == NULL)
582 DPRINTF("touched page %lu -> %lu", mp->mp_pgno, dp->p.mp_pgno);
583 mdb_idl_insert(txn->mt_free_pgs, mp->mp_pgno);
584 pgno = dp->p.mp_pgno;
585 memcpy(&dp->p, mp, txn->mt_env->me_psize);
588 mp->mp_flags |= P_DIRTY;
590 /* Update the page number to new touched page. */
591 if (pp->mp_parent != NULL)
592 NODEPGNO(NODEPTR(pp->mp_parent, pp->mp_pi)) = mp->mp_pgno;
599 mdbenv_sync(MDB_env *env)
602 if (!F_ISSET(env->me_flags, MDB_NOSYNC)) {
603 if (fsync(env->me_fd))
610 mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **ret)
615 if ((txn = calloc(1, sizeof(MDB_txn))) == NULL) {
616 DPRINTF("calloc: %s", strerror(errno));
621 txn->mt_flags |= MDB_TXN_RDONLY;
623 txn->mt_u.dirty_queue = calloc(1, sizeof(*txn->mt_u.dirty_queue));
624 if (txn->mt_u.dirty_queue == NULL) {
628 STAILQ_INIT(txn->mt_u.dirty_queue);
630 pthread_mutex_lock(&env->me_txns->mt_wmutex);
631 env->me_txns->mt_txnid++;
632 txn->mt_free_pgs = env->me_free_pgs;
633 txn->mt_free_pgs[0] = 0;
636 txn->mt_txnid = env->me_txns->mt_txnid;
638 MDB_reader *r = pthread_getspecific(env->me_txkey);
641 pthread_mutex_lock(&env->me_txns->mt_mutex);
642 for (i=0; i<env->me_txns->mt_numreaders; i++)
643 if (env->me_txns->mt_readers[i].mr_pid == 0)
645 if (i == env->me_maxreaders) {
646 pthread_mutex_unlock(&env->me_txns->mti_mutex);
649 env->me_txns->mt_readers[i].mr_pid = getpid();
650 env->me_txns->mt_readers[i].mr_tid = pthread_self();
651 r = &env->me_txns->mt_readers[i];
652 pthread_setspecific(env->me_txkey, r);
653 if (i >= env->me_txns->mt_numreaders)
654 env->me_txns->mt_numreaders = i+1;
655 pthread_mutex_unlock(&env->me_txns->mt_mutex);
657 r->mr_txnid = txn->mt_txnid;
658 txn->mt_u.reader = r;
665 if ((rc = mdbenv_read_meta(env, &toggle)) != MDB_SUCCESS) {
670 /* Copy the DB arrays */
671 txn->mt_numdbs = env->me_numdbs;
672 txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */
673 txn->mt_dbs = malloc(env->me_maxdbs * sizeof(MDB_db));
674 memcpy(txn->mt_dbs, env->me_meta->mm_dbs, 2 * sizeof(MDB_db));
675 if (txn->mt_numdbs > 2)
676 memcpy(txn->mt_dbs+2, env->me_dbs[env->me_db_toggle]+2,
677 (txn->mt_numdbs - 2) * sizeof(MDB_db));
681 txn->mt_flags |= MDB_TXN_METOGGLE;
682 txn->mt_next_pgno = env->me_meta->mm_last_pg+1;
685 DPRINTF("begin transaction %lu on mdbenv %p, root page %lu",
686 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
693 mdb_txn_abort(MDB_txn *txn)
702 DPRINTF("abort transaction %lu on mdbenv %p, root page %lu",
703 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
707 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
708 txn->mt_u.reader->mr_txnid = 0;
713 /* Discard all dirty pages. */
714 while (!STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
715 dp = STAILQ_FIRST(txn->mt_u.dirty_queue);
716 STAILQ_REMOVE_HEAD(txn->mt_u.dirty_queue, h.md_next);
719 free(txn->mt_u.dirty_queue);
721 while ((mop = txn->mt_env->me_pghead)) {
722 txn->mt_env->me_pghead = mop->mo_next;
727 env->me_txns->mt_txnid--;
728 for (i=2; i<env->me_numdbs; i++)
729 env->me_dbxs[i].md_dirty = 0;
730 pthread_mutex_unlock(&env->me_txns->mt_wmutex);
737 mdb_txn_commit(MDB_txn *txn)
746 struct iovec iov[MDB_COMMIT_PAGES];
749 assert(txn->mt_env != NULL);
753 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
754 DPRINTF("attempt to commit read-only transaction");
759 if (txn != env->me_txn) {
760 DPRINTF("attempt to commit unknown transaction");
765 if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) {
766 DPRINTF("error flag is set, can't commit");
771 if (STAILQ_EMPTY(txn->mt_u.dirty_queue))
774 DPRINTF("committing transaction %lu on mdbenv %p, root page %lu",
775 txn->mt_txnid, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
777 /* should only be one record now */
778 if (env->me_pghead) {
782 mop = env->me_pghead;
783 key.mv_size = sizeof(pgno_t);
784 key.mv_data = (char *)&mop->mo_txnid;
785 data.mv_size = MDB_IDL_SIZEOF(mop->mo_pages);
786 data.mv_data = mop->mo_pages;
787 mdb_put0(txn, FREE_DBI, &key, &data, 0);
788 free(env->me_pghead);
789 env->me_pghead = NULL;
791 /* save to free list */
792 if (!MDB_IDL_IS_ZERO(txn->mt_free_pgs)) {
796 /* make sure last page of freeDB is touched and on freelist */
797 key.mv_size = MAXKEYSIZE+1;
799 mpp.mp_parent = NULL;
801 mdb_search_page(txn, FREE_DBI, &key, NULL, 1, &mpp);
806 ULONG *idl = txn->mt_free_pgs;
807 DPRINTF("IDL write txn %lu root %lu num %lu",
808 txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, idl[0]);
809 for (i=0; i<idl[0]; i++) {
810 DPRINTF("IDL %lu", idl[i+1]);
814 /* write to last page of freeDB */
815 key.mv_size = sizeof(pgno_t);
816 key.mv_data = (char *)&txn->mt_txnid;
817 data.mv_size = MDB_IDL_SIZEOF(txn->mt_free_pgs);
818 data.mv_data = txn->mt_free_pgs;
819 mdb_put0(txn, FREE_DBI, &key, &data, 0);
822 /* Update DB root pointers. Their pages have already been
823 * touched so this is all in-place and cannot fail.
827 data.mv_size = sizeof(MDB_db);
829 for (i = 2; i < txn->mt_numdbs; i++) {
830 if (txn->mt_dbxs[i].md_dirty) {
831 data.mv_data = &txn->mt_dbs[i];
832 mdb_put0(txn, MAIN_DBI, &txn->mt_dbxs[i].md_name, &data, 0);
837 /* Commit up to MDB_COMMIT_PAGES dirty pages to disk until done.
844 STAILQ_FOREACH(dp, txn->mt_u.dirty_queue, h.md_next) {
845 if (dp->p.mp_pgno != next) {
847 DPRINTF("committing %u dirty pages", n);
848 rc = writev(env->me_fd, iov, n);
852 DPRINTF("short write, filesystem full?");
854 DPRINTF("writev: %s", strerror(errno));
861 lseek(env->me_fd, dp->p.mp_pgno * env->me_psize, SEEK_SET);
862 next = dp->p.mp_pgno;
864 DPRINTF("committing page %lu", dp->p.mp_pgno);
865 iov[n].iov_len = env->me_psize * dp->h.md_num;
866 iov[n].iov_base = &dp->p;
867 size += iov[n].iov_len;
868 next = dp->p.mp_pgno + dp->h.md_num;
869 /* clear dirty flag */
870 dp->p.mp_flags &= ~P_DIRTY;
871 if (++n >= MDB_COMMIT_PAGES) {
880 DPRINTF("committing %u dirty pages", n);
881 rc = writev(env->me_fd, iov, n);
885 DPRINTF("short write, filesystem full?");
887 DPRINTF("writev: %s", strerror(errno));
894 /* Drop the dirty pages.
896 while (!STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
897 dp = STAILQ_FIRST(txn->mt_u.dirty_queue);
898 STAILQ_REMOVE_HEAD(txn->mt_u.dirty_queue, h.md_next);
902 if ((n = mdbenv_sync(env)) != 0 ||
903 (n = mdbenv_write_meta(txn)) != MDB_SUCCESS ||
904 (n = mdbenv_sync(env)) != 0) {
910 /* update the DB tables */
912 int toggle = !env->me_db_toggle;
914 for (i = 2; i < env->me_numdbs; i++) {
915 if (txn->mt_dbxs[i].md_dirty) {
916 env->me_dbs[toggle][i] = txn->mt_dbs[i];
917 txn->mt_dbxs[i].md_dirty = 0;
920 for (i = env->me_numdbs; i < txn->mt_numdbs; i++) {
921 txn->mt_dbxs[i].md_dirty = 0;
922 env->me_dbxs[i] = txn->mt_dbxs[i];
923 env->me_dbs[toggle][i] = txn->mt_dbs[i];
925 env->me_db_toggle = toggle;
926 env->me_numdbs = txn->mt_numdbs;
931 pthread_mutex_unlock(&env->me_txns->mt_wmutex);
932 free(txn->mt_u.dirty_queue);
943 mdbenv_read_header(MDB_env *env, MDB_meta *meta)
952 /* We don't know the page size yet, so use a minimum value.
955 if ((rc = pread(env->me_fd, page, PAGESIZE, 0)) == 0) {
957 } else if (rc != PAGESIZE) {
960 DPRINTF("read: %s", strerror(errno));
964 p = (MDB_page *)page;
966 if (!F_ISSET(p->mp_flags, P_META)) {
967 DPRINTF("page %lu not a meta page", p->mp_pgno);
972 if (m->mm_magic != MDB_MAGIC) {
973 DPRINTF("meta has invalid magic");
977 if (m->mm_version != MDB_VERSION) {
978 DPRINTF("database is version %u, expected version %u",
979 m->mm_version, MDB_VERSION);
980 return MDB_VERSION_MISMATCH;
983 memcpy(meta, m, sizeof(*m));
988 mdbenv_init_meta(MDB_env *env, MDB_meta *meta)
995 DPRINTF("writing new meta page");
996 psize = sysconf(_SC_PAGE_SIZE);
998 meta->mm_magic = MDB_MAGIC;
999 meta->mm_version = MDB_VERSION;
1000 meta->mm_psize = psize;
1001 meta->mm_last_pg = 1;
1002 meta->mm_flags = env->me_flags & 0xffff;
1003 meta->mm_flags |= MDB_INTEGERKEY;
1004 meta->mm_dbs[0].md_root = P_INVALID;
1005 meta->mm_dbs[1].md_root = P_INVALID;
1007 p = calloc(2, psize);
1009 p->mp_flags = P_META;
1012 memcpy(m, meta, sizeof(*meta));
1014 q = (MDB_page *)((char *)p + psize);
1017 q->mp_flags = P_META;
1020 memcpy(m, meta, sizeof(*meta));
1022 rc = write(env->me_fd, p, psize * 2);
1024 return (rc == (int)psize * 2) ? MDB_SUCCESS : errno;
1028 mdbenv_write_meta(MDB_txn *txn)
1036 assert(txn != NULL);
1037 assert(txn->mt_env != NULL);
1039 DPRINTF("writing meta page %d for root page %lu",
1040 !F_ISSET(txn->mt_flags, MDB_TXN_METOGGLE), txn->mt_dbs[MAIN_DBI].md_root);
1044 ptr = (char *)&meta;
1045 off = offsetof(MDB_meta, mm_dbs[0].md_depth);
1046 len = sizeof(MDB_meta) - off;
1049 meta.mm_dbs[0] = txn->mt_dbs[0];
1050 meta.mm_dbs[1] = txn->mt_dbs[1];
1051 meta.mm_last_pg = txn->mt_next_pgno - 1;
1052 meta.mm_txnid = txn->mt_txnid;
1054 if (!F_ISSET(txn->mt_flags, MDB_TXN_METOGGLE))
1055 off += env->me_psize;
1058 lseek(env->me_fd, off, SEEK_SET);
1059 rc = write(env->me_fd, ptr, len);
1061 DPRINTF("write failed, disk error?");
1069 mdbenv_read_meta(MDB_env *env, int *which)
1073 assert(env != NULL);
1075 if (env->me_metas[0]->mm_txnid < env->me_metas[1]->mm_txnid)
1078 if (env->me_meta != env->me_metas[toggle])
1079 env->me_meta = env->me_metas[toggle];
1083 DPRINTF("Using meta page %d", toggle);
1089 mdbenv_create(MDB_env **env)
1093 e = calloc(1, sizeof(MDB_env));
1094 if (!e) return ENOMEM;
1096 e->me_maxreaders = DEFAULT_READERS;
1105 mdbenv_set_mapsize(MDB_env *env, size_t size)
1109 env->me_mapsize = size;
1114 mdbenv_set_maxdbs(MDB_env *env, int dbs)
1116 env->me_maxdbs = dbs;
1121 mdbenv_set_maxreaders(MDB_env *env, int readers)
1123 env->me_maxreaders = readers;
1128 mdbenv_get_maxreaders(MDB_env *env, int *readers)
1130 if (!env || !readers)
1132 *readers = env->me_maxreaders;
1137 mdbenv_open2(MDB_env *env, unsigned int flags)
1143 env->me_flags = flags;
1145 memset(&meta, 0, sizeof(meta));
1147 if ((i = mdbenv_read_header(env, &meta)) != 0) {
1150 DPRINTF("new mdbenv");
1154 if (!env->me_mapsize) {
1155 env->me_mapsize = newenv ? DEFAULT_MAPSIZE : meta.mm_mapsize;
1159 if (meta.mm_address && (flags & MDB_FIXEDMAP))
1161 env->me_map = mmap(meta.mm_address, env->me_mapsize, PROT_READ, i,
1163 if (env->me_map == MAP_FAILED)
1167 meta.mm_mapsize = env->me_mapsize;
1168 if (flags & MDB_FIXEDMAP)
1169 meta.mm_address = env->me_map;
1170 i = mdbenv_init_meta(env, &meta);
1171 if (i != MDB_SUCCESS) {
1172 munmap(env->me_map, env->me_mapsize);
1176 env->me_psize = meta.mm_psize;
1178 p = (MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)(MDB_page *)env->me_map;
1179 env->me_metas[0] = METADATA(p);
1180 env->me_metas[1] = (MDB_meta *)((char *)env->me_metas[0] + meta.mm_psize);
1182 if ((i = mdbenv_read_meta(env, NULL)) != 0)
1185 DPRINTF("opened database version %u, pagesize %u",
1186 env->me_meta->mm_version, env->me_psize);
1187 DPRINTF("depth: %u", env->me_meta->mm_dbs[MAIN_DBI].md_depth);
1188 DPRINTF("entries: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_entries);
1189 DPRINTF("branch pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_branch_pages);
1190 DPRINTF("leaf pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_leaf_pages);
1191 DPRINTF("overflow pages: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_overflow_pages);
1192 DPRINTF("root: %lu", env->me_meta->mm_dbs[MAIN_DBI].md_root);
1198 mdbenv_reader_dest(void *ptr)
1200 MDB_reader *reader = ptr;
1202 reader->mr_txnid = 0;
1207 /* downgrade the exclusive lock on the region back to shared */
1209 mdbenv_share_locks(MDB_env *env)
1211 struct flock lock_info;
1213 env->me_txns->mt_txnid = env->me_meta->mm_txnid;
1215 memset((void *)&lock_info, 0, sizeof(lock_info));
1216 lock_info.l_type = F_RDLCK;
1217 lock_info.l_whence = SEEK_SET;
1218 lock_info.l_start = 0;
1219 lock_info.l_len = 1;
1220 fcntl(env->me_lfd, F_SETLK, &lock_info);
1224 mdbenv_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
1228 struct flock lock_info;
1232 if ((env->me_lfd = open(lpath, O_RDWR|O_CREAT, mode)) == -1) {
1236 /* Try to get exclusive lock. If we succeed, then
1237 * nobody is using the lock region and we should initialize it.
1239 memset((void *)&lock_info, 0, sizeof(lock_info));
1240 lock_info.l_type = F_WRLCK;
1241 lock_info.l_whence = SEEK_SET;
1242 lock_info.l_start = 0;
1243 lock_info.l_len = 1;
1244 rc = fcntl(env->me_lfd, F_SETLK, &lock_info);
1248 lock_info.l_type = F_RDLCK;
1249 rc = fcntl(env->me_lfd, F_SETLK, &lock_info);
1255 size = lseek(env->me_lfd, 0, SEEK_END);
1256 rsize = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
1257 if (size < rsize && *excl) {
1258 if (ftruncate(env->me_lfd, rsize) != 0) {
1264 size = rsize - sizeof(MDB_txninfo);
1265 env->me_maxreaders = size/sizeof(MDB_reader) + 1;
1267 env->me_txns = mmap(0, rsize, PROT_READ|PROT_WRITE, MAP_SHARED,
1269 if (env->me_txns == MAP_FAILED) {
1274 pthread_mutexattr_t mattr;
1276 pthread_mutexattr_init(&mattr);
1277 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
1278 pthread_mutex_init(&env->me_txns->mt_mutex, &mattr);
1279 pthread_mutex_init(&env->me_txns->mt_wmutex, &mattr);
1280 env->me_txns->mt_version = MDB_VERSION;
1281 env->me_txns->mt_magic = MDB_MAGIC;
1282 env->me_txns->mt_txnid = 0;
1283 env->me_txns->mt_numreaders = 0;
1286 if (env->me_txns->mt_magic != MDB_MAGIC) {
1287 DPRINTF("lock region has invalid magic");
1291 if (env->me_txns->mt_version != MDB_VERSION) {
1292 DPRINTF("lock region is version %u, expected version %u",
1293 env->me_txns->mt_version, MDB_VERSION);
1294 rc = MDB_VERSION_MISMATCH;
1297 if (errno != EACCES && errno != EAGAIN) {
1310 #define LOCKNAME "/lock.mdb"
1311 #define DATANAME "/data.mdb"
1313 mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
1315 int oflags, rc, len, excl;
1316 char *lpath, *dpath;
1319 lpath = malloc(len + sizeof(LOCKNAME) + len + sizeof(DATANAME));
1322 dpath = lpath + len + sizeof(LOCKNAME);
1323 sprintf(lpath, "%s" LOCKNAME, path);
1324 sprintf(dpath, "%s" DATANAME, path);
1326 rc = mdbenv_setup_locks(env, lpath, mode, &excl);
1330 if (F_ISSET(flags, MDB_RDONLY))
1333 oflags = O_RDWR | O_CREAT;
1335 if ((env->me_fd = open(dpath, oflags, mode)) == -1)
1338 if ((rc = mdbenv_open2(env, flags)) != MDB_SUCCESS) {
1342 env->me_path = strdup(path);
1343 DPRINTF("opened dbenv %p", (void *) env);
1344 pthread_key_create(&env->me_txkey, mdbenv_reader_dest);
1346 mdbenv_share_locks(env);
1347 env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
1348 env->me_dbs[0] = calloc(env->me_maxdbs, sizeof(MDB_db));
1349 env->me_dbs[1] = calloc(env->me_maxdbs, sizeof(MDB_db));
1359 mdbenv_close(MDB_env *env)
1364 free(env->me_dbs[1]);
1365 free(env->me_dbs[0]);
1370 munmap(env->me_map, env->me_mapsize);
1374 size_t size = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
1375 munmap(env->me_txns, size);
1381 /* Search for key within a leaf page, using binary search.
1382 * Returns the smallest entry larger or equal to the key.
1383 * If exactp is non-null, stores whether the found entry was an exact match
1384 * in *exactp (1 or 0).
1385 * If kip is non-null, stores the index of the found entry in *kip.
1386 * If no entry larger or equal to the key is found, returns NULL.
1389 mdb_search_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp, MDB_val *key,
1390 int *exactp, unsigned int *kip)
1398 DPRINTF("searching %u keys in %s page %lu",
1400 IS_LEAF(mp) ? "leaf" : "branch",
1403 assert(NUMKEYS(mp) > 0);
1405 memset(&nodekey, 0, sizeof(nodekey));
1407 low = IS_LEAF(mp) ? 0 : 1;
1408 high = NUMKEYS(mp) - 1;
1409 while (low <= high) {
1410 i = (low + high) >> 1;
1411 node = NODEPTR(mp, i);
1413 nodekey.mv_size = node->mn_ksize;
1414 nodekey.mv_data = NODEKEY(node);
1416 if (txn->mt_dbxs[dbi].md_cmp)
1417 rc = txn->mt_dbxs[dbi].md_cmp(key, &nodekey);
1419 rc = _mdb_cmp(txn, dbi, key, &nodekey);
1422 DPRINTF("found leaf index %u [%.*s], rc = %i",
1423 i, (int)nodekey.mv_size, (char *)nodekey.mv_data, rc);
1425 DPRINTF("found branch index %u [%.*s -> %lu], rc = %i",
1426 i, (int)node->mn_ksize, (char *)NODEKEY(node),
1437 if (rc > 0) { /* Found entry is less than the key. */
1438 i++; /* Skip to get the smallest entry larger than key. */
1439 if (i >= NUMKEYS(mp))
1440 /* There is no entry larger or equal to the key. */
1444 *exactp = (rc == 0);
1445 if (kip) /* Store the key index if requested. */
1448 return NODEPTR(mp, i);
1452 cursor_pop_page(MDB_cursor *cursor)
1456 top = CURSOR_TOP(cursor);
1459 DPRINTF("popped page %lu off cursor %p", top->mp_page->mp_pgno, (void *) cursor);
1465 cursor_push_page(MDB_cursor *cursor, MDB_page *mp)
1469 DPRINTF("pushing page %lu on cursor %p", mp->mp_pgno, (void *) cursor);
1471 if ((ppage = calloc(1, sizeof(MDB_ppage))) == NULL)
1473 ppage->mp_page = mp;
1474 CURSOR_PUSH(cursor, ppage);
1479 mdb_get_page(MDB_txn *txn, pgno_t pgno)
1484 if (!F_ISSET(txn->mt_flags, MDB_TXN_RDONLY) && !STAILQ_EMPTY(txn->mt_u.dirty_queue)) {
1486 STAILQ_FOREACH(dp, txn->mt_u.dirty_queue, h.md_next) {
1487 if (dp->p.mp_pgno == pgno) {
1495 if (pgno > txn->mt_env->me_meta->mm_last_pg)
1497 p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
1503 mdb_search_page_root(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
1504 MDB_cursor *cursor, int modify, MDB_pageparent *mpp)
1506 MDB_page *mp = mpp->mp_page;
1509 if (cursor && cursor_push_page(cursor, mp) == NULL)
1512 while (IS_BRANCH(mp)) {
1516 DPRINTF("branch page %lu has %u keys", mp->mp_pgno, NUMKEYS(mp));
1517 assert(NUMKEYS(mp) > 1);
1518 DPRINTF("found index 0 to page %lu", NODEPGNO(NODEPTR(mp, 0)));
1520 if (key == NULL) /* Initialize cursor to first page. */
1522 else if (key->mv_size > MAXKEYSIZE && key->mv_data == NULL) {
1523 /* cursor to last page */
1527 node = mdb_search_node(txn, dbi, mp, key, &exact, &i);
1529 i = NUMKEYS(mp) - 1;
1537 DPRINTF("following index %u for key %.*s",
1538 i, (int)key->mv_size, (char *)key->mv_data);
1539 assert(i < NUMKEYS(mp));
1540 node = NODEPTR(mp, i);
1543 CURSOR_TOP(cursor)->mp_ki = i;
1545 mpp->mp_parent = mp;
1546 if ((mp = mdb_get_page(txn, NODEPGNO(node))) == NULL)
1551 if (cursor && cursor_push_page(cursor, mp) == NULL)
1555 MDB_dhead *dh = ((MDB_dhead *)mp)-1;
1556 if ((rc = mdb_touch(txn, mpp)) != 0)
1558 dh = ((MDB_dhead *)mpp->mp_page)-1;
1559 dh->md_parent = mpp->mp_parent;
1560 dh->md_pi = mpp->mp_pi;
1567 DPRINTF("internal error, index points to a %02X page!?",
1572 DPRINTF("found leaf page %lu for key %.*s", mp->mp_pgno,
1573 key ? (int)key->mv_size : 0, key ? (char *)key->mv_data : NULL);
1578 /* Search for the page a given key should be in.
1579 * Stores a pointer to the found page in *mpp.
1580 * If key is NULL, search for the lowest page (used by mdb_cursor_first).
1581 * If cursor is non-null, pushes parent pages on the cursor stack.
1582 * If modify is true, visited pages are updated with new page numbers.
1585 mdb_search_page(MDB_txn *txn, MDB_dbi dbi, MDB_val *key,
1586 MDB_cursor *cursor, int modify, MDB_pageparent *mpp)
1591 /* Choose which root page to start with. If a transaction is given
1592 * use the root page from the transaction, otherwise read the last
1593 * committed root page.
1595 if (F_ISSET(txn->mt_flags, MDB_TXN_ERROR)) {
1596 DPRINTF("transaction has failed, must abort");
1599 root = txn->mt_dbs[dbi].md_root;
1601 if (root == P_INVALID) { /* Tree is empty. */
1602 DPRINTF("tree is empty");
1603 return MDB_NOTFOUND;
1606 if ((mpp->mp_page = mdb_get_page(txn, root)) == NULL)
1609 DPRINTF("root page has flags 0x%X", mpp->mp_page->mp_flags);
1612 /* For sub-databases, update main root first */
1613 if (dbi > MAIN_DBI && !txn->mt_dbxs[dbi].md_dirty) {
1615 rc = mdb_search_page(txn, MAIN_DBI, &txn->mt_dbxs[dbi].md_name,
1619 txn->mt_dbxs[dbi].md_dirty = 1;
1621 if (!F_ISSET(mpp->mp_page->mp_flags, P_DIRTY)) {
1622 mpp->mp_parent = NULL;
1624 if ((rc = mdb_touch(txn, mpp)))
1626 txn->mt_dbs[dbi].md_root = mpp->mp_page->mp_pgno;
1630 return mdb_search_page_root(txn, dbi, key, cursor, modify, mpp);
1634 mdb_read_data(MDB_txn *txn, MDB_node *leaf, MDB_val *data)
1636 MDB_page *omp; /* overflow mpage */
1639 if (!F_ISSET(leaf->mn_flags, F_BIGDATA)) {
1640 data->mv_size = leaf->mn_dsize;
1641 data->mv_data = NODEDATA(leaf);
1645 /* Read overflow data.
1647 data->mv_size = leaf->mn_dsize;
1648 memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
1649 if ((omp = mdb_get_page(txn, pgno)) == NULL) {
1650 DPRINTF("read overflow page %lu failed", pgno);
1653 data->mv_data = omp;
1659 mdb_get(MDB_txn *txn, MDB_dbi dbi,
1660 MDB_val *key, MDB_val *data)
1668 DPRINTF("===> get key [%.*s]", (int)key->mv_size, (char *)key->mv_data);
1670 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
1673 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
1677 if ((rc = mdb_search_page(txn, dbi, key, NULL, 0, &mpp)) != MDB_SUCCESS)
1680 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, NULL);
1681 if (leaf && exact) {
1682 /* Return first duplicate data item */
1683 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
1686 mdb_xcursor_init0(txn, dbi, &mx);
1687 mdb_xcursor_init1(txn, dbi, &mx, leaf);
1688 rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi, NULL, NULL, 0, &mpp);
1689 if (rc != MDB_SUCCESS)
1691 leaf = NODEPTR(mpp.mp_page, 0);
1693 rc = mdb_read_data(txn, leaf, data);
1702 mdb_sibling(MDB_cursor *cursor, int move_right)
1706 MDB_ppage *parent, *top;
1709 top = CURSOR_TOP(cursor);
1710 if ((parent = SLIST_NEXT(top, mp_entry)) == NULL) {
1711 return MDB_NOTFOUND; /* root has no siblings */
1714 DPRINTF("parent page is page %lu, index %u",
1715 parent->mp_page->mp_pgno, parent->mp_ki);
1717 cursor_pop_page(cursor);
1718 if (move_right ? (parent->mp_ki + 1 >= NUMKEYS(parent->mp_page))
1719 : (parent->mp_ki == 0)) {
1720 DPRINTF("no more keys left, moving to %s sibling",
1721 move_right ? "right" : "left");
1722 if ((rc = mdb_sibling(cursor, move_right)) != MDB_SUCCESS)
1724 parent = CURSOR_TOP(cursor);
1730 DPRINTF("just moving to %s index key %u",
1731 move_right ? "right" : "left", parent->mp_ki);
1733 assert(IS_BRANCH(parent->mp_page));
1735 indx = NODEPTR(parent->mp_page, parent->mp_ki);
1736 if ((mp = mdb_get_page(cursor->mc_txn, indx->mn_pgno)) == NULL)
1739 mp->parent = parent->mp_page;
1740 mp->parent_index = parent->mp_ki;
1743 cursor_push_page(cursor, mp);
1749 mdb_set_key(MDB_node *node, MDB_val *key)
1754 key->mv_size = node->mn_ksize;
1755 key->mv_data = NODEKEY(node);
1761 mdb_cursor_next(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
1768 if (cursor->mc_eof) {
1769 return MDB_NOTFOUND;
1772 assert(cursor->mc_initialized);
1774 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1775 if (op == MDB_NEXT || op == MDB_NEXT_DUP) {
1776 rc = mdb_cursor_next(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_NEXT);
1777 if (op != MDB_NEXT || rc == MDB_SUCCESS)
1782 top = CURSOR_TOP(cursor);
1785 DPRINTF("cursor_next: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
1787 if (top->mp_ki + 1 >= NUMKEYS(mp)) {
1788 DPRINTF("=====> move to next sibling page");
1789 if (mdb_sibling(cursor, 1) != MDB_SUCCESS) {
1791 return MDB_NOTFOUND;
1793 top = CURSOR_TOP(cursor);
1795 DPRINTF("next page is %lu, key index %u", mp->mp_pgno, top->mp_ki);
1799 DPRINTF("==> cursor points to page %lu with %u keys, key index %u",
1800 mp->mp_pgno, NUMKEYS(mp), top->mp_ki);
1802 assert(IS_LEAF(mp));
1803 leaf = NODEPTR(mp, top->mp_ki);
1806 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
1809 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1810 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
1811 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1812 if (rc != MDB_SUCCESS)
1817 return mdb_set_key(leaf, key);
1821 mdb_cursor_prev(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
1828 assert(cursor->mc_initialized);
1830 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1831 if (op == MDB_PREV || op == MDB_PREV_DUP) {
1832 rc = mdb_cursor_prev(&cursor->mc_xcursor->mx_cursor, data, NULL, MDB_PREV);
1833 if (op != MDB_PREV || rc == MDB_SUCCESS)
1838 top = CURSOR_TOP(cursor);
1841 DPRINTF("cursor_prev: top page is %lu in cursor %p", mp->mp_pgno, (void *) cursor);
1843 if (top->mp_ki == 0) {
1844 DPRINTF("=====> move to prev sibling page");
1845 if (mdb_sibling(cursor, 0) != MDB_SUCCESS) {
1846 return MDB_NOTFOUND;
1848 top = CURSOR_TOP(cursor);
1850 top->mp_ki = NUMKEYS(mp) - 1;
1851 DPRINTF("prev page is %lu, key index %u", mp->mp_pgno, top->mp_ki);
1857 DPRINTF("==> cursor points to page %lu with %u keys, key index %u",
1858 mp->mp_pgno, NUMKEYS(mp), top->mp_ki);
1860 assert(IS_LEAF(mp));
1861 leaf = NODEPTR(mp, top->mp_ki);
1864 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data) != MDB_SUCCESS))
1867 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1868 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
1869 rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
1870 if (rc != MDB_SUCCESS)
1875 return mdb_set_key(leaf, key);
1879 mdb_cursor_set(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1880 MDB_cursor_op op, int *exactp)
1889 assert(key->mv_size > 0);
1891 while (CURSOR_TOP(cursor) != NULL)
1892 cursor_pop_page(cursor);
1894 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, key, cursor, 0, &mpp);
1895 if (rc != MDB_SUCCESS)
1897 assert(IS_LEAF(mpp.mp_page));
1899 top = CURSOR_TOP(cursor);
1900 leaf = mdb_search_node(cursor->mc_txn, cursor->mc_dbi, mpp.mp_page, key, exactp, &top->mp_ki);
1901 if (exactp != NULL && !*exactp) {
1902 /* MDB_SET specified and not an exact match. */
1903 return MDB_NOTFOUND;
1907 DPRINTF("===> inexact leaf not found, goto sibling");
1908 if ((rc = mdb_sibling(cursor, 1)) != MDB_SUCCESS)
1909 return rc; /* no entries matched */
1910 top = CURSOR_TOP(cursor);
1912 mpp.mp_page = top->mp_page;
1913 assert(IS_LEAF(mpp.mp_page));
1914 leaf = NODEPTR(mpp.mp_page, 0);
1917 cursor->mc_initialized = 1;
1921 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
1924 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1925 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
1926 if (op == MDB_SET || op == MDB_SET_RANGE) {
1927 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1931 if (op == MDB_GET_BOTH) {
1936 op2 = MDB_SET_RANGE;
1938 rc = mdb_cursor_set(&cursor->mc_xcursor->mx_cursor, data, NULL, op2, ex2p);
1939 if (rc != MDB_SUCCESS)
1945 rc = mdb_set_key(leaf, key);
1946 if (rc == MDB_SUCCESS) {
1947 DPRINTF("==> cursor placed on key %.*s",
1948 (int)key->mv_size, (char *)key->mv_data);
1956 mdb_cursor_first(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
1962 while (CURSOR_TOP(cursor) != NULL)
1963 cursor_pop_page(cursor);
1965 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, NULL, cursor, 0, &mpp);
1966 if (rc != MDB_SUCCESS)
1968 assert(IS_LEAF(mpp.mp_page));
1970 leaf = NODEPTR(mpp.mp_page, 0);
1971 cursor->mc_initialized = 1;
1975 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
1978 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
1979 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
1980 rc = mdb_cursor_first(&cursor->mc_xcursor->mx_cursor, data, NULL);
1985 return mdb_set_key(leaf, key);
1989 mdb_cursor_last(MDB_cursor *cursor, MDB_val *key, MDB_val *data)
1997 while (CURSOR_TOP(cursor) != NULL)
1998 cursor_pop_page(cursor);
2000 lkey.mv_size = MAXKEYSIZE+1;
2001 lkey.mv_data = NULL;
2003 rc = mdb_search_page(cursor->mc_txn, cursor->mc_dbi, &lkey, cursor, 0, &mpp);
2004 if (rc != MDB_SUCCESS)
2006 assert(IS_LEAF(mpp.mp_page));
2008 leaf = NODEPTR(mpp.mp_page, NUMKEYS(mpp.mp_page)-1);
2009 cursor->mc_initialized = 1;
2012 top = CURSOR_TOP(cursor);
2013 top->mp_ki = NUMKEYS(top->mp_page) - 1;
2016 if ((rc = mdb_read_data(cursor->mc_txn, leaf, data)) != MDB_SUCCESS)
2019 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
2020 mdb_xcursor_init1(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor, leaf);
2021 rc = mdb_cursor_last(&cursor->mc_xcursor->mx_cursor, data, NULL);
2027 return mdb_set_key(leaf, key);
2031 mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
2041 case MDB_GET_BOTH_RANGE:
2049 if (key == NULL || key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
2051 } else if (op != MDB_SET_RANGE)
2052 rc = mdb_cursor_set(cursor, key, data, op, NULL);
2054 rc = mdb_cursor_set(cursor, key, data, op, &exact);
2058 case MDB_NEXT_NODUP:
2059 if (!cursor->mc_initialized)
2060 rc = mdb_cursor_first(cursor, key, data);
2062 rc = mdb_cursor_next(cursor, key, data, op);
2066 case MDB_PREV_NODUP:
2067 if (!cursor->mc_initialized || cursor->mc_eof)
2068 rc = mdb_cursor_last(cursor, key, data);
2070 rc = mdb_cursor_prev(cursor, key, data, op);
2073 rc = mdb_cursor_first(cursor, key, data);
2076 rc = mdb_cursor_last(cursor, key, data);
2079 DPRINTF("unhandled/unimplemented cursor operation %u", op);
2087 /* Allocate a page and initialize it
2090 mdb_new_page(MDB_txn *txn, MDB_dbi dbi, uint32_t flags, int num)
2094 if ((dp = mdb_alloc_page(txn, NULL, 0, num)) == NULL)
2096 DPRINTF("allocated new mpage %lu, page size %u",
2097 dp->p.mp_pgno, txn->mt_env->me_psize);
2098 dp->p.mp_flags = flags | P_DIRTY;
2099 dp->p.mp_lower = PAGEHDRSZ;
2100 dp->p.mp_upper = txn->mt_env->me_psize;
2102 if (IS_BRANCH(&dp->p))
2103 txn->mt_dbs[dbi].md_branch_pages++;
2104 else if (IS_LEAF(&dp->p))
2105 txn->mt_dbs[dbi].md_leaf_pages++;
2106 else if (IS_OVERFLOW(&dp->p)) {
2107 txn->mt_dbs[dbi].md_overflow_pages += num;
2108 dp->p.mp_pages = num;
2115 mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data)
2119 sz = LEAFSIZE(key, data);
2120 if (data->mv_size >= env->me_psize / MDB_MINKEYS) {
2121 /* put on overflow page */
2122 sz -= data->mv_size - sizeof(pgno_t);
2125 return sz + sizeof(indx_t);
2129 mdb_branch_size(MDB_env *env, MDB_val *key)
2134 if (sz >= env->me_psize / MDB_MINKEYS) {
2135 /* put on overflow page */
2136 /* not implemented */
2137 /* sz -= key->size - sizeof(pgno_t); */
2140 return sz + sizeof(indx_t);
2144 mdb_add_node(MDB_txn *txn, MDB_dbi dbi, MDB_page *mp, indx_t indx,
2145 MDB_val *key, MDB_val *data, pgno_t pgno, uint8_t flags)
2148 size_t node_size = NODESIZE;
2151 MDB_dpage *ofp = NULL; /* overflow page */
2153 assert(mp->mp_upper >= mp->mp_lower);
2155 DPRINTF("add node [%.*s] to %s page %lu at index %i, key size %zu",
2156 key ? (int)key->mv_size : 0, key ? (char *)key->mv_data : NULL,
2157 IS_LEAF(mp) ? "leaf" : "branch",
2158 mp->mp_pgno, indx, key ? key->mv_size : 0);
2161 node_size += key->mv_size;
2165 if (F_ISSET(flags, F_BIGDATA)) {
2166 /* Data already on overflow page. */
2167 node_size += sizeof(pgno_t);
2168 } else if (data->mv_size >= txn->mt_env->me_psize / MDB_MINKEYS) {
2169 int ovpages = OVPAGES(data->mv_size, txn->mt_env->me_psize);
2170 /* Put data on overflow page. */
2171 DPRINTF("data size is %zu, put on overflow page",
2173 node_size += sizeof(pgno_t);
2174 if ((ofp = mdb_new_page(txn, dbi, P_OVERFLOW, ovpages)) == NULL)
2176 DPRINTF("allocated overflow page %lu", ofp->p.mp_pgno);
2179 node_size += data->mv_size;
2183 if (node_size + sizeof(indx_t) > SIZELEFT(mp)) {
2184 DPRINTF("not enough room in page %lu, got %u ptrs",
2185 mp->mp_pgno, NUMKEYS(mp));
2186 DPRINTF("upper - lower = %u - %u = %u", mp->mp_upper, mp->mp_lower,
2187 mp->mp_upper - mp->mp_lower);
2188 DPRINTF("node size = %zu", node_size);
2192 /* Move higher pointers up one slot. */
2193 for (i = NUMKEYS(mp); i > indx; i--)
2194 mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];
2196 /* Adjust free space offsets. */
2197 ofs = mp->mp_upper - node_size;
2198 assert(ofs >= mp->mp_lower + sizeof(indx_t));
2199 mp->mp_ptrs[indx] = ofs;
2201 mp->mp_lower += sizeof(indx_t);
2203 /* Write the node data. */
2204 node = NODEPTR(mp, indx);
2205 node->mn_ksize = (key == NULL) ? 0 : key->mv_size;
2206 node->mn_flags = flags;
2208 node->mn_dsize = data->mv_size;
2210 node->mn_pgno = pgno;
2213 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
2218 if (F_ISSET(flags, F_BIGDATA))
2219 memcpy(node->mn_data + key->mv_size, data->mv_data,
2222 memcpy(node->mn_data + key->mv_size, data->mv_data,
2225 memcpy(node->mn_data + key->mv_size, &ofp->p.mp_pgno,
2227 memcpy(METADATA(&ofp->p), data->mv_data, data->mv_size);
2235 mdb_del_node(MDB_page *mp, indx_t indx)
2238 indx_t i, j, numkeys, ptr;
2242 DPRINTF("delete node %u on %s page %lu", indx,
2243 IS_LEAF(mp) ? "leaf" : "branch", mp->mp_pgno);
2244 assert(indx < NUMKEYS(mp));
2246 node = NODEPTR(mp, indx);
2247 sz = NODESIZE + node->mn_ksize;
2249 if (F_ISSET(node->mn_flags, F_BIGDATA))
2250 sz += sizeof(pgno_t);
2252 sz += NODEDSZ(node);
2255 ptr = mp->mp_ptrs[indx];
2256 numkeys = NUMKEYS(mp);
2257 for (i = j = 0; i < numkeys; i++) {
2259 mp->mp_ptrs[j] = mp->mp_ptrs[i];
2260 if (mp->mp_ptrs[i] < ptr)
2261 mp->mp_ptrs[j] += sz;
2266 base = (char *)mp + mp->mp_upper;
2267 memmove(base + sz, base, ptr - mp->mp_upper);
2269 mp->mp_lower -= sizeof(indx_t);
2274 mdb_xcursor_init0(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
2279 mx->mx_txn.mt_dbxs = mx->mx_dbxs;
2280 mx->mx_txn.mt_dbs = mx->mx_dbs;
2281 mx->mx_dbxs[0] = txn->mt_dbxs[0];
2282 mx->mx_dbxs[1] = txn->mt_dbxs[1];
2284 mx->mx_dbxs[2] = txn->mt_dbxs[dbi];
2289 mx->mx_dbxs[dbn+1].md_parent = dbn;
2290 mx->mx_dbxs[dbn+1].md_cmp = mx->mx_dbxs[dbn].md_dcmp;
2291 mx->mx_dbxs[dbn+1].md_rel = mx->mx_dbxs[dbn].md_rel;
2292 mx->mx_dbxs[dbn+1].md_dirty = 0;
2293 mx->mx_txn.mt_numdbs = dbn+2;
2295 SLIST_INIT(&mx->mx_cursor.mc_stack);
2296 mx->mx_cursor.mc_txn = &mx->mx_txn;
2297 mx->mx_cursor.mc_dbi = dbn+1;
2301 mdb_xcursor_init1(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx, MDB_node *node)
2303 MDB_db *db = NODEDATA(node);
2305 mx->mx_dbs[0] = txn->mt_dbs[0];
2306 mx->mx_dbs[1] = txn->mt_dbs[1];
2308 mx->mx_dbs[2] = txn->mt_dbs[dbi];
2313 mx->mx_dbs[dbn] = *db;
2314 mx->mx_dbxs[dbn].md_name.mv_data = NODEKEY(node);
2315 mx->mx_dbxs[dbn].md_name.mv_size = node->mn_ksize;
2316 mx->mx_txn.mt_next_pgno = txn->mt_next_pgno;
2317 mx->mx_txn.mt_oldest = txn->mt_oldest;
2318 mx->mx_txn.mt_u = txn->mt_u;
2322 mdb_xcursor_fini(MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
2324 txn->mt_next_pgno = mx->mx_txn.mt_next_pgno;
2325 txn->mt_oldest = mx->mx_txn.mt_oldest;
2326 txn->mt_u = mx->mx_txn.mt_u;
2327 txn->mt_dbs[0] = mx->mx_dbs[0];
2328 txn->mt_dbs[1] = mx->mx_dbs[1];
2329 txn->mt_dbxs[0].md_dirty = mx->mx_dbxs[0].md_dirty;
2330 txn->mt_dbxs[1].md_dirty = mx->mx_dbxs[1].md_dirty;
2332 txn->mt_dbs[dbi] = mx->mx_dbs[2];
2333 txn->mt_dbxs[dbi].md_dirty = mx->mx_dbxs[2].md_dirty;
2338 mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
2341 size_t size = sizeof(MDB_cursor);
2343 if (txn == NULL || ret == NULL || !dbi || dbi >= txn->mt_numdbs)
2346 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT)
2347 size += sizeof(MDB_xcursor);
2349 if ((cursor = calloc(1, size)) != NULL) {
2350 SLIST_INIT(&cursor->mc_stack);
2351 cursor->mc_dbi = dbi;
2352 cursor->mc_txn = txn;
2353 if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
2354 MDB_xcursor *mx = (MDB_xcursor *)(cursor + 1);
2355 cursor->mc_xcursor = mx;
2356 mdb_xcursor_init0(txn, dbi, mx);
2367 /* Return the count of duplicate data items for the current key */
2369 mdb_cursor_count(MDB_cursor *mc, unsigned long *countp)
2371 if (mc == NULL || countp == NULL)
2374 if (!(mc->mc_txn->mt_dbs[mc->mc_dbi].md_flags & MDB_DUPSORT))
2377 if (!mc->mc_xcursor->mx_cursor.mc_initialized)
2380 *countp = mc->mc_xcursor->mx_txn.mt_dbs[mc->mc_xcursor->mx_cursor.mc_dbi].md_entries;
2385 mdb_cursor_close(MDB_cursor *cursor)
2387 if (cursor != NULL) {
2388 while(!CURSOR_EMPTY(cursor))
2389 cursor_pop_page(cursor);
2390 if (cursor->mc_txn->mt_dbs[cursor->mc_dbi].md_flags & MDB_DUPSORT) {
2391 mdb_xcursor_fini(cursor->mc_txn, cursor->mc_dbi, cursor->mc_xcursor);
2392 while(!CURSOR_EMPTY(&cursor->mc_xcursor->mx_cursor))
2393 cursor_pop_page(&cursor->mc_xcursor->mx_cursor);
2401 mdb_update_key(MDB_page *mp, indx_t indx, MDB_val *key)
2403 indx_t ptr, i, numkeys;
2409 node = NODEPTR(mp, indx);
2410 ptr = mp->mp_ptrs[indx];
2411 DPRINTF("update key %u (ofs %u) [%.*s] to [%.*s] on page %lu",
2413 (int)node->mn_ksize, (char *)NODEKEY(node),
2414 (int)key->mv_size, (char *)key->mv_data,
2417 delta = key->mv_size - node->mn_ksize;
2419 if (delta > 0 && SIZELEFT(mp) < delta) {
2420 DPRINTF("OUCH! Not enough room, delta = %d", delta);
2424 numkeys = NUMKEYS(mp);
2425 for (i = 0; i < numkeys; i++) {
2426 if (mp->mp_ptrs[i] <= ptr)
2427 mp->mp_ptrs[i] -= delta;
2430 base = (char *)mp + mp->mp_upper;
2431 len = ptr - mp->mp_upper + NODESIZE;
2432 memmove(base - delta, base, len);
2433 mp->mp_upper -= delta;
2435 node = NODEPTR(mp, indx);
2436 node->mn_ksize = key->mv_size;
2439 memcpy(NODEKEY(node), key->mv_data, key->mv_size);
2444 /* Move a node from src to dst.
2447 mdb_move_node(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src, indx_t srcindx,
2448 MDB_pageparent *dst, indx_t dstindx)
2454 srcnode = NODEPTR(src->mp_page, srcindx);
2455 DPRINTF("moving %s node %u [%.*s] on page %lu to node %u on page %lu",
2456 IS_LEAF(src->mp_page) ? "leaf" : "branch",
2458 (int)srcnode->mn_ksize, (char *)NODEKEY(srcnode),
2459 src->mp_page->mp_pgno,
2460 dstindx, dst->mp_page->mp_pgno);
2462 /* Mark src and dst as dirty. */
2463 if ((rc = mdb_touch(txn, src)) ||
2464 (rc = mdb_touch(txn, dst)))
2467 /* Add the node to the destination page.
2469 key.mv_size = srcnode->mn_ksize;
2470 key.mv_data = NODEKEY(srcnode);
2471 data.mv_size = NODEDSZ(srcnode);
2472 data.mv_data = NODEDATA(srcnode);
2473 rc = mdb_add_node(txn, dbi, dst->mp_page, dstindx, &key, &data, NODEPGNO(srcnode),
2475 if (rc != MDB_SUCCESS)
2478 /* Delete the node from the source page.
2480 mdb_del_node(src->mp_page, srcindx);
2482 /* Update the parent separators.
2484 if (srcindx == 0 && src->mp_pi != 0) {
2485 DPRINTF("update separator for source page %lu to [%.*s]",
2486 src->mp_page->mp_pgno, (int)key.mv_size, (char *)key.mv_data);
2487 if ((rc = mdb_update_key(src->mp_parent, src->mp_pi,
2488 &key)) != MDB_SUCCESS)
2492 if (srcindx == 0 && IS_BRANCH(src->mp_page)) {
2494 nullkey.mv_size = 0;
2495 assert(mdb_update_key(src->mp_page, 0, &nullkey) == MDB_SUCCESS);
2498 if (dstindx == 0 && dst->mp_pi != 0) {
2499 DPRINTF("update separator for destination page %lu to [%.*s]",
2500 dst->mp_page->mp_pgno, (int)key.mv_size, (char *)key.mv_data);
2501 if ((rc = mdb_update_key(dst->mp_parent, dst->mp_pi,
2502 &key)) != MDB_SUCCESS)
2506 if (dstindx == 0 && IS_BRANCH(dst->mp_page)) {
2508 nullkey.mv_size = 0;
2509 assert(mdb_update_key(dst->mp_page, 0, &nullkey) == MDB_SUCCESS);
2516 mdb_merge(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *src, MDB_pageparent *dst)
2525 DPRINTF("merging page %lu and %lu", src->mp_page->mp_pgno, dst->mp_page->mp_pgno);
2527 assert(txn != NULL);
2528 assert(src->mp_parent); /* can't merge root page */
2529 assert(dst->mp_parent);
2531 /* Mark src and dst as dirty. */
2532 if ((rc = mdb_touch(txn, src)) ||
2533 (rc = mdb_touch(txn, dst)))
2536 /* Move all nodes from src to dst.
2538 for (i = 0; i < NUMKEYS(src->mp_page); i++) {
2539 srcnode = NODEPTR(src->mp_page, i);
2541 key.mv_size = srcnode->mn_ksize;
2542 key.mv_data = NODEKEY(srcnode);
2543 data.mv_size = NODEDSZ(srcnode);
2544 data.mv_data = NODEDATA(srcnode);
2545 rc = mdb_add_node(txn, dbi, dst->mp_page, NUMKEYS(dst->mp_page), &key,
2546 &data, NODEPGNO(srcnode), srcnode->mn_flags);
2547 if (rc != MDB_SUCCESS)
2551 DPRINTF("dst page %lu now has %u keys (%.1f%% filled)",
2552 dst->mp_page->mp_pgno, NUMKEYS(dst->mp_page), (float)PAGEFILL(txn->mt_env, dst->mp_page) / 10);
2554 /* Unlink the src page from parent.
2556 mdb_del_node(src->mp_parent, src->mp_pi);
2557 if (src->mp_pi == 0) {
2559 if ((rc = mdb_update_key(src->mp_parent, 0, &key)) != MDB_SUCCESS)
2563 if (IS_LEAF(src->mp_page))
2564 txn->mt_dbs[dbi].md_leaf_pages--;
2566 txn->mt_dbs[dbi].md_branch_pages--;
2568 mpp.mp_page = src->mp_parent;
2569 dh = (MDB_dhead *)src->mp_parent;
2571 mpp.mp_parent = dh->md_parent;
2572 mpp.mp_pi = dh->md_pi;
2574 return mdb_rebalance(txn, dbi, &mpp);
2577 #define FILL_THRESHOLD 250
2580 mdb_rebalance(MDB_txn *txn, MDB_dbi dbi, MDB_pageparent *mpp)
2585 indx_t si = 0, di = 0;
2587 assert(txn != NULL);
2588 assert(mpp != NULL);
2590 DPRINTF("rebalancing %s page %lu (has %u keys, %.1f%% full)",
2591 IS_LEAF(mpp->mp_page) ? "leaf" : "branch",
2592 mpp->mp_page->mp_pgno, NUMKEYS(mpp->mp_page), (float)PAGEFILL(txn->mt_env, mpp->mp_page) / 10);
2594 if (PAGEFILL(txn->mt_env, mpp->mp_page) >= FILL_THRESHOLD) {
2595 DPRINTF("no need to rebalance page %lu, above fill threshold",
2596 mpp->mp_page->mp_pgno);
2600 if (mpp->mp_parent == NULL) {
2601 if (NUMKEYS(mpp->mp_page) == 0) {
2602 DPRINTF("tree is completely empty");
2603 txn->mt_dbs[dbi].md_root = P_INVALID;
2604 txn->mt_dbs[dbi].md_depth--;
2605 txn->mt_dbs[dbi].md_leaf_pages--;
2606 } else if (IS_BRANCH(mpp->mp_page) && NUMKEYS(mpp->mp_page) == 1) {
2607 DPRINTF("collapsing root page!");
2608 txn->mt_dbs[dbi].md_root = NODEPGNO(NODEPTR(mpp->mp_page, 0));
2609 if ((root = mdb_get_page(txn, txn->mt_dbs[dbi].md_root)) == NULL)
2611 txn->mt_dbs[dbi].md_depth--;
2612 txn->mt_dbs[dbi].md_branch_pages--;
2614 DPRINTF("root page doesn't need rebalancing");
2618 /* The parent (branch page) must have at least 2 pointers,
2619 * otherwise the tree is invalid.
2621 assert(NUMKEYS(mpp->mp_parent) > 1);
2623 /* Leaf page fill factor is below the threshold.
2624 * Try to move keys from left or right neighbor, or
2625 * merge with a neighbor page.
2630 if (mpp->mp_pi == 0) {
2631 /* We're the leftmost leaf in our parent.
2633 DPRINTF("reading right neighbor");
2634 node = NODEPTR(mpp->mp_parent, mpp->mp_pi + 1);
2635 if ((npp.mp_page = mdb_get_page(txn, NODEPGNO(node))) == NULL)
2637 npp.mp_pi = mpp->mp_pi + 1;
2639 di = NUMKEYS(mpp->mp_page);
2641 /* There is at least one neighbor to the left.
2643 DPRINTF("reading left neighbor");
2644 node = NODEPTR(mpp->mp_parent, mpp->mp_pi - 1);
2645 if ((npp.mp_page = mdb_get_page(txn, NODEPGNO(node))) == NULL)
2647 npp.mp_pi = mpp->mp_pi - 1;
2648 si = NUMKEYS(npp.mp_page) - 1;
2651 npp.mp_parent = mpp->mp_parent;
2653 DPRINTF("found neighbor page %lu (%u keys, %.1f%% full)",
2654 npp.mp_page->mp_pgno, NUMKEYS(npp.mp_page), (float)PAGEFILL(txn->mt_env, npp.mp_page) / 10);
2656 /* If the neighbor page is above threshold and has at least two
2657 * keys, move one key from it.
2659 * Otherwise we should try to merge them.
2661 if (PAGEFILL(txn->mt_env, npp.mp_page) >= FILL_THRESHOLD && NUMKEYS(npp.mp_page) >= 2)
2662 return mdb_move_node(txn, dbi, &npp, si, mpp, di);
2663 else { /* FIXME: if (has_enough_room()) */
2664 if (mpp->mp_pi == 0)
2665 return mdb_merge(txn, dbi, &npp, mpp);
2667 return mdb_merge(txn, dbi, mpp, &npp);
2672 mdb_del0(MDB_txn *txn, MDB_dbi dbi, unsigned int ki, MDB_pageparent *mpp, MDB_node *leaf)
2676 /* add overflow pages to free list */
2677 if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
2681 memcpy(&pg, NODEDATA(leaf), sizeof(pg));
2682 ovpages = OVPAGES(NODEDSZ(leaf), txn->mt_env->me_psize);
2683 for (i=0; i<ovpages; i++) {
2684 DPRINTF("freed ov page %lu", pg);
2685 mdb_idl_insert(txn->mt_free_pgs, pg);
2689 mdb_del_node(mpp->mp_page, ki);
2690 txn->mt_dbs[dbi].md_entries--;
2691 rc = mdb_rebalance(txn, dbi, mpp);
2692 if (rc != MDB_SUCCESS)
2693 txn->mt_flags |= MDB_TXN_ERROR;
2699 mdb_del(MDB_txn *txn, MDB_dbi dbi,
2700 MDB_val *key, MDB_val *data,
2708 DPRINTF("========> delete key %.*s", (int)key->mv_size, (char *)key->mv_data);
2710 assert(key != NULL);
2712 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
2715 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
2719 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
2723 mpp.mp_parent = NULL;
2725 if ((rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp)) != MDB_SUCCESS)
2728 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
2729 if (leaf == NULL || !exact) {
2730 return MDB_NOTFOUND;
2733 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
2737 mdb_xcursor_init0(txn, dbi, &mx);
2738 mdb_xcursor_init1(txn, dbi, &mx, leaf);
2739 if (flags == MDB_DEL_DUP) {
2740 rc = mdb_del(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, NULL, 0);
2741 mdb_xcursor_fini(txn, dbi, &mx);
2742 if (rc != MDB_SUCCESS)
2744 /* If sub-DB still has entries, we're done */
2745 if (mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root != P_INVALID) {
2746 memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
2750 /* otherwise fall thru and delete the sub-DB */
2752 /* add all the child DB's pages to the free list */
2753 rc = mdb_search_page(&mx.mx_txn, mx.mx_cursor.mc_dbi,
2754 NULL, &mx.mx_cursor, 0, &mp2);
2755 if (rc == MDB_SUCCESS) {
2756 MDB_ppage *top, *parent;
2760 cursor_pop_page(&mx.mx_cursor);
2761 top = CURSOR_TOP(&mx.mx_cursor);
2763 parent = SLIST_NEXT(top, mp_entry);
2764 while (parent != NULL) {
2765 for (i=0; i<NUMKEYS(top->mp_page); i++) {
2766 ni = NODEPTR(top->mp_page, i);
2767 mdb_idl_insert(txn->mt_free_pgs, ni->mn_pgno);
2771 if (parent->mp_ki >= NUMKEYS(parent->mp_page)) {
2772 cursor_pop_page(&mx.mx_cursor);
2773 top = CURSOR_TOP(&mx.mx_cursor);
2774 parent = SLIST_NEXT(top, mp_entry);
2776 ni = NODEPTR(parent->mp_page, parent->mp_ki);
2777 top->mp_page = mdb_get_page(&mx.mx_txn, ni->mn_pgno);
2782 mdb_idl_insert(txn->mt_free_pgs, mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi].md_root);
2787 if (data && (rc = mdb_read_data(txn, leaf, data)) != MDB_SUCCESS)
2790 return mdb_del0(txn, dbi, ki, &mpp, leaf);
2793 /* Split page <*mpp>, and insert <key,(data|newpgno)> in either left or
2794 * right sibling, at index <*newindxp> (as if unsplit). Updates *mpp and
2795 * *newindxp with the actual values after split, ie if *mpp and *newindxp
2796 * refer to a node in the new right sibling page.
2799 mdb_split(MDB_txn *txn, MDB_dbi dbi, MDB_page **mpp, unsigned int *newindxp,
2800 MDB_val *newkey, MDB_val *newdata, pgno_t newpgno)
2803 int rc = MDB_SUCCESS, ins_new = 0;
2806 unsigned int i, j, split_indx;
2808 MDB_val sepkey, rkey, rdata;
2810 MDB_dpage *mdp, *rdp, *pdp;
2813 assert(txn != NULL);
2815 dh = ((MDB_dhead *)*mpp) - 1;
2816 mdp = (MDB_dpage *)dh;
2817 newindx = *newindxp;
2819 DPRINTF("-----> splitting %s page %lu and adding [%.*s] at index %i",
2820 IS_LEAF(&mdp->p) ? "leaf" : "branch", mdp->p.mp_pgno,
2821 (int)newkey->mv_size, (char *)newkey->mv_data, *newindxp);
2823 if (mdp->h.md_parent == NULL) {
2824 if ((pdp = mdb_new_page(txn, dbi, P_BRANCH, 1)) == NULL)
2827 mdp->h.md_parent = &pdp->p;
2828 txn->mt_dbs[dbi].md_root = pdp->p.mp_pgno;
2829 DPRINTF("root split! new root = %lu", pdp->p.mp_pgno);
2830 txn->mt_dbs[dbi].md_depth++;
2832 /* Add left (implicit) pointer. */
2833 if (mdb_add_node(txn, dbi, &pdp->p, 0, NULL, NULL,
2834 mdp->p.mp_pgno, 0) != MDB_SUCCESS)
2837 DPRINTF("parent branch page is %lu", mdp->h.md_parent->mp_pgno);
2840 /* Create a right sibling. */
2841 if ((rdp = mdb_new_page(txn, dbi, mdp->p.mp_flags, 1)) == NULL)
2843 rdp->h.md_parent = mdp->h.md_parent;
2844 rdp->h.md_pi = mdp->h.md_pi + 1;
2845 DPRINTF("new right sibling: page %lu", rdp->p.mp_pgno);
2847 /* Move half of the keys to the right sibling. */
2848 if ((copy = malloc(txn->mt_env->me_psize)) == NULL)
2850 memcpy(copy, &mdp->p, txn->mt_env->me_psize);
2851 memset(&mdp->p.mp_ptrs, 0, txn->mt_env->me_psize - PAGEHDRSZ);
2852 mdp->p.mp_lower = PAGEHDRSZ;
2853 mdp->p.mp_upper = txn->mt_env->me_psize;
2855 split_indx = NUMKEYS(copy) / 2 + 1;
2857 /* First find the separating key between the split pages.
2859 memset(&sepkey, 0, sizeof(sepkey));
2860 if (newindx == split_indx) {
2861 sepkey.mv_size = newkey->mv_size;
2862 sepkey.mv_data = newkey->mv_data;
2864 node = NODEPTR(copy, split_indx);
2865 sepkey.mv_size = node->mn_ksize;
2866 sepkey.mv_data = NODEKEY(node);
2869 DPRINTF("separator is [%.*s]", (int)sepkey.mv_size, (char *)sepkey.mv_data);
2871 /* Copy separator key to the parent.
2873 if (SIZELEFT(rdp->h.md_parent) < mdb_branch_size(txn->mt_env, &sepkey)) {
2874 rc = mdb_split(txn, dbi, &rdp->h.md_parent, &rdp->h.md_pi,
2875 &sepkey, NULL, rdp->p.mp_pgno);
2877 /* Right page might now have changed parent.
2878 * Check if left page also changed parent.
2880 if (rdp->h.md_parent != mdp->h.md_parent &&
2881 mdp->h.md_pi >= NUMKEYS(mdp->h.md_parent)) {
2882 mdp->h.md_parent = rdp->h.md_parent;
2883 mdp->h.md_pi = rdp->h.md_pi - 1;
2886 rc = mdb_add_node(txn, dbi, rdp->h.md_parent, rdp->h.md_pi,
2887 &sepkey, NULL, rdp->p.mp_pgno, 0);
2889 if (rc != MDB_SUCCESS) {
2894 for (i = j = 0; i <= NUMKEYS(copy); j++) {
2895 if (i < split_indx) {
2896 /* Re-insert in left sibling. */
2899 /* Insert in right sibling. */
2900 if (i == split_indx)
2901 /* Reset insert index for right sibling. */
2902 j = (i == newindx && ins_new);
2906 if (i == newindx && !ins_new) {
2907 /* Insert the original entry that caused the split. */
2908 rkey.mv_data = newkey->mv_data;
2909 rkey.mv_size = newkey->mv_size;
2910 if (IS_LEAF(&mdp->p)) {
2911 rdata.mv_data = newdata->mv_data;
2912 rdata.mv_size = newdata->mv_size;
2919 /* Update page and index for the new key. */
2922 } else if (i == NUMKEYS(copy)) {
2925 node = NODEPTR(copy, i);
2926 rkey.mv_data = NODEKEY(node);
2927 rkey.mv_size = node->mn_ksize;
2928 if (IS_LEAF(&mdp->p)) {
2929 rdata.mv_data = NODEDATA(node);
2930 rdata.mv_size = node->mn_dsize;
2932 pgno = node->mn_pgno;
2933 flags = node->mn_flags;
2938 if (!IS_LEAF(&mdp->p) && j == 0) {
2939 /* First branch index doesn't need key data. */
2943 rc = mdb_add_node(txn, dbi, &pdp->p, j, &rkey, &rdata, pgno,flags);
2951 mdb_put0(MDB_txn *txn, MDB_dbi dbi,
2952 MDB_val *key, MDB_val *data, unsigned int flags)
2954 int rc = MDB_SUCCESS, exact;
2958 MDB_val xdata, *rdata;
2961 DPRINTF("==> put key %.*s, size %zu, data size %zu",
2962 (int)key->mv_size, (char *)key->mv_data, key->mv_size, data->mv_size);
2964 mpp.mp_parent = NULL;
2966 rc = mdb_search_page(txn, dbi, key, NULL, 1, &mpp);
2967 if (rc == MDB_SUCCESS) {
2968 leaf = mdb_search_node(txn, dbi, mpp.mp_page, key, &exact, &ki);
2969 if (leaf && exact) {
2970 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
2973 if (flags == MDB_NOOVERWRITE) {
2974 DPRINTF("duplicate key %.*s",
2975 (int)key->mv_size, (char *)key->mv_data);
2976 return MDB_KEYEXIST;
2978 /* same size, just replace it */
2979 if (NODEDSZ(leaf) == data->mv_size) {
2980 memcpy(NODEDATA(leaf), data->mv_data, data->mv_size);
2983 mdb_del_node(mpp.mp_page, ki);
2985 if (leaf == NULL) { /* append if not found */
2986 ki = NUMKEYS(mpp.mp_page);
2987 DPRINTF("appending key at index %i", ki);
2989 } else if (rc == MDB_NOTFOUND) {
2991 /* new file, just write a root leaf page */
2992 DPRINTF("allocating new root leaf page");
2993 if ((dp = mdb_new_page(txn, dbi, P_LEAF, 1)) == NULL) {
2996 mpp.mp_page = &dp->p;
2997 txn->mt_dbs[dbi].md_root = mpp.mp_page->mp_pgno;
2998 txn->mt_dbs[dbi].md_depth++;
3004 assert(IS_LEAF(mpp.mp_page));
3005 DPRINTF("there are %u keys, should insert new key at index %i",
3006 NUMKEYS(mpp.mp_page), ki);
3008 /* For sorted dups, the data item at this level is a DB record
3009 * for a child DB; the actual data elements are stored as keys
3012 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
3014 xdata.mv_size = sizeof(MDB_db);
3015 xdata.mv_data = &dummy;
3016 memset(&dummy, 0, sizeof(dummy));
3017 dummy.md_root = P_INVALID;
3022 if (SIZELEFT(mpp.mp_page) < mdb_leaf_size(txn->mt_env, key, rdata)) {
3023 rc = mdb_split(txn, dbi, &mpp.mp_page, &ki, key, rdata, P_INVALID);
3025 /* There is room already in this leaf page. */
3026 rc = mdb_add_node(txn, dbi, mpp.mp_page, ki, key, rdata, 0, 0);
3029 if (rc != MDB_SUCCESS)
3030 txn->mt_flags |= MDB_TXN_ERROR;
3032 txn->mt_dbs[dbi].md_entries++;
3034 /* Remember if we just added a subdatabase */
3035 if (flags & F_SUBDATA) {
3036 leaf = NODEPTR(mpp.mp_page, ki);
3037 leaf->mn_flags |= F_SUBDATA;
3040 /* Now store the actual data in the child DB. Note that we're
3041 * storing the user data in the keys field, so there are strict
3042 * size limits on dupdata. The actual data fields of the child
3043 * DB are all zero size.
3045 if (F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
3048 leaf = NODEPTR(mpp.mp_page, ki);
3050 mdb_xcursor_init0(txn, dbi, &mx);
3051 mdb_xcursor_init1(txn, dbi, &mx, leaf);
3054 if (flags == MDB_NODUPDATA)
3055 flags = MDB_NOOVERWRITE;
3056 rc = mdb_put0(&mx.mx_txn, mx.mx_cursor.mc_dbi, data, &xdata, flags);
3057 mdb_xcursor_fini(txn, dbi, &mx);
3058 memcpy(NODEDATA(leaf), &mx.mx_txn.mt_dbs[mx.mx_cursor.mc_dbi],
3068 mdb_put(MDB_txn *txn, MDB_dbi dbi,
3069 MDB_val *key, MDB_val *data, unsigned int flags)
3071 assert(key != NULL);
3072 assert(data != NULL);
3074 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
3077 if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
3081 if (key->mv_size == 0 || key->mv_size > MAXKEYSIZE) {
3085 if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA)) != flags)
3088 return mdb_put0(txn, dbi, key, data, flags);
3092 mdbenv_get_flags(MDB_env *env, unsigned int *arg)
3097 *arg = env->me_flags;
3102 mdbenv_get_path(MDB_env *env, const char **arg)
3107 *arg = env->me_path;
3112 mdb_stat0(MDB_env *env, MDB_db *db, MDB_stat *arg)
3114 arg->ms_psize = env->me_psize;
3115 arg->ms_depth = db->md_depth;
3116 arg->ms_branch_pages = db->md_branch_pages;
3117 arg->ms_leaf_pages = db->md_leaf_pages;
3118 arg->ms_overflow_pages = db->md_overflow_pages;
3119 arg->ms_entries = db->md_entries;
3124 mdbenv_stat(MDB_env *env, MDB_stat *arg)
3126 if (env == NULL || arg == NULL)
3129 return mdb_stat0(env, &env->me_meta->mm_dbs[MAIN_DBI], arg);
3132 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
3142 if (flags & (MDB_DUPSORT|MDB_REVERSEKEY|MDB_INTEGERKEY))
3143 txn->mt_dbs[MAIN_DBI].md_flags |= (flags & (MDB_DUPSORT|MDB_REVERSEKEY|MDB_INTEGERKEY));
3147 /* Is the DB already open? */
3149 for (i=2; i<txn->mt_numdbs; i++) {
3150 if (len == txn->mt_dbxs[i].md_name.mv_size &&
3151 !strncmp(name, txn->mt_dbxs[i].md_name.mv_data, len)) {
3157 if (txn->mt_numdbs >= txn->mt_env->me_maxdbs - 1)
3160 /* Find the DB info */
3162 key.mv_data = (void *)name;
3163 rc = mdb_get(txn, MAIN_DBI, &key, &data);
3165 /* Create if requested */
3166 if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
3168 data.mv_size = sizeof(MDB_db);
3169 data.mv_data = &dummy;
3170 memset(&dummy, 0, sizeof(dummy));
3171 dummy.md_root = P_INVALID;
3172 dummy.md_flags = flags & 0xffff;
3173 rc = mdb_put0(txn, MAIN_DBI, &key, &data, F_SUBDATA);
3177 /* OK, got info, add to table */
3178 if (rc == MDB_SUCCESS) {
3179 txn->mt_dbxs[txn->mt_numdbs].md_name.mv_data = strdup(name);
3180 txn->mt_dbxs[txn->mt_numdbs].md_name.mv_size = len;
3181 txn->mt_dbxs[txn->mt_numdbs].md_cmp = NULL;
3182 txn->mt_dbxs[txn->mt_numdbs].md_dcmp = NULL;
3183 txn->mt_dbxs[txn->mt_numdbs].md_rel = NULL;
3184 txn->mt_dbxs[txn->mt_numdbs].md_parent = MAIN_DBI;
3185 txn->mt_dbxs[txn->mt_numdbs].md_dirty = dirty;
3186 memcpy(&txn->mt_dbs[txn->mt_numdbs], data.mv_data, sizeof(MDB_db));
3187 *dbi = txn->mt_numdbs;
3194 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
3196 if (txn == NULL || arg == NULL || dbi >= txn->mt_numdbs)
3199 return mdb_stat0(txn->mt_env, &txn->mt_dbs[dbi], arg);
3202 void mdb_close(MDB_txn *txn, MDB_dbi dbi)
3205 if (dbi <= MAIN_DBI || dbi >= txn->mt_numdbs)
3207 ptr = txn->mt_dbxs[dbi].md_name.mv_data;
3208 txn->mt_dbxs[dbi].md_name.mv_data = NULL;
3209 txn->mt_dbxs[dbi].md_name.mv_size = 0;
3213 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
3215 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
3218 txn->mt_dbxs[dbi].md_cmp = cmp;
3222 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
3224 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
3227 txn->mt_dbxs[dbi].md_dcmp = cmp;
3231 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
3233 if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
3236 txn->mt_dbxs[dbi].md_rel = rel;