From e8b2898efcd2225adc7b14329cc246d9b29f1fa6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 15 Jan 2015 23:03:27 +0100 Subject: Fixed messageqeue and storage. Empty scan is not an error. --- common/messagequeue.cpp | 16 +++++++++++++--- common/messagequeue.h | 1 + common/storage_lmdb.cpp | 24 ++++++++++++------------ 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) void MessageQueue::dequeue(const std::function)> &resultHandler, const std::function &errorHandler) { - mStorage.scan("", 0, [this, resultHandler](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { - const std::string key(static_cast(keyPtr), keySize); + bool readValue = false; + mStorage.scan("", 0, [this, resultHandler, &readValue](void *keyPtr, int keySize, void *valuePtr, int valueSize) -> bool { + const auto key = QByteArray::fromRawData(static_cast(keyPtr), keySize); + if (key.startsWith("__internal")) { + return true; + } + readValue = true; resultHandler(valuePtr, valueSize, [this, key](bool success) { if (success) { mStorage.remove(key.data(), key.size()); @@ -37,16 +42,21 @@ void MessageQueue::dequeue(const std::function bool { - const QByteArray key(static_cast(keyPtr), keySize); + const auto key = QByteArray::fromRawData(static_cast(keyPtr), keySize); if (!key.startsWith("__internal")) { count++; + return false; } + return true; }); return count == 0; } 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: void messageReady(); private: + Q_DISABLE_COPY(MessageQueue); Akonadi2::Storage mStorage; }; 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 const bool implicitTransaction = !d->transaction || d->readTransaction; if (implicitTransaction) { - // TODO: if this fails, still try the write below? if (!startTransaction()) { return false; } @@ -292,7 +291,6 @@ void Storage::scan(const char *keyData, uint keySize, const bool implicitTransaction = !d->transaction; if (implicitTransaction) { - // TODO: if this fails, still try the write below? if (!startTransaction(ReadOnly)) { Error error(d->name.toStdString(), -2, "Could not start transaction"); errorHandler(error); @@ -308,19 +306,18 @@ void Storage::scan(const char *keyData, uint keySize, } if (!keyData || keySize == 0) { - bool gotResult = false; - if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0 && - resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { - while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { - gotResult = true; - if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { - break; + if ((rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST)) == 0) { + if (resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { + while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { + if (!resultHandler(key.mv_data, key.mv_size, data.mv_data, data.mv_size)) { + break; + } } } } //We never find the last value, but ensure we got at least one. - if (gotResult && rc == MDB_NOTFOUND) { + if (rc == MDB_NOTFOUND) { rc = 0; } } else { @@ -400,8 +397,11 @@ qint64 Storage::diskUsage() const void Storage::removeFromDisk() const { QDir dir(d->storageRoot + '/' + d->name); - dir.remove("data.mdb"); - dir.remove("lock.mdb"); + // dir.remove("data.mdb"); + // dir.remove("lock.mdb"); + if (!dir.removeRecursively()) { + qWarning() << "Failed to remove directory" << d->storageRoot << d->name; + } } } // namespace Akonadi2 -- cgit v1.2.3