command.cpp
4.45 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Redis C++11 wrapper.
*/
#include <vector>
#include <set>
#include <unordered_set>
#include "command.hpp"
#include "redox.hpp"
namespace redox {
template<class ReplyT>
bool Command<ReplyT>::is_error_reply() {
if (reply_obj->type == REDIS_REPLY_ERROR) {
logger.error() << cmd << ": " << reply_obj->str;
return true;
}
return false;
}
template<class ReplyT>
bool Command<ReplyT>::is_nil_reply() {
if (reply_obj->type == REDIS_REPLY_NIL) {
logger.warning() << cmd << ": Nil reply.";
return true;
}
return false;
}
template<>
void Command<redisReply*>::invoke_callback() {
invoke(reply_obj);
}
template<>
void Command<std::string>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_STRING && reply_obj->type != REDIS_REPLY_STATUS) {
logger.error() << cmd << ": Received non-string reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
std::string s(reply_obj->str, reply_obj->len);
invoke(s);
}
}
template<>
void Command<char*>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_STRING && reply_obj->type != REDIS_REPLY_STATUS) {
logger.error() << cmd << ": Received non-string reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(reply_obj->str);
}
}
template<>
void Command<int>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_INTEGER) {
logger.error() << cmd << ": Received non-integer reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke((int) reply_obj->integer);
}
}
template<>
void Command<long long int>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(is_nil_reply()) invoke_error(REDOX_NIL_REPLY);
else if(reply_obj->type != REDIS_REPLY_INTEGER) {
logger.error() << cmd << ": Received non-integer reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(reply_obj->integer);
}
}
template<>
void Command<std::nullptr_t>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_NIL) {
logger.error() << cmd << ": Received non-nil reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
invoke(nullptr);
}
}
template<>
void Command<std::vector<std::string>>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_ARRAY) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
std::vector<std::string> v;
size_t count = reply_obj->elements;
for(size_t i = 0; i < count; i++) {
redisReply* r = *(reply_obj->element + i);
if(r->type != REDIS_REPLY_STRING) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
}
v.emplace_back(r->str, r->len);
}
invoke(v);
}
}
template<>
void Command<std::unordered_set<std::string>>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_ARRAY) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
std::unordered_set<std::string> v;
size_t count = reply_obj->elements;
for(size_t i = 0; i < count; i++) {
redisReply* r = *(reply_obj->element + i);
if(r->type != REDIS_REPLY_STRING) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
}
v.emplace(r->str, r->len);
}
invoke(v);
}
}
template<>
void Command<std::set<std::string>>::invoke_callback() {
if(is_error_reply()) invoke_error(REDOX_ERROR_REPLY);
else if(reply_obj->type != REDIS_REPLY_ARRAY) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
} else {
std::set<std::string> v;
size_t count = reply_obj->elements;
for(size_t i = 0; i < count; i++) {
redisReply* r = *(reply_obj->element + i);
if(r->type != REDIS_REPLY_STRING) {
logger.error() << cmd << ": Received non-array reply.";
invoke_error(REDOX_WRONG_TYPE);
}
v.emplace(r->str, r->len);
}
invoke(v);
}
}
} // End namespace redox