File: Redis.h

package info (click to toggle)
ntopng 5.2.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 121,832 kB
  • sloc: javascript: 143,431; cpp: 71,175; ansic: 11,108; sh: 4,687; makefile: 911; python: 587; sql: 512; pascal: 234; perl: 118; ruby: 52; exp: 4
file content (129 lines) | stat: -rw-r--r-- 5,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 * (C) 2013-22 - ntop.org
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#ifndef _REDIS_H_
#define _REDIS_H_

#include "ntop_includes.h"

class Host;

class Redis {
 private:
  redisContext *redis;
  Mutex *l;
  char *redis_host, *redis_password, *redis_version;
#ifdef __linux__
  bool is_socket_connection;
#endif
  struct {
    u_int32_t num_expire, num_get, num_ttl, num_del,
      num_hget, num_hset, num_hdel, num_set,
      num_keys, num_hkeys, num_llen, num_other,
      num_hgetall, num_trim, num_lpush_rpush,
      num_lpop_rpop, num_strlen, num_saved_lookups,
      num_get_address, num_set_resolved_address;
    u_int32_t num_reconnections;
  } stats;
  u_int32_t num_redis_version;
  u_int16_t redis_port;
  u_int8_t redis_db_id;
  pthread_t esThreadLoop;
  pthread_t lsThreadLoop;
  bool operational;
  bool initializationCompleted;
  std::map<std::string, StringCache> stringCache;
  StringFifoQueue *localToResolve, *remoteToResolve;
  u_int numCached;

  char* getRedisVersion();
  void reconnectRedis(bool giveup_on_failure);
  int msg_push(const char * const cmd, const char * const queue_name, const char * const msg, u_int queue_trim_size,
	       bool trace_errors = true, bool head_trim = true);
  int lrpop(const char *queue_name, char *buf, u_int buf_len, bool lpop);
  void addToCache(const char * const key, const char * const value, u_int expire_secs);
  bool isCacheable(const char * const key);
  bool expireCache(char *key, u_int expire_sec);

  void checkDumpable(const char * const key);
  int _set(bool use_nx, const char * const key, const char * const value, u_int expire_secs);
  
 public:
  Redis(const char *redis_host = (char*)"127.0.0.1",
	const char *redis_password = NULL,
	u_int16_t redis_port = 6379, u_int8_t _redis_db_id = 0,
	bool giveup_on_failure = false);
  ~Redis();

  inline char* getVersion()        { return(redis_version);     }
  inline u_int32_t getNumVersion() { return(num_redis_version); }
  inline bool haveRedisDump()      { return((num_redis_version >= 0x020600) ? true : false); }
  void setDefaults();
  inline bool isOperational() { return(operational); };
  inline void setInitializationComplete() { initializationCompleted = true; };
  int info(char *rsp, u_int rsp_len);
  u_int dbsize();
  int expire(char *key, u_int expire_sec);
  int get(char *key, char *rsp, u_int rsp_len, bool cache_it = false);
  int hashGet(const char * const key, const char * const member, char * const rsp, u_int rsp_len);
  int hashDel(const char * const key, const char * const field);
  int hashSet(const char * const key, const char * const field, const char * const value);
  int delHash(char *key, char *member);
  inline int set(const char * const key, const char * const value, u_int expire_secs=0) { return(_set(false, key, value, expire_secs)); }
  /* setnx = set if not existing */
  inline int setnx(const char * const key, const char * const value, u_int expire_secs=0) { return(_set(true, key, value, expire_secs)); }
  int keys(const char *pattern, char ***keys_p);
  int hashKeys(const char *pattern, char ***keys_p);
  int hashGetAll(const char *key, char ***keys_p, char ***values_p);
  int del(char *key);
  int pushHostToResolve(char *hostname, bool dont_check_for_existence, bool localHost);
  int popHostToResolve(char *hostname, u_int hostname_len);

  int getAddress(char *numeric_ip, char *rsp, u_int rsp_len, bool queue_if_not_found);
  int setResolvedAddress(char *numeric_ip, char *symbolic_ip);

  int sadd(const char *set_name, char *item);
  int srem(const char *set_name, char *item);
  int smembers(lua_State* vm, char *setName);
  int smembers(const char *set_name, char ***members);
  bool sismember(const char *set_name, const char * const member);

  int lpush(const char * const queue_name, const char * const msg, u_int queue_trim_size, bool trace_errors = true);
  int rpush(const char * const queue_name, const char * const msg, u_int queue_trim_size);
  int lindex(const char *queue_name, int idx, char *buf, u_int buf_len);
  int ltrim(const char *queue_name, int start_idx, int end_idx);
  u_int hstrlen(const char * const key, const char * const value);
  u_int len(const char * const key);
  u_int llen(const char *queue_name);
  int lset(const char *queue_name, u_int32_t idx, const char *value);
  int lrem(const char *queue_name, const char *value);
  int lrange(const char *list_name, char ***elements, int start_offset, int end_offset);
  int lpop(const char *queue_name, char *buf, u_int buf_len);
  int rpop(const char *queue_name, char *buf, u_int buf_len);
  int incr(const char *key, int amount);
  int flushDb();
  void flushCache();
  void lua(lua_State *vm);
  char* dump(char *key);
  int restore(char *key, char *buf);
};

#endif /* _REDIS_H_ */