forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
867 lines (748 loc) · 21.6 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
// 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"
"os"
"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)
defer func() {
if !db.IsClose() {
require.NoError(t, db.Close())
}
}()
test(t, db)
}
func txPut(t *testing.T, db *DB, bucket string, key, value []byte, ttl uint32, expectErr error) {
err := db.Update(func(tx *Tx) error {
err = tx.Put(bucket, key, value, ttl)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
func txGet(t *testing.T, db *DB, bucket string, key []byte, expectVal []byte, expectErr error) {
err := db.View(func(tx *Tx) error {
e, err := tx.Get(bucket, key)
if expectErr != nil {
require.Equal(t, expectErr, err)
} else {
require.NoError(t, err)
require.EqualValuesf(t, expectVal, e.Value, "err Tx Get. got %s want %s", string(e.Value), string(expectVal))
}
return nil
})
require.NoError(t, err)
}
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 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 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"
key0 := GetTestBytes(0)
val0 := GetRandomBytes(24)
// put
txPut(t, db, bucket, key0, val0, Persistent, nil)
txGet(t, db, bucket, key0, val0, nil)
val1 := GetRandomBytes(24)
// update
txPut(t, db, bucket, key0, val1, Persistent, nil)
txGet(t, db, bucket, key0, val1, nil)
// del
txDel(t, db, bucket, key0, nil)
txGet(t, db, bucket, key0, val1, ErrKeyNotFound)
})
}
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"
txDel(t, db, testBucket, GetTestBytes(0), ErrNotFoundBucket)
txPut(t, db, testBucket, GetTestBytes(1), GetRandomBytes(24), Persistent, nil)
txDel(t, db, testBucket, GetTestBytes(0), ErrKeyNotFound)
})
}
func txSAdd(t *testing.T, db *DB, bucket string, key, value []byte, expectErr error) {
err := db.Update(func(tx *Tx) error {
err := tx.SAdd(bucket, key, value)
assertErr(t, err, expectErr)
return nil
})
require.NoError(t, err)
}
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 {
num := 0
err := tx.SKeys(bucket, pattern, func(key string) bool {
num += 1
return f(key)
})
if expectErr != nil {
assert.ErrorIs(t, expectErr, err)
} else {
assert.NoError(t, err)
assert.Equal(t, expectVal, num)
}
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) {
err := db.Update(func(tx *Tx) error {
err := tx.ZAdd(bucket, key, score, value)
assertErr(t, err, expectErr)
return nil
})
assert.NoError(t, err)
}
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 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, expectErr error, isLeft bool) {
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
})
require.NoError(t, err)
}
func txRange(t *testing.T, db *DB, bucket string, key []byte, start, end, expectLen int) {
err := db.View(func(tx *Tx) error {
list, err := tx.LRange(bucket, key, start, end)
require.NoError(t, err)
require.Equal(t, expectLen, len(list))
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"
txGet(t, db, bucket, GetTestBytes(0), nil, ErrBucketNotFound)
txPut(t, db, bucket, GetTestBytes(1), GetRandomBytes(24), Persistent, 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()))
txPut(t, db, bucket, GetTestBytes(0), GetRandomBytes(24), Persistent, 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)
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"
key := GetTestBytes(0)
val := GetTestBytes(0)
txDeleteBucket(t, db, DataStructureBTree, bucket, ErrBucketNotFound)
txPut(t, db, bucket, key, val, Persistent, nil)
txGet(t, db, bucket, key, val, nil)
txDeleteBucket(t, db, DataStructureBTree, bucket, nil)
txGet(t, db, bucket, key, nil, ErrBucketNotFound)
txDeleteBucket(t, db, DataStructureBTree, bucket, ErrBucketNotFound)
})
}
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"
key := GetTestBytes(0)
val := GetTestBytes(0)
txPut(t, db, bucket, key, val, Persistent, 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"
key := GetTestBytes(0)
val := GetTestBytes(0)
txPut(t, db, bucket, key, val, Persistent, 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_ChangeMode_RestartDB(t *testing.T) {
changeModeRestart := func(firstMode EntryIdxMode, secondMode EntryIdxMode) {
opts := DefaultOptions
opts.EntryIdxMode = firstMode
var err error
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
// k-v
for i := 0; i < 10; i++ {
txPut(t, db, bucket, GetTestBytes(i), GetTestBytes(i), Persistent, nil)
}
// list
for i := 0; i < 10; i++ {
txPush(t, db, bucket, GetTestBytes(0), GetTestBytes(i), nil, true)
}
err = db.Update(func(tx *Tx) error {
return tx.LRem(bucket, GetTestBytes(0), 1, GetTestBytes(5))
})
require.NoError(t, err)
for i := 0; i < 2; i++ {
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(9-i), nil, true)
}
for i := 0; i < 2; i++ {
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(i), nil, false)
}
// set
for i := 0; i < 10; i++ {
txSAdd(t, db, bucket, GetTestBytes(0), GetTestBytes(i), nil)
}
for i := 0; i < 3; i++ {
txSRem(t, db, bucket, GetTestBytes(0), GetTestBytes(i), nil)
}
// zset
for i := 0; i < 10; i++ {
txZAdd(t, db, bucket, GetTestBytes(0), GetTestBytes(i), float64(i), nil)
}
for i := 0; i < 3; i++ {
txZRem(t, db, bucket, GetTestBytes(0), GetTestBytes(i), nil)
}
require.NoError(t, db.Close())
opts.EntryIdxMode = secondMode
db, err = Open(opts)
require.NoError(t, err)
// k-v
for i := 0; i < 10; i++ {
txGet(t, db, bucket, GetTestBytes(i), GetTestBytes(i), nil)
}
// list
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(7), nil, true)
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(6), nil, true)
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(4), nil, true)
txPop(t, db, bucket, GetTestBytes(0), GetTestBytes(2), nil, false)
err = db.View(func(tx *Tx) error {
size, err := tx.LSize(bucket, GetTestBytes(0))
require.NoError(t, err)
require.Equal(t, 1, size)
return nil
})
require.NoError(t, err)
// set
for i := 0; i < 3; i++ {
txSIsMember(t, db, bucket, GetTestBytes(0), GetTestBytes(i), false)
}
for i := 3; i < 10; i++ {
txSIsMember(t, db, bucket, GetTestBytes(0), GetTestBytes(i), true)
}
// zset
for i := 0; i < 3; i++ {
txZScore(t, db, bucket, GetTestBytes(0), GetTestBytes(i), float64(i), ErrSortedSetMemberNotExist)
}
for i := 3; i < 10; i++ {
txZScore(t, db, bucket, GetTestBytes(0), GetTestBytes(i), float64(i), nil)
}
})
}
// HintKeyValAndRAMIdxMode to HintKeyAndRAMIdxMode
changeModeRestart(HintKeyValAndRAMIdxMode, HintKeyAndRAMIdxMode)
// HintKeyAndRAMIdxMode to HintKeyValAndRAMIdxMode
changeModeRestart(HintKeyAndRAMIdxMode, HintKeyValAndRAMIdxMode)
}
func TestTx_SmallFile(t *testing.T) {
opts := DefaultOptions
opts.SegmentSize = 100
opts.EntryIdxMode = HintKeyAndRAMIdxMode
runNutsDBTest(t, &opts, func(t *testing.T, db *DB) {
bucket := "bucket"
err := db.Update(func(tx *Tx) error {
for i := 0; i < 100; i++ {
err := tx.Put(bucket, GetTestBytes(i), GetTestBytes(i), Persistent)
if err != nil {
return err
}
}
return nil
})
require.Nil(t, err)
require.NoError(t, db.Close())
db, _ = Open(opts)
txGet(t, db, bucket, GetTestBytes(10), GetTestBytes(10), nil)
})
}