forked from cosmos/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_operator_test.go
185 lines (155 loc) · 5.83 KB
/
merge_operator_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
package gorocksdb
import (
"testing"
"github.com/facebookgo/ensure"
)
func TestMergeOperator(t *testing.T) {
var (
givenKey = []byte("hello")
givenVal1 = []byte("foo")
givenVal2 = []byte("bar")
givenMerged = []byte("foobar")
)
merger := &mockMergeOperator{
fullMerge: func(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, existingValue, givenVal1)
ensure.DeepEqual(&fatalAsError{t}, operands, [][]byte{givenVal2})
return givenMerged, true
},
}
db := newTestDB(t, "TestMergeOperator", func(opts *Options) {
opts.SetMergeOperator(merger)
})
defer db.Close()
wo := NewDefaultWriteOptions()
ensure.Nil(t, db.Put(wo, givenKey, givenVal1))
ensure.Nil(t, db.Merge(wo, givenKey, givenVal2))
// trigger a compaction to ensure that a merge is performed
db.CompactRange(Range{nil, nil})
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), givenMerged)
}
func TestPartialMergeOperator(t *testing.T) {
var (
givenKey = []byte("hello")
startingVal = []byte("foo")
mergeVal1 = []byte("bar")
mergeVal2 = []byte("baz")
fMergeResult = []byte("foobarbaz")
pMergeResult = []byte("barbaz")
)
merger := &mockMergePartialOperator{
fullMerge: func(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, existingValue, startingVal)
ensure.DeepEqual(&fatalAsError{t}, operands[0], pMergeResult)
return fMergeResult, true
},
partialMerge: func(key, leftOperand, rightOperand []byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, leftOperand, mergeVal1)
ensure.DeepEqual(&fatalAsError{t}, rightOperand, mergeVal2)
return pMergeResult, true
},
}
db := newTestDB(t, "TestMergeOperator", func(opts *Options) {
opts.SetMergeOperator(merger)
})
defer db.Close()
wo := NewDefaultWriteOptions()
defer wo.Destroy()
// insert a starting value and compact to trigger merges
ensure.Nil(t, db.Put(wo, givenKey, startingVal))
// trigger a compaction to ensure that a merge is performed
db.CompactRange(Range{nil, nil})
// we expect these two operands to be passed to merge partial
ensure.Nil(t, db.Merge(wo, givenKey, mergeVal1))
ensure.Nil(t, db.Merge(wo, givenKey, mergeVal2))
// trigger a compaction to ensure that a
// partial and full merge are performed
db.CompactRange(Range{nil, nil})
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), fMergeResult)
}
func TestMergeMultiOperator(t *testing.T) {
var (
givenKey = []byte("hello")
startingVal = []byte("foo")
mergeVal1 = []byte("bar")
mergeVal2 = []byte("baz")
fMergeResult = []byte("foobarbaz")
pMergeResult = []byte("barbaz")
)
merger := &mockMergeMultiOperator{
fullMerge: func(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, existingValue, startingVal)
ensure.DeepEqual(&fatalAsError{t}, operands[0], pMergeResult)
return fMergeResult, true
},
partialMergeMulti: func(key []byte, operands [][]byte) ([]byte, bool) {
ensure.DeepEqual(&fatalAsError{t}, key, givenKey)
ensure.DeepEqual(&fatalAsError{t}, operands[0], mergeVal1)
ensure.DeepEqual(&fatalAsError{t}, operands[1], mergeVal2)
return pMergeResult, true
},
}
db := newTestDB(t, "TestMergeOperator", func(opts *Options) {
opts.SetMergeOperator(merger)
})
defer db.Close()
wo := NewDefaultWriteOptions()
defer wo.Destroy()
// insert a starting value and compact to trigger merges
ensure.Nil(t, db.Put(wo, givenKey, startingVal))
// trigger a compaction to ensure that a merge is performed
db.CompactRange(Range{nil, nil})
// we expect these two operands to be passed to merge multi
ensure.Nil(t, db.Merge(wo, givenKey, mergeVal1))
ensure.Nil(t, db.Merge(wo, givenKey, mergeVal2))
// trigger a compaction to ensure that a
// partial and full merge are performed
db.CompactRange(Range{nil, nil})
ro := NewDefaultReadOptions()
v1, err := db.Get(ro, givenKey)
defer v1.Free()
ensure.Nil(t, err)
ensure.DeepEqual(t, v1.Data(), fMergeResult)
}
// Mock Objects
type mockMergeOperator struct {
fullMerge func(key, existingValue []byte, operands [][]byte) ([]byte, bool)
}
func (m *mockMergeOperator) Name() string { return "gorocksdb.test" }
func (m *mockMergeOperator) FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
return m.fullMerge(key, existingValue, operands)
}
type mockMergeMultiOperator struct {
fullMerge func(key, existingValue []byte, operands [][]byte) ([]byte, bool)
partialMergeMulti func(key []byte, operands [][]byte) ([]byte, bool)
}
func (m *mockMergeMultiOperator) Name() string { return "gorocksdb.multi" }
func (m *mockMergeMultiOperator) FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
return m.fullMerge(key, existingValue, operands)
}
func (m *mockMergeMultiOperator) PartialMergeMulti(key []byte, operands [][]byte) ([]byte, bool) {
return m.partialMergeMulti(key, operands)
}
type mockMergePartialOperator struct {
fullMerge func(key, existingValue []byte, operands [][]byte) ([]byte, bool)
partialMerge func(key, leftOperand, rightOperand []byte) ([]byte, bool)
}
func (m *mockMergePartialOperator) Name() string { return "gorocksdb.partial" }
func (m *mockMergePartialOperator) FullMerge(key, existingValue []byte, operands [][]byte) ([]byte, bool) {
return m.fullMerge(key, existingValue, operands)
}
func (m *mockMergePartialOperator) PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool) {
return m.partialMerge(key, leftOperand, rightOperand)
}