-
Notifications
You must be signed in to change notification settings - Fork 68
/
cf_ts_test.go
178 lines (153 loc) · 5.65 KB
/
cf_ts_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
package grocksdb
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestColumnFamilyPutGetDeleteWithTS(t *testing.T) {
t.Parallel()
dir := t.TempDir()
givenNames := []string{"default", "guide"}
opts := NewDefaultOptions()
opts.SetCreateIfMissingColumnFamilies(true)
opts.SetCreateIfMissing(true)
opts.SetCompression(SnappyCompression)
opts.SetComparator(newDefaultComparatorWithTS())
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")
givenKey1 := []byte("hello1")
givenVal0 := []byte("world0")
givenVal1 := []byte("world1")
givenTs0 := marshalTimestamp(1)
givenTs1 := marshalTimestamp(2)
givenTs2 := marshalTimestamp(3)
{
ro.SetTimestamp(givenTs2)
require.Nil(t, db.PutCFWithTS(wo, cfh[0], givenKey0, givenTs0, givenVal0))
actualVal0, actualTs0, err := db.GetCFWithTS(ro, cfh[0], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal0.Data(), givenVal0)
require.EqualValues(t, actualTs0.Data(), givenTs0)
actualVal0.Free()
actualTs0.Free()
require.Nil(t, db.PutCFWithTS(wo, cfh[1], givenKey1, givenTs1, givenVal1))
actualVal1, actualTs1, err := db.GetCFWithTS(ro, cfh[1], givenKey1)
require.Nil(t, err)
require.EqualValues(t, actualVal1.Data(), givenVal1)
require.EqualValues(t, actualTs1.Data(), givenTs1)
actualVal1.Free()
actualTs1.Free()
actualVal, actualTs, err := db.GetCFWithTS(ro, cfh[0], givenKey1)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
require.EqualValues(t, actualTs.Size(), 0)
actualVal.Free()
actualTs.Free()
actualVal, actualTs, err = db.GetCFWithTS(ro, cfh[1], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
require.EqualValues(t, actualTs.Size(), 0)
actualVal.Free()
actualTs.Free()
require.Nil(t, db.DeleteCFWithTS(wo, cfh[0], givenKey0, givenTs2))
actualVal, actualTs, err = db.GetCFWithTS(ro, cfh[0], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal.Size(), 0)
require.EqualValues(t, actualTs.Size(), 0)
actualVal.Free()
actualTs.Free()
}
{
require.Nil(t, db.PutCFWithTS(wo, cfh[0], givenKey0, givenTs2, givenVal0))
actualVal0, actualTs0, err := db.GetCFWithTS(ro, cfh[0], givenKey0)
require.Nil(t, err)
require.EqualValues(t, actualVal0.Data(), givenVal0)
require.EqualValues(t, actualTs0.Data(), givenTs2)
actualVal0.Free()
actualTs0.Free()
actualVal1, actualTs1, err := db.GetCFWithTS(ro, cfh[1], givenKey1)
require.Nil(t, err)
require.EqualValues(t, actualVal1.Data(), givenVal1)
require.EqualValues(t, actualTs1.Data(), givenTs1)
actualVal1.Free()
actualTs1.Free()
}
}
func TestColumnFamilyMultiGetWithTS(t *testing.T) {
t.Parallel()
db, cfh, cleanup := newTestDBMultiCF(t, []string{"default", "custom"}, func(opts *Options) {
opts.SetComparator(newDefaultComparatorWithTS())
})
defer cleanup()
var (
givenKey1 = []byte("hello1")
givenKey2 = []byte("hello2")
givenKey3 = []byte("hello3")
givenVal1 = []byte("world1")
givenVal2 = []byte("world2")
givenVal3 = []byte("world3")
givenTs1 = marshalTimestamp(0)
givenTs2 = marshalTimestamp(2)
givenTs3 = marshalTimestamp(3)
)
wo := NewDefaultWriteOptions()
defer wo.Destroy()
ro := NewDefaultReadOptions()
ro.SetTimestamp(givenTs3)
defer ro.Destroy()
// create
require.Nil(t, db.PutCFWithTS(wo, cfh[0], givenKey1, givenTs1, givenVal1))
require.Nil(t, db.PutCFWithTS(wo, cfh[1], givenKey2, givenTs2, givenVal2))
require.Nil(t, db.PutCFWithTS(wo, cfh[1], givenKey3, givenTs3, givenVal3))
// column family 0 only has givenKey1
values, times, err := db.MultiGetCFWithTS(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))
require.EqualValues(t, times[0].Data(), []byte(nil))
require.EqualValues(t, times[1].Data(), givenTs1)
require.EqualValues(t, times[2].Data(), []byte(nil))
require.EqualValues(t, times[3].Data(), []byte(nil))
values.Destroy()
times.Destroy()
// column family 1 only has givenKey2 and givenKey3
values, times, err = db.MultiGetCFWithTS(ro, cfh[1], []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(), []byte(nil))
require.EqualValues(t, values[2].Data(), givenVal2)
require.EqualValues(t, values[3].Data(), givenVal3)
require.EqualValues(t, times[0].Data(), []byte(nil))
require.EqualValues(t, times[1].Data(), []byte(nil))
require.EqualValues(t, times[2].Data(), givenTs2)
require.EqualValues(t, times[3].Data(), givenTs3)
values.Destroy()
times.Destroy()
// getting them all from the right CF
values, times, err = db.MultiGetMultiCFWithTS(ro,
ColumnFamilyHandles{cfh[0], cfh[1], cfh[1]},
[][]byte{givenKey1, givenKey2, givenKey3},
)
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)
require.EqualValues(t, times[0].Data(), []byte{0, 0, 0, 0, 0, 0, 0, 0})
require.EqualValues(t, times[1].Data(), givenTs2)
require.EqualValues(t, times[2].Data(), givenTs3)
values.Destroy()
times.Destroy()
}