Skip to content

Commit aa40a31

Browse files
committed
Add row cache class
1 parent a274f87 commit aa40a31

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/app/models/key-models/abstractkey.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <QPair>
77
#include "modules/value-editor/keymodel.h"
88
#include "modules/redisclient/redisclient.h"
9+
#include "rowcache.h"
910

1011
template < typename T > class KeyModel : public ValueEditor::Model
1112
{
@@ -300,7 +301,7 @@ template < typename T > class KeyModel : public ValueEditor::Model
300301
QString m_fullLoadingCmd;
301302
bool m_fullLoadingCmdSupportsRanges;
302303

303-
QList<T> m_rowsCache;
304+
MappedCache<T> m_rowsCache;
304305
QSharedPointer<ValueEditor::ModelSignals> m_notifier;
305306
};
306307

src/app/models/key-models/rowcache.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#pragma once
2+
#include <QPair>
3+
#include <QList>
4+
#include <QHash>
5+
6+
7+
template < typename T > class MappedCache
8+
{
9+
public:
10+
typedef unsigned long int RowIndex;
11+
typedef QPair<RowIndex, RowIndex> CacheRange;
12+
13+
public:
14+
15+
MappedCache()
16+
: m_size(0)
17+
{
18+
19+
}
20+
21+
void addLoadedRange(const CacheRange& range, const QList<T>& dataForRange)
22+
{
23+
m_mapping[range] = dataForRange;
24+
m_size += dataForRange.size();
25+
}
26+
27+
bool isRowLoaded(RowIndex index)
28+
{
29+
CacheRange i = findTargetRange(index);
30+
return i != CacheRange({-1, -1});
31+
}
32+
33+
T getRow(RowIndex index)
34+
{
35+
if (!isRowLoaded(index))
36+
return T();
37+
38+
CacheRange i = findTargetRange(index);
39+
40+
RowIndex mappedIndex = i.first - index;
41+
42+
return m_mapping.value(i).at(mappedIndex);
43+
}
44+
45+
T operator [](RowIndex index) {
46+
return getRow(index);
47+
}
48+
49+
void replace(RowIndex i, T row) {
50+
if (!isRowLoaded(i)) {
51+
return std::
52+
}
53+
54+
55+
56+
}
57+
58+
unsigned long long size() const
59+
{
60+
return m_size;
61+
}
62+
63+
void clear()
64+
{
65+
m_mapping.clear();
66+
m_size = 0;
67+
}
68+
69+
private:
70+
CacheRange findTargetRange(RowIndex index)
71+
{
72+
for (auto i = m_mapping.constBegin();
73+
i != m_mapping.constEnd(); ++i)
74+
{
75+
if (i.key().first <= index && index <= i.key().second) {
76+
return i.key();
77+
}
78+
}
79+
return {-1, -1};
80+
}
81+
82+
private:
83+
QHash<CacheRange, QList<T>> m_mapping;
84+
unsigned long long m_size;
85+
};
86+

src/modules/value-editor/valueviewmodel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ void ValueEditor::ValueViewModel::loadRows(int start, int count)
8989
QString msg = QString("Cannot load key value: %1");
9090

9191
try {
92+
// NOTE(u_glide): Do so for proper rendering of QML table
93+
m_lastLoadedRowFrameSize = totalRowCount() - start;
9294
m_model->loadRows(start, count, [this, start, count, msg](const QString& err)
9395
{
9496
if (!err.isEmpty()) {

0 commit comments

Comments
 (0)