From 44757d932abac6c8346366dfa3c0fb94e5ee0d06 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 3 Dec 2014 20:36:37 +0100 Subject: dummyresource that doesn't work yet --- store/database.cpp | 27 ++++++++++++++++++++------- store/database.h | 5 ++++- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'store') 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 } } -void Database::read(const std::string &sKey) +void Database::read(const std::string &sKey, const std::function &resultHandler) { int rc; MDB_txn *txn; @@ -76,13 +76,26 @@ void Database::read(const std::string &sKey) 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; + if (sKey.empty()) { + std::cout << "Iterating over all values of store!" << std::endl; + while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 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; + resultHandler(resultValue); + } } else { - std::cout << "couldn't find value " << sKey << std::endl; + 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; + resultHandler(resultValue); + } else { + std::cout << "couldn't find value " << sKey << std::endl; + } + } + if (rc) { + std::cerr << "mdb_cursor_get: " << rc << mdb_strerror(rc) << std::endl; } mdb_cursor_close(cursor); 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 @@ +#pragma once + #include #include #include @@ -9,7 +11,8 @@ public: 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); + //Perhaps prefer iterators (assuming we need to be able to match multiple values + void read(const std::string &sKey, const std::function &); private: MDB_env *env; -- cgit v1.2.3