-
Notifications
You must be signed in to change notification settings - Fork 938
/
redis_result.cpp
323 lines (279 loc) · 5.93 KB
/
redis_result.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/stdlib/dbuf_pool.hpp"
#include "acl_cpp/redis/redis_result.hpp"
#endif
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
namespace acl {
redis_result::redis_result(dbuf_pool* dbuf)
: result_type_(REDIS_RESULT_NIL)
, dbuf_(dbuf)
, size_(0)
, idx_(0)
, argv_(NULL)
, lens_(NULL)
, children_(NULL)
, children_size_(10)
, children_idx_(0)
{
acl_assert(dbuf_ != NULL);
}
redis_result::~redis_result()
{
}
void *redis_result::operator new(size_t size, dbuf_pool* dbuf)
{
void* ptr = dbuf->dbuf_alloc(size);
return ptr;
}
void redis_result::operator delete(void* ptr acl_unused,
dbuf_pool* dbuf acl_unused)
{
logger_error("DELETE NOW!");
}
void redis_result::clear()
{
children_ = NULL;
children_idx_ = 0;
}
redis_result& redis_result::set_size(size_t size)
{
if (idx_ > 0) {
logger_error("set size when putting, idx_: %d", (int) idx_);
return *this;
}
size_ = size;
return *this;
}
redis_result& redis_result::set_type(redis_result_t type)
{
result_type_ = type;
return *this;
}
redis_result& redis_result::put(const char* buf, size_t len)
{
if (size_ == 0) {
logger_error("size_ is 0, call set_size first!");
return *this;
}
if (idx_ >= size_) {
logger_error("overflow, idx_(%d) >= size_(%d)",
(int) idx_, (int) size_);
return *this;
}
if (argv_ == NULL) {
argv_ = (const char**) dbuf_->dbuf_alloc(sizeof(char*) * size_);
lens_ = (size_t*) dbuf_->dbuf_alloc(sizeof(size_t) * size_);
}
argv_[idx_] = buf;
lens_[idx_] = len;
idx_++;
return *this;
}
size_t redis_result::get_size() const
{
if (result_type_ == REDIS_RESULT_ARRAY) {
return children_idx_;
} else if (result_type_ == REDIS_RESULT_STRING) {
if (argv_ == NULL || lens_ == NULL)
return 0;
return size_;
} else {
return size_;
}
}
int redis_result::get_integer(bool* success /* = NULL */) const
{
if (success) {
*success = false;
}
if (result_type_ != REDIS_RESULT_INTEGER) {
return -1;
}
const char* ptr = get(0);
if (ptr == NULL || *ptr == 0) {
return -1;
}
if (success) {
*success = true;
}
return atoi(ptr);
}
long long int redis_result::get_integer64(bool* success /* = NULL */) const
{
if (success) {
*success = false;
}
if (result_type_ != REDIS_RESULT_INTEGER) {
return -1;
}
const char* ptr = get(0);
if (ptr == NULL || *ptr == 0) {
return -1;
}
if (success) {
*success = true;
}
return acl_atoi64(ptr);
}
double redis_result::get_double(bool* success /* = NULL */) const
{
if (success) {
*success = false;
}
if (result_type_ != REDIS_RESULT_STRING) {
return -1;
}
const char* ptr = get(0);
if (ptr == NULL || *ptr == 0) {
return -1;
}
if (success) {
*success = true;
}
return atof(ptr);
}
const char* redis_result::get_status() const
{
if (result_type_ != REDIS_RESULT_STATUS) {
return "";
}
const char* ptr = get(0);
return ptr == NULL ? "" : ptr;
}
const char* redis_result::get_error() const
{
if (result_type_ != REDIS_RESULT_ERROR) {
return "";
}
const char* ptr = get(0);
return ptr == NULL ? "" : ptr;
}
const char* redis_result::get(size_t i, size_t* len /* = NULL */) const
{
if (i >= idx_) {
if (len) {
*len = 0;
}
return NULL;
}
if (len) {
*len = lens_[i];
}
return argv_[i];
}
size_t redis_result::get_length() const
{
if (lens_ == NULL) {
return 0;
}
size_t len = 0;
for (size_t i = 0; i < idx_; i++) {
len += lens_[i];
}
return len;
}
int redis_result::argv_to_string(string& buf, bool clear_auto /* = true */) const
{
if (clear_auto) {
buf.clear();
}
if (idx_ == 0) {
return 0;
}
int length = 0;
for (size_t i = 0; i < idx_; i++) {
buf.append(argv_[i], lens_[i]);
length += (int) lens_[i];
}
return length;
}
int redis_result::argv_to_string(char* buf, size_t size) const
{
if (idx_ == 0 || size == 0) {
return 0;
}
size--;
if (size == 0) {
return 0;
}
char* ptr = buf;
int length = 0;
size_t n;
for (size_t i = 0; i < idx_; i++) {
n = size > lens_[i] ? lens_[i] : size;
memcpy(ptr, argv_[i], n);
ptr += n;
size -= n;
length += (int) n;
if (size == 0) {
break;
}
}
*ptr = 0;
return length;
}
redis_result& redis_result::put(const redis_result* rr, size_t idx)
{
if (children_ == NULL) {
children_ = (const redis_result**) dbuf_->dbuf_calloc(
sizeof(redis_result*) * children_size_);
} else if (idx == 0) {
children_idx_ = 0;
}
// +1 是为了确保最后一个数组元素可以被设为 NULL
if (children_idx_ + 1 < children_size_) {
children_[children_idx_++] = rr;
return *this;
}
children_size_ *= 2;
const redis_result** children =(const redis_result**)
dbuf_->dbuf_calloc(sizeof(redis_result*) * children_size_);
for (size_t i = 0; i < children_idx_; i++) {
children[i] = children_[i];
}
children_ = children;
children_[children_idx_++] = rr;
return *this;
}
const redis_result* redis_result::get_child(size_t i) const
{
if (children_ == NULL || i >= children_idx_) {
return NULL;
}
return children_[i];
}
const redis_result** redis_result::get_children(size_t* size) const
{
if (size) {
*size = children_idx_;
}
return children_;
}
const string& redis_result::to_string(string& out) const
{
redis_result_t type = get_type();
if (type != REDIS_RESULT_ARRAY) {
string buf;
argv_to_string(buf);
out += buf;
out += "\r\n";
return out;
}
size_t size;
const redis_result** children = get_children(&size);
if (children == NULL) {
return out;
}
for (size_t i = 0; i < size; i++) {
const redis_result* rr = children[i];
if (rr != NULL) {
rr->to_string(out);
}
}
return out;
}
} // namespace acl
#endif // ACL_CLIENT_ONLY