Skip to content

Commit 3b58be1

Browse files
authored
Merge pull request RedisInsight#4101 from uglide/rejson_support
Add basic ReJSON module support
2 parents eab0213 + 190b1fb commit 3b58be1

File tree

6 files changed

+134
-3
lines changed

6 files changed

+134
-3
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "sortedsetkey.h"
88
#include "hashkey.h"
99
#include "listkey.h"
10+
#include "rejsonkey.h"
1011
#include <QObject>
1112

1213
KeyFactory::KeyFactory()
@@ -56,6 +57,9 @@ void KeyFactory::loadKey(QSharedPointer<RedisClient::Connection> connection,
5657

5758
result = createModel(type, connection, keyFullPath, dbIndex, ttl);
5859

60+
if (!result)
61+
return callback(result, QString(QObject::tr("Unsupported Redis Data type %1").arg(type)));
62+
5963
callback(result, QString());
6064

6165
}, dbIndex);
@@ -92,6 +96,8 @@ QSharedPointer<ValueEditor::Model> KeyFactory::createModel(QString type, QShared
9296
return QSharedPointer<ValueEditor::Model>(new SortedSetKeyModel(connection, keyFullPath, dbIndex, ttl));
9397
} else if (type == "hash") {
9498
return QSharedPointer<ValueEditor::Model>(new HashKeyModel(connection, keyFullPath, dbIndex, ttl));
99+
} else if (type == "ReJSON-RL") {
100+
return QSharedPointer<ValueEditor::Model>(new ReJSONKeyModel(connection, keyFullPath, dbIndex, ttl));
95101
}
96102

97103
return QSharedPointer<ValueEditor::Model>();
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "rejsonkey.h"
2+
#include <qredisclient/connection.h>
3+
4+
ReJSONKeyModel::ReJSONKeyModel(QSharedPointer<RedisClient::Connection> connection,
5+
QByteArray fullPath, int dbIndex, long long ttl)
6+
: KeyModel(connection, fullPath, dbIndex, ttl, false,
7+
QByteArray(), QByteArray(), QByteArray())
8+
{
9+
}
10+
11+
QString ReJSONKeyModel::getType()
12+
{
13+
return "ReJSON";
14+
}
15+
16+
QStringList ReJSONKeyModel::getColumnNames()
17+
{
18+
return QStringList(); // Single value type - No columns
19+
}
20+
21+
QHash<int, QByteArray> ReJSONKeyModel::getRoles()
22+
{
23+
QHash<int, QByteArray> roles;
24+
roles[Roles::Value] = "value";
25+
return roles;
26+
}
27+
28+
QVariant ReJSONKeyModel::getData(int rowIndex, int dataRole)
29+
{
30+
if (!isRowLoaded(rowIndex))
31+
return QVariant();
32+
33+
if (dataRole == Roles::Value)
34+
return m_rowsCache[rowIndex];
35+
36+
return QVariant();
37+
}
38+
39+
void ReJSONKeyModel::updateRow(int rowIndex, const QVariantMap &row)
40+
{
41+
if (rowIndex > 0 || !isRowValid(row)) {
42+
qDebug() << "Row is not valid";
43+
return;
44+
}
45+
46+
QByteArray value = row.value("value").toByteArray();
47+
48+
if (value.isEmpty())
49+
return;
50+
51+
RedisClient::Response result;
52+
try {
53+
result = m_connection->commandSync({"JSON.SET", m_keyFullPath, ".", value}, m_dbIndex);
54+
} catch (const RedisClient::Connection::Exception& e) {
55+
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
56+
}
57+
58+
if (result.isOkMessage()) {
59+
m_rowsCache.clear();
60+
m_rowsCache.addLoadedRange({0, 0}, (QList<QByteArray>() << value));
61+
m_notifier->dataLoaded();
62+
}
63+
}
64+
65+
void ReJSONKeyModel::addRow(const QVariantMap &row)
66+
{
67+
updateRow(0, row);
68+
}
69+
70+
void ReJSONKeyModel::loadRows(unsigned long, unsigned long, std::function<void(const QString&)> callback)
71+
{
72+
try {
73+
m_connection->command({"JSON.GET", m_keyFullPath},
74+
getConnector().data(),
75+
[this, callback](RedisClient::Response r, QString e)
76+
{
77+
if (r.getType() != RedisClient::Response::Bulk || !e.isEmpty()) {
78+
return callback(QString("Cannot load value"));
79+
}
80+
81+
m_rowsCache.clear();
82+
m_rowsCache.push_back(r.getValue().toByteArray());
83+
m_notifier->dataLoaded();
84+
85+
callback(QString());
86+
}, m_dbIndex);
87+
} catch (const RedisClient::Connection::Exception& e) {
88+
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
89+
}
90+
}
91+
92+
void ReJSONKeyModel::removeRow(int)
93+
{
94+
m_rowCount--;
95+
setRemovedIfEmpty();
96+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include "abstractkey.h"
3+
4+
class ReJSONKeyModel : public KeyModel<QByteArray>
5+
{
6+
public:
7+
ReJSONKeyModel(QSharedPointer<RedisClient::Connection> connection,
8+
QByteArray fullPath, int dbIndex, long long ttl);
9+
10+
QString getType() override;
11+
QStringList getColumnNames() override;
12+
QHash<int, QByteArray> getRoles() override;
13+
QVariant getData(int rowIndex, int dataRole) override;
14+
15+
void addRow(const QVariantMap&) override;
16+
virtual void updateRow(int rowIndex, const QVariantMap& row) override;
17+
void loadRows(unsigned long, unsigned long, std::function<void(const QString&)> callback) override;
18+
void removeRow(int) override;
19+
20+
protected:
21+
void addLoadedRowsToCache(const QVariantList&, int) override {}
22+
23+
private:
24+
enum Roles { Value = Qt::UserRole + 1 };
25+
};

src/qml/value-editor/ValueTabs.qml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Repeater {
296296
elide: styleData.elideMode
297297
text: {
298298

299-
if (styleData.value === "" || keyType === "string") {
299+
if (styleData.value === "" || keyType === "string" || keyType === "ReJSON") {
300300
return ""
301301
}
302302

@@ -337,7 +337,7 @@ Repeater {
337337

338338
console.log(keyType)
339339

340-
if (keyType === "string") {
340+
if (keyType === "string" || keyType === "ReJSON") {
341341
valueEditor.loadRowValue(0)
342342
} else {
343343
var columns = columnNames

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ function getEditorByTypeString(keyType) {
1212
return "./editors/SortedSetItemEditor.qml"
1313
} else if (keyType === "hash") {
1414
return "./editors/HashItemEditor.qml"
15+
} else if (keyType === "ReJSON") {
16+
return "./editors/SingleItemEditor.qml"
1517
} else {
1618
console.error("Editor for type " + keyType + " is not defined!")
1719
}

tests/unit_tests/testcases/app/app-tests.pri

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ HEADERS += \
1717
$$APP_SRC_DIR/models/key-models/setkey.h \
1818
$$APP_SRC_DIR/models/key-models/sortedsetkey.h \
1919
$$APP_SRC_DIR/models/key-models/hashkey.h \
20+
$$APP_SRC_DIR/models/key-models/rejsonkey.h \
2021
2122
SOURCES += \
2223
$$PWD/test_*.cpp \
@@ -30,7 +31,8 @@ SOURCES += \
3031
$$APP_SRC_DIR/models/key-models/listlikekey.cpp \
3132
$$APP_SRC_DIR/models/key-models/setkey.cpp \
3233
$$APP_SRC_DIR/models/key-models/sortedsetkey.cpp \
33-
$$APP_SRC_DIR/models/key-models/hashkey.cpp \
34+
$$APP_SRC_DIR/models/key-models/hashkey.cpp \
35+
$$APP_SRC_DIR/models/key-models/rejsonkey.cpp \
3436
3537
OTHER_FILES += \
3638
connections.xml

0 commit comments

Comments
 (0)