-
Notifications
You must be signed in to change notification settings - Fork 35
/
vellum_test.go
680 lines (591 loc) · 12.7 KB
/
vellum_test.go
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
// Copyright (c) 2017 Couchbase, 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.
package vellum
import (
"bytes"
"io/ioutil"
"os"
"reflect"
"testing"
)
func TestRoundTripSimple(t *testing.T) {
f, err := ioutil.TempFile("", "vellum")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = insertStringMap(b, smallSample)
if err != nil {
t.Fatalf("error building: %v", err)
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
// first check all the expected values
got := map[string]uint64{}
itr, err := fst.Iterator(nil, nil)
for err == nil {
key, val := itr.Current()
got[string(key)] = val
err = itr.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
if !reflect.DeepEqual(smallSample, got) {
t.Errorf("expected %v, got: %v", smallSample, got)
}
// some additional tests for items that should not exist
if ok, _ := fst.Contains([]byte("mo")); ok {
t.Errorf("expected to not contain mo, but did")
}
if ok, _ := fst.Contains([]byte("monr")); ok {
t.Errorf("expected to not contain monr, but did")
}
if ok, _ := fst.Contains([]byte("thur")); ok {
t.Errorf("expected to not contain thur, but did")
}
if ok, _ := fst.Contains([]byte("thurp")); ok {
t.Errorf("expected to not contain thurp, but did")
}
if ok, _ := fst.Contains([]byte("tue")); ok {
t.Errorf("expected to not contain tue, but did")
}
if ok, _ := fst.Contains([]byte("tuesd")); ok {
t.Errorf("expected to not contain tuesd, but did")
}
// a few more misc non-existent values to increase coverage
if ok, _ := fst.Contains([]byte("x")); ok {
t.Errorf("expected to not contain x, but did")
}
// now try accessing it through the Automaton interface
exists := AutomatonContains(fst, []byte("mon"))
if !exists {
t.Errorf("expected key 'mon' to exist, doesn't")
}
exists = AutomatonContains(fst, []byte("mons"))
if exists {
t.Errorf("expected key 'mo' to not exist, does")
}
// now try accessing it through the Transducer interface
var val uint64
exists, val = TransducerGet(fst, []byte("mon"))
if !exists {
t.Errorf("expected key 'mon' to exist, doesn't")
}
if val != 2 {
t.Errorf("expected val 2, got %d", val)
}
// now try accessing it through the Transducer interface
// for key that doesn't exist
exists, _ = TransducerGet(fst, []byte("mons"))
if exists {
t.Errorf("expected key 'mo' to not exist, does")
}
minKey, _ := fst.GetMinKey()
if string(minKey) != "mon" {
t.Errorf("expected minKey 'mon', got %v", string(minKey))
}
maxKey, _ := fst.GetMaxKey()
if string(maxKey) != "tye" {
t.Errorf("expected maxKey 'tye', got %v", string(maxKey))
}
}
func TestRoundTripThousand(t *testing.T) {
dataset := thousandTestWords
randomThousandVals := randomValues(dataset)
f, err := ioutil.TempFile("", "vellum")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = insertStrings(b, dataset, randomThousandVals)
if err != nil {
t.Fatalf("error inserting thousand words: %v", err)
}
err = b.Close()
if err != nil {
t.Fatalf("error closing builder: %v", err)
}
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
// first check all the expected values
got := map[string]uint64{}
itr, err := fst.Iterator(nil, nil)
for err == nil {
key, val := itr.Current()
got[string(key)] = val
err = itr.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
for i := 0; i < len(dataset); i++ {
foundVal, ok := got[dataset[i]]
if !ok {
t.Fatalf("expected to find key, but didn't: %s", dataset[i])
}
if foundVal != randomThousandVals[i] {
t.Fatalf("expected value %d for key %s, but got %d", randomThousandVals[i], dataset[i], foundVal)
}
// now remove it
delete(got, dataset[i])
}
if len(got) != 0 {
t.Fatalf("expected got map to be empty after checking, still has %v", got)
}
}
func TestRoundTripEmpty(t *testing.T) {
f, err := ioutil.TempFile("", "vellum")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
if fst.Len() != 0 {
t.Fatalf("expected length 0, got %d", fst.Len())
}
// first check all the expected values
got := map[string]uint64{}
itr, err := fst.Iterator(nil, nil)
for err == nil {
key, val := itr.Current()
got[string(key)] = val
err = itr.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
if len(got) > 0 {
t.Errorf("expected not to see anything, got %v", got)
}
}
func TestRoundTripEmptyString(t *testing.T) {
f, err := ioutil.TempFile("", "vellum")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = b.Insert([]byte(""), 1)
if err != nil {
t.Fatalf("error inserting empty string")
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
if fst.Len() != 1 {
t.Fatalf("expected length 1, got %d", fst.Len())
}
// first check all the expected values
want := map[string]uint64{
"": 1,
}
got := map[string]uint64{}
itr, err := fst.Iterator(nil, nil)
for err == nil {
key, val := itr.Current()
got[string(key)] = val
err = itr.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("expected %v, got: %v", want, got)
}
}
func TestRoundTripEmptyStringAndOthers(t *testing.T) {
f, err := ioutil.TempFile("", "vellum")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = b.Insert([]byte(""), 0)
if err != nil {
t.Fatalf("error inserting empty string")
}
err = b.Insert([]byte("a"), 0)
if err != nil {
t.Fatalf("error inserting empty string")
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
if fst.Len() != 2 {
t.Fatalf("expected length 2, got %d", fst.Len())
}
// first check all the expected values
want := map[string]uint64{
"": 0,
"a": 0,
}
got := map[string]uint64{}
itr, err := fst.Iterator(nil, nil)
for err == nil {
key, val := itr.Current()
got[string(key)] = val
err = itr.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("expected %v, got: %v", want, got)
}
}
func TestMerge(t *testing.T) {
// first create a file with the smallSample data
f, err := ioutil.TempFile("", "vellum1")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err := New(f, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = insertStringMap(b, smallSample)
if err != nil {
t.Fatalf("error building: %v", err)
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
smallSample2 := map[string]uint64{
"bold": 25,
"last": 1,
"next": 500,
"tank": 0,
}
// next create a file with the smallSample2 data
f2, err := ioutil.TempFile("", "vellum1")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f2.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f2.Name())
if err != nil {
t.Fatal(err)
}
}()
b, err = New(f2, nil)
if err != nil {
t.Fatalf("error creating builder: %v", err)
}
err = insertStringMap(b, smallSample2)
if err != nil {
t.Fatalf("error building: %v", err)
}
err = b.Close()
if err != nil {
t.Fatalf("error closing: %v", err)
}
// now open them both up
fst, err := Open(f.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst.Close()
if err != nil {
t.Fatal(err)
}
}()
fst2, err := Open(f2.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fst2.Close()
if err != nil {
t.Fatal(err)
}
}()
// create full range iterators on both
itr, err := fst.Iterator(nil, nil)
if err != nil {
t.Fatalf("error opening iterator: %v", err)
}
itr2, err := fst2.Iterator(nil, nil)
if err != nil {
t.Fatalf("error opening iterator: %v", err)
}
f3, err := ioutil.TempFile("", "vellum1")
if err != nil {
t.Fatal(err)
}
defer func() {
err = f3.Close()
if err != nil {
t.Fatal(err)
}
err = os.Remove(f3.Name())
if err != nil {
t.Fatal(err)
}
}()
err = Merge(f3, nil, []Iterator{itr, itr2}, MergeSum)
if err != nil {
t.Fatalf("error merging iterators: %v", err)
}
// now check it
fstc, err := Open(f3.Name())
if err != nil {
t.Fatalf("error loading set: %v", err)
}
defer func() {
err = fstc.Close()
if err != nil {
t.Fatal(err)
}
}()
if fstc.Len() != 8 {
t.Fatalf("expected length 8, got %d", fst.Len())
}
// now check all the expected values
want := map[string]uint64{
"mon": 2,
"tues": 3,
"thurs": 5,
"tye": 99,
"bold": 25,
"last": 1,
"next": 500,
"tank": 0,
}
got := map[string]uint64{}
itrc, err := fstc.Iterator(nil, nil)
for err == nil {
key, val := itrc.Current()
got[string(key)] = val
err = itrc.Next()
}
if err != ErrIteratorDone {
t.Errorf("iterator error: %v", err)
}
if !reflect.DeepEqual(want, got) {
t.Errorf("expected %v, got: %v", want, got)
}
}
func BenchmarkKey4000K(b *testing.B) {
benchmarkBigKey(b, 4000000)
}
func BenchmarkKey1000K(b *testing.B) {
benchmarkBigKey(b, 1000000)
}
func BenchmarkKey100K(b *testing.B) {
benchmarkBigKey(b, 100000)
}
func BenchmarkKey10K(b *testing.B) {
benchmarkBigKey(b, 10000)
}
func BenchmarkKey1K(b *testing.B) {
benchmarkBigKey(b, 1000)
}
func benchmarkBigKey(b *testing.B, n int) {
big := bytes.Repeat([]byte("a"), n)
b.ResetTimer()
for i := 0; i < b.N; i++ {
b, err := New(ioutil.Discard, nil)
if err != nil {
break
}
err = b.Insert(big, 0)
if err != nil {
break
}
err = b.Close()
if err != nil {
break
}
}
}
func TestMaxWithSubstring(t *testing.T) {
var buf bytes.Buffer
builder, err := New(&buf, nil)
if err != nil {
t.Fatal(err)
}
err = builder.Insert([]byte("1"), 1)
if err != nil {
t.Fatal(err)
}
err = builder.Insert([]byte("11"), 1)
if err != nil {
t.Fatal(err)
}
err = builder.Insert([]byte("9"), 9)
if err != nil {
t.Fatal(err)
}
err = builder.Insert([]byte("99"), 99)
if err != nil {
t.Fatal(err)
}
err = builder.Close()
if err != nil {
t.Fatal(err)
}
fst, err := Load(buf.Bytes())
if err != nil {
t.Fatal(err)
}
mink, err := fst.GetMinKey()
if err != nil {
t.Fatal(err)
}
if string(mink) != "1" {
t.Fatalf("expected max key 1, got %s", string(mink))
}
maxk, err := fst.GetMaxKey()
if err != nil {
t.Fatal(err)
}
if string(maxk) != "99" {
t.Fatalf("expected max key 99, got %s", string(maxk))
}
}