From 767312e2063f4e58af3de0f27aba52de49e14295 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:17:46 +0100 Subject: major reorg that puts Storage (previously Database) into common there is now a top-level tests dir, and a compile time switch for lmdb vs kyotocabinet --- common/storage_kyoto.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 common/storage_kyoto.cpp (limited to 'common/storage_kyoto.cpp') diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp new file mode 100644 index 0000000..05942c2 --- /dev/null +++ b/common/storage_kyoto.cpp @@ -0,0 +1,164 @@ +#include "storage.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +class Storage::Private +{ +public: + Private(const QString &storageRoot, const QString &name); + ~Private(); + + kyotocabinet::TreeDB db; + bool dbOpen; + bool inTransaction; +}; + +Storage::Private::Private(const QString &storageRoot, const QString &name) + : inTransaction(false) +{ + QDir dir; + dir.mkdir(storageRoot); + + //create file + dbOpen = db.open((storageRoot + "/" + name + ".kch").toStdString(), kyotocabinet::BasicDB::OWRITER | kyotocabinet::BasicDB::OCREATE); + if (!dbOpen) { + // TODO: handle error + } +} + +Storage::Private::~Private() +{ + if (dbOpen && inTransaction) { + db.end_transaction(false); + } +} + +Storage::Storage(const QString &storageRoot, const QString &name) + : d(new Private(storageRoot, name)) +{ +} + +Storage::~Storage() +{ + delete d; +} + +bool Storage::isInTransaction() const +{ + return d->inTransaction; +} + +bool Storage::startTransaction(TransactionType type) +{ + if (!d->dbOpen) { + return false; + } + + if (d->inTransaction) { + return true; + } + + //TODO handle errors + d->inTransaction = d->db.begin_transaction(); + return d->inTransaction; +} + +bool Storage::commitTransaction() +{ + if (!d->dbOpen) { + return false; + } + + if (!d->inTransaction) { + return false; + } + + bool success = d->db.end_transaction(true); + d->inTransaction = false; + return success; +} + +void Storage::abortTransaction() +{ + if (!d->dbOpen || !d->inTransaction) { + return; + } + + d->db.end_transaction(false); + d->inTransaction = false; +} + +bool Storage::write(const char *key, size_t keySize, const char *value, size_t valueSize) +{ + if (!d->dbOpen) { + return false; + } + + bool success = d->db.set(key, keySize, value, valueSize); + return success; +} + +bool Storage::write(const std::string &sKey, const std::string &sValue) +{ + if (!d->dbOpen) { + return false; + } + + bool success = d->db.set(sKey, sValue); + return success; +} + +void Storage::read(const std::string &sKey, const std::function &resultHandler) +{ + if (!d->dbOpen) { + return; + } + + std::string value; + if (d->db.get(sKey, &value)) { + resultHandler(value); + } +} + +void Storage::read(const std::string &sKey, const std::function &resultHandler) +{ + if (!d->dbOpen) { + return; + } + + size_t valueSize; + char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); + resultHandler(valueBuffer, valueSize); + delete[] valueBuffer; +} + +qint64 Storage::diskUsage() const +{ + if (!d->dbOpen) { + return 0; + } + + QFileInfo info(QString::fromStdString(d->db.path())); + return info.size(); +} + +void Storage::removeFromDisk() const +{ + if (!d->dbOpen) { + return; + } + + QFileInfo info(QString::fromStdString(d->db.path())); + QDir dir = info.dir(); + dir.remove(info.fileName()); +} -- cgit v1.2.3 From 351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 5 Dec 2014 09:46:53 +0100 Subject: make read return a bool on success not happy with this API, but we need to discuss the whole read thing anyways --- common/storage_kyoto.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'common/storage_kyoto.cpp') diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp index 05942c2..40bd3e6 100644 --- a/common/storage_kyoto.cpp +++ b/common/storage_kyoto.cpp @@ -118,28 +118,34 @@ bool Storage::write(const std::string &sKey, const std::string &sValue) return success; } -void Storage::read(const std::string &sKey, const std::function &resultHandler) +bool Storage::read(const std::string &sKey, const std::function &resultHandler) { if (!d->dbOpen) { - return; + return false; } std::string value; if (d->db.get(sKey, &value)) { resultHandler(value); + return true; } + + return false; } -void Storage::read(const std::string &sKey, const std::function &resultHandler) +bool Storage::read(const std::string &sKey, const std::function &resultHandler) { if (!d->dbOpen) { - return; + return false; } size_t valueSize; char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); - resultHandler(valueBuffer, valueSize); + if (valueBuffer) { + resultHandler(valueBuffer, valueSize); + } delete[] valueBuffer; + return valueBuffer != nullptr; } qint64 Storage::diskUsage() const -- cgit v1.2.3