-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_zset_client.hpp
More file actions
492 lines (397 loc) · 15.8 KB
/
cpp_zset_client.hpp
File metadata and controls
492 lines (397 loc) · 15.8 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#ifndef cpp_zset_client_h__
#define cpp_zset_client_h__
#include "client_interface.hpp"
namespace cpp_redis {
class zset_client :public client {
public:
zset_client() = default;
virtual ~zset_client() {
}
//score 只能为double,int float
int zset_add(std::vector<std::string>&&keys) {
check_args();
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::zset_add),std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code()!= status::int_result_&&
res->get_result_code() != status::status_){
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual std::string zset_score(std::string&& key,std::string&& member)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_score), std::forward<std::string>(key),std::forward<std::string>(member));
if (!socket_->send_msg(std::move(msg))) {
return "";
}
const auto res = socket_->get_responese();
if (res->get_result_code()!=status::results_&&
res->get_result_code() != status::status_){
return "";
}
const auto results = res->get_results();
if (results.empty()){
return "";
}
return ((results[0] == g_nil) ? "" :std::move(results[0]));
}
virtual std::string zset_incrby(std::string&& key,std::string&& increment,std::string&& member)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_incrby),std::forward<std::string>(key),
std::forward<std::string>(increment),std::forward<std::string>(member));
if (!socket_->send_msg(std::move(msg))) {
return "";
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_&&
res->get_result_code() != status::status_) {
return "";
}
const auto results = res->get_results();
if (results.empty()) {
return "";
}
return ((results[0] == g_nil) ? "" : std::move(results[0]));
}
virtual int zset_card(std::string&& key)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_card), std::forward<std::string>(key));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int zset_count(std::string&& key, std::string&& min, std::string&& max)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_count),
std::forward<std::string>(key),std::forward<std::string>(min), std::forward<std::string>(max));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
//可以通过使用 WITHSCORES 选项,来让成员和它的 score 值一并返回,
//返回列表以 value1, score1, ..., valueN, scoreN 的格式表示。
//客户端库可能会返回一些更复杂的数据类型,比如数组、元组等。
//递增排列
virtual std::vector <std::string> zset_range(std::string&& key, std::string&& begin, std::string&& end,bool with_scores)
{
check_args();
std::string msg;
if (with_scores){
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range), std::forward<std::string>(key),
std::forward<std::string>(begin), std::forward<std::string>(end),"WITHSCORES"); //WITHSCORES 选项
}else{
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range), std::forward<std::string>(key),
std::forward<std::string>(begin), std::forward<std::string>(end)); //WITHSCORES 选项
}
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code()!= status::results_&&
res->get_result_code() != status::status_){
return {} ;
}
return std::move(res->get_results());
}
//可以通过使用 WITHSCORES 选项,来让成员和它的 score 值一并返回,
//返回列表以 value1, score1, ..., valueN, scoreN 的格式表示。
//客户端库可能会返回一些更复杂的数据类型,比如数组、元组等。
//递减排列
virtual std::vector <std::string> zset_revrange(std::string&& key, std::string&& begin, std::string&& end, bool with_scores)
{
check_args();
std::string msg;
if (with_scores) {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_revrange), std::forward<std::string>(key),
std::forward<std::string>(begin), std::forward<std::string>(end), "WITHSCORES"); //WITHSCORES 选项
}
else {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_revrange), std::forward<std::string>(key),
std::forward<std::string>(begin), std::forward<std::string>(end)); //WITHSCORES 选项
}
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_&&
res->get_result_code() != status::status_) {
return {} ;
}
return std::move(res->get_results());
}
//求区间内的score
virtual std::vector <std::string> zset_range_score(std::string&& key, std::string&& min, std::string&& max,
bool with_scores, bool limit, std::string&& limit_min, std::string &&limit_max)
{
check_args();
std::string msg;
if (with_scores) {
if (limit){
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range_score), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max), "WITHSCORES", "LIMIT",//WITHSCORES 选项
std::forward<std::string>(limit_min), std::forward<std::string>(limit_max));//limit表示范围
}else{
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range_score), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max), "WITHSCORES"); //WITHSCORES 选项
}
}else {
if (limit) {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range_score), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max), "LIMIT",
std::forward<std::string>(limit_min), std::forward<std::string>(limit_max));//limit表示范围
}else {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_range_score), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max));
}
}
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_&&
res->get_result_code() != status::status_) {
return {} ;
}
return std::move(res->get_results());
}
virtual std::vector <std::string> zset_revrange_score(std::string&& key, std::string&& max, std::string&& min,
bool with_scores, bool limit, std::string&& limit_min, std::string&& limit_max)
{
check_args();
std::string msg;
if (with_scores) {
if (limit) {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rerange_score), std::forward<std::string>(key),
std::forward<std::string>(max), std::forward<std::string>(min), "WITHSCORES", "LIMIT",//WITHSCORES 选项
std::forward<std::string>(limit_min), std::forward<std::string>(limit_max));//limit表示范围
}else {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rerange_score), std::forward<std::string>(key),
std::forward<std::string>(max), std::forward<std::string>(min), "WITHSCORES"); //WITHSCORES 选项
}
}
else {
if (limit) {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rerange_score), std::forward<std::string>(key),
std::forward<std::string>(max), std::forward<std::string>(min), "LIMIT",
std::forward<std::string>(limit_min), std::forward<std::string>(limit_max));//limit表示范围
}else {
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rerange_score), std::forward<std::string>(key),
std::forward<std::string>(max), std::forward<std::string>(min));
}
}
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_&&
res->get_result_code() != status::status_) {
return {};
}
return std::move(res->get_results());
}
//升序,从0开始
virtual int zset_rank(std::string&& key, std::string&& member)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rank),std::forward<std::string>(key),std::forward<std::string>(member));
if (!socket_->send_msg(std::move(msg))) {
return -1;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return -1;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : -1);
}
//降序,从0开始,到从大的开始排
virtual int zset_revrank(std::string&& key, std::string&& member)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_revrank),std::forward<std::string>(key),std::forward<std::string>(member));
if (!socket_->send_msg(std::move(msg))) {
return -1;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return -1;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : -1);
}
virtual bool zset_rem(std::vector<std::string>&&keys)
{
check_args();
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::zset_rem),std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return false;
}
const auto res = socket_->get_responese();
if (res->get_result_code() !=status::int_result_&&
res->get_result_code() != status::status_){
return false;
}
const auto results = res->get_int_results();
if (results.empty()){
return false;
}
return results[0]>0 ?true:false;
}
//移除有序集 key 中,指定排名(rank)区间内的所有成员
virtual int zset_remrangeby_rank(std::string&& key, std::string&& begin, std::string&& end)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_remrangeby_rank),std::forward<std::string>(key),std::forward<std::string>(begin),
std::forward<std::string>(end));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return false;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int zset_remrangebyscore(std::string&& key, std::string&& min, std::string&& max)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_remrangebyscore), std::forward<std::string>(key), std::forward<std::string>(min),
std::forward<std::string>(max));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_&&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int zset_union_store(std::vector<std::string>&& keys, aggregate_mothod mothod)
{
if (mothod >aggregate_mothod::agg_none &&
mothod <=aggregate_mothod::agg_max) {
keys.emplace_back("AGGREGATE");
keys.emplace_back(request_->get_aggregate_mothod(mothod));
}
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::zset_union_store),
std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_){
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
virtual int zset_inter_store(std::vector<std::string>&& keys, aggregate_mothod mothod)
{
if (mothod > aggregate_mothod::agg_none&&
mothod <= aggregate_mothod::agg_max) {
keys.emplace_back("AGGREGATE");
keys.emplace_back(request_->get_aggregate_mothod(mothod));
}
std::string msg = request_->req_n_keys(request_->get_cmd(redis_cmd::zset_inter_store),
std::forward<std::vector<std::string>>(keys));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
/**************************************************************以下几个接口不常用************************************************************************/
//合法的 min 和 max 参数必须包含(或者[, 其中(表示开区间(指定的值不会被包含在范围之内), 而[则表示闭区间(指定的值会被包含在范围之内)。
//特殊值 + 和 - 在 min 参数以及 max 参数中具有特殊的意义, 其中 + 表示正无限, 而 - 表示负无限。
//因此, 向一个所有成员的分值都相同的有序集合发送命令 ZRANGEBYLEX <zset> -+, 命令将返回有序集合中的所有元素。
virtual std::vector <std::string> zset_rangebylex(std::string&& key, std::string&& min,
std::string&& max, bool limit, std::string&& limit_min, std::string&& limit_max)
{
check_args();
std::string msg;
if (limit){
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rangebylex), std::forward<std::string>(key),
std::forward<std::string>(min),std::forward<std::string>(max),std::forward<std::string>(limit_min),std::forward<std::string>(limit_max));
}else{
msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_rangebylex), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max));
}
if (!socket_->send_msg(std::move(msg))) {
return {};
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::results_ &&
res->get_result_code() != status::status_) {
return {};
}
return std::move(res->get_results());
}
virtual int zset_lexcount(std::string&& key, std::string&& min, std::string&& max)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_lexcount),std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ?results[0]: 0);
}
virtual int zset_remrangebylex(std::string&& key, std::string&& min, std::string&& max)
{
check_args();
std::string msg = request_->req_n_key(request_->get_cmd(redis_cmd::zset_remrangebylex), std::forward<std::string>(key),
std::forward<std::string>(min), std::forward<std::string>(max));
if (!socket_->send_msg(std::move(msg))) {
return 0;
}
const auto res = socket_->get_responese();
if (res->get_result_code() != status::int_result_ &&
res->get_result_code() != status::status_) {
return 0;
}
const auto results = res->get_int_results();
return ((!results.empty()) ? results[0] : 0);
}
};
}//cpp_redis
#endif // cpp_zset_client_h__