From 36adc76235fb0a38e7042bc751f988b71627e2a0 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Tue, 11 Dec 2012 06:13:02 +0000 Subject: Changes to get successful C++11 based build. Code cleanup, dependency upgrades, and const + inline optimizations in components, samples, and app hosting server. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1420007 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/constdb/Makefile.am | 2 +- sca-cpp/trunk/components/constdb/client-test.cpp | 71 +++---- sca-cpp/trunk/components/constdb/constdb.cpp | 39 ++-- sca-cpp/trunk/components/constdb/tinycdb-test.cpp | 24 +-- sca-cpp/trunk/components/constdb/tinycdb.hpp | 248 +++++++++------------- 5 files changed, 158 insertions(+), 226 deletions(-) (limited to 'sca-cpp/trunk/components/constdb') diff --git a/sca-cpp/trunk/components/constdb/Makefile.am b/sca-cpp/trunk/components/constdb/Makefile.am index e4504a53e7..a64d13da1c 100644 --- a/sca-cpp/trunk/components/constdb/Makefile.am +++ b/sca-cpp/trunk/components/constdb/Makefile.am @@ -41,7 +41,7 @@ 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 +client_test_LDFLAGS = -lxml2 -lcurl -ljansson dist_noinst_SCRIPTS = constdb-test server-test noinst_PROGRAMS = tinycdb-test client-test diff --git a/sca-cpp/trunk/components/constdb/client-test.cpp b/sca-cpp/trunk/components/constdb/client-test.cpp index ea45762cd6..b796ef01dd 100644 --- a/sca-cpp/trunk/components/constdb/client-test.cpp +++ b/sca-cpp/trunk/components/constdb/client-test.cpp @@ -38,15 +38,15 @@ namespace constdb { const string uri("http://localhost:8090/constdb"); -bool testConstDb() { - http::CURLSession cs("", "", "", "", 0); - - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$2.99"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) +const bool testConstDb() { + const http::CURLSession cs("", "", "", "", 0); + + const list i = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$2.99"))); + const list a = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + i); const failable id = http::post(a, uri, cs); @@ -59,18 +59,18 @@ bool testConstDb() { assert(content(val) == a); } - const list j = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$3.55"))); - const list b = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + const list j = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$3.55"))); + const list b = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + j); { const failable r = http::put(b, uri + p, cs); assert(hasContent(r)); - assert(content(r) == value(true)); + assert(content(r) == trueValue); } { const failable val = http::get(uri + p, cs); @@ -80,7 +80,7 @@ bool testConstDb() { { const failable r = http::del(uri + p, cs); assert(hasContent(r)); - assert(content(r) == value(true)); + assert(content(r) == trueValue); } { const failable val = http::get(uri + p, cs); @@ -90,35 +90,26 @@ bool testConstDb() { return true; } -struct getLoop { - const string path; - const value entry; - http::CURLSession& cs; - getLoop(const string& path, const value& entry, http::CURLSession& cs) : path(path), entry(entry), cs(cs) { - } - const bool operator()() const { - const failable val = http::get(uri + path, cs); - assert(hasContent(val)); - assert(content(val) == entry); - return true; - } -}; - -bool testGetPerf() { - const list i = list() + "content" + (list() + "item" - + (list() + "name" + string("Apple")) - + (list() + "price" + string("$4.55"))); - const list a = list() + (list() + "entry" - + (list() + "title" + string("item")) - + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) +const bool testGetPerf() { + const list i = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$4.55"))); + const list a = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + i); - http::CURLSession cs("", "", "", "", 0); + const http::CURLSession cs("", "", "", "", 0); const failable id = http::post(a, uri, cs); assert(hasContent(id)); const string p = path(content(id)); - const lambda gl = getLoop(p, a, cs); + const blambda gl = [p, a, cs]() -> const bool { + const failable val = http::get(uri + p, cs); + assert(hasContent(val)); + assert(content(val) == a); + return true; + }; cout << "ConstDb get test " << time(gl, 5, 200) << " ms" << endl; return true; diff --git a/sca-cpp/trunk/components/constdb/constdb.cpp b/sca-cpp/trunk/components/constdb/constdb.cpp index 6d1cb15baf..a9a5bc5817 100644 --- a/sca-cpp/trunk/components/constdb/constdb.cpp +++ b/sca-cpp/trunk/components/constdb/constdb.cpp @@ -37,14 +37,14 @@ namespace constdb { /** * Get an item from the database. */ -const failable get(const list& params, tinycdb::TinyCDB& cdb) { +const failable get(const list& params, const tinycdb::TinyCDB& cdb) { return tinycdb::get(car(params), cdb); } /** * Post an item to the database. */ -const failable post(const list& params, tinycdb::TinyCDB& cdb) { +const failable post(const list& params, const tinycdb::TinyCDB& cdb) { const value id = append(car(params), mklist(mkuuid())); const failable val = tinycdb::post(id, cadr(params), cdb); if (!hasContent(val)) @@ -55,7 +55,7 @@ const failable post(const list& params, tinycdb::TinyCDB& cdb) { /** * Put an item into the database. */ -const failable put(const list& params, tinycdb::TinyCDB& cdb) { +const failable put(const list& params, const tinycdb::TinyCDB& cdb) { const failable val = tinycdb::put(car(params), cadr(params), cdb); if (!hasContent(val)) return mkfailure(val); @@ -65,7 +65,7 @@ const failable put(const list& params, tinycdb::TinyCDB& cdb) { /** * Delete an item from the database. */ -const failable del(const list& params, tinycdb::TinyCDB& cdb) { +const failable del(const list& params, const tinycdb::TinyCDB& cdb) { const failable val = tinycdb::del(car(params), cdb); if (!hasContent(val)) return mkfailure(val); @@ -73,14 +73,15 @@ const failable del(const list& params, tinycdb::TinyCDB& cdb) { } /** - * Component implementation lambda function. + * Start the component. */ -class applyConstDb { -public: - applyConstDb(tinycdb::TinyCDB& cdb) : cdb(cdb) { - } +const failable start(unused const list& params) { + // Connect to the configured database and table + const value dbname = ((lvvlambda)car(params))(nilListValue); + const tinycdb::TinyCDB& cdb = *(new (gc_new()) tinycdb::TinyCDB(dbname)); - const value operator()(const list& params) const { + // Return the component implementation lambda function + const lvvlambda applyConstDb = [cdb](const list& params) -> const value { const value func(car(params)); if (func == "get") return get(cdr(params), cdb); @@ -91,22 +92,8 @@ public: if (func == "delete") return del(cdr(params), cdb); return mkfailure(); - } - -private: - tinycdb::TinyCDB& cdb; -}; - -/** - * Start the component. - */ -const failable start(unused const list& params) { - // Connect to the configured database and table - const value dbname = ((lambda&)>)car(params))(list()); - tinycdb::TinyCDB& cdb = *(new (gc_new()) tinycdb::TinyCDB(dbname)); - - // Return the component implementation lambda function - return value(lambda&)>(applyConstDb(cdb))); + }; + return value(applyConstDb); } } diff --git a/sca-cpp/trunk/components/constdb/tinycdb-test.cpp b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp index b3b4ea7fd7..6cc9e9eabb 100644 --- a/sca-cpp/trunk/components/constdb/tinycdb-test.cpp +++ b/sca-cpp/trunk/components/constdb/tinycdb-test.cpp @@ -32,8 +32,8 @@ namespace tuscany { namespace tinycdb { -bool testTinyCDB() { - TinyCDB cdb("tmp/test.cdb"); +const bool testTinyCDB() { + const TinyCDB cdb("tmp/test.cdb"); const value k = mklist("a"); assert(hasContent(post(k, string("AAA"), cdb))); @@ -46,23 +46,15 @@ bool testTinyCDB() { 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 bool testGetPerf() { const value k = mklist("c"); - TinyCDB cdb("tmp/test.cdb"); + const TinyCDB cdb("tmp/test.cdb"); assert(hasContent(post(k, string("CCC"), cdb))); - const lambda gl = getLoop(k, cdb); + const blambda gl = [k, cdb]() -> const bool { + assert((get(k, cdb)) == value(string("CCC"))); + return true; + }; cout << "TinyCDB get test " << time(gl, 5, 100000) << " ms" << endl; return true; } diff --git a/sca-cpp/trunk/components/constdb/tinycdb.hpp b/sca-cpp/trunk/components/constdb/tinycdb.hpp index 68be0a09dc..ce1dcbb011 100644 --- a/sca-cpp/trunk/components/constdb/tinycdb.hpp +++ b/sca-cpp/trunk/components/constdb/tinycdb.hpp @@ -41,20 +41,20 @@ namespace tinycdb { */ class buffer { public: - operator void*() const throw() { + operator void* const () const throw() { return buf; } - operator unsigned char*() const throw() { - return (unsigned char*)buf; + operator unsigned char* const () const throw() { + return (unsigned char* const)buf; } - operator char*() const throw() { - return (char*)buf; + operator char* const () const throw() { + return (char* const)buf; } private: - buffer(const unsigned int size, void* buf) : size(size), buf(buf) { + buffer(const unsigned int size, void* const buf) : size(size), buf(buf) { } unsigned int size; @@ -106,48 +106,53 @@ const string absdbname(const string& name) { */ class TinyCDB { public: - TinyCDB() : owner(false), fd(-1) { - st.st_ino = 0; + TinyCDB() : owner(false), h(*(new (gc_new()) handles())) { } - TinyCDB(const string& name) : owner(true), name(absdbname(name)), fd(-1) { + TinyCDB(const string& name) : owner(true), name(absdbname(name)), h(*(new (gc_new()) handles())) { debug(name, "tinycdb::tinycdb::name"); - st.st_ino = 0; } - TinyCDB(const TinyCDB& c) : owner(false), name(c.name), fd(c.fd) { + TinyCDB(const TinyCDB& c) : owner(false), name(c.name), h(c.h) { debug("tinycdb::tinycdb::copy"); - st.st_ino = c.st.st_ino; } - const TinyCDB& operator=(const TinyCDB& c) { - debug("tinycdb::tinycdb::operator="); - if(this == &c) - return *this; - owner = false; - name = c.name; - fd = c.fd; - st.st_ino = c.st.st_ino; - return *this; - } + TinyCDB& operator=(const TinyCDB& c) = delete; ~TinyCDB() { if (!owner) return; - if (fd == -1) + if (h.fd == -1) return; - close(fd); + close(h.fd); } private: + class handles { + public: + handles() : fd(-1) { + st.st_ino = 0; + } + + handles(const handles& c) : fd(c.fd), st(c.st) { + } + + private: + int fd; + struct stat st; + + friend class TinyCDB; + friend const failable cdbopen(const TinyCDB& cdb); + friend const failable cdbclose(const TinyCDB& cdb); + }; + bool owner; string name; - int fd; - struct stat st; + handles& h; friend const string dbname(const TinyCDB& cdb); - friend const failable cdbopen(TinyCDB& cdb); - friend const failable cdbclose(TinyCDB& cdb); + friend const failable cdbopen(const TinyCDB& cdb); + friend const failable cdbclose(const TinyCDB& cdb); }; /** @@ -160,7 +165,7 @@ const string dbname(const TinyCDB& cdb) { /** * Open a database. */ -const failable cdbopen(TinyCDB& cdb) { +const failable cdbopen(const TinyCDB& cdb) { // Get database file serial number struct stat st; @@ -169,51 +174,51 @@ const failable cdbopen(TinyCDB& cdb) { return mkfailure(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) + if (cdb.h.fd == -1) { + cdb.h.fd = open(c_str(cdb.name), O_RDONLY); + if (cdb.h.fd == -1) return mkfailure(string("Couldn't open tinycdb database file: ") + cdb.name); - debug(cdb.fd, "tinycdb::open::fd"); - cdb.st = st; - return cdb.fd; + debug(cdb.h.fd, "tinycdb::open::fd"); + cdb.h.st = st; + return cdb.h.fd; } // Close and reopen database after a change - if (st.st_ino != cdb.st.st_ino) { + if (st.st_ino != cdb.h.st.st_ino) { // Close current fd - close(cdb.fd); + close(cdb.h.fd); // Reopen database const int newfd = open(c_str(cdb.name), O_RDONLY); if (newfd == -1) return mkfailure(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; + if (newfd == cdb.h.fd) { + debug(cdb.h.fd, "tinycdb::open::fd"); + cdb.h.st = st; + return cdb.h.fd; } // We got a different fd, dup it to the current fd then close it - if (fcntl(newfd, F_DUPFD, cdb.fd) == -1) + if (fcntl(newfd, F_DUPFD, cdb.h.fd) == -1) return mkfailure(string("Couldn't dup tinycdb database file handle: ") + cdb.name); close(newfd); - debug(cdb.fd, "tinycdb::open::fd"); - cdb.st = st; - return cdb.fd; + debug(cdb.h.fd, "tinycdb::open::fd"); + cdb.h.st = st; + return cdb.h.fd; } // No change, just return the current fd - return cdb.fd; + return cdb.h.fd; } /** * Close a database. */ -const failable cdbclose(TinyCDB& cdb) { - close(cdb.fd); - cdb.fd = -1; +const failable cdbclose(const TinyCDB& cdb) { + close(cdb.h.fd); + cdb.h.fd = -1; return true; } @@ -222,7 +227,7 @@ const failable cdbclose(TinyCDB& cdb) { * can return true to let the entry added to the new db, false to skip the * entry, or a failure. */ -const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, buffer& buf, const int tmpfd, TinyCDB& cdb) { +const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, buffer& buf, const int tmpfd, const TinyCDB& cdb) { // Initialize new db structure struct cdb_make cdbm; @@ -241,7 +246,7 @@ const failable rewrite(const lambda(buffer& buf, const unsi if (::read(fd, buf, 2048) != 2048) return mkfailure("Couldn't read tinycdb database header"); pos += 2048; - unsigned int eod = cdb_unpack(buf); + const unsigned int eod = cdb_unpack(buf); debug(pos, "tinycdb::rewrite::eod"); // Read and add the existing entries @@ -251,9 +256,9 @@ const failable rewrite(const lambda(buffer& buf, const unsi if (::read(fd, buf, 8) != 8) return mkfailure("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; + const unsigned int klen = cdb_unpack(buf); + const unsigned int vlen = cdb_unpack(((unsigned char*)buf) + 4); + const unsigned int elen = klen + vlen; // Read existing entry buf = mkbuffer(buf, elen); @@ -264,8 +269,8 @@ const failable rewrite(const lambda(buffer& buf, const unsi 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"); + debug(string((const char* const)buf, klen), "tinycdb::rewrite::existing key"); + debug(string(((const char* const)buf) + klen, vlen), "tinycdb::rewrite::existing value"); const failable u = update(buf, klen, vlen); if (!hasContent(u)) return u; @@ -293,11 +298,11 @@ const failable rewrite(const lambda(buffer& buf, const unsi return true; } -const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, TinyCDB& cdb) { +const failable rewrite(const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)>& update, const lambda(struct cdb_make&)>& finish, const TinyCDB& cdb) { // Create a new temporary db file - string tmpname = dbname(cdb) + ".XXXXXX"; - int tmpfd = mkstemp(const_cast(c_str(tmpname))); + const string tmpname = dbname(cdb) + ".XXXXXX"; + const int tmpfd = mkstemp(const_cast(c_str(tmpname))); if (tmpfd == -1) return mkfailure("Couldn't create temporary tinycdb database"); @@ -314,7 +319,7 @@ const failable rewrite(const lambda(buffer& buf, const unsi if (rename(c_str(tmpname), c_str(dbname(cdb))) == -1) return mkfailure("Couldn't rename temporary tinycdb database"); cdbclose(cdb); - failable ffd = cdbopen(cdb); + const failable ffd = cdbopen(cdb); if (!hasContent(ffd)) return mkfailure(ffd); @@ -324,42 +329,27 @@ const failable rewrite(const lambda(buffer& buf, const unsi /** * Post a new item to the database. */ -struct postUpdate { - const string ks; - postUpdate(const string& ks) : ks(ks) { - } - const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { - if (ks == string((char*)buf, klen)) - return mkfailure("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 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(string("Couldn't add tinycdb entry: ") + ks); - return true; - } -}; - -const failable post(const value& key, const value& val, TinyCDB& cdb) { +const failable post(const value& key, const value& val, const TinyCDB& cdb) { debug(key, "tinycdb::post::key"); debug(val, "tinycdb::post::value"); debug(dbname(cdb), "tinycdb::post::dbname"); - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); + const string ks(write(content(scheme::writeValue(key)))); + const string vs(write(content(scheme::writeValue(val)))); // Process each entry and detect existing key - const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = postUpdate(ks); + 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 mkfailure("Key already exists in tinycdb database"); + return true; + }; // Add the new entry to the db - const lambda(struct cdb_make& cdbm)> finish = postFinish(ks, vs); + 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); @@ -370,42 +360,27 @@ const failable post(const value& key, const value& val, TinyCDB& cdb) { /** * 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 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 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(string("Couldn't add tinycdb entry: ") + ks); - return true; - } -}; - -const failable put(const value& key, const value& val, TinyCDB& cdb) { +const failable put(const value& key, const value& val, const TinyCDB& cdb) { debug(key, "tinycdb::put::key"); debug(val, "tinycdb::put::value"); debug(dbname(cdb), "tinycdb::put::dbname"); - const string ks(scheme::writeValue(key)); - const string vs(scheme::writeValue(val)); + 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& buf, const unsigned int klen, const unsigned int vlen)> update = putUpdate(ks); + 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& cdbm)> finish = putFinish(ks, vs); + 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); @@ -416,7 +391,7 @@ const failable put(const value& key, const value& val, TinyCDB& cdb) { /** * Get an item from the database. */ -const failable get(const value& key, TinyCDB& cdb) { +const failable get(const value& key, const TinyCDB& cdb) { debug(key, "tinycdb::get::key"); debug(dbname(cdb), "tinycdb::get::dbname"); @@ -425,7 +400,7 @@ const failable get(const value& key, TinyCDB& cdb) { return mkfailure(ffd); const int fd = content(ffd); - const string ks(scheme::writeValue(key)); + const string ks(write(content(scheme::writeValue(key)))); cdbi_t vlen; if (cdb_seek(fd, c_str(ks), (unsigned int)length(ks), &vlen) <= 0) { @@ -433,10 +408,10 @@ const failable get(const value& key, TinyCDB& cdb) { os << "Couldn't get tinycdb entry: " << key; return mkfailure(str(os), 404, false); } - char* data = gc_cnew(vlen + 1); + char* const data = gc_cnew(vlen + 1); cdb_bread(fd, data, vlen); data[vlen] = '\0'; - const value val(scheme::readValue(string(data))); + const value val(content(scheme::readValue(string(data)))); debug(val, "tinycdb::get::result"); return val; @@ -445,36 +420,23 @@ const failable get(const value& key, TinyCDB& cdb) { /** * Delete an item from the database */ -struct delUpdate { - const string ks; - delUpdate(const string& ks) : ks(ks) { - } - const failable operator()(buffer& buf, const unsigned int klen, unused const unsigned int vlen) const { - if (ks == string((char*)buf, klen)) - return false; - return true; - } -}; - -struct delFinish { - delFinish() { - } - const failable operator()(unused struct cdb_make& cdbm) const { - return true; - } -}; - -const failable del(const value& key, TinyCDB& cdb) { +const failable del(const value& key, const TinyCDB& cdb) { debug(key, "tinycdb::delete::key"); debug(dbname(cdb), "tinycdb::delete::dbname"); - const string ks(scheme::writeValue(key)); + const string ks(write(content(scheme::writeValue(key)))); // Process each entry and skip existing key - const lambda(buffer& buf, const unsigned int klen, const unsigned int vlen)> update = delUpdate(ks); + 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; + }; // Nothing to do to finish - const lambda(struct cdb_make& cdbm)> finish = delFinish(); + const lambda(struct cdb_make&)> finish = [](unused struct cdb_make& cdbm) -> const failable { + return true; + }; // Rewrite the db const failable r = rewrite(update, finish, cdb); -- cgit v1.2.3