-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostfunc.redis.go
184 lines (147 loc) · 5.74 KB
/
hostfunc.redis.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
package capsule
import "github.com/valyala/fastjson"
// hostRedisSet sets the value of a redis key.
//
// keyPosition: the position of the key in memory.
// keyLength: the length of the key.
// valueStringPosition: the position of the value string in memory.
// valueStringLength: the length of the value string.
// returnValuePosition: the position of the pointer to the return value in memory.
// returnValueLength: the length of the return value.
//
// Returns the new uint32 value.
//export hostRedisSet
func hostRedisSet(
keyPosition, keyLength uint32,
valueStringPosition, valueStringLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32
// RedisSet sends a message to the host to cache a key and its value.
//
// key: the string value of the key to cache.
// value: the byte slice value to cache.
// []byte: a byte slice that contains the response from the host.
func RedisSet(key string, value []byte) ([]byte, error) {
keyPosition, keyLength := getBufferPosSize([]byte(key))
valueStringPosition, valueStringLength := getBufferPosSize(value)
// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32
// Send the lessage to the host
hostRedisSet(
keyPosition, keyLength,
valueStringPosition, valueStringLength,
&responseBufferPtr, &responseBufferSize)
bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)
// check if success or failure
data, err := Result(bufferResponseFromHost)
return data, err
}
// hostRedisGet retrieves a Redis key-value pair from the host.
//
// keyPosition: the position of the key in memory.
// keyLength: the length of the key in memory.
// returnValuePosition: a pointer to the position of the return value in memory.
// returnValueLength: a pointer to the length of the return value in memory.
//
// Returns the new function.
//export hostRedisGet
func hostRedisGet(
keyPosition, keyLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32
// RedisGet retrieves the value for the given key from Redis.
//
// It takes a single string parameter, `key`, which is used to identify the value
// to retrieve from Redis.
//
// RedisGet returns a slice of bytes containing the retrieved value, and an error
// if the retrieval failed or the key was not found.
func RedisGet(key string) ([]byte, error) {
keyPosition, keyLength := getBufferPosSize([]byte(key))
// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32
// Send the lessage to the host
hostRedisGet(
keyPosition, keyLength,
&responseBufferPtr, &responseBufferSize)
bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)
// check if success or failure
data, err := Result(bufferResponseFromHost)
if err != nil {
return nil, err // "key not found"
}
return data, nil
}
// hostRedisDel deletes the Redis key stored at the specified position and returns the length of the deleted key.
//
// keyPosition: the position of the key in Redis.
// keyLength: the length of the key.
// returnValuePosition: a pointer to the position of the return value.
// returnValueLength: a pointer to the length of the return value.
//
// Returns: the length of the deleted key.
//export hostRedisDel
func hostRedisDel(
keyPosition, keyLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32
// RedisDel deletes a Redis key and returns the result as a slice of bytes.
// The key parameter is a string representing the key to be deleted.
// The function returns a slice of bytes and an error.
func RedisDel(key string) ([]byte, error) {
keyPosition, keyLength := getBufferPosSize([]byte(key))
// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32
// Send the lessage to the host
hostRedisDel(
keyPosition, keyLength,
&responseBufferPtr, &responseBufferSize)
bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)
// check if success or failure
data, err := Result(bufferResponseFromHost)
return data, err
}
// hostRedisKeys returns a uint32 representing the new function.
//
// filterPosition: uint32 representing the filter position.
// filterLength: uint32 representing the filter length.
// returnValuePosition: pointer to uint32 representing the return value position.
// returnValueLength: pointer to uint32 representing the return value length.
//
// Returns a uint32 representing the new function.
//export hostRedisKeys
func hostRedisKeys(
filterPosition, filterLength uint32,
returnValuePosition **uint32, returnValueLength *uint32) uint32
// RedisKeys returns an array of Redis keys that match the given filter.
//
// filter: A string used to filter Redis keys.
// Returns an array of strings and an error.
func RedisKeys(filter string) ([]string, error) {
filterPosition, filterLength := getBufferPosSize([]byte(filter))
// This will be use to get the response from the host
var responseBufferPtr *uint32
var responseBufferSize uint32
// Send the lessage to the host
hostRedisKeys(
filterPosition, filterLength,
&responseBufferPtr, &responseBufferSize)
bufferResponseFromHost := readBufferFromMemory(responseBufferPtr, responseBufferSize)
//! 🤚 this is a json string (array json string: `["Hello", "World"]`)
// check if success or failure
data, err := Result(bufferResponseFromHost)
if err != nil {
return nil, err
}
var jsonParser fastjson.Parser
keysArray, err := jsonParser.Parse(string(data))
if err != nil {
return nil, err
}
var keys []string
for _, key := range keysArray.GetArray("keys") {
keys = append(keys, string(key.GetStringBytes()))
//! if it doesn't work, implement my own simple parser
}
return keys, nil
}