diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-12-25 01:49:19 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-12-25 01:49:19 +0000 |
commit | dc15dc32bb3348c760ba3643c083af7e0c8e43fe (patch) | |
tree | 2823b9165d5c3a8a1bc96b3d071f86101bb995b4 /sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp | |
parent | 088b2e47386078c79781a448e0b6e458ddaae23c (diff) |
Create a sandbox branch to experiment with latest APR.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1052740 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp')
-rw-r--r-- | sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp b/sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp new file mode 100644 index 0000000000..89ff3b8157 --- /dev/null +++ b/sandbox/sebastien/cpp/apr-2/components/filedb/filedb.hpp @@ -0,0 +1,225 @@ +/* + * 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_filedb_hpp +#define tuscany_filedb_hpp + +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> + +#include "string.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "fstream.hpp" +#include "element.hpp" +#include "xml.hpp" +#include "../../modules/scheme/eval.hpp" +#include "../../modules/json/json.hpp" + +namespace tuscany { +namespace filedb { + +/** + * Represents a FileDB connection. + */ +class FileDB { +public: + FileDB() : owner(false), jscx(*jscontext("")) { + } + + FileDB(const string& name, const string& format) : owner(true), name(name), format(format), jscx(*jscontext(format)) { + } + + FileDB(const FileDB& c) : owner(false), name(c.name), format(c.format), jscx(c.jscx) { + } + + ~FileDB() { + } + +private: + bool owner; + string name; + string format; + js::JSContext& jscx; + + js::JSContext* jscontext(const string& format) { + if (format != "json") + return NULL; + return new (gc_new<js::JSContext>()) js::JSContext(); + } + + friend const failable<bool> write(const value& v, ostream& os, const string& format, FileDB& db); + friend const failable<value> read(istream& is, const string& format, FileDB& db); + friend const failable<bool> post(const value& key, const value& val, FileDB& db); + friend const failable<bool> put(const value& key, const value& val, FileDB& db); + friend const failable<value> get(const value& key, FileDB& db); + friend const failable<bool> del(const value& key, FileDB& db); +}; + +/** + * Convert a key to a file name. + */ +const string filename(const list<value>& path, const string& root) { + if (isNil(path)) + return root; + string name = root + "/" + scheme::writeValue(car(path)); + return filename(cdr(path), name); +} + +const string filename(const value& key, const string& root) { + if (!isList(key)) + return filename(mklist(key), root); + return filename((list<value>)key, root); +} + +/** + * Make the parent directories of a keyed file. + */ +const failable<bool> mkdirs(const list<value>& path, const string& root) { + if (isNil(cdr(path))) + return true; + string dir = root + "/" + scheme::writeValue(car(path)); + mkdir(c_str(dir), S_IRWXU); + return mkdirs(cdr(path), dir); +} + +/** + * Write a value to a database file. + */ +const failable<bool> write(const value& v, ostream& os, const string& format, FileDB& db) { + if (format == "scheme") { + const string vs(scheme::writeValue(v)); + os << vs; + return true; + } + if (format == "xml") { + failable<list<string> > s = writeXML(valuesToElements(v)); + if (!hasContent(s)) + return mkfailure<bool>(reason(s)); + write(content(s), os); + return true; + } + if (format == "json") { + failable<list<string> > s = json::writeJSON(valuesToElements(v), db.jscx); + if (!hasContent(s)) + return mkfailure<bool>(reason(s)); + write(content(s), os); + return true; + } + return mkfailure<bool>(string("Unsupported database format: ") + format); +} + +/** + * Read a value from a database file. + */ +const failable<value> read(istream& is, const string& format, FileDB& db) { + if (format == "scheme") { + return scheme::readValue(is); + } + if (format == "xml") { + const value v = elementsToValues(readXML(streamList(is))); + return v; + } + if (format == "json") { + const failable<list<value> > fv = json::readJSON(streamList(is), db.jscx); + if (!hasContent(fv)) + return mkfailure<value>(reason(fv)); + const value v = elementsToValues(content(fv)); + return v; + } + return mkfailure<value>(string("Unsupported database format: ") + format); +} + +/** + * Post a new item to the database. + */ +const failable<bool> post(const value& key, const value& val, FileDB& db) { + debug(key, "filedb::post::key"); + debug(val, "filedb::post::value"); + debug(db.name, "filedb::post::dbname"); + + if (isList(key)) + mkdirs(key, db.name); + ofstream os(filename(key, db.name)); + if (os.fail()) + return mkfailure<bool>("Couldn't post file database entry."); + const failable<bool> r = write(val, os, db.format, db); + + debug(r, "filedb::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, FileDB& db) { + debug(key, "filedb::put::key"); + debug(val, "filedb::put::value"); + debug(db.name, "filedb::put::dbname"); + + if (isList(key)) + mkdirs(key, db.name); + ofstream os(filename(key, db.name)); + if (os.fail()) + return mkfailure<bool>("Couldn't put file database entry."); + const failable<bool> r = write(val, os, db.format, db); + + debug(r, "filedb::put::result"); + return r; +} + +/** + * Get an item from the database. + */ +const failable<value> get(const value& key, FileDB& db) { + debug(key, "filedb::get::key"); + debug(db.name, "filedb::get::dbname"); + + ifstream is(filename(key, db.name)); + if (is.fail()) + return mkfailure<value>("Couldn't get file database entry."); + const failable<value> val = read(is, db.format, db); + + debug(val, "filedb::get::result"); + return val; +} + +/** + * Delete an item from the database + */ +const failable<bool> del(const value& key, FileDB& db) { + debug(key, "filedb::delete::key"); + debug(db.name, "filedb::delete::dbname"); + + const int rc = unlink(c_str(filename(key, db.name))); + if (rc == -1) + return mkfailure<bool>("Couldn't delete file database entry."); + + debug(true, "filedb::delete::result"); + return true; +} + +} +} + +#endif /* tuscany_filedb_hpp */ |