forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice_transform.go
73 lines (58 loc) · 2.33 KB
/
slice_transform.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
package gorocksdb
// #include "rocksdb/c.h"
import "C"
// A SliceTransform can be used as a prefix extractor.
type SliceTransform interface {
// Transform a src in domain to a dst in the range.
Transform(src []byte) []byte
// Determine whether this is a valid src upon the function applies.
InDomain(src []byte) bool
// Determine whether dst=Transform(src) for some src.
InRange(src []byte) bool
// Return the name of this transformation.
Name() string
}
// NewFixedPrefixTransform creates a new fixed prefix transform.
func NewFixedPrefixTransform(prefixLen int) SliceTransform {
return NewNativeSliceTransform(C.rocksdb_slicetransform_create_fixed_prefix(C.size_t(prefixLen)))
}
// NewNativeSliceTransform creates a SliceTransform object.
func NewNativeSliceTransform(c *C.rocksdb_slicetransform_t) SliceTransform {
return nativeSliceTransform{c}
}
type nativeSliceTransform struct {
c *C.rocksdb_slicetransform_t
}
func (st nativeSliceTransform) Transform(src []byte) []byte { return nil }
func (st nativeSliceTransform) InDomain(src []byte) bool { return false }
func (st nativeSliceTransform) InRange(src []byte) bool { return false }
func (st nativeSliceTransform) Name() string { return "" }
// Hold references to slice transforms.
var sliceTransforms []SliceTransform
func registerSliceTransform(st SliceTransform) int {
sliceTransforms = append(sliceTransforms, st)
return len(sliceTransforms) - 1
}
//export gorocksdb_slicetransform_transform
func gorocksdb_slicetransform_transform(idx int, cKey *C.char, cKeyLen C.size_t, cDstLen *C.size_t) *C.char {
key := charToByte(cKey, cKeyLen)
dst := sliceTransforms[idx].Transform(key)
*cDstLen = C.size_t(len(dst))
return cByteSlice(dst)
}
//export gorocksdb_slicetransform_in_domain
func gorocksdb_slicetransform_in_domain(idx int, cKey *C.char, cKeyLen C.size_t) C.uchar {
key := charToByte(cKey, cKeyLen)
inDomain := sliceTransforms[idx].InDomain(key)
return boolToChar(inDomain)
}
//export gorocksdb_slicetransform_in_range
func gorocksdb_slicetransform_in_range(idx int, cKey *C.char, cKeyLen C.size_t) C.uchar {
key := charToByte(cKey, cKeyLen)
inRange := sliceTransforms[idx].InRange(key)
return boolToChar(inRange)
}
//export gorocksdb_slicetransform_name
func gorocksdb_slicetransform_name(idx int) *C.char {
return stringToChar(sliceTransforms[idx].Name())
}