-
Notifications
You must be signed in to change notification settings - Fork 1
/
nemo_string.go
170 lines (152 loc) · 3.67 KB
/
nemo_string.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
package gonemo
// #include "nemo_c.h"
// #include <stdlib.h>
import "C"
import (
"errors"
"unsafe"
)
// Del Delete all the single key in different data structure
func (nemo *NEMO) Del(key []byte) (int64, error) {
var (
cErr *C.char
count C.int64_t
)
C.nemo_Del(nemo.c, goByte2char(key), C.size_t(len(key)), &count, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(count), nil
}
// MDel Delete multi keys in different data structure
func (nemo *NEMO) MDel(keys [][]byte) (int64, error) {
var (
cErr *C.char
count C.int64_t
)
l := len(keys)
ckeylist := make([]*C.char, l)
ckeylen := make([]C.size_t, l)
for i, key := range keys {
ckeylist[i] = goBytedup2char(key)
ckeylen[i] = C.size_t(len(key))
}
C.nemo_MDel(nemo.c,
C.int(len(keys)),
(**C.char)(unsafe.Pointer(&ckeylist[0])),
(*C.size_t)(unsafe.Pointer(&ckeylen[0])),
&count, &cErr,
)
for _, key := range ckeylist {
C.free(unsafe.Pointer(key))
}
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(count), nil
}
// Expire Set expire time to a single key in different data structure
func (nemo *NEMO) Expire(key []byte, second int32) (int64, error) {
var (
cErr *C.char
res C.int64_t
)
C.nemo_Expire(nemo.c, goByte2char(key), C.size_t(len(key)), C.int32_t(second), &res, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(res), nil
}
// TTL Return ttl time of a single key
func (nemo *NEMO) TTL(key []byte) (int64, error) {
var (
cErr *C.char
count C.int64_t
)
C.nemo_TTL(nemo.c, goByte2char(key), C.size_t(len(key)), &count, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(count), nil
}
// Persist Remove the expiration from a key
func (nemo *NEMO) Persist(key []byte) (int64, error) {
var (
cErr *C.char
count C.int64_t
)
C.nemo_Persist(nemo.c, goByte2char(key), C.size_t(len(key)), &count, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(count), nil
}
// Expireat Set the expiration for a key as a UNIX timestamp
func (nemo *NEMO) Expireat(key []byte, timestamp int32) (int64, error) {
var (
cErr *C.char
res C.int64_t
)
C.nemo_Expireat(nemo.c, goByte2char(key), C.size_t(len(key)), C.int32_t(timestamp), &res, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(res), nil
}
// Type Return the key's type
func (nemo *NEMO) Type(key []byte) (*string, error) {
var (
cErr *C.char
keyType *C.char
)
C.nemo_Type(nemo.c, goByte2char(key), C.size_t(len(key)), &keyType, &cErr)
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return nil, res
}
res := C.GoString(keyType)
C.free(unsafe.Pointer(keyType))
return &res, nil
}
// Exists Return true if the key does exist
func (nemo *NEMO) Exists(keys [][]byte) (int64, error) {
var cErr *C.char
var cRes C.int64_t
l := len(keys)
ckeylist := make([]*C.char, l)
ckeylen := make([]C.size_t, l)
for i, key := range keys {
ckeylist[i] = goBytedup2char(key)
ckeylen[i] = C.size_t(len(key))
}
C.nemo_Exists(nemo.c, C.int(l),
(**C.char)(unsafe.Pointer(&ckeylist[0])),
(*C.size_t)(unsafe.Pointer(&ckeylen[0])),
&cRes,
&cErr,
)
for _, key := range ckeylist {
C.free(unsafe.Pointer(key))
}
if cErr != nil {
res := errors.New(C.GoString(cErr))
C.free(unsafe.Pointer(cErr))
return 0, res
}
return int64(cRes), nil
}
// func KMDel
// func KScan