Skip to content

Commit 63ff5b0

Browse files
committed
Add support for regex in NS separators
Fix #3669
1 parent 01375b1 commit 63ff5b0

File tree

11 files changed

+88
-33
lines changed

11 files changed

+88
-33
lines changed

src/app/models/connectionconf.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ void ServerConfig::setNamespaceSeparator(QString ns)
3939
return setParam<QString>("namespace_separator", ns);
4040
}
4141

42+
bool ServerConfig::namespaceSeparatorIsRegex() const
43+
{
44+
return param<bool>("namespace_separator_is_regex", false);
45+
}
46+
47+
void ServerConfig::setNamespaceSeparatorIsRegex(bool v)
48+
{
49+
return setParam<bool>("namespace_separator_is_regex", v);
50+
}
51+
4252
uint ServerConfig::databaseScanLimit() const
4353
{
4454
return param<uint>("db_scan_limit", DEFAULT_DB_SCAN_LIMIT);

src/app/models/connectionconf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ServerConfig : public RedisClient::ConnectionConfig
3232
/* Advanced settings */
3333
Q_PROPERTY(QString keysPattern READ keysPattern WRITE setKeysPattern)
3434
Q_PROPERTY(QString namespaceSeparator READ namespaceSeparator WRITE setNamespaceSeparator)
35+
Q_PROPERTY(bool namespaceSeparatorIsRegex READ namespaceSeparatorIsRegex WRITE setNamespaceSeparatorIsRegex)
3536
Q_PROPERTY(uint executeTimeout READ executeTimeout WRITE setExecutionTimeout)
3637
Q_PROPERTY(uint connectionTimeout READ connectionTimeout WRITE setConnectionTimeout)
3738
Q_PROPERTY(bool overrideClusterHost READ overrideClusterHost WRITE setClusterHostOverride)
@@ -59,6 +60,9 @@ class ServerConfig : public RedisClient::ConnectionConfig
5960
QString namespaceSeparator() const;
6061
void setNamespaceSeparator(QString);
6162

63+
bool namespaceSeparatorIsRegex() const;
64+
void setNamespaceSeparatorIsRegex(bool v);
65+
6266
bool luaKeysLoading() const;
6367
void setLuaKeysLoading(bool);
6468

src/app/models/treeoperations.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,9 @@ void TreeOperations::resetConnection() {
252252
QtConcurrent::run([oldConnection]() { oldConnection->disconnect(); });
253253
}
254254

255-
QString TreeOperations::getNamespaceSeparator() {
256-
return m_config.namespaceSeparator();
255+
QRegExp TreeOperations::getNamespaceSeparator() {
256+
return QRegExp(m_config.namespaceSeparator(), Qt::CaseSensitive,
257+
m_config.namespaceSeparatorIsRegex()? QRegExp::RegExp : QRegExp::FixedString);
257258
}
258259

259260
QString TreeOperations::defaultFilter() { return m_config.keysPattern(); }

src/app/models/treeoperations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class TreeOperations : public QObject,
3535

3636
void resetConnection() override;
3737

38-
QString getNamespaceSeparator() override;
38+
QRegExp getNamespaceSeparator() override;
3939

4040
QString defaultFilter() override;
4141

src/modules/connections-tree/items/keyitem.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ QString KeyItem::getDisplayName() const {
4040
m_shortRendering) {
4141
auto parent = parentTreeItemToNs(m_parent);
4242

43-
title = printableString(getFullPath().mid(
44-
parent->getFullPath().size() +
45-
parent->operations()->getNamespaceSeparator().size()));
43+
auto nsRx = parent->operations()->getNamespaceSeparator();
44+
int searchFrom = parent->getFullPath().size() > 0 ? parent->getFullPath().size() - 1 : 0;
45+
int nsRxPos = QString::fromUtf8(getFullPath()).indexOf(nsRx, searchFrom);
46+
47+
int nsSize = 0;
48+
49+
if (nsRxPos >= 0) {
50+
nsSize = nsRx.matchedLength();
51+
}
52+
53+
title = printableString(getFullPath().mid(parent->getFullPath().size() + nsSize));
4654
} else {
4755
title = printableString(getFullPath(), true);
4856
}

src/modules/connections-tree/items/namespaceitem.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ using namespace ConnectionsTree;
1414
NamespaceItem::NamespaceItem(const QByteArray &fullPath,
1515
QSharedPointer<Operations> operations,
1616
QWeakPointer<TreeItem> parent, Model &model,
17-
uint dbIndex, QRegExp filter)
17+
uint dbIndex, QRegExp filter, QString lastNsSeparator)
1818
: AbstractNamespaceItem(model, parent, operations, dbIndex, filter),
1919
m_fullPath(fullPath),
20+
m_lastNsSeparator(lastNsSeparator),
2021
m_removed(false) {}
2122

2223
QString NamespaceItem::getDisplayName() const {
@@ -32,10 +33,11 @@ QString NamespaceItem::getDisplayName() const {
3233
}
3334

3435
QByteArray NamespaceItem::getName() const {
35-
qsizetype pos = m_fullPath.lastIndexOf(m_operations->getNamespaceSeparator());
36+
auto rx = m_operations->getNamespaceSeparator();
37+
qsizetype pos = QString::fromUtf8(m_fullPath).lastIndexOf(rx);
3638

3739
if (pos >= 0) {
38-
return m_fullPath.mid(pos + m_operations->getNamespaceSeparator().size());
40+
return m_fullPath.mid(pos + rx.matchedLength());
3941
} else {
4042
return m_fullPath;
4143
}
@@ -78,15 +80,15 @@ void NamespaceItem::load() {
7880

7981
QString nsFilter = QString("%1%2*")
8082
.arg(QString::fromUtf8(m_fullPath))
81-
.arg(m_operations->getNamespaceSeparator());
83+
.arg(m_lastNsSeparator);
8284

8385
if (!m_filter.isEmpty()) {
8486
if (m_filter.pattern().startsWith(nsFilter.chopped(1))) {
8587
nsFilter = m_filter.pattern();
8688
} else {
8789
nsFilter = QString("%1%2%3")
8890
.arg(QString::fromUtf8(m_fullPath))
89-
.arg(m_operations->getNamespaceSeparator())
91+
.arg(m_lastNsSeparator)
9092
.arg(m_filter.pattern());
9193
}
9294
}
@@ -142,7 +144,7 @@ QHash<QString, std::function<void()>> NamespaceItem::eventHandlers() {
142144
},
143145
QString("%1%2")
144146
.arg(QString::fromUtf8(getFullPath()))
145-
.arg(m_operations->getNamespaceSeparator()));
147+
.arg(m_lastNsSeparator));
146148
});
147149

148150
events.insert("reload", [this]() { reload(); });

src/modules/connections-tree/items/namespaceitem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NamespaceItem : public AbstractNamespaceItem {
1515
NamespaceItem(const QByteArray& fullPath,
1616
QSharedPointer<Operations> operations,
1717
QWeakPointer<TreeItem> parent, Model& model, uint dbIndex,
18-
QRegExp filter);
18+
QRegExp filter, QString lastNsSeparator);
1919

2020
QString getDisplayName() const override;
2121

@@ -38,6 +38,7 @@ class NamespaceItem : public AbstractNamespaceItem {
3838

3939
private:
4040
QByteArray m_fullPath;
41+
QString m_lastNsSeparator;
4142
bool m_removed;
4243
};
4344
} // namespace ConnectionsTree

src/modules/connections-tree/keysrendering.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,20 @@ void KeysTreeRenderer::renderKeys(QSharedPointer<Operations> operations,
2525

2626
int unprocessedPartStart = 0;
2727
if (parent->getFullPath().size() > 0 || parent->type() == "namespace") {
28-
unprocessedPartStart =
29-
parent->getFullPath().size() + settings.nsSeparator.length();
28+
int nsLength = 0;
29+
30+
if (keys.size() > 0) {
31+
QString firstKey = QString::fromUtf8(keys[0]);
32+
int res = firstKey.indexOf(settings.nsSeparator, parent->getFullPath().size());
33+
34+
qDebug() << "NSs regex pos:" << res;
35+
36+
nsLength = settings.nsSeparator.matchedLength();
37+
}
38+
39+
unprocessedPartStart =
40+
parent->getFullPath().size() + nsLength;
41+
3042
}
3143

3244
QByteArray rawKey;
@@ -70,10 +82,17 @@ void KeysTreeRenderer::renderLazily(
7082
QWeakPointer<TreeItem> currentParent =
7183
parent.staticCast<TreeItem>().toWeakRef();
7284

73-
int indexOfNaspaceSeparator =
74-
(settings.nsSeparator.isEmpty())
75-
? -1
76-
: notProcessedKeyPart.indexOf(settings.nsSeparator);
85+
int indexOfNaspaceSeparator = -1;
86+
auto nsSeparator = settings.nsSeparator;
87+
int nsSeparatorLength = nsSeparator.pattern().size();
88+
89+
if (!nsSeparator.isEmpty() && nsSeparator.patternSyntax() == QRegExp::RegExp) {
90+
QString keyPart = QString::fromUtf8(notProcessedKeyPart);
91+
indexOfNaspaceSeparator = keyPart.indexOf(nsSeparator);
92+
93+
qDebug() << "NSs regex pos:" << indexOfNaspaceSeparator << nsSeparator.cap();
94+
nsSeparatorLength = nsSeparator.matchedLength();
95+
}
7796

7897
if (indexOfNaspaceSeparator == -1) {
7998
QSharedPointer<KeyItem> newKey(
@@ -94,7 +113,8 @@ void KeysTreeRenderer::renderLazily(
94113
QByteArray namespaceFullPath = fullKey.mid(0, nsPos);
95114

96115
// Single namespaced key
97-
if (nextKey.isEmpty() || nextKey.indexOf(namespaceFullPath) == -1) {
116+
if (nsSeparator.patternSyntax() != QRegExp::RegExp
117+
&& (nextKey.isEmpty() || nextKey.indexOf(namespaceFullPath) == -1)) {
98118
QSharedPointer<KeyItem> newKey(
99119
new KeyItem(fullKey, currentParent, parent->model()));
100120
parent->append(newKey);
@@ -103,7 +123,7 @@ void KeysTreeRenderer::renderLazily(
103123

104124
namespaceItem = QSharedPointer<NamespaceItem>(
105125
new NamespaceItem(namespaceFullPath, m_operations, currentParent,
106-
parent->model(), settings.dbIndex, settings.filter));
126+
parent->model(), settings.dbIndex, settings.filter, nsSeparator.cap()));
107127

108128
if (expandedNamespaces.contains(namespaceFullPath)) {
109129
namespaceItem->setExpanded(true);
@@ -113,7 +133,6 @@ void KeysTreeRenderer::renderLazily(
113133
}
114134

115135
renderLazily(namespaceItem,
116-
notProcessedKeyPart.mid(indexOfNaspaceSeparator +
117-
settings.nsSeparator.length()),
136+
notProcessedKeyPart.mid(indexOfNaspaceSeparator + nsSeparatorLength),
118137
fullKey, m_operations, settings, expandedNamespaces, level + 1, nextKey);
119138
}

src/modules/connections-tree/keysrendering.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ConnectionsTree {
1616
public:
1717
struct RenderingSettigns {
1818
QRegExp filter;
19-
QString nsSeparator;
19+
QRegExp nsSeparator;
2020
uint dbIndex;
2121
bool sortKeys;
2222
};

src/modules/connections-tree/operations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Operations {
5757
* @brief getNamespaceSeparator
5858
* @return
5959
*/
60-
virtual QString getNamespaceSeparator() = 0;
60+
virtual QRegExp getNamespaceSeparator() = 0;
6161

6262
virtual QString defaultFilter() = 0;
6363

0 commit comments

Comments
 (0)