summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/storage_lmdb.cpp36
1 files changed, 27 insertions, 9 deletions
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 {
43 43
44extern QMutex sMutex; 44extern QMutex sMutex;
45extern QHash<QString, MDB_env *> sEnvironments; 45extern QHash<QString, MDB_env *> sEnvironments;
46extern QHash<QString, MDB_dbi> sDbis;
46 47
47 48
48QMutex sMutex; 49QMutex sMutex;
49QHash<QString, MDB_env *> sEnvironments; 50QHash<QString, MDB_env *> sEnvironments;
51QHash<QString, MDB_dbi> sDbis;
50 52
51int getErrorCode(int e) 53int getErrorCode(int e)
52{ 54{
@@ -87,16 +89,25 @@ public:
87 if (allowDuplicates) { 89 if (allowDuplicates) {
88 flags |= MDB_DUPSORT; 90 flags |= MDB_DUPSORT;
89 } 91 }
90 Q_ASSERT(transaction); 92
91 if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) { 93 QMutexLocker locker(&sMutex);
92 dbi = 0; 94 const auto dbiName = name + db;
93 transaction = 0; 95 if (sDbis.contains(dbiName)) {
94 // The database is not existing, ignore in read-only mode 96 dbi = sDbis.value(dbiName);
95 if (!(readOnly && rc == MDB_NOTFOUND)) { 97 } else {
96 Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc))); 98 Q_ASSERT(transaction);
97 errorHandler ? errorHandler(error) : defaultErrorHandler(error); 99 if (const int rc = mdb_dbi_open(transaction, db.constData(), flags, &dbi)) {
100 dbi = 0;
101 transaction = 0;
102 // The database is not existing, ignore in read-only mode
103 if (!(readOnly && rc == MDB_NOTFOUND)) {
104 SinkWarning() << "Failed to open db " << QByteArray(mdb_strerror(rc));
105 Error error(name.toLatin1(), ErrorCodes::GenericError, "Error while opening database: " + QByteArray(mdb_strerror(rc)));
106 errorHandler ? errorHandler(error) : defaultErrorHandler(error);
107 }
108 return false;
98 } 109 }
99 return false; 110 sDbis.insert(dbiName, dbi);
100 } 111 }
101 return true; 112 return true;
102 } 113 }
@@ -705,6 +716,11 @@ void DataStore::removeFromDisk() const
705 QMutexLocker locker(&sMutex); 716 QMutexLocker locker(&sMutex);
706 SinkTrace() << "Removing database from disk: " << fullPath; 717 SinkTrace() << "Removing database from disk: " << fullPath;
707 sEnvironments.take(fullPath); 718 sEnvironments.take(fullPath);
719 for (const auto &key : sDbis.keys()) {
720 if (key.startsWith(d->name)) {
721 sDbis.remove(key);
722 }
723 }
708 auto env = sEnvironments.take(fullPath); 724 auto env = sEnvironments.take(fullPath);
709 mdb_env_close(env); 725 mdb_env_close(env);
710 QDir dir(fullPath); 726 QDir dir(fullPath);
@@ -716,9 +732,11 @@ void DataStore::removeFromDisk() const
716 732
717void DataStore::clearEnv() 733void DataStore::clearEnv()
718{ 734{
735 QMutexLocker locker(&sMutex);
719 for (auto env : sEnvironments) { 736 for (auto env : sEnvironments) {
720 mdb_env_close(env); 737 mdb_env_close(env);
721 } 738 }
739 sDbis.clear();
722 sEnvironments.clear(); 740 sEnvironments.clear();
723} 741}
724 742