-
Notifications
You must be signed in to change notification settings - Fork 3
/
ctrip_crdt_zset.c
1489 lines (1384 loc) · 55.4 KB
/
ctrip_crdt_zset.c
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
#include "ctrip_crdt_zset.h"
#include "util.h"
#include "crdt_util.h"
#include <rmutil/ziplist.h>
#include <rmutil/sds.h>
#include <assert.h>
#include <math.h>
#include <string.h>
static RedisModuleType *CrdtSS;
static RedisModuleType *CrdtSST;
CrdtTombstoneMethod ZsetTombstoneCommonMethod = {
.merge = crdtSSTMerge,
.filterAndSplit = crdtSSTFilter,
.filterAndSplit2 = crdtSSTFilter2,
.freefilter = freeSSTFilter,
.gc = crdtZsetTombstoneGc,
.purge = crdtZsetTombstonePurge,
.info = crdtZsetTombstoneInfo,
.getVc = clone_sst_vc,
};
CrdtObjectMethod ZSetCommandMethod = {
.merge = crdtSSMerge,
.filterAndSplit = crdtSSFilter,
.filterAndSplit2 = crdtSSFilter2,
.freefilter = freeSSFilter,
};
CrdtDataMethod ZSetDataMethod = {
.propagateDel = crdtZSetDelete,
.info = crdtZSetInfo,
.getLastGid = NULL,
};
/*-----------------------------------------------------------------------------
* Common sorted set API
*----------------------------------------------------------------------------*/
int isCrdtSSTombstone(CrdtTombstone* tom) {
if(tom != NULL && getType(tom) == CRDT_TOMBSTONE && (getDataType(tom) == CRDT_ZSET_TYPE)) {
return CRDT_OK;
}
return CRDT_NO;
}
/*-----------------------------------------------------------------------------
* Sorted set commands
*----------------------------------------------------------------------------*/
//crdt.zadd key gid time vc field <field score del_counter>
const char* crdt_zadd_head = "$9\r\nCRDT.ZADD\r\n";
int replicationFeedCrdtZaddCommand(RedisModuleCtx* ctx, char* cmdbuf, CrdtMeta* meta, sds key, sds* callback, int callback_len) {
// sds vcSds = vectorClockToSds(getMetaVectorClock(meta));
// RedisModule_ReplicationFeedAllSlaves(RedisModule_GetSelectedDb(ctx), "CRDT.zadd", "sllca", key, getMetaGid(set_meta), getMetaTimestamp(set_meta), vcSds, callback, callback_len);
// sdsfree(vcSds);
static size_t crdt_zadd_head_size = 0;
if(crdt_zadd_head_size == 0) {
crdt_zadd_head_size = strlen(crdt_zadd_head);
}
size_t cmdlen = 0;
cmdlen += feedArgc(cmdbuf + cmdlen, callback_len + 5);
cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zadd_head, crdt_zadd_head_size);
// cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zadd_head, crdt_zadd_head_size);
cmdlen += feedStr2Buf(cmdbuf + cmdlen, key, sdslen(key));
cmdlen += feedMeta2Buf(cmdbuf + cmdlen, getMetaGid(meta), getMetaTimestamp(meta), getMetaVectorClock(meta));
for(int i = 0; i<callback_len; i++) {
cmdlen += feedStr2Buf(cmdbuf + cmdlen, callback[i], sdslen(callback[i]));
sdsfree(callback[i]);
}
RedisModule_ReplicationFeedStringToAllSlaves(RedisModule_GetSelectedDb(ctx), cmdbuf, cmdlen);
return cmdlen;
}
int replicationCrdtZaddCommand(RedisModuleCtx* ctx, CrdtMeta* meta, sds key, sds* callback, int callback_len, int callback_byte_size) {
size_t alllen = 16
+ sdslen(key)
+ REPLICATION_MAX_GID_LEN + REPLICATION_MAX_LONGLONG_LEN + REPLICATION_MAX_VC_LEN
+ (callback_len + 1) * (REPLICATION_MAX_LONGLONG_LEN + 2)
+ callback_byte_size ;
char* cmdbuf = RedisModule_GetSharedBuffer(alllen);
if(cmdbuf == NULL) {
cmdbuf = RedisModule_Alloc(alllen);
replicationFeedCrdtZaddCommand(ctx, cmdbuf, meta, key, callback, callback_len);
RedisModule_Free(cmdbuf);
} else {
replicationFeedCrdtZaddCommand(ctx, cmdbuf, meta, key, callback, callback_len);
RedisModule_ReturnSharedBuffer(cmdbuf);
}
return 1;
}
//CRDT.DEL_SS key gid time vc
const char* crdt_del_head = "$11\r\nCRDT.DEL_SS\r\n";
int replicationFeedCrdtDelSSCommand(int dbId, char* cmdbuf, CrdtMeta* meta, sds key, sds* callback, int callback_len) {
// sds vcSds = vectorClockToSds(getMetaVectorClock(meta));
// RedisModule_ReplicationFeedAllSlaves(RedisModule_GetSelectedDb(ctx), "CRDT.zadd", "sllca", key, getMetaGid(set_meta), getMetaTimestamp(set_meta), vcSds, callback, callback_len);
// sdsfree(vcSds);
static size_t crdt_del_head_size = 0;
if(crdt_del_head_size == 0) {
crdt_del_head_size = strlen(crdt_del_head);
}
size_t cmdlen = 0;
cmdlen += feedArgc(cmdbuf + cmdlen, callback_len + 5);
cmdlen += feedBuf(cmdbuf + cmdlen, crdt_del_head, crdt_del_head_size);
// cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zadd_head, crdt_zadd_head_size);
cmdlen += feedStr2Buf(cmdbuf + cmdlen, key, sdslen(key));
cmdlen += feedMeta2Buf(cmdbuf + cmdlen, getMetaGid(meta), getMetaTimestamp(meta), getMetaVectorClock(meta));
for(int i = 0; i<callback_len; i++) {
cmdlen += feedStr2Buf(cmdbuf + cmdlen, callback[i], sdslen(callback[i]));
sdsfree(callback[i]);
}
RedisModule_ReplicationFeedStringToAllSlaves(dbId, cmdbuf, cmdlen);
return cmdlen;
}
// replicationCrdtDelSSCommand(dbId, &del_meta, RedisModule_GetSds(keyRobj), del_counters, dlen)
int replicationCrdtDelSSCommand(int dbId, CrdtMeta* meta, sds key, sds* callback, int callback_len) {
int callback_byte_size = 0;
for(int i = 0; i < callback_len; i++) {
callback_byte_size += sdslen(callback[i]);
}
size_t alllen = 18
+ sdslen(key)
+ REPLICATION_MAX_GID_LEN + REPLICATION_MAX_LONGLONG_LEN + REPLICATION_MAX_VC_LEN
+ (callback_len + 1) * (REPLICATION_MAX_LONGLONG_LEN + 2)
+ callback_byte_size ;
char* cmdbuf = RedisModule_GetSharedBuffer(alllen);
if(cmdbuf == NULL) {
cmdbuf = RedisModule_Alloc(alllen);
replicationFeedCrdtDelSSCommand(dbId, cmdbuf, meta, key, callback, callback_len);
RedisModule_Free(cmdbuf);
} else {
replicationFeedCrdtDelSSCommand(dbId, cmdbuf, meta, key, callback, callback_len);
RedisModule_ReturnSharedBuffer(cmdbuf);
}
return 1;
}
const char* crdt_zincr_head = "$12\r\nCRDT.Zincrby\r\n";
int replicationFeedCrdtZincrCommand(RedisModuleCtx* ctx, char* cmdbuf, CrdtMeta* meta, sds key, sds* callback, int callback_len, double score) {
// sds vcSds = vectorClockToSds(getMetaVectorClock(meta));
// RedisModule_ReplicationFeedAllSlaves(RedisModule_GetSelectedDb(ctx), "CRDT.zincrby", "sllca", key, getMetaGid(set_meta), getMetaTimestamp(set_meta), vcSds, callback, callback_len);
// sdsfree(vcSds);
static size_t crdt_zincr_head_size = 0;
if(crdt_zincr_head_size == 0) {
crdt_zincr_head_size = strlen(crdt_zincr_head);
}
size_t cmdlen = 0;
cmdlen += feedArgc(cmdbuf + cmdlen, callback_len + 5 + 1);
cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zincr_head, crdt_zincr_head_size);
// cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zadd_head, crdt_zadd_head_size);
cmdlen += feedStr2Buf(cmdbuf + cmdlen, key, sdslen(key));
cmdlen += feedMeta2Buf(cmdbuf + cmdlen, getMetaGid(meta), getMetaTimestamp(meta), getMetaVectorClock(meta));
for(int i = 0; i<callback_len; i++) {
cmdlen += feedStr2Buf(cmdbuf + cmdlen, callback[i], sdslen(callback[i]));
sdsfree(callback[i]);
}
cmdlen += feedDouble2Buf(cmdbuf + cmdlen, score);
RedisModule_ReplicationFeedStringToAllSlaves(RedisModule_GetSelectedDb(ctx), cmdbuf, cmdlen);
return cmdlen;
}
int replicationCrdtZincrCommand(RedisModuleCtx* ctx, CrdtMeta* meta, sds key, sds* callback, int callback_len, int callback_byte_size, double score) {
size_t alllen = 20
+ sdslen(key)
+ REPLICATION_MAX_GID_LEN + REPLICATION_MAX_LONGLONG_LEN + REPLICATION_MAX_VC_LEN
+ (callback_len + 1) * (REPLICATION_MAX_LONGLONG_LEN + 2)
+ callback_byte_size
+ REPLICATION_MAX_LONGDOUBLE_LEN;
char* cmdbuf = RedisModule_GetSharedBuffer(alllen);
if(cmdbuf == NULL) {
cmdbuf = RedisModule_Alloc(alllen);
replicationFeedCrdtZincrCommand(ctx, cmdbuf, meta, key, callback, callback_len, score);
RedisModule_Free(cmdbuf);
} else {
replicationFeedCrdtZincrCommand(ctx, cmdbuf, meta, key, callback, callback_len, score);
RedisModule_ReturnSharedBuffer(cmdbuf);
}
return 1;
}
const char* crdt_zrem_head = "$9\r\nCRDT.Zrem\r\n";
int replicationFeedCrdtZremCommand(RedisModuleCtx* ctx, char* cmdbuf, CrdtMeta* meta, sds key, sds* callback, int callback_len) {
static size_t crdt_zrem_head_size = 0;
if(crdt_zrem_head_size == 0) {
crdt_zrem_head_size = strlen(crdt_zrem_head);
}
size_t cmdlen = 0;
cmdlen += feedArgc(cmdbuf + cmdlen, callback_len + 5);
cmdlen += feedBuf(cmdbuf + cmdlen, crdt_zrem_head, crdt_zrem_head_size);
cmdlen += feedStr2Buf(cmdbuf + cmdlen, key, sdslen(key));
cmdlen += feedMeta2Buf(cmdbuf + cmdlen, getMetaGid(meta), getMetaTimestamp(meta), getMetaVectorClock(meta));
for(int i = 0; i<callback_len; i++) {
cmdlen += feedStr2Buf(cmdbuf + cmdlen, callback[i], sdslen(callback[i]));
sdsfree(callback[i]);
}
RedisModule_ReplicationFeedStringToAllSlaves(RedisModule_GetSelectedDb(ctx), cmdbuf, cmdlen);
return cmdlen;
}
int replicationCrdtZremCommand(RedisModuleCtx* ctx, sds key, CrdtMeta* meta ,sds* callback, int callback_len,int callback_byte_size) {
size_t alllen = 20
+ sdslen(key)
+ REPLICATION_MAX_GID_LEN + REPLICATION_MAX_LONGLONG_LEN + REPLICATION_MAX_VC_LEN
+ (callback_len + 1) * (REPLICATION_MAX_LONGLONG_LEN + 2)
+ callback_byte_size ;
char* cmdbuf = RedisModule_GetSharedBuffer(alllen);
if(cmdbuf == NULL) {
cmdbuf = RedisModule_Alloc(alllen);
replicationFeedCrdtZremCommand(ctx, cmdbuf, meta, key, callback, callback_len);
RedisModule_Free(cmdbuf);
} else {
replicationFeedCrdtZremCommand(ctx, cmdbuf, meta, key, callback, callback_len);
RedisModule_ReturnSharedBuffer(cmdbuf);
}
return 1;
}
//zadd <key> <sorted> <field>
int zaddGenericCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int flags) {
static char *nanerr = "resulting score is not a number (NaN)";
// if (argc < 4 || argc % 2 != 0) return RedisModule_WrongArity(ctx);
int result = 0;
double score = 0, newscore;
// double* scores = NULL;
CrdtMeta zadd_meta = {.gid = 0,.timestamp=INIT_TIMESTAMP};
int scoreidx = 2;
/* The following vars are used in order to track what the command actually
* did during the execution, to reply to the client and to trigger the
* notification of keyspace change. */
int added = 0; /* Number of new elements added. */
int updated = 0; /* Number of elements with updated score. */
int processed = 0; /* Number of elements processed, may remain zero with
options like XX. */
//get value
RedisModuleKey* moduleKey = NULL;
while(scoreidx < argc) {
char *opt = RedisModule_GetSds(argv[scoreidx]);
if (!strcasecmp(opt,"nx")) flags |= ZADD_NX;
else if (!strcasecmp(opt,"xx")) flags |= ZADD_XX;
else if (!strcasecmp(opt,"ch")) flags |= ZADD_CH;
else if (!strcasecmp(opt,"incr")) flags |= ZADD_INCR;
else break;
scoreidx++;
}
int incr = (flags & ZADD_INCR) != 0;
int nx = (flags & ZADD_NX) != 0;
int xx = (flags & ZADD_XX) != 0;
int ch = (flags & ZADD_CH) != 0;
/* After the options, we expect to have an even number of args, since
* we expect any number of score-element pairs. */
int elements = argc - scoreidx;
if(elements % 2 || !elements) {
return RedisModule_ReplyWithError(ctx, "ERR syntax error");
}
elements /= 2; /* Now this holds the number of score-element pairs. */
sds callback_items[elements*2];
int callback_len = 0;
int callback_byte_size = 0;
if (nx && xx) {
return RedisModule_ReplyWithError(ctx,
"ERR XX and NX options at the same time are not compatible");
}
if (incr && elements > 1) {
return RedisModule_ReplyWithError(ctx,
"ERR INCR option supports a single increment-element pair");
}
double scores[elements];
sds keys[elements];
for(int i = 0; i < elements; i+=1) {
if(RedisModule_StringToDouble(argv[i * 2 + scoreidx], &scores[i]) != REDISMODULE_OK) {
RedisModule_ReplyWithError(ctx, "ERR value is not a valid float");
goto error;
}
int add = 1;
sds key = RedisModule_GetSds(argv[i * 2 + scoreidx + 1]);
for(int j = i + 1; j < elements; j+=1) {
if(sdscmp(key, RedisModule_GetSds(argv[j * 2 + scoreidx + 1])) == 0) {
add = 0;
}
}
if(add) {
keys[i] = key;
} else {
keys[i] = NULL;
}
}
moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
goto cleanup;
}
CRDT_SS* current = getCurrentValue(moduleKey);
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone) ) {
tombstone = NULL;
}
initIncrMeta(&zadd_meta);
if(current == NULL) {
if(xx) goto reply_to_client;
current = createCrdtZset();
RedisModule_ModuleTypeSetValue(moduleKey, CrdtSS, current);
if(tombstone) {
appendVCForMeta(&zadd_meta, getCrdtSSTLastVc(tombstone));
}
} else {
appendVCForMeta(&zadd_meta, getCrdtSSLastVc(current));
}
for(int i = 0; i < elements; i++) {
if(keys[i] == NULL) { continue; }
int retflags = flags;
score = scores[i];
sds callback_item = zsetAdd2(current, tombstone, &zadd_meta, keys[i], &retflags, score, &newscore);
if(callback_item == NULL) {
RedisModule_ReplyWithError(ctx, nanerr);
goto cleanup;
}
if(sdslen(callback_item) != 0) {
callback_items[callback_len++] = sdsdup(keys[i]);
callback_items[callback_len++] = callback_item;
callback_byte_size += sdslen(keys[i]) + sdslen(callback_item);
} else {
sdsfree(callback_item);
}
if (retflags & ZADD_ADDED) added++;
if (retflags & ZADD_UPDATED) updated++;
if (!(retflags & ZADD_NOP)) processed++;
score = newscore;
}
updateCrdtSSLastVc(current, getMetaVectorClock(&zadd_meta));
if(tombstone) {
if(isNullZsetTombstone(tombstone)) {
RedisModule_DeleteTombstone(moduleKey);
tombstone = NULL;
} else {
zsetTombstoneTryResizeDict(tombstone);
}
}
if (incr) {
replicationCrdtZincrCommand(ctx, &zadd_meta, RedisModule_GetSds(argv[1]), callback_items, callback_len, callback_byte_size, score);
} else {
replicationCrdtZaddCommand(ctx, &zadd_meta, RedisModule_GetSds(argv[1]), callback_items, callback_len, callback_byte_size);
}
reply_to_client:
if (incr) {
if (processed) {
RedisModule_ReplyWithDouble(ctx, score);
} else {
RedisModule_ReplyWithNull(ctx);
}
} else {
RedisModule_ReplyWithLongLong(ctx, ch ? added+updated : added);
}
cleanup:
if(added || updated) {
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_ZSET, incr? "zincr":"zadd", argv[1]);
}
error:
if(zadd_meta.gid != 0) freeIncrMeta(&zadd_meta);
if(moduleKey != NULL) RedisModule_CloseKey(moduleKey);
return result;
}
int zaddCommand(RedisModuleCtx* ctx, RedisModuleString **argv, int argc) {
if(argc < 4) {return RedisModule_WrongArity(ctx);}
return zaddGenericCommand(ctx, argv, argc, ZADD_NONE);
}
/**
* ZINCRBY <key> <score> <field>
*/
int zincrbyCommand(RedisModuleCtx* ctx, RedisModuleString **argv, int argc) {
if(argc != 4) {return RedisModule_WrongArity(ctx);}
return zaddGenericCommand(ctx, argv, argc, ZADD_INCR);
}
//crdt.zadd key gid time vc field <score del_counter>
int crdtZaddCommand(RedisModuleCtx* ctx, RedisModuleString **argv, int argc) {
if (argc < 7 || argc % 2 == 0) return RedisModule_WrongArity(ctx);
CrdtMeta meta = {.gid = 0};
int need_add = 0;
int status = CRDT_OK;
if (readMeta(ctx, argv, 2, &meta) != CRDT_OK) {
return 0;
}
RedisModuleKey* moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
RedisModule_IncrCrdtConflict(TYPECONFLICT | MODIFYCONFLICT);
status = CRDT_ERROR;
goto end;
}
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone)) {
tombstone = NULL;
}
CRDT_SS* current = getCurrentValue(moduleKey);
if(current == NULL) {
current = createCrdtZset();
if(tombstone) {
updateCrdtSSLastVc(current, getCrdtSSTLastVc(tombstone));
}
need_add = 1;
}
int result = 0;
for(int i = 5; i < argc; i += 2) {
sds field = RedisModule_GetSds(argv[i]);
sds info = RedisModule_GetSds(argv[i+1]);
result += zsetTryAdd(current, tombstone, field, &meta, info);
}
if(result == 0 ) {
if(need_add) {
freeCrdtSS(current);
current = NULL;
}
} else {
if(need_add) {
RedisModule_ModuleTypeSetValue(moduleKey, CrdtSS, current);
}
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_SET, "zadd", argv[1]);
}
if(current) {
updateCrdtSSLastVc(current, getMetaVectorClock(&meta));
}
if(tombstone) {
if(isNullZsetTombstone(tombstone)) {
RedisModule_DeleteTombstone(moduleKey);
tombstone = NULL;
} else {
zsetTombstoneTryResizeDict(tombstone);
}
}
RedisModule_MergeVectorClock(getMetaGid(&meta), getMetaVectorClockToLongLong(&meta));
end:
if (meta.gid != 0) {
RedisModule_CrdtReplicateVerbatim(getMetaGid(&meta), ctx);
freeVectorClock(meta.vectorClock);
}
if(moduleKey != NULL ) RedisModule_CloseKey(moduleKey);
if(status == CRDT_OK) {
return RedisModule_ReplyWithOk(ctx);
}else{
return CRDT_ERROR;
}
}
//crdt.zincrby
int crdtZincrbyCommand(RedisModuleCtx* ctx, RedisModuleString **argv, int argc) {
//7 (version <= 1.0.21), 8 (version 1.0.22)
if (argc != 7 && argc != 8 ) return RedisModule_WrongArity(ctx);
CrdtMeta meta = {.gid = 0};
int status = CRDT_OK;
if (readMeta(ctx, argv, 2, &meta) != CRDT_OK) {
return 0;
}
crdtAssert(meta.gid != 0);
RedisModuleKey* moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
RedisModule_IncrCrdtConflict(TYPECONFLICT | MODIFYCONFLICT);
status = CRDT_ERROR;
goto end;
}
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone)) {
tombstone = NULL;
}
CRDT_SS* current = getCurrentValue(moduleKey);
int need_add = 0;
if(current == NULL) {
current = createCrdtZset();
if(tombstone) {
updateCrdtSSLastVc(current, getCrdtSSTLastVc(tombstone));
}
need_add = 1;
}
int result = 0;
sds field = RedisModule_GetSds(argv[5]);
sds score = RedisModule_GetSds(argv[6]);
result += zsetTryIncrby(current, tombstone, field, &meta, score);
if(result == 0) {
if(need_add) {
freeCrdtSS(current);
current = NULL;
}
} else {
if(need_add) {
RedisModule_ModuleTypeSetValue(moduleKey, CrdtSS, current);
}
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_SET, "zincr", argv[1]);
}
if(current) {
updateCrdtSSLastVc(current, getMetaVectorClock(&meta));
}
if(tombstone) {
if(isNullZsetTombstone(tombstone)) {
RedisModule_DeleteTombstone(moduleKey);
tombstone = NULL;
} else {
zsetTombstoneTryResizeDict(tombstone);
}
}
RedisModule_MergeVectorClock(getMetaGid(&meta), getMetaVectorClockToLongLong(&meta));
end:
if (meta.gid != 0) {
RedisModule_CrdtReplicateVerbatim(getMetaGid(&meta), ctx);
freeVectorClock(meta.vectorClock);
}
if(moduleKey != NULL ) RedisModule_CloseKey(moduleKey);
if(status == CRDT_OK) {
return RedisModule_ReplyWithOk(ctx);
}else{
return CRDT_ERROR;
}
}
/**
* ZSCORE <key> <field>
*/
int zscoreCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
int replyed = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
goto error;
}
double result = 0;
CRDT_SS* current = getCurrentValue(moduleKey);
if(current) {
if (getScore(current, RedisModule_GetSds(argv[2]), &result)) {
RedisModule_CloseKey(moduleKey);
return RedisModule_ReplyWithDouble(ctx, result);
}
}
error:
RedisModule_CloseKey(moduleKey);
return RedisModule_ReplyWithNull(ctx);
}
/**
* zcard
*/
int zcardCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
int replyed = 0;
long long result = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
goto end;
}
CRDT_SS* current = getCurrentValue(moduleKey);
if(current) {
result = crdtZsetLength(current);
}
end:
if(moduleKey) RedisModule_CloseKey(moduleKey);
return RedisModule_ReplyWithLongLong(ctx, result);
}
int crdtZSetDelete(int dbId, void* keyRobj, void *key, void *value,long long deltime) {
sds* del_counters = NULL;
RedisModuleKey *moduleKey = (RedisModuleKey *)key;
CrdtMeta del_meta = {.gid = 0,.timestamp=deltime};
initIncrMeta(&del_meta);
VectorClock lastVc = getCrdtSSLastVc(value);
appendVCForMeta(&del_meta, lastVc);
CRDT_SSTombstone *tombstone = getTombstone(moduleKey);
if(tombstone == NULL || !isCrdtSSTombstone(tombstone)) {
tombstone = createCrdtZsetTombstone();
RedisModule_ModuleTombstoneSetValue(moduleKey, CrdtSST, tombstone);
}
int len = crdtZsetLength(value);
del_counters = RedisModule_Alloc(len * sizeof(sds));
int dlen = initSSTombstoneFromSS(tombstone, &del_meta, value, del_counters);
crdtAssert(dlen <= len);
sds vcSds = vectorClockToSds(getMetaVectorClock(&del_meta));
RedisModule_ReplicationFeedAllSlaves(dbId, "CRDT.DEL_SS", "sllca", keyRobj, getMetaGid(&del_meta), getMetaTimestamp(&del_meta), vcSds, del_counters, (size_t)dlen);
sdsfree(vcSds);
for(int i = 0; i < dlen; i++) {
sdsfree(del_counters[i]);
}
freeIncrMeta(&del_meta);
if(del_counters != NULL) RedisModule_Free(del_counters);
return CRDT_OK;
}
//crdt.del_ss key gid timespace vc <field,gid:vcu:type:value>...
int crdtDelSSCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 5) return RedisModule_WrongArity(ctx);
CrdtMeta meta = {.gid = 0};
int status = CRDT_OK;
if (readMeta(ctx, argv, 2, &meta) != CRDT_OK) {
return 0;
}
RedisModuleKey* moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
RedisModule_IncrCrdtConflict(TYPECONFLICT | MODIFYCONFLICT);
status = CRDT_ERROR;
goto end;
}
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone)) {
tombstone = NULL;
}
CRDT_SS* current = getCurrentValue(moduleKey);
if(tombstone == NULL) {
tombstone = createCrdtZsetTombstone();
if(current) {
updateCrdtSSTLastVc(tombstone, getCrdtSSLastVc(current));
}
RedisModule_ModuleTombstoneSetValue(moduleKey, CrdtSST, tombstone);
}
for(int i = 5; i < argc; i += 1) {
sds field_and_del_counter_info = RedisModule_GetSds(argv[i]);
zsetTryRem(tombstone, current, field_and_del_counter_info, &meta);
}
zsetTryDel(current, tombstone, &meta);
if(current) {
if(crdtZsetLength(current) == 0) {
RedisModule_DeleteKey(moduleKey);
} else {
updateCrdtSSLastVc(current, getMetaVectorClock(&meta));
zsetTryResizeDict(current);
}
}
updateCrdtSSTLastVc(tombstone, getMetaVectorClock(&meta));
updateCrdtSSTMaxDel(tombstone, getMetaVectorClock(&meta));
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_SET, "del", argv[1]);
RedisModule_MergeVectorClock(getMetaGid(&meta), getMetaVectorClockToLongLong(&meta));
end:
if (meta.gid != 0) {
RedisModule_CrdtReplicateVerbatim(getMetaGid(&meta), ctx);
freeVectorClock(meta.vectorClock);
}
if(moduleKey != NULL ) RedisModule_CloseKey(moduleKey);
if(status == CRDT_OK) {
return RedisModule_ReplyWithOk(ctx);
}else{
return CRDT_ERROR;
}
}
//zrange key start end
int zrangeGenericCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int reverse) {
if(argc < 4) { return RedisModule_WrongArity(ctx); }
long long start;
long long end;
int withscores = 0;
if(RedisModule_StringToLongLong(argv[2], &start) != REDISMODULE_OK) {
RedisModule_WrongArity(ctx);
return REDISMODULE_ERR;
}
if(RedisModule_StringToLongLong(argv[3], &end) != REDISMODULE_OK) {
RedisModule_WrongArity(ctx);
return REDISMODULE_ERR;
}
if(argc == 5 && !strcasecmp(RedisModule_GetSds(argv[4]), "withscores")) {
withscores = 1;
}
int replyed = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
return RedisModule_ReplyWithArray(ctx , 0);
}
CRDT_SS* current = getCurrentValue(moduleKey);
size_t llen = crdtZsetLength(current);
if(start < 0 ) start = llen + start;
if(end < 0) end = llen + end;
if(start < 0) start = 0;
if(start > end || start >= llen) {
RedisModule_ReplyWithNull(ctx);
goto end;
}
if (end >= llen) end = llen-1;
size_t rangelen = (end-start)+1;
RedisModule_ReplyWithArray(ctx, withscores ? (rangelen*2) : rangelen);
zskiplistNode *ln = zset_get_zsl_element_by_rank(current, reverse, start);
while(rangelen--) {
sds ele = ln->ele;
RedisModule_ReplyWithStringBuffer(ctx,ele,sdslen(ele));
if (withscores)
RedisModule_ReplyWithDouble(ctx,ln->score);
ln = reverse ? ln->backward : ln->level[0].forward;
}
end:
RedisModule_CloseKey(moduleKey);
return 1;
}
int zrangeCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return zrangeGenericCommand(ctx, argv, argc, 0);
}
int zrevrangeCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return zrangeGenericCommand(ctx, argv, argc, 1);
}
//ZREM KEY FILED
int zremCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if(argc < 3) { return RedisModule_WrongArity(ctx);}
CrdtMeta zrem_meta = {.gid = 0,.timestamp=INIT_TIMESTAMP};
int deleted = 0, keyremoved = 0, i;
RedisModuleKey* moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
return REDISMODULE_ERR;
}
CRDT_SS* current = getCurrentValue(moduleKey);
if(current == NULL) {
RedisModule_CloseKey(moduleKey);
return RedisModule_ReplyWithLongLong(ctx , 0);
}
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone) ) {
tombstone = NULL;
}
initIncrMeta(&zrem_meta);
appendVCForMeta(&zrem_meta, getCrdtSSLastVc(current));
int createTombstoned = 0;
if(tombstone == NULL) {
tombstone = createCrdtZsetTombstone();
createTombstoned = 1;
}
sds callback_items[argc-2];
int callback_byte_size = 0;
for(i = 2; i < argc; i++) {
sds callback_item = zsetRem(current, tombstone, &zrem_meta, RedisModule_GetSds(argv[i]));
if(callback_item) {
callback_items[deleted++] = callback_item;
callback_byte_size += sdslen(callback_item);
}
if(crdtZsetLength(current) == 0) {
RedisModule_DeleteKey(moduleKey);
keyremoved = 1;
break;
}
}
if (deleted) {
updateCrdtSSTLastVc(tombstone, getMetaVectorClock(&zrem_meta));
if (createTombstoned) {
RedisModule_ModuleTombstoneSetValue(moduleKey, CrdtSST, tombstone);
}
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_ZSET, "zrem", argv[1]);
if (keyremoved) {
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_ZSET, "del", argv[1]);
} else {
updateCrdtSSLastVc(current, getMetaVectorClock(&zrem_meta));
zsetTryResizeDict(current);
}
replicationCrdtZremCommand(ctx, RedisModule_GetSds(argv[1]), &zrem_meta ,callback_items, deleted, callback_byte_size);
} else {
if (createTombstoned) {
freeCrdtSST(tombstone);
tombstone = NULL;
}
}
RedisModule_ReplyWithLongLong(ctx, deleted);
if(zrem_meta.gid != 0) freeIncrMeta(&zrem_meta);
if(moduleKey != NULL) RedisModule_CloseKey(moduleKey);
return REDISMODULE_OK;
}
//crdt.zrem key git time vc [num key <gid:vcu:type:value>]...
int crdtZremCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc < 6) return RedisModule_WrongArity(ctx);
CrdtMeta meta = {.gid = 0};
int status = CRDT_OK;
if (readMeta(ctx, argv, 2, &meta) != CRDT_OK) {
return 0;
}
RedisModuleKey* moduleKey = getWriteRedisModuleKey(ctx, argv[1], CrdtSS);
if(moduleKey == NULL) {
RedisModule_IncrCrdtConflict(TYPECONFLICT | MODIFYCONFLICT);
status = CRDT_ERROR;
goto end;
}
CrdtTombstone* tombstone = getTombstone(moduleKey);
if(tombstone != NULL && !isCrdtSSTombstone(tombstone)) {
tombstone = NULL;
}
CRDT_SS* current = getCurrentValue(moduleKey);
if(tombstone == NULL) {
tombstone = createCrdtZsetTombstone();
if(current) {
updateCrdtSSTLastVc(tombstone, getCrdtSSLastVc(current));
}
RedisModule_ModuleTombstoneSetValue(moduleKey, CrdtSST, tombstone);
}
int result = 0;
for(int i = 5; i < argc; i += 1) {
sds field_and_del_counter_info = RedisModule_GetSds(argv[i]);
result += zsetTryRem(tombstone, current, field_and_del_counter_info, &meta);
}
if(current && result) {
if(crdtZsetLength(current) == 0) {
RedisModule_DeleteKey(moduleKey);
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_SET, "del", argv[1]);
current = NULL;
} else {
zsetTryResizeDict(current);
}
}
if(tombstone && isNullZsetTombstone(tombstone)) {
RedisModule_DeleteTombstone(moduleKey);
tombstone = NULL;
}
RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_SET, "zrem", argv[1]);
if(tombstone) {
updateCrdtSSTLastVc(tombstone, getMetaVectorClock(&meta));
}
if(current) {
updateCrdtSSLastVc(current, getMetaVectorClock(&meta));
}
RedisModule_MergeVectorClock(getMetaGid(&meta), getMetaVectorClockToLongLong(&meta));
end:
if (meta.gid != 0) {
RedisModule_CrdtReplicateVerbatim(getMetaGid(&meta), ctx);
freeVectorClock(meta.vectorClock);
}
if(moduleKey != NULL ) RedisModule_CloseKey(moduleKey);
if(status == CRDT_OK) {
return RedisModule_ReplyWithOk(ctx);
}else{
return CRDT_ERROR;
}
}
int zrankGenericCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int reverse) {
if(argc != 3) { return RedisModule_WrongArity(ctx); }
int replyed = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
return RedisModule_ReplyWithNull(ctx);
}
CRDT_SS* current = getCurrentValue(moduleKey);
long rank = zsetRank(current, RedisModule_GetSds(argv[2]),reverse);
if ( rank >= 0 ) {
RedisModule_ReplyWithLongLong(ctx, rank);
} else {
RedisModule_ReplyWithNull(ctx);
}
RedisModule_CloseKey(moduleKey);
return 1;
}
//zrankCommand key ele
int zrankCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return zrankGenericCommand(ctx, argv, argc, 0);
}
int zrevrankCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return zrankGenericCommand(ctx, argv, argc, 1);
}
int genericZrangebyscoreCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int reverse) {
if(argc < 4) { return RedisModule_WrongArity(ctx); }
zrangespec range;
long long offset = 0, limit = -1;
int withscores = 0;
unsigned long rangelen = 0;
int minidx, maxidx;
sds* fields = NULL;
double* scores = NULL;
if(reverse) {
maxidx = 2; minidx = 3;
} else {
maxidx = 3; minidx = 2;
}
if (!zslParseRange(RedisModule_GetSds(argv[minidx]), RedisModule_GetSds(argv[maxidx]), &range)) {
return RedisModule_ReplyWithError(ctx, "min or max is not a float");
}
if(argc > 4) {
int remaining = argc - 4;
int pos = 4;
while (remaining) {
if (remaining >= 1 && !strcasecmp(RedisModule_GetSds(argv[pos]),"withscores")) {
pos++; remaining--;
withscores = 1;
} else if (remaining >= 3 && !strcasecmp(RedisModule_GetSds(argv[pos]),"limit")) {
sds offset_str = RedisModule_GetSds(argv[pos+1]);
sds limit_str = RedisModule_GetSds(argv[pos+2]);
if ((!string2ll(offset_str, sdslen(offset_str), &offset))
||
(!string2ll(limit_str, sdslen(limit_str), &limit)
))
{
return RedisModule_WrongArity(ctx);
}
pos += 3; remaining -= 3;
} else {
return RedisModule_ReplyWithError(ctx,"-ERR syntax error");
}
}
}
int replyed = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
return RedisModule_ReplyWithArray(ctx, 0);
}
CRDT_SS* current = RedisModule_ModuleTypeGetValue(moduleKey);
zskiplistNode* ln = zslInRange(current, &range, reverse);
if(ln == NULL) {
if(moduleKey) RedisModule_CloseKey(moduleKey);
return RedisModule_ReplyWithArray(ctx, 0);
}
while (ln && offset--) {
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
int max_len = crdtZsetLength(current);
fields = RedisModule_Alloc(max_len * sizeof(sds));
scores = RedisModule_Alloc(max_len * sizeof(double));
while (ln && limit--) {
/* Abort when the node is no longer in range. */
if (reverse) {
if (!zslValueGteMin(ln->score,&range)) break;
} else {
if (!zslValueLteMax(ln->score,&range)) break;
}
fields[rangelen] = ln->ele;
if (withscores) {
scores[rangelen] = ln->score;
}
rangelen++;
/* Move to next node */
if (reverse) {
ln = ln->backward;
} else {
ln = ln->level[0].forward;
}
}
if (withscores) {
RedisModule_ReplyWithArray(ctx, rangelen * 2);
} else {
RedisModule_ReplyWithArray(ctx, rangelen);
}
for(int i = 0; i < rangelen; i++) {
RedisModule_ReplyWithStringBuffer(ctx,fields[i],sdslen(fields[i]));
if(withscores) {
RedisModule_ReplyWithDouble(ctx,scores[i]);
}
}
if(moduleKey) RedisModule_CloseKey(moduleKey);
if (fields != NULL) RedisModule_Free(fields);
if (scores != NULL) RedisModule_Free(scores);
return 1;
}
int zrangebyscoreCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
return genericZrangebyscoreCommand(ctx, argv, argc, 0);
}
int zrevrangebyscoreCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
return genericZrangebyscoreCommand(ctx, argv, argc, 1);
}
//zcount key before after
int zcountCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if(argc < 4) { return RedisModule_WrongArity(ctx); }
zrangespec range;
unsigned long rank;
int count = 0;
if (!zslParseRange(RedisModule_GetSds(argv[2]), RedisModule_GetSds(argv[3]), &range)) {
return RedisModule_ReplyWithError(ctx, "min or max is not a float");
}
int replyed = 0;
RedisModuleKey* moduleKey = getRedisModuleKey(ctx, argv[1], CrdtSS, REDISMODULE_READ, &replyed);
if (moduleKey == NULL) {
if(replyed) return CRDT_ERROR;
return RedisModule_ReplyWithLongLong(ctx, 0);
}
CRDT_SS* current = RedisModule_ModuleTypeGetValue(moduleKey);