diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/database.cpp | 27 | ||||
-rw-r--r-- | store/database.h | 5 |
2 files changed, 24 insertions, 8 deletions
diff --git a/store/database.cpp b/store/database.cpp index c16a077..7e2019e 100644 --- a/store/database.cpp +++ b/store/database.cpp | |||
@@ -63,7 +63,7 @@ void Database::write(const std::string &sKey, const std::string &sValue, MDB_txn | |||
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | void Database::read(const std::string &sKey) | 66 | void Database::read(const std::string &sKey, const std::function<void(const std::string)> &resultHandler) |
67 | { | 67 | { |
68 | int rc; | 68 | int rc; |
69 | MDB_txn *txn; | 69 | MDB_txn *txn; |
@@ -76,13 +76,26 @@ void Database::read(const std::string &sKey) | |||
76 | 76 | ||
77 | rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); | 77 | rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn); |
78 | rc = mdb_cursor_open(txn, dbi, &cursor); | 78 | rc = mdb_cursor_open(txn, dbi, &cursor); |
79 | // while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { | 79 | if (sKey.empty()) { |
80 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { | 80 | std::cout << "Iterating over all values of store!" << std::endl; |
81 | const std::string resultKey(static_cast<char*>(key.mv_data), key.mv_size); | 81 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
82 | const std::string resultValue(static_cast<char*>(data.mv_data), data.mv_size); | 82 | const std::string resultKey(static_cast<char*>(key.mv_data), key.mv_size); |
83 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | 83 | const std::string resultValue(static_cast<char*>(data.mv_data), data.mv_size); |
84 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | ||
85 | resultHandler(resultValue); | ||
86 | } | ||
84 | } else { | 87 | } else { |
85 | std::cout << "couldn't find value " << sKey << std::endl; | 88 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { |
89 | const std::string resultKey(static_cast<char*>(key.mv_data), key.mv_size); | ||
90 | const std::string resultValue(static_cast<char*>(data.mv_data), data.mv_size); | ||
91 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | ||
92 | resultHandler(resultValue); | ||
93 | } else { | ||
94 | std::cout << "couldn't find value " << sKey << std::endl; | ||
95 | } | ||
96 | } | ||
97 | if (rc) { | ||
98 | std::cerr << "mdb_cursor_get: " << rc << mdb_strerror(rc) << std::endl; | ||
86 | } | 99 | } |
87 | mdb_cursor_close(cursor); | 100 | mdb_cursor_close(cursor); |
88 | mdb_txn_abort(txn); | 101 | mdb_txn_abort(txn); |
diff --git a/store/database.h b/store/database.h index 1a124be..999a89e 100644 --- a/store/database.h +++ b/store/database.h | |||
@@ -1,3 +1,5 @@ | |||
1 | #pragma once | ||
2 | |||
1 | #include <lmdb.h> | 3 | #include <lmdb.h> |
2 | #include <string> | 4 | #include <string> |
3 | #include <QString> | 5 | #include <QString> |
@@ -9,7 +11,8 @@ public: | |||
9 | MDB_txn *startTransaction(); | 11 | MDB_txn *startTransaction(); |
10 | void endTransaction(MDB_txn *transaction); | 12 | void endTransaction(MDB_txn *transaction); |
11 | void write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction); | 13 | void write(const std::string &sKey, const std::string &sValue, MDB_txn *transaction); |
12 | void read(const std::string &sKey); | 14 | //Perhaps prefer iterators (assuming we need to be able to match multiple values |
15 | void read(const std::string &sKey, const std::function<void(const std::string)> &); | ||
13 | 16 | ||
14 | private: | 17 | private: |
15 | MDB_env *env; | 18 | MDB_env *env; |