Shared/Hash.pm

filter: Statementstmt 38.1% 56 / 147 Branchbran 11.1% 6 / 54 Conditioncond 4.5% 1 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 27.5% 11 / 40 Podpod 100% 21 / 21 totaltotal 31.9% 95 / 297 CC 37 SCAR 23.7
CC
37
Cov
28%
keys
42.0
values
30.0
clone
20.0
CRAP 10.7
« Shared/Array.pm ↑ Summary Shared/Scalar.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Hash helper class.
4 ##
5 ###############################################################################
6  
7 package MCE::Shared::Hash;
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶no warnings qw( threads recursion uninitialized numeric );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 our $VERSION = '1.894';
17  
18 ## no critic (TestingAndDebugging::ProhibitNoStrict)
19  
20 4 ▶use MCE::Shared::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use base 'MCE::Shared::Common';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 q("") => \&MCE::Shared::Base::_stringify,
25 q(0+) => \&MCE::Shared::Base::_numify,
26 fallback => 1
27 4 );
28  
29 ###############################################################################
30 ## ----------------------------------------------------------------------------
31 ## Based on Tie::StdHash from Tie::Hash.
32 ##
33 ###############################################################################
34  
35 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=2 SCAR=17.9
36 0 my $self = bless {}, shift;
37 0 ▶ %{ $self } = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
38  
39 0 $self;
40 }
41  
42 0 ▶sub STORE { $_[0]->{ $_[1] } = $_[2] }
Subroutine STORE: not called CC=1 SCAR=6.9
43 0 ▶sub FETCH { $_[0]->{ $_[1] } }
Subroutine FETCH: not called CC=1 SCAR=6.9
44 0 ▶sub DELETE { delete $_[0]->{ $_[1] } }
Subroutine DELETE: not called CC=1 SCAR=6.9
45 0 ▶sub FIRSTKEY { my $a = keys %{ $_[0] }; each %{ $_[0] } }
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
46 0 ▶sub NEXTKEY { each %{ $_[0] } }
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
47 0 ▶sub EXISTS { exists $_[0]->{ $_[1] } }
Subroutine EXISTS: not called CC=1 SCAR=6.9
48 0 ▶sub CLEAR { %{ $_[0] } = () }
Subroutine CLEAR: not called CC=1 SCAR=6.9
49 0 ▶sub SCALAR { scalar keys %{ $_[0] } }
Subroutine SCALAR: not called CC=1 SCAR=6.9
50  
51 ###############################################################################
52 ## ----------------------------------------------------------------------------
53 ## _find, clone, flush, iterator, keys, pairs, values
54 ##
55 ###############################################################################
56  
57 # _find ( { getkeys => 1 }, "query string" )
58 # _find ( { getvals => 1 }, "query string" )
59 # _find ( "query string" ) # pairs
60  
61 0 ▶sub _find {
Subroutine _find: not called CC=2 SCAR=17.9
62 0 my $self = shift;
63 0 ▶ my $params = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
64 0 my $query = shift;
65  
66 0 MCE::Shared::Base::_find_hash( $self, $params, $query );
67 }
68  
69 # clone ( key [, key, ... ] )
70 # clone ( )
71  
72 0 ▶sub clone {
Subroutine clone: not called CC=4 SCAR=30.0
Pod documented
73 0 my $self = shift;
74 0 ▶ my $params = ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
75 0 my %data;
76  
77 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
78 0 @data{ @_ } = @{ $self }{ @_ };
79 }
80 else {
81 0 %data = %{ $self };
82 }
83  
84 0 ▶ $self->clear() if $params->{'flush'};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"flush"} 0 0 0
85  
86 0 bless \%data, ref $self;
87 }
88  
89 # flush ( key [, key, ... ] )
90 # flush ( )
91  
92 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
93 0 shift()->clone( { flush => 1 }, @_ );
94 }
95  
96 # iterator ( key [, key, ... ] )
97 # iterator ( "query string" )
98 # iterator ( )
99  
100 2 ▶sub iterator {
Subroutine iterator: called CC=4 SCAR=16.0
Pod documented
101 2 my ( $self, @keys ) = @_;
102  
103 2 ▶ if ( ! @keys ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @keys) { } 2 0 2
elsif (@keys == 1 and $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/) { } 0 0 0
104 2 @keys = CORE::keys %{ $self };
105 }
106 ▶ elsif ( @keys == 1 && $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/ ) {
Condition0%0/3
@keys == 1 and $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@keys == 1 and $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
@keys == 1 $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
Truth table0%0/3
A and B
A= @keys == 1B= $keys[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
ABresult
0X0
100
111
107 0 @keys = $self->keys($keys[0]);
108 }
109  
110 6 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
111 6 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 2 4 6
112 4 my $key = shift @keys;
113 4 return ( $key => $self->{ $key } );
114 2 };
115 }
116  
117 # keys ( key [, key, ... ] )
118 # keys ( "query string" )
119 # keys ( )
120  
121 0 ▶sub keys {
Subroutine keys: not called CC=6 SCAR=37.4
Pod documented
122 0 my $self = shift;
123  
124 0 ▶ if ( @_ == 1 && $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/) { } 0 0 0
elsif (wantarray) { } 0 0 0
Condition0%0/3
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
@_ == 1 $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
Truth table0%0/3
A and B
A= @_ == 1B= $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
ABresult
0X0
100
111
125 0 $self->_find({ getkeys => 1 }, @_);
126 }
127 elsif ( wantarray ) {
128 0 ▶ @_ ? map { exists $self->{ $_ } ? $_ : undef } @_
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$_} ? : 0 0 0
129 0 ▶ : CORE::keys %{ $self };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
130 }
131 else {
132 0 scalar CORE::keys %{ $self };
133 }
134 }
135  
136 # pairs ( key [, key, ... ] )
137 # pairs ( "query string" )
138 # pairs ( )
139  
140 3 ▶sub pairs {
Subroutine pairs: called CC=5 SCAR=21.6
Pod documented
141 3 my $self = shift;
142  
143 3 ▶ if ( @_ == 1 && $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/) { } 0 3 3
elsif (wantarray) { } 3 0 3
Condition33%1/3
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
!ll&&!rl&&r
300
MC/DC0%0/2
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
@_ == 1 $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
Truth table33%1/3
A and B
A= @_ == 1B= $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
ABresult
0X0
100
111
144 0 $self->_find(@_);
145 }
146 elsif ( wantarray ) {
147 0 @_ ? map { $_ => $self->{ $_ } } @_
148 3 ▶ : %{ $self };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
149 }
150 else {
151 0 scalar CORE::keys %{ $self };
152 }
153 }
154  
155 # values ( key [, key, ... ] )
156 # values ( "query string" )
157 # values ( )
158  
159 0 ▶sub values {
Subroutine values: not called CC=5 SCAR=34.0
Pod documented
160 0 my $self = shift;
161  
162 0 ▶ if ( @_ == 1 && $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/) { } 0 0 0
elsif (wantarray) { } 0 0 0
Condition0%0/3
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
@_ == 1 $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
Truth table0%0/3
A and B
A= @_ == 1B= $_[0] =~ /^(?:key|val)[ ]+\S\S?[ ]+\S/
ABresult
0X0
100
111
163 0 $self->_find({ getvals => 1 }, @_);
164 }
165 elsif ( wantarray ) {
166 0 @_ ? @{ $self }{ @_ }
167 0 ▶ : CORE::values %{ $self };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
168 }
169 else {
170 0 scalar CORE::keys %{ $self };
171 }
172 }
173  
174 ###############################################################################
175 ## ----------------------------------------------------------------------------
176 ## assign, mdel, mexists, mget, mset
177 ##
178 ###############################################################################
179  
180 # assign ( key, value [, key, value, ... ] )
181  
182 0 ▶sub assign {
Subroutine assign: not called CC=1 SCAR=6.9
Pod documented
183 0 $_[0]->clear; shift()->mset(@_);
184 }
185  
186 # mdel ( key [, key, ... ] )
187  
188 0 ▶sub mdel {
Subroutine mdel: not called CC=3 SCAR=24.8
Pod documented
189 0 my $self = shift;
190 0 my ( $cnt, $key ) = ( 0 );
191  
192 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
193 0 $key = shift;
194 0 ▶ $cnt++, delete($self->{ $key }) if ( exists $self->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 0 0 0
195 }
196  
197 0 $cnt;
198 }
199  
200 # mexists ( key [, key, ... ] )
201  
202 0 ▶sub mexists {
Subroutine mexists: not called CC=3 SCAR=24.8
Pod documented
203 0 my $self = shift;
204 0 my $key;
205  
206 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
207 0 $key = shift;
208 0 ▶ return '' unless ( exists $self->{ $key } );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 0 0 0
209 }
210  
211 0 1;
212 }
213  
214 # mget ( key [, key, ... ] )
215  
216 0 ▶sub mget {
Subroutine mget: not called CC=2 SCAR=17.9
Pod documented
217 0 my $self = shift;
218  
219 0 ▶ @_ ? @{ $self }{ @_ } : ();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
220 }
221  
222 # mset ( key, value [, key, value, ... ] )
223  
224 0 ▶sub mset {
Subroutine mset: not called CC=3 SCAR=24.8
Pod documented
225 0 my ( $self, $key ) = ( shift );
226  
227 0 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
228 0 $key = shift, $self->{ $key } = shift;
229 }
230  
231 0 ▶ defined wantarray ? scalar CORE::keys %{ $self } : ();
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
232 }
233  
234 ###############################################################################
235 ## ----------------------------------------------------------------------------
236 ## Sugar API, mostly resembles https://redis.io/commands#string primitives.
237 ##
238 ###############################################################################
239  
240 # append ( key, string )
241  
242 0 ▶sub append {
Subroutine append: not called CC=2 SCAR=17.9
Pod documented
243 0 ▶ length( $_[0]->{ $_[1] } .= $_[2] // '' );
Condition0%0/2
$_[2] // ""
l!l
00
MC/DC0%0/1
$_[2] // ""
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
244 }
245  
246 # decr ( key )
247 # decrby ( key, number )
248 # incr ( key )
249 # incrby ( key, number )
250 # getdecr ( key )
251 # getincr ( key )
252  
253 0 ▶sub decr { --$_[0]->{ $_[1] } }
Subroutine decr: not called CC=1 SCAR=6.9
Pod documented
254 0 ▶sub decrby { $_[0]->{ $_[1] } -= $_[2] || 0 }
Subroutine decrby: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
255 0 ▶sub incr { ++$_[0]->{ $_[1] } }
Subroutine incr: not called CC=1 SCAR=6.9
Pod documented
256 0 ▶sub incrby { $_[0]->{ $_[1] } += $_[2] || 0 }
Subroutine incrby: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[2] || 0
l!l
00
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
257 0 ▶sub getdecr { $_[0]->{ $_[1] }-- // 0 }
Subroutine getdecr: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{$_[1]}-- // 0
l!l
00
MC/DC0%0/1
$_[0]{$_[1]}-- // 0
$_[0]{$_[1]}--
Truth table0%0/2
A
A= $_[0]{$_[1]}--
Aresult
11
00
258 0 ▶sub getincr { $_[0]->{ $_[1] }++ // 0 }
Subroutine getincr: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{$_[1]}++ // 0
l!l
00
MC/DC0%0/1
$_[0]{$_[1]}++ // 0
$_[0]{$_[1]}++
Truth table0%0/2
A
A= $_[0]{$_[1]}++
Aresult
11
00
259  
260 # getset ( key, value )
261  
262 0 ▶sub getset {
Subroutine getset: not called CC=1 SCAR=6.9
Pod documented
263 0 my $old = $_[0]->{ $_[1] };
264 0 $_[0]->{ $_[1] } = $_[2];
265  
266 0 $old;
267 }
268  
269 # setnx ( key, value )
270  
271 0 ▶sub setnx {
Subroutine setnx: not called CC=2 SCAR=17.9
Pod documented
272 0 ▶ return 0 if ( exists $_[0]->{ $_[1] } );
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{$_[1]} 0 0 0
273 0 $_[0]->{ $_[1] } = $_[2];
274  
275 0 1;
276 }
277  
278 # len ( key )
279 # len ( )
280  
281 0 ▶sub len {
Subroutine len: not called CC=2 SCAR=17.9
Pod documented
282 ( defined $_[1] )
283 ? length $_[0]->{ $_[1] }
284 0 ▶ : scalar CORE::keys %{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
285 }
286  
287 {
288 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
289  
290 4 *{ __PACKAGE__.'::new' } = \&TIEHASH;
291 4 *{ __PACKAGE__.'::set' } = \&STORE;
292 4 *{ __PACKAGE__.'::get' } = \&FETCH;
293 4 *{ __PACKAGE__.'::delete' } = \&DELETE;
294 4 *{ __PACKAGE__.'::exists' } = \&EXISTS;
295 4 *{ __PACKAGE__.'::clear' } = \&CLEAR;
296 4 *{ __PACKAGE__.'::del' } = \&delete;
297 4 *{ __PACKAGE__.'::merge' } = \&mset;
298 4 *{ __PACKAGE__.'::vals' } = \&values;
299 }
300  
301 4 1;
302  
303 __END__
« Shared/Array.pm ↑ Summary Shared/Scalar.pm »