diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-26 22:02:47 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-02-26 22:02:47 +0100 |
commit | 21606b4ff8e7cfffe581186622022ee4058e38b3 (patch) | |
tree | 09d409596f5a3e24ea41e197ba36c3f334c07f37 | |
parent | 26c2c7efb7ffc74ec7dbec4f6302d792a7cfad36 (diff) | |
download | sink-21606b4ff8e7cfffe581186622022ee4058e38b3.tar.gz sink-21606b4ff8e7cfffe581186622022ee4058e38b3.zip |
Attempt to fix the issue of opening the wrong database
-rw-r--r-- | common/storage_lmdb.cpp | 36 |
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 | ||
44 | extern QMutex sMutex; | 44 | extern QMutex sMutex; |
45 | extern QHash<QString, MDB_env *> sEnvironments; | 45 | extern QHash<QString, MDB_env *> sEnvironments; |
46 | extern QHash<QString, MDB_dbi> sDbis; | ||
46 | 47 | ||
47 | 48 | ||
48 | QMutex sMutex; | 49 | QMutex sMutex; |
49 | QHash<QString, MDB_env *> sEnvironments; | 50 | QHash<QString, MDB_env *> sEnvironments; |
51 | QHash<QString, MDB_dbi> sDbis; | ||
50 | 52 | ||
51 | int getErrorCode(int e) | 53 | int 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 | ||
717 | void DataStore::clearEnv() | 733 | void 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 | ||