From 3db27ea82c735feb1e7821cd083201ab5bdbc5be Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Sat, 6 Dec 2014 02:53:17 +0100 Subject: crept back in during post-branch-merge conflict fixing apparently --- store/kyotodatabase.cpp | 164 --------------------------------------------- store/kyotodatabase.h | 28 -------- store/test/storagetest.cpp | 113 ------------------------------- 3 files changed, 305 deletions(-) delete mode 100644 store/kyotodatabase.cpp delete mode 100644 store/kyotodatabase.h delete mode 100644 store/test/storagetest.cpp (limited to 'store') diff --git a/store/kyotodatabase.cpp b/store/kyotodatabase.cpp deleted file mode 100644 index 542667a..0000000 --- a/store/kyotodatabase.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "database.h" - -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -class Database::Private -{ -public: - Private(const QString &storageRoot, const QString &name); - ~Private(); - - kyotocabinet::TreeDB db; - bool dbOpen; - bool inTransaction; -}; - -Database::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 - } -} - -Database::Private::~Private() -{ - if (dbOpen && inTransaction) { - db.end_transaction(false); - } -} - -Database::Database(const QString &storageRoot, const QString &name) - : d(new Private(storageRoot, name)) -{ -} - -Database::~Database() -{ - delete d; -} - -bool Database::isInTransaction() const -{ - return d->inTransaction; -} - -bool Database::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 Database::commitTransaction() -{ - if (!d->dbOpen) { - return false; - } - - if (!d->inTransaction) { - return false; - } - - bool success = d->db.end_transaction(true); - d->inTransaction = false; - return success; -} - -void Database::abortTransaction() -{ - if (!d->dbOpen || !d->inTransaction) { - return; - } - - d->db.end_transaction(false); - d->inTransaction = false; -} - -bool Database::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 Database::write(const std::string &sKey, const std::string &sValue) -{ - if (!d->dbOpen) { - return false; - } - - bool success = d->db.set(sKey, sValue); - return success; -} - -void Database::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 Database::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 Database::diskUsage() const -{ - if (!d->dbOpen) { - return 0; - } - - QFileInfo info(QString::fromStdString(d->db.path())); - return info.size(); -} - -void Database::removeFromDisk() const -{ - if (!d->dbOpen) { - return; - } - - QFileInfo info(QString::fromStdString(d->db.path())); - QDir dir = info.dir(); - dir.remove(info.fileName()); -} diff --git a/store/kyotodatabase.h b/store/kyotodatabase.h deleted file mode 100644 index e752ff5..0000000 --- a/store/kyotodatabase.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -class Database { -public: - enum TransactionType { ReadOnly, ReadWrite }; - - Database(const QString &storageRoot, const QString &name); - ~Database(); - bool isInTransaction() const; - bool startTransaction(TransactionType type = ReadWrite); - bool commitTransaction(); - void abortTransaction(); - bool write(const char *key, size_t keySize, const char *value, size_t valueSize); - bool write(const std::string &sKey, const std::string &sValue); - //Perhaps prefer iterators (assuming we need to be able to match multiple values - void read(const std::string &sKey, const std::function &); - void read(const std::string &sKey, const std::function &); - - qint64 diskUsage() const; - void removeFromDisk() const; -private: - class Private; - Private * const d; -}; - diff --git a/store/test/storagetest.cpp b/store/test/storagetest.cpp deleted file mode 100644 index dba4f6c..0000000 --- a/store/test/storagetest.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include - -#include - -#include -#include -#include - -#include "store/database.h" - -class StorageTest : public QObject -{ - Q_OBJECT -private: - //This should point to a directory on disk and not a ramdisk (since we're measuring performance) - QString testDataPath; - QString dbName; - const char *keyPrefix = "key"; - - void populate(int count) - { - Database db(testDataPath, dbName); - for (int i = 0; i < count; i++) { - //This should perhaps become an implementation detail of the db? - if (i % 10000 == 0) { - if (i > 0) { - db.commitTransaction(); - } - db.startTransaction(); - } - db.write(keyPrefix + std::to_string(i), keyPrefix + std::to_string(i)); - } - db.commitTransaction(); - } - - bool verify(Database &db, int i) - { - bool error = false; - const auto reference = keyPrefix + std::to_string(i); - if(!db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { - if (value != reference) { - qDebug() << "Mismatch while reading"; - error = true; - } - })) { - return false; - } - return !error; - } - -private Q_SLOTS: - void initTestCase() - { - testDataPath = "./testdb"; - dbName = "test"; - } - - void cleanupTestCase() - { - Database db(testDataPath, dbName); - db.removeFromDisk(); - } - - - void testRead() - { - const int count = 100; - - populate(count); - - //ensure we can read everything back correctly - { - Database db(testDataPath, dbName); - for (int i = 0; i < count; i++) { - QVERIFY(verify(db, i)); - } - } - - Database db(testDataPath, dbName); - db.removeFromDisk(); - } - - void testConcurrentRead() - { - const int count = 10000; - - populate(count); - - //Try to concurrently read - QList > futures; - const int concurrencyLevel = 4; - for (int num = 0; num < concurrencyLevel; num++) { - futures << QtConcurrent::run([this, count](){ - Database db(testDataPath, dbName); - for (int i = 0; i < count; i++) { - if (!verify(db, i)) { - qWarning() << "invalid value"; - break; - } - } - }); - } - for(auto future : futures) { - future.waitForFinished(); - } - - Database db(testDataPath, dbName); - db.removeFromDisk(); - } -}; - -QTEST_MAIN(StorageTest) -#include "storagetest.moc" -- cgit v1.2.3