summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/storage.h22
-rw-r--r--common/storage_kyoto.cpp31
-rw-r--r--common/storage_lmdb.cpp38
-rw-r--r--dummyresource/facade.cpp3
-rw-r--r--tests/storagebenchmark.cpp2
-rw-r--r--tests/storagetest.cpp16
7 files changed, 79 insertions, 34 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 9b3f777..68330ce 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -13,6 +13,7 @@ endif (STORAGE_KYOTO)
13set(command_SRCS 13set(command_SRCS
14 commands.cpp 14 commands.cpp
15 console.cpp 15 console.cpp
16 storage_common.cpp
16 ${storage_SRCS}) 17 ${storage_SRCS})
17 18
18add_library(${PROJECT_NAME} ${command_SRCS}) 19add_library(${PROJECT_NAME} ${command_SRCS})
diff --git a/common/storage.h b/common/storage.h
index 846e15b..e5e592c 100644
--- a/common/storage.h
+++ b/common/storage.h
@@ -7,6 +7,16 @@ class Storage {
7public: 7public:
8 enum AccessMode { ReadOnly, ReadWrite }; 8 enum AccessMode { ReadOnly, ReadWrite };
9 9
10 class Error
11 {
12 public:
13 Error(const std::string &s, int c, const std::string &m)
14 : store(s), message(m), code(c) {}
15 std::string store;
16 std::string message;
17 int code;
18 };
19
10 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly); 20 Storage(const QString &storageRoot, const QString &name, AccessMode mode = ReadOnly);
11 ~Storage(); 21 ~Storage();
12 bool isInTransaction() const; 22 bool isInTransaction() const;
@@ -15,9 +25,15 @@ public:
15 void abortTransaction(); 25 void abortTransaction();
16 bool write(const char *key, size_t keySize, const char *value, size_t valueSize); 26 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); 27 bool write(const std::string &sKey, const std::string &sValue);
18 //Perhaps prefer iterators (assuming we need to be able to match multiple values 28 void read(const std::string &sKey,
19 bool read(const std::string &sKey, const std::function<void(const std::string &value)> &); 29 const std::function<bool(const std::string &value)> &resultHandler);
20 bool read(const std::string &sKey, const std::function<void(void *ptr, int size)> &); 30 void read(const std::string &sKey,
31 const std::function<bool(const std::string &value)> &resultHandler,
32 const std::function<void(const Storage::Error &error)> &errors);
33 void read(const std::string &sKey, const std::function<bool(void *ptr, int size)> &resultHandler);
34 void read(const std::string &sKey,
35 const std::function<bool(void *ptr, int size)> & resultHandler,
36 const std::function<void(const Storage::Error &error)> &errorHandler);
21 37
22 qint64 diskUsage() const; 38 qint64 diskUsage() const;
23 void removeFromDisk() const; 39 void removeFromDisk() const;
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
27Storage::Private::Private(const QString &storageRoot, const QString &name, AccessMode m) 28Storage::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
132bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 134void 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
147bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 154void 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
162qint64 Storage::diskUsage() const 175qint64 Storage::diskUsage() const
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index 7e23fd3..b7102aa 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -195,20 +195,26 @@ bool Storage::write(const std::string &sKey, const std::string &sValue)
195 return !rc; 195 return !rc;
196} 196}
197 197
198bool Storage::read(const std::string &sKey, const std::function<void(const std::string &value)> &resultHandler) 198void Storage::read(const std::string &sKey,
199 const std::function<bool(const std::string &value)> &resultHandler,
200 const std::function<void(const Storage::Error &error)> &errorHandler)
199{ 201{
200 return read(sKey, 202 read(sKey,
201 [&](void *ptr, int size) { 203 [&](void *ptr, int size) -> bool {
202 const std::string resultValue(static_cast<char*>(ptr), size); 204 const std::string resultValue(static_cast<char*>(ptr), size);
203 resultHandler(resultValue); 205 return resultHandler(resultValue);
204 }); 206 }, errorHandler);
205// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl; 207// std::cout << "key: " << resultKey << " data: " << resultValue << std::endl;
206} 208}
207 209
208bool Storage::read(const std::string &sKey, const std::function<void(void *ptr, int size)> &resultHandler) 210void Storage::read(const std::string &sKey,
211 const std::function<bool(void *ptr, int size)> &resultHandler,
212 const std::function<void(const Storage::Error &error)> &errorHandler)
209{ 213{
210 if (!d->env) { 214 if (!d->env) {
211 return false; 215 Error error(d->name.toStdString(), -1, "Not open");
216 errorHandler(error);
217 return;
212 } 218 }
213 219
214 int rc; 220 int rc;
@@ -223,21 +229,26 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
223 if (implicitTransaction) { 229 if (implicitTransaction) {
224 // TODO: if this fails, still try the write below? 230 // TODO: if this fails, still try the write below?
225 if (!startTransaction(ReadOnly)) { 231 if (!startTransaction(ReadOnly)) {
226 return false; 232 Error error(d->name.toStdString(), -2, "Could not start transaction");
233 errorHandler(error);
234 return;
227 } 235 }
228 } 236 }
229 237
230 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor); 238 rc = mdb_cursor_open(d->transaction, d->dbi, &cursor);
231 if (rc) { 239 if (rc) {
232 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 240 Error error(d->name.toStdString(), rc, mdb_strerror(rc));
233 return false; 241 errorHandler(error);
242 return;
234 } 243 }
235 244
236 if (sKey.empty()) { 245 if (sKey.empty()) {
237 std::cout << "Iterating over all values of store!" << std::endl; 246 std::cout << "Iterating over all values of store!" << std::endl;
238 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST); 247 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
239 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { 248 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
240 resultHandler(key.mv_data, data.mv_size); 249 if (!resultHandler(key.mv_data, data.mv_size)) {
250 break;
251 }
241 } 252 }
242 253
243 //We never find the last value 254 //We never find the last value
@@ -255,8 +266,8 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
255 mdb_cursor_close(cursor); 266 mdb_cursor_close(cursor);
256 267
257 if (rc) { 268 if (rc) {
258 std::cerr << "mdb_cursor_get: " << rc << " " << mdb_strerror(rc) << std::endl; 269 Error error(d->name.toStdString(), rc, mdb_strerror(rc));
259 return false; 270 errorHandler(error);
260 } 271 }
261 272
262 /** 273 /**
@@ -265,7 +276,6 @@ bool Storage::read(const std::string &sKey, const std::function<void(void *ptr,
265 abortTransaction(); 276 abortTransaction();
266 } 277 }
267 */ 278 */
268 return true;
269} 279}
270 280
271qint64 Storage::diskUsage() const 281qint64 Storage::diskUsage() const
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp
index 9758c1b..bfe1de4 100644
--- a/dummyresource/facade.cpp
+++ b/dummyresource/facade.cpp
@@ -77,13 +77,14 @@ void DummyResourceFacade::load(const Akonadi2::Query &query, const std::function
77 qDebug() << "load called"; 77 qDebug() << "load called";
78 //TODO only read values matching the query 78 //TODO only read values matching the query
79 auto storage = QSharedPointer<Storage>::create(Akonadi2::Store::storageLocation(), "dummyresource"); 79 auto storage = QSharedPointer<Storage>::create(Akonadi2::Store::storageLocation(), "dummyresource");
80 storage->read("", [resultCallback, storage](void *data, int size) { 80 storage->read("", [resultCallback, storage](void *data, int size) -> bool {
81 //TODO read second buffer as well 81 //TODO read second buffer as well
82 auto eventBuffer = GetDummyEvent(data); 82 auto eventBuffer = GetDummyEvent(data);
83 auto event = QSharedPointer<DummyEventAdaptor>::create("dummyresource", "key", 0); 83 auto event = QSharedPointer<DummyEventAdaptor>::create("dummyresource", "key", 0);
84 event->buffer = eventBuffer; 84 event->buffer = eventBuffer;
85 event->storage = storage; 85 event->storage = storage;
86 resultCallback(event); 86 resultCallback(event);
87 return true;
87 }); 88 });
88} 89}
89 90
diff --git a/tests/storagebenchmark.cpp b/tests/storagebenchmark.cpp
index 6010539..9c95d3b 100644
--- a/tests/storagebenchmark.cpp
+++ b/tests/storagebenchmark.cpp
@@ -118,7 +118,7 @@ private Q_SLOTS:
118 { 118 {
119 for (int i = 0; i < count; i++) { 119 for (int i = 0; i < count; i++) {
120 if (store) { 120 if (store) {
121 store->read(keyPrefix + std::to_string(i), [](std::string value){}); 121 store->read(keyPrefix + std::to_string(i), [](std::string value) -> bool { return true; });
122 } 122 }
123 } 123 }
124 } 124 }
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp
index 3981c4b..1974355 100644
--- a/tests/storagetest.cpp
+++ b/tests/storagetest.cpp
@@ -38,12 +38,16 @@ private:
38 bool success = true; 38 bool success = true;
39 bool keyMatch = true; 39 bool keyMatch = true;
40 const auto reference = keyPrefix + std::to_string(i); 40 const auto reference = keyPrefix + std::to_string(i);
41 success = storage.read(keyPrefix + std::to_string(i), [&keyMatch, &reference](const std::string &value) { 41 storage.read(keyPrefix + std::to_string(i),
42 if (value != reference) { 42 [&keyMatch, &reference](const std::string &value) -> bool {
43 qDebug() << "Mismatch while reading"; 43 if (value != reference) {
44 keyMatch = false; 44 qDebug() << "Mismatch while reading";
45 } 45 keyMatch = false;
46 }); 46 }
47 return keyMatch;
48 },
49 [&success](const Storage::Error &) { success = false; }
50 );
47 return success && keyMatch; 51 return success && keyMatch;
48 } 52 }
49 53