1
1
#include " treeoperations.h"
2
2
#include < qredisclient/redisclient.h>
3
- #include < qredisclient/utils/compat.h>
4
3
#include " app/widgets/consoletabs.h"
5
4
#include " app/models/connectionconf.h"
6
5
#include " console/consoletab.h"
@@ -14,98 +13,47 @@ TreeOperations::TreeOperations(QSharedPointer<RedisClient::Connection> connectio
14
13
{
15
14
}
16
15
17
- void TreeOperations::getDatabases (std::function<void (ConnectionsTree::Operations ::DatabaseList)> callback)
16
+ void TreeOperations::getDatabases (std::function<void (RedisClient ::DatabaseList)> callback)
18
17
{
19
- using namespace RedisClient ;
20
-
21
- // Get keys count
22
- Response result;
23
- try {
24
- result = m_connection->commandSync (" info" );
25
- } catch (const RedisClient::Connection::Exception& e) {
26
- throw ConnectionsTree::Operations::Exception (" Connection error:\n\n " + QString (e.what ()));
18
+ if (!m_connection->isConnected ()) {
19
+ try {
20
+ m_connection->connect (true );
21
+ } catch (const RedisClient::Connection::Exception& e) {
22
+ throw ConnectionsTree::Operations::Exception (" Connection error: " + QString (e.what ()));
23
+ }
27
24
}
28
25
29
- DatabaseList availableDatabeses;
30
- QSet<int > loadedDatabeses;
26
+ RedisClient::DatabaseList availableDatabeses = m_connection->getKeyspaceInfo ();
31
27
32
- if (result. isErrorMessage ()) {
33
- return callback (availableDatabeses) ;
34
- }
28
+ // detect all databases
29
+ RedisClient::Response scanningResp ;
30
+ int dbIndex = (availableDatabeses. size () == 0 )? 0 : availableDatabeses. lastKey () + 1 ;
35
31
36
- // Parse keyspace info
37
- QString keyspaceInfo = result.getValue ().toString ();
38
- QRegularExpression getDbAndKeysCount (" ^db(\\ d+):keys=(\\ d+)" );
39
- getDbAndKeysCount.setPatternOptions (QRegularExpression::MultilineOption);
40
- QRegularExpressionMatchIterator iter = getDbAndKeysCount.globalMatch (keyspaceInfo);
41
- while (iter.hasNext ()) {
42
- QRegularExpressionMatch match = iter.next ();
43
- int dbIndex = match.captured (1 ).toInt ();
44
- availableDatabeses.push_back ({dbIndex, match.captured (2 ).toInt ()});
45
- loadedDatabeses.insert (dbIndex);
46
- }
32
+ while (true ) {
33
+ try {
34
+ scanningResp = m_connection->commandSync (" select" , QString::number (dbIndex));
35
+ } catch (const RedisClient::Connection::Exception& e) {
36
+ throw ConnectionsTree::Operations::Exception (" Connection error: " + QString (e.what ()));
37
+ }
47
38
48
- int dbCount = (loadedDatabeses.isEmpty ())? 0 : *std::max_element (loadedDatabeses.begin (),
49
- loadedDatabeses.end ());
50
- // detect more db if needed
51
- if (dbCount == 0 ) {
52
- Response scanningResp;
53
- do {
54
- try {
55
- scanningResp = m_connection->commandSync (" select" , QString::number (dbCount));
56
- } catch (const RedisClient::Connection::Exception& e) {
57
- throw ConnectionsTree::Operations::Exception (" Connection error: " + QString (e.what ()));
58
- }
59
- } while (scanningResp.isOkMessage () && ++dbCount);
60
- }
39
+ if (!scanningResp.isOkMessage ())
40
+ break ;
61
41
62
- // build db list
63
- for (int dbIndex = 0 ; dbIndex < dbCount; ++dbIndex)
64
- {
65
- if (loadedDatabeses.contains (dbIndex))
66
- continue ;
67
- availableDatabeses.push_back ({dbIndex, 0 });
42
+ availableDatabeses.insert (dbIndex, 0 );
43
+ ++dbIndex;
68
44
}
69
45
70
- std::sort (availableDatabeses.begin (), availableDatabeses.end (),
71
- [](QPair<int , int > l, QPair<int , int > r) {
72
- return l.first < r.first ;
73
- });
74
-
75
46
return callback (availableDatabeses);
76
47
}
77
48
78
- void TreeOperations::getDatabaseKeys (uint dbIndex, std::function<void (const RawKeysList &, const QString &)> callback)
49
+ void TreeOperations::getDatabaseKeys (uint dbIndex, std::function<void (const RedisClient::Connection:: RawKeysList &, const QString &)> callback)
79
50
{
80
51
QString keyPattern = static_cast <ConnectionConfig>(m_connection->getConfig ()).keysPattern ();
81
52
82
- if (m_connection->getServerVersion () >= 2.8 ) {
83
- QList<QByteArray> rawCmd {
84
- " scan" , " 0" , " MATCH" , keyPattern.toUtf8 (), " COUNT" , " 10000"
85
- };
86
- QSharedPointer<RedisClient::ScanCommand> keyCmd (new RedisClient::ScanCommand (rawCmd, dbIndex));
87
-
88
- try {
89
- m_connection->retrieveCollection (keyCmd, [this , callback](QVariant r, QString err)
90
- {
91
- if (!err.isEmpty ())
92
- callback (RawKeysList (), QString (" Cannot load keys: %1" ).arg (err));
93
-
94
- callback (convertQVariantList (r.toList ()), QString ());
95
- });
96
- } catch (const RedisClient::Connection::Exception& error) {
97
- callback (RawKeysList (), QString (" Cannot load keys: %1" ).arg (error.what ()));
98
- }
99
- } else {
100
- try {
101
- m_connection->command ({" KEYS" , keyPattern.toUtf8 ()}, this ,
102
- [this , callback](RedisClient::Response r, QString)
103
- {
104
- callback (convertQVariantList (r.getValue ().toList ()), QString ());
105
- }, dbIndex);
106
- } catch (const RedisClient::Connection::Exception& error) {
107
- callback (RawKeysList (), QString (" Cannot load keys: %1" ).arg (error.what ()));
108
- }
53
+ try {
54
+ m_connection->getDatabaseKeys (callback, keyPattern, dbIndex);
55
+ } catch (const RedisClient::Connection::Exception& error) {
56
+ callback (RedisClient::Connection::RawKeysList (), QString (" Cannot load keys: %1" ).arg (error.what ()));
109
57
}
110
58
}
111
59
0 commit comments