This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_test.go
321 lines (272 loc) · 9 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
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
package grocksdb
import (
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
func TestColumnFamilyOpen(t *testing.T) {
dir := t.TempDir()
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
opts.SetCompression(LZ4Compression)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
require.Nil(t, err)
defer db.Close()
require.EqualValues(t, len(cfh), 2)
cfh[0].Destroy()
cfh[1].Destroy()
actualNames, err := ListColumnFamilies(opts, dir)
require.Nil(t, err)
require.EqualValues(t, actualNames, givenNames)
}
func TestColumnFamilyCreateDrop(t *testing.T) {
dir := t.TempDir()
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
opts.SetCompression(LZ4HCCompression)
db, err := OpenDb(opts, dir)
require.Nil(t, err)
defer db.Close()
cf, err := db.CreateColumnFamily(opts, "guide")
require.Nil(t, err)
defer cf.Destroy()
actualNames, err := ListColumnFamilies(opts, dir)
require.Nil(t, err)
require.EqualValues(t, actualNames, []string{"default", "guide"})
require.Nil(t, db.DropColumnFamily(cf))
actualNames, err = ListColumnFamilies(opts, dir)
require.Nil(t, err)
require.EqualValues(t, actualNames, []string{"default"})
}
func TestColumnFamilyBatchPutGet(t *testing.T) {
dir := t.TempDir()
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
require.Nil(t, err)
defer db.Close()
require.EqualValues(t, len(cfh), 2)
defer cfh[0].Destroy()
defer cfh[1].Destroy()
wo := NewDefaultWriteOptions()
defer wo.Destroy()
ro := NewDefaultReadOptions()
defer ro.Destroy()
givenKey0 := []byte("hello0")
givenVal0 := []byte("world0")
givenKey1 := []byte("hello1")
givenVal1 := []byte("world1")
b0 := NewWriteBatch()
defer b0.Destroy()
b0.PutCF(cfh[0], givenKey0, givenVal0)
require.Nil(t, db.Write(wo, b0))
actualVal0, err := db.GetCF(ro, cfh[0], givenKey0)
defer actualVal0.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal0.Data(), givenVal0)
b1 := NewWriteBatch()
defer b1.Destroy()
b1.PutCF(cfh[1], givenKey1, givenVal1)
require.Nil(t, db.Write(wo, b1))
actualVal1, err := db.GetCF(ro, cfh[1], givenKey1)
defer actualVal1.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal1.Data(), givenVal1)
actualVal, err := db.GetCF(ro, cfh[0], givenKey1)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
actualVal, err = db.GetCF(ro, cfh[1], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
{
v := db.KeyMayExistsCF(ro, cfh[0], givenKey0, "")
defer v.Free()
require.True(t, v.Size() > 0)
}
// trigger flush
require.Nil(t, db.FlushCF(cfh[0], NewDefaultFlushOptions()))
meta := db.GetColumnFamilyMetadataCF(cfh[0])
require.NotNil(t, meta)
runtime.GC()
require.True(t, meta.Size() > 0)
require.True(t, meta.FileCount() > 0)
require.Equal(t, "default", meta.Name())
{
lms := meta.LevelMetas()
for _, lm := range lms {
require.True(t, lm.Level() >= 0)
require.Equal(t, lm.size, lm.Size())
sms := lm.SstMetas()
for _, sm := range sms {
require.True(t, len(sm.RelativeFileName()) > 0)
require.True(t, sm.Size() > 0)
require.True(t, len(sm.SmallestKey()) > 0)
require.True(t, len(sm.LargestKey()) > 0)
}
}
}
meta = db.GetColumnFamilyMetadataCF(cfh[1])
require.NotNil(t, meta)
require.Equal(t, "guide", meta.Name())
}
func TestColumnFamilyPutGetDelete(t *testing.T) {
dir := t.TempDir()
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
opts.SetCompression(SnappyCompression)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
require.Nil(t, err)
defer db.Close()
require.EqualValues(t, len(cfh), 2)
defer cfh[0].Destroy()
defer cfh[1].Destroy()
wo := NewDefaultWriteOptions()
defer wo.Destroy()
ro := NewDefaultReadOptions()
defer ro.Destroy()
givenKey0 := []byte("hello0")
givenVal0 := []byte("world0")
givenKey1 := []byte("hello1")
givenVal1 := []byte("world1")
{
require.Nil(t, db.PutCF(wo, cfh[0], givenKey0, givenVal0))
actualVal0, err := db.GetCF(ro, cfh[0], givenKey0)
defer actualVal0.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal0.Data(), givenVal0)
require.Nil(t, db.PutCF(wo, cfh[1], givenKey1, givenVal1))
actualVal1, err := db.GetCF(ro, cfh[1], givenKey1)
defer actualVal1.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal1.Data(), givenVal1)
actualVal, err := db.GetCF(ro, cfh[0], givenKey1)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
actualVal, err = db.GetCF(ro, cfh[1], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
require.Nil(t, db.DeleteCF(wo, cfh[0], givenKey0))
actualVal, err = db.GetCF(ro, cfh[0], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
{
v := db.KeyMayExistsCF(ro, cfh[0], givenKey0, "")
defer v.Free()
}
}
{
require.Nil(t, db.PutCF(wo, cfh[0], givenKey0, givenVal0))
actualVal0, err := db.GetCF(ro, cfh[0], givenKey0)
defer actualVal0.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal0.Data(), givenVal0)
require.Nil(t, db.DeleteRangeCF(wo, cfh[0], givenKey0, givenKey1))
actualVal, err := db.GetCF(ro, cfh[0], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
actualVal1, err := db.GetCF(ro, cfh[1], givenKey1)
defer actualVal1.Free()
require.Nil(t, err)
require.EqualValues(t, actualVal1.Data(), givenVal1)
}
}
func newTestDBCF(t *testing.T) (db *DB, cfh []*ColumnFamilyHandle, cleanup func()) {
dir := t.TempDir()
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
opts.SetCompression(ZLibCompression)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
require.Nil(t, err)
for i := 0; i < 5; i++ {
require.Equal(t, "default", cfh[0].Name())
require.EqualValues(t, 0, cfh[0].ID())
require.Equal(t, "guide", cfh[1].Name())
require.EqualValues(t, 1, cfh[1].ID())
runtime.GC()
}
cleanup = func() {
for _, cf := range cfh {
cf.Destroy()
}
db.Close()
}
return db, cfh, cleanup
}
func TestColumnFamilyMultiGet(t *testing.T) {
db, cfh, cleanup := newTestDBCF(t)
defer cleanup()
var (
givenKey1 = []byte("hello1")
givenKey2 = []byte("hello2")
givenKey3 = []byte("hello3")
givenVal1 = []byte("world1")
givenVal2 = []byte("world2")
givenVal3 = []byte("world3")
wo = NewDefaultWriteOptions()
ro = NewDefaultReadOptions()
)
// create
require.Nil(t, db.PutCF(wo, cfh[0], givenKey1, givenVal1))
require.Nil(t, db.PutCF(wo, cfh[1], givenKey2, givenVal2))
require.Nil(t, db.PutCF(wo, cfh[1], givenKey3, givenVal3))
// column family 0 only has givenKey1
{
values, err := db.MultiGetCF(ro, cfh[0], []byte("noexist"), givenKey1, givenKey2, givenKey3)
require.Nil(t, err)
require.EqualValues(t, len(values), 4)
require.EqualValues(t, values[0].Data(), []byte(nil))
require.EqualValues(t, values[1].Data(), givenVal1)
require.EqualValues(t, values[2].Data(), []byte(nil))
require.EqualValues(t, values[3].Data(), []byte(nil))
values.Destroy()
}
// try to compact
require.NoError(t, db.SuggestCompactRangeCF(cfh[0], Range{}))
db.CompactRangeCF(cfh[0], Range{})
{
values, err := db.MultiGetCF(ro, cfh[0], []byte("noexist"), givenKey1, givenKey2, givenKey3)
require.Nil(t, err)
require.EqualValues(t, len(values), 4)
require.EqualValues(t, values[0].Data(), []byte(nil))
require.EqualValues(t, values[1].Data(), givenVal1)
require.EqualValues(t, values[2].Data(), []byte(nil))
require.EqualValues(t, values[3].Data(), []byte(nil))
values.Destroy()
}
// column family 1 only has givenKey2 and givenKey3
values, err := db.MultiGetCF(ro, cfh[1], []byte("noexist"), givenKey1, givenKey2, givenKey3)
defer values.Destroy()
require.Nil(t, err)
require.EqualValues(t, len(values), 4)
require.EqualValues(t, values[0].Data(), []byte(nil))
require.EqualValues(t, values[1].Data(), []byte(nil))
require.EqualValues(t, values[2].Data(), givenVal2)
require.EqualValues(t, values[3].Data(), givenVal3)
// getting them all from the right CF should return them all
values, err = db.MultiGetCFMultiCF(ro,
ColumnFamilyHandles{cfh[0], cfh[1], cfh[1]},
[][]byte{givenKey1, givenKey2, givenKey3},
)
defer values.Destroy()
require.Nil(t, err)
require.EqualValues(t, len(values), 3)
require.EqualValues(t, values[0].Data(), givenVal1)
require.EqualValues(t, values[1].Data(), givenVal2)
require.EqualValues(t, values[2].Data(), givenVal3)
}
func TestCFMetadata(t *testing.T) {
db := newTestDB(t, nil)
defer db.Close()
meta := db.GetColumnFamilyMetadata()
require.NotNil(t, meta)
require.Equal(t, "default", meta.Name())
}