diff options
Diffstat (limited to 'store/database.cpp')
-rw-r--r-- | store/database.cpp | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/store/database.cpp b/store/database.cpp index 71ec47d..fd3aa8f 100644 --- a/store/database.cpp +++ b/store/database.cpp | |||
@@ -21,11 +21,13 @@ public: | |||
21 | MDB_env *env; | 21 | MDB_env *env; |
22 | MDB_txn *transaction; | 22 | MDB_txn *transaction; |
23 | bool readTransaction; | 23 | bool readTransaction; |
24 | bool firstOpen; | ||
24 | }; | 25 | }; |
25 | 26 | ||
26 | Database::Private::Private(const QString &path) | 27 | Database::Private::Private(const QString &path) |
27 | : transaction(0), | 28 | : transaction(0), |
28 | readTransaction(false) | 29 | readTransaction(false), |
30 | firstOpen(true) | ||
29 | { | 31 | { |
30 | QDir dir; | 32 | QDir dir; |
31 | dir.mkdir(path); | 33 | dir.mkdir(path); |
@@ -89,13 +91,22 @@ bool Database::startTransaction(TransactionType type) | |||
89 | abortTransaction(); | 91 | abortTransaction(); |
90 | } | 92 | } |
91 | 93 | ||
92 | //TODO handle errors | 94 | if (d->firstOpen && requestedRead) { |
95 | //A write transaction is at least required the first time | ||
96 | mdb_txn_begin(d->env, nullptr, 0, &d->transaction); | ||
97 | //Open the database | ||
98 | //With this we could open multiple named databases if we wanted to | ||
99 | mdb_dbi_open(d->transaction, nullptr, 0, &d->dbi); | ||
100 | mdb_txn_abort(d->transaction); | ||
101 | } | ||
102 | |||
93 | int rc; | 103 | int rc; |
94 | rc = mdb_txn_begin(d->env, NULL, requestedRead ? MDB_RDONLY : 0, &d->transaction); | 104 | rc = mdb_txn_begin(d->env, NULL, requestedRead ? MDB_RDONLY : 0, &d->transaction); |
95 | if (!rc) { | 105 | if (!rc) { |
96 | rc = mdb_dbi_open(d->transaction, NULL, 0, &d->dbi); | 106 | rc = mdb_dbi_open(d->transaction, NULL, 0, &d->dbi); |
97 | } | 107 | } |
98 | 108 | ||
109 | d->firstOpen = false; | ||
99 | return !rc; | 110 | return !rc; |
100 | } | 111 | } |
101 | 112 | ||
@@ -191,17 +202,6 @@ void Database::read(const std::string &sKey, const std::function<void(void *ptr, | |||
191 | key.mv_size = sKey.size(); | 202 | key.mv_size = sKey.size(); |
192 | key.mv_data = (void*)sKey.data(); | 203 | key.mv_data = (void*)sKey.data(); |
193 | 204 | ||
194 | /* | ||
195 | TODO: do we need a write transaction before a read transaction? only relevant for implicitTransactions in any case | ||
196 | { | ||
197 | //A write transaction is at least required the first time | ||
198 | rc = mdb_txn_begin(env, nullptr, 0, &txn); | ||
199 | //Open the database | ||
200 | //With this we could open multiple named databases if we wanted to | ||
201 | rc = mdb_dbi_open(txn, nullptr, 0, &dbi); | ||
202 | mdb_txn_abort(txn); | ||
203 | } | ||
204 | */ | ||
205 | const bool implicitTransaction = !d->transaction; | 205 | const bool implicitTransaction = !d->transaction; |
206 | if (implicitTransaction) { | 206 | if (implicitTransaction) { |
207 | // TODO: if this fails, still try the write below? | 207 | // TODO: if this fails, still try the write below? |
@@ -218,6 +218,7 @@ TODO: do we need a write transaction before a read transaction? only relevant fo | |||
218 | 218 | ||
219 | if (sKey.empty()) { | 219 | if (sKey.empty()) { |
220 | std::cout << "Iterating over all values of store!" << std::endl; | 220 | std::cout << "Iterating over all values of store!" << std::endl; |
221 | rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); | ||
221 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { | 222 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
222 | resultHandler(key.mv_data, data.mv_size); | 223 | resultHandler(key.mv_data, data.mv_size); |
223 | } | 224 | } |