forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_test.go
154 lines (129 loc) · 4.3 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
package gorocksdb
import (
"io/ioutil"
"testing"
"github.com/facebookgo/ensure"
)
func TestColumnFamilyOpen(t *testing.T) {
dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyOpen")
ensure.Nil(t, err)
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
ensure.Nil(t, err)
defer db.Close()
ensure.DeepEqual(t, len(cfh), 2)
cfh[0].Destroy()
cfh[1].Destroy()
actualNames, err := ListColumnFamilies(opts, dir)
ensure.Nil(t, err)
ensure.SameElements(t, actualNames, givenNames)
}
func TestColumnFamilyCreateDrop(t *testing.T) {
dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyCreate")
ensure.Nil(t, err)
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
db, err := OpenDb(opts, dir)
ensure.Nil(t, err)
defer db.Close()
cf, err := db.CreateColumnFamily(opts, "guide")
ensure.Nil(t, err)
defer cf.Destroy()
actualNames, err := ListColumnFamilies(opts, dir)
ensure.Nil(t, err)
ensure.SameElements(t, actualNames, []string{"default", "guide"})
ensure.Nil(t, db.DropColumnFamily(cf))
actualNames, err = ListColumnFamilies(opts, dir)
ensure.Nil(t, err)
ensure.SameElements(t, actualNames, []string{"default"})
}
func TestColumnFamilyBatchPutGet(t *testing.T) {
dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyPutGet")
ensure.Nil(t, err)
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
ensure.Nil(t, err)
defer db.Close()
ensure.DeepEqual(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)
ensure.Nil(t, db.Write(wo, b0))
actualVal0, err := db.GetCF(ro, cfh[0], givenKey0)
defer actualVal0.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal0.Data(), givenVal0)
b1 := NewWriteBatch()
defer b1.Destroy()
b1.PutCF(cfh[1], givenKey1, givenVal1)
ensure.Nil(t, db.Write(wo, b1))
actualVal1, err := db.GetCF(ro, cfh[1], givenKey1)
defer actualVal1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal1.Data(), givenVal1)
actualVal, err := db.GetCF(ro, cfh[0], givenKey1)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal.Size(), 0)
actualVal, err = db.GetCF(ro, cfh[1], givenKey0)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal.Size(), 0)
}
func TestColumnFamilyPutGetDelete(t *testing.T) {
dir, err := ioutil.TempDir("", "gorocksdb-TestColumnFamilyPutGet")
ensure.Nil(t, err)
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
db, cfh, err := OpenDbColumnFamilies(opts, dir, givenNames, []*Options{opts, opts})
ensure.Nil(t, err)
defer db.Close()
ensure.DeepEqual(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")
ensure.Nil(t, db.PutCF(wo, cfh[0], givenKey0, givenVal0))
actualVal0, err := db.GetCF(ro, cfh[0], givenKey0)
defer actualVal0.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal0.Data(), givenVal0)
ensure.Nil(t, db.PutCF(wo, cfh[1], givenKey1, givenVal1))
actualVal1, err := db.GetCF(ro, cfh[1], givenKey1)
defer actualVal1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal1.Data(), givenVal1)
actualVal, err := db.GetCF(ro, cfh[0], givenKey1)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal.Size(), 0)
actualVal, err = db.GetCF(ro, cfh[1], givenKey0)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal.Size(), 0)
ensure.Nil(t, db.DeleteCF(wo, cfh[0], givenKey0))
actualVal, err = db.GetCF(ro, cfh[0], givenKey0)
ensure.Nil(t, err)
ensure.DeepEqual(t, actualVal.Size(), 0)
}