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_core_commands.c
More file actions
2259 lines (1910 loc) · 79.1 KB
/
valkey_glide_core_commands.c
File metadata and controls
2259 lines (1910 loc) · 79.1 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 <zend.h>
#include <zend_API.h>
#include <zend_exceptions.h>
#include <ext/hash/php_hash.h>
#include <ext/spl/spl_exceptions.h>
#include <ext/standard/info.h>
#include "command_response.h"
#include "common.h"
#include "include/glide_bindings.h"
#include "logger.h"
#include "valkey_glide_commands_common.h"
#include "valkey_glide_core_common.h"
#include "valkey_glide_list_common.h"
#include "valkey_glide_z_common.h"
extern zend_class_entry* ce;
extern zend_class_entry* get_valkey_glide_exception_ce();
/* 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);
/* Create a connection request in protobuf format. Made visible for testing. */
uint8_t* create_connection_request(const char* host,
int port,
size_t* len,
valkey_glide_base_client_configuration_t* config,
valkey_glide_periodic_checks_status_t periodic_checks,
bool is_cluster,
bool refresh_topology_from_initial_nodes) {
/* Create a connection request */
ConnectionRequest__ConnectionRequest conn_req = CONNECTION_REQUEST__CONNECTION_REQUEST__INIT;
/* Set up the node address */
ConnectionRequest__NodeAddress default_addr = CONNECTION_REQUEST__NODE_ADDRESS__INIT;
default_addr.host = (char*) host;
default_addr.port = port;
ConnectionRequest__NodeAddress* default_addr_list[1] = {&default_addr};
bool request_contains_addresses = config->addresses && config->addresses_count > 0;
size_t addresses_count = request_contains_addresses ? config->addresses_count : 0;
ConnectionRequest__NodeAddress
request_addresses[addresses_count]; // Using a variable-length array to avoid memory
// management.
ConnectionRequest__NodeAddress* request_addresses_list[addresses_count];
for (size_t i = 0; i < addresses_count; ++i) {
// Initialize a temporary NodeAddress then copy it to request_addresses. This is not
// strictly necessary since we set all fields anyway, but follows the best practice of using
// protobuf initializers on new messages.
ConnectionRequest__NodeAddress temp_address = CONNECTION_REQUEST__NODE_ADDRESS__INIT;
request_addresses[i] = temp_address;
request_addresses[i].host = (char*) config->addresses[i].host;
request_addresses[i].port = config->addresses[i].port;
request_addresses_list[i] = &request_addresses[i];
}
ConnectionRequest__NodeAddress node_addr = CONNECTION_REQUEST__NODE_ADDRESS__INIT;
node_addr.host = (char*) host;
node_addr.port = port;
/* Add the node address to the connection request. Use the default endpoint if the constructor
call did not supply any endpoints. */
if (request_contains_addresses) {
conn_req.n_addresses = addresses_count;
conn_req.addresses = request_addresses_list;
} else {
conn_req.n_addresses = 1;
conn_req.addresses = default_addr_list;
}
/* Set up authentication if provided */
ConnectionRequest__AuthenticationInfo auth_info = CONNECTION_REQUEST__AUTHENTICATION_INFO__INIT;
ConnectionRequest__IamCredentials iam_creds = CONNECTION_REQUEST__IAM_CREDENTIALS__INIT;
if (config->credentials) {
auth_info.username = (char*) config->credentials->username;
auth_info.password = (char*) config->credentials->password;
/* Set up IAM credentials if provided */
if (config->credentials->iam_config) {
iam_creds.cluster_name = (char*) config->credentials->iam_config->cluster_name;
iam_creds.region = (char*) config->credentials->iam_config->region;
iam_creds.service_type = (config->credentials->iam_config->service_type ==
VALKEY_GLIDE_SERVICE_TYPE_MEMORYDB)
? CONNECTION_REQUEST__SERVICE_TYPE__MEMORYDB
: CONNECTION_REQUEST__SERVICE_TYPE__ELASTICACHE;
iam_creds.refresh_interval_seconds =
config->credentials->iam_config->refresh_interval_seconds;
auth_info.iam_credentials = &iam_creds;
/* Clear password when using IAM */
auth_info.password = (char*) protobuf_c_empty_string;
}
conn_req.authentication_info = &auth_info;
}
/* Set values from configuration */
conn_req.tls_mode = CONNECTION_REQUEST__TLS_MODE__NoTls;
if (config->use_tls) {
if (config->advanced_config && config->advanced_config->tls_config &&
config->advanced_config->tls_config->use_insecure_tls) {
conn_req.tls_mode = CONNECTION_REQUEST__TLS_MODE__InsecureTls;
} else {
conn_req.tls_mode = CONNECTION_REQUEST__TLS_MODE__SecureTls;
}
}
conn_req.cluster_mode_enabled = is_cluster;
conn_req.request_timeout =
config->request_timeout > 0 ? config->request_timeout : 5000; /* Default 5 seconds */
if (config->advanced_config) {
conn_req.connection_timeout = config->advanced_config->connection_timeout;
}
/* Set refresh topology from initial nodes for cluster mode */
if (is_cluster) {
conn_req.refresh_topology_from_initial_nodes = refresh_topology_from_initial_nodes;
} else {
conn_req.refresh_topology_from_initial_nodes = false;
}
conn_req.lazy_connect = config->lazy_connect;
/* Map read_from configuration */
if (config->read_from == VALKEY_GLIDE_READ_FROM_PREFER_REPLICA) {
conn_req.read_from = CONNECTION_REQUEST__READ_FROM__PreferReplica;
} else if (config->read_from == VALKEY_GLIDE_READ_FROM_AZ_AFFINITY) {
conn_req.read_from = CONNECTION_REQUEST__READ_FROM__AZAffinity;
} else if (config->read_from == VALKEY_GLIDE_READ_FROM_AZ_AFFINITY_REPLICAS_AND_PRIMARY) {
conn_req.read_from = CONNECTION_REQUEST__READ_FROM__AZAffinityReplicasAndPrimary;
} else {
conn_req.read_from = CONNECTION_REQUEST__READ_FROM__Primary;
}
/* Set database ID for standalone clients if it is valid. */
if (config->database_id >= 0) {
conn_req.database_id = config->database_id;
} else {
conn_req.database_id = 0;
}
/* Set the periodic checks for cluster clients. */
ConnectionRequest__PeriodicChecksDisabled periodic_check_disabled_info =
CONNECTION_REQUEST__PERIODIC_CHECKS_DISABLED__INIT;
ConnectionRequest__PeriodicChecksManualInterval periodic_checks_manual_interval_info =
CONNECTION_REQUEST__PERIODIC_CHECKS_MANUAL_INTERVAL__INIT;
if (is_cluster && periodic_checks == VALKEY_GLIDE_PERIODIC_CHECKS_DISABLED) {
conn_req.periodic_checks_disabled = &periodic_check_disabled_info;
conn_req.periodic_checks_case =
CONNECTION_REQUEST__CONNECTION_REQUEST__PERIODIC_CHECKS_PERIODIC_CHECKS_DISABLED;
} else {
conn_req.periodic_checks_manual_interval = &periodic_checks_manual_interval_info;
conn_req.periodic_checks_case =
CONNECTION_REQUEST__CONNECTION_REQUEST__PERIODIC_CHECKS_PERIODIC_CHECKS_MANUAL_INTERVAL;
}
conn_req.protocol = CONNECTION_REQUEST__PROTOCOL_VERSION__RESP3;
/* Set the reconnect strategy */
ConnectionRequest__ConnectionRetryStrategy retry_strategy =
CONNECTION_REQUEST__CONNECTION_RETRY_STRATEGY__INIT;
if (config->reconnect_strategy) {
conn_req.connection_retry_strategy = &retry_strategy;
retry_strategy.number_of_retries = config->reconnect_strategy->num_of_retries;
retry_strategy.factor = config->reconnect_strategy->factor;
retry_strategy.exponent_base = config->reconnect_strategy->exponent_base;
retry_strategy.jitter_percent = config->reconnect_strategy->jitter_percent;
}
/* Set client name */
conn_req.client_name = config->client_name ? config->client_name : "valkey-glide-php";
/* Set client AZ */
if (config->client_az) {
conn_req.client_az = config->client_az;
}
/* Calculate the size of the serialized message */
*len = connection_request__connection_request__get_packed_size(&conn_req);
/* Allocate memory for the serialized message */
uint8_t* buffer = (uint8_t*) emalloc(*len);
if (!buffer) {
*len = 0;
return NULL;
}
/* Serialize the message */
connection_request__connection_request__pack(&conn_req, buffer);
return buffer;
}
/* Create a Valkey Glide client or Cluster client using shared properties. */
static const ConnectionResponse* create_base_glide_client(
valkey_glide_base_client_configuration_t* config,
valkey_glide_periodic_checks_status_t periodic_checks,
bool is_cluster,
bool refresh_topology_from_initial_nodes) {
/* Create a connection request using first address or default */
size_t len;
const char* default_host = "localhost";
int default_port = 6379;
uint8_t* request_bytes = create_connection_request(default_host,
default_port,
&len,
config,
periodic_checks,
is_cluster,
refresh_topology_from_initial_nodes);
if (!request_bytes) {
return NULL;
}
/* Set up client type for synchronous operation */
ClientType client_type;
client_type.tag = SyncClient;
/* Create the client */
const ConnectionResponse* conn_resp =
create_client(request_bytes, len, &client_type, NULL /* No PubSub callback */
);
/* Free the request bytes as they're no longer needed */
efree(request_bytes);
/* Check if there was an error */
if (conn_resp->connection_error_message) {
VALKEY_LOG_ERROR("client_creation", conn_resp->connection_error_message);
}
return conn_resp;
}
/* Create a Valkey Glide client */
const ConnectionResponse* create_glide_client(valkey_glide_base_client_configuration_t* config) {
return create_base_glide_client(config, VALKEY_GLIDE_PERIODIC_CHECKS_DISABLED, false, false);
}
const ConnectionResponse* create_glide_cluster_client(
valkey_glide_cluster_client_configuration_t* config) {
return create_base_glide_client(&config->base,
config->periodic_checks_status,
true,
config->refresh_topology_from_initial_nodes);
}
/* Custom result processor for SET commands with GET option support */
struct set_result_data {
int has_get;
};
static int process_set_result(CommandResponse* response, void* output, zval* return_value) {
struct set_result_data* data = (struct set_result_data*) output;
if (!response) {
efree(output);
return 0;
}
switch (response->response_type) {
case Ok:
efree(output);
ZVAL_TRUE(return_value);
return 1; /* Success */
case Null:
efree(output);
ZVAL_FALSE(return_value);
return 0; /* Not set (NX/XX condition not met) */
case String:
/* GET option returned a value */
if (data->has_get && response->string_value) {
char* old_val = emalloc(response->string_value_len + 1);
size_t old_val_len = response->string_value_len;
if (old_val) {
memcpy(old_val, response->string_value, response->string_value_len);
(old_val)[response->string_value_len] = '\0';
}
ZVAL_STRINGL(return_value, old_val, old_val_len);
efree(old_val);
}
efree(output);
return 2; /* GET option returned a value */
default:
efree(output);
ZVAL_FALSE(return_value);
return 0; /* Error */
}
}
/* Custom result processor for PING command */
int process_ping_result(CommandResponse* response, void* output, zval* return_value) {
struct {
char* msg;
size_t msg_len;
}* string_output = output;
char* result;
size_t result_len;
if (!response || !string_output) {
efree(output);
return 0;
}
int status = 0; /* 1 = success, 0 = failure */
if (response->response_type == Ok) {
/* PONG response with no message */
result = estrdup("PONG");
result_len = 4;
status = 1;
} else if (response->response_type == String) {
/* PING with message - echo the message back */
if (response->string_value_len == 0) {
result = emalloc(1);
if (result) {
(result)[0] = '\0';
}
result_len = 0;
} else {
result = emalloc(response->string_value_len + 1);
if (result) {
memcpy(result, response->string_value, response->string_value_len);
(result)[response->string_value_len] = '\0';
}
result_len = response->string_value_len;
}
status = result ? 1 : 0;
} else if (response->response_type == Null) {
result = NULL;
result_len = 0;
status = 0;
}
if (status == 1) {
if (response != NULL) {
/* Check if message was provided */
int has_message = (string_output->msg != NULL && string_output->msg_len > 0);
/* If no message was provided and response is "PONG", return true */
if (!has_message && result_len == 4 && strncmp(result, "PONG", 4) == 0) {
efree(result);
ZVAL_TRUE(return_value);
status = 1;
} else {
/* Otherwise, return the actual response string */
ZVAL_STRINGL(return_value, result, result_len);
efree(result);
status = 1;
}
} else {
/* Success but no response (should return TRUE for PONG) */
ZVAL_TRUE(return_value);
status = 1;
}
}
efree(output);
return status;
}
/* These functions are now defined in command_response.c */
/* Execute a BITCOUNT command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_bitcount_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 start = 0, end = -1;
zend_bool bybit = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Os|llb", &object, ce, &key, &key_len, &start, &end, &bybit) == 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;
}
/* Execute using core framework */
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = BitCount;
args.key = key;
args.key_len = key_len;
/* Set range options */
args.options.start = start;
args.options.end = end;
args.options.has_range = 1;
args.options.bybit = bybit;
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 BITOP command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_bitop_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * op = NULL, *key = NULL;
size_t op_len, key_len;
zval* keys = NULL;
int keys_count = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Oss*", &object, ce, &op, &op_len, &key, &key_len, &keys, &keys_count) ==
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;
}
zval keys_array;
array_init_size(&keys_array, keys_count);
for (int i = 0; i < keys_count; i++) {
zval copy;
ZVAL_COPY(©, &keys[i]);
add_next_index_zval(&keys_array, ©);
}
/* Execute using core framework */
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = BitOp;
args.key = key; /* destination key */
args.key_len = key_len;
/* Add operation as first argument */
args.args[0].type = CORE_ARG_TYPE_STRING;
args.args[0].data.string_arg.value = op;
args.args[0].data.string_arg.len = op_len;
/* Add source keys as array argument (no limit on number of keys) */
args.args[1].type = CORE_ARG_TYPE_ARRAY;
args.args[1].data.array_arg.array = &keys_array;
args.args[1].data.array_arg.count = keys_count;
args.arg_count = 2; /* operation + source keys array */
int result =
execute_core_command(valkey_glide, &args, NULL, process_core_int_result, return_value);
zval_ptr_dtor(&keys_array);
if (result) {
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 BITPOS command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_bitpos_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 bit, start = 0, end = -1;
zend_bool bybit = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osl|llb", &object, ce, &key, &key_len, &bit, &start, &end, &bybit) ==
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;
}
/* Execute using core framework */
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = BitPos;
args.key = key;
args.key_len = key_len;
/* Add bit value argument */
args.args[0].type = CORE_ARG_TYPE_LONG;
args.args[0].data.long_arg.value = bit;
args.arg_count = 1;
/* Set range options */
args.options.start = start;
args.options.end = end;
args.options.has_range = 1;
args.options.bybit = bybit;
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 SET command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_set_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
zval * z_value, *z_expire = NULL, *z_opts = NULL;
char * key = NULL, *val = NULL;
size_t key_len, val_len;
double expire = 0;
zend_long expire_int = 0;
zval* z_set_opts = NULL; /* Will hold our options either from z_expire or z_opts */
int free_val = 0; /* Flag to track if we need to free val */
char* old_val = NULL; /* For storing GET response */
size_t old_val_len = 0;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osz|za", &object, ce, &key, &key_len, &z_value, &z_expire, &z_opts) ==
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 expire parameter was provided */
if (z_expire != NULL) {
switch (Z_TYPE_P(z_expire)) {
case IS_DOUBLE:
/* Double - use as timeout */
expire = Z_DVAL_P(z_expire);
expire_int = (zend_long) expire;
break;
case IS_LONG:
/* Long - use as timeout */
expire = (double) Z_LVAL_P(z_expire);
expire_int = Z_LVAL_P(z_expire);
break;
case IS_ARRAY:
/* Array - use as options */
z_set_opts = z_expire;
break;
case IS_NULL:
/* NULL - ignore */
break;
default:
/* Not a supported type - return false */
return 0;
}
}
/* If options were passed in z_opts, use those instead */
if (z_opts != NULL && Z_TYPE_P(z_opts) == IS_ARRAY) {
z_set_opts = z_opts;
}
/* Convert value based on its type */
switch (Z_TYPE_P(z_value)) {
case IS_STRING:
/* It's already a string, use directly */
val = Z_STRVAL_P(z_value);
val_len = Z_STRLEN_P(z_value);
break;
case IS_LONG:
/* Convert integer to string */
val = long_to_string(Z_LVAL_P(z_value), &val_len);
free_val = 1; // We'll need to free this
break;
case IS_DOUBLE:
/* Convert float to string */
val = double_to_string(Z_DVAL_P(z_value), &val_len);
free_val = 1; // We'll need to free this
break;
case IS_TRUE:
/* Convert boolean TRUE to "1" */
val = estrdup("1");
val_len = 1;
free_val = 1;
break;
case IS_FALSE:
/* Convert boolean FALSE to "0" */
val = estrdup("0");
val_len = 1;
free_val = 1;
break;
case IS_NULL:
/* Convert NULL to empty string */
val = estrdup("");
val_len = 0;
free_val = 1;
break;
default:
/* Unsupported type */
return 0;
}
/* Check if conversion succeeded */
if (!val) {
return 0;
}
/* Execute the SET command using the internal helper function */
int result = execute_set_command_internal(valkey_glide,
key,
key_len,
val,
val_len,
expire_int,
z_set_opts,
&old_val,
&old_val_len,
return_value);
/* Free the allocated string if needed */
if (free_val) {
efree(val);
}
/* Check for batch mode after successful execution */
if (result) {
if (valkey_glide->is_in_batch_mode) {
/* Clean up old_val if it was allocated */
if (old_val) {
efree(old_val);
}
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
}
return 1;
}
return 0; /* Should not reach here, but just in case */
}
/* Execute a SET command using the Valkey Glide client - INTERNAL HELPER FUNCTION */
int execute_set_command_internal(valkey_glide_object* valkey_glide,
const char* key,
size_t key_len,
const char* val,
size_t val_len,
long expire,
zval* opts,
char** old_val,
size_t* old_val_len,
zval* return_value) {
core_command_args_t args = {0};
args.glide_client = valkey_glide->glide_client;
args.cmd_type = Set;
args.key = key;
args.key_len = key_len;
args.raw_options = opts;
/* Add value argument */
args.args[0].type = CORE_ARG_TYPE_STRING;
args.args[0].data.string_arg.value = val;
args.args[0].data.string_arg.len = val_len;
args.arg_count = 1;
/* Parse options */
if (opts) {
if (parse_set_options(opts, &args.options) == 0) {
/* If parsing failed, return error */
return 0;
}
}
/* Set expire if provided */
if (expire > 0) {
args.options.expire_seconds = expire;
args.options.has_expire = 1;
}
/* Prepare result data for GET option */
struct set_result_data* result_data = emalloc(sizeof(struct set_result_data));
result_data->has_get = args.options.get_old_value;
return execute_core_command(valkey_glide, &args, result_data, process_set_result, return_value);
}
/* Execute a SETEX command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_setex_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *val = NULL;
size_t key_len, val_len;
zend_long expire;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osls", &object, ce, &key, &key_len, &expire, &val, &val_len) ==
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;
}
/* Call execute_set_command_internal with expire in seconds (EX) and no special options */
int result = execute_set_command_internal(
valkey_glide, key, key_len, val, val_len, expire, NULL, NULL, NULL, return_value);
if (result == 1) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
}
return 1;
} else {
return 0;
}
}
/* Execute a PSETEX command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_psetex_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *val = NULL;
size_t key_len, val_len;
zend_long expire;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Osls", &object, ce, &key, &key_len, &expire, &val, &val_len) ==
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;
}
/* Create options array for PX option */
zval options;
array_init(&options);
/* Add PX option with expire value */
add_assoc_long_ex(&options, "PX", sizeof("PX") - 1, expire);
/* Call execute_set_command_internal with the PX option */
int result = execute_set_command_internal(
valkey_glide, key, key_len, val, val_len, 0, &options, NULL, NULL, return_value);
/* Clean up options array */
zval_dtor(&options);
if (result == 1) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
}
return 1;
} else {
return 0;
}
}
/* Execute a SETNX command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_setnx_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
char * key = NULL, *val = NULL;
size_t key_len, val_len;
/* Parse parameters */
if (zend_parse_method_parameters(
argc, object, "Oss", &object, ce, &key, &key_len, &val, &val_len) == 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;
}
/* Create options array for NX option */
zval options;
array_init(&options);
/* Add NX option as a numeric index */
zval nx_option;
ZVAL_STRING(&nx_option, "NX");
add_next_index_zval(&options, &nx_option);
/* Call execute_set_command_internal with the NX option and no expiration */
int result = execute_set_command_internal(
valkey_glide, key, key_len, val, val_len, 0, &options, NULL, NULL, return_value);
/* Clean up options array */
zval_dtor(&options);
if (result == 1) {
if (valkey_glide->is_in_batch_mode) {
/* In batch mode, return $this for method chaining */
ZVAL_COPY(return_value, object);
}
return 1;
} else {
return 0;
}
}
/* Function to close a Valkey Glide client */
void close_glide_client(const void* glide_client) {
/* Check if client is valid */
if (!glide_client) {
return;
}
/* Close the client using the close_client function from glide_bindings.h */
close_client(glide_client);
}
/* Execute an ECHO command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_echo_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
zval* args = NULL;
int args_count = 0;
char* msg = NULL;
size_t msg_len = 0;
zend_bool is_cluster = (ce == get_valkey_glide_cluster_ce());
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide || !valkey_glide->glide_client) {
return 0;
}
if (is_cluster && valkey_glide->is_in_batch_mode) {
/* ECHO cannot be used in batch mode */
return 0;
}
/* Setup core command arguments */
core_command_args_t core_args = {0};
core_args.glide_client = valkey_glide->glide_client;
core_args.cmd_type = Echo;
core_args.is_cluster = is_cluster;
if (is_cluster) {
/* Parse parameters for cluster - first parameter is route, second is message */
if (zend_parse_method_parameters(argc, object, "O*", &object, ce, &args, &args_count) ==
FAILURE) {
return 0;
}
if (args_count < 2) {
/* Need both route and message parameters */
return 0;
}
/* Set up routing */
core_args.has_route = 1;
core_args.route_param = &args[0];
/* Get message parameter */
zval* message = &args[1];
if (Z_TYPE_P(message) == IS_STRING) {
msg = Z_STRVAL_P(message);
msg_len = Z_STRLEN_P(message);
} else {
/* Convert to string if needed */
convert_to_string(message);
msg = Z_STRVAL_P(message);
msg_len = Z_STRLEN_P(message);
}
} else {
/* Non-cluster case - parse message parameter only */
if (zend_parse_method_parameters(argc, object, "Os", &object, ce, &msg, &msg_len) ==
FAILURE) {
return 0;
}
}
/* Add message argument to core framework */
core_args.args[0].type = CORE_ARG_TYPE_STRING;
core_args.args[0].data.string_arg.value = msg;
core_args.args[0].data.string_arg.len = msg_len;
core_args.arg_count = 1;
/* Execute using unified core framework */
if (execute_core_command(
valkey_glide, &core_args, NULL, process_core_string_result, return_value)) {
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;
}
return 1;
}
return 0;
}
/* Execute a PING command using the Valkey Glide client - UNIFIED IMPLEMENTATION */
int execute_ping_command(zval* object, int argc, zval* return_value, zend_class_entry* ce) {
valkey_glide_object* valkey_glide;
zval* args = NULL;
int args_count = 0;
char* msg = NULL;
size_t msg_len = 0;
char* response = NULL;
size_t response_len = 0;
zend_bool is_cluster = (ce == get_valkey_glide_cluster_ce());
/* Get ValkeyGlide object */
valkey_glide = VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(valkey_glide_object, object);
if (!valkey_glide || !valkey_glide->glide_client) {
return 0;
}
if (is_cluster && valkey_glide->is_in_batch_mode) {
/* Ping cannot be used in batch mode */
return 0;
}
/* Setup core command arguments */
core_command_args_t core_args = {0};
core_args.glide_client = valkey_glide->glide_client;
core_args.cmd_type = Ping;
core_args.is_cluster = is_cluster;
if (is_cluster) {
/* Parse parameters for cluster - first parameter is route, optional second is message */
if (zend_parse_method_parameters(argc, object, "O*", &object, ce, &args, &args_count) ==
FAILURE) {
return 0;
}
if (args_count == 0) {
/* Need at least the route parameter */
return 0;
}
/* Set up routing */
core_args.has_route = 1;
core_args.route_param = &args[0];
/* Get optional message parameter */
if (args_count > 1) {
zval* message = &args[1];