From 9e1b9e73145e00ea591bd1e0e9777625bad66dc9 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Thu, 3 Jan 2013 07:41:14 +0000 Subject: Add support for HTTP patch and application of patch scripts to server and data store components. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1428192 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/constdb/tinycdb.hpp | 43 ++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'sca-cpp/trunk/components/constdb/tinycdb.hpp') 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 @@ -388,6 +388,37 @@ const failable put(const value& key, const value& val, const TinyCDB& cdb) return r; } +/** + * Patch an item in the database. If the item doesn't exist it is added. + */ +const failable 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(buffer&, const unsigned int, const unsigned int)> update = [ks](buffer& buf, const unsigned int klen, unused const unsigned int vlen) -> const failable { + if (ks == string((char*)buf, klen)) + return false; + return true; + }; + + // Add the new entry to the db + const lambda(struct cdb_make&)> finish = [ks, vs](struct cdb_make& cdbm) -> const failable { + if (cdb_make_add(&cdbm, c_str(ks), (unsigned int)length(ks), c_str(vs), (unsigned int)length(vs)) == -1) + return mkfailure(string("Couldn't add tinycdb entry: ") + ks); + return true; + }; + + // Rewrite the db + const failable r = rewrite(update, finish, cdb); + debug(r, "tinycdb::patch::result"); + return r; +} + /** * Get an item from the database. */ @@ -425,11 +456,14 @@ const failable 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(buffer&, const unsigned int, const unsigned int)> update = [ks](buffer& buf, const unsigned int klen, unused const unsigned int vlen) -> const failable { - if (ks == string((char*)buf, klen)) + const lambda(buffer&, const unsigned int, const unsigned int)> update = [ks, &found](buffer& buf, const unsigned int klen, unused const unsigned int vlen) -> const failable { + if (ks == string((char*)buf, klen)) { + found = true; return false; + } return true; }; @@ -440,6 +474,11 @@ const failable del(const value& key, const TinyCDB& cdb) { // Rewrite the db const failable r = rewrite(update, finish, cdb); + if (!hasContent(r) || !found) { + ostringstream os; + os << "Couldn't delete tinycdb entry: " << key; + return hasContent(r)? mkfailure(str(os), 404, false) : r; + } debug(r, "tinycdb::delete::result"); return r; } -- cgit v1.2.3