diff options
-rw-r--r-- | common/storage.h | 2 | ||||
-rw-r--r-- | common/storage_lmdb.cpp | 16 | ||||
-rw-r--r-- | tests/storagetest.cpp | 13 |
3 files changed, 28 insertions, 3 deletions
diff --git a/common/storage.h b/common/storage.h index 075fcfd..04e3142 100644 --- a/common/storage.h +++ b/common/storage.h | |||
@@ -68,6 +68,8 @@ public: | |||
68 | const std::function<bool(void *keyPtr, int keySize, void *ptr, int size)> &resultHandler, | 68 | const std::function<bool(void *keyPtr, int keySize, void *ptr, int size)> &resultHandler, |
69 | const std::function<void(const Storage::Error &error)> &errorHandler); | 69 | const std::function<void(const Storage::Error &error)> &errorHandler); |
70 | void remove(void const *keyData, uint keySize); | 70 | void remove(void const *keyData, uint keySize); |
71 | void remove(void const *keyData, uint keySize, | ||
72 | const std::function<void(const Storage::Error &error)> &errorHandler); | ||
71 | 73 | ||
72 | static std::function<void(const Storage::Error &error)> basicErrorHandler(); | 74 | static std::function<void(const Storage::Error &error)> basicErrorHandler(); |
73 | qint64 diskUsage() const; | 75 | qint64 diskUsage() const; |
diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 7d06f96..13a6853 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp | |||
@@ -333,19 +333,28 @@ void Storage::scan(const char *keyData, uint keySize, | |||
333 | 333 | ||
334 | void Storage::remove(void const *keyData, uint keySize) | 334 | void Storage::remove(void const *keyData, uint keySize) |
335 | { | 335 | { |
336 | remove(keyData, keySize, basicErrorHandler()); | ||
337 | } | ||
338 | |||
339 | void Storage::remove(void const *keyData, uint keySize, const std::function<void(const Storage::Error &error)> &errorHandler) | ||
340 | { | ||
336 | if (!d->env) { | 341 | if (!d->env) { |
342 | Error error(d->name.toStdString(), -1, "Not open"); | ||
343 | errorHandler(error); | ||
337 | return; | 344 | return; |
338 | } | 345 | } |
339 | 346 | ||
340 | if (d->mode == ReadOnly) { | 347 | if (d->mode == ReadOnly) { |
341 | std::cerr << "tried to write in read-only mode." << std::endl; | 348 | Error error(d->name.toStdString(), -3, "Tried to write in read-only mode"); |
349 | errorHandler(error); | ||
342 | return; | 350 | return; |
343 | } | 351 | } |
344 | 352 | ||
345 | const bool implicitTransaction = !d->transaction || d->readTransaction; | 353 | const bool implicitTransaction = !d->transaction || d->readTransaction; |
346 | if (implicitTransaction) { | 354 | if (implicitTransaction) { |
347 | // TODO: if this fails, still try the write below? | ||
348 | if (!startTransaction()) { | 355 | if (!startTransaction()) { |
356 | Error error(d->name.toStdString(), -2, "Could not start transaction"); | ||
357 | errorHandler(error); | ||
349 | return; | 358 | return; |
350 | } | 359 | } |
351 | } | 360 | } |
@@ -357,7 +366,8 @@ void Storage::remove(void const *keyData, uint keySize) | |||
357 | rc = mdb_del(d->transaction, d->dbi, &key, 0); | 366 | rc = mdb_del(d->transaction, d->dbi, &key, 0); |
358 | 367 | ||
359 | if (rc) { | 368 | if (rc) { |
360 | std::cerr << "mdb_del: " << rc << " " << mdb_strerror(rc) << std::endl; | 369 | Error error(d->name.toStdString(), -1, QString("Error on mdb_del: %1 %2").arg(rc).arg(mdb_strerror(rc)).toStdString()); |
370 | errorHandler(error); | ||
361 | } | 371 | } |
362 | 372 | ||
363 | if (implicitTransaction) { | 373 | if (implicitTransaction) { |
diff --git a/tests/storagetest.cpp b/tests/storagetest.cpp index a771042..a405de0 100644 --- a/tests/storagetest.cpp +++ b/tests/storagetest.cpp | |||
@@ -120,6 +120,19 @@ private Q_SLOTS: | |||
120 | storage.removeFromDisk(); | 120 | storage.removeFromDisk(); |
121 | } | 121 | } |
122 | 122 | ||
123 | void testTurnReadToWrite() | ||
124 | { | ||
125 | populate(3); | ||
126 | Akonadi2::Storage store(testDataPath, dbName, Akonadi2::Storage::ReadWrite); | ||
127 | store.scan("key1", [&](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
128 | store.remove(keyValue, keySize, [](const Akonadi2::Storage::Error &) { | ||
129 | QVERIFY(false); | ||
130 | }); | ||
131 | return false; | ||
132 | }); | ||
133 | store.removeFromDisk(); | ||
134 | } | ||
135 | |||
123 | void testConcurrentRead() | 136 | void testConcurrentRead() |
124 | { | 137 | { |
125 | const int count = 10000; | 138 | const int count = 10000; |