git » nmdb » commit 84513cb

Add support for a "null" backend.

author Alberto Bertogli
2007-07-25 19:56:40 UTC
committer Alberto Bertogli
2007-07-26 23:11:16 UTC
parent 825ffc9e5b6e96b0623b5d03ff05a115d39c19b7

Add support for a "null" backend.

This patch adds support for a new backend called "null" which just
accepts all sets, and misses all gets/dels.

It is useful if you want to use nmdb without a database, to avoid linking
with qdbm.

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

nmdb/Makefile +17 -2
nmdb/be-null.c +37 -0
nmdb/be.h +12 -4

diff --git a/nmdb/Makefile b/nmdb/Makefile
index 5c58c5a..164a66e 100644
--- a/nmdb/Makefile
+++ b/nmdb/Makefile
@@ -1,8 +1,12 @@
 
+# Protocols to enable
 ENABLE_TIPC = 1
 ENABLE_TCP = 1
 ENABLE_UDP = 1
 
+# Backend to use, can be qdbm or null
+BACKEND = qdbm
+
 CFLAGS += -std=c99 -Wall -O3
 ALL_CFLAGS = -D_XOPEN_SOURCE=500 $(CFLAGS)
 ALL_CFLAGS += -DENABLE_TIPC=$(ENABLE_TIPC) \
@@ -25,7 +29,9 @@ endif
 PREFIX=/usr/local
 
 
-OBJS = be-qdbm.o cache.o db.o queue.o net.o parse.o main.o
+OBJS = cache.o db.o queue.o net.o parse.o main.o
+LIBS = -levent -lpthread -lrt
+
 
 ifeq ($(ENABLE_TIPC), 1)
 	OBJS += tipc.o
@@ -45,13 +51,22 @@ else
 	OBJS += udp-stub.o
 endif
 
+ifeq ($(BACKEND), qdbm)
+	OBJS += be-qdbm.o
+	ALL_CFLAGS += -DBACKEND_QDBM
+	LIBS += -lqdbm
+else ifeq ($(BACKEND), null)
+	OBJS += be-null.o
+	ALL_CFLAGS += -DBACKEND_NULL
+endif
+
 
 default: all
 
 all: nmdb
 
 nmdb: $(OBJS)
-	$(CC) $(ALL_CFLAGS) $(OBJS) -levent -lpthread -lrt -lqdbm -o nmdb
+	$(CC) $(ALL_CFLAGS) $(OBJS) $(LIBS) -o nmdb
 
 .c.o:
 	$(CC) $(ALL_CFLAGS) -c $< -o $@
diff --git a/nmdb/be-null.c b/nmdb/be-null.c
new file mode 100644
index 0000000..c358fa0
--- /dev/null
+++ b/nmdb/be-null.c
@@ -0,0 +1,37 @@
+
+#include <stddef.h>	/* size_t */
+#include "be.h"
+
+
+db_t *db_open(const char *name, int flags)
+{
+	/* Use a dumb not-null pointer because it is never looked at outside
+	 * the functions defined here */
+	return (db_t *) 1;
+}
+
+
+int db_close(db_t *db)
+{
+	return 1;
+}
+
+
+int db_set(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t vsize)
+{
+	return 1;
+}
+
+
+int db_get(db_t *db, const unsigned char *key, size_t ksize,
+		unsigned char *val, size_t *vsize)
+{
+	return 0;
+}
+
+int db_del(db_t *db, const unsigned char *key, size_t ksize)
+{
+	return 0;
+}
+
diff --git a/nmdb/be.h b/nmdb/be.h
index 246f3de..59fa21c 100644
--- a/nmdb/be.h
+++ b/nmdb/be.h
@@ -2,10 +2,18 @@
 #ifndef _BE_H
 #define _BE_H
 
-/* The following should be specific to the db backend we use. As we only
- * handle qdbm for now, there's no need to play with #ifdefs. */
-#include <depot.h>
-typedef DEPOT db_t;
+/* Depending on our backend, we define db_t to be different things so the
+ * generic code doesn't have to care about which backend we're using. */
+#if defined BACKEND_QDBM
+  #include <depot.h>
+  typedef DEPOT 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. */
+  typedef int db_t;
+#endif
 
 
 db_t *db_open(const char *name, int flags);