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 --- store/CMakeLists.txt | 1 - store/database.cpp | 164 ---------------------------------------- store/database.h | 28 ------- store/test/CMakeLists.txt | 21 ----- store/test/calendar.fbs | 12 --- store/test/storagebenchmark.cpp | 157 -------------------------------------- 6 files changed, 383 deletions(-) delete mode 100644 store/CMakeLists.txt delete mode 100644 store/database.cpp delete mode 100644 store/database.h delete mode 100644 store/test/CMakeLists.txt delete mode 100644 store/test/calendar.fbs delete mode 100644 store/test/storagebenchmark.cpp (limited to 'store') diff --git a/store/CMakeLists.txt b/store/CMakeLists.txt deleted file mode 100644 index 552439e..0000000 --- a/store/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(test) diff --git a/store/database.cpp b/store/database.cpp deleted file mode 100644 index 542667a..0000000 --- a/store/database.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/database.h b/store/database.h deleted file mode 100644 index e752ff5..0000000 --- a/store/database.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/CMakeLists.txt b/store/test/CMakeLists.txt deleted file mode 100644 index 1b9dc9e..0000000 --- a/store/test/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -set(CMAKE_AUTOMOC ON) -include_directories(${CMAKE_CURRENT_BINARY_DIR}) - -set(store_path "../") -set(store_SRCS - ${store_path}/database.cpp -) - -generate_flatbuffers(calendar) - -macro(manual_tests) - foreach(_testname ${ARGN}) - add_executable(${_testname} ${_testname}.cpp ${store_SRCS}) - qt5_use_modules(${_testname} Core Test) - target_link_libraries(${_testname} kyotocabinet) - endforeach(_testname) -endmacro(auto_tests) - -manual_tests ( - storagebenchmark -) diff --git a/store/test/calendar.fbs b/store/test/calendar.fbs deleted file mode 100644 index 203ee43..0000000 --- a/store/test/calendar.fbs +++ /dev/null @@ -1,12 +0,0 @@ -// example IDL file - -namespace Calendar; - -table Event { - summary:string; - description:string; - attachment:[byte]; -} - -root_type Event; -file_identifier "AKFB"; diff --git a/store/test/storagebenchmark.cpp b/store/test/storagebenchmark.cpp deleted file mode 100644 index d42dea8..0000000 --- a/store/test/storagebenchmark.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include - -#include "calendar_generated.h" - -#include -#include - -#include -#include -#include -#include - -#include "store/database.h" - -using namespace Calendar; -using namespace flatbuffers; - -static std::string createEvent() -{ - FlatBufferBuilder fbb; - { - auto summary = fbb.CreateString("summary"); - - const int attachmentSize = 1024*2; // 2KB - int8_t rawData[attachmentSize]; - auto data = fbb.CreateVector(rawData, attachmentSize); - - Calendar::EventBuilder eventBuilder(fbb); - eventBuilder.add_summary(summary); - eventBuilder.add_attachment(data); - auto eventLocation = eventBuilder.Finish(); - FinishEventBuffer(fbb, eventLocation); - } - return std::string(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize()); -} - -// static void readEvent(const std::string &data) -// { -// auto readEvent = GetEvent(data.c_str()); -// std::cout << readEvent->summary()->c_str() << std::endl; -// } - -class StorageBenchmark : 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; - QString filePath; - const int count = 50000; - -private Q_SLOTS: - void initTestCase() - { - testDataPath = "./testdb"; - dbName = "test"; - filePath = testDataPath + "buffer.fb"; - } - - void testWriteRead_data() - { - QTest::addColumn("useDb"); - QTest::addColumn("count"); - - QTest::newRow("db, 50k") << true << count; - QTest::newRow("file, 50k") << false << count; - } - - void testWriteRead() - { - QFETCH(bool, useDb); - QFETCH(int, count); - - Database *db = 0; - if (useDb) { - db = new Database(testDataPath, dbName); - } - - std::ofstream myfile; - myfile.open(filePath.toStdString()); - const char *keyPrefix = "key"; - - QTime time; - - time.start(); - { - auto event = createEvent(); - for (int i = 0; i < count; i++) { - if (db) { - if (i % 10000 == 0) { - if (i > 0) { - db->commitTransaction(); - } - db->startTransaction(); - } - - db->write(keyPrefix + std::to_string(i), event); - } else { - myfile << event; - } - } - - if (db) { - db->commitTransaction(); - } else { - myfile.close(); - } - } - const int writeDuration = time.restart(); - qDebug() << "Writing took[ms]: " << writeDuration; - - { - for (int i = 0; i < count; i++) { - if (db) { - db->read(keyPrefix + std::to_string(i), [](std::string value){}); - } - } - } - const int readDuration = time.restart(); - - if (db) { - qDebug() << "Reading took[ms]: " << readDuration; - } else { - qDebug() << "File reading is not implemented."; - } - - delete db; - } - - void testBufferCreation() - { - QTime time; - - time.start(); - { - for (int i = 0; i < count; i++) { - auto event = createEvent(); - } - } - const int bufferDuration = time.elapsed(); - qDebug() << "Creating buffers took[ms]: " << bufferDuration; - } - - void testSizes() - { - Database db(testDataPath, dbName); - qDebug() << "Database size [kb]: " << db.diskUsage()/1024; - db.removeFromDisk(); - - QFileInfo fileInfo(filePath); - qDebug() << "File size [kb]: " << fileInfo.size()/1024; - } -}; - -QTEST_MAIN(StorageBenchmark) -#include "storagebenchmark.moc" -- cgit v1.2.3