summaryrefslogtreecommitdiffstats
path: root/common/storage_lmdb.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-10 21:54:04 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-10 21:54:04 +0100
commit95801103c1fc773b074eea1b2d03820099ac09a6 (patch)
treeb55445cddc451b68ff171fceb729e4cc602b2531 /common/storage_lmdb.cpp
parent4911df72dbc202dfcc4c64bb0ba311d7513707ae (diff)
downloadsink-95801103c1fc773b074eea1b2d03820099ac09a6.tar.gz
sink-95801103c1fc773b074eea1b2d03820099ac09a6.zip
Storage: implemented scan
This can replace all our read calls. Note that we need a different API for databases where the value needs to be loaded first, so we can do a key scan before loading values. With this we can do key + value scans in one though.
Diffstat (limited to 'common/storage_lmdb.cpp')
-rw-r--r--common/storage_lmdb.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index b7102aa..95af3a2 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -211,6 +211,15 @@ void Storage::read(const std::string &sKey,
211 const std::function<bool(void *ptr, int size)> &resultHandler, 211 const std::function<bool(void *ptr, int size)> &resultHandler,
212 const std::function<void(const Storage::Error &error)> &errorHandler) 212 const std::function<void(const Storage::Error &error)> &errorHandler)
213{ 213{
214 scan(sKey, [resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) {
215 return resultHandler(valuePtr, valueSize);
216 }, errorHandler);
217}
218
219void Storage::scan(const std::string &sKey,
220 const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler,
221 const std::function<void(const Storage::Error &error)> &errorHandler)
222{
214 if (!d->env) { 223 if (!d->env) {
215 Error error(d->name.toStdString(), -1, "Not open"); 224 Error error(d->name.toStdString(), -1, "Not open");
216 errorHandler(error); 225 errorHandler(error);
@@ -243,10 +252,9 @@ void Storage::read(const std::string &sKey,
243 } 252 }
244 253
245 if (sKey.empty()) { 254 if (sKey.empty()) {
246 std::cout << "Iterating over all values of store!" << std::endl;
247 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); 255 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
248 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { 256 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
249 if (!resultHandler(key.mv_data, data.mv_size)) { 257 if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) {
250 break; 258 break;
251 } 259 }
252 } 260 }
@@ -257,7 +265,7 @@ void Storage::read(const std::string &sKey,
257 } 265 }
258 } else { 266 } else {
259 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { 267 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) {
260 resultHandler(data.mv_data, data.mv_size); 268 resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size);
261 } else { 269 } else {
262 std::cout << "couldn't find value " << sKey << " " << std::endl; 270 std::cout << "couldn't find value " << sKey << " " << std::endl;
263 } 271 }