From d4532190d8d7739184b5f778ebb13f068ef81ca3 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 14 Apr 2016 08:39:30 +0200 Subject: [PATCH] Make updateCounts() faster. --- src/miscellaneous/databasequeries.cpp | 40 +++++++++++++++++++ src/miscellaneous/databasequeries.h | 2 + src/services/abstract/serviceroot.cpp | 38 ++++++++++++++++++ src/services/abstract/serviceroot.h | 2 + src/services/standard/standardserviceroot.cpp | 2 +- 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/miscellaneous/databasequeries.cpp b/src/miscellaneous/databasequeries.cpp index 38ec3d886..f1ef39cca 100644 --- a/src/miscellaneous/databasequeries.cpp +++ b/src/miscellaneous/databasequeries.cpp @@ -220,6 +220,46 @@ QMap DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int return counts; } +QMap DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id, + bool including_total_counts, bool *ok) { + QMap counts; + QSqlQuery q(db); + q.setForwardOnly(true); + + if (including_total_counts) { + q.prepare("SELECT feed, count(*) FROM Messages " + "WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id " + "GROUP BY feed;"); + } + else { + q.prepare("SELECT feed, count(*) FROM Messages " + "WHERE feed IN (SELECT custom_id FROM Feeds WHERE account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = :account_id " + "GROUP BY feed;"); + } + + q.bindValue(QSL(":account_id"), account_id); + + if (q.exec()) { + while (q.next()) { + int feed_id = q.value(0).toInt(); + int new_count = q.value(1).toInt(); + + counts.insert(feed_id, new_count); + } + + if (ok != NULL) { + *ok = true; + } + } + else { + if (ok != NULL) { + *ok = false; + } + } + + return counts; +} + int DatabaseQueries::getMessageCountsForFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool including_total_counts, bool *ok) { QSqlQuery q(db); diff --git a/src/miscellaneous/databasequeries.h b/src/miscellaneous/databasequeries.h index 9d3c992eb..97ae93352 100644 --- a/src/miscellaneous/databasequeries.h +++ b/src/miscellaneous/databasequeries.h @@ -43,6 +43,8 @@ class DatabaseQueries { static bool purgeRecycleBin(QSqlDatabase db); static QMap getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id, bool including_total_counts, bool *ok = NULL); + static QMap getMessageCountsForAccount(QSqlDatabase db, int account_id, + bool including_total_counts, bool *ok = NULL); static int getMessageCountsForFeed(QSqlDatabase db, int feed_custom_id, int account_id, bool including_total_counts, bool *ok = NULL); static int getMessageCountsForBin(QSqlDatabase db, int account_id, bool including_total_counts, bool *ok = NULL); diff --git a/src/services/abstract/serviceroot.cpp b/src/services/abstract/serviceroot.cpp index 5b3dc693e..2d576d2ef 100755 --- a/src/services/abstract/serviceroot.cpp +++ b/src/services/abstract/serviceroot.cpp @@ -75,6 +75,44 @@ QList ServiceRoot::serviceMenu() { return QList(); } +void ServiceRoot::updateCounts(bool including_total_count) { + QList feeds; + + foreach (RootItem *child, getSubTree()) { + if (child->kind() == RootItemKind::Feed) { + feeds.append(child->toFeed()); + } + else if (child->kind() != RootItemKind::Category && child->kind() != RootItemKind::ServiceRoot) { + child->updateCounts(including_total_count); + } + } + + if (feeds.isEmpty()) { + return; + } + + QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); + bool ok; + + if (including_total_count) { + QMap counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok); + + if (ok) { + foreach (Feed *feed, feeds) { + feed->setCountOfAllMessages(counts.value(feed->customId())); + } + } + } + + QMap counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), false, &ok); + + if (ok) { + foreach (Feed *feed, feeds) { + feed->setCountOfUnreadMessages(counts.value(feed->customId())); + } + } +} + void ServiceRoot::completelyRemoveAllData() { // Purge old data from SQL and clean all model items. removeOldFeedTree(true); diff --git a/src/services/abstract/serviceroot.h b/src/services/abstract/serviceroot.h index 8eb7604b6..6e4d5e5c3 100755 --- a/src/services/abstract/serviceroot.h +++ b/src/services/abstract/serviceroot.h @@ -69,6 +69,8 @@ class ServiceRoot : public RootItem { // Access to recycle bin of this account if there is any. virtual RecycleBin *recycleBin() const = 0; + void updateCounts(bool including_total_count); + QList undeletedMessages() const; // Start/stop services. diff --git a/src/services/standard/standardserviceroot.cpp b/src/services/standard/standardserviceroot.cpp index 762fcadd1..9f3e00509 100755 --- a/src/services/standard/standardserviceroot.cpp +++ b/src/services/standard/standardserviceroot.cpp @@ -181,7 +181,7 @@ void StandardServiceRoot::loadFromDatabase(){ // As the last item, add recycle bin, which is needed. appendChild(m_recycleBin); - //updateCounts(true); + updateCounts(true); } void StandardServiceRoot::checkArgumentsForFeedAdding() {