forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringDictionaryTest.cpp
965 lines (884 loc) · 37.6 KB
/
StringDictionaryTest.cpp
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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "TestHelpers.h"
#include "StringDictionary/StringDictionaryProxy.h"
#include <cstdlib>
#include <filesystem>
#include <functional>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
#include <unordered_set>
#ifndef BASE_PATH1
#define BASE_PATH1 "./tmp/dict1"
#endif
#ifndef BASE_PATH2
#define BASE_PATH2 "./tmp/dict2"
#endif
extern bool g_cache_string_hash;
TEST(StringDictionary, AddAndGet) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("foo bar");
auto id2 = string_dict.getOrAdd("foo bar");
ASSERT_EQ(id1, id2);
ASSERT_EQ(0, id1);
auto id3 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id3);
auto id4 = string_dict.getIdOfString(std::string_view("foo bar"));
ASSERT_EQ(id1, id4);
ASSERT_EQ("foo bar", string_dict.getString(id4));
auto id5 = string_dict.getIdOfString(std::string("foo bar"));
ASSERT_EQ(id1, id5);
ASSERT_EQ("foo bar", string_dict.getString(id5));
}
TEST(StringDictionary, Recover) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id1);
auto id2 = string_dict.getOrAdd("baz");
ASSERT_EQ(1, id2);
auto id3 = string_dict.getOrAdd("foo bar");
ASSERT_EQ(0, id3);
auto id4 = string_dict.getOrAdd("fizzbuzz");
ASSERT_EQ(2, id4);
ASSERT_EQ("baz", string_dict.getString(id2));
ASSERT_EQ("foo bar", string_dict.getString(id3));
ASSERT_EQ("fizzbuzz", string_dict.getString(id4));
}
TEST(StringDictionary, HandleEmpty) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
auto id1 = string_dict.getOrAdd("");
auto id2 = string_dict.getOrAdd("");
ASSERT_EQ(id1, id2);
ASSERT_EQ(std::numeric_limits<int32_t>::min(), id1);
}
TEST(StringDictionary, RecoverZero) {
const DictRef dict_ref(-1, 1);
{
StringDictionary string_dict(dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(0), num_strings);
}
StringDictionary string_dict(dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(0), num_strings);
}
const int g_op_count{250000};
TEST(StringDictionary, ManyAddsAndGets) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(std::to_string(i), string_dict.getString(i));
}
}
TEST(StringDictionary, RecoverMany) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
size_t num_strings = string_dict.storageEntryCount();
ASSERT_EQ(static_cast<size_t>(g_op_count), num_strings);
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(i, string_dict.getOrAdd(std::to_string(i)));
}
for (int i = 0; i < g_op_count; ++i) {
CHECK_EQ(std::to_string(i), string_dict.getString(i));
}
}
TEST(StringDictionary, GetStringViews) {
const DictRef dict_ref(-1, 1);
std::shared_ptr<StringDictionary> string_dict = std::make_shared<StringDictionary>(
dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
std::vector<std::string> strings;
std::vector<int32_t> string_ids(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
strings.emplace_back(std::to_string(i));
}
string_dict->getOrAddBulk(strings, string_ids.data());
const auto string_views = string_dict->getStringViews();
ASSERT_EQ(string_views.size(), static_cast<size_t>(g_op_count));
for (int i = 0; i < g_op_count; ++i) {
ASSERT_EQ(strings[i], std::string(string_views[i]));
}
}
TEST(StringDictionary, GetOrAddBulk) {
const DictRef dict_ref(-1, 1);
StringDictionary string_dict(dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
{
// First insert all new strings
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
string_dict.getOrAddBulk(strings, string_ids.data());
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(std::to_string(string_ids[i] * -1), strings[i]);
}
}
}
{
// Now insert half new, half existing strings
// Even idxs are existing, odd are new
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(g_op_count + i / 2));
}
}
string_dict.getOrAddBulk(strings, string_ids.data());
for (int i = 0; i < g_op_count; ++i) {
// Even though the if and else branches result in the same test, both are
// left help diagnose the source of test failures
// (existing strings for the if branch and new strings for the else branch)
if (i % 2 == 0) {
// Existing strings
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
// New strings
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
}
}
}
}
TEST(StringDictionary, GetBulk) {
const DictRef dict_ref(-1, 1);
// Use existing dictionary from GetOrAddBulk
StringDictionary string_dict(dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
{
// First iteration is identical to first of GetOrAddBulk, and results should be the
// same
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
string_dict.getBulk(strings, string_ids.data());
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(std::to_string(string_ids[i] * -1), strings[i]);
}
}
}
{
// Second iteration - even idxs exist, odd do not and should return INVALID_STR_ID
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
// Won't exist in dictionary
strings.emplace_back(std::to_string(g_op_count * 2 + i));
}
}
string_dict.getBulk(strings, string_ids.data());
const std::string invalid_str_id_as_string =
std::to_string(StringDictionary::INVALID_STR_ID);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(string_ids[i], StringDictionary::INVALID_STR_ID);
}
}
}
}
TEST(StringDictionary, BuildTranslationMap) {
const DictRef dict_ref1(-1, 1);
const DictRef dict_ref2(-1, 2);
std::shared_ptr<StringDictionary> source_string_dict =
std::make_shared<StringDictionary>(
dict_ref1, BASE_PATH1, false, false, g_cache_string_hash);
std::shared_ptr<StringDictionary> dest_string_dict = std::make_shared<StringDictionary>(
dict_ref2, BASE_PATH2, false, false, g_cache_string_hash);
// Prep: insert g_op_count strings into source_string_dict
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
strings.emplace_back(std::to_string(i));
}
source_string_dict->getOrAddBulk(strings, string_ids.data());
auto dummy_callback = [](const std::string_view& source_string,
const int32_t source_string_id) { return false; };
{
// First try to translate to empty dictionary.
// Should get back all INVALID_STR_IDs
const auto translated_ids = source_string_dict->buildDictionaryTranslationMap(
dest_string_dict, dummy_callback);
const size_t num_ids = translated_ids.size();
ASSERT_EQ(num_ids, source_string_dict->storageEntryCount());
for (size_t idx = 0; idx < num_ids; ++idx) {
ASSERT_EQ(translated_ids[idx], StringDictionary::INVALID_STR_ID);
}
}
{
// Now insert the same strings inserted into source_string_dict,
// but in reversed order and missing the first 2 strings in the source
// dictionary. Make sure they translate correctly, and that the missing
// strings translate to INVALID_STR_ID
std::vector<std::string> reversed_strings;
constexpr int32_t num_missing_strings{2};
ASSERT_LE(num_missing_strings, g_op_count);
std::vector<int32_t> reversed_string_ids(g_op_count - num_missing_strings);
for (int i = 0; i < g_op_count - num_missing_strings; ++i) {
reversed_strings.emplace_back(std::to_string(g_op_count - i - 1));
}
dest_string_dict->getOrAddBulk(reversed_strings, reversed_string_ids.data());
ASSERT_EQ(dest_string_dict->storageEntryCount(), reversed_strings.size());
const auto translated_ids = source_string_dict->buildDictionaryTranslationMap(
dest_string_dict, dummy_callback);
const size_t num_ids = translated_ids.size();
ASSERT_EQ(num_ids, static_cast<size_t>(g_op_count));
ASSERT_EQ(num_ids,
reversed_strings.size() + static_cast<size_t>(num_missing_strings));
for (int32_t idx = 0; idx < static_cast<int32_t>(num_ids); ++idx) {
if (idx < num_missing_strings) {
ASSERT_EQ(translated_ids[idx], StringDictionary::INVALID_STR_ID);
} else {
ASSERT_EQ(translated_ids[idx], g_op_count - idx - 1);
}
}
}
}
TEST(StringDictionaryProxy, GetOrAddTransient) {
const DictRef dict_ref(-1, 1);
std::shared_ptr<StringDictionary> string_dict = std::make_shared<StringDictionary>(
dict_ref, BASE_PATH1, false, false, g_cache_string_hash);
// Prep underlying dictionary data
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
string_dict->getOrAddBulk(strings, string_ids.data());
// Now make proxy from dictionary
StringDictionaryProxy string_dict_proxy(
string_dict, 1 /* string_dict_id */, string_dict->storageEntryCount());
{
// First iteration is identical to first of the StringDictionary GetOrAddBulk test,
// and results should be the same
std::vector<std::string> strings;
std::vector<int32_t> string_ids(g_op_count);
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
for (size_t i = 0; i < g_op_count; ++i) {
string_ids[i] = string_dict_proxy.getOrAddTransient(strings[i]);
}
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(std::to_string(string_ids[i] * -1), strings[i]);
}
}
}
{
// Now make the odd idx strings new, should get back transient string ids for those
std::vector<std::string> strings;
std::vector<int32_t> string_ids(g_op_count);
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(g_op_count * 4 + i));
}
}
for (size_t i = 0; i < g_op_count; ++i) {
string_ids[i] = string_dict_proxy.getOrAddTransient(strings[i]);
}
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(string_ids[i], -2 - (i / 2));
}
}
}
}
TEST(StringDictionaryProxy, GetOrAddTransientBulk) {
// Use existing dictionary from GetBulk
const DictRef dict_ref(-1, 1);
std::shared_ptr<StringDictionary> string_dict = std::make_shared<StringDictionary>(
dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
StringDictionaryProxy string_dict_proxy(
string_dict, 1 /* string_dict_id */, string_dict->storageEntryCount());
{
// First iteration is identical to first of the StringDictionary GetOrAddBulk test,
// and results should be the same
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
const auto string_ids = string_dict_proxy.getOrAddTransientBulk(strings);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(std::to_string(string_ids[i] * -1), strings[i]);
}
}
}
{
// Now make the odd idx strings new, should get back transient string ids for those
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(g_op_count * 6 + i));
}
}
const auto string_ids = string_dict_proxy.getOrAddTransientBulk(strings);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(string_ids[i], -2 - (i / 2));
}
}
}
}
TEST(StringDictionaryProxy, GetTransientBulk) {
// Use existing dictionary from GetBulk
const DictRef dict_ref(-1, 1);
std::shared_ptr<StringDictionary> string_dict = std::make_shared<StringDictionary>(
dict_ref, BASE_PATH1, false, true, g_cache_string_hash);
StringDictionaryProxy string_dict_proxy(
string_dict, 1 /* string_dict_id */, string_dict->storageEntryCount());
{
// First iteration is identical to first of the StryingDictionaryProxy
// GetOrAddTransientBulk test, and results should be the same
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(i * -1));
}
}
const auto string_ids = string_dict_proxy.getTransientBulk(strings);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(std::to_string(string_ids[i] * -1), strings[i]);
}
}
}
{
// Second iteration - even idxs exist, odd do not and should return INVALID_STR_ID
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
strings.emplace_back(std::to_string(i));
} else {
strings.emplace_back(std::to_string(g_op_count * 8 + i));
}
}
const auto string_ids = string_dict_proxy.getTransientBulk(strings);
for (int i = 0; i < g_op_count; ++i) {
if (i % 2 == 0) {
ASSERT_EQ(std::to_string(string_ids[i]), strings[i]);
} else {
ASSERT_EQ(string_ids[i], StringDictionary::INVALID_STR_ID);
}
}
}
}
TEST(StringDictionaryProxy, BuildIntersectionTranslationMapToOtherProxy) {
// Use existing dictionary from GetBulk
const DictRef dict_ref1(-1, 1);
const DictRef dict_ref2(-1, 2);
std::shared_ptr<StringDictionary> source_string_dict =
std::make_shared<StringDictionary>(
dict_ref1, BASE_PATH1, false, false, g_cache_string_hash);
std::shared_ptr<StringDictionary> dest_string_dict = std::make_shared<StringDictionary>(
dict_ref2, BASE_PATH2, false, false, g_cache_string_hash);
// Prep: insert g_op_count strings into source_string_dict
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
strings.emplace_back(std::to_string(i));
}
source_string_dict->getOrAddBulk(strings, string_ids.data());
{
// First try to translate to empty dictionary.
// Should get back all INVALID_STR_IDs
std::shared_ptr<StringDictionaryProxy> source_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(source_string_dict,
1 /* string_dict_id */,
source_string_dict->storageEntryCount());
std::shared_ptr<StringDictionaryProxy> dest_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(dest_string_dict,
2 /* string_dict_id */,
dest_string_dict->storageEntryCount());
const auto str_proxy_translation_map =
source_string_dict_proxy->buildIntersectionTranslationMapToOtherProxy(
dest_string_dict_proxy.get());
ASSERT_FALSE(str_proxy_translation_map.empty());
const auto& translated_ids = str_proxy_translation_map.getVectorMap();
const size_t num_ids = translated_ids.size() - 1; // Subtract 1 for INVALID_STR_ID
ASSERT_EQ(num_ids, source_string_dict_proxy->entryCount());
for (size_t idx = 0; idx < num_ids; ++idx) {
ASSERT_EQ(translated_ids[idx], StringDictionary::INVALID_STR_ID);
}
}
{
// Add transient entries to source proxy
constexpr int32_t num_missing_ids{10};
ASSERT_LE(num_missing_ids, g_op_count);
const int32_t num_dest_strings{g_op_count - num_missing_ids};
std::vector<int32_t> dest_string_ids(num_dest_strings);
std::vector<std::string> dest_strings;
dest_strings.reserve(num_dest_strings);
for (int i = 0; i < num_dest_strings; ++i) {
dest_strings.emplace_back(std::to_string(i));
}
dest_string_dict->getOrAddBulk(dest_strings, dest_string_ids.data());
std::shared_ptr<StringDictionaryProxy> source_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(source_string_dict,
1 /* string_dict_id */,
source_string_dict->storageEntryCount());
std::shared_ptr<StringDictionaryProxy> dest_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(dest_string_dict,
2 /* string_dict_id */,
dest_string_dict->storageEntryCount());
constexpr int32_t num_source_proxy_transient_ids{64};
constexpr int32_t num_dest_proxy_transient_ids{32};
constexpr int32_t proxy_transient_start{g_op_count * 2};
for (int i = proxy_transient_start;
i < proxy_transient_start + num_source_proxy_transient_ids;
++i) {
source_string_dict_proxy->getOrAddTransient(std::to_string(i));
}
for (int i = proxy_transient_start;
i < proxy_transient_start + num_dest_proxy_transient_ids;
++i) {
dest_string_dict_proxy->getOrAddTransient(std::to_string(i));
}
const auto str_proxy_translation_map =
source_string_dict_proxy->buildIntersectionTranslationMapToOtherProxy(
dest_string_dict_proxy.get());
ASSERT_FALSE(str_proxy_translation_map.empty());
const auto& translated_ids = str_proxy_translation_map.getVectorMap();
const size_t num_ids = translated_ids.size() - 1; // Subtract 1 for INVALID_STR_ID
ASSERT_EQ(num_ids, source_string_dict_proxy->entryCount());
const int32_t start_source_id =
source_string_dict_proxy->transientEntryCount() > 0
? (source_string_dict_proxy->transientEntryCount() + 1) * -1
: 0;
for (int32_t idx = 0; idx < static_cast<int32_t>(num_ids); ++idx) {
const int32_t source_id = idx + start_source_id;
if (source_id == -1 || source_id == 0) {
continue;
}
const std::string source_string = source_string_dict_proxy->getString(source_id);
const int32_t source_string_as_int = std::stoi(source_string);
const bool should_be_missing =
!((source_string_as_int >= 0 && source_string_as_int < num_dest_strings) ||
(source_string_as_int >= (g_op_count * 2) &&
source_string_as_int < (g_op_count * 2 + num_dest_proxy_transient_ids)));
if (translated_ids[idx] != StringDictionary::INVALID_STR_ID) {
ASSERT_FALSE(should_be_missing);
const std::string dest_string =
dest_string_dict_proxy->getString(translated_ids[idx]);
ASSERT_EQ(source_string, dest_string);
} else {
ASSERT_TRUE(should_be_missing);
}
}
}
}
TEST(StringDictionaryProxy, BuildUnionTranslationMapToEmptyProxy) {
// Todo(todd): Migrate this and intersection translation tests to use
// approach and methods in BuildUnionTranslationMapToPartialOverlapProxy
const DictRef dict_ref1(-1, 1);
const DictRef dict_ref2(-1, 2);
std::shared_ptr<StringDictionary> source_string_dict =
std::make_shared<StringDictionary>(
dict_ref1, BASE_PATH1, false, false, g_cache_string_hash);
std::shared_ptr<StringDictionary> dest_string_dict = std::make_shared<StringDictionary>(
dict_ref2, BASE_PATH2, false, false, g_cache_string_hash);
// Prep: insert g_op_count strings into source_string_dict
std::vector<int32_t> string_ids(g_op_count);
std::vector<std::string> strings;
strings.reserve(g_op_count);
for (int i = 0; i < g_op_count; ++i) {
strings.emplace_back(std::to_string(i));
}
source_string_dict->getOrAddBulk(strings, string_ids.data());
{
// First try to union translate to empty dictionary.
// All strings should end up as transient entries in
// destination proxy
std::shared_ptr<StringDictionaryProxy> source_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(source_string_dict,
1 /* string_dict_id */,
source_string_dict->storageEntryCount());
std::shared_ptr<StringDictionaryProxy> dest_string_dict_proxy =
std::make_shared<StringDictionaryProxy>(dest_string_dict,
2 /* string_dict_id */,
dest_string_dict->storageEntryCount());
const auto str_proxy_translation_map =
source_string_dict_proxy->buildUnionTranslationMapToOtherProxy(
dest_string_dict_proxy.get());
ASSERT_FALSE(str_proxy_translation_map.empty());
ASSERT_EQ(str_proxy_translation_map.numUntranslatedStrings(), strings.size());
const auto& translated_ids = str_proxy_translation_map.getVectorMap();
const size_t num_ids = translated_ids.size();
ASSERT_EQ(num_ids - 1UL /* for INVALID_STR_ID */,
source_string_dict_proxy->entryCount());
const auto domain_start = str_proxy_translation_map.domainStart();
for (size_t idx = 0; idx < num_ids; ++idx) {
const auto string_id = static_cast<int32_t>(idx) + domain_start;
if (string_id == -1) {
ASSERT_EQ(translated_ids[idx], StringDictionary::INVALID_STR_ID);
} else {
ASSERT_LT(translated_ids[idx], StringDictionary::INVALID_STR_ID);
ASSERT_EQ(dest_string_dict_proxy->getString(translated_ids[idx]),
strings[string_id]);
}
}
}
}
std::vector<std::string> add_strings_numeric_range(std::shared_ptr<StringDictionary> sd,
const size_t num_vals,
const int32_t start_val) {
CHECK_GE(start_val, 0);
const auto end_val = start_val + static_cast<int32_t>(num_vals);
std::vector<std::string> strings(num_vals);
for (int32_t int_val = start_val; int_val < end_val; ++int_val) {
strings[int_val - start_val] = std::to_string(int_val);
}
std::vector<int32_t> string_ids(num_vals);
sd->getOrAddBulk(strings, string_ids.data());
sd->checkpoint();
return strings;
}
std::vector<std::string> add_strings_numeric_range(StringDictionaryProxy& sdp,
const size_t num_vals,
const int32_t start_val) {
CHECK_LE(start_val, -2);
std::vector<std::string> strings(num_vals);
const auto end_val = start_val - static_cast<int32_t>(num_vals);
for (int32_t int_val = start_val; int_val > end_val; --int_val) {
strings[start_val - int_val] = std::to_string(int_val);
}
sdp.getOrAddTransientBulk(strings);
return strings;
}
size_t calc_expected_untranslated_strings(const size_t num_source_entries,
const size_t num_dest_entries,
const int32_t source_start_val,
const int32_t dest_start_val) {
const int32_t source_abs_min = std::abs(source_start_val); // 0
const int32_t source_abs_max =
std::abs(source_start_val) + static_cast<int32_t>(num_source_entries); // 10
const int32_t dest_abs_min = std::abs(dest_start_val); // 7
const int32_t dest_abs_max =
std::abs(dest_start_val) + static_cast<int32_t>(num_dest_entries); // 12
return static_cast<size_t>(std::max(dest_abs_min - source_abs_min, 0) +
std::max(source_abs_max - dest_abs_max, 0));
}
void verify_translation(const StringDictionaryProxy& source_proxy,
const StringDictionaryProxy& dest_proxy,
const StringDictionaryProxy::IdMap& id_map,
const std::vector<std::string>& persisted_source_strings,
const std::vector<std::string>& transient_source_strings,
const std::vector<std::string>& persisted_dest_strings,
const std::vector<std::string>& transient_dest_strings,
const bool is_union) {
std::unordered_set<std::string> dest_string_set;
for (const auto& str : persisted_dest_strings) {
ASSERT_TRUE(dest_string_set.insert(str).second);
}
for (const auto& str : transient_dest_strings) {
ASSERT_TRUE(dest_string_set.insert(str).second);
}
for (const auto& persisted_source_str : persisted_source_strings) {
const int32_t source_str_id = source_proxy.getIdOfString(persisted_source_str);
ASSERT_GE(source_str_id, 0);
const bool str_was_in_dest_proxy =
dest_string_set.find(persisted_source_str) != dest_string_set.end();
const auto translated_id = id_map[source_str_id];
if (str_was_in_dest_proxy) {
ASSERT_GE(translated_id, 0);
const auto dest_str = dest_proxy.getString(translated_id);
ASSERT_EQ(persisted_source_str, dest_str);
} else {
if (is_union) {
ASSERT_LE(translated_id, -2);
const auto dest_str = dest_proxy.getString(translated_id);
ASSERT_EQ(persisted_source_str, dest_str);
} else {
ASSERT_EQ(translated_id, StringDictionary::INVALID_STR_ID);
}
}
}
}
TEST(StringDictionaryProxy, BuildUnionTranslationMapToPartialOverlapProxy) {
const DictRef dict_ref1(-1, 1);
const DictRef dict_ref2(-1, 2);
std::shared_ptr<StringDictionary> source_sd = std::make_shared<StringDictionary>(
dict_ref1, BASE_PATH1, false, false, g_cache_string_hash);
std::shared_ptr<StringDictionary> dest_sd = std::make_shared<StringDictionary>(
dict_ref2, BASE_PATH2, false, false, g_cache_string_hash);
constexpr size_t num_source_persisted_entries{10};
constexpr size_t num_dest_persisted_entries{5};
constexpr int32_t source_persisted_start_val{0};
constexpr int32_t dest_persisted_start_val{7};
constexpr size_t num_source_transient_entries{10};
constexpr size_t num_dest_transient_entries{5};
constexpr int32_t source_transient_start_val{-2};
constexpr int32_t dest_transient_start_val{-9};
const auto persisted_source_strings = add_strings_numeric_range(
source_sd, num_source_persisted_entries, source_persisted_start_val);
ASSERT_EQ(source_sd->storageEntryCount(), num_source_persisted_entries);
const auto persisted_dest_strings = add_strings_numeric_range(
dest_sd, num_dest_persisted_entries, dest_persisted_start_val);
ASSERT_EQ(dest_sd->storageEntryCount(), num_dest_persisted_entries);
StringDictionaryProxy source_sdp(
source_sd, 1 /* string_dict_id */, source_sd->storageEntryCount());
StringDictionaryProxy dest_sdp(
dest_sd, 2 /* string_dict_id */, dest_sd->storageEntryCount());
const auto transient_source_strings = add_strings_numeric_range(
source_sdp, num_source_transient_entries, source_transient_start_val);
ASSERT_EQ(source_sdp.getDictId(), 1);
ASSERT_EQ(source_sdp.storageEntryCount(), num_source_persisted_entries);
ASSERT_EQ(source_sdp.transientEntryCount(), num_source_transient_entries);
const auto transient_dest_strings = add_strings_numeric_range(
dest_sdp, num_dest_transient_entries, dest_transient_start_val);
ASSERT_EQ(dest_sdp.getDictId(), 2);
ASSERT_EQ(dest_sdp.storageEntryCount(), num_dest_persisted_entries);
ASSERT_EQ(dest_sdp.transientEntryCount(), num_dest_transient_entries);
const auto id_map = source_sdp.buildUnionTranslationMapToOtherProxy(&dest_sdp);
const size_t expected_num_untranslated_strings =
calc_expected_untranslated_strings(num_source_persisted_entries,
num_dest_persisted_entries,
source_persisted_start_val,
dest_persisted_start_val) +
calc_expected_untranslated_strings(num_source_transient_entries,
num_dest_transient_entries,
source_transient_start_val,
dest_transient_start_val);
ASSERT_EQ(id_map.size(),
num_source_persisted_entries + num_source_transient_entries +
1UL /* slot for INVALID_STR_ID */);
ASSERT_EQ(id_map.numNonTransients(), num_source_persisted_entries);
ASSERT_EQ(id_map.numTransients(), num_source_transient_entries);
ASSERT_EQ(id_map.numUntranslatedStrings(), expected_num_untranslated_strings);
verify_translation(source_sdp,
dest_sdp,
id_map,
persisted_source_strings,
transient_source_strings,
persisted_dest_strings,
transient_dest_strings,
true);
}
TEST(StringDictionary, TransientUnion) {
std::string const sd_lhs_path = std::string(BASE_PATH) + "/sd_lhs";
std::string const sd_rhs_path = std::string(BASE_PATH) + "/sd_rhs";
#ifdef _WIN32
mkdir(sd_lhs_path.c_str());
mkdir(sd_rhs_path.c_str());
#else
mkdir(sd_lhs_path.c_str(), 0755);
mkdir(sd_rhs_path.c_str(), 0755);
#endif
dict_ref_t const dict_ref_lhs(100, 10);
auto sd_lhs = std::make_shared<StringDictionary>(
dict_ref_lhs, sd_lhs_path, false, true, g_cache_string_hash);
sd_lhs->getOrAdd("a"); // id = 0
sd_lhs->getOrAdd("b"); // id = 1
sd_lhs->getOrAdd("c"); // id = 2
sd_lhs->getOrAdd("d"); // id = 3
sd_lhs->getOrAdd("e"); // id = 4
dict_ref_t const dict_ref_rhs(100, 20);
auto sd_rhs = std::make_shared<StringDictionary>(
dict_ref_rhs, sd_rhs_path, false, true, g_cache_string_hash);
sd_rhs->getOrAdd("c"); // id = 0
sd_rhs->getOrAdd("d"); // id = 1
sd_rhs->getOrAdd("e"); // id = 2
sd_rhs->getOrAdd("f"); // id = 3
sd_rhs->getOrAdd("g"); // id = 4
sd_rhs->getOrAdd("h"); // id = 5
sd_rhs->getOrAdd("i"); // id = 6
{
// TODO cleanup redundancy of setting SD id here.
StringDictionaryProxy sdp_lhs(
sd_lhs, dict_ref_lhs.dictId, sd_lhs->storageEntryCount());
sdp_lhs.getOrAddTransient("t0"); // id = -2
StringDictionaryProxy sdp_rhs(
sd_rhs, dict_ref_rhs.dictId, sd_rhs->storageEntryCount());
sdp_rhs.getOrAddTransient("t0"); // id = -2
sdp_rhs.getOrAddTransient("t1"); // id = -3
auto const id_map = sdp_lhs.transientUnion(sdp_rhs);
// Expected output:
// source_domain_min_ = -3 = -1 - number of transients in sdp_rhs
// t1 -3 (1st transient added to lhs when calling transientUnion())
// t0 -2 (exising transient id in sd_lhs)
// -1 (There is always at least one -1 so that id=-1 maps to -1)
// c 2 (existing id in sd_lhs)
// d 3 (existing id in sd_lhs)
// e 4 (existing id in sd_lhs)
// f -4 (2nd transient added to lhs when calling transientUnion())
// g -5 (3rd transient added to lhs when calling transientUnion())
// h -6 (4th transient added to lhs when calling transientUnion())
// i -7 (5th transient added to lhs when calling transientUnion())
// rhs id(-3 -2 -1 0 1 2 3 4 5 6)
// translation_map_(-3 -2 -1 2 3 4 -4 -5 -6 -7)
ASSERT_EQ("IdMap(offset_(3) vector_map_(-3 -2 -1 2 3 4 -4 -5 -6 -7))",
boost::lexical_cast<std::string>(id_map));
ASSERT_EQ(0, sdp_lhs.getIdOfString("a"));
ASSERT_EQ(1, sdp_lhs.getIdOfString("b"));
ASSERT_EQ(2, sdp_lhs.getIdOfString("c"));
ASSERT_EQ(3, sdp_lhs.getIdOfString("d"));
ASSERT_EQ(4, sdp_lhs.getIdOfString("e"));
ASSERT_EQ(-2, sdp_lhs.getIdOfString("t0"));
ASSERT_EQ(-3, sdp_lhs.getIdOfString("t1"));
ASSERT_EQ(-4, sdp_lhs.getIdOfString("f"));
ASSERT_EQ(-5, sdp_lhs.getIdOfString("g"));
ASSERT_EQ(-6, sdp_lhs.getIdOfString("h"));
ASSERT_EQ(-7, sdp_lhs.getIdOfString("i"));
}
{ // Swap sd_lhs <-> sd_rhs
StringDictionaryProxy sdp_lhs(
sd_rhs, dict_ref_lhs.dictId, sd_rhs->storageEntryCount());
sdp_lhs.getOrAddTransient("t0");
StringDictionaryProxy sdp_rhs(
sd_lhs, dict_ref_rhs.dictId, sd_lhs->storageEntryCount());
sdp_rhs.getOrAddTransient("t0");
sdp_rhs.getOrAddTransient("t1");
auto const id_map = sdp_lhs.transientUnion(sdp_rhs);
// Expected output:
// source_domain_min_ = -3 = -1 - number of transients in sdp_rhs
// t1 -3 (1st transient added to lhs when calling transientUnion())
// t0 -2 (existing transient id in sd_lhs)
// -1 (There is always at least one -1 so that id=-1 maps to -1)
// a -4 (2nd transient added to lhs when calling transientUnion())
// b -5 (3rd transient added to lhs when calling transientUnion())
// c 0 (existing id in sd_rhs)
// d 1 (existing id in sd_rhs)
// e 2 (existing id in sd_rhs)
// translation_map_(-3 -2 -1 -4 -5 0 1 2)
ASSERT_EQ("IdMap(offset_(3) vector_map_(-3 -2 -1 -4 -5 0 1 2))",
boost::lexical_cast<std::string>(id_map));
ASSERT_EQ(-2, sdp_lhs.getIdOfString("t0"));
ASSERT_EQ(-3, sdp_lhs.getIdOfString("t1"));
ASSERT_EQ(-4, sdp_lhs.getIdOfString("a"));
ASSERT_EQ(-5, sdp_lhs.getIdOfString("b"));
ASSERT_EQ(0, sdp_lhs.getIdOfString("c"));
ASSERT_EQ(1, sdp_lhs.getIdOfString("d"));
ASSERT_EQ(2, sdp_lhs.getIdOfString("e"));
ASSERT_EQ(3, sdp_lhs.getIdOfString("f"));
ASSERT_EQ(4, sdp_lhs.getIdOfString("g"));
ASSERT_EQ(5, sdp_lhs.getIdOfString("h"));
ASSERT_EQ(6, sdp_lhs.getIdOfString("i"));
}
}
void create_directory_if_not_exists(const std::string& path) {
// Up to caller to catch any exceptions
std::filesystem::path fs_path(path);
if (!(std::filesystem::exists(path))) {
std::filesystem::create_directory(path);
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
namespace po = boost::program_options;
po::options_description desc("Options");
// these two are here to allow passing correctly google testing parameters
desc.add_options()("gtest_list_tests", "list all test");
desc.add_options()("gtest_filter", "filters tests, use --help for details");
desc.add_options()(
"enable-string-dict-hash-cache",
po::value<bool>(&g_cache_string_hash)
->default_value(g_cache_string_hash)
->implicit_value(true),
"Cache string hash values in the string dictionary server during import.");
logger::LogOptions log_options(argv[0]);
log_options.severity_ = logger::Severity::FATAL;
log_options.set_options(); // update default values
desc.add(log_options.get_options());
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
try {
create_directory_if_not_exists(BASE_PATH1);
create_directory_if_not_exists(BASE_PATH2);
} catch (std::exception& error) {
LOG(FATAL) << "Could not create string dictionary directories.";
}
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}