forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_array.c
1429 lines (1181 loc) · 40.9 KB
/
redis_array.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
/*
+----------------------------------------------------------------------+
| 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. |
+----------------------------------------------------------------------+
| Author: Nicolas Favre-Felix <[email protected]> |
| Maintainer: Michael Grunder <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "common.h"
#include "ext/standard/info.h"
#include "php_ini.h"
#include "php_redis.h"
#include <zend_exceptions.h>
#include "library.h"
#include "redis_array.h"
#include "redis_array_impl.h"
/* Simple macro to detect failure in a RedisArray call */
#define RA_CALL_FAILED(rv, cmd) ( \
PHPREDIS_ZVAL_IS_STRICT_FALSE(rv) || \
(Z_TYPE_P(rv) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(rv)) == 0) || \
(Z_TYPE_P(rv) == IS_LONG && Z_LVAL_P(rv) == 0 && !strcasecmp(cmd, "TYPE")) \
)
extern zend_class_entry *redis_ce;
zend_class_entry *redis_array_ce;
ZEND_BEGIN_ARG_INFO_EX(arginfo_ctor, 0, 0, 1)
ZEND_ARG_INFO(0, name_or_hosts)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_call, 0, 0, 2)
ZEND_ARG_INFO(0, function_name)
ZEND_ARG_INFO(0, arguments)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_target, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_instance, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_rehash, 0, 0, 0)
ZEND_ARG_INFO(0, callable)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_select, 0, 0, 1)
ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mget, 0, 0, 1)
ZEND_ARG_INFO(0, keys)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mset, 0, 0, 1)
ZEND_ARG_INFO(0, pairs)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_del, 0, 0, 1)
ZEND_ARG_INFO(0, keys)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_getopt, 0, 0, 1)
ZEND_ARG_INFO(0, opt)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_setopt, 0, 0, 2)
ZEND_ARG_INFO(0, opt)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_keys, 0, 0, 1)
ZEND_ARG_INFO(0, pattern)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_multi, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
zend_function_entry redis_array_functions[] = {
PHP_ME(RedisArray, __call, arginfo_call, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, __construct, arginfo_ctor, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _distributor, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _function, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _hosts, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _instance, arginfo_instance, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _rehash, arginfo_rehash, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, _target, arginfo_target, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, bgsave, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, del, arginfo_del, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, discard, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, exec, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, flushall, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, flushdb, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, getOption, arginfo_getopt, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, info, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, keys, arginfo_keys, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, mget, arginfo_mget, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, mset, arginfo_mset, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, multi, arginfo_multi, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, ping, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, save, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, select, arginfo_select, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, setOption,arginfo_setopt, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, unlink, arginfo_void, ZEND_ACC_PUBLIC)
PHP_ME(RedisArray, unwatch, arginfo_void, ZEND_ACC_PUBLIC)
PHP_MALIAS(RedisArray, delete, del, arginfo_del, ZEND_ACC_PUBLIC)
PHP_MALIAS(RedisArray, getMultiple, mget, arginfo_mget, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static void
redis_array_free(RedisArray *ra)
{
int i;
/* Redis objects */
for(i = 0; i< ra->count; i++) {
zval_dtor(&ra->redis[i]);
efree(ra->hosts[i]);
}
efree(ra->redis);
efree(ra->hosts);
/* delete hash function */
zval_dtor(&ra->z_fun);
/* Distributor */
zval_dtor(&ra->z_dist);
/* Delete pur commands */
zend_hash_destroy(ra->pure_cmds);
FREE_HASHTABLE(ra->pure_cmds);
/* Free structure itself */
efree(ra);
}
#if (PHP_MAJOR_VERSION < 7)
typedef struct {
zend_object std;
RedisArray *ra;
} redis_array_object;
void
free_redis_array_object(void *object TSRMLS_DC)
{
redis_array_object *obj = (redis_array_object *)object;
zend_object_std_dtor(&obj->std TSRMLS_CC);
if (obj->ra) {
if (obj->ra->prev) redis_array_free(obj->ra->prev);
redis_array_free(obj->ra);
}
efree(obj);
}
zend_object_value
create_redis_array_object(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
redis_array_object *obj = ecalloc(1, sizeof(redis_array_object));
memset(obj, 0, sizeof(redis_array_object));
zend_object_std_init(&obj->std, ce TSRMLS_CC);
#if PHP_VERSION_ID < 50399
zval *tmp;
zend_hash_copy(obj->std.properties, &ce->default_properties,
(copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
object_properties_init(&obj->std, ce);
#endif
retval.handle = zend_objects_store_put(obj,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
(zend_objects_free_object_storage_t)free_redis_array_object,
NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
}
#else
typedef struct {
RedisArray *ra;
zend_object std;
} redis_array_object;
zend_object_handlers redis_array_object_handlers;
void
free_redis_array_object(zend_object *object)
{
redis_array_object *obj = (redis_array_object *)((char *)(object) - XtOffsetOf(redis_array_object, std));
if (obj->ra) {
if (obj->ra->prev) redis_array_free(obj->ra->prev);
redis_array_free(obj->ra);
}
zend_object_std_dtor(&obj->std TSRMLS_CC);
}
zend_object *
create_redis_array_object(zend_class_entry *ce TSRMLS_DC)
{
redis_array_object *obj = ecalloc(1, sizeof(redis_array_object) + zend_object_properties_size(ce));
obj->ra = NULL;
zend_object_std_init(&obj->std, ce TSRMLS_CC);
object_properties_init(&obj->std, ce);
memcpy(&redis_array_object_handlers, zend_get_std_object_handlers(), sizeof(redis_array_object_handlers));
redis_array_object_handlers.offset = XtOffsetOf(redis_array_object, std);
redis_array_object_handlers.free_obj = free_redis_array_object;
obj->std.handlers = &redis_array_object_handlers;
return &obj->std;
}
#endif
/**
* redis_array_get
*/
PHP_REDIS_API RedisArray *
redis_array_get(zval *id TSRMLS_DC)
{
redis_array_object *obj;
if (Z_TYPE_P(id) == IS_OBJECT) {
obj = PHPREDIS_GET_OBJECT(redis_array_object, id);
return obj->ra;
}
return NULL;
}
/* {{{ proto RedisArray RedisArray::__construct()
Public constructor */
PHP_METHOD(RedisArray, __construct)
{
zval *z0, z_fun, z_dist, *zpData, *z_opts = NULL;
RedisArray *ra = NULL;
zend_bool b_index = 0, b_autorehash = 0, b_pconnect = 0;
HashTable *hPrev = NULL, *hOpts = NULL;
long l_retry_interval = 0;
zend_bool b_lazy_connect = 0;
double d_connect_timeout = 0, read_timeout = 0.0;
redis_array_object *obj;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|a", &z0, &z_opts) == FAILURE) {
RETURN_FALSE;
}
ZVAL_NULL(&z_fun);
ZVAL_NULL(&z_dist);
/* extract options */
if(z_opts) {
hOpts = Z_ARRVAL_P(z_opts);
/* extract previous ring. */
if ((zpData = zend_hash_str_find(hOpts, "previous", sizeof("previous") - 1)) != NULL && Z_TYPE_P(zpData) == IS_ARRAY
&& zend_hash_num_elements(Z_ARRVAL_P(zpData)) != 0
) {
/* consider previous array as non-existent if empty. */
hPrev = Z_ARRVAL_P(zpData);
}
/* extract function name. */
if ((zpData = zend_hash_str_find(hOpts, "function", sizeof("function") - 1)) != NULL) {
ZVAL_ZVAL(&z_fun, zpData, 1, 0);
}
/* extract function name. */
if ((zpData = zend_hash_str_find(hOpts, "distributor", sizeof("distributor") - 1)) != NULL) {
ZVAL_ZVAL(&z_dist, zpData, 1, 0);
}
/* extract index option. */
if ((zpData = zend_hash_str_find(hOpts, "index", sizeof("index") - 1)) != NULL) {
b_index = zval_is_true(zpData);
}
/* extract autorehash option. */
if ((zpData = zend_hash_str_find(hOpts, "autorehash", sizeof("autorehash") - 1)) != NULL) {
b_autorehash = zval_is_true(zpData);
}
/* pconnect */
if ((zpData = zend_hash_str_find(hOpts, "pconnect", sizeof("pconnect") - 1)) != NULL) {
b_pconnect = zval_is_true(zpData);
}
/* extract retry_interval option. */
if ((zpData = zend_hash_str_find(hOpts, "retry_interval", sizeof("retry_interval") - 1)) != NULL) {
if (Z_TYPE_P(zpData) == IS_LONG) {
l_retry_interval = Z_LVAL_P(zpData);
} else if (Z_TYPE_P(zpData) == IS_STRING) {
l_retry_interval = atol(Z_STRVAL_P(zpData));
}
}
/* extract lazy connect option. */
if ((zpData = zend_hash_str_find(hOpts, "lazy_connect", sizeof("lazy_connect") - 1)) != NULL) {
b_lazy_connect = zval_is_true(zpData);
}
/* extract connect_timeout option */
if ((zpData = zend_hash_str_find(hOpts, "connect_timeout", sizeof("connect_timeout") - 1)) != NULL) {
if (Z_TYPE_P(zpData) == IS_DOUBLE) {
d_connect_timeout = Z_DVAL_P(zpData);
} else if (Z_TYPE_P(zpData) == IS_LONG) {
d_connect_timeout = Z_LVAL_P(zpData);
} else if (Z_TYPE_P(zpData) == IS_STRING) {
d_connect_timeout = atof(Z_STRVAL_P(zpData));
}
}
/* extract read_timeout option */
if ((zpData = zend_hash_str_find(hOpts, "read_timeout", sizeof("read_timeout") - 1)) != NULL) {
if (Z_TYPE_P(zpData) == IS_DOUBLE) {
read_timeout = Z_DVAL_P(zpData);
} else if (Z_TYPE_P(zpData) == IS_LONG) {
read_timeout = Z_LVAL_P(zpData);
} else if (Z_TYPE_P(zpData) == IS_STRING) {
read_timeout = atof(Z_STRVAL_P(zpData));
}
}
}
/* extract either name of list of hosts from z0 */
switch(Z_TYPE_P(z0)) {
case IS_STRING:
ra = ra_load_array(Z_STRVAL_P(z0) TSRMLS_CC);
break;
case IS_ARRAY:
ra = ra_make_array(Z_ARRVAL_P(z0), &z_fun, &z_dist, hPrev, b_index, b_pconnect, l_retry_interval, b_lazy_connect, d_connect_timeout, read_timeout TSRMLS_CC);
break;
default:
WRONG_PARAM_COUNT;
}
zval_dtor(&z_dist);
zval_dtor(&z_fun);
if(ra) {
ra->auto_rehash = b_autorehash;
ra->connect_timeout = d_connect_timeout;
if(ra->prev) ra->prev->auto_rehash = b_autorehash;
obj = PHPREDIS_GET_OBJECT(redis_array_object, getThis());
obj->ra = ra;
}
}
static void
ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, int cmd_len, zval *z_args, zval *z_new_target) {
zval z_fun, *redis_inst, *z_callargs, *zp_tmp;
char *key = NULL; /* set to avoid "unused-but-set-variable" */
int i, key_len = 0, argc;
HashTable *h_args;
zend_bool b_write_cmd = 0;
h_args = Z_ARRVAL_P(z_args);
if ((argc = zend_hash_num_elements(h_args)) == 0) {
RETURN_FALSE;
}
if(ra->z_multi_exec) {
redis_inst = ra->z_multi_exec; /* we already have the instance */
} else {
/* extract key and hash it. */
if ((zp_tmp = zend_hash_index_find(h_args, 0)) == NULL || Z_TYPE_P(zp_tmp) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not find key");
RETURN_FALSE;
}
key = Z_STRVAL_P(zp_tmp);
key_len = Z_STRLEN_P(zp_tmp);
/* find node */
redis_inst = ra_find_node(ra, key, key_len, NULL TSRMLS_CC);
if(!redis_inst) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not find any redis servers for this key.");
RETURN_FALSE;
}
}
/* pass call through */
ZVAL_STRINGL(&z_fun, cmd, cmd_len); /* method name */
z_callargs = ecalloc(argc, sizeof(zval));
/* copy args to array */
i = 0;
ZEND_HASH_FOREACH_VAL(h_args, zp_tmp) {
ZVAL_ZVAL(&z_callargs[i], zp_tmp, 1, 0);
i++;
} ZEND_HASH_FOREACH_END();
/* multi/exec */
if(ra->z_multi_exec) {
call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
zval_dtor(return_value);
zval_dtor(&z_fun);
for (i = 0; i < argc; ++i) {
zval_dtor(&z_callargs[i]);
}
efree(z_callargs);
RETURN_ZVAL(getThis(), 1, 0);
}
/* check if write cmd */
b_write_cmd = ra_is_write_cmd(ra, cmd, cmd_len);
/* CALL! */
if(ra->index && b_write_cmd) {
/* add MULTI + SADD */
ra_index_multi(redis_inst, MULTI TSRMLS_CC);
/* call using discarded temp value and extract exec results after. */
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
zval_dtor(return_value);
/* add keys to index. */
ra_index_key(key, key_len, redis_inst TSRMLS_CC);
/* call EXEC */
ra_index_exec(redis_inst, return_value, 0 TSRMLS_CC);
} else { /* call directly through. */
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
if (!b_write_cmd) {
/* check if we have an error. */
if (ra->prev && RA_CALL_FAILED(return_value, cmd)) { /* there was an error reading, try with prev ring. */
/* Free previous return value */
zval_dtor(return_value);
/* ERROR, FALLBACK TO PREVIOUS RING and forward a reference to the first redis instance we were looking at. */
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra->prev, cmd, cmd_len, z_args, z_new_target ? z_new_target : redis_inst);
}
/* Autorehash if the key was found on the previous node if this is a read command and auto rehashing is on */
if (ra->auto_rehash && z_new_target && !RA_CALL_FAILED(return_value, cmd)) { /* move key from old ring to new ring */
ra_move_key(key, key_len, redis_inst, z_new_target TSRMLS_CC);
}
}
}
/* cleanup */
zval_dtor(&z_fun);
for (i = 0; i < argc; ++i) {
zval_dtor(&z_callargs[i]);
}
efree(z_callargs);
}
PHP_METHOD(RedisArray, __call)
{
zval *object;
RedisArray *ra;
zval *z_args;
char *cmd;
strlen_t cmd_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osa",
&object, redis_array_ce, &cmd, &cmd_len, &z_args) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmd_len, z_args, NULL);
}
PHP_METHOD(RedisArray, _hosts)
{
zval *object;
int i;
RedisArray *ra;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
&object, redis_array_ce) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
array_init(return_value);
for(i = 0; i < ra->count; ++i) {
add_next_index_string(return_value, ra->hosts[i]);
}
}
PHP_METHOD(RedisArray, _target)
{
zval *object;
RedisArray *ra;
char *key;
strlen_t key_len;
zval *redis_inst;
int i;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, redis_array_ce, &key, &key_len) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
redis_inst = ra_find_node(ra, key, key_len, &i TSRMLS_CC);
if(redis_inst) {
RETURN_STRING(ra->hosts[i]);
} else {
RETURN_NULL();
}
}
PHP_METHOD(RedisArray, _instance)
{
zval *object;
RedisArray *ra;
char *target;
strlen_t target_len;
zval *z_redis;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, redis_array_ce, &target, &target_len) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
z_redis = ra_find_node_by_name(ra, target, target_len TSRMLS_CC);
if(z_redis) {
RETURN_ZVAL(z_redis, 1, 0);
} else {
RETURN_NULL();
}
}
PHP_METHOD(RedisArray, _function)
{
zval *object, *z_fun;
RedisArray *ra;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
&object, redis_array_ce) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
z_fun = &ra->z_fun;
RETURN_ZVAL(z_fun, 1, 0);
}
PHP_METHOD(RedisArray, _distributor)
{
zval *object, *z_dist;
RedisArray *ra;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
&object, redis_array_ce) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
z_dist = &ra->z_dist;
RETURN_ZVAL(z_dist, 1, 0);
}
PHP_METHOD(RedisArray, _rehash)
{
zval *object;
RedisArray *ra;
zend_fcall_info z_cb = {0};
zend_fcall_info_cache z_cb_cache = {0};
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|f",
&object, redis_array_ce, &z_cb, &z_cb_cache) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
if (ZEND_NUM_ARGS() == 0) {
ra_rehash(ra, NULL, NULL TSRMLS_CC);
} else {
ra_rehash(ra, &z_cb, &z_cb_cache TSRMLS_CC);
}
}
static void multihost_distribute(INTERNAL_FUNCTION_PARAMETERS, const char *method_name)
{
zval *object, z_fun;
int i;
RedisArray *ra;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O",
&object, redis_array_ce) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* prepare call */
ZVAL_STRING(&z_fun, method_name);
array_init(return_value);
for(i = 0; i < ra->count; ++i) {
zval zv, *z_tmp = &zv;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_tmp);
#endif
/* Call each node in turn */
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_fun, z_tmp, 0, NULL);
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
}
zval_dtor(&z_fun);
}
PHP_METHOD(RedisArray, info)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "INFO");
}
PHP_METHOD(RedisArray, ping)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "PING");
}
PHP_METHOD(RedisArray, flushdb)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHDB");
}
PHP_METHOD(RedisArray, flushall)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "FLUSHALL");
}
PHP_METHOD(RedisArray, save)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "SAVE");
}
PHP_METHOD(RedisArray, bgsave)
{
multihost_distribute(INTERNAL_FUNCTION_PARAM_PASSTHRU, "BGSAVE");
}
PHP_METHOD(RedisArray, keys)
{
zval *object, z_args[1], z_fun;
RedisArray *ra;
char *pattern;
strlen_t pattern_len;
int i;
/* Make sure the prototype is correct */
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, redis_array_ce, &pattern, &pattern_len) == FAILURE)
{
RETURN_FALSE;
}
/* Make sure we can grab our RedisArray object */
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* Set up our function call (KEYS) */
ZVAL_STRINGL(&z_fun, "KEYS", 4);
/* We will be passing with one string argument (the pattern) */
ZVAL_STRINGL(z_args, pattern, pattern_len);
/* Init our array return */
array_init(return_value);
/* Iterate our RedisArray nodes */
for(i = 0; i < ra->count; ++i) {
zval zv, *z_tmp = &zv;
#if (PHP_MAJOR_VERSION < 7)
/* Return for this node */
MAKE_STD_ZVAL(z_tmp);
#endif
/* Call KEYS on each node */
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_fun, z_tmp, 1, z_args);
/* Add the result for this host */
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
}
zval_dtor(&z_args[0]);
zval_dtor(&z_fun);
}
PHP_METHOD(RedisArray, getOption)
{
zval *object, z_fun, z_args[1];
int i;
RedisArray *ra;
zend_long opt;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
&object, redis_array_ce, &opt) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* prepare call */
ZVAL_STRINGL(&z_fun, "getOption", 9);
/* copy arg */
ZVAL_LONG(&z_args[0], opt);
array_init(return_value);
for(i = 0; i < ra->count; ++i) {
zval zv, *z_tmp = &zv;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_tmp);
#endif
/* Call each node in turn */
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_fun, z_tmp, 1, z_args);
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
}
zval_dtor(&z_fun);
}
PHP_METHOD(RedisArray, setOption)
{
zval *object, z_fun, z_args[2];
int i;
RedisArray *ra;
zend_long opt;
char *val_str;
strlen_t val_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ols",
&object, redis_array_ce, &opt, &val_str, &val_len) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* prepare call */
ZVAL_STRINGL(&z_fun, "setOption", 9);
/* copy args */
ZVAL_LONG(&z_args[0], opt);
ZVAL_STRINGL(&z_args[1], val_str, val_len);
array_init(return_value);
for(i = 0; i < ra->count; ++i) {
zval zv, *z_tmp = &zv;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_tmp);
#endif
/* Call each node in turn */
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_fun, z_tmp, 2, z_args);
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
}
zval_dtor(&z_args[1]);
zval_dtor(&z_fun);
}
PHP_METHOD(RedisArray, select)
{
zval *object, z_fun, z_args[1];
int i;
RedisArray *ra;
zend_long opt;
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol",
&object, redis_array_ce, &opt) == FAILURE) {
RETURN_FALSE;
}
if ((ra = redis_array_get(object TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* prepare call */
ZVAL_STRINGL(&z_fun, "select", 6);
/* copy args */
ZVAL_LONG(&z_args[0], opt);
array_init(return_value);
for(i = 0; i < ra->count; ++i) {
zval zv, *z_tmp = &zv;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_tmp);
#endif
/* Call each node in turn */
call_user_function(&redis_ce->function_table, &ra->redis[i], &z_fun, z_tmp, 1, z_args);
add_assoc_zval(return_value, ra->hosts[i], z_tmp);
}
zval_dtor(&z_fun);
}
#if (PHP_MAJOR_VERSION < 7)
#define HANDLE_MULTI_EXEC(ra, cmd, cmdlen) do { \
if (ra && ra->z_multi_exec) { \
int i, num_varargs;\
zval ***varargs = NULL, *z_arg_array; \
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O*",\
&object, redis_array_ce, &varargs, &num_varargs) == FAILURE) {\
RETURN_FALSE;\
}\
/* copy all args into a zval hash table */\
MAKE_STD_ZVAL(z_arg_array); \
array_init(z_arg_array);\
for(i = 0; i < num_varargs; ++i) {\
zval *z_tmp;\
MAKE_STD_ZVAL(z_tmp); \
ZVAL_ZVAL(z_tmp, *varargs[i], 1, 0); \
add_next_index_zval(z_arg_array, z_tmp); \
}\
/* call */\
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmdlen, z_arg_array, NULL); \
zval_ptr_dtor(&z_arg_array); \
if(varargs) {\
efree(varargs);\
}\
return;\
}\
}while(0)
#else
#define HANDLE_MULTI_EXEC(ra, cmd, cmdlen) do { \
if (ra && ra->z_multi_exec) { \
int i, num_varargs; \
zval *varargs = NULL, z_arg_array; \
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O*", \
&object, redis_array_ce, &varargs, &num_varargs) == FAILURE) { \
RETURN_FALSE;\
} \
/* copy all args into a zval hash table */\
array_init(&z_arg_array); \
for (i = 0; i < num_varargs; i++) { \
zval z_tmp; \
ZVAL_ZVAL(&z_tmp, &varargs[i], 1, 0); \
add_next_index_zval(&z_arg_array, &z_tmp); \
} \
/* call */\
ra_forward_call(INTERNAL_FUNCTION_PARAM_PASSTHRU, ra, cmd, cmdlen, &z_arg_array, NULL); \
zval_dtor(&z_arg_array); \
return; \
} \
} while(0)
#endif
/* MGET will distribute the call to several nodes and regroup the values. */
PHP_METHOD(RedisArray, mget)
{
zval *object, *z_keys, z_argarray, *data, z_ret, *z_cur, z_tmp_array, *z_tmp;
int i, j, n;
RedisArray *ra;
int *pos, argc, *argc_each;
HashTable *h_keys;
zval **argv;
if ((ra = redis_array_get(getThis() TSRMLS_CC)) == NULL) {
RETURN_FALSE;
}
/* Multi/exec support */
HANDLE_MULTI_EXEC(ra, "MGET", sizeof("MGET") - 1);
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
&object, redis_array_ce, &z_keys) == FAILURE) {
RETURN_FALSE;
}
/* init data structures */
h_keys = Z_ARRVAL_P(z_keys);
if ((argc = zend_hash_num_elements(h_keys)) == 0) {
RETURN_FALSE;
}
argv = emalloc(argc * sizeof(zval*));
pos = emalloc(argc * sizeof(int));
argc_each = emalloc(ra->count * sizeof(int));
memset(argc_each, 0, ra->count * sizeof(int));
/* associate each key to a redis node */
i = 0;
ZEND_HASH_FOREACH_VAL(h_keys, data) {
/* If we need to represent a long key as a string */
unsigned int key_len;
char kbuf[40], *key_lookup;
/* phpredis proper can only use string or long keys, so restrict to that here */
if (Z_TYPE_P(data) != IS_STRING && Z_TYPE_P(data) != IS_LONG) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "MGET: all keys must be strings or longs");
efree(argv);
efree(pos);
efree(argc_each);
RETURN_FALSE;
}
/* Convert to a string for hash lookup if it isn't one */
if (Z_TYPE_P(data) == IS_STRING) {
key_len = Z_STRLEN_P(data);
key_lookup = Z_STRVAL_P(data);
} else {
key_len = snprintf(kbuf, sizeof(kbuf), "%ld", Z_LVAL_P(data));
key_lookup = (char*)kbuf;
}
/* Find our node */
if (ra_find_node(ra, key_lookup, key_len, &pos[i] TSRMLS_CC) == NULL) {
/* TODO: handle */
}
argc_each[pos[i]]++; /* count number of keys per node */
argv[i++] = data;
} ZEND_HASH_FOREACH_END();
array_init(&z_tmp_array);
/* calls */
for(n = 0; n < ra->count; ++n) { /* for each node */
/* We don't even need to make a call to this node if no keys go there */
if(!argc_each[n]) continue;
/* copy args for MGET call on node. */
array_init(&z_argarray);
for(i = 0; i < argc; ++i) {
if(pos[i] != n) continue;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_tmp);
#else
zval zv;
z_tmp = &zv;
#endif
ZVAL_ZVAL(z_tmp, argv[i], 1, 0);
add_next_index_zval(&z_argarray, z_tmp);
}
zval z_fun;
/* prepare call */
ZVAL_STRINGL(&z_fun, "MGET", 4);
/* call MGET on the node */
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
zval_dtor(&z_fun);
/* cleanup args array */