-
Notifications
You must be signed in to change notification settings - Fork 336
/
db_test.go
1643 lines (1463 loc) · 47.2 KB
/
db_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
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 2019 The nutsdb Author. All rights reserved.
//
// 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 nutsdb
import (
"fmt"
"io/ioutil"
"math"
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
db *DB
opt Options
err error
)
const NutsDBTestDirPath = "/tmp/nutsdb-test"
func assertErr(t *testing.T, err error, expectErr error) {
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.NoError(t, err)
}
}
func removeDir(dir string) {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}
func runNutsDBTest(t *testing.T, opts *Options, test func(t *testing.T, db *DB)) {
if opts == nil {
opts = &DefaultOptions
}
if opts.Dir == "" {
opts.Dir = NutsDBTestDirPath
}
defer removeDir(opts.Dir)
db, err := Open(*opts)
require.NoError(t, err)
test(t, db)
t.Cleanup(func() {
if !db.IsClose() {
require.NoError(t, db.Close())
}
})
}
func txPut(t *testing.T, db *DB, bucket string, key, value []byte, ttl uint32, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err = tx.Put(bucket, key, value, ttl)
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txGet(t *testing.T, db *DB, bucket string, key []byte, expectVal []byte, expectErr error) {
err := db.View(func(tx *Tx) error {
value, err := tx.Get(bucket, key)
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.NoError(t, err)
require.EqualValuesf(t, expectVal, value, "err Tx Get. got %s want %s", string(value), string(expectVal))
}
return nil
})
require.NoError(t, err)
}
func txGetAll(t *testing.T, db *DB, bucket string, expectKeys [][]byte, expectValues [][]byte, expectErr error) {
require.NoError(t, db.View(func(tx *Tx) error {
keys, values, err := tx.GetAll(bucket)
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.NoError(t, err)
n := len(keys)
for i := 0; i < n; i++ {
require.Equal(t, expectKeys[i], keys[i])
require.Equal(t, expectValues[i], values[i])
}
}
return nil
}))
}
func txDel(t *testing.T, db *DB, bucket string, key []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.Delete(bucket, key)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txGetMaxOrMinKey(t *testing.T, db *DB, bucket string, isMax bool, expectVal []byte, expectErr error) {
err := db.View(func(tx *Tx) error {
value, err := tx.getMaxOrMinKey(bucket, isMax)
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.NoError(t, err)
require.EqualValuesf(t, expectVal, value, "err Tx Get. got %s want %s", string(value), string(expectVal))
}
return nil
})
require.NoError(t, err)
}
func txDeleteBucket(t *testing.T, db *DB, ds uint16, bucket string, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.DeleteBucket(ds, bucket)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txCreateBucket(t *testing.T, db *DB, ds uint16, bucket string, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.NewBucket(ds, bucket)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func InitOpt(fileDir string, isRemoveFiles bool) {
if fileDir == "" {
fileDir = "/tmp/nutsdbtest"
}
if isRemoveFiles {
files, _ := ioutil.ReadDir(fileDir)
for _, f := range files {
name := f.Name()
if name != "" {
err := os.RemoveAll(fileDir + "/" + name)
if err != nil {
panic(err)
}
}
}
}
opt = DefaultOptions
opt.Dir = fileDir
opt.SegmentSize = 8 * 1024
opt.CleanFdsCacheThreshold = 0.5
opt.MaxFdNumsInCache = 1024
}
func TestDB_Basic(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
key0 := GetTestBytes(0)
val0 := GetRandomBytes(24)
// put
txPut(t, db, bucket, key0, val0, Persistent, nil, nil)
txGet(t, db, bucket, key0, val0, nil)
val1 := GetRandomBytes(24)
// update
txPut(t, db, bucket, key0, val1, Persistent, nil, nil)
txGet(t, db, bucket, key0, val1, nil)
// del
txDel(t, db, bucket, key0, nil)
txGet(t, db, bucket, key0, val1, ErrKeyNotFound)
})
}
func TestDB_ReopenWithDelete(t *testing.T) {
var opts *Options
if opts == nil {
opts = &DefaultOptions
}
if opts.Dir == "" {
opts.Dir = NutsDBTestDirPath
}
db, err := Open(*opts)
require.NoError(t, err)
defer removeDir(opts.Dir)
bucket := "bucket"
txCreateBucket(t, db, DataStructureList, bucket, nil)
txPush(t, db, bucket, GetTestBytes(5), GetTestBytes(0), true, nil, nil)
txPush(t, db, bucket, GetTestBytes(5), GetTestBytes(1), true, nil, nil)
txDeleteBucket(t, db, DataStructureList, bucket, nil)
if !db.IsClose() {
require.NoError(t, db.Close())
}
db, err = Open(*opts)
require.NoError(t, err)
txCreateBucket(t, db, DataStructureList, bucket, nil)
txDeleteBucket(t, db, DataStructureList, bucket, nil)
if !db.IsClose() {
require.NoError(t, db.Close())
}
}
func TestDB_Flock(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
db2, err := Open(db.opt)
require.Nil(t, db2)
require.Equal(t, ErrDirLocked, err)
err = db.Close()
require.NoError(t, err)
db2, err = Open(db.opt)
require.NoError(t, err)
require.NotNil(t, db2)
err = db2.flock.Unlock()
require.NoError(t, err)
require.False(t, db2.flock.Locked())
err = db2.Close()
require.Error(t, err)
require.Equal(t, ErrDirUnlocked, err)
})
}
func TestDB_DeleteANonExistKey(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
testBucket := "test_bucket"
txCreateBucket(t, db, DataStructureBTree, testBucket, nil)
txDel(t, db, testBucket, GetTestBytes(0), ErrKeyNotFound)
txPut(t, db, testBucket, GetTestBytes(1), GetRandomBytes(24), Persistent, nil, nil)
txDel(t, db, testBucket, GetTestBytes(0), ErrKeyNotFound)
})
}
func TestDB_CheckListExpired(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
testBucket := "test_bucket"
txCreateBucket(t, db, DataStructureBTree, testBucket, nil)
txPut(t, db, testBucket, GetTestBytes(0), GetTestBytes(1), Persistent, nil, nil)
txPut(t, db, testBucket, GetTestBytes(1), GetRandomBytes(24), 1, nil, nil)
time.Sleep(1100 * time.Millisecond)
db.checkListExpired()
// this entry still alive
txGet(t, db, testBucket, GetTestBytes(0), GetTestBytes(1), nil)
// this entry will be deleted
txGet(t, db, testBucket, GetTestBytes(1), nil, ErrKeyNotFound)
})
}
func txLRem(t *testing.T, db *DB, bucket string, key []byte, count int, value []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.LRem(bucket, key, count, value)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txLRemByIndex(t *testing.T, db *DB, bucket string, key []byte, expectErr error, indexes ...int) {
err := db.Update(func(tx *Tx) error {
err := tx.LRemByIndex(bucket, key, indexes...)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txSAdd(t *testing.T, db *DB, bucket string, key, value []byte, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.SAdd(bucket, key, value)
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txSKeys(t *testing.T, db *DB, bucket, pattern string, f func(key string) bool, expectVal int, expectErr error) {
err := db.View(func(tx *Tx) error {
patternMatchNum := 0
err := tx.SKeys(bucket, pattern, func(key string) bool {
patternMatchNum += 1
return f(key)
})
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.Equal(t, expectVal, patternMatchNum)
}
return nil
})
require.NoError(t, err)
}
func txSIsMember(t *testing.T, db *DB, bucket string, key, value []byte, expect bool) {
err := db.View(func(tx *Tx) error {
ok, _ := tx.SIsMember(bucket, key, value)
require.Equal(t, expect, ok)
return nil
})
require.NoError(t, err)
}
func txSAreMembers(t *testing.T, db *DB, bucket string, key []byte, expect bool, value ...[]byte) {
err := db.View(func(tx *Tx) error {
ok, _ := tx.SAreMembers(bucket, key, value...)
require.Equal(t, expect, ok)
return nil
})
require.NoError(t, err)
}
func txSHasKey(t *testing.T, db *DB, bucket string, key []byte, expect bool) {
err := db.View(func(tx *Tx) error {
ok, _ := tx.SHasKey(bucket, key)
require.Equal(t, expect, ok)
return nil
})
require.NoError(t, err)
}
func txSMembers(t *testing.T, db *DB, bucket string, key []byte, expectLength int, expectErr error) {
err := db.View(func(tx *Tx) error {
members, err := tx.SMembers(bucket, key)
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.Equal(t, expectLength, len(members))
}
return nil
})
require.NoError(t, err)
}
func txSCard(t *testing.T, db *DB, bucket string, key []byte, expectLength int, expectErr error) {
err := db.View(func(tx *Tx) error {
length, err := tx.SCard(bucket, key)
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.Equal(t, expectLength, length)
}
return nil
})
require.NoError(t, err)
}
func txSDiffByOneBucket(t *testing.T, db *DB, bucket string, key1, key2 []byte, expectVal [][]byte, expectErr error) {
err := db.View(func(tx *Tx) error {
diff, err := tx.SDiffByOneBucket(bucket, key1, key2)
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.ElementsMatch(t, expectVal, diff)
}
return nil
})
require.NoError(t, err)
}
func txSDiffByTwoBucket(t *testing.T, db *DB, bucket1 string, key1 []byte, bucket2 string, key2 []byte, expectVal [][]byte, expectErr error) {
err := db.View(func(tx *Tx) error {
diff, err := tx.SDiffByTwoBuckets(bucket1, key1, bucket2, key2)
if expectErr != nil {
assert.ErrorIs(t, err, expectErr)
} else {
assert.NoError(t, err)
assert.ElementsMatch(t, expectVal, diff)
}
return nil
})
require.NoError(t, err)
}
func txSPop(t *testing.T, db *DB, bucket string, key []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
_, err := tx.SPop(bucket, key)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txSMoveByOneBucket(t *testing.T, db *DB, bucket1 string, key1, key2, val []byte, expectVal bool, expectErr error) {
err := db.View(func(tx *Tx) error {
ok, err := tx.SMoveByOneBucket(bucket1, key1, key2, val)
if expectErr != nil {
assert.ErrorIs(t, err, expectErr)
} else {
assert.NoError(t, err)
assert.Equal(t, expectVal, ok)
}
return nil
})
require.NoError(t, err)
}
func txSMoveByTwoBuckets(t *testing.T, db *DB, bucket1 string, key1 []byte, bucket2 string, key2 []byte, val []byte, expectVal bool, expectErr error) {
err := db.View(func(tx *Tx) error {
ok, err := tx.SMoveByTwoBuckets(bucket1, key1, bucket2, key2, val)
if expectErr != nil {
assert.ErrorIs(t, err, expectErr)
} else {
assert.NoError(t, err)
assert.Equal(t, expectVal, ok)
}
return nil
})
require.NoError(t, err)
}
func txSUnionByOneBucket(t *testing.T, db *DB, bucket1 string, key1, key2 []byte, expectVal [][]byte, expectErr error) {
err := db.View(func(tx *Tx) error {
union, err := tx.SUnionByOneBucket(bucket1, key1, key2)
if expectErr != nil {
assert.ErrorIs(t, err, expectErr)
} else {
assert.NoError(t, err)
assert.ElementsMatch(t, expectVal, union)
}
return nil
})
require.NoError(t, err)
}
func txSUnionByTwoBuckets(t *testing.T, db *DB, bucket1 string, key1 []byte, bucket2 string, key2 []byte, expectVal [][]byte, expectErr error) {
err := db.View(func(tx *Tx) error {
union, err := tx.SUnionByTwoBuckets(bucket1, key1, bucket2, key2)
if expectErr != nil {
assert.ErrorIs(t, err, expectErr)
} else {
assert.NoError(t, err)
assert.ElementsMatch(t, expectVal, union)
}
return nil
})
require.NoError(t, err)
}
func txSRem(t *testing.T, db *DB, bucket string, key, value []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.SRem(bucket, key, value)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txZAdd(t *testing.T, db *DB, bucket string, key, value []byte, score float64, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.ZAdd(bucket, key, score, value)
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txZRem(t *testing.T, db *DB, bucket string, key, value []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.ZRem(bucket, key, value)
assertErr(t, err, expectErr)
return nil
})
assert.NoError(t, err)
}
func txZCard(t *testing.T, db *DB, bucket string, key []byte, expectLength int, expectErr error) {
err := db.View(func(tx *Tx) error {
length, err := tx.ZCard(bucket, key)
if expectErr != nil {
assert.Equal(t, expectErr, err)
} else {
assert.Equal(t, expectLength, length)
}
return nil
})
assert.NoError(t, err)
}
func txZScore(t *testing.T, db *DB, bucket string, key, value []byte, expectScore float64, expectErr error) {
err := db.View(func(tx *Tx) error {
score, err := tx.ZScore(bucket, key, value)
if err != nil {
assert.Equal(t, expectErr, err)
} else {
assert.Equal(t, expectScore, score)
}
return nil
})
assert.NoError(t, err)
}
func txZRank(t *testing.T, db *DB, bucket string, key, value []byte, isRev bool, expectRank int, expectErr error) {
err := db.View(func(tx *Tx) error {
var (
rank int
err error
)
if isRev {
rank, err = tx.ZRevRank(bucket, key, value)
} else {
rank, err = tx.ZRank(bucket, key, value)
}
if expectErr != nil {
assert.Equal(t, expectErr, err)
} else {
assert.Equal(t, expectRank, rank)
}
return nil
})
assert.NoError(t, err)
}
func txZPop(t *testing.T, db *DB, bucket string, key []byte, isMax bool, expectVal []byte, expectScore float64, expectErr error) {
err := db.Update(func(tx *Tx) error {
var (
member *SortedSetMember
err error
)
if isMax {
member, err = tx.ZPopMax(bucket, key)
} else {
member, err = tx.ZPopMin(bucket, key)
}
if expectErr != nil {
assert.Equal(t, expectErr, err)
} else {
assert.Equal(t, expectVal, member.Value)
assert.Equal(t, expectScore, member.Score)
}
return nil
})
assert.NoError(t, err)
}
func txZPeekMin(t *testing.T, db *DB, bucket string, key, expectVal []byte, expectScore float64, expectErr, finalExpectErr error) {
err := db.View(func(tx *Tx) error {
minMem, err1 := tx.ZPeekMin(bucket, key)
assertErr(t, err1, finalExpectErr)
if expectErr == nil {
require.Equal(t, &SortedSetMember{
Value: expectVal,
Score: expectScore,
}, minMem)
}
return err1
})
assertErr(t, err, finalExpectErr)
}
func txZKeys(t *testing.T, db *DB, bucket, pattern string, f func(key string) bool, expectVal int, expectErr error) {
err := db.View(func(tx *Tx) error {
patternMatchNum := 0
err := tx.ZKeys(bucket, pattern, func(key string) bool {
patternMatchNum += 1
return f(key)
})
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.Equal(t, expectVal, patternMatchNum)
}
return nil
})
require.NoError(t, err)
}
func txPop(t *testing.T, db *DB, bucket string, key, expectVal []byte, expectErr error, isLeft bool) {
err := db.Update(func(tx *Tx) error {
var item []byte
var err error
if isLeft {
item, err = tx.LPop(bucket, key)
} else {
item, err = tx.RPop(bucket, key)
}
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.Equal(t, expectVal, item)
}
return nil
})
require.NoError(t, err)
}
func txPush(t *testing.T, db *DB, bucket string, key, val []byte, isLeft bool, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
var err error
if isLeft {
err = tx.LPush(bucket, key, val)
} else {
err = tx.RPush(bucket, key, val)
}
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txMPush(t *testing.T, db *DB, bucket string, key []byte, vals [][]byte, isLeft bool, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
var err error
if isLeft {
err = tx.LPush(bucket, key, vals...)
} else {
err = tx.RPush(bucket, key, vals...)
}
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txPushRaw(t *testing.T, db *DB, bucket string, key, val []byte, isLeft bool, expectErr error, finalExpectErr error) {
err := db.Update(func(tx *Tx) error {
var err error
if isLeft {
err = tx.LPushRaw(bucket, key, val)
} else {
err = tx.RPushRaw(bucket, key, val)
}
assertErr(t, err, expectErr)
return nil
})
assertErr(t, err, finalExpectErr)
}
func txExpireList(t *testing.T, db *DB, bucket string, key []byte, ttl uint32, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.ExpireList(bucket, key, ttl)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txGetListTTL(t *testing.T, db *DB, bucket string, key []byte, expectVal uint32, expectErr error) {
err := db.View(func(tx *Tx) error {
ttl, err := tx.GetListTTL(bucket, key)
assertErr(t, err, expectErr)
require.Equal(t, ttl, expectVal)
return nil
})
require.NoError(t, err)
}
func txLKeys(t *testing.T, db *DB, bucket, pattern string, expectLen int, expectErr error, keysOperation func(keys []string) bool) {
err := db.View(func(tx *Tx) error {
var keys []string
err := tx.LKeys(bucket, pattern, func(key string) bool {
keys = append(keys, key)
return keysOperation(keys)
})
assertErr(t, err, expectErr)
require.Equal(t, expectLen, len(keys))
return nil
})
require.NoError(t, err)
}
func txLRange(t *testing.T, db *DB, bucket string, key []byte, start, end, expectLen int, expectVal [][]byte, expectErr error) {
err := db.View(func(tx *Tx) error {
list, err := tx.LRange(bucket, key, start, end)
assertErr(t, err, expectErr)
require.Equal(t, expectLen, len(list))
if len(expectVal) > 0 {
for i, val := range list {
assert.Equal(t, expectVal[i], val)
}
}
return nil
})
require.NoError(t, err)
}
func txLSize(t *testing.T, db *DB, bucket string, key []byte, expectVal int, expectErr error) {
err := db.View(func(tx *Tx) error {
size, err := tx.LSize(bucket, key)
assertErr(t, err, expectErr)
require.Equal(t, expectVal, size)
return nil
})
require.NoError(t, err)
}
func txLTrim(t *testing.T, db *DB, bucket string, key []byte, start int, end int, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.LTrim(bucket, key, start, end)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txIterateBuckets(t *testing.T, db *DB, ds uint16, pattern string, f func(key string) bool, expectErr error, containsKey ...string) {
err := db.View(func(tx *Tx) error {
var elements []string
err := tx.IterateBuckets(ds, pattern, func(key string) bool {
if f != nil && !f(key) {
return false
}
elements = append(elements, key)
return true
})
if err != nil {
assert.Equal(t, expectErr, err)
} else {
assert.NoError(t, err)
for _, key := range containsKey {
assert.Contains(t, elements, key)
}
}
return nil
})
require.NoError(t, err)
}
func TestDB_GetKeyNotFound(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
txGet(t, db, bucket, GetTestBytes(0), nil, ErrKeyNotFound)
txPut(t, db, bucket, GetTestBytes(1), GetRandomBytes(24), Persistent, nil, nil)
txGet(t, db, bucket, GetTestBytes(0), nil, ErrKeyNotFound)
})
}
func TestDB_Backup(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
backUpDir := "/tmp/nutsdb-backup"
require.NoError(t, db.Backup(backUpDir))
})
}
func TestDB_BackupTarGZ(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
backUpFile := "/tmp/nutsdb-backup/backup.tar.gz"
f, err := os.Create(backUpFile)
require.NoError(t, err)
require.NoError(t, db.BackupTarGZ(f))
})
}
func TestDB_Close(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
require.NoError(t, db.Close())
require.Equal(t, ErrDBClosed, db.Close())
})
}
func TestDB_ErrThenReadWrite(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
bucket := "testForDeadLock"
err = db.View(
func(tx *Tx) error {
return fmt.Errorf("err happened")
})
require.NotNil(t, err)
err = db.View(
func(tx *Tx) error {
key := []byte("key1")
_, err := tx.Get(bucket, key)
if err != nil {
return err
}
return nil
})
require.NotNil(t, err)
notice := make(chan struct{})
go func() {
err = db.Update(
func(tx *Tx) error {
notice <- struct{}{}
return nil
})
require.NoError(t, err)
}()
select {
case <-notice:
case <-time.After(1 * time.Second):
t.Fatalf("exist deadlock")
}
})
}
func TestDB_ErrorHandler(t *testing.T) {
opts := DefaultOptions
handleErrCalled := false
opts.ErrorHandler = ErrorHandlerFunc(func(err error) {
handleErrCalled = true
})
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
err = db.View(
func(tx *Tx) error {
return fmt.Errorf("err happened")
})
require.NotNil(t, err)
require.Equal(t, handleErrCalled, true)
})
}
func TestDB_CommitBuffer(t *testing.T) {
bucket := "bucket"
opts := DefaultOptions
opts.CommitBufferSize = 8 * MB
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
require.Equal(t, int64(8*MB), db.opt.CommitBufferSize)
// When the database starts, the commit buffer should be allocated with the size of CommitBufferSize.
require.Equal(t, 0, db.commitBuffer.Len())
require.Equal(t, db.opt.CommitBufferSize, int64(db.commitBuffer.Cap()))
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
txPut(t, db, bucket, GetTestBytes(0), GetRandomBytes(24), Persistent, nil, nil)
// When tx is committed, content of commit buffer should be empty, but do not release memory
require.Equal(t, 0, db.commitBuffer.Len())
require.Equal(t, db.opt.CommitBufferSize, int64(db.commitBuffer.Cap()))
})
opts = DefaultOptions
opts.CommitBufferSize = 1 * KB
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
require.Equal(t, int64(1*KB), db.opt.CommitBufferSize)
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
err := db.Update(func(tx *Tx) error {
// making this tx big enough, it should not use the commit buffer
for i := 0; i < 1000; i++ {
err := tx.Put(bucket, GetTestBytes(i), GetRandomBytes(1024), Persistent)
require.NoError(t, err)
}
return nil
})
require.NoError(t, err)
require.Equal(t, 0, db.commitBuffer.Len())
require.Equal(t, db.opt.CommitBufferSize, int64(db.commitBuffer.Cap()))
})
}
func TestDB_DeleteBucket(t *testing.T) {
runNutsDBTest(t, nil, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
key := GetTestBytes(0)
val := GetTestBytes(0)
txPut(t, db, bucket, key, val, Persistent, nil, nil)
txGet(t, db, bucket, key, val, nil)
txDeleteBucket(t, db, DataStructureBTree, bucket, nil)
txPut(t, db, bucket, key, val, Persistent, ErrorBucketNotExist, nil)
})
}
func withDBOption(t *testing.T, opt Options, fn func(t *testing.T, db *DB)) {
db, err := Open(opt)
require.NoError(t, err)
defer func() {
os.RemoveAll(db.opt.Dir)
db.Close()
}()
fn(t, db)
}
func withDefaultDB(t *testing.T, fn func(t *testing.T, db *DB)) {
tmpdir, _ := os.MkdirTemp("", "nutsdb")
opt := DefaultOptions
opt.Dir = tmpdir
opt.SegmentSize = 8 * 1024
withDBOption(t, opt, fn)
}
func withRAMIdxDB(t *testing.T, fn func(t *testing.T, db *DB)) {
tmpdir, _ := os.MkdirTemp("", "nutsdb")
opt := DefaultOptions
opt.Dir = tmpdir
opt.EntryIdxMode = HintKeyAndRAMIdxMode
withDBOption(t, opt, fn)
}
func TestDB_HintKeyValAndRAMIdxMode_RestartDB(t *testing.T) {
opts := DefaultOptions
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
key := GetTestBytes(0)
val := GetTestBytes(0)
txPut(t, db, bucket, key, val, Persistent, nil, nil)
txGet(t, db, bucket, key, val, nil)
db.Close()
// restart db with HintKeyValAndRAMIdxMode EntryIdxMode
db, err := Open(db.opt)
require.NoError(t, err)
txGet(t, db, bucket, key, val, nil)
})
}
func TestDB_HintKeyAndRAMIdxMode_RestartDB(t *testing.T) {
opts := DefaultOptions
opts.EntryIdxMode = HintKeyAndRAMIdxMode
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
key := GetTestBytes(0)
val := GetTestBytes(0)
txPut(t, db, bucket, key, val, Persistent, nil, nil)
txGet(t, db, bucket, key, val, nil)
db.Close()
// restart db with HintKeyAndRAMIdxMode EntryIdxMode
db, err := Open(db.opt)
require.NoError(t, err)
txGet(t, db, bucket, key, val, nil)
})
}
func TestDB_HintKeyAndRAMIdxMode_LruCache(t *testing.T) {
opts := DefaultOptions
opts.EntryIdxMode = HintKeyAndRAMIdxMode
lruCacheSizes := []int{0, 5000, 10000, 20000}
for _, lruCacheSize := range lruCacheSizes {
opts.HintKeyAndRAMIdxCacheSize = lruCacheSize
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
txCreateBucket(t, db, DataStructureBTree, bucket, nil)
for i := 0; i < 10000; i++ {
key := []byte(fmt.Sprintf("%10d", i))
val := []byte(fmt.Sprintf("%10d", i))