-
Notifications
You must be signed in to change notification settings - Fork 1
/
vlog_test.go
158 lines (146 loc) · 3.88 KB
/
vlog_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
// Copyright 2021 hardcore-os Project Authors
//
// 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 corekv
import (
"bytes"
"math/rand"
"os"
"testing"
"github.com/hardcore-os/corekv/utils"
"github.com/stretchr/testify/require"
)
var (
// 初始化opt
opt = &Options{
WorkDir: "./work_test",
SSTableMaxSz: 1 << 10,
MemTableSize: 1 << 10,
ValueLogFileSize: 1 << 20,
ValueThreshold: 0,
MaxBatchCount: 10,
MaxBatchSize: 1 << 20,
}
)
func TestVlogBase(t *testing.T) {
// 清理目录
clearDir()
// 打开DB
db := Open(opt)
defer db.Close()
log := db.vlog
var err error
// 创建一个简单的kv entry对象
const val1 = "sampleval012345678901234567890123"
const val2 = "samplevalb012345678901234567890123"
require.True(t, int64(len(val1)) >= db.opt.ValueThreshold)
e1 := &utils.Entry{
Key: []byte("samplekey"),
Value: []byte(val1),
Meta: utils.BitValuePointer,
}
e2 := &utils.Entry{
Key: []byte("samplekeyb"),
Value: []byte(val2),
Meta: utils.BitValuePointer,
}
// 构建一个批量请求的request
b := new(request)
b.Entries = []*utils.Entry{e1, e2}
// 直接写入vlog中
log.write([]*request{b})
require.Len(t, b.Ptrs, 2)
t.Logf("Pointer written: %+v %+v\n", b.Ptrs[0], b.Ptrs[1])
// 从vlog中使用 value ptr指针中查询写入的分段vlog文件
buf1, lf1, err1 := log.readValueBytes(b.Ptrs[0])
buf2, lf2, err2 := log.readValueBytes(b.Ptrs[1])
require.NoError(t, err1)
require.NoError(t, err2)
// 关闭会调的锁
defer utils.RunCallback(log.getUnlockCallback(lf1))
defer utils.RunCallback((log.getUnlockCallback(lf2)))
e1, err = lf1.DecodeEntry(buf1, b.Ptrs[0].Offset)
require.NoError(t, err)
// 从vlog文件中通过指指针反序列化回 entry对象
e2, err = lf1.DecodeEntry(buf2, b.Ptrs[1].Offset)
require.NoError(t, err)
// 比较entry对象是否相等
readEntries := []utils.Entry{*e1, *e2}
require.EqualValues(t, []utils.Entry{
{
Key: []byte("samplekey"),
Value: []byte(val1),
Meta: utils.BitValuePointer,
Offset: b.Ptrs[0].Offset,
},
{
Key: []byte("samplekeyb"),
Value: []byte(val2),
Meta: utils.BitValuePointer,
Offset: b.Ptrs[1].Offset,
},
}, readEntries)
}
func clearDir() {
_, err := os.Stat(opt.WorkDir)
if err == nil {
os.RemoveAll(opt.WorkDir)
}
os.Mkdir(opt.WorkDir, os.ModePerm)
}
func TestValueGC(t *testing.T) {
clearDir()
opt.ValueLogFileSize = 1 << 20
kv := Open(opt)
defer kv.Close()
sz := 32 << 10
kvList := []*utils.Entry{}
for i := 0; i < 100; i++ {
e := newRandEntry(sz)
kvList = append(kvList, &utils.Entry{
Key: e.Key,
Value: e.Value,
Meta: e.Meta,
ExpiresAt: e.ExpiresAt,
})
require.NoError(t, kv.Set(e))
}
kv.RunValueLogGC(0.9)
for _, e := range kvList {
item, err := kv.Get(e.Key)
require.NoError(t, err)
val := getItemValue(t, item)
require.NotNil(t, val)
require.True(t, bytes.Equal(item.Key, e.Key), "key not equal: e:%s, v:%s", e.Key, item.Key)
require.True(t, bytes.Equal(item.Value, e.Value), "value not equal: e:%s, v:%s", e.Value, item.Key)
}
}
func newRandEntry(sz int) *utils.Entry {
v := make([]byte, sz)
rand.Read(v[:rand.Intn(sz)])
e := utils.BuildEntry()
e.Value = v
return e
}
func getItemValue(t *testing.T, item *utils.Entry) (val []byte) {
t.Helper()
if item == nil {
return nil
}
var v []byte
v = append(v, item.Value...)
if v == nil {
return nil
}
return v
}