-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalkey_glide_str_commands.c
More file actions
578 lines (493 loc) · 20.8 KB
/
valkey_glide_str_commands.c
File metadata and controls
578 lines (493 loc) · 20.8 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
| Copyright (c) 2023-2025 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "command_response.h"
#include "include/glide_bindings.h"
#include "valkey_glide_commands_common.h"
#include "valkey_glide_core_common.h"
#include "valkey_glide_z_common.h"
/* Import the string conversion functions from command_response.c */
extern char* long_to_string(long value, size_t* len);
extern char* double_to_string(double value, size_t* len);
/* Execute a TYPE command using the Valkey Glide client - MIGRATED TO CORE FRAMEWORK */
int execute_type_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len = 0;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "Os", &object, ce, &key, &key_len) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
/* If we have a Glide client, use it */
if (valkey_glide->glide_client) {
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = Type;
args.key = key;
args.key_len = key_len;
if (execute_core_command(
valkey_glide, &args, NULL, process_core_type_result, return_value)) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
return 1;
}
return 1;
}
}
return 0;
}
/* Execute an APPEND command using the Valkey Glide client - MIGRATED TO CORE FRAMEWORK */
int execute_append_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *value = NULL;
size_t key_len = 0, value_len = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Oss", &object, ce, &key, &key_len, &value, &value_len) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
/* If we have a Glide client, use it */
if (valkey_glide->glide_client) {
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = Append;
args.key = key;
args.key_len = key_len;
/* Add value argument */
args.args[0].type = CORE_ARG_TYPE_STRING;
args.args[0].data.string_arg.value = value;
args.args[0].data.string_arg.len = value_len;
args.arg_count = 1;
if (execute_core_command(
valkey_glide, &args, NULL, process_core_int_result, return_value)) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
return 1;
}
return 1;
}
}
return 0;
}
/* Execute a GETRANGE command using the Valkey Glide client - MIGRATED TO CORE FRAMEWORK */
int execute_getrange_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *result = NULL;
size_t key_len = 0, result_len = 0;
long start = 0, end = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osll", &object, ce, &key, &key_len, &start, &end) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
/* If we have a Glide client, use it */
if (valkey_glide->glide_client) {
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = GetRange;
args.key = key;
args.key_len = key_len;
/* Add start and end arguments */
args.args[0].type = CORE_ARG_TYPE_LONG;
args.args[0].data.long_arg.value = start;
args.args[1].type = CORE_ARG_TYPE_LONG;
args.args[1].data.long_arg.value = end;
args.arg_count = 2;
/* Allocate string result processor on heap for batch support */
int ret = execute_core_command(
valkey_glide, &args, NULL, process_core_string_result, return_value);
if (ret > 0) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
/* Note: output will be freed later in process_core_string_result */
ZVAL_COPY(return_value, object);
return 1;
}
/* Command succeeded with data */
return 1;
} else if (ret == 0) {
/* Key didn't exist, return empty string */
return 1;
} else {
/* Error */
}
}
return 0;
}
/* Helper function to build SORT command arguments */
static void build_sort_args(const char* key,
size_t key_len,
zval* sort_pattern,
zend_bool* alpha_out,
zend_bool* desc_out,
uintptr_t** args_ptr,
unsigned long** args_len_ptr,
unsigned long* arg_count_ptr,
char** offset_str,
char** count_str) {
zend_bool alpha = 0, desc = 0, explicit_asc = 0;
/* Parse sort options from the pattern array first */
if (sort_pattern && Z_TYPE_P(sort_pattern) == IS_ARRAY) {
HashTable* ht = Z_ARRVAL_P(sort_pattern);
zval* z_ele;
/* Check for SORT option (case-insensitive) */
if ((z_ele = zend_hash_str_find(ht, "sort", sizeof("sort") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht, "SORT", sizeof("SORT") - 1)) != NULL) {
if (Z_TYPE_P(z_ele) == IS_STRING) {
const char* sort_val = Z_STRVAL_P(z_ele);
if (strcasecmp(sort_val, "desc") == 0 || strcasecmp(sort_val, "DESC") == 0) {
desc = 1;
} else if (strcasecmp(sort_val, "asc") == 0 || strcasecmp(sort_val, "ASC") == 0) {
explicit_asc = 1; /* Explicitly ASC */
desc = 0;
}
}
}
/* Check for ALPHA option (case-insensitive) */
if ((z_ele = zend_hash_str_find(ht, "alpha", sizeof("alpha") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht, "ALPHA", sizeof("ALPHA") - 1)) != NULL) {
if (Z_TYPE_P(z_ele) == IS_TRUE ||
(Z_TYPE_P(z_ele) == IS_LONG && Z_LVAL_P(z_ele) != 0) ||
(Z_TYPE_P(z_ele) == IS_STRING && strcasecmp(Z_STRVAL_P(z_ele), "true") == 0)) {
alpha = 1;
}
}
}
/* Set output flags */
if (alpha_out)
*alpha_out = alpha;
if (desc_out)
*desc_out = desc;
/* Calculate the maximum number of arguments */
unsigned long max_args = 1; /* key */
if (sort_pattern && Z_TYPE_P(sort_pattern) == IS_ARRAY) {
/* Patterns array can have: BY, LIMIT, GET, STORE */
HashTable* ht = Z_ARRVAL_P(sort_pattern);
max_args +=
2 * zend_hash_num_elements(ht) + 2; /* Extra space for possible LIMIT offset count */
}
if (alpha)
max_args++; /* ALPHA */
if (desc)
max_args++; /* DESC */
/* Allocate arrays for arguments */
uintptr_t* args = (uintptr_t*) emalloc(max_args * sizeof(uintptr_t));
unsigned long* args_len = (unsigned long*) emalloc(max_args * sizeof(unsigned long));
/* Current argument index */
unsigned long arg_idx = 0;
/* First argument: key */
args[arg_idx] = (uintptr_t) key;
args_len[arg_idx] = key_len;
arg_idx++;
/* Add sort patterns if provided */
if (sort_pattern && Z_TYPE_P(sort_pattern) == IS_ARRAY) {
HashTable* ht = Z_ARRVAL_P(sort_pattern);
zval* z_ele;
zend_string* z_key;
zend_ulong num_key;
/* Check for BY pattern (case-insensitive) */
if ((z_ele = zend_hash_str_find(ht, "by", sizeof("by") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht, "BY", sizeof("BY") - 1)) != NULL) {
if (Z_TYPE_P(z_ele) == IS_STRING) {
/* Add BY keyword */
args[arg_idx] = (uintptr_t) "BY";
args_len[arg_idx] = 2;
arg_idx++;
/* Add BY pattern */
args[arg_idx] = (uintptr_t) Z_STRVAL_P(z_ele);
args_len[arg_idx] = Z_STRLEN_P(z_ele);
arg_idx++;
}
}
/* Check for LIMIT array format: 'LIMIT' => [offset, count] */
zval* z_limit = NULL;
if ((z_limit = zend_hash_str_find(ht, "limit", sizeof("limit") - 1)) != NULL ||
(z_limit = zend_hash_str_find(ht, "LIMIT", sizeof("LIMIT") - 1)) != NULL) {
if (Z_TYPE_P(z_limit) == IS_ARRAY) {
HashTable* limit_ht = Z_ARRVAL_P(z_limit);
if (zend_hash_num_elements(limit_ht) >= 2) {
zval* z_offset = zend_hash_index_find(limit_ht, 0);
zval* z_count = zend_hash_index_find(limit_ht, 1);
if (z_offset && z_count) {
/* Add LIMIT keyword */
args[arg_idx] = (uintptr_t) "LIMIT";
args_len[arg_idx] = 5;
arg_idx++;
/* Add offset */
size_t offset_len;
long offset_val = zval_get_long(z_offset);
*offset_str = long_to_string(offset_val, &offset_len);
if (*offset_str) {
args[arg_idx] = (uintptr_t) *offset_str;
args_len[arg_idx] = offset_len;
arg_idx++;
/* Add count */
size_t count_len;
long count_val = zval_get_long(z_count);
*count_str = long_to_string(count_val, &count_len);
if (*count_str) {
args[arg_idx] = (uintptr_t) *count_str;
args_len[arg_idx] = count_len;
arg_idx++;
}
}
}
}
}
}
/* Check for GET patterns (case-insensitive) */
if ((z_ele = zend_hash_str_find(ht, "get", sizeof("get") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht, "GET", sizeof("GET") - 1)) != NULL) {
if (Z_TYPE_P(z_ele) == IS_ARRAY) {
/* Handle array of GET patterns: 'get' => ['pattern1', 'pattern2'] */
HashTable* get_ht = Z_ARRVAL_P(z_ele);
zval* z_pattern;
ZEND_HASH_FOREACH_VAL(get_ht, z_pattern) {
if (Z_TYPE_P(z_pattern) == IS_STRING) {
/* Add GET keyword */
args[arg_idx] = (uintptr_t) "GET";
args_len[arg_idx] = 3;
arg_idx++;
/* Add GET pattern */
args[arg_idx] = (uintptr_t) Z_STRVAL_P(z_pattern);
args_len[arg_idx] = Z_STRLEN_P(z_pattern);
arg_idx++;
}
}
ZEND_HASH_FOREACH_END();
} else if (Z_TYPE_P(z_ele) == IS_STRING) {
/* Handle single GET pattern: 'get' => 'pattern' */
args[arg_idx] = (uintptr_t) "GET";
args_len[arg_idx] = 3;
arg_idx++;
args[arg_idx] = (uintptr_t) Z_STRVAL_P(z_ele);
args_len[arg_idx] = Z_STRLEN_P(z_ele);
arg_idx++;
}
}
/* Check for STORE destination (case-insensitive) */
if ((z_ele = zend_hash_str_find(ht, "store", sizeof("store") - 1)) != NULL ||
(z_ele = zend_hash_str_find(ht, "STORE", sizeof("STORE") - 1)) != NULL) {
if (Z_TYPE_P(z_ele) == IS_STRING) {
/* Add STORE keyword */
args[arg_idx] = (uintptr_t) "STORE";
args_len[arg_idx] = 5;
arg_idx++;
/* Add STORE destination key */
args[arg_idx] = (uintptr_t) Z_STRVAL_P(z_ele);
args_len[arg_idx] = Z_STRLEN_P(z_ele);
arg_idx++;
}
}
}
/* Add sorting options */
if (alpha) {
args[arg_idx] = (uintptr_t) "ALPHA";
args_len[arg_idx] = 5;
arg_idx++;
}
if (desc) {
args[arg_idx] = (uintptr_t) "DESC";
args_len[arg_idx] = 4;
arg_idx++;
} else if (explicit_asc) {
args[arg_idx] = (uintptr_t) "ASC";
args_len[arg_idx] = 3;
arg_idx++;
}
/* Set output parameters */
*args_ptr = args;
*args_len_ptr = args_len;
*arg_count_ptr = arg_idx;
}
int process_sort_result(CommandResponse* response, void* output, zval* return_value) {
int ret_val = 0;
ret_val =
command_response_to_zval(response, return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
return ret_val;
}
/* Execute a SORT command using the Valkey Glide client */
int execute_sort_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len = 0;
zval* z_opts = NULL;
zend_bool alpha = 0, desc = 0;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "Os|a", &object, ce, &key, &key_len, &z_opts) ==
FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
/* If we have a Glide client, use it */
if (valkey_glide->glide_client) {
/* Build command arguments */
uintptr_t* args = NULL;
unsigned long* args_len = NULL;
unsigned long arg_count = 0;
char* offset_str = NULL;
char* count_str = NULL;
build_sort_args(key,
key_len,
z_opts,
&alpha,
&desc,
&args,
&args_len,
&arg_count,
&offset_str,
&count_str);
if (!args || !args_len || arg_count == 0) {
if (args)
efree(args);
if (args_len)
efree(args_len);
return 0;
}
CommandResult* cmd_result = NULL;
/* Check for batch mode */
if (valkey_glide->is_in_batch_mode) {
/* Create batch-compatible processor wrapper */
int res = buffer_command_for_batch(
valkey_glide, Sort, args, args_len, arg_count, NULL, process_sort_result);
} else {
/* Execute the command */
cmd_result = execute_command(valkey_glide->glide_client,
Sort, /* command type */
arg_count, /* number of arguments */
args, /* arguments */
args_len /* argument lengths */
);
}
/* Free the argument arrays */
efree(args);
efree(args_len);
if (offset_str)
efree(offset_str);
if (count_str)
efree(count_str);
/* Process the result */
int ret_val = 0;
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
ret_val = 1;
} else {
/* Check if we have a valid result */
if (!cmd_result || !cmd_result->response) {
if (cmd_result)
free_command_result(cmd_result);
return 0;
}
/* Check for STORE option */
ret_val = process_sort_result(cmd_result->response, NULL, return_value);
}
free_command_result(cmd_result);
return ret_val;
}
return 0;
}
/* Execute a SORT_RO command using the Valkey Glide client */
int execute_sort_ro_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len = 0;
zval* z_opts = NULL;
zend_bool alpha = 0, desc = 0;
char* offset_str = NULL;
char* count_str = NULL;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "Os|a", &object, ce, &key, &key_len, &z_opts) ==
FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
/* If we have a Glide client, use it */
if (valkey_glide->glide_client) {
/* Build command arguments */
uintptr_t* args = NULL;
unsigned long* args_len = NULL;
unsigned long arg_count = 0;
build_sort_args(key,
key_len,
z_opts,
&alpha,
&desc,
&args,
&args_len,
&arg_count,
&offset_str,
&count_str);
if (!args || !args_len || arg_count == 0) {
if (args)
efree(args);
if (args_len)
efree(args_len);
return 0;
}
CommandResult* cmd_result = NULL;
/* Check for batch mode */
if (valkey_glide->is_in_batch_mode) {
/* Create batch-compatible processor wrapper */
int res = buffer_command_for_batch(
valkey_glide, Sort, args, args_len, arg_count, NULL, process_sort_result);
} else {
/* Execute the command */
cmd_result = execute_command(valkey_glide->glide_client,
SortReadOnly, /* command type */
arg_count, /* number of arguments */
args, /* arguments */
args_len /* argument lengths */
);
}
/* Free the argument arrays */
efree(args);
efree(args_len);
if (offset_str)
efree(offset_str);
if (count_str)
efree(count_str);
int ret_val = 0;
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
ret_val = 1;
} else {
/* Check if we have a valid result */
if (!cmd_result || !cmd_result->response) {
if (cmd_result)
free_command_result(cmd_result);
return 0;
}
ret_val = process_sort_result(cmd_result->response, NULL, return_value);
}
/* Process the result */
free_command_result(cmd_result);
return ret_val;
}
return 0;
}