diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-05 09:46:53 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-05 09:46:53 +0100 |
commit | 351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad (patch) | |
tree | 3ba738fdc01f2b73bc3942ed530db34ef4cc1d6e /common | |
parent | 0c1400c7f0cf2f545a6cd7347314c1158fbfa36f (diff) | |
download | sink-351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad.tar.gz sink-351a66b5fb1c8659bff8ea20d60f5a6d2d3263ad.zip |
make read return a bool on success
not happy with this API, but we need to discuss the whole read
thing anyways
Diffstat (limited to 'common')
-rw-r--r-- | common/storage.h | 4 | ||||
-rw-r--r-- | common/storage_kyoto.cpp | 16 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 18 |
3 files changed, 23 insertions, 15 deletions
diff --git a/common/storage.h b/common/storage.h index f7dbd89..0b548fb 100644 --- a/common/storage.h +++ b/common/storage.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/common/storage_kyoto.cpp b/common/storage_kyoto.cpp index 05942c2..40bd3e6 100644 --- a/common/storage_kyoto.cpp +++ b/common/storage_kyoto.cpp | |||
@@ -118,28 +118,34 @@ bool Storage::write(const std::string &sKey, const std::string &sValue) | |||
118 | return success; | 118 | return success; |
119 | } | 119 | } |
120 | 120 | ||
121 | void Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) | 121 | bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) |
122 | { | 122 | { |
123 | if (!d->dbOpen) { | 123 | if (!d->dbOpen) { |
124 | return; | 124 | return false; |
125 | } | 125 | } |
126 | 126 | ||
127 | std::string value; | 127 | std::string value; |
128 | if (d->db.get(sKey, &value)) { | 128 | if (d->db.get(sKey, &value)) { |
129 | resultHandler(value); | 129 | resultHandler(value); |
130 | return true; | ||
130 | } | 131 | } |
132 | |||
133 | return false; | ||
131 | } | 134 | } |
132 | 135 | ||
133 | void Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) | 136 | bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) |
134 | { | 137 | { |
135 | if (!d->dbOpen) { | 138 | if (!d->dbOpen) { |
136 | return; | 139 | return false; |
137 | } | 140 | } |
138 | 141 | ||
139 | size_t valueSize; | 142 | size_t valueSize; |
140 | char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); | 143 | char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); |
141 | resultHandler(valueBuffer, valueSize); | 144 | if (valueBuffer) { |
145 | resultHandler(valueBuffer, valueSize); | ||
146 | } | ||
142 | delete[] valueBuffer; | 147 | delete[] valueBuffer; |
148 | return valueBuffer != nullptr; | ||
143 | } | 149 | } |
144 | 150 | ||
145 | qint64 Storage::diskUsage() const | 151 | qint64 Storage::diskUsage() const |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 6c25448..18a5aa6 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -180,9 +180,9 @@ bool Storage::write(const std::string &sKey, const std::string &sValue) | |||
180 | return !rc; | 180 | return !rc; |
181 | } | 181 | } |
182 | 182 | ||
183 | void Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) | 183 | bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) |
184 | { | 184 | { |
185 | read(sKey, | 185 | return read(sKey, |
186 | [&](void *ptr, int size) { | 186 | [&](void *ptr, int size) { |
187 | const std::string resultValue(static_cast<char*>(ptr), size); | 187 | const std::string resultValue(static_cast<char*>(ptr), size); |
188 | resultHandler(resultValue); | 188 | resultHandler(resultValue); |
@@ -190,10 +190,10 @@ void Storage::read(const std::string &sKey, const std::function<void(const std:: | |||
190 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; | 190 | // std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; |
191 | } | 191 | } |
192 | 192 | ||
193 | void Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) | 193 | bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) |
194 | { | 194 | { |
195 | if (!d->env) { | 195 | if (!d->env) { |
196 | return; | 196 | return false; |
197 | } | 197 | } |
198 | 198 | ||
199 | int rc; | 199 | int rc; |
@@ -208,14 +208,14 @@ void Storage::read(const std::string &sKey, const std::function<void(void *ptr, | |||
208 | if (implicitTransaction) { | 208 | if (implicitTransaction) { |
209 | // TODO: if this fails, still try the write below? | 209 | // TODO: if this fails, still try the write below? |
210 | if (!startTransaction(ReadOnly)) { | 210 | if (!startTransaction(ReadOnly)) { |
211 | return; | 211 | return false; |
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); | 215 | rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); |
216 | if (rc) { | 216 | if (rc) { |
217 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; | 217 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; |
218 | return; | 218 | return false; |
219 | } | 219 | } |
220 | 220 | ||
221 | if (sKey.empty()) { | 221 | if (sKey.empty()) { |
@@ -237,18 +237,20 @@ void Storage::read(const std::string &sKey, const std::function<void(void *ptr, | |||
237 | } | 237 | } |
238 | } | 238 | } |
239 | 239 | ||
240 | mdb_cursor_close(cursor); | ||
241 | |||
240 | if (rc) { | 242 | if (rc) { |
241 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; | 243 | std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; |
244 | return false | ||
242 | } | 245 | } |
243 | 246 | ||
244 | mdb_cursor_close(cursor); | ||
245 | |||
246 | /** | 247 | /** |
247 | we don't abort the transaction since we need it for reading the values | 248 | we don't abort the transaction since we need it for reading the values |
248 | if (implicitTransaction) { | 249 | if (implicitTransaction) { |
249 | abortTransaction(); | 250 | abortTransaction(); |
250 | } | 251 | } |
251 | */ | 252 | */ |
253 | return true; | ||
252 | } | 254 | } |
253 | 255 | ||
254 | qint64 Storage::diskUsage() const | 256 | qint64 Storage::diskUsage() const |