-
Notifications
You must be signed in to change notification settings - Fork 65
/
slice.go
89 lines (73 loc) · 1.84 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
85
86
87
88
89
package grocksdb
// #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
}
// Slices is collection of Slice.
type Slices []*Slice
// Destroy free slices.
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}
}
// Exists returns if underlying data exists.
func (s *Slice) Exists() bool {
return s.data != nil
}
// Data returns the data of the slice. If the key doesn't exist this will be a
// nil slice.
func (s *Slice) Data() []byte {
if s.Exists() {
return refCBytes(s.data, s.size)
}
return nil
}
// Size returns the size of the data.
func (s *Slice) Size() int {
return int(s.size)
}
// Free frees the slice data.
func (s *Slice) Free() {
if !s.freed {
C.rocksdb_free(unsafe.Pointer(s.data))
s.data = nil
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: c}
}
// Exists returns if underlying data exists.
func (h *PinnableSliceHandle) Exists() bool {
return h.c != nil
}
// Data returns the data of the slice.
func (h *PinnableSliceHandle) Data() []byte {
if h.Exists() {
var cValLen C.size_t
cValue := C.rocksdb_pinnableslice_value(h.c, &cValLen)
return refCBytes(cValue, cValLen)
}
return nil
}
// Destroy calls the destructor of the underlying pinnable slice handle.
func (h *PinnableSliceHandle) Destroy() {
C.rocksdb_pinnableslice_destroy(h.c)
h.c = nil
}