forked from valkey-io/valkey-glide-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalkey_glide_commands_3.c
More file actions
1912 lines (1639 loc) · 70.4 KB
/
valkey_glide_commands_3.c
File metadata and controls
1912 lines (1639 loc) · 70.4 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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
| 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 "ext/standard/php_var.h"
#include "include/glide_bindings.h"
#include "valkey_glide_commands_common.h"
#include "valkey_glide_core_common.h"
#include "valkey_glide_hash_common.h"
#include "valkey_glide_z_common.h"
/* Helper functions for batch state management */
static void clear_batch_state(valkey_glide_object* valkey_glide);
static void expand_command_buffer(valkey_glide_object* valkey_glide);
/* Helper function to process array arguments for FCALL commands */
static void process_array_to_args(zval* array,
uintptr_t* cmd_args,
unsigned long* args_len,
unsigned long* arg_index);
/* Helper functions to reduce code duplication */
static int convert_zval_args_to_strings(zval* args,
int args_count,
uintptr_t** cmd_args,
unsigned long** args_len,
char*** allocated_strings,
int* allocated_count);
static enum RequestType determine_client_command_type(zval* args, int args_count);
static void cleanup_allocated_strings(char** allocated_strings, int allocated_count);
static int command_response_to_zval_wrapper(CommandResponse* response,
void* output,
zval* return_value);
/* Execute a WAIT command using the Valkey Glide client - MIGRATED TO CORE FRAMEWORK */
int execute_wait_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
long numreplicas, timeout;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "Oll", &object, ce, &numreplicas, &timeout) ==
FAILURE) {
return 0;
}
if (numreplicas < 0) {
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 = Wait;
/* WAIT is a server-level command (not key-based) with 2 arguments: numreplicas, timeout */
args.args[0].type = CORE_ARG_TYPE_LONG;
args.args[0].data.long_arg.value = numreplicas;
args.args[1].type = CORE_ARG_TYPE_LONG;
args.args[1].data.long_arg.value = timeout;
args.arg_count = 2;
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;
}
/* Helper function implementations */
/* Clear batch state and free buffered commands */
static void clear_batch_state(valkey_glide_object* valkey_glide) {
if (!valkey_glide) {
return;
}
if (valkey_glide->buffered_commands) {
/* Free each buffered command */
size_t i, j;
for (i = 0; i < valkey_glide->command_count; i++) {
struct batch_command* cmd = &valkey_glide->buffered_commands[i];
/* Free argument arrays */
if (cmd->args) {
for (j = 0; j < cmd->arg_count; j++) {
if (cmd->args[j]) {
efree(cmd->args[j]);
}
}
efree(cmd->args);
}
if (cmd->arg_lengths) {
efree(cmd->arg_lengths);
}
}
efree(valkey_glide->buffered_commands);
valkey_glide->buffered_commands = NULL;
valkey_glide->command_capacity = 0;
}
valkey_glide->is_in_batch_mode = false;
valkey_glide->batch_type = MULTI;
valkey_glide->command_count = 0;
}
/* Expand command buffer capacity */
static void expand_command_buffer(valkey_glide_object* valkey_glide) {
if (!valkey_glide) {
return;
}
size_t new_capacity = valkey_glide->command_capacity * 2;
if (new_capacity == 0) {
new_capacity = 16;
}
struct batch_command* new_buffer = (struct batch_command*) erealloc(
valkey_glide->buffered_commands, new_capacity * sizeof(struct batch_command));
if (new_buffer) {
valkey_glide->buffered_commands = new_buffer;
valkey_glide->command_capacity = new_capacity;
/* Initialize new slots */
size_t i;
for (i = valkey_glide->command_count; i < new_capacity; i++) {
memset(&valkey_glide->buffered_commands[i], 0, sizeof(struct batch_command));
}
}
}
/* Buffer a command for batch execution */
int buffer_command_for_batch(valkey_glide_object* valkey_glide,
enum RequestType cmd_type,
const uintptr_t* args,
const unsigned long* arg_lengths,
uintptr_t arg_count,
void* result_ptr,
z_result_processor_t process_result) {
if (!valkey_glide || !valkey_glide->is_in_batch_mode) {
return 0;
}
/* Expand buffer if needed */
if (valkey_glide->command_count >= valkey_glide->command_capacity) {
expand_command_buffer(valkey_glide);
if (valkey_glide->command_count >= valkey_glide->command_capacity) {
return 0; /* Failed to expand */
}
}
struct batch_command* cmd = &valkey_glide->buffered_commands[valkey_glide->command_count];
/* Store command details */
cmd->request_type = cmd_type;
cmd->arg_count = arg_count;
cmd->result_ptr = result_ptr;
cmd->process_result = process_result;
/* Copy arguments */
if (arg_count > 0 && args && arg_lengths) {
cmd->args = (uint8_t**) emalloc(arg_count * sizeof(uint8_t*));
cmd->arg_lengths = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
uintptr_t i;
for (i = 0; i < arg_count; i++) {
if (args[i] && arg_lengths[i] > 0) {
cmd->args[i] = (uint8_t*) emalloc(arg_lengths[i] + 1);
if (cmd->args[i]) {
memcpy(cmd->args[i], ((uint8_t**) args)[i], arg_lengths[i]);
cmd->args[i][arg_lengths[i]] = '\0';
cmd->arg_lengths[i] = arg_lengths[i];
} else {
cmd->arg_lengths[i] = 0;
}
} else {
cmd->args[i] = NULL;
cmd->arg_lengths[i] = 0;
}
}
} else {
cmd->args = NULL;
cmd->arg_lengths = NULL;
}
valkey_glide->command_count++;
return 1;
}
/* Helper function to process array arguments for FCALL commands */
static void process_array_to_args(zval* array,
uintptr_t* cmd_args,
unsigned long* args_len,
unsigned long* arg_index) {
if (array && Z_TYPE_P(array) == IS_ARRAY) {
zval* val;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(array), val) {
if (Z_TYPE_P(val) != IS_STRING) {
zval temp;
ZVAL_COPY(&temp, val);
convert_to_string(&temp);
cmd_args[*arg_index] = (uintptr_t) Z_STRVAL(temp);
args_len[*arg_index] = Z_STRLEN(temp);
zval_dtor(&temp);
} else {
cmd_args[*arg_index] = (uintptr_t) Z_STRVAL_P(val);
args_len[*arg_index] = Z_STRLEN_P(val);
}
(*arg_index)++;
}
ZEND_HASH_FOREACH_END();
}
}
/* Helper function to convert zval arguments to strings - reduces duplication */
static int convert_zval_args_to_strings(zval* args,
int args_count,
uintptr_t** cmd_args,
unsigned long** args_len,
char*** allocated_strings,
int* allocated_count) {
if (!args || args_count <= 0) {
return 0;
}
*cmd_args = (uintptr_t*) emalloc(args_count * sizeof(uintptr_t));
*args_len = (unsigned long*) emalloc(args_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc(args_count * sizeof(char*));
*allocated_count = 0;
for (int i = 0; i < args_count; i++) {
zval* arg = &args[i];
if (Z_TYPE_P(arg) == IS_STRING) {
/* Already a string, use directly */
(*cmd_args)[i] = (uintptr_t) Z_STRVAL_P(arg);
(*args_len)[i] = Z_STRLEN_P(arg);
} else {
/* Convert non-string types to string */
zval copy;
size_t str_len;
char* str;
ZVAL_DUP(©, arg);
convert_to_string(©);
str_len = Z_STRLEN(copy);
str = emalloc(str_len + 1);
memcpy(str, Z_STRVAL(copy), str_len);
str[str_len] = '\0';
(*cmd_args)[i] = (uintptr_t) str;
(*args_len)[i] = str_len;
/* Track allocated string for cleanup */
(*allocated_strings)[(*allocated_count)++] = str;
zval_dtor(©);
}
}
return 1;
}
/* Helper function to determine CLIENT command type - reduces duplication */
static enum RequestType determine_client_command_type(zval* args, int args_count) {
if (args_count > 0 && Z_TYPE(args[0]) == IS_STRING) {
const char* subcmd = Z_STRVAL(args[0]);
if (strcasecmp(subcmd, "KILL") == 0) {
return args_count > 1 ? ClientKill : ClientKillSimple;
} else if (strcasecmp(subcmd, "LIST") == 0) {
return ClientList;
} else if (strcasecmp(subcmd, "GETNAME") == 0) {
return ClientGetName;
} else if (strcasecmp(subcmd, "ID") == 0) {
return ClientId;
// } else if (strcasecmp(subcmd, "SETNAME") == 0) {
// return ClientSetName;
} else if (strcasecmp(subcmd, "PAUSE") == 0) {
return ClientPause;
} else if (strcasecmp(subcmd, "UNPAUSE") == 0) {
return ClientUnpause;
} else if (strcasecmp(subcmd, "REPLY") == 0) {
return ClientReply;
} else if (strcasecmp(subcmd, "INFO") == 0) {
return ClientInfo;
} else if (strcasecmp(subcmd, "NO-EVICT") == 0) {
return ClientNoEvict;
}
}
return InvalidRequest; /* Default */
}
/* Helper function to determine FUNCTION command type - reduces duplication */
static enum RequestType determine_function_command_type(zval* args, int args_count) {
if (args_count > 0 && Z_TYPE(args[0]) == IS_STRING) {
const char* subcmd = Z_STRVAL(args[0]);
if (strcasecmp(subcmd, "DELETE") == 0) {
return FunctionDelete;
} else if (strcasecmp(subcmd, "DUMP") == 0) {
return FunctionDump;
} else if (strcasecmp(subcmd, "FLUSH") == 0) {
return FunctionFlush;
} else if (strcasecmp(subcmd, "KILL") == 0) {
return FunctionKill;
} else if (strcasecmp(subcmd, "LIST") == 0) {
return FunctionList;
} else if (strcasecmp(subcmd, "LOAD") == 0) {
return FunctionLoad;
} else if (strcasecmp(subcmd, "RESTORE") == 0) {
return FunctionRestore;
} else if (strcasecmp(subcmd, "STATS") == 0) {
return FunctionStats;
}
}
return InvalidRequest; /* Invalid function subcommand */
}
/* Helper function to cleanup allocated strings - reduces duplication */
static void cleanup_allocated_strings(char** allocated_strings, int allocated_count) {
if (allocated_strings) {
for (int i = 0; i < allocated_count; i++) {
if (allocated_strings[i]) {
efree(allocated_strings[i]);
}
}
efree(allocated_strings);
}
}
/* Helper function to parse CLIENT LIST response into array of associative arrays */
static int parse_client_list_response(const char* response_str,
size_t response_len,
zval* return_value) {
if (!response_str || response_len == 0) {
array_init(return_value);
return 1;
}
array_init(return_value);
/* Split response by newlines to get individual client entries */
const char* line_start = response_str;
const char* line_end = response_str;
const char* response_end = response_str + response_len;
while (line_start < response_end) {
/* Find end of current line */
while (line_end < response_end && *line_end != '\n' && *line_end != '\r') {
line_end++;
}
/* Skip empty lines */
if (line_end > line_start) {
size_t line_length = line_end - line_start;
/* Create associative array for this client */
zval client_array;
array_init(&client_array);
/* Parse key=value pairs in this line */
const char* token_start = line_start;
const char* token_end = line_start;
while (token_start < line_start + line_length) {
/* Find end of current token (space-separated) */
while (token_end < line_start + line_length && *token_end != ' ') {
token_end++;
}
if (token_end > token_start) {
/* Look for '=' in token to split key and value */
const char* equals_pos = token_start;
while (equals_pos < token_end && *equals_pos != '=') {
equals_pos++;
}
if (equals_pos < token_end && *equals_pos == '=') {
/* Extract key and value */
size_t key_len = equals_pos - token_start;
size_t value_len = token_end - equals_pos - 1;
if (key_len > 0) {
/* Create null-terminated key string */
char* key = emalloc(key_len + 1);
memcpy(key, token_start, key_len);
key[key_len] = '\0';
/* Create null-terminated value string */
char* value = emalloc(value_len + 1);
if (value_len > 0) {
memcpy(value, equals_pos + 1, value_len);
}
value[value_len] = '\0';
/* Add to client array */
add_assoc_string(&client_array, key, value);
/* Free temporary strings */
efree(key);
efree(value);
}
}
}
/* Move to next token */
token_start = token_end;
/* Skip spaces */
while (token_start < line_start + line_length && *token_start == ' ') {
token_start++;
}
token_end = token_start;
}
/* Add client array to main result array */
add_next_index_zval(return_value, &client_array);
}
/* Move to next line */
while (line_end < response_end && (*line_end == '\n' || *line_end == '\r')) {
line_end++;
}
line_start = line_end;
}
return 1;
}
/* Wrapper function to match z_result_processor_t signature */
static int command_response_to_zval_wrapper(CommandResponse* response,
void* output,
zval* return_value) {
enum RequestType command_type = *((enum RequestType*) output);
efree(output);
if (command_type == ClientList && response->response_type == String) {
return parse_client_list_response(
response->string_value, response->string_value_len, return_value);
} else {
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
}
}
static int process_function_command_reposonse(CommandResponse* response,
void* output,
zval* return_value) {
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_ASSOSIATIVE_ARRAY_MAP_FUNCTION, true);
}
static int process_fcall_command_reposonse(CommandResponse* response,
void* output,
zval* return_value) {
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_ASSOSIATIVE_ARRAY_MAP, false);
}
/* Execute a FUNCTION command using the Valkey Glide client */
int execute_function_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
zval* z_args;
int args_count;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "O*", &object, ce, &z_args, &args_count) ==
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) {
/* Check if args are valid */
if (!z_args || args_count <= 0) {
return 0;
}
/* Determine the specific function command type */
enum RequestType function_command_type =
determine_function_command_type(z_args, args_count);
if (function_command_type == InvalidRequest) {
/* Unknown function subcommand */
return 0;
}
/* Use helper function to convert remaining arguments to strings (skip the subcommand) */
uintptr_t* cmd_args = NULL;
unsigned long* args_len = NULL;
char** allocated_strings = NULL;
int allocated_count = 0;
unsigned long final_arg_count = 0;
/* If there are arguments after the subcommand, convert them */
if (args_count > 1) {
if (!convert_zval_args_to_strings(&z_args[1],
args_count - 1,
&cmd_args,
&args_len,
&allocated_strings,
&allocated_count)) {
return 0;
}
final_arg_count = args_count - 1;
}
/* Check if we're in batch mode */
if (valkey_glide->is_in_batch_mode) {
/* Convert arguments to uint8_t** format for batch processing */
uintptr_t* batch_args = NULL;
unsigned long* arg_lengths = NULL;
if (final_arg_count > 0) {
batch_args = (uintptr_t*) emalloc(final_arg_count * sizeof(uintptr_t*));
arg_lengths = (uintptr_t*) emalloc(final_arg_count * sizeof(unsigned long));
/* Copy arguments to batch format */
for (unsigned long i = 0; i < final_arg_count; i++) {
batch_args[i] = cmd_args[i];
arg_lengths[i] = args_len[i];
}
}
/* Buffer the command for batch execution */
int buffer_result = buffer_command_for_batch(valkey_glide,
function_command_type,
batch_args,
arg_lengths,
final_arg_count,
NULL,
process_function_command_reposonse);
/* Free the argument arrays */
cleanup_allocated_strings(allocated_strings, allocated_count);
if (cmd_args)
efree(cmd_args);
if (args_len)
efree(args_len);
if (batch_args)
efree(batch_args);
if (arg_lengths)
efree(arg_lengths);
if (buffer_result) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
return 1;
}
return 0;
} else {
/* Execute the command directly */
CommandResult* result = execute_command(valkey_glide->glide_client,
function_command_type,
final_arg_count,
cmd_args,
args_len);
/* Free the argument arrays using helper function */
cleanup_allocated_strings(allocated_strings, allocated_count);
if (cmd_args)
efree(cmd_args);
if (args_len)
efree(args_len);
/* Handle the result directly */
int status = 0;
if (result) {
if (result->command_error) {
/* Command failed */
free_command_result(result);
return 0;
}
if (result->response) {
/* FUNCTION can return various types based on subcommand */
status =
process_function_command_reposonse(result->response, NULL, return_value);
free_command_result(result);
return status;
}
free_command_result(result);
}
}
}
return 0;
}
/* Common function to initialize batch mode */
static int initialize_batch_mode(valkey_glide_object* valkey_glide,
int batch_type,
zval* object,
zval* return_value) {
if (!valkey_glide || !valkey_glide->glide_client) {
return 0;
}
if (valkey_glide->is_in_batch_mode) {
return 1;
}
/* Initialize batch mode */
valkey_glide->is_in_batch_mode = true;
valkey_glide->batch_type = batch_type;
valkey_glide->command_count = 0;
/* Initialize buffer if needed */
if (!valkey_glide->buffered_commands) {
valkey_glide->command_capacity = 16; /* Initial capacity */
valkey_glide->buffered_commands = (struct batch_command*) ecalloc(
valkey_glide->command_capacity, sizeof(struct batch_command));
}
/* Return $this for method chaining */
ZVAL_COPY(return_value, object);
return 1;
}
/* Execute a MULTI command using the Valkey Glide client - UPDATED FOR BUFFERING */
int execute_multi_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
long batch_type = MULTI; /* Default to MULTI */
/* Parse optional batch type parameter */
if (zend_parse_method_parameters(argc, object, "O|l", &object, ce, &batch_type) == FAILURE) {
return 0;
}
/* Validate batch type using existing constants */
if (batch_type != MULTI && batch_type != PIPELINE) {
php_error_docref(NULL, E_WARNING, "Invalid batch type. Use MULTI or PIPELINE");
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
return initialize_batch_mode(valkey_glide, (int) batch_type, object, return_value);
}
/* Execute a PIPELINE command using the Valkey Glide client - wrapper using common function */
int execute_pipeline_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
/* Parse parameters - pipeline takes no additional parameters */
if (zend_parse_method_parameters(argc, object, "O", &object, ce) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
return initialize_batch_mode(valkey_glide, PIPELINE, object, return_value);
}
/* Execute a DISCARD command using the Valkey Glide client - UPDATED FOR BUFFERING */
int execute_discard_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "O", &object, ce) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide || !valkey_glide->glide_client) {
return 0;
}
/* Clear batch state if we're in batch mode */
if (valkey_glide->is_in_batch_mode) {
clear_batch_state(valkey_glide);
ZVAL_TRUE(return_value);
return 1;
} else {
ZVAL_FALSE(return_value);
return 0;
}
}
/* Execute an EXEC command using the Valkey Glide client - UPDATED FOR BUFFERING */
int execute_exec_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "O", &object, ce) == FAILURE) {
return 0;
}
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide || !valkey_glide->glide_client) {
return 0;
}
/* Check if we're in batch mode and have buffered commands */
if (!valkey_glide->is_in_batch_mode || valkey_glide->command_count == 0) {
ZVAL_FALSE(return_value);
return 0;
}
/* Convert buffered commands to FFI BatchInfo structure */
struct CmdInfo** cmd_infos =
(struct CmdInfo**) emalloc(valkey_glide->command_count * sizeof(struct CmdInfo*));
if (!cmd_infos) {
clear_batch_state(valkey_glide);
ZVAL_FALSE(return_value);
return 0;
}
/* Create CmdInfo structures for each buffered command */
size_t i;
for (i = 0; i < valkey_glide->command_count; i++) {
struct batch_command* buffered = &valkey_glide->buffered_commands[i];
struct CmdInfo* cmd_info = (struct CmdInfo*) emalloc(sizeof(struct CmdInfo));
if (!cmd_info) {
/* Cleanup on error */
for (size_t j = 0; j < i; j++) {
efree(cmd_infos[j]);
}
efree(cmd_infos);
clear_batch_state(valkey_glide);
ZVAL_FALSE(return_value);
return 0;
}
cmd_info->request_type = buffered->request_type;
cmd_info->args = (const uint8_t* const*) buffered->args;
cmd_info->arg_count = buffered->arg_count;
cmd_info->args_len = (const uintptr_t*) buffered->arg_lengths;
cmd_infos[i] = cmd_info;
}
/* Create BatchInfo structure */
struct BatchInfo batch_info = {.cmd_count = valkey_glide->command_count,
.cmds = (const struct CmdInfo* const*) cmd_infos,
.is_atomic = (valkey_glide->batch_type == MULTI)};
/* Execute via FFI batch() function */
struct CommandResult* result = batch(valkey_glide->glide_client,
0, /* callback_index (not used for sync) */
&batch_info,
false, /* raise_on_error */
NULL, /* options */
0 /* span_ptr */
);
/* Free CmdInfo structures */
for (i = 0; i < valkey_glide->command_count; i++) {
efree(cmd_infos[i]);
}
efree(cmd_infos);
/* Process results and clear batch state */
int status = 0;
if (result) {
if (result->command_error) {
/* Command failed */
free_command_result(result);
clear_batch_state(valkey_glide);
ZVAL_FALSE(return_value);
return 0;
}
status = 1; /* Assume success unless we find issues */
if (result->response) {
if (result->response->response_type != Array ||
result->response->array_value_len != valkey_glide->command_count) {
ZVAL_FALSE(return_value);
status = 0;
free_command_result(result);
clear_batch_state(valkey_glide);
return status;
}
array_init(return_value);
for (int64_t idx = 0; idx < result->response->array_value_len; idx++) {
zval value;
int process_status = valkey_glide->buffered_commands[idx].process_result(
&result->response->array_value[idx],
valkey_glide->buffered_commands[idx].result_ptr,
&value);
if (process_status) {
/* Add the processed result to return array */
add_next_index_zval(return_value, &value);
} else {
/* Process_result failed, use raw response */
ZVAL_FALSE(&value);
add_next_index_zval(return_value, &value);
}
}
} else {
/* Failed to get responses array, return false */
ZVAL_FALSE(return_value);
status = 0;
}
}
free_command_result(result);
clear_batch_state(valkey_glide);
return status;
}
/* Internal function to execute FCALL/FCALL_RO commands using the Valkey Glide client */
static int execute_fcall_command_internal(zval* object,
valkey_glide_object* valkey_glide,
char* name,
size_t name_len,
zval* keys_array,
zval* args_array,
enum RequestType command_type,
zval* return_value) {
/* Check if name is valid */
if (!name || name_len <= 0) {
return 0;
}
/* Calculate numkeys from keys array */
long numkeys = 0;
if (keys_array && Z_TYPE_P(keys_array) == IS_ARRAY) {
numkeys = zend_hash_num_elements(Z_ARRVAL_P(keys_array));
}
/* Calculate args count from args array */
long args_count = 0;
if (args_array && Z_TYPE_P(args_array) == IS_ARRAY) {
args_count = zend_hash_num_elements(Z_ARRVAL_P(args_array));
}
/* Prepare numkeys as string */
char numkeys_str[32];
snprintf(numkeys_str, sizeof(numkeys_str), "%ld", numkeys);
/* Calculate total arguments: function_name + numkeys + keys + args */
unsigned long arg_count = 2 + numkeys + args_count;
uintptr_t* cmd_args = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
unsigned long* args_len = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
/* Set function name and numkeys */
cmd_args[0] = (uintptr_t) name;
args_len[0] = name_len;
cmd_args[1] = (uintptr_t) numkeys_str;
args_len[1] = strlen(numkeys_str);
unsigned long arg_index = 2;
/* Process keys array */
process_array_to_args(keys_array, cmd_args, args_len, &arg_index);
/* Process args array */
process_array_to_args(args_array, cmd_args, args_len, &arg_index);
CommandResult* 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,
command_type,
cmd_args,
args_len,
arg_count,
NULL,
process_fcall_command_reposonse);
} else {
result = execute_command(valkey_glide->glide_client,
command_type, /* command type */
arg_count, /* number of arguments */
cmd_args, /* arguments */
args_len /* argument lengths */
);
}
/* Free the argument arrays */
efree(cmd_args);
efree(args_len);
/* Handle the result directly */
int status = 0;
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
return 1;
}
if (result) {
if (result->command_error) {
/* Command failed */
free_command_result(result);
return 0;
}
else if (result->response) {
/* FCALL can return various types */
status = process_fcall_command_reposonse(result->response, NULL, return_value);
free_command_result(result);
return status;
}
free_command_result(result);
}
return 0;
}
/* Execute an FCALL command using the Valkey Glide client */
int execute_fcall_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* name = NULL;
size_t name_len;
zval* keys_array = NULL;
zval* args_array = NULL;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osa|a", &object, ce, &name, &name_len, &keys_array, &args_array) ==
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) {
return execute_fcall_command_internal(
object, valkey_glide, name, name_len, keys_array, args_array, FCall, return_value);
}
return 0;
}
/* Execute an FCALL_RO command using the Valkey Glide client */
int execute_fcall_ro_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* name = NULL;
size_t name_len;
zval* keys_array = NULL;
zval* args_array = NULL;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osa|a", &object, ce, &name, &name_len, &keys_array, &args_array) ==
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) {
return execute_fcall_command_internal(object,
valkey_glide,
name,
name_len,
keys_array,