-
Notifications
You must be signed in to change notification settings - Fork 1
/
gtid.c
1186 lines (1031 loc) · 35.5 KB
/
gtid.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
/* Copyright (c) 2023, ctrip.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#include "gtid_util.h"
#include "gtid.h"
#ifndef GTID_MALLOC_INCLUDE
#define GTID_MALLOC_INCLUDE "gtid_malloc.h"
#endif
#include GTID_MALLOC_INCLUDE
#define GNO_REPR_MAX_LEN 21
#define MIN(a, b) (a) < (b) ? (a) : (b)
#define MAX(a, b) (a) < (b) ? (b) : (a)
#define GTID_INTERVAL_SKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */
#define GTID_INTERVAL_SKIPLIST_P 0.25 /* Skiplist P = 1/4 */
/* most gtid interval nodes are level 1 */
#define GTID_INTERVAL_MEMORY (sizeof(gtidIntervalNode) + sizeof(gtidIntervalNode*))
static inline int gtidIntervalIsValid(gno_t start, gno_t end) {
return start >= GTID_GNO_INITIAL && start <= end;
}
int gtidIntervalDecode(char* interval_str, size_t len, gno_t *pstart,
gno_t *pend) {
const char *hyphen = "-";
int index = -1;
for(size_t i = 0; i < len; i++) {
if(interval_str[i] == hyphen[0]) {
index = i;
break;
}
}
gno_t start = 0, end = 0;
if(index == -1) {
if(string2ll(interval_str, len, &start)) {
if (pstart) *pstart = start;
if (pend) *pend = start;
return 0;
}
} else {
/* {start}-{end} */
if(string2ll(interval_str, index, &start) &&
string2ll(interval_str+index+1, len-index-1, &end)) {
if (pstart) *pstart = start;
if (pend) *pend = end;
return 0;
}
}
return 1;
}
ssize_t gtidIntervalEncode(char *buf, size_t maxlen, gno_t start, gno_t end) {
size_t len = 0;
if (len+GNO_REPR_MAX_LEN > maxlen) goto err;
len += ll2string(buf+len, GNO_REPR_MAX_LEN, start);
if (start != end) {
if (len+1+GNO_REPR_MAX_LEN > maxlen) goto err;
memcpy(buf+len, "-", 1), len += 1;
len += ll2string(buf + len, GNO_REPR_MAX_LEN, end);
}
return len;
err:
return -1;
}
gtidIntervalNode *gtidIntervalNodeNew(int level, gno_t start, gno_t end) {
size_t intvl_size = sizeof(gtidIntervalNode)+level*sizeof(gtidIntervalNode*);
gtidIntervalNode *interval = gtid_malloc(intvl_size);
memset(interval,0,intvl_size);
interval->level = level;
interval->start = start;
interval->end = end;
return interval;
}
void gtidIntervalNodeFree(gtidIntervalNode* interval) {
gtid_free(interval);
}
gtidIntervalSkipList *gtidIntervalSkipListNew() {
gtidIntervalSkipList *gsl = gtid_malloc(sizeof(*gsl));
gsl->level = 1;
gsl->header = gtidIntervalNodeNew(GTID_INTERVAL_SKIPLIST_MAXLEVEL,0,0);
gsl->tail = gsl->header;
gsl->node_count = 1;
gsl->gno_count = 0;
return gsl;
}
void gtidIntervalSkipListFree(gtidIntervalSkipList *gsl) {
gtidIntervalNode *interval = gsl->header, *next;
while(interval) {
next = interval->forwards[0];
gtidIntervalNodeFree(interval);
interval = next;
}
gtid_free(gsl);
}
gtidIntervalSkipList *gtidIntervalSkipListDup(gtidIntervalSkipList *gsl) {
gtidIntervalNode *leads[GTID_INTERVAL_SKIPLIST_MAXLEVEL], *cur, *tail, *x;
gtidIntervalSkipList *dup = gtid_malloc(sizeof(*gsl));
dup->level = gsl->level;
dup->node_count = gsl->node_count;
dup->gno_count = gsl->gno_count;
dup->header = gtidIntervalNodeNew(GTID_INTERVAL_SKIPLIST_MAXLEVEL,0,0);
for (int i = 0; i < GTID_INTERVAL_SKIPLIST_MAXLEVEL; i++)
leads[i] = dup->header;
tail = dup->header;
cur = gsl->header->forwards[0];
while (cur) {
x = gtidIntervalNodeNew(cur->level,cur->start,cur->end);
for (int level = 0; level < x->level; level++) {
leads[level]->forwards[level] = x;
leads[level] = x;
}
tail = x;
cur = cur->forwards[0];
}
dup->tail = tail;
return dup;
}
static int gitdIntervalRandomLevel(void) {
int level = 1;
while ((rand()&0xFFFF) < (GTID_INTERVAL_SKIPLIST_P * 0xFFFF))
level += 1;
return (level<GTID_INTERVAL_SKIPLIST_MAXLEVEL) ? level : GTID_INTERVAL_SKIPLIST_MAXLEVEL;
}
static inline size_t gtidIntervalNodeGnoCount(struct gtidIntervalNode *interval) {
return interval->end - interval->start + 1;
}
/* return num of gno added. */
gno_t gtidIntervalSkipListAdd(gtidIntervalSkipList *gsl, gno_t start, gno_t end) {
gtidIntervalNode *lefts[GTID_INTERVAL_SKIPLIST_MAXLEVEL],
*rights[GTID_INTERVAL_SKIPLIST_MAXLEVEL], *x, *l, *r;
int i, level;
ssize_t added = 0;
assert(gtidIntervalIsValid(start, end));
/* fast path */
if (gsl->header != gsl->tail && gsl->tail->end+1 == start) {
gsl->tail->end = end;
added = end+1-start;
gsl->gno_count+=added;
return added;
}
x = gsl->header;
for (i = gsl->level-1; i >= 0; i--) {
while (x->forwards[i] && x->forwards[i]->end + 1 < start)
x = x->forwards[i];
lefts[i] = x;
}
x = gsl->header;
for (i = gsl->level-1; i >= 0; i--) {
while (x->forwards[i] && end + 1 >= x->forwards[i]->start)
x = x->forwards[i];
rights[i] = x;
}
if (lefts[0] == rights[0]) {
/* none overlaps with [start, end]: create new one. */
level = gitdIntervalRandomLevel();
x = gtidIntervalNodeNew(level,start,end);
if (level > gsl->level) {
for (i = gsl->level; i < level; i++)
rights[i] = gsl->header;
gsl->level = level;
}
for (i = 0; i < x->level; i++) {
x->forwards[i] = rights[i]->forwards[i];
rights[i]->forwards[i] = x;
}
added = end-start+1;
gsl->gno_count += added;
gsl->node_count++;
if (gsl->tail->forwards[0]) gsl->tail = gsl->tail->forwards[0];
} else {
/* overlaps with [start, end]: join all to rightmost and remove others. */
size_t saved_gno_count;
gtidIntervalNode *next;
l = lefts[0]->forwards[0], r = rights[0], x = rights[0];
saved_gno_count = gtidIntervalNodeGnoCount(x);
x->start = MIN(start,l->start);
x->end = MAX(end,r->end);
added += gtidIntervalNodeGnoCount(x) - saved_gno_count;
for (i = 0; i < x->level; i++)
lefts[i]->forwards[i] = x;
for (i = x->level; i < gsl->level; i++)
lefts[i]->forwards[i] = rights[i]->forwards[i];
while (l && l != r) {
next = l->forwards[0];
gsl->node_count--;
added -= gtidIntervalNodeGnoCount(l);
gtidIntervalNodeFree(l);
l = next;
}
while(gsl->level > 1 && gsl->header->forwards[gsl->level-1] == NULL)
gsl->level--;
gsl->gno_count += added;
}
return added;
}
gno_t gtidIntervalSkipListMerge(gtidIntervalSkipList *dst,
gtidIntervalSkipList *src) {
gno_t added = 0;
gtidIntervalNode *x;
for (x = src->header->forwards[0]; x != NULL; x = x->forwards[0]) {
added += gtidIntervalSkipListAdd(dst, x->start, x->end);
}
return added;
}
gno_t gtidIntervalSkipListRemove(gtidIntervalSkipList *gsl, gno_t start,
gno_t end) {
gtidIntervalNode *lefts[GTID_INTERVAL_SKIPLIST_MAXLEVEL],
*rights[GTID_INTERVAL_SKIPLIST_MAXLEVEL], *x;
int i;
ssize_t removed = 0;
assert(gtidIntervalIsValid(start, end));
/* lefts: last (whole or partial) node to reserve */
x = gsl->header;
for (i = gsl->level-1; i >= 0; i--) {
while (x->forwards[i] && x->forwards[i]->start < start)
x = x->forwards[i];
lefts[i] = x;
}
/* rights: last whole node to remove */
x = gsl->header;
for (i = gsl->level-1; i >= 0; i--) {
while (x->forwards[i] && x->forwards[i]->end <= end)
x = x->forwards[i];
rights[i] = x;
}
if (rights[0]->end < lefts[0]->start) {
/* remove gno within one node: split it. */
int level = gitdIntervalRandomLevel();
x = gtidIntervalNodeNew(level,end+1,lefts[0]->end);
lefts[0]->end = start-1;
if (level > gsl->level) {
for (i = gsl->level; i < level; i++)
lefts[i] = gsl->header;
gsl->level = level;
}
for (i = 0; i < x->level; i++) {
x->forwards[i] = lefts[i]->forwards[i];
lefts[i]->forwards[i] = x;
}
removed = end-start+1;
gsl->gno_count -= removed;
gsl->node_count++;
if (gsl->tail->forwards[0]) gsl->tail = gsl->tail->forwards[0];
} else {
size_t saved_gno_count;
gtidIntervalNode *l, *r, *next;
l = lefts[0];
saved_gno_count = gtidIntervalNodeGnoCount(l);
l->end = MIN(l->end,start-1);
removed += saved_gno_count - gtidIntervalNodeGnoCount(l);
r = rights[0]->forwards[0];
if (r) {
saved_gno_count = gtidIntervalNodeGnoCount(r);
r->start = MAX(r->start,end+1);
removed += saved_gno_count - gtidIntervalNodeGnoCount(r);
} else {
gsl->tail = l;
}
l = lefts[0]->forwards[0];
for (i = 0; i < gsl->level; i++)
lefts[i]->forwards[i] = rights[i]->forwards[i];
while (l && l != r) {
next = l->forwards[0];
gsl->node_count--;
removed += gtidIntervalNodeGnoCount(l);
gtidIntervalNodeFree(l);
l = next;
}
while(gsl->level > 1 && gsl->header->forwards[gsl->level-1] == NULL)
gsl->level--;
gsl->gno_count -= removed;
}
return removed;
}
gno_t gtidIntervalSkipListDiff(gtidIntervalSkipList *dst,
gtidIntervalSkipList *src) {
gno_t removed = 0;
gtidIntervalNode *cur = src->header->forwards[0], *next;
while (cur) {
next = cur->forwards[0];
removed += gtidIntervalSkipListRemove(dst, cur->start, cur->end);
cur = next;
}
return removed;
}
int gtidIntervalSkipListContains(gtidIntervalSkipList *gsl, gno_t gno) {
int i;
gtidIntervalNode *x = gsl->header;
assert(gno >= GTID_GNO_INITIAL);
for (i = gsl->level-1; i >= 0; i--) {
while (x->forwards[i] && x->forwards[i]->start <= gno)
x = x->forwards[i];
}
return x->start <= gno && gno <= x->end;
}
gno_t gtidIntervalSkipListNext(gtidIntervalSkipList *gsl, int update) {
gno_t gno = gsl->tail->end+1;
if (update) gtidIntervalSkipListAdd(gsl, gno, gno);
return gno;
}
gno_t gtidIntervalSkipListCurrent(gtidIntervalSkipList *gsl) {
return gsl->tail->end;
}
/* return -1 if encode failed, otherwise return encoded length. */
ssize_t uuidGnoEncode(char *buf, size_t maxlen, const char *uuid,
size_t uuid_len, gno_t gno) {
size_t len = 0;
if (maxlen < uuid_len+1+GNO_REPR_MAX_LEN) return -1;
memcpy(buf + len, uuid, uuid_len), len += uuid_len;
memcpy(buf + len, ":", 1), len += 1;
len += ll2string(buf + len, GNO_REPR_MAX_LEN, gno);
return len;
}
char* uuidGnoDecode(char* src, size_t src_len, long long* gno, size_t* uuid_len) {
const char *split = ":";
int index = -1;
for(size_t i = 0; i < src_len; i++) {
if(src[i] == split[0]) {
index = i;
break;
}
}
if(index == -1) goto err;
if(string2ll(src+index+1, src_len-index-1, gno) == 0) goto err;
*uuid_len = (size_t)index;
return src;
err:
return NULL;
}
void uuidDup(char **pdup, size_t *pdup_len, const char* uuid,
int uuid_len) {
char* dup = gtid_malloc(uuid_len + 1);
memcpy(dup, uuid, uuid_len);
dup[uuid_len] = '\0';
*pdup = dup;
*pdup_len = uuid_len;
}
uuidSet *uuidSetNew(const char *uuid, size_t uuid_len) {
uuidSet *uuid_set = gtid_malloc(sizeof(*uuid_set));
uuidDup(&uuid_set->uuid,&uuid_set->uuid_len,uuid,uuid_len);
uuid_set->intervals = gtidIntervalSkipListNew();
uuid_set->next = NULL;
return uuid_set;
}
void uuidSetFree(uuidSet* uuid_set) {
gtidIntervalSkipListFree(uuid_set->intervals);
gtid_free(uuid_set->uuid);
gtid_free(uuid_set);
}
uuidSet *uuidSetDup(uuidSet* uuid_set) {
uuidSet *result = gtid_malloc(sizeof(uuidSet));
uuidDup(&result->uuid,&result->uuid_len,uuid_set->uuid,uuid_set->uuid_len);
result->intervals = gtidIntervalSkipListDup(uuid_set->intervals);
result->next = NULL;
return result;
}
gno_t uuidSetCount(uuidSet *uuid_set) {
return uuid_set->intervals->gno_count;
}
uuidSet *uuidSetDecode(char* uuid_set_str, int len) {
const char *colon = ":";
uuidSet *uuid_set = NULL;
if(len <= 0) goto err;
int i;
for(i = 0; i < len; i++) {
if(uuid_set_str[i] == colon[0]) break;
}
uuid_set = uuidSetNew(uuid_set_str, i);
/* this is an empty uuidSet */
if (i == len) return uuid_set;
int ret, start_index = i+1, end_index = i+1;
gno_t start, end;
for (;end_index < len; end_index++) {
if (uuid_set_str[end_index] == colon[0]) {
if (start_index < end_index) {
ret = gtidIntervalDecode(uuid_set_str+start_index,
end_index-start_index, &start, &end);
if (ret == 0 && gtidIntervalIsValid(start,end)) {
uuidSetAdd(uuid_set, start, end);
} else {
goto err;
}
}
start_index = end_index+1;
}
}
if (start_index < end_index) {
ret = gtidIntervalDecode(uuid_set_str+start_index,
end_index-start_index, &start, &end);
if (ret == 0 && gtidIntervalIsValid(start,end)) {
uuidSetAdd(uuid_set, start, end);
} else {
goto err;
}
}
return uuid_set;
err:
if(uuid_set != NULL) {
uuidSetFree(uuid_set);
uuid_set = NULL;
}
return NULL;
}
/* {uuid}: {longlong}-{longlong}* n */
size_t uuidSetEstimatedEncodeBufferSize(uuidSet* uuid_set) {
/* 44 = 1(:) + 21(longlong) + 1(-) + 21(long long) */
return uuid_set->uuid_len + uuid_set->intervals->node_count * 44;
}
ssize_t uuidSetEncode(char *buf, size_t maxlen, uuidSet* uuid_set) {
size_t len = 0, ret;
gtidIntervalNode *cur, *header;
if (len+uuid_set->uuid_len > maxlen) goto err;
memcpy(buf + len, uuid_set->uuid, uuid_set->uuid_len);
len += uuid_set->uuid_len;
header = uuid_set->intervals->header->forwards[0];
for (cur = header; cur != NULL; cur = cur->forwards[0]) {
if (len+1 > maxlen) goto err;
memcpy(buf + len, ":", 1), len += 1;
ret = gtidIntervalEncode(buf+len, maxlen-len, cur->start, cur->end);
if (ret < 0) goto err;
len += ret;
}
return len;
err:
return -1;
}
gno_t uuidSetAdd(uuidSet* uuid_set, gno_t start, gno_t end) {
if (!gtidIntervalIsValid(start, end)) return 0;
return gtidIntervalSkipListAdd(uuid_set->intervals,start,end);
}
gno_t uuidSetRemove(uuidSet* uuid_set, gno_t start, gno_t end) {
if (!gtidIntervalIsValid(start, end)) return 0;
return gtidIntervalSkipListRemove(uuid_set->intervals,start,end);
}
gno_t uuidSetRaise(uuidSet *uuid_set, gno_t watermark) {
if (!gtidIntervalIsValid(1, watermark)) return 0;
return gtidIntervalSkipListAdd(uuid_set->intervals,1,watermark);
}
gno_t uuidSetMerge(uuidSet* dst, uuidSet* src) {
if (dst->uuid_len != src->uuid_len ||
memcmp(dst->uuid, src->uuid, src->uuid_len))
return 0;
gno_t added = gtidIntervalSkipListMerge(dst->intervals, src->intervals);
return added;
}
gno_t uuidSetDiff(uuidSet* dst, uuidSet* src) {
if (dst->uuid_len != src->uuid_len ||
memcmp(dst->uuid, src->uuid, src->uuid_len))
return 0;
return gtidIntervalSkipListDiff(dst->intervals, src->intervals);
}
int uuidSetContains(uuidSet* uuid_set, gno_t gno) {
if (!gtidIntervalIsValid(1, gno)) return 0;
return gtidIntervalSkipListContains(uuid_set->intervals, gno);
}
gno_t uuidSetNext(uuidSet* uuid_set, int update) {
return gtidIntervalSkipListNext(uuid_set->intervals, update);
}
gtidSet* gtidSetNew() {
gtidSet *gtid_set = gtid_malloc(sizeof(*gtid_set));
gtid_set->header = NULL;
gtid_set->tail = NULL;
gtid_set->current = NULL;
gtid_set->curnext = 0;
gtid_set->cached = NULL;
return gtid_set;
}
void gtidSetFree(gtidSet *gtid_set) {
if (gtid_set == NULL) return;
uuidSet *cur = gtid_set->header, *next;
while(cur != NULL) {
next = cur->next;
uuidSetFree(cur);
cur = next;
}
gtid_free(gtid_set);
}
gtidSet* gtidSetDup(gtidSet *gtid_set) {
gtidSet *result = gtid_malloc(sizeof(gtidSet));
uuidSet *cur = gtid_set->header, *x = NULL, *p = NULL;
result->current = NULL;
result->curnext = 0;
result->cached = NULL;
result->header = NULL;
while (cur) {
x = uuidSetDup(cur);
if (p) p->next = x;
if (!result->header) result->header = x;
p = x;
cur = cur->next;
}
result->tail = x;
return result;
}
gno_t gtidSetAppend(gtidSet *gtid_set, uuidSet *uuid_set) {
if (uuid_set == NULL) return 0;
if (gtid_set->header == NULL) {
gtid_set->header = uuid_set;
gtid_set->tail = uuid_set;
} else {
gtid_set->tail->next = uuid_set;
gtid_set->tail = uuid_set;
}
return uuidSetCount(uuid_set);
}
gtidSet *gtidSetDecode(char* src, size_t len) {
uuidSet *uuid_set;
gtidSet* gtid_set = gtidSetNew();
const char *split = ",";
int uuid_str_start_index = 0;
if (len == 0) return gtid_set;
for(size_t i = 0; i < len; i++) {
if(src[i] == split[0]) {
uuid_set = uuidSetDecode(src+uuid_str_start_index,
i-uuid_str_start_index);
if (uuid_set == NULL) goto err;
gtidSetAppend(gtid_set, uuid_set);
uuid_str_start_index = (i + 1);
}
}
uuid_set = uuidSetDecode(src+uuid_str_start_index,
len-uuid_str_start_index);
if (uuid_set == NULL) goto err;
gtidSetAppend(gtid_set, uuid_set);
return gtid_set;
err:
gtidSetFree(gtid_set);
return NULL;
}
size_t gtidSetEstimatedEncodeBufferSize(gtidSet* gtid_set) {
size_t max_len = 1;
uuidSet *cur = gtid_set->header;
while(cur != NULL) {
max_len += uuidSetEstimatedEncodeBufferSize(cur) + 1;
cur = cur->next;
}
return max_len;
}
ssize_t gtidSetEncode(char *buf, size_t maxlen, gtidSet* gtid_set) {
size_t len = 0, ret;
int first = 1;
for (uuidSet *cur = gtid_set->header;cur != NULL; cur = cur->next) {
if (uuidSetCount(cur) == 0) continue;
if (first) {
first = 0;
} else {
if (len+1 > maxlen) goto err;
memcpy(buf+len, ",", 1), len += 1;
}
ret = uuidSetEncode(buf+len, maxlen-len, cur);
if (ret < 0) goto err;
len += ret;
}
return len;
err:
return -1;
}
uuidSet* gtidSetFind(gtidSet* gtid_set, const char* uuid, size_t uuid_len) {
uuidSet *cur = gtid_set->header;
while(cur != NULL) {
if (cur->uuid_len == uuid_len &&
memcmp(cur->uuid, uuid, uuid_len) == 0) {
break;
}
cur = cur->next;
}
return cur;
}
gno_t gtidSetAdd(gtidSet* gtid_set, const char* uuid, size_t uuid_len,
gno_t start, gno_t end) {
uuidSet *cur = NULL;
if (gtid_set->cached &&
gtid_set->cached->uuid_len == uuid_len &&
memcmp(gtid_set->cached->uuid,uuid,uuid_len) == 0) {
/* Fast path: adding to cached uuidSet */
cur = gtid_set->cached;
} else {
cur = gtidSetFind(gtid_set, uuid, uuid_len);
}
if (cur == NULL) {
cur = uuidSetNew(uuid, uuid_len);
gtidSetAppend(gtid_set, cur);
}
gtid_set->cached = cur;
return uuidSetAdd(cur, start, end);
}
gno_t gtidSetRemove(gtidSet* gtid_set, const char *uuid, size_t uuid_len,
gno_t start, gno_t end) {
int removed = 0;
uuidSet *cur = gtid_set->header, *prev = NULL;
while(cur != NULL) {
if (cur->uuid_len == uuid_len &&
memcmp(cur->uuid, uuid, uuid_len) == 0) {
removed = uuidSetRemove(cur,start,end);
if (uuidSetCount(cur) == 0) {
if (gtid_set->current == cur) gtid_set->current = NULL;
if (prev) prev->next = cur->next;
if (gtid_set->header == cur) gtid_set->header = cur->next;
if (gtid_set->tail == cur) gtid_set->tail = prev;
uuidSetFree(cur);
cur = NULL;
}
break;
}
prev = cur;
cur = cur->next;
}
gtid_set->cached = cur;
return removed;
}
gno_t gtidSetMerge(gtidSet* dst, gtidSet* src) {
if (src == NULL) return 0;
gno_t added = 0;
uuidSet *src_uuid_set = src->header, *dst_uuid_set;
while(src_uuid_set != NULL) {
dst_uuid_set = gtidSetFind(dst, src_uuid_set->uuid,
src_uuid_set->uuid_len);
if(dst_uuid_set != NULL) {
added += uuidSetMerge(dst_uuid_set, src_uuid_set);
} else {
added += gtidSetAppend(dst, uuidSetDup(src_uuid_set));
}
src_uuid_set = src_uuid_set->next;
}
return added;
}
gno_t gtidSetDiff(gtidSet* dst, gtidSet* src) {
if (src == NULL) return 0;
gno_t removed = 0;
uuidSet *src_uuid_set, *cur = dst->header, *prev = NULL, *next;
while(cur != NULL) {
next = cur->next;
src_uuid_set = gtidSetFind(src, cur->uuid, cur->uuid_len);
if(src_uuid_set) removed += uuidSetDiff(cur, src_uuid_set);
if (uuidSetCount(cur) == 0) {
/* delete uuid_set if it turns empty */
if (prev) prev->next = next;
if (dst->header == cur) dst->header = next;
if (dst->tail == cur) dst->tail = prev;
if (dst->cached == cur) dst->cached = NULL;
uuidSetFree(cur);
} else {
prev = cur;
}
cur = next;
}
return removed;
}
gno_t gtidSetNext(gtidSet* gtid_set, const char* uuid, size_t uuid_len,
int update) {
uuidSet *uuid_set = gtidSetFind(gtid_set, uuid, uuid_len);
if (uuid_set == NULL) {
if (update) {
uuid_set = uuidSetNew(uuid, uuid_len);
gtidSetAppend(gtid_set, uuid_set);
} else {
return GTID_GNO_INITIAL;
}
}
gtid_set->cached = uuid_set;
return uuidSetNext(uuid_set,update);
}
gno_t gtidSetCount(gtidSet *gtid_set) {
gno_t count = 0;
for (uuidSet *cur = gtid_set->header; cur != NULL; cur = cur->next) {
count += uuidSetCount(cur);
}
return count;
}
int gtidSetContains(gtidSet* gtid_set, const char* uuid, size_t uuid_len,
gno_t gno) {
uuidSet *uuid_set = gtidSetFind(gtid_set, uuid, uuid_len);
gtid_set->cached = uuid_set;
return uuid_set != NULL ? uuidSetContains(uuid_set,gno) : 0;
}
void gtidStatMerge(gtidStat *sum, gtidStat *one) {
sum->uuid_count += one->uuid_count;
sum->used_memory += one->used_memory;
sum->gap_count += one->gap_count;
sum->gno_count += one->gno_count;
}
int gtidSetEqual(gtidSet *set1, gtidSet *set2) {
gtidSet *set;
gno_t count;
set = gtidSetDup(set1);
gtidSetDiff(set,set2);
count = gtidSetCount(set);
gtidSetFree(set);
if (count) return 0;
set = gtidSetDup(set2);
gtidSetDiff(set,set1);
count = gtidSetCount(set);
gtidSetFree(set);
return count ? 0 : 1;
}
/* return 1 if set1 and set2 has common uuid */
int gtidSetRelated(gtidSet *set1, gtidSet *set2) {
uuidSet *uuid_set = set2->header;
while (uuid_set) {
if (gtidSetFind(set1,uuid_set->uuid,uuid_set->uuid_len)) return 1;
uuid_set = uuid_set->next;
}
return 0;
}
void uuidSetGetStat(uuidSet *uuid_set, gtidStat *stat) {
stat->uuid_count = 1;
stat->used_memory = uuid_set->intervals->node_count * GTID_INTERVAL_MEMORY;
stat->gap_count = uuid_set->intervals->node_count-1;
stat->gno_count = uuid_set->intervals->gno_count;
}
void gtidSetGetStat(gtidSet *gtid_set, gtidStat *stat) {
uuidSet *uuid_set = gtid_set->header;
memset(stat,0,sizeof(*stat));
while (uuid_set) {
gtidStat uuid_stat;
uuidSetGetStat(uuid_set, &uuid_stat);
gtidStatMerge(stat, &uuid_stat);
uuid_set = uuid_set->next;
}
}
/* Note that empty uuidSet might be added if needed */
void gtidSetCurrentUuidSetUpdate(gtidSet *gtid_set, const char *uuid,
size_t uuid_len) {
uuidSet *uuid_set = gtidSetFind(gtid_set,uuid,uuid_len);
if (uuid_set == NULL) {
uuid_set = uuidSetNew(uuid,uuid_len);
gtidSetAppend(gtid_set,uuid_set);
}
gtid_set->current = uuid_set;
}
const char *gtidAllocatorName() {
return GTID_ALLOC_LIB;
}
gtidSegment *gtidSegmentNew() {
gtidSegment *seg = gtid_malloc(sizeof(gtidSegment));
memset(seg,0,sizeof(gtidSegment));
seg->capacity = GTID_SEGMENT_NGNO_DEFAULT;
seg->deltas = gtid_malloc(sizeof(segoff_t)*GTID_SEGMENT_NGNO_DEFAULT);
return seg;
}
void gtidSegmentFree(gtidSegment *seg) {
if (seg == NULL) return;
if (seg->uuid) {
gtid_free(seg->uuid);
seg->uuid = NULL;
}
if (seg->deltas) {
gtid_free(seg->deltas);
seg->deltas = NULL;
}
gtid_free(seg);
}
void gtidSegmentReset(gtidSegment *seg, const char *uuid,
size_t uuid_len, gno_t base_gno, long long base_offset) {
if (seg->uuid_len != uuid_len || memcmp(seg->uuid, uuid, uuid_len)) {
if (seg->uuid) gtid_free(seg->uuid);
uuidDup(&seg->uuid,&seg->uuid_len,uuid,uuid_len);
}
seg->base_offset = base_offset;
seg->base_gno = base_gno;
seg->tgno = 0;
seg->ngno = 0;
}
void gtidSegmentAppend(gtidSegment *seg, long long offset) {
size_t delta = offset - seg->base_offset;
assert(delta >= 0 && delta <= SEGOFF_MAX);
assert(seg->ngno <= seg->capacity);
if (seg->ngno == seg->capacity) {
seg->capacity *= 2;
seg->deltas = gtid_realloc(seg->deltas,sizeof(segoff_t)*seg->capacity);
}
seg->deltas[seg->ngno++] = delta;
}
gtidSeq *gtidSeqCreate() {
gtidSeq *seq = gtid_malloc(sizeof(struct gtidSeq));
seq->segment_size = SEGMENT_SIZE;
seq->nsegment = 0;
seq->nfreeseg = 0;
seq->nsegment_deltas = 0;
seq->nfreeseg_deltas = 0;
seq->firstseg = NULL;
seq->lastseg = NULL;
seq->freeseg = NULL;
return seq;
}
void gtidSeqDestroy(gtidSeq *seq) {
gtidSegment *seg, *next;
if (seq == NULL) return;
seg = seq->firstseg;
while (seg) {
next = seg->next;
seq->nsegment--;
seq->nsegment_deltas -= seg->capacity;
gtidSegmentFree(seg);
seg = next;
}
seq->firstseg = NULL;
seq->lastseg = NULL;
assert(seq->nsegment == 0 && seq->nsegment_deltas == 0);
seg = seq->freeseg;
while (seg) {
next = seg->next;
seq->nfreeseg--;
seq->nfreeseg_deltas -= seg->capacity;
gtidSegmentFree(seg);
seg = next;
}
seq->freeseg = NULL;
assert(seq->nfreeseg == 0 && seq->nfreeseg_deltas == 0);
gtid_free(seq);
}
static inline
gtidSegment *gtidSeqSwitchSegment(gtidSeq *seq, const char *uuid,
size_t uuid_len, gno_t base_gno, long long base_offset) {
gtidSegment *seg = NULL;
if (seq->freeseg) {
/* unlink seg from free list */
seg = seq->freeseg;
seq->freeseg = seg->next;
assert(seg->prev == NULL);
seg->next = NULL;
seq->nfreeseg--;
seq->nfreeseg_deltas -= seg->capacity;
} else {
/* create new seg */
seg = gtidSegmentNew();
}
gtidSegmentReset(seg,uuid,uuid_len,base_gno,base_offset);
seg->prev = seq->lastseg;
if (!seq->firstseg) seq->firstseg = seg;
if (seq->lastseg) seq->lastseg->next = seg;
seq->lastseg = seg;
seq->nsegment++;
seq->nsegment_deltas += seg->capacity;
return seg;
}
void gtidSeqAppend(gtidSeq *seq, const char *uuid, size_t uuid_len,
gno_t gno, long long offset) {
gtidSegment *lastseg = seq->lastseg;
if (lastseg) {
long long tail_offset;
assert(lastseg->ngno > 0);
tail_offset = lastseg->base_offset + lastseg->deltas[lastseg->ngno-1];