git » nmdb » commit 2441880

Add a tdb backend

author Alberto Bertogli
2010-04-16 05:40:37 UTC
committer Alberto Bertogli
2010-04-16 05:40:37 UTC
parent 2471150926fb4ab0956b864715dbee2dbda7e297

Add a tdb backend

tdb (http://tdb.samba.org/) is a nice gdbm-alike database, from the Samba
project.

This patch adds support for it.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

INSTALL +4 -2
nmdb/Makefile +6 -1
nmdb/be-tdb.c +71 -0
nmdb/be.h +4 -0

diff --git a/INSTALL b/INSTALL
index 64f1615..862192d 100644
--- a/INSTALL
+++ b/INSTALL
@@ -32,14 +32,16 @@ The backends, on the other hand, are mutually exclusive and only one can be
 selected:
  * qdbm (http://qdbm.sf.net/).
  * bdb (http://www.oracle.com/database/berkeley-db/).
- * null backend, if you don't need a real one.
+ * Tokyo Cabinet (http://1978th.net/tokyocabinet/)
+ * tdb (http://tdb.samba.org/)
+ * A null backend, if you don't need a real one.
 
 By default, all network protocols are enabled, and the qdbm backend is
 selected.
 
 You can change the defaults by passing parameters to make, like this:
 
- $ make BACKEND=[qbdm|bdb|null] ENABLE_$PROTO=[1|0]
+ $ make BACKEND=[qbdm|bdb|tc|tdb|null] ENABLE_$PROTO=[1|0]
 
 Where $PROTO can be TCP, UDP, TIPC or SCTP.
 
diff --git a/nmdb/Makefile b/nmdb/Makefile
index 66a9f14..9d94451 100644
--- a/nmdb/Makefile
+++ b/nmdb/Makefile
@@ -5,7 +5,7 @@ ENABLE_TCP = 1
 ENABLE_UDP = 1
 ENABLE_SCTP = 1
 
-# Backend to use, can be qdbm, bdb, tc, or null
+# Backend to use, can be qdbm, bdb, tc, tdb, or null
 BACKEND = qdbm
 
 CFLAGS += -std=c99 -pedantic -Wall -O3
@@ -78,6 +78,11 @@ ifeq ($(BACKEND), tc)
 	ALL_CFLAGS += `pkg-config tokyocabinet --cflags` -DBACKEND_TC
 	LIBS += `pkg-config tokyocabinet --libs`
 endif
+ifeq ($(BACKEND), tdb)
+	OBJS += be-tdb.o
+	ALL_CFLAGS += `pkg-config tdb --cflags` -DBACKEND_TDB
+	LIBS += `pkg-config tdb --libs`
+endif
 ifeq ($(BACKEND), null)
 	OBJS += be-null.o
 	ALL_CFLAGS += -DBACKEND_NULL
diff --git a/nmdb/be-tdb.c b/nmdb/be-tdb.c
new file mode 100644
index 0000000..7c9f8a0
--- /dev/null
+++ b/nmdb/be-tdb.c
@@ -0,0 +1,71 @@
+
+#include <string.h>	/* memcpy() */
+
+/* tdb.h needs mode_t defined externally, and it is defined in one of these
+(which are the ones required for open() */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "be.h"
+
+
+db_t *db_open(const char *name, int flags)
+{
+	return tdb_open(name, 0, 0, O_CREAT | O_RDWR, 0640);
+}
+
+
+int db_close(db_t *db)
+{
+	return tdb_close(db) == 0;
+}
+
+
+int db_set(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t vsize)
+{
+	TDB_DATA k, v;
+
+	/* we can't maintain "const"ness here because tdb's prototypes; the
+	 * same applies to get and del */
+	k.dptr = key;
+	k.dsize = ksize;
+	v.dptr = val;
+	v.dsize = vsize;
+
+	return tdb_store(db, k, v, TDB_REPLACE) == 0;
+}
+
+
+int db_get(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t *vsize)
+{
+	TDB_DATA k, v;
+
+	k.dptr = key;
+	k.dsize = ksize;
+
+	v = tdb_fetch(db, k);
+	if (v.dptr == NULL)
+		return 0;
+
+	if (v.dsize > *vsize)
+		return 0;
+
+	*vsize = v.dsize;
+	memcpy(val, v.dptr, v.dsize);
+	free(v.dptr);
+	return 1;
+}
+
+int db_del(db_t *db, const unsigned char *key, size_t ksize)
+{
+	TDB_DATA k;
+
+	k.dptr = key;
+	k.dsize = ksize;
+
+	return tdb_delete(db, k) == 0;
+}
+
diff --git a/nmdb/be.h b/nmdb/be.h
index d333b59..f320361 100644
--- a/nmdb/be.h
+++ b/nmdb/be.h
@@ -19,6 +19,10 @@
   #include <tchdb.h>
   typedef TCHDB db_t;
 
+#elif defined BACKEND_TDB
+  #include <tdb.h>
+  typedef TDB_CONTEXT db_t;
+
 #elif defined BACKEND_NULL
   typedef int db_t;