Merge remote-tracking branch 'origin/mdb.master'

This commit is contained in:
Howard Chu 2013-03-06 15:50:47 -08:00
commit 8289ac3b5c
4 changed files with 141 additions and 10 deletions

View file

@ -9,13 +9,15 @@ prefix = /usr/local
IHDRS = lmdb.h IHDRS = lmdb.h
ILIBS = liblmdb.a liblmdb.so ILIBS = liblmdb.a liblmdb.so
IPROGS = mdb_stat mdb_copy IPROGS = mdb_stat mdb_copy
IDOCS = mdb_stat.1 mdb_copy.1
PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5 PROGS = $(IPROGS) mtest mtest2 mtest3 mtest4 mtest5
all: $(ILIBS) $(PROGS) all: $(ILIBS) $(PROGS)
install: $(ILIBS) $(IPROGS) $(IHDRS) install: $(ILIBS) $(IPROGS) $(IHDRS)
cp $(IPROGS) $(prefix)/bin cp $(IPROGS) $(DESTDIR)$(prefix)/bin
cp $(ILIBS) $(prefix)/lib cp $(ILIBS) $(DESTDIR)$(prefix)/lib
cp $(IHDRS) $(prefix)/include cp $(IHDRS) $(DESTDIR)$(prefix)/include
cp $(IDOCS) $(DESTDIR)$(prefix)/man/man1
clean: clean:
rm -rf $(PROGS) *.[ao] *.so *~ testdb rm -rf $(PROGS) *.[ao] *.so *~ testdb
@ -28,7 +30,7 @@ liblmdb.a: mdb.o midl.o
ar rs $@ mdb.o midl.o ar rs $@ mdb.o midl.o
liblmdb.so: mdb.o midl.o liblmdb.so: mdb.o midl.o
gcc -pthread -shared -o $@ mdb.o midl.o $(SOLIBS) $(CC) $(LDFLAGS) -pthread -shared -o $@ mdb.o midl.o $(SOLIBS)
mdb_stat: mdb_stat.o liblmdb.a mdb_stat: mdb_stat.o liblmdb.a
mdb_copy: mdb_copy.o liblmdb.a mdb_copy: mdb_copy.o liblmdb.a

View file

@ -431,9 +431,6 @@ typedef uint16_t indx_t;
* the longer we delay reclaiming old pages, the more likely it is that a * the longer we delay reclaiming old pages, the more likely it is that a
* string of contiguous pages can be found after coalescing old pages from * string of contiguous pages can be found after coalescing old pages from
* many old transactions together. * many old transactions together.
*
* @todo We don't actually do such coalescing yet, we grab pages from one
* old transaction at a time.
* @{ * @{
*/ */
/** Number of slots in the reader table. /** Number of slots in the reader table.
@ -6371,12 +6368,13 @@ mdb_rebalance(MDB_cursor *mc)
DPRINTF("found neighbor page %zu (%u keys, %.1f%% full)", DPRINTF("found neighbor page %zu (%u keys, %.1f%% full)",
mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]), (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10); mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]), (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10);
/* If the neighbor page is above threshold and has at least two /* If the neighbor page is above threshold and has at least three
* keys, move one key from it. * keys, move one key from it. (A page must never have fewer than
* 2 keys.)
* *
* Otherwise we should try to merge them. * Otherwise we should try to merge them.
*/ */
if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) >= 2) if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > 2)
return mdb_node_move(&mn, mc); return mdb_node_move(&mn, mc);
else { else {
if (mc->mc_ki[ptop] == 0) if (mc->mc_ki[ptop] == 0)

View file

@ -0,0 +1,71 @@
/* sample-bdb.c - BerkeleyDB toy/sample
*
* Do a line-by-line comparison of this and sample-mdb.c
*/
/*
* Copyright 2012 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include <stdio.h>
#include <string.h>
#include <db.h>
int main(int argc,char * argv[])
{
int rc;
DB_ENV *env;
DB *dbi;
DBT key, data;
DB_TXN *txn;
DBC *cursor;
char sval[32], kval[32];
#define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD)
rc = db_env_create(&env, 0);
rc = env->open(env, "./testdb", FLAGS, 0664);
rc = db_create(&dbi, env, 0);
rc = env->txn_begin(env, NULL, &txn, 0);
rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664);
memset(&key, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
key.size = sizeof(int);
key.data = sval;
data.size = sizeof(sval);
data.data = sval;
sprintf(sval, "%03x %d foo bar", 32, 3141592);
rc = dbi->put(dbi, txn, &key, &data, 0);
rc = txn->commit(txn, 0);
if (rc) {
fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc));
goto leave;
}
rc = env->txn_begin(env, NULL, &txn, 0);
rc = dbi->cursor(dbi, txn, &cursor, 0);
key.flags = DB_DBT_USERMEM;
key.data = kval;
key.ulen = sizeof(kval);
data.flags = DB_DBT_USERMEM;
data.data = sval;
data.ulen = sizeof(sval);
while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) {
printf("key: %p %.*s, data: %p %.*s\n",
key.data, (int) key.size, (char *) key.data,
data.data, (int) data.size, (char *) data.data);
}
rc = cursor->c_close(cursor);
rc = txn->abort(txn);
leave:
rc = dbi->close(dbi, 0);
rc = env->close(env, 0);
return rc;
}

View file

@ -0,0 +1,60 @@
/* sample-mdb.c - MDB toy/sample
*
* Do a line-by-line comparison of this and sample-bdb.c
*/
/*
* Copyright 2012 Howard Chu, Symas Corp.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include <stdio.h>
#include "lmdb.h"
int main(int argc,char * argv[])
{
int rc;
MDB_env *env;
MDB_dbi dbi;
MDB_val key, data;
MDB_txn *txn;
MDB_cursor *cursor;
char sval[32];
rc = mdb_env_create(&env);
rc = mdb_env_open(env, "./testdb", 0, 0664);
rc = mdb_txn_begin(env, NULL, 0, &txn);
rc = mdb_open(txn, NULL, 0, &dbi);
key.mv_size = sizeof(int);
key.mv_data = sval;
data.mv_size = sizeof(sval);
data.mv_data = sval;
sprintf(sval, "%03x %d foo bar", 32, 3141592);
rc = mdb_put(txn, dbi, &key, &data, 0);
rc = mdb_txn_commit(txn);
if (rc) {
fprintf(stderr, "mdb_txn_commit: (%d) %s\n", rc, mdb_strerror(rc));
goto leave;
}
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
rc = mdb_cursor_open(txn, dbi, &cursor);
while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
printf("key: %p %.*s, data: %p %.*s\n",
key.mv_data, (int) key.mv_size, (char *) key.mv_data,
data.mv_data, (int) data.mv_size, (char *) data.mv_data);
}
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
leave:
mdb_close(env, dbi);
mdb_env_close(env);
return 0;
}