-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
98 lines (81 loc) · 2.07 KB
/
merge_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
package gobitcask
import (
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMergeData(t *testing.T) {
dirName := "./test"
defer os.RemoveAll(dirName)
bc, err := New(
WithDirName(dirName),
WithSegmentSize(128), // bytes
WithMergeOpt(&MergeOption{
Interval: 300 * time.Millisecond,
}),
)
assert.Nil(t, err)
assert.NotNil(t, bc)
for i := 0; i < 100; i++ {
key, val := fmt.Sprintf("key%v", i), fmt.Sprintf("val%v", i)
err = bc.Put([]byte(key), []byte(val))
assert.Nil(t, err)
}
<-time.After(500 * time.Millisecond)
for i := 0; i < 100; i++ {
key, val := fmt.Sprintf("key%v", i), fmt.Sprintf("val%v", i)
fetchedVal, err := bc.Get([]byte(key))
assert.Nil(t, err)
assert.EqualValues(t, val, fetchedVal)
}
}
func TestSkipHintAndMergeFile(t *testing.T) {
dirName := "./test"
defer os.RemoveAll(dirName)
bc, err := New(
WithDirName(dirName),
WithSegmentSize(128), // bytes
WithMergeOpt(&MergeOption{
Interval: 300 * time.Millisecond,
}),
)
assert.Nil(t, err)
assert.NotNil(t, bc)
for i := 0; i < 100; i++ {
key, val := fmt.Sprintf("key%v", i), fmt.Sprintf("val%v", i)
err = bc.Put([]byte(key), []byte(val))
assert.Nil(t, err)
}
// wait for 1st compaction
<-time.After(500 * time.Millisecond)
for i := 0; i < 100; i++ {
key, val := fmt.Sprintf("newkey%v", i), fmt.Sprintf("newval%v", i)
err = bc.Put([]byte(key), []byte(val))
assert.Nil(t, err)
}
// wait for 2nd compaction
<-time.After(500 * time.Millisecond)
err = bc.Close()
assert.Nil(t, err)
bc2, err := New(
WithDirName(dirName),
WithSegmentSize(128), // bytes
WithMergeOpt(&MergeOption{
Interval: 6 * time.Hour,
}),
)
assert.Nil(t, err)
assert.NotNil(t, bc2)
for i := 0; i < 100; i++ {
key, val := fmt.Sprintf("key%v", i), fmt.Sprintf("val%v", i)
fetchedVal, err := bc2.Get([]byte(key))
assert.Nil(t, err)
assert.EqualValues(t, val, fetchedVal)
key, val = fmt.Sprintf("newkey%v", i), fmt.Sprintf("newval%v", i)
fetchedVal, err = bc2.Get([]byte(key))
assert.Nil(t, err)
assert.EqualValues(t, val, fetchedVal)
}
}