forked from tecbot/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparator.go
49 lines (39 loc) · 1.3 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
package gorocksdb
// #include "rocksdb/c.h"
import "C"
// 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
}
// 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 "" }
// Hold references to comperators.
var comperators []Comparator
func registerComperator(cmp Comparator) int {
comperators = append(comperators, cmp)
return len(comperators) - 1
}
//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[idx].Compare(keyA, keyB))
}
//export gorocksdb_comparator_name
func gorocksdb_comparator_name(idx int) *C.char {
return stringToChar(comperators[idx].Name())
}