-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathredisselect.cpp
More file actions
100 lines (82 loc) · 2.13 KB
/
redisselect.cpp
File metadata and controls
100 lines (82 loc) · 2.13 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
#include <string>
#include <memory>
#include <hiredis/hiredis.h>
#include "dbconnector.h"
#include "redisreply.h"
#include "selectable.h"
#include "redisselect.h"
namespace swss {
RedisSelect::RedisSelect(int pri) : Selectable(pri), m_queueLength(-1)
{
}
int RedisSelect::getFd()
{
return m_subscribe->getContext()->fd;
}
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");
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");
}
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 */
std::string s("SUBSCRIBE ");
s += channelName;
RedisReply r(m_subscribe.get(), s, REDIS_REPLY_ARRAY);
}
/* 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
*/
std::string s("PSUBSCRIBE ");
s += channelName;
RedisReply r(m_subscribe.get(), s, REDIS_REPLY_ARRAY);
}
void RedisSelect::setQueueLength(long long int queueLength)
{
m_queueLength = queueLength;
}
}