summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-05 01:02:51 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2014-12-05 01:02:51 +0100
commit639fc60c100204c87b93112516cf3b3117cfff0d (patch)
tree13d117060319396958002020c613d763a9c2cd3e
parent894698bd80007447950e045ab5beed8cb4a03f54 (diff)
downloadsink-639fc60c100204c87b93112516cf3b3117cfff0d.tar.gz
sink-639fc60c100204c87b93112516cf3b3117cfff0d.zip
Return false on error during read.
-rw-r--r--store/database.cpp14
-rw-r--r--store/database.h4
-rw-r--r--store/test/storagetest.cpp6
3 files changed, 14 insertions, 10 deletions
diff --git a/store/database.cpp b/store/database.cpp
index 0c4fd18..c4dfbd6 100644
--- a/store/database.cpp
+++ b/store/database.cpp
@@ -187,9 +187,9 @@ bool Database::write(const std::string &sKey, const std::string &sValue)
187 return !rc; 187 return !rc;
188} 188}
189 189
190void Database::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 190bool Database::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler)
191{ 191{
192 read(sKey, 192 return read(sKey,
193 [&](void *ptr, int size) { 193 [&](void *ptr, int size) {
194 const std::string resultValue(static_cast<char*>(ptr), size); 194 const std::string resultValue(static_cast<char*>(ptr), size);
195 resultHandler(resultValue); 195 resultHandler(resultValue);
@@ -197,10 +197,10 @@ void Database::read(const std::string &sKey, const std::function<void(const std:
197// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; 197// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl;
198} 198}
199 199
200void Database::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 200bool Database::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler)
201{ 201{
202 if (!d->env) { 202 if (!d->env) {
203 return; 203 return false;
204 } 204 }
205 205
206 int rc; 206 int rc;
@@ -215,14 +215,14 @@ void Database::read(const std::string &sKey, const std::function<void(void *ptr,
215 if (implicitTransaction) { 215 if (implicitTransaction) {
216 // TODO: if this fails, still try the write below? 216 // TODO: if this fails, still try the write below?
217 if (!startTransaction(ReadOnly)) { 217 if (!startTransaction(ReadOnly)) {
218 return; 218 return false;
219 } 219 }
220 } 220 }
221 221
222 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); 222 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
223 if (rc) { 223 if (rc) {
224 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 224 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl;
225 return; 225 return false;
226 } 226 }
227 227
228 if (sKey.empty()) { 228 if (sKey.empty()) {
@@ -246,6 +246,7 @@ void Database::read(const std::string &sKey, const std::function<void(void *ptr,
246 246
247 if (rc) { 247 if (rc) {
248 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 248 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl;
249 return false;
249 } 250 }
250 251
251 mdb_cursor_close(cursor); 252 mdb_cursor_close(cursor);
@@ -256,6 +257,7 @@ void Database::read(const std::string &sKey, const std::function<void(void *ptr,
256 abortTransaction(); 257 abortTransaction();
257 } 258 }
258 */ 259 */
260 return true;
259} 261}
260 262
261qint64 Database::diskUsage() const 263qint64 Database::diskUsage() const
diff --git a/store/database.h b/store/database.h
index e752ff5..1cede39 100644
--- a/store/database.h
+++ b/store/database.h
@@ -16,8 +16,8 @@ public:
16 bool write(const char *key, size_t keySize, const char *value, size_t valueSize); 16 bool write(const char *key, size_t keySize, const char *value, size_t valueSize);
17 bool write(const std::string &sKey, const std::string &sValue); 17 bool write(const std::string &sKey, const std::string &sValue);
18 //Perhaps prefer iterators (assuming we need to be able to match multiple values 18 //Perhaps prefer iterators (assuming we need to be able to match multiple values
19 void read(const std::string &sKey, const std::function<void(const std::string &value)> &); 19 bool read(const std::string &sKey, const std::function<void(const std::string &value)> &);
20 void read(const std::string &sKey, const std::function<void(void *ptr, int size)> &); 20 bool read(const std::string &sKey, const std::function<void(void *ptr, int size)> &);
21 21
22 qint64 diskUsage() const; 22 qint64 diskUsage() const;
23 void removeFromDisk() const; 23 void removeFromDisk() const;
diff --git a/store/test/storagetest.cpp b/store/test/storagetest.cpp
index 1b105af..dba4f6c 100644
--- a/store/test/storagetest.cpp
+++ b/store/test/storagetest.cpp
@@ -37,12 +37,14 @@ private:
37 { 37 {
38 bool error = false; 38 bool error = false;
39 const auto reference = keyPrefix + std::to_string(i); 39 const auto reference = keyPrefix + std::to_string(i);
40 db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) { 40 if(!db.read(keyPrefix + std::to_string(i), [&error, &reference](const std::string &value) {
41 if (value != reference) { 41 if (value != reference) {
42 qDebug() << "Mismatch while reading"; 42 qDebug() << "Mismatch while reading";
43 error = true; 43 error = true;
44 } 44 }
45 }); 45 })) {
46 return false;
47 }
46 return !error; 48 return !error;
47 } 49 }
48 50