|
| 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 | +} |
0 commit comments