forked from linxGnu/grocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparator.go
79 lines (64 loc) · 2.12 KB
/
comparator.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
package grocksdb
// #include "rocksdb/c.h"
import "C"
import (
"bytes"
)
// A Comparator object provides a total order across slices that are
// used as keys in an sstable or a database.
type Comparator interface {
// Three-way comparison. Returns value:
// < 0 iff "a" < "b",
// == 0 iff "a" == "b",
// > 0 iff "a" > "b"
Compare(a, b []byte) int
// The name of the comparator.
Name() string
// Return native comparator.
Native() *C.rocksdb_comparator_t
// Destroy comparator.
Destroy()
}
// NewNativeComparator creates a Comparator object.
func NewNativeComparator(c *C.rocksdb_comparator_t) Comparator {
return &nativeComparator{c}
}
type nativeComparator struct {
c *C.rocksdb_comparator_t
}
func (c *nativeComparator) Compare(a, b []byte) int { return 0 }
func (c *nativeComparator) Name() string { return "" }
func (c *nativeComparator) Native() *C.rocksdb_comparator_t {
return c.c
}
func (c *nativeComparator) Destroy() {
C.rocksdb_comparator_destroy(c.c)
c.c = nil
}
// Hold references to comperators.
var comperators = NewCOWList()
type comperatorWrapper struct {
name *C.char
comparator Comparator
}
func registerComperator(cmp Comparator) int {
return comperators.Append(comperatorWrapper{C.CString(cmp.Name()), cmp})
}
//export gorocksdb_comparator_compare
func gorocksdb_comparator_compare(idx int, cKeyA *C.char, cKeyALen C.size_t, cKeyB *C.char, cKeyBLen C.size_t) C.int {
keyA := charToByte(cKeyA, cKeyALen)
keyB := charToByte(cKeyB, cKeyBLen)
return C.int(comperators.Get(idx).(comperatorWrapper).comparator.Compare(keyA, keyB))
}
//export gorocksdb_comparator_name
func gorocksdb_comparator_name(idx int) *C.char {
return comperators.Get(idx).(comperatorWrapper).name
}
// for testing purpose only
type testBytesReverseComparator struct{}
func (cmp *testBytesReverseComparator) Name() string { return "grocksdb.bytes-reverse" }
func (cmp *testBytesReverseComparator) Compare(a, b []byte) int {
return bytes.Compare(a, b) * -1
}
func (cmp *testBytesReverseComparator) Native() *C.rocksdb_comparator_t { return nil }
func (cmp *testBytesReverseComparator) Destroy() {}