forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pika_kv.cc
1501 lines (1381 loc) · 43.6 KB
/
pika_kv.cc
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) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_kv.h"
#include "pstd/include/pstd_string.h"
#include "include/pika_binlog_transverter.h"
#include "include/pika_conf.h"
#include "include/pika_slot_command.h"
extern std::unique_ptr<PikaConf> g_pika_conf;
/* SET key value [NX] [XX] [EX <seconds>] [PX <milliseconds>] */
void SetCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameSet);
return;
}
key_ = argv_[1];
value_ = argv_[2];
condition_ = SetCmd::kNONE;
sec_ = 0;
size_t index = 3;
while (index != argv_.size()) {
std::string opt = argv_[index];
if (strcasecmp(opt.data(), "xx") == 0) {
condition_ = SetCmd::kXX;
} else if (strcasecmp(opt.data(), "nx") == 0) {
condition_ = SetCmd::kNX;
} else if (strcasecmp(opt.data(), "vx") == 0) {
condition_ = SetCmd::kVX;
index++;
if (index == argv_.size()) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
} else {
target_ = argv_[index];
}
} else if ((strcasecmp(opt.data(), "ex") == 0) || (strcasecmp(opt.data(), "px") == 0)) {
condition_ = (condition_ == SetCmd::kNONE) ? SetCmd::kEXORPX : condition_;
index++;
if (index == argv_.size()) {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
if (pstd::string2int(argv_[index].data(), argv_[index].size(), &sec_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
if (strcasecmp(opt.data(), "px") == 0) {
sec_ /= 1000;
}
} else {
res_.SetRes(CmdRes::kSyntaxErr);
return;
}
index++;
}
}
void SetCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s;
int32_t res = 1;
switch (condition_) {
case SetCmd::kXX:
s = slot->db()->Setxx(key_, value_, &res, static_cast<int32_t>(sec_));
break;
case SetCmd::kNX:
s = slot->db()->Setnx(key_, value_, &res, static_cast<int32_t>(sec_));
break;
case SetCmd::kVX:
s = slot->db()->Setvx(key_, target_, value_, &success_, static_cast<int32_t>(sec_));
break;
case SetCmd::kEXORPX:
s = slot->db()->Setex(key_, value_, static_cast<int32_t>(sec_));
break;
default:
s = slot->db()->Set(key_, value_);
break;
}
if (s.ok() || s.IsNotFound()) {
if (condition_ == SetCmd::kVX) {
res_.AppendInteger(success_);
} else {
if (res == 1) {
res_.SetRes(CmdRes::kOk);
AddSlotKey("k", key_, slot);
} else {
res_.AppendStringLen(-1);
}
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
std::string SetCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
if (condition_ == SetCmd::kEXORPX) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLen(content, 4, "*");
// to pksetexat cmd
std::string pksetexat_cmd("pksetexat");
RedisAppendLenUint64(content, pksetexat_cmd.size(), "$");
RedisAppendContent(content, pksetexat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = static_cast<int32_t>(time(nullptr) + sec_);
pstd::ll2string(buf, 100, time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
// value
RedisAppendLenUint64(content, value_.size(), "$");
RedisAppendContent(content, value_);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
} else {
return Cmd::ToBinlog(exec_time, term_id, logic_id, filenum, offset);
}
}
void GetCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGet);
return;
}
key_ = argv_[1];
}
void GetCmd::Do(std::shared_ptr<Slot> slot) {
std::string value;
rocksdb::Status s = slot->db()->Get(key_, &value);
if (s.ok()) {
res_.AppendStringLenUint64(value.size());
res_.AppendContent(value);
} else if (s.IsNotFound()) {
res_.AppendStringLen(-1);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void DelCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, name());
return;
}
auto iter = argv_.begin();
keys_.assign(++iter, argv_.end());
}
void DelCmd::Do(std::shared_ptr<Slot> slot) {
std::map<storage::DataType, storage::Status> type_status;
int64_t count = slot->db()->Del(keys_, &type_status);
if (count >= 0) {
res_.AppendInteger(count);
std::vector<std::string>::const_iterator it;
for (it = keys_.begin(); it != keys_.end(); it++) {
RemSlotKey(*it, slot);
}
} else {
res_.SetRes(CmdRes::kErrOther, "delete error");
}
}
void DelCmd::Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) {
std::map<storage::DataType, storage::Status> type_status;
int64_t count = slot->db()->Del(hint_keys.keys, &type_status);
if (count >= 0) {
split_res_ += count;
} else {
res_.SetRes(CmdRes::kErrOther, "delete error");
}
}
void DelCmd::Merge() { res_.AppendInteger(split_res_); }
void DelCmd::DoBinlog(const std::shared_ptr<SyncMasterSlot>& slot) {
std::string opt = argv_.at(0);
for(auto& key: keys_) {
argv_.clear();
argv_.emplace_back(opt);
argv_.emplace_back(key);
Cmd::DoBinlog(slot);
}
}
void IncrCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameIncr);
return;
}
key_ = argv_[1];
}
void IncrCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Incrby(key_, 1, &new_value_);
if (s.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
AddSlotKey("k", key_, slot);
} else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") {
res_.SetRes(CmdRes::kInvalidInt);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void IncrbyCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameIncrby);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &by_) == 0) {
res_.SetRes(CmdRes::kInvalidInt, kCmdNameIncrby);
return;
}
}
void IncrbyCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Incrby(key_, by_, &new_value_);
if (s.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
AddSlotKey("k", key_, slot);
} else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") {
res_.SetRes(CmdRes::kInvalidInt);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void IncrbyfloatCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameIncrbyfloat);
return;
}
key_ = argv_[1];
value_ = argv_[2];
if (pstd::string2d(argv_[2].data(), argv_[2].size(), &by_) == 0) {
res_.SetRes(CmdRes::kInvalidFloat);
return;
}
}
void IncrbyfloatCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Incrbyfloat(key_, value_, &new_value_);
if (s.ok()) {
res_.AppendStringLenUint64(new_value_.size());
res_.AppendContent(new_value_);
AddSlotKey("k", key_, slot);
} else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a vaild float") {
res_.SetRes(CmdRes::kInvalidFloat);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::KIncrByOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void DecrCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameDecr);
return;
}
key_ = argv_[1];
}
void DecrCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Decrby(key_, 1, &new_value_);
if (s.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
} else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") {
res_.SetRes(CmdRes::kInvalidInt);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void DecrbyCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameDecrby);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &by_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
void DecrbyCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Decrby(key_, by_, &new_value_);
if (s.ok()) {
res_.AppendContent(":" + std::to_string(new_value_));
} else if (s.IsCorruption() && s.ToString() == "Corruption: Value is not a integer") {
res_.SetRes(CmdRes::kInvalidInt);
} else if (s.IsInvalidArgument()) {
res_.SetRes(CmdRes::kOverFlow);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void GetsetCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGetset);
return;
}
key_ = argv_[1];
new_value_ = argv_[2];
}
void GetsetCmd::Do(std::shared_ptr<Slot> slot) {
std::string old_value;
rocksdb::Status s = slot->db()->GetSet(key_, new_value_, &old_value);
if (s.ok()) {
if (old_value.empty()) {
res_.AppendContent("$-1");
} else {
res_.AppendStringLenUint64(old_value.size());
res_.AppendContent(old_value);
}
AddSlotKey("k", key_, slot);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void AppendCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameAppend);
return;
}
key_ = argv_[1];
value_ = argv_[2];
}
void AppendCmd::Do(std::shared_ptr<Slot> slot) {
int32_t new_len = 0;
rocksdb::Status s = slot->db()->Append(key_, value_, &new_len);
if (s.ok() || s.IsNotFound()) {
res_.AppendInteger(new_len);
AddSlotKey("k", key_, slot);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MgetCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMget);
return;
}
keys_ = argv_;
keys_.erase(keys_.begin());
split_res_.resize(keys_.size());
}
void MgetCmd::Do(std::shared_ptr<Slot> slot) {
std::vector<storage::ValueStatus> vss;
rocksdb::Status s = slot->db()->MGet(keys_, &vss);
if (s.ok()) {
res_.AppendArrayLenUint64(vss.size());
for (const auto& vs : vss) {
if (vs.status.ok()) {
res_.AppendStringLenUint64(vs.value.size());
res_.AppendContent(vs.value);
} else {
res_.AppendContent("$-1");
}
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MgetCmd::Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) {
std::vector<storage::ValueStatus> vss;
const std::vector<std::string>& keys = hint_keys.keys;
rocksdb::Status s = slot->db()->MGet(keys, &vss);
if (s.ok()) {
if (hint_keys.hints.size() != vss.size()) {
res_.SetRes(CmdRes::kErrOther, "internal Mget return size invalid");
}
const std::vector<int>& hints = hint_keys.hints;
for (size_t i = 0; i < vss.size(); ++i) {
split_res_[hints[i]] = vss[i];
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MgetCmd::Merge() {
res_.AppendArrayLenUint64(split_res_.size());
for (const auto& vs : split_res_) {
if (vs.status.ok()) {
res_.AppendStringLenUint64(vs.value.size());
res_.AppendContent(vs.value);
} else {
res_.AppendContent("$-1");
}
}
}
void KeysCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameKeys);
return;
}
pattern_ = argv_[1];
if (argv_.size() == 3) {
std::string opt = argv_[2];
if (strcasecmp(opt.data(), "string") == 0) {
type_ = storage::DataType::kStrings;
} else if (strcasecmp(opt.data(), "zset") == 0) {
type_ = storage::DataType::kZSets;
} else if (strcasecmp(opt.data(), "set") == 0) {
type_ = storage::DataType::kSets;
} else if (strcasecmp(opt.data(), "list") == 0) {
type_ = storage::DataType::kLists;
} else if (strcasecmp(opt.data(), "hash") == 0) {
type_ = storage::DataType::kHashes;
} else {
res_.SetRes(CmdRes::kSyntaxErr);
}
} else if (argv_.size() > 3) {
res_.SetRes(CmdRes::kSyntaxErr);
}
}
void KeysCmd::Do(std::shared_ptr<Slot> slot) {
int64_t total_key = 0;
int64_t cursor = 0;
size_t raw_limit = g_pika_conf->max_client_response_size();
std::string raw;
std::vector<std::string> keys;
do {
keys.clear();
cursor = slot->db()->Scan(type_, cursor, pattern_, PIKA_SCAN_STEP_LENGTH, &keys);
for (const auto& key : keys) {
RedisAppendLenUint64(raw, key.size(), "$");
RedisAppendContent(raw, key);
}
if (raw.size() >= raw_limit) {
res_.SetRes(CmdRes::kErrOther, "Response exceeds the max-client-response-size limit");
return;
}
total_key += static_cast<int64_t>(keys.size());
} while (cursor != 0);
res_.AppendArrayLen(total_key);
res_.AppendStringRaw(raw);
}
void SetnxCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameSetnx);
return;
}
key_ = argv_[1];
value_ = argv_[2];
}
void SetnxCmd::Do(std::shared_ptr<Slot> slot) {
success_ = 0;
rocksdb::Status s = slot->db()->Setnx(key_, value_, &success_);
if (s.ok()) {
res_.AppendInteger(success_);
AddSlotKey("k", key_, slot);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
std::string SetnxCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLen(content, 3, "*");
// don't check variable 'success_', because if 'success_' was false, an empty binlog will be saved into file.
// to setnx cmd
std::string set_cmd("setnx");
RedisAppendLenUint64(content, set_cmd.size(), "$");
RedisAppendContent(content, set_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// value
RedisAppendLenUint64(content, value_.size(), "$");
RedisAppendContent(content, value_);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}
void SetexCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameSetex);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &sec_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
value_ = argv_[3];
}
void SetexCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Setex(key_, value_, static_cast<int32_t>(sec_));
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
AddSlotKey("k", key_, slot);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
std::string SetexCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLen(content, 4, "*");
// to pksetexat cmd
std::string pksetexat_cmd("pksetexat");
RedisAppendLenUint64(content, pksetexat_cmd.size(), "$");
RedisAppendContent(content, pksetexat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = static_cast<int32_t>(time(nullptr) + sec_);
pstd::ll2string(buf, 100, time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
// value
RedisAppendLenUint64(content, value_.size(), "$");
RedisAppendContent(content, value_);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}
void PsetexCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePsetex);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &usec_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
value_ = argv_[3];
}
void PsetexCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Setex(key_, value_, static_cast<int32_t>(usec_ / 1000));
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
std::string PsetexCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLen(content, 4, "*");
// to pksetexat cmd
std::string pksetexat_cmd("pksetexat");
RedisAppendLenUint64(content, pksetexat_cmd.size(), "$");
RedisAppendContent(content, pksetexat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// time_stamp
char buf[100];
auto time_stamp = static_cast<int32_t>(time(nullptr) + usec_ / 1000);
pstd::ll2string(buf, 100, time_stamp);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
// value
RedisAppendLenUint64(content, value_.size(), "$");
RedisAppendContent(content, value_);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}
void DelvxCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameDelvx);
return;
}
key_ = argv_[1];
value_ = argv_[2];
}
void DelvxCmd::Do(std::shared_ptr<Slot> slot) {
rocksdb::Status s = slot->db()->Delvx(key_, value_, &success_);
if (s.ok() || s.IsNotFound()) {
res_.AppendInteger(success_);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MsetCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMset);
return;
}
size_t argc = argv_.size();
if (argc % 2 == 0) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMset);
return;
}
kvs_.clear();
for (size_t index = 1; index != argc; index += 2) {
kvs_.push_back({argv_[index], argv_[index + 1]});
}
}
void MsetCmd::Do(std::shared_ptr<Slot> slot) {
storage::Status s = slot->db()->MSet(kvs_);
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
std::vector<storage::KeyValue>::const_iterator it;
for (it = kvs_.begin(); it != kvs_.end(); it++) {
AddSlotKey("k", it->key, slot);
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MsetCmd::Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) {
std::vector<storage::KeyValue> kvs;
const std::vector<std::string>& keys = hint_keys.keys;
const std::vector<int>& hints = hint_keys.hints;
if (keys.size() != hints.size()) {
res_.SetRes(CmdRes::kErrOther, "SplitError hint_keys size not match");
}
for (size_t i = 0; i < keys.size(); i++) {
if (kvs_[hints[i]].key == keys[i]) {
kvs.push_back(kvs_[hints[i]]);
} else {
res_.SetRes(CmdRes::kErrOther, "SplitError hint key: " + keys[i]);
return;
}
}
storage::Status s = slot->db()->MSet(kvs);
if (s.ok()) {
res_.SetRes(CmdRes::kOk);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
return;
}
}
void MsetCmd::Merge() {}
void MsetCmd::DoBinlog(const std::shared_ptr<SyncMasterSlot>& slot) {
PikaCmdArgsType set_argv;
set_argv.resize(3);
//used "set" instead of "SET" to distinguish the binlog of Set
set_argv[0] = "set";
set_cmd_->SetConn(GetConn());
set_cmd_->SetResp(resp_.lock());
for(auto& kv: kvs_){
set_argv[1] = kv.key;
set_argv[2] = kv.value;
set_cmd_->Initial(set_argv, db_name_);
set_cmd_->DoBinlog(slot);
}
}
void MsetnxCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMsetnx);
return;
}
size_t argc = argv_.size();
if (argc % 2 == 0) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameMsetnx);
return;
}
kvs_.clear();
for (size_t index = 1; index != argc; index += 2) {
kvs_.push_back({argv_[index], argv_[index + 1]});
}
}
void MsetnxCmd::Do(std::shared_ptr<Slot> slot) {
success_ = 0;
rocksdb::Status s = slot->db()->MSetnx(kvs_, &success_);
if (s.ok()) {
res_.AppendInteger(success_);
std::vector<storage::KeyValue>::const_iterator it;
for (it = kvs_.begin(); it != kvs_.end(); it++) {
AddSlotKey("k", it->key, slot);
}
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void MsetnxCmd::DoBinlog(const std::shared_ptr<SyncMasterSlot>& slot) {
if(!success_){
//some keys already exist, set operations aborted, no need of binlog
return;
}
PikaCmdArgsType set_argv;
set_argv.resize(3);
//used "set" instead of "SET" to distinguish the binlog of SetCmd
set_argv[0] = "set";
set_cmd_->SetConn(GetConn());
set_cmd_->SetResp(resp_.lock());
for(auto& kv: kvs_){
set_argv[1] = kv.key;
set_argv[2] = kv.value;
set_cmd_->Initial(set_argv, db_name_);
set_cmd_->DoBinlog(slot);
}
}
void GetrangeCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameGetrange);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &start_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
if (pstd::string2int(argv_[3].data(), argv_[3].size(), &end_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
void GetrangeCmd::Do(std::shared_ptr<Slot> slot) {
std::string substr;
rocksdb::Status s = slot->db()->Getrange(key_, start_, end_, &substr);
if (s.ok() || s.IsNotFound()) {
res_.AppendStringLenUint64(substr.size());
res_.AppendContent(substr);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void SetrangeCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameSetrange);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &offset_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
value_ = argv_[3];
}
void SetrangeCmd::Do(std::shared_ptr<Slot> slot) {
int32_t new_len;
rocksdb::Status s = slot->db()->Setrange(key_, offset_, value_, &new_len);
if (s.ok()) {
res_.AppendInteger(new_len);
AddSlotKey("k", key_, slot);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void StrlenCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameStrlen);
return;
}
key_ = argv_[1];
}
void StrlenCmd::Do(std::shared_ptr<Slot> slot) {
int32_t len = 0;
rocksdb::Status s = slot->db()->Strlen(key_, &len);
if (s.ok() || s.IsNotFound()) {
res_.AppendInteger(len);
} else {
res_.SetRes(CmdRes::kErrOther, s.ToString());
}
}
void ExistsCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameExists);
return;
}
keys_ = argv_;
keys_.erase(keys_.begin());
}
void ExistsCmd::Do(std::shared_ptr<Slot> slot) {
std::map<storage::DataType, rocksdb::Status> type_status;
int64_t res = slot->db()->Exists(keys_, &type_status);
if (res != -1) {
res_.AppendInteger(res);
} else {
res_.SetRes(CmdRes::kErrOther, "exists internal error");
}
}
void ExistsCmd::Split(std::shared_ptr<Slot> slot, const HintKeys& hint_keys) {
std::map<storage::DataType, rocksdb::Status> type_status;
int64_t res = slot->db()->Exists(hint_keys.keys, &type_status);
if (res != -1) {
split_res_ += res;
} else {
res_.SetRes(CmdRes::kErrOther, "exists internal error");
}
}
void ExistsCmd::Merge() { res_.AppendInteger(split_res_); }
void ExpireCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameExpire);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &sec_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
void ExpireCmd::Do(std::shared_ptr<Slot> slot) {
std::map<storage::DataType, rocksdb::Status> type_status;
int64_t res = slot->db()->Expire(key_, static_cast<int32_t>(sec_), &type_status);
if (res != -1) {
res_.AppendInteger(res);
} else {
res_.SetRes(CmdRes::kErrOther, "expire internal error");
}
}
std::string ExpireCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLen(content, 3, "*");
// to expireat cmd
std::string expireat_cmd("expireat");
RedisAppendLenUint64(content, expireat_cmd.size(), "$");
RedisAppendContent(content, expireat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// sec
char buf[100];
int64_t expireat = time(nullptr) + sec_;
pstd::ll2string(buf, 100, expireat);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}
void PexpireCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePexpire);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &msec_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
void PexpireCmd::Do(std::shared_ptr<Slot> slot) {
std::map<storage::DataType, rocksdb::Status> type_status;
int64_t res = slot->db()->Expire(key_, static_cast<int32_t>(msec_ / 1000), &type_status);
if (res != -1) {
res_.AppendInteger(res);
} else {
res_.SetRes(CmdRes::kErrOther, "expire internal error");
}
}
std::string PexpireCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLenUint64(content, argv_.size(), "*");
// to expireat cmd
std::string expireat_cmd("expireat");
RedisAppendLenUint64(content, expireat_cmd.size(), "$");
RedisAppendContent(content, expireat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// sec
char buf[100];
int64_t expireat = time(nullptr) + msec_ / 1000;
pstd::ll2string(buf, 100, expireat);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}
void ExpireatCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameExpireat);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &time_stamp_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
void ExpireatCmd::Do(std::shared_ptr<Slot> slot) {
std::map<storage::DataType, rocksdb::Status> type_status;
int32_t res = slot->db()->Expireat(key_, static_cast<int32_t>(time_stamp_), &type_status);
if (res != -1) {
res_.AppendInteger(res);
} else {
res_.SetRes(CmdRes::kErrOther, "expireat internal error");
}
}
void PexpireatCmd::DoInitial() {
if (!CheckArg(argv_.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePexpireat);
return;
}
key_ = argv_[1];
if (pstd::string2int(argv_[2].data(), argv_[2].size(), &time_stamp_ms_) == 0) {
res_.SetRes(CmdRes::kInvalidInt);
return;
}
}
std::string PexpireatCmd::ToBinlog(uint32_t exec_time, uint32_t term_id, uint64_t logic_id, uint32_t filenum,
uint64_t offset) {
std::string content;
content.reserve(RAW_ARGS_LEN);
RedisAppendLenUint64(content, argv_.size(), "*");
// to expireat cmd
std::string expireat_cmd("expireat");
RedisAppendLenUint64(content, expireat_cmd.size(), "$");
RedisAppendContent(content, expireat_cmd);
// key
RedisAppendLenUint64(content, key_.size(), "$");
RedisAppendContent(content, key_);
// sec
char buf[100];
int64_t expireat = time_stamp_ms_ / 1000;
pstd::ll2string(buf, 100, expireat);
std::string at(buf);
RedisAppendLenUint64(content, at.size(), "$");
RedisAppendContent(content, at);
return PikaBinlogTransverter::BinlogEncode(BinlogType::TypeFirst, exec_time, term_id, logic_id, filenum, offset,
content, {});
}