forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_commands.c
3233 lines (2695 loc) · 99 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 -*- */
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| 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 <zend_exceptions.h>
/* 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_format_static(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 TSRMLS_DC)
{
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 TSRMLS_CC, 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 TSRMLS_CC, 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;
}
/* 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;
strlen_t arg_len;
// Parse args
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len)
==FAILURE)
{
return FAILURE;
}
// Build the command without molesting the string
*cmd_len = redis_cmd_format_static(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, *val=NULL;
int val_len, val_free, key_free;
strlen_t key_len;
zend_long expire;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &key, &key_len,
&expire, &z_val)==FAILURE)
{
return FAILURE;
}
// Serialize value, prefix key
val_free = redis_serialize(redis_sock, z_val, &val, &val_len TSRMLS_CC);
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "sls", key, key_len, expire,
val, val_len);
// Set the slot if directed
CMD_SET_SLOT(slot,key,key_len);
if(val_free) efree(val);
if(key_free) efree(key);
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;
strlen_t key_len, val_len;
int key_free;
zend_long lval;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sls", &key, &key_len,
&lval, &val, &val_len)==FAILURE)
{
return FAILURE;
}
// Prefix our key if requested
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct command
*cmd_len = redis_cmd_format_static(cmd, kw, "sds", key, key_len, (int)lval,
val, val_len);
// Set slot
CMD_SET_SLOT(slot,key,key_len);
// Free our key if we prefixed
if(key_free) efree(key);
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, *val;
int key_free, val_free;
strlen_t key_len, val_len;
zval *z_val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len,
&z_val)==FAILURE)
{
return FAILURE;
}
val_free = redis_serialize(redis_sock, z_val, &val, (int *)&val_len TSRMLS_CC);
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "ss", key, key_len, val,
val_len);
// Set our slot if directed
CMD_SET_SLOT(slot,key,key_len);
if(val_free) efree(val);
if(key_free) efree(key);
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;
strlen_t key_len, val_len;
int key_free;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len,
&val, &val_len)==FAILURE)
{
return FAILURE;
}
// Prefix key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct command
*cmd_len = redis_cmd_format_static(cmd, kw, "ss", key, key_len, val,
val_len);
// Set slot if directed
CMD_SET_SLOT(slot,key,key_len);
if (key_free) efree(key);
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 *key, *val1, *val2;
strlen_t key_len, val1_len, val2_len;
int key_free;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &key, &key_len,
&val1, &val1_len, &val2, &val2_len)==FAILURE)
{
return FAILURE;
}
// Prefix key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct command
*cmd_len = redis_cmd_format_static(cmd, kw, "sss", key, key_len, val1,
val1_len, val2, val2_len);
// Set slot
CMD_SET_SLOT(slot,key,key_len);
// Free key if prefixed
if(key_free) efree(key);
// 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 *key1, *key2;
strlen_t key1_len, key2_len;
int key1_free, key2_free;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key1, &key1_len,
&key2, &key2_len)==FAILURE)
{
return FAILURE;
}
// Prefix both keys
key1_free = redis_key_prefix(redis_sock, &key1, (int *)&key1_len);
key2_free = redis_key_prefix(redis_sock, &key2, (int *)&key2_len);
// 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(key1, key1_len);
short slot2 = cluster_hash_key(key2, key2_len);
// Check if Redis would give us a CROSSLOT error
if(slot1 != slot2) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Keys don't hash to the same slot");
if(key1_free) efree(key1);
if(key2_free) efree(key2);
return FAILURE;
}
// They're both the same
*slot = slot1;
}
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "ss", key1, key1_len, key2,
key2_len);
if (key1_free) efree(key1);
if (key2_free) efree(key2);
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;
int key_free;
strlen_t key_len;
zend_long lval;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &key, &key_len,
&lval)==FAILURE)
{
return FAILURE;
}
// Prefix key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Disallow zero length keys (for now)
if(key_len == 0) {
if(key_free) efree(key);
return FAILURE;
}
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "sl", key, key_len, lval);
// Set slot if directed
CMD_SET_SLOT(slot, key, key_len);
if (key_free) efree(key);
// Success!
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;
int key_free;
strlen_t key_len;
zend_long val1, val2;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll", &key, &key_len,
&val1, &val2)==FAILURE)
{
return FAILURE;
}
// Prefix our key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct command
*cmd_len = redis_cmd_format_static(cmd, kw, "sll", key, key_len, val1,
val2);
// Set slot
CMD_SET_SLOT(slot,key,key_len);
if(key_free) efree(key);
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;
strlen_t key_len;
int key_free;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &key, &key_len)
==FAILURE)
{
return FAILURE;
}
// Prefix our key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "s", key, key_len);
// Set slot if directed
CMD_SET_SLOT(slot,key,key_len);
if(key_free) efree(key);
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;
strlen_t key_len;
int key_free;
double val;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd", &key, &key_len,
&val)==FAILURE)
{
return FAILURE;
}
// Prefix our key
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
// Construct our command
*cmd_len = redis_cmd_format_static(cmd, kw, "sf", key, key_len, val);
// Set slot if directed
CMD_SET_SLOT(slot,key,key_len);
if(key_free) efree(key);
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;
}
/* 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;
int key_free;
strlen_t key_len;
zend_long start, end;
zend_bool ws=0;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sll|b", &key, &key_len,
&start, &end, &ws)==FAILURE)
{
return FAILURE;
}
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
if(ws) {
*cmd_len = redis_cmd_format_static(cmd, kw, "sdds", key, key_len, start,
end, "WITHSCORES", sizeof("WITHSCORES")-1);
} else {
*cmd_len = redis_cmd_format_static(cmd, kw, "sdd", key, key_len, start,
end);
}
CMD_SET_SLOT(slot, key, key_len);
// Free key, push out WITHSCORES option
if(key_free) efree(key);
*withscores = ws;
return SUCCESS;
}
/* 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))
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 key_free, has_limit=0;
long offset, count;
strlen_t key_len, start_len, end_len;
zval *z_opt=NULL, *z_ele;
zend_string *zkey;
ulong idx;
HashTable *ht_opt;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "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;
/* Check for withscores and limit */
if (IS_WITHSCORES_ARG(zkey->val, zkey->len)) {
*withscores = zval_is_true(z_ele);
} else if (IS_LIMIT_ARG(zkey->val, zkey->len) && 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();
}
// Prefix our key, set slot
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
CMD_SET_SLOT(slot,key,key_len);
// Construct our command
if(*withscores) {
if(has_limit) {
*cmd_len = redis_cmd_format_static(cmd, kw, "ssssdds", key, key_len,
start, start_len, end, end_len, "LIMIT", 5, offset,
count, "WITHSCORES", 10);
} else {
*cmd_len = redis_cmd_format_static(cmd, kw, "ssss", key, key_len,
start, start_len, end, end_len, "WITHSCORES", 10);
}
} else {
if(has_limit) {
*cmd_len = redis_cmd_format_static(cmd, kw, "ssssdd", key, key_len,
start, start_len, end, end_len, "LIMIT", 5, offset, count);
} else {
*cmd_len = redis_cmd_format_static(cmd, kw, "sss", key, key_len,
start, start_len, end, end_len);
}
}
// Free our key if we prefixed
if(key_free) efree(key);
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;
strlen_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() TSRMLS_CC, "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 TSRMLS_CC, 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 TSRMLS_CC, E_WARNING,
"Invalid AGGREGATE option provided!");
return FAILURE;
}
// "AGGREGATE" + type
argc += 2;
}
// Prefix key
key_free = redis_key_prefix(redis_sock, &key, (int *)&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;
int key_len = zstr->len;
// 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 TSRMLS_CC, 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
if (Z_TYPE_P(z_ele) != IS_LONG && Z_TYPE_P(z_ele) != IS_DOUBLE &&
strncasecmp(Z_STRVAL_P(z_ele), "inf", sizeof("inf")) != 0 &&
strncasecmp(Z_STRVAL_P(z_ele), "-inf", sizeof("-inf")) != 0 &&
strncasecmp(Z_STRVAL_P(z_ele), "+inf", sizeof("+inf")) != 0
) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Weights must be numeric or '-inf','inf','+inf'");
efree(cmdstr.c);
return FAILURE;
}
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:
redis_cmd_append_sstr(&cmdstr, Z_STRVAL_P(z_ele),
Z_STRLEN_P(z_ele));
break;
}
} 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 = emalloc(sizeof(subscribeContext));
int key_len, key_free;
char *key;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "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;
key_len = zstr->len;
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 = emalloc(sizeof(subscribeContext));
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "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);
int key_len = Z_STRLEN_P(z_chan), 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;
strlen_t key_len, min_len, max_len;
int key_free, 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 TSRMLS_CC, E_WARNING,
"Must pass either 3 or 5 arguments");
return FAILURE;
}
if(zend_parse_parameters(argc TSRMLS_CC, "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 TSRMLS_CC, E_WARNING,
"min and max arguments must start with '[' or '('");
return FAILURE;
}
/* Prefix key */
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
/* Construct command */
if(argc == 3) {
*cmd_len = redis_cmd_format_static(cmd, kw, "sss", key, key_len, min,
min_len, max, max_len);
} else {
*cmd_len = redis_cmd_format_static(cmd, kw, "ssssll", key, key_len, min,
min_len, max, max_len, "LIMIT", sizeof("LIMIT")-1, offset, count);
}
/* Pick our slot */
CMD_SET_SLOT(slot,key,key_len);
/* Free key if we prefixed */
if(key_free) efree(key);
return SUCCESS;
}
/* 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;
strlen_t key_len, min_len, max_len;
int key_free;
/* Parse args */
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &key, &key_len,
&min, &min_len, &max, &max_len)==FAILURE)
{
return FAILURE;
}
/* Quick sanity check on min/max */
if(min_len<1 || max_len<1 || (min[0]!='(' && min[0]!='[') ||
(max[0]!='(' && max[0]!='['))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Min and Max arguments must begin with '(' or '['");
return FAILURE;
}
/* Prefix key if we need to */
key_free = redis_key_prefix(redis_sock, &key, (int *)&key_len);
/* Construct command */
*cmd_len = redis_cmd_format_static(cmd, kw, "sss", key, key_len, min,
min_len, max, max_len);
/* set slot */
CMD_SET_SLOT(slot,key,key_len);
/* Free key if prefixed */
if(key_free) efree(key);
return SUCCESS;
}
/* Commands that take a key followed by a variable list of serializable
* values (RPUSH, LPUSH, SADD, SREM, etc...) */
int redis_key_varval_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,
void **ctx)
{
zval *z_args;
smart_string cmdstr = {0};
char *arg;
int arg_free, arg_len, i;
int argc = ZEND_NUM_ARGS();
// We at least need a key and one value
if(argc < 2) {
return FAILURE;
}
// Make sure we at least have a key, and we can get other args
z_args = emalloc(argc * sizeof(zval));
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
return FAILURE;
}
// Grab the first argument (our key) as a string
zend_string *zstr = zval_get_string(&z_args[0]);
arg = zstr->val;
arg_len = zstr->len;
// Prefix if required
arg_free = redis_key_prefix(redis_sock, &arg, &arg_len);
// Start command construction
redis_cmd_init_sstr(&cmdstr, argc, kw, strlen(kw));
redis_cmd_append_sstr(&cmdstr, arg, arg_len);
// Set our slot, free key prefix if we prefixed it
CMD_SET_SLOT(slot,arg,arg_len);
zend_string_release(zstr);
if(arg_free) efree(arg);
// Add our members
for(i=1;i<argc;i++) {
arg_free = redis_serialize(redis_sock, &z_args[i], &arg, &arg_len TSRMLS_CC);
redis_cmd_append_sstr(&cmdstr, arg, arg_len);
if(arg_free) efree(arg);
}
// Push out values
*cmd = cmdstr.c;
*cmd_len = cmdstr.len;
// Cleanup arg array
efree(z_args);
// Success!
return SUCCESS;
}
/* Commands that take a key and then an array of values */
int redis_key_arr_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
char *kw, char **cmd, int *cmd_len, short *slot,