Skip to content

Commit

Permalink
Merge 8b337dd into dd11837
Browse files Browse the repository at this point in the history
  • Loading branch information
uglide committed Oct 19, 2018
2 parents dd11837 + 8b337dd commit 7714d49
Show file tree
Hide file tree
Showing 46 changed files with 3,746 additions and 3,972 deletions.
23 changes: 13 additions & 10 deletions src/app/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "modules/value-editor/valueviewmodel.h"
#include "qmlutils.h"

Application::Application(int &argc, char **argv)
Application::Application(int& argc, char** argv)
: QApplication(argc, argv),
m_engine(this),
m_qmlUtils(QSharedPointer<QmlUtils>(new QmlUtils())),
Expand All @@ -48,11 +48,14 @@ void Application::initModels() {

if (config.isNull()) {
QMessageBox::critical(
nullptr, QObject::tr("Settings directory is not writable"),
QString(QObject::tr(
nullptr,
QCoreApplication::translate("RDM",
"Settings directory is not writable"),
QCoreApplication::translate(
"RDM",
"RDM can't save connections file to settings directory. "
"Please change file permissions or restart RDM as "
"administrator.")));
"administrator."));

throw std::runtime_error("invalid connections config");
}
Expand Down Expand Up @@ -191,8 +194,8 @@ void Application::initQml() {

void Application::initUpdater() {
m_updater = QSharedPointer<Updater>(new Updater());
connect(m_updater.data(), SIGNAL(updateUrlRetrived(QString &)), this,
SLOT(OnNewUpdateAvailable(QString &)));
connect(m_updater.data(), SIGNAL(updateUrlRetrived(QString&)), this,
SLOT(OnNewUpdateAvailable(QString&)));
}

void Application::installTranslator() {
Expand All @@ -214,7 +217,7 @@ void Application::installTranslator() {
locale = preferredLocale;
}

QTranslator *translator = new QTranslator((QObject *)this);
QTranslator* translator = new QTranslator((QObject*)this);
if (translator->load(QString(":/translations/rdm_") + locale)) {
qDebug() << "Load translations file for locale:" << locale;
QCoreApplication::installTranslator(translator);
Expand Down Expand Up @@ -243,10 +246,10 @@ void Application::processCmdArgs() {
m_renderingBackend = parser.value(renderingBackend);
}

void Application::OnNewUpdateAvailable(QString &url) {
void Application::OnNewUpdateAvailable(QString& url) {
QMessageBox::information(
nullptr, "New update available",
QString(QObject::tr(
"Please download new version of Redis Desktop Manager: %1"))
QCoreApplication::translate(
"RDM", "Please download new version of Redis Desktop Manager: %1")
.arg(url));
}
8 changes: 6 additions & 2 deletions src/app/models/key-models/abstractkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <qredisclient/redisclient.h>
#include <qredisclient/utils/text.h>
#include <QByteArray>
#include <QCoreApplication>
#include <QDebug>

#include <QPair>
Expand Down Expand Up @@ -72,7 +73,8 @@ class KeyModel : public ValueEditor::Model {
}

if (result.getValue().toInt() == 0) {
throw Exception(QObject::tr(
throw Exception(QCoreApplication::translate(
"RDM",
"Key with new name already exist in database or original key was "
"removed"));
}
Expand All @@ -96,7 +98,9 @@ class KeyModel : public ValueEditor::Model {
}

if (result.getValue().toInt() == 0) {
throw Exception(QObject::tr("Not support TTL at this key"));
throw Exception(
QCoreApplication::translate("RDM", "Cannot set TTL for key %1")
.arg(getKeyName()));
}
if (ttl >= 0)
m_ttl = ttl;
Expand Down
191 changes: 90 additions & 101 deletions src/app/models/key-models/hashkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,128 @@

HashKeyModel::HashKeyModel(QSharedPointer<RedisClient::Connection> connection,
QByteArray fullPath, int dbIndex, long long ttl)
: KeyModel(connection, fullPath, dbIndex, ttl, true,
"HLEN", "HSCAN %1 0 COUNT 10000")
{
}
: KeyModel(connection, fullPath, dbIndex, ttl, true, "HLEN",
"HSCAN %1 0 COUNT 10000") {}

QString HashKeyModel::getType()
{
return "hash";
}
QString HashKeyModel::getType() { return "hash"; }

QStringList HashKeyModel::getColumnNames()
{
return QStringList() << "row" << "key" << "value";
QStringList HashKeyModel::getColumnNames() {
return QStringList() << "row"
<< "key"
<< "value";
}

QHash<int, QByteArray> HashKeyModel::getRoles()
{
QHash<int, QByteArray> roles;
roles[Roles::RowNumber] = "row";
roles[Roles::Key] = "key";
roles[Roles::Value] = "value";
return roles;
QHash<int, QByteArray> HashKeyModel::getRoles() {
QHash<int, QByteArray> roles;
roles[Roles::RowNumber] = "row";
roles[Roles::Key] = "key";
roles[Roles::Value] = "value";
return roles;
}

QVariant HashKeyModel::getData(int rowIndex, int dataRole)
{
if (!isRowLoaded(rowIndex))
return QVariant();
QVariant HashKeyModel::getData(int rowIndex, int dataRole) {
if (!isRowLoaded(rowIndex)) return QVariant();

QPair<QByteArray, QByteArray> row = m_rowsCache[rowIndex];
QPair<QByteArray, QByteArray> row = m_rowsCache[rowIndex];

if (dataRole == Roles::Key)
return row.first;
else if (dataRole == Roles::Value)
return row.second;
else if (dataRole == Roles::RowNumber)
return rowIndex+1;
if (dataRole == Roles::Key)
return row.first;
else if (dataRole == Roles::Value)
return row.second;
else if (dataRole == Roles::RowNumber)
return rowIndex + 1;

return QVariant();
return QVariant();
}

void HashKeyModel::updateRow(int rowIndex, const QVariantMap &row)
{
if (!isRowLoaded(rowIndex) || !isRowValid(row))
throw Exception(QObject::tr("Invalid row"));
void HashKeyModel::updateRow(int rowIndex, const QVariantMap &row) {
if (!isRowLoaded(rowIndex) || !isRowValid(row))
throw Exception(QCoreApplication::translate("RDM", "Invalid row"));

QPair<QByteArray, QByteArray> cachedRow = m_rowsCache[rowIndex];
QPair<QByteArray, QByteArray> cachedRow = m_rowsCache[rowIndex];

bool keyChanged = cachedRow.first != row["key"].toString();
bool valueChanged = cachedRow.second != row["value"].toString();
bool keyChanged = cachedRow.first != row["key"].toString();
bool valueChanged = cachedRow.second != row["value"].toString();

QPair<QByteArray, QByteArray> newRow(
(keyChanged) ? row["key"].toByteArray() : cachedRow.first,
(valueChanged) ? row["value"].toByteArray() : cachedRow.second
);
QPair<QByteArray, QByteArray> newRow(
(keyChanged) ? row["key"].toByteArray() : cachedRow.first,
(valueChanged) ? row["value"].toByteArray() : cachedRow.second);

if (keyChanged) {
deleteHashRow(cachedRow.first);
}
if (keyChanged) {
deleteHashRow(cachedRow.first);
}

setHashRow(newRow.first, newRow.second);
m_rowsCache.replace(rowIndex, newRow);
setHashRow(newRow.first, newRow.second);
m_rowsCache.replace(rowIndex, newRow);
}

void HashKeyModel::addRow(const QVariantMap &row)
{
if (!isRowValid(row))
throw Exception(QObject::tr("Invalid row"));
void HashKeyModel::addRow(const QVariantMap &row) {
if (!isRowValid(row))
throw Exception(QCoreApplication::translate("RDM", "Invalid row"));

setHashRow(row["key"].toByteArray(), row["value"].toByteArray(), false);
m_rowCount++;
setHashRow(row["key"].toByteArray(), row["value"].toByteArray(), false);
m_rowCount++;
}

void HashKeyModel::removeRow(int i)
{
if (!isRowLoaded(i))
return;
void HashKeyModel::removeRow(int i) {
if (!isRowLoaded(i)) return;

QPair<QByteArray, QByteArray> row = m_rowsCache[i];
QPair<QByteArray, QByteArray> row = m_rowsCache[i];

deleteHashRow(row.first);
deleteHashRow(row.first);

m_rowCount--;
m_rowsCache.removeAt(i);
setRemovedIfEmpty();
m_rowCount--;
m_rowsCache.removeAt(i);
setRemovedIfEmpty();
}

void HashKeyModel::setHashRow(const QByteArray &hashKey, const QByteArray &hashValue,
bool updateIfNotExist)
{
using namespace RedisClient;

Response result;

try {
result = m_connection->commandSync({(updateIfNotExist)? "HSET" : "HSETNX",
m_keyFullPath, hashKey, hashValue}, m_dbIndex);
} catch (const RedisClient::Connection::Exception& e) {
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
}

if (updateIfNotExist == false
&& result.getValue().toInt() == 0)
throw Exception(QObject::tr("Value with the same key already exist"));
void HashKeyModel::setHashRow(const QByteArray &hashKey,
const QByteArray &hashValue,
bool updateIfNotExist) {
using namespace RedisClient;

Response result;

try {
result = m_connection->commandSync({(updateIfNotExist) ? "HSET" : "HSETNX",
m_keyFullPath, hashKey, hashValue},
m_dbIndex);
} catch (const RedisClient::Connection::Exception &e) {
throw Exception(QCoreApplication::translate("RDM", "Connection error: ") +
QString(e.what()));
}

if (updateIfNotExist == false && result.getValue().toInt() == 0)
throw Exception(QCoreApplication::translate(
"RDM", "Value with the same key already exist"));
}

void HashKeyModel::deleteHashRow(const QByteArray &hashKey)
{
try {
m_connection->commandSync({"HDEL", m_keyFullPath, hashKey}, m_dbIndex);
} catch (const RedisClient::Connection::Exception& e) {
throw Exception(QObject::tr("Connection error: ") + QString(e.what()));
}
void HashKeyModel::deleteHashRow(const QByteArray &hashKey) {
try {
m_connection->commandSync({"HDEL", m_keyFullPath, hashKey}, m_dbIndex);
} catch (const RedisClient::Connection::Exception &e) {
throw Exception(QCoreApplication::translate("RDM", "Connection error: ") +
QString(e.what()));
}
}

void HashKeyModel::addLoadedRowsToCache(const QVariantList &rows, int rowStart)
{
QList<QPair<QByteArray, QByteArray>> result;

for (QVariantList::const_iterator item = rows.begin();
item != rows.end(); ++item) {
void HashKeyModel::addLoadedRowsToCache(const QVariantList &rows,
int rowStart) {
QList<QPair<QByteArray, QByteArray>> result;

QPair<QByteArray, QByteArray> value;
value.first = item->toByteArray();
++item;
for (QVariantList::const_iterator item = rows.begin(); item != rows.end();
++item) {
QPair<QByteArray, QByteArray> value;
value.first = item->toByteArray();
++item;

if (item == rows.end())
throw Exception(QObject::tr("Data was loaded from server partially."));
if (item == rows.end())
throw Exception(QCoreApplication::translate(
"RDM", "Data was loaded from server partially."));

value.second = item->toByteArray();
result.push_back(value);
}
value.second = item->toByteArray();
result.push_back(value);
}

m_rowsCache.addLoadedRange({rowStart, rowStart + result.size() - 1},
result);
m_rowsCache.addLoadedRange({rowStart, rowStart + result.size() - 1}, result);
}
Loading

0 comments on commit 7714d49

Please sign in to comment.