-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalkey_glide_script_commands.c
More file actions
387 lines (322 loc) · 13.6 KB
/
valkey_glide_script_commands.c
File metadata and controls
387 lines (322 loc) · 13.6 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "command_response.h"
#include "common.h"
#include "include/glide_bindings.h"
#include "valkey_glide_commands_common.h"
#include "valkey_glide_core_common.h"
// Helper macros for validating CommandResult in script commands
#define VALIDATE_SCRIPT_RESULT_OR_RETURN_FALSE(result, return_value) \
do { \
if (!(result) || (result)->command_error || !(result)->response) { \
if (result) { \
free_command_result(result); \
} \
RETURN_FALSE; \
} \
} while (0)
#define VALIDATE_SCRIPT_RESULT_OR_RETURN_NULL(result, return_value) \
do { \
if (!(result) || (result)->command_error || !(result)->response) { \
if (result) { \
free_command_result(result); \
} \
RETURN_NULL(); \
} \
} while (0)
#define VALIDATE_SCRIPT_RESULT_NO_RESPONSE_OR_RETURN_FALSE(result, return_value) \
do { \
if (!(result) || (result)->command_error) { \
if (result) { \
free_command_result(result); \
} \
RETURN_FALSE; \
} \
} while (0)
/**
* Helper function to handle CommandResult for boolean script commands (void return)
*/
static void handle_script_bool_result(CommandResult* result, zval* return_value) {
if (!result || result->command_error || !result->response) {
ZVAL_FALSE(return_value);
if (result) {
free_command_result(result);
}
return;
}
process_core_bool_result(result->response, NULL, return_value);
free_command_result(result);
}
#include "zend_exceptions.h"
// Helper to convert string array to FFI format
static void prepare_ffi_args(zval* array,
uintptr_t** ptrs,
unsigned long** lens,
unsigned long* count) {
if (Z_TYPE_P(array) != IS_ARRAY) {
*count = 0;
*ptrs = NULL;
*lens = NULL;
return;
}
*count = zend_hash_num_elements(Z_ARRVAL_P(array));
if (*count == 0) {
*ptrs = NULL;
*lens = NULL;
return;
}
*ptrs = emalloc(sizeof(uintptr_t) * (*count));
*lens = emalloc(sizeof(unsigned long) * (*count));
zval* entry;
int i = 0;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array), entry) {
convert_to_string(entry);
(*ptrs)[i] = (uintptr_t) Z_STRVAL_P(entry);
(*lens)[i] = Z_STRLEN_P(entry);
i++;
}
ZEND_HASH_FOREACH_END();
}
// Script execution using invoke_script FFI
// Helper to store script and get hash
char* store_script_and_get_hash(const char* script) {
struct ScriptHashBuffer* hash_buffer = store_script((const uint8_t*) script, strlen(script));
if (!hash_buffer || !hash_buffer->ptr) {
if (hash_buffer) {
free_script_hash_buffer(hash_buffer);
}
return NULL;
}
char* hash = estrndup((const char*) hash_buffer->ptr, hash_buffer->len);
free_script_hash_buffer(hash_buffer);
return hash;
}
// Helper function for script flush command
void execute_script_flush_command(zval* object, zval* return_value, bool is_cluster) {
valkey_glide_object* valkey_glide =
VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide->glide_client) {
ZVAL_FALSE(return_value);
return;
}
CommandResult* result = execute_command(valkey_glide->glide_client, ScriptFlush, 0, NULL, NULL);
handle_script_bool_result(result, return_value);
}
// Helper to build and execute eval-style commands
static void execute_eval_style_command(const char* cmd_name,
size_t cmd_len,
char* script_or_sha,
size_t script_len,
zval* keys_array,
zval* args_array,
zend_long num_keys,
bool num_keys_set,
zval* object,
zval* return_value) {
valkey_glide_object* valkey_glide =
VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide->glide_client) {
RETURN_FALSE;
}
int keys_count = keys_array ? zend_hash_num_elements(Z_ARRVAL_P(keys_array)) : 0;
int args_count = args_array ? zend_hash_num_elements(Z_ARRVAL_P(args_array)) : 0;
if (!num_keys_set) {
num_keys = keys_count;
}
int cmd_count = 3 + keys_count + args_count; // cmd + script + numkeys + keys + args
uintptr_t* cmd_args = emalloc(sizeof(uintptr_t) * cmd_count);
unsigned long* cmd_args_len = emalloc(sizeof(unsigned long) * cmd_count);
int idx = 0;
cmd_args[idx] = (uintptr_t) cmd_name;
cmd_args_len[idx++] = cmd_len;
cmd_args[idx] = (uintptr_t) script_or_sha;
cmd_args_len[idx++] = script_len;
char numkeys_str[32];
snprintf(numkeys_str, sizeof(numkeys_str), "%lld", (long long) num_keys);
cmd_args[idx] = (uintptr_t) numkeys_str;
cmd_args_len[idx++] = strlen(numkeys_str);
if (keys_array) {
zval* entry;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys_array), entry) {
convert_to_string(entry);
cmd_args[idx] = (uintptr_t) Z_STRVAL_P(entry);
cmd_args_len[idx++] = Z_STRLEN_P(entry);
}
ZEND_HASH_FOREACH_END();
}
if (args_array) {
zval* entry;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args_array), entry) {
convert_to_string(entry);
cmd_args[idx] = (uintptr_t) Z_STRVAL_P(entry);
cmd_args_len[idx++] = Z_STRLEN_P(entry);
}
ZEND_HASH_FOREACH_END();
}
CommandResult* result = execute_command(
valkey_glide->glide_client, CustomCommand, cmd_count, cmd_args, cmd_args_len);
efree(cmd_args);
efree(cmd_args_len);
VALIDATE_SCRIPT_RESULT_NO_RESPONSE_OR_RETURN_FALSE(result, return_value);
command_response_to_zval(result->response, return_value, 0, false);
free_command_result(result);
}
// Helper to split PHPRedis-style combined args array into keys and args
static void split_args_array(zval* args_array, zend_long num_keys, zval* keys_out, zval* args_out) {
if (!args_array || num_keys == 0) {
return;
}
int total_args = zend_hash_num_elements(Z_ARRVAL_P(args_array));
// Initialize output arrays
if (num_keys > 0) {
array_init_size(keys_out, num_keys);
}
if (total_args > num_keys) {
array_init_size(args_out, total_args - num_keys);
}
int idx = 0;
zval* entry;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args_array), entry) {
if (idx < num_keys) {
Z_TRY_ADDREF_P(entry);
add_next_index_zval(keys_out, entry);
} else {
Z_TRY_ADDREF_P(entry);
add_next_index_zval(args_out, entry);
}
idx++;
}
ZEND_HASH_FOREACH_END();
}
// Generic implementation for all eval-style commands
static void execute_eval_generic(const char* command_name,
size_t command_name_len,
zval* object,
int argc,
zval* return_value,
bool is_cluster) {
char* first_param;
size_t first_param_len;
zval* args_array = NULL;
zend_long num_keys = 0;
if (argc < 1 || argc > 3) {
RETURN_FALSE;
}
zval* params = emalloc(sizeof(zval) * argc);
if (zend_get_parameters_array_ex(argc, params) == FAILURE) {
efree(params);
RETURN_FALSE;
}
if (Z_TYPE(params[0]) != IS_STRING) {
efree(params);
RETURN_FALSE;
}
first_param = Z_STRVAL(params[0]);
first_param_len = Z_STRLEN(params[0]);
if (argc >= 2 && Z_TYPE(params[1]) == IS_ARRAY) {
args_array = ¶ms[1];
}
if (argc >= 3 && Z_TYPE(params[2]) == IS_LONG) {
num_keys = Z_LVAL(params[2]);
}
// Split args_array into keys and args based on num_keys (PHPRedis compatibility)
zval keys_zval, args_zval;
zval* keys_array = NULL;
zval* argv_array = NULL;
if (args_array && num_keys > 0) {
ZVAL_UNDEF(&keys_zval);
ZVAL_UNDEF(&args_zval);
split_args_array(args_array, num_keys, &keys_zval, &args_zval);
keys_array = &keys_zval;
if (Z_TYPE(args_zval) == IS_ARRAY) {
argv_array = &args_zval;
}
} else if (args_array) {
argv_array = args_array;
}
execute_eval_style_command(command_name,
command_name_len,
first_param,
first_param_len,
keys_array,
argv_array,
num_keys,
true,
object,
return_value);
if (keys_array && Z_TYPE_P(keys_array) == IS_ARRAY) {
zval_ptr_dtor(keys_array);
}
if (argv_array && argv_array != args_array && Z_TYPE_P(argv_array) == IS_ARRAY) {
zval_ptr_dtor(argv_array);
}
efree(params);
}
void execute_eval_command(zval* object, int argc, zval* return_value, bool is_cluster) {
execute_eval_generic("EVAL", strlen("EVAL"), object, argc, return_value, is_cluster);
}
void execute_evalsha_command(zval* object, int argc, zval* return_value, bool is_cluster) {
execute_eval_generic("EVALSHA", strlen("EVALSHA"), object, argc, return_value, is_cluster);
}
void execute_eval_ro_command(zval* object, int argc, zval* return_value, bool is_cluster) {
execute_eval_generic("EVAL_RO", strlen("EVAL_RO"), object, argc, return_value, is_cluster);
}
void execute_evalsha_ro_command(zval* object, int argc, zval* return_value, bool is_cluster) {
execute_eval_generic(
"EVALSHA_RO", strlen("EVALSHA_RO"), object, argc, return_value, is_cluster);
}
void execute_script_exists_command(zval* object, zval* sha1s, zval* return_value, bool is_cluster) {
if (Z_TYPE_P(sha1s) != IS_ARRAY) {
RETURN_FALSE;
}
valkey_glide_object* valkey_glide =
VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide->glide_client) {
RETURN_FALSE;
}
int count = zend_hash_num_elements(Z_ARRVAL_P(sha1s));
uintptr_t* cmd_args = emalloc(sizeof(uintptr_t) * count);
unsigned long* cmd_args_len = emalloc(sizeof(unsigned long) * count);
int idx = 0;
zval* entry;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(sha1s), entry) {
convert_to_string(entry);
cmd_args[idx] = (uintptr_t) Z_STRVAL_P(entry);
cmd_args_len[idx] = Z_STRLEN_P(entry);
idx++;
}
ZEND_HASH_FOREACH_END();
CommandResult* result =
execute_command(valkey_glide->glide_client, ScriptExists, count, cmd_args, cmd_args_len);
efree(cmd_args);
efree(cmd_args_len);
VALIDATE_SCRIPT_RESULT_OR_RETURN_FALSE(result, return_value);
command_response_to_zval(result->response, return_value, 0, false);
free_command_result(result);
}
// Helper function for script show command
void execute_script_show_command(
zval* object, char* sha1, size_t sha1_len, zval* return_value, bool is_cluster) {
valkey_glide_object* valkey_glide =
VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide->glide_client) {
RETURN_NULL();
}
uintptr_t cmd_args[1] = {(uintptr_t) sha1};
unsigned long cmd_args_len[1] = {sha1_len};
CommandResult* result =
execute_command(valkey_glide->glide_client, ScriptShow, 1, cmd_args, cmd_args_len);
VALIDATE_SCRIPT_RESULT_OR_RETURN_NULL(result, return_value);
command_response_to_zval(result->response, return_value, 0, false);
free_command_result(result);
}
// Helper function for script kill command
void execute_script_kill_command(zval* object, zval* return_value, bool is_cluster) {
valkey_glide_object* valkey_glide =
VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide->glide_client) {
RETURN_FALSE;
}
CommandResult* result = execute_command(valkey_glide->glide_client, ScriptKill, 0, NULL, NULL);
VALIDATE_SCRIPT_RESULT_NO_RESPONSE_OR_RETURN_FALSE(result, return_value);
command_response_to_zval(result->response, return_value, 0, false);
free_command_result(result);
}