-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4101 from uglide/rejson_support
Add basic ReJSON module support
- Loading branch information
Showing
6 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters