From a31755bbc81ea94bc760688ce19462ef2742c9ed Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Wed, 30 Nov 2022 09:50:26 +0300 Subject: [PATCH] Switch SQLite to use WAL journaling mode PR #18048. --- src/base/bittorrent/dbresumedatastorage.cpp | 28 +++++++++++++++++++-- src/base/bittorrent/dbresumedatastorage.h | 1 + 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/base/bittorrent/dbresumedatastorage.cpp b/src/base/bittorrent/dbresumedatastorage.cpp index dfaece90e..f8cae088d 100644 --- a/src/base/bittorrent/dbresumedatastorage.cpp +++ b/src/base/bittorrent/dbresumedatastorage.cpp @@ -452,9 +452,17 @@ int BitTorrent::DBResumeDataStorage::currentDBVersion() const void BitTorrent::DBResumeDataStorage::createDB() const { - auto db = QSqlDatabase::database(DB_CONNECTION_NAME); + try + { + enableWALMode(); + } + catch (const RuntimeError &err) + { + LogMsg(tr("Couldn't enable Write-Ahead Logging (WAL) journaling mode. Error: %1.") + .arg(err.message()), Log::WARNING); + } - const QWriteLocker locker {&m_dbLock}; + auto db = QSqlDatabase::database(DB_CONNECTION_NAME); if (!db.transaction()) throw RuntimeError(db.lastError().text()); @@ -568,6 +576,22 @@ void BitTorrent::DBResumeDataStorage::updateDB(const int fromVersion) const } } +void BitTorrent::DBResumeDataStorage::enableWALMode() const +{ + auto db = QSqlDatabase::database(DB_CONNECTION_NAME); + QSqlQuery query {db}; + + if (!query.exec(u"PRAGMA journal_mode = WAL;"_qs)) + throw RuntimeError(query.lastError().text()); + + if (!query.next()) + throw RuntimeError(tr("Couldn't obtain query result.")); + + const QString result = query.value(0).toString(); + if (result.compare(u"WAL"_qs, Qt::CaseInsensitive) != 0) + throw RuntimeError(tr("WAL mode is probably unsupported due to filesystem limitations.")); +} + BitTorrent::DBResumeDataStorage::Worker::Worker(const Path &dbPath, const QString &dbConnectionName, QReadWriteLock &dbLock) : m_path {dbPath} , m_connectionName {dbConnectionName} diff --git a/src/base/bittorrent/dbresumedatastorage.h b/src/base/bittorrent/dbresumedatastorage.h index 1dd9392cb..f5959363a 100644 --- a/src/base/bittorrent/dbresumedatastorage.h +++ b/src/base/bittorrent/dbresumedatastorage.h @@ -58,6 +58,7 @@ namespace BitTorrent int currentDBVersion() const; void createDB() const; void updateDB(int fromVersion) const; + void enableWALMode() const; QThread *m_ioThread = nullptr;