forked from vk-com/kphp-kdb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlists-data.c
7660 lines (6613 loc) · 217 KB
/
lists-data.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
/*
This file is part of VK/KittenPHP-DB-Engine.
VK/KittenPHP-DB-Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
VK/KittenPHP-DB-Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with VK/KittenPHP-DB-Engine. If not, see <http://www.gnu.org/licenses/>.
This program is released under the GPL with the additional exemption
that compiling, linking, and/or using OpenSSL is allowed.
You are free to remove this exemption from derived works.
Copyright 2010-2013 Vkontakte Ltd
2010-2013 Nikolai Durov
2010-2013 Andrei Lopatin
2011-2013 Vitaliy Valtman
*/
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <aio.h>
#include "net-connections.h"
#include "net-aio.h"
#include "kdb-binlog-common.h"
#include "kdb-data-common.h"
#include "kdb-lists-binlog.h"
#include "lists-data.h"
#include "lists-index-layout.h"
#include "server-functions.h"
#include "am-hash.h"
#include "vv-tl-aio.h"
#ifdef DEBUG
# define inline
# define static
#endif
extern int now;
extern int verbosity;
extern int ignore_mode;
#define MAXINT (0x7fffffff)
#define MAXLONG (0x7fffffffffffffffLL)
#ifdef LISTS64
# define MAX_OBJECT_ID MAXLONG
# define MAX_OBJECT_RES (MAX_RES / 2)
# define MAX_LIST_ID MAXLONG
// #define Y_MULT 7047438495421301423LL
// #define Y_MULT_INV 8177354108323140687LL
# define idout "%lld"
# define out_list_id(x) (x)
# define out_object_id(x) (x)
# define FIRST_INT(x) ((int)(x))
# define MAX_LIST_OBJECT_PAIR max_list_object_pair
ltree_x_t max_list_object_pair = {.list_id = (unsigned long long)-1LL, .object_id = MAXLONG};
#elif defined (LISTS_Z)
# define MAX_OBJECT_RES (MAX_RES / object_id_ints)
# define MAX_OBJECT_ID max_object_id
# define MAX_LIST_ID max_list_id
# define MAX_LIST_OBJECT_PAIR max_list_object_pair
# define idout "%s"
extern char *out_list_id (list_id_t list_id);
extern char *out_object_id (object_id_t object_id);
# define FIRST_INT(x) ((x)[0])
#else
# define MAX_OBJECT_ID MAXINT
# define MAX_OBJECT_RES MAX_RES
# define MAX_LIST_OBJECT_PAIR (MAXLONG)
# define MAX_LIST_ID MAXINT
# define Y_MULT 1640859641
# define Y_MULT_INV 265849417
# define idout "%d"
# define out_list_id(x) (x)
# define out_object_id(x) (x)
# define FIRST_INT(x) (x)
#endif
#ifdef LISTS_Z
# define IF_LISTS_Z(c) (c)
#else
# define IF_LISTS_Z(c) (1)
#endif
#ifdef VALUES64
#define valout "%lld"
#else
#define valout "%d"
#endif
#define MIN_OBJECT_ID (~MAX_OBJECT_ID)
int lists_prime;
int max_lists;
int index_mode;
int remove_dates;
int now_override;
extern int metafile_mode;
int revlist_metafile_mode;
int crc32_check_mode;
extern char metafiles_order;
const char empty_string[] = {0};
int value_offset = -1;
extern int max_text_len;
int object_id_ints = 0, list_id_ints = 0;
#ifdef LISTS_Z
int object_list_ints = 0, ltree_node_size = -1, object_id_bytes = 0, list_id_bytes = 0, object_list_bytes = 0;
#define list_object_bytes object_list_bytes
#define list_object_ints object_list_ints
#define COPY_LIST_ID(dest,src) memcpy ((dest), (src), list_id_bytes)
int payload_offset = -1, tree_ext_small_node_size = -1, tree_ext_large_node_size = -1;
int tree_ext_global_node_size = -1;
int list_struct_size = -1;
int cyclic_buffer_entry_size = -1, cyclic_buffer_entry_ints = -1;
int file_list_index_entry_size = -1;
int max_object_id[MAX_OBJECT_ID_INTS];
int max_list_id[MAX_LIST_ID_INTS];
int max_list_object_pair[MAX_OBJECT_ID_INTS + MAX_LIST_ID_INTS];
#define PAYLOAD(T) ((struct tree_payload *)(((char *)(T)) + payload_offset))
#define OARR_ENTRY(A,i) ((A) + object_id_ints * (long) (i))
#define OARR_ENTRY_ADJ(A,i) ((A) + object_id_ints_adjusted * (long) (i))
#define check_debug_list(list_id) (0)
#define check_ignore_list(list_id) (0)
#define FLI_ADJUST(p) ((struct file_list_index_entry *) ((char *) (p) + list_id_bytes))
#define FLI_ENTRY(i) ((char *) FileLists + (long) (i) * file_list_index_entry_size)
#define FLI_ENTRY_ADJUSTED(i) ((struct file_list_index_entry *) (FileLists_adjusted + (long) (i) * file_list_index_entry_size))
#define FLI_LIST_ID(p) ((list_id_t) (p))
#define NEW_FLI_ENTRY(i) ((char *) NewFileLists + (long) (i) * file_list_index_entry_size)
#define NEW_FLI_ENTRY_ADJUSTED(i) ((struct file_list_index_entry *) (NewFileLists_adjusted + (long) (i) * file_list_index_entry_size))
#define MF_ADJUST(p) ((metafile_t *) ((char *) (p) + list_id_bytes))
#define MF_READJUST(p) ((metafile_t *) ((char *) (p) - list_id_bytes))
#define MF_MAGIC(p) (MF_READJUST(p)->magic)
#define MF_LIST_ID(p) (MF_READJUST(p)->list_id)
#define REVLIST_PAIR(RData,i) ((int *) ((char *) RData + (long) (i) * object_list_bytes))
#define REVLIST_OBJECT_ID(RData,i) ((int *) ((char *) REVLIST_PAIR(RData,i) + list_id_bytes))
#define REVLIST_LIST_ID(RData,i) REVLIST_PAIR(RData,i)
#define lev_list_id_bytes list_id_bytes
#define lev_list_object_bytes object_list_bytes
#define lev_object_id_bytes object_id_bytes
#define LEV_OBJECT_ID(E) ((int *) ((char *) (E) + list_id_bytes + 4))
#define LEV_ADJUST_LO(E) ((void *) ((char *) (E) + list_object_bytes))
#define LEV_ADJUST_L(E) ((void *) ((char *) (E) + list_id_bytes))
#else
#define COPY_LIST_ID(dest,src) (dest) = (src)
#define ltree_node_size (sizeof (ltree_t))
#define tree_ext_small_node_size (sizeof (tree_ext_small_t))
#define tree_ext_global_node_size (sizeof (tree_ext_global_t))
#define tree_ext_large_node_size (sizeof (tree_ext_large_t))
#define list_struct_size (sizeof (list_t))
#define PAYLOAD(T) (&((T)->payload))
#define OARR_ENTRY(A,i) ((A)[(i)])
#define OARR_ENTRY_ADJ(A,i) ((A)[(i)*object_id_ints_adjusted])
#define cyclic_buffer_entry_size (sizeof (struct cyclic_buffer_entry))
#define cyclic_buffer_entry_ints (sizeof (struct cyclic_buffer_entry) / 4)
#define file_list_index_entry_size (sizeof (struct file_list_index_entry))
#define check_debug_list(list_id) ((list_id) == debug_list_id)
#define check_ignore_list(list_id) ((list_id) == ignored_list2)
#define FLI_ADJUST(p) (p)
#define FLI_ENTRY(i) (FileLists + (i))
#define FLI_ENTRY_ADJUSTED(i) (FLI_ADJUST(FLI_ENTRY(i)))
#define FLI_LIST_ID(p) ((p)->list_id)
#define NEW_FLI_ENTRY(i) (NewFileLists + (i))
#define NEW_FLI_ENTRY_ADJUSTED(i) (FLI_ADJUST(NEW_FLI_ENTRY(i)))
#define MF_ADJUST(p) (p)
#define MF_READJUST(p) (p)
#define MF_MAGIC(p) ((p)->magic)
#define MF_LIST_ID(p) ((p)->list_id)
#define REVLIST_PAIR(RData,i) RData[i].pair
#define REVLIST_OBJECT_ID(RData,i) RData[i].object_id
#define REVLIST_LIST_ID(RData,i) RData[i].list_id
#define lev_list_id_bytes 0
#define lev_list_object_bytes 0
#define lev_object_id_bytes 0
#define LEV_OBJECT_ID(E) ((E)->object_id)
#define LEV_ADJUST_LO(E) (E)
#define LEV_ADJUST_L(E) (E)
#endif
#define LPAYLOAD(T) (PAYLOAD(LARGE_NODE(T)))
#define FLI_ENTRY_LIST_ID(i) (FLI_LIST_ID(FLI_ENTRY(i)))
#define NEW_FLI_ENTRY_LIST_ID(i) (FLI_LIST_ID(NEW_FLI_ENTRY(i)))
long long malloc_memory;
extern int disable_revlist;
void *zzmalloc (int size) {
malloc_memory += size;
void *r = malloc (size);
assert (r);
return r;
}
void zzfree (void *ptr, int size) {
malloc_memory -= size;
free (ptr);
}
int *CB;
int id_ints;
/* ------ compute struct sizes -------- */
static void compute_struct_sizes (void) {
assert (list_id_ints > 0 && list_id_ints <= MAX_LIST_ID_INTS);
assert (object_id_ints > 0 && object_id_ints <= MAX_OBJECT_ID_INTS);
// compute binlog record sizes
// compute memory structure sizes
#ifdef LISTS_Z
int i;
object_list_ints = object_id_ints + list_id_ints;
ltree_node_size = sizeof (ltree_t) + object_list_ints * 4;
object_id_bytes = object_id_ints * 4;
list_id_bytes = list_id_ints * 4;
object_list_bytes = object_id_bytes + list_id_bytes;
list_struct_size = offsetof (list_t, list_id) + list_id_bytes;
payload_offset = tree_ext_small_node_size = offsetof (tree_ext_small_t, x) + object_id_bytes;
for (i = 0; i < object_id_ints; i++) {
max_object_id[i] = MAXINT;
}
for (i = 0; i < list_id_ints; i++) {
max_list_id[i] = MAXINT;
}
for (i = 0; i < object_id_ints + list_id_ints; i++) {
max_list_object_pair[i] = MAXINT;
}
#ifdef _LP64
if (payload_offset & 4) {
payload_offset += 4;
}
#endif
tree_ext_global_node_size = offsetof (tree_ext_global_t, z) + object_id_bytes;
tree_ext_large_node_size = payload_offset + sizeof (struct tree_payload);
cyclic_buffer_entry_size = sizeof (struct cyclic_buffer_entry) + list_id_bytes;
cyclic_buffer_entry_ints = (cyclic_buffer_entry_size >> 2);
file_list_index_entry_size = sizeof (struct file_list_index_entry) + list_id_bytes;
#endif
// compute offsets for data access
assert (!CB);
CB = zzmalloc (CYCLIC_BUFFER_SIZE * cyclic_buffer_entry_size);
assert (CB);
}
int get_text_len (const char *text) {
int a = *(unsigned char *)text;
if (a <= 253) {
return a;
}
assert (a == 254);
return (*(int *)text) & 0x00ffffff;
}
char *get_text_ptr (char *text) {
// int len = get_text_len (text);
return (*(unsigned char *)text) == 0xfe ? text + 4 : text + 1;
}
int get_text_data_len (const char *text) {
int len = get_text_len (text);
return len <= 253 ? len + 1 : len + 4;
}
/* --------- debug output --------------- */
#ifdef LISTS_Z
static inline char *out_int_vector (const int *A, int len) {
static char buff[65536];
static char *wptr = buff;
int s = len * 12, i;
if (wptr + s > buff + 65536) {
wptr = buff;
}
char *res = wptr;
assert (len > 0 && s < 65536);
for (i = 0; i < len; i++) {
wptr += sprintf (wptr, "%d:", A[i]);
}
wptr[-1] = 0;
return res;
}
char *out_list_id (list_id_t list_id) {
return out_int_vector (list_id, list_id_ints);
}
char *out_object_id (object_id_t object_id) {
return out_int_vector (object_id, object_id_ints);
}
#endif
/* --------- trees with 64-bit key ------ */
int alloc_ltree_nodes;
#ifdef LISTS64
static inline int ltree_x_less (ltree_x_t a, ltree_x_t b) {
return a.object_id < b.object_id || (a.object_id == b.object_id && a.list_id < b.list_id);
}
static inline int ltree_x_compare (ltree_x_t a, ltree_x_t b) {
if (a.object_id < b.object_id) {
return -1;
}
if (a.object_id > b.object_id) {
return 1;
}
if (a.list_id < b.list_id) {
return -1;
}
if (a.list_id > b.list_id) {
return 1;
}
return 0;
}
static inline int ltree_x_equal (ltree_x_t a, ltree_x_t b) {
return a.object_id == b.object_id && a.list_id == b.list_id;
}
#elif defined (LISTS_Z)
static inline int ltree_x_less (ltree_x_t a, ltree_x_t b) {
int i;
for (i = 0; i < object_list_ints; i++) {
if (a[i] < b[i]) {
return 1;
} else if (a[i] > b[i]) {
return 0;
}
}
return 0;
}
static inline int ltree_x_compare (ltree_x_t a, ltree_x_t b) {
int i;
for (i = 0; i < object_list_ints; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
static inline int ltree_x_equal (ltree_x_t a, ltree_x_t b) {
int i;
for (i = 0; i < object_list_ints; i++) {
if (a[i] != b[i]) {
return 0;
}
}
return 1;
}
#else
static inline int ltree_x_less (ltree_x_t a, ltree_x_t b) {
return a < b;
}
static inline int ltree_x_compare (ltree_x_t a, ltree_x_t b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
static inline int ltree_x_equal (ltree_x_t a, ltree_x_t b) {
return a == b;
}
#endif
static inline void combine_ltree_x (list_id_t list_id, object_id_t object_id, var_ltree_x_t *ltx) {
#ifdef LISTS64
ltx->list_id = list_id;
ltx->object_id = object_id;
#elif (defined (LISTS_Z))
memcpy (*ltx, object_id, object_id_bytes);
memcpy (((int *)(*ltx)) + object_id_ints, list_id, list_id_bytes);
#else
*ltx = (((long long) object_id) << 32) + (unsigned) list_id;
#endif
}
static inline object_id_t ltree_x_object_id (ltree_x_t ltx) {
#ifdef LISTS64
return ltx.object_id;
#elif (defined (LISTS_Z))
return (int *)ltx;
#else
return (object_id_t)(ltx >> 32);
#endif
}
static inline list_id_t ltree_x_list_id (ltree_x_t ltx) {
#ifdef LISTS64
return ltx.list_id;
#elif (defined (LISTS_Z))
return ((int *)ltx) + object_id_ints;
#else
return (list_id_t)(ltx);
#endif
}
#ifdef LISTS64
static inline void upcopy_list_id (void *E, list_id_t list_id) {
((int *)E)[2] = (int)(list_id >> 32);
}
static inline void upcopy_object_id (void *E, object_id_t object_id) {
((int *)E)[2] = (int)(object_id >> 32);
}
static inline void upcopy_list_object_id (void *E, list_id_t list_id, object_id_t object_id) {
((int *)E)[2] = (int)(list_id >> 32);
((struct lev_new_entry *)E)->object_id = object_id;
}
#elif (defined (LISTS_Z))
static inline void upcopy_list_id (void *E, list_id_t list_id) {
memcpy (((struct lev_new_entry *)E)->list_id + 1, list_id + 1, list_id_bytes - 4);
}
static inline void upcopy_object_id (void *E, object_id_t object_id) {
memcpy (((struct lev_del_obj *)E)->object_id + 1, object_id + 1, object_id_bytes - 4);
}
static inline void upcopy_list_object_id (void *E, list_id_t list_id, object_id_t object_id) {
memcpy (((struct lev_new_entry *)E)->list_id + 1, list_id + 1, list_id_bytes - 4);
memcpy (((struct lev_new_entry *)E)->list_id + list_id_ints, object_id, object_id_bytes);
}
#else
static inline void upcopy_list_id (void *E, list_id_t list_id) {
}
static inline void upcopy_object_id (void *E, list_id_t list_id) {
}
static inline void upcopy_list_object_id (void *E, list_id_t list_id, object_id_t object_id) {
((struct lev_new_entry *)E)->object_id = object_id;
}
#endif
static ltree_t *new_ltree_node (ltree_x_t x, int y) {
ltree_t *P = zmalloc (ltree_node_size);
assert (P);
alloc_ltree_nodes++;
P->left = P->right = 0;
P->y = y;
#ifndef LISTS_Z
P->x = x;
#else
memcpy (P->x, x, object_list_bytes);
#endif
return P;
}
static void free_ltree_node (ltree_t *T) {
assert (--alloc_ltree_nodes >= 0);
zfree (T, ltree_node_size);
}
static ltree_t *ltree_lookup (ltree_t *T, ltree_x_t x) {
while (T) {
int c = ltree_x_compare (x, T->x);
if (!c) {
return T;
}
T = (c > 0 ? T->right : T->left);
}
return T;
}
static ltree_t *ltree_insert (ltree_t *T, ltree_x_t x, int y) {
ltree_t *Root = T, **U = &Root, **L, **R;
while (T && T->y >= y) {
U = ltree_x_less (x, T->x) ? &T->left : &T->right;
T = *U;
}
*U = new_ltree_node (x, y);
L = &(*U)->left;
R = &(*U)->right;
while (T) {
if (ltree_x_less (x, T->x)) {
*R = T;
R = &T->left;
T = *R;
} else {
*L = T;
L = &T->right;
T = *L;
}
}
*L = *R = 0;
return Root;
}
static ltree_t *ltree_merge (ltree_t *L, ltree_t *R) {
ltree_t *Root, **U = &Root;
while (L && R) {
if (L->y > R->y) {
*U = L;
U = &L->right;
L = *U;
} else {
*U = R;
U = &R->left;
R = *U;
}
}
*U = L ? L : R;
return Root;
}
static ltree_t *ltree_delete (ltree_t *T, ltree_x_t x) {
if (!T) {
return 0;
}
ltree_t *Root = T, **U = &Root, *L, *R;
if (!T) {
return 0;
}
int r;
while ((r = ltree_x_compare (x, T->x)) != 0) {
U = (r < 0) ? &T->left : &T->right;
T = *U;
if (!T) {
return Root;
}
}
L = T->left;
R = T->right;
free_ltree_node (T);
while (L && R) {
if (L->y > R->y) {
*U = L;
U = &L->right;
L = *U;
} else {
*U = R;
U = &R->left;
R = *U;
}
}
*U = L ? L : R;
return Root;
}
/* ========= LISTREE FUNCTIONS ======== */
char *MData, *MDataEnd;
#define NIL ((tree_ext_small_t *)&NIL_NODE)
#define NILG ((tree_ext_global_t *)&NIL_NODE)
#define NILL ((tree_ext_large_t *)&NIL_NODE)
#define NILX ((tree_ext_xglobal_t *)&NIL_NODE)
const static struct tree_ext_small NIL_NODE = {
.left = NIL,
.right = NIL,
.y = -1 << 31,
};
int alloc_large_nodes, alloc_global_nodes, alloc_small_nodes;
static int rpos_to_delta[4] = {0, 1, 0, -1}; // sin (k*pi/2)
#define NODE_TYPE(p) ((p)->rpos & 3)
#define NODE_RPOS(p) ((p)->rpos >> 2)
#define MAKE_RPOS(a,b) (((a)<<2) + (b))
#define LARGE_NODE(T) ((tree_ext_large_t *)(T))
#define GLOBAL_NODE(T) ((tree_ext_global_t *)(T))
#define SMALL_NODE(T) ((tree_ext_small_t *)(T))
static inline tree_ext_small_t *new_tree_subnode_small (object_id_t x, int y, int rpos) {
tree_ext_small_t *P;
P = zmalloc (tree_ext_small_node_size);
assert (P);
alloc_small_nodes++;
P->left = P->right = NIL;
P->y = y;
P->rpos = rpos;
// P->delta = rpos_to_delta[NODE_RPOS(p)];
#ifdef LISTS_Z
memcpy (P->x, x, object_id_bytes);
#else
P->x = x;
#endif
return P;
}
static inline tree_ext_large_t *new_tree_subnode_large (object_id_t x, int y, int rpos) {
tree_ext_large_t * P;
P = zmalloc (tree_ext_large_node_size);
assert (P);
alloc_large_nodes++;
P->left = P->right = NILL;
P->y = y;
P->rpos = rpos;
// P->delta = rpos_to_delta[NODE_RPOS(p)];
#ifdef LISTS_Z
memcpy (P->x, x, object_id_bytes);
#else
P->x = x;
#endif
memset (PAYLOAD (P), 0, sizeof (struct tree_payload));
return P;
}
#ifdef LISTS_Z
static inline int object_id_less (object_id_t a, object_id_t b) {
int i;
for (i = 0; i < object_id_ints; i++) {
if (a[i] < b[i]) {
return 1;
} else if (a[i] > b[i]) {
return 0;
}
}
return 0;
}
static inline int object_id_less_prefix (object_id_t a, object_id_t b) {
int i;
for (i = 0; i < id_ints; i++) {
if (a[i] < b[i]) {
return 1;
} else if (a[i] > b[i]) {
return 0;
}
}
return 0;
}
static inline int object_id_compare (object_id_t a, object_id_t b) {
int i;
for (i = 0; i < object_id_ints; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
static inline int object_id_compare_prefix (object_id_t a, object_id_t b) {
int i;
for (i = 0; i < id_ints; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
static inline int object_id_equal (object_id_t a, object_id_t b) {
int i;
for (i = 0; i < object_id_ints; i++) {
if (a[i] != b[i]) {
return 0;
}
}
return 1;
}
static inline int list_id_less (list_id_t a, list_id_t b) {
int i;
for (i = 0; i < list_id_ints; i++) {
if (a[i] < b[i]) {
return 1;
} else if (a[i] > b[i]) {
return 0;
}
}
return 0;
}
static inline int list_id_compare (list_id_t a, list_id_t b) {
int i;
for (i = 0; i < list_id_ints; i++) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
}
}
return 0;
}
static inline int list_id_equal (list_id_t a, list_id_t b) {
int i;
for (i = 0; i < list_id_ints; i++) {
if (a[i] != b[i]) {
return 0;
}
}
return 1;
}
#else
static inline int object_id_less (object_id_t a, object_id_t b) {
return a < b;
}
static inline int object_id_less_prefix (object_id_t a, object_id_t b) {
return a < b;
}
static inline int object_id_compare (object_id_t a, object_id_t b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
static inline int object_id_compare_prefix (object_id_t a, object_id_t b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
static inline int object_id_equal (object_id_t a, object_id_t b) {
return a == b;
}
static inline int list_id_less (list_id_t a, list_id_t b) {
return a < b;
}
static inline int list_id_compare (list_id_t a, list_id_t b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
static inline int list_id_equal (list_id_t a, list_id_t b) {
return a == b;
}
#endif
static inline tree_ext_small_t *tree_ext_lookup (tree_ext_small_t *T, object_id_t x) {
int p = 0;
while (T != NIL && (p = object_id_compare (x, T->x)) != 0) {
T = (p < 0) ? T->left : T->right;
}
return T;
}
static inline tree_ext_small_t *tree_ext_adjust_deltas (tree_ext_small_t *T, object_id_t x, int delta_incr) {
int p;
while (T != NIL && (p = object_id_compare (x, T->x)) != 0) {
T->delta += delta_incr;
T = (p < 0) ? T->left : T->right;
}
assert (T != NIL);
T->delta += delta_incr;
return T;
}
static inline void tree_ext_relax (tree_ext_small_t *T) {
T->delta = T->left->delta + T->right->delta + rpos_to_delta[NODE_TYPE(T)];
}
static void tree_ext_split (tree_ext_small_t **L, tree_ext_small_t **R, tree_ext_small_t *T, object_id_t x) {
if (T == NIL) { *L = *R = NIL; return; }
if (object_id_less (x, T->x)) {
*R = T;
tree_ext_split (L, &T->left, T->left, x);
} else {
*L = T;
tree_ext_split (&T->right, R, T->right, x);
}
tree_ext_relax (T);
}
static tree_ext_small_t *tree_ext_insert (tree_ext_small_t *T, object_id_t x, int y, tree_ext_small_t *N) {
if (T->y > y) {
if (object_id_less (x, T->x)) {
T->left = tree_ext_insert (T->left, x, y, N);
} else {
// assert (x > T->x);
T->right = tree_ext_insert (T->right, x, y, N);
}
tree_ext_relax (T);
return T;
}
assert (object_id_equal (N->x, x) && N->y == y);
tree_ext_split (&N->left, &N->right, T, x);
tree_ext_relax (N);
return N;
}
static tree_ext_small_t *tree_ext_merge (tree_ext_small_t *L, tree_ext_small_t *R) {
if (L == NIL) { return R; }
if (R == NIL) { return L; }
if (L->y > R->y) {
L->right = tree_ext_merge (L->right, R);
tree_ext_relax (L);
return L;
} else {
R->left = tree_ext_merge (L, R->left);
tree_ext_relax (R);
return R;
}
}
static tree_ext_small_t *DeletedSubnode;
static tree_ext_small_t *tree_ext_delete (tree_ext_small_t *T, object_id_t x) {
assert (T != NIL);
int p = object_id_compare (x, T->x);
if (!p) {
tree_ext_small_t *N = tree_ext_merge (T->left, T->right);
DeletedSubnode = T;
return N;
}
if (p < 0) {
T->left = tree_ext_delete (T->left, x);
} else {
T->right = tree_ext_delete (T->right, x);
}
tree_ext_relax (T);
return T;
}
static inline void free_tree_ext_small_node (tree_ext_small_t *T) {
alloc_small_nodes--;
zfree (T, tree_ext_small_node_size);
}
static void free_tree_ext_small (tree_ext_small_t *T) {
if (T != NIL) {
free_tree_ext_small (T->left);
free_tree_ext_small (T->right);
free_tree_ext_small_node (T);
}
}
static inline void free_tree_ext_large_node (tree_ext_large_t *T) {
int tp = NODE_TYPE (T);
struct tree_payload *P = PAYLOAD (T);
if (tp == TF_ZERO || tp == TF_PLUS) {
if (P->text && P->text != empty_string) {
assert (P->text >= MDataEnd);
zfree (P->text, get_text_data_len (P->text));
}
P->text = 0;
} else {
assert (!P->text);
}
alloc_large_nodes--;
zfree (T, tree_ext_large_node_size);
}
/* GLOBAL TREES */
#ifdef LISTS_GT
static inline tree_ext_global_t *new_tree_subnode_global (global_id_t x, int y, int rpos, object_id_t z) {
tree_ext_global_t *P;
P = zmalloc (tree_ext_global_node_size);
assert (P);
alloc_global_nodes++;
P->left = P->right = NILG;
P->y = y;
P->rpos = rpos;
P->x = x;
#ifdef LISTS_Z
memcpy (P->z, z, object_id_bytes);
#else
P->z = z;
#endif
// P->delta = rpos_to_delta[NODE_RPOS(p)];
return P;
}
static inline tree_ext_global_t *tree_ext_global_lookup (tree_ext_global_t *T, global_id_t x) {
while (T != NILG && x != T->x) {
T = (x < T->x) ? T->left : T->right;
}
return T;
}
static inline tree_ext_global_t *tree_ext_global_adjust_deltas (tree_ext_global_t *T, global_id_t x, int delta_incr) {
while (T != NILG && x != T->x) {
T->delta += delta_incr;
T = (x < T->x) ? T->left : T->right;
}
assert (T != NILG);
T->delta += delta_incr;
return T;
}
static inline void tree_ext_global_relax (tree_ext_global_t *T) {
T->delta = T->left->delta + T->right->delta + rpos_to_delta[NODE_TYPE(T)];
}
static void tree_ext_global_split (tree_ext_global_t **L, tree_ext_global_t **R, tree_ext_global_t *T, global_id_t x) {
if (T == NILG) { *L = *R = NILG; return; }
if (x < T->x) {
*R = T;
tree_ext_global_split (L, &T->left, T->left, x);
} else {
*L = T;
tree_ext_global_split (&T->right, R, T->right, x);
}
tree_ext_global_relax (T);
}
static tree_ext_global_t *tree_ext_global_insert (tree_ext_global_t *T, global_id_t x, int y, tree_ext_global_t *N) {
if (T->y > y) {
if (x < T->x) {
T->left = tree_ext_global_insert (T->left, x, y, N);
} else {
// assert (x > T->x);
T->right = tree_ext_global_insert (T->right, x, y, N);
}
tree_ext_global_relax (T);
return T;
}
assert (N->x == x && N->y == y);
tree_ext_global_split (&N->left, &N->right, T, x);
tree_ext_global_relax (N);
return N;
}
static tree_ext_global_t *tree_ext_global_merge (tree_ext_global_t *L, tree_ext_global_t *R) {
if (L == NILG) { return R; }
if (R == NILG) { return L; }