Skip to content

Commit

Permalink
feat(LeftJoinModel): Use QAbstractItemModel::match for searching key …
Browse files Browse the repository at this point in the history
…idx in right model

It creates a possibility to transparently improve performance of id lookup
in right model by implementing fast search on given role in the righ model
itself by reimplementing QAbstractItemModel::match.

Closes: #12574
  • Loading branch information
micieslak committed Oct 31, 2023
1 parent 7847cb3 commit 04b6fb5
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions ui/StatusQ/src/leftjoinmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ QVariant LeftJoinModel::data(const QModelIndex& index, int role) const
if (m_rightModelDestroyed)
return {};

QVariant joinRoleLeftValue = m_leftModel->data(idx, m_leftModelJoinRole);
auto joinRoleLeftValue = m_leftModel->data(idx, m_leftModelJoinRole);

if (m_lastUsedRightModelIndex.isValid()
&& m_rightModel->data(m_lastUsedRightModelIndex,
Expand All @@ -139,22 +139,15 @@ QVariant LeftJoinModel::data(const QModelIndex& index, int role) const
role - m_rightModelRolesOffset);
}

int rightModelCount = m_rightModel->rowCount();
QModelIndexList match = m_rightModel->match(
m_rightModel->index(0, 0), m_rightModelJoinRole,
joinRoleLeftValue, 1, Qt::MatchExactly);

for (int i = 0; i < rightModelCount; i++) {
auto rightModelIdx = m_rightModel->index(i, 0);
auto rightJointRoleValue = m_rightModel->data(rightModelIdx,
m_rightModelJoinRole);

if (joinRoleLeftValue == rightJointRoleValue) {
m_lastUsedRightModelIndex = rightModelIdx;

return m_rightModel->data(rightModelIdx,
role - m_rightModelRolesOffset);
}
}
if (match.empty())
return {};

return {};
m_lastUsedRightModelIndex = match.first();
return match.first().data(role - m_rightModelRolesOffset);
}

void LeftJoinModel::setLeftModel(QAbstractItemModel* model)
Expand Down

0 comments on commit 04b6fb5

Please sign in to comment.