-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbhandle.go
121 lines (108 loc) · 3.05 KB
/
dbhandle.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"reflect"
"unsafe"
)
// DBNemo rocksdb instance handle
type DBNemo struct {
c *C.nemo_DBNemo_t
}
// GetMetaHandle Return db handle of meta data
func (nemo *NEMO) GetMetaHandle() *DBNemo {
var hd DBNemo
hd.c = C.nemo_GetMetaHandle(nemo.c)
return &hd
}
// GetRaftHandle Return db handle of raft log
func (nemo *NEMO) GetRaftHandle() *DBNemo {
var hd DBNemo
hd.c = C.nemo_GetRaftHandle(nemo.c)
return &hd
}
// GetRaftHandle Return db handle of raft log
func (nemo *NEMO) GetKvHandle() *DBNemo {
var hd DBNemo
hd.c = C.nemo_GetKvHandle(nemo.c)
return &hd
}
// BatchWrite A batch write api for meta data and raft log rocksdb instance
func (nemo *NEMO) BatchWrite(db *DBNemo, wb *WriteBatch, sync bool) error {
var cErr *C.char
C.rocksdb_BatchWrite(nemo.c, db.c, wb.c, C.bool(sync), &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// PutWithHandle Put a key value pair with a db handle
func (nemo *NEMO) PutWithHandle(db *DBNemo, key []byte, value []byte, sync bool) error {
var cErr *C.char
C.nemo_PutWithHandle(nemo.c, db.c, goByte2char(key), C.size_t(len(key)),
goByte2char(value), C.size_t(len(value)),
C.bool(sync),
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// GetWithHandle Get a key value pair with a db handle
func (nemo *NEMO) GetWithHandle(db *DBNemo, key []byte) ([]byte, error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
cCppStr := C.nemo_GetWithHandle(nemo.c, db.c, goByte2char(key), C.size_t(len(key)),
&cVal, &cLen,
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
C.nemo_delCppStr(cCppStr)
return nil, res
}
val := C.GoBytes(unsafe.Pointer(cVal), C.int(cLen))
C.nemo_delCppStr(cCppStr)
return val, nil
}
// GetWithHandleUnSafe Get a key value pair with a db handle
// The second return value is a cpp pointer points to cpp string object.
// Must use func 'FreeCppStr' to free the cpp string object if you don't use the value slice
func (nemo *NEMO) GetWithHandleUnSafe(db *DBNemo, key []byte) ([]byte, unsafe.Pointer, error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
cCppStr := C.nemo_GetWithHandle(nemo.c, db.c, goByte2char(key), C.size_t(len(key)),
&cVal, &cLen,
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return nil, nil, res
}
var v []byte
sH := (*reflect.SliceHeader)(unsafe.Pointer(&v))
sH.Cap, sH.Len, sH.Data = int(cLen), int(cLen), uintptr(unsafe.Pointer(cVal))
return v, cCppStr, nil
}
// DeleteWithHandle Delete a key value pair with a db handle
func (nemo *NEMO) DeleteWithHandle(db *DBNemo, key []byte, sync bool) error {
var cErr *C.char
C.nemo_DeleteWithHandle(nemo.c, db.c, goByte2char(key), C.size_t(len(key)), C.bool(sync), &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}