-
Notifications
You must be signed in to change notification settings - Fork 1
/
nemo_bitmap.go
137 lines (119 loc) · 2.86 KB
/
nemo_bitmap.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
)
// BitSet Set the bit of bitmap
func (nemo *NEMO) BitSet(key []byte, offset int64, on int64) (int64, error) {
var cRes C.int64_t
var cErr *C.char
C.nemo_BitSet(
nemo.c, goByte2char(key), C.size_t(len(key)),
C.int64_t(offset), C.int64_t(on),
&cRes, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
// BitGet Get the bit of bitmap
func (nemo *NEMO) BitGet(key []byte, offset int64, on int64) (int64, error) {
var cRes C.int64_t
var cErr *C.char
C.nemo_BitSet(
nemo.c, goByte2char(key), C.size_t(len(key)),
C.int64_t(offset), C.int64_t(on),
&cRes, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
// BitCount Return the count of bit map
func (nemo *NEMO) BitCount(key []byte) (int64, error) {
var cRes C.int64_t
var cErr *C.char
C.nemo_BitCount(
nemo.c, goByte2char(key), C.size_t(len(key)),
&cRes, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
// BitCountRange Return the count of bit map over range
func (nemo *NEMO) BitCountRange(key []byte, start int64, end int64) (int64, error) {
var cRes C.int64_t
var cErr *C.char
C.nemo_BitCountRange(
nemo.c, goByte2char(key), C.size_t(len(key)),
C.int64_t(start), C.int64_t(end),
&cRes, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
// BitPos Return the bit position
func (nemo *NEMO) BitPos(key []byte, bit int64) (int64, error) {
var cRes C.int64_t
var cErr *C.char
C.nemo_BitPos(
nemo.c, goByte2char(key), C.size_t(len(key)),
C.int64_t(bit),
&cRes, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
//nemo_BitPosWithStart
//nemo_BitPosWithStartEnd
// BitOp Do bit opertation on bitmap
func (nemo *NEMO) BitOp(opType BitOpType, dest []byte, srckeys [][]byte) (int64, error) {
var cErr *C.char
var resLen C.int64_t
l := len(srckeys)
csrckeylist := make([]*C.char, l)
csrckeylen := make([]C.size_t, l)
for i, srckey := range srckeys {
csrckeylist[i] = goBytedup2char(srckey)
csrckeylen[i] = C.size_t(len(srckey))
}
C.nemo_BitOp(nemo.c, C.int(opType),
goByte2char(dest),
C.size_t(len(dest)),
C.int(l),
(**C.char)(unsafe.Pointer(&csrckeylist[0])),
(*C.size_t)(unsafe.Pointer(&csrckeylen[0])),
&resLen,
&cErr,
)
for _, srckey := range csrckeylist {
C.free(unsafe.Pointer(srckey))
}
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(resLen), nil
}