Skip to content

Commit 855636d

Browse files
committed
Improve keys loading
1 parent 07b392d commit 855636d

File tree

6 files changed

+60
-32
lines changed

6 files changed

+60
-32
lines changed

src/app/models/key-models/keyfactory.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <qredisclient/redisclient.h>
44
#include <qredisclient/utils/text.h>
55

6-
#include <QObject>
76
#include <QFile>
7+
#include <QObject>
88

99
#include "hashkey.h"
1010
#include "listkey.h"
@@ -21,8 +21,15 @@ void KeyFactory::loadKey(
2121
int dbIndex,
2222
std::function<void(QSharedPointer<ValueEditor::Model>, const QString&)>
2323
callback) {
24-
auto loadModel = [this, connection, keyFullPath, dbIndex, callback](
25-
RedisClient::Response resp, QString) {
24+
auto processError = [callback, keyFullPath](const QString& err) {
25+
QString msg(QCoreApplication::translate(
26+
"RDM", "Cannot load key %1, connection error occurred: %2"));
27+
callback(QSharedPointer<ValueEditor::Model>(),
28+
msg.arg(printableString(keyFullPath)).arg(err));
29+
};
30+
31+
auto loadModel = [this, connection, keyFullPath, dbIndex, callback,
32+
processError](RedisClient::Response resp) {
2633
QSharedPointer<ValueEditor::Model> result;
2734

2835
if (resp.isErrorMessage() ||
@@ -46,8 +53,8 @@ void KeyFactory::loadKey(
4653
return;
4754
}
4855

49-
auto parseTtl = [this, type, connection, keyFullPath, dbIndex,
50-
callback](const RedisClient::Response& ttlResult) {
56+
auto parseTtl = [this, type, connection, keyFullPath, dbIndex, callback,
57+
processError](const RedisClient::Response& ttlResult) {
5158
long long ttl = -1;
5259

5360
if (ttlResult.type() == RedisClient::Response::Integer) {
@@ -64,27 +71,17 @@ void KeyFactory::loadKey(
6471
callback(result, QString());
6572
};
6673

67-
auto processTtlError = [callback, result, keyFullPath](const QString& err) {
68-
QString msg(QCoreApplication::translate(
69-
"RDM", "Cannot load TTL for key %1, connection error occurred: %2"));
70-
callback(result, msg.arg(printableString(keyFullPath)).arg(err));
71-
};
72-
73-
connection->cmd({"ttl", keyFullPath}, this, -1, parseTtl, processTtlError);
74+
connection->cmd({"ttl", keyFullPath}, this, -1, parseTtl, processError);
7475
};
7576

76-
RedisClient::Command typeCmd({"type", keyFullPath}, this, loadModel, dbIndex);
77-
7877
try {
79-
RedisClient::Response typeResult = connection->runCommand(typeCmd);
80-
if (typeResult.isPermissionError()) {
81-
emit error(typeResult.value().toString());
82-
}
78+
connection->cmd({"type", keyFullPath}, this, dbIndex, loadModel,
79+
processError);
8380
} catch (const RedisClient::Connection::Exception& e) {
84-
callback(
85-
QSharedPointer<ValueEditor::Model>(),
86-
QCoreApplication::translate("RDM", "Cannot retrieve type of the key: ") +
87-
QString(e.what()));
81+
callback(QSharedPointer<ValueEditor::Model>(),
82+
QCoreApplication::translate("RDM",
83+
"Cannot retrieve type of the key: ") +
84+
QString(e.what()));
8885
}
8986
}
9087

@@ -122,14 +119,14 @@ void KeyFactory::submitNewKeyRequest(NewKeyRequest r) {
122119
auto val = r.value();
123120

124121
if (!r.valueFilePath().isEmpty() && QFile::exists(r.valueFilePath())) {
125-
QFile valueFile(r.valueFilePath());
122+
QFile valueFile(r.valueFilePath());
126123

127-
if (!valueFile.open(QIODevice::ReadOnly)) {
128-
return onRowAdded(QCoreApplication::translate(
129-
"RDM", "Cannot open file with key value"));
130-
}
124+
if (!valueFile.open(QIODevice::ReadOnly)) {
125+
return onRowAdded(QCoreApplication::translate(
126+
"RDM", "Cannot open file with key value"));
127+
}
131128

132-
val["value"] = valueFile.readAll();
129+
val["value"] = valueFile.readAll();
133130
}
134131

135132
result->addRow(val, onRowAdded);

src/modules/value-editor/tabsmodel.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ void ValueEditor::TabsModel::openTab(
2828
.arg(key->getDbIndex()),
2929
key.toWeakRef());
3030

31-
QSharedPointer<RedisClient::Connection> conn;
31+
QSharedPointer<RedisClient::Connection> conn;
3232

33-
if (inNewTab || m_viewModels.count() == 0) {
33+
if (inNewTab || m_viewModels.count() == 0) {
3434
beginInsertRows(QModelIndex(), m_viewModels.count(), m_viewModels.count());
3535
m_viewModels.append(viewModel);
3636
endInsertRows();
@@ -89,6 +89,20 @@ void ValueEditor::TabsModel::openTab(
8989
m_events->registerLoggerForConnection(*conn);
9090
}
9191

92+
viewModel->setConnection(conn);
93+
94+
connect(conn.data(), &RedisClient::Connection::shutdownStart,
95+
this, [this, viewModel](){
96+
if (!viewModel) return;
97+
viewModel->setTabError(QCoreApplication::translate("RDM", "Connection error"));
98+
99+
int modelIndex = m_viewModels.indexOf(viewModel);
100+
101+
if (modelIndex != -1) {
102+
emit dataChanged(index(modelIndex, 0), index(modelIndex, 0));
103+
}
104+
});
105+
92106
try {
93107
QtConcurrent::run([this, conn, key, viewModelWeekRef, callbackWrapper]() {
94108
if (!conn->isConnected())

src/modules/value-editor/valueviewmodel.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
ValueEditor::ValueViewModel::ValueViewModel(const QString& loadingTitle)
99
: BaseListModel(),
10+
m_model(nullptr),
11+
m_connection(nullptr),
1012
m_startFramePosition(0),
1113
m_lastLoadedRowFrameSize(0),
1214
m_singlePageMode(false),
@@ -37,6 +39,11 @@ QString ValueEditor::ValueViewModel::tabLoadingTitle() const {
3739
return m_tabTitle;
3840
}
3941

42+
void ValueEditor::ValueViewModel::setTabError(const QString &t)
43+
{
44+
m_tabTitle = t;
45+
}
46+
4047
bool ValueEditor::ValueViewModel::isModelLoaded() const {
4148
return !m_model.isNull();
4249
}
@@ -69,6 +76,11 @@ void ValueEditor::ValueViewModel::setModel(QSharedPointer<Model> model) {
6976
emit modelLoaded();
7077
}
7178

79+
void ValueEditor::ValueViewModel::setConnection(QSharedPointer<RedisClient::Connection> c)
80+
{
81+
m_connection = c;
82+
}
83+
7284
void ValueEditor::ValueViewModel::renameKey(const QString& newKeyName) {
7385
if (!m_model) {
7486
qWarning() << "Model is not loaded";

src/modules/value-editor/valueviewmodel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ class ValueViewModel : public BaseListModel {
3232
QSharedPointer<Model> model();
3333
void setModel(QSharedPointer<Model> model);
3434

35+
void setConnection(QSharedPointer<RedisClient::Connection> c);
36+
3537
QString tabLoadingTitle() const;
3638

39+
void setTabError(const QString& t);
40+
3741
void close();
3842

3943
public:
@@ -84,6 +88,7 @@ class ValueViewModel : public BaseListModel {
8488

8589
private:
8690
QSharedPointer<Model> m_model;
91+
QSharedPointer<RedisClient::Connection> m_connection;
8792
int m_startFramePosition;
8893
int m_lastLoadedRowFrameSize;
8994
bool m_singlePageMode;

src/qml/app.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ ApplicationWindow {
462462
target: valuesModel
463463
ignoreUnknownSignals: true
464464

465-
function onKeyError(error) {
465+
function onTabError(index, error) {
466466
if (index != -1)
467467
tabs.currentIndex = index
468468

0 commit comments

Comments
 (0)