1
#include "keyfactory.h"
2
#include <qredisclient/redisclient.h>
3
#include <qredisclient/utils/text.h>
7
#include "sortedsetkey.h"
11
KeyFactory::KeyFactory()
14
void KeyFactory::loadKey(QSharedPointer<RedisClient::Connection> connection,
15
QByteArray keyFullPath, int dbIndex,
16
std::function<void (QSharedPointer<ValueEditor::Model>, const QString &)> callback)
18
RedisClient::Command typeCmd({"type", keyFullPath}, this,
19
[this, connection, keyFullPath, dbIndex, callback]
20
(RedisClient::Response resp, QString)
23
QSharedPointer<ValueEditor::Model> result;
25
if (resp.isErrorMessage() || resp.getType() != RedisClient::Response::Type::Status) {
26
QString msg("Cannot load key %1, connection error occurred: %2");
27
callback(result, msg.arg(printableString(keyFullPath)).arg(resp.toRawString()));
31
QString type = resp.getValue().toString();
34
QString msg("Cannot load key %1 because it doesn't exist in database."
35
" Please reload connection tree and try again.");
36
callback(result, msg.arg(printableString(keyFullPath)));
40
RedisClient::Response ttlResult;
43
ttlResult = connection->commandSync({"ttl", keyFullPath}, dbIndex);
44
} catch (const RedisClient::Connection::Exception& e) {
45
QString msg("Cannot load TTL for key %1, connection error occurred: %2");
46
callback(result, msg.arg(printableString(keyFullPath)).arg(QString(e.what())));
52
if (ttlResult.getType() == RedisClient::Response::Integer) {
53
ttl = ttlResult.getValue().toLongLong();
56
result = createModel(type, connection, keyFullPath, dbIndex, ttl);
58
callback(result, QString());
63
connection->runCommand(typeCmd);
64
} catch (const RedisClient::Connection::Exception& e) {
65
throw Exception("Cannot retrive type of the key: " + QString(e.what()));
69
void KeyFactory::addKey(QSharedPointer<RedisClient::Connection> connection,
70
QByteArray keyFullPath, int dbIndex,
71
QString type, const QVariantMap &row)
73
QSharedPointer<ValueEditor::Model> result = createModel(type, connection, keyFullPath, dbIndex, -1);
81
QSharedPointer<ValueEditor::Model> KeyFactory::createModel(QString type, QSharedPointer<RedisClient::Connection> connection,
82
QByteArray keyFullPath, int dbIndex, long long ttl)
84
if (type == "string") {
85
return QSharedPointer<ValueEditor::Model>(new StringKeyModel(connection, keyFullPath, dbIndex, ttl));
86
} else if (type == "list") {
87
return QSharedPointer<ValueEditor::Model>(new ListKeyModel(connection, keyFullPath, dbIndex, ttl));
88
} else if (type == "set") {
89
return QSharedPointer<ValueEditor::Model>(new SetKeyModel(connection, keyFullPath, dbIndex, ttl));
90
} else if (type == "zset") {
91
return QSharedPointer<ValueEditor::Model>(new SortedSetKeyModel(connection, keyFullPath, dbIndex, ttl));
92
} else if (type == "hash") {
93
return QSharedPointer<ValueEditor::Model>(new HashKeyModel(connection, keyFullPath, dbIndex, ttl));
96
return QSharedPointer<ValueEditor::Model>();