diff options
-rw-r--r-- | buffertest/CMakeLists.txt | 4 | ||||
-rw-r--r-- | buffertest/main.cpp | 41 | ||||
-rw-r--r-- | store/database.cpp | 90 | ||||
-rw-r--r-- | store/database.h | 16 |
4 files changed, 145 insertions, 6 deletions
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) | |||
2 | 2 | ||
3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) | 3 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) |
4 | 4 | ||
5 | set(store_path "../store/") | ||
6 | |||
5 | set(toynadinbuffertest_SRCS | 7 | set(toynadinbuffertest_SRCS |
8 | ${store_path}/database.cpp | ||
6 | main.cpp | 9 | main.cpp |
7 | ) | 10 | ) |
8 | 11 | ||
@@ -22,5 +25,6 @@ ADD_CUSTOM_TARGET(generate_buffers ALL DEPENDS ${SCHEMA_SOURCEFILES}) | |||
22 | 25 | ||
23 | add_executable(${PROJECT_NAME} ${toynadinbuffertest_SRCS}) | 26 | add_executable(${PROJECT_NAME} ${toynadinbuffertest_SRCS}) |
24 | qt5_use_modules(${PROJECT_NAME} Core) | 27 | qt5_use_modules(${PROJECT_NAME} Core) |
28 | target_link_libraries(${PROJECT_NAME} lmdb) | ||
25 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) | 29 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) |
26 | 30 | ||
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 @@ | |||
1 | #include "calendar_generated.h" | 1 | #include "calendar_generated.h" |
2 | #include <iostream> | 2 | #include <iostream> |
3 | #include <fstream> | 3 | #include <fstream> |
4 | #include <QDir> | ||
5 | #include <QString> | ||
6 | #include <QTime> | ||
7 | #include <qdebug.h> | ||
8 | |||
9 | #include "store/database.h" | ||
4 | 10 | ||
5 | using namespace Calendar; | 11 | using namespace Calendar; |
6 | using namespace flatbuffers; | 12 | using namespace flatbuffers; |
7 | 13 | ||
8 | std::string createEvent() | 14 | std::string createEvent(bool createAttachment = false) |
9 | { | 15 | { |
10 | FlatBufferBuilder fbb; | 16 | FlatBufferBuilder fbb; |
11 | { | 17 | { |
12 | auto summary = fbb.CreateString("summary"); | 18 | auto summary = fbb.CreateString("summary"); |
13 | const int attachmentSize = 1024 * 1024; // 1MB | 19 | |
20 | // const int attachmentSize = 1024 * 1024; // 1MB | ||
21 | const int attachmentSize = 1024*2; // 1KB | ||
14 | int8_t rawData[attachmentSize]; | 22 | int8_t rawData[attachmentSize]; |
15 | auto data = fbb.CreateVector(rawData, attachmentSize); | 23 | auto data = fbb.CreateVector(rawData, attachmentSize); |
16 | 24 | ||
@@ -31,8 +39,29 @@ void readEvent(const std::string &data) | |||
31 | 39 | ||
32 | int main(int argc, char **argv) | 40 | int main(int argc, char **argv) |
33 | { | 41 | { |
34 | std::ofstream myfile; | 42 | Database db; |
35 | myfile.open ("buffer.fb"); | 43 | const int count = 50000; |
36 | myfile << createEvent(); | 44 | QTime time; |
37 | myfile.close(); | 45 | time.start(); |
46 | // std::ofstream myfile; | ||
47 | // myfile.open ("buffer.fb"); | ||
48 | // | ||
49 | auto transaction = db.startTransaction(); | ||
50 | for (int i = 0; i < count; i++) { | ||
51 | const auto key = QString("key%1").arg(i); | ||
52 | auto event = createEvent(true); | ||
53 | db.write(key.toStdString(), event, transaction); | ||
54 | |||
55 | // myfile << createEvent(); | ||
56 | } | ||
57 | db.endTransaction(transaction); | ||
58 | // myfile.close(); | ||
59 | qDebug() << "Writing took: " << time.elapsed(); | ||
60 | |||
61 | time.start(); | ||
62 | for (int i = 0; i < count; i++) { | ||
63 | const auto key = QString("key%1").arg(i); | ||
64 | db.read(key.toStdString()); | ||
65 | } | ||
66 | qDebug() << "Reading took: " << time.elapsed(); | ||
38 | } | 67 | } |
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 @@ | |||
1 | #include "database.h" | ||
2 | |||
3 | #include <iostream> | ||
4 | #include <QDir> | ||
5 | #include <QString> | ||
6 | #include <QTime> | ||
7 | #include <qdebug.h> | ||
8 | |||
9 | Database::Database() | ||
10 | { | ||
11 | int rc; | ||
12 | |||
13 | QDir dir; | ||
14 | dir.mkdir("./testdb"); | ||
15 | |||
16 | //create file | ||
17 | rc = mdb_env_create(&env); | ||
18 | rc = mdb_env_open(env, "./testdb", 0, 0664); | ||
19 | const int dbSize = 10485760*100; //10MB * 100 | ||
20 | mdb_env_set_mapsize(env, dbSize); | ||
21 | |||
22 | if (rc) { | ||
23 | std::cerr << "mdb_env_open: " << rc << mdb_strerror(rc) << std::endl; | ||
24 | } | ||
25 | } | ||
26 | |||
27 | Database::~Database() | ||
28 | { | ||
29 | mdb_close(env, dbi); | ||
30 | mdb_env_close(env); | ||
31 | } | ||
32 | |||
33 | MDB_txn *Database::startTransaction() | ||
34 | { | ||
35 | int rc; | ||
36 | MDB_txn *transaction; | ||
37 | rc = mdb_txn_begin(env, NULL, 0, &transaction); | ||
38 | rc = mdb_open(transaction, NULL, 0, &dbi); | ||
39 | return transaction; | ||
40 | } | ||
41 | |||
42 | void Database::endTransaction(MDB_txn *transaction) | ||
43 | { | ||
44 | int rc; | ||
45 | rc = mdb_txn_commit(transaction); | ||
46 | if (rc) { | ||
47 | std::cerr << "mdb_txn_commit: " << rc << mdb_strerror(rc) << std::endl; | ||
48 | } | ||
49 | } | ||
50 | |||
51 | |||
52 | void Database::write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction) | ||
53 | { | ||
54 | int rc; | ||
55 | MDB_val key, data; | ||
56 | key.mv_size = sKey.size(); | ||
57 | key.mv_data = (void*)sKey.data(); | ||
58 | data.mv_size = sValue.size(); | ||
59 | data.mv_data = (void*)sValue.data(); | ||
60 | rc = mdb_put(transaction, dbi, &key, &data, 0); | ||
61 | if (rc) { | ||
62 | std::cerr << "mdb_put: " << rc << mdb_strerror(rc) << std::endl; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void Database::read(const std::string &sKey) | ||
67 | { | ||
68 | int rc; | ||
69 | MDB_txn *txn; | ||
70 | MDB_val key; | ||
71 | MDB_val data; | ||
72 | MDB_cursor *cursor; | ||
73 | |||
74 | key.mv_size = sKey.size(); | ||
75 | key.mv_data = (void*)sKey.data(); | ||
76 | |||
77 | rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); | ||
78 | rc = mdb_cursor_open(txn, dbi, &cursor); | ||
79 | // while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { | ||
80 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { | ||
81 | const std::string resultKey(static_cast<char*>(key.mv_data), key.mv_size); | ||
82 | const std::string resultValue(static_cast<char*>(data.mv_data), data.mv_size); | ||
83 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | ||
84 | } else { | ||
85 | std::cout << "couldn't find value " << sKey << std::endl; | ||
86 | } | ||
87 | mdb_cursor_close(cursor); | ||
88 | mdb_txn_abort(txn); | ||
89 | } | ||
90 | |||
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 @@ | |||
1 | #include <lmdb.h> | ||
2 | #include <string> | ||
3 | |||
4 | class Database { | ||
5 | public: | ||
6 | Database(); | ||
7 | ~Database(); | ||
8 | MDB_txn *startTransaction(); | ||
9 | void endTransaction(MDB_txn *transaction); | ||
10 | void write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction); | ||
11 | void read(const std::string &sKey); | ||
12 | |||
13 | private: | ||
14 | MDB_env *env; | ||
15 | MDB_dbi dbi; | ||
16 | }; | ||