forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_test.go
276 lines (244 loc) · 6.28 KB
/
cf_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
package lotusdb
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
func TestLotusDB_OpenColumnFamily(t *testing.T) {
path, err := filepath.Abs(filepath.Join("/tmp", "lotusdb-opencf"))
assert.Nil(t, err)
opts := DefaultOptions(path)
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
opencf := func(opts ColumnFamilyOptions) {
cf, err := db.OpenColumnFamily(opts)
assert.Nil(t, err)
assert.NotNil(t, cf)
}
t.Run("default", func(t *testing.T) {
cfopt := DefaultColumnFamilyOptions("cf-1")
opencf(cfopt)
})
t.Run("spec-dir", func(t *testing.T) {
cfopt := DefaultColumnFamilyOptions("cf-2")
dir, _ := ioutil.TempDir("", "lotusdb-opencf2")
defer func() {
_ = os.RemoveAll(dir)
}()
cfopt.DirPath = dir
opencf(cfopt)
})
t.Run("spec-val-dir", func(t *testing.T) {
cfopt := DefaultColumnFamilyOptions("cf-1")
dir, _ := ioutil.TempDir("", "lotusdb")
valDir, _ := ioutil.TempDir("", "lotus-val")
defer func() {
_ = os.RemoveAll(dir)
_ = os.RemoveAll(valDir)
}()
cfopt.DirPath = dir
cfopt.ValueLogDir = valDir
opencf(cfopt)
})
}
func TestColumnFamily_Put(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
type fields struct {
cf *ColumnFamily
}
type args struct {
key []byte
value []byte
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"nil-key-val", fields{cf: cf}, args{key: nil, value: nil}, false,
},
{
"nil-key", fields{cf: cf}, args{key: nil, value: GetValue16B()}, false,
},
{
"nil-val", fields{cf: cf}, args{key: GetKey(4423), value: nil}, false,
},
{
"with-key-val", fields{cf: cf}, args{key: GetKey(990), value: GetValue16B()}, false,
},
{
"with-key-big-val", fields{cf: cf}, args{key: GetKey(44012), value: GetValue4K()}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cf := tt.fields.cf
if err := cf.Put(tt.args.key, tt.args.value); (err != nil) != tt.wantErr {
t.Errorf("Put() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestColumnFamily_PutWithOptions(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
type fields struct {
cf *ColumnFamily
}
type args struct {
key []byte
value []byte
opt *WriteOptions
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"nil-options", fields{cf: cf}, args{key: GetKey(13), value: GetValue128B(), opt: nil}, false,
},
{
"with-sync", fields{cf: cf}, args{key: GetKey(99832), value: GetValue128B(), opt: &WriteOptions{Sync: true}}, false,
},
{
"with-disableWAL", fields{cf: cf}, args{key: GetKey(54221), value: GetValue128B(), opt: &WriteOptions{DisableWal: true}}, false,
},
{
"with-ttl", fields{cf: cf}, args{key: GetKey(9901), value: GetValue128B(), opt: &WriteOptions{ExpiredAt: time.Now().Add(time.Minute).Unix()}}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cf := tt.fields.cf
if err := cf.PutWithOptions(tt.args.key, tt.args.value, tt.args.opt); (err != nil) != tt.wantErr {
t.Errorf("PutWithOptions() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestColumnFamily_Get(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
// write some data for getting
for i := 0; i < 100; i++ {
err := db.Put(GetKey(i), GetValue16B())
if i == 43 {
err := cf.Put(GetKey(i), []byte("lotusdb"))
assert.Nil(t, err)
}
assert.Nil(t, err)
}
type fields struct {
cf *ColumnFamily
}
type args struct {
key []byte
}
tests := []struct {
name string
fields fields
args args
want []byte
wantErr bool
}{
{
"nil", fields{cf: cf}, args{key: nil}, nil, false,
},
{
"not-exist", fields{cf: cf}, args{key: GetKey(9903)}, nil, false,
},
{
"get-from-memtable", fields{cf: cf}, args{key: GetKey(43)}, []byte{108, 111, 116, 117, 115, 100, 98}, false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cf := tt.fields.cf
got, err := cf.Get(tt.args.key)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
}
})
}
}
func TestColumnFamily_Stat(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
// write some data
for i := 0; i < 100; i++ {
err := db.Put(GetKey(i), GetValue16B())
assert.Nil(t, err)
}
stat, err := cf.Stat()
assert.Nil(t, err)
assert.NotNil(t, stat)
}
func TestColumnFamily_IsClosed(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
c1 := cf.IsClosed()
assert.False(t, c1)
err = cf.Close()
assert.Nil(t, err)
c2 := cf.IsClosed()
assert.True(t, c2)
}
func TestColumnFamily_Sync(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
// write some data
for i := 0; i < 100; i++ {
err := db.Put(GetKey(i), GetValue16B())
assert.Nil(t, err)
}
err = cf.Sync()
assert.Nil(t, err)
}
func TestColumnFamily_Options(t *testing.T) {
opts := DefaultOptions("/tmp" + separator + "lotusdb")
db, err := Open(opts)
assert.Nil(t, err)
defer destroyDB(db)
cf, err := db.OpenColumnFamily(DefaultColumnFamilyOptions("cf_default"))
assert.Nil(t, err)
options := cf.Options()
assert.NotNil(t, options.CfName)
}