-
Notifications
You must be signed in to change notification settings - Fork 271
/
squat-trie.c
2095 lines (1825 loc) · 52.5 KB
/
squat-trie.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) 2007-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "str.h"
#include "read-full.h"
#include "istream.h"
#include "ostream.h"
#include "unichar.h"
#include "nfs-workarounds.h"
#include "file-cache.h"
#include "seq-range-array.h"
#include "squat-uidlist.h"
#include "squat-trie-private.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#define DEFAULT_NORMALIZE_MAP_CHARS \
"EOTIRSACDNLMVUGPHBFWYXKJQZ0123456789@.-+#$%_&"
#define DEFAULT_PARTIAL_LEN 4
#define DEFAULT_FULL_LEN 4
#define MAX_FAST_LEVEL 3
#define SEQUENTIAL_COUNT 46
#define TRIE_BYTES_LEFT(n) \
((n) * SQUAT_PACK_MAX_SIZE)
#define TRIE_READAHEAD_SIZE \
I_MAX(4096, 1 + 256 + TRIE_BYTES_LEFT(256))
struct squat_trie_build_context {
struct squat_trie *trie;
struct ostream *output;
struct squat_uidlist_build_context *uidlist_build_ctx;
struct file_lock *file_lock;
struct dotlock *dotlock;
uint32_t first_uid;
unsigned int compress_nodes:1;
};
struct squat_trie_iterate_node {
struct squat_node *node;
ARRAY_TYPE(seq_range) shifts;
unsigned int idx;
};
struct squat_trie_iterate_context {
struct squat_trie *trie;
struct squat_trie_iterate_node cur;
ARRAY(struct squat_trie_iterate_node) parents;
bool failed;
};
static int squat_trie_map(struct squat_trie *trie, bool building);
void squat_trie_delete(struct squat_trie *trie)
{
i_unlink_if_exists(trie->path);
squat_uidlist_delete(trie->uidlist);
}
static void squat_trie_set_corrupted(struct squat_trie *trie)
{
trie->corrupted = TRUE;
i_error("Corrupted file %s", trie->path);
squat_trie_delete(trie);
}
static void squat_trie_normalize_map_build(struct squat_trie *trie)
{
static unsigned char valid_chars[] =
DEFAULT_NORMALIZE_MAP_CHARS;
unsigned int i, j;
memset(trie->default_normalize_map, 0,
sizeof(trie->default_normalize_map));
#if 1
for (i = 0, j = 1; i < sizeof(valid_chars)-1; i++) {
unsigned char chr = valid_chars[i];
if (chr >= 'A' && chr <= 'Z')
trie->default_normalize_map[chr-'A'+'a'] = j;
trie->default_normalize_map[chr] = j++;
}
i_assert(j <= SEQUENTIAL_COUNT);
for (i = 128; i < 256; i++)
trie->default_normalize_map[i] = j++;
#else
for (i = 0; i < sizeof(valid_chars)-1; i++) {
unsigned char chr = valid_chars[i];
if (chr >= 'A' && chr <= 'Z')
trie->default_normalize_map[chr-'A'+'a'] = chr;
trie->default_normalize_map[chr] = chr;
}
for (i = 128; i < 256; i++)
trie->default_normalize_map[i] = i_toupper(i);
#endif
}
static void node_free(struct squat_trie *trie, struct squat_node *node)
{
struct squat_node *children;
unsigned int i;
if (node->leaf_string_length > 0) {
if (NODE_IS_DYNAMIC_LEAF(node))
i_free(node->children.leaf_string);
} else if (!node->children_not_mapped) {
children = NODE_CHILDREN_NODES(node);
trie->node_alloc_size -=
NODE_CHILDREN_ALLOC_SIZE(node->child_count);
for (i = 0; i < node->child_count; i++)
node_free(trie, &children[i]);
i_free(node->children.data);
}
}
struct squat_trie *
squat_trie_init(const char *path, uint32_t uidvalidity,
enum file_lock_method lock_method, enum squat_index_flags flags,
mode_t mode, gid_t gid)
{
struct squat_trie *trie;
trie = i_new(struct squat_trie, 1);
trie->path = i_strdup(path);
trie->uidlist = squat_uidlist_init(trie);
trie->fd = -1;
trie->lock_method = lock_method;
trie->uidvalidity = uidvalidity;
trie->flags = flags;
trie->create_mode = mode;
trie->create_gid = gid;
squat_trie_normalize_map_build(trie);
trie->dotlock_set.use_excl_lock =
(flags & SQUAT_INDEX_FLAG_DOTLOCK_USE_EXCL) != 0;
trie->dotlock_set.nfs_flush = (flags & SQUAT_INDEX_FLAG_NFS_FLUSH) != 0;
trie->dotlock_set.timeout = SQUAT_TRIE_LOCK_TIMEOUT;
trie->dotlock_set.stale_timeout = SQUAT_TRIE_DOTLOCK_STALE_TIMEOUT;
trie->default_partial_len = DEFAULT_PARTIAL_LEN;
trie->default_full_len = DEFAULT_FULL_LEN;
return trie;
}
static void squat_trie_close_fd(struct squat_trie *trie)
{
trie->data = NULL;
trie->data_size = 0;
if (trie->mmap_size != 0) {
if (munmap(trie->mmap_base, trie->mmap_size) < 0)
i_error("munmap(%s) failed: %m", trie->path);
trie->mmap_base = NULL;
trie->mmap_size = 0;
}
if (trie->fd != -1) {
if (close(trie->fd) < 0)
i_error("close(%s) failed: %m", trie->path);
trie->fd = -1;
}
}
static void squat_trie_close(struct squat_trie *trie)
{
trie->corrupted = FALSE;
node_free(trie, &trie->root);
memset(&trie->root, 0, sizeof(trie->root));
memset(&trie->hdr, 0, sizeof(trie->hdr));
squat_trie_close_fd(trie);
if (trie->file_cache != NULL)
file_cache_free(&trie->file_cache);
trie->locked_file_size = 0;
}
void squat_trie_deinit(struct squat_trie **_trie)
{
struct squat_trie *trie = *_trie;
*_trie = NULL;
squat_trie_close(trie);
squat_uidlist_deinit(trie->uidlist);
i_free(trie->path);
i_free(trie);
}
void squat_trie_set_partial_len(struct squat_trie *trie, unsigned int len)
{
trie->default_partial_len = len;
}
void squat_trie_set_full_len(struct squat_trie *trie, unsigned int len)
{
trie->default_full_len = len;
}
static void squat_trie_header_init(struct squat_trie *trie)
{
memset(&trie->hdr, 0, sizeof(trie->hdr));
trie->hdr.version = SQUAT_TRIE_VERSION;
trie->hdr.indexid = time(NULL);
trie->hdr.uidvalidity = trie->uidvalidity;
trie->hdr.partial_len = trie->default_partial_len;
trie->hdr.full_len = trie->default_full_len;
i_assert(sizeof(trie->hdr.normalize_map) ==
sizeof(trie->default_normalize_map));
memcpy(trie->hdr.normalize_map, trie->default_normalize_map,
sizeof(trie->hdr.normalize_map));
}
static int squat_trie_open_fd(struct squat_trie *trie)
{
trie->fd = open(trie->path, O_RDWR);
if (trie->fd == -1) {
if (errno == ENOENT) {
squat_trie_header_init(trie);
return 0;
}
i_error("open(%s) failed: %m", trie->path);
return -1;
}
if (trie->file_cache != NULL)
file_cache_set_fd(trie->file_cache, trie->fd);
return 0;
}
int squat_trie_open(struct squat_trie *trie)
{
squat_trie_close(trie);
if (squat_trie_open_fd(trie) < 0)
return -1;
return squat_trie_map(trie, FALSE);
}
static int squat_trie_is_file_stale(struct squat_trie *trie)
{
struct stat st, st2;
if ((trie->flags & SQUAT_INDEX_FLAG_NFS_FLUSH) != 0)
nfs_flush_file_handle_cache(trie->path);
if (nfs_safe_stat(trie->path, &st) < 0) {
if (errno == ENOENT)
return 1;
i_error("stat(%s) failed: %m", trie->path);
return -1;
}
if (fstat(trie->fd, &st2) < 0) {
if (errno == ESTALE)
return 1;
i_error("fstat(%s) failed: %m", trie->path);
return -1;
}
trie->locked_file_size = st2.st_size;
if (st.st_ino == st2.st_ino && CMP_DEV_T(st.st_dev, st2.st_dev)) {
i_assert(trie->locked_file_size >= trie->data_size);
return 0;
}
return 1;
}
int squat_trie_refresh(struct squat_trie *trie)
{
int ret;
ret = squat_trie_is_file_stale(trie);
if (ret > 0)
ret = squat_trie_open(trie);
return ret;
}
static int squat_trie_lock(struct squat_trie *trie, int lock_type,
struct file_lock **file_lock_r,
struct dotlock **dotlock_r)
{
int ret;
i_assert(trie->fd != -1);
*file_lock_r = NULL;
*dotlock_r = NULL;
for (;;) {
if (trie->lock_method != FILE_LOCK_METHOD_DOTLOCK) {
ret = file_wait_lock(trie->fd, trie->path, lock_type,
trie->lock_method,
SQUAT_TRIE_LOCK_TIMEOUT,
file_lock_r);
} else {
ret = file_dotlock_create(&trie->dotlock_set,
trie->path, 0, dotlock_r);
}
if (ret == 0) {
i_error("squat trie %s: Locking timed out", trie->path);
return 0;
}
if (ret < 0)
return -1;
/* if the trie has been compressed, we need to reopen the
file and try to lock again */
ret = squat_trie_is_file_stale(trie);
if (ret == 0)
break;
if (*file_lock_r != NULL)
file_unlock(file_lock_r);
else
file_dotlock_delete(dotlock_r);
if (ret < 0)
return -1;
squat_trie_close(trie);
if (squat_trie_open_fd(trie) < 0)
return -1;
if (trie->fd == -1)
return 0;
}
if ((trie->flags & SQUAT_INDEX_FLAG_NFS_FLUSH) != 0)
nfs_flush_read_cache_locked(trie->path, trie->fd);
return 1;
}
static void
node_make_squential(struct squat_trie *trie, struct squat_node *node, int level)
{
const unsigned int alloc_size =
NODE_CHILDREN_ALLOC_SIZE(SEQUENTIAL_COUNT);
struct squat_node *children;
unsigned char *chars;
unsigned int i;
i_assert(node->child_count == 0);
trie->node_alloc_size += alloc_size;
node->want_sequential = FALSE;
node->have_sequential = TRUE;
node->child_count = SEQUENTIAL_COUNT;
node->children.data = i_malloc(alloc_size);
chars = NODE_CHILDREN_CHARS(node);
for (i = 0; i < SEQUENTIAL_COUNT; i++)
chars[i] = i;
if (level < MAX_FAST_LEVEL) {
children = NODE_CHILDREN_NODES(node);
for (i = 0; i < SEQUENTIAL_COUNT; i++)
children[i].want_sequential = TRUE;
}
}
static unsigned int
node_add_child(struct squat_trie *trie, struct squat_node *node,
unsigned char chr, int level)
{
unsigned int old_child_count = node->child_count;
struct squat_node *children, *old_children;
unsigned char *chars;
size_t old_size, new_size;
i_assert(node->leaf_string_length == 0);
if (node->want_sequential) {
node_make_squential(trie, node, level);
if (chr < SEQUENTIAL_COUNT)
return chr;
old_child_count = SEQUENTIAL_COUNT;
}
node->child_count++;
new_size = NODE_CHILDREN_ALLOC_SIZE(node->child_count);
if (old_child_count == 0) {
/* first child */
node->children.data = i_malloc(new_size);
trie->node_alloc_size += new_size;
} else {
old_size = NODE_CHILDREN_ALLOC_SIZE(old_child_count);
if (old_size != new_size) {
trie->node_alloc_size += new_size - old_size;
node->children.data = i_realloc(node->children.data,
old_size, new_size);
}
children = NODE_CHILDREN_NODES(node);
old_children = (void *)(NODE_CHILDREN_CHARS(node) +
MEM_ALIGN(old_child_count));
if (children != old_children) {
memmove(children, old_children,
old_child_count * sizeof(struct squat_node));
}
}
chars = NODE_CHILDREN_CHARS(node);
i_assert(chars != NULL);
chars[node->child_count - 1] = chr;
return node->child_count - 1;
}
static int
trie_file_cache_read(struct squat_trie *trie, size_t offset, size_t size)
{
if (trie->file_cache == NULL)
return 0;
if (file_cache_read(trie->file_cache, offset, size) < 0) {
i_error("read(%s) failed: %m", trie->path);
return -1;
}
trie->data = file_cache_get_map(trie->file_cache, &trie->data_size);
return 0;
}
static int
node_read_children(struct squat_trie *trie, struct squat_node *node, int level)
{
const uint8_t *data, *end;
const unsigned char *child_chars;
struct squat_node *child, *children = NULL;
uoff_t node_offset;
unsigned int i, child_idx, child_count;
uoff_t base_offset;
uint32_t num;
i_assert(node->children_not_mapped);
i_assert(!node->have_sequential);
i_assert(trie->unmapped_child_count > 0);
i_assert(trie->data_size <= trie->locked_file_size);
trie->unmapped_child_count--;
node_offset = node->children.offset;
node->children_not_mapped = FALSE;
node->children.data = NULL;
if (trie_file_cache_read(trie, node_offset, TRIE_READAHEAD_SIZE) < 0)
return -1;
if (unlikely(node_offset >= trie->data_size)) {
squat_trie_set_corrupted(trie);
return -1;
}
data = CONST_PTR_OFFSET(trie->data, node_offset);
end = CONST_PTR_OFFSET(trie->data, trie->data_size);
child_count = *data++;
if (unlikely(node_offset + child_count >= trie->data_size)) {
squat_trie_set_corrupted(trie);
return -1;
}
if (child_count == 0)
return 0;
child_chars = data;
data += child_count;
/* get child offsets */
base_offset = node_offset;
for (i = 0; i < child_count; i++) {
/* we always start with !have_sequential, so at i=0 this
check always goes to add the first child */
if (node->have_sequential && child_chars[i] < SEQUENTIAL_COUNT)
child_idx = child_chars[i];
else {
child_idx = node_add_child(trie, node, child_chars[i],
level);
children = NODE_CHILDREN_NODES(node);
}
i_assert(children != NULL);
child = &children[child_idx];
/* 1) child offset */
num = squat_unpack_num(&data, end);
if (num == 0) {
/* no children */
} else {
if ((num & 1) != 0) {
base_offset += num >> 1;
} else {
base_offset -= num >> 1;
}
if (base_offset >= trie->locked_file_size) {
squat_trie_set_corrupted(trie);
return -1;
}
trie->unmapped_child_count++;
child->children_not_mapped = TRUE;
child->children.offset = base_offset;
}
/* 2) uidlist */
child->uid_list_idx = squat_unpack_num(&data, end);
if (child->uid_list_idx == 0) {
/* we don't write nodes with empty uidlists */
squat_trie_set_corrupted(trie);
return -1;
}
if (!UIDLIST_IS_SINGLETON(child->uid_list_idx)) {
/* 3) next uid */
child->next_uid = squat_unpack_num(&data, end) + 1;
} else {
uint32_t idx = child->uid_list_idx;
child->next_uid = 1 +
squat_uidlist_singleton_last_uid(idx);
}
/* 4) unused uids + leaf string flag */
num = squat_unpack_num(&data, end);
child->unused_uids = num >> 1;
if ((num & 1) != 0) {
/* leaf string */
unsigned int len;
unsigned char *dest;
/* 5) leaf string length */
len = child->leaf_string_length =
squat_unpack_num(&data, end) + 1;
if (!NODE_IS_DYNAMIC_LEAF(child))
dest = child->children.static_leaf_string;
else {
dest = child->children.leaf_string =
i_malloc(len);
}
if (trie->file_cache != NULL) {
/* the string may be long -
recalculate the end pos */
size_t offset, size;
offset = (const char *)data -
(const char *)trie->data;
size = len + TRIE_BYTES_LEFT(child_count - i);
if (trie_file_cache_read(trie, offset,
size) < 0)
return -1;
data = CONST_PTR_OFFSET(trie->data, offset);
end = CONST_PTR_OFFSET(trie->data,
trie->data_size);
child_chars = CONST_PTR_OFFSET(trie->data,
node_offset + 1);
}
if ((size_t)(end - data) < len) {
squat_trie_set_corrupted(trie);
return -1;
}
memcpy(dest, data, len);
data += len;
}
}
if (unlikely(data == end)) {
/* we should never get this far */
squat_trie_set_corrupted(trie);
return -1;
}
return 0;
}
static void
node_write_children(struct squat_trie_build_context *ctx,
struct squat_node *node, const uoff_t *node_offsets)
{
struct squat_node *children;
const unsigned char *chars;
uint8_t child_count, buf[SQUAT_PACK_MAX_SIZE * 5], *bufp;
uoff_t base_offset;
unsigned int i;
chars = NODE_CHILDREN_CHARS(node);
children = NODE_CHILDREN_NODES(node);
base_offset = ctx->output->offset;
child_count = node->child_count;
o_stream_nsend(ctx->output, &child_count, 1);
o_stream_nsend(ctx->output, chars, child_count);
for (i = 0; i < child_count; i++) {
bufp = buf;
/* 1) child offset */
if (node_offsets[i] == 0)
*bufp++ = 0;
else if (node_offsets[i] >= base_offset) {
squat_pack_num(&bufp,
((node_offsets[i] - base_offset) << 1) | 1);
base_offset = node_offsets[i];
} else {
squat_pack_num(&bufp,
(base_offset - node_offsets[i]) << 1);
base_offset = node_offsets[i];
}
/* 2) uidlist */
squat_pack_num(&bufp, children[i].uid_list_idx);
if (!UIDLIST_IS_SINGLETON(children[i].uid_list_idx)) {
/* 3) next uid */
squat_pack_num(&bufp, children[i].next_uid - 1);
}
if (children[i].leaf_string_length == 0) {
/* 4a) unused uids */
squat_pack_num(&bufp, children[i].unused_uids << 1);
o_stream_nsend(ctx->output, buf, bufp - buf);
} else {
i_assert(node_offsets[i] == 0);
/* 4b) unused uids + flag */
squat_pack_num(&bufp, (children[i].unused_uids << 1) | 1);
/* 5) leaf string length */
squat_pack_num(&bufp, children[i].leaf_string_length - 1);
o_stream_nsend(ctx->output, buf, bufp - buf);
o_stream_nsend(ctx->output,
NODE_LEAF_STRING(&children[i]),
children[i].leaf_string_length);
}
}
}
static inline void
node_add_uid(struct squat_trie_build_context *ctx, uint32_t uid,
struct squat_node *node)
{
if (uid < node->next_uid) {
/* duplicate */
return;
}
node->unused_uids += uid - node->next_uid;
node->next_uid = uid + 1;
node->uid_list_idx =
squat_uidlist_build_add_uid(ctx->uidlist_build_ctx,
node->uid_list_idx, uid);
}
static void
node_split_string(struct squat_trie_build_context *ctx, struct squat_node *node)
{
struct squat_node *child;
unsigned char *str;
unsigned int uid, idx, leafstr_len = node->leaf_string_length;
i_assert(leafstr_len > 0);
/* make a copy of the leaf string and convert to normal node by
removing it. */
str = t_malloc(leafstr_len);
if (!NODE_IS_DYNAMIC_LEAF(node))
memcpy(str, node->children.static_leaf_string, leafstr_len);
else {
memcpy(str, node->children.leaf_string, leafstr_len);
i_free(node->children.leaf_string);
}
node->leaf_string_length = 0;
/* create a new child node for the rest of the string */
idx = node_add_child(ctx->trie, node, str[0], MAX_FAST_LEVEL);
child = NODE_CHILDREN_NODES(node) + idx;
/* update uidlist to contain all of parent's UIDs */
child->next_uid = node->next_uid - node->unused_uids;
for (uid = 0; uid < child->next_uid; uid++) {
child->uid_list_idx =
squat_uidlist_build_add_uid(ctx->uidlist_build_ctx,
child->uid_list_idx, uid);
}
i_assert(!child->have_sequential && child->children.data == NULL);
if (leafstr_len > 1) {
/* make the child a leaf string */
leafstr_len--;
child->leaf_string_length = leafstr_len;
if (!NODE_IS_DYNAMIC_LEAF(child)) {
memcpy(child->children.static_leaf_string,
str + 1, leafstr_len);
} else {
child->children.leaf_string = i_malloc(leafstr_len);
memcpy(child->children.leaf_string,
str + 1, leafstr_len);
}
}
}
static bool
node_leaf_string_add_or_split(struct squat_trie_build_context *ctx,
struct squat_node *node,
const unsigned char *data, unsigned int data_len)
{
const unsigned char *str = NODE_LEAF_STRING(node);
const unsigned int leafstr_len = node->leaf_string_length;
unsigned int i;
if (data_len != leafstr_len) {
/* different lengths, can't match */
T_BEGIN {
node_split_string(ctx, node);
} T_END;
return FALSE;
}
for (i = 0; i < data_len; i++) {
if (data[i] != str[i]) {
/* non-match */
T_BEGIN {
node_split_string(ctx, node);
} T_END;
return FALSE;
}
}
return TRUE;
}
static int squat_build_add(struct squat_trie_build_context *ctx, uint32_t uid,
const unsigned char *data, unsigned int size)
{
struct squat_trie *trie = ctx->trie;
struct squat_node *node = &trie->root;
const unsigned char *end = data + size;
unsigned char *chars;
unsigned int idx;
int level = 0;
for (;;) {
if (node->children_not_mapped) {
if (unlikely(node_read_children(trie, node, level) < 0))
return -1;
}
if (node->leaf_string_length != 0) {
/* the whole string must match or we need to split
the node */
if (node_leaf_string_add_or_split(ctx, node, data,
end - data)) {
node_add_uid(ctx, uid, node);
return 0;
}
}
node_add_uid(ctx, uid, node);
if (unlikely(uid < node->unused_uids)) {
squat_trie_set_corrupted(trie);
return -1;
}
/* child node's UIDs are relative to ours. so for example if
we're adding UID 4 and this node now has [2,4] UIDs,
unused_uids=3 and so the child node will be adding
UID 4-3 = 1. */
uid -= node->unused_uids;
if (data == end)
return 0;
level++;
if (node->have_sequential) {
i_assert(node->child_count >= SEQUENTIAL_COUNT);
if (*data < SEQUENTIAL_COUNT) {
idx = *data;
goto found;
}
idx = SEQUENTIAL_COUNT;
} else {
idx = 0;
}
chars = NODE_CHILDREN_CHARS(node);
for (; idx < node->child_count; idx++) {
if (chars[idx] == *data)
goto found;
}
break;
found:
data++;
node = NODE_CHILDREN_NODES(node) + idx;
}
/* create new children */
i_assert(node->leaf_string_length == 0);
for (;;) {
idx = node_add_child(trie, node, *data,
size - (end - data) + 1);
node = NODE_CHILDREN_NODES(node) + idx;
node_add_uid(ctx, uid, node);
uid = 0;
if (++data == end)
break;
if (!node->have_sequential) {
/* convert the node into a leaf string */
unsigned int len = end - data;
i_assert(node->children.data == NULL);
node->leaf_string_length = len;
if (!NODE_IS_DYNAMIC_LEAF(node)) {
memcpy(node->children.static_leaf_string,
data, len);
} else {
node->children.leaf_string = i_malloc(len);
memcpy(node->children.leaf_string, data, len);
}
break;
}
}
return 0;
}
static int
squat_build_word_bytes(struct squat_trie_build_context *ctx, uint32_t uid,
const unsigned char *data, unsigned int size)
{
struct squat_trie *trie = ctx->trie;
unsigned int i;
if (trie->hdr.full_len <= trie->hdr.partial_len)
i = 0;
else {
/* the first word is longer than others */
if (squat_build_add(ctx, uid, data,
I_MIN(size, trie->hdr.full_len)) < 0)
return -1;
i = 1;
}
for (; i < size; i++) {
if (squat_build_add(ctx, uid, data + i,
I_MIN(trie->hdr.partial_len, size-i)) < 0)
return -1;
}
return 0;
}
static int
squat_build_word(struct squat_trie_build_context *ctx, uint32_t uid,
const unsigned char *data, const uint8_t *char_lengths,
unsigned int size)
{
struct squat_trie *trie = ctx->trie;
unsigned int i, j, bytelen;
if (char_lengths == NULL) {
/* optimization path: all characters are bytes */
return squat_build_word_bytes(ctx, uid, data, size);
}
if (trie->hdr.full_len <= trie->hdr.partial_len)
i = 0;
else {
/* the first word is longer than others */
bytelen = 0;
for (j = 0; j < trie->hdr.full_len && bytelen < size; j++)
bytelen += char_lengths[bytelen];
i_assert(bytelen <= size);
if (squat_build_add(ctx, uid, data, bytelen) < 0)
return -1;
i = char_lengths[0];
}
for (; i < size; i += char_lengths[i]) {
bytelen = 0;
for (j = 0; j < trie->hdr.partial_len && i+bytelen < size; j++)
bytelen += char_lengths[i + bytelen];
i_assert(i + bytelen <= size);
if (squat_build_add(ctx, uid, data + i, bytelen) < 0)
return -1;
}
return 0;
}
static unsigned char *
squat_data_normalize(struct squat_trie *trie, const unsigned char *data,
unsigned int size)
{
static const unsigned char replacement_utf8[] = { 0xef, 0xbf, 0xbd };
unsigned char *dest;
unsigned int i;
dest = t_malloc(size);
for (i = 0; i < size; i++) {
if (data[i] == replacement_utf8[0] && i + 2 < size &&
data[i+1] == replacement_utf8[1] &&
data[i+2] == replacement_utf8[2]) {
/* Don't index replacement character */
dest[i++] = 0;
dest[i++] = 0;
dest[i] = 0;
} else {
dest[i] = trie->hdr.normalize_map[data[i]];
}
}
return dest;
}
static int
squat_trie_build_more_real(struct squat_trie_build_context *ctx,
uint32_t uid, enum squat_index_type type,
const unsigned char *input, unsigned int size)
{
struct squat_trie *trie = ctx->trie;
const unsigned char *data;
uint8_t *char_lengths;
unsigned int i, start = 0;
bool multibyte_chars = FALSE;
int ret = 0;
uid = uid * 2 + (type == SQUAT_INDEX_TYPE_HEADER ? 1 : 0);
char_lengths = t_malloc(size);
data = squat_data_normalize(trie, input, size);
for (i = 0; i < size; i++) {
char_lengths[i] = uni_utf8_char_bytes(input[i]);
if (char_lengths[i] != 1)
multibyte_chars = TRUE;
if (data[i] != '\0')
continue;
while (start < i && data[start] == '\0')
start++;
if (i != start) {
if (squat_build_word(ctx, uid, data + start,
!multibyte_chars ? NULL :
char_lengths + start,
i - start) < 0) {
ret = -1;
start = i;
break;
}
}
start = i + 1;
}
while (start < i && data[start] == '\0')
start++;
if (i != start) {
if (squat_build_word(ctx, uid, data + start,
!multibyte_chars ? NULL :
char_lengths + start, i - start) < 0)
ret = -1;
}
return ret;
}
int squat_trie_build_more(struct squat_trie_build_context *ctx,
uint32_t uid, enum squat_index_type type,
const unsigned char *input, unsigned int size)
{
int ret = 0;
if (size != 0) T_BEGIN {
ret = squat_trie_build_more_real(ctx, uid, type, input, size);
} T_END;
return ret;
}
static void
node_drop_unused_children(struct squat_trie *trie, struct squat_node *node)
{
unsigned char *chars;
struct squat_node *children_src, *children_dest;
unsigned int i, j, orig_child_count = node->child_count;
chars = NODE_CHILDREN_CHARS(node);
children_src = NODE_CHILDREN_NODES(node);
/* move chars */
for (i = j = 0; i < orig_child_count; i++) {
if (children_src[i].next_uid != 0)
chars[j++] = chars[i];
}
node->child_count = j;
/* move children. note that children_dest may point to different
location than children_src, although they both point to the
same node. */
children_dest = NODE_CHILDREN_NODES(node);
for (i = j = 0; i < orig_child_count; i++) {
if (children_src[i].next_uid != 0)
children_dest[j++] = children_src[i];
else
node_free(trie, &children_src[i]);
}