Make updateCounts() faster.

This commit is contained in:
Martin Rotter 2016-04-14 08:39:30 +02:00
parent 7f4a50336c
commit d4532190d8
5 changed files with 83 additions and 1 deletions

View File

@ -220,6 +220,46 @@ QMap<int,int> DatabaseQueries::getMessageCountsForCategory(QSqlDatabase db, int
return counts;
}
QMap<int,int> DatabaseQueries::getMessageCountsForAccount(QSqlDatabase db, int account_id,
bool including_total_counts, bool *ok) {
QMap<int,int> 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);

View File

@ -43,6 +43,8 @@ class DatabaseQueries {
static bool purgeRecycleBin(QSqlDatabase db);
static QMap<int,int> getMessageCountsForCategory(QSqlDatabase db, int custom_id, int account_id,
bool including_total_counts, bool *ok = NULL);
static QMap<int,int> 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);

View File

@ -75,6 +75,44 @@ QList<QAction*> ServiceRoot::serviceMenu() {
return QList<QAction*>();
}
void ServiceRoot::updateCounts(bool including_total_count) {
QList<Feed*> 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<int,int> counts = DatabaseQueries::getMessageCountsForAccount(database, accountId(), including_total_count, &ok);
if (ok) {
foreach (Feed *feed, feeds) {
feed->setCountOfAllMessages(counts.value(feed->customId()));
}
}
}
QMap<int,int> 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);

View File

@ -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<Message> undeletedMessages() const;
// Start/stop services.

View File

@ -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() {