forked from php-memcached-dev/php-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_memcached.c
4599 lines (3908 loc) · 135 KB
/
php_memcached.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
/*
+----------------------------------------------------------------------+
| Copyright (c) 2009-2010 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. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $ Id: $ */
/* TODO
* - set LIBKETAMA_COMPATIBLE as the default?
* - fix unserialize(serialize($memc))
* - ability to set binary protocol for sessions
*/
#include "php_memcached.h"
#include "php_memcached_private.h"
#include "php_memcached_server.h"
#include "g_fmt.h"
#ifdef HAVE_MEMCACHED_SESSION
# include "php_memcached_session.h"
#endif
#ifdef HAVE_FASTLZ_H
#include <fastlz.h>
#else
#include "fastlz/fastlz.h"
#endif
#include <zlib.h>
#ifdef HAVE_JSON_API
# include "ext/json/php_json.h"
#endif
#ifdef HAVE_MEMCACHED_IGBINARY
# include "ext/igbinary/igbinary.h"
#endif
#ifdef HAVE_MEMCACHED_MSGPACK
# include "ext/msgpack/php_msgpack.h"
#endif
/*
* This is needed because PHP 5.3.[01] does not install JSON_parser.h by default. This
* constant will move into php_json.h in the future anyway.
*/
#ifndef JSON_PARSER_DEFAULT_DEPTH
#define JSON_PARSER_DEFAULT_DEPTH 512
#endif
/****************************************
Custom options
****************************************/
#define MEMC_OPT_COMPRESSION -1001
#define MEMC_OPT_PREFIX_KEY -1002
#define MEMC_OPT_SERIALIZER -1003
#define MEMC_OPT_COMPRESSION_TYPE -1004
#define MEMC_OPT_STORE_RETRY_COUNT -1005
/****************************************
Custom result codes
****************************************/
#define MEMC_RES_PAYLOAD_FAILURE -1001
/****************************************
Payload value flags
****************************************/
#define MEMC_CREATE_MASK(start, n_bits) (((1 << n_bits) - 1) << start)
#define MEMC_MASK_TYPE MEMC_CREATE_MASK(0, 4)
#define MEMC_MASK_INTERNAL MEMC_CREATE_MASK(4, 12)
#define MEMC_MASK_USER MEMC_CREATE_MASK(16, 16)
#define MEMC_VAL_GET_TYPE(flags) ((flags) & MEMC_MASK_TYPE)
#define MEMC_VAL_SET_TYPE(flags, type) ((flags) |= ((type) & MEMC_MASK_TYPE))
#define MEMC_VAL_IS_STRING 0
#define MEMC_VAL_IS_LONG 1
#define MEMC_VAL_IS_DOUBLE 2
#define MEMC_VAL_IS_BOOL 3
#define MEMC_VAL_IS_SERIALIZED 4
#define MEMC_VAL_IS_IGBINARY 5
#define MEMC_VAL_IS_JSON 6
#define MEMC_VAL_IS_MSGPACK 7
#define MEMC_VAL_COMPRESSED (1<<0)
#define MEMC_VAL_COMPRESSION_ZLIB (1<<1)
#define MEMC_VAL_COMPRESSION_FASTLZ (1<<2)
#define MEMC_VAL_GET_FLAGS(internal_flags) ((internal_flags & MEMC_MASK_INTERNAL) >> 4)
#define MEMC_VAL_SET_FLAG(internal_flags, internal_flag) ((internal_flags) |= ((internal_flag << 4) & MEMC_MASK_INTERNAL))
#define MEMC_VAL_HAS_FLAG(internal_flags, internal_flag) ((MEMC_VAL_GET_FLAGS(internal_flags) & internal_flag) == internal_flag)
#define MEMC_VAL_DEL_FLAG(internal_flags, internal_flag) internal_flags &= ~((internal_flag << 4) & MEMC_MASK_INTERNAL)
/****************************************
User-defined flags
****************************************/
#define MEMC_VAL_GET_USER_FLAGS(flags) ((flags & MEMC_MASK_USER) >> 16)
#define MEMC_VAL_SET_USER_FLAGS(flags, udf_flags) ((flags) |= ((udf_flags << 16) & MEMC_MASK_USER))
#define MEMC_VAL_USER_FLAGS_MAX ((1 << 16) - 1)
/****************************************
"get" operation flags
****************************************/
#define MEMC_GET_PRESERVE_ORDER (1<<0)
/****************************************
Helper macros
****************************************/
#define MEMC_METHOD_INIT_VARS \
zval* object = getThis(); \
php_memc_t* i_obj = NULL; \
struct memc_obj* m_obj = NULL;
#define MEMC_METHOD_FETCH_OBJECT \
i_obj = (php_memc_t *) zend_object_store_get_object( object TSRMLS_CC ); \
m_obj = i_obj->obj; \
if (!m_obj) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Memcached constructor was not called"); \
return; \
}
#ifndef DVAL_TO_LVAL
#ifdef _WIN64
# define DVAL_TO_LVAL(d, l) \
if ((d) > LONG_MAX) { \
(l) = (long)(unsigned long)(__int64) (d); \
} else { \
(l) = (long) (d); \
}
#else
# define DVAL_TO_LVAL(d, l) \
if ((d) > LONG_MAX) { \
(l) = (unsigned long) (d); \
} else { \
(l) = (long) (d); \
}
#endif
#endif
#define RETURN_FROM_GET RETURN_FALSE
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 3)
#define zend_parse_parameters_none() \
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "")
#endif
/****************************************
Structures and definitions
****************************************/
enum memcached_compression_type {
COMPRESSION_TYPE_ZLIB = 1,
COMPRESSION_TYPE_FASTLZ = 2,
};
typedef struct {
zend_object zo;
struct memc_obj {
memcached_st *memc;
zend_bool compression;
enum memcached_serializer serializer;
enum memcached_compression_type compression_type;
#if HAVE_MEMCACHED_SASL
zend_bool has_sasl_data;
#endif
long store_retry_count;
} *obj;
zend_bool is_persistent;
zend_bool is_pristine;
int rescode;
int memc_errno;
} php_memc_t;
#ifdef HAVE_MEMCACHED_PROTOCOL
typedef struct {
zend_object zo;
php_memc_proto_handler_t *handler;
} php_memc_server_t;
#endif
enum {
MEMC_OP_SET,
MEMC_OP_TOUCH,
MEMC_OP_ADD,
MEMC_OP_REPLACE,
MEMC_OP_APPEND,
MEMC_OP_PREPEND
};
static zend_class_entry *memcached_ce = NULL;
static zend_class_entry *memcached_exception_ce = NULL;
static zend_object_handlers memcached_object_handlers;
#ifdef HAVE_MEMCACHED_PROTOCOL
static zend_object_handlers memcached_server_object_handlers;
static zend_class_entry *memcached_server_ce = NULL;
#endif
struct callbackContext
{
zval *array;
zval *entry;
memcached_stat_st *stats; /* for use with functions that need stats */
void *return_value;
unsigned int i; /* for use with structures mapped against servers */
};
#if HAVE_SPL
static zend_class_entry *spl_ce_RuntimeException = NULL;
#endif
#if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3)
const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 };
#undef ZEND_BEGIN_ARG_INFO_EX
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args) \
static zend_arg_info name[] = { \
{ NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },
#endif
ZEND_DECLARE_MODULE_GLOBALS(php_memcached)
#ifdef COMPILE_DL_MEMCACHED
ZEND_GET_MODULE(memcached)
#endif
static PHP_INI_MH(OnUpdateCompressionType)
{
if (!new_value) {
MEMC_G(compression_type_real) = COMPRESSION_TYPE_FASTLZ;
} else if (!strcmp(new_value, "fastlz")) {
MEMC_G(compression_type_real) = COMPRESSION_TYPE_FASTLZ;
} else if (!strcmp(new_value, "zlib")) {
MEMC_G(compression_type_real) = COMPRESSION_TYPE_ZLIB;
} else {
return FAILURE;
}
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
}
static PHP_INI_MH(OnUpdateSerializer)
{
if (!new_value) {
MEMC_G(serializer) = SERIALIZER_DEFAULT;
} else if (!strcmp(new_value, "php")) {
MEMC_G(serializer) = SERIALIZER_PHP;
#ifdef HAVE_MEMCACHED_IGBINARY
} else if (!strcmp(new_value, "igbinary")) {
MEMC_G(serializer) = SERIALIZER_IGBINARY;
#endif // IGBINARY
#ifdef HAVE_JSON_API
} else if (!strcmp(new_value, "json")) {
MEMC_G(serializer) = SERIALIZER_JSON;
} else if (!strcmp(new_value, "json_array")) {
MEMC_G(serializer) = SERIALIZER_JSON_ARRAY;
#endif // JSON
#ifdef HAVE_MEMCACHED_MSGPACK
} else if (!strcmp(new_value, "msgpack")) {
MEMC_G(serializer) = SERIALIZER_MSGPACK;
#endif // msgpack
} else {
return FAILURE;
}
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
}
/* {{{ INI entries */
PHP_INI_BEGIN()
#ifdef HAVE_MEMCACHED_SESSION
STD_PHP_INI_ENTRY("memcached.sess_locking", "1", PHP_INI_ALL, OnUpdateBool, sess_locking_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_consistent_hash", "0", PHP_INI_ALL, OnUpdateBool, sess_consistent_hash_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_binary", "0", PHP_INI_ALL, OnUpdateBool, sess_binary_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_lock_wait", "150000", PHP_INI_ALL, OnUpdateLongGEZero,sess_lock_wait, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_lock_max_wait", "0", PHP_INI_ALL, OnUpdateLongGEZero, sess_lock_max_wait, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_lock_expire", "0", PHP_INI_ALL, OnUpdateLongGEZero, sess_lock_expire, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_prefix", "memc.sess.key.", PHP_INI_ALL, OnUpdateString, sess_prefix, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_number_of_replicas", "0", PHP_INI_ALL, OnUpdateLongGEZero, sess_number_of_replicas, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_randomize_replica_read", "0", PHP_INI_ALL, OnUpdateBool, sess_randomize_replica_read, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_remove_failed", "0", PHP_INI_ALL, OnUpdateBool, sess_remove_failed_enabled, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_connect_timeout", "1000", PHP_INI_ALL, OnUpdateLong, sess_connect_timeout, zend_php_memcached_globals, php_memcached_globals)
#if HAVE_MEMCACHED_SASL
STD_PHP_INI_ENTRY("memcached.sess_sasl_username", "", PHP_INI_ALL, OnUpdateString, sess_sasl_username, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.sess_sasl_password", "", PHP_INI_ALL, OnUpdateString, sess_sasl_password, zend_php_memcached_globals, php_memcached_globals)
#endif
#endif
STD_PHP_INI_ENTRY("memcached.compression_type", "fastlz", PHP_INI_ALL, OnUpdateCompressionType, compression_type, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.compression_factor", "1.3", PHP_INI_ALL, OnUpdateReal, compression_factor, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.compression_threshold", "2000", PHP_INI_ALL, OnUpdateLong, compression_threshold, zend_php_memcached_globals, php_memcached_globals)
STD_PHP_INI_ENTRY("memcached.serializer", SERIALIZER_DEFAULT_NAME, PHP_INI_ALL, OnUpdateSerializer, serializer_name, zend_php_memcached_globals, php_memcached_globals)
#if HAVE_MEMCACHED_SASL
STD_PHP_INI_ENTRY("memcached.use_sasl", "0", PHP_INI_SYSTEM, OnUpdateBool, use_sasl, zend_php_memcached_globals, php_memcached_globals)
#endif
STD_PHP_INI_ENTRY("memcached.store_retry_count", "2", PHP_INI_ALL, OnUpdateLong, store_retry_count, zend_php_memcached_globals, php_memcached_globals)
PHP_INI_END()
/* }}} */
/****************************************
Forward declarations
****************************************/
static int php_memc_handle_error(php_memc_t *i_obj, memcached_return status TSRMLS_DC);
static char *php_memc_zval_to_payload(zval *value, size_t *payload_len, uint32_t *flags, enum memcached_serializer serializer, enum memcached_compression_type compression_type TSRMLS_DC);
static int php_memc_zval_from_payload(zval *value, const char *payload, size_t payload_len, uint32_t flags, enum memcached_serializer serializer TSRMLS_DC);
static void php_memc_get_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_store_impl(INTERNAL_FUNCTION_PARAMETERS, int op, zend_bool by_key);
static void php_memc_setMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_delete_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_deleteMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static void php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key);
static memcached_return php_memc_do_cache_callback(zval *memc_obj, zend_fcall_info *fci, zend_fcall_info_cache *fcc, char *key, size_t key_len, zval *value TSRMLS_DC);
static int php_memc_do_result_callback(zval *memc_obj, zend_fcall_info *fci, zend_fcall_info_cache *fcc, memcached_result_st *result TSRMLS_DC);
static memcached_return php_memc_do_serverlist_callback(const memcached_st *ptr, php_memcached_instance_st instance, void *in_context);
static memcached_return php_memc_do_stats_callback(const memcached_st *ptr, php_memcached_instance_st instance, void *in_context);
static memcached_return php_memc_do_version_callback(const memcached_st *ptr, php_memcached_instance_st instance, void *in_context);
static void php_memc_destroy(struct memc_obj *m_obj, zend_bool persistent TSRMLS_DC);
/****************************************
Method implementations
****************************************/
char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC)
{
char *buffer = NULL;
if (fci->object_ptr) {
spprintf (&buffer, 0, "%s::%s", Z_OBJCE_P (fci->object_ptr)->name, fci_cache->function_handler->common.function_name);
} else {
if (Z_TYPE_P (fci->function_name) == IS_OBJECT) {
spprintf (&buffer, 0, "%s", Z_OBJCE_P (fci->function_name)->name);
}
else {
spprintf (&buffer, 0, "%s", Z_STRVAL_P (fci->function_name));
}
}
return buffer;
}
static zend_bool php_memcached_on_new_callback(zval *object, zend_fcall_info *fci, zend_fcall_info_cache *fci_cache, char *persistent_id, int persistent_id_len TSRMLS_DC)
{
zend_bool retval = 1;
zval pid_z;
zval *retval_ptr, *pid_z_ptr = &pid_z;
zval **params[2];
INIT_ZVAL(pid_z);
if (persistent_id) {
ZVAL_STRINGL(pid_z_ptr, persistent_id, persistent_id_len, 1);
}
/* Call the cb */
params[0] = &object;
params[1] = &pid_z_ptr;
fci->params = params;
fci->param_count = 2;
fci->retval_ptr_ptr = &retval_ptr;
fci->no_separation = 1;
if (zend_call_function(fci, fci_cache TSRMLS_CC) == FAILURE) {
char *buf = php_memc_printable_func (fci, fci_cache TSRMLS_CC);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to invoke 'on_new' callback %s()", buf);
efree (buf);
retval = 0;
}
zval_dtor(pid_z_ptr);
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}
return retval;
}
static int le_memc, le_memc_sess;
static int php_memc_list_entry(void)
{
return le_memc;
}
/* {{{ Memcached::__construct([string persistent_id[, callback on_new[, string connection_str]]]))
Creates a Memcached object, optionally using persistent memcache connection */
static PHP_METHOD(Memcached, __construct)
{
zval *object = getThis();
php_memc_t *i_obj;
struct memc_obj *m_obj = NULL;
char *persistent_id = NULL, *conn_str = NULL;
int persistent_id_len, conn_str_len;
zend_bool is_persistent = 0;
char *plist_key = NULL;
int plist_key_len = 0;
zend_fcall_info fci = {0};
zend_fcall_info_cache fci_cache;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!f!s", &persistent_id, &persistent_id_len, &fci, &fci_cache, &conn_str, &conn_str_len) == FAILURE) {
ZVAL_NULL(object);
return;
}
i_obj = (php_memc_t *) zend_object_store_get_object(object TSRMLS_CC);
i_obj->is_pristine = 0;
if (persistent_id && *persistent_id) {
zend_rsrc_list_entry *le = NULL;
is_persistent = 1;
plist_key_len = spprintf(&plist_key, 0, "memcached:id=%s", persistent_id);
plist_key_len += 1;
if (zend_hash_find(&EG(persistent_list), plist_key, plist_key_len, (void *)&le) == SUCCESS) {
if (le->type == php_memc_list_entry()) {
m_obj = (struct memc_obj *) le->ptr;
}
}
i_obj->obj = m_obj;
}
i_obj->is_persistent = is_persistent;
if (!m_obj) {
m_obj = pecalloc(1, sizeof(*m_obj), is_persistent);
if (m_obj == NULL) {
if (plist_key) {
efree(plist_key);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "out of memory: cannot allocate handle");
/* not reached */
}
if (conn_str) {
m_obj->memc = php_memc_create_str(conn_str, conn_str_len);
if (!m_obj->memc) {
char error_buffer[1024];
if (plist_key) {
efree(plist_key);
}
#ifdef HAVE_LIBMEMCACHED_CHECK_CONFIGURATION
if (libmemcached_check_configuration(conn_str, conn_str_len, error_buffer, sizeof(error_buffer)) != MEMCACHED_SUCCESS) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "configuration error %s", error_buffer);
} else {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not allocate libmemcached structure");
}
#else
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not allocate libmemcached structure");
#endif
/* not reached */
}
} else {
m_obj->memc = memcached_create(NULL);
if (m_obj->memc == NULL) {
if (plist_key) {
efree(plist_key);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not allocate libmemcached structure");
/* not reached */
}
}
m_obj->serializer = MEMC_G(serializer);
m_obj->compression_type = MEMC_G(compression_type_real);
m_obj->compression = 1;
m_obj->store_retry_count = MEMC_G(store_retry_count);
i_obj->obj = m_obj;
i_obj->is_pristine = 1;
if (fci.size) { /* will be 0 when not available */
if (!php_memcached_on_new_callback(object, &fci, &fci_cache, persistent_id, persistent_id_len TSRMLS_CC) || EG(exception)) {
/* error calling or exception thrown from callback */
if (plist_key) {
efree(plist_key);
}
i_obj->obj = NULL;
if (is_persistent) {
php_memc_destroy(m_obj, is_persistent TSRMLS_CC);
}
return;
}
}
if (is_persistent) {
zend_rsrc_list_entry le;
le.type = php_memc_list_entry();
le.ptr = m_obj;
if (zend_hash_update(&EG(persistent_list), (char *)plist_key,
plist_key_len, (void *)&le, sizeof(le), NULL) == FAILURE) {
if (plist_key) {
efree(plist_key);
}
php_error_docref(NULL TSRMLS_CC, E_ERROR, "could not register persistent entry");
/* not reached */
}
}
}
if (plist_key) {
efree(plist_key);
}
}
/* }}} */
/* {{{ Memcached::get(string key [, mixed callback [, double &cas_token [, int &udf_flags ] ] ])
Returns a value for the given key or false */
PHP_METHOD(Memcached, get)
{
php_memc_get_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Memcached::getByKey(string server_key, string key [, mixed callback [, double &cas_token [, int &udf_flags ] ] ])
Returns a value for key from the server identified by the server key or false */
PHP_METHOD(Memcached, getByKey)
{
php_memc_get_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ -- php_memc_get_impl */
static void php_memc_get_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key)
{
char *key = NULL;
int key_len = 0;
char *server_key = NULL;
int server_key_len = 0;
const char *payload = NULL;
size_t payload_len = 0;
uint32_t flags = 0;
uint64_t cas = 0;
const char* keys[1] = { NULL };
size_t key_lens[1] = { 0 };
zval *cas_token = NULL;
zval *udf_flags = NULL;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
memcached_result_st result;
memcached_return status = MEMCACHED_SUCCESS;
MEMC_METHOD_INIT_VARS;
if (by_key) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|f!zz", &server_key,
&server_key_len, &key, &key_len, &fci, &fcc, &cas_token, &udf_flags) == FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|f!zz", &key, &key_len,
&fci, &fcc, &cas_token, &udf_flags) == FAILURE) {
return;
}
}
MEMC_METHOD_FETCH_OBJECT;
i_obj->rescode = MEMCACHED_SUCCESS;
if (key_len == 0 || strchr(key, ' ')) {
i_obj->rescode = MEMCACHED_BAD_KEY_PROVIDED;
RETURN_FROM_GET;
}
keys[0] = key;
key_lens[0] = key_len;
uint64_t orig_cas_flag;
orig_cas_flag = memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS);
/*
* Enable CAS support, but only if it is currently disabled.
*/
if (cas_token && PZVAL_IS_REF(cas_token) && orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
}
status = memcached_mget_by_key(m_obj->memc, server_key, server_key_len, keys, key_lens, 1);
if (cas_token && PZVAL_IS_REF(cas_token) && orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, orig_cas_flag);
}
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
RETURN_FROM_GET;
}
status = MEMCACHED_SUCCESS;
memcached_result_create(m_obj->memc, &result);
if (memcached_fetch_result(m_obj->memc, &result, &status) == NULL) {
/* This is for historical reasons */
if (status == MEMCACHED_END)
status = MEMCACHED_NOTFOUND;
/*
* If the result wasn't found, and we have the read-through callback, invoke
* it to get the value. The CAS token will be 0, because we cannot generate it
* ourselves.
*/
if (cas_token) {
ZVAL_DOUBLE(cas_token, 0.0);
}
if (status == MEMCACHED_NOTFOUND && fci.size != 0) {
status = php_memc_do_cache_callback(getThis(), &fci, &fcc, key, key_len, return_value TSRMLS_CC);
}
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
memcached_result_free(&result);
RETURN_FROM_GET;
}
/* if we have a callback, all processing is done */
if (fci.size != 0) {
memcached_result_free(&result);
return;
}
}
/* Fetch all remaining results */
memcached_result_st dummy_result;
memcached_return dummy_status = MEMCACHED_SUCCESS;
memcached_result_create(m_obj->memc, &dummy_result);
while (memcached_fetch_result(m_obj->memc, &dummy_result, &dummy_status) != NULL) {}
memcached_result_free(&dummy_result);
payload = memcached_result_value(&result);
payload_len = memcached_result_length(&result);
flags = memcached_result_flags(&result);
if (cas_token) {
cas = memcached_result_cas(&result);
}
if (php_memc_zval_from_payload(return_value, payload, payload_len, flags, m_obj->serializer TSRMLS_CC) < 0) {
memcached_result_free(&result);
i_obj->rescode = MEMC_RES_PAYLOAD_FAILURE;
RETURN_FROM_GET;
}
if (cas_token) {
zval_dtor(cas_token);
ZVAL_DOUBLE(cas_token, (double)cas);
}
if (udf_flags) {
zval_dtor(udf_flags);
ZVAL_LONG(udf_flags, MEMC_VAL_GET_USER_FLAGS(flags));
}
memcached_result_free(&result);
}
/* }}} */
/* {{{ Memcached::getMulti(array keys [, array &cas_tokens [, array &udf_flags ] ])
Returns values for the given keys or false */
PHP_METHOD(Memcached, getMulti)
{
php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Memcached::getMultiByKey(string server_key, array keys [, array &cas_tokens [, array &udf_flags ] ])
Returns values for the given keys from the server identified by the server key or false */
PHP_METHOD(Memcached, getMultiByKey)
{
php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ -- php_memc_getMulti_impl */
static void php_memc_getMulti_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key)
{
zval *keys = NULL;
char *server_key = NULL;
int server_key_len = 0;
size_t num_keys = 0;
zval **entry = NULL;
const char *payload = NULL;
size_t payload_len = 0;
const char **mkeys = NULL;
size_t *mkeys_len = NULL;
const char *tmp_key = NULL;
size_t res_key_len = 0;
uint32_t flags;
uint64_t cas = 0;
zval *cas_tokens = NULL;
zval *udf_flags = NULL;
uint64_t orig_cas_flag = 0;
zval *value;
long get_flags = 0;
int i = 0;
zend_bool preserve_order;
memcached_result_st result;
memcached_return status = MEMCACHED_SUCCESS;
MEMC_METHOD_INIT_VARS;
if (by_key) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|zlz", &server_key,
&server_key_len, &keys, &cas_tokens, &get_flags, &udf_flags) == FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/|zlz", &keys, &cas_tokens, &get_flags, &udf_flags) == FAILURE) {
return;
}
}
MEMC_METHOD_FETCH_OBJECT;
i_obj->rescode = MEMCACHED_SUCCESS;
preserve_order = (get_flags & MEMC_GET_PRESERVE_ORDER);
num_keys = zend_hash_num_elements(Z_ARRVAL_P(keys));
mkeys = safe_emalloc(num_keys, sizeof(*mkeys), 0);
mkeys_len = safe_emalloc(num_keys, sizeof(*mkeys_len), 0);
array_init(return_value);
/*
* Create the array of keys for libmemcached. If none of the keys were valid
* (strings), set bad key result code and return.
*/
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(keys));
zend_hash_get_current_data(Z_ARRVAL_P(keys), (void**)&entry) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(keys))) {
if (Z_TYPE_PP(entry) != IS_STRING) {
convert_to_string_ex(entry);
}
if (Z_TYPE_PP(entry) == IS_STRING && Z_STRLEN_PP(entry) > 0) {
mkeys[i] = Z_STRVAL_PP(entry);
mkeys_len[i] = Z_STRLEN_PP(entry);
if (preserve_order) {
add_assoc_null_ex(return_value, mkeys[i], mkeys_len[i] + 1);
}
i++;
}
}
if (i == 0) {
i_obj->rescode = MEMCACHED_NOTFOUND;
efree(mkeys);
efree(mkeys_len);
return;
}
/*
* Enable CAS support, but only if it is currently disabled.
*/
if (cas_tokens && PZVAL_IS_REF(cas_tokens)) {
orig_cas_flag = memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS);
if (orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
}
}
status = memcached_mget_by_key(m_obj->memc, server_key, server_key_len, mkeys, mkeys_len, i);
/* Handle error, but ignore, there might still be some result */
php_memc_handle_error(i_obj, status TSRMLS_CC);
/*
* Restore the CAS support flag, but only if we had to turn it on.
*/
if (cas_tokens && PZVAL_IS_REF(cas_tokens) && orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, orig_cas_flag);
}
efree(mkeys);
efree(mkeys_len);
/*
* Iterate through the result set and create the result array. The CAS tokens are
* returned as doubles, because we cannot store potential 64-bit values in longs.
*/
if (cas_tokens) {
if (PZVAL_IS_REF(cas_tokens)) {
/* cas_tokens was passed by reference, we'll create an array for it. */
zval_dtor(cas_tokens);
array_init(cas_tokens);
} else {
/* Not passed by reference, we allow this (eg.: if you specify null
to not enable cas but you want to use the udf_flags parameter).
We destruct it and set it to null for the peace of mind. */
zval_dtor(cas_tokens);
cas_tokens = NULL;
}
}
/*
* Iterate through the result set and create the result array. The flags are
* returned as longs.
*/
if (udf_flags) {
zval_dtor(udf_flags);
array_init(udf_flags);
}
memcached_result_create(m_obj->memc, &result);
while ((memcached_fetch_result(m_obj->memc, &result, &status)) != NULL) {
char res_key [MEMCACHED_MAX_KEY];
if (status != MEMCACHED_SUCCESS) {
status = MEMCACHED_SOME_ERRORS;
php_memc_handle_error(i_obj, status TSRMLS_CC);
continue;
}
payload = memcached_result_value(&result);
payload_len = memcached_result_length(&result);
flags = memcached_result_flags(&result);
tmp_key = memcached_result_key_value(&result);
res_key_len = memcached_result_key_length(&result);
/*
* This may be a bug in libmemcached, the key is not null terminated
* whe using the binary protocol.
*/
memcpy (res_key, tmp_key, res_key_len >= MEMCACHED_MAX_KEY ? MEMCACHED_MAX_KEY - 1 : res_key_len);
res_key [res_key_len] = '\0';
ALLOC_INIT_ZVAL(value);
if (php_memc_zval_from_payload(value, payload, payload_len, flags, m_obj->serializer TSRMLS_CC) < 0) {
zval_ptr_dtor(&value);
if (EG(exception)) {
status = MEMC_RES_PAYLOAD_FAILURE;
php_memc_handle_error(i_obj, status TSRMLS_CC);
memcached_quit(m_obj->memc);
break;
}
status = MEMCACHED_SOME_ERRORS;
i_obj->rescode = MEMCACHED_SOME_ERRORS;
continue;
}
add_assoc_zval_ex(return_value, res_key, res_key_len+1, value);
if (cas_tokens) {
cas = memcached_result_cas(&result);
add_assoc_double_ex(cas_tokens, res_key, res_key_len+1, (double)cas);
}
if (udf_flags) {
add_assoc_long_ex(udf_flags, res_key, res_key_len+1, MEMC_VAL_GET_USER_FLAGS(flags));
}
}
memcached_result_free(&result);
if (EG(exception)) {
/* XXX: cas_tokens should only be set on success, currently we're destructive */
if (cas_tokens) {
zval_dtor(cas_tokens);
ZVAL_NULL(cas_tokens);
}
if (udf_flags) {
zval_dtor(udf_flags);
ZVAL_NULL(udf_flags);
}
zval_dtor(return_value);
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Memcached::getDelayed(array keys [, bool with_cas [, mixed callback ] ])
Sends a request for the given keys and returns immediately */
PHP_METHOD(Memcached, getDelayed)
{
php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Memcached::getDelayedByKey(string server_key, array keys [, bool with_cas [, mixed callback ] ])
Sends a request for the given keys from the server identified by the server key and returns immediately */
PHP_METHOD(Memcached, getDelayedByKey)
{
php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ -- php_memc_getDelayed_impl */
static void php_memc_getDelayed_impl(INTERNAL_FUNCTION_PARAMETERS, zend_bool by_key)
{
zval *keys = NULL;
char *server_key = NULL;
int server_key_len = 0;
zend_bool with_cas = 0;
size_t num_keys = 0;
zval **entry = NULL;
const char **mkeys = NULL;
size_t *mkeys_len = NULL;
uint64_t orig_cas_flag = 0;
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
int i = 0;
memcached_return status = MEMCACHED_SUCCESS;
MEMC_METHOD_INIT_VARS;
if (by_key) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa/|bf!", &server_key,
&server_key_len, &keys, &with_cas, &fci, &fcc) == FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/|bf!", &keys, &with_cas,
&fci, &fcc) == FAILURE) {
return;
}
}
MEMC_METHOD_FETCH_OBJECT;
i_obj->rescode = MEMCACHED_SUCCESS;
/*
* Create the array of keys for libmemcached. If none of the keys were valid
* (strings), set bad key result code and return.
*/
num_keys = zend_hash_num_elements(Z_ARRVAL_P(keys));
mkeys = safe_emalloc(num_keys, sizeof(*mkeys), 0);
mkeys_len = safe_emalloc(num_keys, sizeof(*mkeys_len), 0);
for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(keys));
zend_hash_get_current_data(Z_ARRVAL_P(keys), (void**)&entry) == SUCCESS;
zend_hash_move_forward(Z_ARRVAL_P(keys))) {
if (Z_TYPE_PP(entry) != IS_STRING) {
convert_to_string_ex(entry);
}
if (Z_TYPE_PP(entry) == IS_STRING && Z_STRLEN_PP(entry) > 0) {
mkeys[i] = Z_STRVAL_PP(entry);
mkeys_len[i] = Z_STRLEN_PP(entry);
i++;
}
}
if (i == 0) {
i_obj->rescode = MEMCACHED_BAD_KEY_PROVIDED;
efree(mkeys);
efree(mkeys_len);
zval_dtor(return_value);
RETURN_FALSE;
}
/*
* Enable CAS support, but only if it is currently disabled.
*/
if (with_cas) {
orig_cas_flag = memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS);
if (orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1);
}
}
/*
* Issue the request, but collect results only if the result callback is provided.
*/
status = memcached_mget_by_key(m_obj->memc, server_key, server_key_len, mkeys, mkeys_len, i);
/*
* Restore the CAS support flag, but only if we had to turn it on.
*/
if (with_cas && orig_cas_flag == 0) {
memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, orig_cas_flag);
}
efree(mkeys);
efree(mkeys_len);
if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) {
zval_dtor(return_value);
RETURN_FALSE;
}
if (fci.size != 0) {
/*
* We have a result callback. Iterate through the result set and invoke the
* callback for each one.
*/
memcached_result_st result;