author | Alberto Bertogli
<albertito@gmail.com> 2007-07-30 16:18:48 UTC |
committer | Alberto Bertogli
<albertito@gmail.com> 2007-07-30 16:18:48 UTC |
parent | a6d3fbcab8ac8ae2999e533e51affb69ce9cb218 |
nmdb/Makefile | +5 | -1 |
nmdb/be-bdb.c | +99 | -0 |
nmdb/be.h | +6 | -0 |
diff --git a/nmdb/Makefile b/nmdb/Makefile index 8d0c899..6d848a5 100644 --- a/nmdb/Makefile +++ b/nmdb/Makefile @@ -4,7 +4,7 @@ ENABLE_TIPC = 1 ENABLE_TCP = 1 ENABLE_UDP = 1 -# Backend to use, can be qdbm or null +# Backend to use, can be qdbm, bdb, or null BACKEND = qdbm CFLAGS += -std=c99 -Wall -O3 @@ -55,6 +55,10 @@ ifeq ($(BACKEND), qdbm) OBJS += be-qdbm.o ALL_CFLAGS += -DBACKEND_QDBM LIBS += -lqdbm +else ifeq ($(BACKEND), bdb) + OBJS += be-bdb.o + ALL_CFLAGS += -DBACKEND_BDB + LIBS += -ldb else ifeq ($(BACKEND), null) OBJS += be-null.o ALL_CFLAGS += -DBACKEND_NULL diff --git a/nmdb/be-bdb.c b/nmdb/be-bdb.c new file mode 100644 index 0000000..4a930f8 --- /dev/null +++ b/nmdb/be-bdb.c @@ -0,0 +1,99 @@ + +#include <string.h> /* memset() */ +#include "be.h" + + +db_t *db_open(const char *name, int flags) +{ + int rv; + db_t *db; + + rv = db_create(&db, NULL, 0); + if (rv != 0) + return NULL; + + rv = db->open(db, NULL, name, NULL, DB_HASH, DB_CREATE, 0); + if (rv != 0) { + db->close(db, 0); + return NULL; + } + + return db; +} + + +int db_close(db_t *db) +{ + int rv; + + rv = db->close(db, 0); + if (rv != 0) + return 0; + return 1; +} + + +int db_set(db_t *db, const unsigned char *key, size_t ksize, + unsigned char *val, size_t vsize) +{ + int rv; + DBT k, v; + + memset(&k, 0, sizeof(DBT)); + memset(&v, 0, sizeof(DBT)); + + /* we can't maintain "const"ness here because bdb's prototypes; the + * same applies to get and del */ + k.data = key; + k.size = ksize; + v.data = val; + v.size = vsize; + + rv = db->put(db, NULL, &k, &v, 0); + if (rv != 0) + return 0; + return 1; +} + + +int db_get(db_t *db, const unsigned char *key, size_t ksize, + unsigned char *val, size_t *vsize) +{ + int rv; + DBT k, v; + + memset(&k, 0, sizeof(DBT)); + memset(&v, 0, sizeof(DBT)); + + k.data = key; + k.size = ksize; + v.data = val; + v.ulen = *vsize; + v.flags = DB_DBT_USERMEM; /* we supplied the memory */ + + rv = db->get(db, NULL, &k, &v, 0); + if (rv != 0) { + return 0; + } else { + *vsize = v.size; + return 1; + } +} + +int db_del(db_t *db, const unsigned char *key, size_t ksize) +{ + int rv; + DBT k, v; + + memset(&k, 0, sizeof(DBT)); + memset(&v, 0, sizeof(DBT)); + + k.data = key; + k.size = ksize; + + rv = db->del(db, NULL, &k, 0); + if (rv != 0) + return 0; + return 1; +} + diff --git a/nmdb/be.h b/nmdb/be.h index 59fa21c..fd8e384 100644 --- a/nmdb/be.h +++ b/nmdb/be.h @@ -7,6 +7,12 @@ #if defined BACKEND_QDBM #include <depot.h> typedef DEPOT db_t; +#elif defined BACKEND_BDB + /* typedefs to work around db.h include bug */ + typedef unsigned int u_int; + typedef unsigned long u_long; + #include <db.h> + typedef DB db_t; #elif defined BACKEND_NULL typedef int db_t; #else