diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-09 22:01:35 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-11 01:01:13 +0100 |
commit | 5f7262f5c410cf3f36116c221d28dd393664228d (patch) | |
tree | 5c3fb21cd31835470bdd87cd467a3c6ef0659fed | |
parent | 3207343cbd4484950155146ab632b8304b70d640 (diff) | |
download | sink-5f7262f5c410cf3f36116c221d28dd393664228d.tar.gz sink-5f7262f5c410cf3f36116c221d28dd393664228d.zip |
add another read method and expose a basic error handler for convenience
also, add some todos
-rw-r--r-- | common/storage.h | 7 | ||||
-rw-r--r-- | common/storage_common.cpp | 7 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 23 |
3 files changed, 22 insertions, 15 deletions
diff --git a/common/storage.h b/common/storage.h index 83c307c..a051043 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -32,16 +32,17 @@ public: | |||
32 | const std::function<bool(const std::string &value)> &resultHandler); | 32 | const std::function<bool(const std::string &value)> &resultHandler); |
33 | void read(const std::string &sKey, | 33 | void read(const std::string &sKey, |
34 | const std::function<bool(const std::string &value)> &resultHandler, | 34 | const std::function<bool(const std::string &value)> &resultHandler, |
35 | const std::function<void(const Storage::Error &error)> &errors); | 35 | const std::function<void(const Storage::Error &error)> &errorHandler); |
36 | void read(const std::string &sKey, const std::function<bool(void *ptr, int size)> &resultHandler); | 36 | void read(const std::string &sKey, const std::function<bool(void *ptr, int size)> &resultHandler); |
37 | void read(const std::string &sKey, | 37 | void read(const std::string &sKey, |
38 | const std::function<bool(void *ptr, int size)> & resultHandler, | 38 | const std::function<bool(void *ptr, int size)> & resultHandler, |
39 | const std::function<void(const Storage::Error &error)> &errorHandler); | 39 | const std::function<void(const Storage::Error &error)> &errorHandler); |
40 | void scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler); | 40 | void scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler); |
41 | void scan(const std::string &sKey, | 41 | void scan(const char *keyData, uint keySize, |
42 | const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> & resultHandler, | 42 | const std::function<bool(void *keyPtr, int keySize, void *ptr, int size)> &resultHandler, |
43 | const std::function<void(const Storage::Error &error)> &errorHandler); | 43 | const std::function<void(const Storage::Error &error)> &errorHandler); |
44 | 44 | ||
45 | static std::function<void(const Storage::Error &error)> basicErrorHandler(); | ||
45 | qint64 diskUsage() const; | 46 | qint64 diskUsage() const; |
46 | void removeFromDisk() const; | 47 | void removeFromDisk() const; |
47 | private: | 48 | private: |
diff --git a/common/storage_common.cpp b/common/storage_common.cpp index 246611c..6263bf2 100644 --- a/common/storage_common.cpp +++ b/common/storage_common.cpp | |||
@@ -9,6 +9,11 @@ void errorHandler(const Storage::Error &error) | |||
9 | std::cerr << "Read error in " << error.store << ", code " << error.code << ", message: " << error.message << std::endl; | 9 | std::cerr << "Read error in " << error.store << ", code " << error.code << ", message: " << error.message << std::endl; |
10 | } | 10 | } |
11 | 11 | ||
12 | std::function<void(const Storage::Error &error)> Storage::basicErrorHandler() | ||
13 | { | ||
14 | return errorHandler; | ||
15 | } | ||
16 | |||
12 | void Storage::read(const std::string &sKey, const std::function<bool(const std::string &value)> &resultHandler) | 17 | void Storage::read(const std::string &sKey, const std::function<bool(const std::string &value)> &resultHandler) |
13 | { | 18 | { |
14 | read(sKey, resultHandler, &errorHandler); | 19 | read(sKey, resultHandler, &errorHandler); |
@@ -21,6 +26,6 @@ void Storage::read(const std::string &sKey, const std::function<bool(void *ptr, | |||
21 | 26 | ||
22 | void Storage::scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) | 27 | void Storage::scan(const std::string &sKey, const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler) |
23 | { | 28 | { |
24 | scan(sKey, resultHandler, &errorHandler); | 29 | scan(sKey.data(), sKey.size(), resultHandler, &errorHandler); |
25 | } | 30 | } |
26 | 31 | ||
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 95af3a2..2bffcd7 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -204,19 +204,18 @@ void Storage::read(const std::string &sKey, | |||
204 | const std::string resultValue(static_cast<char*>(ptr), size); | 204 | const std::string resultValue(static_cast<char*>(ptr), size); |
205 | return resultHandler(resultValue); | 205 | return resultHandler(resultValue); |
206 | }, errorHandler); | 206 | }, errorHandler); |
207 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | ||
208 | } | 207 | } |
209 | 208 | ||
210 | void Storage::read(const std::string &sKey, | 209 | void Storage::read(const std::string &sKey, |
211 | const std::function<bool(void *ptr, int size)> &resultHandler, | 210 | const std::function<bool(void *ptr, int size)> &resultHandler, |
212 | const std::function<void(const Storage::Error &error)> &errorHandler) | 211 | const std::function<void(const Storage::Error &error)> &errorHandler) |
213 | { | 212 | { |
214 | scan(sKey, [resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) { | 213 | scan(sKey.data(), sKey.size(), [resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) { |
215 | return resultHandler(valuePtr, valueSize); | 214 | return resultHandler(valuePtr, valueSize); |
216 | }, errorHandler); | 215 | }, errorHandler); |
217 | } | 216 | } |
218 | 217 | ||
219 | void Storage::scan(const std::string &sKey, | 218 | void Storage::scan(const char *keyData, uint keySize, |
220 | const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler, | 219 | const std::function<bool(void *keyPtr, int keySize, void *valuePtr, int valueSize)> &resultHandler, |
221 | const std::function<void(const Storage::Error &error)> &errorHandler) | 220 | const std::function<void(const Storage::Error &error)> &errorHandler) |
222 | { | 221 | { |
@@ -231,8 +230,8 @@ void Storage::scan(const std::string &sKey, | |||
231 | MDB_val data; | 230 | MDB_val data; |
232 | MDB_cursor *cursor; | 231 | MDB_cursor *cursor; |
233 | 232 | ||
234 | key.mv_size = sKey.size(); | 233 | key.mv_data = (void*)keyData; |
235 | key.mv_data = (void*)sKey.data(); | 234 | key.mv_size = keySize; |
236 | 235 | ||
237 | const bool implicitTransaction = !d->transaction; | 236 | const bool implicitTransaction = !d->transaction; |
238 | if (implicitTransaction) { | 237 | if (implicitTransaction) { |
@@ -251,11 +250,13 @@ void Storage::scan(const std::string &sKey, | |||
251 | return; | 250 | return; |
252 | } | 251 | } |
253 | 252 | ||
254 | if (sKey.empty()) { | 253 | if (!keyData || keySize == 0) { |
255 | rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); | 254 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0 && |
256 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { | 255 | resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { |
257 | if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { | 256 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
258 | break; | 257 | if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { |
258 | break; | ||
259 | } | ||
259 | } | 260 | } |
260 | } | 261 | } |
261 | 262 | ||
@@ -267,7 +268,7 @@ void Storage::scan(const std::string &sKey, | |||
267 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { | 268 | if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_SET)) == 0) { |
268 | resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size); | 269 | resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size); |
269 | } else { | 270 | } else { |
270 | std::cout << "couldn't find value " << sKey << " " << std::endl; | 271 | std::cout << "couldn't find value " << std::string(keyData, keySize) << std::endl; |
271 | } | 272 | } |
272 | } | 273 | } |
273 | 274 | ||