Performance with recalculating message counts updated.

This commit is contained in:
Martin Rotter 2016-03-17 09:10:04 +01:00
parent dcf50c62c5
commit c25371eca7
12 changed files with 92 additions and 25 deletions

View File

@ -20,6 +20,10 @@ Fixed:
▪ Fixed little problem with feed list hiding. (bug #163)
▪ Web browser search context menu item now trims the searched string. (bug #168)
Changed:
▪ Better performance when recalculating counts of all/unread messages in categories with many feeds.
3.1.0
—————

View File

@ -50,12 +50,6 @@ MessagesView::~MessagesView() {
qDebug("Destroying MessagesView instance.");
}
void MessagesView::setSortingEnabled(bool enable) {
disconnect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(onSortIndicatorChanged(int,Qt::SortOrder)));
QTreeView::setSortingEnabled(enable);
connect(header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(onSortIndicatorChanged(int,Qt::SortOrder)));
}
void MessagesView::createConnections() {
connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(openSelectedSourceMessagesInternallyNoNewTab()));

View File

@ -36,8 +36,6 @@ class MessagesView : public QTreeView {
explicit MessagesView(QWidget *parent = 0);
virtual ~MessagesView();
void setSortingEnabled(bool enable);
// Model accessors.
inline MessagesProxyModel *model() const {
return m_proxyModel;

View File

@ -17,6 +17,12 @@
#include "services/abstract/category.h"
#include "miscellaneous/application.h"
#include "services/abstract/serviceroot.h"
#include "services/abstract/feed.h"
#include <QSqlQuery>
Category::Category(RootItem *parent) : RootItem(parent) {
setKind(RootItemKind::Category);
@ -24,3 +30,68 @@ Category::Category(RootItem *parent) : RootItem(parent) {
Category::~Category() {
}
void Category::updateCounts(bool including_total_count) {
QList<Feed*> feeds;
foreach (RootItem *child, childItems()) {
if (child->kind() == RootItemKind::Feed) {
feeds.append(child->toFeed());
}
else {
child->updateCounts(including_total_count);
}
}
if (feeds.isEmpty()) {
return;
}
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
QSqlQuery query_all(database);
QMap<int,int> counts;
query_all.setForwardOnly(true);
if (including_total_count) {
query_all.prepare("SELECT feed, count(*) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND account_id = :account_id "
"GROUP BY feed;");
query_all.bindValue(QSL(":category"), customId());
query_all.bindValue(QSL(":account_id"), getParentServiceRoot()->accountId());
if (query_all.exec()) {
while (query_all.next()) {
int feed_id = query_all.value(0).toInt();
int new_count = query_all.value(1).toInt();
counts.insert(feed_id, new_count);
}
foreach (Feed *feed, feeds) {
feed->setCountOfAllMessages(counts.value(feed->customId()));
}
}
}
counts.clear();
query_all.prepare("SELECT feed, count(*) FROM Messages "
"WHERE feed IN (SELECT custom_id FROM Feeds WHERE category = :category AND account_id = :account_id) AND is_deleted = 0 AND is_pdeleted = 0 AND is_read = 0 AND account_id = :account_id "
"GROUP BY feed;");
query_all.bindValue(QSL(":category"), customId());
query_all.bindValue(QSL(":account_id"), getParentServiceRoot()->accountId());
// Obtain count of unread messages.
if (query_all.exec()) {
while (query_all.next()) {
int feed_id = query_all.value(0).toInt();
int new_count = query_all.value(1).toInt();
counts.insert(feed_id, new_count);
}
foreach (Feed *feed, feeds) {
feed->setCountOfUnreadMessages(counts.value(feed->customId()));
}
}
}

View File

@ -27,6 +27,8 @@ class Category : public RootItem {
public:
explicit Category(RootItem *parent = NULL);
virtual ~Category();
void updateCounts(bool including_total_count);
};
#endif // CATEGORY_H

View File

@ -101,6 +101,10 @@ void Feed::setCountOfAllMessages(int count_all_messages) {
}
void Feed::setCountOfUnreadMessages(int count_unread_messages) {
if (status() == NewMessages && count_unread_messages < countOfUnreadMessages()) {
setStatus(Normal);
}
m_unreadCount = count_unread_messages;
}
@ -156,13 +160,7 @@ void Feed::updateCounts(bool including_total_count) {
// Obtain count of unread messages.
if (query_all.exec() && query_all.next()) {
int new_unread_count = query_all.value(0).toInt();
if (status() == NewMessages && new_unread_count < countOfUnreadMessages()) {
setStatus(Normal);
}
setCountOfUnreadMessages(new_unread_count);
setCountOfUnreadMessages(query_all.value(0).toInt());
}
}

View File

@ -310,7 +310,7 @@ QHash<int,Category*> RootItem::getHashedSubTreeCategories() const {
RootItem *active_item = traversable_items.takeFirst();
if (active_item->kind() == RootItemKind::Category && !children.contains(active_item->id())) {
children.insert(active_item->id(), active_item->toCategory());
children.insert(active_item->customId(), active_item->toCategory());
}
traversable_items.append(active_item->childItems());

View File

@ -247,7 +247,7 @@ class RootItem : public QObject {
m_boldFont = bold_font;
}
// NOTE: For standard feed, this WILL equal to id().
// NOTE: For standard feed/category, this WILL equal to id().
int customId() const;
void setCustomId(int custom_id);

View File

@ -209,7 +209,7 @@ void ServiceRoot::storeNewFeedTree(RootItem *root) {
query_feed.bindValue(QSL(":title"), feed->title());
query_feed.bindValue(QSL(":icon"), qApp->icons()->toByteArray(feed->icon()));
query_feed.bindValue(QSL(":category"), feed->parent()->id());
query_feed.bindValue(QSL(":category"), feed->parent()->customId());
query_feed.bindValue(QSL(":protected"), 0);
query_feed.bindValue(QSL(":update_type"), (int) feed->autoUpdateType());
query_feed.bindValue(QSL(":update_interval"), feed->autoUpdateInitialInterval());
@ -535,12 +535,10 @@ void ServiceRoot::assembleFeeds(Assignment feeds) {
if (feed.first == NO_PARENT_CATEGORY) {
// This is top-level feed, add it to the root item.
appendChild(feed.second);
feed.second->updateCounts(true);
}
else if (categories.contains(feed.first)) {
// This feed belongs to this category.
categories.value(feed.first)->appendChild(feed.second);
feed.second->updateCounts(true);
}
else {
qWarning("Feed '%s' is loose, skipping it.", qPrintable(feed.second->title()));

View File

@ -99,9 +99,11 @@ RecycleBin *OwnCloudServiceRoot::recycleBin() const {
}
void OwnCloudServiceRoot::start(bool freshly_activated) {
Q_UNUSED(freshly_activated)
loadFromDatabase();
if (childCount() == 1 && child(0)->kind() == RootItemKind::Bin) {
if (qApp->isFirstRun(QSL("3.1.1")) || (childCount() == 1 && child(0)->kind() == RootItemKind::Bin)) {
syncIn();
}
}
@ -345,5 +347,5 @@ void OwnCloudServiceRoot::loadFromDatabase() {
// As the last item, add recycle bin, which is needed.
appendChild(m_recycleBin);
m_recycleBin->updateCounts(true);
updateCounts(true);
}

View File

@ -235,7 +235,7 @@ void StandardServiceRoot::loadFromDatabase(){
// As the last item, add recycle bin, which is needed.
appendChild(m_recycleBin);
m_recycleBin->updateCounts(true);
updateCounts(true);
}
void StandardServiceRoot::checkArgumentsForFeedAdding() {

View File

@ -54,7 +54,7 @@ void TtRssServiceRoot::start(bool freshly_activated) {
loadFromDatabase();
if (childCount() == 1 && child(0)->kind() == RootItemKind::Bin) {
if (qApp->isFirstRun(QSL("3.1.1")) || (childCount() == 1 && child(0)->kind() == RootItemKind::Bin)) {
syncIn();
}
}
@ -340,7 +340,7 @@ void TtRssServiceRoot::loadFromDatabase() {
// As the last item, add recycle bin, which is needed.
appendChild(m_recycleBin);
m_recycleBin->updateCounts(true);
updateCounts(true);
}
void TtRssServiceRoot::updateTitle() {