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_s_common.c
More file actions
2208 lines (1848 loc) · 72.6 KB
/
valkey_glide_s_common.c
File metadata and controls
2208 lines (1848 loc) · 72.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
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
/*
+----------------------------------------------------------------------+
| ValkeyGlide Glide S-Commands Common Utilities |
+----------------------------------------------------------------------+
| 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 "valkey_glide_s_common.h"
#include "cluster_scan_cursor.h"
#include "command_response.h"
#include "common.h"
#include "logger.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);
/* ====================================================================
* UTILITY FUNCTIONS
* ==================================================================== */
/**
* Allocate command arguments arrays
*/
int allocate_s_command_args(int count, uintptr_t** args_out, unsigned long** args_len_out) {
*args_out = (uintptr_t*) emalloc(count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(count * sizeof(unsigned long));
return 1;
}
/**
* Free command arguments arrays
*/
void cleanup_s_command_args(uintptr_t* args, unsigned long* args_len) {
if (args)
efree(args);
if (args_len)
efree(args_len);
}
/**
* Convert array of zvals to string arguments
*/
int convert_zval_to_string_args(zval* input,
int count,
uintptr_t** args_out,
unsigned long** args_len_out,
int offset,
char*** allocated_strings,
int* allocated_count) {
int i;
zval temp;
for (i = 0; i < count; i++) {
zval* element = &input[i];
if (Z_TYPE_P(element) == IS_STRING) {
(*args_out)[offset + i] = (uintptr_t) Z_STRVAL_P(element);
(*args_len_out)[offset + i] = Z_STRLEN_P(element);
} else {
/* Convert other types to string */
ZVAL_COPY(&temp, element);
convert_to_string(&temp);
/* Create a permanent copy of the string before freeing the zval */
size_t str_len = Z_STRLEN(temp);
char* str_copy = emalloc(str_len + 1);
memcpy(str_copy, Z_STRVAL(temp), str_len);
str_copy[str_len] = '\0';
(*args_out)[offset + i] = (uintptr_t) str_copy;
(*args_len_out)[offset + i] = str_len;
zval_dtor(&temp);
/* Track the allocated string */
if (allocated_strings && allocated_count) {
/* Expand the allocated_strings array */
*allocated_strings =
erealloc(*allocated_strings, (*allocated_count + 1) * sizeof(char*));
(*allocated_strings)[*allocated_count] = str_copy;
(*allocated_count)++;
}
}
}
return 1;
}
/**
* Allocate a string representation of a long value
*/
char* alloc_long_string(long value, size_t* len_out) {
char temp[32];
size_t len = snprintf(temp, sizeof(temp), "%ld", value);
char* result = emalloc(len + 1);
if (result) {
memcpy(result, temp, len);
result[len] = '\0';
if (len_out)
*len_out = len;
}
return result;
}
/* ====================================================================
* ARGUMENT PREPARATION FUNCTIONS
* ==================================================================== */
/**
* Prepare arguments for key + members commands (SADD, SREM, SMISMEMBER)
*/
int prepare_s_key_members_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->key || args->key_len == 0 || !args->members ||
args->members_count <= 0) {
return 0;
}
unsigned long arg_count = 1 + args->members_count; /* key + members */
if (!allocate_s_command_args(arg_count, args_out, args_len_out)) {
return 0;
}
/* Set key as first argument */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
/* Convert and set member arguments */
convert_zval_to_string_args(args->members,
args->members_count,
args_out,
args_len_out,
1,
allocated_strings,
allocated_count);
return arg_count;
}
/**
* Prepare arguments for key-only commands (SCARD, SMEMBERS)
*/
int prepare_s_key_only_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->key || args->key_len == 0) {
return 0;
}
if (!allocate_s_command_args(1, args_out, args_len_out)) {
return 0;
}
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
return 1;
}
/**
* Prepare arguments for key + member commands (SISMEMBER)
*/
int prepare_s_key_member_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->key || args->key_len == 0 || !args->member ||
args->member_len == 0) {
return 0;
}
if (!allocate_s_command_args(2, args_out, args_len_out)) {
return 0;
}
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
(*args_out)[1] = (uintptr_t) args->member;
(*args_len_out)[1] = args->member_len;
return 2;
}
/**
* Prepare arguments for key + count commands (SPOP, SRANDMEMBER)
*/
int prepare_s_key_count_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->key || args->key_len == 0) {
return 0;
}
unsigned long arg_count = args->has_count ? 2 : 1;
if (!allocate_s_command_args(arg_count, args_out, args_len_out)) {
return 0;
}
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
if (args->has_count) {
char* count_str = alloc_long_string(args->count, NULL);
(*args_out)[1] = (uintptr_t) count_str;
(*args_len_out)[1] = strlen(count_str);
/* Track the allocated string */
if (allocated_strings && allocated_count) {
*allocated_strings =
erealloc(*allocated_strings, (*allocated_count + 1) * sizeof(char*));
(*allocated_strings)[*allocated_count] = count_str;
(*allocated_count)++;
}
}
return arg_count;
}
/**
* Prepare arguments for multi-key commands (SINTER, SUNION, SDIFF)
*/
int prepare_s_multi_key_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->keys || args->keys_count <= 0) {
return 0;
}
if (!allocate_s_command_args(args->keys_count, args_out, args_len_out)) {
return 0;
}
convert_zval_to_string_args(args->keys,
args->keys_count,
args_out,
args_len_out,
0,
allocated_strings,
allocated_count);
return args->keys_count;
}
/**
* Prepare arguments for multi-key + limit commands (SINTERCARD)
*/
int prepare_s_multi_key_limit_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->keys || args->keys_count <= 0) {
return 0;
}
unsigned long arg_count =
1 + args->keys_count + (args->has_limit ? 2 : 0); /* numkeys + keys + [LIMIT value] */
if (!allocate_s_command_args(arg_count, args_out, args_len_out)) {
return 0;
}
/* First argument is the number of keys */
char* numkeys_str = alloc_long_string(args->keys_count, NULL);
if (!numkeys_str) {
cleanup_s_command_args(*args_out, *args_len_out);
return 0;
}
(*args_out)[0] = (uintptr_t) numkeys_str;
(*args_len_out)[0] = strlen(numkeys_str);
/* Track the allocated numkeys string */
if (allocated_strings && allocated_count) {
*allocated_strings = erealloc(*allocated_strings, (*allocated_count + 1) * sizeof(char*));
(*allocated_strings)[*allocated_count] = numkeys_str;
(*allocated_count)++;
}
/* Add keys */
convert_zval_to_string_args(args->keys,
args->keys_count,
args_out,
args_len_out,
1,
allocated_strings,
allocated_count);
/* Add LIMIT if specified */
if (args->has_limit) {
(*args_out)[1 + args->keys_count] = (uintptr_t) "LIMIT";
(*args_len_out)[1 + args->keys_count] = 5;
char* limit_str = alloc_long_string(args->limit, NULL);
if (!limit_str) {
cleanup_s_command_args(*args_out, *args_len_out);
return 0;
}
(*args_out)[2 + args->keys_count] = (uintptr_t) limit_str;
(*args_len_out)[2 + args->keys_count] = strlen(limit_str);
/* Track the allocated limit string */
if (allocated_strings && allocated_count) {
*allocated_strings =
erealloc(*allocated_strings, (*allocated_count + 1) * sizeof(char*));
(*allocated_strings)[*allocated_count] = limit_str;
(*allocated_count)++;
}
}
return arg_count;
}
/**
* Prepare arguments for destination + multi-key commands (SINTERSTORE, SUNIONSTORE, SDIFFSTORE)
*/
int prepare_s_dst_multi_key_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->dst_key || args->dst_key_len == 0 || !args->keys ||
args->keys_count <= 0) {
return 0;
}
unsigned long arg_count = 1 + args->keys_count; /* destination + keys */
if (!allocate_s_command_args(arg_count, args_out, args_len_out)) {
return 0;
}
/* Set destination key */
(*args_out)[0] = (uintptr_t) args->dst_key;
(*args_len_out)[0] = args->dst_key_len;
/* Add source keys */
convert_zval_to_string_args(args->keys,
args->keys_count,
args_out,
args_len_out,
1,
allocated_strings,
allocated_count);
return arg_count;
}
/**
* Prepare arguments for two-key + member commands (SMOVE)
*/
int prepare_s_two_key_member_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->src_key || args->src_key_len == 0 || !args->dst_key ||
args->dst_key_len == 0 || !args->member || args->member_len == 0) {
return 0;
}
if (!allocate_s_command_args(3, args_out, args_len_out)) {
return 0;
}
(*args_out)[0] = (uintptr_t) args->src_key;
(*args_len_out)[0] = args->src_key_len;
(*args_out)[1] = (uintptr_t) args->dst_key;
(*args_len_out)[1] = args->dst_key_len;
(*args_out)[2] = (uintptr_t) args->member;
(*args_len_out)[2] = args->member_len;
return 3;
}
/**
* Prepare arguments for scan commands (SCAN, SSCAN)
*/
int prepare_s_scan_args(s_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->glide_client || !args->cursor) {
return 0;
}
int has_pattern = (args->pattern && args->pattern_len > 0);
int has_count = args->has_count;
int has_key = (args->key && args->key_len > 0); /* For SSCAN */
int has_type = args->has_type && (args->type && args->type_len > 0); /* For SCAN only */
unsigned long arg_count =
(has_key ? 1 : 0) + 1 + (has_pattern ? 2 : 0) + (has_count ? 2 : 0) + (has_type ? 2 : 0);
if (!allocate_s_command_args(arg_count, args_out, args_len_out)) {
return 0;
}
int arg_idx = 0;
/* Add key if this is SSCAN */
if (has_key) {
(*args_out)[arg_idx] = (uintptr_t) args->key;
(*args_len_out)[arg_idx] = args->key_len;
arg_idx++;
}
/* Add cursor */
size_t cursor_len = strlen(*args->cursor);
(*args_out)[arg_idx] = (uintptr_t) *args->cursor;
(*args_len_out)[arg_idx] = cursor_len;
arg_idx++;
/* Add MATCH pattern if provided */
if (has_pattern) {
(*args_out)[arg_idx] = (uintptr_t) "MATCH";
(*args_len_out)[arg_idx] = 5;
arg_idx++;
(*args_out)[arg_idx] = (uintptr_t) args->pattern;
(*args_len_out)[arg_idx] = args->pattern_len;
arg_idx++;
}
/* Add COUNT if provided */
if (has_count) {
(*args_out)[arg_idx] = (uintptr_t) "COUNT";
(*args_len_out)[arg_idx] = 5;
arg_idx++;
char* count_str = alloc_long_string(args->count, NULL);
(*args_out)[arg_idx] = (uintptr_t) count_str;
(*args_len_out)[arg_idx] = strlen(count_str);
arg_idx++;
/* Track the allocated count string */
if (allocated_strings && allocated_count) {
*allocated_strings =
erealloc(*allocated_strings, (*allocated_count + 1) * sizeof(char*));
(*allocated_strings)[*allocated_count] = count_str;
(*allocated_count)++;
}
}
/* Add TYPE if provided (SCAN only) */
if (has_type) {
(*args_out)[arg_idx] = (uintptr_t) "TYPE";
(*args_len_out)[arg_idx] = 4;
arg_idx++;
(*args_out)[arg_idx] = (uintptr_t) args->type;
(*args_len_out)[arg_idx] = args->type_len;
arg_idx++;
}
return arg_count;
}
/* ====================================================================
* RESPONSE PROCESSING FUNCTIONS
* ==================================================================== */
/**
* Batch-compatible async result processor for integer responses
*/
int process_s_int_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response) {
ZVAL_LONG(return_value, 0);
return 0;
}
if (response->response_type == Int) {
ZVAL_LONG(return_value, response->int_value);
return 1;
} else if (response->response_type == Null) {
ZVAL_NULL(return_value);
return 1;
}
return 0;
}
/**
* Batch-compatible async result processor for boolean responses
*/
int process_s_bool_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response) {
ZVAL_FALSE(return_value);
return 0;
}
if (response->response_type == Bool) {
ZVAL_BOOL(return_value, response->bool_value);
return 1;
} else if (response->response_type == Ok) {
ZVAL_TRUE(return_value);
return 1;
}
return 0;
}
/**
* Batch-compatible async result processor for set/array responses
*/
int process_s_set_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response) {
ZVAL_NULL(return_value);
return 0;
}
if (response->response_type == Null) {
ZVAL_NULL(return_value);
return 1;
} else if (response->response_type == Sets || response->response_type == Array) {
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
}
ZVAL_NULL(return_value);
return 0;
}
/**
* Batch-compatible async result processor for mixed responses (string or array)
*/
int process_s_mixed_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response) {
ZVAL_FALSE(return_value);
return 0;
}
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
}
/**
* Batch-compatible async result processor for scan responses
*/
typedef struct {
enum RequestType cmd_type;
char* cursor;
zval* scan_iter;
} scan_data_t;
int process_s_scan_result_async(CommandResponse* response, void* output, zval* return_value) {
scan_data_t* args = (scan_data_t*) output;
int status = 0;
if (!response) {
if (args->scan_iter) {
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
VALKEY_LOG_ERROR("scan_processing",
"No response received in process_s_scan_result_async");
args->cursor = "0";
}
ZVAL_NULL(return_value);
return 0;
}
if (response->response_type != Array || response->array_value_len < 2) {
if (args->scan_iter) {
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
args->cursor = "0";
}
ZVAL_NULL(return_value);
return 0;
}
/* Extract cursor from first element */
CommandResponse* cursor_resp = &response->array_value[0];
const char* new_cursor_str = NULL;
if (cursor_resp->response_type == String) {
new_cursor_str = cursor_resp->string_value;
} else {
/* Handle unexpected cursor type */
if (args->scan_iter) {
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
args->cursor = "0";
}
ZVAL_NULL(return_value);
return 0;
}
/* Extract elements from second element */
CommandResponse* elements_resp = &response->array_value[1];
if (elements_resp->response_type != Array) {
array_init(return_value);
if (args->scan_iter) {
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
args->cursor = "0";
}
return 0;
}
/* Handle scan completion: when server returns cursor="0", scan is complete */
if (cursor_resp->string_value_len == 1 && cursor_resp->string_value[0] == '0') {
/* If there are elements in this final batch, return them using robust conversion */
if (elements_resp->array_value_len > 0) {
status = command_response_to_zval(elements_resp,
return_value,
(args->cmd_type == HScan || args->cmd_type == ZScan)
? COMMAND_RESPONSE_SCAN_ASSOSIATIVE_ARRAY
: COMMAND_RESPONSE_NOT_ASSOSIATIVE,
false);
if (args->scan_iter) {
zval_ptr_dtor(args->scan_iter);
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
/* Free old cursor and keep it as "0" to indicate completion */
if (args->cursor) {
efree(args->cursor);
}
args->cursor = emalloc(2);
strcpy(args->cursor, "0");
}
return status;
} else {
/* No elements in final batch - return FALSE to terminate loop */
array_init(return_value);
if (args->scan_iter) {
zval_ptr_dtor(args->scan_iter);
ZVAL_STRINGL(args->scan_iter, "0", 1);
efree(args->cursor);
efree(args);
} else {
/* Free old cursor and keep it as "0" to indicate completion */
if (args->cursor) {
efree(args->cursor);
}
args->cursor = emalloc(2);
strcpy(args->cursor, "0");
}
return 1;
}
}
/* Normal case: cursor != "0", update cursor string and return elements array */
if (args->scan_iter) {
/* For scan_iter mode, we'll update the zval directly and free everything */
status = command_response_to_zval(elements_resp,
return_value,
(args->cmd_type == HScan || args->cmd_type == ZScan)
? COMMAND_RESPONSE_SCAN_ASSOSIATIVE_ARRAY
: COMMAND_RESPONSE_NOT_ASSOSIATIVE,
false);
zval_ptr_dtor(args->scan_iter);
ZVAL_STRINGL(args->scan_iter, new_cursor_str, cursor_resp->string_value_len);
efree(args->cursor);
efree(args);
} else {
/* For non-scan_iter mode, update the cursor pointer */
if (args->cursor) {
efree(args->cursor);
}
/* Use length-controlled string copying to prevent reading beyond string boundary */
size_t cursor_len = cursor_resp->string_value_len;
args->cursor = emalloc(cursor_len + 1);
memcpy(args->cursor, new_cursor_str, cursor_len);
(args->cursor)[cursor_len] = '\0';
/* Use command_response_to_zval for robust element processing */
status = command_response_to_zval(elements_resp,
return_value,
(args->cmd_type == HScan || args->cmd_type == ZScan)
? COMMAND_RESPONSE_SCAN_ASSOSIATIVE_ARRAY
: COMMAND_RESPONSE_NOT_ASSOSIATIVE,
false);
}
return status;
}
/* ====================================================================
* CORE EXECUTION FRAMEWORK
* ==================================================================== */
/**
* Generic command execution for S commands with batch support
*/
int execute_s_generic_command(valkey_glide_object* valkey_glide,
enum RequestType cmd_type,
s_command_category_t category,
s_response_type_t response_type,
s_command_args_t* args,
zval* return_value) {
uintptr_t* cmd_args = NULL;
unsigned long* args_len = NULL;
int arg_count = 0;
int status = 0;
CommandResult* result = NULL;
/* Validate basic parameters */
if (!valkey_glide->glide_client || !args) {
return 0;
}
/* Initialize string tracking arrays */
char** allocated_strings = NULL;
int allocated_count = 0;
/* Prepare arguments based on category */
switch (category) {
case S_CMD_KEY_MEMBERS:
arg_count = prepare_s_key_members_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_KEY_ONLY:
arg_count = prepare_s_key_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_KEY_MEMBER:
arg_count = prepare_s_key_member_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_KEY_COUNT:
arg_count = prepare_s_key_count_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_MULTI_KEY:
arg_count = prepare_s_multi_key_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_MULTI_KEY_LIMIT:
arg_count = prepare_s_multi_key_limit_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_DST_MULTI_KEY:
arg_count = prepare_s_dst_multi_key_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_TWO_KEY_MEMBER:
arg_count = prepare_s_two_key_member_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case S_CMD_SCAN:
arg_count = prepare_s_scan_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
default:
return 0;
}
if (arg_count <= 0 || !cmd_args || !args_len) {
goto cleanup;
}
scan_data_t* scan_data = NULL;
z_result_processor_t process_result = NULL;
switch (response_type) {
case S_RESPONSE_INT:
process_result = process_s_int_result_async;
break;
case S_RESPONSE_BOOL:
process_result = process_s_bool_result_async;
break;
case S_RESPONSE_SET:
process_result = process_s_set_result_async;
break;
case S_RESPONSE_MIXED:
process_result = process_s_mixed_result_async;
break;
case S_RESPONSE_SCAN:
process_result = process_s_scan_result_async;
scan_data = emalloc(sizeof(scan_data_t));
scan_data->cmd_type = cmd_type;
scan_data->cursor = *args->cursor;
scan_data->scan_iter = args->scan_iter;
break;
default:
process_result = process_s_mixed_result_async;
break;
}
/* Check for batch mode */
if (valkey_glide && valkey_glide->is_in_batch_mode) {
/* Buffer command for batch execution */
status = buffer_command_for_batch(
valkey_glide, cmd_type, cmd_args, args_len, arg_count, scan_data, process_result);
goto cleanup;
}
/* Execute the command synchronously */
result = execute_command(valkey_glide->glide_client, cmd_type, arg_count, cmd_args, args_len);
if (result) {
status = process_result(result->response, scan_data, return_value);
}
free_command_result(result);
cleanup:
/* Free all allocated strings tracked by the prepare functions */
if (allocated_strings && allocated_count > 0) {
for (int i = 0; i < allocated_count; i++) {
if (allocated_strings[i]) {
efree(allocated_strings[i]);
}
}
efree(allocated_strings);
}
cleanup_s_command_args(cmd_args, args_len);
return status;
}
/* ====================================================================
* WRAPPER FUNCTIONS FOR EXISTING COMMANDS
* ==================================================================== */
/**
* Execute SADD command using the new signature pattern
*/
int execute_sadd_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len;
zval* z_args;
int members_count = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Os+", &object, ce, &key, &key_len, &z_args, &members_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) {
s_command_args_t args;
INIT_S_COMMAND_ARGS(args);
args.glide_client = valkey_glide->glide_client;
args.key = key;
args.key_len = key_len;
args.members = z_args;
args.members_count = members_count;
if (execute_s_generic_command(
valkey_glide, SAdd, S_CMD_KEY_MEMBERS, S_RESPONSE_INT, &args, return_value)) {
if (valkey_glide->is_in_batch_mode) {
ZVAL_COPY(return_value, object);
}
return 1;
}
}
return 0;
}
/**
* Execute SCARD command using the new signature pattern
*/
int execute_scard_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len;
/* 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) {
s_command_args_t args;
INIT_S_COMMAND_ARGS(args);
args.glide_client = valkey_glide->glide_client;
args.key = key;
args.key_len = key_len;
if (execute_s_generic_command(
valkey_glide, SCard, S_CMD_KEY_ONLY, S_RESPONSE_INT, &args, return_value)) {
if (valkey_glide->is_in_batch_mode) {
ZVAL_COPY(return_value, object);
}
return 1;
}
}
return 0;
}
/**
* Execute SRANDMEMBER command using the new signature pattern
*/
int execute_srandmember_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char* key = NULL;
size_t key_len;
zend_long count = 0;
int has_count = 0;
/* Parse parameters */
if (zend_parse_method_parameters(argc, object, "Os|l", &object, ce, &key, &key_len, &count) ==
FAILURE) {
return 0;
}
/* Check if count parameter was provided */
has_count = (argc > 1);
/* 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) {
s_command_args_t args;
INIT_S_COMMAND_ARGS(args);
args.glide_client = valkey_glide->glide_client;
args.key = key;
args.key_len = key_len;
args.count = has_count ? count : 1;
args.has_count = has_count;
if (execute_s_generic_command(valkey_glide,
SRandMember,
S_CMD_KEY_COUNT,
S_RESPONSE_MIXED,
&args,
return_value)) {
if (valkey_glide->is_in_batch_mode) {
ZVAL_COPY(return_value, object);
}
return 1;
}
}
return 0;
}
/**
* Execute SISMEMBER command using the new signature pattern
*/
int execute_sismember_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *member = NULL;
size_t key_len, member_len;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Oss", &object, ce, &key, &key_len, &member, &member_len) == FAILURE) {