From 21606b4ff8e7cfffe581186622022ee4058e38b3 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 26 Feb 2017 22:02:47 +0100 Subject: Attempt to fix the issue of opening the wrong database --- common/storage_lmdb.cpp | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'common') diff --git a/common/storage_lmdb.cpp b/common/storage_lmdb.cpp index 9789c61..64f7db9 100644 --- a/common/storage_lmdb.cpp +++ b/common/storage_lmdb.cpp @@ -43,10 +43,12 @@ namespace Storage { extern QMutex sMutex; extern QHash sEnvironments; +extern QHash sDbis; QMutex sMutex; QHash sEnvironments; +QHash sDbis; int getErrorCode(int e) { @@ -87,16 +89,25 @@ public: if (allowDuplicates) { flags |= MDB_DUPSORT; } - Q_ASSERT(transaction); - if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) { - dbi = 0; - transaction = 0; - // The database is not existing, ignore in read-only mode - if (!(readOnly && rc == MDB_NOTFOUND)) { - Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc))); - errorHandler ? errorHandler(error) : defaultErrorHandler(error); + + QMutexLocker locker(&sMutex); + const auto dbiName = name + db; + if (sDbis.contains(dbiName)) { + dbi = sDbis.value(dbiName); + } else { + Q_ASSERT(transaction); + if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) { + dbi = 0; + transaction = 0; + // The database is not existing, ignore in read-only mode + if (!(readOnly && rc == MDB_NOTFOUND)) { + SinkWarning() << "Failed to open db " << QByteArray(mdb_strerror(rc)); + Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc))); + errorHandler ? errorHandler(error) : defaultErrorHandler(error); + } + return false; } - return false; + sDbis.insert(dbiName, dbi); } return true; } @@ -705,6 +716,11 @@ void DataStore::removeFromDisk() const QMutexLocker locker(&sMutex); SinkTrace() << "Removing database from disk: " << fullPath; sEnvironments.take(fullPath); + for (const auto &key : sDbis.keys()) { + if (key.startsWith(d->name)) { + sDbis.remove(key); + } + } auto env = sEnvironments.take(fullPath); mdb_env_close(env); QDir dir(fullPath); @@ -716,9 +732,11 @@ void DataStore::removeFromDisk() const void DataStore::clearEnv() { + QMutexLocker locker(&sMutex); for (auto env : sEnvironments) { mdb_env_close(env); } + sDbis.clear(); sEnvironments.clear(); } -- cgit v1.2.3