Clacks/ClacksCache.pm

filter: Statementstmt 32.6% 49 / 150 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 47.0% 16 / 34 Podpod 100% 17 / 17 totaltotal 29.0% 82 / 282 CC 33 SCAR 29.8
CC
33
Cov
23%
reconnect
72.0
set
30.0
deref
30.0
CRAP 19.7
« Clacks/PostgreSQL2Clacks.pm ↑ Summary Clacks.pm »
1 package Net::Clacks::ClacksCache;
2 #---AUTOPRAGMASTART---
3 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw[carp croak confess cluck longmess shortmess];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our $VERSION = 37;
10 1 ▶use autodie qw( close );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Array::Contains;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Encode qw(is_utf8 encode_utf8 decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use builtin qw[true false is_bool];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶no warnings qw(experimental::builtin); ## no critic (TestingAndDebugging::ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
17 #---AUTOPRAGMAEND---
18  
19 1 ▶use Net::Clacks::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 ▶sub new($proto, %config) {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
24 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
25  
26 0 my $self = bless \%config, $class;
27  
28 # Connect on first use
29 #$self->reconnect();
30  
31 0 $self->{initfromhandle} = 0;
32  
33 0 ▶ if(!defined($self->{user})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"user"}) 0 0 0
34 0 croak("User not defined!");
35 }
36  
37 0 ▶ if(!defined($self->{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"password"}) 0 0 0
38 0 croak("Password not defined!");
39 }
40  
41  
42 0 return $self;
43 }
44  
45 ▶sub newFromHandle($proto, $clacks) {
Subroutine newFromHandle: not called CC=2 SCAR=17.9
Pod documented
46 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
47  
48 0 my $self = bless {}, $class;
49  
50 0 $self->{initfromhandle} = 1;
51 0 $self->{clacks} = $clacks;
52  
53 0 $self->extraInits(); # Hook for application specific inits
54  
55 0 return $self;
56 }
57  
58 ▶sub reconnect($self) {
Subroutine reconnect: not called CC=8 SCAR=42.8
Pod documented
59 0 ▶ return if($self->{initfromhandle});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"initfromhandle"} 0 0 0
60 0 ▶ return if(defined($self->{clacks}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"clacks"} 0 0 0
61  
62 0 my $clacks;
63 0 ▶ if(defined($self->{host}) && defined($self->{port})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'host'} and defined $self->{'port'}) { } 0 0 0
elsif (defined $self->{'socketpath'}) { } 0 0 0
Condition0%0/3
defined $self->{"host"} and defined $self->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"host"} and defined $self->{"port"}
defined $self->{"host"} defined $self->{"port"}
Truth table0%0/3
A and B
A= defined $self->{"host"}B= defined $self->{"port"}
ABresult
0X0
100
111
64 $clacks = Net::Clacks::Client->new($self->{host}, $self->{port},
65 $self->{user}, $self->{password},
66 0 ▶ $self->{APPNAME} . '/' . $VERSION, 0)
Branch: 1 branch
BranchTrueFalseTotal
$clacks = "Net::Clacks::Client"->new($self->{"host"}, $self->{"port"}, $self->{"user"}, $self->{"password"}, $self->{"APPNAME"} . '/' . $Net::Clacks::ClacksCache::VERSION, 0) or croak("Can't connect to Clacks server") 0 0 0
67 or croak("Can't connect to Clacks server");
68 } elsif(defined($self->{socketpath})) {
69 $clacks = Net::Clacks::Client->newSocket($self->{socketpath},
70 $self->{user}, $self->{password},
71 0 ▶ $self->{APPNAME} . '/' . $VERSION, 0)
Branch: 1 branch
BranchTrueFalseTotal
$clacks = "Net::Clacks::Client"->newSocket($self->{"socketpath"}, $self->{"user"}, $self->{"password"}, $self->{"APPNAME"} . '/' . $Net::Clacks::ClacksCache::VERSION, 0) or croak("Can't connect to Clacks server") 0 0 0
72 or croak("Can't connect to Clacks server");
73 } else {
74 0 croak("No valid connection configured. Don't know where to connect to!");
75 }
76 0 $self->{clacks} = $clacks;
77  
78 0 $self->{clacks}->disablePing(); # Webclient doesn't know when it is called again
79  
80 0 $self->set("VERSION::" . $self->{APPNAME}, $VERSION);
81  
82 0 $self->{clacks}->activate_memcached_compat;
83 0 $self->{clacks}->disablePing();
84  
85 0 $self->extraInits(); # Hook for application specific inits
86  
87 0 return;
88 }
89  
90 ▶sub extraInits($self) {
Subroutine extraInits: not called CC=1 SCAR=6.9
Pod documented
91 # Hook for application specific inits
92 0 return;
93 }
94  
95 ▶sub extraDestroys($self) {
Subroutine extraDestroys: not called CC=1 SCAR=6.9
Pod documented
96 # Hook for application specific destroys
97 0 return;
98 }
99  
100 ▶sub fastdisconnect($self) {
Subroutine fastdisconnect: not called CC=2 SCAR=17.9
Pod documented
101 0 ▶ return if(!defined($self->{clacks})); # never connected, nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"clacks"} 0 0 0
102 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
103 0 $self->{clacks}->fastdisconnect();
104 };
105  
106 0 return;
107 }
108  
109 ▶sub disconnect($self) {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
110 0 ▶ return if(!defined($self->{clacks})); # never connected, nothing to do
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"clacks"} 0 0 0
111 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
112 0 $self->{clacks}->disconnect();
113 };
114  
115 0 return;
116 }
117  
118 ▶sub DESTROY($self) {
Subroutine DESTROY: not called CC=3 SCAR=24.8
119 # During Perl's global destruction phase, package symbol tables are torn
120 # down in arbitrary order. By the time DESTROY runs here the inner
121 # Net::Clacks::Client (and IO::Socket::SSL, IO::Select, etc.) may already
122 # be unavailable. Skip cleanup entirely in that phase — the kernel closes
123 # any leftover FD on process exit and the server detects EOF.
124 0 ▶ return if(${^GLOBAL_PHASE} eq 'DESTRUCT');
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
125  
126 # Outside global destruction, do a *fast* close (no flush, no QUIT, no
127 # sleeps). DESTROY can run at moments where the graceful path is
128 # inappropriate (worker child unwinding after fork, exception
129 # propagation, local-block exit). Callers who want a graceful protocol-
130 # level close should call $cache->disconnect() explicitly first.
131 0 ▶ if(defined($self->{clacks})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"clacks"}) 0 0 0
132 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
133 0 $self->{clacks}->fastdisconnect();
134 };
135 }
136  
137 # Wrap extraDestroys too — a subclass override might throw during
138 # teardown, and we don't want that to escape DESTROY (Perl turns
139 # propagated exceptions out of DESTROY into "(in cleanup)" warnings).
140 0 eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
141 0 $self->extraDestroys();
142 };
143  
144 0 return;
145 }
146  
147 ▶sub get($self, $key) {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
148 0 $self->reconnect(); # Make sure we are connected
149  
150 0 $key = $self->sanitize_key($key);
151  
152 0 my $value = $self->{clacks}->retrieve($key);
153 0 ▶ return if(!defined($value));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
154  
155 0 ▶ if($value =~ /^PAGECAMELCLACKSYAMLB64\:(.+)/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^PAGECAMELCLACKSYAMLB64\:(.+)/ou) { } 0 0 0
elsif ($value =~ /^PAGECAMELCLACKSB64\:(.+)/ou) { } 0 0 0
156 0 $value = decode_base64($1);
157 0 $value = Load($value);
158 0 $value = $self->deref($value);
159 } elsif($value =~ /^PAGECAMELCLACKSB64\:(.+)/o) {
160 0 $value = decode_base64($1);
161 }
162 0 return $value;
163 }
164  
165 ▶sub set($self, $key, $data) { ## no critic (NamingConventions::ProhibitAmbiguousNames)
Subroutine set: not called CC=5 SCAR=34.0
Pod documented
166 0 $self->reconnect(); # Make sure we are connected
167  
168 0 $key = $self->sanitize_key($key);
169  
170 0 ▶ if(ref $data ne '') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data ne '') { } 0 0 0
elsif ($data =~ /^PAGECAMELCLACKSB64/ou) { } 0 0 0
elsif ($data =~ /\n/ou or $data =~ /\r/ou) { } 0 0 0
171 #$data = 'PAGECAMELCLACKSYAMLB64: ' . encode_base64(Dump($data), '');
172 0 $data = Dump($data);
173 0 $data = 'PAGECAMELCLACKSYAMLB64: ' . encode_base64($data, '');
174 } elsif($data =~ /^PAGECAMELCLACKSB64/o) {
175 # Already encoded? Clacks injection alert? Just don't store the thing...
176 0 return false;
177 ▶ } elsif($data =~ /\n/o || $data =~ /\r/o) {
Condition0%0/3
$data =~ /\n/ou or $data =~ /\r/ou
l!l&&r!l&&!r
000
MC/DC0%0/2
$data =~ /\n/ou or $data =~ /\r/ou
$data =~ /\n/ou $data =~ /\r/ou
Truth table0%0/3
A or B
A= $data =~ /\n/ouB= $data =~ /\r/ou
ABresult
1X1
011
000
178 0 $data = 'PAGECAMELCLACKSB64:' . encode_base64($data, '');
179 }
180  
181 0 $self->{clacks}->store($key, $data);
182  
183 0 return true;
184 }
185  
186 ▶sub delete($self, $key) { ## no critic(BuiltinHomonyms)
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
187 0 $self->reconnect(); # Make sure we are connected
188  
189 0 $key = $self->sanitize_key($key);
190  
191 0 $self->{clacks}->remove($key);
192 0 return true;
193 }
194  
195 0 ▶sub incr($self, $key, $stepsize = '') {
Subroutine incr: not called CC=4 SCAR=30.0
Pod documented
196 0 $self->reconnect(); # Make sure we are connected
197  
198 0 $key = $self->sanitize_key($key);
199  
200 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
201 0 $self->{clacks}->increment($key);
202 } else {
203 0 $self->{clacks}->increment($key, $stepsize);
204 }
205 0 return true;
206 }
207  
208 0 ▶sub decr($self, $key, $stepsize = '') {
Subroutine decr: not called CC=4 SCAR=30.0
Pod documented
209 0 $self->reconnect(); # Make sure we are connected
210  
211 0 $key = $self->sanitize_key($key);
212  
213 0 ▶ if(!defined($stepsize) || $stepsize eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $stepsize or $stepsize eq '') { } 0 0 0
Condition0%0/3
not defined $stepsize or $stepsize eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $stepsize or $stepsize eq ""
not defined $stepsize $stepsize eq ""
Truth table0%0/3
not(A) or B
A= not defined $stepsizeB= $stepsize eq ""
ABresult
1X1
011
000
214 0 $self->{clacks}->decrement($key);
215 } else {
216 0 $self->{clacks}->decrement($key, $stepsize);
217 }
218 0 return true;
219 }
220  
221 ▶sub clacks_set($self, $key, $data) {
Subroutine clacks_set: not called CC=1 SCAR=6.9
Pod documented
222 0 $self->reconnect(); # Make sure we are connected
223  
224 0 $key = $self->sanitize_key($key);
225  
226 0 $self->{clacks}->set($key, $data);
227  
228 0 return true;
229 }
230  
231 ▶sub clacks_notify($self, $key) {
Subroutine clacks_notify: not called CC=1 SCAR=6.9
Pod documented
232 0 $self->reconnect(); # Make sure we are connected
233  
234 0 $key = $self->sanitize_key($key);
235  
236 0 $self->{clacks}->notify($key);
237  
238 0 return true;
239 }
240  
241 ▶sub clacks_keylist($self) {
Subroutine clacks_keylist: not called CC=1 SCAR=6.9
Pod documented
242 0 $self->reconnect(); # Make sure we are connected
243  
244 0 return $self->{clacks}->keylist();
245 }
246  
247  
248 ▶sub sanitize_key($self, $key) {
Subroutine sanitize_key: not called CC=1 SCAR=6.9
Pod documented
249 # Certain chars are not allowed in keys for protocol reason.
250 # We handle this by substituting them with a tripple underline
251  
252 0 $key =~ s/\ /___/go;
253 0 $key =~ s/\=/___/go;
254  
255 0 return $key;
256 }
257  
258 ▶sub deref($self, $val) {
Subroutine deref: not called CC=5 SCAR=34.0
Pod documented
259 0 ▶ return if(!defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
260  
261 0 ▶ while(ref($val) eq "SCALAR" || ref($val) eq "REF") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "SCALAR" or ref $val eq "REF") 0 0 0
Condition0%0/3
ref $val eq "SCALAR" or ref $val eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $val eq "SCALAR" or ref $val eq "REF"
ref $val eq "SCALAR" ref $val eq "REF"
Truth table0%0/3
A or B
A= ref $val eq "SCALAR"B= ref $val eq "REF"
ABresult
1X1
011
000
262 0 $val = ${$val};
263 0 ▶ last if(!defined($val));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
264 }
265  
266 0 return $val;
267 }
268  
269 1 1;
270 __END__
« Clacks/PostgreSQL2Clacks.pm ↑ Summary Clacks.pm »