From 64e82fe5ccfa10e1652c149d10e89b08a5aae165 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 22 Nov 2014 12:05:52 +0100 Subject: Buffertest + store --- buffertest/CMakeLists.txt | 4 +++ buffertest/main.cpp | 41 +++++++++++++++++---- store/database.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++ store/database.h | 16 +++++++++ 4 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 store/database.cpp create mode 100644 store/database.h diff --git a/buffertest/CMakeLists.txt b/buffertest/CMakeLists.txt index 9505b75..fd100d0 100644 --- a/buffertest/CMakeLists.txt +++ b/buffertest/CMakeLists.txt @@ -2,7 +2,10 @@ project(toynadi_buffertest) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) +set(store_path "../store/") + set(toynadinbuffertest_SRCS + ${store_path}/database.cpp main.cpp ) @@ -22,5 +25,6 @@ ADD_CUSTOM_TARGET(generate_buffers ALL DEPENDS ${SCHEMA_SOURCEFILES}) add_executable(${PROJECT_NAME} ${toynadinbuffertest_SRCS}) qt5_use_modules(${PROJECT_NAME} Core) +target_link_libraries(${PROJECT_NAME} lmdb) install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/buffertest/main.cpp b/buffertest/main.cpp index 5bc2326..437e5b3 100644 --- a/buffertest/main.cpp +++ b/buffertest/main.cpp @@ -1,16 +1,24 @@ #include "calendar_generated.h" #include #include +#include +#include +#include +#include + +#include "store/database.h" using namespace Calendar; using namespace flatbuffers; -std::string createEvent() +std::string createEvent(bool createAttachment = false) { FlatBufferBuilder fbb; { auto summary = fbb.CreateString("summary"); - const int attachmentSize = 1024 * 1024; // 1MB + + // const int attachmentSize = 1024 * 1024; // 1MB + const int attachmentSize = 1024*2; // 1KB int8_t rawData[attachmentSize]; auto data = fbb.CreateVector(rawData, attachmentSize); @@ -31,8 +39,29 @@ void readEvent(const std::string &data) int main(int argc, char **argv) { - std::ofstream myfile; - myfile.open ("buffer.fb"); - myfile << createEvent(); - myfile.close(); + Database db; + const int count = 50000; + QTime time; + time.start(); + // std::ofstream myfile; + // myfile.open ("buffer.fb"); + // + auto transaction = db.startTransaction(); + for (int i = 0; i < count; i++) { + const auto key = QString("key%1").arg(i); + auto event = createEvent(true); + db.write(key.toStdString(), event, transaction); + + // myfile << createEvent(); + } + db.endTransaction(transaction); + // myfile.close(); + qDebug() << "Writing took: " << time.elapsed(); + + time.start(); + for (int i = 0; i < count; i++) { + const auto key = QString("key%1").arg(i); + db.read(key.toStdString()); + } + qDebug() << "Reading took: " << time.elapsed(); } diff --git a/store/database.cpp b/store/database.cpp new file mode 100644 index 0000000..d6fe7c0 --- /dev/null +++ b/store/database.cpp @@ -0,0 +1,90 @@ +#include "database.h" + +#include +#include +#include +#include +#include + +Database::Database() +{ + int rc; + + QDir dir; + dir.mkdir("./testdb"); + + //create file + rc = mdb_env_create(&env); + rc = mdb_env_open(env, "./testdb", 0, 0664); + const int dbSize = 10485760*100; //10MB * 100 + mdb_env_set_mapsize(env, dbSize); + + if (rc) { + std::cerr << "mdb_env_open: " << rc << mdb_strerror(rc) << std::endl; + } +} + +Database::~Database() +{ + mdb_close(env, dbi); + mdb_env_close(env); +} + +MDB_txn *Database::startTransaction() +{ + int rc; + MDB_txn *transaction; + rc = mdb_txn_begin(env, NULL, 0, &transaction); + rc = mdb_open(transaction, NULL, 0, &dbi); + return transaction; +} + +void Database::endTransaction(MDB_txn *transaction) +{ + int rc; + rc = mdb_txn_commit(transaction); + if (rc) { + std::cerr << "mdb_txn_commit: " << rc << mdb_strerror(rc) << std::endl; + } +} + + +void Database::write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction) +{ + int rc; + MDB_val key, data; + key.mv_size = sKey.size(); + key.mv_data = (void*)sKey.data(); + data.mv_size = sValue.size(); + data.mv_data = (void*)sValue.data(); + rc = mdb_put(transaction, dbi, &key, &data, 0); + if (rc) { + std::cerr << "mdb_put: " << rc << mdb_strerror(rc) << std::endl; + } +} + +void Database::read(const std::string &sKey) +{ + int rc; + MDB_txn *txn; + MDB_val key; + MDB_val data; + MDB_cursor *cursor; + + key.mv_size = sKey.size(); + key.mv_data = (void*)sKey.data(); + + rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); + rc = mdb_cursor_open(txn, dbi, &cursor); + // while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { + if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { + const std::string resultKey(static_cast(key.mv_data), key.mv_size); + const std::string resultValue(static_cast(data.mv_data), data.mv_size); + // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; + } else { + std::cout << "couldn't find value " << sKey << std::endl; + } + mdb_cursor_close(cursor); + mdb_txn_abort(txn); +} + diff --git a/store/database.h b/store/database.h new file mode 100644 index 0000000..b42b9f7 --- /dev/null +++ b/store/database.h @@ -0,0 +1,16 @@ +#include +#include + +class Database { +public: + Database(); + ~Database(); + MDB_txn *startTransaction(); + void endTransaction(MDB_txn *transaction); + void write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction); + void read(const std::string &sKey); + +private: + MDB_env *env; + MDB_dbi dbi; +}; -- cgit v1.2.3