qredisclient
connection.h
1 #pragma once
2 #include <QObject>
3 #include <QList>
4 #include <QMap>
5 #include <QByteArray>
6 #include <functional>
7 #include <QVariantList>
8 #include <QSharedPointer>
9 #include <QEventLoop>
10 #include <QTimer>
11 #include <QMutex>
12 #include "connectionconfig.h"
13 #include "exception.h"
14 #include "command.h"
15 #include "response.h"
16 #include "scancommand.h"
17 
18 namespace RedisClient {
19 
20 class AbstractTransporter;
21 class CommandExecutor;
22 
23 typedef QMap<int, int> DatabaseList;
24 
29 struct ServerInfo
30 {
31  ServerInfo();
32 
33  double version;
34  bool clusterMode;
35  bool sentinelMode;
36  DatabaseList databases;
37 
38  class ParsedServerInfo : public QHash<QString, QHash<QString, QString>>
39  {
40  public:
41  QVariantMap toVariantMap();
42  };
43 
44  ParsedServerInfo parsed;
45 
46  static ServerInfo fromString(const QString& info);
47 };
48 
53 class Connection : public QObject
54 {
55  Q_OBJECT
56  ADD_EXCEPTION
57 
58  friend class AbstractTransporter;
59 
60 public:
61  enum class Mode { Normal, PubSub, Cluster, Sentinel };
62  class InvalidModeException : public Connection::Exception {};
63 public:
69  Connection(const ConnectionConfig &c, bool autoConnect=true);
70 
75  virtual ~Connection();
76 
83  virtual bool connect(bool wait=true);
84 
89  virtual bool isConnected();
90 
94  virtual void disconnect();
95 
100  ConnectionConfig getConfig() const;
101 
105  void setConnectionConfig(const ConnectionConfig &);
106 
111  Mode mode() const;
112 
117  virtual double getServerVersion();
118 
123  virtual DatabaseList getKeyspaceInfo();
124 
125  /*
126  * Hi-Level Operations API
127  */
131  typedef QList<QByteArray> RawKeysList;
132  typedef std::function<void(const RawKeysList&, const QString&)> RawKeysListCallback;
133 
140  virtual void getDatabaseKeys(RawKeysListCallback callback,
141  const QString& pattern=QString("*"),
142  uint dbIndex = 0);
143 
149  virtual void getClusterKeys(RawKeysListCallback callback,
150  const QString &pattern);
151 
152 
153  typedef QPair<QString, int> Host;
154  typedef QList<Host> HostList;
155 
160  HostList getMasterNodes();
161 
162  /*
163  * Command execution API
164  */
169  void command(const Command& cmd);
170 
176  void command(QList<QByteArray> rawCmd, int db = -1);
177 
185  void command(QList<QByteArray> rawCmd, QObject *owner,
186  RedisClient::Command::Callback callback, int db = -1);
187 
193  Response commandSync(const Command& cmd);
194 
201  Response commandSync(QList<QByteArray> rawCmd, int db = -1);
202 
203  /*
204  * Aliases for ^ function
205  */
206  Response commandSync(QString cmd, int db = -1);
207  Response commandSync(QString cmd, QString arg1, int db = -1);
208  Response commandSync(QString cmd, QString arg1, QString arg2, int db = -1);
209  Response commandSync(QString cmd, QString arg1, QString arg2, QString arg3, int db = -1);
210 
214  typedef std::function<void(QVariant, QString err)> CollectionCallback;
215 
219  typedef std::function<void(QVariant, QString err, bool final)> IncrementalCollectionCallback;
220 
226  virtual void retrieveCollection(QSharedPointer<ScanCommand> cmd,
227  CollectionCallback callback);
228 
234  virtual void retrieveCollectionIncrementally(QSharedPointer<ScanCommand> cmd,
235  IncrementalCollectionCallback callback);
236 
241  virtual void runCommand(const Command &cmd);
242 
243 
248  bool waitForIdle(uint timeout);
249 
250  /*
251  * Low level functions for modification
252  * commands execution.
253  */
254  void setTransporter(QSharedPointer<AbstractTransporter>);
255  QSharedPointer<AbstractTransporter> getTransporter() const;
256 
257 signals:
258  void addCommandToWorker(const Command&);
259  void error(const QString&);
260  void log(const QString&);
261  void connected();
262  void disconnected();
263  void authOk();
264  void authError(const QString&);
265 
266  // Cluster & Sentinel
267  void reconnectTo(const QString& host, int port);
268 
269 protected:
270  void createTransporter();
271  bool isTransporterRunning();
272 
273  Response internalCommandSync(QList<QByteArray> rawCmd);
274 
275  void processScanCommand(QSharedPointer<ScanCommand> cmd,
276  CollectionCallback callback,
277  QSharedPointer<QVariantList> result=QSharedPointer<QVariantList>(),
278  bool incrementalProcessing=false);
279 
280  void changeCurrentDbNumber(int db);
281 
282 protected slots:
283  void auth();
284 
285 protected:
286  ConnectionConfig m_config;
287  QSharedPointer<QThread> m_transporterThread;
288  QSharedPointer<AbstractTransporter> m_transporter;
289 
290  int m_dbNumber;
291  ServerInfo m_serverInfo;
292  Mode m_currentMode;
293  QMutex m_dbNumberMutex;
294  bool m_autoConnect;
295  bool m_stoppingTransporter;
296  RawKeysListCallback m_wrapper;
297 };
298 }
Definition: command.h:8
The Command class.
Definition: command.h:18
The Connection class Main client class.
Definition: connection.h:53
std::function< void(QVariant, QString err, bool final)> IncrementalCollectionCallback
IncrementalCollectionCallback.
Definition: connection.h:219
The ServerInfo struct Represents redis-server information parsed from INFO command.
Definition: connection.h:29
QList< QByteArray > RawKeysList
RawKeysList.
Definition: connection.h:131
The ConnectionConfig class Supports loading settigns from JSON objects.
Definition: connectionconfig.h:16
Definition: response.h:12
std::function< void(QVariant, QString err)> CollectionCallback
CollectionCallback.
Definition: connection.h:214