From c51aa2d573a4eb7de19ceb4a3a62ca01300d52b4 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Tue, 28 Feb 2023 08:57:51 +0300 Subject: [PATCH] Index torrents table by queue position PR #18623. --- src/base/bittorrent/dbresumedatastorage.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/dbresumedatastorage.cpp b/src/base/bittorrent/dbresumedatastorage.cpp index 7d7ff7558..759780517 100644 --- a/src/base/bittorrent/dbresumedatastorage.cpp +++ b/src/base/bittorrent/dbresumedatastorage.cpp @@ -61,7 +61,7 @@ namespace { const QString DB_CONNECTION_NAME = u"ResumeDataStorage"_qs; - const int DB_VERSION = 3; + const int DB_VERSION = 4; const QString DB_TABLE_META = u"meta"_qs; const QString DB_TABLE_TORRENTS = u"torrents"_qs; @@ -511,6 +511,12 @@ void BitTorrent::DBResumeDataStorage::createDB() const if (!query.exec(createTableTorrentsQuery)) throw RuntimeError(query.lastError().text()); + const QString torrentsQueuePositionIndexName = u"%1_%2_INDEX"_qs.arg(DB_TABLE_TORRENTS, DB_COLUMN_QUEUE_POSITION.name); + const QString createTorrentsQueuePositionIndexQuery = u"CREATE INDEX %1 ON %2 (%3)"_qs + .arg(quoted(torrentsQueuePositionIndexName), quoted(DB_TABLE_TORRENTS), quoted(DB_COLUMN_QUEUE_POSITION.name)); + if (!query.exec(createTorrentsQueuePositionIndexQuery)) + throw RuntimeError(query.lastError().text()); + if (!db.commit()) throw RuntimeError(db.lastError().text()); } @@ -553,6 +559,15 @@ void BitTorrent::DBResumeDataStorage::updateDB(const int fromVersion) const throw RuntimeError(query.lastError().text()); } + if (fromVersion <= 3) + { + const QString torrentsQueuePositionIndexName = u"%1_%2_INDEX"_qs.arg(DB_TABLE_TORRENTS, DB_COLUMN_QUEUE_POSITION.name); + const QString createTorrentsQueuePositionIndexQuery = u"CREATE INDEX %1 ON %2 (%3)"_qs + .arg(quoted(torrentsQueuePositionIndexName), quoted(DB_TABLE_TORRENTS), quoted(DB_COLUMN_QUEUE_POSITION.name)); + if (!query.exec(createTorrentsQueuePositionIndexQuery)) + throw RuntimeError(query.lastError().text()); + } + const QString updateMetaVersionQuery = makeUpdateStatement(DB_TABLE_META, {DB_COLUMN_NAME, DB_COLUMN_VALUE}); if (!query.prepare(updateMetaVersionQuery)) throw RuntimeError(query.lastError().text());