~imalinovskiy/rdm/0.8

« back to all changes in this revision

Viewing changes to src/app/models/key-models/keyfactory.cpp

  • Committer: RDM Bot
  • Date: 2016-02-04 11:29:17 UTC
  • Revision ID: git-v1:92436de8ed24fe0a57651f4a61caba27995ad55b
Pull updates from https://github.com/uglide/RedisDesktopManager

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "keyfactory.h"
 
2
#include <qredisclient/redisclient.h>
 
3
#include <qredisclient/utils/text.h>
 
4
 
 
5
#include "stringkey.h"
 
6
#include "setkey.h"
 
7
#include "sortedsetkey.h"
 
8
#include "hashkey.h"
 
9
#include "listkey.h"
 
10
 
 
11
KeyFactory::KeyFactory()
 
12
{}
 
13
 
 
14
void KeyFactory::loadKey(QSharedPointer<RedisClient::Connection> connection,
 
15
                         QByteArray keyFullPath, int dbIndex,
 
16
                         std::function<void (QSharedPointer<ValueEditor::Model>, const QString &)> callback)
 
17
{
 
18
    RedisClient::Command typeCmd({"type", keyFullPath}, this,
 
19
                                 [this, connection, keyFullPath, dbIndex, callback]
 
20
                                 (RedisClient::Response resp, QString)
 
21
    {
 
22
 
 
23
        QSharedPointer<ValueEditor::Model> result;
 
24
 
 
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()));
 
28
            return;
 
29
        }
 
30
 
 
31
        QString type = resp.getValue().toString();        
 
32
 
 
33
        if (type == "none") {
 
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)));
 
37
            return;
 
38
        }
 
39
 
 
40
        RedisClient::Response ttlResult;
 
41
 
 
42
        try {
 
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())));
 
47
            return;
 
48
        }
 
49
 
 
50
        long long ttl = -1;
 
51
 
 
52
        if (ttlResult.getType() == RedisClient::Response::Integer) {
 
53
            ttl = ttlResult.getValue().toLongLong();
 
54
        }
 
55
 
 
56
        result = createModel(type, connection, keyFullPath, dbIndex, ttl);
 
57
 
 
58
        callback(result, QString());
 
59
 
 
60
    }, dbIndex);
 
61
 
 
62
    try {
 
63
        connection->runCommand(typeCmd);
 
64
    } catch (const RedisClient::Connection::Exception& e) {
 
65
        throw Exception("Cannot retrive type of the key: " + QString(e.what()));
 
66
    }
 
67
}
 
68
 
 
69
void KeyFactory::addKey(QSharedPointer<RedisClient::Connection> connection,
 
70
                        QByteArray keyFullPath, int dbIndex,
 
71
                        QString type, const QVariantMap &row)
 
72
{
 
73
    QSharedPointer<ValueEditor::Model> result = createModel(type, connection, keyFullPath, dbIndex, -1);
 
74
 
 
75
    if (!result)
 
76
        return;
 
77
 
 
78
    result->addRow(row);
 
79
}
 
80
 
 
81
QSharedPointer<ValueEditor::Model> KeyFactory::createModel(QString type, QSharedPointer<RedisClient::Connection> connection,
 
82
                                                           QByteArray keyFullPath, int dbIndex, long long ttl)
 
83
{
 
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));
 
94
    }
 
95
 
 
96
    return QSharedPointer<ValueEditor::Model>();
 
97
}
 
98