summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/messagequeue.cpp16
-rw-r--r--common/messagequeue.h1
-rw-r--r--common/storage_lmdb.cpp24
3 files changed, 26 insertions, 15 deletions
diff --git a/common/messagequeue.cpp b/common/messagequeue.cpp
index e1bde4b..99a0112 100644
--- a/common/messagequeue.cpp
+++ b/common/messagequeue.cpp
@@ -21,8 +21,13 @@ void MessageQueue::enqueue(void const *msg, size_t size)
21void MessageQueue::dequeue(const std::function<void(void *ptr, int size, std::function<void(bool success)>)> &resultHandler, 21void MessageQueue::dequeue(const std::function<void(void *ptr, int size, std::function<void(bool success)>)> &resultHandler,
22 const std::function<void(const Error &error)> &errorHandler) 22 const std::function<void(const Error &error)> &errorHandler)
23{ 23{
24 mStorage.scan("", 0, [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { 24 bool readValue = false;
25 const std::string key(static_cast<char*>(keyPtr), keySize); 25 mStorage.scan("", 0, [this, resultHandler, &readValue](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool {
26 const auto key = QByteArray::fromRawData(static_cast<char*>(keyPtr), keySize);
27 if (key.startsWith("__internal")) {
28 return true;
29 }
30 readValue = true;
26 resultHandler(valuePtr, valueSize, [this, key](bool success) { 31 resultHandler(valuePtr, valueSize, [this, key](bool success) {
27 if (success) { 32 if (success) {
28 mStorage.remove(key.data(), key.size()); 33 mStorage.remove(key.data(), key.size());
@@ -37,16 +42,21 @@ void MessageQueue::dequeue(const std::function<void(void *ptr, int size, std::fu
37 errorHandler(Error(error.store, error.code, error.message)); 42 errorHandler(Error(error.store, error.code, error.message));
38 } 43 }
39 ); 44 );
45 if (!readValue) {
46 errorHandler(Error("messagequeue", -1, "No message found"));
47 }
40} 48}
41 49
42bool MessageQueue::isEmpty() 50bool MessageQueue::isEmpty()
43{ 51{
44 int count = 0; 52 int count = 0;
45 mStorage.scan("", [&count](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { 53 mStorage.scan("", [&count](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool {
46 const QByteArray key(static_cast<char*>(keyPtr), keySize); 54 const auto key = QByteArray::fromRawData(static_cast<char*>(keyPtr), keySize);
47 if (!key.startsWith("__internal")) { 55 if (!key.startsWith("__internal")) {
48 count++; 56 count++;
57 return false;
49 } 58 }
59 return true;
50 }); 60 });
51 return count == 0; 61 return count == 0;
52} 62}
diff --git a/common/messagequeue.h b/common/messagequeue.h
index 8783421..0b791c6 100644
--- a/common/messagequeue.h
+++ b/common/messagequeue.h
@@ -36,5 +36,6 @@ signals:
36 void messageReady(); 36 void messageReady();
37 37
38private: 38private:
39 Q_DISABLE_COPY(MessageQueue);
39 Akonadi2::Storage mStorage; 40 Akonadi2::Storage mStorage;
40}; 41};
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp
index ae2be23..2dc4817 100644
--- a/common/storage_lmdb.cpp
+++ b/common/storage_lmdb.cpp
@@ -218,7 +218,6 @@ bool Storage::write(void const *keyPtr, size_t keySize, void const *valuePtr, si
218 218
219 const bool implicitTransaction = !d->transaction || d->readTransaction; 219 const bool implicitTransaction = !d->transaction || d->readTransaction;
220 if (implicitTransaction) { 220 if (implicitTransaction) {
221 // TODO: if this fails, still try the write below?
222 if (!startTransaction()) { 221 if (!startTransaction()) {
223 return false; 222 return false;
224 } 223 }
@@ -292,7 +291,6 @@ void Storage::scan(const char *keyData, uint keySize,
292 291
293 const bool implicitTransaction = !d->transaction; 292 const bool implicitTransaction = !d->transaction;
294 if (implicitTransaction) { 293 if (implicitTransaction) {
295 // TODO: if this fails, still try the write below?
296 if (!startTransaction(ReadOnly)) { 294 if (!startTransaction(ReadOnly)) {
297 Error error(d->name.toStdString(), -2, "Could not start transaction"); 295 Error error(d->name.toStdString(), -2, "Could not start transaction");
298 errorHandler(error); 296 errorHandler(error);
@@ -308,19 +306,18 @@ void Storage::scan(const char *keyData, uint keySize,
308 } 306 }
309 307
310 if (!keyData || keySize == 0) { 308 if (!keyData || keySize == 0) {
311 bool gotResult = false; 309 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) {
312 if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0 && 310 if (resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) {
313 resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { 311 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
314 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { 312 if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) {
315 gotResult = true; 313 break;
316 if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { 314 }
317 break;
318 } 315 }
319 } 316 }
320 } 317 }
321 318
322 //We never find the last value, but ensure we got at least one. 319 //We never find the last value, but ensure we got at least one.
323 if (gotResult && rc == MDB_NOTFOUND) { 320 if (rc == MDB_NOTFOUND) {
324 rc = 0; 321 rc = 0;
325 } 322 }
326 } else { 323 } else {
@@ -400,8 +397,11 @@ qint64 Storage::diskUsage() const
400void Storage::removeFromDisk() const 397void Storage::removeFromDisk() const
401{ 398{
402 QDir dir(d->storageRoot + '/' + d->name); 399 QDir dir(d->storageRoot + '/' + d->name);
403 dir.remove("data.mdb"); 400 // dir.remove("data.mdb");
404 dir.remove("lock.mdb"); 401 // dir.remove("lock.mdb");
402 if (!dir.removeRecursively()) {
403 qWarning() << "Failed to remove directory" << d->storageRoot << d->name;
404 }
405} 405}
406 406
407} // namespace Akonadi2 407} // namespace Akonadi2