You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2010/04/18 00:59:51 UTC

svn commit: r935274 - in /tuscany/sca-cpp/trunk: ./ components/ components/nosqldb/ components/sqldb/ etc/

Author: jsdelfino
Date: Sat Apr 17 22:59:51 2010
New Revision: 935274

URL: http://svn.apache.org/viewvc?rev=935274&view=rev
Log:
Strawman implementation of post, put and delete on a tinycdb database, for now just a simplistic rewrite of the db per update.

Added:
    tuscany/sca-cpp/trunk/components/nosqldb/
    tuscany/sca-cpp/trunk/components/nosqldb/Makefile.am
      - copied, changed from r935273, tuscany/sca-cpp/trunk/components/sqldb/Makefile.am
    tuscany/sca-cpp/trunk/components/nosqldb/client-test.cpp
      - copied, changed from r935273, tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp
    tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test   (contents, props changed)
      - copied, changed from r935273, tuscany/sca-cpp/trunk/components/Makefile.am
    tuscany/sca-cpp/trunk/components/nosqldb/server-test   (contents, props changed)
      - copied, changed from r935273, tuscany/sca-cpp/trunk/components/Makefile.am
    tuscany/sca-cpp/trunk/components/nosqldb/store.composite
    tuscany/sca-cpp/trunk/components/nosqldb/tinycdb   (contents, props changed)
      - copied, changed from r935273, tuscany/sca-cpp/trunk/components/Makefile.am
    tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp
    tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp
Modified:
    tuscany/sca-cpp/trunk/components/Makefile.am
    tuscany/sca-cpp/trunk/components/sqldb/Makefile.am
    tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp
    tuscany/sca-cpp/trunk/components/sqldb/pgsql.hpp
    tuscany/sca-cpp/trunk/configure.ac
    tuscany/sca-cpp/trunk/etc/git-exclude

Modified: tuscany/sca-cpp/trunk/components/Makefile.am
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/Makefile.am?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/Makefile.am Sat Apr 17 22:59:51 2010
@@ -15,7 +15,7 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-SUBDIRS = cache chat log queue sqldb store webservice
+SUBDIRS = cache chat log nosqldb queue sqldb webservice
 
 includedir = $(prefix)/include/components
 nobase_include_HEADERS = */*.hpp

Copied: tuscany/sca-cpp/trunk/components/nosqldb/Makefile.am (from r935273, tuscany/sca-cpp/trunk/components/sqldb/Makefile.am)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/Makefile.am?p2=tuscany/sca-cpp/trunk/components/nosqldb/Makefile.am&p1=tuscany/sca-cpp/trunk/components/sqldb/Makefile.am&r1=935273&r2=935274&rev=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/sqldb/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/Makefile.am Sat Apr 17 22:59:51 2010
@@ -15,29 +15,28 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-if WANT_SQLDB
+if WANT_NOSQLDB
 
-INCLUDES = -I${PGSQL_INCLUDE}
+INCLUDES = -I${TINYCDB_INCLUDE}
 
-#comp_SCRIPTS = pgsql-start pgsql-stop
-compdir=$(prefix)/components/sqldb
+compdir=$(prefix)/components/nosqldb
 
-comp_DATA = pgsql.prefix
-pgsql.prefix: $(top_builddir)/config.status
-	echo ${PGSQL_PREFIX} >pgsql.prefix
+comp_DATA = tinycdb.prefix
+tinycdb.prefix: $(top_builddir)/config.status
+	echo ${TINYCDB_PREFIX} >tinycdb.prefix
 
-comp_LTLIBRARIES = libsqldb.la
+#comp_LTLIBRARIES = libnosqldb.la
 
-libsqldb_la_SOURCES = sqldb.cpp
-libsqldb_la_LDFLAGS = -L${PGSQL_LIB} -R${PGSQL_LIB} -lpq
+#libnosqldb_la_SOURCES = nosqldb.cpp
+#libnosqldb_la_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb
 
-pgsql_test_SOURCES = pgsql-test.cpp
-pgsql_test_LDFLAGS = -L${PGSQL_LIB} -R${PGSQL_LIB} -lpq
+tinycdb_test_SOURCES = tinycdb-test.cpp
+tinycdb_test_LDFLAGS = -L${TINYCDB_LIB} -R${TINYCDB_LIB} -lcdb
 
 client_test_SOURCES = client-test.cpp
 client_test_LDFLAGS = -lxml2 -lcurl -lmozjs
 
-noinst_PROGRAMS = pgsql-test client-test
-TESTS = sqldb-test server-test
+noinst_PROGRAMS = tinycdb-test client-test
+TESTS = nosqldb-test 
 
 endif

Copied: tuscany/sca-cpp/trunk/components/nosqldb/client-test.cpp (from r935273, tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/client-test.cpp?p2=tuscany/sca-cpp/trunk/components/nosqldb/client-test.cpp&p1=tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp&r1=935273&r2=935274&rev=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/client-test.cpp Sat Apr 17 22:59:51 2010
@@ -20,7 +20,7 @@
 /* $Rev$ $Date$ */
 
 /**
- * Test SQLDB component.
+ * Test NoSQL database component.
  */
 
 #include <assert.h>
@@ -34,11 +34,11 @@
 #include "../../modules/http/curl.hpp"
 
 namespace tuscany {
-namespace sqldb {
+namespace nosqldb {
 
-const string uri("http://localhost:8090/sqldb");
+const string uri("http://localhost:8090/nosqldb");
 
-bool testSqlDb() {
+bool testNoSqlDb() {
     http::CURLSession cs;
 
     const list<value> i = list<value>()
@@ -110,7 +110,7 @@ bool testGetPerf() {
     const string p = path(content(id));
 
     const lambda<bool()> gl = getLoop(p, a, cs);
-    cout << "Sqldb get test " << time(gl, 5, 200) << " ms" << endl;
+    cout << "NoSqldb get test " << time(gl, 5, 200) << " ms" << endl;
 
     return true;
 }
@@ -121,8 +121,8 @@ bool testGetPerf() {
 int main() {
     tuscany::cout << "Testing..." << tuscany::endl;
 
-    tuscany::sqldb::testSqlDb();
-    tuscany::sqldb::testGetPerf();
+    tuscany::nosqldb::testNoSqlDb();
+    tuscany::nosqldb::testGetPerf();
 
     tuscany::cout << "OK" << tuscany::endl;
 

Copied: tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test (from r935273, tuscany/sca-cpp/trunk/components/Makefile.am)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test?p2=tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test&p1=tuscany/sca-cpp/trunk/components/Makefile.am&r1=935273&r2=935274&rev=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test Sat Apr 17 22:59:51 2010
@@ -1,3 +1,5 @@
+#!/bin/sh
+
 #  Licensed to the Apache Software Foundation (ASF) under one
 #  or more contributor license agreements.  See the NOTICE file
 #  distributed with this work for additional information
@@ -15,8 +17,16 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-SUBDIRS = cache chat log queue sqldb store webservice
+# Setup
+mkdir -p tmp
+rm -f tmp/db.cdb
+cat >tmp/db.txt <<EOF
+EOF
+./tinycdb -c -m tmp/db.cdb <tmp/db.txt
 
-includedir = $(prefix)/include/components
-nobase_include_HEADERS = */*.hpp
+# Test
+./tinycdb-test # 2>/dev/null
+rc=$?
 
+# Cleanup
+return $rc

Propchange: tuscany/sca-cpp/trunk/components/nosqldb/nosqldb-test
------------------------------------------------------------------------------
    svn:executable = *

Copied: tuscany/sca-cpp/trunk/components/nosqldb/server-test (from r935273, tuscany/sca-cpp/trunk/components/Makefile.am)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/server-test?p2=tuscany/sca-cpp/trunk/components/nosqldb/server-test&p1=tuscany/sca-cpp/trunk/components/Makefile.am&r1=935273&r2=935274&rev=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/server-test Sat Apr 17 22:59:51 2010
@@ -1,3 +1,5 @@
+#!/bin/sh
+
 #  Licensed to the Apache Software Foundation (ASF) under one
 #  or more contributor license agreements.  See the NOTICE file
 #  distributed with this work for additional information
@@ -15,8 +17,27 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-SUBDIRS = cache chat log queue sqldb store webservice
+# Setup
+../../modules/http/httpd-conf tmp localhost 8090 ../../modules/http/htdocs
+../../modules/server/server-conf tmp
+../../modules/server/scheme-conf tmp
+cat >>tmp/conf/httpd.conf <<EOF
+SCAContribution `pwd`/
+SCAComposite sqldb.composite
+EOF
+
+./pgsql-start tmp
+./pgsql "drop table test;" 1>/dev/null 2>&1
+./pgsql "create table test(key text, value text);" 1>/dev/null 2>&1
+../../modules/http/httpd-start tmp
+sleep 2
 
-includedir = $(prefix)/include/components
-nobase_include_HEADERS = */*.hpp
+# Test
+./client-test 2>/dev/null
+rc=$?
 
+# Cleanup
+../../modules/http/httpd-stop tmp
+./pgsql-stop tmp
+sleep 2
+return $rc

Propchange: tuscany/sca-cpp/trunk/components/nosqldb/server-test
------------------------------------------------------------------------------
    svn:executable = *

Added: tuscany/sca-cpp/trunk/components/nosqldb/store.composite
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/store.composite?rev=935274&view=auto
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/store.composite (added)
+++ tuscany/sca-cpp/trunk/components/nosqldb/store.composite Sat Apr 17 22:59:51 2010
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+-->
+<composite xmlns="http://docs.oasis-open.org/ns/opencsa/sca/200912"
+  xmlns:t="http://tuscany.apache.org/xmlns/sca/1.1"
+  targetNamespace="http://tuscany.apache.org/xmlns/sca/components"
+  name="store">
+        
+    <component name="store">
+        <implementation.cpp path=".libs" library="libstore"/>
+        <service name="store">
+            <t:binding.http uri="store"/>
+        </service>
+    </component>     
+
+</composite>

Copied: tuscany/sca-cpp/trunk/components/nosqldb/tinycdb (from r935273, tuscany/sca-cpp/trunk/components/Makefile.am)
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/tinycdb?p2=tuscany/sca-cpp/trunk/components/nosqldb/tinycdb&p1=tuscany/sca-cpp/trunk/components/Makefile.am&r1=935273&r2=935274&rev=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/nosqldb/tinycdb Sat Apr 17 22:59:51 2010
@@ -1,3 +1,5 @@
+#!/bin/sh
+
 #  Licensed to the Apache Software Foundation (ASF) under one
 #  or more contributor license agreements.  See the NOTICE file
 #  distributed with this work for additional information
@@ -15,8 +17,8 @@
 #  specific language governing permissions and limitations
 #  under the License.
 
-SUBDIRS = cache chat log queue sqldb store webservice
+here=`readlink -f $0`; here=`dirname $here`
+tinycdb_prefix=`cat $here/tinycdb.prefix`
 
-includedir = $(prefix)/include/components
-nobase_include_HEADERS = */*.hpp
+$tinycdb_prefix/bin/cdb $*
 

Propchange: tuscany/sca-cpp/trunk/components/nosqldb/tinycdb
------------------------------------------------------------------------------
    svn:executable = *

Added: tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp?rev=935274&view=auto
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp (added)
+++ tuscany/sca-cpp/trunk/components/nosqldb/tinycdb-test.cpp Sat Apr 17 22:59:51 2010
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/* $Rev$ $Date$ */
+
+/**
+ * Test TinyCDB access functions.
+ */
+
+#include <assert.h>
+#include "stream.hpp"
+#include "string.hpp"
+#include "perf.hpp"
+#include "tinycdb.hpp"
+
+namespace tuscany {
+namespace tinycdb {
+
+bool testTinyCDB() {
+    TinyCDB cdb("tmp/db.cdb");
+    const value k = mklist<value>("a");
+
+    assert(hasContent(post(k, string("AAA"), cdb)));
+    assert((get(k, cdb)) == value(string("AAA")));
+    assert(hasContent(put(k, string("aaa"), cdb)));
+    assert((get(k, cdb)) == value(string("aaa")));
+    assert(hasContent(del(k, cdb)));
+    assert(!hasContent(get(k, cdb)));
+
+    return true;
+}
+
+struct getLoop {
+    const value k;
+    TinyCDB& cdb;
+    getLoop(const value& k, TinyCDB& cdb) : k(k), cdb(cdb) {
+    }
+    const bool operator()() const {
+        assert((get(k, cdb)) == value(string("CCC")));
+        return true;
+    }
+};
+
+bool testGetPerf() {
+    const value k = mklist<value>("c");
+    TinyCDB cdb("tmp/db.cdb");
+    assert(hasContent(post(k, string("CCC"), cdb)));
+
+    const lambda<bool()> gl = getLoop(k, cdb);
+    cout << "TinyCDB get test " << time(gl, 5, 100000) << " ms" << endl;
+    return true;
+}
+
+}
+}
+
+int main() {
+    tuscany::cout << "Testing..." << tuscany::endl;
+
+    tuscany::tinycdb::testTinyCDB();
+    tuscany::tinycdb::testGetPerf();
+
+    tuscany::cout << "OK" << tuscany::endl;
+
+    return 0;
+}

Added: tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp?rev=935274&view=auto
==============================================================================
--- tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp (added)
+++ tuscany/sca-cpp/trunk/components/nosqldb/tinycdb.hpp Sat Apr 17 22:59:51 2010
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/* $Rev$ $Date$ */
+
+#ifndef tuscany_tinycdb_hpp
+#define tuscany_tinycdb_hpp
+
+#include <fcntl.h>
+#include <cdb.h>
+
+#include "string.hpp"
+#include "list.hpp"
+#include "value.hpp"
+#include "monad.hpp"
+#include "../../modules/scheme/eval.hpp"
+
+namespace tuscany {
+namespace tinycdb {
+
+/**
+ * A reallocatable buffer.
+ */
+class buffer {
+public:
+    operator void*() const throw() {
+        return buf;
+    }
+
+    operator unsigned char*() const throw() {
+        return (unsigned char*)buf;
+    }
+
+    operator char*() const throw() {
+        return (char*)buf;
+    }
+
+private:
+    buffer(const unsigned int size, void* buf) : size(size), buf(buf) {
+    }
+
+    unsigned int size;
+    void* buf;
+
+    friend const buffer mkbuffer(const unsigned int sz);
+    friend const buffer mkbuffer(const buffer& b, const unsigned int newsz);
+    friend const bool free(const buffer& b);
+};
+
+/**
+ * Make a new buffer.
+ */
+const buffer mkbuffer(const unsigned int sz) {
+    return buffer(sz, malloc(sz));
+}
+
+/**
+ * Make a new buffer by reallocating an existing one.
+ */
+const buffer mkbuffer(const buffer& b, const unsigned int sz) {
+    if (sz <= b.size)
+        return b;
+    return buffer(sz, realloc(b.buf, sz));
+}
+
+/**
+ * Free a buffer.
+ */
+const bool free(const buffer&b) {
+    ::free(b.buf);
+    return true;
+}
+
+/**
+ * Represents a TinyCDB connection.
+ */
+class TinyCDB {
+public:
+    TinyCDB() : owner(false) {
+    }
+
+    TinyCDB(const string& file) : owner(true), file(file) {
+        fd = open(c_str(file), O_RDONLY);
+    }
+
+    TinyCDB(const TinyCDB& c) : owner(false) {
+        file = c.file;
+        fd = c.fd;
+    }
+
+    ~TinyCDB() {
+        if (!owner)
+            return;
+        if (fd == -1)
+            return;
+        close(fd);
+    }
+
+private:
+    bool owner;
+    string file;
+    int fd;
+
+    friend const failable<bool> post(const value& key, const value& val, TinyCDB& cdb);
+    friend const failable<bool> put(const value& key, const value& val, const TinyCDB& cdb);
+    friend const failable<value> get(const value& key, const TinyCDB& cdb);
+    friend const failable<bool> del(const value& key, const TinyCDB& cdb);
+    friend 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);
+    friend 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 fd, TinyCDB& cdb);
+};
+
+/**
+ * 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 fd, TinyCDB& cdb) {
+
+    // Initialize new db structure
+    struct cdb_make cdbm;
+    cdb_make_start(&cdbm, fd);
+
+    // Read the db header
+    unsigned int pos = 0;
+    if (lseek(cdb.fd, 0, SEEK_SET) != 0)
+        return mkfailure<bool>("Could not seek to database start");
+    if (::read(cdb.fd, buf, 2048) != 2048)
+        return mkfailure<bool>("Could not read database header");
+    pos += 2048;
+    unsigned int eod = cdb_unpack(buf);
+    debug(pos, "tinycdb::post::eod");
+
+    // Read and add the existing entries
+    while(pos < eod) {
+        if (eod - pos < 8)
+            return mkfailure<bool>("Invalid database format, couldn't read entry header");
+        if (::read(cdb.fd, buf, 8) != 8)
+            return mkfailure<bool>("Couldn't read 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 database format, couldn't read entry");
+        if ((unsigned int)::read(cdb.fd, buf, elen) != elen)
+            return mkfailure<bool>("Couldn't read entry");
+        pos += elen;
+
+        // Apply the update function to the entry
+        debug(string((char*)buf, klen), "tinycdb::post::existing key");
+        debug(string(((char*)buf) + klen, vlen), "tinycdb::post::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 entry");
+    }
+    if (pos != eod)
+        return mkfailure<bool>("Invalid 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 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) {
+    if (cdb.fd == -1)
+        return mkfailure<bool>("Could not open database");
+
+    // Create a new temporary db file
+    const char* tmpfile = c_str(cdb.file + ".tmp");
+    unlink(tmpfile);
+    int fd = open(tmpfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0666);
+    if (fd == -1)
+        return mkfailure<bool>("Could not create temporary database");
+
+    // Rewrite the db, apply the update function to each entry
+    buffer buf = mkbuffer(2048);
+    const failable<bool> r = rewrite(update, finish, buf, fd, cdb);
+    if (!hasContent(r)) {
+        close(fd);
+        free(buf);
+        return r;
+    }
+
+    // Atomically replace the db and reopen it in read mode
+    if (rename(tmpfile, c_str(cdb.file)) == -1)
+        return mkfailure<bool>("Could not rename temporary database");
+    close(cdb.fd);
+    cdb.fd = open(c_str(cdb.file), O_RDONLY);
+
+    return true;
+}
+
+/**
+ * Post a new item to the database.
+ */
+const failable<bool> post(const value& key, const value& val, TinyCDB& cdb) {
+    debug(key, "tinycdb::post::key");
+    debug(val, "tinycdb::post::value");
+
+    const string ks(scheme::writeValue(key));
+    const string vs(scheme::writeValue(val));
+
+    // Process each entry and detect existing key
+    auto update = [=](buffer& buf, const unsigned int klen, unused const unsigned int vlen)->const failable<bool> {
+        if (ks == string((char*)buf, klen))
+            return mkfailure<bool>("Key already exists");
+        return true;
+    };
+
+    // Add the new entry to the db
+    auto finish = [=](struct cdb_make& cdbm)->const failable<bool> {
+        if (cdb_make_add(&cdbm, c_str(ks), length(ks), c_str(vs), length(vs)) == -1)
+            return mkfailure<bool>("Could not add entry");
+        return true;
+    };
+
+    // Rewrite the db
+    const failable<bool> r = rewrite(update, finish, cdb);
+    debug(r, "tinycdb::post::result");
+    return r;
+}
+
+/**
+ * Update an item in the database. If the item doesn't exist it is added.
+ */
+const failable<bool> put(const value& key, const value& val, TinyCDB& cdb) {
+    debug(key, "tinycdb::put::key");
+    debug(val, "tinycdb::put::value");
+
+    const string ks(scheme::writeValue(key));
+    const string vs(scheme::writeValue(val));
+
+    // Process each entry and skip existing key
+    auto update = [&](buffer& buf, const unsigned int klen, unused const unsigned int vlen)->const failable<bool> {
+        if (ks == string((char*)buf, klen))
+            return false;
+        return true;
+    };
+
+    // Add the new entry to the db
+    auto finish = [&](struct cdb_make& cdbm)->const failable<bool> {
+        if (cdb_make_add(&cdbm, c_str(ks), length(ks), c_str(vs), length(vs)) == -1)
+            return mkfailure<bool>("Could not add entry");
+        return true;
+    };
+
+    // Rewrite the db
+    const failable<bool> r = rewrite(update, finish, cdb);
+    debug(r, "tinycdb::put::result");
+    return r;
+}
+
+/**
+ * Get an item from the database.
+ */
+const failable<value> get(const value& key, const TinyCDB& cdb) {
+    debug(key, "tinycdb::get::key");
+    if (cdb.fd == -1)
+        return mkfailure<value>("Could not open database");
+
+    const string ks(scheme::writeValue(key));
+
+    cdbi_t vlen;
+    if (cdb_seek(cdb.fd, c_str(ks), length(ks), &vlen) <= 0)
+        return mkfailure<value>("Could not get entry");
+    char* data = gc_cnew(vlen + 1);
+    cdb_bread(cdb.fd, data, vlen);
+    data[vlen] = '\0';
+    const value val(scheme::readValue(string(data)));
+
+    debug(val, "tinycdb::get::result");
+    return val;
+}
+
+/**
+ * Delete an item from the database
+ */
+const failable<bool> del(const value& key, TinyCDB& cdb) {
+    debug(key, "tinycdb::delete::key");
+
+    const string ks(scheme::writeValue(key));
+
+    // Process each entry and skip existing key
+    auto update = [=](buffer& buf, const unsigned int klen, unused const unsigned int vlen)->const failable<bool> {
+        if (ks == string((char*)buf, klen))
+            return false;
+        return true;
+    };
+
+    // Nothing to do to finish
+    auto finish = [=](unused struct cdb_make& cdbm)->const failable<bool> {
+        // hack: reference a variable from outer scope to workaround GCC internal error
+        const string xs(ks);
+        return true;
+    };
+
+    // Rewrite the db
+    const failable<bool> r = rewrite(update, finish, cdb);
+    debug(r, "tinycdb::delete::result");
+    return r;
+}
+
+}
+}
+
+#endif /* tuscany_tinycdb_hpp */

Modified: tuscany/sca-cpp/trunk/components/sqldb/Makefile.am
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/sqldb/Makefile.am?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/sqldb/Makefile.am (original)
+++ tuscany/sca-cpp/trunk/components/sqldb/Makefile.am Sat Apr 17 22:59:51 2010
@@ -19,7 +19,7 @@ if WANT_SQLDB
 
 INCLUDES = -I${PGSQL_INCLUDE}
 
-#comp_SCRIPTS = pgsql-start pgsql-stop
+comp_SCRIPTS = pgsql-start pgsql-stop
 compdir=$(prefix)/components/sqldb
 
 comp_DATA = pgsql.prefix

Modified: tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp (original)
+++ tuscany/sca-cpp/trunk/components/sqldb/client-test.cpp Sat Apr 17 22:59:51 2010
@@ -20,7 +20,7 @@
 /* $Rev$ $Date$ */
 
 /**
- * Test SQLDB component.
+ * Test SQL database component.
  */
 
 #include <assert.h>

Modified: tuscany/sca-cpp/trunk/components/sqldb/pgsql.hpp
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/components/sqldb/pgsql.hpp?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/components/sqldb/pgsql.hpp (original)
+++ tuscany/sca-cpp/trunk/components/sqldb/pgsql.hpp Sat Apr 17 22:59:51 2010
@@ -22,6 +22,10 @@
 #ifndef tuscany_pgsql_hpp
 #define tuscany_pgsql_hpp
 
+/**
+ * PostgreSQL access functions.
+ */
+
 #include <libpq-fe.h>
 
 #include "string.hpp"

Modified: tuscany/sca-cpp/trunk/configure.ac
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/configure.ac?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/configure.ac (original)
+++ tuscany/sca-cpp/trunk/configure.ac Sat Apr 17 22:59:51 2010
@@ -697,9 +697,9 @@ AC_CONFIG_FILES([Makefile
                  components/cache/Makefile
                  components/log/Makefile
                  components/chat/Makefile
+                 components/nosqldb/Makefile
                  components/queue/Makefile
                  components/sqldb/Makefile
-                 components/store/Makefile
                  components/webservice/Makefile
                  samples/Makefile
                  test/Makefile

Modified: tuscany/sca-cpp/trunk/etc/git-exclude
URL: http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/etc/git-exclude?rev=935274&r1=935273&r2=935274&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/etc/git-exclude (original)
+++ tuscany/sca-cpp/trunk/etc/git-exclude Sat Apr 17 22:59:51 2010
@@ -97,4 +97,5 @@ axis2-test
 qpid-test
 xmpp-test
 pgsql-test
+tinycdb-test