forked from eyjian/r3c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r3c_cmd.cpp
1212 lines (1134 loc) · 41.4 KB
/
r3c_cmd.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016, Jian Yi <eyjian at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "r3c.h"
#include <inttypes.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
static void my_log_write(const char* format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
fprintf(stderr, "Usage: r3c_cmd command parameter ...\n");
exit(1);
}
r3c::set_info_log_write(my_log_write);
r3c::set_debug_log_write(my_log_write);
try
{
const char* cmd = argv[1];
const char* key = argv[2];
const char* nodes = getenv("H");
if (NULL == nodes)
{
fprintf(stderr, "Environment[H] not set, example: export H=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381\n");
exit(1);
}
if ('\0' == *nodes)
{
fprintf(stderr, "Environment[H] without value, example: export H=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381\n");
exit(1);
}
const char* colon = strchr(nodes, ':');
if (NULL == colon)
{
fprintf(stderr, "Environment[H] with error value, example: export H=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381\n");
exit(1);
}
int i = 0, j=0;
int ret = 0;
int offset = 0;
int count = 0;
int end = 0;
int start = 0;
int64_t ret64 = 0;
int64_t cursor = 0;
int64_t min;
int64_t max;
int64_t increment;
int64_t new_value_int64;
int64_t result_int64;
uint32_t seconds;
std::string str;
std::string value;
std::vector<std::string> fields;
std::vector<std::string> values;
std::vector<std::pair<std::string, int64_t> > values2;
std::map<std::string, std::string> map;
std::map<std::string, std::string>::iterator iter;
std::vector<std::string> vec;
std::pair<std::string, uint16_t> which_node;
r3c::CRedisClient redis_client(nodes);
if (0 == strcasecmp(cmd, "sha1"))
{
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd sha1 string\n");
exit(1);
}
str = key;
fprintf(stdout, "%s\n", r3c::strsha1(str).c_str());
}
else if (0 == strcasecmp(cmd, "slot"))
{
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd slot key\n");
exit(1);
}
str = key;
unsigned int slot = r3c::get_key_slot(&str);
fprintf(stdout, "[%s] => %u\n", key, slot);
}
else if (0 == strcasecmp(cmd, "list"))
{
std::vector<struct r3c::NodeInfo> nodes_info;
int num_nodes = redis_client.list_nodes(&nodes_info, &which_node);
fprintf(stdout, "number of nodes (from %s:%d): %d\n", which_node.first.c_str(), which_node.second, num_nodes);
for (std::vector<struct r3c::NodeInfo>::size_type i=0; i<nodes_info.size(); ++i)
std::cout << "[" << i << "] " << nodes_info[i] << std::endl;
}
else if (0 == strcasecmp(cmd, "flushall"))
{
fprintf(stdout, "["PRINT_COLOR_YELLOW"NOTICE"PRINT_COLOR_NONE"] To clear only a node, set `HOSTS` to a single node\n\n");
std::vector<std::pair<std::string, std::string> > results;
redis_client.flushall(&results);
for (std::vector<std::pair<std::string, std::string> >::size_type i=0; i<results.size(); ++i)
fprintf(stdout, "[%s] %s\n", results[i].first.c_str(), results[i].second.c_str());
}
////////////////////////////////////////////////////////////////////////////
// KEY VALUE
else if (0 == strcasecmp(cmd, "type"))
{
// TYPE
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd type key\n");
exit(1);
}
std::string key_type;
if (!redis_client.key_type(key, &key_type, &which_node))
fprintf(stderr, "key[%s] not exists\n", key);
else
fprintf(stdout, "key[%s]: %s\n", key, key_type.c_str());
}
else if (0 == strcasecmp(cmd, "del"))
{
// DEL
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd del key\n");
exit(1);
}
if (!redis_client.del(key, &which_node))
fprintf(stderr, "[%s] not exist\n", key);
else
fprintf(stdout, "[%s] deleted\n", key);
}
else if (0 == strcasecmp(cmd, "exists"))
{
// EXISTS command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd exists key\n");
exit(1);
}
if (redis_client.exists(key, &which_node))
fprintf(stdout, "[%s] exist\n", key);
else
fprintf(stderr, "[%s] not exist\n", key);
}
else if (0 == strcasecmp(cmd, "expire"))
{
// EXPIRE command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd expire key seconds\n");
exit(1);
}
seconds = atoi(argv[3]);
if (redis_client.expire(key, seconds, &which_node))
fprintf(stdout, "[%s] exist\n", key);
else
fprintf(stderr, "[%s] not exist\n", key);
}
else if ((0 == strcasecmp(cmd, "eval")) || (0 == strcasecmp(cmd, "evalsha")))
{
// EVAL/EVALSHA command
if (argc < 4)
{
if (0 == strcasecmp(cmd, "eval"))
{
fprintf(stderr, "Usage1: r3c_cmd EVAL key lua_scripts\n");
fprintf(stderr, "Usage1: r3c_cmd EVAL key lua_scripts parameter1 parameter2 ...\n");
}
else
{
fprintf(stderr, "Usage1: r3c_cmd EVALSHA key sha1\n");
fprintf(stderr, "Usage1: r3c_cmd EVALSHA key sha1 parameter1 parameter2 ...\n");
}
exit(1);
}
if (4 == argc)
{
const char* lua_scripts_or_sha1 = argv[3];
if (0 == strcasecmp(cmd, "eval"))
{
const r3c::RedisReplyHelper reply = redis_client.eval(key, lua_scripts_or_sha1, &which_node);
if (reply)
std::cout << reply;
}
}
else
{
const char* lua_scripts_or_sha1 = argv[3];
std::vector<std::string> parameters(argc-4);
for (int i=4; i<argc; ++i)
parameters[i-4] = argv[i];
if (0 == strcasecmp(cmd, "eval"))
{
const r3c::RedisReplyHelper reply = redis_client.eval(key, lua_scripts_or_sha1, parameters, &which_node);
if (reply)
std::cout << reply;
}
else
{
const r3c::RedisReplyHelper reply = redis_client.evalsha(key, lua_scripts_or_sha1, parameters, &which_node);
if (reply)
std::cout << reply;
}
}
}
else if (0 == strcasecmp(cmd, "ttl"))
{
// TTL command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd ttl key\n");
exit(1);
}
int ttl = redis_client.ttl(key, &which_node);
if (ttl >= 0)
fprintf(stdout, "[%s] %d\n", key, ttl);
else if (-1 == ttl)
fprintf(stderr, "[%s] no associated expire\n", key);
else if (-2 == ttl)
fprintf(stderr, "[%s] not exist\n", key);
else
fprintf(stderr, "unknown error\n");
}
else if (0 == strcasecmp(cmd, "set"))
{
// SET command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd set key value\n");
exit(1);
}
redis_client.set(key, argv[3], &which_node);
}
else if (0 == strcasecmp(cmd, "setnx"))
{
// SETNX command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd setnx key value\n");
exit(1);
}
if (redis_client.setnx(key, argv[3], &which_node))
fprintf(stdout, "[%s] ok\n", key);
else
fprintf(stderr, "[%s] exists\n", key);
}
else if (0 == strcasecmp(cmd, "setex"))
{
// SETEX command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd setex key seconds value\n");
exit(1);
}
seconds = atoi(argv[3]);
redis_client.setex(key, argv[4], seconds, &which_node);
fprintf(stdout, "[%s] ok\n", key);
}
else if (0 == strcasecmp(cmd, "setnxex"))
{
// SETNXEX command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd setnxex key seconds value\n");
exit(1);
}
seconds = atoi(argv[3]);
if (redis_client.setnxex(key, argv[4], seconds, &which_node))
fprintf(stdout, "[%s] ok\n", key);
else
fprintf(stdout, "[%s] exists\n", key);
}
else if (0 == strcasecmp(cmd, "get"))
{
// GET command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd get key\n");
exit(1);
}
if (!redis_client.get(key, &value, &which_node))
{
fprintf(stderr, "[%s] not exist\n", key);
}
else
{
fprintf(stdout, "[%s] => %s\n", key, value.c_str());
}
}
else if (0 == strcasecmp(cmd, "incrby"))
{
// INCRBY command
if ((argc != 4) && (argc != 5))
{
fprintf(stderr, "Usage1: r3c_cmd incrby key increment\n");
fprintf(stderr, "Usage2: r3c_cmd incrby key increment timeout_seconds\n");
exit(1);
}
increment = atoll(argv[3]);
if (4 == argc)
{
new_value_int64 = redis_client.incrby(key, increment, &which_node);
}
else
{
seconds = static_cast<uint32_t>(atoll(argv[4]));
new_value_int64 = redis_client.incrby(key, increment, increment, seconds, &which_node);
}
fprintf(stdout, "%" PRId64"\n", new_value_int64);
}
else if (0 == strcasecmp(cmd, "scan"))
{
// SCAN command
if ((argc < 3) || (argc > 5))
{
fprintf(stderr, "Usage1: r3c_cmd scan cursor\n");
fprintf(stderr, "Usage2: r3c_cmd scan cursor count\n");
fprintf(stderr, "Usage3: r3c_cmd scan cursor pattern\n");
fprintf(stderr, "Usage4: r3c_cmd scan cursor pattern count\n");
exit(1);
}
cursor = static_cast<int64_t>(atoll(argv[2]));
if (3 == argc)
{
cursor = redis_client.scan(cursor, &values, &which_node);
}
else if (4 == argc)
{
count = atoi(argv[3]);
if (count > 0)
cursor = redis_client.scan(cursor, count, &values, &which_node);
else
cursor = redis_client.scan(cursor, argv[3], &values, &which_node);
}
else if (5 == argc)
{
cursor = redis_client.scan(cursor, argv[2], atoi(argv[3]), &values, &which_node);
}
fprintf(stdout, "cursor: %" PRId64", count: %d\n", cursor, static_cast<int>(values.size()));
for (i=0; i<static_cast<int>(values.size()); ++i)
fprintf(stdout, "%s\n", values[i].c_str());
}
////////////////////////////////////////////////////////////////////////////
// LIST
else if (0 == strcasecmp(cmd, "llen"))
{
// LLEN command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd llen key\n");
exit(1);
}
result_int64 = redis_client.llen(key, &which_node);
fprintf(stdout, "[%s] => %" PRId64"\n", key, result_int64);
}
else if (0 == strcasecmp(cmd, "lpop"))
{
// LPOP command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd lpop key\n");
exit(1);
}
if (redis_client.lpop(key, &value, &which_node))
fprintf(stdout, "[%s] => %s\n", key, value.c_str());
else
fprintf(stdout, "empty\n");
}
else if (0 == strcasecmp(cmd, "lpush"))
{
// LPUSH command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd lpush key value1 value2 ...\n");
exit(1);
}
values.clear();
for (i=3; i<argc; ++i)
values.push_back(argv[i]);
count = redis_client.lpush(key, values, &which_node);
fprintf(stdout, "[%s] => %d\n", key, count);
}
else if (0 == strcasecmp(cmd, "lrange"))
{
// LRANGE command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd lrange key start end\n");
exit(1);
}
end = atoi(argv[4]);
start = atoi(argv[3]);
count = redis_client.lrange(key, start, end, &values, &which_node);
fprintf(stdout, "count: %d\n", count);
for (i=0; i<count; ++i)
fprintf(stdout, "%s\n", values[i].c_str());
}
else if (0 == strcasecmp(cmd, "ltrim"))
{
// LTRIM command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd ltrim key start end\n");
exit(1);
}
end = atoi(argv[4]);
start = atoi(argv[3]);
if (redis_client.ltrim(key, start, end, &which_node))
fprintf(stdout, "OK\n");
else
fprintf(stderr, "ERROR\n");
}
else if (0 == strcasecmp(cmd, "rpop"))
{
// RPOP command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd rpop key\n");
exit(1);
}
if (redis_client.rpop(key, &value, &which_node))
fprintf(stdout, "%s\n", value.c_str());
else
fprintf(stderr, "empty\n");
}
else if (0 == strcasecmp(cmd, "rpush"))
{
// RPUSH command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd rpush key value1 value2 ...\n");
exit(1);
}
if (4 == argc)
{
count = redis_client.rpush(key, argv[3], &which_node);
}
else
{
values.clear();
for (i=3; i<argc; ++i)
values.push_back(argv[i]);
count = redis_client.rpush(key, values, &which_node);
}
fprintf(stdout, "[%s] => %d\n", key, count);
}
else if (0 == strcasecmp(cmd, "rpushx"))
{
// RPUSHX command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd rpushx key value\n");
exit(1);
}
count = redis_client.rpushx(key, argv[3], &which_node);
fprintf(stdout, "[%s] => %d\n", key, count);
}
////////////////////////////////////////////////////////////////////////////
// HASH
else if (0 == strcasecmp(cmd, "hdel"))
{
// HDEL command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd hdel key field1 field2 ...\n");
exit(1);
}
if (4 == argc)
{
if (redis_client.hdel(key, argv[3], &which_node))
fprintf(stdout, "[%s] deleted\n", key);
else
fprintf(stderr, "[%s] not exists\n", key);
}
else
{
std::vector<std::string> fields;
for (i=3; i<argc; ++i)
fields.push_back(argv[i]);
count = redis_client.hmdel(key, fields, &which_node);
if (count > 0)
fprintf(stdout, "[%s] deleted: %d\n", key, count);
else
fprintf(stderr, "[%s] not exists\n", key);
}
}
else if (0 == strcasecmp(cmd, "hexists"))
{
// HEXISTS command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd hexists key field\n");
exit(1);
}
if (redis_client.hexists(key, argv[3], &which_node))
fprintf(stdout, "[%s:%s] exist\n", key, argv[3]);
else
fprintf(stderr, "[%s:%s] not exist\n", key, argv[3]);
}
else if (0 == strcasecmp(cmd, "hlen"))
{
// HLEN command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd hlen key\n");
exit(1);
}
new_value_int64 = redis_client.hlen(key, &which_node);
fprintf(stdout, "[%s] fields count: %" PRId64"\n", key, new_value_int64);
}
else if (0 == strcasecmp(cmd, "hset"))
{
// HSET command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd hset key field value\n");
exit(1);
}
redis_client.hset(key, argv[3], argv[4], &which_node);
}
else if (0 == strcasecmp(cmd, "hsetex"))
{
// HSET command
if (argc != 6)
{
fprintf(stderr, "Usage: r3c_cmd hsetex key field value timeout_seconds\n");
exit(1);
}
seconds = static_cast<uint32_t>(atoll(argv[5]));
redis_client.hsetex(key, argv[3], argv[4], seconds, &which_node);
}
else if (0 == strcasecmp(cmd, "hsetnx"))
{
// HSETNX command
if (argc != 5)
{
fprintf(stderr, "Usage: r3c_cmd hsetnx key field value\n");
exit(1);
}
if (redis_client.hsetnx(key, argv[3], argv[4], &which_node))
fprintf(stdout, "[%s:%s] ok\n", key, argv[3]);
else
fprintf(stderr, "[%s:%s] exists\n", key, argv[3]);
}
else if (0 == strcasecmp(cmd, "hsetnxex"))
{
// HSETNX command
if (argc != 6)
{
fprintf(stderr, "Usage: r3c_cmd hsetnxex key field value timeout_seconds\n");
exit(1);
}
seconds = static_cast<uint32_t>(atoll(argv[5]));
if (redis_client.hsetnxex(key, argv[3], argv[4], seconds, &which_node))
fprintf(stdout, "[%s:%s] ok\n", key, argv[3]);
else
fprintf(stderr, "[%s:%s] exists\n", key, argv[3]);
}
else if (0 == strcasecmp(cmd, "hget"))
{
// HGET command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd hget key field\n");
exit(1);
}
if (!redis_client.hget(key, argv[3], &value, &which_node))
{
fprintf(stdout, "[%s:%s] not exist\n", key, argv[3]);
}
else
{
fprintf(stdout, "[%s:%s] => %s\n", key, argv[3], value.c_str());
}
}
else if (0 == strcasecmp(cmd, "hincrby"))
{
// HINCRBY command
if ((argc < 5) || (argc%2 != 1))
{
fprintf(stderr, "Usage: r3c_cmd hincrby key field1 increment1 field2 increment2 ...\n");
exit(1);
}
if (5 == argc)
{
increment = atoll(argv[4]);
new_value_int64 = redis_client.hincrby(key, argv[3], increment, &which_node);
fprintf(stdout, "%" PRId64"\n", new_value_int64);
}
else
{
std::vector<std::pair<std::string, int64_t> > increments((argc-3)/2);
for (i=3,j=0; i<argc; i+=2,++j)
{
increments[j].first = argv[i];
increments[j].second = static_cast<int64_t>(atoll(argv[i+1]));
}
std::vector<int64_t> values;
redis_client.hmincrby(key, increments, &values, &which_node);
for (std::vector<int64_t>::size_type k=0; k<values.size(); ++k)
fprintf(stdout, "%" PRId64"\n", values[k]);
}
}
else if (0 == strcasecmp(cmd, "hmset"))
{
// HMSET command
if ((argc < 5) || (argc % 2 != 1))
{
fprintf(stderr, "Usage: r3c_cmd hmset key field1 value1 field2 value2 ...\n");
exit(1);
}
for (i=3; i<argc; i+=2)
map[argv[i]] = argv[i+1];
redis_client.hmset(key, map, &which_node);
}
else if (0 == strcasecmp(cmd, "hmget"))
{
// HMGET command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd hmget key field1 field2 ...\n");
exit(1);
}
for (i=3; i<argc; ++i)
vec.push_back(argv[i]);
const int x = redis_client.hmget(key, vec, &map, false, &which_node);
fprintf(stdout, "x: %d\n", x);
if (map.empty())
fprintf(stdout, "not exists\n");
else
for (iter=map.begin(); iter!=map.end(); ++iter)
fprintf(stdout, "%s => %s\n", iter->first.c_str(), iter->second.c_str());
}
else if (0 == strcasecmp(cmd, "hgetall"))
{
// HGETALL command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd hgetall key\n");
exit(1);
}
count = redis_client.hgetall(key, &map, &which_node);
if (0 == count)
{
fprintf(stderr, "[%s] not exists\n", key);
}
else
{
for (iter=map.begin(); iter!=map.end(); ++iter)
fprintf(stdout, "%s => %s\n", iter->first.c_str(), iter->second.c_str());
}
}
else if (0 == strcasecmp(cmd, "hkeys"))
{
// HKEYS command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd hkeys key\n");
exit(1);
}
count = redis_client.hkeys(key, &fields, &which_node);
if (0 == count)
{
fprintf(stderr, "[%s] not exists\n", key);
}
else
{
for (std::vector<std::string>::size_type k=0; k<fields.size(); ++k)
fprintf(stdout, "%s\n", fields[k].c_str());
}
}
else if (0 == strcasecmp(cmd, "hvals"))
{
// HVALS command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd hvals key\n");
exit(1);
}
count = redis_client.hvals(key, &values, &which_node);
if (0 == count)
{
fprintf(stderr, "[%s] not exists\n", key);
}
else
{
for (std::vector<std::string>::size_type k=0; k<values.size(); ++k)
fprintf(stdout, "%s\n", values[k].c_str());
}
}
else if (0 == strcasecmp(cmd, "hscan"))
{
// HSCAN command
if ((argc < 4) || (argc > 6))
{
fprintf(stderr, "Usage1: r3c_cmd hscan key cursor\n");
fprintf(stderr, "Usage2: r3c_cmd hscan key cursor count\n");
fprintf(stderr, "Usage3: r3c_cmd hscan key cursor pattern\n");
fprintf(stderr, "Usage4: r3c_cmd hscan key cursor pattern count\n");
exit(1);
}
if (4 == argc)
cursor = redis_client.hscan(key, atoll(argv[3]), &map, &which_node);
else if (6 == argc)
cursor = redis_client.hscan(key, atoll(argv[3]), argv[4], atoi(argv[5]), &map, &which_node);
else if (5 == argc)
{
count = atoi(argv[4]);
if (count > 0)
cursor = redis_client.hscan(key, atoll(argv[3]), count, &map, &which_node);
else
cursor = redis_client.hscan(key, atoll(argv[3]), argv[4], &map, &which_node);
}
fprintf(stdout, "cursor: %" PRId64", count: %d\n", cursor, static_cast<int>(map.size()));
for (iter=map.begin(); iter!=map.end(); ++iter)
fprintf(stdout, "%s => %s\n", iter->first.c_str(), iter->second.c_str());
}
////////////////////////////////////////////////////////////////////////////
// SET
else if (0 == strcasecmp(cmd, "sadd"))
{
// SADD command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd sadd key value1 value2 ...\n");
exit(1);
}
if (4 == argc)
{
count = redis_client.sadd(key, argv[3], &which_node);
}
else
{
for (i=3; i<argc; ++i)
values.push_back(argv[i]);
count = redis_client.sadd(key, values, &which_node);
}
fprintf(stdout, "%d\n", count);
}
else if (0 == strcasecmp(cmd, "scard"))
{
// SCARD command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd scard key\n");
exit(1);
}
count = redis_client.scard(key, &which_node);
fprintf(stdout, "%d\n", count);
}
else if (0 == strcasecmp(cmd, "sismember"))
{
// SISMEMBER command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd sismember key member\n");
exit(1);
}
if (redis_client.sismember(key, argv[3], &which_node))
fprintf(stdout, "YES\n");
else
fprintf(stdout, "NO\n");
}
else if (0 == strcasecmp(cmd, "smembers"))
{
// SMEMBERS command
if (argc != 3)
{
fprintf(stderr, "Usage: r3c_cmd smembers key\n");
exit(1);
}
count = redis_client.smembers(key, &values, &which_node);
for (i=0; i<count; ++i)
fprintf(stdout, "%s\n", values[i].c_str());
}
else if (0 == strcasecmp(cmd, "spop"))
{
// SPOP command
if ((argc != 3) && (argc != 4))
{
fprintf(stderr, "Usage1: r3c_cmd spop key\n");
fprintf(stderr, "Usage2: r3c_cmd spop key count\n");
exit(1);
}
if (3 == argc)
{
if (redis_client.spop(key, &value, &which_node))
fprintf(stdout, "%s\n", value.c_str());
else
fprintf(stdout, "empty\n");
}
else
{
count = redis_client.spop(key, atoi(argv[3]), &values, &which_node);
fprintf(stdout, "%d\n", count);
for (i=0; i<count; ++i)
{
fprintf(stdout, "%s\n", values[i].c_str());
}
}
}
else if (0 == strcasecmp(cmd, "srandmember"))
{
// SRANDMEMBER command
if (argc != 4)
{
fprintf(stderr, "Usage: r3c_cmd srandmember key count\n");
exit(1);
}
count = redis_client.srandmember(key, atoi(argv[3]), &values, &which_node);
for (i=0; i<count; ++i)
fprintf(stdout, "%s\n", values[i].c_str());
}
else if (0 == strcasecmp(cmd, "srem"))
{
// SREM command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd srem key member1 member2 ...\n");
exit(1);
}
if (4 == argc)
{
count = redis_client.srem(key, argv[3], &which_node);
}
else
{
for (i=3; i<argc; ++i)
values.push_back(argv[i]);
count = redis_client.srem(key, values, &which_node);
}
fprintf(stdout, "%d\n", count);
}
else if (0 == strcasecmp(cmd, "sscan"))
{
// SSCAN command
if ((argc < 4) || (argc > 6))
{
fprintf(stderr, "Usage1: r3c_cmd sscan key cursor\n");
fprintf(stderr, "Usage2: r3c_cmd sscan key cursor count\n");
fprintf(stderr, "Usage3: r3c_cmd sscan key cursor pattern\n");
fprintf(stderr, "Usage4: r3c_cmd sscan key cursor pattern count\n");
exit(1);
}
cursor = static_cast<int64_t>(atoll(argv[3]));
if (4 == argc)
{
cursor = redis_client.sscan(key, cursor, &values, &which_node);
}
else if (5 == argc)
{
count = atoi(argv[4]);
if (count > 0)
cursor = redis_client.sscan(key, cursor, count, &values, &which_node);
else
cursor = redis_client.sscan(key, cursor, argv[4], &values, &which_node);
}
else if (6 == argc)
{
cursor = redis_client.sscan(key, cursor, argv[3], atoi(argv[4]), &values, &which_node);
}
fprintf(stdout, "cursor: %" PRId64", count: %d\n", cursor, static_cast<int>(values.size()));
for (i=0; i<static_cast<int>(values.size()); ++i)
fprintf(stdout, "%s\n", values[i].c_str());
}
////////////////////////////////////////////////////////////////////////////
// SORTED SET
else if (0 == strcasecmp(cmd, "zrem"))
{
// ZREM command
if (argc < 4)
{
fprintf(stderr, "Usage: r3c_cmd zrem key field1 score2 field2 ...\n");
exit(1);
}
for (i=3; i<argc; ++i)
fields.push_back(argv[i]);
count = redis_client.zrem(key, fields, &which_node);
fprintf(stdout, "%d\n", count);
}
else if (0 == strcasecmp(cmd, "zadd"))
{
// ZADD command
if ((argc < 5) || (argc%2!=1))
{
fprintf(stderr, "Usage: r3c_cmd zadd key score1 field1 score2 field2 ...\n");
exit(1);
}
if (5 == argc)
{
count = redis_client.zadd(key, argv[4], atol(argv[3]), r3c::Z_NS, &which_node);
fprintf(stdout, "%d\n", count);
}
else
{
std::map<std::string, int64_t> map;
for (i=3; i<argc; i+=2)
map[argv[i+1]] = atol(argv[i]);
count = redis_client.zadd(key, map, r3c::Z_NS, &which_node);
fprintf(stdout, "%d\n", count);
}
}
else if (0 == strcasecmp(cmd, "zcard"))
{
// ZCARD command
if (argc != 3)