forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_commands.c
4199 lines (3548 loc) · 131 KB
/
redis_commands.c
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
/* -*- Mode: C; tab-width: 4 -*- */
/*
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 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. |
+----------------------------------------------------------------------+
| Original Author: Michael Grunder <[email protected] |
| Maintainer: Nicolas Favre-Felix <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "redis_commands.h"
#include "php_network.h"
#ifndef PHP_WIN32
#include <netinet/tcp.h> /* TCP_KEEPALIVE */
#else
#include <winsock.h>
#endif
#include <zend_exceptions.h>
/* Georadius sort type */
typedef enum geoSortType {
SORT_NONE,
SORT_ASC,
SORT_DESC
} geoSortType;
/* Georadius store type */
typedef enum geoStoreType {
STORE_NONE,
STORE_COORD,
STORE_DIST
} geoStoreType;
/* Georadius options structure */
typedef struct geoOptions {
int withcoord;
int withdist;
int withhash;
long count;
geoSortType sort;
geoStoreType store;
zend_string *key;
} geoOptions;
/* Local passthrough macro for command construction. Given that these methods
* are generic (so they work whether the caller is Redis or RedisCluster) we
* will always have redis_sock, slot*, and */
#define REDIS_CMD_SPPRINTF(ret, kw, fmt, ...) \
redis_spprintf(redis_sock, slot, ret, kw, fmt, ##__VA_ARGS__)
/* Generic commands based on method signature and what kind of things we're
* processing. Lots of Redis commands take something like key, value, or
* key, value long. Each unique signature like this is written only once */
/* A command that takes no arguments */
int redis_empty_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
return SUCCESS;
}
/* Helper to construct a raw command. Given that the cluster and non cluster
* versions are different (RedisCluster needs an additional argument to direct
* the command) we take the start of our array and count */
int redis_build_raw_cmd(zval *z_args, int argc, char **cmd, int *cmd_len)
{
smart_string cmdstr = {0};
int i;
/* Make sure our first argument is a string */
if (Z_TYPE(z_args[0]) != IS_STRING) {
php_error_docref(NULL, E_WARNING,
"When sending a 'raw' command, the first argument must be a string!");
return FAILURE;
}
/* Initialize our command string */
redis_cmd_init_sstr(&cmdstr, argc-1, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
for (i = 1; i < argc; i++) {
switch (Z_TYPE(z_args[i])) {
case IS_STRING:
redis_cmd_append_sstr(&cmdstr, Z_STRVAL(z_args[i]),
Z_STRLEN(z_args[i]));
break;
case IS_LONG:
redis_cmd_append_sstr_long(&cmdstr,Z_LVAL(z_args[i]));
break;
case IS_DOUBLE:
redis_cmd_append_sstr_dbl(&cmdstr,Z_DVAL(z_args[i]));
break;
default:
php_error_docref(NULL, E_WARNING,
"Raw command arguments must be scalar values!");
efree(cmdstr.c);
return FAILURE;
}
}
/* Push command and length to caller */
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
smart_string *
redis_build_script_cmd(smart_string *cmd, int argc, zval *z_args)
{
int i;
zend_string *zstr;
if (Z_TYPE(z_args[0]) != IS_STRING) {
return NULL;
}
// Branch based on the directive
if (!strcasecmp(Z_STRVAL(z_args[0]), "flush") || !strcasecmp(Z_STRVAL(z_args[0]), "kill")) {
// Simple SCRIPT FLUSH, or SCRIPT_KILL command
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, Z_STRVAL(z_args[0]), Z_STRLEN(z_args[0]));
} else if (!strcasecmp(Z_STRVAL(z_args[0]), "load")) {
// Make sure we have a second argument, and it's not empty. If it is
// empty, we can just return an empty array (which is what Redis does)
if (argc < 2 || Z_TYPE(z_args[1]) != IS_STRING || Z_STRLEN(z_args[1]) < 1) {
return NULL;
}
// Format our SCRIPT LOAD command
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, "LOAD", sizeof("LOAD") - 1);
redis_cmd_append_sstr(cmd, Z_STRVAL(z_args[1]), Z_STRLEN(z_args[1]));
} else if (!strcasecmp(Z_STRVAL(z_args[0]), "exists")) {
// Make sure we have a second argument
if (argc < 2) {
return NULL;
}
/* Construct our SCRIPT EXISTS command */
REDIS_CMD_INIT_SSTR_STATIC(cmd, argc, "SCRIPT");
redis_cmd_append_sstr(cmd, "EXISTS", sizeof("EXISTS") - 1);
for (i = 1; i < argc; ++i) {
zstr = zval_get_string(&z_args[i]);
redis_cmd_append_sstr(cmd, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
zend_string_release(zstr);
}
} else {
/* Unknown directive */
return NULL;
}
return cmd;
}
/* Command that takes one optional string */
int redis_opt_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *arg = NULL;
size_t arglen;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &arg, &arglen) == FAILURE) {
return FAILURE;
}
if (arg != NULL) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", arg, arglen);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
}
return SUCCESS;
}
/* Generic command where we just take a string and do nothing to it*/
int redis_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *arg;
size_t arg_len;
// Parse args
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len)
==FAILURE)
{
return FAILURE;
}
// Build the command without molesting the string
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", arg, arg_len);
return SUCCESS;
}
/* Key, long, zval (serialized) */
int redis_key_long_val_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key = NULL;
size_t key_len;
zend_long expire;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slz", &key, &key_len,
&expire, &z_val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "klv", key, key_len, expire, z_val);
return SUCCESS;
}
/* Generic key, long, string (unserialized) */
int redis_key_long_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *val;
size_t key_len, val_len;
zend_long lval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sls", &key, &key_len,
&lval, &val, &val_len) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kds", key, key_len, (int)lval, val, val_len);
return SUCCESS;
}
/* Generic command construction when we just take a key and value */
int redis_kv_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
zval *z_val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &key, &key_len,
&z_val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kv", key, key_len, z_val);
return SUCCESS;
}
/* Generic command that takes a key and an unserialized value */
int redis_key_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *val;
size_t key_len, val_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &key, &key_len,
&val, &val_len) == FAILURE)
{
return FAILURE;
}
// Construct command
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ks", key, key_len, val, val_len);
return SUCCESS;
}
/* Key, string, string without serialization (ZCOUNT, ZREMRANGEBYSCORE) */
int redis_key_str_str_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *k, *v1, *v2;
size_t klen, v1len, v2len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss", &k, &klen,
&v1, &v1len, &v2, &v2len) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", k, klen, v1, v1len, v2, v2len);
// Success!
return SUCCESS;
}
/* Generic command that takes two keys */
int redis_key_key_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *k1, *k2;
size_t k1len, k2len;
int k1free, k2free;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &k1, &k1len,
&k2, &k2len) == FAILURE)
{
return FAILURE;
}
// Prefix both keys
k1free = redis_key_prefix(redis_sock, &k1, &k1len);
k2free = redis_key_prefix(redis_sock, &k2, &k2len);
// If a slot is requested, we can test that they hash the same
if (slot) {
// Slots where these keys resolve
short slot1 = cluster_hash_key(k1, k1len);
short slot2 = cluster_hash_key(k2, k2len);
// Check if Redis would give us a CROSSLOT error
if (slot1 != slot2) {
php_error_docref(0, E_WARNING, "Keys don't hash to the same slot");
if (k1free) efree(k1);
if (k2free) efree(k2);
return FAILURE;
}
// They're both the same
*slot = slot1;
}
/* Send keys as normal strings because we manually prefixed to check against
* cross slot error. */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ss", k1, k1len, k2, k2len);
/* Clean keys up if we prefixed */
if (k1free) efree(k1);
if (k2free) efree(k2);
return SUCCESS;
}
/* Generic command construction where we take a key and a long */
int redis_key_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t keylen;
zend_long lval;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &key, &keylen, &lval)
==FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kl", key, keylen, lval);
// Success!
return SUCCESS;
}
/* long, long */
int redis_long_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zend_long v1, v2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &v1, &v2)
== FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ll", v1, v2);
return SUCCESS;
}
/* key, long, long */
int redis_key_long_long_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
zend_long val1, val2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll", &key, &key_len,
&val1, &val2) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kll", key, key_len, val1, val2);
return SUCCESS;
}
/* Generic command where we take a single key */
int redis_key_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &key, &key_len)
==FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "k", key, key_len);
return SUCCESS;
}
int redis_flush_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot, void **ctx)
{
zend_bool async = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &async) == FAILURE) {
return FAILURE;
}
if (async) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "s", "ASYNC", sizeof("ASYNC") - 1);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "");
}
return SUCCESS;
}
/* Generic command where we take a key and a double */
int redis_key_dbl_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key;
size_t key_len;
double val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sd", &key, &key_len,
&val) == FAILURE)
{
return FAILURE;
}
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kf", key, key_len, val);
return SUCCESS;
}
/* Generic to construct SCAN and variant commands */
int redis_fmt_scan_cmd(char **cmd, REDIS_SCAN_TYPE type, char *key, int key_len,
long it, char *pat, int pat_len, long count)
{
static char *kw[] = {"SCAN","SSCAN","HSCAN","ZSCAN"};
int argc;
smart_string cmdstr = {0};
// Figure out our argument count
argc = 1 + (type!=TYPE_SCAN) + (pat_len>0?2:0) + (count>0?2:0);
redis_cmd_init_sstr(&cmdstr, argc, kw[type], strlen(kw[type]));
// Append our key if it's not a regular SCAN command
if (type != TYPE_SCAN) {
redis_cmd_append_sstr(&cmdstr, key, key_len);
}
// Append cursor
redis_cmd_append_sstr_long(&cmdstr, it);
// Append count if we've got one
if (count) {
redis_cmd_append_sstr(&cmdstr,"COUNT",sizeof("COUNT")-1);
redis_cmd_append_sstr_long(&cmdstr, count);
}
// Append pattern if we've got one
if (pat_len) {
redis_cmd_append_sstr(&cmdstr,"MATCH",sizeof("MATCH")-1);
redis_cmd_append_sstr(&cmdstr,pat,pat_len);
}
// Push command to the caller, return length
*cmd = cmdstr.c;
return cmdstr.len;
}
/* ZRANGEBYSCORE/ZREVRANGEBYSCORE */
#define IS_WITHSCORES_ARG(s, l) \
(l == sizeof("withscores") - 1 && !strncasecmp(s, "withscores", l))
#define IS_LIMIT_ARG(s, l) \
(l == sizeof("limit") - 1 && !strncasecmp(s,"limit", l))
/* ZRANGE/ZREVRANGE */
int redis_zrange_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, int *withscores,
short *slot, void **ctx)
{
char *key;
size_t key_len;
zend_long start, end;
zend_string *zkey;
zval *z_ws = NULL, *z_ele;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sll|z", &key, &key_len,
&start, &end, &z_ws) == FAILURE)
{
return FAILURE;
}
// Clear withscores arg
*withscores = 0;
/* Accept ['withscores' => true], or the legacy `true` value */
if (z_ws) {
if (Z_TYPE_P(z_ws) == IS_ARRAY) {
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(z_ws), zkey, z_ele) {
ZVAL_DEREF(z_ele);
if (IS_WITHSCORES_ARG(ZSTR_VAL(zkey), ZSTR_LEN(zkey))) {
*withscores = zval_is_true(z_ele);
break;
}
} ZEND_HASH_FOREACH_END();
} else if (Z_TYPE_P(z_ws) == IS_TRUE) {
*withscores = Z_TYPE_P(z_ws) == IS_TRUE;
}
}
if (*withscores) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kdds", key, key_len, start, end,
"WITHSCORES", sizeof("WITHSCORES") - 1);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kdd", key, key_len, start, end);
}
return SUCCESS;
}
int redis_zrangebyscore_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, int *withscores,
short *slot, void **ctx)
{
char *key, *start, *end;
int has_limit = 0;
long offset, count;
size_t key_len, start_len, end_len;
zval *z_opt=NULL, *z_ele;
zend_string *zkey;
zend_ulong idx;
HashTable *ht_opt;
PHPREDIS_NOTUSED(idx);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|a", &key, &key_len,
&start, &start_len, &end, &end_len, &z_opt)
==FAILURE)
{
return FAILURE;
}
// Check for an options array
if (z_opt && Z_TYPE_P(z_opt) == IS_ARRAY) {
ht_opt = Z_ARRVAL_P(z_opt);
ZEND_HASH_FOREACH_KEY_VAL(ht_opt, idx, zkey, z_ele) {
/* All options require a string key type */
if (!zkey) continue;
ZVAL_DEREF(z_ele);
/* Check for withscores and limit */
if (IS_WITHSCORES_ARG(ZSTR_VAL(zkey), ZSTR_LEN(zkey))) {
*withscores = zval_is_true(z_ele);
} else if (IS_LIMIT_ARG(ZSTR_VAL(zkey), ZSTR_LEN(zkey)) && Z_TYPE_P(z_ele) == IS_ARRAY) {
HashTable *htlimit = Z_ARRVAL_P(z_ele);
zval *zoff, *zcnt;
/* We need two arguments (offset and count) */
if ((zoff = zend_hash_index_find(htlimit, 0)) != NULL &&
(zcnt = zend_hash_index_find(htlimit, 1)) != NULL
) {
/* Set our limit if we can get valid longs from both args */
offset = zval_get_long(zoff);
count = zval_get_long(zcnt);
has_limit = 1;
}
}
} ZEND_HASH_FOREACH_END();
}
// Construct our command
if (*withscores) {
if (has_limit) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ksssdds", key, key_len,
start, start_len, end, end_len, "LIMIT", 5, offset, count,
"WITHSCORES", 10);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ksss", key, key_len, start,
start_len, end, end_len, "WITHSCORES", 10);
}
} else {
if (has_limit) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ksssdd", key, key_len, start,
start_len, end, end_len, "LIMIT", 5, offset, count);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", key, key_len, start,
start_len, end, end_len);
}
}
return SUCCESS;
}
/* ZUNIONSTORE, ZINTERSTORE */
int redis_zinter_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *agg_op=NULL;
int key_free, argc = 2, keys_count;
size_t key_len, agg_op_len = 0;
zval *z_keys, *z_weights=NULL, *z_ele;
HashTable *ht_keys, *ht_weights=NULL;
smart_string cmdstr = {0};
// Parse args
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa|a!s", &key,
&key_len, &z_keys, &z_weights, &agg_op,
&agg_op_len) == FAILURE)
{
return FAILURE;
}
// Grab our keys
ht_keys = Z_ARRVAL_P(z_keys);
// Nothing to do if there aren't any
if ((keys_count = zend_hash_num_elements(ht_keys)) == 0) {
return FAILURE;
} else {
argc += keys_count;
}
// Handle WEIGHTS
if (z_weights != NULL) {
ht_weights = Z_ARRVAL_P(z_weights);
if (zend_hash_num_elements(ht_weights) != keys_count) {
php_error_docref(NULL, E_WARNING,
"WEIGHTS and keys array should be the same size!");
return FAILURE;
}
// "WEIGHTS" + key count
argc += keys_count + 1;
}
// AGGREGATE option
if (agg_op_len != 0) {
if (strncasecmp(agg_op, "SUM", sizeof("SUM")) &&
strncasecmp(agg_op, "MIN", sizeof("MIN")) &&
strncasecmp(agg_op, "MAX", sizeof("MAX")))
{
php_error_docref(NULL, E_WARNING,
"Invalid AGGREGATE option provided!");
return FAILURE;
}
// "AGGREGATE" + type
argc += 2;
}
// Prefix key
key_free = redis_key_prefix(redis_sock, &key, &key_len);
// Start building our command
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
redis_cmd_append_sstr(&cmdstr, key, key_len);
redis_cmd_append_sstr_int(&cmdstr, keys_count);
// Set our slot, free the key if we prefixed it
CMD_SET_SLOT(slot,key,key_len);
if (key_free) efree(key);
// Process input keys
ZEND_HASH_FOREACH_VAL(ht_keys, z_ele) {
zend_string *zstr = zval_get_string(z_ele);
char *key = ZSTR_VAL(zstr);
size_t key_len = ZSTR_LEN(zstr);
// Prefix key if necissary
int key_free = redis_key_prefix(redis_sock, &key, &key_len);
// If we're in Cluster mode, verify the slot is the same
if (slot && *slot != cluster_hash_key(key,key_len)) {
php_error_docref(NULL, E_WARNING,
"All keys don't hash to the same slot!");
efree(cmdstr.c);
zend_string_release(zstr);
if (key_free) efree(key);
return FAILURE;
}
// Append this input set
redis_cmd_append_sstr(&cmdstr, key, key_len);
// Cleanup
zend_string_release(zstr);
if (key_free) efree(key);
} ZEND_HASH_FOREACH_END();
// Weights
if (ht_weights != NULL) {
redis_cmd_append_sstr(&cmdstr, "WEIGHTS", sizeof("WEIGHTS")-1);
// Process our weights
ZEND_HASH_FOREACH_VAL(ht_weights, z_ele) {
// Ignore non numeric args unless they're inf/-inf
ZVAL_DEREF(z_ele);
switch (Z_TYPE_P(z_ele)) {
case IS_LONG:
redis_cmd_append_sstr_long(&cmdstr, Z_LVAL_P(z_ele));
break;
case IS_DOUBLE:
redis_cmd_append_sstr_dbl(&cmdstr, Z_DVAL_P(z_ele));
break;
case IS_STRING: {
double dval;
zend_long lval;
zend_uchar type = is_numeric_string(Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele), &lval, &dval, 0);
if (type == IS_LONG) {
redis_cmd_append_sstr_long(&cmdstr, lval);
break;
} else if (type == IS_DOUBLE) {
redis_cmd_append_sstr_dbl(&cmdstr, dval);
break;
} else if (strncasecmp(Z_STRVAL_P(z_ele), "-inf", sizeof("-inf") - 1) == 0 ||
strncasecmp(Z_STRVAL_P(z_ele), "+inf", sizeof("+inf") - 1) == 0 ||
strncasecmp(Z_STRVAL_P(z_ele), "inf", sizeof("inf") - 1) == 0
) {
redis_cmd_append_sstr(&cmdstr, Z_STRVAL_P(z_ele), Z_STRLEN_P(z_ele));
break;
}
// fall through
}
default:
php_error_docref(NULL, E_WARNING,
"Weights must be numeric or '-inf','inf','+inf'");
efree(cmdstr.c);
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
}
// AGGREGATE
if (agg_op_len != 0) {
redis_cmd_append_sstr(&cmdstr, "AGGREGATE", sizeof("AGGREGATE")-1);
redis_cmd_append_sstr(&cmdstr, agg_op, agg_op_len);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
return SUCCESS;
}
/* SUBSCRIBE/PSUBSCRIBE */
int redis_subscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *z_arr, *z_chan;
HashTable *ht_chan;
smart_string cmdstr = {0};
subscribeContext *sctx = ecalloc(1, sizeof(*sctx));
size_t key_len;
int key_free;
char *key;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "af", &z_arr,
&(sctx->cb), &(sctx->cb_cache)) == FAILURE)
{
efree(sctx);
return FAILURE;
}
ht_chan = Z_ARRVAL_P(z_arr);
sctx->kw = kw;
sctx->argc = zend_hash_num_elements(ht_chan);
if (sctx->argc == 0) {
efree(sctx);
return FAILURE;
}
// Start command construction
redis_cmd_init_sstr(&cmdstr, sctx->argc, kw, strlen(kw));
// Iterate over channels
ZEND_HASH_FOREACH_VAL(ht_chan, z_chan) {
// We want to deal with strings here
zend_string *zstr = zval_get_string(z_chan);
// Grab channel name, prefix if required
key = ZSTR_VAL(zstr);
key_len = ZSTR_LEN(zstr);
key_free = redis_key_prefix(redis_sock, &key, &key_len);
// Add this channel
redis_cmd_append_sstr(&cmdstr, key, key_len);
zend_string_release(zstr);
// Free our key if it was prefixed
if (key_free) efree(key);
} ZEND_HASH_FOREACH_END();
// Push values out
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
*ctx = (void*)sctx;
// Pick a slot at random
CMD_RAND_SLOT(slot);
return SUCCESS;
}
/* UNSUBSCRIBE/PUNSUBSCRIBE */
int redis_unsubscribe_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *z_arr, *z_chan;
HashTable *ht_arr;
smart_string cmdstr = {0};
subscribeContext *sctx = ecalloc(1, sizeof(*sctx));
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &z_arr) == FAILURE) {
efree(sctx);
return FAILURE;
}
ht_arr = Z_ARRVAL_P(z_arr);
sctx->argc = zend_hash_num_elements(ht_arr);
if (sctx->argc == 0) {
efree(sctx);
return FAILURE;
}
redis_cmd_init_sstr(&cmdstr, sctx->argc, kw, strlen(kw));
ZEND_HASH_FOREACH_VAL(ht_arr, z_chan) {
char *key = Z_STRVAL_P(z_chan);
size_t key_len = Z_STRLEN_P(z_chan);
int key_free;
key_free = redis_key_prefix(redis_sock, &key, &key_len);
redis_cmd_append_sstr(&cmdstr, key, key_len);
if (key_free) efree(key);
} ZEND_HASH_FOREACH_END();
// Push out vals
*cmd_len = cmdstr.len;
*cmd = cmdstr.c;
*ctx = (void*)sctx;
return SUCCESS;
}
/* ZRANGEBYLEX/ZREVRANGEBYLEX */
int redis_zrangebylex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *min, *max;
size_t key_len, min_len, max_len;
int argc = ZEND_NUM_ARGS();
zend_long offset, count;
/* We need either 3 or 5 arguments for this to be valid */
if (argc != 3 && argc != 5) {
php_error_docref(0, E_WARNING, "Must pass either 3 or 5 arguments");
return FAILURE;
}
if (zend_parse_parameters(argc, "sss|ll", &key, &key_len, &min, &min_len,
&max, &max_len, &offset, &count) == FAILURE)
{
return FAILURE;
}
/* min and max must start with '(' or '[', or be either '-' or '+' */
if (min_len < 1 || max_len < 1 ||
(min[0] != '(' && min[0] != '[' &&
(min[0] != '-' || min_len > 1) && (min[0] != '+' || min_len > 1)) ||
(max[0] != '(' && max[0] != '[' &&
(max[0] != '-' || max_len > 1) && (max[0] != '+' || max_len > 1)))
{
php_error_docref(0, E_WARNING,
"min and max arguments must start with '[' or '('");
return FAILURE;
}
/* Construct command */
if (argc == 3) {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", key, key_len, min, min_len,
max, max_len);
} else {
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "ksssll", key, key_len, min, min_len,
max, max_len, "LIMIT", 5, offset, count);
}
return SUCCESS;
}
/* Validate ZLEX* min/max argument strings */
static int validate_zlex_arg(const char *arg, size_t len) {
return (len > 1 && (*arg == '[' || *arg == '(')) ||
(len == 1 && (*arg == '+' || *arg == '-'));
}
/* ZLEXCOUNT/ZREMRANGEBYLEX */
int redis_gen_zlex_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
char *key, *min, *max;
size_t key_len, min_len, max_len;
/* Parse args */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss", &key, &key_len,
&min, &min_len, &max, &max_len) == FAILURE)
{
return FAILURE;
}
/* Quick sanity check on min/max */
if (!validate_zlex_arg(min, min_len) || !validate_zlex_arg(max, max_len)) {
php_error_docref(NULL, E_WARNING,
"Min/Max args can be '-' or '+', or start with '[' or '('");
return FAILURE;
}
/* Construct command */
*cmd_len = REDIS_CMD_SPPRINTF(cmd, kw, "kss", key, key_len, min, min_len,
max, max_len);
return SUCCESS;
}
/* EVAL and EVALSHA */
int redis_eval_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, char *kw,
char **cmd, int *cmd_len, short *slot, void **ctx)
{
char *lua;
int argc = 0;
zval *z_arr = NULL, *z_ele;
HashTable *ht_arr;
zend_long num_keys = 0;
smart_string cmdstr = {0};
size_t lua_len;
zend_string *zstr;
short prevslot = -1;
/* Parse args */
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|al", &lua, &lua_len,
&z_arr, &num_keys) == FAILURE)
{
return FAILURE;
}
/* Grab arg count */
if (z_arr != NULL) {
ht_arr = Z_ARRVAL_P(z_arr);
argc = zend_hash_num_elements(ht_arr);
}
/* EVAL[SHA] {script || sha1} {num keys} */