forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge_operator_test.go
54 lines (46 loc) · 1.6 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
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)
}
type mockMergeOperator struct {
fullMerge func(key, existingValue []byte, operands [][]byte) ([]byte, bool)
partialMerge func(key, leftOperand, rightOperand []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)
}
func (m *mockMergeOperator) PartialMerge(key, leftOperand, rightOperand []byte) ([]byte, bool) {
return m.partialMerge(key, leftOperand, rightOperand)
}