tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r1172029 - /tuscany/sca-cpp/trunk/components/kvdb/
Date Sat, 17 Sep 2011 17:44:56 GMT
Author: rfeng
Date: Sat Sep 17 17:44:55 2011
New Revision: 1172029

URL: http://svn.apache.org/viewvc?rev=1172029&view=rev
Log:
Start to add LevelDB support

Added:
    tuscany/sca-cpp/trunk/components/kvdb/
      - copied from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/
    tuscany/sca-cpp/trunk/components/kvdb/kvdb-test
      - copied unchanged from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test
    tuscany/sca-cpp/trunk/components/kvdb/kvdb.componentType
      - copied, changed from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.componentType
    tuscany/sca-cpp/trunk/components/kvdb/kvdb.composite
      - copied, changed from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.composite
    tuscany/sca-cpp/trunk/components/kvdb/kvdb.cpp
      - copied, changed from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.cpp
    tuscany/sca-cpp/trunk/components/kvdb/leveldb
      - copied, changed from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/tinycdb
    tuscany/sca-cpp/trunk/components/kvdb/leveldb-test.cpp
      - copied unchanged from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp
    tuscany/sca-cpp/trunk/components/kvdb/leveldb.hpp
      - copied, changed from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp
Removed:
    tuscany/sca-cpp/trunk/components/kvdb/nosqldb-test
    tuscany/sca-cpp/trunk/components/kvdb/nosqldb.componentType
    tuscany/sca-cpp/trunk/components/kvdb/nosqldb.composite
    tuscany/sca-cpp/trunk/components/kvdb/nosqldb.cpp
    tuscany/sca-cpp/trunk/components/kvdb/tinycdb
    tuscany/sca-cpp/trunk/components/kvdb/tinycdb-test.cpp
    tuscany/sca-cpp/trunk/components/kvdb/tinycdb.hpp
Modified:
    tuscany/sca-cpp/trunk/components/kvdb/Makefile.am
    tuscany/sca-cpp/trunk/components/kvdb/client-test.cpp
    tuscany/sca-cpp/trunk/components/kvdb/server-test

Modified: tuscany/sca-cpp/trunk/components/kvdb/Makefile.am
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/Makefile.am?rev=1172029&r1=1154780&r2=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/kvdb/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/Makefile.am Sat Sep 17 17:44:55 2011
@@ -15,35 +15,35 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-INCLUDES = -I${TINYCDB_INCLUDE}
+INCLUDES = -I${LEVELDB_INCLUDE}
 
 incl_HEADERS = *.hpp
-incldir = $(prefix)/include/components/nosqldb
+incldir = $(prefix)/include/components/kvdb
 
-dist_comp_SCRIPTS = tinycdb
-compdir=$(prefix)/components/nosqldb
+dist_comp_SCRIPTS = leveldb
+compdir=$(prefix)/components/kvdb
 
-comp_DATA = tinycdb.prefix
-tinycdb.prefix: $(top_builddir)/config.status
-	echo ${TINYCDB_PREFIX} >tinycdb.prefix
+comp_DATA = leveldb.prefix
+leveldb.prefix: $(top_builddir)/config.status
+	echo ${LEVELDB_PREFIX} >leveldb.prefix
 
-EXTRA_DIST = nosqldb.composite nosqldb.componentType
+EXTRA_DIST = kvdb.composite kvdb.componentType
 
-comp_LTLIBRARIES = libnosqldb.la
-noinst_DATA = libnosqldb${libsuffix}
+comp_LTLIBRARIES = libkvdb.la
+noinst_DATA = libkvdb${libsuffix}
 
-libnosqldb_la_SOURCES = nosqldb.cpp
-libnosqldb_la_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb
-libnosqldb${libsuffix}:
-	ln -s .libs/libnosqldb${libsuffix}
+libkvdb_la_SOURCES = kvdb.cpp
+libkvdb_la_LDFLAGS = -L${LEVELDB_LIB} -R${LEVELDB_LIB} -lleveldb
+libkvdb${libsuffix}:
+	ln -s .libs/libkvdb${libsuffix}
 
-tinycdb_test_SOURCES = tinycdb-test.cpp
-tinycdb_test_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb
+leveldb_test_SOURCES = leveldb-test.cpp
+leveldb_test_LDFLAGS = -L${LEVELDB_LIB} -R${LEVELDB_LIB} -lleveldb
 
 client_test_SOURCES = client-test.cpp
 client_test_LDFLAGS = -lxml2 -lcurl -lmozjs
 
-dist_noinst_SCRIPTS = nosqldb-test server-test
-noinst_PROGRAMS = tinycdb-test client-test
-TESTS = nosqldb-test server-test
+dist_noinst_SCRIPTS = kvdb-test server-test
+noinst_PROGRAMS = leveldb-test client-test
+TESTS = kvdb-test server-test
 

Modified: tuscany/sca-cpp/trunk/components/kvdb/client-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/client-test.cpp?rev=1172029&r1=1154780&r2=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/kvdb/client-test.cpp (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/client-test.cpp Sat Sep 17 17:44:55 2011
@@ -20,7 +20,7 @@
 /* $Rev$ $Date$ */
 
 /**
- * Test NoSQL database component.
+ * Test KV (Key-Value) database component.
  */
 
 #include <assert.h>

Copied: tuscany/sca-cpp/trunk/components/kvdb/kvdb.componentType (from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.componentType)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/kvdb.componentType?p2=tuscany/sca-cpp/trunk/components/kvdb/kvdb.componentType&p1=tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.componentType&r1=1154780&r2=1172029&rev=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.componentType (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/kvdb.componentType Sat Sep 17 17:44:55 2011
@@ -22,7 +22,7 @@
   xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1"
   targetNamespace="http://tuscany.apache.org/xmlns/sca/components">
         
-    <service name="nosqldb"/>
+    <service name="nvdb"/>
     <property name="dbname" type="xsd:string"/>
 
-</composite>
+</componentType>

Copied: tuscany/sca-cpp/trunk/components/kvdb/kvdb.composite (from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.composite)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/kvdb.composite?p2=tuscany/sca-cpp/trunk/components/kvdb/kvdb.composite&p1=tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.composite&r1=1154780&r2=1172029&rev=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.composite (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/kvdb.composite Sat Sep 17 17:44:55 2011
@@ -19,13 +19,13 @@
 -->
 <composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
   targetNamespace="http://tuscany.apache.org/xmlns/sca/components"
-  name="nosqldb">
+  name="nvdb">
         
-    <component name="nosqldb">
-        <implementation.cpp path="." library="libnosqldb"/>
+    <component name="nvdb">
+        <implementation.cpp path="." library="libnvdb"/>
         <property name="dbname">tmp/test.cdb</property>
-        <service name="nosqldb">
-            <binding.http uri="nosqldb"/>
+        <service name="nvdb">
+            <binding.http uri="nvdb"/>
         </service>
     </component>     
 

Copied: tuscany/sca-cpp/trunk/components/kvdb/kvdb.cpp (from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.cpp)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/kvdb.cpp?p2=tuscany/sca-cpp/trunk/components/kvdb/kvdb.cpp&p1=tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.cpp&r1=1154780&r2=1172029&rev=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/nosqldb.cpp (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/kvdb.cpp Sat Sep 17 17:44:55 2011
@@ -20,7 +20,7 @@
 /* $Rev$ $Date$ */
 
 /**
- * TinyCDB-based database component implementation.
+ * LevelDB-based database component implementation.
  */
 
 #include "string.hpp"
@@ -28,24 +28,24 @@
 #include "list.hpp"
 #include "value.hpp"
 #include "monad.hpp"
-#include "tinycdb.hpp"
+#include "leveldb.hpp"
 
 namespace tuscany {
-namespace nosqldb {
+namespace nvdb {
 
 /**
  * Get an item from the database.
  */
-const failable<value> get(const list<value>& params, tinycdb::TinyCDB&
cdb) {
-    return tinycdb::get(car(params), cdb);
+const failable<value> get(const list<value>& params, leveldb::LevelDB&
cdb) {
+    return leveldb::get(car(params), cdb);
 }
 
 /**
  * Post an item to the database.
  */
-const failable<value> post(const list<value>& params, tinycdb::TinyCDB&
cdb) {
+const failable<value> post(const list<value>& params, leveldb::LevelDB&
cdb) {
     const value id = append<value>(car(params), mklist(mkuuid()));
-    const failable<bool> val = tinycdb::post(id, cadr(params), cdb);
+    const failable<bool> val = leveldb::post(id, cadr(params), cdb);
     if (!hasContent(val))
         return mkfailure<value>(reason(val));
     return id;
@@ -54,8 +54,8 @@ const failable<value> post(const list<va
 /**
  * Put an item into the database.
  */
-const failable<value> put(const list<value>& params, tinycdb::TinyCDB&
cdb) {
-    const failable<bool> val = tinycdb::put(car(params), cadr(params), cdb);
+const failable<value> put(const list<value>& params, leveldb::LevelDB&
cdb) {
+    const failable<bool> val = leveldb::put(car(params), cadr(params), cdb);
     if (!hasContent(val))
         return mkfailure<value>(reason(val));
     return value(content(val));
@@ -64,8 +64,8 @@ const failable<value> put(const list<val
 /**
  * Delete an item from the database.
  */
-const failable<value> del(const list<value>& params, tinycdb::TinyCDB&
cdb) {
-    const failable<bool> val = tinycdb::del(car(params), cdb);
+const failable<value> del(const list<value>& params, leveldb::LevelDB&
cdb) {
+    const failable<bool> val = leveldb::del(car(params), cdb);
     if (!hasContent(val))
         return mkfailure<value>(reason(val));
     return value(content(val));
@@ -76,7 +76,7 @@ const failable<value> del(const list<val
  */
 class applyNoSqldb {
 public:
-    applyNoSqldb(tinycdb::TinyCDB& cdb) : cdb(cdb) {
+    applyNoSqldb(leveldb::LevelDB& cdb) : cdb(cdb) {
     }
 
     const value operator()(const list<value>& params) const {
@@ -93,7 +93,7 @@ public:
     }
 
 private:
-    tinycdb::TinyCDB& cdb;
+    leveldb::LevelDB& cdb;
 };
 
 /**
@@ -102,7 +102,7 @@ private:
 const failable<value> start(unused const list<value>& params) {
     // Connect to the configured database and table
     const value dbname = ((lambda<value(list<value>)>)car(params))(list<value>());
-    tinycdb::TinyCDB& cdb = *(new (gc_new<tinycdb::TinyCDB>()) tinycdb::TinyCDB(dbname));
+    leveldb::LevelDB& cdb = *(new (gc_new<leveldb::LevelDB>()) leveldb::LevelDB(dbname));
 
     // Return the component implementation lambda function
     return value(lambda<value(const list<value>&)>(applyNoSqldb(cdb)));

Copied: tuscany/sca-cpp/trunk/components/kvdb/leveldb (from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/tinycdb)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/leveldb?p2=tuscany/sca-cpp/trunk/components/kvdb/leveldb&p1=tuscany/sca-cpp/trunk/components/nosqldb/tinycdb&r1=1154780&r2=1172029&rev=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/tinycdb (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/leveldb Sat Sep 17 17:44:55 2011
@@ -18,7 +18,7 @@
 #  under the License.
 
 here=`echo "import os; print os.path.realpath('$0')" | python`; here=`dirname $here`
-tinycdb_prefix=`cat $here/tinycdb.prefix`
+leveldb_prefix=`cat $here/leveldb.prefix`
 
-$tinycdb_prefix/bin/cdb $*
+$leveldb_prefix/bin/cdb $*
 

Copied: tuscany/sca-cpp/trunk/components/kvdb/leveldb.hpp (from r1154780, tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/leveldb.hpp?p2=tuscany/sca-cpp/trunk/components/kvdb/leveldb.hpp&p1=tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp&r1=1154780&r2=1172029&rev=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/leveldb.hpp Sat Sep 17 17:44:55 2011
@@ -19,13 +19,13 @@
 
 /* $Rev$ $Date$ */
 
-#ifndef tuscany_tinycdb_hpp
-#define tuscany_tinycdb_hpp
+#ifndef tuscany_leveldb_hpp
+#define tuscany_leveldb_hpp
 
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
-#include <cdb.h>
+#include <leveldb/db.h>
 
 #include "string.hpp"
 #include "list.hpp"
@@ -34,7 +34,7 @@
 #include "../../modules/scheme/eval.hpp"
 
 namespace tuscany {
-namespace tinycdb {
+namespace leveldb {
 
 /**
  * A reallocatable buffer.
@@ -90,27 +90,27 @@ const bool free(const buffer&b) {
 }
 
 /**
- * Represents a TinyCDB connection.
+ * Represents a LevelDB connection.
  */
-class TinyCDB {
+class LevelDB {
 public:
-    TinyCDB() : owner(false), fd(-1) {
-        debug("tinycdb::tinycdb");
+    LevelDB() : owner(false), fd(-1) {
+        debug("leveldb::leveldb");
         st.st_ino = 0;
     }
 
-    TinyCDB(const string& name) : owner(true), name(name), fd(-1) {
-        debug(name, "tinycdb::tinycdb::name");
+    LevelDB(const string& name) : owner(true), name(name), fd(-1) {
+        debug(name, "leveldb::leveldb::name");
         st.st_ino = 0;
     }
 
-    TinyCDB(const TinyCDB& c) : owner(false), name(c.name), fd(c.fd) {
-        debug("tinycdb::tinycdb::copy");
+    LevelDB(const LevelDB& c) : owner(false), name(c.name), fd(c.fd) {
+        debug("leveldb::leveldb::copy");
         st.st_ino = c.st.st_ino;
     }
 
-    ~TinyCDB() {
-        debug("tinycdb::~tinycdb");
+    ~LevelDB() {
+        debug("leveldb::~leveldb");
         if (!owner)
             return;
         if (fd == -1)
@@ -121,300 +121,91 @@ public:
 private:
     bool owner;
     string name;
-    int fd;
+    leveldb::DB* db;
     struct stat st;
 
-    friend const string dbname(const TinyCDB& cdb);
-    friend const failable<int> cdbopen(TinyCDB& cdb);
-    friend const failable<bool> cdbclose(TinyCDB& cdb);
+    friend const string dbname(const LevelDB& db);
+    friend const failable<int> dbopen(LevelDB& db);
+    friend const failable<bool> dbclose(LevelDB& db);
 };
 
 /**
  * Return the name of the database.
  */
-const string dbname(const TinyCDB& cdb) {
-    return cdb.name;
+const string dbname(const LevelDB& db) {
+    return db.name;
 }
 
 /**
  * Open a database.
  */
-const failable<int> cdbopen(TinyCDB& cdb) {
+const failable<int> dbopen(LevelDB& db) {
 
-    // Get database file serial number
-    struct stat st;
-    const int s = stat(c_str(cdb.name), &st);
-    if (s == -1)
-        return mkfailure<int>(string("Couldn't tinycdb read database stat ") + cdb.name);
-
-    // Open database for the first time
-    if (cdb.fd == -1) {
-        cdb.fd = open(c_str(cdb.name), O_RDONLY);
-        if (cdb.fd == -1)
-            return mkfailure<int>(string("Couldn't open tinycdb database file ") +
cdb.name);
-        debug(cdb.fd, "tinycdb::open::fd");
-        cdb.st = st;
-        return cdb.fd;
-    }
-
-    // Close and reopen database after a change
-    if (st.st_ino != cdb.st.st_ino) {
-
-        // Close current fd
-        close(cdb.fd);
-
-        // Reopen database
-        const int newfd = open(c_str(cdb.name), O_RDONLY);
-        if (newfd == -1)
-            return mkfailure<int>(string("Couldn't open tinycdb database file ") +
cdb.name);
-        if (newfd == cdb.fd) {
-            debug(cdb.fd, "tinycdb::open::fd");
-            cdb.st = st;
-            return cdb.fd;
-        }
-
-        // We got a different fd, dup it to the current fd then close it
-        if (fcntl(newfd, F_DUPFD, cdb.fd) == -1)
-            return mkfailure<int>(string("Couldn't dup tinycdb database file handle
") + cdb.name);
-        close(newfd);
-
-        debug(cdb.fd, "tinycdb::open::fd");
-        cdb.st = st;
-        return cdb.fd;
-    }
-
-    // No change, just return the current fd
-    return cdb.fd;
+	// Get database file serial number
+	struct stat st;
+	const int s = stat(c_str(db.name), &st);
+	if (s == -1)
+		return mkfailure<int>(
+				string("Couldn't leveldb read database stat ") + db.name);
+
+	leveldb::DB* ldb;
+	leveldb::Options options;
+	options.create_if_missing = true;
+	leveldb::Status status = leveldb::DB::Open(options, s, &ldb);
+	db.db = ldb;
 }
 
 /**
  * Close a database.
  */
-const failable<bool> cdbclose(TinyCDB& cdb) {
-    close(cdb.fd);
-    cdb.fd = -1;
+const failable<bool> dbclose(LevelDB& db) {
+	delete db.db;
+	db.db = NULL;
     return true;
 }
 
-/**
- * Rewrite a database. The given update function is passed each entry, and
- * can return true to let the entry added to the new db, false to skip the
- * entry, or a failure.
- */
-const failable<bool> rewrite(const lambda<failable<bool>(buffer& buf,
const unsigned int klen, const unsigned int vlen)>& update, const lambda<failable<bool>(struct
cdb_make&)>& finish, buffer& buf, const int tmpfd, TinyCDB& cdb) {
-
-    // Initialize new db structure
-    struct cdb_make cdbm;
-    cdb_make_start(&cdbm, tmpfd);
-
-    // Open existing db
-    failable<int> ffd = cdbopen(cdb);
-    if (!hasContent(ffd))
-        return mkfailure<bool>(reason(ffd));
-    const int fd = content(ffd);
-
-    // Read the db header
-    unsigned int pos = 0;
-    if (lseek(fd, 0, SEEK_SET) != 0)
-        return mkfailure<bool>("Could not seek to tinycdb database start");
-    if (::read(fd, buf, 2048) != 2048)
-        return mkfailure<bool>("Could not read tinycdb database header");
-    pos += 2048;
-    unsigned int eod = cdb_unpack(buf);
-    debug(pos, "tinycdb::rewrite::eod");
-
-    // Read and add the existing entries
-    while(pos < eod) {
-        if (eod - pos < 8)
-            return mkfailure<bool>("Invalid tinycdb database format, couldn't read
entry header");
-        if (::read(fd, buf, 8) != 8)
-            return mkfailure<bool>("Couldn't read tinycdb entry header");
-        pos += 8;
-        unsigned int klen = cdb_unpack(buf);
-        unsigned int vlen = cdb_unpack(((unsigned char*)buf) + 4);
-        unsigned int elen = klen + vlen;
-
-        // Read existing entry
-        buf = mkbuffer(buf, elen);
-        if (eod - pos < elen)
-            return mkfailure<bool>("Invalid tinycdb database format, couldn't read
entry");
-        if ((unsigned int)::read(fd, buf, elen) != elen)
-            return mkfailure<bool>("Couldn't read tinycdb entry");
-        pos += elen;
-
-        // Apply the update function to the entry
-        debug(string((char*)buf, klen), "tinycdb::rewrite::existing key");
-        debug(string(((char*)buf) + klen, vlen), "tinycdb::rewrite::existing value");
-        const failable<bool> u = update(buf, klen, vlen);
-        if (!hasContent(u))
-            return u;
-
-        // Skip the entry if the update function returned false
-        if (u == false)
-            continue;
-
-        // Add the entry to the new db
-        if (cdb_make_add(&cdbm, buf, klen, ((unsigned char*)buf)+klen, vlen) == -1)
-            return mkfailure<bool>("Could not add tinycdb entry");
-    }
-    if (pos != eod)
-        return mkfailure<bool>("Invalid tinycdb database format");
-
-    // Call the finish function
-    const failable<bool> f = finish(cdbm);
-    if (!hasContent(f))
-        return f;
-
-    // Save the new db
-    if (cdb_make_finish(&cdbm) == -1)
-        return mkfailure<bool>("Could not save tinycdb database");
-
-    return true;
-}
-
-const failable<bool> rewrite(const lambda<failable<bool>(buffer& buf,
const unsigned int klen, const unsigned int vlen)>& update, const lambda<failable<bool>(struct
cdb_make&)>& finish, TinyCDB& cdb) {
-
-    // Create a new temporary db file
-    string tmpname = dbname(cdb) + ".XXXXXX";
-    int tmpfd = mkstemp(const_cast<char*>(c_str(tmpname)));
-    if (tmpfd == -1)
-        return mkfailure<bool>("Could not create temporary tinycdb database");
-
-    // Rewrite the db, apply the update function to each entry
-    buffer buf = mkbuffer(2048);
-    const failable<bool> r = rewrite(update, finish, buf, tmpfd, cdb);
-    if (!hasContent(r)) {
-        close(tmpfd);
-        free(buf);
-        return r;
-    }
-
-    // Atomically replace the db and reopen it in read mode
-    if (rename(c_str(tmpname), c_str(dbname(cdb))) == -1)
-        return mkfailure<bool>("Could not rename temporary tinycdb database");
-    cdbclose(cdb);
-    failable<int> ffd = cdbopen(cdb);
-    if (!hasContent(ffd))
-        return mkfailure<bool>(reason(ffd));
-
-    return true;
-}
 
-/**
- * Post a new item to the database.
- */
-struct postUpdate {
-    const string ks;
-    postUpdate(const string& ks) : ks(ks) {
-    }
-    const failable<bool> operator()(buffer& buf, const unsigned int klen, unused
const unsigned int vlen) const {
-        if (ks == string((char*)buf, klen))
-            return mkfailure<bool>("Key already exists in tinycdb database");
-        return true;
-    }
-};
-
-struct postFinish {
-    const string ks;
-    const string vs;
-    postFinish(const string& ks, const string& vs) : ks(ks), vs(vs) {
-    }
-    const failable<bool> operator()(struct cdb_make& cdbm) const {
-        if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned
int)length(vs)) == -1)
-            return mkfailure<bool>("Could not add tinycdb entry");
-        return true;
-    }
-};
-
-const failable<bool> post(const value& key, const value& val, TinyCDB&
cdb) {
-    debug(key, "tinycdb::post::key");
-    debug(val, "tinycdb::post::value");
-    debug(dbname(cdb), "tinycdb::post::dbname");
+const failable<bool> post(const value& key, const value& val, LevelDB&
db) {
+    debug(key, "leveldb::post::key");
+    debug(val, "leveldb::post::value");
+    debug(dbname(db), "leveldb::post::dbname");
 
     const string ks(scheme::writeValue(key));
     const string vs(scheme::writeValue(val));
 
-    // Process each entry and detect existing key
-    const lambda<failable<bool>(buffer& buf, const unsigned int klen, const
unsigned int vlen)> update = postUpdate(ks);
+    put(ks, vs, db);
 
-    // Add the new entry to the db
-    const lambda<failable<bool>(struct cdb_make& cdbm)> finish = postFinish(ks,
vs);
-
-    // Rewrite the db
-    const failable<bool> r = rewrite(update, finish, cdb);
-    debug(r, "tinycdb::post::result");
+    debug(r, "leveldb::post::result");
     return r;
 }
 
-/**
- * Update an item in the database. If the item doesn't exist it is added.
- */
-struct putUpdate {
-    const string ks;
-    putUpdate(const string& ks) : ks(ks) {
-    }
-    const failable<bool> operator()(buffer& buf, const unsigned int klen, unused
const unsigned int vlen) const {
-        if (ks == string((char*)buf, klen))
-            return false;
-        return true;
-    }
-};
-
-struct putFinish {
-    const string ks;
-    const string vs;
-    putFinish(const string& ks, const string& vs) : ks(ks), vs(vs) {
-    }
-    const failable<bool> operator()(struct cdb_make& cdbm) const {
-        if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned
int)length(vs)) == -1)
-            return mkfailure<bool>("Could not add tinycdb entry");
-        return true;
-    }
-};
 
-const failable<bool> put(const value& key, const value& val, TinyCDB& cdb)
{
-    debug(key, "tinycdb::put::key");
-    debug(val, "tinycdb::put::value");
-    debug(dbname(cdb), "tinycdb::put::dbname");
+const failable<bool> put(const value& key, const value& val, LevelDB& db)
{
+    debug(key, "leveldb::put::key");
+    debug(val, "leveldb::put::value");
+    debug(dbname(db), "leveldb::put::dbname");
 
     const string ks(scheme::writeValue(key));
     const string vs(scheme::writeValue(val));
 
-    // Process each entry and skip existing key
-    const lambda<failable<bool>(buffer& buf, const unsigned int klen, const
unsigned int vlen)> update = putUpdate(ks);
-
-    // Add the new entry to the db
-    const lambda<failable<bool>(struct cdb_make& cdbm)> finish = putFinish(ks,
vs);
-
-    // Rewrite the db
-    const failable<bool> r = rewrite(update, finish, cdb);
-    debug(r, "tinycdb::put::result");
+    debug(r, "leveldb::put::result");
     return r;
 }
 
 /**
  * Get an item from the database.
  */
-const failable<value> get(const value& key, TinyCDB& cdb) {
-    debug(key, "tinycdb::get::key");
-    debug(dbname(cdb), "tinycdb::get::dbname");
-
-    const failable<int> ffd = cdbopen(cdb);
-    if (!hasContent(ffd))
-        return mkfailure<value>(reason(ffd));
-    const int fd = content(ffd);
+const failable<value> get(const value& key, LevelDB& db) {
+    debug(key, "leveldb::get::key");
+    debug(dbname(db), "leveldb::get::dbname");
 
     const string ks(scheme::writeValue(key));
 
-    cdbi_t vlen;
-    if (cdb_seek(fd, c_str(ks), (unsigned int)length(ks), &vlen) <= 0)
-        return mkfailure<value>("Could not get tinycdb entry");
-    char* data = gc_cnew(vlen + 1);
-    cdb_bread(fd, data, vlen);
-    data[vlen] = '\0';
+    std::string data;
+    db.db->Get(leveldb::ReadOptions(), key, &data);
     const value val(scheme::readValue(string(data)));
 
-    debug(val, "tinycdb::get::result");
+    debug(val, "leveldb::get::result");
     return val;
 }
 
@@ -435,30 +226,24 @@ struct delUpdate {
 struct delFinish {
     delFinish() {
     }
-    const failable<bool> operator()(unused struct cdb_make& cdbm) const {
+    const failable<bool> operator()(unused struct db_make& dbm) const {
         return true;
     }
 };
 
-const failable<bool> del(const value& key, TinyCDB& cdb) {
-    debug(key, "tinycdb::delete::key");
-    debug(dbname(cdb), "tinycdb::delete::dbname");
+const failable<bool> del(const value& key, LevelDB& db) {
+    debug(key, "leveldb::delete::key");
+    debug(dbname(db), "leveldb::delete::dbname");
 
     const string ks(scheme::writeValue(key));
 
-    // Process each entry and skip existing key
-    const lambda<failable<bool>(buffer& buf, const unsigned int klen, const
unsigned int vlen)> update = delUpdate(ks);
-
-    // Nothing to do to finish
-    const lambda<failable<bool>(struct cdb_make& cdbm)> finish = delFinish();
+    leveldb::Status s = db.db->Delete(leveldb::WriteOptions(), key);
 
-    // Rewrite the db
-    const failable<bool> r = rewrite(update, finish, cdb);
-    debug(r, "tinycdb::delete::result");
-    return r;
+    debug(r, "leveldb::delete::result");
+    return s.ok();
 }
 
 }
 }
 
-#endif /* tuscany_tinycdb_hpp */
+#endif /* tuscany_leveldb_hpp */

Modified: tuscany/sca-cpp/trunk/components/kvdb/server-test
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/kvdb/server-test?rev=1172029&r1=1154780&r2=1172029&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/kvdb/server-test (original)
+++ tuscany/sca-cpp/trunk/components/kvdb/server-test Sat Sep 17 17:44:55 2011
@@ -26,7 +26,7 @@ SCAContribution `pwd`/
 SCAComposite nosqldb.composite
 EOF
 
-./tinycdb -c -m tmp/test.cdb </dev/null
+./leveldb -c -m tmp/test.cdb </dev/null
 ../../modules/http/httpd-start tmp
 sleep 2
 



Mime
View raw message