git » nmdb » commit d5d2dc0

Add nmdb bindings for newlisp.

author Alberto Bertogli
2007-04-24 23:04:21 UTC
committer Alberto Bertogli
2007-04-24 23:04:21 UTC
parent 83d4fc1398a134e2942811f3a77196fe96bd1e79

Add nmdb bindings for newlisp.

This patch adds nmdb bindings for newlisp (http://www.newlisp.org).
They're very simple, but seem to work just fine, although they have not
been tested thoroughly.

newlisp/nmdb.lsp +114 -0
newlisp/test.lsp +24 -0

diff --git a/newlisp/nmdb.lsp b/newlisp/nmdb.lsp
new file mode 100644
index 0000000..89f8085
--- /dev/null
+++ b/newlisp/nmdb.lsp
@@ -0,0 +1,114 @@
+
+;
+; nmdb bindings for newlisp (http://www.newlisp.org/)
+; Alberto Bertogli (albertito@gmail.com)
+;
+; Functions:
+;
+;   (nmdb:init port) -> Connects with the DB at the given port.
+;   (nmdb:add-server port) -> Adds a new server to the connection.
+;   (nmdb:free ) -> Closes the connection.
+;
+;   (nmdb:db-get key) -> Gets the value associated to the given key, or -1.
+;   (nmdb:cache-get key) -> Like dbget but only get from the cache.
+;
+;   (nmdb:db-set key val ) -> Sets the given key to the given value.
+;   (nmdb:sync-set key val ) -> Like db-set but synchronous.
+;   (nmdb:cache-set key val ) -> Like db-set but only set to the the cache.
+;
+;   (nmdb:db-del key ) -> Removes the given key from the database.
+;   (nmdb:sync-del key ) -> Like db-del but synchronous.
+;   (nmdb:cache-del key ) -> Like db-del but only delete from the cache.
+;
+;
+; Example:
+;   (load "nmdb.lsp")
+;   (nmdb:init 10)
+;   (nmdb:add-server 11)
+;   (nmdb:db-set "Hello" "Newlisp!")
+;   (nmdb:db-get "Hello")
+;   (nmdb:db-del "Hello")
+;   (nmdb:free)
+;
+; For more information check the nmdb docs.
+;
+
+
+(context 'nmdb)
+
+; library loading
+(set 'libnmdb "libnmdb.so")
+
+(import libnmdb "nmdb_init")
+(import libnmdb "nmdb_add_server")
+(import libnmdb "nmdb_free")
+
+(import libnmdb "nmdb_set")
+(import libnmdb "nmdb_set_sync")
+(import libnmdb "nmdb_cache_set")
+
+(import libnmdb "nmdb_get")
+(import libnmdb "nmdb_cache_get")
+
+(import libnmdb "nmdb_del")
+(import libnmdb "nmdb_del_sync")
+(import libnmdb "nmdb_cache_del")
+
+
+; main functions
+
+(define (init port)
+  (set 'NMDB (nmdb_init port))
+  (if (= NMDB 0) (set NMDB nil))
+  (not (= NMDB nil)))
+
+(define (add-server port)
+  (nmdb_add_server NMDB port))
+
+(define (free)
+  (nmdb_free NMDB))
+
+
+; *-get functions
+
+(define (priv-get func key)
+  (letn ( (keylen (length key))
+	  (vallen (* 64 1024))
+	  (val (dup "\000" vallen))
+	)
+    (set 'rv (func NMDB key keylen val vallen))
+    (if (> rv 0)
+      (slice val 0 rv)
+      -1) ) )
+
+(define (db-get key) (priv-get nmdb_get key))
+(define (cache-get key) (priv-get nmdb_cache_get key))
+
+
+; *-set functions
+
+(define (priv-set func key val)
+  (letn ( (keylen (length key))
+	  (vallen (length val))
+	)
+    (func NMDB key keylen val vallen) ) )
+
+(define (db-set key val) (priv-set nmdb_set key val))
+(define (sync-set key val) (priv-set nmdb_set_sync key val))
+(define (cache-set key val) (priv-set nmdb_cache_set key val))
+
+
+; *-del functions
+(define (priv-del func key)
+  (letn ( (keylen (length key)) )
+    (func NMDB key keylen) ) )
+
+(define (db-del key) (priv-del nmdb_del key))
+(define (sync-del key) (priv-del nmdb_del_sync key))
+(define (cache-del key) (priv-del nmdb_cache_del key))
+
+
+(context MAIN)
+
+
+
diff --git a/newlisp/test.lsp b/newlisp/test.lsp
new file mode 100644
index 0000000..a1371dd
--- /dev/null
+++ b/newlisp/test.lsp
@@ -0,0 +1,24 @@
+
+; To run: newlisp test.lsp
+
+(load "nmdb.lsp")
+
+(println "init\t\t"		(nmdb:init 10))
+(println "add-server\t"		(nmdb:add-server 11))
+(println "add-server\t"		(nmdb:add-server 12))
+(println "add-server\t"		(nmdb:add-server 13))
+(println)
+(println "db-set D1 V1\t"	(nmdb:db-set "D1" "D1"))
+(println "sync-set S2 V2\t"	(nmdb:sync-set "S2" "V2"))
+(println "cache-set C3 C3\t"	(nmdb:cache-set "C3" "C3"))
+(println)
+(println "db-get D1\t"		(nmdb:db-get "D1"))
+(println "db-get S2\t"		(nmdb:db-get "S2"))
+(println "cache-get C3\t"	(nmdb:cache-get "C3"))
+(println)
+(println "db-del D1\t"		(nmdb:db-del "D1"))
+(println "sync-del S2\t"	(nmdb:sync-del "S2"))
+(println "cache-del C3\t"	(nmdb:cache-del "C3"))
+
+(exit)
+