-
Notifications
You must be signed in to change notification settings - Fork 10
/
Fast.xs
1548 lines (1358 loc) · 38.8 KB
/
Fast.xs
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) 2007-2010 Tomash Brechko. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8
or, at your option, any later version of Perl 5 you may have
available.
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "src/client.h"
#include <stdlib.h>
#include <string.h>
#define F_STORABLE 0x1
#define F_COMPRESS 0x2
#define F_UTF8 0x4
typedef struct
{
struct client *c;
AV *servers;
int compress_threshold;
double compress_ratio;
SV *compress_method;
SV *decompress_method;
SV *serialize_method;
SV *deserialize_method;
int utf8;
size_t max_size;
} Cache_Memcached_Fast;
static inline
SV**
safe_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
{
SV ** v = av_fetch(av, key, lval);
if ( !v || !SvOK(*v) )
croak("undefined value passed to av_fetch");
return v;
}
static
void
add_server(pTHX_ Cache_Memcached_Fast *memd, SV *addr_sv,
double weight, int noreply)
{
struct client *c = memd->c;
const char *host, *port;
size_t host_len, port_len;
STRLEN len;
int res;
av_push(memd->servers, newSVsv(addr_sv));
if (weight <= 0.0)
croak("Server weight should be positive");
host = SvPV(addr_sv, len);
/*
NOTE: here we relay on the fact that host is zero-terminated.
*/
port = strrchr(host, ':');
if (port)
{
host_len = port++ - host;
port_len = len - host_len - 1;
res = client_add_server(c, host, host_len, port, port_len,
weight, noreply);
}
else
{
res = client_add_server(c, host, len, NULL, 0, weight, noreply);
}
if (res != MEMCACHED_SUCCESS)
croak("Not enough memory");
}
static
void
parse_server(pTHX_ Cache_Memcached_Fast *memd, SV *sv)
{
if (! SvROK(sv))
{
add_server(aTHX_ memd, sv, 1.0, 0);
}
else
{
switch (SvTYPE(SvRV(sv)))
{
case SVt_PVHV:
{
HV *hv = (HV *) SvRV(sv);
SV **addr_sv, **ps;
double weight = 1.0;
int noreply = 0;
addr_sv = hv_fetchs(hv, "address", 0);
if (addr_sv)
SvGETMAGIC(*addr_sv);
else
croak("server should have { address => $addr }");
ps = hv_fetchs(hv, "weight", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
weight = SvNV(*ps);
ps = hv_fetchs(hv, "noreply", 0);
if (ps)
noreply = SvTRUE(*ps);
add_server(aTHX_ memd, *addr_sv, weight, noreply);
}
break;
case SVt_PVAV:
{
AV *av = (AV *) SvRV(sv);
SV **addr_sv, **weight_sv;
double weight = 1.0;
addr_sv = av_fetch(av, 0, 0);
if (addr_sv)
SvGETMAGIC(*addr_sv);
else
croak("server should be [$addr, $weight]");
weight_sv = av_fetch(av, 1, 0);
if (weight_sv)
weight = SvNV(*weight_sv);
add_server(aTHX_ memd, *addr_sv, weight, 0);
}
break;
default:
croak("Not a hash or array reference");
break;
}
}
}
static
void
parse_serialize(pTHX_ Cache_Memcached_Fast *memd, HV *conf)
{
SV **ps;
memd->utf8 = 0;
memd->serialize_method = NULL;
memd->deserialize_method = NULL;
ps = hv_fetchs(conf, "utf8", 0);
if (ps)
memd->utf8 = SvTRUE(*ps);
ps = hv_fetchs(conf, "serialize_methods", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
{
AV *av = (AV *) SvRV(*ps);
memd->serialize_method = newSVsv(*safe_av_fetch(aTHX_ av, 0, 0));
memd->deserialize_method = newSVsv(*safe_av_fetch(aTHX_ av, 1, 0));
}
if (! memd->serialize_method)
croak("Serialize method is not specified");
if (! memd->deserialize_method)
croak("Deserialize method is not specified");
}
static
void
parse_compress(pTHX_ Cache_Memcached_Fast *memd, HV *conf)
{
SV **ps;
memd->compress_threshold = -1;
memd->compress_ratio = 0.8;
memd->compress_method = NULL;
memd->decompress_method = NULL;
ps = hv_fetchs(conf, "compress_threshold", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
memd->compress_threshold = SvIV(*ps);
ps = hv_fetchs(conf, "compress_ratio", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
memd->compress_ratio = SvNV(*ps);
ps = hv_fetchs(conf, "compress_methods", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
{
AV *av = (AV *) SvRV(*ps);
memd->compress_method = newSVsv(*safe_av_fetch(aTHX_ av, 0, 0));
memd->decompress_method = newSVsv(*safe_av_fetch(aTHX_ av, 1, 0));
}
else if (memd->compress_threshold > 0)
{
warn("Compression module was not found, disabling compression");
memd->compress_threshold = -1;
}
}
static
void
parse_config(pTHX_ Cache_Memcached_Fast *memd, HV *conf)
{
struct client *c = memd->c;
SV **ps;
memd->servers = newAV();
ps = hv_fetchs(conf, "ketama_points", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
{
int res = client_set_ketama_points(c, SvIV(*ps));
if (res != MEMCACHED_SUCCESS)
croak("client_set_ketama() failed");
}
ps = hv_fetchs(conf, "hash_namespace", 0);
if (ps)
client_set_hash_namespace(c, SvTRUE(*ps));
ps = hv_fetchs(conf, "servers", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
{
AV *a;
int max_index, i;
if (! SvROK(*ps) || SvTYPE(SvRV(*ps)) != SVt_PVAV)
croak("Not an array reference");
a = (AV *) SvRV(*ps);
max_index = av_len(a);
for (i = 0; i <= max_index; ++i)
{
ps = av_fetch(a, i, 0);
if (! ps)
continue;
SvGETMAGIC(*ps);
parse_server(aTHX_ memd, *ps);
}
}
ps = hv_fetchs(conf, "namespace", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
{
const char *ns;
STRLEN len;
ns = SvPV(*ps, len);
if (client_set_prefix(c, ns, len) != MEMCACHED_SUCCESS)
croak("Not enough memory");
}
ps = hv_fetchs(conf, "connect_timeout", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
client_set_connect_timeout(c, SvNV(*ps) * 1000.0);
ps = hv_fetchs(conf, "io_timeout", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
client_set_io_timeout(c, SvNV(*ps) * 1000.0);
/* For compatibility with Cache::Memcached. */
ps = hv_fetchs(conf, "select_timeout", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
client_set_io_timeout(c, SvNV(*ps) * 1000.0);
ps = hv_fetchs(conf, "max_failures", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
client_set_max_failures(c, SvIV(*ps));
ps = hv_fetchs(conf, "failure_timeout", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
client_set_failure_timeout(c, SvIV(*ps));
ps = hv_fetchs(conf, "close_on_error", 0);
if (ps)
client_set_close_on_error(c, SvTRUE(*ps));
ps = hv_fetchs(conf, "nowait", 0);
if (ps)
client_set_nowait(c, SvTRUE(*ps));
ps = hv_fetchs(conf, "max_size", 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
memd->max_size = SvUV(*ps);
else
memd->max_size = 1024 * 1024;
parse_compress(aTHX_ memd, conf);
parse_serialize(aTHX_ memd, conf);
}
static inline
SV *
compress(pTHX_ Cache_Memcached_Fast *memd, SV *sv, flags_type *flags)
{
if (memd->compress_threshold > 0)
{
STRLEN len = sv_len(sv);
SV *csv, *bsv;
int count;
dSP;
if (len < (STRLEN) memd->compress_threshold)
return sv;
csv = newSV(0);
PUSHMARK(SP);
mXPUSHs(newRV_inc(sv));
mXPUSHs(newRV_noinc(csv));
PUTBACK;
count = call_sv(memd->compress_method, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Compress method returned nothing");
bsv = POPs;
if (SvTRUE(bsv) && sv_len(csv) <= len * memd->compress_ratio)
{
sv = csv;
*flags |= F_COMPRESS;
}
PUTBACK;
}
return sv;
}
static inline
int
decompress(pTHX_ Cache_Memcached_Fast *memd, SV **sv, flags_type flags)
{
int res = 1;
if (flags & F_COMPRESS)
{
SV *rsv, *bsv;
int count;
dSP;
rsv = newSV(0);
PUSHMARK(SP);
mXPUSHs(newRV_inc(*sv));
mXPUSHs(newRV_inc(rsv));
PUTBACK;
count = call_sv(memd->decompress_method, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Decompress method returned nothing");
bsv = POPs;
if (SvTRUE(bsv))
{
SvREFCNT_dec(*sv);
*sv = rsv;
}
else
{
SvREFCNT_dec(rsv);
res = 0;
}
PUTBACK;
}
return res;
}
static inline
SV *
serialize(pTHX_ Cache_Memcached_Fast *memd, SV *sv, flags_type *flags)
{
if (SvROK(sv))
{
int count;
dSP;
PUSHMARK(SP);
XPUSHs(sv);
PUTBACK;
count = call_sv(memd->serialize_method, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Serialize method returned nothing");
sv = POPs;
*flags |= F_STORABLE;
PUTBACK;
}
else if (SvUTF8(sv))
{
/* Copy the value because we will modify it in place. */
sv = sv_2mortal(newSVsv(sv));
if (memd->utf8)
{
sv_utf8_encode(sv);
*flags |= F_UTF8;
}
else
{
sv_utf8_downgrade(sv, 0);
}
}
return sv;
}
static inline
int
deserialize(pTHX_ Cache_Memcached_Fast *memd, SV **sv, flags_type flags)
{
int res = 1;
if (flags & F_STORABLE)
{
SV *rsv;
int count;
dSP;
PUSHMARK(SP);
XPUSHs(*sv);
PUTBACK;
/* FIXME: do we need G_KEPEERR here? */
count = call_sv(memd->deserialize_method, G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1)
croak("Deserialize method returned nothing");
rsv = POPs;
if (! SvTRUE(ERRSV))
{
SvREFCNT_dec(*sv);
*sv = SvREFCNT_inc(rsv);
}
else
{
res = 0;
}
PUTBACK;
}
else if ((flags & F_UTF8) && memd->utf8)
{
res = sv_utf8_decode(*sv);
}
return res;
}
static
void *
alloc_value(value_size_type value_size, void **opaque)
{
dTHX;
SV *sv;
char *res;
sv = newSVpvs("");
res = SvGROW(sv, value_size + 1); /* FIXME: check OOM. */
res[value_size] = '\0';
SvCUR_set(sv, value_size);
*opaque = sv;
return (void *) res;
}
static
void
free_value(void *opaque)
{
dTHX;
SV *sv = (SV *) opaque;
SvREFCNT_dec(sv);
}
struct xs_value_result
{
Cache_Memcached_Fast *memd;
SV *vals;
};
static
void
svalue_store(void *arg, void *opaque, int key_index PERL_UNUSED_DECL, void *meta)
{
dTHX;
SV *value_sv = (SV *) opaque;
struct xs_value_result *value_res = (struct xs_value_result *) arg;
struct meta_object *m = (struct meta_object *) meta;
if (! decompress(aTHX_ value_res->memd, &value_sv, m->flags)
|| ! deserialize(aTHX_ value_res->memd, &value_sv, m->flags))
{
free_value(value_sv);
return;
}
if (! m->use_cas)
{
value_res->vals = value_sv;
}
else
{
AV *cas_val = newAV();
av_extend(cas_val, 1);
av_push(cas_val, newSVuv(m->cas));
av_push(cas_val, value_sv);
value_res->vals = newRV_noinc((SV *) cas_val);
}
}
static
void
mvalue_store(void *arg, void *opaque, int key_index, void *meta)
{
dTHX;
SV *value_sv = (SV *) opaque;
struct xs_value_result *value_res = (struct xs_value_result *) arg;
struct meta_object *m = (struct meta_object *) meta;
if (! decompress(aTHX_ value_res->memd, &value_sv, m->flags)
|| ! deserialize(aTHX_ value_res->memd, &value_sv, m->flags))
{
free_value(value_sv);
return;
}
if (! m->use_cas)
{
av_store((AV *) value_res->vals, key_index, value_sv);
}
else
{
AV *cas_val = newAV();
av_extend(cas_val, 1);
av_push(cas_val, newSVuv(m->cas));
av_push(cas_val, value_sv);
av_store((AV *) value_res->vals, key_index, newRV_noinc((SV *) cas_val));
}
}
static
void
result_store(void *arg, void *opaque, int key_index, void *meta PERL_UNUSED_DECL)
{
dTHX;
AV *av = (AV *) arg;
int res = (ptrdiff_t) opaque;
av_store(av, key_index, res ? newSViv(res) : newSVpvs(""));
}
static
void
embedded_store(void *arg, void *opaque, int key_index, void *meta PERL_UNUSED_DECL)
{
dTHX;
AV *av = (AV *) arg;
SV *sv = (SV *) opaque;
av_store(av, key_index, sv);
}
/*
When SvPV() is called on a magic SV the result of mg_get() is cached
in PV slot. Since we pass around pointers to this storage we have
to avoid value refetch and reallocation that would happen if
mg_get() is called again. Because any magic SV may be put to the
argument list more than once we create a temporal copies of them,
thus braking possible ties and ensuring that every argument is
fetched exactly once.
*/
static inline
char *
SvPV_stable_storage(pTHX_ SV *sv, STRLEN *lp)
{
if (SvGAMAGIC(sv))
sv = sv_2mortal(newSVsv(sv));
return SvPV(sv, *lp);
}
MODULE = Cache::Memcached::Fast PACKAGE = Cache::Memcached::Fast
TYPEMAP: <<TYPEMAP
Cache_Memcached_Fast * T_CACHE_MEMCACHED_FAST
INPUT
T_CACHE_MEMCACHED_FAST
$var = INT2PTR($type, SvIVX(SvRV($arg)));
OUTPUT
T_CACHE_MEMCACHED_FAST
sv_setref_pv($arg, class, (void *) $var);
TYPEMAP
Cache_Memcached_Fast *
_new(char *class, SV *conf)
PROTOTYPE: $$
PREINIT:
Cache_Memcached_Fast *memd;
CODE:
Newx(memd, 1, Cache_Memcached_Fast);
memd->c = client_init();
if (! memd->c)
croak("Not enough memory");
if (! SvROK(conf) || SvTYPE(SvRV(conf)) != SVt_PVHV)
croak("Not a hash reference");
parse_config(aTHX_ memd, (HV *) SvRV(conf));
RETVAL = memd;
OUTPUT:
RETVAL
void
_destroy(Cache_Memcached_Fast *memd)
PROTOTYPE: $
CODE:
client_destroy(memd->c);
if (memd->compress_method)
{
SvREFCNT_dec(memd->compress_method);
SvREFCNT_dec(memd->decompress_method);
}
if (memd->serialize_method)
{
SvREFCNT_dec(memd->serialize_method);
SvREFCNT_dec(memd->deserialize_method);
}
SvREFCNT_dec(memd->servers);
Safefree(memd);
void
_weaken(SV *sv)
PROTOTYPE: $
CODE:
sv_rvweaken(sv);
void
enable_compress(Cache_Memcached_Fast *memd, bool enable)
PROTOTYPE: $$
CODE:
if (enable && ! memd->compress_method)
warn("Compression module was not found, can't enable compression");
else if ((memd->compress_threshold > 0) != enable)
memd->compress_threshold = -memd->compress_threshold;
void
set(Cache_Memcached_Fast *memd, ...)
ALIAS:
add = CMD_ADD
replace = CMD_REPLACE
append = CMD_APPEND
prepend = CMD_PREPEND
cas = CMD_CAS
PROTOTYPE: $@
PREINIT:
int noreply;
struct result_object object =
{ NULL, result_store, NULL, NULL };
const char *key;
STRLEN key_len;
cas_type cas = 0;
const void *buf;
STRLEN buf_len;
flags_type flags = 0;
exptime_type exptime = 0;
int arg = 1;
SV *sv;
PPCODE:
object.arg = newAV();
sv_2mortal((SV *) object.arg);
noreply = (GIMME_V == G_VOID);
client_reset(memd->c, &object, noreply);
key = SvPV_stable_storage(aTHX_ ST(arg), &key_len);
++arg;
if (ix == CMD_CAS)
{
cas = SvUV(ST(arg));
++arg;
}
sv = ST(arg);
++arg;
sv = serialize(aTHX_ memd, sv, &flags);
sv = compress(aTHX_ memd, sv, &flags);
buf = (void *) SvPV_stable_storage(aTHX_ sv, &buf_len);
if (buf_len > memd->max_size)
XSRETURN_EMPTY;
if (items > arg)
{
/* exptime doesn't have to be defined. */
sv = ST(arg);
SvGETMAGIC(sv);
if (SvOK(sv))
exptime = SvIV(sv);
}
if (ix != CMD_CAS)
{
client_prepare_set(memd->c, ix, 0, key, key_len, flags,
exptime, buf, buf_len);
}
else
{
client_prepare_cas(memd->c, 0, key, key_len, cas, flags,
exptime, buf, buf_len);
}
client_execute(memd->c, 2);
if (! noreply)
{
SV **val = av_fetch(object.arg, 0, 0);
if (val)
{
PUSHs(*val);
XSRETURN(1);
}
XSRETURN_EMPTY;
}
void
set_multi(Cache_Memcached_Fast *memd, ...)
ALIAS:
add_multi = CMD_ADD
replace_multi = CMD_REPLACE
append_multi = CMD_APPEND
prepend_multi = CMD_PREPEND
cas_multi = CMD_CAS
PROTOTYPE: $@
PREINIT:
int i, noreply;
struct result_object object =
{ NULL, result_store, NULL, NULL };
PPCODE:
object.arg = newAV();
sv_2mortal((SV *) object.arg);
noreply = (GIMME_V == G_VOID);
client_reset(memd->c, &object, noreply);
for (i = 1; i < items; ++i)
{
SV *sv;
AV *av;
const char *key;
STRLEN key_len;
/*
gcc-3.4.2 gives a warning about possibly uninitialized
cas, so we set it to zero.
*/
cas_type cas = 0;
const void *buf;
STRLEN buf_len;
flags_type flags = 0;
exptime_type exptime = 0;
int arg = 0;
sv = ST(i);
if (! (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV))
croak("Not an array reference");
av = (AV *) SvRV(sv);
key = SvPV_stable_storage(aTHX_ *safe_av_fetch(aTHX_ av, arg, 0), &key_len);
++arg;
if (ix == CMD_CAS)
{
cas = SvUV(*safe_av_fetch(aTHX_ av, arg, 0));
++arg;
}
sv = *safe_av_fetch(aTHX_ av, arg, 0);
++arg;
sv = serialize(aTHX_ memd, sv, &flags);
sv = compress(aTHX_ memd, sv, &flags);
buf = (void *) SvPV_stable_storage(aTHX_ sv, &buf_len);
if (buf_len > memd->max_size)
continue;
if (av_len(av) >= arg)
{
/* exptime doesn't have to be defined. */
SV **ps = av_fetch(av, arg, 0);
if (ps)
SvGETMAGIC(*ps);
if (ps && SvOK(*ps))
exptime = SvIV(*ps);
}
if (ix != CMD_CAS)
{
client_prepare_set(memd->c, ix, i - 1, key, key_len, flags,
exptime, buf, buf_len);
}
else
{
client_prepare_cas(memd->c, i - 1, key, key_len, cas, flags,
exptime, buf, buf_len);
}
}
client_execute(memd->c, 2);
if (! noreply)
{
if (GIMME_V == G_SCALAR)
{
HV *hv = newHV();
for (i = 0; i <= av_len(object.arg); ++i)
{
SV **val = av_fetch(object.arg, i, 0);
if (val && SvOK(*val))
{
SV *key = *av_fetch((AV *) SvRV(ST(i + 1)), 0, 0);
HE *he = hv_store_ent(hv, key,
SvREFCNT_inc(*val), 0);
if (! he)
SvREFCNT_dec(*val);
}
}
mPUSHs(newRV_noinc((SV *) hv));
XSRETURN(1);
}
else
{
I32 max_index = av_len(object.arg);
EXTEND(SP, max_index + 1);
for (i = 0; i <= max_index; ++i)
{
SV **val = av_fetch(object.arg, i, 0);
if (val)
PUSHs(*val);
else
PUSHs(&PL_sv_undef);
}
XSRETURN(max_index + 1);
}
}
void
get(Cache_Memcached_Fast *memd, ...)
ALIAS:
gets = CMD_GETS
PROTOTYPE: $@
PREINIT:
struct xs_value_result value_res;
struct result_object object =
{ alloc_value, svalue_store, free_value, &value_res };
const char *key;
STRLEN key_len;
PPCODE:
value_res.memd = memd;
value_res.vals = NULL;
client_reset(memd->c, &object, 0);
key = SvPV(ST(1), key_len);
client_prepare_get(memd->c, ix, 0, key, key_len);
client_execute(memd->c, 2);
if (value_res.vals)
{
mPUSHs(value_res.vals);
XSRETURN(1);
}
XSRETURN_EMPTY;
void
get_multi(Cache_Memcached_Fast *memd, ...)
ALIAS:
gets_multi = CMD_GETS
PROTOTYPE: $@
PREINIT:
struct xs_value_result value_res;
struct result_object object =
{ alloc_value, mvalue_store, free_value, &value_res };
int i, key_count;
HV *hv;
PPCODE:
key_count = items - 1;
value_res.memd = memd;
value_res.vals = (SV *) newAV();
sv_2mortal(value_res.vals);
av_extend((AV *) value_res.vals, key_count - 1);
client_reset(memd->c, &object, 0);
for (i = 0; i < key_count; ++i)
{
const char *key;
STRLEN key_len;
key = SvPV_stable_storage(aTHX_ ST(i + 1), &key_len);
client_prepare_get(memd->c, ix, i, key, key_len);
}
client_execute(memd->c, 2);
hv = newHV();
for (i = 0; i <= av_len((AV *) value_res.vals); ++i)
{
SV **val = av_fetch((AV *) value_res.vals, i, 0);
if (val && SvOK(*val))
{
SV *key = ST(i + 1);
HE *he = hv_store_ent(hv, key,
SvREFCNT_inc(*val), 0);
if (! he)
SvREFCNT_dec(*val);
}
}
mPUSHs(newRV_noinc((SV *) hv));
XSRETURN(1);
void
gat(Cache_Memcached_Fast *memd, ...)
ALIAS:
gats = CMD_GATS
PROTOTYPE: $@
PREINIT:
struct xs_value_result value_res;
struct result_object object =
{ alloc_value, svalue_store, free_value, &value_res };
const char *key;
STRLEN key_len;
const char *exptime = "0";
STRLEN exptime_len = 1;
SV *sv;
PPCODE:
value_res.memd = memd;
value_res.vals = NULL;
client_reset(memd->c, &object, 0);
sv = ST(1);
SvGETMAGIC(sv);
if (SvOK(sv))
exptime = SvPV(sv, exptime_len);