Switch SQLite to use WAL journaling mode

PR #18048.
This commit is contained in:
Vladimir Golovnev 2022-11-30 09:50:26 +03:00 committed by GitHub
parent b86366f243
commit a31755bbc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View file

@ -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}

View file

@ -58,6 +58,7 @@ namespace BitTorrent
int currentDBVersion() const;
void createDB() const;
void updateDB(int fromVersion) const;
void enableWALMode() const;
QThread *m_ioThread = nullptr;