forked from cosmos/gorocksdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compaction_filter.go
73 lines (61 loc) · 2.53 KB
/
compaction_filter.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 CompactionFilter can be used to filter keys during compaction time.
type CompactionFilter interface {
// If the Filter function returns false, it indicates
// that the kv should be preserved, while a return value of true
// indicates that this key-value should be removed from the
// output of the compaction. The application can inspect
// the existing value of the key and make decision based on it.
//
// When the value is to be preserved, the application has the option
// to modify the existing value and pass it back through a new value.
// To retain the previous value, simply return nil
//
// If multithreaded compaction is being used *and* a single CompactionFilter
// instance was supplied via SetCompactionFilter, this the Filter function may be
// called from different threads concurrently. The application must ensure
// that the call is thread-safe.
Filter(level int, key, val []byte) (remove bool, newVal []byte)
// The name of the compaction filter, for logging
Name() string
}
// NewNativeCompactionFilter creates a CompactionFilter object.
func NewNativeCompactionFilter(c *C.rocksdb_compactionfilter_t) CompactionFilter {
return nativeCompactionFilter{c}
}
type nativeCompactionFilter struct {
c *C.rocksdb_compactionfilter_t
}
func (c nativeCompactionFilter) Filter(level int, key, val []byte) (remove bool, newVal []byte) {
return false, nil
}
func (c nativeCompactionFilter) Name() string { return "" }
// Hold references to compaction filters.
var compactionFilters = NewCOWList()
type compactionFilterWrapper struct {
name *C.char
filter CompactionFilter
}
func registerCompactionFilter(filter CompactionFilter) int {
return compactionFilters.Append(compactionFilterWrapper{C.CString(filter.Name()), filter})
}
//export gorocksdb_compactionfilter_filter
func gorocksdb_compactionfilter_filter(idx int, cLevel C.int, cKey *C.char, cKeyLen C.size_t, cVal *C.char, cValLen C.size_t, cNewVal **C.char, cNewValLen *C.size_t, cValChanged *C.uchar) C.int {
key := charToByte(cKey, cKeyLen)
val := charToByte(cVal, cValLen)
remove, newVal := compactionFilters.Get(idx).(compactionFilterWrapper).filter.Filter(int(cLevel), key, val)
if remove {
return C.int(1)
} else if newVal != nil {
*cNewVal = byteToChar(newVal)
*cNewValLen = C.size_t(len(newVal))
*cValChanged = C.uchar(1)
}
return C.int(0)
}
//export gorocksdb_compactionfilter_name
func gorocksdb_compactionfilter_name(idx int) *C.char {
return compactionFilters.Get(idx).(compactionFilterWrapper).name
}