Skip to content

Commit 45bfa04

Browse files
committed
Improve import from rdb files
1 parent 39b7a47 commit 45bfa04

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed

src/app/models/treeoperations.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ void TreeOperations::requestBulkOperation(
114114
.arg(ns.getFullPath().size() > 0 ? conf().namespaceSeparator() : "");
115115
QRegExp filter(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
116116

117-
emit m_events->requestBulkOperation(m_connection, ns.getDbIndex(), op, filter,
117+
// NOTE(u_glide): Use "clean" connection wihout logger here for better performance
118+
emit m_events->requestBulkOperation(m_connection->clone(), ns.getDbIndex(), op, filter,
118119
callback);
119120
}
120121

@@ -271,7 +272,7 @@ void TreeOperations::copyKeys(ConnectionsTree::AbstractNamespaceItem& ns) {
271272

272273
void TreeOperations::importKeysFromRdb(ConnectionsTree::DatabaseItem& db) {
273274
emit m_events->requestBulkOperation(
274-
m_connection, db.getDbIndex(),
275+
m_connection->clone(), db.getDbIndex(),
275276
BulkOperations::Manager::Operation::IMPORT_RDB_KEYS, QRegExp(".*"),
276277
[&db](QRegExp, int, const QStringList&) { db.reload(); });
277278
}

src/modules/bulk-operations/operations/abstractoperation.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ BulkOperations::AbstractOperation::AbstractOperation(
88
m_keyPattern(keyPattern),
99
m_currentState(State::READY),
1010
m_progress(0),
11-
m_callback(callback) {}
11+
m_callback(callback),
12+
m_lastProgressNotification(0) {}
1213

1314
void BulkOperations::AbstractOperation::getAffectedKeys(
1415
std::function<void(QVariant, QString)> callback) {
@@ -82,5 +83,17 @@ int BulkOperations::AbstractOperation::currentProgress() const {
8283
}
8384

8485
void BulkOperations::AbstractOperation::setMetadata(const QVariantMap& meta) {
85-
m_metadata = meta;
86+
m_metadata = meta;
87+
}
88+
89+
void BulkOperations::AbstractOperation::incrementProgress()
90+
{
91+
QMutexLocker l(&m_processedKeysMutex);
92+
m_progress++;
93+
94+
if (QDateTime::currentMSecsSinceEpoch() - m_lastProgressNotification >= 1000) {
95+
qDebug() << "Notify UI about progress";
96+
emit progress(m_progress);
97+
m_lastProgressNotification = QDateTime::currentMSecsSinceEpoch();
98+
}
8699
}

src/modules/bulk-operations/operations/abstractoperation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class AbstractOperation : public QObject {
5959
QSharedPointer<RedisClient::Connection> targetConnection,
6060
int targetDbIndex) = 0;
6161

62+
void incrementProgress();
63+
6264
protected:
6365
QSharedPointer<RedisClient::Connection> m_connection;
6466
int m_dbIndex;
@@ -72,5 +74,6 @@ class AbstractOperation : public QObject {
7274
QStringList m_errors;
7375
QMutex m_errorsMutex;
7476
QMutex m_processedKeysMutex;
77+
qint64 m_lastProgressNotification;
7578
};
7679
} // namespace BulkOperations

src/modules/bulk-operations/operations/rdbimport.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "rdbimport.h"
22
#include <qpython.h>
33
#include <QFileInfo>
4+
#include <QtConcurrent>
45

56
BulkOperations::RDBImportOperation::RDBImportOperation(
67
QSharedPointer<RedisClient::Connection> connection, int dbIndex,
@@ -63,36 +64,42 @@ void BulkOperations::RDBImportOperation::performOperation(
6364
return returnResults();
6465
}
6566

67+
auto processCommands = [this, returnResults](const QVariantList& commands) {
68+
for (QVariant cmd : commands) {
69+
auto rawCmd = convertToByteArray(cmd);
70+
71+
if (rawCmd.at(0).toLower() == QByteArray("select")) {
72+
continue;
73+
}
74+
75+
auto future = m_connection->cmd(
76+
rawCmd, this, -1,
77+
[this](const RedisClient::Response&) { incrementProgress(); },
78+
[this, rawCmd](const QString& err) {
79+
QMutexLocker l(&m_errorsMutex);
80+
m_errors.append(
81+
QCoreApplication::translate("RDM", "Cannot execute command ") +
82+
QString("%1: %2")
83+
.arg(QString::fromUtf8(rawCmd.join(QByteArray(" "))))
84+
.arg(err));
85+
});
86+
87+
m_combinator->combine(future);
88+
}
89+
m_combinator->subscribe(returnResults, returnResults);
90+
};
91+
6692
m_python->call_native(
6793
"rdb.rdb_export_as_commands",
6894
QVariantList{m_metadata["path"].toString(), m_metadata["db"].toInt(),
6995
m_keyPattern.pattern()},
70-
[returnResults, this](QVariant v) {
96+
[processCommands, this](QVariant v) {
7197
QVariantList commands = v.toList();
7298

73-
for (QVariant cmd : commands) {
74-
auto rawCmd = convertToByteArray(cmd);
75-
76-
auto future = m_connection->cmd(
77-
rawCmd, this, m_dbIndex,
78-
[this](const RedisClient::Response&) {
79-
QMutexLocker l(&m_processedKeysMutex);
80-
m_progress++;
81-
emit progress(m_progress);
82-
},
83-
[this, rawCmd](const QString& err) {
84-
QMutexLocker l(&m_errorsMutex);
85-
m_errors.append(
86-
QCoreApplication::translate("RDM",
87-
"Cannot execute command ") +
88-
QString("%1: %2")
89-
.arg(QString::fromUtf8(rawCmd.join(QByteArray(" "))))
90-
.arg(err));
91-
});
92-
93-
m_combinator->combine(future);
94-
}
95-
96-
m_combinator->subscribe(returnResults, returnResults);
99+
m_connection->cmd(
100+
{"select", QByteArray::number(m_dbIndex)}, this, -1,
101+
[processCommands, commands](const RedisClient::Response&) {
102+
QtConcurrent::run(processCommands, commands);
103+
}, [](const QString&) {});
97104
});
98105
}

0 commit comments

Comments
 (0)