-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathredisselect.cpp
More file actions
134 lines (112 loc) · 3.01 KB
/
redisselect.cpp
File metadata and controls
134 lines (112 loc) · 3.01 KB
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
130
131
132
133
134
#include <string>
#include <memory>
#include <hiredis/hiredis.h>
#include "dbconnector.h"
#include "redisreply.h"
#include "selectable.h"
#include "redisselect.h"
namespace swss {
constexpr unsigned int RedisSelect::SUBSCRIBE_TIMEOUT;
RedisSelect::RedisSelect(int pri) : Selectable(pri), m_queueLength(-1)
{
}
RedisSelect::RedisSelect(DBConnector* parent, int pri) : Selectable(pri), m_queueLength(-1)
{
m_subscribe.reset(parent->newConnector(SUBSCRIBE_TIMEOUT));
}
int RedisSelect::getFd()
{
return m_subscribe->getContext()->fd;
}
const DBConnector* RedisSelect::getDbConnector() const
{
return m_subscribe.get();
}
uint64_t RedisSelect::readData()
{
redisReply *reply = nullptr;
if (redisGetReply(m_subscribe->getContext(), reinterpret_cast<void**>(&reply)) != REDIS_OK)
throw std::runtime_error("Unable to read redis reply from RedisSelect::readData() redisGetReply()");
freeReplyObject(reply);
m_queueLength++;
reply = nullptr;
int status;
do
{
status = redisGetReplyFromReader(m_subscribe->getContext(), reinterpret_cast<void**>(&reply));
if(reply != nullptr && status == REDIS_OK)
{
m_queueLength++;
freeReplyObject(reply);
}
}
while(reply != nullptr && status == REDIS_OK);
if (status != REDIS_OK)
{
throw std::runtime_error("Unable to read redis reply from RedisSelect::readData() redisGetReplyFromReader()");
}
return 0;
}
bool RedisSelect::hasData()
{
return m_queueLength > 0;
}
bool RedisSelect::hasCachedData()
{
return m_queueLength > 1;
}
bool RedisSelect::initializedWithData()
{
return m_queueLength > 0;
}
void RedisSelect::updateAfterRead()
{
m_queueLength--;
}
/* Create a new redisContext, SELECT DB and SUBSCRIBE */
void RedisSelect::subscribe(DBConnector* db, const std::string &channelName)
{
m_subscribe.reset(db->newConnector(SUBSCRIBE_TIMEOUT));
/* Send SUBSCRIBE #channel command */
m_subscribe->subscribe(channelName);
}
void RedisSelect::subscribe(const std::string &channelName)
{
/* Send SUBSCRIBE #channel command */
m_subscribe->subscribe(channelName);
}
/* PSUBSCRIBE */
void RedisSelect::psubscribe(DBConnector* db, const std::string &channelName)
{
m_subscribe.reset(db->newConnector(SUBSCRIBE_TIMEOUT));
/*
* Send PSUBSCRIBE #channel command on the
* non-blocking subscriber DBConnector
*/
m_subscribe->psubscribe(channelName);
}
void RedisSelect::psubscribe(const std::string &channelName)
{
/*
* Send PSUBSCRIBE #channel command on the
* non-blocking subscriber DBConnector
*/
m_subscribe->psubscribe(channelName);
}
/* PUNSUBSCRIBE */
void RedisSelect::punsubscribe(const std::string &channelName)
{
/*
* Send PUNSUBSCRIBE #channel command on the
* non-blocking subscriber DBConnector
*/
if (m_subscribe)
{
m_subscribe->punsubscribe(channelName);
}
}
void RedisSelect::setQueueLength(long long int queueLength)
{
m_queueLength = queueLength;
}
}