-
Notifications
You must be signed in to change notification settings - Fork 271
/
slice.go
84 lines (68 loc) · 1.85 KB
/
slice.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
package gorocksdb
// #include <stdlib.h>
// #include "rocksdb/c.h"
import "C"
import "unsafe"
// Slice is used as a wrapper for non-copy values
type Slice struct {
data *C.char
size C.size_t
freed bool
}
type Slices []*Slice
func (slices Slices) Destroy() {
for _, s := range slices {
s.Free()
}
}
// NewSlice returns a slice with the given data.
func NewSlice(data *C.char, size C.size_t) *Slice {
return &Slice{data, size, false}
}
// StringToSlice is similar to NewSlice, but can be called with
// a Go string type. This exists to make testing integration
// with Gorocksdb easier.
func StringToSlice(data string) *Slice {
return NewSlice(C.CString(data), C.size_t(len(data)))
}
// Data returns the data of the slice. If the key doesn't exist this will be a
// nil slice.
func (s *Slice) Data() []byte {
return charToByte(s.data, s.size)
}
// Size returns the size of the data.
func (s *Slice) Size() int {
return int(s.size)
}
// Exists returns if the key exists
func (s *Slice) Exists() bool {
return s.data != nil
}
// Free frees the slice data.
func (s *Slice) Free() {
if !s.freed {
C.rocksdb_free(unsafe.Pointer(s.data))
s.freed = true
}
}
// PinnableSliceHandle represents a handle to a PinnableSlice.
type PinnableSliceHandle struct {
c *C.rocksdb_pinnableslice_t
}
// NewNativePinnableSliceHandle creates a PinnableSliceHandle object.
func NewNativePinnableSliceHandle(c *C.rocksdb_pinnableslice_t) *PinnableSliceHandle {
return &PinnableSliceHandle{c}
}
// Data returns the data of the slice.
func (h *PinnableSliceHandle) Data() []byte {
if h.c == nil {
return nil
}
var cValLen C.size_t
cValue := C.rocksdb_pinnableslice_value(h.c, &cValLen)
return charToByte(cValue, cValLen)
}
// Destroy calls the destructor of the underlying pinnable slice handle.
func (h *PinnableSliceHandle) Destroy() {
C.rocksdb_pinnableslice_destroy(h.c)
}