From 70a114eb13ea9415cd2275877b524f8b86edb052 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Sat, 1 Nov 2014 20:41:30 +0300 Subject: [PATCH] Speedup persistent data Conflicts: src/mainwindow.cpp src/mainwindow.h --- src/mainwindow.cpp | 11 +++++ src/mainwindow.h | 2 + src/torrentpersistentdata.cpp | 92 +++++++++++------------------------ src/torrentpersistentdata.h | 7 +++ 4 files changed, 48 insertions(+), 64 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index fc44ab223..30a96094f 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -243,6 +243,11 @@ MainWindow::MainWindow(QWidget *parent, const QStringList& torrentCmdLine): QMai preventTimer = new QTimer(this); connect(preventTimer, SIGNAL(timeout()), SLOT(checkForActiveTorrents())); + // Initialization of persistent data + TorrentPersistentData::load(); + connect(&persistentDataSaver, SIGNAL(timeout()), SLOT(savePersistentData())); + persistentDataSaver.start(10000); + // Configure BT session according to options loadPreferences(false); @@ -459,6 +464,7 @@ void MainWindow::shutdownCleanUp() QBtSession::drop(); // Save window size, columns size writeSettings(); + TorrentPersistentData::save(); #ifdef Q_OS_MAC // Workaround to avoid bug http://bugreports.qt.nokia.com/browse/QTBUG-7305 setUnifiedTitleAndToolBarOnMac(false); @@ -1586,6 +1592,11 @@ void MainWindow::handleUpdateCheckFinished(bool update_available, QString new_ve } #endif +void MainWindow::savePersistentData() +{ + TorrentPersistentData::save(); +} + void MainWindow::on_actionDonate_money_triggered() { QDesktopServices::openUrl(QUrl("http://sourceforge.net/donate/index.php?group_id=163414")); diff --git a/src/mainwindow.h b/src/mainwindow.h index b18559ba1..007473b26 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -143,6 +143,7 @@ protected slots: #if defined(Q_OS_WIN) || defined(Q_OS_MAC) void handleUpdateCheckFinished(bool update_available, QString new_version, bool invokedByUser); #endif + void savePersistentData(); protected: void closeEvent(QCloseEvent *); @@ -216,6 +217,7 @@ private: bool has_python; #endif QMenu* toolbarMenu; + QTimer persistentDataSaver; private slots: void on_actionSearch_engine_triggered(); diff --git a/src/torrentpersistentdata.cpp b/src/torrentpersistentdata.cpp index 12cd30c9d..4f9a0eb52 100644 --- a/src/torrentpersistentdata.cpp +++ b/src/torrentpersistentdata.cpp @@ -44,6 +44,8 @@ QHash TorrentTempData::data = QHash TorrentTempData::torrentMoveStates = QHash(); QHash HiddenData::data = QHash(); unsigned int HiddenData::metadata_counter = 0; +QHash TorrentPersistentData::all_data = QHash(); +bool TorrentPersistentData::dirty = false; bool TorrentTempData::hasTempData(const QString &hash) { return data.contains(hash); @@ -197,37 +199,41 @@ void HiddenData::gotMetadata(const QString &hash) { metadata_counter++; } -bool TorrentPersistentData::isKnownTorrent(QString hash) { +void TorrentPersistentData::load() { QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); + all_data = settings.value("torrents").toHash(); + dirty = false; +} + +void TorrentPersistentData::save() { + if (dirty) { + QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + settings.setValue("torrents", all_data); + dirty = false; + } +} + +bool TorrentPersistentData::isKnownTorrent(QString hash) { return all_data.contains(hash); } QStringList TorrentPersistentData::knownTorrents() { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); return all_data.keys(); } void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["max_ratio"] = ratio; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } qreal TorrentPersistentData::getRatioLimit(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("max_ratio", USE_GLOBAL_RATIO).toReal(); } bool TorrentPersistentData::hasPerTorrentRatioLimit() { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); QHash::ConstIterator it = all_data.constBegin(); QHash::ConstIterator itend = all_data.constEnd(); for ( ; it != itend; ++it) { @@ -239,19 +245,15 @@ bool TorrentPersistentData::hasPerTorrentRatioLimit() { } void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); if (!data.contains("add_date")) { data["add_date"] = time; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } } QDateTime TorrentPersistentData::getAddedDate(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); QDateTime dt = data.value("add_date").toDateTime(); if (!dt.isValid()) { @@ -262,34 +264,26 @@ QDateTime TorrentPersistentData::getAddedDate(const QString &hash) { } void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["has_error"] = has_error; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } bool TorrentPersistentData::hasError(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("has_error", false).toBool(); } QDateTime TorrentPersistentData::getSeedDate(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("seed_date").toDateTime(); } void TorrentPersistentData::deletePersistentData(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); if (all_data.contains(hash)) { all_data.remove(hash); - settings.setValue("torrents", all_data); + dirty = true; } } @@ -297,8 +291,6 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c Q_ASSERT(h.is_valid()); qDebug("Saving persistent data for %s", qPrintable(h.hash())); // Save persistent data - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(h.hash()).toHash(); data["is_magnet"] = is_magnet; if (is_magnet) { @@ -317,7 +309,7 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c data["label"] = TorrentTempData::getLabel(h.hash()); // Save data all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + dirty = true; qDebug("TorrentPersistentData: Saving save_path %s, hash: %s", qPrintable(h.save_path()), qPrintable(h.hash())); // Set Added date setAddedDate(h.hash(), QDateTime::currentDateTime()); @@ -328,120 +320,92 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path) { Q_ASSERT(!hash.isEmpty()); qDebug("TorrentPersistentData::saveSavePath(%s)", qPrintable(save_path)); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["save_path"] = save_path; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; qDebug("TorrentPersistentData: Saving save_path: %s, hash: %s", qPrintable(save_path), qPrintable(hash)); } void TorrentPersistentData::saveLabel(const QString &hash, const QString &label) { Q_ASSERT(!hash.isEmpty()); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["label"] = label; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } void TorrentPersistentData::saveName(const QString &hash, const QString &name) { Q_ASSERT(!hash.isEmpty()); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["name"] = name; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } void TorrentPersistentData::savePriority(const QTorrentHandle &h) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[h.hash()].toHash(); data["priority"] = h.queue_position(); all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + dirty = true; } void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[hash].toHash(); data["priority"] = queue_pos; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[h.hash()].toHash(); bool was_seed = data.value("seed", false).toBool(); if (was_seed != h.is_seed()) { data["seed"] = !was_seed; all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + dirty = true; } } void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[hash].toHash(); data["seed"] = seedStatus; all_data[hash] = data; - settings.setValue("torrents", all_data); + dirty = true; } QString TorrentPersistentData::getSavePath(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); //qDebug("TorrentPersistentData: getSavePath %s", data["save_path"].toString().toLocal8Bit().data()); return data.value("save_path").toString(); } QString TorrentPersistentData::getLabel(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("label", "").toString(); } QString TorrentPersistentData::getName(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("name", "").toString(); } int TorrentPersistentData::getPriority(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("priority", -1).toInt(); } bool TorrentPersistentData::isSeed(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("seed", false).toBool(); } bool TorrentPersistentData::isMagnet(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("is_magnet", false).toBool(); } QString TorrentPersistentData::getMagnetUri(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); Q_ASSERT(data.value("is_magnet", false).toBool()); return data.value("magnet_uri").toString(); diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index c46eb11bf..06dda9f7a 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -121,6 +121,9 @@ public: }; public: + static void load(); + static void save(); + static bool isKnownTorrent(QString hash); static QStringList knownTorrents(); static void setRatioLimit(const QString &hash, const qreal &ratio); @@ -151,6 +154,10 @@ public: static bool isSeed(const QString &hash); static bool isMagnet(const QString &hash); static QString getMagnetUri(const QString &hash); + +private: + static QHash all_data; + static bool dirty; }; #endif // TORRENTPERSISTENTDATA_H