Skip to content

Commit

Permalink
Merge pull request #4101 from uglide/rejson_support
Browse files Browse the repository at this point in the history
Add basic ReJSON module support
  • Loading branch information
uglide authored Apr 26, 2018
2 parents eab0213 + 190b1fb commit 3b58be1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/app/models/key-models/keyfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "sortedsetkey.h"
#include "hashkey.h"
#include "listkey.h"
#include "rejsonkey.h"
#include <QObject>

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

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

if (!result)
return callback(result, QString(QObject::tr("Unsupported Redis Data type %1").arg(type)));

callback(result, QString());

}, dbIndex);
Expand Down Expand Up @@ -92,6 +96,8 @@ QSharedPointer<ValueEditor::Model> KeyFactory::createModel(QString type, QShared
return QSharedPointer<ValueEditor::Model>(new SortedSetKeyModel(connection, keyFullPath, dbIndex, ttl));
} else if (type == "hash") {
return QSharedPointer<ValueEditor::Model>(new HashKeyModel(connection, keyFullPath, dbIndex, ttl));
} else if (type == "ReJSON-RL") {
return QSharedPointer<ValueEditor::Model>(new ReJSONKeyModel(connection, keyFullPath, dbIndex, ttl));
}

return QSharedPointer<ValueEditor::Model>();
Expand Down
96 changes: 96 additions & 0 deletions src/app/models/key-models/rejsonkey.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "rejsonkey.h"
#include <qredisclient/connection.h>

ReJSONKeyModel::ReJSONKeyModel(QSharedPointer<RedisClient::Connection> connection,
QByteArray fullPath, int dbIndex, long long ttl)
: KeyModel(connection, fullPath, dbIndex, ttl, false,
QByteArray(), QByteArray(), QByteArray())
{
}

QString ReJSONKeyModel::getType()
{
return "ReJSON";
}

QStringList ReJSONKeyModel::getColumnNames()
{
return QStringList(); // Single value type - No columns
}

QHash<int, QByteArray> ReJSONKeyModel::getRoles()
{
QHash<int, QByteArray> roles;
roles[Roles::Value] = "value";
return roles;
}

QVariant ReJSONKeyModel::getData(int rowIndex, int dataRole)
{
if (!isRowLoaded(rowIndex))
return QVariant();

if (dataRole == Roles::Value)
return m_rowsCache[rowIndex];

return QVariant();
}

void ReJSONKeyModel::updateRow(int rowIndex, const QVariantMap &row)
{
if (rowIndex > 0 || !isRowValid(row)) {
qDebug() << "Row is not valid";
return;
}

QByteArray value = row.value("value").toByteArray();

if (value.isEmpty())
return;

RedisClient::Response result;
try {
result = m_connection->commandSync({"JSON.SET", m_keyFullPath, ".", value}, m_dbIndex);
} catch (const RedisClient::Connection::Exception& e) {
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
}

if (result.isOkMessage()) {
m_rowsCache.clear();
m_rowsCache.addLoadedRange({0, 0}, (QList<QByteArray>() << value));
m_notifier->dataLoaded();
}
}

void ReJSONKeyModel::addRow(const QVariantMap &row)
{
updateRow(0, row);
}

void ReJSONKeyModel::loadRows(unsigned long, unsigned long, std::function<void(const QString&)> callback)
{
try {
m_connection->command({"JSON.GET", m_keyFullPath},
getConnector().data(),
[this, callback](RedisClient::Response r, QString e)
{
if (r.getType() != RedisClient::Response::Bulk || !e.isEmpty()) {
return callback(QString("Cannot load value"));
}

m_rowsCache.clear();
m_rowsCache.push_back(r.getValue().toByteArray());
m_notifier->dataLoaded();

callback(QString());
}, m_dbIndex);
} catch (const RedisClient::Connection::Exception& e) {
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
}
}

void ReJSONKeyModel::removeRow(int)
{
m_rowCount--;
setRemovedIfEmpty();
}
25 changes: 25 additions & 0 deletions src/app/models/key-models/rejsonkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "abstractkey.h"

class ReJSONKeyModel : public KeyModel<QByteArray>
{
public:
ReJSONKeyModel(QSharedPointer<RedisClient::Connection> connection,
QByteArray fullPath, int dbIndex, long long ttl);

QString getType() override;
QStringList getColumnNames() override;
QHash<int, QByteArray> getRoles() override;
QVariant getData(int rowIndex, int dataRole) override;

void addRow(const QVariantMap&) override;
virtual void updateRow(int rowIndex, const QVariantMap& row) override;
void loadRows(unsigned long, unsigned long, std::function<void(const QString&)> callback) override;
void removeRow(int) override;

protected:
void addLoadedRowsToCache(const QVariantList&, int) override {}

private:
enum Roles { Value = Qt::UserRole + 1 };
};
4 changes: 2 additions & 2 deletions src/qml/value-editor/ValueTabs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Repeater {
elide: styleData.elideMode
text: {

if (styleData.value === "" || keyType === "string") {
if (styleData.value === "" || keyType === "string" || keyType === "ReJSON") {
return ""
}

Expand Down Expand Up @@ -337,7 +337,7 @@ Repeater {

console.log(keyType)

if (keyType === "string") {
if (keyType === "string" || keyType === "ReJSON") {
valueEditor.loadRowValue(0)
} else {
var columns = columnNames
Expand Down
2 changes: 2 additions & 0 deletions src/qml/value-editor/editors/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ function getEditorByTypeString(keyType) {
return "./editors/SortedSetItemEditor.qml"
} else if (keyType === "hash") {
return "./editors/HashItemEditor.qml"
} else if (keyType === "ReJSON") {
return "./editors/SingleItemEditor.qml"
} else {
console.error("Editor for type " + keyType + " is not defined!")
}
Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/testcases/app/app-tests.pri
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ HEADERS += \
$$APP_SRC_DIR/models/key-models/setkey.h \
$$APP_SRC_DIR/models/key-models/sortedsetkey.h \
$$APP_SRC_DIR/models/key-models/hashkey.h \
$$APP_SRC_DIR/models/key-models/rejsonkey.h \
SOURCES += \
$$PWD/test_*.cpp \
Expand All @@ -30,7 +31,8 @@ SOURCES += \
$$APP_SRC_DIR/models/key-models/listlikekey.cpp \
$$APP_SRC_DIR/models/key-models/setkey.cpp \
$$APP_SRC_DIR/models/key-models/sortedsetkey.cpp \
$$APP_SRC_DIR/models/key-models/hashkey.cpp \
$$APP_SRC_DIR/models/key-models/hashkey.cpp \
$$APP_SRC_DIR/models/key-models/rejsonkey.cpp \
OTHER_FILES += \
connections.xml
Expand Down

0 comments on commit 3b58be1

Please sign in to comment.