summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components/constdb
diff options
context:
space:
mode:
Diffstat (limited to 'sca-cpp/trunk/components/constdb')
-rw-r--r--sca-cpp/trunk/components/constdb/client-test.cpp26
-rw-r--r--sca-cpp/trunk/components/constdb/constdb.cpp35
-rw-r--r--sca-cpp/trunk/components/constdb/tinycdb-test.cpp2
-rw-r--r--sca-cpp/trunk/components/constdb/tinycdb.hpp43
4 files changed, 104 insertions, 2 deletions
diff --git a/sca-cpp/trunk/components/constdb/client-test.cpp b/sca-cpp/trunk/components/constdb/client-test.cpp
index b796ef01dd..165e3d8836 100644
--- a/sca-cpp/trunk/components/constdb/client-test.cpp
+++ b/sca-cpp/trunk/components/constdb/client-test.cpp
@@ -77,6 +77,32 @@ const bool testConstDb() {
assert(hasContent(val));
assert(content(val) == b);
}
+
+ const list<value> k = nilListValue + "content" + (nilListValue + "item"
+ + (nilListValue + "name" + string("Apple"))
+ + (nilListValue + "price" + string("$3.99")));
+ const list<value> c = nilListValue + (nilListValue + "entry"
+ + (nilListValue + "title" + string("item"))
+ + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"))
+ + k);
+
+ {
+ const list<value> s = nilListValue + "content" +
+ (nilListValue + "patch" + string("(define (patch id e) (tree-subst-assoc '(price) '(price \"$3.99\") e))"));
+ const list<value> ps = nilListValue + (nilListValue + "entry"
+ + (nilListValue + "title" + string("item"))
+ + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"))
+ + s);
+
+ const failable<value> r = http::patch(ps, uri + p, cs);
+ assert(hasContent(r));
+ assert(content(r) == trueValue);
+ }
+ {
+ const failable<value> val = http::get(uri + p, cs);
+ assert(hasContent(val));
+ assert(content(val) == c);
+ }
{
const failable<value> r = http::del(uri + p, cs);
assert(hasContent(r));
diff --git a/sca-cpp/trunk/components/constdb/constdb.cpp b/sca-cpp/trunk/components/constdb/constdb.cpp
index a9a5bc5817..2d34ed2b73 100644
--- a/sca-cpp/trunk/components/constdb/constdb.cpp
+++ b/sca-cpp/trunk/components/constdb/constdb.cpp
@@ -63,6 +63,39 @@ const failable<value> put(const list<value>& params, const tinycdb::TinyCDB& cdb
}
/**
+ * Patch an item in the database.
+ */
+const failable<value> patch(const list<value>& params, const tinycdb::TinyCDB& cdb) {
+ // Read patch
+ value p = assoc<value>("patch", assoc<value>("content", car<value>(cadr(params))));
+ if (isNil(p))
+ return mkfailure<value>("Couldn't read patch script");
+ const string script = cadr<value>(p);
+ debug(script, "tinycdb::patch::script");
+ istringstream is(script);
+
+ // Get existing value from database
+ const failable<value> ival = tinycdb::get(car(params), cdb);
+ if (!hasContent(ival) && rcode(ival) != 404)
+ return mkfailure<value>(ival);
+
+ // Apply patch
+ scheme::Env env = scheme::setupEnvironment();
+ const value pval = scheme::evalScript(cons<value>("patch", scheme::quotedParameters(mklist<value>(car(params), hasContent(ival)? content(ival) : (value)list<value>()))), is, env);
+ if (isNil(pval)) {
+ ostringstream os;
+ os << "Couldn't patch tinycdb entry: " << car(params);
+ return mkfailure<value>(str(os), 404, false);
+ }
+
+ // Push patched value to database
+ const failable<bool> val = tinycdb::patch(car(params), pval, cdb);
+ if (!hasContent(val))
+ return mkfailure<value>(val);
+ return value(content(val));
+}
+
+/**
* Delete an item from the database.
*/
const failable<value> del(const list<value>& params, const tinycdb::TinyCDB& cdb) {
@@ -89,6 +122,8 @@ const failable<value> start(unused const list<value>& params) {
return post(cdr(params), cdb);
if (func == "put")
return put(cdr(params), cdb);
+ if (func == "patch")
+ return patch(cdr(params), cdb);
if (func == "delete")
return del(cdr(params), cdb);
return mkfailure<value>();
diff --git a/sca-cpp/trunk/components/constdb/tinycdb-test.cpp b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp
index 6cc9e9eabb..41bfe12772 100644
--- a/sca-cpp/trunk/components/constdb/tinycdb-test.cpp
+++ b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp
@@ -40,6 +40,8 @@ const bool testTinyCDB() {
assert((get(k, cdb)) == value(string("AAA")));
assert(hasContent(put(k, string("aaa"), cdb)));
assert((get(k, cdb)) == value(string("aaa")));
+ assert(hasContent(patch(k, string("bbb"), cdb)));
+ assert((get(k, cdb)) == value(string("bbb")));
assert(hasContent(del(k, cdb)));
assert(!hasContent(get(k, cdb)));
diff --git a/sca-cpp/trunk/components/constdb/tinycdb.hpp b/sca-cpp/trunk/components/constdb/tinycdb.hpp
index ce1dcbb011..3da5f3c216 100644
--- a/sca-cpp/trunk/components/constdb/tinycdb.hpp
+++ b/sca-cpp/trunk/components/constdb/tinycdb.hpp
@@ -389,6 +389,37 @@ const failable<bool> put(const value& key, const value& val, const TinyCDB& cdb)
}
/**
+ * Patch an item in the database. If the item doesn't exist it is added.
+ */
+const failable<bool> patch(const value& key, const value& val, const TinyCDB& cdb) {
+ debug(key, "tinycdb::patch::key");
+ debug(val, "tinycdb::patch::value");
+ debug(dbname(cdb), "tinycdb::patch::dbname");
+
+ const string ks(write(content(scheme::writeValue(key))));
+ const string vs(write(content(scheme::writeValue(val))));
+
+ // Process each entry and skip existing key
+ const lambda<const failable<bool>(buffer&, const unsigned int, const unsigned int)> update = [ks](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
+ const lambda<const failable<bool>(struct cdb_make&)> finish = [ks, vs](struct cdb_make& cdbm) -> const failable<bool> {
+ if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1)
+ return mkfailure<bool>(string("Couldn't add tinycdb entry: ") + ks);
+ return true;
+ };
+
+ // Rewrite the db
+ const failable<bool> r = rewrite(update, finish, cdb);
+ debug(r, "tinycdb::patch::result");
+ return r;
+}
+
+/**
* Get an item from the database.
*/
const failable<value> get(const value& key, const TinyCDB& cdb) {
@@ -425,11 +456,14 @@ const failable<bool> del(const value& key, const TinyCDB& cdb) {
debug(dbname(cdb), "tinycdb::delete::dbname");
const string ks(write(content(scheme::writeValue(key))));
+ bool found = false;
// Process each entry and skip existing key
- const lambda<const failable<bool>(buffer&, const unsigned int, const unsigned int)> update = [ks](buffer& buf, const unsigned int klen, unused const unsigned int vlen) -> const failable<bool> {
- if (ks == string((char*)buf, klen))
+ const lambda<const failable<bool>(buffer&, const unsigned int, const unsigned int)> update = [ks, &found](buffer& buf, const unsigned int klen, unused const unsigned int vlen) -> const failable<bool> {
+ if (ks == string((char*)buf, klen)) {
+ found = true;
return false;
+ }
return true;
};
@@ -440,6 +474,11 @@ const failable<bool> del(const value& key, const TinyCDB& cdb) {
// Rewrite the db
const failable<bool> r = rewrite(update, finish, cdb);
+ if (!hasContent(r) || !found) {
+ ostringstream os;
+ os << "Couldn't delete tinycdb entry: " << key;
+ return hasContent(r)? mkfailure<bool>(str(os), 404, false) : r;
+ }
debug(r, "tinycdb::delete::result");
return r;
}