forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslice_transform_test.go
112 lines (87 loc) · 2.94 KB
/
slice_transform_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
package gorocksdb
import (
. "github.com/smartystreets/goconvey/convey"
"os"
"testing"
)
type testSliceTransform struct {
initiated bool
}
func (self *testSliceTransform) Transform(src []byte) []byte {
return src[0:3]
}
func (self *testSliceTransform) InDomain(src []byte) bool {
return len(src) >= 3
}
func (self *testSliceTransform) InRange(src []byte) bool {
return len(src) == 3
}
func (self *testSliceTransform) Name() string {
self.initiated = true
return "gorocksdb.test"
}
func TestCustomSliceTransform(t *testing.T) {
dbName := os.TempDir() + "/TestNewSliceTransform"
Convey("Subject: Prefix filtering with custom slice transform", t, func() {
sliceTransform := &testSliceTransform{}
options := NewDefaultOptions()
DestroyDb(dbName, options)
options.SetFilterPolicy(NewBloomFilter(10))
options.SetPrefixExtractor(sliceTransform)
options.SetHashSkipListRep(50000, 4, 4)
options.SetPlainTableFactory(4, 10, 0.75, 16)
options.SetCreateIfMissing(true)
db, err := OpenDb(options, dbName)
defer db.Close()
So(err, ShouldBeNil)
wo := NewDefaultWriteOptions()
So(db.Put(wo, []byte("foo1"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("foo2"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("foo3"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("bar1"), []byte("bar")), ShouldBeNil)
So(db.Put(wo, []byte("bar2"), []byte("bar")), ShouldBeNil)
So(db.Put(wo, []byte("bar3"), []byte("bar")), ShouldBeNil)
ro := NewDefaultReadOptions()
it := db.NewIterator(ro)
defer it.Close()
numFound := 0
for it.Seek([]byte("bar")); it.Valid(); it.Next() {
numFound++
}
So(it.Err(), ShouldBeNil)
So(numFound, ShouldEqual, 3)
})
}
func TestFixedPrefixTransform(t *testing.T) {
dbName := os.TempDir() + "/TestNewFixedPrefixTransform"
Convey("Subject: Prefix filtering with end condition checking", t, func() {
options := NewDefaultOptions()
DestroyDb(dbName, options)
options.SetFilterPolicy(NewBloomFilter(10))
options.SetHashSkipListRep(50000, 4, 4)
options.SetPlainTableFactory(4, 10, 0.75, 16)
options.SetCreateIfMissing(true)
db, err := OpenDb(options, dbName)
defer db.Close()
So(err, ShouldBeNil)
wo := NewDefaultWriteOptions()
So(db.Put(wo, []byte("foo1"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("foo2"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("foo3"), []byte("foo")), ShouldBeNil)
So(db.Put(wo, []byte("bar1"), []byte("bar")), ShouldBeNil)
So(db.Put(wo, []byte("bar2"), []byte("bar")), ShouldBeNil)
So(db.Put(wo, []byte("bar3"), []byte("bar")), ShouldBeNil)
ro := NewDefaultReadOptions()
it := db.NewIterator(ro)
defer it.Close()
numFound := 0
prefix := []byte("bar")
// Iterators must now be checked for passing the end condition
// See https://github.com/facebook/rocksdb/wiki/Prefix-Seek-API-Changes
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
numFound++
}
So(it.Err(), ShouldBeNil)
So(numFound, ShouldEqual, 3)
})
}