git » nmdb » commit 8967449

Add tokyocabinet backend

author Alberto Bertogli
2008-10-03 15:55:28 UTC
committer Alberto Bertogli
2008-10-03 16:12:42 UTC
parent 31201b0650c5df9a89797ae581b68d31351da8f2

Add tokyocabinet backend

This patch adds support for tokyocabinet
(http://tokyocabinet.sf.net/index.html) as a backend.

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

doc/design.rst +3 -2
doc/guide.rst +4 -2
nmdb/Makefile +6 -1
nmdb/be-tc.c +52 -0
nmdb/be.h +7 -0

diff --git a/doc/design.rst b/doc/design.rst
index 44927bd..4470fec 100644
--- a/doc/design.rst
+++ b/doc/design.rst
@@ -123,8 +123,8 @@ operation. A specific solution could have been used, and the database backend
 code is isolated enough to allow this to happen in the future if necessity
 arises.
 
-Several backends are supported (at the moment QDBM_, BDB_, and a null
-backend); the selection is done at build time.
+Several backends are supported (at the moment QDBM_, BDB_, tokyocabinet_ and a
+null backend); the selection is done at build time.
 
 The processing is performed by taking requests from the aforementioned queue,
 and acting upon the database accordingly, which involves calling the backend's
@@ -204,4 +204,5 @@ pattern involves handling lots of different keys.
 .. _memcached: http://www.danga.com/memcached/
 .. _QDBM: http://qdbm.sf.net
 .. _BDB: http://www.oracle.com/technology/products/berkeley-db/db/
+.. _tokyocabinet: http://tokyocabinet.sf.net/index.html
 
diff --git a/doc/guide.rst b/doc/guide.rst
index 12c18e8..3d94cfd 100644
--- a/doc/guide.rst
+++ b/doc/guide.rst
@@ -31,7 +31,7 @@ Prerequisites
 Before you install nmdb, you will need the following software:
 
 - libevent_, a library for fast event handling.
-- QDBM_, for the database backend.
+- Either QDBM_, BDB_ or tokyocabinet_ for the database backend.
 
 And, if you're going to use TIPC_:
 
@@ -417,8 +417,10 @@ know at albertito@blitiri.com.ar.
 .. _libevent: http://www.monkey.org/~provos/libevent/
 .. _TIPC: http://tipc.sf.net
 .. _memcached: http://www.danga.com/memcached/
-.. _QDBM: http://qdbm.sf.net
 .. _`Linux kernel`: http://kernel.org
 .. _tetrations: http://en.wikipedia.org/wiki/Tetration
+.. _QDBM: http://qdbm.sf.net
+.. _BDB: http://www.oracle.com/technology/products/berkeley-db/db/
+.. _tokyocabinet: http://tokyocabinet.sf.net/index.html
 
 
diff --git a/nmdb/Makefile b/nmdb/Makefile
index 098da97..66a9f14 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, or null
+# Backend to use, can be qdbm, bdb, tc, or null
 BACKEND = qdbm
 
 CFLAGS += -std=c99 -pedantic -Wall -O3
@@ -73,6 +73,11 @@ ifeq ($(BACKEND), bdb)
 	ALL_CFLAGS += -DBACKEND_BDB
 	LIBS += -ldb
 endif
+ifeq ($(BACKEND), tc)
+	OBJS += be-tc.o
+	ALL_CFLAGS += `pkg-config tokyocabinet --cflags` -DBACKEND_TC
+	LIBS += `pkg-config tokyocabinet --libs`
+endif
 ifeq ($(BACKEND), null)
 	OBJS += be-null.o
 	ALL_CFLAGS += -DBACKEND_NULL
diff --git a/nmdb/be-tc.c b/nmdb/be-tc.c
new file mode 100644
index 0000000..11f5267
--- /dev/null
+++ b/nmdb/be-tc.c
@@ -0,0 +1,52 @@
+
+#include <tchdb.h>	/* Tokyo Cabinet's hash API */
+#include <stdlib.h>
+
+#include "be.h"
+
+
+db_t *db_open(const char *name, int flags)
+{
+	db_t *db = tchdbnew();
+
+	if (!tchdbopen(db, name, HDBOWRITER | HDBOCREAT))
+		return NULL;
+
+	return db;
+}
+
+
+int db_close(db_t *db)
+{
+	int r = tchdbclose(db);
+	tchdbdel(db);
+	return r;
+}
+
+
+int db_set(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t vsize)
+{
+	return tchdbput(db, key, ksize, val, vsize);
+}
+
+
+int db_get(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t *vsize)
+{
+	int rv;
+
+	rv = tchdbget3(db, key, ksize, val, *vsize);
+	if (rv >= 0) {
+		*vsize = rv;
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+int db_del(db_t *db, const unsigned char *key, size_t ksize)
+{
+	return tchdbout(db, key, ksize);
+}
+
diff --git a/nmdb/be.h b/nmdb/be.h
index fd8e384..d333b59 100644
--- a/nmdb/be.h
+++ b/nmdb/be.h
@@ -7,14 +7,21 @@
 #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_TC
+  #include <tchdb.h>
+  typedef TCHDB db_t;
+
 #elif defined BACKEND_NULL
   typedef int db_t;
+
 #else
   #error "Unknown backend"
   /* Define it anyway, so this is the only warning/error the user sees. */