From a4aeea4322252725165cb62390e880f2861035a6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 11 Jan 2015 16:09:11 +0100 Subject: Remove from storage call. --- common/storage.h | 1 + common/storage_lmdb.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'common') diff --git a/common/storage.h b/common/storage.h index 6ae7838..075fcfd 100644 --- a/common/storage.h +++ b/common/storage.h @@ -67,6 +67,7 @@ public: void scan(const char *keyData, uint keySize, const std::function &resultHandler, const std::function &errorHandler); + void remove(void const *keyData, uint keySize); static std::function basicErrorHandler(); qint64 diskUsage() const; diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index ce271ad..7d06f96 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -331,6 +331,46 @@ void Storage::scan(const char *keyData, uint keySize, } } +void Storage::remove(void const *keyData, uint keySize) +{ + if (!d->env) { + return; + } + + if (d->mode == ReadOnly) { + std::cerr << "tried to write in read-only mode." << std::endl; + return; + } + + const bool implicitTransaction = !d->transaction || d->readTransaction; + if (implicitTransaction) { + // TODO: if this fails, still try the write below? + if (!startTransaction()) { + return; + } + } + + int rc; + MDB_val key; + key.mv_size = keySize; + key.mv_data = const_cast(keyData); + rc = mdb_del(d->transaction, d->dbi, &key, 0); + + if (rc) { + std::cerr << "mdb_del: " << rc << " " << mdb_strerror(rc) << std::endl; + } + + if (implicitTransaction) { + if (rc) { + abortTransaction(); + } else { + rc = commitTransaction(); + } + } + + return; +} + qint64 Storage::diskUsage() const { QFileInfo info(d->storageRoot + '/' + d->name + "/data.mdb"); -- cgit v1.2.3