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_hash_common.c
More file actions
3129 lines (2684 loc) · 105 KB
/
valkey_glide_hash_common.c
File metadata and controls
3129 lines (2684 loc) · 105 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
/*
+----------------------------------------------------------------------+
| Valkey Glide Hash 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "valkey_glide_hash_common.h"
#include "common.h"
#include "ext/standard/php_var.h"
#include "valkey_glide_core_common.h"
#include "valkey_glide_z_common.h"
extern zend_class_entry* ce;
extern zend_class_entry* get_valkey_glide_exception_ce();
/* ====================================================================
* CORE FRAMEWORK FUNCTIONS
* ==================================================================== */
/**
* Generic hash command execution framework with batch support
*/
int execute_h_generic_command(valkey_glide_object* valkey_glide,
enum RequestType cmd_type,
h_command_args_t* args,
void* result_ptr,
z_result_processor_t process_result,
zval* return_value) {
uintptr_t* cmd_args = NULL;
unsigned long* args_len = NULL;
char** allocated_strings = NULL;
int allocated_count = 0;
int arg_count = 0;
int status = 0;
/* Validate basic arguments */
VALIDATE_HASH_ARGS(valkey_glide->glide_client, args->key);
/* Prepare arguments based on command type */
switch (cmd_type) {
case HLen:
arg_count = prepare_h_key_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HGet:
case HExists:
case HStrlen:
arg_count = prepare_h_single_field_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HSetNX:
arg_count = prepare_h_field_value_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HDel:
case HMGet:
arg_count = prepare_h_multi_field_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HSet:
arg_count = prepare_h_set_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HMSet:
arg_count = prepare_h_mset_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HIncrBy:
case HIncrByFloat:
arg_count = prepare_h_incr_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count, cmd_type);
break;
case HRandField:
arg_count = prepare_h_randfield_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HKeys:
case HVals:
case HGetAll:
arg_count = prepare_h_key_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HSetEx:
arg_count = prepare_h_hfe_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HExpire:
case HPExpire:
case HExpireAt:
case HPExpireAt:
arg_count = prepare_h_expire_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HTtl:
case HPTtl:
case HExpireTime:
case HPExpireTime:
case HPersist:
arg_count = prepare_h_field_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HGetEx:
arg_count = prepare_h_getex_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
default:
if (result_ptr) {
efree(args->fields);
efree(result_ptr);
}
return 0;
}
if (arg_count <= 0) {
if (result_ptr) {
efree(args->fields);
efree(result_ptr);
}
goto cleanup;
}
/* Check for batch mode */
if (valkey_glide->is_in_batch_mode) {
status = buffer_command_for_batch(
valkey_glide, cmd_type, cmd_args, args_len, arg_count, result_ptr, process_result);
goto cleanup;
}
/* Execute the command */
CommandResult* result =
execute_command(valkey_glide->glide_client, cmd_type, arg_count, cmd_args, args_len);
/* Process result */
if (result && Z_TYPE_P(return_value) != IS_FALSE) {
if (!result->command_error && result->response && process_result) {
status = process_result(result->response, result_ptr, return_value);
} else {
if (result_ptr) {
efree(args->fields);
efree(result_ptr);
}
}
free_command_result(result);
} else {
if (result_ptr) {
efree(args->fields);
efree(result_ptr);
}
}
cleanup:
/* Clean up allocated resources */
cleanup_h_command_args(allocated_strings, allocated_count, cmd_args, args_len);
return status;
}
z_result_processor_t get_processor_for_response_type(int response_type);
/**
* Batch-aware version for unified command executors
*/
int execute_h_simple_command(valkey_glide_object* valkey_glide,
enum RequestType cmd_type,
h_command_args_t* args,
void* result_ptr,
int response_type,
zval* return_value) {
uintptr_t* cmd_args = NULL;
unsigned long* args_len = NULL;
char** allocated_strings = NULL;
int allocated_count = 0;
int arg_count = 0;
int status = 0;
/* Validate basic arguments */
VALIDATE_HASH_ARGS(valkey_glide->glide_client, args->key);
/* Prepare arguments based on command type */
switch (cmd_type) {
case HLen:
case HKeys:
case HVals:
case HGetAll:
arg_count = prepare_h_key_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HGet:
case HExists:
case HStrlen:
arg_count = prepare_h_single_field_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HSetNX:
arg_count = prepare_h_field_value_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HDel:
arg_count = prepare_h_multi_field_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HSet:
arg_count = prepare_h_set_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HMSet:
arg_count = prepare_h_mset_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HIncrBy:
arg_count = prepare_h_incr_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count, HIncrBy);
break;
case HSetEx:
arg_count = prepare_h_hfe_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HExpire:
case HPExpire:
case HExpireAt:
case HPExpireAt:
arg_count = prepare_h_expire_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HTtl:
case HPTtl:
case HExpireTime:
case HPExpireTime:
case HPersist:
arg_count = prepare_h_field_only_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
case HGetEx:
arg_count = prepare_h_getex_args(
args, &cmd_args, &args_len, &allocated_strings, &allocated_count);
break;
default:
goto cleanup;
}
if (arg_count <= 0) {
goto cleanup;
}
/* Check for batch mode */
z_result_processor_t processor = get_processor_for_response_type(response_type);
if (!processor) {
status = 0;
goto cleanup;
}
if (valkey_glide->is_in_batch_mode) {
status = buffer_command_for_batch(
valkey_glide, cmd_type, cmd_args, args_len, arg_count, result_ptr, processor);
goto cleanup;
}
/* Execute the command */
CommandResult* result =
execute_command(valkey_glide->glide_client, cmd_type, arg_count, cmd_args, args_len);
/* Process result using standard handlers */
if (result && Z_TYPE_P(return_value) != IS_FALSE) {
if (!result->command_error && result->response && processor) {
status = processor(result->response, result_ptr, return_value);
}
free_command_result(result);
} else {
status = 0;
}
cleanup:
/* Clean up allocated resources */
cleanup_h_command_args(allocated_strings, allocated_count, cmd_args, args_len);
return status;
}
/* ====================================================================
* ARGUMENT PREPARATION FUNCTIONS
* ==================================================================== */
// Unified parameter preparation with configurable components
typedef struct {
bool needs_expiry;
bool needs_condition;
bool needs_fields_keyword;
bool field_value_pairs; // true for HSETEX, false for HEXPIRE/HTTL
const char* condition_prefix; // "F" for HSETEX (FNX/FXX), NULL for HEXPIRE (NX/XX)
} h_arg_config_t;
static int prepare_h_args_unified(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count,
const h_arg_config_t* config) {
if (!args->key)
return 0;
// Calculate field count and validate
int field_count = config->field_value_pairs ? args->fv_count / 2 : args->fv_count;
if (config->field_value_pairs && args->fv_count % 2 != 0)
return 0;
// Calculate argument count
int arg_count = 1; // key
if (config->needs_condition && args->mode)
arg_count++;
if (config->needs_expiry && args->expiry > 0)
arg_count += 2; // unit + value
if (config->needs_fields_keyword)
arg_count += 2; // "FIELDS" + count
arg_count += args->fv_count; // fields or field-value pairs
// Allocate arrays
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc((2 + args->fv_count) * sizeof(char*));
*allocated_count = 0;
int arg_idx = 0;
// Add key
(*args_out)[arg_idx] = (uintptr_t) args->key;
(*args_len_out)[arg_idx] = args->key_len;
arg_idx++;
// Add condition
if (config->needs_condition && args->mode) {
const char* condition = args->mode;
if (config->condition_prefix) {
// Convert NX/XX to FNX/FXX for HSETEX
static char condition_buf[4];
snprintf(
condition_buf, sizeof(condition_buf), "%s%s", config->condition_prefix, args->mode);
condition = condition_buf;
}
(*args_out)[arg_idx] = (uintptr_t) condition;
(*args_len_out)[arg_idx] = strlen(condition);
arg_idx++;
}
// Add expiry
if (config->needs_expiry && args->expiry > 0) {
const char* expiry_unit = args->expiry_type ? args->expiry_type : "EX";
(*args_out)[arg_idx] = (uintptr_t) expiry_unit;
(*args_len_out)[arg_idx] = strlen(expiry_unit);
arg_idx++;
size_t expiry_len;
char* expiry_str = safe_format_long_long(args->expiry, &expiry_len);
add_string_arg(expiry_str,
expiry_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
}
// Add FIELDS keyword and count
if (config->needs_fields_keyword) {
(*args_out)[arg_idx] = (uintptr_t) "FIELDS";
(*args_len_out)[arg_idx] = 6;
arg_idx++;
size_t field_count_len;
char* field_count_str = safe_format_int(field_count, &field_count_len);
add_string_arg(field_count_str,
field_count_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
}
// Add fields/field-value pairs
populate_field_args(args->field_values,
args->fv_count,
arg_idx,
*args_out,
*args_len_out,
*allocated_strings,
allocated_count);
return arg_count;
}
// Simplified preparation functions using unified approach
int prepare_h_key_only_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
*args_out = (uintptr_t*) emalloc(sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(sizeof(unsigned long));
*allocated_strings = NULL;
*allocated_count = 0;
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
return 1;
}
int prepare_h_hfe_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
h_arg_config_t config = {
.needs_expiry = true,
.needs_condition = true,
.needs_fields_keyword = true,
.field_value_pairs = true,
.condition_prefix = "F"}; // Correct: F prefix for HSETEX (NX->FNX, XX->FXX)
return prepare_h_args_unified(
args, args_out, args_len_out, allocated_strings, allocated_count, &config);
}
int prepare_h_expire_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->key)
return 0;
// Calculate field count
int field_count = args->fv_count;
// Calculate argument count: key + expiry + condition + "FIELDS" + count + fields
int arg_count = 1; // key
if (args->expiry > 0)
arg_count++; // expiry value (no EX keyword for HEXPIRE)
if (args->mode)
arg_count++; // condition
arg_count += 2; // "FIELDS" + count
arg_count += args->fv_count; // fields
// Allocate arrays
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc((2 + args->fv_count) * sizeof(char*));
*allocated_count = 0;
int arg_idx = 0;
// Add key
(*args_out)[arg_idx] = (uintptr_t) args->key;
(*args_len_out)[arg_idx] = args->key_len;
arg_idx++;
// Add expiry value directly (no EX keyword for HEXPIRE)
if (args->expiry > 0) {
size_t expiry_len;
char* expiry_str = safe_format_long_long(args->expiry, &expiry_len);
add_string_arg(expiry_str,
expiry_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
}
// Add condition (NX/XX directly, no prefix for HEXPIRE)
if (args->mode) {
(*args_out)[arg_idx] = (uintptr_t) args->mode;
(*args_len_out)[arg_idx] = strlen(args->mode);
arg_idx++;
}
// Add FIELDS keyword and count
(*args_out)[arg_idx] = (uintptr_t) "FIELDS";
(*args_len_out)[arg_idx] = 6;
arg_idx++;
size_t field_count_len;
char* field_count_str = safe_format_int(field_count, &field_count_len);
add_string_arg(field_count_str,
field_count_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
// Add fields (no values for HEXPIRE)
populate_field_args(args->field_values,
args->fv_count,
arg_idx,
*args_out,
*args_len_out,
*allocated_strings,
allocated_count);
return arg_count;
}
int prepare_h_field_only_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
h_arg_config_t config = {.needs_expiry = false,
.needs_condition = false,
.needs_fields_keyword = true,
.field_value_pairs = false,
.condition_prefix = NULL};
return prepare_h_args_unified(
args, args_out, args_len_out, allocated_strings, allocated_count, &config);
}
/**
* Prepare arguments for single-field commands (HGET, HEXISTS, HSTRLEN)
*/
int prepare_h_single_field_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->field) {
return 0;
}
/* Allocate argument arrays */
*args_out = (uintptr_t*) emalloc(2 * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(2 * sizeof(unsigned long));
*allocated_strings = NULL;
*allocated_count = 0;
/* Set key and field arguments */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
(*args_out)[1] = (uintptr_t) args->field;
(*args_len_out)[1] = args->field_len;
return 2;
}
/**
* Prepare arguments for field-value commands (HSETNX)
*/
int prepare_h_field_value_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->field || !args->value) {
return 0;
}
/* Allocate argument arrays */
*args_out = (uintptr_t*) emalloc(3 * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(3 * sizeof(unsigned long));
*allocated_strings = NULL;
*allocated_count = 0;
/* Set key, field, and value arguments */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
(*args_out)[1] = (uintptr_t) args->field;
(*args_len_out)[1] = args->field_len;
(*args_out)[2] = (uintptr_t) args->value;
(*args_len_out)[2] = args->value_len;
return 3;
}
/**
* Prepare arguments for multi-field commands (HDEL, HMGET)
*/
int prepare_h_multi_field_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->fields || args->field_count <= 0) {
return 0;
}
/* Calculate total argument count: key + fields */
unsigned long arg_count = 1 + args->field_count;
/* Allocate argument arrays */
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc(args->field_count * sizeof(char*));
*allocated_count = 0;
/* Set key as first argument */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
/* Convert and add fields */
return convert_zval_array_to_args(args->fields,
1,
*args_out,
*args_len_out,
*allocated_strings,
allocated_count,
args->field_count);
}
/**
* Prepare arguments for HSET command (handles both formats)
*/
int prepare_h_set_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->field_values) {
return 0;
}
/* Handle associative array format */
if (args->is_array_arg) {
if (args->fv_count != 1 || Z_TYPE(args->field_values[0]) != IS_ARRAY) {
return 0;
}
zval* z_array = &args->field_values[0];
HashTable* ht = Z_ARRVAL_P(z_array);
int pairs_count = zend_hash_num_elements(ht);
if (pairs_count == 0) {
return 0;
}
/* Prepare command arguments: key + field-value pairs */
unsigned long arg_count = 1 + (pairs_count * 2);
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc((pairs_count * 2) * sizeof(char*));
*allocated_count = 0;
/* First argument: key */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
/* Process field-value pairs */
return process_field_value_pairs(
z_array, *args_out, *args_len_out, 1, *allocated_strings, allocated_count);
} else {
/* Original variadic usage */
if (args->fv_count < 2 || args->fv_count % 2 != 0) {
return 0;
}
/* Prepare command arguments: key + field/value pairs */
unsigned long arg_count = 1 + args->fv_count;
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc(args->fv_count * sizeof(char*));
*allocated_count = 0;
/* First argument: key */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
/* Convert field/value pairs */
return convert_zval_array_to_args(args->field_values,
1,
*args_out,
*args_len_out,
*allocated_strings,
allocated_count,
args->fv_count);
}
}
/**
* Prepare arguments for HMSET command
*/
int prepare_h_mset_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->field_values || args->fv_count <= 0) {
return 0;
}
/* HMSET expects an associative array */
HashTable* ht = Z_ARRVAL_P(args->field_values);
int pairs_count = zend_hash_num_elements(ht);
unsigned long arg_count = 1 + (pairs_count * 2);
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc((pairs_count * 2) * sizeof(char*));
*allocated_count = 0;
/* First argument: key */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
/* Process field-value pairs */
return process_field_value_pairs(
args->field_values, *args_out, *args_len_out, 1, *allocated_strings, allocated_count);
}
/**
* Prepare arguments for increment commands (HINCRBY, HINCRBYFLOAT)
*/
int prepare_h_incr_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count,
enum RequestType cmd_type) {
if (!args->field) {
return 0;
}
/* Allocate argument arrays */
*args_out = (uintptr_t*) emalloc(3 * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(3 * sizeof(unsigned long));
*allocated_strings = (char**) emalloc(sizeof(char*));
*allocated_count = 0;
/* Set key and field arguments */
(*args_out)[0] = (uintptr_t) args->key;
(*args_len_out)[0] = args->key_len;
(*args_out)[1] = (uintptr_t) args->field;
(*args_len_out)[1] = args->field_len;
/* Convert increment value to string */
char* incr_str;
size_t incr_len;
if (args->float_incr != 0.0) {
/* HINCRBYFLOAT */
incr_str = double_to_string(args->float_incr, &incr_len);
} else {
/* HINCRBY */
incr_str = long_to_string(args->increment, &incr_len);
}
(*allocated_strings)[0] = incr_str;
*allocated_count = 1;
(*args_out)[2] = (uintptr_t) incr_str;
(*args_len_out)[2] = incr_len;
return 3;
}
/**
* Prepare arguments for HRANDFIELD command
*/
int prepare_h_randfield_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
/* Calculate argument count */
unsigned long arg_count = 1; /* key */
int need_count_str = 0;
if (args->count != 1) {
arg_count++; /* count */
need_count_str = 1;
}
if (args->withvalues) {
arg_count++; /* WITHVALUES */
if (need_count_str == 0) {
arg_count++; /* count (required with WITHVALUES) */
need_count_str = 1;
}
}
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = need_count_str ? (char**) emalloc(sizeof(char*)) : NULL;
*allocated_count = 0;
/* First argument: key */
int arg_idx = 0;
(*args_out)[arg_idx] = (uintptr_t) args->key;
(*args_len_out)[arg_idx] = args->key_len;
arg_idx++;
/* Add count if needed */
if (need_count_str) {
char* count_str = long_to_string(args->count, &(*args_len_out)[arg_idx]);
(*allocated_strings)[0] = count_str;
*allocated_count = 1;
(*args_out)[arg_idx] = (uintptr_t) count_str;
arg_idx++;
}
/* Add WITHVALUES if specified */
if (args->withvalues) {
const char* withvalues_str = "WITHVALUES";
(*args_out)[arg_idx] = (uintptr_t) withvalues_str;
(*args_len_out)[arg_idx] = strlen(withvalues_str);
arg_idx++;
}
return arg_count;
}
/**
* Safely populate field arguments from zval array
* Handles string conversion and memory management correctly
* NOTE: Always creates copies because cleanup code expects to efree() all allocated_strings
*/
int populate_field_args(zval* field_values,
int fv_count,
int start_idx,
uintptr_t* args_out,
unsigned long* args_len_out,
char** allocated_strings,
int* allocated_count) {
int arg_idx = start_idx;
for (int i = 0; i < fv_count; i++) {
zval* field = &field_values[i];
char* field_str;
if (Z_TYPE_P(field) == IS_STRING) {
/* Create copy of original string - required for cleanup compatibility */
field_str = estrdup(Z_STRVAL_P(field));
} else {
/* Convert to string and create copy */
zval temp;
ZVAL_COPY(&temp, field);
convert_to_string(&temp);
field_str = estrdup(Z_STRVAL(temp));
zval_dtor(&temp);
}
/* Store copy in allocated_strings for cleanup */
allocated_strings[(*allocated_count)++] = field_str;
args_out[arg_idx] = (uintptr_t) field_str;
args_len_out[arg_idx] = strlen(field_str);
arg_idx++;
}
return arg_idx;
}
/**
* Prepare arguments for HGETEX command
* Redis format: HGETEX key [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT
* unix-time-milliseconds|PERSIST] FIELDS numfields field [field ...]
*/
int prepare_h_getex_args(h_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count) {
if (!args->key || !args->fields || args->field_count == 0) {
return 0;
}
int field_count = args->field_count; // just fields, no values
int arg_count = 3 + args->field_count; /* key + "FIELDS" + field_count + fields */
if (args->expiry > 0 || (args->expiry_type && strcmp(args->expiry_type, "PERSIST") == 0)) {
if (args->expiry_type && strcmp(args->expiry_type, "PERSIST") == 0) {
arg_count += 1; /* PERSIST only */
} else {
arg_count += 2; /* expiry unit + expiry value */
}
}
*args_out = (uintptr_t*) emalloc(arg_count * sizeof(uintptr_t));
*args_len_out = (unsigned long*) emalloc(arg_count * sizeof(unsigned long));
*allocated_strings = (char**) emalloc(
(2 + args->field_count) * sizeof(char*)); // expiry + field_count + field conversions
*allocated_count = 0;
int arg_idx = 0;
/* Add key */
(*args_out)[arg_idx] = (uintptr_t) args->key;
(*args_len_out)[arg_idx] = args->key_len;
arg_idx++;
/* Add expiry unit and time if specified */
if (args->expiry > 0 || (args->expiry_type && strcmp(args->expiry_type, "PERSIST") == 0)) {
const char* expiry_unit = args->expiry_type ? args->expiry_type : "EX";
(*args_out)[arg_idx] = (uintptr_t) expiry_unit;
(*args_len_out)[arg_idx] = strlen(expiry_unit);
arg_idx++;
if (strcmp(expiry_unit, "PERSIST") != 0) {
size_t expiry_len;
char* expiry_str = safe_format_long_long(args->expiry, &expiry_len);
add_string_arg(expiry_str,
expiry_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
}
}
/* Add "FIELDS" keyword */
(*args_out)[arg_idx] = (uintptr_t) "FIELDS";
(*args_len_out)[arg_idx] = 6;
arg_idx++;
/* Add field count */
size_t field_count_len;
char* field_count_str = safe_format_int(field_count, &field_count_len);
add_string_arg(field_count_str,
field_count_len,
args_out,
args_len_out,
&arg_idx,
allocated_strings,
allocated_count);
/* Add fields only */
populate_field_args(args->fields,
args->field_count,
arg_idx,
*args_out,
*args_len_out,
*allocated_strings,
allocated_count);
return arg_count;
}
/* ====================================================================
* BATCH-COMPATIBLE RESULT PROCESSORS
* ==================================================================== */
int process_h_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;
}
return 0;
}
/**
* Batch-compatible wrapper for boolean responses
*/
int process_h_bool_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response)
return 0;
int ret_val = -1;
if (response && response->response_type == Bool) {
ret_val = response->bool_value ? 1 : 0;
ZVAL_BOOL(return_value, ret_val);
}
return ret_val;
}
/**
* Batch-compatible wrapper for string responses
*/
int process_h_string_result_async(CommandResponse* response, void* output, zval* return_value) {
if (!response)
return 0;
return command_response_to_zval(
response, return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
}
/**
* Batch-compatible wrapper for array responses
*/
int process_h_array_result_async(CommandResponse* response, void* output, zval* return_value) {
/* Initialize return array */
return command_response_to_zval(
response, (zval*) return_value, COMMAND_RESPONSE_NOT_ASSOSIATIVE, false);
}
/**
* Batch-compatible wrapper for map responses
*/