bulk refreshing of article counts of all labels is now hugely faster

This commit is contained in:
Martin Rotter 2023-06-09 11:17:27 +02:00
parent 83e3126343
commit 4b5a503bc8
6 changed files with 74 additions and 2 deletions

View File

@ -654,6 +654,47 @@ ArticleCounts DatabaseQueries::getMessageCountsForLabel(const QSqlDatabase& db,
}
}
QMap<QString, ArticleCounts> DatabaseQueries::getMessageCountsForAllLabels(const QSqlDatabase& db,
int account_id,
bool* ok) {
QMap<QString, ArticleCounts> counts;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT l.custom_id, ('%.' || l.id || '.%') pid, SUM(m.is_read), COUNT(*) FROM Labels l "
"INNER JOIN Messages m "
" ON m.labels LIKE pid "
"WHERE "
" m.is_deleted = 0 AND "
" m.is_pdeleted = 0 AND "
" m.account_id = :account_id "
"GROUP BY pid;"));
q.bindValue(QSL(":account_id"), account_id);
if (q.exec()) {
while (q.next()) {
QString lbl_custom_id = q.value(0).toString();
ArticleCounts ac;
ac.m_total = q.value(3).toInt();
ac.m_unread = ac.m_total - q.value(2).toInt();
counts.insert(lbl_custom_id, ac);
}
if (ok != nullptr) {
*ok = true;
}
}
else {
if (ok != nullptr) {
*ok = false;
}
}
return counts;
}
ArticleCounts DatabaseQueries::getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool* ok) {
QSqlQuery q(db);

View File

@ -90,6 +90,10 @@ class DatabaseQueries {
Label* label,
int account_id,
bool* ok = nullptr);
static QMap<QString, ArticleCounts> getMessageCountsForAllLabels(const QSqlDatabase& db,
int account_id,
bool* ok = nullptr);
static ArticleCounts getImportantMessageCounts(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static int getUnreadMessageCounts(const QSqlDatabase& db, int account_id, bool* ok = nullptr);
static ArticleCounts getMessageCountsForBin(const QSqlDatabase& db, int account_id, bool* ok = nullptr);

View File

@ -31,6 +31,7 @@ LabelsMenu::LabelsMenu(const QList<Message>& messages, const QList<Label*>& labe
.toStdList()) {
auto count = boolinq::from(messages).count([&db, label](const Message& msg) {
// TODO: slow
return DatabaseQueries::isLabelAssignedToMessage(db, label, msg);
});
@ -142,7 +143,7 @@ void LabelAction::updateActionForState() {
break;
case Qt::CheckState::PartiallyChecked:
highlight = QColor(100, 50, 0);
highlight = Qt::GlobalColor::darkYellow;
break;
case Qt::CheckState::Unchecked:

View File

@ -60,6 +60,31 @@ int LabelsNode::countOfAllMessages() const {
->countOfAllMessages();
}
void LabelsNode::updateCounts(bool including_total_count) {
QSqlDatabase database = qApp->database()->driver()->threadSafeConnection(metaObject()->className());
int account_id = getParentServiceRoot()->accountId();
auto acc = DatabaseQueries::getMessageCountsForAllLabels(database, account_id);
for (Label* lbl : labels()) {
if (!acc.contains(lbl->customId())) {
if (including_total_count) {
lbl->setCountOfAllMessages(0);
}
lbl->setCountOfUnreadMessages(0);
}
else {
auto ac = acc.value(lbl->customId());
if (including_total_count) {
lbl->setCountOfAllMessages(ac.m_total);
}
lbl->setCountOfUnreadMessages(ac.m_unread);
}
}
}
QList<Label*> LabelsNode::labels() const {
auto list = boolinq::from(childItems())
.select([](RootItem* it) {

View File

@ -20,6 +20,7 @@ class LabelsNode : public RootItem {
virtual QList<QAction*> contextMenuFeedsList();
virtual int countOfUnreadMessages() const;
virtual int countOfAllMessages() const;
virtual void updateCounts(bool including_total_count);
public slots:
void createLabel();

View File

@ -139,7 +139,7 @@ void ServiceRoot::updateCounts(bool including_total_count) {
if (child->kind() == RootItem::Kind::Feed) {
feeds.append(child->toFeed());
}
else if (child->kind() != RootItem::Kind::Labels && child->kind() != RootItem::Kind::Category &&
else if (child->kind() != RootItem::Kind::Label && child->kind() != RootItem::Kind::Category &&
child->kind() != RootItem::Kind::ServiceRoot) {
child->updateCounts(including_total_count);
}