-
Notifications
You must be signed in to change notification settings - Fork 1
/
nemo_list.go
281 lines (255 loc) · 6.61 KB
/
nemo_list.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
)
// LIndex Return the val pointed by the index of a list
func (nemo *NEMO) LIndex(key []byte, index int64) ([]byte, error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
C.nemo_LIndex(nemo.c, goByte2char(key), C.size_t(len(key)), C.int64_t(index), &cVal, &cLen, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return nil, res
}
val := C.GoBytes(unsafe.Pointer(cVal), C.int(cLen))
C.free(unsafe.Pointer(cVal))
return val, nil
}
// LLen Return list length
func (nemo *NEMO) LLen(key []byte) (int64, error) {
var cLen C.int64_t
var cErr *C.char
C.nemo_LLen(nemo.c, goByte2char(key), C.size_t(len(key)), &cLen, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cLen), nil
}
// LPush Push a val into a list
func (nemo *NEMO) LPush(key []byte, value []byte) (int64, error) {
var cErr *C.char
var listLen C.int64_t
C.nemo_LPush(nemo.c,
goByte2char(key), C.size_t(len(key)),
goByte2char(value), C.size_t(len(value)),
&listLen, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(listLen), nil
}
// LPop Pop a val of a list
func (nemo *NEMO) LPop(key []byte) ([]byte, error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
C.nemo_LPop(nemo.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, res
}
val := C.GoBytes(unsafe.Pointer(cVal), C.int(cLen))
C.free(unsafe.Pointer(cVal))
return val, nil
}
// LPushx Push a value into a list if the list exists
func (nemo *NEMO) LPushx(key []byte, value []byte) (int64, error) {
var cErr *C.char
var listLen C.int64_t
C.nemo_LPushx(nemo.c,
goByte2char(key), C.size_t(len(key)),
goByte2char(value), C.size_t(len(value)),
&listLen, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(listLen), nil
}
// LRange Return vals between begin and end
func (nemo *NEMO) LRange(key []byte, begin int64, end int64) (index []int64, values [][]byte, err error) {
var n C.size_t
var IndexList *C.int64_t
var vallist **C.char
var vallistlen *C.size_t
var cErr *C.char
var cRes C.int64_t
C.nemo_LRange(nemo.c,
goByte2char(key), C.size_t(len(key)),
C.int64_t(begin), C.int64_t(end),
&n,
&IndexList, &vallist, &vallistlen,
&cRes,
&cErr,
)
if cErr != nil {
err = errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return nil, nil, err
}
if cRes == 0 || n == 0 {
return nil, nil, nil
}
cIndex := cInt64s2Slice(IndexList, int(n))
index = make([]int64, int(n))
for i := range index {
index[i] = int64(cIndex[i])
}
C.free(unsafe.Pointer(IndexList))
return index, cstr2GoMultiByte(int(n), vallist, vallistlen), nil
}
// LSet Set val of given index
func (nemo *NEMO) LSet(key []byte, index int64, value []byte) error {
var cErr *C.char
C.nemo_LSet(nemo.c,
goByte2char(key), C.size_t(len(key)),
C.int64_t(index),
goByte2char(value), C.size_t(len(value)),
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// LTrim Delete list element between begin and end index
func (nemo *NEMO) LTrim(key []byte, begin int64, end int64) error {
var cErr *C.char
C.nemo_LTrim(nemo.c,
goByte2char(key), C.size_t(len(key)),
C.int64_t(begin), C.int64_t(end),
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return res
}
return nil
}
// RPush Push element into list in the tail
func (nemo *NEMO) RPush(key []byte, value []byte) (int64, error) {
var cErr *C.char
var listLen C.int64_t
C.nemo_RPush(nemo.c,
goByte2char(key), C.size_t(len(key)),
goByte2char(value), C.size_t(len(value)),
&listLen, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(listLen), nil
}
// RPop Pop a element of the list in the tail
func (nemo *NEMO) RPop(key []byte) ([]byte, error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
C.nemo_RPop(nemo.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, res
}
val := C.GoBytes(unsafe.Pointer(cVal), C.int(cLen))
C.free(unsafe.Pointer(cVal))
return val, nil
}
// RPushx Push a element in the tail if the list exists
func (nemo *NEMO) RPushx(key []byte, value []byte) (int64, error) {
var cErr *C.char
var listLen C.int64_t
C.nemo_RPushx(nemo.c,
goByte2char(key), C.size_t(len(key)),
goByte2char(value), C.size_t(len(value)),
&listLen, &cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(listLen), nil
}
// RPopLPush Pop a element in the src list tail and pushes it into dest list
// If src list is null or does not exist, return false
func (nemo *NEMO) RPopLPush(srckey []byte, destkey []byte) (exist bool, value []byte, err error) {
var cVal *C.char
var cLen C.size_t
var cErr *C.char
var cRes C.int64_t
C.nemo_RPopLPush(nemo.c,
goByte2char(srckey), C.size_t(len(srckey)),
goByte2char(destkey), C.size_t(len(destkey)),
&cVal, &cLen,
&cRes,
&cErr,
)
if cErr != nil {
err = errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return false, nil, err
}
if cRes == 0 {
return false, nil, nil
}
value = C.GoBytes(unsafe.Pointer(cVal), C.int(cLen))
C.free(unsafe.Pointer(cVal))
return true, value, nil
}
// LInsert Insert element into list with a pivot and position after pivot
func (nemo *NEMO) LInsert(key []byte, pos int, pivot []byte, value []byte) (int64, error) {
var cErr *C.char
var llen C.int64_t
C.nemo_LInsert(nemo.c,
goByte2char(key), C.size_t(len(key)),
C.int(pos),
goByte2char(pivot), C.size_t(len(pivot)),
goByte2char(value), C.size_t(len(value)),
&llen,
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(llen), nil
}
// LRem Remove element of a list
func (nemo *NEMO) LRem(key []byte, count int64, value []byte) (int64, error) {
var cErr *C.char
var resCount C.int64_t
C.nemo_LRem(nemo.c,
goByte2char(key), C.size_t(len(key)),
C.int64_t(count),
goByte2char(value), C.size_t(len(value)),
&resCount,
&cErr,
)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(resCount), nil
}