Skip to content

Commit 2270188

Browse files
committed
Add HyperLogLog support
Close RedisInsight#3656
1 parent 37ee8fb commit 2270188

File tree

12 files changed

+414
-323
lines changed

12 files changed

+414
-323
lines changed

src/app/models/key-models/stringkey.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
StringKeyModel::StringKeyModel(
55
QSharedPointer<RedisClient::Connection> connection, QByteArray fullPath,
66
int dbIndex, long long ttl)
7-
: KeyModel(connection, fullPath, dbIndex, ttl) {}
7+
: KeyModel(connection, fullPath, dbIndex, ttl), m_type("string") {}
88

9-
QString StringKeyModel::type() { return "string"; }
9+
QString StringKeyModel::type() { return m_type; }
1010

1111
QStringList StringKeyModel::getColumnNames() {
1212
return QStringList(); // Single value type - No columns
@@ -19,7 +19,7 @@ QHash<int, QByteArray> StringKeyModel::getRoles() {
1919
}
2020

2121
QVariant StringKeyModel::getData(int rowIndex, int dataRole) {
22-
if (!isRowLoaded(rowIndex)) return QVariant();
22+
if (rowIndex > 0 || !isRowLoaded(rowIndex)) return QVariant();
2323
if (dataRole == Roles::Value) return m_rowsCache[rowIndex];
2424

2525
return QVariant();
@@ -48,7 +48,17 @@ void StringKeyModel::updateRow(int rowIndex, const QVariantMap& row,
4848
}
4949

5050
void StringKeyModel::addRow(const QVariantMap& row, Callback c) {
51-
updateRow(0, row, c);
51+
if (m_type == "hyperloglog") {
52+
QByteArray value = row.value("value").toByteArray();
53+
54+
executeCmd(
55+
{"PFADD", m_keyFullPath, value}, [this, c](const QString& err) {
56+
m_rowCount++;
57+
return c(err);
58+
});
59+
} else {
60+
updateRow(0, row, c);
61+
}
5262
}
5363

5464
void StringKeyModel::loadRows(QVariant, unsigned long,
@@ -59,9 +69,25 @@ void StringKeyModel::loadRows(QVariant, unsigned long,
5969

6070
auto responseHandler = [this, callback](RedisClient::Response r, Callback) {
6171
m_rowsCache.clear();
62-
m_rowsCache.push_back(r.value().toByteArray());
6372

64-
callback(QString(), 1);
73+
QByteArray value = r.value().toByteArray();
74+
75+
m_rowsCache.push_back(value);
76+
m_rowCount = 1;
77+
78+
// Detect HyperLogLog
79+
if (value.startsWith("HYLL")) {
80+
executeCmd(
81+
{"PFCOUNT", m_keyFullPath}, [callback](const QString&) { callback(QString(), 1); },
82+
[this, callback](RedisClient::Response r, Callback) {
83+
m_type = "hyperloglog";
84+
m_rowCount = r.value().toUInt();
85+
callback(QString(), m_rowCount);
86+
},
87+
RedisClient::Response::Integer);
88+
} else {
89+
callback(QString(), 1);
90+
}
6591
};
6692

6793
executeCmd({"GET", m_keyFullPath}, onConnectionError, responseHandler,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ class StringKeyModel : public KeyModel<QByteArray> {
1717
void loadRows(QVariant, unsigned long, LoadRowsCallback callback) override;
1818
void removeRow(int, Callback c) override;
1919

20+
virtual unsigned long rowsCount() override {
21+
return m_rowCount;
22+
}
23+
2024
protected:
2125
int addLoadedRowsToCache(const QVariantList&, QVariant) override { return 1; }
2226

2327
private:
2428
enum Roles { Value = Qt::UserRole + 1 };
29+
30+
QString m_type;
2531
};

src/modules/value-editor/tabsmodel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ QVariant ValueEditor::TabsModel::data(const QModelIndex& index,
8888
return model->getTTL();
8989
case keyType:
9090
return model->type();
91+
case rowsCount:
92+
return (qlonglong)model->rowsCount();
9193
case isMultiRow:
9294
return model->isMultiRow();
9395
case keyModel:
@@ -109,6 +111,7 @@ QHash<int, QByteArray> ValueEditor::TabsModel::roleNames() const {
109111
roles[keyTTL] = "keyTtl";
110112
roles[keyType] = "keyType";
111113
roles[isMultiRow] = "isMultiRow";
114+
roles[rowsCount] = "keyRowsCount";
112115
roles[keyModel] = "keyViewModel";
113116
return roles;
114117
}
@@ -158,6 +161,11 @@ void ValueEditor::TabsModel::loadModel(
158161
auto viewModel = QSharedPointer<ValueViewModel>(new ValueViewModel(model),
159162
&QObject::deleteLater);
160163

164+
connect(viewModel.data(), &ValueViewModel::rowsLoaded, this,
165+
[this, viewModel] (int, int) {
166+
qDebug() << "row loaded (tab model)"; tabChanged(viewModel);
167+
});
168+
161169
if (openNewTab || m_viewModels.count() == 0) {
162170
beginInsertRows(QModelIndex(), m_viewModels.count(), m_viewModels.count());
163171
m_viewModels.append(viewModel);

src/modules/value-editor/tabsmodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TabsModel : public QAbstractListModel {
2525
keyTTL,
2626
keyType,
2727
isMultiRow,
28+
rowsCount,
2829
keyModel,
2930
};
3031

src/qml/value-editor/ValueTabs.qml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import QtQuick.Controls.Styles 1.1
55
import QtQuick.Dialogs 1.2
66
import QtQuick.Window 2.2
77
import "./editors/editor.js" as Editor
8+
import "./../common/platformutils.js" as PlatformUtils
89
import "./../common"
910
import rdm.models 1.0
1011

@@ -165,8 +166,8 @@ Repeater {
165166

166167
Item { visible: isMultiRow; Layout.preferredWidth: 5}
167168
Text {
168-
visible: isMultiRow;
169-
text: qsTranslate("RDM","Size: ") + (keyTab.keyModel? keyTab.keyModel.totalRowCount : "0")
169+
visible: isMultiRow || keyType === "hyperloglog";
170+
text: qsTranslate("RDM","Size: ") + keyRowsCount
170171
}
171172
Item { Layout.preferredWidth: 5}
172173

@@ -432,15 +433,16 @@ Repeater {
432433

433434
Button {
434435
Layout.preferredWidth: 195
435-
text: qsTranslate("RDM","Add Row");
436+
text: qsTranslate("RDM","Add Row")
436437
iconSource: "qrc:/images/add.svg"
437438
onClicked: {
438439
addRowDialog.open()
439440
}
440441

441442
Dialog {
442443
id: addRowDialog
443-
title: qsTranslate("RDM","Add Row")
444+
title: keyType === "hyperloglog"? qsTranslate("RDM","Add Element to HLL")
445+
: qsTranslate("RDM","Add Row")
444446

445447
width: 550
446448
height: 400
@@ -494,9 +496,10 @@ Repeater {
494496
var row = valueAddEditor.item.getValue()
495497

496498
keyTab.keyModel.addRow(row)
497-
keyTab.keyModel.reload()
499+
keyTab.keyModel.reload()
498500
valueAddEditor.item.reset()
499501
valueAddEditor.item.initEmpty()
502+
addRowDialog.close()
500503
});
501504
}
502505
}
@@ -649,6 +652,17 @@ Repeater {
649652
Layout.fillWidth: true
650653
Layout.minimumHeight: 40
651654
Item { Layout.fillWidth: true}
655+
656+
Button {
657+
visible: keyType === "hyperloglog"
658+
Layout.preferredWidth: 195
659+
text: qsTranslate("RDM","Add Element to HLL");
660+
iconSource: "qrc:/images/add.svg"
661+
onClicked: {
662+
addRowDialog.open()
663+
}
664+
}
665+
652666
Button {
653667
text: qsTranslate("RDM","Save")
654668

src/qml/value-editor/editors/editor.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ function getSupportedKeyTypes() {
44
}
55

66
function getEditorByTypeString(keyType) {
7-
if (keyType === "string") {
8-
return "./editors/SingleItemEditor.qml"
9-
} else if (keyType === "list" || keyType === "set") {
10-
return "./editors/SingleItemEditor.qml"
7+
if (keyType === "string"
8+
|| keyType === "hyperloglog"
9+
|| keyType === "list"
10+
|| keyType === "set"
11+
|| keyType === "ReJSON") {
12+
return "./editors/SingleItemEditor.qml"
1113
} else if (keyType === "zset") {
1214
return "./editors/SortedSetItemEditor.qml"
1315
} else if (keyType === "hash") {
1416
return "./editors/HashItemEditor.qml"
15-
} else if (keyType === "ReJSON") {
16-
return "./editors/SingleItemEditor.qml"
1717
} else if (keyType === "stream") {
1818
return "./editors/StreamItemEditor.qml"
1919
} else {

0 commit comments

Comments
 (0)