forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discard_test.go
206 lines (184 loc) · 4.44 KB
/
discard_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
package lotusdb
import (
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
func TestDiscard_listenUpdates(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
// write enough data that can trigger flush operation.
var writeCount = 600000
for i := 0; i <= writeCount; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
// delete or rewrite some.
for i := 0; i < writeCount/2; i = i + 100 {
if i%2 == 0 {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
} else {
err := db.Delete(GetKey(i))
assert.Nil(t, err)
}
}
// write more to flush again.
for i := writeCount; i <= writeCount+300000; i++ {
err := db.Put(GetKey(i), GetValue128B())
assert.Nil(t, err)
}
// check the disacrd file.
cf := db.getColumnFamily(DefaultColumnFamilyName)
ccl, err := cf.vlog.discard.getCCL(10, 0.0000001)
assert.Nil(t, err)
assert.Equal(t, len(ccl), 1)
}
func TestDiscard_newDiscard(t *testing.T) {
t.Run("init", func(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
assert.Equal(t, len(dis.freeList), 341)
assert.Equal(t, len(dis.location), 0)
})
t.Run("with-data", func(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
for i := 1; i < 300; i = i * 5 {
dis.setTotal(uint32(i), 223)
dis.incrDiscard(uint32(i), i*10)
}
assert.Equal(t, len(dis.freeList), 337)
assert.Equal(t, len(dis.location), 4)
// reopen
dis2, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
assert.Equal(t, len(dis2.freeList), 337)
assert.Equal(t, len(dis2.location), 4)
})
}
func TestDiscard_setTotal(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
type args struct {
fid uint32
totalSize int
}
tests := []struct {
name string
dis *discard
args args
}{
{
"zero", dis, args{0, 10},
},
{
"normal", dis, args{334, 123224},
},
{
"set-again-1", dis, args{194, 100},
},
{
"set-again-2", dis, args{194, 150},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dis.setTotal(tt.args.fid, uint32(tt.args.totalSize))
})
}
}
func TestDiscard_clear(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
for i := 0; i < 341; i++ {
dis.setTotal(uint32(i), uint32(i+100))
dis.incrDiscard(uint32(i), i+10)
}
type args struct {
fid uint32
}
tests := []struct {
name string
dis *discard
args args
}{
{
"0", dis, args{0},
},
{
"33", dis, args{33},
},
{
"198", dis, args{198},
},
{
"340", dis, args{340},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.dis.clear(tt.args.fid)
})
}
}
func TestDiscard_incrDiscard(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
for i := 1; i < 300; i = i * 5 {
dis.setTotal(uint32(i-1), uint32(i+1000))
}
for i := 1; i < 300; i = i * 5 {
dis.incrDiscard(uint32(i-1), i+100)
}
ccl, err := dis.getCCL(10, 0.0000001)
assert.Nil(t, err)
assert.Equal(t, len(ccl), 4)
}
func TestDiscard_getCCL(t *testing.T) {
path := filepath.Join("/tmp", "lotusdb-discard")
os.MkdirAll(path, os.ModePerm)
defer os.RemoveAll(path)
dis, err := newDiscard(path, vlogDiscardName)
assert.Nil(t, err)
for i := 1; i < 2000; i = i * 5 {
dis.setTotal(uint32(i-1), uint32(i+1000))
}
for i := 1; i < 2000; i = i * 5 {
dis.incrDiscard(uint32(i-1), i+100)
}
t.Run("normal", func(t *testing.T) {
ccl, err := dis.getCCL(624, 0.0000001)
assert.Nil(t, err)
assert.Equal(t, len(ccl), 4)
})
t.Run("filter-some", func(t *testing.T) {
ccl, err := dis.getCCL(100, 0.2)
assert.Nil(t, err)
assert.Equal(t, len(ccl), 2)
})
t.Run("clear and get", func(t *testing.T) {
dis.clear(124)
ccl, err := dis.getCCL(100, 0.0001)
assert.Nil(t, err)
assert.Equal(t, len(ccl), 4)
})
}