diff options
author | Aaron Seigo <aseigo@kde.org> | 2014-12-06 00:39:07 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2014-12-06 00:39:07 +0100 |
commit | 66bcbab0990c965196991d66ca2a595cf9135074 (patch) | |
tree | 3d592cd8c6369ebdb4407d564b01f31defd939cb /common/storage_kyoto.cpp | |
parent | 1a429fd3103c16899f1f7be25046bb63b2b314c3 (diff) | |
download | sink-66bcbab0990c965196991d66ca2a595cf9135074.tar.gz sink-66bcbab0990c965196991d66ca2a595cf9135074.zip |
read takes an error handler rather than returns a bool
Diffstat (limited to 'common/storage_kyoto.cpp')
-rw-r--r-- | common/storage_kyoto.cpp | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/common/storage_kyoto.cpp b/common/storage_kyoto.cpp index 3d7ab8c..539ada1 100644 --- a/common/storage_kyoto.cpp +++ b/common/storage_kyoto.cpp | |||
@@ -18,14 +18,16 @@ public: | |||
18 | Private(const QString &storageRoot, const QString &name, AccessMode m); | 18 | Private(const QString &storageRoot, const QString &name, AccessMode m); |
19 | ~Private(); | 19 | ~Private(); |
20 | 20 | ||
21 | QString name; | ||
21 | kyotocabinet::TreeDB db; | 22 | kyotocabinet::TreeDB db; |
22 | AccessMode mode; | 23 | AccessMode mode; |
23 | bool dbOpen; | 24 | bool dbOpen; |
24 | bool inTransaction; | 25 | bool inTransaction; |
25 | }; | 26 | }; |
26 | 27 | ||
27 | Storage::Private::Private(const QString &storageRoot, const QString &name, AccessMode m) | 28 | Storage::Private::Private(const QString &storageRoot, const QString &n, AccessMode m) |
28 | : mode(m), | 29 | : name(n), |
30 | mode(m), | ||
29 | dbOpen(false), | 31 | dbOpen(false), |
30 | inTransaction(false) | 32 | inTransaction(false) |
31 | { | 33 | { |
@@ -129,34 +131,45 @@ bool Storage::write(const std::string &sKey, const std::string &sValue) | |||
129 | return success; | 131 | return success; |
130 | } | 132 | } |
131 | 133 | ||
132 | bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) | 134 | void Storage::read(const std::string &sKey, |
135 | const std::function<bool(const std::string &value)> &resultHandler, | ||
136 | const std::function<void(const Storage::Error &error)> &errorHandler) | ||
133 | { | 137 | { |
134 | if (!d->dbOpen) { | 138 | if (!d->dbOpen) { |
135 | return false; | 139 | Error error(d->name.toStdString(), -1, "Not open"); |
140 | errorHandler(error); | ||
141 | return; | ||
136 | } | 142 | } |
137 | 143 | ||
138 | std::string value; | 144 | std::string value; |
139 | if (d->db.get(sKey, &value)) { | 145 | if (d->db.get(sKey, &value)) { |
140 | resultHandler(value); | 146 | resultHandler(value); |
141 | return true; | 147 | return; |
142 | } | 148 | } |
143 | 149 | ||
144 | return false; | 150 | Error error(d->name.toStdString(), d->db.error().code(), d->db.error().message()); |
151 | errorHandler(error); | ||
145 | } | 152 | } |
146 | 153 | ||
147 | bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) | 154 | void Storage::read(const std::string &sKey, |
155 | const std::function<bool(void *ptr, int size)> &resultHandler, | ||
156 | const std::function<void(const Storage::Error &error)> &errorHandler) | ||
148 | { | 157 | { |
149 | if (!d->dbOpen) { | 158 | if (!d->dbOpen) { |
150 | return false; | 159 | Error error(d->name.toStdString(), -1, "Not open"); |
160 | errorHandler(error); | ||
161 | return; | ||
151 | } | 162 | } |
152 | 163 | ||
153 | size_t valueSize; | 164 | size_t valueSize; |
154 | char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); | 165 | char *valueBuffer = d->db.get(sKey.data(), sKey.size(), &valueSize); |
155 | if (valueBuffer) { | 166 | if (valueBuffer) { |
156 | resultHandler(valueBuffer, valueSize); | 167 | resultHandler(valueBuffer, valueSize); |
168 | } else { | ||
169 | Error error(d->name.toStdString(), d->db.error().code(), d->db.error().message()); | ||
170 | errorHandler(error); | ||
157 | } | 171 | } |
158 | delete[] valueBuffer; | 172 | delete[] valueBuffer; |
159 | return valueBuffer != nullptr; | ||
160 | } | 173 | } |
161 | 174 | ||
162 | qint64 Storage::diskUsage() const | 175 | qint64 Storage::diskUsage() const |